xref: /freebsd/sys/netinet/sctp_input.c (revision eec6aed5b8c848841ae8d25940e0a333e5039ce9)
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 */
417a2b42326SMichael Tuexen 		sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, 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... */
788410a3b1eSMichael Tuexen 	sctp_abort_notification(stcb, 1, 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) {
1143410a3b1eSMichael Tuexen 					sctp_abort_notification(stcb, 0, 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) {
1755*eec6aed5SMichael Tuexen 			struct sctp_tcb *local_stcb;
1756*eec6aed5SMichael 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 			 */
1762*eec6aed5SMichael 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,
1767*eec6aed5SMichael Tuexen 			    vrf_id, port);
1768*eec6aed5SMichael Tuexen 			if (local_stcb == NULL) {
1769*eec6aed5SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
1770*eec6aed5SMichael Tuexen 			}
1771*eec6aed5SMichael 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,
205652be287eSRandall Stewart 	    ntohl(initack_cp->init.initiate_tag), vrf_id,
2057c979034bSMichael Tuexen 	    ntohs(initack_cp->init.num_outbound_streams),
2058ec70917fSMichael Tuexen 	    port,
20598a956abeSMichael Tuexen 	    (struct thread *)NULL,
20608a956abeSMichael Tuexen 	    SCTP_DONT_INITIALIZE_AUTH_PARAMS);
2061f8829a4aSRandall Stewart 	if (stcb == NULL) {
2062f8829a4aSRandall Stewart 		struct mbuf *op_err;
2063f8829a4aSRandall Stewart 
2064f8829a4aSRandall Stewart 		/* memory problem? */
2065ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1,
2066ad81507eSRandall Stewart 		    "process_cookie_new: no room for another TCB!\n");
2067ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2068f8829a4aSRandall Stewart 		sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2069b1754ad1SMichael Tuexen 		    src, dst, sh, op_err,
2070457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2071f30ac432SMichael Tuexen 		    vrf_id, port);
2072f8829a4aSRandall Stewart 		return (NULL);
2073f8829a4aSRandall Stewart 	}
2074f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2075f8829a4aSRandall Stewart 	/* get scope variables out of cookie */
2076a1cb341bSMichael Tuexen 	asoc->scope.ipv4_local_scope = cookie->ipv4_scope;
2077a1cb341bSMichael Tuexen 	asoc->scope.site_scope = cookie->site_scope;
2078a1cb341bSMichael Tuexen 	asoc->scope.local_scope = cookie->local_scope;
2079a1cb341bSMichael Tuexen 	asoc->scope.loopback_scope = cookie->loopback_scope;
2080f8829a4aSRandall Stewart 
2081a1cb341bSMichael Tuexen 	if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2082a1cb341bSMichael Tuexen 	    (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2083f8829a4aSRandall Stewart 		struct mbuf *op_err;
2084f8829a4aSRandall Stewart 
2085f8829a4aSRandall Stewart 		/*
2086f8829a4aSRandall Stewart 		 * Houston we have a problem. The EP changed while the
2087f8829a4aSRandall Stewart 		 * cookie was in flight. Only recourse is to abort the
2088f8829a4aSRandall Stewart 		 * association.
2089f8829a4aSRandall Stewart 		 */
2090ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2091f8829a4aSRandall Stewart 		sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2092b1754ad1SMichael Tuexen 		    src, dst, sh, op_err,
2093457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2094f30ac432SMichael Tuexen 		    vrf_id, port);
2095c4739e2fSRandall Stewart 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2096b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2097f8829a4aSRandall Stewart 		return (NULL);
2098f8829a4aSRandall Stewart 	}
2099f8829a4aSRandall Stewart 	/* process the INIT-ACK info (my info) */
2100830d754dSRandall Stewart 	asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2101f8829a4aSRandall Stewart 	asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2102f8829a4aSRandall Stewart 	asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2103f8829a4aSRandall Stewart 	asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2104c54a18d2SRandall Stewart 	asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2105f8829a4aSRandall Stewart 	asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2106f8829a4aSRandall Stewart 	asoc->str_reset_seq_in = asoc->init_seq_number;
2107f8829a4aSRandall Stewart 
2108f8829a4aSRandall Stewart 	asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2109f8829a4aSRandall Stewart 
2110f8829a4aSRandall Stewart 	/* process the INIT info (peer's info) */
21115f2e1835SMichael Tuexen 	if (sctp_process_init(init_cp, stcb) < 0) {
2112b7d130beSMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2113b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2114f8829a4aSRandall Stewart 		return (NULL);
2115f8829a4aSRandall Stewart 	}
2116f8829a4aSRandall Stewart 	/* load all addresses */
21175f2e1835SMichael Tuexen 	if ((retval = sctp_load_addresses_from_init(stcb, m,
21185f2e1835SMichael Tuexen 	    init_offset + sizeof(struct sctp_init_chunk),
21195f2e1835SMichael Tuexen 	    initack_offset, src, dst, init_src, port)) < 0) {
2120b7d130beSMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2121b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2122f8829a4aSRandall Stewart 		return (NULL);
2123f8829a4aSRandall Stewart 	}
2124f8829a4aSRandall Stewart 	/*
2125f8829a4aSRandall Stewart 	 * verify any preceding AUTH chunk that was skipped
2126f8829a4aSRandall Stewart 	 */
2127f8829a4aSRandall Stewart 	/* pull the local authentication parameters from the cookie/init-ack */
2128f8829a4aSRandall Stewart 	sctp_auth_get_cookie_params(stcb, m,
2129f8829a4aSRandall Stewart 	    initack_offset + sizeof(struct sctp_init_ack_chunk),
2130f8829a4aSRandall Stewart 	    initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2131f8829a4aSRandall Stewart 	if (auth_skipped) {
2132f8829a4aSRandall Stewart 		struct sctp_auth_chunk *auth;
2133f8829a4aSRandall Stewart 
21348262311cSMichael Tuexen 		if (auth_len <= SCTP_CHUNK_BUFFER_SIZE) {
213597feba89SMichael Tuexen 			auth = (struct sctp_auth_chunk *)sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
213697feba89SMichael Tuexen 		} else {
213797feba89SMichael Tuexen 			auth = NULL;
213897feba89SMichael Tuexen 		}
2139ad81507eSRandall Stewart 		if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2140f8829a4aSRandall Stewart 			/* auth HMAC failed, dump the assoc and packet */
2141ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_AUTH1,
2142ad81507eSRandall Stewart 			    "COOKIE-ECHO: AUTH failed\n");
2143b7d130beSMichael Tuexen 			(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2144b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_21);
2145f8829a4aSRandall Stewart 			return (NULL);
2146f8829a4aSRandall Stewart 		} else {
2147f8829a4aSRandall Stewart 			/* remaining chunks checked... good to go */
2148f8829a4aSRandall Stewart 			stcb->asoc.authenticated = 1;
2149f8829a4aSRandall Stewart 		}
2150f8829a4aSRandall Stewart 	}
21510053ed28SMichael Tuexen 
2152f8829a4aSRandall Stewart 	/*
2153f8829a4aSRandall Stewart 	 * if we're doing ASCONFs, check to see if we have any new local
2154f8829a4aSRandall Stewart 	 * addresses that need to get added to the peer (eg. addresses
2155f8829a4aSRandall Stewart 	 * changed while cookie echo in flight).  This needs to be done
2156f8829a4aSRandall Stewart 	 * after we go to the OPEN state to do the correct asconf
2157f8829a4aSRandall Stewart 	 * processing. else, make sure we have the correct addresses in our
2158f8829a4aSRandall Stewart 	 * lists
2159f8829a4aSRandall Stewart 	 */
2160f8829a4aSRandall Stewart 
2161f8829a4aSRandall Stewart 	/* warning, we re-use sin, sin6, sa_store here! */
2162f8829a4aSRandall Stewart 	/* pull in local_address (our "from" address) */
2163e6194c2eSMichael Tuexen 	switch (cookie->laddr_type) {
2164e6194c2eSMichael Tuexen #ifdef INET
2165e6194c2eSMichael Tuexen 	case SCTP_IPV4_ADDRESS:
2166f8829a4aSRandall Stewart 		/* source addr is IPv4 */
216724aaac8dSMichael Tuexen 		memset(&store.sin, 0, sizeof(struct sockaddr_in));
216824aaac8dSMichael Tuexen 		store.sin.sin_family = AF_INET;
216924aaac8dSMichael Tuexen 		store.sin.sin_len = sizeof(struct sockaddr_in);
217024aaac8dSMichael Tuexen 		store.sin.sin_addr.s_addr = cookie->laddress[0];
2171e6194c2eSMichael Tuexen 		break;
2172e6194c2eSMichael Tuexen #endif
2173e6194c2eSMichael Tuexen #ifdef INET6
2174e6194c2eSMichael Tuexen 	case SCTP_IPV6_ADDRESS:
2175f8829a4aSRandall Stewart 		/* source addr is IPv6 */
217624aaac8dSMichael Tuexen 		memset(&store.sin6, 0, sizeof(struct sockaddr_in6));
217724aaac8dSMichael Tuexen 		store.sin6.sin6_family = AF_INET6;
217824aaac8dSMichael Tuexen 		store.sin6.sin6_len = sizeof(struct sockaddr_in6);
217924aaac8dSMichael Tuexen 		store.sin6.sin6_scope_id = cookie->scope_id;
218023602b60SMichael Tuexen 		memcpy(&store.sin6.sin6_addr, cookie->laddress, sizeof(struct in6_addr));
2181e6194c2eSMichael Tuexen 		break;
2182e6194c2eSMichael Tuexen #endif
2183e6194c2eSMichael Tuexen 	default:
2184b7d130beSMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2185b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
2186f8829a4aSRandall Stewart 		return (NULL);
2187f8829a4aSRandall Stewart 	}
2188f8829a4aSRandall Stewart 
21891698cbd9SMichael Tuexen 	/* update current state */
21901698cbd9SMichael Tuexen 	SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2191839d21d6SMichael Tuexen 	SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
21921698cbd9SMichael Tuexen 	if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
21931698cbd9SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
21946fb7b4fbSMichael Tuexen 		    stcb->sctp_ep, stcb, NULL);
21951698cbd9SMichael Tuexen 	}
21961698cbd9SMichael Tuexen 	sctp_stop_all_cookie_timers(stcb);
21971698cbd9SMichael Tuexen 	SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
21981698cbd9SMichael Tuexen 	SCTP_STAT_INCR_GAUGE32(sctps_currestab);
21991698cbd9SMichael Tuexen 
2200f8829a4aSRandall Stewart 	/* set up to notify upper layer */
2201f8829a4aSRandall Stewart 	*notification = SCTP_NOTIFY_ASSOC_UP;
2202f8829a4aSRandall Stewart 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2203f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
22045d08768aSMichael Tuexen 	    (!SCTP_IS_LISTENING(inp))) {
2205f8829a4aSRandall Stewart 		/*
2206f8829a4aSRandall Stewart 		 * This is an endpoint that called connect() how it got a
2207f8829a4aSRandall Stewart 		 * cookie that is NEW is a bit of a mystery. It must be that
2208f8829a4aSRandall Stewart 		 * the INIT was sent, but before it got there.. a complete
2209f8829a4aSRandall Stewart 		 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2210f8829a4aSRandall Stewart 		 * should have went to the other code.. not here.. oh well..
2211f8829a4aSRandall Stewart 		 * a bit of protection is worth having..
2212f8829a4aSRandall Stewart 		 */
2213f8829a4aSRandall Stewart 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2214ceaad40aSRandall Stewart 		soisconnected(stcb->sctp_socket);
2215f8829a4aSRandall Stewart 	} else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
22165d08768aSMichael Tuexen 	    (SCTP_IS_LISTENING(inp))) {
2217f8829a4aSRandall Stewart 		/*
2218f8829a4aSRandall Stewart 		 * We don't want to do anything with this one. Since it is
2219f8829a4aSRandall Stewart 		 * the listening guy. The timer will get started for
2220f8829a4aSRandall Stewart 		 * accepted connections in the caller.
2221f8829a4aSRandall Stewart 		 */
2222f8829a4aSRandall Stewart 		;
2223f8829a4aSRandall Stewart 	}
2224f8829a4aSRandall Stewart 	if (stcb->asoc.sctp_autoclose_ticks &&
2225f8829a4aSRandall Stewart 	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2226f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2227f8829a4aSRandall Stewart 	}
2228b54d3a6cSRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2229b15f5411SMichael Tuexen 	*netp = sctp_findnet(stcb, init_src);
2230b15f5411SMichael Tuexen 	if (*netp != NULL) {
22318c8e10b7SMichael Tuexen 		struct timeval old;
22328c8e10b7SMichael Tuexen 
2233b15f5411SMichael Tuexen 		/*
2234b15f5411SMichael Tuexen 		 * Since we did not send a HB, make sure we don't double
2235b15f5411SMichael Tuexen 		 * things.
2236b15f5411SMichael Tuexen 		 */
2237b15f5411SMichael Tuexen 		(*netp)->hb_responded = 1;
2238b15f5411SMichael Tuexen 		/* Calculate the RTT. */
22398c8e10b7SMichael Tuexen 		old.tv_sec = cookie->time_entered.tv_sec;
22408c8e10b7SMichael Tuexen 		old.tv_usec = cookie->time_entered.tv_usec;
224144f2a327SMichael Tuexen 		sctp_calculate_rto(stcb, asoc, *netp, &old, SCTP_RTT_FROM_NON_DATA);
22429a972525SRandall Stewart 	}
22433232788eSRandall Stewart 	/* respond with a COOKIE-ACK */
2244f8829a4aSRandall Stewart 	sctp_send_cookie_ack(stcb);
22453232788eSRandall Stewart 
22463232788eSRandall Stewart 	/*
22473232788eSRandall Stewart 	 * check the address lists for any ASCONFs that need to be sent
22483232788eSRandall Stewart 	 * AFTER the cookie-ack is sent
22493232788eSRandall Stewart 	 */
22503232788eSRandall Stewart 	sctp_check_address_list(stcb, m,
22513232788eSRandall Stewart 	    initack_offset + sizeof(struct sctp_init_ack_chunk),
22523232788eSRandall Stewart 	    initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
225324aaac8dSMichael Tuexen 	    &store.sa, cookie->local_scope, cookie->site_scope,
22543232788eSRandall Stewart 	    cookie->ipv4_scope, cookie->loopback_scope);
22553232788eSRandall Stewart 
2256f8829a4aSRandall Stewart 	return (stcb);
2257f8829a4aSRandall Stewart }
2258f8829a4aSRandall Stewart 
2259830d754dSRandall Stewart /*
2260830d754dSRandall Stewart  * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2261830d754dSRandall Stewart  * we NEED to make sure we are not already using the vtag. If so we
2262830d754dSRandall Stewart  * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2263830d754dSRandall Stewart 	head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2264830d754dSRandall Stewart 							    SCTP_BASE_INFO(hashasocmark))];
2265830d754dSRandall Stewart 	LIST_FOREACH(stcb, head, sctp_asocs) {
2266830d754dSRandall Stewart 	        if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep))  {
2267830d754dSRandall Stewart 		       -- SEND ABORT - TRY AGAIN --
2268830d754dSRandall Stewart 		}
2269830d754dSRandall Stewart 	}
2270830d754dSRandall Stewart */
2271f8829a4aSRandall Stewart 
2272f8829a4aSRandall Stewart /*
2273f8829a4aSRandall Stewart  * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2274f8829a4aSRandall Stewart  * existing (non-NULL) TCB
2275f8829a4aSRandall Stewart  */
2276f8829a4aSRandall Stewart static struct mbuf *
2277f8829a4aSRandall Stewart sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2278b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
2279f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2280f8829a4aSRandall Stewart     struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
228117205eccSRandall Stewart     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2282f30ac432SMichael Tuexen     struct sctp_tcb **locked_tcb,
2283457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
2284f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
2285f8829a4aSRandall Stewart {
2286f8829a4aSRandall Stewart 	struct sctp_state_cookie *cookie;
2287f8829a4aSRandall Stewart 	struct sctp_tcb *l_stcb = *stcb;
2288f8829a4aSRandall Stewart 	struct sctp_inpcb *l_inp;
2289f8829a4aSRandall Stewart 	struct sockaddr *to;
2290f8829a4aSRandall Stewart 	struct sctp_pcb *ep;
2291f8829a4aSRandall Stewart 	struct mbuf *m_sig;
2292f8829a4aSRandall Stewart 	uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2293f8829a4aSRandall Stewart 	uint8_t *sig;
2294f8829a4aSRandall Stewart 	uint8_t cookie_ok = 0;
2295329204ffSMichael Tuexen 	unsigned int sig_offset, cookie_offset;
2296f8829a4aSRandall Stewart 	unsigned int cookie_len;
2297f8829a4aSRandall Stewart 	struct timeval now;
2298a92d5016SMichael Tuexen 	struct timeval time_entered, time_expires;
2299f8829a4aSRandall Stewart 	int notification = 0;
2300f8829a4aSRandall Stewart 	struct sctp_nets *netl;
2301f8829a4aSRandall Stewart 	int had_a_existing_tcb = 0;
23022fad0e55SMichael Tuexen 	int send_int_conf = 0;
2303e6194c2eSMichael Tuexen #ifdef INET
2304e6194c2eSMichael Tuexen 	struct sockaddr_in sin;
2305e6194c2eSMichael Tuexen #endif
2306e6194c2eSMichael Tuexen #ifdef INET6
2307e6194c2eSMichael Tuexen 	struct sockaddr_in6 sin6;
2308e6194c2eSMichael Tuexen #endif
2309e6194c2eSMichael Tuexen 
2310ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
2311ad81507eSRandall Stewart 	    "sctp_handle_cookie: handling COOKIE-ECHO\n");
2312f8829a4aSRandall Stewart 
2313f8829a4aSRandall Stewart 	if (inp_p == NULL) {
2314f8829a4aSRandall Stewart 		return (NULL);
2315f8829a4aSRandall Stewart 	}
2316f8829a4aSRandall Stewart 	cookie = &cp->cookie;
2317f8829a4aSRandall Stewart 	cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2318f8829a4aSRandall Stewart 	cookie_len = ntohs(cp->ch.chunk_length);
2319f8829a4aSRandall Stewart 
2320d44b45dfSMichael Tuexen 	if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2321d44b45dfSMichael Tuexen 	    sizeof(struct sctp_init_chunk) +
2322d44b45dfSMichael Tuexen 	    sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2323d44b45dfSMichael Tuexen 		/* cookie too small */
2324d44b45dfSMichael Tuexen 		return (NULL);
2325d44b45dfSMichael Tuexen 	}
23263db4ea95SMichael Tuexen 	if ((cookie->peerport != sh->src_port) ||
23273db4ea95SMichael Tuexen 	    (cookie->myport != sh->dest_port) ||
2328f8829a4aSRandall Stewart 	    (cookie->my_vtag != sh->v_tag)) {
2329f8829a4aSRandall Stewart 		/*
2330f8829a4aSRandall Stewart 		 * invalid ports or bad tag.  Note that we always leave the
2331f8829a4aSRandall Stewart 		 * v_tag in the header in network order and when we stored
2332f8829a4aSRandall Stewart 		 * it in the my_vtag slot we also left it in network order.
233317205eccSRandall Stewart 		 * This maintains the match even though it may be in the
2334f8829a4aSRandall Stewart 		 * opposite byte order of the machine :->
2335f8829a4aSRandall Stewart 		 */
2336f8829a4aSRandall Stewart 		return (NULL);
2337f8829a4aSRandall Stewart 	}
2338f8829a4aSRandall Stewart 	/*
2339f8829a4aSRandall Stewart 	 * split off the signature into its own mbuf (since it should not be
2340f8829a4aSRandall Stewart 	 * calculated in the sctp_hmac_m() call).
2341f8829a4aSRandall Stewart 	 */
2342f8829a4aSRandall Stewart 	sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2343eb1b1807SGleb Smirnoff 	m_sig = m_split(m, sig_offset, M_NOWAIT);
2344f8829a4aSRandall Stewart 	if (m_sig == NULL) {
2345f8829a4aSRandall Stewart 		/* out of memory or ?? */
2346f8829a4aSRandall Stewart 		return (NULL);
2347f8829a4aSRandall Stewart 	}
2348eadccaccSRandall Stewart #ifdef SCTP_MBUF_LOGGING
2349b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
23504be807c4SMichael Tuexen 		sctp_log_mbc(m_sig, SCTP_MBUF_SPLIT);
2351eadccaccSRandall Stewart 	}
2352eadccaccSRandall Stewart #endif
2353eadccaccSRandall Stewart 
2354f8829a4aSRandall Stewart 	/*
2355f8829a4aSRandall Stewart 	 * compute the signature/digest for the cookie
2356f8829a4aSRandall Stewart 	 */
2357f8829a4aSRandall Stewart 	ep = &(*inp_p)->sctp_ep;
2358f8829a4aSRandall Stewart 	l_inp = *inp_p;
2359f8829a4aSRandall Stewart 	if (l_stcb) {
2360f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(l_stcb);
2361f8829a4aSRandall Stewart 	}
2362f8829a4aSRandall Stewart 	SCTP_INP_RLOCK(l_inp);
2363f8829a4aSRandall Stewart 	if (l_stcb) {
2364f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(l_stcb);
2365f8829a4aSRandall Stewart 	}
2366f8829a4aSRandall Stewart 	/* which cookie is it? */
2367f8829a4aSRandall Stewart 	if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2368f8829a4aSRandall Stewart 	    (ep->current_secret_number != ep->last_secret_number)) {
2369f8829a4aSRandall Stewart 		/* it's the old cookie */
23706e55db54SRandall Stewart 		(void)sctp_hmac_m(SCTP_HMAC,
2371f8829a4aSRandall Stewart 		    (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2372d00aff5dSRandall Stewart 		    SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2373f8829a4aSRandall Stewart 	} else {
2374f8829a4aSRandall Stewart 		/* it's the current cookie */
23756e55db54SRandall Stewart 		(void)sctp_hmac_m(SCTP_HMAC,
2376f8829a4aSRandall Stewart 		    (uint8_t *)ep->secret_key[(int)ep->current_secret_number],
2377d00aff5dSRandall Stewart 		    SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2378f8829a4aSRandall Stewart 	}
2379f8829a4aSRandall Stewart 	/* get the signature */
2380f8829a4aSRandall Stewart 	SCTP_INP_RUNLOCK(l_inp);
2381f8829a4aSRandall Stewart 	sig = (uint8_t *)sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *)&tmp_sig);
2382f8829a4aSRandall Stewart 	if (sig == NULL) {
2383f8829a4aSRandall Stewart 		/* couldn't find signature */
238403b0b021SRandall Stewart 		sctp_m_freem(m_sig);
2385f8829a4aSRandall Stewart 		return (NULL);
2386f8829a4aSRandall Stewart 	}
2387f8829a4aSRandall Stewart 	/* compare the received digest with the computed digest */
238815a087e5SMichael Tuexen 	if (timingsafe_bcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2389f8829a4aSRandall Stewart 		/* try the old cookie? */
2390f8829a4aSRandall Stewart 		if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2391f8829a4aSRandall Stewart 		    (ep->current_secret_number != ep->last_secret_number)) {
2392f8829a4aSRandall Stewart 			/* compute digest with old */
23936e55db54SRandall Stewart 			(void)sctp_hmac_m(SCTP_HMAC,
2394f8829a4aSRandall Stewart 			    (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2395d00aff5dSRandall Stewart 			    SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2396f8829a4aSRandall Stewart 			/* compare */
239715a087e5SMichael Tuexen 			if (timingsafe_bcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2398f8829a4aSRandall Stewart 				cookie_ok = 1;
2399f8829a4aSRandall Stewart 		}
2400f8829a4aSRandall Stewart 	} else {
2401f8829a4aSRandall Stewart 		cookie_ok = 1;
2402f8829a4aSRandall Stewart 	}
2403f8829a4aSRandall Stewart 
2404f8829a4aSRandall Stewart 	/*
2405f8829a4aSRandall Stewart 	 * Now before we continue we must reconstruct our mbuf so that
2406f8829a4aSRandall Stewart 	 * normal processing of any other chunks will work.
2407f8829a4aSRandall Stewart 	 */
2408f8829a4aSRandall Stewart 	{
2409f8829a4aSRandall Stewart 		struct mbuf *m_at;
2410f8829a4aSRandall Stewart 
2411f8829a4aSRandall Stewart 		m_at = m;
2412139bc87fSRandall Stewart 		while (SCTP_BUF_NEXT(m_at) != NULL) {
2413139bc87fSRandall Stewart 			m_at = SCTP_BUF_NEXT(m_at);
2414f8829a4aSRandall Stewart 		}
2415139bc87fSRandall Stewart 		SCTP_BUF_NEXT(m_at) = m_sig;
2416f8829a4aSRandall Stewart 	}
2417f8829a4aSRandall Stewart 
2418f8829a4aSRandall Stewart 	if (cookie_ok == 0) {
2419ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2420ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
2421ad81507eSRandall Stewart 		    "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2422f8829a4aSRandall Stewart 		    (uint32_t)offset, cookie_offset, sig_offset);
2423f8829a4aSRandall Stewart 		return (NULL);
2424f8829a4aSRandall Stewart 	}
24250053ed28SMichael Tuexen 
2426a92d5016SMichael Tuexen 	if (sctp_ticks_to_msecs(cookie->cookie_life) > SCTP_MAX_COOKIE_LIFE) {
2427a92d5016SMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: Invalid cookie lifetime\n");
2428a92d5016SMichael Tuexen 		return (NULL);
2429a92d5016SMichael Tuexen 	}
2430a92d5016SMichael Tuexen 	time_entered.tv_sec = cookie->time_entered.tv_sec;
2431a92d5016SMichael Tuexen 	time_entered.tv_usec = cookie->time_entered.tv_usec;
2432a92d5016SMichael Tuexen 	if ((time_entered.tv_sec < 0) ||
2433a92d5016SMichael Tuexen 	    (time_entered.tv_usec < 0) ||
2434a92d5016SMichael Tuexen 	    (time_entered.tv_usec >= 1000000)) {
2435a92d5016SMichael Tuexen 		/* Invalid time stamp. Cookie must have been modified. */
2436a92d5016SMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: Invalid time stamp\n");
2437a92d5016SMichael Tuexen 		return (NULL);
2438a92d5016SMichael Tuexen 	}
24396e55db54SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&now);
2440a92d5016SMichael Tuexen 	if (timevalcmp(&now, &time_entered, <)) {
2441a92d5016SMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie generated in the future!\n");
2442a92d5016SMichael Tuexen 		return (NULL);
2443a92d5016SMichael Tuexen 	}
2444a92d5016SMichael Tuexen 	/*
2445a92d5016SMichael Tuexen 	 * Check the cookie timestamps to be sure it's not stale.
2446a92d5016SMichael Tuexen 	 * cookie_life is in ticks, so we convert to seconds.
2447a92d5016SMichael Tuexen 	 */
2448a92d5016SMichael Tuexen 	time_expires.tv_sec = time_entered.tv_sec + sctp_ticks_to_secs(cookie->cookie_life);
2449a92d5016SMichael Tuexen 	time_expires.tv_usec = time_entered.tv_usec;
2450f8829a4aSRandall Stewart 	if (timevalcmp(&now, &time_expires, >)) {
2451f8829a4aSRandall Stewart 		/* cookie is stale! */
2452f8829a4aSRandall Stewart 		struct mbuf *op_err;
245386eda749SMichael Tuexen 		struct sctp_error_stale_cookie *cause;
2454564a95f4SMichael Tuexen 		struct timeval diff;
2455564a95f4SMichael Tuexen 		uint32_t staleness;
2456f8829a4aSRandall Stewart 
245786eda749SMichael Tuexen 		op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_stale_cookie),
2458eb1b1807SGleb Smirnoff 		    0, M_NOWAIT, 1, MT_DATA);
2459f8829a4aSRandall Stewart 		if (op_err == NULL) {
2460f8829a4aSRandall Stewart 			/* FOOBAR */
2461f8829a4aSRandall Stewart 			return (NULL);
2462f8829a4aSRandall Stewart 		}
2463f8829a4aSRandall Stewart 		/* Set the len */
246486eda749SMichael Tuexen 		SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_stale_cookie);
246586eda749SMichael Tuexen 		cause = mtod(op_err, struct sctp_error_stale_cookie *);
246686eda749SMichael Tuexen 		cause->cause.code = htons(SCTP_CAUSE_STALE_COOKIE);
2467a92d5016SMichael Tuexen 		cause->cause.length = htons(sizeof(struct sctp_error_stale_cookie));
2468564a95f4SMichael Tuexen 		diff = now;
2469564a95f4SMichael Tuexen 		timevalsub(&diff, &time_expires);
2470c3115febSMichael Tuexen 		if ((uint32_t)diff.tv_sec > UINT32_MAX / 1000000) {
2471564a95f4SMichael Tuexen 			staleness = UINT32_MAX;
2472564a95f4SMichael Tuexen 		} else {
2473564a95f4SMichael Tuexen 			staleness = diff.tv_sec * 1000000;
2474564a95f4SMichael Tuexen 		}
24753ec509bcSMichael Tuexen 		if (UINT32_MAX - staleness >= (uint32_t)diff.tv_usec) {
2476564a95f4SMichael Tuexen 			staleness += diff.tv_usec;
2477564a95f4SMichael Tuexen 		} else {
2478564a95f4SMichael Tuexen 			staleness = UINT32_MAX;
2479564a95f4SMichael Tuexen 		}
2480564a95f4SMichael Tuexen 		cause->stale_time = htonl(staleness);
2481b1754ad1SMichael Tuexen 		sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
2482d089f9b9SMichael Tuexen 		    mflowtype, mflowid, l_inp->fibnum,
2483c54a18d2SRandall Stewart 		    vrf_id, port);
2484f8829a4aSRandall Stewart 		return (NULL);
2485f8829a4aSRandall Stewart 	}
2486f8829a4aSRandall Stewart 	/*
2487f8829a4aSRandall Stewart 	 * Now we must see with the lookup address if we have an existing
2488f8829a4aSRandall Stewart 	 * asoc. This will only happen if we were in the COOKIE-WAIT state
2489f8829a4aSRandall Stewart 	 * and a INIT collided with us and somewhere the peer sent the
2490f8829a4aSRandall Stewart 	 * cookie on another address besides the single address our assoc
2491f8829a4aSRandall Stewart 	 * had for him. In this case we will have one of the tie-tags set at
2492f8829a4aSRandall Stewart 	 * least AND the address field in the cookie can be used to look it
2493f8829a4aSRandall Stewart 	 * up.
2494f8829a4aSRandall Stewart 	 */
2495f8829a4aSRandall Stewart 	to = NULL;
2496e6194c2eSMichael Tuexen 	switch (cookie->addr_type) {
2497e6194c2eSMichael Tuexen #ifdef INET6
2498e6194c2eSMichael Tuexen 	case SCTP_IPV6_ADDRESS:
2499f8829a4aSRandall Stewart 		memset(&sin6, 0, sizeof(sin6));
2500f8829a4aSRandall Stewart 		sin6.sin6_family = AF_INET6;
2501f8829a4aSRandall Stewart 		sin6.sin6_len = sizeof(sin6);
2502f8829a4aSRandall Stewart 		sin6.sin6_port = sh->src_port;
2503f8829a4aSRandall Stewart 		sin6.sin6_scope_id = cookie->scope_id;
2504f8829a4aSRandall Stewart 		memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2505f8829a4aSRandall Stewart 		    sizeof(sin6.sin6_addr.s6_addr));
2506f8829a4aSRandall Stewart 		to = (struct sockaddr *)&sin6;
2507e6194c2eSMichael Tuexen 		break;
2508e6194c2eSMichael Tuexen #endif
2509e6194c2eSMichael Tuexen #ifdef INET
2510e6194c2eSMichael Tuexen 	case SCTP_IPV4_ADDRESS:
2511f8829a4aSRandall Stewart 		memset(&sin, 0, sizeof(sin));
2512f8829a4aSRandall Stewart 		sin.sin_family = AF_INET;
2513f8829a4aSRandall Stewart 		sin.sin_len = sizeof(sin);
2514f8829a4aSRandall Stewart 		sin.sin_port = sh->src_port;
2515f8829a4aSRandall Stewart 		sin.sin_addr.s_addr = cookie->address[0];
2516f8829a4aSRandall Stewart 		to = (struct sockaddr *)&sin;
2517e6194c2eSMichael Tuexen 		break;
2518e6194c2eSMichael Tuexen #endif
2519e6194c2eSMichael Tuexen 	default:
2520ad81507eSRandall Stewart 		/* This should not happen */
2521ad81507eSRandall Stewart 		return (NULL);
2522f8829a4aSRandall Stewart 	}
2523f0dc2113SMichael Tuexen 	if (*stcb == NULL) {
2524f8829a4aSRandall Stewart 		/* Yep, lets check */
2525b1754ad1SMichael Tuexen 		*stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
2526f8829a4aSRandall Stewart 		if (*stcb == NULL) {
2527f8829a4aSRandall Stewart 			/*
2528f8829a4aSRandall Stewart 			 * We should have only got back the same inp. If we
2529f8829a4aSRandall Stewart 			 * got back a different ep we have a problem. The
2530f8829a4aSRandall Stewart 			 * original findep got back l_inp and now
2531f8829a4aSRandall Stewart 			 */
2532f8829a4aSRandall Stewart 			if (l_inp != *inp_p) {
2533ad81507eSRandall Stewart 				SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2534f8829a4aSRandall Stewart 			}
2535a5d547adSRandall Stewart 		} else {
2536a5d547adSRandall Stewart 			if (*locked_tcb == NULL) {
2537a5d547adSRandall Stewart 				/*
2538a5d547adSRandall Stewart 				 * In this case we found the assoc only
2539a5d547adSRandall Stewart 				 * after we locked the create lock. This
2540a5d547adSRandall Stewart 				 * means we are in a colliding case and we
2541a5d547adSRandall Stewart 				 * must make sure that we unlock the tcb if
2542a5d547adSRandall Stewart 				 * its one of the cases where we throw away
2543a5d547adSRandall Stewart 				 * the incoming packets.
2544a5d547adSRandall Stewart 				 */
2545a5d547adSRandall Stewart 				*locked_tcb = *stcb;
2546a5d547adSRandall Stewart 
2547a5d547adSRandall Stewart 				/*
2548a5d547adSRandall Stewart 				 * We must also increment the inp ref count
2549a5d547adSRandall Stewart 				 * since the ref_count flags was set when we
2550a5d547adSRandall Stewart 				 * did not find the TCB, now we found it
2551a5d547adSRandall Stewart 				 * which reduces the refcount.. we must
2552a5d547adSRandall Stewart 				 * raise it back out to balance it all :-)
2553a5d547adSRandall Stewart 				 */
2554a5d547adSRandall Stewart 				SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2555a5d547adSRandall Stewart 				if ((*stcb)->sctp_ep != l_inp) {
2556ad81507eSRandall Stewart 					SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2557dd294dceSMichael Tuexen 					    (void *)(*stcb)->sctp_ep, (void *)l_inp);
2558a5d547adSRandall Stewart 				}
2559a5d547adSRandall Stewart 			}
2560f8829a4aSRandall Stewart 		}
2561f8829a4aSRandall Stewart 	}
25620053ed28SMichael Tuexen 
2563f8829a4aSRandall Stewart 	cookie_len -= SCTP_SIGNATURE_SIZE;
2564f8829a4aSRandall Stewart 	if (*stcb == NULL) {
2565f8829a4aSRandall Stewart 		/* this is the "normal" case... get a new TCB */
2566b1754ad1SMichael Tuexen 		*stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
2567b1754ad1SMichael Tuexen 		    cookie, cookie_len, *inp_p,
2568b1754ad1SMichael Tuexen 		    netp, to, &notification,
2569f30ac432SMichael Tuexen 		    auth_skipped, auth_offset, auth_len,
2570457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2571f30ac432SMichael Tuexen 		    vrf_id, port);
2572f8829a4aSRandall Stewart 	} else {
2573f8829a4aSRandall Stewart 		/* this is abnormal... cookie-echo on existing TCB */
2574f8829a4aSRandall Stewart 		had_a_existing_tcb = 1;
2575b1754ad1SMichael Tuexen 		*stcb = sctp_process_cookie_existing(m, iphlen, offset,
2576b1754ad1SMichael Tuexen 		    src, dst, sh,
2577830d754dSRandall Stewart 		    cookie, cookie_len, *inp_p, *stcb, netp, to,
2578f30ac432SMichael Tuexen 		    &notification, auth_skipped, auth_offset, auth_len,
2579457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2580f30ac432SMichael Tuexen 		    vrf_id, port);
25815f2e1835SMichael Tuexen 		if (*stcb == NULL) {
25825f2e1835SMichael Tuexen 			*locked_tcb = NULL;
25835f2e1835SMichael Tuexen 		}
2584f8829a4aSRandall Stewart 	}
2585f8829a4aSRandall Stewart 
2586f8829a4aSRandall Stewart 	if (*stcb == NULL) {
2587f8829a4aSRandall Stewart 		/* still no TCB... must be bad cookie-echo */
2588f8829a4aSRandall Stewart 		return (NULL);
2589f8829a4aSRandall Stewart 	}
25905fe29cdfSMichael Tuexen 	if (*netp != NULL) {
2591457b4b88SMichael Tuexen 		(*netp)->flowtype = mflowtype;
25925fe29cdfSMichael Tuexen 		(*netp)->flowid = mflowid;
2593a4ae38f1SMichael Tuexen 	}
2594f8829a4aSRandall Stewart 	/*
2595f8829a4aSRandall Stewart 	 * Ok, we built an association so confirm the address we sent the
2596f8829a4aSRandall Stewart 	 * INIT-ACK to.
2597f8829a4aSRandall Stewart 	 */
2598f8829a4aSRandall Stewart 	netl = sctp_findnet(*stcb, to);
2599f8829a4aSRandall Stewart 	/*
2600f8829a4aSRandall Stewart 	 * This code should in theory NOT run but
2601f8829a4aSRandall Stewart 	 */
2602f8829a4aSRandall Stewart 	if (netl == NULL) {
2603f8829a4aSRandall Stewart 		/* TSNH! Huh, why do I need to add this address here? */
2604ec70917fSMichael Tuexen 		if (sctp_add_remote_addr(*stcb, to, NULL, port,
26057154bf4aSMichael Tuexen 		    SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
260660990c0cSMichael Tuexen 			return (NULL);
260760990c0cSMichael Tuexen 		}
2608f8829a4aSRandall Stewart 		netl = sctp_findnet(*stcb, to);
2609f8829a4aSRandall Stewart 	}
2610f8829a4aSRandall Stewart 	if (netl) {
2611f8829a4aSRandall Stewart 		if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2612f8829a4aSRandall Stewart 			netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2613ad81507eSRandall Stewart 			(void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2614f8829a4aSRandall Stewart 			    netl);
26152fad0e55SMichael Tuexen 			send_int_conf = 1;
2616f8829a4aSRandall Stewart 		}
2617f8829a4aSRandall Stewart 	}
2618ca85e948SMichael Tuexen 	sctp_start_net_timers(*stcb);
2619f8829a4aSRandall Stewart 	if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2620f8829a4aSRandall Stewart 		if (!had_a_existing_tcb ||
2621f8829a4aSRandall Stewart 		    (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2622f8829a4aSRandall Stewart 			/*
2623f8829a4aSRandall Stewart 			 * If we have a NEW cookie or the connect never
2624f8829a4aSRandall Stewart 			 * reached the connected state during collision we
2625f8829a4aSRandall Stewart 			 * must do the TCP accept thing.
2626f8829a4aSRandall Stewart 			 */
2627f8829a4aSRandall Stewart 			struct socket *so, *oso;
2628f8829a4aSRandall Stewart 			struct sctp_inpcb *inp;
2629f8829a4aSRandall Stewart 
2630f8829a4aSRandall Stewart 			if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2631f8829a4aSRandall Stewart 				/*
2632f8829a4aSRandall Stewart 				 * For a restart we will keep the same
2633f8829a4aSRandall Stewart 				 * socket, no need to do anything. I THINK!!
2634f8829a4aSRandall Stewart 				 */
26357215cc1bSMichael Tuexen 				sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
26362fad0e55SMichael Tuexen 				if (send_int_conf) {
26372fad0e55SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
26382fad0e55SMichael Tuexen 					    (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
26392fad0e55SMichael Tuexen 				}
2640f8829a4aSRandall Stewart 				return (m);
2641f8829a4aSRandall Stewart 			}
2642f8829a4aSRandall Stewart 			oso = (*inp_p)->sctp_socket;
26432dad8a55SRandall Stewart 			atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2644f8829a4aSRandall Stewart 			SCTP_TCB_UNLOCK((*stcb));
26451fb51a12SBjoern A. Zeeb 			CURVNET_SET(oso->so_vnet);
264693164cf9SRandall Stewart 			so = sonewconn(oso, 0
2647f8829a4aSRandall Stewart 			    );
26481fb51a12SBjoern A. Zeeb 			CURVNET_RESTORE();
2649f8829a4aSRandall Stewart 			SCTP_TCB_LOCK((*stcb));
26502dad8a55SRandall Stewart 			atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
26512dad8a55SRandall Stewart 
2652f8829a4aSRandall Stewart 			if (so == NULL) {
2653f8829a4aSRandall Stewart 				struct mbuf *op_err;
265470486b27SMichael Tuexen 
2655f8829a4aSRandall Stewart 				/* Too many sockets */
2656ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2657ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2658f8829a4aSRandall Stewart 				sctp_abort_association(*inp_p, NULL, m, iphlen,
2659b1754ad1SMichael Tuexen 				    src, dst, sh, op_err,
2660457b4b88SMichael Tuexen 				    mflowtype, mflowid,
2661f30ac432SMichael Tuexen 				    vrf_id, port);
2662b7d130beSMichael Tuexen 				(void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC,
2663b7d130beSMichael Tuexen 				    SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
2664f8829a4aSRandall Stewart 				return (NULL);
2665f8829a4aSRandall Stewart 			}
2666f8829a4aSRandall Stewart 			inp = (struct sctp_inpcb *)so->so_pcb;
266793164cf9SRandall Stewart 			SCTP_INP_INCR_REF(inp);
266893164cf9SRandall Stewart 			/*
266993164cf9SRandall Stewart 			 * We add the unbound flag here so that if we get an
267093164cf9SRandall Stewart 			 * soabort() before we get the move_pcb done, we
267193164cf9SRandall Stewart 			 * will properly cleanup.
267293164cf9SRandall Stewart 			 */
2673f8829a4aSRandall Stewart 			inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2674f8829a4aSRandall Stewart 			    SCTP_PCB_FLAGS_CONNECTED |
2675f8829a4aSRandall Stewart 			    SCTP_PCB_FLAGS_IN_TCPPOOL |
267693164cf9SRandall Stewart 			    SCTP_PCB_FLAGS_UNBOUND |
2677f8829a4aSRandall Stewart 			    (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2678f8829a4aSRandall Stewart 			    SCTP_PCB_FLAGS_DONT_WAKE);
2679f8829a4aSRandall Stewart 			inp->sctp_features = (*inp_p)->sctp_features;
2680851b7298SRandall Stewart 			inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2681f8829a4aSRandall Stewart 			inp->sctp_socket = so;
2682f8829a4aSRandall Stewart 			inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
268359b6d5beSMichael Tuexen 			inp->max_cwnd = (*inp_p)->max_cwnd;
268420083c2eSMichael Tuexen 			inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2685f342355aSMichael Tuexen 			inp->ecn_supported = (*inp_p)->ecn_supported;
2686dd973b0eSMichael Tuexen 			inp->prsctp_supported = (*inp_p)->prsctp_supported;
2687c79bec9cSMichael Tuexen 			inp->auth_supported = (*inp_p)->auth_supported;
2688c79bec9cSMichael Tuexen 			inp->asconf_supported = (*inp_p)->asconf_supported;
2689317e00efSMichael Tuexen 			inp->reconfig_supported = (*inp_p)->reconfig_supported;
2690caea9879SMichael Tuexen 			inp->nrsack_supported = (*inp_p)->nrsack_supported;
2691cb9b8e6fSMichael Tuexen 			inp->pktdrop_supported = (*inp_p)->pktdrop_supported;
2692f8829a4aSRandall Stewart 			inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2693f8829a4aSRandall Stewart 			inp->sctp_context = (*inp_p)->sctp_context;
2694c4e848b7SRandall Stewart 			inp->local_strreset_support = (*inp_p)->local_strreset_support;
2695d089f9b9SMichael Tuexen 			inp->fibnum = (*inp_p)->fibnum;
2696f8829a4aSRandall Stewart 			inp->inp_starting_point_for_iterator = NULL;
2697f8829a4aSRandall Stewart 			/*
2698f8829a4aSRandall Stewart 			 * copy in the authentication parameters from the
2699f8829a4aSRandall Stewart 			 * original endpoint
2700f8829a4aSRandall Stewart 			 */
2701f8829a4aSRandall Stewart 			if (inp->sctp_ep.local_hmacs)
2702f8829a4aSRandall Stewart 				sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2703f8829a4aSRandall Stewart 			inp->sctp_ep.local_hmacs =
2704f8829a4aSRandall Stewart 			    sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2705f8829a4aSRandall Stewart 			if (inp->sctp_ep.local_auth_chunks)
2706f8829a4aSRandall Stewart 				sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2707f8829a4aSRandall Stewart 			inp->sctp_ep.local_auth_chunks =
2708f8829a4aSRandall Stewart 			    sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2709f8829a4aSRandall Stewart 
2710f8829a4aSRandall Stewart 			/*
2711f8829a4aSRandall Stewart 			 * Now we must move it from one hash table to
2712f8829a4aSRandall Stewart 			 * another and get the tcb in the right place.
2713f8829a4aSRandall Stewart 			 */
271488a7eb29SRandall Stewart 
271588a7eb29SRandall Stewart 			/*
271688a7eb29SRandall Stewart 			 * This is where the one-2-one socket is put into
271788a7eb29SRandall Stewart 			 * the accept state waiting for the accept!
271888a7eb29SRandall Stewart 			 */
271988a7eb29SRandall Stewart 			if (*stcb) {
2720839d21d6SMichael Tuexen 				SCTP_ADD_SUBSTATE(*stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
272188a7eb29SRandall Stewart 			}
2722f8829a4aSRandall Stewart 			sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2723d61a0ae0SRandall Stewart 
2724d61a0ae0SRandall Stewart 			atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2725d61a0ae0SRandall Stewart 			SCTP_TCB_UNLOCK((*stcb));
2726d61a0ae0SRandall Stewart 
2727265de5bbSRobert Watson 			sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2728265de5bbSRobert Watson 			    0);
2729d61a0ae0SRandall Stewart 			SCTP_TCB_LOCK((*stcb));
2730d61a0ae0SRandall Stewart 			atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2731d61a0ae0SRandall Stewart 
273293164cf9SRandall Stewart 			/*
273393164cf9SRandall Stewart 			 * now we must check to see if we were aborted while
273493164cf9SRandall Stewart 			 * the move was going on and the lock/unlock
273593164cf9SRandall Stewart 			 * happened.
273693164cf9SRandall Stewart 			 */
273793164cf9SRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
273893164cf9SRandall Stewart 				/*
273993164cf9SRandall Stewart 				 * yep it was, we leave the assoc attached
274093164cf9SRandall Stewart 				 * to the socket since the sctp_inpcb_free()
274193164cf9SRandall Stewart 				 * call will send an abort for us.
274293164cf9SRandall Stewart 				 */
274393164cf9SRandall Stewart 				SCTP_INP_DECR_REF(inp);
274493164cf9SRandall Stewart 				return (NULL);
274593164cf9SRandall Stewart 			}
274693164cf9SRandall Stewart 			SCTP_INP_DECR_REF(inp);
2747f8829a4aSRandall Stewart 			/* Switch over to the new guy */
2748f8829a4aSRandall Stewart 			*inp_p = inp;
2749ceaad40aSRandall Stewart 			sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
27502fad0e55SMichael Tuexen 			if (send_int_conf) {
27512fad0e55SMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
27522fad0e55SMichael Tuexen 				    (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
27532fad0e55SMichael Tuexen 			}
27540053ed28SMichael Tuexen 
2755b7b84c0eSMichael Tuexen 			/*
2756b7b84c0eSMichael Tuexen 			 * Pull it from the incomplete queue and wake the
2757b7b84c0eSMichael Tuexen 			 * guy
2758b7b84c0eSMichael Tuexen 			 */
275993164cf9SRandall Stewart 			soisconnected(so);
2760f8829a4aSRandall Stewart 			return (m);
2761f8829a4aSRandall Stewart 		}
2762f8829a4aSRandall Stewart 	}
27632fad0e55SMichael Tuexen 	if (notification) {
2764ceaad40aSRandall Stewart 		sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2765f8829a4aSRandall Stewart 	}
27662fad0e55SMichael Tuexen 	if (send_int_conf) {
27672fad0e55SMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
27682fad0e55SMichael Tuexen 		    (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
27692fad0e55SMichael Tuexen 	}
2770f8829a4aSRandall Stewart 	return (m);
2771f8829a4aSRandall Stewart }
2772f8829a4aSRandall Stewart 
2773f8829a4aSRandall Stewart static void
27747215cc1bSMichael Tuexen sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2775f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net)
2776f8829a4aSRandall Stewart {
2777f8829a4aSRandall Stewart 	/* cp must not be used, others call this without a c-ack :-) */
2778f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2779efd5e692SMichael Tuexen 	struct sctp_tmit_chunk *chk;
2780f8829a4aSRandall Stewart 
2781ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
2782ad81507eSRandall Stewart 	    "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2783ad234e3cSMichael Tuexen 	if ((stcb == NULL) || (net == NULL)) {
2784f8829a4aSRandall Stewart 		return;
2785ad234e3cSMichael Tuexen 	}
27860053ed28SMichael Tuexen 
2787f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2788469a65d1SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
2789469a65d1SMichael Tuexen 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2790469a65d1SMichael Tuexen 		    asoc->overall_error_count,
2791469a65d1SMichael Tuexen 		    0,
2792469a65d1SMichael Tuexen 		    SCTP_FROM_SCTP_INPUT,
2793469a65d1SMichael Tuexen 		    __LINE__);
2794469a65d1SMichael Tuexen 	}
2795469a65d1SMichael Tuexen 	asoc->overall_error_count = 0;
2796f8829a4aSRandall Stewart 	sctp_stop_all_cookie_timers(stcb);
2797f8829a4aSRandall Stewart 	/* process according to association state */
2798839d21d6SMichael Tuexen 	if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) {
2799f8829a4aSRandall Stewart 		/* state change only needed when I am in right state */
2800ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2801839d21d6SMichael Tuexen 		SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
2802ca85e948SMichael Tuexen 		sctp_start_net_timers(stcb);
2803f8829a4aSRandall Stewart 		if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2804f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
28056fb7b4fbSMichael Tuexen 			    stcb->sctp_ep, stcb, NULL);
2806f8829a4aSRandall Stewart 		}
2807f8829a4aSRandall Stewart 		/* update RTO */
2808f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2809f8829a4aSRandall Stewart 		SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2810f8829a4aSRandall Stewart 		if (asoc->overall_error_count == 0) {
281144f2a327SMichael Tuexen 			sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered,
2812f79aab18SRandall Stewart 			    SCTP_RTT_FROM_NON_DATA);
2813f8829a4aSRandall Stewart 		}
28146e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2815ceaad40aSRandall Stewart 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2816f8829a4aSRandall Stewart 		if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2817f8829a4aSRandall Stewart 		    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2818f8829a4aSRandall Stewart 			stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2819d69e7322SRandall Stewart 			if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
2820ceaad40aSRandall Stewart 				soisconnected(stcb->sctp_socket);
2821d69e7322SRandall Stewart 			}
2822f8829a4aSRandall Stewart 		}
2823f8829a4aSRandall Stewart 		/*
2824f8829a4aSRandall Stewart 		 * since we did not send a HB make sure we don't double
2825f8829a4aSRandall Stewart 		 * things
2826f8829a4aSRandall Stewart 		 */
2827f8829a4aSRandall Stewart 		net->hb_responded = 1;
2828f8829a4aSRandall Stewart 
2829d69e7322SRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2830d69e7322SRandall Stewart 			/*
2831d69e7322SRandall Stewart 			 * We don't need to do the asconf thing, nor hb or
2832d69e7322SRandall Stewart 			 * autoclose if the socket is closed.
2833d69e7322SRandall Stewart 			 */
2834d69e7322SRandall Stewart 			goto closed_socket;
2835d69e7322SRandall Stewart 		}
28360053ed28SMichael Tuexen 
2837d69e7322SRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2838d69e7322SRandall Stewart 		    stcb, net);
2839d69e7322SRandall Stewart 
2840f8829a4aSRandall Stewart 		if (stcb->asoc.sctp_autoclose_ticks &&
2841f8829a4aSRandall Stewart 		    sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2842f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2843f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, NULL);
2844f8829a4aSRandall Stewart 		}
2845f8829a4aSRandall Stewart 		/*
28461b649582SRandall Stewart 		 * send ASCONF if parameters are pending and ASCONFs are
28471b649582SRandall Stewart 		 * allowed (eg. addresses changed when init/cookie echo were
28481b649582SRandall Stewart 		 * in flight)
2849f8829a4aSRandall Stewart 		 */
2850f8829a4aSRandall Stewart 		if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2851c79bec9cSMichael Tuexen 		    (stcb->asoc.asconf_supported == 1) &&
2852f8829a4aSRandall Stewart 		    (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
28531b649582SRandall Stewart #ifdef SCTP_TIMER_BASED_ASCONF
2854f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2855f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb,
2856f8829a4aSRandall Stewart 			    stcb->asoc.primary_destination);
28571b649582SRandall Stewart #else
28583232788eSRandall Stewart 			sctp_send_asconf(stcb, stcb->asoc.primary_destination,
28593232788eSRandall Stewart 			    SCTP_ADDR_NOT_LOCKED);
28601b649582SRandall Stewart #endif
2861f8829a4aSRandall Stewart 		}
2862f8829a4aSRandall Stewart 	}
2863d69e7322SRandall Stewart closed_socket:
2864f8829a4aSRandall Stewart 	/* Toss the cookie if I can */
2865f8829a4aSRandall Stewart 	sctp_toss_old_cookies(stcb, asoc);
2866f8829a4aSRandall Stewart 	/* Restart the timer if we have pending data */
2867efd5e692SMichael Tuexen 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
2868efd5e692SMichael Tuexen 		if (chk->whoTo != NULL) {
2869efd5e692SMichael Tuexen 			break;
2870efd5e692SMichael Tuexen 		}
2871efd5e692SMichael Tuexen 	}
2872efd5e692SMichael Tuexen 	if (chk != NULL) {
28734a9ef3f8SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
2874f8829a4aSRandall Stewart 	}
2875f8829a4aSRandall Stewart }
2876f8829a4aSRandall Stewart 
2877f8829a4aSRandall Stewart static void
2878f8829a4aSRandall Stewart sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
2879f8829a4aSRandall Stewart     struct sctp_tcb *stcb)
2880f8829a4aSRandall Stewart {
2881f8829a4aSRandall Stewart 	struct sctp_nets *net;
2882f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *lchk;
2883a21779f0SRandall Stewart 	struct sctp_ecne_chunk bkup;
288460990c0cSMichael Tuexen 	uint8_t override_bit;
2885a21779f0SRandall Stewart 	uint32_t tsn, window_data_tsn;
2886a21779f0SRandall Stewart 	int len;
2887dec0177dSRandall Stewart 	unsigned int pkt_cnt;
2888f8829a4aSRandall Stewart 
2889a21779f0SRandall Stewart 	len = ntohs(cp->ch.chunk_length);
2890a21779f0SRandall Stewart 	if ((len != sizeof(struct sctp_ecne_chunk)) &&
2891a21779f0SRandall Stewart 	    (len != sizeof(struct old_sctp_ecne_chunk))) {
2892f8829a4aSRandall Stewart 		return;
2893f8829a4aSRandall Stewart 	}
2894a21779f0SRandall Stewart 	if (len == sizeof(struct old_sctp_ecne_chunk)) {
2895a21779f0SRandall Stewart 		/* Its the old format */
2896a21779f0SRandall Stewart 		memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
2897a21779f0SRandall Stewart 		bkup.num_pkts_since_cwr = htonl(1);
2898a21779f0SRandall Stewart 		cp = &bkup;
2899a21779f0SRandall Stewart 	}
2900f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvecne);
2901f8829a4aSRandall Stewart 	tsn = ntohl(cp->tsn);
2902a21779f0SRandall Stewart 	pkt_cnt = ntohl(cp->num_pkts_since_cwr);
2903a21779f0SRandall Stewart 	lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
2904f8829a4aSRandall Stewart 	if (lchk == NULL) {
2905493d8e5aSRandall Stewart 		window_data_tsn = stcb->asoc.sending_seq - 1;
2906f8829a4aSRandall Stewart 	} else {
290749656eefSMichael Tuexen 		window_data_tsn = lchk->rec.data.tsn;
2908f8829a4aSRandall Stewart 	}
2909f8829a4aSRandall Stewart 
2910a21779f0SRandall Stewart 	/* Find where it was sent to if possible. */
2911f8829a4aSRandall Stewart 	net = NULL;
29124a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
291349656eefSMichael Tuexen 		if (lchk->rec.data.tsn == tsn) {
2914f8829a4aSRandall Stewart 			net = lchk->whoTo;
2915899288aeSRandall Stewart 			net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
2916f8829a4aSRandall Stewart 			break;
2917f8829a4aSRandall Stewart 		}
291849656eefSMichael Tuexen 		if (SCTP_TSN_GT(lchk->rec.data.tsn, tsn)) {
2919f8829a4aSRandall Stewart 			break;
2920f8829a4aSRandall Stewart 		}
292120b07a4dSMichael Tuexen 	}
2922a21779f0SRandall Stewart 	if (net == NULL) {
2923a21779f0SRandall Stewart 		/*
2924a21779f0SRandall Stewart 		 * What to do. A previous send of a CWR was possibly lost.
2925a21779f0SRandall Stewart 		 * See how old it is, we may have it marked on the actual
2926a21779f0SRandall Stewart 		 * net.
2927a21779f0SRandall Stewart 		 */
2928a21779f0SRandall Stewart 		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2929a21779f0SRandall Stewart 			if (tsn == net->last_cwr_tsn) {
2930a21779f0SRandall Stewart 				/* Found him, send it off */
293160990c0cSMichael Tuexen 				break;
2932a21779f0SRandall Stewart 			}
2933a21779f0SRandall Stewart 		}
293460990c0cSMichael Tuexen 		if (net == NULL) {
2935a21779f0SRandall Stewart 			/*
293660990c0cSMichael Tuexen 			 * If we reach here, we need to send a special CWR
293760990c0cSMichael Tuexen 			 * that says hey, we did this a long time ago and
293860990c0cSMichael Tuexen 			 * you lost the response.
2939a21779f0SRandall Stewart 			 */
2940a21779f0SRandall Stewart 			net = TAILQ_FIRST(&stcb->asoc.nets);
294160990c0cSMichael Tuexen 			if (net == NULL) {
294260990c0cSMichael Tuexen 				/* TSNH */
294360990c0cSMichael Tuexen 				return;
2944a21779f0SRandall Stewart 			}
294560990c0cSMichael Tuexen 			override_bit = SCTP_CWR_REDUCE_OVERRIDE;
294660990c0cSMichael Tuexen 		} else {
294760990c0cSMichael Tuexen 			override_bit = 0;
294860990c0cSMichael Tuexen 		}
294960990c0cSMichael Tuexen 	} else {
295060990c0cSMichael Tuexen 		override_bit = 0;
295160990c0cSMichael Tuexen 	}
2952493d8e5aSRandall Stewart 	if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
2953493d8e5aSRandall Stewart 	    ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
2954b7b84c0eSMichael Tuexen 		/*
2955b7b84c0eSMichael Tuexen 		 * JRS - Use the congestion control given in the pluggable
2956b7b84c0eSMichael Tuexen 		 * CC module
2957b7b84c0eSMichael Tuexen 		 */
2958493d8e5aSRandall Stewart 		stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
2959f8829a4aSRandall Stewart 		/*
2960a21779f0SRandall Stewart 		 * We reduce once every RTT. So we will only lower cwnd at
2961a21779f0SRandall Stewart 		 * the next sending seq i.e. the window_data_tsn
2962f8829a4aSRandall Stewart 		 */
2963a21779f0SRandall Stewart 		net->cwr_window_tsn = window_data_tsn;
2964a21779f0SRandall Stewart 		net->ecn_ce_pkt_cnt += pkt_cnt;
2965a21779f0SRandall Stewart 		net->lost_cnt = pkt_cnt;
2966a21779f0SRandall Stewart 		net->last_cwr_tsn = tsn;
2967a21779f0SRandall Stewart 	} else {
2968a21779f0SRandall Stewart 		override_bit |= SCTP_CWR_IN_SAME_WINDOW;
2969493d8e5aSRandall Stewart 		if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
2970493d8e5aSRandall Stewart 		    ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
2971a21779f0SRandall Stewart 			/*
2972493d8e5aSRandall Stewart 			 * Another loss in the same window update how many
2973493d8e5aSRandall Stewart 			 * marks/packets lost we have had.
2974a21779f0SRandall Stewart 			 */
2975493d8e5aSRandall Stewart 			int cnt = 1;
2976a21779f0SRandall Stewart 
2977a21779f0SRandall Stewart 			if (pkt_cnt > net->lost_cnt) {
2978a21779f0SRandall Stewart 				/* Should be the case */
2979493d8e5aSRandall Stewart 				cnt = (pkt_cnt - net->lost_cnt);
2980493d8e5aSRandall Stewart 				net->ecn_ce_pkt_cnt += cnt;
2981a21779f0SRandall Stewart 			}
2982493d8e5aSRandall Stewart 			net->lost_cnt = pkt_cnt;
2983a21779f0SRandall Stewart 			net->last_cwr_tsn = tsn;
2984493d8e5aSRandall Stewart 			/*
2985493d8e5aSRandall Stewart 			 * Most CC functions will ignore this call, since we
2986493d8e5aSRandall Stewart 			 * are in-window yet of the initial CE the peer saw.
2987493d8e5aSRandall Stewart 			 */
2988493d8e5aSRandall Stewart 			stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
2989a21779f0SRandall Stewart 		}
2990f8829a4aSRandall Stewart 	}
2991f8829a4aSRandall Stewart 	/*
2992f8829a4aSRandall Stewart 	 * We always send a CWR this way if our previous one was lost our
2993f8829a4aSRandall Stewart 	 * peer will get an update, or if it is not time again to reduce we
2994a21779f0SRandall Stewart 	 * still get the cwr to the peer. Note we set the override when we
2995a21779f0SRandall Stewart 	 * could not find the TSN on the chunk or the destination network.
2996f8829a4aSRandall Stewart 	 */
2997a21779f0SRandall Stewart 	sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
2998f8829a4aSRandall Stewart }
2999f8829a4aSRandall Stewart 
3000f8829a4aSRandall Stewart static void
3001a21779f0SRandall Stewart sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3002f8829a4aSRandall Stewart {
3003f8829a4aSRandall Stewart 	/*
3004f8829a4aSRandall Stewart 	 * Here we get a CWR from the peer. We must look in the outqueue and
3005582212faSEitan Adler 	 * make sure that we have a covered ECNE in the control chunk part.
3006f8829a4aSRandall Stewart 	 * If so remove it.
3007f8829a4aSRandall Stewart 	 */
30086c2cfc04SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
3009f8829a4aSRandall Stewart 	struct sctp_ecne_chunk *ecne;
3010a21779f0SRandall Stewart 	int override;
3011a21779f0SRandall Stewart 	uint32_t cwr_tsn;
3012f8829a4aSRandall Stewart 
3013a21779f0SRandall Stewart 	cwr_tsn = ntohl(cp->tsn);
3014a21779f0SRandall Stewart 	override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
30156c2cfc04SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &stcb->asoc.control_send_queue, sctp_next, nchk) {
3016f8829a4aSRandall Stewart 		if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3017f8829a4aSRandall Stewart 			continue;
3018f8829a4aSRandall Stewart 		}
3019a21779f0SRandall Stewart 		if ((override == 0) && (chk->whoTo != net)) {
3020a21779f0SRandall Stewart 			/* Must be from the right src unless override is set */
3021a21779f0SRandall Stewart 			continue;
3022a21779f0SRandall Stewart 		}
3023f8829a4aSRandall Stewart 		ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3024a21779f0SRandall Stewart 		if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3025f8829a4aSRandall Stewart 			/* this covers this ECNE, we can remove it */
3026f8829a4aSRandall Stewart 			stcb->asoc.ecn_echo_cnt_onq--;
3027f8829a4aSRandall Stewart 			TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3028f8829a4aSRandall Stewart 			    sctp_next);
3029cd6340caSMichael Tuexen 			stcb->asoc.ctrl_queue_cnt--;
3030f8829a4aSRandall Stewart 			sctp_m_freem(chk->data);
3031f8829a4aSRandall Stewart 			chk->data = NULL;
3032689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3033a21779f0SRandall Stewart 			if (override == 0) {
3034f8829a4aSRandall Stewart 				break;
3035f8829a4aSRandall Stewart 			}
3036f8829a4aSRandall Stewart 		}
3037f8829a4aSRandall Stewart 	}
3038a21779f0SRandall Stewart }
3039f8829a4aSRandall Stewart 
3040f8829a4aSRandall Stewart static void
30417215cc1bSMichael Tuexen sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3042f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net)
3043f8829a4aSRandall Stewart {
3044ceaad40aSRandall Stewart 
3045ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
3046ad81507eSRandall Stewart 	    "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3047f8829a4aSRandall Stewart 	if (stcb == NULL)
3048f8829a4aSRandall Stewart 		return;
3049f8829a4aSRandall Stewart 
3050f8829a4aSRandall Stewart 	/* process according to association state */
3051839d21d6SMichael Tuexen 	if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3052f8829a4aSRandall Stewart 		/* unexpected SHUTDOWN-COMPLETE... so ignore... */
30532afb3e84SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
30542afb3e84SRandall Stewart 		    "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3055f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3056f8829a4aSRandall Stewart 		return;
3057f8829a4aSRandall Stewart 	}
3058f8829a4aSRandall Stewart 	/* notify upper layer protocol */
3059f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
3060ceaad40aSRandall Stewart 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
306156f778aaSMichael Tuexen 	}
306256f778aaSMichael Tuexen #ifdef INVARIANTS
30630f1346f7SMichael Tuexen 	if (!TAILQ_EMPTY(&stcb->asoc.send_queue) ||
30640f1346f7SMichael Tuexen 	    !TAILQ_EMPTY(&stcb->asoc.sent_queue) ||
3065124d851aSMichael Tuexen 	    sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
306656f778aaSMichael Tuexen 		panic("Queues are not empty when handling SHUTDOWN-COMPLETE");
3067f8829a4aSRandall Stewart 	}
306856f778aaSMichael Tuexen #endif
3069f8829a4aSRandall Stewart 	/* stop the timer */
3070b7d130beSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net,
3071b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3072f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3073f8829a4aSRandall Stewart 	/* free the TCB */
30742afb3e84SRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
30752afb3e84SRandall Stewart 	    "sctp_handle_shutdown_complete: calls free-asoc\n");
3076b7d130beSMichael Tuexen 	(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
3077b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3078f8829a4aSRandall Stewart 	return;
3079f8829a4aSRandall Stewart }
3080f8829a4aSRandall Stewart 
3081f8829a4aSRandall Stewart static int
3082f8829a4aSRandall Stewart process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3083f8829a4aSRandall Stewart     struct sctp_nets *net, uint8_t flg)
3084f8829a4aSRandall Stewart {
3085f8829a4aSRandall Stewart 	switch (desc->chunk_type) {
3086f8829a4aSRandall Stewart 	case SCTP_DATA:
308732df1c9eSMichael Tuexen 	case SCTP_IDATA:
308832df1c9eSMichael Tuexen 		/* find the tsn to resend (possibly) */
3089f8829a4aSRandall Stewart 		{
3090f8829a4aSRandall Stewart 			uint32_t tsn;
3091f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *tp1;
3092f8829a4aSRandall Stewart 
3093f8829a4aSRandall Stewart 			tsn = ntohl(desc->tsn_ifany);
30944a9ef3f8SMichael Tuexen 			TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
309549656eefSMichael Tuexen 				if (tp1->rec.data.tsn == tsn) {
3096f8829a4aSRandall Stewart 					/* found it */
3097f8829a4aSRandall Stewart 					break;
3098f8829a4aSRandall Stewart 				}
309949656eefSMichael Tuexen 				if (SCTP_TSN_GT(tp1->rec.data.tsn, tsn)) {
3100f8829a4aSRandall Stewart 					/* not found */
3101f8829a4aSRandall Stewart 					tp1 = NULL;
3102f8829a4aSRandall Stewart 					break;
3103f8829a4aSRandall Stewart 				}
3104f8829a4aSRandall Stewart 			}
3105f8829a4aSRandall Stewart 			if (tp1 == NULL) {
3106f8829a4aSRandall Stewart 				/*
3107f8829a4aSRandall Stewart 				 * Do it the other way , aka without paying
3108f8829a4aSRandall Stewart 				 * attention to queue seq order.
3109f8829a4aSRandall Stewart 				 */
3110f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpdnfnd);
31114a9ef3f8SMichael Tuexen 				TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
311249656eefSMichael Tuexen 					if (tp1->rec.data.tsn == tsn) {
3113f8829a4aSRandall Stewart 						/* found it */
3114f8829a4aSRandall Stewart 						break;
3115f8829a4aSRandall Stewart 					}
3116f8829a4aSRandall Stewart 				}
3117f8829a4aSRandall Stewart 			}
3118f8829a4aSRandall Stewart 			if (tp1 == NULL) {
3119f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrptsnnf);
3120f8829a4aSRandall Stewart 			}
3121f8829a4aSRandall Stewart 			if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
31227da23bc8SMichael Tuexen 				if (((flg & SCTP_BADCRC) == 0) &&
31237da23bc8SMichael Tuexen 				    ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
31247da23bc8SMichael Tuexen 					return (0);
31257da23bc8SMichael Tuexen 				}
3126f8829a4aSRandall Stewart 				if ((stcb->asoc.peers_rwnd == 0) &&
3127f8829a4aSRandall Stewart 				    ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3128f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_pdrpdiwnp);
3129f8829a4aSRandall Stewart 					return (0);
3130f8829a4aSRandall Stewart 				}
3131f8829a4aSRandall Stewart 				if (stcb->asoc.peers_rwnd == 0 &&
3132f8829a4aSRandall Stewart 				    (flg & SCTP_FROM_MIDDLE_BOX)) {
3133f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_pdrpdizrw);
3134f8829a4aSRandall Stewart 					return (0);
3135f8829a4aSRandall Stewart 				}
313632df1c9eSMichael Tuexen 				if ((uint32_t)SCTP_BUF_LEN(tp1->data) <
313732df1c9eSMichael Tuexen 				    SCTP_DATA_CHUNK_OVERHEAD(stcb) + SCTP_NUM_DB_TO_VERIFY) {
313832df1c9eSMichael Tuexen 					/* Payload not matching. */
3139f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_pdrpbadd);
3140f8829a4aSRandall Stewart 					return (-1);
3141f8829a4aSRandall Stewart 				}
314232df1c9eSMichael Tuexen 				if (memcmp(mtod(tp1->data, caddr_t)+SCTP_DATA_CHUNK_OVERHEAD(stcb),
314332df1c9eSMichael Tuexen 				    desc->data_bytes, SCTP_NUM_DB_TO_VERIFY) != 0) {
314432df1c9eSMichael Tuexen 					/* Payload not matching. */
314532df1c9eSMichael Tuexen 					SCTP_STAT_INCR(sctps_pdrpbadd);
314632df1c9eSMichael Tuexen 					return (-1);
3147f8829a4aSRandall Stewart 				}
3148f8829a4aSRandall Stewart 				if (tp1->do_rtt) {
3149f8829a4aSRandall Stewart 					/*
3150f8829a4aSRandall Stewart 					 * this guy had a RTO calculation
3151f8829a4aSRandall Stewart 					 * pending on it, cancel it
3152f8829a4aSRandall Stewart 					 */
3153f79aab18SRandall Stewart 					if (tp1->whoTo->rto_needed == 0) {
3154f79aab18SRandall Stewart 						tp1->whoTo->rto_needed = 1;
3155f79aab18SRandall Stewart 					}
3156f8829a4aSRandall Stewart 					tp1->do_rtt = 0;
3157f8829a4aSRandall Stewart 				}
3158f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpmark);
3159f8829a4aSRandall Stewart 				if (tp1->sent != SCTP_DATAGRAM_RESEND)
3160f8829a4aSRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3161f8829a4aSRandall Stewart 				/*
3162f8829a4aSRandall Stewart 				 * mark it as if we were doing a FR, since
3163f8829a4aSRandall Stewart 				 * we will be getting gap ack reports behind
3164f8829a4aSRandall Stewart 				 * the info from the router.
3165f8829a4aSRandall Stewart 				 */
3166f8829a4aSRandall Stewart 				tp1->rec.data.doing_fast_retransmit = 1;
3167f8829a4aSRandall Stewart 				/*
3168f8829a4aSRandall Stewart 				 * mark the tsn with what sequences can
3169f8829a4aSRandall Stewart 				 * cause a new FR.
3170f8829a4aSRandall Stewart 				 */
3171f8829a4aSRandall Stewart 				if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3172f8829a4aSRandall Stewart 					tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3173f8829a4aSRandall Stewart 				} else {
317449656eefSMichael Tuexen 					tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.tsn;
3175f8829a4aSRandall Stewart 				}
3176f8829a4aSRandall Stewart 
3177f8829a4aSRandall Stewart 				/* restart the timer */
3178f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3179b7d130beSMichael Tuexen 				    stcb, tp1->whoTo,
3180b7d130beSMichael Tuexen 				    SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3181f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3182f8829a4aSRandall Stewart 				    stcb, tp1->whoTo);
3183f8829a4aSRandall Stewart 
3184f8829a4aSRandall Stewart 				/* fix counts and things */
3185b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3186c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3187a5d547adSRandall Stewart 					    tp1->whoTo->flight_size,
3188a5d547adSRandall Stewart 					    tp1->book_size,
31899a8e3088SMichael Tuexen 					    (uint32_t)(uintptr_t)stcb,
319049656eefSMichael Tuexen 					    tp1->rec.data.tsn);
319180fefe0aSRandall Stewart 				}
31928933fa13SRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3193c105859eSRandall Stewart 					sctp_flight_size_decrease(tp1);
3194c105859eSRandall Stewart 					sctp_total_flight_decrease(stcb, tp1);
31958933fa13SRandall Stewart 				}
3196f23ba7b1SMichael Tuexen 				tp1->sent = SCTP_DATAGRAM_RESEND;
3197f8829a4aSRandall Stewart 			} {
3198f8829a4aSRandall Stewart 				/* audit code */
3199f8829a4aSRandall Stewart 				unsigned int audit;
3200f8829a4aSRandall Stewart 
3201f8829a4aSRandall Stewart 				audit = 0;
3202f8829a4aSRandall Stewart 				TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3203f8829a4aSRandall Stewart 					if (tp1->sent == SCTP_DATAGRAM_RESEND)
3204f8829a4aSRandall Stewart 						audit++;
3205f8829a4aSRandall Stewart 				}
3206f8829a4aSRandall Stewart 				TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3207f8829a4aSRandall Stewart 				    sctp_next) {
3208f8829a4aSRandall Stewart 					if (tp1->sent == SCTP_DATAGRAM_RESEND)
3209f8829a4aSRandall Stewart 						audit++;
3210f8829a4aSRandall Stewart 				}
3211f8829a4aSRandall Stewart 				if (audit != stcb->asoc.sent_queue_retran_cnt) {
3212ad81507eSRandall Stewart 					SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3213f8829a4aSRandall Stewart 					    audit, stcb->asoc.sent_queue_retran_cnt);
3214f8829a4aSRandall Stewart #ifndef SCTP_AUDITING_ENABLED
3215f8829a4aSRandall Stewart 					stcb->asoc.sent_queue_retran_cnt = audit;
3216f8829a4aSRandall Stewart #endif
3217f8829a4aSRandall Stewart 				}
3218f8829a4aSRandall Stewart 			}
3219f8829a4aSRandall Stewart 		}
3220f8829a4aSRandall Stewart 		break;
3221f8829a4aSRandall Stewart 	case SCTP_ASCONF:
3222f8829a4aSRandall Stewart 		{
3223f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *asconf;
3224f8829a4aSRandall Stewart 
3225f8829a4aSRandall Stewart 			TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3226f8829a4aSRandall Stewart 			    sctp_next) {
3227f8829a4aSRandall Stewart 				if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3228f8829a4aSRandall Stewart 					break;
3229f8829a4aSRandall Stewart 				}
3230f8829a4aSRandall Stewart 			}
3231f8829a4aSRandall Stewart 			if (asconf) {
3232f8829a4aSRandall Stewart 				if (asconf->sent != SCTP_DATAGRAM_RESEND)
3233f8829a4aSRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3234f8829a4aSRandall Stewart 				asconf->sent = SCTP_DATAGRAM_RESEND;
3235f8829a4aSRandall Stewart 				asconf->snd_count--;
3236f8829a4aSRandall Stewart 			}
3237f8829a4aSRandall Stewart 		}
3238f8829a4aSRandall Stewart 		break;
3239f8829a4aSRandall Stewart 	case SCTP_INITIATION:
3240f8829a4aSRandall Stewart 		/* resend the INIT */
3241f8829a4aSRandall Stewart 		stcb->asoc.dropped_special_cnt++;
3242f8829a4aSRandall Stewart 		if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3243f8829a4aSRandall Stewart 			/*
3244f8829a4aSRandall Stewart 			 * If we can get it in, in a few attempts we do
3245f8829a4aSRandall Stewart 			 * this, otherwise we let the timer fire.
3246f8829a4aSRandall Stewart 			 */
3247f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3248b7d130beSMichael Tuexen 			    stcb, net,
3249b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
3250ceaad40aSRandall Stewart 			sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3251f8829a4aSRandall Stewart 		}
3252f8829a4aSRandall Stewart 		break;
3253f8829a4aSRandall Stewart 	case SCTP_SELECTIVE_ACK:
3254b5c16493SMichael Tuexen 	case SCTP_NR_SELECTIVE_ACK:
3255f8829a4aSRandall Stewart 		/* resend the sack */
3256689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3257f8829a4aSRandall Stewart 		break;
3258f8829a4aSRandall Stewart 	case SCTP_HEARTBEAT_REQUEST:
3259f8829a4aSRandall Stewart 		/* resend a demand HB */
3260b54d3a6cSRandall Stewart 		if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3261b7b84c0eSMichael Tuexen 			/*
3262b7b84c0eSMichael Tuexen 			 * Only retransmit if we KNOW we wont destroy the
3263b7b84c0eSMichael Tuexen 			 * tcb
3264b7b84c0eSMichael Tuexen 			 */
3265ca85e948SMichael Tuexen 			sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3266b54d3a6cSRandall Stewart 		}
3267f8829a4aSRandall Stewart 		break;
3268f8829a4aSRandall Stewart 	case SCTP_SHUTDOWN:
3269f8829a4aSRandall Stewart 		sctp_send_shutdown(stcb, net);
3270f8829a4aSRandall Stewart 		break;
3271f8829a4aSRandall Stewart 	case SCTP_SHUTDOWN_ACK:
3272f8829a4aSRandall Stewart 		sctp_send_shutdown_ack(stcb, net);
3273f8829a4aSRandall Stewart 		break;
3274f8829a4aSRandall Stewart 	case SCTP_COOKIE_ECHO:
3275f8829a4aSRandall Stewart 		{
3276f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *cookie;
3277f8829a4aSRandall Stewart 
3278f8829a4aSRandall Stewart 			cookie = NULL;
3279f8829a4aSRandall Stewart 			TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3280f8829a4aSRandall Stewart 			    sctp_next) {
3281f8829a4aSRandall Stewart 				if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3282f8829a4aSRandall Stewart 					break;
3283f8829a4aSRandall Stewart 				}
3284f8829a4aSRandall Stewart 			}
3285f8829a4aSRandall Stewart 			if (cookie) {
3286f8829a4aSRandall Stewart 				if (cookie->sent != SCTP_DATAGRAM_RESEND)
3287f8829a4aSRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3288f8829a4aSRandall Stewart 				cookie->sent = SCTP_DATAGRAM_RESEND;
3289f8829a4aSRandall Stewart 				sctp_stop_all_cookie_timers(stcb);
3290f8829a4aSRandall Stewart 			}
3291f8829a4aSRandall Stewart 		}
3292f8829a4aSRandall Stewart 		break;
3293f8829a4aSRandall Stewart 	case SCTP_COOKIE_ACK:
3294f8829a4aSRandall Stewart 		sctp_send_cookie_ack(stcb);
3295f8829a4aSRandall Stewart 		break;
3296f8829a4aSRandall Stewart 	case SCTP_ASCONF_ACK:
3297f8829a4aSRandall Stewart 		/* resend last asconf ack */
32982afb3e84SRandall Stewart 		sctp_send_asconf_ack(stcb);
3299f8829a4aSRandall Stewart 		break;
330044249214SRandall Stewart 	case SCTP_IFORWARD_CUM_TSN:
3301f8829a4aSRandall Stewart 	case SCTP_FORWARD_CUM_TSN:
3302f8829a4aSRandall Stewart 		send_forward_tsn(stcb, &stcb->asoc);
3303f8829a4aSRandall Stewart 		break;
3304f8829a4aSRandall Stewart 		/* can't do anything with these */
3305f8829a4aSRandall Stewart 	case SCTP_PACKET_DROPPED:
3306f8829a4aSRandall Stewart 	case SCTP_INITIATION_ACK:	/* this should not happen */
3307f8829a4aSRandall Stewart 	case SCTP_HEARTBEAT_ACK:
3308f8829a4aSRandall Stewart 	case SCTP_ABORT_ASSOCIATION:
3309f8829a4aSRandall Stewart 	case SCTP_OPERATION_ERROR:
3310f8829a4aSRandall Stewart 	case SCTP_SHUTDOWN_COMPLETE:
3311f8829a4aSRandall Stewart 	case SCTP_ECN_ECHO:
3312f8829a4aSRandall Stewart 	case SCTP_ECN_CWR:
3313f8829a4aSRandall Stewart 	default:
3314f8829a4aSRandall Stewart 		break;
3315f8829a4aSRandall Stewart 	}
3316f8829a4aSRandall Stewart 	return (0);
3317f8829a4aSRandall Stewart }
3318f8829a4aSRandall Stewart 
3319f8829a4aSRandall Stewart void
3320a169d6ecSMichael Tuexen sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3321f8829a4aSRandall Stewart {
3322a169d6ecSMichael Tuexen 	uint32_t i;
3323f8829a4aSRandall Stewart 	uint16_t temp;
3324f8829a4aSRandall Stewart 
3325f8829a4aSRandall Stewart 	/*
332644249214SRandall Stewart 	 * We set things to 0xffffffff since this is the last delivered
332744249214SRandall Stewart 	 * sequence and we will be sending in 0 after the reset.
3328f8829a4aSRandall Stewart 	 */
3329f8829a4aSRandall Stewart 
3330f8829a4aSRandall Stewart 	if (number_entries) {
3331f8829a4aSRandall Stewart 		for (i = 0; i < number_entries; i++) {
3332f8829a4aSRandall Stewart 			temp = ntohs(list[i]);
3333f8829a4aSRandall Stewart 			if (temp >= stcb->asoc.streamincnt) {
3334f8829a4aSRandall Stewart 				continue;
3335f8829a4aSRandall Stewart 			}
333649656eefSMichael Tuexen 			stcb->asoc.strmin[temp].last_mid_delivered = 0xffffffff;
3337f8829a4aSRandall Stewart 		}
3338f8829a4aSRandall Stewart 	} else {
3339f8829a4aSRandall Stewart 		list = NULL;
3340f8829a4aSRandall Stewart 		for (i = 0; i < stcb->asoc.streamincnt; i++) {
334149656eefSMichael Tuexen 			stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
3342f8829a4aSRandall Stewart 		}
3343f8829a4aSRandall Stewart 	}
3344ceaad40aSRandall Stewart 	sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3345f8829a4aSRandall Stewart }
3346f8829a4aSRandall Stewart 
3347f8829a4aSRandall Stewart static void
334856f778aaSMichael Tuexen sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3349f8829a4aSRandall Stewart {
335056f778aaSMichael Tuexen 	uint32_t i;
3351f8829a4aSRandall Stewart 	uint16_t temp;
3352f8829a4aSRandall Stewart 
335356f778aaSMichael Tuexen 	if (number_entries > 0) {
335456f778aaSMichael Tuexen 		for (i = 0; i < number_entries; i++) {
3355f8829a4aSRandall Stewart 			temp = ntohs(list[i]);
3356f8829a4aSRandall Stewart 			if (temp >= stcb->asoc.streamoutcnt) {
3357f8829a4aSRandall Stewart 				/* no such stream */
3358f8829a4aSRandall Stewart 				continue;
3359f8829a4aSRandall Stewart 			}
336063d5b568SMichael Tuexen 			stcb->asoc.strmout[temp].next_mid_ordered = 0;
336163d5b568SMichael Tuexen 			stcb->asoc.strmout[temp].next_mid_unordered = 0;
3362f8829a4aSRandall Stewart 		}
336356f778aaSMichael Tuexen 	} else {
336456f778aaSMichael Tuexen 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
336563d5b568SMichael Tuexen 			stcb->asoc.strmout[i].next_mid_ordered = 0;
336663d5b568SMichael Tuexen 			stcb->asoc.strmout[i].next_mid_unordered = 0;
336756f778aaSMichael Tuexen 		}
3368f8829a4aSRandall Stewart 	}
3369ceaad40aSRandall Stewart 	sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3370f8829a4aSRandall Stewart }
3371f8829a4aSRandall Stewart 
33727cca1775SRandall Stewart static void
33737cca1775SRandall Stewart sctp_reset_clear_pending(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
33747cca1775SRandall Stewart {
33757cca1775SRandall Stewart 	uint32_t i;
33767cca1775SRandall Stewart 	uint16_t temp;
33777cca1775SRandall Stewart 
33787cca1775SRandall Stewart 	if (number_entries > 0) {
33797cca1775SRandall Stewart 		for (i = 0; i < number_entries; i++) {
33807cca1775SRandall Stewart 			temp = ntohs(list[i]);
33817cca1775SRandall Stewart 			if (temp >= stcb->asoc.streamoutcnt) {
33827cca1775SRandall Stewart 				/* no such stream */
33837cca1775SRandall Stewart 				continue;
33847cca1775SRandall Stewart 			}
33857cca1775SRandall Stewart 			stcb->asoc.strmout[temp].state = SCTP_STREAM_OPEN;
33867cca1775SRandall Stewart 		}
33877cca1775SRandall Stewart 	} else {
33887cca1775SRandall Stewart 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
33897cca1775SRandall Stewart 			stcb->asoc.strmout[i].state = SCTP_STREAM_OPEN;
33907cca1775SRandall Stewart 		}
33917cca1775SRandall Stewart 	}
33927cca1775SRandall Stewart }
33937cca1775SRandall Stewart 
339484f3b49aSMichael Tuexen struct sctp_stream_reset_request *
3395f8829a4aSRandall Stewart sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3396f8829a4aSRandall Stewart {
3397f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3398a169d6ecSMichael Tuexen 	struct sctp_chunkhdr *ch;
339984f3b49aSMichael Tuexen 	struct sctp_stream_reset_request *r;
3400f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
3401f8829a4aSRandall Stewart 	int len, clen;
3402f8829a4aSRandall Stewart 
3403f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
3404163153c2SMichael Tuexen 	chk = asoc->str_reset;
3405163153c2SMichael Tuexen 	if (TAILQ_EMPTY(&asoc->control_send_queue) ||
3406163153c2SMichael Tuexen 	    (chk == NULL)) {
3407d06c82f1SRandall Stewart 		asoc->stream_reset_outstanding = 0;
3408f8829a4aSRandall Stewart 		return (NULL);
3409f8829a4aSRandall Stewart 	}
3410f8829a4aSRandall Stewart 	if (chk->data == NULL) {
3411f8829a4aSRandall Stewart 		return (NULL);
3412f8829a4aSRandall Stewart 	}
3413163153c2SMichael Tuexen 	if (bchk != NULL) {
3414f8829a4aSRandall Stewart 		/* he wants a copy of the chk pointer */
3415f8829a4aSRandall Stewart 		*bchk = chk;
3416f8829a4aSRandall Stewart 	}
3417f8829a4aSRandall Stewart 	clen = chk->send_size;
3418a169d6ecSMichael Tuexen 	ch = mtod(chk->data, struct sctp_chunkhdr *);
341984f3b49aSMichael Tuexen 	r = (struct sctp_stream_reset_request *)(ch + 1);
3420f8829a4aSRandall Stewart 	if (ntohl(r->request_seq) == seq) {
3421f8829a4aSRandall Stewart 		/* found it */
3422f8829a4aSRandall Stewart 		return (r);
3423f8829a4aSRandall Stewart 	}
3424f8829a4aSRandall Stewart 	len = SCTP_SIZE32(ntohs(r->ph.param_length));
3425f8829a4aSRandall Stewart 	if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3426f8829a4aSRandall Stewart 		/* move to the next one, there can only be a max of two */
342784f3b49aSMichael Tuexen 		r = (struct sctp_stream_reset_request *)((caddr_t)r + len);
3428f8829a4aSRandall Stewart 		if (ntohl(r->request_seq) == seq) {
3429f8829a4aSRandall Stewart 			return (r);
3430f8829a4aSRandall Stewart 		}
3431f8829a4aSRandall Stewart 	}
3432f8829a4aSRandall Stewart 	/* that seq is not here */
3433f8829a4aSRandall Stewart 	return (NULL);
3434f8829a4aSRandall Stewart }
3435f8829a4aSRandall Stewart 
3436f8829a4aSRandall Stewart static void
3437f8829a4aSRandall Stewart sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3438f8829a4aSRandall Stewart {
3439f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3440cd6340caSMichael Tuexen 	struct sctp_tmit_chunk *chk;
3441f8829a4aSRandall Stewart 
3442cd6340caSMichael Tuexen 	asoc = &stcb->asoc;
3443cd6340caSMichael Tuexen 	chk = asoc->str_reset;
3444cd6340caSMichael Tuexen 	if (chk == NULL) {
3445f8829a4aSRandall Stewart 		return;
3446f8829a4aSRandall Stewart 	}
3447cd6340caSMichael Tuexen 	asoc->str_reset = NULL;
3448b7d130beSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb,
34496fb7b4fbSMichael Tuexen 	    NULL, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28);
3450cd6340caSMichael Tuexen 	TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
3451cd6340caSMichael Tuexen 	asoc->ctrl_queue_cnt--;
3452f8829a4aSRandall Stewart 	if (chk->data) {
3453f8829a4aSRandall Stewart 		sctp_m_freem(chk->data);
3454f8829a4aSRandall Stewart 		chk->data = NULL;
3455f8829a4aSRandall Stewart 	}
3456689e6a5fSMichael Tuexen 	sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3457f8829a4aSRandall Stewart }
3458f8829a4aSRandall Stewart 
3459f8829a4aSRandall Stewart static int
3460f8829a4aSRandall Stewart sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3461f8829a4aSRandall Stewart     uint32_t seq, uint32_t action,
346208598d70SRandall Stewart     struct sctp_stream_reset_response *respin)
3463f8829a4aSRandall Stewart {
3464f8829a4aSRandall Stewart 	uint16_t type;
3465f4358911SMichael Tuexen 	int lparam_len;
3466f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3467f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
346884f3b49aSMichael Tuexen 	struct sctp_stream_reset_request *req_param;
346984f3b49aSMichael Tuexen 	struct sctp_stream_reset_out_request *req_out_param;
347084f3b49aSMichael Tuexen 	struct sctp_stream_reset_in_request *req_in_param;
347156f778aaSMichael Tuexen 	uint32_t number_entries;
3472f8829a4aSRandall Stewart 
3473f8829a4aSRandall Stewart 	if (asoc->stream_reset_outstanding == 0) {
3474f8829a4aSRandall Stewart 		/* duplicate */
3475f8829a4aSRandall Stewart 		return (0);
3476f8829a4aSRandall Stewart 	}
3477f8829a4aSRandall Stewart 	if (seq == stcb->asoc.str_reset_seq_out) {
347884f3b49aSMichael Tuexen 		req_param = sctp_find_stream_reset(stcb, seq, &chk);
347984f3b49aSMichael Tuexen 		if (req_param != NULL) {
3480f8829a4aSRandall Stewart 			stcb->asoc.str_reset_seq_out++;
348184f3b49aSMichael Tuexen 			type = ntohs(req_param->ph.param_type);
3482f4358911SMichael Tuexen 			lparam_len = ntohs(req_param->ph.param_length);
3483f8829a4aSRandall Stewart 			if (type == SCTP_STR_RESET_OUT_REQUEST) {
34847cca1775SRandall Stewart 				int no_clear = 0;
34857cca1775SRandall Stewart 
348684f3b49aSMichael Tuexen 				req_out_param = (struct sctp_stream_reset_out_request *)req_param;
3487f4358911SMichael Tuexen 				number_entries = (lparam_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3488f8829a4aSRandall Stewart 				asoc->stream_reset_out_is_outstanding = 0;
3489f8829a4aSRandall Stewart 				if (asoc->stream_reset_outstanding)
3490f8829a4aSRandall Stewart 					asoc->stream_reset_outstanding--;
3491f3ebe64cSMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3492f8829a4aSRandall Stewart 					/* do it */
349384f3b49aSMichael Tuexen 					sctp_reset_out_streams(stcb, number_entries, req_out_param->list_of_streams);
3494d4260646SMichael Tuexen 				} else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
349584f3b49aSMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
34967cca1775SRandall Stewart 				} else if (action == SCTP_STREAM_RESET_RESULT_IN_PROGRESS) {
3497b7b84c0eSMichael Tuexen 					/*
3498b7b84c0eSMichael Tuexen 					 * Set it up so we don't stop
3499b7b84c0eSMichael Tuexen 					 * retransmitting
3500b7b84c0eSMichael Tuexen 					 */
3501a4889f2dSMichael Tuexen 					asoc->stream_reset_outstanding++;
35027cca1775SRandall Stewart 					stcb->asoc.str_reset_seq_out--;
35037cca1775SRandall Stewart 					asoc->stream_reset_out_is_outstanding = 1;
35047cca1775SRandall Stewart 					no_clear = 1;
3505f8829a4aSRandall Stewart 				} else {
350684f3b49aSMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3507f8829a4aSRandall Stewart 				}
35087cca1775SRandall Stewart 				if (no_clear == 0) {
35097cca1775SRandall Stewart 					sctp_reset_clear_pending(stcb, number_entries, req_out_param->list_of_streams);
35107cca1775SRandall Stewart 				}
3511f8829a4aSRandall Stewart 			} else if (type == SCTP_STR_RESET_IN_REQUEST) {
351284f3b49aSMichael Tuexen 				req_in_param = (struct sctp_stream_reset_in_request *)req_param;
3513f4358911SMichael Tuexen 				number_entries = (lparam_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3514f8829a4aSRandall Stewart 				if (asoc->stream_reset_outstanding)
3515f8829a4aSRandall Stewart 					asoc->stream_reset_outstanding--;
3516d4260646SMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3517d4260646SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
351884f3b49aSMichael Tuexen 					    number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3519d4260646SMichael Tuexen 				} else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3520c4e848b7SRandall Stewart 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
352184f3b49aSMichael Tuexen 					    number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3522f8829a4aSRandall Stewart 				}
3523c4e848b7SRandall Stewart 			} else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3524ea44232bSRandall Stewart 				/* Ok we now may have more streams */
3525c4e848b7SRandall Stewart 				int num_stream;
3526c4e848b7SRandall Stewart 
3527c4e848b7SRandall Stewart 				num_stream = stcb->asoc.strm_pending_add_size;
3528c4e848b7SRandall Stewart 				if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3529c4e848b7SRandall Stewart 					/* TSNH */
3530c4e848b7SRandall Stewart 					num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3531c4e848b7SRandall Stewart 				}
3532c4e848b7SRandall Stewart 				stcb->asoc.strm_pending_add_size = 0;
35338aae9493SRandall Stewart 				if (asoc->stream_reset_outstanding)
35348aae9493SRandall Stewart 					asoc->stream_reset_outstanding--;
3535f3ebe64cSMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3536ea44232bSRandall Stewart 					/* Put the new streams into effect */
35377cca1775SRandall Stewart 					int i;
35387cca1775SRandall Stewart 
35397cca1775SRandall Stewart 					for (i = asoc->streamoutcnt; i < (asoc->streamoutcnt + num_stream); i++) {
35407cca1775SRandall Stewart 						asoc->strmout[i].state = SCTP_STREAM_OPEN;
35417cca1775SRandall Stewart 					}
35427cca1775SRandall Stewart 					asoc->streamoutcnt += num_stream;
3543c4e848b7SRandall Stewart 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3544d4260646SMichael Tuexen 				} else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3545d4260646SMichael Tuexen 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3546d4260646SMichael Tuexen 					    SCTP_STREAM_CHANGE_DENIED);
3547ea44232bSRandall Stewart 				} else {
3548c4e848b7SRandall Stewart 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3549d4260646SMichael Tuexen 					    SCTP_STREAM_CHANGE_FAILED);
3550c4e848b7SRandall Stewart 				}
3551c4e848b7SRandall Stewart 			} else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3552c4e848b7SRandall Stewart 				if (asoc->stream_reset_outstanding)
3553c4e848b7SRandall Stewart 					asoc->stream_reset_outstanding--;
3554d4260646SMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3555c4e848b7SRandall Stewart 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3556d4260646SMichael Tuexen 					    SCTP_STREAM_CHANGE_DENIED);
3557d4260646SMichael Tuexen 				} else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3558d4260646SMichael Tuexen 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3559d4260646SMichael Tuexen 					    SCTP_STREAM_CHANGE_FAILED);
3560ea44232bSRandall Stewart 				}
3561f8829a4aSRandall Stewart 			} else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3562f8829a4aSRandall Stewart 				/**
3563f8829a4aSRandall Stewart 				 * a) Adopt the new in tsn.
3564f8829a4aSRandall Stewart 				 * b) reset the map
3565f8829a4aSRandall Stewart 				 * c) Adopt the new out-tsn
3566f8829a4aSRandall Stewart 				 */
3567f8829a4aSRandall Stewart 				struct sctp_stream_reset_response_tsn *resp;
3568f8829a4aSRandall Stewart 				struct sctp_forward_tsn_chunk fwdtsn;
3569f8829a4aSRandall Stewart 				int abort_flag = 0;
3570f8829a4aSRandall Stewart 
3571f8829a4aSRandall Stewart 				if (respin == NULL) {
3572f8829a4aSRandall Stewart 					/* huh ? */
3573f8829a4aSRandall Stewart 					return (0);
3574f8829a4aSRandall Stewart 				}
35756a58f0e9SXin LI 				if (ntohs(respin->ph.param_length) < sizeof(struct sctp_stream_reset_response_tsn)) {
35766a58f0e9SXin LI 					return (0);
35776a58f0e9SXin LI 				}
3578f3ebe64cSMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3579f8829a4aSRandall Stewart 					resp = (struct sctp_stream_reset_response_tsn *)respin;
3580f8829a4aSRandall Stewart 					asoc->stream_reset_outstanding--;
3581f8829a4aSRandall Stewart 					fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3582f8829a4aSRandall Stewart 					fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3583f8829a4aSRandall Stewart 					fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3584671d309cSRandall Stewart 					sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3585f8829a4aSRandall Stewart 					if (abort_flag) {
3586f8829a4aSRandall Stewart 						return (1);
3587f8829a4aSRandall Stewart 					}
3588f8829a4aSRandall Stewart 					stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3589b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3590b3f1ea41SRandall Stewart 						sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3591b3f1ea41SRandall Stewart 					}
35920053ed28SMichael Tuexen 
3593d6af161aSRandall Stewart 					stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3594f8829a4aSRandall Stewart 					stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3595f8829a4aSRandall Stewart 					memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3596830d754dSRandall Stewart 
3597830d754dSRandall Stewart 					stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3598b5c16493SMichael Tuexen 					memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
359977acdc25SRandall Stewart 
3600f8829a4aSRandall Stewart 					stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3601f8829a4aSRandall Stewart 					stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3602f8829a4aSRandall Stewart 
3603f8829a4aSRandall Stewart 					sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3604f8829a4aSRandall Stewart 					sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3605c4e848b7SRandall Stewart 					sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3606d4260646SMichael Tuexen 				} else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3607d4260646SMichael Tuexen 					sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3608d4260646SMichael Tuexen 					    SCTP_ASSOC_RESET_DENIED);
3609c4e848b7SRandall Stewart 				} else {
3610c4e848b7SRandall Stewart 					sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3611d4260646SMichael Tuexen 					    SCTP_ASSOC_RESET_FAILED);
3612f8829a4aSRandall Stewart 				}
3613f8829a4aSRandall Stewart 			}
3614f8829a4aSRandall Stewart 			/* get rid of the request and get the request flags */
3615f8829a4aSRandall Stewart 			if (asoc->stream_reset_outstanding == 0) {
3616f8829a4aSRandall Stewart 				sctp_clean_up_stream_reset(stcb);
3617f8829a4aSRandall Stewart 			}
3618f8829a4aSRandall Stewart 		}
3619f8829a4aSRandall Stewart 	}
36207cca1775SRandall Stewart 	if (asoc->stream_reset_outstanding == 0) {
3621c6168599SRandall Stewart 		sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
36227cca1775SRandall Stewart 	}
3623f8829a4aSRandall Stewart 	return (0);
3624f8829a4aSRandall Stewart }
3625f8829a4aSRandall Stewart 
3626f8829a4aSRandall Stewart static void
3627f8829a4aSRandall Stewart sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3628f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk,
3629671d309cSRandall Stewart     struct sctp_stream_reset_in_request *req, int trunc)
3630f8829a4aSRandall Stewart {
3631f8829a4aSRandall Stewart 	uint32_t seq;
3632f8829a4aSRandall Stewart 	int len, i;
3633f8829a4aSRandall Stewart 	int number_entries;
3634f8829a4aSRandall Stewart 	uint16_t temp;
3635f8829a4aSRandall Stewart 
3636f8829a4aSRandall Stewart 	/*
3637f8829a4aSRandall Stewart 	 * peer wants me to send a str-reset to him for my outgoing seq's if
3638f8829a4aSRandall Stewart 	 * seq_in is right.
3639f8829a4aSRandall Stewart 	 */
3640f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3641f8829a4aSRandall Stewart 
3642f8829a4aSRandall Stewart 	seq = ntohl(req->request_seq);
3643f8829a4aSRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3644671d309cSRandall Stewart 		asoc->last_reset_action[1] = asoc->last_reset_action[0];
3645f3ebe64cSMichael Tuexen 		if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3646f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3647f3ebe64cSMichael Tuexen 		} else if (trunc) {
3648f3ebe64cSMichael Tuexen 			/* Can't do it, since they exceeded our buffer size  */
3649f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3650671d309cSRandall Stewart 		} else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3651f8829a4aSRandall Stewart 			len = ntohs(req->ph.param_length);
3652f8829a4aSRandall Stewart 			number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
36537cca1775SRandall Stewart 			if (number_entries) {
3654f8829a4aSRandall Stewart 				for (i = 0; i < number_entries; i++) {
3655f8829a4aSRandall Stewart 					temp = ntohs(req->list_of_streams[i]);
36567cca1775SRandall Stewart 					if (temp >= stcb->asoc.streamoutcnt) {
36577cca1775SRandall Stewart 						asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
36587cca1775SRandall Stewart 						goto bad_boy;
36597cca1775SRandall Stewart 					}
3660f8829a4aSRandall Stewart 					req->list_of_streams[i] = temp;
3661f8829a4aSRandall Stewart 				}
36627cca1775SRandall Stewart 				for (i = 0; i < number_entries; i++) {
36637cca1775SRandall Stewart 					if (stcb->asoc.strmout[req->list_of_streams[i]].state == SCTP_STREAM_OPEN) {
36647cca1775SRandall Stewart 						stcb->asoc.strmout[req->list_of_streams[i]].state = SCTP_STREAM_RESET_PENDING;
36657cca1775SRandall Stewart 					}
36667cca1775SRandall Stewart 				}
36677cca1775SRandall Stewart 			} else {
36687cca1775SRandall Stewart 				/* Its all */
36697cca1775SRandall Stewart 				for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
36707cca1775SRandall Stewart 					if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN)
36717cca1775SRandall Stewart 						stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
36727cca1775SRandall Stewart 				}
36737cca1775SRandall Stewart 			}
3674f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3675f8829a4aSRandall Stewart 		} else {
3676f8829a4aSRandall Stewart 			/* Can't do it, since we have sent one out */
3677f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3678f8829a4aSRandall Stewart 		}
36797cca1775SRandall Stewart bad_boy:
3680c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3681f8829a4aSRandall Stewart 		asoc->str_reset_seq_in++;
3682f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 1 == seq) {
3683f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3684f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 2 == seq) {
3685f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3686f8829a4aSRandall Stewart 	} else {
3687f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3688f8829a4aSRandall Stewart 	}
3689c6168599SRandall Stewart 	sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3690f8829a4aSRandall Stewart }
3691f8829a4aSRandall Stewart 
3692f8829a4aSRandall Stewart static int
3693f8829a4aSRandall Stewart sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3694f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk,
3695f8829a4aSRandall Stewart     struct sctp_stream_reset_tsn_request *req)
3696f8829a4aSRandall Stewart {
3697f8829a4aSRandall Stewart 	/* reset all in and out and update the tsn */
3698f8829a4aSRandall Stewart 	/*
3699f8829a4aSRandall Stewart 	 * A) reset my str-seq's on in and out. B) Select a receive next,
3700f8829a4aSRandall Stewart 	 * and set cum-ack to it. Also process this selected number as a
3701f8829a4aSRandall Stewart 	 * fwd-tsn as well. C) set in the response my next sending seq.
3702f8829a4aSRandall Stewart 	 */
3703f8829a4aSRandall Stewart 	struct sctp_forward_tsn_chunk fwdtsn;
3704f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3705f8829a4aSRandall Stewart 	int abort_flag = 0;
3706f8829a4aSRandall Stewart 	uint32_t seq;
3707f8829a4aSRandall Stewart 
3708f8829a4aSRandall Stewart 	seq = ntohl(req->request_seq);
3709f8829a4aSRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3710ce228dabSMichael Tuexen 		asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
3711f3ebe64cSMichael Tuexen 		if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3712f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3713ce228dabSMichael Tuexen 		} else {
3714f8829a4aSRandall Stewart 			fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3715f8829a4aSRandall Stewart 			fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3716f8829a4aSRandall Stewart 			fwdtsn.ch.chunk_flags = 0;
3717f8829a4aSRandall Stewart 			fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3718671d309cSRandall Stewart 			sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3719f8829a4aSRandall Stewart 			if (abort_flag) {
3720f8829a4aSRandall Stewart 				return (1);
3721f8829a4aSRandall Stewart 			}
3722f3ebe64cSMichael Tuexen 			asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3723b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3724b3f1ea41SRandall Stewart 				sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3725b3f1ea41SRandall Stewart 			}
3726f3ebe64cSMichael Tuexen 			asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3727f3ebe64cSMichael Tuexen 			asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3728f3ebe64cSMichael Tuexen 			memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3729f3ebe64cSMichael Tuexen 			asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3730f3ebe64cSMichael Tuexen 			memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3731f3ebe64cSMichael Tuexen 			atomic_add_int(&asoc->sending_seq, 1);
3732f8829a4aSRandall Stewart 			/* save off historical data for retrans */
3733f3ebe64cSMichael Tuexen 			asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3734f3ebe64cSMichael Tuexen 			asoc->last_sending_seq[0] = asoc->sending_seq;
3735f3ebe64cSMichael Tuexen 			asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3736f3ebe64cSMichael Tuexen 			asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3737f8829a4aSRandall Stewart 			sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3738f8829a4aSRandall Stewart 			sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3739f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3740f3ebe64cSMichael Tuexen 			sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3741ce228dabSMichael Tuexen 		}
3742ce228dabSMichael Tuexen 		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3743ce228dabSMichael Tuexen 		    asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3744f8829a4aSRandall Stewart 		asoc->str_reset_seq_in++;
3745f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 1 == seq) {
3746f8829a4aSRandall Stewart 		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3747f3ebe64cSMichael Tuexen 		    asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3748f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 2 == seq) {
3749f8829a4aSRandall Stewart 		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3750f3ebe64cSMichael Tuexen 		    asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3751f8829a4aSRandall Stewart 	} else {
3752f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3753f8829a4aSRandall Stewart 	}
3754f8829a4aSRandall Stewart 	return (0);
3755f8829a4aSRandall Stewart }
3756f8829a4aSRandall Stewart 
3757f8829a4aSRandall Stewart static void
3758f8829a4aSRandall Stewart sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3759f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk,
3760671d309cSRandall Stewart     struct sctp_stream_reset_out_request *req, int trunc)
3761f8829a4aSRandall Stewart {
3762f8829a4aSRandall Stewart 	uint32_t seq, tsn;
3763f8829a4aSRandall Stewart 	int number_entries, len;
3764f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3765f8829a4aSRandall Stewart 
3766f8829a4aSRandall Stewart 	seq = ntohl(req->request_seq);
3767f8829a4aSRandall Stewart 
3768f8829a4aSRandall Stewart 	/* now if its not a duplicate we process it */
3769f8829a4aSRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3770f8829a4aSRandall Stewart 		len = ntohs(req->ph.param_length);
3771f8829a4aSRandall Stewart 		number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3772f8829a4aSRandall Stewart 		/*
3773f8829a4aSRandall Stewart 		 * the sender is resetting, handle the list issue.. we must
3774f8829a4aSRandall Stewart 		 * a) verify if we can do the reset, if so no problem b) If
3775f8829a4aSRandall Stewart 		 * we can't do the reset we must copy the request. c) queue
3776f8829a4aSRandall Stewart 		 * it, and setup the data in processor to trigger it off
3777f8829a4aSRandall Stewart 		 * when needed and dequeue all the queued data.
3778f8829a4aSRandall Stewart 		 */
3779f8829a4aSRandall Stewart 		tsn = ntohl(req->send_reset_at_tsn);
3780f8829a4aSRandall Stewart 
3781f8829a4aSRandall Stewart 		/* move the reset action back one */
3782f8829a4aSRandall Stewart 		asoc->last_reset_action[1] = asoc->last_reset_action[0];
3783f3ebe64cSMichael Tuexen 		if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3784f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3785f3ebe64cSMichael Tuexen 		} else if (trunc) {
3786f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
378720b07a4dSMichael Tuexen 		} else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3788f8829a4aSRandall Stewart 			/* we can do it now */
3789f8829a4aSRandall Stewart 			sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3790f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3791f8829a4aSRandall Stewart 		} else {
3792f8829a4aSRandall Stewart 			/*
3793f8829a4aSRandall Stewart 			 * we must queue it up and thus wait for the TSN's
3794f8829a4aSRandall Stewart 			 * to arrive that are at or before tsn
3795f8829a4aSRandall Stewart 			 */
3796f8829a4aSRandall Stewart 			struct sctp_stream_reset_list *liste;
3797f8829a4aSRandall Stewart 			int siz;
3798f8829a4aSRandall Stewart 
3799f8829a4aSRandall Stewart 			siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3800f8829a4aSRandall Stewart 			SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3801207304d4SRandall Stewart 			    siz, SCTP_M_STRESET);
3802f8829a4aSRandall Stewart 			if (liste == NULL) {
3803f8829a4aSRandall Stewart 				/* gak out of memory */
3804f3ebe64cSMichael Tuexen 				asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3805f3ebe64cSMichael Tuexen 				sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3806f8829a4aSRandall Stewart 				return;
3807f8829a4aSRandall Stewart 			}
38087cca1775SRandall Stewart 			liste->seq = seq;
3809f8829a4aSRandall Stewart 			liste->tsn = tsn;
3810f8829a4aSRandall Stewart 			liste->number_entries = number_entries;
3811a169d6ecSMichael Tuexen 			memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t));
3812f8829a4aSRandall Stewart 			TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
38137cca1775SRandall Stewart 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_IN_PROGRESS;
3814f8829a4aSRandall Stewart 		}
3815c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3816f8829a4aSRandall Stewart 		asoc->str_reset_seq_in++;
3817f8829a4aSRandall Stewart 	} else if ((asoc->str_reset_seq_in - 1) == seq) {
3818f8829a4aSRandall Stewart 		/*
3819f8829a4aSRandall Stewart 		 * one seq back, just echo back last action since my
3820f8829a4aSRandall Stewart 		 * response was lost.
3821f8829a4aSRandall Stewart 		 */
3822f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3823f8829a4aSRandall Stewart 	} else if ((asoc->str_reset_seq_in - 2) == seq) {
3824f8829a4aSRandall Stewart 		/*
3825f8829a4aSRandall Stewart 		 * two seq back, just echo back last action since my
3826f8829a4aSRandall Stewart 		 * response was lost.
3827f8829a4aSRandall Stewart 		 */
3828f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3829f8829a4aSRandall Stewart 	} else {
3830f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3831f8829a4aSRandall Stewart 	}
3832f8829a4aSRandall Stewart }
3833f8829a4aSRandall Stewart 
3834ea44232bSRandall Stewart static void
3835ea44232bSRandall Stewart sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3836ea44232bSRandall Stewart     struct sctp_stream_reset_add_strm *str_add)
3837ea44232bSRandall Stewart {
3838ea44232bSRandall Stewart 	/*
3839ea44232bSRandall Stewart 	 * Peer is requesting to add more streams. If its within our
3840ea44232bSRandall Stewart 	 * max-streams we will allow it.
3841ea44232bSRandall Stewart 	 */
3842c4e848b7SRandall Stewart 	uint32_t num_stream, i;
3843ea44232bSRandall Stewart 	uint32_t seq;
38448aae9493SRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
38454a9ef3f8SMichael Tuexen 	struct sctp_queued_to_read *ctl, *nctl;
3846ea44232bSRandall Stewart 
3847ea44232bSRandall Stewart 	/* Get the number. */
3848ea44232bSRandall Stewart 	seq = ntohl(str_add->request_seq);
3849ea44232bSRandall Stewart 	num_stream = ntohs(str_add->number_of_streams);
3850ea44232bSRandall Stewart 	/* Now what would be the new total? */
38518aae9493SRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3852ea44232bSRandall Stewart 		num_stream += stcb->asoc.streamincnt;
3853f3ebe64cSMichael Tuexen 		stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3854f3ebe64cSMichael Tuexen 		if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3855f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3856f3ebe64cSMichael Tuexen 		} else if ((num_stream > stcb->asoc.max_inbound_streams) ||
3857c4e848b7SRandall Stewart 		    (num_stream > 0xffff)) {
3858ea44232bSRandall Stewart 			/* We must reject it they ask for to many */
3859ea44232bSRandall Stewart 	denied:
3860f3ebe64cSMichael Tuexen 			stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3861ea44232bSRandall Stewart 		} else {
3862ea44232bSRandall Stewart 			/* Ok, we can do that :-) */
3863ea44232bSRandall Stewart 			struct sctp_stream_in *oldstrm;
3864ea44232bSRandall Stewart 
3865ea44232bSRandall Stewart 			/* save off the old */
3866ea44232bSRandall Stewart 			oldstrm = stcb->asoc.strmin;
3867ea44232bSRandall Stewart 			SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
3868ea44232bSRandall Stewart 			    (num_stream * sizeof(struct sctp_stream_in)),
3869ea44232bSRandall Stewart 			    SCTP_M_STRMI);
3870ea44232bSRandall Stewart 			if (stcb->asoc.strmin == NULL) {
3871ea44232bSRandall Stewart 				stcb->asoc.strmin = oldstrm;
3872ea44232bSRandall Stewart 				goto denied;
3873ea44232bSRandall Stewart 			}
3874ea44232bSRandall Stewart 			/* copy off the old data */
38758aae9493SRandall Stewart 			for (i = 0; i < stcb->asoc.streamincnt; i++) {
38768aae9493SRandall Stewart 				TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
387744249214SRandall Stewart 				TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
387849656eefSMichael Tuexen 				stcb->asoc.strmin[i].sid = i;
387949656eefSMichael Tuexen 				stcb->asoc.strmin[i].last_mid_delivered = oldstrm[i].last_mid_delivered;
38808aae9493SRandall Stewart 				stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
388144249214SRandall Stewart 				stcb->asoc.strmin[i].pd_api_started = oldstrm[i].pd_api_started;
38828aae9493SRandall Stewart 				/* now anything on those queues? */
388344249214SRandall Stewart 				TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next_instrm, nctl) {
388444249214SRandall Stewart 					TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next_instrm);
388544249214SRandall Stewart 					TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next_instrm);
388644249214SRandall Stewart 				}
388744249214SRandall Stewart 				TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].uno_inqueue, next_instrm, nctl) {
388844249214SRandall Stewart 					TAILQ_REMOVE(&oldstrm[i].uno_inqueue, ctl, next_instrm);
388944249214SRandall Stewart 					TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].uno_inqueue, ctl, next_instrm);
38908aae9493SRandall Stewart 				}
38918aae9493SRandall Stewart 			}
3892ea44232bSRandall Stewart 			/* Init the new streams */
3893ea44232bSRandall Stewart 			for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
3894ea44232bSRandall Stewart 				TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
389544249214SRandall Stewart 				TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
389649656eefSMichael Tuexen 				stcb->asoc.strmin[i].sid = i;
389749656eefSMichael Tuexen 				stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
389844249214SRandall Stewart 				stcb->asoc.strmin[i].pd_api_started = 0;
3899ea44232bSRandall Stewart 				stcb->asoc.strmin[i].delivery_started = 0;
3900ea44232bSRandall Stewart 			}
3901ea44232bSRandall Stewart 			SCTP_FREE(oldstrm, SCTP_M_STRMI);
3902ea44232bSRandall Stewart 			/* update the size */
3903ea44232bSRandall Stewart 			stcb->asoc.streamincnt = num_stream;
3904f3ebe64cSMichael Tuexen 			stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3905c4e848b7SRandall Stewart 			sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3906ea44232bSRandall Stewart 		}
3907c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3908c4e848b7SRandall Stewart 		asoc->str_reset_seq_in++;
39098aae9493SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 1) == seq) {
39108aae9493SRandall Stewart 		/*
39118aae9493SRandall Stewart 		 * one seq back, just echo back last action since my
39128aae9493SRandall Stewart 		 * response was lost.
39138aae9493SRandall Stewart 		 */
39148aae9493SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
39158aae9493SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 2) == seq) {
39168aae9493SRandall Stewart 		/*
39178aae9493SRandall Stewart 		 * two seq back, just echo back last action since my
39188aae9493SRandall Stewart 		 * response was lost.
39198aae9493SRandall Stewart 		 */
39208aae9493SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
39218aae9493SRandall Stewart 	} else {
3922f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
39238aae9493SRandall Stewart 	}
3924ea44232bSRandall Stewart }
3925ea44232bSRandall Stewart 
3926c4e848b7SRandall Stewart static void
3927c4e848b7SRandall Stewart sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3928c4e848b7SRandall Stewart     struct sctp_stream_reset_add_strm *str_add)
3929c4e848b7SRandall Stewart {
3930c4e848b7SRandall Stewart 	/*
3931c4e848b7SRandall Stewart 	 * Peer is requesting to add more streams. If its within our
3932c4e848b7SRandall Stewart 	 * max-streams we will allow it.
3933c4e848b7SRandall Stewart 	 */
3934c4e848b7SRandall Stewart 	uint16_t num_stream;
3935c4e848b7SRandall Stewart 	uint32_t seq;
3936c4e848b7SRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3937c4e848b7SRandall Stewart 
3938c4e848b7SRandall Stewart 	/* Get the number. */
3939c4e848b7SRandall Stewart 	seq = ntohl(str_add->request_seq);
3940c4e848b7SRandall Stewart 	num_stream = ntohs(str_add->number_of_streams);
3941c4e848b7SRandall Stewart 	/* Now what would be the new total? */
3942c4e848b7SRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3943c4e848b7SRandall Stewart 		stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3944f3ebe64cSMichael Tuexen 		if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3945f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3946f3ebe64cSMichael Tuexen 		} else if (stcb->asoc.stream_reset_outstanding) {
3947f3ebe64cSMichael Tuexen 			/* We must reject it we have something pending */
3948f3ebe64cSMichael Tuexen 			stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3949c4e848b7SRandall Stewart 		} else {
3950c4e848b7SRandall Stewart 			/* Ok, we can do that :-) */
3951c4e848b7SRandall Stewart 			int mychk;
3952c4e848b7SRandall Stewart 
3953c4e848b7SRandall Stewart 			mychk = stcb->asoc.streamoutcnt;
3954c4e848b7SRandall Stewart 			mychk += num_stream;
3955c4e848b7SRandall Stewart 			if (mychk < 0x10000) {
3956f3ebe64cSMichael Tuexen 				stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
39577cca1775SRandall Stewart 				if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 1, num_stream, 0, 1)) {
3958f3ebe64cSMichael Tuexen 					stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3959c4e848b7SRandall Stewart 				}
3960c4e848b7SRandall Stewart 			} else {
3961f3ebe64cSMichael Tuexen 				stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3962c4e848b7SRandall Stewart 			}
3963c4e848b7SRandall Stewart 		}
3964c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
3965c4e848b7SRandall Stewart 		asoc->str_reset_seq_in++;
3966c4e848b7SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 1) == seq) {
3967c4e848b7SRandall Stewart 		/*
3968c4e848b7SRandall Stewart 		 * one seq back, just echo back last action since my
3969c4e848b7SRandall Stewart 		 * response was lost.
3970c4e848b7SRandall Stewart 		 */
3971c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3972c4e848b7SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 2) == seq) {
3973c4e848b7SRandall Stewart 		/*
3974c4e848b7SRandall Stewart 		 * two seq back, just echo back last action since my
3975c4e848b7SRandall Stewart 		 * response was lost.
3976c4e848b7SRandall Stewart 		 */
3977c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3978c4e848b7SRandall Stewart 	} else {
3979f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3980c4e848b7SRandall Stewart 	}
3981c4e848b7SRandall Stewart }
3982c4e848b7SRandall Stewart 
3983671d309cSRandall Stewart #ifdef __GNUC__
3984671d309cSRandall Stewart __attribute__((noinline))
3985671d309cSRandall Stewart #endif
3986f8829a4aSRandall Stewart static int
3987671d309cSRandall Stewart sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3988a169d6ecSMichael Tuexen     struct sctp_chunkhdr *ch_req)
3989f8829a4aSRandall Stewart {
39906a58f0e9SXin LI 	uint16_t remaining_length, param_len, ptype;
3991671d309cSRandall Stewart 	struct sctp_paramhdr pstore;
3992671d309cSRandall Stewart 	uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
3993c4e848b7SRandall Stewart 	uint32_t seq = 0;
3994f8829a4aSRandall Stewart 	int num_req = 0;
3995671d309cSRandall Stewart 	int trunc = 0;
3996f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
3997f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
3998f8829a4aSRandall Stewart 	struct sctp_paramhdr *ph;
3999f42a358aSRandall Stewart 	int ret_code = 0;
4000f42a358aSRandall Stewart 	int num_param = 0;
4001f8829a4aSRandall Stewart 
4002f8829a4aSRandall Stewart 	/* now it may be a reset or a reset-response */
40036a58f0e9SXin LI 	remaining_length = ntohs(ch_req->chunk_length) - sizeof(struct sctp_chunkhdr);
4004f8829a4aSRandall Stewart 
4005f8829a4aSRandall Stewart 	/* setup for adding the response */
4006f8829a4aSRandall Stewart 	sctp_alloc_a_chunk(stcb, chk);
4007f8829a4aSRandall Stewart 	if (chk == NULL) {
4008f8829a4aSRandall Stewart 		return (ret_code);
4009f8829a4aSRandall Stewart 	}
4010e03159eaSMichael Tuexen 	chk->copy_by_ref = 0;
4011f8829a4aSRandall Stewart 	chk->rec.chunk_id.id = SCTP_STREAM_RESET;
4012d06c82f1SRandall Stewart 	chk->rec.chunk_id.can_take_data = 0;
4013e03159eaSMichael Tuexen 	chk->flags = 0;
4014f8829a4aSRandall Stewart 	chk->asoc = &stcb->asoc;
4015f8829a4aSRandall Stewart 	chk->no_fr_allowed = 0;
4016f8829a4aSRandall Stewart 	chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
401744b7479bSRandall Stewart 	chk->book_size_scale = 0;
4018eb1b1807SGleb Smirnoff 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
4019f8829a4aSRandall Stewart 	if (chk->data == NULL) {
4020f8829a4aSRandall Stewart strres_nochunk:
4021f8829a4aSRandall Stewart 		if (chk->data) {
4022f8829a4aSRandall Stewart 			sctp_m_freem(chk->data);
4023f8829a4aSRandall Stewart 			chk->data = NULL;
4024f8829a4aSRandall Stewart 		}
4025689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4026f8829a4aSRandall Stewart 		return (ret_code);
4027f8829a4aSRandall Stewart 	}
4028139bc87fSRandall Stewart 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4029f8829a4aSRandall Stewart 
4030f8829a4aSRandall Stewart 	/* setup chunk parameters */
4031f8829a4aSRandall Stewart 	chk->sent = SCTP_DATAGRAM_UNSENT;
4032f8829a4aSRandall Stewart 	chk->snd_count = 0;
4033ca85e948SMichael Tuexen 	chk->whoTo = NULL;
4034f8829a4aSRandall Stewart 
4035f8829a4aSRandall Stewart 	ch = mtod(chk->data, struct sctp_chunkhdr *);
4036f8829a4aSRandall Stewart 	ch->chunk_type = SCTP_STREAM_RESET;
4037f8829a4aSRandall Stewart 	ch->chunk_flags = 0;
4038f8829a4aSRandall Stewart 	ch->chunk_length = htons(chk->send_size);
4039139bc87fSRandall Stewart 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4040671d309cSRandall Stewart 	offset += sizeof(struct sctp_chunkhdr);
40416a58f0e9SXin LI 	while (remaining_length >= sizeof(struct sctp_paramhdr)) {
4042671d309cSRandall Stewart 		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *)&pstore);
40436a58f0e9SXin LI 		if (ph == NULL) {
40446a58f0e9SXin LI 			/* TSNH */
4045f8829a4aSRandall Stewart 			break;
4046f8829a4aSRandall Stewart 		}
40476a58f0e9SXin LI 		param_len = ntohs(ph->param_length);
40486a58f0e9SXin LI 		if ((param_len > remaining_length) ||
40496a58f0e9SXin LI 		    (param_len < (sizeof(struct sctp_paramhdr) + sizeof(uint32_t)))) {
40506a58f0e9SXin LI 			/* bad parameter length */
40516a58f0e9SXin LI 			break;
40526a58f0e9SXin LI 		}
40536a58f0e9SXin LI 		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, sizeof(cstore)),
4054671d309cSRandall Stewart 		    (uint8_t *)&cstore);
40556a58f0e9SXin LI 		if (ph == NULL) {
40566a58f0e9SXin LI 			/* TSNH */
40576a58f0e9SXin LI 			break;
40586a58f0e9SXin LI 		}
4059f8829a4aSRandall Stewart 		ptype = ntohs(ph->param_type);
4060f8829a4aSRandall Stewart 		num_param++;
40616a58f0e9SXin LI 		if (param_len > sizeof(cstore)) {
4062671d309cSRandall Stewart 			trunc = 1;
4063671d309cSRandall Stewart 		} else {
4064671d309cSRandall Stewart 			trunc = 0;
4065671d309cSRandall Stewart 		}
4066f8829a4aSRandall Stewart 		if (num_param > SCTP_MAX_RESET_PARAMS) {
4067f8829a4aSRandall Stewart 			/* hit the max of parameters already sorry.. */
4068f8829a4aSRandall Stewart 			break;
4069f8829a4aSRandall Stewart 		}
4070f8829a4aSRandall Stewart 		if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4071f8829a4aSRandall Stewart 			struct sctp_stream_reset_out_request *req_out;
4072f8829a4aSRandall Stewart 
40736a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_out_request)) {
40746a58f0e9SXin LI 				break;
40756a58f0e9SXin LI 			}
4076f8829a4aSRandall Stewart 			req_out = (struct sctp_stream_reset_out_request *)ph;
4077f8829a4aSRandall Stewart 			num_req++;
4078f8829a4aSRandall Stewart 			if (stcb->asoc.stream_reset_outstanding) {
4079f8829a4aSRandall Stewart 				seq = ntohl(req_out->response_seq);
4080f8829a4aSRandall Stewart 				if (seq == stcb->asoc.str_reset_seq_out) {
4081f8829a4aSRandall Stewart 					/* implicit ack */
4082f3ebe64cSMichael Tuexen 					(void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4083f8829a4aSRandall Stewart 				}
4084f8829a4aSRandall Stewart 			}
4085671d309cSRandall Stewart 			sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4086c4e848b7SRandall Stewart 		} else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4087ea44232bSRandall Stewart 			struct sctp_stream_reset_add_strm *str_add;
4088ea44232bSRandall Stewart 
40896a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
40906a58f0e9SXin LI 				break;
40916a58f0e9SXin LI 			}
4092ea44232bSRandall Stewart 			str_add = (struct sctp_stream_reset_add_strm *)ph;
4093ea44232bSRandall Stewart 			num_req++;
4094ea44232bSRandall Stewart 			sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4095c4e848b7SRandall Stewart 		} else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4096c4e848b7SRandall Stewart 			struct sctp_stream_reset_add_strm *str_add;
4097c4e848b7SRandall Stewart 
40986a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
40996a58f0e9SXin LI 				break;
41006a58f0e9SXin LI 			}
4101c4e848b7SRandall Stewart 			str_add = (struct sctp_stream_reset_add_strm *)ph;
4102c4e848b7SRandall Stewart 			num_req++;
4103c4e848b7SRandall Stewart 			sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4104f8829a4aSRandall Stewart 		} else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4105f8829a4aSRandall Stewart 			struct sctp_stream_reset_in_request *req_in;
4106f8829a4aSRandall Stewart 
4107f8829a4aSRandall Stewart 			num_req++;
4108f8829a4aSRandall Stewart 			req_in = (struct sctp_stream_reset_in_request *)ph;
4109671d309cSRandall Stewart 			sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4110f8829a4aSRandall Stewart 		} else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4111f8829a4aSRandall Stewart 			struct sctp_stream_reset_tsn_request *req_tsn;
4112f8829a4aSRandall Stewart 
4113f8829a4aSRandall Stewart 			num_req++;
4114f8829a4aSRandall Stewart 			req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4115f8829a4aSRandall Stewart 			if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4116f8829a4aSRandall Stewart 				ret_code = 1;
4117f8829a4aSRandall Stewart 				goto strres_nochunk;
4118f8829a4aSRandall Stewart 			}
4119f8829a4aSRandall Stewart 			/* no more */
4120f8829a4aSRandall Stewart 			break;
4121f8829a4aSRandall Stewart 		} else if (ptype == SCTP_STR_RESET_RESPONSE) {
4122f8829a4aSRandall Stewart 			struct sctp_stream_reset_response *resp;
4123f8829a4aSRandall Stewart 			uint32_t result;
4124f8829a4aSRandall Stewart 
41256a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_response)) {
41266a58f0e9SXin LI 				break;
41276a58f0e9SXin LI 			}
4128f8829a4aSRandall Stewart 			resp = (struct sctp_stream_reset_response *)ph;
4129f8829a4aSRandall Stewart 			seq = ntohl(resp->response_seq);
4130f8829a4aSRandall Stewart 			result = ntohl(resp->result);
4131f8829a4aSRandall Stewart 			if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4132f8829a4aSRandall Stewart 				ret_code = 1;
4133f8829a4aSRandall Stewart 				goto strres_nochunk;
4134f8829a4aSRandall Stewart 			}
4135f8829a4aSRandall Stewart 		} else {
4136f8829a4aSRandall Stewart 			break;
4137f8829a4aSRandall Stewart 		}
4138671d309cSRandall Stewart 		offset += SCTP_SIZE32(param_len);
41396a58f0e9SXin LI 		if (remaining_length >= SCTP_SIZE32(param_len)) {
41406a58f0e9SXin LI 			remaining_length -= SCTP_SIZE32(param_len);
41416a58f0e9SXin LI 		} else {
41426a58f0e9SXin LI 			remaining_length = 0;
41436a58f0e9SXin LI 		}
4144f8829a4aSRandall Stewart 	}
4145f8829a4aSRandall Stewart 	if (num_req == 0) {
4146f8829a4aSRandall Stewart 		/* we have no response free the stuff */
4147f8829a4aSRandall Stewart 		goto strres_nochunk;
4148f8829a4aSRandall Stewart 	}
4149f8829a4aSRandall Stewart 	/* ok we have a chunk to link in */
4150f8829a4aSRandall Stewart 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4151f8829a4aSRandall Stewart 	    chk,
4152f8829a4aSRandall Stewart 	    sctp_next);
4153f8829a4aSRandall Stewart 	stcb->asoc.ctrl_queue_cnt++;
4154f8829a4aSRandall Stewart 	return (ret_code);
4155f8829a4aSRandall Stewart }
4156f8829a4aSRandall Stewart 
4157f8829a4aSRandall Stewart /*
4158f8829a4aSRandall Stewart  * Handle a router or endpoints report of a packet loss, there are two ways
4159f8829a4aSRandall Stewart  * to handle this, either we get the whole packet and must disect it
4160f8829a4aSRandall Stewart  * ourselves (possibly with truncation and or corruption) or it is a summary
4161f8829a4aSRandall Stewart  * from a middle box that did the disectting for us.
4162f8829a4aSRandall Stewart  */
4163f8829a4aSRandall Stewart static void
4164f8829a4aSRandall Stewart sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4165458303daSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4166f8829a4aSRandall Stewart {
4167f8829a4aSRandall Stewart 	struct sctp_chunk_desc desc;
416832df1c9eSMichael Tuexen 	struct sctp_chunkhdr *chk_hdr;
416932df1c9eSMichael Tuexen 	struct sctp_data_chunk *data_chunk;
417032df1c9eSMichael Tuexen 	struct sctp_idata_chunk *idata_chunk;
417132df1c9eSMichael Tuexen 	uint32_t bottle_bw, on_queue;
417232df1c9eSMichael Tuexen 	uint32_t offset, chk_len;
417332df1c9eSMichael Tuexen 	uint16_t pktdrp_len;
417432df1c9eSMichael Tuexen 	uint8_t pktdrp_flags;
4175f8829a4aSRandall Stewart 
417632df1c9eSMichael Tuexen 	KASSERT(sizeof(struct sctp_pktdrop_chunk) <= limit,
417732df1c9eSMichael Tuexen 	    ("PKTDROP chunk too small"));
417832df1c9eSMichael Tuexen 	pktdrp_flags = cp->ch.chunk_flags;
417932df1c9eSMichael Tuexen 	pktdrp_len = ntohs(cp->ch.chunk_length);
418032df1c9eSMichael Tuexen 	KASSERT(limit <= pktdrp_len, ("Inconsistent limit"));
418132df1c9eSMichael Tuexen 	if (pktdrp_flags & SCTP_PACKET_TRUNCATED) {
41826176f9d6SMichael Tuexen 		if (ntohs(cp->trunc_len) <= pktdrp_len - sizeof(struct sctp_pktdrop_chunk)) {
418332df1c9eSMichael Tuexen 			/* The peer plays games with us. */
418432df1c9eSMichael Tuexen 			return;
418532df1c9eSMichael Tuexen 		}
418632df1c9eSMichael Tuexen 	}
418732df1c9eSMichael Tuexen 	limit -= sizeof(struct sctp_pktdrop_chunk);
418832df1c9eSMichael Tuexen 	offset = 0;
418932df1c9eSMichael Tuexen 	if (offset == limit) {
419032df1c9eSMichael Tuexen 		if (pktdrp_flags & SCTP_FROM_MIDDLE_BOX) {
4191f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_pdrpbwrpt);
419232df1c9eSMichael Tuexen 		}
419332df1c9eSMichael Tuexen 	} else if (offset + sizeof(struct sctphdr) > limit) {
419432df1c9eSMichael Tuexen 		/* Only a partial SCTP common header. */
419532df1c9eSMichael Tuexen 		SCTP_STAT_INCR(sctps_pdrpcrupt);
419632df1c9eSMichael Tuexen 		offset = limit;
4197f8829a4aSRandall Stewart 	} else {
419832df1c9eSMichael Tuexen 		/* XXX: Check embedded SCTP common header. */
419932df1c9eSMichael Tuexen 		offset += sizeof(struct sctphdr);
4200f8829a4aSRandall Stewart 	}
420132df1c9eSMichael Tuexen 	/* Now parse through the chunks themselves. */
420232df1c9eSMichael Tuexen 	while (offset < limit) {
420332df1c9eSMichael Tuexen 		if (offset + sizeof(struct sctp_chunkhdr) > limit) {
420432df1c9eSMichael Tuexen 			SCTP_STAT_INCR(sctps_pdrpcrupt);
420532df1c9eSMichael Tuexen 			break;
4206458303daSRandall Stewart 		}
420732df1c9eSMichael Tuexen 		chk_hdr = (struct sctp_chunkhdr *)(cp->data + offset);
420832df1c9eSMichael Tuexen 		desc.chunk_type = chk_hdr->chunk_type;
4209f8829a4aSRandall Stewart 		/* get amount we need to move */
421032df1c9eSMichael Tuexen 		chk_len = (uint32_t)ntohs(chk_hdr->chunk_length);
421132df1c9eSMichael Tuexen 		if (chk_len < sizeof(struct sctp_chunkhdr)) {
421232df1c9eSMichael Tuexen 			/* Someone is lying... */
4213f8829a4aSRandall Stewart 			break;
4214f8829a4aSRandall Stewart 		}
4215f8829a4aSRandall Stewart 		if (desc.chunk_type == SCTP_DATA) {
421632df1c9eSMichael Tuexen 			if (stcb->asoc.idata_supported) {
421732df1c9eSMichael Tuexen 				/* Some is playing games with us. */
421832df1c9eSMichael Tuexen 				break;
4219f8829a4aSRandall Stewart 			}
422032df1c9eSMichael Tuexen 			if (chk_len <= sizeof(struct sctp_data_chunk)) {
422132df1c9eSMichael Tuexen 				/* Some is playing games with us. */
422232df1c9eSMichael Tuexen 				break;
422332df1c9eSMichael Tuexen 			}
422432df1c9eSMichael Tuexen 			if (chk_len < sizeof(struct sctp_data_chunk) + SCTP_NUM_DB_TO_VERIFY) {
422532df1c9eSMichael Tuexen 				/*
422632df1c9eSMichael Tuexen 				 * Not enough data bytes available in the
422732df1c9eSMichael Tuexen 				 * chunk.
422832df1c9eSMichael Tuexen 				 */
4229f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpnedat);
423032df1c9eSMichael Tuexen 				goto next_chunk;
423132df1c9eSMichael Tuexen 			}
423232df1c9eSMichael Tuexen 			if (offset + sizeof(struct sctp_data_chunk) + SCTP_NUM_DB_TO_VERIFY > limit) {
423332df1c9eSMichael Tuexen 				/* Not enough data in buffer. */
4234f8829a4aSRandall Stewart 				break;
4235f8829a4aSRandall Stewart 			}
423632df1c9eSMichael Tuexen 			data_chunk = (struct sctp_data_chunk *)(cp->data + offset);
423732df1c9eSMichael Tuexen 			memcpy(desc.data_bytes, data_chunk + 1, SCTP_NUM_DB_TO_VERIFY);
423832df1c9eSMichael Tuexen 			desc.tsn_ifany = data_chunk->dp.tsn;
423932df1c9eSMichael Tuexen 			if (pktdrp_flags & SCTP_FROM_MIDDLE_BOX) {
424032df1c9eSMichael Tuexen 				SCTP_STAT_INCR(sctps_pdrpmbda);
424132df1c9eSMichael Tuexen 			}
424232df1c9eSMichael Tuexen 		} else if (desc.chunk_type == SCTP_IDATA) {
424332df1c9eSMichael Tuexen 			if (!stcb->asoc.idata_supported) {
424432df1c9eSMichael Tuexen 				/* Some is playing games with us. */
424532df1c9eSMichael Tuexen 				break;
424632df1c9eSMichael Tuexen 			}
424732df1c9eSMichael Tuexen 			if (chk_len <= sizeof(struct sctp_idata_chunk)) {
424832df1c9eSMichael Tuexen 				/* Some is playing games with us. */
424932df1c9eSMichael Tuexen 				break;
425032df1c9eSMichael Tuexen 			}
425132df1c9eSMichael Tuexen 			if (chk_len < sizeof(struct sctp_idata_chunk) + SCTP_NUM_DB_TO_VERIFY) {
425232df1c9eSMichael Tuexen 				/*
425332df1c9eSMichael Tuexen 				 * Not enough data bytes available in the
425432df1c9eSMichael Tuexen 				 * chunk.
425532df1c9eSMichael Tuexen 				 */
425632df1c9eSMichael Tuexen 				SCTP_STAT_INCR(sctps_pdrpnedat);
425732df1c9eSMichael Tuexen 				goto next_chunk;
425832df1c9eSMichael Tuexen 			}
425932df1c9eSMichael Tuexen 			if (offset + sizeof(struct sctp_idata_chunk) + SCTP_NUM_DB_TO_VERIFY > limit) {
426032df1c9eSMichael Tuexen 				/* Not enough data in buffer. */
426132df1c9eSMichael Tuexen 				break;
426232df1c9eSMichael Tuexen 			}
426332df1c9eSMichael Tuexen 			idata_chunk = (struct sctp_idata_chunk *)(cp->data + offset);
426432df1c9eSMichael Tuexen 			memcpy(desc.data_bytes, idata_chunk + 1, SCTP_NUM_DB_TO_VERIFY);
426532df1c9eSMichael Tuexen 			desc.tsn_ifany = idata_chunk->dp.tsn;
426632df1c9eSMichael Tuexen 			if (pktdrp_flags & SCTP_FROM_MIDDLE_BOX) {
426732df1c9eSMichael Tuexen 				SCTP_STAT_INCR(sctps_pdrpmbda);
426832df1c9eSMichael Tuexen 			}
4269f8829a4aSRandall Stewart 		} else {
427032df1c9eSMichael Tuexen 			if (pktdrp_flags & SCTP_FROM_MIDDLE_BOX) {
4271f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpmbct);
4272f8829a4aSRandall Stewart 			}
427332df1c9eSMichael Tuexen 		}
427432df1c9eSMichael Tuexen 		if (process_chunk_drop(stcb, &desc, net, pktdrp_flags)) {
4275f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_pdrppdbrk);
4276f8829a4aSRandall Stewart 			break;
4277f8829a4aSRandall Stewart 		}
427832df1c9eSMichael Tuexen next_chunk:
427932df1c9eSMichael Tuexen 		offset += SCTP_SIZE32(chk_len);
4280f8829a4aSRandall Stewart 	}
4281f8829a4aSRandall Stewart 	/* Now update any rwnd --- possibly */
428232df1c9eSMichael Tuexen 	if ((pktdrp_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4283f8829a4aSRandall Stewart 		/* From a peer, we get a rwnd report */
4284f8829a4aSRandall Stewart 		uint32_t a_rwnd;
4285f8829a4aSRandall Stewart 
4286f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_pdrpfehos);
4287f8829a4aSRandall Stewart 
4288f8829a4aSRandall Stewart 		bottle_bw = ntohl(cp->bottle_bw);
4289f8829a4aSRandall Stewart 		on_queue = ntohl(cp->current_onq);
4290f8829a4aSRandall Stewart 		if (bottle_bw && on_queue) {
4291f8829a4aSRandall Stewart 			/* a rwnd report is in here */
4292f8829a4aSRandall Stewart 			if (bottle_bw > on_queue)
4293f8829a4aSRandall Stewart 				a_rwnd = bottle_bw - on_queue;
4294f8829a4aSRandall Stewart 			else
4295f8829a4aSRandall Stewart 				a_rwnd = 0;
4296f8829a4aSRandall Stewart 
4297f8829a4aSRandall Stewart 			if (a_rwnd == 0)
4298f8829a4aSRandall Stewart 				stcb->asoc.peers_rwnd = 0;
4299f8829a4aSRandall Stewart 			else {
4300f8829a4aSRandall Stewart 				if (a_rwnd > stcb->asoc.total_flight) {
4301f8829a4aSRandall Stewart 					stcb->asoc.peers_rwnd =
4302f8829a4aSRandall Stewart 					    a_rwnd - stcb->asoc.total_flight;
4303f8829a4aSRandall Stewart 				} else {
4304f8829a4aSRandall Stewart 					stcb->asoc.peers_rwnd = 0;
4305f8829a4aSRandall Stewart 				}
4306f8829a4aSRandall Stewart 				if (stcb->asoc.peers_rwnd <
4307f8829a4aSRandall Stewart 				    stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4308f8829a4aSRandall Stewart 					/* SWS sender side engages */
4309f8829a4aSRandall Stewart 					stcb->asoc.peers_rwnd = 0;
4310f8829a4aSRandall Stewart 				}
4311f8829a4aSRandall Stewart 			}
4312f8829a4aSRandall Stewart 		}
4313f8829a4aSRandall Stewart 	} else {
4314f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_pdrpfmbox);
4315f8829a4aSRandall Stewart 	}
4316f8829a4aSRandall Stewart 
4317f8829a4aSRandall Stewart 	/* now middle boxes in sat networks get a cwnd bump */
431832df1c9eSMichael Tuexen 	if ((pktdrp_flags & SCTP_FROM_MIDDLE_BOX) &&
4319f8829a4aSRandall Stewart 	    (stcb->asoc.sat_t3_loss_recovery == 0) &&
4320f8829a4aSRandall Stewart 	    (stcb->asoc.sat_network)) {
4321f8829a4aSRandall Stewart 		/*
4322cd0a4ff6SPedro F. Giffuni 		 * This is debatable but for sat networks it makes sense
4323f8829a4aSRandall Stewart 		 * Note if a T3 timer has went off, we will prohibit any
4324f8829a4aSRandall Stewart 		 * changes to cwnd until we exit the t3 loss recovery.
4325f8829a4aSRandall Stewart 		 */
4326b54d3a6cSRandall Stewart 		stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4327b54d3a6cSRandall Stewart 		    net, cp, &bottle_bw, &on_queue);
4328f8829a4aSRandall Stewart 	}
4329f8829a4aSRandall Stewart }
4330f8829a4aSRandall Stewart 
4331f8829a4aSRandall Stewart /*
4332f8829a4aSRandall Stewart  * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4333f8829a4aSRandall Stewart  * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4334f8829a4aSRandall Stewart  * offset: offset into the mbuf chain to first chunkhdr - length: is the
4335f8829a4aSRandall Stewart  * length of the complete packet outputs: - length: modified to remaining
4336f8829a4aSRandall Stewart  * length after control processing - netp: modified to new sctp_nets after
4337f8829a4aSRandall Stewart  * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4338f8829a4aSRandall Stewart  * bad packet,...) otherwise return the tcb for this packet
4339f8829a4aSRandall Stewart  */
43403c6f3536SRandall Stewart #ifdef __GNUC__
43413c6f3536SRandall Stewart __attribute__((noinline))
43423c6f3536SRandall Stewart #endif
4343f8829a4aSRandall Stewart static struct sctp_tcb *
4344f8829a4aSRandall Stewart sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4345b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
4346f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
434717205eccSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4348d089f9b9SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4349c54a18d2SRandall Stewart     uint32_t vrf_id, uint16_t port)
4350f8829a4aSRandall Stewart {
4351f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4352ff1ffd74SMichael Tuexen 	struct mbuf *op_err;
4353ff1ffd74SMichael Tuexen 	char msg[SCTP_DIAG_INFO_LEN];
4354f8829a4aSRandall Stewart 	uint32_t vtag_in;
4355f8829a4aSRandall Stewart 	int num_chunks = 0;	/* number of control chunks processed */
4356d0f6ab79SMichael Tuexen 	uint32_t chk_length, contiguous;
4357f8829a4aSRandall Stewart 	int ret;
4358bff64a4dSRandall Stewart 	int abort_no_unlock = 0;
4359899288aeSRandall Stewart 	int ecne_seen = 0;
43609de7354bSMichael Tuexen 	int abort_flag;
4361f8829a4aSRandall Stewart 
4362f8829a4aSRandall Stewart 	/*
4363f8829a4aSRandall Stewart 	 * How big should this be, and should it be alloc'd? Lets try the
4364f8829a4aSRandall Stewart 	 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4365f8829a4aSRandall Stewart 	 * until we get into jumbo grams and such..
4366f8829a4aSRandall Stewart 	 */
4367f42a358aSRandall Stewart 	uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4368f8829a4aSRandall Stewart 	int got_auth = 0;
4369f8829a4aSRandall Stewart 	uint32_t auth_offset = 0, auth_len = 0;
4370f8829a4aSRandall Stewart 	int auth_skipped = 0;
43712afb3e84SRandall Stewart 	int asconf_cnt = 0;
4372ceaad40aSRandall Stewart 
4373ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4374dd294dceSMichael Tuexen 	    iphlen, *offset, length, (void *)stcb);
4375f8829a4aSRandall Stewart 
437680a2d140SMichael Tuexen 	if (stcb) {
437780a2d140SMichael Tuexen 		SCTP_TCB_LOCK_ASSERT(stcb);
437880a2d140SMichael Tuexen 	}
4379f8829a4aSRandall Stewart 	/* validate chunk header length... */
4380f8829a4aSRandall Stewart 	if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4381d61a0ae0SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4382d61a0ae0SRandall Stewart 		    ntohs(ch->chunk_length));
43833ed8d364SMichael Tuexen 		*offset = length;
438480a2d140SMichael Tuexen 		return (stcb);
4385f8829a4aSRandall Stewart 	}
4386f8829a4aSRandall Stewart 	/*
4387f8829a4aSRandall Stewart 	 * validate the verification tag
4388f8829a4aSRandall Stewart 	 */
4389f8829a4aSRandall Stewart 	vtag_in = ntohl(sh->v_tag);
4390f8829a4aSRandall Stewart 
4391f8829a4aSRandall Stewart 	if (ch->chunk_type == SCTP_INITIATION) {
4392d61a0ae0SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4393d61a0ae0SRandall Stewart 		    ntohs(ch->chunk_length), vtag_in);
4394f8829a4aSRandall Stewart 		if (vtag_in != 0) {
4395f8829a4aSRandall Stewart 			/* protocol error- silently discard... */
4396f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_badvtag);
439780a2d140SMichael Tuexen 			if (stcb != NULL) {
439880a2d140SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
43996e55db54SRandall Stewart 			}
4400f8829a4aSRandall Stewart 			return (NULL);
4401f8829a4aSRandall Stewart 		}
4402f8829a4aSRandall Stewart 	} else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4403f8829a4aSRandall Stewart 		/*
4404f8829a4aSRandall Stewart 		 * If there is no stcb, skip the AUTH chunk and process
4405f8829a4aSRandall Stewart 		 * later after a stcb is found (to validate the lookup was
4406f8829a4aSRandall Stewart 		 * valid.
4407f8829a4aSRandall Stewart 		 */
4408f8829a4aSRandall Stewart 		if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4409b3f1ea41SRandall Stewart 		    (stcb == NULL) &&
4410c79bec9cSMichael Tuexen 		    (inp->auth_supported == 1)) {
4411f8829a4aSRandall Stewart 			/* save this chunk for later processing */
4412f8829a4aSRandall Stewart 			auth_skipped = 1;
4413f8829a4aSRandall Stewart 			auth_offset = *offset;
4414f8829a4aSRandall Stewart 			auth_len = ntohs(ch->chunk_length);
4415f8829a4aSRandall Stewart 
4416f8829a4aSRandall Stewart 			/* (temporarily) move past this chunk */
4417f8829a4aSRandall Stewart 			*offset += SCTP_SIZE32(auth_len);
4418f8829a4aSRandall Stewart 			if (*offset >= length) {
4419f8829a4aSRandall Stewart 				/* no more data left in the mbuf chain */
4420f8829a4aSRandall Stewart 				*offset = length;
4421f8829a4aSRandall Stewart 				return (NULL);
4422f8829a4aSRandall Stewart 			}
4423f8829a4aSRandall Stewart 			ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4424f8829a4aSRandall Stewart 			    sizeof(struct sctp_chunkhdr), chunk_buf);
4425f8829a4aSRandall Stewart 		}
4426ad81507eSRandall Stewart 		if (ch == NULL) {
4427ad81507eSRandall Stewart 			/* Help */
4428ad81507eSRandall Stewart 			*offset = length;
442980a2d140SMichael Tuexen 			return (stcb);
4430ad81507eSRandall Stewart 		}
4431f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4432f8829a4aSRandall Stewart 			goto process_control_chunks;
4433f8829a4aSRandall Stewart 		}
4434f8829a4aSRandall Stewart 		/*
4435f8829a4aSRandall Stewart 		 * first check if it's an ASCONF with an unknown src addr we
4436f8829a4aSRandall Stewart 		 * need to look inside to find the association
4437f8829a4aSRandall Stewart 		 */
4438f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
44392afb3e84SRandall Stewart 			struct sctp_chunkhdr *asconf_ch = ch;
44402afb3e84SRandall Stewart 			uint32_t asconf_offset = 0, asconf_len = 0;
44412afb3e84SRandall Stewart 
4442f8829a4aSRandall Stewart 			/* inp's refcount may be reduced */
4443f8829a4aSRandall Stewart 			SCTP_INP_INCR_REF(inp);
4444f8829a4aSRandall Stewart 
44452afb3e84SRandall Stewart 			asconf_offset = *offset;
44462afb3e84SRandall Stewart 			do {
44472afb3e84SRandall Stewart 				asconf_len = ntohs(asconf_ch->chunk_length);
44482afb3e84SRandall Stewart 				if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
44492afb3e84SRandall Stewart 					break;
44507215cc1bSMichael Tuexen 				stcb = sctp_findassociation_ep_asconf(m,
4451b1754ad1SMichael Tuexen 				    *offset,
4452b1754ad1SMichael Tuexen 				    dst,
4453b1754ad1SMichael Tuexen 				    sh, &inp, netp, vrf_id);
44542afb3e84SRandall Stewart 				if (stcb != NULL)
44552afb3e84SRandall Stewart 					break;
44562afb3e84SRandall Stewart 				asconf_offset += SCTP_SIZE32(asconf_len);
44572afb3e84SRandall Stewart 				asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
44582afb3e84SRandall Stewart 				    sizeof(struct sctp_chunkhdr), chunk_buf);
44592afb3e84SRandall Stewart 			} while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4460f8829a4aSRandall Stewart 			if (stcb == NULL) {
4461f8829a4aSRandall Stewart 				/*
4462f8829a4aSRandall Stewart 				 * reduce inp's refcount if not reduced in
4463f8829a4aSRandall Stewart 				 * sctp_findassociation_ep_asconf().
4464f8829a4aSRandall Stewart 				 */
4465f8829a4aSRandall Stewart 				SCTP_INP_DECR_REF(inp);
4466f8829a4aSRandall Stewart 			}
44670053ed28SMichael Tuexen 
4468f8829a4aSRandall Stewart 			/* now go back and verify any auth chunk to be sure */
4469f8829a4aSRandall Stewart 			if (auth_skipped && (stcb != NULL)) {
4470f8829a4aSRandall Stewart 				struct sctp_auth_chunk *auth;
4471f8829a4aSRandall Stewart 
44728262311cSMichael Tuexen 				if (auth_len <= SCTP_CHUNK_BUFFER_SIZE) {
44738262311cSMichael Tuexen 					auth = (struct sctp_auth_chunk *)sctp_m_getptr(m, auth_offset, auth_len, chunk_buf);
4474f8829a4aSRandall Stewart 					got_auth = 1;
4475f8829a4aSRandall Stewart 					auth_skipped = 0;
44768262311cSMichael Tuexen 				} else {
44778262311cSMichael Tuexen 					auth = NULL;
44788262311cSMichael Tuexen 				}
4479ad81507eSRandall Stewart 				if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4480f8829a4aSRandall Stewart 				    auth_offset)) {
4481f8829a4aSRandall Stewart 					/* auth HMAC failed so dump it */
4482f8829a4aSRandall Stewart 					*offset = length;
448380a2d140SMichael Tuexen 					return (stcb);
4484f8829a4aSRandall Stewart 				} else {
4485f8829a4aSRandall Stewart 					/* remaining chunks are HMAC checked */
4486f8829a4aSRandall Stewart 					stcb->asoc.authenticated = 1;
4487f8829a4aSRandall Stewart 				}
4488f8829a4aSRandall Stewart 			}
4489f8829a4aSRandall Stewart 		}
4490f8829a4aSRandall Stewart 		if (stcb == NULL) {
4491999f86d6SMichael Tuexen 			SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4492ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4493ff1ffd74SMichael Tuexen 			    msg);
4494f8829a4aSRandall Stewart 			/* no association, so it's out of the blue... */
4495ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp, op_err,
4496d089f9b9SMichael Tuexen 			    mflowtype, mflowid, inp->fibnum,
4497c54a18d2SRandall Stewart 			    vrf_id, port);
4498f8829a4aSRandall Stewart 			*offset = length;
4499f8829a4aSRandall Stewart 			return (NULL);
4500f8829a4aSRandall Stewart 		}
4501f8829a4aSRandall Stewart 		asoc = &stcb->asoc;
4502f8829a4aSRandall Stewart 		/* ABORT and SHUTDOWN can use either v_tag... */
4503f8829a4aSRandall Stewart 		if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4504f8829a4aSRandall Stewart 		    (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4505f8829a4aSRandall Stewart 		    (ch->chunk_type == SCTP_PACKET_DROPPED)) {
45065db47b3dSMichael Tuexen 			/* Take the T-bit always into account. */
45075db47b3dSMichael Tuexen 			if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) &&
45085db47b3dSMichael Tuexen 			    (vtag_in == asoc->my_vtag)) ||
45095db47b3dSMichael Tuexen 			    (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) &&
4510ff76c8c9SMichael Tuexen 			    (asoc->peer_vtag != htonl(0)) &&
4511f8829a4aSRandall Stewart 			    (vtag_in == asoc->peer_vtag))) {
4512f8829a4aSRandall Stewart 				/* this is valid */
4513f8829a4aSRandall Stewart 			} else {
4514f8829a4aSRandall Stewart 				/* drop this packet... */
4515f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_badvtag);
451680a2d140SMichael Tuexen 				if (stcb != NULL) {
451780a2d140SMichael Tuexen 					SCTP_TCB_UNLOCK(stcb);
45186e55db54SRandall Stewart 				}
4519f8829a4aSRandall Stewart 				return (NULL);
4520f8829a4aSRandall Stewart 			}
4521f8829a4aSRandall Stewart 		} else {
4522f8829a4aSRandall Stewart 			/* for all other chunks, vtag must match */
4523f8829a4aSRandall Stewart 			if (vtag_in != asoc->my_vtag) {
4524f8829a4aSRandall Stewart 				/* invalid vtag... */
4525ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INPUT3,
4526ad81507eSRandall Stewart 				    "invalid vtag: %xh, expect %xh\n",
4527ad81507eSRandall Stewart 				    vtag_in, asoc->my_vtag);
4528f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_badvtag);
452980a2d140SMichael Tuexen 				if (stcb != NULL) {
453080a2d140SMichael Tuexen 					SCTP_TCB_UNLOCK(stcb);
45316e55db54SRandall Stewart 				}
4532f8829a4aSRandall Stewart 				*offset = length;
4533f8829a4aSRandall Stewart 				return (NULL);
4534f8829a4aSRandall Stewart 			}
4535f8829a4aSRandall Stewart 		}
4536b7b84c0eSMichael Tuexen 	}			/* end if !SCTP_COOKIE_ECHO */
4537b7b84c0eSMichael Tuexen 	/*
4538b7b84c0eSMichael Tuexen 	 * process all control chunks...
4539b7b84c0eSMichael Tuexen 	 */
4540f8829a4aSRandall Stewart 	if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4541830d754dSRandall Stewart 	    (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4542f8829a4aSRandall Stewart 	    (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4543839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
4544f8829a4aSRandall Stewart 		/* implied cookie-ack.. we must have lost the ack */
4545f8829a4aSRandall Stewart 		sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4546f8829a4aSRandall Stewart 		    *netp);
4547f8829a4aSRandall Stewart 	}
45480053ed28SMichael Tuexen 
4549f8829a4aSRandall Stewart process_control_chunks:
4550f8829a4aSRandall Stewart 	while (IS_SCTP_CONTROL(ch)) {
4551f8829a4aSRandall Stewart 		/* validate chunk length */
4552f8829a4aSRandall Stewart 		chk_length = ntohs(ch->chunk_length);
4553ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4554f8829a4aSRandall Stewart 		    ch->chunk_type, chk_length);
455580fefe0aSRandall Stewart 		SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
45564c9179adSRandall Stewart 		if (chk_length < sizeof(*ch) ||
45574c9179adSRandall Stewart 		    (*offset + (int)chk_length) > length) {
4558f8829a4aSRandall Stewart 			*offset = length;
455980a2d140SMichael Tuexen 			return (stcb);
4560f8829a4aSRandall Stewart 		}
4561f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4562f8829a4aSRandall Stewart 		/*
4563d0f6ab79SMichael Tuexen 		 * INIT and INIT-ACK only gets the init ack "header" portion
4564d0f6ab79SMichael Tuexen 		 * only because we don't have to process the peer's COOKIE.
4565d0f6ab79SMichael Tuexen 		 * All others get a complete chunk.
4566f8829a4aSRandall Stewart 		 */
4567d0f6ab79SMichael Tuexen 		switch (ch->chunk_type) {
4568d0f6ab79SMichael Tuexen 		case SCTP_INITIATION:
4569d0f6ab79SMichael Tuexen 			contiguous = sizeof(struct sctp_init_chunk);
4570d0f6ab79SMichael Tuexen 			break;
4571d0f6ab79SMichael Tuexen 		case SCTP_INITIATION_ACK:
4572d0f6ab79SMichael Tuexen 			contiguous = sizeof(struct sctp_init_ack_chunk);
4573d0f6ab79SMichael Tuexen 			break;
4574d0f6ab79SMichael Tuexen 		default:
4575d0f6ab79SMichael Tuexen 			contiguous = min(chk_length, sizeof(chunk_buf));
4576d0f6ab79SMichael Tuexen 			break;
45776e55db54SRandall Stewart 		}
4578d06c82f1SRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4579d0f6ab79SMichael Tuexen 		    contiguous,
4580d06c82f1SRandall Stewart 		    chunk_buf);
4581d06c82f1SRandall Stewart 		if (ch == NULL) {
4582d06c82f1SRandall Stewart 			*offset = length;
4583c70d1ef1SMichael Tuexen 			return (stcb);
4584d06c82f1SRandall Stewart 		}
45850053ed28SMichael Tuexen 
4586f8829a4aSRandall Stewart 		num_chunks++;
4587f8829a4aSRandall Stewart 		/* Save off the last place we got a control from */
4588f8829a4aSRandall Stewart 		if (stcb != NULL) {
4589ad81507eSRandall Stewart 			if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4590f8829a4aSRandall Stewart 				/*
4591f8829a4aSRandall Stewart 				 * allow last_control to be NULL if
4592f8829a4aSRandall Stewart 				 * ASCONF... ASCONF processing will find the
4593f8829a4aSRandall Stewart 				 * right net later
4594f8829a4aSRandall Stewart 				 */
4595ad81507eSRandall Stewart 				if ((netp != NULL) && (*netp != NULL))
4596f8829a4aSRandall Stewart 					stcb->asoc.last_control_chunk_from = *netp;
4597f8829a4aSRandall Stewart 			}
4598f8829a4aSRandall Stewart 		}
4599f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
4600f8829a4aSRandall Stewart 		sctp_audit_log(0xB0, ch->chunk_type);
4601f8829a4aSRandall Stewart #endif
4602f8829a4aSRandall Stewart 
4603f8829a4aSRandall Stewart 		/* check to see if this chunk required auth, but isn't */
4604b3f1ea41SRandall Stewart 		if ((stcb != NULL) &&
4605b3f1ea41SRandall Stewart 		    sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4606f8829a4aSRandall Stewart 		    !stcb->asoc.authenticated) {
4607f8829a4aSRandall Stewart 			/* "silently" ignore */
4608f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvauthmissing);
4609f8829a4aSRandall Stewart 			goto next_chunk;
4610f8829a4aSRandall Stewart 		}
4611f8829a4aSRandall Stewart 		switch (ch->chunk_type) {
4612f8829a4aSRandall Stewart 		case SCTP_INITIATION:
4613ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4614d68fdc4dSMichael Tuexen 			/* The INIT chunk must be the only chunk. */
4615d68fdc4dSMichael Tuexen 			if ((num_chunks > 1) ||
4616ab6174d5SMichael Tuexen 			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4617c70d1ef1SMichael Tuexen 				/*
4618c70d1ef1SMichael Tuexen 				 * RFC 4960bis requires stopping the
4619c70d1ef1SMichael Tuexen 				 * processing of the packet.
4620c70d1ef1SMichael Tuexen 				 */
4621f8829a4aSRandall Stewart 				*offset = length;
4622c70d1ef1SMichael Tuexen 				return (stcb);
4623f8829a4aSRandall Stewart 			}
4624d68fdc4dSMichael Tuexen 			/* Honor our resource limit. */
4625d68fdc4dSMichael Tuexen 			if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4626ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4627b1754ad1SMichael Tuexen 				sctp_abort_association(inp, stcb, m, iphlen,
4628b1754ad1SMichael Tuexen 				    src, dst, sh, op_err,
4629457b4b88SMichael Tuexen 				    mflowtype, mflowid,
4630f30ac432SMichael Tuexen 				    vrf_id, port);
4631f8829a4aSRandall Stewart 				*offset = length;
4632f8829a4aSRandall Stewart 				return (NULL);
4633f8829a4aSRandall Stewart 			}
4634b1754ad1SMichael Tuexen 			sctp_handle_init(m, iphlen, *offset, src, dst, sh,
4635ad81507eSRandall Stewart 			    (struct sctp_init_chunk *)ch, inp,
4636ca83f93cSMichael Tuexen 			    stcb, *netp, &abort_no_unlock,
4637457b4b88SMichael Tuexen 			    mflowtype, mflowid,
4638f30ac432SMichael Tuexen 			    vrf_id, port);
4639f8829a4aSRandall Stewart 			*offset = length;
464080a2d140SMichael Tuexen 			if ((!abort_no_unlock) && (stcb != NULL)) {
464180a2d140SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
46426e55db54SRandall Stewart 			}
4643f8829a4aSRandall Stewart 			return (NULL);
4644f8829a4aSRandall Stewart 			break;
46459a972525SRandall Stewart 		case SCTP_PAD_CHUNK:
46469a972525SRandall Stewart 			break;
4647f8829a4aSRandall Stewart 		case SCTP_INITIATION_ACK:
4648469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT_ACK\n");
4649f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4650f8829a4aSRandall Stewart 				/* We are not interested anymore */
465180a2d140SMichael Tuexen 				if ((stcb != NULL) && (stcb->asoc.total_output_queue_size)) {
4652f8829a4aSRandall Stewart 					;
4653f8829a4aSRandall Stewart 				} else {
4654f8829a4aSRandall Stewart 					*offset = length;
465580a2d140SMichael Tuexen 					if (stcb != NULL) {
4656b7d130beSMichael Tuexen 						(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4657b7d130beSMichael Tuexen 						    SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
4658f8829a4aSRandall Stewart 					}
4659f8829a4aSRandall Stewart 					return (NULL);
4660f8829a4aSRandall Stewart 				}
4661f8829a4aSRandall Stewart 			}
4662ab6174d5SMichael Tuexen 			/* The INIT-ACK chunk must be the only chunk. */
4663f8829a4aSRandall Stewart 			if ((num_chunks > 1) ||
4664ab6174d5SMichael Tuexen 			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4665f8829a4aSRandall Stewart 				*offset = length;
466680a2d140SMichael Tuexen 				return (stcb);
4667f8829a4aSRandall Stewart 			}
4668469a65d1SMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
4669b1754ad1SMichael Tuexen 				ret = sctp_handle_init_ack(m, iphlen, *offset,
4670b1754ad1SMichael Tuexen 				    src, dst, sh,
4671f30ac432SMichael Tuexen 				    (struct sctp_init_ack_chunk *)ch,
4672f30ac432SMichael Tuexen 				    stcb, *netp,
4673f30ac432SMichael Tuexen 				    &abort_no_unlock,
4674457b4b88SMichael Tuexen 				    mflowtype, mflowid,
4675f30ac432SMichael Tuexen 				    vrf_id);
4676ad81507eSRandall Stewart 			} else {
4677ad81507eSRandall Stewart 				ret = -1;
4678ad81507eSRandall Stewart 			}
4679d68fdc4dSMichael Tuexen 			*offset = length;
4680d68fdc4dSMichael Tuexen 			if (abort_no_unlock) {
4681d68fdc4dSMichael Tuexen 				return (NULL);
4682d68fdc4dSMichael Tuexen 			}
4683f8829a4aSRandall Stewart 			/*
4684f8829a4aSRandall Stewart 			 * Special case, I must call the output routine to
4685f8829a4aSRandall Stewart 			 * get the cookie echoed
4686f8829a4aSRandall Stewart 			 */
4687d68fdc4dSMichael Tuexen 			if ((stcb != NULL) && (ret == 0)) {
4688ceaad40aSRandall Stewart 				sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4689d68fdc4dSMichael Tuexen 			}
469080a2d140SMichael Tuexen 			return (stcb);
4691f8829a4aSRandall Stewart 			break;
4692f8829a4aSRandall Stewart 		case SCTP_SELECTIVE_ACK:
4693469a65d1SMichael Tuexen 		case SCTP_NR_SELECTIVE_ACK:
4694f8829a4aSRandall Stewart 			{
4695f8829a4aSRandall Stewart 				int abort_now = 0;
4696f8829a4aSRandall Stewart 				uint32_t a_rwnd, cum_ack;
4697469a65d1SMichael Tuexen 				uint16_t num_seg, num_nr_seg, num_dup;
4698cd554309SMichael Tuexen 				uint8_t flags;
4699cd554309SMichael Tuexen 				int offset_seg, offset_dup;
4700f8829a4aSRandall Stewart 
4701469a65d1SMichael Tuexen 				SCTPDBG(SCTP_DEBUG_INPUT3, "%s\n",
4702469a65d1SMichael Tuexen 				    ch->chunk_type == SCTP_SELECTIVE_ACK ? "SCTP_SACK" : "SCTP_NR_SACK");
470320083c2eSMichael Tuexen 				SCTP_STAT_INCR(sctps_recvsacks);
4704cd554309SMichael Tuexen 				if (stcb == NULL) {
4705469a65d1SMichael Tuexen 					SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing %s chunk\n",
4706469a65d1SMichael Tuexen 					    (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK");
4707cd554309SMichael Tuexen 					break;
47086e55db54SRandall Stewart 				}
4709469a65d1SMichael Tuexen 				if (ch->chunk_type == SCTP_SELECTIVE_ACK) {
4710cd554309SMichael Tuexen 					if (chk_length < sizeof(struct sctp_sack_chunk)) {
4711cd554309SMichael Tuexen 						SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4712cd554309SMichael Tuexen 						break;
4713d06c82f1SRandall Stewart 					}
4714469a65d1SMichael Tuexen 				} else {
4715469a65d1SMichael Tuexen 					if (stcb->asoc.nrsack_supported == 0) {
4716469a65d1SMichael Tuexen 						goto unknown_chunk;
4717469a65d1SMichael Tuexen 					}
4718469a65d1SMichael Tuexen 					if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
4719469a65d1SMichael Tuexen 						SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR_SACK chunk, too small\n");
4720469a65d1SMichael Tuexen 						break;
4721469a65d1SMichael Tuexen 					}
4722469a65d1SMichael Tuexen 				}
4723839d21d6SMichael Tuexen 				if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
47242afb3e84SRandall Stewart 					/*-
47252afb3e84SRandall Stewart 					 * If we have sent a shutdown-ack, we will pay no
47262afb3e84SRandall Stewart 					 * attention to a sack sent in to us since
47272afb3e84SRandall Stewart 					 * we don't care anymore.
47282afb3e84SRandall Stewart 					 */
4729a1e13272SRandall Stewart 					break;
47302afb3e84SRandall Stewart 				}
4731cd554309SMichael Tuexen 				flags = ch->chunk_flags;
4732469a65d1SMichael Tuexen 				if (ch->chunk_type == SCTP_SELECTIVE_ACK) {
4733469a65d1SMichael Tuexen 					struct sctp_sack_chunk *sack;
4734469a65d1SMichael Tuexen 
4735469a65d1SMichael Tuexen 					sack = (struct sctp_sack_chunk *)ch;
4736f8829a4aSRandall Stewart 					cum_ack = ntohl(sack->sack.cum_tsn_ack);
4737f8829a4aSRandall Stewart 					num_seg = ntohs(sack->sack.num_gap_ack_blks);
4738469a65d1SMichael Tuexen 					num_nr_seg = 0;
4739cd554309SMichael Tuexen 					num_dup = ntohs(sack->sack.num_dup_tsns);
4740469a65d1SMichael Tuexen 					a_rwnd = ntohl(sack->sack.a_rwnd);
4741cd554309SMichael Tuexen 					if (sizeof(struct sctp_sack_chunk) +
4742cd554309SMichael Tuexen 					    num_seg * sizeof(struct sctp_gap_ack_block) +
4743cd554309SMichael Tuexen 					    num_dup * sizeof(uint32_t) != chk_length) {
4744cd554309SMichael Tuexen 						SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4745cd554309SMichael Tuexen 						break;
4746cd554309SMichael Tuexen 					}
4747cd554309SMichael Tuexen 					offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4748cd554309SMichael Tuexen 					offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4749f8829a4aSRandall Stewart 				} else {
4750830d754dSRandall Stewart 					struct sctp_nr_sack_chunk *nr_sack;
4751830d754dSRandall Stewart 
4752830d754dSRandall Stewart 					nr_sack = (struct sctp_nr_sack_chunk *)ch;
4753830d754dSRandall Stewart 					cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
4754830d754dSRandall Stewart 					num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
4755830d754dSRandall Stewart 					num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4756cd554309SMichael Tuexen 					num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
4757469a65d1SMichael Tuexen 					a_rwnd = ntohl(nr_sack->nr_sack.a_rwnd);
4758cd554309SMichael Tuexen 					if (sizeof(struct sctp_nr_sack_chunk) +
4759cd554309SMichael Tuexen 					    (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
4760cd554309SMichael Tuexen 					    num_dup * sizeof(uint32_t) != chk_length) {
4761cd554309SMichael Tuexen 						SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
4762cd554309SMichael Tuexen 						break;
4763cd554309SMichael Tuexen 					}
4764cd554309SMichael Tuexen 					offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
4765469a65d1SMichael Tuexen 					offset_dup = offset_seg + (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block);
4766469a65d1SMichael Tuexen 				}
4767469a65d1SMichael Tuexen 				SCTPDBG(SCTP_DEBUG_INPUT3, "%s process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4768469a65d1SMichael Tuexen 				    (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK",
4769cd554309SMichael Tuexen 				    cum_ack, num_seg, a_rwnd);
4770830d754dSRandall Stewart 				stcb->asoc.seen_a_sack_this_pkt = 1;
4771830d754dSRandall Stewart 				if ((stcb->asoc.pr_sctp_cnt == 0) &&
4772cd554309SMichael Tuexen 				    (num_seg == 0) && (num_nr_seg == 0) &&
477320b07a4dSMichael Tuexen 				    SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4774830d754dSRandall Stewart 				    (stcb->asoc.saw_sack_with_frags == 0) &&
4775d9c5cfeaSMichael Tuexen 				    (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4776cd554309SMichael Tuexen 				    (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
4777830d754dSRandall Stewart 					/*
4778830d754dSRandall Stewart 					 * We have a SIMPLE sack having no
4779830d754dSRandall Stewart 					 * prior segments and data on sent
4780cd554309SMichael Tuexen 					 * queue to be acked. Use the faster
4781cd554309SMichael Tuexen 					 * path sack processing. We also
4782cd554309SMichael Tuexen 					 * allow window update sacks with no
4783cd554309SMichael Tuexen 					 * missing segments to go this way
4784cd554309SMichael Tuexen 					 * too.
4785830d754dSRandall Stewart 					 */
4786493d8e5aSRandall Stewart 					sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
4787899288aeSRandall Stewart 					    &abort_now, ecne_seen);
4788830d754dSRandall Stewart 				} else {
4789469a65d1SMichael Tuexen 					if ((netp != NULL) && (*netp != NULL)) {
47907215cc1bSMichael Tuexen 						sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4791cd554309SMichael Tuexen 						    num_seg, num_nr_seg, num_dup, &abort_now, flags,
4792899288aeSRandall Stewart 						    cum_ack, a_rwnd, ecne_seen);
4793830d754dSRandall Stewart 					}
4794469a65d1SMichael Tuexen 				}
4795830d754dSRandall Stewart 				if (abort_now) {
4796830d754dSRandall Stewart 					/* ABORT signal from sack processing */
4797830d754dSRandall Stewart 					*offset = length;
4798830d754dSRandall Stewart 					return (NULL);
4799830d754dSRandall Stewart 				}
4800cd554309SMichael Tuexen 				if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4801cd554309SMichael Tuexen 				    TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4802cd554309SMichael Tuexen 				    (stcb->asoc.stream_queue_cnt == 0)) {
4803cd554309SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4804cd554309SMichael Tuexen 				}
4805830d754dSRandall Stewart 				break;
4806469a65d1SMichael Tuexen 			}
4807f8829a4aSRandall Stewart 		case SCTP_HEARTBEAT_REQUEST:
4808ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
4809469a65d1SMichael Tuexen 			if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
4810f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_recvheartbeat);
4811ad81507eSRandall Stewart 				sctp_send_heartbeat_ack(stcb, m, *offset,
4812ad81507eSRandall Stewart 				    chk_length, *netp);
4813ad81507eSRandall Stewart 			}
4814f8829a4aSRandall Stewart 			break;
4815f8829a4aSRandall Stewart 		case SCTP_HEARTBEAT_ACK:
4816469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT_ACK\n");
4817ad81507eSRandall Stewart 			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
4818d06c82f1SRandall Stewart 				/* Its not ours */
48199de7354bSMichael Tuexen 				break;
4820d06c82f1SRandall Stewart 			}
4821f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvheartbeatack);
4822469a65d1SMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
4823f8829a4aSRandall Stewart 				sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
4824f8829a4aSRandall Stewart 				    stcb, *netp);
4825469a65d1SMichael Tuexen 			}
4826f8829a4aSRandall Stewart 			break;
4827f8829a4aSRandall Stewart 		case SCTP_ABORT_ASSOCIATION:
4828207304d4SRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
4829dd294dceSMichael Tuexen 			    (void *)stcb);
4830f8829a4aSRandall Stewart 			*offset = length;
4831701492a5SMichael Tuexen 			if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
4832701492a5SMichael Tuexen 				if (sctp_handle_abort((struct sctp_abort_chunk *)ch, stcb, *netp)) {
4833f8829a4aSRandall Stewart 					return (NULL);
4834701492a5SMichael Tuexen 				} else {
4835701492a5SMichael Tuexen 					return (stcb);
4836701492a5SMichael Tuexen 				}
4837701492a5SMichael Tuexen 			} else {
4838701492a5SMichael Tuexen 				return (NULL);
4839701492a5SMichael Tuexen 			}
4840f8829a4aSRandall Stewart 			break;
4841f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN:
4842207304d4SRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
4843dd294dceSMichael Tuexen 			    (void *)stcb);
4844ad81507eSRandall Stewart 			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
48459de7354bSMichael Tuexen 				break;
4846ad81507eSRandall Stewart 			}
4847469a65d1SMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
48489de7354bSMichael Tuexen 				abort_flag = 0;
4849f8829a4aSRandall Stewart 				sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
4850f8829a4aSRandall Stewart 				    stcb, *netp, &abort_flag);
4851f8829a4aSRandall Stewart 				if (abort_flag) {
4852f8829a4aSRandall Stewart 					*offset = length;
4853f8829a4aSRandall Stewart 					return (NULL);
4854f8829a4aSRandall Stewart 				}
4855f8829a4aSRandall Stewart 			}
4856f8829a4aSRandall Stewart 			break;
4857f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_ACK:
4858469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_ACK, stcb %p\n", (void *)stcb);
4859d1cb8d11SMichael Tuexen 			if ((chk_length == sizeof(struct sctp_shutdown_ack_chunk)) &&
4860d1cb8d11SMichael Tuexen 			    (stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
4861f8829a4aSRandall Stewart 				sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
4862f8829a4aSRandall Stewart 				*offset = length;
4863f8829a4aSRandall Stewart 				return (NULL);
4864d1cb8d11SMichael Tuexen 			}
4865f8829a4aSRandall Stewart 			break;
4866f8829a4aSRandall Stewart 		case SCTP_OPERATION_ERROR:
4867469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP_ERR\n");
4868469a65d1SMichael Tuexen 			if ((stcb != NULL) && (netp != NULL) && (*netp != NULL) &&
48693e87bccdSMichael Tuexen 			    sctp_handle_error(ch, stcb, *netp, contiguous) < 0) {
4870f8829a4aSRandall Stewart 				*offset = length;
4871f8829a4aSRandall Stewart 				return (NULL);
4872f8829a4aSRandall Stewart 			}
4873f8829a4aSRandall Stewart 			break;
4874f8829a4aSRandall Stewart 		case SCTP_COOKIE_ECHO:
4875ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3,
4876469a65d1SMichael Tuexen 			    "SCTP_COOKIE_ECHO, stcb %p\n", (void *)stcb);
4877469a65d1SMichael Tuexen 			if ((stcb != NULL) && (stcb->asoc.total_output_queue_size > 0)) {
4878f8829a4aSRandall Stewart 				;
4879f8829a4aSRandall Stewart 			} else {
4880ad81507eSRandall Stewart 				if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4881f8829a4aSRandall Stewart 					/* We are not interested anymore */
48820c7dc840SRandall Stewart 			abend:
488380a2d140SMichael Tuexen 					if (stcb != NULL) {
488428085b2eSRandall Stewart 						SCTP_TCB_UNLOCK(stcb);
488528085b2eSRandall Stewart 					}
4886f8829a4aSRandall Stewart 					*offset = length;
4887f8829a4aSRandall Stewart 					return (NULL);
4888f8829a4aSRandall Stewart 				}
4889f8829a4aSRandall Stewart 			}
48903017b21bSMichael Tuexen 			/*-
4891f8829a4aSRandall Stewart 			 * First are we accepting? We do this again here
489288a7eb29SRandall Stewart 			 * since it is possible that a previous endpoint WAS
489388a7eb29SRandall Stewart 			 * listening responded to a INIT-ACK and then
4894f8829a4aSRandall Stewart 			 * closed. We opened and bound.. and are now no
4895f8829a4aSRandall Stewart 			 * longer listening.
4896779f106aSGleb Smirnoff 			 *
4897779f106aSGleb Smirnoff 			 * XXXGL: notes on checking listen queue length.
4898779f106aSGleb Smirnoff 			 * 1) SCTP_IS_LISTENING() doesn't necessarily mean
4899779f106aSGleb Smirnoff 			 *    SOLISTENING(), because a listening "UDP type"
4900779f106aSGleb Smirnoff 			 *    socket isn't listening in terms of the socket
4901779f106aSGleb Smirnoff 			 *    layer.  It is a normal data flow socket, that
4902779f106aSGleb Smirnoff 			 *    can fork off new connections.  Thus, we should
4903779f106aSGleb Smirnoff 			 *    look into sol_qlen only in case we are !UDP.
4904779f106aSGleb Smirnoff 			 * 2) Checking sol_qlen in general requires locking
4905779f106aSGleb Smirnoff 			 *    the socket, and this code lacks that.
4906f8829a4aSRandall Stewart 			 */
49075d08768aSMichael Tuexen 			if ((stcb == NULL) &&
49085d08768aSMichael Tuexen 			    (!SCTP_IS_LISTENING(inp) ||
4909779f106aSGleb Smirnoff 			    (!(inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
4910779f106aSGleb Smirnoff 			    inp->sctp_socket->sol_qlen >= inp->sctp_socket->sol_qlimit))) {
4911b201f536SRandall Stewart 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4912b3f1ea41SRandall Stewart 				    (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
4913ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4914b1754ad1SMichael Tuexen 					sctp_abort_association(inp, stcb, m, iphlen,
4915b1754ad1SMichael Tuexen 					    src, dst, sh, op_err,
4916457b4b88SMichael Tuexen 					    mflowtype, mflowid,
4917f30ac432SMichael Tuexen 					    vrf_id, port);
4918f8829a4aSRandall Stewart 				}
4919f8829a4aSRandall Stewart 				*offset = length;
4920f8829a4aSRandall Stewart 				return (NULL);
4921b201f536SRandall Stewart 			} else {
4922f8829a4aSRandall Stewart 				struct mbuf *ret_buf;
4923a5d547adSRandall Stewart 				struct sctp_inpcb *linp;
4924efd5e692SMichael Tuexen 				struct sctp_tmit_chunk *chk;
4925f8829a4aSRandall Stewart 
4926ad81507eSRandall Stewart 				if (stcb) {
4927a5d547adSRandall Stewart 					linp = NULL;
4928ad81507eSRandall Stewart 				} else {
4929a5d547adSRandall Stewart 					linp = inp;
4930ad81507eSRandall Stewart 				}
4931a5d547adSRandall Stewart 
4932469a65d1SMichael Tuexen 				if (linp != NULL) {
4933a5d547adSRandall Stewart 					SCTP_ASOC_CREATE_LOCK(linp);
49340c7dc840SRandall Stewart 					if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
49350c7dc840SRandall Stewart 					    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
49360c7dc840SRandall Stewart 						SCTP_ASOC_CREATE_UNLOCK(linp);
49370c7dc840SRandall Stewart 						goto abend;
49380c7dc840SRandall Stewart 					}
4939ad81507eSRandall Stewart 				}
49400053ed28SMichael Tuexen 
4941469a65d1SMichael Tuexen 				if (netp != NULL) {
494280a2d140SMichael Tuexen 					struct sctp_tcb *locked_stcb;
494380a2d140SMichael Tuexen 
494480a2d140SMichael Tuexen 					locked_stcb = stcb;
4945f8829a4aSRandall Stewart 					ret_buf =
4946f8829a4aSRandall Stewart 					    sctp_handle_cookie_echo(m, iphlen,
4947b1754ad1SMichael Tuexen 					    *offset,
4948b1754ad1SMichael Tuexen 					    src, dst,
4949b1754ad1SMichael Tuexen 					    sh,
4950f8829a4aSRandall Stewart 					    (struct sctp_cookie_echo_chunk *)ch,
4951f8829a4aSRandall Stewart 					    &inp, &stcb, netp,
4952f8829a4aSRandall Stewart 					    auth_skipped,
4953f8829a4aSRandall Stewart 					    auth_offset,
4954a5d547adSRandall Stewart 					    auth_len,
495580a2d140SMichael Tuexen 					    &locked_stcb,
4956457b4b88SMichael Tuexen 					    mflowtype,
4957f30ac432SMichael Tuexen 					    mflowid,
4958c54a18d2SRandall Stewart 					    vrf_id,
4959c54a18d2SRandall Stewart 					    port);
496080a2d140SMichael Tuexen 					if ((locked_stcb != NULL) && (locked_stcb != stcb)) {
496180a2d140SMichael Tuexen 						SCTP_TCB_UNLOCK(locked_stcb);
496280a2d140SMichael Tuexen 					}
496380a2d140SMichael Tuexen 					if (stcb != NULL) {
496480a2d140SMichael Tuexen 						SCTP_TCB_LOCK_ASSERT(stcb);
496580a2d140SMichael Tuexen 					}
4966ad81507eSRandall Stewart 				} else {
4967ad81507eSRandall Stewart 					ret_buf = NULL;
4968ad81507eSRandall Stewart 				}
4969469a65d1SMichael Tuexen 				if (linp != NULL) {
4970a5d547adSRandall Stewart 					SCTP_ASOC_CREATE_UNLOCK(linp);
4971ad81507eSRandall Stewart 				}
4972f8829a4aSRandall Stewart 				if (ret_buf == NULL) {
497380a2d140SMichael Tuexen 					if (stcb != NULL) {
497480a2d140SMichael Tuexen 						SCTP_TCB_UNLOCK(stcb);
4975f8829a4aSRandall Stewart 					}
4976ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INPUT3,
4977ad81507eSRandall Stewart 					    "GAK, null buffer\n");
4978f8829a4aSRandall Stewart 					*offset = length;
4979f8829a4aSRandall Stewart 					return (NULL);
4980f8829a4aSRandall Stewart 				}
4981f8829a4aSRandall Stewart 				/* if AUTH skipped, see if it verified... */
4982f8829a4aSRandall Stewart 				if (auth_skipped) {
4983f8829a4aSRandall Stewart 					got_auth = 1;
4984f8829a4aSRandall Stewart 					auth_skipped = 0;
4985f8829a4aSRandall Stewart 				}
4986efd5e692SMichael Tuexen 				/* Restart the timer if we have pending data */
498786fd36c5SMichael Tuexen 				TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
4988efd5e692SMichael Tuexen 					if (chk->whoTo != NULL) {
4989efd5e692SMichael Tuexen 						break;
4990efd5e692SMichael Tuexen 					}
4991efd5e692SMichael Tuexen 				}
4992efd5e692SMichael Tuexen 				if (chk != NULL) {
49934a9ef3f8SMichael Tuexen 					sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
4994f8829a4aSRandall Stewart 				}
4995f8829a4aSRandall Stewart 			}
4996f8829a4aSRandall Stewart 			break;
4997f8829a4aSRandall Stewart 		case SCTP_COOKIE_ACK:
4998469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE_ACK, stcb %p\n", (void *)stcb);
4999ad81507eSRandall Stewart 			if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
50009de7354bSMichael Tuexen 				break;
500117205eccSRandall Stewart 			}
5002f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5003f8829a4aSRandall Stewart 				/* We are not interested anymore */
5004f8829a4aSRandall Stewart 				if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5005f8829a4aSRandall Stewart 					;
5006ad81507eSRandall Stewart 				} else if (stcb) {
5007b7d130beSMichael Tuexen 					(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5008b7d130beSMichael Tuexen 					    SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
5009f8829a4aSRandall Stewart 					*offset = length;
5010f8829a4aSRandall Stewart 					return (NULL);
5011f8829a4aSRandall Stewart 				}
5012f8829a4aSRandall Stewart 			}
5013469a65d1SMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
5014f8829a4aSRandall Stewart 				sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
50156e55db54SRandall Stewart 			}
5016f8829a4aSRandall Stewart 			break;
5017f8829a4aSRandall Stewart 		case SCTP_ECN_ECHO:
5018469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_ECHO\n");
50199de7354bSMichael Tuexen 			if (stcb == NULL) {
50209de7354bSMichael Tuexen 				break;
5021d06c82f1SRandall Stewart 			}
5022c79bec9cSMichael Tuexen 			if (stcb->asoc.ecn_supported == 0) {
5023c79bec9cSMichael Tuexen 				goto unknown_chunk;
5024c79bec9cSMichael Tuexen 			}
50259de7354bSMichael Tuexen 			if (chk_length != sizeof(struct sctp_ecne_chunk)) {
50269de7354bSMichael Tuexen 				break;
50279de7354bSMichael Tuexen 			}
5028469a65d1SMichael Tuexen 			sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, stcb);
5029899288aeSRandall Stewart 			ecne_seen = 1;
5030f8829a4aSRandall Stewart 			break;
5031f8829a4aSRandall Stewart 		case SCTP_ECN_CWR:
5032469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_CWR\n");
50339de7354bSMichael Tuexen 			if (stcb == NULL) {
50349de7354bSMichael Tuexen 				break;
5035d06c82f1SRandall Stewart 			}
5036c79bec9cSMichael Tuexen 			if (stcb->asoc.ecn_supported == 0) {
5037c79bec9cSMichael Tuexen 				goto unknown_chunk;
5038c79bec9cSMichael Tuexen 			}
50399de7354bSMichael Tuexen 			if (chk_length != sizeof(struct sctp_cwr_chunk)) {
50409de7354bSMichael Tuexen 				break;
50419de7354bSMichael Tuexen 			}
5042a21779f0SRandall Stewart 			sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5043f8829a4aSRandall Stewart 			break;
5044f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_COMPLETE:
5045469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_COMPLETE, stcb %p\n", (void *)stcb);
5046f8829a4aSRandall Stewart 			/* must be first and only chunk */
5047f8829a4aSRandall Stewart 			if ((num_chunks > 1) ||
50484c9179adSRandall Stewart 			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5049f8829a4aSRandall Stewart 				*offset = length;
505080a2d140SMichael Tuexen 				return (stcb);
5051f8829a4aSRandall Stewart 			}
5052d1cb8d11SMichael Tuexen 			if ((chk_length == sizeof(struct sctp_shutdown_complete_chunk)) &&
5053d1cb8d11SMichael Tuexen 			    (stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5054f8829a4aSRandall Stewart 				sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5055f8829a4aSRandall Stewart 				    stcb, *netp);
5056f8829a4aSRandall Stewart 				*offset = length;
5057f8829a4aSRandall Stewart 				return (NULL);
5058d1cb8d11SMichael Tuexen 			}
5059f8829a4aSRandall Stewart 			break;
5060f8829a4aSRandall Stewart 		case SCTP_ASCONF:
5061ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5062469a65d1SMichael Tuexen 			if (stcb != NULL) {
5063c79bec9cSMichael Tuexen 				if (stcb->asoc.asconf_supported == 0) {
5064c79bec9cSMichael Tuexen 					goto unknown_chunk;
5065c79bec9cSMichael Tuexen 				}
5066b1754ad1SMichael Tuexen 				sctp_handle_asconf(m, *offset, src,
50672afb3e84SRandall Stewart 				    (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
50682afb3e84SRandall Stewart 				asconf_cnt++;
50696e55db54SRandall Stewart 			}
5070f8829a4aSRandall Stewart 			break;
5071f8829a4aSRandall Stewart 		case SCTP_ASCONF_ACK:
5072469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF_ACK\n");
50739de7354bSMichael Tuexen 			if (stcb == NULL) {
50749de7354bSMichael Tuexen 				break;
5075d06c82f1SRandall Stewart 			}
5076c79bec9cSMichael Tuexen 			if (stcb->asoc.asconf_supported == 0) {
5077c79bec9cSMichael Tuexen 				goto unknown_chunk;
5078c79bec9cSMichael Tuexen 			}
50799de7354bSMichael Tuexen 			if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
50809de7354bSMichael Tuexen 				break;
50819de7354bSMichael Tuexen 			}
50829de7354bSMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
5083f8829a4aSRandall Stewart 				/* He's alive so give him credit */
5084b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5085c4739e2fSRandall Stewart 					sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5086c4739e2fSRandall Stewart 					    stcb->asoc.overall_error_count,
5087c4739e2fSRandall Stewart 					    0,
5088c4739e2fSRandall Stewart 					    SCTP_FROM_SCTP_INPUT,
5089c4739e2fSRandall Stewart 					    __LINE__);
5090c4739e2fSRandall Stewart 				}
5091f8829a4aSRandall Stewart 				stcb->asoc.overall_error_count = 0;
5092f8829a4aSRandall Stewart 				sctp_handle_asconf_ack(m, *offset,
50933232788eSRandall Stewart 				    (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
50943232788eSRandall Stewart 				if (abort_no_unlock)
50953232788eSRandall Stewart 					return (NULL);
50966e55db54SRandall Stewart 			}
5097f8829a4aSRandall Stewart 			break;
5098f8829a4aSRandall Stewart 		case SCTP_FORWARD_CUM_TSN:
509944249214SRandall Stewart 		case SCTP_IFORWARD_CUM_TSN:
5100c96d7c37SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "%s\n",
5101c96d7c37SMichael Tuexen 			    ch->chunk_type == SCTP_FORWARD_CUM_TSN ? "FORWARD_TSN" : "I_FORWARD_TSN");
51029de7354bSMichael Tuexen 			if (stcb == NULL) {
51039de7354bSMichael Tuexen 				break;
5104d06c82f1SRandall Stewart 			}
5105c79bec9cSMichael Tuexen 			if (stcb->asoc.prsctp_supported == 0) {
5106c79bec9cSMichael Tuexen 				goto unknown_chunk;
5107c79bec9cSMichael Tuexen 			}
51089de7354bSMichael Tuexen 			if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
51099de7354bSMichael Tuexen 				break;
51109de7354bSMichael Tuexen 			}
5111fcbfdc0aSMichael Tuexen 			if (((stcb->asoc.idata_supported == 1) && (ch->chunk_type == SCTP_FORWARD_CUM_TSN)) ||
5112fcbfdc0aSMichael Tuexen 			    ((stcb->asoc.idata_supported == 0) && (ch->chunk_type == SCTP_IFORWARD_CUM_TSN))) {
5113c96d7c37SMichael Tuexen 				if (ch->chunk_type == SCTP_FORWARD_CUM_TSN) {
5114c96d7c37SMichael Tuexen 					SCTP_SNPRINTF(msg, sizeof(msg), "%s", "FORWARD-TSN chunk received when I-FORWARD-TSN was negotiated");
5115c96d7c37SMichael Tuexen 				} else {
5116c96d7c37SMichael Tuexen 					SCTP_SNPRINTF(msg, sizeof(msg), "%s", "I-FORWARD-TSN chunk received when FORWARD-TSN was negotiated");
5117c96d7c37SMichael Tuexen 				}
5118c96d7c37SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
5119c96d7c37SMichael Tuexen 				sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
5120c96d7c37SMichael Tuexen 				*offset = length;
5121c96d7c37SMichael Tuexen 				return (NULL);
5122c96d7c37SMichael Tuexen 			}
5123f8829a4aSRandall Stewart 			*fwd_tsn_seen = 1;
5124f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5125f8829a4aSRandall Stewart 				/* We are not interested anymore */
5126b7d130beSMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5127b7d130beSMichael Tuexen 				    SCTP_FROM_SCTP_INPUT + SCTP_LOC_31);
5128f8829a4aSRandall Stewart 				*offset = length;
5129f8829a4aSRandall Stewart 				return (NULL);
5130f8829a4aSRandall Stewart 			}
513191843cf3SMichael Tuexen 			/*
51329de7354bSMichael Tuexen 			 * For sending a SACK this looks like DATA chunks.
513391843cf3SMichael Tuexen 			 */
513491843cf3SMichael Tuexen 			stcb->asoc.last_data_chunk_from = stcb->asoc.last_control_chunk_from;
51359de7354bSMichael Tuexen 			abort_flag = 0;
5136f8829a4aSRandall Stewart 			sctp_handle_forward_tsn(stcb,
5137671d309cSRandall Stewart 			    (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5138f8829a4aSRandall Stewart 			if (abort_flag) {
5139f8829a4aSRandall Stewart 				*offset = length;
5140f8829a4aSRandall Stewart 				return (NULL);
5141c4739e2fSRandall Stewart 			}
5142f8829a4aSRandall Stewart 			break;
5143f8829a4aSRandall Stewart 		case SCTP_STREAM_RESET:
5144ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
51459de7354bSMichael Tuexen 			if (stcb == NULL) {
51469de7354bSMichael Tuexen 				break;
5147d06c82f1SRandall Stewart 			}
5148317e00efSMichael Tuexen 			if (stcb->asoc.reconfig_supported == 0) {
5149c79bec9cSMichael Tuexen 				goto unknown_chunk;
5150f8829a4aSRandall Stewart 			}
51519de7354bSMichael Tuexen 			if (chk_length < sizeof(struct sctp_stream_reset_tsn_req)) {
51529de7354bSMichael Tuexen 				break;
51539de7354bSMichael Tuexen 			}
5154a169d6ecSMichael Tuexen 			if (sctp_handle_stream_reset(stcb, m, *offset, ch)) {
5155f8829a4aSRandall Stewart 				/* stop processing */
5156f8829a4aSRandall Stewart 				*offset = length;
5157f8829a4aSRandall Stewart 				return (NULL);
5158f8829a4aSRandall Stewart 			}
5159f8829a4aSRandall Stewart 			break;
5160f8829a4aSRandall Stewart 		case SCTP_PACKET_DROPPED:
5161ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
51629de7354bSMichael Tuexen 			if (stcb == NULL) {
51639de7354bSMichael Tuexen 				break;
5164d06c82f1SRandall Stewart 			}
5165c79bec9cSMichael Tuexen 			if (stcb->asoc.pktdrop_supported == 0) {
5166c79bec9cSMichael Tuexen 				goto unknown_chunk;
5167c79bec9cSMichael Tuexen 			}
51689de7354bSMichael Tuexen 			if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
51699de7354bSMichael Tuexen 				break;
51709de7354bSMichael Tuexen 			}
51719de7354bSMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
5172f8829a4aSRandall Stewart 				sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5173458303daSRandall Stewart 				    stcb, *netp,
5174d0f6ab79SMichael Tuexen 				    min(chk_length, contiguous));
51756e55db54SRandall Stewart 			}
5176f8829a4aSRandall Stewart 			break;
5177f8829a4aSRandall Stewart 		case SCTP_AUTHENTICATION:
5178ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5179f8829a4aSRandall Stewart 			if (stcb == NULL) {
5180f8829a4aSRandall Stewart 				/* save the first AUTH for later processing */
5181f8829a4aSRandall Stewart 				if (auth_skipped == 0) {
5182f8829a4aSRandall Stewart 					auth_offset = *offset;
5183f8829a4aSRandall Stewart 					auth_len = chk_length;
5184f8829a4aSRandall Stewart 					auth_skipped = 1;
5185f8829a4aSRandall Stewart 				}
5186f8829a4aSRandall Stewart 				/* skip this chunk (temporarily) */
51879de7354bSMichael Tuexen 				break;
5188f8829a4aSRandall Stewart 			}
5189c79bec9cSMichael Tuexen 			if (stcb->asoc.auth_supported == 0) {
5190c79bec9cSMichael Tuexen 				goto unknown_chunk;
5191c79bec9cSMichael Tuexen 			}
5192d06c82f1SRandall Stewart 			if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5193ad81507eSRandall Stewart 			    (chk_length > (sizeof(struct sctp_auth_chunk) +
5194ad81507eSRandall Stewart 			    SCTP_AUTH_DIGEST_LEN_MAX))) {
5195d06c82f1SRandall Stewart 				/* Its not ours */
5196d06c82f1SRandall Stewart 				*offset = length;
519780a2d140SMichael Tuexen 				return (stcb);
5198d06c82f1SRandall Stewart 			}
5199f8829a4aSRandall Stewart 			if (got_auth == 1) {
5200f8829a4aSRandall Stewart 				/* skip this chunk... it's already auth'd */
52019de7354bSMichael Tuexen 				break;
5202f8829a4aSRandall Stewart 			}
5203f8829a4aSRandall Stewart 			got_auth = 1;
5204f2f66ef6SMichael Tuexen 			if (sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, m, *offset)) {
5205f8829a4aSRandall Stewart 				/* auth HMAC failed so dump the packet */
5206f8829a4aSRandall Stewart 				*offset = length;
5207f8829a4aSRandall Stewart 				return (stcb);
5208f8829a4aSRandall Stewart 			} else {
5209f8829a4aSRandall Stewart 				/* remaining chunks are HMAC checked */
5210f8829a4aSRandall Stewart 				stcb->asoc.authenticated = 1;
5211f8829a4aSRandall Stewart 			}
5212f8829a4aSRandall Stewart 			break;
5213f8829a4aSRandall Stewart 
5214f8829a4aSRandall Stewart 		default:
5215f8829a4aSRandall Stewart 	unknown_chunk:
5216f8829a4aSRandall Stewart 			/* it's an unknown chunk! */
5217e99ce3eaSMichael Tuexen 			if ((ch->chunk_type & 0x40) &&
5218e99ce3eaSMichael Tuexen 			    (stcb != NULL) &&
5219e99ce3eaSMichael Tuexen 			    (SCTP_GET_STATE(stcb) != SCTP_STATE_EMPTY) &&
5220e99ce3eaSMichael Tuexen 			    (SCTP_GET_STATE(stcb) != SCTP_STATE_INUSE) &&
5221e99ce3eaSMichael Tuexen 			    (SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT)) {
522286eda749SMichael Tuexen 				struct sctp_gen_error_cause *cause;
522339cbb549SMichael Tuexen 				int len;
5224f8829a4aSRandall Stewart 
522586eda749SMichael Tuexen 				op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_gen_error_cause),
5226eb1b1807SGleb Smirnoff 				    0, M_NOWAIT, 1, MT_DATA);
522786eda749SMichael Tuexen 				if (op_err != NULL) {
522839cbb549SMichael Tuexen 					len = min(SCTP_SIZE32(chk_length), (uint32_t)(length - *offset));
522986eda749SMichael Tuexen 					cause = mtod(op_err, struct sctp_gen_error_cause *);
523086eda749SMichael Tuexen 					cause->code = htons(SCTP_CAUSE_UNRECOG_CHUNK);
52319a8e3088SMichael Tuexen 					cause->length = htons((uint16_t)(len + sizeof(struct sctp_gen_error_cause)));
523286eda749SMichael Tuexen 					SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
523386eda749SMichael Tuexen 					SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(m, *offset, len, M_NOWAIT);
523486eda749SMichael Tuexen 					if (SCTP_BUF_NEXT(op_err) != NULL) {
5235eadccaccSRandall Stewart #ifdef SCTP_MBUF_LOGGING
5236b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
523730811e70SMichael Tuexen 							sctp_log_mbc(SCTP_BUF_NEXT(op_err), SCTP_MBUF_ICOPY);
5238eadccaccSRandall Stewart 						}
5239eadccaccSRandall Stewart #endif
524086eda749SMichael Tuexen 						sctp_queue_op_err(stcb, op_err);
5241f8829a4aSRandall Stewart 					} else {
524286eda749SMichael Tuexen 						sctp_m_freem(op_err);
5243f8829a4aSRandall Stewart 					}
5244f8829a4aSRandall Stewart 				}
5245f8829a4aSRandall Stewart 			}
5246f8829a4aSRandall Stewart 			if ((ch->chunk_type & 0x80) == 0) {
5247f8829a4aSRandall Stewart 				/* discard this packet */
5248f8829a4aSRandall Stewart 				*offset = length;
5249f8829a4aSRandall Stewart 				return (stcb);
5250b7b84c0eSMichael Tuexen 			}	/* else skip this bad chunk and continue... */
5251b7b84c0eSMichael Tuexen 			break;
5252f8829a4aSRandall Stewart 		}		/* switch (ch->chunk_type) */
5253f8829a4aSRandall Stewart 
5254f8829a4aSRandall Stewart next_chunk:
5255f8829a4aSRandall Stewart 		/* get the next chunk */
5256f8829a4aSRandall Stewart 		*offset += SCTP_SIZE32(chk_length);
5257f8829a4aSRandall Stewart 		if (*offset >= length) {
5258f8829a4aSRandall Stewart 			/* no more data left in the mbuf chain */
5259f8829a4aSRandall Stewart 			break;
5260f8829a4aSRandall Stewart 		}
5261f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5262f8829a4aSRandall Stewart 		    sizeof(struct sctp_chunkhdr), chunk_buf);
5263f8829a4aSRandall Stewart 		if (ch == NULL) {
5264f8829a4aSRandall Stewart 			*offset = length;
526580a2d140SMichael Tuexen 			return (stcb);
5266f8829a4aSRandall Stewart 		}
5267f8829a4aSRandall Stewart 	}			/* while */
52682afb3e84SRandall Stewart 
5269469a65d1SMichael Tuexen 	if ((asconf_cnt > 0) && (stcb != NULL)) {
52702afb3e84SRandall Stewart 		sctp_send_asconf_ack(stcb);
52712afb3e84SRandall Stewart 	}
5272f8829a4aSRandall Stewart 	return (stcb);
5273f8829a4aSRandall Stewart }
5274f8829a4aSRandall Stewart 
5275f8829a4aSRandall Stewart /*
5276f8829a4aSRandall Stewart  * common input chunk processing (v4 and v6)
5277f8829a4aSRandall Stewart  */
52786e55db54SRandall Stewart void
5279b1754ad1SMichael Tuexen sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
5280b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
5281b1754ad1SMichael Tuexen     struct sctphdr *sh, struct sctp_chunkhdr *ch,
5282a8775ad9SMichael Tuexen     uint8_t compute_crc,
5283a8775ad9SMichael Tuexen     uint8_t ecn_bits,
5284d089f9b9SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
5285f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
5286f8829a4aSRandall Stewart {
5287f8829a4aSRandall Stewart 	uint32_t high_tsn;
5288f8829a4aSRandall Stewart 	int fwd_tsn_seen = 0, data_processed = 0;
5289ff1ffd74SMichael Tuexen 	struct mbuf *m = *mm, *op_err;
5290ff1ffd74SMichael Tuexen 	char msg[SCTP_DIAG_INFO_LEN];
5291f8829a4aSRandall Stewart 	int un_sent;
5292493d8e5aSRandall Stewart 	int cnt_ctrl_ready = 0;
529355b175e7SMichael Tuexen 	struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
5294a8775ad9SMichael Tuexen 	struct sctp_tcb *stcb = NULL;
5295e3d6ef0bSMichael Tuexen 	struct sctp_nets *net = NULL;
5296f8829a4aSRandall Stewart 
5297f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvdatagrams);
5298f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
5299f8829a4aSRandall Stewart 	sctp_audit_log(0xE0, 1);
5300f8829a4aSRandall Stewart 	sctp_auditing(0, inp, stcb, net);
5301f8829a4aSRandall Stewart #endif
5302a8775ad9SMichael Tuexen 	if (compute_crc != 0) {
5303a8775ad9SMichael Tuexen 		uint32_t check, calc_check;
5304f8829a4aSRandall Stewart 
5305a8775ad9SMichael Tuexen 		check = sh->checksum;
5306a8775ad9SMichael Tuexen 		sh->checksum = 0;
5307a8775ad9SMichael Tuexen 		calc_check = sctp_calculate_cksum(m, iphlen);
5308a8775ad9SMichael Tuexen 		sh->checksum = check;
5309a8775ad9SMichael Tuexen 		if (calc_check != check) {
5310a8775ad9SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x  m:%p mlen:%d iphlen:%d\n",
5311dd294dceSMichael Tuexen 			    calc_check, check, (void *)m, length, iphlen);
5312a8775ad9SMichael Tuexen 			stcb = sctp_findassociation_addr(m, offset, src, dst,
5313a8775ad9SMichael Tuexen 			    sh, ch, &inp, &net, vrf_id);
53144474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
53153cf729a9SMichael Tuexen 			if ((ch->chunk_type != SCTP_INITIATION) &&
53163cf729a9SMichael Tuexen 			    (net != NULL) && (net->port != port)) {
5317a8775ad9SMichael Tuexen 				if (net->port == 0) {
53183cf729a9SMichael Tuexen 					/* UDP encapsulation turned on. */
53193cf729a9SMichael Tuexen 					net->mtu -= sizeof(struct udphdr);
53203cf729a9SMichael Tuexen 					if (stcb->asoc.smallest_mtu > net->mtu) {
53213cf729a9SMichael Tuexen 						sctp_pathmtu_adjustment(stcb, net->mtu);
53223cf729a9SMichael Tuexen 					}
53233cf729a9SMichael Tuexen 				} else if (port == 0) {
53243cf729a9SMichael Tuexen 					/* UDP encapsulation turned off. */
53253cf729a9SMichael Tuexen 					net->mtu += sizeof(struct udphdr);
53263cf729a9SMichael Tuexen 					/* XXX Update smallest_mtu */
5327a8775ad9SMichael Tuexen 				}
5328a8775ad9SMichael Tuexen 				net->port = port;
5329a8775ad9SMichael Tuexen 			}
53304474d71aSMichael Tuexen #endif
53315fe29cdfSMichael Tuexen 			if (net != NULL) {
5332457b4b88SMichael Tuexen 				net->flowtype = mflowtype;
53335fe29cdfSMichael Tuexen 				net->flowid = mflowid;
5334a8775ad9SMichael Tuexen 			}
53351e88cc8bSMichael Tuexen 			SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5336a8775ad9SMichael Tuexen 			if ((inp != NULL) && (stcb != NULL)) {
5337a8775ad9SMichael Tuexen 				sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
5338a8775ad9SMichael Tuexen 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5339a8775ad9SMichael Tuexen 			} else if ((inp != NULL) && (stcb == NULL)) {
5340a8775ad9SMichael Tuexen 				inp_decr = inp;
5341a8775ad9SMichael Tuexen 			}
5342a8775ad9SMichael Tuexen 			SCTP_STAT_INCR(sctps_badsum);
5343a8775ad9SMichael Tuexen 			SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5344a8775ad9SMichael Tuexen 			goto out;
5345a8775ad9SMichael Tuexen 		}
5346a8775ad9SMichael Tuexen 	}
5347a8775ad9SMichael Tuexen 	/* Destination port of 0 is illegal, based on RFC4960. */
5348a8775ad9SMichael Tuexen 	if (sh->dest_port == 0) {
5349a8775ad9SMichael Tuexen 		SCTP_STAT_INCR(sctps_hdrops);
5350a8775ad9SMichael Tuexen 		goto out;
5351a8775ad9SMichael Tuexen 	}
5352a8775ad9SMichael Tuexen 	stcb = sctp_findassociation_addr(m, offset, src, dst,
5353a8775ad9SMichael Tuexen 	    sh, ch, &inp, &net, vrf_id);
53544474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
53553cf729a9SMichael Tuexen 	if ((ch->chunk_type != SCTP_INITIATION) &&
53563cf729a9SMichael Tuexen 	    (net != NULL) && (net->port != port)) {
5357a8775ad9SMichael Tuexen 		if (net->port == 0) {
53583cf729a9SMichael Tuexen 			/* UDP encapsulation turned on. */
53593cf729a9SMichael Tuexen 			net->mtu -= sizeof(struct udphdr);
53603cf729a9SMichael Tuexen 			if (stcb->asoc.smallest_mtu > net->mtu) {
53613cf729a9SMichael Tuexen 				sctp_pathmtu_adjustment(stcb, net->mtu);
53623cf729a9SMichael Tuexen 			}
53633cf729a9SMichael Tuexen 		} else if (port == 0) {
53643cf729a9SMichael Tuexen 			/* UDP encapsulation turned off. */
53653cf729a9SMichael Tuexen 			net->mtu += sizeof(struct udphdr);
53663cf729a9SMichael Tuexen 			/* XXX Update smallest_mtu */
5367a8775ad9SMichael Tuexen 		}
5368a8775ad9SMichael Tuexen 		net->port = port;
5369a8775ad9SMichael Tuexen 	}
53704474d71aSMichael Tuexen #endif
53715fe29cdfSMichael Tuexen 	if (net != NULL) {
5372457b4b88SMichael Tuexen 		net->flowtype = mflowtype;
53735fe29cdfSMichael Tuexen 		net->flowid = mflowid;
5374a8775ad9SMichael Tuexen 	}
5375a8775ad9SMichael Tuexen 	if (inp == NULL) {
53761e88cc8bSMichael Tuexen 		SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5377a8775ad9SMichael Tuexen 		SCTP_STAT_INCR(sctps_noport);
5378a8775ad9SMichael Tuexen 		if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
5379a8775ad9SMichael Tuexen 			goto out;
5380a8775ad9SMichael Tuexen 		}
5381a8775ad9SMichael Tuexen 		if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5382a8775ad9SMichael Tuexen 			sctp_send_shutdown_complete2(src, dst, sh,
5383d089f9b9SMichael Tuexen 			    mflowtype, mflowid, fibnum,
5384a8775ad9SMichael Tuexen 			    vrf_id, port);
5385a8775ad9SMichael Tuexen 			goto out;
5386a8775ad9SMichael Tuexen 		}
5387a8775ad9SMichael Tuexen 		if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5388a8775ad9SMichael Tuexen 			goto out;
5389a8775ad9SMichael Tuexen 		}
5390a8775ad9SMichael Tuexen 		if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
5391a8775ad9SMichael Tuexen 			if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
5392a8775ad9SMichael Tuexen 			    ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
5393a8775ad9SMichael Tuexen 			    (ch->chunk_type != SCTP_INIT))) {
5394ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5395ff1ffd74SMichael Tuexen 				    "Out of the blue");
5396a8775ad9SMichael Tuexen 				sctp_send_abort(m, iphlen, src, dst,
5397ff1ffd74SMichael Tuexen 				    sh, 0, op_err,
5398d089f9b9SMichael Tuexen 				    mflowtype, mflowid, fibnum,
5399a8775ad9SMichael Tuexen 				    vrf_id, port);
5400a8775ad9SMichael Tuexen 			}
5401a8775ad9SMichael Tuexen 		}
5402a8775ad9SMichael Tuexen 		goto out;
5403a8775ad9SMichael Tuexen 	} else if (stcb == NULL) {
5404a8775ad9SMichael Tuexen 		inp_decr = inp;
5405a8775ad9SMichael Tuexen 	}
5406b3f1ea41SRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5407dd294dceSMichael Tuexen 	    (void *)m, iphlen, offset, length, (void *)stcb);
5408f8829a4aSRandall Stewart 	if (stcb) {
5409f8829a4aSRandall Stewart 		/* always clear this before beginning a packet */
5410f8829a4aSRandall Stewart 		stcb->asoc.authenticated = 0;
5411f8829a4aSRandall Stewart 		stcb->asoc.seen_a_sack_this_pkt = 0;
54122afb3e84SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5413dd294dceSMichael Tuexen 		    (void *)stcb, stcb->asoc.state);
54142afb3e84SRandall Stewart 
5415c4739e2fSRandall Stewart 		if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5416c4739e2fSRandall Stewart 		    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
541763981c2bSRandall Stewart 			/*-
541863981c2bSRandall Stewart 			 * If we hit here, we had a ref count
541963981c2bSRandall Stewart 			 * up when the assoc was aborted and the
542063981c2bSRandall Stewart 			 * timer is clearing out the assoc, we should
542163981c2bSRandall Stewart 			 * NOT respond to any packet.. its OOTB.
542263981c2bSRandall Stewart 			 */
542363981c2bSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
5424a8775ad9SMichael Tuexen 			stcb = NULL;
54251e88cc8bSMichael Tuexen 			SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5426999f86d6SMichael Tuexen 			SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5427ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5428ff1ffd74SMichael Tuexen 			    msg);
5429ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5430d089f9b9SMichael Tuexen 			    mflowtype, mflowid, inp->fibnum,
5431c54a18d2SRandall Stewart 			    vrf_id, port);
5432a8775ad9SMichael Tuexen 			goto out;
543363981c2bSRandall Stewart 		}
5434f8829a4aSRandall Stewart 	}
5435f8829a4aSRandall Stewart 	if (IS_SCTP_CONTROL(ch)) {
5436f8829a4aSRandall Stewart 		/* process the control portion of the SCTP packet */
54373c503c28SRandall Stewart 		/* sa_ignore NO_NULL_CHK */
5438b1754ad1SMichael Tuexen 		stcb = sctp_process_control(m, iphlen, &offset, length,
5439b1754ad1SMichael Tuexen 		    src, dst, sh, ch,
5440f30ac432SMichael Tuexen 		    inp, stcb, &net, &fwd_tsn_seen,
5441d089f9b9SMichael Tuexen 		    mflowtype, mflowid, fibnum,
5442f30ac432SMichael Tuexen 		    vrf_id, port);
5443f8829a4aSRandall Stewart 		if (stcb) {
5444f8829a4aSRandall Stewart 			/*
5445f8829a4aSRandall Stewart 			 * This covers us if the cookie-echo was there and
5446f8829a4aSRandall Stewart 			 * it changes our INP.
5447f8829a4aSRandall Stewart 			 */
5448f8829a4aSRandall Stewart 			inp = stcb->sctp_ep;
54494474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
54503cf729a9SMichael Tuexen 			if ((ch->chunk_type != SCTP_INITIATION) &&
54513cf729a9SMichael Tuexen 			    (net != NULL) && (net->port != port)) {
5452b3f1ea41SRandall Stewart 				if (net->port == 0) {
54533cf729a9SMichael Tuexen 					/* UDP encapsulation turned on. */
54543cf729a9SMichael Tuexen 					net->mtu -= sizeof(struct udphdr);
54553cf729a9SMichael Tuexen 					if (stcb->asoc.smallest_mtu > net->mtu) {
54563cf729a9SMichael Tuexen 						sctp_pathmtu_adjustment(stcb, net->mtu);
54573cf729a9SMichael Tuexen 					}
54583cf729a9SMichael Tuexen 				} else if (port == 0) {
54593cf729a9SMichael Tuexen 					/* UDP encapsulation turned off. */
54603cf729a9SMichael Tuexen 					net->mtu += sizeof(struct udphdr);
54613cf729a9SMichael Tuexen 					/* XXX Update smallest_mtu */
5462b3f1ea41SRandall Stewart 				}
5463b3f1ea41SRandall Stewart 				net->port = port;
5464b3f1ea41SRandall Stewart 			}
54654474d71aSMichael Tuexen #endif
5466f8829a4aSRandall Stewart 		}
5467f8829a4aSRandall Stewart 	} else {
5468f8829a4aSRandall Stewart 		/*
5469f8829a4aSRandall Stewart 		 * no control chunks, so pre-process DATA chunks (these
5470f8829a4aSRandall Stewart 		 * checks are taken care of by control processing)
5471f8829a4aSRandall Stewart 		 */
5472f8829a4aSRandall Stewart 
5473f8829a4aSRandall Stewart 		/*
5474f8829a4aSRandall Stewart 		 * if DATA only packet, and auth is required, then punt...
5475f8829a4aSRandall Stewart 		 * can't have authenticated without any AUTH (control)
5476f8829a4aSRandall Stewart 		 * chunks
5477f8829a4aSRandall Stewart 		 */
5478b3f1ea41SRandall Stewart 		if ((stcb != NULL) &&
5479b3f1ea41SRandall Stewart 		    sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5480f8829a4aSRandall Stewart 			/* "silently" ignore */
54811e88cc8bSMichael Tuexen 			SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5482f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvauthmissing);
5483a8775ad9SMichael Tuexen 			goto out;
5484f8829a4aSRandall Stewart 		}
5485f8829a4aSRandall Stewart 		if (stcb == NULL) {
5486f8829a4aSRandall Stewart 			/* out of the blue DATA chunk */
54871e88cc8bSMichael Tuexen 			SCTP_PROBE5(receive, NULL, NULL, m, NULL, sh);
5488999f86d6SMichael Tuexen 			SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5489ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5490ff1ffd74SMichael Tuexen 			    msg);
5491ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5492d089f9b9SMichael Tuexen 			    mflowtype, mflowid, fibnum,
5493c54a18d2SRandall Stewart 			    vrf_id, port);
5494a8775ad9SMichael Tuexen 			goto out;
5495f8829a4aSRandall Stewart 		}
5496f8829a4aSRandall Stewart 		if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5497f8829a4aSRandall Stewart 			/* v_tag mismatch! */
54981e88cc8bSMichael Tuexen 			SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5499f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_badvtag);
5500a8775ad9SMichael Tuexen 			goto out;
5501f8829a4aSRandall Stewart 		}
5502f8829a4aSRandall Stewart 	}
5503f8829a4aSRandall Stewart 
55041e88cc8bSMichael Tuexen 	SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5505f8829a4aSRandall Stewart 	if (stcb == NULL) {
5506f8829a4aSRandall Stewart 		/*
5507f8829a4aSRandall Stewart 		 * no valid TCB for this packet, or we found it's a bad
5508f8829a4aSRandall Stewart 		 * packet while processing control, or we're done with this
5509f8829a4aSRandall Stewart 		 * packet (done or skip rest of data), so we drop it...
5510f8829a4aSRandall Stewart 		 */
5511a8775ad9SMichael Tuexen 		goto out;
5512f8829a4aSRandall Stewart 	}
55130053ed28SMichael Tuexen 
5514f8829a4aSRandall Stewart 	/*
5515f8829a4aSRandall Stewart 	 * DATA chunk processing
5516f8829a4aSRandall Stewart 	 */
5517f8829a4aSRandall Stewart 	/* plow through the data chunks while length > offset */
5518f8829a4aSRandall Stewart 
5519f8829a4aSRandall Stewart 	/*
5520f8829a4aSRandall Stewart 	 * Rest should be DATA only.  Check authentication state if AUTH for
5521f8829a4aSRandall Stewart 	 * DATA is required.
5522f8829a4aSRandall Stewart 	 */
5523b3f1ea41SRandall Stewart 	if ((length > offset) &&
5524b3f1ea41SRandall Stewart 	    (stcb != NULL) &&
5525b3f1ea41SRandall Stewart 	    sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5526f8829a4aSRandall Stewart 	    !stcb->asoc.authenticated) {
5527f8829a4aSRandall Stewart 		/* "silently" ignore */
5528f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvauthmissing);
5529ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_AUTH1,
5530ad81507eSRandall Stewart 		    "Data chunk requires AUTH, skipped\n");
5531a5d547adSRandall Stewart 		goto trigger_send;
5532f8829a4aSRandall Stewart 	}
5533f8829a4aSRandall Stewart 	if (length > offset) {
5534f8829a4aSRandall Stewart 		int retval;
5535f8829a4aSRandall Stewart 
5536f8829a4aSRandall Stewart 		/*
5537f8829a4aSRandall Stewart 		 * First check to make sure our state is correct. We would
5538f8829a4aSRandall Stewart 		 * not get here unless we really did have a tag, so we don't
5539f8829a4aSRandall Stewart 		 * abort if this happens, just dump the chunk silently.
5540f8829a4aSRandall Stewart 		 */
5541839d21d6SMichael Tuexen 		switch (SCTP_GET_STATE(stcb)) {
5542f8829a4aSRandall Stewart 		case SCTP_STATE_COOKIE_ECHOED:
5543f8829a4aSRandall Stewart 			/*
5544f8829a4aSRandall Stewart 			 * we consider data with valid tags in this state
5545f8829a4aSRandall Stewart 			 * shows us the cookie-ack was lost. Imply it was
5546f8829a4aSRandall Stewart 			 * there.
5547f8829a4aSRandall Stewart 			 */
5548f8829a4aSRandall Stewart 			sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5549f8829a4aSRandall Stewart 			break;
5550f8829a4aSRandall Stewart 		case SCTP_STATE_COOKIE_WAIT:
5551f8829a4aSRandall Stewart 			/*
5552f8829a4aSRandall Stewart 			 * We consider OOTB any data sent during asoc setup.
5553f8829a4aSRandall Stewart 			 */
5554999f86d6SMichael Tuexen 			SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5555ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5556ff1ffd74SMichael Tuexen 			    msg);
5557ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5558d089f9b9SMichael Tuexen 			    mflowtype, mflowid, inp->fibnum,
5559c54a18d2SRandall Stewart 			    vrf_id, port);
5560a8775ad9SMichael Tuexen 			goto out;
556152be287eSRandall Stewart 			/* sa_ignore NOTREACHED */
5562f8829a4aSRandall Stewart 			break;
5563f8829a4aSRandall Stewart 		case SCTP_STATE_EMPTY:	/* should not happen */
5564f8829a4aSRandall Stewart 		case SCTP_STATE_INUSE:	/* should not happen */
5565f8829a4aSRandall Stewart 		case SCTP_STATE_SHUTDOWN_RECEIVED:	/* This is a peer error */
5566f8829a4aSRandall Stewart 		case SCTP_STATE_SHUTDOWN_ACK_SENT:
5567f8829a4aSRandall Stewart 		default:
5568a8775ad9SMichael Tuexen 			goto out;
556952be287eSRandall Stewart 			/* sa_ignore NOTREACHED */
5570f8829a4aSRandall Stewart 			break;
5571f8829a4aSRandall Stewart 		case SCTP_STATE_OPEN:
5572f8829a4aSRandall Stewart 		case SCTP_STATE_SHUTDOWN_SENT:
5573f8829a4aSRandall Stewart 			break;
5574f8829a4aSRandall Stewart 		}
5575f8829a4aSRandall Stewart 		/* plow through the data chunks while length > offset */
5576b1754ad1SMichael Tuexen 		retval = sctp_process_data(mm, iphlen, &offset, length,
5577e7e71dd7SMichael Tuexen 		    inp, stcb, net, &high_tsn);
5578f8829a4aSRandall Stewart 		if (retval == 2) {
5579f8829a4aSRandall Stewart 			/*
5580f8829a4aSRandall Stewart 			 * The association aborted, NO UNLOCK needed since
5581f8829a4aSRandall Stewart 			 * the association is destroyed.
5582f8829a4aSRandall Stewart 			 */
5583a8775ad9SMichael Tuexen 			stcb = NULL;
5584a8775ad9SMichael Tuexen 			goto out;
5585f8829a4aSRandall Stewart 		}
5586b954d816SMichael Tuexen 		if (retval == 0) {
5587f8829a4aSRandall Stewart 			data_processed = 1;
5588b954d816SMichael Tuexen 		}
5589f8829a4aSRandall Stewart 		/*
5590f8829a4aSRandall Stewart 		 * Anything important needs to have been m_copy'ed in
5591f8829a4aSRandall Stewart 		 * process_data
5592f8829a4aSRandall Stewart 		 */
5593f8829a4aSRandall Stewart 	}
55940053ed28SMichael Tuexen 
5595493d8e5aSRandall Stewart 	/* take care of ecn */
559660990c0cSMichael Tuexen 	if ((data_processed == 1) &&
5597f342355aSMichael Tuexen 	    (stcb->asoc.ecn_supported == 1) &&
5598c446091bSMichael Tuexen 	    ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5599493d8e5aSRandall Stewart 		/* Yep, we need to add a ECNE */
5600493d8e5aSRandall Stewart 		sctp_send_ecn_echo(stcb, net, high_tsn);
5601493d8e5aSRandall Stewart 	}
56020053ed28SMichael Tuexen 
5603f8829a4aSRandall Stewart 	if ((data_processed == 0) && (fwd_tsn_seen)) {
56048f777478SMichael Tuexen 		int was_a_gap;
56058f777478SMichael Tuexen 		uint32_t highest_tsn;
5606f8829a4aSRandall Stewart 
560720b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
56088f777478SMichael Tuexen 			highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
56098f777478SMichael Tuexen 		} else {
56108f777478SMichael Tuexen 			highest_tsn = stcb->asoc.highest_tsn_inside_map;
5611f8829a4aSRandall Stewart 		}
561220b07a4dSMichael Tuexen 		was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
56138933fa13SRandall Stewart 		stcb->asoc.send_sack = 1;
56147215cc1bSMichael Tuexen 		sctp_sack_check(stcb, was_a_gap);
56158933fa13SRandall Stewart 	} else if (fwd_tsn_seen) {
56168933fa13SRandall Stewart 		stcb->asoc.send_sack = 1;
5617f8829a4aSRandall Stewart 	}
5618f8829a4aSRandall Stewart 	/* trigger send of any chunks in queue... */
5619a5d547adSRandall Stewart trigger_send:
5620f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
5621f8829a4aSRandall Stewart 	sctp_audit_log(0xE0, 2);
5622f8829a4aSRandall Stewart 	sctp_auditing(1, inp, stcb, net);
5623f8829a4aSRandall Stewart #endif
5624ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT1,
5625ad81507eSRandall Stewart 	    "Check for chunk output prw:%d tqe:%d tf=%d\n",
5626f8829a4aSRandall Stewart 	    stcb->asoc.peers_rwnd,
5627f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5628f8829a4aSRandall Stewart 	    stcb->asoc.total_flight);
5629f8829a4aSRandall Stewart 	un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5630493d8e5aSRandall Stewart 	if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
5631493d8e5aSRandall Stewart 		cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
5632493d8e5aSRandall Stewart 	}
56335114dccbSMichael Tuexen 	if (!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue) ||
56345114dccbSMichael Tuexen 	    cnt_ctrl_ready ||
56355114dccbSMichael Tuexen 	    stcb->asoc.trigger_reset ||
563664c8fc5dSMichael Tuexen 	    ((un_sent > 0) &&
563764c8fc5dSMichael Tuexen 	    (stcb->asoc.peers_rwnd > 0 || stcb->asoc.total_flight == 0))) {
5638ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5639ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5640ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5641f8829a4aSRandall Stewart 	}
5642f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
5643f8829a4aSRandall Stewart 	sctp_audit_log(0xE0, 3);
5644f8829a4aSRandall Stewart 	sctp_auditing(2, inp, stcb, net);
5645f8829a4aSRandall Stewart #endif
5646a8775ad9SMichael Tuexen out:
5647a8775ad9SMichael Tuexen 	if (stcb != NULL) {
5648f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
5649a8775ad9SMichael Tuexen 	}
5650a8775ad9SMichael Tuexen 	if (inp_decr != NULL) {
5651a8775ad9SMichael Tuexen 		/* reduce ref-count */
5652a8775ad9SMichael Tuexen 		SCTP_INP_WLOCK(inp_decr);
5653a8775ad9SMichael Tuexen 		SCTP_INP_DECR_REF(inp_decr);
5654a8775ad9SMichael Tuexen 		SCTP_INP_WUNLOCK(inp_decr);
5655a8775ad9SMichael Tuexen 	}
56566e55db54SRandall Stewart 	return;
5657f8829a4aSRandall Stewart }
5658f8829a4aSRandall Stewart 
5659e6194c2eSMichael Tuexen #ifdef INET
5660f8829a4aSRandall Stewart void
5661af83f5d7SRoman Divacky sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
5662f8829a4aSRandall Stewart {
5663139bc87fSRandall Stewart 	struct mbuf *m;
5664f8829a4aSRandall Stewart 	int iphlen;
5665ad21a364SRandall Stewart 	uint32_t vrf_id = 0;
5666f8829a4aSRandall Stewart 	uint8_t ecn_bits;
5667b1754ad1SMichael Tuexen 	struct sockaddr_in src, dst;
5668f8829a4aSRandall Stewart 	struct ip *ip;
5669f8829a4aSRandall Stewart 	struct sctphdr *sh;
5670f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
56716dc5aabcSMichael Tuexen 	int length, offset;
5672a8775ad9SMichael Tuexen 	uint8_t compute_crc;
5673a8775ad9SMichael Tuexen 	uint32_t mflowid;
5674457b4b88SMichael Tuexen 	uint8_t mflowtype;
5675d089f9b9SMichael Tuexen 	uint16_t fibnum;
56769c7635e1SMichael Tuexen 
56776dc5aabcSMichael Tuexen 	iphlen = off;
567817205eccSRandall Stewart 	if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
567917205eccSRandall Stewart 		SCTP_RELEASE_PKT(i_pak);
568017205eccSRandall Stewart 		return;
568117205eccSRandall Stewart 	}
5682139bc87fSRandall Stewart 	m = SCTP_HEADER_TO_CHAIN(i_pak);
5683f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
5684f8829a4aSRandall Stewart 	/* Log in any input mbufs */
5685b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
56864be807c4SMichael Tuexen 		sctp_log_mbc(m, SCTP_MBUF_INPUT);
568780fefe0aSRandall Stewart 	}
5688f8829a4aSRandall Stewart #endif
5689207304d4SRandall Stewart #ifdef SCTP_PACKET_LOGGING
56906dc5aabcSMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
5691f9384252SMichael Tuexen 		sctp_packet_log(m);
56926dc5aabcSMichael Tuexen 	}
5693207304d4SRandall Stewart #endif
5694a8775ad9SMichael Tuexen 	SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
56951a94cdbeSMichael Tuexen 	    "sctp_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
5696a8775ad9SMichael Tuexen 	    m->m_pkthdr.len,
5697a8775ad9SMichael Tuexen 	    if_name(m->m_pkthdr.rcvif),
56981a94cdbeSMichael Tuexen 	    (int)m->m_pkthdr.csum_flags, CSUM_BITS);
5699f30ac432SMichael Tuexen 	mflowid = m->m_pkthdr.flowid;
5700457b4b88SMichael Tuexen 	mflowtype = M_HASHTYPE_GET(m);
5701d089f9b9SMichael Tuexen 	fibnum = M_GETFIB(m);
57026dc5aabcSMichael Tuexen 	SCTP_STAT_INCR(sctps_recvpackets);
57036dc5aabcSMichael Tuexen 	SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
57046dc5aabcSMichael Tuexen 	/* Get IP, SCTP, and first chunk header together in the first mbuf. */
57056dc5aabcSMichael Tuexen 	offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5706139bc87fSRandall Stewart 	if (SCTP_BUF_LEN(m) < offset) {
5707b1754ad1SMichael Tuexen 		if ((m = m_pullup(m, offset)) == NULL) {
5708f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_hdrops);
5709f8829a4aSRandall Stewart 			return;
5710f8829a4aSRandall Stewart 		}
5711f8829a4aSRandall Stewart 	}
5712b1754ad1SMichael Tuexen 	ip = mtod(m, struct ip *);
57136dc5aabcSMichael Tuexen 	sh = (struct sctphdr *)((caddr_t)ip + iphlen);
57146dc5aabcSMichael Tuexen 	ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
57156dc5aabcSMichael Tuexen 	offset -= sizeof(struct sctp_chunkhdr);
5716b1754ad1SMichael Tuexen 	memset(&src, 0, sizeof(struct sockaddr_in));
5717b1754ad1SMichael Tuexen 	src.sin_family = AF_INET;
5718b1754ad1SMichael Tuexen 	src.sin_len = sizeof(struct sockaddr_in);
5719b1754ad1SMichael Tuexen 	src.sin_port = sh->src_port;
5720b1754ad1SMichael Tuexen 	src.sin_addr = ip->ip_src;
5721b1754ad1SMichael Tuexen 	memset(&dst, 0, sizeof(struct sockaddr_in));
5722b1754ad1SMichael Tuexen 	dst.sin_family = AF_INET;
5723b1754ad1SMichael Tuexen 	dst.sin_len = sizeof(struct sockaddr_in);
5724b1754ad1SMichael Tuexen 	dst.sin_port = sh->dest_port;
5725b1754ad1SMichael Tuexen 	dst.sin_addr = ip->ip_dst;
57268ad458a4SGleb Smirnoff 	length = ntohs(ip->ip_len);
57276dc5aabcSMichael Tuexen 	/* Validate mbuf chain length with IP payload length. */
5728a8775ad9SMichael Tuexen 	if (SCTP_HEADER_LEN(m) != length) {
57296dc5aabcSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT1,
5730a8775ad9SMichael Tuexen 		    "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
5731a99b6783SRandall Stewart 		SCTP_STAT_INCR(sctps_hdrops);
5732a8775ad9SMichael Tuexen 		goto out;
5733a99b6783SRandall Stewart 	}
5734f8829a4aSRandall Stewart 	/* SCTP does not allow broadcasts or multicasts */
5735b1754ad1SMichael Tuexen 	if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
5736a8775ad9SMichael Tuexen 		goto out;
5737f8829a4aSRandall Stewart 	}
5738b1754ad1SMichael Tuexen 	if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
5739a8775ad9SMichael Tuexen 		goto out;
5740f8829a4aSRandall Stewart 	}
5741a8775ad9SMichael Tuexen 	ecn_bits = ip->ip_tos;
5742a99b6783SRandall Stewart 	if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
5743a99b6783SRandall Stewart 		SCTP_STAT_INCR(sctps_recvhwcrc);
5744a8775ad9SMichael Tuexen 		compute_crc = 0;
5745a8775ad9SMichael Tuexen 	} else {
5746a99b6783SRandall Stewart 		SCTP_STAT_INCR(sctps_recvswcrc);
5747a8775ad9SMichael Tuexen 		compute_crc = 1;
5748f8829a4aSRandall Stewart 	}
5749b1754ad1SMichael Tuexen 	sctp_common_input_processing(&m, iphlen, offset, length,
5750b1754ad1SMichael Tuexen 	    (struct sockaddr *)&src,
5751b1754ad1SMichael Tuexen 	    (struct sockaddr *)&dst,
5752a8775ad9SMichael Tuexen 	    sh, ch,
5753a8775ad9SMichael Tuexen 	    compute_crc,
5754a8775ad9SMichael Tuexen 	    ecn_bits,
5755d089f9b9SMichael Tuexen 	    mflowtype, mflowid, fibnum,
5756f30ac432SMichael Tuexen 	    vrf_id, port);
5757a8775ad9SMichael Tuexen out:
5758f8829a4aSRandall Stewart 	if (m) {
5759f8829a4aSRandall Stewart 		sctp_m_freem(m);
5760f8829a4aSRandall Stewart 	}
5761f8829a4aSRandall Stewart 	return;
5762f8829a4aSRandall Stewart }
5763bfc46083SRandall Stewart 
57642f9e6db0SMichael Tuexen #if defined(SCTP_MCORE_INPUT) && defined(SMP)
57650071ee5eSRandall Stewart extern int *sctp_cpuarry;
57660071ee5eSRandall Stewart #endif
5767bfc46083SRandall Stewart 
57688f5a8818SKevin Lo int
576982eaf95eSMichael Tuexen sctp_input(struct mbuf **mp, int *offp, int proto SCTP_UNUSED)
5770c54a18d2SRandall Stewart {
57718f5a8818SKevin Lo 	struct mbuf *m;
57728f5a8818SKevin Lo 	int off;
57738f5a8818SKevin Lo 
57748f5a8818SKevin Lo 	m = *mp;
57758f5a8818SKevin Lo 	off = *offp;
57762f9e6db0SMichael Tuexen #if defined(SCTP_MCORE_INPUT) && defined(SMP)
577782eaf95eSMichael Tuexen 	if (mp_ncpus > 1) {
5778bfc46083SRandall Stewart 		struct ip *ip;
5779bfc46083SRandall Stewart 		struct sctphdr *sh;
5780bfc46083SRandall Stewart 		int offset;
5781bfc46083SRandall Stewart 		int cpu_to_use;
578238521fb9SRandall Stewart 		uint32_t flowid, tag;
5783bfc46083SRandall Stewart 
5784457b4b88SMichael Tuexen 		if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
578538521fb9SRandall Stewart 			flowid = m->m_pkthdr.flowid;
578638521fb9SRandall Stewart 		} else {
578738521fb9SRandall Stewart 			/*
578838521fb9SRandall Stewart 			 * No flow id built by lower layers fix it so we
578938521fb9SRandall Stewart 			 * create one.
579038521fb9SRandall Stewart 			 */
5791b1754ad1SMichael Tuexen 			offset = off + sizeof(struct sctphdr);
5792bfc46083SRandall Stewart 			if (SCTP_BUF_LEN(m) < offset) {
5793b1754ad1SMichael Tuexen 				if ((m = m_pullup(m, offset)) == NULL) {
5794bfc46083SRandall Stewart 					SCTP_STAT_INCR(sctps_hdrops);
57958f5a8818SKevin Lo 					return (IPPROTO_DONE);
5796bfc46083SRandall Stewart 				}
5797bfc46083SRandall Stewart 			}
5798b1754ad1SMichael Tuexen 			ip = mtod(m, struct ip *);
5799bfc46083SRandall Stewart 			sh = (struct sctphdr *)((caddr_t)ip + off);
58000071ee5eSRandall Stewart 			tag = htonl(sh->v_tag);
580138521fb9SRandall Stewart 			flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
580238521fb9SRandall Stewart 			m->m_pkthdr.flowid = flowid;
580336ad8372SSepherosa Ziehau 			M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE_HASH);
58040071ee5eSRandall Stewart 		}
580538521fb9SRandall Stewart 		cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
5806bfc46083SRandall Stewart 		sctp_queue_to_mcore(m, off, cpu_to_use);
58078f5a8818SKevin Lo 		return (IPPROTO_DONE);
5808bfc46083SRandall Stewart 	}
5809bfc46083SRandall Stewart #endif
5810bfc46083SRandall Stewart 	sctp_input_with_port(m, off, 0);
58118f5a8818SKevin Lo 	return (IPPROTO_DONE);
5812c54a18d2SRandall Stewart }
5813e6194c2eSMichael Tuexen #endif
5814