xref: /freebsd/sys/netinet/sctp_input.c (revision 44249214d31199623db2bb9de4680dd7364d5256)
1f8829a4aSRandall Stewart /*-
2830d754dSRandall Stewart  * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3807aad63SMichael Tuexen  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4807aad63SMichael Tuexen  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
5f8829a4aSRandall Stewart  *
6f8829a4aSRandall Stewart  * Redistribution and use in source and binary forms, with or without
7f8829a4aSRandall Stewart  * modification, are permitted provided that the following conditions are met:
8f8829a4aSRandall Stewart  *
9f8829a4aSRandall Stewart  * a) Redistributions of source code must retain the above copyright notice,
10f8829a4aSRandall Stewart  *    this list of conditions and the following disclaimer.
11f8829a4aSRandall Stewart  *
12f8829a4aSRandall Stewart  * b) Redistributions in binary form must reproduce the above copyright
13f8829a4aSRandall Stewart  *    notice, this list of conditions and the following disclaimer in
14f8829a4aSRandall Stewart  *    the documentation and/or other materials provided with the distribution.
15f8829a4aSRandall Stewart  *
16f8829a4aSRandall Stewart  * c) Neither the name of Cisco Systems, Inc. nor the names of its
17f8829a4aSRandall Stewart  *    contributors may be used to endorse or promote products derived
18f8829a4aSRandall Stewart  *    from this software without specific prior written permission.
19f8829a4aSRandall Stewart  *
20f8829a4aSRandall Stewart  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21f8829a4aSRandall Stewart  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22f8829a4aSRandall Stewart  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23f8829a4aSRandall Stewart  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24f8829a4aSRandall Stewart  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25f8829a4aSRandall Stewart  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26f8829a4aSRandall Stewart  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27f8829a4aSRandall Stewart  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28f8829a4aSRandall Stewart  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29f8829a4aSRandall Stewart  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30f8829a4aSRandall Stewart  * THE POSSIBILITY OF SUCH DAMAGE.
31f8829a4aSRandall Stewart  */
32f8829a4aSRandall Stewart 
33f8829a4aSRandall Stewart #include <sys/cdefs.h>
34f8829a4aSRandall Stewart __FBSDID("$FreeBSD$");
35f8829a4aSRandall Stewart 
36f8829a4aSRandall Stewart #include <netinet/sctp_os.h>
37f8829a4aSRandall Stewart #include <netinet/sctp_var.h>
3842551e99SRandall Stewart #include <netinet/sctp_sysctl.h>
39f8829a4aSRandall Stewart #include <netinet/sctp_pcb.h>
40f8829a4aSRandall Stewart #include <netinet/sctp_header.h>
41f8829a4aSRandall Stewart #include <netinet/sctputil.h>
42f8829a4aSRandall Stewart #include <netinet/sctp_output.h>
43f8829a4aSRandall Stewart #include <netinet/sctp_input.h>
44f8829a4aSRandall Stewart #include <netinet/sctp_auth.h>
45f8829a4aSRandall Stewart #include <netinet/sctp_indata.h>
46f8829a4aSRandall Stewart #include <netinet/sctp_asconf.h>
47207304d4SRandall Stewart #include <netinet/sctp_bsd_addr.h>
48851b7298SRandall Stewart #include <netinet/sctp_timer.h>
49a99b6783SRandall Stewart #include <netinet/sctp_crc32.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 
55a5d547adSRandall Stewart 
56a5d547adSRandall Stewart 
57f8829a4aSRandall Stewart static void
58f8829a4aSRandall Stewart sctp_stop_all_cookie_timers(struct sctp_tcb *stcb)
59f8829a4aSRandall Stewart {
60f8829a4aSRandall Stewart 	struct sctp_nets *net;
61f8829a4aSRandall Stewart 
62a5d547adSRandall Stewart 	/*
63a5d547adSRandall Stewart 	 * This now not only stops all cookie timers it also stops any INIT
64a5d547adSRandall Stewart 	 * timers as well. This will make sure that the timers are stopped
65a5d547adSRandall Stewart 	 * in all collision cases.
66a5d547adSRandall Stewart 	 */
67f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
68f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
69a5d547adSRandall Stewart 		if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) {
70f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
71f8829a4aSRandall Stewart 			    stcb->sctp_ep,
72f8829a4aSRandall Stewart 			    stcb,
73a5d547adSRandall Stewart 			    net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1);
74a5d547adSRandall Stewart 		} else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) {
75a5d547adSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_INIT,
76a5d547adSRandall Stewart 			    stcb->sctp_ep,
77a5d547adSRandall Stewart 			    stcb,
78a5d547adSRandall Stewart 			    net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2);
79f8829a4aSRandall Stewart 		}
80f8829a4aSRandall Stewart 	}
81f8829a4aSRandall Stewart }
82f8829a4aSRandall Stewart 
83f8829a4aSRandall Stewart /* INIT handler */
84f8829a4aSRandall Stewart static void
85b1754ad1SMichael Tuexen sctp_handle_init(struct mbuf *m, int iphlen, int offset,
86b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
87f30ac432SMichael Tuexen     struct sctp_init_chunk *cp, struct sctp_inpcb *inp,
88ca83f93cSMichael Tuexen     struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_no_unlock,
89457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
90f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
91f8829a4aSRandall Stewart {
92f8829a4aSRandall Stewart 	struct sctp_init *init;
93f8829a4aSRandall Stewart 	struct mbuf *op_err;
94f8829a4aSRandall Stewart 
95ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n",
96dd294dceSMichael Tuexen 	    (void *)stcb);
97d55b0b1bSRandall Stewart 	if (stcb == NULL) {
98d55b0b1bSRandall Stewart 		SCTP_INP_RLOCK(inp);
99d55b0b1bSRandall Stewart 	}
100d68fdc4dSMichael Tuexen 	/* validate length */
101f8829a4aSRandall Stewart 	if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
102ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
103b1754ad1SMichael Tuexen 		sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
104457b4b88SMichael Tuexen 		    mflowtype, mflowid,
105c54a18d2SRandall Stewart 		    vrf_id, port);
106bff64a4dSRandall Stewart 		if (stcb)
107bff64a4dSRandall Stewart 			*abort_no_unlock = 1;
108d55b0b1bSRandall Stewart 		goto outnow;
109f8829a4aSRandall Stewart 	}
110f8829a4aSRandall Stewart 	/* validate parameters */
111d68fdc4dSMichael Tuexen 	init = &cp->init;
112f8829a4aSRandall Stewart 	if (init->initiate_tag == 0) {
113f8829a4aSRandall Stewart 		/* protocol error... send abort */
114ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
115b1754ad1SMichael Tuexen 		sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
116457b4b88SMichael Tuexen 		    mflowtype, mflowid,
117c54a18d2SRandall Stewart 		    vrf_id, port);
118bff64a4dSRandall Stewart 		if (stcb)
119bff64a4dSRandall Stewart 			*abort_no_unlock = 1;
120d55b0b1bSRandall Stewart 		goto outnow;
121f8829a4aSRandall Stewart 	}
122f8829a4aSRandall Stewart 	if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
123f8829a4aSRandall Stewart 		/* invalid parameter... send abort */
124ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
125b1754ad1SMichael Tuexen 		sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
126457b4b88SMichael Tuexen 		    mflowtype, mflowid,
127c54a18d2SRandall Stewart 		    vrf_id, port);
128b54d3a6cSRandall Stewart 		if (stcb)
129b54d3a6cSRandall Stewart 			*abort_no_unlock = 1;
130d55b0b1bSRandall Stewart 		goto outnow;
131f8829a4aSRandall Stewart 	}
132f8829a4aSRandall Stewart 	if (init->num_inbound_streams == 0) {
133f8829a4aSRandall Stewart 		/* protocol error... send abort */
134ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
135b1754ad1SMichael Tuexen 		sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
136457b4b88SMichael Tuexen 		    mflowtype, mflowid,
137c54a18d2SRandall Stewart 		    vrf_id, port);
138bff64a4dSRandall Stewart 		if (stcb)
139bff64a4dSRandall Stewart 			*abort_no_unlock = 1;
140d55b0b1bSRandall Stewart 		goto outnow;
141f8829a4aSRandall Stewart 	}
142f8829a4aSRandall Stewart 	if (init->num_outbound_streams == 0) {
143f8829a4aSRandall Stewart 		/* protocol error... send abort */
144ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
145b1754ad1SMichael Tuexen 		sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
146457b4b88SMichael Tuexen 		    mflowtype, mflowid,
147c54a18d2SRandall Stewart 		    vrf_id, port);
148bff64a4dSRandall Stewart 		if (stcb)
149bff64a4dSRandall Stewart 			*abort_no_unlock = 1;
150d55b0b1bSRandall Stewart 		goto outnow;
151f8829a4aSRandall Stewart 	}
152f8829a4aSRandall Stewart 	if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
153d68fdc4dSMichael Tuexen 	    offset + ntohs(cp->ch.chunk_length))) {
154f8829a4aSRandall Stewart 		/* auth parameter(s) error... send abort */
155ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
156ff1ffd74SMichael Tuexen 		    "Problem with AUTH parameters");
157ff1ffd74SMichael Tuexen 		sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
158457b4b88SMichael Tuexen 		    mflowtype, mflowid,
159f30ac432SMichael Tuexen 		    vrf_id, port);
160bff64a4dSRandall Stewart 		if (stcb)
161bff64a4dSRandall Stewart 			*abort_no_unlock = 1;
162d55b0b1bSRandall Stewart 		goto outnow;
163f8829a4aSRandall Stewart 	}
164d68fdc4dSMichael Tuexen 	/*
165d68fdc4dSMichael Tuexen 	 * We are only accepting if we have a socket with positive
166d68fdc4dSMichael Tuexen 	 * so_qlimit.
167d68fdc4dSMichael Tuexen 	 */
168d68fdc4dSMichael Tuexen 	if ((stcb == NULL) &&
169d68fdc4dSMichael Tuexen 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
170d68fdc4dSMichael Tuexen 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
171d68fdc4dSMichael Tuexen 	    (inp->sctp_socket == NULL) ||
172d68fdc4dSMichael Tuexen 	    (inp->sctp_socket->so_qlimit == 0))) {
173d68fdc4dSMichael Tuexen 		/*
174d68fdc4dSMichael Tuexen 		 * FIX ME ?? What about TCP model and we have a
175d68fdc4dSMichael Tuexen 		 * match/restart case? Actually no fix is needed. the lookup
176d68fdc4dSMichael Tuexen 		 * will always find the existing assoc so stcb would not be
177d68fdc4dSMichael Tuexen 		 * NULL. It may be questionable to do this since we COULD
178d68fdc4dSMichael Tuexen 		 * just send back the INIT-ACK and hope that the app did
179d68fdc4dSMichael Tuexen 		 * accept()'s by the time the COOKIE was sent. But there is
180d68fdc4dSMichael Tuexen 		 * a price to pay for COOKIE generation and I don't want to
181d68fdc4dSMichael Tuexen 		 * pay it on the chance that the app will actually do some
182d68fdc4dSMichael Tuexen 		 * accepts(). The App just looses and should NOT be in this
183d68fdc4dSMichael Tuexen 		 * state :-)
184d68fdc4dSMichael Tuexen 		 */
185c58e60beSMichael Tuexen 		if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) {
186ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
187ff1ffd74SMichael Tuexen 			    "No listener");
188ff1ffd74SMichael Tuexen 			sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
189d089f9b9SMichael Tuexen 			    mflowtype, mflowid, inp->fibnum,
190f30ac432SMichael Tuexen 			    vrf_id, port);
191c58e60beSMichael Tuexen 		}
192d68fdc4dSMichael Tuexen 		goto outnow;
193d68fdc4dSMichael Tuexen 	}
194d68fdc4dSMichael Tuexen 	if ((stcb != NULL) &&
195d68fdc4dSMichael Tuexen 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
196d68fdc4dSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n");
197d68fdc4dSMichael Tuexen 		sctp_send_shutdown_ack(stcb, NULL);
198d68fdc4dSMichael Tuexen 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
199d68fdc4dSMichael Tuexen 	} else {
200ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
201ca83f93cSMichael Tuexen 		sctp_send_initiate_ack(inp, stcb, net, m, iphlen, offset,
202ca83f93cSMichael Tuexen 		    src, dst, sh, cp,
203457b4b88SMichael Tuexen 		    mflowtype, mflowid,
204f30ac432SMichael Tuexen 		    vrf_id, port,
205b201f536SRandall Stewart 		    ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED));
206d68fdc4dSMichael Tuexen 	}
207d55b0b1bSRandall Stewart outnow:
208d55b0b1bSRandall Stewart 	if (stcb == NULL) {
209d55b0b1bSRandall Stewart 		SCTP_INP_RUNLOCK(inp);
210d55b0b1bSRandall Stewart 	}
211f8829a4aSRandall Stewart }
212f8829a4aSRandall Stewart 
213f8829a4aSRandall Stewart /*
214f8829a4aSRandall Stewart  * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
215f8829a4aSRandall Stewart  */
2165bead436SRandall Stewart 
2175bead436SRandall Stewart int
218689e6a5fSMichael Tuexen sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked
219689e6a5fSMichael Tuexen #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
220689e6a5fSMichael Tuexen     SCTP_UNUSED
221689e6a5fSMichael Tuexen #endif
222689e6a5fSMichael Tuexen )
2235bead436SRandall Stewart {
2245bead436SRandall Stewart 	int unsent_data = 0;
225f7a77f6fSMichael Tuexen 	unsigned int i;
226f7a77f6fSMichael Tuexen 	struct sctp_stream_queue_pending *sp;
2275bead436SRandall Stewart 	struct sctp_association *asoc;
2285bead436SRandall Stewart 
2295bead436SRandall Stewart 	/*
2305bead436SRandall Stewart 	 * This function returns the number of streams that have true unsent
2315bead436SRandall Stewart 	 * data on them. Note that as it looks through it will clean up any
2325bead436SRandall Stewart 	 * places that have old data that has been sent but left at top of
2335bead436SRandall Stewart 	 * stream queue.
2345bead436SRandall Stewart 	 */
2355bead436SRandall Stewart 	asoc = &stcb->asoc;
2365bead436SRandall Stewart 	SCTP_TCB_SEND_LOCK(stcb);
237f7a77f6fSMichael Tuexen 	if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
238f7a77f6fSMichael Tuexen 		/* Check to see if some data queued */
239f7a77f6fSMichael Tuexen 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
24052be287eSRandall Stewart 			/* sa_ignore FREED_MEMORY */
241f7a77f6fSMichael Tuexen 			sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue);
242f7a77f6fSMichael Tuexen 			if (sp == NULL) {
243f7a77f6fSMichael Tuexen 				continue;
244f7a77f6fSMichael Tuexen 			}
2455bead436SRandall Stewart 			if ((sp->msg_is_complete) &&
2465bead436SRandall Stewart 			    (sp->length == 0) &&
2475bead436SRandall Stewart 			    (sp->sender_all_done)) {
2485bead436SRandall Stewart 				/*
2495bead436SRandall Stewart 				 * We are doing differed cleanup. Last time
2505bead436SRandall Stewart 				 * through when we took all the data the
2515bead436SRandall Stewart 				 * sender_all_done was not set.
2525bead436SRandall Stewart 				 */
2535bead436SRandall Stewart 				if (sp->put_last_out == 0) {
2545bead436SRandall Stewart 					SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
2555bead436SRandall Stewart 					SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
2565bead436SRandall Stewart 					    sp->sender_all_done,
2575bead436SRandall Stewart 					    sp->length,
2585bead436SRandall Stewart 					    sp->msg_is_complete,
2595bead436SRandall Stewart 					    sp->put_last_out);
2605bead436SRandall Stewart 				}
261f7a77f6fSMichael Tuexen 				atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
262f7a77f6fSMichael Tuexen 				TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next);
2639eea4a2dSMichael Tuexen 				if (sp->net) {
2645bead436SRandall Stewart 					sctp_free_remote_addr(sp->net);
2659eea4a2dSMichael Tuexen 					sp->net = NULL;
2669eea4a2dSMichael Tuexen 				}
2675bead436SRandall Stewart 				if (sp->data) {
2685bead436SRandall Stewart 					sctp_m_freem(sp->data);
2695bead436SRandall Stewart 					sp->data = NULL;
2705bead436SRandall Stewart 				}
271689e6a5fSMichael Tuexen 				sctp_free_a_strmoq(stcb, sp, so_locked);
2725bead436SRandall Stewart 			} else {
2735bead436SRandall Stewart 				unsent_data++;
2744a9ef3f8SMichael Tuexen 				break;
2755bead436SRandall Stewart 			}
2765bead436SRandall Stewart 		}
2775bead436SRandall Stewart 	}
2785bead436SRandall Stewart 	SCTP_TCB_SEND_UNLOCK(stcb);
2795bead436SRandall Stewart 	return (unsent_data);
2805bead436SRandall Stewart }
2815bead436SRandall Stewart 
282f8829a4aSRandall Stewart static int
2837215cc1bSMichael Tuexen sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb)
284f8829a4aSRandall Stewart {
285f8829a4aSRandall Stewart 	struct sctp_init *init;
286f8829a4aSRandall Stewart 	struct sctp_association *asoc;
287f8829a4aSRandall Stewart 	struct sctp_nets *lnet;
288f8829a4aSRandall Stewart 	unsigned int i;
289f8829a4aSRandall Stewart 
290f8829a4aSRandall Stewart 	init = &cp->init;
291f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
292f8829a4aSRandall Stewart 	/* save off parameters */
293f8829a4aSRandall Stewart 	asoc->peer_vtag = ntohl(init->initiate_tag);
294f8829a4aSRandall Stewart 	asoc->peers_rwnd = ntohl(init->a_rwnd);
295493d8e5aSRandall Stewart 	/* init tsn's */
296493d8e5aSRandall Stewart 	asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
297493d8e5aSRandall Stewart 
2989eea4a2dSMichael Tuexen 	if (!TAILQ_EMPTY(&asoc->nets)) {
299f8829a4aSRandall Stewart 		/* update any ssthresh's that may have a default */
300f8829a4aSRandall Stewart 		TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
301f8829a4aSRandall Stewart 			lnet->ssthresh = asoc->peers_rwnd;
302b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
303f8829a4aSRandall Stewart 				sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
30480fefe0aSRandall Stewart 			}
305f8829a4aSRandall Stewart 		}
306f8829a4aSRandall Stewart 	}
3076a91f103SRandall Stewart 	SCTP_TCB_SEND_LOCK(stcb);
308f8829a4aSRandall Stewart 	if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
309f8829a4aSRandall Stewart 		unsigned int newcnt;
310f8829a4aSRandall Stewart 		struct sctp_stream_out *outs;
3114a9ef3f8SMichael Tuexen 		struct sctp_stream_queue_pending *sp, *nsp;
3124a9ef3f8SMichael Tuexen 		struct sctp_tmit_chunk *chk, *nchk;
313f8829a4aSRandall Stewart 
314810ec536SMichael Tuexen 		/* abandon the upper streams */
315f8829a4aSRandall Stewart 		newcnt = ntohs(init->num_inbound_streams);
3164a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
317810ec536SMichael Tuexen 			if (chk->rec.data.stream_number >= newcnt) {
318810ec536SMichael Tuexen 				TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
319810ec536SMichael Tuexen 				asoc->send_queue_cnt--;
320a7ad6026SMichael Tuexen 				if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) {
321a7ad6026SMichael Tuexen 					asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--;
322a7ad6026SMichael Tuexen #ifdef INVARIANTS
323a7ad6026SMichael Tuexen 				} else {
324a7ad6026SMichael Tuexen 					panic("No chunks on the queues for sid %u.", chk->rec.data.stream_number);
325a7ad6026SMichael Tuexen #endif
326a7ad6026SMichael Tuexen 				}
327810ec536SMichael Tuexen 				if (chk->data != NULL) {
328810ec536SMichael Tuexen 					sctp_free_bufspace(stcb, asoc, chk, 1);
3291edc9dbaSMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
3301edc9dbaSMichael Tuexen 					    0, chk, SCTP_SO_NOT_LOCKED);
331810ec536SMichael Tuexen 					if (chk->data) {
332810ec536SMichael Tuexen 						sctp_m_freem(chk->data);
333810ec536SMichael Tuexen 						chk->data = NULL;
334810ec536SMichael Tuexen 					}
335810ec536SMichael Tuexen 				}
336689e6a5fSMichael Tuexen 				sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
337810ec536SMichael Tuexen 				/* sa_ignore FREED_MEMORY */
338810ec536SMichael Tuexen 			}
339810ec536SMichael Tuexen 		}
340f8829a4aSRandall Stewart 		if (asoc->strmout) {
341f8829a4aSRandall Stewart 			for (i = newcnt; i < asoc->pre_open_streams; i++) {
342f8829a4aSRandall Stewart 				outs = &asoc->strmout[i];
3434a9ef3f8SMichael Tuexen 				TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
344810ec536SMichael Tuexen 					TAILQ_REMOVE(&outs->outqueue, sp, next);
345f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
346f8829a4aSRandall Stewart 					sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
3471edc9dbaSMichael Tuexen 					    stcb, 0, sp, SCTP_SO_NOT_LOCKED);
348f8829a4aSRandall Stewart 					if (sp->data) {
349f8829a4aSRandall Stewart 						sctp_m_freem(sp->data);
350f8829a4aSRandall Stewart 						sp->data = NULL;
351f8829a4aSRandall Stewart 					}
3529eea4a2dSMichael Tuexen 					if (sp->net) {
353f8829a4aSRandall Stewart 						sctp_free_remote_addr(sp->net);
354f8829a4aSRandall Stewart 						sp->net = NULL;
3559eea4a2dSMichael Tuexen 					}
356f8829a4aSRandall Stewart 					/* Free the chunk */
357689e6a5fSMichael Tuexen 					sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED);
3583c503c28SRandall Stewart 					/* sa_ignore FREED_MEMORY */
359f8829a4aSRandall Stewart 				}
3607cca1775SRandall Stewart 				outs->state = SCTP_STREAM_CLOSED;
361f8829a4aSRandall Stewart 			}
362f8829a4aSRandall Stewart 		}
363810ec536SMichael Tuexen 		/* cut back the count */
364f8829a4aSRandall Stewart 		asoc->pre_open_streams = newcnt;
365f8829a4aSRandall Stewart 	}
3666a91f103SRandall Stewart 	SCTP_TCB_SEND_UNLOCK(stcb);
3677cca1775SRandall Stewart 	asoc->streamoutcnt = asoc->pre_open_streams;
368fdc4c9d0SMichael Tuexen 	if (asoc->strmout) {
3697cca1775SRandall Stewart 		for (i = 0; i < asoc->streamoutcnt; i++) {
3707cca1775SRandall Stewart 			asoc->strmout[i].state = SCTP_STREAM_OPEN;
3717cca1775SRandall Stewart 		}
372fdc4c9d0SMichael Tuexen 	}
373830d754dSRandall Stewart 	/* EY - nr_sack: initialize highest tsn in nr_mapping_array */
374830d754dSRandall Stewart 	asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
375b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
376f8829a4aSRandall Stewart 		sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
37780fefe0aSRandall Stewart 	}
378f8829a4aSRandall Stewart 	/* This is the next one we expect */
379f8829a4aSRandall Stewart 	asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
380f8829a4aSRandall Stewart 
381f8829a4aSRandall Stewart 	asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
382d6af161aSRandall Stewart 	asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
383493d8e5aSRandall Stewart 
384f8829a4aSRandall Stewart 	asoc->advanced_peer_ack_point = asoc->last_acked_seq;
385f8829a4aSRandall Stewart 	/* open the requested streams */
386207304d4SRandall Stewart 
387f8829a4aSRandall Stewart 	if (asoc->strmin != NULL) {
388f8829a4aSRandall Stewart 		/* Free the old ones */
3896a91f103SRandall Stewart 		for (i = 0; i < asoc->streamincnt; i++) {
390*44249214SRandall Stewart 			sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue);
391*44249214SRandall Stewart 			sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue);
3926a91f103SRandall Stewart 		}
393207304d4SRandall Stewart 		SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
394f8829a4aSRandall Stewart 	}
395ee1ccd92SMichael Tuexen 	if (asoc->max_inbound_streams > ntohs(init->num_outbound_streams)) {
3966a91f103SRandall Stewart 		asoc->streamincnt = ntohs(init->num_outbound_streams);
397ee1ccd92SMichael Tuexen 	} else {
398ee1ccd92SMichael Tuexen 		asoc->streamincnt = asoc->max_inbound_streams;
3996a91f103SRandall Stewart 	}
400f8829a4aSRandall Stewart 	SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
401207304d4SRandall Stewart 	    sizeof(struct sctp_stream_in), SCTP_M_STRMI);
402f8829a4aSRandall Stewart 	if (asoc->strmin == NULL) {
403f8829a4aSRandall Stewart 		/* we didn't get memory for the streams! */
404ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
405f8829a4aSRandall Stewart 		return (-1);
406f8829a4aSRandall Stewart 	}
407f8829a4aSRandall Stewart 	for (i = 0; i < asoc->streamincnt; i++) {
408f8829a4aSRandall Stewart 		asoc->strmin[i].stream_no = i;
409*44249214SRandall Stewart 		asoc->strmin[i].last_sequence_delivered = 0xffffffff;
410f8829a4aSRandall Stewart 		TAILQ_INIT(&asoc->strmin[i].inqueue);
411*44249214SRandall Stewart 		TAILQ_INIT(&asoc->strmin[i].uno_inqueue);
412*44249214SRandall Stewart 		asoc->strmin[i].pd_api_started = 0;
4139a6142d8SRandall Stewart 		asoc->strmin[i].delivery_started = 0;
414f8829a4aSRandall Stewart 	}
415f8829a4aSRandall Stewart 	/*
416f8829a4aSRandall Stewart 	 * load_address_from_init will put the addresses into the
417f8829a4aSRandall Stewart 	 * association when the COOKIE is processed or the INIT-ACK is
418f8829a4aSRandall Stewart 	 * processed. Both types of COOKIE's existing and new call this
419f8829a4aSRandall Stewart 	 * routine. It will remove addresses that are no longer in the
420f8829a4aSRandall Stewart 	 * association (for the restarting case where addresses are
421f8829a4aSRandall Stewart 	 * removed). Up front when the INIT arrives we will discard it if it
422f8829a4aSRandall Stewart 	 * is a restart and new addresses have been added.
423f8829a4aSRandall Stewart 	 */
4243c503c28SRandall Stewart 	/* sa_ignore MEMLEAK */
425f8829a4aSRandall Stewart 	return (0);
426f8829a4aSRandall Stewart }
427f8829a4aSRandall Stewart 
428f8829a4aSRandall Stewart /*
429f8829a4aSRandall Stewart  * INIT-ACK message processing/consumption returns value < 0 on error
430f8829a4aSRandall Stewart  */
431f8829a4aSRandall Stewart static int
432b1754ad1SMichael Tuexen sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
433b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
434f30ac432SMichael Tuexen     struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
435f30ac432SMichael Tuexen     struct sctp_nets *net, int *abort_no_unlock,
436457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
437f30ac432SMichael Tuexen     uint32_t vrf_id)
438f8829a4aSRandall Stewart {
439f8829a4aSRandall Stewart 	struct sctp_association *asoc;
440f8829a4aSRandall Stewart 	struct mbuf *op_err;
441f8829a4aSRandall Stewart 	int retval, abort_flag;
442f8829a4aSRandall Stewart 	uint32_t initack_limit;
443830d754dSRandall Stewart 	int nat_friendly = 0;
444f8829a4aSRandall Stewart 
445f8829a4aSRandall Stewart 	/* First verify that we have no illegal param's */
446f8829a4aSRandall Stewart 	abort_flag = 0;
447f8829a4aSRandall Stewart 
448f8829a4aSRandall Stewart 	op_err = sctp_arethere_unrecognized_parameters(m,
449f8829a4aSRandall Stewart 	    (offset + sizeof(struct sctp_init_chunk)),
450830d754dSRandall Stewart 	    &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly);
451f8829a4aSRandall Stewart 	if (abort_flag) {
452f8829a4aSRandall Stewart 		/* Send an abort and notify peer */
453a2b42326SMichael Tuexen 		sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
454bff64a4dSRandall Stewart 		*abort_no_unlock = 1;
455f8829a4aSRandall Stewart 		return (-1);
456f8829a4aSRandall Stewart 	}
457f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
458830d754dSRandall Stewart 	asoc->peer_supports_nat = (uint8_t) nat_friendly;
459f8829a4aSRandall Stewart 	/* process the peer's parameters in the INIT-ACK */
4607215cc1bSMichael Tuexen 	retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb);
461f8829a4aSRandall Stewart 	if (retval < 0) {
462f8829a4aSRandall Stewart 		return (retval);
463f8829a4aSRandall Stewart 	}
464f8829a4aSRandall Stewart 	initack_limit = offset + ntohs(cp->ch.chunk_length);
465f8829a4aSRandall Stewart 	/* load all addresses */
4667215cc1bSMichael Tuexen 	if ((retval = sctp_load_addresses_from_init(stcb, m,
467b1754ad1SMichael Tuexen 	    (offset + sizeof(struct sctp_init_chunk)), initack_limit,
468b1754ad1SMichael Tuexen 	    src, dst, NULL))) {
469ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
470ff1ffd74SMichael Tuexen 		    "Problem with address parameters");
471ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1,
472ad81507eSRandall Stewart 		    "Load addresses from INIT causes an abort %d\n",
473ad81507eSRandall Stewart 		    retval);
474b1754ad1SMichael Tuexen 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
475ff1ffd74SMichael Tuexen 		    src, dst, sh, op_err,
476457b4b88SMichael Tuexen 		    mflowtype, mflowid,
477f30ac432SMichael Tuexen 		    vrf_id, net->port);
478bff64a4dSRandall Stewart 		*abort_no_unlock = 1;
479f8829a4aSRandall Stewart 		return (-1);
480f8829a4aSRandall Stewart 	}
48118e198d3SRandall Stewart 	/* if the peer doesn't support asconf, flush the asconf queue */
482c79bec9cSMichael Tuexen 	if (asoc->asconf_supported == 0) {
4834a9ef3f8SMichael Tuexen 		struct sctp_asconf_addr *param, *nparam;
48418e198d3SRandall Stewart 
4854a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
4864a9ef3f8SMichael Tuexen 			TAILQ_REMOVE(&asoc->asconf_queue, param, next);
4874a9ef3f8SMichael Tuexen 			SCTP_FREE(param, SCTP_M_ASC_ADDR);
48818e198d3SRandall Stewart 		}
48918e198d3SRandall Stewart 	}
490f8829a4aSRandall Stewart 	stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
491f8829a4aSRandall Stewart 	    stcb->asoc.local_hmacs);
492f8829a4aSRandall Stewart 	if (op_err) {
493f8829a4aSRandall Stewart 		sctp_queue_op_err(stcb, op_err);
494f8829a4aSRandall Stewart 		/* queuing will steal away the mbuf chain to the out queue */
495f8829a4aSRandall Stewart 		op_err = NULL;
496f8829a4aSRandall Stewart 	}
497f8829a4aSRandall Stewart 	/* extract the cookie and queue it to "echo" it back... */
498b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
499c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
500c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
501c4739e2fSRandall Stewart 		    0,
502c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INPUT,
503c4739e2fSRandall Stewart 		    __LINE__);
504c4739e2fSRandall Stewart 	}
505f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
506f8829a4aSRandall Stewart 	net->error_count = 0;
507f8829a4aSRandall Stewart 
508f8829a4aSRandall Stewart 	/*
509f8829a4aSRandall Stewart 	 * Cancel the INIT timer, We do this first before queueing the
510f8829a4aSRandall Stewart 	 * cookie. We always cancel at the primary to assue that we are
511f8829a4aSRandall Stewart 	 * canceling the timer started by the INIT which always goes to the
512f8829a4aSRandall Stewart 	 * primary.
513f8829a4aSRandall Stewart 	 */
514f8829a4aSRandall Stewart 	sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
515b7d130beSMichael Tuexen 	    asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
516a5d547adSRandall Stewart 
517a5d547adSRandall Stewart 	/* calculate the RTO */
518899288aeSRandall Stewart 	net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy,
519f79aab18SRandall Stewart 	    SCTP_RTT_FROM_NON_DATA);
520f8829a4aSRandall Stewart 	retval = sctp_send_cookie_echo(m, offset, stcb, net);
521f8829a4aSRandall Stewart 	if (retval < 0) {
522f8829a4aSRandall Stewart 		/*
523f8829a4aSRandall Stewart 		 * No cookie, we probably should send a op error. But in any
524f8829a4aSRandall Stewart 		 * case if there is no cookie in the INIT-ACK, we can
525f8829a4aSRandall Stewart 		 * abandon the peer, its broke.
526f8829a4aSRandall Stewart 		 */
527f8829a4aSRandall Stewart 		if (retval == -3) {
5286802b090SMichael Tuexen 			uint16_t len;
529f8829a4aSRandall Stewart 
5306802b090SMichael Tuexen 			len = (uint16_t) (sizeof(struct sctp_error_missing_param) + sizeof(uint16_t));
53186eda749SMichael Tuexen 			/* We abort with an error of missing mandatory param */
53286eda749SMichael Tuexen 			op_err = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
53386eda749SMichael Tuexen 			if (op_err != NULL) {
53486eda749SMichael Tuexen 				struct sctp_error_missing_param *cause;
53586eda749SMichael Tuexen 
53686eda749SMichael Tuexen 				SCTP_BUF_LEN(op_err) = len;
53786eda749SMichael Tuexen 				cause = mtod(op_err, struct sctp_error_missing_param *);
538f8829a4aSRandall Stewart 				/* Subtract the reserved param */
53986eda749SMichael Tuexen 				cause->cause.code = htons(SCTP_CAUSE_MISSING_PARAM);
54086eda749SMichael Tuexen 				cause->cause.length = htons(len);
54186eda749SMichael Tuexen 				cause->num_missing_params = htonl(1);
54286eda749SMichael Tuexen 				cause->type[0] = htons(SCTP_STATE_COOKIE);
543f8829a4aSRandall Stewart 			}
544f8829a4aSRandall Stewart 			sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
545b1754ad1SMichael Tuexen 			    src, dst, sh, op_err,
546457b4b88SMichael Tuexen 			    mflowtype, mflowid,
547f30ac432SMichael Tuexen 			    vrf_id, net->port);
548bff64a4dSRandall Stewart 			*abort_no_unlock = 1;
549f8829a4aSRandall Stewart 		}
550f8829a4aSRandall Stewart 		return (retval);
551f8829a4aSRandall Stewart 	}
552f8829a4aSRandall Stewart 	return (0);
553f8829a4aSRandall Stewart }
554f8829a4aSRandall Stewart 
555f8829a4aSRandall Stewart static void
556f8829a4aSRandall Stewart sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
557f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net)
558f8829a4aSRandall Stewart {
55924aaac8dSMichael Tuexen 	union sctp_sockstore store;
56052129fcdSRandall Stewart 	struct sctp_nets *r_net, *f_net;
561f8829a4aSRandall Stewart 	struct timeval tv;
562d55b0b1bSRandall Stewart 	int req_prim = 0;
563ca85e948SMichael Tuexen 	uint16_t old_error_counter;
564f8829a4aSRandall Stewart 
565f8829a4aSRandall Stewart 	if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
566f8829a4aSRandall Stewart 		/* Invalid length */
567f8829a4aSRandall Stewart 		return;
568f8829a4aSRandall Stewart 	}
569f8829a4aSRandall Stewart 	memset(&store, 0, sizeof(store));
570e6194c2eSMichael Tuexen 	switch (cp->heartbeat.hb_info.addr_family) {
571e6194c2eSMichael Tuexen #ifdef INET
572e6194c2eSMichael Tuexen 	case AF_INET:
573e6194c2eSMichael Tuexen 		if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
57424aaac8dSMichael Tuexen 			store.sin.sin_family = cp->heartbeat.hb_info.addr_family;
57524aaac8dSMichael Tuexen 			store.sin.sin_len = cp->heartbeat.hb_info.addr_len;
57624aaac8dSMichael Tuexen 			store.sin.sin_port = stcb->rport;
57724aaac8dSMichael Tuexen 			memcpy(&store.sin.sin_addr, cp->heartbeat.hb_info.address,
57824aaac8dSMichael Tuexen 			    sizeof(store.sin.sin_addr));
579e6194c2eSMichael Tuexen 		} else {
580e6194c2eSMichael Tuexen 			return;
581e6194c2eSMichael Tuexen 		}
582e6194c2eSMichael Tuexen 		break;
583e6194c2eSMichael Tuexen #endif
584e6194c2eSMichael Tuexen #ifdef INET6
585e6194c2eSMichael Tuexen 	case AF_INET6:
586e6194c2eSMichael Tuexen 		if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
58724aaac8dSMichael Tuexen 			store.sin6.sin6_family = cp->heartbeat.hb_info.addr_family;
58824aaac8dSMichael Tuexen 			store.sin6.sin6_len = cp->heartbeat.hb_info.addr_len;
58924aaac8dSMichael Tuexen 			store.sin6.sin6_port = stcb->rport;
59023602b60SMichael Tuexen 			memcpy(&store.sin6.sin6_addr, cp->heartbeat.hb_info.address, sizeof(struct in6_addr));
591f8829a4aSRandall Stewart 		} else {
592f8829a4aSRandall Stewart 			return;
593f8829a4aSRandall Stewart 		}
594e6194c2eSMichael Tuexen 		break;
595e6194c2eSMichael Tuexen #endif
596e6194c2eSMichael Tuexen 	default:
597e6194c2eSMichael Tuexen 		return;
598e6194c2eSMichael Tuexen 	}
59924aaac8dSMichael Tuexen 	r_net = sctp_findnet(stcb, &store.sa);
600f8829a4aSRandall Stewart 	if (r_net == NULL) {
601ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
602f8829a4aSRandall Stewart 		return;
603f8829a4aSRandall Stewart 	}
604f8829a4aSRandall Stewart 	if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
605f8829a4aSRandall Stewart 	    (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
606f8829a4aSRandall Stewart 	    (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
607f8829a4aSRandall Stewart 		/*
608f8829a4aSRandall Stewart 		 * If the its a HB and it's random value is correct when can
609f8829a4aSRandall Stewart 		 * confirm the destination.
610f8829a4aSRandall Stewart 		 */
611f8829a4aSRandall Stewart 		r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
61242551e99SRandall Stewart 		if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
61342551e99SRandall Stewart 			stcb->asoc.primary_destination = r_net;
61442551e99SRandall Stewart 			r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
61552129fcdSRandall Stewart 			f_net = TAILQ_FIRST(&stcb->asoc.nets);
61652129fcdSRandall Stewart 			if (f_net != r_net) {
61742551e99SRandall Stewart 				/*
61842551e99SRandall Stewart 				 * first one on the list is NOT the primary
61942551e99SRandall Stewart 				 * sctp_cmpaddr() is much more efficent if
62042551e99SRandall Stewart 				 * the primary is the first on the list,
62142551e99SRandall Stewart 				 * make it so.
62242551e99SRandall Stewart 				 */
62352129fcdSRandall Stewart 				TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
62452129fcdSRandall Stewart 				TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
62542551e99SRandall Stewart 			}
626d55b0b1bSRandall Stewart 			req_prim = 1;
62742551e99SRandall Stewart 		}
628f8829a4aSRandall Stewart 		sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
629ceaad40aSRandall Stewart 		    stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
630b7d130beSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb,
631b7d130beSMichael Tuexen 		    r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
632ca85e948SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
633f8829a4aSRandall Stewart 	}
634ca85e948SMichael Tuexen 	old_error_counter = r_net->error_count;
635f8829a4aSRandall Stewart 	r_net->error_count = 0;
636f8829a4aSRandall Stewart 	r_net->hb_responded = 1;
637f8829a4aSRandall Stewart 	tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
638f8829a4aSRandall Stewart 	tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
639f8829a4aSRandall Stewart 	/* Now lets do a RTO with this */
640899288aeSRandall Stewart 	r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy,
641f79aab18SRandall Stewart 	    SCTP_RTT_FROM_NON_DATA);
642ca85e948SMichael Tuexen 	if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) {
643ca85e948SMichael Tuexen 		r_net->dest_state |= SCTP_ADDR_REACHABLE;
644ca85e948SMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
6454b1f78e1SMichael Tuexen 		    0, (void *)r_net, SCTP_SO_NOT_LOCKED);
646ca85e948SMichael Tuexen 	}
647ca85e948SMichael Tuexen 	if (r_net->dest_state & SCTP_ADDR_PF) {
648ca85e948SMichael Tuexen 		r_net->dest_state &= ~SCTP_ADDR_PF;
649ca85e948SMichael Tuexen 		stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
650ca85e948SMichael Tuexen 	}
651ca85e948SMichael Tuexen 	if (old_error_counter > 0) {
652b7d130beSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
653b7d130beSMichael Tuexen 		    stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_5);
654ca85e948SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
655ca85e948SMichael Tuexen 	}
656ca85e948SMichael Tuexen 	if (r_net == stcb->asoc.primary_destination) {
657ca85e948SMichael Tuexen 		if (stcb->asoc.alternate) {
658ca85e948SMichael Tuexen 			/* release the alternate, primary is good */
659ca85e948SMichael Tuexen 			sctp_free_remote_addr(stcb->asoc.alternate);
660ca85e948SMichael Tuexen 			stcb->asoc.alternate = NULL;
661ca85e948SMichael Tuexen 		}
662ca85e948SMichael Tuexen 	}
663d55b0b1bSRandall Stewart 	/* Mobility adaptation */
664d55b0b1bSRandall Stewart 	if (req_prim) {
665d55b0b1bSRandall Stewart 		if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
666d55b0b1bSRandall Stewart 		    SCTP_MOBILITY_BASE) ||
667d55b0b1bSRandall Stewart 		    sctp_is_mobility_feature_on(stcb->sctp_ep,
668d55b0b1bSRandall Stewart 		    SCTP_MOBILITY_FASTHANDOFF)) &&
669d55b0b1bSRandall Stewart 		    sctp_is_mobility_feature_on(stcb->sctp_ep,
670d55b0b1bSRandall Stewart 		    SCTP_MOBILITY_PRIM_DELETED)) {
671d55b0b1bSRandall Stewart 
672b7d130beSMichael Tuexen 			sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED,
673b7d130beSMichael Tuexen 			    stcb->sctp_ep, stcb, NULL,
674b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
675d55b0b1bSRandall Stewart 			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
676d55b0b1bSRandall Stewart 			    SCTP_MOBILITY_FASTHANDOFF)) {
677d55b0b1bSRandall Stewart 				sctp_assoc_immediate_retrans(stcb,
678d55b0b1bSRandall Stewart 				    stcb->asoc.primary_destination);
679d55b0b1bSRandall Stewart 			}
680d55b0b1bSRandall Stewart 			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
681d55b0b1bSRandall Stewart 			    SCTP_MOBILITY_BASE)) {
6829eea4a2dSMichael Tuexen 				sctp_move_chunks_from_net(stcb,
6839eea4a2dSMichael Tuexen 				    stcb->asoc.deleted_primary);
684d55b0b1bSRandall Stewart 			}
685d55b0b1bSRandall Stewart 			sctp_delete_prim_timer(stcb->sctp_ep, stcb,
686d55b0b1bSRandall Stewart 			    stcb->asoc.deleted_primary);
687d55b0b1bSRandall Stewart 		}
688d55b0b1bSRandall Stewart 	}
689f8829a4aSRandall Stewart }
690f8829a4aSRandall Stewart 
691830d754dSRandall Stewart static int
692830d754dSRandall Stewart sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
693830d754dSRandall Stewart {
694830d754dSRandall Stewart 	/*
695830d754dSRandall Stewart 	 * return 0 means we want you to proceed with the abort non-zero
696830d754dSRandall Stewart 	 * means no abort processing
697830d754dSRandall Stewart 	 */
698830d754dSRandall Stewart 	struct sctpasochead *head;
699830d754dSRandall Stewart 
700830d754dSRandall Stewart 	if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
701830d754dSRandall Stewart 		/* generate a new vtag and send init */
702830d754dSRandall Stewart 		LIST_REMOVE(stcb, sctp_asocs);
703830d754dSRandall Stewart 		stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
704830d754dSRandall Stewart 		head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
705830d754dSRandall Stewart 		/*
706830d754dSRandall Stewart 		 * put it in the bucket in the vtag hash of assoc's for the
707830d754dSRandall Stewart 		 * system
708830d754dSRandall Stewart 		 */
709830d754dSRandall Stewart 		LIST_INSERT_HEAD(head, stcb, sctp_asocs);
710830d754dSRandall Stewart 		sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
711830d754dSRandall Stewart 		return (1);
712830d754dSRandall Stewart 	}
713830d754dSRandall Stewart 	if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
714830d754dSRandall Stewart 		/*
715830d754dSRandall Stewart 		 * treat like a case where the cookie expired i.e.: - dump
716830d754dSRandall Stewart 		 * current cookie. - generate a new vtag. - resend init.
717830d754dSRandall Stewart 		 */
718830d754dSRandall Stewart 		/* generate a new vtag and send init */
719830d754dSRandall Stewart 		LIST_REMOVE(stcb, sctp_asocs);
720830d754dSRandall Stewart 		stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED;
721830d754dSRandall Stewart 		stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT;
722830d754dSRandall Stewart 		sctp_stop_all_cookie_timers(stcb);
723830d754dSRandall Stewart 		sctp_toss_old_cookies(stcb, &stcb->asoc);
724830d754dSRandall Stewart 		stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
725830d754dSRandall Stewart 		head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
726830d754dSRandall Stewart 		/*
727830d754dSRandall Stewart 		 * put it in the bucket in the vtag hash of assoc's for the
728830d754dSRandall Stewart 		 * system
729830d754dSRandall Stewart 		 */
730830d754dSRandall Stewart 		LIST_INSERT_HEAD(head, stcb, sctp_asocs);
731830d754dSRandall Stewart 		sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
732830d754dSRandall Stewart 		return (1);
733830d754dSRandall Stewart 	}
734830d754dSRandall Stewart 	return (0);
735830d754dSRandall Stewart }
736830d754dSRandall Stewart 
737830d754dSRandall Stewart static int
738830d754dSRandall Stewart sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
739830d754dSRandall Stewart     struct sctp_nets *net)
740830d754dSRandall Stewart {
741830d754dSRandall Stewart 	/*
742830d754dSRandall Stewart 	 * return 0 means we want you to proceed with the abort non-zero
743830d754dSRandall Stewart 	 * means no abort processing
744830d754dSRandall Stewart 	 */
745c79bec9cSMichael Tuexen 	if (stcb->asoc.auth_supported == 0) {
746830d754dSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
747830d754dSRandall Stewart 		return (0);
748830d754dSRandall Stewart 	}
749830d754dSRandall Stewart 	sctp_asconf_send_nat_state_update(stcb, net);
750830d754dSRandall Stewart 	return (1);
751830d754dSRandall Stewart }
752830d754dSRandall Stewart 
753830d754dSRandall Stewart 
754f8829a4aSRandall Stewart static void
755a2b42326SMichael Tuexen sctp_handle_abort(struct sctp_abort_chunk *abort,
756f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net)
757f8829a4aSRandall Stewart {
758ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
759ceaad40aSRandall Stewart 	struct socket *so;
760ceaad40aSRandall Stewart 
761ceaad40aSRandall Stewart #endif
762830d754dSRandall Stewart 	uint16_t len;
763a2b42326SMichael Tuexen 	uint16_t error;
764ceaad40aSRandall Stewart 
765ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
766f8829a4aSRandall Stewart 	if (stcb == NULL)
767f8829a4aSRandall Stewart 		return;
768f8829a4aSRandall Stewart 
769a2b42326SMichael Tuexen 	len = ntohs(abort->ch.chunk_length);
770830d754dSRandall Stewart 	if (len > sizeof(struct sctp_chunkhdr)) {
771830d754dSRandall Stewart 		/*
772830d754dSRandall Stewart 		 * Need to check the cause codes for our two magic nat
773830d754dSRandall Stewart 		 * aborts which don't kill the assoc necessarily.
774830d754dSRandall Stewart 		 */
77586eda749SMichael Tuexen 		struct sctp_gen_error_cause *cause;
776830d754dSRandall Stewart 
77786eda749SMichael Tuexen 		cause = (struct sctp_gen_error_cause *)(abort + 1);
77886eda749SMichael Tuexen 		error = ntohs(cause->code);
779a2b42326SMichael Tuexen 		if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) {
780830d754dSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
781a2b42326SMichael Tuexen 			    abort->ch.chunk_flags);
782830d754dSRandall Stewart 			if (sctp_handle_nat_colliding_state(stcb)) {
783830d754dSRandall Stewart 				return;
784830d754dSRandall Stewart 			}
785a2b42326SMichael Tuexen 		} else if (error == SCTP_CAUSE_NAT_MISSING_STATE) {
786830d754dSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
787a2b42326SMichael Tuexen 			    abort->ch.chunk_flags);
788830d754dSRandall Stewart 			if (sctp_handle_nat_missing_state(stcb, net)) {
789830d754dSRandall Stewart 				return;
790830d754dSRandall Stewart 			}
791830d754dSRandall Stewart 		}
792a2b42326SMichael Tuexen 	} else {
793a2b42326SMichael Tuexen 		error = 0;
794830d754dSRandall Stewart 	}
795f8829a4aSRandall Stewart 	/* stop any receive timers */
796b7d130beSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net,
797b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_7);
798f8829a4aSRandall Stewart 	/* notify user of the abort and clean up... */
799410a3b1eSMichael Tuexen 	sctp_abort_notification(stcb, 1, error, abort, SCTP_SO_NOT_LOCKED);
800f8829a4aSRandall Stewart 	/* free the tcb */
801f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_aborted);
802f8829a4aSRandall Stewart 	if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
803f8829a4aSRandall Stewart 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
804f8829a4aSRandall Stewart 		SCTP_STAT_DECR_GAUGE32(sctps_currestab);
805f8829a4aSRandall Stewart 	}
806f1f73e57SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
807f1f73e57SRandall Stewart 	sctp_print_out_track_log(stcb);
808f1f73e57SRandall Stewart #endif
809ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
810ceaad40aSRandall Stewart 	so = SCTP_INP_SO(stcb->sctp_ep);
811ceaad40aSRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, 1);
812ceaad40aSRandall Stewart 	SCTP_TCB_UNLOCK(stcb);
813ceaad40aSRandall Stewart 	SCTP_SOCKET_LOCK(so, 1);
814ceaad40aSRandall Stewart 	SCTP_TCB_LOCK(stcb);
815ceaad40aSRandall Stewart 	atomic_subtract_int(&stcb->asoc.refcnt, 1);
816ceaad40aSRandall Stewart #endif
8172afb3e84SRandall Stewart 	stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
818c4739e2fSRandall Stewart 	(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
819b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
820ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
821ceaad40aSRandall Stewart 	SCTP_SOCKET_UNLOCK(so, 1);
822ceaad40aSRandall Stewart #endif
823ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
824f8829a4aSRandall Stewart }
825f8829a4aSRandall Stewart 
826f8829a4aSRandall Stewart static void
827ca85e948SMichael Tuexen sctp_start_net_timers(struct sctp_tcb *stcb)
828ca85e948SMichael Tuexen {
829ca85e948SMichael Tuexen 	uint32_t cnt_hb_sent;
830ca85e948SMichael Tuexen 	struct sctp_nets *net;
831ca85e948SMichael Tuexen 
832ca85e948SMichael Tuexen 	cnt_hb_sent = 0;
833ca85e948SMichael Tuexen 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
834ca85e948SMichael Tuexen 		/*
835ca85e948SMichael Tuexen 		 * For each network start: 1) A pmtu timer. 2) A HB timer 3)
836ca85e948SMichael Tuexen 		 * If the dest in unconfirmed send a hb as well if under
837ca85e948SMichael Tuexen 		 * max_hb_burst have been sent.
838ca85e948SMichael Tuexen 		 */
839ca85e948SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
840ca85e948SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
841ca85e948SMichael Tuexen 		if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
842ca85e948SMichael Tuexen 		    (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
843ca85e948SMichael Tuexen 			sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
844ca85e948SMichael Tuexen 			cnt_hb_sent++;
845ca85e948SMichael Tuexen 		}
846ca85e948SMichael Tuexen 	}
847ca85e948SMichael Tuexen 	if (cnt_hb_sent) {
848ca85e948SMichael Tuexen 		sctp_chunk_output(stcb->sctp_ep, stcb,
849ca85e948SMichael Tuexen 		    SCTP_OUTPUT_FROM_COOKIE_ACK,
850ca85e948SMichael Tuexen 		    SCTP_SO_NOT_LOCKED);
851ca85e948SMichael Tuexen 	}
852ca85e948SMichael Tuexen }
853ca85e948SMichael Tuexen 
854ca85e948SMichael Tuexen 
855ca85e948SMichael Tuexen static void
856f8829a4aSRandall Stewart sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
857f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
858f8829a4aSRandall Stewart {
859f8829a4aSRandall Stewart 	struct sctp_association *asoc;
860f8829a4aSRandall Stewart 	int some_on_streamwheel;
861aa1cfca9SMichael Tuexen 	int old_state;
862f8829a4aSRandall Stewart 
863ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
864ceaad40aSRandall Stewart 	struct socket *so;
865ceaad40aSRandall Stewart 
866ceaad40aSRandall Stewart #endif
867ceaad40aSRandall Stewart 
868ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
869ad81507eSRandall Stewart 	    "sctp_handle_shutdown: handling SHUTDOWN\n");
870f8829a4aSRandall Stewart 	if (stcb == NULL)
871f8829a4aSRandall Stewart 		return;
872a5d547adSRandall Stewart 	asoc = &stcb->asoc;
873a5d547adSRandall Stewart 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
874a5d547adSRandall Stewart 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
875f8829a4aSRandall Stewart 		return;
876f8829a4aSRandall Stewart 	}
877f8829a4aSRandall Stewart 	if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
878f8829a4aSRandall Stewart 		/* Shutdown NOT the expected size */
879f8829a4aSRandall Stewart 		return;
880aa1cfca9SMichael Tuexen 	}
881aa1cfca9SMichael Tuexen 	old_state = SCTP_GET_STATE(asoc);
8827215cc1bSMichael Tuexen 	sctp_update_acked(stcb, cp, abort_flag);
8837e6206afSMichael Tuexen 	if (*abort_flag) {
8847e6206afSMichael Tuexen 		return;
8857e6206afSMichael Tuexen 	}
886a5d547adSRandall Stewart 	if (asoc->control_pdapi) {
887f8829a4aSRandall Stewart 		/*
888f8829a4aSRandall Stewart 		 * With a normal shutdown we assume the end of last record.
889f8829a4aSRandall Stewart 		 */
890f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
891*44249214SRandall Stewart 		if (asoc->control_pdapi->on_strm_q) {
892*44249214SRandall Stewart 			struct sctp_stream_in *strm;
893*44249214SRandall Stewart 
894*44249214SRandall Stewart 			strm = &asoc->strmin[asoc->control_pdapi->sinfo_stream];
895*44249214SRandall Stewart 			if (asoc->control_pdapi->on_strm_q == SCTP_ON_UNORDERED) {
896*44249214SRandall Stewart 				/* Unordered */
897*44249214SRandall Stewart 				TAILQ_REMOVE(&strm->uno_inqueue, asoc->control_pdapi, next_instrm);
898*44249214SRandall Stewart 				asoc->control_pdapi->on_strm_q = 0;
899*44249214SRandall Stewart 			} else if (asoc->control_pdapi->on_strm_q == SCTP_ON_ORDERED) {
900*44249214SRandall Stewart 				/* Ordered */
901*44249214SRandall Stewart 				TAILQ_REMOVE(&strm->inqueue, asoc->control_pdapi, next_instrm);
902*44249214SRandall Stewart 				asoc->control_pdapi->on_strm_q = 0;
903*44249214SRandall Stewart 			} else {
904*44249214SRandall Stewart 				panic("Unknown state on ctrl:%p on_strm_q:%d",
905*44249214SRandall Stewart 				    asoc->control_pdapi,
906*44249214SRandall Stewart 				    asoc->control_pdapi->on_strm_q);
907*44249214SRandall Stewart 			}
908*44249214SRandall Stewart 		}
909*44249214SRandall Stewart 		printf("%s:%d End added to ctl:%p (%d)\n",
910*44249214SRandall Stewart 		    __FUNCTION__,
911*44249214SRandall Stewart 		    __LINE__,
912*44249214SRandall Stewart 		    asoc->control_pdapi,
913*44249214SRandall Stewart 		    asoc->control_pdapi->on_strm_q);
914a5d547adSRandall Stewart 		asoc->control_pdapi->end_added = 1;
915a5d547adSRandall Stewart 		asoc->control_pdapi->pdapi_aborted = 1;
916a5d547adSRandall Stewart 		asoc->control_pdapi = NULL;
917f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
918ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
919ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
920ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
921ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
922ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
923ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
924ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
925ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
926ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
927ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
928ceaad40aSRandall Stewart 			return;
929ceaad40aSRandall Stewart 		}
930ceaad40aSRandall Stewart #endif
931828318e1SMichael Tuexen 		if (stcb->sctp_socket) {
93250cec919SRandall Stewart 			sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
933828318e1SMichael Tuexen 		}
934ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
935ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
936ceaad40aSRandall Stewart #endif
937f8829a4aSRandall Stewart 	}
938f8829a4aSRandall Stewart 	/* goto SHUTDOWN_RECEIVED state to block new requests */
939f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
940f8829a4aSRandall Stewart 		if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
941d61a0ae0SRandall Stewart 		    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
942f8829a4aSRandall Stewart 		    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
943c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED);
944b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
945f8829a4aSRandall Stewart 			/*
946f8829a4aSRandall Stewart 			 * notify upper layer that peer has initiated a
947f8829a4aSRandall Stewart 			 * shutdown
948f8829a4aSRandall Stewart 			 */
949ceaad40aSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
950f8829a4aSRandall Stewart 
951f8829a4aSRandall Stewart 			/* reset time */
9526e55db54SRandall Stewart 			(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
953f8829a4aSRandall Stewart 		}
954f8829a4aSRandall Stewart 	}
955f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
956f8829a4aSRandall Stewart 		/*
957f8829a4aSRandall Stewart 		 * stop the shutdown timer, since we WILL move to
958f8829a4aSRandall Stewart 		 * SHUTDOWN-ACK-SENT.
959f8829a4aSRandall Stewart 		 */
960b7d130beSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
961b7d130beSMichael Tuexen 		    net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
962f8829a4aSRandall Stewart 	}
9635bead436SRandall Stewart 	/* Now is there unsent data on a stream somewhere? */
964689e6a5fSMichael Tuexen 	some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
965f8829a4aSRandall Stewart 
966f8829a4aSRandall Stewart 	if (!TAILQ_EMPTY(&asoc->send_queue) ||
967f8829a4aSRandall Stewart 	    !TAILQ_EMPTY(&asoc->sent_queue) ||
968f8829a4aSRandall Stewart 	    some_on_streamwheel) {
969f8829a4aSRandall Stewart 		/* By returning we will push more data out */
970f8829a4aSRandall Stewart 		return;
971f8829a4aSRandall Stewart 	} else {
972f8829a4aSRandall Stewart 		/* no outstanding data to send, so move on... */
973f8829a4aSRandall Stewart 		/* send SHUTDOWN-ACK */
974f8829a4aSRandall Stewart 		/* move to SHUTDOWN-ACK-SENT state */
975f42a358aSRandall Stewart 		if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
976f42a358aSRandall Stewart 		    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
977f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
978f8829a4aSRandall Stewart 		}
979b201f536SRandall Stewart 		SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
980aa1cfca9SMichael Tuexen 		if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
981aa1cfca9SMichael Tuexen 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
98212af6654SMichael Tuexen 			sctp_stop_timers_for_shutdown(stcb);
983c39cfa1fSMichael Tuexen 			sctp_send_shutdown_ack(stcb, net);
984aa1cfca9SMichael Tuexen 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
985aa1cfca9SMichael Tuexen 			    stcb->sctp_ep, stcb, net);
986aa1cfca9SMichael Tuexen 		} else if (old_state == SCTP_STATE_SHUTDOWN_ACK_SENT) {
987aa1cfca9SMichael Tuexen 			sctp_send_shutdown_ack(stcb, net);
988aa1cfca9SMichael Tuexen 		}
989f8829a4aSRandall Stewart 	}
990f8829a4aSRandall Stewart }
991f8829a4aSRandall Stewart 
992f8829a4aSRandall Stewart static void
9937215cc1bSMichael Tuexen sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
9947b470fc3SMichael Tuexen     struct sctp_tcb *stcb,
9957b470fc3SMichael Tuexen     struct sctp_nets *net)
996f8829a4aSRandall Stewart {
997f8829a4aSRandall Stewart 	struct sctp_association *asoc;
998f8829a4aSRandall Stewart 
999ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1000ceaad40aSRandall Stewart 	struct socket *so;
1001ceaad40aSRandall Stewart 
1002ceaad40aSRandall Stewart 	so = SCTP_INP_SO(stcb->sctp_ep);
1003ceaad40aSRandall Stewart #endif
1004ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
1005ad81507eSRandall Stewart 	    "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
1006f8829a4aSRandall Stewart 	if (stcb == NULL)
1007f8829a4aSRandall Stewart 		return;
1008f8829a4aSRandall Stewart 
1009f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
1010f8829a4aSRandall Stewart 	/* process according to association state */
10117b470fc3SMichael Tuexen 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
10127b470fc3SMichael Tuexen 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
10137b470fc3SMichael Tuexen 		/* unexpected SHUTDOWN-ACK... do OOTB handling... */
10147b470fc3SMichael Tuexen 		sctp_send_shutdown_complete(stcb, net, 1);
10157b470fc3SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
10167b470fc3SMichael Tuexen 		return;
10177b470fc3SMichael Tuexen 	}
1018f8829a4aSRandall Stewart 	if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
1019f8829a4aSRandall Stewart 	    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
1020f8829a4aSRandall Stewart 		/* unexpected SHUTDOWN-ACK... so ignore... */
1021f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
1022f8829a4aSRandall Stewart 		return;
1023f8829a4aSRandall Stewart 	}
1024a5d547adSRandall Stewart 	if (asoc->control_pdapi) {
1025f8829a4aSRandall Stewart 		/*
1026f8829a4aSRandall Stewart 		 * With a normal shutdown we assume the end of last record.
1027f8829a4aSRandall Stewart 		 */
1028f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
1029*44249214SRandall Stewart 		printf("%s:%d End added to ctl:%p (%d)\n",
1030*44249214SRandall Stewart 		    __FUNCTION__,
1031*44249214SRandall Stewart 		    __LINE__,
1032*44249214SRandall Stewart 		    asoc->control_pdapi,
1033*44249214SRandall Stewart 		    asoc->control_pdapi->on_strm_q);
1034a5d547adSRandall Stewart 		asoc->control_pdapi->end_added = 1;
1035a5d547adSRandall Stewart 		asoc->control_pdapi->pdapi_aborted = 1;
1036a5d547adSRandall Stewart 		asoc->control_pdapi = NULL;
1037f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
1038ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1039ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
1040ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
1041ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
1042ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
1043ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
1044ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1045ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
1046ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
1047ceaad40aSRandall Stewart 			return;
1048ceaad40aSRandall Stewart 		}
1049ceaad40aSRandall Stewart #endif
105050cec919SRandall Stewart 		sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1051ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1052ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
1053ceaad40aSRandall Stewart #endif
1054f8829a4aSRandall Stewart 	}
105556f778aaSMichael Tuexen #ifdef INVARIANTS
1056f8829a4aSRandall Stewart 	if (!TAILQ_EMPTY(&asoc->send_queue) ||
1057f8829a4aSRandall Stewart 	    !TAILQ_EMPTY(&asoc->sent_queue) ||
1058f7a77f6fSMichael Tuexen 	    !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
105956f778aaSMichael Tuexen 		panic("Queues are not empty when handling SHUTDOWN-ACK");
1060f8829a4aSRandall Stewart 	}
106156f778aaSMichael Tuexen #endif
1062f8829a4aSRandall Stewart 	/* stop the timer */
1063b7d130beSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net,
1064b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
1065f8829a4aSRandall Stewart 	/* send SHUTDOWN-COMPLETE */
10667b470fc3SMichael Tuexen 	sctp_send_shutdown_complete(stcb, net, 0);
1067f8829a4aSRandall Stewart 	/* notify upper layer protocol */
1068f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
1069f8829a4aSRandall Stewart 		if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1070f8829a4aSRandall Stewart 		    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
10714e88d37aSMichael Tuexen 			stcb->sctp_socket->so_snd.sb_cc = 0;
1072f8829a4aSRandall Stewart 		}
1073c75fdd30SMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
1074f8829a4aSRandall Stewart 	}
1075f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
1076f8829a4aSRandall Stewart 	/* free the TCB but first save off the ep */
1077ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1078ceaad40aSRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, 1);
1079ceaad40aSRandall Stewart 	SCTP_TCB_UNLOCK(stcb);
1080ceaad40aSRandall Stewart 	SCTP_SOCKET_LOCK(so, 1);
1081ceaad40aSRandall Stewart 	SCTP_TCB_LOCK(stcb);
1082ceaad40aSRandall Stewart 	atomic_subtract_int(&stcb->asoc.refcnt, 1);
1083ceaad40aSRandall Stewart #endif
1084c4739e2fSRandall Stewart 	(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1085b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1086ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1087ceaad40aSRandall Stewart 	SCTP_SOCKET_UNLOCK(so, 1);
1088ceaad40aSRandall Stewart #endif
1089f8829a4aSRandall Stewart }
1090f8829a4aSRandall Stewart 
1091f8829a4aSRandall Stewart /*
1092f8829a4aSRandall Stewart  * Skip past the param header and then we will find the chunk that caused the
1093f8829a4aSRandall Stewart  * problem. There are two possiblities ASCONF or FWD-TSN other than that and
1094f8829a4aSRandall Stewart  * our peer must be broken.
1095f8829a4aSRandall Stewart  */
1096f8829a4aSRandall Stewart static void
1097f8829a4aSRandall Stewart sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
1098f8829a4aSRandall Stewart     struct sctp_nets *net)
1099f8829a4aSRandall Stewart {
1100f8829a4aSRandall Stewart 	struct sctp_chunkhdr *chk;
1101f8829a4aSRandall Stewart 
1102f8829a4aSRandall Stewart 	chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
1103f8829a4aSRandall Stewart 	switch (chk->chunk_type) {
1104f8829a4aSRandall Stewart 	case SCTP_ASCONF_ACK:
1105f8829a4aSRandall Stewart 	case SCTP_ASCONF:
1106f8829a4aSRandall Stewart 		sctp_asconf_cleanup(stcb, net);
1107f8829a4aSRandall Stewart 		break;
1108*44249214SRandall Stewart 	case SCTP_IFORWARD_CUM_TSN:
1109f8829a4aSRandall Stewart 	case SCTP_FORWARD_CUM_TSN:
1110dd973b0eSMichael Tuexen 		stcb->asoc.prsctp_supported = 0;
1111f8829a4aSRandall Stewart 		break;
1112f8829a4aSRandall Stewart 	default:
1113ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
1114ad81507eSRandall Stewart 		    "Peer does not support chunk type %d(%x)??\n",
1115f8829a4aSRandall Stewart 		    chk->chunk_type, (uint32_t) chk->chunk_type);
1116f8829a4aSRandall Stewart 		break;
1117f8829a4aSRandall Stewart 	}
1118f8829a4aSRandall Stewart }
1119f8829a4aSRandall Stewart 
1120f8829a4aSRandall Stewart /*
1121f8829a4aSRandall Stewart  * Skip past the param header and then we will find the param that caused the
1122f8829a4aSRandall Stewart  * problem.  There are a number of param's in a ASCONF OR the prsctp param
1123f8829a4aSRandall Stewart  * these will turn of specific features.
1124c79bec9cSMichael Tuexen  * XXX: Is this the right thing to do?
1125f8829a4aSRandall Stewart  */
1126f8829a4aSRandall Stewart static void
1127f8829a4aSRandall Stewart sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
1128f8829a4aSRandall Stewart {
1129f8829a4aSRandall Stewart 	struct sctp_paramhdr *pbad;
1130f8829a4aSRandall Stewart 
1131f8829a4aSRandall Stewart 	pbad = phdr + 1;
1132f8829a4aSRandall Stewart 	switch (ntohs(pbad->param_type)) {
1133f8829a4aSRandall Stewart 		/* pr-sctp draft */
1134f8829a4aSRandall Stewart 	case SCTP_PRSCTP_SUPPORTED:
1135dd973b0eSMichael Tuexen 		stcb->asoc.prsctp_supported = 0;
1136f8829a4aSRandall Stewart 		break;
1137f8829a4aSRandall Stewart 	case SCTP_SUPPORTED_CHUNK_EXT:
1138f8829a4aSRandall Stewart 		break;
1139f8829a4aSRandall Stewart 		/* draft-ietf-tsvwg-addip-sctp */
1140830d754dSRandall Stewart 	case SCTP_HAS_NAT_SUPPORT:
1141830d754dSRandall Stewart 		stcb->asoc.peer_supports_nat = 0;
1142830d754dSRandall Stewart 		break;
1143f8829a4aSRandall Stewart 	case SCTP_ADD_IP_ADDRESS:
1144f8829a4aSRandall Stewart 	case SCTP_DEL_IP_ADDRESS:
1145f8829a4aSRandall Stewart 	case SCTP_SET_PRIM_ADDR:
1146c79bec9cSMichael Tuexen 		stcb->asoc.asconf_supported = 0;
1147f8829a4aSRandall Stewart 		break;
1148f8829a4aSRandall Stewart 	case SCTP_SUCCESS_REPORT:
1149f8829a4aSRandall Stewart 	case SCTP_ERROR_CAUSE_IND:
1150ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1151ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
1152ad81507eSRandall Stewart 		    "Turning off ASCONF to this strange peer\n");
1153c79bec9cSMichael Tuexen 		stcb->asoc.asconf_supported = 0;
1154f8829a4aSRandall Stewart 		break;
1155f8829a4aSRandall Stewart 	default:
1156ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
1157ad81507eSRandall Stewart 		    "Peer does not support param type %d(%x)??\n",
1158f8829a4aSRandall Stewart 		    pbad->param_type, (uint32_t) pbad->param_type);
1159f8829a4aSRandall Stewart 		break;
1160f8829a4aSRandall Stewart 	}
1161f8829a4aSRandall Stewart }
1162f8829a4aSRandall Stewart 
1163f8829a4aSRandall Stewart static int
1164f8829a4aSRandall Stewart sctp_handle_error(struct sctp_chunkhdr *ch,
1165f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net)
1166f8829a4aSRandall Stewart {
1167f8829a4aSRandall Stewart 	int chklen;
1168f8829a4aSRandall Stewart 	struct sctp_paramhdr *phdr;
1169389b1b11SMichael Tuexen 	uint16_t error, error_type;
1170f8829a4aSRandall Stewart 	uint16_t error_len;
1171f8829a4aSRandall Stewart 	struct sctp_association *asoc;
1172f8829a4aSRandall Stewart 	int adjust;
1173f8829a4aSRandall Stewart 
1174ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1175ceaad40aSRandall Stewart 	struct socket *so;
1176ceaad40aSRandall Stewart 
1177ceaad40aSRandall Stewart #endif
1178ceaad40aSRandall Stewart 
1179f8829a4aSRandall Stewart 	/* parse through all of the errors and process */
1180f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
1181f8829a4aSRandall Stewart 	phdr = (struct sctp_paramhdr *)((caddr_t)ch +
1182f8829a4aSRandall Stewart 	    sizeof(struct sctp_chunkhdr));
1183f8829a4aSRandall Stewart 	chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
1184389b1b11SMichael Tuexen 	error = 0;
1185f8829a4aSRandall Stewart 	while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
1186f8829a4aSRandall Stewart 		/* Process an Error Cause */
1187f8829a4aSRandall Stewart 		error_type = ntohs(phdr->param_type);
1188f8829a4aSRandall Stewart 		error_len = ntohs(phdr->param_length);
1189f8829a4aSRandall Stewart 		if ((error_len > chklen) || (error_len == 0)) {
1190f8829a4aSRandall Stewart 			/* invalid param length for this param */
1191ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n",
1192f8829a4aSRandall Stewart 			    chklen, error_len);
1193f8829a4aSRandall Stewart 			return (0);
1194f8829a4aSRandall Stewart 		}
1195389b1b11SMichael Tuexen 		if (error == 0) {
1196389b1b11SMichael Tuexen 			/* report the first error cause */
1197389b1b11SMichael Tuexen 			error = error_type;
1198389b1b11SMichael Tuexen 		}
1199f8829a4aSRandall Stewart 		switch (error_type) {
1200f8829a4aSRandall Stewart 		case SCTP_CAUSE_INVALID_STREAM:
1201f8829a4aSRandall Stewart 		case SCTP_CAUSE_MISSING_PARAM:
1202f8829a4aSRandall Stewart 		case SCTP_CAUSE_INVALID_PARAM:
1203f8829a4aSRandall Stewart 		case SCTP_CAUSE_NO_USER_DATA:
1204ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n",
1205f8829a4aSRandall Stewart 			    error_type);
1206f8829a4aSRandall Stewart 			break;
1207830d754dSRandall Stewart 		case SCTP_CAUSE_NAT_COLLIDING_STATE:
1208830d754dSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
1209830d754dSRandall Stewart 			    ch->chunk_flags);
1210830d754dSRandall Stewart 			if (sctp_handle_nat_colliding_state(stcb)) {
1211830d754dSRandall Stewart 				return (0);
1212830d754dSRandall Stewart 			}
1213830d754dSRandall Stewart 			break;
1214830d754dSRandall Stewart 		case SCTP_CAUSE_NAT_MISSING_STATE:
1215830d754dSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
1216830d754dSRandall Stewart 			    ch->chunk_flags);
1217830d754dSRandall Stewart 			if (sctp_handle_nat_missing_state(stcb, net)) {
1218830d754dSRandall Stewart 				return (0);
1219830d754dSRandall Stewart 			}
1220830d754dSRandall Stewart 			break;
1221f8829a4aSRandall Stewart 		case SCTP_CAUSE_STALE_COOKIE:
1222f8829a4aSRandall Stewart 			/*
1223f8829a4aSRandall Stewart 			 * We only act if we have echoed a cookie and are
1224f8829a4aSRandall Stewart 			 * waiting.
1225f8829a4aSRandall Stewart 			 */
1226f8829a4aSRandall Stewart 			if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
1227f8829a4aSRandall Stewart 				int *p;
1228f8829a4aSRandall Stewart 
1229f8829a4aSRandall Stewart 				p = (int *)((caddr_t)phdr + sizeof(*phdr));
1230f8829a4aSRandall Stewart 				/* Save the time doubled */
1231f8829a4aSRandall Stewart 				asoc->cookie_preserve_req = ntohl(*p) << 1;
1232f8829a4aSRandall Stewart 				asoc->stale_cookie_count++;
1233f8829a4aSRandall Stewart 				if (asoc->stale_cookie_count >
1234f8829a4aSRandall Stewart 				    asoc->max_init_times) {
1235410a3b1eSMichael Tuexen 					sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
1236f8829a4aSRandall Stewart 					/* now free the asoc */
1237ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1238ceaad40aSRandall Stewart 					so = SCTP_INP_SO(stcb->sctp_ep);
1239ceaad40aSRandall Stewart 					atomic_add_int(&stcb->asoc.refcnt, 1);
1240ceaad40aSRandall Stewart 					SCTP_TCB_UNLOCK(stcb);
1241ceaad40aSRandall Stewart 					SCTP_SOCKET_LOCK(so, 1);
1242ceaad40aSRandall Stewart 					SCTP_TCB_LOCK(stcb);
1243ceaad40aSRandall Stewart 					atomic_subtract_int(&stcb->asoc.refcnt, 1);
1244ceaad40aSRandall Stewart #endif
1245c4739e2fSRandall Stewart 					(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1246b7d130beSMichael Tuexen 					    SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1247ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1248ceaad40aSRandall Stewart 					SCTP_SOCKET_UNLOCK(so, 1);
1249ceaad40aSRandall Stewart #endif
1250f8829a4aSRandall Stewart 					return (-1);
1251f8829a4aSRandall Stewart 				}
1252f8829a4aSRandall Stewart 				/* blast back to INIT state */
1253830d754dSRandall Stewart 				sctp_toss_old_cookies(stcb, &stcb->asoc);
1254f8829a4aSRandall Stewart 				asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
1255f8829a4aSRandall Stewart 				asoc->state |= SCTP_STATE_COOKIE_WAIT;
1256f8829a4aSRandall Stewart 				sctp_stop_all_cookie_timers(stcb);
1257ceaad40aSRandall Stewart 				sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1258f8829a4aSRandall Stewart 			}
1259f8829a4aSRandall Stewart 			break;
1260f8829a4aSRandall Stewart 		case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1261f8829a4aSRandall Stewart 			/*
1262f8829a4aSRandall Stewart 			 * Nothing we can do here, we don't do hostname
1263f8829a4aSRandall Stewart 			 * addresses so if the peer does not like my IPv6
1264f8829a4aSRandall Stewart 			 * (or IPv4 for that matter) it does not matter. If
1265f8829a4aSRandall Stewart 			 * they don't support that type of address, they can
1266f8829a4aSRandall Stewart 			 * NOT possibly get that packet type... i.e. with no
1267f8829a4aSRandall Stewart 			 * IPv6 you can't recieve a IPv6 packet. so we can
1268f8829a4aSRandall Stewart 			 * safely ignore this one. If we ever added support
1269f8829a4aSRandall Stewart 			 * for HOSTNAME Addresses, then we would need to do
1270f8829a4aSRandall Stewart 			 * something here.
1271f8829a4aSRandall Stewart 			 */
1272f8829a4aSRandall Stewart 			break;
1273f8829a4aSRandall Stewart 		case SCTP_CAUSE_UNRECOG_CHUNK:
1274f8829a4aSRandall Stewart 			sctp_process_unrecog_chunk(stcb, phdr, net);
1275f8829a4aSRandall Stewart 			break;
1276f8829a4aSRandall Stewart 		case SCTP_CAUSE_UNRECOG_PARAM:
1277f8829a4aSRandall Stewart 			sctp_process_unrecog_param(stcb, phdr);
1278f8829a4aSRandall Stewart 			break;
1279f8829a4aSRandall Stewart 		case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1280f8829a4aSRandall Stewart 			/*
1281f8829a4aSRandall Stewart 			 * We ignore this since the timer will drive out a
1282f8829a4aSRandall Stewart 			 * new cookie anyway and there timer will drive us
1283f8829a4aSRandall Stewart 			 * to send a SHUTDOWN_COMPLETE. We can't send one
1284f8829a4aSRandall Stewart 			 * here since we don't have their tag.
1285f8829a4aSRandall Stewart 			 */
1286f8829a4aSRandall Stewart 			break;
1287f8829a4aSRandall Stewart 		case SCTP_CAUSE_DELETING_LAST_ADDR:
1288f8829a4aSRandall Stewart 		case SCTP_CAUSE_RESOURCE_SHORTAGE:
1289f8829a4aSRandall Stewart 		case SCTP_CAUSE_DELETING_SRC_ADDR:
1290f8829a4aSRandall Stewart 			/*
1291f8829a4aSRandall Stewart 			 * We should NOT get these here, but in a
129293164cf9SRandall Stewart 			 * ASCONF-ACK.
1293f8829a4aSRandall Stewart 			 */
1294ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n",
1295f8829a4aSRandall Stewart 			    error_type);
1296f8829a4aSRandall Stewart 			break;
1297f8829a4aSRandall Stewart 		case SCTP_CAUSE_OUT_OF_RESC:
1298f8829a4aSRandall Stewart 			/*
1299f8829a4aSRandall Stewart 			 * And what, pray tell do we do with the fact that
1300f8829a4aSRandall Stewart 			 * the peer is out of resources? Not really sure we
130193164cf9SRandall Stewart 			 * could do anything but abort. I suspect this
1302f8829a4aSRandall Stewart 			 * should have came WITH an abort instead of in a
1303f8829a4aSRandall Stewart 			 * OP-ERROR.
1304f8829a4aSRandall Stewart 			 */
1305f8829a4aSRandall Stewart 			break;
1306f8829a4aSRandall Stewart 		default:
1307ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n",
1308f8829a4aSRandall Stewart 			    error_type);
1309f8829a4aSRandall Stewart 			break;
1310f8829a4aSRandall Stewart 		}
1311f8829a4aSRandall Stewart 		adjust = SCTP_SIZE32(error_len);
1312f8829a4aSRandall Stewart 		chklen -= adjust;
1313f8829a4aSRandall Stewart 		phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
1314f8829a4aSRandall Stewart 	}
1315389b1b11SMichael Tuexen 	sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, error, ch, SCTP_SO_NOT_LOCKED);
1316f8829a4aSRandall Stewart 	return (0);
1317f8829a4aSRandall Stewart }
1318f8829a4aSRandall Stewart 
1319f8829a4aSRandall Stewart static int
1320b1754ad1SMichael Tuexen sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1321b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
1322f30ac432SMichael Tuexen     struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1323f30ac432SMichael Tuexen     struct sctp_nets *net, int *abort_no_unlock,
1324457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
1325f30ac432SMichael Tuexen     uint32_t vrf_id)
1326f8829a4aSRandall Stewart {
1327f8829a4aSRandall Stewart 	struct sctp_init_ack *init_ack;
1328f8829a4aSRandall Stewart 	struct mbuf *op_err;
1329f8829a4aSRandall Stewart 
1330ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
1331ad81507eSRandall Stewart 	    "sctp_handle_init_ack: handling INIT-ACK\n");
1332ad81507eSRandall Stewart 
1333f8829a4aSRandall Stewart 	if (stcb == NULL) {
1334ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
1335ad81507eSRandall Stewart 		    "sctp_handle_init_ack: TCB is null\n");
1336f8829a4aSRandall Stewart 		return (-1);
1337f8829a4aSRandall Stewart 	}
1338f8829a4aSRandall Stewart 	if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1339f8829a4aSRandall Stewart 		/* Invalid length */
1340ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1341b1754ad1SMichael Tuexen 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1342b1754ad1SMichael Tuexen 		    src, dst, sh, op_err,
1343457b4b88SMichael Tuexen 		    mflowtype, mflowid,
1344f30ac432SMichael Tuexen 		    vrf_id, net->port);
1345bff64a4dSRandall Stewart 		*abort_no_unlock = 1;
1346f8829a4aSRandall Stewart 		return (-1);
1347f8829a4aSRandall Stewart 	}
1348f8829a4aSRandall Stewart 	init_ack = &cp->init;
1349f8829a4aSRandall Stewart 	/* validate parameters */
1350f8829a4aSRandall Stewart 	if (init_ack->initiate_tag == 0) {
1351f8829a4aSRandall Stewart 		/* protocol error... send an abort */
1352ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1353b1754ad1SMichael Tuexen 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1354b1754ad1SMichael Tuexen 		    src, dst, sh, op_err,
1355457b4b88SMichael Tuexen 		    mflowtype, mflowid,
1356f30ac432SMichael Tuexen 		    vrf_id, net->port);
1357bff64a4dSRandall Stewart 		*abort_no_unlock = 1;
1358f8829a4aSRandall Stewart 		return (-1);
1359f8829a4aSRandall Stewart 	}
1360f8829a4aSRandall Stewart 	if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1361f8829a4aSRandall Stewart 		/* protocol error... send an abort */
1362ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1363b1754ad1SMichael Tuexen 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1364b1754ad1SMichael Tuexen 		    src, dst, sh, op_err,
1365457b4b88SMichael Tuexen 		    mflowtype, mflowid,
1366f30ac432SMichael Tuexen 		    vrf_id, net->port);
1367bff64a4dSRandall Stewart 		*abort_no_unlock = 1;
1368f8829a4aSRandall Stewart 		return (-1);
1369f8829a4aSRandall Stewart 	}
1370f8829a4aSRandall Stewart 	if (init_ack->num_inbound_streams == 0) {
1371f8829a4aSRandall Stewart 		/* protocol error... send an abort */
1372ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1373b1754ad1SMichael Tuexen 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1374b1754ad1SMichael Tuexen 		    src, dst, sh, op_err,
1375457b4b88SMichael Tuexen 		    mflowtype, mflowid,
1376f30ac432SMichael Tuexen 		    vrf_id, net->port);
1377bff64a4dSRandall Stewart 		*abort_no_unlock = 1;
1378f8829a4aSRandall Stewart 		return (-1);
1379f8829a4aSRandall Stewart 	}
1380f8829a4aSRandall Stewart 	if (init_ack->num_outbound_streams == 0) {
1381f8829a4aSRandall Stewart 		/* protocol error... send an abort */
1382ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1383b1754ad1SMichael Tuexen 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1384b1754ad1SMichael Tuexen 		    src, dst, sh, op_err,
1385457b4b88SMichael Tuexen 		    mflowtype, mflowid,
1386f30ac432SMichael Tuexen 		    vrf_id, net->port);
1387bff64a4dSRandall Stewart 		*abort_no_unlock = 1;
1388f8829a4aSRandall Stewart 		return (-1);
1389f8829a4aSRandall Stewart 	}
1390f8829a4aSRandall Stewart 	/* process according to association state... */
1391c4739e2fSRandall Stewart 	switch (stcb->asoc.state & SCTP_STATE_MASK) {
1392f8829a4aSRandall Stewart 	case SCTP_STATE_COOKIE_WAIT:
1393f8829a4aSRandall Stewart 		/* this is the expected state for this chunk */
1394f8829a4aSRandall Stewart 		/* process the INIT-ACK parameters */
1395f8829a4aSRandall Stewart 		if (stcb->asoc.primary_destination->dest_state &
1396f8829a4aSRandall Stewart 		    SCTP_ADDR_UNCONFIRMED) {
1397f8829a4aSRandall Stewart 			/*
1398f8829a4aSRandall Stewart 			 * The primary is where we sent the INIT, we can
1399f8829a4aSRandall Stewart 			 * always consider it confirmed when the INIT-ACK is
1400f8829a4aSRandall Stewart 			 * returned. Do this before we load addresses
1401f8829a4aSRandall Stewart 			 * though.
1402f8829a4aSRandall Stewart 			 */
1403f8829a4aSRandall Stewart 			stcb->asoc.primary_destination->dest_state &=
1404f8829a4aSRandall Stewart 			    ~SCTP_ADDR_UNCONFIRMED;
1405f8829a4aSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1406ceaad40aSRandall Stewart 			    stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1407f8829a4aSRandall Stewart 		}
1408b1754ad1SMichael Tuexen 		if (sctp_process_init_ack(m, iphlen, offset, src, dst, sh, cp, stcb,
1409f30ac432SMichael Tuexen 		    net, abort_no_unlock,
1410457b4b88SMichael Tuexen 		    mflowtype, mflowid,
1411f30ac432SMichael Tuexen 		    vrf_id) < 0) {
1412f8829a4aSRandall Stewart 			/* error in parsing parameters */
1413f8829a4aSRandall Stewart 			return (-1);
1414f8829a4aSRandall Stewart 		}
1415f8829a4aSRandall Stewart 		/* update our state */
1416ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1417c4739e2fSRandall Stewart 		SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED);
1418f8829a4aSRandall Stewart 
1419f8829a4aSRandall Stewart 		/* reset the RTO calc */
1420b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1421c4739e2fSRandall Stewart 			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1422c4739e2fSRandall Stewart 			    stcb->asoc.overall_error_count,
1423c4739e2fSRandall Stewart 			    0,
1424c4739e2fSRandall Stewart 			    SCTP_FROM_SCTP_INPUT,
1425c4739e2fSRandall Stewart 			    __LINE__);
1426c4739e2fSRandall Stewart 		}
1427f8829a4aSRandall Stewart 		stcb->asoc.overall_error_count = 0;
14286e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1429f8829a4aSRandall Stewart 		/*
1430f8829a4aSRandall Stewart 		 * collapse the init timer back in case of a exponential
1431a5d547adSRandall Stewart 		 * backoff
1432f8829a4aSRandall Stewart 		 */
1433f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1434f8829a4aSRandall Stewart 		    stcb, net);
1435f8829a4aSRandall Stewart 		/*
1436f8829a4aSRandall Stewart 		 * the send at the end of the inbound data processing will
1437f8829a4aSRandall Stewart 		 * cause the cookie to be sent
1438f8829a4aSRandall Stewart 		 */
1439f8829a4aSRandall Stewart 		break;
1440f8829a4aSRandall Stewart 	case SCTP_STATE_SHUTDOWN_SENT:
1441f8829a4aSRandall Stewart 		/* incorrect state... discard */
1442f8829a4aSRandall Stewart 		break;
1443f8829a4aSRandall Stewart 	case SCTP_STATE_COOKIE_ECHOED:
1444f8829a4aSRandall Stewart 		/* incorrect state... discard */
1445f8829a4aSRandall Stewart 		break;
1446f8829a4aSRandall Stewart 	case SCTP_STATE_OPEN:
1447f8829a4aSRandall Stewart 		/* incorrect state... discard */
1448f8829a4aSRandall Stewart 		break;
1449f8829a4aSRandall Stewart 	case SCTP_STATE_EMPTY:
1450f8829a4aSRandall Stewart 	case SCTP_STATE_INUSE:
1451f8829a4aSRandall Stewart 	default:
1452f8829a4aSRandall Stewart 		/* incorrect state... discard */
1453f8829a4aSRandall Stewart 		return (-1);
1454f8829a4aSRandall Stewart 		break;
1455f8829a4aSRandall Stewart 	}
1456ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1457f8829a4aSRandall Stewart 	return (0);
1458f8829a4aSRandall Stewart }
1459f8829a4aSRandall Stewart 
1460830d754dSRandall Stewart static struct sctp_tcb *
1461830d754dSRandall Stewart sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1462b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
1463830d754dSRandall Stewart     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1464830d754dSRandall Stewart     struct sctp_inpcb *inp, struct sctp_nets **netp,
1465830d754dSRandall Stewart     struct sockaddr *init_src, int *notification,
1466830d754dSRandall Stewart     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1467457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
1468830d754dSRandall Stewart     uint32_t vrf_id, uint16_t port);
1469830d754dSRandall Stewart 
1470f8829a4aSRandall Stewart 
1471f8829a4aSRandall Stewart /*
1472f8829a4aSRandall Stewart  * handle a state cookie for an existing association m: input packet mbuf
1473f8829a4aSRandall Stewart  * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1474f8829a4aSRandall Stewart  * "split" mbuf and the cookie signature does not exist offset: offset into
1475f8829a4aSRandall Stewart  * mbuf to the cookie-echo chunk
1476f8829a4aSRandall Stewart  */
1477f8829a4aSRandall Stewart static struct sctp_tcb *
1478f8829a4aSRandall Stewart sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1479b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
1480f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1481830d754dSRandall Stewart     struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
14827215cc1bSMichael Tuexen     struct sockaddr *init_src, int *notification,
1483f30ac432SMichael Tuexen     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1484457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
1485f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
1486f8829a4aSRandall Stewart {
1487f8829a4aSRandall Stewart 	struct sctp_association *asoc;
1488f8829a4aSRandall Stewart 	struct sctp_init_chunk *init_cp, init_buf;
1489f8829a4aSRandall Stewart 	struct sctp_init_ack_chunk *initack_cp, initack_buf;
1490830d754dSRandall Stewart 	struct sctp_nets *net;
1491830d754dSRandall Stewart 	struct mbuf *op_err;
1492a5d547adSRandall Stewart 	int init_offset, initack_offset, i;
1493f8829a4aSRandall Stewart 	int retval;
14947f34832bSRandall Stewart 	int spec_flag = 0;
14954c9179adSRandall Stewart 	uint32_t how_indx;
1496f8829a4aSRandall Stewart 
1497f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
1498f0396ad1SMichael Tuexen 	int j;
1499f0396ad1SMichael Tuexen 
1500f0396ad1SMichael Tuexen #endif
1501f0396ad1SMichael Tuexen 
1502830d754dSRandall Stewart 	net = *netp;
1503f8829a4aSRandall Stewart 	/* I know that the TCB is non-NULL from the caller */
1504f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
1505f42a358aSRandall Stewart 	for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
150644b7479bSRandall Stewart 		if (asoc->cookie_how[how_indx] == 0)
150744b7479bSRandall Stewart 			break;
150844b7479bSRandall Stewart 	}
150944b7479bSRandall Stewart 	if (how_indx < sizeof(asoc->cookie_how)) {
151044b7479bSRandall Stewart 		asoc->cookie_how[how_indx] = 1;
151144b7479bSRandall Stewart 	}
1512f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1513f8829a4aSRandall Stewart 		/* SHUTDOWN came in after sending INIT-ACK */
1514f8829a4aSRandall Stewart 		sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1515ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_COOKIE_IN_SHUTDOWN, "");
1516b1754ad1SMichael Tuexen 		sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
1517d089f9b9SMichael Tuexen 		    mflowtype, mflowid, inp->fibnum,
1518c54a18d2SRandall Stewart 		    vrf_id, net->port);
151944b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
152044b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 2;
1521f8829a4aSRandall Stewart 		return (NULL);
1522f8829a4aSRandall Stewart 	}
1523f8829a4aSRandall Stewart 	/*
1524f8829a4aSRandall Stewart 	 * find and validate the INIT chunk in the cookie (peer's info) the
1525f8829a4aSRandall Stewart 	 * INIT should start after the cookie-echo header struct (chunk
1526f8829a4aSRandall Stewart 	 * header, state cookie header struct)
1527f8829a4aSRandall Stewart 	 */
1528f8829a4aSRandall Stewart 	init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1529f8829a4aSRandall Stewart 
1530f8829a4aSRandall Stewart 	init_cp = (struct sctp_init_chunk *)
1531f8829a4aSRandall Stewart 	    sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1532f8829a4aSRandall Stewart 	    (uint8_t *) & init_buf);
1533f8829a4aSRandall Stewart 	if (init_cp == NULL) {
1534f8829a4aSRandall Stewart 		/* could not pull a INIT chunk in cookie */
1535f8829a4aSRandall Stewart 		return (NULL);
1536f8829a4aSRandall Stewart 	}
1537f8829a4aSRandall Stewart 	if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1538f8829a4aSRandall Stewart 		return (NULL);
1539f8829a4aSRandall Stewart 	}
1540f8829a4aSRandall Stewart 	/*
1541f8829a4aSRandall Stewart 	 * find and validate the INIT-ACK chunk in the cookie (my info) the
1542f8829a4aSRandall Stewart 	 * INIT-ACK follows the INIT chunk
1543f8829a4aSRandall Stewart 	 */
154460990c0cSMichael Tuexen 	initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1545f8829a4aSRandall Stewart 	initack_cp = (struct sctp_init_ack_chunk *)
1546f8829a4aSRandall Stewart 	    sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1547f8829a4aSRandall Stewart 	    (uint8_t *) & initack_buf);
1548f8829a4aSRandall Stewart 	if (initack_cp == NULL) {
1549f8829a4aSRandall Stewart 		/* could not pull INIT-ACK chunk in cookie */
1550f8829a4aSRandall Stewart 		return (NULL);
1551f8829a4aSRandall Stewart 	}
1552f8829a4aSRandall Stewart 	if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1553f8829a4aSRandall Stewart 		return (NULL);
1554f8829a4aSRandall Stewart 	}
1555f8829a4aSRandall Stewart 	if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1556f8829a4aSRandall Stewart 	    (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1557f8829a4aSRandall Stewart 		/*
1558f8829a4aSRandall Stewart 		 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1559f8829a4aSRandall Stewart 		 * to get into the OPEN state
1560f8829a4aSRandall Stewart 		 */
156144b7479bSRandall Stewart 		if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1562851b7298SRandall Stewart 			/*-
1563851b7298SRandall Stewart 			 * Opps, this means that we somehow generated two vtag's
1564851b7298SRandall Stewart 			 * the same. I.e. we did:
1565851b7298SRandall Stewart 			 *  Us               Peer
1566851b7298SRandall Stewart 			 *   <---INIT(tag=a)------
1567851b7298SRandall Stewart 			 *   ----INIT-ACK(tag=t)-->
1568851b7298SRandall Stewart 			 *   ----INIT(tag=t)------> *1
1569851b7298SRandall Stewart 			 *   <---INIT-ACK(tag=a)---
1570851b7298SRandall Stewart                          *   <----CE(tag=t)------------- *2
1571851b7298SRandall Stewart 			 *
1572851b7298SRandall Stewart 			 * At point *1 we should be generating a different
1573851b7298SRandall Stewart 			 * tag t'. Which means we would throw away the CE and send
1574851b7298SRandall Stewart 			 * ours instead. Basically this is case C (throw away side).
1575851b7298SRandall Stewart 			 */
1576851b7298SRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
1577851b7298SRandall Stewart 				asoc->cookie_how[how_indx] = 17;
1578851b7298SRandall Stewart 			return (NULL);
1579851b7298SRandall Stewart 
158044b7479bSRandall Stewart 		}
15811213f0e7SMichael Tuexen 		switch (SCTP_GET_STATE(asoc)) {
1582f8829a4aSRandall Stewart 		case SCTP_STATE_COOKIE_WAIT:
158344b7479bSRandall Stewart 		case SCTP_STATE_COOKIE_ECHOED:
1584f8829a4aSRandall Stewart 			/*
158517205eccSRandall Stewart 			 * INIT was sent but got a COOKIE_ECHO with the
158644b7479bSRandall Stewart 			 * correct tags... just accept it...but we must
158744b7479bSRandall Stewart 			 * process the init so that we can make sure we have
158844b7479bSRandall Stewart 			 * the right seq no's.
1589f8829a4aSRandall Stewart 			 */
1590f8829a4aSRandall Stewart 			/* First we must process the INIT !! */
15917215cc1bSMichael Tuexen 			retval = sctp_process_init(init_cp, stcb);
1592f8829a4aSRandall Stewart 			if (retval < 0) {
159344b7479bSRandall Stewart 				if (how_indx < sizeof(asoc->cookie_how))
159444b7479bSRandall Stewart 					asoc->cookie_how[how_indx] = 3;
1595f8829a4aSRandall Stewart 				return (NULL);
1596f8829a4aSRandall Stewart 			}
1597f8829a4aSRandall Stewart 			/* we have already processed the INIT so no problem */
1598b7d130beSMichael Tuexen 			sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp,
1599b7d130beSMichael Tuexen 			    stcb, net,
1600b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1601b7d130beSMichael Tuexen 			sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp,
1602b7d130beSMichael Tuexen 			    stcb, net,
1603b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1604f8829a4aSRandall Stewart 			/* update current state */
1605f42a358aSRandall Stewart 			if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1606f42a358aSRandall Stewart 				SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1607f42a358aSRandall Stewart 			else
1608f42a358aSRandall Stewart 				SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1609c4739e2fSRandall Stewart 
1610c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1611f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1612f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1613f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
1614f8829a4aSRandall Stewart 			}
1615f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1616a5d547adSRandall Stewart 			sctp_stop_all_cookie_timers(stcb);
1617f8829a4aSRandall Stewart 			if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1618f8829a4aSRandall Stewart 			    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1619f8829a4aSRandall Stewart 			    (inp->sctp_socket->so_qlimit == 0)
1620f8829a4aSRandall Stewart 			    ) {
1621ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1622ceaad40aSRandall Stewart 				struct socket *so;
1623ceaad40aSRandall Stewart 
1624ceaad40aSRandall Stewart #endif
1625f8829a4aSRandall Stewart 				/*
1626f8829a4aSRandall Stewart 				 * Here is where collision would go if we
1627f8829a4aSRandall Stewart 				 * did a connect() and instead got a
1628f8829a4aSRandall Stewart 				 * init/init-ack/cookie done before the
1629f8829a4aSRandall Stewart 				 * init-ack came back..
1630f8829a4aSRandall Stewart 				 */
1631f8829a4aSRandall Stewart 				stcb->sctp_ep->sctp_flags |=
1632f8829a4aSRandall Stewart 				    SCTP_PCB_FLAGS_CONNECTED;
1633ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1634ceaad40aSRandall Stewart 				so = SCTP_INP_SO(stcb->sctp_ep);
1635ceaad40aSRandall Stewart 				atomic_add_int(&stcb->asoc.refcnt, 1);
1636ceaad40aSRandall Stewart 				SCTP_TCB_UNLOCK(stcb);
1637ceaad40aSRandall Stewart 				SCTP_SOCKET_LOCK(so, 1);
1638ceaad40aSRandall Stewart 				SCTP_TCB_LOCK(stcb);
1639ceaad40aSRandall Stewart 				atomic_add_int(&stcb->asoc.refcnt, -1);
1640ceaad40aSRandall Stewart 				if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1641ceaad40aSRandall Stewart 					SCTP_SOCKET_UNLOCK(so, 1);
1642ceaad40aSRandall Stewart 					return (NULL);
1643ceaad40aSRandall Stewart 				}
1644ceaad40aSRandall Stewart #endif
1645ceaad40aSRandall Stewart 				soisconnected(stcb->sctp_socket);
1646ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1647ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
1648ceaad40aSRandall Stewart #endif
1649f8829a4aSRandall Stewart 			}
1650f8829a4aSRandall Stewart 			/* notify upper layer */
1651f8829a4aSRandall Stewart 			*notification = SCTP_NOTIFY_ASSOC_UP;
1652f8829a4aSRandall Stewart 			/*
1653f8829a4aSRandall Stewart 			 * since we did not send a HB make sure we don't
1654f8829a4aSRandall Stewart 			 * double things
1655f8829a4aSRandall Stewart 			 */
1656f8829a4aSRandall Stewart 			net->hb_responded = 1;
16579a972525SRandall Stewart 			net->RTO = sctp_calculate_rto(stcb, asoc, net,
1658899288aeSRandall Stewart 			    &cookie->time_entered,
1659899288aeSRandall Stewart 			    sctp_align_unsafe_makecopy,
1660f79aab18SRandall Stewart 			    SCTP_RTT_FROM_NON_DATA);
1661f8829a4aSRandall Stewart 
1662f8829a4aSRandall Stewart 			if (stcb->asoc.sctp_autoclose_ticks &&
1663f8829a4aSRandall Stewart 			    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1664f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1665f8829a4aSRandall Stewart 				    inp, stcb, NULL);
1666f8829a4aSRandall Stewart 			}
1667f8829a4aSRandall Stewart 			break;
1668f8829a4aSRandall Stewart 		default:
1669f8829a4aSRandall Stewart 			/*
1670f8829a4aSRandall Stewart 			 * we're in the OPEN state (or beyond), so peer must
1671f8829a4aSRandall Stewart 			 * have simply lost the COOKIE-ACK
1672f8829a4aSRandall Stewart 			 */
1673f8829a4aSRandall Stewart 			break;
1674f8829a4aSRandall Stewart 		}		/* end switch */
1675a5d547adSRandall Stewart 		sctp_stop_all_cookie_timers(stcb);
1676f8829a4aSRandall Stewart 		/*
1677f8829a4aSRandall Stewart 		 * We ignore the return code here.. not sure if we should
1678f8829a4aSRandall Stewart 		 * somehow abort.. but we do have an existing asoc. This
1679f8829a4aSRandall Stewart 		 * really should not fail.
1680f8829a4aSRandall Stewart 		 */
16817215cc1bSMichael Tuexen 		if (sctp_load_addresses_from_init(stcb, m,
1682f8829a4aSRandall Stewart 		    init_offset + sizeof(struct sctp_init_chunk),
1683b1754ad1SMichael Tuexen 		    initack_offset, src, dst, init_src)) {
168444b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
168544b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 4;
1686f8829a4aSRandall Stewart 			return (NULL);
1687f8829a4aSRandall Stewart 		}
1688f8829a4aSRandall Stewart 		/* respond with a COOKIE-ACK */
1689a5d547adSRandall Stewart 		sctp_toss_old_cookies(stcb, asoc);
1690f8829a4aSRandall Stewart 		sctp_send_cookie_ack(stcb);
169144b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
169244b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 5;
1693f8829a4aSRandall Stewart 		return (stcb);
169417205eccSRandall Stewart 	}
1695f8829a4aSRandall Stewart 	if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1696f8829a4aSRandall Stewart 	    ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1697f8829a4aSRandall Stewart 	    cookie->tie_tag_my_vtag == 0 &&
1698f8829a4aSRandall Stewart 	    cookie->tie_tag_peer_vtag == 0) {
1699f8829a4aSRandall Stewart 		/*
1700f8829a4aSRandall Stewart 		 * case C in Section 5.2.4 Table 2: XMOO silently discard
1701f8829a4aSRandall Stewart 		 */
170244b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
170344b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 6;
1704f8829a4aSRandall Stewart 		return (NULL);
1705f8829a4aSRandall Stewart 	}
1706830d754dSRandall Stewart 	/*
1707830d754dSRandall Stewart 	 * If nat support, and the below and stcb is established, send back
1708830d754dSRandall Stewart 	 * a ABORT(colliding state) if we are established.
1709830d754dSRandall Stewart 	 */
1710830d754dSRandall Stewart 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) &&
1711830d754dSRandall Stewart 	    (asoc->peer_supports_nat) &&
1712830d754dSRandall Stewart 	    ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1713830d754dSRandall Stewart 	    ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1714830d754dSRandall Stewart 	    (asoc->peer_vtag == 0)))) {
1715830d754dSRandall Stewart 		/*
1716830d754dSRandall Stewart 		 * Special case - Peer's support nat. We may have two init's
1717830d754dSRandall Stewart 		 * that we gave out the same tag on since one was not
1718830d754dSRandall Stewart 		 * established.. i.e. we get INIT from host-1 behind the nat
1719830d754dSRandall Stewart 		 * and we respond tag-a, we get a INIT from host-2 behind
1720830d754dSRandall Stewart 		 * the nat and we get tag-a again. Then we bring up host-1
1721830d754dSRandall Stewart 		 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1722830d754dSRandall Stewart 		 * Now we have colliding state. We must send an abort here
1723830d754dSRandall Stewart 		 * with colliding state indication.
1724830d754dSRandall Stewart 		 */
1725ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_NAT_COLLIDING_STATE, "");
1726b1754ad1SMichael Tuexen 		sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
1727d089f9b9SMichael Tuexen 		    mflowtype, mflowid, inp->fibnum,
1728f30ac432SMichael Tuexen 		    vrf_id, port);
1729830d754dSRandall Stewart 		return (NULL);
1730830d754dSRandall Stewart 	}
1731830d754dSRandall Stewart 	if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1732830d754dSRandall Stewart 	    ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1733830d754dSRandall Stewart 	    (asoc->peer_vtag == 0))) {
1734f8829a4aSRandall Stewart 		/*
1735f8829a4aSRandall Stewart 		 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1736f8829a4aSRandall Stewart 		 * should be ok, re-accept peer info
1737f8829a4aSRandall Stewart 		 */
173844b7479bSRandall Stewart 		if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
173944b7479bSRandall Stewart 			/*
174044b7479bSRandall Stewart 			 * Extension of case C. If we hit this, then the
174144b7479bSRandall Stewart 			 * random number generator returned the same vtag
174244b7479bSRandall Stewart 			 * when we first sent our INIT-ACK and when we later
174344b7479bSRandall Stewart 			 * sent our INIT. The side with the seq numbers that
174444b7479bSRandall Stewart 			 * are different will be the one that normnally
174544b7479bSRandall Stewart 			 * would have hit case C. This in effect "extends"
174644b7479bSRandall Stewart 			 * our vtags in this collision case to be 64 bits.
174744b7479bSRandall Stewart 			 * The same collision could occur aka you get both
174844b7479bSRandall Stewart 			 * vtag and seq number the same twice in a row.. but
174944b7479bSRandall Stewart 			 * is much less likely. If it did happen then we
175044b7479bSRandall Stewart 			 * would proceed through and bring up the assoc.. we
175144b7479bSRandall Stewart 			 * may end up with the wrong stream setup however..
175244b7479bSRandall Stewart 			 * which would be bad.. but there is no way to
175344b7479bSRandall Stewart 			 * tell.. until we send on a stream that does not
175444b7479bSRandall Stewart 			 * exist :-)
175544b7479bSRandall Stewart 			 */
175644b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
175744b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 7;
175844b7479bSRandall Stewart 
175944b7479bSRandall Stewart 			return (NULL);
176044b7479bSRandall Stewart 		}
176144b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
176244b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 8;
1763b7d130beSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1764b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1765f8829a4aSRandall Stewart 		sctp_stop_all_cookie_timers(stcb);
1766f8829a4aSRandall Stewart 		/*
1767f8829a4aSRandall Stewart 		 * since we did not send a HB make sure we don't double
1768f8829a4aSRandall Stewart 		 * things
1769f8829a4aSRandall Stewart 		 */
1770f8829a4aSRandall Stewart 		net->hb_responded = 1;
1771f8829a4aSRandall Stewart 		if (stcb->asoc.sctp_autoclose_ticks &&
1772f8829a4aSRandall Stewart 		    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1773f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1774f8829a4aSRandall Stewart 			    NULL);
1775f8829a4aSRandall Stewart 		}
1776f8829a4aSRandall Stewart 		asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
17777f34832bSRandall Stewart 		asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1778f8829a4aSRandall Stewart 
17797f34832bSRandall Stewart 		if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
17807f34832bSRandall Stewart 			/*
17817f34832bSRandall Stewart 			 * Ok the peer probably discarded our data (if we
17827f34832bSRandall Stewart 			 * echoed a cookie+data). So anything on the
17837f34832bSRandall Stewart 			 * sent_queue should be marked for retransmit, we
17847f34832bSRandall Stewart 			 * may not get something to kick us so it COULD
17857f34832bSRandall Stewart 			 * still take a timeout to move these.. but it can't
17867f34832bSRandall Stewart 			 * hurt to mark them.
17877f34832bSRandall Stewart 			 */
17887f34832bSRandall Stewart 			struct sctp_tmit_chunk *chk;
17897f34832bSRandall Stewart 
17907f34832bSRandall Stewart 			TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
17917f34832bSRandall Stewart 				if (chk->sent < SCTP_DATAGRAM_RESEND) {
17927f34832bSRandall Stewart 					chk->sent = SCTP_DATAGRAM_RESEND;
1793b54d3a6cSRandall Stewart 					sctp_flight_size_decrease(chk);
1794b54d3a6cSRandall Stewart 					sctp_total_flight_decrease(stcb, chk);
17955e54f665SRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
17967f34832bSRandall Stewart 					spec_flag++;
17977f34832bSRandall Stewart 				}
17987f34832bSRandall Stewart 			}
17997f34832bSRandall Stewart 
18007f34832bSRandall Stewart 		}
1801f8829a4aSRandall Stewart 		/* process the INIT info (peer's info) */
18027215cc1bSMichael Tuexen 		retval = sctp_process_init(init_cp, stcb);
1803f8829a4aSRandall Stewart 		if (retval < 0) {
180444b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
180544b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 9;
1806f8829a4aSRandall Stewart 			return (NULL);
1807f8829a4aSRandall Stewart 		}
18087215cc1bSMichael Tuexen 		if (sctp_load_addresses_from_init(stcb, m,
1809f8829a4aSRandall Stewart 		    init_offset + sizeof(struct sctp_init_chunk),
1810b1754ad1SMichael Tuexen 		    initack_offset, src, dst, init_src)) {
181144b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
181244b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 10;
1813f8829a4aSRandall Stewart 			return (NULL);
1814f8829a4aSRandall Stewart 		}
1815f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1816f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1817f8829a4aSRandall Stewart 			*notification = SCTP_NOTIFY_ASSOC_UP;
1818f8829a4aSRandall Stewart 
1819f8829a4aSRandall Stewart 			if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1820f8829a4aSRandall Stewart 			    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1821f8829a4aSRandall Stewart 			    (inp->sctp_socket->so_qlimit == 0)) {
1822ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1823ceaad40aSRandall Stewart 				struct socket *so;
1824ceaad40aSRandall Stewart 
1825ceaad40aSRandall Stewart #endif
1826f8829a4aSRandall Stewart 				stcb->sctp_ep->sctp_flags |=
1827f8829a4aSRandall Stewart 				    SCTP_PCB_FLAGS_CONNECTED;
1828ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1829ceaad40aSRandall Stewart 				so = SCTP_INP_SO(stcb->sctp_ep);
1830ceaad40aSRandall Stewart 				atomic_add_int(&stcb->asoc.refcnt, 1);
1831ceaad40aSRandall Stewart 				SCTP_TCB_UNLOCK(stcb);
1832ceaad40aSRandall Stewart 				SCTP_SOCKET_LOCK(so, 1);
1833ceaad40aSRandall Stewart 				SCTP_TCB_LOCK(stcb);
1834ceaad40aSRandall Stewart 				atomic_add_int(&stcb->asoc.refcnt, -1);
1835ceaad40aSRandall Stewart 				if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1836ceaad40aSRandall Stewart 					SCTP_SOCKET_UNLOCK(so, 1);
1837ceaad40aSRandall Stewart 					return (NULL);
1838ceaad40aSRandall Stewart 				}
1839ceaad40aSRandall Stewart #endif
1840ceaad40aSRandall Stewart 				soisconnected(stcb->sctp_socket);
1841ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1842ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
1843ceaad40aSRandall Stewart #endif
1844f8829a4aSRandall Stewart 			}
1845f42a358aSRandall Stewart 			if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1846f42a358aSRandall Stewart 				SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1847f42a358aSRandall Stewart 			else
1848f42a358aSRandall Stewart 				SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1849f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1850f42a358aSRandall Stewart 		} else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1851f42a358aSRandall Stewart 			SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1852f42a358aSRandall Stewart 		} else {
1853f42a358aSRandall Stewart 			SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1854f8829a4aSRandall Stewart 		}
1855c4739e2fSRandall Stewart 		SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1856f8829a4aSRandall Stewart 		if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1857f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1858f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, asoc->primary_destination);
1859f8829a4aSRandall Stewart 		}
1860f8829a4aSRandall Stewart 		sctp_stop_all_cookie_timers(stcb);
1861a5d547adSRandall Stewart 		sctp_toss_old_cookies(stcb, asoc);
1862f8829a4aSRandall Stewart 		sctp_send_cookie_ack(stcb);
18637f34832bSRandall Stewart 		if (spec_flag) {
18647f34832bSRandall Stewart 			/*
18657f34832bSRandall Stewart 			 * only if we have retrans set do we do this. What
18667f34832bSRandall Stewart 			 * this call does is get only the COOKIE-ACK out and
18677f34832bSRandall Stewart 			 * then when we return the normal call to
18687f34832bSRandall Stewart 			 * sctp_chunk_output will get the retrans out behind
18697f34832bSRandall Stewart 			 * this.
18707f34832bSRandall Stewart 			 */
1871ceaad40aSRandall Stewart 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
18727f34832bSRandall Stewart 		}
187344b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
187444b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 11;
187544b7479bSRandall Stewart 
1876f8829a4aSRandall Stewart 		return (stcb);
1877f8829a4aSRandall Stewart 	}
1878f8829a4aSRandall Stewart 	if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1879f8829a4aSRandall Stewart 	    ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1880f8829a4aSRandall Stewart 	    cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1881f8829a4aSRandall Stewart 	    cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1882f8829a4aSRandall Stewart 	    cookie->tie_tag_peer_vtag != 0) {
1883f8829a4aSRandall Stewart 		struct sctpasochead *head;
1884f8829a4aSRandall Stewart 
188556f778aaSMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
188656f778aaSMichael Tuexen 		struct socket *so;
188756f778aaSMichael Tuexen 
188856f778aaSMichael Tuexen #endif
188956f778aaSMichael Tuexen 
1890830d754dSRandall Stewart 		if (asoc->peer_supports_nat) {
1891830d754dSRandall Stewart 			/*
189256f778aaSMichael Tuexen 			 * This is a gross gross hack. Just call the
1893830d754dSRandall Stewart 			 * cookie_new code since we are allowing a duplicate
1894830d754dSRandall Stewart 			 * association. I hope this works...
1895830d754dSRandall Stewart 			 */
1896b1754ad1SMichael Tuexen 			return (sctp_process_cookie_new(m, iphlen, offset, src, dst,
1897b1754ad1SMichael Tuexen 			    sh, cookie, cookie_len,
1898830d754dSRandall Stewart 			    inp, netp, init_src, notification,
1899830d754dSRandall Stewart 			    auth_skipped, auth_offset, auth_len,
1900457b4b88SMichael Tuexen 			    mflowtype, mflowid,
1901830d754dSRandall Stewart 			    vrf_id, port));
1902830d754dSRandall Stewart 		}
1903f8829a4aSRandall Stewart 		/*
1904f8829a4aSRandall Stewart 		 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1905f8829a4aSRandall Stewart 		 */
1906a5d547adSRandall Stewart 		/* temp code */
190744b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
190844b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 12;
1909b7d130beSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net,
1910b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1911b7d130beSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1912b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
1913139bc87fSRandall Stewart 
1914f8829a4aSRandall Stewart 		/* notify upper layer */
1915f8829a4aSRandall Stewart 		*notification = SCTP_NOTIFY_ASSOC_RESTART;
1916a5d547adSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
1917f42a358aSRandall Stewart 		if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) &&
1918f42a358aSRandall Stewart 		    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1919f42a358aSRandall Stewart 		    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
1920f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1921f42a358aSRandall Stewart 		}
1922f42a358aSRandall Stewart 		if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1923f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1924f42a358aSRandall Stewart 		} else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1925f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1926f42a358aSRandall Stewart 		}
1927139bc87fSRandall Stewart 		if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1928c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1929139bc87fSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1930139bc87fSRandall Stewart 			    stcb->sctp_ep, stcb, asoc->primary_destination);
1931139bc87fSRandall Stewart 
1932139bc87fSRandall Stewart 		} else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1933139bc87fSRandall Stewart 			/* move to OPEN state, if not in SHUTDOWN_SENT */
1934c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1935139bc87fSRandall Stewart 		}
1936139bc87fSRandall Stewart 		asoc->pre_open_streams =
1937139bc87fSRandall Stewart 		    ntohs(initack_cp->init.num_outbound_streams);
1938139bc87fSRandall Stewart 		asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1939139bc87fSRandall Stewart 		asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1940c54a18d2SRandall Stewart 		asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1941139bc87fSRandall Stewart 
1942139bc87fSRandall Stewart 		asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1943139bc87fSRandall Stewart 
1944139bc87fSRandall Stewart 		asoc->str_reset_seq_in = asoc->init_seq_number;
1945139bc87fSRandall Stewart 
1946139bc87fSRandall Stewart 		asoc->advanced_peer_ack_point = asoc->last_acked_seq;
19470696e120SRandall Stewart 		if (asoc->mapping_array) {
1948139bc87fSRandall Stewart 			memset(asoc->mapping_array, 0,
1949139bc87fSRandall Stewart 			    asoc->mapping_array_size);
19500696e120SRandall Stewart 		}
195177acdc25SRandall Stewart 		if (asoc->nr_mapping_array) {
1952830d754dSRandall Stewart 			memset(asoc->nr_mapping_array, 0,
1953b5c16493SMichael Tuexen 			    asoc->mapping_array_size);
1954830d754dSRandall Stewart 		}
1955a5d547adSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
195656f778aaSMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
195756f778aaSMichael Tuexen 		so = SCTP_INP_SO(stcb->sctp_ep);
195856f778aaSMichael Tuexen 		SCTP_SOCKET_LOCK(so, 1);
195956f778aaSMichael Tuexen #endif
1960a5d547adSRandall Stewart 		SCTP_INP_INFO_WLOCK();
1961a5d547adSRandall Stewart 		SCTP_INP_WLOCK(stcb->sctp_ep);
1962a5d547adSRandall Stewart 		SCTP_TCB_LOCK(stcb);
1963a5d547adSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, -1);
1964f8829a4aSRandall Stewart 		/* send up all the data */
19657f34832bSRandall Stewart 		SCTP_TCB_SEND_LOCK(stcb);
1966f8829a4aSRandall Stewart 
196756f778aaSMichael Tuexen 		sctp_report_all_outbound(stcb, 0, 1, SCTP_SO_LOCKED);
1968a5d547adSRandall Stewart 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1969325c8c46SMichael Tuexen 			stcb->asoc.strmout[i].chunks_on_queues = 0;
1970f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
1971f0396ad1SMichael Tuexen 			for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1972f0396ad1SMichael Tuexen 				asoc->strmout[i].abandoned_sent[j] = 0;
1973f0396ad1SMichael Tuexen 				asoc->strmout[i].abandoned_unsent[j] = 0;
1974f0396ad1SMichael Tuexen 			}
1975f0396ad1SMichael Tuexen #else
1976f0396ad1SMichael Tuexen 			asoc->strmout[i].abandoned_sent[0] = 0;
1977f0396ad1SMichael Tuexen 			asoc->strmout[i].abandoned_unsent[0] = 0;
1978f0396ad1SMichael Tuexen #endif
1979a5d547adSRandall Stewart 			stcb->asoc.strmout[i].stream_no = i;
1980f3b05218SMichael Tuexen 			stcb->asoc.strmout[i].next_sequence_send = 0;
1981a5d547adSRandall Stewart 			stcb->asoc.strmout[i].last_msg_incomplete = 0;
1982a5d547adSRandall Stewart 		}
1983f8829a4aSRandall Stewart 		/* process the INIT-ACK info (my info) */
1984f8829a4aSRandall Stewart 		asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1985f8829a4aSRandall Stewart 		asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1986f8829a4aSRandall Stewart 
1987f8829a4aSRandall Stewart 		/* pull from vtag hash */
1988f8829a4aSRandall Stewart 		LIST_REMOVE(stcb, sctp_asocs);
1989f8829a4aSRandall Stewart 		/* re-insert to new vtag position */
1990b3f1ea41SRandall Stewart 		head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1991b3f1ea41SRandall Stewart 		    SCTP_BASE_INFO(hashasocmark))];
1992f8829a4aSRandall Stewart 		/*
1993f8829a4aSRandall Stewart 		 * put it in the bucket in the vtag hash of assoc's for the
1994f8829a4aSRandall Stewart 		 * system
1995f8829a4aSRandall Stewart 		 */
1996f8829a4aSRandall Stewart 		LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1997f8829a4aSRandall Stewart 
19987f34832bSRandall Stewart 		SCTP_TCB_SEND_UNLOCK(stcb);
1999a5d547adSRandall Stewart 		SCTP_INP_WUNLOCK(stcb->sctp_ep);
2000a5d547adSRandall Stewart 		SCTP_INP_INFO_WUNLOCK();
200156f778aaSMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
200256f778aaSMichael Tuexen 		SCTP_SOCKET_UNLOCK(so, 1);
200356f778aaSMichael Tuexen #endif
200456f778aaSMichael Tuexen 		asoc->total_flight = 0;
200556f778aaSMichael Tuexen 		asoc->total_flight_count = 0;
200656f778aaSMichael Tuexen 		/* process the INIT info (peer's info) */
20077215cc1bSMichael Tuexen 		retval = sctp_process_init(init_cp, stcb);
2008f8829a4aSRandall Stewart 		if (retval < 0) {
200944b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
201044b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 13;
201144b7479bSRandall Stewart 
2012f8829a4aSRandall Stewart 			return (NULL);
2013f8829a4aSRandall Stewart 		}
2014f8829a4aSRandall Stewart 		/*
2015f8829a4aSRandall Stewart 		 * since we did not send a HB make sure we don't double
2016f8829a4aSRandall Stewart 		 * things
2017f8829a4aSRandall Stewart 		 */
2018f8829a4aSRandall Stewart 		net->hb_responded = 1;
2019f8829a4aSRandall Stewart 
20207215cc1bSMichael Tuexen 		if (sctp_load_addresses_from_init(stcb, m,
2021f8829a4aSRandall Stewart 		    init_offset + sizeof(struct sctp_init_chunk),
2022b1754ad1SMichael Tuexen 		    initack_offset, src, dst, init_src)) {
202344b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
202444b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 14;
202544b7479bSRandall Stewart 
2026f8829a4aSRandall Stewart 			return (NULL);
2027f8829a4aSRandall Stewart 		}
2028f8829a4aSRandall Stewart 		/* respond with a COOKIE-ACK */
2029f8829a4aSRandall Stewart 		sctp_stop_all_cookie_timers(stcb);
2030a5d547adSRandall Stewart 		sctp_toss_old_cookies(stcb, asoc);
2031f8829a4aSRandall Stewart 		sctp_send_cookie_ack(stcb);
203244b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
203344b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 15;
2034f8829a4aSRandall Stewart 
2035f8829a4aSRandall Stewart 		return (stcb);
2036f8829a4aSRandall Stewart 	}
203744b7479bSRandall Stewart 	if (how_indx < sizeof(asoc->cookie_how))
203844b7479bSRandall Stewart 		asoc->cookie_how[how_indx] = 16;
2039f8829a4aSRandall Stewart 	/* all other cases... */
2040f8829a4aSRandall Stewart 	return (NULL);
2041f8829a4aSRandall Stewart }
2042f8829a4aSRandall Stewart 
204393164cf9SRandall Stewart 
2044f8829a4aSRandall Stewart /*
2045f8829a4aSRandall Stewart  * handle a state cookie for a new association m: input packet mbuf chain--
2046f8829a4aSRandall Stewart  * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
2047f8829a4aSRandall Stewart  * and the cookie signature does not exist offset: offset into mbuf to the
2048f8829a4aSRandall Stewart  * cookie-echo chunk length: length of the cookie chunk to: where the init
2049f8829a4aSRandall Stewart  * was from returns a new TCB
2050f8829a4aSRandall Stewart  */
2051f30ac432SMichael Tuexen static struct sctp_tcb *
2052f8829a4aSRandall Stewart sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
2053b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
2054f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
2055f8829a4aSRandall Stewart     struct sctp_inpcb *inp, struct sctp_nets **netp,
2056f8829a4aSRandall Stewart     struct sockaddr *init_src, int *notification,
205717205eccSRandall Stewart     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2058457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
2059c54a18d2SRandall Stewart     uint32_t vrf_id, uint16_t port)
2060f8829a4aSRandall Stewart {
2061f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
2062f8829a4aSRandall Stewart 	struct sctp_init_chunk *init_cp, init_buf;
2063f8829a4aSRandall Stewart 	struct sctp_init_ack_chunk *initack_cp, initack_buf;
206424aaac8dSMichael Tuexen 	union sctp_sockstore store;
2065f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2066f8829a4aSRandall Stewart 	int init_offset, initack_offset, initack_limit;
2067f8829a4aSRandall Stewart 	int retval;
2068f8829a4aSRandall Stewart 	int error = 0;
2069f42a358aSRandall Stewart 	uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
2070f8829a4aSRandall Stewart 
2071ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2072ceaad40aSRandall Stewart 	struct socket *so;
2073ceaad40aSRandall Stewart 
2074ceaad40aSRandall Stewart 	so = SCTP_INP_SO(inp);
2075ceaad40aSRandall Stewart #endif
2076ceaad40aSRandall Stewart 
2077f8829a4aSRandall Stewart 	/*
2078f8829a4aSRandall Stewart 	 * find and validate the INIT chunk in the cookie (peer's info) the
2079f8829a4aSRandall Stewart 	 * INIT should start after the cookie-echo header struct (chunk
2080f8829a4aSRandall Stewart 	 * header, state cookie header struct)
2081f8829a4aSRandall Stewart 	 */
2082f8829a4aSRandall Stewart 	init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
2083f8829a4aSRandall Stewart 	init_cp = (struct sctp_init_chunk *)
2084f8829a4aSRandall Stewart 	    sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
2085f8829a4aSRandall Stewart 	    (uint8_t *) & init_buf);
2086f8829a4aSRandall Stewart 	if (init_cp == NULL) {
2087f8829a4aSRandall Stewart 		/* could not pull a INIT chunk in cookie */
2088ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1,
2089ad81507eSRandall Stewart 		    "process_cookie_new: could not pull INIT chunk hdr\n");
2090f8829a4aSRandall Stewart 		return (NULL);
2091f8829a4aSRandall Stewart 	}
2092f8829a4aSRandall Stewart 	if (init_cp->ch.chunk_type != SCTP_INITIATION) {
2093ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
2094f8829a4aSRandall Stewart 		return (NULL);
2095f8829a4aSRandall Stewart 	}
209660990c0cSMichael Tuexen 	initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
2097f8829a4aSRandall Stewart 	/*
2098f8829a4aSRandall Stewart 	 * find and validate the INIT-ACK chunk in the cookie (my info) the
2099f8829a4aSRandall Stewart 	 * INIT-ACK follows the INIT chunk
2100f8829a4aSRandall Stewart 	 */
2101f8829a4aSRandall Stewart 	initack_cp = (struct sctp_init_ack_chunk *)
2102f8829a4aSRandall Stewart 	    sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
2103f8829a4aSRandall Stewart 	    (uint8_t *) & initack_buf);
2104f8829a4aSRandall Stewart 	if (initack_cp == NULL) {
2105f8829a4aSRandall Stewart 		/* could not pull INIT-ACK chunk in cookie */
2106ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2107f8829a4aSRandall Stewart 		return (NULL);
2108f8829a4aSRandall Stewart 	}
2109f8829a4aSRandall Stewart 	if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2110f8829a4aSRandall Stewart 		return (NULL);
2111f8829a4aSRandall Stewart 	}
2112f8829a4aSRandall Stewart 	/*
2113f8829a4aSRandall Stewart 	 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2114f8829a4aSRandall Stewart 	 * "initack_limit" value.  This is because the chk_length field
2115f8829a4aSRandall Stewart 	 * includes the length of the cookie, but the cookie is omitted when
2116f8829a4aSRandall Stewart 	 * the INIT and INIT_ACK are tacked onto the cookie...
2117f8829a4aSRandall Stewart 	 */
2118f8829a4aSRandall Stewart 	initack_limit = offset + cookie_len;
2119f8829a4aSRandall Stewart 
2120f8829a4aSRandall Stewart 	/*
2121f8829a4aSRandall Stewart 	 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2122f8829a4aSRandall Stewart 	 * and popluate
2123f8829a4aSRandall Stewart 	 */
212452be287eSRandall Stewart 
212552be287eSRandall Stewart 	/*
212652be287eSRandall Stewart 	 * Here we do a trick, we set in NULL for the proc/thread argument.
212752be287eSRandall Stewart 	 * We do this since in effect we only use the p argument when the
212852be287eSRandall Stewart 	 * socket is unbound and we must do an implicit bind. Since we are
212952be287eSRandall Stewart 	 * getting a cookie, we cannot be unbound.
213052be287eSRandall Stewart 	 */
2131b5c16493SMichael Tuexen 	stcb = sctp_aloc_assoc(inp, init_src, &error,
213252be287eSRandall Stewart 	    ntohl(initack_cp->init.initiate_tag), vrf_id,
2133c979034bSMichael Tuexen 	    ntohs(initack_cp->init.num_outbound_streams),
213452be287eSRandall Stewart 	    (struct thread *)NULL
213552be287eSRandall Stewart 	    );
2136f8829a4aSRandall Stewart 	if (stcb == NULL) {
2137f8829a4aSRandall Stewart 		struct mbuf *op_err;
2138f8829a4aSRandall Stewart 
2139f8829a4aSRandall Stewart 		/* memory problem? */
2140ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1,
2141ad81507eSRandall Stewart 		    "process_cookie_new: no room for another TCB!\n");
2142ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2143f8829a4aSRandall Stewart 		sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2144b1754ad1SMichael Tuexen 		    src, dst, sh, op_err,
2145457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2146f30ac432SMichael Tuexen 		    vrf_id, port);
2147f8829a4aSRandall Stewart 		return (NULL);
2148f8829a4aSRandall Stewart 	}
2149f8829a4aSRandall Stewart 	/* get the correct sctp_nets */
2150ad81507eSRandall Stewart 	if (netp)
2151f8829a4aSRandall Stewart 		*netp = sctp_findnet(stcb, init_src);
2152ad81507eSRandall Stewart 
2153f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2154f8829a4aSRandall Stewart 	/* get scope variables out of cookie */
2155a1cb341bSMichael Tuexen 	asoc->scope.ipv4_local_scope = cookie->ipv4_scope;
2156a1cb341bSMichael Tuexen 	asoc->scope.site_scope = cookie->site_scope;
2157a1cb341bSMichael Tuexen 	asoc->scope.local_scope = cookie->local_scope;
2158a1cb341bSMichael Tuexen 	asoc->scope.loopback_scope = cookie->loopback_scope;
2159f8829a4aSRandall Stewart 
2160a1cb341bSMichael Tuexen 	if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2161a1cb341bSMichael Tuexen 	    (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2162f8829a4aSRandall Stewart 		struct mbuf *op_err;
2163f8829a4aSRandall Stewart 
2164f8829a4aSRandall Stewart 		/*
2165f8829a4aSRandall Stewart 		 * Houston we have a problem. The EP changed while the
2166f8829a4aSRandall Stewart 		 * cookie was in flight. Only recourse is to abort the
2167f8829a4aSRandall Stewart 		 * association.
2168f8829a4aSRandall Stewart 		 */
21694c9179adSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
2170ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2171f8829a4aSRandall Stewart 		sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2172b1754ad1SMichael Tuexen 		    src, dst, sh, op_err,
2173457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2174f30ac432SMichael Tuexen 		    vrf_id, port);
2175ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2176ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
2177ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
2178ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
2179ceaad40aSRandall Stewart #endif
2180c4739e2fSRandall Stewart 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2181b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2182ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2183ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
2184ceaad40aSRandall Stewart #endif
2185ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
2186f8829a4aSRandall Stewart 		return (NULL);
2187f8829a4aSRandall Stewart 	}
2188f8829a4aSRandall Stewart 	/* process the INIT-ACK info (my info) */
2189830d754dSRandall Stewart 	asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2190f8829a4aSRandall Stewart 	asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2191f8829a4aSRandall Stewart 	asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2192f8829a4aSRandall Stewart 	asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2193f8829a4aSRandall Stewart 	asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2194c54a18d2SRandall Stewart 	asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2195f8829a4aSRandall Stewart 	asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2196f8829a4aSRandall Stewart 	asoc->str_reset_seq_in = asoc->init_seq_number;
2197f8829a4aSRandall Stewart 
2198f8829a4aSRandall Stewart 	asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2199f8829a4aSRandall Stewart 
2200f8829a4aSRandall Stewart 	/* process the INIT info (peer's info) */
2201ad81507eSRandall Stewart 	if (netp)
22027215cc1bSMichael Tuexen 		retval = sctp_process_init(init_cp, stcb);
2203ad81507eSRandall Stewart 	else
2204ad81507eSRandall Stewart 		retval = 0;
2205f8829a4aSRandall Stewart 	if (retval < 0) {
22064c9179adSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
2207ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2208ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
2209ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
2210ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
2211ceaad40aSRandall Stewart #endif
2212b7d130beSMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2213b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2214ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2215ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
2216ceaad40aSRandall Stewart #endif
2217ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
2218f8829a4aSRandall Stewart 		return (NULL);
2219f8829a4aSRandall Stewart 	}
2220f8829a4aSRandall Stewart 	/* load all addresses */
22217215cc1bSMichael Tuexen 	if (sctp_load_addresses_from_init(stcb, m,
2222b1754ad1SMichael Tuexen 	    init_offset + sizeof(struct sctp_init_chunk), initack_offset,
2223b1754ad1SMichael Tuexen 	    src, dst, init_src)) {
22244c9179adSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
2225ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2226ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
2227ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
2228ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
2229ceaad40aSRandall Stewart #endif
2230b7d130beSMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2231b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2232ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2233ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
2234ceaad40aSRandall Stewart #endif
2235ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
2236f8829a4aSRandall Stewart 		return (NULL);
2237f8829a4aSRandall Stewart 	}
2238f8829a4aSRandall Stewart 	/*
2239f8829a4aSRandall Stewart 	 * verify any preceding AUTH chunk that was skipped
2240f8829a4aSRandall Stewart 	 */
2241f8829a4aSRandall Stewart 	/* pull the local authentication parameters from the cookie/init-ack */
2242f8829a4aSRandall Stewart 	sctp_auth_get_cookie_params(stcb, m,
2243f8829a4aSRandall Stewart 	    initack_offset + sizeof(struct sctp_init_ack_chunk),
2244f8829a4aSRandall Stewart 	    initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2245f8829a4aSRandall Stewart 	if (auth_skipped) {
2246f8829a4aSRandall Stewart 		struct sctp_auth_chunk *auth;
2247f8829a4aSRandall Stewart 
2248f8829a4aSRandall Stewart 		auth = (struct sctp_auth_chunk *)
2249f42a358aSRandall Stewart 		    sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2250ad81507eSRandall Stewart 		if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2251f8829a4aSRandall Stewart 			/* auth HMAC failed, dump the assoc and packet */
2252ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_AUTH1,
2253ad81507eSRandall Stewart 			    "COOKIE-ECHO: AUTH failed\n");
2254b54d3a6cSRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, 1);
2255ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2256ceaad40aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
2257ceaad40aSRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
2258ceaad40aSRandall Stewart 			SCTP_TCB_LOCK(stcb);
2259ceaad40aSRandall Stewart #endif
2260b7d130beSMichael Tuexen 			(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2261b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_21);
2262ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2263ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
2264ceaad40aSRandall Stewart #endif
2265ceaad40aSRandall Stewart 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
2266f8829a4aSRandall Stewart 			return (NULL);
2267f8829a4aSRandall Stewart 		} else {
2268f8829a4aSRandall Stewart 			/* remaining chunks checked... good to go */
2269f8829a4aSRandall Stewart 			stcb->asoc.authenticated = 1;
2270f8829a4aSRandall Stewart 		}
2271f8829a4aSRandall Stewart 	}
2272f8829a4aSRandall Stewart 	/* update current state */
2273ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2274c4739e2fSRandall Stewart 	SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2275f8829a4aSRandall Stewart 	if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2276f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2277f8829a4aSRandall Stewart 		    stcb->sctp_ep, stcb, asoc->primary_destination);
2278f8829a4aSRandall Stewart 	}
2279a5d547adSRandall Stewart 	sctp_stop_all_cookie_timers(stcb);
2280f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2281f8829a4aSRandall Stewart 	SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2282f8829a4aSRandall Stewart 
2283f8829a4aSRandall Stewart 	/*
2284f8829a4aSRandall Stewart 	 * if we're doing ASCONFs, check to see if we have any new local
2285f8829a4aSRandall Stewart 	 * addresses that need to get added to the peer (eg. addresses
2286f8829a4aSRandall Stewart 	 * changed while cookie echo in flight).  This needs to be done
2287f8829a4aSRandall Stewart 	 * after we go to the OPEN state to do the correct asconf
2288f8829a4aSRandall Stewart 	 * processing. else, make sure we have the correct addresses in our
2289f8829a4aSRandall Stewart 	 * lists
2290f8829a4aSRandall Stewart 	 */
2291f8829a4aSRandall Stewart 
2292f8829a4aSRandall Stewart 	/* warning, we re-use sin, sin6, sa_store here! */
2293f8829a4aSRandall Stewart 	/* pull in local_address (our "from" address) */
2294e6194c2eSMichael Tuexen 	switch (cookie->laddr_type) {
2295e6194c2eSMichael Tuexen #ifdef INET
2296e6194c2eSMichael Tuexen 	case SCTP_IPV4_ADDRESS:
2297f8829a4aSRandall Stewart 		/* source addr is IPv4 */
229824aaac8dSMichael Tuexen 		memset(&store.sin, 0, sizeof(struct sockaddr_in));
229924aaac8dSMichael Tuexen 		store.sin.sin_family = AF_INET;
230024aaac8dSMichael Tuexen 		store.sin.sin_len = sizeof(struct sockaddr_in);
230124aaac8dSMichael Tuexen 		store.sin.sin_addr.s_addr = cookie->laddress[0];
2302e6194c2eSMichael Tuexen 		break;
2303e6194c2eSMichael Tuexen #endif
2304e6194c2eSMichael Tuexen #ifdef INET6
2305e6194c2eSMichael Tuexen 	case SCTP_IPV6_ADDRESS:
2306f8829a4aSRandall Stewart 		/* source addr is IPv6 */
230724aaac8dSMichael Tuexen 		memset(&store.sin6, 0, sizeof(struct sockaddr_in6));
230824aaac8dSMichael Tuexen 		store.sin6.sin6_family = AF_INET6;
230924aaac8dSMichael Tuexen 		store.sin6.sin6_len = sizeof(struct sockaddr_in6);
231024aaac8dSMichael Tuexen 		store.sin6.sin6_scope_id = cookie->scope_id;
231123602b60SMichael Tuexen 		memcpy(&store.sin6.sin6_addr, cookie->laddress, sizeof(struct in6_addr));
2312e6194c2eSMichael Tuexen 		break;
2313e6194c2eSMichael Tuexen #endif
2314e6194c2eSMichael Tuexen 	default:
23154c9179adSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
2316ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2317ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
2318ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
2319ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
2320ceaad40aSRandall Stewart #endif
2321b7d130beSMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2322b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
2323ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2324ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
2325ceaad40aSRandall Stewart #endif
2326ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
2327f8829a4aSRandall Stewart 		return (NULL);
2328f8829a4aSRandall Stewart 	}
2329f8829a4aSRandall Stewart 
2330f8829a4aSRandall Stewart 	/* set up to notify upper layer */
2331f8829a4aSRandall Stewart 	*notification = SCTP_NOTIFY_ASSOC_UP;
2332f8829a4aSRandall Stewart 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2333f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2334f8829a4aSRandall Stewart 	    (inp->sctp_socket->so_qlimit == 0)) {
2335f8829a4aSRandall Stewart 		/*
2336f8829a4aSRandall Stewart 		 * This is an endpoint that called connect() how it got a
2337f8829a4aSRandall Stewart 		 * cookie that is NEW is a bit of a mystery. It must be that
2338f8829a4aSRandall Stewart 		 * the INIT was sent, but before it got there.. a complete
2339f8829a4aSRandall Stewart 		 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2340f8829a4aSRandall Stewart 		 * should have went to the other code.. not here.. oh well..
2341f8829a4aSRandall Stewart 		 * a bit of protection is worth having..
2342f8829a4aSRandall Stewart 		 */
2343f8829a4aSRandall Stewart 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2344ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2345ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
2346ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
2347ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
2348ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
2349ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
2350ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2351ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
2352ceaad40aSRandall Stewart 			return (NULL);
2353ceaad40aSRandall Stewart 		}
2354ceaad40aSRandall Stewart #endif
2355ceaad40aSRandall Stewart 		soisconnected(stcb->sctp_socket);
2356ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2357ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
2358ceaad40aSRandall Stewart #endif
2359f8829a4aSRandall Stewart 	} else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2360f8829a4aSRandall Stewart 	    (inp->sctp_socket->so_qlimit)) {
2361f8829a4aSRandall Stewart 		/*
2362f8829a4aSRandall Stewart 		 * We don't want to do anything with this one. Since it is
2363f8829a4aSRandall Stewart 		 * the listening guy. The timer will get started for
2364f8829a4aSRandall Stewart 		 * accepted connections in the caller.
2365f8829a4aSRandall Stewart 		 */
2366f8829a4aSRandall Stewart 		;
2367f8829a4aSRandall Stewart 	}
2368f8829a4aSRandall Stewart 	/* since we did not send a HB make sure we don't double things */
2369ad81507eSRandall Stewart 	if ((netp) && (*netp))
2370f8829a4aSRandall Stewart 		(*netp)->hb_responded = 1;
2371f8829a4aSRandall Stewart 
2372f8829a4aSRandall Stewart 	if (stcb->asoc.sctp_autoclose_ticks &&
2373f8829a4aSRandall Stewart 	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2374f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2375f8829a4aSRandall Stewart 	}
2376b54d3a6cSRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
237760862d8eSMichael Tuexen 	if ((netp != NULL) && (*netp != NULL)) {
2378fcbbf5afSMichael Tuexen 		/* calculate the RTT and set the encaps port */
2379a5d547adSRandall Stewart 		(*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
2380899288aeSRandall Stewart 		    &cookie->time_entered, sctp_align_unsafe_makecopy,
2381f79aab18SRandall Stewart 		    SCTP_RTT_FROM_NON_DATA);
238260862d8eSMichael Tuexen #if defined(INET) || defined(INET6)
238360862d8eSMichael Tuexen 		if (((*netp)->port == 0) && (port != 0)) {
23849a8e3088SMichael Tuexen 			sctp_pathmtu_adjustment(stcb, (uint16_t) ((*netp)->mtu - sizeof(struct udphdr)));
238560862d8eSMichael Tuexen 		}
2386fcbbf5afSMichael Tuexen 		(*netp)->port = port;
238760862d8eSMichael Tuexen #endif
23889a972525SRandall Stewart 	}
23893232788eSRandall Stewart 	/* respond with a COOKIE-ACK */
2390f8829a4aSRandall Stewart 	sctp_send_cookie_ack(stcb);
23913232788eSRandall Stewart 
23923232788eSRandall Stewart 	/*
23933232788eSRandall Stewart 	 * check the address lists for any ASCONFs that need to be sent
23943232788eSRandall Stewart 	 * AFTER the cookie-ack is sent
23953232788eSRandall Stewart 	 */
23963232788eSRandall Stewart 	sctp_check_address_list(stcb, m,
23973232788eSRandall Stewart 	    initack_offset + sizeof(struct sctp_init_ack_chunk),
23983232788eSRandall Stewart 	    initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
239924aaac8dSMichael Tuexen 	    &store.sa, cookie->local_scope, cookie->site_scope,
24003232788eSRandall Stewart 	    cookie->ipv4_scope, cookie->loopback_scope);
24013232788eSRandall Stewart 
24023232788eSRandall Stewart 
2403f8829a4aSRandall Stewart 	return (stcb);
2404f8829a4aSRandall Stewart }
2405f8829a4aSRandall Stewart 
2406830d754dSRandall Stewart /*
2407830d754dSRandall Stewart  * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2408830d754dSRandall Stewart  * we NEED to make sure we are not already using the vtag. If so we
2409830d754dSRandall Stewart  * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2410830d754dSRandall Stewart 	head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2411830d754dSRandall Stewart 							    SCTP_BASE_INFO(hashasocmark))];
2412830d754dSRandall Stewart 	LIST_FOREACH(stcb, head, sctp_asocs) {
2413830d754dSRandall Stewart 	        if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep))  {
2414830d754dSRandall Stewart 		       -- SEND ABORT - TRY AGAIN --
2415830d754dSRandall Stewart 		}
2416830d754dSRandall Stewart 	}
2417830d754dSRandall Stewart */
2418f8829a4aSRandall Stewart 
2419f8829a4aSRandall Stewart /*
2420f8829a4aSRandall Stewart  * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2421f8829a4aSRandall Stewart  * existing (non-NULL) TCB
2422f8829a4aSRandall Stewart  */
2423f8829a4aSRandall Stewart static struct mbuf *
2424f8829a4aSRandall Stewart sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2425b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
2426f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2427f8829a4aSRandall Stewart     struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
242817205eccSRandall Stewart     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2429f30ac432SMichael Tuexen     struct sctp_tcb **locked_tcb,
2430457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
2431f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
2432f8829a4aSRandall Stewart {
2433f8829a4aSRandall Stewart 	struct sctp_state_cookie *cookie;
2434f8829a4aSRandall Stewart 	struct sctp_tcb *l_stcb = *stcb;
2435f8829a4aSRandall Stewart 	struct sctp_inpcb *l_inp;
2436f8829a4aSRandall Stewart 	struct sockaddr *to;
2437f8829a4aSRandall Stewart 	struct sctp_pcb *ep;
2438f8829a4aSRandall Stewart 	struct mbuf *m_sig;
2439f8829a4aSRandall Stewart 	uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2440f8829a4aSRandall Stewart 	uint8_t *sig;
2441f8829a4aSRandall Stewart 	uint8_t cookie_ok = 0;
2442329204ffSMichael Tuexen 	unsigned int sig_offset, cookie_offset;
2443f8829a4aSRandall Stewart 	unsigned int cookie_len;
2444f8829a4aSRandall Stewart 	struct timeval now;
2445f8829a4aSRandall Stewart 	struct timeval time_expires;
2446f8829a4aSRandall Stewart 	int notification = 0;
2447f8829a4aSRandall Stewart 	struct sctp_nets *netl;
2448f8829a4aSRandall Stewart 	int had_a_existing_tcb = 0;
24492fad0e55SMichael Tuexen 	int send_int_conf = 0;
2450f8829a4aSRandall Stewart 
2451e6194c2eSMichael Tuexen #ifdef INET
2452e6194c2eSMichael Tuexen 	struct sockaddr_in sin;
2453e6194c2eSMichael Tuexen 
2454e6194c2eSMichael Tuexen #endif
2455e6194c2eSMichael Tuexen #ifdef INET6
2456e6194c2eSMichael Tuexen 	struct sockaddr_in6 sin6;
2457e6194c2eSMichael Tuexen 
2458e6194c2eSMichael Tuexen #endif
2459e6194c2eSMichael Tuexen 
2460ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
2461ad81507eSRandall Stewart 	    "sctp_handle_cookie: handling COOKIE-ECHO\n");
2462f8829a4aSRandall Stewart 
2463f8829a4aSRandall Stewart 	if (inp_p == NULL) {
2464f8829a4aSRandall Stewart 		return (NULL);
2465f8829a4aSRandall Stewart 	}
2466f8829a4aSRandall Stewart 	cookie = &cp->cookie;
2467f8829a4aSRandall Stewart 	cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2468f8829a4aSRandall Stewart 	cookie_len = ntohs(cp->ch.chunk_length);
2469f8829a4aSRandall Stewart 
24703db4ea95SMichael Tuexen 	if ((cookie->peerport != sh->src_port) ||
24713db4ea95SMichael Tuexen 	    (cookie->myport != sh->dest_port) ||
2472f8829a4aSRandall Stewart 	    (cookie->my_vtag != sh->v_tag)) {
2473f8829a4aSRandall Stewart 		/*
2474f8829a4aSRandall Stewart 		 * invalid ports or bad tag.  Note that we always leave the
2475f8829a4aSRandall Stewart 		 * v_tag in the header in network order and when we stored
2476f8829a4aSRandall Stewart 		 * it in the my_vtag slot we also left it in network order.
247717205eccSRandall Stewart 		 * This maintains the match even though it may be in the
2478f8829a4aSRandall Stewart 		 * opposite byte order of the machine :->
2479f8829a4aSRandall Stewart 		 */
2480f8829a4aSRandall Stewart 		return (NULL);
2481f8829a4aSRandall Stewart 	}
2482329204ffSMichael Tuexen 	if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2483f8829a4aSRandall Stewart 	    sizeof(struct sctp_init_chunk) +
2484f8829a4aSRandall Stewart 	    sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2485329204ffSMichael Tuexen 		/* cookie too small */
2486f8829a4aSRandall Stewart 		return (NULL);
2487f8829a4aSRandall Stewart 	}
2488f8829a4aSRandall Stewart 	/*
2489f8829a4aSRandall Stewart 	 * split off the signature into its own mbuf (since it should not be
2490f8829a4aSRandall Stewart 	 * calculated in the sctp_hmac_m() call).
2491f8829a4aSRandall Stewart 	 */
2492f8829a4aSRandall Stewart 	sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2493eb1b1807SGleb Smirnoff 	m_sig = m_split(m, sig_offset, M_NOWAIT);
2494f8829a4aSRandall Stewart 	if (m_sig == NULL) {
2495f8829a4aSRandall Stewart 		/* out of memory or ?? */
2496f8829a4aSRandall Stewart 		return (NULL);
2497f8829a4aSRandall Stewart 	}
2498eadccaccSRandall Stewart #ifdef SCTP_MBUF_LOGGING
2499b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
25004be807c4SMichael Tuexen 		sctp_log_mbc(m_sig, SCTP_MBUF_SPLIT);
2501eadccaccSRandall Stewart 	}
2502eadccaccSRandall Stewart #endif
2503eadccaccSRandall Stewart 
2504f8829a4aSRandall Stewart 	/*
2505f8829a4aSRandall Stewart 	 * compute the signature/digest for the cookie
2506f8829a4aSRandall Stewart 	 */
2507f8829a4aSRandall Stewart 	ep = &(*inp_p)->sctp_ep;
2508f8829a4aSRandall Stewart 	l_inp = *inp_p;
2509f8829a4aSRandall Stewart 	if (l_stcb) {
2510f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(l_stcb);
2511f8829a4aSRandall Stewart 	}
2512f8829a4aSRandall Stewart 	SCTP_INP_RLOCK(l_inp);
2513f8829a4aSRandall Stewart 	if (l_stcb) {
2514f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(l_stcb);
2515f8829a4aSRandall Stewart 	}
2516f8829a4aSRandall Stewart 	/* which cookie is it? */
2517f8829a4aSRandall Stewart 	if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2518f8829a4aSRandall Stewart 	    (ep->current_secret_number != ep->last_secret_number)) {
2519f8829a4aSRandall Stewart 		/* it's the old cookie */
25206e55db54SRandall Stewart 		(void)sctp_hmac_m(SCTP_HMAC,
2521f8829a4aSRandall Stewart 		    (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2522d00aff5dSRandall Stewart 		    SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2523f8829a4aSRandall Stewart 	} else {
2524f8829a4aSRandall Stewart 		/* it's the current cookie */
25256e55db54SRandall Stewart 		(void)sctp_hmac_m(SCTP_HMAC,
2526f8829a4aSRandall Stewart 		    (uint8_t *) ep->secret_key[(int)ep->current_secret_number],
2527d00aff5dSRandall Stewart 		    SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2528f8829a4aSRandall Stewart 	}
2529f8829a4aSRandall Stewart 	/* get the signature */
2530f8829a4aSRandall Stewart 	SCTP_INP_RUNLOCK(l_inp);
2531f8829a4aSRandall Stewart 	sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig);
2532f8829a4aSRandall Stewart 	if (sig == NULL) {
2533f8829a4aSRandall Stewart 		/* couldn't find signature */
253403b0b021SRandall Stewart 		sctp_m_freem(m_sig);
2535f8829a4aSRandall Stewart 		return (NULL);
2536f8829a4aSRandall Stewart 	}
2537f8829a4aSRandall Stewart 	/* compare the received digest with the computed digest */
2538f8829a4aSRandall Stewart 	if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2539f8829a4aSRandall Stewart 		/* try the old cookie? */
2540f8829a4aSRandall Stewart 		if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2541f8829a4aSRandall Stewart 		    (ep->current_secret_number != ep->last_secret_number)) {
2542f8829a4aSRandall Stewart 			/* compute digest with old */
25436e55db54SRandall Stewart 			(void)sctp_hmac_m(SCTP_HMAC,
2544f8829a4aSRandall Stewart 			    (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2545d00aff5dSRandall Stewart 			    SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2546f8829a4aSRandall Stewart 			/* compare */
2547f8829a4aSRandall Stewart 			if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2548f8829a4aSRandall Stewart 				cookie_ok = 1;
2549f8829a4aSRandall Stewart 		}
2550f8829a4aSRandall Stewart 	} else {
2551f8829a4aSRandall Stewart 		cookie_ok = 1;
2552f8829a4aSRandall Stewart 	}
2553f8829a4aSRandall Stewart 
2554f8829a4aSRandall Stewart 	/*
2555f8829a4aSRandall Stewart 	 * Now before we continue we must reconstruct our mbuf so that
2556f8829a4aSRandall Stewart 	 * normal processing of any other chunks will work.
2557f8829a4aSRandall Stewart 	 */
2558f8829a4aSRandall Stewart 	{
2559f8829a4aSRandall Stewart 		struct mbuf *m_at;
2560f8829a4aSRandall Stewart 
2561f8829a4aSRandall Stewart 		m_at = m;
2562139bc87fSRandall Stewart 		while (SCTP_BUF_NEXT(m_at) != NULL) {
2563139bc87fSRandall Stewart 			m_at = SCTP_BUF_NEXT(m_at);
2564f8829a4aSRandall Stewart 		}
2565139bc87fSRandall Stewart 		SCTP_BUF_NEXT(m_at) = m_sig;
2566f8829a4aSRandall Stewart 	}
2567f8829a4aSRandall Stewart 
2568f8829a4aSRandall Stewart 	if (cookie_ok == 0) {
2569ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2570ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
2571ad81507eSRandall Stewart 		    "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2572f8829a4aSRandall Stewart 		    (uint32_t) offset, cookie_offset, sig_offset);
2573f8829a4aSRandall Stewart 		return (NULL);
2574f8829a4aSRandall Stewart 	}
2575f8829a4aSRandall Stewart 	/*
2576f8829a4aSRandall Stewart 	 * check the cookie timestamps to be sure it's not stale
2577f8829a4aSRandall Stewart 	 */
25786e55db54SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&now);
2579f8829a4aSRandall Stewart 	/* Expire time is in Ticks, so we convert to seconds */
25803c503c28SRandall Stewart 	time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2581f8829a4aSRandall Stewart 	time_expires.tv_usec = cookie->time_entered.tv_usec;
2582fc14de76SRandall Stewart 	/*
2583fc14de76SRandall Stewart 	 * TODO sctp_constants.h needs alternative time macros when _KERNEL
2584fc14de76SRandall Stewart 	 * is undefined.
2585fc14de76SRandall Stewart 	 */
2586f8829a4aSRandall Stewart 	if (timevalcmp(&now, &time_expires, >)) {
2587f8829a4aSRandall Stewart 		/* cookie is stale! */
2588f8829a4aSRandall Stewart 		struct mbuf *op_err;
258986eda749SMichael Tuexen 		struct sctp_error_stale_cookie *cause;
2590f8829a4aSRandall Stewart 		uint32_t tim;
2591f8829a4aSRandall Stewart 
259286eda749SMichael Tuexen 		op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_stale_cookie),
2593eb1b1807SGleb Smirnoff 		    0, M_NOWAIT, 1, MT_DATA);
2594f8829a4aSRandall Stewart 		if (op_err == NULL) {
2595f8829a4aSRandall Stewart 			/* FOOBAR */
2596f8829a4aSRandall Stewart 			return (NULL);
2597f8829a4aSRandall Stewart 		}
2598f8829a4aSRandall Stewart 		/* Set the len */
259986eda749SMichael Tuexen 		SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_stale_cookie);
260086eda749SMichael Tuexen 		cause = mtod(op_err, struct sctp_error_stale_cookie *);
260186eda749SMichael Tuexen 		cause->cause.code = htons(SCTP_CAUSE_STALE_COOKIE);
260286eda749SMichael Tuexen 		cause->cause.length = htons((sizeof(struct sctp_paramhdr) +
2603f8829a4aSRandall Stewart 		    (sizeof(uint32_t))));
2604f8829a4aSRandall Stewart 		/* seconds to usec */
2605f8829a4aSRandall Stewart 		tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
2606f8829a4aSRandall Stewart 		/* add in usec */
2607f8829a4aSRandall Stewart 		if (tim == 0)
2608f8829a4aSRandall Stewart 			tim = now.tv_usec - cookie->time_entered.tv_usec;
260986eda749SMichael Tuexen 		cause->stale_time = htonl(tim);
2610b1754ad1SMichael Tuexen 		sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
2611d089f9b9SMichael Tuexen 		    mflowtype, mflowid, l_inp->fibnum,
2612c54a18d2SRandall Stewart 		    vrf_id, port);
2613f8829a4aSRandall Stewart 		return (NULL);
2614f8829a4aSRandall Stewart 	}
2615f8829a4aSRandall Stewart 	/*
2616f8829a4aSRandall Stewart 	 * Now we must see with the lookup address if we have an existing
2617f8829a4aSRandall Stewart 	 * asoc. This will only happen if we were in the COOKIE-WAIT state
2618f8829a4aSRandall Stewart 	 * and a INIT collided with us and somewhere the peer sent the
2619f8829a4aSRandall Stewart 	 * cookie on another address besides the single address our assoc
2620f8829a4aSRandall Stewart 	 * had for him. In this case we will have one of the tie-tags set at
2621f8829a4aSRandall Stewart 	 * least AND the address field in the cookie can be used to look it
2622f8829a4aSRandall Stewart 	 * up.
2623f8829a4aSRandall Stewart 	 */
2624f8829a4aSRandall Stewart 	to = NULL;
2625e6194c2eSMichael Tuexen 	switch (cookie->addr_type) {
2626e6194c2eSMichael Tuexen #ifdef INET6
2627e6194c2eSMichael Tuexen 	case SCTP_IPV6_ADDRESS:
2628f8829a4aSRandall Stewart 		memset(&sin6, 0, sizeof(sin6));
2629f8829a4aSRandall Stewart 		sin6.sin6_family = AF_INET6;
2630f8829a4aSRandall Stewart 		sin6.sin6_len = sizeof(sin6);
2631f8829a4aSRandall Stewart 		sin6.sin6_port = sh->src_port;
2632f8829a4aSRandall Stewart 		sin6.sin6_scope_id = cookie->scope_id;
2633f8829a4aSRandall Stewart 		memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2634f8829a4aSRandall Stewart 		    sizeof(sin6.sin6_addr.s6_addr));
2635f8829a4aSRandall Stewart 		to = (struct sockaddr *)&sin6;
2636e6194c2eSMichael Tuexen 		break;
2637e6194c2eSMichael Tuexen #endif
2638e6194c2eSMichael Tuexen #ifdef INET
2639e6194c2eSMichael Tuexen 	case SCTP_IPV4_ADDRESS:
2640f8829a4aSRandall Stewart 		memset(&sin, 0, sizeof(sin));
2641f8829a4aSRandall Stewart 		sin.sin_family = AF_INET;
2642f8829a4aSRandall Stewart 		sin.sin_len = sizeof(sin);
2643f8829a4aSRandall Stewart 		sin.sin_port = sh->src_port;
2644f8829a4aSRandall Stewart 		sin.sin_addr.s_addr = cookie->address[0];
2645f8829a4aSRandall Stewart 		to = (struct sockaddr *)&sin;
2646e6194c2eSMichael Tuexen 		break;
2647e6194c2eSMichael Tuexen #endif
2648e6194c2eSMichael Tuexen 	default:
2649ad81507eSRandall Stewart 		/* This should not happen */
2650ad81507eSRandall Stewart 		return (NULL);
2651f8829a4aSRandall Stewart 	}
2652f0dc2113SMichael Tuexen 	if (*stcb == NULL) {
2653f8829a4aSRandall Stewart 		/* Yep, lets check */
2654b1754ad1SMichael Tuexen 		*stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
2655f8829a4aSRandall Stewart 		if (*stcb == NULL) {
2656f8829a4aSRandall Stewart 			/*
2657f8829a4aSRandall Stewart 			 * We should have only got back the same inp. If we
2658f8829a4aSRandall Stewart 			 * got back a different ep we have a problem. The
2659f8829a4aSRandall Stewart 			 * original findep got back l_inp and now
2660f8829a4aSRandall Stewart 			 */
2661f8829a4aSRandall Stewart 			if (l_inp != *inp_p) {
2662ad81507eSRandall Stewart 				SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2663f8829a4aSRandall Stewart 			}
2664a5d547adSRandall Stewart 		} else {
2665a5d547adSRandall Stewart 			if (*locked_tcb == NULL) {
2666a5d547adSRandall Stewart 				/*
2667a5d547adSRandall Stewart 				 * In this case we found the assoc only
2668a5d547adSRandall Stewart 				 * after we locked the create lock. This
2669a5d547adSRandall Stewart 				 * means we are in a colliding case and we
2670a5d547adSRandall Stewart 				 * must make sure that we unlock the tcb if
2671a5d547adSRandall Stewart 				 * its one of the cases where we throw away
2672a5d547adSRandall Stewart 				 * the incoming packets.
2673a5d547adSRandall Stewart 				 */
2674a5d547adSRandall Stewart 				*locked_tcb = *stcb;
2675a5d547adSRandall Stewart 
2676a5d547adSRandall Stewart 				/*
2677a5d547adSRandall Stewart 				 * We must also increment the inp ref count
2678a5d547adSRandall Stewart 				 * since the ref_count flags was set when we
2679a5d547adSRandall Stewart 				 * did not find the TCB, now we found it
2680a5d547adSRandall Stewart 				 * which reduces the refcount.. we must
2681a5d547adSRandall Stewart 				 * raise it back out to balance it all :-)
2682a5d547adSRandall Stewart 				 */
2683a5d547adSRandall Stewart 				SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2684a5d547adSRandall Stewart 				if ((*stcb)->sctp_ep != l_inp) {
2685ad81507eSRandall Stewart 					SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2686dd294dceSMichael Tuexen 					    (void *)(*stcb)->sctp_ep, (void *)l_inp);
2687a5d547adSRandall Stewart 				}
2688a5d547adSRandall Stewart 			}
2689f8829a4aSRandall Stewart 		}
2690f8829a4aSRandall Stewart 	}
2691f8829a4aSRandall Stewart 	cookie_len -= SCTP_SIGNATURE_SIZE;
2692f8829a4aSRandall Stewart 	if (*stcb == NULL) {
2693f8829a4aSRandall Stewart 		/* this is the "normal" case... get a new TCB */
2694b1754ad1SMichael Tuexen 		*stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
2695b1754ad1SMichael Tuexen 		    cookie, cookie_len, *inp_p,
2696b1754ad1SMichael Tuexen 		    netp, to, &notification,
2697f30ac432SMichael Tuexen 		    auth_skipped, auth_offset, auth_len,
2698457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2699f30ac432SMichael Tuexen 		    vrf_id, port);
2700f8829a4aSRandall Stewart 	} else {
2701f8829a4aSRandall Stewart 		/* this is abnormal... cookie-echo on existing TCB */
2702f8829a4aSRandall Stewart 		had_a_existing_tcb = 1;
2703b1754ad1SMichael Tuexen 		*stcb = sctp_process_cookie_existing(m, iphlen, offset,
2704b1754ad1SMichael Tuexen 		    src, dst, sh,
2705830d754dSRandall Stewart 		    cookie, cookie_len, *inp_p, *stcb, netp, to,
2706f30ac432SMichael Tuexen 		    &notification, auth_skipped, auth_offset, auth_len,
2707457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2708f30ac432SMichael Tuexen 		    vrf_id, port);
2709f8829a4aSRandall Stewart 	}
2710f8829a4aSRandall Stewart 
2711f8829a4aSRandall Stewart 	if (*stcb == NULL) {
2712f8829a4aSRandall Stewart 		/* still no TCB... must be bad cookie-echo */
2713f8829a4aSRandall Stewart 		return (NULL);
2714f8829a4aSRandall Stewart 	}
27155fe29cdfSMichael Tuexen 	if (*netp != NULL) {
2716457b4b88SMichael Tuexen 		(*netp)->flowtype = mflowtype;
27175fe29cdfSMichael Tuexen 		(*netp)->flowid = mflowid;
2718a4ae38f1SMichael Tuexen 	}
2719f8829a4aSRandall Stewart 	/*
2720f8829a4aSRandall Stewart 	 * Ok, we built an association so confirm the address we sent the
2721f8829a4aSRandall Stewart 	 * INIT-ACK to.
2722f8829a4aSRandall Stewart 	 */
2723f8829a4aSRandall Stewart 	netl = sctp_findnet(*stcb, to);
2724f8829a4aSRandall Stewart 	/*
2725f8829a4aSRandall Stewart 	 * This code should in theory NOT run but
2726f8829a4aSRandall Stewart 	 */
2727f8829a4aSRandall Stewart 	if (netl == NULL) {
2728f8829a4aSRandall Stewart 		/* TSNH! Huh, why do I need to add this address here? */
272960990c0cSMichael Tuexen 		if (sctp_add_remote_addr(*stcb, to, NULL, SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
273060990c0cSMichael Tuexen 			return (NULL);
273160990c0cSMichael Tuexen 		}
2732f8829a4aSRandall Stewart 		netl = sctp_findnet(*stcb, to);
2733f8829a4aSRandall Stewart 	}
2734f8829a4aSRandall Stewart 	if (netl) {
2735f8829a4aSRandall Stewart 		if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2736f8829a4aSRandall Stewart 			netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2737ad81507eSRandall Stewart 			(void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2738f8829a4aSRandall Stewart 			    netl);
27392fad0e55SMichael Tuexen 			send_int_conf = 1;
2740f8829a4aSRandall Stewart 		}
2741f8829a4aSRandall Stewart 	}
2742ca85e948SMichael Tuexen 	sctp_start_net_timers(*stcb);
2743f8829a4aSRandall Stewart 	if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2744f8829a4aSRandall Stewart 		if (!had_a_existing_tcb ||
2745f8829a4aSRandall Stewart 		    (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2746f8829a4aSRandall Stewart 			/*
2747f8829a4aSRandall Stewart 			 * If we have a NEW cookie or the connect never
2748f8829a4aSRandall Stewart 			 * reached the connected state during collision we
2749f8829a4aSRandall Stewart 			 * must do the TCP accept thing.
2750f8829a4aSRandall Stewart 			 */
2751f8829a4aSRandall Stewart 			struct socket *so, *oso;
2752f8829a4aSRandall Stewart 			struct sctp_inpcb *inp;
2753f8829a4aSRandall Stewart 
2754f8829a4aSRandall Stewart 			if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2755f8829a4aSRandall Stewart 				/*
2756f8829a4aSRandall Stewart 				 * For a restart we will keep the same
2757f8829a4aSRandall Stewart 				 * socket, no need to do anything. I THINK!!
2758f8829a4aSRandall Stewart 				 */
27597215cc1bSMichael Tuexen 				sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
27602fad0e55SMichael Tuexen 				if (send_int_conf) {
27612fad0e55SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
27622fad0e55SMichael Tuexen 					    (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
27632fad0e55SMichael Tuexen 				}
2764f8829a4aSRandall Stewart 				return (m);
2765f8829a4aSRandall Stewart 			}
2766f8829a4aSRandall Stewart 			oso = (*inp_p)->sctp_socket;
27672dad8a55SRandall Stewart 			atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2768f8829a4aSRandall Stewart 			SCTP_TCB_UNLOCK((*stcb));
27691fb51a12SBjoern A. Zeeb 			CURVNET_SET(oso->so_vnet);
277093164cf9SRandall Stewart 			so = sonewconn(oso, 0
2771f8829a4aSRandall Stewart 			    );
27721fb51a12SBjoern A. Zeeb 			CURVNET_RESTORE();
2773f8829a4aSRandall Stewart 			SCTP_TCB_LOCK((*stcb));
27742dad8a55SRandall Stewart 			atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
27752dad8a55SRandall Stewart 
2776f8829a4aSRandall Stewart 			if (so == NULL) {
2777f8829a4aSRandall Stewart 				struct mbuf *op_err;
2778f8829a4aSRandall Stewart 
2779ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2780ceaad40aSRandall Stewart 				struct socket *pcb_so;
2781ceaad40aSRandall Stewart 
2782ceaad40aSRandall Stewart #endif
2783f8829a4aSRandall Stewart 				/* Too many sockets */
2784ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2785ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2786f8829a4aSRandall Stewart 				sctp_abort_association(*inp_p, NULL, m, iphlen,
2787b1754ad1SMichael Tuexen 				    src, dst, sh, op_err,
2788457b4b88SMichael Tuexen 				    mflowtype, mflowid,
2789f30ac432SMichael Tuexen 				    vrf_id, port);
2790ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2791ceaad40aSRandall Stewart 				pcb_so = SCTP_INP_SO(*inp_p);
2792ceaad40aSRandall Stewart 				atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2793ceaad40aSRandall Stewart 				SCTP_TCB_UNLOCK((*stcb));
2794ceaad40aSRandall Stewart 				SCTP_SOCKET_LOCK(pcb_so, 1);
2795ceaad40aSRandall Stewart 				SCTP_TCB_LOCK((*stcb));
2796ceaad40aSRandall Stewart 				atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2797ceaad40aSRandall Stewart #endif
2798b7d130beSMichael Tuexen 				(void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC,
2799b7d130beSMichael Tuexen 				    SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
2800ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2801ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(pcb_so, 1);
2802ceaad40aSRandall Stewart #endif
2803f8829a4aSRandall Stewart 				return (NULL);
2804f8829a4aSRandall Stewart 			}
2805f8829a4aSRandall Stewart 			inp = (struct sctp_inpcb *)so->so_pcb;
280693164cf9SRandall Stewart 			SCTP_INP_INCR_REF(inp);
280793164cf9SRandall Stewart 			/*
280893164cf9SRandall Stewart 			 * We add the unbound flag here so that if we get an
280993164cf9SRandall Stewart 			 * soabort() before we get the move_pcb done, we
281093164cf9SRandall Stewart 			 * will properly cleanup.
281193164cf9SRandall Stewart 			 */
2812f8829a4aSRandall Stewart 			inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2813f8829a4aSRandall Stewart 			    SCTP_PCB_FLAGS_CONNECTED |
2814f8829a4aSRandall Stewart 			    SCTP_PCB_FLAGS_IN_TCPPOOL |
281593164cf9SRandall Stewart 			    SCTP_PCB_FLAGS_UNBOUND |
2816f8829a4aSRandall Stewart 			    (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2817f8829a4aSRandall Stewart 			    SCTP_PCB_FLAGS_DONT_WAKE);
2818f8829a4aSRandall Stewart 			inp->sctp_features = (*inp_p)->sctp_features;
2819851b7298SRandall Stewart 			inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2820f8829a4aSRandall Stewart 			inp->sctp_socket = so;
2821f8829a4aSRandall Stewart 			inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
282259b6d5beSMichael Tuexen 			inp->max_cwnd = (*inp_p)->max_cwnd;
282320083c2eSMichael Tuexen 			inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2824f342355aSMichael Tuexen 			inp->ecn_supported = (*inp_p)->ecn_supported;
2825dd973b0eSMichael Tuexen 			inp->prsctp_supported = (*inp_p)->prsctp_supported;
2826c79bec9cSMichael Tuexen 			inp->auth_supported = (*inp_p)->auth_supported;
2827c79bec9cSMichael Tuexen 			inp->asconf_supported = (*inp_p)->asconf_supported;
2828317e00efSMichael Tuexen 			inp->reconfig_supported = (*inp_p)->reconfig_supported;
2829caea9879SMichael Tuexen 			inp->nrsack_supported = (*inp_p)->nrsack_supported;
2830cb9b8e6fSMichael Tuexen 			inp->pktdrop_supported = (*inp_p)->pktdrop_supported;
2831f8829a4aSRandall Stewart 			inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2832f8829a4aSRandall Stewart 			inp->sctp_context = (*inp_p)->sctp_context;
2833c4e848b7SRandall Stewart 			inp->local_strreset_support = (*inp_p)->local_strreset_support;
2834d089f9b9SMichael Tuexen 			inp->fibnum = (*inp_p)->fibnum;
2835f8829a4aSRandall Stewart 			inp->inp_starting_point_for_iterator = NULL;
2836f8829a4aSRandall Stewart 			/*
2837f8829a4aSRandall Stewart 			 * copy in the authentication parameters from the
2838f8829a4aSRandall Stewart 			 * original endpoint
2839f8829a4aSRandall Stewart 			 */
2840f8829a4aSRandall Stewart 			if (inp->sctp_ep.local_hmacs)
2841f8829a4aSRandall Stewart 				sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2842f8829a4aSRandall Stewart 			inp->sctp_ep.local_hmacs =
2843f8829a4aSRandall Stewart 			    sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2844f8829a4aSRandall Stewart 			if (inp->sctp_ep.local_auth_chunks)
2845f8829a4aSRandall Stewart 				sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2846f8829a4aSRandall Stewart 			inp->sctp_ep.local_auth_chunks =
2847f8829a4aSRandall Stewart 			    sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2848f8829a4aSRandall Stewart 
2849f8829a4aSRandall Stewart 			/*
2850f8829a4aSRandall Stewart 			 * Now we must move it from one hash table to
2851f8829a4aSRandall Stewart 			 * another and get the tcb in the right place.
2852f8829a4aSRandall Stewart 			 */
285388a7eb29SRandall Stewart 
285488a7eb29SRandall Stewart 			/*
285588a7eb29SRandall Stewart 			 * This is where the one-2-one socket is put into
285688a7eb29SRandall Stewart 			 * the accept state waiting for the accept!
285788a7eb29SRandall Stewart 			 */
285888a7eb29SRandall Stewart 			if (*stcb) {
285988a7eb29SRandall Stewart 				(*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE;
286088a7eb29SRandall Stewart 			}
2861f8829a4aSRandall Stewart 			sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2862d61a0ae0SRandall Stewart 
2863d61a0ae0SRandall Stewart 			atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2864d61a0ae0SRandall Stewart 			SCTP_TCB_UNLOCK((*stcb));
2865d61a0ae0SRandall Stewart 
2866265de5bbSRobert Watson 			sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2867265de5bbSRobert Watson 			    0);
2868d61a0ae0SRandall Stewart 			SCTP_TCB_LOCK((*stcb));
2869d61a0ae0SRandall Stewart 			atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2870d61a0ae0SRandall Stewart 
2871f8829a4aSRandall Stewart 
287293164cf9SRandall Stewart 			/*
287393164cf9SRandall Stewart 			 * now we must check to see if we were aborted while
287493164cf9SRandall Stewart 			 * the move was going on and the lock/unlock
287593164cf9SRandall Stewart 			 * happened.
287693164cf9SRandall Stewart 			 */
287793164cf9SRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
287893164cf9SRandall Stewart 				/*
287993164cf9SRandall Stewart 				 * yep it was, we leave the assoc attached
288093164cf9SRandall Stewart 				 * to the socket since the sctp_inpcb_free()
288193164cf9SRandall Stewart 				 * call will send an abort for us.
288293164cf9SRandall Stewart 				 */
288393164cf9SRandall Stewart 				SCTP_INP_DECR_REF(inp);
288493164cf9SRandall Stewart 				return (NULL);
288593164cf9SRandall Stewart 			}
288693164cf9SRandall Stewart 			SCTP_INP_DECR_REF(inp);
2887f8829a4aSRandall Stewart 			/* Switch over to the new guy */
2888f8829a4aSRandall Stewart 			*inp_p = inp;
2889ceaad40aSRandall Stewart 			sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
28902fad0e55SMichael Tuexen 			if (send_int_conf) {
28912fad0e55SMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
28922fad0e55SMichael Tuexen 				    (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
28932fad0e55SMichael Tuexen 			}
289493164cf9SRandall Stewart 			/*
289593164cf9SRandall Stewart 			 * Pull it from the incomplete queue and wake the
289693164cf9SRandall Stewart 			 * guy
289793164cf9SRandall Stewart 			 */
2898ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2899ceaad40aSRandall Stewart 			atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2900ceaad40aSRandall Stewart 			SCTP_TCB_UNLOCK((*stcb));
2901ceaad40aSRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
2902ceaad40aSRandall Stewart #endif
290393164cf9SRandall Stewart 			soisconnected(so);
2904ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2905b7a446b8SRandall Stewart 			SCTP_TCB_LOCK((*stcb));
2906b7a446b8SRandall Stewart 			atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2907ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
2908ceaad40aSRandall Stewart #endif
2909f8829a4aSRandall Stewart 			return (m);
2910f8829a4aSRandall Stewart 		}
2911f8829a4aSRandall Stewart 	}
29122fad0e55SMichael Tuexen 	if (notification) {
2913ceaad40aSRandall Stewart 		sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2914f8829a4aSRandall Stewart 	}
29152fad0e55SMichael Tuexen 	if (send_int_conf) {
29162fad0e55SMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
29172fad0e55SMichael Tuexen 		    (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
29182fad0e55SMichael Tuexen 	}
2919f8829a4aSRandall Stewart 	return (m);
2920f8829a4aSRandall Stewart }
2921f8829a4aSRandall Stewart 
2922f8829a4aSRandall Stewart static void
29237215cc1bSMichael Tuexen sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2924f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net)
2925f8829a4aSRandall Stewart {
2926f8829a4aSRandall Stewart 	/* cp must not be used, others call this without a c-ack :-) */
2927f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2928f8829a4aSRandall Stewart 
2929ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
2930ad81507eSRandall Stewart 	    "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2931ad234e3cSMichael Tuexen 	if ((stcb == NULL) || (net == NULL)) {
2932f8829a4aSRandall Stewart 		return;
2933ad234e3cSMichael Tuexen 	}
2934f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2935f8829a4aSRandall Stewart 
2936f8829a4aSRandall Stewart 	sctp_stop_all_cookie_timers(stcb);
2937f8829a4aSRandall Stewart 	/* process according to association state */
2938f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2939f8829a4aSRandall Stewart 		/* state change only needed when I am in right state */
2940ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2941c4739e2fSRandall Stewart 		SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2942ca85e948SMichael Tuexen 		sctp_start_net_timers(stcb);
2943f8829a4aSRandall Stewart 		if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2944f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2945f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, asoc->primary_destination);
2946f8829a4aSRandall Stewart 
2947f8829a4aSRandall Stewart 		}
2948f8829a4aSRandall Stewart 		/* update RTO */
2949f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2950f8829a4aSRandall Stewart 		SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2951f8829a4aSRandall Stewart 		if (asoc->overall_error_count == 0) {
2952f8829a4aSRandall Stewart 			net->RTO = sctp_calculate_rto(stcb, asoc, net,
2953899288aeSRandall Stewart 			    &asoc->time_entered, sctp_align_safe_nocopy,
2954f79aab18SRandall Stewart 			    SCTP_RTT_FROM_NON_DATA);
2955f8829a4aSRandall Stewart 		}
29566e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2957ceaad40aSRandall Stewart 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2958f8829a4aSRandall Stewart 		if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2959f8829a4aSRandall Stewart 		    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2960ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2961ceaad40aSRandall Stewart 			struct socket *so;
2962ceaad40aSRandall Stewart 
2963ceaad40aSRandall Stewart #endif
2964f8829a4aSRandall Stewart 			stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2965ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2966ceaad40aSRandall Stewart 			so = SCTP_INP_SO(stcb->sctp_ep);
2967ceaad40aSRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, 1);
2968ceaad40aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
2969ceaad40aSRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
2970ceaad40aSRandall Stewart 			SCTP_TCB_LOCK(stcb);
2971ceaad40aSRandall Stewart 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
2972ceaad40aSRandall Stewart #endif
2973d69e7322SRandall Stewart 			if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
2974ceaad40aSRandall Stewart 				soisconnected(stcb->sctp_socket);
2975d69e7322SRandall Stewart 			}
2976ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2977ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
2978ceaad40aSRandall Stewart #endif
2979f8829a4aSRandall Stewart 		}
2980f8829a4aSRandall Stewart 		/*
2981f8829a4aSRandall Stewart 		 * since we did not send a HB make sure we don't double
2982f8829a4aSRandall Stewart 		 * things
2983f8829a4aSRandall Stewart 		 */
2984f8829a4aSRandall Stewart 		net->hb_responded = 1;
2985f8829a4aSRandall Stewart 
2986d69e7322SRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2987d69e7322SRandall Stewart 			/*
2988d69e7322SRandall Stewart 			 * We don't need to do the asconf thing, nor hb or
2989d69e7322SRandall Stewart 			 * autoclose if the socket is closed.
2990d69e7322SRandall Stewart 			 */
2991d69e7322SRandall Stewart 			goto closed_socket;
2992d69e7322SRandall Stewart 		}
2993d69e7322SRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2994d69e7322SRandall Stewart 		    stcb, net);
2995d69e7322SRandall Stewart 
2996d69e7322SRandall Stewart 
2997f8829a4aSRandall Stewart 		if (stcb->asoc.sctp_autoclose_ticks &&
2998f8829a4aSRandall Stewart 		    sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2999f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
3000f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, NULL);
3001f8829a4aSRandall Stewart 		}
3002f8829a4aSRandall Stewart 		/*
30031b649582SRandall Stewart 		 * send ASCONF if parameters are pending and ASCONFs are
30041b649582SRandall Stewart 		 * allowed (eg. addresses changed when init/cookie echo were
30051b649582SRandall Stewart 		 * in flight)
3006f8829a4aSRandall Stewart 		 */
3007f8829a4aSRandall Stewart 		if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
3008c79bec9cSMichael Tuexen 		    (stcb->asoc.asconf_supported == 1) &&
3009f8829a4aSRandall Stewart 		    (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
30101b649582SRandall Stewart #ifdef SCTP_TIMER_BASED_ASCONF
3011f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
3012f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb,
3013f8829a4aSRandall Stewart 			    stcb->asoc.primary_destination);
30141b649582SRandall Stewart #else
30153232788eSRandall Stewart 			sctp_send_asconf(stcb, stcb->asoc.primary_destination,
30163232788eSRandall Stewart 			    SCTP_ADDR_NOT_LOCKED);
30171b649582SRandall Stewart #endif
3018f8829a4aSRandall Stewart 		}
3019f8829a4aSRandall Stewart 	}
3020d69e7322SRandall Stewart closed_socket:
3021f8829a4aSRandall Stewart 	/* Toss the cookie if I can */
3022f8829a4aSRandall Stewart 	sctp_toss_old_cookies(stcb, asoc);
3023f8829a4aSRandall Stewart 	if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3024f8829a4aSRandall Stewart 		/* Restart the timer if we have pending data */
3025f8829a4aSRandall Stewart 		struct sctp_tmit_chunk *chk;
3026f8829a4aSRandall Stewart 
3027f8829a4aSRandall Stewart 		chk = TAILQ_FIRST(&asoc->sent_queue);
30284a9ef3f8SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
3029f8829a4aSRandall Stewart 	}
3030f8829a4aSRandall Stewart }
3031f8829a4aSRandall Stewart 
3032f8829a4aSRandall Stewart static void
3033f8829a4aSRandall Stewart sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
3034f8829a4aSRandall Stewart     struct sctp_tcb *stcb)
3035f8829a4aSRandall Stewart {
3036f8829a4aSRandall Stewart 	struct sctp_nets *net;
3037f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *lchk;
3038a21779f0SRandall Stewart 	struct sctp_ecne_chunk bkup;
303960990c0cSMichael Tuexen 	uint8_t override_bit;
3040a21779f0SRandall Stewart 	uint32_t tsn, window_data_tsn;
3041a21779f0SRandall Stewart 	int len;
3042dec0177dSRandall Stewart 	unsigned int pkt_cnt;
3043f8829a4aSRandall Stewart 
3044a21779f0SRandall Stewart 	len = ntohs(cp->ch.chunk_length);
3045a21779f0SRandall Stewart 	if ((len != sizeof(struct sctp_ecne_chunk)) &&
3046a21779f0SRandall Stewart 	    (len != sizeof(struct old_sctp_ecne_chunk))) {
3047f8829a4aSRandall Stewart 		return;
3048f8829a4aSRandall Stewart 	}
3049a21779f0SRandall Stewart 	if (len == sizeof(struct old_sctp_ecne_chunk)) {
3050a21779f0SRandall Stewart 		/* Its the old format */
3051a21779f0SRandall Stewart 		memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
3052a21779f0SRandall Stewart 		bkup.num_pkts_since_cwr = htonl(1);
3053a21779f0SRandall Stewart 		cp = &bkup;
3054a21779f0SRandall Stewart 	}
3055f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvecne);
3056f8829a4aSRandall Stewart 	tsn = ntohl(cp->tsn);
3057a21779f0SRandall Stewart 	pkt_cnt = ntohl(cp->num_pkts_since_cwr);
3058a21779f0SRandall Stewart 	lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
3059f8829a4aSRandall Stewart 	if (lchk == NULL) {
3060493d8e5aSRandall Stewart 		window_data_tsn = stcb->asoc.sending_seq - 1;
3061f8829a4aSRandall Stewart 	} else {
3062493d8e5aSRandall Stewart 		window_data_tsn = lchk->rec.data.TSN_seq;
3063f8829a4aSRandall Stewart 	}
3064f8829a4aSRandall Stewart 
3065a21779f0SRandall Stewart 	/* Find where it was sent to if possible. */
3066f8829a4aSRandall Stewart 	net = NULL;
30674a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
3068f8829a4aSRandall Stewart 		if (lchk->rec.data.TSN_seq == tsn) {
3069f8829a4aSRandall Stewart 			net = lchk->whoTo;
3070899288aeSRandall Stewart 			net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
3071f8829a4aSRandall Stewart 			break;
3072f8829a4aSRandall Stewart 		}
307320b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(lchk->rec.data.TSN_seq, tsn)) {
3074f8829a4aSRandall Stewart 			break;
3075f8829a4aSRandall Stewart 		}
307620b07a4dSMichael Tuexen 	}
3077a21779f0SRandall Stewart 	if (net == NULL) {
3078a21779f0SRandall Stewart 		/*
3079a21779f0SRandall Stewart 		 * What to do. A previous send of a CWR was possibly lost.
3080a21779f0SRandall Stewart 		 * See how old it is, we may have it marked on the actual
3081a21779f0SRandall Stewart 		 * net.
3082a21779f0SRandall Stewart 		 */
3083a21779f0SRandall Stewart 		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3084a21779f0SRandall Stewart 			if (tsn == net->last_cwr_tsn) {
3085a21779f0SRandall Stewart 				/* Found him, send it off */
308660990c0cSMichael Tuexen 				break;
3087a21779f0SRandall Stewart 			}
3088a21779f0SRandall Stewart 		}
308960990c0cSMichael Tuexen 		if (net == NULL) {
3090a21779f0SRandall Stewart 			/*
309160990c0cSMichael Tuexen 			 * If we reach here, we need to send a special CWR
309260990c0cSMichael Tuexen 			 * that says hey, we did this a long time ago and
309360990c0cSMichael Tuexen 			 * you lost the response.
3094a21779f0SRandall Stewart 			 */
3095a21779f0SRandall Stewart 			net = TAILQ_FIRST(&stcb->asoc.nets);
309660990c0cSMichael Tuexen 			if (net == NULL) {
309760990c0cSMichael Tuexen 				/* TSNH */
309860990c0cSMichael Tuexen 				return;
3099a21779f0SRandall Stewart 			}
310060990c0cSMichael Tuexen 			override_bit = SCTP_CWR_REDUCE_OVERRIDE;
310160990c0cSMichael Tuexen 		} else {
310260990c0cSMichael Tuexen 			override_bit = 0;
310360990c0cSMichael Tuexen 		}
310460990c0cSMichael Tuexen 	} else {
310560990c0cSMichael Tuexen 		override_bit = 0;
310660990c0cSMichael Tuexen 	}
3107493d8e5aSRandall Stewart 	if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
3108493d8e5aSRandall Stewart 	    ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3109b54d3a6cSRandall Stewart 		/*
3110b54d3a6cSRandall Stewart 		 * JRS - Use the congestion control given in the pluggable
3111b54d3a6cSRandall Stewart 		 * CC module
3112b54d3a6cSRandall Stewart 		 */
3113493d8e5aSRandall Stewart 		stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
3114f8829a4aSRandall Stewart 		/*
3115a21779f0SRandall Stewart 		 * We reduce once every RTT. So we will only lower cwnd at
3116a21779f0SRandall Stewart 		 * the next sending seq i.e. the window_data_tsn
3117f8829a4aSRandall Stewart 		 */
3118a21779f0SRandall Stewart 		net->cwr_window_tsn = window_data_tsn;
3119a21779f0SRandall Stewart 		net->ecn_ce_pkt_cnt += pkt_cnt;
3120a21779f0SRandall Stewart 		net->lost_cnt = pkt_cnt;
3121a21779f0SRandall Stewart 		net->last_cwr_tsn = tsn;
3122a21779f0SRandall Stewart 	} else {
3123a21779f0SRandall Stewart 		override_bit |= SCTP_CWR_IN_SAME_WINDOW;
3124493d8e5aSRandall Stewart 		if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
3125493d8e5aSRandall Stewart 		    ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3126a21779f0SRandall Stewart 			/*
3127493d8e5aSRandall Stewart 			 * Another loss in the same window update how many
3128493d8e5aSRandall Stewart 			 * marks/packets lost we have had.
3129a21779f0SRandall Stewart 			 */
3130493d8e5aSRandall Stewart 			int cnt = 1;
3131a21779f0SRandall Stewart 
3132a21779f0SRandall Stewart 			if (pkt_cnt > net->lost_cnt) {
3133a21779f0SRandall Stewart 				/* Should be the case */
3134493d8e5aSRandall Stewart 				cnt = (pkt_cnt - net->lost_cnt);
3135493d8e5aSRandall Stewart 				net->ecn_ce_pkt_cnt += cnt;
3136a21779f0SRandall Stewart 			}
3137493d8e5aSRandall Stewart 			net->lost_cnt = pkt_cnt;
3138a21779f0SRandall Stewart 			net->last_cwr_tsn = tsn;
3139493d8e5aSRandall Stewart 			/*
3140493d8e5aSRandall Stewart 			 * Most CC functions will ignore this call, since we
3141493d8e5aSRandall Stewart 			 * are in-window yet of the initial CE the peer saw.
3142493d8e5aSRandall Stewart 			 */
3143493d8e5aSRandall Stewart 			stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
3144a21779f0SRandall Stewart 		}
3145f8829a4aSRandall Stewart 	}
3146f8829a4aSRandall Stewart 	/*
3147f8829a4aSRandall Stewart 	 * We always send a CWR this way if our previous one was lost our
3148f8829a4aSRandall Stewart 	 * peer will get an update, or if it is not time again to reduce we
3149a21779f0SRandall Stewart 	 * still get the cwr to the peer. Note we set the override when we
3150a21779f0SRandall Stewart 	 * could not find the TSN on the chunk or the destination network.
3151f8829a4aSRandall Stewart 	 */
3152a21779f0SRandall Stewart 	sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
3153f8829a4aSRandall Stewart }
3154f8829a4aSRandall Stewart 
3155f8829a4aSRandall Stewart static void
3156a21779f0SRandall Stewart sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3157f8829a4aSRandall Stewart {
3158f8829a4aSRandall Stewart 	/*
3159f8829a4aSRandall Stewart 	 * Here we get a CWR from the peer. We must look in the outqueue and
3160582212faSEitan Adler 	 * make sure that we have a covered ECNE in the control chunk part.
3161f8829a4aSRandall Stewart 	 * If so remove it.
3162f8829a4aSRandall Stewart 	 */
3163f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
3164f8829a4aSRandall Stewart 	struct sctp_ecne_chunk *ecne;
3165a21779f0SRandall Stewart 	int override;
3166a21779f0SRandall Stewart 	uint32_t cwr_tsn;
3167f8829a4aSRandall Stewart 
3168a21779f0SRandall Stewart 	cwr_tsn = ntohl(cp->tsn);
3169a21779f0SRandall Stewart 	override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
3170f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
3171f8829a4aSRandall Stewart 		if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3172f8829a4aSRandall Stewart 			continue;
3173f8829a4aSRandall Stewart 		}
3174a21779f0SRandall Stewart 		if ((override == 0) && (chk->whoTo != net)) {
3175a21779f0SRandall Stewart 			/* Must be from the right src unless override is set */
3176a21779f0SRandall Stewart 			continue;
3177a21779f0SRandall Stewart 		}
3178f8829a4aSRandall Stewart 		ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3179a21779f0SRandall Stewart 		if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3180f8829a4aSRandall Stewart 			/* this covers this ECNE, we can remove it */
3181f8829a4aSRandall Stewart 			stcb->asoc.ecn_echo_cnt_onq--;
3182f8829a4aSRandall Stewart 			TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3183f8829a4aSRandall Stewart 			    sctp_next);
3184f8829a4aSRandall Stewart 			sctp_m_freem(chk->data);
3185f8829a4aSRandall Stewart 			chk->data = NULL;
3186f8829a4aSRandall Stewart 			stcb->asoc.ctrl_queue_cnt--;
3187689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3188a21779f0SRandall Stewart 			if (override == 0) {
3189f8829a4aSRandall Stewart 				break;
3190f8829a4aSRandall Stewart 			}
3191f8829a4aSRandall Stewart 		}
3192f8829a4aSRandall Stewart 	}
3193a21779f0SRandall Stewart }
3194f8829a4aSRandall Stewart 
3195f8829a4aSRandall Stewart static void
31967215cc1bSMichael Tuexen sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3197f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net)
3198f8829a4aSRandall Stewart {
3199f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3200f8829a4aSRandall Stewart 
3201ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3202ceaad40aSRandall Stewart 	struct socket *so;
3203ceaad40aSRandall Stewart 
3204ceaad40aSRandall Stewart #endif
3205ceaad40aSRandall Stewart 
3206ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
3207ad81507eSRandall Stewart 	    "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3208f8829a4aSRandall Stewart 	if (stcb == NULL)
3209f8829a4aSRandall Stewart 		return;
3210f8829a4aSRandall Stewart 
3211f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
3212f8829a4aSRandall Stewart 	/* process according to association state */
3213f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3214f8829a4aSRandall Stewart 		/* unexpected SHUTDOWN-COMPLETE... so ignore... */
32152afb3e84SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
32162afb3e84SRandall Stewart 		    "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3217f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3218f8829a4aSRandall Stewart 		return;
3219f8829a4aSRandall Stewart 	}
3220f8829a4aSRandall Stewart 	/* notify upper layer protocol */
3221f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
3222ceaad40aSRandall Stewart 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
322356f778aaSMichael Tuexen 	}
322456f778aaSMichael Tuexen #ifdef INVARIANTS
3225f8829a4aSRandall Stewart 	if (!TAILQ_EMPTY(&asoc->send_queue) ||
3226f8829a4aSRandall Stewart 	    !TAILQ_EMPTY(&asoc->sent_queue) ||
3227f7a77f6fSMichael Tuexen 	    !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
322856f778aaSMichael Tuexen 		panic("Queues are not empty when handling SHUTDOWN-COMPLETE");
3229f8829a4aSRandall Stewart 	}
323056f778aaSMichael Tuexen #endif
3231f8829a4aSRandall Stewart 	/* stop the timer */
3232b7d130beSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net,
3233b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3234f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3235f8829a4aSRandall Stewart 	/* free the TCB */
32362afb3e84SRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
32372afb3e84SRandall Stewart 	    "sctp_handle_shutdown_complete: calls free-asoc\n");
3238ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3239ceaad40aSRandall Stewart 	so = SCTP_INP_SO(stcb->sctp_ep);
3240ceaad40aSRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, 1);
3241ceaad40aSRandall Stewart 	SCTP_TCB_UNLOCK(stcb);
3242ceaad40aSRandall Stewart 	SCTP_SOCKET_LOCK(so, 1);
3243ceaad40aSRandall Stewart 	SCTP_TCB_LOCK(stcb);
3244ceaad40aSRandall Stewart 	atomic_subtract_int(&stcb->asoc.refcnt, 1);
3245ceaad40aSRandall Stewart #endif
3246b7d130beSMichael Tuexen 	(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
3247b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3248ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3249ceaad40aSRandall Stewart 	SCTP_SOCKET_UNLOCK(so, 1);
3250ceaad40aSRandall Stewart #endif
3251f8829a4aSRandall Stewart 	return;
3252f8829a4aSRandall Stewart }
3253f8829a4aSRandall Stewart 
3254f8829a4aSRandall Stewart static int
3255f8829a4aSRandall Stewart process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3256f8829a4aSRandall Stewart     struct sctp_nets *net, uint8_t flg)
3257f8829a4aSRandall Stewart {
3258f8829a4aSRandall Stewart 	switch (desc->chunk_type) {
3259f8829a4aSRandall Stewart 	case SCTP_DATA:
3260f8829a4aSRandall Stewart 		/* find the tsn to resend (possibly */
3261f8829a4aSRandall Stewart 		{
3262f8829a4aSRandall Stewart 			uint32_t tsn;
3263f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *tp1;
3264f8829a4aSRandall Stewart 
3265f8829a4aSRandall Stewart 			tsn = ntohl(desc->tsn_ifany);
32664a9ef3f8SMichael Tuexen 			TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3267f8829a4aSRandall Stewart 				if (tp1->rec.data.TSN_seq == tsn) {
3268f8829a4aSRandall Stewart 					/* found it */
3269f8829a4aSRandall Stewart 					break;
3270f8829a4aSRandall Stewart 				}
327120b07a4dSMichael Tuexen 				if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, tsn)) {
3272f8829a4aSRandall Stewart 					/* not found */
3273f8829a4aSRandall Stewart 					tp1 = NULL;
3274f8829a4aSRandall Stewart 					break;
3275f8829a4aSRandall Stewart 				}
3276f8829a4aSRandall Stewart 			}
3277f8829a4aSRandall Stewart 			if (tp1 == NULL) {
3278f8829a4aSRandall Stewart 				/*
3279f8829a4aSRandall Stewart 				 * Do it the other way , aka without paying
3280f8829a4aSRandall Stewart 				 * attention to queue seq order.
3281f8829a4aSRandall Stewart 				 */
3282f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpdnfnd);
32834a9ef3f8SMichael Tuexen 				TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3284f8829a4aSRandall Stewart 					if (tp1->rec.data.TSN_seq == tsn) {
3285f8829a4aSRandall Stewart 						/* found it */
3286f8829a4aSRandall Stewart 						break;
3287f8829a4aSRandall Stewart 					}
3288f8829a4aSRandall Stewart 				}
3289f8829a4aSRandall Stewart 			}
3290f8829a4aSRandall Stewart 			if (tp1 == NULL) {
3291f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrptsnnf);
3292f8829a4aSRandall Stewart 			}
3293f8829a4aSRandall Stewart 			if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3294f8829a4aSRandall Stewart 				uint8_t *ddp;
3295f8829a4aSRandall Stewart 
32967da23bc8SMichael Tuexen 				if (((flg & SCTP_BADCRC) == 0) &&
32977da23bc8SMichael Tuexen 				    ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
32987da23bc8SMichael Tuexen 					return (0);
32997da23bc8SMichael Tuexen 				}
3300f8829a4aSRandall Stewart 				if ((stcb->asoc.peers_rwnd == 0) &&
3301f8829a4aSRandall Stewart 				    ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3302f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_pdrpdiwnp);
3303f8829a4aSRandall Stewart 					return (0);
3304f8829a4aSRandall Stewart 				}
3305f8829a4aSRandall Stewart 				if (stcb->asoc.peers_rwnd == 0 &&
3306f8829a4aSRandall Stewart 				    (flg & SCTP_FROM_MIDDLE_BOX)) {
3307f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_pdrpdizrw);
3308f8829a4aSRandall Stewart 					return (0);
3309f8829a4aSRandall Stewart 				}
3310f8829a4aSRandall Stewart 				ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+
3311f8829a4aSRandall Stewart 				    sizeof(struct sctp_data_chunk));
3312f8829a4aSRandall Stewart 				{
3313f8829a4aSRandall Stewart 					unsigned int iii;
3314f8829a4aSRandall Stewart 
3315f8829a4aSRandall Stewart 					for (iii = 0; iii < sizeof(desc->data_bytes);
3316f8829a4aSRandall Stewart 					    iii++) {
3317f8829a4aSRandall Stewart 						if (ddp[iii] != desc->data_bytes[iii]) {
3318f8829a4aSRandall Stewart 							SCTP_STAT_INCR(sctps_pdrpbadd);
3319f8829a4aSRandall Stewart 							return (-1);
3320f8829a4aSRandall Stewart 						}
3321f8829a4aSRandall Stewart 					}
3322f8829a4aSRandall Stewart 				}
3323f8829a4aSRandall Stewart 
3324f8829a4aSRandall Stewart 				if (tp1->do_rtt) {
3325f8829a4aSRandall Stewart 					/*
3326f8829a4aSRandall Stewart 					 * this guy had a RTO calculation
3327f8829a4aSRandall Stewart 					 * pending on it, cancel it
3328f8829a4aSRandall Stewart 					 */
3329f79aab18SRandall Stewart 					if (tp1->whoTo->rto_needed == 0) {
3330f79aab18SRandall Stewart 						tp1->whoTo->rto_needed = 1;
3331f79aab18SRandall Stewart 					}
3332f8829a4aSRandall Stewart 					tp1->do_rtt = 0;
3333f8829a4aSRandall Stewart 				}
3334f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpmark);
3335f8829a4aSRandall Stewart 				if (tp1->sent != SCTP_DATAGRAM_RESEND)
3336f8829a4aSRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3337f8829a4aSRandall Stewart 				/*
3338f8829a4aSRandall Stewart 				 * mark it as if we were doing a FR, since
3339f8829a4aSRandall Stewart 				 * we will be getting gap ack reports behind
3340f8829a4aSRandall Stewart 				 * the info from the router.
3341f8829a4aSRandall Stewart 				 */
3342f8829a4aSRandall Stewart 				tp1->rec.data.doing_fast_retransmit = 1;
3343f8829a4aSRandall Stewart 				/*
3344f8829a4aSRandall Stewart 				 * mark the tsn with what sequences can
3345f8829a4aSRandall Stewart 				 * cause a new FR.
3346f8829a4aSRandall Stewart 				 */
3347f8829a4aSRandall Stewart 				if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3348f8829a4aSRandall Stewart 					tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3349f8829a4aSRandall Stewart 				} else {
3350f8829a4aSRandall Stewart 					tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
3351f8829a4aSRandall Stewart 				}
3352f8829a4aSRandall Stewart 
3353f8829a4aSRandall Stewart 				/* restart the timer */
3354f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3355b7d130beSMichael Tuexen 				    stcb, tp1->whoTo,
3356b7d130beSMichael Tuexen 				    SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3357f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3358f8829a4aSRandall Stewart 				    stcb, tp1->whoTo);
3359f8829a4aSRandall Stewart 
3360f8829a4aSRandall Stewart 				/* fix counts and things */
3361b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3362c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3363a5d547adSRandall Stewart 					    tp1->whoTo->flight_size,
3364a5d547adSRandall Stewart 					    tp1->book_size,
33659a8e3088SMichael Tuexen 					    (uint32_t) (uintptr_t) stcb,
3366a5d547adSRandall Stewart 					    tp1->rec.data.TSN_seq);
336780fefe0aSRandall Stewart 				}
33688933fa13SRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3369c105859eSRandall Stewart 					sctp_flight_size_decrease(tp1);
3370c105859eSRandall Stewart 					sctp_total_flight_decrease(stcb, tp1);
33718933fa13SRandall Stewart 				}
3372f23ba7b1SMichael Tuexen 				tp1->sent = SCTP_DATAGRAM_RESEND;
3373f8829a4aSRandall Stewart 			} {
3374f8829a4aSRandall Stewart 				/* audit code */
3375f8829a4aSRandall Stewart 				unsigned int audit;
3376f8829a4aSRandall Stewart 
3377f8829a4aSRandall Stewart 				audit = 0;
3378f8829a4aSRandall Stewart 				TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3379f8829a4aSRandall Stewart 					if (tp1->sent == SCTP_DATAGRAM_RESEND)
3380f8829a4aSRandall Stewart 						audit++;
3381f8829a4aSRandall Stewart 				}
3382f8829a4aSRandall Stewart 				TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3383f8829a4aSRandall Stewart 				    sctp_next) {
3384f8829a4aSRandall Stewart 					if (tp1->sent == SCTP_DATAGRAM_RESEND)
3385f8829a4aSRandall Stewart 						audit++;
3386f8829a4aSRandall Stewart 				}
3387f8829a4aSRandall Stewart 				if (audit != stcb->asoc.sent_queue_retran_cnt) {
3388ad81507eSRandall Stewart 					SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3389f8829a4aSRandall Stewart 					    audit, stcb->asoc.sent_queue_retran_cnt);
3390f8829a4aSRandall Stewart #ifndef SCTP_AUDITING_ENABLED
3391f8829a4aSRandall Stewart 					stcb->asoc.sent_queue_retran_cnt = audit;
3392f8829a4aSRandall Stewart #endif
3393f8829a4aSRandall Stewart 				}
3394f8829a4aSRandall Stewart 			}
3395f8829a4aSRandall Stewart 		}
3396f8829a4aSRandall Stewart 		break;
3397f8829a4aSRandall Stewart 	case SCTP_ASCONF:
3398f8829a4aSRandall Stewart 		{
3399f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *asconf;
3400f8829a4aSRandall Stewart 
3401f8829a4aSRandall Stewart 			TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3402f8829a4aSRandall Stewart 			    sctp_next) {
3403f8829a4aSRandall Stewart 				if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3404f8829a4aSRandall Stewart 					break;
3405f8829a4aSRandall Stewart 				}
3406f8829a4aSRandall Stewart 			}
3407f8829a4aSRandall Stewart 			if (asconf) {
3408f8829a4aSRandall Stewart 				if (asconf->sent != SCTP_DATAGRAM_RESEND)
3409f8829a4aSRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3410f8829a4aSRandall Stewart 				asconf->sent = SCTP_DATAGRAM_RESEND;
3411f8829a4aSRandall Stewart 				asconf->snd_count--;
3412f8829a4aSRandall Stewart 			}
3413f8829a4aSRandall Stewart 		}
3414f8829a4aSRandall Stewart 		break;
3415f8829a4aSRandall Stewart 	case SCTP_INITIATION:
3416f8829a4aSRandall Stewart 		/* resend the INIT */
3417f8829a4aSRandall Stewart 		stcb->asoc.dropped_special_cnt++;
3418f8829a4aSRandall Stewart 		if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3419f8829a4aSRandall Stewart 			/*
3420f8829a4aSRandall Stewart 			 * If we can get it in, in a few attempts we do
3421f8829a4aSRandall Stewart 			 * this, otherwise we let the timer fire.
3422f8829a4aSRandall Stewart 			 */
3423f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3424b7d130beSMichael Tuexen 			    stcb, net,
3425b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
3426ceaad40aSRandall Stewart 			sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3427f8829a4aSRandall Stewart 		}
3428f8829a4aSRandall Stewart 		break;
3429f8829a4aSRandall Stewart 	case SCTP_SELECTIVE_ACK:
3430b5c16493SMichael Tuexen 	case SCTP_NR_SELECTIVE_ACK:
3431f8829a4aSRandall Stewart 		/* resend the sack */
3432689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3433f8829a4aSRandall Stewart 		break;
3434f8829a4aSRandall Stewart 	case SCTP_HEARTBEAT_REQUEST:
3435f8829a4aSRandall Stewart 		/* resend a demand HB */
3436b54d3a6cSRandall Stewart 		if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3437b54d3a6cSRandall Stewart 			/*
3438b54d3a6cSRandall Stewart 			 * Only retransmit if we KNOW we wont destroy the
3439b54d3a6cSRandall Stewart 			 * tcb
3440b54d3a6cSRandall Stewart 			 */
3441ca85e948SMichael Tuexen 			sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3442b54d3a6cSRandall Stewart 		}
3443f8829a4aSRandall Stewart 		break;
3444f8829a4aSRandall Stewart 	case SCTP_SHUTDOWN:
3445f8829a4aSRandall Stewart 		sctp_send_shutdown(stcb, net);
3446f8829a4aSRandall Stewart 		break;
3447f8829a4aSRandall Stewart 	case SCTP_SHUTDOWN_ACK:
3448f8829a4aSRandall Stewart 		sctp_send_shutdown_ack(stcb, net);
3449f8829a4aSRandall Stewart 		break;
3450f8829a4aSRandall Stewart 	case SCTP_COOKIE_ECHO:
3451f8829a4aSRandall Stewart 		{
3452f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *cookie;
3453f8829a4aSRandall Stewart 
3454f8829a4aSRandall Stewart 			cookie = NULL;
3455f8829a4aSRandall Stewart 			TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3456f8829a4aSRandall Stewart 			    sctp_next) {
3457f8829a4aSRandall Stewart 				if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3458f8829a4aSRandall Stewart 					break;
3459f8829a4aSRandall Stewart 				}
3460f8829a4aSRandall Stewart 			}
3461f8829a4aSRandall Stewart 			if (cookie) {
3462f8829a4aSRandall Stewart 				if (cookie->sent != SCTP_DATAGRAM_RESEND)
3463f8829a4aSRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3464f8829a4aSRandall Stewart 				cookie->sent = SCTP_DATAGRAM_RESEND;
3465f8829a4aSRandall Stewart 				sctp_stop_all_cookie_timers(stcb);
3466f8829a4aSRandall Stewart 			}
3467f8829a4aSRandall Stewart 		}
3468f8829a4aSRandall Stewart 		break;
3469f8829a4aSRandall Stewart 	case SCTP_COOKIE_ACK:
3470f8829a4aSRandall Stewart 		sctp_send_cookie_ack(stcb);
3471f8829a4aSRandall Stewart 		break;
3472f8829a4aSRandall Stewart 	case SCTP_ASCONF_ACK:
3473f8829a4aSRandall Stewart 		/* resend last asconf ack */
34742afb3e84SRandall Stewart 		sctp_send_asconf_ack(stcb);
3475f8829a4aSRandall Stewart 		break;
3476*44249214SRandall Stewart 	case SCTP_IFORWARD_CUM_TSN:
3477f8829a4aSRandall Stewart 	case SCTP_FORWARD_CUM_TSN:
3478f8829a4aSRandall Stewart 		send_forward_tsn(stcb, &stcb->asoc);
3479f8829a4aSRandall Stewart 		break;
3480f8829a4aSRandall Stewart 		/* can't do anything with these */
3481f8829a4aSRandall Stewart 	case SCTP_PACKET_DROPPED:
3482f8829a4aSRandall Stewart 	case SCTP_INITIATION_ACK:	/* this should not happen */
3483f8829a4aSRandall Stewart 	case SCTP_HEARTBEAT_ACK:
3484f8829a4aSRandall Stewart 	case SCTP_ABORT_ASSOCIATION:
3485f8829a4aSRandall Stewart 	case SCTP_OPERATION_ERROR:
3486f8829a4aSRandall Stewart 	case SCTP_SHUTDOWN_COMPLETE:
3487f8829a4aSRandall Stewart 	case SCTP_ECN_ECHO:
3488f8829a4aSRandall Stewart 	case SCTP_ECN_CWR:
3489f8829a4aSRandall Stewart 	default:
3490f8829a4aSRandall Stewart 		break;
3491f8829a4aSRandall Stewart 	}
3492f8829a4aSRandall Stewart 	return (0);
3493f8829a4aSRandall Stewart }
3494f8829a4aSRandall Stewart 
3495f8829a4aSRandall Stewart void
3496a169d6ecSMichael Tuexen sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t * list)
3497f8829a4aSRandall Stewart {
3498a169d6ecSMichael Tuexen 	uint32_t i;
3499f8829a4aSRandall Stewart 	uint16_t temp;
3500f8829a4aSRandall Stewart 
3501f8829a4aSRandall Stewart 	/*
3502*44249214SRandall Stewart 	 * We set things to 0xffffffff since this is the last delivered
3503*44249214SRandall Stewart 	 * sequence and we will be sending in 0 after the reset.
3504f8829a4aSRandall Stewart 	 */
3505f8829a4aSRandall Stewart 
3506f8829a4aSRandall Stewart 	if (number_entries) {
3507f8829a4aSRandall Stewart 		for (i = 0; i < number_entries; i++) {
3508f8829a4aSRandall Stewart 			temp = ntohs(list[i]);
3509f8829a4aSRandall Stewart 			if (temp >= stcb->asoc.streamincnt) {
3510f8829a4aSRandall Stewart 				continue;
3511f8829a4aSRandall Stewart 			}
3512*44249214SRandall Stewart 			stcb->asoc.strmin[temp].last_sequence_delivered = 0xffffffff;
3513f8829a4aSRandall Stewart 		}
3514f8829a4aSRandall Stewart 	} else {
3515f8829a4aSRandall Stewart 		list = NULL;
3516f8829a4aSRandall Stewart 		for (i = 0; i < stcb->asoc.streamincnt; i++) {
3517*44249214SRandall Stewart 			stcb->asoc.strmin[i].last_sequence_delivered = 0xffffffff;
3518f8829a4aSRandall Stewart 		}
3519f8829a4aSRandall Stewart 	}
3520ceaad40aSRandall Stewart 	sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3521f8829a4aSRandall Stewart }
3522f8829a4aSRandall Stewart 
3523f8829a4aSRandall Stewart static void
352456f778aaSMichael Tuexen sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t * list)
3525f8829a4aSRandall Stewart {
352656f778aaSMichael Tuexen 	uint32_t i;
3527f8829a4aSRandall Stewart 	uint16_t temp;
3528f8829a4aSRandall Stewart 
352956f778aaSMichael Tuexen 	if (number_entries > 0) {
353056f778aaSMichael Tuexen 		for (i = 0; i < number_entries; i++) {
3531f8829a4aSRandall Stewart 			temp = ntohs(list[i]);
3532f8829a4aSRandall Stewart 			if (temp >= stcb->asoc.streamoutcnt) {
3533f8829a4aSRandall Stewart 				/* no such stream */
3534f8829a4aSRandall Stewart 				continue;
3535f8829a4aSRandall Stewart 			}
3536f3b05218SMichael Tuexen 			stcb->asoc.strmout[temp].next_sequence_send = 0;
3537f8829a4aSRandall Stewart 		}
353856f778aaSMichael Tuexen 	} else {
353956f778aaSMichael Tuexen 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
354056f778aaSMichael Tuexen 			stcb->asoc.strmout[i].next_sequence_send = 0;
354156f778aaSMichael Tuexen 		}
3542f8829a4aSRandall Stewart 	}
3543ceaad40aSRandall Stewart 	sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3544f8829a4aSRandall Stewart }
3545f8829a4aSRandall Stewart 
35467cca1775SRandall Stewart static void
35477cca1775SRandall Stewart sctp_reset_clear_pending(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t * list)
35487cca1775SRandall Stewart {
35497cca1775SRandall Stewart 	uint32_t i;
35507cca1775SRandall Stewart 	uint16_t temp;
35517cca1775SRandall Stewart 
35527cca1775SRandall Stewart 	if (number_entries > 0) {
35537cca1775SRandall Stewart 		for (i = 0; i < number_entries; i++) {
35547cca1775SRandall Stewart 			temp = ntohs(list[i]);
35557cca1775SRandall Stewart 			if (temp >= stcb->asoc.streamoutcnt) {
35567cca1775SRandall Stewart 				/* no such stream */
35577cca1775SRandall Stewart 				continue;
35587cca1775SRandall Stewart 			}
35597cca1775SRandall Stewart 			stcb->asoc.strmout[temp].state = SCTP_STREAM_OPEN;
35607cca1775SRandall Stewart 		}
35617cca1775SRandall Stewart 	} else {
35627cca1775SRandall Stewart 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
35637cca1775SRandall Stewart 			stcb->asoc.strmout[i].state = SCTP_STREAM_OPEN;
35647cca1775SRandall Stewart 		}
35657cca1775SRandall Stewart 	}
35667cca1775SRandall Stewart }
35677cca1775SRandall Stewart 
3568f8829a4aSRandall Stewart 
356984f3b49aSMichael Tuexen struct sctp_stream_reset_request *
3570f8829a4aSRandall Stewart sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3571f8829a4aSRandall Stewart {
3572f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3573a169d6ecSMichael Tuexen 	struct sctp_chunkhdr *ch;
357484f3b49aSMichael Tuexen 	struct sctp_stream_reset_request *r;
3575f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
3576f8829a4aSRandall Stewart 	int len, clen;
3577f8829a4aSRandall Stewart 
3578f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
3579f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3580d06c82f1SRandall Stewart 		asoc->stream_reset_outstanding = 0;
3581f8829a4aSRandall Stewart 		return (NULL);
3582f8829a4aSRandall Stewart 	}
3583f8829a4aSRandall Stewart 	if (stcb->asoc.str_reset == NULL) {
3584d06c82f1SRandall Stewart 		asoc->stream_reset_outstanding = 0;
3585f8829a4aSRandall Stewart 		return (NULL);
3586f8829a4aSRandall Stewart 	}
3587f8829a4aSRandall Stewart 	chk = stcb->asoc.str_reset;
3588f8829a4aSRandall Stewart 	if (chk->data == NULL) {
3589f8829a4aSRandall Stewart 		return (NULL);
3590f8829a4aSRandall Stewart 	}
3591f8829a4aSRandall Stewart 	if (bchk) {
3592f8829a4aSRandall Stewart 		/* he wants a copy of the chk pointer */
3593f8829a4aSRandall Stewart 		*bchk = chk;
3594f8829a4aSRandall Stewart 	}
3595f8829a4aSRandall Stewart 	clen = chk->send_size;
3596a169d6ecSMichael Tuexen 	ch = mtod(chk->data, struct sctp_chunkhdr *);
359784f3b49aSMichael Tuexen 	r = (struct sctp_stream_reset_request *)(ch + 1);
3598f8829a4aSRandall Stewart 	if (ntohl(r->request_seq) == seq) {
3599f8829a4aSRandall Stewart 		/* found it */
3600f8829a4aSRandall Stewart 		return (r);
3601f8829a4aSRandall Stewart 	}
3602f8829a4aSRandall Stewart 	len = SCTP_SIZE32(ntohs(r->ph.param_length));
3603f8829a4aSRandall Stewart 	if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3604f8829a4aSRandall Stewart 		/* move to the next one, there can only be a max of two */
360584f3b49aSMichael Tuexen 		r = (struct sctp_stream_reset_request *)((caddr_t)r + len);
3606f8829a4aSRandall Stewart 		if (ntohl(r->request_seq) == seq) {
3607f8829a4aSRandall Stewart 			return (r);
3608f8829a4aSRandall Stewart 		}
3609f8829a4aSRandall Stewart 	}
3610f8829a4aSRandall Stewart 	/* that seq is not here */
3611f8829a4aSRandall Stewart 	return (NULL);
3612f8829a4aSRandall Stewart }
3613f8829a4aSRandall Stewart 
3614f8829a4aSRandall Stewart static void
3615f8829a4aSRandall Stewart sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3616f8829a4aSRandall Stewart {
3617f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3618f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk = stcb->asoc.str_reset;
3619f8829a4aSRandall Stewart 
3620f8829a4aSRandall Stewart 	if (stcb->asoc.str_reset == NULL) {
3621f8829a4aSRandall Stewart 		return;
3622f8829a4aSRandall Stewart 	}
3623f42a358aSRandall Stewart 	asoc = &stcb->asoc;
3624f42a358aSRandall Stewart 
3625b7d130beSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb,
3626b7d130beSMichael Tuexen 	    chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28);
3627f8829a4aSRandall Stewart 	TAILQ_REMOVE(&asoc->control_send_queue,
3628f8829a4aSRandall Stewart 	    chk,
3629f8829a4aSRandall Stewart 	    sctp_next);
3630f8829a4aSRandall Stewart 	if (chk->data) {
3631f8829a4aSRandall Stewart 		sctp_m_freem(chk->data);
3632f8829a4aSRandall Stewart 		chk->data = NULL;
3633f8829a4aSRandall Stewart 	}
3634f8829a4aSRandall Stewart 	asoc->ctrl_queue_cnt--;
3635689e6a5fSMichael Tuexen 	sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
363604ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
3637f8829a4aSRandall Stewart 	stcb->asoc.str_reset = NULL;
3638f8829a4aSRandall Stewart }
3639f8829a4aSRandall Stewart 
3640f8829a4aSRandall Stewart 
3641f8829a4aSRandall Stewart static int
3642f8829a4aSRandall Stewart sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3643f8829a4aSRandall Stewart     uint32_t seq, uint32_t action,
364408598d70SRandall Stewart     struct sctp_stream_reset_response *respin)
3645f8829a4aSRandall Stewart {
3646f8829a4aSRandall Stewart 	uint16_t type;
3647f8829a4aSRandall Stewart 	int lparm_len;
3648f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3649f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
365084f3b49aSMichael Tuexen 	struct sctp_stream_reset_request *req_param;
365184f3b49aSMichael Tuexen 	struct sctp_stream_reset_out_request *req_out_param;
365284f3b49aSMichael Tuexen 	struct sctp_stream_reset_in_request *req_in_param;
365356f778aaSMichael Tuexen 	uint32_t number_entries;
3654f8829a4aSRandall Stewart 
3655f8829a4aSRandall Stewart 	if (asoc->stream_reset_outstanding == 0) {
3656f8829a4aSRandall Stewart 		/* duplicate */
3657f8829a4aSRandall Stewart 		return (0);
3658f8829a4aSRandall Stewart 	}
3659f8829a4aSRandall Stewart 	if (seq == stcb->asoc.str_reset_seq_out) {
366084f3b49aSMichael Tuexen 		req_param = sctp_find_stream_reset(stcb, seq, &chk);
366184f3b49aSMichael Tuexen 		if (req_param != NULL) {
3662f8829a4aSRandall Stewart 			stcb->asoc.str_reset_seq_out++;
366384f3b49aSMichael Tuexen 			type = ntohs(req_param->ph.param_type);
366484f3b49aSMichael Tuexen 			lparm_len = ntohs(req_param->ph.param_length);
3665f8829a4aSRandall Stewart 			if (type == SCTP_STR_RESET_OUT_REQUEST) {
36667cca1775SRandall Stewart 				int no_clear = 0;
36677cca1775SRandall Stewart 
366884f3b49aSMichael Tuexen 				req_out_param = (struct sctp_stream_reset_out_request *)req_param;
366908598d70SRandall Stewart 				number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3670f8829a4aSRandall Stewart 				asoc->stream_reset_out_is_outstanding = 0;
3671f8829a4aSRandall Stewart 				if (asoc->stream_reset_outstanding)
3672f8829a4aSRandall Stewart 					asoc->stream_reset_outstanding--;
3673f3ebe64cSMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3674f8829a4aSRandall Stewart 					/* do it */
367584f3b49aSMichael Tuexen 					sctp_reset_out_streams(stcb, number_entries, req_out_param->list_of_streams);
3676d4260646SMichael Tuexen 				} else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
367784f3b49aSMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
36787cca1775SRandall Stewart 				} else if (action == SCTP_STREAM_RESET_RESULT_IN_PROGRESS) {
36797cca1775SRandall Stewart 					/*
36807cca1775SRandall Stewart 					 * Set it up so we don't stop
36817cca1775SRandall Stewart 					 * retransmitting
36827cca1775SRandall Stewart 					 */
3683a4889f2dSMichael Tuexen 					asoc->stream_reset_outstanding++;
36847cca1775SRandall Stewart 					stcb->asoc.str_reset_seq_out--;
36857cca1775SRandall Stewart 					asoc->stream_reset_out_is_outstanding = 1;
36867cca1775SRandall Stewart 					no_clear = 1;
3687f8829a4aSRandall Stewart 				} else {
368884f3b49aSMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3689f8829a4aSRandall Stewart 				}
36907cca1775SRandall Stewart 				if (no_clear == 0) {
36917cca1775SRandall Stewart 					sctp_reset_clear_pending(stcb, number_entries, req_out_param->list_of_streams);
36927cca1775SRandall Stewart 				}
3693f8829a4aSRandall Stewart 			} else if (type == SCTP_STR_RESET_IN_REQUEST) {
369484f3b49aSMichael Tuexen 				req_in_param = (struct sctp_stream_reset_in_request *)req_param;
369508598d70SRandall Stewart 				number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3696f8829a4aSRandall Stewart 				if (asoc->stream_reset_outstanding)
3697f8829a4aSRandall Stewart 					asoc->stream_reset_outstanding--;
3698d4260646SMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3699d4260646SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
370084f3b49aSMichael Tuexen 					    number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3701d4260646SMichael Tuexen 				} else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3702c4e848b7SRandall Stewart 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
370384f3b49aSMichael Tuexen 					    number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3704f8829a4aSRandall Stewart 				}
3705c4e848b7SRandall Stewart 			} else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3706ea44232bSRandall Stewart 				/* Ok we now may have more streams */
3707c4e848b7SRandall Stewart 				int num_stream;
3708c4e848b7SRandall Stewart 
3709c4e848b7SRandall Stewart 				num_stream = stcb->asoc.strm_pending_add_size;
3710c4e848b7SRandall Stewart 				if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3711c4e848b7SRandall Stewart 					/* TSNH */
3712c4e848b7SRandall Stewart 					num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3713c4e848b7SRandall Stewart 				}
3714c4e848b7SRandall Stewart 				stcb->asoc.strm_pending_add_size = 0;
37158aae9493SRandall Stewart 				if (asoc->stream_reset_outstanding)
37168aae9493SRandall Stewart 					asoc->stream_reset_outstanding--;
3717f3ebe64cSMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3718ea44232bSRandall Stewart 					/* Put the new streams into effect */
37197cca1775SRandall Stewart 					int i;
37207cca1775SRandall Stewart 
37217cca1775SRandall Stewart 					for (i = asoc->streamoutcnt; i < (asoc->streamoutcnt + num_stream); i++) {
37227cca1775SRandall Stewart 						asoc->strmout[i].state = SCTP_STREAM_OPEN;
37237cca1775SRandall Stewart 					}
37247cca1775SRandall Stewart 					asoc->streamoutcnt += num_stream;
3725c4e848b7SRandall Stewart 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3726d4260646SMichael Tuexen 				} else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3727d4260646SMichael Tuexen 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3728d4260646SMichael Tuexen 					    SCTP_STREAM_CHANGE_DENIED);
3729ea44232bSRandall Stewart 				} else {
3730c4e848b7SRandall Stewart 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3731d4260646SMichael Tuexen 					    SCTP_STREAM_CHANGE_FAILED);
3732c4e848b7SRandall Stewart 				}
3733c4e848b7SRandall Stewart 			} else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3734c4e848b7SRandall Stewart 				if (asoc->stream_reset_outstanding)
3735c4e848b7SRandall Stewart 					asoc->stream_reset_outstanding--;
3736d4260646SMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3737c4e848b7SRandall Stewart 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3738d4260646SMichael Tuexen 					    SCTP_STREAM_CHANGE_DENIED);
3739d4260646SMichael Tuexen 				} else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3740d4260646SMichael Tuexen 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3741d4260646SMichael Tuexen 					    SCTP_STREAM_CHANGE_FAILED);
3742ea44232bSRandall Stewart 				}
3743f8829a4aSRandall Stewart 			} else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3744f8829a4aSRandall Stewart 				/**
3745f8829a4aSRandall Stewart 				 * a) Adopt the new in tsn.
3746f8829a4aSRandall Stewart 				 * b) reset the map
3747f8829a4aSRandall Stewart 				 * c) Adopt the new out-tsn
3748f8829a4aSRandall Stewart 				 */
3749f8829a4aSRandall Stewart 				struct sctp_stream_reset_response_tsn *resp;
3750f8829a4aSRandall Stewart 				struct sctp_forward_tsn_chunk fwdtsn;
3751f8829a4aSRandall Stewart 				int abort_flag = 0;
3752f8829a4aSRandall Stewart 
3753f8829a4aSRandall Stewart 				if (respin == NULL) {
3754f8829a4aSRandall Stewart 					/* huh ? */
3755f8829a4aSRandall Stewart 					return (0);
3756f8829a4aSRandall Stewart 				}
37576a58f0e9SXin LI 				if (ntohs(respin->ph.param_length) < sizeof(struct sctp_stream_reset_response_tsn)) {
37586a58f0e9SXin LI 					return (0);
37596a58f0e9SXin LI 				}
3760f3ebe64cSMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3761f8829a4aSRandall Stewart 					resp = (struct sctp_stream_reset_response_tsn *)respin;
3762f8829a4aSRandall Stewart 					asoc->stream_reset_outstanding--;
3763f8829a4aSRandall Stewart 					fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3764f8829a4aSRandall Stewart 					fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3765f8829a4aSRandall Stewart 					fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3766671d309cSRandall Stewart 					sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3767f8829a4aSRandall Stewart 					if (abort_flag) {
3768f8829a4aSRandall Stewart 						return (1);
3769f8829a4aSRandall Stewart 					}
3770f8829a4aSRandall Stewart 					stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3771b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3772b3f1ea41SRandall Stewart 						sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3773b3f1ea41SRandall Stewart 					}
3774d6af161aSRandall Stewart 					stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3775f8829a4aSRandall Stewart 					stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3776f8829a4aSRandall Stewart 					memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3777830d754dSRandall Stewart 
3778830d754dSRandall Stewart 					stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3779b5c16493SMichael Tuexen 					memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
378077acdc25SRandall Stewart 
3781f8829a4aSRandall Stewart 					stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3782f8829a4aSRandall Stewart 					stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3783f8829a4aSRandall Stewart 
3784f8829a4aSRandall Stewart 					sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3785f8829a4aSRandall Stewart 					sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3786c4e848b7SRandall Stewart 					sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3787d4260646SMichael Tuexen 				} else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3788d4260646SMichael Tuexen 					sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3789d4260646SMichael Tuexen 					    SCTP_ASSOC_RESET_DENIED);
3790c4e848b7SRandall Stewart 				} else {
3791c4e848b7SRandall Stewart 					sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3792d4260646SMichael Tuexen 					    SCTP_ASSOC_RESET_FAILED);
3793f8829a4aSRandall Stewart 				}
3794f8829a4aSRandall Stewart 			}
3795f8829a4aSRandall Stewart 			/* get rid of the request and get the request flags */
3796f8829a4aSRandall Stewart 			if (asoc->stream_reset_outstanding == 0) {
3797f8829a4aSRandall Stewart 				sctp_clean_up_stream_reset(stcb);
3798f8829a4aSRandall Stewart 			}
3799f8829a4aSRandall Stewart 		}
3800f8829a4aSRandall Stewart 	}
38017cca1775SRandall Stewart 	if (asoc->stream_reset_outstanding == 0) {
3802c6168599SRandall Stewart 		sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
38037cca1775SRandall Stewart 	}
3804f8829a4aSRandall Stewart 	return (0);
3805f8829a4aSRandall Stewart }
3806f8829a4aSRandall Stewart 
3807f8829a4aSRandall Stewart static void
3808f8829a4aSRandall Stewart sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3809f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk,
3810671d309cSRandall Stewart     struct sctp_stream_reset_in_request *req, int trunc)
3811f8829a4aSRandall Stewart {
3812f8829a4aSRandall Stewart 	uint32_t seq;
3813f8829a4aSRandall Stewart 	int len, i;
3814f8829a4aSRandall Stewart 	int number_entries;
3815f8829a4aSRandall Stewart 	uint16_t temp;
3816f8829a4aSRandall Stewart 
3817f8829a4aSRandall Stewart 	/*
3818f8829a4aSRandall Stewart 	 * peer wants me to send a str-reset to him for my outgoing seq's if
3819f8829a4aSRandall Stewart 	 * seq_in is right.
3820f8829a4aSRandall Stewart 	 */
3821f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3822f8829a4aSRandall Stewart 
3823f8829a4aSRandall Stewart 	seq = ntohl(req->request_seq);
3824f8829a4aSRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3825671d309cSRandall Stewart 		asoc->last_reset_action[1] = asoc->last_reset_action[0];
3826f3ebe64cSMichael Tuexen 		if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3827f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3828f3ebe64cSMichael Tuexen 		} else if (trunc) {
3829f3ebe64cSMichael Tuexen 			/* Can't do it, since they exceeded our buffer size  */
3830f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3831671d309cSRandall Stewart 		} else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3832f8829a4aSRandall Stewart 			len = ntohs(req->ph.param_length);
3833f8829a4aSRandall Stewart 			number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
38347cca1775SRandall Stewart 			if (number_entries) {
3835f8829a4aSRandall Stewart 				for (i = 0; i < number_entries; i++) {
3836f8829a4aSRandall Stewart 					temp = ntohs(req->list_of_streams[i]);
38377cca1775SRandall Stewart 					if (temp >= stcb->asoc.streamoutcnt) {
38387cca1775SRandall Stewart 						asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
38397cca1775SRandall Stewart 						goto bad_boy;
38407cca1775SRandall Stewart 					}
3841f8829a4aSRandall Stewart 					req->list_of_streams[i] = temp;
3842f8829a4aSRandall Stewart 				}
38437cca1775SRandall Stewart 				for (i = 0; i < number_entries; i++) {
38447cca1775SRandall Stewart 					if (stcb->asoc.strmout[req->list_of_streams[i]].state == SCTP_STREAM_OPEN) {
38457cca1775SRandall Stewart 						stcb->asoc.strmout[req->list_of_streams[i]].state = SCTP_STREAM_RESET_PENDING;
38467cca1775SRandall Stewart 					}
38477cca1775SRandall Stewart 				}
38487cca1775SRandall Stewart 			} else {
38497cca1775SRandall Stewart 				/* Its all */
38507cca1775SRandall Stewart 				for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
38517cca1775SRandall Stewart 					if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN)
38527cca1775SRandall Stewart 						stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
38537cca1775SRandall Stewart 				}
38547cca1775SRandall Stewart 			}
3855f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3856f8829a4aSRandall Stewart 		} else {
3857f8829a4aSRandall Stewart 			/* Can't do it, since we have sent one out */
3858f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3859f8829a4aSRandall Stewart 		}
38607cca1775SRandall Stewart bad_boy:
3861c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3862f8829a4aSRandall Stewart 		asoc->str_reset_seq_in++;
3863f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 1 == seq) {
3864f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3865f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 2 == seq) {
3866f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3867f8829a4aSRandall Stewart 	} else {
3868f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3869f8829a4aSRandall Stewart 	}
3870c6168599SRandall Stewart 	sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3871f8829a4aSRandall Stewart }
3872f8829a4aSRandall Stewart 
3873f8829a4aSRandall Stewart static int
3874f8829a4aSRandall Stewart sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3875f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk,
3876f8829a4aSRandall Stewart     struct sctp_stream_reset_tsn_request *req)
3877f8829a4aSRandall Stewart {
3878f8829a4aSRandall Stewart 	/* reset all in and out and update the tsn */
3879f8829a4aSRandall Stewart 	/*
3880f8829a4aSRandall Stewart 	 * A) reset my str-seq's on in and out. B) Select a receive next,
3881f8829a4aSRandall Stewart 	 * and set cum-ack to it. Also process this selected number as a
3882f8829a4aSRandall Stewart 	 * fwd-tsn as well. C) set in the response my next sending seq.
3883f8829a4aSRandall Stewart 	 */
3884f8829a4aSRandall Stewart 	struct sctp_forward_tsn_chunk fwdtsn;
3885f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3886f8829a4aSRandall Stewart 	int abort_flag = 0;
3887f8829a4aSRandall Stewart 	uint32_t seq;
3888f8829a4aSRandall Stewart 
3889f8829a4aSRandall Stewart 	seq = ntohl(req->request_seq);
3890f8829a4aSRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3891ce228dabSMichael Tuexen 		asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
3892f3ebe64cSMichael Tuexen 		if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3893f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3894ce228dabSMichael Tuexen 		} else {
3895f8829a4aSRandall Stewart 			fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3896f8829a4aSRandall Stewart 			fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3897f8829a4aSRandall Stewart 			fwdtsn.ch.chunk_flags = 0;
3898f8829a4aSRandall Stewart 			fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3899671d309cSRandall Stewart 			sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3900f8829a4aSRandall Stewart 			if (abort_flag) {
3901f8829a4aSRandall Stewart 				return (1);
3902f8829a4aSRandall Stewart 			}
3903f3ebe64cSMichael Tuexen 			asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3904b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3905b3f1ea41SRandall Stewart 				sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3906b3f1ea41SRandall Stewart 			}
3907f3ebe64cSMichael Tuexen 			asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3908f3ebe64cSMichael Tuexen 			asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3909f3ebe64cSMichael Tuexen 			memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3910f3ebe64cSMichael Tuexen 			asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3911f3ebe64cSMichael Tuexen 			memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3912f3ebe64cSMichael Tuexen 			atomic_add_int(&asoc->sending_seq, 1);
3913f8829a4aSRandall Stewart 			/* save off historical data for retrans */
3914f3ebe64cSMichael Tuexen 			asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3915f3ebe64cSMichael Tuexen 			asoc->last_sending_seq[0] = asoc->sending_seq;
3916f3ebe64cSMichael Tuexen 			asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3917f3ebe64cSMichael Tuexen 			asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3918f8829a4aSRandall Stewart 			sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3919f8829a4aSRandall Stewart 			sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3920f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3921f3ebe64cSMichael Tuexen 			sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3922ce228dabSMichael Tuexen 		}
3923ce228dabSMichael Tuexen 		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3924ce228dabSMichael Tuexen 		    asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3925f8829a4aSRandall Stewart 		asoc->str_reset_seq_in++;
3926f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 1 == seq) {
3927f8829a4aSRandall Stewart 		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3928f3ebe64cSMichael Tuexen 		    asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3929f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 2 == seq) {
3930f8829a4aSRandall Stewart 		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3931f3ebe64cSMichael Tuexen 		    asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3932f8829a4aSRandall Stewart 	} else {
3933f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3934f8829a4aSRandall Stewart 	}
3935f8829a4aSRandall Stewart 	return (0);
3936f8829a4aSRandall Stewart }
3937f8829a4aSRandall Stewart 
3938f8829a4aSRandall Stewart static void
3939f8829a4aSRandall Stewart sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3940f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk,
3941671d309cSRandall Stewart     struct sctp_stream_reset_out_request *req, int trunc)
3942f8829a4aSRandall Stewart {
3943f8829a4aSRandall Stewart 	uint32_t seq, tsn;
3944f8829a4aSRandall Stewart 	int number_entries, len;
3945f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3946f8829a4aSRandall Stewart 
3947f8829a4aSRandall Stewart 	seq = ntohl(req->request_seq);
3948f8829a4aSRandall Stewart 
3949f8829a4aSRandall Stewart 	/* now if its not a duplicate we process it */
3950f8829a4aSRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3951f8829a4aSRandall Stewart 		len = ntohs(req->ph.param_length);
3952f8829a4aSRandall Stewart 		number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3953f8829a4aSRandall Stewart 		/*
3954f8829a4aSRandall Stewart 		 * the sender is resetting, handle the list issue.. we must
3955f8829a4aSRandall Stewart 		 * a) verify if we can do the reset, if so no problem b) If
3956f8829a4aSRandall Stewart 		 * we can't do the reset we must copy the request. c) queue
3957f8829a4aSRandall Stewart 		 * it, and setup the data in processor to trigger it off
3958f8829a4aSRandall Stewart 		 * when needed and dequeue all the queued data.
3959f8829a4aSRandall Stewart 		 */
3960f8829a4aSRandall Stewart 		tsn = ntohl(req->send_reset_at_tsn);
3961f8829a4aSRandall Stewart 
3962f8829a4aSRandall Stewart 		/* move the reset action back one */
3963f8829a4aSRandall Stewart 		asoc->last_reset_action[1] = asoc->last_reset_action[0];
3964f3ebe64cSMichael Tuexen 		if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3965f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3966f3ebe64cSMichael Tuexen 		} else if (trunc) {
3967f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
396820b07a4dSMichael Tuexen 		} else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3969f8829a4aSRandall Stewart 			/* we can do it now */
3970f8829a4aSRandall Stewart 			sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3971f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3972f8829a4aSRandall Stewart 		} else {
3973f8829a4aSRandall Stewart 			/*
3974f8829a4aSRandall Stewart 			 * we must queue it up and thus wait for the TSN's
3975f8829a4aSRandall Stewart 			 * to arrive that are at or before tsn
3976f8829a4aSRandall Stewart 			 */
3977f8829a4aSRandall Stewart 			struct sctp_stream_reset_list *liste;
3978f8829a4aSRandall Stewart 			int siz;
3979f8829a4aSRandall Stewart 
3980f8829a4aSRandall Stewart 			siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3981f8829a4aSRandall Stewart 			SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3982207304d4SRandall Stewart 			    siz, SCTP_M_STRESET);
3983f8829a4aSRandall Stewart 			if (liste == NULL) {
3984f8829a4aSRandall Stewart 				/* gak out of memory */
3985f3ebe64cSMichael Tuexen 				asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3986f3ebe64cSMichael Tuexen 				sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3987f8829a4aSRandall Stewart 				return;
3988f8829a4aSRandall Stewart 			}
39897cca1775SRandall Stewart 			liste->seq = seq;
3990f8829a4aSRandall Stewart 			liste->tsn = tsn;
3991f8829a4aSRandall Stewart 			liste->number_entries = number_entries;
3992a169d6ecSMichael Tuexen 			memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t));
3993f8829a4aSRandall Stewart 			TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
39947cca1775SRandall Stewart 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_IN_PROGRESS;
3995f8829a4aSRandall Stewart 		}
3996c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3997f8829a4aSRandall Stewart 		asoc->str_reset_seq_in++;
3998f8829a4aSRandall Stewart 	} else if ((asoc->str_reset_seq_in - 1) == seq) {
3999f8829a4aSRandall Stewart 		/*
4000f8829a4aSRandall Stewart 		 * one seq back, just echo back last action since my
4001f8829a4aSRandall Stewart 		 * response was lost.
4002f8829a4aSRandall Stewart 		 */
4003f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4004f8829a4aSRandall Stewart 	} else if ((asoc->str_reset_seq_in - 2) == seq) {
4005f8829a4aSRandall Stewart 		/*
4006f8829a4aSRandall Stewart 		 * two seq back, just echo back last action since my
4007f8829a4aSRandall Stewart 		 * response was lost.
4008f8829a4aSRandall Stewart 		 */
4009f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4010f8829a4aSRandall Stewart 	} else {
4011f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4012f8829a4aSRandall Stewart 	}
4013f8829a4aSRandall Stewart }
4014f8829a4aSRandall Stewart 
4015ea44232bSRandall Stewart static void
4016ea44232bSRandall Stewart sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4017ea44232bSRandall Stewart     struct sctp_stream_reset_add_strm *str_add)
4018ea44232bSRandall Stewart {
4019ea44232bSRandall Stewart 	/*
4020ea44232bSRandall Stewart 	 * Peer is requesting to add more streams. If its within our
4021ea44232bSRandall Stewart 	 * max-streams we will allow it.
4022ea44232bSRandall Stewart 	 */
4023c4e848b7SRandall Stewart 	uint32_t num_stream, i;
4024ea44232bSRandall Stewart 	uint32_t seq;
40258aae9493SRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
40264a9ef3f8SMichael Tuexen 	struct sctp_queued_to_read *ctl, *nctl;
4027ea44232bSRandall Stewart 
4028ea44232bSRandall Stewart 	/* Get the number. */
4029ea44232bSRandall Stewart 	seq = ntohl(str_add->request_seq);
4030ea44232bSRandall Stewart 	num_stream = ntohs(str_add->number_of_streams);
4031ea44232bSRandall Stewart 	/* Now what would be the new total? */
40328aae9493SRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
4033ea44232bSRandall Stewart 		num_stream += stcb->asoc.streamincnt;
4034f3ebe64cSMichael Tuexen 		stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4035f3ebe64cSMichael Tuexen 		if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4036f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4037f3ebe64cSMichael Tuexen 		} else if ((num_stream > stcb->asoc.max_inbound_streams) ||
4038c4e848b7SRandall Stewart 		    (num_stream > 0xffff)) {
4039ea44232bSRandall Stewart 			/* We must reject it they ask for to many */
4040ea44232bSRandall Stewart 	denied:
4041f3ebe64cSMichael Tuexen 			stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4042ea44232bSRandall Stewart 		} else {
4043ea44232bSRandall Stewart 			/* Ok, we can do that :-) */
4044ea44232bSRandall Stewart 			struct sctp_stream_in *oldstrm;
4045ea44232bSRandall Stewart 
4046ea44232bSRandall Stewart 			/* save off the old */
4047ea44232bSRandall Stewart 			oldstrm = stcb->asoc.strmin;
4048ea44232bSRandall Stewart 			SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
4049ea44232bSRandall Stewart 			    (num_stream * sizeof(struct sctp_stream_in)),
4050ea44232bSRandall Stewart 			    SCTP_M_STRMI);
4051ea44232bSRandall Stewart 			if (stcb->asoc.strmin == NULL) {
4052ea44232bSRandall Stewart 				stcb->asoc.strmin = oldstrm;
4053ea44232bSRandall Stewart 				goto denied;
4054ea44232bSRandall Stewart 			}
4055ea44232bSRandall Stewart 			/* copy off the old data */
40568aae9493SRandall Stewart 			for (i = 0; i < stcb->asoc.streamincnt; i++) {
40578aae9493SRandall Stewart 				TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4058*44249214SRandall Stewart 				TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
40598aae9493SRandall Stewart 				stcb->asoc.strmin[i].stream_no = i;
40608aae9493SRandall Stewart 				stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered;
40618aae9493SRandall Stewart 				stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
4062*44249214SRandall Stewart 				stcb->asoc.strmin[i].pd_api_started = oldstrm[i].pd_api_started;
40638aae9493SRandall Stewart 				/* now anything on those queues? */
4064*44249214SRandall Stewart 				TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next_instrm, nctl) {
4065*44249214SRandall Stewart 					TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next_instrm);
4066*44249214SRandall Stewart 					TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next_instrm);
4067*44249214SRandall Stewart 				}
4068*44249214SRandall Stewart 				TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].uno_inqueue, next_instrm, nctl) {
4069*44249214SRandall Stewart 					TAILQ_REMOVE(&oldstrm[i].uno_inqueue, ctl, next_instrm);
4070*44249214SRandall Stewart 					TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].uno_inqueue, ctl, next_instrm);
40718aae9493SRandall Stewart 				}
40728aae9493SRandall Stewart 			}
4073ea44232bSRandall Stewart 			/* Init the new streams */
4074ea44232bSRandall Stewart 			for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
4075ea44232bSRandall Stewart 				TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4076*44249214SRandall Stewart 				TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
4077ea44232bSRandall Stewart 				stcb->asoc.strmin[i].stream_no = i;
4078*44249214SRandall Stewart 				stcb->asoc.strmin[i].last_sequence_delivered = 0xffffffff;
4079*44249214SRandall Stewart 				stcb->asoc.strmin[i].pd_api_started = 0;
4080ea44232bSRandall Stewart 				stcb->asoc.strmin[i].delivery_started = 0;
4081ea44232bSRandall Stewart 			}
4082ea44232bSRandall Stewart 			SCTP_FREE(oldstrm, SCTP_M_STRMI);
4083ea44232bSRandall Stewart 			/* update the size */
4084ea44232bSRandall Stewart 			stcb->asoc.streamincnt = num_stream;
4085f3ebe64cSMichael Tuexen 			stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4086c4e848b7SRandall Stewart 			sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
4087ea44232bSRandall Stewart 		}
4088c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4089c4e848b7SRandall Stewart 		asoc->str_reset_seq_in++;
40908aae9493SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 1) == seq) {
40918aae9493SRandall Stewart 		/*
40928aae9493SRandall Stewart 		 * one seq back, just echo back last action since my
40938aae9493SRandall Stewart 		 * response was lost.
40948aae9493SRandall Stewart 		 */
40958aae9493SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
40968aae9493SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 2) == seq) {
40978aae9493SRandall Stewart 		/*
40988aae9493SRandall Stewart 		 * two seq back, just echo back last action since my
40998aae9493SRandall Stewart 		 * response was lost.
41008aae9493SRandall Stewart 		 */
41018aae9493SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
41028aae9493SRandall Stewart 	} else {
4103f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
41048aae9493SRandall Stewart 
41058aae9493SRandall Stewart 	}
4106ea44232bSRandall Stewart }
4107ea44232bSRandall Stewart 
4108c4e848b7SRandall Stewart static void
4109c4e848b7SRandall Stewart sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4110c4e848b7SRandall Stewart     struct sctp_stream_reset_add_strm *str_add)
4111c4e848b7SRandall Stewart {
4112c4e848b7SRandall Stewart 	/*
4113c4e848b7SRandall Stewart 	 * Peer is requesting to add more streams. If its within our
4114c4e848b7SRandall Stewart 	 * max-streams we will allow it.
4115c4e848b7SRandall Stewart 	 */
4116c4e848b7SRandall Stewart 	uint16_t num_stream;
4117c4e848b7SRandall Stewart 	uint32_t seq;
4118c4e848b7SRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
4119c4e848b7SRandall Stewart 
4120c4e848b7SRandall Stewart 	/* Get the number. */
4121c4e848b7SRandall Stewart 	seq = ntohl(str_add->request_seq);
4122c4e848b7SRandall Stewart 	num_stream = ntohs(str_add->number_of_streams);
4123c4e848b7SRandall Stewart 	/* Now what would be the new total? */
4124c4e848b7SRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
4125c4e848b7SRandall Stewart 		stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4126f3ebe64cSMichael Tuexen 		if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4127f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4128f3ebe64cSMichael Tuexen 		} else if (stcb->asoc.stream_reset_outstanding) {
4129f3ebe64cSMichael Tuexen 			/* We must reject it we have something pending */
4130f3ebe64cSMichael Tuexen 			stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
4131c4e848b7SRandall Stewart 		} else {
4132c4e848b7SRandall Stewart 			/* Ok, we can do that :-) */
4133c4e848b7SRandall Stewart 			int mychk;
4134c4e848b7SRandall Stewart 
4135c4e848b7SRandall Stewart 			mychk = stcb->asoc.streamoutcnt;
4136c4e848b7SRandall Stewart 			mychk += num_stream;
4137c4e848b7SRandall Stewart 			if (mychk < 0x10000) {
4138f3ebe64cSMichael Tuexen 				stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
41397cca1775SRandall Stewart 				if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 1, num_stream, 0, 1)) {
4140f3ebe64cSMichael Tuexen 					stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4141c4e848b7SRandall Stewart 				}
4142c4e848b7SRandall Stewart 			} else {
4143f3ebe64cSMichael Tuexen 				stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4144c4e848b7SRandall Stewart 			}
4145c4e848b7SRandall Stewart 		}
4146c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
4147c4e848b7SRandall Stewart 		asoc->str_reset_seq_in++;
4148c4e848b7SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 1) == seq) {
4149c4e848b7SRandall Stewart 		/*
4150c4e848b7SRandall Stewart 		 * one seq back, just echo back last action since my
4151c4e848b7SRandall Stewart 		 * response was lost.
4152c4e848b7SRandall Stewart 		 */
4153c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4154c4e848b7SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 2) == seq) {
4155c4e848b7SRandall Stewart 		/*
4156c4e848b7SRandall Stewart 		 * two seq back, just echo back last action since my
4157c4e848b7SRandall Stewart 		 * response was lost.
4158c4e848b7SRandall Stewart 		 */
4159c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4160c4e848b7SRandall Stewart 	} else {
4161f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4162c4e848b7SRandall Stewart 	}
4163c4e848b7SRandall Stewart }
4164c4e848b7SRandall Stewart 
4165671d309cSRandall Stewart #ifdef __GNUC__
4166671d309cSRandall Stewart __attribute__((noinline))
4167671d309cSRandall Stewart #endif
4168f8829a4aSRandall Stewart 	static int
4169671d309cSRandall Stewart 	    sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
4170a169d6ecSMichael Tuexen         struct sctp_chunkhdr *ch_req)
4171f8829a4aSRandall Stewart {
41726a58f0e9SXin LI 	uint16_t remaining_length, param_len, ptype;
4173671d309cSRandall Stewart 	struct sctp_paramhdr pstore;
4174671d309cSRandall Stewart 	uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
4175c4e848b7SRandall Stewart 	uint32_t seq = 0;
4176f8829a4aSRandall Stewart 	int num_req = 0;
4177671d309cSRandall Stewart 	int trunc = 0;
4178f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
4179f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
4180f8829a4aSRandall Stewart 	struct sctp_paramhdr *ph;
4181f42a358aSRandall Stewart 	int ret_code = 0;
4182f42a358aSRandall Stewart 	int num_param = 0;
4183f8829a4aSRandall Stewart 
4184f8829a4aSRandall Stewart 	/* now it may be a reset or a reset-response */
41856a58f0e9SXin LI 	remaining_length = ntohs(ch_req->chunk_length) - sizeof(struct sctp_chunkhdr);
4186f8829a4aSRandall Stewart 
4187f8829a4aSRandall Stewart 	/* setup for adding the response */
4188f8829a4aSRandall Stewart 	sctp_alloc_a_chunk(stcb, chk);
4189f8829a4aSRandall Stewart 	if (chk == NULL) {
4190f8829a4aSRandall Stewart 		return (ret_code);
4191f8829a4aSRandall Stewart 	}
4192e03159eaSMichael Tuexen 	chk->copy_by_ref = 0;
4193f8829a4aSRandall Stewart 	chk->rec.chunk_id.id = SCTP_STREAM_RESET;
4194d06c82f1SRandall Stewart 	chk->rec.chunk_id.can_take_data = 0;
4195e03159eaSMichael Tuexen 	chk->flags = 0;
4196f8829a4aSRandall Stewart 	chk->asoc = &stcb->asoc;
4197f8829a4aSRandall Stewart 	chk->no_fr_allowed = 0;
4198f8829a4aSRandall Stewart 	chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
419944b7479bSRandall Stewart 	chk->book_size_scale = 0;
4200eb1b1807SGleb Smirnoff 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
4201f8829a4aSRandall Stewart 	if (chk->data == NULL) {
4202f8829a4aSRandall Stewart strres_nochunk:
4203f8829a4aSRandall Stewart 		if (chk->data) {
4204f8829a4aSRandall Stewart 			sctp_m_freem(chk->data);
4205f8829a4aSRandall Stewart 			chk->data = NULL;
4206f8829a4aSRandall Stewart 		}
4207689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4208f8829a4aSRandall Stewart 		return (ret_code);
4209f8829a4aSRandall Stewart 	}
4210139bc87fSRandall Stewart 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4211f8829a4aSRandall Stewart 
4212f8829a4aSRandall Stewart 	/* setup chunk parameters */
4213f8829a4aSRandall Stewart 	chk->sent = SCTP_DATAGRAM_UNSENT;
4214f8829a4aSRandall Stewart 	chk->snd_count = 0;
4215ca85e948SMichael Tuexen 	chk->whoTo = NULL;
4216f8829a4aSRandall Stewart 
4217f8829a4aSRandall Stewart 	ch = mtod(chk->data, struct sctp_chunkhdr *);
4218f8829a4aSRandall Stewart 	ch->chunk_type = SCTP_STREAM_RESET;
4219f8829a4aSRandall Stewart 	ch->chunk_flags = 0;
4220f8829a4aSRandall Stewart 	ch->chunk_length = htons(chk->send_size);
4221139bc87fSRandall Stewart 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4222671d309cSRandall Stewart 	offset += sizeof(struct sctp_chunkhdr);
42236a58f0e9SXin LI 	while (remaining_length >= sizeof(struct sctp_paramhdr)) {
4224671d309cSRandall Stewart 		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore);
42256a58f0e9SXin LI 		if (ph == NULL) {
42266a58f0e9SXin LI 			/* TSNH */
4227f8829a4aSRandall Stewart 			break;
4228f8829a4aSRandall Stewart 		}
42296a58f0e9SXin LI 		param_len = ntohs(ph->param_length);
42306a58f0e9SXin LI 		if ((param_len > remaining_length) ||
42316a58f0e9SXin LI 		    (param_len < (sizeof(struct sctp_paramhdr) + sizeof(uint32_t)))) {
42326a58f0e9SXin LI 			/* bad parameter length */
42336a58f0e9SXin LI 			break;
42346a58f0e9SXin LI 		}
42356a58f0e9SXin LI 		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, sizeof(cstore)),
4236671d309cSRandall Stewart 		    (uint8_t *) & cstore);
42376a58f0e9SXin LI 		if (ph == NULL) {
42386a58f0e9SXin LI 			/* TSNH */
42396a58f0e9SXin LI 			break;
42406a58f0e9SXin LI 		}
4241f8829a4aSRandall Stewart 		ptype = ntohs(ph->param_type);
4242f8829a4aSRandall Stewart 		num_param++;
42436a58f0e9SXin LI 		if (param_len > sizeof(cstore)) {
4244671d309cSRandall Stewart 			trunc = 1;
4245671d309cSRandall Stewart 		} else {
4246671d309cSRandall Stewart 			trunc = 0;
4247671d309cSRandall Stewart 		}
4248f8829a4aSRandall Stewart 		if (num_param > SCTP_MAX_RESET_PARAMS) {
4249f8829a4aSRandall Stewart 			/* hit the max of parameters already sorry.. */
4250f8829a4aSRandall Stewart 			break;
4251f8829a4aSRandall Stewart 		}
4252f8829a4aSRandall Stewart 		if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4253f8829a4aSRandall Stewart 			struct sctp_stream_reset_out_request *req_out;
4254f8829a4aSRandall Stewart 
42556a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_out_request)) {
42566a58f0e9SXin LI 				break;
42576a58f0e9SXin LI 			}
4258f8829a4aSRandall Stewart 			req_out = (struct sctp_stream_reset_out_request *)ph;
4259f8829a4aSRandall Stewart 			num_req++;
4260f8829a4aSRandall Stewart 			if (stcb->asoc.stream_reset_outstanding) {
4261f8829a4aSRandall Stewart 				seq = ntohl(req_out->response_seq);
4262f8829a4aSRandall Stewart 				if (seq == stcb->asoc.str_reset_seq_out) {
4263f8829a4aSRandall Stewart 					/* implicit ack */
4264f3ebe64cSMichael Tuexen 					(void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4265f8829a4aSRandall Stewart 				}
4266f8829a4aSRandall Stewart 			}
4267671d309cSRandall Stewart 			sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4268c4e848b7SRandall Stewart 		} else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4269ea44232bSRandall Stewart 			struct sctp_stream_reset_add_strm *str_add;
4270ea44232bSRandall Stewart 
42716a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
42726a58f0e9SXin LI 				break;
42736a58f0e9SXin LI 			}
4274ea44232bSRandall Stewart 			str_add = (struct sctp_stream_reset_add_strm *)ph;
4275ea44232bSRandall Stewart 			num_req++;
4276ea44232bSRandall Stewart 			sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4277c4e848b7SRandall Stewart 		} else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4278c4e848b7SRandall Stewart 			struct sctp_stream_reset_add_strm *str_add;
4279c4e848b7SRandall Stewart 
42806a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
42816a58f0e9SXin LI 				break;
42826a58f0e9SXin LI 			}
4283c4e848b7SRandall Stewart 			str_add = (struct sctp_stream_reset_add_strm *)ph;
4284c4e848b7SRandall Stewart 			num_req++;
4285c4e848b7SRandall Stewart 			sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4286f8829a4aSRandall Stewart 		} else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4287f8829a4aSRandall Stewart 			struct sctp_stream_reset_in_request *req_in;
4288f8829a4aSRandall Stewart 
4289f8829a4aSRandall Stewart 			num_req++;
4290f8829a4aSRandall Stewart 			req_in = (struct sctp_stream_reset_in_request *)ph;
4291671d309cSRandall Stewart 			sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4292f8829a4aSRandall Stewart 		} else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4293f8829a4aSRandall Stewart 			struct sctp_stream_reset_tsn_request *req_tsn;
4294f8829a4aSRandall Stewart 
4295f8829a4aSRandall Stewart 			num_req++;
4296f8829a4aSRandall Stewart 			req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4297f8829a4aSRandall Stewart 			if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4298f8829a4aSRandall Stewart 				ret_code = 1;
4299f8829a4aSRandall Stewart 				goto strres_nochunk;
4300f8829a4aSRandall Stewart 			}
4301f8829a4aSRandall Stewart 			/* no more */
4302f8829a4aSRandall Stewart 			break;
4303f8829a4aSRandall Stewart 		} else if (ptype == SCTP_STR_RESET_RESPONSE) {
4304f8829a4aSRandall Stewart 			struct sctp_stream_reset_response *resp;
4305f8829a4aSRandall Stewart 			uint32_t result;
4306f8829a4aSRandall Stewart 
43076a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_response)) {
43086a58f0e9SXin LI 				break;
43096a58f0e9SXin LI 			}
4310f8829a4aSRandall Stewart 			resp = (struct sctp_stream_reset_response *)ph;
4311f8829a4aSRandall Stewart 			seq = ntohl(resp->response_seq);
4312f8829a4aSRandall Stewart 			result = ntohl(resp->result);
4313f8829a4aSRandall Stewart 			if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4314f8829a4aSRandall Stewart 				ret_code = 1;
4315f8829a4aSRandall Stewart 				goto strres_nochunk;
4316f8829a4aSRandall Stewart 			}
4317f8829a4aSRandall Stewart 		} else {
4318f8829a4aSRandall Stewart 			break;
4319f8829a4aSRandall Stewart 		}
4320671d309cSRandall Stewart 		offset += SCTP_SIZE32(param_len);
43216a58f0e9SXin LI 		if (remaining_length >= SCTP_SIZE32(param_len)) {
43226a58f0e9SXin LI 			remaining_length -= SCTP_SIZE32(param_len);
43236a58f0e9SXin LI 		} else {
43246a58f0e9SXin LI 			remaining_length = 0;
43256a58f0e9SXin LI 		}
4326f8829a4aSRandall Stewart 	}
4327f8829a4aSRandall Stewart 	if (num_req == 0) {
4328f8829a4aSRandall Stewart 		/* we have no response free the stuff */
4329f8829a4aSRandall Stewart 		goto strres_nochunk;
4330f8829a4aSRandall Stewart 	}
4331f8829a4aSRandall Stewart 	/* ok we have a chunk to link in */
4332f8829a4aSRandall Stewart 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4333f8829a4aSRandall Stewart 	    chk,
4334f8829a4aSRandall Stewart 	    sctp_next);
4335f8829a4aSRandall Stewart 	stcb->asoc.ctrl_queue_cnt++;
4336f8829a4aSRandall Stewart 	return (ret_code);
4337f8829a4aSRandall Stewart }
4338f8829a4aSRandall Stewart 
4339f8829a4aSRandall Stewart /*
4340f8829a4aSRandall Stewart  * Handle a router or endpoints report of a packet loss, there are two ways
4341f8829a4aSRandall Stewart  * to handle this, either we get the whole packet and must disect it
4342f8829a4aSRandall Stewart  * ourselves (possibly with truncation and or corruption) or it is a summary
4343f8829a4aSRandall Stewart  * from a middle box that did the disectting for us.
4344f8829a4aSRandall Stewart  */
4345f8829a4aSRandall Stewart static void
4346f8829a4aSRandall Stewart sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4347458303daSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4348f8829a4aSRandall Stewart {
4349f8829a4aSRandall Stewart 	uint32_t bottle_bw, on_queue;
4350f8829a4aSRandall Stewart 	uint16_t trunc_len;
4351f8829a4aSRandall Stewart 	unsigned int chlen;
4352f8829a4aSRandall Stewart 	unsigned int at;
4353f8829a4aSRandall Stewart 	struct sctp_chunk_desc desc;
4354f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
4355f8829a4aSRandall Stewart 
4356f8829a4aSRandall Stewart 	chlen = ntohs(cp->ch.chunk_length);
4357f8829a4aSRandall Stewart 	chlen -= sizeof(struct sctp_pktdrop_chunk);
4358f8829a4aSRandall Stewart 	/* XXX possible chlen underflow */
4359f8829a4aSRandall Stewart 	if (chlen == 0) {
4360f8829a4aSRandall Stewart 		ch = NULL;
4361f8829a4aSRandall Stewart 		if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4362f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_pdrpbwrpt);
4363f8829a4aSRandall Stewart 	} else {
4364f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4365f8829a4aSRandall Stewart 		chlen -= sizeof(struct sctphdr);
4366f8829a4aSRandall Stewart 		/* XXX possible chlen underflow */
4367f8829a4aSRandall Stewart 		memset(&desc, 0, sizeof(desc));
4368f8829a4aSRandall Stewart 	}
4369f8829a4aSRandall Stewart 	trunc_len = (uint16_t) ntohs(cp->trunc_len);
4370458303daSRandall Stewart 	if (trunc_len > limit) {
4371458303daSRandall Stewart 		trunc_len = limit;
4372458303daSRandall Stewart 	}
4373f8829a4aSRandall Stewart 	/* now the chunks themselves */
4374f8829a4aSRandall Stewart 	while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4375f8829a4aSRandall Stewart 		desc.chunk_type = ch->chunk_type;
4376f8829a4aSRandall Stewart 		/* get amount we need to move */
4377f8829a4aSRandall Stewart 		at = ntohs(ch->chunk_length);
4378f8829a4aSRandall Stewart 		if (at < sizeof(struct sctp_chunkhdr)) {
4379f8829a4aSRandall Stewart 			/* corrupt chunk, maybe at the end? */
4380f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_pdrpcrupt);
4381f8829a4aSRandall Stewart 			break;
4382f8829a4aSRandall Stewart 		}
4383f8829a4aSRandall Stewart 		if (trunc_len == 0) {
4384f8829a4aSRandall Stewart 			/* we are supposed to have all of it */
4385f8829a4aSRandall Stewart 			if (at > chlen) {
4386f8829a4aSRandall Stewart 				/* corrupt skip it */
4387f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpcrupt);
4388f8829a4aSRandall Stewart 				break;
4389f8829a4aSRandall Stewart 			}
4390f8829a4aSRandall Stewart 		} else {
4391f8829a4aSRandall Stewart 			/* is there enough of it left ? */
4392f8829a4aSRandall Stewart 			if (desc.chunk_type == SCTP_DATA) {
4393f8829a4aSRandall Stewart 				if (chlen < (sizeof(struct sctp_data_chunk) +
4394f8829a4aSRandall Stewart 				    sizeof(desc.data_bytes))) {
4395f8829a4aSRandall Stewart 					break;
4396f8829a4aSRandall Stewart 				}
4397f8829a4aSRandall Stewart 			} else {
4398f8829a4aSRandall Stewart 				if (chlen < sizeof(struct sctp_chunkhdr)) {
4399f8829a4aSRandall Stewart 					break;
4400f8829a4aSRandall Stewart 				}
4401f8829a4aSRandall Stewart 			}
4402f8829a4aSRandall Stewart 		}
4403f8829a4aSRandall Stewart 		if (desc.chunk_type == SCTP_DATA) {
4404f8829a4aSRandall Stewart 			/* can we get out the tsn? */
4405f8829a4aSRandall Stewart 			if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4406f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpmbda);
4407f8829a4aSRandall Stewart 
4408f8829a4aSRandall Stewart 			if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4409f8829a4aSRandall Stewart 				/* yep */
4410f8829a4aSRandall Stewart 				struct sctp_data_chunk *dcp;
4411f8829a4aSRandall Stewart 				uint8_t *ddp;
4412f8829a4aSRandall Stewart 				unsigned int iii;
4413f8829a4aSRandall Stewart 
4414f8829a4aSRandall Stewart 				dcp = (struct sctp_data_chunk *)ch;
4415f8829a4aSRandall Stewart 				ddp = (uint8_t *) (dcp + 1);
4416f8829a4aSRandall Stewart 				for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4417f8829a4aSRandall Stewart 					desc.data_bytes[iii] = ddp[iii];
4418f8829a4aSRandall Stewart 				}
4419f8829a4aSRandall Stewart 				desc.tsn_ifany = dcp->dp.tsn;
4420f8829a4aSRandall Stewart 			} else {
4421f8829a4aSRandall Stewart 				/* nope we are done. */
4422f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpnedat);
4423f8829a4aSRandall Stewart 				break;
4424f8829a4aSRandall Stewart 			}
4425f8829a4aSRandall Stewart 		} else {
4426f8829a4aSRandall Stewart 			if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4427f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpmbct);
4428f8829a4aSRandall Stewart 		}
4429f8829a4aSRandall Stewart 
4430f8829a4aSRandall Stewart 		if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4431f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_pdrppdbrk);
4432f8829a4aSRandall Stewart 			break;
4433f8829a4aSRandall Stewart 		}
4434f8829a4aSRandall Stewart 		if (SCTP_SIZE32(at) > chlen) {
4435f8829a4aSRandall Stewart 			break;
4436f8829a4aSRandall Stewart 		}
4437f8829a4aSRandall Stewart 		chlen -= SCTP_SIZE32(at);
4438f8829a4aSRandall Stewart 		if (chlen < sizeof(struct sctp_chunkhdr)) {
4439f8829a4aSRandall Stewart 			/* done, none left */
4440f8829a4aSRandall Stewart 			break;
4441f8829a4aSRandall Stewart 		}
4442f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4443f8829a4aSRandall Stewart 	}
4444f8829a4aSRandall Stewart 	/* Now update any rwnd --- possibly */
4445f8829a4aSRandall Stewart 	if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4446f8829a4aSRandall Stewart 		/* From a peer, we get a rwnd report */
4447f8829a4aSRandall Stewart 		uint32_t a_rwnd;
4448f8829a4aSRandall Stewart 
4449f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_pdrpfehos);
4450f8829a4aSRandall Stewart 
4451f8829a4aSRandall Stewart 		bottle_bw = ntohl(cp->bottle_bw);
4452f8829a4aSRandall Stewart 		on_queue = ntohl(cp->current_onq);
4453f8829a4aSRandall Stewart 		if (bottle_bw && on_queue) {
4454f8829a4aSRandall Stewart 			/* a rwnd report is in here */
4455f8829a4aSRandall Stewart 			if (bottle_bw > on_queue)
4456f8829a4aSRandall Stewart 				a_rwnd = bottle_bw - on_queue;
4457f8829a4aSRandall Stewart 			else
4458f8829a4aSRandall Stewart 				a_rwnd = 0;
4459f8829a4aSRandall Stewart 
4460f8829a4aSRandall Stewart 			if (a_rwnd == 0)
4461f8829a4aSRandall Stewart 				stcb->asoc.peers_rwnd = 0;
4462f8829a4aSRandall Stewart 			else {
4463f8829a4aSRandall Stewart 				if (a_rwnd > stcb->asoc.total_flight) {
4464f8829a4aSRandall Stewart 					stcb->asoc.peers_rwnd =
4465f8829a4aSRandall Stewart 					    a_rwnd - stcb->asoc.total_flight;
4466f8829a4aSRandall Stewart 				} else {
4467f8829a4aSRandall Stewart 					stcb->asoc.peers_rwnd = 0;
4468f8829a4aSRandall Stewart 				}
4469f8829a4aSRandall Stewart 				if (stcb->asoc.peers_rwnd <
4470f8829a4aSRandall Stewart 				    stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4471f8829a4aSRandall Stewart 					/* SWS sender side engages */
4472f8829a4aSRandall Stewart 					stcb->asoc.peers_rwnd = 0;
4473f8829a4aSRandall Stewart 				}
4474f8829a4aSRandall Stewart 			}
4475f8829a4aSRandall Stewart 		}
4476f8829a4aSRandall Stewart 	} else {
4477f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_pdrpfmbox);
4478f8829a4aSRandall Stewart 	}
4479f8829a4aSRandall Stewart 
4480f8829a4aSRandall Stewart 	/* now middle boxes in sat networks get a cwnd bump */
4481f8829a4aSRandall Stewart 	if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4482f8829a4aSRandall Stewart 	    (stcb->asoc.sat_t3_loss_recovery == 0) &&
4483f8829a4aSRandall Stewart 	    (stcb->asoc.sat_network)) {
4484f8829a4aSRandall Stewart 		/*
4485f8829a4aSRandall Stewart 		 * This is debateable but for sat networks it makes sense
4486f8829a4aSRandall Stewart 		 * Note if a T3 timer has went off, we will prohibit any
4487f8829a4aSRandall Stewart 		 * changes to cwnd until we exit the t3 loss recovery.
4488f8829a4aSRandall Stewart 		 */
4489b54d3a6cSRandall Stewart 		stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4490b54d3a6cSRandall Stewart 		    net, cp, &bottle_bw, &on_queue);
4491f8829a4aSRandall Stewart 	}
4492f8829a4aSRandall Stewart }
4493f8829a4aSRandall Stewart 
4494f8829a4aSRandall Stewart /*
4495f8829a4aSRandall Stewart  * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4496f8829a4aSRandall Stewart  * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4497f8829a4aSRandall Stewart  * offset: offset into the mbuf chain to first chunkhdr - length: is the
4498f8829a4aSRandall Stewart  * length of the complete packet outputs: - length: modified to remaining
4499f8829a4aSRandall Stewart  * length after control processing - netp: modified to new sctp_nets after
4500f8829a4aSRandall Stewart  * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4501f8829a4aSRandall Stewart  * bad packet,...) otherwise return the tcb for this packet
4502f8829a4aSRandall Stewart  */
45033c6f3536SRandall Stewart #ifdef __GNUC__
45043c6f3536SRandall Stewart __attribute__((noinline))
45053c6f3536SRandall Stewart #endif
4506f8829a4aSRandall Stewart 	static struct sctp_tcb *
4507f8829a4aSRandall Stewart 	         sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4508b1754ad1SMichael Tuexen              struct sockaddr *src, struct sockaddr *dst,
4509f8829a4aSRandall Stewart              struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
451017205eccSRandall Stewart              struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4511d089f9b9SMichael Tuexen              uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4512c54a18d2SRandall Stewart              uint32_t vrf_id, uint16_t port)
4513f8829a4aSRandall Stewart {
4514f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4515ff1ffd74SMichael Tuexen 	struct mbuf *op_err;
4516ff1ffd74SMichael Tuexen 	char msg[SCTP_DIAG_INFO_LEN];
4517f8829a4aSRandall Stewart 	uint32_t vtag_in;
4518f8829a4aSRandall Stewart 	int num_chunks = 0;	/* number of control chunks processed */
45194c9179adSRandall Stewart 	uint32_t chk_length;
4520f8829a4aSRandall Stewart 	int ret;
4521bff64a4dSRandall Stewart 	int abort_no_unlock = 0;
4522899288aeSRandall Stewart 	int ecne_seen = 0;
4523f8829a4aSRandall Stewart 
4524f8829a4aSRandall Stewart 	/*
4525f8829a4aSRandall Stewart 	 * How big should this be, and should it be alloc'd? Lets try the
4526f8829a4aSRandall Stewart 	 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4527f8829a4aSRandall Stewart 	 * until we get into jumbo grams and such..
4528f8829a4aSRandall Stewart 	 */
4529f42a358aSRandall Stewart 	uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4530f8829a4aSRandall Stewart 	struct sctp_tcb *locked_tcb = stcb;
4531f8829a4aSRandall Stewart 	int got_auth = 0;
4532f8829a4aSRandall Stewart 	uint32_t auth_offset = 0, auth_len = 0;
4533f8829a4aSRandall Stewart 	int auth_skipped = 0;
45342afb3e84SRandall Stewart 	int asconf_cnt = 0;
4535f8829a4aSRandall Stewart 
4536ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4537ceaad40aSRandall Stewart 	struct socket *so;
4538ceaad40aSRandall Stewart 
4539ceaad40aSRandall Stewart #endif
4540ceaad40aSRandall Stewart 
4541ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4542dd294dceSMichael Tuexen 	    iphlen, *offset, length, (void *)stcb);
4543f8829a4aSRandall Stewart 
4544f8829a4aSRandall Stewart 	/* validate chunk header length... */
4545f8829a4aSRandall Stewart 	if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4546d61a0ae0SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4547d61a0ae0SRandall Stewart 		    ntohs(ch->chunk_length));
4548207304d4SRandall Stewart 		if (locked_tcb) {
4549207304d4SRandall Stewart 			SCTP_TCB_UNLOCK(locked_tcb);
4550207304d4SRandall Stewart 		}
4551f8829a4aSRandall Stewart 		return (NULL);
4552f8829a4aSRandall Stewart 	}
4553f8829a4aSRandall Stewart 	/*
4554f8829a4aSRandall Stewart 	 * validate the verification tag
4555f8829a4aSRandall Stewart 	 */
4556f8829a4aSRandall Stewart 	vtag_in = ntohl(sh->v_tag);
4557f8829a4aSRandall Stewart 
4558a5d547adSRandall Stewart 	if (locked_tcb) {
4559a5d547adSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(locked_tcb);
4560a5d547adSRandall Stewart 	}
4561f8829a4aSRandall Stewart 	if (ch->chunk_type == SCTP_INITIATION) {
4562d61a0ae0SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4563d61a0ae0SRandall Stewart 		    ntohs(ch->chunk_length), vtag_in);
4564f8829a4aSRandall Stewart 		if (vtag_in != 0) {
4565f8829a4aSRandall Stewart 			/* protocol error- silently discard... */
4566f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_badvtag);
45676e55db54SRandall Stewart 			if (locked_tcb) {
4568f8829a4aSRandall Stewart 				SCTP_TCB_UNLOCK(locked_tcb);
45696e55db54SRandall Stewart 			}
4570f8829a4aSRandall Stewart 			return (NULL);
4571f8829a4aSRandall Stewart 		}
4572f8829a4aSRandall Stewart 	} else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4573f8829a4aSRandall Stewart 		/*
4574f8829a4aSRandall Stewart 		 * If there is no stcb, skip the AUTH chunk and process
4575f8829a4aSRandall Stewart 		 * later after a stcb is found (to validate the lookup was
4576f8829a4aSRandall Stewart 		 * valid.
4577f8829a4aSRandall Stewart 		 */
4578f8829a4aSRandall Stewart 		if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4579b3f1ea41SRandall Stewart 		    (stcb == NULL) &&
4580c79bec9cSMichael Tuexen 		    (inp->auth_supported == 1)) {
4581f8829a4aSRandall Stewart 			/* save this chunk for later processing */
4582f8829a4aSRandall Stewart 			auth_skipped = 1;
4583f8829a4aSRandall Stewart 			auth_offset = *offset;
4584f8829a4aSRandall Stewart 			auth_len = ntohs(ch->chunk_length);
4585f8829a4aSRandall Stewart 
4586f8829a4aSRandall Stewart 			/* (temporarily) move past this chunk */
4587f8829a4aSRandall Stewart 			*offset += SCTP_SIZE32(auth_len);
4588f8829a4aSRandall Stewart 			if (*offset >= length) {
4589f8829a4aSRandall Stewart 				/* no more data left in the mbuf chain */
4590f8829a4aSRandall Stewart 				*offset = length;
4591207304d4SRandall Stewart 				if (locked_tcb) {
4592207304d4SRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
4593207304d4SRandall Stewart 				}
4594f8829a4aSRandall Stewart 				return (NULL);
4595f8829a4aSRandall Stewart 			}
4596f8829a4aSRandall Stewart 			ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4597f8829a4aSRandall Stewart 			    sizeof(struct sctp_chunkhdr), chunk_buf);
4598f8829a4aSRandall Stewart 		}
4599ad81507eSRandall Stewart 		if (ch == NULL) {
4600ad81507eSRandall Stewart 			/* Help */
4601ad81507eSRandall Stewart 			*offset = length;
4602207304d4SRandall Stewart 			if (locked_tcb) {
4603207304d4SRandall Stewart 				SCTP_TCB_UNLOCK(locked_tcb);
4604207304d4SRandall Stewart 			}
4605ad81507eSRandall Stewart 			return (NULL);
4606ad81507eSRandall Stewart 		}
4607f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4608f8829a4aSRandall Stewart 			goto process_control_chunks;
4609f8829a4aSRandall Stewart 		}
4610f8829a4aSRandall Stewart 		/*
4611f8829a4aSRandall Stewart 		 * first check if it's an ASCONF with an unknown src addr we
4612f8829a4aSRandall Stewart 		 * need to look inside to find the association
4613f8829a4aSRandall Stewart 		 */
4614f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
46152afb3e84SRandall Stewart 			struct sctp_chunkhdr *asconf_ch = ch;
46162afb3e84SRandall Stewart 			uint32_t asconf_offset = 0, asconf_len = 0;
46172afb3e84SRandall Stewart 
4618f8829a4aSRandall Stewart 			/* inp's refcount may be reduced */
4619f8829a4aSRandall Stewart 			SCTP_INP_INCR_REF(inp);
4620f8829a4aSRandall Stewart 
46212afb3e84SRandall Stewart 			asconf_offset = *offset;
46222afb3e84SRandall Stewart 			do {
46232afb3e84SRandall Stewart 				asconf_len = ntohs(asconf_ch->chunk_length);
46242afb3e84SRandall Stewart 				if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
46252afb3e84SRandall Stewart 					break;
46267215cc1bSMichael Tuexen 				stcb = sctp_findassociation_ep_asconf(m,
4627b1754ad1SMichael Tuexen 				    *offset,
4628b1754ad1SMichael Tuexen 				    dst,
4629b1754ad1SMichael Tuexen 				    sh, &inp, netp, vrf_id);
46302afb3e84SRandall Stewart 				if (stcb != NULL)
46312afb3e84SRandall Stewart 					break;
46322afb3e84SRandall Stewart 				asconf_offset += SCTP_SIZE32(asconf_len);
46332afb3e84SRandall Stewart 				asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
46342afb3e84SRandall Stewart 				    sizeof(struct sctp_chunkhdr), chunk_buf);
46352afb3e84SRandall Stewart 			} while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4636f8829a4aSRandall Stewart 			if (stcb == NULL) {
4637f8829a4aSRandall Stewart 				/*
4638f8829a4aSRandall Stewart 				 * reduce inp's refcount if not reduced in
4639f8829a4aSRandall Stewart 				 * sctp_findassociation_ep_asconf().
4640f8829a4aSRandall Stewart 				 */
4641f8829a4aSRandall Stewart 				SCTP_INP_DECR_REF(inp);
46422afb3e84SRandall Stewart 			} else {
46432afb3e84SRandall Stewart 				locked_tcb = stcb;
4644f8829a4aSRandall Stewart 			}
46452afb3e84SRandall Stewart 
4646f8829a4aSRandall Stewart 			/* now go back and verify any auth chunk to be sure */
4647f8829a4aSRandall Stewart 			if (auth_skipped && (stcb != NULL)) {
4648f8829a4aSRandall Stewart 				struct sctp_auth_chunk *auth;
4649f8829a4aSRandall Stewart 
4650f8829a4aSRandall Stewart 				auth = (struct sctp_auth_chunk *)
4651f8829a4aSRandall Stewart 				    sctp_m_getptr(m, auth_offset,
4652f8829a4aSRandall Stewart 				    auth_len, chunk_buf);
4653f8829a4aSRandall Stewart 				got_auth = 1;
4654f8829a4aSRandall Stewart 				auth_skipped = 0;
4655ad81507eSRandall Stewart 				if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4656f8829a4aSRandall Stewart 				    auth_offset)) {
4657f8829a4aSRandall Stewart 					/* auth HMAC failed so dump it */
4658f8829a4aSRandall Stewart 					*offset = length;
4659207304d4SRandall Stewart 					if (locked_tcb) {
4660207304d4SRandall Stewart 						SCTP_TCB_UNLOCK(locked_tcb);
4661207304d4SRandall Stewart 					}
4662f8829a4aSRandall Stewart 					return (NULL);
4663f8829a4aSRandall Stewart 				} else {
4664f8829a4aSRandall Stewart 					/* remaining chunks are HMAC checked */
4665f8829a4aSRandall Stewart 					stcb->asoc.authenticated = 1;
4666f8829a4aSRandall Stewart 				}
4667f8829a4aSRandall Stewart 			}
4668f8829a4aSRandall Stewart 		}
4669f8829a4aSRandall Stewart 		if (stcb == NULL) {
46706e9c45e0SMichael Tuexen 			snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4671ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4672ff1ffd74SMichael Tuexen 			    msg);
4673f8829a4aSRandall Stewart 			/* no association, so it's out of the blue... */
4674ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp, op_err,
4675d089f9b9SMichael Tuexen 			    mflowtype, mflowid, inp->fibnum,
4676c54a18d2SRandall Stewart 			    vrf_id, port);
4677f8829a4aSRandall Stewart 			*offset = length;
46786e55db54SRandall Stewart 			if (locked_tcb) {
4679f8829a4aSRandall Stewart 				SCTP_TCB_UNLOCK(locked_tcb);
46806e55db54SRandall Stewart 			}
4681f8829a4aSRandall Stewart 			return (NULL);
4682f8829a4aSRandall Stewart 		}
4683f8829a4aSRandall Stewart 		asoc = &stcb->asoc;
4684f8829a4aSRandall Stewart 		/* ABORT and SHUTDOWN can use either v_tag... */
4685f8829a4aSRandall Stewart 		if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4686f8829a4aSRandall Stewart 		    (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4687f8829a4aSRandall Stewart 		    (ch->chunk_type == SCTP_PACKET_DROPPED)) {
46885db47b3dSMichael Tuexen 			/* Take the T-bit always into account. */
46895db47b3dSMichael Tuexen 			if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) &&
46905db47b3dSMichael Tuexen 			    (vtag_in == asoc->my_vtag)) ||
46915db47b3dSMichael Tuexen 			    (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) &&
4692f8829a4aSRandall Stewart 			    (vtag_in == asoc->peer_vtag))) {
4693f8829a4aSRandall Stewart 				/* this is valid */
4694f8829a4aSRandall Stewart 			} else {
4695f8829a4aSRandall Stewart 				/* drop this packet... */
4696f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_badvtag);
46976e55db54SRandall Stewart 				if (locked_tcb) {
4698f8829a4aSRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
46996e55db54SRandall Stewart 				}
4700f8829a4aSRandall Stewart 				return (NULL);
4701f8829a4aSRandall Stewart 			}
4702f8829a4aSRandall Stewart 		} else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4703f8829a4aSRandall Stewart 			if (vtag_in != asoc->my_vtag) {
4704f8829a4aSRandall Stewart 				/*
4705f8829a4aSRandall Stewart 				 * this could be a stale SHUTDOWN-ACK or the
4706f8829a4aSRandall Stewart 				 * peer never got the SHUTDOWN-COMPLETE and
4707f8829a4aSRandall Stewart 				 * is still hung; we have started a new asoc
4708f8829a4aSRandall Stewart 				 * but it won't complete until the shutdown
4709f8829a4aSRandall Stewart 				 * is completed
4710f8829a4aSRandall Stewart 				 */
47116e55db54SRandall Stewart 				if (locked_tcb) {
4712f8829a4aSRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
47136e55db54SRandall Stewart 				}
47146e9c45e0SMichael Tuexen 				snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4715ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4716ff1ffd74SMichael Tuexen 				    msg);
4717b1754ad1SMichael Tuexen 				sctp_handle_ootb(m, iphlen, *offset, src, dst,
4718ff1ffd74SMichael Tuexen 				    sh, inp, op_err,
4719d089f9b9SMichael Tuexen 				    mflowtype, mflowid, fibnum,
4720021416fbSMichael Tuexen 				    vrf_id, port);
4721f8829a4aSRandall Stewart 				return (NULL);
4722f8829a4aSRandall Stewart 			}
4723f8829a4aSRandall Stewart 		} else {
4724f8829a4aSRandall Stewart 			/* for all other chunks, vtag must match */
4725f8829a4aSRandall Stewart 			if (vtag_in != asoc->my_vtag) {
4726f8829a4aSRandall Stewart 				/* invalid vtag... */
4727ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INPUT3,
4728ad81507eSRandall Stewart 				    "invalid vtag: %xh, expect %xh\n",
4729ad81507eSRandall Stewart 				    vtag_in, asoc->my_vtag);
4730f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_badvtag);
47316e55db54SRandall Stewart 				if (locked_tcb) {
4732f8829a4aSRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
47336e55db54SRandall Stewart 				}
4734f8829a4aSRandall Stewart 				*offset = length;
4735f8829a4aSRandall Stewart 				return (NULL);
4736f8829a4aSRandall Stewart 			}
4737f8829a4aSRandall Stewart 		}
4738f8829a4aSRandall Stewart 	}			/* end if !SCTP_COOKIE_ECHO */
4739f8829a4aSRandall Stewart 	/*
4740f8829a4aSRandall Stewart 	 * process all control chunks...
4741f8829a4aSRandall Stewart 	 */
4742f8829a4aSRandall Stewart 	if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4743830d754dSRandall Stewart 	    (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4744f8829a4aSRandall Stewart 	    (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4745f8829a4aSRandall Stewart 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
4746f8829a4aSRandall Stewart 		/* implied cookie-ack.. we must have lost the ack */
4747b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4748c4739e2fSRandall Stewart 			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4749c4739e2fSRandall Stewart 			    stcb->asoc.overall_error_count,
4750c4739e2fSRandall Stewart 			    0,
4751c4739e2fSRandall Stewart 			    SCTP_FROM_SCTP_INPUT,
4752c4739e2fSRandall Stewart 			    __LINE__);
4753c4739e2fSRandall Stewart 		}
4754f8829a4aSRandall Stewart 		stcb->asoc.overall_error_count = 0;
4755f8829a4aSRandall Stewart 		sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4756f8829a4aSRandall Stewart 		    *netp);
4757f8829a4aSRandall Stewart 	}
4758f8829a4aSRandall Stewart process_control_chunks:
4759f8829a4aSRandall Stewart 	while (IS_SCTP_CONTROL(ch)) {
4760f8829a4aSRandall Stewart 		/* validate chunk length */
4761f8829a4aSRandall Stewart 		chk_length = ntohs(ch->chunk_length);
4762ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4763f8829a4aSRandall Stewart 		    ch->chunk_type, chk_length);
476480fefe0aSRandall Stewart 		SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
47654c9179adSRandall Stewart 		if (chk_length < sizeof(*ch) ||
47664c9179adSRandall Stewart 		    (*offset + (int)chk_length) > length) {
4767f8829a4aSRandall Stewart 			*offset = length;
47686e55db54SRandall Stewart 			if (locked_tcb) {
4769f8829a4aSRandall Stewart 				SCTP_TCB_UNLOCK(locked_tcb);
47706e55db54SRandall Stewart 			}
4771f8829a4aSRandall Stewart 			return (NULL);
4772f8829a4aSRandall Stewart 		}
4773f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4774f8829a4aSRandall Stewart 		/*
4775f8829a4aSRandall Stewart 		 * INIT-ACK only gets the init ack "header" portion only
4776f8829a4aSRandall Stewart 		 * because we don't have to process the peer's COOKIE. All
4777f8829a4aSRandall Stewart 		 * others get a complete chunk.
4778f8829a4aSRandall Stewart 		 */
4779d06c82f1SRandall Stewart 		if ((ch->chunk_type == SCTP_INITIATION_ACK) ||
4780d06c82f1SRandall Stewart 		    (ch->chunk_type == SCTP_INITIATION)) {
4781f8829a4aSRandall Stewart 			/* get an init-ack chunk */
4782f8829a4aSRandall Stewart 			ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4783f8829a4aSRandall Stewart 			    sizeof(struct sctp_init_ack_chunk), chunk_buf);
4784f8829a4aSRandall Stewart 			if (ch == NULL) {
4785f8829a4aSRandall Stewart 				*offset = length;
47866e55db54SRandall Stewart 				if (locked_tcb) {
4787f8829a4aSRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
47886e55db54SRandall Stewart 				}
4789f8829a4aSRandall Stewart 				return (NULL);
4790f8829a4aSRandall Stewart 			}
47919a972525SRandall Stewart 		} else {
4792e1461651SRandall Stewart 			/* For cookies and all other chunks. */
4793d06c82f1SRandall Stewart 			if (chk_length > sizeof(chunk_buf)) {
4794d06c82f1SRandall Stewart 				/*
4795d06c82f1SRandall Stewart 				 * use just the size of the chunk buffer so
47969a972525SRandall Stewart 				 * the front part of our chunks fit in
47979a972525SRandall Stewart 				 * contiguous space up to the chunk buffer
47989a972525SRandall Stewart 				 * size (508 bytes). For chunks that need to
4799e1461651SRandall Stewart 				 * get more than that they must use the
48009a972525SRandall Stewart 				 * sctp_m_getptr() function or other means
4801e1461651SRandall Stewart 				 * (e.g. know how to parse mbuf chains).
4802e1461651SRandall Stewart 				 * Cookies do this already.
4803d06c82f1SRandall Stewart 				 */
4804d06c82f1SRandall Stewart 				ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4805d06c82f1SRandall Stewart 				    (sizeof(chunk_buf) - 4),
4806d06c82f1SRandall Stewart 				    chunk_buf);
4807d06c82f1SRandall Stewart 				if (ch == NULL) {
4808d06c82f1SRandall Stewart 					*offset = length;
48096e55db54SRandall Stewart 					if (locked_tcb) {
4810d06c82f1SRandall Stewart 						SCTP_TCB_UNLOCK(locked_tcb);
48116e55db54SRandall Stewart 					}
4812d06c82f1SRandall Stewart 					return (NULL);
4813d06c82f1SRandall Stewart 				}
4814d06c82f1SRandall Stewart 			} else {
4815d06c82f1SRandall Stewart 				/* We can fit it all */
4816f8829a4aSRandall Stewart 				ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4817f8829a4aSRandall Stewart 				    chk_length, chunk_buf);
4818f8829a4aSRandall Stewart 				if (ch == NULL) {
4819ad81507eSRandall Stewart 					SCTP_PRINTF("sctp_process_control: Can't get the all data....\n");
4820f8829a4aSRandall Stewart 					*offset = length;
48216e55db54SRandall Stewart 					if (locked_tcb) {
4822f8829a4aSRandall Stewart 						SCTP_TCB_UNLOCK(locked_tcb);
48236e55db54SRandall Stewart 					}
4824f8829a4aSRandall Stewart 					return (NULL);
4825f8829a4aSRandall Stewart 				}
4826f8829a4aSRandall Stewart 			}
48279a972525SRandall Stewart 		}
4828f8829a4aSRandall Stewart 		num_chunks++;
4829f8829a4aSRandall Stewart 		/* Save off the last place we got a control from */
4830f8829a4aSRandall Stewart 		if (stcb != NULL) {
4831ad81507eSRandall Stewart 			if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4832f8829a4aSRandall Stewart 				/*
4833f8829a4aSRandall Stewart 				 * allow last_control to be NULL if
4834f8829a4aSRandall Stewart 				 * ASCONF... ASCONF processing will find the
4835f8829a4aSRandall Stewart 				 * right net later
4836f8829a4aSRandall Stewart 				 */
4837ad81507eSRandall Stewart 				if ((netp != NULL) && (*netp != NULL))
4838f8829a4aSRandall Stewart 					stcb->asoc.last_control_chunk_from = *netp;
4839f8829a4aSRandall Stewart 			}
4840f8829a4aSRandall Stewart 		}
4841f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
4842f8829a4aSRandall Stewart 		sctp_audit_log(0xB0, ch->chunk_type);
4843f8829a4aSRandall Stewart #endif
4844f8829a4aSRandall Stewart 
4845f8829a4aSRandall Stewart 		/* check to see if this chunk required auth, but isn't */
4846b3f1ea41SRandall Stewart 		if ((stcb != NULL) &&
4847c79bec9cSMichael Tuexen 		    (stcb->asoc.auth_supported == 1) &&
4848b3f1ea41SRandall Stewart 		    sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4849f8829a4aSRandall Stewart 		    !stcb->asoc.authenticated) {
4850f8829a4aSRandall Stewart 			/* "silently" ignore */
4851f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvauthmissing);
4852f8829a4aSRandall Stewart 			goto next_chunk;
4853f8829a4aSRandall Stewart 		}
4854f8829a4aSRandall Stewart 		switch (ch->chunk_type) {
4855f8829a4aSRandall Stewart 		case SCTP_INITIATION:
4856ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4857d68fdc4dSMichael Tuexen 			/* The INIT chunk must be the only chunk. */
4858d68fdc4dSMichael Tuexen 			if ((num_chunks > 1) ||
4859ab6174d5SMichael Tuexen 			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
48600941640fSMichael Tuexen 				/* RFC 4960 requires that no ABORT is sent */
4861f8829a4aSRandall Stewart 				*offset = length;
48620941640fSMichael Tuexen 				if (locked_tcb) {
48630941640fSMichael Tuexen 					SCTP_TCB_UNLOCK(locked_tcb);
48640941640fSMichael Tuexen 				}
4865f8829a4aSRandall Stewart 				return (NULL);
4866f8829a4aSRandall Stewart 			}
4867d68fdc4dSMichael Tuexen 			/* Honor our resource limit. */
4868d68fdc4dSMichael Tuexen 			if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4869ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4870b1754ad1SMichael Tuexen 				sctp_abort_association(inp, stcb, m, iphlen,
4871b1754ad1SMichael Tuexen 				    src, dst, sh, op_err,
4872457b4b88SMichael Tuexen 				    mflowtype, mflowid,
4873f30ac432SMichael Tuexen 				    vrf_id, port);
4874f8829a4aSRandall Stewart 				*offset = length;
4875f8829a4aSRandall Stewart 				return (NULL);
4876f8829a4aSRandall Stewart 			}
4877b1754ad1SMichael Tuexen 			sctp_handle_init(m, iphlen, *offset, src, dst, sh,
4878ad81507eSRandall Stewart 			    (struct sctp_init_chunk *)ch, inp,
4879ca83f93cSMichael Tuexen 			    stcb, *netp, &abort_no_unlock,
4880457b4b88SMichael Tuexen 			    mflowtype, mflowid,
4881f30ac432SMichael Tuexen 			    vrf_id, port);
4882f8829a4aSRandall Stewart 			*offset = length;
4883d68fdc4dSMichael Tuexen 			if ((!abort_no_unlock) && (locked_tcb)) {
4884f8829a4aSRandall Stewart 				SCTP_TCB_UNLOCK(locked_tcb);
48856e55db54SRandall Stewart 			}
4886f8829a4aSRandall Stewart 			return (NULL);
4887f8829a4aSRandall Stewart 			break;
48889a972525SRandall Stewart 		case SCTP_PAD_CHUNK:
48899a972525SRandall Stewart 			break;
4890f8829a4aSRandall Stewart 		case SCTP_INITIATION_ACK:
4891ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n");
4892f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4893f8829a4aSRandall Stewart 				/* We are not interested anymore */
4894f8829a4aSRandall Stewart 				if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4895f8829a4aSRandall Stewart 					;
4896f8829a4aSRandall Stewart 				} else {
4897828318e1SMichael Tuexen 					if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
4898faa1e3f4SRandall Stewart 						/* Very unlikely */
4899f8829a4aSRandall Stewart 						SCTP_TCB_UNLOCK(locked_tcb);
49006e55db54SRandall Stewart 					}
4901f8829a4aSRandall Stewart 					*offset = length;
4902f8829a4aSRandall Stewart 					if (stcb) {
4903ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4904ceaad40aSRandall Stewart 						so = SCTP_INP_SO(inp);
4905ceaad40aSRandall Stewart 						atomic_add_int(&stcb->asoc.refcnt, 1);
4906ceaad40aSRandall Stewart 						SCTP_TCB_UNLOCK(stcb);
4907ceaad40aSRandall Stewart 						SCTP_SOCKET_LOCK(so, 1);
4908ceaad40aSRandall Stewart 						SCTP_TCB_LOCK(stcb);
4909ceaad40aSRandall Stewart 						atomic_subtract_int(&stcb->asoc.refcnt, 1);
4910ceaad40aSRandall Stewart #endif
4911b7d130beSMichael Tuexen 						(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4912b7d130beSMichael Tuexen 						    SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
4913ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4914ceaad40aSRandall Stewart 						SCTP_SOCKET_UNLOCK(so, 1);
4915ceaad40aSRandall Stewart #endif
4916f8829a4aSRandall Stewart 					}
4917f8829a4aSRandall Stewart 					return (NULL);
4918f8829a4aSRandall Stewart 				}
4919f8829a4aSRandall Stewart 			}
4920ab6174d5SMichael Tuexen 			/* The INIT-ACK chunk must be the only chunk. */
4921f8829a4aSRandall Stewart 			if ((num_chunks > 1) ||
4922ab6174d5SMichael Tuexen 			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4923f8829a4aSRandall Stewart 				*offset = length;
49246e55db54SRandall Stewart 				if (locked_tcb) {
4925f8829a4aSRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
49266e55db54SRandall Stewart 				}
4927f8829a4aSRandall Stewart 				return (NULL);
4928f8829a4aSRandall Stewart 			}
4929ad81507eSRandall Stewart 			if ((netp) && (*netp)) {
4930b1754ad1SMichael Tuexen 				ret = sctp_handle_init_ack(m, iphlen, *offset,
4931b1754ad1SMichael Tuexen 				    src, dst, sh,
4932f30ac432SMichael Tuexen 				    (struct sctp_init_ack_chunk *)ch,
4933f30ac432SMichael Tuexen 				    stcb, *netp,
4934f30ac432SMichael Tuexen 				    &abort_no_unlock,
4935457b4b88SMichael Tuexen 				    mflowtype, mflowid,
4936f30ac432SMichael Tuexen 				    vrf_id);
4937ad81507eSRandall Stewart 			} else {
4938ad81507eSRandall Stewart 				ret = -1;
4939ad81507eSRandall Stewart 			}
4940d68fdc4dSMichael Tuexen 			*offset = length;
4941d68fdc4dSMichael Tuexen 			if (abort_no_unlock) {
4942d68fdc4dSMichael Tuexen 				return (NULL);
4943d68fdc4dSMichael Tuexen 			}
4944f8829a4aSRandall Stewart 			/*
4945f8829a4aSRandall Stewart 			 * Special case, I must call the output routine to
4946f8829a4aSRandall Stewart 			 * get the cookie echoed
4947f8829a4aSRandall Stewart 			 */
4948d68fdc4dSMichael Tuexen 			if ((stcb != NULL) && (ret == 0)) {
4949ceaad40aSRandall Stewart 				sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4950d68fdc4dSMichael Tuexen 			}
49516e55db54SRandall Stewart 			if (locked_tcb) {
4952f8829a4aSRandall Stewart 				SCTP_TCB_UNLOCK(locked_tcb);
49536e55db54SRandall Stewart 			}
4954f8829a4aSRandall Stewart 			return (NULL);
4955f8829a4aSRandall Stewart 			break;
4956f8829a4aSRandall Stewart 		case SCTP_SELECTIVE_ACK:
4957f8829a4aSRandall Stewart 			{
4958f8829a4aSRandall Stewart 				struct sctp_sack_chunk *sack;
4959f8829a4aSRandall Stewart 				int abort_now = 0;
4960f8829a4aSRandall Stewart 				uint32_t a_rwnd, cum_ack;
4961cd554309SMichael Tuexen 				uint16_t num_seg, num_dup;
4962cd554309SMichael Tuexen 				uint8_t flags;
4963cd554309SMichael Tuexen 				int offset_seg, offset_dup;
4964f8829a4aSRandall Stewart 
496520083c2eSMichael Tuexen 				SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n");
496620083c2eSMichael Tuexen 				SCTP_STAT_INCR(sctps_recvsacks);
4967cd554309SMichael Tuexen 				if (stcb == NULL) {
4968cd554309SMichael Tuexen 					SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n");
4969cd554309SMichael Tuexen 					break;
49706e55db54SRandall Stewart 				}
4971cd554309SMichael Tuexen 				if (chk_length < sizeof(struct sctp_sack_chunk)) {
4972cd554309SMichael Tuexen 					SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4973cd554309SMichael Tuexen 					break;
4974d06c82f1SRandall Stewart 				}
49752afb3e84SRandall Stewart 				if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
49762afb3e84SRandall Stewart 					/*-
49772afb3e84SRandall Stewart 					 * If we have sent a shutdown-ack, we will pay no
49782afb3e84SRandall Stewart 					 * attention to a sack sent in to us since
49792afb3e84SRandall Stewart 					 * we don't care anymore.
49802afb3e84SRandall Stewart 					 */
4981a1e13272SRandall Stewart 					break;
49822afb3e84SRandall Stewart 				}
4983f8829a4aSRandall Stewart 				sack = (struct sctp_sack_chunk *)ch;
4984cd554309SMichael Tuexen 				flags = ch->chunk_flags;
4985f8829a4aSRandall Stewart 				cum_ack = ntohl(sack->sack.cum_tsn_ack);
4986f8829a4aSRandall Stewart 				num_seg = ntohs(sack->sack.num_gap_ack_blks);
4987cd554309SMichael Tuexen 				num_dup = ntohs(sack->sack.num_dup_tsns);
4988f8829a4aSRandall Stewart 				a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd);
4989cd554309SMichael Tuexen 				if (sizeof(struct sctp_sack_chunk) +
4990cd554309SMichael Tuexen 				    num_seg * sizeof(struct sctp_gap_ack_block) +
4991cd554309SMichael Tuexen 				    num_dup * sizeof(uint32_t) != chk_length) {
4992cd554309SMichael Tuexen 					SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4993cd554309SMichael Tuexen 					break;
4994cd554309SMichael Tuexen 				}
4995cd554309SMichael Tuexen 				offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4996cd554309SMichael Tuexen 				offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
499735918f85SRandall Stewart 				SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4998cd554309SMichael Tuexen 				    cum_ack, num_seg, a_rwnd);
4999f8829a4aSRandall Stewart 				stcb->asoc.seen_a_sack_this_pkt = 1;
5000f8829a4aSRandall Stewart 				if ((stcb->asoc.pr_sctp_cnt == 0) &&
5001f8829a4aSRandall Stewart 				    (num_seg == 0) &&
500220b07a4dSMichael Tuexen 				    SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
5003f8829a4aSRandall Stewart 				    (stcb->asoc.saw_sack_with_frags == 0) &&
5004d9c5cfeaSMichael Tuexen 				    (stcb->asoc.saw_sack_with_nr_frags == 0) &&
5005f8829a4aSRandall Stewart 				    (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
5006f8829a4aSRandall Stewart 				    ) {
5007f8829a4aSRandall Stewart 					/*
5008f8829a4aSRandall Stewart 					 * We have a SIMPLE sack having no
5009f8829a4aSRandall Stewart 					 * prior segments and data on sent
5010f8829a4aSRandall Stewart 					 * queue to be acked.. Use the
5011f8829a4aSRandall Stewart 					 * faster path sack processing. We
5012f8829a4aSRandall Stewart 					 * also allow window update sacks
5013f8829a4aSRandall Stewart 					 * with no missing segments to go
5014f8829a4aSRandall Stewart 					 * this way too.
5015f8829a4aSRandall Stewart 					 */
5016899288aeSRandall Stewart 					sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen);
5017f8829a4aSRandall Stewart 				} else {
5018ad81507eSRandall Stewart 					if (netp && *netp)
50197215cc1bSMichael Tuexen 						sctp_handle_sack(m, offset_seg, offset_dup, stcb,
5020cd554309SMichael Tuexen 						    num_seg, 0, num_dup, &abort_now, flags,
5021899288aeSRandall Stewart 						    cum_ack, a_rwnd, ecne_seen);
5022830d754dSRandall Stewart 				}
5023f8829a4aSRandall Stewart 				if (abort_now) {
5024f8829a4aSRandall Stewart 					/* ABORT signal from sack processing */
5025f8829a4aSRandall Stewart 					*offset = length;
5026f8829a4aSRandall Stewart 					return (NULL);
5027f8829a4aSRandall Stewart 				}
5028cd554309SMichael Tuexen 				if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5029cd554309SMichael Tuexen 				    TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5030cd554309SMichael Tuexen 				    (stcb->asoc.stream_queue_cnt == 0)) {
5031cd554309SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
5032cd554309SMichael Tuexen 				}
5033f8829a4aSRandall Stewart 			}
5034f8829a4aSRandall Stewart 			break;
5035830d754dSRandall Stewart 			/*
5036830d754dSRandall Stewart 			 * EY - nr_sack:  If the received chunk is an
5037830d754dSRandall Stewart 			 * nr_sack chunk
5038830d754dSRandall Stewart 			 */
5039830d754dSRandall Stewart 		case SCTP_NR_SELECTIVE_ACK:
5040830d754dSRandall Stewart 			{
5041830d754dSRandall Stewart 				struct sctp_nr_sack_chunk *nr_sack;
5042830d754dSRandall Stewart 				int abort_now = 0;
5043830d754dSRandall Stewart 				uint32_t a_rwnd, cum_ack;
5044cd554309SMichael Tuexen 				uint16_t num_seg, num_nr_seg, num_dup;
5045cd554309SMichael Tuexen 				uint8_t flags;
5046cd554309SMichael Tuexen 				int offset_seg, offset_dup;
5047830d754dSRandall Stewart 
504820083c2eSMichael Tuexen 				SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n");
504920083c2eSMichael Tuexen 				SCTP_STAT_INCR(sctps_recvsacks);
5050cd554309SMichael Tuexen 				if (stcb == NULL) {
5051cd554309SMichael Tuexen 					SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n");
5052cd554309SMichael Tuexen 					break;
5053cd554309SMichael Tuexen 				}
5054caea9879SMichael Tuexen 				if (stcb->asoc.nrsack_supported == 0) {
505552129fcdSRandall Stewart 					goto unknown_chunk;
505652129fcdSRandall Stewart 				}
5057cd554309SMichael Tuexen 				if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
5058cd554309SMichael Tuexen 					SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n");
5059cd554309SMichael Tuexen 					break;
5060cd554309SMichael Tuexen 				}
5061830d754dSRandall Stewart 				if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
5062830d754dSRandall Stewart 					/*-
5063830d754dSRandall Stewart 					 * If we have sent a shutdown-ack, we will pay no
5064830d754dSRandall Stewart 					 * attention to a sack sent in to us since
5065830d754dSRandall Stewart 					 * we don't care anymore.
5066830d754dSRandall Stewart 					 */
5067cd554309SMichael Tuexen 					break;
5068830d754dSRandall Stewart 				}
5069830d754dSRandall Stewart 				nr_sack = (struct sctp_nr_sack_chunk *)ch;
5070cd554309SMichael Tuexen 				flags = ch->chunk_flags;
5071830d754dSRandall Stewart 				cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
5072830d754dSRandall Stewart 				num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
5073830d754dSRandall Stewart 				num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
5074cd554309SMichael Tuexen 				num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
5075830d754dSRandall Stewart 				a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd);
5076cd554309SMichael Tuexen 				if (sizeof(struct sctp_nr_sack_chunk) +
5077cd554309SMichael Tuexen 				    (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
5078cd554309SMichael Tuexen 				    num_dup * sizeof(uint32_t) != chk_length) {
5079cd554309SMichael Tuexen 					SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
5080cd554309SMichael Tuexen 					break;
5081cd554309SMichael Tuexen 				}
5082cd554309SMichael Tuexen 				offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
5083cd554309SMichael Tuexen 				offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
5084830d754dSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
5085cd554309SMichael Tuexen 				    cum_ack, num_seg, a_rwnd);
5086830d754dSRandall Stewart 				stcb->asoc.seen_a_sack_this_pkt = 1;
5087830d754dSRandall Stewart 				if ((stcb->asoc.pr_sctp_cnt == 0) &&
5088cd554309SMichael Tuexen 				    (num_seg == 0) && (num_nr_seg == 0) &&
508920b07a4dSMichael Tuexen 				    SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
5090830d754dSRandall Stewart 				    (stcb->asoc.saw_sack_with_frags == 0) &&
5091d9c5cfeaSMichael Tuexen 				    (stcb->asoc.saw_sack_with_nr_frags == 0) &&
5092cd554309SMichael Tuexen 				    (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
5093830d754dSRandall Stewart 					/*
5094830d754dSRandall Stewart 					 * We have a SIMPLE sack having no
5095830d754dSRandall Stewart 					 * prior segments and data on sent
5096cd554309SMichael Tuexen 					 * queue to be acked. Use the faster
5097cd554309SMichael Tuexen 					 * path sack processing. We also
5098cd554309SMichael Tuexen 					 * allow window update sacks with no
5099cd554309SMichael Tuexen 					 * missing segments to go this way
5100cd554309SMichael Tuexen 					 * too.
5101830d754dSRandall Stewart 					 */
5102493d8e5aSRandall Stewart 					sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
5103899288aeSRandall Stewart 					    &abort_now, ecne_seen);
5104830d754dSRandall Stewart 				} else {
5105830d754dSRandall Stewart 					if (netp && *netp)
51067215cc1bSMichael Tuexen 						sctp_handle_sack(m, offset_seg, offset_dup, stcb,
5107cd554309SMichael Tuexen 						    num_seg, num_nr_seg, num_dup, &abort_now, flags,
5108899288aeSRandall Stewart 						    cum_ack, a_rwnd, ecne_seen);
5109830d754dSRandall Stewart 				}
5110830d754dSRandall Stewart 				if (abort_now) {
5111830d754dSRandall Stewart 					/* ABORT signal from sack processing */
5112830d754dSRandall Stewart 					*offset = length;
5113830d754dSRandall Stewart 					return (NULL);
5114830d754dSRandall Stewart 				}
5115cd554309SMichael Tuexen 				if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5116cd554309SMichael Tuexen 				    TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5117cd554309SMichael Tuexen 				    (stcb->asoc.stream_queue_cnt == 0)) {
5118cd554309SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
5119cd554309SMichael Tuexen 				}
5120830d754dSRandall Stewart 			}
5121830d754dSRandall Stewart 			break;
5122830d754dSRandall Stewart 
5123f8829a4aSRandall Stewart 		case SCTP_HEARTBEAT_REQUEST:
5124ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
5125ad81507eSRandall Stewart 			if ((stcb) && netp && *netp) {
5126f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_recvheartbeat);
5127ad81507eSRandall Stewart 				sctp_send_heartbeat_ack(stcb, m, *offset,
5128ad81507eSRandall Stewart 				    chk_length, *netp);
5129f8829a4aSRandall Stewart 
5130f8829a4aSRandall Stewart 				/* He's alive so give him credit */
5131b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5132c4739e2fSRandall Stewart 					sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5133c4739e2fSRandall Stewart 					    stcb->asoc.overall_error_count,
5134c4739e2fSRandall Stewart 					    0,
5135c4739e2fSRandall Stewart 					    SCTP_FROM_SCTP_INPUT,
5136c4739e2fSRandall Stewart 					    __LINE__);
5137c4739e2fSRandall Stewart 				}
5138f8829a4aSRandall Stewart 				stcb->asoc.overall_error_count = 0;
5139ad81507eSRandall Stewart 			}
5140f8829a4aSRandall Stewart 			break;
5141f8829a4aSRandall Stewart 		case SCTP_HEARTBEAT_ACK:
5142ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n");
5143ad81507eSRandall Stewart 			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
5144d06c82f1SRandall Stewart 				/* Its not ours */
514517205eccSRandall Stewart 				*offset = length;
51466e55db54SRandall Stewart 				if (locked_tcb) {
51476114cd96SRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
51486e55db54SRandall Stewart 				}
5149d06c82f1SRandall Stewart 				return (NULL);
5150d06c82f1SRandall Stewart 			}
5151f8829a4aSRandall Stewart 			/* He's alive so give him credit */
5152b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5153c4739e2fSRandall Stewart 				sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5154c4739e2fSRandall Stewart 				    stcb->asoc.overall_error_count,
5155c4739e2fSRandall Stewart 				    0,
5156c4739e2fSRandall Stewart 				    SCTP_FROM_SCTP_INPUT,
5157c4739e2fSRandall Stewart 				    __LINE__);
5158c4739e2fSRandall Stewart 			}
5159f8829a4aSRandall Stewart 			stcb->asoc.overall_error_count = 0;
5160f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvheartbeatack);
5161ad81507eSRandall Stewart 			if (netp && *netp)
5162f8829a4aSRandall Stewart 				sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
5163f8829a4aSRandall Stewart 				    stcb, *netp);
5164f8829a4aSRandall Stewart 			break;
5165f8829a4aSRandall Stewart 		case SCTP_ABORT_ASSOCIATION:
5166207304d4SRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
5167dd294dceSMichael Tuexen 			    (void *)stcb);
5168ad81507eSRandall Stewart 			if ((stcb) && netp && *netp)
5169f8829a4aSRandall Stewart 				sctp_handle_abort((struct sctp_abort_chunk *)ch,
5170f8829a4aSRandall Stewart 				    stcb, *netp);
5171f8829a4aSRandall Stewart 			*offset = length;
5172f8829a4aSRandall Stewart 			return (NULL);
5173f8829a4aSRandall Stewart 			break;
5174f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN:
5175207304d4SRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
5176dd294dceSMichael Tuexen 			    (void *)stcb);
5177ad81507eSRandall Stewart 			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
5178d06c82f1SRandall Stewart 				*offset = length;
51796e55db54SRandall Stewart 				if (locked_tcb) {
51806114cd96SRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
51816e55db54SRandall Stewart 				}
5182d06c82f1SRandall Stewart 				return (NULL);
5183ad81507eSRandall Stewart 			}
5184ad81507eSRandall Stewart 			if (netp && *netp) {
5185f8829a4aSRandall Stewart 				int abort_flag = 0;
5186f8829a4aSRandall Stewart 
5187f8829a4aSRandall Stewart 				sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
5188f8829a4aSRandall Stewart 				    stcb, *netp, &abort_flag);
5189f8829a4aSRandall Stewart 				if (abort_flag) {
5190f8829a4aSRandall Stewart 					*offset = length;
5191f8829a4aSRandall Stewart 					return (NULL);
5192f8829a4aSRandall Stewart 				}
5193f8829a4aSRandall Stewart 			}
5194f8829a4aSRandall Stewart 			break;
5195f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_ACK:
5196dd294dceSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", (void *)stcb);
5197ad81507eSRandall Stewart 			if ((stcb) && (netp) && (*netp))
5198f8829a4aSRandall Stewart 				sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
5199f8829a4aSRandall Stewart 			*offset = length;
5200f8829a4aSRandall Stewart 			return (NULL);
5201f8829a4aSRandall Stewart 			break;
5202ad81507eSRandall Stewart 
5203f8829a4aSRandall Stewart 		case SCTP_OPERATION_ERROR:
5204ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n");
5205ad81507eSRandall Stewart 			if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) {
5206f8829a4aSRandall Stewart 				*offset = length;
5207f8829a4aSRandall Stewart 				return (NULL);
5208f8829a4aSRandall Stewart 			}
5209f8829a4aSRandall Stewart 			break;
5210f8829a4aSRandall Stewart 		case SCTP_COOKIE_ECHO:
5211ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3,
5212dd294dceSMichael Tuexen 			    "SCTP_COOKIE-ECHO, stcb %p\n", (void *)stcb);
5213f8829a4aSRandall Stewart 			if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5214f8829a4aSRandall Stewart 				;
5215f8829a4aSRandall Stewart 			} else {
5216ad81507eSRandall Stewart 				if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5217f8829a4aSRandall Stewart 					/* We are not interested anymore */
52180c7dc840SRandall Stewart 			abend:
521928085b2eSRandall Stewart 					if (stcb) {
522028085b2eSRandall Stewart 						SCTP_TCB_UNLOCK(stcb);
522128085b2eSRandall Stewart 					}
5222f8829a4aSRandall Stewart 					*offset = length;
5223f8829a4aSRandall Stewart 					return (NULL);
5224f8829a4aSRandall Stewart 				}
5225f8829a4aSRandall Stewart 			}
5226f8829a4aSRandall Stewart 			/*
5227f8829a4aSRandall Stewart 			 * First are we accepting? We do this again here
522888a7eb29SRandall Stewart 			 * since it is possible that a previous endpoint WAS
522988a7eb29SRandall Stewart 			 * listening responded to a INIT-ACK and then
5230f8829a4aSRandall Stewart 			 * closed. We opened and bound.. and are now no
5231f8829a4aSRandall Stewart 			 * longer listening.
5232f8829a4aSRandall Stewart 			 */
5233b201f536SRandall Stewart 
5234b201f536SRandall Stewart 			if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) {
5235b201f536SRandall Stewart 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5236b3f1ea41SRandall Stewart 				    (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
5237ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
5238b1754ad1SMichael Tuexen 					sctp_abort_association(inp, stcb, m, iphlen,
5239b1754ad1SMichael Tuexen 					    src, dst, sh, op_err,
5240457b4b88SMichael Tuexen 					    mflowtype, mflowid,
5241f30ac432SMichael Tuexen 					    vrf_id, port);
5242f8829a4aSRandall Stewart 				}
5243f8829a4aSRandall Stewart 				*offset = length;
5244f8829a4aSRandall Stewart 				return (NULL);
5245b201f536SRandall Stewart 			} else {
5246f8829a4aSRandall Stewart 				struct mbuf *ret_buf;
5247a5d547adSRandall Stewart 				struct sctp_inpcb *linp;
5248f8829a4aSRandall Stewart 
5249ad81507eSRandall Stewart 				if (stcb) {
5250a5d547adSRandall Stewart 					linp = NULL;
5251ad81507eSRandall Stewart 				} else {
5252a5d547adSRandall Stewart 					linp = inp;
5253ad81507eSRandall Stewart 				}
5254a5d547adSRandall Stewart 
5255ad81507eSRandall Stewart 				if (linp) {
5256a5d547adSRandall Stewart 					SCTP_ASOC_CREATE_LOCK(linp);
52570c7dc840SRandall Stewart 					if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
52580c7dc840SRandall Stewart 					    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
52590c7dc840SRandall Stewart 						SCTP_ASOC_CREATE_UNLOCK(linp);
52600c7dc840SRandall Stewart 						goto abend;
52610c7dc840SRandall Stewart 					}
5262ad81507eSRandall Stewart 				}
5263ad81507eSRandall Stewart 				if (netp) {
5264f8829a4aSRandall Stewart 					ret_buf =
5265f8829a4aSRandall Stewart 					    sctp_handle_cookie_echo(m, iphlen,
5266b1754ad1SMichael Tuexen 					    *offset,
5267b1754ad1SMichael Tuexen 					    src, dst,
5268b1754ad1SMichael Tuexen 					    sh,
5269f8829a4aSRandall Stewart 					    (struct sctp_cookie_echo_chunk *)ch,
5270f8829a4aSRandall Stewart 					    &inp, &stcb, netp,
5271f8829a4aSRandall Stewart 					    auth_skipped,
5272f8829a4aSRandall Stewart 					    auth_offset,
5273a5d547adSRandall Stewart 					    auth_len,
527417205eccSRandall Stewart 					    &locked_tcb,
5275457b4b88SMichael Tuexen 					    mflowtype,
5276f30ac432SMichael Tuexen 					    mflowid,
5277c54a18d2SRandall Stewart 					    vrf_id,
5278c54a18d2SRandall Stewart 					    port);
5279ad81507eSRandall Stewart 				} else {
5280ad81507eSRandall Stewart 					ret_buf = NULL;
5281ad81507eSRandall Stewart 				}
5282ad81507eSRandall Stewart 				if (linp) {
5283a5d547adSRandall Stewart 					SCTP_ASOC_CREATE_UNLOCK(linp);
5284ad81507eSRandall Stewart 				}
5285f8829a4aSRandall Stewart 				if (ret_buf == NULL) {
5286f8829a4aSRandall Stewart 					if (locked_tcb) {
5287f8829a4aSRandall Stewart 						SCTP_TCB_UNLOCK(locked_tcb);
5288f8829a4aSRandall Stewart 					}
5289ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INPUT3,
5290ad81507eSRandall Stewart 					    "GAK, null buffer\n");
5291f8829a4aSRandall Stewart 					*offset = length;
5292f8829a4aSRandall Stewart 					return (NULL);
5293f8829a4aSRandall Stewart 				}
5294f8829a4aSRandall Stewart 				/* if AUTH skipped, see if it verified... */
5295f8829a4aSRandall Stewart 				if (auth_skipped) {
5296f8829a4aSRandall Stewart 					got_auth = 1;
5297f8829a4aSRandall Stewart 					auth_skipped = 0;
5298f8829a4aSRandall Stewart 				}
5299f8829a4aSRandall Stewart 				if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
5300f8829a4aSRandall Stewart 					/*
5301f8829a4aSRandall Stewart 					 * Restart the timer if we have
5302f8829a4aSRandall Stewart 					 * pending data
5303f8829a4aSRandall Stewart 					 */
5304f8829a4aSRandall Stewart 					struct sctp_tmit_chunk *chk;
5305f8829a4aSRandall Stewart 
5306f8829a4aSRandall Stewart 					chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
53074a9ef3f8SMichael Tuexen 					sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
5308f8829a4aSRandall Stewart 				}
5309f8829a4aSRandall Stewart 			}
5310f8829a4aSRandall Stewart 			break;
5311f8829a4aSRandall Stewart 		case SCTP_COOKIE_ACK:
5312dd294dceSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", (void *)stcb);
5313ad81507eSRandall Stewart 			if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
53146e55db54SRandall Stewart 				if (locked_tcb) {
531517205eccSRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
53166e55db54SRandall Stewart 				}
531717205eccSRandall Stewart 				return (NULL);
531817205eccSRandall Stewart 			}
5319f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5320f8829a4aSRandall Stewart 				/* We are not interested anymore */
5321f8829a4aSRandall Stewart 				if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5322f8829a4aSRandall Stewart 					;
5323ad81507eSRandall Stewart 				} else if (stcb) {
5324ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5325ceaad40aSRandall Stewart 					so = SCTP_INP_SO(inp);
5326ceaad40aSRandall Stewart 					atomic_add_int(&stcb->asoc.refcnt, 1);
5327ceaad40aSRandall Stewart 					SCTP_TCB_UNLOCK(stcb);
5328ceaad40aSRandall Stewart 					SCTP_SOCKET_LOCK(so, 1);
5329ceaad40aSRandall Stewart 					SCTP_TCB_LOCK(stcb);
5330ceaad40aSRandall Stewart 					atomic_subtract_int(&stcb->asoc.refcnt, 1);
5331ceaad40aSRandall Stewart #endif
5332b7d130beSMichael Tuexen 					(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5333b7d130beSMichael Tuexen 					    SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
5334ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5335ceaad40aSRandall Stewart 					SCTP_SOCKET_UNLOCK(so, 1);
5336ceaad40aSRandall Stewart #endif
5337f8829a4aSRandall Stewart 					*offset = length;
5338f8829a4aSRandall Stewart 					return (NULL);
5339f8829a4aSRandall Stewart 				}
5340f8829a4aSRandall Stewart 			}
5341f8829a4aSRandall Stewart 			/* He's alive so give him credit */
5342ad81507eSRandall Stewart 			if ((stcb) && netp && *netp) {
5343b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5344c4739e2fSRandall Stewart 					sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5345c4739e2fSRandall Stewart 					    stcb->asoc.overall_error_count,
5346c4739e2fSRandall Stewart 					    0,
5347c4739e2fSRandall Stewart 					    SCTP_FROM_SCTP_INPUT,
5348c4739e2fSRandall Stewart 					    __LINE__);
5349c4739e2fSRandall Stewart 				}
5350f8829a4aSRandall Stewart 				stcb->asoc.overall_error_count = 0;
5351f8829a4aSRandall Stewart 				sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
53526e55db54SRandall Stewart 			}
5353f8829a4aSRandall Stewart 			break;
5354f8829a4aSRandall Stewart 		case SCTP_ECN_ECHO:
5355ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n");
5356f8829a4aSRandall Stewart 			/* He's alive so give him credit */
5357ad81507eSRandall Stewart 			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5358d06c82f1SRandall Stewart 				/* Its not ours */
53596e55db54SRandall Stewart 				if (locked_tcb) {
53606114cd96SRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
53616e55db54SRandall Stewart 				}
5362d06c82f1SRandall Stewart 				*offset = length;
5363d06c82f1SRandall Stewart 				return (NULL);
5364d06c82f1SRandall Stewart 			}
53656e55db54SRandall Stewart 			if (stcb) {
5366c79bec9cSMichael Tuexen 				if (stcb->asoc.ecn_supported == 0) {
5367c79bec9cSMichael Tuexen 					goto unknown_chunk;
5368c79bec9cSMichael Tuexen 				}
5369b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5370c4739e2fSRandall Stewart 					sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5371c4739e2fSRandall Stewart 					    stcb->asoc.overall_error_count,
5372c4739e2fSRandall Stewart 					    0,
5373c4739e2fSRandall Stewart 					    SCTP_FROM_SCTP_INPUT,
5374c4739e2fSRandall Stewart 					    __LINE__);
5375c4739e2fSRandall Stewart 				}
5376f8829a4aSRandall Stewart 				stcb->asoc.overall_error_count = 0;
5377f8829a4aSRandall Stewart 				sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
5378f8829a4aSRandall Stewart 				    stcb);
5379899288aeSRandall Stewart 				ecne_seen = 1;
53806e55db54SRandall Stewart 			}
5381f8829a4aSRandall Stewart 			break;
5382f8829a4aSRandall Stewart 		case SCTP_ECN_CWR:
5383ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n");
5384f8829a4aSRandall Stewart 			/* He's alive so give him credit */
5385ad81507eSRandall Stewart 			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5386d06c82f1SRandall Stewart 				/* Its not ours */
53876e55db54SRandall Stewart 				if (locked_tcb) {
53886114cd96SRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
53896e55db54SRandall Stewart 				}
5390d06c82f1SRandall Stewart 				*offset = length;
5391d06c82f1SRandall Stewart 				return (NULL);
5392d06c82f1SRandall Stewart 			}
53936e55db54SRandall Stewart 			if (stcb) {
5394c79bec9cSMichael Tuexen 				if (stcb->asoc.ecn_supported == 0) {
5395c79bec9cSMichael Tuexen 					goto unknown_chunk;
5396c79bec9cSMichael Tuexen 				}
5397b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5398c4739e2fSRandall Stewart 					sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5399c4739e2fSRandall Stewart 					    stcb->asoc.overall_error_count,
5400c4739e2fSRandall Stewart 					    0,
5401c4739e2fSRandall Stewart 					    SCTP_FROM_SCTP_INPUT,
5402c4739e2fSRandall Stewart 					    __LINE__);
5403c4739e2fSRandall Stewart 				}
5404f8829a4aSRandall Stewart 				stcb->asoc.overall_error_count = 0;
5405a21779f0SRandall Stewart 				sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
54066e55db54SRandall Stewart 			}
5407f8829a4aSRandall Stewart 			break;
5408f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_COMPLETE:
5409dd294dceSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", (void *)stcb);
5410f8829a4aSRandall Stewart 			/* must be first and only chunk */
5411f8829a4aSRandall Stewart 			if ((num_chunks > 1) ||
54124c9179adSRandall Stewart 			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5413f8829a4aSRandall Stewart 				*offset = length;
54146e55db54SRandall Stewart 				if (locked_tcb) {
5415f8829a4aSRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
54166e55db54SRandall Stewart 				}
5417f8829a4aSRandall Stewart 				return (NULL);
5418f8829a4aSRandall Stewart 			}
5419ad81507eSRandall Stewart 			if ((stcb) && netp && *netp) {
5420f8829a4aSRandall Stewart 				sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5421f8829a4aSRandall Stewart 				    stcb, *netp);
54226e55db54SRandall Stewart 			}
5423f8829a4aSRandall Stewart 			*offset = length;
5424f8829a4aSRandall Stewart 			return (NULL);
5425f8829a4aSRandall Stewart 			break;
5426f8829a4aSRandall Stewart 		case SCTP_ASCONF:
5427ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5428f8829a4aSRandall Stewart 			/* He's alive so give him credit */
54296e55db54SRandall Stewart 			if (stcb) {
5430c79bec9cSMichael Tuexen 				if (stcb->asoc.asconf_supported == 0) {
5431c79bec9cSMichael Tuexen 					goto unknown_chunk;
5432c79bec9cSMichael Tuexen 				}
5433b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5434c4739e2fSRandall Stewart 					sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5435c4739e2fSRandall Stewart 					    stcb->asoc.overall_error_count,
5436c4739e2fSRandall Stewart 					    0,
5437c4739e2fSRandall Stewart 					    SCTP_FROM_SCTP_INPUT,
5438c4739e2fSRandall Stewart 					    __LINE__);
5439c4739e2fSRandall Stewart 				}
5440f8829a4aSRandall Stewart 				stcb->asoc.overall_error_count = 0;
5441b1754ad1SMichael Tuexen 				sctp_handle_asconf(m, *offset, src,
54422afb3e84SRandall Stewart 				    (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
54432afb3e84SRandall Stewart 				asconf_cnt++;
54446e55db54SRandall Stewart 			}
5445f8829a4aSRandall Stewart 			break;
5446f8829a4aSRandall Stewart 		case SCTP_ASCONF_ACK:
5447ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n");
5448d06c82f1SRandall Stewart 			if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5449d06c82f1SRandall Stewart 				/* Its not ours */
54506e55db54SRandall Stewart 				if (locked_tcb) {
54516114cd96SRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
54526e55db54SRandall Stewart 				}
5453d06c82f1SRandall Stewart 				*offset = length;
5454d06c82f1SRandall Stewart 				return (NULL);
5455d06c82f1SRandall Stewart 			}
5456ad81507eSRandall Stewart 			if ((stcb) && netp && *netp) {
5457c79bec9cSMichael Tuexen 				if (stcb->asoc.asconf_supported == 0) {
5458c79bec9cSMichael Tuexen 					goto unknown_chunk;
5459c79bec9cSMichael Tuexen 				}
5460f8829a4aSRandall Stewart 				/* He's alive so give him credit */
5461b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5462c4739e2fSRandall Stewart 					sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5463c4739e2fSRandall Stewart 					    stcb->asoc.overall_error_count,
5464c4739e2fSRandall Stewart 					    0,
5465c4739e2fSRandall Stewart 					    SCTP_FROM_SCTP_INPUT,
5466c4739e2fSRandall Stewart 					    __LINE__);
5467c4739e2fSRandall Stewart 				}
5468f8829a4aSRandall Stewart 				stcb->asoc.overall_error_count = 0;
5469f8829a4aSRandall Stewart 				sctp_handle_asconf_ack(m, *offset,
54703232788eSRandall Stewart 				    (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
54713232788eSRandall Stewart 				if (abort_no_unlock)
54723232788eSRandall Stewart 					return (NULL);
54736e55db54SRandall Stewart 			}
5474f8829a4aSRandall Stewart 			break;
5475f8829a4aSRandall Stewart 		case SCTP_FORWARD_CUM_TSN:
5476*44249214SRandall Stewart 		case SCTP_IFORWARD_CUM_TSN:
5477ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n");
5478d06c82f1SRandall Stewart 			if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5479d06c82f1SRandall Stewart 				/* Its not ours */
54806e55db54SRandall Stewart 				if (locked_tcb) {
54816114cd96SRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
54826e55db54SRandall Stewart 				}
5483d06c82f1SRandall Stewart 				*offset = length;
5484d06c82f1SRandall Stewart 				return (NULL);
5485d06c82f1SRandall Stewart 			}
5486f8829a4aSRandall Stewart 			/* He's alive so give him credit */
54876e55db54SRandall Stewart 			if (stcb) {
5488f8829a4aSRandall Stewart 				int abort_flag = 0;
5489f8829a4aSRandall Stewart 
5490c79bec9cSMichael Tuexen 				if (stcb->asoc.prsctp_supported == 0) {
5491c79bec9cSMichael Tuexen 					goto unknown_chunk;
5492c79bec9cSMichael Tuexen 				}
5493f8829a4aSRandall Stewart 				stcb->asoc.overall_error_count = 0;
5494b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5495c4739e2fSRandall Stewart 					sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5496c4739e2fSRandall Stewart 					    stcb->asoc.overall_error_count,
5497c4739e2fSRandall Stewart 					    0,
5498c4739e2fSRandall Stewart 					    SCTP_FROM_SCTP_INPUT,
5499c4739e2fSRandall Stewart 					    __LINE__);
5500c4739e2fSRandall Stewart 				}
5501f8829a4aSRandall Stewart 				*fwd_tsn_seen = 1;
5502f8829a4aSRandall Stewart 				if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5503f8829a4aSRandall Stewart 					/* We are not interested anymore */
5504ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5505ceaad40aSRandall Stewart 					so = SCTP_INP_SO(inp);
5506ceaad40aSRandall Stewart 					atomic_add_int(&stcb->asoc.refcnt, 1);
5507ceaad40aSRandall Stewart 					SCTP_TCB_UNLOCK(stcb);
5508ceaad40aSRandall Stewart 					SCTP_SOCKET_LOCK(so, 1);
5509ceaad40aSRandall Stewart 					SCTP_TCB_LOCK(stcb);
5510ceaad40aSRandall Stewart 					atomic_subtract_int(&stcb->asoc.refcnt, 1);
5511ceaad40aSRandall Stewart #endif
5512b7d130beSMichael Tuexen 					(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5513b7d130beSMichael Tuexen 					    SCTP_FROM_SCTP_INPUT + SCTP_LOC_31);
5514ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5515ceaad40aSRandall Stewart 					SCTP_SOCKET_UNLOCK(so, 1);
5516ceaad40aSRandall Stewart #endif
5517f8829a4aSRandall Stewart 					*offset = length;
5518f8829a4aSRandall Stewart 					return (NULL);
5519f8829a4aSRandall Stewart 				}
5520f8829a4aSRandall Stewart 				sctp_handle_forward_tsn(stcb,
5521671d309cSRandall Stewart 				    (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5522f8829a4aSRandall Stewart 				if (abort_flag) {
5523f8829a4aSRandall Stewart 					*offset = length;
5524f8829a4aSRandall Stewart 					return (NULL);
5525f8829a4aSRandall Stewart 				} else {
5526b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5527c4739e2fSRandall Stewart 						sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5528c4739e2fSRandall Stewart 						    stcb->asoc.overall_error_count,
5529c4739e2fSRandall Stewart 						    0,
5530c4739e2fSRandall Stewart 						    SCTP_FROM_SCTP_INPUT,
5531c4739e2fSRandall Stewart 						    __LINE__);
5532c4739e2fSRandall Stewart 					}
5533f8829a4aSRandall Stewart 					stcb->asoc.overall_error_count = 0;
5534f8829a4aSRandall Stewart 				}
5535f8829a4aSRandall Stewart 
5536f8829a4aSRandall Stewart 			}
5537f8829a4aSRandall Stewart 			break;
5538f8829a4aSRandall Stewart 		case SCTP_STREAM_RESET:
5539ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5540ad81507eSRandall Stewart 			if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5541d06c82f1SRandall Stewart 				/* Its not ours */
55426e55db54SRandall Stewart 				if (locked_tcb) {
55436114cd96SRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
55446e55db54SRandall Stewart 				}
5545d06c82f1SRandall Stewart 				*offset = length;
5546d06c82f1SRandall Stewart 				return (NULL);
5547d06c82f1SRandall Stewart 			}
5548317e00efSMichael Tuexen 			if (stcb->asoc.reconfig_supported == 0) {
5549c79bec9cSMichael Tuexen 				goto unknown_chunk;
5550f8829a4aSRandall Stewart 			}
5551a169d6ecSMichael Tuexen 			if (sctp_handle_stream_reset(stcb, m, *offset, ch)) {
5552f8829a4aSRandall Stewart 				/* stop processing */
5553f8829a4aSRandall Stewart 				*offset = length;
5554f8829a4aSRandall Stewart 				return (NULL);
5555f8829a4aSRandall Stewart 			}
5556f8829a4aSRandall Stewart 			break;
5557f8829a4aSRandall Stewart 		case SCTP_PACKET_DROPPED:
5558ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5559f8829a4aSRandall Stewart 			/* re-get it all please */
5560d06c82f1SRandall Stewart 			if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5561d06c82f1SRandall Stewart 				/* Its not ours */
55626e55db54SRandall Stewart 				if (locked_tcb) {
55636114cd96SRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
55646e55db54SRandall Stewart 				}
5565d06c82f1SRandall Stewart 				*offset = length;
5566d06c82f1SRandall Stewart 				return (NULL);
5567d06c82f1SRandall Stewart 			}
5568ad81507eSRandall Stewart 			if (ch && (stcb) && netp && (*netp)) {
5569c79bec9cSMichael Tuexen 				if (stcb->asoc.pktdrop_supported == 0) {
5570c79bec9cSMichael Tuexen 					goto unknown_chunk;
5571c79bec9cSMichael Tuexen 				}
5572f8829a4aSRandall Stewart 				sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5573458303daSRandall Stewart 				    stcb, *netp,
5574458303daSRandall Stewart 				    min(chk_length, (sizeof(chunk_buf) - 4)));
5575458303daSRandall Stewart 
55766e55db54SRandall Stewart 			}
5577f8829a4aSRandall Stewart 			break;
5578f8829a4aSRandall Stewart 		case SCTP_AUTHENTICATION:
5579ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5580f8829a4aSRandall Stewart 			if (stcb == NULL) {
5581f8829a4aSRandall Stewart 				/* save the first AUTH for later processing */
5582f8829a4aSRandall Stewart 				if (auth_skipped == 0) {
5583f8829a4aSRandall Stewart 					auth_offset = *offset;
5584f8829a4aSRandall Stewart 					auth_len = chk_length;
5585f8829a4aSRandall Stewart 					auth_skipped = 1;
5586f8829a4aSRandall Stewart 				}
5587f8829a4aSRandall Stewart 				/* skip this chunk (temporarily) */
5588f8829a4aSRandall Stewart 				goto next_chunk;
5589f8829a4aSRandall Stewart 			}
5590c79bec9cSMichael Tuexen 			if (stcb->asoc.auth_supported == 0) {
5591c79bec9cSMichael Tuexen 				goto unknown_chunk;
5592c79bec9cSMichael Tuexen 			}
5593d06c82f1SRandall Stewart 			if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5594ad81507eSRandall Stewart 			    (chk_length > (sizeof(struct sctp_auth_chunk) +
5595ad81507eSRandall Stewart 			    SCTP_AUTH_DIGEST_LEN_MAX))) {
5596d06c82f1SRandall Stewart 				/* Its not ours */
55976e55db54SRandall Stewart 				if (locked_tcb) {
55986114cd96SRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
55996e55db54SRandall Stewart 				}
5600d06c82f1SRandall Stewart 				*offset = length;
5601d06c82f1SRandall Stewart 				return (NULL);
5602d06c82f1SRandall Stewart 			}
5603f8829a4aSRandall Stewart 			if (got_auth == 1) {
5604f8829a4aSRandall Stewart 				/* skip this chunk... it's already auth'd */
5605f8829a4aSRandall Stewart 				goto next_chunk;
5606f8829a4aSRandall Stewart 			}
5607f8829a4aSRandall Stewart 			got_auth = 1;
5608ad81507eSRandall Stewart 			if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5609f8829a4aSRandall Stewart 			    m, *offset)) {
5610f8829a4aSRandall Stewart 				/* auth HMAC failed so dump the packet */
5611f8829a4aSRandall Stewart 				*offset = length;
5612f8829a4aSRandall Stewart 				return (stcb);
5613f8829a4aSRandall Stewart 			} else {
5614f8829a4aSRandall Stewart 				/* remaining chunks are HMAC checked */
5615f8829a4aSRandall Stewart 				stcb->asoc.authenticated = 1;
5616f8829a4aSRandall Stewart 			}
5617f8829a4aSRandall Stewart 			break;
5618f8829a4aSRandall Stewart 
5619f8829a4aSRandall Stewart 		default:
5620f8829a4aSRandall Stewart 	unknown_chunk:
5621f8829a4aSRandall Stewart 			/* it's an unknown chunk! */
5622f8829a4aSRandall Stewart 			if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
562386eda749SMichael Tuexen 				struct sctp_gen_error_cause *cause;
562439cbb549SMichael Tuexen 				int len;
5625f8829a4aSRandall Stewart 
562686eda749SMichael Tuexen 				op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_gen_error_cause),
5627eb1b1807SGleb Smirnoff 				    0, M_NOWAIT, 1, MT_DATA);
562886eda749SMichael Tuexen 				if (op_err != NULL) {
562939cbb549SMichael Tuexen 					len = min(SCTP_SIZE32(chk_length), (uint32_t) (length - *offset));
563086eda749SMichael Tuexen 					cause = mtod(op_err, struct sctp_gen_error_cause *);
563186eda749SMichael Tuexen 					cause->code = htons(SCTP_CAUSE_UNRECOG_CHUNK);
56329a8e3088SMichael Tuexen 					cause->length = htons((uint16_t) (len + sizeof(struct sctp_gen_error_cause)));
563386eda749SMichael Tuexen 					SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
563486eda749SMichael Tuexen 					SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(m, *offset, len, M_NOWAIT);
563586eda749SMichael Tuexen 					if (SCTP_BUF_NEXT(op_err) != NULL) {
5636eadccaccSRandall Stewart #ifdef SCTP_MBUF_LOGGING
5637b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
563830811e70SMichael Tuexen 							sctp_log_mbc(SCTP_BUF_NEXT(op_err), SCTP_MBUF_ICOPY);
5639eadccaccSRandall Stewart 						}
5640eadccaccSRandall Stewart #endif
564186eda749SMichael Tuexen 						sctp_queue_op_err(stcb, op_err);
5642f8829a4aSRandall Stewart 					} else {
564386eda749SMichael Tuexen 						sctp_m_freem(op_err);
5644f8829a4aSRandall Stewart 					}
5645f8829a4aSRandall Stewart 				}
5646f8829a4aSRandall Stewart 			}
5647f8829a4aSRandall Stewart 			if ((ch->chunk_type & 0x80) == 0) {
5648f8829a4aSRandall Stewart 				/* discard this packet */
5649f8829a4aSRandall Stewart 				*offset = length;
5650f8829a4aSRandall Stewart 				return (stcb);
5651f8829a4aSRandall Stewart 			}	/* else skip this bad chunk and continue... */
5652f8829a4aSRandall Stewart 			break;
5653f8829a4aSRandall Stewart 		}		/* switch (ch->chunk_type) */
5654f8829a4aSRandall Stewart 
5655f8829a4aSRandall Stewart 
5656f8829a4aSRandall Stewart next_chunk:
5657f8829a4aSRandall Stewart 		/* get the next chunk */
5658f8829a4aSRandall Stewart 		*offset += SCTP_SIZE32(chk_length);
5659f8829a4aSRandall Stewart 		if (*offset >= length) {
5660f8829a4aSRandall Stewart 			/* no more data left in the mbuf chain */
5661f8829a4aSRandall Stewart 			break;
5662f8829a4aSRandall Stewart 		}
5663f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5664f8829a4aSRandall Stewart 		    sizeof(struct sctp_chunkhdr), chunk_buf);
5665f8829a4aSRandall Stewart 		if (ch == NULL) {
56666e55db54SRandall Stewart 			if (locked_tcb) {
5667f8829a4aSRandall Stewart 				SCTP_TCB_UNLOCK(locked_tcb);
56686e55db54SRandall Stewart 			}
5669f8829a4aSRandall Stewart 			*offset = length;
5670f8829a4aSRandall Stewart 			return (NULL);
5671f8829a4aSRandall Stewart 		}
5672f8829a4aSRandall Stewart 	}			/* while */
56732afb3e84SRandall Stewart 
56742afb3e84SRandall Stewart 	if (asconf_cnt > 0 && stcb != NULL) {
56752afb3e84SRandall Stewart 		sctp_send_asconf_ack(stcb);
56762afb3e84SRandall Stewart 	}
5677f8829a4aSRandall Stewart 	return (stcb);
5678f8829a4aSRandall Stewart }
5679f8829a4aSRandall Stewart 
5680f8829a4aSRandall Stewart 
5681f8829a4aSRandall Stewart /*
5682f8829a4aSRandall Stewart  * common input chunk processing (v4 and v6)
5683f8829a4aSRandall Stewart  */
56846e55db54SRandall Stewart void
5685b1754ad1SMichael Tuexen sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
5686b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
5687b1754ad1SMichael Tuexen     struct sctphdr *sh, struct sctp_chunkhdr *ch,
5688a8775ad9SMichael Tuexen #if !defined(SCTP_WITH_NO_CSUM)
5689a8775ad9SMichael Tuexen     uint8_t compute_crc,
5690a8775ad9SMichael Tuexen #endif
5691a8775ad9SMichael Tuexen     uint8_t ecn_bits,
5692d089f9b9SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
5693f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
5694f8829a4aSRandall Stewart {
5695f8829a4aSRandall Stewart 	uint32_t high_tsn;
5696f8829a4aSRandall Stewart 	int fwd_tsn_seen = 0, data_processed = 0;
5697ff1ffd74SMichael Tuexen 	struct mbuf *m = *mm, *op_err;
5698ff1ffd74SMichael Tuexen 	char msg[SCTP_DIAG_INFO_LEN];
5699f8829a4aSRandall Stewart 	int un_sent;
5700493d8e5aSRandall Stewart 	int cnt_ctrl_ready = 0;
570155b175e7SMichael Tuexen 	struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
5702a8775ad9SMichael Tuexen 	struct sctp_tcb *stcb = NULL;
5703e3d6ef0bSMichael Tuexen 	struct sctp_nets *net = NULL;
5704f8829a4aSRandall Stewart 
5705f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvdatagrams);
5706f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
5707f8829a4aSRandall Stewart 	sctp_audit_log(0xE0, 1);
5708f8829a4aSRandall Stewart 	sctp_auditing(0, inp, stcb, net);
5709f8829a4aSRandall Stewart #endif
5710a8775ad9SMichael Tuexen #if !defined(SCTP_WITH_NO_CSUM)
5711a8775ad9SMichael Tuexen 	if (compute_crc != 0) {
5712a8775ad9SMichael Tuexen 		uint32_t check, calc_check;
5713f8829a4aSRandall Stewart 
5714a8775ad9SMichael Tuexen 		check = sh->checksum;
5715a8775ad9SMichael Tuexen 		sh->checksum = 0;
5716a8775ad9SMichael Tuexen 		calc_check = sctp_calculate_cksum(m, iphlen);
5717a8775ad9SMichael Tuexen 		sh->checksum = check;
5718a8775ad9SMichael Tuexen 		if (calc_check != check) {
5719a8775ad9SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x  m:%p mlen:%d iphlen:%d\n",
5720dd294dceSMichael Tuexen 			    calc_check, check, (void *)m, length, iphlen);
5721a8775ad9SMichael Tuexen 			stcb = sctp_findassociation_addr(m, offset, src, dst,
5722a8775ad9SMichael Tuexen 			    sh, ch, &inp, &net, vrf_id);
57234474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
57243cf729a9SMichael Tuexen 			if ((ch->chunk_type != SCTP_INITIATION) &&
57253cf729a9SMichael Tuexen 			    (net != NULL) && (net->port != port)) {
5726a8775ad9SMichael Tuexen 				if (net->port == 0) {
57273cf729a9SMichael Tuexen 					/* UDP encapsulation turned on. */
57283cf729a9SMichael Tuexen 					net->mtu -= sizeof(struct udphdr);
57293cf729a9SMichael Tuexen 					if (stcb->asoc.smallest_mtu > net->mtu) {
57303cf729a9SMichael Tuexen 						sctp_pathmtu_adjustment(stcb, net->mtu);
57313cf729a9SMichael Tuexen 					}
57323cf729a9SMichael Tuexen 				} else if (port == 0) {
57333cf729a9SMichael Tuexen 					/* UDP encapsulation turned off. */
57343cf729a9SMichael Tuexen 					net->mtu += sizeof(struct udphdr);
57353cf729a9SMichael Tuexen 					/* XXX Update smallest_mtu */
5736a8775ad9SMichael Tuexen 				}
5737a8775ad9SMichael Tuexen 				net->port = port;
5738a8775ad9SMichael Tuexen 			}
57394474d71aSMichael Tuexen #endif
57405fe29cdfSMichael Tuexen 			if (net != NULL) {
5741457b4b88SMichael Tuexen 				net->flowtype = mflowtype;
57425fe29cdfSMichael Tuexen 				net->flowid = mflowid;
5743a8775ad9SMichael Tuexen 			}
5744a8775ad9SMichael Tuexen 			if ((inp != NULL) && (stcb != NULL)) {
5745a8775ad9SMichael Tuexen 				sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
5746a8775ad9SMichael Tuexen 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5747a8775ad9SMichael Tuexen 			} else if ((inp != NULL) && (stcb == NULL)) {
5748a8775ad9SMichael Tuexen 				inp_decr = inp;
5749a8775ad9SMichael Tuexen 			}
5750a8775ad9SMichael Tuexen 			SCTP_STAT_INCR(sctps_badsum);
5751a8775ad9SMichael Tuexen 			SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5752a8775ad9SMichael Tuexen 			goto out;
5753a8775ad9SMichael Tuexen 		}
5754a8775ad9SMichael Tuexen 	}
5755a8775ad9SMichael Tuexen #endif
5756a8775ad9SMichael Tuexen 	/* Destination port of 0 is illegal, based on RFC4960. */
5757a8775ad9SMichael Tuexen 	if (sh->dest_port == 0) {
5758a8775ad9SMichael Tuexen 		SCTP_STAT_INCR(sctps_hdrops);
5759a8775ad9SMichael Tuexen 		goto out;
5760a8775ad9SMichael Tuexen 	}
5761a8775ad9SMichael Tuexen 	stcb = sctp_findassociation_addr(m, offset, src, dst,
5762a8775ad9SMichael Tuexen 	    sh, ch, &inp, &net, vrf_id);
57634474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
57643cf729a9SMichael Tuexen 	if ((ch->chunk_type != SCTP_INITIATION) &&
57653cf729a9SMichael Tuexen 	    (net != NULL) && (net->port != port)) {
5766a8775ad9SMichael Tuexen 		if (net->port == 0) {
57673cf729a9SMichael Tuexen 			/* UDP encapsulation turned on. */
57683cf729a9SMichael Tuexen 			net->mtu -= sizeof(struct udphdr);
57693cf729a9SMichael Tuexen 			if (stcb->asoc.smallest_mtu > net->mtu) {
57703cf729a9SMichael Tuexen 				sctp_pathmtu_adjustment(stcb, net->mtu);
57713cf729a9SMichael Tuexen 			}
57723cf729a9SMichael Tuexen 		} else if (port == 0) {
57733cf729a9SMichael Tuexen 			/* UDP encapsulation turned off. */
57743cf729a9SMichael Tuexen 			net->mtu += sizeof(struct udphdr);
57753cf729a9SMichael Tuexen 			/* XXX Update smallest_mtu */
5776a8775ad9SMichael Tuexen 		}
5777a8775ad9SMichael Tuexen 		net->port = port;
5778a8775ad9SMichael Tuexen 	}
57794474d71aSMichael Tuexen #endif
57805fe29cdfSMichael Tuexen 	if (net != NULL) {
5781457b4b88SMichael Tuexen 		net->flowtype = mflowtype;
57825fe29cdfSMichael Tuexen 		net->flowid = mflowid;
5783a8775ad9SMichael Tuexen 	}
5784a8775ad9SMichael Tuexen 	if (inp == NULL) {
5785a8775ad9SMichael Tuexen 		SCTP_STAT_INCR(sctps_noport);
5786a8775ad9SMichael Tuexen 		if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
5787a8775ad9SMichael Tuexen 			goto out;
5788a8775ad9SMichael Tuexen 		}
5789a8775ad9SMichael Tuexen 		if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5790a8775ad9SMichael Tuexen 			sctp_send_shutdown_complete2(src, dst, sh,
5791d089f9b9SMichael Tuexen 			    mflowtype, mflowid, fibnum,
5792a8775ad9SMichael Tuexen 			    vrf_id, port);
5793a8775ad9SMichael Tuexen 			goto out;
5794a8775ad9SMichael Tuexen 		}
5795a8775ad9SMichael Tuexen 		if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5796a8775ad9SMichael Tuexen 			goto out;
5797a8775ad9SMichael Tuexen 		}
5798a8775ad9SMichael Tuexen 		if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
5799a8775ad9SMichael Tuexen 			if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
5800a8775ad9SMichael Tuexen 			    ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
5801a8775ad9SMichael Tuexen 			    (ch->chunk_type != SCTP_INIT))) {
5802ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5803ff1ffd74SMichael Tuexen 				    "Out of the blue");
5804a8775ad9SMichael Tuexen 				sctp_send_abort(m, iphlen, src, dst,
5805ff1ffd74SMichael Tuexen 				    sh, 0, op_err,
5806d089f9b9SMichael Tuexen 				    mflowtype, mflowid, fibnum,
5807a8775ad9SMichael Tuexen 				    vrf_id, port);
5808a8775ad9SMichael Tuexen 			}
5809a8775ad9SMichael Tuexen 		}
5810a8775ad9SMichael Tuexen 		goto out;
5811a8775ad9SMichael Tuexen 	} else if (stcb == NULL) {
5812a8775ad9SMichael Tuexen 		inp_decr = inp;
5813a8775ad9SMichael Tuexen 	}
5814a8775ad9SMichael Tuexen #ifdef IPSEC
5815a8775ad9SMichael Tuexen 	/*-
5816a8775ad9SMichael Tuexen 	 * I very much doubt any of the IPSEC stuff will work but I have no
5817a8775ad9SMichael Tuexen 	 * idea, so I will leave it in place.
5818a8775ad9SMichael Tuexen 	 */
5819a8775ad9SMichael Tuexen 	if (inp != NULL) {
5820a8775ad9SMichael Tuexen 		switch (dst->sa_family) {
5821a8775ad9SMichael Tuexen #ifdef INET
5822a8775ad9SMichael Tuexen 		case AF_INET:
5823a8775ad9SMichael Tuexen 			if (ipsec4_in_reject(m, &inp->ip_inp.inp)) {
5824a8775ad9SMichael Tuexen 				SCTP_STAT_INCR(sctps_hdrops);
5825a8775ad9SMichael Tuexen 				goto out;
5826a8775ad9SMichael Tuexen 			}
5827a8775ad9SMichael Tuexen 			break;
5828a8775ad9SMichael Tuexen #endif
5829a8775ad9SMichael Tuexen #ifdef INET6
5830a8775ad9SMichael Tuexen 		case AF_INET6:
5831a8775ad9SMichael Tuexen 			if (ipsec6_in_reject(m, &inp->ip_inp.inp)) {
5832a8775ad9SMichael Tuexen 				SCTP_STAT_INCR(sctps_hdrops);
5833a8775ad9SMichael Tuexen 				goto out;
5834a8775ad9SMichael Tuexen 			}
5835a8775ad9SMichael Tuexen 			break;
5836a8775ad9SMichael Tuexen #endif
5837a8775ad9SMichael Tuexen 		default:
5838a8775ad9SMichael Tuexen 			break;
5839a8775ad9SMichael Tuexen 		}
5840a8775ad9SMichael Tuexen 	}
5841a8775ad9SMichael Tuexen #endif
5842b3f1ea41SRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5843dd294dceSMichael Tuexen 	    (void *)m, iphlen, offset, length, (void *)stcb);
5844f8829a4aSRandall Stewart 	if (stcb) {
5845f8829a4aSRandall Stewart 		/* always clear this before beginning a packet */
5846f8829a4aSRandall Stewart 		stcb->asoc.authenticated = 0;
5847f8829a4aSRandall Stewart 		stcb->asoc.seen_a_sack_this_pkt = 0;
58482afb3e84SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5849dd294dceSMichael Tuexen 		    (void *)stcb, stcb->asoc.state);
58502afb3e84SRandall Stewart 
5851c4739e2fSRandall Stewart 		if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5852c4739e2fSRandall Stewart 		    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
585363981c2bSRandall Stewart 			/*-
585463981c2bSRandall Stewart 			 * If we hit here, we had a ref count
585563981c2bSRandall Stewart 			 * up when the assoc was aborted and the
585663981c2bSRandall Stewart 			 * timer is clearing out the assoc, we should
585763981c2bSRandall Stewart 			 * NOT respond to any packet.. its OOTB.
585863981c2bSRandall Stewart 			 */
585963981c2bSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
5860a8775ad9SMichael Tuexen 			stcb = NULL;
58616e9c45e0SMichael Tuexen 			snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5862ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5863ff1ffd74SMichael Tuexen 			    msg);
5864ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5865d089f9b9SMichael Tuexen 			    mflowtype, mflowid, inp->fibnum,
5866c54a18d2SRandall Stewart 			    vrf_id, port);
5867a8775ad9SMichael Tuexen 			goto out;
586863981c2bSRandall Stewart 		}
5869f8829a4aSRandall Stewart 	}
5870f8829a4aSRandall Stewart 	if (IS_SCTP_CONTROL(ch)) {
5871f8829a4aSRandall Stewart 		/* process the control portion of the SCTP packet */
58723c503c28SRandall Stewart 		/* sa_ignore NO_NULL_CHK */
5873b1754ad1SMichael Tuexen 		stcb = sctp_process_control(m, iphlen, &offset, length,
5874b1754ad1SMichael Tuexen 		    src, dst, sh, ch,
5875f30ac432SMichael Tuexen 		    inp, stcb, &net, &fwd_tsn_seen,
5876d089f9b9SMichael Tuexen 		    mflowtype, mflowid, fibnum,
5877f30ac432SMichael Tuexen 		    vrf_id, port);
5878f8829a4aSRandall Stewart 		if (stcb) {
5879f8829a4aSRandall Stewart 			/*
5880f8829a4aSRandall Stewart 			 * This covers us if the cookie-echo was there and
5881f8829a4aSRandall Stewart 			 * it changes our INP.
5882f8829a4aSRandall Stewart 			 */
5883f8829a4aSRandall Stewart 			inp = stcb->sctp_ep;
58844474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
58853cf729a9SMichael Tuexen 			if ((ch->chunk_type != SCTP_INITIATION) &&
58863cf729a9SMichael Tuexen 			    (net != NULL) && (net->port != port)) {
5887b3f1ea41SRandall Stewart 				if (net->port == 0) {
58883cf729a9SMichael Tuexen 					/* UDP encapsulation turned on. */
58893cf729a9SMichael Tuexen 					net->mtu -= sizeof(struct udphdr);
58903cf729a9SMichael Tuexen 					if (stcb->asoc.smallest_mtu > net->mtu) {
58913cf729a9SMichael Tuexen 						sctp_pathmtu_adjustment(stcb, net->mtu);
58923cf729a9SMichael Tuexen 					}
58933cf729a9SMichael Tuexen 				} else if (port == 0) {
58943cf729a9SMichael Tuexen 					/* UDP encapsulation turned off. */
58953cf729a9SMichael Tuexen 					net->mtu += sizeof(struct udphdr);
58963cf729a9SMichael Tuexen 					/* XXX Update smallest_mtu */
5897b3f1ea41SRandall Stewart 				}
5898b3f1ea41SRandall Stewart 				net->port = port;
5899b3f1ea41SRandall Stewart 			}
59004474d71aSMichael Tuexen #endif
5901f8829a4aSRandall Stewart 		}
5902f8829a4aSRandall Stewart 	} else {
5903f8829a4aSRandall Stewart 		/*
5904f8829a4aSRandall Stewart 		 * no control chunks, so pre-process DATA chunks (these
5905f8829a4aSRandall Stewart 		 * checks are taken care of by control processing)
5906f8829a4aSRandall Stewart 		 */
5907f8829a4aSRandall Stewart 
5908f8829a4aSRandall Stewart 		/*
5909f8829a4aSRandall Stewart 		 * if DATA only packet, and auth is required, then punt...
5910f8829a4aSRandall Stewart 		 * can't have authenticated without any AUTH (control)
5911f8829a4aSRandall Stewart 		 * chunks
5912f8829a4aSRandall Stewart 		 */
5913b3f1ea41SRandall Stewart 		if ((stcb != NULL) &&
5914c79bec9cSMichael Tuexen 		    (stcb->asoc.auth_supported == 1) &&
5915b3f1ea41SRandall Stewart 		    sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5916f8829a4aSRandall Stewart 			/* "silently" ignore */
5917f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvauthmissing);
5918a8775ad9SMichael Tuexen 			goto out;
5919f8829a4aSRandall Stewart 		}
5920f8829a4aSRandall Stewart 		if (stcb == NULL) {
5921f8829a4aSRandall Stewart 			/* out of the blue DATA chunk */
59226e9c45e0SMichael Tuexen 			snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5923ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5924ff1ffd74SMichael Tuexen 			    msg);
5925ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5926d089f9b9SMichael Tuexen 			    mflowtype, mflowid, fibnum,
5927c54a18d2SRandall Stewart 			    vrf_id, port);
5928a8775ad9SMichael Tuexen 			goto out;
5929f8829a4aSRandall Stewart 		}
5930f8829a4aSRandall Stewart 		if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5931f8829a4aSRandall Stewart 			/* v_tag mismatch! */
5932f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_badvtag);
5933a8775ad9SMichael Tuexen 			goto out;
5934f8829a4aSRandall Stewart 		}
5935f8829a4aSRandall Stewart 	}
5936f8829a4aSRandall Stewart 
5937f8829a4aSRandall Stewart 	if (stcb == NULL) {
5938f8829a4aSRandall Stewart 		/*
5939f8829a4aSRandall Stewart 		 * no valid TCB for this packet, or we found it's a bad
5940f8829a4aSRandall Stewart 		 * packet while processing control, or we're done with this
5941f8829a4aSRandall Stewart 		 * packet (done or skip rest of data), so we drop it...
5942f8829a4aSRandall Stewart 		 */
5943a8775ad9SMichael Tuexen 		goto out;
5944f8829a4aSRandall Stewart 	}
5945f8829a4aSRandall Stewart 	/*
5946f8829a4aSRandall Stewart 	 * DATA chunk processing
5947f8829a4aSRandall Stewart 	 */
5948f8829a4aSRandall Stewart 	/* plow through the data chunks while length > offset */
5949f8829a4aSRandall Stewart 
5950f8829a4aSRandall Stewart 	/*
5951f8829a4aSRandall Stewart 	 * Rest should be DATA only.  Check authentication state if AUTH for
5952f8829a4aSRandall Stewart 	 * DATA is required.
5953f8829a4aSRandall Stewart 	 */
5954b3f1ea41SRandall Stewart 	if ((length > offset) &&
5955b3f1ea41SRandall Stewart 	    (stcb != NULL) &&
5956c79bec9cSMichael Tuexen 	    (stcb->asoc.auth_supported == 1) &&
5957b3f1ea41SRandall Stewart 	    sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5958f8829a4aSRandall Stewart 	    !stcb->asoc.authenticated) {
5959f8829a4aSRandall Stewart 		/* "silently" ignore */
5960f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvauthmissing);
5961ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_AUTH1,
5962ad81507eSRandall Stewart 		    "Data chunk requires AUTH, skipped\n");
5963a5d547adSRandall Stewart 		goto trigger_send;
5964f8829a4aSRandall Stewart 	}
5965f8829a4aSRandall Stewart 	if (length > offset) {
5966f8829a4aSRandall Stewart 		int retval;
5967f8829a4aSRandall Stewart 
5968f8829a4aSRandall Stewart 		/*
5969f8829a4aSRandall Stewart 		 * First check to make sure our state is correct. We would
5970f8829a4aSRandall Stewart 		 * not get here unless we really did have a tag, so we don't
5971f8829a4aSRandall Stewart 		 * abort if this happens, just dump the chunk silently.
5972f8829a4aSRandall Stewart 		 */
5973f8829a4aSRandall Stewart 		switch (SCTP_GET_STATE(&stcb->asoc)) {
5974f8829a4aSRandall Stewart 		case SCTP_STATE_COOKIE_ECHOED:
5975f8829a4aSRandall Stewart 			/*
5976f8829a4aSRandall Stewart 			 * we consider data with valid tags in this state
5977f8829a4aSRandall Stewart 			 * shows us the cookie-ack was lost. Imply it was
5978f8829a4aSRandall Stewart 			 * there.
5979f8829a4aSRandall Stewart 			 */
5980b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5981c4739e2fSRandall Stewart 				sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5982c4739e2fSRandall Stewart 				    stcb->asoc.overall_error_count,
5983c4739e2fSRandall Stewart 				    0,
5984c4739e2fSRandall Stewart 				    SCTP_FROM_SCTP_INPUT,
5985c4739e2fSRandall Stewart 				    __LINE__);
5986c4739e2fSRandall Stewart 			}
5987f8829a4aSRandall Stewart 			stcb->asoc.overall_error_count = 0;
5988f8829a4aSRandall Stewart 			sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5989f8829a4aSRandall Stewart 			break;
5990f8829a4aSRandall Stewart 		case SCTP_STATE_COOKIE_WAIT:
5991f8829a4aSRandall Stewart 			/*
5992f8829a4aSRandall Stewart 			 * We consider OOTB any data sent during asoc setup.
5993f8829a4aSRandall Stewart 			 */
59946e9c45e0SMichael Tuexen 			snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5995ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5996ff1ffd74SMichael Tuexen 			    msg);
5997ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5998d089f9b9SMichael Tuexen 			    mflowtype, mflowid, inp->fibnum,
5999c54a18d2SRandall Stewart 			    vrf_id, port);
6000a8775ad9SMichael Tuexen 			goto out;
600152be287eSRandall Stewart 			/* sa_ignore NOTREACHED */
6002f8829a4aSRandall Stewart 			break;
6003f8829a4aSRandall Stewart 		case SCTP_STATE_EMPTY:	/* should not happen */
6004f8829a4aSRandall Stewart 		case SCTP_STATE_INUSE:	/* should not happen */
6005f8829a4aSRandall Stewart 		case SCTP_STATE_SHUTDOWN_RECEIVED:	/* This is a peer error */
6006f8829a4aSRandall Stewart 		case SCTP_STATE_SHUTDOWN_ACK_SENT:
6007f8829a4aSRandall Stewart 		default:
6008a8775ad9SMichael Tuexen 			goto out;
600952be287eSRandall Stewart 			/* sa_ignore NOTREACHED */
6010f8829a4aSRandall Stewart 			break;
6011f8829a4aSRandall Stewart 		case SCTP_STATE_OPEN:
6012f8829a4aSRandall Stewart 		case SCTP_STATE_SHUTDOWN_SENT:
6013f8829a4aSRandall Stewart 			break;
6014f8829a4aSRandall Stewart 		}
6015f8829a4aSRandall Stewart 		/* plow through the data chunks while length > offset */
6016b1754ad1SMichael Tuexen 		retval = sctp_process_data(mm, iphlen, &offset, length,
6017e7e71dd7SMichael Tuexen 		    inp, stcb, net, &high_tsn);
6018f8829a4aSRandall Stewart 		if (retval == 2) {
6019f8829a4aSRandall Stewart 			/*
6020f8829a4aSRandall Stewart 			 * The association aborted, NO UNLOCK needed since
6021f8829a4aSRandall Stewart 			 * the association is destroyed.
6022f8829a4aSRandall Stewart 			 */
6023a8775ad9SMichael Tuexen 			stcb = NULL;
6024a8775ad9SMichael Tuexen 			goto out;
6025f8829a4aSRandall Stewart 		}
6026f8829a4aSRandall Stewart 		data_processed = 1;
6027f8829a4aSRandall Stewart 		/*
6028f8829a4aSRandall Stewart 		 * Anything important needs to have been m_copy'ed in
6029f8829a4aSRandall Stewart 		 * process_data
6030f8829a4aSRandall Stewart 		 */
6031f8829a4aSRandall Stewart 	}
6032493d8e5aSRandall Stewart 	/* take care of ecn */
603360990c0cSMichael Tuexen 	if ((data_processed == 1) &&
6034f342355aSMichael Tuexen 	    (stcb->asoc.ecn_supported == 1) &&
6035c446091bSMichael Tuexen 	    ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
6036493d8e5aSRandall Stewart 		/* Yep, we need to add a ECNE */
6037493d8e5aSRandall Stewart 		sctp_send_ecn_echo(stcb, net, high_tsn);
6038493d8e5aSRandall Stewart 	}
6039f8829a4aSRandall Stewart 	if ((data_processed == 0) && (fwd_tsn_seen)) {
60408f777478SMichael Tuexen 		int was_a_gap;
60418f777478SMichael Tuexen 		uint32_t highest_tsn;
6042f8829a4aSRandall Stewart 
604320b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
60448f777478SMichael Tuexen 			highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
60458f777478SMichael Tuexen 		} else {
60468f777478SMichael Tuexen 			highest_tsn = stcb->asoc.highest_tsn_inside_map;
6047f8829a4aSRandall Stewart 		}
604820b07a4dSMichael Tuexen 		was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
60498933fa13SRandall Stewart 		stcb->asoc.send_sack = 1;
60507215cc1bSMichael Tuexen 		sctp_sack_check(stcb, was_a_gap);
60518933fa13SRandall Stewart 	} else if (fwd_tsn_seen) {
60528933fa13SRandall Stewart 		stcb->asoc.send_sack = 1;
6053f8829a4aSRandall Stewart 	}
6054f8829a4aSRandall Stewart 	/* trigger send of any chunks in queue... */
6055a5d547adSRandall Stewart trigger_send:
6056f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
6057f8829a4aSRandall Stewart 	sctp_audit_log(0xE0, 2);
6058f8829a4aSRandall Stewart 	sctp_auditing(1, inp, stcb, net);
6059f8829a4aSRandall Stewart #endif
6060ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT1,
6061ad81507eSRandall Stewart 	    "Check for chunk output prw:%d tqe:%d tf=%d\n",
6062f8829a4aSRandall Stewart 	    stcb->asoc.peers_rwnd,
6063f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&stcb->asoc.control_send_queue),
6064f8829a4aSRandall Stewart 	    stcb->asoc.total_flight);
6065f8829a4aSRandall Stewart 	un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
6066493d8e5aSRandall Stewart 	if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
6067493d8e5aSRandall Stewart 		cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
6068493d8e5aSRandall Stewart 	}
60695114dccbSMichael Tuexen 	if (!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue) ||
60705114dccbSMichael Tuexen 	    cnt_ctrl_ready ||
60715114dccbSMichael Tuexen 	    stcb->asoc.trigger_reset ||
6072f8829a4aSRandall Stewart 	    ((un_sent) &&
6073f8829a4aSRandall Stewart 	    (stcb->asoc.peers_rwnd > 0 ||
6074f8829a4aSRandall Stewart 	    (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
6075ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
6076ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
6077ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
6078f8829a4aSRandall Stewart 	}
6079f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
6080f8829a4aSRandall Stewart 	sctp_audit_log(0xE0, 3);
6081f8829a4aSRandall Stewart 	sctp_auditing(2, inp, stcb, net);
6082f8829a4aSRandall Stewart #endif
6083a8775ad9SMichael Tuexen out:
6084a8775ad9SMichael Tuexen 	if (stcb != NULL) {
6085f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
6086a8775ad9SMichael Tuexen 	}
6087a8775ad9SMichael Tuexen 	if (inp_decr != NULL) {
6088a8775ad9SMichael Tuexen 		/* reduce ref-count */
6089a8775ad9SMichael Tuexen 		SCTP_INP_WLOCK(inp_decr);
6090a8775ad9SMichael Tuexen 		SCTP_INP_DECR_REF(inp_decr);
6091a8775ad9SMichael Tuexen 		SCTP_INP_WUNLOCK(inp_decr);
6092a8775ad9SMichael Tuexen 	}
60936e55db54SRandall Stewart 	return;
6094f8829a4aSRandall Stewart }
6095f8829a4aSRandall Stewart 
6096e6194c2eSMichael Tuexen #ifdef INET
6097f8829a4aSRandall Stewart void
6098af83f5d7SRoman Divacky sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
6099f8829a4aSRandall Stewart {
6100139bc87fSRandall Stewart 	struct mbuf *m;
6101f8829a4aSRandall Stewart 	int iphlen;
6102ad21a364SRandall Stewart 	uint32_t vrf_id = 0;
6103f8829a4aSRandall Stewart 	uint8_t ecn_bits;
6104b1754ad1SMichael Tuexen 	struct sockaddr_in src, dst;
6105f8829a4aSRandall Stewart 	struct ip *ip;
6106f8829a4aSRandall Stewart 	struct sctphdr *sh;
6107f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
61086dc5aabcSMichael Tuexen 	int length, offset;
6109f8829a4aSRandall Stewart 
61109c7635e1SMichael Tuexen #if !defined(SCTP_WITH_NO_CSUM)
6111a8775ad9SMichael Tuexen 	uint8_t compute_crc;
61129c7635e1SMichael Tuexen 
61139c7635e1SMichael Tuexen #endif
6114a8775ad9SMichael Tuexen 	uint32_t mflowid;
6115457b4b88SMichael Tuexen 	uint8_t mflowtype;
6116d089f9b9SMichael Tuexen 	uint16_t fibnum;
61179c7635e1SMichael Tuexen 
61186dc5aabcSMichael Tuexen 	iphlen = off;
611917205eccSRandall Stewart 	if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
612017205eccSRandall Stewart 		SCTP_RELEASE_PKT(i_pak);
612117205eccSRandall Stewart 		return;
612217205eccSRandall Stewart 	}
6123139bc87fSRandall Stewart 	m = SCTP_HEADER_TO_CHAIN(i_pak);
6124f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
6125f8829a4aSRandall Stewart 	/* Log in any input mbufs */
6126b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
61274be807c4SMichael Tuexen 		sctp_log_mbc(m, SCTP_MBUF_INPUT);
612880fefe0aSRandall Stewart 	}
6129f8829a4aSRandall Stewart #endif
6130207304d4SRandall Stewart #ifdef SCTP_PACKET_LOGGING
61316dc5aabcSMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
6132f9384252SMichael Tuexen 		sctp_packet_log(m);
61336dc5aabcSMichael Tuexen 	}
6134207304d4SRandall Stewart #endif
6135a8775ad9SMichael Tuexen 	SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
61361a94cdbeSMichael Tuexen 	    "sctp_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
6137a8775ad9SMichael Tuexen 	    m->m_pkthdr.len,
6138a8775ad9SMichael Tuexen 	    if_name(m->m_pkthdr.rcvif),
61391a94cdbeSMichael Tuexen 	    (int)m->m_pkthdr.csum_flags, CSUM_BITS);
6140f30ac432SMichael Tuexen 	mflowid = m->m_pkthdr.flowid;
6141457b4b88SMichael Tuexen 	mflowtype = M_HASHTYPE_GET(m);
6142d089f9b9SMichael Tuexen 	fibnum = M_GETFIB(m);
61436dc5aabcSMichael Tuexen 	SCTP_STAT_INCR(sctps_recvpackets);
61446dc5aabcSMichael Tuexen 	SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
61456dc5aabcSMichael Tuexen 	/* Get IP, SCTP, and first chunk header together in the first mbuf. */
61466dc5aabcSMichael Tuexen 	offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6147139bc87fSRandall Stewart 	if (SCTP_BUF_LEN(m) < offset) {
6148b1754ad1SMichael Tuexen 		if ((m = m_pullup(m, offset)) == NULL) {
6149f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_hdrops);
6150f8829a4aSRandall Stewart 			return;
6151f8829a4aSRandall Stewart 		}
6152f8829a4aSRandall Stewart 	}
6153b1754ad1SMichael Tuexen 	ip = mtod(m, struct ip *);
61546dc5aabcSMichael Tuexen 	sh = (struct sctphdr *)((caddr_t)ip + iphlen);
61556dc5aabcSMichael Tuexen 	ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
61566dc5aabcSMichael Tuexen 	offset -= sizeof(struct sctp_chunkhdr);
6157b1754ad1SMichael Tuexen 	memset(&src, 0, sizeof(struct sockaddr_in));
6158b1754ad1SMichael Tuexen 	src.sin_family = AF_INET;
6159b1754ad1SMichael Tuexen 	src.sin_len = sizeof(struct sockaddr_in);
6160b1754ad1SMichael Tuexen 	src.sin_port = sh->src_port;
6161b1754ad1SMichael Tuexen 	src.sin_addr = ip->ip_src;
6162b1754ad1SMichael Tuexen 	memset(&dst, 0, sizeof(struct sockaddr_in));
6163b1754ad1SMichael Tuexen 	dst.sin_family = AF_INET;
6164b1754ad1SMichael Tuexen 	dst.sin_len = sizeof(struct sockaddr_in);
6165b1754ad1SMichael Tuexen 	dst.sin_port = sh->dest_port;
6166b1754ad1SMichael Tuexen 	dst.sin_addr = ip->ip_dst;
61678ad458a4SGleb Smirnoff 	length = ntohs(ip->ip_len);
61686dc5aabcSMichael Tuexen 	/* Validate mbuf chain length with IP payload length. */
6169a8775ad9SMichael Tuexen 	if (SCTP_HEADER_LEN(m) != length) {
61706dc5aabcSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT1,
6171a8775ad9SMichael Tuexen 		    "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
6172a99b6783SRandall Stewart 		SCTP_STAT_INCR(sctps_hdrops);
6173a8775ad9SMichael Tuexen 		goto out;
6174a99b6783SRandall Stewart 	}
6175f8829a4aSRandall Stewart 	/* SCTP does not allow broadcasts or multicasts */
6176b1754ad1SMichael Tuexen 	if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
6177a8775ad9SMichael Tuexen 		goto out;
6178f8829a4aSRandall Stewart 	}
6179b1754ad1SMichael Tuexen 	if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
6180a8775ad9SMichael Tuexen 		goto out;
6181f8829a4aSRandall Stewart 	}
6182a8775ad9SMichael Tuexen 	ecn_bits = ip->ip_tos;
61839c7635e1SMichael Tuexen #if defined(SCTP_WITH_NO_CSUM)
61849c7635e1SMichael Tuexen 	SCTP_STAT_INCR(sctps_recvnocrc);
61859c7635e1SMichael Tuexen #else
6186a99b6783SRandall Stewart 	if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
6187a99b6783SRandall Stewart 		SCTP_STAT_INCR(sctps_recvhwcrc);
6188a8775ad9SMichael Tuexen 		compute_crc = 0;
6189a8775ad9SMichael Tuexen 	} else {
6190a99b6783SRandall Stewart 		SCTP_STAT_INCR(sctps_recvswcrc);
6191a8775ad9SMichael Tuexen 		compute_crc = 1;
6192f8829a4aSRandall Stewart 	}
61936dc5aabcSMichael Tuexen #endif
6194b1754ad1SMichael Tuexen 	sctp_common_input_processing(&m, iphlen, offset, length,
6195b1754ad1SMichael Tuexen 	    (struct sockaddr *)&src,
6196b1754ad1SMichael Tuexen 	    (struct sockaddr *)&dst,
6197a8775ad9SMichael Tuexen 	    sh, ch,
6198a8775ad9SMichael Tuexen #if !defined(SCTP_WITH_NO_CSUM)
6199a8775ad9SMichael Tuexen 	    compute_crc,
6200a8775ad9SMichael Tuexen #endif
6201a8775ad9SMichael Tuexen 	    ecn_bits,
6202d089f9b9SMichael Tuexen 	    mflowtype, mflowid, fibnum,
6203f30ac432SMichael Tuexen 	    vrf_id, port);
6204a8775ad9SMichael Tuexen out:
6205f8829a4aSRandall Stewart 	if (m) {
6206f8829a4aSRandall Stewart 		sctp_m_freem(m);
6207f8829a4aSRandall Stewart 	}
6208f8829a4aSRandall Stewart 	return;
6209f8829a4aSRandall Stewart }
6210bfc46083SRandall Stewart 
62110071ee5eSRandall Stewart #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
62120071ee5eSRandall Stewart extern int *sctp_cpuarry;
62130071ee5eSRandall Stewart 
62140071ee5eSRandall Stewart #endif
6215bfc46083SRandall Stewart 
62168f5a8818SKevin Lo int
621782eaf95eSMichael Tuexen sctp_input(struct mbuf **mp, int *offp, int proto SCTP_UNUSED)
6218c54a18d2SRandall Stewart {
62198f5a8818SKevin Lo 	struct mbuf *m;
62208f5a8818SKevin Lo 	int off;
62218f5a8818SKevin Lo 
62228f5a8818SKevin Lo 	m = *mp;
62238f5a8818SKevin Lo 	off = *offp;
6224bfc46083SRandall Stewart #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
622582eaf95eSMichael Tuexen 	if (mp_ncpus > 1) {
6226bfc46083SRandall Stewart 		struct ip *ip;
6227bfc46083SRandall Stewart 		struct sctphdr *sh;
6228bfc46083SRandall Stewart 		int offset;
6229bfc46083SRandall Stewart 		int cpu_to_use;
623038521fb9SRandall Stewart 		uint32_t flowid, tag;
6231bfc46083SRandall Stewart 
6232457b4b88SMichael Tuexen 		if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
623338521fb9SRandall Stewart 			flowid = m->m_pkthdr.flowid;
623438521fb9SRandall Stewart 		} else {
623538521fb9SRandall Stewart 			/*
623638521fb9SRandall Stewart 			 * No flow id built by lower layers fix it so we
623738521fb9SRandall Stewart 			 * create one.
623838521fb9SRandall Stewart 			 */
6239b1754ad1SMichael Tuexen 			offset = off + sizeof(struct sctphdr);
6240bfc46083SRandall Stewart 			if (SCTP_BUF_LEN(m) < offset) {
6241b1754ad1SMichael Tuexen 				if ((m = m_pullup(m, offset)) == NULL) {
6242bfc46083SRandall Stewart 					SCTP_STAT_INCR(sctps_hdrops);
62438f5a8818SKevin Lo 					return (IPPROTO_DONE);
6244bfc46083SRandall Stewart 				}
6245bfc46083SRandall Stewart 			}
6246b1754ad1SMichael Tuexen 			ip = mtod(m, struct ip *);
6247bfc46083SRandall Stewart 			sh = (struct sctphdr *)((caddr_t)ip + off);
62480071ee5eSRandall Stewart 			tag = htonl(sh->v_tag);
624938521fb9SRandall Stewart 			flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
625038521fb9SRandall Stewart 			m->m_pkthdr.flowid = flowid;
6251457b4b88SMichael Tuexen 			M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE);
62520071ee5eSRandall Stewart 		}
625338521fb9SRandall Stewart 		cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
6254bfc46083SRandall Stewart 		sctp_queue_to_mcore(m, off, cpu_to_use);
62558f5a8818SKevin Lo 		return (IPPROTO_DONE);
6256bfc46083SRandall Stewart 	}
6257bfc46083SRandall Stewart #endif
6258bfc46083SRandall Stewart 	sctp_input_with_port(m, off, 0);
62598f5a8818SKevin Lo 	return (IPPROTO_DONE);
6260c54a18d2SRandall Stewart }
6261e6194c2eSMichael Tuexen 
6262e6194c2eSMichael Tuexen #endif
6263