xref: /freebsd/sys/netinet/sctp_input.c (revision 105b68b42dd11bce5c554b1ef0ddf73aa069d7da)
1f8829a4aSRandall Stewart /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4830d754dSRandall Stewart  * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
5807aad63SMichael Tuexen  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6807aad63SMichael Tuexen  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7f8829a4aSRandall Stewart  *
8f8829a4aSRandall Stewart  * Redistribution and use in source and binary forms, with or without
9f8829a4aSRandall Stewart  * modification, are permitted provided that the following conditions are met:
10f8829a4aSRandall Stewart  *
11f8829a4aSRandall Stewart  * a) Redistributions of source code must retain the above copyright notice,
12f8829a4aSRandall Stewart  *    this list of conditions and the following disclaimer.
13f8829a4aSRandall Stewart  *
14f8829a4aSRandall Stewart  * b) Redistributions in binary form must reproduce the above copyright
15f8829a4aSRandall Stewart  *    notice, this list of conditions and the following disclaimer in
16f8829a4aSRandall Stewart  *    the documentation and/or other materials provided with the distribution.
17f8829a4aSRandall Stewart  *
18f8829a4aSRandall Stewart  * c) Neither the name of Cisco Systems, Inc. nor the names of its
19f8829a4aSRandall Stewart  *    contributors may be used to endorse or promote products derived
20f8829a4aSRandall Stewart  *    from this software without specific prior written permission.
21f8829a4aSRandall Stewart  *
22f8829a4aSRandall Stewart  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23f8829a4aSRandall Stewart  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24f8829a4aSRandall Stewart  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25f8829a4aSRandall Stewart  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26f8829a4aSRandall Stewart  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27f8829a4aSRandall Stewart  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28f8829a4aSRandall Stewart  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29f8829a4aSRandall Stewart  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30f8829a4aSRandall Stewart  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31f8829a4aSRandall Stewart  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32f8829a4aSRandall Stewart  * THE POSSIBILITY OF SUCH DAMAGE.
33f8829a4aSRandall Stewart  */
34f8829a4aSRandall Stewart 
35f8829a4aSRandall Stewart #include <sys/cdefs.h>
36f8829a4aSRandall Stewart __FBSDID("$FreeBSD$");
37f8829a4aSRandall Stewart 
38f8829a4aSRandall Stewart #include <netinet/sctp_os.h>
39f8829a4aSRandall Stewart #include <netinet/sctp_var.h>
4042551e99SRandall Stewart #include <netinet/sctp_sysctl.h>
41f8829a4aSRandall Stewart #include <netinet/sctp_pcb.h>
42f8829a4aSRandall Stewart #include <netinet/sctp_header.h>
43f8829a4aSRandall Stewart #include <netinet/sctputil.h>
44f8829a4aSRandall Stewart #include <netinet/sctp_output.h>
45f8829a4aSRandall Stewart #include <netinet/sctp_input.h>
46f8829a4aSRandall Stewart #include <netinet/sctp_auth.h>
47f8829a4aSRandall Stewart #include <netinet/sctp_indata.h>
48f8829a4aSRandall Stewart #include <netinet/sctp_asconf.h>
49207304d4SRandall Stewart #include <netinet/sctp_bsd_addr.h>
50851b7298SRandall Stewart #include <netinet/sctp_timer.h>
51a99b6783SRandall Stewart #include <netinet/sctp_crc32.h>
52776cd558SMichael Tuexen #include <netinet/sctp_kdtrace.h>
534474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
54c54a18d2SRandall Stewart #include <netinet/udp.h>
554474d71aSMichael Tuexen #endif
56bfc46083SRandall Stewart #include <sys/smp.h>
57f8829a4aSRandall Stewart 
58f8829a4aSRandall Stewart static void
59f8829a4aSRandall Stewart sctp_stop_all_cookie_timers(struct sctp_tcb *stcb)
60f8829a4aSRandall Stewart {
61f8829a4aSRandall Stewart 	struct sctp_nets *net;
62f8829a4aSRandall Stewart 
63a5d547adSRandall Stewart 	/*
64a5d547adSRandall Stewart 	 * This now not only stops all cookie timers it also stops any INIT
65a5d547adSRandall Stewart 	 * timers as well. This will make sure that the timers are stopped
66a5d547adSRandall Stewart 	 * in all collision cases.
67a5d547adSRandall Stewart 	 */
68f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
69f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
70a5d547adSRandall Stewart 		if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) {
71f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
72f8829a4aSRandall Stewart 			    stcb->sctp_ep,
73f8829a4aSRandall Stewart 			    stcb,
74a5d547adSRandall Stewart 			    net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1);
75a5d547adSRandall Stewart 		} else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) {
76a5d547adSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_INIT,
77a5d547adSRandall Stewart 			    stcb->sctp_ep,
78a5d547adSRandall Stewart 			    stcb,
79a5d547adSRandall Stewart 			    net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2);
80f8829a4aSRandall Stewart 		}
81f8829a4aSRandall Stewart 	}
82f8829a4aSRandall Stewart }
83f8829a4aSRandall Stewart 
84f8829a4aSRandall Stewart /* INIT handler */
85f8829a4aSRandall Stewart static void
86b1754ad1SMichael Tuexen sctp_handle_init(struct mbuf *m, int iphlen, int offset,
87b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
88f30ac432SMichael Tuexen     struct sctp_init_chunk *cp, struct sctp_inpcb *inp,
89ca83f93cSMichael Tuexen     struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_no_unlock,
90457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
91f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
92f8829a4aSRandall Stewart {
93f8829a4aSRandall Stewart 	struct sctp_init *init;
94f8829a4aSRandall Stewart 	struct mbuf *op_err;
95f8829a4aSRandall Stewart 
96ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n",
97dd294dceSMichael Tuexen 	    (void *)stcb);
98d55b0b1bSRandall Stewart 	if (stcb == NULL) {
99d55b0b1bSRandall Stewart 		SCTP_INP_RLOCK(inp);
100d55b0b1bSRandall Stewart 	}
101059ec222SMichael Tuexen 	/* Validate parameters */
102d68fdc4dSMichael Tuexen 	init = &cp->init;
103059ec222SMichael Tuexen 	if ((ntohl(init->initiate_tag) == 0) ||
104059ec222SMichael Tuexen 	    (ntohl(init->a_rwnd) < SCTP_MIN_RWND) ||
105059ec222SMichael Tuexen 	    (ntohs(init->num_inbound_streams) == 0) ||
106059ec222SMichael Tuexen 	    (ntohs(init->num_outbound_streams) == 0)) {
107f8829a4aSRandall Stewart 		/* protocol error... send abort */
108ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
109b1754ad1SMichael Tuexen 		sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
110457b4b88SMichael Tuexen 		    mflowtype, mflowid,
111c54a18d2SRandall Stewart 		    vrf_id, port);
112bff64a4dSRandall Stewart 		if (stcb)
113bff64a4dSRandall Stewart 			*abort_no_unlock = 1;
114d55b0b1bSRandall Stewart 		goto outnow;
115f8829a4aSRandall Stewart 	}
116f8829a4aSRandall Stewart 	if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
117d68fdc4dSMichael Tuexen 	    offset + ntohs(cp->ch.chunk_length))) {
118f8829a4aSRandall Stewart 		/* auth parameter(s) error... send abort */
119ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
120ff1ffd74SMichael Tuexen 		    "Problem with AUTH parameters");
121ff1ffd74SMichael Tuexen 		sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
122457b4b88SMichael Tuexen 		    mflowtype, mflowid,
123f30ac432SMichael Tuexen 		    vrf_id, port);
124bff64a4dSRandall Stewart 		if (stcb)
125bff64a4dSRandall Stewart 			*abort_no_unlock = 1;
126d55b0b1bSRandall Stewart 		goto outnow;
127f8829a4aSRandall Stewart 	}
1285d08768aSMichael Tuexen 	/* We are only accepting if we have a listening socket. */
129d68fdc4dSMichael Tuexen 	if ((stcb == NULL) &&
130d68fdc4dSMichael Tuexen 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
131d68fdc4dSMichael Tuexen 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1325d08768aSMichael Tuexen 	    (!SCTP_IS_LISTENING(inp)))) {
133d68fdc4dSMichael Tuexen 		/*
134d68fdc4dSMichael Tuexen 		 * FIX ME ?? What about TCP model and we have a
135d68fdc4dSMichael Tuexen 		 * match/restart case? Actually no fix is needed. the lookup
136d68fdc4dSMichael Tuexen 		 * will always find the existing assoc so stcb would not be
137d68fdc4dSMichael Tuexen 		 * NULL. It may be questionable to do this since we COULD
138d68fdc4dSMichael Tuexen 		 * just send back the INIT-ACK and hope that the app did
139d68fdc4dSMichael Tuexen 		 * accept()'s by the time the COOKIE was sent. But there is
140d68fdc4dSMichael Tuexen 		 * a price to pay for COOKIE generation and I don't want to
141d68fdc4dSMichael Tuexen 		 * pay it on the chance that the app will actually do some
142d68fdc4dSMichael Tuexen 		 * accepts(). The App just looses and should NOT be in this
143d68fdc4dSMichael Tuexen 		 * state :-)
144d68fdc4dSMichael Tuexen 		 */
145c58e60beSMichael Tuexen 		if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) {
146ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
147ff1ffd74SMichael Tuexen 			    "No listener");
148ff1ffd74SMichael Tuexen 			sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
149d089f9b9SMichael Tuexen 			    mflowtype, mflowid, inp->fibnum,
150f30ac432SMichael Tuexen 			    vrf_id, port);
151c58e60beSMichael Tuexen 		}
152d68fdc4dSMichael Tuexen 		goto outnow;
153d68fdc4dSMichael Tuexen 	}
154d68fdc4dSMichael Tuexen 	if ((stcb != NULL) &&
155839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
156d68fdc4dSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n");
157d68fdc4dSMichael Tuexen 		sctp_send_shutdown_ack(stcb, NULL);
158d68fdc4dSMichael Tuexen 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
159d68fdc4dSMichael Tuexen 	} else {
160ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
161ca83f93cSMichael Tuexen 		sctp_send_initiate_ack(inp, stcb, net, m, iphlen, offset,
162ca83f93cSMichael Tuexen 		    src, dst, sh, cp,
163457b4b88SMichael Tuexen 		    mflowtype, mflowid,
164cdd2d7d4SMichael Tuexen 		    vrf_id, port);
165d68fdc4dSMichael Tuexen 	}
166d55b0b1bSRandall Stewart outnow:
167d55b0b1bSRandall Stewart 	if (stcb == NULL) {
168d55b0b1bSRandall Stewart 		SCTP_INP_RUNLOCK(inp);
169d55b0b1bSRandall Stewart 	}
170f8829a4aSRandall Stewart }
171f8829a4aSRandall Stewart 
172f8829a4aSRandall Stewart /*
173f8829a4aSRandall Stewart  * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
174f8829a4aSRandall Stewart  */
1755bead436SRandall Stewart 
1765bead436SRandall Stewart int
17728397ac1SMichael Tuexen sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked)
1785bead436SRandall Stewart {
179124d851aSMichael Tuexen 	int unsent_data;
180f7a77f6fSMichael Tuexen 	unsigned int i;
181f7a77f6fSMichael Tuexen 	struct sctp_stream_queue_pending *sp;
1825bead436SRandall Stewart 	struct sctp_association *asoc;
1835bead436SRandall Stewart 
1845bead436SRandall Stewart 	/*
185124d851aSMichael Tuexen 	 * This function returns if any stream has true unsent data on it.
186124d851aSMichael Tuexen 	 * Note that as it looks through it will clean up any places that
187124d851aSMichael Tuexen 	 * have old data that has been sent but left at top of stream queue.
1885bead436SRandall Stewart 	 */
1895bead436SRandall Stewart 	asoc = &stcb->asoc;
190124d851aSMichael Tuexen 	unsent_data = 0;
1915bead436SRandall Stewart 	SCTP_TCB_SEND_LOCK(stcb);
192f7a77f6fSMichael Tuexen 	if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
193f7a77f6fSMichael Tuexen 		/* Check to see if some data queued */
194f7a77f6fSMichael Tuexen 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
19552be287eSRandall Stewart 			/* sa_ignore FREED_MEMORY */
196f7a77f6fSMichael Tuexen 			sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue);
197f7a77f6fSMichael Tuexen 			if (sp == NULL) {
198f7a77f6fSMichael Tuexen 				continue;
199f7a77f6fSMichael Tuexen 			}
2005bead436SRandall Stewart 			if ((sp->msg_is_complete) &&
2015bead436SRandall Stewart 			    (sp->length == 0) &&
2025bead436SRandall Stewart 			    (sp->sender_all_done)) {
2035bead436SRandall Stewart 				/*
2045bead436SRandall Stewart 				 * We are doing differed cleanup. Last time
2055bead436SRandall Stewart 				 * through when we took all the data the
2065bead436SRandall Stewart 				 * sender_all_done was not set.
2075bead436SRandall Stewart 				 */
2085bead436SRandall Stewart 				if (sp->put_last_out == 0) {
2095bead436SRandall Stewart 					SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
2105bead436SRandall Stewart 					SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
2115bead436SRandall Stewart 					    sp->sender_all_done,
2125bead436SRandall Stewart 					    sp->length,
2135bead436SRandall Stewart 					    sp->msg_is_complete,
2145bead436SRandall Stewart 					    sp->put_last_out);
2155bead436SRandall Stewart 				}
216f7a77f6fSMichael Tuexen 				atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
217f7a77f6fSMichael Tuexen 				TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next);
2184d58b0c3SMichael Tuexen 				stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, &asoc->strmout[i], sp, 1);
2199eea4a2dSMichael Tuexen 				if (sp->net) {
2205bead436SRandall Stewart 					sctp_free_remote_addr(sp->net);
2219eea4a2dSMichael Tuexen 					sp->net = NULL;
2229eea4a2dSMichael Tuexen 				}
2235bead436SRandall Stewart 				if (sp->data) {
2245bead436SRandall Stewart 					sctp_m_freem(sp->data);
2255bead436SRandall Stewart 					sp->data = NULL;
2265bead436SRandall Stewart 				}
227689e6a5fSMichael Tuexen 				sctp_free_a_strmoq(stcb, sp, so_locked);
228124d851aSMichael Tuexen 				if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
229124d851aSMichael Tuexen 					unsent_data++;
230124d851aSMichael Tuexen 				}
2315bead436SRandall Stewart 			} else {
2325bead436SRandall Stewart 				unsent_data++;
233124d851aSMichael Tuexen 			}
234124d851aSMichael Tuexen 			if (unsent_data > 0) {
2354a9ef3f8SMichael Tuexen 				break;
2365bead436SRandall Stewart 			}
2375bead436SRandall Stewart 		}
2385bead436SRandall Stewart 	}
2395bead436SRandall Stewart 	SCTP_TCB_SEND_UNLOCK(stcb);
2405bead436SRandall Stewart 	return (unsent_data);
2415bead436SRandall Stewart }
2425bead436SRandall Stewart 
243f8829a4aSRandall Stewart static int
2447215cc1bSMichael Tuexen sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb)
245f8829a4aSRandall Stewart {
246f8829a4aSRandall Stewart 	struct sctp_init *init;
247f8829a4aSRandall Stewart 	struct sctp_association *asoc;
248f8829a4aSRandall Stewart 	struct sctp_nets *lnet;
249f8829a4aSRandall Stewart 	unsigned int i;
250f8829a4aSRandall Stewart 
251f8829a4aSRandall Stewart 	init = &cp->init;
252f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
253f8829a4aSRandall Stewart 	/* save off parameters */
254f8829a4aSRandall Stewart 	asoc->peer_vtag = ntohl(init->initiate_tag);
255f8829a4aSRandall Stewart 	asoc->peers_rwnd = ntohl(init->a_rwnd);
256493d8e5aSRandall Stewart 	/* init tsn's */
257493d8e5aSRandall Stewart 	asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
258493d8e5aSRandall Stewart 
2599eea4a2dSMichael Tuexen 	if (!TAILQ_EMPTY(&asoc->nets)) {
260f8829a4aSRandall Stewart 		/* update any ssthresh's that may have a default */
261f8829a4aSRandall Stewart 		TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
262f8829a4aSRandall Stewart 			lnet->ssthresh = asoc->peers_rwnd;
263b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
264f8829a4aSRandall Stewart 				sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
26580fefe0aSRandall Stewart 			}
266f8829a4aSRandall Stewart 		}
267f8829a4aSRandall Stewart 	}
2686a91f103SRandall Stewart 	SCTP_TCB_SEND_LOCK(stcb);
269f8829a4aSRandall Stewart 	if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
270f8829a4aSRandall Stewart 		unsigned int newcnt;
271f8829a4aSRandall Stewart 		struct sctp_stream_out *outs;
2724a9ef3f8SMichael Tuexen 		struct sctp_stream_queue_pending *sp, *nsp;
2734a9ef3f8SMichael Tuexen 		struct sctp_tmit_chunk *chk, *nchk;
274f8829a4aSRandall Stewart 
275810ec536SMichael Tuexen 		/* abandon the upper streams */
276f8829a4aSRandall Stewart 		newcnt = ntohs(init->num_inbound_streams);
2774a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
27849656eefSMichael Tuexen 			if (chk->rec.data.sid >= newcnt) {
279810ec536SMichael Tuexen 				TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
280810ec536SMichael Tuexen 				asoc->send_queue_cnt--;
28149656eefSMichael Tuexen 				if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
28249656eefSMichael Tuexen 					asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
283a7ad6026SMichael Tuexen #ifdef INVARIANTS
284a7ad6026SMichael Tuexen 				} else {
28549656eefSMichael Tuexen 					panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
286a7ad6026SMichael Tuexen #endif
287a7ad6026SMichael Tuexen 				}
288810ec536SMichael Tuexen 				if (chk->data != NULL) {
289810ec536SMichael Tuexen 					sctp_free_bufspace(stcb, asoc, chk, 1);
2901edc9dbaSMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
2911edc9dbaSMichael Tuexen 					    0, chk, SCTP_SO_NOT_LOCKED);
292810ec536SMichael Tuexen 					if (chk->data) {
293810ec536SMichael Tuexen 						sctp_m_freem(chk->data);
294810ec536SMichael Tuexen 						chk->data = NULL;
295810ec536SMichael Tuexen 					}
296810ec536SMichael Tuexen 				}
297689e6a5fSMichael Tuexen 				sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
298810ec536SMichael Tuexen 				/* sa_ignore FREED_MEMORY */
299810ec536SMichael Tuexen 			}
300810ec536SMichael Tuexen 		}
301f8829a4aSRandall Stewart 		if (asoc->strmout) {
302f8829a4aSRandall Stewart 			for (i = newcnt; i < asoc->pre_open_streams; i++) {
303f8829a4aSRandall Stewart 				outs = &asoc->strmout[i];
3044a9ef3f8SMichael Tuexen 				TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
3054d58b0c3SMichael Tuexen 					atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
306810ec536SMichael Tuexen 					TAILQ_REMOVE(&outs->outqueue, sp, next);
3074d58b0c3SMichael Tuexen 					stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, 1);
308f8829a4aSRandall Stewart 					sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
3091edc9dbaSMichael Tuexen 					    stcb, 0, sp, SCTP_SO_NOT_LOCKED);
310f8829a4aSRandall Stewart 					if (sp->data) {
311f8829a4aSRandall Stewart 						sctp_m_freem(sp->data);
312f8829a4aSRandall Stewart 						sp->data = NULL;
313f8829a4aSRandall Stewart 					}
3149eea4a2dSMichael Tuexen 					if (sp->net) {
315f8829a4aSRandall Stewart 						sctp_free_remote_addr(sp->net);
316f8829a4aSRandall Stewart 						sp->net = NULL;
3179eea4a2dSMichael Tuexen 					}
318f8829a4aSRandall Stewart 					/* Free the chunk */
319689e6a5fSMichael Tuexen 					sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED);
3203c503c28SRandall Stewart 					/* sa_ignore FREED_MEMORY */
321f8829a4aSRandall Stewart 				}
3227cca1775SRandall Stewart 				outs->state = SCTP_STREAM_CLOSED;
323f8829a4aSRandall Stewart 			}
324f8829a4aSRandall Stewart 		}
325810ec536SMichael Tuexen 		/* cut back the count */
326f8829a4aSRandall Stewart 		asoc->pre_open_streams = newcnt;
327f8829a4aSRandall Stewart 	}
3286a91f103SRandall Stewart 	SCTP_TCB_SEND_UNLOCK(stcb);
3297cca1775SRandall Stewart 	asoc->streamoutcnt = asoc->pre_open_streams;
330fdc4c9d0SMichael Tuexen 	if (asoc->strmout) {
3317cca1775SRandall Stewart 		for (i = 0; i < asoc->streamoutcnt; i++) {
3327cca1775SRandall Stewart 			asoc->strmout[i].state = SCTP_STREAM_OPEN;
3337cca1775SRandall Stewart 		}
334fdc4c9d0SMichael Tuexen 	}
335830d754dSRandall Stewart 	/* EY - nr_sack: initialize highest tsn in nr_mapping_array */
336830d754dSRandall Stewart 	asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
337b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
338f8829a4aSRandall Stewart 		sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
33980fefe0aSRandall Stewart 	}
340f8829a4aSRandall Stewart 	/* This is the next one we expect */
341f8829a4aSRandall Stewart 	asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
342f8829a4aSRandall Stewart 
343f8829a4aSRandall Stewart 	asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
344d6af161aSRandall Stewart 	asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
345493d8e5aSRandall Stewart 
346f8829a4aSRandall Stewart 	asoc->advanced_peer_ack_point = asoc->last_acked_seq;
347f8829a4aSRandall Stewart 	/* open the requested streams */
348207304d4SRandall Stewart 
349f8829a4aSRandall Stewart 	if (asoc->strmin != NULL) {
350f8829a4aSRandall Stewart 		/* Free the old ones */
3516a91f103SRandall Stewart 		for (i = 0; i < asoc->streamincnt; i++) {
35244249214SRandall Stewart 			sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue);
35344249214SRandall Stewart 			sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue);
3546a91f103SRandall Stewart 		}
355207304d4SRandall Stewart 		SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
356f8829a4aSRandall Stewart 	}
357ee1ccd92SMichael Tuexen 	if (asoc->max_inbound_streams > ntohs(init->num_outbound_streams)) {
3586a91f103SRandall Stewart 		asoc->streamincnt = ntohs(init->num_outbound_streams);
359ee1ccd92SMichael Tuexen 	} else {
360ee1ccd92SMichael Tuexen 		asoc->streamincnt = asoc->max_inbound_streams;
3616a91f103SRandall Stewart 	}
362f8829a4aSRandall Stewart 	SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
363207304d4SRandall Stewart 	    sizeof(struct sctp_stream_in), SCTP_M_STRMI);
364f8829a4aSRandall Stewart 	if (asoc->strmin == NULL) {
365f8829a4aSRandall Stewart 		/* we didn't get memory for the streams! */
366ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
367f8829a4aSRandall Stewart 		return (-1);
368f8829a4aSRandall Stewart 	}
369f8829a4aSRandall Stewart 	for (i = 0; i < asoc->streamincnt; i++) {
37049656eefSMichael Tuexen 		asoc->strmin[i].sid = i;
37149656eefSMichael Tuexen 		asoc->strmin[i].last_mid_delivered = 0xffffffff;
372f8829a4aSRandall Stewart 		TAILQ_INIT(&asoc->strmin[i].inqueue);
37344249214SRandall Stewart 		TAILQ_INIT(&asoc->strmin[i].uno_inqueue);
37444249214SRandall Stewart 		asoc->strmin[i].pd_api_started = 0;
3759a6142d8SRandall Stewart 		asoc->strmin[i].delivery_started = 0;
376f8829a4aSRandall Stewart 	}
377f8829a4aSRandall Stewart 	/*
378f8829a4aSRandall Stewart 	 * load_address_from_init will put the addresses into the
379f8829a4aSRandall Stewart 	 * association when the COOKIE is processed or the INIT-ACK is
380f8829a4aSRandall Stewart 	 * processed. Both types of COOKIE's existing and new call this
381f8829a4aSRandall Stewart 	 * routine. It will remove addresses that are no longer in the
382f8829a4aSRandall Stewart 	 * association (for the restarting case where addresses are
383f8829a4aSRandall Stewart 	 * removed). Up front when the INIT arrives we will discard it if it
384f8829a4aSRandall Stewart 	 * is a restart and new addresses have been added.
385f8829a4aSRandall Stewart 	 */
3863c503c28SRandall Stewart 	/* sa_ignore MEMLEAK */
387f8829a4aSRandall Stewart 	return (0);
388f8829a4aSRandall Stewart }
389f8829a4aSRandall Stewart 
390f8829a4aSRandall Stewart /*
391f8829a4aSRandall Stewart  * INIT-ACK message processing/consumption returns value < 0 on error
392f8829a4aSRandall Stewart  */
393f8829a4aSRandall Stewart static int
394b1754ad1SMichael Tuexen sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
395b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
396f30ac432SMichael Tuexen     struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
397f30ac432SMichael Tuexen     struct sctp_nets *net, int *abort_no_unlock,
398457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
399f30ac432SMichael Tuexen     uint32_t vrf_id)
400f8829a4aSRandall Stewart {
401f8829a4aSRandall Stewart 	struct sctp_association *asoc;
402f8829a4aSRandall Stewart 	struct mbuf *op_err;
4036182677fSMichael Tuexen 	int retval, abort_flag, cookie_found;
4046182677fSMichael Tuexen 	int initack_limit;
405830d754dSRandall Stewart 	int nat_friendly = 0;
406f8829a4aSRandall Stewart 
407f8829a4aSRandall Stewart 	/* First verify that we have no illegal param's */
408f8829a4aSRandall Stewart 	abort_flag = 0;
4096182677fSMichael Tuexen 	cookie_found = 0;
410f8829a4aSRandall Stewart 
411f8829a4aSRandall Stewart 	op_err = sctp_arethere_unrecognized_parameters(m,
412f8829a4aSRandall Stewart 	    (offset + sizeof(struct sctp_init_chunk)),
4136182677fSMichael Tuexen 	    &abort_flag, (struct sctp_chunkhdr *)cp,
4146182677fSMichael Tuexen 	    &nat_friendly, &cookie_found);
415f8829a4aSRandall Stewart 	if (abort_flag) {
416f8829a4aSRandall Stewart 		/* Send an abort and notify peer */
417*105b68b4SMichael Tuexen 		sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, false, SCTP_SO_NOT_LOCKED);
418bff64a4dSRandall Stewart 		*abort_no_unlock = 1;
419f8829a4aSRandall Stewart 		return (-1);
420f8829a4aSRandall Stewart 	}
4216182677fSMichael Tuexen 	if (!cookie_found) {
4226182677fSMichael Tuexen 		uint16_t len;
4236182677fSMichael Tuexen 
4240941b9dcSMichael Tuexen 		/* Only report the missing cookie parameter */
4250941b9dcSMichael Tuexen 		if (op_err != NULL) {
4260941b9dcSMichael Tuexen 			sctp_m_freem(op_err);
4270941b9dcSMichael Tuexen 		}
4286182677fSMichael Tuexen 		len = (uint16_t)(sizeof(struct sctp_error_missing_param) + sizeof(uint16_t));
4296182677fSMichael Tuexen 		/* We abort with an error of missing mandatory param */
4306182677fSMichael Tuexen 		op_err = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
4316182677fSMichael Tuexen 		if (op_err != NULL) {
4326182677fSMichael Tuexen 			struct sctp_error_missing_param *cause;
4336182677fSMichael Tuexen 
4346182677fSMichael Tuexen 			SCTP_BUF_LEN(op_err) = len;
4356182677fSMichael Tuexen 			cause = mtod(op_err, struct sctp_error_missing_param *);
4366182677fSMichael Tuexen 			/* Subtract the reserved param */
4376182677fSMichael Tuexen 			cause->cause.code = htons(SCTP_CAUSE_MISSING_PARAM);
4386182677fSMichael Tuexen 			cause->cause.length = htons(len);
4396182677fSMichael Tuexen 			cause->num_missing_params = htonl(1);
4406182677fSMichael Tuexen 			cause->type[0] = htons(SCTP_STATE_COOKIE);
4416182677fSMichael Tuexen 		}
4426182677fSMichael Tuexen 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
4436182677fSMichael Tuexen 		    src, dst, sh, op_err,
4446182677fSMichael Tuexen 		    mflowtype, mflowid,
4456182677fSMichael Tuexen 		    vrf_id, net->port);
4466182677fSMichael Tuexen 		*abort_no_unlock = 1;
4476182677fSMichael Tuexen 		return (-3);
4486182677fSMichael Tuexen 	}
449f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
450830d754dSRandall Stewart 	asoc->peer_supports_nat = (uint8_t)nat_friendly;
451f8829a4aSRandall Stewart 	/* process the peer's parameters in the INIT-ACK */
4525f2e1835SMichael Tuexen 	if (sctp_process_init((struct sctp_init_chunk *)cp, stcb) < 0) {
453645f3a1cSMichael Tuexen 		if (op_err != NULL) {
454645f3a1cSMichael Tuexen 			sctp_m_freem(op_err);
455645f3a1cSMichael Tuexen 		}
4565f2e1835SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4575f2e1835SMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_init() failed\n");
4585f2e1835SMichael Tuexen 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
4595f2e1835SMichael Tuexen 		    src, dst, sh, op_err,
4605f2e1835SMichael Tuexen 		    mflowtype, mflowid,
4615f2e1835SMichael Tuexen 		    vrf_id, net->port);
4625f2e1835SMichael Tuexen 		*abort_no_unlock = 1;
4635f2e1835SMichael Tuexen 		return (-1);
464f8829a4aSRandall Stewart 	}
465f8829a4aSRandall Stewart 	initack_limit = offset + ntohs(cp->ch.chunk_length);
466f8829a4aSRandall Stewart 	/* load all addresses */
4677215cc1bSMichael Tuexen 	if ((retval = sctp_load_addresses_from_init(stcb, m,
4685f2e1835SMichael Tuexen 	    offset + sizeof(struct sctp_init_chunk),
4695f2e1835SMichael Tuexen 	    initack_limit, src, dst, NULL, stcb->asoc.port)) < 0) {
470645f3a1cSMichael Tuexen 		if (op_err != NULL) {
471645f3a1cSMichael Tuexen 			sctp_m_freem(op_err);
472645f3a1cSMichael Tuexen 		}
473ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
474ff1ffd74SMichael Tuexen 		    "Problem with address parameters");
475ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1,
476ad81507eSRandall Stewart 		    "Load addresses from INIT causes an abort %d\n",
477ad81507eSRandall Stewart 		    retval);
478b1754ad1SMichael Tuexen 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
479ff1ffd74SMichael Tuexen 		    src, dst, sh, op_err,
480457b4b88SMichael Tuexen 		    mflowtype, mflowid,
481f30ac432SMichael Tuexen 		    vrf_id, net->port);
482bff64a4dSRandall Stewart 		*abort_no_unlock = 1;
483f8829a4aSRandall Stewart 		return (-1);
484f8829a4aSRandall Stewart 	}
48518e198d3SRandall Stewart 	/* if the peer doesn't support asconf, flush the asconf queue */
486c79bec9cSMichael Tuexen 	if (asoc->asconf_supported == 0) {
4874a9ef3f8SMichael Tuexen 		struct sctp_asconf_addr *param, *nparam;
48818e198d3SRandall Stewart 
4894a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
4904a9ef3f8SMichael Tuexen 			TAILQ_REMOVE(&asoc->asconf_queue, param, next);
4914a9ef3f8SMichael Tuexen 			SCTP_FREE(param, SCTP_M_ASC_ADDR);
49218e198d3SRandall Stewart 		}
49318e198d3SRandall Stewart 	}
4940053ed28SMichael Tuexen 
495f8829a4aSRandall Stewart 	stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
496f8829a4aSRandall Stewart 	    stcb->asoc.local_hmacs);
497f8829a4aSRandall Stewart 	if (op_err) {
498f8829a4aSRandall Stewart 		sctp_queue_op_err(stcb, op_err);
499f8829a4aSRandall Stewart 		/* queuing will steal away the mbuf chain to the out queue */
500f8829a4aSRandall Stewart 		op_err = NULL;
501f8829a4aSRandall Stewart 	}
502f8829a4aSRandall Stewart 	/* extract the cookie and queue it to "echo" it back... */
503b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
504c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
505c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
506c4739e2fSRandall Stewart 		    0,
507c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INPUT,
508c4739e2fSRandall Stewart 		    __LINE__);
509c4739e2fSRandall Stewart 	}
510f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
511f8829a4aSRandall Stewart 	net->error_count = 0;
512f8829a4aSRandall Stewart 
513f8829a4aSRandall Stewart 	/*
514f8829a4aSRandall Stewart 	 * Cancel the INIT timer, We do this first before queueing the
515f8829a4aSRandall Stewart 	 * cookie. We always cancel at the primary to assue that we are
516f8829a4aSRandall Stewart 	 * canceling the timer started by the INIT which always goes to the
517f8829a4aSRandall Stewart 	 * primary.
518f8829a4aSRandall Stewart 	 */
519f8829a4aSRandall Stewart 	sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
520b7d130beSMichael Tuexen 	    asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
521a5d547adSRandall Stewart 
522a5d547adSRandall Stewart 	/* calculate the RTO */
52344f2a327SMichael Tuexen 	sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered,
524f79aab18SRandall Stewart 	    SCTP_RTT_FROM_NON_DATA);
5256182677fSMichael Tuexen 	retval = sctp_send_cookie_echo(m, offset, initack_limit, stcb, net);
526f8829a4aSRandall Stewart 	return (retval);
527f8829a4aSRandall Stewart }
5280053ed28SMichael Tuexen 
529f8829a4aSRandall Stewart static void
530f8829a4aSRandall Stewart sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
531f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net)
532f8829a4aSRandall Stewart {
53324aaac8dSMichael Tuexen 	union sctp_sockstore store;
53452129fcdSRandall Stewart 	struct sctp_nets *r_net, *f_net;
535f8829a4aSRandall Stewart 	struct timeval tv;
536d55b0b1bSRandall Stewart 	int req_prim = 0;
537ca85e948SMichael Tuexen 	uint16_t old_error_counter;
538f8829a4aSRandall Stewart 
539f8829a4aSRandall Stewart 	if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
540f8829a4aSRandall Stewart 		/* Invalid length */
541f8829a4aSRandall Stewart 		return;
542f8829a4aSRandall Stewart 	}
5430053ed28SMichael Tuexen 
544f8829a4aSRandall Stewart 	memset(&store, 0, sizeof(store));
545e6194c2eSMichael Tuexen 	switch (cp->heartbeat.hb_info.addr_family) {
546e6194c2eSMichael Tuexen #ifdef INET
547e6194c2eSMichael Tuexen 	case AF_INET:
548e6194c2eSMichael Tuexen 		if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
54924aaac8dSMichael Tuexen 			store.sin.sin_family = cp->heartbeat.hb_info.addr_family;
55024aaac8dSMichael Tuexen 			store.sin.sin_len = cp->heartbeat.hb_info.addr_len;
55124aaac8dSMichael Tuexen 			store.sin.sin_port = stcb->rport;
55224aaac8dSMichael Tuexen 			memcpy(&store.sin.sin_addr, cp->heartbeat.hb_info.address,
55324aaac8dSMichael Tuexen 			    sizeof(store.sin.sin_addr));
554e6194c2eSMichael Tuexen 		} else {
555e6194c2eSMichael Tuexen 			return;
556e6194c2eSMichael Tuexen 		}
557e6194c2eSMichael Tuexen 		break;
558e6194c2eSMichael Tuexen #endif
559e6194c2eSMichael Tuexen #ifdef INET6
560e6194c2eSMichael Tuexen 	case AF_INET6:
561e6194c2eSMichael Tuexen 		if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
56224aaac8dSMichael Tuexen 			store.sin6.sin6_family = cp->heartbeat.hb_info.addr_family;
56324aaac8dSMichael Tuexen 			store.sin6.sin6_len = cp->heartbeat.hb_info.addr_len;
56424aaac8dSMichael Tuexen 			store.sin6.sin6_port = stcb->rport;
56523602b60SMichael Tuexen 			memcpy(&store.sin6.sin6_addr, cp->heartbeat.hb_info.address, sizeof(struct in6_addr));
566f8829a4aSRandall Stewart 		} else {
567f8829a4aSRandall Stewart 			return;
568f8829a4aSRandall Stewart 		}
569e6194c2eSMichael Tuexen 		break;
570e6194c2eSMichael Tuexen #endif
571e6194c2eSMichael Tuexen 	default:
572e6194c2eSMichael Tuexen 		return;
573e6194c2eSMichael Tuexen 	}
57424aaac8dSMichael Tuexen 	r_net = sctp_findnet(stcb, &store.sa);
575f8829a4aSRandall Stewart 	if (r_net == NULL) {
576ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
577f8829a4aSRandall Stewart 		return;
578f8829a4aSRandall Stewart 	}
579f8829a4aSRandall Stewart 	if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
580f8829a4aSRandall Stewart 	    (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
581f8829a4aSRandall Stewart 	    (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
582f8829a4aSRandall Stewart 		/*
583f8829a4aSRandall Stewart 		 * If the its a HB and it's random value is correct when can
584f8829a4aSRandall Stewart 		 * confirm the destination.
585f8829a4aSRandall Stewart 		 */
586f8829a4aSRandall Stewart 		r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
58742551e99SRandall Stewart 		if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
58842551e99SRandall Stewart 			stcb->asoc.primary_destination = r_net;
58942551e99SRandall Stewart 			r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
59052129fcdSRandall Stewart 			f_net = TAILQ_FIRST(&stcb->asoc.nets);
59152129fcdSRandall Stewart 			if (f_net != r_net) {
59242551e99SRandall Stewart 				/*
59342551e99SRandall Stewart 				 * first one on the list is NOT the primary
594cd0a4ff6SPedro F. Giffuni 				 * sctp_cmpaddr() is much more efficient if
59542551e99SRandall Stewart 				 * the primary is the first on the list,
59642551e99SRandall Stewart 				 * make it so.
59742551e99SRandall Stewart 				 */
59852129fcdSRandall Stewart 				TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
59952129fcdSRandall Stewart 				TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
60042551e99SRandall Stewart 			}
601d55b0b1bSRandall Stewart 			req_prim = 1;
60242551e99SRandall Stewart 		}
603f8829a4aSRandall Stewart 		sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
604ceaad40aSRandall Stewart 		    stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
605b7d130beSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb,
606b7d130beSMichael Tuexen 		    r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
607ca85e948SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
608f8829a4aSRandall Stewart 	}
609469a65d1SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
610469a65d1SMichael Tuexen 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
611469a65d1SMichael Tuexen 		    stcb->asoc.overall_error_count,
612469a65d1SMichael Tuexen 		    0,
613469a65d1SMichael Tuexen 		    SCTP_FROM_SCTP_INPUT,
614469a65d1SMichael Tuexen 		    __LINE__);
615469a65d1SMichael Tuexen 	}
616469a65d1SMichael Tuexen 	stcb->asoc.overall_error_count = 0;
617ca85e948SMichael Tuexen 	old_error_counter = r_net->error_count;
618f8829a4aSRandall Stewart 	r_net->error_count = 0;
619f8829a4aSRandall Stewart 	r_net->hb_responded = 1;
620f8829a4aSRandall Stewart 	tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
621f8829a4aSRandall Stewart 	tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
622f8829a4aSRandall Stewart 	/* Now lets do a RTO with this */
62344f2a327SMichael Tuexen 	sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv,
624f79aab18SRandall Stewart 	    SCTP_RTT_FROM_NON_DATA);
625ca85e948SMichael Tuexen 	if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) {
626ca85e948SMichael Tuexen 		r_net->dest_state |= SCTP_ADDR_REACHABLE;
627ca85e948SMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
6284b1f78e1SMichael Tuexen 		    0, (void *)r_net, SCTP_SO_NOT_LOCKED);
629ca85e948SMichael Tuexen 	}
630ca85e948SMichael Tuexen 	if (r_net->dest_state & SCTP_ADDR_PF) {
631ca85e948SMichael Tuexen 		r_net->dest_state &= ~SCTP_ADDR_PF;
632ca85e948SMichael Tuexen 		stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
633ca85e948SMichael Tuexen 	}
634ca85e948SMichael Tuexen 	if (old_error_counter > 0) {
635b7d130beSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
636b7d130beSMichael Tuexen 		    stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_5);
637ca85e948SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
638ca85e948SMichael Tuexen 	}
639ca85e948SMichael Tuexen 	if (r_net == stcb->asoc.primary_destination) {
640ca85e948SMichael Tuexen 		if (stcb->asoc.alternate) {
641ca85e948SMichael Tuexen 			/* release the alternate, primary is good */
642ca85e948SMichael Tuexen 			sctp_free_remote_addr(stcb->asoc.alternate);
643ca85e948SMichael Tuexen 			stcb->asoc.alternate = NULL;
644ca85e948SMichael Tuexen 		}
645ca85e948SMichael Tuexen 	}
646d55b0b1bSRandall Stewart 	/* Mobility adaptation */
647d55b0b1bSRandall Stewart 	if (req_prim) {
648d55b0b1bSRandall Stewart 		if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
649d55b0b1bSRandall Stewart 		    SCTP_MOBILITY_BASE) ||
650d55b0b1bSRandall Stewart 		    sctp_is_mobility_feature_on(stcb->sctp_ep,
651d55b0b1bSRandall Stewart 		    SCTP_MOBILITY_FASTHANDOFF)) &&
652d55b0b1bSRandall Stewart 		    sctp_is_mobility_feature_on(stcb->sctp_ep,
653d55b0b1bSRandall Stewart 		    SCTP_MOBILITY_PRIM_DELETED)) {
654b7d130beSMichael Tuexen 			sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED,
655b7d130beSMichael Tuexen 			    stcb->sctp_ep, stcb, NULL,
656b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
657d55b0b1bSRandall Stewart 			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
658d55b0b1bSRandall Stewart 			    SCTP_MOBILITY_FASTHANDOFF)) {
659d55b0b1bSRandall Stewart 				sctp_assoc_immediate_retrans(stcb,
660d55b0b1bSRandall Stewart 				    stcb->asoc.primary_destination);
661d55b0b1bSRandall Stewart 			}
662d55b0b1bSRandall Stewart 			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
663d55b0b1bSRandall Stewart 			    SCTP_MOBILITY_BASE)) {
6649eea4a2dSMichael Tuexen 				sctp_move_chunks_from_net(stcb,
6659eea4a2dSMichael Tuexen 				    stcb->asoc.deleted_primary);
666d55b0b1bSRandall Stewart 			}
667a57fb68bSMichael Tuexen 			sctp_delete_prim_timer(stcb->sctp_ep, stcb);
668d55b0b1bSRandall Stewart 		}
669d55b0b1bSRandall Stewart 	}
670f8829a4aSRandall Stewart }
671f8829a4aSRandall Stewart 
672830d754dSRandall Stewart static int
673830d754dSRandall Stewart sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
674830d754dSRandall Stewart {
675830d754dSRandall Stewart 	/*
6761325a0deSMichael Tuexen 	 * Return 0 means we want you to proceed with the abort non-zero
6771325a0deSMichael Tuexen 	 * means no abort processing.
678830d754dSRandall Stewart 	 */
6791325a0deSMichael Tuexen 	uint32_t new_vtag;
680830d754dSRandall Stewart 	struct sctpasochead *head;
681830d754dSRandall Stewart 
682839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
683839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
6841325a0deSMichael Tuexen 		new_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
685d28a3a39SMichael Tuexen 		atomic_add_int(&stcb->asoc.refcnt, 1);
686d28a3a39SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
687d28a3a39SMichael Tuexen 		SCTP_INP_INFO_WLOCK();
688d28a3a39SMichael Tuexen 		SCTP_TCB_LOCK(stcb);
689d28a3a39SMichael Tuexen 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
6901325a0deSMichael Tuexen 	} else {
6911325a0deSMichael Tuexen 		return (0);
692d28a3a39SMichael Tuexen 	}
693839d21d6SMichael Tuexen 	if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) {
694830d754dSRandall Stewart 		/* generate a new vtag and send init */
695830d754dSRandall Stewart 		LIST_REMOVE(stcb, sctp_asocs);
6961325a0deSMichael Tuexen 		stcb->asoc.my_vtag = new_vtag;
697830d754dSRandall Stewart 		head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
698b7b84c0eSMichael Tuexen 		/*
699b7b84c0eSMichael Tuexen 		 * put it in the bucket in the vtag hash of assoc's for the
700b7b84c0eSMichael Tuexen 		 * system
701b7b84c0eSMichael Tuexen 		 */
702830d754dSRandall Stewart 		LIST_INSERT_HEAD(head, stcb, sctp_asocs);
703d28a3a39SMichael Tuexen 		SCTP_INP_INFO_WUNLOCK();
7041325a0deSMichael Tuexen 		sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
705830d754dSRandall Stewart 		return (1);
7061325a0deSMichael Tuexen 	} else {
707830d754dSRandall Stewart 		/*
708830d754dSRandall Stewart 		 * treat like a case where the cookie expired i.e.: - dump
709830d754dSRandall Stewart 		 * current cookie. - generate a new vtag. - resend init.
710830d754dSRandall Stewart 		 */
711830d754dSRandall Stewart 		/* generate a new vtag and send init */
712830d754dSRandall Stewart 		LIST_REMOVE(stcb, sctp_asocs);
713839d21d6SMichael Tuexen 		SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
714830d754dSRandall Stewart 		sctp_stop_all_cookie_timers(stcb);
715830d754dSRandall Stewart 		sctp_toss_old_cookies(stcb, &stcb->asoc);
7161325a0deSMichael Tuexen 		stcb->asoc.my_vtag = new_vtag;
717830d754dSRandall Stewart 		head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
718b7b84c0eSMichael Tuexen 		/*
719b7b84c0eSMichael Tuexen 		 * put it in the bucket in the vtag hash of assoc's for the
720b7b84c0eSMichael Tuexen 		 * system
721b7b84c0eSMichael Tuexen 		 */
722830d754dSRandall Stewart 		LIST_INSERT_HEAD(head, stcb, sctp_asocs);
723d28a3a39SMichael Tuexen 		SCTP_INP_INFO_WUNLOCK();
7241325a0deSMichael Tuexen 		sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
725830d754dSRandall Stewart 		return (1);
726830d754dSRandall Stewart 	}
727830d754dSRandall Stewart 	return (0);
728830d754dSRandall Stewart }
729830d754dSRandall Stewart 
730830d754dSRandall Stewart static int
731830d754dSRandall Stewart sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
732830d754dSRandall Stewart     struct sctp_nets *net)
733830d754dSRandall Stewart {
734830d754dSRandall Stewart 	/*
735830d754dSRandall Stewart 	 * return 0 means we want you to proceed with the abort non-zero
736830d754dSRandall Stewart 	 * means no abort processing
737830d754dSRandall Stewart 	 */
738c79bec9cSMichael Tuexen 	if (stcb->asoc.auth_supported == 0) {
739830d754dSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
740830d754dSRandall Stewart 		return (0);
741830d754dSRandall Stewart 	}
742830d754dSRandall Stewart 	sctp_asconf_send_nat_state_update(stcb, net);
743830d754dSRandall Stewart 	return (1);
744830d754dSRandall Stewart }
745830d754dSRandall Stewart 
746701492a5SMichael Tuexen /* Returns 1 if the stcb was aborted, 0 otherwise */
747701492a5SMichael Tuexen static int
748a2b42326SMichael Tuexen sctp_handle_abort(struct sctp_abort_chunk *abort,
749f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net)
750f8829a4aSRandall Stewart {
751830d754dSRandall Stewart 	uint16_t len;
752a2b42326SMichael Tuexen 	uint16_t error;
753ceaad40aSRandall Stewart 
754ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
755f8829a4aSRandall Stewart 	if (stcb == NULL)
756701492a5SMichael Tuexen 		return (0);
757f8829a4aSRandall Stewart 
758a2b42326SMichael Tuexen 	len = ntohs(abort->ch.chunk_length);
759701492a5SMichael Tuexen 	if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_error_cause)) {
760830d754dSRandall Stewart 		/*
761830d754dSRandall Stewart 		 * Need to check the cause codes for our two magic nat
762830d754dSRandall Stewart 		 * aborts which don't kill the assoc necessarily.
763830d754dSRandall Stewart 		 */
764701492a5SMichael Tuexen 		struct sctp_error_cause *cause;
765830d754dSRandall Stewart 
766701492a5SMichael Tuexen 		cause = (struct sctp_error_cause *)(abort + 1);
76786eda749SMichael Tuexen 		error = ntohs(cause->code);
768a2b42326SMichael Tuexen 		if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) {
769504ee6a0SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state, ABORT flags:%x\n",
770a2b42326SMichael Tuexen 			    abort->ch.chunk_flags);
771830d754dSRandall Stewart 			if (sctp_handle_nat_colliding_state(stcb)) {
772701492a5SMichael Tuexen 				return (0);
773830d754dSRandall Stewart 			}
774a2b42326SMichael Tuexen 		} else if (error == SCTP_CAUSE_NAT_MISSING_STATE) {
775504ee6a0SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state, ABORT flags:%x\n",
776a2b42326SMichael Tuexen 			    abort->ch.chunk_flags);
777830d754dSRandall Stewart 			if (sctp_handle_nat_missing_state(stcb, net)) {
778701492a5SMichael Tuexen 				return (0);
779830d754dSRandall Stewart 			}
780830d754dSRandall Stewart 		}
781a2b42326SMichael Tuexen 	} else {
782a2b42326SMichael Tuexen 		error = 0;
783830d754dSRandall Stewart 	}
784f8829a4aSRandall Stewart 	/* stop any receive timers */
7856fb7b4fbSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
786b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_7);
787f8829a4aSRandall Stewart 	/* notify user of the abort and clean up... */
788*105b68b4SMichael Tuexen 	sctp_abort_notification(stcb, true, false, error, abort, SCTP_SO_NOT_LOCKED);
789f8829a4aSRandall Stewart 	/* free the tcb */
790f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_aborted);
791839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
792839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
793f8829a4aSRandall Stewart 		SCTP_STAT_DECR_GAUGE32(sctps_currestab);
794f8829a4aSRandall Stewart 	}
795f1f73e57SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
796f1f73e57SRandall Stewart 	sctp_print_out_track_log(stcb);
797f1f73e57SRandall Stewart #endif
798c4739e2fSRandall Stewart 	(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
799b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
800ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
801701492a5SMichael Tuexen 	return (1);
802f8829a4aSRandall Stewart }
803f8829a4aSRandall Stewart 
804f8829a4aSRandall Stewart static void
805ca85e948SMichael Tuexen sctp_start_net_timers(struct sctp_tcb *stcb)
806ca85e948SMichael Tuexen {
807ca85e948SMichael Tuexen 	uint32_t cnt_hb_sent;
808ca85e948SMichael Tuexen 	struct sctp_nets *net;
809ca85e948SMichael Tuexen 
810ca85e948SMichael Tuexen 	cnt_hb_sent = 0;
811ca85e948SMichael Tuexen 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
812ca85e948SMichael Tuexen 		/*
813ca85e948SMichael Tuexen 		 * For each network start: 1) A pmtu timer. 2) A HB timer 3)
814ca85e948SMichael Tuexen 		 * If the dest in unconfirmed send a hb as well if under
815ca85e948SMichael Tuexen 		 * max_hb_burst have been sent.
816ca85e948SMichael Tuexen 		 */
817ca85e948SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
818ca85e948SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
819ca85e948SMichael Tuexen 		if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
820ca85e948SMichael Tuexen 		    (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
821ca85e948SMichael Tuexen 			sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
822ca85e948SMichael Tuexen 			cnt_hb_sent++;
823ca85e948SMichael Tuexen 		}
824ca85e948SMichael Tuexen 	}
825ca85e948SMichael Tuexen 	if (cnt_hb_sent) {
826ca85e948SMichael Tuexen 		sctp_chunk_output(stcb->sctp_ep, stcb,
827ca85e948SMichael Tuexen 		    SCTP_OUTPUT_FROM_COOKIE_ACK,
828ca85e948SMichael Tuexen 		    SCTP_SO_NOT_LOCKED);
829ca85e948SMichael Tuexen 	}
830ca85e948SMichael Tuexen }
831ca85e948SMichael Tuexen 
832ca85e948SMichael Tuexen static void
833f8829a4aSRandall Stewart sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
834f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
835f8829a4aSRandall Stewart {
836f8829a4aSRandall Stewart 	struct sctp_association *asoc;
837f8829a4aSRandall Stewart 	int some_on_streamwheel;
838aa1cfca9SMichael Tuexen 	int old_state;
839ceaad40aSRandall Stewart 
840ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
841ad81507eSRandall Stewart 	    "sctp_handle_shutdown: handling SHUTDOWN\n");
842f8829a4aSRandall Stewart 	if (stcb == NULL)
843f8829a4aSRandall Stewart 		return;
844a5d547adSRandall Stewart 	asoc = &stcb->asoc;
845839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
846839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
847f8829a4aSRandall Stewart 		return;
848f8829a4aSRandall Stewart 	}
849f8829a4aSRandall Stewart 	if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
850f8829a4aSRandall Stewart 		/* Shutdown NOT the expected size */
851f8829a4aSRandall Stewart 		return;
852aa1cfca9SMichael Tuexen 	}
853839d21d6SMichael Tuexen 	old_state = SCTP_GET_STATE(stcb);
8547215cc1bSMichael Tuexen 	sctp_update_acked(stcb, cp, abort_flag);
8557e6206afSMichael Tuexen 	if (*abort_flag) {
8567e6206afSMichael Tuexen 		return;
8577e6206afSMichael Tuexen 	}
858a5d547adSRandall Stewart 	if (asoc->control_pdapi) {
859f8829a4aSRandall Stewart 		/*
860f8829a4aSRandall Stewart 		 * With a normal shutdown we assume the end of last record.
861f8829a4aSRandall Stewart 		 */
862f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
86344249214SRandall Stewart 		if (asoc->control_pdapi->on_strm_q) {
86444249214SRandall Stewart 			struct sctp_stream_in *strm;
86544249214SRandall Stewart 
86644249214SRandall Stewart 			strm = &asoc->strmin[asoc->control_pdapi->sinfo_stream];
86744249214SRandall Stewart 			if (asoc->control_pdapi->on_strm_q == SCTP_ON_UNORDERED) {
86844249214SRandall Stewart 				/* Unordered */
86944249214SRandall Stewart 				TAILQ_REMOVE(&strm->uno_inqueue, asoc->control_pdapi, next_instrm);
87044249214SRandall Stewart 				asoc->control_pdapi->on_strm_q = 0;
87144249214SRandall Stewart 			} else if (asoc->control_pdapi->on_strm_q == SCTP_ON_ORDERED) {
87244249214SRandall Stewart 				/* Ordered */
87344249214SRandall Stewart 				TAILQ_REMOVE(&strm->inqueue, asoc->control_pdapi, next_instrm);
87444249214SRandall Stewart 				asoc->control_pdapi->on_strm_q = 0;
87598d5fd97SMichael Tuexen #ifdef INVARIANTS
87644249214SRandall Stewart 			} else {
87744249214SRandall Stewart 				panic("Unknown state on ctrl:%p on_strm_q:%d",
87844249214SRandall Stewart 				    asoc->control_pdapi,
87944249214SRandall Stewart 				    asoc->control_pdapi->on_strm_q);
88098d5fd97SMichael Tuexen #endif
88144249214SRandall Stewart 			}
88244249214SRandall Stewart 		}
883a5d547adSRandall Stewart 		asoc->control_pdapi->end_added = 1;
884a5d547adSRandall Stewart 		asoc->control_pdapi->pdapi_aborted = 1;
885a5d547adSRandall Stewart 		asoc->control_pdapi = NULL;
886f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
887828318e1SMichael Tuexen 		if (stcb->sctp_socket) {
88850cec919SRandall Stewart 			sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
889828318e1SMichael Tuexen 		}
890f8829a4aSRandall Stewart 	}
891f8829a4aSRandall Stewart 	/* goto SHUTDOWN_RECEIVED state to block new requests */
892f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
893839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
894839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
895839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT)) {
896839d21d6SMichael Tuexen 			SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_RECEIVED);
897b7b84c0eSMichael Tuexen 			/*
898b7b84c0eSMichael Tuexen 			 * notify upper layer that peer has initiated a
899b7b84c0eSMichael Tuexen 			 * shutdown
900b7b84c0eSMichael Tuexen 			 */
901ceaad40aSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
902f8829a4aSRandall Stewart 
903f8829a4aSRandall Stewart 			/* reset time */
9046e55db54SRandall Stewart 			(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
905f8829a4aSRandall Stewart 		}
906f8829a4aSRandall Stewart 	}
907839d21d6SMichael Tuexen 	if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) {
908f8829a4aSRandall Stewart 		/*
909f8829a4aSRandall Stewart 		 * stop the shutdown timer, since we WILL move to
910f8829a4aSRandall Stewart 		 * SHUTDOWN-ACK-SENT.
911f8829a4aSRandall Stewart 		 */
912b7d130beSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
913b7d130beSMichael Tuexen 		    net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
914f8829a4aSRandall Stewart 	}
9155bead436SRandall Stewart 	/* Now is there unsent data on a stream somewhere? */
916689e6a5fSMichael Tuexen 	some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
917f8829a4aSRandall Stewart 
918f8829a4aSRandall Stewart 	if (!TAILQ_EMPTY(&asoc->send_queue) ||
919f8829a4aSRandall Stewart 	    !TAILQ_EMPTY(&asoc->sent_queue) ||
920f8829a4aSRandall Stewart 	    some_on_streamwheel) {
921f8829a4aSRandall Stewart 		/* By returning we will push more data out */
922f8829a4aSRandall Stewart 		return;
923f8829a4aSRandall Stewart 	} else {
924f8829a4aSRandall Stewart 		/* no outstanding data to send, so move on... */
925f8829a4aSRandall Stewart 		/* send SHUTDOWN-ACK */
926f8829a4aSRandall Stewart 		/* move to SHUTDOWN-ACK-SENT state */
927839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
928839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
929f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
930f8829a4aSRandall Stewart 		}
931839d21d6SMichael Tuexen 		if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
932839d21d6SMichael Tuexen 			SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_ACK_SENT);
93312af6654SMichael Tuexen 			sctp_stop_timers_for_shutdown(stcb);
934c39cfa1fSMichael Tuexen 			sctp_send_shutdown_ack(stcb, net);
935aa1cfca9SMichael Tuexen 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
936aa1cfca9SMichael Tuexen 			    stcb->sctp_ep, stcb, net);
937aa1cfca9SMichael Tuexen 		} else if (old_state == SCTP_STATE_SHUTDOWN_ACK_SENT) {
938aa1cfca9SMichael Tuexen 			sctp_send_shutdown_ack(stcb, net);
939aa1cfca9SMichael Tuexen 		}
940f8829a4aSRandall Stewart 	}
941f8829a4aSRandall Stewart }
942f8829a4aSRandall Stewart 
943f8829a4aSRandall Stewart static void
9447215cc1bSMichael Tuexen sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
9457b470fc3SMichael Tuexen     struct sctp_tcb *stcb,
9467b470fc3SMichael Tuexen     struct sctp_nets *net)
947f8829a4aSRandall Stewart {
948f8829a4aSRandall Stewart 	struct sctp_association *asoc;
949ceaad40aSRandall Stewart 
950ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
951ad81507eSRandall Stewart 	    "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
952f8829a4aSRandall Stewart 	if (stcb == NULL)
953f8829a4aSRandall Stewart 		return;
954f8829a4aSRandall Stewart 
955f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
956f8829a4aSRandall Stewart 	/* process according to association state */
957839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
958839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
9597b470fc3SMichael Tuexen 		/* unexpected SHUTDOWN-ACK... do OOTB handling... */
9607b470fc3SMichael Tuexen 		sctp_send_shutdown_complete(stcb, net, 1);
9617b470fc3SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
9627b470fc3SMichael Tuexen 		return;
9637b470fc3SMichael Tuexen 	}
964839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
965839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
966f8829a4aSRandall Stewart 		/* unexpected SHUTDOWN-ACK... so ignore... */
967f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
968f8829a4aSRandall Stewart 		return;
969f8829a4aSRandall Stewart 	}
970a5d547adSRandall Stewart 	if (asoc->control_pdapi) {
971f8829a4aSRandall Stewart 		/*
972f8829a4aSRandall Stewart 		 * With a normal shutdown we assume the end of last record.
973f8829a4aSRandall Stewart 		 */
974f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
975a5d547adSRandall Stewart 		asoc->control_pdapi->end_added = 1;
976a5d547adSRandall Stewart 		asoc->control_pdapi->pdapi_aborted = 1;
977a5d547adSRandall Stewart 		asoc->control_pdapi = NULL;
978f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
97950cec919SRandall Stewart 		sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
980f8829a4aSRandall Stewart 	}
98156f778aaSMichael Tuexen #ifdef INVARIANTS
982f8829a4aSRandall Stewart 	if (!TAILQ_EMPTY(&asoc->send_queue) ||
983f8829a4aSRandall Stewart 	    !TAILQ_EMPTY(&asoc->sent_queue) ||
984124d851aSMichael Tuexen 	    sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
98556f778aaSMichael Tuexen 		panic("Queues are not empty when handling SHUTDOWN-ACK");
986f8829a4aSRandall Stewart 	}
98756f778aaSMichael Tuexen #endif
988f8829a4aSRandall Stewart 	/* stop the timer */
989b7d130beSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net,
990b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
991f8829a4aSRandall Stewart 	/* send SHUTDOWN-COMPLETE */
9927b470fc3SMichael Tuexen 	sctp_send_shutdown_complete(stcb, net, 0);
993f8829a4aSRandall Stewart 	/* notify upper layer protocol */
994f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
995f8829a4aSRandall Stewart 		if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
996f8829a4aSRandall Stewart 		    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
9974e88d37aSMichael Tuexen 			stcb->sctp_socket->so_snd.sb_cc = 0;
998f8829a4aSRandall Stewart 		}
999c75fdd30SMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
1000f8829a4aSRandall Stewart 	}
1001f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
1002f8829a4aSRandall Stewart 	/* free the TCB but first save off the ep */
1003c4739e2fSRandall Stewart 	(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1004b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1005f8829a4aSRandall Stewart }
1006f8829a4aSRandall Stewart 
1007f8829a4aSRandall Stewart static void
10086fb7b4fbSMichael Tuexen sctp_process_unrecog_chunk(struct sctp_tcb *stcb, uint8_t chunk_type)
1009f8829a4aSRandall Stewart {
10103e87bccdSMichael Tuexen 	switch (chunk_type) {
1011f8829a4aSRandall Stewart 	case SCTP_ASCONF_ACK:
1012f8829a4aSRandall Stewart 	case SCTP_ASCONF:
10136fb7b4fbSMichael Tuexen 		sctp_asconf_cleanup(stcb);
1014f8829a4aSRandall Stewart 		break;
101544249214SRandall Stewart 	case SCTP_IFORWARD_CUM_TSN:
1016f8829a4aSRandall Stewart 	case SCTP_FORWARD_CUM_TSN:
1017dd973b0eSMichael Tuexen 		stcb->asoc.prsctp_supported = 0;
1018f8829a4aSRandall Stewart 		break;
1019f8829a4aSRandall Stewart 	default:
1020ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
10213e87bccdSMichael Tuexen 		    "Peer does not support chunk type %d (0x%x).\n",
10223e87bccdSMichael Tuexen 		    chunk_type, chunk_type);
1023f8829a4aSRandall Stewart 		break;
1024f8829a4aSRandall Stewart 	}
1025f8829a4aSRandall Stewart }
1026f8829a4aSRandall Stewart 
1027f8829a4aSRandall Stewart /*
1028f8829a4aSRandall Stewart  * Skip past the param header and then we will find the param that caused the
1029f8829a4aSRandall Stewart  * problem.  There are a number of param's in a ASCONF OR the prsctp param
1030f8829a4aSRandall Stewart  * these will turn of specific features.
1031c79bec9cSMichael Tuexen  * XXX: Is this the right thing to do?
1032f8829a4aSRandall Stewart  */
1033f8829a4aSRandall Stewart static void
10343e87bccdSMichael Tuexen sctp_process_unrecog_param(struct sctp_tcb *stcb, uint16_t parameter_type)
1035f8829a4aSRandall Stewart {
10363e87bccdSMichael Tuexen 	switch (parameter_type) {
1037f8829a4aSRandall Stewart 		/* pr-sctp draft */
1038f8829a4aSRandall Stewart 	case SCTP_PRSCTP_SUPPORTED:
1039dd973b0eSMichael Tuexen 		stcb->asoc.prsctp_supported = 0;
1040f8829a4aSRandall Stewart 		break;
1041f8829a4aSRandall Stewart 	case SCTP_SUPPORTED_CHUNK_EXT:
1042f8829a4aSRandall Stewart 		break;
1043f8829a4aSRandall Stewart 		/* draft-ietf-tsvwg-addip-sctp */
1044830d754dSRandall Stewart 	case SCTP_HAS_NAT_SUPPORT:
1045830d754dSRandall Stewart 		stcb->asoc.peer_supports_nat = 0;
1046830d754dSRandall Stewart 		break;
1047f8829a4aSRandall Stewart 	case SCTP_ADD_IP_ADDRESS:
1048f8829a4aSRandall Stewart 	case SCTP_DEL_IP_ADDRESS:
1049f8829a4aSRandall Stewart 	case SCTP_SET_PRIM_ADDR:
1050c79bec9cSMichael Tuexen 		stcb->asoc.asconf_supported = 0;
1051f8829a4aSRandall Stewart 		break;
1052f8829a4aSRandall Stewart 	case SCTP_SUCCESS_REPORT:
1053f8829a4aSRandall Stewart 	case SCTP_ERROR_CAUSE_IND:
1054ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1055ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
1056ad81507eSRandall Stewart 		    "Turning off ASCONF to this strange peer\n");
1057c79bec9cSMichael Tuexen 		stcb->asoc.asconf_supported = 0;
1058f8829a4aSRandall Stewart 		break;
1059f8829a4aSRandall Stewart 	default:
1060ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
10613e87bccdSMichael Tuexen 		    "Peer does not support param type %d (0x%x)??\n",
10623e87bccdSMichael Tuexen 		    parameter_type, parameter_type);
1063f8829a4aSRandall Stewart 		break;
1064f8829a4aSRandall Stewart 	}
1065f8829a4aSRandall Stewart }
1066f8829a4aSRandall Stewart 
1067f8829a4aSRandall Stewart static int
1068f8829a4aSRandall Stewart sctp_handle_error(struct sctp_chunkhdr *ch,
10693e87bccdSMichael Tuexen     struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
1070f8829a4aSRandall Stewart {
10713e87bccdSMichael Tuexen 	struct sctp_error_cause *cause;
1072f8829a4aSRandall Stewart 	struct sctp_association *asoc;
10733e87bccdSMichael Tuexen 	uint32_t remaining_length, adjust;
10743e87bccdSMichael Tuexen 	uint16_t code, cause_code, cause_length;
1075ceaad40aSRandall Stewart 
1076f8829a4aSRandall Stewart 	/* parse through all of the errors and process */
1077f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
10783e87bccdSMichael Tuexen 	cause = (struct sctp_error_cause *)((caddr_t)ch +
1079f8829a4aSRandall Stewart 	    sizeof(struct sctp_chunkhdr));
10803e87bccdSMichael Tuexen 	remaining_length = ntohs(ch->chunk_length);
10813e87bccdSMichael Tuexen 	if (remaining_length > limit) {
10823e87bccdSMichael Tuexen 		remaining_length = limit;
10833e87bccdSMichael Tuexen 	}
10843e87bccdSMichael Tuexen 	if (remaining_length >= sizeof(struct sctp_chunkhdr)) {
10853e87bccdSMichael Tuexen 		remaining_length -= sizeof(struct sctp_chunkhdr);
10863e87bccdSMichael Tuexen 	} else {
10873e87bccdSMichael Tuexen 		remaining_length = 0;
10883e87bccdSMichael Tuexen 	}
10893e87bccdSMichael Tuexen 	code = 0;
10903e87bccdSMichael Tuexen 	while (remaining_length >= sizeof(struct sctp_error_cause)) {
1091f8829a4aSRandall Stewart 		/* Process an Error Cause */
10923e87bccdSMichael Tuexen 		cause_code = ntohs(cause->code);
10933e87bccdSMichael Tuexen 		cause_length = ntohs(cause->length);
10943e87bccdSMichael Tuexen 		if ((cause_length > remaining_length) || (cause_length == 0)) {
10953e87bccdSMichael Tuexen 			/* Invalid cause length, possibly due to truncation. */
10963e87bccdSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in cause - bytes left: %u cause length: %u\n",
10973e87bccdSMichael Tuexen 			    remaining_length, cause_length);
1098f8829a4aSRandall Stewart 			return (0);
1099f8829a4aSRandall Stewart 		}
11003e87bccdSMichael Tuexen 		if (code == 0) {
1101389b1b11SMichael Tuexen 			/* report the first error cause */
11023e87bccdSMichael Tuexen 			code = cause_code;
1103389b1b11SMichael Tuexen 		}
11043e87bccdSMichael Tuexen 		switch (cause_code) {
1105f8829a4aSRandall Stewart 		case SCTP_CAUSE_INVALID_STREAM:
1106f8829a4aSRandall Stewart 		case SCTP_CAUSE_MISSING_PARAM:
1107f8829a4aSRandall Stewart 		case SCTP_CAUSE_INVALID_PARAM:
1108f8829a4aSRandall Stewart 		case SCTP_CAUSE_NO_USER_DATA:
11093e87bccdSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %u back? We have a bug :/ (or do they?)\n",
11103e87bccdSMichael Tuexen 			    cause_code);
1111f8829a4aSRandall Stewart 			break;
1112830d754dSRandall Stewart 		case SCTP_CAUSE_NAT_COLLIDING_STATE:
1113504ee6a0SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state, ERROR flags: %x\n",
1114830d754dSRandall Stewart 			    ch->chunk_flags);
1115830d754dSRandall Stewart 			if (sctp_handle_nat_colliding_state(stcb)) {
1116830d754dSRandall Stewart 				return (0);
1117830d754dSRandall Stewart 			}
1118830d754dSRandall Stewart 			break;
1119830d754dSRandall Stewart 		case SCTP_CAUSE_NAT_MISSING_STATE:
1120504ee6a0SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state, ERROR flags: %x\n",
1121830d754dSRandall Stewart 			    ch->chunk_flags);
1122830d754dSRandall Stewart 			if (sctp_handle_nat_missing_state(stcb, net)) {
1123830d754dSRandall Stewart 				return (0);
1124830d754dSRandall Stewart 			}
1125830d754dSRandall Stewart 			break;
1126f8829a4aSRandall Stewart 		case SCTP_CAUSE_STALE_COOKIE:
1127f8829a4aSRandall Stewart 			/*
1128f8829a4aSRandall Stewart 			 * We only act if we have echoed a cookie and are
1129f8829a4aSRandall Stewart 			 * waiting.
1130f8829a4aSRandall Stewart 			 */
11313e87bccdSMichael Tuexen 			if ((cause_length >= sizeof(struct sctp_error_stale_cookie)) &&
1132839d21d6SMichael Tuexen 			    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
11333e87bccdSMichael Tuexen 				struct sctp_error_stale_cookie *stale_cookie;
1134f8829a4aSRandall Stewart 
11353e87bccdSMichael Tuexen 				stale_cookie = (struct sctp_error_stale_cookie *)cause;
1136a92d5016SMichael Tuexen 				/* stable_time is in usec, convert to msec. */
1137a92d5016SMichael Tuexen 				asoc->cookie_preserve_req = ntohl(stale_cookie->stale_time) / 1000;
1138a92d5016SMichael Tuexen 				/* Double it to be more robust on RTX. */
11393e87bccdSMichael Tuexen 				asoc->cookie_preserve_req *= 2;
1140f8829a4aSRandall Stewart 				asoc->stale_cookie_count++;
1141f8829a4aSRandall Stewart 				if (asoc->stale_cookie_count >
1142f8829a4aSRandall Stewart 				    asoc->max_init_times) {
1143*105b68b4SMichael Tuexen 					sctp_abort_notification(stcb, false, true, 0, NULL, SCTP_SO_NOT_LOCKED);
1144f8829a4aSRandall Stewart 					/* now free the asoc */
1145c4739e2fSRandall Stewart 					(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1146b7d130beSMichael Tuexen 					    SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1147f8829a4aSRandall Stewart 					return (-1);
1148f8829a4aSRandall Stewart 				}
1149f8829a4aSRandall Stewart 				/* blast back to INIT state */
1150830d754dSRandall Stewart 				sctp_toss_old_cookies(stcb, &stcb->asoc);
1151839d21d6SMichael Tuexen 				SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
1152f8829a4aSRandall Stewart 				sctp_stop_all_cookie_timers(stcb);
1153ceaad40aSRandall Stewart 				sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1154f8829a4aSRandall Stewart 			}
1155f8829a4aSRandall Stewart 			break;
1156f8829a4aSRandall Stewart 		case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1157f8829a4aSRandall Stewart 			/*
1158f8829a4aSRandall Stewart 			 * Nothing we can do here, we don't do hostname
1159f8829a4aSRandall Stewart 			 * addresses so if the peer does not like my IPv6
1160f8829a4aSRandall Stewart 			 * (or IPv4 for that matter) it does not matter. If
1161f8829a4aSRandall Stewart 			 * they don't support that type of address, they can
1162f8829a4aSRandall Stewart 			 * NOT possibly get that packet type... i.e. with no
1163cd0a4ff6SPedro F. Giffuni 			 * IPv6 you can't receive a IPv6 packet. so we can
1164f8829a4aSRandall Stewart 			 * safely ignore this one. If we ever added support
1165f8829a4aSRandall Stewart 			 * for HOSTNAME Addresses, then we would need to do
1166f8829a4aSRandall Stewart 			 * something here.
1167f8829a4aSRandall Stewart 			 */
1168f8829a4aSRandall Stewart 			break;
1169f8829a4aSRandall Stewart 		case SCTP_CAUSE_UNRECOG_CHUNK:
11703e87bccdSMichael Tuexen 			if (cause_length >= sizeof(struct sctp_error_unrecognized_chunk)) {
11713e87bccdSMichael Tuexen 				struct sctp_error_unrecognized_chunk *unrec_chunk;
11723e87bccdSMichael Tuexen 
11733e87bccdSMichael Tuexen 				unrec_chunk = (struct sctp_error_unrecognized_chunk *)cause;
11746fb7b4fbSMichael Tuexen 				sctp_process_unrecog_chunk(stcb, unrec_chunk->ch.chunk_type);
11753e87bccdSMichael Tuexen 			}
1176f8829a4aSRandall Stewart 			break;
1177f8829a4aSRandall Stewart 		case SCTP_CAUSE_UNRECOG_PARAM:
11783e87bccdSMichael Tuexen 			/* XXX: We only consider the first parameter */
11793e87bccdSMichael Tuexen 			if (cause_length >= sizeof(struct sctp_error_cause) + sizeof(struct sctp_paramhdr)) {
11803e87bccdSMichael Tuexen 				struct sctp_paramhdr *unrec_parameter;
11813e87bccdSMichael Tuexen 
11823e87bccdSMichael Tuexen 				unrec_parameter = (struct sctp_paramhdr *)(cause + 1);
11833e87bccdSMichael Tuexen 				sctp_process_unrecog_param(stcb, ntohs(unrec_parameter->param_type));
11843e87bccdSMichael Tuexen 			}
1185f8829a4aSRandall Stewart 			break;
1186f8829a4aSRandall Stewart 		case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1187f8829a4aSRandall Stewart 			/*
1188f8829a4aSRandall Stewart 			 * We ignore this since the timer will drive out a
1189f8829a4aSRandall Stewart 			 * new cookie anyway and there timer will drive us
1190f8829a4aSRandall Stewart 			 * to send a SHUTDOWN_COMPLETE. We can't send one
1191f8829a4aSRandall Stewart 			 * here since we don't have their tag.
1192f8829a4aSRandall Stewart 			 */
1193f8829a4aSRandall Stewart 			break;
1194f8829a4aSRandall Stewart 		case SCTP_CAUSE_DELETING_LAST_ADDR:
1195f8829a4aSRandall Stewart 		case SCTP_CAUSE_RESOURCE_SHORTAGE:
1196f8829a4aSRandall Stewart 		case SCTP_CAUSE_DELETING_SRC_ADDR:
1197f8829a4aSRandall Stewart 			/*
1198f8829a4aSRandall Stewart 			 * We should NOT get these here, but in a
119993164cf9SRandall Stewart 			 * ASCONF-ACK.
1200f8829a4aSRandall Stewart 			 */
12013e87bccdSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a error cause with code %u.\n",
12023e87bccdSMichael Tuexen 			    cause_code);
1203f8829a4aSRandall Stewart 			break;
1204f8829a4aSRandall Stewart 		case SCTP_CAUSE_OUT_OF_RESC:
1205f8829a4aSRandall Stewart 			/*
1206f8829a4aSRandall Stewart 			 * And what, pray tell do we do with the fact that
1207f8829a4aSRandall Stewart 			 * the peer is out of resources? Not really sure we
120893164cf9SRandall Stewart 			 * could do anything but abort. I suspect this
1209f8829a4aSRandall Stewart 			 * should have came WITH an abort instead of in a
1210f8829a4aSRandall Stewart 			 * OP-ERROR.
1211f8829a4aSRandall Stewart 			 */
1212f8829a4aSRandall Stewart 			break;
1213f8829a4aSRandall Stewart 		default:
12143e87bccdSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown code 0x%x\n",
12153e87bccdSMichael Tuexen 			    cause_code);
1216f8829a4aSRandall Stewart 			break;
1217f8829a4aSRandall Stewart 		}
12183e87bccdSMichael Tuexen 		adjust = SCTP_SIZE32(cause_length);
12193e87bccdSMichael Tuexen 		if (remaining_length >= adjust) {
12203e87bccdSMichael Tuexen 			remaining_length -= adjust;
12213e87bccdSMichael Tuexen 		} else {
12223e87bccdSMichael Tuexen 			remaining_length = 0;
1223f8829a4aSRandall Stewart 		}
12243e87bccdSMichael Tuexen 		cause = (struct sctp_error_cause *)((caddr_t)cause + adjust);
12253e87bccdSMichael Tuexen 	}
12263e87bccdSMichael Tuexen 	sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, code, ch, SCTP_SO_NOT_LOCKED);
1227f8829a4aSRandall Stewart 	return (0);
1228f8829a4aSRandall Stewart }
1229f8829a4aSRandall Stewart 
1230f8829a4aSRandall Stewart static int
1231b1754ad1SMichael Tuexen sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1232b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
1233f30ac432SMichael Tuexen     struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1234f30ac432SMichael Tuexen     struct sctp_nets *net, int *abort_no_unlock,
1235457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
1236f30ac432SMichael Tuexen     uint32_t vrf_id)
1237f8829a4aSRandall Stewart {
1238f8829a4aSRandall Stewart 	struct sctp_init_ack *init_ack;
1239f8829a4aSRandall Stewart 	struct mbuf *op_err;
1240f8829a4aSRandall Stewart 
1241ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
1242ad81507eSRandall Stewart 	    "sctp_handle_init_ack: handling INIT-ACK\n");
1243ad81507eSRandall Stewart 
1244f8829a4aSRandall Stewart 	if (stcb == NULL) {
1245ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
1246ad81507eSRandall Stewart 		    "sctp_handle_init_ack: TCB is null\n");
1247f8829a4aSRandall Stewart 		return (-1);
1248f8829a4aSRandall Stewart 	}
1249f8829a4aSRandall Stewart 	init_ack = &cp->init;
1250059ec222SMichael Tuexen 	/* Validate parameters. */
1251059ec222SMichael Tuexen 	if ((ntohl(init_ack->initiate_tag) == 0) ||
1252059ec222SMichael Tuexen 	    (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) ||
1253059ec222SMichael Tuexen 	    (ntohs(init_ack->num_inbound_streams) == 0) ||
1254059ec222SMichael Tuexen 	    (ntohs(init_ack->num_outbound_streams) == 0)) {
1255f8829a4aSRandall Stewart 		/* protocol error... send an abort */
1256ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1257b1754ad1SMichael Tuexen 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1258b1754ad1SMichael Tuexen 		    src, dst, sh, op_err,
1259457b4b88SMichael Tuexen 		    mflowtype, mflowid,
1260f30ac432SMichael Tuexen 		    vrf_id, net->port);
1261bff64a4dSRandall Stewart 		*abort_no_unlock = 1;
1262f8829a4aSRandall Stewart 		return (-1);
1263f8829a4aSRandall Stewart 	}
1264f8829a4aSRandall Stewart 	/* process according to association state... */
1265839d21d6SMichael Tuexen 	switch (SCTP_GET_STATE(stcb)) {
1266f8829a4aSRandall Stewart 	case SCTP_STATE_COOKIE_WAIT:
1267f8829a4aSRandall Stewart 		/* this is the expected state for this chunk */
1268f8829a4aSRandall Stewart 		/* process the INIT-ACK parameters */
1269f8829a4aSRandall Stewart 		if (stcb->asoc.primary_destination->dest_state &
1270f8829a4aSRandall Stewart 		    SCTP_ADDR_UNCONFIRMED) {
1271f8829a4aSRandall Stewart 			/*
1272f8829a4aSRandall Stewart 			 * The primary is where we sent the INIT, we can
1273f8829a4aSRandall Stewart 			 * always consider it confirmed when the INIT-ACK is
1274f8829a4aSRandall Stewart 			 * returned. Do this before we load addresses
1275f8829a4aSRandall Stewart 			 * though.
1276f8829a4aSRandall Stewart 			 */
1277f8829a4aSRandall Stewart 			stcb->asoc.primary_destination->dest_state &=
1278f8829a4aSRandall Stewart 			    ~SCTP_ADDR_UNCONFIRMED;
1279f8829a4aSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1280ceaad40aSRandall Stewart 			    stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1281f8829a4aSRandall Stewart 		}
1282b1754ad1SMichael Tuexen 		if (sctp_process_init_ack(m, iphlen, offset, src, dst, sh, cp, stcb,
1283f30ac432SMichael Tuexen 		    net, abort_no_unlock,
1284457b4b88SMichael Tuexen 		    mflowtype, mflowid,
1285f30ac432SMichael Tuexen 		    vrf_id) < 0) {
1286f8829a4aSRandall Stewart 			/* error in parsing parameters */
1287f8829a4aSRandall Stewart 			return (-1);
1288f8829a4aSRandall Stewart 		}
1289f8829a4aSRandall Stewart 		/* update our state */
1290ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1291839d21d6SMichael Tuexen 		SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_ECHOED);
1292f8829a4aSRandall Stewart 
1293f8829a4aSRandall Stewart 		/* reset the RTO calc */
1294b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1295c4739e2fSRandall Stewart 			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1296c4739e2fSRandall Stewart 			    stcb->asoc.overall_error_count,
1297c4739e2fSRandall Stewart 			    0,
1298c4739e2fSRandall Stewart 			    SCTP_FROM_SCTP_INPUT,
1299c4739e2fSRandall Stewart 			    __LINE__);
1300c4739e2fSRandall Stewart 		}
1301f8829a4aSRandall Stewart 		stcb->asoc.overall_error_count = 0;
13026e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1303f8829a4aSRandall Stewart 		/*
1304f8829a4aSRandall Stewart 		 * collapse the init timer back in case of a exponential
1305a5d547adSRandall Stewart 		 * backoff
1306f8829a4aSRandall Stewart 		 */
1307f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1308f8829a4aSRandall Stewart 		    stcb, net);
1309f8829a4aSRandall Stewart 		/*
1310f8829a4aSRandall Stewart 		 * the send at the end of the inbound data processing will
1311f8829a4aSRandall Stewart 		 * cause the cookie to be sent
1312f8829a4aSRandall Stewart 		 */
1313f8829a4aSRandall Stewart 		break;
1314f8829a4aSRandall Stewart 	case SCTP_STATE_SHUTDOWN_SENT:
1315f8829a4aSRandall Stewart 		/* incorrect state... discard */
1316f8829a4aSRandall Stewart 		break;
1317f8829a4aSRandall Stewart 	case SCTP_STATE_COOKIE_ECHOED:
1318f8829a4aSRandall Stewart 		/* incorrect state... discard */
1319f8829a4aSRandall Stewart 		break;
1320f8829a4aSRandall Stewart 	case SCTP_STATE_OPEN:
1321f8829a4aSRandall Stewart 		/* incorrect state... discard */
1322f8829a4aSRandall Stewart 		break;
1323f8829a4aSRandall Stewart 	case SCTP_STATE_EMPTY:
1324f8829a4aSRandall Stewart 	case SCTP_STATE_INUSE:
1325f8829a4aSRandall Stewart 	default:
1326f8829a4aSRandall Stewart 		/* incorrect state... discard */
1327f8829a4aSRandall Stewart 		return (-1);
1328f8829a4aSRandall Stewart 		break;
1329f8829a4aSRandall Stewart 	}
1330ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1331f8829a4aSRandall Stewart 	return (0);
1332f8829a4aSRandall Stewart }
1333f8829a4aSRandall Stewart 
1334830d754dSRandall Stewart static struct sctp_tcb *
1335830d754dSRandall Stewart sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1336b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
1337830d754dSRandall Stewart     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1338830d754dSRandall Stewart     struct sctp_inpcb *inp, struct sctp_nets **netp,
1339830d754dSRandall Stewart     struct sockaddr *init_src, int *notification,
1340830d754dSRandall Stewart     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1341457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
1342830d754dSRandall Stewart     uint32_t vrf_id, uint16_t port);
1343830d754dSRandall Stewart 
1344f8829a4aSRandall Stewart /*
1345f8829a4aSRandall Stewart  * handle a state cookie for an existing association m: input packet mbuf
1346f8829a4aSRandall Stewart  * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1347f8829a4aSRandall Stewart  * "split" mbuf and the cookie signature does not exist offset: offset into
1348f8829a4aSRandall Stewart  * mbuf to the cookie-echo chunk
1349f8829a4aSRandall Stewart  */
1350f8829a4aSRandall Stewart static struct sctp_tcb *
1351f8829a4aSRandall Stewart sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1352b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
1353f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1354830d754dSRandall Stewart     struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
13557215cc1bSMichael Tuexen     struct sockaddr *init_src, int *notification,
1356f30ac432SMichael Tuexen     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1357457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
1358f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
1359f8829a4aSRandall Stewart {
1360f8829a4aSRandall Stewart 	struct sctp_association *asoc;
1361f8829a4aSRandall Stewart 	struct sctp_init_chunk *init_cp, init_buf;
1362f8829a4aSRandall Stewart 	struct sctp_init_ack_chunk *initack_cp, initack_buf;
1363aa6db9a0SMichael Tuexen 	struct sctp_asconf_addr *aparam, *naparam;
1364aa6db9a0SMichael Tuexen 	struct sctp_asconf_ack *aack, *naack;
1365aa6db9a0SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
1366aa6db9a0SMichael Tuexen 	struct sctp_stream_reset_list *strrst, *nstrrst;
1367aa6db9a0SMichael Tuexen 	struct sctp_queued_to_read *sq, *nsq;
1368830d754dSRandall Stewart 	struct sctp_nets *net;
1369830d754dSRandall Stewart 	struct mbuf *op_err;
13708c8e10b7SMichael Tuexen 	struct timeval old;
1371a5d547adSRandall Stewart 	int init_offset, initack_offset, i;
1372f8829a4aSRandall Stewart 	int retval;
13737f34832bSRandall Stewart 	int spec_flag = 0;
13744c9179adSRandall Stewart 	uint32_t how_indx;
1375f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
1376f0396ad1SMichael Tuexen 	int j;
1377f0396ad1SMichael Tuexen #endif
1378f0396ad1SMichael Tuexen 
1379830d754dSRandall Stewart 	net = *netp;
1380f8829a4aSRandall Stewart 	/* I know that the TCB is non-NULL from the caller */
1381f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
1382f42a358aSRandall Stewart 	for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
138344b7479bSRandall Stewart 		if (asoc->cookie_how[how_indx] == 0)
138444b7479bSRandall Stewart 			break;
138544b7479bSRandall Stewart 	}
138644b7479bSRandall Stewart 	if (how_indx < sizeof(asoc->cookie_how)) {
138744b7479bSRandall Stewart 		asoc->cookie_how[how_indx] = 1;
138844b7479bSRandall Stewart 	}
1389839d21d6SMichael Tuexen 	if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1390f8829a4aSRandall Stewart 		/* SHUTDOWN came in after sending INIT-ACK */
1391f8829a4aSRandall Stewart 		sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1392ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_COOKIE_IN_SHUTDOWN, "");
1393b1754ad1SMichael Tuexen 		sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
1394d089f9b9SMichael Tuexen 		    mflowtype, mflowid, inp->fibnum,
1395c54a18d2SRandall Stewart 		    vrf_id, net->port);
139644b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
139744b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 2;
139812dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1399f8829a4aSRandall Stewart 		return (NULL);
1400f8829a4aSRandall Stewart 	}
1401f8829a4aSRandall Stewart 	/*
1402f8829a4aSRandall Stewart 	 * find and validate the INIT chunk in the cookie (peer's info) the
1403f8829a4aSRandall Stewart 	 * INIT should start after the cookie-echo header struct (chunk
1404f8829a4aSRandall Stewart 	 * header, state cookie header struct)
1405f8829a4aSRandall Stewart 	 */
1406f8829a4aSRandall Stewart 	init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1407f8829a4aSRandall Stewart 
1408f8829a4aSRandall Stewart 	init_cp = (struct sctp_init_chunk *)
1409f8829a4aSRandall Stewart 	    sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1410f8829a4aSRandall Stewart 	    (uint8_t *)&init_buf);
1411f8829a4aSRandall Stewart 	if (init_cp == NULL) {
1412f8829a4aSRandall Stewart 		/* could not pull a INIT chunk in cookie */
141312dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1414f8829a4aSRandall Stewart 		return (NULL);
1415f8829a4aSRandall Stewart 	}
1416f8829a4aSRandall Stewart 	if (init_cp->ch.chunk_type != SCTP_INITIATION) {
141712dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1418f8829a4aSRandall Stewart 		return (NULL);
1419f8829a4aSRandall Stewart 	}
1420f8829a4aSRandall Stewart 	/*
1421f8829a4aSRandall Stewart 	 * find and validate the INIT-ACK chunk in the cookie (my info) the
1422f8829a4aSRandall Stewart 	 * INIT-ACK follows the INIT chunk
1423f8829a4aSRandall Stewart 	 */
142460990c0cSMichael Tuexen 	initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1425f8829a4aSRandall Stewart 	initack_cp = (struct sctp_init_ack_chunk *)
1426f8829a4aSRandall Stewart 	    sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1427f8829a4aSRandall Stewart 	    (uint8_t *)&initack_buf);
1428f8829a4aSRandall Stewart 	if (initack_cp == NULL) {
1429f8829a4aSRandall Stewart 		/* could not pull INIT-ACK chunk in cookie */
143012dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1431f8829a4aSRandall Stewart 		return (NULL);
1432f8829a4aSRandall Stewart 	}
1433f8829a4aSRandall Stewart 	if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
143412dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1435f8829a4aSRandall Stewart 		return (NULL);
1436f8829a4aSRandall Stewart 	}
1437f8829a4aSRandall Stewart 	if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1438f8829a4aSRandall Stewart 	    (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1439f8829a4aSRandall Stewart 		/*
1440f8829a4aSRandall Stewart 		 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1441f8829a4aSRandall Stewart 		 * to get into the OPEN state
1442f8829a4aSRandall Stewart 		 */
144344b7479bSRandall Stewart 		if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1444851b7298SRandall Stewart 			/*-
1445851b7298SRandall Stewart 			 * Opps, this means that we somehow generated two vtag's
1446851b7298SRandall Stewart 			 * the same. I.e. we did:
1447851b7298SRandall Stewart 			 *  Us               Peer
1448851b7298SRandall Stewart 			 *   <---INIT(tag=a)------
1449851b7298SRandall Stewart 			 *   ----INIT-ACK(tag=t)-->
1450851b7298SRandall Stewart 			 *   ----INIT(tag=t)------> *1
1451851b7298SRandall Stewart 			 *   <---INIT-ACK(tag=a)---
1452851b7298SRandall Stewart 			 *   <----CE(tag=t)------------- *2
1453851b7298SRandall Stewart 			 *
1454851b7298SRandall Stewart 			 * At point *1 we should be generating a different
1455851b7298SRandall Stewart 			 * tag t'. Which means we would throw away the CE and send
1456851b7298SRandall Stewart 			 * ours instead. Basically this is case C (throw away side).
1457851b7298SRandall Stewart 			 */
1458851b7298SRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
1459851b7298SRandall Stewart 				asoc->cookie_how[how_indx] = 17;
146012dda000SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
1461851b7298SRandall Stewart 			return (NULL);
146244b7479bSRandall Stewart 		}
1463839d21d6SMichael Tuexen 		switch (SCTP_GET_STATE(stcb)) {
1464f8829a4aSRandall Stewart 		case SCTP_STATE_COOKIE_WAIT:
146544b7479bSRandall Stewart 		case SCTP_STATE_COOKIE_ECHOED:
1466f8829a4aSRandall Stewart 			/*
146717205eccSRandall Stewart 			 * INIT was sent but got a COOKIE_ECHO with the
146844b7479bSRandall Stewart 			 * correct tags... just accept it...but we must
146944b7479bSRandall Stewart 			 * process the init so that we can make sure we have
147044b7479bSRandall Stewart 			 * the right seq no's.
1471f8829a4aSRandall Stewart 			 */
1472f8829a4aSRandall Stewart 			/* First we must process the INIT !! */
14735f2e1835SMichael Tuexen 			if (sctp_process_init(init_cp, stcb) < 0) {
147444b7479bSRandall Stewart 				if (how_indx < sizeof(asoc->cookie_how))
147544b7479bSRandall Stewart 					asoc->cookie_how[how_indx] = 3;
14765f2e1835SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
14775f2e1835SMichael Tuexen 				SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_init() failed\n");
14785f2e1835SMichael Tuexen 				sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
14795f2e1835SMichael Tuexen 				    src, dst, sh, op_err,
14805f2e1835SMichael Tuexen 				    mflowtype, mflowid,
14815f2e1835SMichael Tuexen 				    vrf_id, net->port);
1482f8829a4aSRandall Stewart 				return (NULL);
1483f8829a4aSRandall Stewart 			}
1484f8829a4aSRandall Stewart 			/* we have already processed the INIT so no problem */
1485b7d130beSMichael Tuexen 			sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp,
1486b7d130beSMichael Tuexen 			    stcb, net,
1487b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1488b7d130beSMichael Tuexen 			sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp,
1489b7d130beSMichael Tuexen 			    stcb, net,
1490b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1491f8829a4aSRandall Stewart 			/* update current state */
1492839d21d6SMichael Tuexen 			if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)
1493f42a358aSRandall Stewart 				SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1494f42a358aSRandall Stewart 			else
1495f42a358aSRandall Stewart 				SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1496c4739e2fSRandall Stewart 
1497839d21d6SMichael Tuexen 			SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1498f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1499f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
15006fb7b4fbSMichael Tuexen 				    stcb->sctp_ep, stcb, NULL);
1501f8829a4aSRandall Stewart 			}
1502f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1503a5d547adSRandall Stewart 			sctp_stop_all_cookie_timers(stcb);
1504f8829a4aSRandall Stewart 			if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1505f8829a4aSRandall Stewart 			    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
15065d08768aSMichael Tuexen 			    (!SCTP_IS_LISTENING(inp))) {
1507f8829a4aSRandall Stewart 				/*
1508f8829a4aSRandall Stewart 				 * Here is where collision would go if we
1509f8829a4aSRandall Stewart 				 * did a connect() and instead got a
1510f8829a4aSRandall Stewart 				 * init/init-ack/cookie done before the
1511f8829a4aSRandall Stewart 				 * init-ack came back..
1512f8829a4aSRandall Stewart 				 */
1513f8829a4aSRandall Stewart 				stcb->sctp_ep->sctp_flags |=
1514f8829a4aSRandall Stewart 				    SCTP_PCB_FLAGS_CONNECTED;
1515ceaad40aSRandall Stewart 				soisconnected(stcb->sctp_socket);
1516f8829a4aSRandall Stewart 			}
1517f8829a4aSRandall Stewart 			/* notify upper layer */
1518f8829a4aSRandall Stewart 			*notification = SCTP_NOTIFY_ASSOC_UP;
1519f8829a4aSRandall Stewart 			/*
1520f8829a4aSRandall Stewart 			 * since we did not send a HB make sure we don't
1521f8829a4aSRandall Stewart 			 * double things
1522f8829a4aSRandall Stewart 			 */
15238c8e10b7SMichael Tuexen 			old.tv_sec = cookie->time_entered.tv_sec;
15248c8e10b7SMichael Tuexen 			old.tv_usec = cookie->time_entered.tv_usec;
1525f8829a4aSRandall Stewart 			net->hb_responded = 1;
152644f2a327SMichael Tuexen 			sctp_calculate_rto(stcb, asoc, net, &old,
1527f79aab18SRandall Stewart 			    SCTP_RTT_FROM_NON_DATA);
1528f8829a4aSRandall Stewart 
1529f8829a4aSRandall Stewart 			if (stcb->asoc.sctp_autoclose_ticks &&
1530f8829a4aSRandall Stewart 			    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1531f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1532f8829a4aSRandall Stewart 				    inp, stcb, NULL);
1533f8829a4aSRandall Stewart 			}
1534f8829a4aSRandall Stewart 			break;
1535f8829a4aSRandall Stewart 		default:
1536f8829a4aSRandall Stewart 			/*
1537f8829a4aSRandall Stewart 			 * we're in the OPEN state (or beyond), so peer must
1538f8829a4aSRandall Stewart 			 * have simply lost the COOKIE-ACK
1539f8829a4aSRandall Stewart 			 */
1540f8829a4aSRandall Stewart 			break;
1541f8829a4aSRandall Stewart 		}		/* end switch */
1542a5d547adSRandall Stewart 		sctp_stop_all_cookie_timers(stcb);
1543655c200cSAlexander Motin 		if ((retval = sctp_load_addresses_from_init(stcb, m,
1544f8829a4aSRandall Stewart 		    init_offset + sizeof(struct sctp_init_chunk),
15455f2e1835SMichael Tuexen 		    initack_offset, src, dst, init_src, stcb->asoc.port)) < 0) {
154644b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
154744b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 4;
15485f2e1835SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
15495f2e1835SMichael Tuexen 			    "Problem with address parameters");
15505f2e1835SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1,
15515f2e1835SMichael Tuexen 			    "Load addresses from INIT causes an abort %d\n",
15525f2e1835SMichael Tuexen 			    retval);
15535f2e1835SMichael Tuexen 			sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
15545f2e1835SMichael Tuexen 			    src, dst, sh, op_err,
15555f2e1835SMichael Tuexen 			    mflowtype, mflowid,
15565f2e1835SMichael Tuexen 			    vrf_id, net->port);
1557f8829a4aSRandall Stewart 			return (NULL);
1558f8829a4aSRandall Stewart 		}
1559f8829a4aSRandall Stewart 		/* respond with a COOKIE-ACK */
1560a5d547adSRandall Stewart 		sctp_toss_old_cookies(stcb, asoc);
1561f8829a4aSRandall Stewart 		sctp_send_cookie_ack(stcb);
156244b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
156344b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 5;
1564f8829a4aSRandall Stewart 		return (stcb);
156517205eccSRandall Stewart 	}
15660053ed28SMichael Tuexen 
1567f8829a4aSRandall Stewart 	if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1568f8829a4aSRandall Stewart 	    ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1569f8829a4aSRandall Stewart 	    cookie->tie_tag_my_vtag == 0 &&
1570f8829a4aSRandall Stewart 	    cookie->tie_tag_peer_vtag == 0) {
1571f8829a4aSRandall Stewart 		/*
1572f8829a4aSRandall Stewart 		 * case C in Section 5.2.4 Table 2: XMOO silently discard
1573f8829a4aSRandall Stewart 		 */
157444b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
157544b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 6;
157612dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1577f8829a4aSRandall Stewart 		return (NULL);
1578f8829a4aSRandall Stewart 	}
1579830d754dSRandall Stewart 	/*
1580830d754dSRandall Stewart 	 * If nat support, and the below and stcb is established, send back
1581830d754dSRandall Stewart 	 * a ABORT(colliding state) if we are established.
1582830d754dSRandall Stewart 	 */
1583839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) &&
1584830d754dSRandall Stewart 	    (asoc->peer_supports_nat) &&
1585830d754dSRandall Stewart 	    ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1586830d754dSRandall Stewart 	    ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1587830d754dSRandall Stewart 	    (asoc->peer_vtag == 0)))) {
1588830d754dSRandall Stewart 		/*
1589830d754dSRandall Stewart 		 * Special case - Peer's support nat. We may have two init's
1590830d754dSRandall Stewart 		 * that we gave out the same tag on since one was not
1591830d754dSRandall Stewart 		 * established.. i.e. we get INIT from host-1 behind the nat
1592830d754dSRandall Stewart 		 * and we respond tag-a, we get a INIT from host-2 behind
1593830d754dSRandall Stewart 		 * the nat and we get tag-a again. Then we bring up host-1
1594830d754dSRandall Stewart 		 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1595830d754dSRandall Stewart 		 * Now we have colliding state. We must send an abort here
1596830d754dSRandall Stewart 		 * with colliding state indication.
1597830d754dSRandall Stewart 		 */
1598ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_NAT_COLLIDING_STATE, "");
1599b1754ad1SMichael Tuexen 		sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
1600d089f9b9SMichael Tuexen 		    mflowtype, mflowid, inp->fibnum,
1601f30ac432SMichael Tuexen 		    vrf_id, port);
160212dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1603830d754dSRandall Stewart 		return (NULL);
1604830d754dSRandall Stewart 	}
1605830d754dSRandall Stewart 	if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1606830d754dSRandall Stewart 	    ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1607830d754dSRandall Stewart 	    (asoc->peer_vtag == 0))) {
1608f8829a4aSRandall Stewart 		/*
1609f8829a4aSRandall Stewart 		 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1610f8829a4aSRandall Stewart 		 * should be ok, re-accept peer info
1611f8829a4aSRandall Stewart 		 */
161244b7479bSRandall Stewart 		if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
161344b7479bSRandall Stewart 			/*
161444b7479bSRandall Stewart 			 * Extension of case C. If we hit this, then the
161544b7479bSRandall Stewart 			 * random number generator returned the same vtag
161644b7479bSRandall Stewart 			 * when we first sent our INIT-ACK and when we later
161744b7479bSRandall Stewart 			 * sent our INIT. The side with the seq numbers that
161844b7479bSRandall Stewart 			 * are different will be the one that normnally
161944b7479bSRandall Stewart 			 * would have hit case C. This in effect "extends"
162044b7479bSRandall Stewart 			 * our vtags in this collision case to be 64 bits.
162144b7479bSRandall Stewart 			 * The same collision could occur aka you get both
162244b7479bSRandall Stewart 			 * vtag and seq number the same twice in a row.. but
162344b7479bSRandall Stewart 			 * is much less likely. If it did happen then we
162444b7479bSRandall Stewart 			 * would proceed through and bring up the assoc.. we
162544b7479bSRandall Stewart 			 * may end up with the wrong stream setup however..
162644b7479bSRandall Stewart 			 * which would be bad.. but there is no way to
162744b7479bSRandall Stewart 			 * tell.. until we send on a stream that does not
162844b7479bSRandall Stewart 			 * exist :-)
162944b7479bSRandall Stewart 			 */
163044b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
163144b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 7;
163244b7479bSRandall Stewart 
163312dda000SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
163444b7479bSRandall Stewart 			return (NULL);
163544b7479bSRandall Stewart 		}
163644b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
163744b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 8;
1638b7d130beSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1639b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1640f8829a4aSRandall Stewart 		sctp_stop_all_cookie_timers(stcb);
1641f8829a4aSRandall Stewart 		/*
1642f8829a4aSRandall Stewart 		 * since we did not send a HB make sure we don't double
1643f8829a4aSRandall Stewart 		 * things
1644f8829a4aSRandall Stewart 		 */
1645f8829a4aSRandall Stewart 		net->hb_responded = 1;
1646f8829a4aSRandall Stewart 		if (stcb->asoc.sctp_autoclose_ticks &&
1647f8829a4aSRandall Stewart 		    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1648f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1649f8829a4aSRandall Stewart 			    NULL);
1650f8829a4aSRandall Stewart 		}
1651f8829a4aSRandall Stewart 		asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
16520066de1cSMichael Tuexen 		if (asoc->pre_open_streams < asoc->streamoutcnt) {
16530066de1cSMichael Tuexen 			asoc->pre_open_streams = asoc->streamoutcnt;
16540066de1cSMichael Tuexen 		}
1655f8829a4aSRandall Stewart 
16567f34832bSRandall Stewart 		if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
16577f34832bSRandall Stewart 			/*
16587f34832bSRandall Stewart 			 * Ok the peer probably discarded our data (if we
16597f34832bSRandall Stewart 			 * echoed a cookie+data). So anything on the
16607f34832bSRandall Stewart 			 * sent_queue should be marked for retransmit, we
16617f34832bSRandall Stewart 			 * may not get something to kick us so it COULD
16627f34832bSRandall Stewart 			 * still take a timeout to move these.. but it can't
16637f34832bSRandall Stewart 			 * hurt to mark them.
16647f34832bSRandall Stewart 			 */
16657f34832bSRandall Stewart 
16667f34832bSRandall Stewart 			TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
16677f34832bSRandall Stewart 				if (chk->sent < SCTP_DATAGRAM_RESEND) {
16687f34832bSRandall Stewart 					chk->sent = SCTP_DATAGRAM_RESEND;
1669b54d3a6cSRandall Stewart 					sctp_flight_size_decrease(chk);
1670b54d3a6cSRandall Stewart 					sctp_total_flight_decrease(stcb, chk);
16715e54f665SRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
16727f34832bSRandall Stewart 					spec_flag++;
16737f34832bSRandall Stewart 				}
16747f34832bSRandall Stewart 			}
16757f34832bSRandall Stewart 		}
1676f8829a4aSRandall Stewart 		/* process the INIT info (peer's info) */
16775f2e1835SMichael Tuexen 		if (sctp_process_init(init_cp, stcb) < 0) {
167844b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
167944b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 9;
16805f2e1835SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
16815f2e1835SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_init() failed\n");
16825f2e1835SMichael Tuexen 			sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
16835f2e1835SMichael Tuexen 			    src, dst, sh, op_err,
16845f2e1835SMichael Tuexen 			    mflowtype, mflowid,
16855f2e1835SMichael Tuexen 			    vrf_id, net->port);
1686f8829a4aSRandall Stewart 			return (NULL);
1687f8829a4aSRandall Stewart 		}
16885f2e1835SMichael Tuexen 		if ((retval = sctp_load_addresses_from_init(stcb, m,
1689f8829a4aSRandall Stewart 		    init_offset + sizeof(struct sctp_init_chunk),
16905f2e1835SMichael Tuexen 		    initack_offset, src, dst, init_src, stcb->asoc.port)) < 0) {
169144b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
169244b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 10;
16935f2e1835SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
16945f2e1835SMichael Tuexen 			    "Problem with address parameters");
16955f2e1835SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1,
16965f2e1835SMichael Tuexen 			    "Load addresses from INIT causes an abort %d\n",
16975f2e1835SMichael Tuexen 			    retval);
16985f2e1835SMichael Tuexen 			sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
16995f2e1835SMichael Tuexen 			    src, dst, sh, op_err,
17005f2e1835SMichael Tuexen 			    mflowtype, mflowid,
17015f2e1835SMichael Tuexen 			    vrf_id, net->port);
1702f8829a4aSRandall Stewart 			return (NULL);
1703f8829a4aSRandall Stewart 		}
1704839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
1705839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
1706f8829a4aSRandall Stewart 			*notification = SCTP_NOTIFY_ASSOC_UP;
1707f8829a4aSRandall Stewart 
1708f8829a4aSRandall Stewart 			if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1709f8829a4aSRandall Stewart 			    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
17105d08768aSMichael Tuexen 			    (!SCTP_IS_LISTENING(inp))) {
1711c8e55b3cSMichael Tuexen 				stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1712ceaad40aSRandall Stewart 				soisconnected(stcb->sctp_socket);
1713f8829a4aSRandall Stewart 			}
1714839d21d6SMichael Tuexen 			if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)
1715f42a358aSRandall Stewart 				SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1716f42a358aSRandall Stewart 			else
1717f42a358aSRandall Stewart 				SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1718f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1719839d21d6SMichael Tuexen 		} else if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
1720f42a358aSRandall Stewart 			SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1721f42a358aSRandall Stewart 		} else {
1722f42a358aSRandall Stewart 			SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1723f8829a4aSRandall Stewart 		}
1724839d21d6SMichael Tuexen 		SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1725f8829a4aSRandall Stewart 		if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1726f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
17276fb7b4fbSMichael Tuexen 			    stcb->sctp_ep, stcb, NULL);
1728f8829a4aSRandall Stewart 		}
1729f8829a4aSRandall Stewart 		sctp_stop_all_cookie_timers(stcb);
1730a5d547adSRandall Stewart 		sctp_toss_old_cookies(stcb, asoc);
1731f8829a4aSRandall Stewart 		sctp_send_cookie_ack(stcb);
17327f34832bSRandall Stewart 		if (spec_flag) {
17337f34832bSRandall Stewart 			/*
17347f34832bSRandall Stewart 			 * only if we have retrans set do we do this. What
17357f34832bSRandall Stewart 			 * this call does is get only the COOKIE-ACK out and
17367f34832bSRandall Stewart 			 * then when we return the normal call to
17377f34832bSRandall Stewart 			 * sctp_chunk_output will get the retrans out behind
17387f34832bSRandall Stewart 			 * this.
17397f34832bSRandall Stewart 			 */
1740ceaad40aSRandall Stewart 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
17417f34832bSRandall Stewart 		}
174244b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
174344b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 11;
174444b7479bSRandall Stewart 
1745f8829a4aSRandall Stewart 		return (stcb);
1746f8829a4aSRandall Stewart 	}
1747f8829a4aSRandall Stewart 	if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1748f8829a4aSRandall Stewart 	    ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1749f8829a4aSRandall Stewart 	    cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1750f8829a4aSRandall Stewart 	    cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1751f8829a4aSRandall Stewart 	    cookie->tie_tag_peer_vtag != 0) {
1752f8829a4aSRandall Stewart 		struct sctpasochead *head;
175356f778aaSMichael Tuexen 
1754830d754dSRandall Stewart 		if (asoc->peer_supports_nat) {
1755eec6aed5SMichael Tuexen 			struct sctp_tcb *local_stcb;
1756eec6aed5SMichael Tuexen 
1757830d754dSRandall Stewart 			/*
175856f778aaSMichael Tuexen 			 * This is a gross gross hack. Just call the
1759830d754dSRandall Stewart 			 * cookie_new code since we are allowing a duplicate
1760830d754dSRandall Stewart 			 * association. I hope this works...
1761830d754dSRandall Stewart 			 */
1762eec6aed5SMichael Tuexen 			local_stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst,
1763b1754ad1SMichael Tuexen 			    sh, cookie, cookie_len,
1764830d754dSRandall Stewart 			    inp, netp, init_src, notification,
1765830d754dSRandall Stewart 			    auth_skipped, auth_offset, auth_len,
1766457b4b88SMichael Tuexen 			    mflowtype, mflowid,
1767eec6aed5SMichael Tuexen 			    vrf_id, port);
1768eec6aed5SMichael Tuexen 			if (local_stcb == NULL) {
1769eec6aed5SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
1770eec6aed5SMichael Tuexen 			}
1771eec6aed5SMichael Tuexen 			return (local_stcb);
1772830d754dSRandall Stewart 		}
1773f8829a4aSRandall Stewart 		/*
1774f8829a4aSRandall Stewart 		 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1775f8829a4aSRandall Stewart 		 */
1776a5d547adSRandall Stewart 		/* temp code */
177744b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
177844b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 12;
1779a89481d3SMichael Tuexen 		sctp_stop_association_timers(stcb, false);
1780f8829a4aSRandall Stewart 		/* notify upper layer */
1781f8829a4aSRandall Stewart 		*notification = SCTP_NOTIFY_ASSOC_RESTART;
1782a5d547adSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
1783839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) != SCTP_STATE_OPEN) &&
1784839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1785839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT)) {
1786f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1787f42a358aSRandall Stewart 		}
1788839d21d6SMichael Tuexen 		if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
1789f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1790839d21d6SMichael Tuexen 		} else if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) {
1791f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1792f42a358aSRandall Stewart 		}
1793139bc87fSRandall Stewart 		if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1794839d21d6SMichael Tuexen 			SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1795139bc87fSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
17966fb7b4fbSMichael Tuexen 			    stcb->sctp_ep, stcb, NULL);
1797139bc87fSRandall Stewart 
1798839d21d6SMichael Tuexen 		} else if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) {
1799139bc87fSRandall Stewart 			/* move to OPEN state, if not in SHUTDOWN_SENT */
1800839d21d6SMichael Tuexen 			SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1801139bc87fSRandall Stewart 		}
18020066de1cSMichael Tuexen 		if (asoc->pre_open_streams < asoc->streamoutcnt) {
18030066de1cSMichael Tuexen 			asoc->pre_open_streams = asoc->streamoutcnt;
18040066de1cSMichael Tuexen 		}
1805139bc87fSRandall Stewart 		asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1806139bc87fSRandall Stewart 		asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1807c54a18d2SRandall Stewart 		asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1808139bc87fSRandall Stewart 		asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1809139bc87fSRandall Stewart 		asoc->str_reset_seq_in = asoc->init_seq_number;
1810139bc87fSRandall Stewart 		asoc->advanced_peer_ack_point = asoc->last_acked_seq;
18116f155d69SMichael Tuexen 		asoc->send_sack = 1;
1812a89481d3SMichael Tuexen 		asoc->data_pkts_seen = 0;
1813a89481d3SMichael Tuexen 		asoc->last_data_chunk_from = NULL;
1814a89481d3SMichael Tuexen 		asoc->last_control_chunk_from = NULL;
1815a89481d3SMichael Tuexen 		asoc->last_net_cmt_send_started = NULL;
18160696e120SRandall Stewart 		if (asoc->mapping_array) {
1817139bc87fSRandall Stewart 			memset(asoc->mapping_array, 0,
1818139bc87fSRandall Stewart 			    asoc->mapping_array_size);
18190696e120SRandall Stewart 		}
182077acdc25SRandall Stewart 		if (asoc->nr_mapping_array) {
1821830d754dSRandall Stewart 			memset(asoc->nr_mapping_array, 0,
1822b5c16493SMichael Tuexen 			    asoc->mapping_array_size);
1823830d754dSRandall Stewart 		}
1824a5d547adSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
1825a5d547adSRandall Stewart 		SCTP_INP_INFO_WLOCK();
1826a5d547adSRandall Stewart 		SCTP_INP_WLOCK(stcb->sctp_ep);
1827a5d547adSRandall Stewart 		SCTP_TCB_LOCK(stcb);
1828a5d547adSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, -1);
1829f8829a4aSRandall Stewart 		/* send up all the data */
18307f34832bSRandall Stewart 		SCTP_TCB_SEND_LOCK(stcb);
1831f8829a4aSRandall Stewart 
1832f5d30f7fSMichael Tuexen 		sctp_report_all_outbound(stcb, 0, SCTP_SO_LOCKED);
1833a5d547adSRandall Stewart 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1834325c8c46SMichael Tuexen 			stcb->asoc.strmout[i].chunks_on_queues = 0;
1835f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
1836f0396ad1SMichael Tuexen 			for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1837f0396ad1SMichael Tuexen 				asoc->strmout[i].abandoned_sent[j] = 0;
1838f0396ad1SMichael Tuexen 				asoc->strmout[i].abandoned_unsent[j] = 0;
1839f0396ad1SMichael Tuexen 			}
1840f0396ad1SMichael Tuexen #else
1841f0396ad1SMichael Tuexen 			asoc->strmout[i].abandoned_sent[0] = 0;
1842f0396ad1SMichael Tuexen 			asoc->strmout[i].abandoned_unsent[0] = 0;
1843f0396ad1SMichael Tuexen #endif
184463d5b568SMichael Tuexen 			stcb->asoc.strmout[i].next_mid_ordered = 0;
184563d5b568SMichael Tuexen 			stcb->asoc.strmout[i].next_mid_unordered = 0;
18467a051c0aSMichael Tuexen 			stcb->asoc.strmout[i].sid = i;
1847a5d547adSRandall Stewart 			stcb->asoc.strmout[i].last_msg_incomplete = 0;
1848a5d547adSRandall Stewart 		}
1849aa6db9a0SMichael Tuexen 		TAILQ_FOREACH_SAFE(strrst, &asoc->resetHead, next_resp, nstrrst) {
1850aa6db9a0SMichael Tuexen 			TAILQ_REMOVE(&asoc->resetHead, strrst, next_resp);
1851aa6db9a0SMichael Tuexen 			SCTP_FREE(strrst, SCTP_M_STRESET);
1852aa6db9a0SMichael Tuexen 		}
1853aa6db9a0SMichael Tuexen 		TAILQ_FOREACH_SAFE(sq, &asoc->pending_reply_queue, next, nsq) {
1854aa6db9a0SMichael Tuexen 			TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
1855aa6db9a0SMichael Tuexen 			if (sq->data) {
1856aa6db9a0SMichael Tuexen 				sctp_m_freem(sq->data);
1857aa6db9a0SMichael Tuexen 				sq->data = NULL;
1858aa6db9a0SMichael Tuexen 			}
1859aa6db9a0SMichael Tuexen 			sctp_free_remote_addr(sq->whoFrom);
1860aa6db9a0SMichael Tuexen 			sq->whoFrom = NULL;
1861aa6db9a0SMichael Tuexen 			sq->stcb = NULL;
1862aa6db9a0SMichael Tuexen 			sctp_free_a_readq(stcb, sq);
1863aa6db9a0SMichael Tuexen 		}
1864aa6db9a0SMichael Tuexen 		TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
1865aa6db9a0SMichael Tuexen 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
1866aa6db9a0SMichael Tuexen 			if (chk->data) {
1867aa6db9a0SMichael Tuexen 				sctp_m_freem(chk->data);
1868aa6db9a0SMichael Tuexen 				chk->data = NULL;
1869aa6db9a0SMichael Tuexen 			}
1870aa6db9a0SMichael Tuexen 			if (chk->holds_key_ref)
1871aa6db9a0SMichael Tuexen 				sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
1872aa6db9a0SMichael Tuexen 			sctp_free_remote_addr(chk->whoTo);
1873aa6db9a0SMichael Tuexen 			SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
1874aa6db9a0SMichael Tuexen 			SCTP_DECR_CHK_COUNT();
1875aa6db9a0SMichael Tuexen 		}
1876a89481d3SMichael Tuexen 		asoc->ctrl_queue_cnt = 0;
1877a89481d3SMichael Tuexen 		asoc->str_reset = NULL;
1878a89481d3SMichael Tuexen 		asoc->stream_reset_outstanding = 0;
1879aa6db9a0SMichael Tuexen 		TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
1880aa6db9a0SMichael Tuexen 			TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
1881aa6db9a0SMichael Tuexen 			if (chk->data) {
1882aa6db9a0SMichael Tuexen 				sctp_m_freem(chk->data);
1883aa6db9a0SMichael Tuexen 				chk->data = NULL;
1884aa6db9a0SMichael Tuexen 			}
1885aa6db9a0SMichael Tuexen 			if (chk->holds_key_ref)
1886aa6db9a0SMichael Tuexen 				sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
1887aa6db9a0SMichael Tuexen 			sctp_free_remote_addr(chk->whoTo);
1888aa6db9a0SMichael Tuexen 			SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
1889aa6db9a0SMichael Tuexen 			SCTP_DECR_CHK_COUNT();
1890aa6db9a0SMichael Tuexen 		}
1891aa6db9a0SMichael Tuexen 		TAILQ_FOREACH_SAFE(aparam, &asoc->asconf_queue, next, naparam) {
1892aa6db9a0SMichael Tuexen 			TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
1893aa6db9a0SMichael Tuexen 			SCTP_FREE(aparam, SCTP_M_ASC_ADDR);
1894aa6db9a0SMichael Tuexen 		}
1895aa6db9a0SMichael Tuexen 		TAILQ_FOREACH_SAFE(aack, &asoc->asconf_ack_sent, next, naack) {
1896aa6db9a0SMichael Tuexen 			TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next);
1897aa6db9a0SMichael Tuexen 			if (aack->data != NULL) {
1898aa6db9a0SMichael Tuexen 				sctp_m_freem(aack->data);
1899aa6db9a0SMichael Tuexen 			}
1900aa6db9a0SMichael Tuexen 			SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack);
1901aa6db9a0SMichael Tuexen 		}
1902aa6db9a0SMichael Tuexen 
1903f8829a4aSRandall Stewart 		/* process the INIT-ACK info (my info) */
1904f8829a4aSRandall Stewart 		asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1905f8829a4aSRandall Stewart 		asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1906f8829a4aSRandall Stewart 
1907f8829a4aSRandall Stewart 		/* pull from vtag hash */
1908f8829a4aSRandall Stewart 		LIST_REMOVE(stcb, sctp_asocs);
1909f8829a4aSRandall Stewart 		/* re-insert to new vtag position */
1910b3f1ea41SRandall Stewart 		head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1911b3f1ea41SRandall Stewart 		    SCTP_BASE_INFO(hashasocmark))];
1912f8829a4aSRandall Stewart 		/*
1913f8829a4aSRandall Stewart 		 * put it in the bucket in the vtag hash of assoc's for the
1914f8829a4aSRandall Stewart 		 * system
1915f8829a4aSRandall Stewart 		 */
1916f8829a4aSRandall Stewart 		LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1917f8829a4aSRandall Stewart 
19187f34832bSRandall Stewart 		SCTP_TCB_SEND_UNLOCK(stcb);
1919a5d547adSRandall Stewart 		SCTP_INP_WUNLOCK(stcb->sctp_ep);
1920a5d547adSRandall Stewart 		SCTP_INP_INFO_WUNLOCK();
192156f778aaSMichael Tuexen 		asoc->total_flight = 0;
192256f778aaSMichael Tuexen 		asoc->total_flight_count = 0;
192356f778aaSMichael Tuexen 		/* process the INIT info (peer's info) */
19245f2e1835SMichael Tuexen 		if (sctp_process_init(init_cp, stcb) < 0) {
192544b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
192644b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 13;
19275f2e1835SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
19285f2e1835SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_init() failed\n");
19295f2e1835SMichael Tuexen 			sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
19305f2e1835SMichael Tuexen 			    src, dst, sh, op_err,
19315f2e1835SMichael Tuexen 			    mflowtype, mflowid,
19325f2e1835SMichael Tuexen 			    vrf_id, net->port);
1933f8829a4aSRandall Stewart 			return (NULL);
1934f8829a4aSRandall Stewart 		}
1935f8829a4aSRandall Stewart 		/*
1936f8829a4aSRandall Stewart 		 * since we did not send a HB make sure we don't double
1937f8829a4aSRandall Stewart 		 * things
1938f8829a4aSRandall Stewart 		 */
1939f8829a4aSRandall Stewart 		net->hb_responded = 1;
1940f8829a4aSRandall Stewart 
19415f2e1835SMichael Tuexen 		if ((retval = sctp_load_addresses_from_init(stcb, m,
1942f8829a4aSRandall Stewart 		    init_offset + sizeof(struct sctp_init_chunk),
19435f2e1835SMichael Tuexen 		    initack_offset, src, dst, init_src, stcb->asoc.port)) < 0) {
194444b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
194544b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 14;
19465f2e1835SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
19475f2e1835SMichael Tuexen 			    "Problem with address parameters");
19485f2e1835SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1,
19495f2e1835SMichael Tuexen 			    "Load addresses from INIT causes an abort %d\n",
19505f2e1835SMichael Tuexen 			    retval);
19515f2e1835SMichael Tuexen 			sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
19525f2e1835SMichael Tuexen 			    src, dst, sh, op_err,
19535f2e1835SMichael Tuexen 			    mflowtype, mflowid,
19545f2e1835SMichael Tuexen 			    vrf_id, net->port);
1955f8829a4aSRandall Stewart 			return (NULL);
1956f8829a4aSRandall Stewart 		}
1957f8829a4aSRandall Stewart 		/* respond with a COOKIE-ACK */
1958f8829a4aSRandall Stewart 		sctp_send_cookie_ack(stcb);
195944b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
196044b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 15;
1961a89481d3SMichael Tuexen 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE) &&
1962a89481d3SMichael Tuexen 		    (asoc->sctp_autoclose_ticks > 0)) {
1963a89481d3SMichael Tuexen 			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
1964a89481d3SMichael Tuexen 		}
1965f8829a4aSRandall Stewart 		return (stcb);
1966f8829a4aSRandall Stewart 	}
196744b7479bSRandall Stewart 	if (how_indx < sizeof(asoc->cookie_how))
196844b7479bSRandall Stewart 		asoc->cookie_how[how_indx] = 16;
1969f8829a4aSRandall Stewart 	/* all other cases... */
197012dda000SMichael Tuexen 	SCTP_TCB_UNLOCK(stcb);
1971f8829a4aSRandall Stewart 	return (NULL);
1972f8829a4aSRandall Stewart }
1973f8829a4aSRandall Stewart 
1974f8829a4aSRandall Stewart /*
1975f8829a4aSRandall Stewart  * handle a state cookie for a new association m: input packet mbuf chain--
1976f8829a4aSRandall Stewart  * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
1977f8829a4aSRandall Stewart  * and the cookie signature does not exist offset: offset into mbuf to the
1978f8829a4aSRandall Stewart  * cookie-echo chunk length: length of the cookie chunk to: where the init
1979f8829a4aSRandall Stewart  * was from returns a new TCB
1980f8829a4aSRandall Stewart  */
1981f30ac432SMichael Tuexen static struct sctp_tcb *
1982f8829a4aSRandall Stewart sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1983b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
1984f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1985f8829a4aSRandall Stewart     struct sctp_inpcb *inp, struct sctp_nets **netp,
1986f8829a4aSRandall Stewart     struct sockaddr *init_src, int *notification,
198717205eccSRandall Stewart     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1988457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
1989c54a18d2SRandall Stewart     uint32_t vrf_id, uint16_t port)
1990f8829a4aSRandall Stewart {
1991f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
1992f8829a4aSRandall Stewart 	struct sctp_init_chunk *init_cp, init_buf;
1993f8829a4aSRandall Stewart 	struct sctp_init_ack_chunk *initack_cp, initack_buf;
199424aaac8dSMichael Tuexen 	union sctp_sockstore store;
1995f8829a4aSRandall Stewart 	struct sctp_association *asoc;
1996f8829a4aSRandall Stewart 	int init_offset, initack_offset, initack_limit;
1997f8829a4aSRandall Stewart 	int retval;
1998f8829a4aSRandall Stewart 	int error = 0;
19998262311cSMichael Tuexen 	uint8_t auth_chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
2000ceaad40aSRandall Stewart 
2001f8829a4aSRandall Stewart 	/*
2002f8829a4aSRandall Stewart 	 * find and validate the INIT chunk in the cookie (peer's info) the
2003f8829a4aSRandall Stewart 	 * INIT should start after the cookie-echo header struct (chunk
2004f8829a4aSRandall Stewart 	 * header, state cookie header struct)
2005f8829a4aSRandall Stewart 	 */
2006f8829a4aSRandall Stewart 	init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
2007f8829a4aSRandall Stewart 	init_cp = (struct sctp_init_chunk *)
2008f8829a4aSRandall Stewart 	    sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
2009f8829a4aSRandall Stewart 	    (uint8_t *)&init_buf);
2010f8829a4aSRandall Stewart 	if (init_cp == NULL) {
2011f8829a4aSRandall Stewart 		/* could not pull a INIT chunk in cookie */
2012ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1,
2013ad81507eSRandall Stewart 		    "process_cookie_new: could not pull INIT chunk hdr\n");
2014f8829a4aSRandall Stewart 		return (NULL);
2015f8829a4aSRandall Stewart 	}
2016f8829a4aSRandall Stewart 	if (init_cp->ch.chunk_type != SCTP_INITIATION) {
2017ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
2018f8829a4aSRandall Stewart 		return (NULL);
2019f8829a4aSRandall Stewart 	}
202060990c0cSMichael Tuexen 	initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
2021f8829a4aSRandall Stewart 	/*
2022f8829a4aSRandall Stewart 	 * find and validate the INIT-ACK chunk in the cookie (my info) the
2023f8829a4aSRandall Stewart 	 * INIT-ACK follows the INIT chunk
2024f8829a4aSRandall Stewart 	 */
2025f8829a4aSRandall Stewart 	initack_cp = (struct sctp_init_ack_chunk *)
2026f8829a4aSRandall Stewart 	    sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
2027f8829a4aSRandall Stewart 	    (uint8_t *)&initack_buf);
2028f8829a4aSRandall Stewart 	if (initack_cp == NULL) {
2029f8829a4aSRandall Stewart 		/* could not pull INIT-ACK chunk in cookie */
2030ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2031f8829a4aSRandall Stewart 		return (NULL);
2032f8829a4aSRandall Stewart 	}
2033f8829a4aSRandall Stewart 	if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2034f8829a4aSRandall Stewart 		return (NULL);
2035f8829a4aSRandall Stewart 	}
2036f8829a4aSRandall Stewart 	/*
2037f8829a4aSRandall Stewart 	 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2038f8829a4aSRandall Stewart 	 * "initack_limit" value.  This is because the chk_length field
2039f8829a4aSRandall Stewart 	 * includes the length of the cookie, but the cookie is omitted when
2040f8829a4aSRandall Stewart 	 * the INIT and INIT_ACK are tacked onto the cookie...
2041f8829a4aSRandall Stewart 	 */
2042f8829a4aSRandall Stewart 	initack_limit = offset + cookie_len;
2043f8829a4aSRandall Stewart 
2044f8829a4aSRandall Stewart 	/*
2045f8829a4aSRandall Stewart 	 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2046f8829a4aSRandall Stewart 	 * and popluate
2047f8829a4aSRandall Stewart 	 */
204852be287eSRandall Stewart 
204952be287eSRandall Stewart 	/*
205052be287eSRandall Stewart 	 * Here we do a trick, we set in NULL for the proc/thread argument.
205152be287eSRandall Stewart 	 * We do this since in effect we only use the p argument when the
205252be287eSRandall Stewart 	 * socket is unbound and we must do an implicit bind. Since we are
205352be287eSRandall Stewart 	 * getting a cookie, we cannot be unbound.
205452be287eSRandall Stewart 	 */
2055b5c16493SMichael Tuexen 	stcb = sctp_aloc_assoc(inp, init_src, &error,
2056c7f048abSMichael Tuexen 	    ntohl(initack_cp->init.initiate_tag),
2057c7f048abSMichael Tuexen 	    ntohl(initack_cp->init.initial_tsn), vrf_id,
2058c979034bSMichael Tuexen 	    ntohs(initack_cp->init.num_outbound_streams),
2059ec70917fSMichael Tuexen 	    port,
20608a956abeSMichael Tuexen 	    (struct thread *)NULL,
20618a956abeSMichael Tuexen 	    SCTP_DONT_INITIALIZE_AUTH_PARAMS);
2062f8829a4aSRandall Stewart 	if (stcb == NULL) {
2063f8829a4aSRandall Stewart 		struct mbuf *op_err;
2064f8829a4aSRandall Stewart 
2065f8829a4aSRandall Stewart 		/* memory problem? */
2066ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1,
2067ad81507eSRandall Stewart 		    "process_cookie_new: no room for another TCB!\n");
2068ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2069f8829a4aSRandall Stewart 		sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2070b1754ad1SMichael Tuexen 		    src, dst, sh, op_err,
2071457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2072f30ac432SMichael Tuexen 		    vrf_id, port);
2073f8829a4aSRandall Stewart 		return (NULL);
2074f8829a4aSRandall Stewart 	}
2075f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2076f8829a4aSRandall Stewart 	/* get scope variables out of cookie */
2077a1cb341bSMichael Tuexen 	asoc->scope.ipv4_local_scope = cookie->ipv4_scope;
2078a1cb341bSMichael Tuexen 	asoc->scope.site_scope = cookie->site_scope;
2079a1cb341bSMichael Tuexen 	asoc->scope.local_scope = cookie->local_scope;
2080a1cb341bSMichael Tuexen 	asoc->scope.loopback_scope = cookie->loopback_scope;
2081f8829a4aSRandall Stewart 
2082a1cb341bSMichael Tuexen 	if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2083a1cb341bSMichael Tuexen 	    (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2084f8829a4aSRandall Stewart 		struct mbuf *op_err;
2085f8829a4aSRandall Stewart 
2086f8829a4aSRandall Stewart 		/*
2087f8829a4aSRandall Stewart 		 * Houston we have a problem. The EP changed while the
2088f8829a4aSRandall Stewart 		 * cookie was in flight. Only recourse is to abort the
2089f8829a4aSRandall Stewart 		 * association.
2090f8829a4aSRandall Stewart 		 */
2091ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2092f8829a4aSRandall Stewart 		sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2093b1754ad1SMichael Tuexen 		    src, dst, sh, op_err,
2094457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2095f30ac432SMichael Tuexen 		    vrf_id, port);
2096c4739e2fSRandall Stewart 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2097b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2098f8829a4aSRandall Stewart 		return (NULL);
2099f8829a4aSRandall Stewart 	}
2100f8829a4aSRandall Stewart 	/* process the INIT-ACK info (my info) */
2101f8829a4aSRandall Stewart 	asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2102f8829a4aSRandall Stewart 
2103f8829a4aSRandall Stewart 	/* process the INIT info (peer's info) */
21045f2e1835SMichael Tuexen 	if (sctp_process_init(init_cp, stcb) < 0) {
2105b7d130beSMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2106b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2107f8829a4aSRandall Stewart 		return (NULL);
2108f8829a4aSRandall Stewart 	}
2109f8829a4aSRandall Stewart 	/* load all addresses */
21105f2e1835SMichael Tuexen 	if ((retval = sctp_load_addresses_from_init(stcb, m,
21115f2e1835SMichael Tuexen 	    init_offset + sizeof(struct sctp_init_chunk),
21125f2e1835SMichael Tuexen 	    initack_offset, src, dst, init_src, port)) < 0) {
2113b7d130beSMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2114b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2115f8829a4aSRandall Stewart 		return (NULL);
2116f8829a4aSRandall Stewart 	}
2117f8829a4aSRandall Stewart 	/*
2118f8829a4aSRandall Stewart 	 * verify any preceding AUTH chunk that was skipped
2119f8829a4aSRandall Stewart 	 */
2120f8829a4aSRandall Stewart 	/* pull the local authentication parameters from the cookie/init-ack */
2121f8829a4aSRandall Stewart 	sctp_auth_get_cookie_params(stcb, m,
2122f8829a4aSRandall Stewart 	    initack_offset + sizeof(struct sctp_init_ack_chunk),
2123f8829a4aSRandall Stewart 	    initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2124f8829a4aSRandall Stewart 	if (auth_skipped) {
2125f8829a4aSRandall Stewart 		struct sctp_auth_chunk *auth;
2126f8829a4aSRandall Stewart 
21278262311cSMichael Tuexen 		if (auth_len <= SCTP_CHUNK_BUFFER_SIZE) {
212897feba89SMichael Tuexen 			auth = (struct sctp_auth_chunk *)sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
212997feba89SMichael Tuexen 		} else {
213097feba89SMichael Tuexen 			auth = NULL;
213197feba89SMichael Tuexen 		}
2132ad81507eSRandall Stewart 		if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2133f8829a4aSRandall Stewart 			/* auth HMAC failed, dump the assoc and packet */
2134ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_AUTH1,
2135ad81507eSRandall Stewart 			    "COOKIE-ECHO: AUTH failed\n");
2136b7d130beSMichael Tuexen 			(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2137b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_21);
2138f8829a4aSRandall Stewart 			return (NULL);
2139f8829a4aSRandall Stewart 		} else {
2140f8829a4aSRandall Stewart 			/* remaining chunks checked... good to go */
2141f8829a4aSRandall Stewart 			stcb->asoc.authenticated = 1;
2142f8829a4aSRandall Stewart 		}
2143f8829a4aSRandall Stewart 	}
21440053ed28SMichael Tuexen 
2145f8829a4aSRandall Stewart 	/*
2146f8829a4aSRandall Stewart 	 * if we're doing ASCONFs, check to see if we have any new local
2147f8829a4aSRandall Stewart 	 * addresses that need to get added to the peer (eg. addresses
2148f8829a4aSRandall Stewart 	 * changed while cookie echo in flight).  This needs to be done
2149f8829a4aSRandall Stewart 	 * after we go to the OPEN state to do the correct asconf
2150f8829a4aSRandall Stewart 	 * processing. else, make sure we have the correct addresses in our
2151f8829a4aSRandall Stewart 	 * lists
2152f8829a4aSRandall Stewart 	 */
2153f8829a4aSRandall Stewart 
2154f8829a4aSRandall Stewart 	/* warning, we re-use sin, sin6, sa_store here! */
2155f8829a4aSRandall Stewart 	/* pull in local_address (our "from" address) */
2156e6194c2eSMichael Tuexen 	switch (cookie->laddr_type) {
2157e6194c2eSMichael Tuexen #ifdef INET
2158e6194c2eSMichael Tuexen 	case SCTP_IPV4_ADDRESS:
2159f8829a4aSRandall Stewart 		/* source addr is IPv4 */
216024aaac8dSMichael Tuexen 		memset(&store.sin, 0, sizeof(struct sockaddr_in));
216124aaac8dSMichael Tuexen 		store.sin.sin_family = AF_INET;
216224aaac8dSMichael Tuexen 		store.sin.sin_len = sizeof(struct sockaddr_in);
216324aaac8dSMichael Tuexen 		store.sin.sin_addr.s_addr = cookie->laddress[0];
2164e6194c2eSMichael Tuexen 		break;
2165e6194c2eSMichael Tuexen #endif
2166e6194c2eSMichael Tuexen #ifdef INET6
2167e6194c2eSMichael Tuexen 	case SCTP_IPV6_ADDRESS:
2168f8829a4aSRandall Stewart 		/* source addr is IPv6 */
216924aaac8dSMichael Tuexen 		memset(&store.sin6, 0, sizeof(struct sockaddr_in6));
217024aaac8dSMichael Tuexen 		store.sin6.sin6_family = AF_INET6;
217124aaac8dSMichael Tuexen 		store.sin6.sin6_len = sizeof(struct sockaddr_in6);
217224aaac8dSMichael Tuexen 		store.sin6.sin6_scope_id = cookie->scope_id;
217323602b60SMichael Tuexen 		memcpy(&store.sin6.sin6_addr, cookie->laddress, sizeof(struct in6_addr));
2174e6194c2eSMichael Tuexen 		break;
2175e6194c2eSMichael Tuexen #endif
2176e6194c2eSMichael Tuexen 	default:
2177b7d130beSMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2178b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
2179f8829a4aSRandall Stewart 		return (NULL);
2180f8829a4aSRandall Stewart 	}
2181f8829a4aSRandall Stewart 
21821698cbd9SMichael Tuexen 	/* update current state */
21831698cbd9SMichael Tuexen 	SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2184839d21d6SMichael Tuexen 	SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
21851698cbd9SMichael Tuexen 	if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
21861698cbd9SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
21876fb7b4fbSMichael Tuexen 		    stcb->sctp_ep, stcb, NULL);
21881698cbd9SMichael Tuexen 	}
21891698cbd9SMichael Tuexen 	sctp_stop_all_cookie_timers(stcb);
21901698cbd9SMichael Tuexen 	SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
21911698cbd9SMichael Tuexen 	SCTP_STAT_INCR_GAUGE32(sctps_currestab);
21921698cbd9SMichael Tuexen 
2193f8829a4aSRandall Stewart 	/* set up to notify upper layer */
2194f8829a4aSRandall Stewart 	*notification = SCTP_NOTIFY_ASSOC_UP;
2195f8829a4aSRandall Stewart 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2196f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
21975d08768aSMichael Tuexen 	    (!SCTP_IS_LISTENING(inp))) {
2198f8829a4aSRandall Stewart 		/*
2199f8829a4aSRandall Stewart 		 * This is an endpoint that called connect() how it got a
2200f8829a4aSRandall Stewart 		 * cookie that is NEW is a bit of a mystery. It must be that
2201f8829a4aSRandall Stewart 		 * the INIT was sent, but before it got there.. a complete
2202f8829a4aSRandall Stewart 		 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2203f8829a4aSRandall Stewart 		 * should have went to the other code.. not here.. oh well..
2204f8829a4aSRandall Stewart 		 * a bit of protection is worth having..
2205f8829a4aSRandall Stewart 		 */
2206f8829a4aSRandall Stewart 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2207ceaad40aSRandall Stewart 		soisconnected(stcb->sctp_socket);
2208f8829a4aSRandall Stewart 	} else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
22095d08768aSMichael Tuexen 	    (SCTP_IS_LISTENING(inp))) {
2210f8829a4aSRandall Stewart 		/*
2211f8829a4aSRandall Stewart 		 * We don't want to do anything with this one. Since it is
2212f8829a4aSRandall Stewart 		 * the listening guy. The timer will get started for
2213f8829a4aSRandall Stewart 		 * accepted connections in the caller.
2214f8829a4aSRandall Stewart 		 */
2215f8829a4aSRandall Stewart 		;
2216f8829a4aSRandall Stewart 	}
2217f8829a4aSRandall Stewart 	if (stcb->asoc.sctp_autoclose_ticks &&
2218f8829a4aSRandall Stewart 	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2219f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2220f8829a4aSRandall Stewart 	}
2221b54d3a6cSRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2222b15f5411SMichael Tuexen 	*netp = sctp_findnet(stcb, init_src);
2223b15f5411SMichael Tuexen 	if (*netp != NULL) {
22248c8e10b7SMichael Tuexen 		struct timeval old;
22258c8e10b7SMichael Tuexen 
2226b15f5411SMichael Tuexen 		/*
2227b15f5411SMichael Tuexen 		 * Since we did not send a HB, make sure we don't double
2228b15f5411SMichael Tuexen 		 * things.
2229b15f5411SMichael Tuexen 		 */
2230b15f5411SMichael Tuexen 		(*netp)->hb_responded = 1;
2231b15f5411SMichael Tuexen 		/* Calculate the RTT. */
22328c8e10b7SMichael Tuexen 		old.tv_sec = cookie->time_entered.tv_sec;
22338c8e10b7SMichael Tuexen 		old.tv_usec = cookie->time_entered.tv_usec;
223444f2a327SMichael Tuexen 		sctp_calculate_rto(stcb, asoc, *netp, &old, SCTP_RTT_FROM_NON_DATA);
22359a972525SRandall Stewart 	}
22363232788eSRandall Stewart 	/* respond with a COOKIE-ACK */
2237f8829a4aSRandall Stewart 	sctp_send_cookie_ack(stcb);
22383232788eSRandall Stewart 
22393232788eSRandall Stewart 	/*
22403232788eSRandall Stewart 	 * check the address lists for any ASCONFs that need to be sent
22413232788eSRandall Stewart 	 * AFTER the cookie-ack is sent
22423232788eSRandall Stewart 	 */
22433232788eSRandall Stewart 	sctp_check_address_list(stcb, m,
22443232788eSRandall Stewart 	    initack_offset + sizeof(struct sctp_init_ack_chunk),
22453232788eSRandall Stewart 	    initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
224624aaac8dSMichael Tuexen 	    &store.sa, cookie->local_scope, cookie->site_scope,
22473232788eSRandall Stewart 	    cookie->ipv4_scope, cookie->loopback_scope);
22483232788eSRandall Stewart 
2249f8829a4aSRandall Stewart 	return (stcb);
2250f8829a4aSRandall Stewart }
2251f8829a4aSRandall Stewart 
2252830d754dSRandall Stewart /*
2253830d754dSRandall Stewart  * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2254830d754dSRandall Stewart  * we NEED to make sure we are not already using the vtag. If so we
2255830d754dSRandall Stewart  * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2256830d754dSRandall Stewart 	head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2257830d754dSRandall Stewart 							    SCTP_BASE_INFO(hashasocmark))];
2258830d754dSRandall Stewart 	LIST_FOREACH(stcb, head, sctp_asocs) {
2259830d754dSRandall Stewart 	        if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep))  {
2260830d754dSRandall Stewart 		       -- SEND ABORT - TRY AGAIN --
2261830d754dSRandall Stewart 		}
2262830d754dSRandall Stewart 	}
2263830d754dSRandall Stewart */
2264f8829a4aSRandall Stewart 
2265f8829a4aSRandall Stewart /*
2266f8829a4aSRandall Stewart  * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2267f8829a4aSRandall Stewart  * existing (non-NULL) TCB
2268f8829a4aSRandall Stewart  */
2269f8829a4aSRandall Stewart static struct mbuf *
2270f8829a4aSRandall Stewart sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2271b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
2272f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2273f8829a4aSRandall Stewart     struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
227417205eccSRandall Stewart     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2275f30ac432SMichael Tuexen     struct sctp_tcb **locked_tcb,
2276457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
2277f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
2278f8829a4aSRandall Stewart {
2279f8829a4aSRandall Stewart 	struct sctp_state_cookie *cookie;
2280f8829a4aSRandall Stewart 	struct sctp_tcb *l_stcb = *stcb;
2281f8829a4aSRandall Stewart 	struct sctp_inpcb *l_inp;
2282f8829a4aSRandall Stewart 	struct sockaddr *to;
2283f8829a4aSRandall Stewart 	struct sctp_pcb *ep;
2284f8829a4aSRandall Stewart 	struct mbuf *m_sig;
2285f8829a4aSRandall Stewart 	uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2286f8829a4aSRandall Stewart 	uint8_t *sig;
2287f8829a4aSRandall Stewart 	uint8_t cookie_ok = 0;
2288329204ffSMichael Tuexen 	unsigned int sig_offset, cookie_offset;
2289f8829a4aSRandall Stewart 	unsigned int cookie_len;
2290f8829a4aSRandall Stewart 	struct timeval now;
2291a92d5016SMichael Tuexen 	struct timeval time_entered, time_expires;
2292f8829a4aSRandall Stewart 	int notification = 0;
2293f8829a4aSRandall Stewart 	struct sctp_nets *netl;
2294f8829a4aSRandall Stewart 	int had_a_existing_tcb = 0;
22952fad0e55SMichael Tuexen 	int send_int_conf = 0;
2296e6194c2eSMichael Tuexen #ifdef INET
2297e6194c2eSMichael Tuexen 	struct sockaddr_in sin;
2298e6194c2eSMichael Tuexen #endif
2299e6194c2eSMichael Tuexen #ifdef INET6
2300e6194c2eSMichael Tuexen 	struct sockaddr_in6 sin6;
2301e6194c2eSMichael Tuexen #endif
2302e6194c2eSMichael Tuexen 
2303ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
2304ad81507eSRandall Stewart 	    "sctp_handle_cookie: handling COOKIE-ECHO\n");
2305f8829a4aSRandall Stewart 
2306f8829a4aSRandall Stewart 	if (inp_p == NULL) {
2307f8829a4aSRandall Stewart 		return (NULL);
2308f8829a4aSRandall Stewart 	}
2309f8829a4aSRandall Stewart 	cookie = &cp->cookie;
2310f8829a4aSRandall Stewart 	cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2311f8829a4aSRandall Stewart 	cookie_len = ntohs(cp->ch.chunk_length);
2312f8829a4aSRandall Stewart 
2313d44b45dfSMichael Tuexen 	if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2314d44b45dfSMichael Tuexen 	    sizeof(struct sctp_init_chunk) +
2315d44b45dfSMichael Tuexen 	    sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2316d44b45dfSMichael Tuexen 		/* cookie too small */
2317d44b45dfSMichael Tuexen 		return (NULL);
2318d44b45dfSMichael Tuexen 	}
23193db4ea95SMichael Tuexen 	if ((cookie->peerport != sh->src_port) ||
23203db4ea95SMichael Tuexen 	    (cookie->myport != sh->dest_port) ||
2321f8829a4aSRandall Stewart 	    (cookie->my_vtag != sh->v_tag)) {
2322f8829a4aSRandall Stewart 		/*
2323f8829a4aSRandall Stewart 		 * invalid ports or bad tag.  Note that we always leave the
2324f8829a4aSRandall Stewart 		 * v_tag in the header in network order and when we stored
2325f8829a4aSRandall Stewart 		 * it in the my_vtag slot we also left it in network order.
232617205eccSRandall Stewart 		 * This maintains the match even though it may be in the
2327f8829a4aSRandall Stewart 		 * opposite byte order of the machine :->
2328f8829a4aSRandall Stewart 		 */
2329f8829a4aSRandall Stewart 		return (NULL);
2330f8829a4aSRandall Stewart 	}
2331f8829a4aSRandall Stewart 	/*
2332f8829a4aSRandall Stewart 	 * split off the signature into its own mbuf (since it should not be
2333f8829a4aSRandall Stewart 	 * calculated in the sctp_hmac_m() call).
2334f8829a4aSRandall Stewart 	 */
2335f8829a4aSRandall Stewart 	sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2336eb1b1807SGleb Smirnoff 	m_sig = m_split(m, sig_offset, M_NOWAIT);
2337f8829a4aSRandall Stewart 	if (m_sig == NULL) {
2338f8829a4aSRandall Stewart 		/* out of memory or ?? */
2339f8829a4aSRandall Stewart 		return (NULL);
2340f8829a4aSRandall Stewart 	}
2341eadccaccSRandall Stewart #ifdef SCTP_MBUF_LOGGING
2342b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
23434be807c4SMichael Tuexen 		sctp_log_mbc(m_sig, SCTP_MBUF_SPLIT);
2344eadccaccSRandall Stewart 	}
2345eadccaccSRandall Stewart #endif
2346eadccaccSRandall Stewart 
2347f8829a4aSRandall Stewart 	/*
2348f8829a4aSRandall Stewart 	 * compute the signature/digest for the cookie
2349f8829a4aSRandall Stewart 	 */
2350f8829a4aSRandall Stewart 	ep = &(*inp_p)->sctp_ep;
2351f8829a4aSRandall Stewart 	l_inp = *inp_p;
2352f8829a4aSRandall Stewart 	if (l_stcb) {
2353f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(l_stcb);
2354f8829a4aSRandall Stewart 	}
2355f8829a4aSRandall Stewart 	SCTP_INP_RLOCK(l_inp);
2356f8829a4aSRandall Stewart 	if (l_stcb) {
2357f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(l_stcb);
2358f8829a4aSRandall Stewart 	}
2359f8829a4aSRandall Stewart 	/* which cookie is it? */
2360f8829a4aSRandall Stewart 	if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2361f8829a4aSRandall Stewart 	    (ep->current_secret_number != ep->last_secret_number)) {
2362f8829a4aSRandall Stewart 		/* it's the old cookie */
23636e55db54SRandall Stewart 		(void)sctp_hmac_m(SCTP_HMAC,
2364f8829a4aSRandall Stewart 		    (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2365d00aff5dSRandall Stewart 		    SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2366f8829a4aSRandall Stewart 	} else {
2367f8829a4aSRandall Stewart 		/* it's the current cookie */
23686e55db54SRandall Stewart 		(void)sctp_hmac_m(SCTP_HMAC,
2369f8829a4aSRandall Stewart 		    (uint8_t *)ep->secret_key[(int)ep->current_secret_number],
2370d00aff5dSRandall Stewart 		    SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2371f8829a4aSRandall Stewart 	}
2372f8829a4aSRandall Stewart 	/* get the signature */
2373f8829a4aSRandall Stewart 	SCTP_INP_RUNLOCK(l_inp);
2374f8829a4aSRandall Stewart 	sig = (uint8_t *)sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *)&tmp_sig);
2375f8829a4aSRandall Stewart 	if (sig == NULL) {
2376f8829a4aSRandall Stewart 		/* couldn't find signature */
237703b0b021SRandall Stewart 		sctp_m_freem(m_sig);
2378f8829a4aSRandall Stewart 		return (NULL);
2379f8829a4aSRandall Stewart 	}
2380f8829a4aSRandall Stewart 	/* compare the received digest with the computed digest */
238115a087e5SMichael Tuexen 	if (timingsafe_bcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2382f8829a4aSRandall Stewart 		/* try the old cookie? */
2383f8829a4aSRandall Stewart 		if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2384f8829a4aSRandall Stewart 		    (ep->current_secret_number != ep->last_secret_number)) {
2385f8829a4aSRandall Stewart 			/* compute digest with old */
23866e55db54SRandall Stewart 			(void)sctp_hmac_m(SCTP_HMAC,
2387f8829a4aSRandall Stewart 			    (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2388d00aff5dSRandall Stewart 			    SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2389f8829a4aSRandall Stewart 			/* compare */
239015a087e5SMichael Tuexen 			if (timingsafe_bcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2391f8829a4aSRandall Stewart 				cookie_ok = 1;
2392f8829a4aSRandall Stewart 		}
2393f8829a4aSRandall Stewart 	} else {
2394f8829a4aSRandall Stewart 		cookie_ok = 1;
2395f8829a4aSRandall Stewart 	}
2396f8829a4aSRandall Stewart 
2397f8829a4aSRandall Stewart 	/*
2398f8829a4aSRandall Stewart 	 * Now before we continue we must reconstruct our mbuf so that
2399f8829a4aSRandall Stewart 	 * normal processing of any other chunks will work.
2400f8829a4aSRandall Stewart 	 */
2401f8829a4aSRandall Stewart 	{
2402f8829a4aSRandall Stewart 		struct mbuf *m_at;
2403f8829a4aSRandall Stewart 
2404f8829a4aSRandall Stewart 		m_at = m;
2405139bc87fSRandall Stewart 		while (SCTP_BUF_NEXT(m_at) != NULL) {
2406139bc87fSRandall Stewart 			m_at = SCTP_BUF_NEXT(m_at);
2407f8829a4aSRandall Stewart 		}
2408139bc87fSRandall Stewart 		SCTP_BUF_NEXT(m_at) = m_sig;
2409f8829a4aSRandall Stewart 	}
2410f8829a4aSRandall Stewart 
2411f8829a4aSRandall Stewart 	if (cookie_ok == 0) {
2412ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2413ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
2414ad81507eSRandall Stewart 		    "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2415f8829a4aSRandall Stewart 		    (uint32_t)offset, cookie_offset, sig_offset);
2416f8829a4aSRandall Stewart 		return (NULL);
2417f8829a4aSRandall Stewart 	}
24180053ed28SMichael Tuexen 
2419a92d5016SMichael Tuexen 	if (sctp_ticks_to_msecs(cookie->cookie_life) > SCTP_MAX_COOKIE_LIFE) {
2420a92d5016SMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: Invalid cookie lifetime\n");
2421a92d5016SMichael Tuexen 		return (NULL);
2422a92d5016SMichael Tuexen 	}
2423a92d5016SMichael Tuexen 	time_entered.tv_sec = cookie->time_entered.tv_sec;
2424a92d5016SMichael Tuexen 	time_entered.tv_usec = cookie->time_entered.tv_usec;
2425a92d5016SMichael Tuexen 	if ((time_entered.tv_sec < 0) ||
2426a92d5016SMichael Tuexen 	    (time_entered.tv_usec < 0) ||
2427a92d5016SMichael Tuexen 	    (time_entered.tv_usec >= 1000000)) {
2428a92d5016SMichael Tuexen 		/* Invalid time stamp. Cookie must have been modified. */
2429a92d5016SMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: Invalid time stamp\n");
2430a92d5016SMichael Tuexen 		return (NULL);
2431a92d5016SMichael Tuexen 	}
24326e55db54SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&now);
2433a92d5016SMichael Tuexen 	if (timevalcmp(&now, &time_entered, <)) {
2434a92d5016SMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie generated in the future!\n");
2435a92d5016SMichael Tuexen 		return (NULL);
2436a92d5016SMichael Tuexen 	}
2437a92d5016SMichael Tuexen 	/*
2438a92d5016SMichael Tuexen 	 * Check the cookie timestamps to be sure it's not stale.
2439a92d5016SMichael Tuexen 	 * cookie_life is in ticks, so we convert to seconds.
2440a92d5016SMichael Tuexen 	 */
2441a92d5016SMichael Tuexen 	time_expires.tv_sec = time_entered.tv_sec + sctp_ticks_to_secs(cookie->cookie_life);
2442a92d5016SMichael Tuexen 	time_expires.tv_usec = time_entered.tv_usec;
2443f8829a4aSRandall Stewart 	if (timevalcmp(&now, &time_expires, >)) {
2444f8829a4aSRandall Stewart 		/* cookie is stale! */
2445f8829a4aSRandall Stewart 		struct mbuf *op_err;
244686eda749SMichael Tuexen 		struct sctp_error_stale_cookie *cause;
2447564a95f4SMichael Tuexen 		struct timeval diff;
2448564a95f4SMichael Tuexen 		uint32_t staleness;
2449f8829a4aSRandall Stewart 
245086eda749SMichael Tuexen 		op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_stale_cookie),
2451eb1b1807SGleb Smirnoff 		    0, M_NOWAIT, 1, MT_DATA);
2452f8829a4aSRandall Stewart 		if (op_err == NULL) {
2453f8829a4aSRandall Stewart 			/* FOOBAR */
2454f8829a4aSRandall Stewart 			return (NULL);
2455f8829a4aSRandall Stewart 		}
2456f8829a4aSRandall Stewart 		/* Set the len */
245786eda749SMichael Tuexen 		SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_stale_cookie);
245886eda749SMichael Tuexen 		cause = mtod(op_err, struct sctp_error_stale_cookie *);
245986eda749SMichael Tuexen 		cause->cause.code = htons(SCTP_CAUSE_STALE_COOKIE);
2460a92d5016SMichael Tuexen 		cause->cause.length = htons(sizeof(struct sctp_error_stale_cookie));
2461564a95f4SMichael Tuexen 		diff = now;
2462564a95f4SMichael Tuexen 		timevalsub(&diff, &time_expires);
2463c3115febSMichael Tuexen 		if ((uint32_t)diff.tv_sec > UINT32_MAX / 1000000) {
2464564a95f4SMichael Tuexen 			staleness = UINT32_MAX;
2465564a95f4SMichael Tuexen 		} else {
2466564a95f4SMichael Tuexen 			staleness = diff.tv_sec * 1000000;
2467564a95f4SMichael Tuexen 		}
24683ec509bcSMichael Tuexen 		if (UINT32_MAX - staleness >= (uint32_t)diff.tv_usec) {
2469564a95f4SMichael Tuexen 			staleness += diff.tv_usec;
2470564a95f4SMichael Tuexen 		} else {
2471564a95f4SMichael Tuexen 			staleness = UINT32_MAX;
2472564a95f4SMichael Tuexen 		}
2473564a95f4SMichael Tuexen 		cause->stale_time = htonl(staleness);
2474b1754ad1SMichael Tuexen 		sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
2475d089f9b9SMichael Tuexen 		    mflowtype, mflowid, l_inp->fibnum,
2476c54a18d2SRandall Stewart 		    vrf_id, port);
2477f8829a4aSRandall Stewart 		return (NULL);
2478f8829a4aSRandall Stewart 	}
2479f8829a4aSRandall Stewart 	/*
2480f8829a4aSRandall Stewart 	 * Now we must see with the lookup address if we have an existing
2481f8829a4aSRandall Stewart 	 * asoc. This will only happen if we were in the COOKIE-WAIT state
2482f8829a4aSRandall Stewart 	 * and a INIT collided with us and somewhere the peer sent the
2483f8829a4aSRandall Stewart 	 * cookie on another address besides the single address our assoc
2484f8829a4aSRandall Stewart 	 * had for him. In this case we will have one of the tie-tags set at
2485f8829a4aSRandall Stewart 	 * least AND the address field in the cookie can be used to look it
2486f8829a4aSRandall Stewart 	 * up.
2487f8829a4aSRandall Stewart 	 */
2488f8829a4aSRandall Stewart 	to = NULL;
2489e6194c2eSMichael Tuexen 	switch (cookie->addr_type) {
2490e6194c2eSMichael Tuexen #ifdef INET6
2491e6194c2eSMichael Tuexen 	case SCTP_IPV6_ADDRESS:
2492f8829a4aSRandall Stewart 		memset(&sin6, 0, sizeof(sin6));
2493f8829a4aSRandall Stewart 		sin6.sin6_family = AF_INET6;
2494f8829a4aSRandall Stewart 		sin6.sin6_len = sizeof(sin6);
2495f8829a4aSRandall Stewart 		sin6.sin6_port = sh->src_port;
2496f8829a4aSRandall Stewart 		sin6.sin6_scope_id = cookie->scope_id;
2497f8829a4aSRandall Stewart 		memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2498f8829a4aSRandall Stewart 		    sizeof(sin6.sin6_addr.s6_addr));
2499f8829a4aSRandall Stewart 		to = (struct sockaddr *)&sin6;
2500e6194c2eSMichael Tuexen 		break;
2501e6194c2eSMichael Tuexen #endif
2502e6194c2eSMichael Tuexen #ifdef INET
2503e6194c2eSMichael Tuexen 	case SCTP_IPV4_ADDRESS:
2504f8829a4aSRandall Stewart 		memset(&sin, 0, sizeof(sin));
2505f8829a4aSRandall Stewart 		sin.sin_family = AF_INET;
2506f8829a4aSRandall Stewart 		sin.sin_len = sizeof(sin);
2507f8829a4aSRandall Stewart 		sin.sin_port = sh->src_port;
2508f8829a4aSRandall Stewart 		sin.sin_addr.s_addr = cookie->address[0];
2509f8829a4aSRandall Stewart 		to = (struct sockaddr *)&sin;
2510e6194c2eSMichael Tuexen 		break;
2511e6194c2eSMichael Tuexen #endif
2512e6194c2eSMichael Tuexen 	default:
2513ad81507eSRandall Stewart 		/* This should not happen */
2514ad81507eSRandall Stewart 		return (NULL);
2515f8829a4aSRandall Stewart 	}
2516f0dc2113SMichael Tuexen 	if (*stcb == NULL) {
2517f8829a4aSRandall Stewart 		/* Yep, lets check */
2518b1754ad1SMichael Tuexen 		*stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
2519f8829a4aSRandall Stewart 		if (*stcb == NULL) {
2520f8829a4aSRandall Stewart 			/*
2521f8829a4aSRandall Stewart 			 * We should have only got back the same inp. If we
2522f8829a4aSRandall Stewart 			 * got back a different ep we have a problem. The
2523f8829a4aSRandall Stewart 			 * original findep got back l_inp and now
2524f8829a4aSRandall Stewart 			 */
2525f8829a4aSRandall Stewart 			if (l_inp != *inp_p) {
2526ad81507eSRandall Stewart 				SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2527f8829a4aSRandall Stewart 			}
2528a5d547adSRandall Stewart 		} else {
2529a5d547adSRandall Stewart 			if (*locked_tcb == NULL) {
2530a5d547adSRandall Stewart 				/*
2531a5d547adSRandall Stewart 				 * In this case we found the assoc only
2532a5d547adSRandall Stewart 				 * after we locked the create lock. This
2533a5d547adSRandall Stewart 				 * means we are in a colliding case and we
2534a5d547adSRandall Stewart 				 * must make sure that we unlock the tcb if
2535a5d547adSRandall Stewart 				 * its one of the cases where we throw away
2536a5d547adSRandall Stewart 				 * the incoming packets.
2537a5d547adSRandall Stewart 				 */
2538a5d547adSRandall Stewart 				*locked_tcb = *stcb;
2539a5d547adSRandall Stewart 
2540a5d547adSRandall Stewart 				/*
2541a5d547adSRandall Stewart 				 * We must also increment the inp ref count
2542a5d547adSRandall Stewart 				 * since the ref_count flags was set when we
2543a5d547adSRandall Stewart 				 * did not find the TCB, now we found it
2544a5d547adSRandall Stewart 				 * which reduces the refcount.. we must
2545a5d547adSRandall Stewart 				 * raise it back out to balance it all :-)
2546a5d547adSRandall Stewart 				 */
2547a5d547adSRandall Stewart 				SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2548a5d547adSRandall Stewart 				if ((*stcb)->sctp_ep != l_inp) {
2549ad81507eSRandall Stewart 					SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2550dd294dceSMichael Tuexen 					    (void *)(*stcb)->sctp_ep, (void *)l_inp);
2551a5d547adSRandall Stewart 				}
2552a5d547adSRandall Stewart 			}
2553f8829a4aSRandall Stewart 		}
2554f8829a4aSRandall Stewart 	}
25550053ed28SMichael Tuexen 
2556f8829a4aSRandall Stewart 	cookie_len -= SCTP_SIGNATURE_SIZE;
2557f8829a4aSRandall Stewart 	if (*stcb == NULL) {
2558f8829a4aSRandall Stewart 		/* this is the "normal" case... get a new TCB */
2559b1754ad1SMichael Tuexen 		*stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
2560b1754ad1SMichael Tuexen 		    cookie, cookie_len, *inp_p,
2561b1754ad1SMichael Tuexen 		    netp, to, &notification,
2562f30ac432SMichael Tuexen 		    auth_skipped, auth_offset, auth_len,
2563457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2564f30ac432SMichael Tuexen 		    vrf_id, port);
2565f8829a4aSRandall Stewart 	} else {
2566f8829a4aSRandall Stewart 		/* this is abnormal... cookie-echo on existing TCB */
2567f8829a4aSRandall Stewart 		had_a_existing_tcb = 1;
2568b1754ad1SMichael Tuexen 		*stcb = sctp_process_cookie_existing(m, iphlen, offset,
2569b1754ad1SMichael Tuexen 		    src, dst, sh,
2570830d754dSRandall Stewart 		    cookie, cookie_len, *inp_p, *stcb, netp, to,
2571f30ac432SMichael Tuexen 		    &notification, auth_skipped, auth_offset, auth_len,
2572457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2573f30ac432SMichael Tuexen 		    vrf_id, port);
25745f2e1835SMichael Tuexen 		if (*stcb == NULL) {
25755f2e1835SMichael Tuexen 			*locked_tcb = NULL;
25765f2e1835SMichael Tuexen 		}
2577f8829a4aSRandall Stewart 	}
2578f8829a4aSRandall Stewart 
2579f8829a4aSRandall Stewart 	if (*stcb == NULL) {
2580f8829a4aSRandall Stewart 		/* still no TCB... must be bad cookie-echo */
2581f8829a4aSRandall Stewart 		return (NULL);
2582f8829a4aSRandall Stewart 	}
25835fe29cdfSMichael Tuexen 	if (*netp != NULL) {
2584457b4b88SMichael Tuexen 		(*netp)->flowtype = mflowtype;
25855fe29cdfSMichael Tuexen 		(*netp)->flowid = mflowid;
2586a4ae38f1SMichael Tuexen 	}
2587f8829a4aSRandall Stewart 	/*
2588f8829a4aSRandall Stewart 	 * Ok, we built an association so confirm the address we sent the
2589f8829a4aSRandall Stewart 	 * INIT-ACK to.
2590f8829a4aSRandall Stewart 	 */
2591f8829a4aSRandall Stewart 	netl = sctp_findnet(*stcb, to);
2592f8829a4aSRandall Stewart 	/*
2593f8829a4aSRandall Stewart 	 * This code should in theory NOT run but
2594f8829a4aSRandall Stewart 	 */
2595f8829a4aSRandall Stewart 	if (netl == NULL) {
2596f8829a4aSRandall Stewart 		/* TSNH! Huh, why do I need to add this address here? */
2597ec70917fSMichael Tuexen 		if (sctp_add_remote_addr(*stcb, to, NULL, port,
25987154bf4aSMichael Tuexen 		    SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
259960990c0cSMichael Tuexen 			return (NULL);
260060990c0cSMichael Tuexen 		}
2601f8829a4aSRandall Stewart 		netl = sctp_findnet(*stcb, to);
2602f8829a4aSRandall Stewart 	}
2603f8829a4aSRandall Stewart 	if (netl) {
2604f8829a4aSRandall Stewart 		if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2605f8829a4aSRandall Stewart 			netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2606ad81507eSRandall Stewart 			(void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2607f8829a4aSRandall Stewart 			    netl);
26082fad0e55SMichael Tuexen 			send_int_conf = 1;
2609f8829a4aSRandall Stewart 		}
2610f8829a4aSRandall Stewart 	}
2611ca85e948SMichael Tuexen 	sctp_start_net_timers(*stcb);
2612f8829a4aSRandall Stewart 	if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2613f8829a4aSRandall Stewart 		if (!had_a_existing_tcb ||
2614f8829a4aSRandall Stewart 		    (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2615f8829a4aSRandall Stewart 			/*
2616f8829a4aSRandall Stewart 			 * If we have a NEW cookie or the connect never
2617f8829a4aSRandall Stewart 			 * reached the connected state during collision we
2618f8829a4aSRandall Stewart 			 * must do the TCP accept thing.
2619f8829a4aSRandall Stewart 			 */
2620f8829a4aSRandall Stewart 			struct socket *so, *oso;
2621f8829a4aSRandall Stewart 			struct sctp_inpcb *inp;
2622f8829a4aSRandall Stewart 
2623f8829a4aSRandall Stewart 			if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2624f8829a4aSRandall Stewart 				/*
2625f8829a4aSRandall Stewart 				 * For a restart we will keep the same
2626f8829a4aSRandall Stewart 				 * socket, no need to do anything. I THINK!!
2627f8829a4aSRandall Stewart 				 */
26287215cc1bSMichael Tuexen 				sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
26292fad0e55SMichael Tuexen 				if (send_int_conf) {
26302fad0e55SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
26312fad0e55SMichael Tuexen 					    (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
26322fad0e55SMichael Tuexen 				}
2633f8829a4aSRandall Stewart 				return (m);
2634f8829a4aSRandall Stewart 			}
2635f8829a4aSRandall Stewart 			oso = (*inp_p)->sctp_socket;
26362dad8a55SRandall Stewart 			atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2637f8829a4aSRandall Stewart 			SCTP_TCB_UNLOCK((*stcb));
26381fb51a12SBjoern A. Zeeb 			CURVNET_SET(oso->so_vnet);
263993164cf9SRandall Stewart 			so = sonewconn(oso, 0
2640f8829a4aSRandall Stewart 			    );
26411fb51a12SBjoern A. Zeeb 			CURVNET_RESTORE();
2642f8829a4aSRandall Stewart 			SCTP_TCB_LOCK((*stcb));
26432dad8a55SRandall Stewart 			atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
26442dad8a55SRandall Stewart 
2645f8829a4aSRandall Stewart 			if (so == NULL) {
2646f8829a4aSRandall Stewart 				struct mbuf *op_err;
264770486b27SMichael Tuexen 
2648f8829a4aSRandall Stewart 				/* Too many sockets */
2649ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2650ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2651f8829a4aSRandall Stewart 				sctp_abort_association(*inp_p, NULL, m, iphlen,
2652b1754ad1SMichael Tuexen 				    src, dst, sh, op_err,
2653457b4b88SMichael Tuexen 				    mflowtype, mflowid,
2654f30ac432SMichael Tuexen 				    vrf_id, port);
2655b7d130beSMichael Tuexen 				(void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC,
2656b7d130beSMichael Tuexen 				    SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
2657f8829a4aSRandall Stewart 				return (NULL);
2658f8829a4aSRandall Stewart 			}
2659f8829a4aSRandall Stewart 			inp = (struct sctp_inpcb *)so->so_pcb;
266093164cf9SRandall Stewart 			SCTP_INP_INCR_REF(inp);
266193164cf9SRandall Stewart 			/*
266293164cf9SRandall Stewart 			 * We add the unbound flag here so that if we get an
266393164cf9SRandall Stewart 			 * soabort() before we get the move_pcb done, we
266493164cf9SRandall Stewart 			 * will properly cleanup.
266593164cf9SRandall Stewart 			 */
2666f8829a4aSRandall Stewart 			inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2667f8829a4aSRandall Stewart 			    SCTP_PCB_FLAGS_CONNECTED |
2668f8829a4aSRandall Stewart 			    SCTP_PCB_FLAGS_IN_TCPPOOL |
266993164cf9SRandall Stewart 			    SCTP_PCB_FLAGS_UNBOUND |
2670f8829a4aSRandall Stewart 			    (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2671f8829a4aSRandall Stewart 			    SCTP_PCB_FLAGS_DONT_WAKE);
2672f8829a4aSRandall Stewart 			inp->sctp_features = (*inp_p)->sctp_features;
2673851b7298SRandall Stewart 			inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2674f8829a4aSRandall Stewart 			inp->sctp_socket = so;
2675f8829a4aSRandall Stewart 			inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
267659b6d5beSMichael Tuexen 			inp->max_cwnd = (*inp_p)->max_cwnd;
267720083c2eSMichael Tuexen 			inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2678f342355aSMichael Tuexen 			inp->ecn_supported = (*inp_p)->ecn_supported;
2679dd973b0eSMichael Tuexen 			inp->prsctp_supported = (*inp_p)->prsctp_supported;
2680c79bec9cSMichael Tuexen 			inp->auth_supported = (*inp_p)->auth_supported;
2681c79bec9cSMichael Tuexen 			inp->asconf_supported = (*inp_p)->asconf_supported;
2682317e00efSMichael Tuexen 			inp->reconfig_supported = (*inp_p)->reconfig_supported;
2683caea9879SMichael Tuexen 			inp->nrsack_supported = (*inp_p)->nrsack_supported;
2684cb9b8e6fSMichael Tuexen 			inp->pktdrop_supported = (*inp_p)->pktdrop_supported;
2685f8829a4aSRandall Stewart 			inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2686f8829a4aSRandall Stewart 			inp->sctp_context = (*inp_p)->sctp_context;
2687c4e848b7SRandall Stewart 			inp->local_strreset_support = (*inp_p)->local_strreset_support;
2688d089f9b9SMichael Tuexen 			inp->fibnum = (*inp_p)->fibnum;
2689f8829a4aSRandall Stewart 			inp->inp_starting_point_for_iterator = NULL;
2690f8829a4aSRandall Stewart 			/*
2691f8829a4aSRandall Stewart 			 * copy in the authentication parameters from the
2692f8829a4aSRandall Stewart 			 * original endpoint
2693f8829a4aSRandall Stewart 			 */
2694f8829a4aSRandall Stewart 			if (inp->sctp_ep.local_hmacs)
2695f8829a4aSRandall Stewart 				sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2696f8829a4aSRandall Stewart 			inp->sctp_ep.local_hmacs =
2697f8829a4aSRandall Stewart 			    sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2698f8829a4aSRandall Stewart 			if (inp->sctp_ep.local_auth_chunks)
2699f8829a4aSRandall Stewart 				sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2700f8829a4aSRandall Stewart 			inp->sctp_ep.local_auth_chunks =
2701f8829a4aSRandall Stewart 			    sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2702f8829a4aSRandall Stewart 
2703f8829a4aSRandall Stewart 			/*
2704f8829a4aSRandall Stewart 			 * Now we must move it from one hash table to
2705f8829a4aSRandall Stewart 			 * another and get the tcb in the right place.
2706f8829a4aSRandall Stewart 			 */
270788a7eb29SRandall Stewart 
270888a7eb29SRandall Stewart 			/*
270988a7eb29SRandall Stewart 			 * This is where the one-2-one socket is put into
271088a7eb29SRandall Stewart 			 * the accept state waiting for the accept!
271188a7eb29SRandall Stewart 			 */
271288a7eb29SRandall Stewart 			if (*stcb) {
2713839d21d6SMichael Tuexen 				SCTP_ADD_SUBSTATE(*stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
271488a7eb29SRandall Stewart 			}
2715f8829a4aSRandall Stewart 			sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2716d61a0ae0SRandall Stewart 
2717d61a0ae0SRandall Stewart 			atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2718d61a0ae0SRandall Stewart 			SCTP_TCB_UNLOCK((*stcb));
2719d61a0ae0SRandall Stewart 
2720265de5bbSRobert Watson 			sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2721265de5bbSRobert Watson 			    0);
2722d61a0ae0SRandall Stewart 			SCTP_TCB_LOCK((*stcb));
2723d61a0ae0SRandall Stewart 			atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2724d61a0ae0SRandall Stewart 
272593164cf9SRandall Stewart 			/*
272693164cf9SRandall Stewart 			 * now we must check to see if we were aborted while
272793164cf9SRandall Stewart 			 * the move was going on and the lock/unlock
272893164cf9SRandall Stewart 			 * happened.
272993164cf9SRandall Stewart 			 */
273093164cf9SRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
273193164cf9SRandall Stewart 				/*
273293164cf9SRandall Stewart 				 * yep it was, we leave the assoc attached
273393164cf9SRandall Stewart 				 * to the socket since the sctp_inpcb_free()
273493164cf9SRandall Stewart 				 * call will send an abort for us.
273593164cf9SRandall Stewart 				 */
273693164cf9SRandall Stewart 				SCTP_INP_DECR_REF(inp);
273793164cf9SRandall Stewart 				return (NULL);
273893164cf9SRandall Stewart 			}
273993164cf9SRandall Stewart 			SCTP_INP_DECR_REF(inp);
2740f8829a4aSRandall Stewart 			/* Switch over to the new guy */
2741f8829a4aSRandall Stewart 			*inp_p = inp;
2742ceaad40aSRandall Stewart 			sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
27432fad0e55SMichael Tuexen 			if (send_int_conf) {
27442fad0e55SMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
27452fad0e55SMichael Tuexen 				    (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
27462fad0e55SMichael Tuexen 			}
27470053ed28SMichael Tuexen 
2748b7b84c0eSMichael Tuexen 			/*
2749b7b84c0eSMichael Tuexen 			 * Pull it from the incomplete queue and wake the
2750b7b84c0eSMichael Tuexen 			 * guy
2751b7b84c0eSMichael Tuexen 			 */
275293164cf9SRandall Stewart 			soisconnected(so);
2753f8829a4aSRandall Stewart 			return (m);
2754f8829a4aSRandall Stewart 		}
2755f8829a4aSRandall Stewart 	}
27562fad0e55SMichael Tuexen 	if (notification) {
2757ceaad40aSRandall Stewart 		sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2758f8829a4aSRandall Stewart 	}
27592fad0e55SMichael Tuexen 	if (send_int_conf) {
27602fad0e55SMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
27612fad0e55SMichael Tuexen 		    (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
27622fad0e55SMichael Tuexen 	}
2763f8829a4aSRandall Stewart 	return (m);
2764f8829a4aSRandall Stewart }
2765f8829a4aSRandall Stewart 
2766f8829a4aSRandall Stewart static void
27677215cc1bSMichael Tuexen sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2768f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net)
2769f8829a4aSRandall Stewart {
2770f8829a4aSRandall Stewart 	/* cp must not be used, others call this without a c-ack :-) */
2771f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2772efd5e692SMichael Tuexen 	struct sctp_tmit_chunk *chk;
2773f8829a4aSRandall Stewart 
2774ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
2775ad81507eSRandall Stewart 	    "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2776ad234e3cSMichael Tuexen 	if ((stcb == NULL) || (net == NULL)) {
2777f8829a4aSRandall Stewart 		return;
2778ad234e3cSMichael Tuexen 	}
27790053ed28SMichael Tuexen 
2780f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2781469a65d1SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
2782469a65d1SMichael Tuexen 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2783469a65d1SMichael Tuexen 		    asoc->overall_error_count,
2784469a65d1SMichael Tuexen 		    0,
2785469a65d1SMichael Tuexen 		    SCTP_FROM_SCTP_INPUT,
2786469a65d1SMichael Tuexen 		    __LINE__);
2787469a65d1SMichael Tuexen 	}
2788469a65d1SMichael Tuexen 	asoc->overall_error_count = 0;
2789f8829a4aSRandall Stewart 	sctp_stop_all_cookie_timers(stcb);
2790f8829a4aSRandall Stewart 	/* process according to association state */
2791839d21d6SMichael Tuexen 	if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) {
2792f8829a4aSRandall Stewart 		/* state change only needed when I am in right state */
2793ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2794839d21d6SMichael Tuexen 		SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
2795ca85e948SMichael Tuexen 		sctp_start_net_timers(stcb);
2796f8829a4aSRandall Stewart 		if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2797f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
27986fb7b4fbSMichael Tuexen 			    stcb->sctp_ep, stcb, NULL);
2799f8829a4aSRandall Stewart 		}
2800f8829a4aSRandall Stewart 		/* update RTO */
2801f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2802f8829a4aSRandall Stewart 		SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2803f8829a4aSRandall Stewart 		if (asoc->overall_error_count == 0) {
280444f2a327SMichael Tuexen 			sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered,
2805f79aab18SRandall Stewart 			    SCTP_RTT_FROM_NON_DATA);
2806f8829a4aSRandall Stewart 		}
28076e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2808ceaad40aSRandall Stewart 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2809f8829a4aSRandall Stewart 		if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2810f8829a4aSRandall Stewart 		    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2811f8829a4aSRandall Stewart 			stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2812d69e7322SRandall Stewart 			if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
2813ceaad40aSRandall Stewart 				soisconnected(stcb->sctp_socket);
2814d69e7322SRandall Stewart 			}
2815f8829a4aSRandall Stewart 		}
2816f8829a4aSRandall Stewart 		/*
2817f8829a4aSRandall Stewart 		 * since we did not send a HB make sure we don't double
2818f8829a4aSRandall Stewart 		 * things
2819f8829a4aSRandall Stewart 		 */
2820f8829a4aSRandall Stewart 		net->hb_responded = 1;
2821f8829a4aSRandall Stewart 
2822d69e7322SRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2823d69e7322SRandall Stewart 			/*
2824d69e7322SRandall Stewart 			 * We don't need to do the asconf thing, nor hb or
2825d69e7322SRandall Stewart 			 * autoclose if the socket is closed.
2826d69e7322SRandall Stewart 			 */
2827d69e7322SRandall Stewart 			goto closed_socket;
2828d69e7322SRandall Stewart 		}
28290053ed28SMichael Tuexen 
2830d69e7322SRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2831d69e7322SRandall Stewart 		    stcb, net);
2832d69e7322SRandall Stewart 
2833f8829a4aSRandall Stewart 		if (stcb->asoc.sctp_autoclose_ticks &&
2834f8829a4aSRandall Stewart 		    sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2835f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2836f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, NULL);
2837f8829a4aSRandall Stewart 		}
2838f8829a4aSRandall Stewart 		/*
28391b649582SRandall Stewart 		 * send ASCONF if parameters are pending and ASCONFs are
28401b649582SRandall Stewart 		 * allowed (eg. addresses changed when init/cookie echo were
28411b649582SRandall Stewart 		 * in flight)
2842f8829a4aSRandall Stewart 		 */
2843f8829a4aSRandall Stewart 		if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2844c79bec9cSMichael Tuexen 		    (stcb->asoc.asconf_supported == 1) &&
2845f8829a4aSRandall Stewart 		    (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
28461b649582SRandall Stewart #ifdef SCTP_TIMER_BASED_ASCONF
2847f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2848f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb,
2849f8829a4aSRandall Stewart 			    stcb->asoc.primary_destination);
28501b649582SRandall Stewart #else
28513232788eSRandall Stewart 			sctp_send_asconf(stcb, stcb->asoc.primary_destination,
28523232788eSRandall Stewart 			    SCTP_ADDR_NOT_LOCKED);
28531b649582SRandall Stewart #endif
2854f8829a4aSRandall Stewart 		}
2855f8829a4aSRandall Stewart 	}
2856d69e7322SRandall Stewart closed_socket:
2857f8829a4aSRandall Stewart 	/* Toss the cookie if I can */
2858f8829a4aSRandall Stewart 	sctp_toss_old_cookies(stcb, asoc);
2859f8829a4aSRandall Stewart 	/* Restart the timer if we have pending data */
2860efd5e692SMichael Tuexen 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
2861efd5e692SMichael Tuexen 		if (chk->whoTo != NULL) {
2862efd5e692SMichael Tuexen 			break;
2863efd5e692SMichael Tuexen 		}
2864efd5e692SMichael Tuexen 	}
2865efd5e692SMichael Tuexen 	if (chk != NULL) {
28664a9ef3f8SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
2867f8829a4aSRandall Stewart 	}
2868f8829a4aSRandall Stewart }
2869f8829a4aSRandall Stewart 
2870f8829a4aSRandall Stewart static void
2871f8829a4aSRandall Stewart sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
2872f8829a4aSRandall Stewart     struct sctp_tcb *stcb)
2873f8829a4aSRandall Stewart {
2874f8829a4aSRandall Stewart 	struct sctp_nets *net;
2875f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *lchk;
2876a21779f0SRandall Stewart 	struct sctp_ecne_chunk bkup;
287760990c0cSMichael Tuexen 	uint8_t override_bit;
2878a21779f0SRandall Stewart 	uint32_t tsn, window_data_tsn;
2879a21779f0SRandall Stewart 	int len;
2880dec0177dSRandall Stewart 	unsigned int pkt_cnt;
2881f8829a4aSRandall Stewart 
2882a21779f0SRandall Stewart 	len = ntohs(cp->ch.chunk_length);
2883a21779f0SRandall Stewart 	if (len == sizeof(struct old_sctp_ecne_chunk)) {
2884a21779f0SRandall Stewart 		/* Its the old format */
2885a21779f0SRandall Stewart 		memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
2886a21779f0SRandall Stewart 		bkup.num_pkts_since_cwr = htonl(1);
2887a21779f0SRandall Stewart 		cp = &bkup;
2888a21779f0SRandall Stewart 	}
2889f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvecne);
2890f8829a4aSRandall Stewart 	tsn = ntohl(cp->tsn);
2891a21779f0SRandall Stewart 	pkt_cnt = ntohl(cp->num_pkts_since_cwr);
2892a21779f0SRandall Stewart 	lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
2893f8829a4aSRandall Stewart 	if (lchk == NULL) {
2894493d8e5aSRandall Stewart 		window_data_tsn = stcb->asoc.sending_seq - 1;
2895f8829a4aSRandall Stewart 	} else {
289649656eefSMichael Tuexen 		window_data_tsn = lchk->rec.data.tsn;
2897f8829a4aSRandall Stewart 	}
2898f8829a4aSRandall Stewart 
2899a21779f0SRandall Stewart 	/* Find where it was sent to if possible. */
2900f8829a4aSRandall Stewart 	net = NULL;
29014a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
290249656eefSMichael Tuexen 		if (lchk->rec.data.tsn == tsn) {
2903f8829a4aSRandall Stewart 			net = lchk->whoTo;
2904899288aeSRandall Stewart 			net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
2905f8829a4aSRandall Stewart 			break;
2906f8829a4aSRandall Stewart 		}
290749656eefSMichael Tuexen 		if (SCTP_TSN_GT(lchk->rec.data.tsn, tsn)) {
2908f8829a4aSRandall Stewart 			break;
2909f8829a4aSRandall Stewart 		}
291020b07a4dSMichael Tuexen 	}
2911a21779f0SRandall Stewart 	if (net == NULL) {
2912a21779f0SRandall Stewart 		/*
2913a21779f0SRandall Stewart 		 * What to do. A previous send of a CWR was possibly lost.
2914a21779f0SRandall Stewart 		 * See how old it is, we may have it marked on the actual
2915a21779f0SRandall Stewart 		 * net.
2916a21779f0SRandall Stewart 		 */
2917a21779f0SRandall Stewart 		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2918a21779f0SRandall Stewart 			if (tsn == net->last_cwr_tsn) {
2919a21779f0SRandall Stewart 				/* Found him, send it off */
292060990c0cSMichael Tuexen 				break;
2921a21779f0SRandall Stewart 			}
2922a21779f0SRandall Stewart 		}
292360990c0cSMichael Tuexen 		if (net == NULL) {
2924a21779f0SRandall Stewart 			/*
292560990c0cSMichael Tuexen 			 * If we reach here, we need to send a special CWR
292660990c0cSMichael Tuexen 			 * that says hey, we did this a long time ago and
292760990c0cSMichael Tuexen 			 * you lost the response.
2928a21779f0SRandall Stewart 			 */
2929a21779f0SRandall Stewart 			net = TAILQ_FIRST(&stcb->asoc.nets);
293060990c0cSMichael Tuexen 			if (net == NULL) {
293160990c0cSMichael Tuexen 				/* TSNH */
293260990c0cSMichael Tuexen 				return;
2933a21779f0SRandall Stewart 			}
293460990c0cSMichael Tuexen 			override_bit = SCTP_CWR_REDUCE_OVERRIDE;
293560990c0cSMichael Tuexen 		} else {
293660990c0cSMichael Tuexen 			override_bit = 0;
293760990c0cSMichael Tuexen 		}
293860990c0cSMichael Tuexen 	} else {
293960990c0cSMichael Tuexen 		override_bit = 0;
294060990c0cSMichael Tuexen 	}
2941493d8e5aSRandall Stewart 	if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
2942493d8e5aSRandall Stewart 	    ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
2943b7b84c0eSMichael Tuexen 		/*
2944b7b84c0eSMichael Tuexen 		 * JRS - Use the congestion control given in the pluggable
2945b7b84c0eSMichael Tuexen 		 * CC module
2946b7b84c0eSMichael Tuexen 		 */
2947493d8e5aSRandall Stewart 		stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
2948f8829a4aSRandall Stewart 		/*
2949a21779f0SRandall Stewart 		 * We reduce once every RTT. So we will only lower cwnd at
2950a21779f0SRandall Stewart 		 * the next sending seq i.e. the window_data_tsn
2951f8829a4aSRandall Stewart 		 */
2952a21779f0SRandall Stewart 		net->cwr_window_tsn = window_data_tsn;
2953a21779f0SRandall Stewart 		net->ecn_ce_pkt_cnt += pkt_cnt;
2954a21779f0SRandall Stewart 		net->lost_cnt = pkt_cnt;
2955a21779f0SRandall Stewart 		net->last_cwr_tsn = tsn;
2956a21779f0SRandall Stewart 	} else {
2957a21779f0SRandall Stewart 		override_bit |= SCTP_CWR_IN_SAME_WINDOW;
2958493d8e5aSRandall Stewart 		if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
2959493d8e5aSRandall Stewart 		    ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
2960a21779f0SRandall Stewart 			/*
2961493d8e5aSRandall Stewart 			 * Another loss in the same window update how many
2962493d8e5aSRandall Stewart 			 * marks/packets lost we have had.
2963a21779f0SRandall Stewart 			 */
2964493d8e5aSRandall Stewart 			int cnt = 1;
2965a21779f0SRandall Stewart 
2966a21779f0SRandall Stewart 			if (pkt_cnt > net->lost_cnt) {
2967a21779f0SRandall Stewart 				/* Should be the case */
2968493d8e5aSRandall Stewart 				cnt = (pkt_cnt - net->lost_cnt);
2969493d8e5aSRandall Stewart 				net->ecn_ce_pkt_cnt += cnt;
2970a21779f0SRandall Stewart 			}
2971493d8e5aSRandall Stewart 			net->lost_cnt = pkt_cnt;
2972a21779f0SRandall Stewart 			net->last_cwr_tsn = tsn;
2973493d8e5aSRandall Stewart 			/*
2974493d8e5aSRandall Stewart 			 * Most CC functions will ignore this call, since we
2975493d8e5aSRandall Stewart 			 * are in-window yet of the initial CE the peer saw.
2976493d8e5aSRandall Stewart 			 */
2977493d8e5aSRandall Stewart 			stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
2978a21779f0SRandall Stewart 		}
2979f8829a4aSRandall Stewart 	}
2980f8829a4aSRandall Stewart 	/*
2981f8829a4aSRandall Stewart 	 * We always send a CWR this way if our previous one was lost our
2982f8829a4aSRandall Stewart 	 * peer will get an update, or if it is not time again to reduce we
2983a21779f0SRandall Stewart 	 * still get the cwr to the peer. Note we set the override when we
2984a21779f0SRandall Stewart 	 * could not find the TSN on the chunk or the destination network.
2985f8829a4aSRandall Stewart 	 */
2986a21779f0SRandall Stewart 	sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
2987f8829a4aSRandall Stewart }
2988f8829a4aSRandall Stewart 
2989f8829a4aSRandall Stewart static void
2990a21779f0SRandall Stewart sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
2991f8829a4aSRandall Stewart {
2992f8829a4aSRandall Stewart 	/*
2993f8829a4aSRandall Stewart 	 * Here we get a CWR from the peer. We must look in the outqueue and
2994582212faSEitan Adler 	 * make sure that we have a covered ECNE in the control chunk part.
2995f8829a4aSRandall Stewart 	 * If so remove it.
2996f8829a4aSRandall Stewart 	 */
29976c2cfc04SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
2998f8829a4aSRandall Stewart 	struct sctp_ecne_chunk *ecne;
2999a21779f0SRandall Stewart 	int override;
3000a21779f0SRandall Stewart 	uint32_t cwr_tsn;
3001f8829a4aSRandall Stewart 
3002a21779f0SRandall Stewart 	cwr_tsn = ntohl(cp->tsn);
3003a21779f0SRandall Stewart 	override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
30046c2cfc04SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &stcb->asoc.control_send_queue, sctp_next, nchk) {
3005f8829a4aSRandall Stewart 		if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3006f8829a4aSRandall Stewart 			continue;
3007f8829a4aSRandall Stewart 		}
3008a21779f0SRandall Stewart 		if ((override == 0) && (chk->whoTo != net)) {
3009a21779f0SRandall Stewart 			/* Must be from the right src unless override is set */
3010a21779f0SRandall Stewart 			continue;
3011a21779f0SRandall Stewart 		}
3012f8829a4aSRandall Stewart 		ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3013a21779f0SRandall Stewart 		if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3014f8829a4aSRandall Stewart 			/* this covers this ECNE, we can remove it */
3015f8829a4aSRandall Stewart 			stcb->asoc.ecn_echo_cnt_onq--;
3016f8829a4aSRandall Stewart 			TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3017f8829a4aSRandall Stewart 			    sctp_next);
3018cd6340caSMichael Tuexen 			stcb->asoc.ctrl_queue_cnt--;
3019f8829a4aSRandall Stewart 			sctp_m_freem(chk->data);
3020f8829a4aSRandall Stewart 			chk->data = NULL;
3021689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3022a21779f0SRandall Stewart 			if (override == 0) {
3023f8829a4aSRandall Stewart 				break;
3024f8829a4aSRandall Stewart 			}
3025f8829a4aSRandall Stewart 		}
3026f8829a4aSRandall Stewart 	}
3027a21779f0SRandall Stewart }
3028f8829a4aSRandall Stewart 
3029f8829a4aSRandall Stewart static void
30307215cc1bSMichael Tuexen sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3031f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net)
3032f8829a4aSRandall Stewart {
3033ceaad40aSRandall Stewart 
3034ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
3035ad81507eSRandall Stewart 	    "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3036f8829a4aSRandall Stewart 	if (stcb == NULL)
3037f8829a4aSRandall Stewart 		return;
3038f8829a4aSRandall Stewart 
3039f8829a4aSRandall Stewart 	/* process according to association state */
3040839d21d6SMichael Tuexen 	if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3041f8829a4aSRandall Stewart 		/* unexpected SHUTDOWN-COMPLETE... so ignore... */
30422afb3e84SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
30432afb3e84SRandall Stewart 		    "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3044f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3045f8829a4aSRandall Stewart 		return;
3046f8829a4aSRandall Stewart 	}
3047f8829a4aSRandall Stewart 	/* notify upper layer protocol */
3048f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
3049ceaad40aSRandall Stewart 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
305056f778aaSMichael Tuexen 	}
305156f778aaSMichael Tuexen #ifdef INVARIANTS
30520f1346f7SMichael Tuexen 	if (!TAILQ_EMPTY(&stcb->asoc.send_queue) ||
30530f1346f7SMichael Tuexen 	    !TAILQ_EMPTY(&stcb->asoc.sent_queue) ||
3054124d851aSMichael Tuexen 	    sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
305556f778aaSMichael Tuexen 		panic("Queues are not empty when handling SHUTDOWN-COMPLETE");
3056f8829a4aSRandall Stewart 	}
305756f778aaSMichael Tuexen #endif
3058f8829a4aSRandall Stewart 	/* stop the timer */
3059b7d130beSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net,
3060b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3061f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3062f8829a4aSRandall Stewart 	/* free the TCB */
30632afb3e84SRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
30642afb3e84SRandall Stewart 	    "sctp_handle_shutdown_complete: calls free-asoc\n");
3065b7d130beSMichael Tuexen 	(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
3066b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3067f8829a4aSRandall Stewart 	return;
3068f8829a4aSRandall Stewart }
3069f8829a4aSRandall Stewart 
3070f8829a4aSRandall Stewart static int
3071f8829a4aSRandall Stewart process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3072f8829a4aSRandall Stewart     struct sctp_nets *net, uint8_t flg)
3073f8829a4aSRandall Stewart {
3074f8829a4aSRandall Stewart 	switch (desc->chunk_type) {
3075f8829a4aSRandall Stewart 	case SCTP_DATA:
307632df1c9eSMichael Tuexen 	case SCTP_IDATA:
307732df1c9eSMichael Tuexen 		/* find the tsn to resend (possibly) */
3078f8829a4aSRandall Stewart 		{
3079f8829a4aSRandall Stewart 			uint32_t tsn;
3080f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *tp1;
3081f8829a4aSRandall Stewart 
3082f8829a4aSRandall Stewart 			tsn = ntohl(desc->tsn_ifany);
30834a9ef3f8SMichael Tuexen 			TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
308449656eefSMichael Tuexen 				if (tp1->rec.data.tsn == tsn) {
3085f8829a4aSRandall Stewart 					/* found it */
3086f8829a4aSRandall Stewart 					break;
3087f8829a4aSRandall Stewart 				}
308849656eefSMichael Tuexen 				if (SCTP_TSN_GT(tp1->rec.data.tsn, tsn)) {
3089f8829a4aSRandall Stewart 					/* not found */
3090f8829a4aSRandall Stewart 					tp1 = NULL;
3091f8829a4aSRandall Stewart 					break;
3092f8829a4aSRandall Stewart 				}
3093f8829a4aSRandall Stewart 			}
3094f8829a4aSRandall Stewart 			if (tp1 == NULL) {
3095f8829a4aSRandall Stewart 				/*
3096f8829a4aSRandall Stewart 				 * Do it the other way , aka without paying
3097f8829a4aSRandall Stewart 				 * attention to queue seq order.
3098f8829a4aSRandall Stewart 				 */
3099f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpdnfnd);
31004a9ef3f8SMichael Tuexen 				TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
310149656eefSMichael Tuexen 					if (tp1->rec.data.tsn == tsn) {
3102f8829a4aSRandall Stewart 						/* found it */
3103f8829a4aSRandall Stewart 						break;
3104f8829a4aSRandall Stewart 					}
3105f8829a4aSRandall Stewart 				}
3106f8829a4aSRandall Stewart 			}
3107f8829a4aSRandall Stewart 			if (tp1 == NULL) {
3108f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrptsnnf);
3109f8829a4aSRandall Stewart 			}
3110f8829a4aSRandall Stewart 			if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
31117da23bc8SMichael Tuexen 				if (((flg & SCTP_BADCRC) == 0) &&
31127da23bc8SMichael Tuexen 				    ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
31137da23bc8SMichael Tuexen 					return (0);
31147da23bc8SMichael Tuexen 				}
3115f8829a4aSRandall Stewart 				if ((stcb->asoc.peers_rwnd == 0) &&
3116f8829a4aSRandall Stewart 				    ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3117f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_pdrpdiwnp);
3118f8829a4aSRandall Stewart 					return (0);
3119f8829a4aSRandall Stewart 				}
3120f8829a4aSRandall Stewart 				if (stcb->asoc.peers_rwnd == 0 &&
3121f8829a4aSRandall Stewart 				    (flg & SCTP_FROM_MIDDLE_BOX)) {
3122f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_pdrpdizrw);
3123f8829a4aSRandall Stewart 					return (0);
3124f8829a4aSRandall Stewart 				}
312532df1c9eSMichael Tuexen 				if ((uint32_t)SCTP_BUF_LEN(tp1->data) <
312632df1c9eSMichael Tuexen 				    SCTP_DATA_CHUNK_OVERHEAD(stcb) + SCTP_NUM_DB_TO_VERIFY) {
312732df1c9eSMichael Tuexen 					/* Payload not matching. */
3128f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_pdrpbadd);
3129f8829a4aSRandall Stewart 					return (-1);
3130f8829a4aSRandall Stewart 				}
313132df1c9eSMichael Tuexen 				if (memcmp(mtod(tp1->data, caddr_t)+SCTP_DATA_CHUNK_OVERHEAD(stcb),
313232df1c9eSMichael Tuexen 				    desc->data_bytes, SCTP_NUM_DB_TO_VERIFY) != 0) {
313332df1c9eSMichael Tuexen 					/* Payload not matching. */
313432df1c9eSMichael Tuexen 					SCTP_STAT_INCR(sctps_pdrpbadd);
313532df1c9eSMichael Tuexen 					return (-1);
3136f8829a4aSRandall Stewart 				}
3137f8829a4aSRandall Stewart 				if (tp1->do_rtt) {
3138f8829a4aSRandall Stewart 					/*
3139f8829a4aSRandall Stewart 					 * this guy had a RTO calculation
3140f8829a4aSRandall Stewart 					 * pending on it, cancel it
3141f8829a4aSRandall Stewart 					 */
3142f79aab18SRandall Stewart 					if (tp1->whoTo->rto_needed == 0) {
3143f79aab18SRandall Stewart 						tp1->whoTo->rto_needed = 1;
3144f79aab18SRandall Stewart 					}
3145f8829a4aSRandall Stewart 					tp1->do_rtt = 0;
3146f8829a4aSRandall Stewart 				}
3147f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpmark);
3148f8829a4aSRandall Stewart 				if (tp1->sent != SCTP_DATAGRAM_RESEND)
3149f8829a4aSRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3150f8829a4aSRandall Stewart 				/*
3151f8829a4aSRandall Stewart 				 * mark it as if we were doing a FR, since
3152f8829a4aSRandall Stewart 				 * we will be getting gap ack reports behind
3153f8829a4aSRandall Stewart 				 * the info from the router.
3154f8829a4aSRandall Stewart 				 */
3155f8829a4aSRandall Stewart 				tp1->rec.data.doing_fast_retransmit = 1;
3156f8829a4aSRandall Stewart 				/*
3157f8829a4aSRandall Stewart 				 * mark the tsn with what sequences can
3158f8829a4aSRandall Stewart 				 * cause a new FR.
3159f8829a4aSRandall Stewart 				 */
3160f8829a4aSRandall Stewart 				if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3161f8829a4aSRandall Stewart 					tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3162f8829a4aSRandall Stewart 				} else {
316349656eefSMichael Tuexen 					tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.tsn;
3164f8829a4aSRandall Stewart 				}
3165f8829a4aSRandall Stewart 
3166f8829a4aSRandall Stewart 				/* restart the timer */
3167f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3168b7d130beSMichael Tuexen 				    stcb, tp1->whoTo,
3169b7d130beSMichael Tuexen 				    SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3170f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3171f8829a4aSRandall Stewart 				    stcb, tp1->whoTo);
3172f8829a4aSRandall Stewart 
3173f8829a4aSRandall Stewart 				/* fix counts and things */
3174b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3175c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3176a5d547adSRandall Stewart 					    tp1->whoTo->flight_size,
3177a5d547adSRandall Stewart 					    tp1->book_size,
31789a8e3088SMichael Tuexen 					    (uint32_t)(uintptr_t)stcb,
317949656eefSMichael Tuexen 					    tp1->rec.data.tsn);
318080fefe0aSRandall Stewart 				}
31818933fa13SRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3182c105859eSRandall Stewart 					sctp_flight_size_decrease(tp1);
3183c105859eSRandall Stewart 					sctp_total_flight_decrease(stcb, tp1);
31848933fa13SRandall Stewart 				}
3185f23ba7b1SMichael Tuexen 				tp1->sent = SCTP_DATAGRAM_RESEND;
3186f8829a4aSRandall Stewart 			} {
3187f8829a4aSRandall Stewart 				/* audit code */
3188f8829a4aSRandall Stewart 				unsigned int audit;
3189f8829a4aSRandall Stewart 
3190f8829a4aSRandall Stewart 				audit = 0;
3191f8829a4aSRandall Stewart 				TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3192f8829a4aSRandall Stewart 					if (tp1->sent == SCTP_DATAGRAM_RESEND)
3193f8829a4aSRandall Stewart 						audit++;
3194f8829a4aSRandall Stewart 				}
3195f8829a4aSRandall Stewart 				TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3196f8829a4aSRandall Stewart 				    sctp_next) {
3197f8829a4aSRandall Stewart 					if (tp1->sent == SCTP_DATAGRAM_RESEND)
3198f8829a4aSRandall Stewart 						audit++;
3199f8829a4aSRandall Stewart 				}
3200f8829a4aSRandall Stewart 				if (audit != stcb->asoc.sent_queue_retran_cnt) {
3201ad81507eSRandall Stewart 					SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3202f8829a4aSRandall Stewart 					    audit, stcb->asoc.sent_queue_retran_cnt);
3203f8829a4aSRandall Stewart #ifndef SCTP_AUDITING_ENABLED
3204f8829a4aSRandall Stewart 					stcb->asoc.sent_queue_retran_cnt = audit;
3205f8829a4aSRandall Stewart #endif
3206f8829a4aSRandall Stewart 				}
3207f8829a4aSRandall Stewart 			}
3208f8829a4aSRandall Stewart 		}
3209f8829a4aSRandall Stewart 		break;
3210f8829a4aSRandall Stewart 	case SCTP_ASCONF:
3211f8829a4aSRandall Stewart 		{
3212f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *asconf;
3213f8829a4aSRandall Stewart 
3214f8829a4aSRandall Stewart 			TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3215f8829a4aSRandall Stewart 			    sctp_next) {
3216f8829a4aSRandall Stewart 				if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3217f8829a4aSRandall Stewart 					break;
3218f8829a4aSRandall Stewart 				}
3219f8829a4aSRandall Stewart 			}
3220f8829a4aSRandall Stewart 			if (asconf) {
3221f8829a4aSRandall Stewart 				if (asconf->sent != SCTP_DATAGRAM_RESEND)
3222f8829a4aSRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3223f8829a4aSRandall Stewart 				asconf->sent = SCTP_DATAGRAM_RESEND;
3224f8829a4aSRandall Stewart 				asconf->snd_count--;
3225f8829a4aSRandall Stewart 			}
3226f8829a4aSRandall Stewart 		}
3227f8829a4aSRandall Stewart 		break;
3228f8829a4aSRandall Stewart 	case SCTP_INITIATION:
3229f8829a4aSRandall Stewart 		/* resend the INIT */
3230f8829a4aSRandall Stewart 		stcb->asoc.dropped_special_cnt++;
3231f8829a4aSRandall Stewart 		if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3232f8829a4aSRandall Stewart 			/*
3233f8829a4aSRandall Stewart 			 * If we can get it in, in a few attempts we do
3234f8829a4aSRandall Stewart 			 * this, otherwise we let the timer fire.
3235f8829a4aSRandall Stewart 			 */
3236f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3237b7d130beSMichael Tuexen 			    stcb, net,
3238b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
3239ceaad40aSRandall Stewart 			sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3240f8829a4aSRandall Stewart 		}
3241f8829a4aSRandall Stewart 		break;
3242f8829a4aSRandall Stewart 	case SCTP_SELECTIVE_ACK:
3243b5c16493SMichael Tuexen 	case SCTP_NR_SELECTIVE_ACK:
3244f8829a4aSRandall Stewart 		/* resend the sack */
3245689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3246f8829a4aSRandall Stewart 		break;
3247f8829a4aSRandall Stewart 	case SCTP_HEARTBEAT_REQUEST:
3248f8829a4aSRandall Stewart 		/* resend a demand HB */
3249b54d3a6cSRandall Stewart 		if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3250b7b84c0eSMichael Tuexen 			/*
3251b7b84c0eSMichael Tuexen 			 * Only retransmit if we KNOW we wont destroy the
3252b7b84c0eSMichael Tuexen 			 * tcb
3253b7b84c0eSMichael Tuexen 			 */
3254ca85e948SMichael Tuexen 			sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3255b54d3a6cSRandall Stewart 		}
3256f8829a4aSRandall Stewart 		break;
3257f8829a4aSRandall Stewart 	case SCTP_SHUTDOWN:
3258f8829a4aSRandall Stewart 		sctp_send_shutdown(stcb, net);
3259f8829a4aSRandall Stewart 		break;
3260f8829a4aSRandall Stewart 	case SCTP_SHUTDOWN_ACK:
3261f8829a4aSRandall Stewart 		sctp_send_shutdown_ack(stcb, net);
3262f8829a4aSRandall Stewart 		break;
3263f8829a4aSRandall Stewart 	case SCTP_COOKIE_ECHO:
3264f8829a4aSRandall Stewart 		{
3265f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *cookie;
3266f8829a4aSRandall Stewart 
3267f8829a4aSRandall Stewart 			cookie = NULL;
3268f8829a4aSRandall Stewart 			TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3269f8829a4aSRandall Stewart 			    sctp_next) {
3270f8829a4aSRandall Stewart 				if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3271f8829a4aSRandall Stewart 					break;
3272f8829a4aSRandall Stewart 				}
3273f8829a4aSRandall Stewart 			}
3274f8829a4aSRandall Stewart 			if (cookie) {
3275f8829a4aSRandall Stewart 				if (cookie->sent != SCTP_DATAGRAM_RESEND)
3276f8829a4aSRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3277f8829a4aSRandall Stewart 				cookie->sent = SCTP_DATAGRAM_RESEND;
3278f8829a4aSRandall Stewart 				sctp_stop_all_cookie_timers(stcb);
3279f8829a4aSRandall Stewart 			}
3280f8829a4aSRandall Stewart 		}
3281f8829a4aSRandall Stewart 		break;
3282f8829a4aSRandall Stewart 	case SCTP_COOKIE_ACK:
3283f8829a4aSRandall Stewart 		sctp_send_cookie_ack(stcb);
3284f8829a4aSRandall Stewart 		break;
3285f8829a4aSRandall Stewart 	case SCTP_ASCONF_ACK:
3286f8829a4aSRandall Stewart 		/* resend last asconf ack */
32872afb3e84SRandall Stewart 		sctp_send_asconf_ack(stcb);
3288f8829a4aSRandall Stewart 		break;
328944249214SRandall Stewart 	case SCTP_IFORWARD_CUM_TSN:
3290f8829a4aSRandall Stewart 	case SCTP_FORWARD_CUM_TSN:
3291f8829a4aSRandall Stewart 		send_forward_tsn(stcb, &stcb->asoc);
3292f8829a4aSRandall Stewart 		break;
3293f8829a4aSRandall Stewart 		/* can't do anything with these */
3294f8829a4aSRandall Stewart 	case SCTP_PACKET_DROPPED:
3295f8829a4aSRandall Stewart 	case SCTP_INITIATION_ACK:	/* this should not happen */
3296f8829a4aSRandall Stewart 	case SCTP_HEARTBEAT_ACK:
3297f8829a4aSRandall Stewart 	case SCTP_ABORT_ASSOCIATION:
3298f8829a4aSRandall Stewart 	case SCTP_OPERATION_ERROR:
3299f8829a4aSRandall Stewart 	case SCTP_SHUTDOWN_COMPLETE:
3300f8829a4aSRandall Stewart 	case SCTP_ECN_ECHO:
3301f8829a4aSRandall Stewart 	case SCTP_ECN_CWR:
3302f8829a4aSRandall Stewart 	default:
3303f8829a4aSRandall Stewart 		break;
3304f8829a4aSRandall Stewart 	}
3305f8829a4aSRandall Stewart 	return (0);
3306f8829a4aSRandall Stewart }
3307f8829a4aSRandall Stewart 
3308f8829a4aSRandall Stewart void
3309a169d6ecSMichael Tuexen sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3310f8829a4aSRandall Stewart {
3311a169d6ecSMichael Tuexen 	uint32_t i;
3312f8829a4aSRandall Stewart 	uint16_t temp;
3313f8829a4aSRandall Stewart 
3314f8829a4aSRandall Stewart 	/*
331544249214SRandall Stewart 	 * We set things to 0xffffffff since this is the last delivered
331644249214SRandall Stewart 	 * sequence and we will be sending in 0 after the reset.
3317f8829a4aSRandall Stewart 	 */
3318f8829a4aSRandall Stewart 
3319f8829a4aSRandall Stewart 	if (number_entries) {
3320f8829a4aSRandall Stewart 		for (i = 0; i < number_entries; i++) {
3321f8829a4aSRandall Stewart 			temp = ntohs(list[i]);
3322f8829a4aSRandall Stewart 			if (temp >= stcb->asoc.streamincnt) {
3323f8829a4aSRandall Stewart 				continue;
3324f8829a4aSRandall Stewart 			}
332549656eefSMichael Tuexen 			stcb->asoc.strmin[temp].last_mid_delivered = 0xffffffff;
3326f8829a4aSRandall Stewart 		}
3327f8829a4aSRandall Stewart 	} else {
3328f8829a4aSRandall Stewart 		list = NULL;
3329f8829a4aSRandall Stewart 		for (i = 0; i < stcb->asoc.streamincnt; i++) {
333049656eefSMichael Tuexen 			stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
3331f8829a4aSRandall Stewart 		}
3332f8829a4aSRandall Stewart 	}
3333ceaad40aSRandall Stewart 	sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3334f8829a4aSRandall Stewart }
3335f8829a4aSRandall Stewart 
3336f8829a4aSRandall Stewart static void
333756f778aaSMichael Tuexen sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3338f8829a4aSRandall Stewart {
333956f778aaSMichael Tuexen 	uint32_t i;
3340f8829a4aSRandall Stewart 	uint16_t temp;
3341f8829a4aSRandall Stewart 
334256f778aaSMichael Tuexen 	if (number_entries > 0) {
334356f778aaSMichael Tuexen 		for (i = 0; i < number_entries; i++) {
3344f8829a4aSRandall Stewart 			temp = ntohs(list[i]);
3345f8829a4aSRandall Stewart 			if (temp >= stcb->asoc.streamoutcnt) {
3346f8829a4aSRandall Stewart 				/* no such stream */
3347f8829a4aSRandall Stewart 				continue;
3348f8829a4aSRandall Stewart 			}
334963d5b568SMichael Tuexen 			stcb->asoc.strmout[temp].next_mid_ordered = 0;
335063d5b568SMichael Tuexen 			stcb->asoc.strmout[temp].next_mid_unordered = 0;
3351f8829a4aSRandall Stewart 		}
335256f778aaSMichael Tuexen 	} else {
335356f778aaSMichael Tuexen 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
335463d5b568SMichael Tuexen 			stcb->asoc.strmout[i].next_mid_ordered = 0;
335563d5b568SMichael Tuexen 			stcb->asoc.strmout[i].next_mid_unordered = 0;
335656f778aaSMichael Tuexen 		}
3357f8829a4aSRandall Stewart 	}
3358ceaad40aSRandall Stewart 	sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3359f8829a4aSRandall Stewart }
3360f8829a4aSRandall Stewart 
33617cca1775SRandall Stewart static void
33627cca1775SRandall Stewart sctp_reset_clear_pending(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
33637cca1775SRandall Stewart {
33647cca1775SRandall Stewart 	uint32_t i;
33657cca1775SRandall Stewart 	uint16_t temp;
33667cca1775SRandall Stewart 
33677cca1775SRandall Stewart 	if (number_entries > 0) {
33687cca1775SRandall Stewart 		for (i = 0; i < number_entries; i++) {
33697cca1775SRandall Stewart 			temp = ntohs(list[i]);
33707cca1775SRandall Stewart 			if (temp >= stcb->asoc.streamoutcnt) {
33717cca1775SRandall Stewart 				/* no such stream */
33727cca1775SRandall Stewart 				continue;
33737cca1775SRandall Stewart 			}
33747cca1775SRandall Stewart 			stcb->asoc.strmout[temp].state = SCTP_STREAM_OPEN;
33757cca1775SRandall Stewart 		}
33767cca1775SRandall Stewart 	} else {
33777cca1775SRandall Stewart 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
33787cca1775SRandall Stewart 			stcb->asoc.strmout[i].state = SCTP_STREAM_OPEN;
33797cca1775SRandall Stewart 		}
33807cca1775SRandall Stewart 	}
33817cca1775SRandall Stewart }
33827cca1775SRandall Stewart 
338384f3b49aSMichael Tuexen struct sctp_stream_reset_request *
3384f8829a4aSRandall Stewart sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3385f8829a4aSRandall Stewart {
3386f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3387a169d6ecSMichael Tuexen 	struct sctp_chunkhdr *ch;
338884f3b49aSMichael Tuexen 	struct sctp_stream_reset_request *r;
3389f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
3390f8829a4aSRandall Stewart 	int len, clen;
3391f8829a4aSRandall Stewart 
3392f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
3393163153c2SMichael Tuexen 	chk = asoc->str_reset;
3394163153c2SMichael Tuexen 	if (TAILQ_EMPTY(&asoc->control_send_queue) ||
3395163153c2SMichael Tuexen 	    (chk == NULL)) {
3396d06c82f1SRandall Stewart 		asoc->stream_reset_outstanding = 0;
3397f8829a4aSRandall Stewart 		return (NULL);
3398f8829a4aSRandall Stewart 	}
3399f8829a4aSRandall Stewart 	if (chk->data == NULL) {
3400f8829a4aSRandall Stewart 		return (NULL);
3401f8829a4aSRandall Stewart 	}
3402163153c2SMichael Tuexen 	if (bchk != NULL) {
3403f8829a4aSRandall Stewart 		/* he wants a copy of the chk pointer */
3404f8829a4aSRandall Stewart 		*bchk = chk;
3405f8829a4aSRandall Stewart 	}
3406f8829a4aSRandall Stewart 	clen = chk->send_size;
3407a169d6ecSMichael Tuexen 	ch = mtod(chk->data, struct sctp_chunkhdr *);
340884f3b49aSMichael Tuexen 	r = (struct sctp_stream_reset_request *)(ch + 1);
3409f8829a4aSRandall Stewart 	if (ntohl(r->request_seq) == seq) {
3410f8829a4aSRandall Stewart 		/* found it */
3411f8829a4aSRandall Stewart 		return (r);
3412f8829a4aSRandall Stewart 	}
3413f8829a4aSRandall Stewart 	len = SCTP_SIZE32(ntohs(r->ph.param_length));
3414f8829a4aSRandall Stewart 	if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3415f8829a4aSRandall Stewart 		/* move to the next one, there can only be a max of two */
341684f3b49aSMichael Tuexen 		r = (struct sctp_stream_reset_request *)((caddr_t)r + len);
3417f8829a4aSRandall Stewart 		if (ntohl(r->request_seq) == seq) {
3418f8829a4aSRandall Stewart 			return (r);
3419f8829a4aSRandall Stewart 		}
3420f8829a4aSRandall Stewart 	}
3421f8829a4aSRandall Stewart 	/* that seq is not here */
3422f8829a4aSRandall Stewart 	return (NULL);
3423f8829a4aSRandall Stewart }
3424f8829a4aSRandall Stewart 
3425f8829a4aSRandall Stewart static void
3426f8829a4aSRandall Stewart sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3427f8829a4aSRandall Stewart {
3428f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3429cd6340caSMichael Tuexen 	struct sctp_tmit_chunk *chk;
3430f8829a4aSRandall Stewart 
3431cd6340caSMichael Tuexen 	asoc = &stcb->asoc;
3432cd6340caSMichael Tuexen 	chk = asoc->str_reset;
3433cd6340caSMichael Tuexen 	if (chk == NULL) {
3434f8829a4aSRandall Stewart 		return;
3435f8829a4aSRandall Stewart 	}
3436cd6340caSMichael Tuexen 	asoc->str_reset = NULL;
3437b7d130beSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb,
34386fb7b4fbSMichael Tuexen 	    NULL, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28);
3439cd6340caSMichael Tuexen 	TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
3440cd6340caSMichael Tuexen 	asoc->ctrl_queue_cnt--;
3441f8829a4aSRandall Stewart 	if (chk->data) {
3442f8829a4aSRandall Stewart 		sctp_m_freem(chk->data);
3443f8829a4aSRandall Stewart 		chk->data = NULL;
3444f8829a4aSRandall Stewart 	}
3445689e6a5fSMichael Tuexen 	sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3446f8829a4aSRandall Stewart }
3447f8829a4aSRandall Stewart 
3448f8829a4aSRandall Stewart static int
3449f8829a4aSRandall Stewart sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3450f8829a4aSRandall Stewart     uint32_t seq, uint32_t action,
345108598d70SRandall Stewart     struct sctp_stream_reset_response *respin)
3452f8829a4aSRandall Stewart {
3453f8829a4aSRandall Stewart 	uint16_t type;
3454f4358911SMichael Tuexen 	int lparam_len;
3455f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3456f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
345784f3b49aSMichael Tuexen 	struct sctp_stream_reset_request *req_param;
345884f3b49aSMichael Tuexen 	struct sctp_stream_reset_out_request *req_out_param;
345984f3b49aSMichael Tuexen 	struct sctp_stream_reset_in_request *req_in_param;
346056f778aaSMichael Tuexen 	uint32_t number_entries;
3461f8829a4aSRandall Stewart 
3462f8829a4aSRandall Stewart 	if (asoc->stream_reset_outstanding == 0) {
3463f8829a4aSRandall Stewart 		/* duplicate */
3464f8829a4aSRandall Stewart 		return (0);
3465f8829a4aSRandall Stewart 	}
3466f8829a4aSRandall Stewart 	if (seq == stcb->asoc.str_reset_seq_out) {
346784f3b49aSMichael Tuexen 		req_param = sctp_find_stream_reset(stcb, seq, &chk);
346884f3b49aSMichael Tuexen 		if (req_param != NULL) {
3469f8829a4aSRandall Stewart 			stcb->asoc.str_reset_seq_out++;
347084f3b49aSMichael Tuexen 			type = ntohs(req_param->ph.param_type);
3471f4358911SMichael Tuexen 			lparam_len = ntohs(req_param->ph.param_length);
3472f8829a4aSRandall Stewart 			if (type == SCTP_STR_RESET_OUT_REQUEST) {
34737cca1775SRandall Stewart 				int no_clear = 0;
34747cca1775SRandall Stewart 
347584f3b49aSMichael Tuexen 				req_out_param = (struct sctp_stream_reset_out_request *)req_param;
3476f4358911SMichael Tuexen 				number_entries = (lparam_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3477f8829a4aSRandall Stewart 				asoc->stream_reset_out_is_outstanding = 0;
3478f8829a4aSRandall Stewart 				if (asoc->stream_reset_outstanding)
3479f8829a4aSRandall Stewart 					asoc->stream_reset_outstanding--;
3480f3ebe64cSMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3481f8829a4aSRandall Stewart 					/* do it */
348284f3b49aSMichael Tuexen 					sctp_reset_out_streams(stcb, number_entries, req_out_param->list_of_streams);
3483d4260646SMichael Tuexen 				} else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
348484f3b49aSMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
34857cca1775SRandall Stewart 				} else if (action == SCTP_STREAM_RESET_RESULT_IN_PROGRESS) {
3486b7b84c0eSMichael Tuexen 					/*
3487b7b84c0eSMichael Tuexen 					 * Set it up so we don't stop
3488b7b84c0eSMichael Tuexen 					 * retransmitting
3489b7b84c0eSMichael Tuexen 					 */
3490a4889f2dSMichael Tuexen 					asoc->stream_reset_outstanding++;
34917cca1775SRandall Stewart 					stcb->asoc.str_reset_seq_out--;
34927cca1775SRandall Stewart 					asoc->stream_reset_out_is_outstanding = 1;
34937cca1775SRandall Stewart 					no_clear = 1;
3494f8829a4aSRandall Stewart 				} else {
349584f3b49aSMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3496f8829a4aSRandall Stewart 				}
34977cca1775SRandall Stewart 				if (no_clear == 0) {
34987cca1775SRandall Stewart 					sctp_reset_clear_pending(stcb, number_entries, req_out_param->list_of_streams);
34997cca1775SRandall Stewart 				}
3500f8829a4aSRandall Stewart 			} else if (type == SCTP_STR_RESET_IN_REQUEST) {
350184f3b49aSMichael Tuexen 				req_in_param = (struct sctp_stream_reset_in_request *)req_param;
3502f4358911SMichael Tuexen 				number_entries = (lparam_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3503f8829a4aSRandall Stewart 				if (asoc->stream_reset_outstanding)
3504f8829a4aSRandall Stewart 					asoc->stream_reset_outstanding--;
3505d4260646SMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3506d4260646SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
350784f3b49aSMichael Tuexen 					    number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3508d4260646SMichael Tuexen 				} else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3509c4e848b7SRandall Stewart 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
351084f3b49aSMichael Tuexen 					    number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3511f8829a4aSRandall Stewart 				}
3512c4e848b7SRandall Stewart 			} else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3513ea44232bSRandall Stewart 				/* Ok we now may have more streams */
3514c4e848b7SRandall Stewart 				int num_stream;
3515c4e848b7SRandall Stewart 
3516c4e848b7SRandall Stewart 				num_stream = stcb->asoc.strm_pending_add_size;
3517c4e848b7SRandall Stewart 				if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3518c4e848b7SRandall Stewart 					/* TSNH */
3519c4e848b7SRandall Stewart 					num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3520c4e848b7SRandall Stewart 				}
3521c4e848b7SRandall Stewart 				stcb->asoc.strm_pending_add_size = 0;
35228aae9493SRandall Stewart 				if (asoc->stream_reset_outstanding)
35238aae9493SRandall Stewart 					asoc->stream_reset_outstanding--;
3524f3ebe64cSMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3525ea44232bSRandall Stewart 					/* Put the new streams into effect */
35267cca1775SRandall Stewart 					int i;
35277cca1775SRandall Stewart 
35287cca1775SRandall Stewart 					for (i = asoc->streamoutcnt; i < (asoc->streamoutcnt + num_stream); i++) {
35297cca1775SRandall Stewart 						asoc->strmout[i].state = SCTP_STREAM_OPEN;
35307cca1775SRandall Stewart 					}
35317cca1775SRandall Stewart 					asoc->streamoutcnt += num_stream;
3532c4e848b7SRandall Stewart 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3533d4260646SMichael Tuexen 				} else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3534d4260646SMichael Tuexen 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3535d4260646SMichael Tuexen 					    SCTP_STREAM_CHANGE_DENIED);
3536ea44232bSRandall Stewart 				} else {
3537c4e848b7SRandall Stewart 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3538d4260646SMichael Tuexen 					    SCTP_STREAM_CHANGE_FAILED);
3539c4e848b7SRandall Stewart 				}
3540c4e848b7SRandall Stewart 			} else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3541c4e848b7SRandall Stewart 				if (asoc->stream_reset_outstanding)
3542c4e848b7SRandall Stewart 					asoc->stream_reset_outstanding--;
3543d4260646SMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3544c4e848b7SRandall Stewart 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3545d4260646SMichael Tuexen 					    SCTP_STREAM_CHANGE_DENIED);
3546d4260646SMichael Tuexen 				} else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3547d4260646SMichael Tuexen 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3548d4260646SMichael Tuexen 					    SCTP_STREAM_CHANGE_FAILED);
3549ea44232bSRandall Stewart 				}
3550f8829a4aSRandall Stewart 			} else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3551f8829a4aSRandall Stewart 				/**
3552f8829a4aSRandall Stewart 				 * a) Adopt the new in tsn.
3553f8829a4aSRandall Stewart 				 * b) reset the map
3554f8829a4aSRandall Stewart 				 * c) Adopt the new out-tsn
3555f8829a4aSRandall Stewart 				 */
3556f8829a4aSRandall Stewart 				struct sctp_stream_reset_response_tsn *resp;
3557f8829a4aSRandall Stewart 				struct sctp_forward_tsn_chunk fwdtsn;
3558f8829a4aSRandall Stewart 				int abort_flag = 0;
3559f8829a4aSRandall Stewart 
3560f8829a4aSRandall Stewart 				if (respin == NULL) {
3561f8829a4aSRandall Stewart 					/* huh ? */
3562f8829a4aSRandall Stewart 					return (0);
3563f8829a4aSRandall Stewart 				}
35646a58f0e9SXin LI 				if (ntohs(respin->ph.param_length) < sizeof(struct sctp_stream_reset_response_tsn)) {
35656a58f0e9SXin LI 					return (0);
35666a58f0e9SXin LI 				}
3567f3ebe64cSMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3568f8829a4aSRandall Stewart 					resp = (struct sctp_stream_reset_response_tsn *)respin;
3569f8829a4aSRandall Stewart 					asoc->stream_reset_outstanding--;
3570f8829a4aSRandall Stewart 					fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3571f8829a4aSRandall Stewart 					fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3572f8829a4aSRandall Stewart 					fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3573671d309cSRandall Stewart 					sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3574f8829a4aSRandall Stewart 					if (abort_flag) {
3575f8829a4aSRandall Stewart 						return (1);
3576f8829a4aSRandall Stewart 					}
3577f8829a4aSRandall Stewart 					stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3578b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3579b3f1ea41SRandall Stewart 						sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3580b3f1ea41SRandall Stewart 					}
35810053ed28SMichael Tuexen 
3582d6af161aSRandall Stewart 					stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3583f8829a4aSRandall Stewart 					stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3584f8829a4aSRandall Stewart 					memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3585830d754dSRandall Stewart 
3586830d754dSRandall Stewart 					stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3587b5c16493SMichael Tuexen 					memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
358877acdc25SRandall Stewart 
3589f8829a4aSRandall Stewart 					stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3590f8829a4aSRandall Stewart 					stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3591f8829a4aSRandall Stewart 
3592f8829a4aSRandall Stewart 					sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3593f8829a4aSRandall Stewart 					sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3594c4e848b7SRandall Stewart 					sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3595d4260646SMichael Tuexen 				} else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3596d4260646SMichael Tuexen 					sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3597d4260646SMichael Tuexen 					    SCTP_ASSOC_RESET_DENIED);
3598c4e848b7SRandall Stewart 				} else {
3599c4e848b7SRandall Stewart 					sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3600d4260646SMichael Tuexen 					    SCTP_ASSOC_RESET_FAILED);
3601f8829a4aSRandall Stewart 				}
3602f8829a4aSRandall Stewart 			}
3603f8829a4aSRandall Stewart 			/* get rid of the request and get the request flags */
3604f8829a4aSRandall Stewart 			if (asoc->stream_reset_outstanding == 0) {
3605f8829a4aSRandall Stewart 				sctp_clean_up_stream_reset(stcb);
3606f8829a4aSRandall Stewart 			}
3607f8829a4aSRandall Stewart 		}
3608f8829a4aSRandall Stewart 	}
36097cca1775SRandall Stewart 	if (asoc->stream_reset_outstanding == 0) {
3610c6168599SRandall Stewart 		sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
36117cca1775SRandall Stewart 	}
3612f8829a4aSRandall Stewart 	return (0);
3613f8829a4aSRandall Stewart }
3614f8829a4aSRandall Stewart 
3615f8829a4aSRandall Stewart static void
3616f8829a4aSRandall Stewart sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3617f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk,
3618671d309cSRandall Stewart     struct sctp_stream_reset_in_request *req, int trunc)
3619f8829a4aSRandall Stewart {
3620f8829a4aSRandall Stewart 	uint32_t seq;
3621f8829a4aSRandall Stewart 	int len, i;
3622f8829a4aSRandall Stewart 	int number_entries;
3623f8829a4aSRandall Stewart 	uint16_t temp;
3624f8829a4aSRandall Stewart 
3625f8829a4aSRandall Stewart 	/*
3626f8829a4aSRandall Stewart 	 * peer wants me to send a str-reset to him for my outgoing seq's if
3627f8829a4aSRandall Stewart 	 * seq_in is right.
3628f8829a4aSRandall Stewart 	 */
3629f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3630f8829a4aSRandall Stewart 
3631f8829a4aSRandall Stewart 	seq = ntohl(req->request_seq);
3632f8829a4aSRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3633671d309cSRandall Stewart 		asoc->last_reset_action[1] = asoc->last_reset_action[0];
3634f3ebe64cSMichael Tuexen 		if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3635f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3636f3ebe64cSMichael Tuexen 		} else if (trunc) {
3637f3ebe64cSMichael Tuexen 			/* Can't do it, since they exceeded our buffer size  */
3638f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3639671d309cSRandall Stewart 		} else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3640f8829a4aSRandall Stewart 			len = ntohs(req->ph.param_length);
3641f8829a4aSRandall Stewart 			number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
36427cca1775SRandall Stewart 			if (number_entries) {
3643f8829a4aSRandall Stewart 				for (i = 0; i < number_entries; i++) {
3644f8829a4aSRandall Stewart 					temp = ntohs(req->list_of_streams[i]);
36457cca1775SRandall Stewart 					if (temp >= stcb->asoc.streamoutcnt) {
36467cca1775SRandall Stewart 						asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
36477cca1775SRandall Stewart 						goto bad_boy;
36487cca1775SRandall Stewart 					}
3649f8829a4aSRandall Stewart 					req->list_of_streams[i] = temp;
3650f8829a4aSRandall Stewart 				}
36517cca1775SRandall Stewart 				for (i = 0; i < number_entries; i++) {
36527cca1775SRandall Stewart 					if (stcb->asoc.strmout[req->list_of_streams[i]].state == SCTP_STREAM_OPEN) {
36537cca1775SRandall Stewart 						stcb->asoc.strmout[req->list_of_streams[i]].state = SCTP_STREAM_RESET_PENDING;
36547cca1775SRandall Stewart 					}
36557cca1775SRandall Stewart 				}
36567cca1775SRandall Stewart 			} else {
36577cca1775SRandall Stewart 				/* Its all */
36587cca1775SRandall Stewart 				for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
36597cca1775SRandall Stewart 					if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN)
36607cca1775SRandall Stewart 						stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
36617cca1775SRandall Stewart 				}
36627cca1775SRandall Stewart 			}
3663f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3664f8829a4aSRandall Stewart 		} else {
3665f8829a4aSRandall Stewart 			/* Can't do it, since we have sent one out */
3666f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3667f8829a4aSRandall Stewart 		}
36687cca1775SRandall Stewart bad_boy:
3669c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3670f8829a4aSRandall Stewart 		asoc->str_reset_seq_in++;
3671f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 1 == seq) {
3672f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3673f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 2 == seq) {
3674f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3675f8829a4aSRandall Stewart 	} else {
3676f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3677f8829a4aSRandall Stewart 	}
3678c6168599SRandall Stewart 	sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3679f8829a4aSRandall Stewart }
3680f8829a4aSRandall Stewart 
3681f8829a4aSRandall Stewart static int
3682f8829a4aSRandall Stewart sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3683f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk,
3684f8829a4aSRandall Stewart     struct sctp_stream_reset_tsn_request *req)
3685f8829a4aSRandall Stewart {
3686f8829a4aSRandall Stewart 	/* reset all in and out and update the tsn */
3687f8829a4aSRandall Stewart 	/*
3688f8829a4aSRandall Stewart 	 * A) reset my str-seq's on in and out. B) Select a receive next,
3689f8829a4aSRandall Stewart 	 * and set cum-ack to it. Also process this selected number as a
3690f8829a4aSRandall Stewart 	 * fwd-tsn as well. C) set in the response my next sending seq.
3691f8829a4aSRandall Stewart 	 */
3692f8829a4aSRandall Stewart 	struct sctp_forward_tsn_chunk fwdtsn;
3693f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3694f8829a4aSRandall Stewart 	int abort_flag = 0;
3695f8829a4aSRandall Stewart 	uint32_t seq;
3696f8829a4aSRandall Stewart 
3697f8829a4aSRandall Stewart 	seq = ntohl(req->request_seq);
3698f8829a4aSRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3699ce228dabSMichael Tuexen 		asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
3700f3ebe64cSMichael Tuexen 		if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3701f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3702ce228dabSMichael Tuexen 		} else {
3703f8829a4aSRandall Stewart 			fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3704f8829a4aSRandall Stewart 			fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3705f8829a4aSRandall Stewart 			fwdtsn.ch.chunk_flags = 0;
3706f8829a4aSRandall Stewart 			fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3707671d309cSRandall Stewart 			sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3708f8829a4aSRandall Stewart 			if (abort_flag) {
3709f8829a4aSRandall Stewart 				return (1);
3710f8829a4aSRandall Stewart 			}
3711f3ebe64cSMichael Tuexen 			asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3712b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3713b3f1ea41SRandall Stewart 				sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3714b3f1ea41SRandall Stewart 			}
3715f3ebe64cSMichael Tuexen 			asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3716f3ebe64cSMichael Tuexen 			asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3717f3ebe64cSMichael Tuexen 			memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3718f3ebe64cSMichael Tuexen 			asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3719f3ebe64cSMichael Tuexen 			memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3720f3ebe64cSMichael Tuexen 			atomic_add_int(&asoc->sending_seq, 1);
3721f8829a4aSRandall Stewart 			/* save off historical data for retrans */
3722f3ebe64cSMichael Tuexen 			asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3723f3ebe64cSMichael Tuexen 			asoc->last_sending_seq[0] = asoc->sending_seq;
3724f3ebe64cSMichael Tuexen 			asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3725f3ebe64cSMichael Tuexen 			asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3726f8829a4aSRandall Stewart 			sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3727f8829a4aSRandall Stewart 			sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3728f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3729f3ebe64cSMichael Tuexen 			sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3730ce228dabSMichael Tuexen 		}
3731ce228dabSMichael Tuexen 		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3732ce228dabSMichael Tuexen 		    asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3733f8829a4aSRandall Stewart 		asoc->str_reset_seq_in++;
3734f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 1 == seq) {
3735f8829a4aSRandall Stewart 		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3736f3ebe64cSMichael Tuexen 		    asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3737f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 2 == seq) {
3738f8829a4aSRandall Stewart 		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3739f3ebe64cSMichael Tuexen 		    asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3740f8829a4aSRandall Stewart 	} else {
3741f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3742f8829a4aSRandall Stewart 	}
3743f8829a4aSRandall Stewart 	return (0);
3744f8829a4aSRandall Stewart }
3745f8829a4aSRandall Stewart 
3746f8829a4aSRandall Stewart static void
3747f8829a4aSRandall Stewart sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3748f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk,
3749671d309cSRandall Stewart     struct sctp_stream_reset_out_request *req, int trunc)
3750f8829a4aSRandall Stewart {
3751f8829a4aSRandall Stewart 	uint32_t seq, tsn;
3752f8829a4aSRandall Stewart 	int number_entries, len;
3753f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3754f8829a4aSRandall Stewart 
3755f8829a4aSRandall Stewart 	seq = ntohl(req->request_seq);
3756f8829a4aSRandall Stewart 
3757f8829a4aSRandall Stewart 	/* now if its not a duplicate we process it */
3758f8829a4aSRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3759f8829a4aSRandall Stewart 		len = ntohs(req->ph.param_length);
3760f8829a4aSRandall Stewart 		number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3761f8829a4aSRandall Stewart 		/*
3762f8829a4aSRandall Stewart 		 * the sender is resetting, handle the list issue.. we must
3763f8829a4aSRandall Stewart 		 * a) verify if we can do the reset, if so no problem b) If
3764f8829a4aSRandall Stewart 		 * we can't do the reset we must copy the request. c) queue
3765f8829a4aSRandall Stewart 		 * it, and setup the data in processor to trigger it off
3766f8829a4aSRandall Stewart 		 * when needed and dequeue all the queued data.
3767f8829a4aSRandall Stewart 		 */
3768f8829a4aSRandall Stewart 		tsn = ntohl(req->send_reset_at_tsn);
3769f8829a4aSRandall Stewart 
3770f8829a4aSRandall Stewart 		/* move the reset action back one */
3771f8829a4aSRandall Stewart 		asoc->last_reset_action[1] = asoc->last_reset_action[0];
3772f3ebe64cSMichael Tuexen 		if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3773f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3774f3ebe64cSMichael Tuexen 		} else if (trunc) {
3775f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
377620b07a4dSMichael Tuexen 		} else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3777f8829a4aSRandall Stewart 			/* we can do it now */
3778f8829a4aSRandall Stewart 			sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3779f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3780f8829a4aSRandall Stewart 		} else {
3781f8829a4aSRandall Stewart 			/*
3782f8829a4aSRandall Stewart 			 * we must queue it up and thus wait for the TSN's
3783f8829a4aSRandall Stewart 			 * to arrive that are at or before tsn
3784f8829a4aSRandall Stewart 			 */
3785f8829a4aSRandall Stewart 			struct sctp_stream_reset_list *liste;
3786f8829a4aSRandall Stewart 			int siz;
3787f8829a4aSRandall Stewart 
3788f8829a4aSRandall Stewart 			siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3789f8829a4aSRandall Stewart 			SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3790207304d4SRandall Stewart 			    siz, SCTP_M_STRESET);
3791f8829a4aSRandall Stewart 			if (liste == NULL) {
3792f8829a4aSRandall Stewart 				/* gak out of memory */
3793f3ebe64cSMichael Tuexen 				asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3794f3ebe64cSMichael Tuexen 				sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3795f8829a4aSRandall Stewart 				return;
3796f8829a4aSRandall Stewart 			}
37977cca1775SRandall Stewart 			liste->seq = seq;
3798f8829a4aSRandall Stewart 			liste->tsn = tsn;
3799f8829a4aSRandall Stewart 			liste->number_entries = number_entries;
3800a169d6ecSMichael Tuexen 			memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t));
3801f8829a4aSRandall Stewart 			TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
38027cca1775SRandall Stewart 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_IN_PROGRESS;
3803f8829a4aSRandall Stewart 		}
3804c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3805f8829a4aSRandall Stewart 		asoc->str_reset_seq_in++;
3806f8829a4aSRandall Stewart 	} else if ((asoc->str_reset_seq_in - 1) == seq) {
3807f8829a4aSRandall Stewart 		/*
3808f8829a4aSRandall Stewart 		 * one seq back, just echo back last action since my
3809f8829a4aSRandall Stewart 		 * response was lost.
3810f8829a4aSRandall Stewart 		 */
3811f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3812f8829a4aSRandall Stewart 	} else if ((asoc->str_reset_seq_in - 2) == seq) {
3813f8829a4aSRandall Stewart 		/*
3814f8829a4aSRandall Stewart 		 * two seq back, just echo back last action since my
3815f8829a4aSRandall Stewart 		 * response was lost.
3816f8829a4aSRandall Stewart 		 */
3817f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3818f8829a4aSRandall Stewart 	} else {
3819f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3820f8829a4aSRandall Stewart 	}
3821f8829a4aSRandall Stewart }
3822f8829a4aSRandall Stewart 
3823ea44232bSRandall Stewart static void
3824ea44232bSRandall Stewart sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3825ea44232bSRandall Stewart     struct sctp_stream_reset_add_strm *str_add)
3826ea44232bSRandall Stewart {
3827ea44232bSRandall Stewart 	/*
3828ea44232bSRandall Stewart 	 * Peer is requesting to add more streams. If its within our
3829ea44232bSRandall Stewart 	 * max-streams we will allow it.
3830ea44232bSRandall Stewart 	 */
3831c4e848b7SRandall Stewart 	uint32_t num_stream, i;
3832ea44232bSRandall Stewart 	uint32_t seq;
38338aae9493SRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
38344a9ef3f8SMichael Tuexen 	struct sctp_queued_to_read *ctl, *nctl;
3835ea44232bSRandall Stewart 
3836ea44232bSRandall Stewart 	/* Get the number. */
3837ea44232bSRandall Stewart 	seq = ntohl(str_add->request_seq);
3838ea44232bSRandall Stewart 	num_stream = ntohs(str_add->number_of_streams);
3839ea44232bSRandall Stewart 	/* Now what would be the new total? */
38408aae9493SRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3841ea44232bSRandall Stewart 		num_stream += stcb->asoc.streamincnt;
3842f3ebe64cSMichael Tuexen 		stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3843f3ebe64cSMichael Tuexen 		if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3844f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3845f3ebe64cSMichael Tuexen 		} else if ((num_stream > stcb->asoc.max_inbound_streams) ||
3846c4e848b7SRandall Stewart 		    (num_stream > 0xffff)) {
3847ea44232bSRandall Stewart 			/* We must reject it they ask for to many */
3848ea44232bSRandall Stewart 	denied:
3849f3ebe64cSMichael Tuexen 			stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3850ea44232bSRandall Stewart 		} else {
3851ea44232bSRandall Stewart 			/* Ok, we can do that :-) */
3852ea44232bSRandall Stewart 			struct sctp_stream_in *oldstrm;
3853ea44232bSRandall Stewart 
3854ea44232bSRandall Stewart 			/* save off the old */
3855ea44232bSRandall Stewart 			oldstrm = stcb->asoc.strmin;
3856ea44232bSRandall Stewart 			SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
3857ea44232bSRandall Stewart 			    (num_stream * sizeof(struct sctp_stream_in)),
3858ea44232bSRandall Stewart 			    SCTP_M_STRMI);
3859ea44232bSRandall Stewart 			if (stcb->asoc.strmin == NULL) {
3860ea44232bSRandall Stewart 				stcb->asoc.strmin = oldstrm;
3861ea44232bSRandall Stewart 				goto denied;
3862ea44232bSRandall Stewart 			}
3863ea44232bSRandall Stewart 			/* copy off the old data */
38648aae9493SRandall Stewart 			for (i = 0; i < stcb->asoc.streamincnt; i++) {
38658aae9493SRandall Stewart 				TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
386644249214SRandall Stewart 				TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
386749656eefSMichael Tuexen 				stcb->asoc.strmin[i].sid = i;
386849656eefSMichael Tuexen 				stcb->asoc.strmin[i].last_mid_delivered = oldstrm[i].last_mid_delivered;
38698aae9493SRandall Stewart 				stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
387044249214SRandall Stewart 				stcb->asoc.strmin[i].pd_api_started = oldstrm[i].pd_api_started;
38718aae9493SRandall Stewart 				/* now anything on those queues? */
387244249214SRandall Stewart 				TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next_instrm, nctl) {
387344249214SRandall Stewart 					TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next_instrm);
387444249214SRandall Stewart 					TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next_instrm);
387544249214SRandall Stewart 				}
387644249214SRandall Stewart 				TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].uno_inqueue, next_instrm, nctl) {
387744249214SRandall Stewart 					TAILQ_REMOVE(&oldstrm[i].uno_inqueue, ctl, next_instrm);
387844249214SRandall Stewart 					TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].uno_inqueue, ctl, next_instrm);
38798aae9493SRandall Stewart 				}
38808aae9493SRandall Stewart 			}
3881ea44232bSRandall Stewart 			/* Init the new streams */
3882ea44232bSRandall Stewart 			for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
3883ea44232bSRandall Stewart 				TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
388444249214SRandall Stewart 				TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
388549656eefSMichael Tuexen 				stcb->asoc.strmin[i].sid = i;
388649656eefSMichael Tuexen 				stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
388744249214SRandall Stewart 				stcb->asoc.strmin[i].pd_api_started = 0;
3888ea44232bSRandall Stewart 				stcb->asoc.strmin[i].delivery_started = 0;
3889ea44232bSRandall Stewart 			}
3890ea44232bSRandall Stewart 			SCTP_FREE(oldstrm, SCTP_M_STRMI);
3891ea44232bSRandall Stewart 			/* update the size */
3892ea44232bSRandall Stewart 			stcb->asoc.streamincnt = num_stream;
3893f3ebe64cSMichael Tuexen 			stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3894c4e848b7SRandall Stewart 			sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3895ea44232bSRandall Stewart 		}
3896c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3897c4e848b7SRandall Stewart 		asoc->str_reset_seq_in++;
38988aae9493SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 1) == seq) {
38998aae9493SRandall Stewart 		/*
39008aae9493SRandall Stewart 		 * one seq back, just echo back last action since my
39018aae9493SRandall Stewart 		 * response was lost.
39028aae9493SRandall Stewart 		 */
39038aae9493SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
39048aae9493SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 2) == seq) {
39058aae9493SRandall Stewart 		/*
39068aae9493SRandall Stewart 		 * two seq back, just echo back last action since my
39078aae9493SRandall Stewart 		 * response was lost.
39088aae9493SRandall Stewart 		 */
39098aae9493SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
39108aae9493SRandall Stewart 	} else {
3911f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
39128aae9493SRandall Stewart 	}
3913ea44232bSRandall Stewart }
3914ea44232bSRandall Stewart 
3915c4e848b7SRandall Stewart static void
3916c4e848b7SRandall Stewart sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3917c4e848b7SRandall Stewart     struct sctp_stream_reset_add_strm *str_add)
3918c4e848b7SRandall Stewart {
3919c4e848b7SRandall Stewart 	/*
3920c4e848b7SRandall Stewart 	 * Peer is requesting to add more streams. If its within our
3921c4e848b7SRandall Stewart 	 * max-streams we will allow it.
3922c4e848b7SRandall Stewart 	 */
3923c4e848b7SRandall Stewart 	uint16_t num_stream;
3924c4e848b7SRandall Stewart 	uint32_t seq;
3925c4e848b7SRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3926c4e848b7SRandall Stewart 
3927c4e848b7SRandall Stewart 	/* Get the number. */
3928c4e848b7SRandall Stewart 	seq = ntohl(str_add->request_seq);
3929c4e848b7SRandall Stewart 	num_stream = ntohs(str_add->number_of_streams);
3930c4e848b7SRandall Stewart 	/* Now what would be the new total? */
3931c4e848b7SRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3932c4e848b7SRandall Stewart 		stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3933f3ebe64cSMichael Tuexen 		if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3934f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3935f3ebe64cSMichael Tuexen 		} else if (stcb->asoc.stream_reset_outstanding) {
3936f3ebe64cSMichael Tuexen 			/* We must reject it we have something pending */
3937f3ebe64cSMichael Tuexen 			stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3938c4e848b7SRandall Stewart 		} else {
3939c4e848b7SRandall Stewart 			/* Ok, we can do that :-) */
3940c4e848b7SRandall Stewart 			int mychk;
3941c4e848b7SRandall Stewart 
3942c4e848b7SRandall Stewart 			mychk = stcb->asoc.streamoutcnt;
3943c4e848b7SRandall Stewart 			mychk += num_stream;
3944c4e848b7SRandall Stewart 			if (mychk < 0x10000) {
3945f3ebe64cSMichael Tuexen 				stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
39467cca1775SRandall Stewart 				if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 1, num_stream, 0, 1)) {
3947f3ebe64cSMichael Tuexen 					stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3948c4e848b7SRandall Stewart 				}
3949c4e848b7SRandall Stewart 			} else {
3950f3ebe64cSMichael Tuexen 				stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3951c4e848b7SRandall Stewart 			}
3952c4e848b7SRandall Stewart 		}
3953c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
3954c4e848b7SRandall Stewart 		asoc->str_reset_seq_in++;
3955c4e848b7SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 1) == seq) {
3956c4e848b7SRandall Stewart 		/*
3957c4e848b7SRandall Stewart 		 * one seq back, just echo back last action since my
3958c4e848b7SRandall Stewart 		 * response was lost.
3959c4e848b7SRandall Stewart 		 */
3960c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3961c4e848b7SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 2) == seq) {
3962c4e848b7SRandall Stewart 		/*
3963c4e848b7SRandall Stewart 		 * two seq back, just echo back last action since my
3964c4e848b7SRandall Stewart 		 * response was lost.
3965c4e848b7SRandall Stewart 		 */
3966c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3967c4e848b7SRandall Stewart 	} else {
3968f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3969c4e848b7SRandall Stewart 	}
3970c4e848b7SRandall Stewart }
3971c4e848b7SRandall Stewart 
3972671d309cSRandall Stewart #ifdef __GNUC__
3973671d309cSRandall Stewart __attribute__((noinline))
3974671d309cSRandall Stewart #endif
3975f8829a4aSRandall Stewart static int
3976671d309cSRandall Stewart sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3977a169d6ecSMichael Tuexen     struct sctp_chunkhdr *ch_req)
3978f8829a4aSRandall Stewart {
39796a58f0e9SXin LI 	uint16_t remaining_length, param_len, ptype;
3980671d309cSRandall Stewart 	struct sctp_paramhdr pstore;
3981671d309cSRandall Stewart 	uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
3982c4e848b7SRandall Stewart 	uint32_t seq = 0;
3983f8829a4aSRandall Stewart 	int num_req = 0;
3984671d309cSRandall Stewart 	int trunc = 0;
3985f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
3986f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
3987f8829a4aSRandall Stewart 	struct sctp_paramhdr *ph;
3988f42a358aSRandall Stewart 	int ret_code = 0;
3989f42a358aSRandall Stewart 	int num_param = 0;
3990f8829a4aSRandall Stewart 
3991f8829a4aSRandall Stewart 	/* now it may be a reset or a reset-response */
39926a58f0e9SXin LI 	remaining_length = ntohs(ch_req->chunk_length) - sizeof(struct sctp_chunkhdr);
3993f8829a4aSRandall Stewart 
3994f8829a4aSRandall Stewart 	/* setup for adding the response */
3995f8829a4aSRandall Stewart 	sctp_alloc_a_chunk(stcb, chk);
3996f8829a4aSRandall Stewart 	if (chk == NULL) {
3997f8829a4aSRandall Stewart 		return (ret_code);
3998f8829a4aSRandall Stewart 	}
3999e03159eaSMichael Tuexen 	chk->copy_by_ref = 0;
4000f8829a4aSRandall Stewart 	chk->rec.chunk_id.id = SCTP_STREAM_RESET;
4001d06c82f1SRandall Stewart 	chk->rec.chunk_id.can_take_data = 0;
4002e03159eaSMichael Tuexen 	chk->flags = 0;
4003f8829a4aSRandall Stewart 	chk->asoc = &stcb->asoc;
4004f8829a4aSRandall Stewart 	chk->no_fr_allowed = 0;
4005f8829a4aSRandall Stewart 	chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
400644b7479bSRandall Stewart 	chk->book_size_scale = 0;
4007eb1b1807SGleb Smirnoff 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
4008f8829a4aSRandall Stewart 	if (chk->data == NULL) {
4009f8829a4aSRandall Stewart strres_nochunk:
4010f8829a4aSRandall Stewart 		if (chk->data) {
4011f8829a4aSRandall Stewart 			sctp_m_freem(chk->data);
4012f8829a4aSRandall Stewart 			chk->data = NULL;
4013f8829a4aSRandall Stewart 		}
4014689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4015f8829a4aSRandall Stewart 		return (ret_code);
4016f8829a4aSRandall Stewart 	}
4017139bc87fSRandall Stewart 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4018f8829a4aSRandall Stewart 
4019f8829a4aSRandall Stewart 	/* setup chunk parameters */
4020f8829a4aSRandall Stewart 	chk->sent = SCTP_DATAGRAM_UNSENT;
4021f8829a4aSRandall Stewart 	chk->snd_count = 0;
4022ca85e948SMichael Tuexen 	chk->whoTo = NULL;
4023f8829a4aSRandall Stewart 
4024f8829a4aSRandall Stewart 	ch = mtod(chk->data, struct sctp_chunkhdr *);
4025f8829a4aSRandall Stewart 	ch->chunk_type = SCTP_STREAM_RESET;
4026f8829a4aSRandall Stewart 	ch->chunk_flags = 0;
4027f8829a4aSRandall Stewart 	ch->chunk_length = htons(chk->send_size);
4028139bc87fSRandall Stewart 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4029671d309cSRandall Stewart 	offset += sizeof(struct sctp_chunkhdr);
40306a58f0e9SXin LI 	while (remaining_length >= sizeof(struct sctp_paramhdr)) {
4031671d309cSRandall Stewart 		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *)&pstore);
40326a58f0e9SXin LI 		if (ph == NULL) {
40336a58f0e9SXin LI 			/* TSNH */
4034f8829a4aSRandall Stewart 			break;
4035f8829a4aSRandall Stewart 		}
40366a58f0e9SXin LI 		param_len = ntohs(ph->param_length);
40376a58f0e9SXin LI 		if ((param_len > remaining_length) ||
40386a58f0e9SXin LI 		    (param_len < (sizeof(struct sctp_paramhdr) + sizeof(uint32_t)))) {
40396a58f0e9SXin LI 			/* bad parameter length */
40406a58f0e9SXin LI 			break;
40416a58f0e9SXin LI 		}
40426a58f0e9SXin LI 		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, sizeof(cstore)),
4043671d309cSRandall Stewart 		    (uint8_t *)&cstore);
40446a58f0e9SXin LI 		if (ph == NULL) {
40456a58f0e9SXin LI 			/* TSNH */
40466a58f0e9SXin LI 			break;
40476a58f0e9SXin LI 		}
4048f8829a4aSRandall Stewart 		ptype = ntohs(ph->param_type);
4049f8829a4aSRandall Stewart 		num_param++;
40506a58f0e9SXin LI 		if (param_len > sizeof(cstore)) {
4051671d309cSRandall Stewart 			trunc = 1;
4052671d309cSRandall Stewart 		} else {
4053671d309cSRandall Stewart 			trunc = 0;
4054671d309cSRandall Stewart 		}
4055f8829a4aSRandall Stewart 		if (num_param > SCTP_MAX_RESET_PARAMS) {
4056f8829a4aSRandall Stewart 			/* hit the max of parameters already sorry.. */
4057f8829a4aSRandall Stewart 			break;
4058f8829a4aSRandall Stewart 		}
4059f8829a4aSRandall Stewart 		if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4060f8829a4aSRandall Stewart 			struct sctp_stream_reset_out_request *req_out;
4061f8829a4aSRandall Stewart 
40626a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_out_request)) {
40636a58f0e9SXin LI 				break;
40646a58f0e9SXin LI 			}
4065f8829a4aSRandall Stewart 			req_out = (struct sctp_stream_reset_out_request *)ph;
4066f8829a4aSRandall Stewart 			num_req++;
4067f8829a4aSRandall Stewart 			if (stcb->asoc.stream_reset_outstanding) {
4068f8829a4aSRandall Stewart 				seq = ntohl(req_out->response_seq);
4069f8829a4aSRandall Stewart 				if (seq == stcb->asoc.str_reset_seq_out) {
4070f8829a4aSRandall Stewart 					/* implicit ack */
4071f3ebe64cSMichael Tuexen 					(void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4072f8829a4aSRandall Stewart 				}
4073f8829a4aSRandall Stewart 			}
4074671d309cSRandall Stewart 			sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4075c4e848b7SRandall Stewart 		} else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4076ea44232bSRandall Stewart 			struct sctp_stream_reset_add_strm *str_add;
4077ea44232bSRandall Stewart 
40786a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
40796a58f0e9SXin LI 				break;
40806a58f0e9SXin LI 			}
4081ea44232bSRandall Stewart 			str_add = (struct sctp_stream_reset_add_strm *)ph;
4082ea44232bSRandall Stewart 			num_req++;
4083ea44232bSRandall Stewart 			sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4084c4e848b7SRandall Stewart 		} else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4085c4e848b7SRandall Stewart 			struct sctp_stream_reset_add_strm *str_add;
4086c4e848b7SRandall Stewart 
40876a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
40886a58f0e9SXin LI 				break;
40896a58f0e9SXin LI 			}
4090c4e848b7SRandall Stewart 			str_add = (struct sctp_stream_reset_add_strm *)ph;
4091c4e848b7SRandall Stewart 			num_req++;
4092c4e848b7SRandall Stewart 			sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4093f8829a4aSRandall Stewart 		} else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4094f8829a4aSRandall Stewart 			struct sctp_stream_reset_in_request *req_in;
4095f8829a4aSRandall Stewart 
4096f8829a4aSRandall Stewart 			num_req++;
4097f8829a4aSRandall Stewart 			req_in = (struct sctp_stream_reset_in_request *)ph;
4098671d309cSRandall Stewart 			sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4099f8829a4aSRandall Stewart 		} else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4100f8829a4aSRandall Stewart 			struct sctp_stream_reset_tsn_request *req_tsn;
4101f8829a4aSRandall Stewart 
4102f8829a4aSRandall Stewart 			num_req++;
4103f8829a4aSRandall Stewart 			req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4104f8829a4aSRandall Stewart 			if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4105f8829a4aSRandall Stewart 				ret_code = 1;
4106f8829a4aSRandall Stewart 				goto strres_nochunk;
4107f8829a4aSRandall Stewart 			}
4108f8829a4aSRandall Stewart 			/* no more */
4109f8829a4aSRandall Stewart 			break;
4110f8829a4aSRandall Stewart 		} else if (ptype == SCTP_STR_RESET_RESPONSE) {
4111f8829a4aSRandall Stewart 			struct sctp_stream_reset_response *resp;
4112f8829a4aSRandall Stewart 			uint32_t result;
4113f8829a4aSRandall Stewart 
41146a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_response)) {
41156a58f0e9SXin LI 				break;
41166a58f0e9SXin LI 			}
4117f8829a4aSRandall Stewart 			resp = (struct sctp_stream_reset_response *)ph;
4118f8829a4aSRandall Stewart 			seq = ntohl(resp->response_seq);
4119f8829a4aSRandall Stewart 			result = ntohl(resp->result);
4120f8829a4aSRandall Stewart 			if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4121f8829a4aSRandall Stewart 				ret_code = 1;
4122f8829a4aSRandall Stewart 				goto strres_nochunk;
4123f8829a4aSRandall Stewart 			}
4124f8829a4aSRandall Stewart 		} else {
4125f8829a4aSRandall Stewart 			break;
4126f8829a4aSRandall Stewart 		}
4127671d309cSRandall Stewart 		offset += SCTP_SIZE32(param_len);
41286a58f0e9SXin LI 		if (remaining_length >= SCTP_SIZE32(param_len)) {
41296a58f0e9SXin LI 			remaining_length -= SCTP_SIZE32(param_len);
41306a58f0e9SXin LI 		} else {
41316a58f0e9SXin LI 			remaining_length = 0;
41326a58f0e9SXin LI 		}
4133f8829a4aSRandall Stewart 	}
4134f8829a4aSRandall Stewart 	if (num_req == 0) {
4135f8829a4aSRandall Stewart 		/* we have no response free the stuff */
4136f8829a4aSRandall Stewart 		goto strres_nochunk;
4137f8829a4aSRandall Stewart 	}
4138f8829a4aSRandall Stewart 	/* ok we have a chunk to link in */
4139f8829a4aSRandall Stewart 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4140f8829a4aSRandall Stewart 	    chk,
4141f8829a4aSRandall Stewart 	    sctp_next);
4142f8829a4aSRandall Stewart 	stcb->asoc.ctrl_queue_cnt++;
4143f8829a4aSRandall Stewart 	return (ret_code);
4144f8829a4aSRandall Stewart }
4145f8829a4aSRandall Stewart 
4146f8829a4aSRandall Stewart /*
4147f8829a4aSRandall Stewart  * Handle a router or endpoints report of a packet loss, there are two ways
4148f8829a4aSRandall Stewart  * to handle this, either we get the whole packet and must disect it
4149f8829a4aSRandall Stewart  * ourselves (possibly with truncation and or corruption) or it is a summary
4150f8829a4aSRandall Stewart  * from a middle box that did the disectting for us.
4151f8829a4aSRandall Stewart  */
4152f8829a4aSRandall Stewart static void
4153f8829a4aSRandall Stewart sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4154458303daSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4155f8829a4aSRandall Stewart {
4156f8829a4aSRandall Stewart 	struct sctp_chunk_desc desc;
415732df1c9eSMichael Tuexen 	struct sctp_chunkhdr *chk_hdr;
415832df1c9eSMichael Tuexen 	struct sctp_data_chunk *data_chunk;
415932df1c9eSMichael Tuexen 	struct sctp_idata_chunk *idata_chunk;
416032df1c9eSMichael Tuexen 	uint32_t bottle_bw, on_queue;
416132df1c9eSMichael Tuexen 	uint32_t offset, chk_len;
416232df1c9eSMichael Tuexen 	uint16_t pktdrp_len;
416332df1c9eSMichael Tuexen 	uint8_t pktdrp_flags;
4164f8829a4aSRandall Stewart 
416532df1c9eSMichael Tuexen 	KASSERT(sizeof(struct sctp_pktdrop_chunk) <= limit,
416632df1c9eSMichael Tuexen 	    ("PKTDROP chunk too small"));
416732df1c9eSMichael Tuexen 	pktdrp_flags = cp->ch.chunk_flags;
416832df1c9eSMichael Tuexen 	pktdrp_len = ntohs(cp->ch.chunk_length);
416932df1c9eSMichael Tuexen 	KASSERT(limit <= pktdrp_len, ("Inconsistent limit"));
417032df1c9eSMichael Tuexen 	if (pktdrp_flags & SCTP_PACKET_TRUNCATED) {
41716176f9d6SMichael Tuexen 		if (ntohs(cp->trunc_len) <= pktdrp_len - sizeof(struct sctp_pktdrop_chunk)) {
417232df1c9eSMichael Tuexen 			/* The peer plays games with us. */
417332df1c9eSMichael Tuexen 			return;
417432df1c9eSMichael Tuexen 		}
417532df1c9eSMichael Tuexen 	}
417632df1c9eSMichael Tuexen 	limit -= sizeof(struct sctp_pktdrop_chunk);
417732df1c9eSMichael Tuexen 	offset = 0;
417832df1c9eSMichael Tuexen 	if (offset == limit) {
417932df1c9eSMichael Tuexen 		if (pktdrp_flags & SCTP_FROM_MIDDLE_BOX) {
4180f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_pdrpbwrpt);
418132df1c9eSMichael Tuexen 		}
418232df1c9eSMichael Tuexen 	} else if (offset + sizeof(struct sctphdr) > limit) {
418332df1c9eSMichael Tuexen 		/* Only a partial SCTP common header. */
418432df1c9eSMichael Tuexen 		SCTP_STAT_INCR(sctps_pdrpcrupt);
418532df1c9eSMichael Tuexen 		offset = limit;
4186f8829a4aSRandall Stewart 	} else {
418732df1c9eSMichael Tuexen 		/* XXX: Check embedded SCTP common header. */
418832df1c9eSMichael Tuexen 		offset += sizeof(struct sctphdr);
4189f8829a4aSRandall Stewart 	}
419032df1c9eSMichael Tuexen 	/* Now parse through the chunks themselves. */
419132df1c9eSMichael Tuexen 	while (offset < limit) {
419232df1c9eSMichael Tuexen 		if (offset + sizeof(struct sctp_chunkhdr) > limit) {
419332df1c9eSMichael Tuexen 			SCTP_STAT_INCR(sctps_pdrpcrupt);
419432df1c9eSMichael Tuexen 			break;
4195458303daSRandall Stewart 		}
419632df1c9eSMichael Tuexen 		chk_hdr = (struct sctp_chunkhdr *)(cp->data + offset);
419732df1c9eSMichael Tuexen 		desc.chunk_type = chk_hdr->chunk_type;
4198f8829a4aSRandall Stewart 		/* get amount we need to move */
419932df1c9eSMichael Tuexen 		chk_len = (uint32_t)ntohs(chk_hdr->chunk_length);
420032df1c9eSMichael Tuexen 		if (chk_len < sizeof(struct sctp_chunkhdr)) {
420132df1c9eSMichael Tuexen 			/* Someone is lying... */
4202f8829a4aSRandall Stewart 			break;
4203f8829a4aSRandall Stewart 		}
4204f8829a4aSRandall Stewart 		if (desc.chunk_type == SCTP_DATA) {
420532df1c9eSMichael Tuexen 			if (stcb->asoc.idata_supported) {
420632df1c9eSMichael Tuexen 				/* Some is playing games with us. */
420732df1c9eSMichael Tuexen 				break;
4208f8829a4aSRandall Stewart 			}
420932df1c9eSMichael Tuexen 			if (chk_len <= sizeof(struct sctp_data_chunk)) {
421032df1c9eSMichael Tuexen 				/* Some is playing games with us. */
421132df1c9eSMichael Tuexen 				break;
421232df1c9eSMichael Tuexen 			}
421332df1c9eSMichael Tuexen 			if (chk_len < sizeof(struct sctp_data_chunk) + SCTP_NUM_DB_TO_VERIFY) {
421432df1c9eSMichael Tuexen 				/*
421532df1c9eSMichael Tuexen 				 * Not enough data bytes available in the
421632df1c9eSMichael Tuexen 				 * chunk.
421732df1c9eSMichael Tuexen 				 */
4218f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpnedat);
421932df1c9eSMichael Tuexen 				goto next_chunk;
422032df1c9eSMichael Tuexen 			}
422132df1c9eSMichael Tuexen 			if (offset + sizeof(struct sctp_data_chunk) + SCTP_NUM_DB_TO_VERIFY > limit) {
422232df1c9eSMichael Tuexen 				/* Not enough data in buffer. */
4223f8829a4aSRandall Stewart 				break;
4224f8829a4aSRandall Stewart 			}
422532df1c9eSMichael Tuexen 			data_chunk = (struct sctp_data_chunk *)(cp->data + offset);
422632df1c9eSMichael Tuexen 			memcpy(desc.data_bytes, data_chunk + 1, SCTP_NUM_DB_TO_VERIFY);
422732df1c9eSMichael Tuexen 			desc.tsn_ifany = data_chunk->dp.tsn;
422832df1c9eSMichael Tuexen 			if (pktdrp_flags & SCTP_FROM_MIDDLE_BOX) {
422932df1c9eSMichael Tuexen 				SCTP_STAT_INCR(sctps_pdrpmbda);
423032df1c9eSMichael Tuexen 			}
423132df1c9eSMichael Tuexen 		} else if (desc.chunk_type == SCTP_IDATA) {
423232df1c9eSMichael Tuexen 			if (!stcb->asoc.idata_supported) {
423332df1c9eSMichael Tuexen 				/* Some is playing games with us. */
423432df1c9eSMichael Tuexen 				break;
423532df1c9eSMichael Tuexen 			}
423632df1c9eSMichael Tuexen 			if (chk_len <= sizeof(struct sctp_idata_chunk)) {
423732df1c9eSMichael Tuexen 				/* Some is playing games with us. */
423832df1c9eSMichael Tuexen 				break;
423932df1c9eSMichael Tuexen 			}
424032df1c9eSMichael Tuexen 			if (chk_len < sizeof(struct sctp_idata_chunk) + SCTP_NUM_DB_TO_VERIFY) {
424132df1c9eSMichael Tuexen 				/*
424232df1c9eSMichael Tuexen 				 * Not enough data bytes available in the
424332df1c9eSMichael Tuexen 				 * chunk.
424432df1c9eSMichael Tuexen 				 */
424532df1c9eSMichael Tuexen 				SCTP_STAT_INCR(sctps_pdrpnedat);
424632df1c9eSMichael Tuexen 				goto next_chunk;
424732df1c9eSMichael Tuexen 			}
424832df1c9eSMichael Tuexen 			if (offset + sizeof(struct sctp_idata_chunk) + SCTP_NUM_DB_TO_VERIFY > limit) {
424932df1c9eSMichael Tuexen 				/* Not enough data in buffer. */
425032df1c9eSMichael Tuexen 				break;
425132df1c9eSMichael Tuexen 			}
425232df1c9eSMichael Tuexen 			idata_chunk = (struct sctp_idata_chunk *)(cp->data + offset);
425332df1c9eSMichael Tuexen 			memcpy(desc.data_bytes, idata_chunk + 1, SCTP_NUM_DB_TO_VERIFY);
425432df1c9eSMichael Tuexen 			desc.tsn_ifany = idata_chunk->dp.tsn;
425532df1c9eSMichael Tuexen 			if (pktdrp_flags & SCTP_FROM_MIDDLE_BOX) {
425632df1c9eSMichael Tuexen 				SCTP_STAT_INCR(sctps_pdrpmbda);
425732df1c9eSMichael Tuexen 			}
4258f8829a4aSRandall Stewart 		} else {
425932df1c9eSMichael Tuexen 			if (pktdrp_flags & SCTP_FROM_MIDDLE_BOX) {
4260f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpmbct);
4261f8829a4aSRandall Stewart 			}
426232df1c9eSMichael Tuexen 		}
426332df1c9eSMichael Tuexen 		if (process_chunk_drop(stcb, &desc, net, pktdrp_flags)) {
4264f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_pdrppdbrk);
4265f8829a4aSRandall Stewart 			break;
4266f8829a4aSRandall Stewart 		}
426732df1c9eSMichael Tuexen next_chunk:
426832df1c9eSMichael Tuexen 		offset += SCTP_SIZE32(chk_len);
4269f8829a4aSRandall Stewart 	}
4270f8829a4aSRandall Stewart 	/* Now update any rwnd --- possibly */
427132df1c9eSMichael Tuexen 	if ((pktdrp_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4272f8829a4aSRandall Stewart 		/* From a peer, we get a rwnd report */
4273f8829a4aSRandall Stewart 		uint32_t a_rwnd;
4274f8829a4aSRandall Stewart 
4275f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_pdrpfehos);
4276f8829a4aSRandall Stewart 
4277f8829a4aSRandall Stewart 		bottle_bw = ntohl(cp->bottle_bw);
4278f8829a4aSRandall Stewart 		on_queue = ntohl(cp->current_onq);
4279f8829a4aSRandall Stewart 		if (bottle_bw && on_queue) {
4280f8829a4aSRandall Stewart 			/* a rwnd report is in here */
4281f8829a4aSRandall Stewart 			if (bottle_bw > on_queue)
4282f8829a4aSRandall Stewart 				a_rwnd = bottle_bw - on_queue;
4283f8829a4aSRandall Stewart 			else
4284f8829a4aSRandall Stewart 				a_rwnd = 0;
4285f8829a4aSRandall Stewart 
4286f8829a4aSRandall Stewart 			if (a_rwnd == 0)
4287f8829a4aSRandall Stewart 				stcb->asoc.peers_rwnd = 0;
4288f8829a4aSRandall Stewart 			else {
4289f8829a4aSRandall Stewart 				if (a_rwnd > stcb->asoc.total_flight) {
4290f8829a4aSRandall Stewart 					stcb->asoc.peers_rwnd =
4291f8829a4aSRandall Stewart 					    a_rwnd - stcb->asoc.total_flight;
4292f8829a4aSRandall Stewart 				} else {
4293f8829a4aSRandall Stewart 					stcb->asoc.peers_rwnd = 0;
4294f8829a4aSRandall Stewart 				}
4295f8829a4aSRandall Stewart 				if (stcb->asoc.peers_rwnd <
4296f8829a4aSRandall Stewart 				    stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4297f8829a4aSRandall Stewart 					/* SWS sender side engages */
4298f8829a4aSRandall Stewart 					stcb->asoc.peers_rwnd = 0;
4299f8829a4aSRandall Stewart 				}
4300f8829a4aSRandall Stewart 			}
4301f8829a4aSRandall Stewart 		}
4302f8829a4aSRandall Stewart 	} else {
4303f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_pdrpfmbox);
4304f8829a4aSRandall Stewart 	}
4305f8829a4aSRandall Stewart 
4306f8829a4aSRandall Stewart 	/* now middle boxes in sat networks get a cwnd bump */
430732df1c9eSMichael Tuexen 	if ((pktdrp_flags & SCTP_FROM_MIDDLE_BOX) &&
4308f8829a4aSRandall Stewart 	    (stcb->asoc.sat_t3_loss_recovery == 0) &&
4309f8829a4aSRandall Stewart 	    (stcb->asoc.sat_network)) {
4310f8829a4aSRandall Stewart 		/*
4311cd0a4ff6SPedro F. Giffuni 		 * This is debatable but for sat networks it makes sense
4312f8829a4aSRandall Stewart 		 * Note if a T3 timer has went off, we will prohibit any
4313f8829a4aSRandall Stewart 		 * changes to cwnd until we exit the t3 loss recovery.
4314f8829a4aSRandall Stewart 		 */
4315b54d3a6cSRandall Stewart 		stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4316b54d3a6cSRandall Stewart 		    net, cp, &bottle_bw, &on_queue);
4317f8829a4aSRandall Stewart 	}
4318f8829a4aSRandall Stewart }
4319f8829a4aSRandall Stewart 
4320f8829a4aSRandall Stewart /*
4321f8829a4aSRandall Stewart  * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4322f8829a4aSRandall Stewart  * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4323f8829a4aSRandall Stewart  * offset: offset into the mbuf chain to first chunkhdr - length: is the
4324f8829a4aSRandall Stewart  * length of the complete packet outputs: - length: modified to remaining
4325f8829a4aSRandall Stewart  * length after control processing - netp: modified to new sctp_nets after
4326f8829a4aSRandall Stewart  * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4327f8829a4aSRandall Stewart  * bad packet,...) otherwise return the tcb for this packet
4328f8829a4aSRandall Stewart  */
43293c6f3536SRandall Stewart #ifdef __GNUC__
43303c6f3536SRandall Stewart __attribute__((noinline))
43313c6f3536SRandall Stewart #endif
4332f8829a4aSRandall Stewart static struct sctp_tcb *
4333f8829a4aSRandall Stewart sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4334b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
4335f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
433617205eccSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4337d089f9b9SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4338c54a18d2SRandall Stewart     uint32_t vrf_id, uint16_t port)
4339f8829a4aSRandall Stewart {
4340f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4341ff1ffd74SMichael Tuexen 	struct mbuf *op_err;
4342ff1ffd74SMichael Tuexen 	char msg[SCTP_DIAG_INFO_LEN];
4343f8829a4aSRandall Stewart 	uint32_t vtag_in;
4344f8829a4aSRandall Stewart 	int num_chunks = 0;	/* number of control chunks processed */
4345d0f6ab79SMichael Tuexen 	uint32_t chk_length, contiguous;
4346f8829a4aSRandall Stewart 	int ret;
4347bff64a4dSRandall Stewart 	int abort_no_unlock = 0;
4348899288aeSRandall Stewart 	int ecne_seen = 0;
43499de7354bSMichael Tuexen 	int abort_flag;
4350f8829a4aSRandall Stewart 
4351f8829a4aSRandall Stewart 	/*
4352f8829a4aSRandall Stewart 	 * How big should this be, and should it be alloc'd? Lets try the
4353f8829a4aSRandall Stewart 	 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4354f8829a4aSRandall Stewart 	 * until we get into jumbo grams and such..
4355f8829a4aSRandall Stewart 	 */
4356f42a358aSRandall Stewart 	uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4357f8829a4aSRandall Stewart 	int got_auth = 0;
4358f8829a4aSRandall Stewart 	uint32_t auth_offset = 0, auth_len = 0;
4359f8829a4aSRandall Stewart 	int auth_skipped = 0;
43602afb3e84SRandall Stewart 	int asconf_cnt = 0;
4361ceaad40aSRandall Stewart 
4362ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4363dd294dceSMichael Tuexen 	    iphlen, *offset, length, (void *)stcb);
4364f8829a4aSRandall Stewart 
436580a2d140SMichael Tuexen 	if (stcb) {
436680a2d140SMichael Tuexen 		SCTP_TCB_LOCK_ASSERT(stcb);
436780a2d140SMichael Tuexen 	}
4368f8829a4aSRandall Stewart 	/* validate chunk header length... */
4369f8829a4aSRandall Stewart 	if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4370d61a0ae0SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4371d61a0ae0SRandall Stewart 		    ntohs(ch->chunk_length));
43723ed8d364SMichael Tuexen 		*offset = length;
437380a2d140SMichael Tuexen 		return (stcb);
4374f8829a4aSRandall Stewart 	}
4375f8829a4aSRandall Stewart 	/*
4376f8829a4aSRandall Stewart 	 * validate the verification tag
4377f8829a4aSRandall Stewart 	 */
4378f8829a4aSRandall Stewart 	vtag_in = ntohl(sh->v_tag);
4379f8829a4aSRandall Stewart 
4380f8829a4aSRandall Stewart 	if (ch->chunk_type == SCTP_INITIATION) {
4381d61a0ae0SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4382d61a0ae0SRandall Stewart 		    ntohs(ch->chunk_length), vtag_in);
4383f8829a4aSRandall Stewart 		if (vtag_in != 0) {
4384f8829a4aSRandall Stewart 			/* protocol error- silently discard... */
4385f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_badvtag);
438680a2d140SMichael Tuexen 			if (stcb != NULL) {
438780a2d140SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
43886e55db54SRandall Stewart 			}
4389f8829a4aSRandall Stewart 			return (NULL);
4390f8829a4aSRandall Stewart 		}
4391f8829a4aSRandall Stewart 	} else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4392f8829a4aSRandall Stewart 		/*
4393f8829a4aSRandall Stewart 		 * If there is no stcb, skip the AUTH chunk and process
4394f8829a4aSRandall Stewart 		 * later after a stcb is found (to validate the lookup was
4395f8829a4aSRandall Stewart 		 * valid.
4396f8829a4aSRandall Stewart 		 */
4397f8829a4aSRandall Stewart 		if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4398b3f1ea41SRandall Stewart 		    (stcb == NULL) &&
4399c79bec9cSMichael Tuexen 		    (inp->auth_supported == 1)) {
4400f8829a4aSRandall Stewart 			/* save this chunk for later processing */
4401f8829a4aSRandall Stewart 			auth_skipped = 1;
4402f8829a4aSRandall Stewart 			auth_offset = *offset;
4403f8829a4aSRandall Stewart 			auth_len = ntohs(ch->chunk_length);
4404f8829a4aSRandall Stewart 
4405f8829a4aSRandall Stewart 			/* (temporarily) move past this chunk */
4406f8829a4aSRandall Stewart 			*offset += SCTP_SIZE32(auth_len);
4407f8829a4aSRandall Stewart 			if (*offset >= length) {
4408f8829a4aSRandall Stewart 				/* no more data left in the mbuf chain */
4409f8829a4aSRandall Stewart 				*offset = length;
4410f8829a4aSRandall Stewart 				return (NULL);
4411f8829a4aSRandall Stewart 			}
4412f8829a4aSRandall Stewart 			ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4413f8829a4aSRandall Stewart 			    sizeof(struct sctp_chunkhdr), chunk_buf);
4414f8829a4aSRandall Stewart 		}
4415ad81507eSRandall Stewart 		if (ch == NULL) {
4416ad81507eSRandall Stewart 			/* Help */
4417ad81507eSRandall Stewart 			*offset = length;
441880a2d140SMichael Tuexen 			return (stcb);
4419ad81507eSRandall Stewart 		}
4420f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4421f8829a4aSRandall Stewart 			goto process_control_chunks;
4422f8829a4aSRandall Stewart 		}
4423f8829a4aSRandall Stewart 		/*
4424f8829a4aSRandall Stewart 		 * first check if it's an ASCONF with an unknown src addr we
4425f8829a4aSRandall Stewart 		 * need to look inside to find the association
4426f8829a4aSRandall Stewart 		 */
4427f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
44282afb3e84SRandall Stewart 			struct sctp_chunkhdr *asconf_ch = ch;
44292afb3e84SRandall Stewart 			uint32_t asconf_offset = 0, asconf_len = 0;
44302afb3e84SRandall Stewart 
4431f8829a4aSRandall Stewart 			/* inp's refcount may be reduced */
4432f8829a4aSRandall Stewart 			SCTP_INP_INCR_REF(inp);
4433f8829a4aSRandall Stewart 
44342afb3e84SRandall Stewart 			asconf_offset = *offset;
44352afb3e84SRandall Stewart 			do {
44362afb3e84SRandall Stewart 				asconf_len = ntohs(asconf_ch->chunk_length);
44372afb3e84SRandall Stewart 				if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
44382afb3e84SRandall Stewart 					break;
44397215cc1bSMichael Tuexen 				stcb = sctp_findassociation_ep_asconf(m,
4440b1754ad1SMichael Tuexen 				    *offset,
4441b1754ad1SMichael Tuexen 				    dst,
4442b1754ad1SMichael Tuexen 				    sh, &inp, netp, vrf_id);
44432afb3e84SRandall Stewart 				if (stcb != NULL)
44442afb3e84SRandall Stewart 					break;
44452afb3e84SRandall Stewart 				asconf_offset += SCTP_SIZE32(asconf_len);
44462afb3e84SRandall Stewart 				asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
44472afb3e84SRandall Stewart 				    sizeof(struct sctp_chunkhdr), chunk_buf);
44482afb3e84SRandall Stewart 			} while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4449f8829a4aSRandall Stewart 			if (stcb == NULL) {
4450f8829a4aSRandall Stewart 				/*
4451f8829a4aSRandall Stewart 				 * reduce inp's refcount if not reduced in
4452f8829a4aSRandall Stewart 				 * sctp_findassociation_ep_asconf().
4453f8829a4aSRandall Stewart 				 */
4454f8829a4aSRandall Stewart 				SCTP_INP_DECR_REF(inp);
4455f8829a4aSRandall Stewart 			}
44560053ed28SMichael Tuexen 
4457f8829a4aSRandall Stewart 			/* now go back and verify any auth chunk to be sure */
4458f8829a4aSRandall Stewart 			if (auth_skipped && (stcb != NULL)) {
4459f8829a4aSRandall Stewart 				struct sctp_auth_chunk *auth;
4460f8829a4aSRandall Stewart 
44618262311cSMichael Tuexen 				if (auth_len <= SCTP_CHUNK_BUFFER_SIZE) {
44628262311cSMichael Tuexen 					auth = (struct sctp_auth_chunk *)sctp_m_getptr(m, auth_offset, auth_len, chunk_buf);
4463f8829a4aSRandall Stewart 					got_auth = 1;
4464f8829a4aSRandall Stewart 					auth_skipped = 0;
44658262311cSMichael Tuexen 				} else {
44668262311cSMichael Tuexen 					auth = NULL;
44678262311cSMichael Tuexen 				}
4468ad81507eSRandall Stewart 				if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4469f8829a4aSRandall Stewart 				    auth_offset)) {
4470f8829a4aSRandall Stewart 					/* auth HMAC failed so dump it */
4471f8829a4aSRandall Stewart 					*offset = length;
447280a2d140SMichael Tuexen 					return (stcb);
4473f8829a4aSRandall Stewart 				} else {
4474f8829a4aSRandall Stewart 					/* remaining chunks are HMAC checked */
4475f8829a4aSRandall Stewart 					stcb->asoc.authenticated = 1;
4476f8829a4aSRandall Stewart 				}
4477f8829a4aSRandall Stewart 			}
4478f8829a4aSRandall Stewart 		}
4479f8829a4aSRandall Stewart 		if (stcb == NULL) {
4480999f86d6SMichael Tuexen 			SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4481ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4482ff1ffd74SMichael Tuexen 			    msg);
4483f8829a4aSRandall Stewart 			/* no association, so it's out of the blue... */
4484ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp, op_err,
4485d089f9b9SMichael Tuexen 			    mflowtype, mflowid, inp->fibnum,
4486c54a18d2SRandall Stewart 			    vrf_id, port);
4487f8829a4aSRandall Stewart 			*offset = length;
4488f8829a4aSRandall Stewart 			return (NULL);
4489f8829a4aSRandall Stewart 		}
4490f8829a4aSRandall Stewart 		asoc = &stcb->asoc;
4491f8829a4aSRandall Stewart 		/* ABORT and SHUTDOWN can use either v_tag... */
4492f8829a4aSRandall Stewart 		if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4493f8829a4aSRandall Stewart 		    (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4494f8829a4aSRandall Stewart 		    (ch->chunk_type == SCTP_PACKET_DROPPED)) {
44955db47b3dSMichael Tuexen 			/* Take the T-bit always into account. */
44965db47b3dSMichael Tuexen 			if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) &&
44975db47b3dSMichael Tuexen 			    (vtag_in == asoc->my_vtag)) ||
44985db47b3dSMichael Tuexen 			    (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) &&
4499ff76c8c9SMichael Tuexen 			    (asoc->peer_vtag != htonl(0)) &&
4500f8829a4aSRandall Stewart 			    (vtag_in == asoc->peer_vtag))) {
4501f8829a4aSRandall Stewart 				/* this is valid */
4502f8829a4aSRandall Stewart 			} else {
4503f8829a4aSRandall Stewart 				/* drop this packet... */
4504f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_badvtag);
450580a2d140SMichael Tuexen 				if (stcb != NULL) {
450680a2d140SMichael Tuexen 					SCTP_TCB_UNLOCK(stcb);
45076e55db54SRandall Stewart 				}
4508f8829a4aSRandall Stewart 				return (NULL);
4509f8829a4aSRandall Stewart 			}
4510f8829a4aSRandall Stewart 		} else {
4511f8829a4aSRandall Stewart 			/* for all other chunks, vtag must match */
4512f8829a4aSRandall Stewart 			if (vtag_in != asoc->my_vtag) {
4513f8829a4aSRandall Stewart 				/* invalid vtag... */
4514ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INPUT3,
4515ad81507eSRandall Stewart 				    "invalid vtag: %xh, expect %xh\n",
4516ad81507eSRandall Stewart 				    vtag_in, asoc->my_vtag);
4517f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_badvtag);
451880a2d140SMichael Tuexen 				if (stcb != NULL) {
451980a2d140SMichael Tuexen 					SCTP_TCB_UNLOCK(stcb);
45206e55db54SRandall Stewart 				}
4521f8829a4aSRandall Stewart 				*offset = length;
4522f8829a4aSRandall Stewart 				return (NULL);
4523f8829a4aSRandall Stewart 			}
4524f8829a4aSRandall Stewart 		}
4525b7b84c0eSMichael Tuexen 	}			/* end if !SCTP_COOKIE_ECHO */
4526b7b84c0eSMichael Tuexen 	/*
4527b7b84c0eSMichael Tuexen 	 * process all control chunks...
4528b7b84c0eSMichael Tuexen 	 */
4529f8829a4aSRandall Stewart 	if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4530830d754dSRandall Stewart 	    (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4531f8829a4aSRandall Stewart 	    (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4532839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
4533f8829a4aSRandall Stewart 		/* implied cookie-ack.. we must have lost the ack */
4534f8829a4aSRandall Stewart 		sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4535f8829a4aSRandall Stewart 		    *netp);
4536f8829a4aSRandall Stewart 	}
45370053ed28SMichael Tuexen 
4538f8829a4aSRandall Stewart process_control_chunks:
4539f8829a4aSRandall Stewart 	while (IS_SCTP_CONTROL(ch)) {
4540f8829a4aSRandall Stewart 		/* validate chunk length */
4541f8829a4aSRandall Stewart 		chk_length = ntohs(ch->chunk_length);
4542ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4543f8829a4aSRandall Stewart 		    ch->chunk_type, chk_length);
454480fefe0aSRandall Stewart 		SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
45454c9179adSRandall Stewart 		if (chk_length < sizeof(*ch) ||
45464c9179adSRandall Stewart 		    (*offset + (int)chk_length) > length) {
4547f8829a4aSRandall Stewart 			*offset = length;
454880a2d140SMichael Tuexen 			return (stcb);
4549f8829a4aSRandall Stewart 		}
4550f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4551f8829a4aSRandall Stewart 		/*
4552d0f6ab79SMichael Tuexen 		 * INIT and INIT-ACK only gets the init ack "header" portion
4553d0f6ab79SMichael Tuexen 		 * only because we don't have to process the peer's COOKIE.
4554d0f6ab79SMichael Tuexen 		 * All others get a complete chunk.
4555f8829a4aSRandall Stewart 		 */
4556d0f6ab79SMichael Tuexen 		switch (ch->chunk_type) {
4557d0f6ab79SMichael Tuexen 		case SCTP_INITIATION:
4558d0f6ab79SMichael Tuexen 			contiguous = sizeof(struct sctp_init_chunk);
4559d0f6ab79SMichael Tuexen 			break;
4560d0f6ab79SMichael Tuexen 		case SCTP_INITIATION_ACK:
4561d0f6ab79SMichael Tuexen 			contiguous = sizeof(struct sctp_init_ack_chunk);
4562d0f6ab79SMichael Tuexen 			break;
4563d0f6ab79SMichael Tuexen 		default:
4564d0f6ab79SMichael Tuexen 			contiguous = min(chk_length, sizeof(chunk_buf));
4565d0f6ab79SMichael Tuexen 			break;
45666e55db54SRandall Stewart 		}
4567d06c82f1SRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4568d0f6ab79SMichael Tuexen 		    contiguous,
4569d06c82f1SRandall Stewart 		    chunk_buf);
4570d06c82f1SRandall Stewart 		if (ch == NULL) {
4571d06c82f1SRandall Stewart 			*offset = length;
4572c70d1ef1SMichael Tuexen 			return (stcb);
4573d06c82f1SRandall Stewart 		}
45740053ed28SMichael Tuexen 
4575f8829a4aSRandall Stewart 		num_chunks++;
4576f8829a4aSRandall Stewart 		/* Save off the last place we got a control from */
4577f8829a4aSRandall Stewart 		if (stcb != NULL) {
4578ad81507eSRandall Stewart 			if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4579f8829a4aSRandall Stewart 				/*
4580f8829a4aSRandall Stewart 				 * allow last_control to be NULL if
4581f8829a4aSRandall Stewart 				 * ASCONF... ASCONF processing will find the
4582f8829a4aSRandall Stewart 				 * right net later
4583f8829a4aSRandall Stewart 				 */
4584ad81507eSRandall Stewart 				if ((netp != NULL) && (*netp != NULL))
4585f8829a4aSRandall Stewart 					stcb->asoc.last_control_chunk_from = *netp;
4586f8829a4aSRandall Stewart 			}
4587f8829a4aSRandall Stewart 		}
4588f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
4589f8829a4aSRandall Stewart 		sctp_audit_log(0xB0, ch->chunk_type);
4590f8829a4aSRandall Stewart #endif
4591f8829a4aSRandall Stewart 
4592f8829a4aSRandall Stewart 		/* check to see if this chunk required auth, but isn't */
4593b3f1ea41SRandall Stewart 		if ((stcb != NULL) &&
4594b3f1ea41SRandall Stewart 		    sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4595f8829a4aSRandall Stewart 		    !stcb->asoc.authenticated) {
4596f8829a4aSRandall Stewart 			/* "silently" ignore */
4597f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvauthmissing);
4598f8829a4aSRandall Stewart 			goto next_chunk;
4599f8829a4aSRandall Stewart 		}
4600f8829a4aSRandall Stewart 		switch (ch->chunk_type) {
4601f8829a4aSRandall Stewart 		case SCTP_INITIATION:
4602ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4603d68fdc4dSMichael Tuexen 			/* The INIT chunk must be the only chunk. */
4604d68fdc4dSMichael Tuexen 			if ((num_chunks > 1) ||
4605ab6174d5SMichael Tuexen 			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4606c70d1ef1SMichael Tuexen 				/*
4607c70d1ef1SMichael Tuexen 				 * RFC 4960bis requires stopping the
4608c70d1ef1SMichael Tuexen 				 * processing of the packet.
4609c70d1ef1SMichael Tuexen 				 */
4610f8829a4aSRandall Stewart 				*offset = length;
4611c70d1ef1SMichael Tuexen 				return (stcb);
4612f8829a4aSRandall Stewart 			}
4613d68fdc4dSMichael Tuexen 			/* Honor our resource limit. */
4614d68fdc4dSMichael Tuexen 			if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4615ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4616b1754ad1SMichael Tuexen 				sctp_abort_association(inp, stcb, m, iphlen,
4617b1754ad1SMichael Tuexen 				    src, dst, sh, op_err,
4618457b4b88SMichael Tuexen 				    mflowtype, mflowid,
4619f30ac432SMichael Tuexen 				    vrf_id, port);
4620f8829a4aSRandall Stewart 				*offset = length;
4621f8829a4aSRandall Stewart 				return (NULL);
4622f8829a4aSRandall Stewart 			}
4623b1754ad1SMichael Tuexen 			sctp_handle_init(m, iphlen, *offset, src, dst, sh,
4624ad81507eSRandall Stewart 			    (struct sctp_init_chunk *)ch, inp,
4625ca83f93cSMichael Tuexen 			    stcb, *netp, &abort_no_unlock,
4626457b4b88SMichael Tuexen 			    mflowtype, mflowid,
4627f30ac432SMichael Tuexen 			    vrf_id, port);
4628f8829a4aSRandall Stewart 			*offset = length;
462980a2d140SMichael Tuexen 			if ((!abort_no_unlock) && (stcb != NULL)) {
463080a2d140SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
46316e55db54SRandall Stewart 			}
4632f8829a4aSRandall Stewart 			return (NULL);
4633f8829a4aSRandall Stewart 			break;
46349a972525SRandall Stewart 		case SCTP_PAD_CHUNK:
46359a972525SRandall Stewart 			break;
4636f8829a4aSRandall Stewart 		case SCTP_INITIATION_ACK:
4637469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT_ACK\n");
4638f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4639f8829a4aSRandall Stewart 				/* We are not interested anymore */
464080a2d140SMichael Tuexen 				if ((stcb != NULL) && (stcb->asoc.total_output_queue_size)) {
4641f8829a4aSRandall Stewart 					;
4642f8829a4aSRandall Stewart 				} else {
4643f8829a4aSRandall Stewart 					*offset = length;
464480a2d140SMichael Tuexen 					if (stcb != NULL) {
4645b7d130beSMichael Tuexen 						(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4646b7d130beSMichael Tuexen 						    SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
4647f8829a4aSRandall Stewart 					}
4648f8829a4aSRandall Stewart 					return (NULL);
4649f8829a4aSRandall Stewart 				}
4650f8829a4aSRandall Stewart 			}
4651ab6174d5SMichael Tuexen 			/* The INIT-ACK chunk must be the only chunk. */
4652f8829a4aSRandall Stewart 			if ((num_chunks > 1) ||
4653ab6174d5SMichael Tuexen 			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4654f8829a4aSRandall Stewart 				*offset = length;
465580a2d140SMichael Tuexen 				return (stcb);
4656f8829a4aSRandall Stewart 			}
4657469a65d1SMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
4658b1754ad1SMichael Tuexen 				ret = sctp_handle_init_ack(m, iphlen, *offset,
4659b1754ad1SMichael Tuexen 				    src, dst, sh,
4660f30ac432SMichael Tuexen 				    (struct sctp_init_ack_chunk *)ch,
4661f30ac432SMichael Tuexen 				    stcb, *netp,
4662f30ac432SMichael Tuexen 				    &abort_no_unlock,
4663457b4b88SMichael Tuexen 				    mflowtype, mflowid,
4664f30ac432SMichael Tuexen 				    vrf_id);
4665ad81507eSRandall Stewart 			} else {
4666ad81507eSRandall Stewart 				ret = -1;
4667ad81507eSRandall Stewart 			}
4668d68fdc4dSMichael Tuexen 			*offset = length;
4669d68fdc4dSMichael Tuexen 			if (abort_no_unlock) {
4670d68fdc4dSMichael Tuexen 				return (NULL);
4671d68fdc4dSMichael Tuexen 			}
4672f8829a4aSRandall Stewart 			/*
4673f8829a4aSRandall Stewart 			 * Special case, I must call the output routine to
4674f8829a4aSRandall Stewart 			 * get the cookie echoed
4675f8829a4aSRandall Stewart 			 */
4676d68fdc4dSMichael Tuexen 			if ((stcb != NULL) && (ret == 0)) {
4677ceaad40aSRandall Stewart 				sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4678d68fdc4dSMichael Tuexen 			}
467980a2d140SMichael Tuexen 			return (stcb);
4680f8829a4aSRandall Stewart 			break;
4681f8829a4aSRandall Stewart 		case SCTP_SELECTIVE_ACK:
4682469a65d1SMichael Tuexen 		case SCTP_NR_SELECTIVE_ACK:
4683f8829a4aSRandall Stewart 			{
4684f8829a4aSRandall Stewart 				int abort_now = 0;
4685f8829a4aSRandall Stewart 				uint32_t a_rwnd, cum_ack;
4686469a65d1SMichael Tuexen 				uint16_t num_seg, num_nr_seg, num_dup;
4687cd554309SMichael Tuexen 				uint8_t flags;
4688cd554309SMichael Tuexen 				int offset_seg, offset_dup;
4689f8829a4aSRandall Stewart 
4690469a65d1SMichael Tuexen 				SCTPDBG(SCTP_DEBUG_INPUT3, "%s\n",
4691469a65d1SMichael Tuexen 				    ch->chunk_type == SCTP_SELECTIVE_ACK ? "SCTP_SACK" : "SCTP_NR_SACK");
469220083c2eSMichael Tuexen 				SCTP_STAT_INCR(sctps_recvsacks);
4693cd554309SMichael Tuexen 				if (stcb == NULL) {
4694469a65d1SMichael Tuexen 					SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing %s chunk\n",
4695469a65d1SMichael Tuexen 					    (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK");
4696cd554309SMichael Tuexen 					break;
46976e55db54SRandall Stewart 				}
4698469a65d1SMichael Tuexen 				if (ch->chunk_type == SCTP_SELECTIVE_ACK) {
4699cd554309SMichael Tuexen 					if (chk_length < sizeof(struct sctp_sack_chunk)) {
4700cd554309SMichael Tuexen 						SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4701cd554309SMichael Tuexen 						break;
4702d06c82f1SRandall Stewart 					}
4703469a65d1SMichael Tuexen 				} else {
4704469a65d1SMichael Tuexen 					if (stcb->asoc.nrsack_supported == 0) {
4705469a65d1SMichael Tuexen 						goto unknown_chunk;
4706469a65d1SMichael Tuexen 					}
4707469a65d1SMichael Tuexen 					if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
4708469a65d1SMichael Tuexen 						SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR_SACK chunk, too small\n");
4709469a65d1SMichael Tuexen 						break;
4710469a65d1SMichael Tuexen 					}
4711469a65d1SMichael Tuexen 				}
4712839d21d6SMichael Tuexen 				if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
47132afb3e84SRandall Stewart 					/*-
47142afb3e84SRandall Stewart 					 * If we have sent a shutdown-ack, we will pay no
47152afb3e84SRandall Stewart 					 * attention to a sack sent in to us since
47162afb3e84SRandall Stewart 					 * we don't care anymore.
47172afb3e84SRandall Stewart 					 */
4718a1e13272SRandall Stewart 					break;
47192afb3e84SRandall Stewart 				}
4720cd554309SMichael Tuexen 				flags = ch->chunk_flags;
4721469a65d1SMichael Tuexen 				if (ch->chunk_type == SCTP_SELECTIVE_ACK) {
4722469a65d1SMichael Tuexen 					struct sctp_sack_chunk *sack;
4723469a65d1SMichael Tuexen 
4724469a65d1SMichael Tuexen 					sack = (struct sctp_sack_chunk *)ch;
4725f8829a4aSRandall Stewart 					cum_ack = ntohl(sack->sack.cum_tsn_ack);
4726f8829a4aSRandall Stewart 					num_seg = ntohs(sack->sack.num_gap_ack_blks);
4727469a65d1SMichael Tuexen 					num_nr_seg = 0;
4728cd554309SMichael Tuexen 					num_dup = ntohs(sack->sack.num_dup_tsns);
4729469a65d1SMichael Tuexen 					a_rwnd = ntohl(sack->sack.a_rwnd);
4730cd554309SMichael Tuexen 					if (sizeof(struct sctp_sack_chunk) +
4731cd554309SMichael Tuexen 					    num_seg * sizeof(struct sctp_gap_ack_block) +
4732cd554309SMichael Tuexen 					    num_dup * sizeof(uint32_t) != chk_length) {
4733cd554309SMichael Tuexen 						SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4734cd554309SMichael Tuexen 						break;
4735cd554309SMichael Tuexen 					}
4736cd554309SMichael Tuexen 					offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4737cd554309SMichael Tuexen 					offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4738f8829a4aSRandall Stewart 				} else {
4739830d754dSRandall Stewart 					struct sctp_nr_sack_chunk *nr_sack;
4740830d754dSRandall Stewart 
4741830d754dSRandall Stewart 					nr_sack = (struct sctp_nr_sack_chunk *)ch;
4742830d754dSRandall Stewart 					cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
4743830d754dSRandall Stewart 					num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
4744830d754dSRandall Stewart 					num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4745cd554309SMichael Tuexen 					num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
4746469a65d1SMichael Tuexen 					a_rwnd = ntohl(nr_sack->nr_sack.a_rwnd);
4747cd554309SMichael Tuexen 					if (sizeof(struct sctp_nr_sack_chunk) +
4748cd554309SMichael Tuexen 					    (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
4749cd554309SMichael Tuexen 					    num_dup * sizeof(uint32_t) != chk_length) {
4750cd554309SMichael Tuexen 						SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
4751cd554309SMichael Tuexen 						break;
4752cd554309SMichael Tuexen 					}
4753cd554309SMichael Tuexen 					offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
4754469a65d1SMichael Tuexen 					offset_dup = offset_seg + (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block);
4755469a65d1SMichael Tuexen 				}
4756469a65d1SMichael Tuexen 				SCTPDBG(SCTP_DEBUG_INPUT3, "%s process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4757469a65d1SMichael Tuexen 				    (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK",
4758cd554309SMichael Tuexen 				    cum_ack, num_seg, a_rwnd);
4759830d754dSRandall Stewart 				stcb->asoc.seen_a_sack_this_pkt = 1;
4760830d754dSRandall Stewart 				if ((stcb->asoc.pr_sctp_cnt == 0) &&
4761cd554309SMichael Tuexen 				    (num_seg == 0) && (num_nr_seg == 0) &&
476220b07a4dSMichael Tuexen 				    SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4763830d754dSRandall Stewart 				    (stcb->asoc.saw_sack_with_frags == 0) &&
4764d9c5cfeaSMichael Tuexen 				    (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4765cd554309SMichael Tuexen 				    (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
4766830d754dSRandall Stewart 					/*
4767830d754dSRandall Stewart 					 * We have a SIMPLE sack having no
4768830d754dSRandall Stewart 					 * prior segments and data on sent
4769cd554309SMichael Tuexen 					 * queue to be acked. Use the faster
4770cd554309SMichael Tuexen 					 * path sack processing. We also
4771cd554309SMichael Tuexen 					 * allow window update sacks with no
4772cd554309SMichael Tuexen 					 * missing segments to go this way
4773cd554309SMichael Tuexen 					 * too.
4774830d754dSRandall Stewart 					 */
4775493d8e5aSRandall Stewart 					sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
4776899288aeSRandall Stewart 					    &abort_now, ecne_seen);
4777830d754dSRandall Stewart 				} else {
4778469a65d1SMichael Tuexen 					if ((netp != NULL) && (*netp != NULL)) {
47797215cc1bSMichael Tuexen 						sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4780cd554309SMichael Tuexen 						    num_seg, num_nr_seg, num_dup, &abort_now, flags,
4781899288aeSRandall Stewart 						    cum_ack, a_rwnd, ecne_seen);
4782830d754dSRandall Stewart 					}
4783469a65d1SMichael Tuexen 				}
4784830d754dSRandall Stewart 				if (abort_now) {
4785830d754dSRandall Stewart 					/* ABORT signal from sack processing */
4786830d754dSRandall Stewart 					*offset = length;
4787830d754dSRandall Stewart 					return (NULL);
4788830d754dSRandall Stewart 				}
4789cd554309SMichael Tuexen 				if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4790cd554309SMichael Tuexen 				    TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4791cd554309SMichael Tuexen 				    (stcb->asoc.stream_queue_cnt == 0)) {
4792cd554309SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4793cd554309SMichael Tuexen 				}
4794830d754dSRandall Stewart 				break;
4795469a65d1SMichael Tuexen 			}
4796f8829a4aSRandall Stewart 		case SCTP_HEARTBEAT_REQUEST:
4797ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
4798469a65d1SMichael Tuexen 			if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
4799f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_recvheartbeat);
4800ad81507eSRandall Stewart 				sctp_send_heartbeat_ack(stcb, m, *offset,
4801ad81507eSRandall Stewart 				    chk_length, *netp);
4802ad81507eSRandall Stewart 			}
4803f8829a4aSRandall Stewart 			break;
4804f8829a4aSRandall Stewart 		case SCTP_HEARTBEAT_ACK:
4805469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT_ACK\n");
4806ad81507eSRandall Stewart 			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
4807d06c82f1SRandall Stewart 				/* Its not ours */
48089de7354bSMichael Tuexen 				break;
4809d06c82f1SRandall Stewart 			}
4810f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvheartbeatack);
4811469a65d1SMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
4812f8829a4aSRandall Stewart 				sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
4813f8829a4aSRandall Stewart 				    stcb, *netp);
4814469a65d1SMichael Tuexen 			}
4815f8829a4aSRandall Stewart 			break;
4816f8829a4aSRandall Stewart 		case SCTP_ABORT_ASSOCIATION:
4817207304d4SRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
4818dd294dceSMichael Tuexen 			    (void *)stcb);
4819f8829a4aSRandall Stewart 			*offset = length;
4820701492a5SMichael Tuexen 			if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
4821701492a5SMichael Tuexen 				if (sctp_handle_abort((struct sctp_abort_chunk *)ch, stcb, *netp)) {
4822f8829a4aSRandall Stewart 					return (NULL);
4823701492a5SMichael Tuexen 				} else {
4824701492a5SMichael Tuexen 					return (stcb);
4825701492a5SMichael Tuexen 				}
4826701492a5SMichael Tuexen 			} else {
4827701492a5SMichael Tuexen 				return (NULL);
4828701492a5SMichael Tuexen 			}
4829f8829a4aSRandall Stewart 			break;
4830f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN:
4831207304d4SRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
4832dd294dceSMichael Tuexen 			    (void *)stcb);
4833ad81507eSRandall Stewart 			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
48349de7354bSMichael Tuexen 				break;
4835ad81507eSRandall Stewart 			}
4836469a65d1SMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
48379de7354bSMichael Tuexen 				abort_flag = 0;
4838f8829a4aSRandall Stewart 				sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
4839f8829a4aSRandall Stewart 				    stcb, *netp, &abort_flag);
4840f8829a4aSRandall Stewart 				if (abort_flag) {
4841f8829a4aSRandall Stewart 					*offset = length;
4842f8829a4aSRandall Stewart 					return (NULL);
4843f8829a4aSRandall Stewart 				}
4844f8829a4aSRandall Stewart 			}
4845f8829a4aSRandall Stewart 			break;
4846f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_ACK:
4847469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_ACK, stcb %p\n", (void *)stcb);
4848d1cb8d11SMichael Tuexen 			if ((chk_length == sizeof(struct sctp_shutdown_ack_chunk)) &&
4849d1cb8d11SMichael Tuexen 			    (stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
4850f8829a4aSRandall Stewart 				sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
4851f8829a4aSRandall Stewart 				*offset = length;
4852f8829a4aSRandall Stewart 				return (NULL);
4853d1cb8d11SMichael Tuexen 			}
4854f8829a4aSRandall Stewart 			break;
4855f8829a4aSRandall Stewart 		case SCTP_OPERATION_ERROR:
4856469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP_ERR\n");
4857469a65d1SMichael Tuexen 			if ((stcb != NULL) && (netp != NULL) && (*netp != NULL) &&
48583e87bccdSMichael Tuexen 			    sctp_handle_error(ch, stcb, *netp, contiguous) < 0) {
4859f8829a4aSRandall Stewart 				*offset = length;
4860f8829a4aSRandall Stewart 				return (NULL);
4861f8829a4aSRandall Stewart 			}
4862f8829a4aSRandall Stewart 			break;
4863f8829a4aSRandall Stewart 		case SCTP_COOKIE_ECHO:
4864ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3,
4865469a65d1SMichael Tuexen 			    "SCTP_COOKIE_ECHO, stcb %p\n", (void *)stcb);
4866469a65d1SMichael Tuexen 			if ((stcb != NULL) && (stcb->asoc.total_output_queue_size > 0)) {
4867f8829a4aSRandall Stewart 				;
4868f8829a4aSRandall Stewart 			} else {
4869ad81507eSRandall Stewart 				if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4870f8829a4aSRandall Stewart 					/* We are not interested anymore */
48710c7dc840SRandall Stewart 			abend:
487280a2d140SMichael Tuexen 					if (stcb != NULL) {
487328085b2eSRandall Stewart 						SCTP_TCB_UNLOCK(stcb);
487428085b2eSRandall Stewart 					}
4875f8829a4aSRandall Stewart 					*offset = length;
4876f8829a4aSRandall Stewart 					return (NULL);
4877f8829a4aSRandall Stewart 				}
4878f8829a4aSRandall Stewart 			}
48793017b21bSMichael Tuexen 			/*-
4880f8829a4aSRandall Stewart 			 * First are we accepting? We do this again here
488188a7eb29SRandall Stewart 			 * since it is possible that a previous endpoint WAS
488288a7eb29SRandall Stewart 			 * listening responded to a INIT-ACK and then
4883f8829a4aSRandall Stewart 			 * closed. We opened and bound.. and are now no
4884f8829a4aSRandall Stewart 			 * longer listening.
4885779f106aSGleb Smirnoff 			 *
4886779f106aSGleb Smirnoff 			 * XXXGL: notes on checking listen queue length.
4887779f106aSGleb Smirnoff 			 * 1) SCTP_IS_LISTENING() doesn't necessarily mean
4888779f106aSGleb Smirnoff 			 *    SOLISTENING(), because a listening "UDP type"
4889779f106aSGleb Smirnoff 			 *    socket isn't listening in terms of the socket
4890779f106aSGleb Smirnoff 			 *    layer.  It is a normal data flow socket, that
4891779f106aSGleb Smirnoff 			 *    can fork off new connections.  Thus, we should
4892779f106aSGleb Smirnoff 			 *    look into sol_qlen only in case we are !UDP.
4893779f106aSGleb Smirnoff 			 * 2) Checking sol_qlen in general requires locking
4894779f106aSGleb Smirnoff 			 *    the socket, and this code lacks that.
4895f8829a4aSRandall Stewart 			 */
48965d08768aSMichael Tuexen 			if ((stcb == NULL) &&
48975d08768aSMichael Tuexen 			    (!SCTP_IS_LISTENING(inp) ||
4898779f106aSGleb Smirnoff 			    (!(inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
4899779f106aSGleb Smirnoff 			    inp->sctp_socket->sol_qlen >= inp->sctp_socket->sol_qlimit))) {
4900b201f536SRandall Stewart 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4901b3f1ea41SRandall Stewart 				    (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
4902ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4903b1754ad1SMichael Tuexen 					sctp_abort_association(inp, stcb, m, iphlen,
4904b1754ad1SMichael Tuexen 					    src, dst, sh, op_err,
4905457b4b88SMichael Tuexen 					    mflowtype, mflowid,
4906f30ac432SMichael Tuexen 					    vrf_id, port);
4907f8829a4aSRandall Stewart 				}
4908f8829a4aSRandall Stewart 				*offset = length;
4909f8829a4aSRandall Stewart 				return (NULL);
4910b201f536SRandall Stewart 			} else {
4911f8829a4aSRandall Stewart 				struct mbuf *ret_buf;
4912a5d547adSRandall Stewart 				struct sctp_inpcb *linp;
4913efd5e692SMichael Tuexen 				struct sctp_tmit_chunk *chk;
4914f8829a4aSRandall Stewart 
4915ad81507eSRandall Stewart 				if (stcb) {
4916a5d547adSRandall Stewart 					linp = NULL;
4917ad81507eSRandall Stewart 				} else {
4918a5d547adSRandall Stewart 					linp = inp;
4919ad81507eSRandall Stewart 				}
4920a5d547adSRandall Stewart 
4921469a65d1SMichael Tuexen 				if (linp != NULL) {
4922a5d547adSRandall Stewart 					SCTP_ASOC_CREATE_LOCK(linp);
49230c7dc840SRandall Stewart 					if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
49240c7dc840SRandall Stewart 					    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
49250c7dc840SRandall Stewart 						SCTP_ASOC_CREATE_UNLOCK(linp);
49260c7dc840SRandall Stewart 						goto abend;
49270c7dc840SRandall Stewart 					}
4928ad81507eSRandall Stewart 				}
49290053ed28SMichael Tuexen 
4930469a65d1SMichael Tuexen 				if (netp != NULL) {
493180a2d140SMichael Tuexen 					struct sctp_tcb *locked_stcb;
493280a2d140SMichael Tuexen 
493380a2d140SMichael Tuexen 					locked_stcb = stcb;
4934f8829a4aSRandall Stewart 					ret_buf =
4935f8829a4aSRandall Stewart 					    sctp_handle_cookie_echo(m, iphlen,
4936b1754ad1SMichael Tuexen 					    *offset,
4937b1754ad1SMichael Tuexen 					    src, dst,
4938b1754ad1SMichael Tuexen 					    sh,
4939f8829a4aSRandall Stewart 					    (struct sctp_cookie_echo_chunk *)ch,
4940f8829a4aSRandall Stewart 					    &inp, &stcb, netp,
4941f8829a4aSRandall Stewart 					    auth_skipped,
4942f8829a4aSRandall Stewart 					    auth_offset,
4943a5d547adSRandall Stewart 					    auth_len,
494480a2d140SMichael Tuexen 					    &locked_stcb,
4945457b4b88SMichael Tuexen 					    mflowtype,
4946f30ac432SMichael Tuexen 					    mflowid,
4947c54a18d2SRandall Stewart 					    vrf_id,
4948c54a18d2SRandall Stewart 					    port);
494980a2d140SMichael Tuexen 					if ((locked_stcb != NULL) && (locked_stcb != stcb)) {
495080a2d140SMichael Tuexen 						SCTP_TCB_UNLOCK(locked_stcb);
495180a2d140SMichael Tuexen 					}
495280a2d140SMichael Tuexen 					if (stcb != NULL) {
495380a2d140SMichael Tuexen 						SCTP_TCB_LOCK_ASSERT(stcb);
495480a2d140SMichael Tuexen 					}
4955ad81507eSRandall Stewart 				} else {
4956ad81507eSRandall Stewart 					ret_buf = NULL;
4957ad81507eSRandall Stewart 				}
4958469a65d1SMichael Tuexen 				if (linp != NULL) {
4959a5d547adSRandall Stewart 					SCTP_ASOC_CREATE_UNLOCK(linp);
4960ad81507eSRandall Stewart 				}
4961f8829a4aSRandall Stewart 				if (ret_buf == NULL) {
496280a2d140SMichael Tuexen 					if (stcb != NULL) {
496380a2d140SMichael Tuexen 						SCTP_TCB_UNLOCK(stcb);
4964f8829a4aSRandall Stewart 					}
4965ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INPUT3,
4966ad81507eSRandall Stewart 					    "GAK, null buffer\n");
4967f8829a4aSRandall Stewart 					*offset = length;
4968f8829a4aSRandall Stewart 					return (NULL);
4969f8829a4aSRandall Stewart 				}
4970f8829a4aSRandall Stewart 				/* if AUTH skipped, see if it verified... */
4971f8829a4aSRandall Stewart 				if (auth_skipped) {
4972f8829a4aSRandall Stewart 					got_auth = 1;
4973f8829a4aSRandall Stewart 					auth_skipped = 0;
4974f8829a4aSRandall Stewart 				}
4975efd5e692SMichael Tuexen 				/* Restart the timer if we have pending data */
497686fd36c5SMichael Tuexen 				TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
4977efd5e692SMichael Tuexen 					if (chk->whoTo != NULL) {
4978efd5e692SMichael Tuexen 						break;
4979efd5e692SMichael Tuexen 					}
4980efd5e692SMichael Tuexen 				}
4981efd5e692SMichael Tuexen 				if (chk != NULL) {
49824a9ef3f8SMichael Tuexen 					sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
4983f8829a4aSRandall Stewart 				}
4984f8829a4aSRandall Stewart 			}
4985f8829a4aSRandall Stewart 			break;
4986f8829a4aSRandall Stewart 		case SCTP_COOKIE_ACK:
4987469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE_ACK, stcb %p\n", (void *)stcb);
4988ad81507eSRandall Stewart 			if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
49899de7354bSMichael Tuexen 				break;
499017205eccSRandall Stewart 			}
4991f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4992f8829a4aSRandall Stewart 				/* We are not interested anymore */
4993f8829a4aSRandall Stewart 				if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4994f8829a4aSRandall Stewart 					;
4995ad81507eSRandall Stewart 				} else if (stcb) {
4996b7d130beSMichael Tuexen 					(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4997b7d130beSMichael Tuexen 					    SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
4998f8829a4aSRandall Stewart 					*offset = length;
4999f8829a4aSRandall Stewart 					return (NULL);
5000f8829a4aSRandall Stewart 				}
5001f8829a4aSRandall Stewart 			}
5002469a65d1SMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
5003f8829a4aSRandall Stewart 				sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
50046e55db54SRandall Stewart 			}
5005f8829a4aSRandall Stewart 			break;
5006f8829a4aSRandall Stewart 		case SCTP_ECN_ECHO:
5007469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_ECHO\n");
50089de7354bSMichael Tuexen 			if (stcb == NULL) {
50099de7354bSMichael Tuexen 				break;
5010d06c82f1SRandall Stewart 			}
5011c79bec9cSMichael Tuexen 			if (stcb->asoc.ecn_supported == 0) {
5012c79bec9cSMichael Tuexen 				goto unknown_chunk;
5013c79bec9cSMichael Tuexen 			}
50146587a2bdSMichael Tuexen 			if ((chk_length != sizeof(struct sctp_ecne_chunk)) &&
50156587a2bdSMichael Tuexen 			    (chk_length != sizeof(struct old_sctp_ecne_chunk))) {
50169de7354bSMichael Tuexen 				break;
50179de7354bSMichael Tuexen 			}
5018469a65d1SMichael Tuexen 			sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, stcb);
5019899288aeSRandall Stewart 			ecne_seen = 1;
5020f8829a4aSRandall Stewart 			break;
5021f8829a4aSRandall Stewart 		case SCTP_ECN_CWR:
5022469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_CWR\n");
50239de7354bSMichael Tuexen 			if (stcb == NULL) {
50249de7354bSMichael Tuexen 				break;
5025d06c82f1SRandall Stewart 			}
5026c79bec9cSMichael Tuexen 			if (stcb->asoc.ecn_supported == 0) {
5027c79bec9cSMichael Tuexen 				goto unknown_chunk;
5028c79bec9cSMichael Tuexen 			}
50299de7354bSMichael Tuexen 			if (chk_length != sizeof(struct sctp_cwr_chunk)) {
50309de7354bSMichael Tuexen 				break;
50319de7354bSMichael Tuexen 			}
5032a21779f0SRandall Stewart 			sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5033f8829a4aSRandall Stewart 			break;
5034f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_COMPLETE:
5035469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_COMPLETE, stcb %p\n", (void *)stcb);
5036f8829a4aSRandall Stewart 			/* must be first and only chunk */
5037f8829a4aSRandall Stewart 			if ((num_chunks > 1) ||
50384c9179adSRandall Stewart 			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5039f8829a4aSRandall Stewart 				*offset = length;
504080a2d140SMichael Tuexen 				return (stcb);
5041f8829a4aSRandall Stewart 			}
5042d1cb8d11SMichael Tuexen 			if ((chk_length == sizeof(struct sctp_shutdown_complete_chunk)) &&
5043d1cb8d11SMichael Tuexen 			    (stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5044f8829a4aSRandall Stewart 				sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5045f8829a4aSRandall Stewart 				    stcb, *netp);
5046f8829a4aSRandall Stewart 				*offset = length;
5047f8829a4aSRandall Stewart 				return (NULL);
5048d1cb8d11SMichael Tuexen 			}
5049f8829a4aSRandall Stewart 			break;
5050f8829a4aSRandall Stewart 		case SCTP_ASCONF:
5051ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5052469a65d1SMichael Tuexen 			if (stcb != NULL) {
5053c79bec9cSMichael Tuexen 				if (stcb->asoc.asconf_supported == 0) {
5054c79bec9cSMichael Tuexen 					goto unknown_chunk;
5055c79bec9cSMichael Tuexen 				}
5056b1754ad1SMichael Tuexen 				sctp_handle_asconf(m, *offset, src,
50572afb3e84SRandall Stewart 				    (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
50582afb3e84SRandall Stewart 				asconf_cnt++;
50596e55db54SRandall Stewart 			}
5060f8829a4aSRandall Stewart 			break;
5061f8829a4aSRandall Stewart 		case SCTP_ASCONF_ACK:
5062469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF_ACK\n");
50639de7354bSMichael Tuexen 			if (stcb == NULL) {
50649de7354bSMichael Tuexen 				break;
5065d06c82f1SRandall Stewart 			}
5066c79bec9cSMichael Tuexen 			if (stcb->asoc.asconf_supported == 0) {
5067c79bec9cSMichael Tuexen 				goto unknown_chunk;
5068c79bec9cSMichael Tuexen 			}
50699de7354bSMichael Tuexen 			if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
50709de7354bSMichael Tuexen 				break;
50719de7354bSMichael Tuexen 			}
50729de7354bSMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
5073f8829a4aSRandall Stewart 				/* He's alive so give him credit */
5074b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5075c4739e2fSRandall Stewart 					sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5076c4739e2fSRandall Stewart 					    stcb->asoc.overall_error_count,
5077c4739e2fSRandall Stewart 					    0,
5078c4739e2fSRandall Stewart 					    SCTP_FROM_SCTP_INPUT,
5079c4739e2fSRandall Stewart 					    __LINE__);
5080c4739e2fSRandall Stewart 				}
5081f8829a4aSRandall Stewart 				stcb->asoc.overall_error_count = 0;
5082f8829a4aSRandall Stewart 				sctp_handle_asconf_ack(m, *offset,
50833232788eSRandall Stewart 				    (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
50843232788eSRandall Stewart 				if (abort_no_unlock)
50853232788eSRandall Stewart 					return (NULL);
50866e55db54SRandall Stewart 			}
5087f8829a4aSRandall Stewart 			break;
5088f8829a4aSRandall Stewart 		case SCTP_FORWARD_CUM_TSN:
508944249214SRandall Stewart 		case SCTP_IFORWARD_CUM_TSN:
5090c96d7c37SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "%s\n",
5091c96d7c37SMichael Tuexen 			    ch->chunk_type == SCTP_FORWARD_CUM_TSN ? "FORWARD_TSN" : "I_FORWARD_TSN");
50929de7354bSMichael Tuexen 			if (stcb == NULL) {
50939de7354bSMichael Tuexen 				break;
5094d06c82f1SRandall Stewart 			}
5095c79bec9cSMichael Tuexen 			if (stcb->asoc.prsctp_supported == 0) {
5096c79bec9cSMichael Tuexen 				goto unknown_chunk;
5097c79bec9cSMichael Tuexen 			}
50989de7354bSMichael Tuexen 			if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
50999de7354bSMichael Tuexen 				break;
51009de7354bSMichael Tuexen 			}
5101fcbfdc0aSMichael Tuexen 			if (((stcb->asoc.idata_supported == 1) && (ch->chunk_type == SCTP_FORWARD_CUM_TSN)) ||
5102fcbfdc0aSMichael Tuexen 			    ((stcb->asoc.idata_supported == 0) && (ch->chunk_type == SCTP_IFORWARD_CUM_TSN))) {
5103c96d7c37SMichael Tuexen 				if (ch->chunk_type == SCTP_FORWARD_CUM_TSN) {
5104c96d7c37SMichael Tuexen 					SCTP_SNPRINTF(msg, sizeof(msg), "%s", "FORWARD-TSN chunk received when I-FORWARD-TSN was negotiated");
5105c96d7c37SMichael Tuexen 				} else {
5106c96d7c37SMichael Tuexen 					SCTP_SNPRINTF(msg, sizeof(msg), "%s", "I-FORWARD-TSN chunk received when FORWARD-TSN was negotiated");
5107c96d7c37SMichael Tuexen 				}
5108c96d7c37SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
5109*105b68b4SMichael Tuexen 				sctp_abort_an_association(inp, stcb, op_err, false, SCTP_SO_NOT_LOCKED);
5110c96d7c37SMichael Tuexen 				*offset = length;
5111c96d7c37SMichael Tuexen 				return (NULL);
5112c96d7c37SMichael Tuexen 			}
5113f8829a4aSRandall Stewart 			*fwd_tsn_seen = 1;
5114f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5115f8829a4aSRandall Stewart 				/* We are not interested anymore */
5116b7d130beSMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5117b7d130beSMichael Tuexen 				    SCTP_FROM_SCTP_INPUT + SCTP_LOC_31);
5118f8829a4aSRandall Stewart 				*offset = length;
5119f8829a4aSRandall Stewart 				return (NULL);
5120f8829a4aSRandall Stewart 			}
512191843cf3SMichael Tuexen 			/*
51229de7354bSMichael Tuexen 			 * For sending a SACK this looks like DATA chunks.
512391843cf3SMichael Tuexen 			 */
512491843cf3SMichael Tuexen 			stcb->asoc.last_data_chunk_from = stcb->asoc.last_control_chunk_from;
51259de7354bSMichael Tuexen 			abort_flag = 0;
5126f8829a4aSRandall Stewart 			sctp_handle_forward_tsn(stcb,
5127671d309cSRandall Stewart 			    (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5128f8829a4aSRandall Stewart 			if (abort_flag) {
5129f8829a4aSRandall Stewart 				*offset = length;
5130f8829a4aSRandall Stewart 				return (NULL);
5131c4739e2fSRandall Stewart 			}
5132f8829a4aSRandall Stewart 			break;
5133f8829a4aSRandall Stewart 		case SCTP_STREAM_RESET:
5134ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
51359de7354bSMichael Tuexen 			if (stcb == NULL) {
51369de7354bSMichael Tuexen 				break;
5137d06c82f1SRandall Stewart 			}
5138317e00efSMichael Tuexen 			if (stcb->asoc.reconfig_supported == 0) {
5139c79bec9cSMichael Tuexen 				goto unknown_chunk;
5140f8829a4aSRandall Stewart 			}
51419de7354bSMichael Tuexen 			if (chk_length < sizeof(struct sctp_stream_reset_tsn_req)) {
51429de7354bSMichael Tuexen 				break;
51439de7354bSMichael Tuexen 			}
5144a169d6ecSMichael Tuexen 			if (sctp_handle_stream_reset(stcb, m, *offset, ch)) {
5145f8829a4aSRandall Stewart 				/* stop processing */
5146f8829a4aSRandall Stewart 				*offset = length;
5147f8829a4aSRandall Stewart 				return (NULL);
5148f8829a4aSRandall Stewart 			}
5149f8829a4aSRandall Stewart 			break;
5150f8829a4aSRandall Stewart 		case SCTP_PACKET_DROPPED:
5151ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
51529de7354bSMichael Tuexen 			if (stcb == NULL) {
51539de7354bSMichael Tuexen 				break;
5154d06c82f1SRandall Stewart 			}
5155c79bec9cSMichael Tuexen 			if (stcb->asoc.pktdrop_supported == 0) {
5156c79bec9cSMichael Tuexen 				goto unknown_chunk;
5157c79bec9cSMichael Tuexen 			}
51589de7354bSMichael Tuexen 			if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
51599de7354bSMichael Tuexen 				break;
51609de7354bSMichael Tuexen 			}
51619de7354bSMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
5162f8829a4aSRandall Stewart 				sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5163458303daSRandall Stewart 				    stcb, *netp,
5164d0f6ab79SMichael Tuexen 				    min(chk_length, contiguous));
51656e55db54SRandall Stewart 			}
5166f8829a4aSRandall Stewart 			break;
5167f8829a4aSRandall Stewart 		case SCTP_AUTHENTICATION:
5168ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5169f8829a4aSRandall Stewart 			if (stcb == NULL) {
5170f8829a4aSRandall Stewart 				/* save the first AUTH for later processing */
5171f8829a4aSRandall Stewart 				if (auth_skipped == 0) {
5172f8829a4aSRandall Stewart 					auth_offset = *offset;
5173f8829a4aSRandall Stewart 					auth_len = chk_length;
5174f8829a4aSRandall Stewart 					auth_skipped = 1;
5175f8829a4aSRandall Stewart 				}
5176f8829a4aSRandall Stewart 				/* skip this chunk (temporarily) */
51779de7354bSMichael Tuexen 				break;
5178f8829a4aSRandall Stewart 			}
5179c79bec9cSMichael Tuexen 			if (stcb->asoc.auth_supported == 0) {
5180c79bec9cSMichael Tuexen 				goto unknown_chunk;
5181c79bec9cSMichael Tuexen 			}
5182d06c82f1SRandall Stewart 			if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5183ad81507eSRandall Stewart 			    (chk_length > (sizeof(struct sctp_auth_chunk) +
5184ad81507eSRandall Stewart 			    SCTP_AUTH_DIGEST_LEN_MAX))) {
5185d06c82f1SRandall Stewart 				/* Its not ours */
5186d06c82f1SRandall Stewart 				*offset = length;
518780a2d140SMichael Tuexen 				return (stcb);
5188d06c82f1SRandall Stewart 			}
5189f8829a4aSRandall Stewart 			if (got_auth == 1) {
5190f8829a4aSRandall Stewart 				/* skip this chunk... it's already auth'd */
51919de7354bSMichael Tuexen 				break;
5192f8829a4aSRandall Stewart 			}
5193f8829a4aSRandall Stewart 			got_auth = 1;
5194f2f66ef6SMichael Tuexen 			if (sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, m, *offset)) {
5195f8829a4aSRandall Stewart 				/* auth HMAC failed so dump the packet */
5196f8829a4aSRandall Stewart 				*offset = length;
5197f8829a4aSRandall Stewart 				return (stcb);
5198f8829a4aSRandall Stewart 			} else {
5199f8829a4aSRandall Stewart 				/* remaining chunks are HMAC checked */
5200f8829a4aSRandall Stewart 				stcb->asoc.authenticated = 1;
5201f8829a4aSRandall Stewart 			}
5202f8829a4aSRandall Stewart 			break;
5203f8829a4aSRandall Stewart 
5204f8829a4aSRandall Stewart 		default:
5205f8829a4aSRandall Stewart 	unknown_chunk:
5206f8829a4aSRandall Stewart 			/* it's an unknown chunk! */
5207e99ce3eaSMichael Tuexen 			if ((ch->chunk_type & 0x40) &&
5208e99ce3eaSMichael Tuexen 			    (stcb != NULL) &&
5209e99ce3eaSMichael Tuexen 			    (SCTP_GET_STATE(stcb) != SCTP_STATE_EMPTY) &&
5210e99ce3eaSMichael Tuexen 			    (SCTP_GET_STATE(stcb) != SCTP_STATE_INUSE) &&
5211e99ce3eaSMichael Tuexen 			    (SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT)) {
521286eda749SMichael Tuexen 				struct sctp_gen_error_cause *cause;
521339cbb549SMichael Tuexen 				int len;
5214f8829a4aSRandall Stewart 
521586eda749SMichael Tuexen 				op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_gen_error_cause),
5216eb1b1807SGleb Smirnoff 				    0, M_NOWAIT, 1, MT_DATA);
521786eda749SMichael Tuexen 				if (op_err != NULL) {
521839cbb549SMichael Tuexen 					len = min(SCTP_SIZE32(chk_length), (uint32_t)(length - *offset));
521986eda749SMichael Tuexen 					cause = mtod(op_err, struct sctp_gen_error_cause *);
522086eda749SMichael Tuexen 					cause->code = htons(SCTP_CAUSE_UNRECOG_CHUNK);
52219a8e3088SMichael Tuexen 					cause->length = htons((uint16_t)(len + sizeof(struct sctp_gen_error_cause)));
522286eda749SMichael Tuexen 					SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
522386eda749SMichael Tuexen 					SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(m, *offset, len, M_NOWAIT);
522486eda749SMichael Tuexen 					if (SCTP_BUF_NEXT(op_err) != NULL) {
5225eadccaccSRandall Stewart #ifdef SCTP_MBUF_LOGGING
5226b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
522730811e70SMichael Tuexen 							sctp_log_mbc(SCTP_BUF_NEXT(op_err), SCTP_MBUF_ICOPY);
5228eadccaccSRandall Stewart 						}
5229eadccaccSRandall Stewart #endif
523086eda749SMichael Tuexen 						sctp_queue_op_err(stcb, op_err);
5231f8829a4aSRandall Stewart 					} else {
523286eda749SMichael Tuexen 						sctp_m_freem(op_err);
5233f8829a4aSRandall Stewart 					}
5234f8829a4aSRandall Stewart 				}
5235f8829a4aSRandall Stewart 			}
5236f8829a4aSRandall Stewart 			if ((ch->chunk_type & 0x80) == 0) {
5237f8829a4aSRandall Stewart 				/* discard this packet */
5238f8829a4aSRandall Stewart 				*offset = length;
5239f8829a4aSRandall Stewart 				return (stcb);
5240b7b84c0eSMichael Tuexen 			}	/* else skip this bad chunk and continue... */
5241b7b84c0eSMichael Tuexen 			break;
5242f8829a4aSRandall Stewart 		}		/* switch (ch->chunk_type) */
5243f8829a4aSRandall Stewart 
5244f8829a4aSRandall Stewart next_chunk:
5245f8829a4aSRandall Stewart 		/* get the next chunk */
5246f8829a4aSRandall Stewart 		*offset += SCTP_SIZE32(chk_length);
5247f8829a4aSRandall Stewart 		if (*offset >= length) {
5248f8829a4aSRandall Stewart 			/* no more data left in the mbuf chain */
5249f8829a4aSRandall Stewart 			break;
5250f8829a4aSRandall Stewart 		}
5251f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5252f8829a4aSRandall Stewart 		    sizeof(struct sctp_chunkhdr), chunk_buf);
5253f8829a4aSRandall Stewart 		if (ch == NULL) {
5254f8829a4aSRandall Stewart 			*offset = length;
525580a2d140SMichael Tuexen 			return (stcb);
5256f8829a4aSRandall Stewart 		}
5257f8829a4aSRandall Stewart 	}			/* while */
52582afb3e84SRandall Stewart 
5259469a65d1SMichael Tuexen 	if ((asconf_cnt > 0) && (stcb != NULL)) {
52602afb3e84SRandall Stewart 		sctp_send_asconf_ack(stcb);
52612afb3e84SRandall Stewart 	}
5262f8829a4aSRandall Stewart 	return (stcb);
5263f8829a4aSRandall Stewart }
5264f8829a4aSRandall Stewart 
5265f8829a4aSRandall Stewart /*
5266f8829a4aSRandall Stewart  * common input chunk processing (v4 and v6)
5267f8829a4aSRandall Stewart  */
52686e55db54SRandall Stewart void
5269b1754ad1SMichael Tuexen sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
5270b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
5271b1754ad1SMichael Tuexen     struct sctphdr *sh, struct sctp_chunkhdr *ch,
5272a8775ad9SMichael Tuexen     uint8_t compute_crc,
5273a8775ad9SMichael Tuexen     uint8_t ecn_bits,
5274d089f9b9SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
5275f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
5276f8829a4aSRandall Stewart {
5277f8829a4aSRandall Stewart 	uint32_t high_tsn;
5278f8829a4aSRandall Stewart 	int fwd_tsn_seen = 0, data_processed = 0;
5279ff1ffd74SMichael Tuexen 	struct mbuf *m = *mm, *op_err;
5280ff1ffd74SMichael Tuexen 	char msg[SCTP_DIAG_INFO_LEN];
5281f8829a4aSRandall Stewart 	int un_sent;
5282493d8e5aSRandall Stewart 	int cnt_ctrl_ready = 0;
528355b175e7SMichael Tuexen 	struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
5284a8775ad9SMichael Tuexen 	struct sctp_tcb *stcb = NULL;
5285e3d6ef0bSMichael Tuexen 	struct sctp_nets *net = NULL;
5286f8829a4aSRandall Stewart 
5287f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvdatagrams);
5288f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
5289f8829a4aSRandall Stewart 	sctp_audit_log(0xE0, 1);
5290f8829a4aSRandall Stewart 	sctp_auditing(0, inp, stcb, net);
5291f8829a4aSRandall Stewart #endif
5292a8775ad9SMichael Tuexen 	if (compute_crc != 0) {
5293a8775ad9SMichael Tuexen 		uint32_t check, calc_check;
5294f8829a4aSRandall Stewart 
5295a8775ad9SMichael Tuexen 		check = sh->checksum;
5296a8775ad9SMichael Tuexen 		sh->checksum = 0;
5297a8775ad9SMichael Tuexen 		calc_check = sctp_calculate_cksum(m, iphlen);
5298a8775ad9SMichael Tuexen 		sh->checksum = check;
5299a8775ad9SMichael Tuexen 		if (calc_check != check) {
5300a8775ad9SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x  m:%p mlen:%d iphlen:%d\n",
5301dd294dceSMichael Tuexen 			    calc_check, check, (void *)m, length, iphlen);
5302a8775ad9SMichael Tuexen 			stcb = sctp_findassociation_addr(m, offset, src, dst,
5303a8775ad9SMichael Tuexen 			    sh, ch, &inp, &net, vrf_id);
53044474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
53053cf729a9SMichael Tuexen 			if ((ch->chunk_type != SCTP_INITIATION) &&
53063cf729a9SMichael Tuexen 			    (net != NULL) && (net->port != port)) {
5307a8775ad9SMichael Tuexen 				if (net->port == 0) {
53083cf729a9SMichael Tuexen 					/* UDP encapsulation turned on. */
53093cf729a9SMichael Tuexen 					net->mtu -= sizeof(struct udphdr);
53103cf729a9SMichael Tuexen 					if (stcb->asoc.smallest_mtu > net->mtu) {
53113cf729a9SMichael Tuexen 						sctp_pathmtu_adjustment(stcb, net->mtu);
53123cf729a9SMichael Tuexen 					}
53133cf729a9SMichael Tuexen 				} else if (port == 0) {
53143cf729a9SMichael Tuexen 					/* UDP encapsulation turned off. */
53153cf729a9SMichael Tuexen 					net->mtu += sizeof(struct udphdr);
53163cf729a9SMichael Tuexen 					/* XXX Update smallest_mtu */
5317a8775ad9SMichael Tuexen 				}
5318a8775ad9SMichael Tuexen 				net->port = port;
5319a8775ad9SMichael Tuexen 			}
53204474d71aSMichael Tuexen #endif
53215fe29cdfSMichael Tuexen 			if (net != NULL) {
5322457b4b88SMichael Tuexen 				net->flowtype = mflowtype;
53235fe29cdfSMichael Tuexen 				net->flowid = mflowid;
5324a8775ad9SMichael Tuexen 			}
53251e88cc8bSMichael Tuexen 			SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5326a8775ad9SMichael Tuexen 			if ((inp != NULL) && (stcb != NULL)) {
5327a8775ad9SMichael Tuexen 				sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
5328a8775ad9SMichael Tuexen 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5329a8775ad9SMichael Tuexen 			} else if ((inp != NULL) && (stcb == NULL)) {
5330a8775ad9SMichael Tuexen 				inp_decr = inp;
5331a8775ad9SMichael Tuexen 			}
5332a8775ad9SMichael Tuexen 			SCTP_STAT_INCR(sctps_badsum);
5333a8775ad9SMichael Tuexen 			SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5334a8775ad9SMichael Tuexen 			goto out;
5335a8775ad9SMichael Tuexen 		}
5336a8775ad9SMichael Tuexen 	}
5337a8775ad9SMichael Tuexen 	/* Destination port of 0 is illegal, based on RFC4960. */
5338a8775ad9SMichael Tuexen 	if (sh->dest_port == 0) {
5339a8775ad9SMichael Tuexen 		SCTP_STAT_INCR(sctps_hdrops);
5340a8775ad9SMichael Tuexen 		goto out;
5341a8775ad9SMichael Tuexen 	}
5342a8775ad9SMichael Tuexen 	stcb = sctp_findassociation_addr(m, offset, src, dst,
5343a8775ad9SMichael Tuexen 	    sh, ch, &inp, &net, vrf_id);
53444474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
53453cf729a9SMichael Tuexen 	if ((ch->chunk_type != SCTP_INITIATION) &&
53463cf729a9SMichael Tuexen 	    (net != NULL) && (net->port != port)) {
5347a8775ad9SMichael Tuexen 		if (net->port == 0) {
53483cf729a9SMichael Tuexen 			/* UDP encapsulation turned on. */
53493cf729a9SMichael Tuexen 			net->mtu -= sizeof(struct udphdr);
53503cf729a9SMichael Tuexen 			if (stcb->asoc.smallest_mtu > net->mtu) {
53513cf729a9SMichael Tuexen 				sctp_pathmtu_adjustment(stcb, net->mtu);
53523cf729a9SMichael Tuexen 			}
53533cf729a9SMichael Tuexen 		} else if (port == 0) {
53543cf729a9SMichael Tuexen 			/* UDP encapsulation turned off. */
53553cf729a9SMichael Tuexen 			net->mtu += sizeof(struct udphdr);
53563cf729a9SMichael Tuexen 			/* XXX Update smallest_mtu */
5357a8775ad9SMichael Tuexen 		}
5358a8775ad9SMichael Tuexen 		net->port = port;
5359a8775ad9SMichael Tuexen 	}
53604474d71aSMichael Tuexen #endif
53615fe29cdfSMichael Tuexen 	if (net != NULL) {
5362457b4b88SMichael Tuexen 		net->flowtype = mflowtype;
53635fe29cdfSMichael Tuexen 		net->flowid = mflowid;
5364a8775ad9SMichael Tuexen 	}
5365a8775ad9SMichael Tuexen 	if (inp == NULL) {
53661e88cc8bSMichael Tuexen 		SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5367a8775ad9SMichael Tuexen 		SCTP_STAT_INCR(sctps_noport);
5368a8775ad9SMichael Tuexen 		if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
5369a8775ad9SMichael Tuexen 			goto out;
5370a8775ad9SMichael Tuexen 		}
5371a8775ad9SMichael Tuexen 		if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5372a8775ad9SMichael Tuexen 			sctp_send_shutdown_complete2(src, dst, sh,
5373d089f9b9SMichael Tuexen 			    mflowtype, mflowid, fibnum,
5374a8775ad9SMichael Tuexen 			    vrf_id, port);
5375a8775ad9SMichael Tuexen 			goto out;
5376a8775ad9SMichael Tuexen 		}
5377a8775ad9SMichael Tuexen 		if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5378a8775ad9SMichael Tuexen 			goto out;
5379a8775ad9SMichael Tuexen 		}
5380a8775ad9SMichael Tuexen 		if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
5381a8775ad9SMichael Tuexen 			if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
5382a8775ad9SMichael Tuexen 			    ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
5383a8775ad9SMichael Tuexen 			    (ch->chunk_type != SCTP_INIT))) {
5384ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5385ff1ffd74SMichael Tuexen 				    "Out of the blue");
5386a8775ad9SMichael Tuexen 				sctp_send_abort(m, iphlen, src, dst,
5387ff1ffd74SMichael Tuexen 				    sh, 0, op_err,
5388d089f9b9SMichael Tuexen 				    mflowtype, mflowid, fibnum,
5389a8775ad9SMichael Tuexen 				    vrf_id, port);
5390a8775ad9SMichael Tuexen 			}
5391a8775ad9SMichael Tuexen 		}
5392a8775ad9SMichael Tuexen 		goto out;
5393a8775ad9SMichael Tuexen 	} else if (stcb == NULL) {
5394a8775ad9SMichael Tuexen 		inp_decr = inp;
5395a8775ad9SMichael Tuexen 	}
5396b3f1ea41SRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5397dd294dceSMichael Tuexen 	    (void *)m, iphlen, offset, length, (void *)stcb);
5398f8829a4aSRandall Stewart 	if (stcb) {
5399f8829a4aSRandall Stewart 		/* always clear this before beginning a packet */
5400f8829a4aSRandall Stewart 		stcb->asoc.authenticated = 0;
5401f8829a4aSRandall Stewart 		stcb->asoc.seen_a_sack_this_pkt = 0;
54022afb3e84SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5403dd294dceSMichael Tuexen 		    (void *)stcb, stcb->asoc.state);
54042afb3e84SRandall Stewart 
5405c4739e2fSRandall Stewart 		if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5406c4739e2fSRandall Stewart 		    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
540763981c2bSRandall Stewart 			/*-
540863981c2bSRandall Stewart 			 * If we hit here, we had a ref count
540963981c2bSRandall Stewart 			 * up when the assoc was aborted and the
541063981c2bSRandall Stewart 			 * timer is clearing out the assoc, we should
541163981c2bSRandall Stewart 			 * NOT respond to any packet.. its OOTB.
541263981c2bSRandall Stewart 			 */
541363981c2bSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
5414a8775ad9SMichael Tuexen 			stcb = NULL;
54151e88cc8bSMichael Tuexen 			SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5416999f86d6SMichael Tuexen 			SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5417ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5418ff1ffd74SMichael Tuexen 			    msg);
5419ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5420d089f9b9SMichael Tuexen 			    mflowtype, mflowid, inp->fibnum,
5421c54a18d2SRandall Stewart 			    vrf_id, port);
5422a8775ad9SMichael Tuexen 			goto out;
542363981c2bSRandall Stewart 		}
5424f8829a4aSRandall Stewart 	}
5425f8829a4aSRandall Stewart 	if (IS_SCTP_CONTROL(ch)) {
5426f8829a4aSRandall Stewart 		/* process the control portion of the SCTP packet */
54273c503c28SRandall Stewart 		/* sa_ignore NO_NULL_CHK */
5428b1754ad1SMichael Tuexen 		stcb = sctp_process_control(m, iphlen, &offset, length,
5429b1754ad1SMichael Tuexen 		    src, dst, sh, ch,
5430f30ac432SMichael Tuexen 		    inp, stcb, &net, &fwd_tsn_seen,
5431d089f9b9SMichael Tuexen 		    mflowtype, mflowid, fibnum,
5432f30ac432SMichael Tuexen 		    vrf_id, port);
5433f8829a4aSRandall Stewart 		if (stcb) {
5434f8829a4aSRandall Stewart 			/*
5435f8829a4aSRandall Stewart 			 * This covers us if the cookie-echo was there and
5436f8829a4aSRandall Stewart 			 * it changes our INP.
5437f8829a4aSRandall Stewart 			 */
5438f8829a4aSRandall Stewart 			inp = stcb->sctp_ep;
54394474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
54403cf729a9SMichael Tuexen 			if ((ch->chunk_type != SCTP_INITIATION) &&
54413cf729a9SMichael Tuexen 			    (net != NULL) && (net->port != port)) {
5442b3f1ea41SRandall Stewart 				if (net->port == 0) {
54433cf729a9SMichael Tuexen 					/* UDP encapsulation turned on. */
54443cf729a9SMichael Tuexen 					net->mtu -= sizeof(struct udphdr);
54453cf729a9SMichael Tuexen 					if (stcb->asoc.smallest_mtu > net->mtu) {
54463cf729a9SMichael Tuexen 						sctp_pathmtu_adjustment(stcb, net->mtu);
54473cf729a9SMichael Tuexen 					}
54483cf729a9SMichael Tuexen 				} else if (port == 0) {
54493cf729a9SMichael Tuexen 					/* UDP encapsulation turned off. */
54503cf729a9SMichael Tuexen 					net->mtu += sizeof(struct udphdr);
54513cf729a9SMichael Tuexen 					/* XXX Update smallest_mtu */
5452b3f1ea41SRandall Stewart 				}
5453b3f1ea41SRandall Stewart 				net->port = port;
5454b3f1ea41SRandall Stewart 			}
54554474d71aSMichael Tuexen #endif
5456f8829a4aSRandall Stewart 		}
5457f8829a4aSRandall Stewart 	} else {
5458f8829a4aSRandall Stewart 		/*
5459f8829a4aSRandall Stewart 		 * no control chunks, so pre-process DATA chunks (these
5460f8829a4aSRandall Stewart 		 * checks are taken care of by control processing)
5461f8829a4aSRandall Stewart 		 */
5462f8829a4aSRandall Stewart 
5463f8829a4aSRandall Stewart 		/*
5464f8829a4aSRandall Stewart 		 * if DATA only packet, and auth is required, then punt...
5465f8829a4aSRandall Stewart 		 * can't have authenticated without any AUTH (control)
5466f8829a4aSRandall Stewart 		 * chunks
5467f8829a4aSRandall Stewart 		 */
5468b3f1ea41SRandall Stewart 		if ((stcb != NULL) &&
5469b3f1ea41SRandall Stewart 		    sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5470f8829a4aSRandall Stewart 			/* "silently" ignore */
54711e88cc8bSMichael Tuexen 			SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5472f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvauthmissing);
5473a8775ad9SMichael Tuexen 			goto out;
5474f8829a4aSRandall Stewart 		}
5475f8829a4aSRandall Stewart 		if (stcb == NULL) {
5476f8829a4aSRandall Stewart 			/* out of the blue DATA chunk */
54771e88cc8bSMichael Tuexen 			SCTP_PROBE5(receive, NULL, NULL, m, NULL, sh);
5478999f86d6SMichael Tuexen 			SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5479ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5480ff1ffd74SMichael Tuexen 			    msg);
5481ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5482d089f9b9SMichael Tuexen 			    mflowtype, mflowid, fibnum,
5483c54a18d2SRandall Stewart 			    vrf_id, port);
5484a8775ad9SMichael Tuexen 			goto out;
5485f8829a4aSRandall Stewart 		}
5486f8829a4aSRandall Stewart 		if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5487f8829a4aSRandall Stewart 			/* v_tag mismatch! */
54881e88cc8bSMichael Tuexen 			SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5489f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_badvtag);
5490a8775ad9SMichael Tuexen 			goto out;
5491f8829a4aSRandall Stewart 		}
5492f8829a4aSRandall Stewart 	}
5493f8829a4aSRandall Stewart 
54941e88cc8bSMichael Tuexen 	SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5495f8829a4aSRandall Stewart 	if (stcb == NULL) {
5496f8829a4aSRandall Stewart 		/*
5497f8829a4aSRandall Stewart 		 * no valid TCB for this packet, or we found it's a bad
5498f8829a4aSRandall Stewart 		 * packet while processing control, or we're done with this
5499f8829a4aSRandall Stewart 		 * packet (done or skip rest of data), so we drop it...
5500f8829a4aSRandall Stewart 		 */
5501a8775ad9SMichael Tuexen 		goto out;
5502f8829a4aSRandall Stewart 	}
55030053ed28SMichael Tuexen 
5504f8829a4aSRandall Stewart 	/*
5505f8829a4aSRandall Stewart 	 * DATA chunk processing
5506f8829a4aSRandall Stewart 	 */
5507f8829a4aSRandall Stewart 	/* plow through the data chunks while length > offset */
5508f8829a4aSRandall Stewart 
5509f8829a4aSRandall Stewart 	/*
5510f8829a4aSRandall Stewart 	 * Rest should be DATA only.  Check authentication state if AUTH for
5511f8829a4aSRandall Stewart 	 * DATA is required.
5512f8829a4aSRandall Stewart 	 */
5513b3f1ea41SRandall Stewart 	if ((length > offset) &&
5514b3f1ea41SRandall Stewart 	    (stcb != NULL) &&
5515b3f1ea41SRandall Stewart 	    sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5516f8829a4aSRandall Stewart 	    !stcb->asoc.authenticated) {
5517f8829a4aSRandall Stewart 		/* "silently" ignore */
5518f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvauthmissing);
5519ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_AUTH1,
5520ad81507eSRandall Stewart 		    "Data chunk requires AUTH, skipped\n");
5521a5d547adSRandall Stewart 		goto trigger_send;
5522f8829a4aSRandall Stewart 	}
5523f8829a4aSRandall Stewart 	if (length > offset) {
5524f8829a4aSRandall Stewart 		int retval;
5525f8829a4aSRandall Stewart 
5526f8829a4aSRandall Stewart 		/*
5527f8829a4aSRandall Stewart 		 * First check to make sure our state is correct. We would
5528f8829a4aSRandall Stewart 		 * not get here unless we really did have a tag, so we don't
5529f8829a4aSRandall Stewart 		 * abort if this happens, just dump the chunk silently.
5530f8829a4aSRandall Stewart 		 */
5531839d21d6SMichael Tuexen 		switch (SCTP_GET_STATE(stcb)) {
5532f8829a4aSRandall Stewart 		case SCTP_STATE_COOKIE_ECHOED:
5533f8829a4aSRandall Stewart 			/*
5534f8829a4aSRandall Stewart 			 * we consider data with valid tags in this state
5535f8829a4aSRandall Stewart 			 * shows us the cookie-ack was lost. Imply it was
5536f8829a4aSRandall Stewart 			 * there.
5537f8829a4aSRandall Stewart 			 */
5538f8829a4aSRandall Stewart 			sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5539f8829a4aSRandall Stewart 			break;
5540f8829a4aSRandall Stewart 		case SCTP_STATE_COOKIE_WAIT:
5541f8829a4aSRandall Stewart 			/*
5542f8829a4aSRandall Stewart 			 * We consider OOTB any data sent during asoc setup.
5543f8829a4aSRandall Stewart 			 */
5544999f86d6SMichael Tuexen 			SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5545ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5546ff1ffd74SMichael Tuexen 			    msg);
5547ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5548d089f9b9SMichael Tuexen 			    mflowtype, mflowid, inp->fibnum,
5549c54a18d2SRandall Stewart 			    vrf_id, port);
5550a8775ad9SMichael Tuexen 			goto out;
555152be287eSRandall Stewart 			/* sa_ignore NOTREACHED */
5552f8829a4aSRandall Stewart 			break;
5553f8829a4aSRandall Stewart 		case SCTP_STATE_EMPTY:	/* should not happen */
5554f8829a4aSRandall Stewart 		case SCTP_STATE_INUSE:	/* should not happen */
5555f8829a4aSRandall Stewart 		case SCTP_STATE_SHUTDOWN_RECEIVED:	/* This is a peer error */
5556f8829a4aSRandall Stewart 		case SCTP_STATE_SHUTDOWN_ACK_SENT:
5557f8829a4aSRandall Stewart 		default:
5558a8775ad9SMichael Tuexen 			goto out;
555952be287eSRandall Stewart 			/* sa_ignore NOTREACHED */
5560f8829a4aSRandall Stewart 			break;
5561f8829a4aSRandall Stewart 		case SCTP_STATE_OPEN:
5562f8829a4aSRandall Stewart 		case SCTP_STATE_SHUTDOWN_SENT:
5563f8829a4aSRandall Stewart 			break;
5564f8829a4aSRandall Stewart 		}
5565f8829a4aSRandall Stewart 		/* plow through the data chunks while length > offset */
5566b1754ad1SMichael Tuexen 		retval = sctp_process_data(mm, iphlen, &offset, length,
5567e7e71dd7SMichael Tuexen 		    inp, stcb, net, &high_tsn);
5568f8829a4aSRandall Stewart 		if (retval == 2) {
5569f8829a4aSRandall Stewart 			/*
5570f8829a4aSRandall Stewart 			 * The association aborted, NO UNLOCK needed since
5571f8829a4aSRandall Stewart 			 * the association is destroyed.
5572f8829a4aSRandall Stewart 			 */
5573a8775ad9SMichael Tuexen 			stcb = NULL;
5574a8775ad9SMichael Tuexen 			goto out;
5575f8829a4aSRandall Stewart 		}
5576b954d816SMichael Tuexen 		if (retval == 0) {
5577f8829a4aSRandall Stewart 			data_processed = 1;
5578b954d816SMichael Tuexen 		}
5579f8829a4aSRandall Stewart 		/*
5580f8829a4aSRandall Stewart 		 * Anything important needs to have been m_copy'ed in
5581f8829a4aSRandall Stewart 		 * process_data
5582f8829a4aSRandall Stewart 		 */
5583f8829a4aSRandall Stewart 	}
55840053ed28SMichael Tuexen 
5585493d8e5aSRandall Stewart 	/* take care of ecn */
558660990c0cSMichael Tuexen 	if ((data_processed == 1) &&
5587f342355aSMichael Tuexen 	    (stcb->asoc.ecn_supported == 1) &&
5588c446091bSMichael Tuexen 	    ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5589493d8e5aSRandall Stewart 		/* Yep, we need to add a ECNE */
5590493d8e5aSRandall Stewart 		sctp_send_ecn_echo(stcb, net, high_tsn);
5591493d8e5aSRandall Stewart 	}
55920053ed28SMichael Tuexen 
5593f8829a4aSRandall Stewart 	if ((data_processed == 0) && (fwd_tsn_seen)) {
55948f777478SMichael Tuexen 		int was_a_gap;
55958f777478SMichael Tuexen 		uint32_t highest_tsn;
5596f8829a4aSRandall Stewart 
559720b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
55988f777478SMichael Tuexen 			highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
55998f777478SMichael Tuexen 		} else {
56008f777478SMichael Tuexen 			highest_tsn = stcb->asoc.highest_tsn_inside_map;
5601f8829a4aSRandall Stewart 		}
560220b07a4dSMichael Tuexen 		was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
56038933fa13SRandall Stewart 		stcb->asoc.send_sack = 1;
56047215cc1bSMichael Tuexen 		sctp_sack_check(stcb, was_a_gap);
56058933fa13SRandall Stewart 	} else if (fwd_tsn_seen) {
56068933fa13SRandall Stewart 		stcb->asoc.send_sack = 1;
5607f8829a4aSRandall Stewart 	}
5608f8829a4aSRandall Stewart 	/* trigger send of any chunks in queue... */
5609a5d547adSRandall Stewart trigger_send:
5610f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
5611f8829a4aSRandall Stewart 	sctp_audit_log(0xE0, 2);
5612f8829a4aSRandall Stewart 	sctp_auditing(1, inp, stcb, net);
5613f8829a4aSRandall Stewart #endif
5614ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT1,
5615ad81507eSRandall Stewart 	    "Check for chunk output prw:%d tqe:%d tf=%d\n",
5616f8829a4aSRandall Stewart 	    stcb->asoc.peers_rwnd,
5617f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5618f8829a4aSRandall Stewart 	    stcb->asoc.total_flight);
5619f8829a4aSRandall Stewart 	un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5620493d8e5aSRandall Stewart 	if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
5621493d8e5aSRandall Stewart 		cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
5622493d8e5aSRandall Stewart 	}
56235114dccbSMichael Tuexen 	if (!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue) ||
56245114dccbSMichael Tuexen 	    cnt_ctrl_ready ||
56255114dccbSMichael Tuexen 	    stcb->asoc.trigger_reset ||
562664c8fc5dSMichael Tuexen 	    ((un_sent > 0) &&
562764c8fc5dSMichael Tuexen 	    (stcb->asoc.peers_rwnd > 0 || stcb->asoc.total_flight == 0))) {
5628ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5629ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5630ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5631f8829a4aSRandall Stewart 	}
5632f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
5633f8829a4aSRandall Stewart 	sctp_audit_log(0xE0, 3);
5634f8829a4aSRandall Stewart 	sctp_auditing(2, inp, stcb, net);
5635f8829a4aSRandall Stewart #endif
5636a8775ad9SMichael Tuexen out:
5637a8775ad9SMichael Tuexen 	if (stcb != NULL) {
5638f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
5639a8775ad9SMichael Tuexen 	}
5640a8775ad9SMichael Tuexen 	if (inp_decr != NULL) {
5641a8775ad9SMichael Tuexen 		/* reduce ref-count */
5642a8775ad9SMichael Tuexen 		SCTP_INP_WLOCK(inp_decr);
5643a8775ad9SMichael Tuexen 		SCTP_INP_DECR_REF(inp_decr);
5644a8775ad9SMichael Tuexen 		SCTP_INP_WUNLOCK(inp_decr);
5645a8775ad9SMichael Tuexen 	}
56466e55db54SRandall Stewart 	return;
5647f8829a4aSRandall Stewart }
5648f8829a4aSRandall Stewart 
5649e6194c2eSMichael Tuexen #ifdef INET
5650f8829a4aSRandall Stewart void
5651af83f5d7SRoman Divacky sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
5652f8829a4aSRandall Stewart {
5653139bc87fSRandall Stewart 	struct mbuf *m;
5654f8829a4aSRandall Stewart 	int iphlen;
5655ad21a364SRandall Stewart 	uint32_t vrf_id = 0;
5656f8829a4aSRandall Stewart 	uint8_t ecn_bits;
5657b1754ad1SMichael Tuexen 	struct sockaddr_in src, dst;
5658f8829a4aSRandall Stewart 	struct ip *ip;
5659f8829a4aSRandall Stewart 	struct sctphdr *sh;
5660f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
56616dc5aabcSMichael Tuexen 	int length, offset;
5662a8775ad9SMichael Tuexen 	uint8_t compute_crc;
5663a8775ad9SMichael Tuexen 	uint32_t mflowid;
5664457b4b88SMichael Tuexen 	uint8_t mflowtype;
5665d089f9b9SMichael Tuexen 	uint16_t fibnum;
56669c7635e1SMichael Tuexen 
56676dc5aabcSMichael Tuexen 	iphlen = off;
566817205eccSRandall Stewart 	if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
566917205eccSRandall Stewart 		SCTP_RELEASE_PKT(i_pak);
567017205eccSRandall Stewart 		return;
567117205eccSRandall Stewart 	}
5672139bc87fSRandall Stewart 	m = SCTP_HEADER_TO_CHAIN(i_pak);
5673f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
5674f8829a4aSRandall Stewart 	/* Log in any input mbufs */
5675b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
56764be807c4SMichael Tuexen 		sctp_log_mbc(m, SCTP_MBUF_INPUT);
567780fefe0aSRandall Stewart 	}
5678f8829a4aSRandall Stewart #endif
5679207304d4SRandall Stewart #ifdef SCTP_PACKET_LOGGING
56806dc5aabcSMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
5681f9384252SMichael Tuexen 		sctp_packet_log(m);
56826dc5aabcSMichael Tuexen 	}
5683207304d4SRandall Stewart #endif
5684a8775ad9SMichael Tuexen 	SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
56851a94cdbeSMichael Tuexen 	    "sctp_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
5686a8775ad9SMichael Tuexen 	    m->m_pkthdr.len,
5687a8775ad9SMichael Tuexen 	    if_name(m->m_pkthdr.rcvif),
56881a94cdbeSMichael Tuexen 	    (int)m->m_pkthdr.csum_flags, CSUM_BITS);
5689f30ac432SMichael Tuexen 	mflowid = m->m_pkthdr.flowid;
5690457b4b88SMichael Tuexen 	mflowtype = M_HASHTYPE_GET(m);
5691d089f9b9SMichael Tuexen 	fibnum = M_GETFIB(m);
56926dc5aabcSMichael Tuexen 	SCTP_STAT_INCR(sctps_recvpackets);
56936dc5aabcSMichael Tuexen 	SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
56946dc5aabcSMichael Tuexen 	/* Get IP, SCTP, and first chunk header together in the first mbuf. */
56956dc5aabcSMichael Tuexen 	offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5696139bc87fSRandall Stewart 	if (SCTP_BUF_LEN(m) < offset) {
5697b1754ad1SMichael Tuexen 		if ((m = m_pullup(m, offset)) == NULL) {
5698f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_hdrops);
5699f8829a4aSRandall Stewart 			return;
5700f8829a4aSRandall Stewart 		}
5701f8829a4aSRandall Stewart 	}
5702b1754ad1SMichael Tuexen 	ip = mtod(m, struct ip *);
57036dc5aabcSMichael Tuexen 	sh = (struct sctphdr *)((caddr_t)ip + iphlen);
57046dc5aabcSMichael Tuexen 	ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
57056dc5aabcSMichael Tuexen 	offset -= sizeof(struct sctp_chunkhdr);
5706b1754ad1SMichael Tuexen 	memset(&src, 0, sizeof(struct sockaddr_in));
5707b1754ad1SMichael Tuexen 	src.sin_family = AF_INET;
5708b1754ad1SMichael Tuexen 	src.sin_len = sizeof(struct sockaddr_in);
5709b1754ad1SMichael Tuexen 	src.sin_port = sh->src_port;
5710b1754ad1SMichael Tuexen 	src.sin_addr = ip->ip_src;
5711b1754ad1SMichael Tuexen 	memset(&dst, 0, sizeof(struct sockaddr_in));
5712b1754ad1SMichael Tuexen 	dst.sin_family = AF_INET;
5713b1754ad1SMichael Tuexen 	dst.sin_len = sizeof(struct sockaddr_in);
5714b1754ad1SMichael Tuexen 	dst.sin_port = sh->dest_port;
5715b1754ad1SMichael Tuexen 	dst.sin_addr = ip->ip_dst;
57168ad458a4SGleb Smirnoff 	length = ntohs(ip->ip_len);
57176dc5aabcSMichael Tuexen 	/* Validate mbuf chain length with IP payload length. */
5718a8775ad9SMichael Tuexen 	if (SCTP_HEADER_LEN(m) != length) {
57196dc5aabcSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT1,
5720a8775ad9SMichael Tuexen 		    "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
5721a99b6783SRandall Stewart 		SCTP_STAT_INCR(sctps_hdrops);
5722a8775ad9SMichael Tuexen 		goto out;
5723a99b6783SRandall Stewart 	}
5724f8829a4aSRandall Stewart 	/* SCTP does not allow broadcasts or multicasts */
5725b1754ad1SMichael Tuexen 	if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
5726a8775ad9SMichael Tuexen 		goto out;
5727f8829a4aSRandall Stewart 	}
5728b1754ad1SMichael Tuexen 	if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
5729a8775ad9SMichael Tuexen 		goto out;
5730f8829a4aSRandall Stewart 	}
5731a8775ad9SMichael Tuexen 	ecn_bits = ip->ip_tos;
5732a99b6783SRandall Stewart 	if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
5733a99b6783SRandall Stewart 		SCTP_STAT_INCR(sctps_recvhwcrc);
5734a8775ad9SMichael Tuexen 		compute_crc = 0;
5735a8775ad9SMichael Tuexen 	} else {
5736a99b6783SRandall Stewart 		SCTP_STAT_INCR(sctps_recvswcrc);
5737a8775ad9SMichael Tuexen 		compute_crc = 1;
5738f8829a4aSRandall Stewart 	}
5739b1754ad1SMichael Tuexen 	sctp_common_input_processing(&m, iphlen, offset, length,
5740b1754ad1SMichael Tuexen 	    (struct sockaddr *)&src,
5741b1754ad1SMichael Tuexen 	    (struct sockaddr *)&dst,
5742a8775ad9SMichael Tuexen 	    sh, ch,
5743a8775ad9SMichael Tuexen 	    compute_crc,
5744a8775ad9SMichael Tuexen 	    ecn_bits,
5745d089f9b9SMichael Tuexen 	    mflowtype, mflowid, fibnum,
5746f30ac432SMichael Tuexen 	    vrf_id, port);
5747a8775ad9SMichael Tuexen out:
5748f8829a4aSRandall Stewart 	if (m) {
5749f8829a4aSRandall Stewart 		sctp_m_freem(m);
5750f8829a4aSRandall Stewart 	}
5751f8829a4aSRandall Stewart 	return;
5752f8829a4aSRandall Stewart }
5753bfc46083SRandall Stewart 
57542f9e6db0SMichael Tuexen #if defined(SCTP_MCORE_INPUT) && defined(SMP)
57550071ee5eSRandall Stewart extern int *sctp_cpuarry;
57560071ee5eSRandall Stewart #endif
5757bfc46083SRandall Stewart 
57588f5a8818SKevin Lo int
575982eaf95eSMichael Tuexen sctp_input(struct mbuf **mp, int *offp, int proto SCTP_UNUSED)
5760c54a18d2SRandall Stewart {
57618f5a8818SKevin Lo 	struct mbuf *m;
57628f5a8818SKevin Lo 	int off;
57638f5a8818SKevin Lo 
57648f5a8818SKevin Lo 	m = *mp;
57658f5a8818SKevin Lo 	off = *offp;
57662f9e6db0SMichael Tuexen #if defined(SCTP_MCORE_INPUT) && defined(SMP)
576782eaf95eSMichael Tuexen 	if (mp_ncpus > 1) {
5768bfc46083SRandall Stewart 		struct ip *ip;
5769bfc46083SRandall Stewart 		struct sctphdr *sh;
5770bfc46083SRandall Stewart 		int offset;
5771bfc46083SRandall Stewart 		int cpu_to_use;
577238521fb9SRandall Stewart 		uint32_t flowid, tag;
5773bfc46083SRandall Stewart 
5774457b4b88SMichael Tuexen 		if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
577538521fb9SRandall Stewart 			flowid = m->m_pkthdr.flowid;
577638521fb9SRandall Stewart 		} else {
577738521fb9SRandall Stewart 			/*
577838521fb9SRandall Stewart 			 * No flow id built by lower layers fix it so we
577938521fb9SRandall Stewart 			 * create one.
578038521fb9SRandall Stewart 			 */
5781b1754ad1SMichael Tuexen 			offset = off + sizeof(struct sctphdr);
5782bfc46083SRandall Stewart 			if (SCTP_BUF_LEN(m) < offset) {
5783b1754ad1SMichael Tuexen 				if ((m = m_pullup(m, offset)) == NULL) {
5784bfc46083SRandall Stewart 					SCTP_STAT_INCR(sctps_hdrops);
57858f5a8818SKevin Lo 					return (IPPROTO_DONE);
5786bfc46083SRandall Stewart 				}
5787bfc46083SRandall Stewart 			}
5788b1754ad1SMichael Tuexen 			ip = mtod(m, struct ip *);
5789bfc46083SRandall Stewart 			sh = (struct sctphdr *)((caddr_t)ip + off);
57900071ee5eSRandall Stewart 			tag = htonl(sh->v_tag);
579138521fb9SRandall Stewart 			flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
579238521fb9SRandall Stewart 			m->m_pkthdr.flowid = flowid;
579336ad8372SSepherosa Ziehau 			M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE_HASH);
57940071ee5eSRandall Stewart 		}
579538521fb9SRandall Stewart 		cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
5796bfc46083SRandall Stewart 		sctp_queue_to_mcore(m, off, cpu_to_use);
57978f5a8818SKevin Lo 		return (IPPROTO_DONE);
5798bfc46083SRandall Stewart 	}
5799bfc46083SRandall Stewart #endif
5800bfc46083SRandall Stewart 	sctp_input_with_port(m, off, 0);
58018f5a8818SKevin Lo 	return (IPPROTO_DONE);
5802c54a18d2SRandall Stewart }
5803e6194c2eSMichael Tuexen #endif
5804