xref: /freebsd/sys/netinet/sctp_indata.c (revision 20cc2188f3ae034a542c923d224c14b6b188c84a)
1f8829a4aSRandall Stewart /*-
2b1006367SRandall Stewart  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3807aad63SMichael Tuexen  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4807aad63SMichael Tuexen  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
5f8829a4aSRandall Stewart  *
6f8829a4aSRandall Stewart  * Redistribution and use in source and binary forms, with or without
7f8829a4aSRandall Stewart  * modification, are permitted provided that the following conditions are met:
8f8829a4aSRandall Stewart  *
9f8829a4aSRandall Stewart  * a) Redistributions of source code must retain the above copyright notice,
10f8829a4aSRandall Stewart  *    this list of conditions and the following disclaimer.
11f8829a4aSRandall Stewart  *
12f8829a4aSRandall Stewart  * b) Redistributions in binary form must reproduce the above copyright
13f8829a4aSRandall Stewart  *    notice, this list of conditions and the following disclaimer in
14f8829a4aSRandall Stewart  *    the documentation and/or other materials provided with the distribution.
15f8829a4aSRandall Stewart  *
16f8829a4aSRandall Stewart  * c) Neither the name of Cisco Systems, Inc. nor the names of its
17f8829a4aSRandall Stewart  *    contributors may be used to endorse or promote products derived
18f8829a4aSRandall Stewart  *    from this software without specific prior written permission.
19f8829a4aSRandall Stewart  *
20f8829a4aSRandall Stewart  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21f8829a4aSRandall Stewart  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22f8829a4aSRandall Stewart  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23f8829a4aSRandall Stewart  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24f8829a4aSRandall Stewart  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25f8829a4aSRandall Stewart  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26f8829a4aSRandall Stewart  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27f8829a4aSRandall Stewart  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28f8829a4aSRandall Stewart  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29f8829a4aSRandall Stewart  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30f8829a4aSRandall Stewart  * THE POSSIBILITY OF SUCH DAMAGE.
31f8829a4aSRandall Stewart  */
32f8829a4aSRandall Stewart 
33f8829a4aSRandall Stewart #include <sys/cdefs.h>
34f8829a4aSRandall Stewart __FBSDID("$FreeBSD$");
35f8829a4aSRandall Stewart 
36f8829a4aSRandall Stewart #include <netinet/sctp_os.h>
37f8829a4aSRandall Stewart #include <netinet/sctp_var.h>
3842551e99SRandall Stewart #include <netinet/sctp_sysctl.h>
39f8829a4aSRandall Stewart #include <netinet/sctp_pcb.h>
40f8829a4aSRandall Stewart #include <netinet/sctp_header.h>
41f8829a4aSRandall Stewart #include <netinet/sctputil.h>
42f8829a4aSRandall Stewart #include <netinet/sctp_output.h>
43f8829a4aSRandall Stewart #include <netinet/sctp_input.h>
44f8829a4aSRandall Stewart #include <netinet/sctp_indata.h>
45f8829a4aSRandall Stewart #include <netinet/sctp_uio.h>
46f8829a4aSRandall Stewart #include <netinet/sctp_timer.h>
47f8829a4aSRandall Stewart 
48d50c1d79SRandall Stewart 
49f8829a4aSRandall Stewart /*
50f8829a4aSRandall Stewart  * NOTES: On the outbound side of things I need to check the sack timer to
51f8829a4aSRandall Stewart  * see if I should generate a sack into the chunk queue (if I have data to
52f8829a4aSRandall Stewart  * send that is and will be sending it .. for bundling.
53f8829a4aSRandall Stewart  *
54f8829a4aSRandall Stewart  * The callback in sctp_usrreq.c will get called when the socket is read from.
55f8829a4aSRandall Stewart  * This will cause sctp_service_queues() to get called on the top entry in
56f8829a4aSRandall Stewart  * the list.
57f8829a4aSRandall Stewart  */
58f8829a4aSRandall Stewart 
5972fb6fdbSRandall Stewart void
60f8829a4aSRandall Stewart sctp_set_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
61f8829a4aSRandall Stewart {
62b3f1ea41SRandall Stewart 	asoc->my_rwnd = sctp_calc_rwnd(stcb, asoc);
63f8829a4aSRandall Stewart }
64f8829a4aSRandall Stewart 
65f8829a4aSRandall Stewart /* Calculate what the rwnd would be */
6672fb6fdbSRandall Stewart uint32_t
67f8829a4aSRandall Stewart sctp_calc_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
68f8829a4aSRandall Stewart {
69b3f1ea41SRandall Stewart 	uint32_t calc = 0;
70f8829a4aSRandall Stewart 
71f8829a4aSRandall Stewart 	/*
72f8829a4aSRandall Stewart 	 * This is really set wrong with respect to a 1-2-m socket. Since
73f8829a4aSRandall Stewart 	 * the sb_cc is the count that everyone as put up. When we re-write
74f8829a4aSRandall Stewart 	 * sctp_soreceive then we will fix this so that ONLY this
75f8829a4aSRandall Stewart 	 * associations data is taken into account.
76f8829a4aSRandall Stewart 	 */
77f8829a4aSRandall Stewart 	if (stcb->sctp_socket == NULL)
78f8829a4aSRandall Stewart 		return (calc);
79f8829a4aSRandall Stewart 
80f8829a4aSRandall Stewart 	if (stcb->asoc.sb_cc == 0 &&
81f8829a4aSRandall Stewart 	    asoc->size_on_reasm_queue == 0 &&
82f8829a4aSRandall Stewart 	    asoc->size_on_all_streams == 0) {
83f8829a4aSRandall Stewart 		/* Full rwnd granted */
84b3f1ea41SRandall Stewart 		calc = max(SCTP_SB_LIMIT_RCV(stcb->sctp_socket), SCTP_MINIMAL_RWND);
85f8829a4aSRandall Stewart 		return (calc);
86f8829a4aSRandall Stewart 	}
87f8829a4aSRandall Stewart 	/* get actual space */
88f8829a4aSRandall Stewart 	calc = (uint32_t) sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv);
89f8829a4aSRandall Stewart 
90f8829a4aSRandall Stewart 	/*
91f8829a4aSRandall Stewart 	 * take out what has NOT been put on socket queue and we yet hold
92f8829a4aSRandall Stewart 	 * for putting up.
93f8829a4aSRandall Stewart 	 */
9444fbe462SRandall Stewart 	calc = sctp_sbspace_sub(calc, (uint32_t) (asoc->size_on_reasm_queue +
9544fbe462SRandall Stewart 	    asoc->cnt_on_reasm_queue * MSIZE));
9644fbe462SRandall Stewart 	calc = sctp_sbspace_sub(calc, (uint32_t) (asoc->size_on_all_streams +
9744fbe462SRandall Stewart 	    asoc->cnt_on_all_streams * MSIZE));
98f8829a4aSRandall Stewart 
99f8829a4aSRandall Stewart 	if (calc == 0) {
100f8829a4aSRandall Stewart 		/* out of space */
101f8829a4aSRandall Stewart 		return (calc);
102f8829a4aSRandall Stewart 	}
103f8829a4aSRandall Stewart 	/* what is the overhead of all these rwnd's */
1042afb3e84SRandall Stewart 	calc = sctp_sbspace_sub(calc, stcb->asoc.my_rwnd_control_len);
105b3f1ea41SRandall Stewart 	/*
106b3f1ea41SRandall Stewart 	 * If the window gets too small due to ctrl-stuff, reduce it to 1,
107b3f1ea41SRandall Stewart 	 * even it is 0. SWS engaged
108f8829a4aSRandall Stewart 	 */
109b3f1ea41SRandall Stewart 	if (calc < stcb->asoc.my_rwnd_control_len) {
110b3f1ea41SRandall Stewart 		calc = 1;
1112afb3e84SRandall Stewart 	}
112b3f1ea41SRandall Stewart 	return (calc);
113f8829a4aSRandall Stewart }
114f8829a4aSRandall Stewart 
115f8829a4aSRandall Stewart 
116f8829a4aSRandall Stewart 
117f8829a4aSRandall Stewart /*
118f8829a4aSRandall Stewart  * Build out our readq entry based on the incoming packet.
119f8829a4aSRandall Stewart  */
120f8829a4aSRandall Stewart struct sctp_queued_to_read *
121f8829a4aSRandall Stewart sctp_build_readq_entry(struct sctp_tcb *stcb,
122f8829a4aSRandall Stewart     struct sctp_nets *net,
123f8829a4aSRandall Stewart     uint32_t tsn, uint32_t ppid,
124f8829a4aSRandall Stewart     uint32_t context, uint16_t stream_no,
125f8829a4aSRandall Stewart     uint16_t stream_seq, uint8_t flags,
126f8829a4aSRandall Stewart     struct mbuf *dm)
127f8829a4aSRandall Stewart {
128f8829a4aSRandall Stewart 	struct sctp_queued_to_read *read_queue_e = NULL;
129f8829a4aSRandall Stewart 
130f8829a4aSRandall Stewart 	sctp_alloc_a_readq(stcb, read_queue_e);
131f8829a4aSRandall Stewart 	if (read_queue_e == NULL) {
132f8829a4aSRandall Stewart 		goto failed_build;
133f8829a4aSRandall Stewart 	}
134f8829a4aSRandall Stewart 	read_queue_e->sinfo_stream = stream_no;
135f8829a4aSRandall Stewart 	read_queue_e->sinfo_ssn = stream_seq;
136f8829a4aSRandall Stewart 	read_queue_e->sinfo_flags = (flags << 8);
137f8829a4aSRandall Stewart 	read_queue_e->sinfo_ppid = ppid;
1387215cc1bSMichael Tuexen 	read_queue_e->sinfo_context = context;
139f8829a4aSRandall Stewart 	read_queue_e->sinfo_timetolive = 0;
140f8829a4aSRandall Stewart 	read_queue_e->sinfo_tsn = tsn;
141f8829a4aSRandall Stewart 	read_queue_e->sinfo_cumtsn = tsn;
142f8829a4aSRandall Stewart 	read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
143f8829a4aSRandall Stewart 	read_queue_e->whoFrom = net;
144f8829a4aSRandall Stewart 	read_queue_e->length = 0;
145f8829a4aSRandall Stewart 	atomic_add_int(&net->ref_count, 1);
146f8829a4aSRandall Stewart 	read_queue_e->data = dm;
147139bc87fSRandall Stewart 	read_queue_e->spec_flags = 0;
148f8829a4aSRandall Stewart 	read_queue_e->tail_mbuf = NULL;
14917205eccSRandall Stewart 	read_queue_e->aux_data = NULL;
150f8829a4aSRandall Stewart 	read_queue_e->stcb = stcb;
151f8829a4aSRandall Stewart 	read_queue_e->port_from = stcb->rport;
152f8829a4aSRandall Stewart 	read_queue_e->do_not_ref_stcb = 0;
153f8829a4aSRandall Stewart 	read_queue_e->end_added = 0;
1549a6142d8SRandall Stewart 	read_queue_e->some_taken = 0;
15503b0b021SRandall Stewart 	read_queue_e->pdapi_aborted = 0;
156f8829a4aSRandall Stewart failed_build:
157f8829a4aSRandall Stewart 	return (read_queue_e);
158f8829a4aSRandall Stewart }
159f8829a4aSRandall Stewart 
160f8829a4aSRandall Stewart 
161f8829a4aSRandall Stewart /*
162f8829a4aSRandall Stewart  * Build out our readq entry based on the incoming packet.
163f8829a4aSRandall Stewart  */
164f8829a4aSRandall Stewart static struct sctp_queued_to_read *
165f8829a4aSRandall Stewart sctp_build_readq_entry_chk(struct sctp_tcb *stcb,
166f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk)
167f8829a4aSRandall Stewart {
168f8829a4aSRandall Stewart 	struct sctp_queued_to_read *read_queue_e = NULL;
169f8829a4aSRandall Stewart 
170f8829a4aSRandall Stewart 	sctp_alloc_a_readq(stcb, read_queue_e);
171f8829a4aSRandall Stewart 	if (read_queue_e == NULL) {
172f8829a4aSRandall Stewart 		goto failed_build;
173f8829a4aSRandall Stewart 	}
174f8829a4aSRandall Stewart 	read_queue_e->sinfo_stream = chk->rec.data.stream_number;
175f8829a4aSRandall Stewart 	read_queue_e->sinfo_ssn = chk->rec.data.stream_seq;
176f8829a4aSRandall Stewart 	read_queue_e->sinfo_flags = (chk->rec.data.rcv_flags << 8);
177f8829a4aSRandall Stewart 	read_queue_e->sinfo_ppid = chk->rec.data.payloadtype;
178f8829a4aSRandall Stewart 	read_queue_e->sinfo_context = stcb->asoc.context;
179f8829a4aSRandall Stewart 	read_queue_e->sinfo_timetolive = 0;
180f8829a4aSRandall Stewart 	read_queue_e->sinfo_tsn = chk->rec.data.TSN_seq;
181f8829a4aSRandall Stewart 	read_queue_e->sinfo_cumtsn = chk->rec.data.TSN_seq;
182f8829a4aSRandall Stewart 	read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
183f8829a4aSRandall Stewart 	read_queue_e->whoFrom = chk->whoTo;
18417205eccSRandall Stewart 	read_queue_e->aux_data = NULL;
185f8829a4aSRandall Stewart 	read_queue_e->length = 0;
186f8829a4aSRandall Stewart 	atomic_add_int(&chk->whoTo->ref_count, 1);
187f8829a4aSRandall Stewart 	read_queue_e->data = chk->data;
188f8829a4aSRandall Stewart 	read_queue_e->tail_mbuf = NULL;
189f8829a4aSRandall Stewart 	read_queue_e->stcb = stcb;
190f8829a4aSRandall Stewart 	read_queue_e->port_from = stcb->rport;
191139bc87fSRandall Stewart 	read_queue_e->spec_flags = 0;
192f8829a4aSRandall Stewart 	read_queue_e->do_not_ref_stcb = 0;
193f8829a4aSRandall Stewart 	read_queue_e->end_added = 0;
1949a6142d8SRandall Stewart 	read_queue_e->some_taken = 0;
19503b0b021SRandall Stewart 	read_queue_e->pdapi_aborted = 0;
196f8829a4aSRandall Stewart failed_build:
197f8829a4aSRandall Stewart 	return (read_queue_e);
198f8829a4aSRandall Stewart }
199f8829a4aSRandall Stewart 
200f8829a4aSRandall Stewart 
201f8829a4aSRandall Stewart struct mbuf *
202e2e7c62eSMichael Tuexen sctp_build_ctl_nchunk(struct sctp_inpcb *inp, struct sctp_sndrcvinfo *sinfo)
203f8829a4aSRandall Stewart {
204e2e7c62eSMichael Tuexen 	struct sctp_extrcvinfo *seinfo;
205f8829a4aSRandall Stewart 	struct sctp_sndrcvinfo *outinfo;
206e2e7c62eSMichael Tuexen 	struct sctp_rcvinfo *rcvinfo;
207e2e7c62eSMichael Tuexen 	struct sctp_nxtinfo *nxtinfo;
208f8829a4aSRandall Stewart 	struct cmsghdr *cmh;
209f8829a4aSRandall Stewart 	struct mbuf *ret;
210f8829a4aSRandall Stewart 	int len;
211e2e7c62eSMichael Tuexen 	int use_extended;
212e2e7c62eSMichael Tuexen 	int provide_nxt;
213f8829a4aSRandall Stewart 
214e2e7c62eSMichael Tuexen 	if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) &&
215e2e7c62eSMichael Tuexen 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
216e2e7c62eSMichael Tuexen 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) {
217e2e7c62eSMichael Tuexen 		/* user does not want any ancillary data */
218f8829a4aSRandall Stewart 		return (NULL);
219f8829a4aSRandall Stewart 	}
220e2e7c62eSMichael Tuexen 	len = 0;
221e2e7c62eSMichael Tuexen 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO)) {
222e2e7c62eSMichael Tuexen 		len += CMSG_SPACE(sizeof(struct sctp_rcvinfo));
223e2e7c62eSMichael Tuexen 	}
224e2e7c62eSMichael Tuexen 	seinfo = (struct sctp_extrcvinfo *)sinfo;
225e2e7c62eSMichael Tuexen 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO) &&
226e2e7c62eSMichael Tuexen 	    (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_AVAIL)) {
227e2e7c62eSMichael Tuexen 		provide_nxt = 1;
228e2e7c62eSMichael Tuexen 		len += CMSG_SPACE(sizeof(struct sctp_rcvinfo));
229e2e7c62eSMichael Tuexen 	} else {
230e2e7c62eSMichael Tuexen 		provide_nxt = 0;
231e2e7c62eSMichael Tuexen 	}
232e2e7c62eSMichael Tuexen 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
233f8829a4aSRandall Stewart 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
234f8829a4aSRandall Stewart 			use_extended = 1;
235e2e7c62eSMichael Tuexen 			len += CMSG_SPACE(sizeof(struct sctp_extrcvinfo));
236f8829a4aSRandall Stewart 		} else {
237e2e7c62eSMichael Tuexen 			use_extended = 0;
238e2e7c62eSMichael Tuexen 			len += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
239e2e7c62eSMichael Tuexen 		}
240e2e7c62eSMichael Tuexen 	} else {
241e2e7c62eSMichael Tuexen 		use_extended = 0;
242f8829a4aSRandall Stewart 	}
243f8829a4aSRandall Stewart 
244e2e7c62eSMichael Tuexen 	ret = sctp_get_mbuf_for_msg(len, 0, M_DONTWAIT, 1, MT_DATA);
245f8829a4aSRandall Stewart 	if (ret == NULL) {
246f8829a4aSRandall Stewart 		/* No space */
247f8829a4aSRandall Stewart 		return (ret);
248f8829a4aSRandall Stewart 	}
249e2e7c62eSMichael Tuexen 	SCTP_BUF_LEN(ret) = 0;
250e2e7c62eSMichael Tuexen 
251f8829a4aSRandall Stewart 	/* We need a CMSG header followed by the struct */
252f8829a4aSRandall Stewart 	cmh = mtod(ret, struct cmsghdr *);
253e2e7c62eSMichael Tuexen 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO)) {
254f8829a4aSRandall Stewart 		cmh->cmsg_level = IPPROTO_SCTP;
255e2e7c62eSMichael Tuexen 		cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_rcvinfo));
256e2e7c62eSMichael Tuexen 		cmh->cmsg_type = SCTP_RCVINFO;
257e2e7c62eSMichael Tuexen 		rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmh);
258e2e7c62eSMichael Tuexen 		rcvinfo->rcv_sid = sinfo->sinfo_stream;
259e2e7c62eSMichael Tuexen 		rcvinfo->rcv_ssn = sinfo->sinfo_ssn;
260e2e7c62eSMichael Tuexen 		rcvinfo->rcv_flags = sinfo->sinfo_flags;
261e2e7c62eSMichael Tuexen 		rcvinfo->rcv_ppid = sinfo->sinfo_ppid;
262e2e7c62eSMichael Tuexen 		rcvinfo->rcv_tsn = sinfo->sinfo_tsn;
263e2e7c62eSMichael Tuexen 		rcvinfo->rcv_cumtsn = sinfo->sinfo_cumtsn;
264e2e7c62eSMichael Tuexen 		rcvinfo->rcv_context = sinfo->sinfo_context;
265e2e7c62eSMichael Tuexen 		rcvinfo->rcv_assoc_id = sinfo->sinfo_assoc_id;
266e2e7c62eSMichael Tuexen 		cmh = (struct cmsghdr *)((caddr_t)cmh + CMSG_SPACE(sizeof(struct sctp_rcvinfo)));
267e2e7c62eSMichael Tuexen 		SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_rcvinfo));
268f8829a4aSRandall Stewart 	}
269e2e7c62eSMichael Tuexen 	if (provide_nxt) {
270e2e7c62eSMichael Tuexen 		cmh->cmsg_level = IPPROTO_SCTP;
271e2e7c62eSMichael Tuexen 		cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_nxtinfo));
272e2e7c62eSMichael Tuexen 		cmh->cmsg_type = SCTP_NXTINFO;
273e2e7c62eSMichael Tuexen 		nxtinfo = (struct sctp_nxtinfo *)CMSG_DATA(cmh);
274e2e7c62eSMichael Tuexen 		nxtinfo->nxt_sid = seinfo->sreinfo_next_stream;
275e2e7c62eSMichael Tuexen 		nxtinfo->nxt_flags = 0;
276e2e7c62eSMichael Tuexen 		if (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_IS_UNORDERED) {
277e2e7c62eSMichael Tuexen 			nxtinfo->nxt_flags |= SCTP_UNORDERED;
278e2e7c62eSMichael Tuexen 		}
279e2e7c62eSMichael Tuexen 		if (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_IS_NOTIFICATION) {
280e2e7c62eSMichael Tuexen 			nxtinfo->nxt_flags |= SCTP_NOTIFICATION;
281e2e7c62eSMichael Tuexen 		}
282e2e7c62eSMichael Tuexen 		if (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_ISCOMPLETE) {
283e2e7c62eSMichael Tuexen 			nxtinfo->nxt_flags |= SCTP_COMPLETE;
284e2e7c62eSMichael Tuexen 		}
285e2e7c62eSMichael Tuexen 		nxtinfo->nxt_ppid = seinfo->sreinfo_next_ppid;
286e2e7c62eSMichael Tuexen 		nxtinfo->nxt_length = seinfo->sreinfo_next_length;
287e2e7c62eSMichael Tuexen 		nxtinfo->nxt_assoc_id = seinfo->sreinfo_next_aid;
288e2e7c62eSMichael Tuexen 		cmh = (struct cmsghdr *)((caddr_t)cmh + CMSG_SPACE(sizeof(struct sctp_nxtinfo)));
289e2e7c62eSMichael Tuexen 		SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_nxtinfo));
290e2e7c62eSMichael Tuexen 	}
291e2e7c62eSMichael Tuexen 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
292e2e7c62eSMichael Tuexen 		cmh->cmsg_level = IPPROTO_SCTP;
293e2e7c62eSMichael Tuexen 		outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
294e2e7c62eSMichael Tuexen 		if (use_extended) {
295e2e7c62eSMichael Tuexen 			cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
296e2e7c62eSMichael Tuexen 			cmh->cmsg_type = SCTP_EXTRCV;
297e2e7c62eSMichael Tuexen 			memcpy(outinfo, sinfo, sizeof(struct sctp_extrcvinfo));
298e2e7c62eSMichael Tuexen 			SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_extrcvinfo));
299e2e7c62eSMichael Tuexen 		} else {
300e2e7c62eSMichael Tuexen 			cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
301e2e7c62eSMichael Tuexen 			cmh->cmsg_type = SCTP_SNDRCV;
302e2e7c62eSMichael Tuexen 			*outinfo = *sinfo;
303e2e7c62eSMichael Tuexen 			SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
304e2e7c62eSMichael Tuexen 		}
305e2e7c62eSMichael Tuexen 	}
306f8829a4aSRandall Stewart 	return (ret);
307f8829a4aSRandall Stewart }
308f8829a4aSRandall Stewart 
309139bc87fSRandall Stewart 
31077acdc25SRandall Stewart static void
31177acdc25SRandall Stewart sctp_mark_non_revokable(struct sctp_association *asoc, uint32_t tsn)
31277acdc25SRandall Stewart {
3139b2e0767SRandall Stewart 	uint32_t gap, i, cumackp1;
31477acdc25SRandall Stewart 	int fnd = 0;
31577acdc25SRandall Stewart 
31677acdc25SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
31777acdc25SRandall Stewart 		return;
31877acdc25SRandall Stewart 	}
3199b2e0767SRandall Stewart 	cumackp1 = asoc->cumulative_tsn + 1;
32020b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cumackp1, tsn)) {
3219b2e0767SRandall Stewart 		/*
3229b2e0767SRandall Stewart 		 * this tsn is behind the cum ack and thus we don't need to
3239b2e0767SRandall Stewart 		 * worry about it being moved from one to the other.
3249b2e0767SRandall Stewart 		 */
3259b2e0767SRandall Stewart 		return;
3269b2e0767SRandall Stewart 	}
32777acdc25SRandall Stewart 	SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
32877acdc25SRandall Stewart 	if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
329cd3fd531SMichael Tuexen 		SCTP_PRINTF("gap:%x tsn:%x\n", gap, tsn);
33077acdc25SRandall Stewart 		sctp_print_mapping_array(asoc);
331b5c16493SMichael Tuexen #ifdef INVARIANTS
33277acdc25SRandall Stewart 		panic("Things are really messed up now!!");
33377acdc25SRandall Stewart #endif
334b5c16493SMichael Tuexen 	}
33577acdc25SRandall Stewart 	SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
33677acdc25SRandall Stewart 	SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
33720b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
33877acdc25SRandall Stewart 		asoc->highest_tsn_inside_nr_map = tsn;
33977acdc25SRandall Stewart 	}
34077acdc25SRandall Stewart 	if (tsn == asoc->highest_tsn_inside_map) {
34177acdc25SRandall Stewart 		/* We must back down to see what the new highest is */
34220b07a4dSMichael Tuexen 		for (i = tsn - 1; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) {
34377acdc25SRandall Stewart 			SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn);
34477acdc25SRandall Stewart 			if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
34577acdc25SRandall Stewart 				asoc->highest_tsn_inside_map = i;
34677acdc25SRandall Stewart 				fnd = 1;
34777acdc25SRandall Stewart 				break;
34877acdc25SRandall Stewart 			}
34977acdc25SRandall Stewart 		}
35077acdc25SRandall Stewart 		if (!fnd) {
35177acdc25SRandall Stewart 			asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1;
35277acdc25SRandall Stewart 		}
35377acdc25SRandall Stewart 	}
35477acdc25SRandall Stewart }
35577acdc25SRandall Stewart 
35617205eccSRandall Stewart 
357f8829a4aSRandall Stewart /*
358f8829a4aSRandall Stewart  * We are delivering currently from the reassembly queue. We must continue to
359f8829a4aSRandall Stewart  * deliver until we either: 1) run out of space. 2) run out of sequential
360f8829a4aSRandall Stewart  * TSN's 3) hit the SCTP_DATA_LAST_FRAG flag.
361f8829a4aSRandall Stewart  */
362f8829a4aSRandall Stewart static void
363f8829a4aSRandall Stewart sctp_service_reassembly(struct sctp_tcb *stcb, struct sctp_association *asoc)
364f8829a4aSRandall Stewart {
3654a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
366f8829a4aSRandall Stewart 	uint16_t nxt_todel;
367f8829a4aSRandall Stewart 	uint16_t stream_no;
368f8829a4aSRandall Stewart 	int end = 0;
369f8829a4aSRandall Stewart 	int cntDel;
3704a9ef3f8SMichael Tuexen 	struct sctp_queued_to_read *control, *ctl, *nctl;
371f8829a4aSRandall Stewart 
372ad81507eSRandall Stewart 	if (stcb == NULL)
373ad81507eSRandall Stewart 		return;
374ad81507eSRandall Stewart 
375f42a358aSRandall Stewart 	cntDel = stream_no = 0;
376ad81507eSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
377851b7298SRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
378ad81507eSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
379851b7298SRandall Stewart 		/* socket above is long gone or going.. */
380851b7298SRandall Stewart abandon:
381f8829a4aSRandall Stewart 		asoc->fragmented_delivery_inprogress = 0;
3824a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
383f8829a4aSRandall Stewart 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
384f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue -= chk->send_size;
385f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_reasm_queue);
386f8829a4aSRandall Stewart 			/*
387f8829a4aSRandall Stewart 			 * Lose the data pointer, since its in the socket
388f8829a4aSRandall Stewart 			 * buffer
389f8829a4aSRandall Stewart 			 */
390f8829a4aSRandall Stewart 			if (chk->data) {
391f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
392f8829a4aSRandall Stewart 				chk->data = NULL;
393f8829a4aSRandall Stewart 			}
394f8829a4aSRandall Stewart 			/* Now free the address and data */
395689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3963c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
397f8829a4aSRandall Stewart 		}
398f8829a4aSRandall Stewart 		return;
399f8829a4aSRandall Stewart 	}
400f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
4014a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
402f8829a4aSRandall Stewart 		if (chk->rec.data.TSN_seq != (asoc->tsn_last_delivered + 1)) {
403f8829a4aSRandall Stewart 			/* Can't deliver more :< */
404f8829a4aSRandall Stewart 			return;
405f8829a4aSRandall Stewart 		}
406f8829a4aSRandall Stewart 		stream_no = chk->rec.data.stream_number;
407f8829a4aSRandall Stewart 		nxt_todel = asoc->strmin[stream_no].last_sequence_delivered + 1;
408f8829a4aSRandall Stewart 		if (nxt_todel != chk->rec.data.stream_seq &&
409f8829a4aSRandall Stewart 		    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
410f8829a4aSRandall Stewart 			/*
411f8829a4aSRandall Stewart 			 * Not the next sequence to deliver in its stream OR
412f8829a4aSRandall Stewart 			 * unordered
413f8829a4aSRandall Stewart 			 */
414f8829a4aSRandall Stewart 			return;
415f8829a4aSRandall Stewart 		}
416f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
417f8829a4aSRandall Stewart 
418f8829a4aSRandall Stewart 			control = sctp_build_readq_entry_chk(stcb, chk);
419f8829a4aSRandall Stewart 			if (control == NULL) {
420f8829a4aSRandall Stewart 				/* out of memory? */
421f8829a4aSRandall Stewart 				return;
422f8829a4aSRandall Stewart 			}
423f8829a4aSRandall Stewart 			/* save it off for our future deliveries */
424f8829a4aSRandall Stewart 			stcb->asoc.control_pdapi = control;
425f8829a4aSRandall Stewart 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
426f8829a4aSRandall Stewart 				end = 1;
427f8829a4aSRandall Stewart 			else
428f8829a4aSRandall Stewart 				end = 0;
429b5c16493SMichael Tuexen 			sctp_mark_non_revokable(asoc, chk->rec.data.TSN_seq);
430f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep,
431cfde3ff7SRandall Stewart 			    stcb, control, &stcb->sctp_socket->so_rcv, end,
432cfde3ff7SRandall Stewart 			    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
433f8829a4aSRandall Stewart 			cntDel++;
434f8829a4aSRandall Stewart 		} else {
435f8829a4aSRandall Stewart 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
436f8829a4aSRandall Stewart 				end = 1;
437f8829a4aSRandall Stewart 			else
438f8829a4aSRandall Stewart 				end = 0;
439b5c16493SMichael Tuexen 			sctp_mark_non_revokable(asoc, chk->rec.data.TSN_seq);
440f8829a4aSRandall Stewart 			if (sctp_append_to_readq(stcb->sctp_ep, stcb,
441f8829a4aSRandall Stewart 			    stcb->asoc.control_pdapi,
442f8829a4aSRandall Stewart 			    chk->data, end, chk->rec.data.TSN_seq,
443f8829a4aSRandall Stewart 			    &stcb->sctp_socket->so_rcv)) {
444f8829a4aSRandall Stewart 				/*
445f8829a4aSRandall Stewart 				 * something is very wrong, either
446f8829a4aSRandall Stewart 				 * control_pdapi is NULL, or the tail_mbuf
447f8829a4aSRandall Stewart 				 * is corrupt, or there is a EOM already on
448f8829a4aSRandall Stewart 				 * the mbuf chain.
449f8829a4aSRandall Stewart 				 */
450851b7298SRandall Stewart 				if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
451851b7298SRandall Stewart 					goto abandon;
452851b7298SRandall Stewart 				} else {
453df6e0cc3SRandall Stewart #ifdef INVARIANTS
454ad81507eSRandall Stewart 					if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
455f8829a4aSRandall Stewart 						panic("This should not happen control_pdapi NULL?");
456f8829a4aSRandall Stewart 					}
457f8829a4aSRandall Stewart 					/* if we did not panic, it was a EOM */
458f8829a4aSRandall Stewart 					panic("Bad chunking ??");
459df6e0cc3SRandall Stewart #else
460df6e0cc3SRandall Stewart 					if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
461df6e0cc3SRandall Stewart 						SCTP_PRINTF("This should not happen control_pdapi NULL?\n");
462df6e0cc3SRandall Stewart 					}
463df6e0cc3SRandall Stewart 					SCTP_PRINTF("Bad chunking ??\n");
464df6e0cc3SRandall Stewart 					SCTP_PRINTF("Dumping re-assembly queue this will probably hose the association\n");
465df6e0cc3SRandall Stewart 
466df6e0cc3SRandall Stewart #endif
467df6e0cc3SRandall Stewart 					goto abandon;
468f8829a4aSRandall Stewart 				}
469851b7298SRandall Stewart 			}
470f8829a4aSRandall Stewart 			cntDel++;
471f8829a4aSRandall Stewart 		}
472f8829a4aSRandall Stewart 		/* pull it we did it */
473f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
474f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
475f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 0;
476f8829a4aSRandall Stewart 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
477f8829a4aSRandall Stewart 				asoc->strmin[stream_no].last_sequence_delivered++;
478f8829a4aSRandall Stewart 			}
479f8829a4aSRandall Stewart 			if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
480f8829a4aSRandall Stewart 				SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
481f8829a4aSRandall Stewart 			}
482f8829a4aSRandall Stewart 		} else if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
483f8829a4aSRandall Stewart 			/*
484f8829a4aSRandall Stewart 			 * turn the flag back on since we just  delivered
485f8829a4aSRandall Stewart 			 * yet another one.
486f8829a4aSRandall Stewart 			 */
487f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 1;
488f8829a4aSRandall Stewart 		}
489f8829a4aSRandall Stewart 		asoc->tsn_of_pdapi_last_delivered = chk->rec.data.TSN_seq;
490f8829a4aSRandall Stewart 		asoc->last_flags_delivered = chk->rec.data.rcv_flags;
491f8829a4aSRandall Stewart 		asoc->last_strm_seq_delivered = chk->rec.data.stream_seq;
492f8829a4aSRandall Stewart 		asoc->last_strm_no_delivered = chk->rec.data.stream_number;
493f8829a4aSRandall Stewart 
494f8829a4aSRandall Stewart 		asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
495f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue -= chk->send_size;
496f8829a4aSRandall Stewart 		sctp_ucount_decr(asoc->cnt_on_reasm_queue);
497f8829a4aSRandall Stewart 		/* free up the chk */
498f8829a4aSRandall Stewart 		chk->data = NULL;
499689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
500f8829a4aSRandall Stewart 
501f8829a4aSRandall Stewart 		if (asoc->fragmented_delivery_inprogress == 0) {
502f8829a4aSRandall Stewart 			/*
503f8829a4aSRandall Stewart 			 * Now lets see if we can deliver the next one on
504f8829a4aSRandall Stewart 			 * the stream
505f8829a4aSRandall Stewart 			 */
506f8829a4aSRandall Stewart 			struct sctp_stream_in *strm;
507f8829a4aSRandall Stewart 
508f8829a4aSRandall Stewart 			strm = &asoc->strmin[stream_no];
509f8829a4aSRandall Stewart 			nxt_todel = strm->last_sequence_delivered + 1;
5104a9ef3f8SMichael Tuexen 			TAILQ_FOREACH_SAFE(ctl, &strm->inqueue, next, nctl) {
511f8829a4aSRandall Stewart 				/* Deliver more if we can. */
512f8829a4aSRandall Stewart 				if (nxt_todel == ctl->sinfo_ssn) {
513f8829a4aSRandall Stewart 					TAILQ_REMOVE(&strm->inqueue, ctl, next);
514f8829a4aSRandall Stewart 					asoc->size_on_all_streams -= ctl->length;
515f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->cnt_on_all_streams);
516f8829a4aSRandall Stewart 					strm->last_sequence_delivered++;
517b5c16493SMichael Tuexen 					sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
518f8829a4aSRandall Stewart 					sctp_add_to_readq(stcb->sctp_ep, stcb,
519f8829a4aSRandall Stewart 					    ctl,
520cfde3ff7SRandall Stewart 					    &stcb->sctp_socket->so_rcv, 1,
521cfde3ff7SRandall Stewart 					    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
522f8829a4aSRandall Stewart 				} else {
523f8829a4aSRandall Stewart 					break;
524f8829a4aSRandall Stewart 				}
525f8829a4aSRandall Stewart 				nxt_todel = strm->last_sequence_delivered + 1;
526f8829a4aSRandall Stewart 			}
527139bc87fSRandall Stewart 			break;
528f8829a4aSRandall Stewart 		}
5294a9ef3f8SMichael Tuexen 	}
530f8829a4aSRandall Stewart }
531f8829a4aSRandall Stewart 
532f8829a4aSRandall Stewart /*
533f8829a4aSRandall Stewart  * Queue the chunk either right into the socket buffer if it is the next one
534f8829a4aSRandall Stewart  * to go OR put it in the correct place in the delivery queue.  If we do
535f8829a4aSRandall Stewart  * append to the so_buf, keep doing so until we are out of order. One big
536f8829a4aSRandall Stewart  * question still remains, what to do when the socket buffer is FULL??
537f8829a4aSRandall Stewart  */
538f8829a4aSRandall Stewart static void
539f8829a4aSRandall Stewart sctp_queue_data_to_stream(struct sctp_tcb *stcb, struct sctp_association *asoc,
540f8829a4aSRandall Stewart     struct sctp_queued_to_read *control, int *abort_flag)
541f8829a4aSRandall Stewart {
542f8829a4aSRandall Stewart 	/*
543f8829a4aSRandall Stewart 	 * FIX-ME maybe? What happens when the ssn wraps? If we are getting
544f8829a4aSRandall Stewart 	 * all the data in one stream this could happen quite rapidly. One
545f8829a4aSRandall Stewart 	 * could use the TSN to keep track of things, but this scheme breaks
546f8829a4aSRandall Stewart 	 * down in the other type of stream useage that could occur. Send a
547f8829a4aSRandall Stewart 	 * single msg to stream 0, send 4Billion messages to stream 1, now
548f8829a4aSRandall Stewart 	 * send a message to stream 0. You have a situation where the TSN
549f8829a4aSRandall Stewart 	 * has wrapped but not in the stream. Is this worth worrying about
550f8829a4aSRandall Stewart 	 * or should we just change our queue sort at the bottom to be by
551f8829a4aSRandall Stewart 	 * TSN.
552f8829a4aSRandall Stewart 	 *
553f8829a4aSRandall Stewart 	 * Could it also be legal for a peer to send ssn 1 with TSN 2 and ssn 2
554f8829a4aSRandall Stewart 	 * with TSN 1? If the peer is doing some sort of funky TSN/SSN
555f8829a4aSRandall Stewart 	 * assignment this could happen... and I don't see how this would be
556f8829a4aSRandall Stewart 	 * a violation. So for now I am undecided an will leave the sort by
557f8829a4aSRandall Stewart 	 * SSN alone. Maybe a hybred approach is the answer
558f8829a4aSRandall Stewart 	 *
559f8829a4aSRandall Stewart 	 */
560f8829a4aSRandall Stewart 	struct sctp_stream_in *strm;
561f8829a4aSRandall Stewart 	struct sctp_queued_to_read *at;
562f8829a4aSRandall Stewart 	int queue_needed;
563f8829a4aSRandall Stewart 	uint16_t nxt_todel;
564f8829a4aSRandall Stewart 	struct mbuf *oper;
565f8829a4aSRandall Stewart 
566f8829a4aSRandall Stewart 	queue_needed = 1;
567f8829a4aSRandall Stewart 	asoc->size_on_all_streams += control->length;
568f8829a4aSRandall Stewart 	sctp_ucount_incr(asoc->cnt_on_all_streams);
569f8829a4aSRandall Stewart 	strm = &asoc->strmin[control->sinfo_stream];
570f8829a4aSRandall Stewart 	nxt_todel = strm->last_sequence_delivered + 1;
571b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
572f8829a4aSRandall Stewart 		sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INTO_STRD);
57380fefe0aSRandall Stewart 	}
574ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INDATA1,
575ad81507eSRandall Stewart 	    "queue to stream called for ssn:%u lastdel:%u nxt:%u\n",
576f8829a4aSRandall Stewart 	    (uint32_t) control->sinfo_stream,
577ad81507eSRandall Stewart 	    (uint32_t) strm->last_sequence_delivered,
578ad81507eSRandall Stewart 	    (uint32_t) nxt_todel);
57920b07a4dSMichael Tuexen 	if (SCTP_SSN_GE(strm->last_sequence_delivered, control->sinfo_ssn)) {
580f8829a4aSRandall Stewart 		/* The incoming sseq is behind where we last delivered? */
581ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1, "Duplicate S-SEQ:%d delivered:%d from peer, Abort  association\n",
582ad81507eSRandall Stewart 		    control->sinfo_ssn, strm->last_sequence_delivered);
583c40e9cf2SRandall Stewart protocol_error:
584f8829a4aSRandall Stewart 		/*
585f8829a4aSRandall Stewart 		 * throw it in the stream so it gets cleaned up in
586f8829a4aSRandall Stewart 		 * association destruction
587f8829a4aSRandall Stewart 		 */
588f8829a4aSRandall Stewart 		TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
589139bc87fSRandall Stewart 		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
590f8829a4aSRandall Stewart 		    0, M_DONTWAIT, 1, MT_DATA);
591f8829a4aSRandall Stewart 		if (oper) {
592f8829a4aSRandall Stewart 			struct sctp_paramhdr *ph;
593f8829a4aSRandall Stewart 			uint32_t *ippp;
594f8829a4aSRandall Stewart 
595139bc87fSRandall Stewart 			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
596f8829a4aSRandall Stewart 			    (sizeof(uint32_t) * 3);
597f8829a4aSRandall Stewart 			ph = mtod(oper, struct sctp_paramhdr *);
598f8829a4aSRandall Stewart 			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
599139bc87fSRandall Stewart 			ph->param_length = htons(SCTP_BUF_LEN(oper));
600f8829a4aSRandall Stewart 			ippp = (uint32_t *) (ph + 1);
601a5d547adSRandall Stewart 			*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_1);
602f8829a4aSRandall Stewart 			ippp++;
603f8829a4aSRandall Stewart 			*ippp = control->sinfo_tsn;
604f8829a4aSRandall Stewart 			ippp++;
605f8829a4aSRandall Stewart 			*ippp = ((control->sinfo_stream << 16) | control->sinfo_ssn);
606f8829a4aSRandall Stewart 		}
607a5d547adSRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_1;
608a2b42326SMichael Tuexen 		sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
609f8829a4aSRandall Stewart 		*abort_flag = 1;
610f8829a4aSRandall Stewart 		return;
611f8829a4aSRandall Stewart 
612f8829a4aSRandall Stewart 	}
613f8829a4aSRandall Stewart 	if (nxt_todel == control->sinfo_ssn) {
614f8829a4aSRandall Stewart 		/* can be delivered right away? */
615b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
616f8829a4aSRandall Stewart 			sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_IMMED_DEL);
61780fefe0aSRandall Stewart 		}
618830d754dSRandall Stewart 		/* EY it wont be queued if it could be delivered directly */
619f8829a4aSRandall Stewart 		queue_needed = 0;
620f8829a4aSRandall Stewart 		asoc->size_on_all_streams -= control->length;
621f8829a4aSRandall Stewart 		sctp_ucount_decr(asoc->cnt_on_all_streams);
622f8829a4aSRandall Stewart 		strm->last_sequence_delivered++;
62377acdc25SRandall Stewart 
624b5c16493SMichael Tuexen 		sctp_mark_non_revokable(asoc, control->sinfo_tsn);
625f8829a4aSRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb,
626f8829a4aSRandall Stewart 		    control,
627cfde3ff7SRandall Stewart 		    &stcb->sctp_socket->so_rcv, 1,
628cfde3ff7SRandall Stewart 		    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
6294a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(control, &strm->inqueue, next, at) {
630f8829a4aSRandall Stewart 			/* all delivered */
631f8829a4aSRandall Stewart 			nxt_todel = strm->last_sequence_delivered + 1;
632f8829a4aSRandall Stewart 			if (nxt_todel == control->sinfo_ssn) {
633f8829a4aSRandall Stewart 				TAILQ_REMOVE(&strm->inqueue, control, next);
634f8829a4aSRandall Stewart 				asoc->size_on_all_streams -= control->length;
635f8829a4aSRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_all_streams);
636f8829a4aSRandall Stewart 				strm->last_sequence_delivered++;
637f8829a4aSRandall Stewart 				/*
638f8829a4aSRandall Stewart 				 * We ignore the return of deliver_data here
639f8829a4aSRandall Stewart 				 * since we always can hold the chunk on the
640f8829a4aSRandall Stewart 				 * d-queue. And we have a finite number that
641f8829a4aSRandall Stewart 				 * can be delivered from the strq.
642f8829a4aSRandall Stewart 				 */
643b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
644f8829a4aSRandall Stewart 					sctp_log_strm_del(control, NULL,
645f8829a4aSRandall Stewart 					    SCTP_STR_LOG_FROM_IMMED_DEL);
64680fefe0aSRandall Stewart 				}
647b5c16493SMichael Tuexen 				sctp_mark_non_revokable(asoc, control->sinfo_tsn);
648f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
649f8829a4aSRandall Stewart 				    control,
650cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1,
651cfde3ff7SRandall Stewart 				    SCTP_READ_LOCK_NOT_HELD,
652cfde3ff7SRandall Stewart 				    SCTP_SO_NOT_LOCKED);
653f8829a4aSRandall Stewart 				continue;
654f8829a4aSRandall Stewart 			}
655f8829a4aSRandall Stewart 			break;
656f8829a4aSRandall Stewart 		}
657f8829a4aSRandall Stewart 	}
658f8829a4aSRandall Stewart 	if (queue_needed) {
659f8829a4aSRandall Stewart 		/*
660f8829a4aSRandall Stewart 		 * Ok, we did not deliver this guy, find the correct place
661f8829a4aSRandall Stewart 		 * to put it on the queue.
662f8829a4aSRandall Stewart 		 */
66320b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(asoc->cumulative_tsn, control->sinfo_tsn)) {
664c40e9cf2SRandall Stewart 			goto protocol_error;
665c40e9cf2SRandall Stewart 		}
666f8829a4aSRandall Stewart 		if (TAILQ_EMPTY(&strm->inqueue)) {
667f8829a4aSRandall Stewart 			/* Empty queue */
668b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
669f8829a4aSRandall Stewart 				sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INSERT_HD);
67080fefe0aSRandall Stewart 			}
671f8829a4aSRandall Stewart 			TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
672f8829a4aSRandall Stewart 		} else {
673f8829a4aSRandall Stewart 			TAILQ_FOREACH(at, &strm->inqueue, next) {
67420b07a4dSMichael Tuexen 				if (SCTP_SSN_GT(at->sinfo_ssn, control->sinfo_ssn)) {
675f8829a4aSRandall Stewart 					/*
676f8829a4aSRandall Stewart 					 * one in queue is bigger than the
677f8829a4aSRandall Stewart 					 * new one, insert before this one
678f8829a4aSRandall Stewart 					 */
679b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
680f8829a4aSRandall Stewart 						sctp_log_strm_del(control, at,
681f8829a4aSRandall Stewart 						    SCTP_STR_LOG_FROM_INSERT_MD);
68280fefe0aSRandall Stewart 					}
683f8829a4aSRandall Stewart 					TAILQ_INSERT_BEFORE(at, control, next);
684f8829a4aSRandall Stewart 					break;
685f8829a4aSRandall Stewart 				} else if (at->sinfo_ssn == control->sinfo_ssn) {
686f8829a4aSRandall Stewart 					/*
687f8829a4aSRandall Stewart 					 * Gak, He sent me a duplicate str
688f8829a4aSRandall Stewart 					 * seq number
689f8829a4aSRandall Stewart 					 */
690f8829a4aSRandall Stewart 					/*
691f8829a4aSRandall Stewart 					 * foo bar, I guess I will just free
692f8829a4aSRandall Stewart 					 * this new guy, should we abort
693f8829a4aSRandall Stewart 					 * too? FIX ME MAYBE? Or it COULD be
694f8829a4aSRandall Stewart 					 * that the SSN's have wrapped.
695f8829a4aSRandall Stewart 					 * Maybe I should compare to TSN
696f8829a4aSRandall Stewart 					 * somehow... sigh for now just blow
697f8829a4aSRandall Stewart 					 * away the chunk!
698f8829a4aSRandall Stewart 					 */
699f8829a4aSRandall Stewart 
700f8829a4aSRandall Stewart 					if (control->data)
701f8829a4aSRandall Stewart 						sctp_m_freem(control->data);
702f8829a4aSRandall Stewart 					control->data = NULL;
703f8829a4aSRandall Stewart 					asoc->size_on_all_streams -= control->length;
704f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->cnt_on_all_streams);
70524f52bbdSMichael Tuexen 					if (control->whoFrom) {
706f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
7075bead436SRandall Stewart 						control->whoFrom = NULL;
70824f52bbdSMichael Tuexen 					}
709f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
710f8829a4aSRandall Stewart 					return;
711f8829a4aSRandall Stewart 				} else {
712f8829a4aSRandall Stewart 					if (TAILQ_NEXT(at, next) == NULL) {
713f8829a4aSRandall Stewart 						/*
714f8829a4aSRandall Stewart 						 * We are at the end, insert
715f8829a4aSRandall Stewart 						 * it after this one
716f8829a4aSRandall Stewart 						 */
717b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
718f8829a4aSRandall Stewart 							sctp_log_strm_del(control, at,
719f8829a4aSRandall Stewart 							    SCTP_STR_LOG_FROM_INSERT_TL);
72080fefe0aSRandall Stewart 						}
721f8829a4aSRandall Stewart 						TAILQ_INSERT_AFTER(&strm->inqueue,
722f8829a4aSRandall Stewart 						    at, control, next);
723f8829a4aSRandall Stewart 						break;
724f8829a4aSRandall Stewart 					}
725f8829a4aSRandall Stewart 				}
726f8829a4aSRandall Stewart 			}
727f8829a4aSRandall Stewart 		}
728f8829a4aSRandall Stewart 	}
729f8829a4aSRandall Stewart }
730f8829a4aSRandall Stewart 
731f8829a4aSRandall Stewart /*
732f8829a4aSRandall Stewart  * Returns two things: You get the total size of the deliverable parts of the
733f8829a4aSRandall Stewart  * first fragmented message on the reassembly queue. And you get a 1 back if
734f8829a4aSRandall Stewart  * all of the message is ready or a 0 back if the message is still incomplete
735f8829a4aSRandall Stewart  */
736f8829a4aSRandall Stewart static int
737f8829a4aSRandall Stewart sctp_is_all_msg_on_reasm(struct sctp_association *asoc, uint32_t * t_size)
738f8829a4aSRandall Stewart {
739f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
740f8829a4aSRandall Stewart 	uint32_t tsn;
741f8829a4aSRandall Stewart 
742f8829a4aSRandall Stewart 	*t_size = 0;
743f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
744f8829a4aSRandall Stewart 	if (chk == NULL) {
745f8829a4aSRandall Stewart 		/* nothing on the queue */
746f8829a4aSRandall Stewart 		return (0);
747f8829a4aSRandall Stewart 	}
748f8829a4aSRandall Stewart 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
749f8829a4aSRandall Stewart 		/* Not a first on the queue */
750f8829a4aSRandall Stewart 		return (0);
751f8829a4aSRandall Stewart 	}
752f8829a4aSRandall Stewart 	tsn = chk->rec.data.TSN_seq;
7534a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(chk, &asoc->reasmqueue, sctp_next) {
754f8829a4aSRandall Stewart 		if (tsn != chk->rec.data.TSN_seq) {
755f8829a4aSRandall Stewart 			return (0);
756f8829a4aSRandall Stewart 		}
757f8829a4aSRandall Stewart 		*t_size += chk->send_size;
758f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
759f8829a4aSRandall Stewart 			return (1);
760f8829a4aSRandall Stewart 		}
761f8829a4aSRandall Stewart 		tsn++;
762f8829a4aSRandall Stewart 	}
763f8829a4aSRandall Stewart 	return (0);
764f8829a4aSRandall Stewart }
765f8829a4aSRandall Stewart 
766f8829a4aSRandall Stewart static void
767f8829a4aSRandall Stewart sctp_deliver_reasm_check(struct sctp_tcb *stcb, struct sctp_association *asoc)
768f8829a4aSRandall Stewart {
769f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
770f8829a4aSRandall Stewart 	uint16_t nxt_todel;
771810ec536SMichael Tuexen 	uint32_t tsize, pd_point;
772f8829a4aSRandall Stewart 
773139bc87fSRandall Stewart doit_again:
774f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
775f8829a4aSRandall Stewart 	if (chk == NULL) {
776f8829a4aSRandall Stewart 		/* Huh? */
777f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = 0;
778f8829a4aSRandall Stewart 		asoc->cnt_on_reasm_queue = 0;
779f8829a4aSRandall Stewart 		return;
780f8829a4aSRandall Stewart 	}
781f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress == 0) {
782f8829a4aSRandall Stewart 		nxt_todel =
783f8829a4aSRandall Stewart 		    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
784f8829a4aSRandall Stewart 		if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
785f8829a4aSRandall Stewart 		    (nxt_todel == chk->rec.data.stream_seq ||
786f8829a4aSRandall Stewart 		    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
787f8829a4aSRandall Stewart 			/*
788f8829a4aSRandall Stewart 			 * Yep the first one is here and its ok to deliver
789f8829a4aSRandall Stewart 			 * but should we?
790f8829a4aSRandall Stewart 			 */
791810ec536SMichael Tuexen 			if (stcb->sctp_socket) {
79224ae5c4aSMichael Tuexen 				pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket),
793810ec536SMichael Tuexen 				    stcb->sctp_ep->partial_delivery_point);
794810ec536SMichael Tuexen 			} else {
795810ec536SMichael Tuexen 				pd_point = stcb->sctp_ep->partial_delivery_point;
796810ec536SMichael Tuexen 			}
797810ec536SMichael Tuexen 			if (sctp_is_all_msg_on_reasm(asoc, &tsize) || (tsize >= pd_point)) {
798f8829a4aSRandall Stewart 
799f8829a4aSRandall Stewart 				/*
800f8829a4aSRandall Stewart 				 * Yes, we setup to start reception, by
801f8829a4aSRandall Stewart 				 * backing down the TSN just in case we
802f8829a4aSRandall Stewart 				 * can't deliver. If we
803f8829a4aSRandall Stewart 				 */
804f8829a4aSRandall Stewart 				asoc->fragmented_delivery_inprogress = 1;
805f8829a4aSRandall Stewart 				asoc->tsn_last_delivered =
806f8829a4aSRandall Stewart 				    chk->rec.data.TSN_seq - 1;
807f8829a4aSRandall Stewart 				asoc->str_of_pdapi =
808f8829a4aSRandall Stewart 				    chk->rec.data.stream_number;
809f8829a4aSRandall Stewart 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
810f8829a4aSRandall Stewart 				asoc->pdapi_ppid = chk->rec.data.payloadtype;
811f8829a4aSRandall Stewart 				asoc->fragment_flags = chk->rec.data.rcv_flags;
812f8829a4aSRandall Stewart 				sctp_service_reassembly(stcb, asoc);
813f8829a4aSRandall Stewart 			}
814f8829a4aSRandall Stewart 		}
815f8829a4aSRandall Stewart 	} else {
816139bc87fSRandall Stewart 		/*
817139bc87fSRandall Stewart 		 * Service re-assembly will deliver stream data queued at
818139bc87fSRandall Stewart 		 * the end of fragmented delivery.. but it wont know to go
819139bc87fSRandall Stewart 		 * back and call itself again... we do that here with the
820139bc87fSRandall Stewart 		 * got doit_again
821139bc87fSRandall Stewart 		 */
822f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
823139bc87fSRandall Stewart 		if (asoc->fragmented_delivery_inprogress == 0) {
824139bc87fSRandall Stewart 			/*
825139bc87fSRandall Stewart 			 * finished our Fragmented delivery, could be more
826139bc87fSRandall Stewart 			 * waiting?
827139bc87fSRandall Stewart 			 */
828139bc87fSRandall Stewart 			goto doit_again;
829139bc87fSRandall Stewart 		}
830f8829a4aSRandall Stewart 	}
831f8829a4aSRandall Stewart }
832f8829a4aSRandall Stewart 
833f8829a4aSRandall Stewart /*
834f8829a4aSRandall Stewart  * Dump onto the re-assembly queue, in its proper place. After dumping on the
835f8829a4aSRandall Stewart  * queue, see if anthing can be delivered. If so pull it off (or as much as
836f8829a4aSRandall Stewart  * we can. If we run out of space then we must dump what we can and set the
837f8829a4aSRandall Stewart  * appropriate flag to say we queued what we could.
838f8829a4aSRandall Stewart  */
839f8829a4aSRandall Stewart static void
840f8829a4aSRandall Stewart sctp_queue_data_for_reasm(struct sctp_tcb *stcb, struct sctp_association *asoc,
841f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk, int *abort_flag)
842f8829a4aSRandall Stewart {
843f8829a4aSRandall Stewart 	struct mbuf *oper;
84460990c0cSMichael Tuexen 	uint32_t cum_ackp1, prev_tsn, post_tsn;
845f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *at, *prev, *next;
846f8829a4aSRandall Stewart 
847f8829a4aSRandall Stewart 	prev = next = NULL;
848f8829a4aSRandall Stewart 	cum_ackp1 = asoc->tsn_last_delivered + 1;
849f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->reasmqueue)) {
850f8829a4aSRandall Stewart 		/* This is the first one on the queue */
851f8829a4aSRandall Stewart 		TAILQ_INSERT_HEAD(&asoc->reasmqueue, chk, sctp_next);
852f8829a4aSRandall Stewart 		/*
853f8829a4aSRandall Stewart 		 * we do not check for delivery of anything when only one
854f8829a4aSRandall Stewart 		 * fragment is here
855f8829a4aSRandall Stewart 		 */
856f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = chk->send_size;
857f8829a4aSRandall Stewart 		sctp_ucount_incr(asoc->cnt_on_reasm_queue);
858f8829a4aSRandall Stewart 		if (chk->rec.data.TSN_seq == cum_ackp1) {
859f8829a4aSRandall Stewart 			if (asoc->fragmented_delivery_inprogress == 0 &&
860f8829a4aSRandall Stewart 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) !=
861f8829a4aSRandall Stewart 			    SCTP_DATA_FIRST_FRAG) {
862f8829a4aSRandall Stewart 				/*
863f8829a4aSRandall Stewart 				 * An empty queue, no delivery inprogress,
864f8829a4aSRandall Stewart 				 * we hit the next one and it does NOT have
865f8829a4aSRandall Stewart 				 * a FIRST fragment mark.
866f8829a4aSRandall Stewart 				 */
867ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not first, no fragmented delivery in progress\n");
868139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
869f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
870f8829a4aSRandall Stewart 
871f8829a4aSRandall Stewart 				if (oper) {
872f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
873f8829a4aSRandall Stewart 					uint32_t *ippp;
874f8829a4aSRandall Stewart 
875139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
876f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
877f8829a4aSRandall Stewart 					    (sizeof(uint32_t) * 3);
878f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
879f8829a4aSRandall Stewart 					ph->param_type =
880f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
881139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
882f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
883a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_2);
884f8829a4aSRandall Stewart 					ippp++;
885f8829a4aSRandall Stewart 					*ippp = chk->rec.data.TSN_seq;
886f8829a4aSRandall Stewart 					ippp++;
887f8829a4aSRandall Stewart 					*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
888f8829a4aSRandall Stewart 
889f8829a4aSRandall Stewart 				}
890a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_2;
891a2b42326SMichael Tuexen 				sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
892f8829a4aSRandall Stewart 				*abort_flag = 1;
893f8829a4aSRandall Stewart 			} else if (asoc->fragmented_delivery_inprogress &&
894f8829a4aSRandall Stewart 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
895f8829a4aSRandall Stewart 				/*
896f8829a4aSRandall Stewart 				 * We are doing a partial delivery and the
897f8829a4aSRandall Stewart 				 * NEXT chunk MUST be either the LAST or
898f8829a4aSRandall Stewart 				 * MIDDLE fragment NOT a FIRST
899f8829a4aSRandall Stewart 				 */
900ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS a first and fragmented delivery in progress\n");
901139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
902f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
903f8829a4aSRandall Stewart 				if (oper) {
904f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
905f8829a4aSRandall Stewart 					uint32_t *ippp;
906f8829a4aSRandall Stewart 
907139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
908f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
909f8829a4aSRandall Stewart 					    (3 * sizeof(uint32_t));
910f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
911f8829a4aSRandall Stewart 					ph->param_type =
912f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
913139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
914f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
915a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_3);
916f8829a4aSRandall Stewart 					ippp++;
917f8829a4aSRandall Stewart 					*ippp = chk->rec.data.TSN_seq;
918f8829a4aSRandall Stewart 					ippp++;
919f8829a4aSRandall Stewart 					*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
920f8829a4aSRandall Stewart 				}
921a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_3;
922a2b42326SMichael Tuexen 				sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
923f8829a4aSRandall Stewart 				*abort_flag = 1;
924f8829a4aSRandall Stewart 			} else if (asoc->fragmented_delivery_inprogress) {
925f8829a4aSRandall Stewart 				/*
926f8829a4aSRandall Stewart 				 * Here we are ok with a MIDDLE or LAST
927f8829a4aSRandall Stewart 				 * piece
928f8829a4aSRandall Stewart 				 */
929f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
930f8829a4aSRandall Stewart 				    asoc->str_of_pdapi) {
931f8829a4aSRandall Stewart 					/* Got to be the right STR No */
932ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream number %d vs %d\n",
933f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
934f8829a4aSRandall Stewart 					    asoc->str_of_pdapi);
935139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
936f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
937f8829a4aSRandall Stewart 					if (oper) {
938f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
939f8829a4aSRandall Stewart 						uint32_t *ippp;
940f8829a4aSRandall Stewart 
941139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
942f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
943f8829a4aSRandall Stewart 						    (sizeof(uint32_t) * 3);
944f8829a4aSRandall Stewart 						ph = mtod(oper,
945f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
946f8829a4aSRandall Stewart 						ph->param_type =
947f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
948f8829a4aSRandall Stewart 						ph->param_length =
949139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
950f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
951a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_4);
952f8829a4aSRandall Stewart 						ippp++;
953f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
954f8829a4aSRandall Stewart 						ippp++;
955f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
956f8829a4aSRandall Stewart 					}
957a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_4;
958a2b42326SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
959f8829a4aSRandall Stewart 					*abort_flag = 1;
960f8829a4aSRandall Stewart 				} else if ((asoc->fragment_flags & SCTP_DATA_UNORDERED) !=
961f8829a4aSRandall Stewart 					    SCTP_DATA_UNORDERED &&
962b5c16493SMichael Tuexen 				    chk->rec.data.stream_seq != asoc->ssn_of_pdapi) {
963f8829a4aSRandall Stewart 					/* Got to be the right STR Seq */
964ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream seq %d vs %d\n",
965f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
966f8829a4aSRandall Stewart 					    asoc->ssn_of_pdapi);
967139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
968f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
969f8829a4aSRandall Stewart 					if (oper) {
970f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
971f8829a4aSRandall Stewart 						uint32_t *ippp;
972f8829a4aSRandall Stewart 
973139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
974f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
975f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
976f8829a4aSRandall Stewart 						ph = mtod(oper,
977f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
978f8829a4aSRandall Stewart 						ph->param_type =
979f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
980f8829a4aSRandall Stewart 						ph->param_length =
981139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
982f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
983a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_5);
984f8829a4aSRandall Stewart 						ippp++;
985f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
986f8829a4aSRandall Stewart 						ippp++;
987f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
988f8829a4aSRandall Stewart 
989f8829a4aSRandall Stewart 					}
990a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_5;
991a2b42326SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
992f8829a4aSRandall Stewart 					*abort_flag = 1;
993f8829a4aSRandall Stewart 				}
994f8829a4aSRandall Stewart 			}
995f8829a4aSRandall Stewart 		}
996f8829a4aSRandall Stewart 		return;
997f8829a4aSRandall Stewart 	}
998f8829a4aSRandall Stewart 	/* Find its place */
999f8829a4aSRandall Stewart 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
100020b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(at->rec.data.TSN_seq, chk->rec.data.TSN_seq)) {
1001f8829a4aSRandall Stewart 			/*
1002f8829a4aSRandall Stewart 			 * one in queue is bigger than the new one, insert
1003f8829a4aSRandall Stewart 			 * before this one
1004f8829a4aSRandall Stewart 			 */
1005f8829a4aSRandall Stewart 			/* A check */
1006f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue += chk->send_size;
1007f8829a4aSRandall Stewart 			sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1008f8829a4aSRandall Stewart 			next = at;
1009f8829a4aSRandall Stewart 			TAILQ_INSERT_BEFORE(at, chk, sctp_next);
1010f8829a4aSRandall Stewart 			break;
1011f8829a4aSRandall Stewart 		} else if (at->rec.data.TSN_seq == chk->rec.data.TSN_seq) {
1012f8829a4aSRandall Stewart 			/* Gak, He sent me a duplicate str seq number */
1013f8829a4aSRandall Stewart 			/*
1014f8829a4aSRandall Stewart 			 * foo bar, I guess I will just free this new guy,
1015f8829a4aSRandall Stewart 			 * should we abort too? FIX ME MAYBE? Or it COULD be
1016f8829a4aSRandall Stewart 			 * that the SSN's have wrapped. Maybe I should
1017f8829a4aSRandall Stewart 			 * compare to TSN somehow... sigh for now just blow
1018f8829a4aSRandall Stewart 			 * away the chunk!
1019f8829a4aSRandall Stewart 			 */
1020f8829a4aSRandall Stewart 			if (chk->data) {
1021f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
1022f8829a4aSRandall Stewart 				chk->data = NULL;
1023f8829a4aSRandall Stewart 			}
1024689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
1025f8829a4aSRandall Stewart 			return;
1026f8829a4aSRandall Stewart 		} else {
1027f8829a4aSRandall Stewart 			prev = at;
1028f8829a4aSRandall Stewart 			if (TAILQ_NEXT(at, sctp_next) == NULL) {
1029f8829a4aSRandall Stewart 				/*
1030f8829a4aSRandall Stewart 				 * We are at the end, insert it after this
1031f8829a4aSRandall Stewart 				 * one
1032f8829a4aSRandall Stewart 				 */
1033f8829a4aSRandall Stewart 				/* check it first */
1034f8829a4aSRandall Stewart 				asoc->size_on_reasm_queue += chk->send_size;
1035f8829a4aSRandall Stewart 				sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1036f8829a4aSRandall Stewart 				TAILQ_INSERT_AFTER(&asoc->reasmqueue, at, chk, sctp_next);
1037f8829a4aSRandall Stewart 				break;
1038f8829a4aSRandall Stewart 			}
1039f8829a4aSRandall Stewart 		}
1040f8829a4aSRandall Stewart 	}
1041f8829a4aSRandall Stewart 	/* Now the audits */
1042f8829a4aSRandall Stewart 	if (prev) {
1043f8829a4aSRandall Stewart 		prev_tsn = chk->rec.data.TSN_seq - 1;
1044f8829a4aSRandall Stewart 		if (prev_tsn == prev->rec.data.TSN_seq) {
1045f8829a4aSRandall Stewart 			/*
1046f8829a4aSRandall Stewart 			 * Ok the one I am dropping onto the end is the
1047f8829a4aSRandall Stewart 			 * NEXT. A bit of valdiation here.
1048f8829a4aSRandall Stewart 			 */
1049f8829a4aSRandall Stewart 			if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1050f8829a4aSRandall Stewart 			    SCTP_DATA_FIRST_FRAG ||
1051f8829a4aSRandall Stewart 			    (prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1052f8829a4aSRandall Stewart 			    SCTP_DATA_MIDDLE_FRAG) {
1053f8829a4aSRandall Stewart 				/*
1054f8829a4aSRandall Stewart 				 * Insert chk MUST be a MIDDLE or LAST
1055f8829a4aSRandall Stewart 				 * fragment
1056f8829a4aSRandall Stewart 				 */
1057f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1058f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1059ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - It can be a midlle or last but not a first\n");
1060ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it's a FIRST!\n");
1061139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1062f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1063f8829a4aSRandall Stewart 					if (oper) {
1064f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1065f8829a4aSRandall Stewart 						uint32_t *ippp;
1066f8829a4aSRandall Stewart 
1067139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1068f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1069f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1070f8829a4aSRandall Stewart 						ph = mtod(oper,
1071f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1072f8829a4aSRandall Stewart 						ph->param_type =
1073f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1074f8829a4aSRandall Stewart 						ph->param_length =
1075139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1076f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1077a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_6);
1078f8829a4aSRandall Stewart 						ippp++;
1079f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1080f8829a4aSRandall Stewart 						ippp++;
1081f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1082f8829a4aSRandall Stewart 
1083f8829a4aSRandall Stewart 					}
1084a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_6;
1085a2b42326SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
1086f8829a4aSRandall Stewart 					*abort_flag = 1;
1087f8829a4aSRandall Stewart 					return;
1088f8829a4aSRandall Stewart 				}
1089f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
1090f8829a4aSRandall Stewart 				    prev->rec.data.stream_number) {
1091f8829a4aSRandall Stewart 					/*
1092f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1093f8829a4aSRandall Stewart 					 * they must be the same.
1094f8829a4aSRandall Stewart 					 */
1095ad81507eSRandall Stewart 					SCTP_PRINTF("Prev check - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1096f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
1097f8829a4aSRandall Stewart 					    prev->rec.data.stream_number);
1098139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1099f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1100f8829a4aSRandall Stewart 					if (oper) {
1101f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1102f8829a4aSRandall Stewart 						uint32_t *ippp;
1103f8829a4aSRandall Stewart 
1104139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1105f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1106f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1107f8829a4aSRandall Stewart 						ph = mtod(oper,
1108f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1109f8829a4aSRandall Stewart 						ph->param_type =
1110f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1111f8829a4aSRandall Stewart 						ph->param_length =
1112139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1113f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1114a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_7);
1115f8829a4aSRandall Stewart 						ippp++;
1116f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1117f8829a4aSRandall Stewart 						ippp++;
1118f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1119f8829a4aSRandall Stewart 					}
1120a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_7;
1121a2b42326SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
1122f8829a4aSRandall Stewart 					*abort_flag = 1;
1123f8829a4aSRandall Stewart 					return;
1124f8829a4aSRandall Stewart 				}
1125f8829a4aSRandall Stewart 				if ((prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1126f8829a4aSRandall Stewart 				    chk->rec.data.stream_seq !=
1127f8829a4aSRandall Stewart 				    prev->rec.data.stream_seq) {
1128f8829a4aSRandall Stewart 					/*
1129f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1130f8829a4aSRandall Stewart 					 * they must be the same.
1131f8829a4aSRandall Stewart 					 */
1132ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1133f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
1134f8829a4aSRandall Stewart 					    prev->rec.data.stream_seq);
1135139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1136f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1137f8829a4aSRandall Stewart 					if (oper) {
1138f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1139f8829a4aSRandall Stewart 						uint32_t *ippp;
1140f8829a4aSRandall Stewart 
1141139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1142f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1143f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1144f8829a4aSRandall Stewart 						ph = mtod(oper,
1145f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1146f8829a4aSRandall Stewart 						ph->param_type =
1147f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1148f8829a4aSRandall Stewart 						ph->param_length =
1149139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1150f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1151a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_8);
1152f8829a4aSRandall Stewart 						ippp++;
1153f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1154f8829a4aSRandall Stewart 						ippp++;
1155f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1156f8829a4aSRandall Stewart 					}
1157a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_8;
1158a2b42326SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
1159f8829a4aSRandall Stewart 					*abort_flag = 1;
1160f8829a4aSRandall Stewart 					return;
1161f8829a4aSRandall Stewart 				}
1162f8829a4aSRandall Stewart 			} else if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1163f8829a4aSRandall Stewart 			    SCTP_DATA_LAST_FRAG) {
1164f8829a4aSRandall Stewart 				/* Insert chk MUST be a FIRST */
1165f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1166f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1167ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, evil plot, its not FIRST and it must be!\n");
1168139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1169f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1170f8829a4aSRandall Stewart 					if (oper) {
1171f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1172f8829a4aSRandall Stewart 						uint32_t *ippp;
1173f8829a4aSRandall Stewart 
1174139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1175f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1176f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1177f8829a4aSRandall Stewart 						ph = mtod(oper,
1178f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1179f8829a4aSRandall Stewart 						ph->param_type =
1180f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1181f8829a4aSRandall Stewart 						ph->param_length =
1182139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1183f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1184a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_9);
1185f8829a4aSRandall Stewart 						ippp++;
1186f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1187f8829a4aSRandall Stewart 						ippp++;
1188f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1189f8829a4aSRandall Stewart 
1190f8829a4aSRandall Stewart 					}
1191a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_9;
1192a2b42326SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
1193f8829a4aSRandall Stewart 					*abort_flag = 1;
1194f8829a4aSRandall Stewart 					return;
1195f8829a4aSRandall Stewart 				}
1196f8829a4aSRandall Stewart 			}
1197f8829a4aSRandall Stewart 		}
1198f8829a4aSRandall Stewart 	}
1199f8829a4aSRandall Stewart 	if (next) {
1200f8829a4aSRandall Stewart 		post_tsn = chk->rec.data.TSN_seq + 1;
1201f8829a4aSRandall Stewart 		if (post_tsn == next->rec.data.TSN_seq) {
1202f8829a4aSRandall Stewart 			/*
1203f8829a4aSRandall Stewart 			 * Ok the one I am inserting ahead of is my NEXT
1204f8829a4aSRandall Stewart 			 * one. A bit of valdiation here.
1205f8829a4aSRandall Stewart 			 */
1206f8829a4aSRandall Stewart 			if (next->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
1207f8829a4aSRandall Stewart 				/* Insert chk MUST be a last fragment */
1208f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK)
1209f8829a4aSRandall Stewart 				    != SCTP_DATA_LAST_FRAG) {
1210ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is FIRST, we must be LAST\n");
1211ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not a last!\n");
1212139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1213f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1214f8829a4aSRandall Stewart 					if (oper) {
1215f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1216f8829a4aSRandall Stewart 						uint32_t *ippp;
1217f8829a4aSRandall Stewart 
1218139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1219f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1220f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1221f8829a4aSRandall Stewart 						ph = mtod(oper,
1222f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1223f8829a4aSRandall Stewart 						ph->param_type =
1224f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1225f8829a4aSRandall Stewart 						ph->param_length =
1226139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1227f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1228a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_10);
1229f8829a4aSRandall Stewart 						ippp++;
1230f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1231f8829a4aSRandall Stewart 						ippp++;
1232f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1233f8829a4aSRandall Stewart 					}
1234a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_10;
1235a2b42326SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
1236f8829a4aSRandall Stewart 					*abort_flag = 1;
1237f8829a4aSRandall Stewart 					return;
1238f8829a4aSRandall Stewart 				}
1239f8829a4aSRandall Stewart 			} else if ((next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1240f8829a4aSRandall Stewart 				    SCTP_DATA_MIDDLE_FRAG ||
1241f8829a4aSRandall Stewart 				    (next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1242f8829a4aSRandall Stewart 			    SCTP_DATA_LAST_FRAG) {
1243f8829a4aSRandall Stewart 				/*
1244f8829a4aSRandall Stewart 				 * Insert chk CAN be MIDDLE or FIRST NOT
1245f8829a4aSRandall Stewart 				 * LAST
1246f8829a4aSRandall Stewart 				 */
1247f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1248f8829a4aSRandall Stewart 				    SCTP_DATA_LAST_FRAG) {
1249ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is a MIDDLE/LAST\n");
1250ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, new prev chunk is a LAST\n");
1251139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1252f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1253f8829a4aSRandall Stewart 					if (oper) {
1254f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1255f8829a4aSRandall Stewart 						uint32_t *ippp;
1256f8829a4aSRandall Stewart 
1257139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1258f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1259f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1260f8829a4aSRandall Stewart 						ph = mtod(oper,
1261f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1262f8829a4aSRandall Stewart 						ph->param_type =
1263f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1264f8829a4aSRandall Stewart 						ph->param_length =
1265139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1266f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1267a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_11);
1268f8829a4aSRandall Stewart 						ippp++;
1269f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1270f8829a4aSRandall Stewart 						ippp++;
1271f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1272f8829a4aSRandall Stewart 
1273f8829a4aSRandall Stewart 					}
1274a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_11;
1275a2b42326SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
1276f8829a4aSRandall Stewart 					*abort_flag = 1;
1277f8829a4aSRandall Stewart 					return;
1278f8829a4aSRandall Stewart 				}
1279f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
1280f8829a4aSRandall Stewart 				    next->rec.data.stream_number) {
1281f8829a4aSRandall Stewart 					/*
1282f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1283f8829a4aSRandall Stewart 					 * they must be the same.
1284f8829a4aSRandall Stewart 					 */
1285ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1286f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
1287f8829a4aSRandall Stewart 					    next->rec.data.stream_number);
1288139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1289f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1290f8829a4aSRandall Stewart 					if (oper) {
1291f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1292f8829a4aSRandall Stewart 						uint32_t *ippp;
1293f8829a4aSRandall Stewart 
1294139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1295f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1296f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1297f8829a4aSRandall Stewart 						ph = mtod(oper,
1298f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1299f8829a4aSRandall Stewart 						ph->param_type =
1300f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1301f8829a4aSRandall Stewart 						ph->param_length =
1302139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1303f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1304a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_12);
1305f8829a4aSRandall Stewart 						ippp++;
1306f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1307f8829a4aSRandall Stewart 						ippp++;
1308f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1309f8829a4aSRandall Stewart 
1310f8829a4aSRandall Stewart 					}
1311a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_12;
1312a2b42326SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
1313f8829a4aSRandall Stewart 					*abort_flag = 1;
1314f8829a4aSRandall Stewart 					return;
1315f8829a4aSRandall Stewart 				}
1316f8829a4aSRandall Stewart 				if ((next->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1317f8829a4aSRandall Stewart 				    chk->rec.data.stream_seq !=
1318f8829a4aSRandall Stewart 				    next->rec.data.stream_seq) {
1319f8829a4aSRandall Stewart 					/*
1320f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1321f8829a4aSRandall Stewart 					 * they must be the same.
1322f8829a4aSRandall Stewart 					 */
1323ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1324f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
1325f8829a4aSRandall Stewart 					    next->rec.data.stream_seq);
1326139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1327f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1328f8829a4aSRandall Stewart 					if (oper) {
1329f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1330f8829a4aSRandall Stewart 						uint32_t *ippp;
1331f8829a4aSRandall Stewart 
1332139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1333f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1334f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1335f8829a4aSRandall Stewart 						ph = mtod(oper,
1336f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1337f8829a4aSRandall Stewart 						ph->param_type =
1338f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1339f8829a4aSRandall Stewart 						ph->param_length =
1340139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1341f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1342a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_13);
1343f8829a4aSRandall Stewart 						ippp++;
1344f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1345f8829a4aSRandall Stewart 						ippp++;
1346f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1347f8829a4aSRandall Stewart 					}
1348a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_13;
1349a2b42326SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
1350f8829a4aSRandall Stewart 					*abort_flag = 1;
1351f8829a4aSRandall Stewart 					return;
1352f8829a4aSRandall Stewart 				}
1353f8829a4aSRandall Stewart 			}
1354f8829a4aSRandall Stewart 		}
1355f8829a4aSRandall Stewart 	}
1356f8829a4aSRandall Stewart 	/* Do we need to do some delivery? check */
1357f8829a4aSRandall Stewart 	sctp_deliver_reasm_check(stcb, asoc);
1358f8829a4aSRandall Stewart }
1359f8829a4aSRandall Stewart 
1360f8829a4aSRandall Stewart /*
1361f8829a4aSRandall Stewart  * This is an unfortunate routine. It checks to make sure a evil guy is not
1362f8829a4aSRandall Stewart  * stuffing us full of bad packet fragments. A broken peer could also do this
1363f8829a4aSRandall Stewart  * but this is doubtful. It is to bad I must worry about evil crackers sigh
1364f8829a4aSRandall Stewart  * :< more cycles.
1365f8829a4aSRandall Stewart  */
1366f8829a4aSRandall Stewart static int
1367f8829a4aSRandall Stewart sctp_does_tsn_belong_to_reasm(struct sctp_association *asoc,
1368f8829a4aSRandall Stewart     uint32_t TSN_seq)
1369f8829a4aSRandall Stewart {
1370f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *at;
1371f8829a4aSRandall Stewart 	uint32_t tsn_est;
1372f8829a4aSRandall Stewart 
1373f8829a4aSRandall Stewart 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
137420b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(TSN_seq, at->rec.data.TSN_seq)) {
1375f8829a4aSRandall Stewart 			/* is it one bigger? */
1376f8829a4aSRandall Stewart 			tsn_est = at->rec.data.TSN_seq + 1;
1377f8829a4aSRandall Stewart 			if (tsn_est == TSN_seq) {
1378f8829a4aSRandall Stewart 				/* yep. It better be a last then */
1379f8829a4aSRandall Stewart 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1380f8829a4aSRandall Stewart 				    SCTP_DATA_LAST_FRAG) {
1381f8829a4aSRandall Stewart 					/*
1382f8829a4aSRandall Stewart 					 * Ok this guy belongs next to a guy
1383f8829a4aSRandall Stewart 					 * that is NOT last, it should be a
1384f8829a4aSRandall Stewart 					 * middle/last, not a complete
1385f8829a4aSRandall Stewart 					 * chunk.
1386f8829a4aSRandall Stewart 					 */
1387f8829a4aSRandall Stewart 					return (1);
1388f8829a4aSRandall Stewart 				} else {
1389f8829a4aSRandall Stewart 					/*
1390f8829a4aSRandall Stewart 					 * This guy is ok since its a LAST
1391f8829a4aSRandall Stewart 					 * and the new chunk is a fully
1392f8829a4aSRandall Stewart 					 * self- contained one.
1393f8829a4aSRandall Stewart 					 */
1394f8829a4aSRandall Stewart 					return (0);
1395f8829a4aSRandall Stewart 				}
1396f8829a4aSRandall Stewart 			}
1397f8829a4aSRandall Stewart 		} else if (TSN_seq == at->rec.data.TSN_seq) {
1398f8829a4aSRandall Stewart 			/* Software error since I have a dup? */
1399f8829a4aSRandall Stewart 			return (1);
1400f8829a4aSRandall Stewart 		} else {
1401f8829a4aSRandall Stewart 			/*
1402f8829a4aSRandall Stewart 			 * Ok, 'at' is larger than new chunk but does it
1403f8829a4aSRandall Stewart 			 * need to be right before it.
1404f8829a4aSRandall Stewart 			 */
1405f8829a4aSRandall Stewart 			tsn_est = TSN_seq + 1;
1406f8829a4aSRandall Stewart 			if (tsn_est == at->rec.data.TSN_seq) {
1407f8829a4aSRandall Stewart 				/* Yep, It better be a first */
1408f8829a4aSRandall Stewart 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1409f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1410f8829a4aSRandall Stewart 					return (1);
1411f8829a4aSRandall Stewart 				} else {
1412f8829a4aSRandall Stewart 					return (0);
1413f8829a4aSRandall Stewart 				}
1414f8829a4aSRandall Stewart 			}
1415f8829a4aSRandall Stewart 		}
1416f8829a4aSRandall Stewart 	}
1417f8829a4aSRandall Stewart 	return (0);
1418f8829a4aSRandall Stewart }
1419f8829a4aSRandall Stewart 
1420f8829a4aSRandall Stewart 
1421f8829a4aSRandall Stewart static int
1422f8829a4aSRandall Stewart sctp_process_a_data_chunk(struct sctp_tcb *stcb, struct sctp_association *asoc,
1423f8829a4aSRandall Stewart     struct mbuf **m, int offset, struct sctp_data_chunk *ch, int chk_length,
1424f8829a4aSRandall Stewart     struct sctp_nets *net, uint32_t * high_tsn, int *abort_flag,
1425f8829a4aSRandall Stewart     int *break_flag, int last_chunk)
1426f8829a4aSRandall Stewart {
1427f8829a4aSRandall Stewart 	/* Process a data chunk */
1428f8829a4aSRandall Stewart 	/* struct sctp_tmit_chunk *chk; */
1429f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
1430f8829a4aSRandall Stewart 	uint32_t tsn, gap;
1431f8829a4aSRandall Stewart 	struct mbuf *dmbuf;
14327215cc1bSMichael Tuexen 	int the_len;
1433139bc87fSRandall Stewart 	int need_reasm_check = 0;
1434f8829a4aSRandall Stewart 	uint16_t strmno, strmseq;
1435f8829a4aSRandall Stewart 	struct mbuf *oper;
1436f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
1437f42a358aSRandall Stewart 	int ordered;
1438f42a358aSRandall Stewart 	uint32_t protocol_id;
1439f42a358aSRandall Stewart 	uint8_t chunk_flags;
144017205eccSRandall Stewart 	struct sctp_stream_reset_list *liste;
1441f8829a4aSRandall Stewart 
1442f8829a4aSRandall Stewart 	chk = NULL;
1443f8829a4aSRandall Stewart 	tsn = ntohl(ch->dp.tsn);
1444f42a358aSRandall Stewart 	chunk_flags = ch->ch.chunk_flags;
1445b3f1ea41SRandall Stewart 	if ((chunk_flags & SCTP_DATA_SACK_IMMEDIATELY) == SCTP_DATA_SACK_IMMEDIATELY) {
1446b3f1ea41SRandall Stewart 		asoc->send_sack = 1;
1447b3f1ea41SRandall Stewart 	}
1448f42a358aSRandall Stewart 	protocol_id = ch->dp.protocol_id;
144937f144ebSMichael Tuexen 	ordered = ((chunk_flags & SCTP_DATA_UNORDERED) == 0);
1450b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1451c4739e2fSRandall Stewart 		sctp_log_map(tsn, asoc->cumulative_tsn, asoc->highest_tsn_inside_map, SCTP_MAP_TSN_ENTERS);
145280fefe0aSRandall Stewart 	}
1453ad81507eSRandall Stewart 	if (stcb == NULL) {
1454ad81507eSRandall Stewart 		return (0);
1455ad81507eSRandall Stewart 	}
145680fefe0aSRandall Stewart 	SCTP_LTRACE_CHK(stcb->sctp_ep, stcb, ch->ch.chunk_type, tsn);
145720b07a4dSMichael Tuexen 	if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
1458f8829a4aSRandall Stewart 		/* It is a duplicate */
1459f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvdupdata);
1460f8829a4aSRandall Stewart 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1461f8829a4aSRandall Stewart 			/* Record a dup for the next outbound sack */
1462f8829a4aSRandall Stewart 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1463f8829a4aSRandall Stewart 			asoc->numduptsns++;
1464f8829a4aSRandall Stewart 		}
1465b201f536SRandall Stewart 		asoc->send_sack = 1;
1466f8829a4aSRandall Stewart 		return (0);
1467f8829a4aSRandall Stewart 	}
1468f8829a4aSRandall Stewart 	/* Calculate the number of TSN's between the base and this TSN */
1469d50c1d79SRandall Stewart 	SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
1470f8829a4aSRandall Stewart 	if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
1471f8829a4aSRandall Stewart 		/* Can't hold the bit in the mapping at max array, toss it */
1472f8829a4aSRandall Stewart 		return (0);
1473f8829a4aSRandall Stewart 	}
1474f8829a4aSRandall Stewart 	if (gap >= (uint32_t) (asoc->mapping_array_size << 3)) {
1475207304d4SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
14760696e120SRandall Stewart 		if (sctp_expand_mapping_array(asoc, gap)) {
1477f8829a4aSRandall Stewart 			/* Can't expand, drop it */
1478f8829a4aSRandall Stewart 			return (0);
1479f8829a4aSRandall Stewart 		}
1480f8829a4aSRandall Stewart 	}
148120b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(tsn, *high_tsn)) {
1482f8829a4aSRandall Stewart 		*high_tsn = tsn;
1483f8829a4aSRandall Stewart 	}
1484f8829a4aSRandall Stewart 	/* See if we have received this one already */
148577acdc25SRandall Stewart 	if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap) ||
148677acdc25SRandall Stewart 	    SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, gap)) {
1487f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvdupdata);
1488f8829a4aSRandall Stewart 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1489f8829a4aSRandall Stewart 			/* Record a dup for the next outbound sack */
1490f8829a4aSRandall Stewart 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1491f8829a4aSRandall Stewart 			asoc->numduptsns++;
1492f8829a4aSRandall Stewart 		}
149342551e99SRandall Stewart 		asoc->send_sack = 1;
1494f8829a4aSRandall Stewart 		return (0);
1495f8829a4aSRandall Stewart 	}
1496f8829a4aSRandall Stewart 	/*
1497f8829a4aSRandall Stewart 	 * Check to see about the GONE flag, duplicates would cause a sack
1498f8829a4aSRandall Stewart 	 * to be sent up above
1499f8829a4aSRandall Stewart 	 */
1500ad81507eSRandall Stewart 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1501f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1502f8829a4aSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET))
1503f8829a4aSRandall Stewart 	    ) {
1504f8829a4aSRandall Stewart 		/*
1505f8829a4aSRandall Stewart 		 * wait a minute, this guy is gone, there is no longer a
1506f8829a4aSRandall Stewart 		 * receiver. Send peer an ABORT!
1507f8829a4aSRandall Stewart 		 */
1508f8829a4aSRandall Stewart 		struct mbuf *op_err;
1509f8829a4aSRandall Stewart 
1510f8829a4aSRandall Stewart 		op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1511a2b42326SMichael Tuexen 		sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1512f8829a4aSRandall Stewart 		*abort_flag = 1;
1513f8829a4aSRandall Stewart 		return (0);
1514f8829a4aSRandall Stewart 	}
1515f8829a4aSRandall Stewart 	/*
1516f8829a4aSRandall Stewart 	 * Now before going further we see if there is room. If NOT then we
1517f8829a4aSRandall Stewart 	 * MAY let one through only IF this TSN is the one we are waiting
1518f8829a4aSRandall Stewart 	 * for on a partial delivery API.
1519f8829a4aSRandall Stewart 	 */
1520f8829a4aSRandall Stewart 
1521f8829a4aSRandall Stewart 	/* now do the tests */
1522f8829a4aSRandall Stewart 	if (((asoc->cnt_on_all_streams +
1523f8829a4aSRandall Stewart 	    asoc->cnt_on_reasm_queue +
1524b3f1ea41SRandall Stewart 	    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) ||
1525f8829a4aSRandall Stewart 	    (((int)asoc->my_rwnd) <= 0)) {
1526f8829a4aSRandall Stewart 		/*
1527f8829a4aSRandall Stewart 		 * When we have NO room in the rwnd we check to make sure
1528f8829a4aSRandall Stewart 		 * the reader is doing its job...
1529f8829a4aSRandall Stewart 		 */
1530f8829a4aSRandall Stewart 		if (stcb->sctp_socket->so_rcv.sb_cc) {
1531f8829a4aSRandall Stewart 			/* some to read, wake-up */
1532ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1533ceaad40aSRandall Stewart 			struct socket *so;
1534ceaad40aSRandall Stewart 
1535ceaad40aSRandall Stewart 			so = SCTP_INP_SO(stcb->sctp_ep);
1536ceaad40aSRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, 1);
1537ceaad40aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
1538ceaad40aSRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
1539ceaad40aSRandall Stewart 			SCTP_TCB_LOCK(stcb);
1540ceaad40aSRandall Stewart 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
1541ceaad40aSRandall Stewart 			if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1542ceaad40aSRandall Stewart 				/* assoc was freed while we were unlocked */
1543ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
1544ceaad40aSRandall Stewart 				return (0);
1545ceaad40aSRandall Stewart 			}
1546ceaad40aSRandall Stewart #endif
1547f8829a4aSRandall Stewart 			sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1548ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1549ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
1550ceaad40aSRandall Stewart #endif
1551f8829a4aSRandall Stewart 		}
1552f8829a4aSRandall Stewart 		/* now is it in the mapping array of what we have accepted? */
155320b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_map) &&
155420b07a4dSMichael Tuexen 		    SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1555f8829a4aSRandall Stewart 			/* Nope not in the valid range dump it */
1556f8829a4aSRandall Stewart 			sctp_set_rwnd(stcb, asoc);
1557f8829a4aSRandall Stewart 			if ((asoc->cnt_on_all_streams +
1558f8829a4aSRandall Stewart 			    asoc->cnt_on_reasm_queue +
1559b3f1ea41SRandall Stewart 			    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) {
1560f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_datadropchklmt);
1561f8829a4aSRandall Stewart 			} else {
1562f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_datadroprwnd);
1563f8829a4aSRandall Stewart 			}
1564f8829a4aSRandall Stewart 			*break_flag = 1;
1565f8829a4aSRandall Stewart 			return (0);
1566f8829a4aSRandall Stewart 		}
1567f8829a4aSRandall Stewart 	}
1568f8829a4aSRandall Stewart 	strmno = ntohs(ch->dp.stream_id);
1569f8829a4aSRandall Stewart 	if (strmno >= asoc->streamincnt) {
1570f8829a4aSRandall Stewart 		struct sctp_paramhdr *phdr;
1571f8829a4aSRandall Stewart 		struct mbuf *mb;
1572f8829a4aSRandall Stewart 
1573f8829a4aSRandall Stewart 		mb = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) * 2),
1574139bc87fSRandall Stewart 		    0, M_DONTWAIT, 1, MT_DATA);
1575f8829a4aSRandall Stewart 		if (mb != NULL) {
1576f8829a4aSRandall Stewart 			/* add some space up front so prepend will work well */
1577139bc87fSRandall Stewart 			SCTP_BUF_RESV_UF(mb, sizeof(struct sctp_chunkhdr));
1578f8829a4aSRandall Stewart 			phdr = mtod(mb, struct sctp_paramhdr *);
1579f8829a4aSRandall Stewart 			/*
1580f8829a4aSRandall Stewart 			 * Error causes are just param's and this one has
1581f8829a4aSRandall Stewart 			 * two back to back phdr, one with the error type
1582f8829a4aSRandall Stewart 			 * and size, the other with the streamid and a rsvd
1583f8829a4aSRandall Stewart 			 */
1584139bc87fSRandall Stewart 			SCTP_BUF_LEN(mb) = (sizeof(struct sctp_paramhdr) * 2);
1585f8829a4aSRandall Stewart 			phdr->param_type = htons(SCTP_CAUSE_INVALID_STREAM);
1586f8829a4aSRandall Stewart 			phdr->param_length =
1587f8829a4aSRandall Stewart 			    htons(sizeof(struct sctp_paramhdr) * 2);
1588f8829a4aSRandall Stewart 			phdr++;
1589f8829a4aSRandall Stewart 			/* We insert the stream in the type field */
1590f8829a4aSRandall Stewart 			phdr->param_type = ch->dp.stream_id;
1591f8829a4aSRandall Stewart 			/* And set the length to 0 for the rsvd field */
1592f8829a4aSRandall Stewart 			phdr->param_length = 0;
1593f8829a4aSRandall Stewart 			sctp_queue_op_err(stcb, mb);
1594f8829a4aSRandall Stewart 		}
1595f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_badsid);
1596207304d4SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1597830d754dSRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
159820b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1599830d754dSRandall Stewart 			asoc->highest_tsn_inside_nr_map = tsn;
1600d06c82f1SRandall Stewart 		}
1601d06c82f1SRandall Stewart 		if (tsn == (asoc->cumulative_tsn + 1)) {
1602d06c82f1SRandall Stewart 			/* Update cum-ack */
1603d06c82f1SRandall Stewart 			asoc->cumulative_tsn = tsn;
1604d06c82f1SRandall Stewart 		}
1605f8829a4aSRandall Stewart 		return (0);
1606f8829a4aSRandall Stewart 	}
1607f8829a4aSRandall Stewart 	/*
1608f8829a4aSRandall Stewart 	 * Before we continue lets validate that we are not being fooled by
1609f8829a4aSRandall Stewart 	 * an evil attacker. We can only have 4k chunks based on our TSN
1610f8829a4aSRandall Stewart 	 * spread allowed by the mapping array 512 * 8 bits, so there is no
1611f8829a4aSRandall Stewart 	 * way our stream sequence numbers could have wrapped. We of course
1612f8829a4aSRandall Stewart 	 * only validate the FIRST fragment so the bit must be set.
1613f8829a4aSRandall Stewart 	 */
1614f8829a4aSRandall Stewart 	strmseq = ntohs(ch->dp.stream_sequence);
1615f42a358aSRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
161618e198d3SRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
161718e198d3SRandall Stewart 	if (asoc->tsn_in_at >= SCTP_TSN_LOG_SIZE) {
161818e198d3SRandall Stewart 		asoc->tsn_in_at = 0;
161918e198d3SRandall Stewart 		asoc->tsn_in_wrapped = 1;
162018e198d3SRandall Stewart 	}
1621f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].tsn = tsn;
1622f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].strm = strmno;
1623f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].seq = strmseq;
1624f1f73e57SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].sz = chk_length;
1625f1f73e57SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].flgs = chunk_flags;
162618e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].stcb = (void *)stcb;
162718e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].in_pos = asoc->tsn_in_at;
162818e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].in_out = 1;
1629f42a358aSRandall Stewart 	asoc->tsn_in_at++;
1630f42a358aSRandall Stewart #endif
1631f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_FIRST_FRAG) &&
1632d61a0ae0SRandall Stewart 	    (TAILQ_EMPTY(&asoc->resetHead)) &&
1633f42a358aSRandall Stewart 	    (chunk_flags & SCTP_DATA_UNORDERED) == 0 &&
163420b07a4dSMichael Tuexen 	    SCTP_SSN_GE(asoc->strmin[strmno].last_sequence_delivered, strmseq)) {
1635f8829a4aSRandall Stewart 		/* The incoming sseq is behind where we last delivered? */
1636ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1, "EVIL/Broken-Dup S-SEQ:%d delivered:%d from peer, Abort!\n",
1637ad81507eSRandall Stewart 		    strmseq, asoc->strmin[strmno].last_sequence_delivered);
1638139bc87fSRandall Stewart 		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1639f8829a4aSRandall Stewart 		    0, M_DONTWAIT, 1, MT_DATA);
1640f8829a4aSRandall Stewart 		if (oper) {
1641f8829a4aSRandall Stewart 			struct sctp_paramhdr *ph;
1642f8829a4aSRandall Stewart 			uint32_t *ippp;
1643f8829a4aSRandall Stewart 
1644139bc87fSRandall Stewart 			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
1645f8829a4aSRandall Stewart 			    (3 * sizeof(uint32_t));
1646f8829a4aSRandall Stewart 			ph = mtod(oper, struct sctp_paramhdr *);
1647f8829a4aSRandall Stewart 			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1648139bc87fSRandall Stewart 			ph->param_length = htons(SCTP_BUF_LEN(oper));
1649f8829a4aSRandall Stewart 			ippp = (uint32_t *) (ph + 1);
1650a5d547adSRandall Stewart 			*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_14);
1651f8829a4aSRandall Stewart 			ippp++;
1652f8829a4aSRandall Stewart 			*ippp = tsn;
1653f8829a4aSRandall Stewart 			ippp++;
1654f8829a4aSRandall Stewart 			*ippp = ((strmno << 16) | strmseq);
1655f8829a4aSRandall Stewart 
1656f8829a4aSRandall Stewart 		}
1657a5d547adSRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_14;
1658a2b42326SMichael Tuexen 		sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
1659f8829a4aSRandall Stewart 		*abort_flag = 1;
1660f8829a4aSRandall Stewart 		return (0);
1661f8829a4aSRandall Stewart 	}
1662f42a358aSRandall Stewart 	/************************************
1663f42a358aSRandall Stewart 	 * From here down we may find ch-> invalid
1664f42a358aSRandall Stewart 	 * so its a good idea NOT to use it.
1665f42a358aSRandall Stewart 	 *************************************/
1666f42a358aSRandall Stewart 
1667f8829a4aSRandall Stewart 	the_len = (chk_length - sizeof(struct sctp_data_chunk));
1668f8829a4aSRandall Stewart 	if (last_chunk == 0) {
166944b7479bSRandall Stewart 		dmbuf = SCTP_M_COPYM(*m,
1670f8829a4aSRandall Stewart 		    (offset + sizeof(struct sctp_data_chunk)),
1671f8829a4aSRandall Stewart 		    the_len, M_DONTWAIT);
1672f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
1673b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
1674f8829a4aSRandall Stewart 			struct mbuf *mat;
1675f8829a4aSRandall Stewart 
167660990c0cSMichael Tuexen 			for (mat = dmbuf; mat; mat = SCTP_BUF_NEXT(mat)) {
1677139bc87fSRandall Stewart 				if (SCTP_BUF_IS_EXTENDED(mat)) {
1678f8829a4aSRandall Stewart 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
1679f8829a4aSRandall Stewart 				}
1680f8829a4aSRandall Stewart 			}
1681f8829a4aSRandall Stewart 		}
1682f8829a4aSRandall Stewart #endif
1683f8829a4aSRandall Stewart 	} else {
1684f8829a4aSRandall Stewart 		/* We can steal the last chunk */
1685139bc87fSRandall Stewart 		int l_len;
1686139bc87fSRandall Stewart 
1687f8829a4aSRandall Stewart 		dmbuf = *m;
1688f8829a4aSRandall Stewart 		/* lop off the top part */
1689f8829a4aSRandall Stewart 		m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
1690139bc87fSRandall Stewart 		if (SCTP_BUF_NEXT(dmbuf) == NULL) {
1691139bc87fSRandall Stewart 			l_len = SCTP_BUF_LEN(dmbuf);
1692139bc87fSRandall Stewart 		} else {
1693139bc87fSRandall Stewart 			/*
1694139bc87fSRandall Stewart 			 * need to count up the size hopefully does not hit
1695139bc87fSRandall Stewart 			 * this to often :-0
1696139bc87fSRandall Stewart 			 */
1697139bc87fSRandall Stewart 			struct mbuf *lat;
1698139bc87fSRandall Stewart 
1699139bc87fSRandall Stewart 			l_len = 0;
170060990c0cSMichael Tuexen 			for (lat = dmbuf; lat; lat = SCTP_BUF_NEXT(lat)) {
1701139bc87fSRandall Stewart 				l_len += SCTP_BUF_LEN(lat);
1702139bc87fSRandall Stewart 			}
1703139bc87fSRandall Stewart 		}
1704139bc87fSRandall Stewart 		if (l_len > the_len) {
1705f8829a4aSRandall Stewart 			/* Trim the end round bytes off  too */
1706139bc87fSRandall Stewart 			m_adj(dmbuf, -(l_len - the_len));
1707f8829a4aSRandall Stewart 		}
1708f8829a4aSRandall Stewart 	}
1709f8829a4aSRandall Stewart 	if (dmbuf == NULL) {
1710f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_nomem);
1711f8829a4aSRandall Stewart 		return (0);
1712f8829a4aSRandall Stewart 	}
1713f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
1714f8829a4aSRandall Stewart 	    asoc->fragmented_delivery_inprogress == 0 &&
1715f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->resetHead) &&
1716f42a358aSRandall Stewart 	    ((ordered == 0) ||
171736ec9f81SMichael Tuexen 	    ((uint16_t) (asoc->strmin[strmno].last_sequence_delivered + 1) == strmseq &&
1718f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->strmin[strmno].inqueue)))) {
1719f8829a4aSRandall Stewart 		/* Candidate for express delivery */
1720f8829a4aSRandall Stewart 		/*
1721f8829a4aSRandall Stewart 		 * Its not fragmented, No PD-API is up, Nothing in the
1722f8829a4aSRandall Stewart 		 * delivery queue, Its un-ordered OR ordered and the next to
1723f8829a4aSRandall Stewart 		 * deliver AND nothing else is stuck on the stream queue,
1724f8829a4aSRandall Stewart 		 * And there is room for it in the socket buffer. Lets just
1725f8829a4aSRandall Stewart 		 * stuff it up the buffer....
1726f8829a4aSRandall Stewart 		 */
1727f8829a4aSRandall Stewart 
1728f8829a4aSRandall Stewart 		/* It would be nice to avoid this copy if we could :< */
1729f8829a4aSRandall Stewart 		sctp_alloc_a_readq(stcb, control);
1730f8829a4aSRandall Stewart 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1731f42a358aSRandall Stewart 		    protocol_id,
1732f8829a4aSRandall Stewart 		    stcb->asoc.context,
1733f8829a4aSRandall Stewart 		    strmno, strmseq,
1734f42a358aSRandall Stewart 		    chunk_flags,
1735f8829a4aSRandall Stewart 		    dmbuf);
1736f8829a4aSRandall Stewart 		if (control == NULL) {
1737f8829a4aSRandall Stewart 			goto failed_express_del;
1738f8829a4aSRandall Stewart 		}
17391ea735c8SMichael Tuexen 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
174020b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
17411ea735c8SMichael Tuexen 			asoc->highest_tsn_inside_nr_map = tsn;
17421ea735c8SMichael Tuexen 		}
1743cfde3ff7SRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb,
1744cfde3ff7SRandall Stewart 		    control, &stcb->sctp_socket->so_rcv,
1745cfde3ff7SRandall Stewart 		    1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
1746830d754dSRandall Stewart 
1747f42a358aSRandall Stewart 		if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1748f8829a4aSRandall Stewart 			/* for ordered, bump what we delivered */
1749f8829a4aSRandall Stewart 			asoc->strmin[strmno].last_sequence_delivered++;
1750f8829a4aSRandall Stewart 		}
1751f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvexpress);
1752b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
17536a91f103SRandall Stewart 			sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno,
1754f8829a4aSRandall Stewart 			    SCTP_STR_LOG_FROM_EXPRS_DEL);
175580fefe0aSRandall Stewart 		}
1756f8829a4aSRandall Stewart 		control = NULL;
1757b5c16493SMichael Tuexen 
1758f8829a4aSRandall Stewart 		goto finish_express_del;
1759f8829a4aSRandall Stewart 	}
1760f8829a4aSRandall Stewart failed_express_del:
1761f8829a4aSRandall Stewart 	/* If we reach here this is a new chunk */
1762f8829a4aSRandall Stewart 	chk = NULL;
1763f8829a4aSRandall Stewart 	control = NULL;
1764f8829a4aSRandall Stewart 	/* Express for fragmented delivery? */
1765f8829a4aSRandall Stewart 	if ((asoc->fragmented_delivery_inprogress) &&
1766f8829a4aSRandall Stewart 	    (stcb->asoc.control_pdapi) &&
1767f8829a4aSRandall Stewart 	    (asoc->str_of_pdapi == strmno) &&
1768f8829a4aSRandall Stewart 	    (asoc->ssn_of_pdapi == strmseq)
1769f8829a4aSRandall Stewart 	    ) {
1770f8829a4aSRandall Stewart 		control = stcb->asoc.control_pdapi;
1771f42a358aSRandall Stewart 		if ((chunk_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1772f8829a4aSRandall Stewart 			/* Can't be another first? */
1773f8829a4aSRandall Stewart 			goto failed_pdapi_express_del;
1774f8829a4aSRandall Stewart 		}
1775f8829a4aSRandall Stewart 		if (tsn == (control->sinfo_tsn + 1)) {
1776f8829a4aSRandall Stewart 			/* Yep, we can add it on */
1777f8829a4aSRandall Stewart 			int end = 0;
1778f8829a4aSRandall Stewart 
1779f42a358aSRandall Stewart 			if (chunk_flags & SCTP_DATA_LAST_FRAG) {
1780f8829a4aSRandall Stewart 				end = 1;
1781f8829a4aSRandall Stewart 			}
1782f8829a4aSRandall Stewart 			if (sctp_append_to_readq(stcb->sctp_ep, stcb, control, dmbuf, end,
1783f8829a4aSRandall Stewart 			    tsn,
1784f8829a4aSRandall Stewart 			    &stcb->sctp_socket->so_rcv)) {
1785ad81507eSRandall Stewart 				SCTP_PRINTF("Append fails end:%d\n", end);
1786f8829a4aSRandall Stewart 				goto failed_pdapi_express_del;
1787f8829a4aSRandall Stewart 			}
178877acdc25SRandall Stewart 			SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
178920b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1790830d754dSRandall Stewart 				asoc->highest_tsn_inside_nr_map = tsn;
1791830d754dSRandall Stewart 			}
1792f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvexpressm);
1793f8829a4aSRandall Stewart 			control->sinfo_tsn = tsn;
1794f8829a4aSRandall Stewart 			asoc->tsn_last_delivered = tsn;
1795f42a358aSRandall Stewart 			asoc->fragment_flags = chunk_flags;
1796f8829a4aSRandall Stewart 			asoc->tsn_of_pdapi_last_delivered = tsn;
1797f42a358aSRandall Stewart 			asoc->last_flags_delivered = chunk_flags;
1798f8829a4aSRandall Stewart 			asoc->last_strm_seq_delivered = strmseq;
1799f8829a4aSRandall Stewart 			asoc->last_strm_no_delivered = strmno;
1800f8829a4aSRandall Stewart 			if (end) {
1801f8829a4aSRandall Stewart 				/* clean up the flags and such */
1802f8829a4aSRandall Stewart 				asoc->fragmented_delivery_inprogress = 0;
1803f42a358aSRandall Stewart 				if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1804f8829a4aSRandall Stewart 					asoc->strmin[strmno].last_sequence_delivered++;
1805a5d547adSRandall Stewart 				}
1806f8829a4aSRandall Stewart 				stcb->asoc.control_pdapi = NULL;
1807139bc87fSRandall Stewart 				if (TAILQ_EMPTY(&asoc->reasmqueue) == 0) {
1808139bc87fSRandall Stewart 					/*
1809139bc87fSRandall Stewart 					 * There could be another message
1810139bc87fSRandall Stewart 					 * ready
1811139bc87fSRandall Stewart 					 */
1812139bc87fSRandall Stewart 					need_reasm_check = 1;
1813139bc87fSRandall Stewart 				}
1814f8829a4aSRandall Stewart 			}
1815f8829a4aSRandall Stewart 			control = NULL;
1816f8829a4aSRandall Stewart 			goto finish_express_del;
1817f8829a4aSRandall Stewart 		}
1818f8829a4aSRandall Stewart 	}
1819f8829a4aSRandall Stewart failed_pdapi_express_del:
1820f8829a4aSRandall Stewart 	control = NULL;
182177acdc25SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
182277acdc25SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
182320b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
182477acdc25SRandall Stewart 			asoc->highest_tsn_inside_nr_map = tsn;
182577acdc25SRandall Stewart 		}
182677acdc25SRandall Stewart 	} else {
182777acdc25SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
182820b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_map)) {
182977acdc25SRandall Stewart 			asoc->highest_tsn_inside_map = tsn;
183077acdc25SRandall Stewart 		}
183177acdc25SRandall Stewart 	}
1832f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
1833f8829a4aSRandall Stewart 		sctp_alloc_a_chunk(stcb, chk);
1834f8829a4aSRandall Stewart 		if (chk == NULL) {
1835f8829a4aSRandall Stewart 			/* No memory so we drop the chunk */
1836f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_nomem);
1837f8829a4aSRandall Stewart 			if (last_chunk == 0) {
1838f8829a4aSRandall Stewart 				/* we copied it, free the copy */
1839f8829a4aSRandall Stewart 				sctp_m_freem(dmbuf);
1840f8829a4aSRandall Stewart 			}
1841f8829a4aSRandall Stewart 			return (0);
1842f8829a4aSRandall Stewart 		}
1843f8829a4aSRandall Stewart 		chk->rec.data.TSN_seq = tsn;
1844f8829a4aSRandall Stewart 		chk->no_fr_allowed = 0;
1845f8829a4aSRandall Stewart 		chk->rec.data.stream_seq = strmseq;
1846f8829a4aSRandall Stewart 		chk->rec.data.stream_number = strmno;
1847f42a358aSRandall Stewart 		chk->rec.data.payloadtype = protocol_id;
1848f8829a4aSRandall Stewart 		chk->rec.data.context = stcb->asoc.context;
1849f8829a4aSRandall Stewart 		chk->rec.data.doing_fast_retransmit = 0;
1850f42a358aSRandall Stewart 		chk->rec.data.rcv_flags = chunk_flags;
1851f8829a4aSRandall Stewart 		chk->asoc = asoc;
1852f8829a4aSRandall Stewart 		chk->send_size = the_len;
1853f8829a4aSRandall Stewart 		chk->whoTo = net;
1854f8829a4aSRandall Stewart 		atomic_add_int(&net->ref_count, 1);
1855f8829a4aSRandall Stewart 		chk->data = dmbuf;
1856f8829a4aSRandall Stewart 	} else {
1857f8829a4aSRandall Stewart 		sctp_alloc_a_readq(stcb, control);
1858f8829a4aSRandall Stewart 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1859f42a358aSRandall Stewart 		    protocol_id,
1860f8829a4aSRandall Stewart 		    stcb->asoc.context,
1861f8829a4aSRandall Stewart 		    strmno, strmseq,
1862f42a358aSRandall Stewart 		    chunk_flags,
1863f8829a4aSRandall Stewart 		    dmbuf);
1864f8829a4aSRandall Stewart 		if (control == NULL) {
1865f8829a4aSRandall Stewart 			/* No memory so we drop the chunk */
1866f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_nomem);
1867f8829a4aSRandall Stewart 			if (last_chunk == 0) {
1868f8829a4aSRandall Stewart 				/* we copied it, free the copy */
1869f8829a4aSRandall Stewart 				sctp_m_freem(dmbuf);
1870f8829a4aSRandall Stewart 			}
1871f8829a4aSRandall Stewart 			return (0);
1872f8829a4aSRandall Stewart 		}
1873f8829a4aSRandall Stewart 		control->length = the_len;
1874f8829a4aSRandall Stewart 	}
1875f8829a4aSRandall Stewart 
1876f8829a4aSRandall Stewart 	/* Mark it as received */
1877f8829a4aSRandall Stewart 	/* Now queue it where it belongs */
1878f8829a4aSRandall Stewart 	if (control != NULL) {
1879f8829a4aSRandall Stewart 		/* First a sanity check */
1880f8829a4aSRandall Stewart 		if (asoc->fragmented_delivery_inprogress) {
1881f8829a4aSRandall Stewart 			/*
1882f8829a4aSRandall Stewart 			 * Ok, we have a fragmented delivery in progress if
1883f8829a4aSRandall Stewart 			 * this chunk is next to deliver OR belongs in our
1884f8829a4aSRandall Stewart 			 * view to the reassembly, the peer is evil or
1885f8829a4aSRandall Stewart 			 * broken.
1886f8829a4aSRandall Stewart 			 */
1887f8829a4aSRandall Stewart 			uint32_t estimate_tsn;
1888f8829a4aSRandall Stewart 
1889f8829a4aSRandall Stewart 			estimate_tsn = asoc->tsn_last_delivered + 1;
1890f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->reasmqueue) &&
1891f8829a4aSRandall Stewart 			    (estimate_tsn == control->sinfo_tsn)) {
1892f8829a4aSRandall Stewart 				/* Evil/Broke peer */
1893f8829a4aSRandall Stewart 				sctp_m_freem(control->data);
1894f8829a4aSRandall Stewart 				control->data = NULL;
18955bead436SRandall Stewart 				if (control->whoFrom) {
1896f8829a4aSRandall Stewart 					sctp_free_remote_addr(control->whoFrom);
18975bead436SRandall Stewart 					control->whoFrom = NULL;
18985bead436SRandall Stewart 				}
1899f8829a4aSRandall Stewart 				sctp_free_a_readq(stcb, control);
1900139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1901f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
1902f8829a4aSRandall Stewart 				if (oper) {
1903f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
1904f8829a4aSRandall Stewart 					uint32_t *ippp;
1905f8829a4aSRandall Stewart 
1906139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
1907f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
1908f8829a4aSRandall Stewart 					    (3 * sizeof(uint32_t));
1909f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
1910f8829a4aSRandall Stewart 					ph->param_type =
1911f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1912139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
1913f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
1914a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_15);
1915f8829a4aSRandall Stewart 					ippp++;
1916f8829a4aSRandall Stewart 					*ippp = tsn;
1917f8829a4aSRandall Stewart 					ippp++;
1918f8829a4aSRandall Stewart 					*ippp = ((strmno << 16) | strmseq);
1919f8829a4aSRandall Stewart 				}
1920a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_15;
1921a2b42326SMichael Tuexen 				sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
1922f8829a4aSRandall Stewart 				*abort_flag = 1;
1923f8829a4aSRandall Stewart 				return (0);
1924f8829a4aSRandall Stewart 			} else {
1925f8829a4aSRandall Stewart 				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1926f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
1927f8829a4aSRandall Stewart 					control->data = NULL;
19285bead436SRandall Stewart 					if (control->whoFrom) {
1929f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
19305bead436SRandall Stewart 						control->whoFrom = NULL;
19315bead436SRandall Stewart 					}
1932f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
1933f8829a4aSRandall Stewart 
1934139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1935f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1936f8829a4aSRandall Stewart 					if (oper) {
1937f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1938f8829a4aSRandall Stewart 						uint32_t *ippp;
1939f8829a4aSRandall Stewart 
1940139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1941f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1942f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1943f8829a4aSRandall Stewart 						ph = mtod(oper,
1944f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1945f8829a4aSRandall Stewart 						ph->param_type =
1946f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1947f8829a4aSRandall Stewart 						ph->param_length =
1948139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1949f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1950a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_16);
1951f8829a4aSRandall Stewart 						ippp++;
1952f8829a4aSRandall Stewart 						*ippp = tsn;
1953f8829a4aSRandall Stewart 						ippp++;
1954f8829a4aSRandall Stewart 						*ippp = ((strmno << 16) | strmseq);
1955f8829a4aSRandall Stewart 					}
1956a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_16;
1957a2b42326SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
1958f8829a4aSRandall Stewart 					*abort_flag = 1;
1959f8829a4aSRandall Stewart 					return (0);
1960f8829a4aSRandall Stewart 				}
1961f8829a4aSRandall Stewart 			}
1962f8829a4aSRandall Stewart 		} else {
1963f8829a4aSRandall Stewart 			/* No PDAPI running */
1964f8829a4aSRandall Stewart 			if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
1965f8829a4aSRandall Stewart 				/*
1966f8829a4aSRandall Stewart 				 * Reassembly queue is NOT empty validate
1967f8829a4aSRandall Stewart 				 * that this tsn does not need to be in
1968f8829a4aSRandall Stewart 				 * reasembly queue. If it does then our peer
1969f8829a4aSRandall Stewart 				 * is broken or evil.
1970f8829a4aSRandall Stewart 				 */
1971f8829a4aSRandall Stewart 				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1972f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
1973f8829a4aSRandall Stewart 					control->data = NULL;
19745bead436SRandall Stewart 					if (control->whoFrom) {
1975f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
19765bead436SRandall Stewart 						control->whoFrom = NULL;
19775bead436SRandall Stewart 					}
1978f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
1979139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1980f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1981f8829a4aSRandall Stewart 					if (oper) {
1982f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1983f8829a4aSRandall Stewart 						uint32_t *ippp;
1984f8829a4aSRandall Stewart 
1985139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1986f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1987f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1988f8829a4aSRandall Stewart 						ph = mtod(oper,
1989f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1990f8829a4aSRandall Stewart 						ph->param_type =
1991f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1992f8829a4aSRandall Stewart 						ph->param_length =
1993139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1994f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1995a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_17);
1996f8829a4aSRandall Stewart 						ippp++;
1997f8829a4aSRandall Stewart 						*ippp = tsn;
1998f8829a4aSRandall Stewart 						ippp++;
1999f8829a4aSRandall Stewart 						*ippp = ((strmno << 16) | strmseq);
2000f8829a4aSRandall Stewart 					}
2001a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_17;
2002a2b42326SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
2003f8829a4aSRandall Stewart 					*abort_flag = 1;
2004f8829a4aSRandall Stewart 					return (0);
2005f8829a4aSRandall Stewart 				}
2006f8829a4aSRandall Stewart 			}
2007f8829a4aSRandall Stewart 		}
2008f8829a4aSRandall Stewart 		/* ok, if we reach here we have passed the sanity checks */
2009f42a358aSRandall Stewart 		if (chunk_flags & SCTP_DATA_UNORDERED) {
2010f8829a4aSRandall Stewart 			/* queue directly into socket buffer */
2011b5c16493SMichael Tuexen 			sctp_mark_non_revokable(asoc, control->sinfo_tsn);
2012f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep, stcb,
2013f8829a4aSRandall Stewart 			    control,
2014cfde3ff7SRandall Stewart 			    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
2015f8829a4aSRandall Stewart 		} else {
2016f8829a4aSRandall Stewart 			/*
2017f8829a4aSRandall Stewart 			 * Special check for when streams are resetting. We
2018f8829a4aSRandall Stewart 			 * could be more smart about this and check the
2019f8829a4aSRandall Stewart 			 * actual stream to see if it is not being reset..
2020f8829a4aSRandall Stewart 			 * that way we would not create a HOLB when amongst
2021f8829a4aSRandall Stewart 			 * streams being reset and those not being reset.
2022f8829a4aSRandall Stewart 			 *
2023f8829a4aSRandall Stewart 			 * We take complete messages that have a stream reset
2024f8829a4aSRandall Stewart 			 * intervening (aka the TSN is after where our
2025f8829a4aSRandall Stewart 			 * cum-ack needs to be) off and put them on a
2026f8829a4aSRandall Stewart 			 * pending_reply_queue. The reassembly ones we do
2027f8829a4aSRandall Stewart 			 * not have to worry about since they are all sorted
2028f8829a4aSRandall Stewart 			 * and proceessed by TSN order. It is only the
2029f8829a4aSRandall Stewart 			 * singletons I must worry about.
2030f8829a4aSRandall Stewart 			 */
2031f8829a4aSRandall Stewart 			if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
203220b07a4dSMichael Tuexen 			    SCTP_TSN_GT(tsn, liste->tsn)) {
2033f8829a4aSRandall Stewart 				/*
2034f8829a4aSRandall Stewart 				 * yep its past where we need to reset... go
2035f8829a4aSRandall Stewart 				 * ahead and queue it.
2036f8829a4aSRandall Stewart 				 */
2037f8829a4aSRandall Stewart 				if (TAILQ_EMPTY(&asoc->pending_reply_queue)) {
2038f8829a4aSRandall Stewart 					/* first one on */
2039f8829a4aSRandall Stewart 					TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2040f8829a4aSRandall Stewart 				} else {
20414a9ef3f8SMichael Tuexen 					struct sctp_queued_to_read *ctlOn,
20424a9ef3f8SMichael Tuexen 					                   *nctlOn;
2043f8829a4aSRandall Stewart 					unsigned char inserted = 0;
2044f8829a4aSRandall Stewart 
20454a9ef3f8SMichael Tuexen 					TAILQ_FOREACH_SAFE(ctlOn, &asoc->pending_reply_queue, next, nctlOn) {
204620b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(control->sinfo_tsn, ctlOn->sinfo_tsn)) {
20474a9ef3f8SMichael Tuexen 							continue;
2048f8829a4aSRandall Stewart 						} else {
2049f8829a4aSRandall Stewart 							/* found it */
2050f8829a4aSRandall Stewart 							TAILQ_INSERT_BEFORE(ctlOn, control, next);
2051f8829a4aSRandall Stewart 							inserted = 1;
2052f8829a4aSRandall Stewart 							break;
2053f8829a4aSRandall Stewart 						}
2054f8829a4aSRandall Stewart 					}
2055f8829a4aSRandall Stewart 					if (inserted == 0) {
2056f8829a4aSRandall Stewart 						/*
2057f8829a4aSRandall Stewart 						 * must be put at end, use
2058f8829a4aSRandall Stewart 						 * prevP (all setup from
2059f8829a4aSRandall Stewart 						 * loop) to setup nextP.
2060f8829a4aSRandall Stewart 						 */
2061f8829a4aSRandall Stewart 						TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2062f8829a4aSRandall Stewart 					}
2063f8829a4aSRandall Stewart 				}
2064f8829a4aSRandall Stewart 			} else {
2065f8829a4aSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, control, abort_flag);
2066f8829a4aSRandall Stewart 				if (*abort_flag) {
2067f8829a4aSRandall Stewart 					return (0);
2068f8829a4aSRandall Stewart 				}
2069f8829a4aSRandall Stewart 			}
2070f8829a4aSRandall Stewart 		}
2071f8829a4aSRandall Stewart 	} else {
2072f8829a4aSRandall Stewart 		/* Into the re-assembly queue */
2073f8829a4aSRandall Stewart 		sctp_queue_data_for_reasm(stcb, asoc, chk, abort_flag);
2074f8829a4aSRandall Stewart 		if (*abort_flag) {
2075a5d547adSRandall Stewart 			/*
2076a5d547adSRandall Stewart 			 * the assoc is now gone and chk was put onto the
2077a5d547adSRandall Stewart 			 * reasm queue, which has all been freed.
2078a5d547adSRandall Stewart 			 */
2079a5d547adSRandall Stewart 			*m = NULL;
2080f8829a4aSRandall Stewart 			return (0);
2081f8829a4aSRandall Stewart 		}
2082f8829a4aSRandall Stewart 	}
2083f8829a4aSRandall Stewart finish_express_del:
2084307b49efSMichael Tuexen 	if (tsn == (asoc->cumulative_tsn + 1)) {
2085307b49efSMichael Tuexen 		/* Update cum-ack */
2086307b49efSMichael Tuexen 		asoc->cumulative_tsn = tsn;
2087307b49efSMichael Tuexen 	}
2088f8829a4aSRandall Stewart 	if (last_chunk) {
2089f8829a4aSRandall Stewart 		*m = NULL;
2090f8829a4aSRandall Stewart 	}
2091f42a358aSRandall Stewart 	if (ordered) {
2092f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_inorderchunks);
2093f8829a4aSRandall Stewart 	} else {
2094f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_inunorderchunks);
2095f8829a4aSRandall Stewart 	}
2096f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvdata);
2097f8829a4aSRandall Stewart 	/* Set it present please */
2098b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
20996a91f103SRandall Stewart 		sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno, SCTP_STR_LOG_FROM_MARK_TSN);
210080fefe0aSRandall Stewart 	}
2101b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2102f8829a4aSRandall Stewart 		sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2103f8829a4aSRandall Stewart 		    asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
210480fefe0aSRandall Stewart 	}
210517205eccSRandall Stewart 	/* check the special flag for stream resets */
210617205eccSRandall Stewart 	if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
210720b07a4dSMichael Tuexen 	    SCTP_TSN_GE(asoc->cumulative_tsn, liste->tsn)) {
210817205eccSRandall Stewart 		/*
210917205eccSRandall Stewart 		 * we have finished working through the backlogged TSN's now
211017205eccSRandall Stewart 		 * time to reset streams. 1: call reset function. 2: free
211117205eccSRandall Stewart 		 * pending_reply space 3: distribute any chunks in
211217205eccSRandall Stewart 		 * pending_reply_queue.
211317205eccSRandall Stewart 		 */
21144a9ef3f8SMichael Tuexen 		struct sctp_queued_to_read *ctl, *nctl;
211517205eccSRandall Stewart 
211617205eccSRandall Stewart 		sctp_reset_in_stream(stcb, liste->number_entries, liste->req.list_of_streams);
211717205eccSRandall Stewart 		TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
2118207304d4SRandall Stewart 		SCTP_FREE(liste, SCTP_M_STRESET);
21193c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
212017205eccSRandall Stewart 		liste = TAILQ_FIRST(&asoc->resetHead);
21214a9ef3f8SMichael Tuexen 		if (TAILQ_EMPTY(&asoc->resetHead)) {
212217205eccSRandall Stewart 			/* All can be removed */
21234a9ef3f8SMichael Tuexen 			TAILQ_FOREACH_SAFE(ctl, &asoc->pending_reply_queue, next, nctl) {
212417205eccSRandall Stewart 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
212517205eccSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
212617205eccSRandall Stewart 				if (*abort_flag) {
212717205eccSRandall Stewart 					return (0);
212817205eccSRandall Stewart 				}
212917205eccSRandall Stewart 			}
21304a9ef3f8SMichael Tuexen 		} else {
21314a9ef3f8SMichael Tuexen 			TAILQ_FOREACH_SAFE(ctl, &asoc->pending_reply_queue, next, nctl) {
213220b07a4dSMichael Tuexen 				if (SCTP_TSN_GT(ctl->sinfo_tsn, liste->tsn)) {
21334a9ef3f8SMichael Tuexen 					break;
21344a9ef3f8SMichael Tuexen 				}
213517205eccSRandall Stewart 				/*
213617205eccSRandall Stewart 				 * if ctl->sinfo_tsn is <= liste->tsn we can
213717205eccSRandall Stewart 				 * process it which is the NOT of
213817205eccSRandall Stewart 				 * ctl->sinfo_tsn > liste->tsn
213917205eccSRandall Stewart 				 */
214017205eccSRandall Stewart 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
214117205eccSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
214217205eccSRandall Stewart 				if (*abort_flag) {
214317205eccSRandall Stewart 					return (0);
214417205eccSRandall Stewart 				}
214517205eccSRandall Stewart 			}
214617205eccSRandall Stewart 		}
214717205eccSRandall Stewart 		/*
214817205eccSRandall Stewart 		 * Now service re-assembly to pick up anything that has been
214917205eccSRandall Stewart 		 * held on reassembly queue?
215017205eccSRandall Stewart 		 */
215117205eccSRandall Stewart 		sctp_deliver_reasm_check(stcb, asoc);
215217205eccSRandall Stewart 		need_reasm_check = 0;
215317205eccSRandall Stewart 	}
2154139bc87fSRandall Stewart 	if (need_reasm_check) {
2155139bc87fSRandall Stewart 		/* Another one waits ? */
2156139bc87fSRandall Stewart 		sctp_deliver_reasm_check(stcb, asoc);
2157139bc87fSRandall Stewart 	}
2158f8829a4aSRandall Stewart 	return (1);
2159f8829a4aSRandall Stewart }
2160f8829a4aSRandall Stewart 
2161f8829a4aSRandall Stewart int8_t sctp_map_lookup_tab[256] = {
2162b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2163b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2164b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2165b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
2166b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2167b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2168b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2169b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 6,
2170b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2171b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2172b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2173b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
2174b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2175b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2176b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2177b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 7,
2178b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2179b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2180b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2181b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
2182b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2183b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2184b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2185b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 6,
2186b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2187b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2188b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2189b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
2190b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2191b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2192b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2193b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 8
2194f8829a4aSRandall Stewart };
2195f8829a4aSRandall Stewart 
2196f8829a4aSRandall Stewart 
2197f8829a4aSRandall Stewart void
2198b5c16493SMichael Tuexen sctp_slide_mapping_arrays(struct sctp_tcb *stcb)
2199f8829a4aSRandall Stewart {
2200f8829a4aSRandall Stewart 	/*
2201f8829a4aSRandall Stewart 	 * Now we also need to check the mapping array in a couple of ways.
2202f8829a4aSRandall Stewart 	 * 1) Did we move the cum-ack point?
220366bd30bdSRandall Stewart 	 *
220466bd30bdSRandall Stewart 	 * When you first glance at this you might think that all entries that
220566bd30bdSRandall Stewart 	 * make up the postion of the cum-ack would be in the nr-mapping
220666bd30bdSRandall Stewart 	 * array only.. i.e. things up to the cum-ack are always
220766bd30bdSRandall Stewart 	 * deliverable. Thats true with one exception, when its a fragmented
220866bd30bdSRandall Stewart 	 * message we may not deliver the data until some threshold (or all
220966bd30bdSRandall Stewart 	 * of it) is in place. So we must OR the nr_mapping_array and
221066bd30bdSRandall Stewart 	 * mapping_array to get a true picture of the cum-ack.
2211f8829a4aSRandall Stewart 	 */
2212f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2213b3f1ea41SRandall Stewart 	int at;
221466bd30bdSRandall Stewart 	uint8_t val;
2215f8829a4aSRandall Stewart 	int slide_from, slide_end, lgap, distance;
221677acdc25SRandall Stewart 	uint32_t old_cumack, old_base, old_highest, highest_tsn;
2217f8829a4aSRandall Stewart 
2218f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2219f8829a4aSRandall Stewart 
2220f8829a4aSRandall Stewart 	old_cumack = asoc->cumulative_tsn;
2221f8829a4aSRandall Stewart 	old_base = asoc->mapping_array_base_tsn;
2222f8829a4aSRandall Stewart 	old_highest = asoc->highest_tsn_inside_map;
2223f8829a4aSRandall Stewart 	/*
2224f8829a4aSRandall Stewart 	 * We could probably improve this a small bit by calculating the
2225f8829a4aSRandall Stewart 	 * offset of the current cum-ack as the starting point.
2226f8829a4aSRandall Stewart 	 */
2227f8829a4aSRandall Stewart 	at = 0;
2228b5c16493SMichael Tuexen 	for (slide_from = 0; slide_from < stcb->asoc.mapping_array_size; slide_from++) {
222966bd30bdSRandall Stewart 		val = asoc->nr_mapping_array[slide_from] | asoc->mapping_array[slide_from];
223066bd30bdSRandall Stewart 		if (val == 0xff) {
2231f8829a4aSRandall Stewart 			at += 8;
2232f8829a4aSRandall Stewart 		} else {
2233f8829a4aSRandall Stewart 			/* there is a 0 bit */
223466bd30bdSRandall Stewart 			at += sctp_map_lookup_tab[val];
2235f8829a4aSRandall Stewart 			break;
2236f8829a4aSRandall Stewart 		}
2237f8829a4aSRandall Stewart 	}
2238b5c16493SMichael Tuexen 	asoc->cumulative_tsn = asoc->mapping_array_base_tsn + (at - 1);
2239f8829a4aSRandall Stewart 
224020b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->cumulative_tsn, asoc->highest_tsn_inside_map) &&
224120b07a4dSMichael Tuexen 	    SCTP_TSN_GT(asoc->cumulative_tsn, asoc->highest_tsn_inside_nr_map)) {
2242a5d547adSRandall Stewart #ifdef INVARIANTS
2243ceaad40aSRandall Stewart 		panic("huh, cumack 0x%x greater than high-tsn 0x%x in map",
2244ceaad40aSRandall Stewart 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2245f8829a4aSRandall Stewart #else
2246ceaad40aSRandall Stewart 		SCTP_PRINTF("huh, cumack 0x%x greater than high-tsn 0x%x in map - should panic?\n",
2247ceaad40aSRandall Stewart 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
22480e13104dSRandall Stewart 		sctp_print_mapping_array(asoc);
2249b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2250b3f1ea41SRandall Stewart 			sctp_log_map(0, 6, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
2251b3f1ea41SRandall Stewart 		}
2252f8829a4aSRandall Stewart 		asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2253830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = asoc->cumulative_tsn;
2254f8829a4aSRandall Stewart #endif
2255f8829a4aSRandall Stewart 	}
225620b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
225777acdc25SRandall Stewart 		highest_tsn = asoc->highest_tsn_inside_nr_map;
225877acdc25SRandall Stewart 	} else {
225977acdc25SRandall Stewart 		highest_tsn = asoc->highest_tsn_inside_map;
226077acdc25SRandall Stewart 	}
226177acdc25SRandall Stewart 	if ((asoc->cumulative_tsn == highest_tsn) && (at >= 8)) {
2262f8829a4aSRandall Stewart 		/* The complete array was completed by a single FR */
226377acdc25SRandall Stewart 		/* highest becomes the cum-ack */
226437f144ebSMichael Tuexen 		int clr;
226537f144ebSMichael Tuexen 
226637f144ebSMichael Tuexen #ifdef INVARIANTS
226737f144ebSMichael Tuexen 		unsigned int i;
226837f144ebSMichael Tuexen 
226937f144ebSMichael Tuexen #endif
2270f8829a4aSRandall Stewart 
2271f8829a4aSRandall Stewart 		/* clear the array */
2272b5c16493SMichael Tuexen 		clr = ((at + 7) >> 3);
2273c4739e2fSRandall Stewart 		if (clr > asoc->mapping_array_size) {
2274f8829a4aSRandall Stewart 			clr = asoc->mapping_array_size;
2275f8829a4aSRandall Stewart 		}
2276f8829a4aSRandall Stewart 		memset(asoc->mapping_array, 0, clr);
2277830d754dSRandall Stewart 		memset(asoc->nr_mapping_array, 0, clr);
227837f144ebSMichael Tuexen #ifdef INVARIANTS
2279b5c16493SMichael Tuexen 		for (i = 0; i < asoc->mapping_array_size; i++) {
2280b5c16493SMichael Tuexen 			if ((asoc->mapping_array[i]) || (asoc->nr_mapping_array[i])) {
2281cd3fd531SMichael Tuexen 				SCTP_PRINTF("Error Mapping array's not clean at clear\n");
2282b5c16493SMichael Tuexen 				sctp_print_mapping_array(asoc);
2283b5c16493SMichael Tuexen 			}
2284b5c16493SMichael Tuexen 		}
228537f144ebSMichael Tuexen #endif
228677acdc25SRandall Stewart 		asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
228777acdc25SRandall Stewart 		asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2288f8829a4aSRandall Stewart 	} else if (at >= 8) {
2289f8829a4aSRandall Stewart 		/* we can slide the mapping array down */
2290b3f1ea41SRandall Stewart 		/* slide_from holds where we hit the first NON 0xff byte */
2291b3f1ea41SRandall Stewart 
2292f8829a4aSRandall Stewart 		/*
2293f8829a4aSRandall Stewart 		 * now calculate the ceiling of the move using our highest
2294f8829a4aSRandall Stewart 		 * TSN value
2295f8829a4aSRandall Stewart 		 */
229677acdc25SRandall Stewart 		SCTP_CALC_TSN_TO_GAP(lgap, highest_tsn, asoc->mapping_array_base_tsn);
229777acdc25SRandall Stewart 		slide_end = (lgap >> 3);
2298f8829a4aSRandall Stewart 		if (slide_end < slide_from) {
229977acdc25SRandall Stewart 			sctp_print_mapping_array(asoc);
2300d55b0b1bSRandall Stewart #ifdef INVARIANTS
2301f8829a4aSRandall Stewart 			panic("impossible slide");
2302d55b0b1bSRandall Stewart #else
2303cd3fd531SMichael Tuexen 			SCTP_PRINTF("impossible slide lgap:%x slide_end:%x slide_from:%x? at:%d\n",
230477acdc25SRandall Stewart 			    lgap, slide_end, slide_from, at);
2305d55b0b1bSRandall Stewart 			return;
2306d55b0b1bSRandall Stewart #endif
2307f8829a4aSRandall Stewart 		}
2308b3f1ea41SRandall Stewart 		if (slide_end > asoc->mapping_array_size) {
2309b3f1ea41SRandall Stewart #ifdef INVARIANTS
2310b3f1ea41SRandall Stewart 			panic("would overrun buffer");
2311b3f1ea41SRandall Stewart #else
2312cd3fd531SMichael Tuexen 			SCTP_PRINTF("Gak, would have overrun map end:%d slide_end:%d\n",
2313b3f1ea41SRandall Stewart 			    asoc->mapping_array_size, slide_end);
2314b3f1ea41SRandall Stewart 			slide_end = asoc->mapping_array_size;
2315b3f1ea41SRandall Stewart #endif
2316b3f1ea41SRandall Stewart 		}
2317f8829a4aSRandall Stewart 		distance = (slide_end - slide_from) + 1;
2318b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2319f8829a4aSRandall Stewart 			sctp_log_map(old_base, old_cumack, old_highest,
2320f8829a4aSRandall Stewart 			    SCTP_MAP_PREPARE_SLIDE);
2321f8829a4aSRandall Stewart 			sctp_log_map((uint32_t) slide_from, (uint32_t) slide_end,
2322f8829a4aSRandall Stewart 			    (uint32_t) lgap, SCTP_MAP_SLIDE_FROM);
232380fefe0aSRandall Stewart 		}
2324f8829a4aSRandall Stewart 		if (distance + slide_from > asoc->mapping_array_size ||
2325f8829a4aSRandall Stewart 		    distance < 0) {
2326f8829a4aSRandall Stewart 			/*
2327f8829a4aSRandall Stewart 			 * Here we do NOT slide forward the array so that
2328f8829a4aSRandall Stewart 			 * hopefully when more data comes in to fill it up
2329f8829a4aSRandall Stewart 			 * we will be able to slide it forward. Really I
2330f8829a4aSRandall Stewart 			 * don't think this should happen :-0
2331f8829a4aSRandall Stewart 			 */
2332f8829a4aSRandall Stewart 
2333b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2334f8829a4aSRandall Stewart 				sctp_log_map((uint32_t) distance, (uint32_t) slide_from,
2335f8829a4aSRandall Stewart 				    (uint32_t) asoc->mapping_array_size,
2336f8829a4aSRandall Stewart 				    SCTP_MAP_SLIDE_NONE);
233780fefe0aSRandall Stewart 			}
2338f8829a4aSRandall Stewart 		} else {
2339f8829a4aSRandall Stewart 			int ii;
2340f8829a4aSRandall Stewart 
2341f8829a4aSRandall Stewart 			for (ii = 0; ii < distance; ii++) {
234237f144ebSMichael Tuexen 				asoc->mapping_array[ii] = asoc->mapping_array[slide_from + ii];
234337f144ebSMichael Tuexen 				asoc->nr_mapping_array[ii] = asoc->nr_mapping_array[slide_from + ii];
234477acdc25SRandall Stewart 
2345f8829a4aSRandall Stewart 			}
2346aed5947cSMichael Tuexen 			for (ii = distance; ii < asoc->mapping_array_size; ii++) {
2347f8829a4aSRandall Stewart 				asoc->mapping_array[ii] = 0;
234877acdc25SRandall Stewart 				asoc->nr_mapping_array[ii] = 0;
2349f8829a4aSRandall Stewart 			}
2350ee94f0a2SMichael Tuexen 			if (asoc->highest_tsn_inside_map + 1 == asoc->mapping_array_base_tsn) {
2351ee94f0a2SMichael Tuexen 				asoc->highest_tsn_inside_map += (slide_from << 3);
2352ee94f0a2SMichael Tuexen 			}
2353ee94f0a2SMichael Tuexen 			if (asoc->highest_tsn_inside_nr_map + 1 == asoc->mapping_array_base_tsn) {
2354ee94f0a2SMichael Tuexen 				asoc->highest_tsn_inside_nr_map += (slide_from << 3);
2355ee94f0a2SMichael Tuexen 			}
2356f8829a4aSRandall Stewart 			asoc->mapping_array_base_tsn += (slide_from << 3);
2357b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2358f8829a4aSRandall Stewart 				sctp_log_map(asoc->mapping_array_base_tsn,
2359f8829a4aSRandall Stewart 				    asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
2360f8829a4aSRandall Stewart 				    SCTP_MAP_SLIDE_RESULT);
236180fefe0aSRandall Stewart 			}
2362830d754dSRandall Stewart 		}
2363830d754dSRandall Stewart 	}
2364b5c16493SMichael Tuexen }
2365b5c16493SMichael Tuexen 
2366b5c16493SMichael Tuexen void
23677215cc1bSMichael Tuexen sctp_sack_check(struct sctp_tcb *stcb, int was_a_gap)
2368b5c16493SMichael Tuexen {
2369b5c16493SMichael Tuexen 	struct sctp_association *asoc;
2370b5c16493SMichael Tuexen 	uint32_t highest_tsn;
2371b5c16493SMichael Tuexen 
2372b5c16493SMichael Tuexen 	asoc = &stcb->asoc;
237320b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
2374b5c16493SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_nr_map;
2375b5c16493SMichael Tuexen 	} else {
2376b5c16493SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_map;
2377b5c16493SMichael Tuexen 	}
2378b5c16493SMichael Tuexen 
2379830d754dSRandall Stewart 	/*
2380f8829a4aSRandall Stewart 	 * Now we need to see if we need to queue a sack or just start the
2381f8829a4aSRandall Stewart 	 * timer (if allowed).
2382f8829a4aSRandall Stewart 	 */
2383f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2384f8829a4aSRandall Stewart 		/*
2385b5c16493SMichael Tuexen 		 * Ok special case, in SHUTDOWN-SENT case. here we maker
2386b5c16493SMichael Tuexen 		 * sure SACK timer is off and instead send a SHUTDOWN and a
2387b5c16493SMichael Tuexen 		 * SACK
2388f8829a4aSRandall Stewart 		 */
2389139bc87fSRandall Stewart 		if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2390f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
2391a5d547adSRandall Stewart 			    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_INDATA + SCTP_LOC_18);
2392f8829a4aSRandall Stewart 		}
2393ca85e948SMichael Tuexen 		sctp_send_shutdown(stcb,
2394ca85e948SMichael Tuexen 		    ((stcb->asoc.alternate) ? stcb->asoc.alternate : stcb->asoc.primary_destination));
2395689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
2396f8829a4aSRandall Stewart 	} else {
2397f8829a4aSRandall Stewart 		int is_a_gap;
2398f8829a4aSRandall Stewart 
2399f8829a4aSRandall Stewart 		/* is there a gap now ? */
240020b07a4dSMichael Tuexen 		is_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
2401f8829a4aSRandall Stewart 
2402f8829a4aSRandall Stewart 		/*
2403b5c16493SMichael Tuexen 		 * CMT DAC algorithm: increase number of packets received
2404b5c16493SMichael Tuexen 		 * since last ack
2405f8829a4aSRandall Stewart 		 */
2406f8829a4aSRandall Stewart 		stcb->asoc.cmt_dac_pkts_rcvd++;
2407f8829a4aSRandall Stewart 
240842551e99SRandall Stewart 		if ((stcb->asoc.send_sack == 1) ||	/* We need to send a
240942551e99SRandall Stewart 							 * SACK */
2410f8829a4aSRandall Stewart 		    ((was_a_gap) && (is_a_gap == 0)) ||	/* was a gap, but no
2411f8829a4aSRandall Stewart 							 * longer is one */
2412f8829a4aSRandall Stewart 		    (stcb->asoc.numduptsns) ||	/* we have dup's */
2413f8829a4aSRandall Stewart 		    (is_a_gap) ||	/* is still a gap */
241442551e99SRandall Stewart 		    (stcb->asoc.delayed_ack == 0) ||	/* Delayed sack disabled */
241542551e99SRandall Stewart 		    (stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq)	/* hit limit of pkts */
2416f8829a4aSRandall Stewart 		    ) {
2417f8829a4aSRandall Stewart 
24187c99d56fSMichael Tuexen 			if ((stcb->asoc.sctp_cmt_on_off > 0) &&
2419b3f1ea41SRandall Stewart 			    (SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) &&
242042551e99SRandall Stewart 			    (stcb->asoc.send_sack == 0) &&
2421f8829a4aSRandall Stewart 			    (stcb->asoc.numduptsns == 0) &&
2422f8829a4aSRandall Stewart 			    (stcb->asoc.delayed_ack) &&
2423139bc87fSRandall Stewart 			    (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer))) {
2424f8829a4aSRandall Stewart 
2425f8829a4aSRandall Stewart 				/*
2426b5c16493SMichael Tuexen 				 * CMT DAC algorithm: With CMT, delay acks
2427b5c16493SMichael Tuexen 				 * even in the face of
2428f8829a4aSRandall Stewart 				 *
2429b5c16493SMichael Tuexen 				 * reordering. Therefore, if acks that do not
2430b5c16493SMichael Tuexen 				 * have to be sent because of the above
2431b5c16493SMichael Tuexen 				 * reasons, will be delayed. That is, acks
2432b5c16493SMichael Tuexen 				 * that would have been sent due to gap
2433b5c16493SMichael Tuexen 				 * reports will be delayed with DAC. Start
2434f8829a4aSRandall Stewart 				 * the delayed ack timer.
2435f8829a4aSRandall Stewart 				 */
2436f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2437f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, NULL);
2438f8829a4aSRandall Stewart 			} else {
2439f8829a4aSRandall Stewart 				/*
2440b5c16493SMichael Tuexen 				 * Ok we must build a SACK since the timer
2441b5c16493SMichael Tuexen 				 * is pending, we got our first packet OR
2442b5c16493SMichael Tuexen 				 * there are gaps or duplicates.
2443f8829a4aSRandall Stewart 				 */
2444ad81507eSRandall Stewart 				(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2445689e6a5fSMichael Tuexen 				sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
2446f8829a4aSRandall Stewart 			}
2447f8829a4aSRandall Stewart 		} else {
244842551e99SRandall Stewart 			if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2449f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2450f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, NULL);
2451f8829a4aSRandall Stewart 			}
2452f8829a4aSRandall Stewart 		}
2453f8829a4aSRandall Stewart 	}
2454f8829a4aSRandall Stewart }
2455f8829a4aSRandall Stewart 
2456f8829a4aSRandall Stewart void
2457f8829a4aSRandall Stewart sctp_service_queues(struct sctp_tcb *stcb, struct sctp_association *asoc)
2458f8829a4aSRandall Stewart {
2459f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
2460810ec536SMichael Tuexen 	uint32_t tsize, pd_point;
2461f8829a4aSRandall Stewart 	uint16_t nxt_todel;
2462f8829a4aSRandall Stewart 
2463f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
2464f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
2465f8829a4aSRandall Stewart 	}
2466f8829a4aSRandall Stewart 	/* Can we proceed further, i.e. the PD-API is complete */
2467f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
2468f8829a4aSRandall Stewart 		/* no */
2469f8829a4aSRandall Stewart 		return;
2470f8829a4aSRandall Stewart 	}
2471f8829a4aSRandall Stewart 	/*
2472f8829a4aSRandall Stewart 	 * Now is there some other chunk I can deliver from the reassembly
2473f8829a4aSRandall Stewart 	 * queue.
2474f8829a4aSRandall Stewart 	 */
2475139bc87fSRandall Stewart doit_again:
2476f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
2477f8829a4aSRandall Stewart 	if (chk == NULL) {
2478f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = 0;
2479f8829a4aSRandall Stewart 		asoc->cnt_on_reasm_queue = 0;
2480f8829a4aSRandall Stewart 		return;
2481f8829a4aSRandall Stewart 	}
2482f8829a4aSRandall Stewart 	nxt_todel = asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
2483f8829a4aSRandall Stewart 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
2484f8829a4aSRandall Stewart 	    ((nxt_todel == chk->rec.data.stream_seq) ||
2485f8829a4aSRandall Stewart 	    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
2486f8829a4aSRandall Stewart 		/*
2487f8829a4aSRandall Stewart 		 * Yep the first one is here. We setup to start reception,
2488f8829a4aSRandall Stewart 		 * by backing down the TSN just in case we can't deliver.
2489f8829a4aSRandall Stewart 		 */
2490f8829a4aSRandall Stewart 
2491f8829a4aSRandall Stewart 		/*
2492f8829a4aSRandall Stewart 		 * Before we start though either all of the message should
249324ae5c4aSMichael Tuexen 		 * be here or the socket buffer max or nothing on the
2494f8829a4aSRandall Stewart 		 * delivery queue and something can be delivered.
2495f8829a4aSRandall Stewart 		 */
2496810ec536SMichael Tuexen 		if (stcb->sctp_socket) {
249724ae5c4aSMichael Tuexen 			pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket),
2498810ec536SMichael Tuexen 			    stcb->sctp_ep->partial_delivery_point);
2499810ec536SMichael Tuexen 		} else {
2500810ec536SMichael Tuexen 			pd_point = stcb->sctp_ep->partial_delivery_point;
2501810ec536SMichael Tuexen 		}
2502810ec536SMichael Tuexen 		if (sctp_is_all_msg_on_reasm(asoc, &tsize) || (tsize >= pd_point)) {
2503f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 1;
2504f8829a4aSRandall Stewart 			asoc->tsn_last_delivered = chk->rec.data.TSN_seq - 1;
2505f8829a4aSRandall Stewart 			asoc->str_of_pdapi = chk->rec.data.stream_number;
2506f8829a4aSRandall Stewart 			asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
2507f8829a4aSRandall Stewart 			asoc->pdapi_ppid = chk->rec.data.payloadtype;
2508f8829a4aSRandall Stewart 			asoc->fragment_flags = chk->rec.data.rcv_flags;
2509f8829a4aSRandall Stewart 			sctp_service_reassembly(stcb, asoc);
2510139bc87fSRandall Stewart 			if (asoc->fragmented_delivery_inprogress == 0) {
2511139bc87fSRandall Stewart 				goto doit_again;
2512139bc87fSRandall Stewart 			}
2513f8829a4aSRandall Stewart 		}
2514f8829a4aSRandall Stewart 	}
2515f8829a4aSRandall Stewart }
2516f8829a4aSRandall Stewart 
2517f8829a4aSRandall Stewart int
2518f8829a4aSRandall Stewart sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
2519f30ac432SMichael Tuexen     struct sctphdr *sh, struct sctp_inpcb *inp,
2520f30ac432SMichael Tuexen     struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t * high_tsn,
2521f30ac432SMichael Tuexen     uint8_t use_mflowid, uint32_t mflowid,
2522f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
2523f8829a4aSRandall Stewart {
2524f8829a4aSRandall Stewart 	struct sctp_data_chunk *ch, chunk_buf;
2525f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2526f8829a4aSRandall Stewart 	int num_chunks = 0;	/* number of control chunks processed */
2527f8829a4aSRandall Stewart 	int stop_proc = 0;
2528f8829a4aSRandall Stewart 	int chk_length, break_flag, last_chunk;
25298f777478SMichael Tuexen 	int abort_flag = 0, was_a_gap;
2530f8829a4aSRandall Stewart 	struct mbuf *m;
25318f777478SMichael Tuexen 	uint32_t highest_tsn;
2532f8829a4aSRandall Stewart 
2533f8829a4aSRandall Stewart 	/* set the rwnd */
2534f8829a4aSRandall Stewart 	sctp_set_rwnd(stcb, &stcb->asoc);
2535f8829a4aSRandall Stewart 
2536f8829a4aSRandall Stewart 	m = *mm;
2537f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
2538f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
253920b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
25408f777478SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_nr_map;
25418f777478SMichael Tuexen 	} else {
25428f777478SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_map;
2543f8829a4aSRandall Stewart 	}
254420b07a4dSMichael Tuexen 	was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
2545f8829a4aSRandall Stewart 	/*
2546f8829a4aSRandall Stewart 	 * setup where we got the last DATA packet from for any SACK that
2547f8829a4aSRandall Stewart 	 * may need to go out. Don't bump the net. This is done ONLY when a
2548f8829a4aSRandall Stewart 	 * chunk is assigned.
2549f8829a4aSRandall Stewart 	 */
2550f8829a4aSRandall Stewart 	asoc->last_data_chunk_from = net;
2551f8829a4aSRandall Stewart 
2552d06c82f1SRandall Stewart 	/*-
2553f8829a4aSRandall Stewart 	 * Now before we proceed we must figure out if this is a wasted
2554f8829a4aSRandall Stewart 	 * cluster... i.e. it is a small packet sent in and yet the driver
2555f8829a4aSRandall Stewart 	 * underneath allocated a full cluster for it. If so we must copy it
2556f8829a4aSRandall Stewart 	 * to a smaller mbuf and free up the cluster mbuf. This will help
2557d06c82f1SRandall Stewart 	 * with cluster starvation. Note for __Panda__ we don't do this
2558d06c82f1SRandall Stewart 	 * since it has clusters all the way down to 64 bytes.
2559f8829a4aSRandall Stewart 	 */
256044b7479bSRandall Stewart 	if (SCTP_BUF_LEN(m) < (long)MLEN && SCTP_BUF_NEXT(m) == NULL) {
2561f8829a4aSRandall Stewart 		/* we only handle mbufs that are singletons.. not chains */
2562139bc87fSRandall Stewart 		m = sctp_get_mbuf_for_msg(SCTP_BUF_LEN(m), 0, M_DONTWAIT, 1, MT_DATA);
2563f8829a4aSRandall Stewart 		if (m) {
2564f8829a4aSRandall Stewart 			/* ok lets see if we can copy the data up */
2565f8829a4aSRandall Stewart 			caddr_t *from, *to;
2566f8829a4aSRandall Stewart 
2567f8829a4aSRandall Stewart 			/* get the pointers and copy */
2568f8829a4aSRandall Stewart 			to = mtod(m, caddr_t *);
2569f8829a4aSRandall Stewart 			from = mtod((*mm), caddr_t *);
2570139bc87fSRandall Stewart 			memcpy(to, from, SCTP_BUF_LEN((*mm)));
2571f8829a4aSRandall Stewart 			/* copy the length and free up the old */
2572139bc87fSRandall Stewart 			SCTP_BUF_LEN(m) = SCTP_BUF_LEN((*mm));
2573f8829a4aSRandall Stewart 			sctp_m_freem(*mm);
2574f8829a4aSRandall Stewart 			/* sucess, back copy */
2575f8829a4aSRandall Stewart 			*mm = m;
2576f8829a4aSRandall Stewart 		} else {
2577f8829a4aSRandall Stewart 			/* We are in trouble in the mbuf world .. yikes */
2578f8829a4aSRandall Stewart 			m = *mm;
2579f8829a4aSRandall Stewart 		}
2580f8829a4aSRandall Stewart 	}
2581f8829a4aSRandall Stewart 	/* get pointer to the first chunk header */
2582f8829a4aSRandall Stewart 	ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2583f8829a4aSRandall Stewart 	    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2584f8829a4aSRandall Stewart 	if (ch == NULL) {
2585f8829a4aSRandall Stewart 		return (1);
2586f8829a4aSRandall Stewart 	}
2587f8829a4aSRandall Stewart 	/*
2588f8829a4aSRandall Stewart 	 * process all DATA chunks...
2589f8829a4aSRandall Stewart 	 */
2590f8829a4aSRandall Stewart 	*high_tsn = asoc->cumulative_tsn;
2591f8829a4aSRandall Stewart 	break_flag = 0;
259242551e99SRandall Stewart 	asoc->data_pkts_seen++;
2593f8829a4aSRandall Stewart 	while (stop_proc == 0) {
2594f8829a4aSRandall Stewart 		/* validate chunk length */
2595f8829a4aSRandall Stewart 		chk_length = ntohs(ch->ch.chunk_length);
2596f8829a4aSRandall Stewart 		if (length - *offset < chk_length) {
2597f8829a4aSRandall Stewart 			/* all done, mutulated chunk */
2598f8829a4aSRandall Stewart 			stop_proc = 1;
259960990c0cSMichael Tuexen 			continue;
2600f8829a4aSRandall Stewart 		}
2601f8829a4aSRandall Stewart 		if (ch->ch.chunk_type == SCTP_DATA) {
2602f8829a4aSRandall Stewart 			if ((size_t)chk_length < sizeof(struct sctp_data_chunk) + 1) {
2603f8829a4aSRandall Stewart 				/*
2604f8829a4aSRandall Stewart 				 * Need to send an abort since we had a
2605f8829a4aSRandall Stewart 				 * invalid data chunk.
2606f8829a4aSRandall Stewart 				 */
2607f8829a4aSRandall Stewart 				struct mbuf *op_err;
2608f8829a4aSRandall Stewart 
2609139bc87fSRandall Stewart 				op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 2 * sizeof(uint32_t)),
2610f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
2611f8829a4aSRandall Stewart 
2612f8829a4aSRandall Stewart 				if (op_err) {
2613f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
2614f8829a4aSRandall Stewart 					uint32_t *ippp;
2615f8829a4aSRandall Stewart 
2616139bc87fSRandall Stewart 					SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr) +
2617f8829a4aSRandall Stewart 					    (2 * sizeof(uint32_t));
2618f8829a4aSRandall Stewart 					ph = mtod(op_err, struct sctp_paramhdr *);
2619f8829a4aSRandall Stewart 					ph->param_type =
2620f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2621139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(op_err));
2622f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
2623a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_19);
2624f8829a4aSRandall Stewart 					ippp++;
2625f8829a4aSRandall Stewart 					*ippp = asoc->cumulative_tsn;
2626f8829a4aSRandall Stewart 
2627f8829a4aSRandall Stewart 				}
2628a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19;
2629f8829a4aSRandall Stewart 				sctp_abort_association(inp, stcb, m, iphlen, sh,
2630f30ac432SMichael Tuexen 				    op_err,
2631f30ac432SMichael Tuexen 				    use_mflowid, mflowid,
2632f30ac432SMichael Tuexen 				    vrf_id, port);
2633f8829a4aSRandall Stewart 				return (2);
2634f8829a4aSRandall Stewart 			}
2635f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
2636f8829a4aSRandall Stewart 			sctp_audit_log(0xB1, 0);
2637f8829a4aSRandall Stewart #endif
2638f8829a4aSRandall Stewart 			if (SCTP_SIZE32(chk_length) == (length - *offset)) {
2639f8829a4aSRandall Stewart 				last_chunk = 1;
2640f8829a4aSRandall Stewart 			} else {
2641f8829a4aSRandall Stewart 				last_chunk = 0;
2642f8829a4aSRandall Stewart 			}
2643f8829a4aSRandall Stewart 			if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset, ch,
2644f8829a4aSRandall Stewart 			    chk_length, net, high_tsn, &abort_flag, &break_flag,
2645f8829a4aSRandall Stewart 			    last_chunk)) {
2646f8829a4aSRandall Stewart 				num_chunks++;
2647f8829a4aSRandall Stewart 			}
2648f8829a4aSRandall Stewart 			if (abort_flag)
2649f8829a4aSRandall Stewart 				return (2);
2650f8829a4aSRandall Stewart 
2651f8829a4aSRandall Stewart 			if (break_flag) {
2652f8829a4aSRandall Stewart 				/*
2653f8829a4aSRandall Stewart 				 * Set because of out of rwnd space and no
2654f8829a4aSRandall Stewart 				 * drop rep space left.
2655f8829a4aSRandall Stewart 				 */
2656f8829a4aSRandall Stewart 				stop_proc = 1;
265760990c0cSMichael Tuexen 				continue;
2658f8829a4aSRandall Stewart 			}
2659f8829a4aSRandall Stewart 		} else {
2660f8829a4aSRandall Stewart 			/* not a data chunk in the data region */
2661f8829a4aSRandall Stewart 			switch (ch->ch.chunk_type) {
2662f8829a4aSRandall Stewart 			case SCTP_INITIATION:
2663f8829a4aSRandall Stewart 			case SCTP_INITIATION_ACK:
2664f8829a4aSRandall Stewart 			case SCTP_SELECTIVE_ACK:
266560990c0cSMichael Tuexen 			case SCTP_NR_SELECTIVE_ACK:
2666f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_REQUEST:
2667f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_ACK:
2668f8829a4aSRandall Stewart 			case SCTP_ABORT_ASSOCIATION:
2669f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN:
2670f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_ACK:
2671f8829a4aSRandall Stewart 			case SCTP_OPERATION_ERROR:
2672f8829a4aSRandall Stewart 			case SCTP_COOKIE_ECHO:
2673f8829a4aSRandall Stewart 			case SCTP_COOKIE_ACK:
2674f8829a4aSRandall Stewart 			case SCTP_ECN_ECHO:
2675f8829a4aSRandall Stewart 			case SCTP_ECN_CWR:
2676f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_COMPLETE:
2677f8829a4aSRandall Stewart 			case SCTP_AUTHENTICATION:
2678f8829a4aSRandall Stewart 			case SCTP_ASCONF_ACK:
2679f8829a4aSRandall Stewart 			case SCTP_PACKET_DROPPED:
2680f8829a4aSRandall Stewart 			case SCTP_STREAM_RESET:
2681f8829a4aSRandall Stewart 			case SCTP_FORWARD_CUM_TSN:
2682f8829a4aSRandall Stewart 			case SCTP_ASCONF:
2683f8829a4aSRandall Stewart 				/*
2684f8829a4aSRandall Stewart 				 * Now, what do we do with KNOWN chunks that
2685f8829a4aSRandall Stewart 				 * are NOT in the right place?
2686f8829a4aSRandall Stewart 				 *
2687f8829a4aSRandall Stewart 				 * For now, I do nothing but ignore them. We
2688f8829a4aSRandall Stewart 				 * may later want to add sysctl stuff to
2689f8829a4aSRandall Stewart 				 * switch out and do either an ABORT() or
2690f8829a4aSRandall Stewart 				 * possibly process them.
2691f8829a4aSRandall Stewart 				 */
2692b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_strict_data_order)) {
2693f8829a4aSRandall Stewart 					struct mbuf *op_err;
2694f8829a4aSRandall Stewart 
2695f8829a4aSRandall Stewart 					op_err = sctp_generate_invmanparam(SCTP_CAUSE_PROTOCOL_VIOLATION);
2696f30ac432SMichael Tuexen 					sctp_abort_association(inp, stcb,
2697f30ac432SMichael Tuexen 					    m, iphlen,
2698f30ac432SMichael Tuexen 					    sh, op_err,
2699f30ac432SMichael Tuexen 					    use_mflowid, mflowid,
2700f30ac432SMichael Tuexen 					    vrf_id, port);
2701f8829a4aSRandall Stewart 					return (2);
2702f8829a4aSRandall Stewart 				}
2703f8829a4aSRandall Stewart 				break;
2704f8829a4aSRandall Stewart 			default:
2705f8829a4aSRandall Stewart 				/* unknown chunk type, use bit rules */
2706f8829a4aSRandall Stewart 				if (ch->ch.chunk_type & 0x40) {
2707f8829a4aSRandall Stewart 					/* Add a error report to the queue */
2708d61a0ae0SRandall Stewart 					struct mbuf *merr;
2709f8829a4aSRandall Stewart 					struct sctp_paramhdr *phd;
2710f8829a4aSRandall Stewart 
2711d61a0ae0SRandall Stewart 					merr = sctp_get_mbuf_for_msg(sizeof(*phd), 0, M_DONTWAIT, 1, MT_DATA);
2712d61a0ae0SRandall Stewart 					if (merr) {
2713d61a0ae0SRandall Stewart 						phd = mtod(merr, struct sctp_paramhdr *);
2714f8829a4aSRandall Stewart 						/*
2715f8829a4aSRandall Stewart 						 * We cheat and use param
2716f8829a4aSRandall Stewart 						 * type since we did not
2717f8829a4aSRandall Stewart 						 * bother to define a error
2718f8829a4aSRandall Stewart 						 * cause struct. They are
2719f8829a4aSRandall Stewart 						 * the same basic format
2720f8829a4aSRandall Stewart 						 * with different names.
2721f8829a4aSRandall Stewart 						 */
2722f8829a4aSRandall Stewart 						phd->param_type =
2723f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_UNRECOG_CHUNK);
2724f8829a4aSRandall Stewart 						phd->param_length =
2725f8829a4aSRandall Stewart 						    htons(chk_length + sizeof(*phd));
2726d61a0ae0SRandall Stewart 						SCTP_BUF_LEN(merr) = sizeof(*phd);
2727921569e2SMichael Tuexen 						SCTP_BUF_NEXT(merr) = SCTP_M_COPYM(m, *offset, chk_length, M_DONTWAIT);
2728d61a0ae0SRandall Stewart 						if (SCTP_BUF_NEXT(merr)) {
2729921569e2SMichael Tuexen 							if (sctp_pad_lastmbuf(SCTP_BUF_NEXT(merr), SCTP_SIZE32(chk_length) - chk_length, NULL)) {
2730921569e2SMichael Tuexen 								sctp_m_freem(merr);
2731921569e2SMichael Tuexen 							} else {
2732d61a0ae0SRandall Stewart 								sctp_queue_op_err(stcb, merr);
2733921569e2SMichael Tuexen 							}
2734f8829a4aSRandall Stewart 						} else {
2735d61a0ae0SRandall Stewart 							sctp_m_freem(merr);
2736f8829a4aSRandall Stewart 						}
2737f8829a4aSRandall Stewart 					}
2738f8829a4aSRandall Stewart 				}
2739f8829a4aSRandall Stewart 				if ((ch->ch.chunk_type & 0x80) == 0) {
2740f8829a4aSRandall Stewart 					/* discard the rest of this packet */
2741f8829a4aSRandall Stewart 					stop_proc = 1;
2742f8829a4aSRandall Stewart 				}	/* else skip this bad chunk and
2743f8829a4aSRandall Stewart 					 * continue... */
2744f8829a4aSRandall Stewart 				break;
274560990c0cSMichael Tuexen 			}	/* switch of chunk type */
2746f8829a4aSRandall Stewart 		}
2747f8829a4aSRandall Stewart 		*offset += SCTP_SIZE32(chk_length);
2748f8829a4aSRandall Stewart 		if ((*offset >= length) || stop_proc) {
2749f8829a4aSRandall Stewart 			/* no more data left in the mbuf chain */
2750f8829a4aSRandall Stewart 			stop_proc = 1;
2751f8829a4aSRandall Stewart 			continue;
2752f8829a4aSRandall Stewart 		}
2753f8829a4aSRandall Stewart 		ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2754f8829a4aSRandall Stewart 		    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2755f8829a4aSRandall Stewart 		if (ch == NULL) {
2756f8829a4aSRandall Stewart 			*offset = length;
2757f8829a4aSRandall Stewart 			stop_proc = 1;
275860990c0cSMichael Tuexen 			continue;
2759f8829a4aSRandall Stewart 		}
276060990c0cSMichael Tuexen 	}
2761f8829a4aSRandall Stewart 	if (break_flag) {
2762f8829a4aSRandall Stewart 		/*
2763f8829a4aSRandall Stewart 		 * we need to report rwnd overrun drops.
2764f8829a4aSRandall Stewart 		 */
2765*20cc2188SMichael Tuexen 		sctp_send_packet_dropped(stcb, net, *mm, length, iphlen, 0);
2766f8829a4aSRandall Stewart 	}
2767f8829a4aSRandall Stewart 	if (num_chunks) {
2768f8829a4aSRandall Stewart 		/*
2769ceaad40aSRandall Stewart 		 * Did we get data, if so update the time for auto-close and
2770f8829a4aSRandall Stewart 		 * give peer credit for being alive.
2771f8829a4aSRandall Stewart 		 */
2772f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvpktwithdata);
2773b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
2774c4739e2fSRandall Stewart 			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2775c4739e2fSRandall Stewart 			    stcb->asoc.overall_error_count,
2776c4739e2fSRandall Stewart 			    0,
2777c4739e2fSRandall Stewart 			    SCTP_FROM_SCTP_INDATA,
2778c4739e2fSRandall Stewart 			    __LINE__);
2779c4739e2fSRandall Stewart 		}
2780f8829a4aSRandall Stewart 		stcb->asoc.overall_error_count = 0;
27816e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
2782f8829a4aSRandall Stewart 	}
2783f8829a4aSRandall Stewart 	/* now service all of the reassm queue if needed */
2784f8829a4aSRandall Stewart 	if (!(TAILQ_EMPTY(&asoc->reasmqueue)))
2785f8829a4aSRandall Stewart 		sctp_service_queues(stcb, asoc);
2786f8829a4aSRandall Stewart 
2787f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
278842551e99SRandall Stewart 		/* Assure that we ack right away */
278942551e99SRandall Stewart 		stcb->asoc.send_sack = 1;
2790f8829a4aSRandall Stewart 	}
2791f8829a4aSRandall Stewart 	/* Start a sack timer or QUEUE a SACK for sending */
27927215cc1bSMichael Tuexen 	sctp_sack_check(stcb, was_a_gap);
2793f8829a4aSRandall Stewart 	return (0);
2794f8829a4aSRandall Stewart }
2795f8829a4aSRandall Stewart 
27960fa753b3SRandall Stewart static int
27970fa753b3SRandall Stewart sctp_process_segment_range(struct sctp_tcb *stcb, struct sctp_tmit_chunk **p_tp1, uint32_t last_tsn,
27980fa753b3SRandall Stewart     uint16_t frag_strt, uint16_t frag_end, int nr_sacking,
27990fa753b3SRandall Stewart     int *num_frs,
28000fa753b3SRandall Stewart     uint32_t * biggest_newly_acked_tsn,
28010fa753b3SRandall Stewart     uint32_t * this_sack_lowest_newack,
28027215cc1bSMichael Tuexen     int *rto_ok)
28030fa753b3SRandall Stewart {
28040fa753b3SRandall Stewart 	struct sctp_tmit_chunk *tp1;
28050fa753b3SRandall Stewart 	unsigned int theTSN;
2806b5c16493SMichael Tuexen 	int j, wake_him = 0, circled = 0;
28070fa753b3SRandall Stewart 
28080fa753b3SRandall Stewart 	/* Recover the tp1 we last saw */
28090fa753b3SRandall Stewart 	tp1 = *p_tp1;
28100fa753b3SRandall Stewart 	if (tp1 == NULL) {
28110fa753b3SRandall Stewart 		tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
28120fa753b3SRandall Stewart 	}
28130fa753b3SRandall Stewart 	for (j = frag_strt; j <= frag_end; j++) {
28140fa753b3SRandall Stewart 		theTSN = j + last_tsn;
28150fa753b3SRandall Stewart 		while (tp1) {
28160fa753b3SRandall Stewart 			if (tp1->rec.data.doing_fast_retransmit)
28170fa753b3SRandall Stewart 				(*num_frs) += 1;
28180fa753b3SRandall Stewart 
28190fa753b3SRandall Stewart 			/*-
28200fa753b3SRandall Stewart 			 * CMT: CUCv2 algorithm. For each TSN being
28210fa753b3SRandall Stewart 			 * processed from the sent queue, track the
28220fa753b3SRandall Stewart 			 * next expected pseudo-cumack, or
28230fa753b3SRandall Stewart 			 * rtx_pseudo_cumack, if required. Separate
28240fa753b3SRandall Stewart 			 * cumack trackers for first transmissions,
28250fa753b3SRandall Stewart 			 * and retransmissions.
28260fa753b3SRandall Stewart 			 */
28270fa753b3SRandall Stewart 			if ((tp1->whoTo->find_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
28280fa753b3SRandall Stewart 			    (tp1->snd_count == 1)) {
28290fa753b3SRandall Stewart 				tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
28300fa753b3SRandall Stewart 				tp1->whoTo->find_pseudo_cumack = 0;
28310fa753b3SRandall Stewart 			}
28320fa753b3SRandall Stewart 			if ((tp1->whoTo->find_rtx_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
28330fa753b3SRandall Stewart 			    (tp1->snd_count > 1)) {
28340fa753b3SRandall Stewart 				tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
28350fa753b3SRandall Stewart 				tp1->whoTo->find_rtx_pseudo_cumack = 0;
28360fa753b3SRandall Stewart 			}
28370fa753b3SRandall Stewart 			if (tp1->rec.data.TSN_seq == theTSN) {
28380fa753b3SRandall Stewart 				if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
28390fa753b3SRandall Stewart 					/*-
28400fa753b3SRandall Stewart 					 * must be held until
28410fa753b3SRandall Stewart 					 * cum-ack passes
28420fa753b3SRandall Stewart 					 */
28430fa753b3SRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
28440fa753b3SRandall Stewart 						/*-
28450fa753b3SRandall Stewart 						 * If it is less than RESEND, it is
28460fa753b3SRandall Stewart 						 * now no-longer in flight.
28470fa753b3SRandall Stewart 						 * Higher values may already be set
28480fa753b3SRandall Stewart 						 * via previous Gap Ack Blocks...
28490fa753b3SRandall Stewart 						 * i.e. ACKED or RESEND.
28500fa753b3SRandall Stewart 						 */
285120b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
285220b07a4dSMichael Tuexen 						    *biggest_newly_acked_tsn)) {
28530fa753b3SRandall Stewart 							*biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
28540fa753b3SRandall Stewart 						}
28550fa753b3SRandall Stewart 						/*-
28560fa753b3SRandall Stewart 						 * CMT: SFR algo (and HTNA) - set
28570fa753b3SRandall Stewart 						 * saw_newack to 1 for dest being
28580fa753b3SRandall Stewart 						 * newly acked. update
28590fa753b3SRandall Stewart 						 * this_sack_highest_newack if
28600fa753b3SRandall Stewart 						 * appropriate.
28610fa753b3SRandall Stewart 						 */
28620fa753b3SRandall Stewart 						if (tp1->rec.data.chunk_was_revoked == 0)
28630fa753b3SRandall Stewart 							tp1->whoTo->saw_newack = 1;
28640fa753b3SRandall Stewart 
286520b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
286620b07a4dSMichael Tuexen 						    tp1->whoTo->this_sack_highest_newack)) {
28670fa753b3SRandall Stewart 							tp1->whoTo->this_sack_highest_newack =
28680fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq;
28690fa753b3SRandall Stewart 						}
28700fa753b3SRandall Stewart 						/*-
28710fa753b3SRandall Stewart 						 * CMT DAC algo: also update
28720fa753b3SRandall Stewart 						 * this_sack_lowest_newack
28730fa753b3SRandall Stewart 						 */
28740fa753b3SRandall Stewart 						if (*this_sack_lowest_newack == 0) {
28750fa753b3SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
28760fa753b3SRandall Stewart 								sctp_log_sack(*this_sack_lowest_newack,
28770fa753b3SRandall Stewart 								    last_tsn,
28780fa753b3SRandall Stewart 								    tp1->rec.data.TSN_seq,
28790fa753b3SRandall Stewart 								    0,
28800fa753b3SRandall Stewart 								    0,
28810fa753b3SRandall Stewart 								    SCTP_LOG_TSN_ACKED);
28820fa753b3SRandall Stewart 							}
28830fa753b3SRandall Stewart 							*this_sack_lowest_newack = tp1->rec.data.TSN_seq;
28840fa753b3SRandall Stewart 						}
28850fa753b3SRandall Stewart 						/*-
28860fa753b3SRandall Stewart 						 * CMT: CUCv2 algorithm. If (rtx-)pseudo-cumack for corresp
28870fa753b3SRandall Stewart 						 * dest is being acked, then we have a new (rtx-)pseudo-cumack. Set
28880fa753b3SRandall Stewart 						 * new_(rtx_)pseudo_cumack to TRUE so that the cwnd for this dest can be
28890fa753b3SRandall Stewart 						 * updated. Also trigger search for the next expected (rtx-)pseudo-cumack.
28900fa753b3SRandall Stewart 						 * Separate pseudo_cumack trackers for first transmissions and
28910fa753b3SRandall Stewart 						 * retransmissions.
28920fa753b3SRandall Stewart 						 */
28930fa753b3SRandall Stewart 						if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
28940fa753b3SRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0) {
28950fa753b3SRandall Stewart 								tp1->whoTo->new_pseudo_cumack = 1;
28960fa753b3SRandall Stewart 							}
28970fa753b3SRandall Stewart 							tp1->whoTo->find_pseudo_cumack = 1;
28980fa753b3SRandall Stewart 						}
28990fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
29000fa753b3SRandall Stewart 							sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
29010fa753b3SRandall Stewart 						}
29020fa753b3SRandall Stewart 						if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
29030fa753b3SRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0) {
29040fa753b3SRandall Stewart 								tp1->whoTo->new_pseudo_cumack = 1;
29050fa753b3SRandall Stewart 							}
29060fa753b3SRandall Stewart 							tp1->whoTo->find_rtx_pseudo_cumack = 1;
29070fa753b3SRandall Stewart 						}
29080fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
29090fa753b3SRandall Stewart 							sctp_log_sack(*biggest_newly_acked_tsn,
29100fa753b3SRandall Stewart 							    last_tsn,
29110fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq,
29120fa753b3SRandall Stewart 							    frag_strt,
29130fa753b3SRandall Stewart 							    frag_end,
29140fa753b3SRandall Stewart 							    SCTP_LOG_TSN_ACKED);
29150fa753b3SRandall Stewart 						}
29160fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
29170fa753b3SRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
29180fa753b3SRandall Stewart 							    tp1->whoTo->flight_size,
29190fa753b3SRandall Stewart 							    tp1->book_size,
29200fa753b3SRandall Stewart 							    (uintptr_t) tp1->whoTo,
29210fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq);
29220fa753b3SRandall Stewart 						}
29230fa753b3SRandall Stewart 						sctp_flight_size_decrease(tp1);
2924299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
2925299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
2926299108c5SRandall Stewart 							    tp1);
2927299108c5SRandall Stewart 						}
29280fa753b3SRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
29290fa753b3SRandall Stewart 
29300fa753b3SRandall Stewart 						tp1->whoTo->net_ack += tp1->send_size;
29310fa753b3SRandall Stewart 						if (tp1->snd_count < 2) {
29320fa753b3SRandall Stewart 							/*-
29330fa753b3SRandall Stewart 							 * True non-retransmited chunk
29340fa753b3SRandall Stewart 							 */
29350fa753b3SRandall Stewart 							tp1->whoTo->net_ack2 += tp1->send_size;
29360fa753b3SRandall Stewart 
29370fa753b3SRandall Stewart 							/*-
29380fa753b3SRandall Stewart 							 * update RTO too ?
29390fa753b3SRandall Stewart 							 */
29400fa753b3SRandall Stewart 							if (tp1->do_rtt) {
2941f79aab18SRandall Stewart 								if (*rto_ok) {
29420fa753b3SRandall Stewart 									tp1->whoTo->RTO =
29430fa753b3SRandall Stewart 									    sctp_calculate_rto(stcb,
29440fa753b3SRandall Stewart 									    &stcb->asoc,
29450fa753b3SRandall Stewart 									    tp1->whoTo,
29460fa753b3SRandall Stewart 									    &tp1->sent_rcv_time,
2947899288aeSRandall Stewart 									    sctp_align_safe_nocopy,
2948f79aab18SRandall Stewart 									    SCTP_RTT_FROM_DATA);
2949f79aab18SRandall Stewart 									*rto_ok = 0;
2950f79aab18SRandall Stewart 								}
2951f79aab18SRandall Stewart 								if (tp1->whoTo->rto_needed == 0) {
2952f79aab18SRandall Stewart 									tp1->whoTo->rto_needed = 1;
2953f79aab18SRandall Stewart 								}
29540fa753b3SRandall Stewart 								tp1->do_rtt = 0;
29550fa753b3SRandall Stewart 							}
29560fa753b3SRandall Stewart 						}
29570fa753b3SRandall Stewart 					}
29580fa753b3SRandall Stewart 					if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
295920b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
296020b07a4dSMichael Tuexen 						    stcb->asoc.this_sack_highest_gap)) {
29610fa753b3SRandall Stewart 							stcb->asoc.this_sack_highest_gap =
29620fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq;
29630fa753b3SRandall Stewart 						}
29640fa753b3SRandall Stewart 						if (tp1->sent == SCTP_DATAGRAM_RESEND) {
29650fa753b3SRandall Stewart 							sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
29660fa753b3SRandall Stewart #ifdef SCTP_AUDITING_ENABLED
29670fa753b3SRandall Stewart 							sctp_audit_log(0xB2,
29680fa753b3SRandall Stewart 							    (stcb->asoc.sent_queue_retran_cnt & 0x000000ff));
29690fa753b3SRandall Stewart #endif
29700fa753b3SRandall Stewart 						}
29710fa753b3SRandall Stewart 					}
29720fa753b3SRandall Stewart 					/*-
29730fa753b3SRandall Stewart 					 * All chunks NOT UNSENT fall through here and are marked
29740fa753b3SRandall Stewart 					 * (leave PR-SCTP ones that are to skip alone though)
29750fa753b3SRandall Stewart 					 */
29760fa753b3SRandall Stewart 					if (tp1->sent != SCTP_FORWARD_TSN_SKIP)
29770fa753b3SRandall Stewart 						tp1->sent = SCTP_DATAGRAM_MARKED;
29780fa753b3SRandall Stewart 
29790fa753b3SRandall Stewart 					if (tp1->rec.data.chunk_was_revoked) {
29800fa753b3SRandall Stewart 						/* deflate the cwnd */
29810fa753b3SRandall Stewart 						tp1->whoTo->cwnd -= tp1->book_size;
29820fa753b3SRandall Stewart 						tp1->rec.data.chunk_was_revoked = 0;
29830fa753b3SRandall Stewart 					}
29840fa753b3SRandall Stewart 					/* NR Sack code here */
29850fa753b3SRandall Stewart 					if (nr_sacking) {
29860fa753b3SRandall Stewart 						if (tp1->data) {
29870fa753b3SRandall Stewart 							/*
29880fa753b3SRandall Stewart 							 * sa_ignore
29890fa753b3SRandall Stewart 							 * NO_NULL_CHK
29900fa753b3SRandall Stewart 							 */
29910fa753b3SRandall Stewart 							sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
29920fa753b3SRandall Stewart 							sctp_m_freem(tp1->data);
29930fa753b3SRandall Stewart 							tp1->data = NULL;
2994b5c16493SMichael Tuexen 						}
29950fa753b3SRandall Stewart 						wake_him++;
29960fa753b3SRandall Stewart 					}
29970fa753b3SRandall Stewart 				}
29980fa753b3SRandall Stewart 				break;
29990fa753b3SRandall Stewart 			}	/* if (tp1->TSN_seq == theTSN) */
300020b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, theTSN)) {
30010fa753b3SRandall Stewart 				break;
300220b07a4dSMichael Tuexen 			}
30030fa753b3SRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3004b5c16493SMichael Tuexen 			if ((tp1 == NULL) && (circled == 0)) {
3005b5c16493SMichael Tuexen 				circled++;
30060fa753b3SRandall Stewart 				tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
30070fa753b3SRandall Stewart 			}
3008b5c16493SMichael Tuexen 		}		/* end while (tp1) */
3009b5c16493SMichael Tuexen 		if (tp1 == NULL) {
3010b5c16493SMichael Tuexen 			circled = 0;
3011b5c16493SMichael Tuexen 			tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
3012b5c16493SMichael Tuexen 		}
3013b5c16493SMichael Tuexen 		/* In case the fragments were not in order we must reset */
30140fa753b3SRandall Stewart 	}			/* end for (j = fragStart */
30150fa753b3SRandall Stewart 	*p_tp1 = tp1;
30160fa753b3SRandall Stewart 	return (wake_him);	/* Return value only used for nr-sack */
30170fa753b3SRandall Stewart }
30180fa753b3SRandall Stewart 
30190fa753b3SRandall Stewart 
3020cd554309SMichael Tuexen static int
3021458303daSRandall Stewart sctp_handle_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
3022cd554309SMichael Tuexen     uint32_t last_tsn, uint32_t * biggest_tsn_acked,
3023139bc87fSRandall Stewart     uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
30247215cc1bSMichael Tuexen     int num_seg, int num_nr_seg, int *rto_ok)
3025f8829a4aSRandall Stewart {
3026458303daSRandall Stewart 	struct sctp_gap_ack_block *frag, block;
3027f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
30280fa753b3SRandall Stewart 	int i;
3029f8829a4aSRandall Stewart 	int num_frs = 0;
3030cd554309SMichael Tuexen 	int chunk_freed;
3031cd554309SMichael Tuexen 	int non_revocable;
3032d9c5cfeaSMichael Tuexen 	uint16_t frag_strt, frag_end, prev_frag_end;
3033f8829a4aSRandall Stewart 
3034d9c5cfeaSMichael Tuexen 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
3035d9c5cfeaSMichael Tuexen 	prev_frag_end = 0;
3036cd554309SMichael Tuexen 	chunk_freed = 0;
3037f8829a4aSRandall Stewart 
3038cd554309SMichael Tuexen 	for (i = 0; i < (num_seg + num_nr_seg); i++) {
3039d9c5cfeaSMichael Tuexen 		if (i == num_seg) {
3040d9c5cfeaSMichael Tuexen 			prev_frag_end = 0;
3041d9c5cfeaSMichael Tuexen 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
3042d9c5cfeaSMichael Tuexen 		}
3043458303daSRandall Stewart 		frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3044458303daSRandall Stewart 		    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
3045458303daSRandall Stewart 		*offset += sizeof(block);
3046458303daSRandall Stewart 		if (frag == NULL) {
3047cd554309SMichael Tuexen 			return (chunk_freed);
3048458303daSRandall Stewart 		}
3049f8829a4aSRandall Stewart 		frag_strt = ntohs(frag->start);
3050f8829a4aSRandall Stewart 		frag_end = ntohs(frag->end);
3051d9c5cfeaSMichael Tuexen 
3052f8829a4aSRandall Stewart 		if (frag_strt > frag_end) {
3053d9c5cfeaSMichael Tuexen 			/* This gap report is malformed, skip it. */
3054f8829a4aSRandall Stewart 			continue;
3055f8829a4aSRandall Stewart 		}
3056d9c5cfeaSMichael Tuexen 		if (frag_strt <= prev_frag_end) {
3057d9c5cfeaSMichael Tuexen 			/* This gap report is not in order, so restart. */
3058f8829a4aSRandall Stewart 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
3059f8829a4aSRandall Stewart 		}
306020b07a4dSMichael Tuexen 		if (SCTP_TSN_GT((last_tsn + frag_end), *biggest_tsn_acked)) {
3061d9c5cfeaSMichael Tuexen 			*biggest_tsn_acked = last_tsn + frag_end;
3062f8829a4aSRandall Stewart 		}
3063cd554309SMichael Tuexen 		if (i < num_seg) {
3064cd554309SMichael Tuexen 			non_revocable = 0;
3065cd554309SMichael Tuexen 		} else {
3066cd554309SMichael Tuexen 			non_revocable = 1;
3067cd554309SMichael Tuexen 		}
3068cd554309SMichael Tuexen 		if (sctp_process_segment_range(stcb, &tp1, last_tsn, frag_strt, frag_end,
3069cd554309SMichael Tuexen 		    non_revocable, &num_frs, biggest_newly_acked_tsn,
30707215cc1bSMichael Tuexen 		    this_sack_lowest_newack, rto_ok)) {
3071cd554309SMichael Tuexen 			chunk_freed = 1;
3072458303daSRandall Stewart 		}
3073d9c5cfeaSMichael Tuexen 		prev_frag_end = frag_end;
3074f8829a4aSRandall Stewart 	}
3075b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
307680fefe0aSRandall Stewart 		if (num_frs)
307780fefe0aSRandall Stewart 			sctp_log_fr(*biggest_tsn_acked,
307880fefe0aSRandall Stewart 			    *biggest_newly_acked_tsn,
307980fefe0aSRandall Stewart 			    last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
308080fefe0aSRandall Stewart 	}
3081cd554309SMichael Tuexen 	return (chunk_freed);
3082f8829a4aSRandall Stewart }
3083f8829a4aSRandall Stewart 
3084f8829a4aSRandall Stewart static void
3085c105859eSRandall Stewart sctp_check_for_revoked(struct sctp_tcb *stcb,
3086c105859eSRandall Stewart     struct sctp_association *asoc, uint32_t cumack,
308763eda93dSMichael Tuexen     uint32_t biggest_tsn_acked)
3088f8829a4aSRandall Stewart {
3089f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
3090f8829a4aSRandall Stewart 	int tot_revoked = 0;
3091f8829a4aSRandall Stewart 
30924a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
309320b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, cumack)) {
3094f8829a4aSRandall Stewart 			/*
3095f8829a4aSRandall Stewart 			 * ok this guy is either ACK or MARKED. If it is
3096f8829a4aSRandall Stewart 			 * ACKED it has been previously acked but not this
3097f8829a4aSRandall Stewart 			 * time i.e. revoked.  If it is MARKED it was ACK'ed
3098f8829a4aSRandall Stewart 			 * again.
3099f8829a4aSRandall Stewart 			 */
310020b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, biggest_tsn_acked)) {
3101d06c82f1SRandall Stewart 				break;
310220b07a4dSMichael Tuexen 			}
3103f8829a4aSRandall Stewart 			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
3104f8829a4aSRandall Stewart 				/* it has been revoked */
3105f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_SENT;
3106f8829a4aSRandall Stewart 				tp1->rec.data.chunk_was_revoked = 1;
3107a5d547adSRandall Stewart 				/*
310842551e99SRandall Stewart 				 * We must add this stuff back in to assure
310942551e99SRandall Stewart 				 * timers and such get started.
3110a5d547adSRandall Stewart 				 */
3111b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3112c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
3113c105859eSRandall Stewart 					    tp1->whoTo->flight_size,
3114c105859eSRandall Stewart 					    tp1->book_size,
3115c105859eSRandall Stewart 					    (uintptr_t) tp1->whoTo,
3116c105859eSRandall Stewart 					    tp1->rec.data.TSN_seq);
311780fefe0aSRandall Stewart 				}
3118c105859eSRandall Stewart 				sctp_flight_size_increase(tp1);
3119c105859eSRandall Stewart 				sctp_total_flight_increase(stcb, tp1);
312042551e99SRandall Stewart 				/*
312142551e99SRandall Stewart 				 * We inflate the cwnd to compensate for our
312242551e99SRandall Stewart 				 * artificial inflation of the flight_size.
312342551e99SRandall Stewart 				 */
312442551e99SRandall Stewart 				tp1->whoTo->cwnd += tp1->book_size;
3125f8829a4aSRandall Stewart 				tot_revoked++;
3126b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3127f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
3128f8829a4aSRandall Stewart 					    cumack,
3129f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
3130f8829a4aSRandall Stewart 					    0,
3131f8829a4aSRandall Stewart 					    0,
3132f8829a4aSRandall Stewart 					    SCTP_LOG_TSN_REVOKED);
313380fefe0aSRandall Stewart 				}
3134f8829a4aSRandall Stewart 			} else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
3135f8829a4aSRandall Stewart 				/* it has been re-acked in this SACK */
3136f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
3137f8829a4aSRandall Stewart 			}
3138f8829a4aSRandall Stewart 		}
3139f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT)
3140f8829a4aSRandall Stewart 			break;
3141f8829a4aSRandall Stewart 	}
3142f8829a4aSRandall Stewart }
3143f8829a4aSRandall Stewart 
3144830d754dSRandall Stewart 
3145f8829a4aSRandall Stewart static void
3146f8829a4aSRandall Stewart sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
314763eda93dSMichael Tuexen     uint32_t biggest_tsn_acked, uint32_t biggest_tsn_newly_acked, uint32_t this_sack_lowest_newack, int accum_moved)
3148f8829a4aSRandall Stewart {
3149f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
3150f8829a4aSRandall Stewart 	int strike_flag = 0;
3151f8829a4aSRandall Stewart 	struct timeval now;
3152f8829a4aSRandall Stewart 	int tot_retrans = 0;
3153f8829a4aSRandall Stewart 	uint32_t sending_seq;
3154f8829a4aSRandall Stewart 	struct sctp_nets *net;
3155f8829a4aSRandall Stewart 	int num_dests_sacked = 0;
3156f8829a4aSRandall Stewart 
3157f8829a4aSRandall Stewart 	/*
3158f8829a4aSRandall Stewart 	 * select the sending_seq, this is either the next thing ready to be
3159f8829a4aSRandall Stewart 	 * sent but not transmitted, OR, the next seq we assign.
3160f8829a4aSRandall Stewart 	 */
3161f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
3162f8829a4aSRandall Stewart 	if (tp1 == NULL) {
3163f8829a4aSRandall Stewart 		sending_seq = asoc->sending_seq;
3164f8829a4aSRandall Stewart 	} else {
3165f8829a4aSRandall Stewart 		sending_seq = tp1->rec.data.TSN_seq;
3166f8829a4aSRandall Stewart 	}
3167f8829a4aSRandall Stewart 
3168f8829a4aSRandall Stewart 	/* CMT DAC algo: finding out if SACK is a mixed SACK */
31697c99d56fSMichael Tuexen 	if ((asoc->sctp_cmt_on_off > 0) &&
317020083c2eSMichael Tuexen 	    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3171f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3172f8829a4aSRandall Stewart 			if (net->saw_newack)
3173f8829a4aSRandall Stewart 				num_dests_sacked++;
3174f8829a4aSRandall Stewart 		}
3175f8829a4aSRandall Stewart 	}
3176f8829a4aSRandall Stewart 	if (stcb->asoc.peer_supports_prsctp) {
31776e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
3178f8829a4aSRandall Stewart 	}
31794a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
3180f8829a4aSRandall Stewart 		strike_flag = 0;
3181f8829a4aSRandall Stewart 		if (tp1->no_fr_allowed) {
3182f8829a4aSRandall Stewart 			/* this one had a timeout or something */
3183f8829a4aSRandall Stewart 			continue;
3184f8829a4aSRandall Stewart 		}
3185b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3186f8829a4aSRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND)
3187f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3188f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3189f8829a4aSRandall Stewart 				    tp1->sent,
3190f8829a4aSRandall Stewart 				    SCTP_FR_LOG_CHECK_STRIKE);
319180fefe0aSRandall Stewart 		}
319220b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, biggest_tsn_acked) ||
3193f8829a4aSRandall Stewart 		    tp1->sent == SCTP_DATAGRAM_UNSENT) {
3194f8829a4aSRandall Stewart 			/* done */
3195f8829a4aSRandall Stewart 			break;
3196f8829a4aSRandall Stewart 		}
3197f8829a4aSRandall Stewart 		if (stcb->asoc.peer_supports_prsctp) {
3198f8829a4aSRandall Stewart 			if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3199f8829a4aSRandall Stewart 				/* Is it expired? */
320099ddc825SMichael Tuexen 				if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3201f8829a4aSRandall Stewart 					/* Yes so drop it */
3202f8829a4aSRandall Stewart 					if (tp1->data != NULL) {
32031edc9dbaSMichael Tuexen 						(void)sctp_release_pr_sctp_chunk(stcb, tp1, 1,
32040c0982b8SRandall Stewart 						    SCTP_SO_NOT_LOCKED);
3205f8829a4aSRandall Stewart 					}
3206f8829a4aSRandall Stewart 					continue;
3207f8829a4aSRandall Stewart 				}
3208f8829a4aSRandall Stewart 			}
3209f8829a4aSRandall Stewart 		}
321020b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, asoc->this_sack_highest_gap)) {
3211f8829a4aSRandall Stewart 			/* we are beyond the tsn in the sack  */
3212f8829a4aSRandall Stewart 			break;
3213f8829a4aSRandall Stewart 		}
3214f8829a4aSRandall Stewart 		if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
3215f8829a4aSRandall Stewart 			/* either a RESEND, ACKED, or MARKED */
3216f8829a4aSRandall Stewart 			/* skip */
321744fbe462SRandall Stewart 			if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
321844fbe462SRandall Stewart 				/* Continue strikin FWD-TSN chunks */
321944fbe462SRandall Stewart 				tp1->rec.data.fwd_tsn_cnt++;
322044fbe462SRandall Stewart 			}
3221f8829a4aSRandall Stewart 			continue;
3222f8829a4aSRandall Stewart 		}
3223f8829a4aSRandall Stewart 		/*
3224f8829a4aSRandall Stewart 		 * CMT : SFR algo (covers part of DAC and HTNA as well)
3225f8829a4aSRandall Stewart 		 */
3226ad81507eSRandall Stewart 		if (tp1->whoTo && tp1->whoTo->saw_newack == 0) {
3227f8829a4aSRandall Stewart 			/*
3228f8829a4aSRandall Stewart 			 * No new acks were receieved for data sent to this
3229f8829a4aSRandall Stewart 			 * dest. Therefore, according to the SFR algo for
3230f8829a4aSRandall Stewart 			 * CMT, no data sent to this dest can be marked for
3231c105859eSRandall Stewart 			 * FR using this SACK.
3232f8829a4aSRandall Stewart 			 */
3233f8829a4aSRandall Stewart 			continue;
323420b07a4dSMichael Tuexen 		} else if (tp1->whoTo && SCTP_TSN_GT(tp1->rec.data.TSN_seq,
323520b07a4dSMichael Tuexen 		    tp1->whoTo->this_sack_highest_newack)) {
3236f8829a4aSRandall Stewart 			/*
3237f8829a4aSRandall Stewart 			 * CMT: New acks were receieved for data sent to
3238f8829a4aSRandall Stewart 			 * this dest. But no new acks were seen for data
3239f8829a4aSRandall Stewart 			 * sent after tp1. Therefore, according to the SFR
3240f8829a4aSRandall Stewart 			 * algo for CMT, tp1 cannot be marked for FR using
3241f8829a4aSRandall Stewart 			 * this SACK. This step covers part of the DAC algo
3242f8829a4aSRandall Stewart 			 * and the HTNA algo as well.
3243f8829a4aSRandall Stewart 			 */
3244f8829a4aSRandall Stewart 			continue;
3245f8829a4aSRandall Stewart 		}
3246f8829a4aSRandall Stewart 		/*
3247f8829a4aSRandall Stewart 		 * Here we check to see if we were have already done a FR
3248f8829a4aSRandall Stewart 		 * and if so we see if the biggest TSN we saw in the sack is
3249f8829a4aSRandall Stewart 		 * smaller than the recovery point. If so we don't strike
3250f8829a4aSRandall Stewart 		 * the tsn... otherwise we CAN strike the TSN.
3251f8829a4aSRandall Stewart 		 */
3252f8829a4aSRandall Stewart 		/*
325342551e99SRandall Stewart 		 * @@@ JRI: Check for CMT if (accum_moved &&
325442551e99SRandall Stewart 		 * asoc->fast_retran_loss_recovery && (sctp_cmt_on_off ==
325542551e99SRandall Stewart 		 * 0)) {
3256f8829a4aSRandall Stewart 		 */
325742551e99SRandall Stewart 		if (accum_moved && asoc->fast_retran_loss_recovery) {
3258f8829a4aSRandall Stewart 			/*
3259f8829a4aSRandall Stewart 			 * Strike the TSN if in fast-recovery and cum-ack
3260f8829a4aSRandall Stewart 			 * moved.
3261f8829a4aSRandall Stewart 			 */
3262b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3263f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3264f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3265f8829a4aSRandall Stewart 				    tp1->sent,
3266f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
326780fefe0aSRandall Stewart 			}
32685e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3269f8829a4aSRandall Stewart 				tp1->sent++;
32705e54f665SRandall Stewart 			}
32717c99d56fSMichael Tuexen 			if ((asoc->sctp_cmt_on_off > 0) &&
327220083c2eSMichael Tuexen 			    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3273f8829a4aSRandall Stewart 				/*
3274f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3275f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3276f8829a4aSRandall Stewart 				 * because it would have been set to the
3277f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3278f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3279f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3280c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3281c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3282c105859eSRandall Stewart 				 * indicates that two packets have been
3283c105859eSRandall Stewart 				 * received after this missing TSN.
32845e54f665SRandall Stewart 				 */
32855e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
328620b07a4dSMichael Tuexen 				    SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.TSN_seq)) {
3287b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3288f8829a4aSRandall Stewart 						sctp_log_fr(16 + num_dests_sacked,
3289f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3290f8829a4aSRandall Stewart 						    tp1->sent,
3291f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
329280fefe0aSRandall Stewart 					}
3293f8829a4aSRandall Stewart 					tp1->sent++;
3294f8829a4aSRandall Stewart 				}
3295f8829a4aSRandall Stewart 			}
329620083c2eSMichael Tuexen 		} else if ((tp1->rec.data.doing_fast_retransmit) &&
329720083c2eSMichael Tuexen 		    (asoc->sctp_cmt_on_off == 0)) {
3298f8829a4aSRandall Stewart 			/*
3299f8829a4aSRandall Stewart 			 * For those that have done a FR we must take
3300f8829a4aSRandall Stewart 			 * special consideration if we strike. I.e the
3301f8829a4aSRandall Stewart 			 * biggest_newly_acked must be higher than the
3302f8829a4aSRandall Stewart 			 * sending_seq at the time we did the FR.
3303f8829a4aSRandall Stewart 			 */
33045e54f665SRandall Stewart 			if (
3305f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3306f8829a4aSRandall Stewart 			/*
3307f8829a4aSRandall Stewart 			 * If FR's go to new networks, then we must only do
3308f8829a4aSRandall Stewart 			 * this for singly homed asoc's. However if the FR's
3309f8829a4aSRandall Stewart 			 * go to the same network (Armando's work) then its
3310f8829a4aSRandall Stewart 			 * ok to FR multiple times.
3311f8829a4aSRandall Stewart 			 */
33125e54f665SRandall Stewart 			    (asoc->numnets < 2)
3313f8829a4aSRandall Stewart #else
33145e54f665SRandall Stewart 			    (1)
3315f8829a4aSRandall Stewart #endif
33165e54f665SRandall Stewart 			    ) {
33175e54f665SRandall Stewart 
331820b07a4dSMichael Tuexen 				if (SCTP_TSN_GE(biggest_tsn_newly_acked,
3319f8829a4aSRandall Stewart 				    tp1->rec.data.fast_retran_tsn)) {
3320f8829a4aSRandall Stewart 					/*
3321f8829a4aSRandall Stewart 					 * Strike the TSN, since this ack is
3322f8829a4aSRandall Stewart 					 * beyond where things were when we
3323f8829a4aSRandall Stewart 					 * did a FR.
3324f8829a4aSRandall Stewart 					 */
3325b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3326f8829a4aSRandall Stewart 						sctp_log_fr(biggest_tsn_newly_acked,
3327f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3328f8829a4aSRandall Stewart 						    tp1->sent,
3329f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
333080fefe0aSRandall Stewart 					}
33315e54f665SRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3332f8829a4aSRandall Stewart 						tp1->sent++;
33335e54f665SRandall Stewart 					}
3334f8829a4aSRandall Stewart 					strike_flag = 1;
33357c99d56fSMichael Tuexen 					if ((asoc->sctp_cmt_on_off > 0) &&
333620083c2eSMichael Tuexen 					    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3337f8829a4aSRandall Stewart 						/*
3338f8829a4aSRandall Stewart 						 * CMT DAC algorithm: If
3339f8829a4aSRandall Stewart 						 * SACK flag is set to 0,
3340f8829a4aSRandall Stewart 						 * then lowest_newack test
3341f8829a4aSRandall Stewart 						 * will not pass because it
3342f8829a4aSRandall Stewart 						 * would have been set to
3343f8829a4aSRandall Stewart 						 * the cumack earlier. If
3344f8829a4aSRandall Stewart 						 * not already to be rtx'd,
3345f8829a4aSRandall Stewart 						 * If not a mixed sack and
3346f8829a4aSRandall Stewart 						 * if tp1 is not between two
3347f8829a4aSRandall Stewart 						 * sacked TSNs, then mark by
3348c105859eSRandall Stewart 						 * one more. NOTE that we
3349c105859eSRandall Stewart 						 * are marking by one
3350c105859eSRandall Stewart 						 * additional time since the
3351c105859eSRandall Stewart 						 * SACK DAC flag indicates
3352c105859eSRandall Stewart 						 * that two packets have
3353c105859eSRandall Stewart 						 * been received after this
3354c105859eSRandall Stewart 						 * missing TSN.
3355f8829a4aSRandall Stewart 						 */
33565e54f665SRandall Stewart 						if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
33575e54f665SRandall Stewart 						    (num_dests_sacked == 1) &&
335820b07a4dSMichael Tuexen 						    SCTP_TSN_GT(this_sack_lowest_newack,
335920b07a4dSMichael Tuexen 						    tp1->rec.data.TSN_seq)) {
3360b3f1ea41SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3361f8829a4aSRandall Stewart 								sctp_log_fr(32 + num_dests_sacked,
3362f8829a4aSRandall Stewart 								    tp1->rec.data.TSN_seq,
3363f8829a4aSRandall Stewart 								    tp1->sent,
3364f8829a4aSRandall Stewart 								    SCTP_FR_LOG_STRIKE_CHUNK);
336580fefe0aSRandall Stewart 							}
33665e54f665SRandall Stewart 							if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3367f8829a4aSRandall Stewart 								tp1->sent++;
33685e54f665SRandall Stewart 							}
3369f8829a4aSRandall Stewart 						}
3370f8829a4aSRandall Stewart 					}
3371f8829a4aSRandall Stewart 				}
3372f8829a4aSRandall Stewart 			}
3373f8829a4aSRandall Stewart 			/*
337442551e99SRandall Stewart 			 * JRI: TODO: remove code for HTNA algo. CMT's SFR
337542551e99SRandall Stewart 			 * algo covers HTNA.
3376f8829a4aSRandall Stewart 			 */
337720b07a4dSMichael Tuexen 		} else if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
337820b07a4dSMichael Tuexen 		    biggest_tsn_newly_acked)) {
3379f8829a4aSRandall Stewart 			/*
3380f8829a4aSRandall Stewart 			 * We don't strike these: This is the  HTNA
3381f8829a4aSRandall Stewart 			 * algorithm i.e. we don't strike If our TSN is
3382f8829a4aSRandall Stewart 			 * larger than the Highest TSN Newly Acked.
3383f8829a4aSRandall Stewart 			 */
3384f8829a4aSRandall Stewart 			;
3385f8829a4aSRandall Stewart 		} else {
3386f8829a4aSRandall Stewart 			/* Strike the TSN */
3387b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3388f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3389f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3390f8829a4aSRandall Stewart 				    tp1->sent,
3391f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
339280fefe0aSRandall Stewart 			}
33935e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3394f8829a4aSRandall Stewart 				tp1->sent++;
33955e54f665SRandall Stewart 			}
33967c99d56fSMichael Tuexen 			if ((asoc->sctp_cmt_on_off > 0) &&
339720083c2eSMichael Tuexen 			    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3398f8829a4aSRandall Stewart 				/*
3399f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3400f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3401f8829a4aSRandall Stewart 				 * because it would have been set to the
3402f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3403f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3404f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3405c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3406c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3407c105859eSRandall Stewart 				 * indicates that two packets have been
3408c105859eSRandall Stewart 				 * received after this missing TSN.
3409f8829a4aSRandall Stewart 				 */
34105e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
341120b07a4dSMichael Tuexen 				    SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.TSN_seq)) {
3412b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3413f8829a4aSRandall Stewart 						sctp_log_fr(48 + num_dests_sacked,
3414f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3415f8829a4aSRandall Stewart 						    tp1->sent,
3416f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
341780fefe0aSRandall Stewart 					}
3418f8829a4aSRandall Stewart 					tp1->sent++;
3419f8829a4aSRandall Stewart 				}
3420f8829a4aSRandall Stewart 			}
3421f8829a4aSRandall Stewart 		}
3422f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3423f8829a4aSRandall Stewart 			struct sctp_nets *alt;
3424f8829a4aSRandall Stewart 
3425544e35bdSRandall Stewart 			/* fix counts and things */
3426544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3427544e35bdSRandall Stewart 				sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND,
3428544e35bdSRandall Stewart 				    (tp1->whoTo ? (tp1->whoTo->flight_size) : 0),
3429544e35bdSRandall Stewart 				    tp1->book_size,
3430544e35bdSRandall Stewart 				    (uintptr_t) tp1->whoTo,
3431544e35bdSRandall Stewart 				    tp1->rec.data.TSN_seq);
3432544e35bdSRandall Stewart 			}
3433544e35bdSRandall Stewart 			if (tp1->whoTo) {
3434544e35bdSRandall Stewart 				tp1->whoTo->net_ack++;
3435544e35bdSRandall Stewart 				sctp_flight_size_decrease(tp1);
3436299108c5SRandall Stewart 				if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3437299108c5SRandall Stewart 					(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3438299108c5SRandall Stewart 					    tp1);
3439299108c5SRandall Stewart 				}
3440544e35bdSRandall Stewart 			}
3441544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
3442544e35bdSRandall Stewart 				sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
3443544e35bdSRandall Stewart 				    asoc->peers_rwnd, tp1->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3444544e35bdSRandall Stewart 			}
3445544e35bdSRandall Stewart 			/* add back to the rwnd */
3446544e35bdSRandall Stewart 			asoc->peers_rwnd += (tp1->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3447544e35bdSRandall Stewart 
3448544e35bdSRandall Stewart 			/* remove from the total flight */
3449544e35bdSRandall Stewart 			sctp_total_flight_decrease(stcb, tp1);
3450544e35bdSRandall Stewart 
3451475d0674SMichael Tuexen 			if ((stcb->asoc.peer_supports_prsctp) &&
3452475d0674SMichael Tuexen 			    (PR_SCTP_RTX_ENABLED(tp1->flags))) {
3453475d0674SMichael Tuexen 				/*
3454475d0674SMichael Tuexen 				 * Has it been retransmitted tv_sec times? -
3455475d0674SMichael Tuexen 				 * we store the retran count there.
3456475d0674SMichael Tuexen 				 */
3457475d0674SMichael Tuexen 				if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) {
3458475d0674SMichael Tuexen 					/* Yes, so drop it */
3459475d0674SMichael Tuexen 					if (tp1->data != NULL) {
34601edc9dbaSMichael Tuexen 						(void)sctp_release_pr_sctp_chunk(stcb, tp1, 1,
3461475d0674SMichael Tuexen 						    SCTP_SO_NOT_LOCKED);
3462475d0674SMichael Tuexen 					}
3463475d0674SMichael Tuexen 					/* Make sure to flag we had a FR */
3464475d0674SMichael Tuexen 					tp1->whoTo->net_ack++;
3465475d0674SMichael Tuexen 					continue;
3466475d0674SMichael Tuexen 				}
3467475d0674SMichael Tuexen 			}
3468cd3fd531SMichael Tuexen 			/*
3469cd3fd531SMichael Tuexen 			 * SCTP_PRINTF("OK, we are now ready to FR this
3470cd3fd531SMichael Tuexen 			 * guy\n");
3471cd3fd531SMichael Tuexen 			 */
3472b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3473f8829a4aSRandall Stewart 				sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count,
3474f8829a4aSRandall Stewart 				    0, SCTP_FR_MARKED);
347580fefe0aSRandall Stewart 			}
3476f8829a4aSRandall Stewart 			if (strike_flag) {
3477f8829a4aSRandall Stewart 				/* This is a subsequent FR */
3478f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_sendmultfastretrans);
3479f8829a4aSRandall Stewart 			}
34805e54f665SRandall Stewart 			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
34817c99d56fSMichael Tuexen 			if (asoc->sctp_cmt_on_off > 0) {
3482f8829a4aSRandall Stewart 				/*
3483f8829a4aSRandall Stewart 				 * CMT: Using RTX_SSTHRESH policy for CMT.
3484f8829a4aSRandall Stewart 				 * If CMT is being used, then pick dest with
3485f8829a4aSRandall Stewart 				 * largest ssthresh for any retransmission.
3486f8829a4aSRandall Stewart 				 */
3487f8829a4aSRandall Stewart 				tp1->no_fr_allowed = 1;
3488f8829a4aSRandall Stewart 				alt = tp1->whoTo;
34893c503c28SRandall Stewart 				/* sa_ignore NO_NULL_CHK */
349020083c2eSMichael Tuexen 				if (asoc->sctp_cmt_pf > 0) {
3491b54d3a6cSRandall Stewart 					/*
3492b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If CMT PF is on,
3493b54d3a6cSRandall Stewart 					 * use the PF version of
3494b54d3a6cSRandall Stewart 					 * find_alt_net()
3495b54d3a6cSRandall Stewart 					 */
3496b54d3a6cSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 2);
3497b54d3a6cSRandall Stewart 				} else {
3498b54d3a6cSRandall Stewart 					/*
3499b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If only CMT is on,
3500b54d3a6cSRandall Stewart 					 * use the CMT version of
3501b54d3a6cSRandall Stewart 					 * find_alt_net()
3502b54d3a6cSRandall Stewart 					 */
350352be287eSRandall Stewart 					/* sa_ignore NO_NULL_CHK */
3504f8829a4aSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 1);
3505b54d3a6cSRandall Stewart 				}
3506ad81507eSRandall Stewart 				if (alt == NULL) {
3507ad81507eSRandall Stewart 					alt = tp1->whoTo;
3508ad81507eSRandall Stewart 				}
3509f8829a4aSRandall Stewart 				/*
3510f8829a4aSRandall Stewart 				 * CUCv2: If a different dest is picked for
3511f8829a4aSRandall Stewart 				 * the retransmission, then new
3512f8829a4aSRandall Stewart 				 * (rtx-)pseudo_cumack needs to be tracked
3513f8829a4aSRandall Stewart 				 * for orig dest. Let CUCv2 track new (rtx-)
3514f8829a4aSRandall Stewart 				 * pseudo-cumack always.
3515f8829a4aSRandall Stewart 				 */
3516ad81507eSRandall Stewart 				if (tp1->whoTo) {
3517f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
3518f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3519ad81507eSRandall Stewart 				}
3520f8829a4aSRandall Stewart 			} else {/* CMT is OFF */
3521f8829a4aSRandall Stewart 
3522f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3523f8829a4aSRandall Stewart 				/* Can we find an alternate? */
3524f8829a4aSRandall Stewart 				alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0);
3525f8829a4aSRandall Stewart #else
3526f8829a4aSRandall Stewart 				/*
3527f8829a4aSRandall Stewart 				 * default behavior is to NOT retransmit
3528f8829a4aSRandall Stewart 				 * FR's to an alternate. Armando Caro's
3529f8829a4aSRandall Stewart 				 * paper details why.
3530f8829a4aSRandall Stewart 				 */
3531f8829a4aSRandall Stewart 				alt = tp1->whoTo;
3532f8829a4aSRandall Stewart #endif
3533f8829a4aSRandall Stewart 			}
3534f8829a4aSRandall Stewart 
3535f8829a4aSRandall Stewart 			tp1->rec.data.doing_fast_retransmit = 1;
3536f8829a4aSRandall Stewart 			tot_retrans++;
3537f8829a4aSRandall Stewart 			/* mark the sending seq for possible subsequent FR's */
3538f8829a4aSRandall Stewart 			/*
3539cd3fd531SMichael Tuexen 			 * SCTP_PRINTF("Marking TSN for FR new value %x\n",
3540f8829a4aSRandall Stewart 			 * (uint32_t)tpi->rec.data.TSN_seq);
3541f8829a4aSRandall Stewart 			 */
3542f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->send_queue)) {
3543f8829a4aSRandall Stewart 				/*
3544f8829a4aSRandall Stewart 				 * If the queue of send is empty then its
3545f8829a4aSRandall Stewart 				 * the next sequence number that will be
3546f8829a4aSRandall Stewart 				 * assigned so we subtract one from this to
3547f8829a4aSRandall Stewart 				 * get the one we last sent.
3548f8829a4aSRandall Stewart 				 */
3549f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn = sending_seq;
3550f8829a4aSRandall Stewart 			} else {
3551f8829a4aSRandall Stewart 				/*
3552f8829a4aSRandall Stewart 				 * If there are chunks on the send queue
3553f8829a4aSRandall Stewart 				 * (unsent data that has made it from the
3554f8829a4aSRandall Stewart 				 * stream queues but not out the door, we
3555f8829a4aSRandall Stewart 				 * take the first one (which will have the
3556f8829a4aSRandall Stewart 				 * lowest TSN) and subtract one to get the
3557f8829a4aSRandall Stewart 				 * one we last sent.
3558f8829a4aSRandall Stewart 				 */
3559f8829a4aSRandall Stewart 				struct sctp_tmit_chunk *ttt;
3560f8829a4aSRandall Stewart 
3561f8829a4aSRandall Stewart 				ttt = TAILQ_FIRST(&asoc->send_queue);
3562f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn =
3563f8829a4aSRandall Stewart 				    ttt->rec.data.TSN_seq;
3564f8829a4aSRandall Stewart 			}
3565f8829a4aSRandall Stewart 
3566f8829a4aSRandall Stewart 			if (tp1->do_rtt) {
3567f8829a4aSRandall Stewart 				/*
3568f8829a4aSRandall Stewart 				 * this guy had a RTO calculation pending on
3569f8829a4aSRandall Stewart 				 * it, cancel it
3570f8829a4aSRandall Stewart 				 */
357160990c0cSMichael Tuexen 				if ((tp1->whoTo != NULL) &&
357260990c0cSMichael Tuexen 				    (tp1->whoTo->rto_needed == 0)) {
3573f79aab18SRandall Stewart 					tp1->whoTo->rto_needed = 1;
3574f79aab18SRandall Stewart 				}
3575f8829a4aSRandall Stewart 				tp1->do_rtt = 0;
3576f8829a4aSRandall Stewart 			}
3577f8829a4aSRandall Stewart 			if (alt != tp1->whoTo) {
3578f8829a4aSRandall Stewart 				/* yes, there is an alternate. */
3579f8829a4aSRandall Stewart 				sctp_free_remote_addr(tp1->whoTo);
35803c503c28SRandall Stewart 				/* sa_ignore FREED_MEMORY */
3581f8829a4aSRandall Stewart 				tp1->whoTo = alt;
3582f8829a4aSRandall Stewart 				atomic_add_int(&alt->ref_count, 1);
3583f8829a4aSRandall Stewart 			}
3584f8829a4aSRandall Stewart 		}
35854a9ef3f8SMichael Tuexen 	}
3586f8829a4aSRandall Stewart }
3587f8829a4aSRandall Stewart 
3588f8829a4aSRandall Stewart struct sctp_tmit_chunk *
3589f8829a4aSRandall Stewart sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
3590f8829a4aSRandall Stewart     struct sctp_association *asoc)
3591f8829a4aSRandall Stewart {
3592f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL;
3593f8829a4aSRandall Stewart 	struct timeval now;
3594f8829a4aSRandall Stewart 	int now_filled = 0;
3595f8829a4aSRandall Stewart 
3596f8829a4aSRandall Stewart 	if (asoc->peer_supports_prsctp == 0) {
3597f8829a4aSRandall Stewart 		return (NULL);
3598f8829a4aSRandall Stewart 	}
35994a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
3600f8829a4aSRandall Stewart 		if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
3601f8829a4aSRandall Stewart 		    tp1->sent != SCTP_DATAGRAM_RESEND) {
3602f8829a4aSRandall Stewart 			/* no chance to advance, out of here */
3603f8829a4aSRandall Stewart 			break;
3604f8829a4aSRandall Stewart 		}
36050c0982b8SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
36060c0982b8SRandall Stewart 			if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
36070c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
36080c0982b8SRandall Stewart 				    asoc->advanced_peer_ack_point,
36090c0982b8SRandall Stewart 				    tp1->rec.data.TSN_seq, 0, 0);
36100c0982b8SRandall Stewart 			}
36110c0982b8SRandall Stewart 		}
3612f8829a4aSRandall Stewart 		if (!PR_SCTP_ENABLED(tp1->flags)) {
3613f8829a4aSRandall Stewart 			/*
3614f8829a4aSRandall Stewart 			 * We can't fwd-tsn past any that are reliable aka
3615f8829a4aSRandall Stewart 			 * retransmitted until the asoc fails.
3616f8829a4aSRandall Stewart 			 */
3617f8829a4aSRandall Stewart 			break;
3618f8829a4aSRandall Stewart 		}
3619f8829a4aSRandall Stewart 		if (!now_filled) {
36206e55db54SRandall Stewart 			(void)SCTP_GETTIME_TIMEVAL(&now);
3621f8829a4aSRandall Stewart 			now_filled = 1;
3622f8829a4aSRandall Stewart 		}
3623f8829a4aSRandall Stewart 		/*
3624f8829a4aSRandall Stewart 		 * now we got a chunk which is marked for another
3625f8829a4aSRandall Stewart 		 * retransmission to a PR-stream but has run out its chances
3626f8829a4aSRandall Stewart 		 * already maybe OR has been marked to skip now. Can we skip
3627f8829a4aSRandall Stewart 		 * it if its a resend?
3628f8829a4aSRandall Stewart 		 */
3629f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND &&
3630f8829a4aSRandall Stewart 		    (PR_SCTP_TTL_ENABLED(tp1->flags))) {
3631f8829a4aSRandall Stewart 			/*
3632f8829a4aSRandall Stewart 			 * Now is this one marked for resend and its time is
3633f8829a4aSRandall Stewart 			 * now up?
3634f8829a4aSRandall Stewart 			 */
3635f8829a4aSRandall Stewart 			if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3636f8829a4aSRandall Stewart 				/* Yes so drop it */
3637f8829a4aSRandall Stewart 				if (tp1->data) {
3638ad81507eSRandall Stewart 					(void)sctp_release_pr_sctp_chunk(stcb, tp1,
36391edc9dbaSMichael Tuexen 					    1, SCTP_SO_NOT_LOCKED);
3640f8829a4aSRandall Stewart 				}
3641f8829a4aSRandall Stewart 			} else {
3642f8829a4aSRandall Stewart 				/*
3643f8829a4aSRandall Stewart 				 * No, we are done when hit one for resend
3644f8829a4aSRandall Stewart 				 * whos time as not expired.
3645f8829a4aSRandall Stewart 				 */
3646f8829a4aSRandall Stewart 				break;
3647f8829a4aSRandall Stewart 			}
3648f8829a4aSRandall Stewart 		}
3649f8829a4aSRandall Stewart 		/*
3650f8829a4aSRandall Stewart 		 * Ok now if this chunk is marked to drop it we can clean up
3651f8829a4aSRandall Stewart 		 * the chunk, advance our peer ack point and we can check
3652f8829a4aSRandall Stewart 		 * the next chunk.
3653f8829a4aSRandall Stewart 		 */
365444fbe462SRandall Stewart 		if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
3655f8829a4aSRandall Stewart 			/* advance PeerAckPoint goes forward */
365620b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, asoc->advanced_peer_ack_point)) {
3657f8829a4aSRandall Stewart 				asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq;
3658f8829a4aSRandall Stewart 				a_adv = tp1;
36590c0982b8SRandall Stewart 			} else if (tp1->rec.data.TSN_seq == asoc->advanced_peer_ack_point) {
36600c0982b8SRandall Stewart 				/* No update but we do save the chk */
36610c0982b8SRandall Stewart 				a_adv = tp1;
36620c0982b8SRandall Stewart 			}
3663f8829a4aSRandall Stewart 		} else {
3664f8829a4aSRandall Stewart 			/*
3665f8829a4aSRandall Stewart 			 * If it is still in RESEND we can advance no
3666f8829a4aSRandall Stewart 			 * further
3667f8829a4aSRandall Stewart 			 */
3668f8829a4aSRandall Stewart 			break;
3669f8829a4aSRandall Stewart 		}
3670f8829a4aSRandall Stewart 	}
3671f8829a4aSRandall Stewart 	return (a_adv);
3672f8829a4aSRandall Stewart }
3673f8829a4aSRandall Stewart 
36740c0982b8SRandall Stewart static int
3675c105859eSRandall Stewart sctp_fs_audit(struct sctp_association *asoc)
3676bff64a4dSRandall Stewart {
3677bff64a4dSRandall Stewart 	struct sctp_tmit_chunk *chk;
3678bff64a4dSRandall Stewart 	int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0;
36790c0982b8SRandall Stewart 	int entry_flight, entry_cnt, ret;
36800c0982b8SRandall Stewart 
36810c0982b8SRandall Stewart 	entry_flight = asoc->total_flight;
36820c0982b8SRandall Stewart 	entry_cnt = asoc->total_flight_count;
36830c0982b8SRandall Stewart 	ret = 0;
36840c0982b8SRandall Stewart 
36850c0982b8SRandall Stewart 	if (asoc->pr_sctp_cnt >= asoc->sent_queue_cnt)
36860c0982b8SRandall Stewart 		return (0);
3687bff64a4dSRandall Stewart 
3688bff64a4dSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
3689bff64a4dSRandall Stewart 		if (chk->sent < SCTP_DATAGRAM_RESEND) {
3690cd3fd531SMichael Tuexen 			SCTP_PRINTF("Chk TSN:%u size:%d inflight cnt:%d\n",
36910c0982b8SRandall Stewart 			    chk->rec.data.TSN_seq,
36920c0982b8SRandall Stewart 			    chk->send_size,
3693cd3fd531SMichael Tuexen 			    chk->snd_count);
3694bff64a4dSRandall Stewart 			inflight++;
3695bff64a4dSRandall Stewart 		} else if (chk->sent == SCTP_DATAGRAM_RESEND) {
3696bff64a4dSRandall Stewart 			resend++;
3697bff64a4dSRandall Stewart 		} else if (chk->sent < SCTP_DATAGRAM_ACKED) {
3698bff64a4dSRandall Stewart 			inbetween++;
3699bff64a4dSRandall Stewart 		} else if (chk->sent > SCTP_DATAGRAM_ACKED) {
3700bff64a4dSRandall Stewart 			above++;
3701bff64a4dSRandall Stewart 		} else {
3702bff64a4dSRandall Stewart 			acked++;
3703bff64a4dSRandall Stewart 		}
3704bff64a4dSRandall Stewart 	}
3705f1f73e57SRandall Stewart 
3706c105859eSRandall Stewart 	if ((inflight > 0) || (inbetween > 0)) {
3707f1f73e57SRandall Stewart #ifdef INVARIANTS
3708c105859eSRandall Stewart 		panic("Flight size-express incorrect? \n");
3709f1f73e57SRandall Stewart #else
3710cd3fd531SMichael Tuexen 		SCTP_PRINTF("asoc->total_flight:%d cnt:%d\n",
37110c0982b8SRandall Stewart 		    entry_flight, entry_cnt);
37120c0982b8SRandall Stewart 
37130c0982b8SRandall Stewart 		SCTP_PRINTF("Flight size-express incorrect F:%d I:%d R:%d Ab:%d ACK:%d\n",
37140c0982b8SRandall Stewart 		    inflight, inbetween, resend, above, acked);
37150c0982b8SRandall Stewart 		ret = 1;
3716f1f73e57SRandall Stewart #endif
3717bff64a4dSRandall Stewart 	}
37180c0982b8SRandall Stewart 	return (ret);
3719c105859eSRandall Stewart }
3720c105859eSRandall Stewart 
3721c105859eSRandall Stewart 
3722c105859eSRandall Stewart static void
3723c105859eSRandall Stewart sctp_window_probe_recovery(struct sctp_tcb *stcb,
3724c105859eSRandall Stewart     struct sctp_association *asoc,
3725c105859eSRandall Stewart     struct sctp_tmit_chunk *tp1)
3726c105859eSRandall Stewart {
3727dfb11ef8SRandall Stewart 	tp1->window_probe = 0;
37285171328bSRandall Stewart 	if ((tp1->sent >= SCTP_DATAGRAM_ACKED) || (tp1->data == NULL)) {
3729dfb11ef8SRandall Stewart 		/* TSN's skipped we do NOT move back. */
3730dfb11ef8SRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DWN_WP_FWD,
3731dfb11ef8SRandall Stewart 		    tp1->whoTo->flight_size,
3732dfb11ef8SRandall Stewart 		    tp1->book_size,
3733dfb11ef8SRandall Stewart 		    (uintptr_t) tp1->whoTo,
3734dfb11ef8SRandall Stewart 		    tp1->rec.data.TSN_seq);
3735dfb11ef8SRandall Stewart 		return;
3736dfb11ef8SRandall Stewart 	}
37375171328bSRandall Stewart 	/* First setup this by shrinking flight */
3738299108c5SRandall Stewart 	if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3739299108c5SRandall Stewart 		(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3740299108c5SRandall Stewart 		    tp1);
3741299108c5SRandall Stewart 	}
37425171328bSRandall Stewart 	sctp_flight_size_decrease(tp1);
37435171328bSRandall Stewart 	sctp_total_flight_decrease(stcb, tp1);
37445171328bSRandall Stewart 	/* Now mark for resend */
37455171328bSRandall Stewart 	tp1->sent = SCTP_DATAGRAM_RESEND;
3746791437b5SRandall Stewart 	sctp_ucount_incr(asoc->sent_queue_retran_cnt);
3747791437b5SRandall Stewart 
3748b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3749c105859eSRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP,
3750c105859eSRandall Stewart 		    tp1->whoTo->flight_size,
3751c105859eSRandall Stewart 		    tp1->book_size,
3752c105859eSRandall Stewart 		    (uintptr_t) tp1->whoTo,
3753c105859eSRandall Stewart 		    tp1->rec.data.TSN_seq);
375480fefe0aSRandall Stewart 	}
3755c105859eSRandall Stewart }
3756c105859eSRandall Stewart 
3757f8829a4aSRandall Stewart void
3758f8829a4aSRandall Stewart sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
3759899288aeSRandall Stewart     uint32_t rwnd, int *abort_now, int ecne_seen)
3760f8829a4aSRandall Stewart {
3761f8829a4aSRandall Stewart 	struct sctp_nets *net;
3762f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3763f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
37645e54f665SRandall Stewart 	uint32_t old_rwnd;
37655e54f665SRandall Stewart 	int win_probe_recovery = 0;
3766c105859eSRandall Stewart 	int win_probe_recovered = 0;
3767d06c82f1SRandall Stewart 	int j, done_once = 0;
3768f79aab18SRandall Stewart 	int rto_ok = 1;
3769f8829a4aSRandall Stewart 
3770b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
3771d06c82f1SRandall Stewart 		sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
3772d06c82f1SRandall Stewart 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
377380fefe0aSRandall Stewart 	}
3774f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
377518e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
377618e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
377718e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
377818e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
377918e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
378018e198d3SRandall Stewart 	}
378118e198d3SRandall Stewart #endif
3782f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
3783d06c82f1SRandall Stewart 	old_rwnd = asoc->peers_rwnd;
378420b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->last_acked_seq, cumack)) {
37855e54f665SRandall Stewart 		/* old ack */
37865e54f665SRandall Stewart 		return;
3787d06c82f1SRandall Stewart 	} else if (asoc->last_acked_seq == cumack) {
3788d06c82f1SRandall Stewart 		/* Window update sack */
3789d06c82f1SRandall Stewart 		asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
379044fbe462SRandall Stewart 		    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
3791d06c82f1SRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
3792d06c82f1SRandall Stewart 			/* SWS sender side engages */
3793d06c82f1SRandall Stewart 			asoc->peers_rwnd = 0;
3794d06c82f1SRandall Stewart 		}
3795d06c82f1SRandall Stewart 		if (asoc->peers_rwnd > old_rwnd) {
3796d06c82f1SRandall Stewart 			goto again;
3797d06c82f1SRandall Stewart 		}
3798d06c82f1SRandall Stewart 		return;
37995e54f665SRandall Stewart 	}
3800f8829a4aSRandall Stewart 	/* First setup for CC stuff */
3801f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3802a21779f0SRandall Stewart 		if (SCTP_TSN_GT(cumack, net->cwr_window_tsn)) {
3803a21779f0SRandall Stewart 			/* Drag along the window_tsn for cwr's */
3804a21779f0SRandall Stewart 			net->cwr_window_tsn = cumack;
3805a21779f0SRandall Stewart 		}
3806f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
3807f8829a4aSRandall Stewart 		net->net_ack = 0;
3808f8829a4aSRandall Stewart 		net->net_ack2 = 0;
3809132dea7dSRandall Stewart 
3810132dea7dSRandall Stewart 		/*
3811132dea7dSRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
3812132dea7dSRandall Stewart 		 * SACK processing
3813132dea7dSRandall Stewart 		 */
3814132dea7dSRandall Stewart 		net->new_pseudo_cumack = 0;
3815132dea7dSRandall Stewart 		net->will_exit_fast_recovery = 0;
3816299108c5SRandall Stewart 		if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) {
3817299108c5SRandall Stewart 			(*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) (stcb, net);
3818299108c5SRandall Stewart 		}
3819f8829a4aSRandall Stewart 	}
3820b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
3821139bc87fSRandall Stewart 		uint32_t send_s;
3822139bc87fSRandall Stewart 
3823c105859eSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3824c105859eSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
3825c105859eSRandall Stewart 			    sctpchunk_listhead);
3826c105859eSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
3827139bc87fSRandall Stewart 		} else {
3828c105859eSRandall Stewart 			send_s = asoc->sending_seq;
3829139bc87fSRandall Stewart 		}
383020b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(cumack, send_s)) {
3831c105859eSRandall Stewart #ifndef INVARIANTS
3832139bc87fSRandall Stewart 			struct mbuf *oper;
3833139bc87fSRandall Stewart 
3834c105859eSRandall Stewart #endif
3835c105859eSRandall Stewart #ifdef INVARIANTS
3836c105859eSRandall Stewart 			panic("Impossible sack 1");
3837c105859eSRandall Stewart #else
3838b5c16493SMichael Tuexen 
3839139bc87fSRandall Stewart 			*abort_now = 1;
3840139bc87fSRandall Stewart 			/* XXX */
3841139bc87fSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3842139bc87fSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
3843139bc87fSRandall Stewart 			if (oper) {
3844139bc87fSRandall Stewart 				struct sctp_paramhdr *ph;
3845139bc87fSRandall Stewart 				uint32_t *ippp;
3846139bc87fSRandall Stewart 
3847139bc87fSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
3848139bc87fSRandall Stewart 				    sizeof(uint32_t);
3849139bc87fSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
3850139bc87fSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
3851139bc87fSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
3852139bc87fSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
3853139bc87fSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
3854139bc87fSRandall Stewart 			}
3855139bc87fSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
3856a2b42326SMichael Tuexen 			sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
3857139bc87fSRandall Stewart 			return;
3858139bc87fSRandall Stewart #endif
3859139bc87fSRandall Stewart 		}
3860139bc87fSRandall Stewart 	}
3861f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = cumack;
3862b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
3863c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
3864c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
3865c4739e2fSRandall Stewart 		    0,
3866c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
3867c4739e2fSRandall Stewart 		    __LINE__);
3868c4739e2fSRandall Stewart 	}
3869f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
387020b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cumack, asoc->last_acked_seq)) {
3871f8829a4aSRandall Stewart 		/* process the new consecutive TSN first */
38724a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
387320b07a4dSMichael Tuexen 			if (SCTP_TSN_GE(cumack, tp1->rec.data.TSN_seq)) {
387418e198d3SRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
3875cd3fd531SMichael Tuexen 					SCTP_PRINTF("Warning, an unsent is now acked?\n");
387618e198d3SRandall Stewart 				}
3877f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
3878f8829a4aSRandall Stewart 					/*
387918e198d3SRandall Stewart 					 * If it is less than ACKED, it is
388018e198d3SRandall Stewart 					 * now no-longer in flight. Higher
388118e198d3SRandall Stewart 					 * values may occur during marking
3882f8829a4aSRandall Stewart 					 */
3883c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3884b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3885c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
3886a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
3887a5d547adSRandall Stewart 							    tp1->book_size,
3888c105859eSRandall Stewart 							    (uintptr_t) tp1->whoTo,
3889a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
389080fefe0aSRandall Stewart 						}
3891c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
3892299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3893299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3894299108c5SRandall Stewart 							    tp1);
3895299108c5SRandall Stewart 						}
389604ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
3897c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
3898f8829a4aSRandall Stewart 					}
3899f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
3900f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
3901f8829a4aSRandall Stewart 						/*
390218e198d3SRandall Stewart 						 * True non-retransmited
3903f8829a4aSRandall Stewart 						 * chunk
3904f8829a4aSRandall Stewart 						 */
3905f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
3906f8829a4aSRandall Stewart 						    tp1->send_size;
3907f8829a4aSRandall Stewart 
3908f8829a4aSRandall Stewart 						/* update RTO too? */
390962c1ff9cSRandall Stewart 						if (tp1->do_rtt) {
3910f79aab18SRandall Stewart 							if (rto_ok) {
3911f8829a4aSRandall Stewart 								tp1->whoTo->RTO =
391204ee05e8SRandall Stewart 								/*
391304ee05e8SRandall Stewart 								 * sa_ignore
3914f79aab18SRandall Stewart 								 * NO_NULL_CH
3915f79aab18SRandall Stewart 								 * K
391604ee05e8SRandall Stewart 								 */
3917f8829a4aSRandall Stewart 								    sctp_calculate_rto(stcb,
3918f8829a4aSRandall Stewart 								    asoc, tp1->whoTo,
391918e198d3SRandall Stewart 								    &tp1->sent_rcv_time,
3920899288aeSRandall Stewart 								    sctp_align_safe_nocopy,
3921f79aab18SRandall Stewart 								    SCTP_RTT_FROM_DATA);
3922f79aab18SRandall Stewart 								rto_ok = 0;
3923f79aab18SRandall Stewart 							}
3924f79aab18SRandall Stewart 							if (tp1->whoTo->rto_needed == 0) {
3925f79aab18SRandall Stewart 								tp1->whoTo->rto_needed = 1;
3926f79aab18SRandall Stewart 							}
3927f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
3928f8829a4aSRandall Stewart 						}
3929f8829a4aSRandall Stewart 					}
3930132dea7dSRandall Stewart 					/*
393118e198d3SRandall Stewart 					 * CMT: CUCv2 algorithm. From the
393218e198d3SRandall Stewart 					 * cumack'd TSNs, for each TSN being
393318e198d3SRandall Stewart 					 * acked for the first time, set the
393418e198d3SRandall Stewart 					 * following variables for the
393518e198d3SRandall Stewart 					 * corresp destination.
393618e198d3SRandall Stewart 					 * new_pseudo_cumack will trigger a
393718e198d3SRandall Stewart 					 * cwnd update.
393818e198d3SRandall Stewart 					 * find_(rtx_)pseudo_cumack will
393918e198d3SRandall Stewart 					 * trigger search for the next
394018e198d3SRandall Stewart 					 * expected (rtx-)pseudo-cumack.
3941132dea7dSRandall Stewart 					 */
3942132dea7dSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
3943132dea7dSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
3944132dea7dSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3945132dea7dSRandall Stewart 
3946b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
394704ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
3948f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
394980fefe0aSRandall Stewart 					}
3950f8829a4aSRandall Stewart 				}
3951f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3952f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
3953f8829a4aSRandall Stewart 				}
395442551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
395542551e99SRandall Stewart 					/* deflate the cwnd */
395642551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
395742551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
395842551e99SRandall Stewart 				}
3959f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
3960f8829a4aSRandall Stewart 				TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
3961f8829a4aSRandall Stewart 				if (tp1->data) {
396204ee05e8SRandall Stewart 					/* sa_ignore NO_NULL_CHK */
3963f8829a4aSRandall Stewart 					sctp_free_bufspace(stcb, asoc, tp1, 1);
3964f8829a4aSRandall Stewart 					sctp_m_freem(tp1->data);
39654a9ef3f8SMichael Tuexen 					tp1->data = NULL;
3966f8829a4aSRandall Stewart 				}
3967b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3968f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
3969f8829a4aSRandall Stewart 					    cumack,
3970f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
3971f8829a4aSRandall Stewart 					    0,
3972f8829a4aSRandall Stewart 					    0,
3973f8829a4aSRandall Stewart 					    SCTP_LOG_FREE_SENT);
397480fefe0aSRandall Stewart 				}
3975f8829a4aSRandall Stewart 				asoc->sent_queue_cnt--;
3976689e6a5fSMichael Tuexen 				sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED);
397718e198d3SRandall Stewart 			} else {
397818e198d3SRandall Stewart 				break;
3979f8829a4aSRandall Stewart 			}
39805e54f665SRandall Stewart 		}
398118e198d3SRandall Stewart 
398218e198d3SRandall Stewart 	}
398304ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
3984f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
3985ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3986ceaad40aSRandall Stewart 		struct socket *so;
3987ceaad40aSRandall Stewart 
3988ceaad40aSRandall Stewart #endif
3989f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
3990b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
399104ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
39927215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, 1, SCTP_WAKESND_FROM_SACK);
399380fefe0aSRandall Stewart 		}
3994ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3995ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
3996ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
3997ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3998ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
3999ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4000ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4001ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4002ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
4003ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
4004ceaad40aSRandall Stewart 			return;
4005ceaad40aSRandall Stewart 		}
4006ceaad40aSRandall Stewart #endif
4007f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4008ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4009ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4010ceaad40aSRandall Stewart #endif
4011f8829a4aSRandall Stewart 	} else {
4012b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
40137215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, 1, SCTP_NOWAKE_FROM_SACK);
401480fefe0aSRandall Stewart 		}
4015f8829a4aSRandall Stewart 	}
4016f8829a4aSRandall Stewart 
4017b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
4018ca85e948SMichael Tuexen 	if ((asoc->last_acked_seq != cumack) && (ecne_seen == 0)) {
4019ca85e948SMichael Tuexen 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4020ca85e948SMichael Tuexen 			if (net->net_ack2 > 0) {
4021ca85e948SMichael Tuexen 				/*
4022ca85e948SMichael Tuexen 				 * Karn's rule applies to clearing error
4023ca85e948SMichael Tuexen 				 * count, this is optional.
4024ca85e948SMichael Tuexen 				 */
4025ca85e948SMichael Tuexen 				net->error_count = 0;
4026ca85e948SMichael Tuexen 				if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
4027ca85e948SMichael Tuexen 					/* addr came good */
4028ca85e948SMichael Tuexen 					net->dest_state |= SCTP_ADDR_REACHABLE;
4029ca85e948SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
40304b1f78e1SMichael Tuexen 					    0, (void *)net, SCTP_SO_NOT_LOCKED);
4031ca85e948SMichael Tuexen 				}
4032ca85e948SMichael Tuexen 				if (net == stcb->asoc.primary_destination) {
4033ca85e948SMichael Tuexen 					if (stcb->asoc.alternate) {
4034ca85e948SMichael Tuexen 						/*
4035ca85e948SMichael Tuexen 						 * release the alternate,
4036ca85e948SMichael Tuexen 						 * primary is good
4037ca85e948SMichael Tuexen 						 */
4038ca85e948SMichael Tuexen 						sctp_free_remote_addr(stcb->asoc.alternate);
4039ca85e948SMichael Tuexen 						stcb->asoc.alternate = NULL;
4040ca85e948SMichael Tuexen 					}
4041ca85e948SMichael Tuexen 				}
4042ca85e948SMichael Tuexen 				if (net->dest_state & SCTP_ADDR_PF) {
4043ca85e948SMichael Tuexen 					net->dest_state &= ~SCTP_ADDR_PF;
4044ca85e948SMichael Tuexen 					sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
4045ca85e948SMichael Tuexen 					sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
4046ca85e948SMichael Tuexen 					asoc->cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
4047ca85e948SMichael Tuexen 					/* Done with this net */
4048ca85e948SMichael Tuexen 					net->net_ack = 0;
4049ca85e948SMichael Tuexen 				}
4050ca85e948SMichael Tuexen 				/* restore any doubled timers */
4051ca85e948SMichael Tuexen 				net->RTO = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
4052ca85e948SMichael Tuexen 				if (net->RTO < stcb->asoc.minrto) {
4053ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.minrto;
4054ca85e948SMichael Tuexen 				}
4055ca85e948SMichael Tuexen 				if (net->RTO > stcb->asoc.maxrto) {
4056ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.maxrto;
4057ca85e948SMichael Tuexen 				}
4058ca85e948SMichael Tuexen 			}
4059ca85e948SMichael Tuexen 		}
4060b54d3a6cSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
4061ca85e948SMichael Tuexen 	}
4062f8829a4aSRandall Stewart 	asoc->last_acked_seq = cumack;
40635e54f665SRandall Stewart 
4064f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
4065f8829a4aSRandall Stewart 		/* nothing left in-flight */
4066f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4067f8829a4aSRandall Stewart 			net->flight_size = 0;
4068f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4069f8829a4aSRandall Stewart 		}
4070f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4071f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4072f8829a4aSRandall Stewart 	}
4073f8829a4aSRandall Stewart 	/* RWND update */
4074f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
407544fbe462SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4076f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4077f8829a4aSRandall Stewart 		/* SWS sender side engages */
4078f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
4079f8829a4aSRandall Stewart 	}
40805e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
40815e54f665SRandall Stewart 		win_probe_recovery = 1;
40825e54f665SRandall Stewart 	}
4083f8829a4aSRandall Stewart 	/* Now assure a timer where data is queued at */
4084a5d547adSRandall Stewart again:
4085a5d547adSRandall Stewart 	j = 0;
4086f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
40875171328bSRandall Stewart 		int to_ticks;
40885171328bSRandall Stewart 
40895e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
4090c105859eSRandall Stewart 			win_probe_recovered = 1;
40915e54f665SRandall Stewart 			/*
40925e54f665SRandall Stewart 			 * Find first chunk that was used with window probe
40935e54f665SRandall Stewart 			 * and clear the sent
40945e54f665SRandall Stewart 			 */
40953c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
40965e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
40975e54f665SRandall Stewart 				if (tp1->window_probe) {
4098cd554309SMichael Tuexen 					/* move back to data send queue */
40997215cc1bSMichael Tuexen 					sctp_window_probe_recovery(stcb, asoc, tp1);
41005e54f665SRandall Stewart 					break;
41015e54f665SRandall Stewart 				}
41025e54f665SRandall Stewart 			}
41035e54f665SRandall Stewart 		}
4104f8829a4aSRandall Stewart 		if (net->RTO == 0) {
4105f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
4106f8829a4aSRandall Stewart 		} else {
4107f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
4108f8829a4aSRandall Stewart 		}
41095171328bSRandall Stewart 		if (net->flight_size) {
4110a5d547adSRandall Stewart 			j++;
4111ad81507eSRandall Stewart 			(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
4112f8829a4aSRandall Stewart 			    sctp_timeout_handler, &net->rxt_timer);
41135171328bSRandall Stewart 			if (net->window_probe) {
41145171328bSRandall Stewart 				net->window_probe = 0;
41155171328bSRandall Stewart 			}
4116f8829a4aSRandall Stewart 		} else {
41175171328bSRandall Stewart 			if (net->window_probe) {
41185171328bSRandall Stewart 				/*
41195171328bSRandall Stewart 				 * In window probes we must assure a timer
41205171328bSRandall Stewart 				 * is still running there
41215171328bSRandall Stewart 				 */
41225171328bSRandall Stewart 				net->window_probe = 0;
41235171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
41245171328bSRandall Stewart 					SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
41255171328bSRandall Stewart 					    sctp_timeout_handler, &net->rxt_timer);
41265171328bSRandall Stewart 				}
41275171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4128f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4129a5d547adSRandall Stewart 				    stcb, net,
4130a5d547adSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
4131f8829a4aSRandall Stewart 			}
4132f8829a4aSRandall Stewart 		}
4133f8829a4aSRandall Stewart 	}
4134bff64a4dSRandall Stewart 	if ((j == 0) &&
4135bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
4136bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
4137c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
4138bff64a4dSRandall Stewart 	    (done_once == 0)) {
41390c0982b8SRandall Stewart 		/*
41400c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
41410c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
41420c0982b8SRandall Stewart 		 */
41430c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
4144a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4145a5d547adSRandall Stewart 				net->flight_size = 0;
4146a5d547adSRandall Stewart 			}
4147a5d547adSRandall Stewart 			asoc->total_flight = 0;
4148a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
4149a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4150a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4151a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4152c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
4153c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
4154a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4155791437b5SRandall Stewart 					sctp_ucount_incr(asoc->sent_queue_retran_cnt);
4156a5d547adSRandall Stewart 				}
4157a5d547adSRandall Stewart 			}
41580c0982b8SRandall Stewart 		}
4159bff64a4dSRandall Stewart 		done_once = 1;
4160a5d547adSRandall Stewart 		goto again;
4161a5d547adSRandall Stewart 	}
4162f8829a4aSRandall Stewart 	/**********************************/
4163f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
4164f8829a4aSRandall Stewart 	/**********************************/
4165f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4166f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
4167f8829a4aSRandall Stewart 		/* clean up */
4168f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
4169f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4170f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4171f8829a4aSRandall Stewart 		    (asoc->locked_on_sending)
4172f8829a4aSRandall Stewart 		    ) {
4173f8829a4aSRandall Stewart 			struct sctp_stream_queue_pending *sp;
4174f8829a4aSRandall Stewart 
4175f8829a4aSRandall Stewart 			/*
4176f8829a4aSRandall Stewart 			 * I may be in a state where we got all across.. but
4177f8829a4aSRandall Stewart 			 * cannot write more due to a shutdown... we abort
4178f8829a4aSRandall Stewart 			 * since the user did not indicate EOR in this case.
4179f8829a4aSRandall Stewart 			 * The sp will be cleaned during free of the asoc.
4180f8829a4aSRandall Stewart 			 */
4181f8829a4aSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4182f8829a4aSRandall Stewart 			    sctp_streamhead);
41832afb3e84SRandall Stewart 			if ((sp) && (sp->length == 0)) {
41842afb3e84SRandall Stewart 				/* Let cleanup code purge it */
41852afb3e84SRandall Stewart 				if (sp->msg_is_complete) {
41862afb3e84SRandall Stewart 					asoc->stream_queue_cnt--;
41872afb3e84SRandall Stewart 				} else {
4188f8829a4aSRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4189f8829a4aSRandall Stewart 					asoc->locked_on_sending = NULL;
4190f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
4191f8829a4aSRandall Stewart 				}
4192f8829a4aSRandall Stewart 			}
41932afb3e84SRandall Stewart 		}
4194f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4195f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4196f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4197f8829a4aSRandall Stewart 				/* Need to abort here */
4198f8829a4aSRandall Stewart 				struct mbuf *oper;
4199f8829a4aSRandall Stewart 
4200f8829a4aSRandall Stewart 		abort_out_now:
4201f8829a4aSRandall Stewart 				*abort_now = 1;
4202f8829a4aSRandall Stewart 				/* XXX */
4203f8829a4aSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4204f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
4205f8829a4aSRandall Stewart 				if (oper) {
4206f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
4207f8829a4aSRandall Stewart 					uint32_t *ippp;
4208f8829a4aSRandall Stewart 
4209139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4210f8829a4aSRandall Stewart 					    sizeof(uint32_t);
4211f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
4212f8829a4aSRandall Stewart 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4213139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
4214f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
4215a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_24);
4216f8829a4aSRandall Stewart 				}
4217a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
4218a2b42326SMichael Tuexen 				sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
4219f8829a4aSRandall Stewart 			} else {
4220ca85e948SMichael Tuexen 				struct sctp_nets *netp;
4221ca85e948SMichael Tuexen 
4222f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4223f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4224f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4225f42a358aSRandall Stewart 				}
4226c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4227b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4228f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
4229ca85e948SMichael Tuexen 				if (asoc->alternate) {
4230ca85e948SMichael Tuexen 					netp = asoc->alternate;
4231ca85e948SMichael Tuexen 				} else {
4232ca85e948SMichael Tuexen 					netp = asoc->primary_destination;
4233ca85e948SMichael Tuexen 				}
4234ca85e948SMichael Tuexen 				sctp_send_shutdown(stcb, netp);
4235f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4236ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4237f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4238ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4239f8829a4aSRandall Stewart 			}
4240f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4241f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4242ca85e948SMichael Tuexen 			struct sctp_nets *netp;
4243ca85e948SMichael Tuexen 
4244ca85e948SMichael Tuexen 			if (asoc->alternate) {
4245ca85e948SMichael Tuexen 				netp = asoc->alternate;
4246ca85e948SMichael Tuexen 			} else {
4247ca85e948SMichael Tuexen 				netp = asoc->primary_destination;
4248ca85e948SMichael Tuexen 			}
4249f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4250f8829a4aSRandall Stewart 				goto abort_out_now;
4251f8829a4aSRandall Stewart 			}
4252f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4253c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4254b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4255ca85e948SMichael Tuexen 			sctp_send_shutdown_ack(stcb, netp);
425612af6654SMichael Tuexen 			sctp_stop_timers_for_shutdown(stcb);
4257f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4258ca85e948SMichael Tuexen 			    stcb->sctp_ep, stcb, netp);
4259f8829a4aSRandall Stewart 		}
4260f8829a4aSRandall Stewart 	}
4261dfb11ef8SRandall Stewart 	/*********************************************/
4262dfb11ef8SRandall Stewart 	/* Here we perform PR-SCTP procedures        */
4263dfb11ef8SRandall Stewart 	/* (section 4.2)                             */
4264dfb11ef8SRandall Stewart 	/*********************************************/
4265dfb11ef8SRandall Stewart 	/* C1. update advancedPeerAckPoint */
426620b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cumack, asoc->advanced_peer_ack_point)) {
4267dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cumack;
4268dfb11ef8SRandall Stewart 	}
4269830d754dSRandall Stewart 	/* PR-Sctp issues need to be addressed too */
4270830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
4271830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
4272830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
4273830d754dSRandall Stewart 
4274830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
4275830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
4276830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
427720b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cumack)) {
4278830d754dSRandall Stewart 			/*
4279493d8e5aSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing.
4280830d754dSRandall Stewart 			 */
428120b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) {
4282830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
42830c0982b8SRandall Stewart 			} else if (lchk) {
42840c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
428544fbe462SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt >= 3) {
42860c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
42870c0982b8SRandall Stewart 				}
4288830d754dSRandall Stewart 			}
4289830d754dSRandall Stewart 		}
4290830d754dSRandall Stewart 		if (lchk) {
4291830d754dSRandall Stewart 			/* Assure a timer is up */
4292830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4293830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
4294830d754dSRandall Stewart 		}
4295830d754dSRandall Stewart 	}
4296b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
4297f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4298f8829a4aSRandall Stewart 		    rwnd,
4299f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
4300f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
4301f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
430280fefe0aSRandall Stewart 	}
4303f8829a4aSRandall Stewart }
4304f8829a4aSRandall Stewart 
4305f8829a4aSRandall Stewart void
4306cd554309SMichael Tuexen sctp_handle_sack(struct mbuf *m, int offset_seg, int offset_dup,
43077215cc1bSMichael Tuexen     struct sctp_tcb *stcb,
4308cd554309SMichael Tuexen     uint16_t num_seg, uint16_t num_nr_seg, uint16_t num_dup,
4309cd554309SMichael Tuexen     int *abort_now, uint8_t flags,
4310899288aeSRandall Stewart     uint32_t cum_ack, uint32_t rwnd, int ecne_seen)
4311f8829a4aSRandall Stewart {
4312f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4313f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
4314cd554309SMichael Tuexen 	uint32_t last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked, this_sack_lowest_newack;
4315f8829a4aSRandall Stewart 	uint16_t wake_him = 0;
4316c105859eSRandall Stewart 	uint32_t send_s = 0;
4317f8829a4aSRandall Stewart 	long j;
4318f8829a4aSRandall Stewart 	int accum_moved = 0;
4319f8829a4aSRandall Stewart 	int will_exit_fast_recovery = 0;
43205e54f665SRandall Stewart 	uint32_t a_rwnd, old_rwnd;
43215e54f665SRandall Stewart 	int win_probe_recovery = 0;
4322c105859eSRandall Stewart 	int win_probe_recovered = 0;
4323f8829a4aSRandall Stewart 	struct sctp_nets *net = NULL;
4324bff64a4dSRandall Stewart 	int done_once;
4325f79aab18SRandall Stewart 	int rto_ok = 1;
4326f8829a4aSRandall Stewart 	uint8_t reneged_all = 0;
4327f8829a4aSRandall Stewart 	uint8_t cmt_dac_flag;
4328f8829a4aSRandall Stewart 
4329f8829a4aSRandall Stewart 	/*
4330f8829a4aSRandall Stewart 	 * we take any chance we can to service our queues since we cannot
4331f8829a4aSRandall Stewart 	 * get awoken when the socket is read from :<
4332f8829a4aSRandall Stewart 	 */
4333f8829a4aSRandall Stewart 	/*
4334f8829a4aSRandall Stewart 	 * Now perform the actual SACK handling: 1) Verify that it is not an
4335f8829a4aSRandall Stewart 	 * old sack, if so discard. 2) If there is nothing left in the send
4336f8829a4aSRandall Stewart 	 * queue (cum-ack is equal to last acked) then you have a duplicate
4337f8829a4aSRandall Stewart 	 * too, update any rwnd change and verify no timers are running.
4338f8829a4aSRandall Stewart 	 * then return. 3) Process any new consequtive data i.e. cum-ack
4339f8829a4aSRandall Stewart 	 * moved process these first and note that it moved. 4) Process any
4340f8829a4aSRandall Stewart 	 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
4341f8829a4aSRandall Stewart 	 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
4342f8829a4aSRandall Stewart 	 * sync up flightsizes and things, stop all timers and also check
4343f8829a4aSRandall Stewart 	 * for shutdown_pending state. If so then go ahead and send off the
4344f8829a4aSRandall Stewart 	 * shutdown. If in shutdown recv, send off the shutdown-ack and
4345f8829a4aSRandall Stewart 	 * start that timer, Ret. 9) Strike any non-acked things and do FR
4346f8829a4aSRandall Stewart 	 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
4347f8829a4aSRandall Stewart 	 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
4348f8829a4aSRandall Stewart 	 * if in shutdown_recv state.
4349f8829a4aSRandall Stewart 	 */
4350f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
4351f8829a4aSRandall Stewart 	/* CMT DAC algo */
4352f8829a4aSRandall Stewart 	this_sack_lowest_newack = 0;
4353f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_slowpath_sack);
4354cd554309SMichael Tuexen 	last_tsn = cum_ack;
4355cd554309SMichael Tuexen 	cmt_dac_flag = flags & SCTP_SACK_CMT_DAC;
435618e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
435718e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
435818e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
435918e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
436018e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
436118e198d3SRandall Stewart 	}
436218e198d3SRandall Stewart #endif
4363d06c82f1SRandall Stewart 	a_rwnd = rwnd;
4364f8829a4aSRandall Stewart 
4365cd554309SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4366cd554309SMichael Tuexen 		sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack,
4367cd554309SMichael Tuexen 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
4368cd554309SMichael Tuexen 	}
43695e54f665SRandall Stewart 	old_rwnd = stcb->asoc.peers_rwnd;
4370b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4371c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4372c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
4373c4739e2fSRandall Stewart 		    0,
4374c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
4375c4739e2fSRandall Stewart 		    __LINE__);
4376c4739e2fSRandall Stewart 	}
4377f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
4378f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
4379b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4380f8829a4aSRandall Stewart 		sctp_log_sack(asoc->last_acked_seq,
4381f8829a4aSRandall Stewart 		    cum_ack,
4382f8829a4aSRandall Stewart 		    0,
4383f8829a4aSRandall Stewart 		    num_seg,
4384f8829a4aSRandall Stewart 		    num_dup,
4385f8829a4aSRandall Stewart 		    SCTP_LOG_NEW_SACK);
438680fefe0aSRandall Stewart 	}
4387ca85e948SMichael Tuexen 	if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE)) {
4388cd554309SMichael Tuexen 		uint16_t i;
4389458303daSRandall Stewart 		uint32_t *dupdata, dblock;
4390f8829a4aSRandall Stewart 
4391cd554309SMichael Tuexen 		for (i = 0; i < num_dup; i++) {
4392cd554309SMichael Tuexen 			dupdata = (uint32_t *) sctp_m_getptr(m, offset_dup + i * sizeof(uint32_t),
4393458303daSRandall Stewart 			    sizeof(uint32_t), (uint8_t *) & dblock);
4394cd554309SMichael Tuexen 			if (dupdata == NULL) {
4395458303daSRandall Stewart 				break;
4396458303daSRandall Stewart 			}
4397cd554309SMichael Tuexen 			sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
4398f8829a4aSRandall Stewart 		}
4399f8829a4aSRandall Stewart 	}
4400b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4401c105859eSRandall Stewart 		/* reality check */
4402c105859eSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4403c105859eSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
4404c105859eSRandall Stewart 			    sctpchunk_listhead);
4405c105859eSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
4406c105859eSRandall Stewart 		} else {
4407b5c16493SMichael Tuexen 			tp1 = NULL;
4408c105859eSRandall Stewart 			send_s = asoc->sending_seq;
4409c105859eSRandall Stewart 		}
441020b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(cum_ack, send_s)) {
4411c105859eSRandall Stewart 			struct mbuf *oper;
4412c105859eSRandall Stewart 
4413f8829a4aSRandall Stewart 			/*
4414f8829a4aSRandall Stewart 			 * no way, we have not even sent this TSN out yet.
4415f8829a4aSRandall Stewart 			 * Peer is hopelessly messed up with us.
4416f8829a4aSRandall Stewart 			 */
4417cd3fd531SMichael Tuexen 			SCTP_PRINTF("NEW cum_ack:%x send_s:%x is smaller or equal\n",
4418b5c16493SMichael Tuexen 			    cum_ack, send_s);
4419b5c16493SMichael Tuexen 			if (tp1) {
4420cd3fd531SMichael Tuexen 				SCTP_PRINTF("Got send_s from tsn:%x + 1 of tp1:%p\n",
4421b5c16493SMichael Tuexen 				    tp1->rec.data.TSN_seq, tp1);
4422b5c16493SMichael Tuexen 			}
4423f8829a4aSRandall Stewart 	hopeless_peer:
4424f8829a4aSRandall Stewart 			*abort_now = 1;
4425f8829a4aSRandall Stewart 			/* XXX */
4426f8829a4aSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4427f8829a4aSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
4428f8829a4aSRandall Stewart 			if (oper) {
4429f8829a4aSRandall Stewart 				struct sctp_paramhdr *ph;
4430f8829a4aSRandall Stewart 				uint32_t *ippp;
4431f8829a4aSRandall Stewart 
4432139bc87fSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4433f8829a4aSRandall Stewart 				    sizeof(uint32_t);
4434f8829a4aSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
4435f8829a4aSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4436139bc87fSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
4437f8829a4aSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
4438a5d547adSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4439f8829a4aSRandall Stewart 			}
4440a5d547adSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4441a2b42326SMichael Tuexen 			sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
4442f8829a4aSRandall Stewart 			return;
4443f8829a4aSRandall Stewart 		}
4444f8829a4aSRandall Stewart 	}
4445f8829a4aSRandall Stewart 	/**********************/
4446f8829a4aSRandall Stewart 	/* 1) check the range */
4447f8829a4aSRandall Stewart 	/**********************/
444820b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->last_acked_seq, last_tsn)) {
4449f8829a4aSRandall Stewart 		/* acking something behind */
4450f8829a4aSRandall Stewart 		return;
4451f8829a4aSRandall Stewart 	}
4452f8829a4aSRandall Stewart 	/* update the Rwnd of the peer */
4453f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue) &&
4454f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->send_queue) &&
4455cd554309SMichael Tuexen 	    (asoc->stream_queue_cnt == 0)) {
4456f8829a4aSRandall Stewart 		/* nothing left on send/sent and strmq */
4457b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4458f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4459f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
446080fefe0aSRandall Stewart 		}
4461f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
4462f8829a4aSRandall Stewart 		if (asoc->sent_queue_retran_cnt) {
4463f8829a4aSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4464f8829a4aSRandall Stewart 		}
4465f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4466f8829a4aSRandall Stewart 			/* SWS sender side engages */
4467f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
4468f8829a4aSRandall Stewart 		}
4469f8829a4aSRandall Stewart 		/* stop any timers */
4470f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4471f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4472a5d547adSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4473f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4474f8829a4aSRandall Stewart 			net->flight_size = 0;
4475f8829a4aSRandall Stewart 		}
4476f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4477f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4478f8829a4aSRandall Stewart 		return;
4479f8829a4aSRandall Stewart 	}
4480f8829a4aSRandall Stewart 	/*
4481f8829a4aSRandall Stewart 	 * We init netAckSz and netAckSz2 to 0. These are used to track 2
4482f8829a4aSRandall Stewart 	 * things. The total byte count acked is tracked in netAckSz AND
4483f8829a4aSRandall Stewart 	 * netAck2 is used to track the total bytes acked that are un-
4484f8829a4aSRandall Stewart 	 * amibguious and were never retransmitted. We track these on a per
4485f8829a4aSRandall Stewart 	 * destination address basis.
4486f8829a4aSRandall Stewart 	 */
4487f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4488a21779f0SRandall Stewart 		if (SCTP_TSN_GT(cum_ack, net->cwr_window_tsn)) {
4489a21779f0SRandall Stewart 			/* Drag along the window_tsn for cwr's */
4490a21779f0SRandall Stewart 			net->cwr_window_tsn = cum_ack;
4491a21779f0SRandall Stewart 		}
4492f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
4493f8829a4aSRandall Stewart 		net->net_ack = 0;
4494f8829a4aSRandall Stewart 		net->net_ack2 = 0;
4495f8829a4aSRandall Stewart 
4496f8829a4aSRandall Stewart 		/*
449742551e99SRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
449842551e99SRandall Stewart 		 * SACK processing
4499f8829a4aSRandall Stewart 		 */
4500f8829a4aSRandall Stewart 		net->new_pseudo_cumack = 0;
4501f8829a4aSRandall Stewart 		net->will_exit_fast_recovery = 0;
4502299108c5SRandall Stewart 		if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) {
4503299108c5SRandall Stewart 			(*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) (stcb, net);
4504299108c5SRandall Stewart 		}
4505f8829a4aSRandall Stewart 	}
4506f8829a4aSRandall Stewart 	/* process the new consecutive TSN first */
45074a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
450820b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(last_tsn, tp1->rec.data.TSN_seq)) {
4509f8829a4aSRandall Stewart 			if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4510f8829a4aSRandall Stewart 				accum_moved = 1;
4511f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4512f8829a4aSRandall Stewart 					/*
4513f8829a4aSRandall Stewart 					 * If it is less than ACKED, it is
4514f8829a4aSRandall Stewart 					 * now no-longer in flight. Higher
4515f8829a4aSRandall Stewart 					 * values may occur during marking
4516f8829a4aSRandall Stewart 					 */
4517f8829a4aSRandall Stewart 					if ((tp1->whoTo->dest_state &
4518f8829a4aSRandall Stewart 					    SCTP_ADDR_UNCONFIRMED) &&
4519f8829a4aSRandall Stewart 					    (tp1->snd_count < 2)) {
4520f8829a4aSRandall Stewart 						/*
4521f8829a4aSRandall Stewart 						 * If there was no retran
4522f8829a4aSRandall Stewart 						 * and the address is
4523f8829a4aSRandall Stewart 						 * un-confirmed and we sent
4524f8829a4aSRandall Stewart 						 * there and are now
4525f8829a4aSRandall Stewart 						 * sacked.. its confirmed,
4526f8829a4aSRandall Stewart 						 * mark it so.
4527f8829a4aSRandall Stewart 						 */
4528f8829a4aSRandall Stewart 						tp1->whoTo->dest_state &=
4529f8829a4aSRandall Stewart 						    ~SCTP_ADDR_UNCONFIRMED;
4530f8829a4aSRandall Stewart 					}
4531c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4532b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4533c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4534a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
4535a5d547adSRandall Stewart 							    tp1->book_size,
4536c105859eSRandall Stewart 							    (uintptr_t) tp1->whoTo,
4537a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
453880fefe0aSRandall Stewart 						}
4539c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
4540c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
4541299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
4542299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
4543299108c5SRandall Stewart 							    tp1);
4544299108c5SRandall Stewart 						}
4545f8829a4aSRandall Stewart 					}
4546f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
4547f8829a4aSRandall Stewart 
4548f8829a4aSRandall Stewart 					/* CMT SFR and DAC algos */
4549f8829a4aSRandall Stewart 					this_sack_lowest_newack = tp1->rec.data.TSN_seq;
4550f8829a4aSRandall Stewart 					tp1->whoTo->saw_newack = 1;
4551f8829a4aSRandall Stewart 
4552f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
4553f8829a4aSRandall Stewart 						/*
4554f8829a4aSRandall Stewart 						 * True non-retransmited
4555f8829a4aSRandall Stewart 						 * chunk
4556f8829a4aSRandall Stewart 						 */
4557f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
4558f8829a4aSRandall Stewart 						    tp1->send_size;
4559f8829a4aSRandall Stewart 
4560f8829a4aSRandall Stewart 						/* update RTO too? */
4561f8829a4aSRandall Stewart 						if (tp1->do_rtt) {
4562f79aab18SRandall Stewart 							if (rto_ok) {
4563f8829a4aSRandall Stewart 								tp1->whoTo->RTO =
4564f8829a4aSRandall Stewart 								    sctp_calculate_rto(stcb,
4565f8829a4aSRandall Stewart 								    asoc, tp1->whoTo,
456618e198d3SRandall Stewart 								    &tp1->sent_rcv_time,
4567899288aeSRandall Stewart 								    sctp_align_safe_nocopy,
4568f79aab18SRandall Stewart 								    SCTP_RTT_FROM_DATA);
4569f79aab18SRandall Stewart 								rto_ok = 0;
4570f79aab18SRandall Stewart 							}
4571f79aab18SRandall Stewart 							if (tp1->whoTo->rto_needed == 0) {
4572f79aab18SRandall Stewart 								tp1->whoTo->rto_needed = 1;
4573f79aab18SRandall Stewart 							}
4574f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
4575f8829a4aSRandall Stewart 						}
4576f8829a4aSRandall Stewart 					}
4577f8829a4aSRandall Stewart 					/*
4578f8829a4aSRandall Stewart 					 * CMT: CUCv2 algorithm. From the
4579f8829a4aSRandall Stewart 					 * cumack'd TSNs, for each TSN being
4580f8829a4aSRandall Stewart 					 * acked for the first time, set the
4581f8829a4aSRandall Stewart 					 * following variables for the
4582f8829a4aSRandall Stewart 					 * corresp destination.
4583f8829a4aSRandall Stewart 					 * new_pseudo_cumack will trigger a
4584f8829a4aSRandall Stewart 					 * cwnd update.
4585f8829a4aSRandall Stewart 					 * find_(rtx_)pseudo_cumack will
4586f8829a4aSRandall Stewart 					 * trigger search for the next
4587f8829a4aSRandall Stewart 					 * expected (rtx-)pseudo-cumack.
4588f8829a4aSRandall Stewart 					 */
4589f8829a4aSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
4590f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
4591f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
4592f8829a4aSRandall Stewart 
4593f8829a4aSRandall Stewart 
4594b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4595f8829a4aSRandall Stewart 						sctp_log_sack(asoc->last_acked_seq,
4596f8829a4aSRandall Stewart 						    cum_ack,
4597f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
4598f8829a4aSRandall Stewart 						    0,
4599f8829a4aSRandall Stewart 						    0,
4600f8829a4aSRandall Stewart 						    SCTP_LOG_TSN_ACKED);
460180fefe0aSRandall Stewart 					}
4602b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
4603f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
460480fefe0aSRandall Stewart 					}
4605f8829a4aSRandall Stewart 				}
4606f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4607f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4608f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
4609f8829a4aSRandall Stewart 					sctp_audit_log(0xB3,
4610f8829a4aSRandall Stewart 					    (asoc->sent_queue_retran_cnt & 0x000000ff));
4611f8829a4aSRandall Stewart #endif
4612f8829a4aSRandall Stewart 				}
461342551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
461442551e99SRandall Stewart 					/* deflate the cwnd */
461542551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
461642551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
461742551e99SRandall Stewart 				}
4618f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
4619f8829a4aSRandall Stewart 			}
4620f8829a4aSRandall Stewart 		} else {
4621f8829a4aSRandall Stewart 			break;
4622f8829a4aSRandall Stewart 		}
4623f8829a4aSRandall Stewart 	}
4624f8829a4aSRandall Stewart 	biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
4625f8829a4aSRandall Stewart 	/* always set this up to cum-ack */
4626f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = last_tsn;
4627f8829a4aSRandall Stewart 
4628cd554309SMichael Tuexen 	if ((num_seg > 0) || (num_nr_seg > 0)) {
4629f8829a4aSRandall Stewart 
4630f8829a4aSRandall Stewart 		/*
4631f8829a4aSRandall Stewart 		 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
4632f8829a4aSRandall Stewart 		 * to be greater than the cumack. Also reset saw_newack to 0
4633f8829a4aSRandall Stewart 		 * for all dests.
4634f8829a4aSRandall Stewart 		 */
4635f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4636f8829a4aSRandall Stewart 			net->saw_newack = 0;
4637f8829a4aSRandall Stewart 			net->this_sack_highest_newack = last_tsn;
4638f8829a4aSRandall Stewart 		}
4639f8829a4aSRandall Stewart 
4640f8829a4aSRandall Stewart 		/*
4641f8829a4aSRandall Stewart 		 * thisSackHighestGap will increase while handling NEW
4642f8829a4aSRandall Stewart 		 * segments this_sack_highest_newack will increase while
4643f8829a4aSRandall Stewart 		 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
4644f8829a4aSRandall Stewart 		 * used for CMT DAC algo. saw_newack will also change.
4645f8829a4aSRandall Stewart 		 */
4646cd554309SMichael Tuexen 		if (sctp_handle_segments(m, &offset_seg, stcb, asoc, last_tsn, &biggest_tsn_acked,
4647cd554309SMichael Tuexen 		    &biggest_tsn_newly_acked, &this_sack_lowest_newack,
46487215cc1bSMichael Tuexen 		    num_seg, num_nr_seg, &rto_ok)) {
4649cd554309SMichael Tuexen 			wake_him++;
4650cd554309SMichael Tuexen 		}
4651b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4652f8829a4aSRandall Stewart 			/*
4653f8829a4aSRandall Stewart 			 * validate the biggest_tsn_acked in the gap acks if
4654f8829a4aSRandall Stewart 			 * strict adherence is wanted.
4655f8829a4aSRandall Stewart 			 */
465620b07a4dSMichael Tuexen 			if (SCTP_TSN_GE(biggest_tsn_acked, send_s)) {
4657f8829a4aSRandall Stewart 				/*
4658f8829a4aSRandall Stewart 				 * peer is either confused or we are under
4659f8829a4aSRandall Stewart 				 * attack. We must abort.
4660f8829a4aSRandall Stewart 				 */
4661cd3fd531SMichael Tuexen 				SCTP_PRINTF("Hopeless peer! biggest_tsn_acked:%x largest seq:%x\n",
4662cd3fd531SMichael Tuexen 				    biggest_tsn_acked, send_s);
4663f8829a4aSRandall Stewart 				goto hopeless_peer;
4664f8829a4aSRandall Stewart 			}
4665f8829a4aSRandall Stewart 		}
4666f8829a4aSRandall Stewart 	}
4667f8829a4aSRandall Stewart 	/*******************************************/
4668f8829a4aSRandall Stewart 	/* cancel ALL T3-send timer if accum moved */
4669f8829a4aSRandall Stewart 	/*******************************************/
46707c99d56fSMichael Tuexen 	if (asoc->sctp_cmt_on_off > 0) {
4671f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4672f8829a4aSRandall Stewart 			if (net->new_pseudo_cumack)
4673f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4674a5d547adSRandall Stewart 				    stcb, net,
4675a5d547adSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
4676f8829a4aSRandall Stewart 
4677f8829a4aSRandall Stewart 		}
4678f8829a4aSRandall Stewart 	} else {
4679f8829a4aSRandall Stewart 		if (accum_moved) {
4680f8829a4aSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4681f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4682a5d547adSRandall Stewart 				    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
4683f8829a4aSRandall Stewart 			}
4684f8829a4aSRandall Stewart 		}
4685f8829a4aSRandall Stewart 	}
4686f8829a4aSRandall Stewart 	/********************************************/
4687d9c5cfeaSMichael Tuexen 	/* drop the acked chunks from the sentqueue */
4688f8829a4aSRandall Stewart 	/********************************************/
4689f8829a4aSRandall Stewart 	asoc->last_acked_seq = cum_ack;
4690f8829a4aSRandall Stewart 
46917c99d56fSMichael Tuexen 	TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
469220b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, cum_ack)) {
4693f8829a4aSRandall Stewart 			break;
4694f8829a4aSRandall Stewart 		}
4695f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
4696f8829a4aSRandall Stewart 			/* no more sent on list */
4697cd3fd531SMichael Tuexen 			SCTP_PRINTF("Warning, tp1->sent == %d and its now acked?\n",
469818e198d3SRandall Stewart 			    tp1->sent);
4699f8829a4aSRandall Stewart 		}
4700f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4701f8829a4aSRandall Stewart 		if (tp1->pr_sctp_on) {
4702f8829a4aSRandall Stewart 			if (asoc->pr_sctp_cnt != 0)
4703f8829a4aSRandall Stewart 				asoc->pr_sctp_cnt--;
4704f8829a4aSRandall Stewart 		}
47057c99d56fSMichael Tuexen 		asoc->sent_queue_cnt--;
4706f8829a4aSRandall Stewart 		if (tp1->data) {
470704ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
4708f8829a4aSRandall Stewart 			sctp_free_bufspace(stcb, asoc, tp1, 1);
4709f8829a4aSRandall Stewart 			sctp_m_freem(tp1->data);
47107c99d56fSMichael Tuexen 			tp1->data = NULL;
4711810ec536SMichael Tuexen 			if (asoc->peer_supports_prsctp && PR_SCTP_BUF_ENABLED(tp1->flags)) {
4712f8829a4aSRandall Stewart 				asoc->sent_queue_cnt_removeable--;
4713f8829a4aSRandall Stewart 			}
4714f8829a4aSRandall Stewart 		}
4715b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4716f8829a4aSRandall Stewart 			sctp_log_sack(asoc->last_acked_seq,
4717f8829a4aSRandall Stewart 			    cum_ack,
4718f8829a4aSRandall Stewart 			    tp1->rec.data.TSN_seq,
4719f8829a4aSRandall Stewart 			    0,
4720f8829a4aSRandall Stewart 			    0,
4721f8829a4aSRandall Stewart 			    SCTP_LOG_FREE_SENT);
472280fefe0aSRandall Stewart 		}
4723689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED);
4724f8829a4aSRandall Stewart 		wake_him++;
47257c99d56fSMichael Tuexen 	}
47267c99d56fSMichael Tuexen 	if (TAILQ_EMPTY(&asoc->sent_queue) && (asoc->total_flight > 0)) {
47277c99d56fSMichael Tuexen #ifdef INVARIANTS
47287c99d56fSMichael Tuexen 		panic("Warning flight size is postive and should be 0");
47297c99d56fSMichael Tuexen #else
47307c99d56fSMichael Tuexen 		SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
47317c99d56fSMichael Tuexen 		    asoc->total_flight);
47327c99d56fSMichael Tuexen #endif
47337c99d56fSMichael Tuexen 		asoc->total_flight = 0;
47347c99d56fSMichael Tuexen 	}
473504ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
4736f8829a4aSRandall Stewart 	if ((wake_him) && (stcb->sctp_socket)) {
4737ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4738ceaad40aSRandall Stewart 		struct socket *so;
4739ceaad40aSRandall Stewart 
4740ceaad40aSRandall Stewart #endif
4741f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4742b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
47437215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, wake_him, SCTP_WAKESND_FROM_SACK);
474480fefe0aSRandall Stewart 		}
4745ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4746ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
4747ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
4748ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
4749ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
4750ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4751ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4752ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4753ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
4754ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
4755ceaad40aSRandall Stewart 			return;
4756ceaad40aSRandall Stewart 		}
4757ceaad40aSRandall Stewart #endif
4758f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4759ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4760ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4761ceaad40aSRandall Stewart #endif
4762f8829a4aSRandall Stewart 	} else {
4763b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
47647215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, wake_him, SCTP_NOWAKE_FROM_SACK);
476580fefe0aSRandall Stewart 		}
4766f8829a4aSRandall Stewart 	}
4767f8829a4aSRandall Stewart 
476842551e99SRandall Stewart 	if (asoc->fast_retran_loss_recovery && accum_moved) {
476920b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(asoc->last_acked_seq, asoc->fast_recovery_tsn)) {
4770f8829a4aSRandall Stewart 			/* Setup so we will exit RFC2582 fast recovery */
4771f8829a4aSRandall Stewart 			will_exit_fast_recovery = 1;
4772f8829a4aSRandall Stewart 		}
4773f8829a4aSRandall Stewart 	}
4774f8829a4aSRandall Stewart 	/*
4775f8829a4aSRandall Stewart 	 * Check for revoked fragments:
4776f8829a4aSRandall Stewart 	 *
4777f8829a4aSRandall Stewart 	 * if Previous sack - Had no frags then we can't have any revoked if
4778f8829a4aSRandall Stewart 	 * Previous sack - Had frag's then - If we now have frags aka
4779f8829a4aSRandall Stewart 	 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
4780f8829a4aSRandall Stewart 	 * some of them. else - The peer revoked all ACKED fragments, since
4781f8829a4aSRandall Stewart 	 * we had some before and now we have NONE.
4782f8829a4aSRandall Stewart 	 */
4783f8829a4aSRandall Stewart 
4784d9c5cfeaSMichael Tuexen 	if (num_seg) {
4785c105859eSRandall Stewart 		sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
4786d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_frags = 1;
4787d9c5cfeaSMichael Tuexen 	} else if (asoc->saw_sack_with_frags) {
4788f8829a4aSRandall Stewart 		int cnt_revoked = 0;
4789f8829a4aSRandall Stewart 
4790f8829a4aSRandall Stewart 		/* Peer revoked all dg's marked or acked */
4791f8829a4aSRandall Stewart 		TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4792b5c16493SMichael Tuexen 			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
4793f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_SENT;
4794b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4795c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
4796c105859eSRandall Stewart 					    tp1->whoTo->flight_size,
4797c105859eSRandall Stewart 					    tp1->book_size,
4798c105859eSRandall Stewart 					    (uintptr_t) tp1->whoTo,
4799c105859eSRandall Stewart 					    tp1->rec.data.TSN_seq);
480080fefe0aSRandall Stewart 				}
4801c105859eSRandall Stewart 				sctp_flight_size_increase(tp1);
4802c105859eSRandall Stewart 				sctp_total_flight_increase(stcb, tp1);
4803a5d547adSRandall Stewart 				tp1->rec.data.chunk_was_revoked = 1;
480442551e99SRandall Stewart 				/*
480542551e99SRandall Stewart 				 * To ensure that this increase in
48064a9ef3f8SMichael Tuexen 				 * flightsize, which is artificial, does not
48074a9ef3f8SMichael Tuexen 				 * throttle the sender, we also increase the
48084a9ef3f8SMichael Tuexen 				 * cwnd artificially.
480942551e99SRandall Stewart 				 */
481042551e99SRandall Stewart 				tp1->whoTo->cwnd += tp1->book_size;
4811f8829a4aSRandall Stewart 				cnt_revoked++;
4812f8829a4aSRandall Stewart 			}
4813f8829a4aSRandall Stewart 		}
4814f8829a4aSRandall Stewart 		if (cnt_revoked) {
4815f8829a4aSRandall Stewart 			reneged_all = 1;
4816f8829a4aSRandall Stewart 		}
4817f8829a4aSRandall Stewart 		asoc->saw_sack_with_frags = 0;
4818f8829a4aSRandall Stewart 	}
4819d9c5cfeaSMichael Tuexen 	if (num_nr_seg > 0)
4820d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_nr_frags = 1;
4821f8829a4aSRandall Stewart 	else
4822d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_nr_frags = 0;
4823f8829a4aSRandall Stewart 
4824b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
4825ca85e948SMichael Tuexen 	if (ecne_seen == 0) {
4826ca85e948SMichael Tuexen 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4827ca85e948SMichael Tuexen 			if (net->net_ack2 > 0) {
4828ca85e948SMichael Tuexen 				/*
4829ca85e948SMichael Tuexen 				 * Karn's rule applies to clearing error
4830ca85e948SMichael Tuexen 				 * count, this is optional.
4831ca85e948SMichael Tuexen 				 */
4832ca85e948SMichael Tuexen 				net->error_count = 0;
4833ca85e948SMichael Tuexen 				if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
4834ca85e948SMichael Tuexen 					/* addr came good */
4835ca85e948SMichael Tuexen 					net->dest_state |= SCTP_ADDR_REACHABLE;
4836ca85e948SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
48374b1f78e1SMichael Tuexen 					    0, (void *)net, SCTP_SO_NOT_LOCKED);
4838ca85e948SMichael Tuexen 				}
4839ca85e948SMichael Tuexen 				if (net == stcb->asoc.primary_destination) {
4840ca85e948SMichael Tuexen 					if (stcb->asoc.alternate) {
4841ca85e948SMichael Tuexen 						/*
4842ca85e948SMichael Tuexen 						 * release the alternate,
4843ca85e948SMichael Tuexen 						 * primary is good
4844ca85e948SMichael Tuexen 						 */
4845ca85e948SMichael Tuexen 						sctp_free_remote_addr(stcb->asoc.alternate);
4846ca85e948SMichael Tuexen 						stcb->asoc.alternate = NULL;
4847ca85e948SMichael Tuexen 					}
4848ca85e948SMichael Tuexen 				}
4849ca85e948SMichael Tuexen 				if (net->dest_state & SCTP_ADDR_PF) {
4850ca85e948SMichael Tuexen 					net->dest_state &= ~SCTP_ADDR_PF;
4851ca85e948SMichael Tuexen 					sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
4852ca85e948SMichael Tuexen 					sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
4853ca85e948SMichael Tuexen 					asoc->cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
4854ca85e948SMichael Tuexen 					/* Done with this net */
4855ca85e948SMichael Tuexen 					net->net_ack = 0;
4856ca85e948SMichael Tuexen 				}
4857ca85e948SMichael Tuexen 				/* restore any doubled timers */
4858ca85e948SMichael Tuexen 				net->RTO = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
4859ca85e948SMichael Tuexen 				if (net->RTO < stcb->asoc.minrto) {
4860ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.minrto;
4861ca85e948SMichael Tuexen 				}
4862ca85e948SMichael Tuexen 				if (net->RTO > stcb->asoc.maxrto) {
4863ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.maxrto;
4864ca85e948SMichael Tuexen 				}
4865ca85e948SMichael Tuexen 			}
4866ca85e948SMichael Tuexen 		}
4867b54d3a6cSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
4868ca85e948SMichael Tuexen 	}
4869f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
4870f8829a4aSRandall Stewart 		/* nothing left in-flight */
4871f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4872f8829a4aSRandall Stewart 			/* stop all timers */
4873f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4874a5d547adSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
4875f8829a4aSRandall Stewart 			net->flight_size = 0;
4876f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4877f8829a4aSRandall Stewart 		}
4878f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4879f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4880f8829a4aSRandall Stewart 	}
4881f8829a4aSRandall Stewart 	/**********************************/
4882f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
4883f8829a4aSRandall Stewart 	/**********************************/
4884f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4885f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
4886b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4887f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4888f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
488980fefe0aSRandall Stewart 		}
4890f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
4891f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4892f8829a4aSRandall Stewart 			/* SWS sender side engages */
4893f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
4894f8829a4aSRandall Stewart 		}
4895f8829a4aSRandall Stewart 		/* clean up */
4896f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
4897f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4898f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4899f8829a4aSRandall Stewart 		    (asoc->locked_on_sending)
4900f8829a4aSRandall Stewart 		    ) {
4901f8829a4aSRandall Stewart 			struct sctp_stream_queue_pending *sp;
4902f8829a4aSRandall Stewart 
4903f8829a4aSRandall Stewart 			/*
4904f8829a4aSRandall Stewart 			 * I may be in a state where we got all across.. but
4905f8829a4aSRandall Stewart 			 * cannot write more due to a shutdown... we abort
4906f8829a4aSRandall Stewart 			 * since the user did not indicate EOR in this case.
4907f8829a4aSRandall Stewart 			 */
4908f8829a4aSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4909f8829a4aSRandall Stewart 			    sctp_streamhead);
49102afb3e84SRandall Stewart 			if ((sp) && (sp->length == 0)) {
4911f8829a4aSRandall Stewart 				asoc->locked_on_sending = NULL;
49122afb3e84SRandall Stewart 				if (sp->msg_is_complete) {
4913f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
49142afb3e84SRandall Stewart 				} else {
49152afb3e84SRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
49162afb3e84SRandall Stewart 					asoc->stream_queue_cnt--;
49172afb3e84SRandall Stewart 				}
4918f8829a4aSRandall Stewart 			}
4919f8829a4aSRandall Stewart 		}
4920f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4921f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4922f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4923f8829a4aSRandall Stewart 				/* Need to abort here */
4924f8829a4aSRandall Stewart 				struct mbuf *oper;
4925f8829a4aSRandall Stewart 
4926f8829a4aSRandall Stewart 		abort_out_now:
4927f8829a4aSRandall Stewart 				*abort_now = 1;
4928f8829a4aSRandall Stewart 				/* XXX */
4929f8829a4aSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4930f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
4931f8829a4aSRandall Stewart 				if (oper) {
4932f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
4933f8829a4aSRandall Stewart 					uint32_t *ippp;
4934f8829a4aSRandall Stewart 
4935139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4936f8829a4aSRandall Stewart 					    sizeof(uint32_t);
4937f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
4938f8829a4aSRandall Stewart 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4939139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
4940f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
4941a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_31);
4942f8829a4aSRandall Stewart 				}
4943a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
4944a2b42326SMichael Tuexen 				sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
4945f8829a4aSRandall Stewart 				return;
4946f8829a4aSRandall Stewart 			} else {
4947ca85e948SMichael Tuexen 				struct sctp_nets *netp;
4948ca85e948SMichael Tuexen 
4949ca85e948SMichael Tuexen 				if (asoc->alternate) {
4950ca85e948SMichael Tuexen 					netp = asoc->alternate;
4951ca85e948SMichael Tuexen 				} else {
4952ca85e948SMichael Tuexen 					netp = asoc->primary_destination;
4953ca85e948SMichael Tuexen 				}
4954f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4955f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4956f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4957f42a358aSRandall Stewart 				}
4958c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4959b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4960f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
4961ca85e948SMichael Tuexen 				sctp_send_shutdown(stcb, netp);
4962f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4963ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4964f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4965ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4966f8829a4aSRandall Stewart 			}
4967f8829a4aSRandall Stewart 			return;
4968f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4969f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
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 			}
4977f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4978f8829a4aSRandall Stewart 				goto abort_out_now;
4979f8829a4aSRandall Stewart 			}
4980f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4981c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4982b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4983ca85e948SMichael Tuexen 			sctp_send_shutdown_ack(stcb, netp);
498412af6654SMichael Tuexen 			sctp_stop_timers_for_shutdown(stcb);
4985f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4986ca85e948SMichael Tuexen 			    stcb->sctp_ep, stcb, netp);
4987f8829a4aSRandall Stewart 			return;
4988f8829a4aSRandall Stewart 		}
4989f8829a4aSRandall Stewart 	}
4990f8829a4aSRandall Stewart 	/*
4991f8829a4aSRandall Stewart 	 * Now here we are going to recycle net_ack for a different use...
4992f8829a4aSRandall Stewart 	 * HEADS UP.
4993f8829a4aSRandall Stewart 	 */
4994f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4995f8829a4aSRandall Stewart 		net->net_ack = 0;
4996f8829a4aSRandall Stewart 	}
4997f8829a4aSRandall Stewart 
4998f8829a4aSRandall Stewart 	/*
4999f8829a4aSRandall Stewart 	 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
5000f8829a4aSRandall Stewart 	 * to be done. Setting this_sack_lowest_newack to the cum_ack will
5001f8829a4aSRandall Stewart 	 * automatically ensure that.
5002f8829a4aSRandall Stewart 	 */
50037c99d56fSMichael Tuexen 	if ((asoc->sctp_cmt_on_off > 0) &&
500420083c2eSMichael Tuexen 	    SCTP_BASE_SYSCTL(sctp_cmt_use_dac) &&
500520083c2eSMichael Tuexen 	    (cmt_dac_flag == 0)) {
5006f8829a4aSRandall Stewart 		this_sack_lowest_newack = cum_ack;
5007f8829a4aSRandall Stewart 	}
5008cd554309SMichael Tuexen 	if ((num_seg > 0) || (num_nr_seg > 0)) {
5009f8829a4aSRandall Stewart 		sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
5010f8829a4aSRandall Stewart 		    biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
5011f8829a4aSRandall Stewart 	}
5012b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
5013b54d3a6cSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
5014f8829a4aSRandall Stewart 
5015f8829a4aSRandall Stewart 	/* Now are we exiting loss recovery ? */
5016f8829a4aSRandall Stewart 	if (will_exit_fast_recovery) {
5017f8829a4aSRandall Stewart 		/* Ok, we must exit fast recovery */
5018f8829a4aSRandall Stewart 		asoc->fast_retran_loss_recovery = 0;
5019f8829a4aSRandall Stewart 	}
5020f8829a4aSRandall Stewart 	if ((asoc->sat_t3_loss_recovery) &&
502120b07a4dSMichael Tuexen 	    SCTP_TSN_GE(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn)) {
5022f8829a4aSRandall Stewart 		/* end satellite t3 loss recovery */
5023f8829a4aSRandall Stewart 		asoc->sat_t3_loss_recovery = 0;
5024f8829a4aSRandall Stewart 	}
502542551e99SRandall Stewart 	/*
502642551e99SRandall Stewart 	 * CMT Fast recovery
502742551e99SRandall Stewart 	 */
5028f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5029f8829a4aSRandall Stewart 		if (net->will_exit_fast_recovery) {
5030f8829a4aSRandall Stewart 			/* Ok, we must exit fast recovery */
5031f8829a4aSRandall Stewart 			net->fast_retran_loss_recovery = 0;
5032f8829a4aSRandall Stewart 		}
5033f8829a4aSRandall Stewart 	}
5034f8829a4aSRandall Stewart 
5035f8829a4aSRandall Stewart 	/* Adjust and set the new rwnd value */
5036b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
5037f8829a4aSRandall Stewart 		sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
503844fbe462SRandall Stewart 		    asoc->peers_rwnd, asoc->total_flight, (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd);
503980fefe0aSRandall Stewart 	}
5040f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
504144fbe462SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
5042f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5043f8829a4aSRandall Stewart 		/* SWS sender side engages */
5044f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
5045f8829a4aSRandall Stewart 	}
50465e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
50475e54f665SRandall Stewart 		win_probe_recovery = 1;
50485e54f665SRandall Stewart 	}
5049f8829a4aSRandall Stewart 	/*
5050f8829a4aSRandall Stewart 	 * Now we must setup so we have a timer up for anyone with
5051f8829a4aSRandall Stewart 	 * outstanding data.
5052f8829a4aSRandall Stewart 	 */
5053bff64a4dSRandall Stewart 	done_once = 0;
5054a5d547adSRandall Stewart again:
5055a5d547adSRandall Stewart 	j = 0;
5056f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
50575e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
5058c105859eSRandall Stewart 			win_probe_recovered = 1;
50595e54f665SRandall Stewart 			/*-
50605e54f665SRandall Stewart 			 * Find first chunk that was used with
50615e54f665SRandall Stewart 			 * window probe and clear the event. Put
50625e54f665SRandall Stewart 			 * it back into the send queue as if has
50635e54f665SRandall Stewart 			 * not been sent.
50645e54f665SRandall Stewart 			 */
50655e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
50665e54f665SRandall Stewart 				if (tp1->window_probe) {
50677215cc1bSMichael Tuexen 					sctp_window_probe_recovery(stcb, asoc, tp1);
50685e54f665SRandall Stewart 					break;
50695e54f665SRandall Stewart 				}
50705e54f665SRandall Stewart 			}
50715e54f665SRandall Stewart 		}
5072f8829a4aSRandall Stewart 		if (net->flight_size) {
5073a5d547adSRandall Stewart 			j++;
5074cd554309SMichael Tuexen 			if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5075f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5076f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, net);
5077cd554309SMichael Tuexen 			}
50785171328bSRandall Stewart 			if (net->window_probe) {
5079cd554309SMichael Tuexen 				net->window_probe = 0;
50805171328bSRandall Stewart 			}
5081c105859eSRandall Stewart 		} else {
50825171328bSRandall Stewart 			if (net->window_probe) {
50835171328bSRandall Stewart 				/*
50845171328bSRandall Stewart 				 * In window probes we must assure a timer
50855171328bSRandall Stewart 				 * is still running there
50865171328bSRandall Stewart 				 */
50875171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
50885171328bSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_SEND,
50895171328bSRandall Stewart 					    stcb->sctp_ep, stcb, net);
50905171328bSRandall Stewart 
50915171328bSRandall Stewart 				}
50925171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5093c105859eSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5094c105859eSRandall Stewart 				    stcb, net,
5095c105859eSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
5096c105859eSRandall Stewart 			}
5097f8829a4aSRandall Stewart 		}
5098f8829a4aSRandall Stewart 	}
5099bff64a4dSRandall Stewart 	if ((j == 0) &&
5100bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
5101bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
5102c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
5103bff64a4dSRandall Stewart 	    (done_once == 0)) {
51040c0982b8SRandall Stewart 		/*
51050c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
51060c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
51070c0982b8SRandall Stewart 		 */
51080c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
5109a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5110a5d547adSRandall Stewart 				net->flight_size = 0;
5111a5d547adSRandall Stewart 			}
5112a5d547adSRandall Stewart 			asoc->total_flight = 0;
5113a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
5114a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
5115a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5116a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5117c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
5118c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
5119a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5120791437b5SRandall Stewart 					sctp_ucount_incr(asoc->sent_queue_retran_cnt);
5121a5d547adSRandall Stewart 				}
5122a5d547adSRandall Stewart 			}
51230c0982b8SRandall Stewart 		}
5124bff64a4dSRandall Stewart 		done_once = 1;
5125a5d547adSRandall Stewart 		goto again;
5126a5d547adSRandall Stewart 	}
5127cd554309SMichael Tuexen 	/*********************************************/
5128cd554309SMichael Tuexen 	/* Here we perform PR-SCTP procedures        */
5129cd554309SMichael Tuexen 	/* (section 4.2)                             */
5130cd554309SMichael Tuexen 	/*********************************************/
5131cd554309SMichael Tuexen 	/* C1. update advancedPeerAckPoint */
513220b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cum_ack, asoc->advanced_peer_ack_point)) {
5133dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cum_ack;
5134dfb11ef8SRandall Stewart 	}
5135830d754dSRandall Stewart 	/* C2. try to further move advancedPeerAckPoint ahead */
5136830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
5137830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
5138830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
5139830d754dSRandall Stewart 
5140830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
5141830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
5142830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
514320b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cum_ack)) {
5144830d754dSRandall Stewart 			/*
5145493d8e5aSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing.
5146830d754dSRandall Stewart 			 */
51470c0982b8SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
51480c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
51490c0982b8SRandall Stewart 				    0xee, cum_ack, asoc->advanced_peer_ack_point,
51500c0982b8SRandall Stewart 				    old_adv_peer_ack_point);
51510c0982b8SRandall Stewart 			}
515220b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) {
5153830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
51540c0982b8SRandall Stewart 			} else if (lchk) {
51550c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
515644fbe462SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt >= 3) {
51570c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
51580c0982b8SRandall Stewart 				}
5159830d754dSRandall Stewart 			}
5160830d754dSRandall Stewart 		}
5161830d754dSRandall Stewart 		if (lchk) {
5162830d754dSRandall Stewart 			/* Assure a timer is up */
5163830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5164830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
5165830d754dSRandall Stewart 		}
5166830d754dSRandall Stewart 	}
5167b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
5168f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
5169f8829a4aSRandall Stewart 		    a_rwnd,
5170f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
5171f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
5172f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
517380fefe0aSRandall Stewart 	}
5174f8829a4aSRandall Stewart }
5175f8829a4aSRandall Stewart 
5176f8829a4aSRandall Stewart void
51777215cc1bSMichael Tuexen sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp, int *abort_flag)
5178f8829a4aSRandall Stewart {
5179f8829a4aSRandall Stewart 	/* Copy cum-ack */
5180f8829a4aSRandall Stewart 	uint32_t cum_ack, a_rwnd;
5181f8829a4aSRandall Stewart 
5182f8829a4aSRandall Stewart 	cum_ack = ntohl(cp->cumulative_tsn_ack);
5183f8829a4aSRandall Stewart 	/* Arrange so a_rwnd does NOT change */
5184f8829a4aSRandall Stewart 	a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight;
5185f8829a4aSRandall Stewart 
5186f8829a4aSRandall Stewart 	/* Now call the express sack handling */
5187899288aeSRandall Stewart 	sctp_express_handle_sack(stcb, cum_ack, a_rwnd, abort_flag, 0);
5188f8829a4aSRandall Stewart }
5189f8829a4aSRandall Stewart 
5190f8829a4aSRandall Stewart static void
5191f8829a4aSRandall Stewart sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
5192f8829a4aSRandall Stewart     struct sctp_stream_in *strmin)
5193f8829a4aSRandall Stewart {
5194f8829a4aSRandall Stewart 	struct sctp_queued_to_read *ctl, *nctl;
5195f8829a4aSRandall Stewart 	struct sctp_association *asoc;
519683128708SRandall Stewart 	uint16_t tt;
5197f8829a4aSRandall Stewart 
5198f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
5199f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered;
5200f8829a4aSRandall Stewart 	/*
5201f8829a4aSRandall Stewart 	 * First deliver anything prior to and including the stream no that
5202f8829a4aSRandall Stewart 	 * came in
5203f8829a4aSRandall Stewart 	 */
52044a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(ctl, &strmin->inqueue, next, nctl) {
520520b07a4dSMichael Tuexen 		if (SCTP_SSN_GE(tt, ctl->sinfo_ssn)) {
5206f8829a4aSRandall Stewart 			/* this is deliverable now */
5207f8829a4aSRandall Stewart 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5208f8829a4aSRandall Stewart 			/* subtract pending on streams */
5209f8829a4aSRandall Stewart 			asoc->size_on_all_streams -= ctl->length;
5210f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_all_streams);
5211f8829a4aSRandall Stewart 			/* deliver it to at least the delivery-q */
5212f8829a4aSRandall Stewart 			if (stcb->sctp_socket) {
5213b5c16493SMichael Tuexen 				sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
5214f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
5215f8829a4aSRandall Stewart 				    ctl,
5216cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
5217f8829a4aSRandall Stewart 			}
5218f8829a4aSRandall Stewart 		} else {
5219f8829a4aSRandall Stewart 			/* no more delivery now. */
5220f8829a4aSRandall Stewart 			break;
5221f8829a4aSRandall Stewart 		}
5222f8829a4aSRandall Stewart 	}
5223f8829a4aSRandall Stewart 	/*
5224f8829a4aSRandall Stewart 	 * now we must deliver things in queue the normal way  if any are
5225f8829a4aSRandall Stewart 	 * now ready.
5226f8829a4aSRandall Stewart 	 */
5227f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered + 1;
52284a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(ctl, &strmin->inqueue, next, nctl) {
5229f8829a4aSRandall Stewart 		if (tt == ctl->sinfo_ssn) {
5230f8829a4aSRandall Stewart 			/* this is deliverable now */
5231f8829a4aSRandall Stewart 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5232f8829a4aSRandall Stewart 			/* subtract pending on streams */
5233f8829a4aSRandall Stewart 			asoc->size_on_all_streams -= ctl->length;
5234f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_all_streams);
5235f8829a4aSRandall Stewart 			/* deliver it to at least the delivery-q */
5236f8829a4aSRandall Stewart 			strmin->last_sequence_delivered = ctl->sinfo_ssn;
5237f8829a4aSRandall Stewart 			if (stcb->sctp_socket) {
5238b5c16493SMichael Tuexen 				sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
5239f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
5240f8829a4aSRandall Stewart 				    ctl,
5241cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
524277acdc25SRandall Stewart 
5243f8829a4aSRandall Stewart 			}
5244f8829a4aSRandall Stewart 			tt = strmin->last_sequence_delivered + 1;
5245f8829a4aSRandall Stewart 		} else {
5246f8829a4aSRandall Stewart 			break;
5247f8829a4aSRandall Stewart 		}
5248f8829a4aSRandall Stewart 	}
5249f8829a4aSRandall Stewart }
5250f8829a4aSRandall Stewart 
52518933fa13SRandall Stewart static void
52528933fa13SRandall Stewart sctp_flush_reassm_for_str_seq(struct sctp_tcb *stcb,
52538933fa13SRandall Stewart     struct sctp_association *asoc,
52548933fa13SRandall Stewart     uint16_t stream, uint16_t seq)
52558933fa13SRandall Stewart {
52564a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
52578933fa13SRandall Stewart 
52588933fa13SRandall Stewart 	/* For each one on here see if we need to toss it */
52598933fa13SRandall Stewart 	/*
52604a9ef3f8SMichael Tuexen 	 * For now large messages held on the reasmqueue that are complete
52614a9ef3f8SMichael Tuexen 	 * will be tossed too. We could in theory do more work to spin
52624a9ef3f8SMichael Tuexen 	 * through and stop after dumping one msg aka seeing the start of a
52634a9ef3f8SMichael Tuexen 	 * new msg at the head, and call the delivery function... to see if
52644a9ef3f8SMichael Tuexen 	 * it can be delivered... But for now we just dump everything on the
52654a9ef3f8SMichael Tuexen 	 * queue.
52668933fa13SRandall Stewart 	 */
52674a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
52688e71b694SMichael Tuexen 		/*
52694a9ef3f8SMichael Tuexen 		 * Do not toss it if on a different stream or marked for
52704a9ef3f8SMichael Tuexen 		 * unordered delivery in which case the stream sequence
52714a9ef3f8SMichael Tuexen 		 * number has no meaning.
52728e71b694SMichael Tuexen 		 */
52738e71b694SMichael Tuexen 		if ((chk->rec.data.stream_number != stream) ||
52748e71b694SMichael Tuexen 		    ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == SCTP_DATA_UNORDERED)) {
52758933fa13SRandall Stewart 			continue;
52768933fa13SRandall Stewart 		}
52778933fa13SRandall Stewart 		if (chk->rec.data.stream_seq == seq) {
52788933fa13SRandall Stewart 			/* It needs to be tossed */
52798933fa13SRandall Stewart 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
528020b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(chk->rec.data.TSN_seq, asoc->tsn_last_delivered)) {
52814a9ef3f8SMichael Tuexen 				asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
52824a9ef3f8SMichael Tuexen 				asoc->str_of_pdapi = chk->rec.data.stream_number;
52834a9ef3f8SMichael Tuexen 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
52844a9ef3f8SMichael Tuexen 				asoc->fragment_flags = chk->rec.data.rcv_flags;
52858933fa13SRandall Stewart 			}
52868933fa13SRandall Stewart 			asoc->size_on_reasm_queue -= chk->send_size;
52878933fa13SRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_reasm_queue);
52888933fa13SRandall Stewart 
52898933fa13SRandall Stewart 			/* Clear up any stream problem */
529020b07a4dSMichael Tuexen 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) != SCTP_DATA_UNORDERED &&
529120b07a4dSMichael Tuexen 			    SCTP_SSN_GT(chk->rec.data.stream_seq, asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered)) {
52928933fa13SRandall Stewart 				/*
52938933fa13SRandall Stewart 				 * We must dump forward this streams
52944a9ef3f8SMichael Tuexen 				 * sequence number if the chunk is not
52954a9ef3f8SMichael Tuexen 				 * unordered that is being skipped. There is
52964a9ef3f8SMichael Tuexen 				 * a chance that if the peer does not
52974a9ef3f8SMichael Tuexen 				 * include the last fragment in its FWD-TSN
52984a9ef3f8SMichael Tuexen 				 * we WILL have a problem here since you
52994a9ef3f8SMichael Tuexen 				 * would have a partial chunk in queue that
53004a9ef3f8SMichael Tuexen 				 * may not be deliverable. Also if a Partial
53014a9ef3f8SMichael Tuexen 				 * delivery API as started the user may get
53024a9ef3f8SMichael Tuexen 				 * a partial chunk. The next read returning
53034a9ef3f8SMichael Tuexen 				 * a new chunk... really ugly but I see no
53044a9ef3f8SMichael Tuexen 				 * way around it! Maybe a notify??
53058933fa13SRandall Stewart 				 */
53064a9ef3f8SMichael Tuexen 				asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered = chk->rec.data.stream_seq;
53078933fa13SRandall Stewart 			}
53088933fa13SRandall Stewart 			if (chk->data) {
53098933fa13SRandall Stewart 				sctp_m_freem(chk->data);
53108933fa13SRandall Stewart 				chk->data = NULL;
53118933fa13SRandall Stewart 			}
5312689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
531320b07a4dSMichael Tuexen 		} else if (SCTP_SSN_GT(chk->rec.data.stream_seq, seq)) {
53148933fa13SRandall Stewart 			/*
53154a9ef3f8SMichael Tuexen 			 * If the stream_seq is > than the purging one, we
53164a9ef3f8SMichael Tuexen 			 * are done
53178933fa13SRandall Stewart 			 */
53188933fa13SRandall Stewart 			break;
53198933fa13SRandall Stewart 		}
53208933fa13SRandall Stewart 	}
53218933fa13SRandall Stewart }
53228933fa13SRandall Stewart 
53238933fa13SRandall Stewart 
5324f8829a4aSRandall Stewart void
5325f8829a4aSRandall Stewart sctp_handle_forward_tsn(struct sctp_tcb *stcb,
5326b5c16493SMichael Tuexen     struct sctp_forward_tsn_chunk *fwd,
5327b5c16493SMichael Tuexen     int *abort_flag, struct mbuf *m, int offset)
5328f8829a4aSRandall Stewart {
5329f8829a4aSRandall Stewart 	/* The pr-sctp fwd tsn */
5330f8829a4aSRandall Stewart 	/*
5331f8829a4aSRandall Stewart 	 * here we will perform all the data receiver side steps for
5332f8829a4aSRandall Stewart 	 * processing FwdTSN, as required in by pr-sctp draft:
5333f8829a4aSRandall Stewart 	 *
5334f8829a4aSRandall Stewart 	 * Assume we get FwdTSN(x):
5335f8829a4aSRandall Stewart 	 *
5336f8829a4aSRandall Stewart 	 * 1) update local cumTSN to x 2) try to further advance cumTSN to x +
5337f8829a4aSRandall Stewart 	 * others we have 3) examine and update re-ordering queue on
5338f8829a4aSRandall Stewart 	 * pr-in-streams 4) clean up re-assembly queue 5) Send a sack to
5339f8829a4aSRandall Stewart 	 * report where we are.
5340f8829a4aSRandall Stewart 	 */
5341f8829a4aSRandall Stewart 	struct sctp_association *asoc;
53427898f408SRandall Stewart 	uint32_t new_cum_tsn, gap;
53437215cc1bSMichael Tuexen 	unsigned int i, fwd_sz, m_size;
53448933fa13SRandall Stewart 	uint32_t str_seq;
5345f8829a4aSRandall Stewart 	struct sctp_stream_in *strm;
53464a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
53478933fa13SRandall Stewart 	struct sctp_queued_to_read *ctl, *sv;
5348f8829a4aSRandall Stewart 
5349f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
5350f8829a4aSRandall Stewart 	if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
5351ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1,
5352ad81507eSRandall Stewart 		    "Bad size too small/big fwd-tsn\n");
5353f8829a4aSRandall Stewart 		return;
5354f8829a4aSRandall Stewart 	}
5355f8829a4aSRandall Stewart 	m_size = (stcb->asoc.mapping_array_size << 3);
5356f8829a4aSRandall Stewart 	/*************************************************************/
5357f8829a4aSRandall Stewart 	/* 1. Here we update local cumTSN and shift the bitmap array */
5358f8829a4aSRandall Stewart 	/*************************************************************/
5359f8829a4aSRandall Stewart 	new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
5360f8829a4aSRandall Stewart 
536120b07a4dSMichael Tuexen 	if (SCTP_TSN_GE(asoc->cumulative_tsn, new_cum_tsn)) {
5362f8829a4aSRandall Stewart 		/* Already got there ... */
5363f8829a4aSRandall Stewart 		return;
5364f8829a4aSRandall Stewart 	}
5365f8829a4aSRandall Stewart 	/*
5366f8829a4aSRandall Stewart 	 * now we know the new TSN is more advanced, let's find the actual
5367f8829a4aSRandall Stewart 	 * gap
5368f8829a4aSRandall Stewart 	 */
5369b5c16493SMichael Tuexen 	SCTP_CALC_TSN_TO_GAP(gap, new_cum_tsn, asoc->mapping_array_base_tsn);
537077acdc25SRandall Stewart 	asoc->cumulative_tsn = new_cum_tsn;
53712afb3e84SRandall Stewart 	if (gap >= m_size) {
5372f8829a4aSRandall Stewart 		if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) {
537317205eccSRandall Stewart 			struct mbuf *oper;
537417205eccSRandall Stewart 
5375f8829a4aSRandall Stewart 			/*
5376f8829a4aSRandall Stewart 			 * out of range (of single byte chunks in the rwnd I
537717205eccSRandall Stewart 			 * give out). This must be an attacker.
5378f8829a4aSRandall Stewart 			 */
537917205eccSRandall Stewart 			*abort_flag = 1;
538017205eccSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
538117205eccSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
538217205eccSRandall Stewart 			if (oper) {
538317205eccSRandall Stewart 				struct sctp_paramhdr *ph;
538417205eccSRandall Stewart 				uint32_t *ippp;
538517205eccSRandall Stewart 
538617205eccSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
538717205eccSRandall Stewart 				    (sizeof(uint32_t) * 3);
538817205eccSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
538917205eccSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
539017205eccSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
539117205eccSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
539217205eccSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_33);
539317205eccSRandall Stewart 				ippp++;
539417205eccSRandall Stewart 				*ippp = asoc->highest_tsn_inside_map;
539517205eccSRandall Stewart 				ippp++;
539617205eccSRandall Stewart 				*ippp = new_cum_tsn;
539717205eccSRandall Stewart 			}
539817205eccSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_33;
5399a2b42326SMichael Tuexen 			sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
5400f8829a4aSRandall Stewart 			return;
5401f8829a4aSRandall Stewart 		}
5402207304d4SRandall Stewart 		SCTP_STAT_INCR(sctps_fwdtsn_map_over);
540377acdc25SRandall Stewart 
54042afb3e84SRandall Stewart 		memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
54052afb3e84SRandall Stewart 		asoc->mapping_array_base_tsn = new_cum_tsn + 1;
540677acdc25SRandall Stewart 		asoc->highest_tsn_inside_map = new_cum_tsn;
540777acdc25SRandall Stewart 
5408b5c16493SMichael Tuexen 		memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
5409830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = new_cum_tsn;
541077acdc25SRandall Stewart 
5411b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
5412f8829a4aSRandall Stewart 			sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
541380fefe0aSRandall Stewart 		}
54142afb3e84SRandall Stewart 	} else {
54152afb3e84SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
54162afb3e84SRandall Stewart 		for (i = 0; i <= gap; i++) {
54177898f408SRandall Stewart 			if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) &&
54187898f408SRandall Stewart 			    !SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, i)) {
54198933fa13SRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, i);
542020b07a4dSMichael Tuexen 				if (SCTP_TSN_GT(asoc->mapping_array_base_tsn + i, asoc->highest_tsn_inside_nr_map)) {
54217898f408SRandall Stewart 					asoc->highest_tsn_inside_nr_map = asoc->mapping_array_base_tsn + i;
5422b5c16493SMichael Tuexen 				}
5423b5c16493SMichael Tuexen 			}
5424b5c16493SMichael Tuexen 		}
5425f8829a4aSRandall Stewart 	}
5426f8829a4aSRandall Stewart 	/*************************************************************/
5427f8829a4aSRandall Stewart 	/* 2. Clear up re-assembly queue                             */
5428f8829a4aSRandall Stewart 	/*************************************************************/
5429f8829a4aSRandall Stewart 	/*
5430f8829a4aSRandall Stewart 	 * First service it if pd-api is up, just in case we can progress it
5431f8829a4aSRandall Stewart 	 * forward
5432f8829a4aSRandall Stewart 	 */
5433f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
5434f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
5435f8829a4aSRandall Stewart 	}
5436f8829a4aSRandall Stewart 	/* For each one on here see if we need to toss it */
5437f8829a4aSRandall Stewart 	/*
54384a9ef3f8SMichael Tuexen 	 * For now large messages held on the reasmqueue that are complete
54394a9ef3f8SMichael Tuexen 	 * will be tossed too. We could in theory do more work to spin
54404a9ef3f8SMichael Tuexen 	 * through and stop after dumping one msg aka seeing the start of a
54414a9ef3f8SMichael Tuexen 	 * new msg at the head, and call the delivery function... to see if
54424a9ef3f8SMichael Tuexen 	 * it can be delivered... But for now we just dump everything on the
54434a9ef3f8SMichael Tuexen 	 * queue.
5444f8829a4aSRandall Stewart 	 */
54454a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
544620b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(new_cum_tsn, chk->rec.data.TSN_seq)) {
5447f8829a4aSRandall Stewart 			/* It needs to be tossed */
5448f8829a4aSRandall Stewart 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
544920b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(chk->rec.data.TSN_seq, asoc->tsn_last_delivered)) {
54504a9ef3f8SMichael Tuexen 				asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
54514a9ef3f8SMichael Tuexen 				asoc->str_of_pdapi = chk->rec.data.stream_number;
54524a9ef3f8SMichael Tuexen 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
54534a9ef3f8SMichael Tuexen 				asoc->fragment_flags = chk->rec.data.rcv_flags;
5454f8829a4aSRandall Stewart 			}
5455f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue -= chk->send_size;
5456f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5457f8829a4aSRandall Stewart 
5458f8829a4aSRandall Stewart 			/* Clear up any stream problem */
545920b07a4dSMichael Tuexen 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) != SCTP_DATA_UNORDERED &&
546020b07a4dSMichael Tuexen 			    SCTP_SSN_GT(chk->rec.data.stream_seq, asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered)) {
5461f8829a4aSRandall Stewart 				/*
5462f8829a4aSRandall Stewart 				 * We must dump forward this streams
54634a9ef3f8SMichael Tuexen 				 * sequence number if the chunk is not
54644a9ef3f8SMichael Tuexen 				 * unordered that is being skipped. There is
54654a9ef3f8SMichael Tuexen 				 * a chance that if the peer does not
54664a9ef3f8SMichael Tuexen 				 * include the last fragment in its FWD-TSN
54674a9ef3f8SMichael Tuexen 				 * we WILL have a problem here since you
54684a9ef3f8SMichael Tuexen 				 * would have a partial chunk in queue that
54694a9ef3f8SMichael Tuexen 				 * may not be deliverable. Also if a Partial
54704a9ef3f8SMichael Tuexen 				 * delivery API as started the user may get
54714a9ef3f8SMichael Tuexen 				 * a partial chunk. The next read returning
54724a9ef3f8SMichael Tuexen 				 * a new chunk... really ugly but I see no
54734a9ef3f8SMichael Tuexen 				 * way around it! Maybe a notify??
5474f8829a4aSRandall Stewart 				 */
54754a9ef3f8SMichael Tuexen 				asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered = chk->rec.data.stream_seq;
5476f8829a4aSRandall Stewart 			}
5477f8829a4aSRandall Stewart 			if (chk->data) {
5478f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
5479f8829a4aSRandall Stewart 				chk->data = NULL;
5480f8829a4aSRandall Stewart 			}
5481689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
5482f8829a4aSRandall Stewart 		} else {
5483f8829a4aSRandall Stewart 			/*
54844a9ef3f8SMichael Tuexen 			 * Ok we have gone beyond the end of the fwd-tsn's
54854a9ef3f8SMichael Tuexen 			 * mark.
5486f8829a4aSRandall Stewart 			 */
5487f8829a4aSRandall Stewart 			break;
5488f8829a4aSRandall Stewart 		}
5489f8829a4aSRandall Stewart 	}
54908933fa13SRandall Stewart 	/*******************************************************/
54918933fa13SRandall Stewart 	/* 3. Update the PR-stream re-ordering queues and fix  */
54928933fa13SRandall Stewart 	/* delivery issues as needed.                       */
54938933fa13SRandall Stewart 	/*******************************************************/
5494f8829a4aSRandall Stewart 	fwd_sz -= sizeof(*fwd);
5495671d309cSRandall Stewart 	if (m && fwd_sz) {
5496f8829a4aSRandall Stewart 		/* New method. */
5497d61a0ae0SRandall Stewart 		unsigned int num_str;
5498671d309cSRandall Stewart 		struct sctp_strseq *stseq, strseqbuf;
5499671d309cSRandall Stewart 
5500671d309cSRandall Stewart 		offset += sizeof(*fwd);
5501f8829a4aSRandall Stewart 
55028933fa13SRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
5503f8829a4aSRandall Stewart 		num_str = fwd_sz / sizeof(struct sctp_strseq);
5504f8829a4aSRandall Stewart 		for (i = 0; i < num_str; i++) {
5505f8829a4aSRandall Stewart 			uint16_t st;
5506f8829a4aSRandall Stewart 
5507671d309cSRandall Stewart 			stseq = (struct sctp_strseq *)sctp_m_getptr(m, offset,
5508671d309cSRandall Stewart 			    sizeof(struct sctp_strseq),
5509671d309cSRandall Stewart 			    (uint8_t *) & strseqbuf);
5510671d309cSRandall Stewart 			offset += sizeof(struct sctp_strseq);
55112afb3e84SRandall Stewart 			if (stseq == NULL) {
5512671d309cSRandall Stewart 				break;
55132afb3e84SRandall Stewart 			}
5514f8829a4aSRandall Stewart 			/* Convert */
5515851b7298SRandall Stewart 			st = ntohs(stseq->stream);
5516851b7298SRandall Stewart 			stseq->stream = st;
5517851b7298SRandall Stewart 			st = ntohs(stseq->sequence);
5518851b7298SRandall Stewart 			stseq->sequence = st;
55198933fa13SRandall Stewart 
5520f8829a4aSRandall Stewart 			/* now process */
55218933fa13SRandall Stewart 
55228933fa13SRandall Stewart 			/*
55238933fa13SRandall Stewart 			 * Ok we now look for the stream/seq on the read
55248933fa13SRandall Stewart 			 * queue where its not all delivered. If we find it
55258933fa13SRandall Stewart 			 * we transmute the read entry into a PDI_ABORTED.
55268933fa13SRandall Stewart 			 */
5527851b7298SRandall Stewart 			if (stseq->stream >= asoc->streamincnt) {
55282afb3e84SRandall Stewart 				/* screwed up streams, stop!  */
55292afb3e84SRandall Stewart 				break;
5530f8829a4aSRandall Stewart 			}
55318933fa13SRandall Stewart 			if ((asoc->str_of_pdapi == stseq->stream) &&
55328933fa13SRandall Stewart 			    (asoc->ssn_of_pdapi == stseq->sequence)) {
55338933fa13SRandall Stewart 				/*
55348933fa13SRandall Stewart 				 * If this is the one we were partially
55358933fa13SRandall Stewart 				 * delivering now then we no longer are.
55368933fa13SRandall Stewart 				 * Note this will change with the reassembly
55378933fa13SRandall Stewart 				 * re-write.
55388933fa13SRandall Stewart 				 */
55398933fa13SRandall Stewart 				asoc->fragmented_delivery_inprogress = 0;
55408933fa13SRandall Stewart 			}
55418933fa13SRandall Stewart 			sctp_flush_reassm_for_str_seq(stcb, asoc, stseq->stream, stseq->sequence);
55428933fa13SRandall Stewart 			TAILQ_FOREACH(ctl, &stcb->sctp_ep->read_queue, next) {
55438933fa13SRandall Stewart 				if ((ctl->sinfo_stream == stseq->stream) &&
55448933fa13SRandall Stewart 				    (ctl->sinfo_ssn == stseq->sequence)) {
55458933fa13SRandall Stewart 					str_seq = (stseq->stream << 16) | stseq->sequence;
55468933fa13SRandall Stewart 					ctl->end_added = 1;
55478933fa13SRandall Stewart 					ctl->pdapi_aborted = 1;
55488933fa13SRandall Stewart 					sv = stcb->asoc.control_pdapi;
55498933fa13SRandall Stewart 					stcb->asoc.control_pdapi = ctl;
5550810ec536SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5551810ec536SMichael Tuexen 					    stcb,
55528933fa13SRandall Stewart 					    SCTP_PARTIAL_DELIVERY_ABORTED,
5553810ec536SMichael Tuexen 					    (void *)&str_seq,
5554810ec536SMichael Tuexen 					    SCTP_SO_NOT_LOCKED);
55558933fa13SRandall Stewart 					stcb->asoc.control_pdapi = sv;
55568933fa13SRandall Stewart 					break;
55578933fa13SRandall Stewart 				} else if ((ctl->sinfo_stream == stseq->stream) &&
555820b07a4dSMichael Tuexen 				    SCTP_SSN_GT(ctl->sinfo_ssn, stseq->sequence)) {
55598933fa13SRandall Stewart 					/* We are past our victim SSN */
55608933fa13SRandall Stewart 					break;
55618933fa13SRandall Stewart 				}
55628933fa13SRandall Stewart 			}
5563851b7298SRandall Stewart 			strm = &asoc->strmin[stseq->stream];
556420b07a4dSMichael Tuexen 			if (SCTP_SSN_GT(stseq->sequence, strm->last_sequence_delivered)) {
5565f8829a4aSRandall Stewart 				/* Update the sequence number */
556620b07a4dSMichael Tuexen 				strm->last_sequence_delivered = stseq->sequence;
5567f8829a4aSRandall Stewart 			}
5568f8829a4aSRandall Stewart 			/* now kick the stream the new way */
556904ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
5570f8829a4aSRandall Stewart 			sctp_kick_prsctp_reorder_queue(stcb, strm);
5571f8829a4aSRandall Stewart 		}
55728933fa13SRandall Stewart 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5573f8829a4aSRandall Stewart 	}
55747898f408SRandall Stewart 	/*
55757898f408SRandall Stewart 	 * Now slide thing forward.
55767898f408SRandall Stewart 	 */
55777898f408SRandall Stewart 	sctp_slide_mapping_arrays(stcb);
55787898f408SRandall Stewart 
557924f52bbdSMichael Tuexen 	if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
5580139bc87fSRandall Stewart 		/* now lets kick out and check for more fragmented delivery */
558104ee05e8SRandall Stewart 		/* sa_ignore NO_NULL_CHK */
5582139bc87fSRandall Stewart 		sctp_deliver_reasm_check(stcb, &stcb->asoc);
5583139bc87fSRandall Stewart 	}
5584f8829a4aSRandall Stewart }
5585