xref: /freebsd/sys/netinet/sctp_input.c (revision 5114dccbd4bc5050a554c4a8633882b241e30bcb)
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 */
3894a9ef3f8SMichael Tuexen 		struct sctp_queued_to_read *ctl, *nctl;
3906a91f103SRandall Stewart 
3916a91f103SRandall Stewart 		for (i = 0; i < asoc->streamincnt; i++) {
3924a9ef3f8SMichael Tuexen 			TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) {
3936a91f103SRandall Stewart 				TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next);
3946a91f103SRandall Stewart 				sctp_free_remote_addr(ctl->whoFrom);
3955bead436SRandall Stewart 				ctl->whoFrom = NULL;
3966a91f103SRandall Stewart 				sctp_m_freem(ctl->data);
3976a91f103SRandall Stewart 				ctl->data = NULL;
3986a91f103SRandall Stewart 				sctp_free_a_readq(stcb, ctl);
3996a91f103SRandall Stewart 			}
4006a91f103SRandall Stewart 		}
401207304d4SRandall Stewart 		SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
402f8829a4aSRandall Stewart 	}
403ee1ccd92SMichael Tuexen 	if (asoc->max_inbound_streams > ntohs(init->num_outbound_streams)) {
4046a91f103SRandall Stewart 		asoc->streamincnt = ntohs(init->num_outbound_streams);
405ee1ccd92SMichael Tuexen 	} else {
406ee1ccd92SMichael Tuexen 		asoc->streamincnt = asoc->max_inbound_streams;
4076a91f103SRandall Stewart 	}
408f8829a4aSRandall Stewart 	SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
409207304d4SRandall Stewart 	    sizeof(struct sctp_stream_in), SCTP_M_STRMI);
410f8829a4aSRandall Stewart 	if (asoc->strmin == NULL) {
411f8829a4aSRandall Stewart 		/* we didn't get memory for the streams! */
412ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
413f8829a4aSRandall Stewart 		return (-1);
414f8829a4aSRandall Stewart 	}
415f8829a4aSRandall Stewart 	for (i = 0; i < asoc->streamincnt; i++) {
416f8829a4aSRandall Stewart 		asoc->strmin[i].stream_no = i;
417f8829a4aSRandall Stewart 		asoc->strmin[i].last_sequence_delivered = 0xffff;
418f8829a4aSRandall Stewart 		TAILQ_INIT(&asoc->strmin[i].inqueue);
4199a6142d8SRandall Stewart 		asoc->strmin[i].delivery_started = 0;
420f8829a4aSRandall Stewart 	}
421f8829a4aSRandall Stewart 	/*
422f8829a4aSRandall Stewart 	 * load_address_from_init will put the addresses into the
423f8829a4aSRandall Stewart 	 * association when the COOKIE is processed or the INIT-ACK is
424f8829a4aSRandall Stewart 	 * processed. Both types of COOKIE's existing and new call this
425f8829a4aSRandall Stewart 	 * routine. It will remove addresses that are no longer in the
426f8829a4aSRandall Stewart 	 * association (for the restarting case where addresses are
427f8829a4aSRandall Stewart 	 * removed). Up front when the INIT arrives we will discard it if it
428f8829a4aSRandall Stewart 	 * is a restart and new addresses have been added.
429f8829a4aSRandall Stewart 	 */
4303c503c28SRandall Stewart 	/* sa_ignore MEMLEAK */
431f8829a4aSRandall Stewart 	return (0);
432f8829a4aSRandall Stewart }
433f8829a4aSRandall Stewart 
434f8829a4aSRandall Stewart /*
435f8829a4aSRandall Stewart  * INIT-ACK message processing/consumption returns value < 0 on error
436f8829a4aSRandall Stewart  */
437f8829a4aSRandall Stewart static int
438b1754ad1SMichael Tuexen sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
439b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
440f30ac432SMichael Tuexen     struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
441f30ac432SMichael Tuexen     struct sctp_nets *net, int *abort_no_unlock,
442457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
443f30ac432SMichael Tuexen     uint32_t vrf_id)
444f8829a4aSRandall Stewart {
445f8829a4aSRandall Stewart 	struct sctp_association *asoc;
446f8829a4aSRandall Stewart 	struct mbuf *op_err;
447f8829a4aSRandall Stewart 	int retval, abort_flag;
448f8829a4aSRandall Stewart 	uint32_t initack_limit;
449830d754dSRandall Stewart 	int nat_friendly = 0;
450f8829a4aSRandall Stewart 
451f8829a4aSRandall Stewart 	/* First verify that we have no illegal param's */
452f8829a4aSRandall Stewart 	abort_flag = 0;
453f8829a4aSRandall Stewart 
454f8829a4aSRandall Stewart 	op_err = sctp_arethere_unrecognized_parameters(m,
455f8829a4aSRandall Stewart 	    (offset + sizeof(struct sctp_init_chunk)),
456830d754dSRandall Stewart 	    &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly);
457f8829a4aSRandall Stewart 	if (abort_flag) {
458f8829a4aSRandall Stewart 		/* Send an abort and notify peer */
459a2b42326SMichael Tuexen 		sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
460bff64a4dSRandall Stewart 		*abort_no_unlock = 1;
461f8829a4aSRandall Stewart 		return (-1);
462f8829a4aSRandall Stewart 	}
463f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
464830d754dSRandall Stewart 	asoc->peer_supports_nat = (uint8_t) nat_friendly;
465f8829a4aSRandall Stewart 	/* process the peer's parameters in the INIT-ACK */
4667215cc1bSMichael Tuexen 	retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb);
467f8829a4aSRandall Stewart 	if (retval < 0) {
468f8829a4aSRandall Stewart 		return (retval);
469f8829a4aSRandall Stewart 	}
470f8829a4aSRandall Stewart 	initack_limit = offset + ntohs(cp->ch.chunk_length);
471f8829a4aSRandall Stewart 	/* load all addresses */
4727215cc1bSMichael Tuexen 	if ((retval = sctp_load_addresses_from_init(stcb, m,
473b1754ad1SMichael Tuexen 	    (offset + sizeof(struct sctp_init_chunk)), initack_limit,
474b1754ad1SMichael Tuexen 	    src, dst, NULL))) {
475ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
476ff1ffd74SMichael Tuexen 		    "Problem with address parameters");
477ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1,
478ad81507eSRandall Stewart 		    "Load addresses from INIT causes an abort %d\n",
479ad81507eSRandall Stewart 		    retval);
480b1754ad1SMichael Tuexen 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
481ff1ffd74SMichael Tuexen 		    src, dst, sh, op_err,
482457b4b88SMichael Tuexen 		    mflowtype, mflowid,
483f30ac432SMichael Tuexen 		    vrf_id, net->port);
484bff64a4dSRandall Stewart 		*abort_no_unlock = 1;
485f8829a4aSRandall Stewart 		return (-1);
486f8829a4aSRandall Stewart 	}
48718e198d3SRandall Stewart 	/* if the peer doesn't support asconf, flush the asconf queue */
488c79bec9cSMichael Tuexen 	if (asoc->asconf_supported == 0) {
4894a9ef3f8SMichael Tuexen 		struct sctp_asconf_addr *param, *nparam;
49018e198d3SRandall Stewart 
4914a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
4924a9ef3f8SMichael Tuexen 			TAILQ_REMOVE(&asoc->asconf_queue, param, next);
4934a9ef3f8SMichael Tuexen 			SCTP_FREE(param, SCTP_M_ASC_ADDR);
49418e198d3SRandall Stewart 		}
49518e198d3SRandall Stewart 	}
496f8829a4aSRandall Stewart 	stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
497f8829a4aSRandall Stewart 	    stcb->asoc.local_hmacs);
498f8829a4aSRandall Stewart 	if (op_err) {
499f8829a4aSRandall Stewart 		sctp_queue_op_err(stcb, op_err);
500f8829a4aSRandall Stewart 		/* queuing will steal away the mbuf chain to the out queue */
501f8829a4aSRandall Stewart 		op_err = NULL;
502f8829a4aSRandall Stewart 	}
503f8829a4aSRandall Stewart 	/* extract the cookie and queue it to "echo" it back... */
504b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
505c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
506c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
507c4739e2fSRandall Stewart 		    0,
508c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INPUT,
509c4739e2fSRandall Stewart 		    __LINE__);
510c4739e2fSRandall Stewart 	}
511f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
512f8829a4aSRandall Stewart 	net->error_count = 0;
513f8829a4aSRandall Stewart 
514f8829a4aSRandall Stewart 	/*
515f8829a4aSRandall Stewart 	 * Cancel the INIT timer, We do this first before queueing the
516f8829a4aSRandall Stewart 	 * cookie. We always cancel at the primary to assue that we are
517f8829a4aSRandall Stewart 	 * canceling the timer started by the INIT which always goes to the
518f8829a4aSRandall Stewart 	 * primary.
519f8829a4aSRandall Stewart 	 */
520f8829a4aSRandall Stewart 	sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
521b7d130beSMichael Tuexen 	    asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
522a5d547adSRandall Stewart 
523a5d547adSRandall Stewart 	/* calculate the RTO */
524899288aeSRandall Stewart 	net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered, sctp_align_safe_nocopy,
525f79aab18SRandall Stewart 	    SCTP_RTT_FROM_NON_DATA);
526f8829a4aSRandall Stewart 	retval = sctp_send_cookie_echo(m, offset, stcb, net);
527f8829a4aSRandall Stewart 	if (retval < 0) {
528f8829a4aSRandall Stewart 		/*
529f8829a4aSRandall Stewart 		 * No cookie, we probably should send a op error. But in any
530f8829a4aSRandall Stewart 		 * case if there is no cookie in the INIT-ACK, we can
531f8829a4aSRandall Stewart 		 * abandon the peer, its broke.
532f8829a4aSRandall Stewart 		 */
533f8829a4aSRandall Stewart 		if (retval == -3) {
5346802b090SMichael Tuexen 			uint16_t len;
535f8829a4aSRandall Stewart 
5366802b090SMichael Tuexen 			len = (uint16_t) (sizeof(struct sctp_error_missing_param) + sizeof(uint16_t));
53786eda749SMichael Tuexen 			/* We abort with an error of missing mandatory param */
53886eda749SMichael Tuexen 			op_err = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
53986eda749SMichael Tuexen 			if (op_err != NULL) {
54086eda749SMichael Tuexen 				struct sctp_error_missing_param *cause;
54186eda749SMichael Tuexen 
54286eda749SMichael Tuexen 				SCTP_BUF_LEN(op_err) = len;
54386eda749SMichael Tuexen 				cause = mtod(op_err, struct sctp_error_missing_param *);
544f8829a4aSRandall Stewart 				/* Subtract the reserved param */
54586eda749SMichael Tuexen 				cause->cause.code = htons(SCTP_CAUSE_MISSING_PARAM);
54686eda749SMichael Tuexen 				cause->cause.length = htons(len);
54786eda749SMichael Tuexen 				cause->num_missing_params = htonl(1);
54886eda749SMichael Tuexen 				cause->type[0] = htons(SCTP_STATE_COOKIE);
549f8829a4aSRandall Stewart 			}
550f8829a4aSRandall Stewart 			sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
551b1754ad1SMichael Tuexen 			    src, dst, sh, op_err,
552457b4b88SMichael Tuexen 			    mflowtype, mflowid,
553f30ac432SMichael Tuexen 			    vrf_id, net->port);
554bff64a4dSRandall Stewart 			*abort_no_unlock = 1;
555f8829a4aSRandall Stewart 		}
556f8829a4aSRandall Stewart 		return (retval);
557f8829a4aSRandall Stewart 	}
558f8829a4aSRandall Stewart 	return (0);
559f8829a4aSRandall Stewart }
560f8829a4aSRandall Stewart 
561f8829a4aSRandall Stewart static void
562f8829a4aSRandall Stewart sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
563f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net)
564f8829a4aSRandall Stewart {
56524aaac8dSMichael Tuexen 	union sctp_sockstore store;
56652129fcdSRandall Stewart 	struct sctp_nets *r_net, *f_net;
567f8829a4aSRandall Stewart 	struct timeval tv;
568d55b0b1bSRandall Stewart 	int req_prim = 0;
569ca85e948SMichael Tuexen 	uint16_t old_error_counter;
570f8829a4aSRandall Stewart 
571f8829a4aSRandall Stewart 	if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
572f8829a4aSRandall Stewart 		/* Invalid length */
573f8829a4aSRandall Stewart 		return;
574f8829a4aSRandall Stewart 	}
575f8829a4aSRandall Stewart 	memset(&store, 0, sizeof(store));
576e6194c2eSMichael Tuexen 	switch (cp->heartbeat.hb_info.addr_family) {
577e6194c2eSMichael Tuexen #ifdef INET
578e6194c2eSMichael Tuexen 	case AF_INET:
579e6194c2eSMichael Tuexen 		if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
58024aaac8dSMichael Tuexen 			store.sin.sin_family = cp->heartbeat.hb_info.addr_family;
58124aaac8dSMichael Tuexen 			store.sin.sin_len = cp->heartbeat.hb_info.addr_len;
58224aaac8dSMichael Tuexen 			store.sin.sin_port = stcb->rport;
58324aaac8dSMichael Tuexen 			memcpy(&store.sin.sin_addr, cp->heartbeat.hb_info.address,
58424aaac8dSMichael Tuexen 			    sizeof(store.sin.sin_addr));
585e6194c2eSMichael Tuexen 		} else {
586e6194c2eSMichael Tuexen 			return;
587e6194c2eSMichael Tuexen 		}
588e6194c2eSMichael Tuexen 		break;
589e6194c2eSMichael Tuexen #endif
590e6194c2eSMichael Tuexen #ifdef INET6
591e6194c2eSMichael Tuexen 	case AF_INET6:
592e6194c2eSMichael Tuexen 		if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
59324aaac8dSMichael Tuexen 			store.sin6.sin6_family = cp->heartbeat.hb_info.addr_family;
59424aaac8dSMichael Tuexen 			store.sin6.sin6_len = cp->heartbeat.hb_info.addr_len;
59524aaac8dSMichael Tuexen 			store.sin6.sin6_port = stcb->rport;
59623602b60SMichael Tuexen 			memcpy(&store.sin6.sin6_addr, cp->heartbeat.hb_info.address, sizeof(struct in6_addr));
597f8829a4aSRandall Stewart 		} else {
598f8829a4aSRandall Stewart 			return;
599f8829a4aSRandall Stewart 		}
600e6194c2eSMichael Tuexen 		break;
601e6194c2eSMichael Tuexen #endif
602e6194c2eSMichael Tuexen 	default:
603e6194c2eSMichael Tuexen 		return;
604e6194c2eSMichael Tuexen 	}
60524aaac8dSMichael Tuexen 	r_net = sctp_findnet(stcb, &store.sa);
606f8829a4aSRandall Stewart 	if (r_net == NULL) {
607ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
608f8829a4aSRandall Stewart 		return;
609f8829a4aSRandall Stewart 	}
610f8829a4aSRandall Stewart 	if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
611f8829a4aSRandall Stewart 	    (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
612f8829a4aSRandall Stewart 	    (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
613f8829a4aSRandall Stewart 		/*
614f8829a4aSRandall Stewart 		 * If the its a HB and it's random value is correct when can
615f8829a4aSRandall Stewart 		 * confirm the destination.
616f8829a4aSRandall Stewart 		 */
617f8829a4aSRandall Stewart 		r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
61842551e99SRandall Stewart 		if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
61942551e99SRandall Stewart 			stcb->asoc.primary_destination = r_net;
62042551e99SRandall Stewart 			r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
62152129fcdSRandall Stewart 			f_net = TAILQ_FIRST(&stcb->asoc.nets);
62252129fcdSRandall Stewart 			if (f_net != r_net) {
62342551e99SRandall Stewart 				/*
62442551e99SRandall Stewart 				 * first one on the list is NOT the primary
62542551e99SRandall Stewart 				 * sctp_cmpaddr() is much more efficent if
62642551e99SRandall Stewart 				 * the primary is the first on the list,
62742551e99SRandall Stewart 				 * make it so.
62842551e99SRandall Stewart 				 */
62952129fcdSRandall Stewart 				TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
63052129fcdSRandall Stewart 				TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
63142551e99SRandall Stewart 			}
632d55b0b1bSRandall Stewart 			req_prim = 1;
63342551e99SRandall Stewart 		}
634f8829a4aSRandall Stewart 		sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
635ceaad40aSRandall Stewart 		    stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
636b7d130beSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb,
637b7d130beSMichael Tuexen 		    r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
638ca85e948SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
639f8829a4aSRandall Stewart 	}
640ca85e948SMichael Tuexen 	old_error_counter = r_net->error_count;
641f8829a4aSRandall Stewart 	r_net->error_count = 0;
642f8829a4aSRandall Stewart 	r_net->hb_responded = 1;
643f8829a4aSRandall Stewart 	tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
644f8829a4aSRandall Stewart 	tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
645f8829a4aSRandall Stewart 	/* Now lets do a RTO with this */
646899288aeSRandall Stewart 	r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv, sctp_align_safe_nocopy,
647f79aab18SRandall Stewart 	    SCTP_RTT_FROM_NON_DATA);
648ca85e948SMichael Tuexen 	if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) {
649ca85e948SMichael Tuexen 		r_net->dest_state |= SCTP_ADDR_REACHABLE;
650ca85e948SMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
6514b1f78e1SMichael Tuexen 		    0, (void *)r_net, SCTP_SO_NOT_LOCKED);
652ca85e948SMichael Tuexen 	}
653ca85e948SMichael Tuexen 	if (r_net->dest_state & SCTP_ADDR_PF) {
654ca85e948SMichael Tuexen 		r_net->dest_state &= ~SCTP_ADDR_PF;
655ca85e948SMichael Tuexen 		stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
656ca85e948SMichael Tuexen 	}
657ca85e948SMichael Tuexen 	if (old_error_counter > 0) {
658b7d130beSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
659b7d130beSMichael Tuexen 		    stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_5);
660ca85e948SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
661ca85e948SMichael Tuexen 	}
662ca85e948SMichael Tuexen 	if (r_net == stcb->asoc.primary_destination) {
663ca85e948SMichael Tuexen 		if (stcb->asoc.alternate) {
664ca85e948SMichael Tuexen 			/* release the alternate, primary is good */
665ca85e948SMichael Tuexen 			sctp_free_remote_addr(stcb->asoc.alternate);
666ca85e948SMichael Tuexen 			stcb->asoc.alternate = NULL;
667ca85e948SMichael Tuexen 		}
668ca85e948SMichael Tuexen 	}
669d55b0b1bSRandall Stewart 	/* Mobility adaptation */
670d55b0b1bSRandall Stewart 	if (req_prim) {
671d55b0b1bSRandall Stewart 		if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
672d55b0b1bSRandall Stewart 		    SCTP_MOBILITY_BASE) ||
673d55b0b1bSRandall Stewart 		    sctp_is_mobility_feature_on(stcb->sctp_ep,
674d55b0b1bSRandall Stewart 		    SCTP_MOBILITY_FASTHANDOFF)) &&
675d55b0b1bSRandall Stewart 		    sctp_is_mobility_feature_on(stcb->sctp_ep,
676d55b0b1bSRandall Stewart 		    SCTP_MOBILITY_PRIM_DELETED)) {
677d55b0b1bSRandall Stewart 
678b7d130beSMichael Tuexen 			sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED,
679b7d130beSMichael Tuexen 			    stcb->sctp_ep, stcb, NULL,
680b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
681d55b0b1bSRandall Stewart 			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
682d55b0b1bSRandall Stewart 			    SCTP_MOBILITY_FASTHANDOFF)) {
683d55b0b1bSRandall Stewart 				sctp_assoc_immediate_retrans(stcb,
684d55b0b1bSRandall Stewart 				    stcb->asoc.primary_destination);
685d55b0b1bSRandall Stewart 			}
686d55b0b1bSRandall Stewart 			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
687d55b0b1bSRandall Stewart 			    SCTP_MOBILITY_BASE)) {
6889eea4a2dSMichael Tuexen 				sctp_move_chunks_from_net(stcb,
6899eea4a2dSMichael Tuexen 				    stcb->asoc.deleted_primary);
690d55b0b1bSRandall Stewart 			}
691d55b0b1bSRandall Stewart 			sctp_delete_prim_timer(stcb->sctp_ep, stcb,
692d55b0b1bSRandall Stewart 			    stcb->asoc.deleted_primary);
693d55b0b1bSRandall Stewart 		}
694d55b0b1bSRandall Stewart 	}
695f8829a4aSRandall Stewart }
696f8829a4aSRandall Stewart 
697830d754dSRandall Stewart static int
698830d754dSRandall Stewart sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
699830d754dSRandall Stewart {
700830d754dSRandall Stewart 	/*
701830d754dSRandall Stewart 	 * return 0 means we want you to proceed with the abort non-zero
702830d754dSRandall Stewart 	 * means no abort processing
703830d754dSRandall Stewart 	 */
704830d754dSRandall Stewart 	struct sctpasochead *head;
705830d754dSRandall Stewart 
706830d754dSRandall Stewart 	if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
707830d754dSRandall Stewart 		/* generate a new vtag and send init */
708830d754dSRandall Stewart 		LIST_REMOVE(stcb, sctp_asocs);
709830d754dSRandall Stewart 		stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
710830d754dSRandall Stewart 		head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
711830d754dSRandall Stewart 		/*
712830d754dSRandall Stewart 		 * put it in the bucket in the vtag hash of assoc's for the
713830d754dSRandall Stewart 		 * system
714830d754dSRandall Stewart 		 */
715830d754dSRandall Stewart 		LIST_INSERT_HEAD(head, stcb, sctp_asocs);
716830d754dSRandall Stewart 		sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
717830d754dSRandall Stewart 		return (1);
718830d754dSRandall Stewart 	}
719830d754dSRandall Stewart 	if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
720830d754dSRandall Stewart 		/*
721830d754dSRandall Stewart 		 * treat like a case where the cookie expired i.e.: - dump
722830d754dSRandall Stewart 		 * current cookie. - generate a new vtag. - resend init.
723830d754dSRandall Stewart 		 */
724830d754dSRandall Stewart 		/* generate a new vtag and send init */
725830d754dSRandall Stewart 		LIST_REMOVE(stcb, sctp_asocs);
726830d754dSRandall Stewart 		stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED;
727830d754dSRandall Stewart 		stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT;
728830d754dSRandall Stewart 		sctp_stop_all_cookie_timers(stcb);
729830d754dSRandall Stewart 		sctp_toss_old_cookies(stcb, &stcb->asoc);
730830d754dSRandall Stewart 		stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
731830d754dSRandall Stewart 		head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
732830d754dSRandall Stewart 		/*
733830d754dSRandall Stewart 		 * put it in the bucket in the vtag hash of assoc's for the
734830d754dSRandall Stewart 		 * system
735830d754dSRandall Stewart 		 */
736830d754dSRandall Stewart 		LIST_INSERT_HEAD(head, stcb, sctp_asocs);
737830d754dSRandall Stewart 		sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
738830d754dSRandall Stewart 		return (1);
739830d754dSRandall Stewart 	}
740830d754dSRandall Stewart 	return (0);
741830d754dSRandall Stewart }
742830d754dSRandall Stewart 
743830d754dSRandall Stewart static int
744830d754dSRandall Stewart sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
745830d754dSRandall Stewart     struct sctp_nets *net)
746830d754dSRandall Stewart {
747830d754dSRandall Stewart 	/*
748830d754dSRandall Stewart 	 * return 0 means we want you to proceed with the abort non-zero
749830d754dSRandall Stewart 	 * means no abort processing
750830d754dSRandall Stewart 	 */
751c79bec9cSMichael Tuexen 	if (stcb->asoc.auth_supported == 0) {
752830d754dSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
753830d754dSRandall Stewart 		return (0);
754830d754dSRandall Stewart 	}
755830d754dSRandall Stewart 	sctp_asconf_send_nat_state_update(stcb, net);
756830d754dSRandall Stewart 	return (1);
757830d754dSRandall Stewart }
758830d754dSRandall Stewart 
759830d754dSRandall Stewart 
760f8829a4aSRandall Stewart static void
761a2b42326SMichael Tuexen sctp_handle_abort(struct sctp_abort_chunk *abort,
762f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net)
763f8829a4aSRandall Stewart {
764ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
765ceaad40aSRandall Stewart 	struct socket *so;
766ceaad40aSRandall Stewart 
767ceaad40aSRandall Stewart #endif
768830d754dSRandall Stewart 	uint16_t len;
769a2b42326SMichael Tuexen 	uint16_t error;
770ceaad40aSRandall Stewart 
771ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
772f8829a4aSRandall Stewart 	if (stcb == NULL)
773f8829a4aSRandall Stewart 		return;
774f8829a4aSRandall Stewart 
775a2b42326SMichael Tuexen 	len = ntohs(abort->ch.chunk_length);
776830d754dSRandall Stewart 	if (len > sizeof(struct sctp_chunkhdr)) {
777830d754dSRandall Stewart 		/*
778830d754dSRandall Stewart 		 * Need to check the cause codes for our two magic nat
779830d754dSRandall Stewart 		 * aborts which don't kill the assoc necessarily.
780830d754dSRandall Stewart 		 */
78186eda749SMichael Tuexen 		struct sctp_gen_error_cause *cause;
782830d754dSRandall Stewart 
78386eda749SMichael Tuexen 		cause = (struct sctp_gen_error_cause *)(abort + 1);
78486eda749SMichael Tuexen 		error = ntohs(cause->code);
785a2b42326SMichael Tuexen 		if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) {
786830d754dSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
787a2b42326SMichael Tuexen 			    abort->ch.chunk_flags);
788830d754dSRandall Stewart 			if (sctp_handle_nat_colliding_state(stcb)) {
789830d754dSRandall Stewart 				return;
790830d754dSRandall Stewart 			}
791a2b42326SMichael Tuexen 		} else if (error == SCTP_CAUSE_NAT_MISSING_STATE) {
792830d754dSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
793a2b42326SMichael Tuexen 			    abort->ch.chunk_flags);
794830d754dSRandall Stewart 			if (sctp_handle_nat_missing_state(stcb, net)) {
795830d754dSRandall Stewart 				return;
796830d754dSRandall Stewart 			}
797830d754dSRandall Stewart 		}
798a2b42326SMichael Tuexen 	} else {
799a2b42326SMichael Tuexen 		error = 0;
800830d754dSRandall Stewart 	}
801f8829a4aSRandall Stewart 	/* stop any receive timers */
802b7d130beSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net,
803b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_7);
804f8829a4aSRandall Stewart 	/* notify user of the abort and clean up... */
805410a3b1eSMichael Tuexen 	sctp_abort_notification(stcb, 1, error, abort, SCTP_SO_NOT_LOCKED);
806f8829a4aSRandall Stewart 	/* free the tcb */
807f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_aborted);
808f8829a4aSRandall Stewart 	if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
809f8829a4aSRandall Stewart 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
810f8829a4aSRandall Stewart 		SCTP_STAT_DECR_GAUGE32(sctps_currestab);
811f8829a4aSRandall Stewart 	}
812f1f73e57SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
813f1f73e57SRandall Stewart 	sctp_print_out_track_log(stcb);
814f1f73e57SRandall Stewart #endif
815ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
816ceaad40aSRandall Stewart 	so = SCTP_INP_SO(stcb->sctp_ep);
817ceaad40aSRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, 1);
818ceaad40aSRandall Stewart 	SCTP_TCB_UNLOCK(stcb);
819ceaad40aSRandall Stewart 	SCTP_SOCKET_LOCK(so, 1);
820ceaad40aSRandall Stewart 	SCTP_TCB_LOCK(stcb);
821ceaad40aSRandall Stewart 	atomic_subtract_int(&stcb->asoc.refcnt, 1);
822ceaad40aSRandall Stewart #endif
8232afb3e84SRandall Stewart 	stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
824c4739e2fSRandall Stewart 	(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
825b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
826ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
827ceaad40aSRandall Stewart 	SCTP_SOCKET_UNLOCK(so, 1);
828ceaad40aSRandall Stewart #endif
829ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
830f8829a4aSRandall Stewart }
831f8829a4aSRandall Stewart 
832f8829a4aSRandall Stewart static void
833ca85e948SMichael Tuexen sctp_start_net_timers(struct sctp_tcb *stcb)
834ca85e948SMichael Tuexen {
835ca85e948SMichael Tuexen 	uint32_t cnt_hb_sent;
836ca85e948SMichael Tuexen 	struct sctp_nets *net;
837ca85e948SMichael Tuexen 
838ca85e948SMichael Tuexen 	cnt_hb_sent = 0;
839ca85e948SMichael Tuexen 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
840ca85e948SMichael Tuexen 		/*
841ca85e948SMichael Tuexen 		 * For each network start: 1) A pmtu timer. 2) A HB timer 3)
842ca85e948SMichael Tuexen 		 * If the dest in unconfirmed send a hb as well if under
843ca85e948SMichael Tuexen 		 * max_hb_burst have been sent.
844ca85e948SMichael Tuexen 		 */
845ca85e948SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
846ca85e948SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
847ca85e948SMichael Tuexen 		if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
848ca85e948SMichael Tuexen 		    (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
849ca85e948SMichael Tuexen 			sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
850ca85e948SMichael Tuexen 			cnt_hb_sent++;
851ca85e948SMichael Tuexen 		}
852ca85e948SMichael Tuexen 	}
853ca85e948SMichael Tuexen 	if (cnt_hb_sent) {
854ca85e948SMichael Tuexen 		sctp_chunk_output(stcb->sctp_ep, stcb,
855ca85e948SMichael Tuexen 		    SCTP_OUTPUT_FROM_COOKIE_ACK,
856ca85e948SMichael Tuexen 		    SCTP_SO_NOT_LOCKED);
857ca85e948SMichael Tuexen 	}
858ca85e948SMichael Tuexen }
859ca85e948SMichael Tuexen 
860ca85e948SMichael Tuexen 
861ca85e948SMichael Tuexen static void
862f8829a4aSRandall Stewart sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
863f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
864f8829a4aSRandall Stewart {
865f8829a4aSRandall Stewart 	struct sctp_association *asoc;
866f8829a4aSRandall Stewart 	int some_on_streamwheel;
867aa1cfca9SMichael Tuexen 	int old_state;
868f8829a4aSRandall Stewart 
869ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
870ceaad40aSRandall Stewart 	struct socket *so;
871ceaad40aSRandall Stewart 
872ceaad40aSRandall Stewart #endif
873ceaad40aSRandall Stewart 
874ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
875ad81507eSRandall Stewart 	    "sctp_handle_shutdown: handling SHUTDOWN\n");
876f8829a4aSRandall Stewart 	if (stcb == NULL)
877f8829a4aSRandall Stewart 		return;
878a5d547adSRandall Stewart 	asoc = &stcb->asoc;
879a5d547adSRandall Stewart 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
880a5d547adSRandall Stewart 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
881f8829a4aSRandall Stewart 		return;
882f8829a4aSRandall Stewart 	}
883f8829a4aSRandall Stewart 	if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
884f8829a4aSRandall Stewart 		/* Shutdown NOT the expected size */
885f8829a4aSRandall Stewart 		return;
886aa1cfca9SMichael Tuexen 	}
887aa1cfca9SMichael Tuexen 	old_state = SCTP_GET_STATE(asoc);
8887215cc1bSMichael Tuexen 	sctp_update_acked(stcb, cp, abort_flag);
8897e6206afSMichael Tuexen 	if (*abort_flag) {
8907e6206afSMichael Tuexen 		return;
8917e6206afSMichael Tuexen 	}
892a5d547adSRandall Stewart 	if (asoc->control_pdapi) {
893f8829a4aSRandall Stewart 		/*
894f8829a4aSRandall Stewart 		 * With a normal shutdown we assume the end of last record.
895f8829a4aSRandall Stewart 		 */
896f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
897a5d547adSRandall Stewart 		asoc->control_pdapi->end_added = 1;
898a5d547adSRandall Stewart 		asoc->control_pdapi->pdapi_aborted = 1;
899a5d547adSRandall Stewart 		asoc->control_pdapi = NULL;
900f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
901ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
902ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
903ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
904ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
905ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
906ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
907ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
908ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
909ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
910ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
911ceaad40aSRandall Stewart 			return;
912ceaad40aSRandall Stewart 		}
913ceaad40aSRandall Stewart #endif
914828318e1SMichael Tuexen 		if (stcb->sctp_socket) {
91550cec919SRandall Stewart 			sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
916828318e1SMichael Tuexen 		}
917ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
918ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
919ceaad40aSRandall Stewart #endif
920f8829a4aSRandall Stewart 	}
921f8829a4aSRandall Stewart 	/* goto SHUTDOWN_RECEIVED state to block new requests */
922f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
923f8829a4aSRandall Stewart 		if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
924d61a0ae0SRandall Stewart 		    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
925f8829a4aSRandall Stewart 		    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
926c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED);
927b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
928f8829a4aSRandall Stewart 			/*
929f8829a4aSRandall Stewart 			 * notify upper layer that peer has initiated a
930f8829a4aSRandall Stewart 			 * shutdown
931f8829a4aSRandall Stewart 			 */
932ceaad40aSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
933f8829a4aSRandall Stewart 
934f8829a4aSRandall Stewart 			/* reset time */
9356e55db54SRandall Stewart 			(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
936f8829a4aSRandall Stewart 		}
937f8829a4aSRandall Stewart 	}
938f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
939f8829a4aSRandall Stewart 		/*
940f8829a4aSRandall Stewart 		 * stop the shutdown timer, since we WILL move to
941f8829a4aSRandall Stewart 		 * SHUTDOWN-ACK-SENT.
942f8829a4aSRandall Stewart 		 */
943b7d130beSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
944b7d130beSMichael Tuexen 		    net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
945f8829a4aSRandall Stewart 	}
9465bead436SRandall Stewart 	/* Now is there unsent data on a stream somewhere? */
947689e6a5fSMichael Tuexen 	some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
948f8829a4aSRandall Stewart 
949f8829a4aSRandall Stewart 	if (!TAILQ_EMPTY(&asoc->send_queue) ||
950f8829a4aSRandall Stewart 	    !TAILQ_EMPTY(&asoc->sent_queue) ||
951f8829a4aSRandall Stewart 	    some_on_streamwheel) {
952f8829a4aSRandall Stewart 		/* By returning we will push more data out */
953f8829a4aSRandall Stewart 		return;
954f8829a4aSRandall Stewart 	} else {
955f8829a4aSRandall Stewart 		/* no outstanding data to send, so move on... */
956f8829a4aSRandall Stewart 		/* send SHUTDOWN-ACK */
957f8829a4aSRandall Stewart 		/* move to SHUTDOWN-ACK-SENT state */
958f42a358aSRandall Stewart 		if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
959f42a358aSRandall Stewart 		    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
960f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
961f8829a4aSRandall Stewart 		}
962b201f536SRandall Stewart 		SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
963aa1cfca9SMichael Tuexen 		if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
964aa1cfca9SMichael Tuexen 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
96512af6654SMichael Tuexen 			sctp_stop_timers_for_shutdown(stcb);
966c39cfa1fSMichael Tuexen 			sctp_send_shutdown_ack(stcb, net);
967aa1cfca9SMichael Tuexen 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
968aa1cfca9SMichael Tuexen 			    stcb->sctp_ep, stcb, net);
969aa1cfca9SMichael Tuexen 		} else if (old_state == SCTP_STATE_SHUTDOWN_ACK_SENT) {
970aa1cfca9SMichael Tuexen 			sctp_send_shutdown_ack(stcb, net);
971aa1cfca9SMichael Tuexen 		}
972f8829a4aSRandall Stewart 	}
973f8829a4aSRandall Stewart }
974f8829a4aSRandall Stewart 
975f8829a4aSRandall Stewart static void
9767215cc1bSMichael Tuexen sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
9777b470fc3SMichael Tuexen     struct sctp_tcb *stcb,
9787b470fc3SMichael Tuexen     struct sctp_nets *net)
979f8829a4aSRandall Stewart {
980f8829a4aSRandall Stewart 	struct sctp_association *asoc;
981f8829a4aSRandall Stewart 
982ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
983ceaad40aSRandall Stewart 	struct socket *so;
984ceaad40aSRandall Stewart 
985ceaad40aSRandall Stewart 	so = SCTP_INP_SO(stcb->sctp_ep);
986ceaad40aSRandall Stewart #endif
987ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
988ad81507eSRandall Stewart 	    "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
989f8829a4aSRandall Stewart 	if (stcb == NULL)
990f8829a4aSRandall Stewart 		return;
991f8829a4aSRandall Stewart 
992f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
993f8829a4aSRandall Stewart 	/* process according to association state */
9947b470fc3SMichael Tuexen 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
9957b470fc3SMichael Tuexen 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
9967b470fc3SMichael Tuexen 		/* unexpected SHUTDOWN-ACK... do OOTB handling... */
9977b470fc3SMichael Tuexen 		sctp_send_shutdown_complete(stcb, net, 1);
9987b470fc3SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
9997b470fc3SMichael Tuexen 		return;
10007b470fc3SMichael Tuexen 	}
1001f8829a4aSRandall Stewart 	if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
1002f8829a4aSRandall Stewart 	    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
1003f8829a4aSRandall Stewart 		/* unexpected SHUTDOWN-ACK... so ignore... */
1004f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
1005f8829a4aSRandall Stewart 		return;
1006f8829a4aSRandall Stewart 	}
1007a5d547adSRandall Stewart 	if (asoc->control_pdapi) {
1008f8829a4aSRandall Stewart 		/*
1009f8829a4aSRandall Stewart 		 * With a normal shutdown we assume the end of last record.
1010f8829a4aSRandall Stewart 		 */
1011f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
1012a5d547adSRandall Stewart 		asoc->control_pdapi->end_added = 1;
1013a5d547adSRandall Stewart 		asoc->control_pdapi->pdapi_aborted = 1;
1014a5d547adSRandall Stewart 		asoc->control_pdapi = NULL;
1015f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
1016ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1017ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
1018ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
1019ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
1020ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
1021ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
1022ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1023ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
1024ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
1025ceaad40aSRandall Stewart 			return;
1026ceaad40aSRandall Stewart 		}
1027ceaad40aSRandall Stewart #endif
102850cec919SRandall Stewart 		sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1029ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1030ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
1031ceaad40aSRandall Stewart #endif
1032f8829a4aSRandall Stewart 	}
103356f778aaSMichael Tuexen #ifdef INVARIANTS
1034f8829a4aSRandall Stewart 	if (!TAILQ_EMPTY(&asoc->send_queue) ||
1035f8829a4aSRandall Stewart 	    !TAILQ_EMPTY(&asoc->sent_queue) ||
1036f7a77f6fSMichael Tuexen 	    !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
103756f778aaSMichael Tuexen 		panic("Queues are not empty when handling SHUTDOWN-ACK");
1038f8829a4aSRandall Stewart 	}
103956f778aaSMichael Tuexen #endif
1040f8829a4aSRandall Stewart 	/* stop the timer */
1041b7d130beSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net,
1042b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
1043f8829a4aSRandall Stewart 	/* send SHUTDOWN-COMPLETE */
10447b470fc3SMichael Tuexen 	sctp_send_shutdown_complete(stcb, net, 0);
1045f8829a4aSRandall Stewart 	/* notify upper layer protocol */
1046f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
1047f8829a4aSRandall Stewart 		if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1048f8829a4aSRandall Stewart 		    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
10494e88d37aSMichael Tuexen 			stcb->sctp_socket->so_snd.sb_cc = 0;
1050f8829a4aSRandall Stewart 		}
1051c75fdd30SMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
1052f8829a4aSRandall Stewart 	}
1053f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
1054f8829a4aSRandall Stewart 	/* free the TCB but first save off the ep */
1055ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1056ceaad40aSRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, 1);
1057ceaad40aSRandall Stewart 	SCTP_TCB_UNLOCK(stcb);
1058ceaad40aSRandall Stewart 	SCTP_SOCKET_LOCK(so, 1);
1059ceaad40aSRandall Stewart 	SCTP_TCB_LOCK(stcb);
1060ceaad40aSRandall Stewart 	atomic_subtract_int(&stcb->asoc.refcnt, 1);
1061ceaad40aSRandall Stewart #endif
1062c4739e2fSRandall Stewart 	(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1063b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1064ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1065ceaad40aSRandall Stewart 	SCTP_SOCKET_UNLOCK(so, 1);
1066ceaad40aSRandall Stewart #endif
1067f8829a4aSRandall Stewart }
1068f8829a4aSRandall Stewart 
1069f8829a4aSRandall Stewart /*
1070f8829a4aSRandall Stewart  * Skip past the param header and then we will find the chunk that caused the
1071f8829a4aSRandall Stewart  * problem. There are two possiblities ASCONF or FWD-TSN other than that and
1072f8829a4aSRandall Stewart  * our peer must be broken.
1073f8829a4aSRandall Stewart  */
1074f8829a4aSRandall Stewart static void
1075f8829a4aSRandall Stewart sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
1076f8829a4aSRandall Stewart     struct sctp_nets *net)
1077f8829a4aSRandall Stewart {
1078f8829a4aSRandall Stewart 	struct sctp_chunkhdr *chk;
1079f8829a4aSRandall Stewart 
1080f8829a4aSRandall Stewart 	chk = (struct sctp_chunkhdr *)((caddr_t)phdr + sizeof(*phdr));
1081f8829a4aSRandall Stewart 	switch (chk->chunk_type) {
1082f8829a4aSRandall Stewart 	case SCTP_ASCONF_ACK:
1083f8829a4aSRandall Stewart 	case SCTP_ASCONF:
1084f8829a4aSRandall Stewart 		sctp_asconf_cleanup(stcb, net);
1085f8829a4aSRandall Stewart 		break;
1086f8829a4aSRandall Stewart 	case SCTP_FORWARD_CUM_TSN:
1087dd973b0eSMichael Tuexen 		stcb->asoc.prsctp_supported = 0;
1088f8829a4aSRandall Stewart 		break;
1089f8829a4aSRandall Stewart 	default:
1090ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
1091ad81507eSRandall Stewart 		    "Peer does not support chunk type %d(%x)??\n",
1092f8829a4aSRandall Stewart 		    chk->chunk_type, (uint32_t) chk->chunk_type);
1093f8829a4aSRandall Stewart 		break;
1094f8829a4aSRandall Stewart 	}
1095f8829a4aSRandall Stewart }
1096f8829a4aSRandall Stewart 
1097f8829a4aSRandall Stewart /*
1098f8829a4aSRandall Stewart  * Skip past the param header and then we will find the param that caused the
1099f8829a4aSRandall Stewart  * problem.  There are a number of param's in a ASCONF OR the prsctp param
1100f8829a4aSRandall Stewart  * these will turn of specific features.
1101c79bec9cSMichael Tuexen  * XXX: Is this the right thing to do?
1102f8829a4aSRandall Stewart  */
1103f8829a4aSRandall Stewart static void
1104f8829a4aSRandall Stewart sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
1105f8829a4aSRandall Stewart {
1106f8829a4aSRandall Stewart 	struct sctp_paramhdr *pbad;
1107f8829a4aSRandall Stewart 
1108f8829a4aSRandall Stewart 	pbad = phdr + 1;
1109f8829a4aSRandall Stewart 	switch (ntohs(pbad->param_type)) {
1110f8829a4aSRandall Stewart 		/* pr-sctp draft */
1111f8829a4aSRandall Stewart 	case SCTP_PRSCTP_SUPPORTED:
1112dd973b0eSMichael Tuexen 		stcb->asoc.prsctp_supported = 0;
1113f8829a4aSRandall Stewart 		break;
1114f8829a4aSRandall Stewart 	case SCTP_SUPPORTED_CHUNK_EXT:
1115f8829a4aSRandall Stewart 		break;
1116f8829a4aSRandall Stewart 		/* draft-ietf-tsvwg-addip-sctp */
1117830d754dSRandall Stewart 	case SCTP_HAS_NAT_SUPPORT:
1118830d754dSRandall Stewart 		stcb->asoc.peer_supports_nat = 0;
1119830d754dSRandall Stewart 		break;
1120f8829a4aSRandall Stewart 	case SCTP_ADD_IP_ADDRESS:
1121f8829a4aSRandall Stewart 	case SCTP_DEL_IP_ADDRESS:
1122f8829a4aSRandall Stewart 	case SCTP_SET_PRIM_ADDR:
1123c79bec9cSMichael Tuexen 		stcb->asoc.asconf_supported = 0;
1124f8829a4aSRandall Stewart 		break;
1125f8829a4aSRandall Stewart 	case SCTP_SUCCESS_REPORT:
1126f8829a4aSRandall Stewart 	case SCTP_ERROR_CAUSE_IND:
1127ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1128ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
1129ad81507eSRandall Stewart 		    "Turning off ASCONF to this strange peer\n");
1130c79bec9cSMichael Tuexen 		stcb->asoc.asconf_supported = 0;
1131f8829a4aSRandall Stewart 		break;
1132f8829a4aSRandall Stewart 	default:
1133ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
1134ad81507eSRandall Stewart 		    "Peer does not support param type %d(%x)??\n",
1135f8829a4aSRandall Stewart 		    pbad->param_type, (uint32_t) pbad->param_type);
1136f8829a4aSRandall Stewart 		break;
1137f8829a4aSRandall Stewart 	}
1138f8829a4aSRandall Stewart }
1139f8829a4aSRandall Stewart 
1140f8829a4aSRandall Stewart static int
1141f8829a4aSRandall Stewart sctp_handle_error(struct sctp_chunkhdr *ch,
1142f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net)
1143f8829a4aSRandall Stewart {
1144f8829a4aSRandall Stewart 	int chklen;
1145f8829a4aSRandall Stewart 	struct sctp_paramhdr *phdr;
1146389b1b11SMichael Tuexen 	uint16_t error, error_type;
1147f8829a4aSRandall Stewart 	uint16_t error_len;
1148f8829a4aSRandall Stewart 	struct sctp_association *asoc;
1149f8829a4aSRandall Stewart 	int adjust;
1150f8829a4aSRandall Stewart 
1151ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1152ceaad40aSRandall Stewart 	struct socket *so;
1153ceaad40aSRandall Stewart 
1154ceaad40aSRandall Stewart #endif
1155ceaad40aSRandall Stewart 
1156f8829a4aSRandall Stewart 	/* parse through all of the errors and process */
1157f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
1158f8829a4aSRandall Stewart 	phdr = (struct sctp_paramhdr *)((caddr_t)ch +
1159f8829a4aSRandall Stewart 	    sizeof(struct sctp_chunkhdr));
1160f8829a4aSRandall Stewart 	chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
1161389b1b11SMichael Tuexen 	error = 0;
1162f8829a4aSRandall Stewart 	while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
1163f8829a4aSRandall Stewart 		/* Process an Error Cause */
1164f8829a4aSRandall Stewart 		error_type = ntohs(phdr->param_type);
1165f8829a4aSRandall Stewart 		error_len = ntohs(phdr->param_length);
1166f8829a4aSRandall Stewart 		if ((error_len > chklen) || (error_len == 0)) {
1167f8829a4aSRandall Stewart 			/* invalid param length for this param */
1168ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in error param- chunk left:%d errorlen:%d\n",
1169f8829a4aSRandall Stewart 			    chklen, error_len);
1170f8829a4aSRandall Stewart 			return (0);
1171f8829a4aSRandall Stewart 		}
1172389b1b11SMichael Tuexen 		if (error == 0) {
1173389b1b11SMichael Tuexen 			/* report the first error cause */
1174389b1b11SMichael Tuexen 			error = error_type;
1175389b1b11SMichael Tuexen 		}
1176f8829a4aSRandall Stewart 		switch (error_type) {
1177f8829a4aSRandall Stewart 		case SCTP_CAUSE_INVALID_STREAM:
1178f8829a4aSRandall Stewart 		case SCTP_CAUSE_MISSING_PARAM:
1179f8829a4aSRandall Stewart 		case SCTP_CAUSE_INVALID_PARAM:
1180f8829a4aSRandall Stewart 		case SCTP_CAUSE_NO_USER_DATA:
1181ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %d back? We have a bug :/ (or do they?)\n",
1182f8829a4aSRandall Stewart 			    error_type);
1183f8829a4aSRandall Stewart 			break;
1184830d754dSRandall Stewart 		case SCTP_CAUSE_NAT_COLLIDING_STATE:
1185830d754dSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
1186830d754dSRandall Stewart 			    ch->chunk_flags);
1187830d754dSRandall Stewart 			if (sctp_handle_nat_colliding_state(stcb)) {
1188830d754dSRandall Stewart 				return (0);
1189830d754dSRandall Stewart 			}
1190830d754dSRandall Stewart 			break;
1191830d754dSRandall Stewart 		case SCTP_CAUSE_NAT_MISSING_STATE:
1192830d754dSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
1193830d754dSRandall Stewart 			    ch->chunk_flags);
1194830d754dSRandall Stewart 			if (sctp_handle_nat_missing_state(stcb, net)) {
1195830d754dSRandall Stewart 				return (0);
1196830d754dSRandall Stewart 			}
1197830d754dSRandall Stewart 			break;
1198f8829a4aSRandall Stewart 		case SCTP_CAUSE_STALE_COOKIE:
1199f8829a4aSRandall Stewart 			/*
1200f8829a4aSRandall Stewart 			 * We only act if we have echoed a cookie and are
1201f8829a4aSRandall Stewart 			 * waiting.
1202f8829a4aSRandall Stewart 			 */
1203f8829a4aSRandall Stewart 			if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
1204f8829a4aSRandall Stewart 				int *p;
1205f8829a4aSRandall Stewart 
1206f8829a4aSRandall Stewart 				p = (int *)((caddr_t)phdr + sizeof(*phdr));
1207f8829a4aSRandall Stewart 				/* Save the time doubled */
1208f8829a4aSRandall Stewart 				asoc->cookie_preserve_req = ntohl(*p) << 1;
1209f8829a4aSRandall Stewart 				asoc->stale_cookie_count++;
1210f8829a4aSRandall Stewart 				if (asoc->stale_cookie_count >
1211f8829a4aSRandall Stewart 				    asoc->max_init_times) {
1212410a3b1eSMichael Tuexen 					sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
1213f8829a4aSRandall Stewart 					/* now free the asoc */
1214ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1215ceaad40aSRandall Stewart 					so = SCTP_INP_SO(stcb->sctp_ep);
1216ceaad40aSRandall Stewart 					atomic_add_int(&stcb->asoc.refcnt, 1);
1217ceaad40aSRandall Stewart 					SCTP_TCB_UNLOCK(stcb);
1218ceaad40aSRandall Stewart 					SCTP_SOCKET_LOCK(so, 1);
1219ceaad40aSRandall Stewart 					SCTP_TCB_LOCK(stcb);
1220ceaad40aSRandall Stewart 					atomic_subtract_int(&stcb->asoc.refcnt, 1);
1221ceaad40aSRandall Stewart #endif
1222c4739e2fSRandall Stewart 					(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1223b7d130beSMichael Tuexen 					    SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1224ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1225ceaad40aSRandall Stewart 					SCTP_SOCKET_UNLOCK(so, 1);
1226ceaad40aSRandall Stewart #endif
1227f8829a4aSRandall Stewart 					return (-1);
1228f8829a4aSRandall Stewart 				}
1229f8829a4aSRandall Stewart 				/* blast back to INIT state */
1230830d754dSRandall Stewart 				sctp_toss_old_cookies(stcb, &stcb->asoc);
1231f8829a4aSRandall Stewart 				asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
1232f8829a4aSRandall Stewart 				asoc->state |= SCTP_STATE_COOKIE_WAIT;
1233f8829a4aSRandall Stewart 				sctp_stop_all_cookie_timers(stcb);
1234ceaad40aSRandall Stewart 				sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1235f8829a4aSRandall Stewart 			}
1236f8829a4aSRandall Stewart 			break;
1237f8829a4aSRandall Stewart 		case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1238f8829a4aSRandall Stewart 			/*
1239f8829a4aSRandall Stewart 			 * Nothing we can do here, we don't do hostname
1240f8829a4aSRandall Stewart 			 * addresses so if the peer does not like my IPv6
1241f8829a4aSRandall Stewart 			 * (or IPv4 for that matter) it does not matter. If
1242f8829a4aSRandall Stewart 			 * they don't support that type of address, they can
1243f8829a4aSRandall Stewart 			 * NOT possibly get that packet type... i.e. with no
1244f8829a4aSRandall Stewart 			 * IPv6 you can't recieve a IPv6 packet. so we can
1245f8829a4aSRandall Stewart 			 * safely ignore this one. If we ever added support
1246f8829a4aSRandall Stewart 			 * for HOSTNAME Addresses, then we would need to do
1247f8829a4aSRandall Stewart 			 * something here.
1248f8829a4aSRandall Stewart 			 */
1249f8829a4aSRandall Stewart 			break;
1250f8829a4aSRandall Stewart 		case SCTP_CAUSE_UNRECOG_CHUNK:
1251f8829a4aSRandall Stewart 			sctp_process_unrecog_chunk(stcb, phdr, net);
1252f8829a4aSRandall Stewart 			break;
1253f8829a4aSRandall Stewart 		case SCTP_CAUSE_UNRECOG_PARAM:
1254f8829a4aSRandall Stewart 			sctp_process_unrecog_param(stcb, phdr);
1255f8829a4aSRandall Stewart 			break;
1256f8829a4aSRandall Stewart 		case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1257f8829a4aSRandall Stewart 			/*
1258f8829a4aSRandall Stewart 			 * We ignore this since the timer will drive out a
1259f8829a4aSRandall Stewart 			 * new cookie anyway and there timer will drive us
1260f8829a4aSRandall Stewart 			 * to send a SHUTDOWN_COMPLETE. We can't send one
1261f8829a4aSRandall Stewart 			 * here since we don't have their tag.
1262f8829a4aSRandall Stewart 			 */
1263f8829a4aSRandall Stewart 			break;
1264f8829a4aSRandall Stewart 		case SCTP_CAUSE_DELETING_LAST_ADDR:
1265f8829a4aSRandall Stewart 		case SCTP_CAUSE_RESOURCE_SHORTAGE:
1266f8829a4aSRandall Stewart 		case SCTP_CAUSE_DELETING_SRC_ADDR:
1267f8829a4aSRandall Stewart 			/*
1268f8829a4aSRandall Stewart 			 * We should NOT get these here, but in a
126993164cf9SRandall Stewart 			 * ASCONF-ACK.
1270f8829a4aSRandall Stewart 			 */
1271ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a Operational Error?<%d>?\n",
1272f8829a4aSRandall Stewart 			    error_type);
1273f8829a4aSRandall Stewart 			break;
1274f8829a4aSRandall Stewart 		case SCTP_CAUSE_OUT_OF_RESC:
1275f8829a4aSRandall Stewart 			/*
1276f8829a4aSRandall Stewart 			 * And what, pray tell do we do with the fact that
1277f8829a4aSRandall Stewart 			 * the peer is out of resources? Not really sure we
127893164cf9SRandall Stewart 			 * could do anything but abort. I suspect this
1279f8829a4aSRandall Stewart 			 * should have came WITH an abort instead of in a
1280f8829a4aSRandall Stewart 			 * OP-ERROR.
1281f8829a4aSRandall Stewart 			 */
1282f8829a4aSRandall Stewart 			break;
1283f8829a4aSRandall Stewart 		default:
1284ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown error type = 0x%xh\n",
1285f8829a4aSRandall Stewart 			    error_type);
1286f8829a4aSRandall Stewart 			break;
1287f8829a4aSRandall Stewart 		}
1288f8829a4aSRandall Stewart 		adjust = SCTP_SIZE32(error_len);
1289f8829a4aSRandall Stewart 		chklen -= adjust;
1290f8829a4aSRandall Stewart 		phdr = (struct sctp_paramhdr *)((caddr_t)phdr + adjust);
1291f8829a4aSRandall Stewart 	}
1292389b1b11SMichael Tuexen 	sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, error, ch, SCTP_SO_NOT_LOCKED);
1293f8829a4aSRandall Stewart 	return (0);
1294f8829a4aSRandall Stewart }
1295f8829a4aSRandall Stewart 
1296f8829a4aSRandall Stewart static int
1297b1754ad1SMichael Tuexen sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1298b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
1299f30ac432SMichael Tuexen     struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1300f30ac432SMichael Tuexen     struct sctp_nets *net, int *abort_no_unlock,
1301457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
1302f30ac432SMichael Tuexen     uint32_t vrf_id)
1303f8829a4aSRandall Stewart {
1304f8829a4aSRandall Stewart 	struct sctp_init_ack *init_ack;
1305f8829a4aSRandall Stewart 	struct mbuf *op_err;
1306f8829a4aSRandall Stewart 
1307ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
1308ad81507eSRandall Stewart 	    "sctp_handle_init_ack: handling INIT-ACK\n");
1309ad81507eSRandall Stewart 
1310f8829a4aSRandall Stewart 	if (stcb == NULL) {
1311ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
1312ad81507eSRandall Stewart 		    "sctp_handle_init_ack: TCB is null\n");
1313f8829a4aSRandall Stewart 		return (-1);
1314f8829a4aSRandall Stewart 	}
1315f8829a4aSRandall Stewart 	if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1316f8829a4aSRandall Stewart 		/* Invalid length */
1317ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1318b1754ad1SMichael Tuexen 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1319b1754ad1SMichael Tuexen 		    src, dst, sh, op_err,
1320457b4b88SMichael Tuexen 		    mflowtype, mflowid,
1321f30ac432SMichael Tuexen 		    vrf_id, net->port);
1322bff64a4dSRandall Stewart 		*abort_no_unlock = 1;
1323f8829a4aSRandall Stewart 		return (-1);
1324f8829a4aSRandall Stewart 	}
1325f8829a4aSRandall Stewart 	init_ack = &cp->init;
1326f8829a4aSRandall Stewart 	/* validate parameters */
1327f8829a4aSRandall Stewart 	if (init_ack->initiate_tag == 0) {
1328f8829a4aSRandall Stewart 		/* protocol error... send an abort */
1329ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1330b1754ad1SMichael Tuexen 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1331b1754ad1SMichael Tuexen 		    src, dst, sh, op_err,
1332457b4b88SMichael Tuexen 		    mflowtype, mflowid,
1333f30ac432SMichael Tuexen 		    vrf_id, net->port);
1334bff64a4dSRandall Stewart 		*abort_no_unlock = 1;
1335f8829a4aSRandall Stewart 		return (-1);
1336f8829a4aSRandall Stewart 	}
1337f8829a4aSRandall Stewart 	if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1338f8829a4aSRandall Stewart 		/* protocol error... send an abort */
1339ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1340b1754ad1SMichael Tuexen 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1341b1754ad1SMichael Tuexen 		    src, dst, sh, op_err,
1342457b4b88SMichael Tuexen 		    mflowtype, mflowid,
1343f30ac432SMichael Tuexen 		    vrf_id, net->port);
1344bff64a4dSRandall Stewart 		*abort_no_unlock = 1;
1345f8829a4aSRandall Stewart 		return (-1);
1346f8829a4aSRandall Stewart 	}
1347f8829a4aSRandall Stewart 	if (init_ack->num_inbound_streams == 0) {
1348f8829a4aSRandall Stewart 		/* protocol error... send an abort */
1349ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1350b1754ad1SMichael Tuexen 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1351b1754ad1SMichael Tuexen 		    src, dst, sh, op_err,
1352457b4b88SMichael Tuexen 		    mflowtype, mflowid,
1353f30ac432SMichael Tuexen 		    vrf_id, net->port);
1354bff64a4dSRandall Stewart 		*abort_no_unlock = 1;
1355f8829a4aSRandall Stewart 		return (-1);
1356f8829a4aSRandall Stewart 	}
1357f8829a4aSRandall Stewart 	if (init_ack->num_outbound_streams == 0) {
1358f8829a4aSRandall Stewart 		/* protocol error... send an abort */
1359ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1360b1754ad1SMichael Tuexen 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1361b1754ad1SMichael Tuexen 		    src, dst, sh, op_err,
1362457b4b88SMichael Tuexen 		    mflowtype, mflowid,
1363f30ac432SMichael Tuexen 		    vrf_id, net->port);
1364bff64a4dSRandall Stewart 		*abort_no_unlock = 1;
1365f8829a4aSRandall Stewart 		return (-1);
1366f8829a4aSRandall Stewart 	}
1367f8829a4aSRandall Stewart 	/* process according to association state... */
1368c4739e2fSRandall Stewart 	switch (stcb->asoc.state & SCTP_STATE_MASK) {
1369f8829a4aSRandall Stewart 	case SCTP_STATE_COOKIE_WAIT:
1370f8829a4aSRandall Stewart 		/* this is the expected state for this chunk */
1371f8829a4aSRandall Stewart 		/* process the INIT-ACK parameters */
1372f8829a4aSRandall Stewart 		if (stcb->asoc.primary_destination->dest_state &
1373f8829a4aSRandall Stewart 		    SCTP_ADDR_UNCONFIRMED) {
1374f8829a4aSRandall Stewart 			/*
1375f8829a4aSRandall Stewart 			 * The primary is where we sent the INIT, we can
1376f8829a4aSRandall Stewart 			 * always consider it confirmed when the INIT-ACK is
1377f8829a4aSRandall Stewart 			 * returned. Do this before we load addresses
1378f8829a4aSRandall Stewart 			 * though.
1379f8829a4aSRandall Stewart 			 */
1380f8829a4aSRandall Stewart 			stcb->asoc.primary_destination->dest_state &=
1381f8829a4aSRandall Stewart 			    ~SCTP_ADDR_UNCONFIRMED;
1382f8829a4aSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1383ceaad40aSRandall Stewart 			    stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1384f8829a4aSRandall Stewart 		}
1385b1754ad1SMichael Tuexen 		if (sctp_process_init_ack(m, iphlen, offset, src, dst, sh, cp, stcb,
1386f30ac432SMichael Tuexen 		    net, abort_no_unlock,
1387457b4b88SMichael Tuexen 		    mflowtype, mflowid,
1388f30ac432SMichael Tuexen 		    vrf_id) < 0) {
1389f8829a4aSRandall Stewart 			/* error in parsing parameters */
1390f8829a4aSRandall Stewart 			return (-1);
1391f8829a4aSRandall Stewart 		}
1392f8829a4aSRandall Stewart 		/* update our state */
1393ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1394c4739e2fSRandall Stewart 		SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED);
1395f8829a4aSRandall Stewart 
1396f8829a4aSRandall Stewart 		/* reset the RTO calc */
1397b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1398c4739e2fSRandall Stewart 			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1399c4739e2fSRandall Stewart 			    stcb->asoc.overall_error_count,
1400c4739e2fSRandall Stewart 			    0,
1401c4739e2fSRandall Stewart 			    SCTP_FROM_SCTP_INPUT,
1402c4739e2fSRandall Stewart 			    __LINE__);
1403c4739e2fSRandall Stewart 		}
1404f8829a4aSRandall Stewart 		stcb->asoc.overall_error_count = 0;
14056e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1406f8829a4aSRandall Stewart 		/*
1407f8829a4aSRandall Stewart 		 * collapse the init timer back in case of a exponential
1408a5d547adSRandall Stewart 		 * backoff
1409f8829a4aSRandall Stewart 		 */
1410f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1411f8829a4aSRandall Stewart 		    stcb, net);
1412f8829a4aSRandall Stewart 		/*
1413f8829a4aSRandall Stewart 		 * the send at the end of the inbound data processing will
1414f8829a4aSRandall Stewart 		 * cause the cookie to be sent
1415f8829a4aSRandall Stewart 		 */
1416f8829a4aSRandall Stewart 		break;
1417f8829a4aSRandall Stewart 	case SCTP_STATE_SHUTDOWN_SENT:
1418f8829a4aSRandall Stewart 		/* incorrect state... discard */
1419f8829a4aSRandall Stewart 		break;
1420f8829a4aSRandall Stewart 	case SCTP_STATE_COOKIE_ECHOED:
1421f8829a4aSRandall Stewart 		/* incorrect state... discard */
1422f8829a4aSRandall Stewart 		break;
1423f8829a4aSRandall Stewart 	case SCTP_STATE_OPEN:
1424f8829a4aSRandall Stewart 		/* incorrect state... discard */
1425f8829a4aSRandall Stewart 		break;
1426f8829a4aSRandall Stewart 	case SCTP_STATE_EMPTY:
1427f8829a4aSRandall Stewart 	case SCTP_STATE_INUSE:
1428f8829a4aSRandall Stewart 	default:
1429f8829a4aSRandall Stewart 		/* incorrect state... discard */
1430f8829a4aSRandall Stewart 		return (-1);
1431f8829a4aSRandall Stewart 		break;
1432f8829a4aSRandall Stewart 	}
1433ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1434f8829a4aSRandall Stewart 	return (0);
1435f8829a4aSRandall Stewart }
1436f8829a4aSRandall Stewart 
1437830d754dSRandall Stewart static struct sctp_tcb *
1438830d754dSRandall Stewart sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1439b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
1440830d754dSRandall Stewart     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1441830d754dSRandall Stewart     struct sctp_inpcb *inp, struct sctp_nets **netp,
1442830d754dSRandall Stewart     struct sockaddr *init_src, int *notification,
1443830d754dSRandall Stewart     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1444457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
1445830d754dSRandall Stewart     uint32_t vrf_id, uint16_t port);
1446830d754dSRandall Stewart 
1447f8829a4aSRandall Stewart 
1448f8829a4aSRandall Stewart /*
1449f8829a4aSRandall Stewart  * handle a state cookie for an existing association m: input packet mbuf
1450f8829a4aSRandall Stewart  * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1451f8829a4aSRandall Stewart  * "split" mbuf and the cookie signature does not exist offset: offset into
1452f8829a4aSRandall Stewart  * mbuf to the cookie-echo chunk
1453f8829a4aSRandall Stewart  */
1454f8829a4aSRandall Stewart static struct sctp_tcb *
1455f8829a4aSRandall Stewart sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1456b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
1457f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1458830d754dSRandall Stewart     struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
14597215cc1bSMichael Tuexen     struct sockaddr *init_src, int *notification,
1460f30ac432SMichael Tuexen     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1461457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
1462f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
1463f8829a4aSRandall Stewart {
1464f8829a4aSRandall Stewart 	struct sctp_association *asoc;
1465f8829a4aSRandall Stewart 	struct sctp_init_chunk *init_cp, init_buf;
1466f8829a4aSRandall Stewart 	struct sctp_init_ack_chunk *initack_cp, initack_buf;
1467830d754dSRandall Stewart 	struct sctp_nets *net;
1468830d754dSRandall Stewart 	struct mbuf *op_err;
1469a5d547adSRandall Stewart 	int init_offset, initack_offset, i;
1470f8829a4aSRandall Stewart 	int retval;
14717f34832bSRandall Stewart 	int spec_flag = 0;
14724c9179adSRandall Stewart 	uint32_t how_indx;
1473f8829a4aSRandall Stewart 
1474f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
1475f0396ad1SMichael Tuexen 	int j;
1476f0396ad1SMichael Tuexen 
1477f0396ad1SMichael Tuexen #endif
1478f0396ad1SMichael Tuexen 
1479830d754dSRandall Stewart 	net = *netp;
1480f8829a4aSRandall Stewart 	/* I know that the TCB is non-NULL from the caller */
1481f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
1482f42a358aSRandall Stewart 	for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
148344b7479bSRandall Stewart 		if (asoc->cookie_how[how_indx] == 0)
148444b7479bSRandall Stewart 			break;
148544b7479bSRandall Stewart 	}
148644b7479bSRandall Stewart 	if (how_indx < sizeof(asoc->cookie_how)) {
148744b7479bSRandall Stewart 		asoc->cookie_how[how_indx] = 1;
148844b7479bSRandall Stewart 	}
1489f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1490f8829a4aSRandall Stewart 		/* SHUTDOWN came in after sending INIT-ACK */
1491f8829a4aSRandall Stewart 		sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1492ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_COOKIE_IN_SHUTDOWN, "");
1493b1754ad1SMichael Tuexen 		sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
1494d089f9b9SMichael Tuexen 		    mflowtype, mflowid, inp->fibnum,
1495c54a18d2SRandall Stewart 		    vrf_id, net->port);
149644b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
149744b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 2;
1498f8829a4aSRandall Stewart 		return (NULL);
1499f8829a4aSRandall Stewart 	}
1500f8829a4aSRandall Stewart 	/*
1501f8829a4aSRandall Stewart 	 * find and validate the INIT chunk in the cookie (peer's info) the
1502f8829a4aSRandall Stewart 	 * INIT should start after the cookie-echo header struct (chunk
1503f8829a4aSRandall Stewart 	 * header, state cookie header struct)
1504f8829a4aSRandall Stewart 	 */
1505f8829a4aSRandall Stewart 	init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1506f8829a4aSRandall Stewart 
1507f8829a4aSRandall Stewart 	init_cp = (struct sctp_init_chunk *)
1508f8829a4aSRandall Stewart 	    sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1509f8829a4aSRandall Stewart 	    (uint8_t *) & init_buf);
1510f8829a4aSRandall Stewart 	if (init_cp == NULL) {
1511f8829a4aSRandall Stewart 		/* could not pull a INIT chunk in cookie */
1512f8829a4aSRandall Stewart 		return (NULL);
1513f8829a4aSRandall Stewart 	}
1514f8829a4aSRandall Stewart 	if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1515f8829a4aSRandall Stewart 		return (NULL);
1516f8829a4aSRandall Stewart 	}
1517f8829a4aSRandall Stewart 	/*
1518f8829a4aSRandall Stewart 	 * find and validate the INIT-ACK chunk in the cookie (my info) the
1519f8829a4aSRandall Stewart 	 * INIT-ACK follows the INIT chunk
1520f8829a4aSRandall Stewart 	 */
152160990c0cSMichael Tuexen 	initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1522f8829a4aSRandall Stewart 	initack_cp = (struct sctp_init_ack_chunk *)
1523f8829a4aSRandall Stewart 	    sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1524f8829a4aSRandall Stewart 	    (uint8_t *) & initack_buf);
1525f8829a4aSRandall Stewart 	if (initack_cp == NULL) {
1526f8829a4aSRandall Stewart 		/* could not pull INIT-ACK chunk in cookie */
1527f8829a4aSRandall Stewart 		return (NULL);
1528f8829a4aSRandall Stewart 	}
1529f8829a4aSRandall Stewart 	if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1530f8829a4aSRandall Stewart 		return (NULL);
1531f8829a4aSRandall Stewart 	}
1532f8829a4aSRandall Stewart 	if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1533f8829a4aSRandall Stewart 	    (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1534f8829a4aSRandall Stewart 		/*
1535f8829a4aSRandall Stewart 		 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1536f8829a4aSRandall Stewart 		 * to get into the OPEN state
1537f8829a4aSRandall Stewart 		 */
153844b7479bSRandall Stewart 		if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1539851b7298SRandall Stewart 			/*-
1540851b7298SRandall Stewart 			 * Opps, this means that we somehow generated two vtag's
1541851b7298SRandall Stewart 			 * the same. I.e. we did:
1542851b7298SRandall Stewart 			 *  Us               Peer
1543851b7298SRandall Stewart 			 *   <---INIT(tag=a)------
1544851b7298SRandall Stewart 			 *   ----INIT-ACK(tag=t)-->
1545851b7298SRandall Stewart 			 *   ----INIT(tag=t)------> *1
1546851b7298SRandall Stewart 			 *   <---INIT-ACK(tag=a)---
1547851b7298SRandall Stewart                          *   <----CE(tag=t)------------- *2
1548851b7298SRandall Stewart 			 *
1549851b7298SRandall Stewart 			 * At point *1 we should be generating a different
1550851b7298SRandall Stewart 			 * tag t'. Which means we would throw away the CE and send
1551851b7298SRandall Stewart 			 * ours instead. Basically this is case C (throw away side).
1552851b7298SRandall Stewart 			 */
1553851b7298SRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
1554851b7298SRandall Stewart 				asoc->cookie_how[how_indx] = 17;
1555851b7298SRandall Stewart 			return (NULL);
1556851b7298SRandall Stewart 
155744b7479bSRandall Stewart 		}
15581213f0e7SMichael Tuexen 		switch (SCTP_GET_STATE(asoc)) {
1559f8829a4aSRandall Stewart 		case SCTP_STATE_COOKIE_WAIT:
156044b7479bSRandall Stewart 		case SCTP_STATE_COOKIE_ECHOED:
1561f8829a4aSRandall Stewart 			/*
156217205eccSRandall Stewart 			 * INIT was sent but got a COOKIE_ECHO with the
156344b7479bSRandall Stewart 			 * correct tags... just accept it...but we must
156444b7479bSRandall Stewart 			 * process the init so that we can make sure we have
156544b7479bSRandall Stewart 			 * the right seq no's.
1566f8829a4aSRandall Stewart 			 */
1567f8829a4aSRandall Stewart 			/* First we must process the INIT !! */
15687215cc1bSMichael Tuexen 			retval = sctp_process_init(init_cp, stcb);
1569f8829a4aSRandall Stewart 			if (retval < 0) {
157044b7479bSRandall Stewart 				if (how_indx < sizeof(asoc->cookie_how))
157144b7479bSRandall Stewart 					asoc->cookie_how[how_indx] = 3;
1572f8829a4aSRandall Stewart 				return (NULL);
1573f8829a4aSRandall Stewart 			}
1574f8829a4aSRandall Stewart 			/* we have already processed the INIT so no problem */
1575b7d130beSMichael Tuexen 			sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp,
1576b7d130beSMichael Tuexen 			    stcb, net,
1577b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1578b7d130beSMichael Tuexen 			sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp,
1579b7d130beSMichael Tuexen 			    stcb, net,
1580b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1581f8829a4aSRandall Stewart 			/* update current state */
1582f42a358aSRandall Stewart 			if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1583f42a358aSRandall Stewart 				SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1584f42a358aSRandall Stewart 			else
1585f42a358aSRandall Stewart 				SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1586c4739e2fSRandall Stewart 
1587c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1588f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1589f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1590f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
1591f8829a4aSRandall Stewart 			}
1592f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1593a5d547adSRandall Stewart 			sctp_stop_all_cookie_timers(stcb);
1594f8829a4aSRandall Stewart 			if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1595f8829a4aSRandall Stewart 			    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1596f8829a4aSRandall Stewart 			    (inp->sctp_socket->so_qlimit == 0)
1597f8829a4aSRandall Stewart 			    ) {
1598ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1599ceaad40aSRandall Stewart 				struct socket *so;
1600ceaad40aSRandall Stewart 
1601ceaad40aSRandall Stewart #endif
1602f8829a4aSRandall Stewart 				/*
1603f8829a4aSRandall Stewart 				 * Here is where collision would go if we
1604f8829a4aSRandall Stewart 				 * did a connect() and instead got a
1605f8829a4aSRandall Stewart 				 * init/init-ack/cookie done before the
1606f8829a4aSRandall Stewart 				 * init-ack came back..
1607f8829a4aSRandall Stewart 				 */
1608f8829a4aSRandall Stewart 				stcb->sctp_ep->sctp_flags |=
1609f8829a4aSRandall Stewart 				    SCTP_PCB_FLAGS_CONNECTED;
1610ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1611ceaad40aSRandall Stewart 				so = SCTP_INP_SO(stcb->sctp_ep);
1612ceaad40aSRandall Stewart 				atomic_add_int(&stcb->asoc.refcnt, 1);
1613ceaad40aSRandall Stewart 				SCTP_TCB_UNLOCK(stcb);
1614ceaad40aSRandall Stewart 				SCTP_SOCKET_LOCK(so, 1);
1615ceaad40aSRandall Stewart 				SCTP_TCB_LOCK(stcb);
1616ceaad40aSRandall Stewart 				atomic_add_int(&stcb->asoc.refcnt, -1);
1617ceaad40aSRandall Stewart 				if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1618ceaad40aSRandall Stewart 					SCTP_SOCKET_UNLOCK(so, 1);
1619ceaad40aSRandall Stewart 					return (NULL);
1620ceaad40aSRandall Stewart 				}
1621ceaad40aSRandall Stewart #endif
1622ceaad40aSRandall Stewart 				soisconnected(stcb->sctp_socket);
1623ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1624ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
1625ceaad40aSRandall Stewart #endif
1626f8829a4aSRandall Stewart 			}
1627f8829a4aSRandall Stewart 			/* notify upper layer */
1628f8829a4aSRandall Stewart 			*notification = SCTP_NOTIFY_ASSOC_UP;
1629f8829a4aSRandall Stewart 			/*
1630f8829a4aSRandall Stewart 			 * since we did not send a HB make sure we don't
1631f8829a4aSRandall Stewart 			 * double things
1632f8829a4aSRandall Stewart 			 */
1633f8829a4aSRandall Stewart 			net->hb_responded = 1;
16349a972525SRandall Stewart 			net->RTO = sctp_calculate_rto(stcb, asoc, net,
1635899288aeSRandall Stewart 			    &cookie->time_entered,
1636899288aeSRandall Stewart 			    sctp_align_unsafe_makecopy,
1637f79aab18SRandall Stewart 			    SCTP_RTT_FROM_NON_DATA);
1638f8829a4aSRandall Stewart 
1639f8829a4aSRandall Stewart 			if (stcb->asoc.sctp_autoclose_ticks &&
1640f8829a4aSRandall Stewart 			    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1641f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1642f8829a4aSRandall Stewart 				    inp, stcb, NULL);
1643f8829a4aSRandall Stewart 			}
1644f8829a4aSRandall Stewart 			break;
1645f8829a4aSRandall Stewart 		default:
1646f8829a4aSRandall Stewart 			/*
1647f8829a4aSRandall Stewart 			 * we're in the OPEN state (or beyond), so peer must
1648f8829a4aSRandall Stewart 			 * have simply lost the COOKIE-ACK
1649f8829a4aSRandall Stewart 			 */
1650f8829a4aSRandall Stewart 			break;
1651f8829a4aSRandall Stewart 		}		/* end switch */
1652a5d547adSRandall Stewart 		sctp_stop_all_cookie_timers(stcb);
1653f8829a4aSRandall Stewart 		/*
1654f8829a4aSRandall Stewart 		 * We ignore the return code here.. not sure if we should
1655f8829a4aSRandall Stewart 		 * somehow abort.. but we do have an existing asoc. This
1656f8829a4aSRandall Stewart 		 * really should not fail.
1657f8829a4aSRandall Stewart 		 */
16587215cc1bSMichael Tuexen 		if (sctp_load_addresses_from_init(stcb, m,
1659f8829a4aSRandall Stewart 		    init_offset + sizeof(struct sctp_init_chunk),
1660b1754ad1SMichael Tuexen 		    initack_offset, src, dst, init_src)) {
166144b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
166244b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 4;
1663f8829a4aSRandall Stewart 			return (NULL);
1664f8829a4aSRandall Stewart 		}
1665f8829a4aSRandall Stewart 		/* respond with a COOKIE-ACK */
1666a5d547adSRandall Stewart 		sctp_toss_old_cookies(stcb, asoc);
1667f8829a4aSRandall Stewart 		sctp_send_cookie_ack(stcb);
166844b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
166944b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 5;
1670f8829a4aSRandall Stewart 		return (stcb);
167117205eccSRandall Stewart 	}
1672f8829a4aSRandall Stewart 	if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1673f8829a4aSRandall Stewart 	    ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1674f8829a4aSRandall Stewart 	    cookie->tie_tag_my_vtag == 0 &&
1675f8829a4aSRandall Stewart 	    cookie->tie_tag_peer_vtag == 0) {
1676f8829a4aSRandall Stewart 		/*
1677f8829a4aSRandall Stewart 		 * case C in Section 5.2.4 Table 2: XMOO silently discard
1678f8829a4aSRandall Stewart 		 */
167944b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
168044b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 6;
1681f8829a4aSRandall Stewart 		return (NULL);
1682f8829a4aSRandall Stewart 	}
1683830d754dSRandall Stewart 	/*
1684830d754dSRandall Stewart 	 * If nat support, and the below and stcb is established, send back
1685830d754dSRandall Stewart 	 * a ABORT(colliding state) if we are established.
1686830d754dSRandall Stewart 	 */
1687830d754dSRandall Stewart 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) &&
1688830d754dSRandall Stewart 	    (asoc->peer_supports_nat) &&
1689830d754dSRandall Stewart 	    ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1690830d754dSRandall Stewart 	    ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1691830d754dSRandall Stewart 	    (asoc->peer_vtag == 0)))) {
1692830d754dSRandall Stewart 		/*
1693830d754dSRandall Stewart 		 * Special case - Peer's support nat. We may have two init's
1694830d754dSRandall Stewart 		 * that we gave out the same tag on since one was not
1695830d754dSRandall Stewart 		 * established.. i.e. we get INIT from host-1 behind the nat
1696830d754dSRandall Stewart 		 * and we respond tag-a, we get a INIT from host-2 behind
1697830d754dSRandall Stewart 		 * the nat and we get tag-a again. Then we bring up host-1
1698830d754dSRandall Stewart 		 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1699830d754dSRandall Stewart 		 * Now we have colliding state. We must send an abort here
1700830d754dSRandall Stewart 		 * with colliding state indication.
1701830d754dSRandall Stewart 		 */
1702ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_NAT_COLLIDING_STATE, "");
1703b1754ad1SMichael Tuexen 		sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
1704d089f9b9SMichael Tuexen 		    mflowtype, mflowid, inp->fibnum,
1705f30ac432SMichael Tuexen 		    vrf_id, port);
1706830d754dSRandall Stewart 		return (NULL);
1707830d754dSRandall Stewart 	}
1708830d754dSRandall Stewart 	if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1709830d754dSRandall Stewart 	    ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1710830d754dSRandall Stewart 	    (asoc->peer_vtag == 0))) {
1711f8829a4aSRandall Stewart 		/*
1712f8829a4aSRandall Stewart 		 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1713f8829a4aSRandall Stewart 		 * should be ok, re-accept peer info
1714f8829a4aSRandall Stewart 		 */
171544b7479bSRandall Stewart 		if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
171644b7479bSRandall Stewart 			/*
171744b7479bSRandall Stewart 			 * Extension of case C. If we hit this, then the
171844b7479bSRandall Stewart 			 * random number generator returned the same vtag
171944b7479bSRandall Stewart 			 * when we first sent our INIT-ACK and when we later
172044b7479bSRandall Stewart 			 * sent our INIT. The side with the seq numbers that
172144b7479bSRandall Stewart 			 * are different will be the one that normnally
172244b7479bSRandall Stewart 			 * would have hit case C. This in effect "extends"
172344b7479bSRandall Stewart 			 * our vtags in this collision case to be 64 bits.
172444b7479bSRandall Stewart 			 * The same collision could occur aka you get both
172544b7479bSRandall Stewart 			 * vtag and seq number the same twice in a row.. but
172644b7479bSRandall Stewart 			 * is much less likely. If it did happen then we
172744b7479bSRandall Stewart 			 * would proceed through and bring up the assoc.. we
172844b7479bSRandall Stewart 			 * may end up with the wrong stream setup however..
172944b7479bSRandall Stewart 			 * which would be bad.. but there is no way to
173044b7479bSRandall Stewart 			 * tell.. until we send on a stream that does not
173144b7479bSRandall Stewart 			 * exist :-)
173244b7479bSRandall Stewart 			 */
173344b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
173444b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 7;
173544b7479bSRandall Stewart 
173644b7479bSRandall Stewart 			return (NULL);
173744b7479bSRandall Stewart 		}
173844b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
173944b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 8;
1740b7d130beSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1741b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1742f8829a4aSRandall Stewart 		sctp_stop_all_cookie_timers(stcb);
1743f8829a4aSRandall Stewart 		/*
1744f8829a4aSRandall Stewart 		 * since we did not send a HB make sure we don't double
1745f8829a4aSRandall Stewart 		 * things
1746f8829a4aSRandall Stewart 		 */
1747f8829a4aSRandall Stewart 		net->hb_responded = 1;
1748f8829a4aSRandall Stewart 		if (stcb->asoc.sctp_autoclose_ticks &&
1749f8829a4aSRandall Stewart 		    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1750f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1751f8829a4aSRandall Stewart 			    NULL);
1752f8829a4aSRandall Stewart 		}
1753f8829a4aSRandall Stewart 		asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
17547f34832bSRandall Stewart 		asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1755f8829a4aSRandall Stewart 
17567f34832bSRandall Stewart 		if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
17577f34832bSRandall Stewart 			/*
17587f34832bSRandall Stewart 			 * Ok the peer probably discarded our data (if we
17597f34832bSRandall Stewart 			 * echoed a cookie+data). So anything on the
17607f34832bSRandall Stewart 			 * sent_queue should be marked for retransmit, we
17617f34832bSRandall Stewart 			 * may not get something to kick us so it COULD
17627f34832bSRandall Stewart 			 * still take a timeout to move these.. but it can't
17637f34832bSRandall Stewart 			 * hurt to mark them.
17647f34832bSRandall Stewart 			 */
17657f34832bSRandall Stewart 			struct sctp_tmit_chunk *chk;
17667f34832bSRandall Stewart 
17677f34832bSRandall Stewart 			TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
17687f34832bSRandall Stewart 				if (chk->sent < SCTP_DATAGRAM_RESEND) {
17697f34832bSRandall Stewart 					chk->sent = SCTP_DATAGRAM_RESEND;
1770b54d3a6cSRandall Stewart 					sctp_flight_size_decrease(chk);
1771b54d3a6cSRandall Stewart 					sctp_total_flight_decrease(stcb, chk);
17725e54f665SRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
17737f34832bSRandall Stewart 					spec_flag++;
17747f34832bSRandall Stewart 				}
17757f34832bSRandall Stewart 			}
17767f34832bSRandall Stewart 
17777f34832bSRandall Stewart 		}
1778f8829a4aSRandall Stewart 		/* process the INIT info (peer's info) */
17797215cc1bSMichael Tuexen 		retval = sctp_process_init(init_cp, stcb);
1780f8829a4aSRandall Stewart 		if (retval < 0) {
178144b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
178244b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 9;
1783f8829a4aSRandall Stewart 			return (NULL);
1784f8829a4aSRandall Stewart 		}
17857215cc1bSMichael Tuexen 		if (sctp_load_addresses_from_init(stcb, m,
1786f8829a4aSRandall Stewart 		    init_offset + sizeof(struct sctp_init_chunk),
1787b1754ad1SMichael Tuexen 		    initack_offset, src, dst, init_src)) {
178844b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
178944b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 10;
1790f8829a4aSRandall Stewart 			return (NULL);
1791f8829a4aSRandall Stewart 		}
1792f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1793f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1794f8829a4aSRandall Stewart 			*notification = SCTP_NOTIFY_ASSOC_UP;
1795f8829a4aSRandall Stewart 
1796f8829a4aSRandall Stewart 			if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1797f8829a4aSRandall Stewart 			    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1798f8829a4aSRandall Stewart 			    (inp->sctp_socket->so_qlimit == 0)) {
1799ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1800ceaad40aSRandall Stewart 				struct socket *so;
1801ceaad40aSRandall Stewart 
1802ceaad40aSRandall Stewart #endif
1803f8829a4aSRandall Stewart 				stcb->sctp_ep->sctp_flags |=
1804f8829a4aSRandall Stewart 				    SCTP_PCB_FLAGS_CONNECTED;
1805ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1806ceaad40aSRandall Stewart 				so = SCTP_INP_SO(stcb->sctp_ep);
1807ceaad40aSRandall Stewart 				atomic_add_int(&stcb->asoc.refcnt, 1);
1808ceaad40aSRandall Stewart 				SCTP_TCB_UNLOCK(stcb);
1809ceaad40aSRandall Stewart 				SCTP_SOCKET_LOCK(so, 1);
1810ceaad40aSRandall Stewart 				SCTP_TCB_LOCK(stcb);
1811ceaad40aSRandall Stewart 				atomic_add_int(&stcb->asoc.refcnt, -1);
1812ceaad40aSRandall Stewart 				if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1813ceaad40aSRandall Stewart 					SCTP_SOCKET_UNLOCK(so, 1);
1814ceaad40aSRandall Stewart 					return (NULL);
1815ceaad40aSRandall Stewart 				}
1816ceaad40aSRandall Stewart #endif
1817ceaad40aSRandall Stewart 				soisconnected(stcb->sctp_socket);
1818ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1819ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
1820ceaad40aSRandall Stewart #endif
1821f8829a4aSRandall Stewart 			}
1822f42a358aSRandall Stewart 			if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1823f42a358aSRandall Stewart 				SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1824f42a358aSRandall Stewart 			else
1825f42a358aSRandall Stewart 				SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1826f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1827f42a358aSRandall Stewart 		} else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1828f42a358aSRandall Stewart 			SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1829f42a358aSRandall Stewart 		} else {
1830f42a358aSRandall Stewart 			SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1831f8829a4aSRandall Stewart 		}
1832c4739e2fSRandall Stewart 		SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1833f8829a4aSRandall Stewart 		if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1834f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1835f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, asoc->primary_destination);
1836f8829a4aSRandall Stewart 		}
1837f8829a4aSRandall Stewart 		sctp_stop_all_cookie_timers(stcb);
1838a5d547adSRandall Stewart 		sctp_toss_old_cookies(stcb, asoc);
1839f8829a4aSRandall Stewart 		sctp_send_cookie_ack(stcb);
18407f34832bSRandall Stewart 		if (spec_flag) {
18417f34832bSRandall Stewart 			/*
18427f34832bSRandall Stewart 			 * only if we have retrans set do we do this. What
18437f34832bSRandall Stewart 			 * this call does is get only the COOKIE-ACK out and
18447f34832bSRandall Stewart 			 * then when we return the normal call to
18457f34832bSRandall Stewart 			 * sctp_chunk_output will get the retrans out behind
18467f34832bSRandall Stewart 			 * this.
18477f34832bSRandall Stewart 			 */
1848ceaad40aSRandall Stewart 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
18497f34832bSRandall Stewart 		}
185044b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
185144b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 11;
185244b7479bSRandall Stewart 
1853f8829a4aSRandall Stewart 		return (stcb);
1854f8829a4aSRandall Stewart 	}
1855f8829a4aSRandall Stewart 	if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1856f8829a4aSRandall Stewart 	    ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1857f8829a4aSRandall Stewart 	    cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1858f8829a4aSRandall Stewart 	    cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1859f8829a4aSRandall Stewart 	    cookie->tie_tag_peer_vtag != 0) {
1860f8829a4aSRandall Stewart 		struct sctpasochead *head;
1861f8829a4aSRandall Stewart 
186256f778aaSMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
186356f778aaSMichael Tuexen 		struct socket *so;
186456f778aaSMichael Tuexen 
186556f778aaSMichael Tuexen #endif
186656f778aaSMichael Tuexen 
1867830d754dSRandall Stewart 		if (asoc->peer_supports_nat) {
1868830d754dSRandall Stewart 			/*
186956f778aaSMichael Tuexen 			 * This is a gross gross hack. Just call the
1870830d754dSRandall Stewart 			 * cookie_new code since we are allowing a duplicate
1871830d754dSRandall Stewart 			 * association. I hope this works...
1872830d754dSRandall Stewart 			 */
1873b1754ad1SMichael Tuexen 			return (sctp_process_cookie_new(m, iphlen, offset, src, dst,
1874b1754ad1SMichael Tuexen 			    sh, cookie, cookie_len,
1875830d754dSRandall Stewart 			    inp, netp, init_src, notification,
1876830d754dSRandall Stewart 			    auth_skipped, auth_offset, auth_len,
1877457b4b88SMichael Tuexen 			    mflowtype, mflowid,
1878830d754dSRandall Stewart 			    vrf_id, port));
1879830d754dSRandall Stewart 		}
1880f8829a4aSRandall Stewart 		/*
1881f8829a4aSRandall Stewart 		 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1882f8829a4aSRandall Stewart 		 */
1883a5d547adSRandall Stewart 		/* temp code */
188444b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
188544b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 12;
1886b7d130beSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net,
1887b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1888b7d130beSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1889b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
1890139bc87fSRandall Stewart 
1891f8829a4aSRandall Stewart 		/* notify upper layer */
1892f8829a4aSRandall Stewart 		*notification = SCTP_NOTIFY_ASSOC_RESTART;
1893a5d547adSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
1894f42a358aSRandall Stewart 		if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) &&
1895f42a358aSRandall Stewart 		    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1896f42a358aSRandall Stewart 		    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
1897f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1898f42a358aSRandall Stewart 		}
1899f42a358aSRandall Stewart 		if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1900f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1901f42a358aSRandall Stewart 		} else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1902f42a358aSRandall Stewart 			SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1903f42a358aSRandall Stewart 		}
1904139bc87fSRandall Stewart 		if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1905c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1906139bc87fSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1907139bc87fSRandall Stewart 			    stcb->sctp_ep, stcb, asoc->primary_destination);
1908139bc87fSRandall Stewart 
1909139bc87fSRandall Stewart 		} else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1910139bc87fSRandall Stewart 			/* move to OPEN state, if not in SHUTDOWN_SENT */
1911c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1912139bc87fSRandall Stewart 		}
1913139bc87fSRandall Stewart 		asoc->pre_open_streams =
1914139bc87fSRandall Stewart 		    ntohs(initack_cp->init.num_outbound_streams);
1915139bc87fSRandall Stewart 		asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1916139bc87fSRandall Stewart 		asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1917c54a18d2SRandall Stewart 		asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1918139bc87fSRandall Stewart 
1919139bc87fSRandall Stewart 		asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1920139bc87fSRandall Stewart 
1921139bc87fSRandall Stewart 		asoc->str_reset_seq_in = asoc->init_seq_number;
1922139bc87fSRandall Stewart 
1923139bc87fSRandall Stewart 		asoc->advanced_peer_ack_point = asoc->last_acked_seq;
19240696e120SRandall Stewart 		if (asoc->mapping_array) {
1925139bc87fSRandall Stewart 			memset(asoc->mapping_array, 0,
1926139bc87fSRandall Stewart 			    asoc->mapping_array_size);
19270696e120SRandall Stewart 		}
192877acdc25SRandall Stewart 		if (asoc->nr_mapping_array) {
1929830d754dSRandall Stewart 			memset(asoc->nr_mapping_array, 0,
1930b5c16493SMichael Tuexen 			    asoc->mapping_array_size);
1931830d754dSRandall Stewart 		}
1932a5d547adSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
193356f778aaSMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
193456f778aaSMichael Tuexen 		so = SCTP_INP_SO(stcb->sctp_ep);
193556f778aaSMichael Tuexen 		SCTP_SOCKET_LOCK(so, 1);
193656f778aaSMichael Tuexen #endif
1937a5d547adSRandall Stewart 		SCTP_INP_INFO_WLOCK();
1938a5d547adSRandall Stewart 		SCTP_INP_WLOCK(stcb->sctp_ep);
1939a5d547adSRandall Stewart 		SCTP_TCB_LOCK(stcb);
1940a5d547adSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, -1);
1941f8829a4aSRandall Stewart 		/* send up all the data */
19427f34832bSRandall Stewart 		SCTP_TCB_SEND_LOCK(stcb);
1943f8829a4aSRandall Stewart 
194456f778aaSMichael Tuexen 		sctp_report_all_outbound(stcb, 0, 1, SCTP_SO_LOCKED);
1945a5d547adSRandall Stewart 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1946325c8c46SMichael Tuexen 			stcb->asoc.strmout[i].chunks_on_queues = 0;
1947f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
1948f0396ad1SMichael Tuexen 			for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1949f0396ad1SMichael Tuexen 				asoc->strmout[i].abandoned_sent[j] = 0;
1950f0396ad1SMichael Tuexen 				asoc->strmout[i].abandoned_unsent[j] = 0;
1951f0396ad1SMichael Tuexen 			}
1952f0396ad1SMichael Tuexen #else
1953f0396ad1SMichael Tuexen 			asoc->strmout[i].abandoned_sent[0] = 0;
1954f0396ad1SMichael Tuexen 			asoc->strmout[i].abandoned_unsent[0] = 0;
1955f0396ad1SMichael Tuexen #endif
1956a5d547adSRandall Stewart 			stcb->asoc.strmout[i].stream_no = i;
1957f3b05218SMichael Tuexen 			stcb->asoc.strmout[i].next_sequence_send = 0;
1958a5d547adSRandall Stewart 			stcb->asoc.strmout[i].last_msg_incomplete = 0;
1959a5d547adSRandall Stewart 		}
1960f8829a4aSRandall Stewart 		/* process the INIT-ACK info (my info) */
1961f8829a4aSRandall Stewart 		asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1962f8829a4aSRandall Stewart 		asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1963f8829a4aSRandall Stewart 
1964f8829a4aSRandall Stewart 		/* pull from vtag hash */
1965f8829a4aSRandall Stewart 		LIST_REMOVE(stcb, sctp_asocs);
1966f8829a4aSRandall Stewart 		/* re-insert to new vtag position */
1967b3f1ea41SRandall Stewart 		head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
1968b3f1ea41SRandall Stewart 		    SCTP_BASE_INFO(hashasocmark))];
1969f8829a4aSRandall Stewart 		/*
1970f8829a4aSRandall Stewart 		 * put it in the bucket in the vtag hash of assoc's for the
1971f8829a4aSRandall Stewart 		 * system
1972f8829a4aSRandall Stewart 		 */
1973f8829a4aSRandall Stewart 		LIST_INSERT_HEAD(head, stcb, sctp_asocs);
1974f8829a4aSRandall Stewart 
19757f34832bSRandall Stewart 		SCTP_TCB_SEND_UNLOCK(stcb);
1976a5d547adSRandall Stewart 		SCTP_INP_WUNLOCK(stcb->sctp_ep);
1977a5d547adSRandall Stewart 		SCTP_INP_INFO_WUNLOCK();
197856f778aaSMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
197956f778aaSMichael Tuexen 		SCTP_SOCKET_UNLOCK(so, 1);
198056f778aaSMichael Tuexen #endif
198156f778aaSMichael Tuexen 		asoc->total_flight = 0;
198256f778aaSMichael Tuexen 		asoc->total_flight_count = 0;
198356f778aaSMichael Tuexen 		/* process the INIT info (peer's info) */
19847215cc1bSMichael Tuexen 		retval = sctp_process_init(init_cp, stcb);
1985f8829a4aSRandall Stewart 		if (retval < 0) {
198644b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
198744b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 13;
198844b7479bSRandall Stewart 
1989f8829a4aSRandall Stewart 			return (NULL);
1990f8829a4aSRandall Stewart 		}
1991f8829a4aSRandall Stewart 		/*
1992f8829a4aSRandall Stewart 		 * since we did not send a HB make sure we don't double
1993f8829a4aSRandall Stewart 		 * things
1994f8829a4aSRandall Stewart 		 */
1995f8829a4aSRandall Stewart 		net->hb_responded = 1;
1996f8829a4aSRandall Stewart 
19977215cc1bSMichael Tuexen 		if (sctp_load_addresses_from_init(stcb, m,
1998f8829a4aSRandall Stewart 		    init_offset + sizeof(struct sctp_init_chunk),
1999b1754ad1SMichael Tuexen 		    initack_offset, src, dst, init_src)) {
200044b7479bSRandall Stewart 			if (how_indx < sizeof(asoc->cookie_how))
200144b7479bSRandall Stewart 				asoc->cookie_how[how_indx] = 14;
200244b7479bSRandall Stewart 
2003f8829a4aSRandall Stewart 			return (NULL);
2004f8829a4aSRandall Stewart 		}
2005f8829a4aSRandall Stewart 		/* respond with a COOKIE-ACK */
2006f8829a4aSRandall Stewart 		sctp_stop_all_cookie_timers(stcb);
2007a5d547adSRandall Stewart 		sctp_toss_old_cookies(stcb, asoc);
2008f8829a4aSRandall Stewart 		sctp_send_cookie_ack(stcb);
200944b7479bSRandall Stewart 		if (how_indx < sizeof(asoc->cookie_how))
201044b7479bSRandall Stewart 			asoc->cookie_how[how_indx] = 15;
2011f8829a4aSRandall Stewart 
2012f8829a4aSRandall Stewart 		return (stcb);
2013f8829a4aSRandall Stewart 	}
201444b7479bSRandall Stewart 	if (how_indx < sizeof(asoc->cookie_how))
201544b7479bSRandall Stewart 		asoc->cookie_how[how_indx] = 16;
2016f8829a4aSRandall Stewart 	/* all other cases... */
2017f8829a4aSRandall Stewart 	return (NULL);
2018f8829a4aSRandall Stewart }
2019f8829a4aSRandall Stewart 
202093164cf9SRandall Stewart 
2021f8829a4aSRandall Stewart /*
2022f8829a4aSRandall Stewart  * handle a state cookie for a new association m: input packet mbuf chain--
2023f8829a4aSRandall Stewart  * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
2024f8829a4aSRandall Stewart  * and the cookie signature does not exist offset: offset into mbuf to the
2025f8829a4aSRandall Stewart  * cookie-echo chunk length: length of the cookie chunk to: where the init
2026f8829a4aSRandall Stewart  * was from returns a new TCB
2027f8829a4aSRandall Stewart  */
2028f30ac432SMichael Tuexen static struct sctp_tcb *
2029f8829a4aSRandall Stewart sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
2030b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
2031f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
2032f8829a4aSRandall Stewart     struct sctp_inpcb *inp, struct sctp_nets **netp,
2033f8829a4aSRandall Stewart     struct sockaddr *init_src, int *notification,
203417205eccSRandall Stewart     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2035457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
2036c54a18d2SRandall Stewart     uint32_t vrf_id, uint16_t port)
2037f8829a4aSRandall Stewart {
2038f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
2039f8829a4aSRandall Stewart 	struct sctp_init_chunk *init_cp, init_buf;
2040f8829a4aSRandall Stewart 	struct sctp_init_ack_chunk *initack_cp, initack_buf;
204124aaac8dSMichael Tuexen 	union sctp_sockstore store;
2042f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2043f8829a4aSRandall Stewart 	int init_offset, initack_offset, initack_limit;
2044f8829a4aSRandall Stewart 	int retval;
2045f8829a4aSRandall Stewart 	int error = 0;
2046f42a358aSRandall Stewart 	uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
2047f8829a4aSRandall Stewart 
2048ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2049ceaad40aSRandall Stewart 	struct socket *so;
2050ceaad40aSRandall Stewart 
2051ceaad40aSRandall Stewart 	so = SCTP_INP_SO(inp);
2052ceaad40aSRandall Stewart #endif
2053ceaad40aSRandall Stewart 
2054f8829a4aSRandall Stewart 	/*
2055f8829a4aSRandall Stewart 	 * find and validate the INIT chunk in the cookie (peer's info) the
2056f8829a4aSRandall Stewart 	 * INIT should start after the cookie-echo header struct (chunk
2057f8829a4aSRandall Stewart 	 * header, state cookie header struct)
2058f8829a4aSRandall Stewart 	 */
2059f8829a4aSRandall Stewart 	init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
2060f8829a4aSRandall Stewart 	init_cp = (struct sctp_init_chunk *)
2061f8829a4aSRandall Stewart 	    sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
2062f8829a4aSRandall Stewart 	    (uint8_t *) & init_buf);
2063f8829a4aSRandall Stewart 	if (init_cp == NULL) {
2064f8829a4aSRandall Stewart 		/* could not pull a INIT chunk in cookie */
2065ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1,
2066ad81507eSRandall Stewart 		    "process_cookie_new: could not pull INIT chunk hdr\n");
2067f8829a4aSRandall Stewart 		return (NULL);
2068f8829a4aSRandall Stewart 	}
2069f8829a4aSRandall Stewart 	if (init_cp->ch.chunk_type != SCTP_INITIATION) {
2070ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
2071f8829a4aSRandall Stewart 		return (NULL);
2072f8829a4aSRandall Stewart 	}
207360990c0cSMichael Tuexen 	initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
2074f8829a4aSRandall Stewart 	/*
2075f8829a4aSRandall Stewart 	 * find and validate the INIT-ACK chunk in the cookie (my info) the
2076f8829a4aSRandall Stewart 	 * INIT-ACK follows the INIT chunk
2077f8829a4aSRandall Stewart 	 */
2078f8829a4aSRandall Stewart 	initack_cp = (struct sctp_init_ack_chunk *)
2079f8829a4aSRandall Stewart 	    sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
2080f8829a4aSRandall Stewart 	    (uint8_t *) & initack_buf);
2081f8829a4aSRandall Stewart 	if (initack_cp == NULL) {
2082f8829a4aSRandall Stewart 		/* could not pull INIT-ACK chunk in cookie */
2083ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2084f8829a4aSRandall Stewart 		return (NULL);
2085f8829a4aSRandall Stewart 	}
2086f8829a4aSRandall Stewart 	if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2087f8829a4aSRandall Stewart 		return (NULL);
2088f8829a4aSRandall Stewart 	}
2089f8829a4aSRandall Stewart 	/*
2090f8829a4aSRandall Stewart 	 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2091f8829a4aSRandall Stewart 	 * "initack_limit" value.  This is because the chk_length field
2092f8829a4aSRandall Stewart 	 * includes the length of the cookie, but the cookie is omitted when
2093f8829a4aSRandall Stewart 	 * the INIT and INIT_ACK are tacked onto the cookie...
2094f8829a4aSRandall Stewart 	 */
2095f8829a4aSRandall Stewart 	initack_limit = offset + cookie_len;
2096f8829a4aSRandall Stewart 
2097f8829a4aSRandall Stewart 	/*
2098f8829a4aSRandall Stewart 	 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2099f8829a4aSRandall Stewart 	 * and popluate
2100f8829a4aSRandall Stewart 	 */
210152be287eSRandall Stewart 
210252be287eSRandall Stewart 	/*
210352be287eSRandall Stewart 	 * Here we do a trick, we set in NULL for the proc/thread argument.
210452be287eSRandall Stewart 	 * We do this since in effect we only use the p argument when the
210552be287eSRandall Stewart 	 * socket is unbound and we must do an implicit bind. Since we are
210652be287eSRandall Stewart 	 * getting a cookie, we cannot be unbound.
210752be287eSRandall Stewart 	 */
2108b5c16493SMichael Tuexen 	stcb = sctp_aloc_assoc(inp, init_src, &error,
210952be287eSRandall Stewart 	    ntohl(initack_cp->init.initiate_tag), vrf_id,
2110c979034bSMichael Tuexen 	    ntohs(initack_cp->init.num_outbound_streams),
211152be287eSRandall Stewart 	    (struct thread *)NULL
211252be287eSRandall Stewart 	    );
2113f8829a4aSRandall Stewart 	if (stcb == NULL) {
2114f8829a4aSRandall Stewart 		struct mbuf *op_err;
2115f8829a4aSRandall Stewart 
2116f8829a4aSRandall Stewart 		/* memory problem? */
2117ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1,
2118ad81507eSRandall Stewart 		    "process_cookie_new: no room for another TCB!\n");
2119ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2120f8829a4aSRandall Stewart 		sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2121b1754ad1SMichael Tuexen 		    src, dst, sh, op_err,
2122457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2123f30ac432SMichael Tuexen 		    vrf_id, port);
2124f8829a4aSRandall Stewart 		return (NULL);
2125f8829a4aSRandall Stewart 	}
2126f8829a4aSRandall Stewart 	/* get the correct sctp_nets */
2127ad81507eSRandall Stewart 	if (netp)
2128f8829a4aSRandall Stewart 		*netp = sctp_findnet(stcb, init_src);
2129ad81507eSRandall Stewart 
2130f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2131f8829a4aSRandall Stewart 	/* get scope variables out of cookie */
2132a1cb341bSMichael Tuexen 	asoc->scope.ipv4_local_scope = cookie->ipv4_scope;
2133a1cb341bSMichael Tuexen 	asoc->scope.site_scope = cookie->site_scope;
2134a1cb341bSMichael Tuexen 	asoc->scope.local_scope = cookie->local_scope;
2135a1cb341bSMichael Tuexen 	asoc->scope.loopback_scope = cookie->loopback_scope;
2136f8829a4aSRandall Stewart 
2137a1cb341bSMichael Tuexen 	if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2138a1cb341bSMichael Tuexen 	    (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2139f8829a4aSRandall Stewart 		struct mbuf *op_err;
2140f8829a4aSRandall Stewart 
2141f8829a4aSRandall Stewart 		/*
2142f8829a4aSRandall Stewart 		 * Houston we have a problem. The EP changed while the
2143f8829a4aSRandall Stewart 		 * cookie was in flight. Only recourse is to abort the
2144f8829a4aSRandall Stewart 		 * association.
2145f8829a4aSRandall Stewart 		 */
21464c9179adSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
2147ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2148f8829a4aSRandall Stewart 		sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2149b1754ad1SMichael Tuexen 		    src, dst, sh, op_err,
2150457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2151f30ac432SMichael Tuexen 		    vrf_id, port);
2152ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2153ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
2154ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
2155ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
2156ceaad40aSRandall Stewart #endif
2157c4739e2fSRandall Stewart 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2158b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2159ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2160ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
2161ceaad40aSRandall Stewart #endif
2162ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
2163f8829a4aSRandall Stewart 		return (NULL);
2164f8829a4aSRandall Stewart 	}
2165f8829a4aSRandall Stewart 	/* process the INIT-ACK info (my info) */
2166830d754dSRandall Stewart 	asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2167f8829a4aSRandall Stewart 	asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2168f8829a4aSRandall Stewart 	asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2169f8829a4aSRandall Stewart 	asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2170f8829a4aSRandall Stewart 	asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2171c54a18d2SRandall Stewart 	asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2172f8829a4aSRandall Stewart 	asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2173f8829a4aSRandall Stewart 	asoc->str_reset_seq_in = asoc->init_seq_number;
2174f8829a4aSRandall Stewart 
2175f8829a4aSRandall Stewart 	asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2176f8829a4aSRandall Stewart 
2177f8829a4aSRandall Stewart 	/* process the INIT info (peer's info) */
2178ad81507eSRandall Stewart 	if (netp)
21797215cc1bSMichael Tuexen 		retval = sctp_process_init(init_cp, stcb);
2180ad81507eSRandall Stewart 	else
2181ad81507eSRandall Stewart 		retval = 0;
2182f8829a4aSRandall Stewart 	if (retval < 0) {
21834c9179adSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
2184ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2185ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
2186ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
2187ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
2188ceaad40aSRandall Stewart #endif
2189b7d130beSMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2190b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2191ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2192ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
2193ceaad40aSRandall Stewart #endif
2194ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
2195f8829a4aSRandall Stewart 		return (NULL);
2196f8829a4aSRandall Stewart 	}
2197f8829a4aSRandall Stewart 	/* load all addresses */
21987215cc1bSMichael Tuexen 	if (sctp_load_addresses_from_init(stcb, m,
2199b1754ad1SMichael Tuexen 	    init_offset + sizeof(struct sctp_init_chunk), initack_offset,
2200b1754ad1SMichael Tuexen 	    src, dst, init_src)) {
22014c9179adSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
2202ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2203ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
2204ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
2205ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
2206ceaad40aSRandall Stewart #endif
2207b7d130beSMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2208b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2209ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2210ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
2211ceaad40aSRandall Stewart #endif
2212ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
2213f8829a4aSRandall Stewart 		return (NULL);
2214f8829a4aSRandall Stewart 	}
2215f8829a4aSRandall Stewart 	/*
2216f8829a4aSRandall Stewart 	 * verify any preceding AUTH chunk that was skipped
2217f8829a4aSRandall Stewart 	 */
2218f8829a4aSRandall Stewart 	/* pull the local authentication parameters from the cookie/init-ack */
2219f8829a4aSRandall Stewart 	sctp_auth_get_cookie_params(stcb, m,
2220f8829a4aSRandall Stewart 	    initack_offset + sizeof(struct sctp_init_ack_chunk),
2221f8829a4aSRandall Stewart 	    initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2222f8829a4aSRandall Stewart 	if (auth_skipped) {
2223f8829a4aSRandall Stewart 		struct sctp_auth_chunk *auth;
2224f8829a4aSRandall Stewart 
2225f8829a4aSRandall Stewart 		auth = (struct sctp_auth_chunk *)
2226f42a358aSRandall Stewart 		    sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2227ad81507eSRandall Stewart 		if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2228f8829a4aSRandall Stewart 			/* auth HMAC failed, dump the assoc and packet */
2229ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_AUTH1,
2230ad81507eSRandall Stewart 			    "COOKIE-ECHO: AUTH failed\n");
2231b54d3a6cSRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, 1);
2232ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2233ceaad40aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
2234ceaad40aSRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
2235ceaad40aSRandall Stewart 			SCTP_TCB_LOCK(stcb);
2236ceaad40aSRandall Stewart #endif
2237b7d130beSMichael Tuexen 			(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2238b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_21);
2239ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2240ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
2241ceaad40aSRandall Stewart #endif
2242ceaad40aSRandall Stewart 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
2243f8829a4aSRandall Stewart 			return (NULL);
2244f8829a4aSRandall Stewart 		} else {
2245f8829a4aSRandall Stewart 			/* remaining chunks checked... good to go */
2246f8829a4aSRandall Stewart 			stcb->asoc.authenticated = 1;
2247f8829a4aSRandall Stewart 		}
2248f8829a4aSRandall Stewart 	}
2249f8829a4aSRandall Stewart 	/* update current state */
2250ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2251c4739e2fSRandall Stewart 	SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2252f8829a4aSRandall Stewart 	if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2253f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2254f8829a4aSRandall Stewart 		    stcb->sctp_ep, stcb, asoc->primary_destination);
2255f8829a4aSRandall Stewart 	}
2256a5d547adSRandall Stewart 	sctp_stop_all_cookie_timers(stcb);
2257f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2258f8829a4aSRandall Stewart 	SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2259f8829a4aSRandall Stewart 
2260f8829a4aSRandall Stewart 	/*
2261f8829a4aSRandall Stewart 	 * if we're doing ASCONFs, check to see if we have any new local
2262f8829a4aSRandall Stewart 	 * addresses that need to get added to the peer (eg. addresses
2263f8829a4aSRandall Stewart 	 * changed while cookie echo in flight).  This needs to be done
2264f8829a4aSRandall Stewart 	 * after we go to the OPEN state to do the correct asconf
2265f8829a4aSRandall Stewart 	 * processing. else, make sure we have the correct addresses in our
2266f8829a4aSRandall Stewart 	 * lists
2267f8829a4aSRandall Stewart 	 */
2268f8829a4aSRandall Stewart 
2269f8829a4aSRandall Stewart 	/* warning, we re-use sin, sin6, sa_store here! */
2270f8829a4aSRandall Stewart 	/* pull in local_address (our "from" address) */
2271e6194c2eSMichael Tuexen 	switch (cookie->laddr_type) {
2272e6194c2eSMichael Tuexen #ifdef INET
2273e6194c2eSMichael Tuexen 	case SCTP_IPV4_ADDRESS:
2274f8829a4aSRandall Stewart 		/* source addr is IPv4 */
227524aaac8dSMichael Tuexen 		memset(&store.sin, 0, sizeof(struct sockaddr_in));
227624aaac8dSMichael Tuexen 		store.sin.sin_family = AF_INET;
227724aaac8dSMichael Tuexen 		store.sin.sin_len = sizeof(struct sockaddr_in);
227824aaac8dSMichael Tuexen 		store.sin.sin_addr.s_addr = cookie->laddress[0];
2279e6194c2eSMichael Tuexen 		break;
2280e6194c2eSMichael Tuexen #endif
2281e6194c2eSMichael Tuexen #ifdef INET6
2282e6194c2eSMichael Tuexen 	case SCTP_IPV6_ADDRESS:
2283f8829a4aSRandall Stewart 		/* source addr is IPv6 */
228424aaac8dSMichael Tuexen 		memset(&store.sin6, 0, sizeof(struct sockaddr_in6));
228524aaac8dSMichael Tuexen 		store.sin6.sin6_family = AF_INET6;
228624aaac8dSMichael Tuexen 		store.sin6.sin6_len = sizeof(struct sockaddr_in6);
228724aaac8dSMichael Tuexen 		store.sin6.sin6_scope_id = cookie->scope_id;
228823602b60SMichael Tuexen 		memcpy(&store.sin6.sin6_addr, cookie->laddress, sizeof(struct in6_addr));
2289e6194c2eSMichael Tuexen 		break;
2290e6194c2eSMichael Tuexen #endif
2291e6194c2eSMichael Tuexen 	default:
22924c9179adSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
2293ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2294ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
2295ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
2296ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
2297ceaad40aSRandall Stewart #endif
2298b7d130beSMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2299b7d130beSMichael Tuexen 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
2300ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2301ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
2302ceaad40aSRandall Stewart #endif
2303ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
2304f8829a4aSRandall Stewart 		return (NULL);
2305f8829a4aSRandall Stewart 	}
2306f8829a4aSRandall Stewart 
2307f8829a4aSRandall Stewart 	/* set up to notify upper layer */
2308f8829a4aSRandall Stewart 	*notification = SCTP_NOTIFY_ASSOC_UP;
2309f8829a4aSRandall Stewart 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2310f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2311f8829a4aSRandall Stewart 	    (inp->sctp_socket->so_qlimit == 0)) {
2312f8829a4aSRandall Stewart 		/*
2313f8829a4aSRandall Stewart 		 * This is an endpoint that called connect() how it got a
2314f8829a4aSRandall Stewart 		 * cookie that is NEW is a bit of a mystery. It must be that
2315f8829a4aSRandall Stewart 		 * the INIT was sent, but before it got there.. a complete
2316f8829a4aSRandall Stewart 		 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2317f8829a4aSRandall Stewart 		 * should have went to the other code.. not here.. oh well..
2318f8829a4aSRandall Stewart 		 * a bit of protection is worth having..
2319f8829a4aSRandall Stewart 		 */
2320f8829a4aSRandall Stewart 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2321ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2322ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
2323ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
2324ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
2325ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
2326ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
2327ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2328ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
2329ceaad40aSRandall Stewart 			return (NULL);
2330ceaad40aSRandall Stewart 		}
2331ceaad40aSRandall Stewart #endif
2332ceaad40aSRandall Stewart 		soisconnected(stcb->sctp_socket);
2333ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2334ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
2335ceaad40aSRandall Stewart #endif
2336f8829a4aSRandall Stewart 	} else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2337f8829a4aSRandall Stewart 	    (inp->sctp_socket->so_qlimit)) {
2338f8829a4aSRandall Stewart 		/*
2339f8829a4aSRandall Stewart 		 * We don't want to do anything with this one. Since it is
2340f8829a4aSRandall Stewart 		 * the listening guy. The timer will get started for
2341f8829a4aSRandall Stewart 		 * accepted connections in the caller.
2342f8829a4aSRandall Stewart 		 */
2343f8829a4aSRandall Stewart 		;
2344f8829a4aSRandall Stewart 	}
2345f8829a4aSRandall Stewart 	/* since we did not send a HB make sure we don't double things */
2346ad81507eSRandall Stewart 	if ((netp) && (*netp))
2347f8829a4aSRandall Stewart 		(*netp)->hb_responded = 1;
2348f8829a4aSRandall Stewart 
2349f8829a4aSRandall Stewart 	if (stcb->asoc.sctp_autoclose_ticks &&
2350f8829a4aSRandall Stewart 	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2351f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2352f8829a4aSRandall Stewart 	}
2353b54d3a6cSRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
235460862d8eSMichael Tuexen 	if ((netp != NULL) && (*netp != NULL)) {
2355fcbbf5afSMichael Tuexen 		/* calculate the RTT and set the encaps port */
2356a5d547adSRandall Stewart 		(*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
2357899288aeSRandall Stewart 		    &cookie->time_entered, sctp_align_unsafe_makecopy,
2358f79aab18SRandall Stewart 		    SCTP_RTT_FROM_NON_DATA);
235960862d8eSMichael Tuexen #if defined(INET) || defined(INET6)
236060862d8eSMichael Tuexen 		if (((*netp)->port == 0) && (port != 0)) {
23619a8e3088SMichael Tuexen 			sctp_pathmtu_adjustment(stcb, (uint16_t) ((*netp)->mtu - sizeof(struct udphdr)));
236260862d8eSMichael Tuexen 		}
2363fcbbf5afSMichael Tuexen 		(*netp)->port = port;
236460862d8eSMichael Tuexen #endif
23659a972525SRandall Stewart 	}
23663232788eSRandall Stewart 	/* respond with a COOKIE-ACK */
2367f8829a4aSRandall Stewart 	sctp_send_cookie_ack(stcb);
23683232788eSRandall Stewart 
23693232788eSRandall Stewart 	/*
23703232788eSRandall Stewart 	 * check the address lists for any ASCONFs that need to be sent
23713232788eSRandall Stewart 	 * AFTER the cookie-ack is sent
23723232788eSRandall Stewart 	 */
23733232788eSRandall Stewart 	sctp_check_address_list(stcb, m,
23743232788eSRandall Stewart 	    initack_offset + sizeof(struct sctp_init_ack_chunk),
23753232788eSRandall Stewart 	    initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
237624aaac8dSMichael Tuexen 	    &store.sa, cookie->local_scope, cookie->site_scope,
23773232788eSRandall Stewart 	    cookie->ipv4_scope, cookie->loopback_scope);
23783232788eSRandall Stewart 
23793232788eSRandall Stewart 
2380f8829a4aSRandall Stewart 	return (stcb);
2381f8829a4aSRandall Stewart }
2382f8829a4aSRandall Stewart 
2383830d754dSRandall Stewart /*
2384830d754dSRandall Stewart  * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2385830d754dSRandall Stewart  * we NEED to make sure we are not already using the vtag. If so we
2386830d754dSRandall Stewart  * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2387830d754dSRandall Stewart 	head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2388830d754dSRandall Stewart 							    SCTP_BASE_INFO(hashasocmark))];
2389830d754dSRandall Stewart 	LIST_FOREACH(stcb, head, sctp_asocs) {
2390830d754dSRandall Stewart 	        if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep))  {
2391830d754dSRandall Stewart 		       -- SEND ABORT - TRY AGAIN --
2392830d754dSRandall Stewart 		}
2393830d754dSRandall Stewart 	}
2394830d754dSRandall Stewart */
2395f8829a4aSRandall Stewart 
2396f8829a4aSRandall Stewart /*
2397f8829a4aSRandall Stewart  * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2398f8829a4aSRandall Stewart  * existing (non-NULL) TCB
2399f8829a4aSRandall Stewart  */
2400f8829a4aSRandall Stewart static struct mbuf *
2401f8829a4aSRandall Stewart sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2402b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
2403f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2404f8829a4aSRandall Stewart     struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
240517205eccSRandall Stewart     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2406f30ac432SMichael Tuexen     struct sctp_tcb **locked_tcb,
2407457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
2408f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
2409f8829a4aSRandall Stewart {
2410f8829a4aSRandall Stewart 	struct sctp_state_cookie *cookie;
2411f8829a4aSRandall Stewart 	struct sctp_tcb *l_stcb = *stcb;
2412f8829a4aSRandall Stewart 	struct sctp_inpcb *l_inp;
2413f8829a4aSRandall Stewart 	struct sockaddr *to;
2414f8829a4aSRandall Stewart 	struct sctp_pcb *ep;
2415f8829a4aSRandall Stewart 	struct mbuf *m_sig;
2416f8829a4aSRandall Stewart 	uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2417f8829a4aSRandall Stewart 	uint8_t *sig;
2418f8829a4aSRandall Stewart 	uint8_t cookie_ok = 0;
2419329204ffSMichael Tuexen 	unsigned int sig_offset, cookie_offset;
2420f8829a4aSRandall Stewart 	unsigned int cookie_len;
2421f8829a4aSRandall Stewart 	struct timeval now;
2422f8829a4aSRandall Stewart 	struct timeval time_expires;
2423f8829a4aSRandall Stewart 	int notification = 0;
2424f8829a4aSRandall Stewart 	struct sctp_nets *netl;
2425f8829a4aSRandall Stewart 	int had_a_existing_tcb = 0;
24262fad0e55SMichael Tuexen 	int send_int_conf = 0;
2427f8829a4aSRandall Stewart 
2428e6194c2eSMichael Tuexen #ifdef INET
2429e6194c2eSMichael Tuexen 	struct sockaddr_in sin;
2430e6194c2eSMichael Tuexen 
2431e6194c2eSMichael Tuexen #endif
2432e6194c2eSMichael Tuexen #ifdef INET6
2433e6194c2eSMichael Tuexen 	struct sockaddr_in6 sin6;
2434e6194c2eSMichael Tuexen 
2435e6194c2eSMichael Tuexen #endif
2436e6194c2eSMichael Tuexen 
2437ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
2438ad81507eSRandall Stewart 	    "sctp_handle_cookie: handling COOKIE-ECHO\n");
2439f8829a4aSRandall Stewart 
2440f8829a4aSRandall Stewart 	if (inp_p == NULL) {
2441f8829a4aSRandall Stewart 		return (NULL);
2442f8829a4aSRandall Stewart 	}
2443f8829a4aSRandall Stewart 	cookie = &cp->cookie;
2444f8829a4aSRandall Stewart 	cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2445f8829a4aSRandall Stewart 	cookie_len = ntohs(cp->ch.chunk_length);
2446f8829a4aSRandall Stewart 
24473db4ea95SMichael Tuexen 	if ((cookie->peerport != sh->src_port) ||
24483db4ea95SMichael Tuexen 	    (cookie->myport != sh->dest_port) ||
2449f8829a4aSRandall Stewart 	    (cookie->my_vtag != sh->v_tag)) {
2450f8829a4aSRandall Stewart 		/*
2451f8829a4aSRandall Stewart 		 * invalid ports or bad tag.  Note that we always leave the
2452f8829a4aSRandall Stewart 		 * v_tag in the header in network order and when we stored
2453f8829a4aSRandall Stewart 		 * it in the my_vtag slot we also left it in network order.
245417205eccSRandall Stewart 		 * This maintains the match even though it may be in the
2455f8829a4aSRandall Stewart 		 * opposite byte order of the machine :->
2456f8829a4aSRandall Stewart 		 */
2457f8829a4aSRandall Stewart 		return (NULL);
2458f8829a4aSRandall Stewart 	}
2459329204ffSMichael Tuexen 	if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2460f8829a4aSRandall Stewart 	    sizeof(struct sctp_init_chunk) +
2461f8829a4aSRandall Stewart 	    sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2462329204ffSMichael Tuexen 		/* cookie too small */
2463f8829a4aSRandall Stewart 		return (NULL);
2464f8829a4aSRandall Stewart 	}
2465f8829a4aSRandall Stewart 	/*
2466f8829a4aSRandall Stewart 	 * split off the signature into its own mbuf (since it should not be
2467f8829a4aSRandall Stewart 	 * calculated in the sctp_hmac_m() call).
2468f8829a4aSRandall Stewart 	 */
2469f8829a4aSRandall Stewart 	sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2470eb1b1807SGleb Smirnoff 	m_sig = m_split(m, sig_offset, M_NOWAIT);
2471f8829a4aSRandall Stewart 	if (m_sig == NULL) {
2472f8829a4aSRandall Stewart 		/* out of memory or ?? */
2473f8829a4aSRandall Stewart 		return (NULL);
2474f8829a4aSRandall Stewart 	}
2475eadccaccSRandall Stewart #ifdef SCTP_MBUF_LOGGING
2476b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
24774be807c4SMichael Tuexen 		sctp_log_mbc(m_sig, SCTP_MBUF_SPLIT);
2478eadccaccSRandall Stewart 	}
2479eadccaccSRandall Stewart #endif
2480eadccaccSRandall Stewart 
2481f8829a4aSRandall Stewart 	/*
2482f8829a4aSRandall Stewart 	 * compute the signature/digest for the cookie
2483f8829a4aSRandall Stewart 	 */
2484f8829a4aSRandall Stewart 	ep = &(*inp_p)->sctp_ep;
2485f8829a4aSRandall Stewart 	l_inp = *inp_p;
2486f8829a4aSRandall Stewart 	if (l_stcb) {
2487f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(l_stcb);
2488f8829a4aSRandall Stewart 	}
2489f8829a4aSRandall Stewart 	SCTP_INP_RLOCK(l_inp);
2490f8829a4aSRandall Stewart 	if (l_stcb) {
2491f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(l_stcb);
2492f8829a4aSRandall Stewart 	}
2493f8829a4aSRandall Stewart 	/* which cookie is it? */
2494f8829a4aSRandall Stewart 	if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2495f8829a4aSRandall Stewart 	    (ep->current_secret_number != ep->last_secret_number)) {
2496f8829a4aSRandall Stewart 		/* it's the old cookie */
24976e55db54SRandall Stewart 		(void)sctp_hmac_m(SCTP_HMAC,
2498f8829a4aSRandall Stewart 		    (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
2499d00aff5dSRandall Stewart 		    SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2500f8829a4aSRandall Stewart 	} else {
2501f8829a4aSRandall Stewart 		/* it's the current cookie */
25026e55db54SRandall Stewart 		(void)sctp_hmac_m(SCTP_HMAC,
2503f8829a4aSRandall Stewart 		    (uint8_t *) ep->secret_key[(int)ep->current_secret_number],
2504d00aff5dSRandall Stewart 		    SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2505f8829a4aSRandall Stewart 	}
2506f8829a4aSRandall Stewart 	/* get the signature */
2507f8829a4aSRandall Stewart 	SCTP_INP_RUNLOCK(l_inp);
2508f8829a4aSRandall Stewart 	sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig);
2509f8829a4aSRandall Stewart 	if (sig == NULL) {
2510f8829a4aSRandall Stewart 		/* couldn't find signature */
251103b0b021SRandall Stewart 		sctp_m_freem(m_sig);
2512f8829a4aSRandall Stewart 		return (NULL);
2513f8829a4aSRandall Stewart 	}
2514f8829a4aSRandall Stewart 	/* compare the received digest with the computed digest */
2515f8829a4aSRandall Stewart 	if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2516f8829a4aSRandall Stewart 		/* try the old cookie? */
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 			/* compute digest with old */
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 			/* compare */
2524f8829a4aSRandall Stewart 			if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2525f8829a4aSRandall Stewart 				cookie_ok = 1;
2526f8829a4aSRandall Stewart 		}
2527f8829a4aSRandall Stewart 	} else {
2528f8829a4aSRandall Stewart 		cookie_ok = 1;
2529f8829a4aSRandall Stewart 	}
2530f8829a4aSRandall Stewart 
2531f8829a4aSRandall Stewart 	/*
2532f8829a4aSRandall Stewart 	 * Now before we continue we must reconstruct our mbuf so that
2533f8829a4aSRandall Stewart 	 * normal processing of any other chunks will work.
2534f8829a4aSRandall Stewart 	 */
2535f8829a4aSRandall Stewart 	{
2536f8829a4aSRandall Stewart 		struct mbuf *m_at;
2537f8829a4aSRandall Stewart 
2538f8829a4aSRandall Stewart 		m_at = m;
2539139bc87fSRandall Stewart 		while (SCTP_BUF_NEXT(m_at) != NULL) {
2540139bc87fSRandall Stewart 			m_at = SCTP_BUF_NEXT(m_at);
2541f8829a4aSRandall Stewart 		}
2542139bc87fSRandall Stewart 		SCTP_BUF_NEXT(m_at) = m_sig;
2543f8829a4aSRandall Stewart 	}
2544f8829a4aSRandall Stewart 
2545f8829a4aSRandall Stewart 	if (cookie_ok == 0) {
2546ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2547ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
2548ad81507eSRandall Stewart 		    "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2549f8829a4aSRandall Stewart 		    (uint32_t) offset, cookie_offset, sig_offset);
2550f8829a4aSRandall Stewart 		return (NULL);
2551f8829a4aSRandall Stewart 	}
2552f8829a4aSRandall Stewart 	/*
2553f8829a4aSRandall Stewart 	 * check the cookie timestamps to be sure it's not stale
2554f8829a4aSRandall Stewart 	 */
25556e55db54SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&now);
2556f8829a4aSRandall Stewart 	/* Expire time is in Ticks, so we convert to seconds */
25573c503c28SRandall Stewart 	time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2558f8829a4aSRandall Stewart 	time_expires.tv_usec = cookie->time_entered.tv_usec;
2559fc14de76SRandall Stewart 	/*
2560fc14de76SRandall Stewart 	 * TODO sctp_constants.h needs alternative time macros when _KERNEL
2561fc14de76SRandall Stewart 	 * is undefined.
2562fc14de76SRandall Stewart 	 */
2563f8829a4aSRandall Stewart 	if (timevalcmp(&now, &time_expires, >)) {
2564f8829a4aSRandall Stewart 		/* cookie is stale! */
2565f8829a4aSRandall Stewart 		struct mbuf *op_err;
256686eda749SMichael Tuexen 		struct sctp_error_stale_cookie *cause;
2567f8829a4aSRandall Stewart 		uint32_t tim;
2568f8829a4aSRandall Stewart 
256986eda749SMichael Tuexen 		op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_stale_cookie),
2570eb1b1807SGleb Smirnoff 		    0, M_NOWAIT, 1, MT_DATA);
2571f8829a4aSRandall Stewart 		if (op_err == NULL) {
2572f8829a4aSRandall Stewart 			/* FOOBAR */
2573f8829a4aSRandall Stewart 			return (NULL);
2574f8829a4aSRandall Stewart 		}
2575f8829a4aSRandall Stewart 		/* Set the len */
257686eda749SMichael Tuexen 		SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_stale_cookie);
257786eda749SMichael Tuexen 		cause = mtod(op_err, struct sctp_error_stale_cookie *);
257886eda749SMichael Tuexen 		cause->cause.code = htons(SCTP_CAUSE_STALE_COOKIE);
257986eda749SMichael Tuexen 		cause->cause.length = htons((sizeof(struct sctp_paramhdr) +
2580f8829a4aSRandall Stewart 		    (sizeof(uint32_t))));
2581f8829a4aSRandall Stewart 		/* seconds to usec */
2582f8829a4aSRandall Stewart 		tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
2583f8829a4aSRandall Stewart 		/* add in usec */
2584f8829a4aSRandall Stewart 		if (tim == 0)
2585f8829a4aSRandall Stewart 			tim = now.tv_usec - cookie->time_entered.tv_usec;
258686eda749SMichael Tuexen 		cause->stale_time = htonl(tim);
2587b1754ad1SMichael Tuexen 		sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
2588d089f9b9SMichael Tuexen 		    mflowtype, mflowid, l_inp->fibnum,
2589c54a18d2SRandall Stewart 		    vrf_id, port);
2590f8829a4aSRandall Stewart 		return (NULL);
2591f8829a4aSRandall Stewart 	}
2592f8829a4aSRandall Stewart 	/*
2593f8829a4aSRandall Stewart 	 * Now we must see with the lookup address if we have an existing
2594f8829a4aSRandall Stewart 	 * asoc. This will only happen if we were in the COOKIE-WAIT state
2595f8829a4aSRandall Stewart 	 * and a INIT collided with us and somewhere the peer sent the
2596f8829a4aSRandall Stewart 	 * cookie on another address besides the single address our assoc
2597f8829a4aSRandall Stewart 	 * had for him. In this case we will have one of the tie-tags set at
2598f8829a4aSRandall Stewart 	 * least AND the address field in the cookie can be used to look it
2599f8829a4aSRandall Stewart 	 * up.
2600f8829a4aSRandall Stewart 	 */
2601f8829a4aSRandall Stewart 	to = NULL;
2602e6194c2eSMichael Tuexen 	switch (cookie->addr_type) {
2603e6194c2eSMichael Tuexen #ifdef INET6
2604e6194c2eSMichael Tuexen 	case SCTP_IPV6_ADDRESS:
2605f8829a4aSRandall Stewart 		memset(&sin6, 0, sizeof(sin6));
2606f8829a4aSRandall Stewart 		sin6.sin6_family = AF_INET6;
2607f8829a4aSRandall Stewart 		sin6.sin6_len = sizeof(sin6);
2608f8829a4aSRandall Stewart 		sin6.sin6_port = sh->src_port;
2609f8829a4aSRandall Stewart 		sin6.sin6_scope_id = cookie->scope_id;
2610f8829a4aSRandall Stewart 		memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2611f8829a4aSRandall Stewart 		    sizeof(sin6.sin6_addr.s6_addr));
2612f8829a4aSRandall Stewart 		to = (struct sockaddr *)&sin6;
2613e6194c2eSMichael Tuexen 		break;
2614e6194c2eSMichael Tuexen #endif
2615e6194c2eSMichael Tuexen #ifdef INET
2616e6194c2eSMichael Tuexen 	case SCTP_IPV4_ADDRESS:
2617f8829a4aSRandall Stewart 		memset(&sin, 0, sizeof(sin));
2618f8829a4aSRandall Stewart 		sin.sin_family = AF_INET;
2619f8829a4aSRandall Stewart 		sin.sin_len = sizeof(sin);
2620f8829a4aSRandall Stewart 		sin.sin_port = sh->src_port;
2621f8829a4aSRandall Stewart 		sin.sin_addr.s_addr = cookie->address[0];
2622f8829a4aSRandall Stewart 		to = (struct sockaddr *)&sin;
2623e6194c2eSMichael Tuexen 		break;
2624e6194c2eSMichael Tuexen #endif
2625e6194c2eSMichael Tuexen 	default:
2626ad81507eSRandall Stewart 		/* This should not happen */
2627ad81507eSRandall Stewart 		return (NULL);
2628f8829a4aSRandall Stewart 	}
2629f0dc2113SMichael Tuexen 	if (*stcb == NULL) {
2630f8829a4aSRandall Stewart 		/* Yep, lets check */
2631b1754ad1SMichael Tuexen 		*stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
2632f8829a4aSRandall Stewart 		if (*stcb == NULL) {
2633f8829a4aSRandall Stewart 			/*
2634f8829a4aSRandall Stewart 			 * We should have only got back the same inp. If we
2635f8829a4aSRandall Stewart 			 * got back a different ep we have a problem. The
2636f8829a4aSRandall Stewart 			 * original findep got back l_inp and now
2637f8829a4aSRandall Stewart 			 */
2638f8829a4aSRandall Stewart 			if (l_inp != *inp_p) {
2639ad81507eSRandall Stewart 				SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2640f8829a4aSRandall Stewart 			}
2641a5d547adSRandall Stewart 		} else {
2642a5d547adSRandall Stewart 			if (*locked_tcb == NULL) {
2643a5d547adSRandall Stewart 				/*
2644a5d547adSRandall Stewart 				 * In this case we found the assoc only
2645a5d547adSRandall Stewart 				 * after we locked the create lock. This
2646a5d547adSRandall Stewart 				 * means we are in a colliding case and we
2647a5d547adSRandall Stewart 				 * must make sure that we unlock the tcb if
2648a5d547adSRandall Stewart 				 * its one of the cases where we throw away
2649a5d547adSRandall Stewart 				 * the incoming packets.
2650a5d547adSRandall Stewart 				 */
2651a5d547adSRandall Stewart 				*locked_tcb = *stcb;
2652a5d547adSRandall Stewart 
2653a5d547adSRandall Stewart 				/*
2654a5d547adSRandall Stewart 				 * We must also increment the inp ref count
2655a5d547adSRandall Stewart 				 * since the ref_count flags was set when we
2656a5d547adSRandall Stewart 				 * did not find the TCB, now we found it
2657a5d547adSRandall Stewart 				 * which reduces the refcount.. we must
2658a5d547adSRandall Stewart 				 * raise it back out to balance it all :-)
2659a5d547adSRandall Stewart 				 */
2660a5d547adSRandall Stewart 				SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2661a5d547adSRandall Stewart 				if ((*stcb)->sctp_ep != l_inp) {
2662ad81507eSRandall Stewart 					SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2663dd294dceSMichael Tuexen 					    (void *)(*stcb)->sctp_ep, (void *)l_inp);
2664a5d547adSRandall Stewart 				}
2665a5d547adSRandall Stewart 			}
2666f8829a4aSRandall Stewart 		}
2667f8829a4aSRandall Stewart 	}
2668f8829a4aSRandall Stewart 	cookie_len -= SCTP_SIGNATURE_SIZE;
2669f8829a4aSRandall Stewart 	if (*stcb == NULL) {
2670f8829a4aSRandall Stewart 		/* this is the "normal" case... get a new TCB */
2671b1754ad1SMichael Tuexen 		*stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
2672b1754ad1SMichael Tuexen 		    cookie, cookie_len, *inp_p,
2673b1754ad1SMichael Tuexen 		    netp, to, &notification,
2674f30ac432SMichael Tuexen 		    auth_skipped, auth_offset, auth_len,
2675457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2676f30ac432SMichael Tuexen 		    vrf_id, port);
2677f8829a4aSRandall Stewart 	} else {
2678f8829a4aSRandall Stewart 		/* this is abnormal... cookie-echo on existing TCB */
2679f8829a4aSRandall Stewart 		had_a_existing_tcb = 1;
2680b1754ad1SMichael Tuexen 		*stcb = sctp_process_cookie_existing(m, iphlen, offset,
2681b1754ad1SMichael Tuexen 		    src, dst, sh,
2682830d754dSRandall Stewart 		    cookie, cookie_len, *inp_p, *stcb, netp, to,
2683f30ac432SMichael Tuexen 		    &notification, auth_skipped, auth_offset, auth_len,
2684457b4b88SMichael Tuexen 		    mflowtype, mflowid,
2685f30ac432SMichael Tuexen 		    vrf_id, port);
2686f8829a4aSRandall Stewart 	}
2687f8829a4aSRandall Stewart 
2688f8829a4aSRandall Stewart 	if (*stcb == NULL) {
2689f8829a4aSRandall Stewart 		/* still no TCB... must be bad cookie-echo */
2690f8829a4aSRandall Stewart 		return (NULL);
2691f8829a4aSRandall Stewart 	}
26925fe29cdfSMichael Tuexen 	if (*netp != NULL) {
2693457b4b88SMichael Tuexen 		(*netp)->flowtype = mflowtype;
26945fe29cdfSMichael Tuexen 		(*netp)->flowid = mflowid;
2695a4ae38f1SMichael Tuexen 	}
2696f8829a4aSRandall Stewart 	/*
2697f8829a4aSRandall Stewart 	 * Ok, we built an association so confirm the address we sent the
2698f8829a4aSRandall Stewart 	 * INIT-ACK to.
2699f8829a4aSRandall Stewart 	 */
2700f8829a4aSRandall Stewart 	netl = sctp_findnet(*stcb, to);
2701f8829a4aSRandall Stewart 	/*
2702f8829a4aSRandall Stewart 	 * This code should in theory NOT run but
2703f8829a4aSRandall Stewart 	 */
2704f8829a4aSRandall Stewart 	if (netl == NULL) {
2705f8829a4aSRandall Stewart 		/* TSNH! Huh, why do I need to add this address here? */
270660990c0cSMichael Tuexen 		if (sctp_add_remote_addr(*stcb, to, NULL, SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
270760990c0cSMichael Tuexen 			return (NULL);
270860990c0cSMichael Tuexen 		}
2709f8829a4aSRandall Stewart 		netl = sctp_findnet(*stcb, to);
2710f8829a4aSRandall Stewart 	}
2711f8829a4aSRandall Stewart 	if (netl) {
2712f8829a4aSRandall Stewart 		if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2713f8829a4aSRandall Stewart 			netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2714ad81507eSRandall Stewart 			(void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2715f8829a4aSRandall Stewart 			    netl);
27162fad0e55SMichael Tuexen 			send_int_conf = 1;
2717f8829a4aSRandall Stewart 		}
2718f8829a4aSRandall Stewart 	}
2719ca85e948SMichael Tuexen 	sctp_start_net_timers(*stcb);
2720f8829a4aSRandall Stewart 	if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2721f8829a4aSRandall Stewart 		if (!had_a_existing_tcb ||
2722f8829a4aSRandall Stewart 		    (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2723f8829a4aSRandall Stewart 			/*
2724f8829a4aSRandall Stewart 			 * If we have a NEW cookie or the connect never
2725f8829a4aSRandall Stewart 			 * reached the connected state during collision we
2726f8829a4aSRandall Stewart 			 * must do the TCP accept thing.
2727f8829a4aSRandall Stewart 			 */
2728f8829a4aSRandall Stewart 			struct socket *so, *oso;
2729f8829a4aSRandall Stewart 			struct sctp_inpcb *inp;
2730f8829a4aSRandall Stewart 
2731f8829a4aSRandall Stewart 			if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2732f8829a4aSRandall Stewart 				/*
2733f8829a4aSRandall Stewart 				 * For a restart we will keep the same
2734f8829a4aSRandall Stewart 				 * socket, no need to do anything. I THINK!!
2735f8829a4aSRandall Stewart 				 */
27367215cc1bSMichael Tuexen 				sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
27372fad0e55SMichael Tuexen 				if (send_int_conf) {
27382fad0e55SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
27392fad0e55SMichael Tuexen 					    (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
27402fad0e55SMichael Tuexen 				}
2741f8829a4aSRandall Stewart 				return (m);
2742f8829a4aSRandall Stewart 			}
2743f8829a4aSRandall Stewart 			oso = (*inp_p)->sctp_socket;
27442dad8a55SRandall Stewart 			atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2745f8829a4aSRandall Stewart 			SCTP_TCB_UNLOCK((*stcb));
27461fb51a12SBjoern A. Zeeb 			CURVNET_SET(oso->so_vnet);
274793164cf9SRandall Stewart 			so = sonewconn(oso, 0
2748f8829a4aSRandall Stewart 			    );
27491fb51a12SBjoern A. Zeeb 			CURVNET_RESTORE();
2750f8829a4aSRandall Stewart 			SCTP_TCB_LOCK((*stcb));
27512dad8a55SRandall Stewart 			atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
27522dad8a55SRandall Stewart 
2753f8829a4aSRandall Stewart 			if (so == NULL) {
2754f8829a4aSRandall Stewart 				struct mbuf *op_err;
2755f8829a4aSRandall Stewart 
2756ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2757ceaad40aSRandall Stewart 				struct socket *pcb_so;
2758ceaad40aSRandall Stewart 
2759ceaad40aSRandall Stewart #endif
2760f8829a4aSRandall Stewart 				/* Too many sockets */
2761ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2762ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2763f8829a4aSRandall Stewart 				sctp_abort_association(*inp_p, NULL, m, iphlen,
2764b1754ad1SMichael Tuexen 				    src, dst, sh, op_err,
2765457b4b88SMichael Tuexen 				    mflowtype, mflowid,
2766f30ac432SMichael Tuexen 				    vrf_id, port);
2767ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2768ceaad40aSRandall Stewart 				pcb_so = SCTP_INP_SO(*inp_p);
2769ceaad40aSRandall Stewart 				atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2770ceaad40aSRandall Stewart 				SCTP_TCB_UNLOCK((*stcb));
2771ceaad40aSRandall Stewart 				SCTP_SOCKET_LOCK(pcb_so, 1);
2772ceaad40aSRandall Stewart 				SCTP_TCB_LOCK((*stcb));
2773ceaad40aSRandall Stewart 				atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2774ceaad40aSRandall Stewart #endif
2775b7d130beSMichael Tuexen 				(void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC,
2776b7d130beSMichael Tuexen 				    SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
2777ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2778ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(pcb_so, 1);
2779ceaad40aSRandall Stewart #endif
2780f8829a4aSRandall Stewart 				return (NULL);
2781f8829a4aSRandall Stewart 			}
2782f8829a4aSRandall Stewart 			inp = (struct sctp_inpcb *)so->so_pcb;
278393164cf9SRandall Stewart 			SCTP_INP_INCR_REF(inp);
278493164cf9SRandall Stewart 			/*
278593164cf9SRandall Stewart 			 * We add the unbound flag here so that if we get an
278693164cf9SRandall Stewart 			 * soabort() before we get the move_pcb done, we
278793164cf9SRandall Stewart 			 * will properly cleanup.
278893164cf9SRandall Stewart 			 */
2789f8829a4aSRandall Stewart 			inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2790f8829a4aSRandall Stewart 			    SCTP_PCB_FLAGS_CONNECTED |
2791f8829a4aSRandall Stewart 			    SCTP_PCB_FLAGS_IN_TCPPOOL |
279293164cf9SRandall Stewart 			    SCTP_PCB_FLAGS_UNBOUND |
2793f8829a4aSRandall Stewart 			    (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2794f8829a4aSRandall Stewart 			    SCTP_PCB_FLAGS_DONT_WAKE);
2795f8829a4aSRandall Stewart 			inp->sctp_features = (*inp_p)->sctp_features;
2796851b7298SRandall Stewart 			inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2797f8829a4aSRandall Stewart 			inp->sctp_socket = so;
2798f8829a4aSRandall Stewart 			inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
279959b6d5beSMichael Tuexen 			inp->max_cwnd = (*inp_p)->max_cwnd;
280020083c2eSMichael Tuexen 			inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2801f342355aSMichael Tuexen 			inp->ecn_supported = (*inp_p)->ecn_supported;
2802dd973b0eSMichael Tuexen 			inp->prsctp_supported = (*inp_p)->prsctp_supported;
2803c79bec9cSMichael Tuexen 			inp->auth_supported = (*inp_p)->auth_supported;
2804c79bec9cSMichael Tuexen 			inp->asconf_supported = (*inp_p)->asconf_supported;
2805317e00efSMichael Tuexen 			inp->reconfig_supported = (*inp_p)->reconfig_supported;
2806caea9879SMichael Tuexen 			inp->nrsack_supported = (*inp_p)->nrsack_supported;
2807cb9b8e6fSMichael Tuexen 			inp->pktdrop_supported = (*inp_p)->pktdrop_supported;
2808f8829a4aSRandall Stewart 			inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2809f8829a4aSRandall Stewart 			inp->sctp_context = (*inp_p)->sctp_context;
2810c4e848b7SRandall Stewart 			inp->local_strreset_support = (*inp_p)->local_strreset_support;
2811d089f9b9SMichael Tuexen 			inp->fibnum = (*inp_p)->fibnum;
2812f8829a4aSRandall Stewart 			inp->inp_starting_point_for_iterator = NULL;
2813f8829a4aSRandall Stewart 			/*
2814f8829a4aSRandall Stewart 			 * copy in the authentication parameters from the
2815f8829a4aSRandall Stewart 			 * original endpoint
2816f8829a4aSRandall Stewart 			 */
2817f8829a4aSRandall Stewart 			if (inp->sctp_ep.local_hmacs)
2818f8829a4aSRandall Stewart 				sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2819f8829a4aSRandall Stewart 			inp->sctp_ep.local_hmacs =
2820f8829a4aSRandall Stewart 			    sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2821f8829a4aSRandall Stewart 			if (inp->sctp_ep.local_auth_chunks)
2822f8829a4aSRandall Stewart 				sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2823f8829a4aSRandall Stewart 			inp->sctp_ep.local_auth_chunks =
2824f8829a4aSRandall Stewart 			    sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2825f8829a4aSRandall Stewart 
2826f8829a4aSRandall Stewart 			/*
2827f8829a4aSRandall Stewart 			 * Now we must move it from one hash table to
2828f8829a4aSRandall Stewart 			 * another and get the tcb in the right place.
2829f8829a4aSRandall Stewart 			 */
283088a7eb29SRandall Stewart 
283188a7eb29SRandall Stewart 			/*
283288a7eb29SRandall Stewart 			 * This is where the one-2-one socket is put into
283388a7eb29SRandall Stewart 			 * the accept state waiting for the accept!
283488a7eb29SRandall Stewart 			 */
283588a7eb29SRandall Stewart 			if (*stcb) {
283688a7eb29SRandall Stewart 				(*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE;
283788a7eb29SRandall Stewart 			}
2838f8829a4aSRandall Stewart 			sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2839d61a0ae0SRandall Stewart 
2840d61a0ae0SRandall Stewart 			atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2841d61a0ae0SRandall Stewart 			SCTP_TCB_UNLOCK((*stcb));
2842d61a0ae0SRandall Stewart 
2843265de5bbSRobert Watson 			sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2844265de5bbSRobert Watson 			    0);
2845d61a0ae0SRandall Stewart 			SCTP_TCB_LOCK((*stcb));
2846d61a0ae0SRandall Stewart 			atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2847d61a0ae0SRandall Stewart 
2848f8829a4aSRandall Stewart 
284993164cf9SRandall Stewart 			/*
285093164cf9SRandall Stewart 			 * now we must check to see if we were aborted while
285193164cf9SRandall Stewart 			 * the move was going on and the lock/unlock
285293164cf9SRandall Stewart 			 * happened.
285393164cf9SRandall Stewart 			 */
285493164cf9SRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
285593164cf9SRandall Stewart 				/*
285693164cf9SRandall Stewart 				 * yep it was, we leave the assoc attached
285793164cf9SRandall Stewart 				 * to the socket since the sctp_inpcb_free()
285893164cf9SRandall Stewart 				 * call will send an abort for us.
285993164cf9SRandall Stewart 				 */
286093164cf9SRandall Stewart 				SCTP_INP_DECR_REF(inp);
286193164cf9SRandall Stewart 				return (NULL);
286293164cf9SRandall Stewart 			}
286393164cf9SRandall Stewart 			SCTP_INP_DECR_REF(inp);
2864f8829a4aSRandall Stewart 			/* Switch over to the new guy */
2865f8829a4aSRandall Stewart 			*inp_p = inp;
2866ceaad40aSRandall Stewart 			sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
28672fad0e55SMichael Tuexen 			if (send_int_conf) {
28682fad0e55SMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
28692fad0e55SMichael Tuexen 				    (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
28702fad0e55SMichael Tuexen 			}
287193164cf9SRandall Stewart 			/*
287293164cf9SRandall Stewart 			 * Pull it from the incomplete queue and wake the
287393164cf9SRandall Stewart 			 * guy
287493164cf9SRandall Stewart 			 */
2875ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2876ceaad40aSRandall Stewart 			atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2877ceaad40aSRandall Stewart 			SCTP_TCB_UNLOCK((*stcb));
2878ceaad40aSRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
2879ceaad40aSRandall Stewart #endif
288093164cf9SRandall Stewart 			soisconnected(so);
2881ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2882b7a446b8SRandall Stewart 			SCTP_TCB_LOCK((*stcb));
2883b7a446b8SRandall Stewart 			atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2884ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
2885ceaad40aSRandall Stewart #endif
2886f8829a4aSRandall Stewart 			return (m);
2887f8829a4aSRandall Stewart 		}
2888f8829a4aSRandall Stewart 	}
28892fad0e55SMichael Tuexen 	if (notification) {
2890ceaad40aSRandall Stewart 		sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2891f8829a4aSRandall Stewart 	}
28922fad0e55SMichael Tuexen 	if (send_int_conf) {
28932fad0e55SMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
28942fad0e55SMichael Tuexen 		    (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
28952fad0e55SMichael Tuexen 	}
2896f8829a4aSRandall Stewart 	return (m);
2897f8829a4aSRandall Stewart }
2898f8829a4aSRandall Stewart 
2899f8829a4aSRandall Stewart static void
29007215cc1bSMichael Tuexen sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2901f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net)
2902f8829a4aSRandall Stewart {
2903f8829a4aSRandall Stewart 	/* cp must not be used, others call this without a c-ack :-) */
2904f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2905f8829a4aSRandall Stewart 
2906ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
2907ad81507eSRandall Stewart 	    "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2908ad234e3cSMichael Tuexen 	if ((stcb == NULL) || (net == NULL)) {
2909f8829a4aSRandall Stewart 		return;
2910ad234e3cSMichael Tuexen 	}
2911f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2912f8829a4aSRandall Stewart 
2913f8829a4aSRandall Stewart 	sctp_stop_all_cookie_timers(stcb);
2914f8829a4aSRandall Stewart 	/* process according to association state */
2915f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2916f8829a4aSRandall Stewart 		/* state change only needed when I am in right state */
2917ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2918c4739e2fSRandall Stewart 		SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2919ca85e948SMichael Tuexen 		sctp_start_net_timers(stcb);
2920f8829a4aSRandall Stewart 		if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2921f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2922f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, asoc->primary_destination);
2923f8829a4aSRandall Stewart 
2924f8829a4aSRandall Stewart 		}
2925f8829a4aSRandall Stewart 		/* update RTO */
2926f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2927f8829a4aSRandall Stewart 		SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2928f8829a4aSRandall Stewart 		if (asoc->overall_error_count == 0) {
2929f8829a4aSRandall Stewart 			net->RTO = sctp_calculate_rto(stcb, asoc, net,
2930899288aeSRandall Stewart 			    &asoc->time_entered, sctp_align_safe_nocopy,
2931f79aab18SRandall Stewart 			    SCTP_RTT_FROM_NON_DATA);
2932f8829a4aSRandall Stewart 		}
29336e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2934ceaad40aSRandall Stewart 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2935f8829a4aSRandall Stewart 		if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2936f8829a4aSRandall Stewart 		    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2937ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2938ceaad40aSRandall Stewart 			struct socket *so;
2939ceaad40aSRandall Stewart 
2940ceaad40aSRandall Stewart #endif
2941f8829a4aSRandall Stewart 			stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2942ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2943ceaad40aSRandall Stewart 			so = SCTP_INP_SO(stcb->sctp_ep);
2944ceaad40aSRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, 1);
2945ceaad40aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
2946ceaad40aSRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
2947ceaad40aSRandall Stewart 			SCTP_TCB_LOCK(stcb);
2948ceaad40aSRandall Stewart 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
2949ceaad40aSRandall Stewart #endif
2950d69e7322SRandall Stewart 			if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
2951ceaad40aSRandall Stewart 				soisconnected(stcb->sctp_socket);
2952d69e7322SRandall Stewart 			}
2953ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2954ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
2955ceaad40aSRandall Stewart #endif
2956f8829a4aSRandall Stewart 		}
2957f8829a4aSRandall Stewart 		/*
2958f8829a4aSRandall Stewart 		 * since we did not send a HB make sure we don't double
2959f8829a4aSRandall Stewart 		 * things
2960f8829a4aSRandall Stewart 		 */
2961f8829a4aSRandall Stewart 		net->hb_responded = 1;
2962f8829a4aSRandall Stewart 
2963d69e7322SRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2964d69e7322SRandall Stewart 			/*
2965d69e7322SRandall Stewart 			 * We don't need to do the asconf thing, nor hb or
2966d69e7322SRandall Stewart 			 * autoclose if the socket is closed.
2967d69e7322SRandall Stewart 			 */
2968d69e7322SRandall Stewart 			goto closed_socket;
2969d69e7322SRandall Stewart 		}
2970d69e7322SRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2971d69e7322SRandall Stewart 		    stcb, net);
2972d69e7322SRandall Stewart 
2973d69e7322SRandall Stewart 
2974f8829a4aSRandall Stewart 		if (stcb->asoc.sctp_autoclose_ticks &&
2975f8829a4aSRandall Stewart 		    sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2976f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2977f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, NULL);
2978f8829a4aSRandall Stewart 		}
2979f8829a4aSRandall Stewart 		/*
29801b649582SRandall Stewart 		 * send ASCONF if parameters are pending and ASCONFs are
29811b649582SRandall Stewart 		 * allowed (eg. addresses changed when init/cookie echo were
29821b649582SRandall Stewart 		 * in flight)
2983f8829a4aSRandall Stewart 		 */
2984f8829a4aSRandall Stewart 		if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2985c79bec9cSMichael Tuexen 		    (stcb->asoc.asconf_supported == 1) &&
2986f8829a4aSRandall Stewart 		    (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
29871b649582SRandall Stewart #ifdef SCTP_TIMER_BASED_ASCONF
2988f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2989f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb,
2990f8829a4aSRandall Stewart 			    stcb->asoc.primary_destination);
29911b649582SRandall Stewart #else
29923232788eSRandall Stewart 			sctp_send_asconf(stcb, stcb->asoc.primary_destination,
29933232788eSRandall Stewart 			    SCTP_ADDR_NOT_LOCKED);
29941b649582SRandall Stewart #endif
2995f8829a4aSRandall Stewart 		}
2996f8829a4aSRandall Stewart 	}
2997d69e7322SRandall Stewart closed_socket:
2998f8829a4aSRandall Stewart 	/* Toss the cookie if I can */
2999f8829a4aSRandall Stewart 	sctp_toss_old_cookies(stcb, asoc);
3000f8829a4aSRandall Stewart 	if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3001f8829a4aSRandall Stewart 		/* Restart the timer if we have pending data */
3002f8829a4aSRandall Stewart 		struct sctp_tmit_chunk *chk;
3003f8829a4aSRandall Stewart 
3004f8829a4aSRandall Stewart 		chk = TAILQ_FIRST(&asoc->sent_queue);
30054a9ef3f8SMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
3006f8829a4aSRandall Stewart 	}
3007f8829a4aSRandall Stewart }
3008f8829a4aSRandall Stewart 
3009f8829a4aSRandall Stewart static void
3010f8829a4aSRandall Stewart sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
3011f8829a4aSRandall Stewart     struct sctp_tcb *stcb)
3012f8829a4aSRandall Stewart {
3013f8829a4aSRandall Stewart 	struct sctp_nets *net;
3014f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *lchk;
3015a21779f0SRandall Stewart 	struct sctp_ecne_chunk bkup;
301660990c0cSMichael Tuexen 	uint8_t override_bit;
3017a21779f0SRandall Stewart 	uint32_t tsn, window_data_tsn;
3018a21779f0SRandall Stewart 	int len;
3019dec0177dSRandall Stewart 	unsigned int pkt_cnt;
3020f8829a4aSRandall Stewart 
3021a21779f0SRandall Stewart 	len = ntohs(cp->ch.chunk_length);
3022a21779f0SRandall Stewart 	if ((len != sizeof(struct sctp_ecne_chunk)) &&
3023a21779f0SRandall Stewart 	    (len != sizeof(struct old_sctp_ecne_chunk))) {
3024f8829a4aSRandall Stewart 		return;
3025f8829a4aSRandall Stewart 	}
3026a21779f0SRandall Stewart 	if (len == sizeof(struct old_sctp_ecne_chunk)) {
3027a21779f0SRandall Stewart 		/* Its the old format */
3028a21779f0SRandall Stewart 		memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
3029a21779f0SRandall Stewart 		bkup.num_pkts_since_cwr = htonl(1);
3030a21779f0SRandall Stewart 		cp = &bkup;
3031a21779f0SRandall Stewart 	}
3032f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvecne);
3033f8829a4aSRandall Stewart 	tsn = ntohl(cp->tsn);
3034a21779f0SRandall Stewart 	pkt_cnt = ntohl(cp->num_pkts_since_cwr);
3035a21779f0SRandall Stewart 	lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
3036f8829a4aSRandall Stewart 	if (lchk == NULL) {
3037493d8e5aSRandall Stewart 		window_data_tsn = stcb->asoc.sending_seq - 1;
3038f8829a4aSRandall Stewart 	} else {
3039493d8e5aSRandall Stewart 		window_data_tsn = lchk->rec.data.TSN_seq;
3040f8829a4aSRandall Stewart 	}
3041f8829a4aSRandall Stewart 
3042a21779f0SRandall Stewart 	/* Find where it was sent to if possible. */
3043f8829a4aSRandall Stewart 	net = NULL;
30444a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
3045f8829a4aSRandall Stewart 		if (lchk->rec.data.TSN_seq == tsn) {
3046f8829a4aSRandall Stewart 			net = lchk->whoTo;
3047899288aeSRandall Stewart 			net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
3048f8829a4aSRandall Stewart 			break;
3049f8829a4aSRandall Stewart 		}
305020b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(lchk->rec.data.TSN_seq, tsn)) {
3051f8829a4aSRandall Stewart 			break;
3052f8829a4aSRandall Stewart 		}
305320b07a4dSMichael Tuexen 	}
3054a21779f0SRandall Stewart 	if (net == NULL) {
3055a21779f0SRandall Stewart 		/*
3056a21779f0SRandall Stewart 		 * What to do. A previous send of a CWR was possibly lost.
3057a21779f0SRandall Stewart 		 * See how old it is, we may have it marked on the actual
3058a21779f0SRandall Stewart 		 * net.
3059a21779f0SRandall Stewart 		 */
3060a21779f0SRandall Stewart 		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3061a21779f0SRandall Stewart 			if (tsn == net->last_cwr_tsn) {
3062a21779f0SRandall Stewart 				/* Found him, send it off */
306360990c0cSMichael Tuexen 				break;
3064a21779f0SRandall Stewart 			}
3065a21779f0SRandall Stewart 		}
306660990c0cSMichael Tuexen 		if (net == NULL) {
3067a21779f0SRandall Stewart 			/*
306860990c0cSMichael Tuexen 			 * If we reach here, we need to send a special CWR
306960990c0cSMichael Tuexen 			 * that says hey, we did this a long time ago and
307060990c0cSMichael Tuexen 			 * you lost the response.
3071a21779f0SRandall Stewart 			 */
3072a21779f0SRandall Stewart 			net = TAILQ_FIRST(&stcb->asoc.nets);
307360990c0cSMichael Tuexen 			if (net == NULL) {
307460990c0cSMichael Tuexen 				/* TSNH */
307560990c0cSMichael Tuexen 				return;
3076a21779f0SRandall Stewart 			}
307760990c0cSMichael Tuexen 			override_bit = SCTP_CWR_REDUCE_OVERRIDE;
307860990c0cSMichael Tuexen 		} else {
307960990c0cSMichael Tuexen 			override_bit = 0;
308060990c0cSMichael Tuexen 		}
308160990c0cSMichael Tuexen 	} else {
308260990c0cSMichael Tuexen 		override_bit = 0;
308360990c0cSMichael Tuexen 	}
3084493d8e5aSRandall Stewart 	if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
3085493d8e5aSRandall Stewart 	    ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3086b54d3a6cSRandall Stewart 		/*
3087b54d3a6cSRandall Stewart 		 * JRS - Use the congestion control given in the pluggable
3088b54d3a6cSRandall Stewart 		 * CC module
3089b54d3a6cSRandall Stewart 		 */
3090493d8e5aSRandall Stewart 		stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
3091f8829a4aSRandall Stewart 		/*
3092a21779f0SRandall Stewart 		 * We reduce once every RTT. So we will only lower cwnd at
3093a21779f0SRandall Stewart 		 * the next sending seq i.e. the window_data_tsn
3094f8829a4aSRandall Stewart 		 */
3095a21779f0SRandall Stewart 		net->cwr_window_tsn = window_data_tsn;
3096a21779f0SRandall Stewart 		net->ecn_ce_pkt_cnt += pkt_cnt;
3097a21779f0SRandall Stewart 		net->lost_cnt = pkt_cnt;
3098a21779f0SRandall Stewart 		net->last_cwr_tsn = tsn;
3099a21779f0SRandall Stewart 	} else {
3100a21779f0SRandall Stewart 		override_bit |= SCTP_CWR_IN_SAME_WINDOW;
3101493d8e5aSRandall Stewart 		if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
3102493d8e5aSRandall Stewart 		    ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3103a21779f0SRandall Stewart 			/*
3104493d8e5aSRandall Stewart 			 * Another loss in the same window update how many
3105493d8e5aSRandall Stewart 			 * marks/packets lost we have had.
3106a21779f0SRandall Stewart 			 */
3107493d8e5aSRandall Stewart 			int cnt = 1;
3108a21779f0SRandall Stewart 
3109a21779f0SRandall Stewart 			if (pkt_cnt > net->lost_cnt) {
3110a21779f0SRandall Stewart 				/* Should be the case */
3111493d8e5aSRandall Stewart 				cnt = (pkt_cnt - net->lost_cnt);
3112493d8e5aSRandall Stewart 				net->ecn_ce_pkt_cnt += cnt;
3113a21779f0SRandall Stewart 			}
3114493d8e5aSRandall Stewart 			net->lost_cnt = pkt_cnt;
3115a21779f0SRandall Stewart 			net->last_cwr_tsn = tsn;
3116493d8e5aSRandall Stewart 			/*
3117493d8e5aSRandall Stewart 			 * Most CC functions will ignore this call, since we
3118493d8e5aSRandall Stewart 			 * are in-window yet of the initial CE the peer saw.
3119493d8e5aSRandall Stewart 			 */
3120493d8e5aSRandall Stewart 			stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
3121a21779f0SRandall Stewart 		}
3122f8829a4aSRandall Stewart 	}
3123f8829a4aSRandall Stewart 	/*
3124f8829a4aSRandall Stewart 	 * We always send a CWR this way if our previous one was lost our
3125f8829a4aSRandall Stewart 	 * peer will get an update, or if it is not time again to reduce we
3126a21779f0SRandall Stewart 	 * still get the cwr to the peer. Note we set the override when we
3127a21779f0SRandall Stewart 	 * could not find the TSN on the chunk or the destination network.
3128f8829a4aSRandall Stewart 	 */
3129a21779f0SRandall Stewart 	sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
3130f8829a4aSRandall Stewart }
3131f8829a4aSRandall Stewart 
3132f8829a4aSRandall Stewart static void
3133a21779f0SRandall Stewart sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3134f8829a4aSRandall Stewart {
3135f8829a4aSRandall Stewart 	/*
3136f8829a4aSRandall Stewart 	 * Here we get a CWR from the peer. We must look in the outqueue and
3137582212faSEitan Adler 	 * make sure that we have a covered ECNE in the control chunk part.
3138f8829a4aSRandall Stewart 	 * If so remove it.
3139f8829a4aSRandall Stewart 	 */
3140f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
3141f8829a4aSRandall Stewart 	struct sctp_ecne_chunk *ecne;
3142a21779f0SRandall Stewart 	int override;
3143a21779f0SRandall Stewart 	uint32_t cwr_tsn;
3144f8829a4aSRandall Stewart 
3145a21779f0SRandall Stewart 	cwr_tsn = ntohl(cp->tsn);
3146a21779f0SRandall Stewart 	override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
3147f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
3148f8829a4aSRandall Stewart 		if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3149f8829a4aSRandall Stewart 			continue;
3150f8829a4aSRandall Stewart 		}
3151a21779f0SRandall Stewart 		if ((override == 0) && (chk->whoTo != net)) {
3152a21779f0SRandall Stewart 			/* Must be from the right src unless override is set */
3153a21779f0SRandall Stewart 			continue;
3154a21779f0SRandall Stewart 		}
3155f8829a4aSRandall Stewart 		ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3156a21779f0SRandall Stewart 		if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3157f8829a4aSRandall Stewart 			/* this covers this ECNE, we can remove it */
3158f8829a4aSRandall Stewart 			stcb->asoc.ecn_echo_cnt_onq--;
3159f8829a4aSRandall Stewart 			TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3160f8829a4aSRandall Stewart 			    sctp_next);
3161f8829a4aSRandall Stewart 			sctp_m_freem(chk->data);
3162f8829a4aSRandall Stewart 			chk->data = NULL;
3163f8829a4aSRandall Stewart 			stcb->asoc.ctrl_queue_cnt--;
3164689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3165a21779f0SRandall Stewart 			if (override == 0) {
3166f8829a4aSRandall Stewart 				break;
3167f8829a4aSRandall Stewart 			}
3168f8829a4aSRandall Stewart 		}
3169f8829a4aSRandall Stewart 	}
3170a21779f0SRandall Stewart }
3171f8829a4aSRandall Stewart 
3172f8829a4aSRandall Stewart static void
31737215cc1bSMichael Tuexen sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3174f8829a4aSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net)
3175f8829a4aSRandall Stewart {
3176f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3177f8829a4aSRandall Stewart 
3178ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3179ceaad40aSRandall Stewart 	struct socket *so;
3180ceaad40aSRandall Stewart 
3181ceaad40aSRandall Stewart #endif
3182ceaad40aSRandall Stewart 
3183ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
3184ad81507eSRandall Stewart 	    "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3185f8829a4aSRandall Stewart 	if (stcb == NULL)
3186f8829a4aSRandall Stewart 		return;
3187f8829a4aSRandall Stewart 
3188f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
3189f8829a4aSRandall Stewart 	/* process according to association state */
3190f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3191f8829a4aSRandall Stewart 		/* unexpected SHUTDOWN-COMPLETE... so ignore... */
31922afb3e84SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2,
31932afb3e84SRandall Stewart 		    "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3194f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3195f8829a4aSRandall Stewart 		return;
3196f8829a4aSRandall Stewart 	}
3197f8829a4aSRandall Stewart 	/* notify upper layer protocol */
3198f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
3199ceaad40aSRandall Stewart 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
320056f778aaSMichael Tuexen 	}
320156f778aaSMichael Tuexen #ifdef INVARIANTS
3202f8829a4aSRandall Stewart 	if (!TAILQ_EMPTY(&asoc->send_queue) ||
3203f8829a4aSRandall Stewart 	    !TAILQ_EMPTY(&asoc->sent_queue) ||
3204f7a77f6fSMichael Tuexen 	    !stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
320556f778aaSMichael Tuexen 		panic("Queues are not empty when handling SHUTDOWN-COMPLETE");
3206f8829a4aSRandall Stewart 	}
320756f778aaSMichael Tuexen #endif
3208f8829a4aSRandall Stewart 	/* stop the timer */
3209b7d130beSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net,
3210b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3211f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3212f8829a4aSRandall Stewart 	/* free the TCB */
32132afb3e84SRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT2,
32142afb3e84SRandall Stewart 	    "sctp_handle_shutdown_complete: calls free-asoc\n");
3215ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3216ceaad40aSRandall Stewart 	so = SCTP_INP_SO(stcb->sctp_ep);
3217ceaad40aSRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, 1);
3218ceaad40aSRandall Stewart 	SCTP_TCB_UNLOCK(stcb);
3219ceaad40aSRandall Stewart 	SCTP_SOCKET_LOCK(so, 1);
3220ceaad40aSRandall Stewart 	SCTP_TCB_LOCK(stcb);
3221ceaad40aSRandall Stewart 	atomic_subtract_int(&stcb->asoc.refcnt, 1);
3222ceaad40aSRandall Stewart #endif
3223b7d130beSMichael Tuexen 	(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
3224b7d130beSMichael Tuexen 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3225ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3226ceaad40aSRandall Stewart 	SCTP_SOCKET_UNLOCK(so, 1);
3227ceaad40aSRandall Stewart #endif
3228f8829a4aSRandall Stewart 	return;
3229f8829a4aSRandall Stewart }
3230f8829a4aSRandall Stewart 
3231f8829a4aSRandall Stewart static int
3232f8829a4aSRandall Stewart process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3233f8829a4aSRandall Stewart     struct sctp_nets *net, uint8_t flg)
3234f8829a4aSRandall Stewart {
3235f8829a4aSRandall Stewart 	switch (desc->chunk_type) {
3236f8829a4aSRandall Stewart 	case SCTP_DATA:
3237f8829a4aSRandall Stewart 		/* find the tsn to resend (possibly */
3238f8829a4aSRandall Stewart 		{
3239f8829a4aSRandall Stewart 			uint32_t tsn;
3240f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *tp1;
3241f8829a4aSRandall Stewart 
3242f8829a4aSRandall Stewart 			tsn = ntohl(desc->tsn_ifany);
32434a9ef3f8SMichael Tuexen 			TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3244f8829a4aSRandall Stewart 				if (tp1->rec.data.TSN_seq == tsn) {
3245f8829a4aSRandall Stewart 					/* found it */
3246f8829a4aSRandall Stewart 					break;
3247f8829a4aSRandall Stewart 				}
324820b07a4dSMichael Tuexen 				if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, tsn)) {
3249f8829a4aSRandall Stewart 					/* not found */
3250f8829a4aSRandall Stewart 					tp1 = NULL;
3251f8829a4aSRandall Stewart 					break;
3252f8829a4aSRandall Stewart 				}
3253f8829a4aSRandall Stewart 			}
3254f8829a4aSRandall Stewart 			if (tp1 == NULL) {
3255f8829a4aSRandall Stewart 				/*
3256f8829a4aSRandall Stewart 				 * Do it the other way , aka without paying
3257f8829a4aSRandall Stewart 				 * attention to queue seq order.
3258f8829a4aSRandall Stewart 				 */
3259f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpdnfnd);
32604a9ef3f8SMichael Tuexen 				TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3261f8829a4aSRandall Stewart 					if (tp1->rec.data.TSN_seq == tsn) {
3262f8829a4aSRandall Stewart 						/* found it */
3263f8829a4aSRandall Stewart 						break;
3264f8829a4aSRandall Stewart 					}
3265f8829a4aSRandall Stewart 				}
3266f8829a4aSRandall Stewart 			}
3267f8829a4aSRandall Stewart 			if (tp1 == NULL) {
3268f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrptsnnf);
3269f8829a4aSRandall Stewart 			}
3270f8829a4aSRandall Stewart 			if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3271f8829a4aSRandall Stewart 				uint8_t *ddp;
3272f8829a4aSRandall Stewart 
32737da23bc8SMichael Tuexen 				if (((flg & SCTP_BADCRC) == 0) &&
32747da23bc8SMichael Tuexen 				    ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
32757da23bc8SMichael Tuexen 					return (0);
32767da23bc8SMichael Tuexen 				}
3277f8829a4aSRandall Stewart 				if ((stcb->asoc.peers_rwnd == 0) &&
3278f8829a4aSRandall Stewart 				    ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3279f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_pdrpdiwnp);
3280f8829a4aSRandall Stewart 					return (0);
3281f8829a4aSRandall Stewart 				}
3282f8829a4aSRandall Stewart 				if (stcb->asoc.peers_rwnd == 0 &&
3283f8829a4aSRandall Stewart 				    (flg & SCTP_FROM_MIDDLE_BOX)) {
3284f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_pdrpdizrw);
3285f8829a4aSRandall Stewart 					return (0);
3286f8829a4aSRandall Stewart 				}
3287f8829a4aSRandall Stewart 				ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+
3288f8829a4aSRandall Stewart 				    sizeof(struct sctp_data_chunk));
3289f8829a4aSRandall Stewart 				{
3290f8829a4aSRandall Stewart 					unsigned int iii;
3291f8829a4aSRandall Stewart 
3292f8829a4aSRandall Stewart 					for (iii = 0; iii < sizeof(desc->data_bytes);
3293f8829a4aSRandall Stewart 					    iii++) {
3294f8829a4aSRandall Stewart 						if (ddp[iii] != desc->data_bytes[iii]) {
3295f8829a4aSRandall Stewart 							SCTP_STAT_INCR(sctps_pdrpbadd);
3296f8829a4aSRandall Stewart 							return (-1);
3297f8829a4aSRandall Stewart 						}
3298f8829a4aSRandall Stewart 					}
3299f8829a4aSRandall Stewart 				}
3300f8829a4aSRandall Stewart 
3301f8829a4aSRandall Stewart 				if (tp1->do_rtt) {
3302f8829a4aSRandall Stewart 					/*
3303f8829a4aSRandall Stewart 					 * this guy had a RTO calculation
3304f8829a4aSRandall Stewart 					 * pending on it, cancel it
3305f8829a4aSRandall Stewart 					 */
3306f79aab18SRandall Stewart 					if (tp1->whoTo->rto_needed == 0) {
3307f79aab18SRandall Stewart 						tp1->whoTo->rto_needed = 1;
3308f79aab18SRandall Stewart 					}
3309f8829a4aSRandall Stewart 					tp1->do_rtt = 0;
3310f8829a4aSRandall Stewart 				}
3311f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpmark);
3312f8829a4aSRandall Stewart 				if (tp1->sent != SCTP_DATAGRAM_RESEND)
3313f8829a4aSRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3314f8829a4aSRandall Stewart 				/*
3315f8829a4aSRandall Stewart 				 * mark it as if we were doing a FR, since
3316f8829a4aSRandall Stewart 				 * we will be getting gap ack reports behind
3317f8829a4aSRandall Stewart 				 * the info from the router.
3318f8829a4aSRandall Stewart 				 */
3319f8829a4aSRandall Stewart 				tp1->rec.data.doing_fast_retransmit = 1;
3320f8829a4aSRandall Stewart 				/*
3321f8829a4aSRandall Stewart 				 * mark the tsn with what sequences can
3322f8829a4aSRandall Stewart 				 * cause a new FR.
3323f8829a4aSRandall Stewart 				 */
3324f8829a4aSRandall Stewart 				if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3325f8829a4aSRandall Stewart 					tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3326f8829a4aSRandall Stewart 				} else {
3327f8829a4aSRandall Stewart 					tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
3328f8829a4aSRandall Stewart 				}
3329f8829a4aSRandall Stewart 
3330f8829a4aSRandall Stewart 				/* restart the timer */
3331f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3332b7d130beSMichael Tuexen 				    stcb, tp1->whoTo,
3333b7d130beSMichael Tuexen 				    SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3334f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3335f8829a4aSRandall Stewart 				    stcb, tp1->whoTo);
3336f8829a4aSRandall Stewart 
3337f8829a4aSRandall Stewart 				/* fix counts and things */
3338b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3339c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3340a5d547adSRandall Stewart 					    tp1->whoTo->flight_size,
3341a5d547adSRandall Stewart 					    tp1->book_size,
33429a8e3088SMichael Tuexen 					    (uint32_t) (uintptr_t) stcb,
3343a5d547adSRandall Stewart 					    tp1->rec.data.TSN_seq);
334480fefe0aSRandall Stewart 				}
33458933fa13SRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3346c105859eSRandall Stewart 					sctp_flight_size_decrease(tp1);
3347c105859eSRandall Stewart 					sctp_total_flight_decrease(stcb, tp1);
33488933fa13SRandall Stewart 				}
3349f23ba7b1SMichael Tuexen 				tp1->sent = SCTP_DATAGRAM_RESEND;
3350f8829a4aSRandall Stewart 			} {
3351f8829a4aSRandall Stewart 				/* audit code */
3352f8829a4aSRandall Stewart 				unsigned int audit;
3353f8829a4aSRandall Stewart 
3354f8829a4aSRandall Stewart 				audit = 0;
3355f8829a4aSRandall Stewart 				TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3356f8829a4aSRandall Stewart 					if (tp1->sent == SCTP_DATAGRAM_RESEND)
3357f8829a4aSRandall Stewart 						audit++;
3358f8829a4aSRandall Stewart 				}
3359f8829a4aSRandall Stewart 				TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3360f8829a4aSRandall Stewart 				    sctp_next) {
3361f8829a4aSRandall Stewart 					if (tp1->sent == SCTP_DATAGRAM_RESEND)
3362f8829a4aSRandall Stewart 						audit++;
3363f8829a4aSRandall Stewart 				}
3364f8829a4aSRandall Stewart 				if (audit != stcb->asoc.sent_queue_retran_cnt) {
3365ad81507eSRandall Stewart 					SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3366f8829a4aSRandall Stewart 					    audit, stcb->asoc.sent_queue_retran_cnt);
3367f8829a4aSRandall Stewart #ifndef SCTP_AUDITING_ENABLED
3368f8829a4aSRandall Stewart 					stcb->asoc.sent_queue_retran_cnt = audit;
3369f8829a4aSRandall Stewart #endif
3370f8829a4aSRandall Stewart 				}
3371f8829a4aSRandall Stewart 			}
3372f8829a4aSRandall Stewart 		}
3373f8829a4aSRandall Stewart 		break;
3374f8829a4aSRandall Stewart 	case SCTP_ASCONF:
3375f8829a4aSRandall Stewart 		{
3376f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *asconf;
3377f8829a4aSRandall Stewart 
3378f8829a4aSRandall Stewart 			TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3379f8829a4aSRandall Stewart 			    sctp_next) {
3380f8829a4aSRandall Stewart 				if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3381f8829a4aSRandall Stewart 					break;
3382f8829a4aSRandall Stewart 				}
3383f8829a4aSRandall Stewart 			}
3384f8829a4aSRandall Stewart 			if (asconf) {
3385f8829a4aSRandall Stewart 				if (asconf->sent != SCTP_DATAGRAM_RESEND)
3386f8829a4aSRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3387f8829a4aSRandall Stewart 				asconf->sent = SCTP_DATAGRAM_RESEND;
3388f8829a4aSRandall Stewart 				asconf->snd_count--;
3389f8829a4aSRandall Stewart 			}
3390f8829a4aSRandall Stewart 		}
3391f8829a4aSRandall Stewart 		break;
3392f8829a4aSRandall Stewart 	case SCTP_INITIATION:
3393f8829a4aSRandall Stewart 		/* resend the INIT */
3394f8829a4aSRandall Stewart 		stcb->asoc.dropped_special_cnt++;
3395f8829a4aSRandall Stewart 		if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3396f8829a4aSRandall Stewart 			/*
3397f8829a4aSRandall Stewart 			 * If we can get it in, in a few attempts we do
3398f8829a4aSRandall Stewart 			 * this, otherwise we let the timer fire.
3399f8829a4aSRandall Stewart 			 */
3400f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3401b7d130beSMichael Tuexen 			    stcb, net,
3402b7d130beSMichael Tuexen 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
3403ceaad40aSRandall Stewart 			sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3404f8829a4aSRandall Stewart 		}
3405f8829a4aSRandall Stewart 		break;
3406f8829a4aSRandall Stewart 	case SCTP_SELECTIVE_ACK:
3407b5c16493SMichael Tuexen 	case SCTP_NR_SELECTIVE_ACK:
3408f8829a4aSRandall Stewart 		/* resend the sack */
3409689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3410f8829a4aSRandall Stewart 		break;
3411f8829a4aSRandall Stewart 	case SCTP_HEARTBEAT_REQUEST:
3412f8829a4aSRandall Stewart 		/* resend a demand HB */
3413b54d3a6cSRandall Stewart 		if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3414b54d3a6cSRandall Stewart 			/*
3415b54d3a6cSRandall Stewart 			 * Only retransmit if we KNOW we wont destroy the
3416b54d3a6cSRandall Stewart 			 * tcb
3417b54d3a6cSRandall Stewart 			 */
3418ca85e948SMichael Tuexen 			sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3419b54d3a6cSRandall Stewart 		}
3420f8829a4aSRandall Stewart 		break;
3421f8829a4aSRandall Stewart 	case SCTP_SHUTDOWN:
3422f8829a4aSRandall Stewart 		sctp_send_shutdown(stcb, net);
3423f8829a4aSRandall Stewart 		break;
3424f8829a4aSRandall Stewart 	case SCTP_SHUTDOWN_ACK:
3425f8829a4aSRandall Stewart 		sctp_send_shutdown_ack(stcb, net);
3426f8829a4aSRandall Stewart 		break;
3427f8829a4aSRandall Stewart 	case SCTP_COOKIE_ECHO:
3428f8829a4aSRandall Stewart 		{
3429f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *cookie;
3430f8829a4aSRandall Stewart 
3431f8829a4aSRandall Stewart 			cookie = NULL;
3432f8829a4aSRandall Stewart 			TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3433f8829a4aSRandall Stewart 			    sctp_next) {
3434f8829a4aSRandall Stewart 				if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3435f8829a4aSRandall Stewart 					break;
3436f8829a4aSRandall Stewart 				}
3437f8829a4aSRandall Stewart 			}
3438f8829a4aSRandall Stewart 			if (cookie) {
3439f8829a4aSRandall Stewart 				if (cookie->sent != SCTP_DATAGRAM_RESEND)
3440f8829a4aSRandall Stewart 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3441f8829a4aSRandall Stewart 				cookie->sent = SCTP_DATAGRAM_RESEND;
3442f8829a4aSRandall Stewart 				sctp_stop_all_cookie_timers(stcb);
3443f8829a4aSRandall Stewart 			}
3444f8829a4aSRandall Stewart 		}
3445f8829a4aSRandall Stewart 		break;
3446f8829a4aSRandall Stewart 	case SCTP_COOKIE_ACK:
3447f8829a4aSRandall Stewart 		sctp_send_cookie_ack(stcb);
3448f8829a4aSRandall Stewart 		break;
3449f8829a4aSRandall Stewart 	case SCTP_ASCONF_ACK:
3450f8829a4aSRandall Stewart 		/* resend last asconf ack */
34512afb3e84SRandall Stewart 		sctp_send_asconf_ack(stcb);
3452f8829a4aSRandall Stewart 		break;
3453f8829a4aSRandall Stewart 	case SCTP_FORWARD_CUM_TSN:
3454f8829a4aSRandall Stewart 		send_forward_tsn(stcb, &stcb->asoc);
3455f8829a4aSRandall Stewart 		break;
3456f8829a4aSRandall Stewart 		/* can't do anything with these */
3457f8829a4aSRandall Stewart 	case SCTP_PACKET_DROPPED:
3458f8829a4aSRandall Stewart 	case SCTP_INITIATION_ACK:	/* this should not happen */
3459f8829a4aSRandall Stewart 	case SCTP_HEARTBEAT_ACK:
3460f8829a4aSRandall Stewart 	case SCTP_ABORT_ASSOCIATION:
3461f8829a4aSRandall Stewart 	case SCTP_OPERATION_ERROR:
3462f8829a4aSRandall Stewart 	case SCTP_SHUTDOWN_COMPLETE:
3463f8829a4aSRandall Stewart 	case SCTP_ECN_ECHO:
3464f8829a4aSRandall Stewart 	case SCTP_ECN_CWR:
3465f8829a4aSRandall Stewart 	default:
3466f8829a4aSRandall Stewart 		break;
3467f8829a4aSRandall Stewart 	}
3468f8829a4aSRandall Stewart 	return (0);
3469f8829a4aSRandall Stewart }
3470f8829a4aSRandall Stewart 
3471f8829a4aSRandall Stewart void
3472a169d6ecSMichael Tuexen sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t * list)
3473f8829a4aSRandall Stewart {
3474a169d6ecSMichael Tuexen 	uint32_t i;
3475f8829a4aSRandall Stewart 	uint16_t temp;
3476f8829a4aSRandall Stewart 
3477f8829a4aSRandall Stewart 	/*
3478f8829a4aSRandall Stewart 	 * We set things to 0xffff since this is the last delivered sequence
3479f8829a4aSRandall Stewart 	 * and we will be sending in 0 after the reset.
3480f8829a4aSRandall Stewart 	 */
3481f8829a4aSRandall Stewart 
3482f8829a4aSRandall Stewart 	if (number_entries) {
3483f8829a4aSRandall Stewart 		for (i = 0; i < number_entries; i++) {
3484f8829a4aSRandall Stewart 			temp = ntohs(list[i]);
3485f8829a4aSRandall Stewart 			if (temp >= stcb->asoc.streamincnt) {
3486f8829a4aSRandall Stewart 				continue;
3487f8829a4aSRandall Stewart 			}
3488f8829a4aSRandall Stewart 			stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff;
3489f8829a4aSRandall Stewart 		}
3490f8829a4aSRandall Stewart 	} else {
3491f8829a4aSRandall Stewart 		list = NULL;
3492f8829a4aSRandall Stewart 		for (i = 0; i < stcb->asoc.streamincnt; i++) {
3493f8829a4aSRandall Stewart 			stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
3494f8829a4aSRandall Stewart 		}
3495f8829a4aSRandall Stewart 	}
3496ceaad40aSRandall Stewart 	sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3497f8829a4aSRandall Stewart }
3498f8829a4aSRandall Stewart 
3499f8829a4aSRandall Stewart static void
350056f778aaSMichael Tuexen sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t * list)
3501f8829a4aSRandall Stewart {
350256f778aaSMichael Tuexen 	uint32_t i;
3503f8829a4aSRandall Stewart 	uint16_t temp;
3504f8829a4aSRandall Stewart 
350556f778aaSMichael Tuexen 	if (number_entries > 0) {
350656f778aaSMichael Tuexen 		for (i = 0; i < number_entries; i++) {
3507f8829a4aSRandall Stewart 			temp = ntohs(list[i]);
3508f8829a4aSRandall Stewart 			if (temp >= stcb->asoc.streamoutcnt) {
3509f8829a4aSRandall Stewart 				/* no such stream */
3510f8829a4aSRandall Stewart 				continue;
3511f8829a4aSRandall Stewart 			}
3512f3b05218SMichael Tuexen 			stcb->asoc.strmout[temp].next_sequence_send = 0;
3513f8829a4aSRandall Stewart 		}
351456f778aaSMichael Tuexen 	} else {
351556f778aaSMichael Tuexen 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
351656f778aaSMichael Tuexen 			stcb->asoc.strmout[i].next_sequence_send = 0;
351756f778aaSMichael Tuexen 		}
3518f8829a4aSRandall Stewart 	}
3519ceaad40aSRandall Stewart 	sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3520f8829a4aSRandall Stewart }
3521f8829a4aSRandall Stewart 
35227cca1775SRandall Stewart static void
35237cca1775SRandall Stewart sctp_reset_clear_pending(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t * list)
35247cca1775SRandall Stewart {
35257cca1775SRandall Stewart 	uint32_t i;
35267cca1775SRandall Stewart 	uint16_t temp;
35277cca1775SRandall Stewart 
35287cca1775SRandall Stewart 	if (number_entries > 0) {
35297cca1775SRandall Stewart 		for (i = 0; i < number_entries; i++) {
35307cca1775SRandall Stewart 			temp = ntohs(list[i]);
35317cca1775SRandall Stewart 			if (temp >= stcb->asoc.streamoutcnt) {
35327cca1775SRandall Stewart 				/* no such stream */
35337cca1775SRandall Stewart 				continue;
35347cca1775SRandall Stewart 			}
35357cca1775SRandall Stewart 			stcb->asoc.strmout[temp].state = SCTP_STREAM_OPEN;
35367cca1775SRandall Stewart 		}
35377cca1775SRandall Stewart 	} else {
35387cca1775SRandall Stewart 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
35397cca1775SRandall Stewart 			stcb->asoc.strmout[i].state = SCTP_STREAM_OPEN;
35407cca1775SRandall Stewart 		}
35417cca1775SRandall Stewart 	}
35427cca1775SRandall Stewart }
35437cca1775SRandall Stewart 
3544f8829a4aSRandall Stewart 
354584f3b49aSMichael Tuexen struct sctp_stream_reset_request *
3546f8829a4aSRandall Stewart sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3547f8829a4aSRandall Stewart {
3548f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3549a169d6ecSMichael Tuexen 	struct sctp_chunkhdr *ch;
355084f3b49aSMichael Tuexen 	struct sctp_stream_reset_request *r;
3551f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
3552f8829a4aSRandall Stewart 	int len, clen;
3553f8829a4aSRandall Stewart 
3554f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
3555f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3556d06c82f1SRandall Stewart 		asoc->stream_reset_outstanding = 0;
3557f8829a4aSRandall Stewart 		return (NULL);
3558f8829a4aSRandall Stewart 	}
3559f8829a4aSRandall Stewart 	if (stcb->asoc.str_reset == NULL) {
3560d06c82f1SRandall Stewart 		asoc->stream_reset_outstanding = 0;
3561f8829a4aSRandall Stewart 		return (NULL);
3562f8829a4aSRandall Stewart 	}
3563f8829a4aSRandall Stewart 	chk = stcb->asoc.str_reset;
3564f8829a4aSRandall Stewart 	if (chk->data == NULL) {
3565f8829a4aSRandall Stewart 		return (NULL);
3566f8829a4aSRandall Stewart 	}
3567f8829a4aSRandall Stewart 	if (bchk) {
3568f8829a4aSRandall Stewart 		/* he wants a copy of the chk pointer */
3569f8829a4aSRandall Stewart 		*bchk = chk;
3570f8829a4aSRandall Stewart 	}
3571f8829a4aSRandall Stewart 	clen = chk->send_size;
3572a169d6ecSMichael Tuexen 	ch = mtod(chk->data, struct sctp_chunkhdr *);
357384f3b49aSMichael Tuexen 	r = (struct sctp_stream_reset_request *)(ch + 1);
3574f8829a4aSRandall Stewart 	if (ntohl(r->request_seq) == seq) {
3575f8829a4aSRandall Stewart 		/* found it */
3576f8829a4aSRandall Stewart 		return (r);
3577f8829a4aSRandall Stewart 	}
3578f8829a4aSRandall Stewart 	len = SCTP_SIZE32(ntohs(r->ph.param_length));
3579f8829a4aSRandall Stewart 	if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3580f8829a4aSRandall Stewart 		/* move to the next one, there can only be a max of two */
358184f3b49aSMichael Tuexen 		r = (struct sctp_stream_reset_request *)((caddr_t)r + len);
3582f8829a4aSRandall Stewart 		if (ntohl(r->request_seq) == seq) {
3583f8829a4aSRandall Stewart 			return (r);
3584f8829a4aSRandall Stewart 		}
3585f8829a4aSRandall Stewart 	}
3586f8829a4aSRandall Stewart 	/* that seq is not here */
3587f8829a4aSRandall Stewart 	return (NULL);
3588f8829a4aSRandall Stewart }
3589f8829a4aSRandall Stewart 
3590f8829a4aSRandall Stewart static void
3591f8829a4aSRandall Stewart sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3592f8829a4aSRandall Stewart {
3593f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3594f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk = stcb->asoc.str_reset;
3595f8829a4aSRandall Stewart 
3596f8829a4aSRandall Stewart 	if (stcb->asoc.str_reset == NULL) {
3597f8829a4aSRandall Stewart 		return;
3598f8829a4aSRandall Stewart 	}
3599f42a358aSRandall Stewart 	asoc = &stcb->asoc;
3600f42a358aSRandall Stewart 
3601b7d130beSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb,
3602b7d130beSMichael Tuexen 	    chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28);
3603f8829a4aSRandall Stewart 	TAILQ_REMOVE(&asoc->control_send_queue,
3604f8829a4aSRandall Stewart 	    chk,
3605f8829a4aSRandall Stewart 	    sctp_next);
3606f8829a4aSRandall Stewart 	if (chk->data) {
3607f8829a4aSRandall Stewart 		sctp_m_freem(chk->data);
3608f8829a4aSRandall Stewart 		chk->data = NULL;
3609f8829a4aSRandall Stewart 	}
3610f8829a4aSRandall Stewart 	asoc->ctrl_queue_cnt--;
3611689e6a5fSMichael Tuexen 	sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
361204ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
3613f8829a4aSRandall Stewart 	stcb->asoc.str_reset = NULL;
3614f8829a4aSRandall Stewart }
3615f8829a4aSRandall Stewart 
3616f8829a4aSRandall Stewart 
3617f8829a4aSRandall Stewart static int
3618f8829a4aSRandall Stewart sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3619f8829a4aSRandall Stewart     uint32_t seq, uint32_t action,
362008598d70SRandall Stewart     struct sctp_stream_reset_response *respin)
3621f8829a4aSRandall Stewart {
3622f8829a4aSRandall Stewart 	uint16_t type;
3623f8829a4aSRandall Stewart 	int lparm_len;
3624f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3625f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
362684f3b49aSMichael Tuexen 	struct sctp_stream_reset_request *req_param;
362784f3b49aSMichael Tuexen 	struct sctp_stream_reset_out_request *req_out_param;
362884f3b49aSMichael Tuexen 	struct sctp_stream_reset_in_request *req_in_param;
362956f778aaSMichael Tuexen 	uint32_t number_entries;
3630f8829a4aSRandall Stewart 
3631f8829a4aSRandall Stewart 	if (asoc->stream_reset_outstanding == 0) {
3632f8829a4aSRandall Stewart 		/* duplicate */
3633f8829a4aSRandall Stewart 		return (0);
3634f8829a4aSRandall Stewart 	}
3635f8829a4aSRandall Stewart 	if (seq == stcb->asoc.str_reset_seq_out) {
363684f3b49aSMichael Tuexen 		req_param = sctp_find_stream_reset(stcb, seq, &chk);
363784f3b49aSMichael Tuexen 		if (req_param != NULL) {
3638f8829a4aSRandall Stewart 			stcb->asoc.str_reset_seq_out++;
363984f3b49aSMichael Tuexen 			type = ntohs(req_param->ph.param_type);
364084f3b49aSMichael Tuexen 			lparm_len = ntohs(req_param->ph.param_length);
3641f8829a4aSRandall Stewart 			if (type == SCTP_STR_RESET_OUT_REQUEST) {
36427cca1775SRandall Stewart 				int no_clear = 0;
36437cca1775SRandall Stewart 
364484f3b49aSMichael Tuexen 				req_out_param = (struct sctp_stream_reset_out_request *)req_param;
364508598d70SRandall Stewart 				number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3646f8829a4aSRandall Stewart 				asoc->stream_reset_out_is_outstanding = 0;
3647f8829a4aSRandall Stewart 				if (asoc->stream_reset_outstanding)
3648f8829a4aSRandall Stewart 					asoc->stream_reset_outstanding--;
3649f3ebe64cSMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3650f8829a4aSRandall Stewart 					/* do it */
365184f3b49aSMichael Tuexen 					sctp_reset_out_streams(stcb, number_entries, req_out_param->list_of_streams);
3652d4260646SMichael Tuexen 				} else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
365384f3b49aSMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
36547cca1775SRandall Stewart 				} else if (action == SCTP_STREAM_RESET_RESULT_IN_PROGRESS) {
36557cca1775SRandall Stewart 					/*
36567cca1775SRandall Stewart 					 * Set it up so we don't stop
36577cca1775SRandall Stewart 					 * retransmitting
36587cca1775SRandall Stewart 					 */
3659a4889f2dSMichael Tuexen 					asoc->stream_reset_outstanding++;
36607cca1775SRandall Stewart 					stcb->asoc.str_reset_seq_out--;
36617cca1775SRandall Stewart 					asoc->stream_reset_out_is_outstanding = 1;
36627cca1775SRandall Stewart 					no_clear = 1;
3663f8829a4aSRandall Stewart 				} else {
366484f3b49aSMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3665f8829a4aSRandall Stewart 				}
36667cca1775SRandall Stewart 				if (no_clear == 0) {
36677cca1775SRandall Stewart 					sctp_reset_clear_pending(stcb, number_entries, req_out_param->list_of_streams);
36687cca1775SRandall Stewart 				}
3669f8829a4aSRandall Stewart 			} else if (type == SCTP_STR_RESET_IN_REQUEST) {
367084f3b49aSMichael Tuexen 				req_in_param = (struct sctp_stream_reset_in_request *)req_param;
367108598d70SRandall Stewart 				number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3672f8829a4aSRandall Stewart 				if (asoc->stream_reset_outstanding)
3673f8829a4aSRandall Stewart 					asoc->stream_reset_outstanding--;
3674d4260646SMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3675d4260646SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
367684f3b49aSMichael Tuexen 					    number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3677d4260646SMichael Tuexen 				} else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3678c4e848b7SRandall Stewart 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
367984f3b49aSMichael Tuexen 					    number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3680f8829a4aSRandall Stewart 				}
3681c4e848b7SRandall Stewart 			} else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3682ea44232bSRandall Stewart 				/* Ok we now may have more streams */
3683c4e848b7SRandall Stewart 				int num_stream;
3684c4e848b7SRandall Stewart 
3685c4e848b7SRandall Stewart 				num_stream = stcb->asoc.strm_pending_add_size;
3686c4e848b7SRandall Stewart 				if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3687c4e848b7SRandall Stewart 					/* TSNH */
3688c4e848b7SRandall Stewart 					num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3689c4e848b7SRandall Stewart 				}
3690c4e848b7SRandall Stewart 				stcb->asoc.strm_pending_add_size = 0;
36918aae9493SRandall Stewart 				if (asoc->stream_reset_outstanding)
36928aae9493SRandall Stewart 					asoc->stream_reset_outstanding--;
3693f3ebe64cSMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3694ea44232bSRandall Stewart 					/* Put the new streams into effect */
36957cca1775SRandall Stewart 					int i;
36967cca1775SRandall Stewart 
36977cca1775SRandall Stewart 					for (i = asoc->streamoutcnt; i < (asoc->streamoutcnt + num_stream); i++) {
36987cca1775SRandall Stewart 						asoc->strmout[i].state = SCTP_STREAM_OPEN;
36997cca1775SRandall Stewart 					}
37007cca1775SRandall Stewart 					asoc->streamoutcnt += num_stream;
3701c4e848b7SRandall Stewart 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3702d4260646SMichael Tuexen 				} else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3703d4260646SMichael Tuexen 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3704d4260646SMichael Tuexen 					    SCTP_STREAM_CHANGE_DENIED);
3705ea44232bSRandall Stewart 				} else {
3706c4e848b7SRandall Stewart 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3707d4260646SMichael Tuexen 					    SCTP_STREAM_CHANGE_FAILED);
3708c4e848b7SRandall Stewart 				}
3709c4e848b7SRandall Stewart 			} else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3710c4e848b7SRandall Stewart 				if (asoc->stream_reset_outstanding)
3711c4e848b7SRandall Stewart 					asoc->stream_reset_outstanding--;
3712d4260646SMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3713c4e848b7SRandall Stewart 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3714d4260646SMichael Tuexen 					    SCTP_STREAM_CHANGE_DENIED);
3715d4260646SMichael Tuexen 				} else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3716d4260646SMichael Tuexen 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3717d4260646SMichael Tuexen 					    SCTP_STREAM_CHANGE_FAILED);
3718ea44232bSRandall Stewart 				}
3719f8829a4aSRandall Stewart 			} else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3720f8829a4aSRandall Stewart 				/**
3721f8829a4aSRandall Stewart 				 * a) Adopt the new in tsn.
3722f8829a4aSRandall Stewart 				 * b) reset the map
3723f8829a4aSRandall Stewart 				 * c) Adopt the new out-tsn
3724f8829a4aSRandall Stewart 				 */
3725f8829a4aSRandall Stewart 				struct sctp_stream_reset_response_tsn *resp;
3726f8829a4aSRandall Stewart 				struct sctp_forward_tsn_chunk fwdtsn;
3727f8829a4aSRandall Stewart 				int abort_flag = 0;
3728f8829a4aSRandall Stewart 
3729f8829a4aSRandall Stewart 				if (respin == NULL) {
3730f8829a4aSRandall Stewart 					/* huh ? */
3731f8829a4aSRandall Stewart 					return (0);
3732f8829a4aSRandall Stewart 				}
37336a58f0e9SXin LI 				if (ntohs(respin->ph.param_length) < sizeof(struct sctp_stream_reset_response_tsn)) {
37346a58f0e9SXin LI 					return (0);
37356a58f0e9SXin LI 				}
3736f3ebe64cSMichael Tuexen 				if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3737f8829a4aSRandall Stewart 					resp = (struct sctp_stream_reset_response_tsn *)respin;
3738f8829a4aSRandall Stewart 					asoc->stream_reset_outstanding--;
3739f8829a4aSRandall Stewart 					fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3740f8829a4aSRandall Stewart 					fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3741f8829a4aSRandall Stewart 					fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3742671d309cSRandall Stewart 					sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3743f8829a4aSRandall Stewart 					if (abort_flag) {
3744f8829a4aSRandall Stewart 						return (1);
3745f8829a4aSRandall Stewart 					}
3746f8829a4aSRandall Stewart 					stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3747b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3748b3f1ea41SRandall Stewart 						sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3749b3f1ea41SRandall Stewart 					}
3750d6af161aSRandall Stewart 					stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3751f8829a4aSRandall Stewart 					stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3752f8829a4aSRandall Stewart 					memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3753830d754dSRandall Stewart 
3754830d754dSRandall Stewart 					stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3755b5c16493SMichael Tuexen 					memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
375677acdc25SRandall Stewart 
3757f8829a4aSRandall Stewart 					stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3758f8829a4aSRandall Stewart 					stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3759f8829a4aSRandall Stewart 
3760f8829a4aSRandall Stewart 					sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3761f8829a4aSRandall Stewart 					sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3762c4e848b7SRandall Stewart 					sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3763d4260646SMichael Tuexen 				} else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3764d4260646SMichael Tuexen 					sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3765d4260646SMichael Tuexen 					    SCTP_ASSOC_RESET_DENIED);
3766c4e848b7SRandall Stewart 				} else {
3767c4e848b7SRandall Stewart 					sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3768d4260646SMichael Tuexen 					    SCTP_ASSOC_RESET_FAILED);
3769f8829a4aSRandall Stewart 				}
3770f8829a4aSRandall Stewart 			}
3771f8829a4aSRandall Stewart 			/* get rid of the request and get the request flags */
3772f8829a4aSRandall Stewart 			if (asoc->stream_reset_outstanding == 0) {
3773f8829a4aSRandall Stewart 				sctp_clean_up_stream_reset(stcb);
3774f8829a4aSRandall Stewart 			}
3775f8829a4aSRandall Stewart 		}
3776f8829a4aSRandall Stewart 	}
37777cca1775SRandall Stewart 	if (asoc->stream_reset_outstanding == 0) {
3778c6168599SRandall Stewart 		sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
37797cca1775SRandall Stewart 	}
3780f8829a4aSRandall Stewart 	return (0);
3781f8829a4aSRandall Stewart }
3782f8829a4aSRandall Stewart 
3783f8829a4aSRandall Stewart static void
3784f8829a4aSRandall Stewart sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3785f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk,
3786671d309cSRandall Stewart     struct sctp_stream_reset_in_request *req, int trunc)
3787f8829a4aSRandall Stewart {
3788f8829a4aSRandall Stewart 	uint32_t seq;
3789f8829a4aSRandall Stewart 	int len, i;
3790f8829a4aSRandall Stewart 	int number_entries;
3791f8829a4aSRandall Stewart 	uint16_t temp;
3792f8829a4aSRandall Stewart 
3793f8829a4aSRandall Stewart 	/*
3794f8829a4aSRandall Stewart 	 * peer wants me to send a str-reset to him for my outgoing seq's if
3795f8829a4aSRandall Stewart 	 * seq_in is right.
3796f8829a4aSRandall Stewart 	 */
3797f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3798f8829a4aSRandall Stewart 
3799f8829a4aSRandall Stewart 	seq = ntohl(req->request_seq);
3800f8829a4aSRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3801671d309cSRandall Stewart 		asoc->last_reset_action[1] = asoc->last_reset_action[0];
3802f3ebe64cSMichael Tuexen 		if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3803f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3804f3ebe64cSMichael Tuexen 		} else if (trunc) {
3805f3ebe64cSMichael Tuexen 			/* Can't do it, since they exceeded our buffer size  */
3806f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3807671d309cSRandall Stewart 		} else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3808f8829a4aSRandall Stewart 			len = ntohs(req->ph.param_length);
3809f8829a4aSRandall Stewart 			number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
38107cca1775SRandall Stewart 			if (number_entries) {
3811f8829a4aSRandall Stewart 				for (i = 0; i < number_entries; i++) {
3812f8829a4aSRandall Stewart 					temp = ntohs(req->list_of_streams[i]);
38137cca1775SRandall Stewart 					if (temp >= stcb->asoc.streamoutcnt) {
38147cca1775SRandall Stewart 						asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
38157cca1775SRandall Stewart 						goto bad_boy;
38167cca1775SRandall Stewart 					}
3817f8829a4aSRandall Stewart 					req->list_of_streams[i] = temp;
3818f8829a4aSRandall Stewart 				}
38197cca1775SRandall Stewart 				for (i = 0; i < number_entries; i++) {
38207cca1775SRandall Stewart 					if (stcb->asoc.strmout[req->list_of_streams[i]].state == SCTP_STREAM_OPEN) {
38217cca1775SRandall Stewart 						stcb->asoc.strmout[req->list_of_streams[i]].state = SCTP_STREAM_RESET_PENDING;
38227cca1775SRandall Stewart 					}
38237cca1775SRandall Stewart 				}
38247cca1775SRandall Stewart 			} else {
38257cca1775SRandall Stewart 				/* Its all */
38267cca1775SRandall Stewart 				for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
38277cca1775SRandall Stewart 					if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN)
38287cca1775SRandall Stewart 						stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
38297cca1775SRandall Stewart 				}
38307cca1775SRandall Stewart 			}
3831f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3832f8829a4aSRandall Stewart 		} else {
3833f8829a4aSRandall Stewart 			/* Can't do it, since we have sent one out */
3834f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3835f8829a4aSRandall Stewart 		}
38367cca1775SRandall Stewart bad_boy:
3837c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3838f8829a4aSRandall Stewart 		asoc->str_reset_seq_in++;
3839f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 1 == seq) {
3840f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3841f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 2 == seq) {
3842f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3843f8829a4aSRandall Stewart 	} else {
3844f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3845f8829a4aSRandall Stewart 	}
3846c6168599SRandall Stewart 	sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3847f8829a4aSRandall Stewart }
3848f8829a4aSRandall Stewart 
3849f8829a4aSRandall Stewart static int
3850f8829a4aSRandall Stewart sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3851f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk,
3852f8829a4aSRandall Stewart     struct sctp_stream_reset_tsn_request *req)
3853f8829a4aSRandall Stewart {
3854f8829a4aSRandall Stewart 	/* reset all in and out and update the tsn */
3855f8829a4aSRandall Stewart 	/*
3856f8829a4aSRandall Stewart 	 * A) reset my str-seq's on in and out. B) Select a receive next,
3857f8829a4aSRandall Stewart 	 * and set cum-ack to it. Also process this selected number as a
3858f8829a4aSRandall Stewart 	 * fwd-tsn as well. C) set in the response my next sending seq.
3859f8829a4aSRandall Stewart 	 */
3860f8829a4aSRandall Stewart 	struct sctp_forward_tsn_chunk fwdtsn;
3861f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3862f8829a4aSRandall Stewart 	int abort_flag = 0;
3863f8829a4aSRandall Stewart 	uint32_t seq;
3864f8829a4aSRandall Stewart 
3865f8829a4aSRandall Stewart 	seq = ntohl(req->request_seq);
3866f8829a4aSRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3867ce228dabSMichael Tuexen 		asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
3868f3ebe64cSMichael Tuexen 		if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3869f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3870ce228dabSMichael Tuexen 		} else {
3871f8829a4aSRandall Stewart 			fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3872f8829a4aSRandall Stewart 			fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3873f8829a4aSRandall Stewart 			fwdtsn.ch.chunk_flags = 0;
3874f8829a4aSRandall Stewart 			fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3875671d309cSRandall Stewart 			sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3876f8829a4aSRandall Stewart 			if (abort_flag) {
3877f8829a4aSRandall Stewart 				return (1);
3878f8829a4aSRandall Stewart 			}
3879f3ebe64cSMichael Tuexen 			asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3880b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3881b3f1ea41SRandall Stewart 				sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3882b3f1ea41SRandall Stewart 			}
3883f3ebe64cSMichael Tuexen 			asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3884f3ebe64cSMichael Tuexen 			asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3885f3ebe64cSMichael Tuexen 			memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3886f3ebe64cSMichael Tuexen 			asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3887f3ebe64cSMichael Tuexen 			memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3888f3ebe64cSMichael Tuexen 			atomic_add_int(&asoc->sending_seq, 1);
3889f8829a4aSRandall Stewart 			/* save off historical data for retrans */
3890f3ebe64cSMichael Tuexen 			asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3891f3ebe64cSMichael Tuexen 			asoc->last_sending_seq[0] = asoc->sending_seq;
3892f3ebe64cSMichael Tuexen 			asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3893f3ebe64cSMichael Tuexen 			asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3894f8829a4aSRandall Stewart 			sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3895f8829a4aSRandall Stewart 			sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3896f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3897f3ebe64cSMichael Tuexen 			sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3898ce228dabSMichael Tuexen 		}
3899ce228dabSMichael Tuexen 		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3900ce228dabSMichael Tuexen 		    asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3901f8829a4aSRandall Stewart 		asoc->str_reset_seq_in++;
3902f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 1 == seq) {
3903f8829a4aSRandall Stewart 		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3904f3ebe64cSMichael Tuexen 		    asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3905f8829a4aSRandall Stewart 	} else if (asoc->str_reset_seq_in - 2 == seq) {
3906f8829a4aSRandall Stewart 		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3907f3ebe64cSMichael Tuexen 		    asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3908f8829a4aSRandall Stewart 	} else {
3909f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3910f8829a4aSRandall Stewart 	}
3911f8829a4aSRandall Stewart 	return (0);
3912f8829a4aSRandall Stewart }
3913f8829a4aSRandall Stewart 
3914f8829a4aSRandall Stewart static void
3915f8829a4aSRandall Stewart sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3916f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk,
3917671d309cSRandall Stewart     struct sctp_stream_reset_out_request *req, int trunc)
3918f8829a4aSRandall Stewart {
3919f8829a4aSRandall Stewart 	uint32_t seq, tsn;
3920f8829a4aSRandall Stewart 	int number_entries, len;
3921f8829a4aSRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
3922f8829a4aSRandall Stewart 
3923f8829a4aSRandall Stewart 	seq = ntohl(req->request_seq);
3924f8829a4aSRandall Stewart 
3925f8829a4aSRandall Stewart 	/* now if its not a duplicate we process it */
3926f8829a4aSRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
3927f8829a4aSRandall Stewart 		len = ntohs(req->ph.param_length);
3928f8829a4aSRandall Stewart 		number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3929f8829a4aSRandall Stewart 		/*
3930f8829a4aSRandall Stewart 		 * the sender is resetting, handle the list issue.. we must
3931f8829a4aSRandall Stewart 		 * a) verify if we can do the reset, if so no problem b) If
3932f8829a4aSRandall Stewart 		 * we can't do the reset we must copy the request. c) queue
3933f8829a4aSRandall Stewart 		 * it, and setup the data in processor to trigger it off
3934f8829a4aSRandall Stewart 		 * when needed and dequeue all the queued data.
3935f8829a4aSRandall Stewart 		 */
3936f8829a4aSRandall Stewart 		tsn = ntohl(req->send_reset_at_tsn);
3937f8829a4aSRandall Stewart 
3938f8829a4aSRandall Stewart 		/* move the reset action back one */
3939f8829a4aSRandall Stewart 		asoc->last_reset_action[1] = asoc->last_reset_action[0];
3940f3ebe64cSMichael Tuexen 		if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3941f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3942f3ebe64cSMichael Tuexen 		} else if (trunc) {
3943f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
394420b07a4dSMichael Tuexen 		} else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3945f8829a4aSRandall Stewart 			/* we can do it now */
3946f8829a4aSRandall Stewart 			sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3947f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3948f8829a4aSRandall Stewart 		} else {
3949f8829a4aSRandall Stewart 			/*
3950f8829a4aSRandall Stewart 			 * we must queue it up and thus wait for the TSN's
3951f8829a4aSRandall Stewart 			 * to arrive that are at or before tsn
3952f8829a4aSRandall Stewart 			 */
3953f8829a4aSRandall Stewart 			struct sctp_stream_reset_list *liste;
3954f8829a4aSRandall Stewart 			int siz;
3955f8829a4aSRandall Stewart 
3956f8829a4aSRandall Stewart 			siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3957f8829a4aSRandall Stewart 			SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3958207304d4SRandall Stewart 			    siz, SCTP_M_STRESET);
3959f8829a4aSRandall Stewart 			if (liste == NULL) {
3960f8829a4aSRandall Stewart 				/* gak out of memory */
3961f3ebe64cSMichael Tuexen 				asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3962f3ebe64cSMichael Tuexen 				sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3963f8829a4aSRandall Stewart 				return;
3964f8829a4aSRandall Stewart 			}
39657cca1775SRandall Stewart 			liste->seq = seq;
3966f8829a4aSRandall Stewart 			liste->tsn = tsn;
3967f8829a4aSRandall Stewart 			liste->number_entries = number_entries;
3968a169d6ecSMichael Tuexen 			memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t));
3969f8829a4aSRandall Stewart 			TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
39707cca1775SRandall Stewart 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_IN_PROGRESS;
3971f8829a4aSRandall Stewart 		}
3972c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3973f8829a4aSRandall Stewart 		asoc->str_reset_seq_in++;
3974f8829a4aSRandall Stewart 	} else if ((asoc->str_reset_seq_in - 1) == seq) {
3975f8829a4aSRandall Stewart 		/*
3976f8829a4aSRandall Stewart 		 * one seq back, just echo back last action since my
3977f8829a4aSRandall Stewart 		 * response was lost.
3978f8829a4aSRandall Stewart 		 */
3979f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3980f8829a4aSRandall Stewart 	} else if ((asoc->str_reset_seq_in - 2) == seq) {
3981f8829a4aSRandall Stewart 		/*
3982f8829a4aSRandall Stewart 		 * two seq back, just echo back last action since my
3983f8829a4aSRandall Stewart 		 * response was lost.
3984f8829a4aSRandall Stewart 		 */
3985f8829a4aSRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3986f8829a4aSRandall Stewart 	} else {
3987f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3988f8829a4aSRandall Stewart 	}
3989f8829a4aSRandall Stewart }
3990f8829a4aSRandall Stewart 
3991ea44232bSRandall Stewart static void
3992ea44232bSRandall Stewart sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
3993ea44232bSRandall Stewart     struct sctp_stream_reset_add_strm *str_add)
3994ea44232bSRandall Stewart {
3995ea44232bSRandall Stewart 	/*
3996ea44232bSRandall Stewart 	 * Peer is requesting to add more streams. If its within our
3997ea44232bSRandall Stewart 	 * max-streams we will allow it.
3998ea44232bSRandall Stewart 	 */
3999c4e848b7SRandall Stewart 	uint32_t num_stream, i;
4000ea44232bSRandall Stewart 	uint32_t seq;
40018aae9493SRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
40024a9ef3f8SMichael Tuexen 	struct sctp_queued_to_read *ctl, *nctl;
4003ea44232bSRandall Stewart 
4004ea44232bSRandall Stewart 	/* Get the number. */
4005ea44232bSRandall Stewart 	seq = ntohl(str_add->request_seq);
4006ea44232bSRandall Stewart 	num_stream = ntohs(str_add->number_of_streams);
4007ea44232bSRandall Stewart 	/* Now what would be the new total? */
40088aae9493SRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
4009ea44232bSRandall Stewart 		num_stream += stcb->asoc.streamincnt;
4010f3ebe64cSMichael Tuexen 		stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4011f3ebe64cSMichael Tuexen 		if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4012f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4013f3ebe64cSMichael Tuexen 		} else if ((num_stream > stcb->asoc.max_inbound_streams) ||
4014c4e848b7SRandall Stewart 		    (num_stream > 0xffff)) {
4015ea44232bSRandall Stewart 			/* We must reject it they ask for to many */
4016ea44232bSRandall Stewart 	denied:
4017f3ebe64cSMichael Tuexen 			stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4018ea44232bSRandall Stewart 		} else {
4019ea44232bSRandall Stewart 			/* Ok, we can do that :-) */
4020ea44232bSRandall Stewart 			struct sctp_stream_in *oldstrm;
4021ea44232bSRandall Stewart 
4022ea44232bSRandall Stewart 			/* save off the old */
4023ea44232bSRandall Stewart 			oldstrm = stcb->asoc.strmin;
4024ea44232bSRandall Stewart 			SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
4025ea44232bSRandall Stewart 			    (num_stream * sizeof(struct sctp_stream_in)),
4026ea44232bSRandall Stewart 			    SCTP_M_STRMI);
4027ea44232bSRandall Stewart 			if (stcb->asoc.strmin == NULL) {
4028ea44232bSRandall Stewart 				stcb->asoc.strmin = oldstrm;
4029ea44232bSRandall Stewart 				goto denied;
4030ea44232bSRandall Stewart 			}
4031ea44232bSRandall Stewart 			/* copy off the old data */
40328aae9493SRandall Stewart 			for (i = 0; i < stcb->asoc.streamincnt; i++) {
40338aae9493SRandall Stewart 				TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
40348aae9493SRandall Stewart 				stcb->asoc.strmin[i].stream_no = i;
40358aae9493SRandall Stewart 				stcb->asoc.strmin[i].last_sequence_delivered = oldstrm[i].last_sequence_delivered;
40368aae9493SRandall Stewart 				stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
40378aae9493SRandall Stewart 				/* now anything on those queues? */
40384a9ef3f8SMichael Tuexen 				TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next, nctl) {
40398aae9493SRandall Stewart 					TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next);
40408aae9493SRandall Stewart 					TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next);
40418aae9493SRandall Stewart 				}
40428aae9493SRandall Stewart 			}
4043ea44232bSRandall Stewart 			/* Init the new streams */
4044ea44232bSRandall Stewart 			for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
4045ea44232bSRandall Stewart 				TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4046ea44232bSRandall Stewart 				stcb->asoc.strmin[i].stream_no = i;
4047ea44232bSRandall Stewart 				stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
4048ea44232bSRandall Stewart 				stcb->asoc.strmin[i].delivery_started = 0;
4049ea44232bSRandall Stewart 			}
4050ea44232bSRandall Stewart 			SCTP_FREE(oldstrm, SCTP_M_STRMI);
4051ea44232bSRandall Stewart 			/* update the size */
4052ea44232bSRandall Stewart 			stcb->asoc.streamincnt = num_stream;
4053f3ebe64cSMichael Tuexen 			stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4054c4e848b7SRandall Stewart 			sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
4055ea44232bSRandall Stewart 		}
4056c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4057c4e848b7SRandall Stewart 		asoc->str_reset_seq_in++;
40588aae9493SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 1) == seq) {
40598aae9493SRandall Stewart 		/*
40608aae9493SRandall Stewart 		 * one seq back, just echo back last action since my
40618aae9493SRandall Stewart 		 * response was lost.
40628aae9493SRandall Stewart 		 */
40638aae9493SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
40648aae9493SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 2) == seq) {
40658aae9493SRandall Stewart 		/*
40668aae9493SRandall Stewart 		 * two seq back, just echo back last action since my
40678aae9493SRandall Stewart 		 * response was lost.
40688aae9493SRandall Stewart 		 */
40698aae9493SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
40708aae9493SRandall Stewart 	} else {
4071f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
40728aae9493SRandall Stewart 
40738aae9493SRandall Stewart 	}
4074ea44232bSRandall Stewart }
4075ea44232bSRandall Stewart 
4076c4e848b7SRandall Stewart static void
4077c4e848b7SRandall Stewart sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4078c4e848b7SRandall Stewart     struct sctp_stream_reset_add_strm *str_add)
4079c4e848b7SRandall Stewart {
4080c4e848b7SRandall Stewart 	/*
4081c4e848b7SRandall Stewart 	 * Peer is requesting to add more streams. If its within our
4082c4e848b7SRandall Stewart 	 * max-streams we will allow it.
4083c4e848b7SRandall Stewart 	 */
4084c4e848b7SRandall Stewart 	uint16_t num_stream;
4085c4e848b7SRandall Stewart 	uint32_t seq;
4086c4e848b7SRandall Stewart 	struct sctp_association *asoc = &stcb->asoc;
4087c4e848b7SRandall Stewart 
4088c4e848b7SRandall Stewart 	/* Get the number. */
4089c4e848b7SRandall Stewart 	seq = ntohl(str_add->request_seq);
4090c4e848b7SRandall Stewart 	num_stream = ntohs(str_add->number_of_streams);
4091c4e848b7SRandall Stewart 	/* Now what would be the new total? */
4092c4e848b7SRandall Stewart 	if (asoc->str_reset_seq_in == seq) {
4093c4e848b7SRandall Stewart 		stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4094f3ebe64cSMichael Tuexen 		if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4095f3ebe64cSMichael Tuexen 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4096f3ebe64cSMichael Tuexen 		} else if (stcb->asoc.stream_reset_outstanding) {
4097f3ebe64cSMichael Tuexen 			/* We must reject it we have something pending */
4098f3ebe64cSMichael Tuexen 			stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
4099c4e848b7SRandall Stewart 		} else {
4100c4e848b7SRandall Stewart 			/* Ok, we can do that :-) */
4101c4e848b7SRandall Stewart 			int mychk;
4102c4e848b7SRandall Stewart 
4103c4e848b7SRandall Stewart 			mychk = stcb->asoc.streamoutcnt;
4104c4e848b7SRandall Stewart 			mychk += num_stream;
4105c4e848b7SRandall Stewart 			if (mychk < 0x10000) {
4106f3ebe64cSMichael Tuexen 				stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
41077cca1775SRandall Stewart 				if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 1, num_stream, 0, 1)) {
4108f3ebe64cSMichael Tuexen 					stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4109c4e848b7SRandall Stewart 				}
4110c4e848b7SRandall Stewart 			} else {
4111f3ebe64cSMichael Tuexen 				stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4112c4e848b7SRandall Stewart 			}
4113c4e848b7SRandall Stewart 		}
4114c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
4115c4e848b7SRandall Stewart 		asoc->str_reset_seq_in++;
4116c4e848b7SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 1) == seq) {
4117c4e848b7SRandall Stewart 		/*
4118c4e848b7SRandall Stewart 		 * one seq back, just echo back last action since my
4119c4e848b7SRandall Stewart 		 * response was lost.
4120c4e848b7SRandall Stewart 		 */
4121c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4122c4e848b7SRandall Stewart 	} else if ((asoc->str_reset_seq_in - 2) == seq) {
4123c4e848b7SRandall Stewart 		/*
4124c4e848b7SRandall Stewart 		 * two seq back, just echo back last action since my
4125c4e848b7SRandall Stewart 		 * response was lost.
4126c4e848b7SRandall Stewart 		 */
4127c4e848b7SRandall Stewart 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4128c4e848b7SRandall Stewart 	} else {
4129f3ebe64cSMichael Tuexen 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4130c4e848b7SRandall Stewart 	}
4131c4e848b7SRandall Stewart }
4132c4e848b7SRandall Stewart 
4133671d309cSRandall Stewart #ifdef __GNUC__
4134671d309cSRandall Stewart __attribute__((noinline))
4135671d309cSRandall Stewart #endif
4136f8829a4aSRandall Stewart 	static int
4137671d309cSRandall Stewart 	    sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
4138a169d6ecSMichael Tuexen         struct sctp_chunkhdr *ch_req)
4139f8829a4aSRandall Stewart {
41406a58f0e9SXin LI 	uint16_t remaining_length, param_len, ptype;
4141671d309cSRandall Stewart 	struct sctp_paramhdr pstore;
4142671d309cSRandall Stewart 	uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
4143c4e848b7SRandall Stewart 	uint32_t seq = 0;
4144f8829a4aSRandall Stewart 	int num_req = 0;
4145671d309cSRandall Stewart 	int trunc = 0;
4146f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
4147f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
4148f8829a4aSRandall Stewart 	struct sctp_paramhdr *ph;
4149f42a358aSRandall Stewart 	int ret_code = 0;
4150f42a358aSRandall Stewart 	int num_param = 0;
4151f8829a4aSRandall Stewart 
4152f8829a4aSRandall Stewart 	/* now it may be a reset or a reset-response */
41536a58f0e9SXin LI 	remaining_length = ntohs(ch_req->chunk_length) - sizeof(struct sctp_chunkhdr);
4154f8829a4aSRandall Stewart 
4155f8829a4aSRandall Stewart 	/* setup for adding the response */
4156f8829a4aSRandall Stewart 	sctp_alloc_a_chunk(stcb, chk);
4157f8829a4aSRandall Stewart 	if (chk == NULL) {
4158f8829a4aSRandall Stewart 		return (ret_code);
4159f8829a4aSRandall Stewart 	}
4160e03159eaSMichael Tuexen 	chk->copy_by_ref = 0;
4161f8829a4aSRandall Stewart 	chk->rec.chunk_id.id = SCTP_STREAM_RESET;
4162d06c82f1SRandall Stewart 	chk->rec.chunk_id.can_take_data = 0;
4163e03159eaSMichael Tuexen 	chk->flags = 0;
4164f8829a4aSRandall Stewart 	chk->asoc = &stcb->asoc;
4165f8829a4aSRandall Stewart 	chk->no_fr_allowed = 0;
4166f8829a4aSRandall Stewart 	chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
416744b7479bSRandall Stewart 	chk->book_size_scale = 0;
4168eb1b1807SGleb Smirnoff 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
4169f8829a4aSRandall Stewart 	if (chk->data == NULL) {
4170f8829a4aSRandall Stewart strres_nochunk:
4171f8829a4aSRandall Stewart 		if (chk->data) {
4172f8829a4aSRandall Stewart 			sctp_m_freem(chk->data);
4173f8829a4aSRandall Stewart 			chk->data = NULL;
4174f8829a4aSRandall Stewart 		}
4175689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4176f8829a4aSRandall Stewart 		return (ret_code);
4177f8829a4aSRandall Stewart 	}
4178139bc87fSRandall Stewart 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4179f8829a4aSRandall Stewart 
4180f8829a4aSRandall Stewart 	/* setup chunk parameters */
4181f8829a4aSRandall Stewart 	chk->sent = SCTP_DATAGRAM_UNSENT;
4182f8829a4aSRandall Stewart 	chk->snd_count = 0;
4183ca85e948SMichael Tuexen 	chk->whoTo = NULL;
4184f8829a4aSRandall Stewart 
4185f8829a4aSRandall Stewart 	ch = mtod(chk->data, struct sctp_chunkhdr *);
4186f8829a4aSRandall Stewart 	ch->chunk_type = SCTP_STREAM_RESET;
4187f8829a4aSRandall Stewart 	ch->chunk_flags = 0;
4188f8829a4aSRandall Stewart 	ch->chunk_length = htons(chk->send_size);
4189139bc87fSRandall Stewart 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4190671d309cSRandall Stewart 	offset += sizeof(struct sctp_chunkhdr);
41916a58f0e9SXin LI 	while (remaining_length >= sizeof(struct sctp_paramhdr)) {
4192671d309cSRandall Stewart 		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore);
41936a58f0e9SXin LI 		if (ph == NULL) {
41946a58f0e9SXin LI 			/* TSNH */
4195f8829a4aSRandall Stewart 			break;
4196f8829a4aSRandall Stewart 		}
41976a58f0e9SXin LI 		param_len = ntohs(ph->param_length);
41986a58f0e9SXin LI 		if ((param_len > remaining_length) ||
41996a58f0e9SXin LI 		    (param_len < (sizeof(struct sctp_paramhdr) + sizeof(uint32_t)))) {
42006a58f0e9SXin LI 			/* bad parameter length */
42016a58f0e9SXin LI 			break;
42026a58f0e9SXin LI 		}
42036a58f0e9SXin LI 		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, sizeof(cstore)),
4204671d309cSRandall Stewart 		    (uint8_t *) & cstore);
42056a58f0e9SXin LI 		if (ph == NULL) {
42066a58f0e9SXin LI 			/* TSNH */
42076a58f0e9SXin LI 			break;
42086a58f0e9SXin LI 		}
4209f8829a4aSRandall Stewart 		ptype = ntohs(ph->param_type);
4210f8829a4aSRandall Stewart 		num_param++;
42116a58f0e9SXin LI 		if (param_len > sizeof(cstore)) {
4212671d309cSRandall Stewart 			trunc = 1;
4213671d309cSRandall Stewart 		} else {
4214671d309cSRandall Stewart 			trunc = 0;
4215671d309cSRandall Stewart 		}
4216f8829a4aSRandall Stewart 		if (num_param > SCTP_MAX_RESET_PARAMS) {
4217f8829a4aSRandall Stewart 			/* hit the max of parameters already sorry.. */
4218f8829a4aSRandall Stewart 			break;
4219f8829a4aSRandall Stewart 		}
4220f8829a4aSRandall Stewart 		if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4221f8829a4aSRandall Stewart 			struct sctp_stream_reset_out_request *req_out;
4222f8829a4aSRandall Stewart 
42236a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_out_request)) {
42246a58f0e9SXin LI 				break;
42256a58f0e9SXin LI 			}
4226f8829a4aSRandall Stewart 			req_out = (struct sctp_stream_reset_out_request *)ph;
4227f8829a4aSRandall Stewart 			num_req++;
4228f8829a4aSRandall Stewart 			if (stcb->asoc.stream_reset_outstanding) {
4229f8829a4aSRandall Stewart 				seq = ntohl(req_out->response_seq);
4230f8829a4aSRandall Stewart 				if (seq == stcb->asoc.str_reset_seq_out) {
4231f8829a4aSRandall Stewart 					/* implicit ack */
4232f3ebe64cSMichael Tuexen 					(void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4233f8829a4aSRandall Stewart 				}
4234f8829a4aSRandall Stewart 			}
4235671d309cSRandall Stewart 			sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4236c4e848b7SRandall Stewart 		} else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4237ea44232bSRandall Stewart 			struct sctp_stream_reset_add_strm *str_add;
4238ea44232bSRandall Stewart 
42396a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
42406a58f0e9SXin LI 				break;
42416a58f0e9SXin LI 			}
4242ea44232bSRandall Stewart 			str_add = (struct sctp_stream_reset_add_strm *)ph;
4243ea44232bSRandall Stewart 			num_req++;
4244ea44232bSRandall Stewart 			sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4245c4e848b7SRandall Stewart 		} else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4246c4e848b7SRandall Stewart 			struct sctp_stream_reset_add_strm *str_add;
4247c4e848b7SRandall Stewart 
42486a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
42496a58f0e9SXin LI 				break;
42506a58f0e9SXin LI 			}
4251c4e848b7SRandall Stewart 			str_add = (struct sctp_stream_reset_add_strm *)ph;
4252c4e848b7SRandall Stewart 			num_req++;
4253c4e848b7SRandall Stewart 			sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4254f8829a4aSRandall Stewart 		} else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4255f8829a4aSRandall Stewart 			struct sctp_stream_reset_in_request *req_in;
4256f8829a4aSRandall Stewart 
4257f8829a4aSRandall Stewart 			num_req++;
4258f8829a4aSRandall Stewart 			req_in = (struct sctp_stream_reset_in_request *)ph;
4259671d309cSRandall Stewart 			sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4260f8829a4aSRandall Stewart 		} else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4261f8829a4aSRandall Stewart 			struct sctp_stream_reset_tsn_request *req_tsn;
4262f8829a4aSRandall Stewart 
4263f8829a4aSRandall Stewart 			num_req++;
4264f8829a4aSRandall Stewart 			req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4265f8829a4aSRandall Stewart 			if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4266f8829a4aSRandall Stewart 				ret_code = 1;
4267f8829a4aSRandall Stewart 				goto strres_nochunk;
4268f8829a4aSRandall Stewart 			}
4269f8829a4aSRandall Stewart 			/* no more */
4270f8829a4aSRandall Stewart 			break;
4271f8829a4aSRandall Stewart 		} else if (ptype == SCTP_STR_RESET_RESPONSE) {
4272f8829a4aSRandall Stewart 			struct sctp_stream_reset_response *resp;
4273f8829a4aSRandall Stewart 			uint32_t result;
4274f8829a4aSRandall Stewart 
42756a58f0e9SXin LI 			if (param_len < sizeof(struct sctp_stream_reset_response)) {
42766a58f0e9SXin LI 				break;
42776a58f0e9SXin LI 			}
4278f8829a4aSRandall Stewart 			resp = (struct sctp_stream_reset_response *)ph;
4279f8829a4aSRandall Stewart 			seq = ntohl(resp->response_seq);
4280f8829a4aSRandall Stewart 			result = ntohl(resp->result);
4281f8829a4aSRandall Stewart 			if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4282f8829a4aSRandall Stewart 				ret_code = 1;
4283f8829a4aSRandall Stewart 				goto strres_nochunk;
4284f8829a4aSRandall Stewart 			}
4285f8829a4aSRandall Stewart 		} else {
4286f8829a4aSRandall Stewart 			break;
4287f8829a4aSRandall Stewart 		}
4288671d309cSRandall Stewart 		offset += SCTP_SIZE32(param_len);
42896a58f0e9SXin LI 		if (remaining_length >= SCTP_SIZE32(param_len)) {
42906a58f0e9SXin LI 			remaining_length -= SCTP_SIZE32(param_len);
42916a58f0e9SXin LI 		} else {
42926a58f0e9SXin LI 			remaining_length = 0;
42936a58f0e9SXin LI 		}
4294f8829a4aSRandall Stewart 	}
4295f8829a4aSRandall Stewart 	if (num_req == 0) {
4296f8829a4aSRandall Stewart 		/* we have no response free the stuff */
4297f8829a4aSRandall Stewart 		goto strres_nochunk;
4298f8829a4aSRandall Stewart 	}
4299f8829a4aSRandall Stewart 	/* ok we have a chunk to link in */
4300f8829a4aSRandall Stewart 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4301f8829a4aSRandall Stewart 	    chk,
4302f8829a4aSRandall Stewart 	    sctp_next);
4303f8829a4aSRandall Stewart 	stcb->asoc.ctrl_queue_cnt++;
4304f8829a4aSRandall Stewart 	return (ret_code);
4305f8829a4aSRandall Stewart }
4306f8829a4aSRandall Stewart 
4307f8829a4aSRandall Stewart /*
4308f8829a4aSRandall Stewart  * Handle a router or endpoints report of a packet loss, there are two ways
4309f8829a4aSRandall Stewart  * to handle this, either we get the whole packet and must disect it
4310f8829a4aSRandall Stewart  * ourselves (possibly with truncation and or corruption) or it is a summary
4311f8829a4aSRandall Stewart  * from a middle box that did the disectting for us.
4312f8829a4aSRandall Stewart  */
4313f8829a4aSRandall Stewart static void
4314f8829a4aSRandall Stewart sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4315458303daSRandall Stewart     struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4316f8829a4aSRandall Stewart {
4317f8829a4aSRandall Stewart 	uint32_t bottle_bw, on_queue;
4318f8829a4aSRandall Stewart 	uint16_t trunc_len;
4319f8829a4aSRandall Stewart 	unsigned int chlen;
4320f8829a4aSRandall Stewart 	unsigned int at;
4321f8829a4aSRandall Stewart 	struct sctp_chunk_desc desc;
4322f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
4323f8829a4aSRandall Stewart 
4324f8829a4aSRandall Stewart 	chlen = ntohs(cp->ch.chunk_length);
4325f8829a4aSRandall Stewart 	chlen -= sizeof(struct sctp_pktdrop_chunk);
4326f8829a4aSRandall Stewart 	/* XXX possible chlen underflow */
4327f8829a4aSRandall Stewart 	if (chlen == 0) {
4328f8829a4aSRandall Stewart 		ch = NULL;
4329f8829a4aSRandall Stewart 		if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4330f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_pdrpbwrpt);
4331f8829a4aSRandall Stewart 	} else {
4332f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4333f8829a4aSRandall Stewart 		chlen -= sizeof(struct sctphdr);
4334f8829a4aSRandall Stewart 		/* XXX possible chlen underflow */
4335f8829a4aSRandall Stewart 		memset(&desc, 0, sizeof(desc));
4336f8829a4aSRandall Stewart 	}
4337f8829a4aSRandall Stewart 	trunc_len = (uint16_t) ntohs(cp->trunc_len);
4338458303daSRandall Stewart 	if (trunc_len > limit) {
4339458303daSRandall Stewart 		trunc_len = limit;
4340458303daSRandall Stewart 	}
4341f8829a4aSRandall Stewart 	/* now the chunks themselves */
4342f8829a4aSRandall Stewart 	while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4343f8829a4aSRandall Stewart 		desc.chunk_type = ch->chunk_type;
4344f8829a4aSRandall Stewart 		/* get amount we need to move */
4345f8829a4aSRandall Stewart 		at = ntohs(ch->chunk_length);
4346f8829a4aSRandall Stewart 		if (at < sizeof(struct sctp_chunkhdr)) {
4347f8829a4aSRandall Stewart 			/* corrupt chunk, maybe at the end? */
4348f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_pdrpcrupt);
4349f8829a4aSRandall Stewart 			break;
4350f8829a4aSRandall Stewart 		}
4351f8829a4aSRandall Stewart 		if (trunc_len == 0) {
4352f8829a4aSRandall Stewart 			/* we are supposed to have all of it */
4353f8829a4aSRandall Stewart 			if (at > chlen) {
4354f8829a4aSRandall Stewart 				/* corrupt skip it */
4355f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpcrupt);
4356f8829a4aSRandall Stewart 				break;
4357f8829a4aSRandall Stewart 			}
4358f8829a4aSRandall Stewart 		} else {
4359f8829a4aSRandall Stewart 			/* is there enough of it left ? */
4360f8829a4aSRandall Stewart 			if (desc.chunk_type == SCTP_DATA) {
4361f8829a4aSRandall Stewart 				if (chlen < (sizeof(struct sctp_data_chunk) +
4362f8829a4aSRandall Stewart 				    sizeof(desc.data_bytes))) {
4363f8829a4aSRandall Stewart 					break;
4364f8829a4aSRandall Stewart 				}
4365f8829a4aSRandall Stewart 			} else {
4366f8829a4aSRandall Stewart 				if (chlen < sizeof(struct sctp_chunkhdr)) {
4367f8829a4aSRandall Stewart 					break;
4368f8829a4aSRandall Stewart 				}
4369f8829a4aSRandall Stewart 			}
4370f8829a4aSRandall Stewart 		}
4371f8829a4aSRandall Stewart 		if (desc.chunk_type == SCTP_DATA) {
4372f8829a4aSRandall Stewart 			/* can we get out the tsn? */
4373f8829a4aSRandall Stewart 			if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4374f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpmbda);
4375f8829a4aSRandall Stewart 
4376f8829a4aSRandall Stewart 			if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4377f8829a4aSRandall Stewart 				/* yep */
4378f8829a4aSRandall Stewart 				struct sctp_data_chunk *dcp;
4379f8829a4aSRandall Stewart 				uint8_t *ddp;
4380f8829a4aSRandall Stewart 				unsigned int iii;
4381f8829a4aSRandall Stewart 
4382f8829a4aSRandall Stewart 				dcp = (struct sctp_data_chunk *)ch;
4383f8829a4aSRandall Stewart 				ddp = (uint8_t *) (dcp + 1);
4384f8829a4aSRandall Stewart 				for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4385f8829a4aSRandall Stewart 					desc.data_bytes[iii] = ddp[iii];
4386f8829a4aSRandall Stewart 				}
4387f8829a4aSRandall Stewart 				desc.tsn_ifany = dcp->dp.tsn;
4388f8829a4aSRandall Stewart 			} else {
4389f8829a4aSRandall Stewart 				/* nope we are done. */
4390f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpnedat);
4391f8829a4aSRandall Stewart 				break;
4392f8829a4aSRandall Stewart 			}
4393f8829a4aSRandall Stewart 		} else {
4394f8829a4aSRandall Stewart 			if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4395f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_pdrpmbct);
4396f8829a4aSRandall Stewart 		}
4397f8829a4aSRandall Stewart 
4398f8829a4aSRandall Stewart 		if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4399f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_pdrppdbrk);
4400f8829a4aSRandall Stewart 			break;
4401f8829a4aSRandall Stewart 		}
4402f8829a4aSRandall Stewart 		if (SCTP_SIZE32(at) > chlen) {
4403f8829a4aSRandall Stewart 			break;
4404f8829a4aSRandall Stewart 		}
4405f8829a4aSRandall Stewart 		chlen -= SCTP_SIZE32(at);
4406f8829a4aSRandall Stewart 		if (chlen < sizeof(struct sctp_chunkhdr)) {
4407f8829a4aSRandall Stewart 			/* done, none left */
4408f8829a4aSRandall Stewart 			break;
4409f8829a4aSRandall Stewart 		}
4410f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4411f8829a4aSRandall Stewart 	}
4412f8829a4aSRandall Stewart 	/* Now update any rwnd --- possibly */
4413f8829a4aSRandall Stewart 	if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4414f8829a4aSRandall Stewart 		/* From a peer, we get a rwnd report */
4415f8829a4aSRandall Stewart 		uint32_t a_rwnd;
4416f8829a4aSRandall Stewart 
4417f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_pdrpfehos);
4418f8829a4aSRandall Stewart 
4419f8829a4aSRandall Stewart 		bottle_bw = ntohl(cp->bottle_bw);
4420f8829a4aSRandall Stewart 		on_queue = ntohl(cp->current_onq);
4421f8829a4aSRandall Stewart 		if (bottle_bw && on_queue) {
4422f8829a4aSRandall Stewart 			/* a rwnd report is in here */
4423f8829a4aSRandall Stewart 			if (bottle_bw > on_queue)
4424f8829a4aSRandall Stewart 				a_rwnd = bottle_bw - on_queue;
4425f8829a4aSRandall Stewart 			else
4426f8829a4aSRandall Stewart 				a_rwnd = 0;
4427f8829a4aSRandall Stewart 
4428f8829a4aSRandall Stewart 			if (a_rwnd == 0)
4429f8829a4aSRandall Stewart 				stcb->asoc.peers_rwnd = 0;
4430f8829a4aSRandall Stewart 			else {
4431f8829a4aSRandall Stewart 				if (a_rwnd > stcb->asoc.total_flight) {
4432f8829a4aSRandall Stewart 					stcb->asoc.peers_rwnd =
4433f8829a4aSRandall Stewart 					    a_rwnd - stcb->asoc.total_flight;
4434f8829a4aSRandall Stewart 				} else {
4435f8829a4aSRandall Stewart 					stcb->asoc.peers_rwnd = 0;
4436f8829a4aSRandall Stewart 				}
4437f8829a4aSRandall Stewart 				if (stcb->asoc.peers_rwnd <
4438f8829a4aSRandall Stewart 				    stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4439f8829a4aSRandall Stewart 					/* SWS sender side engages */
4440f8829a4aSRandall Stewart 					stcb->asoc.peers_rwnd = 0;
4441f8829a4aSRandall Stewart 				}
4442f8829a4aSRandall Stewart 			}
4443f8829a4aSRandall Stewart 		}
4444f8829a4aSRandall Stewart 	} else {
4445f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_pdrpfmbox);
4446f8829a4aSRandall Stewart 	}
4447f8829a4aSRandall Stewart 
4448f8829a4aSRandall Stewart 	/* now middle boxes in sat networks get a cwnd bump */
4449f8829a4aSRandall Stewart 	if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4450f8829a4aSRandall Stewart 	    (stcb->asoc.sat_t3_loss_recovery == 0) &&
4451f8829a4aSRandall Stewart 	    (stcb->asoc.sat_network)) {
4452f8829a4aSRandall Stewart 		/*
4453f8829a4aSRandall Stewart 		 * This is debateable but for sat networks it makes sense
4454f8829a4aSRandall Stewart 		 * Note if a T3 timer has went off, we will prohibit any
4455f8829a4aSRandall Stewart 		 * changes to cwnd until we exit the t3 loss recovery.
4456f8829a4aSRandall Stewart 		 */
4457b54d3a6cSRandall Stewart 		stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4458b54d3a6cSRandall Stewart 		    net, cp, &bottle_bw, &on_queue);
4459f8829a4aSRandall Stewart 	}
4460f8829a4aSRandall Stewart }
4461f8829a4aSRandall Stewart 
4462f8829a4aSRandall Stewart /*
4463f8829a4aSRandall Stewart  * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4464f8829a4aSRandall Stewart  * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4465f8829a4aSRandall Stewart  * offset: offset into the mbuf chain to first chunkhdr - length: is the
4466f8829a4aSRandall Stewart  * length of the complete packet outputs: - length: modified to remaining
4467f8829a4aSRandall Stewart  * length after control processing - netp: modified to new sctp_nets after
4468f8829a4aSRandall Stewart  * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4469f8829a4aSRandall Stewart  * bad packet,...) otherwise return the tcb for this packet
4470f8829a4aSRandall Stewart  */
44713c6f3536SRandall Stewart #ifdef __GNUC__
44723c6f3536SRandall Stewart __attribute__((noinline))
44733c6f3536SRandall Stewart #endif
4474f8829a4aSRandall Stewart 	static struct sctp_tcb *
4475f8829a4aSRandall Stewart 	         sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4476b1754ad1SMichael Tuexen              struct sockaddr *src, struct sockaddr *dst,
4477f8829a4aSRandall Stewart              struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
447817205eccSRandall Stewart              struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4479d089f9b9SMichael Tuexen              uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4480c54a18d2SRandall Stewart              uint32_t vrf_id, uint16_t port)
4481f8829a4aSRandall Stewart {
4482f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4483ff1ffd74SMichael Tuexen 	struct mbuf *op_err;
4484ff1ffd74SMichael Tuexen 	char msg[SCTP_DIAG_INFO_LEN];
4485f8829a4aSRandall Stewart 	uint32_t vtag_in;
4486f8829a4aSRandall Stewart 	int num_chunks = 0;	/* number of control chunks processed */
44874c9179adSRandall Stewart 	uint32_t chk_length;
4488f8829a4aSRandall Stewart 	int ret;
4489bff64a4dSRandall Stewart 	int abort_no_unlock = 0;
4490899288aeSRandall Stewart 	int ecne_seen = 0;
4491f8829a4aSRandall Stewart 
4492f8829a4aSRandall Stewart 	/*
4493f8829a4aSRandall Stewart 	 * How big should this be, and should it be alloc'd? Lets try the
4494f8829a4aSRandall Stewart 	 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4495f8829a4aSRandall Stewart 	 * until we get into jumbo grams and such..
4496f8829a4aSRandall Stewart 	 */
4497f42a358aSRandall Stewart 	uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4498f8829a4aSRandall Stewart 	struct sctp_tcb *locked_tcb = stcb;
4499f8829a4aSRandall Stewart 	int got_auth = 0;
4500f8829a4aSRandall Stewart 	uint32_t auth_offset = 0, auth_len = 0;
4501f8829a4aSRandall Stewart 	int auth_skipped = 0;
45022afb3e84SRandall Stewart 	int asconf_cnt = 0;
4503f8829a4aSRandall Stewart 
4504ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4505ceaad40aSRandall Stewart 	struct socket *so;
4506ceaad40aSRandall Stewart 
4507ceaad40aSRandall Stewart #endif
4508ceaad40aSRandall Stewart 
4509ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4510dd294dceSMichael Tuexen 	    iphlen, *offset, length, (void *)stcb);
4511f8829a4aSRandall Stewart 
4512f8829a4aSRandall Stewart 	/* validate chunk header length... */
4513f8829a4aSRandall Stewart 	if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4514d61a0ae0SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4515d61a0ae0SRandall Stewart 		    ntohs(ch->chunk_length));
4516207304d4SRandall Stewart 		if (locked_tcb) {
4517207304d4SRandall Stewart 			SCTP_TCB_UNLOCK(locked_tcb);
4518207304d4SRandall Stewart 		}
4519f8829a4aSRandall Stewart 		return (NULL);
4520f8829a4aSRandall Stewart 	}
4521f8829a4aSRandall Stewart 	/*
4522f8829a4aSRandall Stewart 	 * validate the verification tag
4523f8829a4aSRandall Stewart 	 */
4524f8829a4aSRandall Stewart 	vtag_in = ntohl(sh->v_tag);
4525f8829a4aSRandall Stewart 
4526a5d547adSRandall Stewart 	if (locked_tcb) {
4527a5d547adSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(locked_tcb);
4528a5d547adSRandall Stewart 	}
4529f8829a4aSRandall Stewart 	if (ch->chunk_type == SCTP_INITIATION) {
4530d61a0ae0SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4531d61a0ae0SRandall Stewart 		    ntohs(ch->chunk_length), vtag_in);
4532f8829a4aSRandall Stewart 		if (vtag_in != 0) {
4533f8829a4aSRandall Stewart 			/* protocol error- silently discard... */
4534f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_badvtag);
45356e55db54SRandall Stewart 			if (locked_tcb) {
4536f8829a4aSRandall Stewart 				SCTP_TCB_UNLOCK(locked_tcb);
45376e55db54SRandall Stewart 			}
4538f8829a4aSRandall Stewart 			return (NULL);
4539f8829a4aSRandall Stewart 		}
4540f8829a4aSRandall Stewart 	} else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4541f8829a4aSRandall Stewart 		/*
4542f8829a4aSRandall Stewart 		 * If there is no stcb, skip the AUTH chunk and process
4543f8829a4aSRandall Stewart 		 * later after a stcb is found (to validate the lookup was
4544f8829a4aSRandall Stewart 		 * valid.
4545f8829a4aSRandall Stewart 		 */
4546f8829a4aSRandall Stewart 		if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4547b3f1ea41SRandall Stewart 		    (stcb == NULL) &&
4548c79bec9cSMichael Tuexen 		    (inp->auth_supported == 1)) {
4549f8829a4aSRandall Stewart 			/* save this chunk for later processing */
4550f8829a4aSRandall Stewart 			auth_skipped = 1;
4551f8829a4aSRandall Stewart 			auth_offset = *offset;
4552f8829a4aSRandall Stewart 			auth_len = ntohs(ch->chunk_length);
4553f8829a4aSRandall Stewart 
4554f8829a4aSRandall Stewart 			/* (temporarily) move past this chunk */
4555f8829a4aSRandall Stewart 			*offset += SCTP_SIZE32(auth_len);
4556f8829a4aSRandall Stewart 			if (*offset >= length) {
4557f8829a4aSRandall Stewart 				/* no more data left in the mbuf chain */
4558f8829a4aSRandall Stewart 				*offset = length;
4559207304d4SRandall Stewart 				if (locked_tcb) {
4560207304d4SRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
4561207304d4SRandall Stewart 				}
4562f8829a4aSRandall Stewart 				return (NULL);
4563f8829a4aSRandall Stewart 			}
4564f8829a4aSRandall Stewart 			ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4565f8829a4aSRandall Stewart 			    sizeof(struct sctp_chunkhdr), chunk_buf);
4566f8829a4aSRandall Stewart 		}
4567ad81507eSRandall Stewart 		if (ch == NULL) {
4568ad81507eSRandall Stewart 			/* Help */
4569ad81507eSRandall Stewart 			*offset = length;
4570207304d4SRandall Stewart 			if (locked_tcb) {
4571207304d4SRandall Stewart 				SCTP_TCB_UNLOCK(locked_tcb);
4572207304d4SRandall Stewart 			}
4573ad81507eSRandall Stewart 			return (NULL);
4574ad81507eSRandall Stewart 		}
4575f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4576f8829a4aSRandall Stewart 			goto process_control_chunks;
4577f8829a4aSRandall Stewart 		}
4578f8829a4aSRandall Stewart 		/*
4579f8829a4aSRandall Stewart 		 * first check if it's an ASCONF with an unknown src addr we
4580f8829a4aSRandall Stewart 		 * need to look inside to find the association
4581f8829a4aSRandall Stewart 		 */
4582f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
45832afb3e84SRandall Stewart 			struct sctp_chunkhdr *asconf_ch = ch;
45842afb3e84SRandall Stewart 			uint32_t asconf_offset = 0, asconf_len = 0;
45852afb3e84SRandall Stewart 
4586f8829a4aSRandall Stewart 			/* inp's refcount may be reduced */
4587f8829a4aSRandall Stewart 			SCTP_INP_INCR_REF(inp);
4588f8829a4aSRandall Stewart 
45892afb3e84SRandall Stewart 			asconf_offset = *offset;
45902afb3e84SRandall Stewart 			do {
45912afb3e84SRandall Stewart 				asconf_len = ntohs(asconf_ch->chunk_length);
45922afb3e84SRandall Stewart 				if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
45932afb3e84SRandall Stewart 					break;
45947215cc1bSMichael Tuexen 				stcb = sctp_findassociation_ep_asconf(m,
4595b1754ad1SMichael Tuexen 				    *offset,
4596b1754ad1SMichael Tuexen 				    dst,
4597b1754ad1SMichael Tuexen 				    sh, &inp, netp, vrf_id);
45982afb3e84SRandall Stewart 				if (stcb != NULL)
45992afb3e84SRandall Stewart 					break;
46002afb3e84SRandall Stewart 				asconf_offset += SCTP_SIZE32(asconf_len);
46012afb3e84SRandall Stewart 				asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
46022afb3e84SRandall Stewart 				    sizeof(struct sctp_chunkhdr), chunk_buf);
46032afb3e84SRandall Stewart 			} while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4604f8829a4aSRandall Stewart 			if (stcb == NULL) {
4605f8829a4aSRandall Stewart 				/*
4606f8829a4aSRandall Stewart 				 * reduce inp's refcount if not reduced in
4607f8829a4aSRandall Stewart 				 * sctp_findassociation_ep_asconf().
4608f8829a4aSRandall Stewart 				 */
4609f8829a4aSRandall Stewart 				SCTP_INP_DECR_REF(inp);
46102afb3e84SRandall Stewart 			} else {
46112afb3e84SRandall Stewart 				locked_tcb = stcb;
4612f8829a4aSRandall Stewart 			}
46132afb3e84SRandall Stewart 
4614f8829a4aSRandall Stewart 			/* now go back and verify any auth chunk to be sure */
4615f8829a4aSRandall Stewart 			if (auth_skipped && (stcb != NULL)) {
4616f8829a4aSRandall Stewart 				struct sctp_auth_chunk *auth;
4617f8829a4aSRandall Stewart 
4618f8829a4aSRandall Stewart 				auth = (struct sctp_auth_chunk *)
4619f8829a4aSRandall Stewart 				    sctp_m_getptr(m, auth_offset,
4620f8829a4aSRandall Stewart 				    auth_len, chunk_buf);
4621f8829a4aSRandall Stewart 				got_auth = 1;
4622f8829a4aSRandall Stewart 				auth_skipped = 0;
4623ad81507eSRandall Stewart 				if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4624f8829a4aSRandall Stewart 				    auth_offset)) {
4625f8829a4aSRandall Stewart 					/* auth HMAC failed so dump it */
4626f8829a4aSRandall Stewart 					*offset = length;
4627207304d4SRandall Stewart 					if (locked_tcb) {
4628207304d4SRandall Stewart 						SCTP_TCB_UNLOCK(locked_tcb);
4629207304d4SRandall Stewart 					}
4630f8829a4aSRandall Stewart 					return (NULL);
4631f8829a4aSRandall Stewart 				} else {
4632f8829a4aSRandall Stewart 					/* remaining chunks are HMAC checked */
4633f8829a4aSRandall Stewart 					stcb->asoc.authenticated = 1;
4634f8829a4aSRandall Stewart 				}
4635f8829a4aSRandall Stewart 			}
4636f8829a4aSRandall Stewart 		}
4637f8829a4aSRandall Stewart 		if (stcb == NULL) {
46386e9c45e0SMichael Tuexen 			snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4639ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4640ff1ffd74SMichael Tuexen 			    msg);
4641f8829a4aSRandall Stewart 			/* no association, so it's out of the blue... */
4642ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp, op_err,
4643d089f9b9SMichael Tuexen 			    mflowtype, mflowid, inp->fibnum,
4644c54a18d2SRandall Stewart 			    vrf_id, port);
4645f8829a4aSRandall Stewart 			*offset = length;
46466e55db54SRandall Stewart 			if (locked_tcb) {
4647f8829a4aSRandall Stewart 				SCTP_TCB_UNLOCK(locked_tcb);
46486e55db54SRandall Stewart 			}
4649f8829a4aSRandall Stewart 			return (NULL);
4650f8829a4aSRandall Stewart 		}
4651f8829a4aSRandall Stewart 		asoc = &stcb->asoc;
4652f8829a4aSRandall Stewart 		/* ABORT and SHUTDOWN can use either v_tag... */
4653f8829a4aSRandall Stewart 		if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4654f8829a4aSRandall Stewart 		    (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4655f8829a4aSRandall Stewart 		    (ch->chunk_type == SCTP_PACKET_DROPPED)) {
46565db47b3dSMichael Tuexen 			/* Take the T-bit always into account. */
46575db47b3dSMichael Tuexen 			if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) &&
46585db47b3dSMichael Tuexen 			    (vtag_in == asoc->my_vtag)) ||
46595db47b3dSMichael Tuexen 			    (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) &&
4660f8829a4aSRandall Stewart 			    (vtag_in == asoc->peer_vtag))) {
4661f8829a4aSRandall Stewart 				/* this is valid */
4662f8829a4aSRandall Stewart 			} else {
4663f8829a4aSRandall Stewart 				/* drop this packet... */
4664f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_badvtag);
46656e55db54SRandall Stewart 				if (locked_tcb) {
4666f8829a4aSRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
46676e55db54SRandall Stewart 				}
4668f8829a4aSRandall Stewart 				return (NULL);
4669f8829a4aSRandall Stewart 			}
4670f8829a4aSRandall Stewart 		} else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4671f8829a4aSRandall Stewart 			if (vtag_in != asoc->my_vtag) {
4672f8829a4aSRandall Stewart 				/*
4673f8829a4aSRandall Stewart 				 * this could be a stale SHUTDOWN-ACK or the
4674f8829a4aSRandall Stewart 				 * peer never got the SHUTDOWN-COMPLETE and
4675f8829a4aSRandall Stewart 				 * is still hung; we have started a new asoc
4676f8829a4aSRandall Stewart 				 * but it won't complete until the shutdown
4677f8829a4aSRandall Stewart 				 * is completed
4678f8829a4aSRandall Stewart 				 */
46796e55db54SRandall Stewart 				if (locked_tcb) {
4680f8829a4aSRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
46816e55db54SRandall Stewart 				}
46826e9c45e0SMichael Tuexen 				snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4683ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4684ff1ffd74SMichael Tuexen 				    msg);
4685b1754ad1SMichael Tuexen 				sctp_handle_ootb(m, iphlen, *offset, src, dst,
4686ff1ffd74SMichael Tuexen 				    sh, inp, op_err,
4687d089f9b9SMichael Tuexen 				    mflowtype, mflowid, fibnum,
4688021416fbSMichael Tuexen 				    vrf_id, port);
4689f8829a4aSRandall Stewart 				return (NULL);
4690f8829a4aSRandall Stewart 			}
4691f8829a4aSRandall Stewart 		} else {
4692f8829a4aSRandall Stewart 			/* for all other chunks, vtag must match */
4693f8829a4aSRandall Stewart 			if (vtag_in != asoc->my_vtag) {
4694f8829a4aSRandall Stewart 				/* invalid vtag... */
4695ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INPUT3,
4696ad81507eSRandall Stewart 				    "invalid vtag: %xh, expect %xh\n",
4697ad81507eSRandall Stewart 				    vtag_in, asoc->my_vtag);
4698f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_badvtag);
46996e55db54SRandall Stewart 				if (locked_tcb) {
4700f8829a4aSRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
47016e55db54SRandall Stewart 				}
4702f8829a4aSRandall Stewart 				*offset = length;
4703f8829a4aSRandall Stewart 				return (NULL);
4704f8829a4aSRandall Stewart 			}
4705f8829a4aSRandall Stewart 		}
4706f8829a4aSRandall Stewart 	}			/* end if !SCTP_COOKIE_ECHO */
4707f8829a4aSRandall Stewart 	/*
4708f8829a4aSRandall Stewart 	 * process all control chunks...
4709f8829a4aSRandall Stewart 	 */
4710f8829a4aSRandall Stewart 	if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4711830d754dSRandall Stewart 	    (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4712f8829a4aSRandall Stewart 	    (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4713f8829a4aSRandall Stewart 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
4714f8829a4aSRandall Stewart 		/* implied cookie-ack.. we must have lost the ack */
4715b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4716c4739e2fSRandall Stewart 			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4717c4739e2fSRandall Stewart 			    stcb->asoc.overall_error_count,
4718c4739e2fSRandall Stewart 			    0,
4719c4739e2fSRandall Stewart 			    SCTP_FROM_SCTP_INPUT,
4720c4739e2fSRandall Stewart 			    __LINE__);
4721c4739e2fSRandall Stewart 		}
4722f8829a4aSRandall Stewart 		stcb->asoc.overall_error_count = 0;
4723f8829a4aSRandall Stewart 		sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4724f8829a4aSRandall Stewart 		    *netp);
4725f8829a4aSRandall Stewart 	}
4726f8829a4aSRandall Stewart process_control_chunks:
4727f8829a4aSRandall Stewart 	while (IS_SCTP_CONTROL(ch)) {
4728f8829a4aSRandall Stewart 		/* validate chunk length */
4729f8829a4aSRandall Stewart 		chk_length = ntohs(ch->chunk_length);
4730ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4731f8829a4aSRandall Stewart 		    ch->chunk_type, chk_length);
473280fefe0aSRandall Stewart 		SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
47334c9179adSRandall Stewart 		if (chk_length < sizeof(*ch) ||
47344c9179adSRandall Stewart 		    (*offset + (int)chk_length) > length) {
4735f8829a4aSRandall Stewart 			*offset = length;
47366e55db54SRandall Stewart 			if (locked_tcb) {
4737f8829a4aSRandall Stewart 				SCTP_TCB_UNLOCK(locked_tcb);
47386e55db54SRandall Stewart 			}
4739f8829a4aSRandall Stewart 			return (NULL);
4740f8829a4aSRandall Stewart 		}
4741f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4742f8829a4aSRandall Stewart 		/*
4743f8829a4aSRandall Stewart 		 * INIT-ACK only gets the init ack "header" portion only
4744f8829a4aSRandall Stewart 		 * because we don't have to process the peer's COOKIE. All
4745f8829a4aSRandall Stewart 		 * others get a complete chunk.
4746f8829a4aSRandall Stewart 		 */
4747d06c82f1SRandall Stewart 		if ((ch->chunk_type == SCTP_INITIATION_ACK) ||
4748d06c82f1SRandall Stewart 		    (ch->chunk_type == SCTP_INITIATION)) {
4749f8829a4aSRandall Stewart 			/* get an init-ack chunk */
4750f8829a4aSRandall Stewart 			ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4751f8829a4aSRandall Stewart 			    sizeof(struct sctp_init_ack_chunk), chunk_buf);
4752f8829a4aSRandall Stewart 			if (ch == NULL) {
4753f8829a4aSRandall Stewart 				*offset = length;
47546e55db54SRandall Stewart 				if (locked_tcb) {
4755f8829a4aSRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
47566e55db54SRandall Stewart 				}
4757f8829a4aSRandall Stewart 				return (NULL);
4758f8829a4aSRandall Stewart 			}
47599a972525SRandall Stewart 		} else {
4760e1461651SRandall Stewart 			/* For cookies and all other chunks. */
4761d06c82f1SRandall Stewart 			if (chk_length > sizeof(chunk_buf)) {
4762d06c82f1SRandall Stewart 				/*
4763d06c82f1SRandall Stewart 				 * use just the size of the chunk buffer so
47649a972525SRandall Stewart 				 * the front part of our chunks fit in
47659a972525SRandall Stewart 				 * contiguous space up to the chunk buffer
47669a972525SRandall Stewart 				 * size (508 bytes). For chunks that need to
4767e1461651SRandall Stewart 				 * get more than that they must use the
47689a972525SRandall Stewart 				 * sctp_m_getptr() function or other means
4769e1461651SRandall Stewart 				 * (e.g. know how to parse mbuf chains).
4770e1461651SRandall Stewart 				 * Cookies do this already.
4771d06c82f1SRandall Stewart 				 */
4772d06c82f1SRandall Stewart 				ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4773d06c82f1SRandall Stewart 				    (sizeof(chunk_buf) - 4),
4774d06c82f1SRandall Stewart 				    chunk_buf);
4775d06c82f1SRandall Stewart 				if (ch == NULL) {
4776d06c82f1SRandall Stewart 					*offset = length;
47776e55db54SRandall Stewart 					if (locked_tcb) {
4778d06c82f1SRandall Stewart 						SCTP_TCB_UNLOCK(locked_tcb);
47796e55db54SRandall Stewart 					}
4780d06c82f1SRandall Stewart 					return (NULL);
4781d06c82f1SRandall Stewart 				}
4782d06c82f1SRandall Stewart 			} else {
4783d06c82f1SRandall Stewart 				/* We can fit it all */
4784f8829a4aSRandall Stewart 				ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4785f8829a4aSRandall Stewart 				    chk_length, chunk_buf);
4786f8829a4aSRandall Stewart 				if (ch == NULL) {
4787ad81507eSRandall Stewart 					SCTP_PRINTF("sctp_process_control: Can't get the all data....\n");
4788f8829a4aSRandall Stewart 					*offset = length;
47896e55db54SRandall Stewart 					if (locked_tcb) {
4790f8829a4aSRandall Stewart 						SCTP_TCB_UNLOCK(locked_tcb);
47916e55db54SRandall Stewart 					}
4792f8829a4aSRandall Stewart 					return (NULL);
4793f8829a4aSRandall Stewart 				}
4794f8829a4aSRandall Stewart 			}
47959a972525SRandall Stewart 		}
4796f8829a4aSRandall Stewart 		num_chunks++;
4797f8829a4aSRandall Stewart 		/* Save off the last place we got a control from */
4798f8829a4aSRandall Stewart 		if (stcb != NULL) {
4799ad81507eSRandall Stewart 			if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4800f8829a4aSRandall Stewart 				/*
4801f8829a4aSRandall Stewart 				 * allow last_control to be NULL if
4802f8829a4aSRandall Stewart 				 * ASCONF... ASCONF processing will find the
4803f8829a4aSRandall Stewart 				 * right net later
4804f8829a4aSRandall Stewart 				 */
4805ad81507eSRandall Stewart 				if ((netp != NULL) && (*netp != NULL))
4806f8829a4aSRandall Stewart 					stcb->asoc.last_control_chunk_from = *netp;
4807f8829a4aSRandall Stewart 			}
4808f8829a4aSRandall Stewart 		}
4809f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
4810f8829a4aSRandall Stewart 		sctp_audit_log(0xB0, ch->chunk_type);
4811f8829a4aSRandall Stewart #endif
4812f8829a4aSRandall Stewart 
4813f8829a4aSRandall Stewart 		/* check to see if this chunk required auth, but isn't */
4814b3f1ea41SRandall Stewart 		if ((stcb != NULL) &&
4815c79bec9cSMichael Tuexen 		    (stcb->asoc.auth_supported == 1) &&
4816b3f1ea41SRandall Stewart 		    sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4817f8829a4aSRandall Stewart 		    !stcb->asoc.authenticated) {
4818f8829a4aSRandall Stewart 			/* "silently" ignore */
4819f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvauthmissing);
4820f8829a4aSRandall Stewart 			goto next_chunk;
4821f8829a4aSRandall Stewart 		}
4822f8829a4aSRandall Stewart 		switch (ch->chunk_type) {
4823f8829a4aSRandall Stewart 		case SCTP_INITIATION:
4824ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4825d68fdc4dSMichael Tuexen 			/* The INIT chunk must be the only chunk. */
4826d68fdc4dSMichael Tuexen 			if ((num_chunks > 1) ||
4827ab6174d5SMichael Tuexen 			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
48280941640fSMichael Tuexen 				/* RFC 4960 requires that no ABORT is sent */
4829f8829a4aSRandall Stewart 				*offset = length;
48300941640fSMichael Tuexen 				if (locked_tcb) {
48310941640fSMichael Tuexen 					SCTP_TCB_UNLOCK(locked_tcb);
48320941640fSMichael Tuexen 				}
4833f8829a4aSRandall Stewart 				return (NULL);
4834f8829a4aSRandall Stewart 			}
4835d68fdc4dSMichael Tuexen 			/* Honor our resource limit. */
4836d68fdc4dSMichael Tuexen 			if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4837ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4838b1754ad1SMichael Tuexen 				sctp_abort_association(inp, stcb, m, iphlen,
4839b1754ad1SMichael Tuexen 				    src, dst, sh, op_err,
4840457b4b88SMichael Tuexen 				    mflowtype, mflowid,
4841f30ac432SMichael Tuexen 				    vrf_id, port);
4842f8829a4aSRandall Stewart 				*offset = length;
4843f8829a4aSRandall Stewart 				return (NULL);
4844f8829a4aSRandall Stewart 			}
4845b1754ad1SMichael Tuexen 			sctp_handle_init(m, iphlen, *offset, src, dst, sh,
4846ad81507eSRandall Stewart 			    (struct sctp_init_chunk *)ch, inp,
4847ca83f93cSMichael Tuexen 			    stcb, *netp, &abort_no_unlock,
4848457b4b88SMichael Tuexen 			    mflowtype, mflowid,
4849f30ac432SMichael Tuexen 			    vrf_id, port);
4850f8829a4aSRandall Stewart 			*offset = length;
4851d68fdc4dSMichael Tuexen 			if ((!abort_no_unlock) && (locked_tcb)) {
4852f8829a4aSRandall Stewart 				SCTP_TCB_UNLOCK(locked_tcb);
48536e55db54SRandall Stewart 			}
4854f8829a4aSRandall Stewart 			return (NULL);
4855f8829a4aSRandall Stewart 			break;
48569a972525SRandall Stewart 		case SCTP_PAD_CHUNK:
48579a972525SRandall Stewart 			break;
4858f8829a4aSRandall Stewart 		case SCTP_INITIATION_ACK:
4859ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n");
4860f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4861f8829a4aSRandall Stewart 				/* We are not interested anymore */
4862f8829a4aSRandall Stewart 				if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4863f8829a4aSRandall Stewart 					;
4864f8829a4aSRandall Stewart 				} else {
4865828318e1SMichael Tuexen 					if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
4866faa1e3f4SRandall Stewart 						/* Very unlikely */
4867f8829a4aSRandall Stewart 						SCTP_TCB_UNLOCK(locked_tcb);
48686e55db54SRandall Stewart 					}
4869f8829a4aSRandall Stewart 					*offset = length;
4870f8829a4aSRandall Stewart 					if (stcb) {
4871ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4872ceaad40aSRandall Stewart 						so = SCTP_INP_SO(inp);
4873ceaad40aSRandall Stewart 						atomic_add_int(&stcb->asoc.refcnt, 1);
4874ceaad40aSRandall Stewart 						SCTP_TCB_UNLOCK(stcb);
4875ceaad40aSRandall Stewart 						SCTP_SOCKET_LOCK(so, 1);
4876ceaad40aSRandall Stewart 						SCTP_TCB_LOCK(stcb);
4877ceaad40aSRandall Stewart 						atomic_subtract_int(&stcb->asoc.refcnt, 1);
4878ceaad40aSRandall Stewart #endif
4879b7d130beSMichael Tuexen 						(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4880b7d130beSMichael Tuexen 						    SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
4881ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4882ceaad40aSRandall Stewart 						SCTP_SOCKET_UNLOCK(so, 1);
4883ceaad40aSRandall Stewart #endif
4884f8829a4aSRandall Stewart 					}
4885f8829a4aSRandall Stewart 					return (NULL);
4886f8829a4aSRandall Stewart 				}
4887f8829a4aSRandall Stewart 			}
4888ab6174d5SMichael Tuexen 			/* The INIT-ACK chunk must be the only chunk. */
4889f8829a4aSRandall Stewart 			if ((num_chunks > 1) ||
4890ab6174d5SMichael Tuexen 			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4891f8829a4aSRandall Stewart 				*offset = length;
48926e55db54SRandall Stewart 				if (locked_tcb) {
4893f8829a4aSRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
48946e55db54SRandall Stewart 				}
4895f8829a4aSRandall Stewart 				return (NULL);
4896f8829a4aSRandall Stewart 			}
4897ad81507eSRandall Stewart 			if ((netp) && (*netp)) {
4898b1754ad1SMichael Tuexen 				ret = sctp_handle_init_ack(m, iphlen, *offset,
4899b1754ad1SMichael Tuexen 				    src, dst, sh,
4900f30ac432SMichael Tuexen 				    (struct sctp_init_ack_chunk *)ch,
4901f30ac432SMichael Tuexen 				    stcb, *netp,
4902f30ac432SMichael Tuexen 				    &abort_no_unlock,
4903457b4b88SMichael Tuexen 				    mflowtype, mflowid,
4904f30ac432SMichael Tuexen 				    vrf_id);
4905ad81507eSRandall Stewart 			} else {
4906ad81507eSRandall Stewart 				ret = -1;
4907ad81507eSRandall Stewart 			}
4908d68fdc4dSMichael Tuexen 			*offset = length;
4909d68fdc4dSMichael Tuexen 			if (abort_no_unlock) {
4910d68fdc4dSMichael Tuexen 				return (NULL);
4911d68fdc4dSMichael Tuexen 			}
4912f8829a4aSRandall Stewart 			/*
4913f8829a4aSRandall Stewart 			 * Special case, I must call the output routine to
4914f8829a4aSRandall Stewart 			 * get the cookie echoed
4915f8829a4aSRandall Stewart 			 */
4916d68fdc4dSMichael Tuexen 			if ((stcb != NULL) && (ret == 0)) {
4917ceaad40aSRandall Stewart 				sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4918d68fdc4dSMichael Tuexen 			}
49196e55db54SRandall Stewart 			if (locked_tcb) {
4920f8829a4aSRandall Stewart 				SCTP_TCB_UNLOCK(locked_tcb);
49216e55db54SRandall Stewart 			}
4922f8829a4aSRandall Stewart 			return (NULL);
4923f8829a4aSRandall Stewart 			break;
4924f8829a4aSRandall Stewart 		case SCTP_SELECTIVE_ACK:
4925f8829a4aSRandall Stewart 			{
4926f8829a4aSRandall Stewart 				struct sctp_sack_chunk *sack;
4927f8829a4aSRandall Stewart 				int abort_now = 0;
4928f8829a4aSRandall Stewart 				uint32_t a_rwnd, cum_ack;
4929cd554309SMichael Tuexen 				uint16_t num_seg, num_dup;
4930cd554309SMichael Tuexen 				uint8_t flags;
4931cd554309SMichael Tuexen 				int offset_seg, offset_dup;
4932f8829a4aSRandall Stewart 
493320083c2eSMichael Tuexen 				SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n");
493420083c2eSMichael Tuexen 				SCTP_STAT_INCR(sctps_recvsacks);
4935cd554309SMichael Tuexen 				if (stcb == NULL) {
4936cd554309SMichael Tuexen 					SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing SACK chunk\n");
4937cd554309SMichael Tuexen 					break;
49386e55db54SRandall Stewart 				}
4939cd554309SMichael Tuexen 				if (chk_length < sizeof(struct sctp_sack_chunk)) {
4940cd554309SMichael Tuexen 					SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4941cd554309SMichael Tuexen 					break;
4942d06c82f1SRandall Stewart 				}
49432afb3e84SRandall Stewart 				if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
49442afb3e84SRandall Stewart 					/*-
49452afb3e84SRandall Stewart 					 * If we have sent a shutdown-ack, we will pay no
49462afb3e84SRandall Stewart 					 * attention to a sack sent in to us since
49472afb3e84SRandall Stewart 					 * we don't care anymore.
49482afb3e84SRandall Stewart 					 */
4949a1e13272SRandall Stewart 					break;
49502afb3e84SRandall Stewart 				}
4951f8829a4aSRandall Stewart 				sack = (struct sctp_sack_chunk *)ch;
4952cd554309SMichael Tuexen 				flags = ch->chunk_flags;
4953f8829a4aSRandall Stewart 				cum_ack = ntohl(sack->sack.cum_tsn_ack);
4954f8829a4aSRandall Stewart 				num_seg = ntohs(sack->sack.num_gap_ack_blks);
4955cd554309SMichael Tuexen 				num_dup = ntohs(sack->sack.num_dup_tsns);
4956f8829a4aSRandall Stewart 				a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd);
4957cd554309SMichael Tuexen 				if (sizeof(struct sctp_sack_chunk) +
4958cd554309SMichael Tuexen 				    num_seg * sizeof(struct sctp_gap_ack_block) +
4959cd554309SMichael Tuexen 				    num_dup * sizeof(uint32_t) != chk_length) {
4960cd554309SMichael Tuexen 					SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4961cd554309SMichael Tuexen 					break;
4962cd554309SMichael Tuexen 				}
4963cd554309SMichael Tuexen 				offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4964cd554309SMichael Tuexen 				offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
496535918f85SRandall Stewart 				SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4966cd554309SMichael Tuexen 				    cum_ack, num_seg, a_rwnd);
4967f8829a4aSRandall Stewart 				stcb->asoc.seen_a_sack_this_pkt = 1;
4968f8829a4aSRandall Stewart 				if ((stcb->asoc.pr_sctp_cnt == 0) &&
4969f8829a4aSRandall Stewart 				    (num_seg == 0) &&
497020b07a4dSMichael Tuexen 				    SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
4971f8829a4aSRandall Stewart 				    (stcb->asoc.saw_sack_with_frags == 0) &&
4972d9c5cfeaSMichael Tuexen 				    (stcb->asoc.saw_sack_with_nr_frags == 0) &&
4973f8829a4aSRandall Stewart 				    (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
4974f8829a4aSRandall Stewart 				    ) {
4975f8829a4aSRandall Stewart 					/*
4976f8829a4aSRandall Stewart 					 * We have a SIMPLE sack having no
4977f8829a4aSRandall Stewart 					 * prior segments and data on sent
4978f8829a4aSRandall Stewart 					 * queue to be acked.. Use the
4979f8829a4aSRandall Stewart 					 * faster path sack processing. We
4980f8829a4aSRandall Stewart 					 * also allow window update sacks
4981f8829a4aSRandall Stewart 					 * with no missing segments to go
4982f8829a4aSRandall Stewart 					 * this way too.
4983f8829a4aSRandall Stewart 					 */
4984899288aeSRandall Stewart 					sctp_express_handle_sack(stcb, cum_ack, a_rwnd, &abort_now, ecne_seen);
4985f8829a4aSRandall Stewart 				} else {
4986ad81507eSRandall Stewart 					if (netp && *netp)
49877215cc1bSMichael Tuexen 						sctp_handle_sack(m, offset_seg, offset_dup, stcb,
4988cd554309SMichael Tuexen 						    num_seg, 0, num_dup, &abort_now, flags,
4989899288aeSRandall Stewart 						    cum_ack, a_rwnd, ecne_seen);
4990830d754dSRandall Stewart 				}
4991f8829a4aSRandall Stewart 				if (abort_now) {
4992f8829a4aSRandall Stewart 					/* ABORT signal from sack processing */
4993f8829a4aSRandall Stewart 					*offset = length;
4994f8829a4aSRandall Stewart 					return (NULL);
4995f8829a4aSRandall Stewart 				}
4996cd554309SMichael Tuexen 				if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4997cd554309SMichael Tuexen 				    TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4998cd554309SMichael Tuexen 				    (stcb->asoc.stream_queue_cnt == 0)) {
4999cd554309SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
5000cd554309SMichael Tuexen 				}
5001f8829a4aSRandall Stewart 			}
5002f8829a4aSRandall Stewart 			break;
5003830d754dSRandall Stewart 			/*
5004830d754dSRandall Stewart 			 * EY - nr_sack:  If the received chunk is an
5005830d754dSRandall Stewart 			 * nr_sack chunk
5006830d754dSRandall Stewart 			 */
5007830d754dSRandall Stewart 		case SCTP_NR_SELECTIVE_ACK:
5008830d754dSRandall Stewart 			{
5009830d754dSRandall Stewart 				struct sctp_nr_sack_chunk *nr_sack;
5010830d754dSRandall Stewart 				int abort_now = 0;
5011830d754dSRandall Stewart 				uint32_t a_rwnd, cum_ack;
5012cd554309SMichael Tuexen 				uint16_t num_seg, num_nr_seg, num_dup;
5013cd554309SMichael Tuexen 				uint8_t flags;
5014cd554309SMichael Tuexen 				int offset_seg, offset_dup;
5015830d754dSRandall Stewart 
501620083c2eSMichael Tuexen 				SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n");
501720083c2eSMichael Tuexen 				SCTP_STAT_INCR(sctps_recvsacks);
5018cd554309SMichael Tuexen 				if (stcb == NULL) {
5019cd554309SMichael Tuexen 					SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing NR-SACK chunk\n");
5020cd554309SMichael Tuexen 					break;
5021cd554309SMichael Tuexen 				}
5022caea9879SMichael Tuexen 				if (stcb->asoc.nrsack_supported == 0) {
502352129fcdSRandall Stewart 					goto unknown_chunk;
502452129fcdSRandall Stewart 				}
5025cd554309SMichael Tuexen 				if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
5026cd554309SMichael Tuexen 					SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR-SACK chunk, too small\n");
5027cd554309SMichael Tuexen 					break;
5028cd554309SMichael Tuexen 				}
5029830d754dSRandall Stewart 				if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
5030830d754dSRandall Stewart 					/*-
5031830d754dSRandall Stewart 					 * If we have sent a shutdown-ack, we will pay no
5032830d754dSRandall Stewart 					 * attention to a sack sent in to us since
5033830d754dSRandall Stewart 					 * we don't care anymore.
5034830d754dSRandall Stewart 					 */
5035cd554309SMichael Tuexen 					break;
5036830d754dSRandall Stewart 				}
5037830d754dSRandall Stewart 				nr_sack = (struct sctp_nr_sack_chunk *)ch;
5038cd554309SMichael Tuexen 				flags = ch->chunk_flags;
5039830d754dSRandall Stewart 				cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
5040830d754dSRandall Stewart 				num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
5041830d754dSRandall Stewart 				num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
5042cd554309SMichael Tuexen 				num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
5043830d754dSRandall Stewart 				a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd);
5044cd554309SMichael Tuexen 				if (sizeof(struct sctp_nr_sack_chunk) +
5045cd554309SMichael Tuexen 				    (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
5046cd554309SMichael Tuexen 				    num_dup * sizeof(uint32_t) != chk_length) {
5047cd554309SMichael Tuexen 					SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
5048cd554309SMichael Tuexen 					break;
5049cd554309SMichael Tuexen 				}
5050cd554309SMichael Tuexen 				offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
5051cd554309SMichael Tuexen 				offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
5052830d754dSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
5053cd554309SMichael Tuexen 				    cum_ack, num_seg, a_rwnd);
5054830d754dSRandall Stewart 				stcb->asoc.seen_a_sack_this_pkt = 1;
5055830d754dSRandall Stewart 				if ((stcb->asoc.pr_sctp_cnt == 0) &&
5056cd554309SMichael Tuexen 				    (num_seg == 0) && (num_nr_seg == 0) &&
505720b07a4dSMichael Tuexen 				    SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
5058830d754dSRandall Stewart 				    (stcb->asoc.saw_sack_with_frags == 0) &&
5059d9c5cfeaSMichael Tuexen 				    (stcb->asoc.saw_sack_with_nr_frags == 0) &&
5060cd554309SMichael Tuexen 				    (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
5061830d754dSRandall Stewart 					/*
5062830d754dSRandall Stewart 					 * We have a SIMPLE sack having no
5063830d754dSRandall Stewart 					 * prior segments and data on sent
5064cd554309SMichael Tuexen 					 * queue to be acked. Use the faster
5065cd554309SMichael Tuexen 					 * path sack processing. We also
5066cd554309SMichael Tuexen 					 * allow window update sacks with no
5067cd554309SMichael Tuexen 					 * missing segments to go this way
5068cd554309SMichael Tuexen 					 * too.
5069830d754dSRandall Stewart 					 */
5070493d8e5aSRandall Stewart 					sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
5071899288aeSRandall Stewart 					    &abort_now, ecne_seen);
5072830d754dSRandall Stewart 				} else {
5073830d754dSRandall Stewart 					if (netp && *netp)
50747215cc1bSMichael Tuexen 						sctp_handle_sack(m, offset_seg, offset_dup, stcb,
5075cd554309SMichael Tuexen 						    num_seg, num_nr_seg, num_dup, &abort_now, flags,
5076899288aeSRandall Stewart 						    cum_ack, a_rwnd, ecne_seen);
5077830d754dSRandall Stewart 				}
5078830d754dSRandall Stewart 				if (abort_now) {
5079830d754dSRandall Stewart 					/* ABORT signal from sack processing */
5080830d754dSRandall Stewart 					*offset = length;
5081830d754dSRandall Stewart 					return (NULL);
5082830d754dSRandall Stewart 				}
5083cd554309SMichael Tuexen 				if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5084cd554309SMichael Tuexen 				    TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5085cd554309SMichael Tuexen 				    (stcb->asoc.stream_queue_cnt == 0)) {
5086cd554309SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
5087cd554309SMichael Tuexen 				}
5088830d754dSRandall Stewart 			}
5089830d754dSRandall Stewart 			break;
5090830d754dSRandall Stewart 
5091f8829a4aSRandall Stewart 		case SCTP_HEARTBEAT_REQUEST:
5092ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
5093ad81507eSRandall Stewart 			if ((stcb) && netp && *netp) {
5094f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_recvheartbeat);
5095ad81507eSRandall Stewart 				sctp_send_heartbeat_ack(stcb, m, *offset,
5096ad81507eSRandall Stewart 				    chk_length, *netp);
5097f8829a4aSRandall Stewart 
5098f8829a4aSRandall Stewart 				/* He's alive so give him credit */
5099b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5100c4739e2fSRandall Stewart 					sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5101c4739e2fSRandall Stewart 					    stcb->asoc.overall_error_count,
5102c4739e2fSRandall Stewart 					    0,
5103c4739e2fSRandall Stewart 					    SCTP_FROM_SCTP_INPUT,
5104c4739e2fSRandall Stewart 					    __LINE__);
5105c4739e2fSRandall Stewart 				}
5106f8829a4aSRandall Stewart 				stcb->asoc.overall_error_count = 0;
5107ad81507eSRandall Stewart 			}
5108f8829a4aSRandall Stewart 			break;
5109f8829a4aSRandall Stewart 		case SCTP_HEARTBEAT_ACK:
5110ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n");
5111ad81507eSRandall Stewart 			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
5112d06c82f1SRandall Stewart 				/* Its not ours */
511317205eccSRandall Stewart 				*offset = length;
51146e55db54SRandall Stewart 				if (locked_tcb) {
51156114cd96SRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
51166e55db54SRandall Stewart 				}
5117d06c82f1SRandall Stewart 				return (NULL);
5118d06c82f1SRandall Stewart 			}
5119f8829a4aSRandall Stewart 			/* He's alive so give him credit */
5120b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5121c4739e2fSRandall Stewart 				sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5122c4739e2fSRandall Stewart 				    stcb->asoc.overall_error_count,
5123c4739e2fSRandall Stewart 				    0,
5124c4739e2fSRandall Stewart 				    SCTP_FROM_SCTP_INPUT,
5125c4739e2fSRandall Stewart 				    __LINE__);
5126c4739e2fSRandall Stewart 			}
5127f8829a4aSRandall Stewart 			stcb->asoc.overall_error_count = 0;
5128f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvheartbeatack);
5129ad81507eSRandall Stewart 			if (netp && *netp)
5130f8829a4aSRandall Stewart 				sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
5131f8829a4aSRandall Stewart 				    stcb, *netp);
5132f8829a4aSRandall Stewart 			break;
5133f8829a4aSRandall Stewart 		case SCTP_ABORT_ASSOCIATION:
5134207304d4SRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
5135dd294dceSMichael Tuexen 			    (void *)stcb);
5136ad81507eSRandall Stewart 			if ((stcb) && netp && *netp)
5137f8829a4aSRandall Stewart 				sctp_handle_abort((struct sctp_abort_chunk *)ch,
5138f8829a4aSRandall Stewart 				    stcb, *netp);
5139f8829a4aSRandall Stewart 			*offset = length;
5140f8829a4aSRandall Stewart 			return (NULL);
5141f8829a4aSRandall Stewart 			break;
5142f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN:
5143207304d4SRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
5144dd294dceSMichael Tuexen 			    (void *)stcb);
5145ad81507eSRandall Stewart 			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
5146d06c82f1SRandall Stewart 				*offset = length;
51476e55db54SRandall Stewart 				if (locked_tcb) {
51486114cd96SRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
51496e55db54SRandall Stewart 				}
5150d06c82f1SRandall Stewart 				return (NULL);
5151ad81507eSRandall Stewart 			}
5152ad81507eSRandall Stewart 			if (netp && *netp) {
5153f8829a4aSRandall Stewart 				int abort_flag = 0;
5154f8829a4aSRandall Stewart 
5155f8829a4aSRandall Stewart 				sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
5156f8829a4aSRandall Stewart 				    stcb, *netp, &abort_flag);
5157f8829a4aSRandall Stewart 				if (abort_flag) {
5158f8829a4aSRandall Stewart 					*offset = length;
5159f8829a4aSRandall Stewart 					return (NULL);
5160f8829a4aSRandall Stewart 				}
5161f8829a4aSRandall Stewart 			}
5162f8829a4aSRandall Stewart 			break;
5163f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_ACK:
5164dd294dceSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", (void *)stcb);
5165ad81507eSRandall Stewart 			if ((stcb) && (netp) && (*netp))
5166f8829a4aSRandall Stewart 				sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
5167f8829a4aSRandall Stewart 			*offset = length;
5168f8829a4aSRandall Stewart 			return (NULL);
5169f8829a4aSRandall Stewart 			break;
5170ad81507eSRandall Stewart 
5171f8829a4aSRandall Stewart 		case SCTP_OPERATION_ERROR:
5172ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n");
5173ad81507eSRandall Stewart 			if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) {
5174f8829a4aSRandall Stewart 				*offset = length;
5175f8829a4aSRandall Stewart 				return (NULL);
5176f8829a4aSRandall Stewart 			}
5177f8829a4aSRandall Stewart 			break;
5178f8829a4aSRandall Stewart 		case SCTP_COOKIE_ECHO:
5179ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3,
5180dd294dceSMichael Tuexen 			    "SCTP_COOKIE-ECHO, stcb %p\n", (void *)stcb);
5181f8829a4aSRandall Stewart 			if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5182f8829a4aSRandall Stewart 				;
5183f8829a4aSRandall Stewart 			} else {
5184ad81507eSRandall Stewart 				if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5185f8829a4aSRandall Stewart 					/* We are not interested anymore */
51860c7dc840SRandall Stewart 			abend:
518728085b2eSRandall Stewart 					if (stcb) {
518828085b2eSRandall Stewart 						SCTP_TCB_UNLOCK(stcb);
518928085b2eSRandall Stewart 					}
5190f8829a4aSRandall Stewart 					*offset = length;
5191f8829a4aSRandall Stewart 					return (NULL);
5192f8829a4aSRandall Stewart 				}
5193f8829a4aSRandall Stewart 			}
5194f8829a4aSRandall Stewart 			/*
5195f8829a4aSRandall Stewart 			 * First are we accepting? We do this again here
519688a7eb29SRandall Stewart 			 * since it is possible that a previous endpoint WAS
519788a7eb29SRandall Stewart 			 * listening responded to a INIT-ACK and then
5198f8829a4aSRandall Stewart 			 * closed. We opened and bound.. and are now no
5199f8829a4aSRandall Stewart 			 * longer listening.
5200f8829a4aSRandall Stewart 			 */
5201b201f536SRandall Stewart 
5202b201f536SRandall Stewart 			if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) {
5203b201f536SRandall Stewart 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5204b3f1ea41SRandall Stewart 				    (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
5205ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
5206b1754ad1SMichael Tuexen 					sctp_abort_association(inp, stcb, m, iphlen,
5207b1754ad1SMichael Tuexen 					    src, dst, sh, op_err,
5208457b4b88SMichael Tuexen 					    mflowtype, mflowid,
5209f30ac432SMichael Tuexen 					    vrf_id, port);
5210f8829a4aSRandall Stewart 				}
5211f8829a4aSRandall Stewart 				*offset = length;
5212f8829a4aSRandall Stewart 				return (NULL);
5213b201f536SRandall Stewart 			} else {
5214f8829a4aSRandall Stewart 				struct mbuf *ret_buf;
5215a5d547adSRandall Stewart 				struct sctp_inpcb *linp;
5216f8829a4aSRandall Stewart 
5217ad81507eSRandall Stewart 				if (stcb) {
5218a5d547adSRandall Stewart 					linp = NULL;
5219ad81507eSRandall Stewart 				} else {
5220a5d547adSRandall Stewart 					linp = inp;
5221ad81507eSRandall Stewart 				}
5222a5d547adSRandall Stewart 
5223ad81507eSRandall Stewart 				if (linp) {
5224a5d547adSRandall Stewart 					SCTP_ASOC_CREATE_LOCK(linp);
52250c7dc840SRandall Stewart 					if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
52260c7dc840SRandall Stewart 					    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
52270c7dc840SRandall Stewart 						SCTP_ASOC_CREATE_UNLOCK(linp);
52280c7dc840SRandall Stewart 						goto abend;
52290c7dc840SRandall Stewart 					}
5230ad81507eSRandall Stewart 				}
5231ad81507eSRandall Stewart 				if (netp) {
5232f8829a4aSRandall Stewart 					ret_buf =
5233f8829a4aSRandall Stewart 					    sctp_handle_cookie_echo(m, iphlen,
5234b1754ad1SMichael Tuexen 					    *offset,
5235b1754ad1SMichael Tuexen 					    src, dst,
5236b1754ad1SMichael Tuexen 					    sh,
5237f8829a4aSRandall Stewart 					    (struct sctp_cookie_echo_chunk *)ch,
5238f8829a4aSRandall Stewart 					    &inp, &stcb, netp,
5239f8829a4aSRandall Stewart 					    auth_skipped,
5240f8829a4aSRandall Stewart 					    auth_offset,
5241a5d547adSRandall Stewart 					    auth_len,
524217205eccSRandall Stewart 					    &locked_tcb,
5243457b4b88SMichael Tuexen 					    mflowtype,
5244f30ac432SMichael Tuexen 					    mflowid,
5245c54a18d2SRandall Stewart 					    vrf_id,
5246c54a18d2SRandall Stewart 					    port);
5247ad81507eSRandall Stewart 				} else {
5248ad81507eSRandall Stewart 					ret_buf = NULL;
5249ad81507eSRandall Stewart 				}
5250ad81507eSRandall Stewart 				if (linp) {
5251a5d547adSRandall Stewart 					SCTP_ASOC_CREATE_UNLOCK(linp);
5252ad81507eSRandall Stewart 				}
5253f8829a4aSRandall Stewart 				if (ret_buf == NULL) {
5254f8829a4aSRandall Stewart 					if (locked_tcb) {
5255f8829a4aSRandall Stewart 						SCTP_TCB_UNLOCK(locked_tcb);
5256f8829a4aSRandall Stewart 					}
5257ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INPUT3,
5258ad81507eSRandall Stewart 					    "GAK, null buffer\n");
5259f8829a4aSRandall Stewart 					*offset = length;
5260f8829a4aSRandall Stewart 					return (NULL);
5261f8829a4aSRandall Stewart 				}
5262f8829a4aSRandall Stewart 				/* if AUTH skipped, see if it verified... */
5263f8829a4aSRandall Stewart 				if (auth_skipped) {
5264f8829a4aSRandall Stewart 					got_auth = 1;
5265f8829a4aSRandall Stewart 					auth_skipped = 0;
5266f8829a4aSRandall Stewart 				}
5267f8829a4aSRandall Stewart 				if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
5268f8829a4aSRandall Stewart 					/*
5269f8829a4aSRandall Stewart 					 * Restart the timer if we have
5270f8829a4aSRandall Stewart 					 * pending data
5271f8829a4aSRandall Stewart 					 */
5272f8829a4aSRandall Stewart 					struct sctp_tmit_chunk *chk;
5273f8829a4aSRandall Stewart 
5274f8829a4aSRandall Stewart 					chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
52754a9ef3f8SMichael Tuexen 					sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
5276f8829a4aSRandall Stewart 				}
5277f8829a4aSRandall Stewart 			}
5278f8829a4aSRandall Stewart 			break;
5279f8829a4aSRandall Stewart 		case SCTP_COOKIE_ACK:
5280dd294dceSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", (void *)stcb);
5281ad81507eSRandall Stewart 			if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
52826e55db54SRandall Stewart 				if (locked_tcb) {
528317205eccSRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
52846e55db54SRandall Stewart 				}
528517205eccSRandall Stewart 				return (NULL);
528617205eccSRandall Stewart 			}
5287f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5288f8829a4aSRandall Stewart 				/* We are not interested anymore */
5289f8829a4aSRandall Stewart 				if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5290f8829a4aSRandall Stewart 					;
5291ad81507eSRandall Stewart 				} else if (stcb) {
5292ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5293ceaad40aSRandall Stewart 					so = SCTP_INP_SO(inp);
5294ceaad40aSRandall Stewart 					atomic_add_int(&stcb->asoc.refcnt, 1);
5295ceaad40aSRandall Stewart 					SCTP_TCB_UNLOCK(stcb);
5296ceaad40aSRandall Stewart 					SCTP_SOCKET_LOCK(so, 1);
5297ceaad40aSRandall Stewart 					SCTP_TCB_LOCK(stcb);
5298ceaad40aSRandall Stewart 					atomic_subtract_int(&stcb->asoc.refcnt, 1);
5299ceaad40aSRandall Stewart #endif
5300b7d130beSMichael Tuexen 					(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5301b7d130beSMichael Tuexen 					    SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
5302ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5303ceaad40aSRandall Stewart 					SCTP_SOCKET_UNLOCK(so, 1);
5304ceaad40aSRandall Stewart #endif
5305f8829a4aSRandall Stewart 					*offset = length;
5306f8829a4aSRandall Stewart 					return (NULL);
5307f8829a4aSRandall Stewart 				}
5308f8829a4aSRandall Stewart 			}
5309f8829a4aSRandall Stewart 			/* He's alive so give him credit */
5310ad81507eSRandall Stewart 			if ((stcb) && netp && *netp) {
5311b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5312c4739e2fSRandall Stewart 					sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5313c4739e2fSRandall Stewart 					    stcb->asoc.overall_error_count,
5314c4739e2fSRandall Stewart 					    0,
5315c4739e2fSRandall Stewart 					    SCTP_FROM_SCTP_INPUT,
5316c4739e2fSRandall Stewart 					    __LINE__);
5317c4739e2fSRandall Stewart 				}
5318f8829a4aSRandall Stewart 				stcb->asoc.overall_error_count = 0;
5319f8829a4aSRandall Stewart 				sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
53206e55db54SRandall Stewart 			}
5321f8829a4aSRandall Stewart 			break;
5322f8829a4aSRandall Stewart 		case SCTP_ECN_ECHO:
5323ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n");
5324f8829a4aSRandall Stewart 			/* He's alive so give him credit */
5325ad81507eSRandall Stewart 			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5326d06c82f1SRandall Stewart 				/* Its not ours */
53276e55db54SRandall Stewart 				if (locked_tcb) {
53286114cd96SRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
53296e55db54SRandall Stewart 				}
5330d06c82f1SRandall Stewart 				*offset = length;
5331d06c82f1SRandall Stewart 				return (NULL);
5332d06c82f1SRandall Stewart 			}
53336e55db54SRandall Stewart 			if (stcb) {
5334c79bec9cSMichael Tuexen 				if (stcb->asoc.ecn_supported == 0) {
5335c79bec9cSMichael Tuexen 					goto unknown_chunk;
5336c79bec9cSMichael Tuexen 				}
5337b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5338c4739e2fSRandall Stewart 					sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5339c4739e2fSRandall Stewart 					    stcb->asoc.overall_error_count,
5340c4739e2fSRandall Stewart 					    0,
5341c4739e2fSRandall Stewart 					    SCTP_FROM_SCTP_INPUT,
5342c4739e2fSRandall Stewart 					    __LINE__);
5343c4739e2fSRandall Stewart 				}
5344f8829a4aSRandall Stewart 				stcb->asoc.overall_error_count = 0;
5345f8829a4aSRandall Stewart 				sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
5346f8829a4aSRandall Stewart 				    stcb);
5347899288aeSRandall Stewart 				ecne_seen = 1;
53486e55db54SRandall Stewart 			}
5349f8829a4aSRandall Stewart 			break;
5350f8829a4aSRandall Stewart 		case SCTP_ECN_CWR:
5351ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n");
5352f8829a4aSRandall Stewart 			/* He's alive so give him credit */
5353ad81507eSRandall Stewart 			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5354d06c82f1SRandall Stewart 				/* Its not ours */
53556e55db54SRandall Stewart 				if (locked_tcb) {
53566114cd96SRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
53576e55db54SRandall Stewart 				}
5358d06c82f1SRandall Stewart 				*offset = length;
5359d06c82f1SRandall Stewart 				return (NULL);
5360d06c82f1SRandall Stewart 			}
53616e55db54SRandall Stewart 			if (stcb) {
5362c79bec9cSMichael Tuexen 				if (stcb->asoc.ecn_supported == 0) {
5363c79bec9cSMichael Tuexen 					goto unknown_chunk;
5364c79bec9cSMichael Tuexen 				}
5365b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5366c4739e2fSRandall Stewart 					sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5367c4739e2fSRandall Stewart 					    stcb->asoc.overall_error_count,
5368c4739e2fSRandall Stewart 					    0,
5369c4739e2fSRandall Stewart 					    SCTP_FROM_SCTP_INPUT,
5370c4739e2fSRandall Stewart 					    __LINE__);
5371c4739e2fSRandall Stewart 				}
5372f8829a4aSRandall Stewart 				stcb->asoc.overall_error_count = 0;
5373a21779f0SRandall Stewart 				sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
53746e55db54SRandall Stewart 			}
5375f8829a4aSRandall Stewart 			break;
5376f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_COMPLETE:
5377dd294dceSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", (void *)stcb);
5378f8829a4aSRandall Stewart 			/* must be first and only chunk */
5379f8829a4aSRandall Stewart 			if ((num_chunks > 1) ||
53804c9179adSRandall Stewart 			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5381f8829a4aSRandall Stewart 				*offset = length;
53826e55db54SRandall Stewart 				if (locked_tcb) {
5383f8829a4aSRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
53846e55db54SRandall Stewart 				}
5385f8829a4aSRandall Stewart 				return (NULL);
5386f8829a4aSRandall Stewart 			}
5387ad81507eSRandall Stewart 			if ((stcb) && netp && *netp) {
5388f8829a4aSRandall Stewart 				sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5389f8829a4aSRandall Stewart 				    stcb, *netp);
53906e55db54SRandall Stewart 			}
5391f8829a4aSRandall Stewart 			*offset = length;
5392f8829a4aSRandall Stewart 			return (NULL);
5393f8829a4aSRandall Stewart 			break;
5394f8829a4aSRandall Stewart 		case SCTP_ASCONF:
5395ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5396f8829a4aSRandall Stewart 			/* He's alive so give him credit */
53976e55db54SRandall Stewart 			if (stcb) {
5398c79bec9cSMichael Tuexen 				if (stcb->asoc.asconf_supported == 0) {
5399c79bec9cSMichael Tuexen 					goto unknown_chunk;
5400c79bec9cSMichael Tuexen 				}
5401b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5402c4739e2fSRandall Stewart 					sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5403c4739e2fSRandall Stewart 					    stcb->asoc.overall_error_count,
5404c4739e2fSRandall Stewart 					    0,
5405c4739e2fSRandall Stewart 					    SCTP_FROM_SCTP_INPUT,
5406c4739e2fSRandall Stewart 					    __LINE__);
5407c4739e2fSRandall Stewart 				}
5408f8829a4aSRandall Stewart 				stcb->asoc.overall_error_count = 0;
5409b1754ad1SMichael Tuexen 				sctp_handle_asconf(m, *offset, src,
54102afb3e84SRandall Stewart 				    (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
54112afb3e84SRandall Stewart 				asconf_cnt++;
54126e55db54SRandall Stewart 			}
5413f8829a4aSRandall Stewart 			break;
5414f8829a4aSRandall Stewart 		case SCTP_ASCONF_ACK:
5415ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n");
5416d06c82f1SRandall Stewart 			if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5417d06c82f1SRandall Stewart 				/* Its not ours */
54186e55db54SRandall Stewart 				if (locked_tcb) {
54196114cd96SRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
54206e55db54SRandall Stewart 				}
5421d06c82f1SRandall Stewart 				*offset = length;
5422d06c82f1SRandall Stewart 				return (NULL);
5423d06c82f1SRandall Stewart 			}
5424ad81507eSRandall Stewart 			if ((stcb) && netp && *netp) {
5425c79bec9cSMichael Tuexen 				if (stcb->asoc.asconf_supported == 0) {
5426c79bec9cSMichael Tuexen 					goto unknown_chunk;
5427c79bec9cSMichael Tuexen 				}
5428f8829a4aSRandall Stewart 				/* He's alive so give him credit */
5429b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5430c4739e2fSRandall Stewart 					sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5431c4739e2fSRandall Stewart 					    stcb->asoc.overall_error_count,
5432c4739e2fSRandall Stewart 					    0,
5433c4739e2fSRandall Stewart 					    SCTP_FROM_SCTP_INPUT,
5434c4739e2fSRandall Stewart 					    __LINE__);
5435c4739e2fSRandall Stewart 				}
5436f8829a4aSRandall Stewart 				stcb->asoc.overall_error_count = 0;
5437f8829a4aSRandall Stewart 				sctp_handle_asconf_ack(m, *offset,
54383232788eSRandall Stewart 				    (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
54393232788eSRandall Stewart 				if (abort_no_unlock)
54403232788eSRandall Stewart 					return (NULL);
54416e55db54SRandall Stewart 			}
5442f8829a4aSRandall Stewart 			break;
5443f8829a4aSRandall Stewart 		case SCTP_FORWARD_CUM_TSN:
5444ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n");
5445d06c82f1SRandall Stewart 			if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5446d06c82f1SRandall Stewart 				/* Its not ours */
54476e55db54SRandall Stewart 				if (locked_tcb) {
54486114cd96SRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
54496e55db54SRandall Stewart 				}
5450d06c82f1SRandall Stewart 				*offset = length;
5451d06c82f1SRandall Stewart 				return (NULL);
5452d06c82f1SRandall Stewart 			}
5453f8829a4aSRandall Stewart 			/* He's alive so give him credit */
54546e55db54SRandall Stewart 			if (stcb) {
5455f8829a4aSRandall Stewart 				int abort_flag = 0;
5456f8829a4aSRandall Stewart 
5457c79bec9cSMichael Tuexen 				if (stcb->asoc.prsctp_supported == 0) {
5458c79bec9cSMichael Tuexen 					goto unknown_chunk;
5459c79bec9cSMichael Tuexen 				}
5460f8829a4aSRandall Stewart 				stcb->asoc.overall_error_count = 0;
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 				*fwd_tsn_seen = 1;
5469f8829a4aSRandall Stewart 				if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5470f8829a4aSRandall Stewart 					/* We are not interested anymore */
5471ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5472ceaad40aSRandall Stewart 					so = SCTP_INP_SO(inp);
5473ceaad40aSRandall Stewart 					atomic_add_int(&stcb->asoc.refcnt, 1);
5474ceaad40aSRandall Stewart 					SCTP_TCB_UNLOCK(stcb);
5475ceaad40aSRandall Stewart 					SCTP_SOCKET_LOCK(so, 1);
5476ceaad40aSRandall Stewart 					SCTP_TCB_LOCK(stcb);
5477ceaad40aSRandall Stewart 					atomic_subtract_int(&stcb->asoc.refcnt, 1);
5478ceaad40aSRandall Stewart #endif
5479b7d130beSMichael Tuexen 					(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5480b7d130beSMichael Tuexen 					    SCTP_FROM_SCTP_INPUT + SCTP_LOC_31);
5481ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5482ceaad40aSRandall Stewart 					SCTP_SOCKET_UNLOCK(so, 1);
5483ceaad40aSRandall Stewart #endif
5484f8829a4aSRandall Stewart 					*offset = length;
5485f8829a4aSRandall Stewart 					return (NULL);
5486f8829a4aSRandall Stewart 				}
5487f8829a4aSRandall Stewart 				sctp_handle_forward_tsn(stcb,
5488671d309cSRandall Stewart 				    (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5489f8829a4aSRandall Stewart 				if (abort_flag) {
5490f8829a4aSRandall Stewart 					*offset = length;
5491f8829a4aSRandall Stewart 					return (NULL);
5492f8829a4aSRandall Stewart 				} else {
5493b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5494c4739e2fSRandall Stewart 						sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5495c4739e2fSRandall Stewart 						    stcb->asoc.overall_error_count,
5496c4739e2fSRandall Stewart 						    0,
5497c4739e2fSRandall Stewart 						    SCTP_FROM_SCTP_INPUT,
5498c4739e2fSRandall Stewart 						    __LINE__);
5499c4739e2fSRandall Stewart 					}
5500f8829a4aSRandall Stewart 					stcb->asoc.overall_error_count = 0;
5501f8829a4aSRandall Stewart 				}
5502f8829a4aSRandall Stewart 
5503f8829a4aSRandall Stewart 			}
5504f8829a4aSRandall Stewart 			break;
5505f8829a4aSRandall Stewart 		case SCTP_STREAM_RESET:
5506ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5507ad81507eSRandall Stewart 			if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5508d06c82f1SRandall Stewart 				/* Its not ours */
55096e55db54SRandall Stewart 				if (locked_tcb) {
55106114cd96SRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
55116e55db54SRandall Stewart 				}
5512d06c82f1SRandall Stewart 				*offset = length;
5513d06c82f1SRandall Stewart 				return (NULL);
5514d06c82f1SRandall Stewart 			}
5515317e00efSMichael Tuexen 			if (stcb->asoc.reconfig_supported == 0) {
5516c79bec9cSMichael Tuexen 				goto unknown_chunk;
5517f8829a4aSRandall Stewart 			}
5518a169d6ecSMichael Tuexen 			if (sctp_handle_stream_reset(stcb, m, *offset, ch)) {
5519f8829a4aSRandall Stewart 				/* stop processing */
5520f8829a4aSRandall Stewart 				*offset = length;
5521f8829a4aSRandall Stewart 				return (NULL);
5522f8829a4aSRandall Stewart 			}
5523f8829a4aSRandall Stewart 			break;
5524f8829a4aSRandall Stewart 		case SCTP_PACKET_DROPPED:
5525ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5526f8829a4aSRandall Stewart 			/* re-get it all please */
5527d06c82f1SRandall Stewart 			if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5528d06c82f1SRandall Stewart 				/* Its not ours */
55296e55db54SRandall Stewart 				if (locked_tcb) {
55306114cd96SRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
55316e55db54SRandall Stewart 				}
5532d06c82f1SRandall Stewart 				*offset = length;
5533d06c82f1SRandall Stewart 				return (NULL);
5534d06c82f1SRandall Stewart 			}
5535ad81507eSRandall Stewart 			if (ch && (stcb) && netp && (*netp)) {
5536c79bec9cSMichael Tuexen 				if (stcb->asoc.pktdrop_supported == 0) {
5537c79bec9cSMichael Tuexen 					goto unknown_chunk;
5538c79bec9cSMichael Tuexen 				}
5539f8829a4aSRandall Stewart 				sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5540458303daSRandall Stewart 				    stcb, *netp,
5541458303daSRandall Stewart 				    min(chk_length, (sizeof(chunk_buf) - 4)));
5542458303daSRandall Stewart 
55436e55db54SRandall Stewart 			}
5544f8829a4aSRandall Stewart 			break;
5545f8829a4aSRandall Stewart 		case SCTP_AUTHENTICATION:
5546ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5547f8829a4aSRandall Stewart 			if (stcb == NULL) {
5548f8829a4aSRandall Stewart 				/* save the first AUTH for later processing */
5549f8829a4aSRandall Stewart 				if (auth_skipped == 0) {
5550f8829a4aSRandall Stewart 					auth_offset = *offset;
5551f8829a4aSRandall Stewart 					auth_len = chk_length;
5552f8829a4aSRandall Stewart 					auth_skipped = 1;
5553f8829a4aSRandall Stewart 				}
5554f8829a4aSRandall Stewart 				/* skip this chunk (temporarily) */
5555f8829a4aSRandall Stewart 				goto next_chunk;
5556f8829a4aSRandall Stewart 			}
5557c79bec9cSMichael Tuexen 			if (stcb->asoc.auth_supported == 0) {
5558c79bec9cSMichael Tuexen 				goto unknown_chunk;
5559c79bec9cSMichael Tuexen 			}
5560d06c82f1SRandall Stewart 			if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5561ad81507eSRandall Stewart 			    (chk_length > (sizeof(struct sctp_auth_chunk) +
5562ad81507eSRandall Stewart 			    SCTP_AUTH_DIGEST_LEN_MAX))) {
5563d06c82f1SRandall Stewart 				/* Its not ours */
55646e55db54SRandall Stewart 				if (locked_tcb) {
55656114cd96SRandall Stewart 					SCTP_TCB_UNLOCK(locked_tcb);
55666e55db54SRandall Stewart 				}
5567d06c82f1SRandall Stewart 				*offset = length;
5568d06c82f1SRandall Stewart 				return (NULL);
5569d06c82f1SRandall Stewart 			}
5570f8829a4aSRandall Stewart 			if (got_auth == 1) {
5571f8829a4aSRandall Stewart 				/* skip this chunk... it's already auth'd */
5572f8829a4aSRandall Stewart 				goto next_chunk;
5573f8829a4aSRandall Stewart 			}
5574f8829a4aSRandall Stewart 			got_auth = 1;
5575ad81507eSRandall Stewart 			if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5576f8829a4aSRandall Stewart 			    m, *offset)) {
5577f8829a4aSRandall Stewart 				/* auth HMAC failed so dump the packet */
5578f8829a4aSRandall Stewart 				*offset = length;
5579f8829a4aSRandall Stewart 				return (stcb);
5580f8829a4aSRandall Stewart 			} else {
5581f8829a4aSRandall Stewart 				/* remaining chunks are HMAC checked */
5582f8829a4aSRandall Stewart 				stcb->asoc.authenticated = 1;
5583f8829a4aSRandall Stewart 			}
5584f8829a4aSRandall Stewart 			break;
5585f8829a4aSRandall Stewart 
5586f8829a4aSRandall Stewart 		default:
5587f8829a4aSRandall Stewart 	unknown_chunk:
5588f8829a4aSRandall Stewart 			/* it's an unknown chunk! */
5589f8829a4aSRandall Stewart 			if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
559086eda749SMichael Tuexen 				struct sctp_gen_error_cause *cause;
559139cbb549SMichael Tuexen 				int len;
5592f8829a4aSRandall Stewart 
559386eda749SMichael Tuexen 				op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_gen_error_cause),
5594eb1b1807SGleb Smirnoff 				    0, M_NOWAIT, 1, MT_DATA);
559586eda749SMichael Tuexen 				if (op_err != NULL) {
559639cbb549SMichael Tuexen 					len = min(SCTP_SIZE32(chk_length), (uint32_t) (length - *offset));
559786eda749SMichael Tuexen 					cause = mtod(op_err, struct sctp_gen_error_cause *);
559886eda749SMichael Tuexen 					cause->code = htons(SCTP_CAUSE_UNRECOG_CHUNK);
55999a8e3088SMichael Tuexen 					cause->length = htons((uint16_t) (len + sizeof(struct sctp_gen_error_cause)));
560086eda749SMichael Tuexen 					SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
560186eda749SMichael Tuexen 					SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(m, *offset, len, M_NOWAIT);
560286eda749SMichael Tuexen 					if (SCTP_BUF_NEXT(op_err) != NULL) {
5603eadccaccSRandall Stewart #ifdef SCTP_MBUF_LOGGING
5604b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
560530811e70SMichael Tuexen 							sctp_log_mbc(SCTP_BUF_NEXT(op_err), SCTP_MBUF_ICOPY);
5606eadccaccSRandall Stewart 						}
5607eadccaccSRandall Stewart #endif
560886eda749SMichael Tuexen 						sctp_queue_op_err(stcb, op_err);
5609f8829a4aSRandall Stewart 					} else {
561086eda749SMichael Tuexen 						sctp_m_freem(op_err);
5611f8829a4aSRandall Stewart 					}
5612f8829a4aSRandall Stewart 				}
5613f8829a4aSRandall Stewart 			}
5614f8829a4aSRandall Stewart 			if ((ch->chunk_type & 0x80) == 0) {
5615f8829a4aSRandall Stewart 				/* discard this packet */
5616f8829a4aSRandall Stewart 				*offset = length;
5617f8829a4aSRandall Stewart 				return (stcb);
5618f8829a4aSRandall Stewart 			}	/* else skip this bad chunk and continue... */
5619f8829a4aSRandall Stewart 			break;
5620f8829a4aSRandall Stewart 		}		/* switch (ch->chunk_type) */
5621f8829a4aSRandall Stewart 
5622f8829a4aSRandall Stewart 
5623f8829a4aSRandall Stewart next_chunk:
5624f8829a4aSRandall Stewart 		/* get the next chunk */
5625f8829a4aSRandall Stewart 		*offset += SCTP_SIZE32(chk_length);
5626f8829a4aSRandall Stewart 		if (*offset >= length) {
5627f8829a4aSRandall Stewart 			/* no more data left in the mbuf chain */
5628f8829a4aSRandall Stewart 			break;
5629f8829a4aSRandall Stewart 		}
5630f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5631f8829a4aSRandall Stewart 		    sizeof(struct sctp_chunkhdr), chunk_buf);
5632f8829a4aSRandall Stewart 		if (ch == NULL) {
56336e55db54SRandall Stewart 			if (locked_tcb) {
5634f8829a4aSRandall Stewart 				SCTP_TCB_UNLOCK(locked_tcb);
56356e55db54SRandall Stewart 			}
5636f8829a4aSRandall Stewart 			*offset = length;
5637f8829a4aSRandall Stewart 			return (NULL);
5638f8829a4aSRandall Stewart 		}
5639f8829a4aSRandall Stewart 	}			/* while */
56402afb3e84SRandall Stewart 
56412afb3e84SRandall Stewart 	if (asconf_cnt > 0 && stcb != NULL) {
56422afb3e84SRandall Stewart 		sctp_send_asconf_ack(stcb);
56432afb3e84SRandall Stewart 	}
5644f8829a4aSRandall Stewart 	return (stcb);
5645f8829a4aSRandall Stewart }
5646f8829a4aSRandall Stewart 
5647f8829a4aSRandall Stewart 
5648f8829a4aSRandall Stewart /*
5649f8829a4aSRandall Stewart  * common input chunk processing (v4 and v6)
5650f8829a4aSRandall Stewart  */
56516e55db54SRandall Stewart void
5652b1754ad1SMichael Tuexen sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
5653b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
5654b1754ad1SMichael Tuexen     struct sctphdr *sh, struct sctp_chunkhdr *ch,
5655a8775ad9SMichael Tuexen #if !defined(SCTP_WITH_NO_CSUM)
5656a8775ad9SMichael Tuexen     uint8_t compute_crc,
5657a8775ad9SMichael Tuexen #endif
5658a8775ad9SMichael Tuexen     uint8_t ecn_bits,
5659d089f9b9SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
5660f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
5661f8829a4aSRandall Stewart {
5662f8829a4aSRandall Stewart 	uint32_t high_tsn;
5663f8829a4aSRandall Stewart 	int fwd_tsn_seen = 0, data_processed = 0;
5664ff1ffd74SMichael Tuexen 	struct mbuf *m = *mm, *op_err;
5665ff1ffd74SMichael Tuexen 	char msg[SCTP_DIAG_INFO_LEN];
5666f8829a4aSRandall Stewart 	int un_sent;
5667493d8e5aSRandall Stewart 	int cnt_ctrl_ready = 0;
566855b175e7SMichael Tuexen 	struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
5669a8775ad9SMichael Tuexen 	struct sctp_tcb *stcb = NULL;
5670e3d6ef0bSMichael Tuexen 	struct sctp_nets *net = NULL;
5671f8829a4aSRandall Stewart 
5672f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvdatagrams);
5673f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
5674f8829a4aSRandall Stewart 	sctp_audit_log(0xE0, 1);
5675f8829a4aSRandall Stewart 	sctp_auditing(0, inp, stcb, net);
5676f8829a4aSRandall Stewart #endif
5677a8775ad9SMichael Tuexen #if !defined(SCTP_WITH_NO_CSUM)
5678a8775ad9SMichael Tuexen 	if (compute_crc != 0) {
5679a8775ad9SMichael Tuexen 		uint32_t check, calc_check;
5680f8829a4aSRandall Stewart 
5681a8775ad9SMichael Tuexen 		check = sh->checksum;
5682a8775ad9SMichael Tuexen 		sh->checksum = 0;
5683a8775ad9SMichael Tuexen 		calc_check = sctp_calculate_cksum(m, iphlen);
5684a8775ad9SMichael Tuexen 		sh->checksum = check;
5685a8775ad9SMichael Tuexen 		if (calc_check != check) {
5686a8775ad9SMichael Tuexen 			SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x  m:%p mlen:%d iphlen:%d\n",
5687dd294dceSMichael Tuexen 			    calc_check, check, (void *)m, length, iphlen);
5688a8775ad9SMichael Tuexen 			stcb = sctp_findassociation_addr(m, offset, src, dst,
5689a8775ad9SMichael Tuexen 			    sh, ch, &inp, &net, vrf_id);
56904474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
56913cf729a9SMichael Tuexen 			if ((ch->chunk_type != SCTP_INITIATION) &&
56923cf729a9SMichael Tuexen 			    (net != NULL) && (net->port != port)) {
5693a8775ad9SMichael Tuexen 				if (net->port == 0) {
56943cf729a9SMichael Tuexen 					/* UDP encapsulation turned on. */
56953cf729a9SMichael Tuexen 					net->mtu -= sizeof(struct udphdr);
56963cf729a9SMichael Tuexen 					if (stcb->asoc.smallest_mtu > net->mtu) {
56973cf729a9SMichael Tuexen 						sctp_pathmtu_adjustment(stcb, net->mtu);
56983cf729a9SMichael Tuexen 					}
56993cf729a9SMichael Tuexen 				} else if (port == 0) {
57003cf729a9SMichael Tuexen 					/* UDP encapsulation turned off. */
57013cf729a9SMichael Tuexen 					net->mtu += sizeof(struct udphdr);
57023cf729a9SMichael Tuexen 					/* XXX Update smallest_mtu */
5703a8775ad9SMichael Tuexen 				}
5704a8775ad9SMichael Tuexen 				net->port = port;
5705a8775ad9SMichael Tuexen 			}
57064474d71aSMichael Tuexen #endif
57075fe29cdfSMichael Tuexen 			if (net != NULL) {
5708457b4b88SMichael Tuexen 				net->flowtype = mflowtype;
57095fe29cdfSMichael Tuexen 				net->flowid = mflowid;
5710a8775ad9SMichael Tuexen 			}
5711a8775ad9SMichael Tuexen 			if ((inp != NULL) && (stcb != NULL)) {
5712a8775ad9SMichael Tuexen 				sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
5713a8775ad9SMichael Tuexen 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5714a8775ad9SMichael Tuexen 			} else if ((inp != NULL) && (stcb == NULL)) {
5715a8775ad9SMichael Tuexen 				inp_decr = inp;
5716a8775ad9SMichael Tuexen 			}
5717a8775ad9SMichael Tuexen 			SCTP_STAT_INCR(sctps_badsum);
5718a8775ad9SMichael Tuexen 			SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5719a8775ad9SMichael Tuexen 			goto out;
5720a8775ad9SMichael Tuexen 		}
5721a8775ad9SMichael Tuexen 	}
5722a8775ad9SMichael Tuexen #endif
5723a8775ad9SMichael Tuexen 	/* Destination port of 0 is illegal, based on RFC4960. */
5724a8775ad9SMichael Tuexen 	if (sh->dest_port == 0) {
5725a8775ad9SMichael Tuexen 		SCTP_STAT_INCR(sctps_hdrops);
5726a8775ad9SMichael Tuexen 		goto out;
5727a8775ad9SMichael Tuexen 	}
5728a8775ad9SMichael Tuexen 	stcb = sctp_findassociation_addr(m, offset, src, dst,
5729a8775ad9SMichael Tuexen 	    sh, ch, &inp, &net, vrf_id);
57304474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
57313cf729a9SMichael Tuexen 	if ((ch->chunk_type != SCTP_INITIATION) &&
57323cf729a9SMichael Tuexen 	    (net != NULL) && (net->port != port)) {
5733a8775ad9SMichael Tuexen 		if (net->port == 0) {
57343cf729a9SMichael Tuexen 			/* UDP encapsulation turned on. */
57353cf729a9SMichael Tuexen 			net->mtu -= sizeof(struct udphdr);
57363cf729a9SMichael Tuexen 			if (stcb->asoc.smallest_mtu > net->mtu) {
57373cf729a9SMichael Tuexen 				sctp_pathmtu_adjustment(stcb, net->mtu);
57383cf729a9SMichael Tuexen 			}
57393cf729a9SMichael Tuexen 		} else if (port == 0) {
57403cf729a9SMichael Tuexen 			/* UDP encapsulation turned off. */
57413cf729a9SMichael Tuexen 			net->mtu += sizeof(struct udphdr);
57423cf729a9SMichael Tuexen 			/* XXX Update smallest_mtu */
5743a8775ad9SMichael Tuexen 		}
5744a8775ad9SMichael Tuexen 		net->port = port;
5745a8775ad9SMichael Tuexen 	}
57464474d71aSMichael Tuexen #endif
57475fe29cdfSMichael Tuexen 	if (net != NULL) {
5748457b4b88SMichael Tuexen 		net->flowtype = mflowtype;
57495fe29cdfSMichael Tuexen 		net->flowid = mflowid;
5750a8775ad9SMichael Tuexen 	}
5751a8775ad9SMichael Tuexen 	if (inp == NULL) {
5752a8775ad9SMichael Tuexen 		SCTP_STAT_INCR(sctps_noport);
5753a8775ad9SMichael Tuexen 		if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
5754a8775ad9SMichael Tuexen 			goto out;
5755a8775ad9SMichael Tuexen 		}
5756a8775ad9SMichael Tuexen 		if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5757a8775ad9SMichael Tuexen 			sctp_send_shutdown_complete2(src, dst, sh,
5758d089f9b9SMichael Tuexen 			    mflowtype, mflowid, fibnum,
5759a8775ad9SMichael Tuexen 			    vrf_id, port);
5760a8775ad9SMichael Tuexen 			goto out;
5761a8775ad9SMichael Tuexen 		}
5762a8775ad9SMichael Tuexen 		if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5763a8775ad9SMichael Tuexen 			goto out;
5764a8775ad9SMichael Tuexen 		}
5765a8775ad9SMichael Tuexen 		if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
5766a8775ad9SMichael Tuexen 			if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
5767a8775ad9SMichael Tuexen 			    ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
5768a8775ad9SMichael Tuexen 			    (ch->chunk_type != SCTP_INIT))) {
5769ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5770ff1ffd74SMichael Tuexen 				    "Out of the blue");
5771a8775ad9SMichael Tuexen 				sctp_send_abort(m, iphlen, src, dst,
5772ff1ffd74SMichael Tuexen 				    sh, 0, op_err,
5773d089f9b9SMichael Tuexen 				    mflowtype, mflowid, fibnum,
5774a8775ad9SMichael Tuexen 				    vrf_id, port);
5775a8775ad9SMichael Tuexen 			}
5776a8775ad9SMichael Tuexen 		}
5777a8775ad9SMichael Tuexen 		goto out;
5778a8775ad9SMichael Tuexen 	} else if (stcb == NULL) {
5779a8775ad9SMichael Tuexen 		inp_decr = inp;
5780a8775ad9SMichael Tuexen 	}
5781a8775ad9SMichael Tuexen #ifdef IPSEC
5782a8775ad9SMichael Tuexen 	/*-
5783a8775ad9SMichael Tuexen 	 * I very much doubt any of the IPSEC stuff will work but I have no
5784a8775ad9SMichael Tuexen 	 * idea, so I will leave it in place.
5785a8775ad9SMichael Tuexen 	 */
5786a8775ad9SMichael Tuexen 	if (inp != NULL) {
5787a8775ad9SMichael Tuexen 		switch (dst->sa_family) {
5788a8775ad9SMichael Tuexen #ifdef INET
5789a8775ad9SMichael Tuexen 		case AF_INET:
5790a8775ad9SMichael Tuexen 			if (ipsec4_in_reject(m, &inp->ip_inp.inp)) {
5791a8775ad9SMichael Tuexen 				SCTP_STAT_INCR(sctps_hdrops);
5792a8775ad9SMichael Tuexen 				goto out;
5793a8775ad9SMichael Tuexen 			}
5794a8775ad9SMichael Tuexen 			break;
5795a8775ad9SMichael Tuexen #endif
5796a8775ad9SMichael Tuexen #ifdef INET6
5797a8775ad9SMichael Tuexen 		case AF_INET6:
5798a8775ad9SMichael Tuexen 			if (ipsec6_in_reject(m, &inp->ip_inp.inp)) {
5799a8775ad9SMichael Tuexen 				SCTP_STAT_INCR(sctps_hdrops);
5800a8775ad9SMichael Tuexen 				goto out;
5801a8775ad9SMichael Tuexen 			}
5802a8775ad9SMichael Tuexen 			break;
5803a8775ad9SMichael Tuexen #endif
5804a8775ad9SMichael Tuexen 		default:
5805a8775ad9SMichael Tuexen 			break;
5806a8775ad9SMichael Tuexen 		}
5807a8775ad9SMichael Tuexen 	}
5808a8775ad9SMichael Tuexen #endif
5809b3f1ea41SRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5810dd294dceSMichael Tuexen 	    (void *)m, iphlen, offset, length, (void *)stcb);
5811f8829a4aSRandall Stewart 	if (stcb) {
5812f8829a4aSRandall Stewart 		/* always clear this before beginning a packet */
5813f8829a4aSRandall Stewart 		stcb->asoc.authenticated = 0;
5814f8829a4aSRandall Stewart 		stcb->asoc.seen_a_sack_this_pkt = 0;
58152afb3e84SRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5816dd294dceSMichael Tuexen 		    (void *)stcb, stcb->asoc.state);
58172afb3e84SRandall Stewart 
5818c4739e2fSRandall Stewart 		if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5819c4739e2fSRandall Stewart 		    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
582063981c2bSRandall Stewart 			/*-
582163981c2bSRandall Stewart 			 * If we hit here, we had a ref count
582263981c2bSRandall Stewart 			 * up when the assoc was aborted and the
582363981c2bSRandall Stewart 			 * timer is clearing out the assoc, we should
582463981c2bSRandall Stewart 			 * NOT respond to any packet.. its OOTB.
582563981c2bSRandall Stewart 			 */
582663981c2bSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
5827a8775ad9SMichael Tuexen 			stcb = NULL;
58286e9c45e0SMichael Tuexen 			snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5829ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5830ff1ffd74SMichael Tuexen 			    msg);
5831ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5832d089f9b9SMichael Tuexen 			    mflowtype, mflowid, inp->fibnum,
5833c54a18d2SRandall Stewart 			    vrf_id, port);
5834a8775ad9SMichael Tuexen 			goto out;
583563981c2bSRandall Stewart 		}
5836f8829a4aSRandall Stewart 	}
5837f8829a4aSRandall Stewart 	if (IS_SCTP_CONTROL(ch)) {
5838f8829a4aSRandall Stewart 		/* process the control portion of the SCTP packet */
58393c503c28SRandall Stewart 		/* sa_ignore NO_NULL_CHK */
5840b1754ad1SMichael Tuexen 		stcb = sctp_process_control(m, iphlen, &offset, length,
5841b1754ad1SMichael Tuexen 		    src, dst, sh, ch,
5842f30ac432SMichael Tuexen 		    inp, stcb, &net, &fwd_tsn_seen,
5843d089f9b9SMichael Tuexen 		    mflowtype, mflowid, fibnum,
5844f30ac432SMichael Tuexen 		    vrf_id, port);
5845f8829a4aSRandall Stewart 		if (stcb) {
5846f8829a4aSRandall Stewart 			/*
5847f8829a4aSRandall Stewart 			 * This covers us if the cookie-echo was there and
5848f8829a4aSRandall Stewart 			 * it changes our INP.
5849f8829a4aSRandall Stewart 			 */
5850f8829a4aSRandall Stewart 			inp = stcb->sctp_ep;
58514474d71aSMichael Tuexen #if defined(INET) || defined(INET6)
58523cf729a9SMichael Tuexen 			if ((ch->chunk_type != SCTP_INITIATION) &&
58533cf729a9SMichael Tuexen 			    (net != NULL) && (net->port != port)) {
5854b3f1ea41SRandall Stewart 				if (net->port == 0) {
58553cf729a9SMichael Tuexen 					/* UDP encapsulation turned on. */
58563cf729a9SMichael Tuexen 					net->mtu -= sizeof(struct udphdr);
58573cf729a9SMichael Tuexen 					if (stcb->asoc.smallest_mtu > net->mtu) {
58583cf729a9SMichael Tuexen 						sctp_pathmtu_adjustment(stcb, net->mtu);
58593cf729a9SMichael Tuexen 					}
58603cf729a9SMichael Tuexen 				} else if (port == 0) {
58613cf729a9SMichael Tuexen 					/* UDP encapsulation turned off. */
58623cf729a9SMichael Tuexen 					net->mtu += sizeof(struct udphdr);
58633cf729a9SMichael Tuexen 					/* XXX Update smallest_mtu */
5864b3f1ea41SRandall Stewart 				}
5865b3f1ea41SRandall Stewart 				net->port = port;
5866b3f1ea41SRandall Stewart 			}
58674474d71aSMichael Tuexen #endif
5868f8829a4aSRandall Stewart 		}
5869f8829a4aSRandall Stewart 	} else {
5870f8829a4aSRandall Stewart 		/*
5871f8829a4aSRandall Stewart 		 * no control chunks, so pre-process DATA chunks (these
5872f8829a4aSRandall Stewart 		 * checks are taken care of by control processing)
5873f8829a4aSRandall Stewart 		 */
5874f8829a4aSRandall Stewart 
5875f8829a4aSRandall Stewart 		/*
5876f8829a4aSRandall Stewart 		 * if DATA only packet, and auth is required, then punt...
5877f8829a4aSRandall Stewart 		 * can't have authenticated without any AUTH (control)
5878f8829a4aSRandall Stewart 		 * chunks
5879f8829a4aSRandall Stewart 		 */
5880b3f1ea41SRandall Stewart 		if ((stcb != NULL) &&
5881c79bec9cSMichael Tuexen 		    (stcb->asoc.auth_supported == 1) &&
5882b3f1ea41SRandall Stewart 		    sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5883f8829a4aSRandall Stewart 			/* "silently" ignore */
5884f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvauthmissing);
5885a8775ad9SMichael Tuexen 			goto out;
5886f8829a4aSRandall Stewart 		}
5887f8829a4aSRandall Stewart 		if (stcb == NULL) {
5888f8829a4aSRandall Stewart 			/* out of the blue DATA chunk */
58896e9c45e0SMichael Tuexen 			snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5890ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5891ff1ffd74SMichael Tuexen 			    msg);
5892ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5893d089f9b9SMichael Tuexen 			    mflowtype, mflowid, fibnum,
5894c54a18d2SRandall Stewart 			    vrf_id, port);
5895a8775ad9SMichael Tuexen 			goto out;
5896f8829a4aSRandall Stewart 		}
5897f8829a4aSRandall Stewart 		if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5898f8829a4aSRandall Stewart 			/* v_tag mismatch! */
5899f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_badvtag);
5900a8775ad9SMichael Tuexen 			goto out;
5901f8829a4aSRandall Stewart 		}
5902f8829a4aSRandall Stewart 	}
5903f8829a4aSRandall Stewart 
5904f8829a4aSRandall Stewart 	if (stcb == NULL) {
5905f8829a4aSRandall Stewart 		/*
5906f8829a4aSRandall Stewart 		 * no valid TCB for this packet, or we found it's a bad
5907f8829a4aSRandall Stewart 		 * packet while processing control, or we're done with this
5908f8829a4aSRandall Stewart 		 * packet (done or skip rest of data), so we drop it...
5909f8829a4aSRandall Stewart 		 */
5910a8775ad9SMichael Tuexen 		goto out;
5911f8829a4aSRandall Stewart 	}
5912f8829a4aSRandall Stewart 	/*
5913f8829a4aSRandall Stewart 	 * DATA chunk processing
5914f8829a4aSRandall Stewart 	 */
5915f8829a4aSRandall Stewart 	/* plow through the data chunks while length > offset */
5916f8829a4aSRandall Stewart 
5917f8829a4aSRandall Stewart 	/*
5918f8829a4aSRandall Stewart 	 * Rest should be DATA only.  Check authentication state if AUTH for
5919f8829a4aSRandall Stewart 	 * DATA is required.
5920f8829a4aSRandall Stewart 	 */
5921b3f1ea41SRandall Stewart 	if ((length > offset) &&
5922b3f1ea41SRandall Stewart 	    (stcb != NULL) &&
5923c79bec9cSMichael Tuexen 	    (stcb->asoc.auth_supported == 1) &&
5924b3f1ea41SRandall Stewart 	    sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5925f8829a4aSRandall Stewart 	    !stcb->asoc.authenticated) {
5926f8829a4aSRandall Stewart 		/* "silently" ignore */
5927f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvauthmissing);
5928ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_AUTH1,
5929ad81507eSRandall Stewart 		    "Data chunk requires AUTH, skipped\n");
5930a5d547adSRandall Stewart 		goto trigger_send;
5931f8829a4aSRandall Stewart 	}
5932f8829a4aSRandall Stewart 	if (length > offset) {
5933f8829a4aSRandall Stewart 		int retval;
5934f8829a4aSRandall Stewart 
5935f8829a4aSRandall Stewart 		/*
5936f8829a4aSRandall Stewart 		 * First check to make sure our state is correct. We would
5937f8829a4aSRandall Stewart 		 * not get here unless we really did have a tag, so we don't
5938f8829a4aSRandall Stewart 		 * abort if this happens, just dump the chunk silently.
5939f8829a4aSRandall Stewart 		 */
5940f8829a4aSRandall Stewart 		switch (SCTP_GET_STATE(&stcb->asoc)) {
5941f8829a4aSRandall Stewart 		case SCTP_STATE_COOKIE_ECHOED:
5942f8829a4aSRandall Stewart 			/*
5943f8829a4aSRandall Stewart 			 * we consider data with valid tags in this state
5944f8829a4aSRandall Stewart 			 * shows us the cookie-ack was lost. Imply it was
5945f8829a4aSRandall Stewart 			 * there.
5946f8829a4aSRandall Stewart 			 */
5947b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5948c4739e2fSRandall Stewart 				sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5949c4739e2fSRandall Stewart 				    stcb->asoc.overall_error_count,
5950c4739e2fSRandall Stewart 				    0,
5951c4739e2fSRandall Stewart 				    SCTP_FROM_SCTP_INPUT,
5952c4739e2fSRandall Stewart 				    __LINE__);
5953c4739e2fSRandall Stewart 			}
5954f8829a4aSRandall Stewart 			stcb->asoc.overall_error_count = 0;
5955f8829a4aSRandall Stewart 			sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5956f8829a4aSRandall Stewart 			break;
5957f8829a4aSRandall Stewart 		case SCTP_STATE_COOKIE_WAIT:
5958f8829a4aSRandall Stewart 			/*
5959f8829a4aSRandall Stewart 			 * We consider OOTB any data sent during asoc setup.
5960f8829a4aSRandall Stewart 			 */
59616e9c45e0SMichael Tuexen 			snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5962ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5963ff1ffd74SMichael Tuexen 			    msg);
5964ff1ffd74SMichael Tuexen 			sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5965d089f9b9SMichael Tuexen 			    mflowtype, mflowid, inp->fibnum,
5966c54a18d2SRandall Stewart 			    vrf_id, port);
5967a8775ad9SMichael Tuexen 			goto out;
596852be287eSRandall Stewart 			/* sa_ignore NOTREACHED */
5969f8829a4aSRandall Stewart 			break;
5970f8829a4aSRandall Stewart 		case SCTP_STATE_EMPTY:	/* should not happen */
5971f8829a4aSRandall Stewart 		case SCTP_STATE_INUSE:	/* should not happen */
5972f8829a4aSRandall Stewart 		case SCTP_STATE_SHUTDOWN_RECEIVED:	/* This is a peer error */
5973f8829a4aSRandall Stewart 		case SCTP_STATE_SHUTDOWN_ACK_SENT:
5974f8829a4aSRandall Stewart 		default:
5975a8775ad9SMichael Tuexen 			goto out;
597652be287eSRandall Stewart 			/* sa_ignore NOTREACHED */
5977f8829a4aSRandall Stewart 			break;
5978f8829a4aSRandall Stewart 		case SCTP_STATE_OPEN:
5979f8829a4aSRandall Stewart 		case SCTP_STATE_SHUTDOWN_SENT:
5980f8829a4aSRandall Stewart 			break;
5981f8829a4aSRandall Stewart 		}
5982f8829a4aSRandall Stewart 		/* plow through the data chunks while length > offset */
5983b1754ad1SMichael Tuexen 		retval = sctp_process_data(mm, iphlen, &offset, length,
5984e7e71dd7SMichael Tuexen 		    inp, stcb, net, &high_tsn);
5985f8829a4aSRandall Stewart 		if (retval == 2) {
5986f8829a4aSRandall Stewart 			/*
5987f8829a4aSRandall Stewart 			 * The association aborted, NO UNLOCK needed since
5988f8829a4aSRandall Stewart 			 * the association is destroyed.
5989f8829a4aSRandall Stewart 			 */
5990a8775ad9SMichael Tuexen 			stcb = NULL;
5991a8775ad9SMichael Tuexen 			goto out;
5992f8829a4aSRandall Stewart 		}
5993f8829a4aSRandall Stewart 		data_processed = 1;
5994f8829a4aSRandall Stewart 		/*
5995f8829a4aSRandall Stewart 		 * Anything important needs to have been m_copy'ed in
5996f8829a4aSRandall Stewart 		 * process_data
5997f8829a4aSRandall Stewart 		 */
5998f8829a4aSRandall Stewart 	}
5999493d8e5aSRandall Stewart 	/* take care of ecn */
600060990c0cSMichael Tuexen 	if ((data_processed == 1) &&
6001f342355aSMichael Tuexen 	    (stcb->asoc.ecn_supported == 1) &&
6002c446091bSMichael Tuexen 	    ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
6003493d8e5aSRandall Stewart 		/* Yep, we need to add a ECNE */
6004493d8e5aSRandall Stewart 		sctp_send_ecn_echo(stcb, net, high_tsn);
6005493d8e5aSRandall Stewart 	}
6006f8829a4aSRandall Stewart 	if ((data_processed == 0) && (fwd_tsn_seen)) {
60078f777478SMichael Tuexen 		int was_a_gap;
60088f777478SMichael Tuexen 		uint32_t highest_tsn;
6009f8829a4aSRandall Stewart 
601020b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
60118f777478SMichael Tuexen 			highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
60128f777478SMichael Tuexen 		} else {
60138f777478SMichael Tuexen 			highest_tsn = stcb->asoc.highest_tsn_inside_map;
6014f8829a4aSRandall Stewart 		}
601520b07a4dSMichael Tuexen 		was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
60168933fa13SRandall Stewart 		stcb->asoc.send_sack = 1;
60177215cc1bSMichael Tuexen 		sctp_sack_check(stcb, was_a_gap);
60188933fa13SRandall Stewart 	} else if (fwd_tsn_seen) {
60198933fa13SRandall Stewart 		stcb->asoc.send_sack = 1;
6020f8829a4aSRandall Stewart 	}
6021f8829a4aSRandall Stewart 	/* trigger send of any chunks in queue... */
6022a5d547adSRandall Stewart trigger_send:
6023f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
6024f8829a4aSRandall Stewart 	sctp_audit_log(0xE0, 2);
6025f8829a4aSRandall Stewart 	sctp_auditing(1, inp, stcb, net);
6026f8829a4aSRandall Stewart #endif
6027ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT1,
6028ad81507eSRandall Stewart 	    "Check for chunk output prw:%d tqe:%d tf=%d\n",
6029f8829a4aSRandall Stewart 	    stcb->asoc.peers_rwnd,
6030f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&stcb->asoc.control_send_queue),
6031f8829a4aSRandall Stewart 	    stcb->asoc.total_flight);
6032f8829a4aSRandall Stewart 	un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
6033493d8e5aSRandall Stewart 	if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
6034493d8e5aSRandall Stewart 		cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
6035493d8e5aSRandall Stewart 	}
6036*5114dccbSMichael Tuexen 	if (!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue) ||
6037*5114dccbSMichael Tuexen 	    cnt_ctrl_ready ||
6038*5114dccbSMichael Tuexen 	    stcb->asoc.trigger_reset ||
6039f8829a4aSRandall Stewart 	    ((un_sent) &&
6040f8829a4aSRandall Stewart 	    (stcb->asoc.peers_rwnd > 0 ||
6041f8829a4aSRandall Stewart 	    (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
6042ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
6043ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
6044ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
6045f8829a4aSRandall Stewart 	}
6046f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
6047f8829a4aSRandall Stewart 	sctp_audit_log(0xE0, 3);
6048f8829a4aSRandall Stewart 	sctp_auditing(2, inp, stcb, net);
6049f8829a4aSRandall Stewart #endif
6050a8775ad9SMichael Tuexen out:
6051a8775ad9SMichael Tuexen 	if (stcb != NULL) {
6052f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
6053a8775ad9SMichael Tuexen 	}
6054a8775ad9SMichael Tuexen 	if (inp_decr != NULL) {
6055a8775ad9SMichael Tuexen 		/* reduce ref-count */
6056a8775ad9SMichael Tuexen 		SCTP_INP_WLOCK(inp_decr);
6057a8775ad9SMichael Tuexen 		SCTP_INP_DECR_REF(inp_decr);
6058a8775ad9SMichael Tuexen 		SCTP_INP_WUNLOCK(inp_decr);
6059a8775ad9SMichael Tuexen 	}
60606e55db54SRandall Stewart 	return;
6061f8829a4aSRandall Stewart }
6062f8829a4aSRandall Stewart 
6063e6194c2eSMichael Tuexen #ifdef INET
6064f8829a4aSRandall Stewart void
6065af83f5d7SRoman Divacky sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
6066f8829a4aSRandall Stewart {
6067139bc87fSRandall Stewart 	struct mbuf *m;
6068f8829a4aSRandall Stewart 	int iphlen;
6069ad21a364SRandall Stewart 	uint32_t vrf_id = 0;
6070f8829a4aSRandall Stewart 	uint8_t ecn_bits;
6071b1754ad1SMichael Tuexen 	struct sockaddr_in src, dst;
6072f8829a4aSRandall Stewart 	struct ip *ip;
6073f8829a4aSRandall Stewart 	struct sctphdr *sh;
6074f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
60756dc5aabcSMichael Tuexen 	int length, offset;
6076f8829a4aSRandall Stewart 
60779c7635e1SMichael Tuexen #if !defined(SCTP_WITH_NO_CSUM)
6078a8775ad9SMichael Tuexen 	uint8_t compute_crc;
60799c7635e1SMichael Tuexen 
60809c7635e1SMichael Tuexen #endif
6081a8775ad9SMichael Tuexen 	uint32_t mflowid;
6082457b4b88SMichael Tuexen 	uint8_t mflowtype;
6083d089f9b9SMichael Tuexen 	uint16_t fibnum;
60849c7635e1SMichael Tuexen 
60856dc5aabcSMichael Tuexen 	iphlen = off;
608617205eccSRandall Stewart 	if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
608717205eccSRandall Stewart 		SCTP_RELEASE_PKT(i_pak);
608817205eccSRandall Stewart 		return;
608917205eccSRandall Stewart 	}
6090139bc87fSRandall Stewart 	m = SCTP_HEADER_TO_CHAIN(i_pak);
6091f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
6092f8829a4aSRandall Stewart 	/* Log in any input mbufs */
6093b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
60944be807c4SMichael Tuexen 		sctp_log_mbc(m, SCTP_MBUF_INPUT);
609580fefe0aSRandall Stewart 	}
6096f8829a4aSRandall Stewart #endif
6097207304d4SRandall Stewart #ifdef SCTP_PACKET_LOGGING
60986dc5aabcSMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
6099f9384252SMichael Tuexen 		sctp_packet_log(m);
61006dc5aabcSMichael Tuexen 	}
6101207304d4SRandall Stewart #endif
6102a8775ad9SMichael Tuexen 	SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
61031a94cdbeSMichael Tuexen 	    "sctp_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
6104a8775ad9SMichael Tuexen 	    m->m_pkthdr.len,
6105a8775ad9SMichael Tuexen 	    if_name(m->m_pkthdr.rcvif),
61061a94cdbeSMichael Tuexen 	    (int)m->m_pkthdr.csum_flags, CSUM_BITS);
6107f30ac432SMichael Tuexen 	mflowid = m->m_pkthdr.flowid;
6108457b4b88SMichael Tuexen 	mflowtype = M_HASHTYPE_GET(m);
6109d089f9b9SMichael Tuexen 	fibnum = M_GETFIB(m);
61106dc5aabcSMichael Tuexen 	SCTP_STAT_INCR(sctps_recvpackets);
61116dc5aabcSMichael Tuexen 	SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
61126dc5aabcSMichael Tuexen 	/* Get IP, SCTP, and first chunk header together in the first mbuf. */
61136dc5aabcSMichael Tuexen 	offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6114139bc87fSRandall Stewart 	if (SCTP_BUF_LEN(m) < offset) {
6115b1754ad1SMichael Tuexen 		if ((m = m_pullup(m, offset)) == NULL) {
6116f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_hdrops);
6117f8829a4aSRandall Stewart 			return;
6118f8829a4aSRandall Stewart 		}
6119f8829a4aSRandall Stewart 	}
6120b1754ad1SMichael Tuexen 	ip = mtod(m, struct ip *);
61216dc5aabcSMichael Tuexen 	sh = (struct sctphdr *)((caddr_t)ip + iphlen);
61226dc5aabcSMichael Tuexen 	ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
61236dc5aabcSMichael Tuexen 	offset -= sizeof(struct sctp_chunkhdr);
6124b1754ad1SMichael Tuexen 	memset(&src, 0, sizeof(struct sockaddr_in));
6125b1754ad1SMichael Tuexen 	src.sin_family = AF_INET;
6126b1754ad1SMichael Tuexen 	src.sin_len = sizeof(struct sockaddr_in);
6127b1754ad1SMichael Tuexen 	src.sin_port = sh->src_port;
6128b1754ad1SMichael Tuexen 	src.sin_addr = ip->ip_src;
6129b1754ad1SMichael Tuexen 	memset(&dst, 0, sizeof(struct sockaddr_in));
6130b1754ad1SMichael Tuexen 	dst.sin_family = AF_INET;
6131b1754ad1SMichael Tuexen 	dst.sin_len = sizeof(struct sockaddr_in);
6132b1754ad1SMichael Tuexen 	dst.sin_port = sh->dest_port;
6133b1754ad1SMichael Tuexen 	dst.sin_addr = ip->ip_dst;
61348ad458a4SGleb Smirnoff 	length = ntohs(ip->ip_len);
61356dc5aabcSMichael Tuexen 	/* Validate mbuf chain length with IP payload length. */
6136a8775ad9SMichael Tuexen 	if (SCTP_HEADER_LEN(m) != length) {
61376dc5aabcSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT1,
6138a8775ad9SMichael Tuexen 		    "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
6139a99b6783SRandall Stewart 		SCTP_STAT_INCR(sctps_hdrops);
6140a8775ad9SMichael Tuexen 		goto out;
6141a99b6783SRandall Stewart 	}
6142f8829a4aSRandall Stewart 	/* SCTP does not allow broadcasts or multicasts */
6143b1754ad1SMichael Tuexen 	if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
6144a8775ad9SMichael Tuexen 		goto out;
6145f8829a4aSRandall Stewart 	}
6146b1754ad1SMichael Tuexen 	if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
6147a8775ad9SMichael Tuexen 		goto out;
6148f8829a4aSRandall Stewart 	}
6149a8775ad9SMichael Tuexen 	ecn_bits = ip->ip_tos;
61509c7635e1SMichael Tuexen #if defined(SCTP_WITH_NO_CSUM)
61519c7635e1SMichael Tuexen 	SCTP_STAT_INCR(sctps_recvnocrc);
61529c7635e1SMichael Tuexen #else
6153a99b6783SRandall Stewart 	if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
6154a99b6783SRandall Stewart 		SCTP_STAT_INCR(sctps_recvhwcrc);
6155a8775ad9SMichael Tuexen 		compute_crc = 0;
6156a8775ad9SMichael Tuexen 	} else {
6157a99b6783SRandall Stewart 		SCTP_STAT_INCR(sctps_recvswcrc);
6158a8775ad9SMichael Tuexen 		compute_crc = 1;
6159f8829a4aSRandall Stewart 	}
61606dc5aabcSMichael Tuexen #endif
6161b1754ad1SMichael Tuexen 	sctp_common_input_processing(&m, iphlen, offset, length,
6162b1754ad1SMichael Tuexen 	    (struct sockaddr *)&src,
6163b1754ad1SMichael Tuexen 	    (struct sockaddr *)&dst,
6164a8775ad9SMichael Tuexen 	    sh, ch,
6165a8775ad9SMichael Tuexen #if !defined(SCTP_WITH_NO_CSUM)
6166a8775ad9SMichael Tuexen 	    compute_crc,
6167a8775ad9SMichael Tuexen #endif
6168a8775ad9SMichael Tuexen 	    ecn_bits,
6169d089f9b9SMichael Tuexen 	    mflowtype, mflowid, fibnum,
6170f30ac432SMichael Tuexen 	    vrf_id, port);
6171a8775ad9SMichael Tuexen out:
6172f8829a4aSRandall Stewart 	if (m) {
6173f8829a4aSRandall Stewart 		sctp_m_freem(m);
6174f8829a4aSRandall Stewart 	}
6175f8829a4aSRandall Stewart 	return;
6176f8829a4aSRandall Stewart }
6177bfc46083SRandall Stewart 
61780071ee5eSRandall Stewart #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
61790071ee5eSRandall Stewart extern int *sctp_cpuarry;
61800071ee5eSRandall Stewart 
61810071ee5eSRandall Stewart #endif
6182bfc46083SRandall Stewart 
61838f5a8818SKevin Lo int
618482eaf95eSMichael Tuexen sctp_input(struct mbuf **mp, int *offp, int proto SCTP_UNUSED)
6185c54a18d2SRandall Stewart {
61868f5a8818SKevin Lo 	struct mbuf *m;
61878f5a8818SKevin Lo 	int off;
61888f5a8818SKevin Lo 
61898f5a8818SKevin Lo 	m = *mp;
61908f5a8818SKevin Lo 	off = *offp;
6191bfc46083SRandall Stewart #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
619282eaf95eSMichael Tuexen 	if (mp_ncpus > 1) {
6193bfc46083SRandall Stewart 		struct ip *ip;
6194bfc46083SRandall Stewart 		struct sctphdr *sh;
6195bfc46083SRandall Stewart 		int offset;
6196bfc46083SRandall Stewart 		int cpu_to_use;
619738521fb9SRandall Stewart 		uint32_t flowid, tag;
6198bfc46083SRandall Stewart 
6199457b4b88SMichael Tuexen 		if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
620038521fb9SRandall Stewart 			flowid = m->m_pkthdr.flowid;
620138521fb9SRandall Stewart 		} else {
620238521fb9SRandall Stewart 			/*
620338521fb9SRandall Stewart 			 * No flow id built by lower layers fix it so we
620438521fb9SRandall Stewart 			 * create one.
620538521fb9SRandall Stewart 			 */
6206b1754ad1SMichael Tuexen 			offset = off + sizeof(struct sctphdr);
6207bfc46083SRandall Stewart 			if (SCTP_BUF_LEN(m) < offset) {
6208b1754ad1SMichael Tuexen 				if ((m = m_pullup(m, offset)) == NULL) {
6209bfc46083SRandall Stewart 					SCTP_STAT_INCR(sctps_hdrops);
62108f5a8818SKevin Lo 					return (IPPROTO_DONE);
6211bfc46083SRandall Stewart 				}
6212bfc46083SRandall Stewart 			}
6213b1754ad1SMichael Tuexen 			ip = mtod(m, struct ip *);
6214bfc46083SRandall Stewart 			sh = (struct sctphdr *)((caddr_t)ip + off);
62150071ee5eSRandall Stewart 			tag = htonl(sh->v_tag);
621638521fb9SRandall Stewart 			flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
621738521fb9SRandall Stewart 			m->m_pkthdr.flowid = flowid;
6218457b4b88SMichael Tuexen 			M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE);
62190071ee5eSRandall Stewart 		}
622038521fb9SRandall Stewart 		cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
6221bfc46083SRandall Stewart 		sctp_queue_to_mcore(m, off, cpu_to_use);
62228f5a8818SKevin Lo 		return (IPPROTO_DONE);
6223bfc46083SRandall Stewart 	}
6224bfc46083SRandall Stewart #endif
6225bfc46083SRandall Stewart 	sctp_input_with_port(m, off, 0);
62268f5a8818SKevin Lo 	return (IPPROTO_DONE);
6227c54a18d2SRandall Stewart }
6228e6194c2eSMichael Tuexen 
6229e6194c2eSMichael Tuexen #endif
6230