xref: /freebsd/sys/netinet/sctp_input.c (revision 9d8a3718e24c9ec1ffca6efba64aba3e308aee96)
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
sctp_stop_all_cookie_timers(struct sctp_tcb * stcb)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
sctp_handle_init(struct mbuf * m,int iphlen,int offset,struct sockaddr * src,struct sockaddr * dst,struct sctphdr * sh,struct sctp_init_chunk * cp,struct sctp_inpcb * inp,struct sctp_tcb * stcb,struct sctp_nets * net,uint8_t mflowtype,uint32_t mflowid,uint32_t vrf_id,uint16_t port)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
sctp_is_there_unsent_data(struct sctp_tcb * stcb,int so_locked)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
sctp_process_init(struct sctp_init_chunk * cp,struct sctp_tcb * stcb)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
sctp_process_init_ack(struct mbuf * m,int iphlen,int offset,struct sockaddr * src,struct sockaddr * dst,struct sctphdr * sh,struct sctp_init_ack_chunk * cp,struct sctp_tcb * stcb,struct sctp_nets * net,int * abort_no_unlock,uint8_t mflowtype,uint32_t mflowid,uint32_t vrf_id)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,
409644cffe6SMichael Tuexen 	    &nat_friendly, &cookie_found, NULL);
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
sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk * cp,struct sctp_tcb * stcb,struct sctp_nets * net)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
sctp_handle_nat_colliding_state(struct sctp_tcb * stcb)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
sctp_handle_nat_missing_state(struct sctp_tcb * stcb,struct sctp_nets * net)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
sctp_handle_abort(struct sctp_abort_chunk * abort,struct sctp_tcb * stcb,struct sctp_nets * net)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
sctp_start_net_timers(struct sctp_tcb * stcb)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
sctp_check_data_from_peer(struct sctp_tcb * stcb,int * abort_flag)833d18c845fSMichael Tuexen sctp_check_data_from_peer(struct sctp_tcb *stcb, int *abort_flag)
834d18c845fSMichael Tuexen {
835d18c845fSMichael Tuexen 	char msg[SCTP_DIAG_INFO_LEN];
836d18c845fSMichael Tuexen 	struct sctp_association *asoc;
837d18c845fSMichael Tuexen 	struct mbuf *op_err;
838d18c845fSMichael Tuexen 	unsigned int i;
839d18c845fSMichael Tuexen 
840d18c845fSMichael Tuexen 	*abort_flag = 0;
841d18c845fSMichael Tuexen 	asoc = &stcb->asoc;
842d18c845fSMichael Tuexen 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->cumulative_tsn) ||
843d18c845fSMichael Tuexen 	    SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn)) {
844d18c845fSMichael Tuexen 		SCTP_SNPRINTF(msg, sizeof(msg), "Missing TSN");
845d18c845fSMichael Tuexen 		*abort_flag = 1;
846d18c845fSMichael Tuexen 	}
847d18c845fSMichael Tuexen 	if (!*abort_flag) {
848d18c845fSMichael Tuexen 		for (i = 0; i < asoc->streamincnt; i++) {
849d18c845fSMichael Tuexen 			if (!TAILQ_EMPTY(&asoc->strmin[i].inqueue) ||
850d18c845fSMichael Tuexen 			    !TAILQ_EMPTY(&asoc->strmin[i].uno_inqueue)) {
851d18c845fSMichael Tuexen 				SCTP_SNPRINTF(msg, sizeof(msg), "Missing user data");
852d18c845fSMichael Tuexen 				*abort_flag = 1;
853d18c845fSMichael Tuexen 				break;
854d18c845fSMichael Tuexen 			}
855d18c845fSMichael Tuexen 		}
856d18c845fSMichael Tuexen 	}
857d18c845fSMichael Tuexen 	if (*abort_flag) {
858d18c845fSMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
859d18c845fSMichael Tuexen 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INPUT + SCTP_LOC_9;
860d18c845fSMichael Tuexen 		sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, false, SCTP_SO_NOT_LOCKED);
861d18c845fSMichael Tuexen 	}
862d18c845fSMichael Tuexen }
863d18c845fSMichael Tuexen 
864d18c845fSMichael Tuexen static void
sctp_handle_shutdown(struct sctp_shutdown_chunk * cp,struct sctp_tcb * stcb,struct sctp_nets * net,int * abort_flag)865f8829a4aSRandall Stewart sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
866f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
867f8829a4aSRandall Stewart {
868f8829a4aSRandall Stewart 	int some_on_streamwheel;
869aa1cfca9SMichael Tuexen 	int old_state;
870ceaad40aSRandall Stewart 
8714f14d4b6SMichael Tuexen 	SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_shutdown: handling SHUTDOWN\n");
872f8829a4aSRandall Stewart 	if (stcb == NULL)
873f8829a4aSRandall Stewart 		return;
874839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
875839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
876f8829a4aSRandall Stewart 		return;
877f8829a4aSRandall Stewart 	}
878f8829a4aSRandall Stewart 	if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
879f8829a4aSRandall Stewart 		/* Shutdown NOT the expected size */
880f8829a4aSRandall Stewart 		return;
881aa1cfca9SMichael Tuexen 	}
882839d21d6SMichael Tuexen 	old_state = SCTP_GET_STATE(stcb);
8837215cc1bSMichael Tuexen 	sctp_update_acked(stcb, cp, abort_flag);
8847e6206afSMichael Tuexen 	if (*abort_flag) {
8857e6206afSMichael Tuexen 		return;
8867e6206afSMichael Tuexen 	}
887d18c845fSMichael Tuexen 	sctp_check_data_from_peer(stcb, abort_flag);
888d18c845fSMichael Tuexen 	if (*abort_flag) {
889d18c845fSMichael Tuexen 		return;
890d18c845fSMichael Tuexen 	}
891f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
892839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
893839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
894839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT)) {
895839d21d6SMichael Tuexen 			SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_RECEIVED);
896b7b84c0eSMichael Tuexen 			/*
897b7b84c0eSMichael Tuexen 			 * notify upper layer that peer has initiated a
898b7b84c0eSMichael Tuexen 			 * shutdown
899b7b84c0eSMichael Tuexen 			 */
900ceaad40aSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
901f8829a4aSRandall Stewart 
902f8829a4aSRandall Stewart 			/* reset time */
9031095da75SMichael Tuexen 			(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
904f8829a4aSRandall Stewart 		}
905f8829a4aSRandall Stewart 	}
906839d21d6SMichael Tuexen 	if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) {
907f8829a4aSRandall Stewart 		/*
908f8829a4aSRandall Stewart 		 * stop the shutdown timer, since we WILL move to
909f8829a4aSRandall Stewart 		 * SHUTDOWN-ACK-SENT.
910f8829a4aSRandall Stewart 		 */
911b7d130beSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
912b7d130beSMichael Tuexen 		    net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
913f8829a4aSRandall Stewart 	}
9145bead436SRandall Stewart 	/* Now is there unsent data on a stream somewhere? */
915689e6a5fSMichael Tuexen 	some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
916f8829a4aSRandall Stewart 
9171095da75SMichael Tuexen 	if (!TAILQ_EMPTY(&stcb->asoc.send_queue) ||
9181095da75SMichael Tuexen 	    !TAILQ_EMPTY(&stcb->asoc.sent_queue) ||
919f8829a4aSRandall Stewart 	    some_on_streamwheel) {
920f8829a4aSRandall Stewart 		/* By returning we will push more data out */
921f8829a4aSRandall Stewart 		return;
922f8829a4aSRandall Stewart 	} else {
923f8829a4aSRandall Stewart 		/* no outstanding data to send, so move on... */
924f8829a4aSRandall Stewart 		/* send SHUTDOWN-ACK */
925f8829a4aSRandall Stewart 		/* move to SHUTDOWN-ACK-SENT state */
926839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
927839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
928f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
929f8829a4aSRandall Stewart 		}
930839d21d6SMichael Tuexen 		if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
931839d21d6SMichael Tuexen 			SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_ACK_SENT);
93212af6654SMichael Tuexen 			sctp_stop_timers_for_shutdown(stcb);
933c39cfa1fSMichael Tuexen 			sctp_send_shutdown_ack(stcb, net);
934aa1cfca9SMichael Tuexen 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
935aa1cfca9SMichael Tuexen 			    stcb->sctp_ep, stcb, net);
936aa1cfca9SMichael Tuexen 		} else if (old_state == SCTP_STATE_SHUTDOWN_ACK_SENT) {
937aa1cfca9SMichael Tuexen 			sctp_send_shutdown_ack(stcb, net);
938aa1cfca9SMichael Tuexen 		}
939f8829a4aSRandall Stewart 	}
940f8829a4aSRandall Stewart }
941f8829a4aSRandall Stewart 
942f8829a4aSRandall Stewart static void
sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk * cp SCTP_UNUSED,struct sctp_tcb * stcb,struct sctp_nets * net)9437215cc1bSMichael Tuexen sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
9447b470fc3SMichael Tuexen     struct sctp_tcb *stcb,
9457b470fc3SMichael Tuexen     struct sctp_nets *net)
946f8829a4aSRandall Stewart {
947d18c845fSMichael Tuexen 	int abort_flag;
948d18c845fSMichael Tuexen 
949ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
950ad81507eSRandall Stewart 	    "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
9514f14d4b6SMichael Tuexen 	if (stcb == NULL) {
952f8829a4aSRandall Stewart 		return;
9534f14d4b6SMichael Tuexen 	}
954f8829a4aSRandall Stewart 
955f8829a4aSRandall Stewart 	/* process according to association state */
956839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
957839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
9587b470fc3SMichael Tuexen 		/* unexpected SHUTDOWN-ACK... do OOTB handling... */
9597b470fc3SMichael Tuexen 		sctp_send_shutdown_complete(stcb, net, 1);
9607b470fc3SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
9617b470fc3SMichael Tuexen 		return;
9627b470fc3SMichael Tuexen 	}
963839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
964839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
965f8829a4aSRandall Stewart 		/* unexpected SHUTDOWN-ACK... so ignore... */
966f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
967f8829a4aSRandall Stewart 		return;
968f8829a4aSRandall Stewart 	}
969d18c845fSMichael Tuexen 	sctp_check_data_from_peer(stcb, &abort_flag);
970d18c845fSMichael Tuexen 	if (abort_flag) {
971d18c845fSMichael Tuexen 		return;
972d18c845fSMichael Tuexen 	}
97356f778aaSMichael Tuexen #ifdef INVARIANTS
9741095da75SMichael Tuexen 	if (!TAILQ_EMPTY(&stcb->asoc.send_queue) ||
9751095da75SMichael Tuexen 	    !TAILQ_EMPTY(&stcb->asoc.sent_queue) ||
976124d851aSMichael Tuexen 	    sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
97756f778aaSMichael Tuexen 		panic("Queues are not empty when handling SHUTDOWN-ACK");
978f8829a4aSRandall Stewart 	}
97956f778aaSMichael Tuexen #endif
980f8829a4aSRandall Stewart 	/* stop the timer */
981b7d130beSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net,
982b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
983f8829a4aSRandall Stewart 	/* send SHUTDOWN-COMPLETE */
9847b470fc3SMichael Tuexen 	sctp_send_shutdown_complete(stcb, net, 0);
985f8829a4aSRandall Stewart 	/* notify upper layer protocol */
986f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
987f8829a4aSRandall Stewart 		if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
988f8829a4aSRandall Stewart 		    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
989f210e4fbSMichael Tuexen 			SCTP_SB_CLEAR(stcb->sctp_socket->so_snd);
990f8829a4aSRandall Stewart 		}
991c75fdd30SMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
992f8829a4aSRandall Stewart 	}
993f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
994f8829a4aSRandall Stewart 	/* free the TCB but first save off the ep */
995c4739e2fSRandall Stewart 	(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
996b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
997f8829a4aSRandall Stewart }
998f8829a4aSRandall Stewart 
999f8829a4aSRandall Stewart static void
sctp_process_unrecog_chunk(struct sctp_tcb * stcb,uint8_t chunk_type)10006fb7b4fbSMichael Tuexen sctp_process_unrecog_chunk(struct sctp_tcb *stcb, uint8_t chunk_type)
1001f8829a4aSRandall Stewart {
10023e87bccdSMichael Tuexen 	switch (chunk_type) {
1003f8829a4aSRandall Stewart 	case SCTP_ASCONF_ACK:
1004f8829a4aSRandall Stewart 	case SCTP_ASCONF:
10056fb7b4fbSMichael Tuexen 		sctp_asconf_cleanup(stcb);
1006f8829a4aSRandall Stewart 		break;
100744249214SRandall Stewart 	case SCTP_IFORWARD_CUM_TSN:
1008f8829a4aSRandall Stewart 	case SCTP_FORWARD_CUM_TSN:
1009dd973b0eSMichael Tuexen 		stcb->asoc.prsctp_supported = 0;
1010f8829a4aSRandall Stewart 		break;
1011f8829a4aSRandall Stewart 	default:
1012ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
10133e87bccdSMichael Tuexen 		    "Peer does not support chunk type %d (0x%x).\n",
10143e87bccdSMichael Tuexen 		    chunk_type, chunk_type);
1015f8829a4aSRandall Stewart 		break;
1016f8829a4aSRandall Stewart 	}
1017f8829a4aSRandall Stewart }
1018f8829a4aSRandall Stewart 
1019f8829a4aSRandall Stewart /*
1020f8829a4aSRandall Stewart  * Skip past the param header and then we will find the param that caused the
1021f8829a4aSRandall Stewart  * problem.  There are a number of param's in a ASCONF OR the prsctp param
1022f8829a4aSRandall Stewart  * these will turn of specific features.
1023c79bec9cSMichael Tuexen  * XXX: Is this the right thing to do?
1024f8829a4aSRandall Stewart  */
1025f8829a4aSRandall Stewart static void
sctp_process_unrecog_param(struct sctp_tcb * stcb,uint16_t parameter_type)10263e87bccdSMichael Tuexen sctp_process_unrecog_param(struct sctp_tcb *stcb, uint16_t parameter_type)
1027f8829a4aSRandall Stewart {
10283e87bccdSMichael Tuexen 	switch (parameter_type) {
1029f8829a4aSRandall Stewart 		/* pr-sctp draft */
1030f8829a4aSRandall Stewart 	case SCTP_PRSCTP_SUPPORTED:
1031dd973b0eSMichael Tuexen 		stcb->asoc.prsctp_supported = 0;
1032f8829a4aSRandall Stewart 		break;
1033f8829a4aSRandall Stewart 	case SCTP_SUPPORTED_CHUNK_EXT:
1034f8829a4aSRandall Stewart 		break;
1035f8829a4aSRandall Stewart 		/* draft-ietf-tsvwg-addip-sctp */
1036830d754dSRandall Stewart 	case SCTP_HAS_NAT_SUPPORT:
1037830d754dSRandall Stewart 		stcb->asoc.peer_supports_nat = 0;
1038830d754dSRandall Stewart 		break;
1039f8829a4aSRandall Stewart 	case SCTP_ADD_IP_ADDRESS:
1040f8829a4aSRandall Stewart 	case SCTP_DEL_IP_ADDRESS:
1041f8829a4aSRandall Stewart 	case SCTP_SET_PRIM_ADDR:
1042c79bec9cSMichael Tuexen 		stcb->asoc.asconf_supported = 0;
1043f8829a4aSRandall Stewart 		break;
1044f8829a4aSRandall Stewart 	case SCTP_SUCCESS_REPORT:
1045f8829a4aSRandall Stewart 	case SCTP_ERROR_CAUSE_IND:
1046ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1047ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
1048ad81507eSRandall Stewart 		    "Turning off ASCONF to this strange peer\n");
1049c79bec9cSMichael Tuexen 		stcb->asoc.asconf_supported = 0;
1050f8829a4aSRandall Stewart 		break;
1051f8829a4aSRandall Stewart 	default:
1052ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
10533e87bccdSMichael Tuexen 		    "Peer does not support param type %d (0x%x)??\n",
10543e87bccdSMichael Tuexen 		    parameter_type, parameter_type);
1055f8829a4aSRandall Stewart 		break;
1056f8829a4aSRandall Stewart 	}
1057f8829a4aSRandall Stewart }
1058f8829a4aSRandall Stewart 
1059f8829a4aSRandall Stewart static int
sctp_handle_error(struct sctp_chunkhdr * ch,struct sctp_tcb * stcb,struct sctp_nets * net,uint32_t limit)1060f8829a4aSRandall Stewart sctp_handle_error(struct sctp_chunkhdr *ch,
10613e87bccdSMichael Tuexen     struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
1062f8829a4aSRandall Stewart {
10633e87bccdSMichael Tuexen 	struct sctp_error_cause *cause;
1064f8829a4aSRandall Stewart 	struct sctp_association *asoc;
10653e87bccdSMichael Tuexen 	uint32_t remaining_length, adjust;
10663e87bccdSMichael Tuexen 	uint16_t code, cause_code, cause_length;
1067ceaad40aSRandall Stewart 
1068f8829a4aSRandall Stewart 	/* parse through all of the errors and process */
1069f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
10703e87bccdSMichael Tuexen 	cause = (struct sctp_error_cause *)((caddr_t)ch +
1071f8829a4aSRandall Stewart 	    sizeof(struct sctp_chunkhdr));
10723e87bccdSMichael Tuexen 	remaining_length = ntohs(ch->chunk_length);
10733e87bccdSMichael Tuexen 	if (remaining_length > limit) {
10743e87bccdSMichael Tuexen 		remaining_length = limit;
10753e87bccdSMichael Tuexen 	}
10763e87bccdSMichael Tuexen 	if (remaining_length >= sizeof(struct sctp_chunkhdr)) {
10773e87bccdSMichael Tuexen 		remaining_length -= sizeof(struct sctp_chunkhdr);
10783e87bccdSMichael Tuexen 	} else {
10793e87bccdSMichael Tuexen 		remaining_length = 0;
10803e87bccdSMichael Tuexen 	}
10813e87bccdSMichael Tuexen 	code = 0;
10823e87bccdSMichael Tuexen 	while (remaining_length >= sizeof(struct sctp_error_cause)) {
1083f8829a4aSRandall Stewart 		/* Process an Error Cause */
10843e87bccdSMichael Tuexen 		cause_code = ntohs(cause->code);
10853e87bccdSMichael Tuexen 		cause_length = ntohs(cause->length);
10863e87bccdSMichael Tuexen 		if ((cause_length > remaining_length) || (cause_length == 0)) {
10873e87bccdSMichael Tuexen 			/* Invalid cause length, possibly due to truncation. */
10883e87bccdSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in cause - bytes left: %u cause length: %u\n",
10893e87bccdSMichael Tuexen 			    remaining_length, cause_length);
1090f8829a4aSRandall Stewart 			return (0);
1091f8829a4aSRandall Stewart 		}
10923e87bccdSMichael Tuexen 		if (code == 0) {
1093389b1b11SMichael Tuexen 			/* report the first error cause */
10943e87bccdSMichael Tuexen 			code = cause_code;
1095389b1b11SMichael Tuexen 		}
10963e87bccdSMichael Tuexen 		switch (cause_code) {
1097f8829a4aSRandall Stewart 		case SCTP_CAUSE_INVALID_STREAM:
1098f8829a4aSRandall Stewart 		case SCTP_CAUSE_MISSING_PARAM:
1099f8829a4aSRandall Stewart 		case SCTP_CAUSE_INVALID_PARAM:
1100f8829a4aSRandall Stewart 		case SCTP_CAUSE_NO_USER_DATA:
11013e87bccdSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %u back? We have a bug :/ (or do they?)\n",
11023e87bccdSMichael Tuexen 			    cause_code);
1103f8829a4aSRandall Stewart 			break;
1104830d754dSRandall Stewart 		case SCTP_CAUSE_NAT_COLLIDING_STATE:
1105504ee6a0SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state, ERROR flags: %x\n",
1106830d754dSRandall Stewart 			    ch->chunk_flags);
1107830d754dSRandall Stewart 			if (sctp_handle_nat_colliding_state(stcb)) {
1108830d754dSRandall Stewart 				return (0);
1109830d754dSRandall Stewart 			}
1110830d754dSRandall Stewart 			break;
1111830d754dSRandall Stewart 		case SCTP_CAUSE_NAT_MISSING_STATE:
1112504ee6a0SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state, ERROR flags: %x\n",
1113830d754dSRandall Stewart 			    ch->chunk_flags);
1114830d754dSRandall Stewart 			if (sctp_handle_nat_missing_state(stcb, net)) {
1115830d754dSRandall Stewart 				return (0);
1116830d754dSRandall Stewart 			}
1117830d754dSRandall Stewart 			break;
1118f8829a4aSRandall Stewart 		case SCTP_CAUSE_STALE_COOKIE:
1119f8829a4aSRandall Stewart 			/*
1120f8829a4aSRandall Stewart 			 * We only act if we have echoed a cookie and are
1121f8829a4aSRandall Stewart 			 * waiting.
1122f8829a4aSRandall Stewart 			 */
11233e87bccdSMichael Tuexen 			if ((cause_length >= sizeof(struct sctp_error_stale_cookie)) &&
1124839d21d6SMichael Tuexen 			    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
11251f0e1344SMichael Tuexen 				struct timeval now;
11263e87bccdSMichael Tuexen 				struct sctp_error_stale_cookie *stale_cookie;
11271f0e1344SMichael Tuexen 				uint64_t stale_time;
1128f8829a4aSRandall Stewart 
1129f8829a4aSRandall Stewart 				asoc->stale_cookie_count++;
11301f0e1344SMichael Tuexen 				if (asoc->stale_cookie_count > asoc->max_init_times) {
1131105b68b4SMichael Tuexen 					sctp_abort_notification(stcb, false, true, 0, NULL, SCTP_SO_NOT_LOCKED);
1132c4739e2fSRandall Stewart 					(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1133b7d130beSMichael Tuexen 					    SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1134f8829a4aSRandall Stewart 					return (-1);
1135f8829a4aSRandall Stewart 				}
11361f0e1344SMichael Tuexen 				stale_cookie = (struct sctp_error_stale_cookie *)cause;
11371f0e1344SMichael Tuexen 				stale_time = ntohl(stale_cookie->stale_time);
11381f0e1344SMichael Tuexen 				if (stale_time == 0) {
11391f0e1344SMichael Tuexen 					/* Use an RTT as an approximation. */
11401f0e1344SMichael Tuexen 					(void)SCTP_GETTIME_TIMEVAL(&now);
11411f0e1344SMichael Tuexen 					timevalsub(&now, &asoc->time_entered);
11421f0e1344SMichael Tuexen 					stale_time = (uint64_t)1000000 * (uint64_t)now.tv_sec + (uint64_t)now.tv_usec;
11431f0e1344SMichael Tuexen 					if (stale_time == 0) {
11441f0e1344SMichael Tuexen 						stale_time = 1;
11451f0e1344SMichael Tuexen 					}
11461f0e1344SMichael Tuexen 				}
11471f0e1344SMichael Tuexen 				/*
11481f0e1344SMichael Tuexen 				 * stale_time is in usec, convert it to
11491f0e1344SMichael Tuexen 				 * msec. Round upwards, to ensure that it is
11501f0e1344SMichael Tuexen 				 * non-zero.
11511f0e1344SMichael Tuexen 				 */
11521f0e1344SMichael Tuexen 				stale_time = (stale_time + 999) / 1000;
11531f0e1344SMichael Tuexen 				/* Double it, to be more robust on RTX. */
11541f0e1344SMichael Tuexen 				stale_time = 2 * stale_time;
11551f0e1344SMichael Tuexen 				asoc->cookie_preserve_req = (uint32_t)stale_time;
11561f0e1344SMichael Tuexen 				if (asoc->overall_error_count == 0) {
11571f0e1344SMichael Tuexen 					sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered,
11581f0e1344SMichael Tuexen 					    SCTP_RTT_FROM_NON_DATA);
11591f0e1344SMichael Tuexen 				}
11601f0e1344SMichael Tuexen 				asoc->overall_error_count = 0;
11611f0e1344SMichael Tuexen 				/* Blast back to INIT state */
1162830d754dSRandall Stewart 				sctp_toss_old_cookies(stcb, &stcb->asoc);
1163f8829a4aSRandall Stewart 				sctp_stop_all_cookie_timers(stcb);
11641f0e1344SMichael Tuexen 				SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
11651f0e1344SMichael Tuexen 				(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
1166ceaad40aSRandall Stewart 				sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1167f8829a4aSRandall Stewart 			}
1168f8829a4aSRandall Stewart 			break;
1169f8829a4aSRandall Stewart 		case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1170f8829a4aSRandall Stewart 			/*
1171f8829a4aSRandall Stewart 			 * Nothing we can do here, we don't do hostname
1172f8829a4aSRandall Stewart 			 * addresses so if the peer does not like my IPv6
1173f8829a4aSRandall Stewart 			 * (or IPv4 for that matter) it does not matter. If
1174f8829a4aSRandall Stewart 			 * they don't support that type of address, they can
1175f8829a4aSRandall Stewart 			 * NOT possibly get that packet type... i.e. with no
1176cd0a4ff6SPedro F. Giffuni 			 * IPv6 you can't receive a IPv6 packet. so we can
1177f8829a4aSRandall Stewart 			 * safely ignore this one. If we ever added support
1178f8829a4aSRandall Stewart 			 * for HOSTNAME Addresses, then we would need to do
1179f8829a4aSRandall Stewart 			 * something here.
1180f8829a4aSRandall Stewart 			 */
1181f8829a4aSRandall Stewart 			break;
1182f8829a4aSRandall Stewart 		case SCTP_CAUSE_UNRECOG_CHUNK:
11833e87bccdSMichael Tuexen 			if (cause_length >= sizeof(struct sctp_error_unrecognized_chunk)) {
11843e87bccdSMichael Tuexen 				struct sctp_error_unrecognized_chunk *unrec_chunk;
11853e87bccdSMichael Tuexen 
11863e87bccdSMichael Tuexen 				unrec_chunk = (struct sctp_error_unrecognized_chunk *)cause;
11876fb7b4fbSMichael Tuexen 				sctp_process_unrecog_chunk(stcb, unrec_chunk->ch.chunk_type);
11883e87bccdSMichael Tuexen 			}
1189f8829a4aSRandall Stewart 			break;
1190f8829a4aSRandall Stewart 		case SCTP_CAUSE_UNRECOG_PARAM:
11913e87bccdSMichael Tuexen 			/* XXX: We only consider the first parameter */
11923e87bccdSMichael Tuexen 			if (cause_length >= sizeof(struct sctp_error_cause) + sizeof(struct sctp_paramhdr)) {
11933e87bccdSMichael Tuexen 				struct sctp_paramhdr *unrec_parameter;
11943e87bccdSMichael Tuexen 
11953e87bccdSMichael Tuexen 				unrec_parameter = (struct sctp_paramhdr *)(cause + 1);
11963e87bccdSMichael Tuexen 				sctp_process_unrecog_param(stcb, ntohs(unrec_parameter->param_type));
11973e87bccdSMichael Tuexen 			}
1198f8829a4aSRandall Stewart 			break;
1199f8829a4aSRandall Stewart 		case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1200f8829a4aSRandall Stewart 			/*
1201f8829a4aSRandall Stewart 			 * We ignore this since the timer will drive out a
1202f8829a4aSRandall Stewart 			 * new cookie anyway and there timer will drive us
1203f8829a4aSRandall Stewart 			 * to send a SHUTDOWN_COMPLETE. We can't send one
1204f8829a4aSRandall Stewart 			 * here since we don't have their tag.
1205f8829a4aSRandall Stewart 			 */
1206f8829a4aSRandall Stewart 			break;
1207f8829a4aSRandall Stewart 		case SCTP_CAUSE_DELETING_LAST_ADDR:
1208f8829a4aSRandall Stewart 		case SCTP_CAUSE_RESOURCE_SHORTAGE:
1209f8829a4aSRandall Stewart 		case SCTP_CAUSE_DELETING_SRC_ADDR:
1210f8829a4aSRandall Stewart 			/*
1211f8829a4aSRandall Stewart 			 * We should NOT get these here, but in a
121293164cf9SRandall Stewart 			 * ASCONF-ACK.
1213f8829a4aSRandall Stewart 			 */
12143e87bccdSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a error cause with code %u.\n",
12153e87bccdSMichael Tuexen 			    cause_code);
1216f8829a4aSRandall Stewart 			break;
1217f8829a4aSRandall Stewart 		case SCTP_CAUSE_OUT_OF_RESC:
1218f8829a4aSRandall Stewart 			/*
1219f8829a4aSRandall Stewart 			 * And what, pray tell do we do with the fact that
1220f8829a4aSRandall Stewart 			 * the peer is out of resources? Not really sure we
122193164cf9SRandall Stewart 			 * could do anything but abort. I suspect this
1222f8829a4aSRandall Stewart 			 * should have came WITH an abort instead of in a
1223f8829a4aSRandall Stewart 			 * OP-ERROR.
1224f8829a4aSRandall Stewart 			 */
1225f8829a4aSRandall Stewart 			break;
1226f8829a4aSRandall Stewart 		default:
12273e87bccdSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown code 0x%x\n",
12283e87bccdSMichael Tuexen 			    cause_code);
1229f8829a4aSRandall Stewart 			break;
1230f8829a4aSRandall Stewart 		}
12313e87bccdSMichael Tuexen 		adjust = SCTP_SIZE32(cause_length);
12323e87bccdSMichael Tuexen 		if (remaining_length >= adjust) {
12333e87bccdSMichael Tuexen 			remaining_length -= adjust;
12343e87bccdSMichael Tuexen 		} else {
12353e87bccdSMichael Tuexen 			remaining_length = 0;
1236f8829a4aSRandall Stewart 		}
12373e87bccdSMichael Tuexen 		cause = (struct sctp_error_cause *)((caddr_t)cause + adjust);
12383e87bccdSMichael Tuexen 	}
12393e87bccdSMichael Tuexen 	sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, code, ch, SCTP_SO_NOT_LOCKED);
1240f8829a4aSRandall Stewart 	return (0);
1241f8829a4aSRandall Stewart }
1242f8829a4aSRandall Stewart 
1243f8829a4aSRandall Stewart static int
sctp_handle_init_ack(struct mbuf * m,int iphlen,int offset,struct sockaddr * src,struct sockaddr * dst,struct sctphdr * sh,struct sctp_init_ack_chunk * cp,struct sctp_tcb * stcb,struct sctp_nets * net,int * abort_no_unlock,uint8_t mflowtype,uint32_t mflowid,uint32_t vrf_id)1244b1754ad1SMichael Tuexen sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1245b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
1246f30ac432SMichael Tuexen     struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1247f30ac432SMichael Tuexen     struct sctp_nets *net, int *abort_no_unlock,
1248457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
1249f30ac432SMichael Tuexen     uint32_t vrf_id)
1250f8829a4aSRandall Stewart {
1251f8829a4aSRandall Stewart 	struct sctp_init_ack *init_ack;
1252f8829a4aSRandall Stewart 	struct mbuf *op_err;
1253f8829a4aSRandall Stewart 
1254ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
1255ad81507eSRandall Stewart 	    "sctp_handle_init_ack: handling INIT-ACK\n");
1256ad81507eSRandall Stewart 
1257f8829a4aSRandall Stewart 	if (stcb == NULL) {
1258ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
1259ad81507eSRandall Stewart 		    "sctp_handle_init_ack: TCB is null\n");
1260f8829a4aSRandall Stewart 		return (-1);
1261f8829a4aSRandall Stewart 	}
1262a3665770SMichael Tuexen 	/* Only process the INIT-ACK chunk in COOKIE WAIT state. */
1263a3665770SMichael Tuexen 	if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) {
1264f8829a4aSRandall Stewart 		init_ack = &cp->init;
1265059ec222SMichael Tuexen 		/* Validate parameters. */
1266059ec222SMichael Tuexen 		if ((ntohl(init_ack->initiate_tag) == 0) ||
1267059ec222SMichael Tuexen 		    (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) ||
1268059ec222SMichael Tuexen 		    (ntohs(init_ack->num_inbound_streams) == 0) ||
1269059ec222SMichael Tuexen 		    (ntohs(init_ack->num_outbound_streams) == 0)) {
1270a3665770SMichael Tuexen 			/* One of the mandatory parameters is illegal. */
1271ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1272b1754ad1SMichael Tuexen 			sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1273b1754ad1SMichael Tuexen 			    src, dst, sh, op_err,
1274457b4b88SMichael Tuexen 			    mflowtype, mflowid,
1275f30ac432SMichael Tuexen 			    vrf_id, net->port);
1276bff64a4dSRandall Stewart 			*abort_no_unlock = 1;
1277f8829a4aSRandall Stewart 			return (-1);
1278f8829a4aSRandall Stewart 		}
1279f8829a4aSRandall Stewart 		if (stcb->asoc.primary_destination->dest_state &
1280f8829a4aSRandall Stewart 		    SCTP_ADDR_UNCONFIRMED) {
1281f8829a4aSRandall Stewart 			/*
1282f8829a4aSRandall Stewart 			 * The primary is where we sent the INIT, we can
1283f8829a4aSRandall Stewart 			 * always consider it confirmed when the INIT-ACK is
1284f8829a4aSRandall Stewart 			 * returned. Do this before we load addresses
1285f8829a4aSRandall Stewart 			 * though.
1286f8829a4aSRandall Stewart 			 */
1287f8829a4aSRandall Stewart 			stcb->asoc.primary_destination->dest_state &=
1288f8829a4aSRandall Stewart 			    ~SCTP_ADDR_UNCONFIRMED;
1289f8829a4aSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1290ceaad40aSRandall Stewart 			    stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1291f8829a4aSRandall Stewart 		}
1292b1754ad1SMichael Tuexen 		if (sctp_process_init_ack(m, iphlen, offset, src, dst, sh, cp, stcb,
1293f30ac432SMichael Tuexen 		    net, abort_no_unlock,
1294457b4b88SMichael Tuexen 		    mflowtype, mflowid,
1295f30ac432SMichael Tuexen 		    vrf_id) < 0) {
1296f8829a4aSRandall Stewart 			/* error in parsing parameters */
1297f8829a4aSRandall Stewart 			return (-1);
1298f8829a4aSRandall Stewart 		}
1299a3665770SMichael Tuexen 		/* Update our state. */
1300ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1301839d21d6SMichael Tuexen 		SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_ECHOED);
1302f8829a4aSRandall Stewart 
1303a3665770SMichael Tuexen 		/* Reset the RTO calculation. */
1304b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1305c4739e2fSRandall Stewart 			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1306c4739e2fSRandall Stewart 			    stcb->asoc.overall_error_count,
1307c4739e2fSRandall Stewart 			    0,
1308c4739e2fSRandall Stewart 			    SCTP_FROM_SCTP_INPUT,
1309c4739e2fSRandall Stewart 			    __LINE__);
1310c4739e2fSRandall Stewart 		}
1311f8829a4aSRandall Stewart 		stcb->asoc.overall_error_count = 0;
13126e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1313f8829a4aSRandall Stewart 		/*
1314a3665770SMichael Tuexen 		 * Collapse the init timer back in case of a exponential
1315a3665770SMichael Tuexen 		 * backoff.
1316f8829a4aSRandall Stewart 		 */
1317f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1318f8829a4aSRandall Stewart 		    stcb, net);
1319f8829a4aSRandall Stewart 		/*
1320a3665770SMichael Tuexen 		 * The output routine at the end of the inbound data
1321a3665770SMichael Tuexen 		 * processing will cause the cookie to be sent.
1322f8829a4aSRandall Stewart 		 */
1323ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1324f8829a4aSRandall Stewart 		return (0);
1325a3665770SMichael Tuexen 	} else {
1326a3665770SMichael Tuexen 		return (-1);
1327a3665770SMichael Tuexen 	}
1328f8829a4aSRandall Stewart }
1329f8829a4aSRandall Stewart 
1330830d754dSRandall Stewart static struct sctp_tcb *
1331830d754dSRandall Stewart sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1332b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
1333830d754dSRandall Stewart     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1334830d754dSRandall Stewart     struct sctp_inpcb *inp, struct sctp_nets **netp,
1335830d754dSRandall Stewart     struct sockaddr *init_src, int *notification,
1336830d754dSRandall Stewart     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1337457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
1338830d754dSRandall Stewart     uint32_t vrf_id, uint16_t port);
1339830d754dSRandall Stewart 
1340f8829a4aSRandall Stewart /*
1341f8829a4aSRandall Stewart  * handle a state cookie for an existing association m: input packet mbuf
1342f8829a4aSRandall Stewart  * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1343f8829a4aSRandall Stewart  * "split" mbuf and the cookie signature does not exist offset: offset into
1344f8829a4aSRandall Stewart  * mbuf to the cookie-echo chunk
1345f8829a4aSRandall Stewart  */
1346f8829a4aSRandall Stewart static struct sctp_tcb *
sctp_process_cookie_existing(struct mbuf * m,int iphlen,int offset,struct sockaddr * src,struct sockaddr * dst,struct sctphdr * sh,struct sctp_state_cookie * cookie,int cookie_len,struct sctp_inpcb * inp,struct sctp_tcb * stcb,struct sctp_nets ** netp,struct sockaddr * init_src,int * notification,int auth_skipped,uint32_t auth_offset,uint32_t auth_len,uint8_t mflowtype,uint32_t mflowid,uint32_t vrf_id,uint16_t port)1347f8829a4aSRandall Stewart sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1348b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
1349f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1350830d754dSRandall Stewart     struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
13517215cc1bSMichael Tuexen     struct sockaddr *init_src, int *notification,
1352f30ac432SMichael Tuexen     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1353457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
1354f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
1355f8829a4aSRandall Stewart {
1356f8829a4aSRandall Stewart 	struct sctp_association *asoc;
1357f8829a4aSRandall Stewart 	struct sctp_init_chunk *init_cp, init_buf;
1358f8829a4aSRandall Stewart 	struct sctp_init_ack_chunk *initack_cp, initack_buf;
1359aa6db9a0SMichael Tuexen 	struct sctp_asconf_addr *aparam, *naparam;
1360aa6db9a0SMichael Tuexen 	struct sctp_asconf_ack *aack, *naack;
1361aa6db9a0SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
1362aa6db9a0SMichael Tuexen 	struct sctp_stream_reset_list *strrst, *nstrrst;
1363aa6db9a0SMichael Tuexen 	struct sctp_queued_to_read *sq, *nsq;
1364830d754dSRandall Stewart 	struct sctp_nets *net;
1365830d754dSRandall Stewart 	struct mbuf *op_err;
1366a5d547adSRandall Stewart 	int init_offset, initack_offset, i;
1367f8829a4aSRandall Stewart 	int retval;
13687f34832bSRandall Stewart 	int spec_flag = 0;
13694c9179adSRandall Stewart 	uint32_t how_indx;
1370f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
1371f0396ad1SMichael Tuexen 	int j;
1372f0396ad1SMichael Tuexen #endif
1373f0396ad1SMichael Tuexen 
1374830d754dSRandall Stewart 	net = *netp;
1375f8829a4aSRandall Stewart 	/* I know that the TCB is non-NULL from the caller */
1376f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
1377f42a358aSRandall Stewart 	for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
137844b7479bSRandall Stewart 		if (asoc->cookie_how[how_indx] == 0)
137944b7479bSRandall Stewart 			break;
138044b7479bSRandall Stewart 	}
138144b7479bSRandall Stewart 	if (how_indx < sizeof(asoc->cookie_how)) {
138244b7479bSRandall Stewart 		asoc->cookie_how[how_indx] = 1;
138344b7479bSRandall Stewart 	}
1384839d21d6SMichael Tuexen 	if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1385f8829a4aSRandall Stewart 		/* SHUTDOWN came in after sending INIT-ACK */
1386f8829a4aSRandall Stewart 		sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1387ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_COOKIE_IN_SHUTDOWN, "");
1388b1754ad1SMichael Tuexen 		sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
1389d089f9b9SMichael Tuexen 		    mflowtype, mflowid, inp->fibnum,
1390c54a18d2SRandall Stewart 		    vrf_id, net->port);
139144b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
139244b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 2;
139312dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1394f8829a4aSRandall Stewart 		return (NULL);
1395f8829a4aSRandall Stewart 	}
1396f8829a4aSRandall Stewart 	/*
1397f8829a4aSRandall Stewart 	 * find and validate the INIT chunk in the cookie (peer's info) the
1398f8829a4aSRandall Stewart 	 * INIT should start after the cookie-echo header struct (chunk
1399f8829a4aSRandall Stewart 	 * header, state cookie header struct)
1400f8829a4aSRandall Stewart 	 */
1401f8829a4aSRandall Stewart 	init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1402f8829a4aSRandall Stewart 
1403f8829a4aSRandall Stewart 	init_cp = (struct sctp_init_chunk *)
1404f8829a4aSRandall Stewart 	    sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1405f8829a4aSRandall Stewart 	    (uint8_t *)&init_buf);
1406f8829a4aSRandall Stewart 	if (init_cp == NULL) {
1407f8829a4aSRandall Stewart 		/* could not pull a INIT chunk in cookie */
140812dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1409f8829a4aSRandall Stewart 		return (NULL);
1410f8829a4aSRandall Stewart 	}
1411f8829a4aSRandall Stewart 	if (init_cp->ch.chunk_type != SCTP_INITIATION) {
141212dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1413f8829a4aSRandall Stewart 		return (NULL);
1414f8829a4aSRandall Stewart 	}
1415f8829a4aSRandall Stewart 	/*
1416f8829a4aSRandall Stewart 	 * find and validate the INIT-ACK chunk in the cookie (my info) the
1417f8829a4aSRandall Stewart 	 * INIT-ACK follows the INIT chunk
1418f8829a4aSRandall Stewart 	 */
141960990c0cSMichael Tuexen 	initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1420f8829a4aSRandall Stewart 	initack_cp = (struct sctp_init_ack_chunk *)
1421f8829a4aSRandall Stewart 	    sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1422f8829a4aSRandall Stewart 	    (uint8_t *)&initack_buf);
1423f8829a4aSRandall Stewart 	if (initack_cp == NULL) {
1424f8829a4aSRandall Stewart 		/* could not pull INIT-ACK chunk in cookie */
142512dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1426f8829a4aSRandall Stewart 		return (NULL);
1427f8829a4aSRandall Stewart 	}
1428f8829a4aSRandall Stewart 	if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
142912dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1430f8829a4aSRandall Stewart 		return (NULL);
1431f8829a4aSRandall Stewart 	}
1432f8829a4aSRandall Stewart 	if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1433f8829a4aSRandall Stewart 	    (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1434f8829a4aSRandall Stewart 		/*
1435f8829a4aSRandall Stewart 		 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1436f8829a4aSRandall Stewart 		 * to get into the OPEN state
1437f8829a4aSRandall Stewart 		 */
143844b7479bSRandall Stewart 		if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1439851b7298SRandall Stewart 			/*-
1440851b7298SRandall Stewart 			 * Opps, this means that we somehow generated two vtag's
1441851b7298SRandall Stewart 			 * the same. I.e. we did:
1442851b7298SRandall Stewart 			 *  Us               Peer
1443851b7298SRandall Stewart 			 *   <---INIT(tag=a)------
1444851b7298SRandall Stewart 			 *   ----INIT-ACK(tag=t)-->
1445851b7298SRandall Stewart 			 *   ----INIT(tag=t)------> *1
1446851b7298SRandall Stewart 			 *   <---INIT-ACK(tag=a)---
1447851b7298SRandall Stewart 			 *   <----CE(tag=t)------------- *2
1448851b7298SRandall Stewart 			 *
1449851b7298SRandall Stewart 			 * At point *1 we should be generating a different
1450851b7298SRandall Stewart 			 * tag t'. Which means we would throw away the CE and send
1451851b7298SRandall Stewart 			 * ours instead. Basically this is case C (throw away side).
1452851b7298SRandall Stewart 			 */
1453851b7298SRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
1454851b7298SRandall Stewart 				asoc->cookie_how[how_indx] = 17;
145512dda000SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
1456851b7298SRandall Stewart 			return (NULL);
145744b7479bSRandall Stewart 		}
1458839d21d6SMichael Tuexen 		switch (SCTP_GET_STATE(stcb)) {
1459f8829a4aSRandall Stewart 		case SCTP_STATE_COOKIE_WAIT:
146044b7479bSRandall Stewart 		case SCTP_STATE_COOKIE_ECHOED:
1461f8829a4aSRandall Stewart 			/*
146217205eccSRandall Stewart 			 * INIT was sent but got a COOKIE_ECHO with the
146344b7479bSRandall Stewart 			 * correct tags... just accept it...but we must
146444b7479bSRandall Stewart 			 * process the init so that we can make sure we have
146544b7479bSRandall Stewart 			 * the right seq no's.
1466f8829a4aSRandall Stewart 			 */
1467f8829a4aSRandall Stewart 			/* First we must process the INIT !! */
14685f2e1835SMichael Tuexen 			if (sctp_process_init(init_cp, stcb) < 0) {
146944b7479bSRandall Stewart 				if (how_indx < sizeof(asoc->cookie_how))
147044b7479bSRandall Stewart 					asoc->cookie_how[how_indx] = 3;
14715f2e1835SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
14725f2e1835SMichael Tuexen 				SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_init() failed\n");
14735f2e1835SMichael Tuexen 				sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
14745f2e1835SMichael Tuexen 				    src, dst, sh, op_err,
14755f2e1835SMichael Tuexen 				    mflowtype, mflowid,
14765f2e1835SMichael Tuexen 				    vrf_id, net->port);
1477f8829a4aSRandall Stewart 				return (NULL);
1478f8829a4aSRandall Stewart 			}
1479f8829a4aSRandall Stewart 			/* we have already processed the INIT so no problem */
1480b7d130beSMichael Tuexen 			sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp,
1481b7d130beSMichael Tuexen 			    stcb, net,
1482b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1483b7d130beSMichael Tuexen 			sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp,
1484b7d130beSMichael Tuexen 			    stcb, net,
1485b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1486f8829a4aSRandall Stewart 			/* update current state */
1487839d21d6SMichael Tuexen 			if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)
1488f42a358aSRandall Stewart 				SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1489f42a358aSRandall Stewart 			else
1490f42a358aSRandall Stewart 				SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1491c4739e2fSRandall Stewart 
1492839d21d6SMichael Tuexen 			SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1493f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1494a5d547adSRandall Stewart 			sctp_stop_all_cookie_timers(stcb);
1495f8829a4aSRandall Stewart 			if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1496f8829a4aSRandall Stewart 			    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
14975d08768aSMichael Tuexen 			    (!SCTP_IS_LISTENING(inp))) {
1498f8829a4aSRandall Stewart 				/*
1499f8829a4aSRandall Stewart 				 * Here is where collision would go if we
1500f8829a4aSRandall Stewart 				 * did a connect() and instead got a
1501f8829a4aSRandall Stewart 				 * init/init-ack/cookie done before the
1502f8829a4aSRandall Stewart 				 * init-ack came back..
1503f8829a4aSRandall Stewart 				 */
1504a5c2009dSMichael Tuexen 				sctp_pcb_add_flags(stcb->sctp_ep, SCTP_PCB_FLAGS_CONNECTED);
1505ceaad40aSRandall Stewart 				soisconnected(stcb->sctp_socket);
1506f8829a4aSRandall Stewart 			}
1507f8829a4aSRandall Stewart 			/* notify upper layer */
1508f8829a4aSRandall Stewart 			*notification = SCTP_NOTIFY_ASSOC_UP;
1509f8829a4aSRandall Stewart 			net->hb_responded = 1;
1510f8829a4aSRandall Stewart 			if (stcb->asoc.sctp_autoclose_ticks &&
1511f8829a4aSRandall Stewart 			    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1512f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1513f8829a4aSRandall Stewart 				    inp, stcb, NULL);
1514f8829a4aSRandall Stewart 			}
1515f8829a4aSRandall Stewart 			break;
1516f8829a4aSRandall Stewart 		default:
1517f8829a4aSRandall Stewart 			/*
1518f8829a4aSRandall Stewart 			 * we're in the OPEN state (or beyond), so peer must
1519f8829a4aSRandall Stewart 			 * have simply lost the COOKIE-ACK
1520f8829a4aSRandall Stewart 			 */
1521f8829a4aSRandall Stewart 			break;
1522f8829a4aSRandall Stewart 		}		/* end switch */
1523a5d547adSRandall Stewart 		sctp_stop_all_cookie_timers(stcb);
1524655c200cSAlexander Motin 		if ((retval = sctp_load_addresses_from_init(stcb, m,
1525f8829a4aSRandall Stewart 		    init_offset + sizeof(struct sctp_init_chunk),
15265f2e1835SMichael Tuexen 		    initack_offset, src, dst, init_src, stcb->asoc.port)) < 0) {
152744b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
152844b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 4;
15295f2e1835SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
15305f2e1835SMichael Tuexen 			    "Problem with address parameters");
15315f2e1835SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1,
15325f2e1835SMichael Tuexen 			    "Load addresses from INIT causes an abort %d\n",
15335f2e1835SMichael Tuexen 			    retval);
15345f2e1835SMichael Tuexen 			sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
15355f2e1835SMichael Tuexen 			    src, dst, sh, op_err,
15365f2e1835SMichael Tuexen 			    mflowtype, mflowid,
15375f2e1835SMichael Tuexen 			    vrf_id, net->port);
1538f8829a4aSRandall Stewart 			return (NULL);
1539f8829a4aSRandall Stewart 		}
1540f8829a4aSRandall Stewart 		/* respond with a COOKIE-ACK */
1541a5d547adSRandall Stewart 		sctp_toss_old_cookies(stcb, asoc);
1542f8829a4aSRandall Stewart 		sctp_send_cookie_ack(stcb);
154344b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
154444b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 5;
1545f8829a4aSRandall Stewart 		return (stcb);
154617205eccSRandall Stewart 	}
15470053ed28SMichael Tuexen 
1548f8829a4aSRandall Stewart 	if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1549f8829a4aSRandall Stewart 	    ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1550f8829a4aSRandall Stewart 	    cookie->tie_tag_my_vtag == 0 &&
1551f8829a4aSRandall Stewart 	    cookie->tie_tag_peer_vtag == 0) {
1552f8829a4aSRandall Stewart 		/*
1553f8829a4aSRandall Stewart 		 * case C in Section 5.2.4 Table 2: XMOO silently discard
1554f8829a4aSRandall Stewart 		 */
155544b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
155644b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 6;
155712dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1558f8829a4aSRandall Stewart 		return (NULL);
1559f8829a4aSRandall Stewart 	}
1560830d754dSRandall Stewart 	/*
1561830d754dSRandall Stewart 	 * If nat support, and the below and stcb is established, send back
1562830d754dSRandall Stewart 	 * a ABORT(colliding state) if we are established.
1563830d754dSRandall Stewart 	 */
1564839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) &&
1565830d754dSRandall Stewart 	    (asoc->peer_supports_nat) &&
1566830d754dSRandall Stewart 	    ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1567830d754dSRandall Stewart 	    ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1568830d754dSRandall Stewart 	    (asoc->peer_vtag == 0)))) {
1569830d754dSRandall Stewart 		/*
1570830d754dSRandall Stewart 		 * Special case - Peer's support nat. We may have two init's
1571830d754dSRandall Stewart 		 * that we gave out the same tag on since one was not
1572830d754dSRandall Stewart 		 * established.. i.e. we get INIT from host-1 behind the nat
1573830d754dSRandall Stewart 		 * and we respond tag-a, we get a INIT from host-2 behind
1574830d754dSRandall Stewart 		 * the nat and we get tag-a again. Then we bring up host-1
1575830d754dSRandall Stewart 		 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1576830d754dSRandall Stewart 		 * Now we have colliding state. We must send an abort here
1577830d754dSRandall Stewart 		 * with colliding state indication.
1578830d754dSRandall Stewart 		 */
1579ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_NAT_COLLIDING_STATE, "");
1580b1754ad1SMichael Tuexen 		sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
1581d089f9b9SMichael Tuexen 		    mflowtype, mflowid, inp->fibnum,
1582f30ac432SMichael Tuexen 		    vrf_id, port);
158312dda000SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
1584830d754dSRandall Stewart 		return (NULL);
1585830d754dSRandall Stewart 	}
1586830d754dSRandall Stewart 	if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1587830d754dSRandall Stewart 	    ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1588830d754dSRandall Stewart 	    (asoc->peer_vtag == 0))) {
1589f8829a4aSRandall Stewart 		/*
1590f8829a4aSRandall Stewart 		 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1591f8829a4aSRandall Stewart 		 * should be ok, re-accept peer info
1592f8829a4aSRandall Stewart 		 */
159344b7479bSRandall Stewart 		if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
159444b7479bSRandall Stewart 			/*
159544b7479bSRandall Stewart 			 * Extension of case C. If we hit this, then the
159644b7479bSRandall Stewart 			 * random number generator returned the same vtag
159744b7479bSRandall Stewart 			 * when we first sent our INIT-ACK and when we later
159844b7479bSRandall Stewart 			 * sent our INIT. The side with the seq numbers that
1599e7e65008SMichael Tuexen 			 * are different will be the one that normally would
1600e7e65008SMichael Tuexen 			 * have hit case C. This in effect "extends" our
1601e7e65008SMichael Tuexen 			 * vtags in this collision case to be 64 bits. The
1602e7e65008SMichael Tuexen 			 * same collision could occur aka you get both vtag
1603e7e65008SMichael Tuexen 			 * and seq number the same twice in a row.. but is
1604e7e65008SMichael Tuexen 			 * much less likely. If it did happen then we would
1605e7e65008SMichael Tuexen 			 * proceed through and bring up the assoc.. we may
1606e7e65008SMichael Tuexen 			 * end up with the wrong stream setup however..
160744b7479bSRandall Stewart 			 * which would be bad.. but there is no way to
160844b7479bSRandall Stewart 			 * tell.. until we send on a stream that does not
160944b7479bSRandall Stewart 			 * exist :-)
161044b7479bSRandall Stewart 			 */
161144b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
161244b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 7;
161344b7479bSRandall Stewart 
161412dda000SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
161544b7479bSRandall Stewart 			return (NULL);
161644b7479bSRandall Stewart 		}
161744b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
161844b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 8;
1619b7d130beSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1620b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1621f8829a4aSRandall Stewart 		sctp_stop_all_cookie_timers(stcb);
1622f8829a4aSRandall Stewart 		/*
1623f8829a4aSRandall Stewart 		 * since we did not send a HB make sure we don't double
1624f8829a4aSRandall Stewart 		 * things
1625f8829a4aSRandall Stewart 		 */
1626f8829a4aSRandall Stewart 		net->hb_responded = 1;
1627f8829a4aSRandall Stewart 		if (stcb->asoc.sctp_autoclose_ticks &&
1628f8829a4aSRandall Stewart 		    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1629f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1630f8829a4aSRandall Stewart 			    NULL);
1631f8829a4aSRandall Stewart 		}
1632f8829a4aSRandall Stewart 		asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
16330066de1cSMichael Tuexen 		if (asoc->pre_open_streams < asoc->streamoutcnt) {
16340066de1cSMichael Tuexen 			asoc->pre_open_streams = asoc->streamoutcnt;
16350066de1cSMichael Tuexen 		}
1636f8829a4aSRandall Stewart 
16377f34832bSRandall Stewart 		if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
16387f34832bSRandall Stewart 			/*
16397f34832bSRandall Stewart 			 * Ok the peer probably discarded our data (if we
16407f34832bSRandall Stewart 			 * echoed a cookie+data). So anything on the
16417f34832bSRandall Stewart 			 * sent_queue should be marked for retransmit, we
16427f34832bSRandall Stewart 			 * may not get something to kick us so it COULD
16437f34832bSRandall Stewart 			 * still take a timeout to move these.. but it can't
16447f34832bSRandall Stewart 			 * hurt to mark them.
16457f34832bSRandall Stewart 			 */
16467f34832bSRandall Stewart 
16477f34832bSRandall Stewart 			TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
16487f34832bSRandall Stewart 				if (chk->sent < SCTP_DATAGRAM_RESEND) {
16497f34832bSRandall Stewart 					chk->sent = SCTP_DATAGRAM_RESEND;
1650b54d3a6cSRandall Stewart 					sctp_flight_size_decrease(chk);
1651b54d3a6cSRandall Stewart 					sctp_total_flight_decrease(stcb, chk);
16525e54f665SRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
16537f34832bSRandall Stewart 					spec_flag++;
16547f34832bSRandall Stewart 				}
16557f34832bSRandall Stewart 			}
16567f34832bSRandall Stewart 		}
1657f8829a4aSRandall Stewart 		/* process the INIT info (peer's info) */
16585f2e1835SMichael Tuexen 		if (sctp_process_init(init_cp, stcb) < 0) {
165944b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
166044b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 9;
16615f2e1835SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
16625f2e1835SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_init() failed\n");
16635f2e1835SMichael Tuexen 			sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
16645f2e1835SMichael Tuexen 			    src, dst, sh, op_err,
16655f2e1835SMichael Tuexen 			    mflowtype, mflowid,
16665f2e1835SMichael Tuexen 			    vrf_id, net->port);
1667f8829a4aSRandall Stewart 			return (NULL);
1668f8829a4aSRandall Stewart 		}
16695f2e1835SMichael Tuexen 		if ((retval = sctp_load_addresses_from_init(stcb, m,
1670f8829a4aSRandall Stewart 		    init_offset + sizeof(struct sctp_init_chunk),
16715f2e1835SMichael Tuexen 		    initack_offset, src, dst, init_src, stcb->asoc.port)) < 0) {
167244b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
167344b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 10;
16745f2e1835SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
16755f2e1835SMichael Tuexen 			    "Problem with address parameters");
16765f2e1835SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1,
16775f2e1835SMichael Tuexen 			    "Load addresses from INIT causes an abort %d\n",
16785f2e1835SMichael Tuexen 			    retval);
16795f2e1835SMichael Tuexen 			sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
16805f2e1835SMichael Tuexen 			    src, dst, sh, op_err,
16815f2e1835SMichael Tuexen 			    mflowtype, mflowid,
16825f2e1835SMichael Tuexen 			    vrf_id, net->port);
1683f8829a4aSRandall Stewart 			return (NULL);
1684f8829a4aSRandall Stewart 		}
1685839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
1686839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
1687f8829a4aSRandall Stewart 			*notification = SCTP_NOTIFY_ASSOC_UP;
1688f8829a4aSRandall Stewart 
1689f8829a4aSRandall Stewart 			if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1690f8829a4aSRandall Stewart 			    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
16915d08768aSMichael Tuexen 			    (!SCTP_IS_LISTENING(inp))) {
1692a5c2009dSMichael Tuexen 				sctp_pcb_add_flags(stcb->sctp_ep, SCTP_PCB_FLAGS_CONNECTED);
1693ceaad40aSRandall Stewart 				soisconnected(stcb->sctp_socket);
1694f8829a4aSRandall Stewart 			}
1695839d21d6SMichael Tuexen 			if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)
1696f42a358aSRandall Stewart 				SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1697f42a358aSRandall Stewart 			else
1698f42a358aSRandall Stewart 				SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1699f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1700839d21d6SMichael Tuexen 		} else if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
1701f42a358aSRandall Stewart 			SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1702f42a358aSRandall Stewart 		} else {
1703f42a358aSRandall Stewart 			SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1704f8829a4aSRandall Stewart 		}
1705839d21d6SMichael Tuexen 		SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1706f8829a4aSRandall Stewart 		sctp_stop_all_cookie_timers(stcb);
1707a5d547adSRandall Stewart 		sctp_toss_old_cookies(stcb, asoc);
1708f8829a4aSRandall Stewart 		sctp_send_cookie_ack(stcb);
17097f34832bSRandall Stewart 		if (spec_flag) {
17107f34832bSRandall Stewart 			/*
17117f34832bSRandall Stewart 			 * only if we have retrans set do we do this. What
17127f34832bSRandall Stewart 			 * this call does is get only the COOKIE-ACK out and
17137f34832bSRandall Stewart 			 * then when we return the normal call to
17147f34832bSRandall Stewart 			 * sctp_chunk_output will get the retrans out behind
17157f34832bSRandall Stewart 			 * this.
17167f34832bSRandall Stewart 			 */
1717ceaad40aSRandall Stewart 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
17187f34832bSRandall Stewart 		}
171944b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
172044b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 11;
172144b7479bSRandall Stewart 
1722f8829a4aSRandall Stewart 		return (stcb);
1723f8829a4aSRandall Stewart 	}
1724f8829a4aSRandall Stewart 	if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1725f8829a4aSRandall Stewart 	    ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1726f8829a4aSRandall Stewart 	    cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1727f8829a4aSRandall Stewart 	    cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1728f8829a4aSRandall Stewart 	    cookie->tie_tag_peer_vtag != 0) {
1729f8829a4aSRandall Stewart 		struct sctpasochead *head;
173056f778aaSMichael Tuexen 
1731830d754dSRandall Stewart 		if (asoc->peer_supports_nat) {
1732eec6aed5SMichael Tuexen 			struct sctp_tcb *local_stcb;
1733eec6aed5SMichael Tuexen 
1734830d754dSRandall Stewart 			/*
173556f778aaSMichael Tuexen 			 * This is a gross gross hack. Just call the
1736830d754dSRandall Stewart 			 * cookie_new code since we are allowing a duplicate
1737830d754dSRandall Stewart 			 * association. I hope this works...
1738830d754dSRandall Stewart 			 */
1739eec6aed5SMichael Tuexen 			local_stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst,
1740b1754ad1SMichael Tuexen 			    sh, cookie, cookie_len,
1741830d754dSRandall Stewart 			    inp, netp, init_src, notification,
1742830d754dSRandall Stewart 			    auth_skipped, auth_offset, auth_len,
1743457b4b88SMichael Tuexen 			    mflowtype, mflowid,
1744eec6aed5SMichael Tuexen 			    vrf_id, port);
1745eec6aed5SMichael Tuexen 			if (local_stcb == NULL) {
1746eec6aed5SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
1747eec6aed5SMichael Tuexen 			}
1748eec6aed5SMichael Tuexen 			return (local_stcb);
1749830d754dSRandall Stewart 		}
1750f8829a4aSRandall Stewart 		/*
1751f8829a4aSRandall Stewart 		 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1752f8829a4aSRandall Stewart 		 */
1753a5d547adSRandall Stewart 		/* temp code */
175444b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
175544b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 12;
1756a89481d3SMichael Tuexen 		sctp_stop_association_timers(stcb, false);
1757f8829a4aSRandall Stewart 		/* notify upper layer */
1758f8829a4aSRandall Stewart 		*notification = SCTP_NOTIFY_ASSOC_RESTART;
1759a5d547adSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
1760839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) != SCTP_STATE_OPEN) &&
1761839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1762839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT)) {
1763f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1764f42a358aSRandall Stewart 		}
1765839d21d6SMichael Tuexen 		if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
1766f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1767839d21d6SMichael Tuexen 		} else if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) {
1768f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1769f42a358aSRandall Stewart 		}
1770139bc87fSRandall Stewart 		if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1771839d21d6SMichael Tuexen 			SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1772139bc87fSRandall Stewart 
1773839d21d6SMichael Tuexen 		} else if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) {
1774139bc87fSRandall Stewart 			/* move to OPEN state, if not in SHUTDOWN_SENT */
1775839d21d6SMichael Tuexen 			SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1776139bc87fSRandall Stewart 		}
17770066de1cSMichael Tuexen 		if (asoc->pre_open_streams < asoc->streamoutcnt) {
17780066de1cSMichael Tuexen 			asoc->pre_open_streams = asoc->streamoutcnt;
17790066de1cSMichael Tuexen 		}
1780139bc87fSRandall Stewart 		asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1781139bc87fSRandall Stewart 		asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1782c54a18d2SRandall Stewart 		asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1783139bc87fSRandall Stewart 		asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1784139bc87fSRandall Stewart 		asoc->str_reset_seq_in = asoc->init_seq_number;
1785139bc87fSRandall Stewart 		asoc->advanced_peer_ack_point = asoc->last_acked_seq;
17866f155d69SMichael Tuexen 		asoc->send_sack = 1;
1787a89481d3SMichael Tuexen 		asoc->data_pkts_seen = 0;
1788a89481d3SMichael Tuexen 		asoc->last_data_chunk_from = NULL;
1789a89481d3SMichael Tuexen 		asoc->last_control_chunk_from = NULL;
1790a89481d3SMichael Tuexen 		asoc->last_net_cmt_send_started = NULL;
17910696e120SRandall Stewart 		if (asoc->mapping_array) {
1792139bc87fSRandall Stewart 			memset(asoc->mapping_array, 0,
1793139bc87fSRandall Stewart 			    asoc->mapping_array_size);
17940696e120SRandall Stewart 		}
179577acdc25SRandall Stewart 		if (asoc->nr_mapping_array) {
1796830d754dSRandall Stewart 			memset(asoc->nr_mapping_array, 0,
1797b5c16493SMichael Tuexen 			    asoc->mapping_array_size);
1798830d754dSRandall Stewart 		}
1799a5d547adSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
1800a5d547adSRandall Stewart 		SCTP_INP_INFO_WLOCK();
1801a5d547adSRandall Stewart 		SCTP_INP_WLOCK(stcb->sctp_ep);
1802a5d547adSRandall Stewart 		SCTP_TCB_LOCK(stcb);
18033c1ba6f3SMichael Tuexen 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
1804f8829a4aSRandall Stewart 		/* send up all the data */
1805f5d30f7fSMichael Tuexen 		sctp_report_all_outbound(stcb, 0, SCTP_SO_LOCKED);
1806a5d547adSRandall Stewart 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1807325c8c46SMichael Tuexen 			stcb->asoc.strmout[i].chunks_on_queues = 0;
1808f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
1809f0396ad1SMichael Tuexen 			for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1810f0396ad1SMichael Tuexen 				asoc->strmout[i].abandoned_sent[j] = 0;
1811f0396ad1SMichael Tuexen 				asoc->strmout[i].abandoned_unsent[j] = 0;
1812f0396ad1SMichael Tuexen 			}
1813f0396ad1SMichael Tuexen #else
1814f0396ad1SMichael Tuexen 			asoc->strmout[i].abandoned_sent[0] = 0;
1815f0396ad1SMichael Tuexen 			asoc->strmout[i].abandoned_unsent[0] = 0;
1816f0396ad1SMichael Tuexen #endif
181763d5b568SMichael Tuexen 			stcb->asoc.strmout[i].next_mid_ordered = 0;
181863d5b568SMichael Tuexen 			stcb->asoc.strmout[i].next_mid_unordered = 0;
18197a051c0aSMichael Tuexen 			stcb->asoc.strmout[i].sid = i;
1820a5d547adSRandall Stewart 			stcb->asoc.strmout[i].last_msg_incomplete = 0;
1821a5d547adSRandall Stewart 		}
1822aa6db9a0SMichael Tuexen 		TAILQ_FOREACH_SAFE(strrst, &asoc->resetHead, next_resp, nstrrst) {
1823aa6db9a0SMichael Tuexen 			TAILQ_REMOVE(&asoc->resetHead, strrst, next_resp);
1824aa6db9a0SMichael Tuexen 			SCTP_FREE(strrst, SCTP_M_STRESET);
1825aa6db9a0SMichael Tuexen 		}
1826aa6db9a0SMichael Tuexen 		TAILQ_FOREACH_SAFE(sq, &asoc->pending_reply_queue, next, nsq) {
1827aa6db9a0SMichael Tuexen 			TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
1828aa6db9a0SMichael Tuexen 			if (sq->data) {
1829aa6db9a0SMichael Tuexen 				sctp_m_freem(sq->data);
1830aa6db9a0SMichael Tuexen 				sq->data = NULL;
1831aa6db9a0SMichael Tuexen 			}
1832aa6db9a0SMichael Tuexen 			sctp_free_remote_addr(sq->whoFrom);
1833aa6db9a0SMichael Tuexen 			sq->whoFrom = NULL;
1834aa6db9a0SMichael Tuexen 			sq->stcb = NULL;
1835aa6db9a0SMichael Tuexen 			sctp_free_a_readq(stcb, sq);
1836aa6db9a0SMichael Tuexen 		}
1837aa6db9a0SMichael Tuexen 		TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
1838aa6db9a0SMichael Tuexen 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
1839aa6db9a0SMichael Tuexen 			if (chk->data) {
1840aa6db9a0SMichael Tuexen 				sctp_m_freem(chk->data);
1841aa6db9a0SMichael Tuexen 				chk->data = NULL;
1842aa6db9a0SMichael Tuexen 			}
1843aa6db9a0SMichael Tuexen 			if (chk->holds_key_ref)
1844aa6db9a0SMichael Tuexen 				sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
1845aa6db9a0SMichael Tuexen 			sctp_free_remote_addr(chk->whoTo);
1846aa6db9a0SMichael Tuexen 			SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
1847aa6db9a0SMichael Tuexen 			SCTP_DECR_CHK_COUNT();
1848aa6db9a0SMichael Tuexen 		}
1849a89481d3SMichael Tuexen 		asoc->ctrl_queue_cnt = 0;
1850a89481d3SMichael Tuexen 		asoc->str_reset = NULL;
1851a89481d3SMichael Tuexen 		asoc->stream_reset_outstanding = 0;
1852aa6db9a0SMichael Tuexen 		TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
1853aa6db9a0SMichael Tuexen 			TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
1854aa6db9a0SMichael Tuexen 			if (chk->data) {
1855aa6db9a0SMichael Tuexen 				sctp_m_freem(chk->data);
1856aa6db9a0SMichael Tuexen 				chk->data = NULL;
1857aa6db9a0SMichael Tuexen 			}
1858aa6db9a0SMichael Tuexen 			if (chk->holds_key_ref)
1859aa6db9a0SMichael Tuexen 				sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
1860aa6db9a0SMichael Tuexen 			sctp_free_remote_addr(chk->whoTo);
1861aa6db9a0SMichael Tuexen 			SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
1862aa6db9a0SMichael Tuexen 			SCTP_DECR_CHK_COUNT();
1863aa6db9a0SMichael Tuexen 		}
1864aa6db9a0SMichael Tuexen 		TAILQ_FOREACH_SAFE(aparam, &asoc->asconf_queue, next, naparam) {
1865aa6db9a0SMichael Tuexen 			TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
1866aa6db9a0SMichael Tuexen 			SCTP_FREE(aparam, SCTP_M_ASC_ADDR);
1867aa6db9a0SMichael Tuexen 		}
1868aa6db9a0SMichael Tuexen 		TAILQ_FOREACH_SAFE(aack, &asoc->asconf_ack_sent, next, naack) {
1869aa6db9a0SMichael Tuexen 			TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next);
1870aa6db9a0SMichael Tuexen 			if (aack->data != NULL) {
1871aa6db9a0SMichael Tuexen 				sctp_m_freem(aack->data);
1872aa6db9a0SMichael Tuexen 			}
1873aa6db9a0SMichael Tuexen 			SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack);
1874aa6db9a0SMichael Tuexen 		}
187552640d61SMichael Tuexen 		asoc->rcv_edmid = cookie->rcv_edmid;
1876aa6db9a0SMichael Tuexen 
1877f8829a4aSRandall Stewart 		/* process the INIT-ACK info (my info) */
1878f8829a4aSRandall Stewart 		asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1879f8829a4aSRandall Stewart 		asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1880f8829a4aSRandall Stewart 
1881f8829a4aSRandall Stewart 		/* pull from vtag hash */
1882f8829a4aSRandall Stewart 		LIST_REMOVE(stcb, sctp_asocs);
1883f8829a4aSRandall Stewart 		/* re-insert to new vtag position */
1884b3f1ea41SRandall Stewart 		head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1885b3f1ea41SRandall Stewart 		    SCTP_BASE_INFO(hashasocmark))];
1886f8829a4aSRandall Stewart 		/*
1887f8829a4aSRandall Stewart 		 * put it in the bucket in the vtag hash of assoc's for the
1888f8829a4aSRandall Stewart 		 * system
1889f8829a4aSRandall Stewart 		 */
1890f8829a4aSRandall Stewart 		LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1891f8829a4aSRandall Stewart 
1892a5d547adSRandall Stewart 		SCTP_INP_WUNLOCK(stcb->sctp_ep);
1893a5d547adSRandall Stewart 		SCTP_INP_INFO_WUNLOCK();
189456f778aaSMichael Tuexen 		asoc->total_flight = 0;
189556f778aaSMichael Tuexen 		asoc->total_flight_count = 0;
189656f778aaSMichael Tuexen 		/* process the INIT info (peer's info) */
18975f2e1835SMichael Tuexen 		if (sctp_process_init(init_cp, stcb) < 0) {
189844b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
189944b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 13;
19005f2e1835SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
19015f2e1835SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_init() failed\n");
19025f2e1835SMichael Tuexen 			sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
19035f2e1835SMichael Tuexen 			    src, dst, sh, op_err,
19045f2e1835SMichael Tuexen 			    mflowtype, mflowid,
19055f2e1835SMichael Tuexen 			    vrf_id, net->port);
1906f8829a4aSRandall Stewart 			return (NULL);
1907f8829a4aSRandall Stewart 		}
1908f8829a4aSRandall Stewart 		/*
1909f8829a4aSRandall Stewart 		 * since we did not send a HB make sure we don't double
1910f8829a4aSRandall Stewart 		 * things
1911f8829a4aSRandall Stewart 		 */
1912f8829a4aSRandall Stewart 		net->hb_responded = 1;
1913f8829a4aSRandall Stewart 
19145f2e1835SMichael Tuexen 		if ((retval = sctp_load_addresses_from_init(stcb, m,
1915f8829a4aSRandall Stewart 		    init_offset + sizeof(struct sctp_init_chunk),
19165f2e1835SMichael Tuexen 		    initack_offset, src, dst, init_src, stcb->asoc.port)) < 0) {
191744b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
191844b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 14;
19195f2e1835SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
19205f2e1835SMichael Tuexen 			    "Problem with address parameters");
19215f2e1835SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1,
19225f2e1835SMichael Tuexen 			    "Load addresses from INIT causes an abort %d\n",
19235f2e1835SMichael Tuexen 			    retval);
19245f2e1835SMichael Tuexen 			sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
19255f2e1835SMichael Tuexen 			    src, dst, sh, op_err,
19265f2e1835SMichael Tuexen 			    mflowtype, mflowid,
19275f2e1835SMichael Tuexen 			    vrf_id, net->port);
1928f8829a4aSRandall Stewart 			return (NULL);
1929f8829a4aSRandall Stewart 		}
1930f8829a4aSRandall Stewart 		/* respond with a COOKIE-ACK */
1931f8829a4aSRandall Stewart 		sctp_send_cookie_ack(stcb);
193244b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
193344b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 15;
1934a89481d3SMichael Tuexen 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE) &&
1935a89481d3SMichael Tuexen 		    (asoc->sctp_autoclose_ticks > 0)) {
1936a89481d3SMichael Tuexen 			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
1937a89481d3SMichael Tuexen 		}
1938f8829a4aSRandall Stewart 		return (stcb);
1939f8829a4aSRandall Stewart 	}
194044b7479bSRandall Stewart 	if (how_indx < sizeof(asoc->cookie_how))
194144b7479bSRandall Stewart 		asoc->cookie_how[how_indx] = 16;
1942f8829a4aSRandall Stewart 	/* all other cases... */
194312dda000SMichael Tuexen 	SCTP_TCB_UNLOCK(stcb);
1944f8829a4aSRandall Stewart 	return (NULL);
1945f8829a4aSRandall Stewart }
1946f8829a4aSRandall Stewart 
1947f8829a4aSRandall Stewart /*
1948f8829a4aSRandall Stewart  * handle a state cookie for a new association m: input packet mbuf chain--
1949f8829a4aSRandall Stewart  * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
1950f8829a4aSRandall Stewart  * and the cookie signature does not exist offset: offset into mbuf to the
1951f8829a4aSRandall Stewart  * cookie-echo chunk length: length of the cookie chunk to: where the init
1952f8829a4aSRandall Stewart  * was from returns a new TCB
1953f8829a4aSRandall Stewart  */
1954f30ac432SMichael Tuexen static struct sctp_tcb *
sctp_process_cookie_new(struct mbuf * m,int iphlen,int offset,struct sockaddr * src,struct sockaddr * dst,struct sctphdr * sh,struct sctp_state_cookie * cookie,int cookie_len,struct sctp_inpcb * inp,struct sctp_nets ** netp,struct sockaddr * init_src,int * notification,int auth_skipped,uint32_t auth_offset,uint32_t auth_len,uint8_t mflowtype,uint32_t mflowid,uint32_t vrf_id,uint16_t port)1955f8829a4aSRandall Stewart sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1956b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
1957f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1958f8829a4aSRandall Stewart     struct sctp_inpcb *inp, struct sctp_nets **netp,
1959f8829a4aSRandall Stewart     struct sockaddr *init_src, int *notification,
196017205eccSRandall Stewart     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1961457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
1962c54a18d2SRandall Stewart     uint32_t vrf_id, uint16_t port)
1963f8829a4aSRandall Stewart {
1964f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
1965f8829a4aSRandall Stewart 	struct sctp_init_chunk *init_cp, init_buf;
1966f8829a4aSRandall Stewart 	struct sctp_init_ack_chunk *initack_cp, initack_buf;
196724aaac8dSMichael Tuexen 	union sctp_sockstore store;
1968f8829a4aSRandall Stewart 	struct sctp_association *asoc;
1969f8829a4aSRandall Stewart 	int init_offset, initack_offset, initack_limit;
1970f8829a4aSRandall Stewart 	int error = 0;
19718262311cSMichael Tuexen 	uint8_t auth_chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
1972ceaad40aSRandall Stewart 
1973f8829a4aSRandall Stewart 	/*
1974f8829a4aSRandall Stewart 	 * find and validate the INIT chunk in the cookie (peer's info) the
1975f8829a4aSRandall Stewart 	 * INIT should start after the cookie-echo header struct (chunk
1976f8829a4aSRandall Stewart 	 * header, state cookie header struct)
1977f8829a4aSRandall Stewart 	 */
1978f8829a4aSRandall Stewart 	init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
1979f8829a4aSRandall Stewart 	init_cp = (struct sctp_init_chunk *)
1980f8829a4aSRandall Stewart 	    sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1981f8829a4aSRandall Stewart 	    (uint8_t *)&init_buf);
1982f8829a4aSRandall Stewart 	if (init_cp == NULL) {
1983f8829a4aSRandall Stewart 		/* could not pull a INIT chunk in cookie */
1984ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1,
1985ad81507eSRandall Stewart 		    "process_cookie_new: could not pull INIT chunk hdr\n");
1986f8829a4aSRandall Stewart 		return (NULL);
1987f8829a4aSRandall Stewart 	}
1988f8829a4aSRandall Stewart 	if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1989ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
1990f8829a4aSRandall Stewart 		return (NULL);
1991f8829a4aSRandall Stewart 	}
199260990c0cSMichael Tuexen 	initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1993f8829a4aSRandall Stewart 	/*
1994f8829a4aSRandall Stewart 	 * find and validate the INIT-ACK chunk in the cookie (my info) the
1995f8829a4aSRandall Stewart 	 * INIT-ACK follows the INIT chunk
1996f8829a4aSRandall Stewart 	 */
1997f8829a4aSRandall Stewart 	initack_cp = (struct sctp_init_ack_chunk *)
1998f8829a4aSRandall Stewart 	    sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1999f8829a4aSRandall Stewart 	    (uint8_t *)&initack_buf);
2000f8829a4aSRandall Stewart 	if (initack_cp == NULL) {
2001f8829a4aSRandall Stewart 		/* could not pull INIT-ACK chunk in cookie */
2002ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2003f8829a4aSRandall Stewart 		return (NULL);
2004f8829a4aSRandall Stewart 	}
2005f8829a4aSRandall Stewart 	if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2006f8829a4aSRandall Stewart 		return (NULL);
2007f8829a4aSRandall Stewart 	}
2008f8829a4aSRandall Stewart 	/*
2009f8829a4aSRandall Stewart 	 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2010f8829a4aSRandall Stewart 	 * "initack_limit" value.  This is because the chk_length field
2011f8829a4aSRandall Stewart 	 * includes the length of the cookie, but the cookie is omitted when
2012f8829a4aSRandall Stewart 	 * the INIT and INIT_ACK are tacked onto the cookie...
2013f8829a4aSRandall Stewart 	 */
2014f8829a4aSRandall Stewart 	initack_limit = offset + cookie_len;
2015f8829a4aSRandall Stewart 
2016f8829a4aSRandall Stewart 	/*
2017f8829a4aSRandall Stewart 	 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2018e7e65008SMichael Tuexen 	 * and populate
2019f8829a4aSRandall Stewart 	 */
202052be287eSRandall Stewart 
202152be287eSRandall Stewart 	/*
202252be287eSRandall Stewart 	 * Here we do a trick, we set in NULL for the proc/thread argument.
202352be287eSRandall Stewart 	 * We do this since in effect we only use the p argument when the
202452be287eSRandall Stewart 	 * socket is unbound and we must do an implicit bind. Since we are
202552be287eSRandall Stewart 	 * getting a cookie, we cannot be unbound.
202652be287eSRandall Stewart 	 */
2027b5c16493SMichael Tuexen 	stcb = sctp_aloc_assoc(inp, init_src, &error,
2028c7f048abSMichael Tuexen 	    ntohl(initack_cp->init.initiate_tag),
2029c7f048abSMichael Tuexen 	    ntohl(initack_cp->init.initial_tsn), vrf_id,
2030c979034bSMichael Tuexen 	    ntohs(initack_cp->init.num_outbound_streams),
2031ec70917fSMichael Tuexen 	    port,
20328a956abeSMichael Tuexen 	    (struct thread *)NULL,
20338a956abeSMichael Tuexen 	    SCTP_DONT_INITIALIZE_AUTH_PARAMS);
2034f8829a4aSRandall Stewart 	if (stcb == NULL) {
2035f8829a4aSRandall Stewart 		struct mbuf *op_err;
2036f8829a4aSRandall Stewart 
2037f8829a4aSRandall Stewart 		/* memory problem? */
2038ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1,
2039ad81507eSRandall Stewart 		    "process_cookie_new: no room for another TCB!\n");
2040ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2041f8829a4aSRandall Stewart 		sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2042b1754ad1SMichael Tuexen 		    src, dst, sh, op_err,
2043457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2044f30ac432SMichael Tuexen 		    vrf_id, port);
2045f8829a4aSRandall Stewart 		return (NULL);
2046f8829a4aSRandall Stewart 	}
2047f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2048f8829a4aSRandall Stewart 	/* get scope variables out of cookie */
2049a1cb341bSMichael Tuexen 	asoc->scope.ipv4_local_scope = cookie->ipv4_scope;
2050a1cb341bSMichael Tuexen 	asoc->scope.site_scope = cookie->site_scope;
2051a1cb341bSMichael Tuexen 	asoc->scope.local_scope = cookie->local_scope;
2052a1cb341bSMichael Tuexen 	asoc->scope.loopback_scope = cookie->loopback_scope;
2053f8829a4aSRandall Stewart 
2054a1cb341bSMichael Tuexen 	if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2055a1cb341bSMichael Tuexen 	    (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2056f8829a4aSRandall Stewart 		struct mbuf *op_err;
2057f8829a4aSRandall Stewart 
2058f8829a4aSRandall Stewart 		/*
2059f8829a4aSRandall Stewart 		 * Houston we have a problem. The EP changed while the
2060f8829a4aSRandall Stewart 		 * cookie was in flight. Only recourse is to abort the
2061f8829a4aSRandall Stewart 		 * association.
2062f8829a4aSRandall Stewart 		 */
2063ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2064f8829a4aSRandall Stewart 		sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2065b1754ad1SMichael Tuexen 		    src, dst, sh, op_err,
2066457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2067f30ac432SMichael Tuexen 		    vrf_id, port);
2068c4739e2fSRandall Stewart 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2069b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2070f8829a4aSRandall Stewart 		return (NULL);
2071f8829a4aSRandall Stewart 	}
207252640d61SMichael Tuexen 	asoc->rcv_edmid = cookie->rcv_edmid;
2073f8829a4aSRandall Stewart 	/* process the INIT-ACK info (my info) */
2074f8829a4aSRandall Stewart 	asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2075f8829a4aSRandall Stewart 
2076f8829a4aSRandall Stewart 	/* process the INIT info (peer's info) */
20775f2e1835SMichael Tuexen 	if (sctp_process_init(init_cp, stcb) < 0) {
2078b7d130beSMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2079b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2080f8829a4aSRandall Stewart 		return (NULL);
2081f8829a4aSRandall Stewart 	}
2082f8829a4aSRandall Stewart 	/* load all addresses */
208345421646SMichael Tuexen 	if (sctp_load_addresses_from_init(stcb, m,
20845f2e1835SMichael Tuexen 	    init_offset + sizeof(struct sctp_init_chunk),
208545421646SMichael Tuexen 	    initack_offset, src, dst, init_src, port) < 0) {
2086b7d130beSMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2087b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2088f8829a4aSRandall Stewart 		return (NULL);
2089f8829a4aSRandall Stewart 	}
2090f8829a4aSRandall Stewart 	/*
2091f8829a4aSRandall Stewart 	 * verify any preceding AUTH chunk that was skipped
2092f8829a4aSRandall Stewart 	 */
2093f8829a4aSRandall Stewart 	/* pull the local authentication parameters from the cookie/init-ack */
2094f8829a4aSRandall Stewart 	sctp_auth_get_cookie_params(stcb, m,
2095f8829a4aSRandall Stewart 	    initack_offset + sizeof(struct sctp_init_ack_chunk),
2096f8829a4aSRandall Stewart 	    initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2097f8829a4aSRandall Stewart 	if (auth_skipped) {
2098f8829a4aSRandall Stewart 		struct sctp_auth_chunk *auth;
2099f8829a4aSRandall Stewart 
21008262311cSMichael Tuexen 		if (auth_len <= SCTP_CHUNK_BUFFER_SIZE) {
210197feba89SMichael Tuexen 			auth = (struct sctp_auth_chunk *)sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
210297feba89SMichael Tuexen 		} else {
210397feba89SMichael Tuexen 			auth = NULL;
210497feba89SMichael Tuexen 		}
2105ad81507eSRandall Stewart 		if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2106f8829a4aSRandall Stewart 			/* auth HMAC failed, dump the assoc and packet */
2107ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_AUTH1,
2108ad81507eSRandall Stewart 			    "COOKIE-ECHO: AUTH failed\n");
2109b7d130beSMichael Tuexen 			(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2110b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_21);
2111f8829a4aSRandall Stewart 			return (NULL);
2112f8829a4aSRandall Stewart 		} else {
2113f8829a4aSRandall Stewart 			/* remaining chunks checked... good to go */
2114f8829a4aSRandall Stewart 			stcb->asoc.authenticated = 1;
2115f8829a4aSRandall Stewart 		}
2116f8829a4aSRandall Stewart 	}
21170053ed28SMichael Tuexen 
2118f8829a4aSRandall Stewart 	/*
2119f8829a4aSRandall Stewart 	 * if we're doing ASCONFs, check to see if we have any new local
2120f8829a4aSRandall Stewart 	 * addresses that need to get added to the peer (eg. addresses
2121f8829a4aSRandall Stewart 	 * changed while cookie echo in flight).  This needs to be done
2122f8829a4aSRandall Stewart 	 * after we go to the OPEN state to do the correct asconf
2123f8829a4aSRandall Stewart 	 * processing. else, make sure we have the correct addresses in our
2124f8829a4aSRandall Stewart 	 * lists
2125f8829a4aSRandall Stewart 	 */
2126f8829a4aSRandall Stewart 
2127f8829a4aSRandall Stewart 	/* warning, we re-use sin, sin6, sa_store here! */
2128f8829a4aSRandall Stewart 	/* pull in local_address (our "from" address) */
2129e6194c2eSMichael Tuexen 	switch (cookie->laddr_type) {
2130e6194c2eSMichael Tuexen #ifdef INET
2131e6194c2eSMichael Tuexen 	case SCTP_IPV4_ADDRESS:
2132f8829a4aSRandall Stewart 		/* source addr is IPv4 */
213324aaac8dSMichael Tuexen 		memset(&store.sin, 0, sizeof(struct sockaddr_in));
213424aaac8dSMichael Tuexen 		store.sin.sin_family = AF_INET;
213524aaac8dSMichael Tuexen 		store.sin.sin_len = sizeof(struct sockaddr_in);
213624aaac8dSMichael Tuexen 		store.sin.sin_addr.s_addr = cookie->laddress[0];
2137e6194c2eSMichael Tuexen 		break;
2138e6194c2eSMichael Tuexen #endif
2139e6194c2eSMichael Tuexen #ifdef INET6
2140e6194c2eSMichael Tuexen 	case SCTP_IPV6_ADDRESS:
2141f8829a4aSRandall Stewart 		/* source addr is IPv6 */
214224aaac8dSMichael Tuexen 		memset(&store.sin6, 0, sizeof(struct sockaddr_in6));
214324aaac8dSMichael Tuexen 		store.sin6.sin6_family = AF_INET6;
214424aaac8dSMichael Tuexen 		store.sin6.sin6_len = sizeof(struct sockaddr_in6);
214524aaac8dSMichael Tuexen 		store.sin6.sin6_scope_id = cookie->scope_id;
214623602b60SMichael Tuexen 		memcpy(&store.sin6.sin6_addr, cookie->laddress, sizeof(struct in6_addr));
2147e6194c2eSMichael Tuexen 		break;
2148e6194c2eSMichael Tuexen #endif
2149e6194c2eSMichael Tuexen 	default:
2150b7d130beSMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2151b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
2152f8829a4aSRandall Stewart 		return (NULL);
2153f8829a4aSRandall Stewart 	}
2154f8829a4aSRandall Stewart 
21551698cbd9SMichael Tuexen 	/* update current state */
21561698cbd9SMichael Tuexen 	SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2157839d21d6SMichael Tuexen 	SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
21581698cbd9SMichael Tuexen 	sctp_stop_all_cookie_timers(stcb);
21591698cbd9SMichael Tuexen 	SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
21601698cbd9SMichael Tuexen 	SCTP_STAT_INCR_GAUGE32(sctps_currestab);
21611698cbd9SMichael Tuexen 
2162f8829a4aSRandall Stewart 	/* set up to notify upper layer */
2163f8829a4aSRandall Stewart 	*notification = SCTP_NOTIFY_ASSOC_UP;
2164f8829a4aSRandall Stewart 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2165f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
21665d08768aSMichael Tuexen 	    (!SCTP_IS_LISTENING(inp))) {
2167f8829a4aSRandall Stewart 		/*
2168f8829a4aSRandall Stewart 		 * This is an endpoint that called connect() how it got a
2169f8829a4aSRandall Stewart 		 * cookie that is NEW is a bit of a mystery. It must be that
2170f8829a4aSRandall Stewart 		 * the INIT was sent, but before it got there.. a complete
2171f8829a4aSRandall Stewart 		 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2172f8829a4aSRandall Stewart 		 * should have went to the other code.. not here.. oh well..
2173f8829a4aSRandall Stewart 		 * a bit of protection is worth having..
21742d5c48ecSMark Johnston 		 *
21752d5c48ecSMark Johnston 		 * XXXMJ unlocked
2176f8829a4aSRandall Stewart 		 */
2177a5c2009dSMichael Tuexen 		sctp_pcb_add_flags(stcb->sctp_ep, SCTP_PCB_FLAGS_CONNECTED);
2178ceaad40aSRandall Stewart 		soisconnected(stcb->sctp_socket);
2179f8829a4aSRandall Stewart 	} else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
21805d08768aSMichael Tuexen 	    (SCTP_IS_LISTENING(inp))) {
2181f8829a4aSRandall Stewart 		/*
2182f8829a4aSRandall Stewart 		 * We don't want to do anything with this one. Since it is
2183f8829a4aSRandall Stewart 		 * the listening guy. The timer will get started for
2184f8829a4aSRandall Stewart 		 * accepted connections in the caller.
2185f8829a4aSRandall Stewart 		 */
2186f8829a4aSRandall Stewart 		;
2187f8829a4aSRandall Stewart 	}
2188f8829a4aSRandall Stewart 	if (stcb->asoc.sctp_autoclose_ticks &&
2189f8829a4aSRandall Stewart 	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2190f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2191f8829a4aSRandall Stewart 	}
2192b54d3a6cSRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2193b15f5411SMichael Tuexen 	*netp = sctp_findnet(stcb, init_src);
2194b15f5411SMichael Tuexen 	if (*netp != NULL) {
2195b15f5411SMichael Tuexen 		/*
2196b15f5411SMichael Tuexen 		 * Since we did not send a HB, make sure we don't double
2197b15f5411SMichael Tuexen 		 * things.
2198b15f5411SMichael Tuexen 		 */
2199b15f5411SMichael Tuexen 		(*netp)->hb_responded = 1;
22009a972525SRandall Stewart 	}
22013232788eSRandall Stewart 	/* respond with a COOKIE-ACK */
2202f8829a4aSRandall Stewart 	sctp_send_cookie_ack(stcb);
22033232788eSRandall Stewart 
22043232788eSRandall Stewart 	/*
22053232788eSRandall Stewart 	 * check the address lists for any ASCONFs that need to be sent
22063232788eSRandall Stewart 	 * AFTER the cookie-ack is sent
22073232788eSRandall Stewart 	 */
22083232788eSRandall Stewart 	sctp_check_address_list(stcb, m,
22093232788eSRandall Stewart 	    initack_offset + sizeof(struct sctp_init_ack_chunk),
22103232788eSRandall Stewart 	    initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
221124aaac8dSMichael Tuexen 	    &store.sa, cookie->local_scope, cookie->site_scope,
22123232788eSRandall Stewart 	    cookie->ipv4_scope, cookie->loopback_scope);
22133232788eSRandall Stewart 
2214f8829a4aSRandall Stewart 	return (stcb);
2215f8829a4aSRandall Stewart }
2216f8829a4aSRandall Stewart 
2217830d754dSRandall Stewart /*
2218830d754dSRandall Stewart  * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2219830d754dSRandall Stewart  * we NEED to make sure we are not already using the vtag. If so we
2220830d754dSRandall Stewart  * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2221830d754dSRandall Stewart 	head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2222830d754dSRandall Stewart 							    SCTP_BASE_INFO(hashasocmark))];
2223830d754dSRandall Stewart 	LIST_FOREACH(stcb, head, sctp_asocs) {
2224830d754dSRandall Stewart 	        if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2225830d754dSRandall Stewart 		       -- SEND ABORT - TRY AGAIN --
2226830d754dSRandall Stewart 		}
2227830d754dSRandall Stewart 	}
2228830d754dSRandall Stewart */
2229f8829a4aSRandall Stewart 
2230f8829a4aSRandall Stewart /*
2231f8829a4aSRandall Stewart  * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2232f8829a4aSRandall Stewart  * existing (non-NULL) TCB
2233f8829a4aSRandall Stewart  */
2234f8829a4aSRandall Stewart static struct mbuf *
sctp_handle_cookie_echo(struct mbuf * m,int iphlen,int offset,struct sockaddr * src,struct sockaddr * dst,struct sctphdr * sh,struct sctp_cookie_echo_chunk * cp,struct sctp_inpcb ** inp_p,struct sctp_tcb ** stcb,struct sctp_nets ** netp,int auth_skipped,uint32_t auth_offset,uint32_t auth_len,struct sctp_tcb ** locked_tcb,uint8_t mflowtype,uint32_t mflowid,uint32_t vrf_id,uint16_t port)2235f8829a4aSRandall Stewart sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2236b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
2237f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2238f8829a4aSRandall Stewart     struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
223917205eccSRandall Stewart     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2240f30ac432SMichael Tuexen     struct sctp_tcb **locked_tcb,
2241457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
2242f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
2243f8829a4aSRandall Stewart {
2244f8829a4aSRandall Stewart 	struct sctp_state_cookie *cookie;
2245f8829a4aSRandall Stewart 	struct sctp_tcb *l_stcb = *stcb;
2246f8829a4aSRandall Stewart 	struct sctp_inpcb *l_inp;
2247f8829a4aSRandall Stewart 	struct sockaddr *to;
2248f8829a4aSRandall Stewart 	struct sctp_pcb *ep;
2249f8829a4aSRandall Stewart 	struct mbuf *m_sig;
2250f8829a4aSRandall Stewart 	uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2251f8829a4aSRandall Stewart 	uint8_t *sig;
2252f8829a4aSRandall Stewart 	uint8_t cookie_ok = 0;
2253329204ffSMichael Tuexen 	unsigned int sig_offset, cookie_offset;
2254f8829a4aSRandall Stewart 	unsigned int cookie_len;
2255f8829a4aSRandall Stewart 	struct timeval now;
2256a92d5016SMichael Tuexen 	struct timeval time_entered, time_expires;
2257f8829a4aSRandall Stewart 	int notification = 0;
2258f8829a4aSRandall Stewart 	struct sctp_nets *netl;
2259f8829a4aSRandall Stewart 	int had_a_existing_tcb = 0;
22602fad0e55SMichael Tuexen 	int send_int_conf = 0;
2261e6194c2eSMichael Tuexen #ifdef INET
2262e6194c2eSMichael Tuexen 	struct sockaddr_in sin;
2263e6194c2eSMichael Tuexen #endif
2264e6194c2eSMichael Tuexen #ifdef INET6
2265e6194c2eSMichael Tuexen 	struct sockaddr_in6 sin6;
2266e6194c2eSMichael Tuexen #endif
2267e6194c2eSMichael Tuexen 
2268ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
2269ad81507eSRandall Stewart 	    "sctp_handle_cookie: handling COOKIE-ECHO\n");
2270f8829a4aSRandall Stewart 
2271f8829a4aSRandall Stewart 	if (inp_p == NULL) {
2272f8829a4aSRandall Stewart 		return (NULL);
2273f8829a4aSRandall Stewart 	}
2274f8829a4aSRandall Stewart 	cookie = &cp->cookie;
2275f8829a4aSRandall Stewart 	cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2276f8829a4aSRandall Stewart 	cookie_len = ntohs(cp->ch.chunk_length);
2277f8829a4aSRandall Stewart 
2278d44b45dfSMichael Tuexen 	if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2279d44b45dfSMichael Tuexen 	    sizeof(struct sctp_init_chunk) +
2280d44b45dfSMichael Tuexen 	    sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2281d44b45dfSMichael Tuexen 		/* cookie too small */
2282d44b45dfSMichael Tuexen 		return (NULL);
2283d44b45dfSMichael Tuexen 	}
22843db4ea95SMichael Tuexen 	if ((cookie->peerport != sh->src_port) ||
22853db4ea95SMichael Tuexen 	    (cookie->myport != sh->dest_port) ||
2286f8829a4aSRandall Stewart 	    (cookie->my_vtag != sh->v_tag)) {
2287f8829a4aSRandall Stewart 		/*
2288f8829a4aSRandall Stewart 		 * invalid ports or bad tag.  Note that we always leave the
2289f8829a4aSRandall Stewart 		 * v_tag in the header in network order and when we stored
2290f8829a4aSRandall Stewart 		 * it in the my_vtag slot we also left it in network order.
229117205eccSRandall Stewart 		 * This maintains the match even though it may be in the
2292f8829a4aSRandall Stewart 		 * opposite byte order of the machine :->
2293f8829a4aSRandall Stewart 		 */
2294f8829a4aSRandall Stewart 		return (NULL);
2295f8829a4aSRandall Stewart 	}
2296f8829a4aSRandall Stewart 	/*
2297f8829a4aSRandall Stewart 	 * split off the signature into its own mbuf (since it should not be
2298f8829a4aSRandall Stewart 	 * calculated in the sctp_hmac_m() call).
2299f8829a4aSRandall Stewart 	 */
2300f8829a4aSRandall Stewart 	sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2301eb1b1807SGleb Smirnoff 	m_sig = m_split(m, sig_offset, M_NOWAIT);
2302f8829a4aSRandall Stewart 	if (m_sig == NULL) {
2303f8829a4aSRandall Stewart 		/* out of memory or ?? */
2304f8829a4aSRandall Stewart 		return (NULL);
2305f8829a4aSRandall Stewart 	}
2306eadccaccSRandall Stewart #ifdef SCTP_MBUF_LOGGING
2307b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
23084be807c4SMichael Tuexen 		sctp_log_mbc(m_sig, SCTP_MBUF_SPLIT);
2309eadccaccSRandall Stewart 	}
2310eadccaccSRandall Stewart #endif
2311eadccaccSRandall Stewart 
2312f8829a4aSRandall Stewart 	/*
2313f8829a4aSRandall Stewart 	 * compute the signature/digest for the cookie
2314f8829a4aSRandall Stewart 	 */
2315e0127ea4SMichael Tuexen 	if (l_stcb != NULL) {
2316e0127ea4SMichael Tuexen 		atomic_add_int(&l_stcb->asoc.refcnt, 1);
2317f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(l_stcb);
2318f8829a4aSRandall Stewart 	}
2319e0127ea4SMichael Tuexen 	l_inp = *inp_p;
2320f8829a4aSRandall Stewart 	SCTP_INP_RLOCK(l_inp);
2321e0127ea4SMichael Tuexen 	if (l_stcb != NULL) {
2322f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(l_stcb);
2323e0127ea4SMichael Tuexen 		atomic_subtract_int(&l_stcb->asoc.refcnt, 1);
2324f8829a4aSRandall Stewart 	}
2325e0127ea4SMichael Tuexen 	if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
2326e0127ea4SMichael Tuexen 		SCTP_INP_RUNLOCK(l_inp);
2327e0127ea4SMichael Tuexen 		sctp_m_freem(m_sig);
2328e0127ea4SMichael Tuexen 		return (NULL);
2329e0127ea4SMichael Tuexen 	}
2330e0127ea4SMichael Tuexen 	ep = &(*inp_p)->sctp_ep;
2331f8829a4aSRandall Stewart 	/* which cookie is it? */
2332*9d8a3718SMichael Tuexen 	if ((cookie->time_entered.tv_sec < ep->time_of_secret_change) &&
2333f8829a4aSRandall Stewart 	    (ep->current_secret_number != ep->last_secret_number)) {
2334f8829a4aSRandall Stewart 		/* it's the old cookie */
23356e55db54SRandall Stewart 		(void)sctp_hmac_m(SCTP_HMAC,
2336f8829a4aSRandall Stewart 		    (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2337d00aff5dSRandall Stewart 		    SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2338f8829a4aSRandall Stewart 	} else {
2339f8829a4aSRandall Stewart 		/* it's the current cookie */
23406e55db54SRandall Stewart 		(void)sctp_hmac_m(SCTP_HMAC,
2341f8829a4aSRandall Stewart 		    (uint8_t *)ep->secret_key[(int)ep->current_secret_number],
2342d00aff5dSRandall Stewart 		    SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2343f8829a4aSRandall Stewart 	}
2344f8829a4aSRandall Stewart 	/* get the signature */
2345f8829a4aSRandall Stewart 	SCTP_INP_RUNLOCK(l_inp);
2346f8829a4aSRandall Stewart 	sig = (uint8_t *)sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *)&tmp_sig);
2347f8829a4aSRandall Stewart 	if (sig == NULL) {
2348f8829a4aSRandall Stewart 		/* couldn't find signature */
234903b0b021SRandall Stewart 		sctp_m_freem(m_sig);
2350f8829a4aSRandall Stewart 		return (NULL);
2351f8829a4aSRandall Stewart 	}
2352f8829a4aSRandall Stewart 	/* compare the received digest with the computed digest */
235315a087e5SMichael Tuexen 	if (timingsafe_bcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2354f8829a4aSRandall Stewart 		/* try the old cookie? */
2355*9d8a3718SMichael Tuexen 		if ((cookie->time_entered.tv_sec == ep->time_of_secret_change) &&
2356f8829a4aSRandall Stewart 		    (ep->current_secret_number != ep->last_secret_number)) {
2357f8829a4aSRandall Stewart 			/* compute digest with old */
23586e55db54SRandall Stewart 			(void)sctp_hmac_m(SCTP_HMAC,
2359f8829a4aSRandall Stewart 			    (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2360d00aff5dSRandall Stewart 			    SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2361f8829a4aSRandall Stewart 			/* compare */
236215a087e5SMichael Tuexen 			if (timingsafe_bcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2363f8829a4aSRandall Stewart 				cookie_ok = 1;
2364f8829a4aSRandall Stewart 		}
2365f8829a4aSRandall Stewart 	} else {
2366f8829a4aSRandall Stewart 		cookie_ok = 1;
2367f8829a4aSRandall Stewart 	}
2368f8829a4aSRandall Stewart 
2369f8829a4aSRandall Stewart 	/*
2370f8829a4aSRandall Stewart 	 * Now before we continue we must reconstruct our mbuf so that
2371f8829a4aSRandall Stewart 	 * normal processing of any other chunks will work.
2372f8829a4aSRandall Stewart 	 */
2373f8829a4aSRandall Stewart 	{
2374f8829a4aSRandall Stewart 		struct mbuf *m_at;
2375f8829a4aSRandall Stewart 
2376f8829a4aSRandall Stewart 		m_at = m;
2377139bc87fSRandall Stewart 		while (SCTP_BUF_NEXT(m_at) != NULL) {
2378139bc87fSRandall Stewart 			m_at = SCTP_BUF_NEXT(m_at);
2379f8829a4aSRandall Stewart 		}
2380139bc87fSRandall Stewart 		SCTP_BUF_NEXT(m_at) = m_sig;
2381f8829a4aSRandall Stewart 	}
2382f8829a4aSRandall Stewart 
2383f8829a4aSRandall Stewart 	if (cookie_ok == 0) {
2384ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2385ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
2386ad81507eSRandall Stewart 		    "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2387f8829a4aSRandall Stewart 		    (uint32_t)offset, cookie_offset, sig_offset);
2388f8829a4aSRandall Stewart 		return (NULL);
2389f8829a4aSRandall Stewart 	}
23900053ed28SMichael Tuexen 
2391a92d5016SMichael Tuexen 	if (sctp_ticks_to_msecs(cookie->cookie_life) > SCTP_MAX_COOKIE_LIFE) {
2392a92d5016SMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: Invalid cookie lifetime\n");
2393a92d5016SMichael Tuexen 		return (NULL);
2394a92d5016SMichael Tuexen 	}
2395a92d5016SMichael Tuexen 	time_entered.tv_sec = cookie->time_entered.tv_sec;
2396a92d5016SMichael Tuexen 	time_entered.tv_usec = cookie->time_entered.tv_usec;
2397a92d5016SMichael Tuexen 	if ((time_entered.tv_sec < 0) ||
2398a92d5016SMichael Tuexen 	    (time_entered.tv_usec < 0) ||
2399a92d5016SMichael Tuexen 	    (time_entered.tv_usec >= 1000000)) {
2400a92d5016SMichael Tuexen 		/* Invalid time stamp. Cookie must have been modified. */
2401a92d5016SMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: Invalid time stamp\n");
2402a92d5016SMichael Tuexen 		return (NULL);
2403a92d5016SMichael Tuexen 	}
24046e55db54SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&now);
2405a92d5016SMichael Tuexen 	if (timevalcmp(&now, &time_entered, <)) {
2406a92d5016SMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie generated in the future!\n");
2407a92d5016SMichael Tuexen 		return (NULL);
2408a92d5016SMichael Tuexen 	}
2409a92d5016SMichael Tuexen 	/*
2410a92d5016SMichael Tuexen 	 * Check the cookie timestamps to be sure it's not stale.
2411a92d5016SMichael Tuexen 	 * cookie_life is in ticks, so we convert to seconds.
2412a92d5016SMichael Tuexen 	 */
2413a92d5016SMichael Tuexen 	time_expires.tv_sec = time_entered.tv_sec + sctp_ticks_to_secs(cookie->cookie_life);
2414a92d5016SMichael Tuexen 	time_expires.tv_usec = time_entered.tv_usec;
2415f8829a4aSRandall Stewart 	if (timevalcmp(&now, &time_expires, >)) {
2416f8829a4aSRandall Stewart 		/* cookie is stale! */
2417f8829a4aSRandall Stewart 		struct mbuf *op_err;
241886eda749SMichael Tuexen 		struct sctp_error_stale_cookie *cause;
2419564a95f4SMichael Tuexen 		struct timeval diff;
2420564a95f4SMichael Tuexen 		uint32_t staleness;
2421f8829a4aSRandall Stewart 
242286eda749SMichael Tuexen 		op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_stale_cookie),
2423eb1b1807SGleb Smirnoff 		    0, M_NOWAIT, 1, MT_DATA);
2424f8829a4aSRandall Stewart 		if (op_err == NULL) {
2425f8829a4aSRandall Stewart 			/* FOOBAR */
2426f8829a4aSRandall Stewart 			return (NULL);
2427f8829a4aSRandall Stewart 		}
2428f8829a4aSRandall Stewart 		/* Set the len */
242986eda749SMichael Tuexen 		SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_stale_cookie);
243086eda749SMichael Tuexen 		cause = mtod(op_err, struct sctp_error_stale_cookie *);
243186eda749SMichael Tuexen 		cause->cause.code = htons(SCTP_CAUSE_STALE_COOKIE);
2432a92d5016SMichael Tuexen 		cause->cause.length = htons(sizeof(struct sctp_error_stale_cookie));
2433564a95f4SMichael Tuexen 		diff = now;
2434564a95f4SMichael Tuexen 		timevalsub(&diff, &time_expires);
2435c3115febSMichael Tuexen 		if ((uint32_t)diff.tv_sec > UINT32_MAX / 1000000) {
2436564a95f4SMichael Tuexen 			staleness = UINT32_MAX;
2437564a95f4SMichael Tuexen 		} else {
2438aab1d593SMichael Tuexen 			staleness = (uint32_t)diff.tv_sec * 1000000;
2439564a95f4SMichael Tuexen 		}
24403ec509bcSMichael Tuexen 		if (UINT32_MAX - staleness >= (uint32_t)diff.tv_usec) {
2441aab1d593SMichael Tuexen 			staleness += (uint32_t)diff.tv_usec;
2442564a95f4SMichael Tuexen 		} else {
2443564a95f4SMichael Tuexen 			staleness = UINT32_MAX;
2444564a95f4SMichael Tuexen 		}
2445564a95f4SMichael Tuexen 		cause->stale_time = htonl(staleness);
2446b1754ad1SMichael Tuexen 		sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
2447d089f9b9SMichael Tuexen 		    mflowtype, mflowid, l_inp->fibnum,
2448c54a18d2SRandall Stewart 		    vrf_id, port);
2449f8829a4aSRandall Stewart 		return (NULL);
2450f8829a4aSRandall Stewart 	}
2451f8829a4aSRandall Stewart 	/*
2452f8829a4aSRandall Stewart 	 * Now we must see with the lookup address if we have an existing
2453f8829a4aSRandall Stewart 	 * asoc. This will only happen if we were in the COOKIE-WAIT state
2454f8829a4aSRandall Stewart 	 * and a INIT collided with us and somewhere the peer sent the
2455f8829a4aSRandall Stewart 	 * cookie on another address besides the single address our assoc
2456f8829a4aSRandall Stewart 	 * had for him. In this case we will have one of the tie-tags set at
2457f8829a4aSRandall Stewart 	 * least AND the address field in the cookie can be used to look it
2458f8829a4aSRandall Stewart 	 * up.
2459f8829a4aSRandall Stewart 	 */
2460f8829a4aSRandall Stewart 	to = NULL;
2461e6194c2eSMichael Tuexen 	switch (cookie->addr_type) {
2462e6194c2eSMichael Tuexen #ifdef INET6
2463e6194c2eSMichael Tuexen 	case SCTP_IPV6_ADDRESS:
2464f8829a4aSRandall Stewart 		memset(&sin6, 0, sizeof(sin6));
2465f8829a4aSRandall Stewart 		sin6.sin6_family = AF_INET6;
2466f8829a4aSRandall Stewart 		sin6.sin6_len = sizeof(sin6);
2467f8829a4aSRandall Stewart 		sin6.sin6_port = sh->src_port;
2468f8829a4aSRandall Stewart 		sin6.sin6_scope_id = cookie->scope_id;
2469f8829a4aSRandall Stewart 		memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2470f8829a4aSRandall Stewart 		    sizeof(sin6.sin6_addr.s6_addr));
2471f8829a4aSRandall Stewart 		to = (struct sockaddr *)&sin6;
2472e6194c2eSMichael Tuexen 		break;
2473e6194c2eSMichael Tuexen #endif
2474e6194c2eSMichael Tuexen #ifdef INET
2475e6194c2eSMichael Tuexen 	case SCTP_IPV4_ADDRESS:
2476f8829a4aSRandall Stewart 		memset(&sin, 0, sizeof(sin));
2477f8829a4aSRandall Stewart 		sin.sin_family = AF_INET;
2478f8829a4aSRandall Stewart 		sin.sin_len = sizeof(sin);
2479f8829a4aSRandall Stewart 		sin.sin_port = sh->src_port;
2480f8829a4aSRandall Stewart 		sin.sin_addr.s_addr = cookie->address[0];
2481f8829a4aSRandall Stewart 		to = (struct sockaddr *)&sin;
2482e6194c2eSMichael Tuexen 		break;
2483e6194c2eSMichael Tuexen #endif
2484e6194c2eSMichael Tuexen 	default:
2485ad81507eSRandall Stewart 		/* This should not happen */
2486ad81507eSRandall Stewart 		return (NULL);
2487f8829a4aSRandall Stewart 	}
2488f0dc2113SMichael Tuexen 	if (*stcb == NULL) {
2489f8829a4aSRandall Stewart 		/* Yep, lets check */
2490b1754ad1SMichael Tuexen 		*stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
2491f8829a4aSRandall Stewart 		if (*stcb == NULL) {
2492f8829a4aSRandall Stewart 			/*
2493f8829a4aSRandall Stewart 			 * We should have only got back the same inp. If we
2494f8829a4aSRandall Stewart 			 * got back a different ep we have a problem. The
2495f8829a4aSRandall Stewart 			 * original findep got back l_inp and now
2496f8829a4aSRandall Stewart 			 */
2497f8829a4aSRandall Stewart 			if (l_inp != *inp_p) {
2498ad81507eSRandall Stewart 				SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2499f8829a4aSRandall Stewart 			}
2500a5d547adSRandall Stewart 		} else {
2501a5d547adSRandall Stewart 			if (*locked_tcb == NULL) {
2502a5d547adSRandall Stewart 				/*
2503a5d547adSRandall Stewart 				 * In this case we found the assoc only
2504a5d547adSRandall Stewart 				 * after we locked the create lock. This
2505a5d547adSRandall Stewart 				 * means we are in a colliding case and we
2506a5d547adSRandall Stewart 				 * must make sure that we unlock the tcb if
2507a5d547adSRandall Stewart 				 * its one of the cases where we throw away
2508a5d547adSRandall Stewart 				 * the incoming packets.
2509a5d547adSRandall Stewart 				 */
2510a5d547adSRandall Stewart 				*locked_tcb = *stcb;
2511a5d547adSRandall Stewart 
2512a5d547adSRandall Stewart 				/*
2513a5d547adSRandall Stewart 				 * We must also increment the inp ref count
2514a5d547adSRandall Stewart 				 * since the ref_count flags was set when we
2515a5d547adSRandall Stewart 				 * did not find the TCB, now we found it
2516a5d547adSRandall Stewart 				 * which reduces the refcount.. we must
2517a5d547adSRandall Stewart 				 * raise it back out to balance it all :-)
2518a5d547adSRandall Stewart 				 */
2519a5d547adSRandall Stewart 				SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2520a5d547adSRandall Stewart 				if ((*stcb)->sctp_ep != l_inp) {
2521ad81507eSRandall Stewart 					SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2522dd294dceSMichael Tuexen 					    (void *)(*stcb)->sctp_ep, (void *)l_inp);
2523a5d547adSRandall Stewart 				}
2524a5d547adSRandall Stewart 			}
2525f8829a4aSRandall Stewart 		}
2526f8829a4aSRandall Stewart 	}
25270053ed28SMichael Tuexen 
2528f8829a4aSRandall Stewart 	cookie_len -= SCTP_SIGNATURE_SIZE;
2529f8829a4aSRandall Stewart 	if (*stcb == NULL) {
2530f8829a4aSRandall Stewart 		/* this is the "normal" case... get a new TCB */
2531b1754ad1SMichael Tuexen 		*stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
2532b1754ad1SMichael Tuexen 		    cookie, cookie_len, *inp_p,
2533b1754ad1SMichael Tuexen 		    netp, to, &notification,
2534f30ac432SMichael Tuexen 		    auth_skipped, auth_offset, auth_len,
2535457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2536f30ac432SMichael Tuexen 		    vrf_id, port);
2537f8829a4aSRandall Stewart 	} else {
2538f8829a4aSRandall Stewart 		/* this is abnormal... cookie-echo on existing TCB */
2539f8829a4aSRandall Stewart 		had_a_existing_tcb = 1;
2540b1754ad1SMichael Tuexen 		*stcb = sctp_process_cookie_existing(m, iphlen, offset,
2541b1754ad1SMichael Tuexen 		    src, dst, sh,
2542830d754dSRandall Stewart 		    cookie, cookie_len, *inp_p, *stcb, netp, to,
2543f30ac432SMichael Tuexen 		    &notification, auth_skipped, auth_offset, auth_len,
2544457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2545f30ac432SMichael Tuexen 		    vrf_id, port);
25465f2e1835SMichael Tuexen 		if (*stcb == NULL) {
25475f2e1835SMichael Tuexen 			*locked_tcb = NULL;
25485f2e1835SMichael Tuexen 		}
2549f8829a4aSRandall Stewart 	}
2550f8829a4aSRandall Stewart 
2551f8829a4aSRandall Stewart 	if (*stcb == NULL) {
2552f8829a4aSRandall Stewart 		/* still no TCB... must be bad cookie-echo */
2553f8829a4aSRandall Stewart 		return (NULL);
2554f8829a4aSRandall Stewart 	}
25555fe29cdfSMichael Tuexen 	if (*netp != NULL) {
2556457b4b88SMichael Tuexen 		(*netp)->flowtype = mflowtype;
25575fe29cdfSMichael Tuexen 		(*netp)->flowid = mflowid;
2558a4ae38f1SMichael Tuexen 	}
2559f8829a4aSRandall Stewart 	/*
2560f8829a4aSRandall Stewart 	 * Ok, we built an association so confirm the address we sent the
2561f8829a4aSRandall Stewart 	 * INIT-ACK to.
2562f8829a4aSRandall Stewart 	 */
2563f8829a4aSRandall Stewart 	netl = sctp_findnet(*stcb, to);
2564f8829a4aSRandall Stewart 	/*
2565f8829a4aSRandall Stewart 	 * This code should in theory NOT run but
2566f8829a4aSRandall Stewart 	 */
2567f8829a4aSRandall Stewart 	if (netl == NULL) {
2568f8829a4aSRandall Stewart 		/* TSNH! Huh, why do I need to add this address here? */
2569ec70917fSMichael Tuexen 		if (sctp_add_remote_addr(*stcb, to, NULL, port,
25707154bf4aSMichael Tuexen 		    SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
257160990c0cSMichael Tuexen 			return (NULL);
257260990c0cSMichael Tuexen 		}
2573f8829a4aSRandall Stewart 		netl = sctp_findnet(*stcb, to);
2574f8829a4aSRandall Stewart 	}
2575f8829a4aSRandall Stewart 	if (netl) {
2576f8829a4aSRandall Stewart 		if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2577f8829a4aSRandall Stewart 			netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2578ad81507eSRandall Stewart 			(void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2579f8829a4aSRandall Stewart 			    netl);
25802fad0e55SMichael Tuexen 			send_int_conf = 1;
2581f8829a4aSRandall Stewart 		}
2582f8829a4aSRandall Stewart 	}
2583ca85e948SMichael Tuexen 	sctp_start_net_timers(*stcb);
2584f8829a4aSRandall Stewart 	if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2585f8829a4aSRandall Stewart 		if (!had_a_existing_tcb ||
2586f8829a4aSRandall Stewart 		    (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2587f8829a4aSRandall Stewart 			/*
2588f8829a4aSRandall Stewart 			 * If we have a NEW cookie or the connect never
2589f8829a4aSRandall Stewart 			 * reached the connected state during collision we
2590f8829a4aSRandall Stewart 			 * must do the TCP accept thing.
2591f8829a4aSRandall Stewart 			 */
2592f8829a4aSRandall Stewart 			struct socket *so, *oso;
2593f8829a4aSRandall Stewart 			struct sctp_inpcb *inp;
2594f8829a4aSRandall Stewart 
2595f8829a4aSRandall Stewart 			if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2596f8829a4aSRandall Stewart 				/*
2597f8829a4aSRandall Stewart 				 * For a restart we will keep the same
2598f8829a4aSRandall Stewart 				 * socket, no need to do anything. I THINK!!
2599f8829a4aSRandall Stewart 				 */
26007215cc1bSMichael Tuexen 				sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
26012fad0e55SMichael Tuexen 				if (send_int_conf) {
26022fad0e55SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
26032fad0e55SMichael Tuexen 					    (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
26042fad0e55SMichael Tuexen 				}
2605f8829a4aSRandall Stewart 				return (m);
2606f8829a4aSRandall Stewart 			}
2607f8829a4aSRandall Stewart 			oso = (*inp_p)->sctp_socket;
26082dad8a55SRandall Stewart 			atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2609f8829a4aSRandall Stewart 			SCTP_TCB_UNLOCK((*stcb));
26101fb51a12SBjoern A. Zeeb 			CURVNET_SET(oso->so_vnet);
261193164cf9SRandall Stewart 			so = sonewconn(oso, 0
2612f8829a4aSRandall Stewart 			    );
26131fb51a12SBjoern A. Zeeb 			CURVNET_RESTORE();
2614f8829a4aSRandall Stewart 			SCTP_TCB_LOCK((*stcb));
26152dad8a55SRandall Stewart 			atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
26162dad8a55SRandall Stewart 
2617f8829a4aSRandall Stewart 			if (so == NULL) {
2618f8829a4aSRandall Stewart 				struct mbuf *op_err;
261970486b27SMichael Tuexen 
2620f8829a4aSRandall Stewart 				/* Too many sockets */
2621ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2622ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2623f8829a4aSRandall Stewart 				sctp_abort_association(*inp_p, NULL, m, iphlen,
2624b1754ad1SMichael Tuexen 				    src, dst, sh, op_err,
2625457b4b88SMichael Tuexen 				    mflowtype, mflowid,
2626f30ac432SMichael Tuexen 				    vrf_id, port);
2627b7d130beSMichael Tuexen 				(void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC,
2628b7d130beSMichael Tuexen 				    SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
2629f8829a4aSRandall Stewart 				return (NULL);
2630f8829a4aSRandall Stewart 			}
2631f8829a4aSRandall Stewart 			inp = (struct sctp_inpcb *)so->so_pcb;
263293164cf9SRandall Stewart 			SCTP_INP_INCR_REF(inp);
263393164cf9SRandall Stewart 			/*
263493164cf9SRandall Stewart 			 * We add the unbound flag here so that if we get an
263593164cf9SRandall Stewart 			 * soabort() before we get the move_pcb done, we
263693164cf9SRandall Stewart 			 * will properly cleanup.
263793164cf9SRandall Stewart 			 */
2638f8829a4aSRandall Stewart 			inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2639f8829a4aSRandall Stewart 			    SCTP_PCB_FLAGS_CONNECTED |
2640f8829a4aSRandall Stewart 			    SCTP_PCB_FLAGS_IN_TCPPOOL |
264193164cf9SRandall Stewart 			    SCTP_PCB_FLAGS_UNBOUND |
2642f8829a4aSRandall Stewart 			    (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2643f8829a4aSRandall Stewart 			    SCTP_PCB_FLAGS_DONT_WAKE);
2644f8829a4aSRandall Stewart 			inp->sctp_features = (*inp_p)->sctp_features;
2645851b7298SRandall Stewart 			inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2646f8829a4aSRandall Stewart 			inp->sctp_socket = so;
2647f8829a4aSRandall Stewart 			inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
264859b6d5beSMichael Tuexen 			inp->max_cwnd = (*inp_p)->max_cwnd;
264920083c2eSMichael Tuexen 			inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2650f342355aSMichael Tuexen 			inp->ecn_supported = (*inp_p)->ecn_supported;
2651dd973b0eSMichael Tuexen 			inp->prsctp_supported = (*inp_p)->prsctp_supported;
2652c79bec9cSMichael Tuexen 			inp->auth_supported = (*inp_p)->auth_supported;
2653c79bec9cSMichael Tuexen 			inp->asconf_supported = (*inp_p)->asconf_supported;
2654317e00efSMichael Tuexen 			inp->reconfig_supported = (*inp_p)->reconfig_supported;
2655caea9879SMichael Tuexen 			inp->nrsack_supported = (*inp_p)->nrsack_supported;
2656cb9b8e6fSMichael Tuexen 			inp->pktdrop_supported = (*inp_p)->pktdrop_supported;
2657f8829a4aSRandall Stewart 			inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2658f8829a4aSRandall Stewart 			inp->sctp_context = (*inp_p)->sctp_context;
2659c4e848b7SRandall Stewart 			inp->local_strreset_support = (*inp_p)->local_strreset_support;
2660d089f9b9SMichael Tuexen 			inp->fibnum = (*inp_p)->fibnum;
2661f8829a4aSRandall Stewart 			/*
2662f8829a4aSRandall Stewart 			 * copy in the authentication parameters from the
2663f8829a4aSRandall Stewart 			 * original endpoint
2664f8829a4aSRandall Stewart 			 */
2665f8829a4aSRandall Stewart 			if (inp->sctp_ep.local_hmacs)
2666f8829a4aSRandall Stewart 				sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2667f8829a4aSRandall Stewart 			inp->sctp_ep.local_hmacs =
2668f8829a4aSRandall Stewart 			    sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2669f8829a4aSRandall Stewart 			if (inp->sctp_ep.local_auth_chunks)
2670f8829a4aSRandall Stewart 				sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2671f8829a4aSRandall Stewart 			inp->sctp_ep.local_auth_chunks =
2672f8829a4aSRandall Stewart 			    sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2673f8829a4aSRandall Stewart 
2674f8829a4aSRandall Stewart 			/*
2675f8829a4aSRandall Stewart 			 * Now we must move it from one hash table to
2676f8829a4aSRandall Stewart 			 * another and get the tcb in the right place.
2677f8829a4aSRandall Stewart 			 */
267888a7eb29SRandall Stewart 
267988a7eb29SRandall Stewart 			/*
268088a7eb29SRandall Stewart 			 * This is where the one-2-one socket is put into
268188a7eb29SRandall Stewart 			 * the accept state waiting for the accept!
268288a7eb29SRandall Stewart 			 */
268388a7eb29SRandall Stewart 			if (*stcb) {
2684839d21d6SMichael Tuexen 				SCTP_ADD_SUBSTATE(*stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
268588a7eb29SRandall Stewart 			}
2686f8829a4aSRandall Stewart 			sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2687d61a0ae0SRandall Stewart 
2688d61a0ae0SRandall Stewart 			atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2689d61a0ae0SRandall Stewart 			SCTP_TCB_UNLOCK((*stcb));
2690d61a0ae0SRandall Stewart 
2691265de5bbSRobert Watson 			sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2692265de5bbSRobert Watson 			    0);
2693d61a0ae0SRandall Stewart 			SCTP_TCB_LOCK((*stcb));
2694d61a0ae0SRandall Stewart 			atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2695d61a0ae0SRandall Stewart 
269693164cf9SRandall Stewart 			/*
269793164cf9SRandall Stewart 			 * now we must check to see if we were aborted while
269893164cf9SRandall Stewart 			 * the move was going on and the lock/unlock
269993164cf9SRandall Stewart 			 * happened.
270093164cf9SRandall Stewart 			 */
270193164cf9SRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
270293164cf9SRandall Stewart 				/*
270393164cf9SRandall Stewart 				 * yep it was, we leave the assoc attached
270493164cf9SRandall Stewart 				 * to the socket since the sctp_inpcb_free()
270593164cf9SRandall Stewart 				 * call will send an abort for us.
270693164cf9SRandall Stewart 				 */
270793164cf9SRandall Stewart 				SCTP_INP_DECR_REF(inp);
270893164cf9SRandall Stewart 				return (NULL);
270993164cf9SRandall Stewart 			}
271093164cf9SRandall Stewart 			SCTP_INP_DECR_REF(inp);
2711f8829a4aSRandall Stewart 			/* Switch over to the new guy */
2712f8829a4aSRandall Stewart 			*inp_p = inp;
2713ceaad40aSRandall Stewart 			sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
27142fad0e55SMichael Tuexen 			if (send_int_conf) {
27152fad0e55SMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
27162fad0e55SMichael Tuexen 				    (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
27172fad0e55SMichael Tuexen 			}
27180053ed28SMichael Tuexen 
2719b7b84c0eSMichael Tuexen 			/*
2720b7b84c0eSMichael Tuexen 			 * Pull it from the incomplete queue and wake the
2721b7b84c0eSMichael Tuexen 			 * guy
2722b7b84c0eSMichael Tuexen 			 */
272393164cf9SRandall Stewart 			soisconnected(so);
2724f8829a4aSRandall Stewart 			return (m);
2725f8829a4aSRandall Stewart 		}
2726f8829a4aSRandall Stewart 	}
27272fad0e55SMichael Tuexen 	if (notification) {
2728ceaad40aSRandall Stewart 		sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2729f8829a4aSRandall Stewart 	}
27302fad0e55SMichael Tuexen 	if (send_int_conf) {
27312fad0e55SMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
27322fad0e55SMichael Tuexen 		    (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
27332fad0e55SMichael Tuexen 	}
2734f8829a4aSRandall Stewart 	return (m);
2735f8829a4aSRandall Stewart }
2736f8829a4aSRandall Stewart 
2737f8829a4aSRandall Stewart static void
sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk * cp SCTP_UNUSED,struct sctp_tcb * stcb,struct sctp_nets * net)27387215cc1bSMichael Tuexen sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2739f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net)
2740f8829a4aSRandall Stewart {
2741f8829a4aSRandall Stewart 	/* cp must not be used, others call this without a c-ack :-) */
2742f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2743efd5e692SMichael Tuexen 	struct sctp_tmit_chunk *chk;
2744f8829a4aSRandall Stewart 
2745ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
2746ad81507eSRandall Stewart 	    "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2747ad234e3cSMichael Tuexen 	if ((stcb == NULL) || (net == NULL)) {
2748f8829a4aSRandall Stewart 		return;
2749ad234e3cSMichael Tuexen 	}
27500053ed28SMichael Tuexen 
2751f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2752469a65d1SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
2753469a65d1SMichael Tuexen 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2754469a65d1SMichael Tuexen 		    asoc->overall_error_count,
2755469a65d1SMichael Tuexen 		    0,
2756469a65d1SMichael Tuexen 		    SCTP_FROM_SCTP_INPUT,
2757469a65d1SMichael Tuexen 		    __LINE__);
2758469a65d1SMichael Tuexen 	}
2759f8829a4aSRandall Stewart 	sctp_stop_all_cookie_timers(stcb);
2760d9ae4adfSMichael Tuexen 	sctp_toss_old_cookies(stcb, asoc);
2761f8829a4aSRandall Stewart 	/* process according to association state */
2762839d21d6SMichael Tuexen 	if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) {
2763f8829a4aSRandall Stewart 		/* state change only needed when I am in right state */
2764ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2765839d21d6SMichael Tuexen 		SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
2766ca85e948SMichael Tuexen 		sctp_start_net_timers(stcb);
2767f8829a4aSRandall Stewart 		/* update RTO */
2768f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2769f8829a4aSRandall Stewart 		SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2770f8829a4aSRandall Stewart 		if (asoc->overall_error_count == 0) {
277144f2a327SMichael Tuexen 			sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered,
2772f79aab18SRandall Stewart 			    SCTP_RTT_FROM_NON_DATA);
2773f8829a4aSRandall Stewart 		}
27748ed1e2c8SMichael Tuexen 		/*
27758ed1e2c8SMichael Tuexen 		 * Since we did not send a HB make sure we don't double
27768ed1e2c8SMichael Tuexen 		 * things.
27778ed1e2c8SMichael Tuexen 		 */
27788ed1e2c8SMichael Tuexen 		asoc->overall_error_count = 0;
27798ed1e2c8SMichael Tuexen 		net->hb_responded = 1;
27806e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2781ceaad40aSRandall Stewart 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2782f8829a4aSRandall Stewart 		if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2783f8829a4aSRandall Stewart 		    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2784a5c2009dSMichael Tuexen 			sctp_pcb_add_flags(stcb->sctp_ep, SCTP_PCB_FLAGS_CONNECTED);
2785d69e7322SRandall Stewart 			if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
2786ceaad40aSRandall Stewart 				soisconnected(stcb->sctp_socket);
2787d69e7322SRandall Stewart 			}
2788f8829a4aSRandall Stewart 		}
2789f8829a4aSRandall Stewart 
2790d9ae4adfSMichael Tuexen 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
2791d9ae4adfSMichael Tuexen 		    TAILQ_EMPTY(&asoc->send_queue) &&
2792d9ae4adfSMichael Tuexen 		    TAILQ_EMPTY(&asoc->sent_queue) &&
2793d9ae4adfSMichael Tuexen 		    (asoc->stream_queue_cnt == 0)) {
2794d9ae4adfSMichael Tuexen 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2795d9ae4adfSMichael Tuexen 			SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
2796d9ae4adfSMichael Tuexen 			sctp_stop_timers_for_shutdown(stcb);
2797d9ae4adfSMichael Tuexen 			sctp_send_shutdown(stcb, net);
2798d9ae4adfSMichael Tuexen 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
2799d9ae4adfSMichael Tuexen 			    stcb->sctp_ep, stcb, net);
2800d9ae4adfSMichael Tuexen 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2801d9ae4adfSMichael Tuexen 			    stcb->sctp_ep, stcb, NULL);
2802d9ae4adfSMichael Tuexen 			sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
2803d9ae4adfSMichael Tuexen 		}
2804d9ae4adfSMichael Tuexen 
2805d69e7322SRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2806d69e7322SRandall Stewart 			/*
2807d69e7322SRandall Stewart 			 * We don't need to do the asconf thing, nor hb or
2808d69e7322SRandall Stewart 			 * autoclose if the socket is closed.
2809d69e7322SRandall Stewart 			 */
2810d69e7322SRandall Stewart 			goto closed_socket;
2811d69e7322SRandall Stewart 		}
28120053ed28SMichael Tuexen 
2813d69e7322SRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2814d69e7322SRandall Stewart 		    stcb, net);
2815d69e7322SRandall Stewart 
2816f8829a4aSRandall Stewart 		if (stcb->asoc.sctp_autoclose_ticks &&
2817f8829a4aSRandall Stewart 		    sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2818f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2819f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, NULL);
2820f8829a4aSRandall Stewart 		}
2821f8829a4aSRandall Stewart 		/*
28221b649582SRandall Stewart 		 * send ASCONF if parameters are pending and ASCONFs are
28231b649582SRandall Stewart 		 * allowed (eg. addresses changed when init/cookie echo were
28241b649582SRandall Stewart 		 * in flight)
2825f8829a4aSRandall Stewart 		 */
2826f8829a4aSRandall Stewart 		if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2827c79bec9cSMichael Tuexen 		    (stcb->asoc.asconf_supported == 1) &&
2828f8829a4aSRandall Stewart 		    (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
28291b649582SRandall Stewart #ifdef SCTP_TIMER_BASED_ASCONF
2830f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2831f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb,
2832f8829a4aSRandall Stewart 			    stcb->asoc.primary_destination);
28331b649582SRandall Stewart #else
28343232788eSRandall Stewart 			sctp_send_asconf(stcb, stcb->asoc.primary_destination,
28353232788eSRandall Stewart 			    SCTP_ADDR_NOT_LOCKED);
28361b649582SRandall Stewart #endif
2837f8829a4aSRandall Stewart 		}
2838f8829a4aSRandall Stewart 	}
2839d69e7322SRandall Stewart closed_socket:
2840f8829a4aSRandall Stewart 	/* Restart the timer if we have pending data */
2841efd5e692SMichael Tuexen 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
2842efd5e692SMichael Tuexen 		if (chk->whoTo != NULL) {
2843efd5e692SMichael Tuexen 			break;
2844efd5e692SMichael Tuexen 		}
2845efd5e692SMichael Tuexen 	}
2846efd5e692SMichael Tuexen 	if (chk != NULL) {
28474a9ef3f8SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
2848f8829a4aSRandall Stewart 	}
2849f8829a4aSRandall Stewart }
2850f8829a4aSRandall Stewart 
2851f8829a4aSRandall Stewart static void
sctp_handle_ecn_echo(struct sctp_ecne_chunk * cp,struct sctp_tcb * stcb)2852f8829a4aSRandall Stewart sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
2853f8829a4aSRandall Stewart     struct sctp_tcb *stcb)
2854f8829a4aSRandall Stewart {
2855f8829a4aSRandall Stewart 	struct sctp_nets *net;
2856f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *lchk;
2857a21779f0SRandall Stewart 	struct sctp_ecne_chunk bkup;
285860990c0cSMichael Tuexen 	uint8_t override_bit;
2859a21779f0SRandall Stewart 	uint32_t tsn, window_data_tsn;
2860a21779f0SRandall Stewart 	int len;
2861dec0177dSRandall Stewart 	unsigned int pkt_cnt;
2862f8829a4aSRandall Stewart 
2863a21779f0SRandall Stewart 	len = ntohs(cp->ch.chunk_length);
2864a21779f0SRandall Stewart 	if (len == sizeof(struct old_sctp_ecne_chunk)) {
2865a21779f0SRandall Stewart 		/* Its the old format */
2866a21779f0SRandall Stewart 		memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
2867a21779f0SRandall Stewart 		bkup.num_pkts_since_cwr = htonl(1);
2868a21779f0SRandall Stewart 		cp = &bkup;
2869a21779f0SRandall Stewart 	}
2870f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvecne);
2871f8829a4aSRandall Stewart 	tsn = ntohl(cp->tsn);
2872a21779f0SRandall Stewart 	pkt_cnt = ntohl(cp->num_pkts_since_cwr);
2873a21779f0SRandall Stewart 	lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
2874f8829a4aSRandall Stewart 	if (lchk == NULL) {
2875493d8e5aSRandall Stewart 		window_data_tsn = stcb->asoc.sending_seq - 1;
2876f8829a4aSRandall Stewart 	} else {
287749656eefSMichael Tuexen 		window_data_tsn = lchk->rec.data.tsn;
2878f8829a4aSRandall Stewart 	}
2879f8829a4aSRandall Stewart 
2880a21779f0SRandall Stewart 	/* Find where it was sent to if possible. */
2881f8829a4aSRandall Stewart 	net = NULL;
28824a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
288349656eefSMichael Tuexen 		if (lchk->rec.data.tsn == tsn) {
2884f8829a4aSRandall Stewart 			net = lchk->whoTo;
2885899288aeSRandall Stewart 			net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
2886f8829a4aSRandall Stewart 			break;
2887f8829a4aSRandall Stewart 		}
288849656eefSMichael Tuexen 		if (SCTP_TSN_GT(lchk->rec.data.tsn, tsn)) {
2889f8829a4aSRandall Stewart 			break;
2890f8829a4aSRandall Stewart 		}
289120b07a4dSMichael Tuexen 	}
2892a21779f0SRandall Stewart 	if (net == NULL) {
2893a21779f0SRandall Stewart 		/*
2894a21779f0SRandall Stewart 		 * What to do. A previous send of a CWR was possibly lost.
2895a21779f0SRandall Stewart 		 * See how old it is, we may have it marked on the actual
2896a21779f0SRandall Stewart 		 * net.
2897a21779f0SRandall Stewart 		 */
2898a21779f0SRandall Stewart 		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2899a21779f0SRandall Stewart 			if (tsn == net->last_cwr_tsn) {
2900a21779f0SRandall Stewart 				/* Found him, send it off */
290160990c0cSMichael Tuexen 				break;
2902a21779f0SRandall Stewart 			}
2903a21779f0SRandall Stewart 		}
290460990c0cSMichael Tuexen 		if (net == NULL) {
2905a21779f0SRandall Stewart 			/*
290660990c0cSMichael Tuexen 			 * If we reach here, we need to send a special CWR
290760990c0cSMichael Tuexen 			 * that says hey, we did this a long time ago and
290860990c0cSMichael Tuexen 			 * you lost the response.
2909a21779f0SRandall Stewart 			 */
2910a21779f0SRandall Stewart 			net = TAILQ_FIRST(&stcb->asoc.nets);
291160990c0cSMichael Tuexen 			if (net == NULL) {
291260990c0cSMichael Tuexen 				/* TSNH */
291360990c0cSMichael Tuexen 				return;
2914a21779f0SRandall Stewart 			}
291560990c0cSMichael Tuexen 			override_bit = SCTP_CWR_REDUCE_OVERRIDE;
291660990c0cSMichael Tuexen 		} else {
291760990c0cSMichael Tuexen 			override_bit = 0;
291860990c0cSMichael Tuexen 		}
291960990c0cSMichael Tuexen 	} else {
292060990c0cSMichael Tuexen 		override_bit = 0;
292160990c0cSMichael Tuexen 	}
2922493d8e5aSRandall Stewart 	if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
2923493d8e5aSRandall Stewart 	    ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
2924b7b84c0eSMichael Tuexen 		/*
2925b7b84c0eSMichael Tuexen 		 * JRS - Use the congestion control given in the pluggable
2926b7b84c0eSMichael Tuexen 		 * CC module
2927b7b84c0eSMichael Tuexen 		 */
2928493d8e5aSRandall Stewart 		stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
2929f8829a4aSRandall Stewart 		/*
2930a21779f0SRandall Stewart 		 * We reduce once every RTT. So we will only lower cwnd at
2931a21779f0SRandall Stewart 		 * the next sending seq i.e. the window_data_tsn
2932f8829a4aSRandall Stewart 		 */
2933a21779f0SRandall Stewart 		net->cwr_window_tsn = window_data_tsn;
2934a21779f0SRandall Stewart 		net->ecn_ce_pkt_cnt += pkt_cnt;
2935a21779f0SRandall Stewart 		net->lost_cnt = pkt_cnt;
2936a21779f0SRandall Stewart 		net->last_cwr_tsn = tsn;
2937a21779f0SRandall Stewart 	} else {
2938a21779f0SRandall Stewart 		override_bit |= SCTP_CWR_IN_SAME_WINDOW;
2939493d8e5aSRandall Stewart 		if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
2940493d8e5aSRandall Stewart 		    ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
2941a21779f0SRandall Stewart 			/*
2942493d8e5aSRandall Stewart 			 * Another loss in the same window update how many
2943493d8e5aSRandall Stewart 			 * marks/packets lost we have had.
2944a21779f0SRandall Stewart 			 */
2945493d8e5aSRandall Stewart 			int cnt = 1;
2946a21779f0SRandall Stewart 
2947a21779f0SRandall Stewart 			if (pkt_cnt > net->lost_cnt) {
2948a21779f0SRandall Stewart 				/* Should be the case */
2949493d8e5aSRandall Stewart 				cnt = (pkt_cnt - net->lost_cnt);
2950493d8e5aSRandall Stewart 				net->ecn_ce_pkt_cnt += cnt;
2951a21779f0SRandall Stewart 			}
2952493d8e5aSRandall Stewart 			net->lost_cnt = pkt_cnt;
2953a21779f0SRandall Stewart 			net->last_cwr_tsn = tsn;
2954493d8e5aSRandall Stewart 			/*
2955493d8e5aSRandall Stewart 			 * Most CC functions will ignore this call, since we
2956493d8e5aSRandall Stewart 			 * are in-window yet of the initial CE the peer saw.
2957493d8e5aSRandall Stewart 			 */
2958493d8e5aSRandall Stewart 			stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
2959a21779f0SRandall Stewart 		}
2960f8829a4aSRandall Stewart 	}
2961f8829a4aSRandall Stewart 	/*
2962f8829a4aSRandall Stewart 	 * We always send a CWR this way if our previous one was lost our
2963f8829a4aSRandall Stewart 	 * peer will get an update, or if it is not time again to reduce we
2964a21779f0SRandall Stewart 	 * still get the cwr to the peer. Note we set the override when we
2965a21779f0SRandall Stewart 	 * could not find the TSN on the chunk or the destination network.
2966f8829a4aSRandall Stewart 	 */
2967a21779f0SRandall Stewart 	sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
2968f8829a4aSRandall Stewart }
2969f8829a4aSRandall Stewart 
2970f8829a4aSRandall Stewart static void
sctp_handle_ecn_cwr(struct sctp_cwr_chunk * cp,struct sctp_tcb * stcb,struct sctp_nets * net)2971a21779f0SRandall Stewart sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
2972f8829a4aSRandall Stewart {
2973f8829a4aSRandall Stewart 	/*
2974f8829a4aSRandall Stewart 	 * Here we get a CWR from the peer. We must look in the outqueue and
2975582212faSEitan Adler 	 * make sure that we have a covered ECNE in the control chunk part.
2976f8829a4aSRandall Stewart 	 * If so remove it.
2977f8829a4aSRandall Stewart 	 */
29786c2cfc04SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
2979f8829a4aSRandall Stewart 	struct sctp_ecne_chunk *ecne;
2980a21779f0SRandall Stewart 	int override;
2981a21779f0SRandall Stewart 	uint32_t cwr_tsn;
2982f8829a4aSRandall Stewart 
2983a21779f0SRandall Stewart 	cwr_tsn = ntohl(cp->tsn);
2984a21779f0SRandall Stewart 	override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
29856c2cfc04SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &stcb->asoc.control_send_queue, sctp_next, nchk) {
2986f8829a4aSRandall Stewart 		if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
2987f8829a4aSRandall Stewart 			continue;
2988f8829a4aSRandall Stewart 		}
2989a21779f0SRandall Stewart 		if ((override == 0) && (chk->whoTo != net)) {
2990a21779f0SRandall Stewart 			/* Must be from the right src unless override is set */
2991a21779f0SRandall Stewart 			continue;
2992a21779f0SRandall Stewart 		}
2993f8829a4aSRandall Stewart 		ecne = mtod(chk->data, struct sctp_ecne_chunk *);
2994a21779f0SRandall Stewart 		if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
2995f8829a4aSRandall Stewart 			/* this covers this ECNE, we can remove it */
2996f8829a4aSRandall Stewart 			stcb->asoc.ecn_echo_cnt_onq--;
2997f8829a4aSRandall Stewart 			TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
2998f8829a4aSRandall Stewart 			    sctp_next);
2999cd6340caSMichael Tuexen 			stcb->asoc.ctrl_queue_cnt--;
3000f8829a4aSRandall Stewart 			sctp_m_freem(chk->data);
3001f8829a4aSRandall Stewart 			chk->data = NULL;
3002689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3003a21779f0SRandall Stewart 			if (override == 0) {
3004f8829a4aSRandall Stewart 				break;
3005f8829a4aSRandall Stewart 			}
3006f8829a4aSRandall Stewart 		}
3007f8829a4aSRandall Stewart 	}
3008a21779f0SRandall Stewart }
3009f8829a4aSRandall Stewart 
3010f8829a4aSRandall Stewart static void
sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk * cp SCTP_UNUSED,struct sctp_tcb * stcb,struct sctp_nets * net)30117215cc1bSMichael Tuexen sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3012f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net)
3013f8829a4aSRandall Stewart {
3014ceaad40aSRandall Stewart 
3015ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
3016ad81507eSRandall Stewart 	    "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3017f8829a4aSRandall Stewart 	if (stcb == NULL)
3018f8829a4aSRandall Stewart 		return;
3019f8829a4aSRandall Stewart 
3020f8829a4aSRandall Stewart 	/* process according to association state */
3021839d21d6SMichael Tuexen 	if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3022f8829a4aSRandall Stewart 		/* unexpected SHUTDOWN-COMPLETE... so ignore... */
30232afb3e84SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
30242afb3e84SRandall Stewart 		    "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3025f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3026f8829a4aSRandall Stewart 		return;
3027f8829a4aSRandall Stewart 	}
3028f8829a4aSRandall Stewart 	/* notify upper layer protocol */
3029f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
3030ceaad40aSRandall Stewart 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
303156f778aaSMichael Tuexen 	}
303256f778aaSMichael Tuexen #ifdef INVARIANTS
30330f1346f7SMichael Tuexen 	if (!TAILQ_EMPTY(&stcb->asoc.send_queue) ||
30340f1346f7SMichael Tuexen 	    !TAILQ_EMPTY(&stcb->asoc.sent_queue) ||
3035124d851aSMichael Tuexen 	    sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
303656f778aaSMichael Tuexen 		panic("Queues are not empty when handling SHUTDOWN-COMPLETE");
3037f8829a4aSRandall Stewart 	}
303856f778aaSMichael Tuexen #endif
3039f8829a4aSRandall Stewart 	/* stop the timer */
3040b7d130beSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net,
3041b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3042f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3043f8829a4aSRandall Stewart 	/* free the TCB */
30442afb3e84SRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
30452afb3e84SRandall Stewart 	    "sctp_handle_shutdown_complete: calls free-asoc\n");
3046b7d130beSMichael Tuexen 	(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
3047b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3048f8829a4aSRandall Stewart 	return;
3049f8829a4aSRandall Stewart }
3050f8829a4aSRandall Stewart 
3051f8829a4aSRandall Stewart static int
process_chunk_drop(struct sctp_tcb * stcb,struct sctp_chunk_desc * desc,struct sctp_nets * net,uint8_t flg)3052f8829a4aSRandall Stewart process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3053f8829a4aSRandall Stewart     struct sctp_nets *net, uint8_t flg)
3054f8829a4aSRandall Stewart {
3055f8829a4aSRandall Stewart 	switch (desc->chunk_type) {
3056f8829a4aSRandall Stewart 	case SCTP_DATA:
305732df1c9eSMichael Tuexen 	case SCTP_IDATA:
305832df1c9eSMichael Tuexen 		/* find the tsn to resend (possibly) */
3059f8829a4aSRandall Stewart 		{
3060f8829a4aSRandall Stewart 			uint32_t tsn;
3061f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *tp1;
3062f8829a4aSRandall Stewart 
3063f8829a4aSRandall Stewart 			tsn = ntohl(desc->tsn_ifany);
30644a9ef3f8SMichael Tuexen 			TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
306549656eefSMichael Tuexen 				if (tp1->rec.data.tsn == tsn) {
3066f8829a4aSRandall Stewart 					/* found it */
3067f8829a4aSRandall Stewart 					break;
3068f8829a4aSRandall Stewart 				}
306949656eefSMichael Tuexen 				if (SCTP_TSN_GT(tp1->rec.data.tsn, tsn)) {
3070f8829a4aSRandall Stewart 					/* not found */
3071f8829a4aSRandall Stewart 					tp1 = NULL;
3072f8829a4aSRandall Stewart 					break;
3073f8829a4aSRandall Stewart 				}
3074f8829a4aSRandall Stewart 			}
3075f8829a4aSRandall Stewart 			if (tp1 == NULL) {
3076f8829a4aSRandall Stewart 				/*
3077f8829a4aSRandall Stewart 				 * Do it the other way , aka without paying
3078f8829a4aSRandall Stewart 				 * attention to queue seq order.
3079f8829a4aSRandall Stewart 				 */
3080f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpdnfnd);
30814a9ef3f8SMichael Tuexen 				TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
308249656eefSMichael Tuexen 					if (tp1->rec.data.tsn == tsn) {
3083f8829a4aSRandall Stewart 						/* found it */
3084f8829a4aSRandall Stewart 						break;
3085f8829a4aSRandall Stewart 					}
3086f8829a4aSRandall Stewart 				}
3087f8829a4aSRandall Stewart 			}
3088f8829a4aSRandall Stewart 			if (tp1 == NULL) {
3089f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrptsnnf);
3090f8829a4aSRandall Stewart 			}
3091f8829a4aSRandall Stewart 			if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
30927da23bc8SMichael Tuexen 				if (((flg & SCTP_BADCRC) == 0) &&
30937da23bc8SMichael Tuexen 				    ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
30947da23bc8SMichael Tuexen 					return (0);
30957da23bc8SMichael Tuexen 				}
3096f8829a4aSRandall Stewart 				if ((stcb->asoc.peers_rwnd == 0) &&
3097f8829a4aSRandall Stewart 				    ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3098f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_pdrpdiwnp);
3099f8829a4aSRandall Stewart 					return (0);
3100f8829a4aSRandall Stewart 				}
3101f8829a4aSRandall Stewart 				if (stcb->asoc.peers_rwnd == 0 &&
3102f8829a4aSRandall Stewart 				    (flg & SCTP_FROM_MIDDLE_BOX)) {
3103f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_pdrpdizrw);
3104f8829a4aSRandall Stewart 					return (0);
3105f8829a4aSRandall Stewart 				}
310632df1c9eSMichael Tuexen 				if ((uint32_t)SCTP_BUF_LEN(tp1->data) <
310732df1c9eSMichael Tuexen 				    SCTP_DATA_CHUNK_OVERHEAD(stcb) + SCTP_NUM_DB_TO_VERIFY) {
310832df1c9eSMichael Tuexen 					/* Payload not matching. */
3109f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_pdrpbadd);
3110f8829a4aSRandall Stewart 					return (-1);
3111f8829a4aSRandall Stewart 				}
311232df1c9eSMichael Tuexen 				if (memcmp(mtod(tp1->data, caddr_t)+SCTP_DATA_CHUNK_OVERHEAD(stcb),
311332df1c9eSMichael Tuexen 				    desc->data_bytes, SCTP_NUM_DB_TO_VERIFY) != 0) {
311432df1c9eSMichael Tuexen 					/* Payload not matching. */
311532df1c9eSMichael Tuexen 					SCTP_STAT_INCR(sctps_pdrpbadd);
311632df1c9eSMichael Tuexen 					return (-1);
3117f8829a4aSRandall Stewart 				}
3118f8829a4aSRandall Stewart 				if (tp1->do_rtt) {
3119f8829a4aSRandall Stewart 					/*
3120f8829a4aSRandall Stewart 					 * this guy had a RTO calculation
3121f8829a4aSRandall Stewart 					 * pending on it, cancel it
3122f8829a4aSRandall Stewart 					 */
3123f79aab18SRandall Stewart 					if (tp1->whoTo->rto_needed == 0) {
3124f79aab18SRandall Stewart 						tp1->whoTo->rto_needed = 1;
3125f79aab18SRandall Stewart 					}
3126f8829a4aSRandall Stewart 					tp1->do_rtt = 0;
3127f8829a4aSRandall Stewart 				}
3128f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpmark);
3129f8829a4aSRandall Stewart 				if (tp1->sent != SCTP_DATAGRAM_RESEND)
3130f8829a4aSRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3131f8829a4aSRandall Stewart 				/*
3132f8829a4aSRandall Stewart 				 * mark it as if we were doing a FR, since
3133f8829a4aSRandall Stewart 				 * we will be getting gap ack reports behind
3134f8829a4aSRandall Stewart 				 * the info from the router.
3135f8829a4aSRandall Stewart 				 */
3136f8829a4aSRandall Stewart 				tp1->rec.data.doing_fast_retransmit = 1;
3137f8829a4aSRandall Stewart 				/*
3138f8829a4aSRandall Stewart 				 * mark the tsn with what sequences can
3139f8829a4aSRandall Stewart 				 * cause a new FR.
3140f8829a4aSRandall Stewart 				 */
3141f8829a4aSRandall Stewart 				if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3142f8829a4aSRandall Stewart 					tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3143f8829a4aSRandall Stewart 				} else {
314449656eefSMichael Tuexen 					tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.tsn;
3145f8829a4aSRandall Stewart 				}
3146f8829a4aSRandall Stewart 
3147f8829a4aSRandall Stewart 				/* restart the timer */
3148f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3149b7d130beSMichael Tuexen 				    stcb, tp1->whoTo,
3150b7d130beSMichael Tuexen 				    SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3151f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3152f8829a4aSRandall Stewart 				    stcb, tp1->whoTo);
3153f8829a4aSRandall Stewart 
3154f8829a4aSRandall Stewart 				/* fix counts and things */
3155b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3156c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3157a5d547adSRandall Stewart 					    tp1->whoTo->flight_size,
3158a5d547adSRandall Stewart 					    tp1->book_size,
31599a8e3088SMichael Tuexen 					    (uint32_t)(uintptr_t)stcb,
316049656eefSMichael Tuexen 					    tp1->rec.data.tsn);
316180fefe0aSRandall Stewart 				}
31628933fa13SRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3163c105859eSRandall Stewart 					sctp_flight_size_decrease(tp1);
3164c105859eSRandall Stewart 					sctp_total_flight_decrease(stcb, tp1);
31658933fa13SRandall Stewart 				}
3166f23ba7b1SMichael Tuexen 				tp1->sent = SCTP_DATAGRAM_RESEND;
3167f8829a4aSRandall Stewart 			} {
3168f8829a4aSRandall Stewart 				/* audit code */
3169f8829a4aSRandall Stewart 				unsigned int audit;
3170f8829a4aSRandall Stewart 
3171f8829a4aSRandall Stewart 				audit = 0;
3172f8829a4aSRandall Stewart 				TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3173f8829a4aSRandall Stewart 					if (tp1->sent == SCTP_DATAGRAM_RESEND)
3174f8829a4aSRandall Stewart 						audit++;
3175f8829a4aSRandall Stewart 				}
3176f8829a4aSRandall Stewart 				TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3177f8829a4aSRandall Stewart 				    sctp_next) {
3178f8829a4aSRandall Stewart 					if (tp1->sent == SCTP_DATAGRAM_RESEND)
3179f8829a4aSRandall Stewart 						audit++;
3180f8829a4aSRandall Stewart 				}
3181f8829a4aSRandall Stewart 				if (audit != stcb->asoc.sent_queue_retran_cnt) {
3182ad81507eSRandall Stewart 					SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3183f8829a4aSRandall Stewart 					    audit, stcb->asoc.sent_queue_retran_cnt);
3184f8829a4aSRandall Stewart #ifndef SCTP_AUDITING_ENABLED
3185f8829a4aSRandall Stewart 					stcb->asoc.sent_queue_retran_cnt = audit;
3186f8829a4aSRandall Stewart #endif
3187f8829a4aSRandall Stewart 				}
3188f8829a4aSRandall Stewart 			}
3189f8829a4aSRandall Stewart 		}
3190f8829a4aSRandall Stewart 		break;
3191f8829a4aSRandall Stewart 	case SCTP_ASCONF:
3192f8829a4aSRandall Stewart 		{
3193f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *asconf;
3194f8829a4aSRandall Stewart 
3195f8829a4aSRandall Stewart 			TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3196f8829a4aSRandall Stewart 			    sctp_next) {
3197f8829a4aSRandall Stewart 				if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3198f8829a4aSRandall Stewart 					break;
3199f8829a4aSRandall Stewart 				}
3200f8829a4aSRandall Stewart 			}
3201f8829a4aSRandall Stewart 			if (asconf) {
3202f8829a4aSRandall Stewart 				if (asconf->sent != SCTP_DATAGRAM_RESEND)
3203f8829a4aSRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3204f8829a4aSRandall Stewart 				asconf->sent = SCTP_DATAGRAM_RESEND;
3205f8829a4aSRandall Stewart 				asconf->snd_count--;
3206f8829a4aSRandall Stewart 			}
3207f8829a4aSRandall Stewart 		}
3208f8829a4aSRandall Stewart 		break;
3209f8829a4aSRandall Stewart 	case SCTP_INITIATION:
3210f8829a4aSRandall Stewart 		/* resend the INIT */
3211f8829a4aSRandall Stewart 		stcb->asoc.dropped_special_cnt++;
3212f8829a4aSRandall Stewart 		if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3213f8829a4aSRandall Stewart 			/*
3214f8829a4aSRandall Stewart 			 * If we can get it in, in a few attempts we do
3215f8829a4aSRandall Stewart 			 * this, otherwise we let the timer fire.
3216f8829a4aSRandall Stewart 			 */
3217f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3218b7d130beSMichael Tuexen 			    stcb, net,
3219b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
3220ceaad40aSRandall Stewart 			sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3221f8829a4aSRandall Stewart 		}
3222f8829a4aSRandall Stewart 		break;
3223f8829a4aSRandall Stewart 	case SCTP_SELECTIVE_ACK:
3224b5c16493SMichael Tuexen 	case SCTP_NR_SELECTIVE_ACK:
3225f8829a4aSRandall Stewart 		/* resend the sack */
3226689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3227f8829a4aSRandall Stewart 		break;
3228f8829a4aSRandall Stewart 	case SCTP_HEARTBEAT_REQUEST:
3229f8829a4aSRandall Stewart 		/* resend a demand HB */
3230b54d3a6cSRandall Stewart 		if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3231b7b84c0eSMichael Tuexen 			/*
3232b7b84c0eSMichael Tuexen 			 * Only retransmit if we KNOW we wont destroy the
3233b7b84c0eSMichael Tuexen 			 * tcb
3234b7b84c0eSMichael Tuexen 			 */
3235ca85e948SMichael Tuexen 			sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3236b54d3a6cSRandall Stewart 		}
3237f8829a4aSRandall Stewart 		break;
3238f8829a4aSRandall Stewart 	case SCTP_SHUTDOWN:
3239f8829a4aSRandall Stewart 		sctp_send_shutdown(stcb, net);
3240f8829a4aSRandall Stewart 		break;
3241f8829a4aSRandall Stewart 	case SCTP_SHUTDOWN_ACK:
3242f8829a4aSRandall Stewart 		sctp_send_shutdown_ack(stcb, net);
3243f8829a4aSRandall Stewart 		break;
3244f8829a4aSRandall Stewart 	case SCTP_COOKIE_ECHO:
3245f8829a4aSRandall Stewart 		{
3246f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *cookie;
3247f8829a4aSRandall Stewart 
3248f8829a4aSRandall Stewart 			cookie = NULL;
3249f8829a4aSRandall Stewart 			TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3250f8829a4aSRandall Stewart 			    sctp_next) {
3251f8829a4aSRandall Stewart 				if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3252f8829a4aSRandall Stewart 					break;
3253f8829a4aSRandall Stewart 				}
3254f8829a4aSRandall Stewart 			}
3255f8829a4aSRandall Stewart 			if (cookie) {
3256f8829a4aSRandall Stewart 				if (cookie->sent != SCTP_DATAGRAM_RESEND)
3257f8829a4aSRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3258f8829a4aSRandall Stewart 				cookie->sent = SCTP_DATAGRAM_RESEND;
3259f8829a4aSRandall Stewart 				sctp_stop_all_cookie_timers(stcb);
3260f8829a4aSRandall Stewart 			}
3261f8829a4aSRandall Stewart 		}
3262f8829a4aSRandall Stewart 		break;
3263f8829a4aSRandall Stewart 	case SCTP_COOKIE_ACK:
3264f8829a4aSRandall Stewart 		sctp_send_cookie_ack(stcb);
3265f8829a4aSRandall Stewart 		break;
3266f8829a4aSRandall Stewart 	case SCTP_ASCONF_ACK:
3267f8829a4aSRandall Stewart 		/* resend last asconf ack */
32682afb3e84SRandall Stewart 		sctp_send_asconf_ack(stcb);
3269f8829a4aSRandall Stewart 		break;
327044249214SRandall Stewart 	case SCTP_IFORWARD_CUM_TSN:
3271f8829a4aSRandall Stewart 	case SCTP_FORWARD_CUM_TSN:
3272f8829a4aSRandall Stewart 		send_forward_tsn(stcb, &stcb->asoc);
3273f8829a4aSRandall Stewart 		break;
3274f8829a4aSRandall Stewart 		/* can't do anything with these */
3275f8829a4aSRandall Stewart 	case SCTP_PACKET_DROPPED:
3276f8829a4aSRandall Stewart 	case SCTP_INITIATION_ACK:	/* this should not happen */
3277f8829a4aSRandall Stewart 	case SCTP_HEARTBEAT_ACK:
3278f8829a4aSRandall Stewart 	case SCTP_ABORT_ASSOCIATION:
3279f8829a4aSRandall Stewart 	case SCTP_OPERATION_ERROR:
3280f8829a4aSRandall Stewart 	case SCTP_SHUTDOWN_COMPLETE:
3281f8829a4aSRandall Stewart 	case SCTP_ECN_ECHO:
3282f8829a4aSRandall Stewart 	case SCTP_ECN_CWR:
3283f8829a4aSRandall Stewart 	default:
3284f8829a4aSRandall Stewart 		break;
3285f8829a4aSRandall Stewart 	}
3286f8829a4aSRandall Stewart 	return (0);
3287f8829a4aSRandall Stewart }
3288f8829a4aSRandall Stewart 
3289f8829a4aSRandall Stewart void
sctp_reset_in_stream(struct sctp_tcb * stcb,uint32_t number_entries,uint16_t * list)3290a169d6ecSMichael Tuexen sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3291f8829a4aSRandall Stewart {
3292a169d6ecSMichael Tuexen 	uint32_t i;
3293f8829a4aSRandall Stewart 	uint16_t temp;
3294f8829a4aSRandall Stewart 
3295f8829a4aSRandall Stewart 	/*
329644249214SRandall Stewart 	 * We set things to 0xffffffff since this is the last delivered
329744249214SRandall Stewart 	 * sequence and we will be sending in 0 after the reset.
3298f8829a4aSRandall Stewart 	 */
3299f8829a4aSRandall Stewart 
3300f8829a4aSRandall Stewart 	if (number_entries) {
3301f8829a4aSRandall Stewart 		for (i = 0; i < number_entries; i++) {
3302f8829a4aSRandall Stewart 			temp = ntohs(list[i]);
3303f8829a4aSRandall Stewart 			if (temp >= stcb->asoc.streamincnt) {
3304f8829a4aSRandall Stewart 				continue;
3305f8829a4aSRandall Stewart 			}
330649656eefSMichael Tuexen 			stcb->asoc.strmin[temp].last_mid_delivered = 0xffffffff;
3307f8829a4aSRandall Stewart 		}
3308f8829a4aSRandall Stewart 	} else {
3309f8829a4aSRandall Stewart 		list = NULL;
3310f8829a4aSRandall Stewart 		for (i = 0; i < stcb->asoc.streamincnt; i++) {
331149656eefSMichael Tuexen 			stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
3312f8829a4aSRandall Stewart 		}
3313f8829a4aSRandall Stewart 	}
3314ceaad40aSRandall Stewart 	sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3315f8829a4aSRandall Stewart }
3316f8829a4aSRandall Stewart 
3317f8829a4aSRandall Stewart static void
sctp_reset_out_streams(struct sctp_tcb * stcb,uint32_t number_entries,uint16_t * list)331856f778aaSMichael Tuexen sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3319f8829a4aSRandall Stewart {
332056f778aaSMichael Tuexen 	uint32_t i;
3321f8829a4aSRandall Stewart 	uint16_t temp;
3322f8829a4aSRandall Stewart 
332356f778aaSMichael Tuexen 	if (number_entries > 0) {
332456f778aaSMichael Tuexen 		for (i = 0; i < number_entries; i++) {
3325f8829a4aSRandall Stewart 			temp = ntohs(list[i]);
3326f8829a4aSRandall Stewart 			if (temp >= stcb->asoc.streamoutcnt) {
3327f8829a4aSRandall Stewart 				/* no such stream */
3328f8829a4aSRandall Stewart 				continue;
3329f8829a4aSRandall Stewart 			}
333063d5b568SMichael Tuexen 			stcb->asoc.strmout[temp].next_mid_ordered = 0;
333163d5b568SMichael Tuexen 			stcb->asoc.strmout[temp].next_mid_unordered = 0;
3332f8829a4aSRandall Stewart 		}
333356f778aaSMichael Tuexen 	} else {
333456f778aaSMichael Tuexen 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
333563d5b568SMichael Tuexen 			stcb->asoc.strmout[i].next_mid_ordered = 0;
333663d5b568SMichael Tuexen 			stcb->asoc.strmout[i].next_mid_unordered = 0;
333756f778aaSMichael Tuexen 		}
3338f8829a4aSRandall Stewart 	}
3339ceaad40aSRandall Stewart 	sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3340f8829a4aSRandall Stewart }
3341f8829a4aSRandall Stewart 
33427cca1775SRandall Stewart static void
sctp_reset_clear_pending(struct sctp_tcb * stcb,uint32_t number_entries,uint16_t * list)33437cca1775SRandall Stewart sctp_reset_clear_pending(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
33447cca1775SRandall Stewart {
33457cca1775SRandall Stewart 	uint32_t i;
33467cca1775SRandall Stewart 	uint16_t temp;
33477cca1775SRandall Stewart 
33487cca1775SRandall Stewart 	if (number_entries > 0) {
33497cca1775SRandall Stewart 		for (i = 0; i < number_entries; i++) {
33507cca1775SRandall Stewart 			temp = ntohs(list[i]);
33517cca1775SRandall Stewart 			if (temp >= stcb->asoc.streamoutcnt) {
33527cca1775SRandall Stewart 				/* no such stream */
33537cca1775SRandall Stewart 				continue;
33547cca1775SRandall Stewart 			}
33557cca1775SRandall Stewart 			stcb->asoc.strmout[temp].state = SCTP_STREAM_OPEN;
33567cca1775SRandall Stewart 		}
33577cca1775SRandall Stewart 	} else {
33587cca1775SRandall Stewart 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
33597cca1775SRandall Stewart 			stcb->asoc.strmout[i].state = SCTP_STREAM_OPEN;
33607cca1775SRandall Stewart 		}
33617cca1775SRandall Stewart 	}
33627cca1775SRandall Stewart }
33637cca1775SRandall Stewart 
336484f3b49aSMichael Tuexen struct sctp_stream_reset_request *
sctp_find_stream_reset(struct sctp_tcb * stcb,uint32_t seq,struct sctp_tmit_chunk ** bchk)3365f8829a4aSRandall Stewart sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3366f8829a4aSRandall Stewart {
3367f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3368a169d6ecSMichael Tuexen 	struct sctp_chunkhdr *ch;
336984f3b49aSMichael Tuexen 	struct sctp_stream_reset_request *r;
3370f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
3371f8829a4aSRandall Stewart 	int len, clen;
3372f8829a4aSRandall Stewart 
3373f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
3374163153c2SMichael Tuexen 	chk = asoc->str_reset;
3375163153c2SMichael Tuexen 	if (TAILQ_EMPTY(&asoc->control_send_queue) ||
3376163153c2SMichael Tuexen 	    (chk == NULL)) {
3377d06c82f1SRandall Stewart 		asoc->stream_reset_outstanding = 0;
3378f8829a4aSRandall Stewart 		return (NULL);
3379f8829a4aSRandall Stewart 	}
3380f8829a4aSRandall Stewart 	if (chk->data == NULL) {
3381f8829a4aSRandall Stewart 		return (NULL);
3382f8829a4aSRandall Stewart 	}
3383163153c2SMichael Tuexen 	if (bchk != NULL) {
3384f8829a4aSRandall Stewart 		/* he wants a copy of the chk pointer */
3385f8829a4aSRandall Stewart 		*bchk = chk;
3386f8829a4aSRandall Stewart 	}
3387f8829a4aSRandall Stewart 	clen = chk->send_size;
3388a169d6ecSMichael Tuexen 	ch = mtod(chk->data, struct sctp_chunkhdr *);
338984f3b49aSMichael Tuexen 	r = (struct sctp_stream_reset_request *)(ch + 1);
3390f8829a4aSRandall Stewart 	if (ntohl(r->request_seq) == seq) {
3391f8829a4aSRandall Stewart 		/* found it */
3392f8829a4aSRandall Stewart 		return (r);
3393f8829a4aSRandall Stewart 	}
3394f8829a4aSRandall Stewart 	len = SCTP_SIZE32(ntohs(r->ph.param_length));
3395f8829a4aSRandall Stewart 	if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3396f8829a4aSRandall Stewart 		/* move to the next one, there can only be a max of two */
339784f3b49aSMichael Tuexen 		r = (struct sctp_stream_reset_request *)((caddr_t)r + len);
3398f8829a4aSRandall Stewart 		if (ntohl(r->request_seq) == seq) {
3399f8829a4aSRandall Stewart 			return (r);
3400f8829a4aSRandall Stewart 		}
3401f8829a4aSRandall Stewart 	}
3402f8829a4aSRandall Stewart 	/* that seq is not here */
3403f8829a4aSRandall Stewart 	return (NULL);
3404f8829a4aSRandall Stewart }
3405f8829a4aSRandall Stewart 
3406f8829a4aSRandall Stewart static void
sctp_clean_up_stream_reset(struct sctp_tcb * stcb)3407f8829a4aSRandall Stewart sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3408f8829a4aSRandall Stewart {
3409f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3410cd6340caSMichael Tuexen 	struct sctp_tmit_chunk *chk;
3411f8829a4aSRandall Stewart 
3412cd6340caSMichael Tuexen 	asoc = &stcb->asoc;
3413cd6340caSMichael Tuexen 	chk = asoc->str_reset;
3414cd6340caSMichael Tuexen 	if (chk == NULL) {
3415f8829a4aSRandall Stewart 		return;
3416f8829a4aSRandall Stewart 	}
3417cd6340caSMichael Tuexen 	asoc->str_reset = NULL;
3418b7d130beSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb,
34196fb7b4fbSMichael Tuexen 	    NULL, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28);
3420cd6340caSMichael Tuexen 	TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
3421cd6340caSMichael Tuexen 	asoc->ctrl_queue_cnt--;
3422f8829a4aSRandall Stewart 	if (chk->data) {
3423f8829a4aSRandall Stewart 		sctp_m_freem(chk->data);
3424f8829a4aSRandall Stewart 		chk->data = NULL;
3425f8829a4aSRandall Stewart 	}
3426689e6a5fSMichael Tuexen 	sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3427f8829a4aSRandall Stewart }
3428f8829a4aSRandall Stewart 
3429f8829a4aSRandall Stewart static int
sctp_handle_stream_reset_response(struct sctp_tcb * stcb,uint32_t seq,uint32_t action,struct sctp_stream_reset_response * respin)3430f8829a4aSRandall Stewart sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3431f8829a4aSRandall Stewart     uint32_t seq, uint32_t action,
343208598d70SRandall Stewart     struct sctp_stream_reset_response *respin)
3433f8829a4aSRandall Stewart {
3434f8829a4aSRandall Stewart 	uint16_t type;
3435f4358911SMichael Tuexen 	int lparam_len;
3436f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3437f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
343884f3b49aSMichael Tuexen 	struct sctp_stream_reset_request *req_param;
343984f3b49aSMichael Tuexen 	struct sctp_stream_reset_out_request *req_out_param;
344084f3b49aSMichael Tuexen 	struct sctp_stream_reset_in_request *req_in_param;
344156f778aaSMichael Tuexen 	uint32_t number_entries;
3442f8829a4aSRandall Stewart 
3443f8829a4aSRandall Stewart 	if (asoc->stream_reset_outstanding == 0) {
3444f8829a4aSRandall Stewart 		/* duplicate */
3445f8829a4aSRandall Stewart 		return (0);
3446f8829a4aSRandall Stewart 	}
3447f8829a4aSRandall Stewart 	if (seq == stcb->asoc.str_reset_seq_out) {
344884f3b49aSMichael Tuexen 		req_param = sctp_find_stream_reset(stcb, seq, &chk);
344984f3b49aSMichael Tuexen 		if (req_param != NULL) {
3450f8829a4aSRandall Stewart 			stcb->asoc.str_reset_seq_out++;
345184f3b49aSMichael Tuexen 			type = ntohs(req_param->ph.param_type);
3452f4358911SMichael Tuexen 			lparam_len = ntohs(req_param->ph.param_length);
3453f8829a4aSRandall Stewart 			if (type == SCTP_STR_RESET_OUT_REQUEST) {
34547cca1775SRandall Stewart 				int no_clear = 0;
34557cca1775SRandall Stewart 
345684f3b49aSMichael Tuexen 				req_out_param = (struct sctp_stream_reset_out_request *)req_param;
3457f4358911SMichael Tuexen 				number_entries = (lparam_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3458f8829a4aSRandall Stewart 				asoc->stream_reset_out_is_outstanding = 0;
3459f8829a4aSRandall Stewart 				if (asoc->stream_reset_outstanding)
3460f8829a4aSRandall Stewart 					asoc->stream_reset_outstanding--;
3461f3ebe64cSMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3462f8829a4aSRandall Stewart 					/* do it */
346384f3b49aSMichael Tuexen 					sctp_reset_out_streams(stcb, number_entries, req_out_param->list_of_streams);
3464d4260646SMichael Tuexen 				} else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
346584f3b49aSMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
34667cca1775SRandall Stewart 				} else if (action == SCTP_STREAM_RESET_RESULT_IN_PROGRESS) {
3467b7b84c0eSMichael Tuexen 					/*
3468b7b84c0eSMichael Tuexen 					 * Set it up so we don't stop
3469b7b84c0eSMichael Tuexen 					 * retransmitting
3470b7b84c0eSMichael Tuexen 					 */
3471a4889f2dSMichael Tuexen 					asoc->stream_reset_outstanding++;
34727cca1775SRandall Stewart 					stcb->asoc.str_reset_seq_out--;
34737cca1775SRandall Stewart 					asoc->stream_reset_out_is_outstanding = 1;
34747cca1775SRandall Stewart 					no_clear = 1;
3475f8829a4aSRandall Stewart 				} else {
347684f3b49aSMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3477f8829a4aSRandall Stewart 				}
34787cca1775SRandall Stewart 				if (no_clear == 0) {
34797cca1775SRandall Stewart 					sctp_reset_clear_pending(stcb, number_entries, req_out_param->list_of_streams);
34807cca1775SRandall Stewart 				}
3481f8829a4aSRandall Stewart 			} else if (type == SCTP_STR_RESET_IN_REQUEST) {
348284f3b49aSMichael Tuexen 				req_in_param = (struct sctp_stream_reset_in_request *)req_param;
3483f4358911SMichael Tuexen 				number_entries = (lparam_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3484f8829a4aSRandall Stewart 				if (asoc->stream_reset_outstanding)
3485f8829a4aSRandall Stewart 					asoc->stream_reset_outstanding--;
3486d4260646SMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3487d4260646SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
348884f3b49aSMichael Tuexen 					    number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3489d4260646SMichael Tuexen 				} else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3490c4e848b7SRandall Stewart 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
349184f3b49aSMichael Tuexen 					    number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3492f8829a4aSRandall Stewart 				}
3493c4e848b7SRandall Stewart 			} else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3494ea44232bSRandall Stewart 				/* Ok we now may have more streams */
3495c4e848b7SRandall Stewart 				int num_stream;
3496c4e848b7SRandall Stewart 
3497c4e848b7SRandall Stewart 				num_stream = stcb->asoc.strm_pending_add_size;
3498c4e848b7SRandall Stewart 				if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3499c4e848b7SRandall Stewart 					/* TSNH */
3500c4e848b7SRandall Stewart 					num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3501c4e848b7SRandall Stewart 				}
3502c4e848b7SRandall Stewart 				stcb->asoc.strm_pending_add_size = 0;
35038aae9493SRandall Stewart 				if (asoc->stream_reset_outstanding)
35048aae9493SRandall Stewart 					asoc->stream_reset_outstanding--;
3505f3ebe64cSMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3506ea44232bSRandall Stewart 					/* Put the new streams into effect */
35077cca1775SRandall Stewart 					int i;
35087cca1775SRandall Stewart 
35097cca1775SRandall Stewart 					for (i = asoc->streamoutcnt; i < (asoc->streamoutcnt + num_stream); i++) {
35107cca1775SRandall Stewart 						asoc->strmout[i].state = SCTP_STREAM_OPEN;
35117cca1775SRandall Stewart 					}
35127cca1775SRandall Stewart 					asoc->streamoutcnt += num_stream;
35133ac76647SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3514d4260646SMichael Tuexen 				} else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
35153ac76647SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD, stcb, SCTP_STREAM_CHANGE_DENIED, NULL, SCTP_SO_NOT_LOCKED);
3516ea44232bSRandall Stewart 				} else {
35173ac76647SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD, stcb, SCTP_STREAM_CHANGE_FAILED, NULL, SCTP_SO_NOT_LOCKED);
3518c4e848b7SRandall Stewart 				}
3519c4e848b7SRandall Stewart 			} else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3520c4e848b7SRandall Stewart 				if (asoc->stream_reset_outstanding)
3521c4e848b7SRandall Stewart 					asoc->stream_reset_outstanding--;
3522d4260646SMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
35233ac76647SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD, stcb, SCTP_STREAM_CHANGE_DENIED, NULL, SCTP_SO_NOT_LOCKED);
3524d4260646SMichael Tuexen 				} else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
35253ac76647SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD, stcb, SCTP_STREAM_CHANGE_DENIED, NULL, SCTP_SO_NOT_LOCKED);
3526ea44232bSRandall Stewart 				}
3527f8829a4aSRandall Stewart 			} else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3528f8829a4aSRandall Stewart 				/**
3529f8829a4aSRandall Stewart 				 * a) Adopt the new in tsn.
3530f8829a4aSRandall Stewart 				 * b) reset the map
3531f8829a4aSRandall Stewart 				 * c) Adopt the new out-tsn
3532f8829a4aSRandall Stewart 				 */
3533f8829a4aSRandall Stewart 				struct sctp_stream_reset_response_tsn *resp;
3534f8829a4aSRandall Stewart 				struct sctp_forward_tsn_chunk fwdtsn;
3535f8829a4aSRandall Stewart 				int abort_flag = 0;
3536f8829a4aSRandall Stewart 
3537f8829a4aSRandall Stewart 				if (respin == NULL) {
3538f8829a4aSRandall Stewart 					/* huh ? */
3539f8829a4aSRandall Stewart 					return (0);
3540f8829a4aSRandall Stewart 				}
35416a58f0e9SXin LI 				if (ntohs(respin->ph.param_length) < sizeof(struct sctp_stream_reset_response_tsn)) {
35426a58f0e9SXin LI 					return (0);
35436a58f0e9SXin LI 				}
3544f3ebe64cSMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3545f8829a4aSRandall Stewart 					resp = (struct sctp_stream_reset_response_tsn *)respin;
3546f8829a4aSRandall Stewart 					asoc->stream_reset_outstanding--;
3547f8829a4aSRandall Stewart 					fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3548f8829a4aSRandall Stewart 					fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3549f8829a4aSRandall Stewart 					fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3550671d309cSRandall Stewart 					sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3551f8829a4aSRandall Stewart 					if (abort_flag) {
3552f8829a4aSRandall Stewart 						return (1);
3553f8829a4aSRandall Stewart 					}
3554f8829a4aSRandall Stewart 					stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3555b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3556b3f1ea41SRandall Stewart 						sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3557b3f1ea41SRandall Stewart 					}
35580053ed28SMichael Tuexen 
3559d6af161aSRandall Stewart 					stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3560f8829a4aSRandall Stewart 					stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3561f8829a4aSRandall Stewart 					memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3562830d754dSRandall Stewart 
3563830d754dSRandall Stewart 					stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3564b5c16493SMichael Tuexen 					memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
356577acdc25SRandall Stewart 
3566f8829a4aSRandall Stewart 					stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3567f8829a4aSRandall Stewart 					stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3568f8829a4aSRandall Stewart 
3569f8829a4aSRandall Stewart 					sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3570f8829a4aSRandall Stewart 					sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
35713ac76647SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_TSN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3572d4260646SMichael Tuexen 				} else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
35733ac76647SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_TSN, stcb, SCTP_ASSOC_RESET_DENIED, NULL, SCTP_SO_NOT_LOCKED);
3574c4e848b7SRandall Stewart 				} else {
35753ac76647SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_TSN, stcb, SCTP_ASSOC_RESET_FAILED, NULL, SCTP_SO_NOT_LOCKED);
3576f8829a4aSRandall Stewart 				}
3577f8829a4aSRandall Stewart 			}
3578f8829a4aSRandall Stewart 			/* get rid of the request and get the request flags */
3579f8829a4aSRandall Stewart 			if (asoc->stream_reset_outstanding == 0) {
3580f8829a4aSRandall Stewart 				sctp_clean_up_stream_reset(stcb);
3581f8829a4aSRandall Stewart 			}
3582f8829a4aSRandall Stewart 		}
3583f8829a4aSRandall Stewart 	}
35847cca1775SRandall Stewart 	if (asoc->stream_reset_outstanding == 0) {
3585c6168599SRandall Stewart 		sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
35867cca1775SRandall Stewart 	}
3587f8829a4aSRandall Stewart 	return (0);
3588f8829a4aSRandall Stewart }
3589f8829a4aSRandall Stewart 
3590f8829a4aSRandall Stewart static void
sctp_handle_str_reset_request_in(struct sctp_tcb * stcb,struct sctp_tmit_chunk * chk,struct sctp_stream_reset_in_request * req,int trunc)3591f8829a4aSRandall Stewart sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3592f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk,
3593671d309cSRandall Stewart     struct sctp_stream_reset_in_request *req, int trunc)
3594f8829a4aSRandall Stewart {
3595f8829a4aSRandall Stewart 	uint32_t seq;
3596f8829a4aSRandall Stewart 	int len, i;
3597f8829a4aSRandall Stewart 	int number_entries;
3598f8829a4aSRandall Stewart 	uint16_t temp;
3599f8829a4aSRandall Stewart 
3600f8829a4aSRandall Stewart 	/*
3601f8829a4aSRandall Stewart 	 * peer wants me to send a str-reset to him for my outgoing seq's if
3602f8829a4aSRandall Stewart 	 * seq_in is right.
3603f8829a4aSRandall Stewart 	 */
3604f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3605f8829a4aSRandall Stewart 
3606f8829a4aSRandall Stewart 	seq = ntohl(req->request_seq);
3607f8829a4aSRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3608671d309cSRandall Stewart 		asoc->last_reset_action[1] = asoc->last_reset_action[0];
36099b2a35b3SMichael Tuexen 		if ((asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ) == 0) {
3610f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3611f3ebe64cSMichael Tuexen 		} else if (trunc) {
3612f3ebe64cSMichael Tuexen 			/* Can't do it, since they exceeded our buffer size  */
3613f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3614671d309cSRandall Stewart 		} else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3615f8829a4aSRandall Stewart 			len = ntohs(req->ph.param_length);
3616f8829a4aSRandall Stewart 			number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
36177cca1775SRandall Stewart 			if (number_entries) {
3618f8829a4aSRandall Stewart 				for (i = 0; i < number_entries; i++) {
3619f8829a4aSRandall Stewart 					temp = ntohs(req->list_of_streams[i]);
36207cca1775SRandall Stewart 					if (temp >= stcb->asoc.streamoutcnt) {
36217cca1775SRandall Stewart 						asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
36227cca1775SRandall Stewart 						goto bad_boy;
36237cca1775SRandall Stewart 					}
3624f8829a4aSRandall Stewart 					req->list_of_streams[i] = temp;
3625f8829a4aSRandall Stewart 				}
36267cca1775SRandall Stewart 				for (i = 0; i < number_entries; i++) {
36277cca1775SRandall Stewart 					if (stcb->asoc.strmout[req->list_of_streams[i]].state == SCTP_STREAM_OPEN) {
36287cca1775SRandall Stewart 						stcb->asoc.strmout[req->list_of_streams[i]].state = SCTP_STREAM_RESET_PENDING;
36297cca1775SRandall Stewart 					}
36307cca1775SRandall Stewart 				}
36317cca1775SRandall Stewart 			} else {
36327cca1775SRandall Stewart 				/* Its all */
36337cca1775SRandall Stewart 				for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
36347cca1775SRandall Stewart 					if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN)
36357cca1775SRandall Stewart 						stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
36367cca1775SRandall Stewart 				}
36377cca1775SRandall Stewart 			}
3638f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3639f8829a4aSRandall Stewart 		} else {
3640f8829a4aSRandall Stewart 			/* Can't do it, since we have sent one out */
3641f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3642f8829a4aSRandall Stewart 		}
36437cca1775SRandall Stewart bad_boy:
3644c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3645f8829a4aSRandall Stewart 		asoc->str_reset_seq_in++;
3646f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 1 == seq) {
3647f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3648f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 2 == seq) {
3649f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3650f8829a4aSRandall Stewart 	} else {
3651f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3652f8829a4aSRandall Stewart 	}
3653c6168599SRandall Stewart 	sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3654f8829a4aSRandall Stewart }
3655f8829a4aSRandall Stewart 
3656f8829a4aSRandall Stewart static int
sctp_handle_str_reset_request_tsn(struct sctp_tcb * stcb,struct sctp_tmit_chunk * chk,struct sctp_stream_reset_tsn_request * req)3657f8829a4aSRandall Stewart sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3658f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk,
3659f8829a4aSRandall Stewart     struct sctp_stream_reset_tsn_request *req)
3660f8829a4aSRandall Stewart {
3661f8829a4aSRandall Stewart 	/* reset all in and out and update the tsn */
3662f8829a4aSRandall Stewart 	/*
3663f8829a4aSRandall Stewart 	 * A) reset my str-seq's on in and out. B) Select a receive next,
3664f8829a4aSRandall Stewart 	 * and set cum-ack to it. Also process this selected number as a
3665f8829a4aSRandall Stewart 	 * fwd-tsn as well. C) set in the response my next sending seq.
3666f8829a4aSRandall Stewart 	 */
3667f8829a4aSRandall Stewart 	struct sctp_forward_tsn_chunk fwdtsn;
3668f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3669f8829a4aSRandall Stewart 	int abort_flag = 0;
3670f8829a4aSRandall Stewart 	uint32_t seq;
3671f8829a4aSRandall Stewart 
3672f8829a4aSRandall Stewart 	seq = ntohl(req->request_seq);
3673f8829a4aSRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3674ce228dabSMichael Tuexen 		asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
36759b2a35b3SMichael Tuexen 		if ((asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ) == 0) {
3676f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3677ce228dabSMichael Tuexen 		} else {
3678f8829a4aSRandall Stewart 			fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3679f8829a4aSRandall Stewart 			fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3680f8829a4aSRandall Stewart 			fwdtsn.ch.chunk_flags = 0;
3681f8829a4aSRandall Stewart 			fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3682671d309cSRandall Stewart 			sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3683f8829a4aSRandall Stewart 			if (abort_flag) {
3684f8829a4aSRandall Stewart 				return (1);
3685f8829a4aSRandall Stewart 			}
3686f3ebe64cSMichael Tuexen 			asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3687b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3688b3f1ea41SRandall Stewart 				sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3689b3f1ea41SRandall Stewart 			}
3690f3ebe64cSMichael Tuexen 			asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3691f3ebe64cSMichael Tuexen 			asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3692f3ebe64cSMichael Tuexen 			memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3693f3ebe64cSMichael Tuexen 			asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3694f3ebe64cSMichael Tuexen 			memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3695f3ebe64cSMichael Tuexen 			atomic_add_int(&asoc->sending_seq, 1);
3696f8829a4aSRandall Stewart 			/* save off historical data for retrans */
3697f3ebe64cSMichael Tuexen 			asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3698f3ebe64cSMichael Tuexen 			asoc->last_sending_seq[0] = asoc->sending_seq;
3699f3ebe64cSMichael Tuexen 			asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3700f3ebe64cSMichael Tuexen 			asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3701f8829a4aSRandall Stewart 			sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3702f8829a4aSRandall Stewart 			sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3703f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
37043ac76647SMichael Tuexen 			sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_TSN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3705ce228dabSMichael Tuexen 		}
3706ce228dabSMichael Tuexen 		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3707ce228dabSMichael Tuexen 		    asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3708f8829a4aSRandall Stewart 		asoc->str_reset_seq_in++;
3709f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 1 == seq) {
3710f8829a4aSRandall Stewart 		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3711f3ebe64cSMichael Tuexen 		    asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3712f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 2 == seq) {
3713f8829a4aSRandall Stewart 		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3714f3ebe64cSMichael Tuexen 		    asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3715f8829a4aSRandall Stewart 	} else {
3716f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3717f8829a4aSRandall Stewart 	}
3718f8829a4aSRandall Stewart 	return (0);
3719f8829a4aSRandall Stewart }
3720f8829a4aSRandall Stewart 
3721f8829a4aSRandall Stewart static void
sctp_handle_str_reset_request_out(struct sctp_tcb * stcb,struct sctp_tmit_chunk * chk,struct sctp_stream_reset_out_request * req,int trunc)3722f8829a4aSRandall Stewart sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3723f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk,
3724671d309cSRandall Stewart     struct sctp_stream_reset_out_request *req, int trunc)
3725f8829a4aSRandall Stewart {
3726f8829a4aSRandall Stewart 	uint32_t seq, tsn;
3727f8829a4aSRandall Stewart 	int number_entries, len;
3728f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3729f8829a4aSRandall Stewart 
3730f8829a4aSRandall Stewart 	seq = ntohl(req->request_seq);
3731f8829a4aSRandall Stewart 
3732f8829a4aSRandall Stewart 	/* now if its not a duplicate we process it */
3733f8829a4aSRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3734f8829a4aSRandall Stewart 		len = ntohs(req->ph.param_length);
3735f8829a4aSRandall Stewart 		number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3736f8829a4aSRandall Stewart 		/*
3737f8829a4aSRandall Stewart 		 * the sender is resetting, handle the list issue.. we must
3738f8829a4aSRandall Stewart 		 * a) verify if we can do the reset, if so no problem b) If
3739f8829a4aSRandall Stewart 		 * we can't do the reset we must copy the request. c) queue
3740f8829a4aSRandall Stewart 		 * it, and setup the data in processor to trigger it off
3741f8829a4aSRandall Stewart 		 * when needed and dequeue all the queued data.
3742f8829a4aSRandall Stewart 		 */
3743f8829a4aSRandall Stewart 		tsn = ntohl(req->send_reset_at_tsn);
3744f8829a4aSRandall Stewart 
3745f8829a4aSRandall Stewart 		/* move the reset action back one */
3746f8829a4aSRandall Stewart 		asoc->last_reset_action[1] = asoc->last_reset_action[0];
37479b2a35b3SMichael Tuexen 		if ((asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ) == 0) {
3748f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3749f3ebe64cSMichael Tuexen 		} else if (trunc) {
3750f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
375120b07a4dSMichael Tuexen 		} else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3752f8829a4aSRandall Stewart 			/* we can do it now */
3753f8829a4aSRandall Stewart 			sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3754f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3755f8829a4aSRandall Stewart 		} else {
3756f8829a4aSRandall Stewart 			/*
3757f8829a4aSRandall Stewart 			 * we must queue it up and thus wait for the TSN's
3758f8829a4aSRandall Stewart 			 * to arrive that are at or before tsn
3759f8829a4aSRandall Stewart 			 */
3760f8829a4aSRandall Stewart 			struct sctp_stream_reset_list *liste;
3761f8829a4aSRandall Stewart 			int siz;
3762f8829a4aSRandall Stewart 
3763f8829a4aSRandall Stewart 			siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3764f8829a4aSRandall Stewart 			SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3765207304d4SRandall Stewart 			    siz, SCTP_M_STRESET);
3766f8829a4aSRandall Stewart 			if (liste == NULL) {
3767f8829a4aSRandall Stewart 				/* gak out of memory */
3768f3ebe64cSMichael Tuexen 				asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3769f3ebe64cSMichael Tuexen 				sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3770f8829a4aSRandall Stewart 				return;
3771f8829a4aSRandall Stewart 			}
37727cca1775SRandall Stewart 			liste->seq = seq;
3773f8829a4aSRandall Stewart 			liste->tsn = tsn;
3774f8829a4aSRandall Stewart 			liste->number_entries = number_entries;
3775a169d6ecSMichael Tuexen 			memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t));
3776f8829a4aSRandall Stewart 			TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
37777cca1775SRandall Stewart 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_IN_PROGRESS;
3778f8829a4aSRandall Stewart 		}
3779c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3780f8829a4aSRandall Stewart 		asoc->str_reset_seq_in++;
3781f8829a4aSRandall Stewart 	} else if ((asoc->str_reset_seq_in - 1) == seq) {
3782f8829a4aSRandall Stewart 		/*
3783f8829a4aSRandall Stewart 		 * one seq back, just echo back last action since my
3784f8829a4aSRandall Stewart 		 * response was lost.
3785f8829a4aSRandall Stewart 		 */
3786f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3787f8829a4aSRandall Stewart 	} else if ((asoc->str_reset_seq_in - 2) == seq) {
3788f8829a4aSRandall Stewart 		/*
3789f8829a4aSRandall Stewart 		 * two seq back, just echo back last action since my
3790f8829a4aSRandall Stewart 		 * response was lost.
3791f8829a4aSRandall Stewart 		 */
3792f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3793f8829a4aSRandall Stewart 	} else {
3794f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3795f8829a4aSRandall Stewart 	}
3796f8829a4aSRandall Stewart }
3797f8829a4aSRandall Stewart 
3798ea44232bSRandall Stewart static void
sctp_handle_str_reset_add_strm(struct sctp_tcb * stcb,struct sctp_tmit_chunk * chk,struct sctp_stream_reset_add_strm * str_add)3799ea44232bSRandall Stewart sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3800ea44232bSRandall Stewart     struct sctp_stream_reset_add_strm *str_add)
3801ea44232bSRandall Stewart {
3802ea44232bSRandall Stewart 	/*
3803ea44232bSRandall Stewart 	 * Peer is requesting to add more streams. If its within our
3804ea44232bSRandall Stewart 	 * max-streams we will allow it.
3805ea44232bSRandall Stewart 	 */
3806c4e848b7SRandall Stewart 	uint32_t num_stream, i;
3807ea44232bSRandall Stewart 	uint32_t seq;
38088aae9493SRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
38094a9ef3f8SMichael Tuexen 	struct sctp_queued_to_read *ctl, *nctl;
3810ea44232bSRandall Stewart 
3811ea44232bSRandall Stewart 	/* Get the number. */
3812ea44232bSRandall Stewart 	seq = ntohl(str_add->request_seq);
3813ea44232bSRandall Stewart 	num_stream = ntohs(str_add->number_of_streams);
3814ea44232bSRandall Stewart 	/* Now what would be the new total? */
38158aae9493SRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3816ea44232bSRandall Stewart 		num_stream += stcb->asoc.streamincnt;
3817f3ebe64cSMichael Tuexen 		stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
38189b2a35b3SMichael Tuexen 		if ((asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ) == 0) {
3819f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3820f3ebe64cSMichael Tuexen 		} else if ((num_stream > stcb->asoc.max_inbound_streams) ||
3821c4e848b7SRandall Stewart 		    (num_stream > 0xffff)) {
3822ea44232bSRandall Stewart 			/* We must reject it they ask for to many */
3823ea44232bSRandall Stewart 	denied:
3824f3ebe64cSMichael Tuexen 			stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3825ea44232bSRandall Stewart 		} else {
3826ea44232bSRandall Stewart 			/* Ok, we can do that :-) */
3827ea44232bSRandall Stewart 			struct sctp_stream_in *oldstrm;
3828ea44232bSRandall Stewart 
3829ea44232bSRandall Stewart 			/* save off the old */
3830ea44232bSRandall Stewart 			oldstrm = stcb->asoc.strmin;
3831ea44232bSRandall Stewart 			SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
3832ea44232bSRandall Stewart 			    (num_stream * sizeof(struct sctp_stream_in)),
3833ea44232bSRandall Stewart 			    SCTP_M_STRMI);
3834ea44232bSRandall Stewart 			if (stcb->asoc.strmin == NULL) {
3835ea44232bSRandall Stewart 				stcb->asoc.strmin = oldstrm;
3836ea44232bSRandall Stewart 				goto denied;
3837ea44232bSRandall Stewart 			}
3838ea44232bSRandall Stewart 			/* copy off the old data */
38398aae9493SRandall Stewart 			for (i = 0; i < stcb->asoc.streamincnt; i++) {
38408aae9493SRandall Stewart 				TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
384144249214SRandall Stewart 				TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
384249656eefSMichael Tuexen 				stcb->asoc.strmin[i].sid = i;
384349656eefSMichael Tuexen 				stcb->asoc.strmin[i].last_mid_delivered = oldstrm[i].last_mid_delivered;
38448aae9493SRandall Stewart 				stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
384544249214SRandall Stewart 				stcb->asoc.strmin[i].pd_api_started = oldstrm[i].pd_api_started;
38468aae9493SRandall Stewart 				/* now anything on those queues? */
384744249214SRandall Stewart 				TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next_instrm, nctl) {
384844249214SRandall Stewart 					TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next_instrm);
384944249214SRandall Stewart 					TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next_instrm);
385044249214SRandall Stewart 				}
385144249214SRandall Stewart 				TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].uno_inqueue, next_instrm, nctl) {
385244249214SRandall Stewart 					TAILQ_REMOVE(&oldstrm[i].uno_inqueue, ctl, next_instrm);
385344249214SRandall Stewart 					TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].uno_inqueue, ctl, next_instrm);
38548aae9493SRandall Stewart 				}
38558aae9493SRandall Stewart 			}
3856ea44232bSRandall Stewart 			/* Init the new streams */
3857ea44232bSRandall Stewart 			for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
3858ea44232bSRandall Stewart 				TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
385944249214SRandall Stewart 				TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
386049656eefSMichael Tuexen 				stcb->asoc.strmin[i].sid = i;
386149656eefSMichael Tuexen 				stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
386244249214SRandall Stewart 				stcb->asoc.strmin[i].pd_api_started = 0;
3863ea44232bSRandall Stewart 				stcb->asoc.strmin[i].delivery_started = 0;
3864ea44232bSRandall Stewart 			}
3865ea44232bSRandall Stewart 			SCTP_FREE(oldstrm, SCTP_M_STRMI);
3866ea44232bSRandall Stewart 			/* update the size */
3867ea44232bSRandall Stewart 			stcb->asoc.streamincnt = num_stream;
3868f3ebe64cSMichael Tuexen 			stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
38693ac76647SMichael Tuexen 			sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_ADD, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3870ea44232bSRandall Stewart 		}
3871c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3872c4e848b7SRandall Stewart 		asoc->str_reset_seq_in++;
38738aae9493SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 1) == seq) {
38748aae9493SRandall Stewart 		/*
38758aae9493SRandall Stewart 		 * one seq back, just echo back last action since my
38768aae9493SRandall Stewart 		 * response was lost.
38778aae9493SRandall Stewart 		 */
38788aae9493SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
38798aae9493SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 2) == seq) {
38808aae9493SRandall Stewart 		/*
38818aae9493SRandall Stewart 		 * two seq back, just echo back last action since my
38828aae9493SRandall Stewart 		 * response was lost.
38838aae9493SRandall Stewart 		 */
38848aae9493SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
38858aae9493SRandall Stewart 	} else {
3886f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
38878aae9493SRandall Stewart 	}
3888ea44232bSRandall Stewart }
3889ea44232bSRandall Stewart 
3890c4e848b7SRandall Stewart static void
sctp_handle_str_reset_add_out_strm(struct sctp_tcb * stcb,struct sctp_tmit_chunk * chk,struct sctp_stream_reset_add_strm * str_add)3891c4e848b7SRandall Stewart sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3892c4e848b7SRandall Stewart     struct sctp_stream_reset_add_strm *str_add)
3893c4e848b7SRandall Stewart {
3894c4e848b7SRandall Stewart 	/*
3895c4e848b7SRandall Stewart 	 * Peer is requesting to add more streams. If its within our
3896c4e848b7SRandall Stewart 	 * max-streams we will allow it.
3897c4e848b7SRandall Stewart 	 */
3898c4e848b7SRandall Stewart 	uint16_t num_stream;
3899c4e848b7SRandall Stewart 	uint32_t seq;
3900c4e848b7SRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3901c4e848b7SRandall Stewart 
3902c4e848b7SRandall Stewart 	/* Get the number. */
3903c4e848b7SRandall Stewart 	seq = ntohl(str_add->request_seq);
3904c4e848b7SRandall Stewart 	num_stream = ntohs(str_add->number_of_streams);
3905c4e848b7SRandall Stewart 	/* Now what would be the new total? */
3906c4e848b7SRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3907c4e848b7SRandall Stewart 		stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
39089b2a35b3SMichael Tuexen 		if ((asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ) == 0) {
3909f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3910f3ebe64cSMichael Tuexen 		} else if (stcb->asoc.stream_reset_outstanding) {
3911f3ebe64cSMichael Tuexen 			/* We must reject it we have something pending */
3912f3ebe64cSMichael Tuexen 			stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3913c4e848b7SRandall Stewart 		} else {
3914c4e848b7SRandall Stewart 			/* Ok, we can do that :-) */
3915c4e848b7SRandall Stewart 			int mychk;
3916c4e848b7SRandall Stewart 
3917c4e848b7SRandall Stewart 			mychk = stcb->asoc.streamoutcnt;
3918c4e848b7SRandall Stewart 			mychk += num_stream;
3919c4e848b7SRandall Stewart 			if (mychk < 0x10000) {
3920f3ebe64cSMichael Tuexen 				stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
39217cca1775SRandall Stewart 				if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 1, num_stream, 0, 1)) {
3922f3ebe64cSMichael Tuexen 					stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3923c4e848b7SRandall Stewart 				}
3924c4e848b7SRandall Stewart 			} else {
3925f3ebe64cSMichael Tuexen 				stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3926c4e848b7SRandall Stewart 			}
3927c4e848b7SRandall Stewart 		}
3928c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
3929c4e848b7SRandall Stewart 		asoc->str_reset_seq_in++;
3930c4e848b7SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 1) == seq) {
3931c4e848b7SRandall Stewart 		/*
3932c4e848b7SRandall Stewart 		 * one seq back, just echo back last action since my
3933c4e848b7SRandall Stewart 		 * response was lost.
3934c4e848b7SRandall Stewart 		 */
3935c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3936c4e848b7SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 2) == seq) {
3937c4e848b7SRandall Stewart 		/*
3938c4e848b7SRandall Stewart 		 * two seq back, just echo back last action since my
3939c4e848b7SRandall Stewart 		 * response was lost.
3940c4e848b7SRandall Stewart 		 */
3941c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3942c4e848b7SRandall Stewart 	} else {
3943f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3944c4e848b7SRandall Stewart 	}
3945c4e848b7SRandall Stewart }
3946c4e848b7SRandall Stewart 
3947671d309cSRandall Stewart #ifdef __GNUC__
3948671d309cSRandall Stewart __attribute__((noinline))
3949671d309cSRandall Stewart #endif
3950f8829a4aSRandall Stewart static int
sctp_handle_stream_reset(struct sctp_tcb * stcb,struct mbuf * m,int offset,struct sctp_chunkhdr * ch_req)3951671d309cSRandall Stewart sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3952a169d6ecSMichael Tuexen     struct sctp_chunkhdr *ch_req)
3953f8829a4aSRandall Stewart {
39546a58f0e9SXin LI 	uint16_t remaining_length, param_len, ptype;
3955671d309cSRandall Stewart 	struct sctp_paramhdr pstore;
3956671d309cSRandall Stewart 	uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
3957c4e848b7SRandall Stewart 	uint32_t seq = 0;
3958f8829a4aSRandall Stewart 	int num_req = 0;
3959671d309cSRandall Stewart 	int trunc = 0;
3960f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
3961f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
3962f8829a4aSRandall Stewart 	struct sctp_paramhdr *ph;
3963f42a358aSRandall Stewart 	int ret_code = 0;
3964f42a358aSRandall Stewart 	int num_param = 0;
3965f8829a4aSRandall Stewart 
3966f8829a4aSRandall Stewart 	/* now it may be a reset or a reset-response */
39676a58f0e9SXin LI 	remaining_length = ntohs(ch_req->chunk_length) - sizeof(struct sctp_chunkhdr);
3968f8829a4aSRandall Stewart 
3969f8829a4aSRandall Stewart 	/* setup for adding the response */
3970f8829a4aSRandall Stewart 	sctp_alloc_a_chunk(stcb, chk);
3971f8829a4aSRandall Stewart 	if (chk == NULL) {
3972f8829a4aSRandall Stewart 		return (ret_code);
3973f8829a4aSRandall Stewart 	}
3974e03159eaSMichael Tuexen 	chk->copy_by_ref = 0;
3975f8829a4aSRandall Stewart 	chk->rec.chunk_id.id = SCTP_STREAM_RESET;
3976d06c82f1SRandall Stewart 	chk->rec.chunk_id.can_take_data = 0;
3977e03159eaSMichael Tuexen 	chk->flags = 0;
3978f8829a4aSRandall Stewart 	chk->asoc = &stcb->asoc;
3979f8829a4aSRandall Stewart 	chk->no_fr_allowed = 0;
3980f8829a4aSRandall Stewart 	chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
398144b7479bSRandall Stewart 	chk->book_size_scale = 0;
3982eb1b1807SGleb Smirnoff 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
3983f8829a4aSRandall Stewart 	if (chk->data == NULL) {
3984f8829a4aSRandall Stewart strres_nochunk:
3985f8829a4aSRandall Stewart 		if (chk->data) {
3986f8829a4aSRandall Stewart 			sctp_m_freem(chk->data);
3987f8829a4aSRandall Stewart 			chk->data = NULL;
3988f8829a4aSRandall Stewart 		}
3989689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3990f8829a4aSRandall Stewart 		return (ret_code);
3991f8829a4aSRandall Stewart 	}
3992139bc87fSRandall Stewart 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
3993f8829a4aSRandall Stewart 
3994f8829a4aSRandall Stewart 	/* setup chunk parameters */
3995f8829a4aSRandall Stewart 	chk->sent = SCTP_DATAGRAM_UNSENT;
3996f8829a4aSRandall Stewart 	chk->snd_count = 0;
3997ca85e948SMichael Tuexen 	chk->whoTo = NULL;
3998f8829a4aSRandall Stewart 
3999f8829a4aSRandall Stewart 	ch = mtod(chk->data, struct sctp_chunkhdr *);
4000f8829a4aSRandall Stewart 	ch->chunk_type = SCTP_STREAM_RESET;
4001f8829a4aSRandall Stewart 	ch->chunk_flags = 0;
4002f8829a4aSRandall Stewart 	ch->chunk_length = htons(chk->send_size);
4003139bc87fSRandall Stewart 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4004671d309cSRandall Stewart 	offset += sizeof(struct sctp_chunkhdr);
40056a58f0e9SXin LI 	while (remaining_length >= sizeof(struct sctp_paramhdr)) {
4006671d309cSRandall Stewart 		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *)&pstore);
40076a58f0e9SXin LI 		if (ph == NULL) {
40086a58f0e9SXin LI 			/* TSNH */
4009f8829a4aSRandall Stewart 			break;
4010f8829a4aSRandall Stewart 		}
40116a58f0e9SXin LI 		param_len = ntohs(ph->param_length);
40126a58f0e9SXin LI 		if ((param_len > remaining_length) ||
40136a58f0e9SXin LI 		    (param_len < (sizeof(struct sctp_paramhdr) + sizeof(uint32_t)))) {
40146a58f0e9SXin LI 			/* bad parameter length */
40156a58f0e9SXin LI 			break;
40166a58f0e9SXin LI 		}
40176a58f0e9SXin LI 		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, sizeof(cstore)),
4018671d309cSRandall Stewart 		    (uint8_t *)&cstore);
40196a58f0e9SXin LI 		if (ph == NULL) {
40206a58f0e9SXin LI 			/* TSNH */
40216a58f0e9SXin LI 			break;
40226a58f0e9SXin LI 		}
4023f8829a4aSRandall Stewart 		ptype = ntohs(ph->param_type);
4024f8829a4aSRandall Stewart 		num_param++;
40256a58f0e9SXin LI 		if (param_len > sizeof(cstore)) {
4026671d309cSRandall Stewart 			trunc = 1;
4027671d309cSRandall Stewart 		} else {
4028671d309cSRandall Stewart 			trunc = 0;
4029671d309cSRandall Stewart 		}
4030f8829a4aSRandall Stewart 		if (num_param > SCTP_MAX_RESET_PARAMS) {
4031f8829a4aSRandall Stewart 			/* hit the max of parameters already sorry.. */
4032f8829a4aSRandall Stewart 			break;
4033f8829a4aSRandall Stewart 		}
4034f8829a4aSRandall Stewart 		if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4035f8829a4aSRandall Stewart 			struct sctp_stream_reset_out_request *req_out;
4036f8829a4aSRandall Stewart 
40376a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_out_request)) {
40386a58f0e9SXin LI 				break;
40396a58f0e9SXin LI 			}
4040f8829a4aSRandall Stewart 			req_out = (struct sctp_stream_reset_out_request *)ph;
4041f8829a4aSRandall Stewart 			num_req++;
4042f8829a4aSRandall Stewart 			if (stcb->asoc.stream_reset_outstanding) {
4043f8829a4aSRandall Stewart 				seq = ntohl(req_out->response_seq);
4044f8829a4aSRandall Stewart 				if (seq == stcb->asoc.str_reset_seq_out) {
4045f8829a4aSRandall Stewart 					/* implicit ack */
4046f3ebe64cSMichael Tuexen 					(void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4047f8829a4aSRandall Stewart 				}
4048f8829a4aSRandall Stewart 			}
4049671d309cSRandall Stewart 			sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4050c4e848b7SRandall Stewart 		} else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4051ea44232bSRandall Stewart 			struct sctp_stream_reset_add_strm *str_add;
4052ea44232bSRandall Stewart 
40536a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
40546a58f0e9SXin LI 				break;
40556a58f0e9SXin LI 			}
4056ea44232bSRandall Stewart 			str_add = (struct sctp_stream_reset_add_strm *)ph;
4057ea44232bSRandall Stewart 			num_req++;
4058ea44232bSRandall Stewart 			sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4059c4e848b7SRandall Stewart 		} else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4060c4e848b7SRandall Stewart 			struct sctp_stream_reset_add_strm *str_add;
4061c4e848b7SRandall Stewart 
40626a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
40636a58f0e9SXin LI 				break;
40646a58f0e9SXin LI 			}
4065c4e848b7SRandall Stewart 			str_add = (struct sctp_stream_reset_add_strm *)ph;
4066c4e848b7SRandall Stewart 			num_req++;
4067c4e848b7SRandall Stewart 			sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4068f8829a4aSRandall Stewart 		} else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4069f8829a4aSRandall Stewart 			struct sctp_stream_reset_in_request *req_in;
4070f8829a4aSRandall Stewart 
4071f8829a4aSRandall Stewart 			num_req++;
4072f8829a4aSRandall Stewart 			req_in = (struct sctp_stream_reset_in_request *)ph;
4073671d309cSRandall Stewart 			sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4074f8829a4aSRandall Stewart 		} else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4075f8829a4aSRandall Stewart 			struct sctp_stream_reset_tsn_request *req_tsn;
4076f8829a4aSRandall Stewart 
4077f8829a4aSRandall Stewart 			num_req++;
4078f8829a4aSRandall Stewart 			req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4079f8829a4aSRandall Stewart 			if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4080f8829a4aSRandall Stewart 				ret_code = 1;
4081f8829a4aSRandall Stewart 				goto strres_nochunk;
4082f8829a4aSRandall Stewart 			}
4083f8829a4aSRandall Stewart 			/* no more */
4084f8829a4aSRandall Stewart 			break;
4085f8829a4aSRandall Stewart 		} else if (ptype == SCTP_STR_RESET_RESPONSE) {
4086f8829a4aSRandall Stewart 			struct sctp_stream_reset_response *resp;
4087f8829a4aSRandall Stewart 			uint32_t result;
4088f8829a4aSRandall Stewart 
40896a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_response)) {
40906a58f0e9SXin LI 				break;
40916a58f0e9SXin LI 			}
4092f8829a4aSRandall Stewart 			resp = (struct sctp_stream_reset_response *)ph;
4093f8829a4aSRandall Stewart 			seq = ntohl(resp->response_seq);
4094f8829a4aSRandall Stewart 			result = ntohl(resp->result);
4095f8829a4aSRandall Stewart 			if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4096f8829a4aSRandall Stewart 				ret_code = 1;
4097f8829a4aSRandall Stewart 				goto strres_nochunk;
4098f8829a4aSRandall Stewart 			}
4099f8829a4aSRandall Stewart 		} else {
4100f8829a4aSRandall Stewart 			break;
4101f8829a4aSRandall Stewart 		}
4102671d309cSRandall Stewart 		offset += SCTP_SIZE32(param_len);
41036a58f0e9SXin LI 		if (remaining_length >= SCTP_SIZE32(param_len)) {
41046a58f0e9SXin LI 			remaining_length -= SCTP_SIZE32(param_len);
41056a58f0e9SXin LI 		} else {
41066a58f0e9SXin LI 			remaining_length = 0;
41076a58f0e9SXin LI 		}
4108f8829a4aSRandall Stewart 	}
4109f8829a4aSRandall Stewart 	if (num_req == 0) {
4110f8829a4aSRandall Stewart 		/* we have no response free the stuff */
4111f8829a4aSRandall Stewart 		goto strres_nochunk;
4112f8829a4aSRandall Stewart 	}
4113f8829a4aSRandall Stewart 	/* ok we have a chunk to link in */
4114f8829a4aSRandall Stewart 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4115f8829a4aSRandall Stewart 	    chk,
4116f8829a4aSRandall Stewart 	    sctp_next);
4117f8829a4aSRandall Stewart 	stcb->asoc.ctrl_queue_cnt++;
4118f8829a4aSRandall Stewart 	return (ret_code);
4119f8829a4aSRandall Stewart }
4120f8829a4aSRandall Stewart 
4121f8829a4aSRandall Stewart /*
4122f8829a4aSRandall Stewart  * Handle a router or endpoints report of a packet loss, there are two ways
4123f8829a4aSRandall Stewart  * to handle this, either we get the whole packet and must disect it
4124f8829a4aSRandall Stewart  * ourselves (possibly with truncation and or corruption) or it is a summary
4125e7e65008SMichael Tuexen  * from a middle box that did the disecting for us.
4126f8829a4aSRandall Stewart  */
4127f8829a4aSRandall Stewart static void
sctp_handle_packet_dropped(struct sctp_pktdrop_chunk * cp,struct sctp_tcb * stcb,struct sctp_nets * net,uint32_t limit)4128f8829a4aSRandall Stewart sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4129458303daSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4130f8829a4aSRandall Stewart {
4131f8829a4aSRandall Stewart 	struct sctp_chunk_desc desc;
413232df1c9eSMichael Tuexen 	struct sctp_chunkhdr *chk_hdr;
413332df1c9eSMichael Tuexen 	struct sctp_data_chunk *data_chunk;
413432df1c9eSMichael Tuexen 	struct sctp_idata_chunk *idata_chunk;
413532df1c9eSMichael Tuexen 	uint32_t bottle_bw, on_queue;
413632df1c9eSMichael Tuexen 	uint32_t offset, chk_len;
413732df1c9eSMichael Tuexen 	uint16_t pktdrp_len;
413832df1c9eSMichael Tuexen 	uint8_t pktdrp_flags;
4139f8829a4aSRandall Stewart 
414032df1c9eSMichael Tuexen 	KASSERT(sizeof(struct sctp_pktdrop_chunk) <= limit,
414132df1c9eSMichael Tuexen 	    ("PKTDROP chunk too small"));
414232df1c9eSMichael Tuexen 	pktdrp_flags = cp->ch.chunk_flags;
414332df1c9eSMichael Tuexen 	pktdrp_len = ntohs(cp->ch.chunk_length);
414432df1c9eSMichael Tuexen 	KASSERT(limit <= pktdrp_len, ("Inconsistent limit"));
414532df1c9eSMichael Tuexen 	if (pktdrp_flags & SCTP_PACKET_TRUNCATED) {
41466176f9d6SMichael Tuexen 		if (ntohs(cp->trunc_len) <= pktdrp_len - sizeof(struct sctp_pktdrop_chunk)) {
414732df1c9eSMichael Tuexen 			/* The peer plays games with us. */
414832df1c9eSMichael Tuexen 			return;
414932df1c9eSMichael Tuexen 		}
415032df1c9eSMichael Tuexen 	}
415132df1c9eSMichael Tuexen 	limit -= sizeof(struct sctp_pktdrop_chunk);
415232df1c9eSMichael Tuexen 	offset = 0;
415332df1c9eSMichael Tuexen 	if (offset == limit) {
415432df1c9eSMichael Tuexen 		if (pktdrp_flags & SCTP_FROM_MIDDLE_BOX) {
4155f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_pdrpbwrpt);
415632df1c9eSMichael Tuexen 		}
415732df1c9eSMichael Tuexen 	} else if (offset + sizeof(struct sctphdr) > limit) {
415832df1c9eSMichael Tuexen 		/* Only a partial SCTP common header. */
415932df1c9eSMichael Tuexen 		SCTP_STAT_INCR(sctps_pdrpcrupt);
416032df1c9eSMichael Tuexen 		offset = limit;
4161f8829a4aSRandall Stewart 	} else {
416232df1c9eSMichael Tuexen 		/* XXX: Check embedded SCTP common header. */
416332df1c9eSMichael Tuexen 		offset += sizeof(struct sctphdr);
4164f8829a4aSRandall Stewart 	}
416532df1c9eSMichael Tuexen 	/* Now parse through the chunks themselves. */
416632df1c9eSMichael Tuexen 	while (offset < limit) {
416732df1c9eSMichael Tuexen 		if (offset + sizeof(struct sctp_chunkhdr) > limit) {
416832df1c9eSMichael Tuexen 			SCTP_STAT_INCR(sctps_pdrpcrupt);
416932df1c9eSMichael Tuexen 			break;
4170458303daSRandall Stewart 		}
417132df1c9eSMichael Tuexen 		chk_hdr = (struct sctp_chunkhdr *)(cp->data + offset);
417232df1c9eSMichael Tuexen 		desc.chunk_type = chk_hdr->chunk_type;
4173f8829a4aSRandall Stewart 		/* get amount we need to move */
417432df1c9eSMichael Tuexen 		chk_len = (uint32_t)ntohs(chk_hdr->chunk_length);
417532df1c9eSMichael Tuexen 		if (chk_len < sizeof(struct sctp_chunkhdr)) {
417632df1c9eSMichael Tuexen 			/* Someone is lying... */
4177f8829a4aSRandall Stewart 			break;
4178f8829a4aSRandall Stewart 		}
4179f8829a4aSRandall Stewart 		if (desc.chunk_type == SCTP_DATA) {
418032df1c9eSMichael Tuexen 			if (stcb->asoc.idata_supported) {
418132df1c9eSMichael Tuexen 				/* Some is playing games with us. */
418232df1c9eSMichael Tuexen 				break;
4183f8829a4aSRandall Stewart 			}
418432df1c9eSMichael Tuexen 			if (chk_len <= sizeof(struct sctp_data_chunk)) {
418532df1c9eSMichael Tuexen 				/* Some is playing games with us. */
418632df1c9eSMichael Tuexen 				break;
418732df1c9eSMichael Tuexen 			}
418832df1c9eSMichael Tuexen 			if (chk_len < sizeof(struct sctp_data_chunk) + SCTP_NUM_DB_TO_VERIFY) {
418932df1c9eSMichael Tuexen 				/*
419032df1c9eSMichael Tuexen 				 * Not enough data bytes available in the
419132df1c9eSMichael Tuexen 				 * chunk.
419232df1c9eSMichael Tuexen 				 */
4193f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpnedat);
419432df1c9eSMichael Tuexen 				goto next_chunk;
419532df1c9eSMichael Tuexen 			}
419632df1c9eSMichael Tuexen 			if (offset + sizeof(struct sctp_data_chunk) + SCTP_NUM_DB_TO_VERIFY > limit) {
419732df1c9eSMichael Tuexen 				/* Not enough data in buffer. */
4198f8829a4aSRandall Stewart 				break;
4199f8829a4aSRandall Stewart 			}
420032df1c9eSMichael Tuexen 			data_chunk = (struct sctp_data_chunk *)(cp->data + offset);
420132df1c9eSMichael Tuexen 			memcpy(desc.data_bytes, data_chunk + 1, SCTP_NUM_DB_TO_VERIFY);
420232df1c9eSMichael Tuexen 			desc.tsn_ifany = data_chunk->dp.tsn;
420332df1c9eSMichael Tuexen 			if (pktdrp_flags & SCTP_FROM_MIDDLE_BOX) {
420432df1c9eSMichael Tuexen 				SCTP_STAT_INCR(sctps_pdrpmbda);
420532df1c9eSMichael Tuexen 			}
420632df1c9eSMichael Tuexen 		} else if (desc.chunk_type == SCTP_IDATA) {
420732df1c9eSMichael Tuexen 			if (!stcb->asoc.idata_supported) {
420832df1c9eSMichael Tuexen 				/* Some is playing games with us. */
420932df1c9eSMichael Tuexen 				break;
421032df1c9eSMichael Tuexen 			}
421132df1c9eSMichael Tuexen 			if (chk_len <= sizeof(struct sctp_idata_chunk)) {
421232df1c9eSMichael Tuexen 				/* Some is playing games with us. */
421332df1c9eSMichael Tuexen 				break;
421432df1c9eSMichael Tuexen 			}
421532df1c9eSMichael Tuexen 			if (chk_len < sizeof(struct sctp_idata_chunk) + SCTP_NUM_DB_TO_VERIFY) {
421632df1c9eSMichael Tuexen 				/*
421732df1c9eSMichael Tuexen 				 * Not enough data bytes available in the
421832df1c9eSMichael Tuexen 				 * chunk.
421932df1c9eSMichael Tuexen 				 */
422032df1c9eSMichael Tuexen 				SCTP_STAT_INCR(sctps_pdrpnedat);
422132df1c9eSMichael Tuexen 				goto next_chunk;
422232df1c9eSMichael Tuexen 			}
422332df1c9eSMichael Tuexen 			if (offset + sizeof(struct sctp_idata_chunk) + SCTP_NUM_DB_TO_VERIFY > limit) {
422432df1c9eSMichael Tuexen 				/* Not enough data in buffer. */
422532df1c9eSMichael Tuexen 				break;
422632df1c9eSMichael Tuexen 			}
422732df1c9eSMichael Tuexen 			idata_chunk = (struct sctp_idata_chunk *)(cp->data + offset);
422832df1c9eSMichael Tuexen 			memcpy(desc.data_bytes, idata_chunk + 1, SCTP_NUM_DB_TO_VERIFY);
422932df1c9eSMichael Tuexen 			desc.tsn_ifany = idata_chunk->dp.tsn;
423032df1c9eSMichael Tuexen 			if (pktdrp_flags & SCTP_FROM_MIDDLE_BOX) {
423132df1c9eSMichael Tuexen 				SCTP_STAT_INCR(sctps_pdrpmbda);
423232df1c9eSMichael Tuexen 			}
4233f8829a4aSRandall Stewart 		} else {
423483dcc779SMichael Tuexen 			desc.tsn_ifany = htonl(0);
423583dcc779SMichael Tuexen 			memset(desc.data_bytes, 0, SCTP_NUM_DB_TO_VERIFY);
423632df1c9eSMichael Tuexen 			if (pktdrp_flags & SCTP_FROM_MIDDLE_BOX) {
4237f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpmbct);
4238f8829a4aSRandall Stewart 			}
423932df1c9eSMichael Tuexen 		}
424032df1c9eSMichael Tuexen 		if (process_chunk_drop(stcb, &desc, net, pktdrp_flags)) {
4241f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_pdrppdbrk);
4242f8829a4aSRandall Stewart 			break;
4243f8829a4aSRandall Stewart 		}
424432df1c9eSMichael Tuexen next_chunk:
424532df1c9eSMichael Tuexen 		offset += SCTP_SIZE32(chk_len);
4246f8829a4aSRandall Stewart 	}
4247f8829a4aSRandall Stewart 	/* Now update any rwnd --- possibly */
424832df1c9eSMichael Tuexen 	if ((pktdrp_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4249f8829a4aSRandall Stewart 		/* From a peer, we get a rwnd report */
4250f8829a4aSRandall Stewart 		uint32_t a_rwnd;
4251f8829a4aSRandall Stewart 
4252f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_pdrpfehos);
4253f8829a4aSRandall Stewart 
4254f8829a4aSRandall Stewart 		bottle_bw = ntohl(cp->bottle_bw);
4255f8829a4aSRandall Stewart 		on_queue = ntohl(cp->current_onq);
4256f8829a4aSRandall Stewart 		if (bottle_bw && on_queue) {
4257f8829a4aSRandall Stewart 			/* a rwnd report is in here */
4258f8829a4aSRandall Stewart 			if (bottle_bw > on_queue)
4259f8829a4aSRandall Stewart 				a_rwnd = bottle_bw - on_queue;
4260f8829a4aSRandall Stewart 			else
4261f8829a4aSRandall Stewart 				a_rwnd = 0;
4262f8829a4aSRandall Stewart 
4263f8829a4aSRandall Stewart 			if (a_rwnd == 0)
4264f8829a4aSRandall Stewart 				stcb->asoc.peers_rwnd = 0;
4265f8829a4aSRandall Stewart 			else {
4266f8829a4aSRandall Stewart 				if (a_rwnd > stcb->asoc.total_flight) {
4267f8829a4aSRandall Stewart 					stcb->asoc.peers_rwnd =
4268f8829a4aSRandall Stewart 					    a_rwnd - stcb->asoc.total_flight;
4269f8829a4aSRandall Stewart 				} else {
4270f8829a4aSRandall Stewart 					stcb->asoc.peers_rwnd = 0;
4271f8829a4aSRandall Stewart 				}
4272f8829a4aSRandall Stewart 				if (stcb->asoc.peers_rwnd <
4273f8829a4aSRandall Stewart 				    stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4274f8829a4aSRandall Stewart 					/* SWS sender side engages */
4275f8829a4aSRandall Stewart 					stcb->asoc.peers_rwnd = 0;
4276f8829a4aSRandall Stewart 				}
4277f8829a4aSRandall Stewart 			}
4278f8829a4aSRandall Stewart 		}
4279f8829a4aSRandall Stewart 	} else {
4280f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_pdrpfmbox);
4281f8829a4aSRandall Stewart 	}
4282f8829a4aSRandall Stewart 
4283f8829a4aSRandall Stewart 	/* now middle boxes in sat networks get a cwnd bump */
428432df1c9eSMichael Tuexen 	if ((pktdrp_flags & SCTP_FROM_MIDDLE_BOX) &&
4285f8829a4aSRandall Stewart 	    (stcb->asoc.sat_t3_loss_recovery == 0) &&
4286f8829a4aSRandall Stewart 	    (stcb->asoc.sat_network)) {
4287f8829a4aSRandall Stewart 		/*
4288cd0a4ff6SPedro F. Giffuni 		 * This is debatable but for sat networks it makes sense
4289f8829a4aSRandall Stewart 		 * Note if a T3 timer has went off, we will prohibit any
4290f8829a4aSRandall Stewart 		 * changes to cwnd until we exit the t3 loss recovery.
4291f8829a4aSRandall Stewart 		 */
4292b54d3a6cSRandall Stewart 		stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4293b54d3a6cSRandall Stewart 		    net, cp, &bottle_bw, &on_queue);
4294f8829a4aSRandall Stewart 	}
4295f8829a4aSRandall Stewart }
4296f8829a4aSRandall Stewart 
4297f8829a4aSRandall Stewart /*
4298f8829a4aSRandall Stewart  * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4299f8829a4aSRandall Stewart  * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4300f8829a4aSRandall Stewart  * offset: offset into the mbuf chain to first chunkhdr - length: is the
4301f8829a4aSRandall Stewart  * length of the complete packet outputs: - length: modified to remaining
4302f8829a4aSRandall Stewart  * length after control processing - netp: modified to new sctp_nets after
4303f8829a4aSRandall Stewart  * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4304f8829a4aSRandall Stewart  * bad packet,...) otherwise return the tcb for this packet
4305f8829a4aSRandall Stewart  */
43063c6f3536SRandall Stewart #ifdef __GNUC__
43073c6f3536SRandall Stewart __attribute__((noinline))
43083c6f3536SRandall Stewart #endif
4309f8829a4aSRandall Stewart static struct sctp_tcb *
sctp_process_control(struct mbuf * m,int iphlen,int * offset,int length,struct sockaddr * src,struct sockaddr * dst,struct sctphdr * sh,struct sctp_chunkhdr * ch,struct sctp_inpcb * inp,struct sctp_tcb * stcb,struct sctp_nets ** netp,int * fwd_tsn_seen,uint8_t mflowtype,uint32_t mflowid,uint16_t fibnum,uint32_t vrf_id,uint16_t port)4310f8829a4aSRandall Stewart sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4311b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
4312f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
431317205eccSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4314d089f9b9SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4315c54a18d2SRandall Stewart     uint32_t vrf_id, uint16_t port)
4316f8829a4aSRandall Stewart {
4317f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4318ff1ffd74SMichael Tuexen 	struct mbuf *op_err;
4319ff1ffd74SMichael Tuexen 	char msg[SCTP_DIAG_INFO_LEN];
4320f8829a4aSRandall Stewart 	uint32_t vtag_in;
4321f8829a4aSRandall Stewart 	int num_chunks = 0;	/* number of control chunks processed */
4322d0f6ab79SMichael Tuexen 	uint32_t chk_length, contiguous;
4323f8829a4aSRandall Stewart 	int ret;
4324bff64a4dSRandall Stewart 	int abort_no_unlock = 0;
4325899288aeSRandall Stewart 	int ecne_seen = 0;
43269de7354bSMichael Tuexen 	int abort_flag;
4327f8829a4aSRandall Stewart 
4328f8829a4aSRandall Stewart 	/*
4329f8829a4aSRandall Stewart 	 * How big should this be, and should it be alloc'd? Lets try the
4330f8829a4aSRandall Stewart 	 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4331f8829a4aSRandall Stewart 	 * until we get into jumbo grams and such..
4332f8829a4aSRandall Stewart 	 */
4333f42a358aSRandall Stewart 	uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4334f8829a4aSRandall Stewart 	int got_auth = 0;
4335f8829a4aSRandall Stewart 	uint32_t auth_offset = 0, auth_len = 0;
4336f8829a4aSRandall Stewart 	int auth_skipped = 0;
43372afb3e84SRandall Stewart 	int asconf_cnt = 0;
4338ceaad40aSRandall Stewart 
4339ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4340dd294dceSMichael Tuexen 	    iphlen, *offset, length, (void *)stcb);
4341f8829a4aSRandall Stewart 
434280a2d140SMichael Tuexen 	if (stcb) {
434380a2d140SMichael Tuexen 		SCTP_TCB_LOCK_ASSERT(stcb);
434480a2d140SMichael Tuexen 	}
4345f8829a4aSRandall Stewart 	/* validate chunk header length... */
4346f8829a4aSRandall Stewart 	if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4347d61a0ae0SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4348d61a0ae0SRandall Stewart 		    ntohs(ch->chunk_length));
43493ed8d364SMichael Tuexen 		*offset = length;
435080a2d140SMichael Tuexen 		return (stcb);
4351f8829a4aSRandall Stewart 	}
4352f8829a4aSRandall Stewart 	/*
4353f8829a4aSRandall Stewart 	 * validate the verification tag
4354f8829a4aSRandall Stewart 	 */
4355f8829a4aSRandall Stewart 	vtag_in = ntohl(sh->v_tag);
4356f8829a4aSRandall Stewart 
4357f8829a4aSRandall Stewart 	if (ch->chunk_type == SCTP_INITIATION) {
4358d61a0ae0SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4359d61a0ae0SRandall Stewart 		    ntohs(ch->chunk_length), vtag_in);
4360f8829a4aSRandall Stewart 		if (vtag_in != 0) {
4361f8829a4aSRandall Stewart 			/* protocol error- silently discard... */
4362f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_badvtag);
436380a2d140SMichael Tuexen 			if (stcb != NULL) {
436480a2d140SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
43656e55db54SRandall Stewart 			}
4366f8829a4aSRandall Stewart 			return (NULL);
4367f8829a4aSRandall Stewart 		}
4368f8829a4aSRandall Stewart 	} else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4369f8829a4aSRandall Stewart 		/*
4370f8829a4aSRandall Stewart 		 * If there is no stcb, skip the AUTH chunk and process
4371f8829a4aSRandall Stewart 		 * later after a stcb is found (to validate the lookup was
4372f8829a4aSRandall Stewart 		 * valid.
4373f8829a4aSRandall Stewart 		 */
4374f8829a4aSRandall Stewart 		if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4375b3f1ea41SRandall Stewart 		    (stcb == NULL) &&
4376c79bec9cSMichael Tuexen 		    (inp->auth_supported == 1)) {
4377f8829a4aSRandall Stewart 			/* save this chunk for later processing */
4378f8829a4aSRandall Stewart 			auth_skipped = 1;
4379f8829a4aSRandall Stewart 			auth_offset = *offset;
4380f8829a4aSRandall Stewart 			auth_len = ntohs(ch->chunk_length);
4381f8829a4aSRandall Stewart 
4382f8829a4aSRandall Stewart 			/* (temporarily) move past this chunk */
4383f8829a4aSRandall Stewart 			*offset += SCTP_SIZE32(auth_len);
4384f8829a4aSRandall Stewart 			if (*offset >= length) {
4385f8829a4aSRandall Stewart 				/* no more data left in the mbuf chain */
4386f8829a4aSRandall Stewart 				*offset = length;
4387f8829a4aSRandall Stewart 				return (NULL);
4388f8829a4aSRandall Stewart 			}
4389f8829a4aSRandall Stewart 			ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4390f8829a4aSRandall Stewart 			    sizeof(struct sctp_chunkhdr), chunk_buf);
4391f8829a4aSRandall Stewart 		}
4392ad81507eSRandall Stewart 		if (ch == NULL) {
4393ad81507eSRandall Stewart 			/* Help */
4394ad81507eSRandall Stewart 			*offset = length;
439580a2d140SMichael Tuexen 			return (stcb);
4396ad81507eSRandall Stewart 		}
4397f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4398f8829a4aSRandall Stewart 			goto process_control_chunks;
4399f8829a4aSRandall Stewart 		}
4400f8829a4aSRandall Stewart 		/*
4401f8829a4aSRandall Stewart 		 * first check if it's an ASCONF with an unknown src addr we
4402f8829a4aSRandall Stewart 		 * need to look inside to find the association
4403f8829a4aSRandall Stewart 		 */
4404f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
44052afb3e84SRandall Stewart 			struct sctp_chunkhdr *asconf_ch = ch;
44062afb3e84SRandall Stewart 			uint32_t asconf_offset = 0, asconf_len = 0;
44072afb3e84SRandall Stewart 
4408f8829a4aSRandall Stewart 			/* inp's refcount may be reduced */
4409f8829a4aSRandall Stewart 			SCTP_INP_INCR_REF(inp);
4410f8829a4aSRandall Stewart 
44112afb3e84SRandall Stewart 			asconf_offset = *offset;
44122afb3e84SRandall Stewart 			do {
44132afb3e84SRandall Stewart 				asconf_len = ntohs(asconf_ch->chunk_length);
44142afb3e84SRandall Stewart 				if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
44152afb3e84SRandall Stewart 					break;
44167215cc1bSMichael Tuexen 				stcb = sctp_findassociation_ep_asconf(m,
4417b1754ad1SMichael Tuexen 				    *offset,
4418b1754ad1SMichael Tuexen 				    dst,
4419b1754ad1SMichael Tuexen 				    sh, &inp, netp, vrf_id);
44202afb3e84SRandall Stewart 				if (stcb != NULL)
44212afb3e84SRandall Stewart 					break;
44222afb3e84SRandall Stewart 				asconf_offset += SCTP_SIZE32(asconf_len);
44232afb3e84SRandall Stewart 				asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
44242afb3e84SRandall Stewart 				    sizeof(struct sctp_chunkhdr), chunk_buf);
44252afb3e84SRandall Stewart 			} while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4426f8829a4aSRandall Stewart 			if (stcb == NULL) {
4427f8829a4aSRandall Stewart 				/*
4428f8829a4aSRandall Stewart 				 * reduce inp's refcount if not reduced in
4429f8829a4aSRandall Stewart 				 * sctp_findassociation_ep_asconf().
4430f8829a4aSRandall Stewart 				 */
4431f8829a4aSRandall Stewart 				SCTP_INP_DECR_REF(inp);
4432f8829a4aSRandall Stewart 			}
44330053ed28SMichael Tuexen 
4434f8829a4aSRandall Stewart 			/* now go back and verify any auth chunk to be sure */
4435f8829a4aSRandall Stewart 			if (auth_skipped && (stcb != NULL)) {
4436f8829a4aSRandall Stewart 				struct sctp_auth_chunk *auth;
4437f8829a4aSRandall Stewart 
44388262311cSMichael Tuexen 				if (auth_len <= SCTP_CHUNK_BUFFER_SIZE) {
44398262311cSMichael Tuexen 					auth = (struct sctp_auth_chunk *)sctp_m_getptr(m, auth_offset, auth_len, chunk_buf);
4440f8829a4aSRandall Stewart 					got_auth = 1;
4441f8829a4aSRandall Stewart 					auth_skipped = 0;
44428262311cSMichael Tuexen 				} else {
44438262311cSMichael Tuexen 					auth = NULL;
44448262311cSMichael Tuexen 				}
4445ad81507eSRandall Stewart 				if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4446f8829a4aSRandall Stewart 				    auth_offset)) {
4447f8829a4aSRandall Stewart 					/* auth HMAC failed so dump it */
4448f8829a4aSRandall Stewart 					*offset = length;
444980a2d140SMichael Tuexen 					return (stcb);
4450f8829a4aSRandall Stewart 				} else {
4451f8829a4aSRandall Stewart 					/* remaining chunks are HMAC checked */
4452f8829a4aSRandall Stewart 					stcb->asoc.authenticated = 1;
4453f8829a4aSRandall Stewart 				}
4454f8829a4aSRandall Stewart 			}
4455f8829a4aSRandall Stewart 		}
4456f8829a4aSRandall Stewart 		if (stcb == NULL) {
4457999f86d6SMichael Tuexen 			SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4458ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4459ff1ffd74SMichael Tuexen 			    msg);
4460f8829a4aSRandall Stewart 			/* no association, so it's out of the blue... */
4461ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp, op_err,
4462d089f9b9SMichael Tuexen 			    mflowtype, mflowid, inp->fibnum,
4463c54a18d2SRandall Stewart 			    vrf_id, port);
4464f8829a4aSRandall Stewart 			*offset = length;
4465f8829a4aSRandall Stewart 			return (NULL);
4466f8829a4aSRandall Stewart 		}
4467f8829a4aSRandall Stewart 		asoc = &stcb->asoc;
4468f8829a4aSRandall Stewart 		/* ABORT and SHUTDOWN can use either v_tag... */
4469f8829a4aSRandall Stewart 		if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4470f8829a4aSRandall Stewart 		    (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4471f8829a4aSRandall Stewart 		    (ch->chunk_type == SCTP_PACKET_DROPPED)) {
44725db47b3dSMichael Tuexen 			/* Take the T-bit always into account. */
44735db47b3dSMichael Tuexen 			if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) &&
44745db47b3dSMichael Tuexen 			    (vtag_in == asoc->my_vtag)) ||
44755db47b3dSMichael Tuexen 			    (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) &&
4476ff76c8c9SMichael Tuexen 			    (asoc->peer_vtag != htonl(0)) &&
4477f8829a4aSRandall Stewart 			    (vtag_in == asoc->peer_vtag))) {
4478f8829a4aSRandall Stewart 				/* this is valid */
4479f8829a4aSRandall Stewart 			} else {
4480f8829a4aSRandall Stewart 				/* drop this packet... */
4481f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_badvtag);
448280a2d140SMichael Tuexen 				if (stcb != NULL) {
448380a2d140SMichael Tuexen 					SCTP_TCB_UNLOCK(stcb);
44846e55db54SRandall Stewart 				}
4485f8829a4aSRandall Stewart 				return (NULL);
4486f8829a4aSRandall Stewart 			}
4487f8829a4aSRandall Stewart 		} else {
4488f8829a4aSRandall Stewart 			/* for all other chunks, vtag must match */
4489f8829a4aSRandall Stewart 			if (vtag_in != asoc->my_vtag) {
4490f8829a4aSRandall Stewart 				/* invalid vtag... */
4491ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INPUT3,
4492ad81507eSRandall Stewart 				    "invalid vtag: %xh, expect %xh\n",
4493ad81507eSRandall Stewart 				    vtag_in, asoc->my_vtag);
4494f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_badvtag);
449580a2d140SMichael Tuexen 				if (stcb != NULL) {
449680a2d140SMichael Tuexen 					SCTP_TCB_UNLOCK(stcb);
44976e55db54SRandall Stewart 				}
4498f8829a4aSRandall Stewart 				*offset = length;
4499f8829a4aSRandall Stewart 				return (NULL);
4500f8829a4aSRandall Stewart 			}
4501f8829a4aSRandall Stewart 		}
4502b7b84c0eSMichael Tuexen 	}			/* end if !SCTP_COOKIE_ECHO */
4503b7b84c0eSMichael Tuexen 	/*
4504b7b84c0eSMichael Tuexen 	 * process all control chunks...
4505b7b84c0eSMichael Tuexen 	 */
4506f8829a4aSRandall Stewart 	if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4507830d754dSRandall Stewart 	    (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4508f8829a4aSRandall Stewart 	    (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4509839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
4510f8829a4aSRandall Stewart 		/* implied cookie-ack.. we must have lost the ack */
4511f8829a4aSRandall Stewart 		sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4512f8829a4aSRandall Stewart 		    *netp);
4513f8829a4aSRandall Stewart 	}
45140053ed28SMichael Tuexen 
4515f8829a4aSRandall Stewart process_control_chunks:
4516f8829a4aSRandall Stewart 	while (IS_SCTP_CONTROL(ch)) {
4517f8829a4aSRandall Stewart 		/* validate chunk length */
4518f8829a4aSRandall Stewart 		chk_length = ntohs(ch->chunk_length);
4519ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4520f8829a4aSRandall Stewart 		    ch->chunk_type, chk_length);
452180fefe0aSRandall Stewart 		SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
45224c9179adSRandall Stewart 		if (chk_length < sizeof(*ch) ||
45234c9179adSRandall Stewart 		    (*offset + (int)chk_length) > length) {
4524f8829a4aSRandall Stewart 			*offset = length;
452580a2d140SMichael Tuexen 			return (stcb);
4526f8829a4aSRandall Stewart 		}
4527f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4528f8829a4aSRandall Stewart 		/*
4529d0f6ab79SMichael Tuexen 		 * INIT and INIT-ACK only gets the init ack "header" portion
4530d0f6ab79SMichael Tuexen 		 * only because we don't have to process the peer's COOKIE.
4531d0f6ab79SMichael Tuexen 		 * All others get a complete chunk.
4532f8829a4aSRandall Stewart 		 */
4533d0f6ab79SMichael Tuexen 		switch (ch->chunk_type) {
4534d0f6ab79SMichael Tuexen 		case SCTP_INITIATION:
4535d0f6ab79SMichael Tuexen 			contiguous = sizeof(struct sctp_init_chunk);
4536d0f6ab79SMichael Tuexen 			break;
4537d0f6ab79SMichael Tuexen 		case SCTP_INITIATION_ACK:
4538d0f6ab79SMichael Tuexen 			contiguous = sizeof(struct sctp_init_ack_chunk);
4539d0f6ab79SMichael Tuexen 			break;
4540d0f6ab79SMichael Tuexen 		default:
4541d0f6ab79SMichael Tuexen 			contiguous = min(chk_length, sizeof(chunk_buf));
4542d0f6ab79SMichael Tuexen 			break;
45436e55db54SRandall Stewart 		}
4544d06c82f1SRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4545d0f6ab79SMichael Tuexen 		    contiguous,
4546d06c82f1SRandall Stewart 		    chunk_buf);
4547d06c82f1SRandall Stewart 		if (ch == NULL) {
4548d06c82f1SRandall Stewart 			*offset = length;
4549c70d1ef1SMichael Tuexen 			return (stcb);
4550d06c82f1SRandall Stewart 		}
45510053ed28SMichael Tuexen 
4552f8829a4aSRandall Stewart 		num_chunks++;
4553f8829a4aSRandall Stewart 		/* Save off the last place we got a control from */
4554f8829a4aSRandall Stewart 		if (stcb != NULL) {
4555ad81507eSRandall Stewart 			if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4556f8829a4aSRandall Stewart 				/*
4557f8829a4aSRandall Stewart 				 * allow last_control to be NULL if
4558f8829a4aSRandall Stewart 				 * ASCONF... ASCONF processing will find the
4559f8829a4aSRandall Stewart 				 * right net later
4560f8829a4aSRandall Stewart 				 */
4561ad81507eSRandall Stewart 				if ((netp != NULL) && (*netp != NULL))
4562f8829a4aSRandall Stewart 					stcb->asoc.last_control_chunk_from = *netp;
4563f8829a4aSRandall Stewart 			}
4564f8829a4aSRandall Stewart 		}
4565f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
4566f8829a4aSRandall Stewart 		sctp_audit_log(0xB0, ch->chunk_type);
4567f8829a4aSRandall Stewart #endif
4568f8829a4aSRandall Stewart 
4569f8829a4aSRandall Stewart 		/* check to see if this chunk required auth, but isn't */
4570b3f1ea41SRandall Stewart 		if ((stcb != NULL) &&
4571b3f1ea41SRandall Stewart 		    sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4572f8829a4aSRandall Stewart 		    !stcb->asoc.authenticated) {
4573f8829a4aSRandall Stewart 			/* "silently" ignore */
4574f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvauthmissing);
4575f8829a4aSRandall Stewart 			goto next_chunk;
4576f8829a4aSRandall Stewart 		}
4577f8829a4aSRandall Stewart 		switch (ch->chunk_type) {
4578f8829a4aSRandall Stewart 		case SCTP_INITIATION:
4579ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4580d68fdc4dSMichael Tuexen 			/* The INIT chunk must be the only chunk. */
4581d68fdc4dSMichael Tuexen 			if ((num_chunks > 1) ||
4582ab6174d5SMichael Tuexen 			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4583c70d1ef1SMichael Tuexen 				/*
4584c70d1ef1SMichael Tuexen 				 * RFC 4960bis requires stopping the
4585c70d1ef1SMichael Tuexen 				 * processing of the packet.
4586c70d1ef1SMichael Tuexen 				 */
4587f8829a4aSRandall Stewart 				*offset = length;
4588c70d1ef1SMichael Tuexen 				return (stcb);
4589f8829a4aSRandall Stewart 			}
4590d68fdc4dSMichael Tuexen 			/* Honor our resource limit. */
4591d68fdc4dSMichael Tuexen 			if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4592ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4593eba8e643SMichael Tuexen 				sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
4594eba8e643SMichael Tuexen 				    mflowtype, mflowid, inp->fibnum,
4595f30ac432SMichael Tuexen 				    vrf_id, port);
4596f8829a4aSRandall Stewart 				*offset = length;
4597eba8e643SMichael Tuexen 				if (stcb != NULL) {
4598eba8e643SMichael Tuexen 					SCTP_TCB_UNLOCK(stcb);
4599eba8e643SMichael Tuexen 				}
4600f8829a4aSRandall Stewart 				return (NULL);
4601f8829a4aSRandall Stewart 			}
4602b1754ad1SMichael Tuexen 			sctp_handle_init(m, iphlen, *offset, src, dst, sh,
4603ad81507eSRandall Stewart 			    (struct sctp_init_chunk *)ch, inp,
4604eba8e643SMichael Tuexen 			    stcb, *netp,
4605457b4b88SMichael Tuexen 			    mflowtype, mflowid,
4606f30ac432SMichael Tuexen 			    vrf_id, port);
4607f8829a4aSRandall Stewart 			*offset = length;
4608eba8e643SMichael Tuexen 			if (stcb != NULL) {
460980a2d140SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
46106e55db54SRandall Stewart 			}
4611f8829a4aSRandall Stewart 			return (NULL);
4612f8829a4aSRandall Stewart 			break;
46139a972525SRandall Stewart 		case SCTP_PAD_CHUNK:
46149a972525SRandall Stewart 			break;
4615f8829a4aSRandall Stewart 		case SCTP_INITIATION_ACK:
4616469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT_ACK\n");
4617f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4618f8829a4aSRandall Stewart 				/* We are not interested anymore */
461980a2d140SMichael Tuexen 				if ((stcb != NULL) && (stcb->asoc.total_output_queue_size)) {
4620f8829a4aSRandall Stewart 					;
4621f8829a4aSRandall Stewart 				} else {
4622f8829a4aSRandall Stewart 					*offset = length;
462380a2d140SMichael Tuexen 					if (stcb != NULL) {
4624b7d130beSMichael Tuexen 						(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4625b7d130beSMichael Tuexen 						    SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
4626f8829a4aSRandall Stewart 					}
4627f8829a4aSRandall Stewart 					return (NULL);
4628f8829a4aSRandall Stewart 				}
4629f8829a4aSRandall Stewart 			}
4630ab6174d5SMichael Tuexen 			/* The INIT-ACK chunk must be the only chunk. */
4631f8829a4aSRandall Stewart 			if ((num_chunks > 1) ||
4632ab6174d5SMichael Tuexen 			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4633f8829a4aSRandall Stewart 				*offset = length;
463480a2d140SMichael Tuexen 				return (stcb);
4635f8829a4aSRandall Stewart 			}
4636469a65d1SMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
4637b1754ad1SMichael Tuexen 				ret = sctp_handle_init_ack(m, iphlen, *offset,
4638b1754ad1SMichael Tuexen 				    src, dst, sh,
4639f30ac432SMichael Tuexen 				    (struct sctp_init_ack_chunk *)ch,
4640f30ac432SMichael Tuexen 				    stcb, *netp,
4641f30ac432SMichael Tuexen 				    &abort_no_unlock,
4642457b4b88SMichael Tuexen 				    mflowtype, mflowid,
4643f30ac432SMichael Tuexen 				    vrf_id);
4644ad81507eSRandall Stewart 			} else {
4645ad81507eSRandall Stewart 				ret = -1;
4646ad81507eSRandall Stewart 			}
4647d68fdc4dSMichael Tuexen 			*offset = length;
4648d68fdc4dSMichael Tuexen 			if (abort_no_unlock) {
4649d68fdc4dSMichael Tuexen 				return (NULL);
4650d68fdc4dSMichael Tuexen 			}
4651f8829a4aSRandall Stewart 			/*
4652f8829a4aSRandall Stewart 			 * Special case, I must call the output routine to
4653f8829a4aSRandall Stewart 			 * get the cookie echoed
4654f8829a4aSRandall Stewart 			 */
4655d68fdc4dSMichael Tuexen 			if ((stcb != NULL) && (ret == 0)) {
4656ceaad40aSRandall Stewart 				sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4657d68fdc4dSMichael Tuexen 			}
465880a2d140SMichael Tuexen 			return (stcb);
4659f8829a4aSRandall Stewart 			break;
4660f8829a4aSRandall Stewart 		case SCTP_SELECTIVE_ACK:
4661469a65d1SMichael Tuexen 		case SCTP_NR_SELECTIVE_ACK:
4662f8829a4aSRandall Stewart 			{
4663f8829a4aSRandall Stewart 				int abort_now = 0;
4664f8829a4aSRandall Stewart 				uint32_t a_rwnd, cum_ack;
4665469a65d1SMichael Tuexen 				uint16_t num_seg, num_nr_seg, num_dup;
4666cd554309SMichael Tuexen 				uint8_t flags;
4667cd554309SMichael Tuexen 				int offset_seg, offset_dup;
4668f8829a4aSRandall Stewart 
4669469a65d1SMichael Tuexen 				SCTPDBG(SCTP_DEBUG_INPUT3, "%s\n",
4670469a65d1SMichael Tuexen 				    ch->chunk_type == SCTP_SELECTIVE_ACK ? "SCTP_SACK" : "SCTP_NR_SACK");
467120083c2eSMichael Tuexen 				SCTP_STAT_INCR(sctps_recvsacks);
4672cd554309SMichael Tuexen 				if (stcb == NULL) {
4673469a65d1SMichael Tuexen 					SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing %s chunk\n",
4674469a65d1SMichael Tuexen 					    (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK");
4675cd554309SMichael Tuexen 					break;
46766e55db54SRandall Stewart 				}
4677469a65d1SMichael Tuexen 				if (ch->chunk_type == SCTP_SELECTIVE_ACK) {
4678cd554309SMichael Tuexen 					if (chk_length < sizeof(struct sctp_sack_chunk)) {
4679cd554309SMichael Tuexen 						SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4680cd554309SMichael Tuexen 						break;
4681d06c82f1SRandall Stewart 					}
4682469a65d1SMichael Tuexen 				} else {
4683469a65d1SMichael Tuexen 					if (stcb->asoc.nrsack_supported == 0) {
4684469a65d1SMichael Tuexen 						goto unknown_chunk;
4685469a65d1SMichael Tuexen 					}
4686469a65d1SMichael Tuexen 					if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
4687469a65d1SMichael Tuexen 						SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR_SACK chunk, too small\n");
4688469a65d1SMichael Tuexen 						break;
4689469a65d1SMichael Tuexen 					}
4690469a65d1SMichael Tuexen 				}
4691839d21d6SMichael Tuexen 				if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
46922afb3e84SRandall Stewart 					/*-
46932afb3e84SRandall Stewart 					 * If we have sent a shutdown-ack, we will pay no
46942afb3e84SRandall Stewart 					 * attention to a sack sent in to us since
46952afb3e84SRandall Stewart 					 * we don't care anymore.
46962afb3e84SRandall Stewart 					 */
4697a1e13272SRandall Stewart 					break;
46982afb3e84SRandall Stewart 				}
4699cd554309SMichael Tuexen 				flags = ch->chunk_flags;
4700469a65d1SMichael Tuexen 				if (ch->chunk_type == SCTP_SELECTIVE_ACK) {
4701469a65d1SMichael Tuexen 					struct sctp_sack_chunk *sack;
4702469a65d1SMichael Tuexen 
4703469a65d1SMichael Tuexen 					sack = (struct sctp_sack_chunk *)ch;
4704f8829a4aSRandall Stewart 					cum_ack = ntohl(sack->sack.cum_tsn_ack);
4705f8829a4aSRandall Stewart 					num_seg = ntohs(sack->sack.num_gap_ack_blks);
4706469a65d1SMichael Tuexen 					num_nr_seg = 0;
4707cd554309SMichael Tuexen 					num_dup = ntohs(sack->sack.num_dup_tsns);
4708469a65d1SMichael Tuexen 					a_rwnd = ntohl(sack->sack.a_rwnd);
4709cd554309SMichael Tuexen 					if (sizeof(struct sctp_sack_chunk) +
4710cd554309SMichael Tuexen 					    num_seg * sizeof(struct sctp_gap_ack_block) +
4711cd554309SMichael Tuexen 					    num_dup * sizeof(uint32_t) != chk_length) {
4712cd554309SMichael Tuexen 						SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4713cd554309SMichael Tuexen 						break;
4714cd554309SMichael Tuexen 					}
4715cd554309SMichael Tuexen 					offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4716cd554309SMichael Tuexen 					offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4717f8829a4aSRandall Stewart 				} else {
4718830d754dSRandall Stewart 					struct sctp_nr_sack_chunk *nr_sack;
4719830d754dSRandall Stewart 
4720830d754dSRandall Stewart 					nr_sack = (struct sctp_nr_sack_chunk *)ch;
4721830d754dSRandall Stewart 					cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
4722830d754dSRandall Stewart 					num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
4723830d754dSRandall Stewart 					num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4724cd554309SMichael Tuexen 					num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
4725469a65d1SMichael Tuexen 					a_rwnd = ntohl(nr_sack->nr_sack.a_rwnd);
4726cd554309SMichael Tuexen 					if (sizeof(struct sctp_nr_sack_chunk) +
4727cd554309SMichael Tuexen 					    (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
4728cd554309SMichael Tuexen 					    num_dup * sizeof(uint32_t) != chk_length) {
4729cd554309SMichael Tuexen 						SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
4730cd554309SMichael Tuexen 						break;
4731cd554309SMichael Tuexen 					}
4732cd554309SMichael Tuexen 					offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
4733469a65d1SMichael Tuexen 					offset_dup = offset_seg + (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block);
4734469a65d1SMichael Tuexen 				}
4735469a65d1SMichael Tuexen 				SCTPDBG(SCTP_DEBUG_INPUT3, "%s process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4736469a65d1SMichael Tuexen 				    (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK",
4737cd554309SMichael Tuexen 				    cum_ack, num_seg, a_rwnd);
4738830d754dSRandall Stewart 				stcb->asoc.seen_a_sack_this_pkt = 1;
4739830d754dSRandall Stewart 				if ((stcb->asoc.pr_sctp_cnt == 0) &&
4740cd554309SMichael Tuexen 				    (num_seg == 0) && (num_nr_seg == 0) &&
474120b07a4dSMichael Tuexen 				    SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4742830d754dSRandall Stewart 				    (stcb->asoc.saw_sack_with_frags == 0) &&
4743d9c5cfeaSMichael Tuexen 				    (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4744cd554309SMichael Tuexen 				    (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
4745830d754dSRandall Stewart 					/*
4746830d754dSRandall Stewart 					 * We have a SIMPLE sack having no
4747830d754dSRandall Stewart 					 * prior segments and data on sent
4748cd554309SMichael Tuexen 					 * queue to be acked. Use the faster
4749cd554309SMichael Tuexen 					 * path sack processing. We also
4750cd554309SMichael Tuexen 					 * allow window update sacks with no
4751cd554309SMichael Tuexen 					 * missing segments to go this way
4752cd554309SMichael Tuexen 					 * too.
4753830d754dSRandall Stewart 					 */
4754493d8e5aSRandall Stewart 					sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
4755899288aeSRandall Stewart 					    &abort_now, ecne_seen);
4756830d754dSRandall Stewart 				} else {
4757469a65d1SMichael Tuexen 					if ((netp != NULL) && (*netp != NULL)) {
47587215cc1bSMichael Tuexen 						sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4759cd554309SMichael Tuexen 						    num_seg, num_nr_seg, num_dup, &abort_now, flags,
4760899288aeSRandall Stewart 						    cum_ack, a_rwnd, ecne_seen);
4761830d754dSRandall Stewart 					}
4762469a65d1SMichael Tuexen 				}
4763830d754dSRandall Stewart 				if (abort_now) {
4764830d754dSRandall Stewart 					/* ABORT signal from sack processing */
4765830d754dSRandall Stewart 					*offset = length;
4766830d754dSRandall Stewart 					return (NULL);
4767830d754dSRandall Stewart 				}
4768cd554309SMichael Tuexen 				if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4769cd554309SMichael Tuexen 				    TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4770cd554309SMichael Tuexen 				    (stcb->asoc.stream_queue_cnt == 0)) {
4771cd554309SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4772cd554309SMichael Tuexen 				}
4773830d754dSRandall Stewart 				break;
4774469a65d1SMichael Tuexen 			}
4775f8829a4aSRandall Stewart 		case SCTP_HEARTBEAT_REQUEST:
4776ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
4777469a65d1SMichael Tuexen 			if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
4778f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_recvheartbeat);
4779ad81507eSRandall Stewart 				sctp_send_heartbeat_ack(stcb, m, *offset,
4780ad81507eSRandall Stewart 				    chk_length, *netp);
4781ad81507eSRandall Stewart 			}
4782f8829a4aSRandall Stewart 			break;
4783f8829a4aSRandall Stewart 		case SCTP_HEARTBEAT_ACK:
4784469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT_ACK\n");
4785ad81507eSRandall Stewart 			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
4786d06c82f1SRandall Stewart 				/* Its not ours */
47879de7354bSMichael Tuexen 				break;
4788d06c82f1SRandall Stewart 			}
4789f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvheartbeatack);
4790469a65d1SMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
4791f8829a4aSRandall Stewart 				sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
4792f8829a4aSRandall Stewart 				    stcb, *netp);
4793469a65d1SMichael Tuexen 			}
4794f8829a4aSRandall Stewart 			break;
4795f8829a4aSRandall Stewart 		case SCTP_ABORT_ASSOCIATION:
4796207304d4SRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
4797dd294dceSMichael Tuexen 			    (void *)stcb);
4798f8829a4aSRandall Stewart 			*offset = length;
4799701492a5SMichael Tuexen 			if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
4800701492a5SMichael Tuexen 				if (sctp_handle_abort((struct sctp_abort_chunk *)ch, stcb, *netp)) {
4801f8829a4aSRandall Stewart 					return (NULL);
4802701492a5SMichael Tuexen 				} else {
4803701492a5SMichael Tuexen 					return (stcb);
4804701492a5SMichael Tuexen 				}
4805701492a5SMichael Tuexen 			} else {
4806701492a5SMichael Tuexen 				return (NULL);
4807701492a5SMichael Tuexen 			}
4808f8829a4aSRandall Stewart 			break;
4809f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN:
4810207304d4SRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
4811dd294dceSMichael Tuexen 			    (void *)stcb);
4812ad81507eSRandall Stewart 			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
48139de7354bSMichael Tuexen 				break;
4814ad81507eSRandall Stewart 			}
4815469a65d1SMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
48169de7354bSMichael Tuexen 				abort_flag = 0;
4817f8829a4aSRandall Stewart 				sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
4818f8829a4aSRandall Stewart 				    stcb, *netp, &abort_flag);
4819f8829a4aSRandall Stewart 				if (abort_flag) {
4820f8829a4aSRandall Stewart 					*offset = length;
4821f8829a4aSRandall Stewart 					return (NULL);
4822f8829a4aSRandall Stewart 				}
4823f8829a4aSRandall Stewart 			}
4824f8829a4aSRandall Stewart 			break;
4825f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_ACK:
4826469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_ACK, stcb %p\n", (void *)stcb);
4827d1cb8d11SMichael Tuexen 			if ((chk_length == sizeof(struct sctp_shutdown_ack_chunk)) &&
4828d1cb8d11SMichael Tuexen 			    (stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
4829f8829a4aSRandall Stewart 				sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
4830f8829a4aSRandall Stewart 				*offset = length;
4831f8829a4aSRandall Stewart 				return (NULL);
4832d1cb8d11SMichael Tuexen 			}
4833f8829a4aSRandall Stewart 			break;
4834f8829a4aSRandall Stewart 		case SCTP_OPERATION_ERROR:
4835469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP_ERR\n");
4836469a65d1SMichael Tuexen 			if ((stcb != NULL) && (netp != NULL) && (*netp != NULL) &&
48373e87bccdSMichael Tuexen 			    sctp_handle_error(ch, stcb, *netp, contiguous) < 0) {
4838f8829a4aSRandall Stewart 				*offset = length;
4839f8829a4aSRandall Stewart 				return (NULL);
4840f8829a4aSRandall Stewart 			}
4841f8829a4aSRandall Stewart 			break;
4842f8829a4aSRandall Stewart 		case SCTP_COOKIE_ECHO:
4843ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3,
4844469a65d1SMichael Tuexen 			    "SCTP_COOKIE_ECHO, stcb %p\n", (void *)stcb);
4845469a65d1SMichael Tuexen 			if ((stcb != NULL) && (stcb->asoc.total_output_queue_size > 0)) {
4846f8829a4aSRandall Stewart 				;
4847f8829a4aSRandall Stewart 			} else {
4848ad81507eSRandall Stewart 				if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4849f8829a4aSRandall Stewart 					/* We are not interested anymore */
48500c7dc840SRandall Stewart 			abend:
485180a2d140SMichael Tuexen 					if (stcb != NULL) {
485228085b2eSRandall Stewart 						SCTP_TCB_UNLOCK(stcb);
485328085b2eSRandall Stewart 					}
4854f8829a4aSRandall Stewart 					*offset = length;
4855f8829a4aSRandall Stewart 					return (NULL);
4856f8829a4aSRandall Stewart 				}
4857f8829a4aSRandall Stewart 			}
48583017b21bSMichael Tuexen 			/*-
4859f8829a4aSRandall Stewart 			 * First are we accepting? We do this again here
486088a7eb29SRandall Stewart 			 * since it is possible that a previous endpoint WAS
486188a7eb29SRandall Stewart 			 * listening responded to a INIT-ACK and then
4862f8829a4aSRandall Stewart 			 * closed. We opened and bound.. and are now no
4863f8829a4aSRandall Stewart 			 * longer listening.
4864779f106aSGleb Smirnoff 			 *
4865779f106aSGleb Smirnoff 			 * XXXGL: notes on checking listen queue length.
4866779f106aSGleb Smirnoff 			 * 1) SCTP_IS_LISTENING() doesn't necessarily mean
4867779f106aSGleb Smirnoff 			 *    SOLISTENING(), because a listening "UDP type"
4868779f106aSGleb Smirnoff 			 *    socket isn't listening in terms of the socket
4869779f106aSGleb Smirnoff 			 *    layer.  It is a normal data flow socket, that
4870779f106aSGleb Smirnoff 			 *    can fork off new connections.  Thus, we should
4871779f106aSGleb Smirnoff 			 *    look into sol_qlen only in case we are !UDP.
4872779f106aSGleb Smirnoff 			 * 2) Checking sol_qlen in general requires locking
4873779f106aSGleb Smirnoff 			 *    the socket, and this code lacks that.
4874f8829a4aSRandall Stewart 			 */
48755d08768aSMichael Tuexen 			if ((stcb == NULL) &&
48765d08768aSMichael Tuexen 			    (!SCTP_IS_LISTENING(inp) ||
48779b2a35b3SMichael Tuexen 			    (((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) == 0) &&
4878779f106aSGleb Smirnoff 			    inp->sctp_socket->sol_qlen >= inp->sctp_socket->sol_qlimit))) {
4879b201f536SRandall Stewart 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4880b3f1ea41SRandall Stewart 				    (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
4881ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4882b1754ad1SMichael Tuexen 					sctp_abort_association(inp, stcb, m, iphlen,
4883b1754ad1SMichael Tuexen 					    src, dst, sh, op_err,
4884457b4b88SMichael Tuexen 					    mflowtype, mflowid,
4885f30ac432SMichael Tuexen 					    vrf_id, port);
4886f8829a4aSRandall Stewart 				}
4887f8829a4aSRandall Stewart 				*offset = length;
4888f8829a4aSRandall Stewart 				return (NULL);
4889b201f536SRandall Stewart 			} else {
4890f8829a4aSRandall Stewart 				struct mbuf *ret_buf;
4891a5d547adSRandall Stewart 				struct sctp_inpcb *linp;
4892efd5e692SMichael Tuexen 				struct sctp_tmit_chunk *chk;
4893f8829a4aSRandall Stewart 
4894c98bf2a4SMark Johnston 				if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE |
4895c98bf2a4SMark Johnston 				    SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4896c98bf2a4SMark Johnston 					goto abend;
4897c98bf2a4SMark Johnston 				}
4898c98bf2a4SMark Johnston 
4899ad81507eSRandall Stewart 				if (stcb) {
4900a5d547adSRandall Stewart 					linp = NULL;
4901ad81507eSRandall Stewart 				} else {
4902a5d547adSRandall Stewart 					linp = inp;
4903ad81507eSRandall Stewart 				}
4904a5d547adSRandall Stewart 
4905469a65d1SMichael Tuexen 				if (linp != NULL) {
4906a5d547adSRandall Stewart 					SCTP_ASOC_CREATE_LOCK(linp);
4907ad81507eSRandall Stewart 				}
49080053ed28SMichael Tuexen 
4909469a65d1SMichael Tuexen 				if (netp != NULL) {
491080a2d140SMichael Tuexen 					struct sctp_tcb *locked_stcb;
491180a2d140SMichael Tuexen 
491280a2d140SMichael Tuexen 					locked_stcb = stcb;
4913f8829a4aSRandall Stewart 					ret_buf =
4914f8829a4aSRandall Stewart 					    sctp_handle_cookie_echo(m, iphlen,
4915b1754ad1SMichael Tuexen 					    *offset,
4916b1754ad1SMichael Tuexen 					    src, dst,
4917b1754ad1SMichael Tuexen 					    sh,
4918f8829a4aSRandall Stewart 					    (struct sctp_cookie_echo_chunk *)ch,
4919f8829a4aSRandall Stewart 					    &inp, &stcb, netp,
4920f8829a4aSRandall Stewart 					    auth_skipped,
4921f8829a4aSRandall Stewart 					    auth_offset,
4922a5d547adSRandall Stewart 					    auth_len,
492380a2d140SMichael Tuexen 					    &locked_stcb,
4924457b4b88SMichael Tuexen 					    mflowtype,
4925f30ac432SMichael Tuexen 					    mflowid,
4926c54a18d2SRandall Stewart 					    vrf_id,
4927c54a18d2SRandall Stewart 					    port);
492880a2d140SMichael Tuexen 					if ((locked_stcb != NULL) && (locked_stcb != stcb)) {
492980a2d140SMichael Tuexen 						SCTP_TCB_UNLOCK(locked_stcb);
493080a2d140SMichael Tuexen 					}
493180a2d140SMichael Tuexen 					if (stcb != NULL) {
493280a2d140SMichael Tuexen 						SCTP_TCB_LOCK_ASSERT(stcb);
493380a2d140SMichael Tuexen 					}
4934ad81507eSRandall Stewart 				} else {
4935ad81507eSRandall Stewart 					ret_buf = NULL;
4936ad81507eSRandall Stewart 				}
4937469a65d1SMichael Tuexen 				if (linp != NULL) {
4938a5d547adSRandall Stewart 					SCTP_ASOC_CREATE_UNLOCK(linp);
4939ad81507eSRandall Stewart 				}
4940f8829a4aSRandall Stewart 				if (ret_buf == NULL) {
494180a2d140SMichael Tuexen 					if (stcb != NULL) {
494280a2d140SMichael Tuexen 						SCTP_TCB_UNLOCK(stcb);
4943f8829a4aSRandall Stewart 					}
4944ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INPUT3,
4945ad81507eSRandall Stewart 					    "GAK, null buffer\n");
4946f8829a4aSRandall Stewart 					*offset = length;
4947f8829a4aSRandall Stewart 					return (NULL);
4948f8829a4aSRandall Stewart 				}
4949f8829a4aSRandall Stewart 				/* if AUTH skipped, see if it verified... */
4950f8829a4aSRandall Stewart 				if (auth_skipped) {
4951f8829a4aSRandall Stewart 					got_auth = 1;
4952f8829a4aSRandall Stewart 					auth_skipped = 0;
4953f8829a4aSRandall Stewart 				}
4954efd5e692SMichael Tuexen 				/* Restart the timer if we have pending data */
495586fd36c5SMichael Tuexen 				TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
4956efd5e692SMichael Tuexen 					if (chk->whoTo != NULL) {
4957efd5e692SMichael Tuexen 						break;
4958efd5e692SMichael Tuexen 					}
4959efd5e692SMichael Tuexen 				}
4960efd5e692SMichael Tuexen 				if (chk != NULL) {
49614a9ef3f8SMichael Tuexen 					sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
4962f8829a4aSRandall Stewart 				}
4963f8829a4aSRandall Stewart 			}
4964f8829a4aSRandall Stewart 			break;
4965f8829a4aSRandall Stewart 		case SCTP_COOKIE_ACK:
4966469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE_ACK, stcb %p\n", (void *)stcb);
4967ad81507eSRandall Stewart 			if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
49689de7354bSMichael Tuexen 				break;
496917205eccSRandall Stewart 			}
4970f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4971f8829a4aSRandall Stewart 				/* We are not interested anymore */
4972f8829a4aSRandall Stewart 				if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4973f8829a4aSRandall Stewart 					;
4974ad81507eSRandall Stewart 				} else if (stcb) {
4975b7d130beSMichael Tuexen 					(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4976b7d130beSMichael Tuexen 					    SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
4977f8829a4aSRandall Stewart 					*offset = length;
4978f8829a4aSRandall Stewart 					return (NULL);
4979f8829a4aSRandall Stewart 				}
4980f8829a4aSRandall Stewart 			}
4981469a65d1SMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
4982f8829a4aSRandall Stewart 				sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
49836e55db54SRandall Stewart 			}
4984f8829a4aSRandall Stewart 			break;
4985f8829a4aSRandall Stewart 		case SCTP_ECN_ECHO:
4986469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_ECHO\n");
49879de7354bSMichael Tuexen 			if (stcb == NULL) {
49889de7354bSMichael Tuexen 				break;
4989d06c82f1SRandall Stewart 			}
4990c79bec9cSMichael Tuexen 			if (stcb->asoc.ecn_supported == 0) {
4991c79bec9cSMichael Tuexen 				goto unknown_chunk;
4992c79bec9cSMichael Tuexen 			}
49936587a2bdSMichael Tuexen 			if ((chk_length != sizeof(struct sctp_ecne_chunk)) &&
49946587a2bdSMichael Tuexen 			    (chk_length != sizeof(struct old_sctp_ecne_chunk))) {
49959de7354bSMichael Tuexen 				break;
49969de7354bSMichael Tuexen 			}
4997469a65d1SMichael Tuexen 			sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, stcb);
4998899288aeSRandall Stewart 			ecne_seen = 1;
4999f8829a4aSRandall Stewart 			break;
5000f8829a4aSRandall Stewart 		case SCTP_ECN_CWR:
5001469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_CWR\n");
50029de7354bSMichael Tuexen 			if (stcb == NULL) {
50039de7354bSMichael Tuexen 				break;
5004d06c82f1SRandall Stewart 			}
5005c79bec9cSMichael Tuexen 			if (stcb->asoc.ecn_supported == 0) {
5006c79bec9cSMichael Tuexen 				goto unknown_chunk;
5007c79bec9cSMichael Tuexen 			}
50089de7354bSMichael Tuexen 			if (chk_length != sizeof(struct sctp_cwr_chunk)) {
50099de7354bSMichael Tuexen 				break;
50109de7354bSMichael Tuexen 			}
5011a21779f0SRandall Stewart 			sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5012f8829a4aSRandall Stewart 			break;
5013f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_COMPLETE:
5014469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_COMPLETE, stcb %p\n", (void *)stcb);
5015f8829a4aSRandall Stewart 			/* must be first and only chunk */
5016f8829a4aSRandall Stewart 			if ((num_chunks > 1) ||
50174c9179adSRandall Stewart 			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5018f8829a4aSRandall Stewart 				*offset = length;
501980a2d140SMichael Tuexen 				return (stcb);
5020f8829a4aSRandall Stewart 			}
5021d1cb8d11SMichael Tuexen 			if ((chk_length == sizeof(struct sctp_shutdown_complete_chunk)) &&
5022d1cb8d11SMichael Tuexen 			    (stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5023f8829a4aSRandall Stewart 				sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5024f8829a4aSRandall Stewart 				    stcb, *netp);
5025f8829a4aSRandall Stewart 				*offset = length;
5026f8829a4aSRandall Stewart 				return (NULL);
5027d1cb8d11SMichael Tuexen 			}
5028f8829a4aSRandall Stewart 			break;
5029f8829a4aSRandall Stewart 		case SCTP_ASCONF:
5030ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5031469a65d1SMichael Tuexen 			if (stcb != NULL) {
5032c79bec9cSMichael Tuexen 				if (stcb->asoc.asconf_supported == 0) {
5033c79bec9cSMichael Tuexen 					goto unknown_chunk;
5034c79bec9cSMichael Tuexen 				}
5035b1754ad1SMichael Tuexen 				sctp_handle_asconf(m, *offset, src,
50362afb3e84SRandall Stewart 				    (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
50372afb3e84SRandall Stewart 				asconf_cnt++;
50386e55db54SRandall Stewart 			}
5039f8829a4aSRandall Stewart 			break;
5040f8829a4aSRandall Stewart 		case SCTP_ASCONF_ACK:
5041469a65d1SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF_ACK\n");
50429de7354bSMichael Tuexen 			if (stcb == NULL) {
50439de7354bSMichael Tuexen 				break;
5044d06c82f1SRandall Stewart 			}
5045c79bec9cSMichael Tuexen 			if (stcb->asoc.asconf_supported == 0) {
5046c79bec9cSMichael Tuexen 				goto unknown_chunk;
5047c79bec9cSMichael Tuexen 			}
50489de7354bSMichael Tuexen 			if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
50499de7354bSMichael Tuexen 				break;
50509de7354bSMichael Tuexen 			}
50519de7354bSMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
5052f8829a4aSRandall Stewart 				/* He's alive so give him credit */
5053b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5054c4739e2fSRandall Stewart 					sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5055c4739e2fSRandall Stewart 					    stcb->asoc.overall_error_count,
5056c4739e2fSRandall Stewart 					    0,
5057c4739e2fSRandall Stewart 					    SCTP_FROM_SCTP_INPUT,
5058c4739e2fSRandall Stewart 					    __LINE__);
5059c4739e2fSRandall Stewart 				}
5060f8829a4aSRandall Stewart 				stcb->asoc.overall_error_count = 0;
5061f8829a4aSRandall Stewart 				sctp_handle_asconf_ack(m, *offset,
50623232788eSRandall Stewart 				    (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
50633232788eSRandall Stewart 				if (abort_no_unlock)
50643232788eSRandall Stewart 					return (NULL);
50656e55db54SRandall Stewart 			}
5066f8829a4aSRandall Stewart 			break;
5067f8829a4aSRandall Stewart 		case SCTP_FORWARD_CUM_TSN:
506844249214SRandall Stewart 		case SCTP_IFORWARD_CUM_TSN:
5069c96d7c37SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "%s\n",
5070c96d7c37SMichael Tuexen 			    ch->chunk_type == SCTP_FORWARD_CUM_TSN ? "FORWARD_TSN" : "I_FORWARD_TSN");
50719de7354bSMichael Tuexen 			if (stcb == NULL) {
50729de7354bSMichael Tuexen 				break;
5073d06c82f1SRandall Stewart 			}
5074c79bec9cSMichael Tuexen 			if (stcb->asoc.prsctp_supported == 0) {
5075c79bec9cSMichael Tuexen 				goto unknown_chunk;
5076c79bec9cSMichael Tuexen 			}
50779de7354bSMichael Tuexen 			if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
50789de7354bSMichael Tuexen 				break;
50799de7354bSMichael Tuexen 			}
5080fcbfdc0aSMichael Tuexen 			if (((stcb->asoc.idata_supported == 1) && (ch->chunk_type == SCTP_FORWARD_CUM_TSN)) ||
5081fcbfdc0aSMichael Tuexen 			    ((stcb->asoc.idata_supported == 0) && (ch->chunk_type == SCTP_IFORWARD_CUM_TSN))) {
5082c96d7c37SMichael Tuexen 				if (ch->chunk_type == SCTP_FORWARD_CUM_TSN) {
5083c96d7c37SMichael Tuexen 					SCTP_SNPRINTF(msg, sizeof(msg), "%s", "FORWARD-TSN chunk received when I-FORWARD-TSN was negotiated");
5084c96d7c37SMichael Tuexen 				} else {
5085c96d7c37SMichael Tuexen 					SCTP_SNPRINTF(msg, sizeof(msg), "%s", "I-FORWARD-TSN chunk received when FORWARD-TSN was negotiated");
5086c96d7c37SMichael Tuexen 				}
5087c96d7c37SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
5088105b68b4SMichael Tuexen 				sctp_abort_an_association(inp, stcb, op_err, false, SCTP_SO_NOT_LOCKED);
5089c96d7c37SMichael Tuexen 				*offset = length;
5090c96d7c37SMichael Tuexen 				return (NULL);
5091c96d7c37SMichael Tuexen 			}
5092f8829a4aSRandall Stewart 			*fwd_tsn_seen = 1;
5093f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5094f8829a4aSRandall Stewart 				/* We are not interested anymore */
5095b7d130beSMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5096b7d130beSMichael Tuexen 				    SCTP_FROM_SCTP_INPUT + SCTP_LOC_31);
5097f8829a4aSRandall Stewart 				*offset = length;
5098f8829a4aSRandall Stewart 				return (NULL);
5099f8829a4aSRandall Stewart 			}
510091843cf3SMichael Tuexen 			/*
51019de7354bSMichael Tuexen 			 * For sending a SACK this looks like DATA chunks.
510291843cf3SMichael Tuexen 			 */
510391843cf3SMichael Tuexen 			stcb->asoc.last_data_chunk_from = stcb->asoc.last_control_chunk_from;
51049de7354bSMichael Tuexen 			abort_flag = 0;
5105f8829a4aSRandall Stewart 			sctp_handle_forward_tsn(stcb,
5106671d309cSRandall Stewart 			    (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5107f8829a4aSRandall Stewart 			if (abort_flag) {
5108f8829a4aSRandall Stewart 				*offset = length;
5109f8829a4aSRandall Stewart 				return (NULL);
5110c4739e2fSRandall Stewart 			}
5111f8829a4aSRandall Stewart 			break;
5112f8829a4aSRandall Stewart 		case SCTP_STREAM_RESET:
5113ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
51149de7354bSMichael Tuexen 			if (stcb == NULL) {
51159de7354bSMichael Tuexen 				break;
5116d06c82f1SRandall Stewart 			}
5117317e00efSMichael Tuexen 			if (stcb->asoc.reconfig_supported == 0) {
5118c79bec9cSMichael Tuexen 				goto unknown_chunk;
5119f8829a4aSRandall Stewart 			}
51209de7354bSMichael Tuexen 			if (chk_length < sizeof(struct sctp_stream_reset_tsn_req)) {
51219de7354bSMichael Tuexen 				break;
51229de7354bSMichael Tuexen 			}
5123a169d6ecSMichael Tuexen 			if (sctp_handle_stream_reset(stcb, m, *offset, ch)) {
5124f8829a4aSRandall Stewart 				/* stop processing */
5125f8829a4aSRandall Stewart 				*offset = length;
5126f8829a4aSRandall Stewart 				return (NULL);
5127f8829a4aSRandall Stewart 			}
5128f8829a4aSRandall Stewart 			break;
5129f8829a4aSRandall Stewart 		case SCTP_PACKET_DROPPED:
5130ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
51319de7354bSMichael Tuexen 			if (stcb == NULL) {
51329de7354bSMichael Tuexen 				break;
5133d06c82f1SRandall Stewart 			}
5134c79bec9cSMichael Tuexen 			if (stcb->asoc.pktdrop_supported == 0) {
5135c79bec9cSMichael Tuexen 				goto unknown_chunk;
5136c79bec9cSMichael Tuexen 			}
51379de7354bSMichael Tuexen 			if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
51389de7354bSMichael Tuexen 				break;
51399de7354bSMichael Tuexen 			}
51409de7354bSMichael Tuexen 			if ((netp != NULL) && (*netp != NULL)) {
5141f8829a4aSRandall Stewart 				sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5142458303daSRandall Stewart 				    stcb, *netp,
5143d0f6ab79SMichael Tuexen 				    min(chk_length, contiguous));
51446e55db54SRandall Stewart 			}
5145f8829a4aSRandall Stewart 			break;
5146f8829a4aSRandall Stewart 		case SCTP_AUTHENTICATION:
5147ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5148f8829a4aSRandall Stewart 			if (stcb == NULL) {
5149f8829a4aSRandall Stewart 				/* save the first AUTH for later processing */
5150f8829a4aSRandall Stewart 				if (auth_skipped == 0) {
5151f8829a4aSRandall Stewart 					auth_offset = *offset;
5152f8829a4aSRandall Stewart 					auth_len = chk_length;
5153f8829a4aSRandall Stewart 					auth_skipped = 1;
5154f8829a4aSRandall Stewart 				}
5155f8829a4aSRandall Stewart 				/* skip this chunk (temporarily) */
51569de7354bSMichael Tuexen 				break;
5157f8829a4aSRandall Stewart 			}
5158c79bec9cSMichael Tuexen 			if (stcb->asoc.auth_supported == 0) {
5159c79bec9cSMichael Tuexen 				goto unknown_chunk;
5160c79bec9cSMichael Tuexen 			}
5161d06c82f1SRandall Stewart 			if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5162ad81507eSRandall Stewart 			    (chk_length > (sizeof(struct sctp_auth_chunk) +
5163ad81507eSRandall Stewart 			    SCTP_AUTH_DIGEST_LEN_MAX))) {
5164d06c82f1SRandall Stewart 				/* Its not ours */
5165d06c82f1SRandall Stewart 				*offset = length;
516680a2d140SMichael Tuexen 				return (stcb);
5167d06c82f1SRandall Stewart 			}
5168f8829a4aSRandall Stewart 			if (got_auth == 1) {
5169f8829a4aSRandall Stewart 				/* skip this chunk... it's already auth'd */
51709de7354bSMichael Tuexen 				break;
5171f8829a4aSRandall Stewart 			}
5172f8829a4aSRandall Stewart 			got_auth = 1;
5173f2f66ef6SMichael Tuexen 			if (sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch, m, *offset)) {
5174f8829a4aSRandall Stewart 				/* auth HMAC failed so dump the packet */
5175f8829a4aSRandall Stewart 				*offset = length;
5176f8829a4aSRandall Stewart 				return (stcb);
5177f8829a4aSRandall Stewart 			} else {
5178f8829a4aSRandall Stewart 				/* remaining chunks are HMAC checked */
5179f8829a4aSRandall Stewart 				stcb->asoc.authenticated = 1;
5180f8829a4aSRandall Stewart 			}
5181f8829a4aSRandall Stewart 			break;
5182f8829a4aSRandall Stewart 
5183f8829a4aSRandall Stewart 		default:
5184f8829a4aSRandall Stewart 	unknown_chunk:
5185f8829a4aSRandall Stewart 			/* it's an unknown chunk! */
5186e99ce3eaSMichael Tuexen 			if ((ch->chunk_type & 0x40) &&
5187e99ce3eaSMichael Tuexen 			    (stcb != NULL) &&
5188e99ce3eaSMichael Tuexen 			    (SCTP_GET_STATE(stcb) != SCTP_STATE_EMPTY) &&
5189e99ce3eaSMichael Tuexen 			    (SCTP_GET_STATE(stcb) != SCTP_STATE_INUSE) &&
5190e99ce3eaSMichael Tuexen 			    (SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT)) {
519186eda749SMichael Tuexen 				struct sctp_gen_error_cause *cause;
519239cbb549SMichael Tuexen 				int len;
5193f8829a4aSRandall Stewart 
519486eda749SMichael Tuexen 				op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_gen_error_cause),
5195eb1b1807SGleb Smirnoff 				    0, M_NOWAIT, 1, MT_DATA);
519686eda749SMichael Tuexen 				if (op_err != NULL) {
519739cbb549SMichael Tuexen 					len = min(SCTP_SIZE32(chk_length), (uint32_t)(length - *offset));
519886eda749SMichael Tuexen 					cause = mtod(op_err, struct sctp_gen_error_cause *);
519986eda749SMichael Tuexen 					cause->code = htons(SCTP_CAUSE_UNRECOG_CHUNK);
52009a8e3088SMichael Tuexen 					cause->length = htons((uint16_t)(len + sizeof(struct sctp_gen_error_cause)));
520186eda749SMichael Tuexen 					SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
520286eda749SMichael Tuexen 					SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(m, *offset, len, M_NOWAIT);
520386eda749SMichael Tuexen 					if (SCTP_BUF_NEXT(op_err) != NULL) {
5204eadccaccSRandall Stewart #ifdef SCTP_MBUF_LOGGING
5205b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
520630811e70SMichael Tuexen 							sctp_log_mbc(SCTP_BUF_NEXT(op_err), SCTP_MBUF_ICOPY);
5207eadccaccSRandall Stewart 						}
5208eadccaccSRandall Stewart #endif
520986eda749SMichael Tuexen 						sctp_queue_op_err(stcb, op_err);
5210f8829a4aSRandall Stewart 					} else {
521186eda749SMichael Tuexen 						sctp_m_freem(op_err);
5212f8829a4aSRandall Stewart 					}
5213f8829a4aSRandall Stewart 				}
5214f8829a4aSRandall Stewart 			}
5215f8829a4aSRandall Stewart 			if ((ch->chunk_type & 0x80) == 0) {
5216f8829a4aSRandall Stewart 				/* discard this packet */
5217f8829a4aSRandall Stewart 				*offset = length;
5218f8829a4aSRandall Stewart 				return (stcb);
5219b7b84c0eSMichael Tuexen 			}	/* else skip this bad chunk and continue... */
5220b7b84c0eSMichael Tuexen 			break;
5221f8829a4aSRandall Stewart 		}		/* switch (ch->chunk_type) */
5222f8829a4aSRandall Stewart 
5223f8829a4aSRandall Stewart next_chunk:
5224f8829a4aSRandall Stewart 		/* get the next chunk */
5225f8829a4aSRandall Stewart 		*offset += SCTP_SIZE32(chk_length);
5226f8829a4aSRandall Stewart 		if (*offset >= length) {
5227f8829a4aSRandall Stewart 			/* no more data left in the mbuf chain */
5228f8829a4aSRandall Stewart 			break;
5229f8829a4aSRandall Stewart 		}
5230f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5231f8829a4aSRandall Stewart 		    sizeof(struct sctp_chunkhdr), chunk_buf);
5232f8829a4aSRandall Stewart 		if (ch == NULL) {
5233f8829a4aSRandall Stewart 			*offset = length;
523480a2d140SMichael Tuexen 			return (stcb);
5235f8829a4aSRandall Stewart 		}
5236f8829a4aSRandall Stewart 	}			/* while */
52372afb3e84SRandall Stewart 
5238469a65d1SMichael Tuexen 	if ((asconf_cnt > 0) && (stcb != NULL)) {
52392afb3e84SRandall Stewart 		sctp_send_asconf_ack(stcb);
52402afb3e84SRandall Stewart 	}
5241f8829a4aSRandall Stewart 	return (stcb);
5242f8829a4aSRandall Stewart }
5243f8829a4aSRandall Stewart 
5244f8829a4aSRandall Stewart /*
5245f8829a4aSRandall Stewart  * common input chunk processing (v4 and v6)
5246f8829a4aSRandall Stewart  */
52476e55db54SRandall Stewart void
sctp_common_input_processing(struct mbuf ** mm,int iphlen,int offset,int length,struct sockaddr * src,struct sockaddr * dst,struct sctphdr * sh,struct sctp_chunkhdr * ch,uint8_t compute_crc,uint8_t ecn_bits,uint8_t mflowtype,uint32_t mflowid,uint16_t fibnum,uint32_t vrf_id,uint16_t port)5248b1754ad1SMichael Tuexen sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
5249b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
5250b1754ad1SMichael Tuexen     struct sctphdr *sh, struct sctp_chunkhdr *ch,
5251a8775ad9SMichael Tuexen     uint8_t compute_crc,
5252a8775ad9SMichael Tuexen     uint8_t ecn_bits,
5253d089f9b9SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
5254f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
5255f8829a4aSRandall Stewart {
5256ff1ffd74SMichael Tuexen 	char msg[SCTP_DIAG_INFO_LEN];
52574a2b92d9SMichael Tuexen 	struct mbuf *m = *mm, *op_err;
525855b175e7SMichael Tuexen 	struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
5259a8775ad9SMichael Tuexen 	struct sctp_tcb *stcb = NULL;
5260e3d6ef0bSMichael Tuexen 	struct sctp_nets *net = NULL;
52614a2b92d9SMichael Tuexen 	uint32_t high_tsn;
52624a2b92d9SMichael Tuexen 	uint32_t cksum_in_hdr;
52634a2b92d9SMichael Tuexen 	int un_sent;
52644a2b92d9SMichael Tuexen 	int cnt_ctrl_ready = 0;
52654a2b92d9SMichael Tuexen 	int fwd_tsn_seen = 0, data_processed = 0;
52664a2b92d9SMichael Tuexen 	bool cksum_validated, stcb_looked_up;
5267f8829a4aSRandall Stewart 
5268f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvdatagrams);
5269f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
5270f8829a4aSRandall Stewart 	sctp_audit_log(0xE0, 1);
5271f8829a4aSRandall Stewart 	sctp_auditing(0, inp, stcb, net);
5272f8829a4aSRandall Stewart #endif
5273f8829a4aSRandall Stewart 
52744a2b92d9SMichael Tuexen 	stcb_looked_up = false;
52754a2b92d9SMichael Tuexen 	if (compute_crc != 0) {
52764a2b92d9SMichael Tuexen 		cksum_validated = false;
52774a2b92d9SMichael Tuexen 		cksum_in_hdr = sh->checksum;
52784a2b92d9SMichael Tuexen 		if (cksum_in_hdr != htonl(0)) {
52794a2b92d9SMichael Tuexen 			uint32_t cksum_calculated;
52804a2b92d9SMichael Tuexen 
52814a2b92d9SMichael Tuexen 	validate_cksum:
5282a8775ad9SMichael Tuexen 			sh->checksum = 0;
52834a2b92d9SMichael Tuexen 			cksum_calculated = sctp_calculate_cksum(m, iphlen);
52844a2b92d9SMichael Tuexen 			sh->checksum = cksum_in_hdr;
52854a2b92d9SMichael Tuexen 			if (cksum_calculated != cksum_in_hdr) {
52864a2b92d9SMichael Tuexen 				if (stcb_looked_up) {
52874a2b92d9SMichael Tuexen 					/*
52884a2b92d9SMichael Tuexen 					 * The packet has a zero checksum,
52894a2b92d9SMichael Tuexen 					 * which is not the correct CRC, no
52904a2b92d9SMichael Tuexen 					 * stcb has been found or an stcb
52914a2b92d9SMichael Tuexen 					 * has been found but an incorrect
52924a2b92d9SMichael Tuexen 					 * zero checksum is not acceptable.
52934a2b92d9SMichael Tuexen 					 */
52944a2b92d9SMichael Tuexen 					KASSERT(cksum_in_hdr == htonl(0),
52954a2b92d9SMichael Tuexen 					    ("cksum in header not zero: %x",
52964a2b92d9SMichael Tuexen 					    ntohl(cksum_in_hdr)));
52974a2b92d9SMichael Tuexen 					if ((inp == NULL) &&
52984a2b92d9SMichael Tuexen 					    (SCTP_BASE_SYSCTL(sctp_ootb_with_zero_cksum) == 1)) {
52994a2b92d9SMichael Tuexen 						/*
53004a2b92d9SMichael Tuexen 						 * This is an OOTB packet,
53014a2b92d9SMichael Tuexen 						 * depending on the sysctl
53024a2b92d9SMichael Tuexen 						 * variable, pretend that
53034a2b92d9SMichael Tuexen 						 * the checksum is
53044a2b92d9SMichael Tuexen 						 * acceptable, to allow an
53054a2b92d9SMichael Tuexen 						 * appropriate response
53064a2b92d9SMichael Tuexen 						 * (ABORT, for examlpe) to
53074a2b92d9SMichael Tuexen 						 * be sent.
53084a2b92d9SMichael Tuexen 						 */
53094a2b92d9SMichael Tuexen 						KASSERT(stcb == NULL,
53104a2b92d9SMichael Tuexen 						    ("stcb is %p", stcb));
53114a2b92d9SMichael Tuexen 						SCTP_STAT_INCR(sctps_recvzerocrc);
53124a2b92d9SMichael Tuexen 						goto cksum_validated;
53134a2b92d9SMichael Tuexen 					}
53144a2b92d9SMichael Tuexen 				} else {
5315a8775ad9SMichael Tuexen 					stcb = sctp_findassociation_addr(m, offset, src, dst,
5316a8775ad9SMichael Tuexen 					    sh, ch, &inp, &net, vrf_id);
53174a2b92d9SMichael Tuexen 				}
53184a2b92d9SMichael Tuexen 				SCTPDBG(SCTP_DEBUG_INPUT1, "Bad cksum in SCTP packet:%x calculated:%x m:%p mlen:%d iphlen:%d\n",
53194a2b92d9SMichael Tuexen 				    ntohl(cksum_in_hdr), ntohl(cksum_calculated), (void *)m, length, iphlen);
53204474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
53213cf729a9SMichael Tuexen 				if ((ch->chunk_type != SCTP_INITIATION) &&
53223cf729a9SMichael Tuexen 				    (net != NULL) && (net->port != port)) {
5323a8775ad9SMichael Tuexen 					if (net->port == 0) {
53244a2b92d9SMichael Tuexen 						/*
53254a2b92d9SMichael Tuexen 						 * UDP encapsulation turned
53264a2b92d9SMichael Tuexen 						 * on.
53274a2b92d9SMichael Tuexen 						 */
53283cf729a9SMichael Tuexen 						net->mtu -= sizeof(struct udphdr);
53293cf729a9SMichael Tuexen 						if (stcb->asoc.smallest_mtu > net->mtu) {
53302de2ae33SMichael Tuexen 							sctp_pathmtu_adjustment(stcb, net->mtu, true);
53313cf729a9SMichael Tuexen 						}
53323cf729a9SMichael Tuexen 					} else if (port == 0) {
53334a2b92d9SMichael Tuexen 						/*
53344a2b92d9SMichael Tuexen 						 * UDP encapsulation turned
53354a2b92d9SMichael Tuexen 						 * off.
53364a2b92d9SMichael Tuexen 						 */
53373cf729a9SMichael Tuexen 						net->mtu += sizeof(struct udphdr);
53383cf729a9SMichael Tuexen 						/* XXX Update smallest_mtu */
5339a8775ad9SMichael Tuexen 					}
5340a8775ad9SMichael Tuexen 					net->port = port;
5341a8775ad9SMichael Tuexen 				}
53424474d71aSMichael Tuexen #endif
53435fe29cdfSMichael Tuexen 				if (net != NULL) {
5344457b4b88SMichael Tuexen 					net->flowtype = mflowtype;
53455fe29cdfSMichael Tuexen 					net->flowid = mflowid;
5346a8775ad9SMichael Tuexen 				}
53471e88cc8bSMichael Tuexen 				SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5348a8775ad9SMichael Tuexen 				if ((inp != NULL) && (stcb != NULL)) {
53494a2b92d9SMichael Tuexen 					if (stcb->asoc.pktdrop_supported) {
5350a8775ad9SMichael Tuexen 						sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
5351a8775ad9SMichael Tuexen 						sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
53524a2b92d9SMichael Tuexen 					}
5353a8775ad9SMichael Tuexen 				} else if ((inp != NULL) && (stcb == NULL)) {
5354a8775ad9SMichael Tuexen 					inp_decr = inp;
5355a8775ad9SMichael Tuexen 				}
5356a8775ad9SMichael Tuexen 				SCTP_STAT_INCR(sctps_badsum);
5357a8775ad9SMichael Tuexen 				SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5358a8775ad9SMichael Tuexen 				goto out;
53594a2b92d9SMichael Tuexen 			} else {
53604a2b92d9SMichael Tuexen 				cksum_validated = true;
5361a8775ad9SMichael Tuexen 			}
5362a8775ad9SMichael Tuexen 		}
53634a2b92d9SMichael Tuexen 		KASSERT(cksum_validated || cksum_in_hdr == htonl(0),
53644a2b92d9SMichael Tuexen 		    ("cksum 0x%08x not zero and not validated", ntohl(cksum_in_hdr)));
53654a2b92d9SMichael Tuexen 		if (!cksum_validated) {
5366a8775ad9SMichael Tuexen 			stcb = sctp_findassociation_addr(m, offset, src, dst,
5367a8775ad9SMichael Tuexen 			    sh, ch, &inp, &net, vrf_id);
53684a2b92d9SMichael Tuexen 			stcb_looked_up = true;
536952640d61SMichael Tuexen 			if (stcb == NULL) {
53704a2b92d9SMichael Tuexen 				goto validate_cksum;
53714a2b92d9SMichael Tuexen 			}
537252640d61SMichael Tuexen 			if (stcb->asoc.rcv_edmid == SCTP_EDMID_NONE) {
537352640d61SMichael Tuexen 				goto validate_cksum;
537452640d61SMichael Tuexen 			}
537552640d61SMichael Tuexen 			KASSERT(stcb->asoc.rcv_edmid == SCTP_EDMID_LOWER_LAYER_DTLS,
537652640d61SMichael Tuexen 			    ("Unexpected EDMID %u", stcb->asoc.rcv_edmid));
53774a2b92d9SMichael Tuexen 			SCTP_STAT_INCR(sctps_recvzerocrc);
53784a2b92d9SMichael Tuexen 		}
53794a2b92d9SMichael Tuexen 	}
53804a2b92d9SMichael Tuexen cksum_validated:
53814a2b92d9SMichael Tuexen 	/* Destination port of 0 is illegal, based on RFC4960. */
53824a2b92d9SMichael Tuexen 	if (sh->dest_port == htons(0)) {
53834a2b92d9SMichael Tuexen 		SCTP_STAT_INCR(sctps_hdrops);
53844a2b92d9SMichael Tuexen 		if ((stcb == NULL) && (inp != NULL)) {
53854a2b92d9SMichael Tuexen 			inp_decr = inp;
53864a2b92d9SMichael Tuexen 		}
53874a2b92d9SMichael Tuexen 		goto out;
53884a2b92d9SMichael Tuexen 	}
53894a2b92d9SMichael Tuexen 	if (!stcb_looked_up) {
53904a2b92d9SMichael Tuexen 		stcb = sctp_findassociation_addr(m, offset, src, dst,
53914a2b92d9SMichael Tuexen 		    sh, ch, &inp, &net, vrf_id);
53924a2b92d9SMichael Tuexen 	}
53934474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
53943cf729a9SMichael Tuexen 	if ((ch->chunk_type != SCTP_INITIATION) &&
53953cf729a9SMichael Tuexen 	    (net != NULL) && (net->port != port)) {
5396a8775ad9SMichael Tuexen 		if (net->port == 0) {
53973cf729a9SMichael Tuexen 			/* UDP encapsulation turned on. */
53983cf729a9SMichael Tuexen 			net->mtu -= sizeof(struct udphdr);
53993cf729a9SMichael Tuexen 			if (stcb->asoc.smallest_mtu > net->mtu) {
54002de2ae33SMichael Tuexen 				sctp_pathmtu_adjustment(stcb, net->mtu, true);
54013cf729a9SMichael Tuexen 			}
54023cf729a9SMichael Tuexen 		} else if (port == 0) {
54033cf729a9SMichael Tuexen 			/* UDP encapsulation turned off. */
54043cf729a9SMichael Tuexen 			net->mtu += sizeof(struct udphdr);
54053cf729a9SMichael Tuexen 			/* XXX Update smallest_mtu */
5406a8775ad9SMichael Tuexen 		}
5407a8775ad9SMichael Tuexen 		net->port = port;
5408a8775ad9SMichael Tuexen 	}
54094474d71aSMichael Tuexen #endif
54105fe29cdfSMichael Tuexen 	if (net != NULL) {
5411457b4b88SMichael Tuexen 		net->flowtype = mflowtype;
54125fe29cdfSMichael Tuexen 		net->flowid = mflowid;
5413a8775ad9SMichael Tuexen 	}
5414a8775ad9SMichael Tuexen 	if (inp == NULL) {
5415a8775ad9SMichael Tuexen 		SCTP_STAT_INCR(sctps_noport);
54164a2b92d9SMichael Tuexen 		SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5417a8775ad9SMichael Tuexen 		if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
5418a8775ad9SMichael Tuexen 			goto out;
5419a8775ad9SMichael Tuexen 		}
5420a8775ad9SMichael Tuexen 		if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5421502d5e85SMichael Tuexen 			SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
5422a8775ad9SMichael Tuexen 			sctp_send_shutdown_complete2(src, dst, sh,
5423d089f9b9SMichael Tuexen 			    mflowtype, mflowid, fibnum,
5424a8775ad9SMichael Tuexen 			    vrf_id, port);
5425a8775ad9SMichael Tuexen 			goto out;
5426a8775ad9SMichael Tuexen 		}
5427a8775ad9SMichael Tuexen 		if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5428502d5e85SMichael Tuexen 			SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
5429a8775ad9SMichael Tuexen 			goto out;
5430a8775ad9SMichael Tuexen 		}
5431a8775ad9SMichael Tuexen 		if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
5432a8775ad9SMichael Tuexen 			if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
5433a8775ad9SMichael Tuexen 			    ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
5434a8775ad9SMichael Tuexen 			    (ch->chunk_type != SCTP_INIT))) {
5435ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5436ff1ffd74SMichael Tuexen 				    "Out of the blue");
5437a8775ad9SMichael Tuexen 				sctp_send_abort(m, iphlen, src, dst,
5438ff1ffd74SMichael Tuexen 				    sh, 0, op_err,
5439d089f9b9SMichael Tuexen 				    mflowtype, mflowid, fibnum,
5440a8775ad9SMichael Tuexen 				    vrf_id, port);
5441a8775ad9SMichael Tuexen 			}
5442a8775ad9SMichael Tuexen 		}
5443a8775ad9SMichael Tuexen 		goto out;
5444a8775ad9SMichael Tuexen 	} else if (stcb == NULL) {
5445a8775ad9SMichael Tuexen 		inp_decr = inp;
5446a8775ad9SMichael Tuexen 	}
5447b3f1ea41SRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5448dd294dceSMichael Tuexen 	    (void *)m, iphlen, offset, length, (void *)stcb);
5449f8829a4aSRandall Stewart 	if (stcb) {
5450f8829a4aSRandall Stewart 		/* always clear this before beginning a packet */
5451f8829a4aSRandall Stewart 		stcb->asoc.authenticated = 0;
5452f8829a4aSRandall Stewart 		stcb->asoc.seen_a_sack_this_pkt = 0;
54532afb3e84SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5454dd294dceSMichael Tuexen 		    (void *)stcb, stcb->asoc.state);
54552afb3e84SRandall Stewart 
5456c4739e2fSRandall Stewart 		if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5457c4739e2fSRandall Stewart 		    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
545863981c2bSRandall Stewart 			/*-
545963981c2bSRandall Stewart 			 * If we hit here, we had a ref count
546063981c2bSRandall Stewart 			 * up when the assoc was aborted and the
546163981c2bSRandall Stewart 			 * timer is clearing out the assoc, we should
546263981c2bSRandall Stewart 			 * NOT respond to any packet.. its OOTB.
546363981c2bSRandall Stewart 			 */
546463981c2bSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
5465a8775ad9SMichael Tuexen 			stcb = NULL;
54661e88cc8bSMichael Tuexen 			SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5467999f86d6SMichael Tuexen 			SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5468ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5469ff1ffd74SMichael Tuexen 			    msg);
5470ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5471d089f9b9SMichael Tuexen 			    mflowtype, mflowid, inp->fibnum,
5472c54a18d2SRandall Stewart 			    vrf_id, port);
5473a8775ad9SMichael Tuexen 			goto out;
547463981c2bSRandall Stewart 		}
5475f8829a4aSRandall Stewart 	}
5476f8829a4aSRandall Stewart 	if (IS_SCTP_CONTROL(ch)) {
5477f8829a4aSRandall Stewart 		/* process the control portion of the SCTP packet */
54783c503c28SRandall Stewart 		/* sa_ignore NO_NULL_CHK */
5479b1754ad1SMichael Tuexen 		stcb = sctp_process_control(m, iphlen, &offset, length,
5480b1754ad1SMichael Tuexen 		    src, dst, sh, ch,
5481f30ac432SMichael Tuexen 		    inp, stcb, &net, &fwd_tsn_seen,
5482d089f9b9SMichael Tuexen 		    mflowtype, mflowid, fibnum,
5483f30ac432SMichael Tuexen 		    vrf_id, port);
5484f8829a4aSRandall Stewart 		if (stcb) {
5485f8829a4aSRandall Stewart 			/*
5486f8829a4aSRandall Stewart 			 * This covers us if the cookie-echo was there and
5487f8829a4aSRandall Stewart 			 * it changes our INP.
5488f8829a4aSRandall Stewart 			 */
5489f8829a4aSRandall Stewart 			inp = stcb->sctp_ep;
54904474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
54913cf729a9SMichael Tuexen 			if ((ch->chunk_type != SCTP_INITIATION) &&
54923cf729a9SMichael Tuexen 			    (net != NULL) && (net->port != port)) {
5493b3f1ea41SRandall Stewart 				if (net->port == 0) {
54943cf729a9SMichael Tuexen 					/* UDP encapsulation turned on. */
54953cf729a9SMichael Tuexen 					net->mtu -= sizeof(struct udphdr);
54963cf729a9SMichael Tuexen 					if (stcb->asoc.smallest_mtu > net->mtu) {
54972de2ae33SMichael Tuexen 						sctp_pathmtu_adjustment(stcb, net->mtu, true);
54983cf729a9SMichael Tuexen 					}
54993cf729a9SMichael Tuexen 				} else if (port == 0) {
55003cf729a9SMichael Tuexen 					/* UDP encapsulation turned off. */
55013cf729a9SMichael Tuexen 					net->mtu += sizeof(struct udphdr);
55023cf729a9SMichael Tuexen 					/* XXX Update smallest_mtu */
5503b3f1ea41SRandall Stewart 				}
5504b3f1ea41SRandall Stewart 				net->port = port;
5505b3f1ea41SRandall Stewart 			}
55064474d71aSMichael Tuexen #endif
5507f8829a4aSRandall Stewart 		}
5508f8829a4aSRandall Stewart 	} else {
5509f8829a4aSRandall Stewart 		/*
5510f8829a4aSRandall Stewart 		 * no control chunks, so pre-process DATA chunks (these
5511f8829a4aSRandall Stewart 		 * checks are taken care of by control processing)
5512f8829a4aSRandall Stewart 		 */
5513f8829a4aSRandall Stewart 
5514f8829a4aSRandall Stewart 		/*
5515f8829a4aSRandall Stewart 		 * if DATA only packet, and auth is required, then punt...
5516f8829a4aSRandall Stewart 		 * can't have authenticated without any AUTH (control)
5517f8829a4aSRandall Stewart 		 * chunks
5518f8829a4aSRandall Stewart 		 */
5519b3f1ea41SRandall Stewart 		if ((stcb != NULL) &&
5520b3f1ea41SRandall Stewart 		    sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5521f8829a4aSRandall Stewart 			/* "silently" ignore */
55221e88cc8bSMichael Tuexen 			SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5523f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvauthmissing);
5524a8775ad9SMichael Tuexen 			goto out;
5525f8829a4aSRandall Stewart 		}
5526f8829a4aSRandall Stewart 		if (stcb == NULL) {
5527f8829a4aSRandall Stewart 			/* out of the blue DATA chunk */
55281e88cc8bSMichael Tuexen 			SCTP_PROBE5(receive, NULL, NULL, m, NULL, sh);
5529999f86d6SMichael Tuexen 			SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5530ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5531ff1ffd74SMichael Tuexen 			    msg);
5532ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5533d089f9b9SMichael Tuexen 			    mflowtype, mflowid, fibnum,
5534c54a18d2SRandall Stewart 			    vrf_id, port);
5535a8775ad9SMichael Tuexen 			goto out;
5536f8829a4aSRandall Stewart 		}
5537f8829a4aSRandall Stewart 		if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5538f8829a4aSRandall Stewart 			/* v_tag mismatch! */
55391e88cc8bSMichael Tuexen 			SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5540f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_badvtag);
5541a8775ad9SMichael Tuexen 			goto out;
5542f8829a4aSRandall Stewart 		}
5543f8829a4aSRandall Stewart 	}
5544f8829a4aSRandall Stewart 
55451e88cc8bSMichael Tuexen 	SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5546f8829a4aSRandall Stewart 	if (stcb == NULL) {
5547f8829a4aSRandall Stewart 		/*
5548f8829a4aSRandall Stewart 		 * no valid TCB for this packet, or we found it's a bad
5549f8829a4aSRandall Stewart 		 * packet while processing control, or we're done with this
5550f8829a4aSRandall Stewart 		 * packet (done or skip rest of data), so we drop it...
5551f8829a4aSRandall Stewart 		 */
5552a8775ad9SMichael Tuexen 		goto out;
5553f8829a4aSRandall Stewart 	}
55540053ed28SMichael Tuexen 
5555f8829a4aSRandall Stewart 	/*
5556f8829a4aSRandall Stewart 	 * DATA chunk processing
5557f8829a4aSRandall Stewart 	 */
5558f8829a4aSRandall Stewart 	/* plow through the data chunks while length > offset */
5559f8829a4aSRandall Stewart 
5560f8829a4aSRandall Stewart 	/*
5561f8829a4aSRandall Stewart 	 * Rest should be DATA only.  Check authentication state if AUTH for
5562f8829a4aSRandall Stewart 	 * DATA is required.
5563f8829a4aSRandall Stewart 	 */
5564b3f1ea41SRandall Stewart 	if ((length > offset) &&
5565b3f1ea41SRandall Stewart 	    (stcb != NULL) &&
5566b3f1ea41SRandall Stewart 	    sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5567f8829a4aSRandall Stewart 	    !stcb->asoc.authenticated) {
5568f8829a4aSRandall Stewart 		/* "silently" ignore */
5569f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvauthmissing);
5570ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_AUTH1,
5571ad81507eSRandall Stewart 		    "Data chunk requires AUTH, skipped\n");
5572a5d547adSRandall Stewart 		goto trigger_send;
5573f8829a4aSRandall Stewart 	}
5574f8829a4aSRandall Stewart 	if (length > offset) {
5575f8829a4aSRandall Stewart 		int retval;
5576f8829a4aSRandall Stewart 
5577f8829a4aSRandall Stewart 		/*
5578f8829a4aSRandall Stewart 		 * First check to make sure our state is correct. We would
5579f8829a4aSRandall Stewart 		 * not get here unless we really did have a tag, so we don't
5580f8829a4aSRandall Stewart 		 * abort if this happens, just dump the chunk silently.
5581f8829a4aSRandall Stewart 		 */
5582839d21d6SMichael Tuexen 		switch (SCTP_GET_STATE(stcb)) {
5583f8829a4aSRandall Stewart 		case SCTP_STATE_COOKIE_ECHOED:
5584f8829a4aSRandall Stewart 			/*
5585f8829a4aSRandall Stewart 			 * we consider data with valid tags in this state
5586f8829a4aSRandall Stewart 			 * shows us the cookie-ack was lost. Imply it was
5587f8829a4aSRandall Stewart 			 * there.
5588f8829a4aSRandall Stewart 			 */
5589f8829a4aSRandall Stewart 			sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5590f8829a4aSRandall Stewart 			break;
5591f8829a4aSRandall Stewart 		case SCTP_STATE_COOKIE_WAIT:
5592f8829a4aSRandall Stewart 			/*
5593f8829a4aSRandall Stewart 			 * We consider OOTB any data sent during asoc setup.
5594f8829a4aSRandall Stewart 			 */
5595999f86d6SMichael Tuexen 			SCTP_SNPRINTF(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5596ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5597ff1ffd74SMichael Tuexen 			    msg);
5598ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5599d089f9b9SMichael Tuexen 			    mflowtype, mflowid, inp->fibnum,
5600c54a18d2SRandall Stewart 			    vrf_id, port);
5601a8775ad9SMichael Tuexen 			goto out;
560252be287eSRandall Stewart 			/* sa_ignore NOTREACHED */
5603f8829a4aSRandall Stewart 			break;
5604f8829a4aSRandall Stewart 		case SCTP_STATE_EMPTY:	/* should not happen */
5605f8829a4aSRandall Stewart 		case SCTP_STATE_INUSE:	/* should not happen */
5606f8829a4aSRandall Stewart 		case SCTP_STATE_SHUTDOWN_RECEIVED:	/* This is a peer error */
5607f8829a4aSRandall Stewart 		case SCTP_STATE_SHUTDOWN_ACK_SENT:
5608f8829a4aSRandall Stewart 		default:
5609a8775ad9SMichael Tuexen 			goto out;
561052be287eSRandall Stewart 			/* sa_ignore NOTREACHED */
5611f8829a4aSRandall Stewart 			break;
5612f8829a4aSRandall Stewart 		case SCTP_STATE_OPEN:
5613f8829a4aSRandall Stewart 		case SCTP_STATE_SHUTDOWN_SENT:
5614f8829a4aSRandall Stewart 			break;
5615f8829a4aSRandall Stewart 		}
5616f8829a4aSRandall Stewart 		/* plow through the data chunks while length > offset */
5617b1754ad1SMichael Tuexen 		retval = sctp_process_data(mm, iphlen, &offset, length,
5618e7e71dd7SMichael Tuexen 		    inp, stcb, net, &high_tsn);
5619f8829a4aSRandall Stewart 		if (retval == 2) {
5620f8829a4aSRandall Stewart 			/*
5621f8829a4aSRandall Stewart 			 * The association aborted, NO UNLOCK needed since
5622f8829a4aSRandall Stewart 			 * the association is destroyed.
5623f8829a4aSRandall Stewart 			 */
5624a8775ad9SMichael Tuexen 			stcb = NULL;
5625a8775ad9SMichael Tuexen 			goto out;
5626f8829a4aSRandall Stewart 		}
5627b954d816SMichael Tuexen 		if (retval == 0) {
5628f8829a4aSRandall Stewart 			data_processed = 1;
5629b954d816SMichael Tuexen 		}
5630f8829a4aSRandall Stewart 		/*
5631f8829a4aSRandall Stewart 		 * Anything important needs to have been m_copy'ed in
5632f8829a4aSRandall Stewart 		 * process_data
5633f8829a4aSRandall Stewart 		 */
5634f8829a4aSRandall Stewart 	}
56350053ed28SMichael Tuexen 
5636493d8e5aSRandall Stewart 	/* take care of ecn */
563760990c0cSMichael Tuexen 	if ((data_processed == 1) &&
5638f342355aSMichael Tuexen 	    (stcb->asoc.ecn_supported == 1) &&
5639c446091bSMichael Tuexen 	    ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5640493d8e5aSRandall Stewart 		/* Yep, we need to add a ECNE */
5641493d8e5aSRandall Stewart 		sctp_send_ecn_echo(stcb, net, high_tsn);
5642493d8e5aSRandall Stewart 	}
56430053ed28SMichael Tuexen 
5644f8829a4aSRandall Stewart 	if ((data_processed == 0) && (fwd_tsn_seen)) {
56458f777478SMichael Tuexen 		int was_a_gap;
56468f777478SMichael Tuexen 		uint32_t highest_tsn;
5647f8829a4aSRandall Stewart 
564820b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
56498f777478SMichael Tuexen 			highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
56508f777478SMichael Tuexen 		} else {
56518f777478SMichael Tuexen 			highest_tsn = stcb->asoc.highest_tsn_inside_map;
5652f8829a4aSRandall Stewart 		}
565320b07a4dSMichael Tuexen 		was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
56548933fa13SRandall Stewart 		stcb->asoc.send_sack = 1;
56557215cc1bSMichael Tuexen 		sctp_sack_check(stcb, was_a_gap);
56568933fa13SRandall Stewart 	} else if (fwd_tsn_seen) {
56578933fa13SRandall Stewart 		stcb->asoc.send_sack = 1;
5658f8829a4aSRandall Stewart 	}
5659f8829a4aSRandall Stewart 	/* trigger send of any chunks in queue... */
5660a5d547adSRandall Stewart trigger_send:
5661f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
5662f8829a4aSRandall Stewart 	sctp_audit_log(0xE0, 2);
5663f8829a4aSRandall Stewart 	sctp_auditing(1, inp, stcb, net);
5664f8829a4aSRandall Stewart #endif
5665ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT1,
5666ad81507eSRandall Stewart 	    "Check for chunk output prw:%d tqe:%d tf=%d\n",
5667f8829a4aSRandall Stewart 	    stcb->asoc.peers_rwnd,
5668f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5669f8829a4aSRandall Stewart 	    stcb->asoc.total_flight);
5670f8829a4aSRandall Stewart 	un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5671493d8e5aSRandall Stewart 	if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
5672493d8e5aSRandall Stewart 		cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
5673493d8e5aSRandall Stewart 	}
56745114dccbSMichael Tuexen 	if (!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue) ||
56755114dccbSMichael Tuexen 	    cnt_ctrl_ready ||
56765114dccbSMichael Tuexen 	    stcb->asoc.trigger_reset ||
567764c8fc5dSMichael Tuexen 	    ((un_sent > 0) &&
567864c8fc5dSMichael Tuexen 	    (stcb->asoc.peers_rwnd > 0 || stcb->asoc.total_flight == 0))) {
5679ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5680ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5681ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5682f8829a4aSRandall Stewart 	}
5683f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
5684f8829a4aSRandall Stewart 	sctp_audit_log(0xE0, 3);
5685f8829a4aSRandall Stewart 	sctp_auditing(2, inp, stcb, net);
5686f8829a4aSRandall Stewart #endif
5687a8775ad9SMichael Tuexen out:
5688a8775ad9SMichael Tuexen 	if (stcb != NULL) {
5689f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
5690a8775ad9SMichael Tuexen 	}
5691a8775ad9SMichael Tuexen 	if (inp_decr != NULL) {
5692a8775ad9SMichael Tuexen 		/* reduce ref-count */
5693a8775ad9SMichael Tuexen 		SCTP_INP_WLOCK(inp_decr);
5694a8775ad9SMichael Tuexen 		SCTP_INP_DECR_REF(inp_decr);
5695a8775ad9SMichael Tuexen 		SCTP_INP_WUNLOCK(inp_decr);
5696a8775ad9SMichael Tuexen 	}
56976e55db54SRandall Stewart 	return;
5698f8829a4aSRandall Stewart }
5699f8829a4aSRandall Stewart 
5700e6194c2eSMichael Tuexen #ifdef INET
5701f8829a4aSRandall Stewart void
sctp_input_with_port(struct mbuf * i_pak,int off,uint16_t port)5702af83f5d7SRoman Divacky sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
5703f8829a4aSRandall Stewart {
5704139bc87fSRandall Stewart 	struct mbuf *m;
5705f8829a4aSRandall Stewart 	int iphlen;
5706ad21a364SRandall Stewart 	uint32_t vrf_id = 0;
5707f8829a4aSRandall Stewart 	uint8_t ecn_bits;
5708b1754ad1SMichael Tuexen 	struct sockaddr_in src, dst;
5709f8829a4aSRandall Stewart 	struct ip *ip;
5710f8829a4aSRandall Stewart 	struct sctphdr *sh;
5711f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
57126dc5aabcSMichael Tuexen 	int length, offset;
5713a8775ad9SMichael Tuexen 	uint8_t compute_crc;
5714a8775ad9SMichael Tuexen 	uint32_t mflowid;
5715457b4b88SMichael Tuexen 	uint8_t mflowtype;
5716d089f9b9SMichael Tuexen 	uint16_t fibnum;
57179c7635e1SMichael Tuexen 
57186dc5aabcSMichael Tuexen 	iphlen = off;
571917205eccSRandall Stewart 	if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
572017205eccSRandall Stewart 		SCTP_RELEASE_PKT(i_pak);
572117205eccSRandall Stewart 		return;
572217205eccSRandall Stewart 	}
5723139bc87fSRandall Stewart 	m = SCTP_HEADER_TO_CHAIN(i_pak);
5724f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
5725f8829a4aSRandall Stewart 	/* Log in any input mbufs */
5726b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
57274be807c4SMichael Tuexen 		sctp_log_mbc(m, SCTP_MBUF_INPUT);
572880fefe0aSRandall Stewart 	}
5729f8829a4aSRandall Stewart #endif
5730207304d4SRandall Stewart #ifdef SCTP_PACKET_LOGGING
57316dc5aabcSMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
5732f9384252SMichael Tuexen 		sctp_packet_log(m);
57336dc5aabcSMichael Tuexen 	}
5734207304d4SRandall Stewart #endif
5735a8775ad9SMichael Tuexen 	SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
57361a94cdbeSMichael Tuexen 	    "sctp_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
5737a8775ad9SMichael Tuexen 	    m->m_pkthdr.len,
5738a8775ad9SMichael Tuexen 	    if_name(m->m_pkthdr.rcvif),
57391a94cdbeSMichael Tuexen 	    (int)m->m_pkthdr.csum_flags, CSUM_BITS);
5740f30ac432SMichael Tuexen 	mflowid = m->m_pkthdr.flowid;
5741457b4b88SMichael Tuexen 	mflowtype = M_HASHTYPE_GET(m);
5742d089f9b9SMichael Tuexen 	fibnum = M_GETFIB(m);
57436dc5aabcSMichael Tuexen 	SCTP_STAT_INCR(sctps_recvpackets);
57446dc5aabcSMichael Tuexen 	SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
57456dc5aabcSMichael Tuexen 	/* Get IP, SCTP, and first chunk header together in the first mbuf. */
57466dc5aabcSMichael Tuexen 	offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5747139bc87fSRandall Stewart 	if (SCTP_BUF_LEN(m) < offset) {
5748b1754ad1SMichael Tuexen 		if ((m = m_pullup(m, offset)) == NULL) {
5749f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_hdrops);
5750f8829a4aSRandall Stewart 			return;
5751f8829a4aSRandall Stewart 		}
5752f8829a4aSRandall Stewart 	}
5753b1754ad1SMichael Tuexen 	ip = mtod(m, struct ip *);
57546dc5aabcSMichael Tuexen 	sh = (struct sctphdr *)((caddr_t)ip + iphlen);
57556dc5aabcSMichael Tuexen 	ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
57566dc5aabcSMichael Tuexen 	offset -= sizeof(struct sctp_chunkhdr);
5757b1754ad1SMichael Tuexen 	memset(&src, 0, sizeof(struct sockaddr_in));
5758b1754ad1SMichael Tuexen 	src.sin_family = AF_INET;
5759b1754ad1SMichael Tuexen 	src.sin_len = sizeof(struct sockaddr_in);
5760b1754ad1SMichael Tuexen 	src.sin_port = sh->src_port;
5761b1754ad1SMichael Tuexen 	src.sin_addr = ip->ip_src;
5762b1754ad1SMichael Tuexen 	memset(&dst, 0, sizeof(struct sockaddr_in));
5763b1754ad1SMichael Tuexen 	dst.sin_family = AF_INET;
5764b1754ad1SMichael Tuexen 	dst.sin_len = sizeof(struct sockaddr_in);
5765b1754ad1SMichael Tuexen 	dst.sin_port = sh->dest_port;
5766b1754ad1SMichael Tuexen 	dst.sin_addr = ip->ip_dst;
57678ad458a4SGleb Smirnoff 	length = ntohs(ip->ip_len);
57686dc5aabcSMichael Tuexen 	/* Validate mbuf chain length with IP payload length. */
5769a8775ad9SMichael Tuexen 	if (SCTP_HEADER_LEN(m) != length) {
57706dc5aabcSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT1,
5771a8775ad9SMichael Tuexen 		    "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
5772a99b6783SRandall Stewart 		SCTP_STAT_INCR(sctps_hdrops);
5773a8775ad9SMichael Tuexen 		goto out;
5774a99b6783SRandall Stewart 	}
5775f8829a4aSRandall Stewart 	/* SCTP does not allow broadcasts or multicasts */
5776b1754ad1SMichael Tuexen 	if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
5777a8775ad9SMichael Tuexen 		goto out;
5778f8829a4aSRandall Stewart 	}
5779b1754ad1SMichael Tuexen 	if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
5780a8775ad9SMichael Tuexen 		goto out;
5781f8829a4aSRandall Stewart 	}
5782a8775ad9SMichael Tuexen 	ecn_bits = ip->ip_tos;
5783a99b6783SRandall Stewart 	if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
5784a99b6783SRandall Stewart 		SCTP_STAT_INCR(sctps_recvhwcrc);
5785a8775ad9SMichael Tuexen 		compute_crc = 0;
5786a8775ad9SMichael Tuexen 	} else {
5787a99b6783SRandall Stewart 		SCTP_STAT_INCR(sctps_recvswcrc);
5788a8775ad9SMichael Tuexen 		compute_crc = 1;
5789f8829a4aSRandall Stewart 	}
5790b1754ad1SMichael Tuexen 	sctp_common_input_processing(&m, iphlen, offset, length,
5791b1754ad1SMichael Tuexen 	    (struct sockaddr *)&src,
5792b1754ad1SMichael Tuexen 	    (struct sockaddr *)&dst,
5793a8775ad9SMichael Tuexen 	    sh, ch,
5794a8775ad9SMichael Tuexen 	    compute_crc,
5795a8775ad9SMichael Tuexen 	    ecn_bits,
5796d089f9b9SMichael Tuexen 	    mflowtype, mflowid, fibnum,
5797f30ac432SMichael Tuexen 	    vrf_id, port);
5798a8775ad9SMichael Tuexen out:
5799f8829a4aSRandall Stewart 	if (m) {
5800f8829a4aSRandall Stewart 		sctp_m_freem(m);
5801f8829a4aSRandall Stewart 	}
5802f8829a4aSRandall Stewart 	return;
5803f8829a4aSRandall Stewart }
5804bfc46083SRandall Stewart 
58052f9e6db0SMichael Tuexen #if defined(SCTP_MCORE_INPUT) && defined(SMP)
58060071ee5eSRandall Stewart extern int *sctp_cpuarry;
58070071ee5eSRandall Stewart #endif
5808bfc46083SRandall Stewart 
58098f5a8818SKevin Lo int
sctp_input(struct mbuf ** mp,int * offp,int proto SCTP_UNUSED)581082eaf95eSMichael Tuexen sctp_input(struct mbuf **mp, int *offp, int proto SCTP_UNUSED)
5811c54a18d2SRandall Stewart {
58128f5a8818SKevin Lo 	struct mbuf *m;
58138f5a8818SKevin Lo 	int off;
58148f5a8818SKevin Lo 
58158f5a8818SKevin Lo 	m = *mp;
58168f5a8818SKevin Lo 	off = *offp;
58172f9e6db0SMichael Tuexen #if defined(SCTP_MCORE_INPUT) && defined(SMP)
581882eaf95eSMichael Tuexen 	if (mp_ncpus > 1) {
5819bfc46083SRandall Stewart 		struct ip *ip;
5820bfc46083SRandall Stewart 		struct sctphdr *sh;
5821bfc46083SRandall Stewart 		int offset;
5822bfc46083SRandall Stewart 		int cpu_to_use;
582338521fb9SRandall Stewart 		uint32_t flowid, tag;
5824bfc46083SRandall Stewart 
5825457b4b88SMichael Tuexen 		if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
582638521fb9SRandall Stewart 			flowid = m->m_pkthdr.flowid;
582738521fb9SRandall Stewart 		} else {
582838521fb9SRandall Stewart 			/*
582938521fb9SRandall Stewart 			 * No flow id built by lower layers fix it so we
583038521fb9SRandall Stewart 			 * create one.
583138521fb9SRandall Stewart 			 */
5832b1754ad1SMichael Tuexen 			offset = off + sizeof(struct sctphdr);
5833bfc46083SRandall Stewart 			if (SCTP_BUF_LEN(m) < offset) {
5834b1754ad1SMichael Tuexen 				if ((m = m_pullup(m, offset)) == NULL) {
5835bfc46083SRandall Stewart 					SCTP_STAT_INCR(sctps_hdrops);
58368f5a8818SKevin Lo 					return (IPPROTO_DONE);
5837bfc46083SRandall Stewart 				}
5838bfc46083SRandall Stewart 			}
5839b1754ad1SMichael Tuexen 			ip = mtod(m, struct ip *);
5840bfc46083SRandall Stewart 			sh = (struct sctphdr *)((caddr_t)ip + off);
58410071ee5eSRandall Stewart 			tag = htonl(sh->v_tag);
584238521fb9SRandall Stewart 			flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
584338521fb9SRandall Stewart 			m->m_pkthdr.flowid = flowid;
584436ad8372SSepherosa Ziehau 			M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE_HASH);
58450071ee5eSRandall Stewart 		}
584638521fb9SRandall Stewart 		cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
5847bfc46083SRandall Stewart 		sctp_queue_to_mcore(m, off, cpu_to_use);
58488f5a8818SKevin Lo 		return (IPPROTO_DONE);
5849bfc46083SRandall Stewart 	}
5850bfc46083SRandall Stewart #endif
5851bfc46083SRandall Stewart 	sctp_input_with_port(m, off, 0);
58528f5a8818SKevin Lo 	return (IPPROTO_DONE);
5853c54a18d2SRandall Stewart }
5854e6194c2eSMichael Tuexen #endif
5855