xref: /freebsd/sys/netinet/sctp_input.c (revision 8ed1e2c88012cfca9fc4131a2637452cc9106b81)
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,
89eba8e643SMichael Tuexen     struct sctp_tcb *stcb, struct sctp_nets *net,
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;
103eba8e643SMichael Tuexen 	if (ntohl(init->initiate_tag) == 0) {
104eba8e643SMichael Tuexen 		goto outnow;
105eba8e643SMichael Tuexen 	}
106eba8e643SMichael Tuexen 	if ((ntohl(init->a_rwnd) < SCTP_MIN_RWND) ||
107059ec222SMichael Tuexen 	    (ntohs(init->num_inbound_streams) == 0) ||
108059ec222SMichael Tuexen 	    (ntohs(init->num_outbound_streams) == 0)) {
109f8829a4aSRandall Stewart 		/* protocol error... send abort */
110ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
111eba8e643SMichael Tuexen 		sctp_send_abort(m, iphlen, src, dst, sh, init->initiate_tag, op_err,
112eba8e643SMichael Tuexen 		    mflowtype, mflowid, inp->fibnum,
113c54a18d2SRandall Stewart 		    vrf_id, port);
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");
121eba8e643SMichael Tuexen 		sctp_send_abort(m, iphlen, src, dst, sh, init->initiate_tag, op_err,
122eba8e643SMichael Tuexen 		    mflowtype, mflowid, inp->fibnum,
123f30ac432SMichael Tuexen 		    vrf_id, port);
124d55b0b1bSRandall Stewart 		goto outnow;
125f8829a4aSRandall Stewart 	}
1265d08768aSMichael Tuexen 	/* We are only accepting if we have a listening socket. */
127d68fdc4dSMichael Tuexen 	if ((stcb == NULL) &&
128d68fdc4dSMichael Tuexen 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
129d68fdc4dSMichael Tuexen 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1305d08768aSMichael Tuexen 	    (!SCTP_IS_LISTENING(inp)))) {
131d68fdc4dSMichael Tuexen 		/*
132d68fdc4dSMichael Tuexen 		 * FIX ME ?? What about TCP model and we have a
133d68fdc4dSMichael Tuexen 		 * match/restart case? Actually no fix is needed. the lookup
134d68fdc4dSMichael Tuexen 		 * will always find the existing assoc so stcb would not be
135d68fdc4dSMichael Tuexen 		 * NULL. It may be questionable to do this since we COULD
136d68fdc4dSMichael Tuexen 		 * just send back the INIT-ACK and hope that the app did
137d68fdc4dSMichael Tuexen 		 * accept()'s by the time the COOKIE was sent. But there is
138d68fdc4dSMichael Tuexen 		 * a price to pay for COOKIE generation and I don't want to
139d68fdc4dSMichael Tuexen 		 * pay it on the chance that the app will actually do some
140d68fdc4dSMichael Tuexen 		 * accepts(). The App just looses and should NOT be in this
141d68fdc4dSMichael Tuexen 		 * state :-)
142d68fdc4dSMichael Tuexen 		 */
143c58e60beSMichael Tuexen 		if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) {
144ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
145ff1ffd74SMichael Tuexen 			    "No listener");
146ff1ffd74SMichael Tuexen 			sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
147d089f9b9SMichael Tuexen 			    mflowtype, mflowid, inp->fibnum,
148f30ac432SMichael Tuexen 			    vrf_id, port);
149c58e60beSMichael Tuexen 		}
150d68fdc4dSMichael Tuexen 		goto outnow;
151d68fdc4dSMichael Tuexen 	}
152d68fdc4dSMichael Tuexen 	if ((stcb != NULL) &&
153839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
154d68fdc4dSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n");
155d68fdc4dSMichael Tuexen 		sctp_send_shutdown_ack(stcb, NULL);
156d68fdc4dSMichael Tuexen 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
157d68fdc4dSMichael Tuexen 	} else {
158ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
159ca83f93cSMichael Tuexen 		sctp_send_initiate_ack(inp, stcb, net, m, iphlen, offset,
160ca83f93cSMichael Tuexen 		    src, dst, sh, cp,
161457b4b88SMichael Tuexen 		    mflowtype, mflowid,
162cdd2d7d4SMichael Tuexen 		    vrf_id, port);
163d68fdc4dSMichael Tuexen 	}
164d55b0b1bSRandall Stewart outnow:
165d55b0b1bSRandall Stewart 	if (stcb == NULL) {
166d55b0b1bSRandall Stewart 		SCTP_INP_RUNLOCK(inp);
167d55b0b1bSRandall Stewart 	}
168f8829a4aSRandall Stewart }
169f8829a4aSRandall Stewart 
170f8829a4aSRandall Stewart /*
171f8829a4aSRandall Stewart  * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
172f8829a4aSRandall Stewart  */
1735bead436SRandall Stewart 
1745bead436SRandall Stewart int
17528397ac1SMichael Tuexen sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked)
1765bead436SRandall Stewart {
177124d851aSMichael Tuexen 	int unsent_data;
178f7a77f6fSMichael Tuexen 	unsigned int i;
179f7a77f6fSMichael Tuexen 	struct sctp_stream_queue_pending *sp;
1805bead436SRandall Stewart 	struct sctp_association *asoc;
1815bead436SRandall Stewart 
1825ac91821SMichael Tuexen 	SCTP_TCB_LOCK_ASSERT(stcb);
1835ac91821SMichael Tuexen 
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;
191f7a77f6fSMichael Tuexen 	if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
192f7a77f6fSMichael Tuexen 		/* Check to see if some data queued */
193f7a77f6fSMichael Tuexen 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
19452be287eSRandall Stewart 			/* sa_ignore FREED_MEMORY */
195f7a77f6fSMichael Tuexen 			sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue);
196f7a77f6fSMichael Tuexen 			if (sp == NULL) {
197f7a77f6fSMichael Tuexen 				continue;
198f7a77f6fSMichael Tuexen 			}
1995bead436SRandall Stewart 			if ((sp->msg_is_complete) &&
2005bead436SRandall Stewart 			    (sp->length == 0) &&
2015bead436SRandall Stewart 			    (sp->sender_all_done)) {
2025bead436SRandall Stewart 				/*
2035bead436SRandall Stewart 				 * We are doing differed cleanup. Last time
2045bead436SRandall Stewart 				 * through when we took all the data the
2055bead436SRandall Stewart 				 * sender_all_done was not set.
2065bead436SRandall Stewart 				 */
2075bead436SRandall Stewart 				if (sp->put_last_out == 0) {
2085bead436SRandall Stewart 					SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
2095bead436SRandall Stewart 					SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
2105bead436SRandall Stewart 					    sp->sender_all_done,
2115bead436SRandall Stewart 					    sp->length,
2125bead436SRandall Stewart 					    sp->msg_is_complete,
2135bead436SRandall Stewart 					    sp->put_last_out);
2145bead436SRandall Stewart 				}
215f7a77f6fSMichael Tuexen 				atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
216f7a77f6fSMichael Tuexen 				TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next);
217762ae0ecSMichael Tuexen 				stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, &asoc->strmout[i], sp);
2189eea4a2dSMichael Tuexen 				if (sp->net) {
2195bead436SRandall Stewart 					sctp_free_remote_addr(sp->net);
2209eea4a2dSMichael Tuexen 					sp->net = NULL;
2219eea4a2dSMichael Tuexen 				}
2225bead436SRandall Stewart 				if (sp->data) {
2235bead436SRandall Stewart 					sctp_m_freem(sp->data);
2245bead436SRandall Stewart 					sp->data = NULL;
2255bead436SRandall Stewart 				}
226689e6a5fSMichael Tuexen 				sctp_free_a_strmoq(stcb, sp, so_locked);
227124d851aSMichael Tuexen 				if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
228124d851aSMichael Tuexen 					unsent_data++;
229124d851aSMichael Tuexen 				}
2305bead436SRandall Stewart 			} else {
2315bead436SRandall Stewart 				unsent_data++;
232124d851aSMichael Tuexen 			}
233124d851aSMichael Tuexen 			if (unsent_data > 0) {
2344a9ef3f8SMichael Tuexen 				break;
2355bead436SRandall Stewart 			}
2365bead436SRandall Stewart 		}
2375bead436SRandall Stewart 	}
2385bead436SRandall Stewart 	return (unsent_data);
2395bead436SRandall Stewart }
2405bead436SRandall Stewart 
241f8829a4aSRandall Stewart static int
2427215cc1bSMichael Tuexen sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb)
243f8829a4aSRandall Stewart {
244f8829a4aSRandall Stewart 	struct sctp_init *init;
245f8829a4aSRandall Stewart 	struct sctp_association *asoc;
246f8829a4aSRandall Stewart 	struct sctp_nets *lnet;
247f8829a4aSRandall Stewart 	unsigned int i;
248f8829a4aSRandall Stewart 
2495ac91821SMichael Tuexen 	SCTP_TCB_LOCK_ASSERT(stcb);
2505ac91821SMichael Tuexen 
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 	}
268f8829a4aSRandall Stewart 	if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
269f8829a4aSRandall Stewart 		unsigned int newcnt;
270f8829a4aSRandall Stewart 		struct sctp_stream_out *outs;
2714a9ef3f8SMichael Tuexen 		struct sctp_stream_queue_pending *sp, *nsp;
2724a9ef3f8SMichael Tuexen 		struct sctp_tmit_chunk *chk, *nchk;
273f8829a4aSRandall Stewart 
274810ec536SMichael Tuexen 		/* abandon the upper streams */
275f8829a4aSRandall Stewart 		newcnt = ntohs(init->num_inbound_streams);
2764a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
27749656eefSMichael Tuexen 			if (chk->rec.data.sid >= newcnt) {
278810ec536SMichael Tuexen 				TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
279810ec536SMichael Tuexen 				asoc->send_queue_cnt--;
28049656eefSMichael Tuexen 				if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
28149656eefSMichael Tuexen 					asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
282a7ad6026SMichael Tuexen #ifdef INVARIANTS
283a7ad6026SMichael Tuexen 				} else {
28449656eefSMichael Tuexen 					panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
285a7ad6026SMichael Tuexen #endif
286a7ad6026SMichael Tuexen 				}
287810ec536SMichael Tuexen 				if (chk->data != NULL) {
288810ec536SMichael Tuexen 					sctp_free_bufspace(stcb, asoc, chk, 1);
2891edc9dbaSMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
2901edc9dbaSMichael Tuexen 					    0, chk, SCTP_SO_NOT_LOCKED);
291810ec536SMichael Tuexen 					if (chk->data) {
292810ec536SMichael Tuexen 						sctp_m_freem(chk->data);
293810ec536SMichael Tuexen 						chk->data = NULL;
294810ec536SMichael Tuexen 					}
295810ec536SMichael Tuexen 				}
296689e6a5fSMichael Tuexen 				sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
297810ec536SMichael Tuexen 				/* sa_ignore FREED_MEMORY */
298810ec536SMichael Tuexen 			}
299810ec536SMichael Tuexen 		}
300f8829a4aSRandall Stewart 		if (asoc->strmout) {
301f8829a4aSRandall Stewart 			for (i = newcnt; i < asoc->pre_open_streams; i++) {
302f8829a4aSRandall Stewart 				outs = &asoc->strmout[i];
3034a9ef3f8SMichael Tuexen 				TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
3044d58b0c3SMichael Tuexen 					atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
305810ec536SMichael Tuexen 					TAILQ_REMOVE(&outs->outqueue, sp, next);
306762ae0ecSMichael Tuexen 					stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp);
307f8829a4aSRandall Stewart 					sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
3081edc9dbaSMichael Tuexen 					    stcb, 0, sp, SCTP_SO_NOT_LOCKED);
309f8829a4aSRandall Stewart 					if (sp->data) {
310f8829a4aSRandall Stewart 						sctp_m_freem(sp->data);
311f8829a4aSRandall Stewart 						sp->data = NULL;
312f8829a4aSRandall Stewart 					}
3139eea4a2dSMichael Tuexen 					if (sp->net) {
314f8829a4aSRandall Stewart 						sctp_free_remote_addr(sp->net);
315f8829a4aSRandall Stewart 						sp->net = NULL;
3169eea4a2dSMichael Tuexen 					}
317f8829a4aSRandall Stewart 					/* Free the chunk */
318689e6a5fSMichael Tuexen 					sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED);
3193c503c28SRandall Stewart 					/* sa_ignore FREED_MEMORY */
320f8829a4aSRandall Stewart 				}
3217cca1775SRandall Stewart 				outs->state = SCTP_STREAM_CLOSED;
322f8829a4aSRandall Stewart 			}
323f8829a4aSRandall Stewart 		}
324810ec536SMichael Tuexen 		/* cut back the count */
325f8829a4aSRandall Stewart 		asoc->pre_open_streams = newcnt;
326f8829a4aSRandall Stewart 	}
3277cca1775SRandall Stewart 	asoc->streamoutcnt = asoc->pre_open_streams;
328fdc4c9d0SMichael Tuexen 	if (asoc->strmout) {
3297cca1775SRandall Stewart 		for (i = 0; i < asoc->streamoutcnt; i++) {
3307cca1775SRandall Stewart 			asoc->strmout[i].state = SCTP_STREAM_OPEN;
3317cca1775SRandall Stewart 		}
332fdc4c9d0SMichael Tuexen 	}
333830d754dSRandall Stewart 	/* EY - nr_sack: initialize highest tsn in nr_mapping_array */
334830d754dSRandall Stewart 	asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
335b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
336f8829a4aSRandall Stewart 		sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
33780fefe0aSRandall Stewart 	}
338f8829a4aSRandall Stewart 	/* This is the next one we expect */
339f8829a4aSRandall Stewart 	asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
340f8829a4aSRandall Stewart 
341f8829a4aSRandall Stewart 	asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
342d6af161aSRandall Stewart 	asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
343493d8e5aSRandall Stewart 
344f8829a4aSRandall Stewart 	asoc->advanced_peer_ack_point = asoc->last_acked_seq;
345f8829a4aSRandall Stewart 	/* open the requested streams */
346207304d4SRandall Stewart 
347f8829a4aSRandall Stewart 	if (asoc->strmin != NULL) {
348f8829a4aSRandall Stewart 		/* Free the old ones */
3496a91f103SRandall Stewart 		for (i = 0; i < asoc->streamincnt; i++) {
35044249214SRandall Stewart 			sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue);
35144249214SRandall Stewart 			sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue);
3526a91f103SRandall Stewart 		}
353207304d4SRandall Stewart 		SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
354f8829a4aSRandall Stewart 	}
355ee1ccd92SMichael Tuexen 	if (asoc->max_inbound_streams > ntohs(init->num_outbound_streams)) {
3566a91f103SRandall Stewart 		asoc->streamincnt = ntohs(init->num_outbound_streams);
357ee1ccd92SMichael Tuexen 	} else {
358ee1ccd92SMichael Tuexen 		asoc->streamincnt = asoc->max_inbound_streams;
3596a91f103SRandall Stewart 	}
360f8829a4aSRandall Stewart 	SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
361207304d4SRandall Stewart 	    sizeof(struct sctp_stream_in), SCTP_M_STRMI);
362f8829a4aSRandall Stewart 	if (asoc->strmin == NULL) {
363f8829a4aSRandall Stewart 		/* we didn't get memory for the streams! */
364ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
365f8829a4aSRandall Stewart 		return (-1);
366f8829a4aSRandall Stewart 	}
367f8829a4aSRandall Stewart 	for (i = 0; i < asoc->streamincnt; i++) {
36849656eefSMichael Tuexen 		asoc->strmin[i].sid = i;
36949656eefSMichael Tuexen 		asoc->strmin[i].last_mid_delivered = 0xffffffff;
370f8829a4aSRandall Stewart 		TAILQ_INIT(&asoc->strmin[i].inqueue);
37144249214SRandall Stewart 		TAILQ_INIT(&asoc->strmin[i].uno_inqueue);
37244249214SRandall Stewart 		asoc->strmin[i].pd_api_started = 0;
3739a6142d8SRandall Stewart 		asoc->strmin[i].delivery_started = 0;
374f8829a4aSRandall Stewart 	}
375f8829a4aSRandall Stewart 	/*
376f8829a4aSRandall Stewart 	 * load_address_from_init will put the addresses into the
377f8829a4aSRandall Stewart 	 * association when the COOKIE is processed or the INIT-ACK is
378f8829a4aSRandall Stewart 	 * processed. Both types of COOKIE's existing and new call this
379f8829a4aSRandall Stewart 	 * routine. It will remove addresses that are no longer in the
380f8829a4aSRandall Stewart 	 * association (for the restarting case where addresses are
381f8829a4aSRandall Stewart 	 * removed). Up front when the INIT arrives we will discard it if it
382f8829a4aSRandall Stewart 	 * is a restart and new addresses have been added.
383f8829a4aSRandall Stewart 	 */
3843c503c28SRandall Stewart 	/* sa_ignore MEMLEAK */
385f8829a4aSRandall Stewart 	return (0);
386f8829a4aSRandall Stewart }
387f8829a4aSRandall Stewart 
388f8829a4aSRandall Stewart /*
389f8829a4aSRandall Stewart  * INIT-ACK message processing/consumption returns value < 0 on error
390f8829a4aSRandall Stewart  */
391f8829a4aSRandall Stewart static int
392b1754ad1SMichael Tuexen sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
393b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
394f30ac432SMichael Tuexen     struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
395f30ac432SMichael Tuexen     struct sctp_nets *net, int *abort_no_unlock,
396457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
397f30ac432SMichael Tuexen     uint32_t vrf_id)
398f8829a4aSRandall Stewart {
399f8829a4aSRandall Stewart 	struct sctp_association *asoc;
400f8829a4aSRandall Stewart 	struct mbuf *op_err;
4016182677fSMichael Tuexen 	int retval, abort_flag, cookie_found;
4026182677fSMichael Tuexen 	int initack_limit;
403830d754dSRandall Stewart 	int nat_friendly = 0;
404f8829a4aSRandall Stewart 
405f8829a4aSRandall Stewart 	/* First verify that we have no illegal param's */
406f8829a4aSRandall Stewart 	abort_flag = 0;
4076182677fSMichael Tuexen 	cookie_found = 0;
408f8829a4aSRandall Stewart 
409f8829a4aSRandall Stewart 	op_err = sctp_arethere_unrecognized_parameters(m,
410f8829a4aSRandall Stewart 	    (offset + sizeof(struct sctp_init_chunk)),
4116182677fSMichael Tuexen 	    &abort_flag, (struct sctp_chunkhdr *)cp,
4126182677fSMichael Tuexen 	    &nat_friendly, &cookie_found);
413f8829a4aSRandall Stewart 	if (abort_flag) {
414f8829a4aSRandall Stewart 		/* Send an abort and notify peer */
415dd36606bSMichael Tuexen 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
416dd36606bSMichael Tuexen 		    src, dst, sh, op_err,
417dd36606bSMichael Tuexen 		    mflowtype, mflowid,
418dd36606bSMichael Tuexen 		    vrf_id, net->port);
419bff64a4dSRandall Stewart 		*abort_no_unlock = 1;
420f8829a4aSRandall Stewart 		return (-1);
421f8829a4aSRandall Stewart 	}
4226182677fSMichael Tuexen 	if (!cookie_found) {
4236182677fSMichael Tuexen 		uint16_t len;
4246182677fSMichael Tuexen 
4250941b9dcSMichael Tuexen 		/* Only report the missing cookie parameter */
4260941b9dcSMichael Tuexen 		if (op_err != NULL) {
4270941b9dcSMichael Tuexen 			sctp_m_freem(op_err);
4280941b9dcSMichael Tuexen 		}
4296182677fSMichael Tuexen 		len = (uint16_t)(sizeof(struct sctp_error_missing_param) + sizeof(uint16_t));
4306182677fSMichael Tuexen 		/* We abort with an error of missing mandatory param */
4316182677fSMichael Tuexen 		op_err = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
4326182677fSMichael Tuexen 		if (op_err != NULL) {
4336182677fSMichael Tuexen 			struct sctp_error_missing_param *cause;
4346182677fSMichael Tuexen 
4356182677fSMichael Tuexen 			SCTP_BUF_LEN(op_err) = len;
4366182677fSMichael Tuexen 			cause = mtod(op_err, struct sctp_error_missing_param *);
4376182677fSMichael Tuexen 			/* Subtract the reserved param */
4386182677fSMichael Tuexen 			cause->cause.code = htons(SCTP_CAUSE_MISSING_PARAM);
4396182677fSMichael Tuexen 			cause->cause.length = htons(len);
4406182677fSMichael Tuexen 			cause->num_missing_params = htonl(1);
4416182677fSMichael Tuexen 			cause->type[0] = htons(SCTP_STATE_COOKIE);
4426182677fSMichael Tuexen 		}
4436182677fSMichael Tuexen 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
4446182677fSMichael Tuexen 		    src, dst, sh, op_err,
4456182677fSMichael Tuexen 		    mflowtype, mflowid,
4466182677fSMichael Tuexen 		    vrf_id, net->port);
4476182677fSMichael Tuexen 		*abort_no_unlock = 1;
4486182677fSMichael Tuexen 		return (-3);
4496182677fSMichael Tuexen 	}
450f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
451830d754dSRandall Stewart 	asoc->peer_supports_nat = (uint8_t)nat_friendly;
452f8829a4aSRandall Stewart 	/* process the peer's parameters in the INIT-ACK */
4535f2e1835SMichael Tuexen 	if (sctp_process_init((struct sctp_init_chunk *)cp, stcb) < 0) {
454645f3a1cSMichael Tuexen 		if (op_err != NULL) {
455645f3a1cSMichael Tuexen 			sctp_m_freem(op_err);
456645f3a1cSMichael Tuexen 		}
4575f2e1835SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4585f2e1835SMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_init() failed\n");
4595f2e1835SMichael Tuexen 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
4605f2e1835SMichael Tuexen 		    src, dst, sh, op_err,
4615f2e1835SMichael Tuexen 		    mflowtype, mflowid,
4625f2e1835SMichael Tuexen 		    vrf_id, net->port);
4635f2e1835SMichael Tuexen 		*abort_no_unlock = 1;
4645f2e1835SMichael Tuexen 		return (-1);
465f8829a4aSRandall Stewart 	}
466f8829a4aSRandall Stewart 	initack_limit = offset + ntohs(cp->ch.chunk_length);
467f8829a4aSRandall Stewart 	/* load all addresses */
4687215cc1bSMichael Tuexen 	if ((retval = sctp_load_addresses_from_init(stcb, m,
4695f2e1835SMichael Tuexen 	    offset + sizeof(struct sctp_init_chunk),
4705f2e1835SMichael Tuexen 	    initack_limit, src, dst, NULL, stcb->asoc.port)) < 0) {
471645f3a1cSMichael Tuexen 		if (op_err != NULL) {
472645f3a1cSMichael Tuexen 			sctp_m_freem(op_err);
473645f3a1cSMichael Tuexen 		}
474ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
475ff1ffd74SMichael Tuexen 		    "Problem with address parameters");
476ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1,
477ad81507eSRandall Stewart 		    "Load addresses from INIT causes an abort %d\n",
478ad81507eSRandall Stewart 		    retval);
479b1754ad1SMichael Tuexen 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
480ff1ffd74SMichael Tuexen 		    src, dst, sh, op_err,
481457b4b88SMichael Tuexen 		    mflowtype, mflowid,
482f30ac432SMichael Tuexen 		    vrf_id, net->port);
483bff64a4dSRandall Stewart 		*abort_no_unlock = 1;
484f8829a4aSRandall Stewart 		return (-1);
485f8829a4aSRandall Stewart 	}
48618e198d3SRandall Stewart 	/* if the peer doesn't support asconf, flush the asconf queue */
487c79bec9cSMichael Tuexen 	if (asoc->asconf_supported == 0) {
4884a9ef3f8SMichael Tuexen 		struct sctp_asconf_addr *param, *nparam;
48918e198d3SRandall Stewart 
4904a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
4914a9ef3f8SMichael Tuexen 			TAILQ_REMOVE(&asoc->asconf_queue, param, next);
4924a9ef3f8SMichael Tuexen 			SCTP_FREE(param, SCTP_M_ASC_ADDR);
49318e198d3SRandall Stewart 		}
49418e198d3SRandall Stewart 	}
4950053ed28SMichael Tuexen 
496f8829a4aSRandall Stewart 	stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
497f8829a4aSRandall Stewart 	    stcb->asoc.local_hmacs);
498f8829a4aSRandall Stewart 	if (op_err) {
499f8829a4aSRandall Stewart 		sctp_queue_op_err(stcb, op_err);
500f8829a4aSRandall Stewart 		/* queuing will steal away the mbuf chain to the out queue */
501f8829a4aSRandall Stewart 		op_err = NULL;
502f8829a4aSRandall Stewart 	}
503f8829a4aSRandall Stewart 	/* extract the cookie and queue it to "echo" it back... */
504b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
505c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
506c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
507c4739e2fSRandall Stewart 		    0,
508c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INPUT,
509c4739e2fSRandall Stewart 		    __LINE__);
510c4739e2fSRandall Stewart 	}
511f8829a4aSRandall Stewart 
512f8829a4aSRandall Stewart 	/*
513f8829a4aSRandall Stewart 	 * Cancel the INIT timer, We do this first before queueing the
514d2e61614SGordon Bergling 	 * cookie. We always cancel at the primary to assume that we are
515f8829a4aSRandall Stewart 	 * canceling the timer started by the INIT which always goes to the
516f8829a4aSRandall Stewart 	 * primary.
517f8829a4aSRandall Stewart 	 */
518f8829a4aSRandall Stewart 	sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
519b7d130beSMichael Tuexen 	    asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
520a5d547adSRandall Stewart 
521a5d547adSRandall Stewart 	/* calculate the RTO */
522*8ed1e2c8SMichael Tuexen 	if (asoc->overall_error_count == 0) {
52344f2a327SMichael Tuexen 		sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered,
524f79aab18SRandall Stewart 		    SCTP_RTT_FROM_NON_DATA);
525*8ed1e2c8SMichael Tuexen 	}
526*8ed1e2c8SMichael Tuexen 	stcb->asoc.overall_error_count = 0;
527*8ed1e2c8SMichael Tuexen 	net->error_count = 0;
5286182677fSMichael Tuexen 	retval = sctp_send_cookie_echo(m, offset, initack_limit, stcb, net);
529f8829a4aSRandall Stewart 	return (retval);
530f8829a4aSRandall Stewart }
5310053ed28SMichael Tuexen 
532f8829a4aSRandall Stewart static void
533f8829a4aSRandall Stewart sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
534f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net)
535f8829a4aSRandall Stewart {
53624aaac8dSMichael Tuexen 	union sctp_sockstore store;
53752129fcdSRandall Stewart 	struct sctp_nets *r_net, *f_net;
538f8829a4aSRandall Stewart 	struct timeval tv;
539d55b0b1bSRandall Stewart 	int req_prim = 0;
540ca85e948SMichael Tuexen 	uint16_t old_error_counter;
541f8829a4aSRandall Stewart 
542f8829a4aSRandall Stewart 	if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
543f8829a4aSRandall Stewart 		/* Invalid length */
544f8829a4aSRandall Stewart 		return;
545f8829a4aSRandall Stewart 	}
5460053ed28SMichael Tuexen 
547f8829a4aSRandall Stewart 	memset(&store, 0, sizeof(store));
548e6194c2eSMichael Tuexen 	switch (cp->heartbeat.hb_info.addr_family) {
549e6194c2eSMichael Tuexen #ifdef INET
550e6194c2eSMichael Tuexen 	case AF_INET:
551e6194c2eSMichael Tuexen 		if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
55224aaac8dSMichael Tuexen 			store.sin.sin_family = cp->heartbeat.hb_info.addr_family;
55324aaac8dSMichael Tuexen 			store.sin.sin_len = cp->heartbeat.hb_info.addr_len;
55424aaac8dSMichael Tuexen 			store.sin.sin_port = stcb->rport;
55524aaac8dSMichael Tuexen 			memcpy(&store.sin.sin_addr, cp->heartbeat.hb_info.address,
55624aaac8dSMichael Tuexen 			    sizeof(store.sin.sin_addr));
557e6194c2eSMichael Tuexen 		} else {
558e6194c2eSMichael Tuexen 			return;
559e6194c2eSMichael Tuexen 		}
560e6194c2eSMichael Tuexen 		break;
561e6194c2eSMichael Tuexen #endif
562e6194c2eSMichael Tuexen #ifdef INET6
563e6194c2eSMichael Tuexen 	case AF_INET6:
564e6194c2eSMichael Tuexen 		if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
56524aaac8dSMichael Tuexen 			store.sin6.sin6_family = cp->heartbeat.hb_info.addr_family;
56624aaac8dSMichael Tuexen 			store.sin6.sin6_len = cp->heartbeat.hb_info.addr_len;
56724aaac8dSMichael Tuexen 			store.sin6.sin6_port = stcb->rport;
56823602b60SMichael Tuexen 			memcpy(&store.sin6.sin6_addr, cp->heartbeat.hb_info.address, sizeof(struct in6_addr));
569f8829a4aSRandall Stewart 		} else {
570f8829a4aSRandall Stewart 			return;
571f8829a4aSRandall Stewart 		}
572e6194c2eSMichael Tuexen 		break;
573e6194c2eSMichael Tuexen #endif
574e6194c2eSMichael Tuexen 	default:
575e6194c2eSMichael Tuexen 		return;
576e6194c2eSMichael Tuexen 	}
57724aaac8dSMichael Tuexen 	r_net = sctp_findnet(stcb, &store.sa);
578f8829a4aSRandall Stewart 	if (r_net == NULL) {
579ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
580f8829a4aSRandall Stewart 		return;
581f8829a4aSRandall Stewart 	}
582f8829a4aSRandall Stewart 	if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
583f8829a4aSRandall Stewart 	    (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
584f8829a4aSRandall Stewart 	    (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
585f8829a4aSRandall Stewart 		/*
586f8829a4aSRandall Stewart 		 * If the its a HB and it's random value is correct when can
587f8829a4aSRandall Stewart 		 * confirm the destination.
588f8829a4aSRandall Stewart 		 */
589f8829a4aSRandall Stewart 		r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
59042551e99SRandall Stewart 		if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
59142551e99SRandall Stewart 			stcb->asoc.primary_destination = r_net;
59242551e99SRandall Stewart 			r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
59352129fcdSRandall Stewart 			f_net = TAILQ_FIRST(&stcb->asoc.nets);
59452129fcdSRandall Stewart 			if (f_net != r_net) {
59542551e99SRandall Stewart 				/*
59642551e99SRandall Stewart 				 * first one on the list is NOT the primary
597cd0a4ff6SPedro F. Giffuni 				 * sctp_cmpaddr() is much more efficient if
59842551e99SRandall Stewart 				 * the primary is the first on the list,
59942551e99SRandall Stewart 				 * make it so.
60042551e99SRandall Stewart 				 */
60152129fcdSRandall Stewart 				TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
60252129fcdSRandall Stewart 				TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
60342551e99SRandall Stewart 			}
604d55b0b1bSRandall Stewart 			req_prim = 1;
60542551e99SRandall Stewart 		}
606f8829a4aSRandall Stewart 		sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
607ceaad40aSRandall Stewart 		    stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
608b7d130beSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb,
609b7d130beSMichael Tuexen 		    r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
610ca85e948SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
611f8829a4aSRandall Stewart 	}
612469a65d1SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
613469a65d1SMichael Tuexen 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
614469a65d1SMichael Tuexen 		    stcb->asoc.overall_error_count,
615469a65d1SMichael Tuexen 		    0,
616469a65d1SMichael Tuexen 		    SCTP_FROM_SCTP_INPUT,
617469a65d1SMichael Tuexen 		    __LINE__);
618469a65d1SMichael Tuexen 	}
619469a65d1SMichael Tuexen 	stcb->asoc.overall_error_count = 0;
620ca85e948SMichael Tuexen 	old_error_counter = r_net->error_count;
621f8829a4aSRandall Stewart 	r_net->error_count = 0;
622f8829a4aSRandall Stewart 	r_net->hb_responded = 1;
623f8829a4aSRandall Stewart 	tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
624f8829a4aSRandall Stewart 	tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
625f8829a4aSRandall Stewart 	/* Now lets do a RTO with this */
62644f2a327SMichael Tuexen 	sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv,
627f79aab18SRandall Stewart 	    SCTP_RTT_FROM_NON_DATA);
6289b2a35b3SMichael Tuexen 	if ((r_net->dest_state & SCTP_ADDR_REACHABLE) == 0) {
629ca85e948SMichael Tuexen 		r_net->dest_state |= SCTP_ADDR_REACHABLE;
630ca85e948SMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
6314b1f78e1SMichael Tuexen 		    0, (void *)r_net, SCTP_SO_NOT_LOCKED);
632ca85e948SMichael Tuexen 	}
633ca85e948SMichael Tuexen 	if (r_net->dest_state & SCTP_ADDR_PF) {
634ca85e948SMichael Tuexen 		r_net->dest_state &= ~SCTP_ADDR_PF;
635ca85e948SMichael Tuexen 		stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
636ca85e948SMichael Tuexen 	}
637ca85e948SMichael Tuexen 	if (old_error_counter > 0) {
638b7d130beSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
639b7d130beSMichael Tuexen 		    stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_5);
640ca85e948SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
641ca85e948SMichael Tuexen 	}
642ca85e948SMichael Tuexen 	if (r_net == stcb->asoc.primary_destination) {
643ca85e948SMichael Tuexen 		if (stcb->asoc.alternate) {
644ca85e948SMichael Tuexen 			/* release the alternate, primary is good */
645ca85e948SMichael Tuexen 			sctp_free_remote_addr(stcb->asoc.alternate);
646ca85e948SMichael Tuexen 			stcb->asoc.alternate = NULL;
647ca85e948SMichael Tuexen 		}
648ca85e948SMichael Tuexen 	}
649d55b0b1bSRandall Stewart 	/* Mobility adaptation */
650d55b0b1bSRandall Stewart 	if (req_prim) {
651d55b0b1bSRandall Stewart 		if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
652d55b0b1bSRandall Stewart 		    SCTP_MOBILITY_BASE) ||
653d55b0b1bSRandall Stewart 		    sctp_is_mobility_feature_on(stcb->sctp_ep,
654d55b0b1bSRandall Stewart 		    SCTP_MOBILITY_FASTHANDOFF)) &&
655d55b0b1bSRandall Stewart 		    sctp_is_mobility_feature_on(stcb->sctp_ep,
656d55b0b1bSRandall Stewart 		    SCTP_MOBILITY_PRIM_DELETED)) {
657b7d130beSMichael Tuexen 			sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED,
658b7d130beSMichael Tuexen 			    stcb->sctp_ep, stcb, NULL,
659b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
660d55b0b1bSRandall Stewart 			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
661d55b0b1bSRandall Stewart 			    SCTP_MOBILITY_FASTHANDOFF)) {
662d55b0b1bSRandall Stewart 				sctp_assoc_immediate_retrans(stcb,
663d55b0b1bSRandall Stewart 				    stcb->asoc.primary_destination);
664d55b0b1bSRandall Stewart 			}
665d55b0b1bSRandall Stewart 			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
666d55b0b1bSRandall Stewart 			    SCTP_MOBILITY_BASE)) {
6679eea4a2dSMichael Tuexen 				sctp_move_chunks_from_net(stcb,
6689eea4a2dSMichael Tuexen 				    stcb->asoc.deleted_primary);
669d55b0b1bSRandall Stewart 			}
670a57fb68bSMichael Tuexen 			sctp_delete_prim_timer(stcb->sctp_ep, stcb);
671d55b0b1bSRandall Stewart 		}
672d55b0b1bSRandall Stewart 	}
673f8829a4aSRandall Stewart }
674f8829a4aSRandall Stewart 
675830d754dSRandall Stewart static int
676830d754dSRandall Stewart sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
677830d754dSRandall Stewart {
678830d754dSRandall Stewart 	/*
6791325a0deSMichael Tuexen 	 * Return 0 means we want you to proceed with the abort non-zero
6801325a0deSMichael Tuexen 	 * means no abort processing.
681830d754dSRandall Stewart 	 */
6821325a0deSMichael Tuexen 	uint32_t new_vtag;
683830d754dSRandall Stewart 	struct sctpasochead *head;
684830d754dSRandall Stewart 
685839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
686839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
687d28a3a39SMichael Tuexen 		atomic_add_int(&stcb->asoc.refcnt, 1);
688d28a3a39SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
689d28a3a39SMichael Tuexen 		SCTP_INP_INFO_WLOCK();
690d28a3a39SMichael Tuexen 		SCTP_TCB_LOCK(stcb);
691d28a3a39SMichael Tuexen 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
6921325a0deSMichael Tuexen 	} else {
6931325a0deSMichael Tuexen 		return (0);
694d28a3a39SMichael Tuexen 	}
69529545986SMichael Tuexen 	new_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
696839d21d6SMichael Tuexen 	if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) {
697830d754dSRandall Stewart 		/* generate a new vtag and send init */
698830d754dSRandall Stewart 		LIST_REMOVE(stcb, sctp_asocs);
6991325a0deSMichael Tuexen 		stcb->asoc.my_vtag = new_vtag;
700830d754dSRandall Stewart 		head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
701b7b84c0eSMichael Tuexen 		/*
702b7b84c0eSMichael Tuexen 		 * put it in the bucket in the vtag hash of assoc's for the
703b7b84c0eSMichael Tuexen 		 * system
704b7b84c0eSMichael Tuexen 		 */
705830d754dSRandall Stewart 		LIST_INSERT_HEAD(head, stcb, sctp_asocs);
706d28a3a39SMichael Tuexen 		SCTP_INP_INFO_WUNLOCK();
7071325a0deSMichael Tuexen 		sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
708830d754dSRandall Stewart 		return (1);
7091325a0deSMichael Tuexen 	} else {
710830d754dSRandall Stewart 		/*
711830d754dSRandall Stewart 		 * treat like a case where the cookie expired i.e.: - dump
712830d754dSRandall Stewart 		 * current cookie. - generate a new vtag. - resend init.
713830d754dSRandall Stewart 		 */
714830d754dSRandall Stewart 		/* generate a new vtag and send init */
715830d754dSRandall Stewart 		LIST_REMOVE(stcb, sctp_asocs);
716839d21d6SMichael Tuexen 		SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
717830d754dSRandall Stewart 		sctp_stop_all_cookie_timers(stcb);
718830d754dSRandall Stewart 		sctp_toss_old_cookies(stcb, &stcb->asoc);
7191325a0deSMichael Tuexen 		stcb->asoc.my_vtag = new_vtag;
720830d754dSRandall Stewart 		head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
721b7b84c0eSMichael Tuexen 		/*
722b7b84c0eSMichael Tuexen 		 * put it in the bucket in the vtag hash of assoc's for the
723b7b84c0eSMichael Tuexen 		 * system
724b7b84c0eSMichael Tuexen 		 */
725830d754dSRandall Stewart 		LIST_INSERT_HEAD(head, stcb, sctp_asocs);
726d28a3a39SMichael Tuexen 		SCTP_INP_INFO_WUNLOCK();
7271325a0deSMichael Tuexen 		sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
728830d754dSRandall Stewart 		return (1);
729830d754dSRandall Stewart 	}
730830d754dSRandall Stewart 	return (0);
731830d754dSRandall Stewart }
732830d754dSRandall Stewart 
733830d754dSRandall Stewart static int
734830d754dSRandall Stewart sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
735830d754dSRandall Stewart     struct sctp_nets *net)
736830d754dSRandall Stewart {
737830d754dSRandall Stewart 	/*
738830d754dSRandall Stewart 	 * return 0 means we want you to proceed with the abort non-zero
739830d754dSRandall Stewart 	 * means no abort processing
740830d754dSRandall Stewart 	 */
741c79bec9cSMichael Tuexen 	if (stcb->asoc.auth_supported == 0) {
742830d754dSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
743830d754dSRandall Stewart 		return (0);
744830d754dSRandall Stewart 	}
745830d754dSRandall Stewart 	sctp_asconf_send_nat_state_update(stcb, net);
746830d754dSRandall Stewart 	return (1);
747830d754dSRandall Stewart }
748830d754dSRandall Stewart 
749701492a5SMichael Tuexen /* Returns 1 if the stcb was aborted, 0 otherwise */
750701492a5SMichael Tuexen static int
751a2b42326SMichael Tuexen sctp_handle_abort(struct sctp_abort_chunk *abort,
752f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net)
753f8829a4aSRandall Stewart {
754830d754dSRandall Stewart 	uint16_t len;
755a2b42326SMichael Tuexen 	uint16_t error;
756ceaad40aSRandall Stewart 
757ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
758f8829a4aSRandall Stewart 	if (stcb == NULL)
759701492a5SMichael Tuexen 		return (0);
760f8829a4aSRandall Stewart 
761a2b42326SMichael Tuexen 	len = ntohs(abort->ch.chunk_length);
762701492a5SMichael Tuexen 	if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_error_cause)) {
763830d754dSRandall Stewart 		/*
764830d754dSRandall Stewart 		 * Need to check the cause codes for our two magic nat
765830d754dSRandall Stewart 		 * aborts which don't kill the assoc necessarily.
766830d754dSRandall Stewart 		 */
767701492a5SMichael Tuexen 		struct sctp_error_cause *cause;
768830d754dSRandall Stewart 
769701492a5SMichael Tuexen 		cause = (struct sctp_error_cause *)(abort + 1);
77086eda749SMichael Tuexen 		error = ntohs(cause->code);
771a2b42326SMichael Tuexen 		if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) {
772504ee6a0SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state, ABORT flags:%x\n",
773a2b42326SMichael Tuexen 			    abort->ch.chunk_flags);
774830d754dSRandall Stewart 			if (sctp_handle_nat_colliding_state(stcb)) {
775701492a5SMichael Tuexen 				return (0);
776830d754dSRandall Stewart 			}
777a2b42326SMichael Tuexen 		} else if (error == SCTP_CAUSE_NAT_MISSING_STATE) {
778504ee6a0SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state, ABORT flags:%x\n",
779a2b42326SMichael Tuexen 			    abort->ch.chunk_flags);
780830d754dSRandall Stewart 			if (sctp_handle_nat_missing_state(stcb, net)) {
781701492a5SMichael Tuexen 				return (0);
782830d754dSRandall Stewart 			}
783830d754dSRandall Stewart 		}
784a2b42326SMichael Tuexen 	} else {
785a2b42326SMichael Tuexen 		error = 0;
786830d754dSRandall Stewart 	}
787f8829a4aSRandall Stewart 	/* stop any receive timers */
7886fb7b4fbSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
789b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_7);
790f8829a4aSRandall Stewart 	/* notify user of the abort and clean up... */
791105b68b4SMichael Tuexen 	sctp_abort_notification(stcb, true, false, error, abort, SCTP_SO_NOT_LOCKED);
792f8829a4aSRandall Stewart 	/* free the tcb */
793f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_aborted);
794839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
795839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
796f8829a4aSRandall Stewart 		SCTP_STAT_DECR_GAUGE32(sctps_currestab);
797f8829a4aSRandall Stewart 	}
798f1f73e57SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
799f1f73e57SRandall Stewart 	sctp_print_out_track_log(stcb);
800f1f73e57SRandall Stewart #endif
801c4739e2fSRandall Stewart 	(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
802b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
803ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
804701492a5SMichael Tuexen 	return (1);
805f8829a4aSRandall Stewart }
806f8829a4aSRandall Stewart 
807f8829a4aSRandall Stewart static void
808ca85e948SMichael Tuexen sctp_start_net_timers(struct sctp_tcb *stcb)
809ca85e948SMichael Tuexen {
810ca85e948SMichael Tuexen 	uint32_t cnt_hb_sent;
811ca85e948SMichael Tuexen 	struct sctp_nets *net;
812ca85e948SMichael Tuexen 
813ca85e948SMichael Tuexen 	cnt_hb_sent = 0;
814ca85e948SMichael Tuexen 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
815ca85e948SMichael Tuexen 		/*
816ca85e948SMichael Tuexen 		 * For each network start: 1) A pmtu timer. 2) A HB timer 3)
817ca85e948SMichael Tuexen 		 * If the dest in unconfirmed send a hb as well if under
818ca85e948SMichael Tuexen 		 * max_hb_burst have been sent.
819ca85e948SMichael Tuexen 		 */
820ca85e948SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
821ca85e948SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
822ca85e948SMichael Tuexen 		if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
823ca85e948SMichael Tuexen 		    (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
824ca85e948SMichael Tuexen 			sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
825ca85e948SMichael Tuexen 			cnt_hb_sent++;
826ca85e948SMichael Tuexen 		}
827ca85e948SMichael Tuexen 	}
828ca85e948SMichael Tuexen 	if (cnt_hb_sent) {
829ca85e948SMichael Tuexen 		sctp_chunk_output(stcb->sctp_ep, stcb,
830ca85e948SMichael Tuexen 		    SCTP_OUTPUT_FROM_COOKIE_ACK,
831ca85e948SMichael Tuexen 		    SCTP_SO_NOT_LOCKED);
832ca85e948SMichael Tuexen 	}
833ca85e948SMichael Tuexen }
834ca85e948SMichael Tuexen 
835ca85e948SMichael Tuexen static void
836f8829a4aSRandall Stewart sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
837f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
838f8829a4aSRandall Stewart {
839f8829a4aSRandall Stewart 	struct sctp_association *asoc;
840f8829a4aSRandall Stewart 	int some_on_streamwheel;
841aa1cfca9SMichael Tuexen 	int old_state;
842ceaad40aSRandall Stewart 
843ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
844ad81507eSRandall Stewart 	    "sctp_handle_shutdown: handling SHUTDOWN\n");
845f8829a4aSRandall Stewart 	if (stcb == NULL)
846f8829a4aSRandall Stewart 		return;
847a5d547adSRandall Stewart 	asoc = &stcb->asoc;
848839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
849839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
850f8829a4aSRandall Stewart 		return;
851f8829a4aSRandall Stewart 	}
852f8829a4aSRandall Stewart 	if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
853f8829a4aSRandall Stewart 		/* Shutdown NOT the expected size */
854f8829a4aSRandall Stewart 		return;
855aa1cfca9SMichael Tuexen 	}
856839d21d6SMichael Tuexen 	old_state = SCTP_GET_STATE(stcb);
8577215cc1bSMichael Tuexen 	sctp_update_acked(stcb, cp, abort_flag);
8587e6206afSMichael Tuexen 	if (*abort_flag) {
8597e6206afSMichael Tuexen 		return;
8607e6206afSMichael Tuexen 	}
861a5d547adSRandall Stewart 	if (asoc->control_pdapi) {
862f8829a4aSRandall Stewart 		/*
863f8829a4aSRandall Stewart 		 * With a normal shutdown we assume the end of last record.
864f8829a4aSRandall Stewart 		 */
865f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
86644249214SRandall Stewart 		if (asoc->control_pdapi->on_strm_q) {
86744249214SRandall Stewart 			struct sctp_stream_in *strm;
86844249214SRandall Stewart 
86944249214SRandall Stewart 			strm = &asoc->strmin[asoc->control_pdapi->sinfo_stream];
87044249214SRandall Stewart 			if (asoc->control_pdapi->on_strm_q == SCTP_ON_UNORDERED) {
87144249214SRandall Stewart 				/* Unordered */
87244249214SRandall Stewart 				TAILQ_REMOVE(&strm->uno_inqueue, asoc->control_pdapi, next_instrm);
87344249214SRandall Stewart 				asoc->control_pdapi->on_strm_q = 0;
87444249214SRandall Stewart 			} else if (asoc->control_pdapi->on_strm_q == SCTP_ON_ORDERED) {
87544249214SRandall Stewart 				/* Ordered */
87644249214SRandall Stewart 				TAILQ_REMOVE(&strm->inqueue, asoc->control_pdapi, next_instrm);
87744249214SRandall Stewart 				asoc->control_pdapi->on_strm_q = 0;
87898d5fd97SMichael Tuexen #ifdef INVARIANTS
87944249214SRandall Stewart 			} else {
88044249214SRandall Stewart 				panic("Unknown state on ctrl:%p on_strm_q:%d",
88144249214SRandall Stewart 				    asoc->control_pdapi,
88244249214SRandall Stewart 				    asoc->control_pdapi->on_strm_q);
88398d5fd97SMichael Tuexen #endif
88444249214SRandall Stewart 			}
88544249214SRandall Stewart 		}
886a5d547adSRandall Stewart 		asoc->control_pdapi->end_added = 1;
887a5d547adSRandall Stewart 		asoc->control_pdapi->pdapi_aborted = 1;
888a5d547adSRandall Stewart 		asoc->control_pdapi = NULL;
889f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
890828318e1SMichael Tuexen 		if (stcb->sctp_socket) {
89150cec919SRandall Stewart 			sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
892828318e1SMichael Tuexen 		}
893f8829a4aSRandall Stewart 	}
894f8829a4aSRandall Stewart 	/* goto SHUTDOWN_RECEIVED state to block new requests */
895f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
896839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
897839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
898839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT)) {
899839d21d6SMichael Tuexen 			SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_RECEIVED);
900b7b84c0eSMichael Tuexen 			/*
901b7b84c0eSMichael Tuexen 			 * notify upper layer that peer has initiated a
902b7b84c0eSMichael Tuexen 			 * shutdown
903b7b84c0eSMichael Tuexen 			 */
904ceaad40aSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
905f8829a4aSRandall Stewart 
906f8829a4aSRandall Stewart 			/* reset time */
9076e55db54SRandall Stewart 			(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
908f8829a4aSRandall Stewart 		}
909f8829a4aSRandall Stewart 	}
910839d21d6SMichael Tuexen 	if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) {
911f8829a4aSRandall Stewart 		/*
912f8829a4aSRandall Stewart 		 * stop the shutdown timer, since we WILL move to
913f8829a4aSRandall Stewart 		 * SHUTDOWN-ACK-SENT.
914f8829a4aSRandall Stewart 		 */
915b7d130beSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
916b7d130beSMichael Tuexen 		    net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
917f8829a4aSRandall Stewart 	}
9185bead436SRandall Stewart 	/* Now is there unsent data on a stream somewhere? */
919689e6a5fSMichael Tuexen 	some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
920f8829a4aSRandall Stewart 
921f8829a4aSRandall Stewart 	if (!TAILQ_EMPTY(&asoc->send_queue) ||
922f8829a4aSRandall Stewart 	    !TAILQ_EMPTY(&asoc->sent_queue) ||
923f8829a4aSRandall Stewart 	    some_on_streamwheel) {
924f8829a4aSRandall Stewart 		/* By returning we will push more data out */
925f8829a4aSRandall Stewart 		return;
926f8829a4aSRandall Stewart 	} else {
927f8829a4aSRandall Stewart 		/* no outstanding data to send, so move on... */
928f8829a4aSRandall Stewart 		/* send SHUTDOWN-ACK */
929f8829a4aSRandall Stewart 		/* move to SHUTDOWN-ACK-SENT state */
930839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
931839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
932f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
933f8829a4aSRandall Stewart 		}
934839d21d6SMichael Tuexen 		if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
935839d21d6SMichael Tuexen 			SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_ACK_SENT);
93612af6654SMichael Tuexen 			sctp_stop_timers_for_shutdown(stcb);
937c39cfa1fSMichael Tuexen 			sctp_send_shutdown_ack(stcb, net);
938aa1cfca9SMichael Tuexen 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
939aa1cfca9SMichael Tuexen 			    stcb->sctp_ep, stcb, net);
940aa1cfca9SMichael Tuexen 		} else if (old_state == SCTP_STATE_SHUTDOWN_ACK_SENT) {
941aa1cfca9SMichael Tuexen 			sctp_send_shutdown_ack(stcb, net);
942aa1cfca9SMichael Tuexen 		}
943f8829a4aSRandall Stewart 	}
944f8829a4aSRandall Stewart }
945f8829a4aSRandall Stewart 
946f8829a4aSRandall Stewart static void
9477215cc1bSMichael Tuexen sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
9487b470fc3SMichael Tuexen     struct sctp_tcb *stcb,
9497b470fc3SMichael Tuexen     struct sctp_nets *net)
950f8829a4aSRandall Stewart {
951f8829a4aSRandall Stewart 	struct sctp_association *asoc;
952ceaad40aSRandall Stewart 
953ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
954ad81507eSRandall Stewart 	    "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
955f8829a4aSRandall Stewart 	if (stcb == NULL)
956f8829a4aSRandall Stewart 		return;
957f8829a4aSRandall Stewart 
958f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
959f8829a4aSRandall Stewart 	/* process according to association state */
960839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
961839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
9627b470fc3SMichael Tuexen 		/* unexpected SHUTDOWN-ACK... do OOTB handling... */
9637b470fc3SMichael Tuexen 		sctp_send_shutdown_complete(stcb, net, 1);
9647b470fc3SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
9657b470fc3SMichael Tuexen 		return;
9667b470fc3SMichael Tuexen 	}
967839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
968839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
969f8829a4aSRandall Stewart 		/* unexpected SHUTDOWN-ACK... so ignore... */
970f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
971f8829a4aSRandall Stewart 		return;
972f8829a4aSRandall Stewart 	}
973a5d547adSRandall Stewart 	if (asoc->control_pdapi) {
974f8829a4aSRandall Stewart 		/*
975f8829a4aSRandall Stewart 		 * With a normal shutdown we assume the end of last record.
976f8829a4aSRandall Stewart 		 */
977f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
978a5d547adSRandall Stewart 		asoc->control_pdapi->end_added = 1;
979a5d547adSRandall Stewart 		asoc->control_pdapi->pdapi_aborted = 1;
980a5d547adSRandall Stewart 		asoc->control_pdapi = NULL;
981f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
98250cec919SRandall Stewart 		sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
983f8829a4aSRandall Stewart 	}
98456f778aaSMichael Tuexen #ifdef INVARIANTS
985f8829a4aSRandall Stewart 	if (!TAILQ_EMPTY(&asoc->send_queue) ||
986f8829a4aSRandall Stewart 	    !TAILQ_EMPTY(&asoc->sent_queue) ||
987124d851aSMichael Tuexen 	    sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
98856f778aaSMichael Tuexen 		panic("Queues are not empty when handling SHUTDOWN-ACK");
989f8829a4aSRandall Stewart 	}
99056f778aaSMichael Tuexen #endif
991f8829a4aSRandall Stewart 	/* stop the timer */
992b7d130beSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net,
993b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
994f8829a4aSRandall Stewart 	/* send SHUTDOWN-COMPLETE */
9957b470fc3SMichael Tuexen 	sctp_send_shutdown_complete(stcb, net, 0);
996f8829a4aSRandall Stewart 	/* notify upper layer protocol */
997f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
998f8829a4aSRandall Stewart 		if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
999f8829a4aSRandall Stewart 		    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1000f210e4fbSMichael Tuexen 			SCTP_SB_CLEAR(stcb->sctp_socket->so_snd);
1001f8829a4aSRandall Stewart 		}
1002c75fdd30SMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
1003f8829a4aSRandall Stewart 	}
1004f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
1005f8829a4aSRandall Stewart 	/* free the TCB but first save off the ep */
1006c4739e2fSRandall Stewart 	(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1007b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1008f8829a4aSRandall Stewart }
1009f8829a4aSRandall Stewart 
1010f8829a4aSRandall Stewart static void
10116fb7b4fbSMichael Tuexen sctp_process_unrecog_chunk(struct sctp_tcb *stcb, uint8_t chunk_type)
1012f8829a4aSRandall Stewart {
10133e87bccdSMichael Tuexen 	switch (chunk_type) {
1014f8829a4aSRandall Stewart 	case SCTP_ASCONF_ACK:
1015f8829a4aSRandall Stewart 	case SCTP_ASCONF:
10166fb7b4fbSMichael Tuexen 		sctp_asconf_cleanup(stcb);
1017f8829a4aSRandall Stewart 		break;
101844249214SRandall Stewart 	case SCTP_IFORWARD_CUM_TSN:
1019f8829a4aSRandall Stewart 	case SCTP_FORWARD_CUM_TSN:
1020dd973b0eSMichael Tuexen 		stcb->asoc.prsctp_supported = 0;
1021f8829a4aSRandall Stewart 		break;
1022f8829a4aSRandall Stewart 	default:
1023ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
10243e87bccdSMichael Tuexen 		    "Peer does not support chunk type %d (0x%x).\n",
10253e87bccdSMichael Tuexen 		    chunk_type, chunk_type);
1026f8829a4aSRandall Stewart 		break;
1027f8829a4aSRandall Stewart 	}
1028f8829a4aSRandall Stewart }
1029f8829a4aSRandall Stewart 
1030f8829a4aSRandall Stewart /*
1031f8829a4aSRandall Stewart  * Skip past the param header and then we will find the param that caused the
1032f8829a4aSRandall Stewart  * problem.  There are a number of param's in a ASCONF OR the prsctp param
1033f8829a4aSRandall Stewart  * these will turn of specific features.
1034c79bec9cSMichael Tuexen  * XXX: Is this the right thing to do?
1035f8829a4aSRandall Stewart  */
1036f8829a4aSRandall Stewart static void
10373e87bccdSMichael Tuexen sctp_process_unrecog_param(struct sctp_tcb *stcb, uint16_t parameter_type)
1038f8829a4aSRandall Stewart {
10393e87bccdSMichael Tuexen 	switch (parameter_type) {
1040f8829a4aSRandall Stewart 		/* pr-sctp draft */
1041f8829a4aSRandall Stewart 	case SCTP_PRSCTP_SUPPORTED:
1042dd973b0eSMichael Tuexen 		stcb->asoc.prsctp_supported = 0;
1043f8829a4aSRandall Stewart 		break;
1044f8829a4aSRandall Stewart 	case SCTP_SUPPORTED_CHUNK_EXT:
1045f8829a4aSRandall Stewart 		break;
1046f8829a4aSRandall Stewart 		/* draft-ietf-tsvwg-addip-sctp */
1047830d754dSRandall Stewart 	case SCTP_HAS_NAT_SUPPORT:
1048830d754dSRandall Stewart 		stcb->asoc.peer_supports_nat = 0;
1049830d754dSRandall Stewart 		break;
1050f8829a4aSRandall Stewart 	case SCTP_ADD_IP_ADDRESS:
1051f8829a4aSRandall Stewart 	case SCTP_DEL_IP_ADDRESS:
1052f8829a4aSRandall Stewart 	case SCTP_SET_PRIM_ADDR:
1053c79bec9cSMichael Tuexen 		stcb->asoc.asconf_supported = 0;
1054f8829a4aSRandall Stewart 		break;
1055f8829a4aSRandall Stewart 	case SCTP_SUCCESS_REPORT:
1056f8829a4aSRandall Stewart 	case SCTP_ERROR_CAUSE_IND:
1057ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1058ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
1059ad81507eSRandall Stewart 		    "Turning off ASCONF to this strange peer\n");
1060c79bec9cSMichael Tuexen 		stcb->asoc.asconf_supported = 0;
1061f8829a4aSRandall Stewart 		break;
1062f8829a4aSRandall Stewart 	default:
1063ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
10643e87bccdSMichael Tuexen 		    "Peer does not support param type %d (0x%x)??\n",
10653e87bccdSMichael Tuexen 		    parameter_type, parameter_type);
1066f8829a4aSRandall Stewart 		break;
1067f8829a4aSRandall Stewart 	}
1068f8829a4aSRandall Stewart }
1069f8829a4aSRandall Stewart 
1070f8829a4aSRandall Stewart static int
1071f8829a4aSRandall Stewart sctp_handle_error(struct sctp_chunkhdr *ch,
10723e87bccdSMichael Tuexen     struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
1073f8829a4aSRandall Stewart {
10743e87bccdSMichael Tuexen 	struct sctp_error_cause *cause;
1075f8829a4aSRandall Stewart 	struct sctp_association *asoc;
10763e87bccdSMichael Tuexen 	uint32_t remaining_length, adjust;
10773e87bccdSMichael Tuexen 	uint16_t code, cause_code, cause_length;
1078ceaad40aSRandall Stewart 
1079f8829a4aSRandall Stewart 	/* parse through all of the errors and process */
1080f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
10813e87bccdSMichael Tuexen 	cause = (struct sctp_error_cause *)((caddr_t)ch +
1082f8829a4aSRandall Stewart 	    sizeof(struct sctp_chunkhdr));
10833e87bccdSMichael Tuexen 	remaining_length = ntohs(ch->chunk_length);
10843e87bccdSMichael Tuexen 	if (remaining_length > limit) {
10853e87bccdSMichael Tuexen 		remaining_length = limit;
10863e87bccdSMichael Tuexen 	}
10873e87bccdSMichael Tuexen 	if (remaining_length >= sizeof(struct sctp_chunkhdr)) {
10883e87bccdSMichael Tuexen 		remaining_length -= sizeof(struct sctp_chunkhdr);
10893e87bccdSMichael Tuexen 	} else {
10903e87bccdSMichael Tuexen 		remaining_length = 0;
10913e87bccdSMichael Tuexen 	}
10923e87bccdSMichael Tuexen 	code = 0;
10933e87bccdSMichael Tuexen 	while (remaining_length >= sizeof(struct sctp_error_cause)) {
1094f8829a4aSRandall Stewart 		/* Process an Error Cause */
10953e87bccdSMichael Tuexen 		cause_code = ntohs(cause->code);
10963e87bccdSMichael Tuexen 		cause_length = ntohs(cause->length);
10973e87bccdSMichael Tuexen 		if ((cause_length > remaining_length) || (cause_length == 0)) {
10983e87bccdSMichael Tuexen 			/* Invalid cause length, possibly due to truncation. */
10993e87bccdSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in cause - bytes left: %u cause length: %u\n",
11003e87bccdSMichael Tuexen 			    remaining_length, cause_length);
1101f8829a4aSRandall Stewart 			return (0);
1102f8829a4aSRandall Stewart 		}
11033e87bccdSMichael Tuexen 		if (code == 0) {
1104389b1b11SMichael Tuexen 			/* report the first error cause */
11053e87bccdSMichael Tuexen 			code = cause_code;
1106389b1b11SMichael Tuexen 		}
11073e87bccdSMichael Tuexen 		switch (cause_code) {
1108f8829a4aSRandall Stewart 		case SCTP_CAUSE_INVALID_STREAM:
1109f8829a4aSRandall Stewart 		case SCTP_CAUSE_MISSING_PARAM:
1110f8829a4aSRandall Stewart 		case SCTP_CAUSE_INVALID_PARAM:
1111f8829a4aSRandall Stewart 		case SCTP_CAUSE_NO_USER_DATA:
11123e87bccdSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %u back? We have a bug :/ (or do they?)\n",
11133e87bccdSMichael Tuexen 			    cause_code);
1114f8829a4aSRandall Stewart 			break;
1115830d754dSRandall Stewart 		case SCTP_CAUSE_NAT_COLLIDING_STATE:
1116504ee6a0SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state, ERROR flags: %x\n",
1117830d754dSRandall Stewart 			    ch->chunk_flags);
1118830d754dSRandall Stewart 			if (sctp_handle_nat_colliding_state(stcb)) {
1119830d754dSRandall Stewart 				return (0);
1120830d754dSRandall Stewart 			}
1121830d754dSRandall Stewart 			break;
1122830d754dSRandall Stewart 		case SCTP_CAUSE_NAT_MISSING_STATE:
1123504ee6a0SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state, ERROR flags: %x\n",
1124830d754dSRandall Stewart 			    ch->chunk_flags);
1125830d754dSRandall Stewart 			if (sctp_handle_nat_missing_state(stcb, net)) {
1126830d754dSRandall Stewart 				return (0);
1127830d754dSRandall Stewart 			}
1128830d754dSRandall Stewart 			break;
1129f8829a4aSRandall Stewart 		case SCTP_CAUSE_STALE_COOKIE:
1130f8829a4aSRandall Stewart 			/*
1131f8829a4aSRandall Stewart 			 * We only act if we have echoed a cookie and are
1132f8829a4aSRandall Stewart 			 * waiting.
1133f8829a4aSRandall Stewart 			 */
11343e87bccdSMichael Tuexen 			if ((cause_length >= sizeof(struct sctp_error_stale_cookie)) &&
1135839d21d6SMichael Tuexen 			    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
11363e87bccdSMichael Tuexen 				struct sctp_error_stale_cookie *stale_cookie;
1137f8829a4aSRandall Stewart 
11383e87bccdSMichael Tuexen 				stale_cookie = (struct sctp_error_stale_cookie *)cause;
1139a92d5016SMichael Tuexen 				/* stable_time is in usec, convert to msec. */
1140a92d5016SMichael Tuexen 				asoc->cookie_preserve_req = ntohl(stale_cookie->stale_time) / 1000;
1141a92d5016SMichael Tuexen 				/* Double it to be more robust on RTX. */
11423e87bccdSMichael Tuexen 				asoc->cookie_preserve_req *= 2;
1143f8829a4aSRandall Stewart 				asoc->stale_cookie_count++;
1144f8829a4aSRandall Stewart 				if (asoc->stale_cookie_count >
1145f8829a4aSRandall Stewart 				    asoc->max_init_times) {
1146105b68b4SMichael Tuexen 					sctp_abort_notification(stcb, false, true, 0, NULL, SCTP_SO_NOT_LOCKED);
1147f8829a4aSRandall Stewart 					/* now free the asoc */
1148c4739e2fSRandall Stewart 					(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1149b7d130beSMichael Tuexen 					    SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1150f8829a4aSRandall Stewart 					return (-1);
1151f8829a4aSRandall Stewart 				}
1152f8829a4aSRandall Stewart 				/* blast back to INIT state */
1153830d754dSRandall Stewart 				sctp_toss_old_cookies(stcb, &stcb->asoc);
1154839d21d6SMichael Tuexen 				SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
1155f8829a4aSRandall Stewart 				sctp_stop_all_cookie_timers(stcb);
1156ceaad40aSRandall Stewart 				sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1157f8829a4aSRandall Stewart 			}
1158f8829a4aSRandall Stewart 			break;
1159f8829a4aSRandall Stewart 		case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1160f8829a4aSRandall Stewart 			/*
1161f8829a4aSRandall Stewart 			 * Nothing we can do here, we don't do hostname
1162f8829a4aSRandall Stewart 			 * addresses so if the peer does not like my IPv6
1163f8829a4aSRandall Stewart 			 * (or IPv4 for that matter) it does not matter. If
1164f8829a4aSRandall Stewart 			 * they don't support that type of address, they can
1165f8829a4aSRandall Stewart 			 * NOT possibly get that packet type... i.e. with no
1166cd0a4ff6SPedro F. Giffuni 			 * IPv6 you can't receive a IPv6 packet. so we can
1167f8829a4aSRandall Stewart 			 * safely ignore this one. If we ever added support
1168f8829a4aSRandall Stewart 			 * for HOSTNAME Addresses, then we would need to do
1169f8829a4aSRandall Stewart 			 * something here.
1170f8829a4aSRandall Stewart 			 */
1171f8829a4aSRandall Stewart 			break;
1172f8829a4aSRandall Stewart 		case SCTP_CAUSE_UNRECOG_CHUNK:
11733e87bccdSMichael Tuexen 			if (cause_length >= sizeof(struct sctp_error_unrecognized_chunk)) {
11743e87bccdSMichael Tuexen 				struct sctp_error_unrecognized_chunk *unrec_chunk;
11753e87bccdSMichael Tuexen 
11763e87bccdSMichael Tuexen 				unrec_chunk = (struct sctp_error_unrecognized_chunk *)cause;
11776fb7b4fbSMichael Tuexen 				sctp_process_unrecog_chunk(stcb, unrec_chunk->ch.chunk_type);
11783e87bccdSMichael Tuexen 			}
1179f8829a4aSRandall Stewart 			break;
1180f8829a4aSRandall Stewart 		case SCTP_CAUSE_UNRECOG_PARAM:
11813e87bccdSMichael Tuexen 			/* XXX: We only consider the first parameter */
11823e87bccdSMichael Tuexen 			if (cause_length >= sizeof(struct sctp_error_cause) + sizeof(struct sctp_paramhdr)) {
11833e87bccdSMichael Tuexen 				struct sctp_paramhdr *unrec_parameter;
11843e87bccdSMichael Tuexen 
11853e87bccdSMichael Tuexen 				unrec_parameter = (struct sctp_paramhdr *)(cause + 1);
11863e87bccdSMichael Tuexen 				sctp_process_unrecog_param(stcb, ntohs(unrec_parameter->param_type));
11873e87bccdSMichael Tuexen 			}
1188f8829a4aSRandall Stewart 			break;
1189f8829a4aSRandall Stewart 		case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1190f8829a4aSRandall Stewart 			/*
1191f8829a4aSRandall Stewart 			 * We ignore this since the timer will drive out a
1192f8829a4aSRandall Stewart 			 * new cookie anyway and there timer will drive us
1193f8829a4aSRandall Stewart 			 * to send a SHUTDOWN_COMPLETE. We can't send one
1194f8829a4aSRandall Stewart 			 * here since we don't have their tag.
1195f8829a4aSRandall Stewart 			 */
1196f8829a4aSRandall Stewart 			break;
1197f8829a4aSRandall Stewart 		case SCTP_CAUSE_DELETING_LAST_ADDR:
1198f8829a4aSRandall Stewart 		case SCTP_CAUSE_RESOURCE_SHORTAGE:
1199f8829a4aSRandall Stewart 		case SCTP_CAUSE_DELETING_SRC_ADDR:
1200f8829a4aSRandall Stewart 			/*
1201f8829a4aSRandall Stewart 			 * We should NOT get these here, but in a
120293164cf9SRandall Stewart 			 * ASCONF-ACK.
1203f8829a4aSRandall Stewart 			 */
12043e87bccdSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a error cause with code %u.\n",
12053e87bccdSMichael Tuexen 			    cause_code);
1206f8829a4aSRandall Stewart 			break;
1207f8829a4aSRandall Stewart 		case SCTP_CAUSE_OUT_OF_RESC:
1208f8829a4aSRandall Stewart 			/*
1209f8829a4aSRandall Stewart 			 * And what, pray tell do we do with the fact that
1210f8829a4aSRandall Stewart 			 * the peer is out of resources? Not really sure we
121193164cf9SRandall Stewart 			 * could do anything but abort. I suspect this
1212f8829a4aSRandall Stewart 			 * should have came WITH an abort instead of in a
1213f8829a4aSRandall Stewart 			 * OP-ERROR.
1214f8829a4aSRandall Stewart 			 */
1215f8829a4aSRandall Stewart 			break;
1216f8829a4aSRandall Stewart 		default:
12173e87bccdSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown code 0x%x\n",
12183e87bccdSMichael Tuexen 			    cause_code);
1219f8829a4aSRandall Stewart 			break;
1220f8829a4aSRandall Stewart 		}
12213e87bccdSMichael Tuexen 		adjust = SCTP_SIZE32(cause_length);
12223e87bccdSMichael Tuexen 		if (remaining_length >= adjust) {
12233e87bccdSMichael Tuexen 			remaining_length -= adjust;
12243e87bccdSMichael Tuexen 		} else {
12253e87bccdSMichael Tuexen 			remaining_length = 0;
1226f8829a4aSRandall Stewart 		}
12273e87bccdSMichael Tuexen 		cause = (struct sctp_error_cause *)((caddr_t)cause + adjust);
12283e87bccdSMichael Tuexen 	}
12293e87bccdSMichael Tuexen 	sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, code, ch, SCTP_SO_NOT_LOCKED);
1230f8829a4aSRandall Stewart 	return (0);
1231f8829a4aSRandall Stewart }
1232f8829a4aSRandall Stewart 
1233f8829a4aSRandall Stewart static int
1234b1754ad1SMichael Tuexen sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1235b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
1236f30ac432SMichael Tuexen     struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1237f30ac432SMichael Tuexen     struct sctp_nets *net, int *abort_no_unlock,
1238457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
1239f30ac432SMichael Tuexen     uint32_t vrf_id)
1240f8829a4aSRandall Stewart {
1241f8829a4aSRandall Stewart 	struct sctp_init_ack *init_ack;
1242f8829a4aSRandall Stewart 	struct mbuf *op_err;
1243f8829a4aSRandall Stewart 
1244ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
1245ad81507eSRandall Stewart 	    "sctp_handle_init_ack: handling INIT-ACK\n");
1246ad81507eSRandall Stewart 
1247f8829a4aSRandall Stewart 	if (stcb == NULL) {
1248ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
1249ad81507eSRandall Stewart 		    "sctp_handle_init_ack: TCB is null\n");
1250f8829a4aSRandall Stewart 		return (-1);
1251f8829a4aSRandall Stewart 	}
1252a3665770SMichael Tuexen 	/* Only process the INIT-ACK chunk in COOKIE WAIT state. */
1253a3665770SMichael Tuexen 	if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) {
1254f8829a4aSRandall Stewart 		init_ack = &cp->init;
1255059ec222SMichael Tuexen 		/* Validate parameters. */
1256059ec222SMichael Tuexen 		if ((ntohl(init_ack->initiate_tag) == 0) ||
1257059ec222SMichael Tuexen 		    (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) ||
1258059ec222SMichael Tuexen 		    (ntohs(init_ack->num_inbound_streams) == 0) ||
1259059ec222SMichael Tuexen 		    (ntohs(init_ack->num_outbound_streams) == 0)) {
1260a3665770SMichael Tuexen 			/* One of the mandatory parameters is illegal. */
1261ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1262b1754ad1SMichael Tuexen 			sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1263b1754ad1SMichael Tuexen 			    src, dst, sh, op_err,
1264457b4b88SMichael Tuexen 			    mflowtype, mflowid,
1265f30ac432SMichael Tuexen 			    vrf_id, net->port);
1266bff64a4dSRandall Stewart 			*abort_no_unlock = 1;
1267f8829a4aSRandall Stewart 			return (-1);
1268f8829a4aSRandall Stewart 		}
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 		}
1289a3665770SMichael Tuexen 		/* 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 
1293a3665770SMichael Tuexen 		/* Reset the RTO calculation. */
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 		/*
1304a3665770SMichael Tuexen 		 * Collapse the init timer back in case of a exponential
1305a3665770SMichael Tuexen 		 * backoff.
1306f8829a4aSRandall Stewart 		 */
1307f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1308f8829a4aSRandall Stewart 		    stcb, net);
1309f8829a4aSRandall Stewart 		/*
1310a3665770SMichael Tuexen 		 * The output routine at the end of the inbound data
1311a3665770SMichael Tuexen 		 * processing will cause the cookie to be sent.
1312f8829a4aSRandall Stewart 		 */
1313ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1314f8829a4aSRandall Stewart 		return (0);
1315a3665770SMichael Tuexen 	} else {
1316a3665770SMichael Tuexen 		return (-1);
1317a3665770SMichael Tuexen 	}
1318f8829a4aSRandall Stewart }
1319f8829a4aSRandall Stewart 
1320830d754dSRandall Stewart static struct sctp_tcb *
1321830d754dSRandall Stewart sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1322b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
1323830d754dSRandall Stewart     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1324830d754dSRandall Stewart     struct sctp_inpcb *inp, struct sctp_nets **netp,
1325830d754dSRandall Stewart     struct sockaddr *init_src, int *notification,
1326830d754dSRandall Stewart     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1327457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
1328830d754dSRandall Stewart     uint32_t vrf_id, uint16_t port);
1329830d754dSRandall Stewart 
1330f8829a4aSRandall Stewart /*
1331f8829a4aSRandall Stewart  * handle a state cookie for an existing association m: input packet mbuf
1332f8829a4aSRandall Stewart  * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1333f8829a4aSRandall Stewart  * "split" mbuf and the cookie signature does not exist offset: offset into
1334f8829a4aSRandall Stewart  * mbuf to the cookie-echo chunk
1335f8829a4aSRandall Stewart  */
1336f8829a4aSRandall Stewart static struct sctp_tcb *
1337f8829a4aSRandall Stewart sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1338b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
1339f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1340830d754dSRandall Stewart     struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
13417215cc1bSMichael Tuexen     struct sockaddr *init_src, int *notification,
1342f30ac432SMichael Tuexen     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1343457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
1344f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
1345f8829a4aSRandall Stewart {
1346f8829a4aSRandall Stewart 	struct sctp_association *asoc;
1347f8829a4aSRandall Stewart 	struct sctp_init_chunk *init_cp, init_buf;
1348f8829a4aSRandall Stewart 	struct sctp_init_ack_chunk *initack_cp, initack_buf;
1349aa6db9a0SMichael Tuexen 	struct sctp_asconf_addr *aparam, *naparam;
1350aa6db9a0SMichael Tuexen 	struct sctp_asconf_ack *aack, *naack;
1351aa6db9a0SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
1352aa6db9a0SMichael Tuexen 	struct sctp_stream_reset_list *strrst, *nstrrst;
1353aa6db9a0SMichael Tuexen 	struct sctp_queued_to_read *sq, *nsq;
1354830d754dSRandall Stewart 	struct sctp_nets *net;
1355830d754dSRandall Stewart 	struct mbuf *op_err;
1356a5d547adSRandall Stewart 	int init_offset, initack_offset, i;
1357f8829a4aSRandall Stewart 	int retval;
13587f34832bSRandall Stewart 	int spec_flag = 0;
13594c9179adSRandall Stewart 	uint32_t how_indx;
1360f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
1361f0396ad1SMichael Tuexen 	int j;
1362f0396ad1SMichael Tuexen #endif
1363f0396ad1SMichael Tuexen 
1364830d754dSRandall Stewart 	net = *netp;
1365f8829a4aSRandall Stewart 	/* I know that the TCB is non-NULL from the caller */
1366f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
1367f42a358aSRandall Stewart 	for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
136844b7479bSRandall Stewart 		if (asoc->cookie_how[how_indx] == 0)
136944b7479bSRandall Stewart 			break;
137044b7479bSRandall Stewart 	}
137144b7479bSRandall Stewart 	if (how_indx < sizeof(asoc->cookie_how)) {
137244b7479bSRandall Stewart 		asoc->cookie_how[how_indx] = 1;
137344b7479bSRandall Stewart 	}
1374839d21d6SMichael Tuexen 	if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1375f8829a4aSRandall Stewart 		/* SHUTDOWN came in after sending INIT-ACK */
1376f8829a4aSRandall Stewart 		sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1377ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_COOKIE_IN_SHUTDOWN, "");
1378b1754ad1SMichael Tuexen 		sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
1379d089f9b9SMichael Tuexen 		    mflowtype, mflowid, inp->fibnum,
1380c54a18d2SRandall Stewart 		    vrf_id, net->port);
138144b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
138244b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 2;
138312dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1384f8829a4aSRandall Stewart 		return (NULL);
1385f8829a4aSRandall Stewart 	}
1386f8829a4aSRandall Stewart 	/*
1387f8829a4aSRandall Stewart 	 * find and validate the INIT chunk in the cookie (peer's info) the
1388f8829a4aSRandall Stewart 	 * INIT should start after the cookie-echo header struct (chunk
1389f8829a4aSRandall Stewart 	 * header, state cookie header struct)
1390f8829a4aSRandall Stewart 	 */
1391f8829a4aSRandall Stewart 	init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1392f8829a4aSRandall Stewart 
1393f8829a4aSRandall Stewart 	init_cp = (struct sctp_init_chunk *)
1394f8829a4aSRandall Stewart 	    sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1395f8829a4aSRandall Stewart 	    (uint8_t *)&init_buf);
1396f8829a4aSRandall Stewart 	if (init_cp == NULL) {
1397f8829a4aSRandall Stewart 		/* could not pull a INIT chunk in cookie */
139812dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1399f8829a4aSRandall Stewart 		return (NULL);
1400f8829a4aSRandall Stewart 	}
1401f8829a4aSRandall Stewart 	if (init_cp->ch.chunk_type != SCTP_INITIATION) {
140212dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1403f8829a4aSRandall Stewart 		return (NULL);
1404f8829a4aSRandall Stewart 	}
1405f8829a4aSRandall Stewart 	/*
1406f8829a4aSRandall Stewart 	 * find and validate the INIT-ACK chunk in the cookie (my info) the
1407f8829a4aSRandall Stewart 	 * INIT-ACK follows the INIT chunk
1408f8829a4aSRandall Stewart 	 */
140960990c0cSMichael Tuexen 	initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1410f8829a4aSRandall Stewart 	initack_cp = (struct sctp_init_ack_chunk *)
1411f8829a4aSRandall Stewart 	    sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1412f8829a4aSRandall Stewart 	    (uint8_t *)&initack_buf);
1413f8829a4aSRandall Stewart 	if (initack_cp == NULL) {
1414f8829a4aSRandall Stewart 		/* could not pull INIT-ACK chunk in cookie */
141512dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1416f8829a4aSRandall Stewart 		return (NULL);
1417f8829a4aSRandall Stewart 	}
1418f8829a4aSRandall Stewart 	if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
141912dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1420f8829a4aSRandall Stewart 		return (NULL);
1421f8829a4aSRandall Stewart 	}
1422f8829a4aSRandall Stewart 	if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1423f8829a4aSRandall Stewart 	    (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1424f8829a4aSRandall Stewart 		/*
1425f8829a4aSRandall Stewart 		 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1426f8829a4aSRandall Stewart 		 * to get into the OPEN state
1427f8829a4aSRandall Stewart 		 */
142844b7479bSRandall Stewart 		if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1429851b7298SRandall Stewart 			/*-
1430851b7298SRandall Stewart 			 * Opps, this means that we somehow generated two vtag's
1431851b7298SRandall Stewart 			 * the same. I.e. we did:
1432851b7298SRandall Stewart 			 *  Us               Peer
1433851b7298SRandall Stewart 			 *   <---INIT(tag=a)------
1434851b7298SRandall Stewart 			 *   ----INIT-ACK(tag=t)-->
1435851b7298SRandall Stewart 			 *   ----INIT(tag=t)------> *1
1436851b7298SRandall Stewart 			 *   <---INIT-ACK(tag=a)---
1437851b7298SRandall Stewart 			 *   <----CE(tag=t)------------- *2
1438851b7298SRandall Stewart 			 *
1439851b7298SRandall Stewart 			 * At point *1 we should be generating a different
1440851b7298SRandall Stewart 			 * tag t'. Which means we would throw away the CE and send
1441851b7298SRandall Stewart 			 * ours instead. Basically this is case C (throw away side).
1442851b7298SRandall Stewart 			 */
1443851b7298SRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
1444851b7298SRandall Stewart 				asoc->cookie_how[how_indx] = 17;
144512dda000SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
1446851b7298SRandall Stewart 			return (NULL);
144744b7479bSRandall Stewart 		}
1448839d21d6SMichael Tuexen 		switch (SCTP_GET_STATE(stcb)) {
1449f8829a4aSRandall Stewart 		case SCTP_STATE_COOKIE_WAIT:
145044b7479bSRandall Stewart 		case SCTP_STATE_COOKIE_ECHOED:
1451f8829a4aSRandall Stewart 			/*
145217205eccSRandall Stewart 			 * INIT was sent but got a COOKIE_ECHO with the
145344b7479bSRandall Stewart 			 * correct tags... just accept it...but we must
145444b7479bSRandall Stewart 			 * process the init so that we can make sure we have
145544b7479bSRandall Stewart 			 * the right seq no's.
1456f8829a4aSRandall Stewart 			 */
1457f8829a4aSRandall Stewart 			/* First we must process the INIT !! */
14585f2e1835SMichael Tuexen 			if (sctp_process_init(init_cp, stcb) < 0) {
145944b7479bSRandall Stewart 				if (how_indx < sizeof(asoc->cookie_how))
146044b7479bSRandall Stewart 					asoc->cookie_how[how_indx] = 3;
14615f2e1835SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
14625f2e1835SMichael Tuexen 				SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_init() failed\n");
14635f2e1835SMichael Tuexen 				sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
14645f2e1835SMichael Tuexen 				    src, dst, sh, op_err,
14655f2e1835SMichael Tuexen 				    mflowtype, mflowid,
14665f2e1835SMichael Tuexen 				    vrf_id, net->port);
1467f8829a4aSRandall Stewart 				return (NULL);
1468f8829a4aSRandall Stewart 			}
1469f8829a4aSRandall Stewart 			/* we have already processed the INIT so no problem */
1470b7d130beSMichael Tuexen 			sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp,
1471b7d130beSMichael Tuexen 			    stcb, net,
1472b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1473b7d130beSMichael Tuexen 			sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp,
1474b7d130beSMichael Tuexen 			    stcb, net,
1475b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1476f8829a4aSRandall Stewart 			/* update current state */
1477839d21d6SMichael Tuexen 			if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)
1478f42a358aSRandall Stewart 				SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1479f42a358aSRandall Stewart 			else
1480f42a358aSRandall Stewart 				SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1481c4739e2fSRandall Stewart 
1482839d21d6SMichael Tuexen 			SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1483f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1484f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
14856fb7b4fbSMichael Tuexen 				    stcb->sctp_ep, stcb, NULL);
1486f8829a4aSRandall Stewart 			}
1487f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1488a5d547adSRandall Stewart 			sctp_stop_all_cookie_timers(stcb);
1489f8829a4aSRandall Stewart 			if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1490f8829a4aSRandall Stewart 			    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
14915d08768aSMichael Tuexen 			    (!SCTP_IS_LISTENING(inp))) {
1492f8829a4aSRandall Stewart 				/*
1493f8829a4aSRandall Stewart 				 * Here is where collision would go if we
1494f8829a4aSRandall Stewart 				 * did a connect() and instead got a
1495f8829a4aSRandall Stewart 				 * init/init-ack/cookie done before the
1496f8829a4aSRandall Stewart 				 * init-ack came back..
1497f8829a4aSRandall Stewart 				 */
1498a5c2009dSMichael Tuexen 				sctp_pcb_add_flags(stcb->sctp_ep, SCTP_PCB_FLAGS_CONNECTED);
1499ceaad40aSRandall Stewart 				soisconnected(stcb->sctp_socket);
1500f8829a4aSRandall Stewart 			}
1501f8829a4aSRandall Stewart 			/* notify upper layer */
1502f8829a4aSRandall Stewart 			*notification = SCTP_NOTIFY_ASSOC_UP;
1503f8829a4aSRandall Stewart 			net->hb_responded = 1;
1504f8829a4aSRandall Stewart 			if (stcb->asoc.sctp_autoclose_ticks &&
1505f8829a4aSRandall Stewart 			    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1506f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1507f8829a4aSRandall Stewart 				    inp, stcb, NULL);
1508f8829a4aSRandall Stewart 			}
1509f8829a4aSRandall Stewart 			break;
1510f8829a4aSRandall Stewart 		default:
1511f8829a4aSRandall Stewart 			/*
1512f8829a4aSRandall Stewart 			 * we're in the OPEN state (or beyond), so peer must
1513f8829a4aSRandall Stewart 			 * have simply lost the COOKIE-ACK
1514f8829a4aSRandall Stewart 			 */
1515f8829a4aSRandall Stewart 			break;
1516f8829a4aSRandall Stewart 		}		/* end switch */
1517a5d547adSRandall Stewart 		sctp_stop_all_cookie_timers(stcb);
1518655c200cSAlexander Motin 		if ((retval = sctp_load_addresses_from_init(stcb, m,
1519f8829a4aSRandall Stewart 		    init_offset + sizeof(struct sctp_init_chunk),
15205f2e1835SMichael Tuexen 		    initack_offset, src, dst, init_src, stcb->asoc.port)) < 0) {
152144b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
152244b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 4;
15235f2e1835SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
15245f2e1835SMichael Tuexen 			    "Problem with address parameters");
15255f2e1835SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1,
15265f2e1835SMichael Tuexen 			    "Load addresses from INIT causes an abort %d\n",
15275f2e1835SMichael Tuexen 			    retval);
15285f2e1835SMichael Tuexen 			sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
15295f2e1835SMichael Tuexen 			    src, dst, sh, op_err,
15305f2e1835SMichael Tuexen 			    mflowtype, mflowid,
15315f2e1835SMichael Tuexen 			    vrf_id, net->port);
1532f8829a4aSRandall Stewart 			return (NULL);
1533f8829a4aSRandall Stewart 		}
1534f8829a4aSRandall Stewart 		/* respond with a COOKIE-ACK */
1535a5d547adSRandall Stewart 		sctp_toss_old_cookies(stcb, asoc);
1536f8829a4aSRandall Stewart 		sctp_send_cookie_ack(stcb);
153744b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
153844b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 5;
1539f8829a4aSRandall Stewart 		return (stcb);
154017205eccSRandall Stewart 	}
15410053ed28SMichael Tuexen 
1542f8829a4aSRandall Stewart 	if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1543f8829a4aSRandall Stewart 	    ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1544f8829a4aSRandall Stewart 	    cookie->tie_tag_my_vtag == 0 &&
1545f8829a4aSRandall Stewart 	    cookie->tie_tag_peer_vtag == 0) {
1546f8829a4aSRandall Stewart 		/*
1547f8829a4aSRandall Stewart 		 * case C in Section 5.2.4 Table 2: XMOO silently discard
1548f8829a4aSRandall Stewart 		 */
154944b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
155044b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 6;
155112dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1552f8829a4aSRandall Stewart 		return (NULL);
1553f8829a4aSRandall Stewart 	}
1554830d754dSRandall Stewart 	/*
1555830d754dSRandall Stewart 	 * If nat support, and the below and stcb is established, send back
1556830d754dSRandall Stewart 	 * a ABORT(colliding state) if we are established.
1557830d754dSRandall Stewart 	 */
1558839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) &&
1559830d754dSRandall Stewart 	    (asoc->peer_supports_nat) &&
1560830d754dSRandall Stewart 	    ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1561830d754dSRandall Stewart 	    ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1562830d754dSRandall Stewart 	    (asoc->peer_vtag == 0)))) {
1563830d754dSRandall Stewart 		/*
1564830d754dSRandall Stewart 		 * Special case - Peer's support nat. We may have two init's
1565830d754dSRandall Stewart 		 * that we gave out the same tag on since one was not
1566830d754dSRandall Stewart 		 * established.. i.e. we get INIT from host-1 behind the nat
1567830d754dSRandall Stewart 		 * and we respond tag-a, we get a INIT from host-2 behind
1568830d754dSRandall Stewart 		 * the nat and we get tag-a again. Then we bring up host-1
1569830d754dSRandall Stewart 		 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1570830d754dSRandall Stewart 		 * Now we have colliding state. We must send an abort here
1571830d754dSRandall Stewart 		 * with colliding state indication.
1572830d754dSRandall Stewart 		 */
1573ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_NAT_COLLIDING_STATE, "");
1574b1754ad1SMichael Tuexen 		sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
1575d089f9b9SMichael Tuexen 		    mflowtype, mflowid, inp->fibnum,
1576f30ac432SMichael Tuexen 		    vrf_id, port);
157712dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1578830d754dSRandall Stewart 		return (NULL);
1579830d754dSRandall Stewart 	}
1580830d754dSRandall Stewart 	if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1581830d754dSRandall Stewart 	    ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1582830d754dSRandall Stewart 	    (asoc->peer_vtag == 0))) {
1583f8829a4aSRandall Stewart 		/*
1584f8829a4aSRandall Stewart 		 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1585f8829a4aSRandall Stewart 		 * should be ok, re-accept peer info
1586f8829a4aSRandall Stewart 		 */
158744b7479bSRandall Stewart 		if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
158844b7479bSRandall Stewart 			/*
158944b7479bSRandall Stewart 			 * Extension of case C. If we hit this, then the
159044b7479bSRandall Stewart 			 * random number generator returned the same vtag
159144b7479bSRandall Stewart 			 * when we first sent our INIT-ACK and when we later
159244b7479bSRandall Stewart 			 * sent our INIT. The side with the seq numbers that
1593e7e65008SMichael Tuexen 			 * are different will be the one that normally would
1594e7e65008SMichael Tuexen 			 * have hit case C. This in effect "extends" our
1595e7e65008SMichael Tuexen 			 * vtags in this collision case to be 64 bits. The
1596e7e65008SMichael Tuexen 			 * same collision could occur aka you get both vtag
1597e7e65008SMichael Tuexen 			 * and seq number the same twice in a row.. but is
1598e7e65008SMichael Tuexen 			 * much less likely. If it did happen then we would
1599e7e65008SMichael Tuexen 			 * proceed through and bring up the assoc.. we may
1600e7e65008SMichael Tuexen 			 * end up with the wrong stream setup however..
160144b7479bSRandall Stewart 			 * which would be bad.. but there is no way to
160244b7479bSRandall Stewart 			 * tell.. until we send on a stream that does not
160344b7479bSRandall Stewart 			 * exist :-)
160444b7479bSRandall Stewart 			 */
160544b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
160644b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 7;
160744b7479bSRandall Stewart 
160812dda000SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
160944b7479bSRandall Stewart 			return (NULL);
161044b7479bSRandall Stewart 		}
161144b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
161244b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 8;
1613b7d130beSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1614b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1615f8829a4aSRandall Stewart 		sctp_stop_all_cookie_timers(stcb);
1616f8829a4aSRandall Stewart 		/*
1617f8829a4aSRandall Stewart 		 * since we did not send a HB make sure we don't double
1618f8829a4aSRandall Stewart 		 * things
1619f8829a4aSRandall Stewart 		 */
1620f8829a4aSRandall Stewart 		net->hb_responded = 1;
1621f8829a4aSRandall Stewart 		if (stcb->asoc.sctp_autoclose_ticks &&
1622f8829a4aSRandall Stewart 		    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1623f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1624f8829a4aSRandall Stewart 			    NULL);
1625f8829a4aSRandall Stewart 		}
1626f8829a4aSRandall Stewart 		asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
16270066de1cSMichael Tuexen 		if (asoc->pre_open_streams < asoc->streamoutcnt) {
16280066de1cSMichael Tuexen 			asoc->pre_open_streams = asoc->streamoutcnt;
16290066de1cSMichael Tuexen 		}
1630f8829a4aSRandall Stewart 
16317f34832bSRandall Stewart 		if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
16327f34832bSRandall Stewart 			/*
16337f34832bSRandall Stewart 			 * Ok the peer probably discarded our data (if we
16347f34832bSRandall Stewart 			 * echoed a cookie+data). So anything on the
16357f34832bSRandall Stewart 			 * sent_queue should be marked for retransmit, we
16367f34832bSRandall Stewart 			 * may not get something to kick us so it COULD
16377f34832bSRandall Stewart 			 * still take a timeout to move these.. but it can't
16387f34832bSRandall Stewart 			 * hurt to mark them.
16397f34832bSRandall Stewart 			 */
16407f34832bSRandall Stewart 
16417f34832bSRandall Stewart 			TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
16427f34832bSRandall Stewart 				if (chk->sent < SCTP_DATAGRAM_RESEND) {
16437f34832bSRandall Stewart 					chk->sent = SCTP_DATAGRAM_RESEND;
1644b54d3a6cSRandall Stewart 					sctp_flight_size_decrease(chk);
1645b54d3a6cSRandall Stewart 					sctp_total_flight_decrease(stcb, chk);
16465e54f665SRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
16477f34832bSRandall Stewart 					spec_flag++;
16487f34832bSRandall Stewart 				}
16497f34832bSRandall Stewart 			}
16507f34832bSRandall Stewart 		}
1651f8829a4aSRandall Stewart 		/* process the INIT info (peer's info) */
16525f2e1835SMichael Tuexen 		if (sctp_process_init(init_cp, stcb) < 0) {
165344b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
165444b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 9;
16555f2e1835SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
16565f2e1835SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_init() failed\n");
16575f2e1835SMichael Tuexen 			sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
16585f2e1835SMichael Tuexen 			    src, dst, sh, op_err,
16595f2e1835SMichael Tuexen 			    mflowtype, mflowid,
16605f2e1835SMichael Tuexen 			    vrf_id, net->port);
1661f8829a4aSRandall Stewart 			return (NULL);
1662f8829a4aSRandall Stewart 		}
16635f2e1835SMichael Tuexen 		if ((retval = sctp_load_addresses_from_init(stcb, m,
1664f8829a4aSRandall Stewart 		    init_offset + sizeof(struct sctp_init_chunk),
16655f2e1835SMichael Tuexen 		    initack_offset, src, dst, init_src, stcb->asoc.port)) < 0) {
166644b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
166744b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 10;
16685f2e1835SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
16695f2e1835SMichael Tuexen 			    "Problem with address parameters");
16705f2e1835SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1,
16715f2e1835SMichael Tuexen 			    "Load addresses from INIT causes an abort %d\n",
16725f2e1835SMichael Tuexen 			    retval);
16735f2e1835SMichael Tuexen 			sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
16745f2e1835SMichael Tuexen 			    src, dst, sh, op_err,
16755f2e1835SMichael Tuexen 			    mflowtype, mflowid,
16765f2e1835SMichael Tuexen 			    vrf_id, net->port);
1677f8829a4aSRandall Stewart 			return (NULL);
1678f8829a4aSRandall Stewart 		}
1679839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
1680839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
1681f8829a4aSRandall Stewart 			*notification = SCTP_NOTIFY_ASSOC_UP;
1682f8829a4aSRandall Stewart 
1683f8829a4aSRandall Stewart 			if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1684f8829a4aSRandall Stewart 			    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
16855d08768aSMichael Tuexen 			    (!SCTP_IS_LISTENING(inp))) {
1686a5c2009dSMichael Tuexen 				sctp_pcb_add_flags(stcb->sctp_ep, SCTP_PCB_FLAGS_CONNECTED);
1687ceaad40aSRandall Stewart 				soisconnected(stcb->sctp_socket);
1688f8829a4aSRandall Stewart 			}
1689839d21d6SMichael Tuexen 			if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)
1690f42a358aSRandall Stewart 				SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1691f42a358aSRandall Stewart 			else
1692f42a358aSRandall Stewart 				SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1693f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1694839d21d6SMichael Tuexen 		} else if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
1695f42a358aSRandall Stewart 			SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1696f42a358aSRandall Stewart 		} else {
1697f42a358aSRandall Stewart 			SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1698f8829a4aSRandall Stewart 		}
1699839d21d6SMichael Tuexen 		SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1700f8829a4aSRandall Stewart 		if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1701f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
17026fb7b4fbSMichael Tuexen 			    stcb->sctp_ep, stcb, NULL);
1703f8829a4aSRandall Stewart 		}
1704f8829a4aSRandall Stewart 		sctp_stop_all_cookie_timers(stcb);
1705a5d547adSRandall Stewart 		sctp_toss_old_cookies(stcb, asoc);
1706f8829a4aSRandall Stewart 		sctp_send_cookie_ack(stcb);
17077f34832bSRandall Stewart 		if (spec_flag) {
17087f34832bSRandall Stewart 			/*
17097f34832bSRandall Stewart 			 * only if we have retrans set do we do this. What
17107f34832bSRandall Stewart 			 * this call does is get only the COOKIE-ACK out and
17117f34832bSRandall Stewart 			 * then when we return the normal call to
17127f34832bSRandall Stewart 			 * sctp_chunk_output will get the retrans out behind
17137f34832bSRandall Stewart 			 * this.
17147f34832bSRandall Stewart 			 */
1715ceaad40aSRandall Stewart 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
17167f34832bSRandall Stewart 		}
171744b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
171844b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 11;
171944b7479bSRandall Stewart 
1720f8829a4aSRandall Stewart 		return (stcb);
1721f8829a4aSRandall Stewart 	}
1722f8829a4aSRandall Stewart 	if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1723f8829a4aSRandall Stewart 	    ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1724f8829a4aSRandall Stewart 	    cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1725f8829a4aSRandall Stewart 	    cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1726f8829a4aSRandall Stewart 	    cookie->tie_tag_peer_vtag != 0) {
1727f8829a4aSRandall Stewart 		struct sctpasochead *head;
172856f778aaSMichael Tuexen 
1729830d754dSRandall Stewart 		if (asoc->peer_supports_nat) {
1730eec6aed5SMichael Tuexen 			struct sctp_tcb *local_stcb;
1731eec6aed5SMichael Tuexen 
1732830d754dSRandall Stewart 			/*
173356f778aaSMichael Tuexen 			 * This is a gross gross hack. Just call the
1734830d754dSRandall Stewart 			 * cookie_new code since we are allowing a duplicate
1735830d754dSRandall Stewart 			 * association. I hope this works...
1736830d754dSRandall Stewart 			 */
1737eec6aed5SMichael Tuexen 			local_stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst,
1738b1754ad1SMichael Tuexen 			    sh, cookie, cookie_len,
1739830d754dSRandall Stewart 			    inp, netp, init_src, notification,
1740830d754dSRandall Stewart 			    auth_skipped, auth_offset, auth_len,
1741457b4b88SMichael Tuexen 			    mflowtype, mflowid,
1742eec6aed5SMichael Tuexen 			    vrf_id, port);
1743eec6aed5SMichael Tuexen 			if (local_stcb == NULL) {
1744eec6aed5SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
1745eec6aed5SMichael Tuexen 			}
1746eec6aed5SMichael Tuexen 			return (local_stcb);
1747830d754dSRandall Stewart 		}
1748f8829a4aSRandall Stewart 		/*
1749f8829a4aSRandall Stewart 		 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1750f8829a4aSRandall Stewart 		 */
1751a5d547adSRandall Stewart 		/* temp code */
175244b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
175344b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 12;
1754a89481d3SMichael Tuexen 		sctp_stop_association_timers(stcb, false);
1755f8829a4aSRandall Stewart 		/* notify upper layer */
1756f8829a4aSRandall Stewart 		*notification = SCTP_NOTIFY_ASSOC_RESTART;
1757a5d547adSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
1758839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) != SCTP_STATE_OPEN) &&
1759839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1760839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT)) {
1761f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1762f42a358aSRandall Stewart 		}
1763839d21d6SMichael Tuexen 		if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
1764f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1765839d21d6SMichael Tuexen 		} else if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) {
1766f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1767f42a358aSRandall Stewart 		}
1768139bc87fSRandall Stewart 		if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1769839d21d6SMichael Tuexen 			SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1770139bc87fSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
17716fb7b4fbSMichael Tuexen 			    stcb->sctp_ep, stcb, NULL);
1772139bc87fSRandall Stewart 
1773839d21d6SMichael Tuexen 		} else if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) {
1774139bc87fSRandall Stewart 			/* move to OPEN state, if not in SHUTDOWN_SENT */
1775839d21d6SMichael Tuexen 			SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1776139bc87fSRandall Stewart 		}
17770066de1cSMichael Tuexen 		if (asoc->pre_open_streams < asoc->streamoutcnt) {
17780066de1cSMichael Tuexen 			asoc->pre_open_streams = asoc->streamoutcnt;
17790066de1cSMichael Tuexen 		}
1780139bc87fSRandall Stewart 		asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1781139bc87fSRandall Stewart 		asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1782c54a18d2SRandall Stewart 		asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1783139bc87fSRandall Stewart 		asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1784139bc87fSRandall Stewart 		asoc->str_reset_seq_in = asoc->init_seq_number;
1785139bc87fSRandall Stewart 		asoc->advanced_peer_ack_point = asoc->last_acked_seq;
17866f155d69SMichael Tuexen 		asoc->send_sack = 1;
1787a89481d3SMichael Tuexen 		asoc->data_pkts_seen = 0;
1788a89481d3SMichael Tuexen 		asoc->last_data_chunk_from = NULL;
1789a89481d3SMichael Tuexen 		asoc->last_control_chunk_from = NULL;
1790a89481d3SMichael Tuexen 		asoc->last_net_cmt_send_started = NULL;
17910696e120SRandall Stewart 		if (asoc->mapping_array) {
1792139bc87fSRandall Stewart 			memset(asoc->mapping_array, 0,
1793139bc87fSRandall Stewart 			    asoc->mapping_array_size);
17940696e120SRandall Stewart 		}
179577acdc25SRandall Stewart 		if (asoc->nr_mapping_array) {
1796830d754dSRandall Stewart 			memset(asoc->nr_mapping_array, 0,
1797b5c16493SMichael Tuexen 			    asoc->mapping_array_size);
1798830d754dSRandall Stewart 		}
1799a5d547adSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
1800a5d547adSRandall Stewart 		SCTP_INP_INFO_WLOCK();
1801a5d547adSRandall Stewart 		SCTP_INP_WLOCK(stcb->sctp_ep);
1802a5d547adSRandall Stewart 		SCTP_TCB_LOCK(stcb);
18033c1ba6f3SMichael Tuexen 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
1804f8829a4aSRandall Stewart 		/* send up all the data */
1805f5d30f7fSMichael Tuexen 		sctp_report_all_outbound(stcb, 0, SCTP_SO_LOCKED);
1806a5d547adSRandall Stewart 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1807325c8c46SMichael Tuexen 			stcb->asoc.strmout[i].chunks_on_queues = 0;
1808f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
1809f0396ad1SMichael Tuexen 			for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1810f0396ad1SMichael Tuexen 				asoc->strmout[i].abandoned_sent[j] = 0;
1811f0396ad1SMichael Tuexen 				asoc->strmout[i].abandoned_unsent[j] = 0;
1812f0396ad1SMichael Tuexen 			}
1813f0396ad1SMichael Tuexen #else
1814f0396ad1SMichael Tuexen 			asoc->strmout[i].abandoned_sent[0] = 0;
1815f0396ad1SMichael Tuexen 			asoc->strmout[i].abandoned_unsent[0] = 0;
1816f0396ad1SMichael Tuexen #endif
181763d5b568SMichael Tuexen 			stcb->asoc.strmout[i].next_mid_ordered = 0;
181863d5b568SMichael Tuexen 			stcb->asoc.strmout[i].next_mid_unordered = 0;
18197a051c0aSMichael Tuexen 			stcb->asoc.strmout[i].sid = i;
1820a5d547adSRandall Stewart 			stcb->asoc.strmout[i].last_msg_incomplete = 0;
1821a5d547adSRandall Stewart 		}
1822aa6db9a0SMichael Tuexen 		TAILQ_FOREACH_SAFE(strrst, &asoc->resetHead, next_resp, nstrrst) {
1823aa6db9a0SMichael Tuexen 			TAILQ_REMOVE(&asoc->resetHead, strrst, next_resp);
1824aa6db9a0SMichael Tuexen 			SCTP_FREE(strrst, SCTP_M_STRESET);
1825aa6db9a0SMichael Tuexen 		}
1826aa6db9a0SMichael Tuexen 		TAILQ_FOREACH_SAFE(sq, &asoc->pending_reply_queue, next, nsq) {
1827aa6db9a0SMichael Tuexen 			TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
1828aa6db9a0SMichael Tuexen 			if (sq->data) {
1829aa6db9a0SMichael Tuexen 				sctp_m_freem(sq->data);
1830aa6db9a0SMichael Tuexen 				sq->data = NULL;
1831aa6db9a0SMichael Tuexen 			}
1832aa6db9a0SMichael Tuexen 			sctp_free_remote_addr(sq->whoFrom);
1833aa6db9a0SMichael Tuexen 			sq->whoFrom = NULL;
1834aa6db9a0SMichael Tuexen 			sq->stcb = NULL;
1835aa6db9a0SMichael Tuexen 			sctp_free_a_readq(stcb, sq);
1836aa6db9a0SMichael Tuexen 		}
1837aa6db9a0SMichael Tuexen 		TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
1838aa6db9a0SMichael Tuexen 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
1839aa6db9a0SMichael Tuexen 			if (chk->data) {
1840aa6db9a0SMichael Tuexen 				sctp_m_freem(chk->data);
1841aa6db9a0SMichael Tuexen 				chk->data = NULL;
1842aa6db9a0SMichael Tuexen 			}
1843aa6db9a0SMichael Tuexen 			if (chk->holds_key_ref)
1844aa6db9a0SMichael Tuexen 				sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
1845aa6db9a0SMichael Tuexen 			sctp_free_remote_addr(chk->whoTo);
1846aa6db9a0SMichael Tuexen 			SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
1847aa6db9a0SMichael Tuexen 			SCTP_DECR_CHK_COUNT();
1848aa6db9a0SMichael Tuexen 		}
1849a89481d3SMichael Tuexen 		asoc->ctrl_queue_cnt = 0;
1850a89481d3SMichael Tuexen 		asoc->str_reset = NULL;
1851a89481d3SMichael Tuexen 		asoc->stream_reset_outstanding = 0;
1852aa6db9a0SMichael Tuexen 		TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
1853aa6db9a0SMichael Tuexen 			TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
1854aa6db9a0SMichael Tuexen 			if (chk->data) {
1855aa6db9a0SMichael Tuexen 				sctp_m_freem(chk->data);
1856aa6db9a0SMichael Tuexen 				chk->data = NULL;
1857aa6db9a0SMichael Tuexen 			}
1858aa6db9a0SMichael Tuexen 			if (chk->holds_key_ref)
1859aa6db9a0SMichael Tuexen 				sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
1860aa6db9a0SMichael Tuexen 			sctp_free_remote_addr(chk->whoTo);
1861aa6db9a0SMichael Tuexen 			SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
1862aa6db9a0SMichael Tuexen 			SCTP_DECR_CHK_COUNT();
1863aa6db9a0SMichael Tuexen 		}
1864aa6db9a0SMichael Tuexen 		TAILQ_FOREACH_SAFE(aparam, &asoc->asconf_queue, next, naparam) {
1865aa6db9a0SMichael Tuexen 			TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
1866aa6db9a0SMichael Tuexen 			SCTP_FREE(aparam, SCTP_M_ASC_ADDR);
1867aa6db9a0SMichael Tuexen 		}
1868aa6db9a0SMichael Tuexen 		TAILQ_FOREACH_SAFE(aack, &asoc->asconf_ack_sent, next, naack) {
1869aa6db9a0SMichael Tuexen 			TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next);
1870aa6db9a0SMichael Tuexen 			if (aack->data != NULL) {
1871aa6db9a0SMichael Tuexen 				sctp_m_freem(aack->data);
1872aa6db9a0SMichael Tuexen 			}
1873aa6db9a0SMichael Tuexen 			SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack);
1874aa6db9a0SMichael Tuexen 		}
18756026b45aSMichael Tuexen 		asoc->zero_checksum = cookie->zero_checksum;
1876aa6db9a0SMichael Tuexen 
1877f8829a4aSRandall Stewart 		/* process the INIT-ACK info (my info) */
1878f8829a4aSRandall Stewart 		asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1879f8829a4aSRandall Stewart 		asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1880f8829a4aSRandall Stewart 
1881f8829a4aSRandall Stewart 		/* pull from vtag hash */
1882f8829a4aSRandall Stewart 		LIST_REMOVE(stcb, sctp_asocs);
1883f8829a4aSRandall Stewart 		/* re-insert to new vtag position */
1884b3f1ea41SRandall Stewart 		head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1885b3f1ea41SRandall Stewart 		    SCTP_BASE_INFO(hashasocmark))];
1886f8829a4aSRandall Stewart 		/*
1887f8829a4aSRandall Stewart 		 * put it in the bucket in the vtag hash of assoc's for the
1888f8829a4aSRandall Stewart 		 * system
1889f8829a4aSRandall Stewart 		 */
1890f8829a4aSRandall Stewart 		LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1891f8829a4aSRandall Stewart 
1892a5d547adSRandall Stewart 		SCTP_INP_WUNLOCK(stcb->sctp_ep);
1893a5d547adSRandall Stewart 		SCTP_INP_INFO_WUNLOCK();
189456f778aaSMichael Tuexen 		asoc->total_flight = 0;
189556f778aaSMichael Tuexen 		asoc->total_flight_count = 0;
189656f778aaSMichael Tuexen 		/* process the INIT info (peer's info) */
18975f2e1835SMichael Tuexen 		if (sctp_process_init(init_cp, stcb) < 0) {
189844b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
189944b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 13;
19005f2e1835SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
19015f2e1835SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_init() failed\n");
19025f2e1835SMichael Tuexen 			sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
19035f2e1835SMichael Tuexen 			    src, dst, sh, op_err,
19045f2e1835SMichael Tuexen 			    mflowtype, mflowid,
19055f2e1835SMichael Tuexen 			    vrf_id, net->port);
1906f8829a4aSRandall Stewart 			return (NULL);
1907f8829a4aSRandall Stewart 		}
1908f8829a4aSRandall Stewart 		/*
1909f8829a4aSRandall Stewart 		 * since we did not send a HB make sure we don't double
1910f8829a4aSRandall Stewart 		 * things
1911f8829a4aSRandall Stewart 		 */
1912f8829a4aSRandall Stewart 		net->hb_responded = 1;
1913f8829a4aSRandall Stewart 
19145f2e1835SMichael Tuexen 		if ((retval = sctp_load_addresses_from_init(stcb, m,
1915f8829a4aSRandall Stewart 		    init_offset + sizeof(struct sctp_init_chunk),
19165f2e1835SMichael Tuexen 		    initack_offset, src, dst, init_src, stcb->asoc.port)) < 0) {
191744b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
191844b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 14;
19195f2e1835SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
19205f2e1835SMichael Tuexen 			    "Problem with address parameters");
19215f2e1835SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1,
19225f2e1835SMichael Tuexen 			    "Load addresses from INIT causes an abort %d\n",
19235f2e1835SMichael Tuexen 			    retval);
19245f2e1835SMichael Tuexen 			sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
19255f2e1835SMichael Tuexen 			    src, dst, sh, op_err,
19265f2e1835SMichael Tuexen 			    mflowtype, mflowid,
19275f2e1835SMichael Tuexen 			    vrf_id, net->port);
1928f8829a4aSRandall Stewart 			return (NULL);
1929f8829a4aSRandall Stewart 		}
1930f8829a4aSRandall Stewart 		/* respond with a COOKIE-ACK */
1931f8829a4aSRandall Stewart 		sctp_send_cookie_ack(stcb);
193244b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
193344b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 15;
1934a89481d3SMichael Tuexen 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE) &&
1935a89481d3SMichael Tuexen 		    (asoc->sctp_autoclose_ticks > 0)) {
1936a89481d3SMichael Tuexen 			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
1937a89481d3SMichael Tuexen 		}
1938f8829a4aSRandall Stewart 		return (stcb);
1939f8829a4aSRandall Stewart 	}
194044b7479bSRandall Stewart 	if (how_indx < sizeof(asoc->cookie_how))
194144b7479bSRandall Stewart 		asoc->cookie_how[how_indx] = 16;
1942f8829a4aSRandall Stewart 	/* all other cases... */
194312dda000SMichael Tuexen 	SCTP_TCB_UNLOCK(stcb);
1944f8829a4aSRandall Stewart 	return (NULL);
1945f8829a4aSRandall Stewart }
1946f8829a4aSRandall Stewart 
1947f8829a4aSRandall Stewart /*
1948f8829a4aSRandall Stewart  * handle a state cookie for a new association m: input packet mbuf chain--
1949f8829a4aSRandall Stewart  * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
1950f8829a4aSRandall Stewart  * and the cookie signature does not exist offset: offset into mbuf to the
1951f8829a4aSRandall Stewart  * cookie-echo chunk length: length of the cookie chunk to: where the init
1952f8829a4aSRandall Stewart  * was from returns a new TCB
1953f8829a4aSRandall Stewart  */
1954f30ac432SMichael Tuexen static struct sctp_tcb *
1955f8829a4aSRandall Stewart sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1956b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
1957f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1958f8829a4aSRandall Stewart     struct sctp_inpcb *inp, struct sctp_nets **netp,
1959f8829a4aSRandall Stewart     struct sockaddr *init_src, int *notification,
196017205eccSRandall Stewart     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1961457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
1962c54a18d2SRandall Stewart     uint32_t vrf_id, uint16_t port)
1963f8829a4aSRandall Stewart {
1964f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
1965f8829a4aSRandall Stewart 	struct sctp_init_chunk *init_cp, init_buf;
1966f8829a4aSRandall Stewart 	struct sctp_init_ack_chunk *initack_cp, initack_buf;
196724aaac8dSMichael Tuexen 	union sctp_sockstore store;
1968f8829a4aSRandall Stewart 	struct sctp_association *asoc;
1969f8829a4aSRandall Stewart 	int init_offset, initack_offset, initack_limit;
1970f8829a4aSRandall Stewart 	int error = 0;
19718262311cSMichael Tuexen 	uint8_t auth_chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
1972ceaad40aSRandall Stewart 
1973f8829a4aSRandall Stewart 	/*
1974f8829a4aSRandall Stewart 	 * find and validate the INIT chunk in the cookie (peer's info) the
1975f8829a4aSRandall Stewart 	 * INIT should start after the cookie-echo header struct (chunk
1976f8829a4aSRandall Stewart 	 * header, state cookie header struct)
1977f8829a4aSRandall Stewart 	 */
1978f8829a4aSRandall Stewart 	init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
1979f8829a4aSRandall Stewart 	init_cp = (struct sctp_init_chunk *)
1980f8829a4aSRandall Stewart 	    sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1981f8829a4aSRandall Stewart 	    (uint8_t *)&init_buf);
1982f8829a4aSRandall Stewart 	if (init_cp == NULL) {
1983f8829a4aSRandall Stewart 		/* could not pull a INIT chunk in cookie */
1984ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1,
1985ad81507eSRandall Stewart 		    "process_cookie_new: could not pull INIT chunk hdr\n");
1986f8829a4aSRandall Stewart 		return (NULL);
1987f8829a4aSRandall Stewart 	}
1988f8829a4aSRandall Stewart 	if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1989ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
1990f8829a4aSRandall Stewart 		return (NULL);
1991f8829a4aSRandall Stewart 	}
199260990c0cSMichael Tuexen 	initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1993f8829a4aSRandall Stewart 	/*
1994f8829a4aSRandall Stewart 	 * find and validate the INIT-ACK chunk in the cookie (my info) the
1995f8829a4aSRandall Stewart 	 * INIT-ACK follows the INIT chunk
1996f8829a4aSRandall Stewart 	 */
1997f8829a4aSRandall Stewart 	initack_cp = (struct sctp_init_ack_chunk *)
1998f8829a4aSRandall Stewart 	    sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1999f8829a4aSRandall Stewart 	    (uint8_t *)&initack_buf);
2000f8829a4aSRandall Stewart 	if (initack_cp == NULL) {
2001f8829a4aSRandall Stewart 		/* could not pull INIT-ACK chunk in cookie */
2002ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2003f8829a4aSRandall Stewart 		return (NULL);
2004f8829a4aSRandall Stewart 	}
2005f8829a4aSRandall Stewart 	if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2006f8829a4aSRandall Stewart 		return (NULL);
2007f8829a4aSRandall Stewart 	}
2008f8829a4aSRandall Stewart 	/*
2009f8829a4aSRandall Stewart 	 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2010f8829a4aSRandall Stewart 	 * "initack_limit" value.  This is because the chk_length field
2011f8829a4aSRandall Stewart 	 * includes the length of the cookie, but the cookie is omitted when
2012f8829a4aSRandall Stewart 	 * the INIT and INIT_ACK are tacked onto the cookie...
2013f8829a4aSRandall Stewart 	 */
2014f8829a4aSRandall Stewart 	initack_limit = offset + cookie_len;
2015f8829a4aSRandall Stewart 
2016f8829a4aSRandall Stewart 	/*
2017f8829a4aSRandall Stewart 	 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2018e7e65008SMichael Tuexen 	 * and populate
2019f8829a4aSRandall Stewart 	 */
202052be287eSRandall Stewart 
202152be287eSRandall Stewart 	/*
202252be287eSRandall Stewart 	 * Here we do a trick, we set in NULL for the proc/thread argument.
202352be287eSRandall Stewart 	 * We do this since in effect we only use the p argument when the
202452be287eSRandall Stewart 	 * socket is unbound and we must do an implicit bind. Since we are
202552be287eSRandall Stewart 	 * getting a cookie, we cannot be unbound.
202652be287eSRandall Stewart 	 */
2027b5c16493SMichael Tuexen 	stcb = sctp_aloc_assoc(inp, init_src, &error,
2028c7f048abSMichael Tuexen 	    ntohl(initack_cp->init.initiate_tag),
2029c7f048abSMichael Tuexen 	    ntohl(initack_cp->init.initial_tsn), vrf_id,
2030c979034bSMichael Tuexen 	    ntohs(initack_cp->init.num_outbound_streams),
2031ec70917fSMichael Tuexen 	    port,
20328a956abeSMichael Tuexen 	    (struct thread *)NULL,
20338a956abeSMichael Tuexen 	    SCTP_DONT_INITIALIZE_AUTH_PARAMS);
2034f8829a4aSRandall Stewart 	if (stcb == NULL) {
2035f8829a4aSRandall Stewart 		struct mbuf *op_err;
2036f8829a4aSRandall Stewart 
2037f8829a4aSRandall Stewart 		/* memory problem? */
2038ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1,
2039ad81507eSRandall Stewart 		    "process_cookie_new: no room for another TCB!\n");
2040ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2041f8829a4aSRandall Stewart 		sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2042b1754ad1SMichael Tuexen 		    src, dst, sh, op_err,
2043457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2044f30ac432SMichael Tuexen 		    vrf_id, port);
2045f8829a4aSRandall Stewart 		return (NULL);
2046f8829a4aSRandall Stewart 	}
2047f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2048f8829a4aSRandall Stewart 	/* get scope variables out of cookie */
2049a1cb341bSMichael Tuexen 	asoc->scope.ipv4_local_scope = cookie->ipv4_scope;
2050a1cb341bSMichael Tuexen 	asoc->scope.site_scope = cookie->site_scope;
2051a1cb341bSMichael Tuexen 	asoc->scope.local_scope = cookie->local_scope;
2052a1cb341bSMichael Tuexen 	asoc->scope.loopback_scope = cookie->loopback_scope;
2053f8829a4aSRandall Stewart 
2054a1cb341bSMichael Tuexen 	if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2055a1cb341bSMichael Tuexen 	    (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2056f8829a4aSRandall Stewart 		struct mbuf *op_err;
2057f8829a4aSRandall Stewart 
2058f8829a4aSRandall Stewart 		/*
2059f8829a4aSRandall Stewart 		 * Houston we have a problem. The EP changed while the
2060f8829a4aSRandall Stewart 		 * cookie was in flight. Only recourse is to abort the
2061f8829a4aSRandall Stewart 		 * association.
2062f8829a4aSRandall Stewart 		 */
2063ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2064f8829a4aSRandall Stewart 		sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2065b1754ad1SMichael Tuexen 		    src, dst, sh, op_err,
2066457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2067f30ac432SMichael Tuexen 		    vrf_id, port);
2068c4739e2fSRandall Stewart 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2069b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2070f8829a4aSRandall Stewart 		return (NULL);
2071f8829a4aSRandall Stewart 	}
20726026b45aSMichael Tuexen 	asoc->zero_checksum = cookie->zero_checksum;
2073f8829a4aSRandall Stewart 	/* process the INIT-ACK info (my info) */
2074f8829a4aSRandall Stewart 	asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2075f8829a4aSRandall Stewart 
2076f8829a4aSRandall Stewart 	/* process the INIT info (peer's info) */
20775f2e1835SMichael Tuexen 	if (sctp_process_init(init_cp, stcb) < 0) {
2078b7d130beSMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2079b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2080f8829a4aSRandall Stewart 		return (NULL);
2081f8829a4aSRandall Stewart 	}
2082f8829a4aSRandall Stewart 	/* load all addresses */
208345421646SMichael Tuexen 	if (sctp_load_addresses_from_init(stcb, m,
20845f2e1835SMichael Tuexen 	    init_offset + sizeof(struct sctp_init_chunk),
208545421646SMichael Tuexen 	    initack_offset, src, dst, init_src, port) < 0) {
2086b7d130beSMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2087b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2088f8829a4aSRandall Stewart 		return (NULL);
2089f8829a4aSRandall Stewart 	}
2090f8829a4aSRandall Stewart 	/*
2091f8829a4aSRandall Stewart 	 * verify any preceding AUTH chunk that was skipped
2092f8829a4aSRandall Stewart 	 */
2093f8829a4aSRandall Stewart 	/* pull the local authentication parameters from the cookie/init-ack */
2094f8829a4aSRandall Stewart 	sctp_auth_get_cookie_params(stcb, m,
2095f8829a4aSRandall Stewart 	    initack_offset + sizeof(struct sctp_init_ack_chunk),
2096f8829a4aSRandall Stewart 	    initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2097f8829a4aSRandall Stewart 	if (auth_skipped) {
2098f8829a4aSRandall Stewart 		struct sctp_auth_chunk *auth;
2099f8829a4aSRandall Stewart 
21008262311cSMichael Tuexen 		if (auth_len <= SCTP_CHUNK_BUFFER_SIZE) {
210197feba89SMichael Tuexen 			auth = (struct sctp_auth_chunk *)sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
210297feba89SMichael Tuexen 		} else {
210397feba89SMichael Tuexen 			auth = NULL;
210497feba89SMichael Tuexen 		}
2105ad81507eSRandall Stewart 		if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2106f8829a4aSRandall Stewart 			/* auth HMAC failed, dump the assoc and packet */
2107ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_AUTH1,
2108ad81507eSRandall Stewart 			    "COOKIE-ECHO: AUTH failed\n");
2109b7d130beSMichael Tuexen 			(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2110b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_21);
2111f8829a4aSRandall Stewart 			return (NULL);
2112f8829a4aSRandall Stewart 		} else {
2113f8829a4aSRandall Stewart 			/* remaining chunks checked... good to go */
2114f8829a4aSRandall Stewart 			stcb->asoc.authenticated = 1;
2115f8829a4aSRandall Stewart 		}
2116f8829a4aSRandall Stewart 	}
21170053ed28SMichael Tuexen 
2118f8829a4aSRandall Stewart 	/*
2119f8829a4aSRandall Stewart 	 * if we're doing ASCONFs, check to see if we have any new local
2120f8829a4aSRandall Stewart 	 * addresses that need to get added to the peer (eg. addresses
2121f8829a4aSRandall Stewart 	 * changed while cookie echo in flight).  This needs to be done
2122f8829a4aSRandall Stewart 	 * after we go to the OPEN state to do the correct asconf
2123f8829a4aSRandall Stewart 	 * processing. else, make sure we have the correct addresses in our
2124f8829a4aSRandall Stewart 	 * lists
2125f8829a4aSRandall Stewart 	 */
2126f8829a4aSRandall Stewart 
2127f8829a4aSRandall Stewart 	/* warning, we re-use sin, sin6, sa_store here! */
2128f8829a4aSRandall Stewart 	/* pull in local_address (our "from" address) */
2129e6194c2eSMichael Tuexen 	switch (cookie->laddr_type) {
2130e6194c2eSMichael Tuexen #ifdef INET
2131e6194c2eSMichael Tuexen 	case SCTP_IPV4_ADDRESS:
2132f8829a4aSRandall Stewart 		/* source addr is IPv4 */
213324aaac8dSMichael Tuexen 		memset(&store.sin, 0, sizeof(struct sockaddr_in));
213424aaac8dSMichael Tuexen 		store.sin.sin_family = AF_INET;
213524aaac8dSMichael Tuexen 		store.sin.sin_len = sizeof(struct sockaddr_in);
213624aaac8dSMichael Tuexen 		store.sin.sin_addr.s_addr = cookie->laddress[0];
2137e6194c2eSMichael Tuexen 		break;
2138e6194c2eSMichael Tuexen #endif
2139e6194c2eSMichael Tuexen #ifdef INET6
2140e6194c2eSMichael Tuexen 	case SCTP_IPV6_ADDRESS:
2141f8829a4aSRandall Stewart 		/* source addr is IPv6 */
214224aaac8dSMichael Tuexen 		memset(&store.sin6, 0, sizeof(struct sockaddr_in6));
214324aaac8dSMichael Tuexen 		store.sin6.sin6_family = AF_INET6;
214424aaac8dSMichael Tuexen 		store.sin6.sin6_len = sizeof(struct sockaddr_in6);
214524aaac8dSMichael Tuexen 		store.sin6.sin6_scope_id = cookie->scope_id;
214623602b60SMichael Tuexen 		memcpy(&store.sin6.sin6_addr, cookie->laddress, sizeof(struct in6_addr));
2147e6194c2eSMichael Tuexen 		break;
2148e6194c2eSMichael Tuexen #endif
2149e6194c2eSMichael Tuexen 	default:
2150b7d130beSMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2151b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
2152f8829a4aSRandall Stewart 		return (NULL);
2153f8829a4aSRandall Stewart 	}
2154f8829a4aSRandall Stewart 
21551698cbd9SMichael Tuexen 	/* update current state */
21561698cbd9SMichael Tuexen 	SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2157839d21d6SMichael Tuexen 	SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
21581698cbd9SMichael Tuexen 	if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
21591698cbd9SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
21606fb7b4fbSMichael Tuexen 		    stcb->sctp_ep, stcb, NULL);
21611698cbd9SMichael Tuexen 	}
21621698cbd9SMichael Tuexen 	sctp_stop_all_cookie_timers(stcb);
21631698cbd9SMichael Tuexen 	SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
21641698cbd9SMichael Tuexen 	SCTP_STAT_INCR_GAUGE32(sctps_currestab);
21651698cbd9SMichael Tuexen 
2166f8829a4aSRandall Stewart 	/* set up to notify upper layer */
2167f8829a4aSRandall Stewart 	*notification = SCTP_NOTIFY_ASSOC_UP;
2168f8829a4aSRandall Stewart 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2169f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
21705d08768aSMichael Tuexen 	    (!SCTP_IS_LISTENING(inp))) {
2171f8829a4aSRandall Stewart 		/*
2172f8829a4aSRandall Stewart 		 * This is an endpoint that called connect() how it got a
2173f8829a4aSRandall Stewart 		 * cookie that is NEW is a bit of a mystery. It must be that
2174f8829a4aSRandall Stewart 		 * the INIT was sent, but before it got there.. a complete
2175f8829a4aSRandall Stewart 		 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2176f8829a4aSRandall Stewart 		 * should have went to the other code.. not here.. oh well..
2177f8829a4aSRandall Stewart 		 * a bit of protection is worth having..
21782d5c48ecSMark Johnston 		 *
21792d5c48ecSMark Johnston 		 * XXXMJ unlocked
2180f8829a4aSRandall Stewart 		 */
2181a5c2009dSMichael Tuexen 		sctp_pcb_add_flags(stcb->sctp_ep, SCTP_PCB_FLAGS_CONNECTED);
2182ceaad40aSRandall Stewart 		soisconnected(stcb->sctp_socket);
2183f8829a4aSRandall Stewart 	} else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
21845d08768aSMichael Tuexen 	    (SCTP_IS_LISTENING(inp))) {
2185f8829a4aSRandall Stewart 		/*
2186f8829a4aSRandall Stewart 		 * We don't want to do anything with this one. Since it is
2187f8829a4aSRandall Stewart 		 * the listening guy. The timer will get started for
2188f8829a4aSRandall Stewart 		 * accepted connections in the caller.
2189f8829a4aSRandall Stewart 		 */
2190f8829a4aSRandall Stewart 		;
2191f8829a4aSRandall Stewart 	}
2192f8829a4aSRandall Stewart 	if (stcb->asoc.sctp_autoclose_ticks &&
2193f8829a4aSRandall Stewart 	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2194f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2195f8829a4aSRandall Stewart 	}
2196b54d3a6cSRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2197b15f5411SMichael Tuexen 	*netp = sctp_findnet(stcb, init_src);
2198b15f5411SMichael Tuexen 	if (*netp != NULL) {
2199b15f5411SMichael Tuexen 		/*
2200b15f5411SMichael Tuexen 		 * Since we did not send a HB, make sure we don't double
2201b15f5411SMichael Tuexen 		 * things.
2202b15f5411SMichael Tuexen 		 */
2203b15f5411SMichael Tuexen 		(*netp)->hb_responded = 1;
22049a972525SRandall Stewart 	}
22053232788eSRandall Stewart 	/* respond with a COOKIE-ACK */
2206f8829a4aSRandall Stewart 	sctp_send_cookie_ack(stcb);
22073232788eSRandall Stewart 
22083232788eSRandall Stewart 	/*
22093232788eSRandall Stewart 	 * check the address lists for any ASCONFs that need to be sent
22103232788eSRandall Stewart 	 * AFTER the cookie-ack is sent
22113232788eSRandall Stewart 	 */
22123232788eSRandall Stewart 	sctp_check_address_list(stcb, m,
22133232788eSRandall Stewart 	    initack_offset + sizeof(struct sctp_init_ack_chunk),
22143232788eSRandall Stewart 	    initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
221524aaac8dSMichael Tuexen 	    &store.sa, cookie->local_scope, cookie->site_scope,
22163232788eSRandall Stewart 	    cookie->ipv4_scope, cookie->loopback_scope);
22173232788eSRandall Stewart 
2218f8829a4aSRandall Stewart 	return (stcb);
2219f8829a4aSRandall Stewart }
2220f8829a4aSRandall Stewart 
2221830d754dSRandall Stewart /*
2222830d754dSRandall Stewart  * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2223830d754dSRandall Stewart  * we NEED to make sure we are not already using the vtag. If so we
2224830d754dSRandall Stewart  * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2225830d754dSRandall Stewart 	head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2226830d754dSRandall Stewart 							    SCTP_BASE_INFO(hashasocmark))];
2227830d754dSRandall Stewart 	LIST_FOREACH(stcb, head, sctp_asocs) {
2228830d754dSRandall Stewart 	        if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2229830d754dSRandall Stewart 		       -- SEND ABORT - TRY AGAIN --
2230830d754dSRandall Stewart 		}
2231830d754dSRandall Stewart 	}
2232830d754dSRandall Stewart */
2233f8829a4aSRandall Stewart 
2234f8829a4aSRandall Stewart /*
2235f8829a4aSRandall Stewart  * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2236f8829a4aSRandall Stewart  * existing (non-NULL) TCB
2237f8829a4aSRandall Stewart  */
2238f8829a4aSRandall Stewart static struct mbuf *
2239f8829a4aSRandall Stewart sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2240b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
2241f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2242f8829a4aSRandall Stewart     struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
224317205eccSRandall Stewart     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2244f30ac432SMichael Tuexen     struct sctp_tcb **locked_tcb,
2245457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
2246f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
2247f8829a4aSRandall Stewart {
2248f8829a4aSRandall Stewart 	struct sctp_state_cookie *cookie;
2249f8829a4aSRandall Stewart 	struct sctp_tcb *l_stcb = *stcb;
2250f8829a4aSRandall Stewart 	struct sctp_inpcb *l_inp;
2251f8829a4aSRandall Stewart 	struct sockaddr *to;
2252f8829a4aSRandall Stewart 	struct sctp_pcb *ep;
2253f8829a4aSRandall Stewart 	struct mbuf *m_sig;
2254f8829a4aSRandall Stewart 	uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2255f8829a4aSRandall Stewart 	uint8_t *sig;
2256f8829a4aSRandall Stewart 	uint8_t cookie_ok = 0;
2257329204ffSMichael Tuexen 	unsigned int sig_offset, cookie_offset;
2258f8829a4aSRandall Stewart 	unsigned int cookie_len;
2259f8829a4aSRandall Stewart 	struct timeval now;
2260a92d5016SMichael Tuexen 	struct timeval time_entered, time_expires;
2261f8829a4aSRandall Stewart 	int notification = 0;
2262f8829a4aSRandall Stewart 	struct sctp_nets *netl;
2263f8829a4aSRandall Stewart 	int had_a_existing_tcb = 0;
22642fad0e55SMichael Tuexen 	int send_int_conf = 0;
2265e6194c2eSMichael Tuexen #ifdef INET
2266e6194c2eSMichael Tuexen 	struct sockaddr_in sin;
2267e6194c2eSMichael Tuexen #endif
2268e6194c2eSMichael Tuexen #ifdef INET6
2269e6194c2eSMichael Tuexen 	struct sockaddr_in6 sin6;
2270e6194c2eSMichael Tuexen #endif
2271e6194c2eSMichael Tuexen 
2272ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
2273ad81507eSRandall Stewart 	    "sctp_handle_cookie: handling COOKIE-ECHO\n");
2274f8829a4aSRandall Stewart 
2275f8829a4aSRandall Stewart 	if (inp_p == NULL) {
2276f8829a4aSRandall Stewart 		return (NULL);
2277f8829a4aSRandall Stewart 	}
2278f8829a4aSRandall Stewart 	cookie = &cp->cookie;
2279f8829a4aSRandall Stewart 	cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2280f8829a4aSRandall Stewart 	cookie_len = ntohs(cp->ch.chunk_length);
2281f8829a4aSRandall Stewart 
2282d44b45dfSMichael Tuexen 	if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2283d44b45dfSMichael Tuexen 	    sizeof(struct sctp_init_chunk) +
2284d44b45dfSMichael Tuexen 	    sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2285d44b45dfSMichael Tuexen 		/* cookie too small */
2286d44b45dfSMichael Tuexen 		return (NULL);
2287d44b45dfSMichael Tuexen 	}
22883db4ea95SMichael Tuexen 	if ((cookie->peerport != sh->src_port) ||
22893db4ea95SMichael Tuexen 	    (cookie->myport != sh->dest_port) ||
2290f8829a4aSRandall Stewart 	    (cookie->my_vtag != sh->v_tag)) {
2291f8829a4aSRandall Stewart 		/*
2292f8829a4aSRandall Stewart 		 * invalid ports or bad tag.  Note that we always leave the
2293f8829a4aSRandall Stewart 		 * v_tag in the header in network order and when we stored
2294f8829a4aSRandall Stewart 		 * it in the my_vtag slot we also left it in network order.
229517205eccSRandall Stewart 		 * This maintains the match even though it may be in the
2296f8829a4aSRandall Stewart 		 * opposite byte order of the machine :->
2297f8829a4aSRandall Stewart 		 */
2298f8829a4aSRandall Stewart 		return (NULL);
2299f8829a4aSRandall Stewart 	}
2300f8829a4aSRandall Stewart 	/*
2301f8829a4aSRandall Stewart 	 * split off the signature into its own mbuf (since it should not be
2302f8829a4aSRandall Stewart 	 * calculated in the sctp_hmac_m() call).
2303f8829a4aSRandall Stewart 	 */
2304f8829a4aSRandall Stewart 	sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2305eb1b1807SGleb Smirnoff 	m_sig = m_split(m, sig_offset, M_NOWAIT);
2306f8829a4aSRandall Stewart 	if (m_sig == NULL) {
2307f8829a4aSRandall Stewart 		/* out of memory or ?? */
2308f8829a4aSRandall Stewart 		return (NULL);
2309f8829a4aSRandall Stewart 	}
2310eadccaccSRandall Stewart #ifdef SCTP_MBUF_LOGGING
2311b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
23124be807c4SMichael Tuexen 		sctp_log_mbc(m_sig, SCTP_MBUF_SPLIT);
2313eadccaccSRandall Stewart 	}
2314eadccaccSRandall Stewart #endif
2315eadccaccSRandall Stewart 
2316f8829a4aSRandall Stewart 	/*
2317f8829a4aSRandall Stewart 	 * compute the signature/digest for the cookie
2318f8829a4aSRandall Stewart 	 */
2319e0127ea4SMichael Tuexen 	if (l_stcb != NULL) {
2320e0127ea4SMichael Tuexen 		atomic_add_int(&l_stcb->asoc.refcnt, 1);
2321f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(l_stcb);
2322f8829a4aSRandall Stewart 	}
2323e0127ea4SMichael Tuexen 	l_inp = *inp_p;
2324f8829a4aSRandall Stewart 	SCTP_INP_RLOCK(l_inp);
2325e0127ea4SMichael Tuexen 	if (l_stcb != NULL) {
2326f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(l_stcb);
2327e0127ea4SMichael Tuexen 		atomic_subtract_int(&l_stcb->asoc.refcnt, 1);
2328f8829a4aSRandall Stewart 	}
2329e0127ea4SMichael Tuexen 	if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
2330e0127ea4SMichael Tuexen 		SCTP_INP_RUNLOCK(l_inp);
2331e0127ea4SMichael Tuexen 		sctp_m_freem(m_sig);
2332e0127ea4SMichael Tuexen 		return (NULL);
2333e0127ea4SMichael Tuexen 	}
2334e0127ea4SMichael Tuexen 	ep = &(*inp_p)->sctp_ep;
2335f8829a4aSRandall Stewart 	/* which cookie is it? */
2336f8829a4aSRandall Stewart 	if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2337f8829a4aSRandall Stewart 	    (ep->current_secret_number != ep->last_secret_number)) {
2338f8829a4aSRandall Stewart 		/* it's the old cookie */
23396e55db54SRandall Stewart 		(void)sctp_hmac_m(SCTP_HMAC,
2340f8829a4aSRandall Stewart 		    (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2341d00aff5dSRandall Stewart 		    SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2342f8829a4aSRandall Stewart 	} else {
2343f8829a4aSRandall Stewart 		/* it's the current cookie */
23446e55db54SRandall Stewart 		(void)sctp_hmac_m(SCTP_HMAC,
2345f8829a4aSRandall Stewart 		    (uint8_t *)ep->secret_key[(int)ep->current_secret_number],
2346d00aff5dSRandall Stewart 		    SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2347f8829a4aSRandall Stewart 	}
2348f8829a4aSRandall Stewart 	/* get the signature */
2349f8829a4aSRandall Stewart 	SCTP_INP_RUNLOCK(l_inp);
2350f8829a4aSRandall Stewart 	sig = (uint8_t *)sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *)&tmp_sig);
2351f8829a4aSRandall Stewart 	if (sig == NULL) {
2352f8829a4aSRandall Stewart 		/* couldn't find signature */
235303b0b021SRandall Stewart 		sctp_m_freem(m_sig);
2354f8829a4aSRandall Stewart 		return (NULL);
2355f8829a4aSRandall Stewart 	}
2356f8829a4aSRandall Stewart 	/* compare the received digest with the computed digest */
235715a087e5SMichael Tuexen 	if (timingsafe_bcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2358f8829a4aSRandall Stewart 		/* try the old cookie? */
2359f8829a4aSRandall Stewart 		if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2360f8829a4aSRandall Stewart 		    (ep->current_secret_number != ep->last_secret_number)) {
2361f8829a4aSRandall Stewart 			/* compute digest with old */
23626e55db54SRandall Stewart 			(void)sctp_hmac_m(SCTP_HMAC,
2363f8829a4aSRandall Stewart 			    (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2364d00aff5dSRandall Stewart 			    SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2365f8829a4aSRandall Stewart 			/* compare */
236615a087e5SMichael Tuexen 			if (timingsafe_bcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2367f8829a4aSRandall Stewart 				cookie_ok = 1;
2368f8829a4aSRandall Stewart 		}
2369f8829a4aSRandall Stewart 	} else {
2370f8829a4aSRandall Stewart 		cookie_ok = 1;
2371f8829a4aSRandall Stewart 	}
2372f8829a4aSRandall Stewart 
2373f8829a4aSRandall Stewart 	/*
2374f8829a4aSRandall Stewart 	 * Now before we continue we must reconstruct our mbuf so that
2375f8829a4aSRandall Stewart 	 * normal processing of any other chunks will work.
2376f8829a4aSRandall Stewart 	 */
2377f8829a4aSRandall Stewart 	{
2378f8829a4aSRandall Stewart 		struct mbuf *m_at;
2379f8829a4aSRandall Stewart 
2380f8829a4aSRandall Stewart 		m_at = m;
2381139bc87fSRandall Stewart 		while (SCTP_BUF_NEXT(m_at) != NULL) {
2382139bc87fSRandall Stewart 			m_at = SCTP_BUF_NEXT(m_at);
2383f8829a4aSRandall Stewart 		}
2384139bc87fSRandall Stewart 		SCTP_BUF_NEXT(m_at) = m_sig;
2385f8829a4aSRandall Stewart 	}
2386f8829a4aSRandall Stewart 
2387f8829a4aSRandall Stewart 	if (cookie_ok == 0) {
2388ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2389ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
2390ad81507eSRandall Stewart 		    "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2391f8829a4aSRandall Stewart 		    (uint32_t)offset, cookie_offset, sig_offset);
2392f8829a4aSRandall Stewart 		return (NULL);
2393f8829a4aSRandall Stewart 	}
23940053ed28SMichael Tuexen 
2395a92d5016SMichael Tuexen 	if (sctp_ticks_to_msecs(cookie->cookie_life) > SCTP_MAX_COOKIE_LIFE) {
2396a92d5016SMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: Invalid cookie lifetime\n");
2397a92d5016SMichael Tuexen 		return (NULL);
2398a92d5016SMichael Tuexen 	}
2399a92d5016SMichael Tuexen 	time_entered.tv_sec = cookie->time_entered.tv_sec;
2400a92d5016SMichael Tuexen 	time_entered.tv_usec = cookie->time_entered.tv_usec;
2401a92d5016SMichael Tuexen 	if ((time_entered.tv_sec < 0) ||
2402a92d5016SMichael Tuexen 	    (time_entered.tv_usec < 0) ||
2403a92d5016SMichael Tuexen 	    (time_entered.tv_usec >= 1000000)) {
2404a92d5016SMichael Tuexen 		/* Invalid time stamp. Cookie must have been modified. */
2405a92d5016SMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: Invalid time stamp\n");
2406a92d5016SMichael Tuexen 		return (NULL);
2407a92d5016SMichael Tuexen 	}
24086e55db54SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&now);
2409a92d5016SMichael Tuexen 	if (timevalcmp(&now, &time_entered, <)) {
2410a92d5016SMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie generated in the future!\n");
2411a92d5016SMichael Tuexen 		return (NULL);
2412a92d5016SMichael Tuexen 	}
2413a92d5016SMichael Tuexen 	/*
2414a92d5016SMichael Tuexen 	 * Check the cookie timestamps to be sure it's not stale.
2415a92d5016SMichael Tuexen 	 * cookie_life is in ticks, so we convert to seconds.
2416a92d5016SMichael Tuexen 	 */
2417a92d5016SMichael Tuexen 	time_expires.tv_sec = time_entered.tv_sec + sctp_ticks_to_secs(cookie->cookie_life);
2418a92d5016SMichael Tuexen 	time_expires.tv_usec = time_entered.tv_usec;
2419f8829a4aSRandall Stewart 	if (timevalcmp(&now, &time_expires, >)) {
2420f8829a4aSRandall Stewart 		/* cookie is stale! */
2421f8829a4aSRandall Stewart 		struct mbuf *op_err;
242286eda749SMichael Tuexen 		struct sctp_error_stale_cookie *cause;
2423564a95f4SMichael Tuexen 		struct timeval diff;
2424564a95f4SMichael Tuexen 		uint32_t staleness;
2425f8829a4aSRandall Stewart 
242686eda749SMichael Tuexen 		op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_stale_cookie),
2427eb1b1807SGleb Smirnoff 		    0, M_NOWAIT, 1, MT_DATA);
2428f8829a4aSRandall Stewart 		if (op_err == NULL) {
2429f8829a4aSRandall Stewart 			/* FOOBAR */
2430f8829a4aSRandall Stewart 			return (NULL);
2431f8829a4aSRandall Stewart 		}
2432f8829a4aSRandall Stewart 		/* Set the len */
243386eda749SMichael Tuexen 		SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_stale_cookie);
243486eda749SMichael Tuexen 		cause = mtod(op_err, struct sctp_error_stale_cookie *);
243586eda749SMichael Tuexen 		cause->cause.code = htons(SCTP_CAUSE_STALE_COOKIE);
2436a92d5016SMichael Tuexen 		cause->cause.length = htons(sizeof(struct sctp_error_stale_cookie));
2437564a95f4SMichael Tuexen 		diff = now;
2438564a95f4SMichael Tuexen 		timevalsub(&diff, &time_expires);
2439c3115febSMichael Tuexen 		if ((uint32_t)diff.tv_sec > UINT32_MAX / 1000000) {
2440564a95f4SMichael Tuexen 			staleness = UINT32_MAX;
2441564a95f4SMichael Tuexen 		} else {
2442aab1d593SMichael Tuexen 			staleness = (uint32_t)diff.tv_sec * 1000000;
2443564a95f4SMichael Tuexen 		}
24443ec509bcSMichael Tuexen 		if (UINT32_MAX - staleness >= (uint32_t)diff.tv_usec) {
2445aab1d593SMichael Tuexen 			staleness += (uint32_t)diff.tv_usec;
2446564a95f4SMichael Tuexen 		} else {
2447564a95f4SMichael Tuexen 			staleness = UINT32_MAX;
2448564a95f4SMichael Tuexen 		}
2449564a95f4SMichael Tuexen 		cause->stale_time = htonl(staleness);
2450b1754ad1SMichael Tuexen 		sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
2451d089f9b9SMichael Tuexen 		    mflowtype, mflowid, l_inp->fibnum,
2452c54a18d2SRandall Stewart 		    vrf_id, port);
2453f8829a4aSRandall Stewart 		return (NULL);
2454f8829a4aSRandall Stewart 	}
2455f8829a4aSRandall Stewart 	/*
2456f8829a4aSRandall Stewart 	 * Now we must see with the lookup address if we have an existing
2457f8829a4aSRandall Stewart 	 * asoc. This will only happen if we were in the COOKIE-WAIT state
2458f8829a4aSRandall Stewart 	 * and a INIT collided with us and somewhere the peer sent the
2459f8829a4aSRandall Stewart 	 * cookie on another address besides the single address our assoc
2460f8829a4aSRandall Stewart 	 * had for him. In this case we will have one of the tie-tags set at
2461f8829a4aSRandall Stewart 	 * least AND the address field in the cookie can be used to look it
2462f8829a4aSRandall Stewart 	 * up.
2463f8829a4aSRandall Stewart 	 */
2464f8829a4aSRandall Stewart 	to = NULL;
2465e6194c2eSMichael Tuexen 	switch (cookie->addr_type) {
2466e6194c2eSMichael Tuexen #ifdef INET6
2467e6194c2eSMichael Tuexen 	case SCTP_IPV6_ADDRESS:
2468f8829a4aSRandall Stewart 		memset(&sin6, 0, sizeof(sin6));
2469f8829a4aSRandall Stewart 		sin6.sin6_family = AF_INET6;
2470f8829a4aSRandall Stewart 		sin6.sin6_len = sizeof(sin6);
2471f8829a4aSRandall Stewart 		sin6.sin6_port = sh->src_port;
2472f8829a4aSRandall Stewart 		sin6.sin6_scope_id = cookie->scope_id;
2473f8829a4aSRandall Stewart 		memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2474f8829a4aSRandall Stewart 		    sizeof(sin6.sin6_addr.s6_addr));
2475f8829a4aSRandall Stewart 		to = (struct sockaddr *)&sin6;
2476e6194c2eSMichael Tuexen 		break;
2477e6194c2eSMichael Tuexen #endif
2478e6194c2eSMichael Tuexen #ifdef INET
2479e6194c2eSMichael Tuexen 	case SCTP_IPV4_ADDRESS:
2480f8829a4aSRandall Stewart 		memset(&sin, 0, sizeof(sin));
2481f8829a4aSRandall Stewart 		sin.sin_family = AF_INET;
2482f8829a4aSRandall Stewart 		sin.sin_len = sizeof(sin);
2483f8829a4aSRandall Stewart 		sin.sin_port = sh->src_port;
2484f8829a4aSRandall Stewart 		sin.sin_addr.s_addr = cookie->address[0];
2485f8829a4aSRandall Stewart 		to = (struct sockaddr *)&sin;
2486e6194c2eSMichael Tuexen 		break;
2487e6194c2eSMichael Tuexen #endif
2488e6194c2eSMichael Tuexen 	default:
2489ad81507eSRandall Stewart 		/* This should not happen */
2490ad81507eSRandall Stewart 		return (NULL);
2491f8829a4aSRandall Stewart 	}
2492f0dc2113SMichael Tuexen 	if (*stcb == NULL) {
2493f8829a4aSRandall Stewart 		/* Yep, lets check */
2494b1754ad1SMichael Tuexen 		*stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
2495f8829a4aSRandall Stewart 		if (*stcb == NULL) {
2496f8829a4aSRandall Stewart 			/*
2497f8829a4aSRandall Stewart 			 * We should have only got back the same inp. If we
2498f8829a4aSRandall Stewart 			 * got back a different ep we have a problem. The
2499f8829a4aSRandall Stewart 			 * original findep got back l_inp and now
2500f8829a4aSRandall Stewart 			 */
2501f8829a4aSRandall Stewart 			if (l_inp != *inp_p) {
2502ad81507eSRandall Stewart 				SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2503f8829a4aSRandall Stewart 			}
2504a5d547adSRandall Stewart 		} else {
2505a5d547adSRandall Stewart 			if (*locked_tcb == NULL) {
2506a5d547adSRandall Stewart 				/*
2507a5d547adSRandall Stewart 				 * In this case we found the assoc only
2508a5d547adSRandall Stewart 				 * after we locked the create lock. This
2509a5d547adSRandall Stewart 				 * means we are in a colliding case and we
2510a5d547adSRandall Stewart 				 * must make sure that we unlock the tcb if
2511a5d547adSRandall Stewart 				 * its one of the cases where we throw away
2512a5d547adSRandall Stewart 				 * the incoming packets.
2513a5d547adSRandall Stewart 				 */
2514a5d547adSRandall Stewart 				*locked_tcb = *stcb;
2515a5d547adSRandall Stewart 
2516a5d547adSRandall Stewart 				/*
2517a5d547adSRandall Stewart 				 * We must also increment the inp ref count
2518a5d547adSRandall Stewart 				 * since the ref_count flags was set when we
2519a5d547adSRandall Stewart 				 * did not find the TCB, now we found it
2520a5d547adSRandall Stewart 				 * which reduces the refcount.. we must
2521a5d547adSRandall Stewart 				 * raise it back out to balance it all :-)
2522a5d547adSRandall Stewart 				 */
2523a5d547adSRandall Stewart 				SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2524a5d547adSRandall Stewart 				if ((*stcb)->sctp_ep != l_inp) {
2525ad81507eSRandall Stewart 					SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2526dd294dceSMichael Tuexen 					    (void *)(*stcb)->sctp_ep, (void *)l_inp);
2527a5d547adSRandall Stewart 				}
2528a5d547adSRandall Stewart 			}
2529f8829a4aSRandall Stewart 		}
2530f8829a4aSRandall Stewart 	}
25310053ed28SMichael Tuexen 
2532f8829a4aSRandall Stewart 	cookie_len -= SCTP_SIGNATURE_SIZE;
2533f8829a4aSRandall Stewart 	if (*stcb == NULL) {
2534f8829a4aSRandall Stewart 		/* this is the "normal" case... get a new TCB */
2535b1754ad1SMichael Tuexen 		*stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
2536b1754ad1SMichael Tuexen 		    cookie, cookie_len, *inp_p,
2537b1754ad1SMichael Tuexen 		    netp, to, &notification,
2538f30ac432SMichael Tuexen 		    auth_skipped, auth_offset, auth_len,
2539457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2540f30ac432SMichael Tuexen 		    vrf_id, port);
2541f8829a4aSRandall Stewart 	} else {
2542f8829a4aSRandall Stewart 		/* this is abnormal... cookie-echo on existing TCB */
2543f8829a4aSRandall Stewart 		had_a_existing_tcb = 1;
2544b1754ad1SMichael Tuexen 		*stcb = sctp_process_cookie_existing(m, iphlen, offset,
2545b1754ad1SMichael Tuexen 		    src, dst, sh,
2546830d754dSRandall Stewart 		    cookie, cookie_len, *inp_p, *stcb, netp, to,
2547f30ac432SMichael Tuexen 		    &notification, auth_skipped, auth_offset, auth_len,
2548457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2549f30ac432SMichael Tuexen 		    vrf_id, port);
25505f2e1835SMichael Tuexen 		if (*stcb == NULL) {
25515f2e1835SMichael Tuexen 			*locked_tcb = NULL;
25525f2e1835SMichael Tuexen 		}
2553f8829a4aSRandall Stewart 	}
2554f8829a4aSRandall Stewart 
2555f8829a4aSRandall Stewart 	if (*stcb == NULL) {
2556f8829a4aSRandall Stewart 		/* still no TCB... must be bad cookie-echo */
2557f8829a4aSRandall Stewart 		return (NULL);
2558f8829a4aSRandall Stewart 	}
25595fe29cdfSMichael Tuexen 	if (*netp != NULL) {
2560457b4b88SMichael Tuexen 		(*netp)->flowtype = mflowtype;
25615fe29cdfSMichael Tuexen 		(*netp)->flowid = mflowid;
2562a4ae38f1SMichael Tuexen 	}
2563f8829a4aSRandall Stewart 	/*
2564f8829a4aSRandall Stewart 	 * Ok, we built an association so confirm the address we sent the
2565f8829a4aSRandall Stewart 	 * INIT-ACK to.
2566f8829a4aSRandall Stewart 	 */
2567f8829a4aSRandall Stewart 	netl = sctp_findnet(*stcb, to);
2568f8829a4aSRandall Stewart 	/*
2569f8829a4aSRandall Stewart 	 * This code should in theory NOT run but
2570f8829a4aSRandall Stewart 	 */
2571f8829a4aSRandall Stewart 	if (netl == NULL) {
2572f8829a4aSRandall Stewart 		/* TSNH! Huh, why do I need to add this address here? */
2573ec70917fSMichael Tuexen 		if (sctp_add_remote_addr(*stcb, to, NULL, port,
25747154bf4aSMichael Tuexen 		    SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
257560990c0cSMichael Tuexen 			return (NULL);
257660990c0cSMichael Tuexen 		}
2577f8829a4aSRandall Stewart 		netl = sctp_findnet(*stcb, to);
2578f8829a4aSRandall Stewart 	}
2579f8829a4aSRandall Stewart 	if (netl) {
2580f8829a4aSRandall Stewart 		if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2581f8829a4aSRandall Stewart 			netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2582ad81507eSRandall Stewart 			(void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2583f8829a4aSRandall Stewart 			    netl);
25842fad0e55SMichael Tuexen 			send_int_conf = 1;
2585f8829a4aSRandall Stewart 		}
2586f8829a4aSRandall Stewart 	}
2587ca85e948SMichael Tuexen 	sctp_start_net_timers(*stcb);
2588f8829a4aSRandall Stewart 	if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2589f8829a4aSRandall Stewart 		if (!had_a_existing_tcb ||
2590f8829a4aSRandall Stewart 		    (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2591f8829a4aSRandall Stewart 			/*
2592f8829a4aSRandall Stewart 			 * If we have a NEW cookie or the connect never
2593f8829a4aSRandall Stewart 			 * reached the connected state during collision we
2594f8829a4aSRandall Stewart 			 * must do the TCP accept thing.
2595f8829a4aSRandall Stewart 			 */
2596f8829a4aSRandall Stewart 			struct socket *so, *oso;
2597f8829a4aSRandall Stewart 			struct sctp_inpcb *inp;
2598f8829a4aSRandall Stewart 
2599f8829a4aSRandall Stewart 			if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2600f8829a4aSRandall Stewart 				/*
2601f8829a4aSRandall Stewart 				 * For a restart we will keep the same
2602f8829a4aSRandall Stewart 				 * socket, no need to do anything. I THINK!!
2603f8829a4aSRandall Stewart 				 */
26047215cc1bSMichael Tuexen 				sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
26052fad0e55SMichael Tuexen 				if (send_int_conf) {
26062fad0e55SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
26072fad0e55SMichael Tuexen 					    (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
26082fad0e55SMichael Tuexen 				}
2609f8829a4aSRandall Stewart 				return (m);
2610f8829a4aSRandall Stewart 			}
2611f8829a4aSRandall Stewart 			oso = (*inp_p)->sctp_socket;
26122dad8a55SRandall Stewart 			atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2613f8829a4aSRandall Stewart 			SCTP_TCB_UNLOCK((*stcb));
26141fb51a12SBjoern A. Zeeb 			CURVNET_SET(oso->so_vnet);
261593164cf9SRandall Stewart 			so = sonewconn(oso, 0
2616f8829a4aSRandall Stewart 			    );
26171fb51a12SBjoern A. Zeeb 			CURVNET_RESTORE();
2618f8829a4aSRandall Stewart 			SCTP_TCB_LOCK((*stcb));
26192dad8a55SRandall Stewart 			atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
26202dad8a55SRandall Stewart 
2621f8829a4aSRandall Stewart 			if (so == NULL) {
2622f8829a4aSRandall Stewart 				struct mbuf *op_err;
262370486b27SMichael Tuexen 
2624f8829a4aSRandall Stewart 				/* Too many sockets */
2625ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2626ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2627f8829a4aSRandall Stewart 				sctp_abort_association(*inp_p, NULL, m, iphlen,
2628b1754ad1SMichael Tuexen 				    src, dst, sh, op_err,
2629457b4b88SMichael Tuexen 				    mflowtype, mflowid,
2630f30ac432SMichael Tuexen 				    vrf_id, port);
2631b7d130beSMichael Tuexen 				(void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC,
2632b7d130beSMichael Tuexen 				    SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
2633f8829a4aSRandall Stewart 				return (NULL);
2634f8829a4aSRandall Stewart 			}
2635f8829a4aSRandall Stewart 			inp = (struct sctp_inpcb *)so->so_pcb;
263693164cf9SRandall Stewart 			SCTP_INP_INCR_REF(inp);
263793164cf9SRandall Stewart 			/*
263893164cf9SRandall Stewart 			 * We add the unbound flag here so that if we get an
263993164cf9SRandall Stewart 			 * soabort() before we get the move_pcb done, we
264093164cf9SRandall Stewart 			 * will properly cleanup.
264193164cf9SRandall Stewart 			 */
2642f8829a4aSRandall Stewart 			inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2643f8829a4aSRandall Stewart 			    SCTP_PCB_FLAGS_CONNECTED |
2644f8829a4aSRandall Stewart 			    SCTP_PCB_FLAGS_IN_TCPPOOL |
264593164cf9SRandall Stewart 			    SCTP_PCB_FLAGS_UNBOUND |
2646f8829a4aSRandall Stewart 			    (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2647f8829a4aSRandall Stewart 			    SCTP_PCB_FLAGS_DONT_WAKE);
2648f8829a4aSRandall Stewart 			inp->sctp_features = (*inp_p)->sctp_features;
2649851b7298SRandall Stewart 			inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2650f8829a4aSRandall Stewart 			inp->sctp_socket = so;
2651f8829a4aSRandall Stewart 			inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
265259b6d5beSMichael Tuexen 			inp->max_cwnd = (*inp_p)->max_cwnd;
265320083c2eSMichael Tuexen 			inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2654f342355aSMichael Tuexen 			inp->ecn_supported = (*inp_p)->ecn_supported;
2655dd973b0eSMichael Tuexen 			inp->prsctp_supported = (*inp_p)->prsctp_supported;
2656c79bec9cSMichael Tuexen 			inp->auth_supported = (*inp_p)->auth_supported;
2657c79bec9cSMichael Tuexen 			inp->asconf_supported = (*inp_p)->asconf_supported;
2658317e00efSMichael Tuexen 			inp->reconfig_supported = (*inp_p)->reconfig_supported;
2659caea9879SMichael Tuexen 			inp->nrsack_supported = (*inp_p)->nrsack_supported;
2660cb9b8e6fSMichael Tuexen 			inp->pktdrop_supported = (*inp_p)->pktdrop_supported;
2661f8829a4aSRandall Stewart 			inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2662f8829a4aSRandall Stewart 			inp->sctp_context = (*inp_p)->sctp_context;
2663c4e848b7SRandall Stewart 			inp->local_strreset_support = (*inp_p)->local_strreset_support;
2664d089f9b9SMichael Tuexen 			inp->fibnum = (*inp_p)->fibnum;
2665f8829a4aSRandall Stewart 			/*
2666f8829a4aSRandall Stewart 			 * copy in the authentication parameters from the
2667f8829a4aSRandall Stewart 			 * original endpoint
2668f8829a4aSRandall Stewart 			 */
2669f8829a4aSRandall Stewart 			if (inp->sctp_ep.local_hmacs)
2670f8829a4aSRandall Stewart 				sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2671f8829a4aSRandall Stewart 			inp->sctp_ep.local_hmacs =
2672f8829a4aSRandall Stewart 			    sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2673f8829a4aSRandall Stewart 			if (inp->sctp_ep.local_auth_chunks)
2674f8829a4aSRandall Stewart 				sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2675f8829a4aSRandall Stewart 			inp->sctp_ep.local_auth_chunks =
2676f8829a4aSRandall Stewart 			    sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2677f8829a4aSRandall Stewart 
2678f8829a4aSRandall Stewart 			/*
2679f8829a4aSRandall Stewart 			 * Now we must move it from one hash table to
2680f8829a4aSRandall Stewart 			 * another and get the tcb in the right place.
2681f8829a4aSRandall Stewart 			 */
268288a7eb29SRandall Stewart 
268388a7eb29SRandall Stewart 			/*
268488a7eb29SRandall Stewart 			 * This is where the one-2-one socket is put into
268588a7eb29SRandall Stewart 			 * the accept state waiting for the accept!
268688a7eb29SRandall Stewart 			 */
268788a7eb29SRandall Stewart 			if (*stcb) {
2688839d21d6SMichael Tuexen 				SCTP_ADD_SUBSTATE(*stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
268988a7eb29SRandall Stewart 			}
2690f8829a4aSRandall Stewart 			sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2691d61a0ae0SRandall Stewart 
2692d61a0ae0SRandall Stewart 			atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2693d61a0ae0SRandall Stewart 			SCTP_TCB_UNLOCK((*stcb));
2694d61a0ae0SRandall Stewart 
2695265de5bbSRobert Watson 			sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2696265de5bbSRobert Watson 			    0);
2697d61a0ae0SRandall Stewart 			SCTP_TCB_LOCK((*stcb));
2698d61a0ae0SRandall Stewart 			atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2699d61a0ae0SRandall Stewart 
270093164cf9SRandall Stewart 			/*
270193164cf9SRandall Stewart 			 * now we must check to see if we were aborted while
270293164cf9SRandall Stewart 			 * the move was going on and the lock/unlock
270393164cf9SRandall Stewart 			 * happened.
270493164cf9SRandall Stewart 			 */
270593164cf9SRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
270693164cf9SRandall Stewart 				/*
270793164cf9SRandall Stewart 				 * yep it was, we leave the assoc attached
270893164cf9SRandall Stewart 				 * to the socket since the sctp_inpcb_free()
270993164cf9SRandall Stewart 				 * call will send an abort for us.
271093164cf9SRandall Stewart 				 */
271193164cf9SRandall Stewart 				SCTP_INP_DECR_REF(inp);
271293164cf9SRandall Stewart 				return (NULL);
271393164cf9SRandall Stewart 			}
271493164cf9SRandall Stewart 			SCTP_INP_DECR_REF(inp);
2715f8829a4aSRandall Stewart 			/* Switch over to the new guy */
2716f8829a4aSRandall Stewart 			*inp_p = inp;
2717ceaad40aSRandall Stewart 			sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
27182fad0e55SMichael Tuexen 			if (send_int_conf) {
27192fad0e55SMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
27202fad0e55SMichael Tuexen 				    (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
27212fad0e55SMichael Tuexen 			}
27220053ed28SMichael Tuexen 
2723b7b84c0eSMichael Tuexen 			/*
2724b7b84c0eSMichael Tuexen 			 * Pull it from the incomplete queue and wake the
2725b7b84c0eSMichael Tuexen 			 * guy
2726b7b84c0eSMichael Tuexen 			 */
272793164cf9SRandall Stewart 			soisconnected(so);
2728f8829a4aSRandall Stewart 			return (m);
2729f8829a4aSRandall Stewart 		}
2730f8829a4aSRandall Stewart 	}
27312fad0e55SMichael Tuexen 	if (notification) {
2732ceaad40aSRandall Stewart 		sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2733f8829a4aSRandall Stewart 	}
27342fad0e55SMichael Tuexen 	if (send_int_conf) {
27352fad0e55SMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
27362fad0e55SMichael Tuexen 		    (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
27372fad0e55SMichael Tuexen 	}
2738f8829a4aSRandall Stewart 	return (m);
2739f8829a4aSRandall Stewart }
2740f8829a4aSRandall Stewart 
2741f8829a4aSRandall Stewart static void
27427215cc1bSMichael Tuexen sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2743f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net)
2744f8829a4aSRandall Stewart {
2745f8829a4aSRandall Stewart 	/* cp must not be used, others call this without a c-ack :-) */
2746f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2747efd5e692SMichael Tuexen 	struct sctp_tmit_chunk *chk;
2748f8829a4aSRandall Stewart 
2749ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
2750ad81507eSRandall Stewart 	    "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2751ad234e3cSMichael Tuexen 	if ((stcb == NULL) || (net == NULL)) {
2752f8829a4aSRandall Stewart 		return;
2753ad234e3cSMichael Tuexen 	}
27540053ed28SMichael Tuexen 
2755f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2756469a65d1SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
2757469a65d1SMichael Tuexen 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2758469a65d1SMichael Tuexen 		    asoc->overall_error_count,
2759469a65d1SMichael Tuexen 		    0,
2760469a65d1SMichael Tuexen 		    SCTP_FROM_SCTP_INPUT,
2761469a65d1SMichael Tuexen 		    __LINE__);
2762469a65d1SMichael Tuexen 	}
2763f8829a4aSRandall Stewart 	sctp_stop_all_cookie_timers(stcb);
2764f8829a4aSRandall Stewart 	/* process according to association state */
2765839d21d6SMichael Tuexen 	if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) {
2766f8829a4aSRandall Stewart 		/* state change only needed when I am in right state */
2767ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2768839d21d6SMichael Tuexen 		SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
2769ca85e948SMichael Tuexen 		sctp_start_net_timers(stcb);
2770f8829a4aSRandall Stewart 		if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2771f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
27726fb7b4fbSMichael Tuexen 			    stcb->sctp_ep, stcb, NULL);
2773f8829a4aSRandall Stewart 		}
2774f8829a4aSRandall Stewart 		/* update RTO */
2775f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2776f8829a4aSRandall Stewart 		SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2777f8829a4aSRandall Stewart 		if (asoc->overall_error_count == 0) {
277844f2a327SMichael Tuexen 			sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered,
2779f79aab18SRandall Stewart 			    SCTP_RTT_FROM_NON_DATA);
2780f8829a4aSRandall Stewart 		}
2781*8ed1e2c8SMichael Tuexen 		/*
2782*8ed1e2c8SMichael Tuexen 		 * Since we did not send a HB make sure we don't double
2783*8ed1e2c8SMichael Tuexen 		 * things.
2784*8ed1e2c8SMichael Tuexen 		 */
2785*8ed1e2c8SMichael Tuexen 		asoc->overall_error_count = 0;
2786*8ed1e2c8SMichael Tuexen 		net->hb_responded = 1;
27876e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2788ceaad40aSRandall Stewart 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2789f8829a4aSRandall Stewart 		if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2790f8829a4aSRandall Stewart 		    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2791a5c2009dSMichael Tuexen 			sctp_pcb_add_flags(stcb->sctp_ep, SCTP_PCB_FLAGS_CONNECTED);
2792d69e7322SRandall Stewart 			if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
2793ceaad40aSRandall Stewart 				soisconnected(stcb->sctp_socket);
2794d69e7322SRandall Stewart 			}
2795f8829a4aSRandall Stewart 		}
2796f8829a4aSRandall Stewart 
2797d69e7322SRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2798d69e7322SRandall Stewart 			/*
2799d69e7322SRandall Stewart 			 * We don't need to do the asconf thing, nor hb or
2800d69e7322SRandall Stewart 			 * autoclose if the socket is closed.
2801d69e7322SRandall Stewart 			 */
2802d69e7322SRandall Stewart 			goto closed_socket;
2803d69e7322SRandall Stewart 		}
28040053ed28SMichael Tuexen 
2805d69e7322SRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2806d69e7322SRandall Stewart 		    stcb, net);
2807d69e7322SRandall Stewart 
2808f8829a4aSRandall Stewart 		if (stcb->asoc.sctp_autoclose_ticks &&
2809f8829a4aSRandall Stewart 		    sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2810f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2811f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, NULL);
2812f8829a4aSRandall Stewart 		}
2813f8829a4aSRandall Stewart 		/*
28141b649582SRandall Stewart 		 * send ASCONF if parameters are pending and ASCONFs are
28151b649582SRandall Stewart 		 * allowed (eg. addresses changed when init/cookie echo were
28161b649582SRandall Stewart 		 * in flight)
2817f8829a4aSRandall Stewart 		 */
2818f8829a4aSRandall Stewart 		if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2819c79bec9cSMichael Tuexen 		    (stcb->asoc.asconf_supported == 1) &&
2820f8829a4aSRandall Stewart 		    (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
28211b649582SRandall Stewart #ifdef SCTP_TIMER_BASED_ASCONF
2822f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2823f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb,
2824f8829a4aSRandall Stewart 			    stcb->asoc.primary_destination);
28251b649582SRandall Stewart #else
28263232788eSRandall Stewart 			sctp_send_asconf(stcb, stcb->asoc.primary_destination,
28273232788eSRandall Stewart 			    SCTP_ADDR_NOT_LOCKED);
28281b649582SRandall Stewart #endif
2829f8829a4aSRandall Stewart 		}
2830f8829a4aSRandall Stewart 	}
2831d69e7322SRandall Stewart closed_socket:
2832f8829a4aSRandall Stewart 	/* Toss the cookie if I can */
2833f8829a4aSRandall Stewart 	sctp_toss_old_cookies(stcb, asoc);
2834f8829a4aSRandall Stewart 	/* Restart the timer if we have pending data */
2835efd5e692SMichael Tuexen 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
2836efd5e692SMichael Tuexen 		if (chk->whoTo != NULL) {
2837efd5e692SMichael Tuexen 			break;
2838efd5e692SMichael Tuexen 		}
2839efd5e692SMichael Tuexen 	}
2840efd5e692SMichael Tuexen 	if (chk != NULL) {
28414a9ef3f8SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
2842f8829a4aSRandall Stewart 	}
2843f8829a4aSRandall Stewart }
2844f8829a4aSRandall Stewart 
2845f8829a4aSRandall Stewart static void
2846f8829a4aSRandall Stewart sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
2847f8829a4aSRandall Stewart     struct sctp_tcb *stcb)
2848f8829a4aSRandall Stewart {
2849f8829a4aSRandall Stewart 	struct sctp_nets *net;
2850f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *lchk;
2851a21779f0SRandall Stewart 	struct sctp_ecne_chunk bkup;
285260990c0cSMichael Tuexen 	uint8_t override_bit;
2853a21779f0SRandall Stewart 	uint32_t tsn, window_data_tsn;
2854a21779f0SRandall Stewart 	int len;
2855dec0177dSRandall Stewart 	unsigned int pkt_cnt;
2856f8829a4aSRandall Stewart 
2857a21779f0SRandall Stewart 	len = ntohs(cp->ch.chunk_length);
2858a21779f0SRandall Stewart 	if (len == sizeof(struct old_sctp_ecne_chunk)) {
2859a21779f0SRandall Stewart 		/* Its the old format */
2860a21779f0SRandall Stewart 		memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
2861a21779f0SRandall Stewart 		bkup.num_pkts_since_cwr = htonl(1);
2862a21779f0SRandall Stewart 		cp = &bkup;
2863a21779f0SRandall Stewart 	}
2864f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvecne);
2865f8829a4aSRandall Stewart 	tsn = ntohl(cp->tsn);
2866a21779f0SRandall Stewart 	pkt_cnt = ntohl(cp->num_pkts_since_cwr);
2867a21779f0SRandall Stewart 	lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
2868f8829a4aSRandall Stewart 	if (lchk == NULL) {
2869493d8e5aSRandall Stewart 		window_data_tsn = stcb->asoc.sending_seq - 1;
2870f8829a4aSRandall Stewart 	} else {
287149656eefSMichael Tuexen 		window_data_tsn = lchk->rec.data.tsn;
2872f8829a4aSRandall Stewart 	}
2873f8829a4aSRandall Stewart 
2874a21779f0SRandall Stewart 	/* Find where it was sent to if possible. */
2875f8829a4aSRandall Stewart 	net = NULL;
28764a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
287749656eefSMichael Tuexen 		if (lchk->rec.data.tsn == tsn) {
2878f8829a4aSRandall Stewart 			net = lchk->whoTo;
2879899288aeSRandall Stewart 			net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
2880f8829a4aSRandall Stewart 			break;
2881f8829a4aSRandall Stewart 		}
288249656eefSMichael Tuexen 		if (SCTP_TSN_GT(lchk->rec.data.tsn, tsn)) {
2883f8829a4aSRandall Stewart 			break;
2884f8829a4aSRandall Stewart 		}
288520b07a4dSMichael Tuexen 	}
2886a21779f0SRandall Stewart 	if (net == NULL) {
2887a21779f0SRandall Stewart 		/*
2888a21779f0SRandall Stewart 		 * What to do. A previous send of a CWR was possibly lost.
2889a21779f0SRandall Stewart 		 * See how old it is, we may have it marked on the actual
2890a21779f0SRandall Stewart 		 * net.
2891a21779f0SRandall Stewart 		 */
2892a21779f0SRandall Stewart 		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2893a21779f0SRandall Stewart 			if (tsn == net->last_cwr_tsn) {
2894a21779f0SRandall Stewart 				/* Found him, send it off */
289560990c0cSMichael Tuexen 				break;
2896a21779f0SRandall Stewart 			}
2897a21779f0SRandall Stewart 		}
289860990c0cSMichael Tuexen 		if (net == NULL) {
2899a21779f0SRandall Stewart 			/*
290060990c0cSMichael Tuexen 			 * If we reach here, we need to send a special CWR
290160990c0cSMichael Tuexen 			 * that says hey, we did this a long time ago and
290260990c0cSMichael Tuexen 			 * you lost the response.
2903a21779f0SRandall Stewart 			 */
2904a21779f0SRandall Stewart 			net = TAILQ_FIRST(&stcb->asoc.nets);
290560990c0cSMichael Tuexen 			if (net == NULL) {
290660990c0cSMichael Tuexen 				/* TSNH */
290760990c0cSMichael Tuexen 				return;
2908a21779f0SRandall Stewart 			}
290960990c0cSMichael Tuexen 			override_bit = SCTP_CWR_REDUCE_OVERRIDE;
291060990c0cSMichael Tuexen 		} else {
291160990c0cSMichael Tuexen 			override_bit = 0;
291260990c0cSMichael Tuexen 		}
291360990c0cSMichael Tuexen 	} else {
291460990c0cSMichael Tuexen 		override_bit = 0;
291560990c0cSMichael Tuexen 	}
2916493d8e5aSRandall Stewart 	if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
2917493d8e5aSRandall Stewart 	    ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
2918b7b84c0eSMichael Tuexen 		/*
2919b7b84c0eSMichael Tuexen 		 * JRS - Use the congestion control given in the pluggable
2920b7b84c0eSMichael Tuexen 		 * CC module
2921b7b84c0eSMichael Tuexen 		 */
2922493d8e5aSRandall Stewart 		stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
2923f8829a4aSRandall Stewart 		/*
2924a21779f0SRandall Stewart 		 * We reduce once every RTT. So we will only lower cwnd at
2925a21779f0SRandall Stewart 		 * the next sending seq i.e. the window_data_tsn
2926f8829a4aSRandall Stewart 		 */
2927a21779f0SRandall Stewart 		net->cwr_window_tsn = window_data_tsn;
2928a21779f0SRandall Stewart 		net->ecn_ce_pkt_cnt += pkt_cnt;
2929a21779f0SRandall Stewart 		net->lost_cnt = pkt_cnt;
2930a21779f0SRandall Stewart 		net->last_cwr_tsn = tsn;
2931a21779f0SRandall Stewart 	} else {
2932a21779f0SRandall Stewart 		override_bit |= SCTP_CWR_IN_SAME_WINDOW;
2933493d8e5aSRandall Stewart 		if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
2934493d8e5aSRandall Stewart 		    ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
2935a21779f0SRandall Stewart 			/*
2936493d8e5aSRandall Stewart 			 * Another loss in the same window update how many
2937493d8e5aSRandall Stewart 			 * marks/packets lost we have had.
2938a21779f0SRandall Stewart 			 */
2939493d8e5aSRandall Stewart 			int cnt = 1;
2940a21779f0SRandall Stewart 
2941a21779f0SRandall Stewart 			if (pkt_cnt > net->lost_cnt) {
2942a21779f0SRandall Stewart 				/* Should be the case */
2943493d8e5aSRandall Stewart 				cnt = (pkt_cnt - net->lost_cnt);
2944493d8e5aSRandall Stewart 				net->ecn_ce_pkt_cnt += cnt;
2945a21779f0SRandall Stewart 			}
2946493d8e5aSRandall Stewart 			net->lost_cnt = pkt_cnt;
2947a21779f0SRandall Stewart 			net->last_cwr_tsn = tsn;
2948493d8e5aSRandall Stewart 			/*
2949493d8e5aSRandall Stewart 			 * Most CC functions will ignore this call, since we
2950493d8e5aSRandall Stewart 			 * are in-window yet of the initial CE the peer saw.
2951493d8e5aSRandall Stewart 			 */
2952493d8e5aSRandall Stewart 			stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
2953a21779f0SRandall Stewart 		}
2954f8829a4aSRandall Stewart 	}
2955f8829a4aSRandall Stewart 	/*
2956f8829a4aSRandall Stewart 	 * We always send a CWR this way if our previous one was lost our
2957f8829a4aSRandall Stewart 	 * peer will get an update, or if it is not time again to reduce we
2958a21779f0SRandall Stewart 	 * still get the cwr to the peer. Note we set the override when we
2959a21779f0SRandall Stewart 	 * could not find the TSN on the chunk or the destination network.
2960f8829a4aSRandall Stewart 	 */
2961a21779f0SRandall Stewart 	sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
2962f8829a4aSRandall Stewart }
2963f8829a4aSRandall Stewart 
2964f8829a4aSRandall Stewart static void
2965a21779f0SRandall Stewart sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
2966f8829a4aSRandall Stewart {
2967f8829a4aSRandall Stewart 	/*
2968f8829a4aSRandall Stewart 	 * Here we get a CWR from the peer. We must look in the outqueue and
2969582212faSEitan Adler 	 * make sure that we have a covered ECNE in the control chunk part.
2970f8829a4aSRandall Stewart 	 * If so remove it.
2971f8829a4aSRandall Stewart 	 */
29726c2cfc04SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
2973f8829a4aSRandall Stewart 	struct sctp_ecne_chunk *ecne;
2974a21779f0SRandall Stewart 	int override;
2975a21779f0SRandall Stewart 	uint32_t cwr_tsn;
2976f8829a4aSRandall Stewart 
2977a21779f0SRandall Stewart 	cwr_tsn = ntohl(cp->tsn);
2978a21779f0SRandall Stewart 	override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
29796c2cfc04SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &stcb->asoc.control_send_queue, sctp_next, nchk) {
2980f8829a4aSRandall Stewart 		if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
2981f8829a4aSRandall Stewart 			continue;
2982f8829a4aSRandall Stewart 		}
2983a21779f0SRandall Stewart 		if ((override == 0) && (chk->whoTo != net)) {
2984a21779f0SRandall Stewart 			/* Must be from the right src unless override is set */
2985a21779f0SRandall Stewart 			continue;
2986a21779f0SRandall Stewart 		}
2987f8829a4aSRandall Stewart 		ecne = mtod(chk->data, struct sctp_ecne_chunk *);
2988a21779f0SRandall Stewart 		if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
2989f8829a4aSRandall Stewart 			/* this covers this ECNE, we can remove it */
2990f8829a4aSRandall Stewart 			stcb->asoc.ecn_echo_cnt_onq--;
2991f8829a4aSRandall Stewart 			TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
2992f8829a4aSRandall Stewart 			    sctp_next);
2993cd6340caSMichael Tuexen 			stcb->asoc.ctrl_queue_cnt--;
2994f8829a4aSRandall Stewart 			sctp_m_freem(chk->data);
2995f8829a4aSRandall Stewart 			chk->data = NULL;
2996689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
2997a21779f0SRandall Stewart 			if (override == 0) {
2998f8829a4aSRandall Stewart 				break;
2999f8829a4aSRandall Stewart 			}
3000f8829a4aSRandall Stewart 		}
3001f8829a4aSRandall Stewart 	}
3002a21779f0SRandall Stewart }
3003f8829a4aSRandall Stewart 
3004f8829a4aSRandall Stewart static void
30057215cc1bSMichael Tuexen sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3006f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net)
3007f8829a4aSRandall Stewart {
3008ceaad40aSRandall Stewart 
3009ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
3010ad81507eSRandall Stewart 	    "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3011f8829a4aSRandall Stewart 	if (stcb == NULL)
3012f8829a4aSRandall Stewart 		return;
3013f8829a4aSRandall Stewart 
3014f8829a4aSRandall Stewart 	/* process according to association state */
3015839d21d6SMichael Tuexen 	if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3016f8829a4aSRandall Stewart 		/* unexpected SHUTDOWN-COMPLETE... so ignore... */
30172afb3e84SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
30182afb3e84SRandall Stewart 		    "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3019f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3020f8829a4aSRandall Stewart 		return;
3021f8829a4aSRandall Stewart 	}
3022f8829a4aSRandall Stewart 	/* notify upper layer protocol */
3023f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
3024ceaad40aSRandall Stewart 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
302556f778aaSMichael Tuexen 	}
302656f778aaSMichael Tuexen #ifdef INVARIANTS
30270f1346f7SMichael Tuexen 	if (!TAILQ_EMPTY(&stcb->asoc.send_queue) ||
30280f1346f7SMichael Tuexen 	    !TAILQ_EMPTY(&stcb->asoc.sent_queue) ||
3029124d851aSMichael Tuexen 	    sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
303056f778aaSMichael Tuexen 		panic("Queues are not empty when handling SHUTDOWN-COMPLETE");
3031f8829a4aSRandall Stewart 	}
303256f778aaSMichael Tuexen #endif
3033f8829a4aSRandall Stewart 	/* stop the timer */
3034b7d130beSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net,
3035b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3036f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3037f8829a4aSRandall Stewart 	/* free the TCB */
30382afb3e84SRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
30392afb3e84SRandall Stewart 	    "sctp_handle_shutdown_complete: calls free-asoc\n");
3040b7d130beSMichael Tuexen 	(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
3041b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3042f8829a4aSRandall Stewart 	return;
3043f8829a4aSRandall Stewart }
3044f8829a4aSRandall Stewart 
3045f8829a4aSRandall Stewart static int
3046f8829a4aSRandall Stewart process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3047f8829a4aSRandall Stewart     struct sctp_nets *net, uint8_t flg)
3048f8829a4aSRandall Stewart {
3049f8829a4aSRandall Stewart 	switch (desc->chunk_type) {
3050f8829a4aSRandall Stewart 	case SCTP_DATA:
305132df1c9eSMichael Tuexen 	case SCTP_IDATA:
305232df1c9eSMichael Tuexen 		/* find the tsn to resend (possibly) */
3053f8829a4aSRandall Stewart 		{
3054f8829a4aSRandall Stewart 			uint32_t tsn;
3055f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *tp1;
3056f8829a4aSRandall Stewart 
3057f8829a4aSRandall Stewart 			tsn = ntohl(desc->tsn_ifany);
30584a9ef3f8SMichael Tuexen 			TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
305949656eefSMichael Tuexen 				if (tp1->rec.data.tsn == tsn) {
3060f8829a4aSRandall Stewart 					/* found it */
3061f8829a4aSRandall Stewart 					break;
3062f8829a4aSRandall Stewart 				}
306349656eefSMichael Tuexen 				if (SCTP_TSN_GT(tp1->rec.data.tsn, tsn)) {
3064f8829a4aSRandall Stewart 					/* not found */
3065f8829a4aSRandall Stewart 					tp1 = NULL;
3066f8829a4aSRandall Stewart 					break;
3067f8829a4aSRandall Stewart 				}
3068f8829a4aSRandall Stewart 			}
3069f8829a4aSRandall Stewart 			if (tp1 == NULL) {
3070f8829a4aSRandall Stewart 				/*
3071f8829a4aSRandall Stewart 				 * Do it the other way , aka without paying
3072f8829a4aSRandall Stewart 				 * attention to queue seq order.
3073f8829a4aSRandall Stewart 				 */
3074f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpdnfnd);
30754a9ef3f8SMichael Tuexen 				TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
307649656eefSMichael Tuexen 					if (tp1->rec.data.tsn == tsn) {
3077f8829a4aSRandall Stewart 						/* found it */
3078f8829a4aSRandall Stewart 						break;
3079f8829a4aSRandall Stewart 					}
3080f8829a4aSRandall Stewart 				}
3081f8829a4aSRandall Stewart 			}
3082f8829a4aSRandall Stewart 			if (tp1 == NULL) {
3083f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrptsnnf);
3084f8829a4aSRandall Stewart 			}
3085f8829a4aSRandall Stewart 			if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
30867da23bc8SMichael Tuexen 				if (((flg & SCTP_BADCRC) == 0) &&
30877da23bc8SMichael Tuexen 				    ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
30887da23bc8SMichael Tuexen 					return (0);
30897da23bc8SMichael Tuexen 				}
3090f8829a4aSRandall Stewart 				if ((stcb->asoc.peers_rwnd == 0) &&
3091f8829a4aSRandall Stewart 				    ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3092f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_pdrpdiwnp);
3093f8829a4aSRandall Stewart 					return (0);
3094f8829a4aSRandall Stewart 				}
3095f8829a4aSRandall Stewart 				if (stcb->asoc.peers_rwnd == 0 &&
3096f8829a4aSRandall Stewart 				    (flg & SCTP_FROM_MIDDLE_BOX)) {
3097f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_pdrpdizrw);
3098f8829a4aSRandall Stewart 					return (0);
3099f8829a4aSRandall Stewart 				}
310032df1c9eSMichael Tuexen 				if ((uint32_t)SCTP_BUF_LEN(tp1->data) <
310132df1c9eSMichael Tuexen 				    SCTP_DATA_CHUNK_OVERHEAD(stcb) + SCTP_NUM_DB_TO_VERIFY) {
310232df1c9eSMichael Tuexen 					/* Payload not matching. */
3103f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_pdrpbadd);
3104f8829a4aSRandall Stewart 					return (-1);
3105f8829a4aSRandall Stewart 				}
310632df1c9eSMichael Tuexen 				if (memcmp(mtod(tp1->data, caddr_t)+SCTP_DATA_CHUNK_OVERHEAD(stcb),
310732df1c9eSMichael Tuexen 				    desc->data_bytes, SCTP_NUM_DB_TO_VERIFY) != 0) {
310832df1c9eSMichael Tuexen 					/* Payload not matching. */
310932df1c9eSMichael Tuexen 					SCTP_STAT_INCR(sctps_pdrpbadd);
311032df1c9eSMichael Tuexen 					return (-1);
3111f8829a4aSRandall Stewart 				}
3112f8829a4aSRandall Stewart 				if (tp1->do_rtt) {
3113f8829a4aSRandall Stewart 					/*
3114f8829a4aSRandall Stewart 					 * this guy had a RTO calculation
3115f8829a4aSRandall Stewart 					 * pending on it, cancel it
3116f8829a4aSRandall Stewart 					 */
3117f79aab18SRandall Stewart 					if (tp1->whoTo->rto_needed == 0) {
3118f79aab18SRandall Stewart 						tp1->whoTo->rto_needed = 1;
3119f79aab18SRandall Stewart 					}
3120f8829a4aSRandall Stewart 					tp1->do_rtt = 0;
3121f8829a4aSRandall Stewart 				}
3122f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpmark);
3123f8829a4aSRandall Stewart 				if (tp1->sent != SCTP_DATAGRAM_RESEND)
3124f8829a4aSRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3125f8829a4aSRandall Stewart 				/*
3126f8829a4aSRandall Stewart 				 * mark it as if we were doing a FR, since
3127f8829a4aSRandall Stewart 				 * we will be getting gap ack reports behind
3128f8829a4aSRandall Stewart 				 * the info from the router.
3129f8829a4aSRandall Stewart 				 */
3130f8829a4aSRandall Stewart 				tp1->rec.data.doing_fast_retransmit = 1;
3131f8829a4aSRandall Stewart 				/*
3132f8829a4aSRandall Stewart 				 * mark the tsn with what sequences can
3133f8829a4aSRandall Stewart 				 * cause a new FR.
3134f8829a4aSRandall Stewart 				 */
3135f8829a4aSRandall Stewart 				if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3136f8829a4aSRandall Stewart 					tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3137f8829a4aSRandall Stewart 				} else {
313849656eefSMichael Tuexen 					tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.tsn;
3139f8829a4aSRandall Stewart 				}
3140f8829a4aSRandall Stewart 
3141f8829a4aSRandall Stewart 				/* restart the timer */
3142f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3143b7d130beSMichael Tuexen 				    stcb, tp1->whoTo,
3144b7d130beSMichael Tuexen 				    SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3145f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3146f8829a4aSRandall Stewart 				    stcb, tp1->whoTo);
3147f8829a4aSRandall Stewart 
3148f8829a4aSRandall Stewart 				/* fix counts and things */
3149b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3150c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3151a5d547adSRandall Stewart 					    tp1->whoTo->flight_size,
3152a5d547adSRandall Stewart 					    tp1->book_size,
31539a8e3088SMichael Tuexen 					    (uint32_t)(uintptr_t)stcb,
315449656eefSMichael Tuexen 					    tp1->rec.data.tsn);
315580fefe0aSRandall Stewart 				}
31568933fa13SRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3157c105859eSRandall Stewart 					sctp_flight_size_decrease(tp1);
3158c105859eSRandall Stewart 					sctp_total_flight_decrease(stcb, tp1);
31598933fa13SRandall Stewart 				}
3160f23ba7b1SMichael Tuexen 				tp1->sent = SCTP_DATAGRAM_RESEND;
3161f8829a4aSRandall Stewart 			} {
3162f8829a4aSRandall Stewart 				/* audit code */
3163f8829a4aSRandall Stewart 				unsigned int audit;
3164f8829a4aSRandall Stewart 
3165f8829a4aSRandall Stewart 				audit = 0;
3166f8829a4aSRandall Stewart 				TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3167f8829a4aSRandall Stewart 					if (tp1->sent == SCTP_DATAGRAM_RESEND)
3168f8829a4aSRandall Stewart 						audit++;
3169f8829a4aSRandall Stewart 				}
3170f8829a4aSRandall Stewart 				TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3171f8829a4aSRandall Stewart 				    sctp_next) {
3172f8829a4aSRandall Stewart 					if (tp1->sent == SCTP_DATAGRAM_RESEND)
3173f8829a4aSRandall Stewart 						audit++;
3174f8829a4aSRandall Stewart 				}
3175f8829a4aSRandall Stewart 				if (audit != stcb->asoc.sent_queue_retran_cnt) {
3176ad81507eSRandall Stewart 					SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3177f8829a4aSRandall Stewart 					    audit, stcb->asoc.sent_queue_retran_cnt);
3178f8829a4aSRandall Stewart #ifndef SCTP_AUDITING_ENABLED
3179f8829a4aSRandall Stewart 					stcb->asoc.sent_queue_retran_cnt = audit;
3180f8829a4aSRandall Stewart #endif
3181f8829a4aSRandall Stewart 				}
3182f8829a4aSRandall Stewart 			}
3183f8829a4aSRandall Stewart 		}
3184f8829a4aSRandall Stewart 		break;
3185f8829a4aSRandall Stewart 	case SCTP_ASCONF:
3186f8829a4aSRandall Stewart 		{
3187f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *asconf;
3188f8829a4aSRandall Stewart 
3189f8829a4aSRandall Stewart 			TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3190f8829a4aSRandall Stewart 			    sctp_next) {
3191f8829a4aSRandall Stewart 				if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3192f8829a4aSRandall Stewart 					break;
3193f8829a4aSRandall Stewart 				}
3194f8829a4aSRandall Stewart 			}
3195f8829a4aSRandall Stewart 			if (asconf) {
3196f8829a4aSRandall Stewart 				if (asconf->sent != SCTP_DATAGRAM_RESEND)
3197f8829a4aSRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3198f8829a4aSRandall Stewart 				asconf->sent = SCTP_DATAGRAM_RESEND;
3199f8829a4aSRandall Stewart 				asconf->snd_count--;
3200f8829a4aSRandall Stewart 			}
3201f8829a4aSRandall Stewart 		}
3202f8829a4aSRandall Stewart 		break;
3203f8829a4aSRandall Stewart 	case SCTP_INITIATION:
3204f8829a4aSRandall Stewart 		/* resend the INIT */
3205f8829a4aSRandall Stewart 		stcb->asoc.dropped_special_cnt++;
3206f8829a4aSRandall Stewart 		if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3207f8829a4aSRandall Stewart 			/*
3208f8829a4aSRandall Stewart 			 * If we can get it in, in a few attempts we do
3209f8829a4aSRandall Stewart 			 * this, otherwise we let the timer fire.
3210f8829a4aSRandall Stewart 			 */
3211f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3212b7d130beSMichael Tuexen 			    stcb, net,
3213b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
3214ceaad40aSRandall Stewart 			sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3215f8829a4aSRandall Stewart 		}
3216f8829a4aSRandall Stewart 		break;
3217f8829a4aSRandall Stewart 	case SCTP_SELECTIVE_ACK:
3218b5c16493SMichael Tuexen 	case SCTP_NR_SELECTIVE_ACK:
3219f8829a4aSRandall Stewart 		/* resend the sack */
3220689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3221f8829a4aSRandall Stewart 		break;
3222f8829a4aSRandall Stewart 	case SCTP_HEARTBEAT_REQUEST:
3223f8829a4aSRandall Stewart 		/* resend a demand HB */
3224b54d3a6cSRandall Stewart 		if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3225b7b84c0eSMichael Tuexen 			/*
3226b7b84c0eSMichael Tuexen 			 * Only retransmit if we KNOW we wont destroy the
3227b7b84c0eSMichael Tuexen 			 * tcb
3228b7b84c0eSMichael Tuexen 			 */
3229ca85e948SMichael Tuexen 			sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3230b54d3a6cSRandall Stewart 		}
3231f8829a4aSRandall Stewart 		break;
3232f8829a4aSRandall Stewart 	case SCTP_SHUTDOWN:
3233f8829a4aSRandall Stewart 		sctp_send_shutdown(stcb, net);
3234f8829a4aSRandall Stewart 		break;
3235f8829a4aSRandall Stewart 	case SCTP_SHUTDOWN_ACK:
3236f8829a4aSRandall Stewart 		sctp_send_shutdown_ack(stcb, net);
3237f8829a4aSRandall Stewart 		break;
3238f8829a4aSRandall Stewart 	case SCTP_COOKIE_ECHO:
3239f8829a4aSRandall Stewart 		{
3240f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *cookie;
3241f8829a4aSRandall Stewart 
3242f8829a4aSRandall Stewart 			cookie = NULL;
3243f8829a4aSRandall Stewart 			TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3244f8829a4aSRandall Stewart 			    sctp_next) {
3245f8829a4aSRandall Stewart 				if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3246f8829a4aSRandall Stewart 					break;
3247f8829a4aSRandall Stewart 				}
3248f8829a4aSRandall Stewart 			}
3249f8829a4aSRandall Stewart 			if (cookie) {
3250f8829a4aSRandall Stewart 				if (cookie->sent != SCTP_DATAGRAM_RESEND)
3251f8829a4aSRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3252f8829a4aSRandall Stewart 				cookie->sent = SCTP_DATAGRAM_RESEND;
3253f8829a4aSRandall Stewart 				sctp_stop_all_cookie_timers(stcb);
3254f8829a4aSRandall Stewart 			}
3255f8829a4aSRandall Stewart 		}
3256f8829a4aSRandall Stewart 		break;
3257f8829a4aSRandall Stewart 	case SCTP_COOKIE_ACK:
3258f8829a4aSRandall Stewart 		sctp_send_cookie_ack(stcb);
3259f8829a4aSRandall Stewart 		break;
3260f8829a4aSRandall Stewart 	case SCTP_ASCONF_ACK:
3261f8829a4aSRandall Stewart 		/* resend last asconf ack */
32622afb3e84SRandall Stewart 		sctp_send_asconf_ack(stcb);
3263f8829a4aSRandall Stewart 		break;
326444249214SRandall Stewart 	case SCTP_IFORWARD_CUM_TSN:
3265f8829a4aSRandall Stewart 	case SCTP_FORWARD_CUM_TSN:
3266f8829a4aSRandall Stewart 		send_forward_tsn(stcb, &stcb->asoc);
3267f8829a4aSRandall Stewart 		break;
3268f8829a4aSRandall Stewart 		/* can't do anything with these */
3269f8829a4aSRandall Stewart 	case SCTP_PACKET_DROPPED:
3270f8829a4aSRandall Stewart 	case SCTP_INITIATION_ACK:	/* this should not happen */
3271f8829a4aSRandall Stewart 	case SCTP_HEARTBEAT_ACK:
3272f8829a4aSRandall Stewart 	case SCTP_ABORT_ASSOCIATION:
3273f8829a4aSRandall Stewart 	case SCTP_OPERATION_ERROR:
3274f8829a4aSRandall Stewart 	case SCTP_SHUTDOWN_COMPLETE:
3275f8829a4aSRandall Stewart 	case SCTP_ECN_ECHO:
3276f8829a4aSRandall Stewart 	case SCTP_ECN_CWR:
3277f8829a4aSRandall Stewart 	default:
3278f8829a4aSRandall Stewart 		break;
3279f8829a4aSRandall Stewart 	}
3280f8829a4aSRandall Stewart 	return (0);
3281f8829a4aSRandall Stewart }
3282f8829a4aSRandall Stewart 
3283f8829a4aSRandall Stewart void
3284a169d6ecSMichael Tuexen sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3285f8829a4aSRandall Stewart {
3286a169d6ecSMichael Tuexen 	uint32_t i;
3287f8829a4aSRandall Stewart 	uint16_t temp;
3288f8829a4aSRandall Stewart 
3289f8829a4aSRandall Stewart 	/*
329044249214SRandall Stewart 	 * We set things to 0xffffffff since this is the last delivered
329144249214SRandall Stewart 	 * sequence and we will be sending in 0 after the reset.
3292f8829a4aSRandall Stewart 	 */
3293f8829a4aSRandall Stewart 
3294f8829a4aSRandall Stewart 	if (number_entries) {
3295f8829a4aSRandall Stewart 		for (i = 0; i < number_entries; i++) {
3296f8829a4aSRandall Stewart 			temp = ntohs(list[i]);
3297f8829a4aSRandall Stewart 			if (temp >= stcb->asoc.streamincnt) {
3298f8829a4aSRandall Stewart 				continue;
3299f8829a4aSRandall Stewart 			}
330049656eefSMichael Tuexen 			stcb->asoc.strmin[temp].last_mid_delivered = 0xffffffff;
3301f8829a4aSRandall Stewart 		}
3302f8829a4aSRandall Stewart 	} else {
3303f8829a4aSRandall Stewart 		list = NULL;
3304f8829a4aSRandall Stewart 		for (i = 0; i < stcb->asoc.streamincnt; i++) {
330549656eefSMichael Tuexen 			stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
3306f8829a4aSRandall Stewart 		}
3307f8829a4aSRandall Stewart 	}
3308ceaad40aSRandall Stewart 	sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3309f8829a4aSRandall Stewart }
3310f8829a4aSRandall Stewart 
3311f8829a4aSRandall Stewart static void
331256f778aaSMichael Tuexen sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3313f8829a4aSRandall Stewart {
331456f778aaSMichael Tuexen 	uint32_t i;
3315f8829a4aSRandall Stewart 	uint16_t temp;
3316f8829a4aSRandall Stewart 
331756f778aaSMichael Tuexen 	if (number_entries > 0) {
331856f778aaSMichael Tuexen 		for (i = 0; i < number_entries; i++) {
3319f8829a4aSRandall Stewart 			temp = ntohs(list[i]);
3320f8829a4aSRandall Stewart 			if (temp >= stcb->asoc.streamoutcnt) {
3321f8829a4aSRandall Stewart 				/* no such stream */
3322f8829a4aSRandall Stewart 				continue;
3323f8829a4aSRandall Stewart 			}
332463d5b568SMichael Tuexen 			stcb->asoc.strmout[temp].next_mid_ordered = 0;
332563d5b568SMichael Tuexen 			stcb->asoc.strmout[temp].next_mid_unordered = 0;
3326f8829a4aSRandall Stewart 		}
332756f778aaSMichael Tuexen 	} else {
332856f778aaSMichael Tuexen 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
332963d5b568SMichael Tuexen 			stcb->asoc.strmout[i].next_mid_ordered = 0;
333063d5b568SMichael Tuexen 			stcb->asoc.strmout[i].next_mid_unordered = 0;
333156f778aaSMichael Tuexen 		}
3332f8829a4aSRandall Stewart 	}
3333ceaad40aSRandall Stewart 	sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3334f8829a4aSRandall Stewart }
3335f8829a4aSRandall Stewart 
33367cca1775SRandall Stewart static void
33377cca1775SRandall Stewart sctp_reset_clear_pending(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
33387cca1775SRandall Stewart {
33397cca1775SRandall Stewart 	uint32_t i;
33407cca1775SRandall Stewart 	uint16_t temp;
33417cca1775SRandall Stewart 
33427cca1775SRandall Stewart 	if (number_entries > 0) {
33437cca1775SRandall Stewart 		for (i = 0; i < number_entries; i++) {
33447cca1775SRandall Stewart 			temp = ntohs(list[i]);
33457cca1775SRandall Stewart 			if (temp >= stcb->asoc.streamoutcnt) {
33467cca1775SRandall Stewart 				/* no such stream */
33477cca1775SRandall Stewart 				continue;
33487cca1775SRandall Stewart 			}
33497cca1775SRandall Stewart 			stcb->asoc.strmout[temp].state = SCTP_STREAM_OPEN;
33507cca1775SRandall Stewart 		}
33517cca1775SRandall Stewart 	} else {
33527cca1775SRandall Stewart 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
33537cca1775SRandall Stewart 			stcb->asoc.strmout[i].state = SCTP_STREAM_OPEN;
33547cca1775SRandall Stewart 		}
33557cca1775SRandall Stewart 	}
33567cca1775SRandall Stewart }
33577cca1775SRandall Stewart 
335884f3b49aSMichael Tuexen struct sctp_stream_reset_request *
3359f8829a4aSRandall Stewart sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3360f8829a4aSRandall Stewart {
3361f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3362a169d6ecSMichael Tuexen 	struct sctp_chunkhdr *ch;
336384f3b49aSMichael Tuexen 	struct sctp_stream_reset_request *r;
3364f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
3365f8829a4aSRandall Stewart 	int len, clen;
3366f8829a4aSRandall Stewart 
3367f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
3368163153c2SMichael Tuexen 	chk = asoc->str_reset;
3369163153c2SMichael Tuexen 	if (TAILQ_EMPTY(&asoc->control_send_queue) ||
3370163153c2SMichael Tuexen 	    (chk == NULL)) {
3371d06c82f1SRandall Stewart 		asoc->stream_reset_outstanding = 0;
3372f8829a4aSRandall Stewart 		return (NULL);
3373f8829a4aSRandall Stewart 	}
3374f8829a4aSRandall Stewart 	if (chk->data == NULL) {
3375f8829a4aSRandall Stewart 		return (NULL);
3376f8829a4aSRandall Stewart 	}
3377163153c2SMichael Tuexen 	if (bchk != NULL) {
3378f8829a4aSRandall Stewart 		/* he wants a copy of the chk pointer */
3379f8829a4aSRandall Stewart 		*bchk = chk;
3380f8829a4aSRandall Stewart 	}
3381f8829a4aSRandall Stewart 	clen = chk->send_size;
3382a169d6ecSMichael Tuexen 	ch = mtod(chk->data, struct sctp_chunkhdr *);
338384f3b49aSMichael Tuexen 	r = (struct sctp_stream_reset_request *)(ch + 1);
3384f8829a4aSRandall Stewart 	if (ntohl(r->request_seq) == seq) {
3385f8829a4aSRandall Stewart 		/* found it */
3386f8829a4aSRandall Stewart 		return (r);
3387f8829a4aSRandall Stewart 	}
3388f8829a4aSRandall Stewart 	len = SCTP_SIZE32(ntohs(r->ph.param_length));
3389f8829a4aSRandall Stewart 	if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3390f8829a4aSRandall Stewart 		/* move to the next one, there can only be a max of two */
339184f3b49aSMichael Tuexen 		r = (struct sctp_stream_reset_request *)((caddr_t)r + len);
3392f8829a4aSRandall Stewart 		if (ntohl(r->request_seq) == seq) {
3393f8829a4aSRandall Stewart 			return (r);
3394f8829a4aSRandall Stewart 		}
3395f8829a4aSRandall Stewart 	}
3396f8829a4aSRandall Stewart 	/* that seq is not here */
3397f8829a4aSRandall Stewart 	return (NULL);
3398f8829a4aSRandall Stewart }
3399f8829a4aSRandall Stewart 
3400f8829a4aSRandall Stewart static void
3401f8829a4aSRandall Stewart sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3402f8829a4aSRandall Stewart {
3403f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3404cd6340caSMichael Tuexen 	struct sctp_tmit_chunk *chk;
3405f8829a4aSRandall Stewart 
3406cd6340caSMichael Tuexen 	asoc = &stcb->asoc;
3407cd6340caSMichael Tuexen 	chk = asoc->str_reset;
3408cd6340caSMichael Tuexen 	if (chk == NULL) {
3409f8829a4aSRandall Stewart 		return;
3410f8829a4aSRandall Stewart 	}
3411cd6340caSMichael Tuexen 	asoc->str_reset = NULL;
3412b7d130beSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb,
34136fb7b4fbSMichael Tuexen 	    NULL, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28);
3414cd6340caSMichael Tuexen 	TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
3415cd6340caSMichael Tuexen 	asoc->ctrl_queue_cnt--;
3416f8829a4aSRandall Stewart 	if (chk->data) {
3417f8829a4aSRandall Stewart 		sctp_m_freem(chk->data);
3418f8829a4aSRandall Stewart 		chk->data = NULL;
3419f8829a4aSRandall Stewart 	}
3420689e6a5fSMichael Tuexen 	sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3421f8829a4aSRandall Stewart }
3422f8829a4aSRandall Stewart 
3423f8829a4aSRandall Stewart static int
3424f8829a4aSRandall Stewart sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3425f8829a4aSRandall Stewart     uint32_t seq, uint32_t action,
342608598d70SRandall Stewart     struct sctp_stream_reset_response *respin)
3427f8829a4aSRandall Stewart {
3428f8829a4aSRandall Stewart 	uint16_t type;
3429f4358911SMichael Tuexen 	int lparam_len;
3430f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3431f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
343284f3b49aSMichael Tuexen 	struct sctp_stream_reset_request *req_param;
343384f3b49aSMichael Tuexen 	struct sctp_stream_reset_out_request *req_out_param;
343484f3b49aSMichael Tuexen 	struct sctp_stream_reset_in_request *req_in_param;
343556f778aaSMichael Tuexen 	uint32_t number_entries;
3436f8829a4aSRandall Stewart 
3437f8829a4aSRandall Stewart 	if (asoc->stream_reset_outstanding == 0) {
3438f8829a4aSRandall Stewart 		/* duplicate */
3439f8829a4aSRandall Stewart 		return (0);
3440f8829a4aSRandall Stewart 	}
3441f8829a4aSRandall Stewart 	if (seq == stcb->asoc.str_reset_seq_out) {
344284f3b49aSMichael Tuexen 		req_param = sctp_find_stream_reset(stcb, seq, &chk);
344384f3b49aSMichael Tuexen 		if (req_param != NULL) {
3444f8829a4aSRandall Stewart 			stcb->asoc.str_reset_seq_out++;
344584f3b49aSMichael Tuexen 			type = ntohs(req_param->ph.param_type);
3446f4358911SMichael Tuexen 			lparam_len = ntohs(req_param->ph.param_length);
3447f8829a4aSRandall Stewart 			if (type == SCTP_STR_RESET_OUT_REQUEST) {
34487cca1775SRandall Stewart 				int no_clear = 0;
34497cca1775SRandall Stewart 
345084f3b49aSMichael Tuexen 				req_out_param = (struct sctp_stream_reset_out_request *)req_param;
3451f4358911SMichael Tuexen 				number_entries = (lparam_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3452f8829a4aSRandall Stewart 				asoc->stream_reset_out_is_outstanding = 0;
3453f8829a4aSRandall Stewart 				if (asoc->stream_reset_outstanding)
3454f8829a4aSRandall Stewart 					asoc->stream_reset_outstanding--;
3455f3ebe64cSMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3456f8829a4aSRandall Stewart 					/* do it */
345784f3b49aSMichael Tuexen 					sctp_reset_out_streams(stcb, number_entries, req_out_param->list_of_streams);
3458d4260646SMichael Tuexen 				} else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
345984f3b49aSMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
34607cca1775SRandall Stewart 				} else if (action == SCTP_STREAM_RESET_RESULT_IN_PROGRESS) {
3461b7b84c0eSMichael Tuexen 					/*
3462b7b84c0eSMichael Tuexen 					 * Set it up so we don't stop
3463b7b84c0eSMichael Tuexen 					 * retransmitting
3464b7b84c0eSMichael Tuexen 					 */
3465a4889f2dSMichael Tuexen 					asoc->stream_reset_outstanding++;
34667cca1775SRandall Stewart 					stcb->asoc.str_reset_seq_out--;
34677cca1775SRandall Stewart 					asoc->stream_reset_out_is_outstanding = 1;
34687cca1775SRandall Stewart 					no_clear = 1;
3469f8829a4aSRandall Stewart 				} else {
347084f3b49aSMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3471f8829a4aSRandall Stewart 				}
34727cca1775SRandall Stewart 				if (no_clear == 0) {
34737cca1775SRandall Stewart 					sctp_reset_clear_pending(stcb, number_entries, req_out_param->list_of_streams);
34747cca1775SRandall Stewart 				}
3475f8829a4aSRandall Stewart 			} else if (type == SCTP_STR_RESET_IN_REQUEST) {
347684f3b49aSMichael Tuexen 				req_in_param = (struct sctp_stream_reset_in_request *)req_param;
3477f4358911SMichael Tuexen 				number_entries = (lparam_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3478f8829a4aSRandall Stewart 				if (asoc->stream_reset_outstanding)
3479f8829a4aSRandall Stewart 					asoc->stream_reset_outstanding--;
3480d4260646SMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3481d4260646SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
348284f3b49aSMichael Tuexen 					    number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3483d4260646SMichael Tuexen 				} else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3484c4e848b7SRandall Stewart 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
348584f3b49aSMichael Tuexen 					    number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3486f8829a4aSRandall Stewart 				}
3487c4e848b7SRandall Stewart 			} else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3488ea44232bSRandall Stewart 				/* Ok we now may have more streams */
3489c4e848b7SRandall Stewart 				int num_stream;
3490c4e848b7SRandall Stewart 
3491c4e848b7SRandall Stewart 				num_stream = stcb->asoc.strm_pending_add_size;
3492c4e848b7SRandall Stewart 				if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3493c4e848b7SRandall Stewart 					/* TSNH */
3494c4e848b7SRandall Stewart 					num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3495c4e848b7SRandall Stewart 				}
3496c4e848b7SRandall Stewart 				stcb->asoc.strm_pending_add_size = 0;
34978aae9493SRandall Stewart 				if (asoc->stream_reset_outstanding)
34988aae9493SRandall Stewart 					asoc->stream_reset_outstanding--;
3499f3ebe64cSMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3500ea44232bSRandall Stewart 					/* Put the new streams into effect */
35017cca1775SRandall Stewart 					int i;
35027cca1775SRandall Stewart 
35037cca1775SRandall Stewart 					for (i = asoc->streamoutcnt; i < (asoc->streamoutcnt + num_stream); i++) {
35047cca1775SRandall Stewart 						asoc->strmout[i].state = SCTP_STREAM_OPEN;
35057cca1775SRandall Stewart 					}
35067cca1775SRandall Stewart 					asoc->streamoutcnt += num_stream;
3507c4e848b7SRandall Stewart 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3508d4260646SMichael Tuexen 				} else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3509d4260646SMichael Tuexen 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3510d4260646SMichael Tuexen 					    SCTP_STREAM_CHANGE_DENIED);
3511ea44232bSRandall Stewart 				} else {
3512c4e848b7SRandall Stewart 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3513d4260646SMichael Tuexen 					    SCTP_STREAM_CHANGE_FAILED);
3514c4e848b7SRandall Stewart 				}
3515c4e848b7SRandall Stewart 			} else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3516c4e848b7SRandall Stewart 				if (asoc->stream_reset_outstanding)
3517c4e848b7SRandall Stewart 					asoc->stream_reset_outstanding--;
3518d4260646SMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3519c4e848b7SRandall Stewart 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3520d4260646SMichael Tuexen 					    SCTP_STREAM_CHANGE_DENIED);
3521d4260646SMichael Tuexen 				} else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3522d4260646SMichael Tuexen 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3523d4260646SMichael Tuexen 					    SCTP_STREAM_CHANGE_FAILED);
3524ea44232bSRandall Stewart 				}
3525f8829a4aSRandall Stewart 			} else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3526f8829a4aSRandall Stewart 				/**
3527f8829a4aSRandall Stewart 				 * a) Adopt the new in tsn.
3528f8829a4aSRandall Stewart 				 * b) reset the map
3529f8829a4aSRandall Stewart 				 * c) Adopt the new out-tsn
3530f8829a4aSRandall Stewart 				 */
3531f8829a4aSRandall Stewart 				struct sctp_stream_reset_response_tsn *resp;
3532f8829a4aSRandall Stewart 				struct sctp_forward_tsn_chunk fwdtsn;
3533f8829a4aSRandall Stewart 				int abort_flag = 0;
3534f8829a4aSRandall Stewart 
3535f8829a4aSRandall Stewart 				if (respin == NULL) {
3536f8829a4aSRandall Stewart 					/* huh ? */
3537f8829a4aSRandall Stewart 					return (0);
3538f8829a4aSRandall Stewart 				}
35396a58f0e9SXin LI 				if (ntohs(respin->ph.param_length) < sizeof(struct sctp_stream_reset_response_tsn)) {
35406a58f0e9SXin LI 					return (0);
35416a58f0e9SXin LI 				}
3542f3ebe64cSMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3543f8829a4aSRandall Stewart 					resp = (struct sctp_stream_reset_response_tsn *)respin;
3544f8829a4aSRandall Stewart 					asoc->stream_reset_outstanding--;
3545f8829a4aSRandall Stewart 					fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3546f8829a4aSRandall Stewart 					fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3547f8829a4aSRandall Stewart 					fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3548671d309cSRandall Stewart 					sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3549f8829a4aSRandall Stewart 					if (abort_flag) {
3550f8829a4aSRandall Stewart 						return (1);
3551f8829a4aSRandall Stewart 					}
3552f8829a4aSRandall Stewart 					stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3553b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3554b3f1ea41SRandall Stewart 						sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3555b3f1ea41SRandall Stewart 					}
35560053ed28SMichael Tuexen 
3557d6af161aSRandall Stewart 					stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3558f8829a4aSRandall Stewart 					stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3559f8829a4aSRandall Stewart 					memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3560830d754dSRandall Stewart 
3561830d754dSRandall Stewart 					stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3562b5c16493SMichael Tuexen 					memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
356377acdc25SRandall Stewart 
3564f8829a4aSRandall Stewart 					stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3565f8829a4aSRandall Stewart 					stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3566f8829a4aSRandall Stewart 
3567f8829a4aSRandall Stewart 					sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3568f8829a4aSRandall Stewart 					sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3569c4e848b7SRandall Stewart 					sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3570d4260646SMichael Tuexen 				} else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3571d4260646SMichael Tuexen 					sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3572d4260646SMichael Tuexen 					    SCTP_ASSOC_RESET_DENIED);
3573c4e848b7SRandall Stewart 				} else {
3574c4e848b7SRandall Stewart 					sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3575d4260646SMichael Tuexen 					    SCTP_ASSOC_RESET_FAILED);
3576f8829a4aSRandall Stewart 				}
3577f8829a4aSRandall Stewart 			}
3578f8829a4aSRandall Stewart 			/* get rid of the request and get the request flags */
3579f8829a4aSRandall Stewart 			if (asoc->stream_reset_outstanding == 0) {
3580f8829a4aSRandall Stewart 				sctp_clean_up_stream_reset(stcb);
3581f8829a4aSRandall Stewart 			}
3582f8829a4aSRandall Stewart 		}
3583f8829a4aSRandall Stewart 	}
35847cca1775SRandall Stewart 	if (asoc->stream_reset_outstanding == 0) {
3585c6168599SRandall Stewart 		sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
35867cca1775SRandall Stewart 	}
3587f8829a4aSRandall Stewart 	return (0);
3588f8829a4aSRandall Stewart }
3589f8829a4aSRandall Stewart 
3590f8829a4aSRandall Stewart static void
3591f8829a4aSRandall Stewart sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3592f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk,
3593671d309cSRandall Stewart     struct sctp_stream_reset_in_request *req, int trunc)
3594f8829a4aSRandall Stewart {
3595f8829a4aSRandall Stewart 	uint32_t seq;
3596f8829a4aSRandall Stewart 	int len, i;
3597f8829a4aSRandall Stewart 	int number_entries;
3598f8829a4aSRandall Stewart 	uint16_t temp;
3599f8829a4aSRandall Stewart 
3600f8829a4aSRandall Stewart 	/*
3601f8829a4aSRandall Stewart 	 * peer wants me to send a str-reset to him for my outgoing seq's if
3602f8829a4aSRandall Stewart 	 * seq_in is right.
3603f8829a4aSRandall Stewart 	 */
3604f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3605f8829a4aSRandall Stewart 
3606f8829a4aSRandall Stewart 	seq = ntohl(req->request_seq);
3607f8829a4aSRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3608671d309cSRandall Stewart 		asoc->last_reset_action[1] = asoc->last_reset_action[0];
36099b2a35b3SMichael Tuexen 		if ((asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ) == 0) {
3610f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3611f3ebe64cSMichael Tuexen 		} else if (trunc) {
3612f3ebe64cSMichael Tuexen 			/* Can't do it, since they exceeded our buffer size  */
3613f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3614671d309cSRandall Stewart 		} else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3615f8829a4aSRandall Stewart 			len = ntohs(req->ph.param_length);
3616f8829a4aSRandall Stewart 			number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
36177cca1775SRandall Stewart 			if (number_entries) {
3618f8829a4aSRandall Stewart 				for (i = 0; i < number_entries; i++) {
3619f8829a4aSRandall Stewart 					temp = ntohs(req->list_of_streams[i]);
36207cca1775SRandall Stewart 					if (temp >= stcb->asoc.streamoutcnt) {
36217cca1775SRandall Stewart 						asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
36227cca1775SRandall Stewart 						goto bad_boy;
36237cca1775SRandall Stewart 					}
3624f8829a4aSRandall Stewart 					req->list_of_streams[i] = temp;
3625f8829a4aSRandall Stewart 				}
36267cca1775SRandall Stewart 				for (i = 0; i < number_entries; i++) {
36277cca1775SRandall Stewart 					if (stcb->asoc.strmout[req->list_of_streams[i]].state == SCTP_STREAM_OPEN) {
36287cca1775SRandall Stewart 						stcb->asoc.strmout[req->list_of_streams[i]].state = SCTP_STREAM_RESET_PENDING;
36297cca1775SRandall Stewart 					}
36307cca1775SRandall Stewart 				}
36317cca1775SRandall Stewart 			} else {
36327cca1775SRandall Stewart 				/* Its all */
36337cca1775SRandall Stewart 				for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
36347cca1775SRandall Stewart 					if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN)
36357cca1775SRandall Stewart 						stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
36367cca1775SRandall Stewart 				}
36377cca1775SRandall Stewart 			}
3638f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3639f8829a4aSRandall Stewart 		} else {
3640f8829a4aSRandall Stewart 			/* Can't do it, since we have sent one out */
3641f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3642f8829a4aSRandall Stewart 		}
36437cca1775SRandall Stewart bad_boy:
3644c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3645f8829a4aSRandall Stewart 		asoc->str_reset_seq_in++;
3646f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 1 == seq) {
3647f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3648f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 2 == seq) {
3649f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3650f8829a4aSRandall Stewart 	} else {
3651f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3652f8829a4aSRandall Stewart 	}
3653c6168599SRandall Stewart 	sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3654f8829a4aSRandall Stewart }
3655f8829a4aSRandall Stewart 
3656f8829a4aSRandall Stewart static int
3657f8829a4aSRandall Stewart sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3658f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk,
3659f8829a4aSRandall Stewart     struct sctp_stream_reset_tsn_request *req)
3660f8829a4aSRandall Stewart {
3661f8829a4aSRandall Stewart 	/* reset all in and out and update the tsn */
3662f8829a4aSRandall Stewart 	/*
3663f8829a4aSRandall Stewart 	 * A) reset my str-seq's on in and out. B) Select a receive next,
3664f8829a4aSRandall Stewart 	 * and set cum-ack to it. Also process this selected number as a
3665f8829a4aSRandall Stewart 	 * fwd-tsn as well. C) set in the response my next sending seq.
3666f8829a4aSRandall Stewart 	 */
3667f8829a4aSRandall Stewart 	struct sctp_forward_tsn_chunk fwdtsn;
3668f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3669f8829a4aSRandall Stewart 	int abort_flag = 0;
3670f8829a4aSRandall Stewart 	uint32_t seq;
3671f8829a4aSRandall Stewart 
3672f8829a4aSRandall Stewart 	seq = ntohl(req->request_seq);
3673f8829a4aSRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3674ce228dabSMichael Tuexen 		asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
36759b2a35b3SMichael Tuexen 		if ((asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ) == 0) {
3676f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3677ce228dabSMichael Tuexen 		} else {
3678f8829a4aSRandall Stewart 			fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3679f8829a4aSRandall Stewart 			fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3680f8829a4aSRandall Stewart 			fwdtsn.ch.chunk_flags = 0;
3681f8829a4aSRandall Stewart 			fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3682671d309cSRandall Stewart 			sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3683f8829a4aSRandall Stewart 			if (abort_flag) {
3684f8829a4aSRandall Stewart 				return (1);
3685f8829a4aSRandall Stewart 			}
3686f3ebe64cSMichael Tuexen 			asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3687b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3688b3f1ea41SRandall Stewart 				sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3689b3f1ea41SRandall Stewart 			}
3690f3ebe64cSMichael Tuexen 			asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3691f3ebe64cSMichael Tuexen 			asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3692f3ebe64cSMichael Tuexen 			memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3693f3ebe64cSMichael Tuexen 			asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3694f3ebe64cSMichael Tuexen 			memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3695f3ebe64cSMichael Tuexen 			atomic_add_int(&asoc->sending_seq, 1);
3696f8829a4aSRandall Stewart 			/* save off historical data for retrans */
3697f3ebe64cSMichael Tuexen 			asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3698f3ebe64cSMichael Tuexen 			asoc->last_sending_seq[0] = asoc->sending_seq;
3699f3ebe64cSMichael Tuexen 			asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3700f3ebe64cSMichael Tuexen 			asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3701f8829a4aSRandall Stewart 			sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3702f8829a4aSRandall Stewart 			sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3703f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3704f3ebe64cSMichael Tuexen 			sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3705ce228dabSMichael Tuexen 		}
3706ce228dabSMichael Tuexen 		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3707ce228dabSMichael Tuexen 		    asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3708f8829a4aSRandall Stewart 		asoc->str_reset_seq_in++;
3709f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 1 == seq) {
3710f8829a4aSRandall Stewart 		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3711f3ebe64cSMichael Tuexen 		    asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3712f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 2 == seq) {
3713f8829a4aSRandall Stewart 		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3714f3ebe64cSMichael Tuexen 		    asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3715f8829a4aSRandall Stewart 	} else {
3716f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3717f8829a4aSRandall Stewart 	}
3718f8829a4aSRandall Stewart 	return (0);
3719f8829a4aSRandall Stewart }
3720f8829a4aSRandall Stewart 
3721f8829a4aSRandall Stewart static void
3722f8829a4aSRandall Stewart sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3723f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk,
3724671d309cSRandall Stewart     struct sctp_stream_reset_out_request *req, int trunc)
3725f8829a4aSRandall Stewart {
3726f8829a4aSRandall Stewart 	uint32_t seq, tsn;
3727f8829a4aSRandall Stewart 	int number_entries, len;
3728f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3729f8829a4aSRandall Stewart 
3730f8829a4aSRandall Stewart 	seq = ntohl(req->request_seq);
3731f8829a4aSRandall Stewart 
3732f8829a4aSRandall Stewart 	/* now if its not a duplicate we process it */
3733f8829a4aSRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3734f8829a4aSRandall Stewart 		len = ntohs(req->ph.param_length);
3735f8829a4aSRandall Stewart 		number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3736f8829a4aSRandall Stewart 		/*
3737f8829a4aSRandall Stewart 		 * the sender is resetting, handle the list issue.. we must
3738f8829a4aSRandall Stewart 		 * a) verify if we can do the reset, if so no problem b) If
3739f8829a4aSRandall Stewart 		 * we can't do the reset we must copy the request. c) queue
3740f8829a4aSRandall Stewart 		 * it, and setup the data in processor to trigger it off
3741f8829a4aSRandall Stewart 		 * when needed and dequeue all the queued data.
3742f8829a4aSRandall Stewart 		 */
3743f8829a4aSRandall Stewart 		tsn = ntohl(req->send_reset_at_tsn);
3744f8829a4aSRandall Stewart 
3745f8829a4aSRandall Stewart 		/* move the reset action back one */
3746f8829a4aSRandall Stewart 		asoc->last_reset_action[1] = asoc->last_reset_action[0];
37479b2a35b3SMichael Tuexen 		if ((asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ) == 0) {
3748f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3749f3ebe64cSMichael Tuexen 		} else if (trunc) {
3750f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
375120b07a4dSMichael Tuexen 		} else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3752f8829a4aSRandall Stewart 			/* we can do it now */
3753f8829a4aSRandall Stewart 			sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3754f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3755f8829a4aSRandall Stewart 		} else {
3756f8829a4aSRandall Stewart 			/*
3757f8829a4aSRandall Stewart 			 * we must queue it up and thus wait for the TSN's
3758f8829a4aSRandall Stewart 			 * to arrive that are at or before tsn
3759f8829a4aSRandall Stewart 			 */
3760f8829a4aSRandall Stewart 			struct sctp_stream_reset_list *liste;
3761f8829a4aSRandall Stewart 			int siz;
3762f8829a4aSRandall Stewart 
3763f8829a4aSRandall Stewart 			siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3764f8829a4aSRandall Stewart 			SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3765207304d4SRandall Stewart 			    siz, SCTP_M_STRESET);
3766f8829a4aSRandall Stewart 			if (liste == NULL) {
3767f8829a4aSRandall Stewart 				/* gak out of memory */
3768f3ebe64cSMichael Tuexen 				asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3769f3ebe64cSMichael Tuexen 				sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3770f8829a4aSRandall Stewart 				return;
3771f8829a4aSRandall Stewart 			}
37727cca1775SRandall Stewart 			liste->seq = seq;
3773f8829a4aSRandall Stewart 			liste->tsn = tsn;
3774f8829a4aSRandall Stewart 			liste->number_entries = number_entries;
3775a169d6ecSMichael Tuexen 			memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t));
3776f8829a4aSRandall Stewart 			TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
37777cca1775SRandall Stewart 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_IN_PROGRESS;
3778f8829a4aSRandall Stewart 		}
3779c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3780f8829a4aSRandall Stewart 		asoc->str_reset_seq_in++;
3781f8829a4aSRandall Stewart 	} else if ((asoc->str_reset_seq_in - 1) == seq) {
3782f8829a4aSRandall Stewart 		/*
3783f8829a4aSRandall Stewart 		 * one seq back, just echo back last action since my
3784f8829a4aSRandall Stewart 		 * response was lost.
3785f8829a4aSRandall Stewart 		 */
3786f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3787f8829a4aSRandall Stewart 	} else if ((asoc->str_reset_seq_in - 2) == seq) {
3788f8829a4aSRandall Stewart 		/*
3789f8829a4aSRandall Stewart 		 * two seq back, just echo back last action since my
3790f8829a4aSRandall Stewart 		 * response was lost.
3791f8829a4aSRandall Stewart 		 */
3792f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3793f8829a4aSRandall Stewart 	} else {
3794f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3795f8829a4aSRandall Stewart 	}
3796f8829a4aSRandall Stewart }
3797f8829a4aSRandall Stewart 
3798ea44232bSRandall Stewart static void
3799ea44232bSRandall Stewart sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3800ea44232bSRandall Stewart     struct sctp_stream_reset_add_strm *str_add)
3801ea44232bSRandall Stewart {
3802ea44232bSRandall Stewart 	/*
3803ea44232bSRandall Stewart 	 * Peer is requesting to add more streams. If its within our
3804ea44232bSRandall Stewart 	 * max-streams we will allow it.
3805ea44232bSRandall Stewart 	 */
3806c4e848b7SRandall Stewart 	uint32_t num_stream, i;
3807ea44232bSRandall Stewart 	uint32_t seq;
38088aae9493SRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
38094a9ef3f8SMichael Tuexen 	struct sctp_queued_to_read *ctl, *nctl;
3810ea44232bSRandall Stewart 
3811ea44232bSRandall Stewart 	/* Get the number. */
3812ea44232bSRandall Stewart 	seq = ntohl(str_add->request_seq);
3813ea44232bSRandall Stewart 	num_stream = ntohs(str_add->number_of_streams);
3814ea44232bSRandall Stewart 	/* Now what would be the new total? */
38158aae9493SRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3816ea44232bSRandall Stewart 		num_stream += stcb->asoc.streamincnt;
3817f3ebe64cSMichael Tuexen 		stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
38189b2a35b3SMichael Tuexen 		if ((asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ) == 0) {
3819f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3820f3ebe64cSMichael Tuexen 		} else if ((num_stream > stcb->asoc.max_inbound_streams) ||
3821c4e848b7SRandall Stewart 		    (num_stream > 0xffff)) {
3822ea44232bSRandall Stewart 			/* We must reject it they ask for to many */
3823ea44232bSRandall Stewart 	denied:
3824f3ebe64cSMichael Tuexen 			stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3825ea44232bSRandall Stewart 		} else {
3826ea44232bSRandall Stewart 			/* Ok, we can do that :-) */
3827ea44232bSRandall Stewart 			struct sctp_stream_in *oldstrm;
3828ea44232bSRandall Stewart 
3829ea44232bSRandall Stewart 			/* save off the old */
3830ea44232bSRandall Stewart 			oldstrm = stcb->asoc.strmin;
3831ea44232bSRandall Stewart 			SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
3832ea44232bSRandall Stewart 			    (num_stream * sizeof(struct sctp_stream_in)),
3833ea44232bSRandall Stewart 			    SCTP_M_STRMI);
3834ea44232bSRandall Stewart 			if (stcb->asoc.strmin == NULL) {
3835ea44232bSRandall Stewart 				stcb->asoc.strmin = oldstrm;
3836ea44232bSRandall Stewart 				goto denied;
3837ea44232bSRandall Stewart 			}
3838ea44232bSRandall Stewart 			/* copy off the old data */
38398aae9493SRandall Stewart 			for (i = 0; i < stcb->asoc.streamincnt; i++) {
38408aae9493SRandall Stewart 				TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
384144249214SRandall Stewart 				TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
384249656eefSMichael Tuexen 				stcb->asoc.strmin[i].sid = i;
384349656eefSMichael Tuexen 				stcb->asoc.strmin[i].last_mid_delivered = oldstrm[i].last_mid_delivered;
38448aae9493SRandall Stewart 				stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
384544249214SRandall Stewart 				stcb->asoc.strmin[i].pd_api_started = oldstrm[i].pd_api_started;
38468aae9493SRandall Stewart 				/* now anything on those queues? */
384744249214SRandall Stewart 				TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next_instrm, nctl) {
384844249214SRandall Stewart 					TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next_instrm);
384944249214SRandall Stewart 					TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next_instrm);
385044249214SRandall Stewart 				}
385144249214SRandall Stewart 				TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].uno_inqueue, next_instrm, nctl) {
385244249214SRandall Stewart 					TAILQ_REMOVE(&oldstrm[i].uno_inqueue, ctl, next_instrm);
385344249214SRandall Stewart 					TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].uno_inqueue, ctl, next_instrm);
38548aae9493SRandall Stewart 				}
38558aae9493SRandall Stewart 			}
3856ea44232bSRandall Stewart 			/* Init the new streams */
3857ea44232bSRandall Stewart 			for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
3858ea44232bSRandall Stewart 				TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
385944249214SRandall Stewart 				TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
386049656eefSMichael Tuexen 				stcb->asoc.strmin[i].sid = i;
386149656eefSMichael Tuexen 				stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
386244249214SRandall Stewart 				stcb->asoc.strmin[i].pd_api_started = 0;
3863ea44232bSRandall Stewart 				stcb->asoc.strmin[i].delivery_started = 0;
3864ea44232bSRandall Stewart 			}
3865ea44232bSRandall Stewart 			SCTP_FREE(oldstrm, SCTP_M_STRMI);
3866ea44232bSRandall Stewart 			/* update the size */
3867ea44232bSRandall Stewart 			stcb->asoc.streamincnt = num_stream;
3868f3ebe64cSMichael Tuexen 			stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3869c4e848b7SRandall Stewart 			sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3870ea44232bSRandall Stewart 		}
3871c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3872c4e848b7SRandall Stewart 		asoc->str_reset_seq_in++;
38738aae9493SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 1) == seq) {
38748aae9493SRandall Stewart 		/*
38758aae9493SRandall Stewart 		 * one seq back, just echo back last action since my
38768aae9493SRandall Stewart 		 * response was lost.
38778aae9493SRandall Stewart 		 */
38788aae9493SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
38798aae9493SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 2) == seq) {
38808aae9493SRandall Stewart 		/*
38818aae9493SRandall Stewart 		 * two seq back, just echo back last action since my
38828aae9493SRandall Stewart 		 * response was lost.
38838aae9493SRandall Stewart 		 */
38848aae9493SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
38858aae9493SRandall Stewart 	} else {
3886f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
38878aae9493SRandall Stewart 	}
3888ea44232bSRandall Stewart }
3889ea44232bSRandall Stewart 
3890c4e848b7SRandall Stewart static void
3891c4e848b7SRandall Stewart sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3892c4e848b7SRandall Stewart     struct sctp_stream_reset_add_strm *str_add)
3893c4e848b7SRandall Stewart {
3894c4e848b7SRandall Stewart 	/*
3895c4e848b7SRandall Stewart 	 * Peer is requesting to add more streams. If its within our
3896c4e848b7SRandall Stewart 	 * max-streams we will allow it.
3897c4e848b7SRandall Stewart 	 */
3898c4e848b7SRandall Stewart 	uint16_t num_stream;
3899c4e848b7SRandall Stewart 	uint32_t seq;
3900c4e848b7SRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3901c4e848b7SRandall Stewart 
3902c4e848b7SRandall Stewart 	/* Get the number. */
3903c4e848b7SRandall Stewart 	seq = ntohl(str_add->request_seq);
3904c4e848b7SRandall Stewart 	num_stream = ntohs(str_add->number_of_streams);
3905c4e848b7SRandall Stewart 	/* Now what would be the new total? */
3906c4e848b7SRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3907c4e848b7SRandall Stewart 		stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
39089b2a35b3SMichael Tuexen 		if ((asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ) == 0) {
3909f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3910f3ebe64cSMichael Tuexen 		} else if (stcb->asoc.stream_reset_outstanding) {
3911f3ebe64cSMichael Tuexen 			/* We must reject it we have something pending */
3912f3ebe64cSMichael Tuexen 			stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3913c4e848b7SRandall Stewart 		} else {
3914c4e848b7SRandall Stewart 			/* Ok, we can do that :-) */
3915c4e848b7SRandall Stewart 			int mychk;
3916c4e848b7SRandall Stewart 
3917c4e848b7SRandall Stewart 			mychk = stcb->asoc.streamoutcnt;
3918c4e848b7SRandall Stewart 			mychk += num_stream;
3919c4e848b7SRandall Stewart 			if (mychk < 0x10000) {
3920f3ebe64cSMichael Tuexen 				stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
39217cca1775SRandall Stewart 				if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 1, num_stream, 0, 1)) {
3922f3ebe64cSMichael Tuexen 					stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3923c4e848b7SRandall Stewart 				}
3924c4e848b7SRandall Stewart 			} else {
3925f3ebe64cSMichael Tuexen 				stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3926c4e848b7SRandall Stewart 			}
3927c4e848b7SRandall Stewart 		}
3928c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
3929c4e848b7SRandall Stewart 		asoc->str_reset_seq_in++;
3930c4e848b7SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 1) == seq) {
3931c4e848b7SRandall Stewart 		/*
3932c4e848b7SRandall Stewart 		 * one seq back, just echo back last action since my
3933c4e848b7SRandall Stewart 		 * response was lost.
3934c4e848b7SRandall Stewart 		 */
3935c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3936c4e848b7SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 2) == seq) {
3937c4e848b7SRandall Stewart 		/*
3938c4e848b7SRandall Stewart 		 * two seq back, just echo back last action since my
3939c4e848b7SRandall Stewart 		 * response was lost.
3940c4e848b7SRandall Stewart 		 */
3941c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3942c4e848b7SRandall Stewart 	} else {
3943f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3944c4e848b7SRandall Stewart 	}
3945c4e848b7SRandall Stewart }
3946c4e848b7SRandall Stewart 
3947671d309cSRandall Stewart #ifdef __GNUC__
3948671d309cSRandall Stewart __attribute__((noinline))
3949671d309cSRandall Stewart #endif
3950f8829a4aSRandall Stewart static int
3951671d309cSRandall Stewart sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3952a169d6ecSMichael Tuexen     struct sctp_chunkhdr *ch_req)
3953f8829a4aSRandall Stewart {
39546a58f0e9SXin LI 	uint16_t remaining_length, param_len, ptype;
3955671d309cSRandall Stewart 	struct sctp_paramhdr pstore;
3956671d309cSRandall Stewart 	uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
3957c4e848b7SRandall Stewart 	uint32_t seq = 0;
3958f8829a4aSRandall Stewart 	int num_req = 0;
3959671d309cSRandall Stewart 	int trunc = 0;
3960f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
3961f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
3962f8829a4aSRandall Stewart 	struct sctp_paramhdr *ph;
3963f42a358aSRandall Stewart 	int ret_code = 0;
3964f42a358aSRandall Stewart 	int num_param = 0;
3965f8829a4aSRandall Stewart 
3966f8829a4aSRandall Stewart 	/* now it may be a reset or a reset-response */
39676a58f0e9SXin LI 	remaining_length = ntohs(ch_req->chunk_length) - sizeof(struct sctp_chunkhdr);
3968f8829a4aSRandall Stewart 
3969f8829a4aSRandall Stewart 	/* setup for adding the response */
3970f8829a4aSRandall Stewart 	sctp_alloc_a_chunk(stcb, chk);
3971f8829a4aSRandall Stewart 	if (chk == NULL) {
3972f8829a4aSRandall Stewart 		return (ret_code);
3973f8829a4aSRandall Stewart 	}
3974e03159eaSMichael Tuexen 	chk->copy_by_ref = 0;
3975f8829a4aSRandall Stewart 	chk->rec.chunk_id.id = SCTP_STREAM_RESET;
3976d06c82f1SRandall Stewart 	chk->rec.chunk_id.can_take_data = 0;
3977e03159eaSMichael Tuexen 	chk->flags = 0;
3978f8829a4aSRandall Stewart 	chk->asoc = &stcb->asoc;
3979f8829a4aSRandall Stewart 	chk->no_fr_allowed = 0;
3980f8829a4aSRandall Stewart 	chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
398144b7479bSRandall Stewart 	chk->book_size_scale = 0;
3982eb1b1807SGleb Smirnoff 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
3983f8829a4aSRandall Stewart 	if (chk->data == NULL) {
3984f8829a4aSRandall Stewart strres_nochunk:
3985f8829a4aSRandall Stewart 		if (chk->data) {
3986f8829a4aSRandall Stewart 			sctp_m_freem(chk->data);
3987f8829a4aSRandall Stewart 			chk->data = NULL;
3988f8829a4aSRandall Stewart 		}
3989689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3990f8829a4aSRandall Stewart 		return (ret_code);
3991f8829a4aSRandall Stewart 	}
3992139bc87fSRandall Stewart 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
3993f8829a4aSRandall Stewart 
3994f8829a4aSRandall Stewart 	/* setup chunk parameters */
3995f8829a4aSRandall Stewart 	chk->sent = SCTP_DATAGRAM_UNSENT;
3996f8829a4aSRandall Stewart 	chk->snd_count = 0;
3997ca85e948SMichael Tuexen 	chk->whoTo = NULL;
3998f8829a4aSRandall Stewart 
3999f8829a4aSRandall Stewart 	ch = mtod(chk->data, struct sctp_chunkhdr *);
4000f8829a4aSRandall Stewart 	ch->chunk_type = SCTP_STREAM_RESET;
4001f8829a4aSRandall Stewart 	ch->chunk_flags = 0;
4002f8829a4aSRandall Stewart 	ch->chunk_length = htons(chk->send_size);
4003139bc87fSRandall Stewart 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4004671d309cSRandall Stewart 	offset += sizeof(struct sctp_chunkhdr);
40056a58f0e9SXin LI 	while (remaining_length >= sizeof(struct sctp_paramhdr)) {
4006671d309cSRandall Stewart 		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *)&pstore);
40076a58f0e9SXin LI 		if (ph == NULL) {
40086a58f0e9SXin LI 			/* TSNH */
4009f8829a4aSRandall Stewart 			break;
4010f8829a4aSRandall Stewart 		}
40116a58f0e9SXin LI 		param_len = ntohs(ph->param_length);
40126a58f0e9SXin LI 		if ((param_len > remaining_length) ||
40136a58f0e9SXin LI 		    (param_len < (sizeof(struct sctp_paramhdr) + sizeof(uint32_t)))) {
40146a58f0e9SXin LI 			/* bad parameter length */
40156a58f0e9SXin LI 			break;
40166a58f0e9SXin LI 		}
40176a58f0e9SXin LI 		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, sizeof(cstore)),
4018671d309cSRandall Stewart 		    (uint8_t *)&cstore);
40196a58f0e9SXin LI 		if (ph == NULL) {
40206a58f0e9SXin LI 			/* TSNH */
40216a58f0e9SXin LI 			break;
40226a58f0e9SXin LI 		}
4023f8829a4aSRandall Stewart 		ptype = ntohs(ph->param_type);
4024f8829a4aSRandall Stewart 		num_param++;
40256a58f0e9SXin LI 		if (param_len > sizeof(cstore)) {
4026671d309cSRandall Stewart 			trunc = 1;
4027671d309cSRandall Stewart 		} else {
4028671d309cSRandall Stewart 			trunc = 0;
4029671d309cSRandall Stewart 		}
4030f8829a4aSRandall Stewart 		if (num_param > SCTP_MAX_RESET_PARAMS) {
4031f8829a4aSRandall Stewart 			/* hit the max of parameters already sorry.. */
4032f8829a4aSRandall Stewart 			break;
4033f8829a4aSRandall Stewart 		}
4034f8829a4aSRandall Stewart 		if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4035f8829a4aSRandall Stewart 			struct sctp_stream_reset_out_request *req_out;
4036f8829a4aSRandall Stewart 
40376a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_out_request)) {
40386a58f0e9SXin LI 				break;
40396a58f0e9SXin LI 			}
4040f8829a4aSRandall Stewart 			req_out = (struct sctp_stream_reset_out_request *)ph;
4041f8829a4aSRandall Stewart 			num_req++;
4042f8829a4aSRandall Stewart 			if (stcb->asoc.stream_reset_outstanding) {
4043f8829a4aSRandall Stewart 				seq = ntohl(req_out->response_seq);
4044f8829a4aSRandall Stewart 				if (seq == stcb->asoc.str_reset_seq_out) {
4045f8829a4aSRandall Stewart 					/* implicit ack */
4046f3ebe64cSMichael Tuexen 					(void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4047f8829a4aSRandall Stewart 				}
4048f8829a4aSRandall Stewart 			}
4049671d309cSRandall Stewart 			sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4050c4e848b7SRandall Stewart 		} else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4051ea44232bSRandall Stewart 			struct sctp_stream_reset_add_strm *str_add;
4052ea44232bSRandall Stewart 
40536a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
40546a58f0e9SXin LI 				break;
40556a58f0e9SXin LI 			}
4056ea44232bSRandall Stewart 			str_add = (struct sctp_stream_reset_add_strm *)ph;
4057ea44232bSRandall Stewart 			num_req++;
4058ea44232bSRandall Stewart 			sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4059c4e848b7SRandall Stewart 		} else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4060c4e848b7SRandall Stewart 			struct sctp_stream_reset_add_strm *str_add;
4061c4e848b7SRandall Stewart 
40626a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
40636a58f0e9SXin LI 				break;
40646a58f0e9SXin LI 			}
4065c4e848b7SRandall Stewart 			str_add = (struct sctp_stream_reset_add_strm *)ph;
4066c4e848b7SRandall Stewart 			num_req++;
4067c4e848b7SRandall Stewart 			sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4068f8829a4aSRandall Stewart 		} else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4069f8829a4aSRandall Stewart 			struct sctp_stream_reset_in_request *req_in;
4070f8829a4aSRandall Stewart 
4071f8829a4aSRandall Stewart 			num_req++;
4072f8829a4aSRandall Stewart 			req_in = (struct sctp_stream_reset_in_request *)ph;
4073671d309cSRandall Stewart 			sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4074f8829a4aSRandall Stewart 		} else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4075f8829a4aSRandall Stewart 			struct sctp_stream_reset_tsn_request *req_tsn;
4076f8829a4aSRandall Stewart 
4077f8829a4aSRandall Stewart 			num_req++;
4078f8829a4aSRandall Stewart 			req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4079f8829a4aSRandall Stewart 			if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4080f8829a4aSRandall Stewart 				ret_code = 1;
4081f8829a4aSRandall Stewart 				goto strres_nochunk;
4082f8829a4aSRandall Stewart 			}
4083f8829a4aSRandall Stewart 			/* no more */
4084f8829a4aSRandall Stewart 			break;
4085f8829a4aSRandall Stewart 		} else if (ptype == SCTP_STR_RESET_RESPONSE) {
4086f8829a4aSRandall Stewart 			struct sctp_stream_reset_response *resp;
4087f8829a4aSRandall Stewart 			uint32_t result;
4088f8829a4aSRandall Stewart 
40896a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_response)) {
40906a58f0e9SXin LI 				break;
40916a58f0e9SXin LI 			}
4092f8829a4aSRandall Stewart 			resp = (struct sctp_stream_reset_response *)ph;
4093f8829a4aSRandall Stewart 			seq = ntohl(resp->response_seq);
4094f8829a4aSRandall Stewart 			result = ntohl(resp->result);
4095f8829a4aSRandall Stewart 			if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4096f8829a4aSRandall Stewart 				ret_code = 1;
4097f8829a4aSRandall Stewart 				goto strres_nochunk;
4098f8829a4aSRandall Stewart 			}
4099f8829a4aSRandall Stewart 		} else {
4100f8829a4aSRandall Stewart 			break;
4101f8829a4aSRandall Stewart 		}
4102671d309cSRandall Stewart 		offset += SCTP_SIZE32(param_len);
41036a58f0e9SXin LI 		if (remaining_length >= SCTP_SIZE32(param_len)) {
41046a58f0e9SXin LI 			remaining_length -= SCTP_SIZE32(param_len);
41056a58f0e9SXin LI 		} else {
41066a58f0e9SXin LI 			remaining_length = 0;
41076a58f0e9SXin LI 		}
4108f8829a4aSRandall Stewart 	}
4109f8829a4aSRandall Stewart 	if (num_req == 0) {
4110f8829a4aSRandall Stewart 		/* we have no response free the stuff */
4111f8829a4aSRandall Stewart 		goto strres_nochunk;
4112f8829a4aSRandall Stewart 	}
4113f8829a4aSRandall Stewart 	/* ok we have a chunk to link in */
4114f8829a4aSRandall Stewart 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4115f8829a4aSRandall Stewart 	    chk,
4116f8829a4aSRandall Stewart 	    sctp_next);
4117f8829a4aSRandall Stewart 	stcb->asoc.ctrl_queue_cnt++;
4118f8829a4aSRandall Stewart 	return (ret_code);
4119f8829a4aSRandall Stewart }
4120f8829a4aSRandall Stewart 
4121f8829a4aSRandall Stewart /*
4122f8829a4aSRandall Stewart  * Handle a router or endpoints report of a packet loss, there are two ways
4123f8829a4aSRandall Stewart  * to handle this, either we get the whole packet and must disect it
4124f8829a4aSRandall Stewart  * ourselves (possibly with truncation and or corruption) or it is a summary
4125e7e65008SMichael Tuexen  * from a middle box that did the disecting for us.
4126f8829a4aSRandall Stewart  */
4127f8829a4aSRandall Stewart static void
4128f8829a4aSRandall Stewart sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4129458303daSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4130f8829a4aSRandall Stewart {
4131f8829a4aSRandall Stewart 	struct sctp_chunk_desc desc;
413232df1c9eSMichael Tuexen 	struct sctp_chunkhdr *chk_hdr;
413332df1c9eSMichael Tuexen 	struct sctp_data_chunk *data_chunk;
413432df1c9eSMichael Tuexen 	struct sctp_idata_chunk *idata_chunk;
413532df1c9eSMichael Tuexen 	uint32_t bottle_bw, on_queue;
413632df1c9eSMichael Tuexen 	uint32_t offset, chk_len;
413732df1c9eSMichael Tuexen 	uint16_t pktdrp_len;
413832df1c9eSMichael Tuexen 	uint8_t pktdrp_flags;
4139f8829a4aSRandall Stewart 
414032df1c9eSMichael Tuexen 	KASSERT(sizeof(struct sctp_pktdrop_chunk) <= limit,
414132df1c9eSMichael Tuexen 	    ("PKTDROP chunk too small"));
414232df1c9eSMichael Tuexen 	pktdrp_flags = cp->ch.chunk_flags;
414332df1c9eSMichael Tuexen 	pktdrp_len = ntohs(cp->ch.chunk_length);
414432df1c9eSMichael Tuexen 	KASSERT(limit <= pktdrp_len, ("Inconsistent limit"));
414532df1c9eSMichael Tuexen 	if (pktdrp_flags & SCTP_PACKET_TRUNCATED) {
41466176f9d6SMichael Tuexen 		if (ntohs(cp->trunc_len) <= pktdrp_len - sizeof(struct sctp_pktdrop_chunk)) {
414732df1c9eSMichael Tuexen 			/* The peer plays games with us. */
414832df1c9eSMichael Tuexen 			return;
414932df1c9eSMichael Tuexen 		}
415032df1c9eSMichael Tuexen 	}
415132df1c9eSMichael Tuexen 	limit -= sizeof(struct sctp_pktdrop_chunk);
415232df1c9eSMichael Tuexen 	offset = 0;
415332df1c9eSMichael Tuexen 	if (offset == limit) {
415432df1c9eSMichael Tuexen 		if (pktdrp_flags & SCTP_FROM_MIDDLE_BOX) {
4155f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_pdrpbwrpt);
415632df1c9eSMichael Tuexen 		}
415732df1c9eSMichael Tuexen 	} else if (offset + sizeof(struct sctphdr) > limit) {
415832df1c9eSMichael Tuexen 		/* Only a partial SCTP common header. */
415932df1c9eSMichael Tuexen 		SCTP_STAT_INCR(sctps_pdrpcrupt);
416032df1c9eSMichael Tuexen 		offset = limit;
4161f8829a4aSRandall Stewart 	} else {
416232df1c9eSMichael Tuexen 		/* XXX: Check embedded SCTP common header. */
416332df1c9eSMichael Tuexen 		offset += sizeof(struct sctphdr);
4164f8829a4aSRandall Stewart 	}
416532df1c9eSMichael Tuexen 	/* Now parse through the chunks themselves. */
416632df1c9eSMichael Tuexen 	while (offset < limit) {
416732df1c9eSMichael Tuexen 		if (offset + sizeof(struct sctp_chunkhdr) > limit) {
416832df1c9eSMichael Tuexen 			SCTP_STAT_INCR(sctps_pdrpcrupt);
416932df1c9eSMichael Tuexen 			break;
4170458303daSRandall Stewart 		}
417132df1c9eSMichael Tuexen 		chk_hdr = (struct sctp_chunkhdr *)(cp->data + offset);
417232df1c9eSMichael Tuexen 		desc.chunk_type = chk_hdr->chunk_type;
4173f8829a4aSRandall Stewart 		/* get amount we need to move */
417432df1c9eSMichael Tuexen 		chk_len = (uint32_t)ntohs(chk_hdr->chunk_length);
417532df1c9eSMichael Tuexen 		if (chk_len < sizeof(struct sctp_chunkhdr)) {
417632df1c9eSMichael Tuexen 			/* Someone is lying... */
4177f8829a4aSRandall Stewart 			break;
4178f8829a4aSRandall Stewart 		}
4179f8829a4aSRandall Stewart 		if (desc.chunk_type == SCTP_DATA) {
418032df1c9eSMichael Tuexen 			if (stcb->asoc.idata_supported) {
418132df1c9eSMichael Tuexen 				/* Some is playing games with us. */
418232df1c9eSMichael Tuexen 				break;
4183f8829a4aSRandall Stewart 			}
418432df1c9eSMichael Tuexen 			if (chk_len <= sizeof(struct sctp_data_chunk)) {
418532df1c9eSMichael Tuexen 				/* Some is playing games with us. */
418632df1c9eSMichael Tuexen 				break;
418732df1c9eSMichael Tuexen 			}
418832df1c9eSMichael Tuexen 			if (chk_len < sizeof(struct sctp_data_chunk) + SCTP_NUM_DB_TO_VERIFY) {
418932df1c9eSMichael Tuexen 				/*
419032df1c9eSMichael Tuexen 				 * Not enough data bytes available in the
419132df1c9eSMichael Tuexen 				 * chunk.
419232df1c9eSMichael Tuexen 				 */
4193f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpnedat);
419432df1c9eSMichael Tuexen 				goto next_chunk;
419532df1c9eSMichael Tuexen 			}
419632df1c9eSMichael Tuexen 			if (offset + sizeof(struct sctp_data_chunk) + SCTP_NUM_DB_TO_VERIFY > limit) {
419732df1c9eSMichael Tuexen 				/* Not enough data in buffer. */
4198f8829a4aSRandall Stewart 				break;
4199f8829a4aSRandall Stewart 			}
420032df1c9eSMichael Tuexen 			data_chunk = (struct sctp_data_chunk *)(cp->data + offset);
420132df1c9eSMichael Tuexen 			memcpy(desc.data_bytes, data_chunk + 1, SCTP_NUM_DB_TO_VERIFY);
420232df1c9eSMichael Tuexen 			desc.tsn_ifany = data_chunk->dp.tsn;
420332df1c9eSMichael Tuexen 			if (pktdrp_flags & SCTP_FROM_MIDDLE_BOX) {
420432df1c9eSMichael Tuexen 				SCTP_STAT_INCR(sctps_pdrpmbda);
420532df1c9eSMichael Tuexen 			}
420632df1c9eSMichael Tuexen 		} else if (desc.chunk_type == SCTP_IDATA) {
420732df1c9eSMichael Tuexen 			if (!stcb->asoc.idata_supported) {
420832df1c9eSMichael Tuexen 				/* Some is playing games with us. */
420932df1c9eSMichael Tuexen 				break;
421032df1c9eSMichael Tuexen 			}
421132df1c9eSMichael Tuexen 			if (chk_len <= sizeof(struct sctp_idata_chunk)) {
421232df1c9eSMichael Tuexen 				/* Some is playing games with us. */
421332df1c9eSMichael Tuexen 				break;
421432df1c9eSMichael Tuexen 			}
421532df1c9eSMichael Tuexen 			if (chk_len < sizeof(struct sctp_idata_chunk) + SCTP_NUM_DB_TO_VERIFY) {
421632df1c9eSMichael Tuexen 				/*
421732df1c9eSMichael Tuexen 				 * Not enough data bytes available in the
421832df1c9eSMichael Tuexen 				 * chunk.
421932df1c9eSMichael Tuexen 				 */
422032df1c9eSMichael Tuexen 				SCTP_STAT_INCR(sctps_pdrpnedat);
422132df1c9eSMichael Tuexen 				goto next_chunk;
422232df1c9eSMichael Tuexen 			}
422332df1c9eSMichael Tuexen 			if (offset + sizeof(struct sctp_idata_chunk) + SCTP_NUM_DB_TO_VERIFY > limit) {
422432df1c9eSMichael Tuexen 				/* Not enough data in buffer. */
422532df1c9eSMichael Tuexen 				break;
422632df1c9eSMichael Tuexen 			}
422732df1c9eSMichael Tuexen 			idata_chunk = (struct sctp_idata_chunk *)(cp->data + offset);
422832df1c9eSMichael Tuexen 			memcpy(desc.data_bytes, idata_chunk + 1, SCTP_NUM_DB_TO_VERIFY);
422932df1c9eSMichael Tuexen 			desc.tsn_ifany = idata_chunk->dp.tsn;
423032df1c9eSMichael Tuexen 			if (pktdrp_flags & SCTP_FROM_MIDDLE_BOX) {
423132df1c9eSMichael Tuexen 				SCTP_STAT_INCR(sctps_pdrpmbda);
423232df1c9eSMichael Tuexen 			}
4233f8829a4aSRandall Stewart 		} else {
423432df1c9eSMichael Tuexen 			if (pktdrp_flags & SCTP_FROM_MIDDLE_BOX) {
4235f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpmbct);
4236f8829a4aSRandall Stewart 			}
423732df1c9eSMichael Tuexen 		}
423832df1c9eSMichael Tuexen 		if (process_chunk_drop(stcb, &desc, net, pktdrp_flags)) {
4239f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_pdrppdbrk);
4240f8829a4aSRandall Stewart 			break;
4241f8829a4aSRandall Stewart 		}
424232df1c9eSMichael Tuexen next_chunk:
424332df1c9eSMichael Tuexen 		offset += SCTP_SIZE32(chk_len);
4244f8829a4aSRandall Stewart 	}
4245f8829a4aSRandall Stewart 	/* Now update any rwnd --- possibly */
424632df1c9eSMichael Tuexen 	if ((pktdrp_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4247f8829a4aSRandall Stewart 		/* From a peer, we get a rwnd report */
4248f8829a4aSRandall Stewart 		uint32_t a_rwnd;
4249f8829a4aSRandall Stewart 
4250f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_pdrpfehos);
4251f8829a4aSRandall Stewart 
4252f8829a4aSRandall Stewart 		bottle_bw = ntohl(cp->bottle_bw);
4253f8829a4aSRandall Stewart 		on_queue = ntohl(cp->current_onq);
4254f8829a4aSRandall Stewart 		if (bottle_bw && on_queue) {
4255f8829a4aSRandall Stewart 			/* a rwnd report is in here */
4256f8829a4aSRandall Stewart 			if (bottle_bw > on_queue)
4257f8829a4aSRandall Stewart 				a_rwnd = bottle_bw - on_queue;
4258f8829a4aSRandall Stewart 			else
4259f8829a4aSRandall Stewart 				a_rwnd = 0;
4260f8829a4aSRandall Stewart 
4261f8829a4aSRandall Stewart 			if (a_rwnd == 0)
4262f8829a4aSRandall Stewart 				stcb->asoc.peers_rwnd = 0;
4263f8829a4aSRandall Stewart 			else {
4264f8829a4aSRandall Stewart 				if (a_rwnd > stcb->asoc.total_flight) {
4265f8829a4aSRandall Stewart 					stcb->asoc.peers_rwnd =
4266f8829a4aSRandall Stewart 					    a_rwnd - stcb->asoc.total_flight;
4267f8829a4aSRandall Stewart 				} else {
4268f8829a4aSRandall Stewart 					stcb->asoc.peers_rwnd = 0;
4269f8829a4aSRandall Stewart 				}
4270f8829a4aSRandall Stewart 				if (stcb->asoc.peers_rwnd <
4271f8829a4aSRandall Stewart 				    stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4272f8829a4aSRandall Stewart 					/* SWS sender side engages */
4273f8829a4aSRandall Stewart 					stcb->asoc.peers_rwnd = 0;
4274f8829a4aSRandall Stewart 				}
4275f8829a4aSRandall Stewart 			}
4276f8829a4aSRandall Stewart 		}
4277f8829a4aSRandall Stewart 	} else {
4278f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_pdrpfmbox);
4279f8829a4aSRandall Stewart 	}
4280f8829a4aSRandall Stewart 
4281f8829a4aSRandall Stewart 	/* now middle boxes in sat networks get a cwnd bump */
428232df1c9eSMichael Tuexen 	if ((pktdrp_flags & SCTP_FROM_MIDDLE_BOX) &&
4283f8829a4aSRandall Stewart 	    (stcb->asoc.sat_t3_loss_recovery == 0) &&
4284f8829a4aSRandall Stewart 	    (stcb->asoc.sat_network)) {
4285f8829a4aSRandall Stewart 		/*
4286cd0a4ff6SPedro F. Giffuni 		 * This is debatable but for sat networks it makes sense
4287f8829a4aSRandall Stewart 		 * Note if a T3 timer has went off, we will prohibit any
4288f8829a4aSRandall Stewart 		 * changes to cwnd until we exit the t3 loss recovery.
4289f8829a4aSRandall Stewart 		 */
4290b54d3a6cSRandall Stewart 		stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4291b54d3a6cSRandall Stewart 		    net, cp, &bottle_bw, &on_queue);
4292f8829a4aSRandall Stewart 	}
4293f8829a4aSRandall Stewart }
4294f8829a4aSRandall Stewart 
4295f8829a4aSRandall Stewart /*
4296f8829a4aSRandall Stewart  * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4297f8829a4aSRandall Stewart  * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4298f8829a4aSRandall Stewart  * offset: offset into the mbuf chain to first chunkhdr - length: is the
4299f8829a4aSRandall Stewart  * length of the complete packet outputs: - length: modified to remaining
4300f8829a4aSRandall Stewart  * length after control processing - netp: modified to new sctp_nets after
4301f8829a4aSRandall Stewart  * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4302f8829a4aSRandall Stewart  * bad packet,...) otherwise return the tcb for this packet
4303f8829a4aSRandall Stewart  */
43043c6f3536SRandall Stewart #ifdef __GNUC__
43053c6f3536SRandall Stewart __attribute__((noinline))
43063c6f3536SRandall Stewart #endif
4307f8829a4aSRandall Stewart static struct sctp_tcb *
4308f8829a4aSRandall Stewart sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4309b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
4310f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
431117205eccSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4312d089f9b9SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4313c54a18d2SRandall Stewart     uint32_t vrf_id, uint16_t port)
4314f8829a4aSRandall Stewart {
4315f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4316ff1ffd74SMichael Tuexen 	struct mbuf *op_err;
4317ff1ffd74SMichael Tuexen 	char msg[SCTP_DIAG_INFO_LEN];
4318f8829a4aSRandall Stewart 	uint32_t vtag_in;
4319f8829a4aSRandall Stewart 	int num_chunks = 0;	/* number of control chunks processed */
4320d0f6ab79SMichael Tuexen 	uint32_t chk_length, contiguous;
4321f8829a4aSRandall Stewart 	int ret;
4322bff64a4dSRandall Stewart 	int abort_no_unlock = 0;
4323899288aeSRandall Stewart 	int ecne_seen = 0;
43249de7354bSMichael Tuexen 	int abort_flag;
4325f8829a4aSRandall Stewart 
4326f8829a4aSRandall Stewart 	/*
4327f8829a4aSRandall Stewart 	 * How big should this be, and should it be alloc'd? Lets try the
4328f8829a4aSRandall Stewart 	 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4329f8829a4aSRandall Stewart 	 * until we get into jumbo grams and such..
4330f8829a4aSRandall Stewart 	 */
4331f42a358aSRandall Stewart 	uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4332f8829a4aSRandall Stewart 	int got_auth = 0;
4333f8829a4aSRandall Stewart 	uint32_t auth_offset = 0, auth_len = 0;
4334f8829a4aSRandall Stewart 	int auth_skipped = 0;
43352afb3e84SRandall Stewart 	int asconf_cnt = 0;
4336ceaad40aSRandall Stewart 
4337ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4338dd294dceSMichael Tuexen 	    iphlen, *offset, length, (void *)stcb);
4339f8829a4aSRandall Stewart 
434080a2d140SMichael Tuexen 	if (stcb) {
434180a2d140SMichael Tuexen 		SCTP_TCB_LOCK_ASSERT(stcb);
434280a2d140SMichael Tuexen 	}
4343f8829a4aSRandall Stewart 	/* validate chunk header length... */
4344f8829a4aSRandall Stewart 	if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4345d61a0ae0SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4346d61a0ae0SRandall Stewart 		    ntohs(ch->chunk_length));
43473ed8d364SMichael Tuexen 		*offset = length;
434880a2d140SMichael Tuexen 		return (stcb);
4349f8829a4aSRandall Stewart 	}
4350f8829a4aSRandall Stewart 	/*
4351f8829a4aSRandall Stewart 	 * validate the verification tag
4352f8829a4aSRandall Stewart 	 */
4353f8829a4aSRandall Stewart 	vtag_in = ntohl(sh->v_tag);
4354f8829a4aSRandall Stewart 
4355f8829a4aSRandall Stewart 	if (ch->chunk_type == SCTP_INITIATION) {
4356d61a0ae0SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4357d61a0ae0SRandall Stewart 		    ntohs(ch->chunk_length), vtag_in);
4358f8829a4aSRandall Stewart 		if (vtag_in != 0) {
4359f8829a4aSRandall Stewart 			/* protocol error- silently discard... */
4360f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_badvtag);
436180a2d140SMichael Tuexen 			if (stcb != NULL) {
436280a2d140SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
43636e55db54SRandall Stewart 			}
4364f8829a4aSRandall Stewart 			return (NULL);
4365f8829a4aSRandall Stewart 		}
4366f8829a4aSRandall Stewart 	} else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4367f8829a4aSRandall Stewart 		/*
4368f8829a4aSRandall Stewart 		 * If there is no stcb, skip the AUTH chunk and process
4369f8829a4aSRandall Stewart 		 * later after a stcb is found (to validate the lookup was
4370f8829a4aSRandall Stewart 		 * valid.
4371f8829a4aSRandall Stewart 		 */
4372f8829a4aSRandall Stewart 		if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4373b3f1ea41SRandall Stewart 		    (stcb == NULL) &&
4374c79bec9cSMichael Tuexen 		    (inp->auth_supported == 1)) {
4375f8829a4aSRandall Stewart 			/* save this chunk for later processing */
4376f8829a4aSRandall Stewart 			auth_skipped = 1;
4377f8829a4aSRandall Stewart 			auth_offset = *offset;
4378f8829a4aSRandall Stewart 			auth_len = ntohs(ch->chunk_length);
4379f8829a4aSRandall Stewart 
4380f8829a4aSRandall Stewart 			/* (temporarily) move past this chunk */
4381f8829a4aSRandall Stewart 			*offset += SCTP_SIZE32(auth_len);
4382f8829a4aSRandall Stewart 			if (*offset >= length) {
4383f8829a4aSRandall Stewart 				/* no more data left in the mbuf chain */
4384f8829a4aSRandall Stewart 				*offset = length;
4385f8829a4aSRandall Stewart 				return (NULL);
4386f8829a4aSRandall Stewart 			}
4387f8829a4aSRandall Stewart 			ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4388f8829a4aSRandall Stewart 			    sizeof(struct sctp_chunkhdr), chunk_buf);
4389f8829a4aSRandall Stewart 		}
4390ad81507eSRandall Stewart 		if (ch == NULL) {
4391ad81507eSRandall Stewart 			/* Help */
4392ad81507eSRandall Stewart 			*offset = length;
439380a2d140SMichael Tuexen 			return (stcb);
4394ad81507eSRandall Stewart 		}
4395f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4396f8829a4aSRandall Stewart 			goto process_control_chunks;
4397f8829a4aSRandall Stewart 		}
4398f8829a4aSRandall Stewart 		/*
4399f8829a4aSRandall Stewart 		 * first check if it's an ASCONF with an unknown src addr we
4400f8829a4aSRandall Stewart 		 * need to look inside to find the association
4401f8829a4aSRandall Stewart 		 */
4402f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
44032afb3e84SRandall Stewart 			struct sctp_chunkhdr *asconf_ch = ch;
44042afb3e84SRandall Stewart 			uint32_t asconf_offset = 0, asconf_len = 0;
44052afb3e84SRandall Stewart 
4406f8829a4aSRandall Stewart 			/* inp's refcount may be reduced */
4407f8829a4aSRandall Stewart 			SCTP_INP_INCR_REF(inp);
4408f8829a4aSRandall Stewart 
44092afb3e84SRandall Stewart 			asconf_offset = *offset;
44102afb3e84SRandall Stewart 			do {
44112afb3e84SRandall Stewart 				asconf_len = ntohs(asconf_ch->chunk_length);
44122afb3e84SRandall Stewart 				if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
44132afb3e84SRandall Stewart 					break;
44147215cc1bSMichael Tuexen 				stcb = sctp_findassociation_ep_asconf(m,
4415b1754ad1SMichael Tuexen 				    *offset,
4416b1754ad1SMichael Tuexen 				    dst,
4417b1754ad1SMichael Tuexen 				    sh, &inp, netp, vrf_id);
44182afb3e84SRandall Stewart 				if (stcb != NULL)
44192afb3e84SRandall Stewart 					break;
44202afb3e84SRandall Stewart 				asconf_offset += SCTP_SIZE32(asconf_len);
44212afb3e84SRandall Stewart 				asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
44222afb3e84SRandall Stewart 				    sizeof(struct sctp_chunkhdr), chunk_buf);
44232afb3e84SRandall Stewart 			} while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4424f8829a4aSRandall Stewart 			if (stcb == NULL) {
4425f8829a4aSRandall Stewart 				/*
4426f8829a4aSRandall Stewart 				 * reduce inp's refcount if not reduced in
4427f8829a4aSRandall Stewart 				 * sctp_findassociation_ep_asconf().
4428f8829a4aSRandall Stewart 				 */
4429f8829a4aSRandall Stewart 				SCTP_INP_DECR_REF(inp);
4430f8829a4aSRandall Stewart 			}
44310053ed28SMichael Tuexen 
4432f8829a4aSRandall Stewart 			/* now go back and verify any auth chunk to be sure */
4433f8829a4aSRandall Stewart 			if (auth_skipped && (stcb != NULL)) {
4434f8829a4aSRandall Stewart 				struct sctp_auth_chunk *auth;
4435f8829a4aSRandall Stewart 
44368262311cSMichael Tuexen 				if (auth_len <= SCTP_CHUNK_BUFFER_SIZE) {
44378262311cSMichael Tuexen 					auth = (struct sctp_auth_chunk *)sctp_m_getptr(m, auth_offset, auth_len, chunk_buf);
4438f8829a4aSRandall Stewart 					got_auth = 1;
4439f8829a4aSRandall Stewart 					auth_skipped = 0;
44408262311cSMichael Tuexen 				} else {
44418262311cSMichael Tuexen 					auth = NULL;
44428262311cSMichael Tuexen 				}
4443ad81507eSRandall Stewart 				if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4444f8829a4aSRandall Stewart 				    auth_offset)) {
4445f8829a4aSRandall Stewart 					/* auth HMAC failed so dump it */
4446f8829a4aSRandall Stewart 					*offset = length;
444780a2d140SMichael Tuexen 					return (stcb);
4448f8829a4aSRandall Stewart 				} else {
4449f8829a4aSRandall Stewart 					/* remaining chunks are HMAC checked */
4450f8829a4aSRandall Stewart 					stcb->asoc.authenticated = 1;
4451f8829a4aSRandall Stewart 				}
4452f8829a4aSRandall Stewart 			}
4453f8829a4aSRandall Stewart 		}
4454f8829a4aSRandall Stewart 		if (stcb == NULL) {
4455999f86d6SMichael Tuexen 			SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4456ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4457ff1ffd74SMichael Tuexen 			    msg);
4458f8829a4aSRandall Stewart 			/* no association, so it's out of the blue... */
4459ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp, op_err,
4460d089f9b9SMichael Tuexen 			    mflowtype, mflowid, inp->fibnum,
4461c54a18d2SRandall Stewart 			    vrf_id, port);
4462f8829a4aSRandall Stewart 			*offset = length;
4463f8829a4aSRandall Stewart 			return (NULL);
4464f8829a4aSRandall Stewart 		}
4465f8829a4aSRandall Stewart 		asoc = &stcb->asoc;
4466f8829a4aSRandall Stewart 		/* ABORT and SHUTDOWN can use either v_tag... */
4467f8829a4aSRandall Stewart 		if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4468f8829a4aSRandall Stewart 		    (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4469f8829a4aSRandall Stewart 		    (ch->chunk_type == SCTP_PACKET_DROPPED)) {
44705db47b3dSMichael Tuexen 			/* Take the T-bit always into account. */
44715db47b3dSMichael Tuexen 			if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) &&
44725db47b3dSMichael Tuexen 			    (vtag_in == asoc->my_vtag)) ||
44735db47b3dSMichael Tuexen 			    (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) &&
4474ff76c8c9SMichael Tuexen 			    (asoc->peer_vtag != htonl(0)) &&
4475f8829a4aSRandall Stewart 			    (vtag_in == asoc->peer_vtag))) {
4476f8829a4aSRandall Stewart 				/* this is valid */
4477f8829a4aSRandall Stewart 			} else {
4478f8829a4aSRandall Stewart 				/* drop this packet... */
4479f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_badvtag);
448080a2d140SMichael Tuexen 				if (stcb != NULL) {
448180a2d140SMichael Tuexen 					SCTP_TCB_UNLOCK(stcb);
44826e55db54SRandall Stewart 				}
4483f8829a4aSRandall Stewart 				return (NULL);
4484f8829a4aSRandall Stewart 			}
4485f8829a4aSRandall Stewart 		} else {
4486f8829a4aSRandall Stewart 			/* for all other chunks, vtag must match */
4487f8829a4aSRandall Stewart 			if (vtag_in != asoc->my_vtag) {
4488f8829a4aSRandall Stewart 				/* invalid vtag... */
4489ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INPUT3,
4490ad81507eSRandall Stewart 				    "invalid vtag: %xh, expect %xh\n",
4491ad81507eSRandall Stewart 				    vtag_in, asoc->my_vtag);
4492f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_badvtag);
449380a2d140SMichael Tuexen 				if (stcb != NULL) {
449480a2d140SMichael Tuexen 					SCTP_TCB_UNLOCK(stcb);
44956e55db54SRandall Stewart 				}
4496f8829a4aSRandall Stewart 				*offset = length;
4497f8829a4aSRandall Stewart 				return (NULL);
4498f8829a4aSRandall Stewart 			}
4499f8829a4aSRandall Stewart 		}
4500b7b84c0eSMichael Tuexen 	}			/* end if !SCTP_COOKIE_ECHO */
4501b7b84c0eSMichael Tuexen 	/*
4502b7b84c0eSMichael Tuexen 	 * process all control chunks...
4503b7b84c0eSMichael Tuexen 	 */
4504f8829a4aSRandall Stewart 	if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4505830d754dSRandall Stewart 	    (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4506f8829a4aSRandall Stewart 	    (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4507839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
4508f8829a4aSRandall Stewart 		/* implied cookie-ack.. we must have lost the ack */
4509f8829a4aSRandall Stewart 		sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4510f8829a4aSRandall Stewart 		    *netp);
4511f8829a4aSRandall Stewart 	}
45120053ed28SMichael Tuexen 
4513f8829a4aSRandall Stewart process_control_chunks:
4514f8829a4aSRandall Stewart 	while (IS_SCTP_CONTROL(ch)) {
4515f8829a4aSRandall Stewart 		/* validate chunk length */
4516f8829a4aSRandall Stewart 		chk_length = ntohs(ch->chunk_length);
4517ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4518f8829a4aSRandall Stewart 		    ch->chunk_type, chk_length);
451980fefe0aSRandall Stewart 		SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
45204c9179adSRandall Stewart 		if (chk_length < sizeof(*ch) ||
45214c9179adSRandall Stewart 		    (*offset + (int)chk_length) > length) {
4522f8829a4aSRandall Stewart 			*offset = length;
452380a2d140SMichael Tuexen 			return (stcb);
4524f8829a4aSRandall Stewart 		}
4525f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4526f8829a4aSRandall Stewart 		/*
4527d0f6ab79SMichael Tuexen 		 * INIT and INIT-ACK only gets the init ack "header" portion
4528d0f6ab79SMichael Tuexen 		 * only because we don't have to process the peer's COOKIE.
4529d0f6ab79SMichael Tuexen 		 * All others get a complete chunk.
4530f8829a4aSRandall Stewart 		 */
4531d0f6ab79SMichael Tuexen 		switch (ch->chunk_type) {
4532d0f6ab79SMichael Tuexen 		case SCTP_INITIATION:
4533d0f6ab79SMichael Tuexen 			contiguous = sizeof(struct sctp_init_chunk);
4534d0f6ab79SMichael Tuexen 			break;
4535d0f6ab79SMichael Tuexen 		case SCTP_INITIATION_ACK:
4536d0f6ab79SMichael Tuexen 			contiguous = sizeof(struct sctp_init_ack_chunk);
4537d0f6ab79SMichael Tuexen 			break;
4538d0f6ab79SMichael Tuexen 		default:
4539d0f6ab79SMichael Tuexen 			contiguous = min(chk_length, sizeof(chunk_buf));
4540d0f6ab79SMichael Tuexen 			break;
45416e55db54SRandall Stewart 		}
4542d06c82f1SRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4543d0f6ab79SMichael Tuexen 		    contiguous,
4544d06c82f1SRandall Stewart 		    chunk_buf);
4545d06c82f1SRandall Stewart 		if (ch == NULL) {
4546d06c82f1SRandall Stewart 			*offset = length;
4547c70d1ef1SMichael Tuexen 			return (stcb);
4548d06c82f1SRandall Stewart 		}
45490053ed28SMichael Tuexen 
4550f8829a4aSRandall Stewart 		num_chunks++;
4551f8829a4aSRandall Stewart 		/* Save off the last place we got a control from */
4552f8829a4aSRandall Stewart 		if (stcb != NULL) {
4553ad81507eSRandall Stewart 			if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4554f8829a4aSRandall Stewart 				/*
4555f8829a4aSRandall Stewart 				 * allow last_control to be NULL if
4556f8829a4aSRandall Stewart 				 * ASCONF... ASCONF processing will find the
4557f8829a4aSRandall Stewart 				 * right net later
4558f8829a4aSRandall Stewart 				 */
4559ad81507eSRandall Stewart 				if ((netp != NULL) && (*netp != NULL))
4560f8829a4aSRandall Stewart 					stcb->asoc.last_control_chunk_from = *netp;
4561f8829a4aSRandall Stewart 			}
4562f8829a4aSRandall Stewart 		}
4563f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
4564f8829a4aSRandall Stewart 		sctp_audit_log(0xB0, ch->chunk_type);
4565f8829a4aSRandall Stewart #endif
4566f8829a4aSRandall Stewart 
4567f8829a4aSRandall Stewart 		/* check to see if this chunk required auth, but isn't */
4568b3f1ea41SRandall Stewart 		if ((stcb != NULL) &&
4569b3f1ea41SRandall Stewart 		    sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4570f8829a4aSRandall Stewart 		    !stcb->asoc.authenticated) {
4571f8829a4aSRandall Stewart 			/* "silently" ignore */
4572f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvauthmissing);
4573f8829a4aSRandall Stewart 			goto next_chunk;
4574f8829a4aSRandall Stewart 		}
4575f8829a4aSRandall Stewart 		switch (ch->chunk_type) {
4576f8829a4aSRandall Stewart 		case SCTP_INITIATION:
4577ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4578d68fdc4dSMichael Tuexen 			/* The INIT chunk must be the only chunk. */
4579d68fdc4dSMichael Tuexen 			if ((num_chunks > 1) ||
4580ab6174d5SMichael Tuexen 			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4581c70d1ef1SMichael Tuexen 				/*
4582c70d1ef1SMichael Tuexen 				 * RFC 4960bis requires stopping the
4583c70d1ef1SMichael Tuexen 				 * processing of the packet.
4584c70d1ef1SMichael Tuexen 				 */
4585f8829a4aSRandall Stewart 				*offset = length;
4586c70d1ef1SMichael Tuexen 				return (stcb);
4587f8829a4aSRandall Stewart 			}
4588d68fdc4dSMichael Tuexen 			/* Honor our resource limit. */
4589d68fdc4dSMichael Tuexen 			if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4590ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4591eba8e643SMichael Tuexen 				sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
4592eba8e643SMichael Tuexen 				    mflowtype, mflowid, inp->fibnum,
4593f30ac432SMichael Tuexen 				    vrf_id, port);
4594f8829a4aSRandall Stewart 				*offset = length;
4595eba8e643SMichael Tuexen 				if (stcb != NULL) {
4596eba8e643SMichael Tuexen 					SCTP_TCB_UNLOCK(stcb);
4597eba8e643SMichael Tuexen 				}
4598f8829a4aSRandall Stewart 				return (NULL);
4599f8829a4aSRandall Stewart 			}
4600b1754ad1SMichael Tuexen 			sctp_handle_init(m, iphlen, *offset, src, dst, sh,
4601ad81507eSRandall Stewart 			    (struct sctp_init_chunk *)ch, inp,
4602eba8e643SMichael Tuexen 			    stcb, *netp,
4603457b4b88SMichael Tuexen 			    mflowtype, mflowid,
4604f30ac432SMichael Tuexen 			    vrf_id, port);
4605f8829a4aSRandall Stewart 			*offset = length;
4606eba8e643SMichael Tuexen 			if (stcb != NULL) {
460780a2d140SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
46086e55db54SRandall Stewart 			}
4609f8829a4aSRandall Stewart 			return (NULL);
4610f8829a4aSRandall Stewart 			break;
46119a972525SRandall Stewart 		case SCTP_PAD_CHUNK:
46129a972525SRandall Stewart 			break;
4613f8829a4aSRandall Stewart 		case SCTP_INITIATION_ACK:
4614469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT_ACK\n");
4615f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4616f8829a4aSRandall Stewart 				/* We are not interested anymore */
461780a2d140SMichael Tuexen 				if ((stcb != NULL) && (stcb->asoc.total_output_queue_size)) {
4618f8829a4aSRandall Stewart 					;
4619f8829a4aSRandall Stewart 				} else {
4620f8829a4aSRandall Stewart 					*offset = length;
462180a2d140SMichael Tuexen 					if (stcb != NULL) {
4622b7d130beSMichael Tuexen 						(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4623b7d130beSMichael Tuexen 						    SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
4624f8829a4aSRandall Stewart 					}
4625f8829a4aSRandall Stewart 					return (NULL);
4626f8829a4aSRandall Stewart 				}
4627f8829a4aSRandall Stewart 			}
4628ab6174d5SMichael Tuexen 			/* The INIT-ACK chunk must be the only chunk. */
4629f8829a4aSRandall Stewart 			if ((num_chunks > 1) ||
4630ab6174d5SMichael Tuexen 			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4631f8829a4aSRandall Stewart 				*offset = length;
463280a2d140SMichael Tuexen 				return (stcb);
4633f8829a4aSRandall Stewart 			}
4634469a65d1SMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
4635b1754ad1SMichael Tuexen 				ret = sctp_handle_init_ack(m, iphlen, *offset,
4636b1754ad1SMichael Tuexen 				    src, dst, sh,
4637f30ac432SMichael Tuexen 				    (struct sctp_init_ack_chunk *)ch,
4638f30ac432SMichael Tuexen 				    stcb, *netp,
4639f30ac432SMichael Tuexen 				    &abort_no_unlock,
4640457b4b88SMichael Tuexen 				    mflowtype, mflowid,
4641f30ac432SMichael Tuexen 				    vrf_id);
4642ad81507eSRandall Stewart 			} else {
4643ad81507eSRandall Stewart 				ret = -1;
4644ad81507eSRandall Stewart 			}
4645d68fdc4dSMichael Tuexen 			*offset = length;
4646d68fdc4dSMichael Tuexen 			if (abort_no_unlock) {
4647d68fdc4dSMichael Tuexen 				return (NULL);
4648d68fdc4dSMichael Tuexen 			}
4649f8829a4aSRandall Stewart 			/*
4650f8829a4aSRandall Stewart 			 * Special case, I must call the output routine to
4651f8829a4aSRandall Stewart 			 * get the cookie echoed
4652f8829a4aSRandall Stewart 			 */
4653d68fdc4dSMichael Tuexen 			if ((stcb != NULL) && (ret == 0)) {
4654ceaad40aSRandall Stewart 				sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4655d68fdc4dSMichael Tuexen 			}
465680a2d140SMichael Tuexen 			return (stcb);
4657f8829a4aSRandall Stewart 			break;
4658f8829a4aSRandall Stewart 		case SCTP_SELECTIVE_ACK:
4659469a65d1SMichael Tuexen 		case SCTP_NR_SELECTIVE_ACK:
4660f8829a4aSRandall Stewart 			{
4661f8829a4aSRandall Stewart 				int abort_now = 0;
4662f8829a4aSRandall Stewart 				uint32_t a_rwnd, cum_ack;
4663469a65d1SMichael Tuexen 				uint16_t num_seg, num_nr_seg, num_dup;
4664cd554309SMichael Tuexen 				uint8_t flags;
4665cd554309SMichael Tuexen 				int offset_seg, offset_dup;
4666f8829a4aSRandall Stewart 
4667469a65d1SMichael Tuexen 				SCTPDBG(SCTP_DEBUG_INPUT3, "%s\n",
4668469a65d1SMichael Tuexen 				    ch->chunk_type == SCTP_SELECTIVE_ACK ? "SCTP_SACK" : "SCTP_NR_SACK");
466920083c2eSMichael Tuexen 				SCTP_STAT_INCR(sctps_recvsacks);
4670cd554309SMichael Tuexen 				if (stcb == NULL) {
4671469a65d1SMichael Tuexen 					SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing %s chunk\n",
4672469a65d1SMichael Tuexen 					    (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK");
4673cd554309SMichael Tuexen 					break;
46746e55db54SRandall Stewart 				}
4675469a65d1SMichael Tuexen 				if (ch->chunk_type == SCTP_SELECTIVE_ACK) {
4676cd554309SMichael Tuexen 					if (chk_length < sizeof(struct sctp_sack_chunk)) {
4677cd554309SMichael Tuexen 						SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4678cd554309SMichael Tuexen 						break;
4679d06c82f1SRandall Stewart 					}
4680469a65d1SMichael Tuexen 				} else {
4681469a65d1SMichael Tuexen 					if (stcb->asoc.nrsack_supported == 0) {
4682469a65d1SMichael Tuexen 						goto unknown_chunk;
4683469a65d1SMichael Tuexen 					}
4684469a65d1SMichael Tuexen 					if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
4685469a65d1SMichael Tuexen 						SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR_SACK chunk, too small\n");
4686469a65d1SMichael Tuexen 						break;
4687469a65d1SMichael Tuexen 					}
4688469a65d1SMichael Tuexen 				}
4689839d21d6SMichael Tuexen 				if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
46902afb3e84SRandall Stewart 					/*-
46912afb3e84SRandall Stewart 					 * If we have sent a shutdown-ack, we will pay no
46922afb3e84SRandall Stewart 					 * attention to a sack sent in to us since
46932afb3e84SRandall Stewart 					 * we don't care anymore.
46942afb3e84SRandall Stewart 					 */
4695a1e13272SRandall Stewart 					break;
46962afb3e84SRandall Stewart 				}
4697cd554309SMichael Tuexen 				flags = ch->chunk_flags;
4698469a65d1SMichael Tuexen 				if (ch->chunk_type == SCTP_SELECTIVE_ACK) {
4699469a65d1SMichael Tuexen 					struct sctp_sack_chunk *sack;
4700469a65d1SMichael Tuexen 
4701469a65d1SMichael Tuexen 					sack = (struct sctp_sack_chunk *)ch;
4702f8829a4aSRandall Stewart 					cum_ack = ntohl(sack->sack.cum_tsn_ack);
4703f8829a4aSRandall Stewart 					num_seg = ntohs(sack->sack.num_gap_ack_blks);
4704469a65d1SMichael Tuexen 					num_nr_seg = 0;
4705cd554309SMichael Tuexen 					num_dup = ntohs(sack->sack.num_dup_tsns);
4706469a65d1SMichael Tuexen 					a_rwnd = ntohl(sack->sack.a_rwnd);
4707cd554309SMichael Tuexen 					if (sizeof(struct sctp_sack_chunk) +
4708cd554309SMichael Tuexen 					    num_seg * sizeof(struct sctp_gap_ack_block) +
4709cd554309SMichael Tuexen 					    num_dup * sizeof(uint32_t) != chk_length) {
4710cd554309SMichael Tuexen 						SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4711cd554309SMichael Tuexen 						break;
4712cd554309SMichael Tuexen 					}
4713cd554309SMichael Tuexen 					offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4714cd554309SMichael Tuexen 					offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4715f8829a4aSRandall Stewart 				} else {
4716830d754dSRandall Stewart 					struct sctp_nr_sack_chunk *nr_sack;
4717830d754dSRandall Stewart 
4718830d754dSRandall Stewart 					nr_sack = (struct sctp_nr_sack_chunk *)ch;
4719830d754dSRandall Stewart 					cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
4720830d754dSRandall Stewart 					num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
4721830d754dSRandall Stewart 					num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4722cd554309SMichael Tuexen 					num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
4723469a65d1SMichael Tuexen 					a_rwnd = ntohl(nr_sack->nr_sack.a_rwnd);
4724cd554309SMichael Tuexen 					if (sizeof(struct sctp_nr_sack_chunk) +
4725cd554309SMichael Tuexen 					    (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
4726cd554309SMichael Tuexen 					    num_dup * sizeof(uint32_t) != chk_length) {
4727cd554309SMichael Tuexen 						SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
4728cd554309SMichael Tuexen 						break;
4729cd554309SMichael Tuexen 					}
4730cd554309SMichael Tuexen 					offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
4731469a65d1SMichael Tuexen 					offset_dup = offset_seg + (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block);
4732469a65d1SMichael Tuexen 				}
4733469a65d1SMichael Tuexen 				SCTPDBG(SCTP_DEBUG_INPUT3, "%s process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4734469a65d1SMichael Tuexen 				    (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK",
4735cd554309SMichael Tuexen 				    cum_ack, num_seg, a_rwnd);
4736830d754dSRandall Stewart 				stcb->asoc.seen_a_sack_this_pkt = 1;
4737830d754dSRandall Stewart 				if ((stcb->asoc.pr_sctp_cnt == 0) &&
4738cd554309SMichael Tuexen 				    (num_seg == 0) && (num_nr_seg == 0) &&
473920b07a4dSMichael Tuexen 				    SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4740830d754dSRandall Stewart 				    (stcb->asoc.saw_sack_with_frags == 0) &&
4741d9c5cfeaSMichael Tuexen 				    (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4742cd554309SMichael Tuexen 				    (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
4743830d754dSRandall Stewart 					/*
4744830d754dSRandall Stewart 					 * We have a SIMPLE sack having no
4745830d754dSRandall Stewart 					 * prior segments and data on sent
4746cd554309SMichael Tuexen 					 * queue to be acked. Use the faster
4747cd554309SMichael Tuexen 					 * path sack processing. We also
4748cd554309SMichael Tuexen 					 * allow window update sacks with no
4749cd554309SMichael Tuexen 					 * missing segments to go this way
4750cd554309SMichael Tuexen 					 * too.
4751830d754dSRandall Stewart 					 */
4752493d8e5aSRandall Stewart 					sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
4753899288aeSRandall Stewart 					    &abort_now, ecne_seen);
4754830d754dSRandall Stewart 				} else {
4755469a65d1SMichael Tuexen 					if ((netp != NULL) && (*netp != NULL)) {
47567215cc1bSMichael Tuexen 						sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4757cd554309SMichael Tuexen 						    num_seg, num_nr_seg, num_dup, &abort_now, flags,
4758899288aeSRandall Stewart 						    cum_ack, a_rwnd, ecne_seen);
4759830d754dSRandall Stewart 					}
4760469a65d1SMichael Tuexen 				}
4761830d754dSRandall Stewart 				if (abort_now) {
4762830d754dSRandall Stewart 					/* ABORT signal from sack processing */
4763830d754dSRandall Stewart 					*offset = length;
4764830d754dSRandall Stewart 					return (NULL);
4765830d754dSRandall Stewart 				}
4766cd554309SMichael Tuexen 				if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4767cd554309SMichael Tuexen 				    TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4768cd554309SMichael Tuexen 				    (stcb->asoc.stream_queue_cnt == 0)) {
4769cd554309SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4770cd554309SMichael Tuexen 				}
4771830d754dSRandall Stewart 				break;
4772469a65d1SMichael Tuexen 			}
4773f8829a4aSRandall Stewart 		case SCTP_HEARTBEAT_REQUEST:
4774ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
4775469a65d1SMichael Tuexen 			if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
4776f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_recvheartbeat);
4777ad81507eSRandall Stewart 				sctp_send_heartbeat_ack(stcb, m, *offset,
4778ad81507eSRandall Stewart 				    chk_length, *netp);
4779ad81507eSRandall Stewart 			}
4780f8829a4aSRandall Stewart 			break;
4781f8829a4aSRandall Stewart 		case SCTP_HEARTBEAT_ACK:
4782469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT_ACK\n");
4783ad81507eSRandall Stewart 			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
4784d06c82f1SRandall Stewart 				/* Its not ours */
47859de7354bSMichael Tuexen 				break;
4786d06c82f1SRandall Stewart 			}
4787f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvheartbeatack);
4788469a65d1SMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
4789f8829a4aSRandall Stewart 				sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
4790f8829a4aSRandall Stewart 				    stcb, *netp);
4791469a65d1SMichael Tuexen 			}
4792f8829a4aSRandall Stewart 			break;
4793f8829a4aSRandall Stewart 		case SCTP_ABORT_ASSOCIATION:
4794207304d4SRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
4795dd294dceSMichael Tuexen 			    (void *)stcb);
4796f8829a4aSRandall Stewart 			*offset = length;
4797701492a5SMichael Tuexen 			if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
4798701492a5SMichael Tuexen 				if (sctp_handle_abort((struct sctp_abort_chunk *)ch, stcb, *netp)) {
4799f8829a4aSRandall Stewart 					return (NULL);
4800701492a5SMichael Tuexen 				} else {
4801701492a5SMichael Tuexen 					return (stcb);
4802701492a5SMichael Tuexen 				}
4803701492a5SMichael Tuexen 			} else {
4804701492a5SMichael Tuexen 				return (NULL);
4805701492a5SMichael Tuexen 			}
4806f8829a4aSRandall Stewart 			break;
4807f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN:
4808207304d4SRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
4809dd294dceSMichael Tuexen 			    (void *)stcb);
4810ad81507eSRandall Stewart 			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
48119de7354bSMichael Tuexen 				break;
4812ad81507eSRandall Stewart 			}
4813469a65d1SMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
48149de7354bSMichael Tuexen 				abort_flag = 0;
4815f8829a4aSRandall Stewart 				sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
4816f8829a4aSRandall Stewart 				    stcb, *netp, &abort_flag);
4817f8829a4aSRandall Stewart 				if (abort_flag) {
4818f8829a4aSRandall Stewart 					*offset = length;
4819f8829a4aSRandall Stewart 					return (NULL);
4820f8829a4aSRandall Stewart 				}
4821f8829a4aSRandall Stewart 			}
4822f8829a4aSRandall Stewart 			break;
4823f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_ACK:
4824469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_ACK, stcb %p\n", (void *)stcb);
4825d1cb8d11SMichael Tuexen 			if ((chk_length == sizeof(struct sctp_shutdown_ack_chunk)) &&
4826d1cb8d11SMichael Tuexen 			    (stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
4827f8829a4aSRandall Stewart 				sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
4828f8829a4aSRandall Stewart 				*offset = length;
4829f8829a4aSRandall Stewart 				return (NULL);
4830d1cb8d11SMichael Tuexen 			}
4831f8829a4aSRandall Stewart 			break;
4832f8829a4aSRandall Stewart 		case SCTP_OPERATION_ERROR:
4833469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP_ERR\n");
4834469a65d1SMichael Tuexen 			if ((stcb != NULL) && (netp != NULL) && (*netp != NULL) &&
48353e87bccdSMichael Tuexen 			    sctp_handle_error(ch, stcb, *netp, contiguous) < 0) {
4836f8829a4aSRandall Stewart 				*offset = length;
4837f8829a4aSRandall Stewart 				return (NULL);
4838f8829a4aSRandall Stewart 			}
4839f8829a4aSRandall Stewart 			break;
4840f8829a4aSRandall Stewart 		case SCTP_COOKIE_ECHO:
4841ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3,
4842469a65d1SMichael Tuexen 			    "SCTP_COOKIE_ECHO, stcb %p\n", (void *)stcb);
4843469a65d1SMichael Tuexen 			if ((stcb != NULL) && (stcb->asoc.total_output_queue_size > 0)) {
4844f8829a4aSRandall Stewart 				;
4845f8829a4aSRandall Stewart 			} else {
4846ad81507eSRandall Stewart 				if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4847f8829a4aSRandall Stewart 					/* We are not interested anymore */
48480c7dc840SRandall Stewart 			abend:
484980a2d140SMichael Tuexen 					if (stcb != NULL) {
485028085b2eSRandall Stewart 						SCTP_TCB_UNLOCK(stcb);
485128085b2eSRandall Stewart 					}
4852f8829a4aSRandall Stewart 					*offset = length;
4853f8829a4aSRandall Stewart 					return (NULL);
4854f8829a4aSRandall Stewart 				}
4855f8829a4aSRandall Stewart 			}
48563017b21bSMichael Tuexen 			/*-
4857f8829a4aSRandall Stewart 			 * First are we accepting? We do this again here
485888a7eb29SRandall Stewart 			 * since it is possible that a previous endpoint WAS
485988a7eb29SRandall Stewart 			 * listening responded to a INIT-ACK and then
4860f8829a4aSRandall Stewart 			 * closed. We opened and bound.. and are now no
4861f8829a4aSRandall Stewart 			 * longer listening.
4862779f106aSGleb Smirnoff 			 *
4863779f106aSGleb Smirnoff 			 * XXXGL: notes on checking listen queue length.
4864779f106aSGleb Smirnoff 			 * 1) SCTP_IS_LISTENING() doesn't necessarily mean
4865779f106aSGleb Smirnoff 			 *    SOLISTENING(), because a listening "UDP type"
4866779f106aSGleb Smirnoff 			 *    socket isn't listening in terms of the socket
4867779f106aSGleb Smirnoff 			 *    layer.  It is a normal data flow socket, that
4868779f106aSGleb Smirnoff 			 *    can fork off new connections.  Thus, we should
4869779f106aSGleb Smirnoff 			 *    look into sol_qlen only in case we are !UDP.
4870779f106aSGleb Smirnoff 			 * 2) Checking sol_qlen in general requires locking
4871779f106aSGleb Smirnoff 			 *    the socket, and this code lacks that.
4872f8829a4aSRandall Stewart 			 */
48735d08768aSMichael Tuexen 			if ((stcb == NULL) &&
48745d08768aSMichael Tuexen 			    (!SCTP_IS_LISTENING(inp) ||
48759b2a35b3SMichael Tuexen 			    (((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) == 0) &&
4876779f106aSGleb Smirnoff 			    inp->sctp_socket->sol_qlen >= inp->sctp_socket->sol_qlimit))) {
4877b201f536SRandall Stewart 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4878b3f1ea41SRandall Stewart 				    (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
4879ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4880b1754ad1SMichael Tuexen 					sctp_abort_association(inp, stcb, m, iphlen,
4881b1754ad1SMichael Tuexen 					    src, dst, sh, op_err,
4882457b4b88SMichael Tuexen 					    mflowtype, mflowid,
4883f30ac432SMichael Tuexen 					    vrf_id, port);
4884f8829a4aSRandall Stewart 				}
4885f8829a4aSRandall Stewart 				*offset = length;
4886f8829a4aSRandall Stewart 				return (NULL);
4887b201f536SRandall Stewart 			} else {
4888f8829a4aSRandall Stewart 				struct mbuf *ret_buf;
4889a5d547adSRandall Stewart 				struct sctp_inpcb *linp;
4890efd5e692SMichael Tuexen 				struct sctp_tmit_chunk *chk;
4891f8829a4aSRandall Stewart 
4892c98bf2a4SMark Johnston 				if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE |
4893c98bf2a4SMark Johnston 				    SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4894c98bf2a4SMark Johnston 					goto abend;
4895c98bf2a4SMark Johnston 				}
4896c98bf2a4SMark Johnston 
4897ad81507eSRandall Stewart 				if (stcb) {
4898a5d547adSRandall Stewart 					linp = NULL;
4899ad81507eSRandall Stewart 				} else {
4900a5d547adSRandall Stewart 					linp = inp;
4901ad81507eSRandall Stewart 				}
4902a5d547adSRandall Stewart 
4903469a65d1SMichael Tuexen 				if (linp != NULL) {
4904a5d547adSRandall Stewart 					SCTP_ASOC_CREATE_LOCK(linp);
4905ad81507eSRandall Stewart 				}
49060053ed28SMichael Tuexen 
4907469a65d1SMichael Tuexen 				if (netp != NULL) {
490880a2d140SMichael Tuexen 					struct sctp_tcb *locked_stcb;
490980a2d140SMichael Tuexen 
491080a2d140SMichael Tuexen 					locked_stcb = stcb;
4911f8829a4aSRandall Stewart 					ret_buf =
4912f8829a4aSRandall Stewart 					    sctp_handle_cookie_echo(m, iphlen,
4913b1754ad1SMichael Tuexen 					    *offset,
4914b1754ad1SMichael Tuexen 					    src, dst,
4915b1754ad1SMichael Tuexen 					    sh,
4916f8829a4aSRandall Stewart 					    (struct sctp_cookie_echo_chunk *)ch,
4917f8829a4aSRandall Stewart 					    &inp, &stcb, netp,
4918f8829a4aSRandall Stewart 					    auth_skipped,
4919f8829a4aSRandall Stewart 					    auth_offset,
4920a5d547adSRandall Stewart 					    auth_len,
492180a2d140SMichael Tuexen 					    &locked_stcb,
4922457b4b88SMichael Tuexen 					    mflowtype,
4923f30ac432SMichael Tuexen 					    mflowid,
4924c54a18d2SRandall Stewart 					    vrf_id,
4925c54a18d2SRandall Stewart 					    port);
492680a2d140SMichael Tuexen 					if ((locked_stcb != NULL) && (locked_stcb != stcb)) {
492780a2d140SMichael Tuexen 						SCTP_TCB_UNLOCK(locked_stcb);
492880a2d140SMichael Tuexen 					}
492980a2d140SMichael Tuexen 					if (stcb != NULL) {
493080a2d140SMichael Tuexen 						SCTP_TCB_LOCK_ASSERT(stcb);
493180a2d140SMichael Tuexen 					}
4932ad81507eSRandall Stewart 				} else {
4933ad81507eSRandall Stewart 					ret_buf = NULL;
4934ad81507eSRandall Stewart 				}
4935469a65d1SMichael Tuexen 				if (linp != NULL) {
4936a5d547adSRandall Stewart 					SCTP_ASOC_CREATE_UNLOCK(linp);
4937ad81507eSRandall Stewart 				}
4938f8829a4aSRandall Stewart 				if (ret_buf == NULL) {
493980a2d140SMichael Tuexen 					if (stcb != NULL) {
494080a2d140SMichael Tuexen 						SCTP_TCB_UNLOCK(stcb);
4941f8829a4aSRandall Stewart 					}
4942ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INPUT3,
4943ad81507eSRandall Stewart 					    "GAK, null buffer\n");
4944f8829a4aSRandall Stewart 					*offset = length;
4945f8829a4aSRandall Stewart 					return (NULL);
4946f8829a4aSRandall Stewart 				}
4947f8829a4aSRandall Stewart 				/* if AUTH skipped, see if it verified... */
4948f8829a4aSRandall Stewart 				if (auth_skipped) {
4949f8829a4aSRandall Stewart 					got_auth = 1;
4950f8829a4aSRandall Stewart 					auth_skipped = 0;
4951f8829a4aSRandall Stewart 				}
4952efd5e692SMichael Tuexen 				/* Restart the timer if we have pending data */
495386fd36c5SMichael Tuexen 				TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
4954efd5e692SMichael Tuexen 					if (chk->whoTo != NULL) {
4955efd5e692SMichael Tuexen 						break;
4956efd5e692SMichael Tuexen 					}
4957efd5e692SMichael Tuexen 				}
4958efd5e692SMichael Tuexen 				if (chk != NULL) {
49594a9ef3f8SMichael Tuexen 					sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
4960f8829a4aSRandall Stewart 				}
4961f8829a4aSRandall Stewart 			}
4962f8829a4aSRandall Stewart 			break;
4963f8829a4aSRandall Stewart 		case SCTP_COOKIE_ACK:
4964469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE_ACK, stcb %p\n", (void *)stcb);
4965ad81507eSRandall Stewart 			if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
49669de7354bSMichael Tuexen 				break;
496717205eccSRandall Stewart 			}
4968f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4969f8829a4aSRandall Stewart 				/* We are not interested anymore */
4970f8829a4aSRandall Stewart 				if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4971f8829a4aSRandall Stewart 					;
4972ad81507eSRandall Stewart 				} else if (stcb) {
4973b7d130beSMichael Tuexen 					(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4974b7d130beSMichael Tuexen 					    SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
4975f8829a4aSRandall Stewart 					*offset = length;
4976f8829a4aSRandall Stewart 					return (NULL);
4977f8829a4aSRandall Stewart 				}
4978f8829a4aSRandall Stewart 			}
4979469a65d1SMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
4980f8829a4aSRandall Stewart 				sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
49816e55db54SRandall Stewart 			}
4982f8829a4aSRandall Stewart 			break;
4983f8829a4aSRandall Stewart 		case SCTP_ECN_ECHO:
4984469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_ECHO\n");
49859de7354bSMichael Tuexen 			if (stcb == NULL) {
49869de7354bSMichael Tuexen 				break;
4987d06c82f1SRandall Stewart 			}
4988c79bec9cSMichael Tuexen 			if (stcb->asoc.ecn_supported == 0) {
4989c79bec9cSMichael Tuexen 				goto unknown_chunk;
4990c79bec9cSMichael Tuexen 			}
49916587a2bdSMichael Tuexen 			if ((chk_length != sizeof(struct sctp_ecne_chunk)) &&
49926587a2bdSMichael Tuexen 			    (chk_length != sizeof(struct old_sctp_ecne_chunk))) {
49939de7354bSMichael Tuexen 				break;
49949de7354bSMichael Tuexen 			}
4995469a65d1SMichael Tuexen 			sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, stcb);
4996899288aeSRandall Stewart 			ecne_seen = 1;
4997f8829a4aSRandall Stewart 			break;
4998f8829a4aSRandall Stewart 		case SCTP_ECN_CWR:
4999469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_CWR\n");
50009de7354bSMichael Tuexen 			if (stcb == NULL) {
50019de7354bSMichael Tuexen 				break;
5002d06c82f1SRandall Stewart 			}
5003c79bec9cSMichael Tuexen 			if (stcb->asoc.ecn_supported == 0) {
5004c79bec9cSMichael Tuexen 				goto unknown_chunk;
5005c79bec9cSMichael Tuexen 			}
50069de7354bSMichael Tuexen 			if (chk_length != sizeof(struct sctp_cwr_chunk)) {
50079de7354bSMichael Tuexen 				break;
50089de7354bSMichael Tuexen 			}
5009a21779f0SRandall Stewart 			sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5010f8829a4aSRandall Stewart 			break;
5011f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_COMPLETE:
5012469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_COMPLETE, stcb %p\n", (void *)stcb);
5013f8829a4aSRandall Stewart 			/* must be first and only chunk */
5014f8829a4aSRandall Stewart 			if ((num_chunks > 1) ||
50154c9179adSRandall Stewart 			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5016f8829a4aSRandall Stewart 				*offset = length;
501780a2d140SMichael Tuexen 				return (stcb);
5018f8829a4aSRandall Stewart 			}
5019d1cb8d11SMichael Tuexen 			if ((chk_length == sizeof(struct sctp_shutdown_complete_chunk)) &&
5020d1cb8d11SMichael Tuexen 			    (stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5021f8829a4aSRandall Stewart 				sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5022f8829a4aSRandall Stewart 				    stcb, *netp);
5023f8829a4aSRandall Stewart 				*offset = length;
5024f8829a4aSRandall Stewart 				return (NULL);
5025d1cb8d11SMichael Tuexen 			}
5026f8829a4aSRandall Stewart 			break;
5027f8829a4aSRandall Stewart 		case SCTP_ASCONF:
5028ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5029469a65d1SMichael Tuexen 			if (stcb != NULL) {
5030c79bec9cSMichael Tuexen 				if (stcb->asoc.asconf_supported == 0) {
5031c79bec9cSMichael Tuexen 					goto unknown_chunk;
5032c79bec9cSMichael Tuexen 				}
5033b1754ad1SMichael Tuexen 				sctp_handle_asconf(m, *offset, src,
50342afb3e84SRandall Stewart 				    (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
50352afb3e84SRandall Stewart 				asconf_cnt++;
50366e55db54SRandall Stewart 			}
5037f8829a4aSRandall Stewart 			break;
5038f8829a4aSRandall Stewart 		case SCTP_ASCONF_ACK:
5039469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF_ACK\n");
50409de7354bSMichael Tuexen 			if (stcb == NULL) {
50419de7354bSMichael Tuexen 				break;
5042d06c82f1SRandall Stewart 			}
5043c79bec9cSMichael Tuexen 			if (stcb->asoc.asconf_supported == 0) {
5044c79bec9cSMichael Tuexen 				goto unknown_chunk;
5045c79bec9cSMichael Tuexen 			}
50469de7354bSMichael Tuexen 			if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
50479de7354bSMichael Tuexen 				break;
50489de7354bSMichael Tuexen 			}
50499de7354bSMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
5050f8829a4aSRandall Stewart 				/* He's alive so give him credit */
5051b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5052c4739e2fSRandall Stewart 					sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5053c4739e2fSRandall Stewart 					    stcb->asoc.overall_error_count,
5054c4739e2fSRandall Stewart 					    0,
5055c4739e2fSRandall Stewart 					    SCTP_FROM_SCTP_INPUT,
5056c4739e2fSRandall Stewart 					    __LINE__);
5057c4739e2fSRandall Stewart 				}
5058f8829a4aSRandall Stewart 				stcb->asoc.overall_error_count = 0;
5059f8829a4aSRandall Stewart 				sctp_handle_asconf_ack(m, *offset,
50603232788eSRandall Stewart 				    (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
50613232788eSRandall Stewart 				if (abort_no_unlock)
50623232788eSRandall Stewart 					return (NULL);
50636e55db54SRandall Stewart 			}
5064f8829a4aSRandall Stewart 			break;
5065f8829a4aSRandall Stewart 		case SCTP_FORWARD_CUM_TSN:
506644249214SRandall Stewart 		case SCTP_IFORWARD_CUM_TSN:
5067c96d7c37SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "%s\n",
5068c96d7c37SMichael Tuexen 			    ch->chunk_type == SCTP_FORWARD_CUM_TSN ? "FORWARD_TSN" : "I_FORWARD_TSN");
50699de7354bSMichael Tuexen 			if (stcb == NULL) {
50709de7354bSMichael Tuexen 				break;
5071d06c82f1SRandall Stewart 			}
5072c79bec9cSMichael Tuexen 			if (stcb->asoc.prsctp_supported == 0) {
5073c79bec9cSMichael Tuexen 				goto unknown_chunk;
5074c79bec9cSMichael Tuexen 			}
50759de7354bSMichael Tuexen 			if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
50769de7354bSMichael Tuexen 				break;
50779de7354bSMichael Tuexen 			}
5078fcbfdc0aSMichael Tuexen 			if (((stcb->asoc.idata_supported == 1) && (ch->chunk_type == SCTP_FORWARD_CUM_TSN)) ||
5079fcbfdc0aSMichael Tuexen 			    ((stcb->asoc.idata_supported == 0) && (ch->chunk_type == SCTP_IFORWARD_CUM_TSN))) {
5080c96d7c37SMichael Tuexen 				if (ch->chunk_type == SCTP_FORWARD_CUM_TSN) {
5081c96d7c37SMichael Tuexen 					SCTP_SNPRINTF(msg, sizeof(msg), "%s", "FORWARD-TSN chunk received when I-FORWARD-TSN was negotiated");
5082c96d7c37SMichael Tuexen 				} else {
5083c96d7c37SMichael Tuexen 					SCTP_SNPRINTF(msg, sizeof(msg), "%s", "I-FORWARD-TSN chunk received when FORWARD-TSN was negotiated");
5084c96d7c37SMichael Tuexen 				}
5085c96d7c37SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
5086105b68b4SMichael Tuexen 				sctp_abort_an_association(inp, stcb, op_err, false, SCTP_SO_NOT_LOCKED);
5087c96d7c37SMichael Tuexen 				*offset = length;
5088c96d7c37SMichael Tuexen 				return (NULL);
5089c96d7c37SMichael Tuexen 			}
5090f8829a4aSRandall Stewart 			*fwd_tsn_seen = 1;
5091f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5092f8829a4aSRandall Stewart 				/* We are not interested anymore */
5093b7d130beSMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5094b7d130beSMichael Tuexen 				    SCTP_FROM_SCTP_INPUT + SCTP_LOC_31);
5095f8829a4aSRandall Stewart 				*offset = length;
5096f8829a4aSRandall Stewart 				return (NULL);
5097f8829a4aSRandall Stewart 			}
509891843cf3SMichael Tuexen 			/*
50999de7354bSMichael Tuexen 			 * For sending a SACK this looks like DATA chunks.
510091843cf3SMichael Tuexen 			 */
510191843cf3SMichael Tuexen 			stcb->asoc.last_data_chunk_from = stcb->asoc.last_control_chunk_from;
51029de7354bSMichael Tuexen 			abort_flag = 0;
5103f8829a4aSRandall Stewart 			sctp_handle_forward_tsn(stcb,
5104671d309cSRandall Stewart 			    (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5105f8829a4aSRandall Stewart 			if (abort_flag) {
5106f8829a4aSRandall Stewart 				*offset = length;
5107f8829a4aSRandall Stewart 				return (NULL);
5108c4739e2fSRandall Stewart 			}
5109f8829a4aSRandall Stewart 			break;
5110f8829a4aSRandall Stewart 		case SCTP_STREAM_RESET:
5111ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
51129de7354bSMichael Tuexen 			if (stcb == NULL) {
51139de7354bSMichael Tuexen 				break;
5114d06c82f1SRandall Stewart 			}
5115317e00efSMichael Tuexen 			if (stcb->asoc.reconfig_supported == 0) {
5116c79bec9cSMichael Tuexen 				goto unknown_chunk;
5117f8829a4aSRandall Stewart 			}
51189de7354bSMichael Tuexen 			if (chk_length < sizeof(struct sctp_stream_reset_tsn_req)) {
51199de7354bSMichael Tuexen 				break;
51209de7354bSMichael Tuexen 			}
5121a169d6ecSMichael Tuexen 			if (sctp_handle_stream_reset(stcb, m, *offset, ch)) {
5122f8829a4aSRandall Stewart 				/* stop processing */
5123f8829a4aSRandall Stewart 				*offset = length;
5124f8829a4aSRandall Stewart 				return (NULL);
5125f8829a4aSRandall Stewart 			}
5126f8829a4aSRandall Stewart 			break;
5127f8829a4aSRandall Stewart 		case SCTP_PACKET_DROPPED:
5128ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
51299de7354bSMichael Tuexen 			if (stcb == NULL) {
51309de7354bSMichael Tuexen 				break;
5131d06c82f1SRandall Stewart 			}
5132c79bec9cSMichael Tuexen 			if (stcb->asoc.pktdrop_supported == 0) {
5133c79bec9cSMichael Tuexen 				goto unknown_chunk;
5134c79bec9cSMichael Tuexen 			}
51359de7354bSMichael Tuexen 			if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
51369de7354bSMichael Tuexen 				break;
51379de7354bSMichael Tuexen 			}
51389de7354bSMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
5139f8829a4aSRandall Stewart 				sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5140458303daSRandall Stewart 				    stcb, *netp,
5141d0f6ab79SMichael Tuexen 				    min(chk_length, contiguous));
51426e55db54SRandall Stewart 			}
5143f8829a4aSRandall Stewart 			break;
5144f8829a4aSRandall Stewart 		case SCTP_AUTHENTICATION:
5145ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5146f8829a4aSRandall Stewart 			if (stcb == NULL) {
5147f8829a4aSRandall Stewart 				/* save the first AUTH for later processing */
5148f8829a4aSRandall Stewart 				if (auth_skipped == 0) {
5149f8829a4aSRandall Stewart 					auth_offset = *offset;
5150f8829a4aSRandall Stewart 					auth_len = chk_length;
5151f8829a4aSRandall Stewart 					auth_skipped = 1;
5152f8829a4aSRandall Stewart 				}
5153f8829a4aSRandall Stewart 				/* skip this chunk (temporarily) */
51549de7354bSMichael Tuexen 				break;
5155f8829a4aSRandall Stewart 			}
5156c79bec9cSMichael Tuexen 			if (stcb->asoc.auth_supported == 0) {
5157c79bec9cSMichael Tuexen 				goto unknown_chunk;
5158c79bec9cSMichael Tuexen 			}
5159d06c82f1SRandall Stewart 			if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5160ad81507eSRandall Stewart 			    (chk_length > (sizeof(struct sctp_auth_chunk) +
5161ad81507eSRandall Stewart 			    SCTP_AUTH_DIGEST_LEN_MAX))) {
5162d06c82f1SRandall Stewart 				/* Its not ours */
5163d06c82f1SRandall Stewart 				*offset = length;
516480a2d140SMichael Tuexen 				return (stcb);
5165d06c82f1SRandall Stewart 			}
5166f8829a4aSRandall Stewart 			if (got_auth == 1) {
5167f8829a4aSRandall Stewart 				/* skip this chunk... it's already auth'd */
51689de7354bSMichael Tuexen 				break;
5169f8829a4aSRandall Stewart 			}
5170f8829a4aSRandall Stewart 			got_auth = 1;
5171f2f66ef6SMichael Tuexen 			if (sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, m, *offset)) {
5172f8829a4aSRandall Stewart 				/* auth HMAC failed so dump the packet */
5173f8829a4aSRandall Stewart 				*offset = length;
5174f8829a4aSRandall Stewart 				return (stcb);
5175f8829a4aSRandall Stewart 			} else {
5176f8829a4aSRandall Stewart 				/* remaining chunks are HMAC checked */
5177f8829a4aSRandall Stewart 				stcb->asoc.authenticated = 1;
5178f8829a4aSRandall Stewart 			}
5179f8829a4aSRandall Stewart 			break;
5180f8829a4aSRandall Stewart 
5181f8829a4aSRandall Stewart 		default:
5182f8829a4aSRandall Stewart 	unknown_chunk:
5183f8829a4aSRandall Stewart 			/* it's an unknown chunk! */
5184e99ce3eaSMichael Tuexen 			if ((ch->chunk_type & 0x40) &&
5185e99ce3eaSMichael Tuexen 			    (stcb != NULL) &&
5186e99ce3eaSMichael Tuexen 			    (SCTP_GET_STATE(stcb) != SCTP_STATE_EMPTY) &&
5187e99ce3eaSMichael Tuexen 			    (SCTP_GET_STATE(stcb) != SCTP_STATE_INUSE) &&
5188e99ce3eaSMichael Tuexen 			    (SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT)) {
518986eda749SMichael Tuexen 				struct sctp_gen_error_cause *cause;
519039cbb549SMichael Tuexen 				int len;
5191f8829a4aSRandall Stewart 
519286eda749SMichael Tuexen 				op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_gen_error_cause),
5193eb1b1807SGleb Smirnoff 				    0, M_NOWAIT, 1, MT_DATA);
519486eda749SMichael Tuexen 				if (op_err != NULL) {
519539cbb549SMichael Tuexen 					len = min(SCTP_SIZE32(chk_length), (uint32_t)(length - *offset));
519686eda749SMichael Tuexen 					cause = mtod(op_err, struct sctp_gen_error_cause *);
519786eda749SMichael Tuexen 					cause->code = htons(SCTP_CAUSE_UNRECOG_CHUNK);
51989a8e3088SMichael Tuexen 					cause->length = htons((uint16_t)(len + sizeof(struct sctp_gen_error_cause)));
519986eda749SMichael Tuexen 					SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
520086eda749SMichael Tuexen 					SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(m, *offset, len, M_NOWAIT);
520186eda749SMichael Tuexen 					if (SCTP_BUF_NEXT(op_err) != NULL) {
5202eadccaccSRandall Stewart #ifdef SCTP_MBUF_LOGGING
5203b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
520430811e70SMichael Tuexen 							sctp_log_mbc(SCTP_BUF_NEXT(op_err), SCTP_MBUF_ICOPY);
5205eadccaccSRandall Stewart 						}
5206eadccaccSRandall Stewart #endif
520786eda749SMichael Tuexen 						sctp_queue_op_err(stcb, op_err);
5208f8829a4aSRandall Stewart 					} else {
520986eda749SMichael Tuexen 						sctp_m_freem(op_err);
5210f8829a4aSRandall Stewart 					}
5211f8829a4aSRandall Stewart 				}
5212f8829a4aSRandall Stewart 			}
5213f8829a4aSRandall Stewart 			if ((ch->chunk_type & 0x80) == 0) {
5214f8829a4aSRandall Stewart 				/* discard this packet */
5215f8829a4aSRandall Stewart 				*offset = length;
5216f8829a4aSRandall Stewart 				return (stcb);
5217b7b84c0eSMichael Tuexen 			}	/* else skip this bad chunk and continue... */
5218b7b84c0eSMichael Tuexen 			break;
5219f8829a4aSRandall Stewart 		}		/* switch (ch->chunk_type) */
5220f8829a4aSRandall Stewart 
5221f8829a4aSRandall Stewart next_chunk:
5222f8829a4aSRandall Stewart 		/* get the next chunk */
5223f8829a4aSRandall Stewart 		*offset += SCTP_SIZE32(chk_length);
5224f8829a4aSRandall Stewart 		if (*offset >= length) {
5225f8829a4aSRandall Stewart 			/* no more data left in the mbuf chain */
5226f8829a4aSRandall Stewart 			break;
5227f8829a4aSRandall Stewart 		}
5228f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5229f8829a4aSRandall Stewart 		    sizeof(struct sctp_chunkhdr), chunk_buf);
5230f8829a4aSRandall Stewart 		if (ch == NULL) {
5231f8829a4aSRandall Stewart 			*offset = length;
523280a2d140SMichael Tuexen 			return (stcb);
5233f8829a4aSRandall Stewart 		}
5234f8829a4aSRandall Stewart 	}			/* while */
52352afb3e84SRandall Stewart 
5236469a65d1SMichael Tuexen 	if ((asconf_cnt > 0) && (stcb != NULL)) {
52372afb3e84SRandall Stewart 		sctp_send_asconf_ack(stcb);
52382afb3e84SRandall Stewart 	}
5239f8829a4aSRandall Stewart 	return (stcb);
5240f8829a4aSRandall Stewart }
5241f8829a4aSRandall Stewart 
5242f8829a4aSRandall Stewart /*
5243f8829a4aSRandall Stewart  * common input chunk processing (v4 and v6)
5244f8829a4aSRandall Stewart  */
52456e55db54SRandall Stewart void
5246b1754ad1SMichael Tuexen sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
5247b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
5248b1754ad1SMichael Tuexen     struct sctphdr *sh, struct sctp_chunkhdr *ch,
5249a8775ad9SMichael Tuexen     uint8_t compute_crc,
5250a8775ad9SMichael Tuexen     uint8_t ecn_bits,
5251d089f9b9SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
5252f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
5253f8829a4aSRandall Stewart {
5254ff1ffd74SMichael Tuexen 	char msg[SCTP_DIAG_INFO_LEN];
52554a2b92d9SMichael Tuexen 	struct mbuf *m = *mm, *op_err;
525655b175e7SMichael Tuexen 	struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
5257a8775ad9SMichael Tuexen 	struct sctp_tcb *stcb = NULL;
5258e3d6ef0bSMichael Tuexen 	struct sctp_nets *net = NULL;
52594a2b92d9SMichael Tuexen 	uint32_t high_tsn;
52604a2b92d9SMichael Tuexen 	uint32_t cksum_in_hdr;
52614a2b92d9SMichael Tuexen 	int un_sent;
52624a2b92d9SMichael Tuexen 	int cnt_ctrl_ready = 0;
52634a2b92d9SMichael Tuexen 	int fwd_tsn_seen = 0, data_processed = 0;
52644a2b92d9SMichael Tuexen 	bool cksum_validated, stcb_looked_up;
5265f8829a4aSRandall Stewart 
5266f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvdatagrams);
5267f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
5268f8829a4aSRandall Stewart 	sctp_audit_log(0xE0, 1);
5269f8829a4aSRandall Stewart 	sctp_auditing(0, inp, stcb, net);
5270f8829a4aSRandall Stewart #endif
5271f8829a4aSRandall Stewart 
52724a2b92d9SMichael Tuexen 	stcb_looked_up = false;
52734a2b92d9SMichael Tuexen 	if (compute_crc != 0) {
52744a2b92d9SMichael Tuexen 		cksum_validated = false;
52754a2b92d9SMichael Tuexen 		cksum_in_hdr = sh->checksum;
52764a2b92d9SMichael Tuexen 		if (cksum_in_hdr != htonl(0)) {
52774a2b92d9SMichael Tuexen 			uint32_t cksum_calculated;
52784a2b92d9SMichael Tuexen 
52794a2b92d9SMichael Tuexen 	validate_cksum:
5280a8775ad9SMichael Tuexen 			sh->checksum = 0;
52814a2b92d9SMichael Tuexen 			cksum_calculated = sctp_calculate_cksum(m, iphlen);
52824a2b92d9SMichael Tuexen 			sh->checksum = cksum_in_hdr;
52834a2b92d9SMichael Tuexen 			if (cksum_calculated != cksum_in_hdr) {
52844a2b92d9SMichael Tuexen 				if (stcb_looked_up) {
52854a2b92d9SMichael Tuexen 					/*
52864a2b92d9SMichael Tuexen 					 * The packet has a zero checksum,
52874a2b92d9SMichael Tuexen 					 * which is not the correct CRC, no
52884a2b92d9SMichael Tuexen 					 * stcb has been found or an stcb
52894a2b92d9SMichael Tuexen 					 * has been found but an incorrect
52904a2b92d9SMichael Tuexen 					 * zero checksum is not acceptable.
52914a2b92d9SMichael Tuexen 					 */
52924a2b92d9SMichael Tuexen 					KASSERT(cksum_in_hdr == htonl(0),
52934a2b92d9SMichael Tuexen 					    ("cksum in header not zero: %x",
52944a2b92d9SMichael Tuexen 					    ntohl(cksum_in_hdr)));
52954a2b92d9SMichael Tuexen 					if ((inp == NULL) &&
52964a2b92d9SMichael Tuexen 					    (SCTP_BASE_SYSCTL(sctp_ootb_with_zero_cksum) == 1)) {
52974a2b92d9SMichael Tuexen 						/*
52984a2b92d9SMichael Tuexen 						 * This is an OOTB packet,
52994a2b92d9SMichael Tuexen 						 * depending on the sysctl
53004a2b92d9SMichael Tuexen 						 * variable, pretend that
53014a2b92d9SMichael Tuexen 						 * the checksum is
53024a2b92d9SMichael Tuexen 						 * acceptable, to allow an
53034a2b92d9SMichael Tuexen 						 * appropriate response
53044a2b92d9SMichael Tuexen 						 * (ABORT, for examlpe) to
53054a2b92d9SMichael Tuexen 						 * be sent.
53064a2b92d9SMichael Tuexen 						 */
53074a2b92d9SMichael Tuexen 						KASSERT(stcb == NULL,
53084a2b92d9SMichael Tuexen 						    ("stcb is %p", stcb));
53094a2b92d9SMichael Tuexen 						SCTP_STAT_INCR(sctps_recvzerocrc);
53104a2b92d9SMichael Tuexen 						goto cksum_validated;
53114a2b92d9SMichael Tuexen 					}
53124a2b92d9SMichael Tuexen 				} else {
5313a8775ad9SMichael Tuexen 					stcb = sctp_findassociation_addr(m, offset, src, dst,
5314a8775ad9SMichael Tuexen 					    sh, ch, &inp, &net, vrf_id);
53154a2b92d9SMichael Tuexen 				}
53164a2b92d9SMichael Tuexen 				SCTPDBG(SCTP_DEBUG_INPUT1, "Bad cksum in SCTP packet:%x calculated:%x m:%p mlen:%d iphlen:%d\n",
53174a2b92d9SMichael Tuexen 				    ntohl(cksum_in_hdr), ntohl(cksum_calculated), (void *)m, length, iphlen);
53184474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
53193cf729a9SMichael Tuexen 				if ((ch->chunk_type != SCTP_INITIATION) &&
53203cf729a9SMichael Tuexen 				    (net != NULL) && (net->port != port)) {
5321a8775ad9SMichael Tuexen 					if (net->port == 0) {
53224a2b92d9SMichael Tuexen 						/*
53234a2b92d9SMichael Tuexen 						 * UDP encapsulation turned
53244a2b92d9SMichael Tuexen 						 * on.
53254a2b92d9SMichael Tuexen 						 */
53263cf729a9SMichael Tuexen 						net->mtu -= sizeof(struct udphdr);
53273cf729a9SMichael Tuexen 						if (stcb->asoc.smallest_mtu > net->mtu) {
53282de2ae33SMichael Tuexen 							sctp_pathmtu_adjustment(stcb, net->mtu, true);
53293cf729a9SMichael Tuexen 						}
53303cf729a9SMichael Tuexen 					} else if (port == 0) {
53314a2b92d9SMichael Tuexen 						/*
53324a2b92d9SMichael Tuexen 						 * UDP encapsulation turned
53334a2b92d9SMichael Tuexen 						 * off.
53344a2b92d9SMichael Tuexen 						 */
53353cf729a9SMichael Tuexen 						net->mtu += sizeof(struct udphdr);
53363cf729a9SMichael Tuexen 						/* XXX Update smallest_mtu */
5337a8775ad9SMichael Tuexen 					}
5338a8775ad9SMichael Tuexen 					net->port = port;
5339a8775ad9SMichael Tuexen 				}
53404474d71aSMichael Tuexen #endif
53415fe29cdfSMichael Tuexen 				if (net != NULL) {
5342457b4b88SMichael Tuexen 					net->flowtype = mflowtype;
53435fe29cdfSMichael Tuexen 					net->flowid = mflowid;
5344a8775ad9SMichael Tuexen 				}
53451e88cc8bSMichael Tuexen 				SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5346a8775ad9SMichael Tuexen 				if ((inp != NULL) && (stcb != NULL)) {
53474a2b92d9SMichael Tuexen 					if (stcb->asoc.pktdrop_supported) {
5348a8775ad9SMichael Tuexen 						sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
5349a8775ad9SMichael Tuexen 						sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
53504a2b92d9SMichael Tuexen 					}
5351a8775ad9SMichael Tuexen 				} else if ((inp != NULL) && (stcb == NULL)) {
5352a8775ad9SMichael Tuexen 					inp_decr = inp;
5353a8775ad9SMichael Tuexen 				}
5354a8775ad9SMichael Tuexen 				SCTP_STAT_INCR(sctps_badsum);
5355a8775ad9SMichael Tuexen 				SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5356a8775ad9SMichael Tuexen 				goto out;
53574a2b92d9SMichael Tuexen 			} else {
53584a2b92d9SMichael Tuexen 				cksum_validated = true;
5359a8775ad9SMichael Tuexen 			}
5360a8775ad9SMichael Tuexen 		}
53614a2b92d9SMichael Tuexen 		KASSERT(cksum_validated || cksum_in_hdr == htonl(0),
53624a2b92d9SMichael Tuexen 		    ("cksum 0x%08x not zero and not validated", ntohl(cksum_in_hdr)));
53634a2b92d9SMichael Tuexen 		if (!cksum_validated) {
5364a8775ad9SMichael Tuexen 			stcb = sctp_findassociation_addr(m, offset, src, dst,
5365a8775ad9SMichael Tuexen 			    sh, ch, &inp, &net, vrf_id);
53664a2b92d9SMichael Tuexen 			stcb_looked_up = true;
53674a2b92d9SMichael Tuexen 			if ((stcb == NULL) || (stcb->asoc.zero_checksum == 0)) {
53684a2b92d9SMichael Tuexen 				goto validate_cksum;
53694a2b92d9SMichael Tuexen 			}
53704a2b92d9SMichael Tuexen 			SCTP_STAT_INCR(sctps_recvzerocrc);
53714a2b92d9SMichael Tuexen 		}
53724a2b92d9SMichael Tuexen 	}
53734a2b92d9SMichael Tuexen cksum_validated:
53744a2b92d9SMichael Tuexen 	/* Destination port of 0 is illegal, based on RFC4960. */
53754a2b92d9SMichael Tuexen 	if (sh->dest_port == htons(0)) {
53764a2b92d9SMichael Tuexen 		SCTP_STAT_INCR(sctps_hdrops);
53774a2b92d9SMichael Tuexen 		if ((stcb == NULL) && (inp != NULL)) {
53784a2b92d9SMichael Tuexen 			inp_decr = inp;
53794a2b92d9SMichael Tuexen 		}
53804a2b92d9SMichael Tuexen 		goto out;
53814a2b92d9SMichael Tuexen 	}
53824a2b92d9SMichael Tuexen 	if (!stcb_looked_up) {
53834a2b92d9SMichael Tuexen 		stcb = sctp_findassociation_addr(m, offset, src, dst,
53844a2b92d9SMichael Tuexen 		    sh, ch, &inp, &net, vrf_id);
53854a2b92d9SMichael Tuexen 	}
53864474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
53873cf729a9SMichael Tuexen 	if ((ch->chunk_type != SCTP_INITIATION) &&
53883cf729a9SMichael Tuexen 	    (net != NULL) && (net->port != port)) {
5389a8775ad9SMichael Tuexen 		if (net->port == 0) {
53903cf729a9SMichael Tuexen 			/* UDP encapsulation turned on. */
53913cf729a9SMichael Tuexen 			net->mtu -= sizeof(struct udphdr);
53923cf729a9SMichael Tuexen 			if (stcb->asoc.smallest_mtu > net->mtu) {
53932de2ae33SMichael Tuexen 				sctp_pathmtu_adjustment(stcb, net->mtu, true);
53943cf729a9SMichael Tuexen 			}
53953cf729a9SMichael Tuexen 		} else if (port == 0) {
53963cf729a9SMichael Tuexen 			/* UDP encapsulation turned off. */
53973cf729a9SMichael Tuexen 			net->mtu += sizeof(struct udphdr);
53983cf729a9SMichael Tuexen 			/* XXX Update smallest_mtu */
5399a8775ad9SMichael Tuexen 		}
5400a8775ad9SMichael Tuexen 		net->port = port;
5401a8775ad9SMichael Tuexen 	}
54024474d71aSMichael Tuexen #endif
54035fe29cdfSMichael Tuexen 	if (net != NULL) {
5404457b4b88SMichael Tuexen 		net->flowtype = mflowtype;
54055fe29cdfSMichael Tuexen 		net->flowid = mflowid;
5406a8775ad9SMichael Tuexen 	}
5407a8775ad9SMichael Tuexen 	if (inp == NULL) {
5408a8775ad9SMichael Tuexen 		SCTP_STAT_INCR(sctps_noport);
54094a2b92d9SMichael Tuexen 		SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5410a8775ad9SMichael Tuexen 		if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
5411a8775ad9SMichael Tuexen 			goto out;
5412a8775ad9SMichael Tuexen 		}
5413a8775ad9SMichael Tuexen 		if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5414502d5e85SMichael Tuexen 			SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
5415a8775ad9SMichael Tuexen 			sctp_send_shutdown_complete2(src, dst, sh,
5416d089f9b9SMichael Tuexen 			    mflowtype, mflowid, fibnum,
5417a8775ad9SMichael Tuexen 			    vrf_id, port);
5418a8775ad9SMichael Tuexen 			goto out;
5419a8775ad9SMichael Tuexen 		}
5420a8775ad9SMichael Tuexen 		if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5421502d5e85SMichael Tuexen 			SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
5422a8775ad9SMichael Tuexen 			goto out;
5423a8775ad9SMichael Tuexen 		}
5424a8775ad9SMichael Tuexen 		if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
5425a8775ad9SMichael Tuexen 			if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
5426a8775ad9SMichael Tuexen 			    ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
5427a8775ad9SMichael Tuexen 			    (ch->chunk_type != SCTP_INIT))) {
5428ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5429ff1ffd74SMichael Tuexen 				    "Out of the blue");
5430a8775ad9SMichael Tuexen 				sctp_send_abort(m, iphlen, src, dst,
5431ff1ffd74SMichael Tuexen 				    sh, 0, op_err,
5432d089f9b9SMichael Tuexen 				    mflowtype, mflowid, fibnum,
5433a8775ad9SMichael Tuexen 				    vrf_id, port);
5434a8775ad9SMichael Tuexen 			}
5435a8775ad9SMichael Tuexen 		}
5436a8775ad9SMichael Tuexen 		goto out;
5437a8775ad9SMichael Tuexen 	} else if (stcb == NULL) {
5438a8775ad9SMichael Tuexen 		inp_decr = inp;
5439a8775ad9SMichael Tuexen 	}
5440b3f1ea41SRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5441dd294dceSMichael Tuexen 	    (void *)m, iphlen, offset, length, (void *)stcb);
5442f8829a4aSRandall Stewart 	if (stcb) {
5443f8829a4aSRandall Stewart 		/* always clear this before beginning a packet */
5444f8829a4aSRandall Stewart 		stcb->asoc.authenticated = 0;
5445f8829a4aSRandall Stewart 		stcb->asoc.seen_a_sack_this_pkt = 0;
54462afb3e84SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5447dd294dceSMichael Tuexen 		    (void *)stcb, stcb->asoc.state);
54482afb3e84SRandall Stewart 
5449c4739e2fSRandall Stewart 		if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5450c4739e2fSRandall Stewart 		    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
545163981c2bSRandall Stewart 			/*-
545263981c2bSRandall Stewart 			 * If we hit here, we had a ref count
545363981c2bSRandall Stewart 			 * up when the assoc was aborted and the
545463981c2bSRandall Stewart 			 * timer is clearing out the assoc, we should
545563981c2bSRandall Stewart 			 * NOT respond to any packet.. its OOTB.
545663981c2bSRandall Stewart 			 */
545763981c2bSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
5458a8775ad9SMichael Tuexen 			stcb = NULL;
54591e88cc8bSMichael Tuexen 			SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5460999f86d6SMichael Tuexen 			SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5461ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5462ff1ffd74SMichael Tuexen 			    msg);
5463ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5464d089f9b9SMichael Tuexen 			    mflowtype, mflowid, inp->fibnum,
5465c54a18d2SRandall Stewart 			    vrf_id, port);
5466a8775ad9SMichael Tuexen 			goto out;
546763981c2bSRandall Stewart 		}
5468f8829a4aSRandall Stewart 	}
5469f8829a4aSRandall Stewart 	if (IS_SCTP_CONTROL(ch)) {
5470f8829a4aSRandall Stewart 		/* process the control portion of the SCTP packet */
54713c503c28SRandall Stewart 		/* sa_ignore NO_NULL_CHK */
5472b1754ad1SMichael Tuexen 		stcb = sctp_process_control(m, iphlen, &offset, length,
5473b1754ad1SMichael Tuexen 		    src, dst, sh, ch,
5474f30ac432SMichael Tuexen 		    inp, stcb, &net, &fwd_tsn_seen,
5475d089f9b9SMichael Tuexen 		    mflowtype, mflowid, fibnum,
5476f30ac432SMichael Tuexen 		    vrf_id, port);
5477f8829a4aSRandall Stewart 		if (stcb) {
5478f8829a4aSRandall Stewart 			/*
5479f8829a4aSRandall Stewart 			 * This covers us if the cookie-echo was there and
5480f8829a4aSRandall Stewart 			 * it changes our INP.
5481f8829a4aSRandall Stewart 			 */
5482f8829a4aSRandall Stewart 			inp = stcb->sctp_ep;
54834474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
54843cf729a9SMichael Tuexen 			if ((ch->chunk_type != SCTP_INITIATION) &&
54853cf729a9SMichael Tuexen 			    (net != NULL) && (net->port != port)) {
5486b3f1ea41SRandall Stewart 				if (net->port == 0) {
54873cf729a9SMichael Tuexen 					/* UDP encapsulation turned on. */
54883cf729a9SMichael Tuexen 					net->mtu -= sizeof(struct udphdr);
54893cf729a9SMichael Tuexen 					if (stcb->asoc.smallest_mtu > net->mtu) {
54902de2ae33SMichael Tuexen 						sctp_pathmtu_adjustment(stcb, net->mtu, true);
54913cf729a9SMichael Tuexen 					}
54923cf729a9SMichael Tuexen 				} else if (port == 0) {
54933cf729a9SMichael Tuexen 					/* UDP encapsulation turned off. */
54943cf729a9SMichael Tuexen 					net->mtu += sizeof(struct udphdr);
54953cf729a9SMichael Tuexen 					/* XXX Update smallest_mtu */
5496b3f1ea41SRandall Stewart 				}
5497b3f1ea41SRandall Stewart 				net->port = port;
5498b3f1ea41SRandall Stewart 			}
54994474d71aSMichael Tuexen #endif
5500f8829a4aSRandall Stewart 		}
5501f8829a4aSRandall Stewart 	} else {
5502f8829a4aSRandall Stewart 		/*
5503f8829a4aSRandall Stewart 		 * no control chunks, so pre-process DATA chunks (these
5504f8829a4aSRandall Stewart 		 * checks are taken care of by control processing)
5505f8829a4aSRandall Stewart 		 */
5506f8829a4aSRandall Stewart 
5507f8829a4aSRandall Stewart 		/*
5508f8829a4aSRandall Stewart 		 * if DATA only packet, and auth is required, then punt...
5509f8829a4aSRandall Stewart 		 * can't have authenticated without any AUTH (control)
5510f8829a4aSRandall Stewart 		 * chunks
5511f8829a4aSRandall Stewart 		 */
5512b3f1ea41SRandall Stewart 		if ((stcb != NULL) &&
5513b3f1ea41SRandall Stewart 		    sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5514f8829a4aSRandall Stewart 			/* "silently" ignore */
55151e88cc8bSMichael Tuexen 			SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5516f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvauthmissing);
5517a8775ad9SMichael Tuexen 			goto out;
5518f8829a4aSRandall Stewart 		}
5519f8829a4aSRandall Stewart 		if (stcb == NULL) {
5520f8829a4aSRandall Stewart 			/* out of the blue DATA chunk */
55211e88cc8bSMichael Tuexen 			SCTP_PROBE5(receive, NULL, NULL, m, NULL, sh);
5522999f86d6SMichael Tuexen 			SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5523ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5524ff1ffd74SMichael Tuexen 			    msg);
5525ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5526d089f9b9SMichael Tuexen 			    mflowtype, mflowid, fibnum,
5527c54a18d2SRandall Stewart 			    vrf_id, port);
5528a8775ad9SMichael Tuexen 			goto out;
5529f8829a4aSRandall Stewart 		}
5530f8829a4aSRandall Stewart 		if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5531f8829a4aSRandall Stewart 			/* v_tag mismatch! */
55321e88cc8bSMichael Tuexen 			SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5533f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_badvtag);
5534a8775ad9SMichael Tuexen 			goto out;
5535f8829a4aSRandall Stewart 		}
5536f8829a4aSRandall Stewart 	}
5537f8829a4aSRandall Stewart 
55381e88cc8bSMichael Tuexen 	SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5539f8829a4aSRandall Stewart 	if (stcb == NULL) {
5540f8829a4aSRandall Stewart 		/*
5541f8829a4aSRandall Stewart 		 * no valid TCB for this packet, or we found it's a bad
5542f8829a4aSRandall Stewart 		 * packet while processing control, or we're done with this
5543f8829a4aSRandall Stewart 		 * packet (done or skip rest of data), so we drop it...
5544f8829a4aSRandall Stewart 		 */
5545a8775ad9SMichael Tuexen 		goto out;
5546f8829a4aSRandall Stewart 	}
55470053ed28SMichael Tuexen 
5548f8829a4aSRandall Stewart 	/*
5549f8829a4aSRandall Stewart 	 * DATA chunk processing
5550f8829a4aSRandall Stewart 	 */
5551f8829a4aSRandall Stewart 	/* plow through the data chunks while length > offset */
5552f8829a4aSRandall Stewart 
5553f8829a4aSRandall Stewart 	/*
5554f8829a4aSRandall Stewart 	 * Rest should be DATA only.  Check authentication state if AUTH for
5555f8829a4aSRandall Stewart 	 * DATA is required.
5556f8829a4aSRandall Stewart 	 */
5557b3f1ea41SRandall Stewart 	if ((length > offset) &&
5558b3f1ea41SRandall Stewart 	    (stcb != NULL) &&
5559b3f1ea41SRandall Stewart 	    sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5560f8829a4aSRandall Stewart 	    !stcb->asoc.authenticated) {
5561f8829a4aSRandall Stewart 		/* "silently" ignore */
5562f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvauthmissing);
5563ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_AUTH1,
5564ad81507eSRandall Stewart 		    "Data chunk requires AUTH, skipped\n");
5565a5d547adSRandall Stewart 		goto trigger_send;
5566f8829a4aSRandall Stewart 	}
5567f8829a4aSRandall Stewart 	if (length > offset) {
5568f8829a4aSRandall Stewart 		int retval;
5569f8829a4aSRandall Stewart 
5570f8829a4aSRandall Stewart 		/*
5571f8829a4aSRandall Stewart 		 * First check to make sure our state is correct. We would
5572f8829a4aSRandall Stewart 		 * not get here unless we really did have a tag, so we don't
5573f8829a4aSRandall Stewart 		 * abort if this happens, just dump the chunk silently.
5574f8829a4aSRandall Stewart 		 */
5575839d21d6SMichael Tuexen 		switch (SCTP_GET_STATE(stcb)) {
5576f8829a4aSRandall Stewart 		case SCTP_STATE_COOKIE_ECHOED:
5577f8829a4aSRandall Stewart 			/*
5578f8829a4aSRandall Stewart 			 * we consider data with valid tags in this state
5579f8829a4aSRandall Stewart 			 * shows us the cookie-ack was lost. Imply it was
5580f8829a4aSRandall Stewart 			 * there.
5581f8829a4aSRandall Stewart 			 */
5582f8829a4aSRandall Stewart 			sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5583f8829a4aSRandall Stewart 			break;
5584f8829a4aSRandall Stewart 		case SCTP_STATE_COOKIE_WAIT:
5585f8829a4aSRandall Stewart 			/*
5586f8829a4aSRandall Stewart 			 * We consider OOTB any data sent during asoc setup.
5587f8829a4aSRandall Stewart 			 */
5588999f86d6SMichael Tuexen 			SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5589ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5590ff1ffd74SMichael Tuexen 			    msg);
5591ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5592d089f9b9SMichael Tuexen 			    mflowtype, mflowid, inp->fibnum,
5593c54a18d2SRandall Stewart 			    vrf_id, port);
5594a8775ad9SMichael Tuexen 			goto out;
559552be287eSRandall Stewart 			/* sa_ignore NOTREACHED */
5596f8829a4aSRandall Stewart 			break;
5597f8829a4aSRandall Stewart 		case SCTP_STATE_EMPTY:	/* should not happen */
5598f8829a4aSRandall Stewart 		case SCTP_STATE_INUSE:	/* should not happen */
5599f8829a4aSRandall Stewart 		case SCTP_STATE_SHUTDOWN_RECEIVED:	/* This is a peer error */
5600f8829a4aSRandall Stewart 		case SCTP_STATE_SHUTDOWN_ACK_SENT:
5601f8829a4aSRandall Stewart 		default:
5602a8775ad9SMichael Tuexen 			goto out;
560352be287eSRandall Stewart 			/* sa_ignore NOTREACHED */
5604f8829a4aSRandall Stewart 			break;
5605f8829a4aSRandall Stewart 		case SCTP_STATE_OPEN:
5606f8829a4aSRandall Stewart 		case SCTP_STATE_SHUTDOWN_SENT:
5607f8829a4aSRandall Stewart 			break;
5608f8829a4aSRandall Stewart 		}
5609f8829a4aSRandall Stewart 		/* plow through the data chunks while length > offset */
5610b1754ad1SMichael Tuexen 		retval = sctp_process_data(mm, iphlen, &offset, length,
5611e7e71dd7SMichael Tuexen 		    inp, stcb, net, &high_tsn);
5612f8829a4aSRandall Stewart 		if (retval == 2) {
5613f8829a4aSRandall Stewart 			/*
5614f8829a4aSRandall Stewart 			 * The association aborted, NO UNLOCK needed since
5615f8829a4aSRandall Stewart 			 * the association is destroyed.
5616f8829a4aSRandall Stewart 			 */
5617a8775ad9SMichael Tuexen 			stcb = NULL;
5618a8775ad9SMichael Tuexen 			goto out;
5619f8829a4aSRandall Stewart 		}
5620b954d816SMichael Tuexen 		if (retval == 0) {
5621f8829a4aSRandall Stewart 			data_processed = 1;
5622b954d816SMichael Tuexen 		}
5623f8829a4aSRandall Stewart 		/*
5624f8829a4aSRandall Stewart 		 * Anything important needs to have been m_copy'ed in
5625f8829a4aSRandall Stewart 		 * process_data
5626f8829a4aSRandall Stewart 		 */
5627f8829a4aSRandall Stewart 	}
56280053ed28SMichael Tuexen 
5629493d8e5aSRandall Stewart 	/* take care of ecn */
563060990c0cSMichael Tuexen 	if ((data_processed == 1) &&
5631f342355aSMichael Tuexen 	    (stcb->asoc.ecn_supported == 1) &&
5632c446091bSMichael Tuexen 	    ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5633493d8e5aSRandall Stewart 		/* Yep, we need to add a ECNE */
5634493d8e5aSRandall Stewart 		sctp_send_ecn_echo(stcb, net, high_tsn);
5635493d8e5aSRandall Stewart 	}
56360053ed28SMichael Tuexen 
5637f8829a4aSRandall Stewart 	if ((data_processed == 0) && (fwd_tsn_seen)) {
56388f777478SMichael Tuexen 		int was_a_gap;
56398f777478SMichael Tuexen 		uint32_t highest_tsn;
5640f8829a4aSRandall Stewart 
564120b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
56428f777478SMichael Tuexen 			highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
56438f777478SMichael Tuexen 		} else {
56448f777478SMichael Tuexen 			highest_tsn = stcb->asoc.highest_tsn_inside_map;
5645f8829a4aSRandall Stewart 		}
564620b07a4dSMichael Tuexen 		was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
56478933fa13SRandall Stewart 		stcb->asoc.send_sack = 1;
56487215cc1bSMichael Tuexen 		sctp_sack_check(stcb, was_a_gap);
56498933fa13SRandall Stewart 	} else if (fwd_tsn_seen) {
56508933fa13SRandall Stewart 		stcb->asoc.send_sack = 1;
5651f8829a4aSRandall Stewart 	}
5652f8829a4aSRandall Stewart 	/* trigger send of any chunks in queue... */
5653a5d547adSRandall Stewart trigger_send:
5654f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
5655f8829a4aSRandall Stewart 	sctp_audit_log(0xE0, 2);
5656f8829a4aSRandall Stewart 	sctp_auditing(1, inp, stcb, net);
5657f8829a4aSRandall Stewart #endif
5658ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT1,
5659ad81507eSRandall Stewart 	    "Check for chunk output prw:%d tqe:%d tf=%d\n",
5660f8829a4aSRandall Stewart 	    stcb->asoc.peers_rwnd,
5661f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5662f8829a4aSRandall Stewart 	    stcb->asoc.total_flight);
5663f8829a4aSRandall Stewart 	un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5664493d8e5aSRandall Stewart 	if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
5665493d8e5aSRandall Stewart 		cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
5666493d8e5aSRandall Stewart 	}
56675114dccbSMichael Tuexen 	if (!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue) ||
56685114dccbSMichael Tuexen 	    cnt_ctrl_ready ||
56695114dccbSMichael Tuexen 	    stcb->asoc.trigger_reset ||
567064c8fc5dSMichael Tuexen 	    ((un_sent > 0) &&
567164c8fc5dSMichael Tuexen 	    (stcb->asoc.peers_rwnd > 0 || stcb->asoc.total_flight == 0))) {
5672ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5673ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5674ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5675f8829a4aSRandall Stewart 	}
5676f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
5677f8829a4aSRandall Stewart 	sctp_audit_log(0xE0, 3);
5678f8829a4aSRandall Stewart 	sctp_auditing(2, inp, stcb, net);
5679f8829a4aSRandall Stewart #endif
5680a8775ad9SMichael Tuexen out:
5681a8775ad9SMichael Tuexen 	if (stcb != NULL) {
5682f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
5683a8775ad9SMichael Tuexen 	}
5684a8775ad9SMichael Tuexen 	if (inp_decr != NULL) {
5685a8775ad9SMichael Tuexen 		/* reduce ref-count */
5686a8775ad9SMichael Tuexen 		SCTP_INP_WLOCK(inp_decr);
5687a8775ad9SMichael Tuexen 		SCTP_INP_DECR_REF(inp_decr);
5688a8775ad9SMichael Tuexen 		SCTP_INP_WUNLOCK(inp_decr);
5689a8775ad9SMichael Tuexen 	}
56906e55db54SRandall Stewart 	return;
5691f8829a4aSRandall Stewart }
5692f8829a4aSRandall Stewart 
5693e6194c2eSMichael Tuexen #ifdef INET
5694f8829a4aSRandall Stewart void
5695af83f5d7SRoman Divacky sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
5696f8829a4aSRandall Stewart {
5697139bc87fSRandall Stewart 	struct mbuf *m;
5698f8829a4aSRandall Stewart 	int iphlen;
5699ad21a364SRandall Stewart 	uint32_t vrf_id = 0;
5700f8829a4aSRandall Stewart 	uint8_t ecn_bits;
5701b1754ad1SMichael Tuexen 	struct sockaddr_in src, dst;
5702f8829a4aSRandall Stewart 	struct ip *ip;
5703f8829a4aSRandall Stewart 	struct sctphdr *sh;
5704f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
57056dc5aabcSMichael Tuexen 	int length, offset;
5706a8775ad9SMichael Tuexen 	uint8_t compute_crc;
5707a8775ad9SMichael Tuexen 	uint32_t mflowid;
5708457b4b88SMichael Tuexen 	uint8_t mflowtype;
5709d089f9b9SMichael Tuexen 	uint16_t fibnum;
57109c7635e1SMichael Tuexen 
57116dc5aabcSMichael Tuexen 	iphlen = off;
571217205eccSRandall Stewart 	if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
571317205eccSRandall Stewart 		SCTP_RELEASE_PKT(i_pak);
571417205eccSRandall Stewart 		return;
571517205eccSRandall Stewart 	}
5716139bc87fSRandall Stewart 	m = SCTP_HEADER_TO_CHAIN(i_pak);
5717f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
5718f8829a4aSRandall Stewart 	/* Log in any input mbufs */
5719b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
57204be807c4SMichael Tuexen 		sctp_log_mbc(m, SCTP_MBUF_INPUT);
572180fefe0aSRandall Stewart 	}
5722f8829a4aSRandall Stewart #endif
5723207304d4SRandall Stewart #ifdef SCTP_PACKET_LOGGING
57246dc5aabcSMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
5725f9384252SMichael Tuexen 		sctp_packet_log(m);
57266dc5aabcSMichael Tuexen 	}
5727207304d4SRandall Stewart #endif
5728a8775ad9SMichael Tuexen 	SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
57291a94cdbeSMichael Tuexen 	    "sctp_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
5730a8775ad9SMichael Tuexen 	    m->m_pkthdr.len,
5731a8775ad9SMichael Tuexen 	    if_name(m->m_pkthdr.rcvif),
57321a94cdbeSMichael Tuexen 	    (int)m->m_pkthdr.csum_flags, CSUM_BITS);
5733f30ac432SMichael Tuexen 	mflowid = m->m_pkthdr.flowid;
5734457b4b88SMichael Tuexen 	mflowtype = M_HASHTYPE_GET(m);
5735d089f9b9SMichael Tuexen 	fibnum = M_GETFIB(m);
57366dc5aabcSMichael Tuexen 	SCTP_STAT_INCR(sctps_recvpackets);
57376dc5aabcSMichael Tuexen 	SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
57386dc5aabcSMichael Tuexen 	/* Get IP, SCTP, and first chunk header together in the first mbuf. */
57396dc5aabcSMichael Tuexen 	offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5740139bc87fSRandall Stewart 	if (SCTP_BUF_LEN(m) < offset) {
5741b1754ad1SMichael Tuexen 		if ((m = m_pullup(m, offset)) == NULL) {
5742f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_hdrops);
5743f8829a4aSRandall Stewart 			return;
5744f8829a4aSRandall Stewart 		}
5745f8829a4aSRandall Stewart 	}
5746b1754ad1SMichael Tuexen 	ip = mtod(m, struct ip *);
57476dc5aabcSMichael Tuexen 	sh = (struct sctphdr *)((caddr_t)ip + iphlen);
57486dc5aabcSMichael Tuexen 	ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
57496dc5aabcSMichael Tuexen 	offset -= sizeof(struct sctp_chunkhdr);
5750b1754ad1SMichael Tuexen 	memset(&src, 0, sizeof(struct sockaddr_in));
5751b1754ad1SMichael Tuexen 	src.sin_family = AF_INET;
5752b1754ad1SMichael Tuexen 	src.sin_len = sizeof(struct sockaddr_in);
5753b1754ad1SMichael Tuexen 	src.sin_port = sh->src_port;
5754b1754ad1SMichael Tuexen 	src.sin_addr = ip->ip_src;
5755b1754ad1SMichael Tuexen 	memset(&dst, 0, sizeof(struct sockaddr_in));
5756b1754ad1SMichael Tuexen 	dst.sin_family = AF_INET;
5757b1754ad1SMichael Tuexen 	dst.sin_len = sizeof(struct sockaddr_in);
5758b1754ad1SMichael Tuexen 	dst.sin_port = sh->dest_port;
5759b1754ad1SMichael Tuexen 	dst.sin_addr = ip->ip_dst;
57608ad458a4SGleb Smirnoff 	length = ntohs(ip->ip_len);
57616dc5aabcSMichael Tuexen 	/* Validate mbuf chain length with IP payload length. */
5762a8775ad9SMichael Tuexen 	if (SCTP_HEADER_LEN(m) != length) {
57636dc5aabcSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT1,
5764a8775ad9SMichael Tuexen 		    "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
5765a99b6783SRandall Stewart 		SCTP_STAT_INCR(sctps_hdrops);
5766a8775ad9SMichael Tuexen 		goto out;
5767a99b6783SRandall Stewart 	}
5768f8829a4aSRandall Stewart 	/* SCTP does not allow broadcasts or multicasts */
5769b1754ad1SMichael Tuexen 	if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
5770a8775ad9SMichael Tuexen 		goto out;
5771f8829a4aSRandall Stewart 	}
5772b1754ad1SMichael Tuexen 	if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
5773a8775ad9SMichael Tuexen 		goto out;
5774f8829a4aSRandall Stewart 	}
5775a8775ad9SMichael Tuexen 	ecn_bits = ip->ip_tos;
5776a99b6783SRandall Stewart 	if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
5777a99b6783SRandall Stewart 		SCTP_STAT_INCR(sctps_recvhwcrc);
5778a8775ad9SMichael Tuexen 		compute_crc = 0;
5779a8775ad9SMichael Tuexen 	} else {
5780a99b6783SRandall Stewart 		SCTP_STAT_INCR(sctps_recvswcrc);
5781a8775ad9SMichael Tuexen 		compute_crc = 1;
5782f8829a4aSRandall Stewart 	}
5783b1754ad1SMichael Tuexen 	sctp_common_input_processing(&m, iphlen, offset, length,
5784b1754ad1SMichael Tuexen 	    (struct sockaddr *)&src,
5785b1754ad1SMichael Tuexen 	    (struct sockaddr *)&dst,
5786a8775ad9SMichael Tuexen 	    sh, ch,
5787a8775ad9SMichael Tuexen 	    compute_crc,
5788a8775ad9SMichael Tuexen 	    ecn_bits,
5789d089f9b9SMichael Tuexen 	    mflowtype, mflowid, fibnum,
5790f30ac432SMichael Tuexen 	    vrf_id, port);
5791a8775ad9SMichael Tuexen out:
5792f8829a4aSRandall Stewart 	if (m) {
5793f8829a4aSRandall Stewart 		sctp_m_freem(m);
5794f8829a4aSRandall Stewart 	}
5795f8829a4aSRandall Stewart 	return;
5796f8829a4aSRandall Stewart }
5797bfc46083SRandall Stewart 
57982f9e6db0SMichael Tuexen #if defined(SCTP_MCORE_INPUT) && defined(SMP)
57990071ee5eSRandall Stewart extern int *sctp_cpuarry;
58000071ee5eSRandall Stewart #endif
5801bfc46083SRandall Stewart 
58028f5a8818SKevin Lo int
580382eaf95eSMichael Tuexen sctp_input(struct mbuf **mp, int *offp, int proto SCTP_UNUSED)
5804c54a18d2SRandall Stewart {
58058f5a8818SKevin Lo 	struct mbuf *m;
58068f5a8818SKevin Lo 	int off;
58078f5a8818SKevin Lo 
58088f5a8818SKevin Lo 	m = *mp;
58098f5a8818SKevin Lo 	off = *offp;
58102f9e6db0SMichael Tuexen #if defined(SCTP_MCORE_INPUT) && defined(SMP)
581182eaf95eSMichael Tuexen 	if (mp_ncpus > 1) {
5812bfc46083SRandall Stewart 		struct ip *ip;
5813bfc46083SRandall Stewart 		struct sctphdr *sh;
5814bfc46083SRandall Stewart 		int offset;
5815bfc46083SRandall Stewart 		int cpu_to_use;
581638521fb9SRandall Stewart 		uint32_t flowid, tag;
5817bfc46083SRandall Stewart 
5818457b4b88SMichael Tuexen 		if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
581938521fb9SRandall Stewart 			flowid = m->m_pkthdr.flowid;
582038521fb9SRandall Stewart 		} else {
582138521fb9SRandall Stewart 			/*
582238521fb9SRandall Stewart 			 * No flow id built by lower layers fix it so we
582338521fb9SRandall Stewart 			 * create one.
582438521fb9SRandall Stewart 			 */
5825b1754ad1SMichael Tuexen 			offset = off + sizeof(struct sctphdr);
5826bfc46083SRandall Stewart 			if (SCTP_BUF_LEN(m) < offset) {
5827b1754ad1SMichael Tuexen 				if ((m = m_pullup(m, offset)) == NULL) {
5828bfc46083SRandall Stewart 					SCTP_STAT_INCR(sctps_hdrops);
58298f5a8818SKevin Lo 					return (IPPROTO_DONE);
5830bfc46083SRandall Stewart 				}
5831bfc46083SRandall Stewart 			}
5832b1754ad1SMichael Tuexen 			ip = mtod(m, struct ip *);
5833bfc46083SRandall Stewart 			sh = (struct sctphdr *)((caddr_t)ip + off);
58340071ee5eSRandall Stewart 			tag = htonl(sh->v_tag);
583538521fb9SRandall Stewart 			flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
583638521fb9SRandall Stewart 			m->m_pkthdr.flowid = flowid;
583736ad8372SSepherosa Ziehau 			M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE_HASH);
58380071ee5eSRandall Stewart 		}
583938521fb9SRandall Stewart 		cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
5840bfc46083SRandall Stewart 		sctp_queue_to_mcore(m, off, cpu_to_use);
58418f5a8818SKevin Lo 		return (IPPROTO_DONE);
5842bfc46083SRandall Stewart 	}
5843bfc46083SRandall Stewart #endif
5844bfc46083SRandall Stewart 	sctp_input_with_port(m, off, 0);
58458f5a8818SKevin Lo 	return (IPPROTO_DONE);
5846c54a18d2SRandall Stewart }
5847e6194c2eSMichael Tuexen #endif
5848