xref: /freebsd/sys/netinet/sctp_input.c (revision 1095da75032b439d893c0947eda2f3738ecfe494)
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 <netinet/sctp_os.h>
36f8829a4aSRandall Stewart #include <netinet/sctp_var.h>
3742551e99SRandall Stewart #include <netinet/sctp_sysctl.h>
38f8829a4aSRandall Stewart #include <netinet/sctp_pcb.h>
39f8829a4aSRandall Stewart #include <netinet/sctp_header.h>
40f8829a4aSRandall Stewart #include <netinet/sctputil.h>
41f8829a4aSRandall Stewart #include <netinet/sctp_output.h>
42f8829a4aSRandall Stewart #include <netinet/sctp_input.h>
43f8829a4aSRandall Stewart #include <netinet/sctp_auth.h>
44f8829a4aSRandall Stewart #include <netinet/sctp_indata.h>
45f8829a4aSRandall Stewart #include <netinet/sctp_asconf.h>
46207304d4SRandall Stewart #include <netinet/sctp_bsd_addr.h>
47851b7298SRandall Stewart #include <netinet/sctp_timer.h>
48a99b6783SRandall Stewart #include <netinet/sctp_crc32.h>
49776cd558SMichael Tuexen #include <netinet/sctp_kdtrace.h>
504474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
51c54a18d2SRandall Stewart #include <netinet/udp.h>
524474d71aSMichael Tuexen #endif
53bfc46083SRandall Stewart #include <sys/smp.h>
54f8829a4aSRandall Stewart 
55f8829a4aSRandall Stewart static void
56f8829a4aSRandall Stewart sctp_stop_all_cookie_timers(struct sctp_tcb *stcb)
57f8829a4aSRandall Stewart {
58f8829a4aSRandall Stewart 	struct sctp_nets *net;
59f8829a4aSRandall Stewart 
60a5d547adSRandall Stewart 	/*
61a5d547adSRandall Stewart 	 * This now not only stops all cookie timers it also stops any INIT
62a5d547adSRandall Stewart 	 * timers as well. This will make sure that the timers are stopped
63a5d547adSRandall Stewart 	 * in all collision cases.
64a5d547adSRandall Stewart 	 */
65f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
66f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
67a5d547adSRandall Stewart 		if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) {
68f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
69f8829a4aSRandall Stewart 			    stcb->sctp_ep,
70f8829a4aSRandall Stewart 			    stcb,
71a5d547adSRandall Stewart 			    net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1);
72a5d547adSRandall Stewart 		} else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) {
73a5d547adSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_INIT,
74a5d547adSRandall Stewart 			    stcb->sctp_ep,
75a5d547adSRandall Stewart 			    stcb,
76a5d547adSRandall Stewart 			    net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2);
77f8829a4aSRandall Stewart 		}
78f8829a4aSRandall Stewart 	}
79f8829a4aSRandall Stewart }
80f8829a4aSRandall Stewart 
81f8829a4aSRandall Stewart /* INIT handler */
82f8829a4aSRandall Stewart static void
83b1754ad1SMichael Tuexen sctp_handle_init(struct mbuf *m, int iphlen, int offset,
84b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
85f30ac432SMichael Tuexen     struct sctp_init_chunk *cp, struct sctp_inpcb *inp,
86eba8e643SMichael Tuexen     struct sctp_tcb *stcb, struct sctp_nets *net,
87457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
88f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
89f8829a4aSRandall Stewart {
90f8829a4aSRandall Stewart 	struct sctp_init *init;
91f8829a4aSRandall Stewart 	struct mbuf *op_err;
92f8829a4aSRandall Stewart 
93ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n",
94dd294dceSMichael Tuexen 	    (void *)stcb);
95d55b0b1bSRandall Stewart 	if (stcb == NULL) {
96d55b0b1bSRandall Stewart 		SCTP_INP_RLOCK(inp);
97d55b0b1bSRandall Stewart 	}
98059ec222SMichael Tuexen 	/* Validate parameters */
99d68fdc4dSMichael Tuexen 	init = &cp->init;
100eba8e643SMichael Tuexen 	if (ntohl(init->initiate_tag) == 0) {
101eba8e643SMichael Tuexen 		goto outnow;
102eba8e643SMichael Tuexen 	}
103eba8e643SMichael Tuexen 	if ((ntohl(init->a_rwnd) < SCTP_MIN_RWND) ||
104059ec222SMichael Tuexen 	    (ntohs(init->num_inbound_streams) == 0) ||
105059ec222SMichael Tuexen 	    (ntohs(init->num_outbound_streams) == 0)) {
106f8829a4aSRandall Stewart 		/* protocol error... send abort */
107ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
108eba8e643SMichael Tuexen 		sctp_send_abort(m, iphlen, src, dst, sh, init->initiate_tag, op_err,
109eba8e643SMichael Tuexen 		    mflowtype, mflowid, inp->fibnum,
110c54a18d2SRandall Stewart 		    vrf_id, port);
111d55b0b1bSRandall Stewart 		goto outnow;
112f8829a4aSRandall Stewart 	}
113f8829a4aSRandall Stewart 	if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
114d68fdc4dSMichael Tuexen 	    offset + ntohs(cp->ch.chunk_length))) {
115f8829a4aSRandall Stewart 		/* auth parameter(s) error... send abort */
116ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
117ff1ffd74SMichael Tuexen 		    "Problem with AUTH parameters");
118eba8e643SMichael Tuexen 		sctp_send_abort(m, iphlen, src, dst, sh, init->initiate_tag, op_err,
119eba8e643SMichael Tuexen 		    mflowtype, mflowid, inp->fibnum,
120f30ac432SMichael Tuexen 		    vrf_id, port);
121d55b0b1bSRandall Stewart 		goto outnow;
122f8829a4aSRandall Stewart 	}
1235d08768aSMichael Tuexen 	/* We are only accepting if we have a listening socket. */
124d68fdc4dSMichael Tuexen 	if ((stcb == NULL) &&
125d68fdc4dSMichael Tuexen 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
126d68fdc4dSMichael Tuexen 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1275d08768aSMichael Tuexen 	    (!SCTP_IS_LISTENING(inp)))) {
128d68fdc4dSMichael Tuexen 		/*
129d68fdc4dSMichael Tuexen 		 * FIX ME ?? What about TCP model and we have a
130d68fdc4dSMichael Tuexen 		 * match/restart case? Actually no fix is needed. the lookup
131d68fdc4dSMichael Tuexen 		 * will always find the existing assoc so stcb would not be
132d68fdc4dSMichael Tuexen 		 * NULL. It may be questionable to do this since we COULD
133d68fdc4dSMichael Tuexen 		 * just send back the INIT-ACK and hope that the app did
134d68fdc4dSMichael Tuexen 		 * accept()'s by the time the COOKIE was sent. But there is
135d68fdc4dSMichael Tuexen 		 * a price to pay for COOKIE generation and I don't want to
136d68fdc4dSMichael Tuexen 		 * pay it on the chance that the app will actually do some
137d68fdc4dSMichael Tuexen 		 * accepts(). The App just looses and should NOT be in this
138d68fdc4dSMichael Tuexen 		 * state :-)
139d68fdc4dSMichael Tuexen 		 */
140c58e60beSMichael Tuexen 		if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) {
141ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
142ff1ffd74SMichael Tuexen 			    "No listener");
143ff1ffd74SMichael Tuexen 			sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
144d089f9b9SMichael Tuexen 			    mflowtype, mflowid, inp->fibnum,
145f30ac432SMichael Tuexen 			    vrf_id, port);
146c58e60beSMichael Tuexen 		}
147d68fdc4dSMichael Tuexen 		goto outnow;
148d68fdc4dSMichael Tuexen 	}
149d68fdc4dSMichael Tuexen 	if ((stcb != NULL) &&
150839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
151d68fdc4dSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n");
152d68fdc4dSMichael Tuexen 		sctp_send_shutdown_ack(stcb, NULL);
153d68fdc4dSMichael Tuexen 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
154d68fdc4dSMichael Tuexen 	} else {
155ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
156ca83f93cSMichael Tuexen 		sctp_send_initiate_ack(inp, stcb, net, m, iphlen, offset,
157ca83f93cSMichael Tuexen 		    src, dst, sh, cp,
158457b4b88SMichael Tuexen 		    mflowtype, mflowid,
159cdd2d7d4SMichael Tuexen 		    vrf_id, port);
160d68fdc4dSMichael Tuexen 	}
161d55b0b1bSRandall Stewart outnow:
162d55b0b1bSRandall Stewart 	if (stcb == NULL) {
163d55b0b1bSRandall Stewart 		SCTP_INP_RUNLOCK(inp);
164d55b0b1bSRandall Stewart 	}
165f8829a4aSRandall Stewart }
166f8829a4aSRandall Stewart 
167f8829a4aSRandall Stewart /*
168f8829a4aSRandall Stewart  * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
169f8829a4aSRandall Stewart  */
1705bead436SRandall Stewart 
1715bead436SRandall Stewart int
17228397ac1SMichael Tuexen sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked)
1735bead436SRandall Stewart {
174124d851aSMichael Tuexen 	int unsent_data;
175f7a77f6fSMichael Tuexen 	unsigned int i;
176f7a77f6fSMichael Tuexen 	struct sctp_stream_queue_pending *sp;
1775bead436SRandall Stewart 	struct sctp_association *asoc;
1785bead436SRandall Stewart 
1795ac91821SMichael Tuexen 	SCTP_TCB_LOCK_ASSERT(stcb);
1805ac91821SMichael Tuexen 
1815bead436SRandall Stewart 	/*
182124d851aSMichael Tuexen 	 * This function returns if any stream has true unsent data on it.
183124d851aSMichael Tuexen 	 * Note that as it looks through it will clean up any places that
184124d851aSMichael Tuexen 	 * have old data that has been sent but left at top of stream queue.
1855bead436SRandall Stewart 	 */
1865bead436SRandall Stewart 	asoc = &stcb->asoc;
187124d851aSMichael Tuexen 	unsent_data = 0;
188f7a77f6fSMichael Tuexen 	if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
189f7a77f6fSMichael Tuexen 		/* Check to see if some data queued */
190f7a77f6fSMichael Tuexen 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
19152be287eSRandall Stewart 			/* sa_ignore FREED_MEMORY */
192f7a77f6fSMichael Tuexen 			sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue);
193f7a77f6fSMichael Tuexen 			if (sp == NULL) {
194f7a77f6fSMichael Tuexen 				continue;
195f7a77f6fSMichael Tuexen 			}
1965bead436SRandall Stewart 			if ((sp->msg_is_complete) &&
1975bead436SRandall Stewart 			    (sp->length == 0) &&
1985bead436SRandall Stewart 			    (sp->sender_all_done)) {
1995bead436SRandall Stewart 				/*
2005bead436SRandall Stewart 				 * We are doing differed cleanup. Last time
2015bead436SRandall Stewart 				 * through when we took all the data the
2025bead436SRandall Stewart 				 * sender_all_done was not set.
2035bead436SRandall Stewart 				 */
2045bead436SRandall Stewart 				if (sp->put_last_out == 0) {
2055bead436SRandall Stewart 					SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
2065bead436SRandall Stewart 					SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
2075bead436SRandall Stewart 					    sp->sender_all_done,
2085bead436SRandall Stewart 					    sp->length,
2095bead436SRandall Stewart 					    sp->msg_is_complete,
2105bead436SRandall Stewart 					    sp->put_last_out);
2115bead436SRandall Stewart 				}
212f7a77f6fSMichael Tuexen 				atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
213f7a77f6fSMichael Tuexen 				TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next);
214762ae0ecSMichael Tuexen 				stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, &asoc->strmout[i], sp);
2159eea4a2dSMichael Tuexen 				if (sp->net) {
2165bead436SRandall Stewart 					sctp_free_remote_addr(sp->net);
2179eea4a2dSMichael Tuexen 					sp->net = NULL;
2189eea4a2dSMichael Tuexen 				}
2195bead436SRandall Stewart 				if (sp->data) {
2205bead436SRandall Stewart 					sctp_m_freem(sp->data);
2215bead436SRandall Stewart 					sp->data = NULL;
2225bead436SRandall Stewart 				}
223689e6a5fSMichael Tuexen 				sctp_free_a_strmoq(stcb, sp, so_locked);
224124d851aSMichael Tuexen 				if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
225124d851aSMichael Tuexen 					unsent_data++;
226124d851aSMichael Tuexen 				}
2275bead436SRandall Stewart 			} else {
2285bead436SRandall Stewart 				unsent_data++;
229124d851aSMichael Tuexen 			}
230124d851aSMichael Tuexen 			if (unsent_data > 0) {
2314a9ef3f8SMichael Tuexen 				break;
2325bead436SRandall Stewart 			}
2335bead436SRandall Stewart 		}
2345bead436SRandall Stewart 	}
2355bead436SRandall Stewart 	return (unsent_data);
2365bead436SRandall Stewart }
2375bead436SRandall Stewart 
238f8829a4aSRandall Stewart static int
2397215cc1bSMichael Tuexen sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb)
240f8829a4aSRandall Stewart {
241f8829a4aSRandall Stewart 	struct sctp_init *init;
242f8829a4aSRandall Stewart 	struct sctp_association *asoc;
243f8829a4aSRandall Stewart 	struct sctp_nets *lnet;
244f8829a4aSRandall Stewart 	unsigned int i;
245f8829a4aSRandall Stewart 
2465ac91821SMichael Tuexen 	SCTP_TCB_LOCK_ASSERT(stcb);
2475ac91821SMichael Tuexen 
248f8829a4aSRandall Stewart 	init = &cp->init;
249f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
250f8829a4aSRandall Stewart 	/* save off parameters */
251f8829a4aSRandall Stewart 	asoc->peer_vtag = ntohl(init->initiate_tag);
252f8829a4aSRandall Stewart 	asoc->peers_rwnd = ntohl(init->a_rwnd);
253493d8e5aSRandall Stewart 	/* init tsn's */
254493d8e5aSRandall Stewart 	asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
255493d8e5aSRandall Stewart 
2569eea4a2dSMichael Tuexen 	if (!TAILQ_EMPTY(&asoc->nets)) {
257f8829a4aSRandall Stewart 		/* update any ssthresh's that may have a default */
258f8829a4aSRandall Stewart 		TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
259f8829a4aSRandall Stewart 			lnet->ssthresh = asoc->peers_rwnd;
260b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
261f8829a4aSRandall Stewart 				sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
26280fefe0aSRandall Stewart 			}
263f8829a4aSRandall Stewart 		}
264f8829a4aSRandall Stewart 	}
265f8829a4aSRandall Stewart 	if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
266f8829a4aSRandall Stewart 		unsigned int newcnt;
267f8829a4aSRandall Stewart 		struct sctp_stream_out *outs;
2684a9ef3f8SMichael Tuexen 		struct sctp_stream_queue_pending *sp, *nsp;
2694a9ef3f8SMichael Tuexen 		struct sctp_tmit_chunk *chk, *nchk;
270f8829a4aSRandall Stewart 
271810ec536SMichael Tuexen 		/* abandon the upper streams */
272f8829a4aSRandall Stewart 		newcnt = ntohs(init->num_inbound_streams);
2734a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
27449656eefSMichael Tuexen 			if (chk->rec.data.sid >= newcnt) {
275810ec536SMichael Tuexen 				TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
276810ec536SMichael Tuexen 				asoc->send_queue_cnt--;
27749656eefSMichael Tuexen 				if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
27849656eefSMichael Tuexen 					asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
279a7ad6026SMichael Tuexen #ifdef INVARIANTS
280a7ad6026SMichael Tuexen 				} else {
28149656eefSMichael Tuexen 					panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
282a7ad6026SMichael Tuexen #endif
283a7ad6026SMichael Tuexen 				}
284810ec536SMichael Tuexen 				if (chk->data != NULL) {
285810ec536SMichael Tuexen 					sctp_free_bufspace(stcb, asoc, chk, 1);
2861edc9dbaSMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
2871edc9dbaSMichael Tuexen 					    0, chk, SCTP_SO_NOT_LOCKED);
288810ec536SMichael Tuexen 					if (chk->data) {
289810ec536SMichael Tuexen 						sctp_m_freem(chk->data);
290810ec536SMichael Tuexen 						chk->data = NULL;
291810ec536SMichael Tuexen 					}
292810ec536SMichael Tuexen 				}
293689e6a5fSMichael Tuexen 				sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
294810ec536SMichael Tuexen 				/* sa_ignore FREED_MEMORY */
295810ec536SMichael Tuexen 			}
296810ec536SMichael Tuexen 		}
297f8829a4aSRandall Stewart 		if (asoc->strmout) {
298f8829a4aSRandall Stewart 			for (i = newcnt; i < asoc->pre_open_streams; i++) {
299f8829a4aSRandall Stewart 				outs = &asoc->strmout[i];
3004a9ef3f8SMichael Tuexen 				TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
3014d58b0c3SMichael Tuexen 					atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
302810ec536SMichael Tuexen 					TAILQ_REMOVE(&outs->outqueue, sp, next);
303762ae0ecSMichael Tuexen 					stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp);
304f8829a4aSRandall Stewart 					sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
3051edc9dbaSMichael Tuexen 					    stcb, 0, sp, SCTP_SO_NOT_LOCKED);
306f8829a4aSRandall Stewart 					if (sp->data) {
307f8829a4aSRandall Stewart 						sctp_m_freem(sp->data);
308f8829a4aSRandall Stewart 						sp->data = NULL;
309f8829a4aSRandall Stewart 					}
3109eea4a2dSMichael Tuexen 					if (sp->net) {
311f8829a4aSRandall Stewart 						sctp_free_remote_addr(sp->net);
312f8829a4aSRandall Stewart 						sp->net = NULL;
3139eea4a2dSMichael Tuexen 					}
314f8829a4aSRandall Stewart 					/* Free the chunk */
315689e6a5fSMichael Tuexen 					sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED);
3163c503c28SRandall Stewart 					/* sa_ignore FREED_MEMORY */
317f8829a4aSRandall Stewart 				}
3187cca1775SRandall Stewart 				outs->state = SCTP_STREAM_CLOSED;
319f8829a4aSRandall Stewart 			}
320f8829a4aSRandall Stewart 		}
321810ec536SMichael Tuexen 		/* cut back the count */
322f8829a4aSRandall Stewart 		asoc->pre_open_streams = newcnt;
323f8829a4aSRandall Stewart 	}
3247cca1775SRandall Stewart 	asoc->streamoutcnt = asoc->pre_open_streams;
325fdc4c9d0SMichael Tuexen 	if (asoc->strmout) {
3267cca1775SRandall Stewart 		for (i = 0; i < asoc->streamoutcnt; i++) {
3277cca1775SRandall Stewart 			asoc->strmout[i].state = SCTP_STREAM_OPEN;
3287cca1775SRandall Stewart 		}
329fdc4c9d0SMichael Tuexen 	}
330830d754dSRandall Stewart 	/* EY - nr_sack: initialize highest tsn in nr_mapping_array */
331830d754dSRandall Stewart 	asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
332b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
333f8829a4aSRandall Stewart 		sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
33480fefe0aSRandall Stewart 	}
335f8829a4aSRandall Stewart 	/* This is the next one we expect */
336f8829a4aSRandall Stewart 	asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
337f8829a4aSRandall Stewart 
338f8829a4aSRandall Stewart 	asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
339d6af161aSRandall Stewart 	asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
340493d8e5aSRandall Stewart 
341f8829a4aSRandall Stewart 	asoc->advanced_peer_ack_point = asoc->last_acked_seq;
342f8829a4aSRandall Stewart 	/* open the requested streams */
343207304d4SRandall Stewart 
344f8829a4aSRandall Stewart 	if (asoc->strmin != NULL) {
345f8829a4aSRandall Stewart 		/* Free the old ones */
3466a91f103SRandall Stewart 		for (i = 0; i < asoc->streamincnt; i++) {
34744249214SRandall Stewart 			sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue);
34844249214SRandall Stewart 			sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue);
3496a91f103SRandall Stewart 		}
350207304d4SRandall Stewart 		SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
351f8829a4aSRandall Stewart 	}
352ee1ccd92SMichael Tuexen 	if (asoc->max_inbound_streams > ntohs(init->num_outbound_streams)) {
3536a91f103SRandall Stewart 		asoc->streamincnt = ntohs(init->num_outbound_streams);
354ee1ccd92SMichael Tuexen 	} else {
355ee1ccd92SMichael Tuexen 		asoc->streamincnt = asoc->max_inbound_streams;
3566a91f103SRandall Stewart 	}
357f8829a4aSRandall Stewart 	SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
358207304d4SRandall Stewart 	    sizeof(struct sctp_stream_in), SCTP_M_STRMI);
359f8829a4aSRandall Stewart 	if (asoc->strmin == NULL) {
360f8829a4aSRandall Stewart 		/* we didn't get memory for the streams! */
361ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
362f8829a4aSRandall Stewart 		return (-1);
363f8829a4aSRandall Stewart 	}
364f8829a4aSRandall Stewart 	for (i = 0; i < asoc->streamincnt; i++) {
36549656eefSMichael Tuexen 		asoc->strmin[i].sid = i;
36649656eefSMichael Tuexen 		asoc->strmin[i].last_mid_delivered = 0xffffffff;
367f8829a4aSRandall Stewart 		TAILQ_INIT(&asoc->strmin[i].inqueue);
36844249214SRandall Stewart 		TAILQ_INIT(&asoc->strmin[i].uno_inqueue);
36944249214SRandall Stewart 		asoc->strmin[i].pd_api_started = 0;
3709a6142d8SRandall Stewart 		asoc->strmin[i].delivery_started = 0;
371f8829a4aSRandall Stewart 	}
372f8829a4aSRandall Stewart 	/*
373f8829a4aSRandall Stewart 	 * load_address_from_init will put the addresses into the
374f8829a4aSRandall Stewart 	 * association when the COOKIE is processed or the INIT-ACK is
375f8829a4aSRandall Stewart 	 * processed. Both types of COOKIE's existing and new call this
376f8829a4aSRandall Stewart 	 * routine. It will remove addresses that are no longer in the
377f8829a4aSRandall Stewart 	 * association (for the restarting case where addresses are
378f8829a4aSRandall Stewart 	 * removed). Up front when the INIT arrives we will discard it if it
379f8829a4aSRandall Stewart 	 * is a restart and new addresses have been added.
380f8829a4aSRandall Stewart 	 */
3813c503c28SRandall Stewart 	/* sa_ignore MEMLEAK */
382f8829a4aSRandall Stewart 	return (0);
383f8829a4aSRandall Stewart }
384f8829a4aSRandall Stewart 
385f8829a4aSRandall Stewart /*
386f8829a4aSRandall Stewart  * INIT-ACK message processing/consumption returns value < 0 on error
387f8829a4aSRandall Stewart  */
388f8829a4aSRandall Stewart static int
389b1754ad1SMichael Tuexen sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
390b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
391f30ac432SMichael Tuexen     struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
392f30ac432SMichael Tuexen     struct sctp_nets *net, int *abort_no_unlock,
393457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
394f30ac432SMichael Tuexen     uint32_t vrf_id)
395f8829a4aSRandall Stewart {
396f8829a4aSRandall Stewart 	struct sctp_association *asoc;
397f8829a4aSRandall Stewart 	struct mbuf *op_err;
3986182677fSMichael Tuexen 	int retval, abort_flag, cookie_found;
3996182677fSMichael Tuexen 	int initack_limit;
400830d754dSRandall Stewart 	int nat_friendly = 0;
401f8829a4aSRandall Stewart 
402f8829a4aSRandall Stewart 	/* First verify that we have no illegal param's */
403f8829a4aSRandall Stewart 	abort_flag = 0;
4046182677fSMichael Tuexen 	cookie_found = 0;
405f8829a4aSRandall Stewart 
406f8829a4aSRandall Stewart 	op_err = sctp_arethere_unrecognized_parameters(m,
407f8829a4aSRandall Stewart 	    (offset + sizeof(struct sctp_init_chunk)),
4086182677fSMichael Tuexen 	    &abort_flag, (struct sctp_chunkhdr *)cp,
4096182677fSMichael Tuexen 	    &nat_friendly, &cookie_found);
410f8829a4aSRandall Stewart 	if (abort_flag) {
411f8829a4aSRandall Stewart 		/* Send an abort and notify peer */
412dd36606bSMichael Tuexen 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
413dd36606bSMichael Tuexen 		    src, dst, sh, op_err,
414dd36606bSMichael Tuexen 		    mflowtype, mflowid,
415dd36606bSMichael Tuexen 		    vrf_id, net->port);
416bff64a4dSRandall Stewart 		*abort_no_unlock = 1;
417f8829a4aSRandall Stewart 		return (-1);
418f8829a4aSRandall Stewart 	}
4196182677fSMichael Tuexen 	if (!cookie_found) {
4206182677fSMichael Tuexen 		uint16_t len;
4216182677fSMichael Tuexen 
4220941b9dcSMichael Tuexen 		/* Only report the missing cookie parameter */
4230941b9dcSMichael Tuexen 		if (op_err != NULL) {
4240941b9dcSMichael Tuexen 			sctp_m_freem(op_err);
4250941b9dcSMichael Tuexen 		}
4266182677fSMichael Tuexen 		len = (uint16_t)(sizeof(struct sctp_error_missing_param) + sizeof(uint16_t));
4276182677fSMichael Tuexen 		/* We abort with an error of missing mandatory param */
4286182677fSMichael Tuexen 		op_err = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
4296182677fSMichael Tuexen 		if (op_err != NULL) {
4306182677fSMichael Tuexen 			struct sctp_error_missing_param *cause;
4316182677fSMichael Tuexen 
4326182677fSMichael Tuexen 			SCTP_BUF_LEN(op_err) = len;
4336182677fSMichael Tuexen 			cause = mtod(op_err, struct sctp_error_missing_param *);
4346182677fSMichael Tuexen 			/* Subtract the reserved param */
4356182677fSMichael Tuexen 			cause->cause.code = htons(SCTP_CAUSE_MISSING_PARAM);
4366182677fSMichael Tuexen 			cause->cause.length = htons(len);
4376182677fSMichael Tuexen 			cause->num_missing_params = htonl(1);
4386182677fSMichael Tuexen 			cause->type[0] = htons(SCTP_STATE_COOKIE);
4396182677fSMichael Tuexen 		}
4406182677fSMichael Tuexen 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
4416182677fSMichael Tuexen 		    src, dst, sh, op_err,
4426182677fSMichael Tuexen 		    mflowtype, mflowid,
4436182677fSMichael Tuexen 		    vrf_id, net->port);
4446182677fSMichael Tuexen 		*abort_no_unlock = 1;
4456182677fSMichael Tuexen 		return (-3);
4466182677fSMichael Tuexen 	}
447f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
448830d754dSRandall Stewart 	asoc->peer_supports_nat = (uint8_t)nat_friendly;
449f8829a4aSRandall Stewart 	/* process the peer's parameters in the INIT-ACK */
4505f2e1835SMichael Tuexen 	if (sctp_process_init((struct sctp_init_chunk *)cp, stcb) < 0) {
451645f3a1cSMichael Tuexen 		if (op_err != NULL) {
452645f3a1cSMichael Tuexen 			sctp_m_freem(op_err);
453645f3a1cSMichael Tuexen 		}
4545f2e1835SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4555f2e1835SMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_init() failed\n");
4565f2e1835SMichael Tuexen 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
4575f2e1835SMichael Tuexen 		    src, dst, sh, op_err,
4585f2e1835SMichael Tuexen 		    mflowtype, mflowid,
4595f2e1835SMichael Tuexen 		    vrf_id, net->port);
4605f2e1835SMichael Tuexen 		*abort_no_unlock = 1;
4615f2e1835SMichael Tuexen 		return (-1);
462f8829a4aSRandall Stewart 	}
463f8829a4aSRandall Stewart 	initack_limit = offset + ntohs(cp->ch.chunk_length);
464f8829a4aSRandall Stewart 	/* load all addresses */
4657215cc1bSMichael Tuexen 	if ((retval = sctp_load_addresses_from_init(stcb, m,
4665f2e1835SMichael Tuexen 	    offset + sizeof(struct sctp_init_chunk),
4675f2e1835SMichael Tuexen 	    initack_limit, src, dst, NULL, stcb->asoc.port)) < 0) {
468645f3a1cSMichael Tuexen 		if (op_err != NULL) {
469645f3a1cSMichael Tuexen 			sctp_m_freem(op_err);
470645f3a1cSMichael Tuexen 		}
471ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
472ff1ffd74SMichael Tuexen 		    "Problem with address parameters");
473ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1,
474ad81507eSRandall Stewart 		    "Load addresses from INIT causes an abort %d\n",
475ad81507eSRandall Stewart 		    retval);
476b1754ad1SMichael Tuexen 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
477ff1ffd74SMichael Tuexen 		    src, dst, sh, op_err,
478457b4b88SMichael Tuexen 		    mflowtype, mflowid,
479f30ac432SMichael Tuexen 		    vrf_id, net->port);
480bff64a4dSRandall Stewart 		*abort_no_unlock = 1;
481f8829a4aSRandall Stewart 		return (-1);
482f8829a4aSRandall Stewart 	}
48318e198d3SRandall Stewart 	/* if the peer doesn't support asconf, flush the asconf queue */
484c79bec9cSMichael Tuexen 	if (asoc->asconf_supported == 0) {
4854a9ef3f8SMichael Tuexen 		struct sctp_asconf_addr *param, *nparam;
48618e198d3SRandall Stewart 
4874a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
4884a9ef3f8SMichael Tuexen 			TAILQ_REMOVE(&asoc->asconf_queue, param, next);
4894a9ef3f8SMichael Tuexen 			SCTP_FREE(param, SCTP_M_ASC_ADDR);
49018e198d3SRandall Stewart 		}
49118e198d3SRandall Stewart 	}
4920053ed28SMichael Tuexen 
493f8829a4aSRandall Stewart 	stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
494f8829a4aSRandall Stewart 	    stcb->asoc.local_hmacs);
495f8829a4aSRandall Stewart 	if (op_err) {
496f8829a4aSRandall Stewart 		sctp_queue_op_err(stcb, op_err);
497f8829a4aSRandall Stewart 		/* queuing will steal away the mbuf chain to the out queue */
498f8829a4aSRandall Stewart 		op_err = NULL;
499f8829a4aSRandall Stewart 	}
500f8829a4aSRandall Stewart 	/* extract the cookie and queue it to "echo" it back... */
501b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
502c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
503c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
504c4739e2fSRandall Stewart 		    0,
505c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INPUT,
506c4739e2fSRandall Stewart 		    __LINE__);
507c4739e2fSRandall Stewart 	}
508f8829a4aSRandall Stewart 
509f8829a4aSRandall Stewart 	/*
510f8829a4aSRandall Stewart 	 * Cancel the INIT timer, We do this first before queueing the
511d2e61614SGordon Bergling 	 * cookie. We always cancel at the primary to assume that we are
512f8829a4aSRandall Stewart 	 * canceling the timer started by the INIT which always goes to the
513f8829a4aSRandall Stewart 	 * primary.
514f8829a4aSRandall Stewart 	 */
515f8829a4aSRandall Stewart 	sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
516b7d130beSMichael Tuexen 	    asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
517a5d547adSRandall Stewart 
518a5d547adSRandall Stewart 	/* calculate the RTO */
5198ed1e2c8SMichael Tuexen 	if (asoc->overall_error_count == 0) {
52044f2a327SMichael Tuexen 		sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered,
521f79aab18SRandall Stewart 		    SCTP_RTT_FROM_NON_DATA);
5228ed1e2c8SMichael Tuexen 	}
5238ed1e2c8SMichael Tuexen 	stcb->asoc.overall_error_count = 0;
5248ed1e2c8SMichael Tuexen 	net->error_count = 0;
5256182677fSMichael Tuexen 	retval = sctp_send_cookie_echo(m, offset, initack_limit, stcb, net);
526f8829a4aSRandall Stewart 	return (retval);
527f8829a4aSRandall Stewart }
5280053ed28SMichael Tuexen 
529f8829a4aSRandall Stewart static void
530f8829a4aSRandall Stewart sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
531f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net)
532f8829a4aSRandall Stewart {
53324aaac8dSMichael Tuexen 	union sctp_sockstore store;
53452129fcdSRandall Stewart 	struct sctp_nets *r_net, *f_net;
535f8829a4aSRandall Stewart 	struct timeval tv;
536d55b0b1bSRandall Stewart 	int req_prim = 0;
537ca85e948SMichael Tuexen 	uint16_t old_error_counter;
538f8829a4aSRandall Stewart 
539f8829a4aSRandall Stewart 	if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
540f8829a4aSRandall Stewart 		/* Invalid length */
541f8829a4aSRandall Stewart 		return;
542f8829a4aSRandall Stewart 	}
5430053ed28SMichael Tuexen 
544f8829a4aSRandall Stewart 	memset(&store, 0, sizeof(store));
545e6194c2eSMichael Tuexen 	switch (cp->heartbeat.hb_info.addr_family) {
546e6194c2eSMichael Tuexen #ifdef INET
547e6194c2eSMichael Tuexen 	case AF_INET:
548e6194c2eSMichael Tuexen 		if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
54924aaac8dSMichael Tuexen 			store.sin.sin_family = cp->heartbeat.hb_info.addr_family;
55024aaac8dSMichael Tuexen 			store.sin.sin_len = cp->heartbeat.hb_info.addr_len;
55124aaac8dSMichael Tuexen 			store.sin.sin_port = stcb->rport;
55224aaac8dSMichael Tuexen 			memcpy(&store.sin.sin_addr, cp->heartbeat.hb_info.address,
55324aaac8dSMichael Tuexen 			    sizeof(store.sin.sin_addr));
554e6194c2eSMichael Tuexen 		} else {
555e6194c2eSMichael Tuexen 			return;
556e6194c2eSMichael Tuexen 		}
557e6194c2eSMichael Tuexen 		break;
558e6194c2eSMichael Tuexen #endif
559e6194c2eSMichael Tuexen #ifdef INET6
560e6194c2eSMichael Tuexen 	case AF_INET6:
561e6194c2eSMichael Tuexen 		if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
56224aaac8dSMichael Tuexen 			store.sin6.sin6_family = cp->heartbeat.hb_info.addr_family;
56324aaac8dSMichael Tuexen 			store.sin6.sin6_len = cp->heartbeat.hb_info.addr_len;
56424aaac8dSMichael Tuexen 			store.sin6.sin6_port = stcb->rport;
56523602b60SMichael Tuexen 			memcpy(&store.sin6.sin6_addr, cp->heartbeat.hb_info.address, sizeof(struct in6_addr));
566f8829a4aSRandall Stewart 		} else {
567f8829a4aSRandall Stewart 			return;
568f8829a4aSRandall Stewart 		}
569e6194c2eSMichael Tuexen 		break;
570e6194c2eSMichael Tuexen #endif
571e6194c2eSMichael Tuexen 	default:
572e6194c2eSMichael Tuexen 		return;
573e6194c2eSMichael Tuexen 	}
57424aaac8dSMichael Tuexen 	r_net = sctp_findnet(stcb, &store.sa);
575f8829a4aSRandall Stewart 	if (r_net == NULL) {
576ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
577f8829a4aSRandall Stewart 		return;
578f8829a4aSRandall Stewart 	}
579f8829a4aSRandall Stewart 	if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
580f8829a4aSRandall Stewart 	    (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
581f8829a4aSRandall Stewart 	    (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
582f8829a4aSRandall Stewart 		/*
583f8829a4aSRandall Stewart 		 * If the its a HB and it's random value is correct when can
584f8829a4aSRandall Stewart 		 * confirm the destination.
585f8829a4aSRandall Stewart 		 */
586f8829a4aSRandall Stewart 		r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
58742551e99SRandall Stewart 		if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
58842551e99SRandall Stewart 			stcb->asoc.primary_destination = r_net;
58942551e99SRandall Stewart 			r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
59052129fcdSRandall Stewart 			f_net = TAILQ_FIRST(&stcb->asoc.nets);
59152129fcdSRandall Stewart 			if (f_net != r_net) {
59242551e99SRandall Stewart 				/*
59342551e99SRandall Stewart 				 * first one on the list is NOT the primary
594cd0a4ff6SPedro F. Giffuni 				 * sctp_cmpaddr() is much more efficient if
59542551e99SRandall Stewart 				 * the primary is the first on the list,
59642551e99SRandall Stewart 				 * make it so.
59742551e99SRandall Stewart 				 */
59852129fcdSRandall Stewart 				TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
59952129fcdSRandall Stewart 				TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
60042551e99SRandall Stewart 			}
601d55b0b1bSRandall Stewart 			req_prim = 1;
60242551e99SRandall Stewart 		}
603f8829a4aSRandall Stewart 		sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
604ceaad40aSRandall Stewart 		    stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
605b7d130beSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb,
606b7d130beSMichael Tuexen 		    r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
607ca85e948SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
608f8829a4aSRandall Stewart 	}
609469a65d1SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
610469a65d1SMichael Tuexen 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
611469a65d1SMichael Tuexen 		    stcb->asoc.overall_error_count,
612469a65d1SMichael Tuexen 		    0,
613469a65d1SMichael Tuexen 		    SCTP_FROM_SCTP_INPUT,
614469a65d1SMichael Tuexen 		    __LINE__);
615469a65d1SMichael Tuexen 	}
616469a65d1SMichael Tuexen 	stcb->asoc.overall_error_count = 0;
617ca85e948SMichael Tuexen 	old_error_counter = r_net->error_count;
618f8829a4aSRandall Stewart 	r_net->error_count = 0;
619f8829a4aSRandall Stewart 	r_net->hb_responded = 1;
620f8829a4aSRandall Stewart 	tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
621f8829a4aSRandall Stewart 	tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
622f8829a4aSRandall Stewart 	/* Now lets do a RTO with this */
62344f2a327SMichael Tuexen 	sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv,
624f79aab18SRandall Stewart 	    SCTP_RTT_FROM_NON_DATA);
6259b2a35b3SMichael Tuexen 	if ((r_net->dest_state & SCTP_ADDR_REACHABLE) == 0) {
626ca85e948SMichael Tuexen 		r_net->dest_state |= SCTP_ADDR_REACHABLE;
627ca85e948SMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
6284b1f78e1SMichael Tuexen 		    0, (void *)r_net, SCTP_SO_NOT_LOCKED);
629ca85e948SMichael Tuexen 	}
630ca85e948SMichael Tuexen 	if (r_net->dest_state & SCTP_ADDR_PF) {
631ca85e948SMichael Tuexen 		r_net->dest_state &= ~SCTP_ADDR_PF;
632ca85e948SMichael Tuexen 		stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
633ca85e948SMichael Tuexen 	}
634ca85e948SMichael Tuexen 	if (old_error_counter > 0) {
635b7d130beSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
636b7d130beSMichael Tuexen 		    stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_5);
637ca85e948SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
638ca85e948SMichael Tuexen 	}
639ca85e948SMichael Tuexen 	if (r_net == stcb->asoc.primary_destination) {
640ca85e948SMichael Tuexen 		if (stcb->asoc.alternate) {
641ca85e948SMichael Tuexen 			/* release the alternate, primary is good */
642ca85e948SMichael Tuexen 			sctp_free_remote_addr(stcb->asoc.alternate);
643ca85e948SMichael Tuexen 			stcb->asoc.alternate = NULL;
644ca85e948SMichael Tuexen 		}
645ca85e948SMichael Tuexen 	}
646d55b0b1bSRandall Stewart 	/* Mobility adaptation */
647d55b0b1bSRandall Stewart 	if (req_prim) {
648d55b0b1bSRandall Stewart 		if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
649d55b0b1bSRandall Stewart 		    SCTP_MOBILITY_BASE) ||
650d55b0b1bSRandall Stewart 		    sctp_is_mobility_feature_on(stcb->sctp_ep,
651d55b0b1bSRandall Stewart 		    SCTP_MOBILITY_FASTHANDOFF)) &&
652d55b0b1bSRandall Stewart 		    sctp_is_mobility_feature_on(stcb->sctp_ep,
653d55b0b1bSRandall Stewart 		    SCTP_MOBILITY_PRIM_DELETED)) {
654b7d130beSMichael Tuexen 			sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED,
655b7d130beSMichael Tuexen 			    stcb->sctp_ep, stcb, NULL,
656b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
657d55b0b1bSRandall Stewart 			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
658d55b0b1bSRandall Stewart 			    SCTP_MOBILITY_FASTHANDOFF)) {
659d55b0b1bSRandall Stewart 				sctp_assoc_immediate_retrans(stcb,
660d55b0b1bSRandall Stewart 				    stcb->asoc.primary_destination);
661d55b0b1bSRandall Stewart 			}
662d55b0b1bSRandall Stewart 			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
663d55b0b1bSRandall Stewart 			    SCTP_MOBILITY_BASE)) {
6649eea4a2dSMichael Tuexen 				sctp_move_chunks_from_net(stcb,
6659eea4a2dSMichael Tuexen 				    stcb->asoc.deleted_primary);
666d55b0b1bSRandall Stewart 			}
667a57fb68bSMichael Tuexen 			sctp_delete_prim_timer(stcb->sctp_ep, stcb);
668d55b0b1bSRandall Stewart 		}
669d55b0b1bSRandall Stewart 	}
670f8829a4aSRandall Stewart }
671f8829a4aSRandall Stewart 
672830d754dSRandall Stewart static int
673830d754dSRandall Stewart sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
674830d754dSRandall Stewart {
675830d754dSRandall Stewart 	/*
6761325a0deSMichael Tuexen 	 * Return 0 means we want you to proceed with the abort non-zero
6771325a0deSMichael Tuexen 	 * means no abort processing.
678830d754dSRandall Stewart 	 */
6791325a0deSMichael Tuexen 	uint32_t new_vtag;
680830d754dSRandall Stewart 	struct sctpasochead *head;
681830d754dSRandall Stewart 
682839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
683839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
684d28a3a39SMichael Tuexen 		atomic_add_int(&stcb->asoc.refcnt, 1);
685d28a3a39SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
686d28a3a39SMichael Tuexen 		SCTP_INP_INFO_WLOCK();
687d28a3a39SMichael Tuexen 		SCTP_TCB_LOCK(stcb);
688d28a3a39SMichael Tuexen 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
6891325a0deSMichael Tuexen 	} else {
6901325a0deSMichael Tuexen 		return (0);
691d28a3a39SMichael Tuexen 	}
69229545986SMichael Tuexen 	new_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
693839d21d6SMichael Tuexen 	if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) {
694830d754dSRandall Stewart 		/* generate a new vtag and send init */
695830d754dSRandall Stewart 		LIST_REMOVE(stcb, sctp_asocs);
6961325a0deSMichael Tuexen 		stcb->asoc.my_vtag = new_vtag;
697830d754dSRandall Stewart 		head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
698b7b84c0eSMichael Tuexen 		/*
699b7b84c0eSMichael Tuexen 		 * put it in the bucket in the vtag hash of assoc's for the
700b7b84c0eSMichael Tuexen 		 * system
701b7b84c0eSMichael Tuexen 		 */
702830d754dSRandall Stewart 		LIST_INSERT_HEAD(head, stcb, sctp_asocs);
703d28a3a39SMichael Tuexen 		SCTP_INP_INFO_WUNLOCK();
7041325a0deSMichael Tuexen 		sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
705830d754dSRandall Stewart 		return (1);
7061325a0deSMichael Tuexen 	} else {
707830d754dSRandall Stewart 		/*
708830d754dSRandall Stewart 		 * treat like a case where the cookie expired i.e.: - dump
709830d754dSRandall Stewart 		 * current cookie. - generate a new vtag. - resend init.
710830d754dSRandall Stewart 		 */
711830d754dSRandall Stewart 		/* generate a new vtag and send init */
712830d754dSRandall Stewart 		LIST_REMOVE(stcb, sctp_asocs);
713839d21d6SMichael Tuexen 		SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
714830d754dSRandall Stewart 		sctp_stop_all_cookie_timers(stcb);
715830d754dSRandall Stewart 		sctp_toss_old_cookies(stcb, &stcb->asoc);
7161325a0deSMichael Tuexen 		stcb->asoc.my_vtag = new_vtag;
717830d754dSRandall Stewart 		head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
718b7b84c0eSMichael Tuexen 		/*
719b7b84c0eSMichael Tuexen 		 * put it in the bucket in the vtag hash of assoc's for the
720b7b84c0eSMichael Tuexen 		 * system
721b7b84c0eSMichael Tuexen 		 */
722830d754dSRandall Stewart 		LIST_INSERT_HEAD(head, stcb, sctp_asocs);
723d28a3a39SMichael Tuexen 		SCTP_INP_INFO_WUNLOCK();
7241325a0deSMichael Tuexen 		sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
725830d754dSRandall Stewart 		return (1);
726830d754dSRandall Stewart 	}
727830d754dSRandall Stewart 	return (0);
728830d754dSRandall Stewart }
729830d754dSRandall Stewart 
730830d754dSRandall Stewart static int
731830d754dSRandall Stewart sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
732830d754dSRandall Stewart     struct sctp_nets *net)
733830d754dSRandall Stewart {
734830d754dSRandall Stewart 	/*
735830d754dSRandall Stewart 	 * return 0 means we want you to proceed with the abort non-zero
736830d754dSRandall Stewart 	 * means no abort processing
737830d754dSRandall Stewart 	 */
738c79bec9cSMichael Tuexen 	if (stcb->asoc.auth_supported == 0) {
739830d754dSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
740830d754dSRandall Stewart 		return (0);
741830d754dSRandall Stewart 	}
742830d754dSRandall Stewart 	sctp_asconf_send_nat_state_update(stcb, net);
743830d754dSRandall Stewart 	return (1);
744830d754dSRandall Stewart }
745830d754dSRandall Stewart 
746701492a5SMichael Tuexen /* Returns 1 if the stcb was aborted, 0 otherwise */
747701492a5SMichael Tuexen static int
748a2b42326SMichael Tuexen sctp_handle_abort(struct sctp_abort_chunk *abort,
749f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net)
750f8829a4aSRandall Stewart {
751830d754dSRandall Stewart 	uint16_t len;
752a2b42326SMichael Tuexen 	uint16_t error;
753ceaad40aSRandall Stewart 
754ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
755f8829a4aSRandall Stewart 	if (stcb == NULL)
756701492a5SMichael Tuexen 		return (0);
757f8829a4aSRandall Stewart 
758a2b42326SMichael Tuexen 	len = ntohs(abort->ch.chunk_length);
759701492a5SMichael Tuexen 	if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_error_cause)) {
760830d754dSRandall Stewart 		/*
761830d754dSRandall Stewart 		 * Need to check the cause codes for our two magic nat
762830d754dSRandall Stewart 		 * aborts which don't kill the assoc necessarily.
763830d754dSRandall Stewart 		 */
764701492a5SMichael Tuexen 		struct sctp_error_cause *cause;
765830d754dSRandall Stewart 
766701492a5SMichael Tuexen 		cause = (struct sctp_error_cause *)(abort + 1);
76786eda749SMichael Tuexen 		error = ntohs(cause->code);
768a2b42326SMichael Tuexen 		if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) {
769504ee6a0SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state, ABORT flags:%x\n",
770a2b42326SMichael Tuexen 			    abort->ch.chunk_flags);
771830d754dSRandall Stewart 			if (sctp_handle_nat_colliding_state(stcb)) {
772701492a5SMichael Tuexen 				return (0);
773830d754dSRandall Stewart 			}
774a2b42326SMichael Tuexen 		} else if (error == SCTP_CAUSE_NAT_MISSING_STATE) {
775504ee6a0SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state, ABORT flags:%x\n",
776a2b42326SMichael Tuexen 			    abort->ch.chunk_flags);
777830d754dSRandall Stewart 			if (sctp_handle_nat_missing_state(stcb, net)) {
778701492a5SMichael Tuexen 				return (0);
779830d754dSRandall Stewart 			}
780830d754dSRandall Stewart 		}
781a2b42326SMichael Tuexen 	} else {
782a2b42326SMichael Tuexen 		error = 0;
783830d754dSRandall Stewart 	}
784f8829a4aSRandall Stewart 	/* stop any receive timers */
7856fb7b4fbSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
786b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_7);
787f8829a4aSRandall Stewart 	/* notify user of the abort and clean up... */
788105b68b4SMichael Tuexen 	sctp_abort_notification(stcb, true, false, error, abort, SCTP_SO_NOT_LOCKED);
789f8829a4aSRandall Stewart 	/* free the tcb */
790f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_aborted);
791839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
792839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
793f8829a4aSRandall Stewart 		SCTP_STAT_DECR_GAUGE32(sctps_currestab);
794f8829a4aSRandall Stewart 	}
795f1f73e57SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
796f1f73e57SRandall Stewart 	sctp_print_out_track_log(stcb);
797f1f73e57SRandall Stewart #endif
798c4739e2fSRandall Stewart 	(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
799b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
800ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
801701492a5SMichael Tuexen 	return (1);
802f8829a4aSRandall Stewart }
803f8829a4aSRandall Stewart 
804f8829a4aSRandall Stewart static void
805ca85e948SMichael Tuexen sctp_start_net_timers(struct sctp_tcb *stcb)
806ca85e948SMichael Tuexen {
807ca85e948SMichael Tuexen 	uint32_t cnt_hb_sent;
808ca85e948SMichael Tuexen 	struct sctp_nets *net;
809ca85e948SMichael Tuexen 
810ca85e948SMichael Tuexen 	cnt_hb_sent = 0;
811ca85e948SMichael Tuexen 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
812ca85e948SMichael Tuexen 		/*
813ca85e948SMichael Tuexen 		 * For each network start: 1) A pmtu timer. 2) A HB timer 3)
814ca85e948SMichael Tuexen 		 * If the dest in unconfirmed send a hb as well if under
815ca85e948SMichael Tuexen 		 * max_hb_burst have been sent.
816ca85e948SMichael Tuexen 		 */
817ca85e948SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
818ca85e948SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
819ca85e948SMichael Tuexen 		if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
820ca85e948SMichael Tuexen 		    (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
821ca85e948SMichael Tuexen 			sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
822ca85e948SMichael Tuexen 			cnt_hb_sent++;
823ca85e948SMichael Tuexen 		}
824ca85e948SMichael Tuexen 	}
825ca85e948SMichael Tuexen 	if (cnt_hb_sent) {
826ca85e948SMichael Tuexen 		sctp_chunk_output(stcb->sctp_ep, stcb,
827ca85e948SMichael Tuexen 		    SCTP_OUTPUT_FROM_COOKIE_ACK,
828ca85e948SMichael Tuexen 		    SCTP_SO_NOT_LOCKED);
829ca85e948SMichael Tuexen 	}
830ca85e948SMichael Tuexen }
831ca85e948SMichael Tuexen 
832ca85e948SMichael Tuexen static void
833f8829a4aSRandall Stewart sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
834f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
835f8829a4aSRandall Stewart {
836f8829a4aSRandall Stewart 	int some_on_streamwheel;
837aa1cfca9SMichael Tuexen 	int old_state;
838ceaad40aSRandall Stewart 
8394f14d4b6SMichael Tuexen 	SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_shutdown: handling SHUTDOWN\n");
840f8829a4aSRandall Stewart 	if (stcb == NULL)
841f8829a4aSRandall Stewart 		return;
842839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
843839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
844f8829a4aSRandall Stewart 		return;
845f8829a4aSRandall Stewart 	}
846f8829a4aSRandall Stewart 	if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
847f8829a4aSRandall Stewart 		/* Shutdown NOT the expected size */
848f8829a4aSRandall Stewart 		return;
849aa1cfca9SMichael Tuexen 	}
850839d21d6SMichael Tuexen 	old_state = SCTP_GET_STATE(stcb);
8517215cc1bSMichael Tuexen 	sctp_update_acked(stcb, cp, abort_flag);
8527e6206afSMichael Tuexen 	if (*abort_flag) {
8537e6206afSMichael Tuexen 		return;
8547e6206afSMichael Tuexen 	}
855f8829a4aSRandall Stewart 	/*
8564f14d4b6SMichael Tuexen 	 * FIXME MT: Handle the case where there are still incomplete
8574f14d4b6SMichael Tuexen 	 * received user messages or known missing user messages from the
8584f14d4b6SMichael Tuexen 	 * peer. One way to handle this is to abort the associations in this
8594f14d4b6SMichael Tuexen 	 * case.
860f8829a4aSRandall Stewart 	 */
861f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
862839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
863839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
864839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT)) {
865839d21d6SMichael Tuexen 			SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_RECEIVED);
866b7b84c0eSMichael Tuexen 			/*
867b7b84c0eSMichael Tuexen 			 * notify upper layer that peer has initiated a
868b7b84c0eSMichael Tuexen 			 * shutdown
869b7b84c0eSMichael Tuexen 			 */
870ceaad40aSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
871f8829a4aSRandall Stewart 
872f8829a4aSRandall Stewart 			/* reset time */
873*1095da75SMichael Tuexen 			(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
874f8829a4aSRandall Stewart 		}
875f8829a4aSRandall Stewart 	}
876839d21d6SMichael Tuexen 	if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) {
877f8829a4aSRandall Stewart 		/*
878f8829a4aSRandall Stewart 		 * stop the shutdown timer, since we WILL move to
879f8829a4aSRandall Stewart 		 * SHUTDOWN-ACK-SENT.
880f8829a4aSRandall Stewart 		 */
881b7d130beSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
882b7d130beSMichael Tuexen 		    net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
883f8829a4aSRandall Stewart 	}
8845bead436SRandall Stewart 	/* Now is there unsent data on a stream somewhere? */
885689e6a5fSMichael Tuexen 	some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
886f8829a4aSRandall Stewart 
887*1095da75SMichael Tuexen 	if (!TAILQ_EMPTY(&stcb->asoc.send_queue) ||
888*1095da75SMichael Tuexen 	    !TAILQ_EMPTY(&stcb->asoc.sent_queue) ||
889f8829a4aSRandall Stewart 	    some_on_streamwheel) {
890f8829a4aSRandall Stewart 		/* By returning we will push more data out */
891f8829a4aSRandall Stewart 		return;
892f8829a4aSRandall Stewart 	} else {
893f8829a4aSRandall Stewart 		/* no outstanding data to send, so move on... */
894f8829a4aSRandall Stewart 		/* send SHUTDOWN-ACK */
895f8829a4aSRandall Stewart 		/* move to SHUTDOWN-ACK-SENT state */
896839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
897839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
898f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
899f8829a4aSRandall Stewart 		}
900839d21d6SMichael Tuexen 		if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
901839d21d6SMichael Tuexen 			SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_ACK_SENT);
90212af6654SMichael Tuexen 			sctp_stop_timers_for_shutdown(stcb);
903c39cfa1fSMichael Tuexen 			sctp_send_shutdown_ack(stcb, net);
904aa1cfca9SMichael Tuexen 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
905aa1cfca9SMichael Tuexen 			    stcb->sctp_ep, stcb, net);
906aa1cfca9SMichael Tuexen 		} else if (old_state == SCTP_STATE_SHUTDOWN_ACK_SENT) {
907aa1cfca9SMichael Tuexen 			sctp_send_shutdown_ack(stcb, net);
908aa1cfca9SMichael Tuexen 		}
909f8829a4aSRandall Stewart 	}
910f8829a4aSRandall Stewart }
911f8829a4aSRandall Stewart 
912f8829a4aSRandall Stewart static void
9137215cc1bSMichael Tuexen sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
9147b470fc3SMichael Tuexen     struct sctp_tcb *stcb,
9157b470fc3SMichael Tuexen     struct sctp_nets *net)
916f8829a4aSRandall Stewart {
917ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
918ad81507eSRandall Stewart 	    "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
9194f14d4b6SMichael Tuexen 	if (stcb == NULL) {
920f8829a4aSRandall Stewart 		return;
9214f14d4b6SMichael Tuexen 	}
922f8829a4aSRandall Stewart 
923f8829a4aSRandall Stewart 	/* process according to association state */
924839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
925839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
9267b470fc3SMichael Tuexen 		/* unexpected SHUTDOWN-ACK... do OOTB handling... */
9277b470fc3SMichael Tuexen 		sctp_send_shutdown_complete(stcb, net, 1);
9287b470fc3SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
9297b470fc3SMichael Tuexen 		return;
9307b470fc3SMichael Tuexen 	}
931839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
932839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
933f8829a4aSRandall Stewart 		/* unexpected SHUTDOWN-ACK... so ignore... */
934f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
935f8829a4aSRandall Stewart 		return;
936f8829a4aSRandall Stewart 	}
937f8829a4aSRandall Stewart 	/*
9384f14d4b6SMichael Tuexen 	 * FIXME MT: Handle the case where there are still incomplete
9394f14d4b6SMichael Tuexen 	 * received user messages or known missing user messages from the
9404f14d4b6SMichael Tuexen 	 * peer. One way to handle this is to abort the associations in this
9414f14d4b6SMichael Tuexen 	 * case.
942f8829a4aSRandall Stewart 	 */
94356f778aaSMichael Tuexen #ifdef INVARIANTS
944*1095da75SMichael Tuexen 	if (!TAILQ_EMPTY(&stcb->asoc.send_queue) ||
945*1095da75SMichael Tuexen 	    !TAILQ_EMPTY(&stcb->asoc.sent_queue) ||
946124d851aSMichael Tuexen 	    sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
94756f778aaSMichael Tuexen 		panic("Queues are not empty when handling SHUTDOWN-ACK");
948f8829a4aSRandall Stewart 	}
94956f778aaSMichael Tuexen #endif
950f8829a4aSRandall Stewart 	/* stop the timer */
951b7d130beSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net,
952b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
953f8829a4aSRandall Stewart 	/* send SHUTDOWN-COMPLETE */
9547b470fc3SMichael Tuexen 	sctp_send_shutdown_complete(stcb, net, 0);
955f8829a4aSRandall Stewart 	/* notify upper layer protocol */
956f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
957f8829a4aSRandall Stewart 		if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
958f8829a4aSRandall Stewart 		    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
959f210e4fbSMichael Tuexen 			SCTP_SB_CLEAR(stcb->sctp_socket->so_snd);
960f8829a4aSRandall Stewart 		}
961c75fdd30SMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
962f8829a4aSRandall Stewart 	}
963f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
964f8829a4aSRandall Stewart 	/* free the TCB but first save off the ep */
965c4739e2fSRandall Stewart 	(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
966b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
967f8829a4aSRandall Stewart }
968f8829a4aSRandall Stewart 
969f8829a4aSRandall Stewart static void
9706fb7b4fbSMichael Tuexen sctp_process_unrecog_chunk(struct sctp_tcb *stcb, uint8_t chunk_type)
971f8829a4aSRandall Stewart {
9723e87bccdSMichael Tuexen 	switch (chunk_type) {
973f8829a4aSRandall Stewart 	case SCTP_ASCONF_ACK:
974f8829a4aSRandall Stewart 	case SCTP_ASCONF:
9756fb7b4fbSMichael Tuexen 		sctp_asconf_cleanup(stcb);
976f8829a4aSRandall Stewart 		break;
97744249214SRandall Stewart 	case SCTP_IFORWARD_CUM_TSN:
978f8829a4aSRandall Stewart 	case SCTP_FORWARD_CUM_TSN:
979dd973b0eSMichael Tuexen 		stcb->asoc.prsctp_supported = 0;
980f8829a4aSRandall Stewart 		break;
981f8829a4aSRandall Stewart 	default:
982ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
9833e87bccdSMichael Tuexen 		    "Peer does not support chunk type %d (0x%x).\n",
9843e87bccdSMichael Tuexen 		    chunk_type, chunk_type);
985f8829a4aSRandall Stewart 		break;
986f8829a4aSRandall Stewart 	}
987f8829a4aSRandall Stewart }
988f8829a4aSRandall Stewart 
989f8829a4aSRandall Stewart /*
990f8829a4aSRandall Stewart  * Skip past the param header and then we will find the param that caused the
991f8829a4aSRandall Stewart  * problem.  There are a number of param's in a ASCONF OR the prsctp param
992f8829a4aSRandall Stewart  * these will turn of specific features.
993c79bec9cSMichael Tuexen  * XXX: Is this the right thing to do?
994f8829a4aSRandall Stewart  */
995f8829a4aSRandall Stewart static void
9963e87bccdSMichael Tuexen sctp_process_unrecog_param(struct sctp_tcb *stcb, uint16_t parameter_type)
997f8829a4aSRandall Stewart {
9983e87bccdSMichael Tuexen 	switch (parameter_type) {
999f8829a4aSRandall Stewart 		/* pr-sctp draft */
1000f8829a4aSRandall Stewart 	case SCTP_PRSCTP_SUPPORTED:
1001dd973b0eSMichael Tuexen 		stcb->asoc.prsctp_supported = 0;
1002f8829a4aSRandall Stewart 		break;
1003f8829a4aSRandall Stewart 	case SCTP_SUPPORTED_CHUNK_EXT:
1004f8829a4aSRandall Stewart 		break;
1005f8829a4aSRandall Stewart 		/* draft-ietf-tsvwg-addip-sctp */
1006830d754dSRandall Stewart 	case SCTP_HAS_NAT_SUPPORT:
1007830d754dSRandall Stewart 		stcb->asoc.peer_supports_nat = 0;
1008830d754dSRandall Stewart 		break;
1009f8829a4aSRandall Stewart 	case SCTP_ADD_IP_ADDRESS:
1010f8829a4aSRandall Stewart 	case SCTP_DEL_IP_ADDRESS:
1011f8829a4aSRandall Stewart 	case SCTP_SET_PRIM_ADDR:
1012c79bec9cSMichael Tuexen 		stcb->asoc.asconf_supported = 0;
1013f8829a4aSRandall Stewart 		break;
1014f8829a4aSRandall Stewart 	case SCTP_SUCCESS_REPORT:
1015f8829a4aSRandall Stewart 	case SCTP_ERROR_CAUSE_IND:
1016ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1017ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
1018ad81507eSRandall Stewart 		    "Turning off ASCONF to this strange peer\n");
1019c79bec9cSMichael Tuexen 		stcb->asoc.asconf_supported = 0;
1020f8829a4aSRandall Stewart 		break;
1021f8829a4aSRandall Stewart 	default:
1022ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
10233e87bccdSMichael Tuexen 		    "Peer does not support param type %d (0x%x)??\n",
10243e87bccdSMichael Tuexen 		    parameter_type, parameter_type);
1025f8829a4aSRandall Stewart 		break;
1026f8829a4aSRandall Stewart 	}
1027f8829a4aSRandall Stewart }
1028f8829a4aSRandall Stewart 
1029f8829a4aSRandall Stewart static int
1030f8829a4aSRandall Stewart sctp_handle_error(struct sctp_chunkhdr *ch,
10313e87bccdSMichael Tuexen     struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
1032f8829a4aSRandall Stewart {
10333e87bccdSMichael Tuexen 	struct sctp_error_cause *cause;
1034f8829a4aSRandall Stewart 	struct sctp_association *asoc;
10353e87bccdSMichael Tuexen 	uint32_t remaining_length, adjust;
10363e87bccdSMichael Tuexen 	uint16_t code, cause_code, cause_length;
1037ceaad40aSRandall Stewart 
1038f8829a4aSRandall Stewart 	/* parse through all of the errors and process */
1039f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
10403e87bccdSMichael Tuexen 	cause = (struct sctp_error_cause *)((caddr_t)ch +
1041f8829a4aSRandall Stewart 	    sizeof(struct sctp_chunkhdr));
10423e87bccdSMichael Tuexen 	remaining_length = ntohs(ch->chunk_length);
10433e87bccdSMichael Tuexen 	if (remaining_length > limit) {
10443e87bccdSMichael Tuexen 		remaining_length = limit;
10453e87bccdSMichael Tuexen 	}
10463e87bccdSMichael Tuexen 	if (remaining_length >= sizeof(struct sctp_chunkhdr)) {
10473e87bccdSMichael Tuexen 		remaining_length -= sizeof(struct sctp_chunkhdr);
10483e87bccdSMichael Tuexen 	} else {
10493e87bccdSMichael Tuexen 		remaining_length = 0;
10503e87bccdSMichael Tuexen 	}
10513e87bccdSMichael Tuexen 	code = 0;
10523e87bccdSMichael Tuexen 	while (remaining_length >= sizeof(struct sctp_error_cause)) {
1053f8829a4aSRandall Stewart 		/* Process an Error Cause */
10543e87bccdSMichael Tuexen 		cause_code = ntohs(cause->code);
10553e87bccdSMichael Tuexen 		cause_length = ntohs(cause->length);
10563e87bccdSMichael Tuexen 		if ((cause_length > remaining_length) || (cause_length == 0)) {
10573e87bccdSMichael Tuexen 			/* Invalid cause length, possibly due to truncation. */
10583e87bccdSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in cause - bytes left: %u cause length: %u\n",
10593e87bccdSMichael Tuexen 			    remaining_length, cause_length);
1060f8829a4aSRandall Stewart 			return (0);
1061f8829a4aSRandall Stewart 		}
10623e87bccdSMichael Tuexen 		if (code == 0) {
1063389b1b11SMichael Tuexen 			/* report the first error cause */
10643e87bccdSMichael Tuexen 			code = cause_code;
1065389b1b11SMichael Tuexen 		}
10663e87bccdSMichael Tuexen 		switch (cause_code) {
1067f8829a4aSRandall Stewart 		case SCTP_CAUSE_INVALID_STREAM:
1068f8829a4aSRandall Stewart 		case SCTP_CAUSE_MISSING_PARAM:
1069f8829a4aSRandall Stewart 		case SCTP_CAUSE_INVALID_PARAM:
1070f8829a4aSRandall Stewart 		case SCTP_CAUSE_NO_USER_DATA:
10713e87bccdSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %u back? We have a bug :/ (or do they?)\n",
10723e87bccdSMichael Tuexen 			    cause_code);
1073f8829a4aSRandall Stewart 			break;
1074830d754dSRandall Stewart 		case SCTP_CAUSE_NAT_COLLIDING_STATE:
1075504ee6a0SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state, ERROR flags: %x\n",
1076830d754dSRandall Stewart 			    ch->chunk_flags);
1077830d754dSRandall Stewart 			if (sctp_handle_nat_colliding_state(stcb)) {
1078830d754dSRandall Stewart 				return (0);
1079830d754dSRandall Stewart 			}
1080830d754dSRandall Stewart 			break;
1081830d754dSRandall Stewart 		case SCTP_CAUSE_NAT_MISSING_STATE:
1082504ee6a0SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state, ERROR flags: %x\n",
1083830d754dSRandall Stewart 			    ch->chunk_flags);
1084830d754dSRandall Stewart 			if (sctp_handle_nat_missing_state(stcb, net)) {
1085830d754dSRandall Stewart 				return (0);
1086830d754dSRandall Stewart 			}
1087830d754dSRandall Stewart 			break;
1088f8829a4aSRandall Stewart 		case SCTP_CAUSE_STALE_COOKIE:
1089f8829a4aSRandall Stewart 			/*
1090f8829a4aSRandall Stewart 			 * We only act if we have echoed a cookie and are
1091f8829a4aSRandall Stewart 			 * waiting.
1092f8829a4aSRandall Stewart 			 */
10933e87bccdSMichael Tuexen 			if ((cause_length >= sizeof(struct sctp_error_stale_cookie)) &&
1094839d21d6SMichael Tuexen 			    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
10951f0e1344SMichael Tuexen 				struct timeval now;
10963e87bccdSMichael Tuexen 				struct sctp_error_stale_cookie *stale_cookie;
10971f0e1344SMichael Tuexen 				uint64_t stale_time;
1098f8829a4aSRandall Stewart 
1099f8829a4aSRandall Stewart 				asoc->stale_cookie_count++;
11001f0e1344SMichael Tuexen 				if (asoc->stale_cookie_count > asoc->max_init_times) {
1101105b68b4SMichael Tuexen 					sctp_abort_notification(stcb, false, true, 0, NULL, SCTP_SO_NOT_LOCKED);
1102c4739e2fSRandall Stewart 					(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1103b7d130beSMichael Tuexen 					    SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1104f8829a4aSRandall Stewart 					return (-1);
1105f8829a4aSRandall Stewart 				}
11061f0e1344SMichael Tuexen 				stale_cookie = (struct sctp_error_stale_cookie *)cause;
11071f0e1344SMichael Tuexen 				stale_time = ntohl(stale_cookie->stale_time);
11081f0e1344SMichael Tuexen 				if (stale_time == 0) {
11091f0e1344SMichael Tuexen 					/* Use an RTT as an approximation. */
11101f0e1344SMichael Tuexen 					(void)SCTP_GETTIME_TIMEVAL(&now);
11111f0e1344SMichael Tuexen 					timevalsub(&now, &asoc->time_entered);
11121f0e1344SMichael Tuexen 					stale_time = (uint64_t)1000000 * (uint64_t)now.tv_sec + (uint64_t)now.tv_usec;
11131f0e1344SMichael Tuexen 					if (stale_time == 0) {
11141f0e1344SMichael Tuexen 						stale_time = 1;
11151f0e1344SMichael Tuexen 					}
11161f0e1344SMichael Tuexen 				}
11171f0e1344SMichael Tuexen 				/*
11181f0e1344SMichael Tuexen 				 * stale_time is in usec, convert it to
11191f0e1344SMichael Tuexen 				 * msec. Round upwards, to ensure that it is
11201f0e1344SMichael Tuexen 				 * non-zero.
11211f0e1344SMichael Tuexen 				 */
11221f0e1344SMichael Tuexen 				stale_time = (stale_time + 999) / 1000;
11231f0e1344SMichael Tuexen 				/* Double it, to be more robust on RTX. */
11241f0e1344SMichael Tuexen 				stale_time = 2 * stale_time;
11251f0e1344SMichael Tuexen 				asoc->cookie_preserve_req = (uint32_t)stale_time;
11261f0e1344SMichael Tuexen 				if (asoc->overall_error_count == 0) {
11271f0e1344SMichael Tuexen 					sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered,
11281f0e1344SMichael Tuexen 					    SCTP_RTT_FROM_NON_DATA);
11291f0e1344SMichael Tuexen 				}
11301f0e1344SMichael Tuexen 				asoc->overall_error_count = 0;
11311f0e1344SMichael Tuexen 				/* Blast back to INIT state */
1132830d754dSRandall Stewart 				sctp_toss_old_cookies(stcb, &stcb->asoc);
1133f8829a4aSRandall Stewart 				sctp_stop_all_cookie_timers(stcb);
11341f0e1344SMichael Tuexen 				SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
11351f0e1344SMichael Tuexen 				(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
1136ceaad40aSRandall Stewart 				sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1137f8829a4aSRandall Stewart 			}
1138f8829a4aSRandall Stewart 			break;
1139f8829a4aSRandall Stewart 		case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1140f8829a4aSRandall Stewart 			/*
1141f8829a4aSRandall Stewart 			 * Nothing we can do here, we don't do hostname
1142f8829a4aSRandall Stewart 			 * addresses so if the peer does not like my IPv6
1143f8829a4aSRandall Stewart 			 * (or IPv4 for that matter) it does not matter. If
1144f8829a4aSRandall Stewart 			 * they don't support that type of address, they can
1145f8829a4aSRandall Stewart 			 * NOT possibly get that packet type... i.e. with no
1146cd0a4ff6SPedro F. Giffuni 			 * IPv6 you can't receive a IPv6 packet. so we can
1147f8829a4aSRandall Stewart 			 * safely ignore this one. If we ever added support
1148f8829a4aSRandall Stewart 			 * for HOSTNAME Addresses, then we would need to do
1149f8829a4aSRandall Stewart 			 * something here.
1150f8829a4aSRandall Stewart 			 */
1151f8829a4aSRandall Stewart 			break;
1152f8829a4aSRandall Stewart 		case SCTP_CAUSE_UNRECOG_CHUNK:
11533e87bccdSMichael Tuexen 			if (cause_length >= sizeof(struct sctp_error_unrecognized_chunk)) {
11543e87bccdSMichael Tuexen 				struct sctp_error_unrecognized_chunk *unrec_chunk;
11553e87bccdSMichael Tuexen 
11563e87bccdSMichael Tuexen 				unrec_chunk = (struct sctp_error_unrecognized_chunk *)cause;
11576fb7b4fbSMichael Tuexen 				sctp_process_unrecog_chunk(stcb, unrec_chunk->ch.chunk_type);
11583e87bccdSMichael Tuexen 			}
1159f8829a4aSRandall Stewart 			break;
1160f8829a4aSRandall Stewart 		case SCTP_CAUSE_UNRECOG_PARAM:
11613e87bccdSMichael Tuexen 			/* XXX: We only consider the first parameter */
11623e87bccdSMichael Tuexen 			if (cause_length >= sizeof(struct sctp_error_cause) + sizeof(struct sctp_paramhdr)) {
11633e87bccdSMichael Tuexen 				struct sctp_paramhdr *unrec_parameter;
11643e87bccdSMichael Tuexen 
11653e87bccdSMichael Tuexen 				unrec_parameter = (struct sctp_paramhdr *)(cause + 1);
11663e87bccdSMichael Tuexen 				sctp_process_unrecog_param(stcb, ntohs(unrec_parameter->param_type));
11673e87bccdSMichael Tuexen 			}
1168f8829a4aSRandall Stewart 			break;
1169f8829a4aSRandall Stewart 		case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1170f8829a4aSRandall Stewart 			/*
1171f8829a4aSRandall Stewart 			 * We ignore this since the timer will drive out a
1172f8829a4aSRandall Stewart 			 * new cookie anyway and there timer will drive us
1173f8829a4aSRandall Stewart 			 * to send a SHUTDOWN_COMPLETE. We can't send one
1174f8829a4aSRandall Stewart 			 * here since we don't have their tag.
1175f8829a4aSRandall Stewart 			 */
1176f8829a4aSRandall Stewart 			break;
1177f8829a4aSRandall Stewart 		case SCTP_CAUSE_DELETING_LAST_ADDR:
1178f8829a4aSRandall Stewart 		case SCTP_CAUSE_RESOURCE_SHORTAGE:
1179f8829a4aSRandall Stewart 		case SCTP_CAUSE_DELETING_SRC_ADDR:
1180f8829a4aSRandall Stewart 			/*
1181f8829a4aSRandall Stewart 			 * We should NOT get these here, but in a
118293164cf9SRandall Stewart 			 * ASCONF-ACK.
1183f8829a4aSRandall Stewart 			 */
11843e87bccdSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a error cause with code %u.\n",
11853e87bccdSMichael Tuexen 			    cause_code);
1186f8829a4aSRandall Stewart 			break;
1187f8829a4aSRandall Stewart 		case SCTP_CAUSE_OUT_OF_RESC:
1188f8829a4aSRandall Stewart 			/*
1189f8829a4aSRandall Stewart 			 * And what, pray tell do we do with the fact that
1190f8829a4aSRandall Stewart 			 * the peer is out of resources? Not really sure we
119193164cf9SRandall Stewart 			 * could do anything but abort. I suspect this
1192f8829a4aSRandall Stewart 			 * should have came WITH an abort instead of in a
1193f8829a4aSRandall Stewart 			 * OP-ERROR.
1194f8829a4aSRandall Stewart 			 */
1195f8829a4aSRandall Stewart 			break;
1196f8829a4aSRandall Stewart 		default:
11973e87bccdSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown code 0x%x\n",
11983e87bccdSMichael Tuexen 			    cause_code);
1199f8829a4aSRandall Stewart 			break;
1200f8829a4aSRandall Stewart 		}
12013e87bccdSMichael Tuexen 		adjust = SCTP_SIZE32(cause_length);
12023e87bccdSMichael Tuexen 		if (remaining_length >= adjust) {
12033e87bccdSMichael Tuexen 			remaining_length -= adjust;
12043e87bccdSMichael Tuexen 		} else {
12053e87bccdSMichael Tuexen 			remaining_length = 0;
1206f8829a4aSRandall Stewart 		}
12073e87bccdSMichael Tuexen 		cause = (struct sctp_error_cause *)((caddr_t)cause + adjust);
12083e87bccdSMichael Tuexen 	}
12093e87bccdSMichael Tuexen 	sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, code, ch, SCTP_SO_NOT_LOCKED);
1210f8829a4aSRandall Stewart 	return (0);
1211f8829a4aSRandall Stewart }
1212f8829a4aSRandall Stewart 
1213f8829a4aSRandall Stewart static int
1214b1754ad1SMichael Tuexen sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1215b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
1216f30ac432SMichael Tuexen     struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1217f30ac432SMichael Tuexen     struct sctp_nets *net, int *abort_no_unlock,
1218457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
1219f30ac432SMichael Tuexen     uint32_t vrf_id)
1220f8829a4aSRandall Stewart {
1221f8829a4aSRandall Stewart 	struct sctp_init_ack *init_ack;
1222f8829a4aSRandall Stewart 	struct mbuf *op_err;
1223f8829a4aSRandall Stewart 
1224ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
1225ad81507eSRandall Stewart 	    "sctp_handle_init_ack: handling INIT-ACK\n");
1226ad81507eSRandall Stewart 
1227f8829a4aSRandall Stewart 	if (stcb == NULL) {
1228ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
1229ad81507eSRandall Stewart 		    "sctp_handle_init_ack: TCB is null\n");
1230f8829a4aSRandall Stewart 		return (-1);
1231f8829a4aSRandall Stewart 	}
1232a3665770SMichael Tuexen 	/* Only process the INIT-ACK chunk in COOKIE WAIT state. */
1233a3665770SMichael Tuexen 	if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) {
1234f8829a4aSRandall Stewart 		init_ack = &cp->init;
1235059ec222SMichael Tuexen 		/* Validate parameters. */
1236059ec222SMichael Tuexen 		if ((ntohl(init_ack->initiate_tag) == 0) ||
1237059ec222SMichael Tuexen 		    (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) ||
1238059ec222SMichael Tuexen 		    (ntohs(init_ack->num_inbound_streams) == 0) ||
1239059ec222SMichael Tuexen 		    (ntohs(init_ack->num_outbound_streams) == 0)) {
1240a3665770SMichael Tuexen 			/* One of the mandatory parameters is illegal. */
1241ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1242b1754ad1SMichael Tuexen 			sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1243b1754ad1SMichael Tuexen 			    src, dst, sh, op_err,
1244457b4b88SMichael Tuexen 			    mflowtype, mflowid,
1245f30ac432SMichael Tuexen 			    vrf_id, net->port);
1246bff64a4dSRandall Stewart 			*abort_no_unlock = 1;
1247f8829a4aSRandall Stewart 			return (-1);
1248f8829a4aSRandall Stewart 		}
1249f8829a4aSRandall Stewart 		if (stcb->asoc.primary_destination->dest_state &
1250f8829a4aSRandall Stewart 		    SCTP_ADDR_UNCONFIRMED) {
1251f8829a4aSRandall Stewart 			/*
1252f8829a4aSRandall Stewart 			 * The primary is where we sent the INIT, we can
1253f8829a4aSRandall Stewart 			 * always consider it confirmed when the INIT-ACK is
1254f8829a4aSRandall Stewart 			 * returned. Do this before we load addresses
1255f8829a4aSRandall Stewart 			 * though.
1256f8829a4aSRandall Stewart 			 */
1257f8829a4aSRandall Stewart 			stcb->asoc.primary_destination->dest_state &=
1258f8829a4aSRandall Stewart 			    ~SCTP_ADDR_UNCONFIRMED;
1259f8829a4aSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1260ceaad40aSRandall Stewart 			    stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1261f8829a4aSRandall Stewart 		}
1262b1754ad1SMichael Tuexen 		if (sctp_process_init_ack(m, iphlen, offset, src, dst, sh, cp, stcb,
1263f30ac432SMichael Tuexen 		    net, abort_no_unlock,
1264457b4b88SMichael Tuexen 		    mflowtype, mflowid,
1265f30ac432SMichael Tuexen 		    vrf_id) < 0) {
1266f8829a4aSRandall Stewart 			/* error in parsing parameters */
1267f8829a4aSRandall Stewart 			return (-1);
1268f8829a4aSRandall Stewart 		}
1269a3665770SMichael Tuexen 		/* Update our state. */
1270ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1271839d21d6SMichael Tuexen 		SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_ECHOED);
1272f8829a4aSRandall Stewart 
1273a3665770SMichael Tuexen 		/* Reset the RTO calculation. */
1274b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1275c4739e2fSRandall Stewart 			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1276c4739e2fSRandall Stewart 			    stcb->asoc.overall_error_count,
1277c4739e2fSRandall Stewart 			    0,
1278c4739e2fSRandall Stewart 			    SCTP_FROM_SCTP_INPUT,
1279c4739e2fSRandall Stewart 			    __LINE__);
1280c4739e2fSRandall Stewart 		}
1281f8829a4aSRandall Stewart 		stcb->asoc.overall_error_count = 0;
12826e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1283f8829a4aSRandall Stewart 		/*
1284a3665770SMichael Tuexen 		 * Collapse the init timer back in case of a exponential
1285a3665770SMichael Tuexen 		 * backoff.
1286f8829a4aSRandall Stewart 		 */
1287f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1288f8829a4aSRandall Stewart 		    stcb, net);
1289f8829a4aSRandall Stewart 		/*
1290a3665770SMichael Tuexen 		 * The output routine at the end of the inbound data
1291a3665770SMichael Tuexen 		 * processing will cause the cookie to be sent.
1292f8829a4aSRandall Stewart 		 */
1293ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1294f8829a4aSRandall Stewart 		return (0);
1295a3665770SMichael Tuexen 	} else {
1296a3665770SMichael Tuexen 		return (-1);
1297a3665770SMichael Tuexen 	}
1298f8829a4aSRandall Stewart }
1299f8829a4aSRandall Stewart 
1300830d754dSRandall Stewart static struct sctp_tcb *
1301830d754dSRandall Stewart sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1302b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
1303830d754dSRandall Stewart     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1304830d754dSRandall Stewart     struct sctp_inpcb *inp, struct sctp_nets **netp,
1305830d754dSRandall Stewart     struct sockaddr *init_src, int *notification,
1306830d754dSRandall Stewart     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1307457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
1308830d754dSRandall Stewart     uint32_t vrf_id, uint16_t port);
1309830d754dSRandall Stewart 
1310f8829a4aSRandall Stewart /*
1311f8829a4aSRandall Stewart  * handle a state cookie for an existing association m: input packet mbuf
1312f8829a4aSRandall Stewart  * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1313f8829a4aSRandall Stewart  * "split" mbuf and the cookie signature does not exist offset: offset into
1314f8829a4aSRandall Stewart  * mbuf to the cookie-echo chunk
1315f8829a4aSRandall Stewart  */
1316f8829a4aSRandall Stewart static struct sctp_tcb *
1317f8829a4aSRandall Stewart sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1318b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
1319f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1320830d754dSRandall Stewart     struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
13217215cc1bSMichael Tuexen     struct sockaddr *init_src, int *notification,
1322f30ac432SMichael Tuexen     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1323457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
1324f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
1325f8829a4aSRandall Stewart {
1326f8829a4aSRandall Stewart 	struct sctp_association *asoc;
1327f8829a4aSRandall Stewart 	struct sctp_init_chunk *init_cp, init_buf;
1328f8829a4aSRandall Stewart 	struct sctp_init_ack_chunk *initack_cp, initack_buf;
1329aa6db9a0SMichael Tuexen 	struct sctp_asconf_addr *aparam, *naparam;
1330aa6db9a0SMichael Tuexen 	struct sctp_asconf_ack *aack, *naack;
1331aa6db9a0SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
1332aa6db9a0SMichael Tuexen 	struct sctp_stream_reset_list *strrst, *nstrrst;
1333aa6db9a0SMichael Tuexen 	struct sctp_queued_to_read *sq, *nsq;
1334830d754dSRandall Stewart 	struct sctp_nets *net;
1335830d754dSRandall Stewart 	struct mbuf *op_err;
1336a5d547adSRandall Stewart 	int init_offset, initack_offset, i;
1337f8829a4aSRandall Stewart 	int retval;
13387f34832bSRandall Stewart 	int spec_flag = 0;
13394c9179adSRandall Stewart 	uint32_t how_indx;
1340f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
1341f0396ad1SMichael Tuexen 	int j;
1342f0396ad1SMichael Tuexen #endif
1343f0396ad1SMichael Tuexen 
1344830d754dSRandall Stewart 	net = *netp;
1345f8829a4aSRandall Stewart 	/* I know that the TCB is non-NULL from the caller */
1346f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
1347f42a358aSRandall Stewart 	for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
134844b7479bSRandall Stewart 		if (asoc->cookie_how[how_indx] == 0)
134944b7479bSRandall Stewart 			break;
135044b7479bSRandall Stewart 	}
135144b7479bSRandall Stewart 	if (how_indx < sizeof(asoc->cookie_how)) {
135244b7479bSRandall Stewart 		asoc->cookie_how[how_indx] = 1;
135344b7479bSRandall Stewart 	}
1354839d21d6SMichael Tuexen 	if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1355f8829a4aSRandall Stewart 		/* SHUTDOWN came in after sending INIT-ACK */
1356f8829a4aSRandall Stewart 		sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1357ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_COOKIE_IN_SHUTDOWN, "");
1358b1754ad1SMichael Tuexen 		sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
1359d089f9b9SMichael Tuexen 		    mflowtype, mflowid, inp->fibnum,
1360c54a18d2SRandall Stewart 		    vrf_id, net->port);
136144b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
136244b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 2;
136312dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1364f8829a4aSRandall Stewart 		return (NULL);
1365f8829a4aSRandall Stewart 	}
1366f8829a4aSRandall Stewart 	/*
1367f8829a4aSRandall Stewart 	 * find and validate the INIT chunk in the cookie (peer's info) the
1368f8829a4aSRandall Stewart 	 * INIT should start after the cookie-echo header struct (chunk
1369f8829a4aSRandall Stewart 	 * header, state cookie header struct)
1370f8829a4aSRandall Stewart 	 */
1371f8829a4aSRandall Stewart 	init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1372f8829a4aSRandall Stewart 
1373f8829a4aSRandall Stewart 	init_cp = (struct sctp_init_chunk *)
1374f8829a4aSRandall Stewart 	    sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1375f8829a4aSRandall Stewart 	    (uint8_t *)&init_buf);
1376f8829a4aSRandall Stewart 	if (init_cp == NULL) {
1377f8829a4aSRandall Stewart 		/* could not pull a INIT chunk in cookie */
137812dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1379f8829a4aSRandall Stewart 		return (NULL);
1380f8829a4aSRandall Stewart 	}
1381f8829a4aSRandall Stewart 	if (init_cp->ch.chunk_type != SCTP_INITIATION) {
138212dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1383f8829a4aSRandall Stewart 		return (NULL);
1384f8829a4aSRandall Stewart 	}
1385f8829a4aSRandall Stewart 	/*
1386f8829a4aSRandall Stewart 	 * find and validate the INIT-ACK chunk in the cookie (my info) the
1387f8829a4aSRandall Stewart 	 * INIT-ACK follows the INIT chunk
1388f8829a4aSRandall Stewart 	 */
138960990c0cSMichael Tuexen 	initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1390f8829a4aSRandall Stewart 	initack_cp = (struct sctp_init_ack_chunk *)
1391f8829a4aSRandall Stewart 	    sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1392f8829a4aSRandall Stewart 	    (uint8_t *)&initack_buf);
1393f8829a4aSRandall Stewart 	if (initack_cp == NULL) {
1394f8829a4aSRandall Stewart 		/* could not pull INIT-ACK chunk in cookie */
139512dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1396f8829a4aSRandall Stewart 		return (NULL);
1397f8829a4aSRandall Stewart 	}
1398f8829a4aSRandall Stewart 	if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
139912dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1400f8829a4aSRandall Stewart 		return (NULL);
1401f8829a4aSRandall Stewart 	}
1402f8829a4aSRandall Stewart 	if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1403f8829a4aSRandall Stewart 	    (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1404f8829a4aSRandall Stewart 		/*
1405f8829a4aSRandall Stewart 		 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1406f8829a4aSRandall Stewart 		 * to get into the OPEN state
1407f8829a4aSRandall Stewart 		 */
140844b7479bSRandall Stewart 		if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1409851b7298SRandall Stewart 			/*-
1410851b7298SRandall Stewart 			 * Opps, this means that we somehow generated two vtag's
1411851b7298SRandall Stewart 			 * the same. I.e. we did:
1412851b7298SRandall Stewart 			 *  Us               Peer
1413851b7298SRandall Stewart 			 *   <---INIT(tag=a)------
1414851b7298SRandall Stewart 			 *   ----INIT-ACK(tag=t)-->
1415851b7298SRandall Stewart 			 *   ----INIT(tag=t)------> *1
1416851b7298SRandall Stewart 			 *   <---INIT-ACK(tag=a)---
1417851b7298SRandall Stewart 			 *   <----CE(tag=t)------------- *2
1418851b7298SRandall Stewart 			 *
1419851b7298SRandall Stewart 			 * At point *1 we should be generating a different
1420851b7298SRandall Stewart 			 * tag t'. Which means we would throw away the CE and send
1421851b7298SRandall Stewart 			 * ours instead. Basically this is case C (throw away side).
1422851b7298SRandall Stewart 			 */
1423851b7298SRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
1424851b7298SRandall Stewart 				asoc->cookie_how[how_indx] = 17;
142512dda000SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
1426851b7298SRandall Stewart 			return (NULL);
142744b7479bSRandall Stewart 		}
1428839d21d6SMichael Tuexen 		switch (SCTP_GET_STATE(stcb)) {
1429f8829a4aSRandall Stewart 		case SCTP_STATE_COOKIE_WAIT:
143044b7479bSRandall Stewart 		case SCTP_STATE_COOKIE_ECHOED:
1431f8829a4aSRandall Stewart 			/*
143217205eccSRandall Stewart 			 * INIT was sent but got a COOKIE_ECHO with the
143344b7479bSRandall Stewart 			 * correct tags... just accept it...but we must
143444b7479bSRandall Stewart 			 * process the init so that we can make sure we have
143544b7479bSRandall Stewart 			 * the right seq no's.
1436f8829a4aSRandall Stewart 			 */
1437f8829a4aSRandall Stewart 			/* First we must process the INIT !! */
14385f2e1835SMichael Tuexen 			if (sctp_process_init(init_cp, stcb) < 0) {
143944b7479bSRandall Stewart 				if (how_indx < sizeof(asoc->cookie_how))
144044b7479bSRandall Stewart 					asoc->cookie_how[how_indx] = 3;
14415f2e1835SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
14425f2e1835SMichael Tuexen 				SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_init() failed\n");
14435f2e1835SMichael Tuexen 				sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
14445f2e1835SMichael Tuexen 				    src, dst, sh, op_err,
14455f2e1835SMichael Tuexen 				    mflowtype, mflowid,
14465f2e1835SMichael Tuexen 				    vrf_id, net->port);
1447f8829a4aSRandall Stewart 				return (NULL);
1448f8829a4aSRandall Stewart 			}
1449f8829a4aSRandall Stewart 			/* we have already processed the INIT so no problem */
1450b7d130beSMichael Tuexen 			sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp,
1451b7d130beSMichael Tuexen 			    stcb, net,
1452b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1453b7d130beSMichael Tuexen 			sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp,
1454b7d130beSMichael Tuexen 			    stcb, net,
1455b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1456f8829a4aSRandall Stewart 			/* update current state */
1457839d21d6SMichael Tuexen 			if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)
1458f42a358aSRandall Stewart 				SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1459f42a358aSRandall Stewart 			else
1460f42a358aSRandall Stewart 				SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1461c4739e2fSRandall Stewart 
1462839d21d6SMichael Tuexen 			SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1463f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1464a5d547adSRandall Stewart 			sctp_stop_all_cookie_timers(stcb);
1465f8829a4aSRandall Stewart 			if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1466f8829a4aSRandall Stewart 			    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
14675d08768aSMichael Tuexen 			    (!SCTP_IS_LISTENING(inp))) {
1468f8829a4aSRandall Stewart 				/*
1469f8829a4aSRandall Stewart 				 * Here is where collision would go if we
1470f8829a4aSRandall Stewart 				 * did a connect() and instead got a
1471f8829a4aSRandall Stewart 				 * init/init-ack/cookie done before the
1472f8829a4aSRandall Stewart 				 * init-ack came back..
1473f8829a4aSRandall Stewart 				 */
1474a5c2009dSMichael Tuexen 				sctp_pcb_add_flags(stcb->sctp_ep, SCTP_PCB_FLAGS_CONNECTED);
1475ceaad40aSRandall Stewart 				soisconnected(stcb->sctp_socket);
1476f8829a4aSRandall Stewart 			}
1477f8829a4aSRandall Stewart 			/* notify upper layer */
1478f8829a4aSRandall Stewart 			*notification = SCTP_NOTIFY_ASSOC_UP;
1479f8829a4aSRandall Stewart 			net->hb_responded = 1;
1480f8829a4aSRandall Stewart 			if (stcb->asoc.sctp_autoclose_ticks &&
1481f8829a4aSRandall Stewart 			    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1482f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1483f8829a4aSRandall Stewart 				    inp, stcb, NULL);
1484f8829a4aSRandall Stewart 			}
1485f8829a4aSRandall Stewart 			break;
1486f8829a4aSRandall Stewart 		default:
1487f8829a4aSRandall Stewart 			/*
1488f8829a4aSRandall Stewart 			 * we're in the OPEN state (or beyond), so peer must
1489f8829a4aSRandall Stewart 			 * have simply lost the COOKIE-ACK
1490f8829a4aSRandall Stewart 			 */
1491f8829a4aSRandall Stewart 			break;
1492f8829a4aSRandall Stewart 		}		/* end switch */
1493a5d547adSRandall Stewart 		sctp_stop_all_cookie_timers(stcb);
1494655c200cSAlexander Motin 		if ((retval = sctp_load_addresses_from_init(stcb, m,
1495f8829a4aSRandall Stewart 		    init_offset + sizeof(struct sctp_init_chunk),
14965f2e1835SMichael Tuexen 		    initack_offset, src, dst, init_src, stcb->asoc.port)) < 0) {
149744b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
149844b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 4;
14995f2e1835SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
15005f2e1835SMichael Tuexen 			    "Problem with address parameters");
15015f2e1835SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1,
15025f2e1835SMichael Tuexen 			    "Load addresses from INIT causes an abort %d\n",
15035f2e1835SMichael Tuexen 			    retval);
15045f2e1835SMichael Tuexen 			sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
15055f2e1835SMichael Tuexen 			    src, dst, sh, op_err,
15065f2e1835SMichael Tuexen 			    mflowtype, mflowid,
15075f2e1835SMichael Tuexen 			    vrf_id, net->port);
1508f8829a4aSRandall Stewart 			return (NULL);
1509f8829a4aSRandall Stewart 		}
1510f8829a4aSRandall Stewart 		/* respond with a COOKIE-ACK */
1511a5d547adSRandall Stewart 		sctp_toss_old_cookies(stcb, asoc);
1512f8829a4aSRandall Stewart 		sctp_send_cookie_ack(stcb);
151344b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
151444b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 5;
1515f8829a4aSRandall Stewart 		return (stcb);
151617205eccSRandall Stewart 	}
15170053ed28SMichael Tuexen 
1518f8829a4aSRandall Stewart 	if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1519f8829a4aSRandall Stewart 	    ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1520f8829a4aSRandall Stewart 	    cookie->tie_tag_my_vtag == 0 &&
1521f8829a4aSRandall Stewart 	    cookie->tie_tag_peer_vtag == 0) {
1522f8829a4aSRandall Stewart 		/*
1523f8829a4aSRandall Stewart 		 * case C in Section 5.2.4 Table 2: XMOO silently discard
1524f8829a4aSRandall Stewart 		 */
152544b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
152644b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 6;
152712dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1528f8829a4aSRandall Stewart 		return (NULL);
1529f8829a4aSRandall Stewart 	}
1530830d754dSRandall Stewart 	/*
1531830d754dSRandall Stewart 	 * If nat support, and the below and stcb is established, send back
1532830d754dSRandall Stewart 	 * a ABORT(colliding state) if we are established.
1533830d754dSRandall Stewart 	 */
1534839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) &&
1535830d754dSRandall Stewart 	    (asoc->peer_supports_nat) &&
1536830d754dSRandall Stewart 	    ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1537830d754dSRandall Stewart 	    ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1538830d754dSRandall Stewart 	    (asoc->peer_vtag == 0)))) {
1539830d754dSRandall Stewart 		/*
1540830d754dSRandall Stewart 		 * Special case - Peer's support nat. We may have two init's
1541830d754dSRandall Stewart 		 * that we gave out the same tag on since one was not
1542830d754dSRandall Stewart 		 * established.. i.e. we get INIT from host-1 behind the nat
1543830d754dSRandall Stewart 		 * and we respond tag-a, we get a INIT from host-2 behind
1544830d754dSRandall Stewart 		 * the nat and we get tag-a again. Then we bring up host-1
1545830d754dSRandall Stewart 		 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1546830d754dSRandall Stewart 		 * Now we have colliding state. We must send an abort here
1547830d754dSRandall Stewart 		 * with colliding state indication.
1548830d754dSRandall Stewart 		 */
1549ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_NAT_COLLIDING_STATE, "");
1550b1754ad1SMichael Tuexen 		sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
1551d089f9b9SMichael Tuexen 		    mflowtype, mflowid, inp->fibnum,
1552f30ac432SMichael Tuexen 		    vrf_id, port);
155312dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1554830d754dSRandall Stewart 		return (NULL);
1555830d754dSRandall Stewart 	}
1556830d754dSRandall Stewart 	if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1557830d754dSRandall Stewart 	    ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1558830d754dSRandall Stewart 	    (asoc->peer_vtag == 0))) {
1559f8829a4aSRandall Stewart 		/*
1560f8829a4aSRandall Stewart 		 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1561f8829a4aSRandall Stewart 		 * should be ok, re-accept peer info
1562f8829a4aSRandall Stewart 		 */
156344b7479bSRandall Stewart 		if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
156444b7479bSRandall Stewart 			/*
156544b7479bSRandall Stewart 			 * Extension of case C. If we hit this, then the
156644b7479bSRandall Stewart 			 * random number generator returned the same vtag
156744b7479bSRandall Stewart 			 * when we first sent our INIT-ACK and when we later
156844b7479bSRandall Stewart 			 * sent our INIT. The side with the seq numbers that
1569e7e65008SMichael Tuexen 			 * are different will be the one that normally would
1570e7e65008SMichael Tuexen 			 * have hit case C. This in effect "extends" our
1571e7e65008SMichael Tuexen 			 * vtags in this collision case to be 64 bits. The
1572e7e65008SMichael Tuexen 			 * same collision could occur aka you get both vtag
1573e7e65008SMichael Tuexen 			 * and seq number the same twice in a row.. but is
1574e7e65008SMichael Tuexen 			 * much less likely. If it did happen then we would
1575e7e65008SMichael Tuexen 			 * proceed through and bring up the assoc.. we may
1576e7e65008SMichael Tuexen 			 * end up with the wrong stream setup however..
157744b7479bSRandall Stewart 			 * which would be bad.. but there is no way to
157844b7479bSRandall Stewart 			 * tell.. until we send on a stream that does not
157944b7479bSRandall Stewart 			 * exist :-)
158044b7479bSRandall Stewart 			 */
158144b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
158244b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 7;
158344b7479bSRandall Stewart 
158412dda000SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
158544b7479bSRandall Stewart 			return (NULL);
158644b7479bSRandall Stewart 		}
158744b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
158844b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 8;
1589b7d130beSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1590b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1591f8829a4aSRandall Stewart 		sctp_stop_all_cookie_timers(stcb);
1592f8829a4aSRandall Stewart 		/*
1593f8829a4aSRandall Stewart 		 * since we did not send a HB make sure we don't double
1594f8829a4aSRandall Stewart 		 * things
1595f8829a4aSRandall Stewart 		 */
1596f8829a4aSRandall Stewart 		net->hb_responded = 1;
1597f8829a4aSRandall Stewart 		if (stcb->asoc.sctp_autoclose_ticks &&
1598f8829a4aSRandall Stewart 		    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1599f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1600f8829a4aSRandall Stewart 			    NULL);
1601f8829a4aSRandall Stewart 		}
1602f8829a4aSRandall Stewart 		asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
16030066de1cSMichael Tuexen 		if (asoc->pre_open_streams < asoc->streamoutcnt) {
16040066de1cSMichael Tuexen 			asoc->pre_open_streams = asoc->streamoutcnt;
16050066de1cSMichael Tuexen 		}
1606f8829a4aSRandall Stewart 
16077f34832bSRandall Stewart 		if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
16087f34832bSRandall Stewart 			/*
16097f34832bSRandall Stewart 			 * Ok the peer probably discarded our data (if we
16107f34832bSRandall Stewart 			 * echoed a cookie+data). So anything on the
16117f34832bSRandall Stewart 			 * sent_queue should be marked for retransmit, we
16127f34832bSRandall Stewart 			 * may not get something to kick us so it COULD
16137f34832bSRandall Stewart 			 * still take a timeout to move these.. but it can't
16147f34832bSRandall Stewart 			 * hurt to mark them.
16157f34832bSRandall Stewart 			 */
16167f34832bSRandall Stewart 
16177f34832bSRandall Stewart 			TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
16187f34832bSRandall Stewart 				if (chk->sent < SCTP_DATAGRAM_RESEND) {
16197f34832bSRandall Stewart 					chk->sent = SCTP_DATAGRAM_RESEND;
1620b54d3a6cSRandall Stewart 					sctp_flight_size_decrease(chk);
1621b54d3a6cSRandall Stewart 					sctp_total_flight_decrease(stcb, chk);
16225e54f665SRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
16237f34832bSRandall Stewart 					spec_flag++;
16247f34832bSRandall Stewart 				}
16257f34832bSRandall Stewart 			}
16267f34832bSRandall Stewart 		}
1627f8829a4aSRandall Stewart 		/* process the INIT info (peer's info) */
16285f2e1835SMichael Tuexen 		if (sctp_process_init(init_cp, stcb) < 0) {
162944b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
163044b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 9;
16315f2e1835SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
16325f2e1835SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_init() failed\n");
16335f2e1835SMichael Tuexen 			sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
16345f2e1835SMichael Tuexen 			    src, dst, sh, op_err,
16355f2e1835SMichael Tuexen 			    mflowtype, mflowid,
16365f2e1835SMichael Tuexen 			    vrf_id, net->port);
1637f8829a4aSRandall Stewart 			return (NULL);
1638f8829a4aSRandall Stewart 		}
16395f2e1835SMichael Tuexen 		if ((retval = sctp_load_addresses_from_init(stcb, m,
1640f8829a4aSRandall Stewart 		    init_offset + sizeof(struct sctp_init_chunk),
16415f2e1835SMichael Tuexen 		    initack_offset, src, dst, init_src, stcb->asoc.port)) < 0) {
164244b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
164344b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 10;
16445f2e1835SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
16455f2e1835SMichael Tuexen 			    "Problem with address parameters");
16465f2e1835SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1,
16475f2e1835SMichael Tuexen 			    "Load addresses from INIT causes an abort %d\n",
16485f2e1835SMichael Tuexen 			    retval);
16495f2e1835SMichael Tuexen 			sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
16505f2e1835SMichael Tuexen 			    src, dst, sh, op_err,
16515f2e1835SMichael Tuexen 			    mflowtype, mflowid,
16525f2e1835SMichael Tuexen 			    vrf_id, net->port);
1653f8829a4aSRandall Stewart 			return (NULL);
1654f8829a4aSRandall Stewart 		}
1655839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
1656839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
1657f8829a4aSRandall Stewart 			*notification = SCTP_NOTIFY_ASSOC_UP;
1658f8829a4aSRandall Stewart 
1659f8829a4aSRandall Stewart 			if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1660f8829a4aSRandall Stewart 			    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
16615d08768aSMichael Tuexen 			    (!SCTP_IS_LISTENING(inp))) {
1662a5c2009dSMichael Tuexen 				sctp_pcb_add_flags(stcb->sctp_ep, SCTP_PCB_FLAGS_CONNECTED);
1663ceaad40aSRandall Stewart 				soisconnected(stcb->sctp_socket);
1664f8829a4aSRandall Stewart 			}
1665839d21d6SMichael Tuexen 			if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)
1666f42a358aSRandall Stewart 				SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1667f42a358aSRandall Stewart 			else
1668f42a358aSRandall Stewart 				SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1669f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1670839d21d6SMichael Tuexen 		} else if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
1671f42a358aSRandall Stewart 			SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1672f42a358aSRandall Stewart 		} else {
1673f42a358aSRandall Stewart 			SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1674f8829a4aSRandall Stewart 		}
1675839d21d6SMichael Tuexen 		SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1676f8829a4aSRandall Stewart 		sctp_stop_all_cookie_timers(stcb);
1677a5d547adSRandall Stewart 		sctp_toss_old_cookies(stcb, asoc);
1678f8829a4aSRandall Stewart 		sctp_send_cookie_ack(stcb);
16797f34832bSRandall Stewart 		if (spec_flag) {
16807f34832bSRandall Stewart 			/*
16817f34832bSRandall Stewart 			 * only if we have retrans set do we do this. What
16827f34832bSRandall Stewart 			 * this call does is get only the COOKIE-ACK out and
16837f34832bSRandall Stewart 			 * then when we return the normal call to
16847f34832bSRandall Stewart 			 * sctp_chunk_output will get the retrans out behind
16857f34832bSRandall Stewart 			 * this.
16867f34832bSRandall Stewart 			 */
1687ceaad40aSRandall Stewart 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
16887f34832bSRandall Stewart 		}
168944b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
169044b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 11;
169144b7479bSRandall Stewart 
1692f8829a4aSRandall Stewart 		return (stcb);
1693f8829a4aSRandall Stewart 	}
1694f8829a4aSRandall Stewart 	if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1695f8829a4aSRandall Stewart 	    ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1696f8829a4aSRandall Stewart 	    cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1697f8829a4aSRandall Stewart 	    cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1698f8829a4aSRandall Stewart 	    cookie->tie_tag_peer_vtag != 0) {
1699f8829a4aSRandall Stewart 		struct sctpasochead *head;
170056f778aaSMichael Tuexen 
1701830d754dSRandall Stewart 		if (asoc->peer_supports_nat) {
1702eec6aed5SMichael Tuexen 			struct sctp_tcb *local_stcb;
1703eec6aed5SMichael Tuexen 
1704830d754dSRandall Stewart 			/*
170556f778aaSMichael Tuexen 			 * This is a gross gross hack. Just call the
1706830d754dSRandall Stewart 			 * cookie_new code since we are allowing a duplicate
1707830d754dSRandall Stewart 			 * association. I hope this works...
1708830d754dSRandall Stewart 			 */
1709eec6aed5SMichael Tuexen 			local_stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst,
1710b1754ad1SMichael Tuexen 			    sh, cookie, cookie_len,
1711830d754dSRandall Stewart 			    inp, netp, init_src, notification,
1712830d754dSRandall Stewart 			    auth_skipped, auth_offset, auth_len,
1713457b4b88SMichael Tuexen 			    mflowtype, mflowid,
1714eec6aed5SMichael Tuexen 			    vrf_id, port);
1715eec6aed5SMichael Tuexen 			if (local_stcb == NULL) {
1716eec6aed5SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
1717eec6aed5SMichael Tuexen 			}
1718eec6aed5SMichael Tuexen 			return (local_stcb);
1719830d754dSRandall Stewart 		}
1720f8829a4aSRandall Stewart 		/*
1721f8829a4aSRandall Stewart 		 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1722f8829a4aSRandall Stewart 		 */
1723a5d547adSRandall Stewart 		/* temp code */
172444b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
172544b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 12;
1726a89481d3SMichael Tuexen 		sctp_stop_association_timers(stcb, false);
1727f8829a4aSRandall Stewart 		/* notify upper layer */
1728f8829a4aSRandall Stewart 		*notification = SCTP_NOTIFY_ASSOC_RESTART;
1729a5d547adSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
1730839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) != SCTP_STATE_OPEN) &&
1731839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1732839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT)) {
1733f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1734f42a358aSRandall Stewart 		}
1735839d21d6SMichael Tuexen 		if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
1736f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1737839d21d6SMichael Tuexen 		} else if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) {
1738f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1739f42a358aSRandall Stewart 		}
1740139bc87fSRandall Stewart 		if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1741839d21d6SMichael Tuexen 			SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1742139bc87fSRandall Stewart 
1743839d21d6SMichael Tuexen 		} else if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) {
1744139bc87fSRandall Stewart 			/* move to OPEN state, if not in SHUTDOWN_SENT */
1745839d21d6SMichael Tuexen 			SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1746139bc87fSRandall Stewart 		}
17470066de1cSMichael Tuexen 		if (asoc->pre_open_streams < asoc->streamoutcnt) {
17480066de1cSMichael Tuexen 			asoc->pre_open_streams = asoc->streamoutcnt;
17490066de1cSMichael Tuexen 		}
1750139bc87fSRandall Stewart 		asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1751139bc87fSRandall Stewart 		asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1752c54a18d2SRandall Stewart 		asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1753139bc87fSRandall Stewart 		asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1754139bc87fSRandall Stewart 		asoc->str_reset_seq_in = asoc->init_seq_number;
1755139bc87fSRandall Stewart 		asoc->advanced_peer_ack_point = asoc->last_acked_seq;
17566f155d69SMichael Tuexen 		asoc->send_sack = 1;
1757a89481d3SMichael Tuexen 		asoc->data_pkts_seen = 0;
1758a89481d3SMichael Tuexen 		asoc->last_data_chunk_from = NULL;
1759a89481d3SMichael Tuexen 		asoc->last_control_chunk_from = NULL;
1760a89481d3SMichael Tuexen 		asoc->last_net_cmt_send_started = NULL;
17610696e120SRandall Stewart 		if (asoc->mapping_array) {
1762139bc87fSRandall Stewart 			memset(asoc->mapping_array, 0,
1763139bc87fSRandall Stewart 			    asoc->mapping_array_size);
17640696e120SRandall Stewart 		}
176577acdc25SRandall Stewart 		if (asoc->nr_mapping_array) {
1766830d754dSRandall Stewart 			memset(asoc->nr_mapping_array, 0,
1767b5c16493SMichael Tuexen 			    asoc->mapping_array_size);
1768830d754dSRandall Stewart 		}
1769a5d547adSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
1770a5d547adSRandall Stewart 		SCTP_INP_INFO_WLOCK();
1771a5d547adSRandall Stewart 		SCTP_INP_WLOCK(stcb->sctp_ep);
1772a5d547adSRandall Stewart 		SCTP_TCB_LOCK(stcb);
17733c1ba6f3SMichael Tuexen 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
1774f8829a4aSRandall Stewart 		/* send up all the data */
1775f5d30f7fSMichael Tuexen 		sctp_report_all_outbound(stcb, 0, SCTP_SO_LOCKED);
1776a5d547adSRandall Stewart 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1777325c8c46SMichael Tuexen 			stcb->asoc.strmout[i].chunks_on_queues = 0;
1778f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
1779f0396ad1SMichael Tuexen 			for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1780f0396ad1SMichael Tuexen 				asoc->strmout[i].abandoned_sent[j] = 0;
1781f0396ad1SMichael Tuexen 				asoc->strmout[i].abandoned_unsent[j] = 0;
1782f0396ad1SMichael Tuexen 			}
1783f0396ad1SMichael Tuexen #else
1784f0396ad1SMichael Tuexen 			asoc->strmout[i].abandoned_sent[0] = 0;
1785f0396ad1SMichael Tuexen 			asoc->strmout[i].abandoned_unsent[0] = 0;
1786f0396ad1SMichael Tuexen #endif
178763d5b568SMichael Tuexen 			stcb->asoc.strmout[i].next_mid_ordered = 0;
178863d5b568SMichael Tuexen 			stcb->asoc.strmout[i].next_mid_unordered = 0;
17897a051c0aSMichael Tuexen 			stcb->asoc.strmout[i].sid = i;
1790a5d547adSRandall Stewart 			stcb->asoc.strmout[i].last_msg_incomplete = 0;
1791a5d547adSRandall Stewart 		}
1792aa6db9a0SMichael Tuexen 		TAILQ_FOREACH_SAFE(strrst, &asoc->resetHead, next_resp, nstrrst) {
1793aa6db9a0SMichael Tuexen 			TAILQ_REMOVE(&asoc->resetHead, strrst, next_resp);
1794aa6db9a0SMichael Tuexen 			SCTP_FREE(strrst, SCTP_M_STRESET);
1795aa6db9a0SMichael Tuexen 		}
1796aa6db9a0SMichael Tuexen 		TAILQ_FOREACH_SAFE(sq, &asoc->pending_reply_queue, next, nsq) {
1797aa6db9a0SMichael Tuexen 			TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
1798aa6db9a0SMichael Tuexen 			if (sq->data) {
1799aa6db9a0SMichael Tuexen 				sctp_m_freem(sq->data);
1800aa6db9a0SMichael Tuexen 				sq->data = NULL;
1801aa6db9a0SMichael Tuexen 			}
1802aa6db9a0SMichael Tuexen 			sctp_free_remote_addr(sq->whoFrom);
1803aa6db9a0SMichael Tuexen 			sq->whoFrom = NULL;
1804aa6db9a0SMichael Tuexen 			sq->stcb = NULL;
1805aa6db9a0SMichael Tuexen 			sctp_free_a_readq(stcb, sq);
1806aa6db9a0SMichael Tuexen 		}
1807aa6db9a0SMichael Tuexen 		TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
1808aa6db9a0SMichael Tuexen 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
1809aa6db9a0SMichael Tuexen 			if (chk->data) {
1810aa6db9a0SMichael Tuexen 				sctp_m_freem(chk->data);
1811aa6db9a0SMichael Tuexen 				chk->data = NULL;
1812aa6db9a0SMichael Tuexen 			}
1813aa6db9a0SMichael Tuexen 			if (chk->holds_key_ref)
1814aa6db9a0SMichael Tuexen 				sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
1815aa6db9a0SMichael Tuexen 			sctp_free_remote_addr(chk->whoTo);
1816aa6db9a0SMichael Tuexen 			SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
1817aa6db9a0SMichael Tuexen 			SCTP_DECR_CHK_COUNT();
1818aa6db9a0SMichael Tuexen 		}
1819a89481d3SMichael Tuexen 		asoc->ctrl_queue_cnt = 0;
1820a89481d3SMichael Tuexen 		asoc->str_reset = NULL;
1821a89481d3SMichael Tuexen 		asoc->stream_reset_outstanding = 0;
1822aa6db9a0SMichael Tuexen 		TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
1823aa6db9a0SMichael Tuexen 			TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
1824aa6db9a0SMichael Tuexen 			if (chk->data) {
1825aa6db9a0SMichael Tuexen 				sctp_m_freem(chk->data);
1826aa6db9a0SMichael Tuexen 				chk->data = NULL;
1827aa6db9a0SMichael Tuexen 			}
1828aa6db9a0SMichael Tuexen 			if (chk->holds_key_ref)
1829aa6db9a0SMichael Tuexen 				sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
1830aa6db9a0SMichael Tuexen 			sctp_free_remote_addr(chk->whoTo);
1831aa6db9a0SMichael Tuexen 			SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
1832aa6db9a0SMichael Tuexen 			SCTP_DECR_CHK_COUNT();
1833aa6db9a0SMichael Tuexen 		}
1834aa6db9a0SMichael Tuexen 		TAILQ_FOREACH_SAFE(aparam, &asoc->asconf_queue, next, naparam) {
1835aa6db9a0SMichael Tuexen 			TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
1836aa6db9a0SMichael Tuexen 			SCTP_FREE(aparam, SCTP_M_ASC_ADDR);
1837aa6db9a0SMichael Tuexen 		}
1838aa6db9a0SMichael Tuexen 		TAILQ_FOREACH_SAFE(aack, &asoc->asconf_ack_sent, next, naack) {
1839aa6db9a0SMichael Tuexen 			TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next);
1840aa6db9a0SMichael Tuexen 			if (aack->data != NULL) {
1841aa6db9a0SMichael Tuexen 				sctp_m_freem(aack->data);
1842aa6db9a0SMichael Tuexen 			}
1843aa6db9a0SMichael Tuexen 			SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack);
1844aa6db9a0SMichael Tuexen 		}
184552640d61SMichael Tuexen 		asoc->rcv_edmid = cookie->rcv_edmid;
1846aa6db9a0SMichael Tuexen 
1847f8829a4aSRandall Stewart 		/* process the INIT-ACK info (my info) */
1848f8829a4aSRandall Stewart 		asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1849f8829a4aSRandall Stewart 		asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1850f8829a4aSRandall Stewart 
1851f8829a4aSRandall Stewart 		/* pull from vtag hash */
1852f8829a4aSRandall Stewart 		LIST_REMOVE(stcb, sctp_asocs);
1853f8829a4aSRandall Stewart 		/* re-insert to new vtag position */
1854b3f1ea41SRandall Stewart 		head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1855b3f1ea41SRandall Stewart 		    SCTP_BASE_INFO(hashasocmark))];
1856f8829a4aSRandall Stewart 		/*
1857f8829a4aSRandall Stewart 		 * put it in the bucket in the vtag hash of assoc's for the
1858f8829a4aSRandall Stewart 		 * system
1859f8829a4aSRandall Stewart 		 */
1860f8829a4aSRandall Stewart 		LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1861f8829a4aSRandall Stewart 
1862a5d547adSRandall Stewart 		SCTP_INP_WUNLOCK(stcb->sctp_ep);
1863a5d547adSRandall Stewart 		SCTP_INP_INFO_WUNLOCK();
186456f778aaSMichael Tuexen 		asoc->total_flight = 0;
186556f778aaSMichael Tuexen 		asoc->total_flight_count = 0;
186656f778aaSMichael Tuexen 		/* process the INIT info (peer's info) */
18675f2e1835SMichael Tuexen 		if (sctp_process_init(init_cp, stcb) < 0) {
186844b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
186944b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 13;
18705f2e1835SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
18715f2e1835SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_init() failed\n");
18725f2e1835SMichael Tuexen 			sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
18735f2e1835SMichael Tuexen 			    src, dst, sh, op_err,
18745f2e1835SMichael Tuexen 			    mflowtype, mflowid,
18755f2e1835SMichael Tuexen 			    vrf_id, net->port);
1876f8829a4aSRandall Stewart 			return (NULL);
1877f8829a4aSRandall Stewart 		}
1878f8829a4aSRandall Stewart 		/*
1879f8829a4aSRandall Stewart 		 * since we did not send a HB make sure we don't double
1880f8829a4aSRandall Stewart 		 * things
1881f8829a4aSRandall Stewart 		 */
1882f8829a4aSRandall Stewart 		net->hb_responded = 1;
1883f8829a4aSRandall Stewart 
18845f2e1835SMichael Tuexen 		if ((retval = sctp_load_addresses_from_init(stcb, m,
1885f8829a4aSRandall Stewart 		    init_offset + sizeof(struct sctp_init_chunk),
18865f2e1835SMichael Tuexen 		    initack_offset, src, dst, init_src, stcb->asoc.port)) < 0) {
188744b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
188844b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 14;
18895f2e1835SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
18905f2e1835SMichael Tuexen 			    "Problem with address parameters");
18915f2e1835SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1,
18925f2e1835SMichael Tuexen 			    "Load addresses from INIT causes an abort %d\n",
18935f2e1835SMichael Tuexen 			    retval);
18945f2e1835SMichael Tuexen 			sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
18955f2e1835SMichael Tuexen 			    src, dst, sh, op_err,
18965f2e1835SMichael Tuexen 			    mflowtype, mflowid,
18975f2e1835SMichael Tuexen 			    vrf_id, net->port);
1898f8829a4aSRandall Stewart 			return (NULL);
1899f8829a4aSRandall Stewart 		}
1900f8829a4aSRandall Stewart 		/* respond with a COOKIE-ACK */
1901f8829a4aSRandall Stewart 		sctp_send_cookie_ack(stcb);
190244b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
190344b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 15;
1904a89481d3SMichael Tuexen 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE) &&
1905a89481d3SMichael Tuexen 		    (asoc->sctp_autoclose_ticks > 0)) {
1906a89481d3SMichael Tuexen 			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
1907a89481d3SMichael Tuexen 		}
1908f8829a4aSRandall Stewart 		return (stcb);
1909f8829a4aSRandall Stewart 	}
191044b7479bSRandall Stewart 	if (how_indx < sizeof(asoc->cookie_how))
191144b7479bSRandall Stewart 		asoc->cookie_how[how_indx] = 16;
1912f8829a4aSRandall Stewart 	/* all other cases... */
191312dda000SMichael Tuexen 	SCTP_TCB_UNLOCK(stcb);
1914f8829a4aSRandall Stewart 	return (NULL);
1915f8829a4aSRandall Stewart }
1916f8829a4aSRandall Stewart 
1917f8829a4aSRandall Stewart /*
1918f8829a4aSRandall Stewart  * handle a state cookie for a new association m: input packet mbuf chain--
1919f8829a4aSRandall Stewart  * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
1920f8829a4aSRandall Stewart  * and the cookie signature does not exist offset: offset into mbuf to the
1921f8829a4aSRandall Stewart  * cookie-echo chunk length: length of the cookie chunk to: where the init
1922f8829a4aSRandall Stewart  * was from returns a new TCB
1923f8829a4aSRandall Stewart  */
1924f30ac432SMichael Tuexen static struct sctp_tcb *
1925f8829a4aSRandall Stewart sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1926b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
1927f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1928f8829a4aSRandall Stewart     struct sctp_inpcb *inp, struct sctp_nets **netp,
1929f8829a4aSRandall Stewart     struct sockaddr *init_src, int *notification,
193017205eccSRandall Stewart     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1931457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
1932c54a18d2SRandall Stewart     uint32_t vrf_id, uint16_t port)
1933f8829a4aSRandall Stewart {
1934f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
1935f8829a4aSRandall Stewart 	struct sctp_init_chunk *init_cp, init_buf;
1936f8829a4aSRandall Stewart 	struct sctp_init_ack_chunk *initack_cp, initack_buf;
193724aaac8dSMichael Tuexen 	union sctp_sockstore store;
1938f8829a4aSRandall Stewart 	struct sctp_association *asoc;
1939f8829a4aSRandall Stewart 	int init_offset, initack_offset, initack_limit;
1940f8829a4aSRandall Stewart 	int error = 0;
19418262311cSMichael Tuexen 	uint8_t auth_chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
1942ceaad40aSRandall Stewart 
1943f8829a4aSRandall Stewart 	/*
1944f8829a4aSRandall Stewart 	 * find and validate the INIT chunk in the cookie (peer's info) the
1945f8829a4aSRandall Stewart 	 * INIT should start after the cookie-echo header struct (chunk
1946f8829a4aSRandall Stewart 	 * header, state cookie header struct)
1947f8829a4aSRandall Stewart 	 */
1948f8829a4aSRandall Stewart 	init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
1949f8829a4aSRandall Stewart 	init_cp = (struct sctp_init_chunk *)
1950f8829a4aSRandall Stewart 	    sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1951f8829a4aSRandall Stewart 	    (uint8_t *)&init_buf);
1952f8829a4aSRandall Stewart 	if (init_cp == NULL) {
1953f8829a4aSRandall Stewart 		/* could not pull a INIT chunk in cookie */
1954ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1,
1955ad81507eSRandall Stewart 		    "process_cookie_new: could not pull INIT chunk hdr\n");
1956f8829a4aSRandall Stewart 		return (NULL);
1957f8829a4aSRandall Stewart 	}
1958f8829a4aSRandall Stewart 	if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1959ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
1960f8829a4aSRandall Stewart 		return (NULL);
1961f8829a4aSRandall Stewart 	}
196260990c0cSMichael Tuexen 	initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1963f8829a4aSRandall Stewart 	/*
1964f8829a4aSRandall Stewart 	 * find and validate the INIT-ACK chunk in the cookie (my info) the
1965f8829a4aSRandall Stewart 	 * INIT-ACK follows the INIT chunk
1966f8829a4aSRandall Stewart 	 */
1967f8829a4aSRandall Stewart 	initack_cp = (struct sctp_init_ack_chunk *)
1968f8829a4aSRandall Stewart 	    sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1969f8829a4aSRandall Stewart 	    (uint8_t *)&initack_buf);
1970f8829a4aSRandall Stewart 	if (initack_cp == NULL) {
1971f8829a4aSRandall Stewart 		/* could not pull INIT-ACK chunk in cookie */
1972ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
1973f8829a4aSRandall Stewart 		return (NULL);
1974f8829a4aSRandall Stewart 	}
1975f8829a4aSRandall Stewart 	if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1976f8829a4aSRandall Stewart 		return (NULL);
1977f8829a4aSRandall Stewart 	}
1978f8829a4aSRandall Stewart 	/*
1979f8829a4aSRandall Stewart 	 * NOTE: We can't use the INIT_ACK's chk_length to determine the
1980f8829a4aSRandall Stewart 	 * "initack_limit" value.  This is because the chk_length field
1981f8829a4aSRandall Stewart 	 * includes the length of the cookie, but the cookie is omitted when
1982f8829a4aSRandall Stewart 	 * the INIT and INIT_ACK are tacked onto the cookie...
1983f8829a4aSRandall Stewart 	 */
1984f8829a4aSRandall Stewart 	initack_limit = offset + cookie_len;
1985f8829a4aSRandall Stewart 
1986f8829a4aSRandall Stewart 	/*
1987f8829a4aSRandall Stewart 	 * now that we know the INIT/INIT-ACK are in place, create a new TCB
1988e7e65008SMichael Tuexen 	 * and populate
1989f8829a4aSRandall Stewart 	 */
199052be287eSRandall Stewart 
199152be287eSRandall Stewart 	/*
199252be287eSRandall Stewart 	 * Here we do a trick, we set in NULL for the proc/thread argument.
199352be287eSRandall Stewart 	 * We do this since in effect we only use the p argument when the
199452be287eSRandall Stewart 	 * socket is unbound and we must do an implicit bind. Since we are
199552be287eSRandall Stewart 	 * getting a cookie, we cannot be unbound.
199652be287eSRandall Stewart 	 */
1997b5c16493SMichael Tuexen 	stcb = sctp_aloc_assoc(inp, init_src, &error,
1998c7f048abSMichael Tuexen 	    ntohl(initack_cp->init.initiate_tag),
1999c7f048abSMichael Tuexen 	    ntohl(initack_cp->init.initial_tsn), vrf_id,
2000c979034bSMichael Tuexen 	    ntohs(initack_cp->init.num_outbound_streams),
2001ec70917fSMichael Tuexen 	    port,
20028a956abeSMichael Tuexen 	    (struct thread *)NULL,
20038a956abeSMichael Tuexen 	    SCTP_DONT_INITIALIZE_AUTH_PARAMS);
2004f8829a4aSRandall Stewart 	if (stcb == NULL) {
2005f8829a4aSRandall Stewart 		struct mbuf *op_err;
2006f8829a4aSRandall Stewart 
2007f8829a4aSRandall Stewart 		/* memory problem? */
2008ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1,
2009ad81507eSRandall Stewart 		    "process_cookie_new: no room for another TCB!\n");
2010ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2011f8829a4aSRandall Stewart 		sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2012b1754ad1SMichael Tuexen 		    src, dst, sh, op_err,
2013457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2014f30ac432SMichael Tuexen 		    vrf_id, port);
2015f8829a4aSRandall Stewart 		return (NULL);
2016f8829a4aSRandall Stewart 	}
2017f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2018f8829a4aSRandall Stewart 	/* get scope variables out of cookie */
2019a1cb341bSMichael Tuexen 	asoc->scope.ipv4_local_scope = cookie->ipv4_scope;
2020a1cb341bSMichael Tuexen 	asoc->scope.site_scope = cookie->site_scope;
2021a1cb341bSMichael Tuexen 	asoc->scope.local_scope = cookie->local_scope;
2022a1cb341bSMichael Tuexen 	asoc->scope.loopback_scope = cookie->loopback_scope;
2023f8829a4aSRandall Stewart 
2024a1cb341bSMichael Tuexen 	if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2025a1cb341bSMichael Tuexen 	    (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2026f8829a4aSRandall Stewart 		struct mbuf *op_err;
2027f8829a4aSRandall Stewart 
2028f8829a4aSRandall Stewart 		/*
2029f8829a4aSRandall Stewart 		 * Houston we have a problem. The EP changed while the
2030f8829a4aSRandall Stewart 		 * cookie was in flight. Only recourse is to abort the
2031f8829a4aSRandall Stewart 		 * association.
2032f8829a4aSRandall Stewart 		 */
2033ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2034f8829a4aSRandall Stewart 		sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2035b1754ad1SMichael Tuexen 		    src, dst, sh, op_err,
2036457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2037f30ac432SMichael Tuexen 		    vrf_id, port);
2038c4739e2fSRandall Stewart 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2039b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2040f8829a4aSRandall Stewart 		return (NULL);
2041f8829a4aSRandall Stewart 	}
204252640d61SMichael Tuexen 	asoc->rcv_edmid = cookie->rcv_edmid;
2043f8829a4aSRandall Stewart 	/* process the INIT-ACK info (my info) */
2044f8829a4aSRandall Stewart 	asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2045f8829a4aSRandall Stewart 
2046f8829a4aSRandall Stewart 	/* process the INIT info (peer's info) */
20475f2e1835SMichael Tuexen 	if (sctp_process_init(init_cp, stcb) < 0) {
2048b7d130beSMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2049b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2050f8829a4aSRandall Stewart 		return (NULL);
2051f8829a4aSRandall Stewart 	}
2052f8829a4aSRandall Stewart 	/* load all addresses */
205345421646SMichael Tuexen 	if (sctp_load_addresses_from_init(stcb, m,
20545f2e1835SMichael Tuexen 	    init_offset + sizeof(struct sctp_init_chunk),
205545421646SMichael Tuexen 	    initack_offset, src, dst, init_src, port) < 0) {
2056b7d130beSMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2057b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2058f8829a4aSRandall Stewart 		return (NULL);
2059f8829a4aSRandall Stewart 	}
2060f8829a4aSRandall Stewart 	/*
2061f8829a4aSRandall Stewart 	 * verify any preceding AUTH chunk that was skipped
2062f8829a4aSRandall Stewart 	 */
2063f8829a4aSRandall Stewart 	/* pull the local authentication parameters from the cookie/init-ack */
2064f8829a4aSRandall Stewart 	sctp_auth_get_cookie_params(stcb, m,
2065f8829a4aSRandall Stewart 	    initack_offset + sizeof(struct sctp_init_ack_chunk),
2066f8829a4aSRandall Stewart 	    initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2067f8829a4aSRandall Stewart 	if (auth_skipped) {
2068f8829a4aSRandall Stewart 		struct sctp_auth_chunk *auth;
2069f8829a4aSRandall Stewart 
20708262311cSMichael Tuexen 		if (auth_len <= SCTP_CHUNK_BUFFER_SIZE) {
207197feba89SMichael Tuexen 			auth = (struct sctp_auth_chunk *)sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
207297feba89SMichael Tuexen 		} else {
207397feba89SMichael Tuexen 			auth = NULL;
207497feba89SMichael Tuexen 		}
2075ad81507eSRandall Stewart 		if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2076f8829a4aSRandall Stewart 			/* auth HMAC failed, dump the assoc and packet */
2077ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_AUTH1,
2078ad81507eSRandall Stewart 			    "COOKIE-ECHO: AUTH failed\n");
2079b7d130beSMichael Tuexen 			(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2080b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_21);
2081f8829a4aSRandall Stewart 			return (NULL);
2082f8829a4aSRandall Stewart 		} else {
2083f8829a4aSRandall Stewart 			/* remaining chunks checked... good to go */
2084f8829a4aSRandall Stewart 			stcb->asoc.authenticated = 1;
2085f8829a4aSRandall Stewart 		}
2086f8829a4aSRandall Stewart 	}
20870053ed28SMichael Tuexen 
2088f8829a4aSRandall Stewart 	/*
2089f8829a4aSRandall Stewart 	 * if we're doing ASCONFs, check to see if we have any new local
2090f8829a4aSRandall Stewart 	 * addresses that need to get added to the peer (eg. addresses
2091f8829a4aSRandall Stewart 	 * changed while cookie echo in flight).  This needs to be done
2092f8829a4aSRandall Stewart 	 * after we go to the OPEN state to do the correct asconf
2093f8829a4aSRandall Stewart 	 * processing. else, make sure we have the correct addresses in our
2094f8829a4aSRandall Stewart 	 * lists
2095f8829a4aSRandall Stewart 	 */
2096f8829a4aSRandall Stewart 
2097f8829a4aSRandall Stewart 	/* warning, we re-use sin, sin6, sa_store here! */
2098f8829a4aSRandall Stewart 	/* pull in local_address (our "from" address) */
2099e6194c2eSMichael Tuexen 	switch (cookie->laddr_type) {
2100e6194c2eSMichael Tuexen #ifdef INET
2101e6194c2eSMichael Tuexen 	case SCTP_IPV4_ADDRESS:
2102f8829a4aSRandall Stewart 		/* source addr is IPv4 */
210324aaac8dSMichael Tuexen 		memset(&store.sin, 0, sizeof(struct sockaddr_in));
210424aaac8dSMichael Tuexen 		store.sin.sin_family = AF_INET;
210524aaac8dSMichael Tuexen 		store.sin.sin_len = sizeof(struct sockaddr_in);
210624aaac8dSMichael Tuexen 		store.sin.sin_addr.s_addr = cookie->laddress[0];
2107e6194c2eSMichael Tuexen 		break;
2108e6194c2eSMichael Tuexen #endif
2109e6194c2eSMichael Tuexen #ifdef INET6
2110e6194c2eSMichael Tuexen 	case SCTP_IPV6_ADDRESS:
2111f8829a4aSRandall Stewart 		/* source addr is IPv6 */
211224aaac8dSMichael Tuexen 		memset(&store.sin6, 0, sizeof(struct sockaddr_in6));
211324aaac8dSMichael Tuexen 		store.sin6.sin6_family = AF_INET6;
211424aaac8dSMichael Tuexen 		store.sin6.sin6_len = sizeof(struct sockaddr_in6);
211524aaac8dSMichael Tuexen 		store.sin6.sin6_scope_id = cookie->scope_id;
211623602b60SMichael Tuexen 		memcpy(&store.sin6.sin6_addr, cookie->laddress, sizeof(struct in6_addr));
2117e6194c2eSMichael Tuexen 		break;
2118e6194c2eSMichael Tuexen #endif
2119e6194c2eSMichael Tuexen 	default:
2120b7d130beSMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2121b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
2122f8829a4aSRandall Stewart 		return (NULL);
2123f8829a4aSRandall Stewart 	}
2124f8829a4aSRandall Stewart 
21251698cbd9SMichael Tuexen 	/* update current state */
21261698cbd9SMichael Tuexen 	SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2127839d21d6SMichael Tuexen 	SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
21281698cbd9SMichael Tuexen 	sctp_stop_all_cookie_timers(stcb);
21291698cbd9SMichael Tuexen 	SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
21301698cbd9SMichael Tuexen 	SCTP_STAT_INCR_GAUGE32(sctps_currestab);
21311698cbd9SMichael Tuexen 
2132f8829a4aSRandall Stewart 	/* set up to notify upper layer */
2133f8829a4aSRandall Stewart 	*notification = SCTP_NOTIFY_ASSOC_UP;
2134f8829a4aSRandall Stewart 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2135f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
21365d08768aSMichael Tuexen 	    (!SCTP_IS_LISTENING(inp))) {
2137f8829a4aSRandall Stewart 		/*
2138f8829a4aSRandall Stewart 		 * This is an endpoint that called connect() how it got a
2139f8829a4aSRandall Stewart 		 * cookie that is NEW is a bit of a mystery. It must be that
2140f8829a4aSRandall Stewart 		 * the INIT was sent, but before it got there.. a complete
2141f8829a4aSRandall Stewart 		 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2142f8829a4aSRandall Stewart 		 * should have went to the other code.. not here.. oh well..
2143f8829a4aSRandall Stewart 		 * a bit of protection is worth having..
21442d5c48ecSMark Johnston 		 *
21452d5c48ecSMark Johnston 		 * XXXMJ unlocked
2146f8829a4aSRandall Stewart 		 */
2147a5c2009dSMichael Tuexen 		sctp_pcb_add_flags(stcb->sctp_ep, SCTP_PCB_FLAGS_CONNECTED);
2148ceaad40aSRandall Stewart 		soisconnected(stcb->sctp_socket);
2149f8829a4aSRandall Stewart 	} else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
21505d08768aSMichael Tuexen 	    (SCTP_IS_LISTENING(inp))) {
2151f8829a4aSRandall Stewart 		/*
2152f8829a4aSRandall Stewart 		 * We don't want to do anything with this one. Since it is
2153f8829a4aSRandall Stewart 		 * the listening guy. The timer will get started for
2154f8829a4aSRandall Stewart 		 * accepted connections in the caller.
2155f8829a4aSRandall Stewart 		 */
2156f8829a4aSRandall Stewart 		;
2157f8829a4aSRandall Stewart 	}
2158f8829a4aSRandall Stewart 	if (stcb->asoc.sctp_autoclose_ticks &&
2159f8829a4aSRandall Stewart 	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2160f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2161f8829a4aSRandall Stewart 	}
2162b54d3a6cSRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2163b15f5411SMichael Tuexen 	*netp = sctp_findnet(stcb, init_src);
2164b15f5411SMichael Tuexen 	if (*netp != NULL) {
2165b15f5411SMichael Tuexen 		/*
2166b15f5411SMichael Tuexen 		 * Since we did not send a HB, make sure we don't double
2167b15f5411SMichael Tuexen 		 * things.
2168b15f5411SMichael Tuexen 		 */
2169b15f5411SMichael Tuexen 		(*netp)->hb_responded = 1;
21709a972525SRandall Stewart 	}
21713232788eSRandall Stewart 	/* respond with a COOKIE-ACK */
2172f8829a4aSRandall Stewart 	sctp_send_cookie_ack(stcb);
21733232788eSRandall Stewart 
21743232788eSRandall Stewart 	/*
21753232788eSRandall Stewart 	 * check the address lists for any ASCONFs that need to be sent
21763232788eSRandall Stewart 	 * AFTER the cookie-ack is sent
21773232788eSRandall Stewart 	 */
21783232788eSRandall Stewart 	sctp_check_address_list(stcb, m,
21793232788eSRandall Stewart 	    initack_offset + sizeof(struct sctp_init_ack_chunk),
21803232788eSRandall Stewart 	    initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
218124aaac8dSMichael Tuexen 	    &store.sa, cookie->local_scope, cookie->site_scope,
21823232788eSRandall Stewart 	    cookie->ipv4_scope, cookie->loopback_scope);
21833232788eSRandall Stewart 
2184f8829a4aSRandall Stewart 	return (stcb);
2185f8829a4aSRandall Stewart }
2186f8829a4aSRandall Stewart 
2187830d754dSRandall Stewart /*
2188830d754dSRandall Stewart  * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2189830d754dSRandall Stewart  * we NEED to make sure we are not already using the vtag. If so we
2190830d754dSRandall Stewart  * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2191830d754dSRandall Stewart 	head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2192830d754dSRandall Stewart 							    SCTP_BASE_INFO(hashasocmark))];
2193830d754dSRandall Stewart 	LIST_FOREACH(stcb, head, sctp_asocs) {
2194830d754dSRandall Stewart 	        if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2195830d754dSRandall Stewart 		       -- SEND ABORT - TRY AGAIN --
2196830d754dSRandall Stewart 		}
2197830d754dSRandall Stewart 	}
2198830d754dSRandall Stewart */
2199f8829a4aSRandall Stewart 
2200f8829a4aSRandall Stewart /*
2201f8829a4aSRandall Stewart  * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2202f8829a4aSRandall Stewart  * existing (non-NULL) TCB
2203f8829a4aSRandall Stewart  */
2204f8829a4aSRandall Stewart static struct mbuf *
2205f8829a4aSRandall Stewart sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2206b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
2207f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2208f8829a4aSRandall Stewart     struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
220917205eccSRandall Stewart     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2210f30ac432SMichael Tuexen     struct sctp_tcb **locked_tcb,
2211457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
2212f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
2213f8829a4aSRandall Stewart {
2214f8829a4aSRandall Stewart 	struct sctp_state_cookie *cookie;
2215f8829a4aSRandall Stewart 	struct sctp_tcb *l_stcb = *stcb;
2216f8829a4aSRandall Stewart 	struct sctp_inpcb *l_inp;
2217f8829a4aSRandall Stewart 	struct sockaddr *to;
2218f8829a4aSRandall Stewart 	struct sctp_pcb *ep;
2219f8829a4aSRandall Stewart 	struct mbuf *m_sig;
2220f8829a4aSRandall Stewart 	uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2221f8829a4aSRandall Stewart 	uint8_t *sig;
2222f8829a4aSRandall Stewart 	uint8_t cookie_ok = 0;
2223329204ffSMichael Tuexen 	unsigned int sig_offset, cookie_offset;
2224f8829a4aSRandall Stewart 	unsigned int cookie_len;
2225f8829a4aSRandall Stewart 	struct timeval now;
2226a92d5016SMichael Tuexen 	struct timeval time_entered, time_expires;
2227f8829a4aSRandall Stewart 	int notification = 0;
2228f8829a4aSRandall Stewart 	struct sctp_nets *netl;
2229f8829a4aSRandall Stewart 	int had_a_existing_tcb = 0;
22302fad0e55SMichael Tuexen 	int send_int_conf = 0;
2231e6194c2eSMichael Tuexen #ifdef INET
2232e6194c2eSMichael Tuexen 	struct sockaddr_in sin;
2233e6194c2eSMichael Tuexen #endif
2234e6194c2eSMichael Tuexen #ifdef INET6
2235e6194c2eSMichael Tuexen 	struct sockaddr_in6 sin6;
2236e6194c2eSMichael Tuexen #endif
2237e6194c2eSMichael Tuexen 
2238ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
2239ad81507eSRandall Stewart 	    "sctp_handle_cookie: handling COOKIE-ECHO\n");
2240f8829a4aSRandall Stewart 
2241f8829a4aSRandall Stewart 	if (inp_p == NULL) {
2242f8829a4aSRandall Stewart 		return (NULL);
2243f8829a4aSRandall Stewart 	}
2244f8829a4aSRandall Stewart 	cookie = &cp->cookie;
2245f8829a4aSRandall Stewart 	cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2246f8829a4aSRandall Stewart 	cookie_len = ntohs(cp->ch.chunk_length);
2247f8829a4aSRandall Stewart 
2248d44b45dfSMichael Tuexen 	if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2249d44b45dfSMichael Tuexen 	    sizeof(struct sctp_init_chunk) +
2250d44b45dfSMichael Tuexen 	    sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2251d44b45dfSMichael Tuexen 		/* cookie too small */
2252d44b45dfSMichael Tuexen 		return (NULL);
2253d44b45dfSMichael Tuexen 	}
22543db4ea95SMichael Tuexen 	if ((cookie->peerport != sh->src_port) ||
22553db4ea95SMichael Tuexen 	    (cookie->myport != sh->dest_port) ||
2256f8829a4aSRandall Stewart 	    (cookie->my_vtag != sh->v_tag)) {
2257f8829a4aSRandall Stewart 		/*
2258f8829a4aSRandall Stewart 		 * invalid ports or bad tag.  Note that we always leave the
2259f8829a4aSRandall Stewart 		 * v_tag in the header in network order and when we stored
2260f8829a4aSRandall Stewart 		 * it in the my_vtag slot we also left it in network order.
226117205eccSRandall Stewart 		 * This maintains the match even though it may be in the
2262f8829a4aSRandall Stewart 		 * opposite byte order of the machine :->
2263f8829a4aSRandall Stewart 		 */
2264f8829a4aSRandall Stewart 		return (NULL);
2265f8829a4aSRandall Stewart 	}
2266f8829a4aSRandall Stewart 	/*
2267f8829a4aSRandall Stewart 	 * split off the signature into its own mbuf (since it should not be
2268f8829a4aSRandall Stewart 	 * calculated in the sctp_hmac_m() call).
2269f8829a4aSRandall Stewart 	 */
2270f8829a4aSRandall Stewart 	sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2271eb1b1807SGleb Smirnoff 	m_sig = m_split(m, sig_offset, M_NOWAIT);
2272f8829a4aSRandall Stewart 	if (m_sig == NULL) {
2273f8829a4aSRandall Stewart 		/* out of memory or ?? */
2274f8829a4aSRandall Stewart 		return (NULL);
2275f8829a4aSRandall Stewart 	}
2276eadccaccSRandall Stewart #ifdef SCTP_MBUF_LOGGING
2277b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
22784be807c4SMichael Tuexen 		sctp_log_mbc(m_sig, SCTP_MBUF_SPLIT);
2279eadccaccSRandall Stewart 	}
2280eadccaccSRandall Stewart #endif
2281eadccaccSRandall Stewart 
2282f8829a4aSRandall Stewart 	/*
2283f8829a4aSRandall Stewart 	 * compute the signature/digest for the cookie
2284f8829a4aSRandall Stewart 	 */
2285e0127ea4SMichael Tuexen 	if (l_stcb != NULL) {
2286e0127ea4SMichael Tuexen 		atomic_add_int(&l_stcb->asoc.refcnt, 1);
2287f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(l_stcb);
2288f8829a4aSRandall Stewart 	}
2289e0127ea4SMichael Tuexen 	l_inp = *inp_p;
2290f8829a4aSRandall Stewart 	SCTP_INP_RLOCK(l_inp);
2291e0127ea4SMichael Tuexen 	if (l_stcb != NULL) {
2292f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(l_stcb);
2293e0127ea4SMichael Tuexen 		atomic_subtract_int(&l_stcb->asoc.refcnt, 1);
2294f8829a4aSRandall Stewart 	}
2295e0127ea4SMichael Tuexen 	if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
2296e0127ea4SMichael Tuexen 		SCTP_INP_RUNLOCK(l_inp);
2297e0127ea4SMichael Tuexen 		sctp_m_freem(m_sig);
2298e0127ea4SMichael Tuexen 		return (NULL);
2299e0127ea4SMichael Tuexen 	}
2300e0127ea4SMichael Tuexen 	ep = &(*inp_p)->sctp_ep;
2301f8829a4aSRandall Stewart 	/* which cookie is it? */
2302f8829a4aSRandall Stewart 	if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2303f8829a4aSRandall Stewart 	    (ep->current_secret_number != ep->last_secret_number)) {
2304f8829a4aSRandall Stewart 		/* it's the old cookie */
23056e55db54SRandall Stewart 		(void)sctp_hmac_m(SCTP_HMAC,
2306f8829a4aSRandall Stewart 		    (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2307d00aff5dSRandall Stewart 		    SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2308f8829a4aSRandall Stewart 	} else {
2309f8829a4aSRandall Stewart 		/* it's the current cookie */
23106e55db54SRandall Stewart 		(void)sctp_hmac_m(SCTP_HMAC,
2311f8829a4aSRandall Stewart 		    (uint8_t *)ep->secret_key[(int)ep->current_secret_number],
2312d00aff5dSRandall Stewart 		    SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2313f8829a4aSRandall Stewart 	}
2314f8829a4aSRandall Stewart 	/* get the signature */
2315f8829a4aSRandall Stewart 	SCTP_INP_RUNLOCK(l_inp);
2316f8829a4aSRandall Stewart 	sig = (uint8_t *)sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *)&tmp_sig);
2317f8829a4aSRandall Stewart 	if (sig == NULL) {
2318f8829a4aSRandall Stewart 		/* couldn't find signature */
231903b0b021SRandall Stewart 		sctp_m_freem(m_sig);
2320f8829a4aSRandall Stewart 		return (NULL);
2321f8829a4aSRandall Stewart 	}
2322f8829a4aSRandall Stewart 	/* compare the received digest with the computed digest */
232315a087e5SMichael Tuexen 	if (timingsafe_bcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2324f8829a4aSRandall Stewart 		/* try the old cookie? */
2325f8829a4aSRandall Stewart 		if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2326f8829a4aSRandall Stewart 		    (ep->current_secret_number != ep->last_secret_number)) {
2327f8829a4aSRandall Stewart 			/* compute digest with old */
23286e55db54SRandall Stewart 			(void)sctp_hmac_m(SCTP_HMAC,
2329f8829a4aSRandall Stewart 			    (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2330d00aff5dSRandall Stewart 			    SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2331f8829a4aSRandall Stewart 			/* compare */
233215a087e5SMichael Tuexen 			if (timingsafe_bcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2333f8829a4aSRandall Stewart 				cookie_ok = 1;
2334f8829a4aSRandall Stewart 		}
2335f8829a4aSRandall Stewart 	} else {
2336f8829a4aSRandall Stewart 		cookie_ok = 1;
2337f8829a4aSRandall Stewart 	}
2338f8829a4aSRandall Stewart 
2339f8829a4aSRandall Stewart 	/*
2340f8829a4aSRandall Stewart 	 * Now before we continue we must reconstruct our mbuf so that
2341f8829a4aSRandall Stewart 	 * normal processing of any other chunks will work.
2342f8829a4aSRandall Stewart 	 */
2343f8829a4aSRandall Stewart 	{
2344f8829a4aSRandall Stewart 		struct mbuf *m_at;
2345f8829a4aSRandall Stewart 
2346f8829a4aSRandall Stewart 		m_at = m;
2347139bc87fSRandall Stewart 		while (SCTP_BUF_NEXT(m_at) != NULL) {
2348139bc87fSRandall Stewart 			m_at = SCTP_BUF_NEXT(m_at);
2349f8829a4aSRandall Stewart 		}
2350139bc87fSRandall Stewart 		SCTP_BUF_NEXT(m_at) = m_sig;
2351f8829a4aSRandall Stewart 	}
2352f8829a4aSRandall Stewart 
2353f8829a4aSRandall Stewart 	if (cookie_ok == 0) {
2354ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2355ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
2356ad81507eSRandall Stewart 		    "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2357f8829a4aSRandall Stewart 		    (uint32_t)offset, cookie_offset, sig_offset);
2358f8829a4aSRandall Stewart 		return (NULL);
2359f8829a4aSRandall Stewart 	}
23600053ed28SMichael Tuexen 
2361a92d5016SMichael Tuexen 	if (sctp_ticks_to_msecs(cookie->cookie_life) > SCTP_MAX_COOKIE_LIFE) {
2362a92d5016SMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: Invalid cookie lifetime\n");
2363a92d5016SMichael Tuexen 		return (NULL);
2364a92d5016SMichael Tuexen 	}
2365a92d5016SMichael Tuexen 	time_entered.tv_sec = cookie->time_entered.tv_sec;
2366a92d5016SMichael Tuexen 	time_entered.tv_usec = cookie->time_entered.tv_usec;
2367a92d5016SMichael Tuexen 	if ((time_entered.tv_sec < 0) ||
2368a92d5016SMichael Tuexen 	    (time_entered.tv_usec < 0) ||
2369a92d5016SMichael Tuexen 	    (time_entered.tv_usec >= 1000000)) {
2370a92d5016SMichael Tuexen 		/* Invalid time stamp. Cookie must have been modified. */
2371a92d5016SMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: Invalid time stamp\n");
2372a92d5016SMichael Tuexen 		return (NULL);
2373a92d5016SMichael Tuexen 	}
23746e55db54SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&now);
2375a92d5016SMichael Tuexen 	if (timevalcmp(&now, &time_entered, <)) {
2376a92d5016SMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie generated in the future!\n");
2377a92d5016SMichael Tuexen 		return (NULL);
2378a92d5016SMichael Tuexen 	}
2379a92d5016SMichael Tuexen 	/*
2380a92d5016SMichael Tuexen 	 * Check the cookie timestamps to be sure it's not stale.
2381a92d5016SMichael Tuexen 	 * cookie_life is in ticks, so we convert to seconds.
2382a92d5016SMichael Tuexen 	 */
2383a92d5016SMichael Tuexen 	time_expires.tv_sec = time_entered.tv_sec + sctp_ticks_to_secs(cookie->cookie_life);
2384a92d5016SMichael Tuexen 	time_expires.tv_usec = time_entered.tv_usec;
2385f8829a4aSRandall Stewart 	if (timevalcmp(&now, &time_expires, >)) {
2386f8829a4aSRandall Stewart 		/* cookie is stale! */
2387f8829a4aSRandall Stewart 		struct mbuf *op_err;
238886eda749SMichael Tuexen 		struct sctp_error_stale_cookie *cause;
2389564a95f4SMichael Tuexen 		struct timeval diff;
2390564a95f4SMichael Tuexen 		uint32_t staleness;
2391f8829a4aSRandall Stewart 
239286eda749SMichael Tuexen 		op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_stale_cookie),
2393eb1b1807SGleb Smirnoff 		    0, M_NOWAIT, 1, MT_DATA);
2394f8829a4aSRandall Stewart 		if (op_err == NULL) {
2395f8829a4aSRandall Stewart 			/* FOOBAR */
2396f8829a4aSRandall Stewart 			return (NULL);
2397f8829a4aSRandall Stewart 		}
2398f8829a4aSRandall Stewart 		/* Set the len */
239986eda749SMichael Tuexen 		SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_stale_cookie);
240086eda749SMichael Tuexen 		cause = mtod(op_err, struct sctp_error_stale_cookie *);
240186eda749SMichael Tuexen 		cause->cause.code = htons(SCTP_CAUSE_STALE_COOKIE);
2402a92d5016SMichael Tuexen 		cause->cause.length = htons(sizeof(struct sctp_error_stale_cookie));
2403564a95f4SMichael Tuexen 		diff = now;
2404564a95f4SMichael Tuexen 		timevalsub(&diff, &time_expires);
2405c3115febSMichael Tuexen 		if ((uint32_t)diff.tv_sec > UINT32_MAX / 1000000) {
2406564a95f4SMichael Tuexen 			staleness = UINT32_MAX;
2407564a95f4SMichael Tuexen 		} else {
2408aab1d593SMichael Tuexen 			staleness = (uint32_t)diff.tv_sec * 1000000;
2409564a95f4SMichael Tuexen 		}
24103ec509bcSMichael Tuexen 		if (UINT32_MAX - staleness >= (uint32_t)diff.tv_usec) {
2411aab1d593SMichael Tuexen 			staleness += (uint32_t)diff.tv_usec;
2412564a95f4SMichael Tuexen 		} else {
2413564a95f4SMichael Tuexen 			staleness = UINT32_MAX;
2414564a95f4SMichael Tuexen 		}
2415564a95f4SMichael Tuexen 		cause->stale_time = htonl(staleness);
2416b1754ad1SMichael Tuexen 		sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
2417d089f9b9SMichael Tuexen 		    mflowtype, mflowid, l_inp->fibnum,
2418c54a18d2SRandall Stewart 		    vrf_id, port);
2419f8829a4aSRandall Stewart 		return (NULL);
2420f8829a4aSRandall Stewart 	}
2421f8829a4aSRandall Stewart 	/*
2422f8829a4aSRandall Stewart 	 * Now we must see with the lookup address if we have an existing
2423f8829a4aSRandall Stewart 	 * asoc. This will only happen if we were in the COOKIE-WAIT state
2424f8829a4aSRandall Stewart 	 * and a INIT collided with us and somewhere the peer sent the
2425f8829a4aSRandall Stewart 	 * cookie on another address besides the single address our assoc
2426f8829a4aSRandall Stewart 	 * had for him. In this case we will have one of the tie-tags set at
2427f8829a4aSRandall Stewart 	 * least AND the address field in the cookie can be used to look it
2428f8829a4aSRandall Stewart 	 * up.
2429f8829a4aSRandall Stewart 	 */
2430f8829a4aSRandall Stewart 	to = NULL;
2431e6194c2eSMichael Tuexen 	switch (cookie->addr_type) {
2432e6194c2eSMichael Tuexen #ifdef INET6
2433e6194c2eSMichael Tuexen 	case SCTP_IPV6_ADDRESS:
2434f8829a4aSRandall Stewart 		memset(&sin6, 0, sizeof(sin6));
2435f8829a4aSRandall Stewart 		sin6.sin6_family = AF_INET6;
2436f8829a4aSRandall Stewart 		sin6.sin6_len = sizeof(sin6);
2437f8829a4aSRandall Stewart 		sin6.sin6_port = sh->src_port;
2438f8829a4aSRandall Stewart 		sin6.sin6_scope_id = cookie->scope_id;
2439f8829a4aSRandall Stewart 		memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2440f8829a4aSRandall Stewart 		    sizeof(sin6.sin6_addr.s6_addr));
2441f8829a4aSRandall Stewart 		to = (struct sockaddr *)&sin6;
2442e6194c2eSMichael Tuexen 		break;
2443e6194c2eSMichael Tuexen #endif
2444e6194c2eSMichael Tuexen #ifdef INET
2445e6194c2eSMichael Tuexen 	case SCTP_IPV4_ADDRESS:
2446f8829a4aSRandall Stewart 		memset(&sin, 0, sizeof(sin));
2447f8829a4aSRandall Stewart 		sin.sin_family = AF_INET;
2448f8829a4aSRandall Stewart 		sin.sin_len = sizeof(sin);
2449f8829a4aSRandall Stewart 		sin.sin_port = sh->src_port;
2450f8829a4aSRandall Stewart 		sin.sin_addr.s_addr = cookie->address[0];
2451f8829a4aSRandall Stewart 		to = (struct sockaddr *)&sin;
2452e6194c2eSMichael Tuexen 		break;
2453e6194c2eSMichael Tuexen #endif
2454e6194c2eSMichael Tuexen 	default:
2455ad81507eSRandall Stewart 		/* This should not happen */
2456ad81507eSRandall Stewart 		return (NULL);
2457f8829a4aSRandall Stewart 	}
2458f0dc2113SMichael Tuexen 	if (*stcb == NULL) {
2459f8829a4aSRandall Stewart 		/* Yep, lets check */
2460b1754ad1SMichael Tuexen 		*stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
2461f8829a4aSRandall Stewart 		if (*stcb == NULL) {
2462f8829a4aSRandall Stewart 			/*
2463f8829a4aSRandall Stewart 			 * We should have only got back the same inp. If we
2464f8829a4aSRandall Stewart 			 * got back a different ep we have a problem. The
2465f8829a4aSRandall Stewart 			 * original findep got back l_inp and now
2466f8829a4aSRandall Stewart 			 */
2467f8829a4aSRandall Stewart 			if (l_inp != *inp_p) {
2468ad81507eSRandall Stewart 				SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2469f8829a4aSRandall Stewart 			}
2470a5d547adSRandall Stewart 		} else {
2471a5d547adSRandall Stewart 			if (*locked_tcb == NULL) {
2472a5d547adSRandall Stewart 				/*
2473a5d547adSRandall Stewart 				 * In this case we found the assoc only
2474a5d547adSRandall Stewart 				 * after we locked the create lock. This
2475a5d547adSRandall Stewart 				 * means we are in a colliding case and we
2476a5d547adSRandall Stewart 				 * must make sure that we unlock the tcb if
2477a5d547adSRandall Stewart 				 * its one of the cases where we throw away
2478a5d547adSRandall Stewart 				 * the incoming packets.
2479a5d547adSRandall Stewart 				 */
2480a5d547adSRandall Stewart 				*locked_tcb = *stcb;
2481a5d547adSRandall Stewart 
2482a5d547adSRandall Stewart 				/*
2483a5d547adSRandall Stewart 				 * We must also increment the inp ref count
2484a5d547adSRandall Stewart 				 * since the ref_count flags was set when we
2485a5d547adSRandall Stewart 				 * did not find the TCB, now we found it
2486a5d547adSRandall Stewart 				 * which reduces the refcount.. we must
2487a5d547adSRandall Stewart 				 * raise it back out to balance it all :-)
2488a5d547adSRandall Stewart 				 */
2489a5d547adSRandall Stewart 				SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2490a5d547adSRandall Stewart 				if ((*stcb)->sctp_ep != l_inp) {
2491ad81507eSRandall Stewart 					SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2492dd294dceSMichael Tuexen 					    (void *)(*stcb)->sctp_ep, (void *)l_inp);
2493a5d547adSRandall Stewart 				}
2494a5d547adSRandall Stewart 			}
2495f8829a4aSRandall Stewart 		}
2496f8829a4aSRandall Stewart 	}
24970053ed28SMichael Tuexen 
2498f8829a4aSRandall Stewart 	cookie_len -= SCTP_SIGNATURE_SIZE;
2499f8829a4aSRandall Stewart 	if (*stcb == NULL) {
2500f8829a4aSRandall Stewart 		/* this is the "normal" case... get a new TCB */
2501b1754ad1SMichael Tuexen 		*stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
2502b1754ad1SMichael Tuexen 		    cookie, cookie_len, *inp_p,
2503b1754ad1SMichael Tuexen 		    netp, to, &notification,
2504f30ac432SMichael Tuexen 		    auth_skipped, auth_offset, auth_len,
2505457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2506f30ac432SMichael Tuexen 		    vrf_id, port);
2507f8829a4aSRandall Stewart 	} else {
2508f8829a4aSRandall Stewart 		/* this is abnormal... cookie-echo on existing TCB */
2509f8829a4aSRandall Stewart 		had_a_existing_tcb = 1;
2510b1754ad1SMichael Tuexen 		*stcb = sctp_process_cookie_existing(m, iphlen, offset,
2511b1754ad1SMichael Tuexen 		    src, dst, sh,
2512830d754dSRandall Stewart 		    cookie, cookie_len, *inp_p, *stcb, netp, to,
2513f30ac432SMichael Tuexen 		    &notification, auth_skipped, auth_offset, auth_len,
2514457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2515f30ac432SMichael Tuexen 		    vrf_id, port);
25165f2e1835SMichael Tuexen 		if (*stcb == NULL) {
25175f2e1835SMichael Tuexen 			*locked_tcb = NULL;
25185f2e1835SMichael Tuexen 		}
2519f8829a4aSRandall Stewart 	}
2520f8829a4aSRandall Stewart 
2521f8829a4aSRandall Stewart 	if (*stcb == NULL) {
2522f8829a4aSRandall Stewart 		/* still no TCB... must be bad cookie-echo */
2523f8829a4aSRandall Stewart 		return (NULL);
2524f8829a4aSRandall Stewart 	}
25255fe29cdfSMichael Tuexen 	if (*netp != NULL) {
2526457b4b88SMichael Tuexen 		(*netp)->flowtype = mflowtype;
25275fe29cdfSMichael Tuexen 		(*netp)->flowid = mflowid;
2528a4ae38f1SMichael Tuexen 	}
2529f8829a4aSRandall Stewart 	/*
2530f8829a4aSRandall Stewart 	 * Ok, we built an association so confirm the address we sent the
2531f8829a4aSRandall Stewart 	 * INIT-ACK to.
2532f8829a4aSRandall Stewart 	 */
2533f8829a4aSRandall Stewart 	netl = sctp_findnet(*stcb, to);
2534f8829a4aSRandall Stewart 	/*
2535f8829a4aSRandall Stewart 	 * This code should in theory NOT run but
2536f8829a4aSRandall Stewart 	 */
2537f8829a4aSRandall Stewart 	if (netl == NULL) {
2538f8829a4aSRandall Stewart 		/* TSNH! Huh, why do I need to add this address here? */
2539ec70917fSMichael Tuexen 		if (sctp_add_remote_addr(*stcb, to, NULL, port,
25407154bf4aSMichael Tuexen 		    SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
254160990c0cSMichael Tuexen 			return (NULL);
254260990c0cSMichael Tuexen 		}
2543f8829a4aSRandall Stewart 		netl = sctp_findnet(*stcb, to);
2544f8829a4aSRandall Stewart 	}
2545f8829a4aSRandall Stewart 	if (netl) {
2546f8829a4aSRandall Stewart 		if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2547f8829a4aSRandall Stewart 			netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2548ad81507eSRandall Stewart 			(void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2549f8829a4aSRandall Stewart 			    netl);
25502fad0e55SMichael Tuexen 			send_int_conf = 1;
2551f8829a4aSRandall Stewart 		}
2552f8829a4aSRandall Stewart 	}
2553ca85e948SMichael Tuexen 	sctp_start_net_timers(*stcb);
2554f8829a4aSRandall Stewart 	if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2555f8829a4aSRandall Stewart 		if (!had_a_existing_tcb ||
2556f8829a4aSRandall Stewart 		    (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2557f8829a4aSRandall Stewart 			/*
2558f8829a4aSRandall Stewart 			 * If we have a NEW cookie or the connect never
2559f8829a4aSRandall Stewart 			 * reached the connected state during collision we
2560f8829a4aSRandall Stewart 			 * must do the TCP accept thing.
2561f8829a4aSRandall Stewart 			 */
2562f8829a4aSRandall Stewart 			struct socket *so, *oso;
2563f8829a4aSRandall Stewart 			struct sctp_inpcb *inp;
2564f8829a4aSRandall Stewart 
2565f8829a4aSRandall Stewart 			if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2566f8829a4aSRandall Stewart 				/*
2567f8829a4aSRandall Stewart 				 * For a restart we will keep the same
2568f8829a4aSRandall Stewart 				 * socket, no need to do anything. I THINK!!
2569f8829a4aSRandall Stewart 				 */
25707215cc1bSMichael Tuexen 				sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
25712fad0e55SMichael Tuexen 				if (send_int_conf) {
25722fad0e55SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
25732fad0e55SMichael Tuexen 					    (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
25742fad0e55SMichael Tuexen 				}
2575f8829a4aSRandall Stewart 				return (m);
2576f8829a4aSRandall Stewart 			}
2577f8829a4aSRandall Stewart 			oso = (*inp_p)->sctp_socket;
25782dad8a55SRandall Stewart 			atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2579f8829a4aSRandall Stewart 			SCTP_TCB_UNLOCK((*stcb));
25801fb51a12SBjoern A. Zeeb 			CURVNET_SET(oso->so_vnet);
258193164cf9SRandall Stewart 			so = sonewconn(oso, 0
2582f8829a4aSRandall Stewart 			    );
25831fb51a12SBjoern A. Zeeb 			CURVNET_RESTORE();
2584f8829a4aSRandall Stewart 			SCTP_TCB_LOCK((*stcb));
25852dad8a55SRandall Stewart 			atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
25862dad8a55SRandall Stewart 
2587f8829a4aSRandall Stewart 			if (so == NULL) {
2588f8829a4aSRandall Stewart 				struct mbuf *op_err;
258970486b27SMichael Tuexen 
2590f8829a4aSRandall Stewart 				/* Too many sockets */
2591ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2592ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2593f8829a4aSRandall Stewart 				sctp_abort_association(*inp_p, NULL, m, iphlen,
2594b1754ad1SMichael Tuexen 				    src, dst, sh, op_err,
2595457b4b88SMichael Tuexen 				    mflowtype, mflowid,
2596f30ac432SMichael Tuexen 				    vrf_id, port);
2597b7d130beSMichael Tuexen 				(void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC,
2598b7d130beSMichael Tuexen 				    SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
2599f8829a4aSRandall Stewart 				return (NULL);
2600f8829a4aSRandall Stewart 			}
2601f8829a4aSRandall Stewart 			inp = (struct sctp_inpcb *)so->so_pcb;
260293164cf9SRandall Stewart 			SCTP_INP_INCR_REF(inp);
260393164cf9SRandall Stewart 			/*
260493164cf9SRandall Stewart 			 * We add the unbound flag here so that if we get an
260593164cf9SRandall Stewart 			 * soabort() before we get the move_pcb done, we
260693164cf9SRandall Stewart 			 * will properly cleanup.
260793164cf9SRandall Stewart 			 */
2608f8829a4aSRandall Stewart 			inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2609f8829a4aSRandall Stewart 			    SCTP_PCB_FLAGS_CONNECTED |
2610f8829a4aSRandall Stewart 			    SCTP_PCB_FLAGS_IN_TCPPOOL |
261193164cf9SRandall Stewart 			    SCTP_PCB_FLAGS_UNBOUND |
2612f8829a4aSRandall Stewart 			    (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2613f8829a4aSRandall Stewart 			    SCTP_PCB_FLAGS_DONT_WAKE);
2614f8829a4aSRandall Stewart 			inp->sctp_features = (*inp_p)->sctp_features;
2615851b7298SRandall Stewart 			inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2616f8829a4aSRandall Stewart 			inp->sctp_socket = so;
2617f8829a4aSRandall Stewart 			inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
261859b6d5beSMichael Tuexen 			inp->max_cwnd = (*inp_p)->max_cwnd;
261920083c2eSMichael Tuexen 			inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2620f342355aSMichael Tuexen 			inp->ecn_supported = (*inp_p)->ecn_supported;
2621dd973b0eSMichael Tuexen 			inp->prsctp_supported = (*inp_p)->prsctp_supported;
2622c79bec9cSMichael Tuexen 			inp->auth_supported = (*inp_p)->auth_supported;
2623c79bec9cSMichael Tuexen 			inp->asconf_supported = (*inp_p)->asconf_supported;
2624317e00efSMichael Tuexen 			inp->reconfig_supported = (*inp_p)->reconfig_supported;
2625caea9879SMichael Tuexen 			inp->nrsack_supported = (*inp_p)->nrsack_supported;
2626cb9b8e6fSMichael Tuexen 			inp->pktdrop_supported = (*inp_p)->pktdrop_supported;
2627f8829a4aSRandall Stewart 			inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2628f8829a4aSRandall Stewart 			inp->sctp_context = (*inp_p)->sctp_context;
2629c4e848b7SRandall Stewart 			inp->local_strreset_support = (*inp_p)->local_strreset_support;
2630d089f9b9SMichael Tuexen 			inp->fibnum = (*inp_p)->fibnum;
2631f8829a4aSRandall Stewart 			/*
2632f8829a4aSRandall Stewart 			 * copy in the authentication parameters from the
2633f8829a4aSRandall Stewart 			 * original endpoint
2634f8829a4aSRandall Stewart 			 */
2635f8829a4aSRandall Stewart 			if (inp->sctp_ep.local_hmacs)
2636f8829a4aSRandall Stewart 				sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2637f8829a4aSRandall Stewart 			inp->sctp_ep.local_hmacs =
2638f8829a4aSRandall Stewart 			    sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2639f8829a4aSRandall Stewart 			if (inp->sctp_ep.local_auth_chunks)
2640f8829a4aSRandall Stewart 				sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2641f8829a4aSRandall Stewart 			inp->sctp_ep.local_auth_chunks =
2642f8829a4aSRandall Stewart 			    sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2643f8829a4aSRandall Stewart 
2644f8829a4aSRandall Stewart 			/*
2645f8829a4aSRandall Stewart 			 * Now we must move it from one hash table to
2646f8829a4aSRandall Stewart 			 * another and get the tcb in the right place.
2647f8829a4aSRandall Stewart 			 */
264888a7eb29SRandall Stewart 
264988a7eb29SRandall Stewart 			/*
265088a7eb29SRandall Stewart 			 * This is where the one-2-one socket is put into
265188a7eb29SRandall Stewart 			 * the accept state waiting for the accept!
265288a7eb29SRandall Stewart 			 */
265388a7eb29SRandall Stewart 			if (*stcb) {
2654839d21d6SMichael Tuexen 				SCTP_ADD_SUBSTATE(*stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
265588a7eb29SRandall Stewart 			}
2656f8829a4aSRandall Stewart 			sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2657d61a0ae0SRandall Stewart 
2658d61a0ae0SRandall Stewart 			atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2659d61a0ae0SRandall Stewart 			SCTP_TCB_UNLOCK((*stcb));
2660d61a0ae0SRandall Stewart 
2661265de5bbSRobert Watson 			sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2662265de5bbSRobert Watson 			    0);
2663d61a0ae0SRandall Stewart 			SCTP_TCB_LOCK((*stcb));
2664d61a0ae0SRandall Stewart 			atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2665d61a0ae0SRandall Stewart 
266693164cf9SRandall Stewart 			/*
266793164cf9SRandall Stewart 			 * now we must check to see if we were aborted while
266893164cf9SRandall Stewart 			 * the move was going on and the lock/unlock
266993164cf9SRandall Stewart 			 * happened.
267093164cf9SRandall Stewart 			 */
267193164cf9SRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
267293164cf9SRandall Stewart 				/*
267393164cf9SRandall Stewart 				 * yep it was, we leave the assoc attached
267493164cf9SRandall Stewart 				 * to the socket since the sctp_inpcb_free()
267593164cf9SRandall Stewart 				 * call will send an abort for us.
267693164cf9SRandall Stewart 				 */
267793164cf9SRandall Stewart 				SCTP_INP_DECR_REF(inp);
267893164cf9SRandall Stewart 				return (NULL);
267993164cf9SRandall Stewart 			}
268093164cf9SRandall Stewart 			SCTP_INP_DECR_REF(inp);
2681f8829a4aSRandall Stewart 			/* Switch over to the new guy */
2682f8829a4aSRandall Stewart 			*inp_p = inp;
2683ceaad40aSRandall Stewart 			sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
26842fad0e55SMichael Tuexen 			if (send_int_conf) {
26852fad0e55SMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
26862fad0e55SMichael Tuexen 				    (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
26872fad0e55SMichael Tuexen 			}
26880053ed28SMichael Tuexen 
2689b7b84c0eSMichael Tuexen 			/*
2690b7b84c0eSMichael Tuexen 			 * Pull it from the incomplete queue and wake the
2691b7b84c0eSMichael Tuexen 			 * guy
2692b7b84c0eSMichael Tuexen 			 */
269393164cf9SRandall Stewart 			soisconnected(so);
2694f8829a4aSRandall Stewart 			return (m);
2695f8829a4aSRandall Stewart 		}
2696f8829a4aSRandall Stewart 	}
26972fad0e55SMichael Tuexen 	if (notification) {
2698ceaad40aSRandall Stewart 		sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2699f8829a4aSRandall Stewart 	}
27002fad0e55SMichael Tuexen 	if (send_int_conf) {
27012fad0e55SMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
27022fad0e55SMichael Tuexen 		    (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
27032fad0e55SMichael Tuexen 	}
2704f8829a4aSRandall Stewart 	return (m);
2705f8829a4aSRandall Stewart }
2706f8829a4aSRandall Stewart 
2707f8829a4aSRandall Stewart static void
27087215cc1bSMichael Tuexen sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2709f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net)
2710f8829a4aSRandall Stewart {
2711f8829a4aSRandall Stewart 	/* cp must not be used, others call this without a c-ack :-) */
2712f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2713efd5e692SMichael Tuexen 	struct sctp_tmit_chunk *chk;
2714f8829a4aSRandall Stewart 
2715ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
2716ad81507eSRandall Stewart 	    "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2717ad234e3cSMichael Tuexen 	if ((stcb == NULL) || (net == NULL)) {
2718f8829a4aSRandall Stewart 		return;
2719ad234e3cSMichael Tuexen 	}
27200053ed28SMichael Tuexen 
2721f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2722469a65d1SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
2723469a65d1SMichael Tuexen 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2724469a65d1SMichael Tuexen 		    asoc->overall_error_count,
2725469a65d1SMichael Tuexen 		    0,
2726469a65d1SMichael Tuexen 		    SCTP_FROM_SCTP_INPUT,
2727469a65d1SMichael Tuexen 		    __LINE__);
2728469a65d1SMichael Tuexen 	}
2729f8829a4aSRandall Stewart 	sctp_stop_all_cookie_timers(stcb);
2730d9ae4adfSMichael Tuexen 	sctp_toss_old_cookies(stcb, asoc);
2731f8829a4aSRandall Stewart 	/* process according to association state */
2732839d21d6SMichael Tuexen 	if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) {
2733f8829a4aSRandall Stewart 		/* state change only needed when I am in right state */
2734ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2735839d21d6SMichael Tuexen 		SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
2736ca85e948SMichael Tuexen 		sctp_start_net_timers(stcb);
2737f8829a4aSRandall Stewart 		/* update RTO */
2738f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2739f8829a4aSRandall Stewart 		SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2740f8829a4aSRandall Stewart 		if (asoc->overall_error_count == 0) {
274144f2a327SMichael Tuexen 			sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered,
2742f79aab18SRandall Stewart 			    SCTP_RTT_FROM_NON_DATA);
2743f8829a4aSRandall Stewart 		}
27448ed1e2c8SMichael Tuexen 		/*
27458ed1e2c8SMichael Tuexen 		 * Since we did not send a HB make sure we don't double
27468ed1e2c8SMichael Tuexen 		 * things.
27478ed1e2c8SMichael Tuexen 		 */
27488ed1e2c8SMichael Tuexen 		asoc->overall_error_count = 0;
27498ed1e2c8SMichael Tuexen 		net->hb_responded = 1;
27506e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2751ceaad40aSRandall Stewart 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2752f8829a4aSRandall Stewart 		if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2753f8829a4aSRandall Stewart 		    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2754a5c2009dSMichael Tuexen 			sctp_pcb_add_flags(stcb->sctp_ep, SCTP_PCB_FLAGS_CONNECTED);
2755d69e7322SRandall Stewart 			if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
2756ceaad40aSRandall Stewart 				soisconnected(stcb->sctp_socket);
2757d69e7322SRandall Stewart 			}
2758f8829a4aSRandall Stewart 		}
2759f8829a4aSRandall Stewart 
2760d9ae4adfSMichael Tuexen 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
2761d9ae4adfSMichael Tuexen 		    TAILQ_EMPTY(&asoc->send_queue) &&
2762d9ae4adfSMichael Tuexen 		    TAILQ_EMPTY(&asoc->sent_queue) &&
2763d9ae4adfSMichael Tuexen 		    (asoc->stream_queue_cnt == 0)) {
2764d9ae4adfSMichael Tuexen 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2765d9ae4adfSMichael Tuexen 			SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
2766d9ae4adfSMichael Tuexen 			sctp_stop_timers_for_shutdown(stcb);
2767d9ae4adfSMichael Tuexen 			sctp_send_shutdown(stcb, net);
2768d9ae4adfSMichael Tuexen 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
2769d9ae4adfSMichael Tuexen 			    stcb->sctp_ep, stcb, net);
2770d9ae4adfSMichael Tuexen 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2771d9ae4adfSMichael Tuexen 			    stcb->sctp_ep, stcb, NULL);
2772d9ae4adfSMichael Tuexen 			sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
2773d9ae4adfSMichael Tuexen 		}
2774d9ae4adfSMichael Tuexen 
2775d69e7322SRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2776d69e7322SRandall Stewart 			/*
2777d69e7322SRandall Stewart 			 * We don't need to do the asconf thing, nor hb or
2778d69e7322SRandall Stewart 			 * autoclose if the socket is closed.
2779d69e7322SRandall Stewart 			 */
2780d69e7322SRandall Stewart 			goto closed_socket;
2781d69e7322SRandall Stewart 		}
27820053ed28SMichael Tuexen 
2783d69e7322SRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2784d69e7322SRandall Stewart 		    stcb, net);
2785d69e7322SRandall Stewart 
2786f8829a4aSRandall Stewart 		if (stcb->asoc.sctp_autoclose_ticks &&
2787f8829a4aSRandall Stewart 		    sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2788f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2789f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, NULL);
2790f8829a4aSRandall Stewart 		}
2791f8829a4aSRandall Stewart 		/*
27921b649582SRandall Stewart 		 * send ASCONF if parameters are pending and ASCONFs are
27931b649582SRandall Stewart 		 * allowed (eg. addresses changed when init/cookie echo were
27941b649582SRandall Stewart 		 * in flight)
2795f8829a4aSRandall Stewart 		 */
2796f8829a4aSRandall Stewart 		if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2797c79bec9cSMichael Tuexen 		    (stcb->asoc.asconf_supported == 1) &&
2798f8829a4aSRandall Stewart 		    (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
27991b649582SRandall Stewart #ifdef SCTP_TIMER_BASED_ASCONF
2800f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2801f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb,
2802f8829a4aSRandall Stewart 			    stcb->asoc.primary_destination);
28031b649582SRandall Stewart #else
28043232788eSRandall Stewart 			sctp_send_asconf(stcb, stcb->asoc.primary_destination,
28053232788eSRandall Stewart 			    SCTP_ADDR_NOT_LOCKED);
28061b649582SRandall Stewart #endif
2807f8829a4aSRandall Stewart 		}
2808f8829a4aSRandall Stewart 	}
2809d69e7322SRandall Stewart closed_socket:
2810f8829a4aSRandall Stewart 	/* Restart the timer if we have pending data */
2811efd5e692SMichael Tuexen 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
2812efd5e692SMichael Tuexen 		if (chk->whoTo != NULL) {
2813efd5e692SMichael Tuexen 			break;
2814efd5e692SMichael Tuexen 		}
2815efd5e692SMichael Tuexen 	}
2816efd5e692SMichael Tuexen 	if (chk != NULL) {
28174a9ef3f8SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
2818f8829a4aSRandall Stewart 	}
2819f8829a4aSRandall Stewart }
2820f8829a4aSRandall Stewart 
2821f8829a4aSRandall Stewart static void
2822f8829a4aSRandall Stewart sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
2823f8829a4aSRandall Stewart     struct sctp_tcb *stcb)
2824f8829a4aSRandall Stewart {
2825f8829a4aSRandall Stewart 	struct sctp_nets *net;
2826f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *lchk;
2827a21779f0SRandall Stewart 	struct sctp_ecne_chunk bkup;
282860990c0cSMichael Tuexen 	uint8_t override_bit;
2829a21779f0SRandall Stewart 	uint32_t tsn, window_data_tsn;
2830a21779f0SRandall Stewart 	int len;
2831dec0177dSRandall Stewart 	unsigned int pkt_cnt;
2832f8829a4aSRandall Stewart 
2833a21779f0SRandall Stewart 	len = ntohs(cp->ch.chunk_length);
2834a21779f0SRandall Stewart 	if (len == sizeof(struct old_sctp_ecne_chunk)) {
2835a21779f0SRandall Stewart 		/* Its the old format */
2836a21779f0SRandall Stewart 		memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
2837a21779f0SRandall Stewart 		bkup.num_pkts_since_cwr = htonl(1);
2838a21779f0SRandall Stewart 		cp = &bkup;
2839a21779f0SRandall Stewart 	}
2840f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvecne);
2841f8829a4aSRandall Stewart 	tsn = ntohl(cp->tsn);
2842a21779f0SRandall Stewart 	pkt_cnt = ntohl(cp->num_pkts_since_cwr);
2843a21779f0SRandall Stewart 	lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
2844f8829a4aSRandall Stewart 	if (lchk == NULL) {
2845493d8e5aSRandall Stewart 		window_data_tsn = stcb->asoc.sending_seq - 1;
2846f8829a4aSRandall Stewart 	} else {
284749656eefSMichael Tuexen 		window_data_tsn = lchk->rec.data.tsn;
2848f8829a4aSRandall Stewart 	}
2849f8829a4aSRandall Stewart 
2850a21779f0SRandall Stewart 	/* Find where it was sent to if possible. */
2851f8829a4aSRandall Stewart 	net = NULL;
28524a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
285349656eefSMichael Tuexen 		if (lchk->rec.data.tsn == tsn) {
2854f8829a4aSRandall Stewart 			net = lchk->whoTo;
2855899288aeSRandall Stewart 			net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
2856f8829a4aSRandall Stewart 			break;
2857f8829a4aSRandall Stewart 		}
285849656eefSMichael Tuexen 		if (SCTP_TSN_GT(lchk->rec.data.tsn, tsn)) {
2859f8829a4aSRandall Stewart 			break;
2860f8829a4aSRandall Stewart 		}
286120b07a4dSMichael Tuexen 	}
2862a21779f0SRandall Stewart 	if (net == NULL) {
2863a21779f0SRandall Stewart 		/*
2864a21779f0SRandall Stewart 		 * What to do. A previous send of a CWR was possibly lost.
2865a21779f0SRandall Stewart 		 * See how old it is, we may have it marked on the actual
2866a21779f0SRandall Stewart 		 * net.
2867a21779f0SRandall Stewart 		 */
2868a21779f0SRandall Stewart 		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2869a21779f0SRandall Stewart 			if (tsn == net->last_cwr_tsn) {
2870a21779f0SRandall Stewart 				/* Found him, send it off */
287160990c0cSMichael Tuexen 				break;
2872a21779f0SRandall Stewart 			}
2873a21779f0SRandall Stewart 		}
287460990c0cSMichael Tuexen 		if (net == NULL) {
2875a21779f0SRandall Stewart 			/*
287660990c0cSMichael Tuexen 			 * If we reach here, we need to send a special CWR
287760990c0cSMichael Tuexen 			 * that says hey, we did this a long time ago and
287860990c0cSMichael Tuexen 			 * you lost the response.
2879a21779f0SRandall Stewart 			 */
2880a21779f0SRandall Stewart 			net = TAILQ_FIRST(&stcb->asoc.nets);
288160990c0cSMichael Tuexen 			if (net == NULL) {
288260990c0cSMichael Tuexen 				/* TSNH */
288360990c0cSMichael Tuexen 				return;
2884a21779f0SRandall Stewart 			}
288560990c0cSMichael Tuexen 			override_bit = SCTP_CWR_REDUCE_OVERRIDE;
288660990c0cSMichael Tuexen 		} else {
288760990c0cSMichael Tuexen 			override_bit = 0;
288860990c0cSMichael Tuexen 		}
288960990c0cSMichael Tuexen 	} else {
289060990c0cSMichael Tuexen 		override_bit = 0;
289160990c0cSMichael Tuexen 	}
2892493d8e5aSRandall Stewart 	if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
2893493d8e5aSRandall Stewart 	    ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
2894b7b84c0eSMichael Tuexen 		/*
2895b7b84c0eSMichael Tuexen 		 * JRS - Use the congestion control given in the pluggable
2896b7b84c0eSMichael Tuexen 		 * CC module
2897b7b84c0eSMichael Tuexen 		 */
2898493d8e5aSRandall Stewart 		stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
2899f8829a4aSRandall Stewart 		/*
2900a21779f0SRandall Stewart 		 * We reduce once every RTT. So we will only lower cwnd at
2901a21779f0SRandall Stewart 		 * the next sending seq i.e. the window_data_tsn
2902f8829a4aSRandall Stewart 		 */
2903a21779f0SRandall Stewart 		net->cwr_window_tsn = window_data_tsn;
2904a21779f0SRandall Stewart 		net->ecn_ce_pkt_cnt += pkt_cnt;
2905a21779f0SRandall Stewart 		net->lost_cnt = pkt_cnt;
2906a21779f0SRandall Stewart 		net->last_cwr_tsn = tsn;
2907a21779f0SRandall Stewart 	} else {
2908a21779f0SRandall Stewart 		override_bit |= SCTP_CWR_IN_SAME_WINDOW;
2909493d8e5aSRandall Stewart 		if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
2910493d8e5aSRandall Stewart 		    ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
2911a21779f0SRandall Stewart 			/*
2912493d8e5aSRandall Stewart 			 * Another loss in the same window update how many
2913493d8e5aSRandall Stewart 			 * marks/packets lost we have had.
2914a21779f0SRandall Stewart 			 */
2915493d8e5aSRandall Stewart 			int cnt = 1;
2916a21779f0SRandall Stewart 
2917a21779f0SRandall Stewart 			if (pkt_cnt > net->lost_cnt) {
2918a21779f0SRandall Stewart 				/* Should be the case */
2919493d8e5aSRandall Stewart 				cnt = (pkt_cnt - net->lost_cnt);
2920493d8e5aSRandall Stewart 				net->ecn_ce_pkt_cnt += cnt;
2921a21779f0SRandall Stewart 			}
2922493d8e5aSRandall Stewart 			net->lost_cnt = pkt_cnt;
2923a21779f0SRandall Stewart 			net->last_cwr_tsn = tsn;
2924493d8e5aSRandall Stewart 			/*
2925493d8e5aSRandall Stewart 			 * Most CC functions will ignore this call, since we
2926493d8e5aSRandall Stewart 			 * are in-window yet of the initial CE the peer saw.
2927493d8e5aSRandall Stewart 			 */
2928493d8e5aSRandall Stewart 			stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
2929a21779f0SRandall Stewart 		}
2930f8829a4aSRandall Stewart 	}
2931f8829a4aSRandall Stewart 	/*
2932f8829a4aSRandall Stewart 	 * We always send a CWR this way if our previous one was lost our
2933f8829a4aSRandall Stewart 	 * peer will get an update, or if it is not time again to reduce we
2934a21779f0SRandall Stewart 	 * still get the cwr to the peer. Note we set the override when we
2935a21779f0SRandall Stewart 	 * could not find the TSN on the chunk or the destination network.
2936f8829a4aSRandall Stewart 	 */
2937a21779f0SRandall Stewart 	sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
2938f8829a4aSRandall Stewart }
2939f8829a4aSRandall Stewart 
2940f8829a4aSRandall Stewart static void
2941a21779f0SRandall Stewart sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
2942f8829a4aSRandall Stewart {
2943f8829a4aSRandall Stewart 	/*
2944f8829a4aSRandall Stewart 	 * Here we get a CWR from the peer. We must look in the outqueue and
2945582212faSEitan Adler 	 * make sure that we have a covered ECNE in the control chunk part.
2946f8829a4aSRandall Stewart 	 * If so remove it.
2947f8829a4aSRandall Stewart 	 */
29486c2cfc04SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
2949f8829a4aSRandall Stewart 	struct sctp_ecne_chunk *ecne;
2950a21779f0SRandall Stewart 	int override;
2951a21779f0SRandall Stewart 	uint32_t cwr_tsn;
2952f8829a4aSRandall Stewart 
2953a21779f0SRandall Stewart 	cwr_tsn = ntohl(cp->tsn);
2954a21779f0SRandall Stewart 	override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
29556c2cfc04SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &stcb->asoc.control_send_queue, sctp_next, nchk) {
2956f8829a4aSRandall Stewart 		if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
2957f8829a4aSRandall Stewart 			continue;
2958f8829a4aSRandall Stewart 		}
2959a21779f0SRandall Stewart 		if ((override == 0) && (chk->whoTo != net)) {
2960a21779f0SRandall Stewart 			/* Must be from the right src unless override is set */
2961a21779f0SRandall Stewart 			continue;
2962a21779f0SRandall Stewart 		}
2963f8829a4aSRandall Stewart 		ecne = mtod(chk->data, struct sctp_ecne_chunk *);
2964a21779f0SRandall Stewart 		if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
2965f8829a4aSRandall Stewart 			/* this covers this ECNE, we can remove it */
2966f8829a4aSRandall Stewart 			stcb->asoc.ecn_echo_cnt_onq--;
2967f8829a4aSRandall Stewart 			TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
2968f8829a4aSRandall Stewart 			    sctp_next);
2969cd6340caSMichael Tuexen 			stcb->asoc.ctrl_queue_cnt--;
2970f8829a4aSRandall Stewart 			sctp_m_freem(chk->data);
2971f8829a4aSRandall Stewart 			chk->data = NULL;
2972689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
2973a21779f0SRandall Stewart 			if (override == 0) {
2974f8829a4aSRandall Stewart 				break;
2975f8829a4aSRandall Stewart 			}
2976f8829a4aSRandall Stewart 		}
2977f8829a4aSRandall Stewart 	}
2978a21779f0SRandall Stewart }
2979f8829a4aSRandall Stewart 
2980f8829a4aSRandall Stewart static void
29817215cc1bSMichael Tuexen sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
2982f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net)
2983f8829a4aSRandall Stewart {
2984ceaad40aSRandall Stewart 
2985ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
2986ad81507eSRandall Stewart 	    "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
2987f8829a4aSRandall Stewart 	if (stcb == NULL)
2988f8829a4aSRandall Stewart 		return;
2989f8829a4aSRandall Stewart 
2990f8829a4aSRandall Stewart 	/* process according to association state */
2991839d21d6SMichael Tuexen 	if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
2992f8829a4aSRandall Stewart 		/* unexpected SHUTDOWN-COMPLETE... so ignore... */
29932afb3e84SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
29942afb3e84SRandall Stewart 		    "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
2995f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
2996f8829a4aSRandall Stewart 		return;
2997f8829a4aSRandall Stewart 	}
2998f8829a4aSRandall Stewart 	/* notify upper layer protocol */
2999f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
3000ceaad40aSRandall Stewart 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
300156f778aaSMichael Tuexen 	}
300256f778aaSMichael Tuexen #ifdef INVARIANTS
30030f1346f7SMichael Tuexen 	if (!TAILQ_EMPTY(&stcb->asoc.send_queue) ||
30040f1346f7SMichael Tuexen 	    !TAILQ_EMPTY(&stcb->asoc.sent_queue) ||
3005124d851aSMichael Tuexen 	    sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
300656f778aaSMichael Tuexen 		panic("Queues are not empty when handling SHUTDOWN-COMPLETE");
3007f8829a4aSRandall Stewart 	}
300856f778aaSMichael Tuexen #endif
3009f8829a4aSRandall Stewart 	/* stop the timer */
3010b7d130beSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net,
3011b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3012f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3013f8829a4aSRandall Stewart 	/* free the TCB */
30142afb3e84SRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
30152afb3e84SRandall Stewart 	    "sctp_handle_shutdown_complete: calls free-asoc\n");
3016b7d130beSMichael Tuexen 	(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
3017b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3018f8829a4aSRandall Stewart 	return;
3019f8829a4aSRandall Stewart }
3020f8829a4aSRandall Stewart 
3021f8829a4aSRandall Stewart static int
3022f8829a4aSRandall Stewart process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3023f8829a4aSRandall Stewart     struct sctp_nets *net, uint8_t flg)
3024f8829a4aSRandall Stewart {
3025f8829a4aSRandall Stewart 	switch (desc->chunk_type) {
3026f8829a4aSRandall Stewart 	case SCTP_DATA:
302732df1c9eSMichael Tuexen 	case SCTP_IDATA:
302832df1c9eSMichael Tuexen 		/* find the tsn to resend (possibly) */
3029f8829a4aSRandall Stewart 		{
3030f8829a4aSRandall Stewart 			uint32_t tsn;
3031f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *tp1;
3032f8829a4aSRandall Stewart 
3033f8829a4aSRandall Stewart 			tsn = ntohl(desc->tsn_ifany);
30344a9ef3f8SMichael Tuexen 			TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
303549656eefSMichael Tuexen 				if (tp1->rec.data.tsn == tsn) {
3036f8829a4aSRandall Stewart 					/* found it */
3037f8829a4aSRandall Stewart 					break;
3038f8829a4aSRandall Stewart 				}
303949656eefSMichael Tuexen 				if (SCTP_TSN_GT(tp1->rec.data.tsn, tsn)) {
3040f8829a4aSRandall Stewart 					/* not found */
3041f8829a4aSRandall Stewart 					tp1 = NULL;
3042f8829a4aSRandall Stewart 					break;
3043f8829a4aSRandall Stewart 				}
3044f8829a4aSRandall Stewart 			}
3045f8829a4aSRandall Stewart 			if (tp1 == NULL) {
3046f8829a4aSRandall Stewart 				/*
3047f8829a4aSRandall Stewart 				 * Do it the other way , aka without paying
3048f8829a4aSRandall Stewart 				 * attention to queue seq order.
3049f8829a4aSRandall Stewart 				 */
3050f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpdnfnd);
30514a9ef3f8SMichael Tuexen 				TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
305249656eefSMichael Tuexen 					if (tp1->rec.data.tsn == tsn) {
3053f8829a4aSRandall Stewart 						/* found it */
3054f8829a4aSRandall Stewart 						break;
3055f8829a4aSRandall Stewart 					}
3056f8829a4aSRandall Stewart 				}
3057f8829a4aSRandall Stewart 			}
3058f8829a4aSRandall Stewart 			if (tp1 == NULL) {
3059f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrptsnnf);
3060f8829a4aSRandall Stewart 			}
3061f8829a4aSRandall Stewart 			if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
30627da23bc8SMichael Tuexen 				if (((flg & SCTP_BADCRC) == 0) &&
30637da23bc8SMichael Tuexen 				    ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
30647da23bc8SMichael Tuexen 					return (0);
30657da23bc8SMichael Tuexen 				}
3066f8829a4aSRandall Stewart 				if ((stcb->asoc.peers_rwnd == 0) &&
3067f8829a4aSRandall Stewart 				    ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3068f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_pdrpdiwnp);
3069f8829a4aSRandall Stewart 					return (0);
3070f8829a4aSRandall Stewart 				}
3071f8829a4aSRandall Stewart 				if (stcb->asoc.peers_rwnd == 0 &&
3072f8829a4aSRandall Stewart 				    (flg & SCTP_FROM_MIDDLE_BOX)) {
3073f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_pdrpdizrw);
3074f8829a4aSRandall Stewart 					return (0);
3075f8829a4aSRandall Stewart 				}
307632df1c9eSMichael Tuexen 				if ((uint32_t)SCTP_BUF_LEN(tp1->data) <
307732df1c9eSMichael Tuexen 				    SCTP_DATA_CHUNK_OVERHEAD(stcb) + SCTP_NUM_DB_TO_VERIFY) {
307832df1c9eSMichael Tuexen 					/* Payload not matching. */
3079f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_pdrpbadd);
3080f8829a4aSRandall Stewart 					return (-1);
3081f8829a4aSRandall Stewart 				}
308232df1c9eSMichael Tuexen 				if (memcmp(mtod(tp1->data, caddr_t)+SCTP_DATA_CHUNK_OVERHEAD(stcb),
308332df1c9eSMichael Tuexen 				    desc->data_bytes, SCTP_NUM_DB_TO_VERIFY) != 0) {
308432df1c9eSMichael Tuexen 					/* Payload not matching. */
308532df1c9eSMichael Tuexen 					SCTP_STAT_INCR(sctps_pdrpbadd);
308632df1c9eSMichael Tuexen 					return (-1);
3087f8829a4aSRandall Stewart 				}
3088f8829a4aSRandall Stewart 				if (tp1->do_rtt) {
3089f8829a4aSRandall Stewart 					/*
3090f8829a4aSRandall Stewart 					 * this guy had a RTO calculation
3091f8829a4aSRandall Stewart 					 * pending on it, cancel it
3092f8829a4aSRandall Stewart 					 */
3093f79aab18SRandall Stewart 					if (tp1->whoTo->rto_needed == 0) {
3094f79aab18SRandall Stewart 						tp1->whoTo->rto_needed = 1;
3095f79aab18SRandall Stewart 					}
3096f8829a4aSRandall Stewart 					tp1->do_rtt = 0;
3097f8829a4aSRandall Stewart 				}
3098f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpmark);
3099f8829a4aSRandall Stewart 				if (tp1->sent != SCTP_DATAGRAM_RESEND)
3100f8829a4aSRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3101f8829a4aSRandall Stewart 				/*
3102f8829a4aSRandall Stewart 				 * mark it as if we were doing a FR, since
3103f8829a4aSRandall Stewart 				 * we will be getting gap ack reports behind
3104f8829a4aSRandall Stewart 				 * the info from the router.
3105f8829a4aSRandall Stewart 				 */
3106f8829a4aSRandall Stewart 				tp1->rec.data.doing_fast_retransmit = 1;
3107f8829a4aSRandall Stewart 				/*
3108f8829a4aSRandall Stewart 				 * mark the tsn with what sequences can
3109f8829a4aSRandall Stewart 				 * cause a new FR.
3110f8829a4aSRandall Stewart 				 */
3111f8829a4aSRandall Stewart 				if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3112f8829a4aSRandall Stewart 					tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3113f8829a4aSRandall Stewart 				} else {
311449656eefSMichael Tuexen 					tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.tsn;
3115f8829a4aSRandall Stewart 				}
3116f8829a4aSRandall Stewart 
3117f8829a4aSRandall Stewart 				/* restart the timer */
3118f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3119b7d130beSMichael Tuexen 				    stcb, tp1->whoTo,
3120b7d130beSMichael Tuexen 				    SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3121f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3122f8829a4aSRandall Stewart 				    stcb, tp1->whoTo);
3123f8829a4aSRandall Stewart 
3124f8829a4aSRandall Stewart 				/* fix counts and things */
3125b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3126c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3127a5d547adSRandall Stewart 					    tp1->whoTo->flight_size,
3128a5d547adSRandall Stewart 					    tp1->book_size,
31299a8e3088SMichael Tuexen 					    (uint32_t)(uintptr_t)stcb,
313049656eefSMichael Tuexen 					    tp1->rec.data.tsn);
313180fefe0aSRandall Stewart 				}
31328933fa13SRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3133c105859eSRandall Stewart 					sctp_flight_size_decrease(tp1);
3134c105859eSRandall Stewart 					sctp_total_flight_decrease(stcb, tp1);
31358933fa13SRandall Stewart 				}
3136f23ba7b1SMichael Tuexen 				tp1->sent = SCTP_DATAGRAM_RESEND;
3137f8829a4aSRandall Stewart 			} {
3138f8829a4aSRandall Stewart 				/* audit code */
3139f8829a4aSRandall Stewart 				unsigned int audit;
3140f8829a4aSRandall Stewart 
3141f8829a4aSRandall Stewart 				audit = 0;
3142f8829a4aSRandall Stewart 				TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3143f8829a4aSRandall Stewart 					if (tp1->sent == SCTP_DATAGRAM_RESEND)
3144f8829a4aSRandall Stewart 						audit++;
3145f8829a4aSRandall Stewart 				}
3146f8829a4aSRandall Stewart 				TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3147f8829a4aSRandall Stewart 				    sctp_next) {
3148f8829a4aSRandall Stewart 					if (tp1->sent == SCTP_DATAGRAM_RESEND)
3149f8829a4aSRandall Stewart 						audit++;
3150f8829a4aSRandall Stewart 				}
3151f8829a4aSRandall Stewart 				if (audit != stcb->asoc.sent_queue_retran_cnt) {
3152ad81507eSRandall Stewart 					SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3153f8829a4aSRandall Stewart 					    audit, stcb->asoc.sent_queue_retran_cnt);
3154f8829a4aSRandall Stewart #ifndef SCTP_AUDITING_ENABLED
3155f8829a4aSRandall Stewart 					stcb->asoc.sent_queue_retran_cnt = audit;
3156f8829a4aSRandall Stewart #endif
3157f8829a4aSRandall Stewart 				}
3158f8829a4aSRandall Stewart 			}
3159f8829a4aSRandall Stewart 		}
3160f8829a4aSRandall Stewart 		break;
3161f8829a4aSRandall Stewart 	case SCTP_ASCONF:
3162f8829a4aSRandall Stewart 		{
3163f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *asconf;
3164f8829a4aSRandall Stewart 
3165f8829a4aSRandall Stewart 			TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3166f8829a4aSRandall Stewart 			    sctp_next) {
3167f8829a4aSRandall Stewart 				if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3168f8829a4aSRandall Stewart 					break;
3169f8829a4aSRandall Stewart 				}
3170f8829a4aSRandall Stewart 			}
3171f8829a4aSRandall Stewart 			if (asconf) {
3172f8829a4aSRandall Stewart 				if (asconf->sent != SCTP_DATAGRAM_RESEND)
3173f8829a4aSRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3174f8829a4aSRandall Stewart 				asconf->sent = SCTP_DATAGRAM_RESEND;
3175f8829a4aSRandall Stewart 				asconf->snd_count--;
3176f8829a4aSRandall Stewart 			}
3177f8829a4aSRandall Stewart 		}
3178f8829a4aSRandall Stewart 		break;
3179f8829a4aSRandall Stewart 	case SCTP_INITIATION:
3180f8829a4aSRandall Stewart 		/* resend the INIT */
3181f8829a4aSRandall Stewart 		stcb->asoc.dropped_special_cnt++;
3182f8829a4aSRandall Stewart 		if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3183f8829a4aSRandall Stewart 			/*
3184f8829a4aSRandall Stewart 			 * If we can get it in, in a few attempts we do
3185f8829a4aSRandall Stewart 			 * this, otherwise we let the timer fire.
3186f8829a4aSRandall Stewart 			 */
3187f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3188b7d130beSMichael Tuexen 			    stcb, net,
3189b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
3190ceaad40aSRandall Stewart 			sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3191f8829a4aSRandall Stewart 		}
3192f8829a4aSRandall Stewart 		break;
3193f8829a4aSRandall Stewart 	case SCTP_SELECTIVE_ACK:
3194b5c16493SMichael Tuexen 	case SCTP_NR_SELECTIVE_ACK:
3195f8829a4aSRandall Stewart 		/* resend the sack */
3196689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3197f8829a4aSRandall Stewart 		break;
3198f8829a4aSRandall Stewart 	case SCTP_HEARTBEAT_REQUEST:
3199f8829a4aSRandall Stewart 		/* resend a demand HB */
3200b54d3a6cSRandall Stewart 		if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3201b7b84c0eSMichael Tuexen 			/*
3202b7b84c0eSMichael Tuexen 			 * Only retransmit if we KNOW we wont destroy the
3203b7b84c0eSMichael Tuexen 			 * tcb
3204b7b84c0eSMichael Tuexen 			 */
3205ca85e948SMichael Tuexen 			sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3206b54d3a6cSRandall Stewart 		}
3207f8829a4aSRandall Stewart 		break;
3208f8829a4aSRandall Stewart 	case SCTP_SHUTDOWN:
3209f8829a4aSRandall Stewart 		sctp_send_shutdown(stcb, net);
3210f8829a4aSRandall Stewart 		break;
3211f8829a4aSRandall Stewart 	case SCTP_SHUTDOWN_ACK:
3212f8829a4aSRandall Stewart 		sctp_send_shutdown_ack(stcb, net);
3213f8829a4aSRandall Stewart 		break;
3214f8829a4aSRandall Stewart 	case SCTP_COOKIE_ECHO:
3215f8829a4aSRandall Stewart 		{
3216f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *cookie;
3217f8829a4aSRandall Stewart 
3218f8829a4aSRandall Stewart 			cookie = NULL;
3219f8829a4aSRandall Stewart 			TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3220f8829a4aSRandall Stewart 			    sctp_next) {
3221f8829a4aSRandall Stewart 				if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3222f8829a4aSRandall Stewart 					break;
3223f8829a4aSRandall Stewart 				}
3224f8829a4aSRandall Stewart 			}
3225f8829a4aSRandall Stewart 			if (cookie) {
3226f8829a4aSRandall Stewart 				if (cookie->sent != SCTP_DATAGRAM_RESEND)
3227f8829a4aSRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3228f8829a4aSRandall Stewart 				cookie->sent = SCTP_DATAGRAM_RESEND;
3229f8829a4aSRandall Stewart 				sctp_stop_all_cookie_timers(stcb);
3230f8829a4aSRandall Stewart 			}
3231f8829a4aSRandall Stewart 		}
3232f8829a4aSRandall Stewart 		break;
3233f8829a4aSRandall Stewart 	case SCTP_COOKIE_ACK:
3234f8829a4aSRandall Stewart 		sctp_send_cookie_ack(stcb);
3235f8829a4aSRandall Stewart 		break;
3236f8829a4aSRandall Stewart 	case SCTP_ASCONF_ACK:
3237f8829a4aSRandall Stewart 		/* resend last asconf ack */
32382afb3e84SRandall Stewart 		sctp_send_asconf_ack(stcb);
3239f8829a4aSRandall Stewart 		break;
324044249214SRandall Stewart 	case SCTP_IFORWARD_CUM_TSN:
3241f8829a4aSRandall Stewart 	case SCTP_FORWARD_CUM_TSN:
3242f8829a4aSRandall Stewart 		send_forward_tsn(stcb, &stcb->asoc);
3243f8829a4aSRandall Stewart 		break;
3244f8829a4aSRandall Stewart 		/* can't do anything with these */
3245f8829a4aSRandall Stewart 	case SCTP_PACKET_DROPPED:
3246f8829a4aSRandall Stewart 	case SCTP_INITIATION_ACK:	/* this should not happen */
3247f8829a4aSRandall Stewart 	case SCTP_HEARTBEAT_ACK:
3248f8829a4aSRandall Stewart 	case SCTP_ABORT_ASSOCIATION:
3249f8829a4aSRandall Stewart 	case SCTP_OPERATION_ERROR:
3250f8829a4aSRandall Stewart 	case SCTP_SHUTDOWN_COMPLETE:
3251f8829a4aSRandall Stewart 	case SCTP_ECN_ECHO:
3252f8829a4aSRandall Stewart 	case SCTP_ECN_CWR:
3253f8829a4aSRandall Stewart 	default:
3254f8829a4aSRandall Stewart 		break;
3255f8829a4aSRandall Stewart 	}
3256f8829a4aSRandall Stewart 	return (0);
3257f8829a4aSRandall Stewart }
3258f8829a4aSRandall Stewart 
3259f8829a4aSRandall Stewart void
3260a169d6ecSMichael Tuexen sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3261f8829a4aSRandall Stewart {
3262a169d6ecSMichael Tuexen 	uint32_t i;
3263f8829a4aSRandall Stewart 	uint16_t temp;
3264f8829a4aSRandall Stewart 
3265f8829a4aSRandall Stewart 	/*
326644249214SRandall Stewart 	 * We set things to 0xffffffff since this is the last delivered
326744249214SRandall Stewart 	 * sequence and we will be sending in 0 after the reset.
3268f8829a4aSRandall Stewart 	 */
3269f8829a4aSRandall Stewart 
3270f8829a4aSRandall Stewart 	if (number_entries) {
3271f8829a4aSRandall Stewart 		for (i = 0; i < number_entries; i++) {
3272f8829a4aSRandall Stewart 			temp = ntohs(list[i]);
3273f8829a4aSRandall Stewart 			if (temp >= stcb->asoc.streamincnt) {
3274f8829a4aSRandall Stewart 				continue;
3275f8829a4aSRandall Stewart 			}
327649656eefSMichael Tuexen 			stcb->asoc.strmin[temp].last_mid_delivered = 0xffffffff;
3277f8829a4aSRandall Stewart 		}
3278f8829a4aSRandall Stewart 	} else {
3279f8829a4aSRandall Stewart 		list = NULL;
3280f8829a4aSRandall Stewart 		for (i = 0; i < stcb->asoc.streamincnt; i++) {
328149656eefSMichael Tuexen 			stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
3282f8829a4aSRandall Stewart 		}
3283f8829a4aSRandall Stewart 	}
3284ceaad40aSRandall Stewart 	sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3285f8829a4aSRandall Stewart }
3286f8829a4aSRandall Stewart 
3287f8829a4aSRandall Stewart static void
328856f778aaSMichael Tuexen sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3289f8829a4aSRandall Stewart {
329056f778aaSMichael Tuexen 	uint32_t i;
3291f8829a4aSRandall Stewart 	uint16_t temp;
3292f8829a4aSRandall Stewart 
329356f778aaSMichael Tuexen 	if (number_entries > 0) {
329456f778aaSMichael Tuexen 		for (i = 0; i < number_entries; i++) {
3295f8829a4aSRandall Stewart 			temp = ntohs(list[i]);
3296f8829a4aSRandall Stewart 			if (temp >= stcb->asoc.streamoutcnt) {
3297f8829a4aSRandall Stewart 				/* no such stream */
3298f8829a4aSRandall Stewart 				continue;
3299f8829a4aSRandall Stewart 			}
330063d5b568SMichael Tuexen 			stcb->asoc.strmout[temp].next_mid_ordered = 0;
330163d5b568SMichael Tuexen 			stcb->asoc.strmout[temp].next_mid_unordered = 0;
3302f8829a4aSRandall Stewart 		}
330356f778aaSMichael Tuexen 	} else {
330456f778aaSMichael Tuexen 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
330563d5b568SMichael Tuexen 			stcb->asoc.strmout[i].next_mid_ordered = 0;
330663d5b568SMichael Tuexen 			stcb->asoc.strmout[i].next_mid_unordered = 0;
330756f778aaSMichael Tuexen 		}
3308f8829a4aSRandall Stewart 	}
3309ceaad40aSRandall Stewart 	sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3310f8829a4aSRandall Stewart }
3311f8829a4aSRandall Stewart 
33127cca1775SRandall Stewart static void
33137cca1775SRandall Stewart sctp_reset_clear_pending(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
33147cca1775SRandall Stewart {
33157cca1775SRandall Stewart 	uint32_t i;
33167cca1775SRandall Stewart 	uint16_t temp;
33177cca1775SRandall Stewart 
33187cca1775SRandall Stewart 	if (number_entries > 0) {
33197cca1775SRandall Stewart 		for (i = 0; i < number_entries; i++) {
33207cca1775SRandall Stewart 			temp = ntohs(list[i]);
33217cca1775SRandall Stewart 			if (temp >= stcb->asoc.streamoutcnt) {
33227cca1775SRandall Stewart 				/* no such stream */
33237cca1775SRandall Stewart 				continue;
33247cca1775SRandall Stewart 			}
33257cca1775SRandall Stewart 			stcb->asoc.strmout[temp].state = SCTP_STREAM_OPEN;
33267cca1775SRandall Stewart 		}
33277cca1775SRandall Stewart 	} else {
33287cca1775SRandall Stewart 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
33297cca1775SRandall Stewart 			stcb->asoc.strmout[i].state = SCTP_STREAM_OPEN;
33307cca1775SRandall Stewart 		}
33317cca1775SRandall Stewart 	}
33327cca1775SRandall Stewart }
33337cca1775SRandall Stewart 
333484f3b49aSMichael Tuexen struct sctp_stream_reset_request *
3335f8829a4aSRandall Stewart sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3336f8829a4aSRandall Stewart {
3337f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3338a169d6ecSMichael Tuexen 	struct sctp_chunkhdr *ch;
333984f3b49aSMichael Tuexen 	struct sctp_stream_reset_request *r;
3340f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
3341f8829a4aSRandall Stewart 	int len, clen;
3342f8829a4aSRandall Stewart 
3343f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
3344163153c2SMichael Tuexen 	chk = asoc->str_reset;
3345163153c2SMichael Tuexen 	if (TAILQ_EMPTY(&asoc->control_send_queue) ||
3346163153c2SMichael Tuexen 	    (chk == NULL)) {
3347d06c82f1SRandall Stewart 		asoc->stream_reset_outstanding = 0;
3348f8829a4aSRandall Stewart 		return (NULL);
3349f8829a4aSRandall Stewart 	}
3350f8829a4aSRandall Stewart 	if (chk->data == NULL) {
3351f8829a4aSRandall Stewart 		return (NULL);
3352f8829a4aSRandall Stewart 	}
3353163153c2SMichael Tuexen 	if (bchk != NULL) {
3354f8829a4aSRandall Stewart 		/* he wants a copy of the chk pointer */
3355f8829a4aSRandall Stewart 		*bchk = chk;
3356f8829a4aSRandall Stewart 	}
3357f8829a4aSRandall Stewart 	clen = chk->send_size;
3358a169d6ecSMichael Tuexen 	ch = mtod(chk->data, struct sctp_chunkhdr *);
335984f3b49aSMichael Tuexen 	r = (struct sctp_stream_reset_request *)(ch + 1);
3360f8829a4aSRandall Stewart 	if (ntohl(r->request_seq) == seq) {
3361f8829a4aSRandall Stewart 		/* found it */
3362f8829a4aSRandall Stewart 		return (r);
3363f8829a4aSRandall Stewart 	}
3364f8829a4aSRandall Stewart 	len = SCTP_SIZE32(ntohs(r->ph.param_length));
3365f8829a4aSRandall Stewart 	if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3366f8829a4aSRandall Stewart 		/* move to the next one, there can only be a max of two */
336784f3b49aSMichael Tuexen 		r = (struct sctp_stream_reset_request *)((caddr_t)r + len);
3368f8829a4aSRandall Stewart 		if (ntohl(r->request_seq) == seq) {
3369f8829a4aSRandall Stewart 			return (r);
3370f8829a4aSRandall Stewart 		}
3371f8829a4aSRandall Stewart 	}
3372f8829a4aSRandall Stewart 	/* that seq is not here */
3373f8829a4aSRandall Stewart 	return (NULL);
3374f8829a4aSRandall Stewart }
3375f8829a4aSRandall Stewart 
3376f8829a4aSRandall Stewart static void
3377f8829a4aSRandall Stewart sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3378f8829a4aSRandall Stewart {
3379f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3380cd6340caSMichael Tuexen 	struct sctp_tmit_chunk *chk;
3381f8829a4aSRandall Stewart 
3382cd6340caSMichael Tuexen 	asoc = &stcb->asoc;
3383cd6340caSMichael Tuexen 	chk = asoc->str_reset;
3384cd6340caSMichael Tuexen 	if (chk == NULL) {
3385f8829a4aSRandall Stewart 		return;
3386f8829a4aSRandall Stewart 	}
3387cd6340caSMichael Tuexen 	asoc->str_reset = NULL;
3388b7d130beSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb,
33896fb7b4fbSMichael Tuexen 	    NULL, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28);
3390cd6340caSMichael Tuexen 	TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
3391cd6340caSMichael Tuexen 	asoc->ctrl_queue_cnt--;
3392f8829a4aSRandall Stewart 	if (chk->data) {
3393f8829a4aSRandall Stewart 		sctp_m_freem(chk->data);
3394f8829a4aSRandall Stewart 		chk->data = NULL;
3395f8829a4aSRandall Stewart 	}
3396689e6a5fSMichael Tuexen 	sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3397f8829a4aSRandall Stewart }
3398f8829a4aSRandall Stewart 
3399f8829a4aSRandall Stewart static int
3400f8829a4aSRandall Stewart sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3401f8829a4aSRandall Stewart     uint32_t seq, uint32_t action,
340208598d70SRandall Stewart     struct sctp_stream_reset_response *respin)
3403f8829a4aSRandall Stewart {
3404f8829a4aSRandall Stewart 	uint16_t type;
3405f4358911SMichael Tuexen 	int lparam_len;
3406f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3407f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
340884f3b49aSMichael Tuexen 	struct sctp_stream_reset_request *req_param;
340984f3b49aSMichael Tuexen 	struct sctp_stream_reset_out_request *req_out_param;
341084f3b49aSMichael Tuexen 	struct sctp_stream_reset_in_request *req_in_param;
341156f778aaSMichael Tuexen 	uint32_t number_entries;
3412f8829a4aSRandall Stewart 
3413f8829a4aSRandall Stewart 	if (asoc->stream_reset_outstanding == 0) {
3414f8829a4aSRandall Stewart 		/* duplicate */
3415f8829a4aSRandall Stewart 		return (0);
3416f8829a4aSRandall Stewart 	}
3417f8829a4aSRandall Stewart 	if (seq == stcb->asoc.str_reset_seq_out) {
341884f3b49aSMichael Tuexen 		req_param = sctp_find_stream_reset(stcb, seq, &chk);
341984f3b49aSMichael Tuexen 		if (req_param != NULL) {
3420f8829a4aSRandall Stewart 			stcb->asoc.str_reset_seq_out++;
342184f3b49aSMichael Tuexen 			type = ntohs(req_param->ph.param_type);
3422f4358911SMichael Tuexen 			lparam_len = ntohs(req_param->ph.param_length);
3423f8829a4aSRandall Stewart 			if (type == SCTP_STR_RESET_OUT_REQUEST) {
34247cca1775SRandall Stewart 				int no_clear = 0;
34257cca1775SRandall Stewart 
342684f3b49aSMichael Tuexen 				req_out_param = (struct sctp_stream_reset_out_request *)req_param;
3427f4358911SMichael Tuexen 				number_entries = (lparam_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3428f8829a4aSRandall Stewart 				asoc->stream_reset_out_is_outstanding = 0;
3429f8829a4aSRandall Stewart 				if (asoc->stream_reset_outstanding)
3430f8829a4aSRandall Stewart 					asoc->stream_reset_outstanding--;
3431f3ebe64cSMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3432f8829a4aSRandall Stewart 					/* do it */
343384f3b49aSMichael Tuexen 					sctp_reset_out_streams(stcb, number_entries, req_out_param->list_of_streams);
3434d4260646SMichael Tuexen 				} else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
343584f3b49aSMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
34367cca1775SRandall Stewart 				} else if (action == SCTP_STREAM_RESET_RESULT_IN_PROGRESS) {
3437b7b84c0eSMichael Tuexen 					/*
3438b7b84c0eSMichael Tuexen 					 * Set it up so we don't stop
3439b7b84c0eSMichael Tuexen 					 * retransmitting
3440b7b84c0eSMichael Tuexen 					 */
3441a4889f2dSMichael Tuexen 					asoc->stream_reset_outstanding++;
34427cca1775SRandall Stewart 					stcb->asoc.str_reset_seq_out--;
34437cca1775SRandall Stewart 					asoc->stream_reset_out_is_outstanding = 1;
34447cca1775SRandall Stewart 					no_clear = 1;
3445f8829a4aSRandall Stewart 				} else {
344684f3b49aSMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3447f8829a4aSRandall Stewart 				}
34487cca1775SRandall Stewart 				if (no_clear == 0) {
34497cca1775SRandall Stewart 					sctp_reset_clear_pending(stcb, number_entries, req_out_param->list_of_streams);
34507cca1775SRandall Stewart 				}
3451f8829a4aSRandall Stewart 			} else if (type == SCTP_STR_RESET_IN_REQUEST) {
345284f3b49aSMichael Tuexen 				req_in_param = (struct sctp_stream_reset_in_request *)req_param;
3453f4358911SMichael Tuexen 				number_entries = (lparam_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3454f8829a4aSRandall Stewart 				if (asoc->stream_reset_outstanding)
3455f8829a4aSRandall Stewart 					asoc->stream_reset_outstanding--;
3456d4260646SMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3457d4260646SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
345884f3b49aSMichael Tuexen 					    number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3459d4260646SMichael Tuexen 				} else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3460c4e848b7SRandall Stewart 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
346184f3b49aSMichael Tuexen 					    number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3462f8829a4aSRandall Stewart 				}
3463c4e848b7SRandall Stewart 			} else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3464ea44232bSRandall Stewart 				/* Ok we now may have more streams */
3465c4e848b7SRandall Stewart 				int num_stream;
3466c4e848b7SRandall Stewart 
3467c4e848b7SRandall Stewart 				num_stream = stcb->asoc.strm_pending_add_size;
3468c4e848b7SRandall Stewart 				if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3469c4e848b7SRandall Stewart 					/* TSNH */
3470c4e848b7SRandall Stewart 					num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3471c4e848b7SRandall Stewart 				}
3472c4e848b7SRandall Stewart 				stcb->asoc.strm_pending_add_size = 0;
34738aae9493SRandall Stewart 				if (asoc->stream_reset_outstanding)
34748aae9493SRandall Stewart 					asoc->stream_reset_outstanding--;
3475f3ebe64cSMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3476ea44232bSRandall Stewart 					/* Put the new streams into effect */
34777cca1775SRandall Stewart 					int i;
34787cca1775SRandall Stewart 
34797cca1775SRandall Stewart 					for (i = asoc->streamoutcnt; i < (asoc->streamoutcnt + num_stream); i++) {
34807cca1775SRandall Stewart 						asoc->strmout[i].state = SCTP_STREAM_OPEN;
34817cca1775SRandall Stewart 					}
34827cca1775SRandall Stewart 					asoc->streamoutcnt += num_stream;
3483c4e848b7SRandall Stewart 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3484d4260646SMichael Tuexen 				} else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3485d4260646SMichael Tuexen 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3486d4260646SMichael Tuexen 					    SCTP_STREAM_CHANGE_DENIED);
3487ea44232bSRandall Stewart 				} else {
3488c4e848b7SRandall Stewart 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3489d4260646SMichael Tuexen 					    SCTP_STREAM_CHANGE_FAILED);
3490c4e848b7SRandall Stewart 				}
3491c4e848b7SRandall Stewart 			} else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3492c4e848b7SRandall Stewart 				if (asoc->stream_reset_outstanding)
3493c4e848b7SRandall Stewart 					asoc->stream_reset_outstanding--;
3494d4260646SMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3495c4e848b7SRandall Stewart 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3496d4260646SMichael Tuexen 					    SCTP_STREAM_CHANGE_DENIED);
3497d4260646SMichael Tuexen 				} else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3498d4260646SMichael Tuexen 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3499d4260646SMichael Tuexen 					    SCTP_STREAM_CHANGE_FAILED);
3500ea44232bSRandall Stewart 				}
3501f8829a4aSRandall Stewart 			} else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3502f8829a4aSRandall Stewart 				/**
3503f8829a4aSRandall Stewart 				 * a) Adopt the new in tsn.
3504f8829a4aSRandall Stewart 				 * b) reset the map
3505f8829a4aSRandall Stewart 				 * c) Adopt the new out-tsn
3506f8829a4aSRandall Stewart 				 */
3507f8829a4aSRandall Stewart 				struct sctp_stream_reset_response_tsn *resp;
3508f8829a4aSRandall Stewart 				struct sctp_forward_tsn_chunk fwdtsn;
3509f8829a4aSRandall Stewart 				int abort_flag = 0;
3510f8829a4aSRandall Stewart 
3511f8829a4aSRandall Stewart 				if (respin == NULL) {
3512f8829a4aSRandall Stewart 					/* huh ? */
3513f8829a4aSRandall Stewart 					return (0);
3514f8829a4aSRandall Stewart 				}
35156a58f0e9SXin LI 				if (ntohs(respin->ph.param_length) < sizeof(struct sctp_stream_reset_response_tsn)) {
35166a58f0e9SXin LI 					return (0);
35176a58f0e9SXin LI 				}
3518f3ebe64cSMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3519f8829a4aSRandall Stewart 					resp = (struct sctp_stream_reset_response_tsn *)respin;
3520f8829a4aSRandall Stewart 					asoc->stream_reset_outstanding--;
3521f8829a4aSRandall Stewart 					fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3522f8829a4aSRandall Stewart 					fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3523f8829a4aSRandall Stewart 					fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3524671d309cSRandall Stewart 					sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3525f8829a4aSRandall Stewart 					if (abort_flag) {
3526f8829a4aSRandall Stewart 						return (1);
3527f8829a4aSRandall Stewart 					}
3528f8829a4aSRandall Stewart 					stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3529b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3530b3f1ea41SRandall Stewart 						sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3531b3f1ea41SRandall Stewart 					}
35320053ed28SMichael Tuexen 
3533d6af161aSRandall Stewart 					stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3534f8829a4aSRandall Stewart 					stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3535f8829a4aSRandall Stewart 					memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3536830d754dSRandall Stewart 
3537830d754dSRandall Stewart 					stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3538b5c16493SMichael Tuexen 					memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
353977acdc25SRandall Stewart 
3540f8829a4aSRandall Stewart 					stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3541f8829a4aSRandall Stewart 					stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3542f8829a4aSRandall Stewart 
3543f8829a4aSRandall Stewart 					sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3544f8829a4aSRandall Stewart 					sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3545c4e848b7SRandall Stewart 					sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3546d4260646SMichael Tuexen 				} else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3547d4260646SMichael Tuexen 					sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3548d4260646SMichael Tuexen 					    SCTP_ASSOC_RESET_DENIED);
3549c4e848b7SRandall Stewart 				} else {
3550c4e848b7SRandall Stewart 					sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3551d4260646SMichael Tuexen 					    SCTP_ASSOC_RESET_FAILED);
3552f8829a4aSRandall Stewart 				}
3553f8829a4aSRandall Stewart 			}
3554f8829a4aSRandall Stewart 			/* get rid of the request and get the request flags */
3555f8829a4aSRandall Stewart 			if (asoc->stream_reset_outstanding == 0) {
3556f8829a4aSRandall Stewart 				sctp_clean_up_stream_reset(stcb);
3557f8829a4aSRandall Stewart 			}
3558f8829a4aSRandall Stewart 		}
3559f8829a4aSRandall Stewart 	}
35607cca1775SRandall Stewart 	if (asoc->stream_reset_outstanding == 0) {
3561c6168599SRandall Stewart 		sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
35627cca1775SRandall Stewart 	}
3563f8829a4aSRandall Stewart 	return (0);
3564f8829a4aSRandall Stewart }
3565f8829a4aSRandall Stewart 
3566f8829a4aSRandall Stewart static void
3567f8829a4aSRandall Stewart sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3568f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk,
3569671d309cSRandall Stewart     struct sctp_stream_reset_in_request *req, int trunc)
3570f8829a4aSRandall Stewart {
3571f8829a4aSRandall Stewart 	uint32_t seq;
3572f8829a4aSRandall Stewart 	int len, i;
3573f8829a4aSRandall Stewart 	int number_entries;
3574f8829a4aSRandall Stewart 	uint16_t temp;
3575f8829a4aSRandall Stewart 
3576f8829a4aSRandall Stewart 	/*
3577f8829a4aSRandall Stewart 	 * peer wants me to send a str-reset to him for my outgoing seq's if
3578f8829a4aSRandall Stewart 	 * seq_in is right.
3579f8829a4aSRandall Stewart 	 */
3580f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3581f8829a4aSRandall Stewart 
3582f8829a4aSRandall Stewart 	seq = ntohl(req->request_seq);
3583f8829a4aSRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3584671d309cSRandall Stewart 		asoc->last_reset_action[1] = asoc->last_reset_action[0];
35859b2a35b3SMichael Tuexen 		if ((asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ) == 0) {
3586f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3587f3ebe64cSMichael Tuexen 		} else if (trunc) {
3588f3ebe64cSMichael Tuexen 			/* Can't do it, since they exceeded our buffer size  */
3589f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3590671d309cSRandall Stewart 		} else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3591f8829a4aSRandall Stewart 			len = ntohs(req->ph.param_length);
3592f8829a4aSRandall Stewart 			number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
35937cca1775SRandall Stewart 			if (number_entries) {
3594f8829a4aSRandall Stewart 				for (i = 0; i < number_entries; i++) {
3595f8829a4aSRandall Stewart 					temp = ntohs(req->list_of_streams[i]);
35967cca1775SRandall Stewart 					if (temp >= stcb->asoc.streamoutcnt) {
35977cca1775SRandall Stewart 						asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
35987cca1775SRandall Stewart 						goto bad_boy;
35997cca1775SRandall Stewart 					}
3600f8829a4aSRandall Stewart 					req->list_of_streams[i] = temp;
3601f8829a4aSRandall Stewart 				}
36027cca1775SRandall Stewart 				for (i = 0; i < number_entries; i++) {
36037cca1775SRandall Stewart 					if (stcb->asoc.strmout[req->list_of_streams[i]].state == SCTP_STREAM_OPEN) {
36047cca1775SRandall Stewart 						stcb->asoc.strmout[req->list_of_streams[i]].state = SCTP_STREAM_RESET_PENDING;
36057cca1775SRandall Stewart 					}
36067cca1775SRandall Stewart 				}
36077cca1775SRandall Stewart 			} else {
36087cca1775SRandall Stewart 				/* Its all */
36097cca1775SRandall Stewart 				for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
36107cca1775SRandall Stewart 					if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN)
36117cca1775SRandall Stewart 						stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
36127cca1775SRandall Stewart 				}
36137cca1775SRandall Stewart 			}
3614f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3615f8829a4aSRandall Stewart 		} else {
3616f8829a4aSRandall Stewart 			/* Can't do it, since we have sent one out */
3617f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3618f8829a4aSRandall Stewart 		}
36197cca1775SRandall Stewart bad_boy:
3620c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3621f8829a4aSRandall Stewart 		asoc->str_reset_seq_in++;
3622f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 1 == seq) {
3623f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3624f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 2 == seq) {
3625f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3626f8829a4aSRandall Stewart 	} else {
3627f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3628f8829a4aSRandall Stewart 	}
3629c6168599SRandall Stewart 	sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3630f8829a4aSRandall Stewart }
3631f8829a4aSRandall Stewart 
3632f8829a4aSRandall Stewart static int
3633f8829a4aSRandall Stewart sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3634f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk,
3635f8829a4aSRandall Stewart     struct sctp_stream_reset_tsn_request *req)
3636f8829a4aSRandall Stewart {
3637f8829a4aSRandall Stewart 	/* reset all in and out and update the tsn */
3638f8829a4aSRandall Stewart 	/*
3639f8829a4aSRandall Stewart 	 * A) reset my str-seq's on in and out. B) Select a receive next,
3640f8829a4aSRandall Stewart 	 * and set cum-ack to it. Also process this selected number as a
3641f8829a4aSRandall Stewart 	 * fwd-tsn as well. C) set in the response my next sending seq.
3642f8829a4aSRandall Stewart 	 */
3643f8829a4aSRandall Stewart 	struct sctp_forward_tsn_chunk fwdtsn;
3644f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3645f8829a4aSRandall Stewart 	int abort_flag = 0;
3646f8829a4aSRandall Stewart 	uint32_t seq;
3647f8829a4aSRandall Stewart 
3648f8829a4aSRandall Stewart 	seq = ntohl(req->request_seq);
3649f8829a4aSRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3650ce228dabSMichael Tuexen 		asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
36519b2a35b3SMichael Tuexen 		if ((asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ) == 0) {
3652f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3653ce228dabSMichael Tuexen 		} else {
3654f8829a4aSRandall Stewart 			fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3655f8829a4aSRandall Stewart 			fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3656f8829a4aSRandall Stewart 			fwdtsn.ch.chunk_flags = 0;
3657f8829a4aSRandall Stewart 			fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3658671d309cSRandall Stewart 			sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3659f8829a4aSRandall Stewart 			if (abort_flag) {
3660f8829a4aSRandall Stewart 				return (1);
3661f8829a4aSRandall Stewart 			}
3662f3ebe64cSMichael Tuexen 			asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3663b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3664b3f1ea41SRandall Stewart 				sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3665b3f1ea41SRandall Stewart 			}
3666f3ebe64cSMichael Tuexen 			asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3667f3ebe64cSMichael Tuexen 			asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3668f3ebe64cSMichael Tuexen 			memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3669f3ebe64cSMichael Tuexen 			asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3670f3ebe64cSMichael Tuexen 			memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3671f3ebe64cSMichael Tuexen 			atomic_add_int(&asoc->sending_seq, 1);
3672f8829a4aSRandall Stewart 			/* save off historical data for retrans */
3673f3ebe64cSMichael Tuexen 			asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3674f3ebe64cSMichael Tuexen 			asoc->last_sending_seq[0] = asoc->sending_seq;
3675f3ebe64cSMichael Tuexen 			asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3676f3ebe64cSMichael Tuexen 			asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3677f8829a4aSRandall Stewart 			sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3678f8829a4aSRandall Stewart 			sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3679f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3680f3ebe64cSMichael Tuexen 			sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3681ce228dabSMichael Tuexen 		}
3682ce228dabSMichael Tuexen 		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3683ce228dabSMichael Tuexen 		    asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3684f8829a4aSRandall Stewart 		asoc->str_reset_seq_in++;
3685f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 1 == seq) {
3686f8829a4aSRandall Stewart 		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3687f3ebe64cSMichael Tuexen 		    asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3688f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 2 == seq) {
3689f8829a4aSRandall Stewart 		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3690f3ebe64cSMichael Tuexen 		    asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3691f8829a4aSRandall Stewart 	} else {
3692f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3693f8829a4aSRandall Stewart 	}
3694f8829a4aSRandall Stewart 	return (0);
3695f8829a4aSRandall Stewart }
3696f8829a4aSRandall Stewart 
3697f8829a4aSRandall Stewart static void
3698f8829a4aSRandall Stewart sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3699f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk,
3700671d309cSRandall Stewart     struct sctp_stream_reset_out_request *req, int trunc)
3701f8829a4aSRandall Stewart {
3702f8829a4aSRandall Stewart 	uint32_t seq, tsn;
3703f8829a4aSRandall Stewart 	int number_entries, len;
3704f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3705f8829a4aSRandall Stewart 
3706f8829a4aSRandall Stewart 	seq = ntohl(req->request_seq);
3707f8829a4aSRandall Stewart 
3708f8829a4aSRandall Stewart 	/* now if its not a duplicate we process it */
3709f8829a4aSRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3710f8829a4aSRandall Stewart 		len = ntohs(req->ph.param_length);
3711f8829a4aSRandall Stewart 		number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3712f8829a4aSRandall Stewart 		/*
3713f8829a4aSRandall Stewart 		 * the sender is resetting, handle the list issue.. we must
3714f8829a4aSRandall Stewart 		 * a) verify if we can do the reset, if so no problem b) If
3715f8829a4aSRandall Stewart 		 * we can't do the reset we must copy the request. c) queue
3716f8829a4aSRandall Stewart 		 * it, and setup the data in processor to trigger it off
3717f8829a4aSRandall Stewart 		 * when needed and dequeue all the queued data.
3718f8829a4aSRandall Stewart 		 */
3719f8829a4aSRandall Stewart 		tsn = ntohl(req->send_reset_at_tsn);
3720f8829a4aSRandall Stewart 
3721f8829a4aSRandall Stewart 		/* move the reset action back one */
3722f8829a4aSRandall Stewart 		asoc->last_reset_action[1] = asoc->last_reset_action[0];
37239b2a35b3SMichael Tuexen 		if ((asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ) == 0) {
3724f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3725f3ebe64cSMichael Tuexen 		} else if (trunc) {
3726f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
372720b07a4dSMichael Tuexen 		} else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3728f8829a4aSRandall Stewart 			/* we can do it now */
3729f8829a4aSRandall Stewart 			sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3730f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3731f8829a4aSRandall Stewart 		} else {
3732f8829a4aSRandall Stewart 			/*
3733f8829a4aSRandall Stewart 			 * we must queue it up and thus wait for the TSN's
3734f8829a4aSRandall Stewart 			 * to arrive that are at or before tsn
3735f8829a4aSRandall Stewart 			 */
3736f8829a4aSRandall Stewart 			struct sctp_stream_reset_list *liste;
3737f8829a4aSRandall Stewart 			int siz;
3738f8829a4aSRandall Stewart 
3739f8829a4aSRandall Stewart 			siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3740f8829a4aSRandall Stewart 			SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3741207304d4SRandall Stewart 			    siz, SCTP_M_STRESET);
3742f8829a4aSRandall Stewart 			if (liste == NULL) {
3743f8829a4aSRandall Stewart 				/* gak out of memory */
3744f3ebe64cSMichael Tuexen 				asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3745f3ebe64cSMichael Tuexen 				sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3746f8829a4aSRandall Stewart 				return;
3747f8829a4aSRandall Stewart 			}
37487cca1775SRandall Stewart 			liste->seq = seq;
3749f8829a4aSRandall Stewart 			liste->tsn = tsn;
3750f8829a4aSRandall Stewart 			liste->number_entries = number_entries;
3751a169d6ecSMichael Tuexen 			memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t));
3752f8829a4aSRandall Stewart 			TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
37537cca1775SRandall Stewart 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_IN_PROGRESS;
3754f8829a4aSRandall Stewart 		}
3755c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3756f8829a4aSRandall Stewart 		asoc->str_reset_seq_in++;
3757f8829a4aSRandall Stewart 	} else if ((asoc->str_reset_seq_in - 1) == seq) {
3758f8829a4aSRandall Stewart 		/*
3759f8829a4aSRandall Stewart 		 * one seq back, just echo back last action since my
3760f8829a4aSRandall Stewart 		 * response was lost.
3761f8829a4aSRandall Stewart 		 */
3762f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3763f8829a4aSRandall Stewart 	} else if ((asoc->str_reset_seq_in - 2) == seq) {
3764f8829a4aSRandall Stewart 		/*
3765f8829a4aSRandall Stewart 		 * two seq back, just echo back last action since my
3766f8829a4aSRandall Stewart 		 * response was lost.
3767f8829a4aSRandall Stewart 		 */
3768f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3769f8829a4aSRandall Stewart 	} else {
3770f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3771f8829a4aSRandall Stewart 	}
3772f8829a4aSRandall Stewart }
3773f8829a4aSRandall Stewart 
3774ea44232bSRandall Stewart static void
3775ea44232bSRandall Stewart sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3776ea44232bSRandall Stewart     struct sctp_stream_reset_add_strm *str_add)
3777ea44232bSRandall Stewart {
3778ea44232bSRandall Stewart 	/*
3779ea44232bSRandall Stewart 	 * Peer is requesting to add more streams. If its within our
3780ea44232bSRandall Stewart 	 * max-streams we will allow it.
3781ea44232bSRandall Stewart 	 */
3782c4e848b7SRandall Stewart 	uint32_t num_stream, i;
3783ea44232bSRandall Stewart 	uint32_t seq;
37848aae9493SRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
37854a9ef3f8SMichael Tuexen 	struct sctp_queued_to_read *ctl, *nctl;
3786ea44232bSRandall Stewart 
3787ea44232bSRandall Stewart 	/* Get the number. */
3788ea44232bSRandall Stewart 	seq = ntohl(str_add->request_seq);
3789ea44232bSRandall Stewart 	num_stream = ntohs(str_add->number_of_streams);
3790ea44232bSRandall Stewart 	/* Now what would be the new total? */
37918aae9493SRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3792ea44232bSRandall Stewart 		num_stream += stcb->asoc.streamincnt;
3793f3ebe64cSMichael Tuexen 		stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
37949b2a35b3SMichael Tuexen 		if ((asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ) == 0) {
3795f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3796f3ebe64cSMichael Tuexen 		} else if ((num_stream > stcb->asoc.max_inbound_streams) ||
3797c4e848b7SRandall Stewart 		    (num_stream > 0xffff)) {
3798ea44232bSRandall Stewart 			/* We must reject it they ask for to many */
3799ea44232bSRandall Stewart 	denied:
3800f3ebe64cSMichael Tuexen 			stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3801ea44232bSRandall Stewart 		} else {
3802ea44232bSRandall Stewart 			/* Ok, we can do that :-) */
3803ea44232bSRandall Stewart 			struct sctp_stream_in *oldstrm;
3804ea44232bSRandall Stewart 
3805ea44232bSRandall Stewart 			/* save off the old */
3806ea44232bSRandall Stewart 			oldstrm = stcb->asoc.strmin;
3807ea44232bSRandall Stewart 			SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
3808ea44232bSRandall Stewart 			    (num_stream * sizeof(struct sctp_stream_in)),
3809ea44232bSRandall Stewart 			    SCTP_M_STRMI);
3810ea44232bSRandall Stewart 			if (stcb->asoc.strmin == NULL) {
3811ea44232bSRandall Stewart 				stcb->asoc.strmin = oldstrm;
3812ea44232bSRandall Stewart 				goto denied;
3813ea44232bSRandall Stewart 			}
3814ea44232bSRandall Stewart 			/* copy off the old data */
38158aae9493SRandall Stewart 			for (i = 0; i < stcb->asoc.streamincnt; i++) {
38168aae9493SRandall Stewart 				TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
381744249214SRandall Stewart 				TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
381849656eefSMichael Tuexen 				stcb->asoc.strmin[i].sid = i;
381949656eefSMichael Tuexen 				stcb->asoc.strmin[i].last_mid_delivered = oldstrm[i].last_mid_delivered;
38208aae9493SRandall Stewart 				stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
382144249214SRandall Stewart 				stcb->asoc.strmin[i].pd_api_started = oldstrm[i].pd_api_started;
38228aae9493SRandall Stewart 				/* now anything on those queues? */
382344249214SRandall Stewart 				TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next_instrm, nctl) {
382444249214SRandall Stewart 					TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next_instrm);
382544249214SRandall Stewart 					TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next_instrm);
382644249214SRandall Stewart 				}
382744249214SRandall Stewart 				TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].uno_inqueue, next_instrm, nctl) {
382844249214SRandall Stewart 					TAILQ_REMOVE(&oldstrm[i].uno_inqueue, ctl, next_instrm);
382944249214SRandall Stewart 					TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].uno_inqueue, ctl, next_instrm);
38308aae9493SRandall Stewart 				}
38318aae9493SRandall Stewart 			}
3832ea44232bSRandall Stewart 			/* Init the new streams */
3833ea44232bSRandall Stewart 			for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
3834ea44232bSRandall Stewart 				TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
383544249214SRandall Stewart 				TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
383649656eefSMichael Tuexen 				stcb->asoc.strmin[i].sid = i;
383749656eefSMichael Tuexen 				stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
383844249214SRandall Stewart 				stcb->asoc.strmin[i].pd_api_started = 0;
3839ea44232bSRandall Stewart 				stcb->asoc.strmin[i].delivery_started = 0;
3840ea44232bSRandall Stewart 			}
3841ea44232bSRandall Stewart 			SCTP_FREE(oldstrm, SCTP_M_STRMI);
3842ea44232bSRandall Stewart 			/* update the size */
3843ea44232bSRandall Stewart 			stcb->asoc.streamincnt = num_stream;
3844f3ebe64cSMichael Tuexen 			stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3845c4e848b7SRandall Stewart 			sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3846ea44232bSRandall Stewart 		}
3847c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3848c4e848b7SRandall Stewart 		asoc->str_reset_seq_in++;
38498aae9493SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 1) == seq) {
38508aae9493SRandall Stewart 		/*
38518aae9493SRandall Stewart 		 * one seq back, just echo back last action since my
38528aae9493SRandall Stewart 		 * response was lost.
38538aae9493SRandall Stewart 		 */
38548aae9493SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
38558aae9493SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 2) == seq) {
38568aae9493SRandall Stewart 		/*
38578aae9493SRandall Stewart 		 * two seq back, just echo back last action since my
38588aae9493SRandall Stewart 		 * response was lost.
38598aae9493SRandall Stewart 		 */
38608aae9493SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
38618aae9493SRandall Stewart 	} else {
3862f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
38638aae9493SRandall Stewart 	}
3864ea44232bSRandall Stewart }
3865ea44232bSRandall Stewart 
3866c4e848b7SRandall Stewart static void
3867c4e848b7SRandall Stewart sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3868c4e848b7SRandall Stewart     struct sctp_stream_reset_add_strm *str_add)
3869c4e848b7SRandall Stewart {
3870c4e848b7SRandall Stewart 	/*
3871c4e848b7SRandall Stewart 	 * Peer is requesting to add more streams. If its within our
3872c4e848b7SRandall Stewart 	 * max-streams we will allow it.
3873c4e848b7SRandall Stewart 	 */
3874c4e848b7SRandall Stewart 	uint16_t num_stream;
3875c4e848b7SRandall Stewart 	uint32_t seq;
3876c4e848b7SRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3877c4e848b7SRandall Stewart 
3878c4e848b7SRandall Stewart 	/* Get the number. */
3879c4e848b7SRandall Stewart 	seq = ntohl(str_add->request_seq);
3880c4e848b7SRandall Stewart 	num_stream = ntohs(str_add->number_of_streams);
3881c4e848b7SRandall Stewart 	/* Now what would be the new total? */
3882c4e848b7SRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3883c4e848b7SRandall Stewart 		stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
38849b2a35b3SMichael Tuexen 		if ((asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ) == 0) {
3885f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3886f3ebe64cSMichael Tuexen 		} else if (stcb->asoc.stream_reset_outstanding) {
3887f3ebe64cSMichael Tuexen 			/* We must reject it we have something pending */
3888f3ebe64cSMichael Tuexen 			stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3889c4e848b7SRandall Stewart 		} else {
3890c4e848b7SRandall Stewart 			/* Ok, we can do that :-) */
3891c4e848b7SRandall Stewart 			int mychk;
3892c4e848b7SRandall Stewart 
3893c4e848b7SRandall Stewart 			mychk = stcb->asoc.streamoutcnt;
3894c4e848b7SRandall Stewart 			mychk += num_stream;
3895c4e848b7SRandall Stewart 			if (mychk < 0x10000) {
3896f3ebe64cSMichael Tuexen 				stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
38977cca1775SRandall Stewart 				if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 1, num_stream, 0, 1)) {
3898f3ebe64cSMichael Tuexen 					stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3899c4e848b7SRandall Stewart 				}
3900c4e848b7SRandall Stewart 			} else {
3901f3ebe64cSMichael Tuexen 				stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3902c4e848b7SRandall Stewart 			}
3903c4e848b7SRandall Stewart 		}
3904c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
3905c4e848b7SRandall Stewart 		asoc->str_reset_seq_in++;
3906c4e848b7SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 1) == seq) {
3907c4e848b7SRandall Stewart 		/*
3908c4e848b7SRandall Stewart 		 * one seq back, just echo back last action since my
3909c4e848b7SRandall Stewart 		 * response was lost.
3910c4e848b7SRandall Stewart 		 */
3911c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3912c4e848b7SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 2) == seq) {
3913c4e848b7SRandall Stewart 		/*
3914c4e848b7SRandall Stewart 		 * two seq back, just echo back last action since my
3915c4e848b7SRandall Stewart 		 * response was lost.
3916c4e848b7SRandall Stewart 		 */
3917c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3918c4e848b7SRandall Stewart 	} else {
3919f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3920c4e848b7SRandall Stewart 	}
3921c4e848b7SRandall Stewart }
3922c4e848b7SRandall Stewart 
3923671d309cSRandall Stewart #ifdef __GNUC__
3924671d309cSRandall Stewart __attribute__((noinline))
3925671d309cSRandall Stewart #endif
3926f8829a4aSRandall Stewart static int
3927671d309cSRandall Stewart sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3928a169d6ecSMichael Tuexen     struct sctp_chunkhdr *ch_req)
3929f8829a4aSRandall Stewart {
39306a58f0e9SXin LI 	uint16_t remaining_length, param_len, ptype;
3931671d309cSRandall Stewart 	struct sctp_paramhdr pstore;
3932671d309cSRandall Stewart 	uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
3933c4e848b7SRandall Stewart 	uint32_t seq = 0;
3934f8829a4aSRandall Stewart 	int num_req = 0;
3935671d309cSRandall Stewart 	int trunc = 0;
3936f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
3937f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
3938f8829a4aSRandall Stewart 	struct sctp_paramhdr *ph;
3939f42a358aSRandall Stewart 	int ret_code = 0;
3940f42a358aSRandall Stewart 	int num_param = 0;
3941f8829a4aSRandall Stewart 
3942f8829a4aSRandall Stewart 	/* now it may be a reset or a reset-response */
39436a58f0e9SXin LI 	remaining_length = ntohs(ch_req->chunk_length) - sizeof(struct sctp_chunkhdr);
3944f8829a4aSRandall Stewart 
3945f8829a4aSRandall Stewart 	/* setup for adding the response */
3946f8829a4aSRandall Stewart 	sctp_alloc_a_chunk(stcb, chk);
3947f8829a4aSRandall Stewart 	if (chk == NULL) {
3948f8829a4aSRandall Stewart 		return (ret_code);
3949f8829a4aSRandall Stewart 	}
3950e03159eaSMichael Tuexen 	chk->copy_by_ref = 0;
3951f8829a4aSRandall Stewart 	chk->rec.chunk_id.id = SCTP_STREAM_RESET;
3952d06c82f1SRandall Stewart 	chk->rec.chunk_id.can_take_data = 0;
3953e03159eaSMichael Tuexen 	chk->flags = 0;
3954f8829a4aSRandall Stewart 	chk->asoc = &stcb->asoc;
3955f8829a4aSRandall Stewart 	chk->no_fr_allowed = 0;
3956f8829a4aSRandall Stewart 	chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
395744b7479bSRandall Stewart 	chk->book_size_scale = 0;
3958eb1b1807SGleb Smirnoff 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
3959f8829a4aSRandall Stewart 	if (chk->data == NULL) {
3960f8829a4aSRandall Stewart strres_nochunk:
3961f8829a4aSRandall Stewart 		if (chk->data) {
3962f8829a4aSRandall Stewart 			sctp_m_freem(chk->data);
3963f8829a4aSRandall Stewart 			chk->data = NULL;
3964f8829a4aSRandall Stewart 		}
3965689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3966f8829a4aSRandall Stewart 		return (ret_code);
3967f8829a4aSRandall Stewart 	}
3968139bc87fSRandall Stewart 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
3969f8829a4aSRandall Stewart 
3970f8829a4aSRandall Stewart 	/* setup chunk parameters */
3971f8829a4aSRandall Stewart 	chk->sent = SCTP_DATAGRAM_UNSENT;
3972f8829a4aSRandall Stewart 	chk->snd_count = 0;
3973ca85e948SMichael Tuexen 	chk->whoTo = NULL;
3974f8829a4aSRandall Stewart 
3975f8829a4aSRandall Stewart 	ch = mtod(chk->data, struct sctp_chunkhdr *);
3976f8829a4aSRandall Stewart 	ch->chunk_type = SCTP_STREAM_RESET;
3977f8829a4aSRandall Stewart 	ch->chunk_flags = 0;
3978f8829a4aSRandall Stewart 	ch->chunk_length = htons(chk->send_size);
3979139bc87fSRandall Stewart 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
3980671d309cSRandall Stewart 	offset += sizeof(struct sctp_chunkhdr);
39816a58f0e9SXin LI 	while (remaining_length >= sizeof(struct sctp_paramhdr)) {
3982671d309cSRandall Stewart 		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *)&pstore);
39836a58f0e9SXin LI 		if (ph == NULL) {
39846a58f0e9SXin LI 			/* TSNH */
3985f8829a4aSRandall Stewart 			break;
3986f8829a4aSRandall Stewart 		}
39876a58f0e9SXin LI 		param_len = ntohs(ph->param_length);
39886a58f0e9SXin LI 		if ((param_len > remaining_length) ||
39896a58f0e9SXin LI 		    (param_len < (sizeof(struct sctp_paramhdr) + sizeof(uint32_t)))) {
39906a58f0e9SXin LI 			/* bad parameter length */
39916a58f0e9SXin LI 			break;
39926a58f0e9SXin LI 		}
39936a58f0e9SXin LI 		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, sizeof(cstore)),
3994671d309cSRandall Stewart 		    (uint8_t *)&cstore);
39956a58f0e9SXin LI 		if (ph == NULL) {
39966a58f0e9SXin LI 			/* TSNH */
39976a58f0e9SXin LI 			break;
39986a58f0e9SXin LI 		}
3999f8829a4aSRandall Stewart 		ptype = ntohs(ph->param_type);
4000f8829a4aSRandall Stewart 		num_param++;
40016a58f0e9SXin LI 		if (param_len > sizeof(cstore)) {
4002671d309cSRandall Stewart 			trunc = 1;
4003671d309cSRandall Stewart 		} else {
4004671d309cSRandall Stewart 			trunc = 0;
4005671d309cSRandall Stewart 		}
4006f8829a4aSRandall Stewart 		if (num_param > SCTP_MAX_RESET_PARAMS) {
4007f8829a4aSRandall Stewart 			/* hit the max of parameters already sorry.. */
4008f8829a4aSRandall Stewart 			break;
4009f8829a4aSRandall Stewart 		}
4010f8829a4aSRandall Stewart 		if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4011f8829a4aSRandall Stewart 			struct sctp_stream_reset_out_request *req_out;
4012f8829a4aSRandall Stewart 
40136a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_out_request)) {
40146a58f0e9SXin LI 				break;
40156a58f0e9SXin LI 			}
4016f8829a4aSRandall Stewart 			req_out = (struct sctp_stream_reset_out_request *)ph;
4017f8829a4aSRandall Stewart 			num_req++;
4018f8829a4aSRandall Stewart 			if (stcb->asoc.stream_reset_outstanding) {
4019f8829a4aSRandall Stewart 				seq = ntohl(req_out->response_seq);
4020f8829a4aSRandall Stewart 				if (seq == stcb->asoc.str_reset_seq_out) {
4021f8829a4aSRandall Stewart 					/* implicit ack */
4022f3ebe64cSMichael Tuexen 					(void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4023f8829a4aSRandall Stewart 				}
4024f8829a4aSRandall Stewart 			}
4025671d309cSRandall Stewart 			sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4026c4e848b7SRandall Stewart 		} else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4027ea44232bSRandall Stewart 			struct sctp_stream_reset_add_strm *str_add;
4028ea44232bSRandall Stewart 
40296a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
40306a58f0e9SXin LI 				break;
40316a58f0e9SXin LI 			}
4032ea44232bSRandall Stewart 			str_add = (struct sctp_stream_reset_add_strm *)ph;
4033ea44232bSRandall Stewart 			num_req++;
4034ea44232bSRandall Stewart 			sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4035c4e848b7SRandall Stewart 		} else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4036c4e848b7SRandall Stewart 			struct sctp_stream_reset_add_strm *str_add;
4037c4e848b7SRandall Stewart 
40386a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
40396a58f0e9SXin LI 				break;
40406a58f0e9SXin LI 			}
4041c4e848b7SRandall Stewart 			str_add = (struct sctp_stream_reset_add_strm *)ph;
4042c4e848b7SRandall Stewart 			num_req++;
4043c4e848b7SRandall Stewart 			sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4044f8829a4aSRandall Stewart 		} else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4045f8829a4aSRandall Stewart 			struct sctp_stream_reset_in_request *req_in;
4046f8829a4aSRandall Stewart 
4047f8829a4aSRandall Stewart 			num_req++;
4048f8829a4aSRandall Stewart 			req_in = (struct sctp_stream_reset_in_request *)ph;
4049671d309cSRandall Stewart 			sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4050f8829a4aSRandall Stewart 		} else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4051f8829a4aSRandall Stewart 			struct sctp_stream_reset_tsn_request *req_tsn;
4052f8829a4aSRandall Stewart 
4053f8829a4aSRandall Stewart 			num_req++;
4054f8829a4aSRandall Stewart 			req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4055f8829a4aSRandall Stewart 			if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4056f8829a4aSRandall Stewart 				ret_code = 1;
4057f8829a4aSRandall Stewart 				goto strres_nochunk;
4058f8829a4aSRandall Stewart 			}
4059f8829a4aSRandall Stewart 			/* no more */
4060f8829a4aSRandall Stewart 			break;
4061f8829a4aSRandall Stewart 		} else if (ptype == SCTP_STR_RESET_RESPONSE) {
4062f8829a4aSRandall Stewart 			struct sctp_stream_reset_response *resp;
4063f8829a4aSRandall Stewart 			uint32_t result;
4064f8829a4aSRandall Stewart 
40656a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_response)) {
40666a58f0e9SXin LI 				break;
40676a58f0e9SXin LI 			}
4068f8829a4aSRandall Stewart 			resp = (struct sctp_stream_reset_response *)ph;
4069f8829a4aSRandall Stewart 			seq = ntohl(resp->response_seq);
4070f8829a4aSRandall Stewart 			result = ntohl(resp->result);
4071f8829a4aSRandall Stewart 			if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4072f8829a4aSRandall Stewart 				ret_code = 1;
4073f8829a4aSRandall Stewart 				goto strres_nochunk;
4074f8829a4aSRandall Stewart 			}
4075f8829a4aSRandall Stewart 		} else {
4076f8829a4aSRandall Stewart 			break;
4077f8829a4aSRandall Stewart 		}
4078671d309cSRandall Stewart 		offset += SCTP_SIZE32(param_len);
40796a58f0e9SXin LI 		if (remaining_length >= SCTP_SIZE32(param_len)) {
40806a58f0e9SXin LI 			remaining_length -= SCTP_SIZE32(param_len);
40816a58f0e9SXin LI 		} else {
40826a58f0e9SXin LI 			remaining_length = 0;
40836a58f0e9SXin LI 		}
4084f8829a4aSRandall Stewart 	}
4085f8829a4aSRandall Stewart 	if (num_req == 0) {
4086f8829a4aSRandall Stewart 		/* we have no response free the stuff */
4087f8829a4aSRandall Stewart 		goto strres_nochunk;
4088f8829a4aSRandall Stewart 	}
4089f8829a4aSRandall Stewart 	/* ok we have a chunk to link in */
4090f8829a4aSRandall Stewart 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4091f8829a4aSRandall Stewart 	    chk,
4092f8829a4aSRandall Stewart 	    sctp_next);
4093f8829a4aSRandall Stewart 	stcb->asoc.ctrl_queue_cnt++;
4094f8829a4aSRandall Stewart 	return (ret_code);
4095f8829a4aSRandall Stewart }
4096f8829a4aSRandall Stewart 
4097f8829a4aSRandall Stewart /*
4098f8829a4aSRandall Stewart  * Handle a router or endpoints report of a packet loss, there are two ways
4099f8829a4aSRandall Stewart  * to handle this, either we get the whole packet and must disect it
4100f8829a4aSRandall Stewart  * ourselves (possibly with truncation and or corruption) or it is a summary
4101e7e65008SMichael Tuexen  * from a middle box that did the disecting for us.
4102f8829a4aSRandall Stewart  */
4103f8829a4aSRandall Stewart static void
4104f8829a4aSRandall Stewart sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4105458303daSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4106f8829a4aSRandall Stewart {
4107f8829a4aSRandall Stewart 	struct sctp_chunk_desc desc;
410832df1c9eSMichael Tuexen 	struct sctp_chunkhdr *chk_hdr;
410932df1c9eSMichael Tuexen 	struct sctp_data_chunk *data_chunk;
411032df1c9eSMichael Tuexen 	struct sctp_idata_chunk *idata_chunk;
411132df1c9eSMichael Tuexen 	uint32_t bottle_bw, on_queue;
411232df1c9eSMichael Tuexen 	uint32_t offset, chk_len;
411332df1c9eSMichael Tuexen 	uint16_t pktdrp_len;
411432df1c9eSMichael Tuexen 	uint8_t pktdrp_flags;
4115f8829a4aSRandall Stewart 
411632df1c9eSMichael Tuexen 	KASSERT(sizeof(struct sctp_pktdrop_chunk) <= limit,
411732df1c9eSMichael Tuexen 	    ("PKTDROP chunk too small"));
411832df1c9eSMichael Tuexen 	pktdrp_flags = cp->ch.chunk_flags;
411932df1c9eSMichael Tuexen 	pktdrp_len = ntohs(cp->ch.chunk_length);
412032df1c9eSMichael Tuexen 	KASSERT(limit <= pktdrp_len, ("Inconsistent limit"));
412132df1c9eSMichael Tuexen 	if (pktdrp_flags & SCTP_PACKET_TRUNCATED) {
41226176f9d6SMichael Tuexen 		if (ntohs(cp->trunc_len) <= pktdrp_len - sizeof(struct sctp_pktdrop_chunk)) {
412332df1c9eSMichael Tuexen 			/* The peer plays games with us. */
412432df1c9eSMichael Tuexen 			return;
412532df1c9eSMichael Tuexen 		}
412632df1c9eSMichael Tuexen 	}
412732df1c9eSMichael Tuexen 	limit -= sizeof(struct sctp_pktdrop_chunk);
412832df1c9eSMichael Tuexen 	offset = 0;
412932df1c9eSMichael Tuexen 	if (offset == limit) {
413032df1c9eSMichael Tuexen 		if (pktdrp_flags & SCTP_FROM_MIDDLE_BOX) {
4131f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_pdrpbwrpt);
413232df1c9eSMichael Tuexen 		}
413332df1c9eSMichael Tuexen 	} else if (offset + sizeof(struct sctphdr) > limit) {
413432df1c9eSMichael Tuexen 		/* Only a partial SCTP common header. */
413532df1c9eSMichael Tuexen 		SCTP_STAT_INCR(sctps_pdrpcrupt);
413632df1c9eSMichael Tuexen 		offset = limit;
4137f8829a4aSRandall Stewart 	} else {
413832df1c9eSMichael Tuexen 		/* XXX: Check embedded SCTP common header. */
413932df1c9eSMichael Tuexen 		offset += sizeof(struct sctphdr);
4140f8829a4aSRandall Stewart 	}
414132df1c9eSMichael Tuexen 	/* Now parse through the chunks themselves. */
414232df1c9eSMichael Tuexen 	while (offset < limit) {
414332df1c9eSMichael Tuexen 		if (offset + sizeof(struct sctp_chunkhdr) > limit) {
414432df1c9eSMichael Tuexen 			SCTP_STAT_INCR(sctps_pdrpcrupt);
414532df1c9eSMichael Tuexen 			break;
4146458303daSRandall Stewart 		}
414732df1c9eSMichael Tuexen 		chk_hdr = (struct sctp_chunkhdr *)(cp->data + offset);
414832df1c9eSMichael Tuexen 		desc.chunk_type = chk_hdr->chunk_type;
4149f8829a4aSRandall Stewart 		/* get amount we need to move */
415032df1c9eSMichael Tuexen 		chk_len = (uint32_t)ntohs(chk_hdr->chunk_length);
415132df1c9eSMichael Tuexen 		if (chk_len < sizeof(struct sctp_chunkhdr)) {
415232df1c9eSMichael Tuexen 			/* Someone is lying... */
4153f8829a4aSRandall Stewart 			break;
4154f8829a4aSRandall Stewart 		}
4155f8829a4aSRandall Stewart 		if (desc.chunk_type == SCTP_DATA) {
415632df1c9eSMichael Tuexen 			if (stcb->asoc.idata_supported) {
415732df1c9eSMichael Tuexen 				/* Some is playing games with us. */
415832df1c9eSMichael Tuexen 				break;
4159f8829a4aSRandall Stewart 			}
416032df1c9eSMichael Tuexen 			if (chk_len <= sizeof(struct sctp_data_chunk)) {
416132df1c9eSMichael Tuexen 				/* Some is playing games with us. */
416232df1c9eSMichael Tuexen 				break;
416332df1c9eSMichael Tuexen 			}
416432df1c9eSMichael Tuexen 			if (chk_len < sizeof(struct sctp_data_chunk) + SCTP_NUM_DB_TO_VERIFY) {
416532df1c9eSMichael Tuexen 				/*
416632df1c9eSMichael Tuexen 				 * Not enough data bytes available in the
416732df1c9eSMichael Tuexen 				 * chunk.
416832df1c9eSMichael Tuexen 				 */
4169f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpnedat);
417032df1c9eSMichael Tuexen 				goto next_chunk;
417132df1c9eSMichael Tuexen 			}
417232df1c9eSMichael Tuexen 			if (offset + sizeof(struct sctp_data_chunk) + SCTP_NUM_DB_TO_VERIFY > limit) {
417332df1c9eSMichael Tuexen 				/* Not enough data in buffer. */
4174f8829a4aSRandall Stewart 				break;
4175f8829a4aSRandall Stewart 			}
417632df1c9eSMichael Tuexen 			data_chunk = (struct sctp_data_chunk *)(cp->data + offset);
417732df1c9eSMichael Tuexen 			memcpy(desc.data_bytes, data_chunk + 1, SCTP_NUM_DB_TO_VERIFY);
417832df1c9eSMichael Tuexen 			desc.tsn_ifany = data_chunk->dp.tsn;
417932df1c9eSMichael Tuexen 			if (pktdrp_flags & SCTP_FROM_MIDDLE_BOX) {
418032df1c9eSMichael Tuexen 				SCTP_STAT_INCR(sctps_pdrpmbda);
418132df1c9eSMichael Tuexen 			}
418232df1c9eSMichael Tuexen 		} else if (desc.chunk_type == SCTP_IDATA) {
418332df1c9eSMichael Tuexen 			if (!stcb->asoc.idata_supported) {
418432df1c9eSMichael Tuexen 				/* Some is playing games with us. */
418532df1c9eSMichael Tuexen 				break;
418632df1c9eSMichael Tuexen 			}
418732df1c9eSMichael Tuexen 			if (chk_len <= sizeof(struct sctp_idata_chunk)) {
418832df1c9eSMichael Tuexen 				/* Some is playing games with us. */
418932df1c9eSMichael Tuexen 				break;
419032df1c9eSMichael Tuexen 			}
419132df1c9eSMichael Tuexen 			if (chk_len < sizeof(struct sctp_idata_chunk) + SCTP_NUM_DB_TO_VERIFY) {
419232df1c9eSMichael Tuexen 				/*
419332df1c9eSMichael Tuexen 				 * Not enough data bytes available in the
419432df1c9eSMichael Tuexen 				 * chunk.
419532df1c9eSMichael Tuexen 				 */
419632df1c9eSMichael Tuexen 				SCTP_STAT_INCR(sctps_pdrpnedat);
419732df1c9eSMichael Tuexen 				goto next_chunk;
419832df1c9eSMichael Tuexen 			}
419932df1c9eSMichael Tuexen 			if (offset + sizeof(struct sctp_idata_chunk) + SCTP_NUM_DB_TO_VERIFY > limit) {
420032df1c9eSMichael Tuexen 				/* Not enough data in buffer. */
420132df1c9eSMichael Tuexen 				break;
420232df1c9eSMichael Tuexen 			}
420332df1c9eSMichael Tuexen 			idata_chunk = (struct sctp_idata_chunk *)(cp->data + offset);
420432df1c9eSMichael Tuexen 			memcpy(desc.data_bytes, idata_chunk + 1, SCTP_NUM_DB_TO_VERIFY);
420532df1c9eSMichael Tuexen 			desc.tsn_ifany = idata_chunk->dp.tsn;
420632df1c9eSMichael Tuexen 			if (pktdrp_flags & SCTP_FROM_MIDDLE_BOX) {
420732df1c9eSMichael Tuexen 				SCTP_STAT_INCR(sctps_pdrpmbda);
420832df1c9eSMichael Tuexen 			}
4209f8829a4aSRandall Stewart 		} else {
421032df1c9eSMichael Tuexen 			if (pktdrp_flags & SCTP_FROM_MIDDLE_BOX) {
4211f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpmbct);
4212f8829a4aSRandall Stewart 			}
421332df1c9eSMichael Tuexen 		}
421432df1c9eSMichael Tuexen 		if (process_chunk_drop(stcb, &desc, net, pktdrp_flags)) {
4215f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_pdrppdbrk);
4216f8829a4aSRandall Stewart 			break;
4217f8829a4aSRandall Stewart 		}
421832df1c9eSMichael Tuexen next_chunk:
421932df1c9eSMichael Tuexen 		offset += SCTP_SIZE32(chk_len);
4220f8829a4aSRandall Stewart 	}
4221f8829a4aSRandall Stewart 	/* Now update any rwnd --- possibly */
422232df1c9eSMichael Tuexen 	if ((pktdrp_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4223f8829a4aSRandall Stewart 		/* From a peer, we get a rwnd report */
4224f8829a4aSRandall Stewart 		uint32_t a_rwnd;
4225f8829a4aSRandall Stewart 
4226f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_pdrpfehos);
4227f8829a4aSRandall Stewart 
4228f8829a4aSRandall Stewart 		bottle_bw = ntohl(cp->bottle_bw);
4229f8829a4aSRandall Stewart 		on_queue = ntohl(cp->current_onq);
4230f8829a4aSRandall Stewart 		if (bottle_bw && on_queue) {
4231f8829a4aSRandall Stewart 			/* a rwnd report is in here */
4232f8829a4aSRandall Stewart 			if (bottle_bw > on_queue)
4233f8829a4aSRandall Stewart 				a_rwnd = bottle_bw - on_queue;
4234f8829a4aSRandall Stewart 			else
4235f8829a4aSRandall Stewart 				a_rwnd = 0;
4236f8829a4aSRandall Stewart 
4237f8829a4aSRandall Stewart 			if (a_rwnd == 0)
4238f8829a4aSRandall Stewart 				stcb->asoc.peers_rwnd = 0;
4239f8829a4aSRandall Stewart 			else {
4240f8829a4aSRandall Stewart 				if (a_rwnd > stcb->asoc.total_flight) {
4241f8829a4aSRandall Stewart 					stcb->asoc.peers_rwnd =
4242f8829a4aSRandall Stewart 					    a_rwnd - stcb->asoc.total_flight;
4243f8829a4aSRandall Stewart 				} else {
4244f8829a4aSRandall Stewart 					stcb->asoc.peers_rwnd = 0;
4245f8829a4aSRandall Stewart 				}
4246f8829a4aSRandall Stewart 				if (stcb->asoc.peers_rwnd <
4247f8829a4aSRandall Stewart 				    stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4248f8829a4aSRandall Stewart 					/* SWS sender side engages */
4249f8829a4aSRandall Stewart 					stcb->asoc.peers_rwnd = 0;
4250f8829a4aSRandall Stewart 				}
4251f8829a4aSRandall Stewart 			}
4252f8829a4aSRandall Stewart 		}
4253f8829a4aSRandall Stewart 	} else {
4254f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_pdrpfmbox);
4255f8829a4aSRandall Stewart 	}
4256f8829a4aSRandall Stewart 
4257f8829a4aSRandall Stewart 	/* now middle boxes in sat networks get a cwnd bump */
425832df1c9eSMichael Tuexen 	if ((pktdrp_flags & SCTP_FROM_MIDDLE_BOX) &&
4259f8829a4aSRandall Stewart 	    (stcb->asoc.sat_t3_loss_recovery == 0) &&
4260f8829a4aSRandall Stewart 	    (stcb->asoc.sat_network)) {
4261f8829a4aSRandall Stewart 		/*
4262cd0a4ff6SPedro F. Giffuni 		 * This is debatable but for sat networks it makes sense
4263f8829a4aSRandall Stewart 		 * Note if a T3 timer has went off, we will prohibit any
4264f8829a4aSRandall Stewart 		 * changes to cwnd until we exit the t3 loss recovery.
4265f8829a4aSRandall Stewart 		 */
4266b54d3a6cSRandall Stewart 		stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4267b54d3a6cSRandall Stewart 		    net, cp, &bottle_bw, &on_queue);
4268f8829a4aSRandall Stewart 	}
4269f8829a4aSRandall Stewart }
4270f8829a4aSRandall Stewart 
4271f8829a4aSRandall Stewart /*
4272f8829a4aSRandall Stewart  * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4273f8829a4aSRandall Stewart  * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4274f8829a4aSRandall Stewart  * offset: offset into the mbuf chain to first chunkhdr - length: is the
4275f8829a4aSRandall Stewart  * length of the complete packet outputs: - length: modified to remaining
4276f8829a4aSRandall Stewart  * length after control processing - netp: modified to new sctp_nets after
4277f8829a4aSRandall Stewart  * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4278f8829a4aSRandall Stewart  * bad packet,...) otherwise return the tcb for this packet
4279f8829a4aSRandall Stewart  */
42803c6f3536SRandall Stewart #ifdef __GNUC__
42813c6f3536SRandall Stewart __attribute__((noinline))
42823c6f3536SRandall Stewart #endif
4283f8829a4aSRandall Stewart static struct sctp_tcb *
4284f8829a4aSRandall Stewart sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4285b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
4286f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
428717205eccSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4288d089f9b9SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4289c54a18d2SRandall Stewart     uint32_t vrf_id, uint16_t port)
4290f8829a4aSRandall Stewart {
4291f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4292ff1ffd74SMichael Tuexen 	struct mbuf *op_err;
4293ff1ffd74SMichael Tuexen 	char msg[SCTP_DIAG_INFO_LEN];
4294f8829a4aSRandall Stewart 	uint32_t vtag_in;
4295f8829a4aSRandall Stewart 	int num_chunks = 0;	/* number of control chunks processed */
4296d0f6ab79SMichael Tuexen 	uint32_t chk_length, contiguous;
4297f8829a4aSRandall Stewart 	int ret;
4298bff64a4dSRandall Stewart 	int abort_no_unlock = 0;
4299899288aeSRandall Stewart 	int ecne_seen = 0;
43009de7354bSMichael Tuexen 	int abort_flag;
4301f8829a4aSRandall Stewart 
4302f8829a4aSRandall Stewart 	/*
4303f8829a4aSRandall Stewart 	 * How big should this be, and should it be alloc'd? Lets try the
4304f8829a4aSRandall Stewart 	 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4305f8829a4aSRandall Stewart 	 * until we get into jumbo grams and such..
4306f8829a4aSRandall Stewart 	 */
4307f42a358aSRandall Stewart 	uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4308f8829a4aSRandall Stewart 	int got_auth = 0;
4309f8829a4aSRandall Stewart 	uint32_t auth_offset = 0, auth_len = 0;
4310f8829a4aSRandall Stewart 	int auth_skipped = 0;
43112afb3e84SRandall Stewart 	int asconf_cnt = 0;
4312ceaad40aSRandall Stewart 
4313ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4314dd294dceSMichael Tuexen 	    iphlen, *offset, length, (void *)stcb);
4315f8829a4aSRandall Stewart 
431680a2d140SMichael Tuexen 	if (stcb) {
431780a2d140SMichael Tuexen 		SCTP_TCB_LOCK_ASSERT(stcb);
431880a2d140SMichael Tuexen 	}
4319f8829a4aSRandall Stewart 	/* validate chunk header length... */
4320f8829a4aSRandall Stewart 	if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4321d61a0ae0SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4322d61a0ae0SRandall Stewart 		    ntohs(ch->chunk_length));
43233ed8d364SMichael Tuexen 		*offset = length;
432480a2d140SMichael Tuexen 		return (stcb);
4325f8829a4aSRandall Stewart 	}
4326f8829a4aSRandall Stewart 	/*
4327f8829a4aSRandall Stewart 	 * validate the verification tag
4328f8829a4aSRandall Stewart 	 */
4329f8829a4aSRandall Stewart 	vtag_in = ntohl(sh->v_tag);
4330f8829a4aSRandall Stewart 
4331f8829a4aSRandall Stewart 	if (ch->chunk_type == SCTP_INITIATION) {
4332d61a0ae0SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4333d61a0ae0SRandall Stewart 		    ntohs(ch->chunk_length), vtag_in);
4334f8829a4aSRandall Stewart 		if (vtag_in != 0) {
4335f8829a4aSRandall Stewart 			/* protocol error- silently discard... */
4336f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_badvtag);
433780a2d140SMichael Tuexen 			if (stcb != NULL) {
433880a2d140SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
43396e55db54SRandall Stewart 			}
4340f8829a4aSRandall Stewart 			return (NULL);
4341f8829a4aSRandall Stewart 		}
4342f8829a4aSRandall Stewart 	} else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4343f8829a4aSRandall Stewart 		/*
4344f8829a4aSRandall Stewart 		 * If there is no stcb, skip the AUTH chunk and process
4345f8829a4aSRandall Stewart 		 * later after a stcb is found (to validate the lookup was
4346f8829a4aSRandall Stewart 		 * valid.
4347f8829a4aSRandall Stewart 		 */
4348f8829a4aSRandall Stewart 		if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4349b3f1ea41SRandall Stewart 		    (stcb == NULL) &&
4350c79bec9cSMichael Tuexen 		    (inp->auth_supported == 1)) {
4351f8829a4aSRandall Stewart 			/* save this chunk for later processing */
4352f8829a4aSRandall Stewart 			auth_skipped = 1;
4353f8829a4aSRandall Stewart 			auth_offset = *offset;
4354f8829a4aSRandall Stewart 			auth_len = ntohs(ch->chunk_length);
4355f8829a4aSRandall Stewart 
4356f8829a4aSRandall Stewart 			/* (temporarily) move past this chunk */
4357f8829a4aSRandall Stewart 			*offset += SCTP_SIZE32(auth_len);
4358f8829a4aSRandall Stewart 			if (*offset >= length) {
4359f8829a4aSRandall Stewart 				/* no more data left in the mbuf chain */
4360f8829a4aSRandall Stewart 				*offset = length;
4361f8829a4aSRandall Stewart 				return (NULL);
4362f8829a4aSRandall Stewart 			}
4363f8829a4aSRandall Stewart 			ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4364f8829a4aSRandall Stewart 			    sizeof(struct sctp_chunkhdr), chunk_buf);
4365f8829a4aSRandall Stewart 		}
4366ad81507eSRandall Stewart 		if (ch == NULL) {
4367ad81507eSRandall Stewart 			/* Help */
4368ad81507eSRandall Stewart 			*offset = length;
436980a2d140SMichael Tuexen 			return (stcb);
4370ad81507eSRandall Stewart 		}
4371f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4372f8829a4aSRandall Stewart 			goto process_control_chunks;
4373f8829a4aSRandall Stewart 		}
4374f8829a4aSRandall Stewart 		/*
4375f8829a4aSRandall Stewart 		 * first check if it's an ASCONF with an unknown src addr we
4376f8829a4aSRandall Stewart 		 * need to look inside to find the association
4377f8829a4aSRandall Stewart 		 */
4378f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
43792afb3e84SRandall Stewart 			struct sctp_chunkhdr *asconf_ch = ch;
43802afb3e84SRandall Stewart 			uint32_t asconf_offset = 0, asconf_len = 0;
43812afb3e84SRandall Stewart 
4382f8829a4aSRandall Stewart 			/* inp's refcount may be reduced */
4383f8829a4aSRandall Stewart 			SCTP_INP_INCR_REF(inp);
4384f8829a4aSRandall Stewart 
43852afb3e84SRandall Stewart 			asconf_offset = *offset;
43862afb3e84SRandall Stewart 			do {
43872afb3e84SRandall Stewart 				asconf_len = ntohs(asconf_ch->chunk_length);
43882afb3e84SRandall Stewart 				if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
43892afb3e84SRandall Stewart 					break;
43907215cc1bSMichael Tuexen 				stcb = sctp_findassociation_ep_asconf(m,
4391b1754ad1SMichael Tuexen 				    *offset,
4392b1754ad1SMichael Tuexen 				    dst,
4393b1754ad1SMichael Tuexen 				    sh, &inp, netp, vrf_id);
43942afb3e84SRandall Stewart 				if (stcb != NULL)
43952afb3e84SRandall Stewart 					break;
43962afb3e84SRandall Stewart 				asconf_offset += SCTP_SIZE32(asconf_len);
43972afb3e84SRandall Stewart 				asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
43982afb3e84SRandall Stewart 				    sizeof(struct sctp_chunkhdr), chunk_buf);
43992afb3e84SRandall Stewart 			} while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4400f8829a4aSRandall Stewart 			if (stcb == NULL) {
4401f8829a4aSRandall Stewart 				/*
4402f8829a4aSRandall Stewart 				 * reduce inp's refcount if not reduced in
4403f8829a4aSRandall Stewart 				 * sctp_findassociation_ep_asconf().
4404f8829a4aSRandall Stewart 				 */
4405f8829a4aSRandall Stewart 				SCTP_INP_DECR_REF(inp);
4406f8829a4aSRandall Stewart 			}
44070053ed28SMichael Tuexen 
4408f8829a4aSRandall Stewart 			/* now go back and verify any auth chunk to be sure */
4409f8829a4aSRandall Stewart 			if (auth_skipped && (stcb != NULL)) {
4410f8829a4aSRandall Stewart 				struct sctp_auth_chunk *auth;
4411f8829a4aSRandall Stewart 
44128262311cSMichael Tuexen 				if (auth_len <= SCTP_CHUNK_BUFFER_SIZE) {
44138262311cSMichael Tuexen 					auth = (struct sctp_auth_chunk *)sctp_m_getptr(m, auth_offset, auth_len, chunk_buf);
4414f8829a4aSRandall Stewart 					got_auth = 1;
4415f8829a4aSRandall Stewart 					auth_skipped = 0;
44168262311cSMichael Tuexen 				} else {
44178262311cSMichael Tuexen 					auth = NULL;
44188262311cSMichael Tuexen 				}
4419ad81507eSRandall Stewart 				if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4420f8829a4aSRandall Stewart 				    auth_offset)) {
4421f8829a4aSRandall Stewart 					/* auth HMAC failed so dump it */
4422f8829a4aSRandall Stewart 					*offset = length;
442380a2d140SMichael Tuexen 					return (stcb);
4424f8829a4aSRandall Stewart 				} else {
4425f8829a4aSRandall Stewart 					/* remaining chunks are HMAC checked */
4426f8829a4aSRandall Stewart 					stcb->asoc.authenticated = 1;
4427f8829a4aSRandall Stewart 				}
4428f8829a4aSRandall Stewart 			}
4429f8829a4aSRandall Stewart 		}
4430f8829a4aSRandall Stewart 		if (stcb == NULL) {
4431999f86d6SMichael Tuexen 			SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4432ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4433ff1ffd74SMichael Tuexen 			    msg);
4434f8829a4aSRandall Stewart 			/* no association, so it's out of the blue... */
4435ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp, op_err,
4436d089f9b9SMichael Tuexen 			    mflowtype, mflowid, inp->fibnum,
4437c54a18d2SRandall Stewart 			    vrf_id, port);
4438f8829a4aSRandall Stewart 			*offset = length;
4439f8829a4aSRandall Stewart 			return (NULL);
4440f8829a4aSRandall Stewart 		}
4441f8829a4aSRandall Stewart 		asoc = &stcb->asoc;
4442f8829a4aSRandall Stewart 		/* ABORT and SHUTDOWN can use either v_tag... */
4443f8829a4aSRandall Stewart 		if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4444f8829a4aSRandall Stewart 		    (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4445f8829a4aSRandall Stewart 		    (ch->chunk_type == SCTP_PACKET_DROPPED)) {
44465db47b3dSMichael Tuexen 			/* Take the T-bit always into account. */
44475db47b3dSMichael Tuexen 			if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) &&
44485db47b3dSMichael Tuexen 			    (vtag_in == asoc->my_vtag)) ||
44495db47b3dSMichael Tuexen 			    (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) &&
4450ff76c8c9SMichael Tuexen 			    (asoc->peer_vtag != htonl(0)) &&
4451f8829a4aSRandall Stewart 			    (vtag_in == asoc->peer_vtag))) {
4452f8829a4aSRandall Stewart 				/* this is valid */
4453f8829a4aSRandall Stewart 			} else {
4454f8829a4aSRandall Stewart 				/* drop this packet... */
4455f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_badvtag);
445680a2d140SMichael Tuexen 				if (stcb != NULL) {
445780a2d140SMichael Tuexen 					SCTP_TCB_UNLOCK(stcb);
44586e55db54SRandall Stewart 				}
4459f8829a4aSRandall Stewart 				return (NULL);
4460f8829a4aSRandall Stewart 			}
4461f8829a4aSRandall Stewart 		} else {
4462f8829a4aSRandall Stewart 			/* for all other chunks, vtag must match */
4463f8829a4aSRandall Stewart 			if (vtag_in != asoc->my_vtag) {
4464f8829a4aSRandall Stewart 				/* invalid vtag... */
4465ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INPUT3,
4466ad81507eSRandall Stewart 				    "invalid vtag: %xh, expect %xh\n",
4467ad81507eSRandall Stewart 				    vtag_in, asoc->my_vtag);
4468f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_badvtag);
446980a2d140SMichael Tuexen 				if (stcb != NULL) {
447080a2d140SMichael Tuexen 					SCTP_TCB_UNLOCK(stcb);
44716e55db54SRandall Stewart 				}
4472f8829a4aSRandall Stewart 				*offset = length;
4473f8829a4aSRandall Stewart 				return (NULL);
4474f8829a4aSRandall Stewart 			}
4475f8829a4aSRandall Stewart 		}
4476b7b84c0eSMichael Tuexen 	}			/* end if !SCTP_COOKIE_ECHO */
4477b7b84c0eSMichael Tuexen 	/*
4478b7b84c0eSMichael Tuexen 	 * process all control chunks...
4479b7b84c0eSMichael Tuexen 	 */
4480f8829a4aSRandall Stewart 	if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4481830d754dSRandall Stewart 	    (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4482f8829a4aSRandall Stewart 	    (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4483839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
4484f8829a4aSRandall Stewart 		/* implied cookie-ack.. we must have lost the ack */
4485f8829a4aSRandall Stewart 		sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4486f8829a4aSRandall Stewart 		    *netp);
4487f8829a4aSRandall Stewart 	}
44880053ed28SMichael Tuexen 
4489f8829a4aSRandall Stewart process_control_chunks:
4490f8829a4aSRandall Stewart 	while (IS_SCTP_CONTROL(ch)) {
4491f8829a4aSRandall Stewart 		/* validate chunk length */
4492f8829a4aSRandall Stewart 		chk_length = ntohs(ch->chunk_length);
4493ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4494f8829a4aSRandall Stewart 		    ch->chunk_type, chk_length);
449580fefe0aSRandall Stewart 		SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
44964c9179adSRandall Stewart 		if (chk_length < sizeof(*ch) ||
44974c9179adSRandall Stewart 		    (*offset + (int)chk_length) > length) {
4498f8829a4aSRandall Stewart 			*offset = length;
449980a2d140SMichael Tuexen 			return (stcb);
4500f8829a4aSRandall Stewart 		}
4501f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4502f8829a4aSRandall Stewart 		/*
4503d0f6ab79SMichael Tuexen 		 * INIT and INIT-ACK only gets the init ack "header" portion
4504d0f6ab79SMichael Tuexen 		 * only because we don't have to process the peer's COOKIE.
4505d0f6ab79SMichael Tuexen 		 * All others get a complete chunk.
4506f8829a4aSRandall Stewart 		 */
4507d0f6ab79SMichael Tuexen 		switch (ch->chunk_type) {
4508d0f6ab79SMichael Tuexen 		case SCTP_INITIATION:
4509d0f6ab79SMichael Tuexen 			contiguous = sizeof(struct sctp_init_chunk);
4510d0f6ab79SMichael Tuexen 			break;
4511d0f6ab79SMichael Tuexen 		case SCTP_INITIATION_ACK:
4512d0f6ab79SMichael Tuexen 			contiguous = sizeof(struct sctp_init_ack_chunk);
4513d0f6ab79SMichael Tuexen 			break;
4514d0f6ab79SMichael Tuexen 		default:
4515d0f6ab79SMichael Tuexen 			contiguous = min(chk_length, sizeof(chunk_buf));
4516d0f6ab79SMichael Tuexen 			break;
45176e55db54SRandall Stewart 		}
4518d06c82f1SRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4519d0f6ab79SMichael Tuexen 		    contiguous,
4520d06c82f1SRandall Stewart 		    chunk_buf);
4521d06c82f1SRandall Stewart 		if (ch == NULL) {
4522d06c82f1SRandall Stewart 			*offset = length;
4523c70d1ef1SMichael Tuexen 			return (stcb);
4524d06c82f1SRandall Stewart 		}
45250053ed28SMichael Tuexen 
4526f8829a4aSRandall Stewart 		num_chunks++;
4527f8829a4aSRandall Stewart 		/* Save off the last place we got a control from */
4528f8829a4aSRandall Stewart 		if (stcb != NULL) {
4529ad81507eSRandall Stewart 			if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4530f8829a4aSRandall Stewart 				/*
4531f8829a4aSRandall Stewart 				 * allow last_control to be NULL if
4532f8829a4aSRandall Stewart 				 * ASCONF... ASCONF processing will find the
4533f8829a4aSRandall Stewart 				 * right net later
4534f8829a4aSRandall Stewart 				 */
4535ad81507eSRandall Stewart 				if ((netp != NULL) && (*netp != NULL))
4536f8829a4aSRandall Stewart 					stcb->asoc.last_control_chunk_from = *netp;
4537f8829a4aSRandall Stewart 			}
4538f8829a4aSRandall Stewart 		}
4539f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
4540f8829a4aSRandall Stewart 		sctp_audit_log(0xB0, ch->chunk_type);
4541f8829a4aSRandall Stewart #endif
4542f8829a4aSRandall Stewart 
4543f8829a4aSRandall Stewart 		/* check to see if this chunk required auth, but isn't */
4544b3f1ea41SRandall Stewart 		if ((stcb != NULL) &&
4545b3f1ea41SRandall Stewart 		    sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4546f8829a4aSRandall Stewart 		    !stcb->asoc.authenticated) {
4547f8829a4aSRandall Stewart 			/* "silently" ignore */
4548f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvauthmissing);
4549f8829a4aSRandall Stewart 			goto next_chunk;
4550f8829a4aSRandall Stewart 		}
4551f8829a4aSRandall Stewart 		switch (ch->chunk_type) {
4552f8829a4aSRandall Stewart 		case SCTP_INITIATION:
4553ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4554d68fdc4dSMichael Tuexen 			/* The INIT chunk must be the only chunk. */
4555d68fdc4dSMichael Tuexen 			if ((num_chunks > 1) ||
4556ab6174d5SMichael Tuexen 			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4557c70d1ef1SMichael Tuexen 				/*
4558c70d1ef1SMichael Tuexen 				 * RFC 4960bis requires stopping the
4559c70d1ef1SMichael Tuexen 				 * processing of the packet.
4560c70d1ef1SMichael Tuexen 				 */
4561f8829a4aSRandall Stewart 				*offset = length;
4562c70d1ef1SMichael Tuexen 				return (stcb);
4563f8829a4aSRandall Stewart 			}
4564d68fdc4dSMichael Tuexen 			/* Honor our resource limit. */
4565d68fdc4dSMichael Tuexen 			if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4566ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4567eba8e643SMichael Tuexen 				sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
4568eba8e643SMichael Tuexen 				    mflowtype, mflowid, inp->fibnum,
4569f30ac432SMichael Tuexen 				    vrf_id, port);
4570f8829a4aSRandall Stewart 				*offset = length;
4571eba8e643SMichael Tuexen 				if (stcb != NULL) {
4572eba8e643SMichael Tuexen 					SCTP_TCB_UNLOCK(stcb);
4573eba8e643SMichael Tuexen 				}
4574f8829a4aSRandall Stewart 				return (NULL);
4575f8829a4aSRandall Stewart 			}
4576b1754ad1SMichael Tuexen 			sctp_handle_init(m, iphlen, *offset, src, dst, sh,
4577ad81507eSRandall Stewart 			    (struct sctp_init_chunk *)ch, inp,
4578eba8e643SMichael Tuexen 			    stcb, *netp,
4579457b4b88SMichael Tuexen 			    mflowtype, mflowid,
4580f30ac432SMichael Tuexen 			    vrf_id, port);
4581f8829a4aSRandall Stewart 			*offset = length;
4582eba8e643SMichael Tuexen 			if (stcb != NULL) {
458380a2d140SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
45846e55db54SRandall Stewart 			}
4585f8829a4aSRandall Stewart 			return (NULL);
4586f8829a4aSRandall Stewart 			break;
45879a972525SRandall Stewart 		case SCTP_PAD_CHUNK:
45889a972525SRandall Stewart 			break;
4589f8829a4aSRandall Stewart 		case SCTP_INITIATION_ACK:
4590469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT_ACK\n");
4591f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4592f8829a4aSRandall Stewart 				/* We are not interested anymore */
459380a2d140SMichael Tuexen 				if ((stcb != NULL) && (stcb->asoc.total_output_queue_size)) {
4594f8829a4aSRandall Stewart 					;
4595f8829a4aSRandall Stewart 				} else {
4596f8829a4aSRandall Stewart 					*offset = length;
459780a2d140SMichael Tuexen 					if (stcb != NULL) {
4598b7d130beSMichael Tuexen 						(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4599b7d130beSMichael Tuexen 						    SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
4600f8829a4aSRandall Stewart 					}
4601f8829a4aSRandall Stewart 					return (NULL);
4602f8829a4aSRandall Stewart 				}
4603f8829a4aSRandall Stewart 			}
4604ab6174d5SMichael Tuexen 			/* The INIT-ACK chunk must be the only chunk. */
4605f8829a4aSRandall Stewart 			if ((num_chunks > 1) ||
4606ab6174d5SMichael Tuexen 			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4607f8829a4aSRandall Stewart 				*offset = length;
460880a2d140SMichael Tuexen 				return (stcb);
4609f8829a4aSRandall Stewart 			}
4610469a65d1SMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
4611b1754ad1SMichael Tuexen 				ret = sctp_handle_init_ack(m, iphlen, *offset,
4612b1754ad1SMichael Tuexen 				    src, dst, sh,
4613f30ac432SMichael Tuexen 				    (struct sctp_init_ack_chunk *)ch,
4614f30ac432SMichael Tuexen 				    stcb, *netp,
4615f30ac432SMichael Tuexen 				    &abort_no_unlock,
4616457b4b88SMichael Tuexen 				    mflowtype, mflowid,
4617f30ac432SMichael Tuexen 				    vrf_id);
4618ad81507eSRandall Stewart 			} else {
4619ad81507eSRandall Stewart 				ret = -1;
4620ad81507eSRandall Stewart 			}
4621d68fdc4dSMichael Tuexen 			*offset = length;
4622d68fdc4dSMichael Tuexen 			if (abort_no_unlock) {
4623d68fdc4dSMichael Tuexen 				return (NULL);
4624d68fdc4dSMichael Tuexen 			}
4625f8829a4aSRandall Stewart 			/*
4626f8829a4aSRandall Stewart 			 * Special case, I must call the output routine to
4627f8829a4aSRandall Stewart 			 * get the cookie echoed
4628f8829a4aSRandall Stewart 			 */
4629d68fdc4dSMichael Tuexen 			if ((stcb != NULL) && (ret == 0)) {
4630ceaad40aSRandall Stewart 				sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4631d68fdc4dSMichael Tuexen 			}
463280a2d140SMichael Tuexen 			return (stcb);
4633f8829a4aSRandall Stewart 			break;
4634f8829a4aSRandall Stewart 		case SCTP_SELECTIVE_ACK:
4635469a65d1SMichael Tuexen 		case SCTP_NR_SELECTIVE_ACK:
4636f8829a4aSRandall Stewart 			{
4637f8829a4aSRandall Stewart 				int abort_now = 0;
4638f8829a4aSRandall Stewart 				uint32_t a_rwnd, cum_ack;
4639469a65d1SMichael Tuexen 				uint16_t num_seg, num_nr_seg, num_dup;
4640cd554309SMichael Tuexen 				uint8_t flags;
4641cd554309SMichael Tuexen 				int offset_seg, offset_dup;
4642f8829a4aSRandall Stewart 
4643469a65d1SMichael Tuexen 				SCTPDBG(SCTP_DEBUG_INPUT3, "%s\n",
4644469a65d1SMichael Tuexen 				    ch->chunk_type == SCTP_SELECTIVE_ACK ? "SCTP_SACK" : "SCTP_NR_SACK");
464520083c2eSMichael Tuexen 				SCTP_STAT_INCR(sctps_recvsacks);
4646cd554309SMichael Tuexen 				if (stcb == NULL) {
4647469a65d1SMichael Tuexen 					SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing %s chunk\n",
4648469a65d1SMichael Tuexen 					    (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK");
4649cd554309SMichael Tuexen 					break;
46506e55db54SRandall Stewart 				}
4651469a65d1SMichael Tuexen 				if (ch->chunk_type == SCTP_SELECTIVE_ACK) {
4652cd554309SMichael Tuexen 					if (chk_length < sizeof(struct sctp_sack_chunk)) {
4653cd554309SMichael Tuexen 						SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4654cd554309SMichael Tuexen 						break;
4655d06c82f1SRandall Stewart 					}
4656469a65d1SMichael Tuexen 				} else {
4657469a65d1SMichael Tuexen 					if (stcb->asoc.nrsack_supported == 0) {
4658469a65d1SMichael Tuexen 						goto unknown_chunk;
4659469a65d1SMichael Tuexen 					}
4660469a65d1SMichael Tuexen 					if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
4661469a65d1SMichael Tuexen 						SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR_SACK chunk, too small\n");
4662469a65d1SMichael Tuexen 						break;
4663469a65d1SMichael Tuexen 					}
4664469a65d1SMichael Tuexen 				}
4665839d21d6SMichael Tuexen 				if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
46662afb3e84SRandall Stewart 					/*-
46672afb3e84SRandall Stewart 					 * If we have sent a shutdown-ack, we will pay no
46682afb3e84SRandall Stewart 					 * attention to a sack sent in to us since
46692afb3e84SRandall Stewart 					 * we don't care anymore.
46702afb3e84SRandall Stewart 					 */
4671a1e13272SRandall Stewart 					break;
46722afb3e84SRandall Stewart 				}
4673cd554309SMichael Tuexen 				flags = ch->chunk_flags;
4674469a65d1SMichael Tuexen 				if (ch->chunk_type == SCTP_SELECTIVE_ACK) {
4675469a65d1SMichael Tuexen 					struct sctp_sack_chunk *sack;
4676469a65d1SMichael Tuexen 
4677469a65d1SMichael Tuexen 					sack = (struct sctp_sack_chunk *)ch;
4678f8829a4aSRandall Stewart 					cum_ack = ntohl(sack->sack.cum_tsn_ack);
4679f8829a4aSRandall Stewart 					num_seg = ntohs(sack->sack.num_gap_ack_blks);
4680469a65d1SMichael Tuexen 					num_nr_seg = 0;
4681cd554309SMichael Tuexen 					num_dup = ntohs(sack->sack.num_dup_tsns);
4682469a65d1SMichael Tuexen 					a_rwnd = ntohl(sack->sack.a_rwnd);
4683cd554309SMichael Tuexen 					if (sizeof(struct sctp_sack_chunk) +
4684cd554309SMichael Tuexen 					    num_seg * sizeof(struct sctp_gap_ack_block) +
4685cd554309SMichael Tuexen 					    num_dup * sizeof(uint32_t) != chk_length) {
4686cd554309SMichael Tuexen 						SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4687cd554309SMichael Tuexen 						break;
4688cd554309SMichael Tuexen 					}
4689cd554309SMichael Tuexen 					offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4690cd554309SMichael Tuexen 					offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4691f8829a4aSRandall Stewart 				} else {
4692830d754dSRandall Stewart 					struct sctp_nr_sack_chunk *nr_sack;
4693830d754dSRandall Stewart 
4694830d754dSRandall Stewart 					nr_sack = (struct sctp_nr_sack_chunk *)ch;
4695830d754dSRandall Stewart 					cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
4696830d754dSRandall Stewart 					num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
4697830d754dSRandall Stewart 					num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4698cd554309SMichael Tuexen 					num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
4699469a65d1SMichael Tuexen 					a_rwnd = ntohl(nr_sack->nr_sack.a_rwnd);
4700cd554309SMichael Tuexen 					if (sizeof(struct sctp_nr_sack_chunk) +
4701cd554309SMichael Tuexen 					    (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
4702cd554309SMichael Tuexen 					    num_dup * sizeof(uint32_t) != chk_length) {
4703cd554309SMichael Tuexen 						SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
4704cd554309SMichael Tuexen 						break;
4705cd554309SMichael Tuexen 					}
4706cd554309SMichael Tuexen 					offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
4707469a65d1SMichael Tuexen 					offset_dup = offset_seg + (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block);
4708469a65d1SMichael Tuexen 				}
4709469a65d1SMichael Tuexen 				SCTPDBG(SCTP_DEBUG_INPUT3, "%s process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4710469a65d1SMichael Tuexen 				    (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK",
4711cd554309SMichael Tuexen 				    cum_ack, num_seg, a_rwnd);
4712830d754dSRandall Stewart 				stcb->asoc.seen_a_sack_this_pkt = 1;
4713830d754dSRandall Stewart 				if ((stcb->asoc.pr_sctp_cnt == 0) &&
4714cd554309SMichael Tuexen 				    (num_seg == 0) && (num_nr_seg == 0) &&
471520b07a4dSMichael Tuexen 				    SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4716830d754dSRandall Stewart 				    (stcb->asoc.saw_sack_with_frags == 0) &&
4717d9c5cfeaSMichael Tuexen 				    (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4718cd554309SMichael Tuexen 				    (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
4719830d754dSRandall Stewart 					/*
4720830d754dSRandall Stewart 					 * We have a SIMPLE sack having no
4721830d754dSRandall Stewart 					 * prior segments and data on sent
4722cd554309SMichael Tuexen 					 * queue to be acked. Use the faster
4723cd554309SMichael Tuexen 					 * path sack processing. We also
4724cd554309SMichael Tuexen 					 * allow window update sacks with no
4725cd554309SMichael Tuexen 					 * missing segments to go this way
4726cd554309SMichael Tuexen 					 * too.
4727830d754dSRandall Stewart 					 */
4728493d8e5aSRandall Stewart 					sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
4729899288aeSRandall Stewart 					    &abort_now, ecne_seen);
4730830d754dSRandall Stewart 				} else {
4731469a65d1SMichael Tuexen 					if ((netp != NULL) && (*netp != NULL)) {
47327215cc1bSMichael Tuexen 						sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4733cd554309SMichael Tuexen 						    num_seg, num_nr_seg, num_dup, &abort_now, flags,
4734899288aeSRandall Stewart 						    cum_ack, a_rwnd, ecne_seen);
4735830d754dSRandall Stewart 					}
4736469a65d1SMichael Tuexen 				}
4737830d754dSRandall Stewart 				if (abort_now) {
4738830d754dSRandall Stewart 					/* ABORT signal from sack processing */
4739830d754dSRandall Stewart 					*offset = length;
4740830d754dSRandall Stewart 					return (NULL);
4741830d754dSRandall Stewart 				}
4742cd554309SMichael Tuexen 				if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4743cd554309SMichael Tuexen 				    TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4744cd554309SMichael Tuexen 				    (stcb->asoc.stream_queue_cnt == 0)) {
4745cd554309SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4746cd554309SMichael Tuexen 				}
4747830d754dSRandall Stewart 				break;
4748469a65d1SMichael Tuexen 			}
4749f8829a4aSRandall Stewart 		case SCTP_HEARTBEAT_REQUEST:
4750ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
4751469a65d1SMichael Tuexen 			if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
4752f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_recvheartbeat);
4753ad81507eSRandall Stewart 				sctp_send_heartbeat_ack(stcb, m, *offset,
4754ad81507eSRandall Stewart 				    chk_length, *netp);
4755ad81507eSRandall Stewart 			}
4756f8829a4aSRandall Stewart 			break;
4757f8829a4aSRandall Stewart 		case SCTP_HEARTBEAT_ACK:
4758469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT_ACK\n");
4759ad81507eSRandall Stewart 			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
4760d06c82f1SRandall Stewart 				/* Its not ours */
47619de7354bSMichael Tuexen 				break;
4762d06c82f1SRandall Stewart 			}
4763f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvheartbeatack);
4764469a65d1SMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
4765f8829a4aSRandall Stewart 				sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
4766f8829a4aSRandall Stewart 				    stcb, *netp);
4767469a65d1SMichael Tuexen 			}
4768f8829a4aSRandall Stewart 			break;
4769f8829a4aSRandall Stewart 		case SCTP_ABORT_ASSOCIATION:
4770207304d4SRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
4771dd294dceSMichael Tuexen 			    (void *)stcb);
4772f8829a4aSRandall Stewart 			*offset = length;
4773701492a5SMichael Tuexen 			if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
4774701492a5SMichael Tuexen 				if (sctp_handle_abort((struct sctp_abort_chunk *)ch, stcb, *netp)) {
4775f8829a4aSRandall Stewart 					return (NULL);
4776701492a5SMichael Tuexen 				} else {
4777701492a5SMichael Tuexen 					return (stcb);
4778701492a5SMichael Tuexen 				}
4779701492a5SMichael Tuexen 			} else {
4780701492a5SMichael Tuexen 				return (NULL);
4781701492a5SMichael Tuexen 			}
4782f8829a4aSRandall Stewart 			break;
4783f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN:
4784207304d4SRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
4785dd294dceSMichael Tuexen 			    (void *)stcb);
4786ad81507eSRandall Stewart 			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
47879de7354bSMichael Tuexen 				break;
4788ad81507eSRandall Stewart 			}
4789469a65d1SMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
47909de7354bSMichael Tuexen 				abort_flag = 0;
4791f8829a4aSRandall Stewart 				sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
4792f8829a4aSRandall Stewart 				    stcb, *netp, &abort_flag);
4793f8829a4aSRandall Stewart 				if (abort_flag) {
4794f8829a4aSRandall Stewart 					*offset = length;
4795f8829a4aSRandall Stewart 					return (NULL);
4796f8829a4aSRandall Stewart 				}
4797f8829a4aSRandall Stewart 			}
4798f8829a4aSRandall Stewart 			break;
4799f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_ACK:
4800469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_ACK, stcb %p\n", (void *)stcb);
4801d1cb8d11SMichael Tuexen 			if ((chk_length == sizeof(struct sctp_shutdown_ack_chunk)) &&
4802d1cb8d11SMichael Tuexen 			    (stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
4803f8829a4aSRandall Stewart 				sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
4804f8829a4aSRandall Stewart 				*offset = length;
4805f8829a4aSRandall Stewart 				return (NULL);
4806d1cb8d11SMichael Tuexen 			}
4807f8829a4aSRandall Stewart 			break;
4808f8829a4aSRandall Stewart 		case SCTP_OPERATION_ERROR:
4809469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP_ERR\n");
4810469a65d1SMichael Tuexen 			if ((stcb != NULL) && (netp != NULL) && (*netp != NULL) &&
48113e87bccdSMichael Tuexen 			    sctp_handle_error(ch, stcb, *netp, contiguous) < 0) {
4812f8829a4aSRandall Stewart 				*offset = length;
4813f8829a4aSRandall Stewart 				return (NULL);
4814f8829a4aSRandall Stewart 			}
4815f8829a4aSRandall Stewart 			break;
4816f8829a4aSRandall Stewart 		case SCTP_COOKIE_ECHO:
4817ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3,
4818469a65d1SMichael Tuexen 			    "SCTP_COOKIE_ECHO, stcb %p\n", (void *)stcb);
4819469a65d1SMichael Tuexen 			if ((stcb != NULL) && (stcb->asoc.total_output_queue_size > 0)) {
4820f8829a4aSRandall Stewart 				;
4821f8829a4aSRandall Stewart 			} else {
4822ad81507eSRandall Stewart 				if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4823f8829a4aSRandall Stewart 					/* We are not interested anymore */
48240c7dc840SRandall Stewart 			abend:
482580a2d140SMichael Tuexen 					if (stcb != NULL) {
482628085b2eSRandall Stewart 						SCTP_TCB_UNLOCK(stcb);
482728085b2eSRandall Stewart 					}
4828f8829a4aSRandall Stewart 					*offset = length;
4829f8829a4aSRandall Stewart 					return (NULL);
4830f8829a4aSRandall Stewart 				}
4831f8829a4aSRandall Stewart 			}
48323017b21bSMichael Tuexen 			/*-
4833f8829a4aSRandall Stewart 			 * First are we accepting? We do this again here
483488a7eb29SRandall Stewart 			 * since it is possible that a previous endpoint WAS
483588a7eb29SRandall Stewart 			 * listening responded to a INIT-ACK and then
4836f8829a4aSRandall Stewart 			 * closed. We opened and bound.. and are now no
4837f8829a4aSRandall Stewart 			 * longer listening.
4838779f106aSGleb Smirnoff 			 *
4839779f106aSGleb Smirnoff 			 * XXXGL: notes on checking listen queue length.
4840779f106aSGleb Smirnoff 			 * 1) SCTP_IS_LISTENING() doesn't necessarily mean
4841779f106aSGleb Smirnoff 			 *    SOLISTENING(), because a listening "UDP type"
4842779f106aSGleb Smirnoff 			 *    socket isn't listening in terms of the socket
4843779f106aSGleb Smirnoff 			 *    layer.  It is a normal data flow socket, that
4844779f106aSGleb Smirnoff 			 *    can fork off new connections.  Thus, we should
4845779f106aSGleb Smirnoff 			 *    look into sol_qlen only in case we are !UDP.
4846779f106aSGleb Smirnoff 			 * 2) Checking sol_qlen in general requires locking
4847779f106aSGleb Smirnoff 			 *    the socket, and this code lacks that.
4848f8829a4aSRandall Stewart 			 */
48495d08768aSMichael Tuexen 			if ((stcb == NULL) &&
48505d08768aSMichael Tuexen 			    (!SCTP_IS_LISTENING(inp) ||
48519b2a35b3SMichael Tuexen 			    (((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) == 0) &&
4852779f106aSGleb Smirnoff 			    inp->sctp_socket->sol_qlen >= inp->sctp_socket->sol_qlimit))) {
4853b201f536SRandall Stewart 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4854b3f1ea41SRandall Stewart 				    (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
4855ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4856b1754ad1SMichael Tuexen 					sctp_abort_association(inp, stcb, m, iphlen,
4857b1754ad1SMichael Tuexen 					    src, dst, sh, op_err,
4858457b4b88SMichael Tuexen 					    mflowtype, mflowid,
4859f30ac432SMichael Tuexen 					    vrf_id, port);
4860f8829a4aSRandall Stewart 				}
4861f8829a4aSRandall Stewart 				*offset = length;
4862f8829a4aSRandall Stewart 				return (NULL);
4863b201f536SRandall Stewart 			} else {
4864f8829a4aSRandall Stewart 				struct mbuf *ret_buf;
4865a5d547adSRandall Stewart 				struct sctp_inpcb *linp;
4866efd5e692SMichael Tuexen 				struct sctp_tmit_chunk *chk;
4867f8829a4aSRandall Stewart 
4868c98bf2a4SMark Johnston 				if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE |
4869c98bf2a4SMark Johnston 				    SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4870c98bf2a4SMark Johnston 					goto abend;
4871c98bf2a4SMark Johnston 				}
4872c98bf2a4SMark Johnston 
4873ad81507eSRandall Stewart 				if (stcb) {
4874a5d547adSRandall Stewart 					linp = NULL;
4875ad81507eSRandall Stewart 				} else {
4876a5d547adSRandall Stewart 					linp = inp;
4877ad81507eSRandall Stewart 				}
4878a5d547adSRandall Stewart 
4879469a65d1SMichael Tuexen 				if (linp != NULL) {
4880a5d547adSRandall Stewart 					SCTP_ASOC_CREATE_LOCK(linp);
4881ad81507eSRandall Stewart 				}
48820053ed28SMichael Tuexen 
4883469a65d1SMichael Tuexen 				if (netp != NULL) {
488480a2d140SMichael Tuexen 					struct sctp_tcb *locked_stcb;
488580a2d140SMichael Tuexen 
488680a2d140SMichael Tuexen 					locked_stcb = stcb;
4887f8829a4aSRandall Stewart 					ret_buf =
4888f8829a4aSRandall Stewart 					    sctp_handle_cookie_echo(m, iphlen,
4889b1754ad1SMichael Tuexen 					    *offset,
4890b1754ad1SMichael Tuexen 					    src, dst,
4891b1754ad1SMichael Tuexen 					    sh,
4892f8829a4aSRandall Stewart 					    (struct sctp_cookie_echo_chunk *)ch,
4893f8829a4aSRandall Stewart 					    &inp, &stcb, netp,
4894f8829a4aSRandall Stewart 					    auth_skipped,
4895f8829a4aSRandall Stewart 					    auth_offset,
4896a5d547adSRandall Stewart 					    auth_len,
489780a2d140SMichael Tuexen 					    &locked_stcb,
4898457b4b88SMichael Tuexen 					    mflowtype,
4899f30ac432SMichael Tuexen 					    mflowid,
4900c54a18d2SRandall Stewart 					    vrf_id,
4901c54a18d2SRandall Stewart 					    port);
490280a2d140SMichael Tuexen 					if ((locked_stcb != NULL) && (locked_stcb != stcb)) {
490380a2d140SMichael Tuexen 						SCTP_TCB_UNLOCK(locked_stcb);
490480a2d140SMichael Tuexen 					}
490580a2d140SMichael Tuexen 					if (stcb != NULL) {
490680a2d140SMichael Tuexen 						SCTP_TCB_LOCK_ASSERT(stcb);
490780a2d140SMichael Tuexen 					}
4908ad81507eSRandall Stewart 				} else {
4909ad81507eSRandall Stewart 					ret_buf = NULL;
4910ad81507eSRandall Stewart 				}
4911469a65d1SMichael Tuexen 				if (linp != NULL) {
4912a5d547adSRandall Stewart 					SCTP_ASOC_CREATE_UNLOCK(linp);
4913ad81507eSRandall Stewart 				}
4914f8829a4aSRandall Stewart 				if (ret_buf == NULL) {
491580a2d140SMichael Tuexen 					if (stcb != NULL) {
491680a2d140SMichael Tuexen 						SCTP_TCB_UNLOCK(stcb);
4917f8829a4aSRandall Stewart 					}
4918ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INPUT3,
4919ad81507eSRandall Stewart 					    "GAK, null buffer\n");
4920f8829a4aSRandall Stewart 					*offset = length;
4921f8829a4aSRandall Stewart 					return (NULL);
4922f8829a4aSRandall Stewart 				}
4923f8829a4aSRandall Stewart 				/* if AUTH skipped, see if it verified... */
4924f8829a4aSRandall Stewart 				if (auth_skipped) {
4925f8829a4aSRandall Stewart 					got_auth = 1;
4926f8829a4aSRandall Stewart 					auth_skipped = 0;
4927f8829a4aSRandall Stewart 				}
4928efd5e692SMichael Tuexen 				/* Restart the timer if we have pending data */
492986fd36c5SMichael Tuexen 				TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
4930efd5e692SMichael Tuexen 					if (chk->whoTo != NULL) {
4931efd5e692SMichael Tuexen 						break;
4932efd5e692SMichael Tuexen 					}
4933efd5e692SMichael Tuexen 				}
4934efd5e692SMichael Tuexen 				if (chk != NULL) {
49354a9ef3f8SMichael Tuexen 					sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
4936f8829a4aSRandall Stewart 				}
4937f8829a4aSRandall Stewart 			}
4938f8829a4aSRandall Stewart 			break;
4939f8829a4aSRandall Stewart 		case SCTP_COOKIE_ACK:
4940469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE_ACK, stcb %p\n", (void *)stcb);
4941ad81507eSRandall Stewart 			if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
49429de7354bSMichael Tuexen 				break;
494317205eccSRandall Stewart 			}
4944f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4945f8829a4aSRandall Stewart 				/* We are not interested anymore */
4946f8829a4aSRandall Stewart 				if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4947f8829a4aSRandall Stewart 					;
4948ad81507eSRandall Stewart 				} else if (stcb) {
4949b7d130beSMichael Tuexen 					(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4950b7d130beSMichael Tuexen 					    SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
4951f8829a4aSRandall Stewart 					*offset = length;
4952f8829a4aSRandall Stewart 					return (NULL);
4953f8829a4aSRandall Stewart 				}
4954f8829a4aSRandall Stewart 			}
4955469a65d1SMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
4956f8829a4aSRandall Stewart 				sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
49576e55db54SRandall Stewart 			}
4958f8829a4aSRandall Stewart 			break;
4959f8829a4aSRandall Stewart 		case SCTP_ECN_ECHO:
4960469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_ECHO\n");
49619de7354bSMichael Tuexen 			if (stcb == NULL) {
49629de7354bSMichael Tuexen 				break;
4963d06c82f1SRandall Stewart 			}
4964c79bec9cSMichael Tuexen 			if (stcb->asoc.ecn_supported == 0) {
4965c79bec9cSMichael Tuexen 				goto unknown_chunk;
4966c79bec9cSMichael Tuexen 			}
49676587a2bdSMichael Tuexen 			if ((chk_length != sizeof(struct sctp_ecne_chunk)) &&
49686587a2bdSMichael Tuexen 			    (chk_length != sizeof(struct old_sctp_ecne_chunk))) {
49699de7354bSMichael Tuexen 				break;
49709de7354bSMichael Tuexen 			}
4971469a65d1SMichael Tuexen 			sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, stcb);
4972899288aeSRandall Stewart 			ecne_seen = 1;
4973f8829a4aSRandall Stewart 			break;
4974f8829a4aSRandall Stewart 		case SCTP_ECN_CWR:
4975469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_CWR\n");
49769de7354bSMichael Tuexen 			if (stcb == NULL) {
49779de7354bSMichael Tuexen 				break;
4978d06c82f1SRandall Stewart 			}
4979c79bec9cSMichael Tuexen 			if (stcb->asoc.ecn_supported == 0) {
4980c79bec9cSMichael Tuexen 				goto unknown_chunk;
4981c79bec9cSMichael Tuexen 			}
49829de7354bSMichael Tuexen 			if (chk_length != sizeof(struct sctp_cwr_chunk)) {
49839de7354bSMichael Tuexen 				break;
49849de7354bSMichael Tuexen 			}
4985a21779f0SRandall Stewart 			sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
4986f8829a4aSRandall Stewart 			break;
4987f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_COMPLETE:
4988469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_COMPLETE, stcb %p\n", (void *)stcb);
4989f8829a4aSRandall Stewart 			/* must be first and only chunk */
4990f8829a4aSRandall Stewart 			if ((num_chunks > 1) ||
49914c9179adSRandall Stewart 			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4992f8829a4aSRandall Stewart 				*offset = length;
499380a2d140SMichael Tuexen 				return (stcb);
4994f8829a4aSRandall Stewart 			}
4995d1cb8d11SMichael Tuexen 			if ((chk_length == sizeof(struct sctp_shutdown_complete_chunk)) &&
4996d1cb8d11SMichael Tuexen 			    (stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
4997f8829a4aSRandall Stewart 				sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
4998f8829a4aSRandall Stewart 				    stcb, *netp);
4999f8829a4aSRandall Stewart 				*offset = length;
5000f8829a4aSRandall Stewart 				return (NULL);
5001d1cb8d11SMichael Tuexen 			}
5002f8829a4aSRandall Stewart 			break;
5003f8829a4aSRandall Stewart 		case SCTP_ASCONF:
5004ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5005469a65d1SMichael Tuexen 			if (stcb != NULL) {
5006c79bec9cSMichael Tuexen 				if (stcb->asoc.asconf_supported == 0) {
5007c79bec9cSMichael Tuexen 					goto unknown_chunk;
5008c79bec9cSMichael Tuexen 				}
5009b1754ad1SMichael Tuexen 				sctp_handle_asconf(m, *offset, src,
50102afb3e84SRandall Stewart 				    (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
50112afb3e84SRandall Stewart 				asconf_cnt++;
50126e55db54SRandall Stewart 			}
5013f8829a4aSRandall Stewart 			break;
5014f8829a4aSRandall Stewart 		case SCTP_ASCONF_ACK:
5015469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF_ACK\n");
50169de7354bSMichael Tuexen 			if (stcb == NULL) {
50179de7354bSMichael Tuexen 				break;
5018d06c82f1SRandall Stewart 			}
5019c79bec9cSMichael Tuexen 			if (stcb->asoc.asconf_supported == 0) {
5020c79bec9cSMichael Tuexen 				goto unknown_chunk;
5021c79bec9cSMichael Tuexen 			}
50229de7354bSMichael Tuexen 			if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
50239de7354bSMichael Tuexen 				break;
50249de7354bSMichael Tuexen 			}
50259de7354bSMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
5026f8829a4aSRandall Stewart 				/* He's alive so give him credit */
5027b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5028c4739e2fSRandall Stewart 					sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5029c4739e2fSRandall Stewart 					    stcb->asoc.overall_error_count,
5030c4739e2fSRandall Stewart 					    0,
5031c4739e2fSRandall Stewart 					    SCTP_FROM_SCTP_INPUT,
5032c4739e2fSRandall Stewart 					    __LINE__);
5033c4739e2fSRandall Stewart 				}
5034f8829a4aSRandall Stewart 				stcb->asoc.overall_error_count = 0;
5035f8829a4aSRandall Stewart 				sctp_handle_asconf_ack(m, *offset,
50363232788eSRandall Stewart 				    (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
50373232788eSRandall Stewart 				if (abort_no_unlock)
50383232788eSRandall Stewart 					return (NULL);
50396e55db54SRandall Stewart 			}
5040f8829a4aSRandall Stewart 			break;
5041f8829a4aSRandall Stewart 		case SCTP_FORWARD_CUM_TSN:
504244249214SRandall Stewart 		case SCTP_IFORWARD_CUM_TSN:
5043c96d7c37SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "%s\n",
5044c96d7c37SMichael Tuexen 			    ch->chunk_type == SCTP_FORWARD_CUM_TSN ? "FORWARD_TSN" : "I_FORWARD_TSN");
50459de7354bSMichael Tuexen 			if (stcb == NULL) {
50469de7354bSMichael Tuexen 				break;
5047d06c82f1SRandall Stewart 			}
5048c79bec9cSMichael Tuexen 			if (stcb->asoc.prsctp_supported == 0) {
5049c79bec9cSMichael Tuexen 				goto unknown_chunk;
5050c79bec9cSMichael Tuexen 			}
50519de7354bSMichael Tuexen 			if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
50529de7354bSMichael Tuexen 				break;
50539de7354bSMichael Tuexen 			}
5054fcbfdc0aSMichael Tuexen 			if (((stcb->asoc.idata_supported == 1) && (ch->chunk_type == SCTP_FORWARD_CUM_TSN)) ||
5055fcbfdc0aSMichael Tuexen 			    ((stcb->asoc.idata_supported == 0) && (ch->chunk_type == SCTP_IFORWARD_CUM_TSN))) {
5056c96d7c37SMichael Tuexen 				if (ch->chunk_type == SCTP_FORWARD_CUM_TSN) {
5057c96d7c37SMichael Tuexen 					SCTP_SNPRINTF(msg, sizeof(msg), "%s", "FORWARD-TSN chunk received when I-FORWARD-TSN was negotiated");
5058c96d7c37SMichael Tuexen 				} else {
5059c96d7c37SMichael Tuexen 					SCTP_SNPRINTF(msg, sizeof(msg), "%s", "I-FORWARD-TSN chunk received when FORWARD-TSN was negotiated");
5060c96d7c37SMichael Tuexen 				}
5061c96d7c37SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
5062105b68b4SMichael Tuexen 				sctp_abort_an_association(inp, stcb, op_err, false, SCTP_SO_NOT_LOCKED);
5063c96d7c37SMichael Tuexen 				*offset = length;
5064c96d7c37SMichael Tuexen 				return (NULL);
5065c96d7c37SMichael Tuexen 			}
5066f8829a4aSRandall Stewart 			*fwd_tsn_seen = 1;
5067f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5068f8829a4aSRandall Stewart 				/* We are not interested anymore */
5069b7d130beSMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5070b7d130beSMichael Tuexen 				    SCTP_FROM_SCTP_INPUT + SCTP_LOC_31);
5071f8829a4aSRandall Stewart 				*offset = length;
5072f8829a4aSRandall Stewart 				return (NULL);
5073f8829a4aSRandall Stewart 			}
507491843cf3SMichael Tuexen 			/*
50759de7354bSMichael Tuexen 			 * For sending a SACK this looks like DATA chunks.
507691843cf3SMichael Tuexen 			 */
507791843cf3SMichael Tuexen 			stcb->asoc.last_data_chunk_from = stcb->asoc.last_control_chunk_from;
50789de7354bSMichael Tuexen 			abort_flag = 0;
5079f8829a4aSRandall Stewart 			sctp_handle_forward_tsn(stcb,
5080671d309cSRandall Stewart 			    (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5081f8829a4aSRandall Stewart 			if (abort_flag) {
5082f8829a4aSRandall Stewart 				*offset = length;
5083f8829a4aSRandall Stewart 				return (NULL);
5084c4739e2fSRandall Stewart 			}
5085f8829a4aSRandall Stewart 			break;
5086f8829a4aSRandall Stewart 		case SCTP_STREAM_RESET:
5087ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
50889de7354bSMichael Tuexen 			if (stcb == NULL) {
50899de7354bSMichael Tuexen 				break;
5090d06c82f1SRandall Stewart 			}
5091317e00efSMichael Tuexen 			if (stcb->asoc.reconfig_supported == 0) {
5092c79bec9cSMichael Tuexen 				goto unknown_chunk;
5093f8829a4aSRandall Stewart 			}
50949de7354bSMichael Tuexen 			if (chk_length < sizeof(struct sctp_stream_reset_tsn_req)) {
50959de7354bSMichael Tuexen 				break;
50969de7354bSMichael Tuexen 			}
5097a169d6ecSMichael Tuexen 			if (sctp_handle_stream_reset(stcb, m, *offset, ch)) {
5098f8829a4aSRandall Stewart 				/* stop processing */
5099f8829a4aSRandall Stewart 				*offset = length;
5100f8829a4aSRandall Stewart 				return (NULL);
5101f8829a4aSRandall Stewart 			}
5102f8829a4aSRandall Stewart 			break;
5103f8829a4aSRandall Stewart 		case SCTP_PACKET_DROPPED:
5104ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
51059de7354bSMichael Tuexen 			if (stcb == NULL) {
51069de7354bSMichael Tuexen 				break;
5107d06c82f1SRandall Stewart 			}
5108c79bec9cSMichael Tuexen 			if (stcb->asoc.pktdrop_supported == 0) {
5109c79bec9cSMichael Tuexen 				goto unknown_chunk;
5110c79bec9cSMichael Tuexen 			}
51119de7354bSMichael Tuexen 			if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
51129de7354bSMichael Tuexen 				break;
51139de7354bSMichael Tuexen 			}
51149de7354bSMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
5115f8829a4aSRandall Stewart 				sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5116458303daSRandall Stewart 				    stcb, *netp,
5117d0f6ab79SMichael Tuexen 				    min(chk_length, contiguous));
51186e55db54SRandall Stewart 			}
5119f8829a4aSRandall Stewart 			break;
5120f8829a4aSRandall Stewart 		case SCTP_AUTHENTICATION:
5121ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5122f8829a4aSRandall Stewart 			if (stcb == NULL) {
5123f8829a4aSRandall Stewart 				/* save the first AUTH for later processing */
5124f8829a4aSRandall Stewart 				if (auth_skipped == 0) {
5125f8829a4aSRandall Stewart 					auth_offset = *offset;
5126f8829a4aSRandall Stewart 					auth_len = chk_length;
5127f8829a4aSRandall Stewart 					auth_skipped = 1;
5128f8829a4aSRandall Stewart 				}
5129f8829a4aSRandall Stewart 				/* skip this chunk (temporarily) */
51309de7354bSMichael Tuexen 				break;
5131f8829a4aSRandall Stewart 			}
5132c79bec9cSMichael Tuexen 			if (stcb->asoc.auth_supported == 0) {
5133c79bec9cSMichael Tuexen 				goto unknown_chunk;
5134c79bec9cSMichael Tuexen 			}
5135d06c82f1SRandall Stewart 			if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5136ad81507eSRandall Stewart 			    (chk_length > (sizeof(struct sctp_auth_chunk) +
5137ad81507eSRandall Stewart 			    SCTP_AUTH_DIGEST_LEN_MAX))) {
5138d06c82f1SRandall Stewart 				/* Its not ours */
5139d06c82f1SRandall Stewart 				*offset = length;
514080a2d140SMichael Tuexen 				return (stcb);
5141d06c82f1SRandall Stewart 			}
5142f8829a4aSRandall Stewart 			if (got_auth == 1) {
5143f8829a4aSRandall Stewart 				/* skip this chunk... it's already auth'd */
51449de7354bSMichael Tuexen 				break;
5145f8829a4aSRandall Stewart 			}
5146f8829a4aSRandall Stewart 			got_auth = 1;
5147f2f66ef6SMichael Tuexen 			if (sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, m, *offset)) {
5148f8829a4aSRandall Stewart 				/* auth HMAC failed so dump the packet */
5149f8829a4aSRandall Stewart 				*offset = length;
5150f8829a4aSRandall Stewart 				return (stcb);
5151f8829a4aSRandall Stewart 			} else {
5152f8829a4aSRandall Stewart 				/* remaining chunks are HMAC checked */
5153f8829a4aSRandall Stewart 				stcb->asoc.authenticated = 1;
5154f8829a4aSRandall Stewart 			}
5155f8829a4aSRandall Stewart 			break;
5156f8829a4aSRandall Stewart 
5157f8829a4aSRandall Stewart 		default:
5158f8829a4aSRandall Stewart 	unknown_chunk:
5159f8829a4aSRandall Stewart 			/* it's an unknown chunk! */
5160e99ce3eaSMichael Tuexen 			if ((ch->chunk_type & 0x40) &&
5161e99ce3eaSMichael Tuexen 			    (stcb != NULL) &&
5162e99ce3eaSMichael Tuexen 			    (SCTP_GET_STATE(stcb) != SCTP_STATE_EMPTY) &&
5163e99ce3eaSMichael Tuexen 			    (SCTP_GET_STATE(stcb) != SCTP_STATE_INUSE) &&
5164e99ce3eaSMichael Tuexen 			    (SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT)) {
516586eda749SMichael Tuexen 				struct sctp_gen_error_cause *cause;
516639cbb549SMichael Tuexen 				int len;
5167f8829a4aSRandall Stewart 
516886eda749SMichael Tuexen 				op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_gen_error_cause),
5169eb1b1807SGleb Smirnoff 				    0, M_NOWAIT, 1, MT_DATA);
517086eda749SMichael Tuexen 				if (op_err != NULL) {
517139cbb549SMichael Tuexen 					len = min(SCTP_SIZE32(chk_length), (uint32_t)(length - *offset));
517286eda749SMichael Tuexen 					cause = mtod(op_err, struct sctp_gen_error_cause *);
517386eda749SMichael Tuexen 					cause->code = htons(SCTP_CAUSE_UNRECOG_CHUNK);
51749a8e3088SMichael Tuexen 					cause->length = htons((uint16_t)(len + sizeof(struct sctp_gen_error_cause)));
517586eda749SMichael Tuexen 					SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
517686eda749SMichael Tuexen 					SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(m, *offset, len, M_NOWAIT);
517786eda749SMichael Tuexen 					if (SCTP_BUF_NEXT(op_err) != NULL) {
5178eadccaccSRandall Stewart #ifdef SCTP_MBUF_LOGGING
5179b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
518030811e70SMichael Tuexen 							sctp_log_mbc(SCTP_BUF_NEXT(op_err), SCTP_MBUF_ICOPY);
5181eadccaccSRandall Stewart 						}
5182eadccaccSRandall Stewart #endif
518386eda749SMichael Tuexen 						sctp_queue_op_err(stcb, op_err);
5184f8829a4aSRandall Stewart 					} else {
518586eda749SMichael Tuexen 						sctp_m_freem(op_err);
5186f8829a4aSRandall Stewart 					}
5187f8829a4aSRandall Stewart 				}
5188f8829a4aSRandall Stewart 			}
5189f8829a4aSRandall Stewart 			if ((ch->chunk_type & 0x80) == 0) {
5190f8829a4aSRandall Stewart 				/* discard this packet */
5191f8829a4aSRandall Stewart 				*offset = length;
5192f8829a4aSRandall Stewart 				return (stcb);
5193b7b84c0eSMichael Tuexen 			}	/* else skip this bad chunk and continue... */
5194b7b84c0eSMichael Tuexen 			break;
5195f8829a4aSRandall Stewart 		}		/* switch (ch->chunk_type) */
5196f8829a4aSRandall Stewart 
5197f8829a4aSRandall Stewart next_chunk:
5198f8829a4aSRandall Stewart 		/* get the next chunk */
5199f8829a4aSRandall Stewart 		*offset += SCTP_SIZE32(chk_length);
5200f8829a4aSRandall Stewart 		if (*offset >= length) {
5201f8829a4aSRandall Stewart 			/* no more data left in the mbuf chain */
5202f8829a4aSRandall Stewart 			break;
5203f8829a4aSRandall Stewart 		}
5204f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5205f8829a4aSRandall Stewart 		    sizeof(struct sctp_chunkhdr), chunk_buf);
5206f8829a4aSRandall Stewart 		if (ch == NULL) {
5207f8829a4aSRandall Stewart 			*offset = length;
520880a2d140SMichael Tuexen 			return (stcb);
5209f8829a4aSRandall Stewart 		}
5210f8829a4aSRandall Stewart 	}			/* while */
52112afb3e84SRandall Stewart 
5212469a65d1SMichael Tuexen 	if ((asconf_cnt > 0) && (stcb != NULL)) {
52132afb3e84SRandall Stewart 		sctp_send_asconf_ack(stcb);
52142afb3e84SRandall Stewart 	}
5215f8829a4aSRandall Stewart 	return (stcb);
5216f8829a4aSRandall Stewart }
5217f8829a4aSRandall Stewart 
5218f8829a4aSRandall Stewart /*
5219f8829a4aSRandall Stewart  * common input chunk processing (v4 and v6)
5220f8829a4aSRandall Stewart  */
52216e55db54SRandall Stewart void
5222b1754ad1SMichael Tuexen sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
5223b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
5224b1754ad1SMichael Tuexen     struct sctphdr *sh, struct sctp_chunkhdr *ch,
5225a8775ad9SMichael Tuexen     uint8_t compute_crc,
5226a8775ad9SMichael Tuexen     uint8_t ecn_bits,
5227d089f9b9SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
5228f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
5229f8829a4aSRandall Stewart {
5230ff1ffd74SMichael Tuexen 	char msg[SCTP_DIAG_INFO_LEN];
52314a2b92d9SMichael Tuexen 	struct mbuf *m = *mm, *op_err;
523255b175e7SMichael Tuexen 	struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
5233a8775ad9SMichael Tuexen 	struct sctp_tcb *stcb = NULL;
5234e3d6ef0bSMichael Tuexen 	struct sctp_nets *net = NULL;
52354a2b92d9SMichael Tuexen 	uint32_t high_tsn;
52364a2b92d9SMichael Tuexen 	uint32_t cksum_in_hdr;
52374a2b92d9SMichael Tuexen 	int un_sent;
52384a2b92d9SMichael Tuexen 	int cnt_ctrl_ready = 0;
52394a2b92d9SMichael Tuexen 	int fwd_tsn_seen = 0, data_processed = 0;
52404a2b92d9SMichael Tuexen 	bool cksum_validated, stcb_looked_up;
5241f8829a4aSRandall Stewart 
5242f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvdatagrams);
5243f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
5244f8829a4aSRandall Stewart 	sctp_audit_log(0xE0, 1);
5245f8829a4aSRandall Stewart 	sctp_auditing(0, inp, stcb, net);
5246f8829a4aSRandall Stewart #endif
5247f8829a4aSRandall Stewart 
52484a2b92d9SMichael Tuexen 	stcb_looked_up = false;
52494a2b92d9SMichael Tuexen 	if (compute_crc != 0) {
52504a2b92d9SMichael Tuexen 		cksum_validated = false;
52514a2b92d9SMichael Tuexen 		cksum_in_hdr = sh->checksum;
52524a2b92d9SMichael Tuexen 		if (cksum_in_hdr != htonl(0)) {
52534a2b92d9SMichael Tuexen 			uint32_t cksum_calculated;
52544a2b92d9SMichael Tuexen 
52554a2b92d9SMichael Tuexen 	validate_cksum:
5256a8775ad9SMichael Tuexen 			sh->checksum = 0;
52574a2b92d9SMichael Tuexen 			cksum_calculated = sctp_calculate_cksum(m, iphlen);
52584a2b92d9SMichael Tuexen 			sh->checksum = cksum_in_hdr;
52594a2b92d9SMichael Tuexen 			if (cksum_calculated != cksum_in_hdr) {
52604a2b92d9SMichael Tuexen 				if (stcb_looked_up) {
52614a2b92d9SMichael Tuexen 					/*
52624a2b92d9SMichael Tuexen 					 * The packet has a zero checksum,
52634a2b92d9SMichael Tuexen 					 * which is not the correct CRC, no
52644a2b92d9SMichael Tuexen 					 * stcb has been found or an stcb
52654a2b92d9SMichael Tuexen 					 * has been found but an incorrect
52664a2b92d9SMichael Tuexen 					 * zero checksum is not acceptable.
52674a2b92d9SMichael Tuexen 					 */
52684a2b92d9SMichael Tuexen 					KASSERT(cksum_in_hdr == htonl(0),
52694a2b92d9SMichael Tuexen 					    ("cksum in header not zero: %x",
52704a2b92d9SMichael Tuexen 					    ntohl(cksum_in_hdr)));
52714a2b92d9SMichael Tuexen 					if ((inp == NULL) &&
52724a2b92d9SMichael Tuexen 					    (SCTP_BASE_SYSCTL(sctp_ootb_with_zero_cksum) == 1)) {
52734a2b92d9SMichael Tuexen 						/*
52744a2b92d9SMichael Tuexen 						 * This is an OOTB packet,
52754a2b92d9SMichael Tuexen 						 * depending on the sysctl
52764a2b92d9SMichael Tuexen 						 * variable, pretend that
52774a2b92d9SMichael Tuexen 						 * the checksum is
52784a2b92d9SMichael Tuexen 						 * acceptable, to allow an
52794a2b92d9SMichael Tuexen 						 * appropriate response
52804a2b92d9SMichael Tuexen 						 * (ABORT, for examlpe) to
52814a2b92d9SMichael Tuexen 						 * be sent.
52824a2b92d9SMichael Tuexen 						 */
52834a2b92d9SMichael Tuexen 						KASSERT(stcb == NULL,
52844a2b92d9SMichael Tuexen 						    ("stcb is %p", stcb));
52854a2b92d9SMichael Tuexen 						SCTP_STAT_INCR(sctps_recvzerocrc);
52864a2b92d9SMichael Tuexen 						goto cksum_validated;
52874a2b92d9SMichael Tuexen 					}
52884a2b92d9SMichael Tuexen 				} else {
5289a8775ad9SMichael Tuexen 					stcb = sctp_findassociation_addr(m, offset, src, dst,
5290a8775ad9SMichael Tuexen 					    sh, ch, &inp, &net, vrf_id);
52914a2b92d9SMichael Tuexen 				}
52924a2b92d9SMichael Tuexen 				SCTPDBG(SCTP_DEBUG_INPUT1, "Bad cksum in SCTP packet:%x calculated:%x m:%p mlen:%d iphlen:%d\n",
52934a2b92d9SMichael Tuexen 				    ntohl(cksum_in_hdr), ntohl(cksum_calculated), (void *)m, length, iphlen);
52944474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
52953cf729a9SMichael Tuexen 				if ((ch->chunk_type != SCTP_INITIATION) &&
52963cf729a9SMichael Tuexen 				    (net != NULL) && (net->port != port)) {
5297a8775ad9SMichael Tuexen 					if (net->port == 0) {
52984a2b92d9SMichael Tuexen 						/*
52994a2b92d9SMichael Tuexen 						 * UDP encapsulation turned
53004a2b92d9SMichael Tuexen 						 * on.
53014a2b92d9SMichael Tuexen 						 */
53023cf729a9SMichael Tuexen 						net->mtu -= sizeof(struct udphdr);
53033cf729a9SMichael Tuexen 						if (stcb->asoc.smallest_mtu > net->mtu) {
53042de2ae33SMichael Tuexen 							sctp_pathmtu_adjustment(stcb, net->mtu, true);
53053cf729a9SMichael Tuexen 						}
53063cf729a9SMichael Tuexen 					} else if (port == 0) {
53074a2b92d9SMichael Tuexen 						/*
53084a2b92d9SMichael Tuexen 						 * UDP encapsulation turned
53094a2b92d9SMichael Tuexen 						 * off.
53104a2b92d9SMichael Tuexen 						 */
53113cf729a9SMichael Tuexen 						net->mtu += sizeof(struct udphdr);
53123cf729a9SMichael Tuexen 						/* XXX Update smallest_mtu */
5313a8775ad9SMichael Tuexen 					}
5314a8775ad9SMichael Tuexen 					net->port = port;
5315a8775ad9SMichael Tuexen 				}
53164474d71aSMichael Tuexen #endif
53175fe29cdfSMichael Tuexen 				if (net != NULL) {
5318457b4b88SMichael Tuexen 					net->flowtype = mflowtype;
53195fe29cdfSMichael Tuexen 					net->flowid = mflowid;
5320a8775ad9SMichael Tuexen 				}
53211e88cc8bSMichael Tuexen 				SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5322a8775ad9SMichael Tuexen 				if ((inp != NULL) && (stcb != NULL)) {
53234a2b92d9SMichael Tuexen 					if (stcb->asoc.pktdrop_supported) {
5324a8775ad9SMichael Tuexen 						sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
5325a8775ad9SMichael Tuexen 						sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
53264a2b92d9SMichael Tuexen 					}
5327a8775ad9SMichael Tuexen 				} else if ((inp != NULL) && (stcb == NULL)) {
5328a8775ad9SMichael Tuexen 					inp_decr = inp;
5329a8775ad9SMichael Tuexen 				}
5330a8775ad9SMichael Tuexen 				SCTP_STAT_INCR(sctps_badsum);
5331a8775ad9SMichael Tuexen 				SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5332a8775ad9SMichael Tuexen 				goto out;
53334a2b92d9SMichael Tuexen 			} else {
53344a2b92d9SMichael Tuexen 				cksum_validated = true;
5335a8775ad9SMichael Tuexen 			}
5336a8775ad9SMichael Tuexen 		}
53374a2b92d9SMichael Tuexen 		KASSERT(cksum_validated || cksum_in_hdr == htonl(0),
53384a2b92d9SMichael Tuexen 		    ("cksum 0x%08x not zero and not validated", ntohl(cksum_in_hdr)));
53394a2b92d9SMichael Tuexen 		if (!cksum_validated) {
5340a8775ad9SMichael Tuexen 			stcb = sctp_findassociation_addr(m, offset, src, dst,
5341a8775ad9SMichael Tuexen 			    sh, ch, &inp, &net, vrf_id);
53424a2b92d9SMichael Tuexen 			stcb_looked_up = true;
534352640d61SMichael Tuexen 			if (stcb == NULL) {
53444a2b92d9SMichael Tuexen 				goto validate_cksum;
53454a2b92d9SMichael Tuexen 			}
534652640d61SMichael Tuexen 			if (stcb->asoc.rcv_edmid == SCTP_EDMID_NONE) {
534752640d61SMichael Tuexen 				goto validate_cksum;
534852640d61SMichael Tuexen 			}
534952640d61SMichael Tuexen 			KASSERT(stcb->asoc.rcv_edmid == SCTP_EDMID_LOWER_LAYER_DTLS,
535052640d61SMichael Tuexen 			    ("Unexpected EDMID %u", stcb->asoc.rcv_edmid));
53514a2b92d9SMichael Tuexen 			SCTP_STAT_INCR(sctps_recvzerocrc);
53524a2b92d9SMichael Tuexen 		}
53534a2b92d9SMichael Tuexen 	}
53544a2b92d9SMichael Tuexen cksum_validated:
53554a2b92d9SMichael Tuexen 	/* Destination port of 0 is illegal, based on RFC4960. */
53564a2b92d9SMichael Tuexen 	if (sh->dest_port == htons(0)) {
53574a2b92d9SMichael Tuexen 		SCTP_STAT_INCR(sctps_hdrops);
53584a2b92d9SMichael Tuexen 		if ((stcb == NULL) && (inp != NULL)) {
53594a2b92d9SMichael Tuexen 			inp_decr = inp;
53604a2b92d9SMichael Tuexen 		}
53614a2b92d9SMichael Tuexen 		goto out;
53624a2b92d9SMichael Tuexen 	}
53634a2b92d9SMichael Tuexen 	if (!stcb_looked_up) {
53644a2b92d9SMichael Tuexen 		stcb = sctp_findassociation_addr(m, offset, src, dst,
53654a2b92d9SMichael Tuexen 		    sh, ch, &inp, &net, vrf_id);
53664a2b92d9SMichael Tuexen 	}
53674474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
53683cf729a9SMichael Tuexen 	if ((ch->chunk_type != SCTP_INITIATION) &&
53693cf729a9SMichael Tuexen 	    (net != NULL) && (net->port != port)) {
5370a8775ad9SMichael Tuexen 		if (net->port == 0) {
53713cf729a9SMichael Tuexen 			/* UDP encapsulation turned on. */
53723cf729a9SMichael Tuexen 			net->mtu -= sizeof(struct udphdr);
53733cf729a9SMichael Tuexen 			if (stcb->asoc.smallest_mtu > net->mtu) {
53742de2ae33SMichael Tuexen 				sctp_pathmtu_adjustment(stcb, net->mtu, true);
53753cf729a9SMichael Tuexen 			}
53763cf729a9SMichael Tuexen 		} else if (port == 0) {
53773cf729a9SMichael Tuexen 			/* UDP encapsulation turned off. */
53783cf729a9SMichael Tuexen 			net->mtu += sizeof(struct udphdr);
53793cf729a9SMichael Tuexen 			/* XXX Update smallest_mtu */
5380a8775ad9SMichael Tuexen 		}
5381a8775ad9SMichael Tuexen 		net->port = port;
5382a8775ad9SMichael Tuexen 	}
53834474d71aSMichael Tuexen #endif
53845fe29cdfSMichael Tuexen 	if (net != NULL) {
5385457b4b88SMichael Tuexen 		net->flowtype = mflowtype;
53865fe29cdfSMichael Tuexen 		net->flowid = mflowid;
5387a8775ad9SMichael Tuexen 	}
5388a8775ad9SMichael Tuexen 	if (inp == NULL) {
5389a8775ad9SMichael Tuexen 		SCTP_STAT_INCR(sctps_noport);
53904a2b92d9SMichael Tuexen 		SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5391a8775ad9SMichael Tuexen 		if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
5392a8775ad9SMichael Tuexen 			goto out;
5393a8775ad9SMichael Tuexen 		}
5394a8775ad9SMichael Tuexen 		if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5395502d5e85SMichael Tuexen 			SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
5396a8775ad9SMichael Tuexen 			sctp_send_shutdown_complete2(src, dst, sh,
5397d089f9b9SMichael Tuexen 			    mflowtype, mflowid, fibnum,
5398a8775ad9SMichael Tuexen 			    vrf_id, port);
5399a8775ad9SMichael Tuexen 			goto out;
5400a8775ad9SMichael Tuexen 		}
5401a8775ad9SMichael Tuexen 		if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5402502d5e85SMichael Tuexen 			SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
5403a8775ad9SMichael Tuexen 			goto out;
5404a8775ad9SMichael Tuexen 		}
5405a8775ad9SMichael Tuexen 		if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
5406a8775ad9SMichael Tuexen 			if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
5407a8775ad9SMichael Tuexen 			    ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
5408a8775ad9SMichael Tuexen 			    (ch->chunk_type != SCTP_INIT))) {
5409ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5410ff1ffd74SMichael Tuexen 				    "Out of the blue");
5411a8775ad9SMichael Tuexen 				sctp_send_abort(m, iphlen, src, dst,
5412ff1ffd74SMichael Tuexen 				    sh, 0, op_err,
5413d089f9b9SMichael Tuexen 				    mflowtype, mflowid, fibnum,
5414a8775ad9SMichael Tuexen 				    vrf_id, port);
5415a8775ad9SMichael Tuexen 			}
5416a8775ad9SMichael Tuexen 		}
5417a8775ad9SMichael Tuexen 		goto out;
5418a8775ad9SMichael Tuexen 	} else if (stcb == NULL) {
5419a8775ad9SMichael Tuexen 		inp_decr = inp;
5420a8775ad9SMichael Tuexen 	}
5421b3f1ea41SRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5422dd294dceSMichael Tuexen 	    (void *)m, iphlen, offset, length, (void *)stcb);
5423f8829a4aSRandall Stewart 	if (stcb) {
5424f8829a4aSRandall Stewart 		/* always clear this before beginning a packet */
5425f8829a4aSRandall Stewart 		stcb->asoc.authenticated = 0;
5426f8829a4aSRandall Stewart 		stcb->asoc.seen_a_sack_this_pkt = 0;
54272afb3e84SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5428dd294dceSMichael Tuexen 		    (void *)stcb, stcb->asoc.state);
54292afb3e84SRandall Stewart 
5430c4739e2fSRandall Stewart 		if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5431c4739e2fSRandall Stewart 		    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
543263981c2bSRandall Stewart 			/*-
543363981c2bSRandall Stewart 			 * If we hit here, we had a ref count
543463981c2bSRandall Stewart 			 * up when the assoc was aborted and the
543563981c2bSRandall Stewart 			 * timer is clearing out the assoc, we should
543663981c2bSRandall Stewart 			 * NOT respond to any packet.. its OOTB.
543763981c2bSRandall Stewart 			 */
543863981c2bSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
5439a8775ad9SMichael Tuexen 			stcb = NULL;
54401e88cc8bSMichael Tuexen 			SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5441999f86d6SMichael Tuexen 			SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5442ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5443ff1ffd74SMichael Tuexen 			    msg);
5444ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5445d089f9b9SMichael Tuexen 			    mflowtype, mflowid, inp->fibnum,
5446c54a18d2SRandall Stewart 			    vrf_id, port);
5447a8775ad9SMichael Tuexen 			goto out;
544863981c2bSRandall Stewart 		}
5449f8829a4aSRandall Stewart 	}
5450f8829a4aSRandall Stewart 	if (IS_SCTP_CONTROL(ch)) {
5451f8829a4aSRandall Stewart 		/* process the control portion of the SCTP packet */
54523c503c28SRandall Stewart 		/* sa_ignore NO_NULL_CHK */
5453b1754ad1SMichael Tuexen 		stcb = sctp_process_control(m, iphlen, &offset, length,
5454b1754ad1SMichael Tuexen 		    src, dst, sh, ch,
5455f30ac432SMichael Tuexen 		    inp, stcb, &net, &fwd_tsn_seen,
5456d089f9b9SMichael Tuexen 		    mflowtype, mflowid, fibnum,
5457f30ac432SMichael Tuexen 		    vrf_id, port);
5458f8829a4aSRandall Stewart 		if (stcb) {
5459f8829a4aSRandall Stewart 			/*
5460f8829a4aSRandall Stewart 			 * This covers us if the cookie-echo was there and
5461f8829a4aSRandall Stewart 			 * it changes our INP.
5462f8829a4aSRandall Stewart 			 */
5463f8829a4aSRandall Stewart 			inp = stcb->sctp_ep;
54644474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
54653cf729a9SMichael Tuexen 			if ((ch->chunk_type != SCTP_INITIATION) &&
54663cf729a9SMichael Tuexen 			    (net != NULL) && (net->port != port)) {
5467b3f1ea41SRandall Stewart 				if (net->port == 0) {
54683cf729a9SMichael Tuexen 					/* UDP encapsulation turned on. */
54693cf729a9SMichael Tuexen 					net->mtu -= sizeof(struct udphdr);
54703cf729a9SMichael Tuexen 					if (stcb->asoc.smallest_mtu > net->mtu) {
54712de2ae33SMichael Tuexen 						sctp_pathmtu_adjustment(stcb, net->mtu, true);
54723cf729a9SMichael Tuexen 					}
54733cf729a9SMichael Tuexen 				} else if (port == 0) {
54743cf729a9SMichael Tuexen 					/* UDP encapsulation turned off. */
54753cf729a9SMichael Tuexen 					net->mtu += sizeof(struct udphdr);
54763cf729a9SMichael Tuexen 					/* XXX Update smallest_mtu */
5477b3f1ea41SRandall Stewart 				}
5478b3f1ea41SRandall Stewart 				net->port = port;
5479b3f1ea41SRandall Stewart 			}
54804474d71aSMichael Tuexen #endif
5481f8829a4aSRandall Stewart 		}
5482f8829a4aSRandall Stewart 	} else {
5483f8829a4aSRandall Stewart 		/*
5484f8829a4aSRandall Stewart 		 * no control chunks, so pre-process DATA chunks (these
5485f8829a4aSRandall Stewart 		 * checks are taken care of by control processing)
5486f8829a4aSRandall Stewart 		 */
5487f8829a4aSRandall Stewart 
5488f8829a4aSRandall Stewart 		/*
5489f8829a4aSRandall Stewart 		 * if DATA only packet, and auth is required, then punt...
5490f8829a4aSRandall Stewart 		 * can't have authenticated without any AUTH (control)
5491f8829a4aSRandall Stewart 		 * chunks
5492f8829a4aSRandall Stewart 		 */
5493b3f1ea41SRandall Stewart 		if ((stcb != NULL) &&
5494b3f1ea41SRandall Stewart 		    sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5495f8829a4aSRandall Stewart 			/* "silently" ignore */
54961e88cc8bSMichael Tuexen 			SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5497f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvauthmissing);
5498a8775ad9SMichael Tuexen 			goto out;
5499f8829a4aSRandall Stewart 		}
5500f8829a4aSRandall Stewart 		if (stcb == NULL) {
5501f8829a4aSRandall Stewart 			/* out of the blue DATA chunk */
55021e88cc8bSMichael Tuexen 			SCTP_PROBE5(receive, NULL, NULL, m, NULL, sh);
5503999f86d6SMichael Tuexen 			SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5504ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5505ff1ffd74SMichael Tuexen 			    msg);
5506ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5507d089f9b9SMichael Tuexen 			    mflowtype, mflowid, fibnum,
5508c54a18d2SRandall Stewart 			    vrf_id, port);
5509a8775ad9SMichael Tuexen 			goto out;
5510f8829a4aSRandall Stewart 		}
5511f8829a4aSRandall Stewart 		if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5512f8829a4aSRandall Stewart 			/* v_tag mismatch! */
55131e88cc8bSMichael Tuexen 			SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5514f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_badvtag);
5515a8775ad9SMichael Tuexen 			goto out;
5516f8829a4aSRandall Stewart 		}
5517f8829a4aSRandall Stewart 	}
5518f8829a4aSRandall Stewart 
55191e88cc8bSMichael Tuexen 	SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5520f8829a4aSRandall Stewart 	if (stcb == NULL) {
5521f8829a4aSRandall Stewart 		/*
5522f8829a4aSRandall Stewart 		 * no valid TCB for this packet, or we found it's a bad
5523f8829a4aSRandall Stewart 		 * packet while processing control, or we're done with this
5524f8829a4aSRandall Stewart 		 * packet (done or skip rest of data), so we drop it...
5525f8829a4aSRandall Stewart 		 */
5526a8775ad9SMichael Tuexen 		goto out;
5527f8829a4aSRandall Stewart 	}
55280053ed28SMichael Tuexen 
5529f8829a4aSRandall Stewart 	/*
5530f8829a4aSRandall Stewart 	 * DATA chunk processing
5531f8829a4aSRandall Stewart 	 */
5532f8829a4aSRandall Stewart 	/* plow through the data chunks while length > offset */
5533f8829a4aSRandall Stewart 
5534f8829a4aSRandall Stewart 	/*
5535f8829a4aSRandall Stewart 	 * Rest should be DATA only.  Check authentication state if AUTH for
5536f8829a4aSRandall Stewart 	 * DATA is required.
5537f8829a4aSRandall Stewart 	 */
5538b3f1ea41SRandall Stewart 	if ((length > offset) &&
5539b3f1ea41SRandall Stewart 	    (stcb != NULL) &&
5540b3f1ea41SRandall Stewart 	    sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5541f8829a4aSRandall Stewart 	    !stcb->asoc.authenticated) {
5542f8829a4aSRandall Stewart 		/* "silently" ignore */
5543f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvauthmissing);
5544ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_AUTH1,
5545ad81507eSRandall Stewart 		    "Data chunk requires AUTH, skipped\n");
5546a5d547adSRandall Stewart 		goto trigger_send;
5547f8829a4aSRandall Stewart 	}
5548f8829a4aSRandall Stewart 	if (length > offset) {
5549f8829a4aSRandall Stewart 		int retval;
5550f8829a4aSRandall Stewart 
5551f8829a4aSRandall Stewart 		/*
5552f8829a4aSRandall Stewart 		 * First check to make sure our state is correct. We would
5553f8829a4aSRandall Stewart 		 * not get here unless we really did have a tag, so we don't
5554f8829a4aSRandall Stewart 		 * abort if this happens, just dump the chunk silently.
5555f8829a4aSRandall Stewart 		 */
5556839d21d6SMichael Tuexen 		switch (SCTP_GET_STATE(stcb)) {
5557f8829a4aSRandall Stewart 		case SCTP_STATE_COOKIE_ECHOED:
5558f8829a4aSRandall Stewart 			/*
5559f8829a4aSRandall Stewart 			 * we consider data with valid tags in this state
5560f8829a4aSRandall Stewart 			 * shows us the cookie-ack was lost. Imply it was
5561f8829a4aSRandall Stewart 			 * there.
5562f8829a4aSRandall Stewart 			 */
5563f8829a4aSRandall Stewart 			sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5564f8829a4aSRandall Stewart 			break;
5565f8829a4aSRandall Stewart 		case SCTP_STATE_COOKIE_WAIT:
5566f8829a4aSRandall Stewart 			/*
5567f8829a4aSRandall Stewart 			 * We consider OOTB any data sent during asoc setup.
5568f8829a4aSRandall Stewart 			 */
5569999f86d6SMichael Tuexen 			SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5570ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5571ff1ffd74SMichael Tuexen 			    msg);
5572ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5573d089f9b9SMichael Tuexen 			    mflowtype, mflowid, inp->fibnum,
5574c54a18d2SRandall Stewart 			    vrf_id, port);
5575a8775ad9SMichael Tuexen 			goto out;
557652be287eSRandall Stewart 			/* sa_ignore NOTREACHED */
5577f8829a4aSRandall Stewart 			break;
5578f8829a4aSRandall Stewart 		case SCTP_STATE_EMPTY:	/* should not happen */
5579f8829a4aSRandall Stewart 		case SCTP_STATE_INUSE:	/* should not happen */
5580f8829a4aSRandall Stewart 		case SCTP_STATE_SHUTDOWN_RECEIVED:	/* This is a peer error */
5581f8829a4aSRandall Stewart 		case SCTP_STATE_SHUTDOWN_ACK_SENT:
5582f8829a4aSRandall Stewart 		default:
5583a8775ad9SMichael Tuexen 			goto out;
558452be287eSRandall Stewart 			/* sa_ignore NOTREACHED */
5585f8829a4aSRandall Stewart 			break;
5586f8829a4aSRandall Stewart 		case SCTP_STATE_OPEN:
5587f8829a4aSRandall Stewart 		case SCTP_STATE_SHUTDOWN_SENT:
5588f8829a4aSRandall Stewart 			break;
5589f8829a4aSRandall Stewart 		}
5590f8829a4aSRandall Stewart 		/* plow through the data chunks while length > offset */
5591b1754ad1SMichael Tuexen 		retval = sctp_process_data(mm, iphlen, &offset, length,
5592e7e71dd7SMichael Tuexen 		    inp, stcb, net, &high_tsn);
5593f8829a4aSRandall Stewart 		if (retval == 2) {
5594f8829a4aSRandall Stewart 			/*
5595f8829a4aSRandall Stewart 			 * The association aborted, NO UNLOCK needed since
5596f8829a4aSRandall Stewart 			 * the association is destroyed.
5597f8829a4aSRandall Stewart 			 */
5598a8775ad9SMichael Tuexen 			stcb = NULL;
5599a8775ad9SMichael Tuexen 			goto out;
5600f8829a4aSRandall Stewart 		}
5601b954d816SMichael Tuexen 		if (retval == 0) {
5602f8829a4aSRandall Stewart 			data_processed = 1;
5603b954d816SMichael Tuexen 		}
5604f8829a4aSRandall Stewart 		/*
5605f8829a4aSRandall Stewart 		 * Anything important needs to have been m_copy'ed in
5606f8829a4aSRandall Stewart 		 * process_data
5607f8829a4aSRandall Stewart 		 */
5608f8829a4aSRandall Stewart 	}
56090053ed28SMichael Tuexen 
5610493d8e5aSRandall Stewart 	/* take care of ecn */
561160990c0cSMichael Tuexen 	if ((data_processed == 1) &&
5612f342355aSMichael Tuexen 	    (stcb->asoc.ecn_supported == 1) &&
5613c446091bSMichael Tuexen 	    ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5614493d8e5aSRandall Stewart 		/* Yep, we need to add a ECNE */
5615493d8e5aSRandall Stewart 		sctp_send_ecn_echo(stcb, net, high_tsn);
5616493d8e5aSRandall Stewart 	}
56170053ed28SMichael Tuexen 
5618f8829a4aSRandall Stewart 	if ((data_processed == 0) && (fwd_tsn_seen)) {
56198f777478SMichael Tuexen 		int was_a_gap;
56208f777478SMichael Tuexen 		uint32_t highest_tsn;
5621f8829a4aSRandall Stewart 
562220b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
56238f777478SMichael Tuexen 			highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
56248f777478SMichael Tuexen 		} else {
56258f777478SMichael Tuexen 			highest_tsn = stcb->asoc.highest_tsn_inside_map;
5626f8829a4aSRandall Stewart 		}
562720b07a4dSMichael Tuexen 		was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
56288933fa13SRandall Stewart 		stcb->asoc.send_sack = 1;
56297215cc1bSMichael Tuexen 		sctp_sack_check(stcb, was_a_gap);
56308933fa13SRandall Stewart 	} else if (fwd_tsn_seen) {
56318933fa13SRandall Stewart 		stcb->asoc.send_sack = 1;
5632f8829a4aSRandall Stewart 	}
5633f8829a4aSRandall Stewart 	/* trigger send of any chunks in queue... */
5634a5d547adSRandall Stewart trigger_send:
5635f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
5636f8829a4aSRandall Stewart 	sctp_audit_log(0xE0, 2);
5637f8829a4aSRandall Stewart 	sctp_auditing(1, inp, stcb, net);
5638f8829a4aSRandall Stewart #endif
5639ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT1,
5640ad81507eSRandall Stewart 	    "Check for chunk output prw:%d tqe:%d tf=%d\n",
5641f8829a4aSRandall Stewart 	    stcb->asoc.peers_rwnd,
5642f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5643f8829a4aSRandall Stewart 	    stcb->asoc.total_flight);
5644f8829a4aSRandall Stewart 	un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5645493d8e5aSRandall Stewart 	if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
5646493d8e5aSRandall Stewart 		cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
5647493d8e5aSRandall Stewart 	}
56485114dccbSMichael Tuexen 	if (!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue) ||
56495114dccbSMichael Tuexen 	    cnt_ctrl_ready ||
56505114dccbSMichael Tuexen 	    stcb->asoc.trigger_reset ||
565164c8fc5dSMichael Tuexen 	    ((un_sent > 0) &&
565264c8fc5dSMichael Tuexen 	    (stcb->asoc.peers_rwnd > 0 || stcb->asoc.total_flight == 0))) {
5653ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5654ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5655ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5656f8829a4aSRandall Stewart 	}
5657f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
5658f8829a4aSRandall Stewart 	sctp_audit_log(0xE0, 3);
5659f8829a4aSRandall Stewart 	sctp_auditing(2, inp, stcb, net);
5660f8829a4aSRandall Stewart #endif
5661a8775ad9SMichael Tuexen out:
5662a8775ad9SMichael Tuexen 	if (stcb != NULL) {
5663f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
5664a8775ad9SMichael Tuexen 	}
5665a8775ad9SMichael Tuexen 	if (inp_decr != NULL) {
5666a8775ad9SMichael Tuexen 		/* reduce ref-count */
5667a8775ad9SMichael Tuexen 		SCTP_INP_WLOCK(inp_decr);
5668a8775ad9SMichael Tuexen 		SCTP_INP_DECR_REF(inp_decr);
5669a8775ad9SMichael Tuexen 		SCTP_INP_WUNLOCK(inp_decr);
5670a8775ad9SMichael Tuexen 	}
56716e55db54SRandall Stewart 	return;
5672f8829a4aSRandall Stewart }
5673f8829a4aSRandall Stewart 
5674e6194c2eSMichael Tuexen #ifdef INET
5675f8829a4aSRandall Stewart void
5676af83f5d7SRoman Divacky sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
5677f8829a4aSRandall Stewart {
5678139bc87fSRandall Stewart 	struct mbuf *m;
5679f8829a4aSRandall Stewart 	int iphlen;
5680ad21a364SRandall Stewart 	uint32_t vrf_id = 0;
5681f8829a4aSRandall Stewart 	uint8_t ecn_bits;
5682b1754ad1SMichael Tuexen 	struct sockaddr_in src, dst;
5683f8829a4aSRandall Stewart 	struct ip *ip;
5684f8829a4aSRandall Stewart 	struct sctphdr *sh;
5685f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
56866dc5aabcSMichael Tuexen 	int length, offset;
5687a8775ad9SMichael Tuexen 	uint8_t compute_crc;
5688a8775ad9SMichael Tuexen 	uint32_t mflowid;
5689457b4b88SMichael Tuexen 	uint8_t mflowtype;
5690d089f9b9SMichael Tuexen 	uint16_t fibnum;
56919c7635e1SMichael Tuexen 
56926dc5aabcSMichael Tuexen 	iphlen = off;
569317205eccSRandall Stewart 	if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
569417205eccSRandall Stewart 		SCTP_RELEASE_PKT(i_pak);
569517205eccSRandall Stewart 		return;
569617205eccSRandall Stewart 	}
5697139bc87fSRandall Stewart 	m = SCTP_HEADER_TO_CHAIN(i_pak);
5698f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
5699f8829a4aSRandall Stewart 	/* Log in any input mbufs */
5700b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
57014be807c4SMichael Tuexen 		sctp_log_mbc(m, SCTP_MBUF_INPUT);
570280fefe0aSRandall Stewart 	}
5703f8829a4aSRandall Stewart #endif
5704207304d4SRandall Stewart #ifdef SCTP_PACKET_LOGGING
57056dc5aabcSMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
5706f9384252SMichael Tuexen 		sctp_packet_log(m);
57076dc5aabcSMichael Tuexen 	}
5708207304d4SRandall Stewart #endif
5709a8775ad9SMichael Tuexen 	SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
57101a94cdbeSMichael Tuexen 	    "sctp_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
5711a8775ad9SMichael Tuexen 	    m->m_pkthdr.len,
5712a8775ad9SMichael Tuexen 	    if_name(m->m_pkthdr.rcvif),
57131a94cdbeSMichael Tuexen 	    (int)m->m_pkthdr.csum_flags, CSUM_BITS);
5714f30ac432SMichael Tuexen 	mflowid = m->m_pkthdr.flowid;
5715457b4b88SMichael Tuexen 	mflowtype = M_HASHTYPE_GET(m);
5716d089f9b9SMichael Tuexen 	fibnum = M_GETFIB(m);
57176dc5aabcSMichael Tuexen 	SCTP_STAT_INCR(sctps_recvpackets);
57186dc5aabcSMichael Tuexen 	SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
57196dc5aabcSMichael Tuexen 	/* Get IP, SCTP, and first chunk header together in the first mbuf. */
57206dc5aabcSMichael Tuexen 	offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5721139bc87fSRandall Stewart 	if (SCTP_BUF_LEN(m) < offset) {
5722b1754ad1SMichael Tuexen 		if ((m = m_pullup(m, offset)) == NULL) {
5723f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_hdrops);
5724f8829a4aSRandall Stewart 			return;
5725f8829a4aSRandall Stewart 		}
5726f8829a4aSRandall Stewart 	}
5727b1754ad1SMichael Tuexen 	ip = mtod(m, struct ip *);
57286dc5aabcSMichael Tuexen 	sh = (struct sctphdr *)((caddr_t)ip + iphlen);
57296dc5aabcSMichael Tuexen 	ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
57306dc5aabcSMichael Tuexen 	offset -= sizeof(struct sctp_chunkhdr);
5731b1754ad1SMichael Tuexen 	memset(&src, 0, sizeof(struct sockaddr_in));
5732b1754ad1SMichael Tuexen 	src.sin_family = AF_INET;
5733b1754ad1SMichael Tuexen 	src.sin_len = sizeof(struct sockaddr_in);
5734b1754ad1SMichael Tuexen 	src.sin_port = sh->src_port;
5735b1754ad1SMichael Tuexen 	src.sin_addr = ip->ip_src;
5736b1754ad1SMichael Tuexen 	memset(&dst, 0, sizeof(struct sockaddr_in));
5737b1754ad1SMichael Tuexen 	dst.sin_family = AF_INET;
5738b1754ad1SMichael Tuexen 	dst.sin_len = sizeof(struct sockaddr_in);
5739b1754ad1SMichael Tuexen 	dst.sin_port = sh->dest_port;
5740b1754ad1SMichael Tuexen 	dst.sin_addr = ip->ip_dst;
57418ad458a4SGleb Smirnoff 	length = ntohs(ip->ip_len);
57426dc5aabcSMichael Tuexen 	/* Validate mbuf chain length with IP payload length. */
5743a8775ad9SMichael Tuexen 	if (SCTP_HEADER_LEN(m) != length) {
57446dc5aabcSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT1,
5745a8775ad9SMichael Tuexen 		    "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
5746a99b6783SRandall Stewart 		SCTP_STAT_INCR(sctps_hdrops);
5747a8775ad9SMichael Tuexen 		goto out;
5748a99b6783SRandall Stewart 	}
5749f8829a4aSRandall Stewart 	/* SCTP does not allow broadcasts or multicasts */
5750b1754ad1SMichael Tuexen 	if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
5751a8775ad9SMichael Tuexen 		goto out;
5752f8829a4aSRandall Stewart 	}
5753b1754ad1SMichael Tuexen 	if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
5754a8775ad9SMichael Tuexen 		goto out;
5755f8829a4aSRandall Stewart 	}
5756a8775ad9SMichael Tuexen 	ecn_bits = ip->ip_tos;
5757a99b6783SRandall Stewart 	if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
5758a99b6783SRandall Stewart 		SCTP_STAT_INCR(sctps_recvhwcrc);
5759a8775ad9SMichael Tuexen 		compute_crc = 0;
5760a8775ad9SMichael Tuexen 	} else {
5761a99b6783SRandall Stewart 		SCTP_STAT_INCR(sctps_recvswcrc);
5762a8775ad9SMichael Tuexen 		compute_crc = 1;
5763f8829a4aSRandall Stewart 	}
5764b1754ad1SMichael Tuexen 	sctp_common_input_processing(&m, iphlen, offset, length,
5765b1754ad1SMichael Tuexen 	    (struct sockaddr *)&src,
5766b1754ad1SMichael Tuexen 	    (struct sockaddr *)&dst,
5767a8775ad9SMichael Tuexen 	    sh, ch,
5768a8775ad9SMichael Tuexen 	    compute_crc,
5769a8775ad9SMichael Tuexen 	    ecn_bits,
5770d089f9b9SMichael Tuexen 	    mflowtype, mflowid, fibnum,
5771f30ac432SMichael Tuexen 	    vrf_id, port);
5772a8775ad9SMichael Tuexen out:
5773f8829a4aSRandall Stewart 	if (m) {
5774f8829a4aSRandall Stewart 		sctp_m_freem(m);
5775f8829a4aSRandall Stewart 	}
5776f8829a4aSRandall Stewart 	return;
5777f8829a4aSRandall Stewart }
5778bfc46083SRandall Stewart 
57792f9e6db0SMichael Tuexen #if defined(SCTP_MCORE_INPUT) && defined(SMP)
57800071ee5eSRandall Stewart extern int *sctp_cpuarry;
57810071ee5eSRandall Stewart #endif
5782bfc46083SRandall Stewart 
57838f5a8818SKevin Lo int
578482eaf95eSMichael Tuexen sctp_input(struct mbuf **mp, int *offp, int proto SCTP_UNUSED)
5785c54a18d2SRandall Stewart {
57868f5a8818SKevin Lo 	struct mbuf *m;
57878f5a8818SKevin Lo 	int off;
57888f5a8818SKevin Lo 
57898f5a8818SKevin Lo 	m = *mp;
57908f5a8818SKevin Lo 	off = *offp;
57912f9e6db0SMichael Tuexen #if defined(SCTP_MCORE_INPUT) && defined(SMP)
579282eaf95eSMichael Tuexen 	if (mp_ncpus > 1) {
5793bfc46083SRandall Stewart 		struct ip *ip;
5794bfc46083SRandall Stewart 		struct sctphdr *sh;
5795bfc46083SRandall Stewart 		int offset;
5796bfc46083SRandall Stewart 		int cpu_to_use;
579738521fb9SRandall Stewart 		uint32_t flowid, tag;
5798bfc46083SRandall Stewart 
5799457b4b88SMichael Tuexen 		if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
580038521fb9SRandall Stewart 			flowid = m->m_pkthdr.flowid;
580138521fb9SRandall Stewart 		} else {
580238521fb9SRandall Stewart 			/*
580338521fb9SRandall Stewart 			 * No flow id built by lower layers fix it so we
580438521fb9SRandall Stewart 			 * create one.
580538521fb9SRandall Stewart 			 */
5806b1754ad1SMichael Tuexen 			offset = off + sizeof(struct sctphdr);
5807bfc46083SRandall Stewart 			if (SCTP_BUF_LEN(m) < offset) {
5808b1754ad1SMichael Tuexen 				if ((m = m_pullup(m, offset)) == NULL) {
5809bfc46083SRandall Stewart 					SCTP_STAT_INCR(sctps_hdrops);
58108f5a8818SKevin Lo 					return (IPPROTO_DONE);
5811bfc46083SRandall Stewart 				}
5812bfc46083SRandall Stewart 			}
5813b1754ad1SMichael Tuexen 			ip = mtod(m, struct ip *);
5814bfc46083SRandall Stewart 			sh = (struct sctphdr *)((caddr_t)ip + off);
58150071ee5eSRandall Stewart 			tag = htonl(sh->v_tag);
581638521fb9SRandall Stewart 			flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
581738521fb9SRandall Stewart 			m->m_pkthdr.flowid = flowid;
581836ad8372SSepherosa Ziehau 			M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE_HASH);
58190071ee5eSRandall Stewart 		}
582038521fb9SRandall Stewart 		cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
5821bfc46083SRandall Stewart 		sctp_queue_to_mcore(m, off, cpu_to_use);
58228f5a8818SKevin Lo 		return (IPPROTO_DONE);
5823bfc46083SRandall Stewart 	}
5824bfc46083SRandall Stewart #endif
5825bfc46083SRandall Stewart 	sctp_input_with_port(m, off, 0);
58268f5a8818SKevin Lo 	return (IPPROTO_DONE);
5827c54a18d2SRandall Stewart }
5828e6194c2eSMichael Tuexen #endif
5829