xref: /freebsd/sys/netinet/sctp_indata.c (revision 60990c0c060ca786f943cc978d4be566a1939d50)
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)) {
33177acdc25SRandall Stewart 		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;
610f8829a4aSRandall Stewart 		sctp_abort_an_association(stcb->sctp_ep, stcb,
611ceaad40aSRandall Stewart 		    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
612f8829a4aSRandall Stewart 
613f8829a4aSRandall Stewart 		*abort_flag = 1;
614f8829a4aSRandall Stewart 		return;
615f8829a4aSRandall Stewart 
616f8829a4aSRandall Stewart 	}
617f8829a4aSRandall Stewart 	if (nxt_todel == control->sinfo_ssn) {
618f8829a4aSRandall Stewart 		/* can be delivered right away? */
619b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
620f8829a4aSRandall Stewart 			sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_IMMED_DEL);
62180fefe0aSRandall Stewart 		}
622830d754dSRandall Stewart 		/* EY it wont be queued if it could be delivered directly */
623f8829a4aSRandall Stewart 		queue_needed = 0;
624f8829a4aSRandall Stewart 		asoc->size_on_all_streams -= control->length;
625f8829a4aSRandall Stewart 		sctp_ucount_decr(asoc->cnt_on_all_streams);
626f8829a4aSRandall Stewart 		strm->last_sequence_delivered++;
62777acdc25SRandall Stewart 
628b5c16493SMichael Tuexen 		sctp_mark_non_revokable(asoc, control->sinfo_tsn);
629f8829a4aSRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb,
630f8829a4aSRandall Stewart 		    control,
631cfde3ff7SRandall Stewart 		    &stcb->sctp_socket->so_rcv, 1,
632cfde3ff7SRandall Stewart 		    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
6334a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(control, &strm->inqueue, next, at) {
634f8829a4aSRandall Stewart 			/* all delivered */
635f8829a4aSRandall Stewart 			nxt_todel = strm->last_sequence_delivered + 1;
636f8829a4aSRandall Stewart 			if (nxt_todel == control->sinfo_ssn) {
637f8829a4aSRandall Stewart 				TAILQ_REMOVE(&strm->inqueue, control, next);
638f8829a4aSRandall Stewart 				asoc->size_on_all_streams -= control->length;
639f8829a4aSRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_all_streams);
640f8829a4aSRandall Stewart 				strm->last_sequence_delivered++;
641f8829a4aSRandall Stewart 				/*
642f8829a4aSRandall Stewart 				 * We ignore the return of deliver_data here
643f8829a4aSRandall Stewart 				 * since we always can hold the chunk on the
644f8829a4aSRandall Stewart 				 * d-queue. And we have a finite number that
645f8829a4aSRandall Stewart 				 * can be delivered from the strq.
646f8829a4aSRandall Stewart 				 */
647b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
648f8829a4aSRandall Stewart 					sctp_log_strm_del(control, NULL,
649f8829a4aSRandall Stewart 					    SCTP_STR_LOG_FROM_IMMED_DEL);
65080fefe0aSRandall Stewart 				}
651b5c16493SMichael Tuexen 				sctp_mark_non_revokable(asoc, control->sinfo_tsn);
652f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
653f8829a4aSRandall Stewart 				    control,
654cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1,
655cfde3ff7SRandall Stewart 				    SCTP_READ_LOCK_NOT_HELD,
656cfde3ff7SRandall Stewart 				    SCTP_SO_NOT_LOCKED);
657f8829a4aSRandall Stewart 				continue;
658f8829a4aSRandall Stewart 			}
659f8829a4aSRandall Stewart 			break;
660f8829a4aSRandall Stewart 		}
661f8829a4aSRandall Stewart 	}
662f8829a4aSRandall Stewart 	if (queue_needed) {
663f8829a4aSRandall Stewart 		/*
664f8829a4aSRandall Stewart 		 * Ok, we did not deliver this guy, find the correct place
665f8829a4aSRandall Stewart 		 * to put it on the queue.
666f8829a4aSRandall Stewart 		 */
66720b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(asoc->cumulative_tsn, control->sinfo_tsn)) {
668c40e9cf2SRandall Stewart 			goto protocol_error;
669c40e9cf2SRandall Stewart 		}
670f8829a4aSRandall Stewart 		if (TAILQ_EMPTY(&strm->inqueue)) {
671f8829a4aSRandall Stewart 			/* Empty queue */
672b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
673f8829a4aSRandall Stewart 				sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INSERT_HD);
67480fefe0aSRandall Stewart 			}
675f8829a4aSRandall Stewart 			TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
676f8829a4aSRandall Stewart 		} else {
677f8829a4aSRandall Stewart 			TAILQ_FOREACH(at, &strm->inqueue, next) {
67820b07a4dSMichael Tuexen 				if (SCTP_SSN_GT(at->sinfo_ssn, control->sinfo_ssn)) {
679f8829a4aSRandall Stewart 					/*
680f8829a4aSRandall Stewart 					 * one in queue is bigger than the
681f8829a4aSRandall Stewart 					 * new one, insert before this one
682f8829a4aSRandall Stewart 					 */
683b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
684f8829a4aSRandall Stewart 						sctp_log_strm_del(control, at,
685f8829a4aSRandall Stewart 						    SCTP_STR_LOG_FROM_INSERT_MD);
68680fefe0aSRandall Stewart 					}
687f8829a4aSRandall Stewart 					TAILQ_INSERT_BEFORE(at, control, next);
688f8829a4aSRandall Stewart 					break;
689f8829a4aSRandall Stewart 				} else if (at->sinfo_ssn == control->sinfo_ssn) {
690f8829a4aSRandall Stewart 					/*
691f8829a4aSRandall Stewart 					 * Gak, He sent me a duplicate str
692f8829a4aSRandall Stewart 					 * seq number
693f8829a4aSRandall Stewart 					 */
694f8829a4aSRandall Stewart 					/*
695f8829a4aSRandall Stewart 					 * foo bar, I guess I will just free
696f8829a4aSRandall Stewart 					 * this new guy, should we abort
697f8829a4aSRandall Stewart 					 * too? FIX ME MAYBE? Or it COULD be
698f8829a4aSRandall Stewart 					 * that the SSN's have wrapped.
699f8829a4aSRandall Stewart 					 * Maybe I should compare to TSN
700f8829a4aSRandall Stewart 					 * somehow... sigh for now just blow
701f8829a4aSRandall Stewart 					 * away the chunk!
702f8829a4aSRandall Stewart 					 */
703f8829a4aSRandall Stewart 
704f8829a4aSRandall Stewart 					if (control->data)
705f8829a4aSRandall Stewart 						sctp_m_freem(control->data);
706f8829a4aSRandall Stewart 					control->data = NULL;
707f8829a4aSRandall Stewart 					asoc->size_on_all_streams -= control->length;
708f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->cnt_on_all_streams);
70924f52bbdSMichael Tuexen 					if (control->whoFrom) {
710f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
7115bead436SRandall Stewart 						control->whoFrom = NULL;
71224f52bbdSMichael Tuexen 					}
713f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
714f8829a4aSRandall Stewart 					return;
715f8829a4aSRandall Stewart 				} else {
716f8829a4aSRandall Stewart 					if (TAILQ_NEXT(at, next) == NULL) {
717f8829a4aSRandall Stewart 						/*
718f8829a4aSRandall Stewart 						 * We are at the end, insert
719f8829a4aSRandall Stewart 						 * it after this one
720f8829a4aSRandall Stewart 						 */
721b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
722f8829a4aSRandall Stewart 							sctp_log_strm_del(control, at,
723f8829a4aSRandall Stewart 							    SCTP_STR_LOG_FROM_INSERT_TL);
72480fefe0aSRandall Stewart 						}
725f8829a4aSRandall Stewart 						TAILQ_INSERT_AFTER(&strm->inqueue,
726f8829a4aSRandall Stewart 						    at, control, next);
727f8829a4aSRandall Stewart 						break;
728f8829a4aSRandall Stewart 					}
729f8829a4aSRandall Stewart 				}
730f8829a4aSRandall Stewart 			}
731f8829a4aSRandall Stewart 		}
732f8829a4aSRandall Stewart 	}
733f8829a4aSRandall Stewart }
734f8829a4aSRandall Stewart 
735f8829a4aSRandall Stewart /*
736f8829a4aSRandall Stewart  * Returns two things: You get the total size of the deliverable parts of the
737f8829a4aSRandall Stewart  * first fragmented message on the reassembly queue. And you get a 1 back if
738f8829a4aSRandall Stewart  * all of the message is ready or a 0 back if the message is still incomplete
739f8829a4aSRandall Stewart  */
740f8829a4aSRandall Stewart static int
741f8829a4aSRandall Stewart sctp_is_all_msg_on_reasm(struct sctp_association *asoc, uint32_t * t_size)
742f8829a4aSRandall Stewart {
743f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
744f8829a4aSRandall Stewart 	uint32_t tsn;
745f8829a4aSRandall Stewart 
746f8829a4aSRandall Stewart 	*t_size = 0;
747f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
748f8829a4aSRandall Stewart 	if (chk == NULL) {
749f8829a4aSRandall Stewart 		/* nothing on the queue */
750f8829a4aSRandall Stewart 		return (0);
751f8829a4aSRandall Stewart 	}
752f8829a4aSRandall Stewart 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
753f8829a4aSRandall Stewart 		/* Not a first on the queue */
754f8829a4aSRandall Stewart 		return (0);
755f8829a4aSRandall Stewart 	}
756f8829a4aSRandall Stewart 	tsn = chk->rec.data.TSN_seq;
7574a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(chk, &asoc->reasmqueue, sctp_next) {
758f8829a4aSRandall Stewart 		if (tsn != chk->rec.data.TSN_seq) {
759f8829a4aSRandall Stewart 			return (0);
760f8829a4aSRandall Stewart 		}
761f8829a4aSRandall Stewart 		*t_size += chk->send_size;
762f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
763f8829a4aSRandall Stewart 			return (1);
764f8829a4aSRandall Stewart 		}
765f8829a4aSRandall Stewart 		tsn++;
766f8829a4aSRandall Stewart 	}
767f8829a4aSRandall Stewart 	return (0);
768f8829a4aSRandall Stewart }
769f8829a4aSRandall Stewart 
770f8829a4aSRandall Stewart static void
771f8829a4aSRandall Stewart sctp_deliver_reasm_check(struct sctp_tcb *stcb, struct sctp_association *asoc)
772f8829a4aSRandall Stewart {
773f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
774f8829a4aSRandall Stewart 	uint16_t nxt_todel;
775810ec536SMichael Tuexen 	uint32_t tsize, pd_point;
776f8829a4aSRandall Stewart 
777139bc87fSRandall Stewart doit_again:
778f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
779f8829a4aSRandall Stewart 	if (chk == NULL) {
780f8829a4aSRandall Stewart 		/* Huh? */
781f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = 0;
782f8829a4aSRandall Stewart 		asoc->cnt_on_reasm_queue = 0;
783f8829a4aSRandall Stewart 		return;
784f8829a4aSRandall Stewart 	}
785f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress == 0) {
786f8829a4aSRandall Stewart 		nxt_todel =
787f8829a4aSRandall Stewart 		    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
788f8829a4aSRandall Stewart 		if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
789f8829a4aSRandall Stewart 		    (nxt_todel == chk->rec.data.stream_seq ||
790f8829a4aSRandall Stewart 		    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
791f8829a4aSRandall Stewart 			/*
792f8829a4aSRandall Stewart 			 * Yep the first one is here and its ok to deliver
793f8829a4aSRandall Stewart 			 * but should we?
794f8829a4aSRandall Stewart 			 */
795810ec536SMichael Tuexen 			if (stcb->sctp_socket) {
79624ae5c4aSMichael Tuexen 				pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket),
797810ec536SMichael Tuexen 				    stcb->sctp_ep->partial_delivery_point);
798810ec536SMichael Tuexen 			} else {
799810ec536SMichael Tuexen 				pd_point = stcb->sctp_ep->partial_delivery_point;
800810ec536SMichael Tuexen 			}
801810ec536SMichael Tuexen 			if (sctp_is_all_msg_on_reasm(asoc, &tsize) || (tsize >= pd_point)) {
802f8829a4aSRandall Stewart 
803f8829a4aSRandall Stewart 				/*
804f8829a4aSRandall Stewart 				 * Yes, we setup to start reception, by
805f8829a4aSRandall Stewart 				 * backing down the TSN just in case we
806f8829a4aSRandall Stewart 				 * can't deliver. If we
807f8829a4aSRandall Stewart 				 */
808f8829a4aSRandall Stewart 				asoc->fragmented_delivery_inprogress = 1;
809f8829a4aSRandall Stewart 				asoc->tsn_last_delivered =
810f8829a4aSRandall Stewart 				    chk->rec.data.TSN_seq - 1;
811f8829a4aSRandall Stewart 				asoc->str_of_pdapi =
812f8829a4aSRandall Stewart 				    chk->rec.data.stream_number;
813f8829a4aSRandall Stewart 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
814f8829a4aSRandall Stewart 				asoc->pdapi_ppid = chk->rec.data.payloadtype;
815f8829a4aSRandall Stewart 				asoc->fragment_flags = chk->rec.data.rcv_flags;
816f8829a4aSRandall Stewart 				sctp_service_reassembly(stcb, asoc);
817f8829a4aSRandall Stewart 			}
818f8829a4aSRandall Stewart 		}
819f8829a4aSRandall Stewart 	} else {
820139bc87fSRandall Stewart 		/*
821139bc87fSRandall Stewart 		 * Service re-assembly will deliver stream data queued at
822139bc87fSRandall Stewart 		 * the end of fragmented delivery.. but it wont know to go
823139bc87fSRandall Stewart 		 * back and call itself again... we do that here with the
824139bc87fSRandall Stewart 		 * got doit_again
825139bc87fSRandall Stewart 		 */
826f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
827139bc87fSRandall Stewart 		if (asoc->fragmented_delivery_inprogress == 0) {
828139bc87fSRandall Stewart 			/*
829139bc87fSRandall Stewart 			 * finished our Fragmented delivery, could be more
830139bc87fSRandall Stewart 			 * waiting?
831139bc87fSRandall Stewart 			 */
832139bc87fSRandall Stewart 			goto doit_again;
833139bc87fSRandall Stewart 		}
834f8829a4aSRandall Stewart 	}
835f8829a4aSRandall Stewart }
836f8829a4aSRandall Stewart 
837f8829a4aSRandall Stewart /*
838f8829a4aSRandall Stewart  * Dump onto the re-assembly queue, in its proper place. After dumping on the
839f8829a4aSRandall Stewart  * queue, see if anthing can be delivered. If so pull it off (or as much as
840f8829a4aSRandall Stewart  * we can. If we run out of space then we must dump what we can and set the
841f8829a4aSRandall Stewart  * appropriate flag to say we queued what we could.
842f8829a4aSRandall Stewart  */
843f8829a4aSRandall Stewart static void
844f8829a4aSRandall Stewart sctp_queue_data_for_reasm(struct sctp_tcb *stcb, struct sctp_association *asoc,
845f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk, int *abort_flag)
846f8829a4aSRandall Stewart {
847f8829a4aSRandall Stewart 	struct mbuf *oper;
848*60990c0cSMichael Tuexen 	uint32_t cum_ackp1, prev_tsn, post_tsn;
849f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *at, *prev, *next;
850f8829a4aSRandall Stewart 
851f8829a4aSRandall Stewart 	prev = next = NULL;
852f8829a4aSRandall Stewart 	cum_ackp1 = asoc->tsn_last_delivered + 1;
853f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->reasmqueue)) {
854f8829a4aSRandall Stewart 		/* This is the first one on the queue */
855f8829a4aSRandall Stewart 		TAILQ_INSERT_HEAD(&asoc->reasmqueue, chk, sctp_next);
856f8829a4aSRandall Stewart 		/*
857f8829a4aSRandall Stewart 		 * we do not check for delivery of anything when only one
858f8829a4aSRandall Stewart 		 * fragment is here
859f8829a4aSRandall Stewart 		 */
860f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = chk->send_size;
861f8829a4aSRandall Stewart 		sctp_ucount_incr(asoc->cnt_on_reasm_queue);
862f8829a4aSRandall Stewart 		if (chk->rec.data.TSN_seq == cum_ackp1) {
863f8829a4aSRandall Stewart 			if (asoc->fragmented_delivery_inprogress == 0 &&
864f8829a4aSRandall Stewart 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) !=
865f8829a4aSRandall Stewart 			    SCTP_DATA_FIRST_FRAG) {
866f8829a4aSRandall Stewart 				/*
867f8829a4aSRandall Stewart 				 * An empty queue, no delivery inprogress,
868f8829a4aSRandall Stewart 				 * we hit the next one and it does NOT have
869f8829a4aSRandall Stewart 				 * a FIRST fragment mark.
870f8829a4aSRandall Stewart 				 */
871ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not first, no fragmented delivery in progress\n");
872139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
873f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
874f8829a4aSRandall Stewart 
875f8829a4aSRandall Stewart 				if (oper) {
876f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
877f8829a4aSRandall Stewart 					uint32_t *ippp;
878f8829a4aSRandall Stewart 
879139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
880f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
881f8829a4aSRandall Stewart 					    (sizeof(uint32_t) * 3);
882f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
883f8829a4aSRandall Stewart 					ph->param_type =
884f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
885139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
886f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
887a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_2);
888f8829a4aSRandall Stewart 					ippp++;
889f8829a4aSRandall Stewart 					*ippp = chk->rec.data.TSN_seq;
890f8829a4aSRandall Stewart 					ippp++;
891f8829a4aSRandall Stewart 					*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
892f8829a4aSRandall Stewart 
893f8829a4aSRandall Stewart 				}
894a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_2;
895f8829a4aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb,
896ceaad40aSRandall Stewart 				    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
897f8829a4aSRandall Stewart 				*abort_flag = 1;
898f8829a4aSRandall Stewart 			} else if (asoc->fragmented_delivery_inprogress &&
899f8829a4aSRandall Stewart 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
900f8829a4aSRandall Stewart 				/*
901f8829a4aSRandall Stewart 				 * We are doing a partial delivery and the
902f8829a4aSRandall Stewart 				 * NEXT chunk MUST be either the LAST or
903f8829a4aSRandall Stewart 				 * MIDDLE fragment NOT a FIRST
904f8829a4aSRandall Stewart 				 */
905ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS a first and fragmented delivery in progress\n");
906139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
907f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
908f8829a4aSRandall Stewart 				if (oper) {
909f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
910f8829a4aSRandall Stewart 					uint32_t *ippp;
911f8829a4aSRandall Stewart 
912139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
913f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
914f8829a4aSRandall Stewart 					    (3 * sizeof(uint32_t));
915f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
916f8829a4aSRandall Stewart 					ph->param_type =
917f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
918139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
919f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
920a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_3);
921f8829a4aSRandall Stewart 					ippp++;
922f8829a4aSRandall Stewart 					*ippp = chk->rec.data.TSN_seq;
923f8829a4aSRandall Stewart 					ippp++;
924f8829a4aSRandall Stewart 					*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
925f8829a4aSRandall Stewart 				}
926a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_3;
927f8829a4aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb,
928ceaad40aSRandall Stewart 				    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
929f8829a4aSRandall Stewart 				*abort_flag = 1;
930f8829a4aSRandall Stewart 			} else if (asoc->fragmented_delivery_inprogress) {
931f8829a4aSRandall Stewart 				/*
932f8829a4aSRandall Stewart 				 * Here we are ok with a MIDDLE or LAST
933f8829a4aSRandall Stewart 				 * piece
934f8829a4aSRandall Stewart 				 */
935f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
936f8829a4aSRandall Stewart 				    asoc->str_of_pdapi) {
937f8829a4aSRandall Stewart 					/* Got to be the right STR No */
938ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream number %d vs %d\n",
939f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
940f8829a4aSRandall Stewart 					    asoc->str_of_pdapi);
941139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
942f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
943f8829a4aSRandall Stewart 					if (oper) {
944f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
945f8829a4aSRandall Stewart 						uint32_t *ippp;
946f8829a4aSRandall Stewart 
947139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
948f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
949f8829a4aSRandall Stewart 						    (sizeof(uint32_t) * 3);
950f8829a4aSRandall Stewart 						ph = mtod(oper,
951f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
952f8829a4aSRandall Stewart 						ph->param_type =
953f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
954f8829a4aSRandall Stewart 						ph->param_length =
955139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
956f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
957a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_4);
958f8829a4aSRandall Stewart 						ippp++;
959f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
960f8829a4aSRandall Stewart 						ippp++;
961f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
962f8829a4aSRandall Stewart 					}
963a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_4;
964f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
965ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
966f8829a4aSRandall Stewart 					*abort_flag = 1;
967f8829a4aSRandall Stewart 				} else if ((asoc->fragment_flags & SCTP_DATA_UNORDERED) !=
968f8829a4aSRandall Stewart 					    SCTP_DATA_UNORDERED &&
969b5c16493SMichael Tuexen 				    chk->rec.data.stream_seq != asoc->ssn_of_pdapi) {
970f8829a4aSRandall Stewart 					/* Got to be the right STR Seq */
971ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream seq %d vs %d\n",
972f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
973f8829a4aSRandall Stewart 					    asoc->ssn_of_pdapi);
974139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
975f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
976f8829a4aSRandall Stewart 					if (oper) {
977f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
978f8829a4aSRandall Stewart 						uint32_t *ippp;
979f8829a4aSRandall Stewart 
980139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
981f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
982f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
983f8829a4aSRandall Stewart 						ph = mtod(oper,
984f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
985f8829a4aSRandall Stewart 						ph->param_type =
986f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
987f8829a4aSRandall Stewart 						ph->param_length =
988139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
989f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
990a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_5);
991f8829a4aSRandall Stewart 						ippp++;
992f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
993f8829a4aSRandall Stewart 						ippp++;
994f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
995f8829a4aSRandall Stewart 
996f8829a4aSRandall Stewart 					}
997a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_5;
998f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
999ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1000f8829a4aSRandall Stewart 					*abort_flag = 1;
1001f8829a4aSRandall Stewart 				}
1002f8829a4aSRandall Stewart 			}
1003f8829a4aSRandall Stewart 		}
1004f8829a4aSRandall Stewart 		return;
1005f8829a4aSRandall Stewart 	}
1006f8829a4aSRandall Stewart 	/* Find its place */
1007f8829a4aSRandall Stewart 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
100820b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(at->rec.data.TSN_seq, chk->rec.data.TSN_seq)) {
1009f8829a4aSRandall Stewart 			/*
1010f8829a4aSRandall Stewart 			 * one in queue is bigger than the new one, insert
1011f8829a4aSRandall Stewart 			 * before this one
1012f8829a4aSRandall Stewart 			 */
1013f8829a4aSRandall Stewart 			/* A check */
1014f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue += chk->send_size;
1015f8829a4aSRandall Stewart 			sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1016f8829a4aSRandall Stewart 			next = at;
1017f8829a4aSRandall Stewart 			TAILQ_INSERT_BEFORE(at, chk, sctp_next);
1018f8829a4aSRandall Stewart 			break;
1019f8829a4aSRandall Stewart 		} else if (at->rec.data.TSN_seq == chk->rec.data.TSN_seq) {
1020f8829a4aSRandall Stewart 			/* Gak, He sent me a duplicate str seq number */
1021f8829a4aSRandall Stewart 			/*
1022f8829a4aSRandall Stewart 			 * foo bar, I guess I will just free this new guy,
1023f8829a4aSRandall Stewart 			 * should we abort too? FIX ME MAYBE? Or it COULD be
1024f8829a4aSRandall Stewart 			 * that the SSN's have wrapped. Maybe I should
1025f8829a4aSRandall Stewart 			 * compare to TSN somehow... sigh for now just blow
1026f8829a4aSRandall Stewart 			 * away the chunk!
1027f8829a4aSRandall Stewart 			 */
1028f8829a4aSRandall Stewart 			if (chk->data) {
1029f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
1030f8829a4aSRandall Stewart 				chk->data = NULL;
1031f8829a4aSRandall Stewart 			}
1032689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
1033f8829a4aSRandall Stewart 			return;
1034f8829a4aSRandall Stewart 		} else {
1035f8829a4aSRandall Stewart 			prev = at;
1036f8829a4aSRandall Stewart 			if (TAILQ_NEXT(at, sctp_next) == NULL) {
1037f8829a4aSRandall Stewart 				/*
1038f8829a4aSRandall Stewart 				 * We are at the end, insert it after this
1039f8829a4aSRandall Stewart 				 * one
1040f8829a4aSRandall Stewart 				 */
1041f8829a4aSRandall Stewart 				/* check it first */
1042f8829a4aSRandall Stewart 				asoc->size_on_reasm_queue += chk->send_size;
1043f8829a4aSRandall Stewart 				sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1044f8829a4aSRandall Stewart 				TAILQ_INSERT_AFTER(&asoc->reasmqueue, at, chk, sctp_next);
1045f8829a4aSRandall Stewart 				break;
1046f8829a4aSRandall Stewart 			}
1047f8829a4aSRandall Stewart 		}
1048f8829a4aSRandall Stewart 	}
1049f8829a4aSRandall Stewart 	/* Now the audits */
1050f8829a4aSRandall Stewart 	if (prev) {
1051f8829a4aSRandall Stewart 		prev_tsn = chk->rec.data.TSN_seq - 1;
1052f8829a4aSRandall Stewart 		if (prev_tsn == prev->rec.data.TSN_seq) {
1053f8829a4aSRandall Stewart 			/*
1054f8829a4aSRandall Stewart 			 * Ok the one I am dropping onto the end is the
1055f8829a4aSRandall Stewart 			 * NEXT. A bit of valdiation here.
1056f8829a4aSRandall Stewart 			 */
1057f8829a4aSRandall Stewart 			if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1058f8829a4aSRandall Stewart 			    SCTP_DATA_FIRST_FRAG ||
1059f8829a4aSRandall Stewart 			    (prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1060f8829a4aSRandall Stewart 			    SCTP_DATA_MIDDLE_FRAG) {
1061f8829a4aSRandall Stewart 				/*
1062f8829a4aSRandall Stewart 				 * Insert chk MUST be a MIDDLE or LAST
1063f8829a4aSRandall Stewart 				 * fragment
1064f8829a4aSRandall Stewart 				 */
1065f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1066f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1067ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - It can be a midlle or last but not a first\n");
1068ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it's a FIRST!\n");
1069139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1070f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1071f8829a4aSRandall Stewart 					if (oper) {
1072f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1073f8829a4aSRandall Stewart 						uint32_t *ippp;
1074f8829a4aSRandall Stewart 
1075139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1076f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1077f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1078f8829a4aSRandall Stewart 						ph = mtod(oper,
1079f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1080f8829a4aSRandall Stewart 						ph->param_type =
1081f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1082f8829a4aSRandall Stewart 						ph->param_length =
1083139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1084f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1085a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_6);
1086f8829a4aSRandall Stewart 						ippp++;
1087f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1088f8829a4aSRandall Stewart 						ippp++;
1089f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1090f8829a4aSRandall Stewart 
1091f8829a4aSRandall Stewart 					}
1092a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_6;
1093f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1094ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1095f8829a4aSRandall Stewart 					*abort_flag = 1;
1096f8829a4aSRandall Stewart 					return;
1097f8829a4aSRandall Stewart 				}
1098f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
1099f8829a4aSRandall Stewart 				    prev->rec.data.stream_number) {
1100f8829a4aSRandall Stewart 					/*
1101f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1102f8829a4aSRandall Stewart 					 * they must be the same.
1103f8829a4aSRandall Stewart 					 */
1104ad81507eSRandall Stewart 					SCTP_PRINTF("Prev check - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1105f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
1106f8829a4aSRandall Stewart 					    prev->rec.data.stream_number);
1107139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1108f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1109f8829a4aSRandall Stewart 					if (oper) {
1110f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1111f8829a4aSRandall Stewart 						uint32_t *ippp;
1112f8829a4aSRandall Stewart 
1113139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1114f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1115f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1116f8829a4aSRandall Stewart 						ph = mtod(oper,
1117f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1118f8829a4aSRandall Stewart 						ph->param_type =
1119f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1120f8829a4aSRandall Stewart 						ph->param_length =
1121139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1122f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1123a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_7);
1124f8829a4aSRandall Stewart 						ippp++;
1125f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1126f8829a4aSRandall Stewart 						ippp++;
1127f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1128f8829a4aSRandall Stewart 					}
1129a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_7;
1130f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1131ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1132f8829a4aSRandall Stewart 
1133f8829a4aSRandall Stewart 					*abort_flag = 1;
1134f8829a4aSRandall Stewart 					return;
1135f8829a4aSRandall Stewart 				}
1136f8829a4aSRandall Stewart 				if ((prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1137f8829a4aSRandall Stewart 				    chk->rec.data.stream_seq !=
1138f8829a4aSRandall Stewart 				    prev->rec.data.stream_seq) {
1139f8829a4aSRandall Stewart 					/*
1140f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1141f8829a4aSRandall Stewart 					 * they must be the same.
1142f8829a4aSRandall Stewart 					 */
1143ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1144f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
1145f8829a4aSRandall Stewart 					    prev->rec.data.stream_seq);
1146139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1147f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1148f8829a4aSRandall Stewart 					if (oper) {
1149f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1150f8829a4aSRandall Stewart 						uint32_t *ippp;
1151f8829a4aSRandall Stewart 
1152139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1153f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1154f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1155f8829a4aSRandall Stewart 						ph = mtod(oper,
1156f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1157f8829a4aSRandall Stewart 						ph->param_type =
1158f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1159f8829a4aSRandall Stewart 						ph->param_length =
1160139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1161f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1162a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_8);
1163f8829a4aSRandall Stewart 						ippp++;
1164f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1165f8829a4aSRandall Stewart 						ippp++;
1166f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1167f8829a4aSRandall Stewart 					}
1168a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_8;
1169f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1170ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1171f8829a4aSRandall Stewart 
1172f8829a4aSRandall Stewart 					*abort_flag = 1;
1173f8829a4aSRandall Stewart 					return;
1174f8829a4aSRandall Stewart 				}
1175f8829a4aSRandall Stewart 			} else if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1176f8829a4aSRandall Stewart 			    SCTP_DATA_LAST_FRAG) {
1177f8829a4aSRandall Stewart 				/* Insert chk MUST be a FIRST */
1178f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1179f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1180ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, evil plot, its not FIRST and it must be!\n");
1181139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1182f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1183f8829a4aSRandall Stewart 					if (oper) {
1184f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1185f8829a4aSRandall Stewart 						uint32_t *ippp;
1186f8829a4aSRandall Stewart 
1187139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1188f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1189f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1190f8829a4aSRandall Stewart 						ph = mtod(oper,
1191f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1192f8829a4aSRandall Stewart 						ph->param_type =
1193f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1194f8829a4aSRandall Stewart 						ph->param_length =
1195139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1196f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1197a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_9);
1198f8829a4aSRandall Stewart 						ippp++;
1199f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1200f8829a4aSRandall Stewart 						ippp++;
1201f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1202f8829a4aSRandall Stewart 
1203f8829a4aSRandall Stewart 					}
1204a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_9;
1205f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1206ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1207f8829a4aSRandall Stewart 
1208f8829a4aSRandall Stewart 					*abort_flag = 1;
1209f8829a4aSRandall Stewart 					return;
1210f8829a4aSRandall Stewart 				}
1211f8829a4aSRandall Stewart 			}
1212f8829a4aSRandall Stewart 		}
1213f8829a4aSRandall Stewart 	}
1214f8829a4aSRandall Stewart 	if (next) {
1215f8829a4aSRandall Stewart 		post_tsn = chk->rec.data.TSN_seq + 1;
1216f8829a4aSRandall Stewart 		if (post_tsn == next->rec.data.TSN_seq) {
1217f8829a4aSRandall Stewart 			/*
1218f8829a4aSRandall Stewart 			 * Ok the one I am inserting ahead of is my NEXT
1219f8829a4aSRandall Stewart 			 * one. A bit of valdiation here.
1220f8829a4aSRandall Stewart 			 */
1221f8829a4aSRandall Stewart 			if (next->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
1222f8829a4aSRandall Stewart 				/* Insert chk MUST be a last fragment */
1223f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK)
1224f8829a4aSRandall Stewart 				    != SCTP_DATA_LAST_FRAG) {
1225ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is FIRST, we must be LAST\n");
1226ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not a last!\n");
1227139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1228f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1229f8829a4aSRandall Stewart 					if (oper) {
1230f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1231f8829a4aSRandall Stewart 						uint32_t *ippp;
1232f8829a4aSRandall Stewart 
1233139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1234f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1235f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1236f8829a4aSRandall Stewart 						ph = mtod(oper,
1237f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1238f8829a4aSRandall Stewart 						ph->param_type =
1239f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1240f8829a4aSRandall Stewart 						ph->param_length =
1241139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1242f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1243a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_10);
1244f8829a4aSRandall Stewart 						ippp++;
1245f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1246f8829a4aSRandall Stewart 						ippp++;
1247f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1248f8829a4aSRandall Stewart 					}
1249a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_10;
1250f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1251ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1252f8829a4aSRandall Stewart 
1253f8829a4aSRandall Stewart 					*abort_flag = 1;
1254f8829a4aSRandall Stewart 					return;
1255f8829a4aSRandall Stewart 				}
1256f8829a4aSRandall Stewart 			} else if ((next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1257f8829a4aSRandall Stewart 				    SCTP_DATA_MIDDLE_FRAG ||
1258f8829a4aSRandall Stewart 				    (next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1259f8829a4aSRandall Stewart 			    SCTP_DATA_LAST_FRAG) {
1260f8829a4aSRandall Stewart 				/*
1261f8829a4aSRandall Stewart 				 * Insert chk CAN be MIDDLE or FIRST NOT
1262f8829a4aSRandall Stewart 				 * LAST
1263f8829a4aSRandall Stewart 				 */
1264f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1265f8829a4aSRandall Stewart 				    SCTP_DATA_LAST_FRAG) {
1266ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is a MIDDLE/LAST\n");
1267ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, new prev chunk is a LAST\n");
1268139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1269f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1270f8829a4aSRandall Stewart 					if (oper) {
1271f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1272f8829a4aSRandall Stewart 						uint32_t *ippp;
1273f8829a4aSRandall Stewart 
1274139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1275f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1276f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1277f8829a4aSRandall Stewart 						ph = mtod(oper,
1278f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1279f8829a4aSRandall Stewart 						ph->param_type =
1280f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1281f8829a4aSRandall Stewart 						ph->param_length =
1282139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1283f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1284a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_11);
1285f8829a4aSRandall Stewart 						ippp++;
1286f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1287f8829a4aSRandall Stewart 						ippp++;
1288f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1289f8829a4aSRandall Stewart 
1290f8829a4aSRandall Stewart 					}
1291a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_11;
1292f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1293ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1294f8829a4aSRandall Stewart 
1295f8829a4aSRandall Stewart 					*abort_flag = 1;
1296f8829a4aSRandall Stewart 					return;
1297f8829a4aSRandall Stewart 				}
1298f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
1299f8829a4aSRandall Stewart 				    next->rec.data.stream_number) {
1300f8829a4aSRandall Stewart 					/*
1301f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1302f8829a4aSRandall Stewart 					 * they must be the same.
1303f8829a4aSRandall Stewart 					 */
1304ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1305f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
1306f8829a4aSRandall Stewart 					    next->rec.data.stream_number);
1307139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1308f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1309f8829a4aSRandall Stewart 					if (oper) {
1310f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1311f8829a4aSRandall Stewart 						uint32_t *ippp;
1312f8829a4aSRandall Stewart 
1313139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1314f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1315f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1316f8829a4aSRandall Stewart 						ph = mtod(oper,
1317f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1318f8829a4aSRandall Stewart 						ph->param_type =
1319f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1320f8829a4aSRandall Stewart 						ph->param_length =
1321139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1322f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1323a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_12);
1324f8829a4aSRandall Stewart 						ippp++;
1325f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1326f8829a4aSRandall Stewart 						ippp++;
1327f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1328f8829a4aSRandall Stewart 
1329f8829a4aSRandall Stewart 					}
1330a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_12;
1331f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1332ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1333f8829a4aSRandall Stewart 
1334f8829a4aSRandall Stewart 					*abort_flag = 1;
1335f8829a4aSRandall Stewart 					return;
1336f8829a4aSRandall Stewart 				}
1337f8829a4aSRandall Stewart 				if ((next->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1338f8829a4aSRandall Stewart 				    chk->rec.data.stream_seq !=
1339f8829a4aSRandall Stewart 				    next->rec.data.stream_seq) {
1340f8829a4aSRandall Stewart 					/*
1341f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1342f8829a4aSRandall Stewart 					 * they must be the same.
1343f8829a4aSRandall Stewart 					 */
1344ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1345f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
1346f8829a4aSRandall Stewart 					    next->rec.data.stream_seq);
1347139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1348f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1349f8829a4aSRandall Stewart 					if (oper) {
1350f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1351f8829a4aSRandall Stewart 						uint32_t *ippp;
1352f8829a4aSRandall Stewart 
1353139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1354f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1355f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1356f8829a4aSRandall Stewart 						ph = mtod(oper,
1357f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1358f8829a4aSRandall Stewart 						ph->param_type =
1359f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1360f8829a4aSRandall Stewart 						ph->param_length =
1361139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1362f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1363a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_13);
1364f8829a4aSRandall Stewart 						ippp++;
1365f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1366f8829a4aSRandall Stewart 						ippp++;
1367f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1368f8829a4aSRandall Stewart 					}
1369a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_13;
1370f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1371ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1372f8829a4aSRandall Stewart 
1373f8829a4aSRandall Stewart 					*abort_flag = 1;
1374f8829a4aSRandall Stewart 					return;
1375f8829a4aSRandall Stewart 				}
1376f8829a4aSRandall Stewart 			}
1377f8829a4aSRandall Stewart 		}
1378f8829a4aSRandall Stewart 	}
1379f8829a4aSRandall Stewart 	/* Do we need to do some delivery? check */
1380f8829a4aSRandall Stewart 	sctp_deliver_reasm_check(stcb, asoc);
1381f8829a4aSRandall Stewart }
1382f8829a4aSRandall Stewart 
1383f8829a4aSRandall Stewart /*
1384f8829a4aSRandall Stewart  * This is an unfortunate routine. It checks to make sure a evil guy is not
1385f8829a4aSRandall Stewart  * stuffing us full of bad packet fragments. A broken peer could also do this
1386f8829a4aSRandall Stewart  * but this is doubtful. It is to bad I must worry about evil crackers sigh
1387f8829a4aSRandall Stewart  * :< more cycles.
1388f8829a4aSRandall Stewart  */
1389f8829a4aSRandall Stewart static int
1390f8829a4aSRandall Stewart sctp_does_tsn_belong_to_reasm(struct sctp_association *asoc,
1391f8829a4aSRandall Stewart     uint32_t TSN_seq)
1392f8829a4aSRandall Stewart {
1393f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *at;
1394f8829a4aSRandall Stewart 	uint32_t tsn_est;
1395f8829a4aSRandall Stewart 
1396f8829a4aSRandall Stewart 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
139720b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(TSN_seq, at->rec.data.TSN_seq)) {
1398f8829a4aSRandall Stewart 			/* is it one bigger? */
1399f8829a4aSRandall Stewart 			tsn_est = at->rec.data.TSN_seq + 1;
1400f8829a4aSRandall Stewart 			if (tsn_est == TSN_seq) {
1401f8829a4aSRandall Stewart 				/* yep. It better be a last then */
1402f8829a4aSRandall Stewart 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1403f8829a4aSRandall Stewart 				    SCTP_DATA_LAST_FRAG) {
1404f8829a4aSRandall Stewart 					/*
1405f8829a4aSRandall Stewart 					 * Ok this guy belongs next to a guy
1406f8829a4aSRandall Stewart 					 * that is NOT last, it should be a
1407f8829a4aSRandall Stewart 					 * middle/last, not a complete
1408f8829a4aSRandall Stewart 					 * chunk.
1409f8829a4aSRandall Stewart 					 */
1410f8829a4aSRandall Stewart 					return (1);
1411f8829a4aSRandall Stewart 				} else {
1412f8829a4aSRandall Stewart 					/*
1413f8829a4aSRandall Stewart 					 * This guy is ok since its a LAST
1414f8829a4aSRandall Stewart 					 * and the new chunk is a fully
1415f8829a4aSRandall Stewart 					 * self- contained one.
1416f8829a4aSRandall Stewart 					 */
1417f8829a4aSRandall Stewart 					return (0);
1418f8829a4aSRandall Stewart 				}
1419f8829a4aSRandall Stewart 			}
1420f8829a4aSRandall Stewart 		} else if (TSN_seq == at->rec.data.TSN_seq) {
1421f8829a4aSRandall Stewart 			/* Software error since I have a dup? */
1422f8829a4aSRandall Stewart 			return (1);
1423f8829a4aSRandall Stewart 		} else {
1424f8829a4aSRandall Stewart 			/*
1425f8829a4aSRandall Stewart 			 * Ok, 'at' is larger than new chunk but does it
1426f8829a4aSRandall Stewart 			 * need to be right before it.
1427f8829a4aSRandall Stewart 			 */
1428f8829a4aSRandall Stewart 			tsn_est = TSN_seq + 1;
1429f8829a4aSRandall Stewart 			if (tsn_est == at->rec.data.TSN_seq) {
1430f8829a4aSRandall Stewart 				/* Yep, It better be a first */
1431f8829a4aSRandall Stewart 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1432f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1433f8829a4aSRandall Stewart 					return (1);
1434f8829a4aSRandall Stewart 				} else {
1435f8829a4aSRandall Stewart 					return (0);
1436f8829a4aSRandall Stewart 				}
1437f8829a4aSRandall Stewart 			}
1438f8829a4aSRandall Stewart 		}
1439f8829a4aSRandall Stewart 	}
1440f8829a4aSRandall Stewart 	return (0);
1441f8829a4aSRandall Stewart }
1442f8829a4aSRandall Stewart 
1443f8829a4aSRandall Stewart 
1444f8829a4aSRandall Stewart static int
1445f8829a4aSRandall Stewart sctp_process_a_data_chunk(struct sctp_tcb *stcb, struct sctp_association *asoc,
1446f8829a4aSRandall Stewart     struct mbuf **m, int offset, struct sctp_data_chunk *ch, int chk_length,
1447f8829a4aSRandall Stewart     struct sctp_nets *net, uint32_t * high_tsn, int *abort_flag,
1448f8829a4aSRandall Stewart     int *break_flag, int last_chunk)
1449f8829a4aSRandall Stewart {
1450f8829a4aSRandall Stewart 	/* Process a data chunk */
1451f8829a4aSRandall Stewart 	/* struct sctp_tmit_chunk *chk; */
1452f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
1453f8829a4aSRandall Stewart 	uint32_t tsn, gap;
1454f8829a4aSRandall Stewart 	struct mbuf *dmbuf;
14557215cc1bSMichael Tuexen 	int the_len;
1456139bc87fSRandall Stewart 	int need_reasm_check = 0;
1457f8829a4aSRandall Stewart 	uint16_t strmno, strmseq;
1458f8829a4aSRandall Stewart 	struct mbuf *oper;
1459f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
1460f42a358aSRandall Stewart 	int ordered;
1461f42a358aSRandall Stewart 	uint32_t protocol_id;
1462f42a358aSRandall Stewart 	uint8_t chunk_flags;
146317205eccSRandall Stewart 	struct sctp_stream_reset_list *liste;
1464f8829a4aSRandall Stewart 
1465f8829a4aSRandall Stewart 	chk = NULL;
1466f8829a4aSRandall Stewart 	tsn = ntohl(ch->dp.tsn);
1467f42a358aSRandall Stewart 	chunk_flags = ch->ch.chunk_flags;
1468b3f1ea41SRandall Stewart 	if ((chunk_flags & SCTP_DATA_SACK_IMMEDIATELY) == SCTP_DATA_SACK_IMMEDIATELY) {
1469b3f1ea41SRandall Stewart 		asoc->send_sack = 1;
1470b3f1ea41SRandall Stewart 	}
1471f42a358aSRandall Stewart 	protocol_id = ch->dp.protocol_id;
147237f144ebSMichael Tuexen 	ordered = ((chunk_flags & SCTP_DATA_UNORDERED) == 0);
1473b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1474c4739e2fSRandall Stewart 		sctp_log_map(tsn, asoc->cumulative_tsn, asoc->highest_tsn_inside_map, SCTP_MAP_TSN_ENTERS);
147580fefe0aSRandall Stewart 	}
1476ad81507eSRandall Stewart 	if (stcb == NULL) {
1477ad81507eSRandall Stewart 		return (0);
1478ad81507eSRandall Stewart 	}
147980fefe0aSRandall Stewart 	SCTP_LTRACE_CHK(stcb->sctp_ep, stcb, ch->ch.chunk_type, tsn);
148020b07a4dSMichael Tuexen 	if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
1481f8829a4aSRandall Stewart 		/* It is a duplicate */
1482f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvdupdata);
1483f8829a4aSRandall Stewart 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1484f8829a4aSRandall Stewart 			/* Record a dup for the next outbound sack */
1485f8829a4aSRandall Stewart 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1486f8829a4aSRandall Stewart 			asoc->numduptsns++;
1487f8829a4aSRandall Stewart 		}
1488b201f536SRandall Stewart 		asoc->send_sack = 1;
1489f8829a4aSRandall Stewart 		return (0);
1490f8829a4aSRandall Stewart 	}
1491f8829a4aSRandall Stewart 	/* Calculate the number of TSN's between the base and this TSN */
1492d50c1d79SRandall Stewart 	SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
1493f8829a4aSRandall Stewart 	if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
1494f8829a4aSRandall Stewart 		/* Can't hold the bit in the mapping at max array, toss it */
1495f8829a4aSRandall Stewart 		return (0);
1496f8829a4aSRandall Stewart 	}
1497f8829a4aSRandall Stewart 	if (gap >= (uint32_t) (asoc->mapping_array_size << 3)) {
1498207304d4SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
14990696e120SRandall Stewart 		if (sctp_expand_mapping_array(asoc, gap)) {
1500f8829a4aSRandall Stewart 			/* Can't expand, drop it */
1501f8829a4aSRandall Stewart 			return (0);
1502f8829a4aSRandall Stewart 		}
1503f8829a4aSRandall Stewart 	}
150420b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(tsn, *high_tsn)) {
1505f8829a4aSRandall Stewart 		*high_tsn = tsn;
1506f8829a4aSRandall Stewart 	}
1507f8829a4aSRandall Stewart 	/* See if we have received this one already */
150877acdc25SRandall Stewart 	if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap) ||
150977acdc25SRandall Stewart 	    SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, gap)) {
1510f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvdupdata);
1511f8829a4aSRandall Stewart 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1512f8829a4aSRandall Stewart 			/* Record a dup for the next outbound sack */
1513f8829a4aSRandall Stewart 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1514f8829a4aSRandall Stewart 			asoc->numduptsns++;
1515f8829a4aSRandall Stewart 		}
151642551e99SRandall Stewart 		asoc->send_sack = 1;
1517f8829a4aSRandall Stewart 		return (0);
1518f8829a4aSRandall Stewart 	}
1519f8829a4aSRandall Stewart 	/*
1520f8829a4aSRandall Stewart 	 * Check to see about the GONE flag, duplicates would cause a sack
1521f8829a4aSRandall Stewart 	 * to be sent up above
1522f8829a4aSRandall Stewart 	 */
1523ad81507eSRandall Stewart 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1524f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1525f8829a4aSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET))
1526f8829a4aSRandall Stewart 	    ) {
1527f8829a4aSRandall Stewart 		/*
1528f8829a4aSRandall Stewart 		 * wait a minute, this guy is gone, there is no longer a
1529f8829a4aSRandall Stewart 		 * receiver. Send peer an ABORT!
1530f8829a4aSRandall Stewart 		 */
1531f8829a4aSRandall Stewart 		struct mbuf *op_err;
1532f8829a4aSRandall Stewart 
1533f8829a4aSRandall Stewart 		op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1534ceaad40aSRandall Stewart 		sctp_abort_an_association(stcb->sctp_ep, stcb, 0, op_err, SCTP_SO_NOT_LOCKED);
1535f8829a4aSRandall Stewart 		*abort_flag = 1;
1536f8829a4aSRandall Stewart 		return (0);
1537f8829a4aSRandall Stewart 	}
1538f8829a4aSRandall Stewart 	/*
1539f8829a4aSRandall Stewart 	 * Now before going further we see if there is room. If NOT then we
1540f8829a4aSRandall Stewart 	 * MAY let one through only IF this TSN is the one we are waiting
1541f8829a4aSRandall Stewart 	 * for on a partial delivery API.
1542f8829a4aSRandall Stewart 	 */
1543f8829a4aSRandall Stewart 
1544f8829a4aSRandall Stewart 	/* now do the tests */
1545f8829a4aSRandall Stewart 	if (((asoc->cnt_on_all_streams +
1546f8829a4aSRandall Stewart 	    asoc->cnt_on_reasm_queue +
1547b3f1ea41SRandall Stewart 	    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) ||
1548f8829a4aSRandall Stewart 	    (((int)asoc->my_rwnd) <= 0)) {
1549f8829a4aSRandall Stewart 		/*
1550f8829a4aSRandall Stewart 		 * When we have NO room in the rwnd we check to make sure
1551f8829a4aSRandall Stewart 		 * the reader is doing its job...
1552f8829a4aSRandall Stewart 		 */
1553f8829a4aSRandall Stewart 		if (stcb->sctp_socket->so_rcv.sb_cc) {
1554f8829a4aSRandall Stewart 			/* some to read, wake-up */
1555ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1556ceaad40aSRandall Stewart 			struct socket *so;
1557ceaad40aSRandall Stewart 
1558ceaad40aSRandall Stewart 			so = SCTP_INP_SO(stcb->sctp_ep);
1559ceaad40aSRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, 1);
1560ceaad40aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
1561ceaad40aSRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
1562ceaad40aSRandall Stewart 			SCTP_TCB_LOCK(stcb);
1563ceaad40aSRandall Stewart 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
1564ceaad40aSRandall Stewart 			if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1565ceaad40aSRandall Stewart 				/* assoc was freed while we were unlocked */
1566ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
1567ceaad40aSRandall Stewart 				return (0);
1568ceaad40aSRandall Stewart 			}
1569ceaad40aSRandall Stewart #endif
1570f8829a4aSRandall Stewart 			sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1571ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1572ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
1573ceaad40aSRandall Stewart #endif
1574f8829a4aSRandall Stewart 		}
1575f8829a4aSRandall Stewart 		/* now is it in the mapping array of what we have accepted? */
157620b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_map) &&
157720b07a4dSMichael Tuexen 		    SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1578f8829a4aSRandall Stewart 			/* Nope not in the valid range dump it */
1579f8829a4aSRandall Stewart 			sctp_set_rwnd(stcb, asoc);
1580f8829a4aSRandall Stewart 			if ((asoc->cnt_on_all_streams +
1581f8829a4aSRandall Stewart 			    asoc->cnt_on_reasm_queue +
1582b3f1ea41SRandall Stewart 			    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) {
1583f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_datadropchklmt);
1584f8829a4aSRandall Stewart 			} else {
1585f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_datadroprwnd);
1586f8829a4aSRandall Stewart 			}
1587f8829a4aSRandall Stewart 			*break_flag = 1;
1588f8829a4aSRandall Stewart 			return (0);
1589f8829a4aSRandall Stewart 		}
1590f8829a4aSRandall Stewart 	}
1591f8829a4aSRandall Stewart 	strmno = ntohs(ch->dp.stream_id);
1592f8829a4aSRandall Stewart 	if (strmno >= asoc->streamincnt) {
1593f8829a4aSRandall Stewart 		struct sctp_paramhdr *phdr;
1594f8829a4aSRandall Stewart 		struct mbuf *mb;
1595f8829a4aSRandall Stewart 
1596f8829a4aSRandall Stewart 		mb = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) * 2),
1597139bc87fSRandall Stewart 		    0, M_DONTWAIT, 1, MT_DATA);
1598f8829a4aSRandall Stewart 		if (mb != NULL) {
1599f8829a4aSRandall Stewart 			/* add some space up front so prepend will work well */
1600139bc87fSRandall Stewart 			SCTP_BUF_RESV_UF(mb, sizeof(struct sctp_chunkhdr));
1601f8829a4aSRandall Stewart 			phdr = mtod(mb, struct sctp_paramhdr *);
1602f8829a4aSRandall Stewart 			/*
1603f8829a4aSRandall Stewart 			 * Error causes are just param's and this one has
1604f8829a4aSRandall Stewart 			 * two back to back phdr, one with the error type
1605f8829a4aSRandall Stewart 			 * and size, the other with the streamid and a rsvd
1606f8829a4aSRandall Stewart 			 */
1607139bc87fSRandall Stewart 			SCTP_BUF_LEN(mb) = (sizeof(struct sctp_paramhdr) * 2);
1608f8829a4aSRandall Stewart 			phdr->param_type = htons(SCTP_CAUSE_INVALID_STREAM);
1609f8829a4aSRandall Stewart 			phdr->param_length =
1610f8829a4aSRandall Stewart 			    htons(sizeof(struct sctp_paramhdr) * 2);
1611f8829a4aSRandall Stewart 			phdr++;
1612f8829a4aSRandall Stewart 			/* We insert the stream in the type field */
1613f8829a4aSRandall Stewart 			phdr->param_type = ch->dp.stream_id;
1614f8829a4aSRandall Stewart 			/* And set the length to 0 for the rsvd field */
1615f8829a4aSRandall Stewart 			phdr->param_length = 0;
1616f8829a4aSRandall Stewart 			sctp_queue_op_err(stcb, mb);
1617f8829a4aSRandall Stewart 		}
1618f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_badsid);
1619207304d4SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1620830d754dSRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
162120b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1622830d754dSRandall Stewart 			asoc->highest_tsn_inside_nr_map = tsn;
1623d06c82f1SRandall Stewart 		}
1624d06c82f1SRandall Stewart 		if (tsn == (asoc->cumulative_tsn + 1)) {
1625d06c82f1SRandall Stewart 			/* Update cum-ack */
1626d06c82f1SRandall Stewart 			asoc->cumulative_tsn = tsn;
1627d06c82f1SRandall Stewart 		}
1628f8829a4aSRandall Stewart 		return (0);
1629f8829a4aSRandall Stewart 	}
1630f8829a4aSRandall Stewart 	/*
1631f8829a4aSRandall Stewart 	 * Before we continue lets validate that we are not being fooled by
1632f8829a4aSRandall Stewart 	 * an evil attacker. We can only have 4k chunks based on our TSN
1633f8829a4aSRandall Stewart 	 * spread allowed by the mapping array 512 * 8 bits, so there is no
1634f8829a4aSRandall Stewart 	 * way our stream sequence numbers could have wrapped. We of course
1635f8829a4aSRandall Stewart 	 * only validate the FIRST fragment so the bit must be set.
1636f8829a4aSRandall Stewart 	 */
1637f8829a4aSRandall Stewart 	strmseq = ntohs(ch->dp.stream_sequence);
1638f42a358aSRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
163918e198d3SRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
164018e198d3SRandall Stewart 	if (asoc->tsn_in_at >= SCTP_TSN_LOG_SIZE) {
164118e198d3SRandall Stewart 		asoc->tsn_in_at = 0;
164218e198d3SRandall Stewart 		asoc->tsn_in_wrapped = 1;
164318e198d3SRandall Stewart 	}
1644f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].tsn = tsn;
1645f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].strm = strmno;
1646f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].seq = strmseq;
1647f1f73e57SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].sz = chk_length;
1648f1f73e57SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].flgs = chunk_flags;
164918e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].stcb = (void *)stcb;
165018e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].in_pos = asoc->tsn_in_at;
165118e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].in_out = 1;
1652f42a358aSRandall Stewart 	asoc->tsn_in_at++;
1653f42a358aSRandall Stewart #endif
1654f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_FIRST_FRAG) &&
1655d61a0ae0SRandall Stewart 	    (TAILQ_EMPTY(&asoc->resetHead)) &&
1656f42a358aSRandall Stewart 	    (chunk_flags & SCTP_DATA_UNORDERED) == 0 &&
165720b07a4dSMichael Tuexen 	    SCTP_SSN_GE(asoc->strmin[strmno].last_sequence_delivered, strmseq)) {
1658f8829a4aSRandall Stewart 		/* The incoming sseq is behind where we last delivered? */
1659ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1, "EVIL/Broken-Dup S-SEQ:%d delivered:%d from peer, Abort!\n",
1660ad81507eSRandall Stewart 		    strmseq, asoc->strmin[strmno].last_sequence_delivered);
1661139bc87fSRandall Stewart 		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1662f8829a4aSRandall Stewart 		    0, M_DONTWAIT, 1, MT_DATA);
1663f8829a4aSRandall Stewart 		if (oper) {
1664f8829a4aSRandall Stewart 			struct sctp_paramhdr *ph;
1665f8829a4aSRandall Stewart 			uint32_t *ippp;
1666f8829a4aSRandall Stewart 
1667139bc87fSRandall Stewart 			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
1668f8829a4aSRandall Stewart 			    (3 * sizeof(uint32_t));
1669f8829a4aSRandall Stewart 			ph = mtod(oper, struct sctp_paramhdr *);
1670f8829a4aSRandall Stewart 			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1671139bc87fSRandall Stewart 			ph->param_length = htons(SCTP_BUF_LEN(oper));
1672f8829a4aSRandall Stewart 			ippp = (uint32_t *) (ph + 1);
1673a5d547adSRandall Stewart 			*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_14);
1674f8829a4aSRandall Stewart 			ippp++;
1675f8829a4aSRandall Stewart 			*ippp = tsn;
1676f8829a4aSRandall Stewart 			ippp++;
1677f8829a4aSRandall Stewart 			*ippp = ((strmno << 16) | strmseq);
1678f8829a4aSRandall Stewart 
1679f8829a4aSRandall Stewart 		}
1680a5d547adSRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_14;
1681f8829a4aSRandall Stewart 		sctp_abort_an_association(stcb->sctp_ep, stcb,
1682ceaad40aSRandall Stewart 		    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1683f8829a4aSRandall Stewart 		*abort_flag = 1;
1684f8829a4aSRandall Stewart 		return (0);
1685f8829a4aSRandall Stewart 	}
1686f42a358aSRandall Stewart 	/************************************
1687f42a358aSRandall Stewart 	 * From here down we may find ch-> invalid
1688f42a358aSRandall Stewart 	 * so its a good idea NOT to use it.
1689f42a358aSRandall Stewart 	 *************************************/
1690f42a358aSRandall Stewart 
1691f8829a4aSRandall Stewart 	the_len = (chk_length - sizeof(struct sctp_data_chunk));
1692f8829a4aSRandall Stewart 	if (last_chunk == 0) {
169344b7479bSRandall Stewart 		dmbuf = SCTP_M_COPYM(*m,
1694f8829a4aSRandall Stewart 		    (offset + sizeof(struct sctp_data_chunk)),
1695f8829a4aSRandall Stewart 		    the_len, M_DONTWAIT);
1696f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
1697b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
1698f8829a4aSRandall Stewart 			struct mbuf *mat;
1699f8829a4aSRandall Stewart 
1700*60990c0cSMichael Tuexen 			for (mat = dmbuf; mat; mat = SCTP_BUF_NEXT(mat)) {
1701139bc87fSRandall Stewart 				if (SCTP_BUF_IS_EXTENDED(mat)) {
1702f8829a4aSRandall Stewart 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
1703f8829a4aSRandall Stewart 				}
1704f8829a4aSRandall Stewart 			}
1705f8829a4aSRandall Stewart 		}
1706f8829a4aSRandall Stewart #endif
1707f8829a4aSRandall Stewart 	} else {
1708f8829a4aSRandall Stewart 		/* We can steal the last chunk */
1709139bc87fSRandall Stewart 		int l_len;
1710139bc87fSRandall Stewart 
1711f8829a4aSRandall Stewart 		dmbuf = *m;
1712f8829a4aSRandall Stewart 		/* lop off the top part */
1713f8829a4aSRandall Stewart 		m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
1714139bc87fSRandall Stewart 		if (SCTP_BUF_NEXT(dmbuf) == NULL) {
1715139bc87fSRandall Stewart 			l_len = SCTP_BUF_LEN(dmbuf);
1716139bc87fSRandall Stewart 		} else {
1717139bc87fSRandall Stewart 			/*
1718139bc87fSRandall Stewart 			 * need to count up the size hopefully does not hit
1719139bc87fSRandall Stewart 			 * this to often :-0
1720139bc87fSRandall Stewart 			 */
1721139bc87fSRandall Stewart 			struct mbuf *lat;
1722139bc87fSRandall Stewart 
1723139bc87fSRandall Stewart 			l_len = 0;
1724*60990c0cSMichael Tuexen 			for (lat = dmbuf; lat; lat = SCTP_BUF_NEXT(lat)) {
1725139bc87fSRandall Stewart 				l_len += SCTP_BUF_LEN(lat);
1726139bc87fSRandall Stewart 			}
1727139bc87fSRandall Stewart 		}
1728139bc87fSRandall Stewart 		if (l_len > the_len) {
1729f8829a4aSRandall Stewart 			/* Trim the end round bytes off  too */
1730139bc87fSRandall Stewart 			m_adj(dmbuf, -(l_len - the_len));
1731f8829a4aSRandall Stewart 		}
1732f8829a4aSRandall Stewart 	}
1733f8829a4aSRandall Stewart 	if (dmbuf == NULL) {
1734f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_nomem);
1735f8829a4aSRandall Stewart 		return (0);
1736f8829a4aSRandall Stewart 	}
1737f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
1738f8829a4aSRandall Stewart 	    asoc->fragmented_delivery_inprogress == 0 &&
1739f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->resetHead) &&
1740f42a358aSRandall Stewart 	    ((ordered == 0) ||
174136ec9f81SMichael Tuexen 	    ((uint16_t) (asoc->strmin[strmno].last_sequence_delivered + 1) == strmseq &&
1742f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->strmin[strmno].inqueue)))) {
1743f8829a4aSRandall Stewart 		/* Candidate for express delivery */
1744f8829a4aSRandall Stewart 		/*
1745f8829a4aSRandall Stewart 		 * Its not fragmented, No PD-API is up, Nothing in the
1746f8829a4aSRandall Stewart 		 * delivery queue, Its un-ordered OR ordered and the next to
1747f8829a4aSRandall Stewart 		 * deliver AND nothing else is stuck on the stream queue,
1748f8829a4aSRandall Stewart 		 * And there is room for it in the socket buffer. Lets just
1749f8829a4aSRandall Stewart 		 * stuff it up the buffer....
1750f8829a4aSRandall Stewart 		 */
1751f8829a4aSRandall Stewart 
1752f8829a4aSRandall Stewart 		/* It would be nice to avoid this copy if we could :< */
1753f8829a4aSRandall Stewart 		sctp_alloc_a_readq(stcb, control);
1754f8829a4aSRandall Stewart 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1755f42a358aSRandall Stewart 		    protocol_id,
1756f8829a4aSRandall Stewart 		    stcb->asoc.context,
1757f8829a4aSRandall Stewart 		    strmno, strmseq,
1758f42a358aSRandall Stewart 		    chunk_flags,
1759f8829a4aSRandall Stewart 		    dmbuf);
1760f8829a4aSRandall Stewart 		if (control == NULL) {
1761f8829a4aSRandall Stewart 			goto failed_express_del;
1762f8829a4aSRandall Stewart 		}
17631ea735c8SMichael Tuexen 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
176420b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
17651ea735c8SMichael Tuexen 			asoc->highest_tsn_inside_nr_map = tsn;
17661ea735c8SMichael Tuexen 		}
1767cfde3ff7SRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb,
1768cfde3ff7SRandall Stewart 		    control, &stcb->sctp_socket->so_rcv,
1769cfde3ff7SRandall Stewart 		    1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
1770830d754dSRandall Stewart 
1771f42a358aSRandall Stewart 		if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1772f8829a4aSRandall Stewart 			/* for ordered, bump what we delivered */
1773f8829a4aSRandall Stewart 			asoc->strmin[strmno].last_sequence_delivered++;
1774f8829a4aSRandall Stewart 		}
1775f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvexpress);
1776b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
17776a91f103SRandall Stewart 			sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno,
1778f8829a4aSRandall Stewart 			    SCTP_STR_LOG_FROM_EXPRS_DEL);
177980fefe0aSRandall Stewart 		}
1780f8829a4aSRandall Stewart 		control = NULL;
1781b5c16493SMichael Tuexen 
1782f8829a4aSRandall Stewart 		goto finish_express_del;
1783f8829a4aSRandall Stewart 	}
1784f8829a4aSRandall Stewart failed_express_del:
1785f8829a4aSRandall Stewart 	/* If we reach here this is a new chunk */
1786f8829a4aSRandall Stewart 	chk = NULL;
1787f8829a4aSRandall Stewart 	control = NULL;
1788f8829a4aSRandall Stewart 	/* Express for fragmented delivery? */
1789f8829a4aSRandall Stewart 	if ((asoc->fragmented_delivery_inprogress) &&
1790f8829a4aSRandall Stewart 	    (stcb->asoc.control_pdapi) &&
1791f8829a4aSRandall Stewart 	    (asoc->str_of_pdapi == strmno) &&
1792f8829a4aSRandall Stewart 	    (asoc->ssn_of_pdapi == strmseq)
1793f8829a4aSRandall Stewart 	    ) {
1794f8829a4aSRandall Stewart 		control = stcb->asoc.control_pdapi;
1795f42a358aSRandall Stewart 		if ((chunk_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1796f8829a4aSRandall Stewart 			/* Can't be another first? */
1797f8829a4aSRandall Stewart 			goto failed_pdapi_express_del;
1798f8829a4aSRandall Stewart 		}
1799f8829a4aSRandall Stewart 		if (tsn == (control->sinfo_tsn + 1)) {
1800f8829a4aSRandall Stewart 			/* Yep, we can add it on */
1801f8829a4aSRandall Stewart 			int end = 0;
1802f8829a4aSRandall Stewart 
1803f42a358aSRandall Stewart 			if (chunk_flags & SCTP_DATA_LAST_FRAG) {
1804f8829a4aSRandall Stewart 				end = 1;
1805f8829a4aSRandall Stewart 			}
1806f8829a4aSRandall Stewart 			if (sctp_append_to_readq(stcb->sctp_ep, stcb, control, dmbuf, end,
1807f8829a4aSRandall Stewart 			    tsn,
1808f8829a4aSRandall Stewart 			    &stcb->sctp_socket->so_rcv)) {
1809ad81507eSRandall Stewart 				SCTP_PRINTF("Append fails end:%d\n", end);
1810f8829a4aSRandall Stewart 				goto failed_pdapi_express_del;
1811f8829a4aSRandall Stewart 			}
181277acdc25SRandall Stewart 			SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
181320b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1814830d754dSRandall Stewart 				asoc->highest_tsn_inside_nr_map = tsn;
1815830d754dSRandall Stewart 			}
1816f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvexpressm);
1817f8829a4aSRandall Stewart 			control->sinfo_tsn = tsn;
1818f8829a4aSRandall Stewart 			asoc->tsn_last_delivered = tsn;
1819f42a358aSRandall Stewart 			asoc->fragment_flags = chunk_flags;
1820f8829a4aSRandall Stewart 			asoc->tsn_of_pdapi_last_delivered = tsn;
1821f42a358aSRandall Stewart 			asoc->last_flags_delivered = chunk_flags;
1822f8829a4aSRandall Stewart 			asoc->last_strm_seq_delivered = strmseq;
1823f8829a4aSRandall Stewart 			asoc->last_strm_no_delivered = strmno;
1824f8829a4aSRandall Stewart 			if (end) {
1825f8829a4aSRandall Stewart 				/* clean up the flags and such */
1826f8829a4aSRandall Stewart 				asoc->fragmented_delivery_inprogress = 0;
1827f42a358aSRandall Stewart 				if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1828f8829a4aSRandall Stewart 					asoc->strmin[strmno].last_sequence_delivered++;
1829a5d547adSRandall Stewart 				}
1830f8829a4aSRandall Stewart 				stcb->asoc.control_pdapi = NULL;
1831139bc87fSRandall Stewart 				if (TAILQ_EMPTY(&asoc->reasmqueue) == 0) {
1832139bc87fSRandall Stewart 					/*
1833139bc87fSRandall Stewart 					 * There could be another message
1834139bc87fSRandall Stewart 					 * ready
1835139bc87fSRandall Stewart 					 */
1836139bc87fSRandall Stewart 					need_reasm_check = 1;
1837139bc87fSRandall Stewart 				}
1838f8829a4aSRandall Stewart 			}
1839f8829a4aSRandall Stewart 			control = NULL;
1840f8829a4aSRandall Stewart 			goto finish_express_del;
1841f8829a4aSRandall Stewart 		}
1842f8829a4aSRandall Stewart 	}
1843f8829a4aSRandall Stewart failed_pdapi_express_del:
1844f8829a4aSRandall Stewart 	control = NULL;
184577acdc25SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
184677acdc25SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
184720b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
184877acdc25SRandall Stewart 			asoc->highest_tsn_inside_nr_map = tsn;
184977acdc25SRandall Stewart 		}
185077acdc25SRandall Stewart 	} else {
185177acdc25SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
185220b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_map)) {
185377acdc25SRandall Stewart 			asoc->highest_tsn_inside_map = tsn;
185477acdc25SRandall Stewart 		}
185577acdc25SRandall Stewart 	}
1856f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
1857f8829a4aSRandall Stewart 		sctp_alloc_a_chunk(stcb, chk);
1858f8829a4aSRandall Stewart 		if (chk == NULL) {
1859f8829a4aSRandall Stewart 			/* No memory so we drop the chunk */
1860f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_nomem);
1861f8829a4aSRandall Stewart 			if (last_chunk == 0) {
1862f8829a4aSRandall Stewart 				/* we copied it, free the copy */
1863f8829a4aSRandall Stewart 				sctp_m_freem(dmbuf);
1864f8829a4aSRandall Stewart 			}
1865f8829a4aSRandall Stewart 			return (0);
1866f8829a4aSRandall Stewart 		}
1867f8829a4aSRandall Stewart 		chk->rec.data.TSN_seq = tsn;
1868f8829a4aSRandall Stewart 		chk->no_fr_allowed = 0;
1869f8829a4aSRandall Stewart 		chk->rec.data.stream_seq = strmseq;
1870f8829a4aSRandall Stewart 		chk->rec.data.stream_number = strmno;
1871f42a358aSRandall Stewart 		chk->rec.data.payloadtype = protocol_id;
1872f8829a4aSRandall Stewart 		chk->rec.data.context = stcb->asoc.context;
1873f8829a4aSRandall Stewart 		chk->rec.data.doing_fast_retransmit = 0;
1874f42a358aSRandall Stewart 		chk->rec.data.rcv_flags = chunk_flags;
1875f8829a4aSRandall Stewart 		chk->asoc = asoc;
1876f8829a4aSRandall Stewart 		chk->send_size = the_len;
1877f8829a4aSRandall Stewart 		chk->whoTo = net;
1878f8829a4aSRandall Stewart 		atomic_add_int(&net->ref_count, 1);
1879f8829a4aSRandall Stewart 		chk->data = dmbuf;
1880f8829a4aSRandall Stewart 	} else {
1881f8829a4aSRandall Stewart 		sctp_alloc_a_readq(stcb, control);
1882f8829a4aSRandall Stewart 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1883f42a358aSRandall Stewart 		    protocol_id,
1884f8829a4aSRandall Stewart 		    stcb->asoc.context,
1885f8829a4aSRandall Stewart 		    strmno, strmseq,
1886f42a358aSRandall Stewart 		    chunk_flags,
1887f8829a4aSRandall Stewart 		    dmbuf);
1888f8829a4aSRandall Stewart 		if (control == NULL) {
1889f8829a4aSRandall Stewart 			/* No memory so we drop the chunk */
1890f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_nomem);
1891f8829a4aSRandall Stewart 			if (last_chunk == 0) {
1892f8829a4aSRandall Stewart 				/* we copied it, free the copy */
1893f8829a4aSRandall Stewart 				sctp_m_freem(dmbuf);
1894f8829a4aSRandall Stewart 			}
1895f8829a4aSRandall Stewart 			return (0);
1896f8829a4aSRandall Stewart 		}
1897f8829a4aSRandall Stewart 		control->length = the_len;
1898f8829a4aSRandall Stewart 	}
1899f8829a4aSRandall Stewart 
1900f8829a4aSRandall Stewart 	/* Mark it as received */
1901f8829a4aSRandall Stewart 	/* Now queue it where it belongs */
1902f8829a4aSRandall Stewart 	if (control != NULL) {
1903f8829a4aSRandall Stewart 		/* First a sanity check */
1904f8829a4aSRandall Stewart 		if (asoc->fragmented_delivery_inprogress) {
1905f8829a4aSRandall Stewart 			/*
1906f8829a4aSRandall Stewart 			 * Ok, we have a fragmented delivery in progress if
1907f8829a4aSRandall Stewart 			 * this chunk is next to deliver OR belongs in our
1908f8829a4aSRandall Stewart 			 * view to the reassembly, the peer is evil or
1909f8829a4aSRandall Stewart 			 * broken.
1910f8829a4aSRandall Stewart 			 */
1911f8829a4aSRandall Stewart 			uint32_t estimate_tsn;
1912f8829a4aSRandall Stewart 
1913f8829a4aSRandall Stewart 			estimate_tsn = asoc->tsn_last_delivered + 1;
1914f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->reasmqueue) &&
1915f8829a4aSRandall Stewart 			    (estimate_tsn == control->sinfo_tsn)) {
1916f8829a4aSRandall Stewart 				/* Evil/Broke peer */
1917f8829a4aSRandall Stewart 				sctp_m_freem(control->data);
1918f8829a4aSRandall Stewart 				control->data = NULL;
19195bead436SRandall Stewart 				if (control->whoFrom) {
1920f8829a4aSRandall Stewart 					sctp_free_remote_addr(control->whoFrom);
19215bead436SRandall Stewart 					control->whoFrom = NULL;
19225bead436SRandall Stewart 				}
1923f8829a4aSRandall Stewart 				sctp_free_a_readq(stcb, control);
1924139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1925f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
1926f8829a4aSRandall Stewart 				if (oper) {
1927f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
1928f8829a4aSRandall Stewart 					uint32_t *ippp;
1929f8829a4aSRandall Stewart 
1930139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
1931f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
1932f8829a4aSRandall Stewart 					    (3 * sizeof(uint32_t));
1933f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
1934f8829a4aSRandall Stewart 					ph->param_type =
1935f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1936139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
1937f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
1938a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_15);
1939f8829a4aSRandall Stewart 					ippp++;
1940f8829a4aSRandall Stewart 					*ippp = tsn;
1941f8829a4aSRandall Stewart 					ippp++;
1942f8829a4aSRandall Stewart 					*ippp = ((strmno << 16) | strmseq);
1943f8829a4aSRandall Stewart 				}
1944a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_15;
1945f8829a4aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb,
1946ceaad40aSRandall Stewart 				    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1947f8829a4aSRandall Stewart 
1948f8829a4aSRandall Stewart 				*abort_flag = 1;
1949f8829a4aSRandall Stewart 				return (0);
1950f8829a4aSRandall Stewart 			} else {
1951f8829a4aSRandall Stewart 				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1952f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
1953f8829a4aSRandall Stewart 					control->data = NULL;
19545bead436SRandall Stewart 					if (control->whoFrom) {
1955f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
19565bead436SRandall Stewart 						control->whoFrom = NULL;
19575bead436SRandall Stewart 					}
1958f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
1959f8829a4aSRandall Stewart 
1960139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1961f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1962f8829a4aSRandall Stewart 					if (oper) {
1963f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1964f8829a4aSRandall Stewart 						uint32_t *ippp;
1965f8829a4aSRandall Stewart 
1966139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1967f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1968f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1969f8829a4aSRandall Stewart 						ph = mtod(oper,
1970f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1971f8829a4aSRandall Stewart 						ph->param_type =
1972f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1973f8829a4aSRandall Stewart 						ph->param_length =
1974139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1975f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1976a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_16);
1977f8829a4aSRandall Stewart 						ippp++;
1978f8829a4aSRandall Stewart 						*ippp = tsn;
1979f8829a4aSRandall Stewart 						ippp++;
1980f8829a4aSRandall Stewart 						*ippp = ((strmno << 16) | strmseq);
1981f8829a4aSRandall Stewart 					}
1982a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_16;
1983f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1984ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1985f8829a4aSRandall Stewart 
1986f8829a4aSRandall Stewart 					*abort_flag = 1;
1987f8829a4aSRandall Stewart 					return (0);
1988f8829a4aSRandall Stewart 				}
1989f8829a4aSRandall Stewart 			}
1990f8829a4aSRandall Stewart 		} else {
1991f8829a4aSRandall Stewart 			/* No PDAPI running */
1992f8829a4aSRandall Stewart 			if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
1993f8829a4aSRandall Stewart 				/*
1994f8829a4aSRandall Stewart 				 * Reassembly queue is NOT empty validate
1995f8829a4aSRandall Stewart 				 * that this tsn does not need to be in
1996f8829a4aSRandall Stewart 				 * reasembly queue. If it does then our peer
1997f8829a4aSRandall Stewart 				 * is broken or evil.
1998f8829a4aSRandall Stewart 				 */
1999f8829a4aSRandall Stewart 				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
2000f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
2001f8829a4aSRandall Stewart 					control->data = NULL;
20025bead436SRandall Stewart 					if (control->whoFrom) {
2003f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
20045bead436SRandall Stewart 						control->whoFrom = NULL;
20055bead436SRandall Stewart 					}
2006f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
2007139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
2008f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
2009f8829a4aSRandall Stewart 					if (oper) {
2010f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
2011f8829a4aSRandall Stewart 						uint32_t *ippp;
2012f8829a4aSRandall Stewart 
2013139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
2014f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
2015f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
2016f8829a4aSRandall Stewart 						ph = mtod(oper,
2017f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
2018f8829a4aSRandall Stewart 						ph->param_type =
2019f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2020f8829a4aSRandall Stewart 						ph->param_length =
2021139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
2022f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
2023a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_17);
2024f8829a4aSRandall Stewart 						ippp++;
2025f8829a4aSRandall Stewart 						*ippp = tsn;
2026f8829a4aSRandall Stewart 						ippp++;
2027f8829a4aSRandall Stewart 						*ippp = ((strmno << 16) | strmseq);
2028f8829a4aSRandall Stewart 					}
2029a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_17;
2030f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
2031ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2032f8829a4aSRandall Stewart 
2033f8829a4aSRandall Stewart 					*abort_flag = 1;
2034f8829a4aSRandall Stewart 					return (0);
2035f8829a4aSRandall Stewart 				}
2036f8829a4aSRandall Stewart 			}
2037f8829a4aSRandall Stewart 		}
2038f8829a4aSRandall Stewart 		/* ok, if we reach here we have passed the sanity checks */
2039f42a358aSRandall Stewart 		if (chunk_flags & SCTP_DATA_UNORDERED) {
2040f8829a4aSRandall Stewart 			/* queue directly into socket buffer */
2041b5c16493SMichael Tuexen 			sctp_mark_non_revokable(asoc, control->sinfo_tsn);
2042f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep, stcb,
2043f8829a4aSRandall Stewart 			    control,
2044cfde3ff7SRandall Stewart 			    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
2045f8829a4aSRandall Stewart 		} else {
2046f8829a4aSRandall Stewart 			/*
2047f8829a4aSRandall Stewart 			 * Special check for when streams are resetting. We
2048f8829a4aSRandall Stewart 			 * could be more smart about this and check the
2049f8829a4aSRandall Stewart 			 * actual stream to see if it is not being reset..
2050f8829a4aSRandall Stewart 			 * that way we would not create a HOLB when amongst
2051f8829a4aSRandall Stewart 			 * streams being reset and those not being reset.
2052f8829a4aSRandall Stewart 			 *
2053f8829a4aSRandall Stewart 			 * We take complete messages that have a stream reset
2054f8829a4aSRandall Stewart 			 * intervening (aka the TSN is after where our
2055f8829a4aSRandall Stewart 			 * cum-ack needs to be) off and put them on a
2056f8829a4aSRandall Stewart 			 * pending_reply_queue. The reassembly ones we do
2057f8829a4aSRandall Stewart 			 * not have to worry about since they are all sorted
2058f8829a4aSRandall Stewart 			 * and proceessed by TSN order. It is only the
2059f8829a4aSRandall Stewart 			 * singletons I must worry about.
2060f8829a4aSRandall Stewart 			 */
2061f8829a4aSRandall Stewart 			if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
206220b07a4dSMichael Tuexen 			    SCTP_TSN_GT(tsn, liste->tsn)) {
2063f8829a4aSRandall Stewart 				/*
2064f8829a4aSRandall Stewart 				 * yep its past where we need to reset... go
2065f8829a4aSRandall Stewart 				 * ahead and queue it.
2066f8829a4aSRandall Stewart 				 */
2067f8829a4aSRandall Stewart 				if (TAILQ_EMPTY(&asoc->pending_reply_queue)) {
2068f8829a4aSRandall Stewart 					/* first one on */
2069f8829a4aSRandall Stewart 					TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2070f8829a4aSRandall Stewart 				} else {
20714a9ef3f8SMichael Tuexen 					struct sctp_queued_to_read *ctlOn,
20724a9ef3f8SMichael Tuexen 					                   *nctlOn;
2073f8829a4aSRandall Stewart 					unsigned char inserted = 0;
2074f8829a4aSRandall Stewart 
20754a9ef3f8SMichael Tuexen 					TAILQ_FOREACH_SAFE(ctlOn, &asoc->pending_reply_queue, next, nctlOn) {
207620b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(control->sinfo_tsn, ctlOn->sinfo_tsn)) {
20774a9ef3f8SMichael Tuexen 							continue;
2078f8829a4aSRandall Stewart 						} else {
2079f8829a4aSRandall Stewart 							/* found it */
2080f8829a4aSRandall Stewart 							TAILQ_INSERT_BEFORE(ctlOn, control, next);
2081f8829a4aSRandall Stewart 							inserted = 1;
2082f8829a4aSRandall Stewart 							break;
2083f8829a4aSRandall Stewart 						}
2084f8829a4aSRandall Stewart 					}
2085f8829a4aSRandall Stewart 					if (inserted == 0) {
2086f8829a4aSRandall Stewart 						/*
2087f8829a4aSRandall Stewart 						 * must be put at end, use
2088f8829a4aSRandall Stewart 						 * prevP (all setup from
2089f8829a4aSRandall Stewart 						 * loop) to setup nextP.
2090f8829a4aSRandall Stewart 						 */
2091f8829a4aSRandall Stewart 						TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2092f8829a4aSRandall Stewart 					}
2093f8829a4aSRandall Stewart 				}
2094f8829a4aSRandall Stewart 			} else {
2095f8829a4aSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, control, abort_flag);
2096f8829a4aSRandall Stewart 				if (*abort_flag) {
2097f8829a4aSRandall Stewart 					return (0);
2098f8829a4aSRandall Stewart 				}
2099f8829a4aSRandall Stewart 			}
2100f8829a4aSRandall Stewart 		}
2101f8829a4aSRandall Stewart 	} else {
2102f8829a4aSRandall Stewart 		/* Into the re-assembly queue */
2103f8829a4aSRandall Stewart 		sctp_queue_data_for_reasm(stcb, asoc, chk, abort_flag);
2104f8829a4aSRandall Stewart 		if (*abort_flag) {
2105a5d547adSRandall Stewart 			/*
2106a5d547adSRandall Stewart 			 * the assoc is now gone and chk was put onto the
2107a5d547adSRandall Stewart 			 * reasm queue, which has all been freed.
2108a5d547adSRandall Stewart 			 */
2109a5d547adSRandall Stewart 			*m = NULL;
2110f8829a4aSRandall Stewart 			return (0);
2111f8829a4aSRandall Stewart 		}
2112f8829a4aSRandall Stewart 	}
2113f8829a4aSRandall Stewart finish_express_del:
2114307b49efSMichael Tuexen 	if (tsn == (asoc->cumulative_tsn + 1)) {
2115307b49efSMichael Tuexen 		/* Update cum-ack */
2116307b49efSMichael Tuexen 		asoc->cumulative_tsn = tsn;
2117307b49efSMichael Tuexen 	}
2118f8829a4aSRandall Stewart 	if (last_chunk) {
2119f8829a4aSRandall Stewart 		*m = NULL;
2120f8829a4aSRandall Stewart 	}
2121f42a358aSRandall Stewart 	if (ordered) {
2122f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_inorderchunks);
2123f8829a4aSRandall Stewart 	} else {
2124f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_inunorderchunks);
2125f8829a4aSRandall Stewart 	}
2126f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvdata);
2127f8829a4aSRandall Stewart 	/* Set it present please */
2128b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
21296a91f103SRandall Stewart 		sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno, SCTP_STR_LOG_FROM_MARK_TSN);
213080fefe0aSRandall Stewart 	}
2131b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2132f8829a4aSRandall Stewart 		sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2133f8829a4aSRandall Stewart 		    asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
213480fefe0aSRandall Stewart 	}
213517205eccSRandall Stewart 	/* check the special flag for stream resets */
213617205eccSRandall Stewart 	if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
213720b07a4dSMichael Tuexen 	    SCTP_TSN_GE(asoc->cumulative_tsn, liste->tsn)) {
213817205eccSRandall Stewart 		/*
213917205eccSRandall Stewart 		 * we have finished working through the backlogged TSN's now
214017205eccSRandall Stewart 		 * time to reset streams. 1: call reset function. 2: free
214117205eccSRandall Stewart 		 * pending_reply space 3: distribute any chunks in
214217205eccSRandall Stewart 		 * pending_reply_queue.
214317205eccSRandall Stewart 		 */
21444a9ef3f8SMichael Tuexen 		struct sctp_queued_to_read *ctl, *nctl;
214517205eccSRandall Stewart 
214617205eccSRandall Stewart 		sctp_reset_in_stream(stcb, liste->number_entries, liste->req.list_of_streams);
214717205eccSRandall Stewart 		TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
2148207304d4SRandall Stewart 		SCTP_FREE(liste, SCTP_M_STRESET);
21493c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
215017205eccSRandall Stewart 		liste = TAILQ_FIRST(&asoc->resetHead);
21514a9ef3f8SMichael Tuexen 		if (TAILQ_EMPTY(&asoc->resetHead)) {
215217205eccSRandall Stewart 			/* All can be removed */
21534a9ef3f8SMichael Tuexen 			TAILQ_FOREACH_SAFE(ctl, &asoc->pending_reply_queue, next, nctl) {
215417205eccSRandall Stewart 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
215517205eccSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
215617205eccSRandall Stewart 				if (*abort_flag) {
215717205eccSRandall Stewart 					return (0);
215817205eccSRandall Stewart 				}
215917205eccSRandall Stewart 			}
21604a9ef3f8SMichael Tuexen 		} else {
21614a9ef3f8SMichael Tuexen 			TAILQ_FOREACH_SAFE(ctl, &asoc->pending_reply_queue, next, nctl) {
216220b07a4dSMichael Tuexen 				if (SCTP_TSN_GT(ctl->sinfo_tsn, liste->tsn)) {
21634a9ef3f8SMichael Tuexen 					break;
21644a9ef3f8SMichael Tuexen 				}
216517205eccSRandall Stewart 				/*
216617205eccSRandall Stewart 				 * if ctl->sinfo_tsn is <= liste->tsn we can
216717205eccSRandall Stewart 				 * process it which is the NOT of
216817205eccSRandall Stewart 				 * ctl->sinfo_tsn > liste->tsn
216917205eccSRandall Stewart 				 */
217017205eccSRandall Stewart 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
217117205eccSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
217217205eccSRandall Stewart 				if (*abort_flag) {
217317205eccSRandall Stewart 					return (0);
217417205eccSRandall Stewart 				}
217517205eccSRandall Stewart 			}
217617205eccSRandall Stewart 		}
217717205eccSRandall Stewart 		/*
217817205eccSRandall Stewart 		 * Now service re-assembly to pick up anything that has been
217917205eccSRandall Stewart 		 * held on reassembly queue?
218017205eccSRandall Stewart 		 */
218117205eccSRandall Stewart 		sctp_deliver_reasm_check(stcb, asoc);
218217205eccSRandall Stewart 		need_reasm_check = 0;
218317205eccSRandall Stewart 	}
2184139bc87fSRandall Stewart 	if (need_reasm_check) {
2185139bc87fSRandall Stewart 		/* Another one waits ? */
2186139bc87fSRandall Stewart 		sctp_deliver_reasm_check(stcb, asoc);
2187139bc87fSRandall Stewart 	}
2188f8829a4aSRandall Stewart 	return (1);
2189f8829a4aSRandall Stewart }
2190f8829a4aSRandall Stewart 
2191f8829a4aSRandall Stewart int8_t sctp_map_lookup_tab[256] = {
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, 5,
2196b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2197b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2198b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2199b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 6,
2200b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2201b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2202b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2203b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
2204b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2205b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2206b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2207b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 7,
2208b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2209b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2210b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2211b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
2212b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2213b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2214b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2215b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 6,
2216b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2217b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2218b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2219b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
2220b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2221b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2222b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2223b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 8
2224f8829a4aSRandall Stewart };
2225f8829a4aSRandall Stewart 
2226f8829a4aSRandall Stewart 
2227f8829a4aSRandall Stewart void
2228b5c16493SMichael Tuexen sctp_slide_mapping_arrays(struct sctp_tcb *stcb)
2229f8829a4aSRandall Stewart {
2230f8829a4aSRandall Stewart 	/*
2231f8829a4aSRandall Stewart 	 * Now we also need to check the mapping array in a couple of ways.
2232f8829a4aSRandall Stewart 	 * 1) Did we move the cum-ack point?
223366bd30bdSRandall Stewart 	 *
223466bd30bdSRandall Stewart 	 * When you first glance at this you might think that all entries that
223566bd30bdSRandall Stewart 	 * make up the postion of the cum-ack would be in the nr-mapping
223666bd30bdSRandall Stewart 	 * array only.. i.e. things up to the cum-ack are always
223766bd30bdSRandall Stewart 	 * deliverable. Thats true with one exception, when its a fragmented
223866bd30bdSRandall Stewart 	 * message we may not deliver the data until some threshold (or all
223966bd30bdSRandall Stewart 	 * of it) is in place. So we must OR the nr_mapping_array and
224066bd30bdSRandall Stewart 	 * mapping_array to get a true picture of the cum-ack.
2241f8829a4aSRandall Stewart 	 */
2242f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2243b3f1ea41SRandall Stewart 	int at;
224466bd30bdSRandall Stewart 	uint8_t val;
2245f8829a4aSRandall Stewart 	int slide_from, slide_end, lgap, distance;
224677acdc25SRandall Stewart 	uint32_t old_cumack, old_base, old_highest, highest_tsn;
2247f8829a4aSRandall Stewart 
2248f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2249f8829a4aSRandall Stewart 
2250f8829a4aSRandall Stewart 	old_cumack = asoc->cumulative_tsn;
2251f8829a4aSRandall Stewart 	old_base = asoc->mapping_array_base_tsn;
2252f8829a4aSRandall Stewart 	old_highest = asoc->highest_tsn_inside_map;
2253f8829a4aSRandall Stewart 	/*
2254f8829a4aSRandall Stewart 	 * We could probably improve this a small bit by calculating the
2255f8829a4aSRandall Stewart 	 * offset of the current cum-ack as the starting point.
2256f8829a4aSRandall Stewart 	 */
2257f8829a4aSRandall Stewart 	at = 0;
2258b5c16493SMichael Tuexen 	for (slide_from = 0; slide_from < stcb->asoc.mapping_array_size; slide_from++) {
225966bd30bdSRandall Stewart 		val = asoc->nr_mapping_array[slide_from] | asoc->mapping_array[slide_from];
226066bd30bdSRandall Stewart 		if (val == 0xff) {
2261f8829a4aSRandall Stewart 			at += 8;
2262f8829a4aSRandall Stewart 		} else {
2263f8829a4aSRandall Stewart 			/* there is a 0 bit */
226466bd30bdSRandall Stewart 			at += sctp_map_lookup_tab[val];
2265f8829a4aSRandall Stewart 			break;
2266f8829a4aSRandall Stewart 		}
2267f8829a4aSRandall Stewart 	}
2268b5c16493SMichael Tuexen 	asoc->cumulative_tsn = asoc->mapping_array_base_tsn + (at - 1);
2269f8829a4aSRandall Stewart 
227020b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->cumulative_tsn, asoc->highest_tsn_inside_map) &&
227120b07a4dSMichael Tuexen 	    SCTP_TSN_GT(asoc->cumulative_tsn, asoc->highest_tsn_inside_nr_map)) {
2272a5d547adSRandall Stewart #ifdef INVARIANTS
2273ceaad40aSRandall Stewart 		panic("huh, cumack 0x%x greater than high-tsn 0x%x in map",
2274ceaad40aSRandall Stewart 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2275f8829a4aSRandall Stewart #else
2276ceaad40aSRandall Stewart 		SCTP_PRINTF("huh, cumack 0x%x greater than high-tsn 0x%x in map - should panic?\n",
2277ceaad40aSRandall Stewart 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
22780e13104dSRandall Stewart 		sctp_print_mapping_array(asoc);
2279b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2280b3f1ea41SRandall Stewart 			sctp_log_map(0, 6, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
2281b3f1ea41SRandall Stewart 		}
2282f8829a4aSRandall Stewart 		asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2283830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = asoc->cumulative_tsn;
2284f8829a4aSRandall Stewart #endif
2285f8829a4aSRandall Stewart 	}
228620b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
228777acdc25SRandall Stewart 		highest_tsn = asoc->highest_tsn_inside_nr_map;
228877acdc25SRandall Stewart 	} else {
228977acdc25SRandall Stewart 		highest_tsn = asoc->highest_tsn_inside_map;
229077acdc25SRandall Stewart 	}
229177acdc25SRandall Stewart 	if ((asoc->cumulative_tsn == highest_tsn) && (at >= 8)) {
2292f8829a4aSRandall Stewart 		/* The complete array was completed by a single FR */
229377acdc25SRandall Stewart 		/* highest becomes the cum-ack */
229437f144ebSMichael Tuexen 		int clr;
229537f144ebSMichael Tuexen 
229637f144ebSMichael Tuexen #ifdef INVARIANTS
229737f144ebSMichael Tuexen 		unsigned int i;
229837f144ebSMichael Tuexen 
229937f144ebSMichael Tuexen #endif
2300f8829a4aSRandall Stewart 
2301f8829a4aSRandall Stewart 		/* clear the array */
2302b5c16493SMichael Tuexen 		clr = ((at + 7) >> 3);
2303c4739e2fSRandall Stewart 		if (clr > asoc->mapping_array_size) {
2304f8829a4aSRandall Stewart 			clr = asoc->mapping_array_size;
2305f8829a4aSRandall Stewart 		}
2306f8829a4aSRandall Stewart 		memset(asoc->mapping_array, 0, clr);
2307830d754dSRandall Stewart 		memset(asoc->nr_mapping_array, 0, clr);
230837f144ebSMichael Tuexen #ifdef INVARIANTS
2309b5c16493SMichael Tuexen 		for (i = 0; i < asoc->mapping_array_size; i++) {
2310b5c16493SMichael Tuexen 			if ((asoc->mapping_array[i]) || (asoc->nr_mapping_array[i])) {
2311b5c16493SMichael Tuexen 				printf("Error Mapping array's not clean at clear\n");
2312b5c16493SMichael Tuexen 				sctp_print_mapping_array(asoc);
2313b5c16493SMichael Tuexen 			}
2314b5c16493SMichael Tuexen 		}
231537f144ebSMichael Tuexen #endif
231677acdc25SRandall Stewart 		asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
231777acdc25SRandall Stewart 		asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2318f8829a4aSRandall Stewart 	} else if (at >= 8) {
2319f8829a4aSRandall Stewart 		/* we can slide the mapping array down */
2320b3f1ea41SRandall Stewart 		/* slide_from holds where we hit the first NON 0xff byte */
2321b3f1ea41SRandall Stewart 
2322f8829a4aSRandall Stewart 		/*
2323f8829a4aSRandall Stewart 		 * now calculate the ceiling of the move using our highest
2324f8829a4aSRandall Stewart 		 * TSN value
2325f8829a4aSRandall Stewart 		 */
232677acdc25SRandall Stewart 		SCTP_CALC_TSN_TO_GAP(lgap, highest_tsn, asoc->mapping_array_base_tsn);
232777acdc25SRandall Stewart 		slide_end = (lgap >> 3);
2328f8829a4aSRandall Stewart 		if (slide_end < slide_from) {
232977acdc25SRandall Stewart 			sctp_print_mapping_array(asoc);
2330d55b0b1bSRandall Stewart #ifdef INVARIANTS
2331f8829a4aSRandall Stewart 			panic("impossible slide");
2332d55b0b1bSRandall Stewart #else
233377acdc25SRandall Stewart 			printf("impossible slide lgap:%x slide_end:%x slide_from:%x? at:%d\n",
233477acdc25SRandall Stewart 			    lgap, slide_end, slide_from, at);
2335d55b0b1bSRandall Stewart 			return;
2336d55b0b1bSRandall Stewart #endif
2337f8829a4aSRandall Stewart 		}
2338b3f1ea41SRandall Stewart 		if (slide_end > asoc->mapping_array_size) {
2339b3f1ea41SRandall Stewart #ifdef INVARIANTS
2340b3f1ea41SRandall Stewart 			panic("would overrun buffer");
2341b3f1ea41SRandall Stewart #else
2342b3f1ea41SRandall Stewart 			printf("Gak, would have overrun map end:%d slide_end:%d\n",
2343b3f1ea41SRandall Stewart 			    asoc->mapping_array_size, slide_end);
2344b3f1ea41SRandall Stewart 			slide_end = asoc->mapping_array_size;
2345b3f1ea41SRandall Stewart #endif
2346b3f1ea41SRandall Stewart 		}
2347f8829a4aSRandall Stewart 		distance = (slide_end - slide_from) + 1;
2348b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2349f8829a4aSRandall Stewart 			sctp_log_map(old_base, old_cumack, old_highest,
2350f8829a4aSRandall Stewart 			    SCTP_MAP_PREPARE_SLIDE);
2351f8829a4aSRandall Stewart 			sctp_log_map((uint32_t) slide_from, (uint32_t) slide_end,
2352f8829a4aSRandall Stewart 			    (uint32_t) lgap, SCTP_MAP_SLIDE_FROM);
235380fefe0aSRandall Stewart 		}
2354f8829a4aSRandall Stewart 		if (distance + slide_from > asoc->mapping_array_size ||
2355f8829a4aSRandall Stewart 		    distance < 0) {
2356f8829a4aSRandall Stewart 			/*
2357f8829a4aSRandall Stewart 			 * Here we do NOT slide forward the array so that
2358f8829a4aSRandall Stewart 			 * hopefully when more data comes in to fill it up
2359f8829a4aSRandall Stewart 			 * we will be able to slide it forward. Really I
2360f8829a4aSRandall Stewart 			 * don't think this should happen :-0
2361f8829a4aSRandall Stewart 			 */
2362f8829a4aSRandall Stewart 
2363b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2364f8829a4aSRandall Stewart 				sctp_log_map((uint32_t) distance, (uint32_t) slide_from,
2365f8829a4aSRandall Stewart 				    (uint32_t) asoc->mapping_array_size,
2366f8829a4aSRandall Stewart 				    SCTP_MAP_SLIDE_NONE);
236780fefe0aSRandall Stewart 			}
2368f8829a4aSRandall Stewart 		} else {
2369f8829a4aSRandall Stewart 			int ii;
2370f8829a4aSRandall Stewart 
2371f8829a4aSRandall Stewart 			for (ii = 0; ii < distance; ii++) {
237237f144ebSMichael Tuexen 				asoc->mapping_array[ii] = asoc->mapping_array[slide_from + ii];
237337f144ebSMichael Tuexen 				asoc->nr_mapping_array[ii] = asoc->nr_mapping_array[slide_from + ii];
237477acdc25SRandall Stewart 
2375f8829a4aSRandall Stewart 			}
2376aed5947cSMichael Tuexen 			for (ii = distance; ii < asoc->mapping_array_size; ii++) {
2377f8829a4aSRandall Stewart 				asoc->mapping_array[ii] = 0;
237877acdc25SRandall Stewart 				asoc->nr_mapping_array[ii] = 0;
2379f8829a4aSRandall Stewart 			}
2380ee94f0a2SMichael Tuexen 			if (asoc->highest_tsn_inside_map + 1 == asoc->mapping_array_base_tsn) {
2381ee94f0a2SMichael Tuexen 				asoc->highest_tsn_inside_map += (slide_from << 3);
2382ee94f0a2SMichael Tuexen 			}
2383ee94f0a2SMichael Tuexen 			if (asoc->highest_tsn_inside_nr_map + 1 == asoc->mapping_array_base_tsn) {
2384ee94f0a2SMichael Tuexen 				asoc->highest_tsn_inside_nr_map += (slide_from << 3);
2385ee94f0a2SMichael Tuexen 			}
2386f8829a4aSRandall Stewart 			asoc->mapping_array_base_tsn += (slide_from << 3);
2387b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2388f8829a4aSRandall Stewart 				sctp_log_map(asoc->mapping_array_base_tsn,
2389f8829a4aSRandall Stewart 				    asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
2390f8829a4aSRandall Stewart 				    SCTP_MAP_SLIDE_RESULT);
239180fefe0aSRandall Stewart 			}
2392830d754dSRandall Stewart 		}
2393830d754dSRandall Stewart 	}
2394b5c16493SMichael Tuexen }
2395b5c16493SMichael Tuexen 
2396b5c16493SMichael Tuexen void
23977215cc1bSMichael Tuexen sctp_sack_check(struct sctp_tcb *stcb, int was_a_gap)
2398b5c16493SMichael Tuexen {
2399b5c16493SMichael Tuexen 	struct sctp_association *asoc;
2400b5c16493SMichael Tuexen 	uint32_t highest_tsn;
2401b5c16493SMichael Tuexen 
2402b5c16493SMichael Tuexen 	asoc = &stcb->asoc;
240320b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
2404b5c16493SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_nr_map;
2405b5c16493SMichael Tuexen 	} else {
2406b5c16493SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_map;
2407b5c16493SMichael Tuexen 	}
2408b5c16493SMichael Tuexen 
2409830d754dSRandall Stewart 	/*
2410f8829a4aSRandall Stewart 	 * Now we need to see if we need to queue a sack or just start the
2411f8829a4aSRandall Stewart 	 * timer (if allowed).
2412f8829a4aSRandall Stewart 	 */
2413f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2414f8829a4aSRandall Stewart 		/*
2415b5c16493SMichael Tuexen 		 * Ok special case, in SHUTDOWN-SENT case. here we maker
2416b5c16493SMichael Tuexen 		 * sure SACK timer is off and instead send a SHUTDOWN and a
2417b5c16493SMichael Tuexen 		 * SACK
2418f8829a4aSRandall Stewart 		 */
2419139bc87fSRandall Stewart 		if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2420f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
2421a5d547adSRandall Stewart 			    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_INDATA + SCTP_LOC_18);
2422f8829a4aSRandall Stewart 		}
2423ca85e948SMichael Tuexen 		sctp_send_shutdown(stcb,
2424ca85e948SMichael Tuexen 		    ((stcb->asoc.alternate) ? stcb->asoc.alternate : stcb->asoc.primary_destination));
2425689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
2426f8829a4aSRandall Stewart 	} else {
2427f8829a4aSRandall Stewart 		int is_a_gap;
2428f8829a4aSRandall Stewart 
2429f8829a4aSRandall Stewart 		/* is there a gap now ? */
243020b07a4dSMichael Tuexen 		is_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
2431f8829a4aSRandall Stewart 
2432f8829a4aSRandall Stewart 		/*
2433b5c16493SMichael Tuexen 		 * CMT DAC algorithm: increase number of packets received
2434b5c16493SMichael Tuexen 		 * since last ack
2435f8829a4aSRandall Stewart 		 */
2436f8829a4aSRandall Stewart 		stcb->asoc.cmt_dac_pkts_rcvd++;
2437f8829a4aSRandall Stewart 
243842551e99SRandall Stewart 		if ((stcb->asoc.send_sack == 1) ||	/* We need to send a
243942551e99SRandall Stewart 							 * SACK */
2440f8829a4aSRandall Stewart 		    ((was_a_gap) && (is_a_gap == 0)) ||	/* was a gap, but no
2441f8829a4aSRandall Stewart 							 * longer is one */
2442f8829a4aSRandall Stewart 		    (stcb->asoc.numduptsns) ||	/* we have dup's */
2443f8829a4aSRandall Stewart 		    (is_a_gap) ||	/* is still a gap */
244442551e99SRandall Stewart 		    (stcb->asoc.delayed_ack == 0) ||	/* Delayed sack disabled */
244542551e99SRandall Stewart 		    (stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq)	/* hit limit of pkts */
2446f8829a4aSRandall Stewart 		    ) {
2447f8829a4aSRandall Stewart 
24487c99d56fSMichael Tuexen 			if ((stcb->asoc.sctp_cmt_on_off > 0) &&
2449b3f1ea41SRandall Stewart 			    (SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) &&
245042551e99SRandall Stewart 			    (stcb->asoc.send_sack == 0) &&
2451f8829a4aSRandall Stewart 			    (stcb->asoc.numduptsns == 0) &&
2452f8829a4aSRandall Stewart 			    (stcb->asoc.delayed_ack) &&
2453139bc87fSRandall Stewart 			    (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer))) {
2454f8829a4aSRandall Stewart 
2455f8829a4aSRandall Stewart 				/*
2456b5c16493SMichael Tuexen 				 * CMT DAC algorithm: With CMT, delay acks
2457b5c16493SMichael Tuexen 				 * even in the face of
2458f8829a4aSRandall Stewart 				 *
2459b5c16493SMichael Tuexen 				 * reordering. Therefore, if acks that do not
2460b5c16493SMichael Tuexen 				 * have to be sent because of the above
2461b5c16493SMichael Tuexen 				 * reasons, will be delayed. That is, acks
2462b5c16493SMichael Tuexen 				 * that would have been sent due to gap
2463b5c16493SMichael Tuexen 				 * reports will be delayed with DAC. Start
2464f8829a4aSRandall Stewart 				 * the delayed ack timer.
2465f8829a4aSRandall Stewart 				 */
2466f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2467f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, NULL);
2468f8829a4aSRandall Stewart 			} else {
2469f8829a4aSRandall Stewart 				/*
2470b5c16493SMichael Tuexen 				 * Ok we must build a SACK since the timer
2471b5c16493SMichael Tuexen 				 * is pending, we got our first packet OR
2472b5c16493SMichael Tuexen 				 * there are gaps or duplicates.
2473f8829a4aSRandall Stewart 				 */
2474ad81507eSRandall Stewart 				(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2475689e6a5fSMichael Tuexen 				sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
2476f8829a4aSRandall Stewart 			}
2477f8829a4aSRandall Stewart 		} else {
247842551e99SRandall Stewart 			if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2479f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2480f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, NULL);
2481f8829a4aSRandall Stewart 			}
2482f8829a4aSRandall Stewart 		}
2483f8829a4aSRandall Stewart 	}
2484f8829a4aSRandall Stewart }
2485f8829a4aSRandall Stewart 
2486f8829a4aSRandall Stewart void
2487f8829a4aSRandall Stewart sctp_service_queues(struct sctp_tcb *stcb, struct sctp_association *asoc)
2488f8829a4aSRandall Stewart {
2489f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
2490810ec536SMichael Tuexen 	uint32_t tsize, pd_point;
2491f8829a4aSRandall Stewart 	uint16_t nxt_todel;
2492f8829a4aSRandall Stewart 
2493f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
2494f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
2495f8829a4aSRandall Stewart 	}
2496f8829a4aSRandall Stewart 	/* Can we proceed further, i.e. the PD-API is complete */
2497f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
2498f8829a4aSRandall Stewart 		/* no */
2499f8829a4aSRandall Stewart 		return;
2500f8829a4aSRandall Stewart 	}
2501f8829a4aSRandall Stewart 	/*
2502f8829a4aSRandall Stewart 	 * Now is there some other chunk I can deliver from the reassembly
2503f8829a4aSRandall Stewart 	 * queue.
2504f8829a4aSRandall Stewart 	 */
2505139bc87fSRandall Stewart doit_again:
2506f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
2507f8829a4aSRandall Stewart 	if (chk == NULL) {
2508f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = 0;
2509f8829a4aSRandall Stewart 		asoc->cnt_on_reasm_queue = 0;
2510f8829a4aSRandall Stewart 		return;
2511f8829a4aSRandall Stewart 	}
2512f8829a4aSRandall Stewart 	nxt_todel = asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
2513f8829a4aSRandall Stewart 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
2514f8829a4aSRandall Stewart 	    ((nxt_todel == chk->rec.data.stream_seq) ||
2515f8829a4aSRandall Stewart 	    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
2516f8829a4aSRandall Stewart 		/*
2517f8829a4aSRandall Stewart 		 * Yep the first one is here. We setup to start reception,
2518f8829a4aSRandall Stewart 		 * by backing down the TSN just in case we can't deliver.
2519f8829a4aSRandall Stewart 		 */
2520f8829a4aSRandall Stewart 
2521f8829a4aSRandall Stewart 		/*
2522f8829a4aSRandall Stewart 		 * Before we start though either all of the message should
252324ae5c4aSMichael Tuexen 		 * be here or the socket buffer max or nothing on the
2524f8829a4aSRandall Stewart 		 * delivery queue and something can be delivered.
2525f8829a4aSRandall Stewart 		 */
2526810ec536SMichael Tuexen 		if (stcb->sctp_socket) {
252724ae5c4aSMichael Tuexen 			pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket),
2528810ec536SMichael Tuexen 			    stcb->sctp_ep->partial_delivery_point);
2529810ec536SMichael Tuexen 		} else {
2530810ec536SMichael Tuexen 			pd_point = stcb->sctp_ep->partial_delivery_point;
2531810ec536SMichael Tuexen 		}
2532810ec536SMichael Tuexen 		if (sctp_is_all_msg_on_reasm(asoc, &tsize) || (tsize >= pd_point)) {
2533f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 1;
2534f8829a4aSRandall Stewart 			asoc->tsn_last_delivered = chk->rec.data.TSN_seq - 1;
2535f8829a4aSRandall Stewart 			asoc->str_of_pdapi = chk->rec.data.stream_number;
2536f8829a4aSRandall Stewart 			asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
2537f8829a4aSRandall Stewart 			asoc->pdapi_ppid = chk->rec.data.payloadtype;
2538f8829a4aSRandall Stewart 			asoc->fragment_flags = chk->rec.data.rcv_flags;
2539f8829a4aSRandall Stewart 			sctp_service_reassembly(stcb, asoc);
2540139bc87fSRandall Stewart 			if (asoc->fragmented_delivery_inprogress == 0) {
2541139bc87fSRandall Stewart 				goto doit_again;
2542139bc87fSRandall Stewart 			}
2543f8829a4aSRandall Stewart 		}
2544f8829a4aSRandall Stewart 	}
2545f8829a4aSRandall Stewart }
2546f8829a4aSRandall Stewart 
2547f8829a4aSRandall Stewart int
2548f8829a4aSRandall Stewart sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
2549f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2550f8829a4aSRandall Stewart     struct sctp_nets *net, uint32_t * high_tsn)
2551f8829a4aSRandall Stewart {
2552f8829a4aSRandall Stewart 	struct sctp_data_chunk *ch, chunk_buf;
2553f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2554f8829a4aSRandall Stewart 	int num_chunks = 0;	/* number of control chunks processed */
2555f8829a4aSRandall Stewart 	int stop_proc = 0;
2556f8829a4aSRandall Stewart 	int chk_length, break_flag, last_chunk;
25578f777478SMichael Tuexen 	int abort_flag = 0, was_a_gap;
2558f8829a4aSRandall Stewart 	struct mbuf *m;
25598f777478SMichael Tuexen 	uint32_t highest_tsn;
2560f8829a4aSRandall Stewart 
2561f8829a4aSRandall Stewart 	/* set the rwnd */
2562f8829a4aSRandall Stewart 	sctp_set_rwnd(stcb, &stcb->asoc);
2563f8829a4aSRandall Stewart 
2564f8829a4aSRandall Stewart 	m = *mm;
2565f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
2566f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
256720b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
25688f777478SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_nr_map;
25698f777478SMichael Tuexen 	} else {
25708f777478SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_map;
2571f8829a4aSRandall Stewart 	}
257220b07a4dSMichael Tuexen 	was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
2573f8829a4aSRandall Stewart 	/*
2574f8829a4aSRandall Stewart 	 * setup where we got the last DATA packet from for any SACK that
2575f8829a4aSRandall Stewart 	 * may need to go out. Don't bump the net. This is done ONLY when a
2576f8829a4aSRandall Stewart 	 * chunk is assigned.
2577f8829a4aSRandall Stewart 	 */
2578f8829a4aSRandall Stewart 	asoc->last_data_chunk_from = net;
2579f8829a4aSRandall Stewart 
2580d06c82f1SRandall Stewart 	/*-
2581f8829a4aSRandall Stewart 	 * Now before we proceed we must figure out if this is a wasted
2582f8829a4aSRandall Stewart 	 * cluster... i.e. it is a small packet sent in and yet the driver
2583f8829a4aSRandall Stewart 	 * underneath allocated a full cluster for it. If so we must copy it
2584f8829a4aSRandall Stewart 	 * to a smaller mbuf and free up the cluster mbuf. This will help
2585d06c82f1SRandall Stewart 	 * with cluster starvation. Note for __Panda__ we don't do this
2586d06c82f1SRandall Stewart 	 * since it has clusters all the way down to 64 bytes.
2587f8829a4aSRandall Stewart 	 */
258844b7479bSRandall Stewart 	if (SCTP_BUF_LEN(m) < (long)MLEN && SCTP_BUF_NEXT(m) == NULL) {
2589f8829a4aSRandall Stewart 		/* we only handle mbufs that are singletons.. not chains */
2590139bc87fSRandall Stewart 		m = sctp_get_mbuf_for_msg(SCTP_BUF_LEN(m), 0, M_DONTWAIT, 1, MT_DATA);
2591f8829a4aSRandall Stewart 		if (m) {
2592f8829a4aSRandall Stewart 			/* ok lets see if we can copy the data up */
2593f8829a4aSRandall Stewart 			caddr_t *from, *to;
2594f8829a4aSRandall Stewart 
2595f8829a4aSRandall Stewart 			/* get the pointers and copy */
2596f8829a4aSRandall Stewart 			to = mtod(m, caddr_t *);
2597f8829a4aSRandall Stewart 			from = mtod((*mm), caddr_t *);
2598139bc87fSRandall Stewart 			memcpy(to, from, SCTP_BUF_LEN((*mm)));
2599f8829a4aSRandall Stewart 			/* copy the length and free up the old */
2600139bc87fSRandall Stewart 			SCTP_BUF_LEN(m) = SCTP_BUF_LEN((*mm));
2601f8829a4aSRandall Stewart 			sctp_m_freem(*mm);
2602f8829a4aSRandall Stewart 			/* sucess, back copy */
2603f8829a4aSRandall Stewart 			*mm = m;
2604f8829a4aSRandall Stewart 		} else {
2605f8829a4aSRandall Stewart 			/* We are in trouble in the mbuf world .. yikes */
2606f8829a4aSRandall Stewart 			m = *mm;
2607f8829a4aSRandall Stewart 		}
2608f8829a4aSRandall Stewart 	}
2609f8829a4aSRandall Stewart 	/* get pointer to the first chunk header */
2610f8829a4aSRandall Stewart 	ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2611f8829a4aSRandall Stewart 	    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2612f8829a4aSRandall Stewart 	if (ch == NULL) {
2613f8829a4aSRandall Stewart 		return (1);
2614f8829a4aSRandall Stewart 	}
2615f8829a4aSRandall Stewart 	/*
2616f8829a4aSRandall Stewart 	 * process all DATA chunks...
2617f8829a4aSRandall Stewart 	 */
2618f8829a4aSRandall Stewart 	*high_tsn = asoc->cumulative_tsn;
2619f8829a4aSRandall Stewart 	break_flag = 0;
262042551e99SRandall Stewart 	asoc->data_pkts_seen++;
2621f8829a4aSRandall Stewart 	while (stop_proc == 0) {
2622f8829a4aSRandall Stewart 		/* validate chunk length */
2623f8829a4aSRandall Stewart 		chk_length = ntohs(ch->ch.chunk_length);
2624f8829a4aSRandall Stewart 		if (length - *offset < chk_length) {
2625f8829a4aSRandall Stewart 			/* all done, mutulated chunk */
2626f8829a4aSRandall Stewart 			stop_proc = 1;
2627*60990c0cSMichael Tuexen 			continue;
2628f8829a4aSRandall Stewart 		}
2629f8829a4aSRandall Stewart 		if (ch->ch.chunk_type == SCTP_DATA) {
2630f8829a4aSRandall Stewart 			if ((size_t)chk_length < sizeof(struct sctp_data_chunk) + 1) {
2631f8829a4aSRandall Stewart 				/*
2632f8829a4aSRandall Stewart 				 * Need to send an abort since we had a
2633f8829a4aSRandall Stewart 				 * invalid data chunk.
2634f8829a4aSRandall Stewart 				 */
2635f8829a4aSRandall Stewart 				struct mbuf *op_err;
2636f8829a4aSRandall Stewart 
2637139bc87fSRandall Stewart 				op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 2 * sizeof(uint32_t)),
2638f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
2639f8829a4aSRandall Stewart 
2640f8829a4aSRandall Stewart 				if (op_err) {
2641f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
2642f8829a4aSRandall Stewart 					uint32_t *ippp;
2643f8829a4aSRandall Stewart 
2644139bc87fSRandall Stewart 					SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr) +
2645f8829a4aSRandall Stewart 					    (2 * sizeof(uint32_t));
2646f8829a4aSRandall Stewart 					ph = mtod(op_err, struct sctp_paramhdr *);
2647f8829a4aSRandall Stewart 					ph->param_type =
2648f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2649139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(op_err));
2650f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
2651a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_19);
2652f8829a4aSRandall Stewart 					ippp++;
2653f8829a4aSRandall Stewart 					*ippp = asoc->cumulative_tsn;
2654f8829a4aSRandall Stewart 
2655f8829a4aSRandall Stewart 				}
2656a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19;
2657f8829a4aSRandall Stewart 				sctp_abort_association(inp, stcb, m, iphlen, sh,
2658c54a18d2SRandall Stewart 				    op_err, 0, net->port);
2659f8829a4aSRandall Stewart 				return (2);
2660f8829a4aSRandall Stewart 			}
2661f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
2662f8829a4aSRandall Stewart 			sctp_audit_log(0xB1, 0);
2663f8829a4aSRandall Stewart #endif
2664f8829a4aSRandall Stewart 			if (SCTP_SIZE32(chk_length) == (length - *offset)) {
2665f8829a4aSRandall Stewart 				last_chunk = 1;
2666f8829a4aSRandall Stewart 			} else {
2667f8829a4aSRandall Stewart 				last_chunk = 0;
2668f8829a4aSRandall Stewart 			}
2669f8829a4aSRandall Stewart 			if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset, ch,
2670f8829a4aSRandall Stewart 			    chk_length, net, high_tsn, &abort_flag, &break_flag,
2671f8829a4aSRandall Stewart 			    last_chunk)) {
2672f8829a4aSRandall Stewart 				num_chunks++;
2673f8829a4aSRandall Stewart 			}
2674f8829a4aSRandall Stewart 			if (abort_flag)
2675f8829a4aSRandall Stewart 				return (2);
2676f8829a4aSRandall Stewart 
2677f8829a4aSRandall Stewart 			if (break_flag) {
2678f8829a4aSRandall Stewart 				/*
2679f8829a4aSRandall Stewart 				 * Set because of out of rwnd space and no
2680f8829a4aSRandall Stewart 				 * drop rep space left.
2681f8829a4aSRandall Stewart 				 */
2682f8829a4aSRandall Stewart 				stop_proc = 1;
2683*60990c0cSMichael Tuexen 				continue;
2684f8829a4aSRandall Stewart 			}
2685f8829a4aSRandall Stewart 		} else {
2686f8829a4aSRandall Stewart 			/* not a data chunk in the data region */
2687f8829a4aSRandall Stewart 			switch (ch->ch.chunk_type) {
2688f8829a4aSRandall Stewart 			case SCTP_INITIATION:
2689f8829a4aSRandall Stewart 			case SCTP_INITIATION_ACK:
2690f8829a4aSRandall Stewart 			case SCTP_SELECTIVE_ACK:
2691*60990c0cSMichael Tuexen 			case SCTP_NR_SELECTIVE_ACK:
2692f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_REQUEST:
2693f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_ACK:
2694f8829a4aSRandall Stewart 			case SCTP_ABORT_ASSOCIATION:
2695f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN:
2696f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_ACK:
2697f8829a4aSRandall Stewart 			case SCTP_OPERATION_ERROR:
2698f8829a4aSRandall Stewart 			case SCTP_COOKIE_ECHO:
2699f8829a4aSRandall Stewart 			case SCTP_COOKIE_ACK:
2700f8829a4aSRandall Stewart 			case SCTP_ECN_ECHO:
2701f8829a4aSRandall Stewart 			case SCTP_ECN_CWR:
2702f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_COMPLETE:
2703f8829a4aSRandall Stewart 			case SCTP_AUTHENTICATION:
2704f8829a4aSRandall Stewart 			case SCTP_ASCONF_ACK:
2705f8829a4aSRandall Stewart 			case SCTP_PACKET_DROPPED:
2706f8829a4aSRandall Stewart 			case SCTP_STREAM_RESET:
2707f8829a4aSRandall Stewart 			case SCTP_FORWARD_CUM_TSN:
2708f8829a4aSRandall Stewart 			case SCTP_ASCONF:
2709f8829a4aSRandall Stewart 				/*
2710f8829a4aSRandall Stewart 				 * Now, what do we do with KNOWN chunks that
2711f8829a4aSRandall Stewart 				 * are NOT in the right place?
2712f8829a4aSRandall Stewart 				 *
2713f8829a4aSRandall Stewart 				 * For now, I do nothing but ignore them. We
2714f8829a4aSRandall Stewart 				 * may later want to add sysctl stuff to
2715f8829a4aSRandall Stewart 				 * switch out and do either an ABORT() or
2716f8829a4aSRandall Stewart 				 * possibly process them.
2717f8829a4aSRandall Stewart 				 */
2718b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_strict_data_order)) {
2719f8829a4aSRandall Stewart 					struct mbuf *op_err;
2720f8829a4aSRandall Stewart 
2721f8829a4aSRandall Stewart 					op_err = sctp_generate_invmanparam(SCTP_CAUSE_PROTOCOL_VIOLATION);
2722c54a18d2SRandall Stewart 					sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 0, net->port);
2723f8829a4aSRandall Stewart 					return (2);
2724f8829a4aSRandall Stewart 				}
2725f8829a4aSRandall Stewart 				break;
2726f8829a4aSRandall Stewart 			default:
2727f8829a4aSRandall Stewart 				/* unknown chunk type, use bit rules */
2728f8829a4aSRandall Stewart 				if (ch->ch.chunk_type & 0x40) {
2729f8829a4aSRandall Stewart 					/* Add a error report to the queue */
2730d61a0ae0SRandall Stewart 					struct mbuf *merr;
2731f8829a4aSRandall Stewart 					struct sctp_paramhdr *phd;
2732f8829a4aSRandall Stewart 
2733d61a0ae0SRandall Stewart 					merr = sctp_get_mbuf_for_msg(sizeof(*phd), 0, M_DONTWAIT, 1, MT_DATA);
2734d61a0ae0SRandall Stewart 					if (merr) {
2735d61a0ae0SRandall Stewart 						phd = mtod(merr, struct sctp_paramhdr *);
2736f8829a4aSRandall Stewart 						/*
2737f8829a4aSRandall Stewart 						 * We cheat and use param
2738f8829a4aSRandall Stewart 						 * type since we did not
2739f8829a4aSRandall Stewart 						 * bother to define a error
2740f8829a4aSRandall Stewart 						 * cause struct. They are
2741f8829a4aSRandall Stewart 						 * the same basic format
2742f8829a4aSRandall Stewart 						 * with different names.
2743f8829a4aSRandall Stewart 						 */
2744f8829a4aSRandall Stewart 						phd->param_type =
2745f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_UNRECOG_CHUNK);
2746f8829a4aSRandall Stewart 						phd->param_length =
2747f8829a4aSRandall Stewart 						    htons(chk_length + sizeof(*phd));
2748d61a0ae0SRandall Stewart 						SCTP_BUF_LEN(merr) = sizeof(*phd);
2749d61a0ae0SRandall Stewart 						SCTP_BUF_NEXT(merr) = SCTP_M_COPYM(m, *offset,
2750f8829a4aSRandall Stewart 						    SCTP_SIZE32(chk_length),
2751f8829a4aSRandall Stewart 						    M_DONTWAIT);
2752d61a0ae0SRandall Stewart 						if (SCTP_BUF_NEXT(merr)) {
2753d61a0ae0SRandall Stewart 							sctp_queue_op_err(stcb, merr);
2754f8829a4aSRandall Stewart 						} else {
2755d61a0ae0SRandall Stewart 							sctp_m_freem(merr);
2756f8829a4aSRandall Stewart 						}
2757f8829a4aSRandall Stewart 					}
2758f8829a4aSRandall Stewart 				}
2759f8829a4aSRandall Stewart 				if ((ch->ch.chunk_type & 0x80) == 0) {
2760f8829a4aSRandall Stewart 					/* discard the rest of this packet */
2761f8829a4aSRandall Stewart 					stop_proc = 1;
2762f8829a4aSRandall Stewart 				}	/* else skip this bad chunk and
2763f8829a4aSRandall Stewart 					 * continue... */
2764f8829a4aSRandall Stewart 				break;
2765*60990c0cSMichael Tuexen 			}	/* switch of chunk type */
2766f8829a4aSRandall Stewart 		}
2767f8829a4aSRandall Stewart 		*offset += SCTP_SIZE32(chk_length);
2768f8829a4aSRandall Stewart 		if ((*offset >= length) || stop_proc) {
2769f8829a4aSRandall Stewart 			/* no more data left in the mbuf chain */
2770f8829a4aSRandall Stewart 			stop_proc = 1;
2771f8829a4aSRandall Stewart 			continue;
2772f8829a4aSRandall Stewart 		}
2773f8829a4aSRandall Stewart 		ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2774f8829a4aSRandall Stewart 		    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2775f8829a4aSRandall Stewart 		if (ch == NULL) {
2776f8829a4aSRandall Stewart 			*offset = length;
2777f8829a4aSRandall Stewart 			stop_proc = 1;
2778*60990c0cSMichael Tuexen 			continue;
2779f8829a4aSRandall Stewart 		}
2780*60990c0cSMichael Tuexen 	}
2781f8829a4aSRandall Stewart 	if (break_flag) {
2782f8829a4aSRandall Stewart 		/*
2783f8829a4aSRandall Stewart 		 * we need to report rwnd overrun drops.
2784f8829a4aSRandall Stewart 		 */
2785f8829a4aSRandall Stewart 		sctp_send_packet_dropped(stcb, net, *mm, iphlen, 0);
2786f8829a4aSRandall Stewart 	}
2787f8829a4aSRandall Stewart 	if (num_chunks) {
2788f8829a4aSRandall Stewart 		/*
2789ceaad40aSRandall Stewart 		 * Did we get data, if so update the time for auto-close and
2790f8829a4aSRandall Stewart 		 * give peer credit for being alive.
2791f8829a4aSRandall Stewart 		 */
2792f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvpktwithdata);
2793b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
2794c4739e2fSRandall Stewart 			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2795c4739e2fSRandall Stewart 			    stcb->asoc.overall_error_count,
2796c4739e2fSRandall Stewart 			    0,
2797c4739e2fSRandall Stewart 			    SCTP_FROM_SCTP_INDATA,
2798c4739e2fSRandall Stewart 			    __LINE__);
2799c4739e2fSRandall Stewart 		}
2800f8829a4aSRandall Stewart 		stcb->asoc.overall_error_count = 0;
28016e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
2802f8829a4aSRandall Stewart 	}
2803f8829a4aSRandall Stewart 	/* now service all of the reassm queue if needed */
2804f8829a4aSRandall Stewart 	if (!(TAILQ_EMPTY(&asoc->reasmqueue)))
2805f8829a4aSRandall Stewart 		sctp_service_queues(stcb, asoc);
2806f8829a4aSRandall Stewart 
2807f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
280842551e99SRandall Stewart 		/* Assure that we ack right away */
280942551e99SRandall Stewart 		stcb->asoc.send_sack = 1;
2810f8829a4aSRandall Stewart 	}
2811f8829a4aSRandall Stewart 	/* Start a sack timer or QUEUE a SACK for sending */
28127215cc1bSMichael Tuexen 	sctp_sack_check(stcb, was_a_gap);
2813f8829a4aSRandall Stewart 	return (0);
2814f8829a4aSRandall Stewart }
2815f8829a4aSRandall Stewart 
28160fa753b3SRandall Stewart static int
28170fa753b3SRandall Stewart sctp_process_segment_range(struct sctp_tcb *stcb, struct sctp_tmit_chunk **p_tp1, uint32_t last_tsn,
28180fa753b3SRandall Stewart     uint16_t frag_strt, uint16_t frag_end, int nr_sacking,
28190fa753b3SRandall Stewart     int *num_frs,
28200fa753b3SRandall Stewart     uint32_t * biggest_newly_acked_tsn,
28210fa753b3SRandall Stewart     uint32_t * this_sack_lowest_newack,
28227215cc1bSMichael Tuexen     int *rto_ok)
28230fa753b3SRandall Stewart {
28240fa753b3SRandall Stewart 	struct sctp_tmit_chunk *tp1;
28250fa753b3SRandall Stewart 	unsigned int theTSN;
2826b5c16493SMichael Tuexen 	int j, wake_him = 0, circled = 0;
28270fa753b3SRandall Stewart 
28280fa753b3SRandall Stewart 	/* Recover the tp1 we last saw */
28290fa753b3SRandall Stewart 	tp1 = *p_tp1;
28300fa753b3SRandall Stewart 	if (tp1 == NULL) {
28310fa753b3SRandall Stewart 		tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
28320fa753b3SRandall Stewart 	}
28330fa753b3SRandall Stewart 	for (j = frag_strt; j <= frag_end; j++) {
28340fa753b3SRandall Stewart 		theTSN = j + last_tsn;
28350fa753b3SRandall Stewart 		while (tp1) {
28360fa753b3SRandall Stewart 			if (tp1->rec.data.doing_fast_retransmit)
28370fa753b3SRandall Stewart 				(*num_frs) += 1;
28380fa753b3SRandall Stewart 
28390fa753b3SRandall Stewart 			/*-
28400fa753b3SRandall Stewart 			 * CMT: CUCv2 algorithm. For each TSN being
28410fa753b3SRandall Stewart 			 * processed from the sent queue, track the
28420fa753b3SRandall Stewart 			 * next expected pseudo-cumack, or
28430fa753b3SRandall Stewart 			 * rtx_pseudo_cumack, if required. Separate
28440fa753b3SRandall Stewart 			 * cumack trackers for first transmissions,
28450fa753b3SRandall Stewart 			 * and retransmissions.
28460fa753b3SRandall Stewart 			 */
28470fa753b3SRandall Stewart 			if ((tp1->whoTo->find_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
28480fa753b3SRandall Stewart 			    (tp1->snd_count == 1)) {
28490fa753b3SRandall Stewart 				tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
28500fa753b3SRandall Stewart 				tp1->whoTo->find_pseudo_cumack = 0;
28510fa753b3SRandall Stewart 			}
28520fa753b3SRandall Stewart 			if ((tp1->whoTo->find_rtx_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
28530fa753b3SRandall Stewart 			    (tp1->snd_count > 1)) {
28540fa753b3SRandall Stewart 				tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
28550fa753b3SRandall Stewart 				tp1->whoTo->find_rtx_pseudo_cumack = 0;
28560fa753b3SRandall Stewart 			}
28570fa753b3SRandall Stewart 			if (tp1->rec.data.TSN_seq == theTSN) {
28580fa753b3SRandall Stewart 				if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
28590fa753b3SRandall Stewart 					/*-
28600fa753b3SRandall Stewart 					 * must be held until
28610fa753b3SRandall Stewart 					 * cum-ack passes
28620fa753b3SRandall Stewart 					 */
28630fa753b3SRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
28640fa753b3SRandall Stewart 						/*-
28650fa753b3SRandall Stewart 						 * If it is less than RESEND, it is
28660fa753b3SRandall Stewart 						 * now no-longer in flight.
28670fa753b3SRandall Stewart 						 * Higher values may already be set
28680fa753b3SRandall Stewart 						 * via previous Gap Ack Blocks...
28690fa753b3SRandall Stewart 						 * i.e. ACKED or RESEND.
28700fa753b3SRandall Stewart 						 */
287120b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
287220b07a4dSMichael Tuexen 						    *biggest_newly_acked_tsn)) {
28730fa753b3SRandall Stewart 							*biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
28740fa753b3SRandall Stewart 						}
28750fa753b3SRandall Stewart 						/*-
28760fa753b3SRandall Stewart 						 * CMT: SFR algo (and HTNA) - set
28770fa753b3SRandall Stewart 						 * saw_newack to 1 for dest being
28780fa753b3SRandall Stewart 						 * newly acked. update
28790fa753b3SRandall Stewart 						 * this_sack_highest_newack if
28800fa753b3SRandall Stewart 						 * appropriate.
28810fa753b3SRandall Stewart 						 */
28820fa753b3SRandall Stewart 						if (tp1->rec.data.chunk_was_revoked == 0)
28830fa753b3SRandall Stewart 							tp1->whoTo->saw_newack = 1;
28840fa753b3SRandall Stewart 
288520b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
288620b07a4dSMichael Tuexen 						    tp1->whoTo->this_sack_highest_newack)) {
28870fa753b3SRandall Stewart 							tp1->whoTo->this_sack_highest_newack =
28880fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq;
28890fa753b3SRandall Stewart 						}
28900fa753b3SRandall Stewart 						/*-
28910fa753b3SRandall Stewart 						 * CMT DAC algo: also update
28920fa753b3SRandall Stewart 						 * this_sack_lowest_newack
28930fa753b3SRandall Stewart 						 */
28940fa753b3SRandall Stewart 						if (*this_sack_lowest_newack == 0) {
28950fa753b3SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
28960fa753b3SRandall Stewart 								sctp_log_sack(*this_sack_lowest_newack,
28970fa753b3SRandall Stewart 								    last_tsn,
28980fa753b3SRandall Stewart 								    tp1->rec.data.TSN_seq,
28990fa753b3SRandall Stewart 								    0,
29000fa753b3SRandall Stewart 								    0,
29010fa753b3SRandall Stewart 								    SCTP_LOG_TSN_ACKED);
29020fa753b3SRandall Stewart 							}
29030fa753b3SRandall Stewart 							*this_sack_lowest_newack = tp1->rec.data.TSN_seq;
29040fa753b3SRandall Stewart 						}
29050fa753b3SRandall Stewart 						/*-
29060fa753b3SRandall Stewart 						 * CMT: CUCv2 algorithm. If (rtx-)pseudo-cumack for corresp
29070fa753b3SRandall Stewart 						 * dest is being acked, then we have a new (rtx-)pseudo-cumack. Set
29080fa753b3SRandall Stewart 						 * new_(rtx_)pseudo_cumack to TRUE so that the cwnd for this dest can be
29090fa753b3SRandall Stewart 						 * updated. Also trigger search for the next expected (rtx-)pseudo-cumack.
29100fa753b3SRandall Stewart 						 * Separate pseudo_cumack trackers for first transmissions and
29110fa753b3SRandall Stewart 						 * retransmissions.
29120fa753b3SRandall Stewart 						 */
29130fa753b3SRandall Stewart 						if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
29140fa753b3SRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0) {
29150fa753b3SRandall Stewart 								tp1->whoTo->new_pseudo_cumack = 1;
29160fa753b3SRandall Stewart 							}
29170fa753b3SRandall Stewart 							tp1->whoTo->find_pseudo_cumack = 1;
29180fa753b3SRandall Stewart 						}
29190fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
29200fa753b3SRandall Stewart 							sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
29210fa753b3SRandall Stewart 						}
29220fa753b3SRandall Stewart 						if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
29230fa753b3SRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0) {
29240fa753b3SRandall Stewart 								tp1->whoTo->new_pseudo_cumack = 1;
29250fa753b3SRandall Stewart 							}
29260fa753b3SRandall Stewart 							tp1->whoTo->find_rtx_pseudo_cumack = 1;
29270fa753b3SRandall Stewart 						}
29280fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
29290fa753b3SRandall Stewart 							sctp_log_sack(*biggest_newly_acked_tsn,
29300fa753b3SRandall Stewart 							    last_tsn,
29310fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq,
29320fa753b3SRandall Stewart 							    frag_strt,
29330fa753b3SRandall Stewart 							    frag_end,
29340fa753b3SRandall Stewart 							    SCTP_LOG_TSN_ACKED);
29350fa753b3SRandall Stewart 						}
29360fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
29370fa753b3SRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
29380fa753b3SRandall Stewart 							    tp1->whoTo->flight_size,
29390fa753b3SRandall Stewart 							    tp1->book_size,
29400fa753b3SRandall Stewart 							    (uintptr_t) tp1->whoTo,
29410fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq);
29420fa753b3SRandall Stewart 						}
29430fa753b3SRandall Stewart 						sctp_flight_size_decrease(tp1);
2944299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
2945299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
2946299108c5SRandall Stewart 							    tp1);
2947299108c5SRandall Stewart 						}
29480fa753b3SRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
29490fa753b3SRandall Stewart 
29500fa753b3SRandall Stewart 						tp1->whoTo->net_ack += tp1->send_size;
29510fa753b3SRandall Stewart 						if (tp1->snd_count < 2) {
29520fa753b3SRandall Stewart 							/*-
29530fa753b3SRandall Stewart 							 * True non-retransmited chunk
29540fa753b3SRandall Stewart 							 */
29550fa753b3SRandall Stewart 							tp1->whoTo->net_ack2 += tp1->send_size;
29560fa753b3SRandall Stewart 
29570fa753b3SRandall Stewart 							/*-
29580fa753b3SRandall Stewart 							 * update RTO too ?
29590fa753b3SRandall Stewart 							 */
29600fa753b3SRandall Stewart 							if (tp1->do_rtt) {
2961f79aab18SRandall Stewart 								if (*rto_ok) {
29620fa753b3SRandall Stewart 									tp1->whoTo->RTO =
29630fa753b3SRandall Stewart 									    sctp_calculate_rto(stcb,
29640fa753b3SRandall Stewart 									    &stcb->asoc,
29650fa753b3SRandall Stewart 									    tp1->whoTo,
29660fa753b3SRandall Stewart 									    &tp1->sent_rcv_time,
2967899288aeSRandall Stewart 									    sctp_align_safe_nocopy,
2968f79aab18SRandall Stewart 									    SCTP_RTT_FROM_DATA);
2969f79aab18SRandall Stewart 									*rto_ok = 0;
2970f79aab18SRandall Stewart 								}
2971f79aab18SRandall Stewart 								if (tp1->whoTo->rto_needed == 0) {
2972f79aab18SRandall Stewart 									tp1->whoTo->rto_needed = 1;
2973f79aab18SRandall Stewart 								}
29740fa753b3SRandall Stewart 								tp1->do_rtt = 0;
29750fa753b3SRandall Stewart 							}
29760fa753b3SRandall Stewart 						}
29770fa753b3SRandall Stewart 					}
29780fa753b3SRandall Stewart 					if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
297920b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
298020b07a4dSMichael Tuexen 						    stcb->asoc.this_sack_highest_gap)) {
29810fa753b3SRandall Stewart 							stcb->asoc.this_sack_highest_gap =
29820fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq;
29830fa753b3SRandall Stewart 						}
29840fa753b3SRandall Stewart 						if (tp1->sent == SCTP_DATAGRAM_RESEND) {
29850fa753b3SRandall Stewart 							sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
29860fa753b3SRandall Stewart #ifdef SCTP_AUDITING_ENABLED
29870fa753b3SRandall Stewart 							sctp_audit_log(0xB2,
29880fa753b3SRandall Stewart 							    (stcb->asoc.sent_queue_retran_cnt & 0x000000ff));
29890fa753b3SRandall Stewart #endif
29900fa753b3SRandall Stewart 						}
29910fa753b3SRandall Stewart 					}
29920fa753b3SRandall Stewart 					/*-
29930fa753b3SRandall Stewart 					 * All chunks NOT UNSENT fall through here and are marked
29940fa753b3SRandall Stewart 					 * (leave PR-SCTP ones that are to skip alone though)
29950fa753b3SRandall Stewart 					 */
29960fa753b3SRandall Stewart 					if (tp1->sent != SCTP_FORWARD_TSN_SKIP)
29970fa753b3SRandall Stewart 						tp1->sent = SCTP_DATAGRAM_MARKED;
29980fa753b3SRandall Stewart 
29990fa753b3SRandall Stewart 					if (tp1->rec.data.chunk_was_revoked) {
30000fa753b3SRandall Stewart 						/* deflate the cwnd */
30010fa753b3SRandall Stewart 						tp1->whoTo->cwnd -= tp1->book_size;
30020fa753b3SRandall Stewart 						tp1->rec.data.chunk_was_revoked = 0;
30030fa753b3SRandall Stewart 					}
30040fa753b3SRandall Stewart 					/* NR Sack code here */
30050fa753b3SRandall Stewart 					if (nr_sacking) {
30060fa753b3SRandall Stewart 						if (tp1->data) {
30070fa753b3SRandall Stewart 							/*
30080fa753b3SRandall Stewart 							 * sa_ignore
30090fa753b3SRandall Stewart 							 * NO_NULL_CHK
30100fa753b3SRandall Stewart 							 */
30110fa753b3SRandall Stewart 							sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
30120fa753b3SRandall Stewart 							sctp_m_freem(tp1->data);
30130fa753b3SRandall Stewart 							tp1->data = NULL;
3014b5c16493SMichael Tuexen 						}
30150fa753b3SRandall Stewart 						wake_him++;
30160fa753b3SRandall Stewart 					}
30170fa753b3SRandall Stewart 				}
30180fa753b3SRandall Stewart 				break;
30190fa753b3SRandall Stewart 			}	/* if (tp1->TSN_seq == theTSN) */
302020b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, theTSN)) {
30210fa753b3SRandall Stewart 				break;
302220b07a4dSMichael Tuexen 			}
30230fa753b3SRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3024b5c16493SMichael Tuexen 			if ((tp1 == NULL) && (circled == 0)) {
3025b5c16493SMichael Tuexen 				circled++;
30260fa753b3SRandall Stewart 				tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
30270fa753b3SRandall Stewart 			}
3028b5c16493SMichael Tuexen 		}		/* end while (tp1) */
3029b5c16493SMichael Tuexen 		if (tp1 == NULL) {
3030b5c16493SMichael Tuexen 			circled = 0;
3031b5c16493SMichael Tuexen 			tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
3032b5c16493SMichael Tuexen 		}
3033b5c16493SMichael Tuexen 		/* In case the fragments were not in order we must reset */
30340fa753b3SRandall Stewart 	}			/* end for (j = fragStart */
30350fa753b3SRandall Stewart 	*p_tp1 = tp1;
30360fa753b3SRandall Stewart 	return (wake_him);	/* Return value only used for nr-sack */
30370fa753b3SRandall Stewart }
30380fa753b3SRandall Stewart 
30390fa753b3SRandall Stewart 
3040cd554309SMichael Tuexen static int
3041458303daSRandall Stewart sctp_handle_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
3042cd554309SMichael Tuexen     uint32_t last_tsn, uint32_t * biggest_tsn_acked,
3043139bc87fSRandall Stewart     uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
30447215cc1bSMichael Tuexen     int num_seg, int num_nr_seg, int *rto_ok)
3045f8829a4aSRandall Stewart {
3046458303daSRandall Stewart 	struct sctp_gap_ack_block *frag, block;
3047f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
30480fa753b3SRandall Stewart 	int i;
3049f8829a4aSRandall Stewart 	int num_frs = 0;
3050cd554309SMichael Tuexen 	int chunk_freed;
3051cd554309SMichael Tuexen 	int non_revocable;
3052d9c5cfeaSMichael Tuexen 	uint16_t frag_strt, frag_end, prev_frag_end;
3053f8829a4aSRandall Stewart 
3054d9c5cfeaSMichael Tuexen 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
3055d9c5cfeaSMichael Tuexen 	prev_frag_end = 0;
3056cd554309SMichael Tuexen 	chunk_freed = 0;
3057f8829a4aSRandall Stewart 
3058cd554309SMichael Tuexen 	for (i = 0; i < (num_seg + num_nr_seg); i++) {
3059d9c5cfeaSMichael Tuexen 		if (i == num_seg) {
3060d9c5cfeaSMichael Tuexen 			prev_frag_end = 0;
3061d9c5cfeaSMichael Tuexen 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
3062d9c5cfeaSMichael Tuexen 		}
3063458303daSRandall Stewart 		frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3064458303daSRandall Stewart 		    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
3065458303daSRandall Stewart 		*offset += sizeof(block);
3066458303daSRandall Stewart 		if (frag == NULL) {
3067cd554309SMichael Tuexen 			return (chunk_freed);
3068458303daSRandall Stewart 		}
3069f8829a4aSRandall Stewart 		frag_strt = ntohs(frag->start);
3070f8829a4aSRandall Stewart 		frag_end = ntohs(frag->end);
3071d9c5cfeaSMichael Tuexen 
3072f8829a4aSRandall Stewart 		if (frag_strt > frag_end) {
3073d9c5cfeaSMichael Tuexen 			/* This gap report is malformed, skip it. */
3074f8829a4aSRandall Stewart 			continue;
3075f8829a4aSRandall Stewart 		}
3076d9c5cfeaSMichael Tuexen 		if (frag_strt <= prev_frag_end) {
3077d9c5cfeaSMichael Tuexen 			/* This gap report is not in order, so restart. */
3078f8829a4aSRandall Stewart 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
3079f8829a4aSRandall Stewart 		}
308020b07a4dSMichael Tuexen 		if (SCTP_TSN_GT((last_tsn + frag_end), *biggest_tsn_acked)) {
3081d9c5cfeaSMichael Tuexen 			*biggest_tsn_acked = last_tsn + frag_end;
3082f8829a4aSRandall Stewart 		}
3083cd554309SMichael Tuexen 		if (i < num_seg) {
3084cd554309SMichael Tuexen 			non_revocable = 0;
3085cd554309SMichael Tuexen 		} else {
3086cd554309SMichael Tuexen 			non_revocable = 1;
3087cd554309SMichael Tuexen 		}
3088cd554309SMichael Tuexen 		if (sctp_process_segment_range(stcb, &tp1, last_tsn, frag_strt, frag_end,
3089cd554309SMichael Tuexen 		    non_revocable, &num_frs, biggest_newly_acked_tsn,
30907215cc1bSMichael Tuexen 		    this_sack_lowest_newack, rto_ok)) {
3091cd554309SMichael Tuexen 			chunk_freed = 1;
3092458303daSRandall Stewart 		}
3093d9c5cfeaSMichael Tuexen 		prev_frag_end = frag_end;
3094f8829a4aSRandall Stewart 	}
3095b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
309680fefe0aSRandall Stewart 		if (num_frs)
309780fefe0aSRandall Stewart 			sctp_log_fr(*biggest_tsn_acked,
309880fefe0aSRandall Stewart 			    *biggest_newly_acked_tsn,
309980fefe0aSRandall Stewart 			    last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
310080fefe0aSRandall Stewart 	}
3101cd554309SMichael Tuexen 	return (chunk_freed);
3102f8829a4aSRandall Stewart }
3103f8829a4aSRandall Stewart 
3104f8829a4aSRandall Stewart static void
3105c105859eSRandall Stewart sctp_check_for_revoked(struct sctp_tcb *stcb,
3106c105859eSRandall Stewart     struct sctp_association *asoc, uint32_t cumack,
310763eda93dSMichael Tuexen     uint32_t biggest_tsn_acked)
3108f8829a4aSRandall Stewart {
3109f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
3110f8829a4aSRandall Stewart 	int tot_revoked = 0;
3111f8829a4aSRandall Stewart 
31124a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
311320b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, cumack)) {
3114f8829a4aSRandall Stewart 			/*
3115f8829a4aSRandall Stewart 			 * ok this guy is either ACK or MARKED. If it is
3116f8829a4aSRandall Stewart 			 * ACKED it has been previously acked but not this
3117f8829a4aSRandall Stewart 			 * time i.e. revoked.  If it is MARKED it was ACK'ed
3118f8829a4aSRandall Stewart 			 * again.
3119f8829a4aSRandall Stewart 			 */
312020b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, biggest_tsn_acked)) {
3121d06c82f1SRandall Stewart 				break;
312220b07a4dSMichael Tuexen 			}
3123f8829a4aSRandall Stewart 			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
3124f8829a4aSRandall Stewart 				/* it has been revoked */
3125f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_SENT;
3126f8829a4aSRandall Stewart 				tp1->rec.data.chunk_was_revoked = 1;
3127a5d547adSRandall Stewart 				/*
312842551e99SRandall Stewart 				 * We must add this stuff back in to assure
312942551e99SRandall Stewart 				 * timers and such get started.
3130a5d547adSRandall Stewart 				 */
3131b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3132c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
3133c105859eSRandall Stewart 					    tp1->whoTo->flight_size,
3134c105859eSRandall Stewart 					    tp1->book_size,
3135c105859eSRandall Stewart 					    (uintptr_t) tp1->whoTo,
3136c105859eSRandall Stewart 					    tp1->rec.data.TSN_seq);
313780fefe0aSRandall Stewart 				}
3138c105859eSRandall Stewart 				sctp_flight_size_increase(tp1);
3139c105859eSRandall Stewart 				sctp_total_flight_increase(stcb, tp1);
314042551e99SRandall Stewart 				/*
314142551e99SRandall Stewart 				 * We inflate the cwnd to compensate for our
314242551e99SRandall Stewart 				 * artificial inflation of the flight_size.
314342551e99SRandall Stewart 				 */
314442551e99SRandall Stewart 				tp1->whoTo->cwnd += tp1->book_size;
3145f8829a4aSRandall Stewart 				tot_revoked++;
3146b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3147f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
3148f8829a4aSRandall Stewart 					    cumack,
3149f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
3150f8829a4aSRandall Stewart 					    0,
3151f8829a4aSRandall Stewart 					    0,
3152f8829a4aSRandall Stewart 					    SCTP_LOG_TSN_REVOKED);
315380fefe0aSRandall Stewart 				}
3154f8829a4aSRandall Stewart 			} else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
3155f8829a4aSRandall Stewart 				/* it has been re-acked in this SACK */
3156f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
3157f8829a4aSRandall Stewart 			}
3158f8829a4aSRandall Stewart 		}
3159f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT)
3160f8829a4aSRandall Stewart 			break;
3161f8829a4aSRandall Stewart 	}
3162f8829a4aSRandall Stewart }
3163f8829a4aSRandall Stewart 
3164830d754dSRandall Stewart 
3165f8829a4aSRandall Stewart static void
3166f8829a4aSRandall Stewart sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
316763eda93dSMichael Tuexen     uint32_t biggest_tsn_acked, uint32_t biggest_tsn_newly_acked, uint32_t this_sack_lowest_newack, int accum_moved)
3168f8829a4aSRandall Stewart {
3169f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
3170f8829a4aSRandall Stewart 	int strike_flag = 0;
3171f8829a4aSRandall Stewart 	struct timeval now;
3172f8829a4aSRandall Stewart 	int tot_retrans = 0;
3173f8829a4aSRandall Stewart 	uint32_t sending_seq;
3174f8829a4aSRandall Stewart 	struct sctp_nets *net;
3175f8829a4aSRandall Stewart 	int num_dests_sacked = 0;
3176f8829a4aSRandall Stewart 
3177f8829a4aSRandall Stewart 	/*
3178f8829a4aSRandall Stewart 	 * select the sending_seq, this is either the next thing ready to be
3179f8829a4aSRandall Stewart 	 * sent but not transmitted, OR, the next seq we assign.
3180f8829a4aSRandall Stewart 	 */
3181f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
3182f8829a4aSRandall Stewart 	if (tp1 == NULL) {
3183f8829a4aSRandall Stewart 		sending_seq = asoc->sending_seq;
3184f8829a4aSRandall Stewart 	} else {
3185f8829a4aSRandall Stewart 		sending_seq = tp1->rec.data.TSN_seq;
3186f8829a4aSRandall Stewart 	}
3187f8829a4aSRandall Stewart 
3188f8829a4aSRandall Stewart 	/* CMT DAC algo: finding out if SACK is a mixed SACK */
31897c99d56fSMichael Tuexen 	if ((asoc->sctp_cmt_on_off > 0) &&
319020083c2eSMichael Tuexen 	    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3191f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3192f8829a4aSRandall Stewart 			if (net->saw_newack)
3193f8829a4aSRandall Stewart 				num_dests_sacked++;
3194f8829a4aSRandall Stewart 		}
3195f8829a4aSRandall Stewart 	}
3196f8829a4aSRandall Stewart 	if (stcb->asoc.peer_supports_prsctp) {
31976e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
3198f8829a4aSRandall Stewart 	}
31994a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
3200f8829a4aSRandall Stewart 		strike_flag = 0;
3201f8829a4aSRandall Stewart 		if (tp1->no_fr_allowed) {
3202f8829a4aSRandall Stewart 			/* this one had a timeout or something */
3203f8829a4aSRandall Stewart 			continue;
3204f8829a4aSRandall Stewart 		}
3205b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3206f8829a4aSRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND)
3207f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3208f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3209f8829a4aSRandall Stewart 				    tp1->sent,
3210f8829a4aSRandall Stewart 				    SCTP_FR_LOG_CHECK_STRIKE);
321180fefe0aSRandall Stewart 		}
321220b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, biggest_tsn_acked) ||
3213f8829a4aSRandall Stewart 		    tp1->sent == SCTP_DATAGRAM_UNSENT) {
3214f8829a4aSRandall Stewart 			/* done */
3215f8829a4aSRandall Stewart 			break;
3216f8829a4aSRandall Stewart 		}
3217f8829a4aSRandall Stewart 		if (stcb->asoc.peer_supports_prsctp) {
3218f8829a4aSRandall Stewart 			if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3219f8829a4aSRandall Stewart 				/* Is it expired? */
322099ddc825SMichael Tuexen 				if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3221f8829a4aSRandall Stewart 					/* Yes so drop it */
3222f8829a4aSRandall Stewart 					if (tp1->data != NULL) {
3223ad81507eSRandall Stewart 						(void)sctp_release_pr_sctp_chunk(stcb, tp1,
3224f8829a4aSRandall Stewart 						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
32250c0982b8SRandall Stewart 						    SCTP_SO_NOT_LOCKED);
3226f8829a4aSRandall Stewart 					}
3227f8829a4aSRandall Stewart 					continue;
3228f8829a4aSRandall Stewart 				}
3229f8829a4aSRandall Stewart 			}
3230f8829a4aSRandall Stewart 		}
323120b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, asoc->this_sack_highest_gap)) {
3232f8829a4aSRandall Stewart 			/* we are beyond the tsn in the sack  */
3233f8829a4aSRandall Stewart 			break;
3234f8829a4aSRandall Stewart 		}
3235f8829a4aSRandall Stewart 		if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
3236f8829a4aSRandall Stewart 			/* either a RESEND, ACKED, or MARKED */
3237f8829a4aSRandall Stewart 			/* skip */
323844fbe462SRandall Stewart 			if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
323944fbe462SRandall Stewart 				/* Continue strikin FWD-TSN chunks */
324044fbe462SRandall Stewart 				tp1->rec.data.fwd_tsn_cnt++;
324144fbe462SRandall Stewart 			}
3242f8829a4aSRandall Stewart 			continue;
3243f8829a4aSRandall Stewart 		}
3244f8829a4aSRandall Stewart 		/*
3245f8829a4aSRandall Stewart 		 * CMT : SFR algo (covers part of DAC and HTNA as well)
3246f8829a4aSRandall Stewart 		 */
3247ad81507eSRandall Stewart 		if (tp1->whoTo && tp1->whoTo->saw_newack == 0) {
3248f8829a4aSRandall Stewart 			/*
3249f8829a4aSRandall Stewart 			 * No new acks were receieved for data sent to this
3250f8829a4aSRandall Stewart 			 * dest. Therefore, according to the SFR algo for
3251f8829a4aSRandall Stewart 			 * CMT, no data sent to this dest can be marked for
3252c105859eSRandall Stewart 			 * FR using this SACK.
3253f8829a4aSRandall Stewart 			 */
3254f8829a4aSRandall Stewart 			continue;
325520b07a4dSMichael Tuexen 		} else if (tp1->whoTo && SCTP_TSN_GT(tp1->rec.data.TSN_seq,
325620b07a4dSMichael Tuexen 		    tp1->whoTo->this_sack_highest_newack)) {
3257f8829a4aSRandall Stewart 			/*
3258f8829a4aSRandall Stewart 			 * CMT: New acks were receieved for data sent to
3259f8829a4aSRandall Stewart 			 * this dest. But no new acks were seen for data
3260f8829a4aSRandall Stewart 			 * sent after tp1. Therefore, according to the SFR
3261f8829a4aSRandall Stewart 			 * algo for CMT, tp1 cannot be marked for FR using
3262f8829a4aSRandall Stewart 			 * this SACK. This step covers part of the DAC algo
3263f8829a4aSRandall Stewart 			 * and the HTNA algo as well.
3264f8829a4aSRandall Stewart 			 */
3265f8829a4aSRandall Stewart 			continue;
3266f8829a4aSRandall Stewart 		}
3267f8829a4aSRandall Stewart 		/*
3268f8829a4aSRandall Stewart 		 * Here we check to see if we were have already done a FR
3269f8829a4aSRandall Stewart 		 * and if so we see if the biggest TSN we saw in the sack is
3270f8829a4aSRandall Stewart 		 * smaller than the recovery point. If so we don't strike
3271f8829a4aSRandall Stewart 		 * the tsn... otherwise we CAN strike the TSN.
3272f8829a4aSRandall Stewart 		 */
3273f8829a4aSRandall Stewart 		/*
327442551e99SRandall Stewart 		 * @@@ JRI: Check for CMT if (accum_moved &&
327542551e99SRandall Stewart 		 * asoc->fast_retran_loss_recovery && (sctp_cmt_on_off ==
327642551e99SRandall Stewart 		 * 0)) {
3277f8829a4aSRandall Stewart 		 */
327842551e99SRandall Stewart 		if (accum_moved && asoc->fast_retran_loss_recovery) {
3279f8829a4aSRandall Stewart 			/*
3280f8829a4aSRandall Stewart 			 * Strike the TSN if in fast-recovery and cum-ack
3281f8829a4aSRandall Stewart 			 * moved.
3282f8829a4aSRandall Stewart 			 */
3283b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3284f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3285f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3286f8829a4aSRandall Stewart 				    tp1->sent,
3287f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
328880fefe0aSRandall Stewart 			}
32895e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3290f8829a4aSRandall Stewart 				tp1->sent++;
32915e54f665SRandall Stewart 			}
32927c99d56fSMichael Tuexen 			if ((asoc->sctp_cmt_on_off > 0) &&
329320083c2eSMichael Tuexen 			    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3294f8829a4aSRandall Stewart 				/*
3295f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3296f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3297f8829a4aSRandall Stewart 				 * because it would have been set to the
3298f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3299f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3300f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3301c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3302c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3303c105859eSRandall Stewart 				 * indicates that two packets have been
3304c105859eSRandall Stewart 				 * received after this missing TSN.
33055e54f665SRandall Stewart 				 */
33065e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
330720b07a4dSMichael Tuexen 				    SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.TSN_seq)) {
3308b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3309f8829a4aSRandall Stewart 						sctp_log_fr(16 + num_dests_sacked,
3310f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3311f8829a4aSRandall Stewart 						    tp1->sent,
3312f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
331380fefe0aSRandall Stewart 					}
3314f8829a4aSRandall Stewart 					tp1->sent++;
3315f8829a4aSRandall Stewart 				}
3316f8829a4aSRandall Stewart 			}
331720083c2eSMichael Tuexen 		} else if ((tp1->rec.data.doing_fast_retransmit) &&
331820083c2eSMichael Tuexen 		    (asoc->sctp_cmt_on_off == 0)) {
3319f8829a4aSRandall Stewart 			/*
3320f8829a4aSRandall Stewart 			 * For those that have done a FR we must take
3321f8829a4aSRandall Stewart 			 * special consideration if we strike. I.e the
3322f8829a4aSRandall Stewart 			 * biggest_newly_acked must be higher than the
3323f8829a4aSRandall Stewart 			 * sending_seq at the time we did the FR.
3324f8829a4aSRandall Stewart 			 */
33255e54f665SRandall Stewart 			if (
3326f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3327f8829a4aSRandall Stewart 			/*
3328f8829a4aSRandall Stewart 			 * If FR's go to new networks, then we must only do
3329f8829a4aSRandall Stewart 			 * this for singly homed asoc's. However if the FR's
3330f8829a4aSRandall Stewart 			 * go to the same network (Armando's work) then its
3331f8829a4aSRandall Stewart 			 * ok to FR multiple times.
3332f8829a4aSRandall Stewart 			 */
33335e54f665SRandall Stewart 			    (asoc->numnets < 2)
3334f8829a4aSRandall Stewart #else
33355e54f665SRandall Stewart 			    (1)
3336f8829a4aSRandall Stewart #endif
33375e54f665SRandall Stewart 			    ) {
33385e54f665SRandall Stewart 
333920b07a4dSMichael Tuexen 				if (SCTP_TSN_GE(biggest_tsn_newly_acked,
3340f8829a4aSRandall Stewart 				    tp1->rec.data.fast_retran_tsn)) {
3341f8829a4aSRandall Stewart 					/*
3342f8829a4aSRandall Stewart 					 * Strike the TSN, since this ack is
3343f8829a4aSRandall Stewart 					 * beyond where things were when we
3344f8829a4aSRandall Stewart 					 * did a FR.
3345f8829a4aSRandall Stewart 					 */
3346b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3347f8829a4aSRandall Stewart 						sctp_log_fr(biggest_tsn_newly_acked,
3348f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3349f8829a4aSRandall Stewart 						    tp1->sent,
3350f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
335180fefe0aSRandall Stewart 					}
33525e54f665SRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3353f8829a4aSRandall Stewart 						tp1->sent++;
33545e54f665SRandall Stewart 					}
3355f8829a4aSRandall Stewart 					strike_flag = 1;
33567c99d56fSMichael Tuexen 					if ((asoc->sctp_cmt_on_off > 0) &&
335720083c2eSMichael Tuexen 					    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3358f8829a4aSRandall Stewart 						/*
3359f8829a4aSRandall Stewart 						 * CMT DAC algorithm: If
3360f8829a4aSRandall Stewart 						 * SACK flag is set to 0,
3361f8829a4aSRandall Stewart 						 * then lowest_newack test
3362f8829a4aSRandall Stewart 						 * will not pass because it
3363f8829a4aSRandall Stewart 						 * would have been set to
3364f8829a4aSRandall Stewart 						 * the cumack earlier. If
3365f8829a4aSRandall Stewart 						 * not already to be rtx'd,
3366f8829a4aSRandall Stewart 						 * If not a mixed sack and
3367f8829a4aSRandall Stewart 						 * if tp1 is not between two
3368f8829a4aSRandall Stewart 						 * sacked TSNs, then mark by
3369c105859eSRandall Stewart 						 * one more. NOTE that we
3370c105859eSRandall Stewart 						 * are marking by one
3371c105859eSRandall Stewart 						 * additional time since the
3372c105859eSRandall Stewart 						 * SACK DAC flag indicates
3373c105859eSRandall Stewart 						 * that two packets have
3374c105859eSRandall Stewart 						 * been received after this
3375c105859eSRandall Stewart 						 * missing TSN.
3376f8829a4aSRandall Stewart 						 */
33775e54f665SRandall Stewart 						if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
33785e54f665SRandall Stewart 						    (num_dests_sacked == 1) &&
337920b07a4dSMichael Tuexen 						    SCTP_TSN_GT(this_sack_lowest_newack,
338020b07a4dSMichael Tuexen 						    tp1->rec.data.TSN_seq)) {
3381b3f1ea41SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3382f8829a4aSRandall Stewart 								sctp_log_fr(32 + num_dests_sacked,
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 							}
3390f8829a4aSRandall Stewart 						}
3391f8829a4aSRandall Stewart 					}
3392f8829a4aSRandall Stewart 				}
3393f8829a4aSRandall Stewart 			}
3394f8829a4aSRandall Stewart 			/*
339542551e99SRandall Stewart 			 * JRI: TODO: remove code for HTNA algo. CMT's SFR
339642551e99SRandall Stewart 			 * algo covers HTNA.
3397f8829a4aSRandall Stewart 			 */
339820b07a4dSMichael Tuexen 		} else if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
339920b07a4dSMichael Tuexen 		    biggest_tsn_newly_acked)) {
3400f8829a4aSRandall Stewart 			/*
3401f8829a4aSRandall Stewart 			 * We don't strike these: This is the  HTNA
3402f8829a4aSRandall Stewart 			 * algorithm i.e. we don't strike If our TSN is
3403f8829a4aSRandall Stewart 			 * larger than the Highest TSN Newly Acked.
3404f8829a4aSRandall Stewart 			 */
3405f8829a4aSRandall Stewart 			;
3406f8829a4aSRandall Stewart 		} else {
3407f8829a4aSRandall Stewart 			/* Strike the TSN */
3408b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3409f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3410f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3411f8829a4aSRandall Stewart 				    tp1->sent,
3412f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
341380fefe0aSRandall Stewart 			}
34145e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3415f8829a4aSRandall Stewart 				tp1->sent++;
34165e54f665SRandall Stewart 			}
34177c99d56fSMichael Tuexen 			if ((asoc->sctp_cmt_on_off > 0) &&
341820083c2eSMichael Tuexen 			    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3419f8829a4aSRandall Stewart 				/*
3420f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3421f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3422f8829a4aSRandall Stewart 				 * because it would have been set to the
3423f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3424f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3425f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3426c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3427c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3428c105859eSRandall Stewart 				 * indicates that two packets have been
3429c105859eSRandall Stewart 				 * received after this missing TSN.
3430f8829a4aSRandall Stewart 				 */
34315e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
343220b07a4dSMichael Tuexen 				    SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.TSN_seq)) {
3433b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3434f8829a4aSRandall Stewart 						sctp_log_fr(48 + num_dests_sacked,
3435f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3436f8829a4aSRandall Stewart 						    tp1->sent,
3437f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
343880fefe0aSRandall Stewart 					}
3439f8829a4aSRandall Stewart 					tp1->sent++;
3440f8829a4aSRandall Stewart 				}
3441f8829a4aSRandall Stewart 			}
3442f8829a4aSRandall Stewart 		}
3443f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3444f8829a4aSRandall Stewart 			struct sctp_nets *alt;
3445f8829a4aSRandall Stewart 
3446544e35bdSRandall Stewart 			/* fix counts and things */
3447544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3448544e35bdSRandall Stewart 				sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND,
3449544e35bdSRandall Stewart 				    (tp1->whoTo ? (tp1->whoTo->flight_size) : 0),
3450544e35bdSRandall Stewart 				    tp1->book_size,
3451544e35bdSRandall Stewart 				    (uintptr_t) tp1->whoTo,
3452544e35bdSRandall Stewart 				    tp1->rec.data.TSN_seq);
3453544e35bdSRandall Stewart 			}
3454544e35bdSRandall Stewart 			if (tp1->whoTo) {
3455544e35bdSRandall Stewart 				tp1->whoTo->net_ack++;
3456544e35bdSRandall Stewart 				sctp_flight_size_decrease(tp1);
3457299108c5SRandall Stewart 				if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3458299108c5SRandall Stewart 					(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3459299108c5SRandall Stewart 					    tp1);
3460299108c5SRandall Stewart 				}
3461544e35bdSRandall Stewart 			}
3462544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
3463544e35bdSRandall Stewart 				sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
3464544e35bdSRandall Stewart 				    asoc->peers_rwnd, tp1->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3465544e35bdSRandall Stewart 			}
3466544e35bdSRandall Stewart 			/* add back to the rwnd */
3467544e35bdSRandall Stewart 			asoc->peers_rwnd += (tp1->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3468544e35bdSRandall Stewart 
3469544e35bdSRandall Stewart 			/* remove from the total flight */
3470544e35bdSRandall Stewart 			sctp_total_flight_decrease(stcb, tp1);
3471544e35bdSRandall Stewart 
3472475d0674SMichael Tuexen 			if ((stcb->asoc.peer_supports_prsctp) &&
3473475d0674SMichael Tuexen 			    (PR_SCTP_RTX_ENABLED(tp1->flags))) {
3474475d0674SMichael Tuexen 				/*
3475475d0674SMichael Tuexen 				 * Has it been retransmitted tv_sec times? -
3476475d0674SMichael Tuexen 				 * we store the retran count there.
3477475d0674SMichael Tuexen 				 */
3478475d0674SMichael Tuexen 				if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) {
3479475d0674SMichael Tuexen 					/* Yes, so drop it */
3480475d0674SMichael Tuexen 					if (tp1->data != NULL) {
3481475d0674SMichael Tuexen 						(void)sctp_release_pr_sctp_chunk(stcb, tp1,
3482475d0674SMichael Tuexen 						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3483475d0674SMichael Tuexen 						    SCTP_SO_NOT_LOCKED);
3484475d0674SMichael Tuexen 					}
3485475d0674SMichael Tuexen 					/* Make sure to flag we had a FR */
3486475d0674SMichael Tuexen 					tp1->whoTo->net_ack++;
3487475d0674SMichael Tuexen 					continue;
3488475d0674SMichael Tuexen 				}
3489475d0674SMichael Tuexen 			}
3490f8829a4aSRandall Stewart 			/* printf("OK, we are now ready to FR this guy\n"); */
3491b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3492f8829a4aSRandall Stewart 				sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count,
3493f8829a4aSRandall Stewart 				    0, SCTP_FR_MARKED);
349480fefe0aSRandall Stewart 			}
3495f8829a4aSRandall Stewart 			if (strike_flag) {
3496f8829a4aSRandall Stewart 				/* This is a subsequent FR */
3497f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_sendmultfastretrans);
3498f8829a4aSRandall Stewart 			}
34995e54f665SRandall Stewart 			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
35007c99d56fSMichael Tuexen 			if (asoc->sctp_cmt_on_off > 0) {
3501f8829a4aSRandall Stewart 				/*
3502f8829a4aSRandall Stewart 				 * CMT: Using RTX_SSTHRESH policy for CMT.
3503f8829a4aSRandall Stewart 				 * If CMT is being used, then pick dest with
3504f8829a4aSRandall Stewart 				 * largest ssthresh for any retransmission.
3505f8829a4aSRandall Stewart 				 */
3506f8829a4aSRandall Stewart 				tp1->no_fr_allowed = 1;
3507f8829a4aSRandall Stewart 				alt = tp1->whoTo;
35083c503c28SRandall Stewart 				/* sa_ignore NO_NULL_CHK */
350920083c2eSMichael Tuexen 				if (asoc->sctp_cmt_pf > 0) {
3510b54d3a6cSRandall Stewart 					/*
3511b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If CMT PF is on,
3512b54d3a6cSRandall Stewart 					 * use the PF version of
3513b54d3a6cSRandall Stewart 					 * find_alt_net()
3514b54d3a6cSRandall Stewart 					 */
3515b54d3a6cSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 2);
3516b54d3a6cSRandall Stewart 				} else {
3517b54d3a6cSRandall Stewart 					/*
3518b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If only CMT is on,
3519b54d3a6cSRandall Stewart 					 * use the CMT version of
3520b54d3a6cSRandall Stewart 					 * find_alt_net()
3521b54d3a6cSRandall Stewart 					 */
352252be287eSRandall Stewart 					/* sa_ignore NO_NULL_CHK */
3523f8829a4aSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 1);
3524b54d3a6cSRandall Stewart 				}
3525ad81507eSRandall Stewart 				if (alt == NULL) {
3526ad81507eSRandall Stewart 					alt = tp1->whoTo;
3527ad81507eSRandall Stewart 				}
3528f8829a4aSRandall Stewart 				/*
3529f8829a4aSRandall Stewart 				 * CUCv2: If a different dest is picked for
3530f8829a4aSRandall Stewart 				 * the retransmission, then new
3531f8829a4aSRandall Stewart 				 * (rtx-)pseudo_cumack needs to be tracked
3532f8829a4aSRandall Stewart 				 * for orig dest. Let CUCv2 track new (rtx-)
3533f8829a4aSRandall Stewart 				 * pseudo-cumack always.
3534f8829a4aSRandall Stewart 				 */
3535ad81507eSRandall Stewart 				if (tp1->whoTo) {
3536f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
3537f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3538ad81507eSRandall Stewart 				}
3539f8829a4aSRandall Stewart 			} else {/* CMT is OFF */
3540f8829a4aSRandall Stewart 
3541f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3542f8829a4aSRandall Stewart 				/* Can we find an alternate? */
3543f8829a4aSRandall Stewart 				alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0);
3544f8829a4aSRandall Stewart #else
3545f8829a4aSRandall Stewart 				/*
3546f8829a4aSRandall Stewart 				 * default behavior is to NOT retransmit
3547f8829a4aSRandall Stewart 				 * FR's to an alternate. Armando Caro's
3548f8829a4aSRandall Stewart 				 * paper details why.
3549f8829a4aSRandall Stewart 				 */
3550f8829a4aSRandall Stewart 				alt = tp1->whoTo;
3551f8829a4aSRandall Stewart #endif
3552f8829a4aSRandall Stewart 			}
3553f8829a4aSRandall Stewart 
3554f8829a4aSRandall Stewart 			tp1->rec.data.doing_fast_retransmit = 1;
3555f8829a4aSRandall Stewart 			tot_retrans++;
3556f8829a4aSRandall Stewart 			/* mark the sending seq for possible subsequent FR's */
3557f8829a4aSRandall Stewart 			/*
3558f8829a4aSRandall Stewart 			 * printf("Marking TSN for FR new value %x\n",
3559f8829a4aSRandall Stewart 			 * (uint32_t)tpi->rec.data.TSN_seq);
3560f8829a4aSRandall Stewart 			 */
3561f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->send_queue)) {
3562f8829a4aSRandall Stewart 				/*
3563f8829a4aSRandall Stewart 				 * If the queue of send is empty then its
3564f8829a4aSRandall Stewart 				 * the next sequence number that will be
3565f8829a4aSRandall Stewart 				 * assigned so we subtract one from this to
3566f8829a4aSRandall Stewart 				 * get the one we last sent.
3567f8829a4aSRandall Stewart 				 */
3568f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn = sending_seq;
3569f8829a4aSRandall Stewart 			} else {
3570f8829a4aSRandall Stewart 				/*
3571f8829a4aSRandall Stewart 				 * If there are chunks on the send queue
3572f8829a4aSRandall Stewart 				 * (unsent data that has made it from the
3573f8829a4aSRandall Stewart 				 * stream queues but not out the door, we
3574f8829a4aSRandall Stewart 				 * take the first one (which will have the
3575f8829a4aSRandall Stewart 				 * lowest TSN) and subtract one to get the
3576f8829a4aSRandall Stewart 				 * one we last sent.
3577f8829a4aSRandall Stewart 				 */
3578f8829a4aSRandall Stewart 				struct sctp_tmit_chunk *ttt;
3579f8829a4aSRandall Stewart 
3580f8829a4aSRandall Stewart 				ttt = TAILQ_FIRST(&asoc->send_queue);
3581f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn =
3582f8829a4aSRandall Stewart 				    ttt->rec.data.TSN_seq;
3583f8829a4aSRandall Stewart 			}
3584f8829a4aSRandall Stewart 
3585f8829a4aSRandall Stewart 			if (tp1->do_rtt) {
3586f8829a4aSRandall Stewart 				/*
3587f8829a4aSRandall Stewart 				 * this guy had a RTO calculation pending on
3588f8829a4aSRandall Stewart 				 * it, cancel it
3589f8829a4aSRandall Stewart 				 */
3590*60990c0cSMichael Tuexen 				if ((tp1->whoTo != NULL) &&
3591*60990c0cSMichael Tuexen 				    (tp1->whoTo->rto_needed == 0)) {
3592f79aab18SRandall Stewart 					tp1->whoTo->rto_needed = 1;
3593f79aab18SRandall Stewart 				}
3594f8829a4aSRandall Stewart 				tp1->do_rtt = 0;
3595f8829a4aSRandall Stewart 			}
3596f8829a4aSRandall Stewart 			if (alt != tp1->whoTo) {
3597f8829a4aSRandall Stewart 				/* yes, there is an alternate. */
3598f8829a4aSRandall Stewart 				sctp_free_remote_addr(tp1->whoTo);
35993c503c28SRandall Stewart 				/* sa_ignore FREED_MEMORY */
3600f8829a4aSRandall Stewart 				tp1->whoTo = alt;
3601f8829a4aSRandall Stewart 				atomic_add_int(&alt->ref_count, 1);
3602f8829a4aSRandall Stewart 			}
3603f8829a4aSRandall Stewart 		}
36044a9ef3f8SMichael Tuexen 	}
3605f8829a4aSRandall Stewart }
3606f8829a4aSRandall Stewart 
3607f8829a4aSRandall Stewart struct sctp_tmit_chunk *
3608f8829a4aSRandall Stewart sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
3609f8829a4aSRandall Stewart     struct sctp_association *asoc)
3610f8829a4aSRandall Stewart {
3611f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL;
3612f8829a4aSRandall Stewart 	struct timeval now;
3613f8829a4aSRandall Stewart 	int now_filled = 0;
3614f8829a4aSRandall Stewart 
3615f8829a4aSRandall Stewart 	if (asoc->peer_supports_prsctp == 0) {
3616f8829a4aSRandall Stewart 		return (NULL);
3617f8829a4aSRandall Stewart 	}
36184a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
3619f8829a4aSRandall Stewart 		if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
3620f8829a4aSRandall Stewart 		    tp1->sent != SCTP_DATAGRAM_RESEND) {
3621f8829a4aSRandall Stewart 			/* no chance to advance, out of here */
3622f8829a4aSRandall Stewart 			break;
3623f8829a4aSRandall Stewart 		}
36240c0982b8SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
36250c0982b8SRandall Stewart 			if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
36260c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
36270c0982b8SRandall Stewart 				    asoc->advanced_peer_ack_point,
36280c0982b8SRandall Stewart 				    tp1->rec.data.TSN_seq, 0, 0);
36290c0982b8SRandall Stewart 			}
36300c0982b8SRandall Stewart 		}
3631f8829a4aSRandall Stewart 		if (!PR_SCTP_ENABLED(tp1->flags)) {
3632f8829a4aSRandall Stewart 			/*
3633f8829a4aSRandall Stewart 			 * We can't fwd-tsn past any that are reliable aka
3634f8829a4aSRandall Stewart 			 * retransmitted until the asoc fails.
3635f8829a4aSRandall Stewart 			 */
3636f8829a4aSRandall Stewart 			break;
3637f8829a4aSRandall Stewart 		}
3638f8829a4aSRandall Stewart 		if (!now_filled) {
36396e55db54SRandall Stewart 			(void)SCTP_GETTIME_TIMEVAL(&now);
3640f8829a4aSRandall Stewart 			now_filled = 1;
3641f8829a4aSRandall Stewart 		}
3642f8829a4aSRandall Stewart 		/*
3643f8829a4aSRandall Stewart 		 * now we got a chunk which is marked for another
3644f8829a4aSRandall Stewart 		 * retransmission to a PR-stream but has run out its chances
3645f8829a4aSRandall Stewart 		 * already maybe OR has been marked to skip now. Can we skip
3646f8829a4aSRandall Stewart 		 * it if its a resend?
3647f8829a4aSRandall Stewart 		 */
3648f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND &&
3649f8829a4aSRandall Stewart 		    (PR_SCTP_TTL_ENABLED(tp1->flags))) {
3650f8829a4aSRandall Stewart 			/*
3651f8829a4aSRandall Stewart 			 * Now is this one marked for resend and its time is
3652f8829a4aSRandall Stewart 			 * now up?
3653f8829a4aSRandall Stewart 			 */
3654f8829a4aSRandall Stewart 			if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3655f8829a4aSRandall Stewart 				/* Yes so drop it */
3656f8829a4aSRandall Stewart 				if (tp1->data) {
3657ad81507eSRandall Stewart 					(void)sctp_release_pr_sctp_chunk(stcb, tp1,
3658f8829a4aSRandall Stewart 					    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
36590c0982b8SRandall Stewart 					    SCTP_SO_NOT_LOCKED);
3660f8829a4aSRandall Stewart 				}
3661f8829a4aSRandall Stewart 			} else {
3662f8829a4aSRandall Stewart 				/*
3663f8829a4aSRandall Stewart 				 * No, we are done when hit one for resend
3664f8829a4aSRandall Stewart 				 * whos time as not expired.
3665f8829a4aSRandall Stewart 				 */
3666f8829a4aSRandall Stewart 				break;
3667f8829a4aSRandall Stewart 			}
3668f8829a4aSRandall Stewart 		}
3669f8829a4aSRandall Stewart 		/*
3670f8829a4aSRandall Stewart 		 * Ok now if this chunk is marked to drop it we can clean up
3671f8829a4aSRandall Stewart 		 * the chunk, advance our peer ack point and we can check
3672f8829a4aSRandall Stewart 		 * the next chunk.
3673f8829a4aSRandall Stewart 		 */
367444fbe462SRandall Stewart 		if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
3675f8829a4aSRandall Stewart 			/* advance PeerAckPoint goes forward */
367620b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, asoc->advanced_peer_ack_point)) {
3677f8829a4aSRandall Stewart 				asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq;
3678f8829a4aSRandall Stewart 				a_adv = tp1;
36790c0982b8SRandall Stewart 			} else if (tp1->rec.data.TSN_seq == asoc->advanced_peer_ack_point) {
36800c0982b8SRandall Stewart 				/* No update but we do save the chk */
36810c0982b8SRandall Stewart 				a_adv = tp1;
36820c0982b8SRandall Stewart 			}
3683f8829a4aSRandall Stewart 		} else {
3684f8829a4aSRandall Stewart 			/*
3685f8829a4aSRandall Stewart 			 * If it is still in RESEND we can advance no
3686f8829a4aSRandall Stewart 			 * further
3687f8829a4aSRandall Stewart 			 */
3688f8829a4aSRandall Stewart 			break;
3689f8829a4aSRandall Stewart 		}
3690f8829a4aSRandall Stewart 	}
3691f8829a4aSRandall Stewart 	return (a_adv);
3692f8829a4aSRandall Stewart }
3693f8829a4aSRandall Stewart 
36940c0982b8SRandall Stewart static int
3695c105859eSRandall Stewart sctp_fs_audit(struct sctp_association *asoc)
3696bff64a4dSRandall Stewart {
3697bff64a4dSRandall Stewart 	struct sctp_tmit_chunk *chk;
3698bff64a4dSRandall Stewart 	int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0;
36990c0982b8SRandall Stewart 	int entry_flight, entry_cnt, ret;
37000c0982b8SRandall Stewart 
37010c0982b8SRandall Stewart 	entry_flight = asoc->total_flight;
37020c0982b8SRandall Stewart 	entry_cnt = asoc->total_flight_count;
37030c0982b8SRandall Stewart 	ret = 0;
37040c0982b8SRandall Stewart 
37050c0982b8SRandall Stewart 	if (asoc->pr_sctp_cnt >= asoc->sent_queue_cnt)
37060c0982b8SRandall Stewart 		return (0);
3707bff64a4dSRandall Stewart 
3708bff64a4dSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
3709bff64a4dSRandall Stewart 		if (chk->sent < SCTP_DATAGRAM_RESEND) {
37100c0982b8SRandall Stewart 			printf("Chk TSN:%u size:%d inflight cnt:%d\n",
37110c0982b8SRandall Stewart 			    chk->rec.data.TSN_seq,
37120c0982b8SRandall Stewart 			    chk->send_size,
37130c0982b8SRandall Stewart 			    chk->snd_count
37140c0982b8SRandall Stewart 			    );
3715bff64a4dSRandall Stewart 			inflight++;
3716bff64a4dSRandall Stewart 		} else if (chk->sent == SCTP_DATAGRAM_RESEND) {
3717bff64a4dSRandall Stewart 			resend++;
3718bff64a4dSRandall Stewart 		} else if (chk->sent < SCTP_DATAGRAM_ACKED) {
3719bff64a4dSRandall Stewart 			inbetween++;
3720bff64a4dSRandall Stewart 		} else if (chk->sent > SCTP_DATAGRAM_ACKED) {
3721bff64a4dSRandall Stewart 			above++;
3722bff64a4dSRandall Stewart 		} else {
3723bff64a4dSRandall Stewart 			acked++;
3724bff64a4dSRandall Stewart 		}
3725bff64a4dSRandall Stewart 	}
3726f1f73e57SRandall Stewart 
3727c105859eSRandall Stewart 	if ((inflight > 0) || (inbetween > 0)) {
3728f1f73e57SRandall Stewart #ifdef INVARIANTS
3729c105859eSRandall Stewart 		panic("Flight size-express incorrect? \n");
3730f1f73e57SRandall Stewart #else
37310c0982b8SRandall Stewart 		printf("asoc->total_flight:%d cnt:%d\n",
37320c0982b8SRandall Stewart 		    entry_flight, entry_cnt);
37330c0982b8SRandall Stewart 
37340c0982b8SRandall Stewart 		SCTP_PRINTF("Flight size-express incorrect F:%d I:%d R:%d Ab:%d ACK:%d\n",
37350c0982b8SRandall Stewart 		    inflight, inbetween, resend, above, acked);
37360c0982b8SRandall Stewart 		ret = 1;
3737f1f73e57SRandall Stewart #endif
3738bff64a4dSRandall Stewart 	}
37390c0982b8SRandall Stewart 	return (ret);
3740c105859eSRandall Stewart }
3741c105859eSRandall Stewart 
3742c105859eSRandall Stewart 
3743c105859eSRandall Stewart static void
3744c105859eSRandall Stewart sctp_window_probe_recovery(struct sctp_tcb *stcb,
3745c105859eSRandall Stewart     struct sctp_association *asoc,
3746c105859eSRandall Stewart     struct sctp_tmit_chunk *tp1)
3747c105859eSRandall Stewart {
3748dfb11ef8SRandall Stewart 	tp1->window_probe = 0;
37495171328bSRandall Stewart 	if ((tp1->sent >= SCTP_DATAGRAM_ACKED) || (tp1->data == NULL)) {
3750dfb11ef8SRandall Stewart 		/* TSN's skipped we do NOT move back. */
3751dfb11ef8SRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DWN_WP_FWD,
3752dfb11ef8SRandall Stewart 		    tp1->whoTo->flight_size,
3753dfb11ef8SRandall Stewart 		    tp1->book_size,
3754dfb11ef8SRandall Stewart 		    (uintptr_t) tp1->whoTo,
3755dfb11ef8SRandall Stewart 		    tp1->rec.data.TSN_seq);
3756dfb11ef8SRandall Stewart 		return;
3757dfb11ef8SRandall Stewart 	}
37585171328bSRandall Stewart 	/* First setup this by shrinking flight */
3759299108c5SRandall Stewart 	if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3760299108c5SRandall Stewart 		(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3761299108c5SRandall Stewart 		    tp1);
3762299108c5SRandall Stewart 	}
37635171328bSRandall Stewart 	sctp_flight_size_decrease(tp1);
37645171328bSRandall Stewart 	sctp_total_flight_decrease(stcb, tp1);
37655171328bSRandall Stewart 	/* Now mark for resend */
37665171328bSRandall Stewart 	tp1->sent = SCTP_DATAGRAM_RESEND;
3767791437b5SRandall Stewart 	sctp_ucount_incr(asoc->sent_queue_retran_cnt);
3768791437b5SRandall Stewart 
3769b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3770c105859eSRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP,
3771c105859eSRandall Stewart 		    tp1->whoTo->flight_size,
3772c105859eSRandall Stewart 		    tp1->book_size,
3773c105859eSRandall Stewart 		    (uintptr_t) tp1->whoTo,
3774c105859eSRandall Stewart 		    tp1->rec.data.TSN_seq);
377580fefe0aSRandall Stewart 	}
3776c105859eSRandall Stewart }
3777c105859eSRandall Stewart 
3778f8829a4aSRandall Stewart void
3779f8829a4aSRandall Stewart sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
3780899288aeSRandall Stewart     uint32_t rwnd, int *abort_now, int ecne_seen)
3781f8829a4aSRandall Stewart {
3782f8829a4aSRandall Stewart 	struct sctp_nets *net;
3783f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3784f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
37855e54f665SRandall Stewart 	uint32_t old_rwnd;
37865e54f665SRandall Stewart 	int win_probe_recovery = 0;
3787c105859eSRandall Stewart 	int win_probe_recovered = 0;
3788d06c82f1SRandall Stewart 	int j, done_once = 0;
3789f79aab18SRandall Stewart 	int rto_ok = 1;
3790f8829a4aSRandall Stewart 
3791b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
3792d06c82f1SRandall Stewart 		sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
3793d06c82f1SRandall Stewart 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
379480fefe0aSRandall Stewart 	}
3795f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
379618e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
379718e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
379818e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
379918e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
380018e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
380118e198d3SRandall Stewart 	}
380218e198d3SRandall Stewart #endif
3803f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
3804d06c82f1SRandall Stewart 	old_rwnd = asoc->peers_rwnd;
380520b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->last_acked_seq, cumack)) {
38065e54f665SRandall Stewart 		/* old ack */
38075e54f665SRandall Stewart 		return;
3808d06c82f1SRandall Stewart 	} else if (asoc->last_acked_seq == cumack) {
3809d06c82f1SRandall Stewart 		/* Window update sack */
3810d06c82f1SRandall Stewart 		asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
381144fbe462SRandall Stewart 		    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
3812d06c82f1SRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
3813d06c82f1SRandall Stewart 			/* SWS sender side engages */
3814d06c82f1SRandall Stewart 			asoc->peers_rwnd = 0;
3815d06c82f1SRandall Stewart 		}
3816d06c82f1SRandall Stewart 		if (asoc->peers_rwnd > old_rwnd) {
3817d06c82f1SRandall Stewart 			goto again;
3818d06c82f1SRandall Stewart 		}
3819d06c82f1SRandall Stewart 		return;
38205e54f665SRandall Stewart 	}
3821f8829a4aSRandall Stewart 	/* First setup for CC stuff */
3822f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3823a21779f0SRandall Stewart 		if (SCTP_TSN_GT(cumack, net->cwr_window_tsn)) {
3824a21779f0SRandall Stewart 			/* Drag along the window_tsn for cwr's */
3825a21779f0SRandall Stewart 			net->cwr_window_tsn = cumack;
3826a21779f0SRandall Stewart 		}
3827f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
3828f8829a4aSRandall Stewart 		net->net_ack = 0;
3829f8829a4aSRandall Stewart 		net->net_ack2 = 0;
3830132dea7dSRandall Stewart 
3831132dea7dSRandall Stewart 		/*
3832132dea7dSRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
3833132dea7dSRandall Stewart 		 * SACK processing
3834132dea7dSRandall Stewart 		 */
3835132dea7dSRandall Stewart 		net->new_pseudo_cumack = 0;
3836132dea7dSRandall Stewart 		net->will_exit_fast_recovery = 0;
3837299108c5SRandall Stewart 		if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) {
3838299108c5SRandall Stewart 			(*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) (stcb, net);
3839299108c5SRandall Stewart 		}
3840f8829a4aSRandall Stewart 	}
3841b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
3842139bc87fSRandall Stewart 		uint32_t send_s;
3843139bc87fSRandall Stewart 
3844c105859eSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3845c105859eSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
3846c105859eSRandall Stewart 			    sctpchunk_listhead);
3847c105859eSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
3848139bc87fSRandall Stewart 		} else {
3849c105859eSRandall Stewart 			send_s = asoc->sending_seq;
3850139bc87fSRandall Stewart 		}
385120b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(cumack, send_s)) {
3852c105859eSRandall Stewart #ifndef INVARIANTS
3853139bc87fSRandall Stewart 			struct mbuf *oper;
3854139bc87fSRandall Stewart 
3855c105859eSRandall Stewart #endif
3856c105859eSRandall Stewart #ifdef INVARIANTS
3857c105859eSRandall Stewart 			panic("Impossible sack 1");
3858c105859eSRandall Stewart #else
3859b5c16493SMichael Tuexen 
3860139bc87fSRandall Stewart 			*abort_now = 1;
3861139bc87fSRandall Stewart 			/* XXX */
3862139bc87fSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3863139bc87fSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
3864139bc87fSRandall Stewart 			if (oper) {
3865139bc87fSRandall Stewart 				struct sctp_paramhdr *ph;
3866139bc87fSRandall Stewart 				uint32_t *ippp;
3867139bc87fSRandall Stewart 
3868139bc87fSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
3869139bc87fSRandall Stewart 				    sizeof(uint32_t);
3870139bc87fSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
3871139bc87fSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
3872139bc87fSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
3873139bc87fSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
3874139bc87fSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
3875139bc87fSRandall Stewart 			}
3876139bc87fSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
3877ceaad40aSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
3878139bc87fSRandall Stewart 			return;
3879139bc87fSRandall Stewart #endif
3880139bc87fSRandall Stewart 		}
3881139bc87fSRandall Stewart 	}
3882f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = cumack;
3883b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
3884c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
3885c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
3886c4739e2fSRandall Stewart 		    0,
3887c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
3888c4739e2fSRandall Stewart 		    __LINE__);
3889c4739e2fSRandall Stewart 	}
3890f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
389120b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cumack, asoc->last_acked_seq)) {
3892f8829a4aSRandall Stewart 		/* process the new consecutive TSN first */
38934a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
389420b07a4dSMichael Tuexen 			if (SCTP_TSN_GE(cumack, tp1->rec.data.TSN_seq)) {
389518e198d3SRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
389618e198d3SRandall Stewart 					printf("Warning, an unsent is now acked?\n");
389718e198d3SRandall Stewart 				}
3898f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
3899f8829a4aSRandall Stewart 					/*
390018e198d3SRandall Stewart 					 * If it is less than ACKED, it is
390118e198d3SRandall Stewart 					 * now no-longer in flight. Higher
390218e198d3SRandall Stewart 					 * values may occur during marking
3903f8829a4aSRandall Stewart 					 */
3904c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3905b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3906c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
3907a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
3908a5d547adSRandall Stewart 							    tp1->book_size,
3909c105859eSRandall Stewart 							    (uintptr_t) tp1->whoTo,
3910a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
391180fefe0aSRandall Stewart 						}
3912c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
3913299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3914299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3915299108c5SRandall Stewart 							    tp1);
3916299108c5SRandall Stewart 						}
391704ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
3918c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
3919f8829a4aSRandall Stewart 					}
3920f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
3921f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
3922f8829a4aSRandall Stewart 						/*
392318e198d3SRandall Stewart 						 * True non-retransmited
3924f8829a4aSRandall Stewart 						 * chunk
3925f8829a4aSRandall Stewart 						 */
3926f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
3927f8829a4aSRandall Stewart 						    tp1->send_size;
3928f8829a4aSRandall Stewart 
3929f8829a4aSRandall Stewart 						/* update RTO too? */
393062c1ff9cSRandall Stewart 						if (tp1->do_rtt) {
3931f79aab18SRandall Stewart 							if (rto_ok) {
3932f8829a4aSRandall Stewart 								tp1->whoTo->RTO =
393304ee05e8SRandall Stewart 								/*
393404ee05e8SRandall Stewart 								 * sa_ignore
3935f79aab18SRandall Stewart 								 * NO_NULL_CH
3936f79aab18SRandall Stewart 								 * K
393704ee05e8SRandall Stewart 								 */
3938f8829a4aSRandall Stewart 								    sctp_calculate_rto(stcb,
3939f8829a4aSRandall Stewart 								    asoc, tp1->whoTo,
394018e198d3SRandall Stewart 								    &tp1->sent_rcv_time,
3941899288aeSRandall Stewart 								    sctp_align_safe_nocopy,
3942f79aab18SRandall Stewart 								    SCTP_RTT_FROM_DATA);
3943f79aab18SRandall Stewart 								rto_ok = 0;
3944f79aab18SRandall Stewart 							}
3945f79aab18SRandall Stewart 							if (tp1->whoTo->rto_needed == 0) {
3946f79aab18SRandall Stewart 								tp1->whoTo->rto_needed = 1;
3947f79aab18SRandall Stewart 							}
3948f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
3949f8829a4aSRandall Stewart 						}
3950f8829a4aSRandall Stewart 					}
3951132dea7dSRandall Stewart 					/*
395218e198d3SRandall Stewart 					 * CMT: CUCv2 algorithm. From the
395318e198d3SRandall Stewart 					 * cumack'd TSNs, for each TSN being
395418e198d3SRandall Stewart 					 * acked for the first time, set the
395518e198d3SRandall Stewart 					 * following variables for the
395618e198d3SRandall Stewart 					 * corresp destination.
395718e198d3SRandall Stewart 					 * new_pseudo_cumack will trigger a
395818e198d3SRandall Stewart 					 * cwnd update.
395918e198d3SRandall Stewart 					 * find_(rtx_)pseudo_cumack will
396018e198d3SRandall Stewart 					 * trigger search for the next
396118e198d3SRandall Stewart 					 * expected (rtx-)pseudo-cumack.
3962132dea7dSRandall Stewart 					 */
3963132dea7dSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
3964132dea7dSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
3965132dea7dSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3966132dea7dSRandall Stewart 
3967b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
396804ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
3969f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
397080fefe0aSRandall Stewart 					}
3971f8829a4aSRandall Stewart 				}
3972f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3973f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
3974f8829a4aSRandall Stewart 				}
397542551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
397642551e99SRandall Stewart 					/* deflate the cwnd */
397742551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
397842551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
397942551e99SRandall Stewart 				}
3980f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
3981f8829a4aSRandall Stewart 				TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
3982f8829a4aSRandall Stewart 				if (tp1->data) {
398304ee05e8SRandall Stewart 					/* sa_ignore NO_NULL_CHK */
3984f8829a4aSRandall Stewart 					sctp_free_bufspace(stcb, asoc, tp1, 1);
3985f8829a4aSRandall Stewart 					sctp_m_freem(tp1->data);
39864a9ef3f8SMichael Tuexen 					tp1->data = NULL;
3987f8829a4aSRandall Stewart 				}
3988b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3989f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
3990f8829a4aSRandall Stewart 					    cumack,
3991f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
3992f8829a4aSRandall Stewart 					    0,
3993f8829a4aSRandall Stewart 					    0,
3994f8829a4aSRandall Stewart 					    SCTP_LOG_FREE_SENT);
399580fefe0aSRandall Stewart 				}
3996f8829a4aSRandall Stewart 				asoc->sent_queue_cnt--;
3997689e6a5fSMichael Tuexen 				sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED);
399818e198d3SRandall Stewart 			} else {
399918e198d3SRandall Stewart 				break;
4000f8829a4aSRandall Stewart 			}
40015e54f665SRandall Stewart 		}
400218e198d3SRandall Stewart 
400318e198d3SRandall Stewart 	}
400404ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
4005f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
4006ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4007ceaad40aSRandall Stewart 		struct socket *so;
4008ceaad40aSRandall Stewart 
4009ceaad40aSRandall Stewart #endif
4010f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4011b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
401204ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
40137215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, 1, SCTP_WAKESND_FROM_SACK);
401480fefe0aSRandall Stewart 		}
4015ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4016ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
4017ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
4018ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
4019ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
4020ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4021ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4022ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4023ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
4024ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
4025ceaad40aSRandall Stewart 			return;
4026ceaad40aSRandall Stewart 		}
4027ceaad40aSRandall Stewart #endif
4028f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4029ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4030ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4031ceaad40aSRandall Stewart #endif
4032f8829a4aSRandall Stewart 	} else {
4033b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
40347215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, 1, SCTP_NOWAKE_FROM_SACK);
403580fefe0aSRandall Stewart 		}
4036f8829a4aSRandall Stewart 	}
4037f8829a4aSRandall Stewart 
4038b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
4039ca85e948SMichael Tuexen 	if ((asoc->last_acked_seq != cumack) && (ecne_seen == 0)) {
4040ca85e948SMichael Tuexen 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4041ca85e948SMichael Tuexen 			if (net->net_ack2 > 0) {
4042ca85e948SMichael Tuexen 				/*
4043ca85e948SMichael Tuexen 				 * Karn's rule applies to clearing error
4044ca85e948SMichael Tuexen 				 * count, this is optional.
4045ca85e948SMichael Tuexen 				 */
4046ca85e948SMichael Tuexen 				net->error_count = 0;
4047ca85e948SMichael Tuexen 				if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
4048ca85e948SMichael Tuexen 					/* addr came good */
4049ca85e948SMichael Tuexen 					net->dest_state |= SCTP_ADDR_REACHABLE;
4050ca85e948SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
4051ca85e948SMichael Tuexen 					    SCTP_RECEIVED_SACK, (void *)net, SCTP_SO_NOT_LOCKED);
4052ca85e948SMichael Tuexen 				}
4053ca85e948SMichael Tuexen 				if (net == stcb->asoc.primary_destination) {
4054ca85e948SMichael Tuexen 					if (stcb->asoc.alternate) {
4055ca85e948SMichael Tuexen 						/*
4056ca85e948SMichael Tuexen 						 * release the alternate,
4057ca85e948SMichael Tuexen 						 * primary is good
4058ca85e948SMichael Tuexen 						 */
4059ca85e948SMichael Tuexen 						sctp_free_remote_addr(stcb->asoc.alternate);
4060ca85e948SMichael Tuexen 						stcb->asoc.alternate = NULL;
4061ca85e948SMichael Tuexen 					}
4062ca85e948SMichael Tuexen 				}
4063ca85e948SMichael Tuexen 				if (net->dest_state & SCTP_ADDR_PF) {
4064ca85e948SMichael Tuexen 					net->dest_state &= ~SCTP_ADDR_PF;
4065ca85e948SMichael Tuexen 					sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
4066ca85e948SMichael Tuexen 					sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
4067ca85e948SMichael Tuexen 					asoc->cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
4068ca85e948SMichael Tuexen 					/* Done with this net */
4069ca85e948SMichael Tuexen 					net->net_ack = 0;
4070ca85e948SMichael Tuexen 				}
4071ca85e948SMichael Tuexen 				/* restore any doubled timers */
4072ca85e948SMichael Tuexen 				net->RTO = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
4073ca85e948SMichael Tuexen 				if (net->RTO < stcb->asoc.minrto) {
4074ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.minrto;
4075ca85e948SMichael Tuexen 				}
4076ca85e948SMichael Tuexen 				if (net->RTO > stcb->asoc.maxrto) {
4077ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.maxrto;
4078ca85e948SMichael Tuexen 				}
4079ca85e948SMichael Tuexen 			}
4080ca85e948SMichael Tuexen 		}
4081b54d3a6cSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
4082ca85e948SMichael Tuexen 	}
4083f8829a4aSRandall Stewart 	asoc->last_acked_seq = cumack;
40845e54f665SRandall Stewart 
4085f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
4086f8829a4aSRandall Stewart 		/* nothing left in-flight */
4087f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4088f8829a4aSRandall Stewart 			net->flight_size = 0;
4089f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4090f8829a4aSRandall Stewart 		}
4091f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4092f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4093f8829a4aSRandall Stewart 	}
4094f8829a4aSRandall Stewart 	/* RWND update */
4095f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
409644fbe462SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4097f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4098f8829a4aSRandall Stewart 		/* SWS sender side engages */
4099f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
4100f8829a4aSRandall Stewart 	}
41015e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
41025e54f665SRandall Stewart 		win_probe_recovery = 1;
41035e54f665SRandall Stewart 	}
4104f8829a4aSRandall Stewart 	/* Now assure a timer where data is queued at */
4105a5d547adSRandall Stewart again:
4106a5d547adSRandall Stewart 	j = 0;
4107f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
41085171328bSRandall Stewart 		int to_ticks;
41095171328bSRandall Stewart 
41105e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
4111c105859eSRandall Stewart 			win_probe_recovered = 1;
41125e54f665SRandall Stewart 			/*
41135e54f665SRandall Stewart 			 * Find first chunk that was used with window probe
41145e54f665SRandall Stewart 			 * and clear the sent
41155e54f665SRandall Stewart 			 */
41163c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
41175e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
41185e54f665SRandall Stewart 				if (tp1->window_probe) {
4119cd554309SMichael Tuexen 					/* move back to data send queue */
41207215cc1bSMichael Tuexen 					sctp_window_probe_recovery(stcb, asoc, tp1);
41215e54f665SRandall Stewart 					break;
41225e54f665SRandall Stewart 				}
41235e54f665SRandall Stewart 			}
41245e54f665SRandall Stewart 		}
4125f8829a4aSRandall Stewart 		if (net->RTO == 0) {
4126f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
4127f8829a4aSRandall Stewart 		} else {
4128f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
4129f8829a4aSRandall Stewart 		}
41305171328bSRandall Stewart 		if (net->flight_size) {
4131a5d547adSRandall Stewart 			j++;
4132ad81507eSRandall Stewart 			(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
4133f8829a4aSRandall Stewart 			    sctp_timeout_handler, &net->rxt_timer);
41345171328bSRandall Stewart 			if (net->window_probe) {
41355171328bSRandall Stewart 				net->window_probe = 0;
41365171328bSRandall Stewart 			}
4137f8829a4aSRandall Stewart 		} else {
41385171328bSRandall Stewart 			if (net->window_probe) {
41395171328bSRandall Stewart 				/*
41405171328bSRandall Stewart 				 * In window probes we must assure a timer
41415171328bSRandall Stewart 				 * is still running there
41425171328bSRandall Stewart 				 */
41435171328bSRandall Stewart 				net->window_probe = 0;
41445171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
41455171328bSRandall Stewart 					SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
41465171328bSRandall Stewart 					    sctp_timeout_handler, &net->rxt_timer);
41475171328bSRandall Stewart 				}
41485171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4149f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4150a5d547adSRandall Stewart 				    stcb, net,
4151a5d547adSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
4152f8829a4aSRandall Stewart 			}
4153f8829a4aSRandall Stewart 		}
4154f8829a4aSRandall Stewart 	}
4155bff64a4dSRandall Stewart 	if ((j == 0) &&
4156bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
4157bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
4158c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
4159bff64a4dSRandall Stewart 	    (done_once == 0)) {
41600c0982b8SRandall Stewart 		/*
41610c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
41620c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
41630c0982b8SRandall Stewart 		 */
41640c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
4165a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4166a5d547adSRandall Stewart 				net->flight_size = 0;
4167a5d547adSRandall Stewart 			}
4168a5d547adSRandall Stewart 			asoc->total_flight = 0;
4169a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
4170a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4171a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4172a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4173c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
4174c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
4175a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4176791437b5SRandall Stewart 					sctp_ucount_incr(asoc->sent_queue_retran_cnt);
4177a5d547adSRandall Stewart 				}
4178a5d547adSRandall Stewart 			}
41790c0982b8SRandall Stewart 		}
4180bff64a4dSRandall Stewart 		done_once = 1;
4181a5d547adSRandall Stewart 		goto again;
4182a5d547adSRandall Stewart 	}
4183f8829a4aSRandall Stewart 	/**********************************/
4184f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
4185f8829a4aSRandall Stewart 	/**********************************/
4186f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4187f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
4188f8829a4aSRandall Stewart 		/* clean up */
4189f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
4190f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4191f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4192f8829a4aSRandall Stewart 		    (asoc->locked_on_sending)
4193f8829a4aSRandall Stewart 		    ) {
4194f8829a4aSRandall Stewart 			struct sctp_stream_queue_pending *sp;
4195f8829a4aSRandall Stewart 
4196f8829a4aSRandall Stewart 			/*
4197f8829a4aSRandall Stewart 			 * I may be in a state where we got all across.. but
4198f8829a4aSRandall Stewart 			 * cannot write more due to a shutdown... we abort
4199f8829a4aSRandall Stewart 			 * since the user did not indicate EOR in this case.
4200f8829a4aSRandall Stewart 			 * The sp will be cleaned during free of the asoc.
4201f8829a4aSRandall Stewart 			 */
4202f8829a4aSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4203f8829a4aSRandall Stewart 			    sctp_streamhead);
42042afb3e84SRandall Stewart 			if ((sp) && (sp->length == 0)) {
42052afb3e84SRandall Stewart 				/* Let cleanup code purge it */
42062afb3e84SRandall Stewart 				if (sp->msg_is_complete) {
42072afb3e84SRandall Stewart 					asoc->stream_queue_cnt--;
42082afb3e84SRandall Stewart 				} else {
4209f8829a4aSRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4210f8829a4aSRandall Stewart 					asoc->locked_on_sending = NULL;
4211f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
4212f8829a4aSRandall Stewart 				}
4213f8829a4aSRandall Stewart 			}
42142afb3e84SRandall Stewart 		}
4215f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4216f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4217f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4218f8829a4aSRandall Stewart 				/* Need to abort here */
4219f8829a4aSRandall Stewart 				struct mbuf *oper;
4220f8829a4aSRandall Stewart 
4221f8829a4aSRandall Stewart 		abort_out_now:
4222f8829a4aSRandall Stewart 				*abort_now = 1;
4223f8829a4aSRandall Stewart 				/* XXX */
4224f8829a4aSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4225f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
4226f8829a4aSRandall Stewart 				if (oper) {
4227f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
4228f8829a4aSRandall Stewart 					uint32_t *ippp;
4229f8829a4aSRandall Stewart 
4230139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4231f8829a4aSRandall Stewart 					    sizeof(uint32_t);
4232f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
4233f8829a4aSRandall Stewart 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4234139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
4235f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
4236a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_24);
4237f8829a4aSRandall Stewart 				}
4238a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
4239ceaad40aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
4240f8829a4aSRandall Stewart 			} else {
4241ca85e948SMichael Tuexen 				struct sctp_nets *netp;
4242ca85e948SMichael Tuexen 
4243f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4244f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4245f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4246f42a358aSRandall Stewart 				}
4247c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4248b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4249f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
4250ca85e948SMichael Tuexen 				if (asoc->alternate) {
4251ca85e948SMichael Tuexen 					netp = asoc->alternate;
4252ca85e948SMichael Tuexen 				} else {
4253ca85e948SMichael Tuexen 					netp = asoc->primary_destination;
4254ca85e948SMichael Tuexen 				}
4255ca85e948SMichael Tuexen 				sctp_send_shutdown(stcb, netp);
4256f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4257ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4258f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4259ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4260f8829a4aSRandall Stewart 			}
4261f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4262f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4263ca85e948SMichael Tuexen 			struct sctp_nets *netp;
4264ca85e948SMichael Tuexen 
4265ca85e948SMichael Tuexen 			if (asoc->alternate) {
4266ca85e948SMichael Tuexen 				netp = asoc->alternate;
4267ca85e948SMichael Tuexen 			} else {
4268ca85e948SMichael Tuexen 				netp = asoc->primary_destination;
4269ca85e948SMichael Tuexen 			}
4270f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4271f8829a4aSRandall Stewart 				goto abort_out_now;
4272f8829a4aSRandall Stewart 			}
4273f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4274c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4275b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4276ca85e948SMichael Tuexen 			sctp_send_shutdown_ack(stcb, netp);
427712af6654SMichael Tuexen 			sctp_stop_timers_for_shutdown(stcb);
4278f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4279ca85e948SMichael Tuexen 			    stcb->sctp_ep, stcb, netp);
4280f8829a4aSRandall Stewart 		}
4281f8829a4aSRandall Stewart 	}
4282dfb11ef8SRandall Stewart 	/*********************************************/
4283dfb11ef8SRandall Stewart 	/* Here we perform PR-SCTP procedures        */
4284dfb11ef8SRandall Stewart 	/* (section 4.2)                             */
4285dfb11ef8SRandall Stewart 	/*********************************************/
4286dfb11ef8SRandall Stewart 	/* C1. update advancedPeerAckPoint */
428720b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cumack, asoc->advanced_peer_ack_point)) {
4288dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cumack;
4289dfb11ef8SRandall Stewart 	}
4290830d754dSRandall Stewart 	/* PR-Sctp issues need to be addressed too */
4291830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
4292830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
4293830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
4294830d754dSRandall Stewart 
4295830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
4296830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
4297830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
429820b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cumack)) {
4299830d754dSRandall Stewart 			/*
4300493d8e5aSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing.
4301830d754dSRandall Stewart 			 */
430220b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) {
4303830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
43040c0982b8SRandall Stewart 			} else if (lchk) {
43050c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
430644fbe462SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt >= 3) {
43070c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
43080c0982b8SRandall Stewart 				}
4309830d754dSRandall Stewart 			}
4310830d754dSRandall Stewart 		}
4311830d754dSRandall Stewart 		if (lchk) {
4312830d754dSRandall Stewart 			/* Assure a timer is up */
4313830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4314830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
4315830d754dSRandall Stewart 		}
4316830d754dSRandall Stewart 	}
4317b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
4318f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4319f8829a4aSRandall Stewart 		    rwnd,
4320f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
4321f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
4322f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
432380fefe0aSRandall Stewart 	}
4324f8829a4aSRandall Stewart }
4325f8829a4aSRandall Stewart 
4326f8829a4aSRandall Stewart void
4327cd554309SMichael Tuexen sctp_handle_sack(struct mbuf *m, int offset_seg, int offset_dup,
43287215cc1bSMichael Tuexen     struct sctp_tcb *stcb,
4329cd554309SMichael Tuexen     uint16_t num_seg, uint16_t num_nr_seg, uint16_t num_dup,
4330cd554309SMichael Tuexen     int *abort_now, uint8_t flags,
4331899288aeSRandall Stewart     uint32_t cum_ack, uint32_t rwnd, int ecne_seen)
4332f8829a4aSRandall Stewart {
4333f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4334f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
4335cd554309SMichael Tuexen 	uint32_t last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked, this_sack_lowest_newack;
4336f8829a4aSRandall Stewart 	uint16_t wake_him = 0;
4337c105859eSRandall Stewart 	uint32_t send_s = 0;
4338f8829a4aSRandall Stewart 	long j;
4339f8829a4aSRandall Stewart 	int accum_moved = 0;
4340f8829a4aSRandall Stewart 	int will_exit_fast_recovery = 0;
43415e54f665SRandall Stewart 	uint32_t a_rwnd, old_rwnd;
43425e54f665SRandall Stewart 	int win_probe_recovery = 0;
4343c105859eSRandall Stewart 	int win_probe_recovered = 0;
4344f8829a4aSRandall Stewart 	struct sctp_nets *net = NULL;
4345bff64a4dSRandall Stewart 	int done_once;
4346f79aab18SRandall Stewart 	int rto_ok = 1;
4347f8829a4aSRandall Stewart 	uint8_t reneged_all = 0;
4348f8829a4aSRandall Stewart 	uint8_t cmt_dac_flag;
4349f8829a4aSRandall Stewart 
4350f8829a4aSRandall Stewart 	/*
4351f8829a4aSRandall Stewart 	 * we take any chance we can to service our queues since we cannot
4352f8829a4aSRandall Stewart 	 * get awoken when the socket is read from :<
4353f8829a4aSRandall Stewart 	 */
4354f8829a4aSRandall Stewart 	/*
4355f8829a4aSRandall Stewart 	 * Now perform the actual SACK handling: 1) Verify that it is not an
4356f8829a4aSRandall Stewart 	 * old sack, if so discard. 2) If there is nothing left in the send
4357f8829a4aSRandall Stewart 	 * queue (cum-ack is equal to last acked) then you have a duplicate
4358f8829a4aSRandall Stewart 	 * too, update any rwnd change and verify no timers are running.
4359f8829a4aSRandall Stewart 	 * then return. 3) Process any new consequtive data i.e. cum-ack
4360f8829a4aSRandall Stewart 	 * moved process these first and note that it moved. 4) Process any
4361f8829a4aSRandall Stewart 	 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
4362f8829a4aSRandall Stewart 	 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
4363f8829a4aSRandall Stewart 	 * sync up flightsizes and things, stop all timers and also check
4364f8829a4aSRandall Stewart 	 * for shutdown_pending state. If so then go ahead and send off the
4365f8829a4aSRandall Stewart 	 * shutdown. If in shutdown recv, send off the shutdown-ack and
4366f8829a4aSRandall Stewart 	 * start that timer, Ret. 9) Strike any non-acked things and do FR
4367f8829a4aSRandall Stewart 	 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
4368f8829a4aSRandall Stewart 	 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
4369f8829a4aSRandall Stewart 	 * if in shutdown_recv state.
4370f8829a4aSRandall Stewart 	 */
4371f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
4372f8829a4aSRandall Stewart 	/* CMT DAC algo */
4373f8829a4aSRandall Stewart 	this_sack_lowest_newack = 0;
4374f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_slowpath_sack);
4375cd554309SMichael Tuexen 	last_tsn = cum_ack;
4376cd554309SMichael Tuexen 	cmt_dac_flag = flags & SCTP_SACK_CMT_DAC;
437718e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
437818e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
437918e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
438018e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
438118e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
438218e198d3SRandall Stewart 	}
438318e198d3SRandall Stewart #endif
4384d06c82f1SRandall Stewart 	a_rwnd = rwnd;
4385f8829a4aSRandall Stewart 
4386cd554309SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4387cd554309SMichael Tuexen 		sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack,
4388cd554309SMichael Tuexen 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
4389cd554309SMichael Tuexen 	}
43905e54f665SRandall Stewart 	old_rwnd = stcb->asoc.peers_rwnd;
4391b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4392c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4393c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
4394c4739e2fSRandall Stewart 		    0,
4395c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
4396c4739e2fSRandall Stewart 		    __LINE__);
4397c4739e2fSRandall Stewart 	}
4398f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
4399f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
4400b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4401f8829a4aSRandall Stewart 		sctp_log_sack(asoc->last_acked_seq,
4402f8829a4aSRandall Stewart 		    cum_ack,
4403f8829a4aSRandall Stewart 		    0,
4404f8829a4aSRandall Stewart 		    num_seg,
4405f8829a4aSRandall Stewart 		    num_dup,
4406f8829a4aSRandall Stewart 		    SCTP_LOG_NEW_SACK);
440780fefe0aSRandall Stewart 	}
4408ca85e948SMichael Tuexen 	if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE)) {
4409cd554309SMichael Tuexen 		uint16_t i;
4410458303daSRandall Stewart 		uint32_t *dupdata, dblock;
4411f8829a4aSRandall Stewart 
4412cd554309SMichael Tuexen 		for (i = 0; i < num_dup; i++) {
4413cd554309SMichael Tuexen 			dupdata = (uint32_t *) sctp_m_getptr(m, offset_dup + i * sizeof(uint32_t),
4414458303daSRandall Stewart 			    sizeof(uint32_t), (uint8_t *) & dblock);
4415cd554309SMichael Tuexen 			if (dupdata == NULL) {
4416458303daSRandall Stewart 				break;
4417458303daSRandall Stewart 			}
4418cd554309SMichael Tuexen 			sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
4419f8829a4aSRandall Stewart 		}
4420f8829a4aSRandall Stewart 	}
4421b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4422c105859eSRandall Stewart 		/* reality check */
4423c105859eSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4424c105859eSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
4425c105859eSRandall Stewart 			    sctpchunk_listhead);
4426c105859eSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
4427c105859eSRandall Stewart 		} else {
4428b5c16493SMichael Tuexen 			tp1 = NULL;
4429c105859eSRandall Stewart 			send_s = asoc->sending_seq;
4430c105859eSRandall Stewart 		}
443120b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(cum_ack, send_s)) {
4432c105859eSRandall Stewart 			struct mbuf *oper;
4433c105859eSRandall Stewart 
4434f8829a4aSRandall Stewart 			/*
4435f8829a4aSRandall Stewart 			 * no way, we have not even sent this TSN out yet.
4436f8829a4aSRandall Stewart 			 * Peer is hopelessly messed up with us.
4437f8829a4aSRandall Stewart 			 */
4438b5c16493SMichael Tuexen 			printf("NEW cum_ack:%x send_s:%x is smaller or equal\n",
4439b5c16493SMichael Tuexen 			    cum_ack, send_s);
4440b5c16493SMichael Tuexen 			if (tp1) {
4441b5c16493SMichael Tuexen 				printf("Got send_s from tsn:%x + 1 of tp1:%p\n",
4442b5c16493SMichael Tuexen 				    tp1->rec.data.TSN_seq, tp1);
4443b5c16493SMichael Tuexen 			}
4444f8829a4aSRandall Stewart 	hopeless_peer:
4445f8829a4aSRandall Stewart 			*abort_now = 1;
4446f8829a4aSRandall Stewart 			/* XXX */
4447f8829a4aSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4448f8829a4aSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
4449f8829a4aSRandall Stewart 			if (oper) {
4450f8829a4aSRandall Stewart 				struct sctp_paramhdr *ph;
4451f8829a4aSRandall Stewart 				uint32_t *ippp;
4452f8829a4aSRandall Stewart 
4453139bc87fSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4454f8829a4aSRandall Stewart 				    sizeof(uint32_t);
4455f8829a4aSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
4456f8829a4aSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4457139bc87fSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
4458f8829a4aSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
4459a5d547adSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4460f8829a4aSRandall Stewart 			}
4461a5d547adSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4462ceaad40aSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
4463f8829a4aSRandall Stewart 			return;
4464f8829a4aSRandall Stewart 		}
4465f8829a4aSRandall Stewart 	}
4466f8829a4aSRandall Stewart 	/**********************/
4467f8829a4aSRandall Stewart 	/* 1) check the range */
4468f8829a4aSRandall Stewart 	/**********************/
446920b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->last_acked_seq, last_tsn)) {
4470f8829a4aSRandall Stewart 		/* acking something behind */
4471f8829a4aSRandall Stewart 		return;
4472f8829a4aSRandall Stewart 	}
4473f8829a4aSRandall Stewart 	/* update the Rwnd of the peer */
4474f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue) &&
4475f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->send_queue) &&
4476cd554309SMichael Tuexen 	    (asoc->stream_queue_cnt == 0)) {
4477f8829a4aSRandall Stewart 		/* nothing left on send/sent and strmq */
4478b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4479f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4480f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
448180fefe0aSRandall Stewart 		}
4482f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
4483f8829a4aSRandall Stewart 		if (asoc->sent_queue_retran_cnt) {
4484f8829a4aSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4485f8829a4aSRandall Stewart 		}
4486f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4487f8829a4aSRandall Stewart 			/* SWS sender side engages */
4488f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
4489f8829a4aSRandall Stewart 		}
4490f8829a4aSRandall Stewart 		/* stop any timers */
4491f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4492f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4493a5d547adSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4494f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4495f8829a4aSRandall Stewart 			net->flight_size = 0;
4496f8829a4aSRandall Stewart 		}
4497f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4498f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4499f8829a4aSRandall Stewart 		return;
4500f8829a4aSRandall Stewart 	}
4501f8829a4aSRandall Stewart 	/*
4502f8829a4aSRandall Stewart 	 * We init netAckSz and netAckSz2 to 0. These are used to track 2
4503f8829a4aSRandall Stewart 	 * things. The total byte count acked is tracked in netAckSz AND
4504f8829a4aSRandall Stewart 	 * netAck2 is used to track the total bytes acked that are un-
4505f8829a4aSRandall Stewart 	 * amibguious and were never retransmitted. We track these on a per
4506f8829a4aSRandall Stewart 	 * destination address basis.
4507f8829a4aSRandall Stewart 	 */
4508f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4509a21779f0SRandall Stewart 		if (SCTP_TSN_GT(cum_ack, net->cwr_window_tsn)) {
4510a21779f0SRandall Stewart 			/* Drag along the window_tsn for cwr's */
4511a21779f0SRandall Stewart 			net->cwr_window_tsn = cum_ack;
4512a21779f0SRandall Stewart 		}
4513f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
4514f8829a4aSRandall Stewart 		net->net_ack = 0;
4515f8829a4aSRandall Stewart 		net->net_ack2 = 0;
4516f8829a4aSRandall Stewart 
4517f8829a4aSRandall Stewart 		/*
451842551e99SRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
451942551e99SRandall Stewart 		 * SACK processing
4520f8829a4aSRandall Stewart 		 */
4521f8829a4aSRandall Stewart 		net->new_pseudo_cumack = 0;
4522f8829a4aSRandall Stewart 		net->will_exit_fast_recovery = 0;
4523299108c5SRandall Stewart 		if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) {
4524299108c5SRandall Stewart 			(*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) (stcb, net);
4525299108c5SRandall Stewart 		}
4526f8829a4aSRandall Stewart 	}
4527f8829a4aSRandall Stewart 	/* process the new consecutive TSN first */
45284a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
452920b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(last_tsn, tp1->rec.data.TSN_seq)) {
4530f8829a4aSRandall Stewart 			if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4531f8829a4aSRandall Stewart 				accum_moved = 1;
4532f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4533f8829a4aSRandall Stewart 					/*
4534f8829a4aSRandall Stewart 					 * If it is less than ACKED, it is
4535f8829a4aSRandall Stewart 					 * now no-longer in flight. Higher
4536f8829a4aSRandall Stewart 					 * values may occur during marking
4537f8829a4aSRandall Stewart 					 */
4538f8829a4aSRandall Stewart 					if ((tp1->whoTo->dest_state &
4539f8829a4aSRandall Stewart 					    SCTP_ADDR_UNCONFIRMED) &&
4540f8829a4aSRandall Stewart 					    (tp1->snd_count < 2)) {
4541f8829a4aSRandall Stewart 						/*
4542f8829a4aSRandall Stewart 						 * If there was no retran
4543f8829a4aSRandall Stewart 						 * and the address is
4544f8829a4aSRandall Stewart 						 * un-confirmed and we sent
4545f8829a4aSRandall Stewart 						 * there and are now
4546f8829a4aSRandall Stewart 						 * sacked.. its confirmed,
4547f8829a4aSRandall Stewart 						 * mark it so.
4548f8829a4aSRandall Stewart 						 */
4549f8829a4aSRandall Stewart 						tp1->whoTo->dest_state &=
4550f8829a4aSRandall Stewart 						    ~SCTP_ADDR_UNCONFIRMED;
4551f8829a4aSRandall Stewart 					}
4552c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4553b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4554c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4555a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
4556a5d547adSRandall Stewart 							    tp1->book_size,
4557c105859eSRandall Stewart 							    (uintptr_t) tp1->whoTo,
4558a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
455980fefe0aSRandall Stewart 						}
4560c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
4561c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
4562299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
4563299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
4564299108c5SRandall Stewart 							    tp1);
4565299108c5SRandall Stewart 						}
4566f8829a4aSRandall Stewart 					}
4567f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
4568f8829a4aSRandall Stewart 
4569f8829a4aSRandall Stewart 					/* CMT SFR and DAC algos */
4570f8829a4aSRandall Stewart 					this_sack_lowest_newack = tp1->rec.data.TSN_seq;
4571f8829a4aSRandall Stewart 					tp1->whoTo->saw_newack = 1;
4572f8829a4aSRandall Stewart 
4573f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
4574f8829a4aSRandall Stewart 						/*
4575f8829a4aSRandall Stewart 						 * True non-retransmited
4576f8829a4aSRandall Stewart 						 * chunk
4577f8829a4aSRandall Stewart 						 */
4578f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
4579f8829a4aSRandall Stewart 						    tp1->send_size;
4580f8829a4aSRandall Stewart 
4581f8829a4aSRandall Stewart 						/* update RTO too? */
4582f8829a4aSRandall Stewart 						if (tp1->do_rtt) {
4583f79aab18SRandall Stewart 							if (rto_ok) {
4584f8829a4aSRandall Stewart 								tp1->whoTo->RTO =
4585f8829a4aSRandall Stewart 								    sctp_calculate_rto(stcb,
4586f8829a4aSRandall Stewart 								    asoc, tp1->whoTo,
458718e198d3SRandall Stewart 								    &tp1->sent_rcv_time,
4588899288aeSRandall Stewart 								    sctp_align_safe_nocopy,
4589f79aab18SRandall Stewart 								    SCTP_RTT_FROM_DATA);
4590f79aab18SRandall Stewart 								rto_ok = 0;
4591f79aab18SRandall Stewart 							}
4592f79aab18SRandall Stewart 							if (tp1->whoTo->rto_needed == 0) {
4593f79aab18SRandall Stewart 								tp1->whoTo->rto_needed = 1;
4594f79aab18SRandall Stewart 							}
4595f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
4596f8829a4aSRandall Stewart 						}
4597f8829a4aSRandall Stewart 					}
4598f8829a4aSRandall Stewart 					/*
4599f8829a4aSRandall Stewart 					 * CMT: CUCv2 algorithm. From the
4600f8829a4aSRandall Stewart 					 * cumack'd TSNs, for each TSN being
4601f8829a4aSRandall Stewart 					 * acked for the first time, set the
4602f8829a4aSRandall Stewart 					 * following variables for the
4603f8829a4aSRandall Stewart 					 * corresp destination.
4604f8829a4aSRandall Stewart 					 * new_pseudo_cumack will trigger a
4605f8829a4aSRandall Stewart 					 * cwnd update.
4606f8829a4aSRandall Stewart 					 * find_(rtx_)pseudo_cumack will
4607f8829a4aSRandall Stewart 					 * trigger search for the next
4608f8829a4aSRandall Stewart 					 * expected (rtx-)pseudo-cumack.
4609f8829a4aSRandall Stewart 					 */
4610f8829a4aSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
4611f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
4612f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
4613f8829a4aSRandall Stewart 
4614f8829a4aSRandall Stewart 
4615b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4616f8829a4aSRandall Stewart 						sctp_log_sack(asoc->last_acked_seq,
4617f8829a4aSRandall Stewart 						    cum_ack,
4618f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
4619f8829a4aSRandall Stewart 						    0,
4620f8829a4aSRandall Stewart 						    0,
4621f8829a4aSRandall Stewart 						    SCTP_LOG_TSN_ACKED);
462280fefe0aSRandall Stewart 					}
4623b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
4624f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
462580fefe0aSRandall Stewart 					}
4626f8829a4aSRandall Stewart 				}
4627f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4628f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4629f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
4630f8829a4aSRandall Stewart 					sctp_audit_log(0xB3,
4631f8829a4aSRandall Stewart 					    (asoc->sent_queue_retran_cnt & 0x000000ff));
4632f8829a4aSRandall Stewart #endif
4633f8829a4aSRandall Stewart 				}
463442551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
463542551e99SRandall Stewart 					/* deflate the cwnd */
463642551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
463742551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
463842551e99SRandall Stewart 				}
4639f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
4640f8829a4aSRandall Stewart 			}
4641f8829a4aSRandall Stewart 		} else {
4642f8829a4aSRandall Stewart 			break;
4643f8829a4aSRandall Stewart 		}
4644f8829a4aSRandall Stewart 	}
4645f8829a4aSRandall Stewart 	biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
4646f8829a4aSRandall Stewart 	/* always set this up to cum-ack */
4647f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = last_tsn;
4648f8829a4aSRandall Stewart 
4649cd554309SMichael Tuexen 	if ((num_seg > 0) || (num_nr_seg > 0)) {
4650f8829a4aSRandall Stewart 
4651f8829a4aSRandall Stewart 		/*
4652f8829a4aSRandall Stewart 		 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
4653f8829a4aSRandall Stewart 		 * to be greater than the cumack. Also reset saw_newack to 0
4654f8829a4aSRandall Stewart 		 * for all dests.
4655f8829a4aSRandall Stewart 		 */
4656f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4657f8829a4aSRandall Stewart 			net->saw_newack = 0;
4658f8829a4aSRandall Stewart 			net->this_sack_highest_newack = last_tsn;
4659f8829a4aSRandall Stewart 		}
4660f8829a4aSRandall Stewart 
4661f8829a4aSRandall Stewart 		/*
4662f8829a4aSRandall Stewart 		 * thisSackHighestGap will increase while handling NEW
4663f8829a4aSRandall Stewart 		 * segments this_sack_highest_newack will increase while
4664f8829a4aSRandall Stewart 		 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
4665f8829a4aSRandall Stewart 		 * used for CMT DAC algo. saw_newack will also change.
4666f8829a4aSRandall Stewart 		 */
4667cd554309SMichael Tuexen 		if (sctp_handle_segments(m, &offset_seg, stcb, asoc, last_tsn, &biggest_tsn_acked,
4668cd554309SMichael Tuexen 		    &biggest_tsn_newly_acked, &this_sack_lowest_newack,
46697215cc1bSMichael Tuexen 		    num_seg, num_nr_seg, &rto_ok)) {
4670cd554309SMichael Tuexen 			wake_him++;
4671cd554309SMichael Tuexen 		}
4672b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4673f8829a4aSRandall Stewart 			/*
4674f8829a4aSRandall Stewart 			 * validate the biggest_tsn_acked in the gap acks if
4675f8829a4aSRandall Stewart 			 * strict adherence is wanted.
4676f8829a4aSRandall Stewart 			 */
467720b07a4dSMichael Tuexen 			if (SCTP_TSN_GE(biggest_tsn_acked, send_s)) {
4678f8829a4aSRandall Stewart 				/*
4679f8829a4aSRandall Stewart 				 * peer is either confused or we are under
4680f8829a4aSRandall Stewart 				 * attack. We must abort.
4681f8829a4aSRandall Stewart 				 */
4682b5c16493SMichael Tuexen 				printf("Hopeless peer! biggest_tsn_acked:%x largest seq:%x\n",
4683b5c16493SMichael Tuexen 				    biggest_tsn_acked,
4684b5c16493SMichael Tuexen 				    send_s);
4685b5c16493SMichael Tuexen 
4686f8829a4aSRandall Stewart 				goto hopeless_peer;
4687f8829a4aSRandall Stewart 			}
4688f8829a4aSRandall Stewart 		}
4689f8829a4aSRandall Stewart 	}
4690f8829a4aSRandall Stewart 	/*******************************************/
4691f8829a4aSRandall Stewart 	/* cancel ALL T3-send timer if accum moved */
4692f8829a4aSRandall Stewart 	/*******************************************/
46937c99d56fSMichael Tuexen 	if (asoc->sctp_cmt_on_off > 0) {
4694f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4695f8829a4aSRandall Stewart 			if (net->new_pseudo_cumack)
4696f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4697a5d547adSRandall Stewart 				    stcb, net,
4698a5d547adSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
4699f8829a4aSRandall Stewart 
4700f8829a4aSRandall Stewart 		}
4701f8829a4aSRandall Stewart 	} else {
4702f8829a4aSRandall Stewart 		if (accum_moved) {
4703f8829a4aSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4704f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4705a5d547adSRandall Stewart 				    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
4706f8829a4aSRandall Stewart 			}
4707f8829a4aSRandall Stewart 		}
4708f8829a4aSRandall Stewart 	}
4709f8829a4aSRandall Stewart 	/********************************************/
4710d9c5cfeaSMichael Tuexen 	/* drop the acked chunks from the sentqueue */
4711f8829a4aSRandall Stewart 	/********************************************/
4712f8829a4aSRandall Stewart 	asoc->last_acked_seq = cum_ack;
4713f8829a4aSRandall Stewart 
47147c99d56fSMichael Tuexen 	TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
471520b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, cum_ack)) {
4716f8829a4aSRandall Stewart 			break;
4717f8829a4aSRandall Stewart 		}
4718f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
4719f8829a4aSRandall Stewart 			/* no more sent on list */
472018e198d3SRandall Stewart 			printf("Warning, tp1->sent == %d and its now acked?\n",
472118e198d3SRandall Stewart 			    tp1->sent);
4722f8829a4aSRandall Stewart 		}
4723f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4724f8829a4aSRandall Stewart 		if (tp1->pr_sctp_on) {
4725f8829a4aSRandall Stewart 			if (asoc->pr_sctp_cnt != 0)
4726f8829a4aSRandall Stewart 				asoc->pr_sctp_cnt--;
4727f8829a4aSRandall Stewart 		}
47287c99d56fSMichael Tuexen 		asoc->sent_queue_cnt--;
4729f8829a4aSRandall Stewart 		if (tp1->data) {
473004ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
4731f8829a4aSRandall Stewart 			sctp_free_bufspace(stcb, asoc, tp1, 1);
4732f8829a4aSRandall Stewart 			sctp_m_freem(tp1->data);
47337c99d56fSMichael Tuexen 			tp1->data = NULL;
4734810ec536SMichael Tuexen 			if (asoc->peer_supports_prsctp && PR_SCTP_BUF_ENABLED(tp1->flags)) {
4735f8829a4aSRandall Stewart 				asoc->sent_queue_cnt_removeable--;
4736f8829a4aSRandall Stewart 			}
4737f8829a4aSRandall Stewart 		}
4738b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4739f8829a4aSRandall Stewart 			sctp_log_sack(asoc->last_acked_seq,
4740f8829a4aSRandall Stewart 			    cum_ack,
4741f8829a4aSRandall Stewart 			    tp1->rec.data.TSN_seq,
4742f8829a4aSRandall Stewart 			    0,
4743f8829a4aSRandall Stewart 			    0,
4744f8829a4aSRandall Stewart 			    SCTP_LOG_FREE_SENT);
474580fefe0aSRandall Stewart 		}
4746689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED);
4747f8829a4aSRandall Stewart 		wake_him++;
47487c99d56fSMichael Tuexen 	}
47497c99d56fSMichael Tuexen 	if (TAILQ_EMPTY(&asoc->sent_queue) && (asoc->total_flight > 0)) {
47507c99d56fSMichael Tuexen #ifdef INVARIANTS
47517c99d56fSMichael Tuexen 		panic("Warning flight size is postive and should be 0");
47527c99d56fSMichael Tuexen #else
47537c99d56fSMichael Tuexen 		SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
47547c99d56fSMichael Tuexen 		    asoc->total_flight);
47557c99d56fSMichael Tuexen #endif
47567c99d56fSMichael Tuexen 		asoc->total_flight = 0;
47577c99d56fSMichael Tuexen 	}
475804ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
4759f8829a4aSRandall Stewart 	if ((wake_him) && (stcb->sctp_socket)) {
4760ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4761ceaad40aSRandall Stewart 		struct socket *so;
4762ceaad40aSRandall Stewart 
4763ceaad40aSRandall Stewart #endif
4764f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4765b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
47667215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, wake_him, SCTP_WAKESND_FROM_SACK);
476780fefe0aSRandall Stewart 		}
4768ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4769ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
4770ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
4771ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
4772ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
4773ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4774ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4775ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4776ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
4777ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
4778ceaad40aSRandall Stewart 			return;
4779ceaad40aSRandall Stewart 		}
4780ceaad40aSRandall Stewart #endif
4781f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4782ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4783ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4784ceaad40aSRandall Stewart #endif
4785f8829a4aSRandall Stewart 	} else {
4786b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
47877215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, wake_him, SCTP_NOWAKE_FROM_SACK);
478880fefe0aSRandall Stewart 		}
4789f8829a4aSRandall Stewart 	}
4790f8829a4aSRandall Stewart 
479142551e99SRandall Stewart 	if (asoc->fast_retran_loss_recovery && accum_moved) {
479220b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(asoc->last_acked_seq, asoc->fast_recovery_tsn)) {
4793f8829a4aSRandall Stewart 			/* Setup so we will exit RFC2582 fast recovery */
4794f8829a4aSRandall Stewart 			will_exit_fast_recovery = 1;
4795f8829a4aSRandall Stewart 		}
4796f8829a4aSRandall Stewart 	}
4797f8829a4aSRandall Stewart 	/*
4798f8829a4aSRandall Stewart 	 * Check for revoked fragments:
4799f8829a4aSRandall Stewart 	 *
4800f8829a4aSRandall Stewart 	 * if Previous sack - Had no frags then we can't have any revoked if
4801f8829a4aSRandall Stewart 	 * Previous sack - Had frag's then - If we now have frags aka
4802f8829a4aSRandall Stewart 	 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
4803f8829a4aSRandall Stewart 	 * some of them. else - The peer revoked all ACKED fragments, since
4804f8829a4aSRandall Stewart 	 * we had some before and now we have NONE.
4805f8829a4aSRandall Stewart 	 */
4806f8829a4aSRandall Stewart 
4807d9c5cfeaSMichael Tuexen 	if (num_seg) {
4808c105859eSRandall Stewart 		sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
4809d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_frags = 1;
4810d9c5cfeaSMichael Tuexen 	} else if (asoc->saw_sack_with_frags) {
4811f8829a4aSRandall Stewart 		int cnt_revoked = 0;
4812f8829a4aSRandall Stewart 
4813f8829a4aSRandall Stewart 		/* Peer revoked all dg's marked or acked */
4814f8829a4aSRandall Stewart 		TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4815b5c16493SMichael Tuexen 			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
4816f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_SENT;
4817b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4818c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
4819c105859eSRandall Stewart 					    tp1->whoTo->flight_size,
4820c105859eSRandall Stewart 					    tp1->book_size,
4821c105859eSRandall Stewart 					    (uintptr_t) tp1->whoTo,
4822c105859eSRandall Stewart 					    tp1->rec.data.TSN_seq);
482380fefe0aSRandall Stewart 				}
4824c105859eSRandall Stewart 				sctp_flight_size_increase(tp1);
4825c105859eSRandall Stewart 				sctp_total_flight_increase(stcb, tp1);
4826a5d547adSRandall Stewart 				tp1->rec.data.chunk_was_revoked = 1;
482742551e99SRandall Stewart 				/*
482842551e99SRandall Stewart 				 * To ensure that this increase in
48294a9ef3f8SMichael Tuexen 				 * flightsize, which is artificial, does not
48304a9ef3f8SMichael Tuexen 				 * throttle the sender, we also increase the
48314a9ef3f8SMichael Tuexen 				 * cwnd artificially.
483242551e99SRandall Stewart 				 */
483342551e99SRandall Stewart 				tp1->whoTo->cwnd += tp1->book_size;
4834f8829a4aSRandall Stewart 				cnt_revoked++;
4835f8829a4aSRandall Stewart 			}
4836f8829a4aSRandall Stewart 		}
4837f8829a4aSRandall Stewart 		if (cnt_revoked) {
4838f8829a4aSRandall Stewart 			reneged_all = 1;
4839f8829a4aSRandall Stewart 		}
4840f8829a4aSRandall Stewart 		asoc->saw_sack_with_frags = 0;
4841f8829a4aSRandall Stewart 	}
4842d9c5cfeaSMichael Tuexen 	if (num_nr_seg > 0)
4843d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_nr_frags = 1;
4844f8829a4aSRandall Stewart 	else
4845d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_nr_frags = 0;
4846f8829a4aSRandall Stewart 
4847b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
4848ca85e948SMichael Tuexen 	if (ecne_seen == 0) {
4849ca85e948SMichael Tuexen 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4850ca85e948SMichael Tuexen 			if (net->net_ack2 > 0) {
4851ca85e948SMichael Tuexen 				/*
4852ca85e948SMichael Tuexen 				 * Karn's rule applies to clearing error
4853ca85e948SMichael Tuexen 				 * count, this is optional.
4854ca85e948SMichael Tuexen 				 */
4855ca85e948SMichael Tuexen 				net->error_count = 0;
4856ca85e948SMichael Tuexen 				if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
4857ca85e948SMichael Tuexen 					/* addr came good */
4858ca85e948SMichael Tuexen 					net->dest_state |= SCTP_ADDR_REACHABLE;
4859ca85e948SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
4860ca85e948SMichael Tuexen 					    SCTP_RECEIVED_SACK, (void *)net, SCTP_SO_NOT_LOCKED);
4861ca85e948SMichael Tuexen 				}
4862ca85e948SMichael Tuexen 				if (net == stcb->asoc.primary_destination) {
4863ca85e948SMichael Tuexen 					if (stcb->asoc.alternate) {
4864ca85e948SMichael Tuexen 						/*
4865ca85e948SMichael Tuexen 						 * release the alternate,
4866ca85e948SMichael Tuexen 						 * primary is good
4867ca85e948SMichael Tuexen 						 */
4868ca85e948SMichael Tuexen 						sctp_free_remote_addr(stcb->asoc.alternate);
4869ca85e948SMichael Tuexen 						stcb->asoc.alternate = NULL;
4870ca85e948SMichael Tuexen 					}
4871ca85e948SMichael Tuexen 				}
4872ca85e948SMichael Tuexen 				if (net->dest_state & SCTP_ADDR_PF) {
4873ca85e948SMichael Tuexen 					net->dest_state &= ~SCTP_ADDR_PF;
4874ca85e948SMichael Tuexen 					sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
4875ca85e948SMichael Tuexen 					sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
4876ca85e948SMichael Tuexen 					asoc->cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
4877ca85e948SMichael Tuexen 					/* Done with this net */
4878ca85e948SMichael Tuexen 					net->net_ack = 0;
4879ca85e948SMichael Tuexen 				}
4880ca85e948SMichael Tuexen 				/* restore any doubled timers */
4881ca85e948SMichael Tuexen 				net->RTO = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
4882ca85e948SMichael Tuexen 				if (net->RTO < stcb->asoc.minrto) {
4883ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.minrto;
4884ca85e948SMichael Tuexen 				}
4885ca85e948SMichael Tuexen 				if (net->RTO > stcb->asoc.maxrto) {
4886ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.maxrto;
4887ca85e948SMichael Tuexen 				}
4888ca85e948SMichael Tuexen 			}
4889ca85e948SMichael Tuexen 		}
4890b54d3a6cSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
4891ca85e948SMichael Tuexen 	}
4892f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
4893f8829a4aSRandall Stewart 		/* nothing left in-flight */
4894f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4895f8829a4aSRandall Stewart 			/* stop all timers */
4896f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4897a5d547adSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
4898f8829a4aSRandall Stewart 			net->flight_size = 0;
4899f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4900f8829a4aSRandall Stewart 		}
4901f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4902f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4903f8829a4aSRandall Stewart 	}
4904f8829a4aSRandall Stewart 	/**********************************/
4905f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
4906f8829a4aSRandall Stewart 	/**********************************/
4907f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4908f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
4909b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4910f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4911f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
491280fefe0aSRandall Stewart 		}
4913f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
4914f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4915f8829a4aSRandall Stewart 			/* SWS sender side engages */
4916f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
4917f8829a4aSRandall Stewart 		}
4918f8829a4aSRandall Stewart 		/* clean up */
4919f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
4920f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4921f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4922f8829a4aSRandall Stewart 		    (asoc->locked_on_sending)
4923f8829a4aSRandall Stewart 		    ) {
4924f8829a4aSRandall Stewart 			struct sctp_stream_queue_pending *sp;
4925f8829a4aSRandall Stewart 
4926f8829a4aSRandall Stewart 			/*
4927f8829a4aSRandall Stewart 			 * I may be in a state where we got all across.. but
4928f8829a4aSRandall Stewart 			 * cannot write more due to a shutdown... we abort
4929f8829a4aSRandall Stewart 			 * since the user did not indicate EOR in this case.
4930f8829a4aSRandall Stewart 			 */
4931f8829a4aSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4932f8829a4aSRandall Stewart 			    sctp_streamhead);
49332afb3e84SRandall Stewart 			if ((sp) && (sp->length == 0)) {
4934f8829a4aSRandall Stewart 				asoc->locked_on_sending = NULL;
49352afb3e84SRandall Stewart 				if (sp->msg_is_complete) {
4936f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
49372afb3e84SRandall Stewart 				} else {
49382afb3e84SRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
49392afb3e84SRandall Stewart 					asoc->stream_queue_cnt--;
49402afb3e84SRandall Stewart 				}
4941f8829a4aSRandall Stewart 			}
4942f8829a4aSRandall Stewart 		}
4943f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4944f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4945f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4946f8829a4aSRandall Stewart 				/* Need to abort here */
4947f8829a4aSRandall Stewart 				struct mbuf *oper;
4948f8829a4aSRandall Stewart 
4949f8829a4aSRandall Stewart 		abort_out_now:
4950f8829a4aSRandall Stewart 				*abort_now = 1;
4951f8829a4aSRandall Stewart 				/* XXX */
4952f8829a4aSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4953f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
4954f8829a4aSRandall Stewart 				if (oper) {
4955f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
4956f8829a4aSRandall Stewart 					uint32_t *ippp;
4957f8829a4aSRandall Stewart 
4958139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4959f8829a4aSRandall Stewart 					    sizeof(uint32_t);
4960f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
4961f8829a4aSRandall Stewart 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4962139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
4963f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
4964a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_31);
4965f8829a4aSRandall Stewart 				}
4966a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
4967ceaad40aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
4968f8829a4aSRandall Stewart 				return;
4969f8829a4aSRandall Stewart 			} else {
4970ca85e948SMichael Tuexen 				struct sctp_nets *netp;
4971ca85e948SMichael Tuexen 
4972ca85e948SMichael Tuexen 				if (asoc->alternate) {
4973ca85e948SMichael Tuexen 					netp = asoc->alternate;
4974ca85e948SMichael Tuexen 				} else {
4975ca85e948SMichael Tuexen 					netp = asoc->primary_destination;
4976ca85e948SMichael Tuexen 				}
4977f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4978f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4979f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4980f42a358aSRandall Stewart 				}
4981c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4982b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4983f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
4984ca85e948SMichael Tuexen 				sctp_send_shutdown(stcb, netp);
4985f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4986ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4987f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4988ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4989f8829a4aSRandall Stewart 			}
4990f8829a4aSRandall Stewart 			return;
4991f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4992f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4993ca85e948SMichael Tuexen 			struct sctp_nets *netp;
4994ca85e948SMichael Tuexen 
4995ca85e948SMichael Tuexen 			if (asoc->alternate) {
4996ca85e948SMichael Tuexen 				netp = asoc->alternate;
4997ca85e948SMichael Tuexen 			} else {
4998ca85e948SMichael Tuexen 				netp = asoc->primary_destination;
4999ca85e948SMichael Tuexen 			}
5000f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
5001f8829a4aSRandall Stewart 				goto abort_out_now;
5002f8829a4aSRandall Stewart 			}
5003f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5004c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
5005b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
5006ca85e948SMichael Tuexen 			sctp_send_shutdown_ack(stcb, netp);
500712af6654SMichael Tuexen 			sctp_stop_timers_for_shutdown(stcb);
5008f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
5009ca85e948SMichael Tuexen 			    stcb->sctp_ep, stcb, netp);
5010f8829a4aSRandall Stewart 			return;
5011f8829a4aSRandall Stewart 		}
5012f8829a4aSRandall Stewart 	}
5013f8829a4aSRandall Stewart 	/*
5014f8829a4aSRandall Stewart 	 * Now here we are going to recycle net_ack for a different use...
5015f8829a4aSRandall Stewart 	 * HEADS UP.
5016f8829a4aSRandall Stewart 	 */
5017f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5018f8829a4aSRandall Stewart 		net->net_ack = 0;
5019f8829a4aSRandall Stewart 	}
5020f8829a4aSRandall Stewart 
5021f8829a4aSRandall Stewart 	/*
5022f8829a4aSRandall Stewart 	 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
5023f8829a4aSRandall Stewart 	 * to be done. Setting this_sack_lowest_newack to the cum_ack will
5024f8829a4aSRandall Stewart 	 * automatically ensure that.
5025f8829a4aSRandall Stewart 	 */
50267c99d56fSMichael Tuexen 	if ((asoc->sctp_cmt_on_off > 0) &&
502720083c2eSMichael Tuexen 	    SCTP_BASE_SYSCTL(sctp_cmt_use_dac) &&
502820083c2eSMichael Tuexen 	    (cmt_dac_flag == 0)) {
5029f8829a4aSRandall Stewart 		this_sack_lowest_newack = cum_ack;
5030f8829a4aSRandall Stewart 	}
5031cd554309SMichael Tuexen 	if ((num_seg > 0) || (num_nr_seg > 0)) {
5032f8829a4aSRandall Stewart 		sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
5033f8829a4aSRandall Stewart 		    biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
5034f8829a4aSRandall Stewart 	}
5035b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
5036b54d3a6cSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
5037f8829a4aSRandall Stewart 
5038f8829a4aSRandall Stewart 	/* Now are we exiting loss recovery ? */
5039f8829a4aSRandall Stewart 	if (will_exit_fast_recovery) {
5040f8829a4aSRandall Stewart 		/* Ok, we must exit fast recovery */
5041f8829a4aSRandall Stewart 		asoc->fast_retran_loss_recovery = 0;
5042f8829a4aSRandall Stewart 	}
5043f8829a4aSRandall Stewart 	if ((asoc->sat_t3_loss_recovery) &&
504420b07a4dSMichael Tuexen 	    SCTP_TSN_GE(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn)) {
5045f8829a4aSRandall Stewart 		/* end satellite t3 loss recovery */
5046f8829a4aSRandall Stewart 		asoc->sat_t3_loss_recovery = 0;
5047f8829a4aSRandall Stewart 	}
504842551e99SRandall Stewart 	/*
504942551e99SRandall Stewart 	 * CMT Fast recovery
505042551e99SRandall Stewart 	 */
5051f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5052f8829a4aSRandall Stewart 		if (net->will_exit_fast_recovery) {
5053f8829a4aSRandall Stewart 			/* Ok, we must exit fast recovery */
5054f8829a4aSRandall Stewart 			net->fast_retran_loss_recovery = 0;
5055f8829a4aSRandall Stewart 		}
5056f8829a4aSRandall Stewart 	}
5057f8829a4aSRandall Stewart 
5058f8829a4aSRandall Stewart 	/* Adjust and set the new rwnd value */
5059b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
5060f8829a4aSRandall Stewart 		sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
506144fbe462SRandall Stewart 		    asoc->peers_rwnd, asoc->total_flight, (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd);
506280fefe0aSRandall Stewart 	}
5063f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
506444fbe462SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
5065f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5066f8829a4aSRandall Stewart 		/* SWS sender side engages */
5067f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
5068f8829a4aSRandall Stewart 	}
50695e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
50705e54f665SRandall Stewart 		win_probe_recovery = 1;
50715e54f665SRandall Stewart 	}
5072f8829a4aSRandall Stewart 	/*
5073f8829a4aSRandall Stewart 	 * Now we must setup so we have a timer up for anyone with
5074f8829a4aSRandall Stewart 	 * outstanding data.
5075f8829a4aSRandall Stewart 	 */
5076bff64a4dSRandall Stewart 	done_once = 0;
5077a5d547adSRandall Stewart again:
5078a5d547adSRandall Stewart 	j = 0;
5079f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
50805e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
5081c105859eSRandall Stewart 			win_probe_recovered = 1;
50825e54f665SRandall Stewart 			/*-
50835e54f665SRandall Stewart 			 * Find first chunk that was used with
50845e54f665SRandall Stewart 			 * window probe and clear the event. Put
50855e54f665SRandall Stewart 			 * it back into the send queue as if has
50865e54f665SRandall Stewart 			 * not been sent.
50875e54f665SRandall Stewart 			 */
50885e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
50895e54f665SRandall Stewart 				if (tp1->window_probe) {
50907215cc1bSMichael Tuexen 					sctp_window_probe_recovery(stcb, asoc, tp1);
50915e54f665SRandall Stewart 					break;
50925e54f665SRandall Stewart 				}
50935e54f665SRandall Stewart 			}
50945e54f665SRandall Stewart 		}
5095f8829a4aSRandall Stewart 		if (net->flight_size) {
5096a5d547adSRandall Stewart 			j++;
5097cd554309SMichael Tuexen 			if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5098f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5099f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, net);
5100cd554309SMichael Tuexen 			}
51015171328bSRandall Stewart 			if (net->window_probe) {
5102cd554309SMichael Tuexen 				net->window_probe = 0;
51035171328bSRandall Stewart 			}
5104c105859eSRandall Stewart 		} else {
51055171328bSRandall Stewart 			if (net->window_probe) {
51065171328bSRandall Stewart 				/*
51075171328bSRandall Stewart 				 * In window probes we must assure a timer
51085171328bSRandall Stewart 				 * is still running there
51095171328bSRandall Stewart 				 */
51105171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
51115171328bSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_SEND,
51125171328bSRandall Stewart 					    stcb->sctp_ep, stcb, net);
51135171328bSRandall Stewart 
51145171328bSRandall Stewart 				}
51155171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5116c105859eSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5117c105859eSRandall Stewart 				    stcb, net,
5118c105859eSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
5119c105859eSRandall Stewart 			}
5120f8829a4aSRandall Stewart 		}
5121f8829a4aSRandall Stewart 	}
5122bff64a4dSRandall Stewart 	if ((j == 0) &&
5123bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
5124bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
5125c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
5126bff64a4dSRandall Stewart 	    (done_once == 0)) {
51270c0982b8SRandall Stewart 		/*
51280c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
51290c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
51300c0982b8SRandall Stewart 		 */
51310c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
5132a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5133a5d547adSRandall Stewart 				net->flight_size = 0;
5134a5d547adSRandall Stewart 			}
5135a5d547adSRandall Stewart 			asoc->total_flight = 0;
5136a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
5137a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
5138a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5139a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5140c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
5141c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
5142a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5143791437b5SRandall Stewart 					sctp_ucount_incr(asoc->sent_queue_retran_cnt);
5144a5d547adSRandall Stewart 				}
5145a5d547adSRandall Stewart 			}
51460c0982b8SRandall Stewart 		}
5147bff64a4dSRandall Stewart 		done_once = 1;
5148a5d547adSRandall Stewart 		goto again;
5149a5d547adSRandall Stewart 	}
5150cd554309SMichael Tuexen 	/*********************************************/
5151cd554309SMichael Tuexen 	/* Here we perform PR-SCTP procedures        */
5152cd554309SMichael Tuexen 	/* (section 4.2)                             */
5153cd554309SMichael Tuexen 	/*********************************************/
5154cd554309SMichael Tuexen 	/* C1. update advancedPeerAckPoint */
515520b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cum_ack, asoc->advanced_peer_ack_point)) {
5156dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cum_ack;
5157dfb11ef8SRandall Stewart 	}
5158830d754dSRandall Stewart 	/* C2. try to further move advancedPeerAckPoint ahead */
5159830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
5160830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
5161830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
5162830d754dSRandall Stewart 
5163830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
5164830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
5165830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
516620b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cum_ack)) {
5167830d754dSRandall Stewart 			/*
5168493d8e5aSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing.
5169830d754dSRandall Stewart 			 */
51700c0982b8SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
51710c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
51720c0982b8SRandall Stewart 				    0xee, cum_ack, asoc->advanced_peer_ack_point,
51730c0982b8SRandall Stewart 				    old_adv_peer_ack_point);
51740c0982b8SRandall Stewart 			}
517520b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) {
5176830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
51770c0982b8SRandall Stewart 			} else if (lchk) {
51780c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
517944fbe462SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt >= 3) {
51800c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
51810c0982b8SRandall Stewart 				}
5182830d754dSRandall Stewart 			}
5183830d754dSRandall Stewart 		}
5184830d754dSRandall Stewart 		if (lchk) {
5185830d754dSRandall Stewart 			/* Assure a timer is up */
5186830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5187830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
5188830d754dSRandall Stewart 		}
5189830d754dSRandall Stewart 	}
5190b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
5191f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
5192f8829a4aSRandall Stewart 		    a_rwnd,
5193f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
5194f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
5195f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
519680fefe0aSRandall Stewart 	}
5197f8829a4aSRandall Stewart }
5198f8829a4aSRandall Stewart 
5199f8829a4aSRandall Stewart void
52007215cc1bSMichael Tuexen sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp, int *abort_flag)
5201f8829a4aSRandall Stewart {
5202f8829a4aSRandall Stewart 	/* Copy cum-ack */
5203f8829a4aSRandall Stewart 	uint32_t cum_ack, a_rwnd;
5204f8829a4aSRandall Stewart 
5205f8829a4aSRandall Stewart 	cum_ack = ntohl(cp->cumulative_tsn_ack);
5206f8829a4aSRandall Stewart 	/* Arrange so a_rwnd does NOT change */
5207f8829a4aSRandall Stewart 	a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight;
5208f8829a4aSRandall Stewart 
5209f8829a4aSRandall Stewart 	/* Now call the express sack handling */
5210899288aeSRandall Stewart 	sctp_express_handle_sack(stcb, cum_ack, a_rwnd, abort_flag, 0);
5211f8829a4aSRandall Stewart }
5212f8829a4aSRandall Stewart 
5213f8829a4aSRandall Stewart static void
5214f8829a4aSRandall Stewart sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
5215f8829a4aSRandall Stewart     struct sctp_stream_in *strmin)
5216f8829a4aSRandall Stewart {
5217f8829a4aSRandall Stewart 	struct sctp_queued_to_read *ctl, *nctl;
5218f8829a4aSRandall Stewart 	struct sctp_association *asoc;
521983128708SRandall Stewart 	uint16_t tt;
5220f8829a4aSRandall Stewart 
5221f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
5222f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered;
5223f8829a4aSRandall Stewart 	/*
5224f8829a4aSRandall Stewart 	 * First deliver anything prior to and including the stream no that
5225f8829a4aSRandall Stewart 	 * came in
5226f8829a4aSRandall Stewart 	 */
52274a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(ctl, &strmin->inqueue, next, nctl) {
522820b07a4dSMichael Tuexen 		if (SCTP_SSN_GE(tt, ctl->sinfo_ssn)) {
5229f8829a4aSRandall Stewart 			/* this is deliverable now */
5230f8829a4aSRandall Stewart 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5231f8829a4aSRandall Stewart 			/* subtract pending on streams */
5232f8829a4aSRandall Stewart 			asoc->size_on_all_streams -= ctl->length;
5233f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_all_streams);
5234f8829a4aSRandall Stewart 			/* deliver it to at least the delivery-q */
5235f8829a4aSRandall Stewart 			if (stcb->sctp_socket) {
5236b5c16493SMichael Tuexen 				sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
5237f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
5238f8829a4aSRandall Stewart 				    ctl,
5239cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
5240f8829a4aSRandall Stewart 			}
5241f8829a4aSRandall Stewart 		} else {
5242f8829a4aSRandall Stewart 			/* no more delivery now. */
5243f8829a4aSRandall Stewart 			break;
5244f8829a4aSRandall Stewart 		}
5245f8829a4aSRandall Stewart 	}
5246f8829a4aSRandall Stewart 	/*
5247f8829a4aSRandall Stewart 	 * now we must deliver things in queue the normal way  if any are
5248f8829a4aSRandall Stewart 	 * now ready.
5249f8829a4aSRandall Stewart 	 */
5250f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered + 1;
52514a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(ctl, &strmin->inqueue, next, nctl) {
5252f8829a4aSRandall Stewart 		if (tt == ctl->sinfo_ssn) {
5253f8829a4aSRandall Stewart 			/* this is deliverable now */
5254f8829a4aSRandall Stewart 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5255f8829a4aSRandall Stewart 			/* subtract pending on streams */
5256f8829a4aSRandall Stewart 			asoc->size_on_all_streams -= ctl->length;
5257f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_all_streams);
5258f8829a4aSRandall Stewart 			/* deliver it to at least the delivery-q */
5259f8829a4aSRandall Stewart 			strmin->last_sequence_delivered = ctl->sinfo_ssn;
5260f8829a4aSRandall Stewart 			if (stcb->sctp_socket) {
5261b5c16493SMichael Tuexen 				sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
5262f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
5263f8829a4aSRandall Stewart 				    ctl,
5264cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
526577acdc25SRandall Stewart 
5266f8829a4aSRandall Stewart 			}
5267f8829a4aSRandall Stewart 			tt = strmin->last_sequence_delivered + 1;
5268f8829a4aSRandall Stewart 		} else {
5269f8829a4aSRandall Stewart 			break;
5270f8829a4aSRandall Stewart 		}
5271f8829a4aSRandall Stewart 	}
5272f8829a4aSRandall Stewart }
5273f8829a4aSRandall Stewart 
52748933fa13SRandall Stewart static void
52758933fa13SRandall Stewart sctp_flush_reassm_for_str_seq(struct sctp_tcb *stcb,
52768933fa13SRandall Stewart     struct sctp_association *asoc,
52778933fa13SRandall Stewart     uint16_t stream, uint16_t seq)
52788933fa13SRandall Stewart {
52794a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
52808933fa13SRandall Stewart 
52818933fa13SRandall Stewart 	/* For each one on here see if we need to toss it */
52828933fa13SRandall Stewart 	/*
52834a9ef3f8SMichael Tuexen 	 * For now large messages held on the reasmqueue that are complete
52844a9ef3f8SMichael Tuexen 	 * will be tossed too. We could in theory do more work to spin
52854a9ef3f8SMichael Tuexen 	 * through and stop after dumping one msg aka seeing the start of a
52864a9ef3f8SMichael Tuexen 	 * new msg at the head, and call the delivery function... to see if
52874a9ef3f8SMichael Tuexen 	 * it can be delivered... But for now we just dump everything on the
52884a9ef3f8SMichael Tuexen 	 * queue.
52898933fa13SRandall Stewart 	 */
52904a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
52918e71b694SMichael Tuexen 		/*
52924a9ef3f8SMichael Tuexen 		 * Do not toss it if on a different stream or marked for
52934a9ef3f8SMichael Tuexen 		 * unordered delivery in which case the stream sequence
52944a9ef3f8SMichael Tuexen 		 * number has no meaning.
52958e71b694SMichael Tuexen 		 */
52968e71b694SMichael Tuexen 		if ((chk->rec.data.stream_number != stream) ||
52978e71b694SMichael Tuexen 		    ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == SCTP_DATA_UNORDERED)) {
52988933fa13SRandall Stewart 			continue;
52998933fa13SRandall Stewart 		}
53008933fa13SRandall Stewart 		if (chk->rec.data.stream_seq == seq) {
53018933fa13SRandall Stewart 			/* It needs to be tossed */
53028933fa13SRandall Stewart 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
530320b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(chk->rec.data.TSN_seq, asoc->tsn_last_delivered)) {
53044a9ef3f8SMichael Tuexen 				asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
53054a9ef3f8SMichael Tuexen 				asoc->str_of_pdapi = chk->rec.data.stream_number;
53064a9ef3f8SMichael Tuexen 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
53074a9ef3f8SMichael Tuexen 				asoc->fragment_flags = chk->rec.data.rcv_flags;
53088933fa13SRandall Stewart 			}
53098933fa13SRandall Stewart 			asoc->size_on_reasm_queue -= chk->send_size;
53108933fa13SRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_reasm_queue);
53118933fa13SRandall Stewart 
53128933fa13SRandall Stewart 			/* Clear up any stream problem */
531320b07a4dSMichael Tuexen 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) != SCTP_DATA_UNORDERED &&
531420b07a4dSMichael Tuexen 			    SCTP_SSN_GT(chk->rec.data.stream_seq, asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered)) {
53158933fa13SRandall Stewart 				/*
53168933fa13SRandall Stewart 				 * We must dump forward this streams
53174a9ef3f8SMichael Tuexen 				 * sequence number if the chunk is not
53184a9ef3f8SMichael Tuexen 				 * unordered that is being skipped. There is
53194a9ef3f8SMichael Tuexen 				 * a chance that if the peer does not
53204a9ef3f8SMichael Tuexen 				 * include the last fragment in its FWD-TSN
53214a9ef3f8SMichael Tuexen 				 * we WILL have a problem here since you
53224a9ef3f8SMichael Tuexen 				 * would have a partial chunk in queue that
53234a9ef3f8SMichael Tuexen 				 * may not be deliverable. Also if a Partial
53244a9ef3f8SMichael Tuexen 				 * delivery API as started the user may get
53254a9ef3f8SMichael Tuexen 				 * a partial chunk. The next read returning
53264a9ef3f8SMichael Tuexen 				 * a new chunk... really ugly but I see no
53274a9ef3f8SMichael Tuexen 				 * way around it! Maybe a notify??
53288933fa13SRandall Stewart 				 */
53294a9ef3f8SMichael Tuexen 				asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered = chk->rec.data.stream_seq;
53308933fa13SRandall Stewart 			}
53318933fa13SRandall Stewart 			if (chk->data) {
53328933fa13SRandall Stewart 				sctp_m_freem(chk->data);
53338933fa13SRandall Stewart 				chk->data = NULL;
53348933fa13SRandall Stewart 			}
5335689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
533620b07a4dSMichael Tuexen 		} else if (SCTP_SSN_GT(chk->rec.data.stream_seq, seq)) {
53378933fa13SRandall Stewart 			/*
53384a9ef3f8SMichael Tuexen 			 * If the stream_seq is > than the purging one, we
53394a9ef3f8SMichael Tuexen 			 * are done
53408933fa13SRandall Stewart 			 */
53418933fa13SRandall Stewart 			break;
53428933fa13SRandall Stewart 		}
53438933fa13SRandall Stewart 	}
53448933fa13SRandall Stewart }
53458933fa13SRandall Stewart 
53468933fa13SRandall Stewart 
5347f8829a4aSRandall Stewart void
5348f8829a4aSRandall Stewart sctp_handle_forward_tsn(struct sctp_tcb *stcb,
5349b5c16493SMichael Tuexen     struct sctp_forward_tsn_chunk *fwd,
5350b5c16493SMichael Tuexen     int *abort_flag, struct mbuf *m, int offset)
5351f8829a4aSRandall Stewart {
5352f8829a4aSRandall Stewart 	/* The pr-sctp fwd tsn */
5353f8829a4aSRandall Stewart 	/*
5354f8829a4aSRandall Stewart 	 * here we will perform all the data receiver side steps for
5355f8829a4aSRandall Stewart 	 * processing FwdTSN, as required in by pr-sctp draft:
5356f8829a4aSRandall Stewart 	 *
5357f8829a4aSRandall Stewart 	 * Assume we get FwdTSN(x):
5358f8829a4aSRandall Stewart 	 *
5359f8829a4aSRandall Stewart 	 * 1) update local cumTSN to x 2) try to further advance cumTSN to x +
5360f8829a4aSRandall Stewart 	 * others we have 3) examine and update re-ordering queue on
5361f8829a4aSRandall Stewart 	 * pr-in-streams 4) clean up re-assembly queue 5) Send a sack to
5362f8829a4aSRandall Stewart 	 * report where we are.
5363f8829a4aSRandall Stewart 	 */
5364f8829a4aSRandall Stewart 	struct sctp_association *asoc;
53657898f408SRandall Stewart 	uint32_t new_cum_tsn, gap;
53667215cc1bSMichael Tuexen 	unsigned int i, fwd_sz, m_size;
53678933fa13SRandall Stewart 	uint32_t str_seq;
5368f8829a4aSRandall Stewart 	struct sctp_stream_in *strm;
53694a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
53708933fa13SRandall Stewart 	struct sctp_queued_to_read *ctl, *sv;
5371f8829a4aSRandall Stewart 
5372f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
5373f8829a4aSRandall Stewart 	if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
5374ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1,
5375ad81507eSRandall Stewart 		    "Bad size too small/big fwd-tsn\n");
5376f8829a4aSRandall Stewart 		return;
5377f8829a4aSRandall Stewart 	}
5378f8829a4aSRandall Stewart 	m_size = (stcb->asoc.mapping_array_size << 3);
5379f8829a4aSRandall Stewart 	/*************************************************************/
5380f8829a4aSRandall Stewart 	/* 1. Here we update local cumTSN and shift the bitmap array */
5381f8829a4aSRandall Stewart 	/*************************************************************/
5382f8829a4aSRandall Stewart 	new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
5383f8829a4aSRandall Stewart 
538420b07a4dSMichael Tuexen 	if (SCTP_TSN_GE(asoc->cumulative_tsn, new_cum_tsn)) {
5385f8829a4aSRandall Stewart 		/* Already got there ... */
5386f8829a4aSRandall Stewart 		return;
5387f8829a4aSRandall Stewart 	}
5388f8829a4aSRandall Stewart 	/*
5389f8829a4aSRandall Stewart 	 * now we know the new TSN is more advanced, let's find the actual
5390f8829a4aSRandall Stewart 	 * gap
5391f8829a4aSRandall Stewart 	 */
5392b5c16493SMichael Tuexen 	SCTP_CALC_TSN_TO_GAP(gap, new_cum_tsn, asoc->mapping_array_base_tsn);
539377acdc25SRandall Stewart 	asoc->cumulative_tsn = new_cum_tsn;
53942afb3e84SRandall Stewart 	if (gap >= m_size) {
5395f8829a4aSRandall Stewart 		if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) {
539617205eccSRandall Stewart 			struct mbuf *oper;
539717205eccSRandall Stewart 
5398f8829a4aSRandall Stewart 			/*
5399f8829a4aSRandall Stewart 			 * out of range (of single byte chunks in the rwnd I
540017205eccSRandall Stewart 			 * give out). This must be an attacker.
5401f8829a4aSRandall Stewart 			 */
540217205eccSRandall Stewart 			*abort_flag = 1;
540317205eccSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
540417205eccSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
540517205eccSRandall Stewart 			if (oper) {
540617205eccSRandall Stewart 				struct sctp_paramhdr *ph;
540717205eccSRandall Stewart 				uint32_t *ippp;
540817205eccSRandall Stewart 
540917205eccSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
541017205eccSRandall Stewart 				    (sizeof(uint32_t) * 3);
541117205eccSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
541217205eccSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
541317205eccSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
541417205eccSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
541517205eccSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_33);
541617205eccSRandall Stewart 				ippp++;
541717205eccSRandall Stewart 				*ippp = asoc->highest_tsn_inside_map;
541817205eccSRandall Stewart 				ippp++;
541917205eccSRandall Stewart 				*ippp = new_cum_tsn;
542017205eccSRandall Stewart 			}
542117205eccSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_33;
542217205eccSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb,
5423ceaad40aSRandall Stewart 			    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
5424f8829a4aSRandall Stewart 			return;
5425f8829a4aSRandall Stewart 		}
5426207304d4SRandall Stewart 		SCTP_STAT_INCR(sctps_fwdtsn_map_over);
542777acdc25SRandall Stewart 
54282afb3e84SRandall Stewart 		memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
54292afb3e84SRandall Stewart 		asoc->mapping_array_base_tsn = new_cum_tsn + 1;
543077acdc25SRandall Stewart 		asoc->highest_tsn_inside_map = new_cum_tsn;
543177acdc25SRandall Stewart 
5432b5c16493SMichael Tuexen 		memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
5433830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = new_cum_tsn;
543477acdc25SRandall Stewart 
5435b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
5436f8829a4aSRandall Stewart 			sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
543780fefe0aSRandall Stewart 		}
54382afb3e84SRandall Stewart 	} else {
54392afb3e84SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
54402afb3e84SRandall Stewart 		for (i = 0; i <= gap; i++) {
54417898f408SRandall Stewart 			if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) &&
54427898f408SRandall Stewart 			    !SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, i)) {
54438933fa13SRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, i);
544420b07a4dSMichael Tuexen 				if (SCTP_TSN_GT(asoc->mapping_array_base_tsn + i, asoc->highest_tsn_inside_nr_map)) {
54457898f408SRandall Stewart 					asoc->highest_tsn_inside_nr_map = asoc->mapping_array_base_tsn + i;
5446b5c16493SMichael Tuexen 				}
5447b5c16493SMichael Tuexen 			}
5448b5c16493SMichael Tuexen 		}
5449f8829a4aSRandall Stewart 	}
5450f8829a4aSRandall Stewart 	/*************************************************************/
5451f8829a4aSRandall Stewart 	/* 2. Clear up re-assembly queue                             */
5452f8829a4aSRandall Stewart 	/*************************************************************/
5453f8829a4aSRandall Stewart 	/*
5454f8829a4aSRandall Stewart 	 * First service it if pd-api is up, just in case we can progress it
5455f8829a4aSRandall Stewart 	 * forward
5456f8829a4aSRandall Stewart 	 */
5457f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
5458f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
5459f8829a4aSRandall Stewart 	}
5460f8829a4aSRandall Stewart 	/* For each one on here see if we need to toss it */
5461f8829a4aSRandall Stewart 	/*
54624a9ef3f8SMichael Tuexen 	 * For now large messages held on the reasmqueue that are complete
54634a9ef3f8SMichael Tuexen 	 * will be tossed too. We could in theory do more work to spin
54644a9ef3f8SMichael Tuexen 	 * through and stop after dumping one msg aka seeing the start of a
54654a9ef3f8SMichael Tuexen 	 * new msg at the head, and call the delivery function... to see if
54664a9ef3f8SMichael Tuexen 	 * it can be delivered... But for now we just dump everything on the
54674a9ef3f8SMichael Tuexen 	 * queue.
5468f8829a4aSRandall Stewart 	 */
54694a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
547020b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(new_cum_tsn, chk->rec.data.TSN_seq)) {
5471f8829a4aSRandall Stewart 			/* It needs to be tossed */
5472f8829a4aSRandall Stewart 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
547320b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(chk->rec.data.TSN_seq, asoc->tsn_last_delivered)) {
54744a9ef3f8SMichael Tuexen 				asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
54754a9ef3f8SMichael Tuexen 				asoc->str_of_pdapi = chk->rec.data.stream_number;
54764a9ef3f8SMichael Tuexen 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
54774a9ef3f8SMichael Tuexen 				asoc->fragment_flags = chk->rec.data.rcv_flags;
5478f8829a4aSRandall Stewart 			}
5479f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue -= chk->send_size;
5480f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5481f8829a4aSRandall Stewart 
5482f8829a4aSRandall Stewart 			/* Clear up any stream problem */
548320b07a4dSMichael Tuexen 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) != SCTP_DATA_UNORDERED &&
548420b07a4dSMichael Tuexen 			    SCTP_SSN_GT(chk->rec.data.stream_seq, asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered)) {
5485f8829a4aSRandall Stewart 				/*
5486f8829a4aSRandall Stewart 				 * We must dump forward this streams
54874a9ef3f8SMichael Tuexen 				 * sequence number if the chunk is not
54884a9ef3f8SMichael Tuexen 				 * unordered that is being skipped. There is
54894a9ef3f8SMichael Tuexen 				 * a chance that if the peer does not
54904a9ef3f8SMichael Tuexen 				 * include the last fragment in its FWD-TSN
54914a9ef3f8SMichael Tuexen 				 * we WILL have a problem here since you
54924a9ef3f8SMichael Tuexen 				 * would have a partial chunk in queue that
54934a9ef3f8SMichael Tuexen 				 * may not be deliverable. Also if a Partial
54944a9ef3f8SMichael Tuexen 				 * delivery API as started the user may get
54954a9ef3f8SMichael Tuexen 				 * a partial chunk. The next read returning
54964a9ef3f8SMichael Tuexen 				 * a new chunk... really ugly but I see no
54974a9ef3f8SMichael Tuexen 				 * way around it! Maybe a notify??
5498f8829a4aSRandall Stewart 				 */
54994a9ef3f8SMichael Tuexen 				asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered = chk->rec.data.stream_seq;
5500f8829a4aSRandall Stewart 			}
5501f8829a4aSRandall Stewart 			if (chk->data) {
5502f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
5503f8829a4aSRandall Stewart 				chk->data = NULL;
5504f8829a4aSRandall Stewart 			}
5505689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
5506f8829a4aSRandall Stewart 		} else {
5507f8829a4aSRandall Stewart 			/*
55084a9ef3f8SMichael Tuexen 			 * Ok we have gone beyond the end of the fwd-tsn's
55094a9ef3f8SMichael Tuexen 			 * mark.
5510f8829a4aSRandall Stewart 			 */
5511f8829a4aSRandall Stewart 			break;
5512f8829a4aSRandall Stewart 		}
5513f8829a4aSRandall Stewart 	}
55148933fa13SRandall Stewart 	/*******************************************************/
55158933fa13SRandall Stewart 	/* 3. Update the PR-stream re-ordering queues and fix  */
55168933fa13SRandall Stewart 	/* delivery issues as needed.                       */
55178933fa13SRandall Stewart 	/*******************************************************/
5518f8829a4aSRandall Stewart 	fwd_sz -= sizeof(*fwd);
5519671d309cSRandall Stewart 	if (m && fwd_sz) {
5520f8829a4aSRandall Stewart 		/* New method. */
5521d61a0ae0SRandall Stewart 		unsigned int num_str;
5522671d309cSRandall Stewart 		struct sctp_strseq *stseq, strseqbuf;
5523671d309cSRandall Stewart 
5524671d309cSRandall Stewart 		offset += sizeof(*fwd);
5525f8829a4aSRandall Stewart 
55268933fa13SRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
5527f8829a4aSRandall Stewart 		num_str = fwd_sz / sizeof(struct sctp_strseq);
5528f8829a4aSRandall Stewart 		for (i = 0; i < num_str; i++) {
5529f8829a4aSRandall Stewart 			uint16_t st;
5530f8829a4aSRandall Stewart 
5531671d309cSRandall Stewart 			stseq = (struct sctp_strseq *)sctp_m_getptr(m, offset,
5532671d309cSRandall Stewart 			    sizeof(struct sctp_strseq),
5533671d309cSRandall Stewart 			    (uint8_t *) & strseqbuf);
5534671d309cSRandall Stewart 			offset += sizeof(struct sctp_strseq);
55352afb3e84SRandall Stewart 			if (stseq == NULL) {
5536671d309cSRandall Stewart 				break;
55372afb3e84SRandall Stewart 			}
5538f8829a4aSRandall Stewart 			/* Convert */
5539851b7298SRandall Stewart 			st = ntohs(stseq->stream);
5540851b7298SRandall Stewart 			stseq->stream = st;
5541851b7298SRandall Stewart 			st = ntohs(stseq->sequence);
5542851b7298SRandall Stewart 			stseq->sequence = st;
55438933fa13SRandall Stewart 
5544f8829a4aSRandall Stewart 			/* now process */
55458933fa13SRandall Stewart 
55468933fa13SRandall Stewart 			/*
55478933fa13SRandall Stewart 			 * Ok we now look for the stream/seq on the read
55488933fa13SRandall Stewart 			 * queue where its not all delivered. If we find it
55498933fa13SRandall Stewart 			 * we transmute the read entry into a PDI_ABORTED.
55508933fa13SRandall Stewart 			 */
5551851b7298SRandall Stewart 			if (stseq->stream >= asoc->streamincnt) {
55522afb3e84SRandall Stewart 				/* screwed up streams, stop!  */
55532afb3e84SRandall Stewart 				break;
5554f8829a4aSRandall Stewart 			}
55558933fa13SRandall Stewart 			if ((asoc->str_of_pdapi == stseq->stream) &&
55568933fa13SRandall Stewart 			    (asoc->ssn_of_pdapi == stseq->sequence)) {
55578933fa13SRandall Stewart 				/*
55588933fa13SRandall Stewart 				 * If this is the one we were partially
55598933fa13SRandall Stewart 				 * delivering now then we no longer are.
55608933fa13SRandall Stewart 				 * Note this will change with the reassembly
55618933fa13SRandall Stewart 				 * re-write.
55628933fa13SRandall Stewart 				 */
55638933fa13SRandall Stewart 				asoc->fragmented_delivery_inprogress = 0;
55648933fa13SRandall Stewart 			}
55658933fa13SRandall Stewart 			sctp_flush_reassm_for_str_seq(stcb, asoc, stseq->stream, stseq->sequence);
55668933fa13SRandall Stewart 			TAILQ_FOREACH(ctl, &stcb->sctp_ep->read_queue, next) {
55678933fa13SRandall Stewart 				if ((ctl->sinfo_stream == stseq->stream) &&
55688933fa13SRandall Stewart 				    (ctl->sinfo_ssn == stseq->sequence)) {
55698933fa13SRandall Stewart 					str_seq = (stseq->stream << 16) | stseq->sequence;
55708933fa13SRandall Stewart 					ctl->end_added = 1;
55718933fa13SRandall Stewart 					ctl->pdapi_aborted = 1;
55728933fa13SRandall Stewart 					sv = stcb->asoc.control_pdapi;
55738933fa13SRandall Stewart 					stcb->asoc.control_pdapi = ctl;
5574810ec536SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5575810ec536SMichael Tuexen 					    stcb,
55768933fa13SRandall Stewart 					    SCTP_PARTIAL_DELIVERY_ABORTED,
5577810ec536SMichael Tuexen 					    (void *)&str_seq,
5578810ec536SMichael Tuexen 					    SCTP_SO_NOT_LOCKED);
55798933fa13SRandall Stewart 					stcb->asoc.control_pdapi = sv;
55808933fa13SRandall Stewart 					break;
55818933fa13SRandall Stewart 				} else if ((ctl->sinfo_stream == stseq->stream) &&
558220b07a4dSMichael Tuexen 				    SCTP_SSN_GT(ctl->sinfo_ssn, stseq->sequence)) {
55838933fa13SRandall Stewart 					/* We are past our victim SSN */
55848933fa13SRandall Stewart 					break;
55858933fa13SRandall Stewart 				}
55868933fa13SRandall Stewart 			}
5587851b7298SRandall Stewart 			strm = &asoc->strmin[stseq->stream];
558820b07a4dSMichael Tuexen 			if (SCTP_SSN_GT(stseq->sequence, strm->last_sequence_delivered)) {
5589f8829a4aSRandall Stewart 				/* Update the sequence number */
559020b07a4dSMichael Tuexen 				strm->last_sequence_delivered = stseq->sequence;
5591f8829a4aSRandall Stewart 			}
5592f8829a4aSRandall Stewart 			/* now kick the stream the new way */
559304ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
5594f8829a4aSRandall Stewart 			sctp_kick_prsctp_reorder_queue(stcb, strm);
5595f8829a4aSRandall Stewart 		}
55968933fa13SRandall Stewart 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5597f8829a4aSRandall Stewart 	}
55987898f408SRandall Stewart 	/*
55997898f408SRandall Stewart 	 * Now slide thing forward.
56007898f408SRandall Stewart 	 */
56017898f408SRandall Stewart 	sctp_slide_mapping_arrays(stcb);
56027898f408SRandall Stewart 
560324f52bbdSMichael Tuexen 	if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
5604139bc87fSRandall Stewart 		/* now lets kick out and check for more fragmented delivery */
560504ee05e8SRandall Stewart 		/* sa_ignore NO_NULL_CHK */
5606139bc87fSRandall Stewart 		sctp_deliver_reasm_check(stcb, &stcb->asoc);
5607139bc87fSRandall Stewart 	}
5608f8829a4aSRandall Stewart }
5609