xref: /freebsd/sys/netinet/sctp_input.c (revision 2c77ec54190294415c52a8da0c0d9c5a957c03a3)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
5  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * a) Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  *
14  * b) Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the distribution.
17  *
18  * c) Neither the name of Cisco Systems, Inc. nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <netinet/sctp_os.h>
39 #include <netinet/sctp_var.h>
40 #include <netinet/sctp_sysctl.h>
41 #include <netinet/sctp_pcb.h>
42 #include <netinet/sctp_header.h>
43 #include <netinet/sctputil.h>
44 #include <netinet/sctp_output.h>
45 #include <netinet/sctp_input.h>
46 #include <netinet/sctp_auth.h>
47 #include <netinet/sctp_indata.h>
48 #include <netinet/sctp_asconf.h>
49 #include <netinet/sctp_bsd_addr.h>
50 #include <netinet/sctp_timer.h>
51 #include <netinet/sctp_crc32.h>
52 #if defined(INET) || defined(INET6)
53 #include <netinet/udp.h>
54 #endif
55 #include <sys/smp.h>
56 
57 
58 
59 static void
60 sctp_stop_all_cookie_timers(struct sctp_tcb *stcb)
61 {
62 	struct sctp_nets *net;
63 
64 	/*
65 	 * This now not only stops all cookie timers it also stops any INIT
66 	 * timers as well. This will make sure that the timers are stopped
67 	 * in all collision cases.
68 	 */
69 	SCTP_TCB_LOCK_ASSERT(stcb);
70 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
71 		if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) {
72 			sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
73 			    stcb->sctp_ep,
74 			    stcb,
75 			    net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1);
76 		} else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) {
77 			sctp_timer_stop(SCTP_TIMER_TYPE_INIT,
78 			    stcb->sctp_ep,
79 			    stcb,
80 			    net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2);
81 		}
82 	}
83 }
84 
85 /* INIT handler */
86 static void
87 sctp_handle_init(struct mbuf *m, int iphlen, int offset,
88     struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
89     struct sctp_init_chunk *cp, struct sctp_inpcb *inp,
90     struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_no_unlock,
91     uint8_t mflowtype, uint32_t mflowid,
92     uint32_t vrf_id, uint16_t port)
93 {
94 	struct sctp_init *init;
95 	struct mbuf *op_err;
96 
97 	SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n",
98 	    (void *)stcb);
99 	if (stcb == NULL) {
100 		SCTP_INP_RLOCK(inp);
101 	}
102 	/* validate length */
103 	if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
104 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
105 		sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
106 		    mflowtype, mflowid,
107 		    vrf_id, port);
108 		if (stcb)
109 			*abort_no_unlock = 1;
110 		goto outnow;
111 	}
112 	/* validate parameters */
113 	init = &cp->init;
114 	if (init->initiate_tag == 0) {
115 		/* protocol error... send abort */
116 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
117 		sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
118 		    mflowtype, mflowid,
119 		    vrf_id, port);
120 		if (stcb)
121 			*abort_no_unlock = 1;
122 		goto outnow;
123 	}
124 	if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
125 		/* invalid parameter... send abort */
126 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
127 		sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
128 		    mflowtype, mflowid,
129 		    vrf_id, port);
130 		if (stcb)
131 			*abort_no_unlock = 1;
132 		goto outnow;
133 	}
134 	if (init->num_inbound_streams == 0) {
135 		/* protocol error... send abort */
136 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
137 		sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
138 		    mflowtype, mflowid,
139 		    vrf_id, port);
140 		if (stcb)
141 			*abort_no_unlock = 1;
142 		goto outnow;
143 	}
144 	if (init->num_outbound_streams == 0) {
145 		/* protocol error... send abort */
146 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
147 		sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
148 		    mflowtype, mflowid,
149 		    vrf_id, port);
150 		if (stcb)
151 			*abort_no_unlock = 1;
152 		goto outnow;
153 	}
154 	if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
155 	    offset + ntohs(cp->ch.chunk_length))) {
156 		/* auth parameter(s) error... send abort */
157 		op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
158 		    "Problem with AUTH parameters");
159 		sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
160 		    mflowtype, mflowid,
161 		    vrf_id, port);
162 		if (stcb)
163 			*abort_no_unlock = 1;
164 		goto outnow;
165 	}
166 	/* We are only accepting if we have a listening socket. */
167 	if ((stcb == NULL) &&
168 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
169 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
170 	    (!SCTP_IS_LISTENING(inp)))) {
171 		/*
172 		 * FIX ME ?? What about TCP model and we have a
173 		 * match/restart case? Actually no fix is needed. the lookup
174 		 * will always find the existing assoc so stcb would not be
175 		 * NULL. It may be questionable to do this since we COULD
176 		 * just send back the INIT-ACK and hope that the app did
177 		 * accept()'s by the time the COOKIE was sent. But there is
178 		 * a price to pay for COOKIE generation and I don't want to
179 		 * pay it on the chance that the app will actually do some
180 		 * accepts(). The App just looses and should NOT be in this
181 		 * state :-)
182 		 */
183 		if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) {
184 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
185 			    "No listener");
186 			sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
187 			    mflowtype, mflowid, inp->fibnum,
188 			    vrf_id, port);
189 		}
190 		goto outnow;
191 	}
192 	if ((stcb != NULL) &&
193 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
194 		SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n");
195 		sctp_send_shutdown_ack(stcb, NULL);
196 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
197 	} else {
198 		SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
199 		sctp_send_initiate_ack(inp, stcb, net, m, iphlen, offset,
200 		    src, dst, sh, cp,
201 		    mflowtype, mflowid,
202 		    vrf_id, port);
203 	}
204 outnow:
205 	if (stcb == NULL) {
206 		SCTP_INP_RUNLOCK(inp);
207 	}
208 }
209 
210 /*
211  * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
212  */
213 
214 int
215 sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked
216 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
217     SCTP_UNUSED
218 #endif
219 )
220 {
221 	int unsent_data;
222 	unsigned int i;
223 	struct sctp_stream_queue_pending *sp;
224 	struct sctp_association *asoc;
225 
226 	/*
227 	 * This function returns if any stream has true unsent data on it.
228 	 * Note that as it looks through it will clean up any places that
229 	 * have old data that has been sent but left at top of stream queue.
230 	 */
231 	asoc = &stcb->asoc;
232 	unsent_data = 0;
233 	SCTP_TCB_SEND_LOCK(stcb);
234 	if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
235 		/* Check to see if some data queued */
236 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
237 			/* sa_ignore FREED_MEMORY */
238 			sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue);
239 			if (sp == NULL) {
240 				continue;
241 			}
242 			if ((sp->msg_is_complete) &&
243 			    (sp->length == 0) &&
244 			    (sp->sender_all_done)) {
245 				/*
246 				 * We are doing differed cleanup. Last time
247 				 * through when we took all the data the
248 				 * sender_all_done was not set.
249 				 */
250 				if (sp->put_last_out == 0) {
251 					SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
252 					SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
253 					    sp->sender_all_done,
254 					    sp->length,
255 					    sp->msg_is_complete,
256 					    sp->put_last_out);
257 				}
258 				atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
259 				TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next);
260 				stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, &asoc->strmout[i], sp, 1);
261 				if (sp->net) {
262 					sctp_free_remote_addr(sp->net);
263 					sp->net = NULL;
264 				}
265 				if (sp->data) {
266 					sctp_m_freem(sp->data);
267 					sp->data = NULL;
268 				}
269 				sctp_free_a_strmoq(stcb, sp, so_locked);
270 				if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
271 					unsent_data++;
272 				}
273 			} else {
274 				unsent_data++;
275 			}
276 			if (unsent_data > 0) {
277 				break;
278 			}
279 		}
280 	}
281 	SCTP_TCB_SEND_UNLOCK(stcb);
282 	return (unsent_data);
283 }
284 
285 static int
286 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb)
287 {
288 	struct sctp_init *init;
289 	struct sctp_association *asoc;
290 	struct sctp_nets *lnet;
291 	unsigned int i;
292 
293 	init = &cp->init;
294 	asoc = &stcb->asoc;
295 	/* save off parameters */
296 	asoc->peer_vtag = ntohl(init->initiate_tag);
297 	asoc->peers_rwnd = ntohl(init->a_rwnd);
298 	/* init tsn's */
299 	asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
300 
301 	if (!TAILQ_EMPTY(&asoc->nets)) {
302 		/* update any ssthresh's that may have a default */
303 		TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
304 			lnet->ssthresh = asoc->peers_rwnd;
305 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
306 				sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
307 			}
308 
309 		}
310 	}
311 	SCTP_TCB_SEND_LOCK(stcb);
312 	if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
313 		unsigned int newcnt;
314 		struct sctp_stream_out *outs;
315 		struct sctp_stream_queue_pending *sp, *nsp;
316 		struct sctp_tmit_chunk *chk, *nchk;
317 
318 		/* abandon the upper streams */
319 		newcnt = ntohs(init->num_inbound_streams);
320 		TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
321 			if (chk->rec.data.sid >= newcnt) {
322 				TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
323 				asoc->send_queue_cnt--;
324 				if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
325 					asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
326 #ifdef INVARIANTS
327 				} else {
328 					panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
329 #endif
330 				}
331 				if (chk->data != NULL) {
332 					sctp_free_bufspace(stcb, asoc, chk, 1);
333 					sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
334 					    0, chk, SCTP_SO_NOT_LOCKED);
335 					if (chk->data) {
336 						sctp_m_freem(chk->data);
337 						chk->data = NULL;
338 					}
339 				}
340 				sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
341 				/* sa_ignore FREED_MEMORY */
342 			}
343 		}
344 		if (asoc->strmout) {
345 			for (i = newcnt; i < asoc->pre_open_streams; i++) {
346 				outs = &asoc->strmout[i];
347 				TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
348 					atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
349 					TAILQ_REMOVE(&outs->outqueue, sp, next);
350 					stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, 1);
351 					sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
352 					    stcb, 0, sp, SCTP_SO_NOT_LOCKED);
353 					if (sp->data) {
354 						sctp_m_freem(sp->data);
355 						sp->data = NULL;
356 					}
357 					if (sp->net) {
358 						sctp_free_remote_addr(sp->net);
359 						sp->net = NULL;
360 					}
361 					/* Free the chunk */
362 					sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED);
363 					/* sa_ignore FREED_MEMORY */
364 				}
365 				outs->state = SCTP_STREAM_CLOSED;
366 			}
367 		}
368 		/* cut back the count */
369 		asoc->pre_open_streams = newcnt;
370 	}
371 	SCTP_TCB_SEND_UNLOCK(stcb);
372 	asoc->streamoutcnt = asoc->pre_open_streams;
373 	if (asoc->strmout) {
374 		for (i = 0; i < asoc->streamoutcnt; i++) {
375 			asoc->strmout[i].state = SCTP_STREAM_OPEN;
376 		}
377 	}
378 	/* EY - nr_sack: initialize highest tsn in nr_mapping_array */
379 	asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
380 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
381 		sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
382 	}
383 	/* This is the next one we expect */
384 	asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
385 
386 	asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
387 	asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
388 
389 	asoc->advanced_peer_ack_point = asoc->last_acked_seq;
390 	/* open the requested streams */
391 
392 	if (asoc->strmin != NULL) {
393 		/* Free the old ones */
394 		for (i = 0; i < asoc->streamincnt; i++) {
395 			sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue);
396 			sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue);
397 		}
398 		SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
399 	}
400 	if (asoc->max_inbound_streams > ntohs(init->num_outbound_streams)) {
401 		asoc->streamincnt = ntohs(init->num_outbound_streams);
402 	} else {
403 		asoc->streamincnt = asoc->max_inbound_streams;
404 	}
405 	SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
406 	    sizeof(struct sctp_stream_in), SCTP_M_STRMI);
407 	if (asoc->strmin == NULL) {
408 		/* we didn't get memory for the streams! */
409 		SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
410 		return (-1);
411 	}
412 	for (i = 0; i < asoc->streamincnt; i++) {
413 		asoc->strmin[i].sid = i;
414 		asoc->strmin[i].last_mid_delivered = 0xffffffff;
415 		TAILQ_INIT(&asoc->strmin[i].inqueue);
416 		TAILQ_INIT(&asoc->strmin[i].uno_inqueue);
417 		asoc->strmin[i].pd_api_started = 0;
418 		asoc->strmin[i].delivery_started = 0;
419 	}
420 	/*
421 	 * load_address_from_init will put the addresses into the
422 	 * association when the COOKIE is processed or the INIT-ACK is
423 	 * processed. Both types of COOKIE's existing and new call this
424 	 * routine. It will remove addresses that are no longer in the
425 	 * association (for the restarting case where addresses are
426 	 * removed). Up front when the INIT arrives we will discard it if it
427 	 * is a restart and new addresses have been added.
428 	 */
429 	/* sa_ignore MEMLEAK */
430 	return (0);
431 }
432 
433 /*
434  * INIT-ACK message processing/consumption returns value < 0 on error
435  */
436 static int
437 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
438     struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
439     struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
440     struct sctp_nets *net, int *abort_no_unlock,
441     uint8_t mflowtype, uint32_t mflowid,
442     uint32_t vrf_id)
443 {
444 	struct sctp_association *asoc;
445 	struct mbuf *op_err;
446 	int retval, abort_flag;
447 	uint32_t initack_limit;
448 	int nat_friendly = 0;
449 
450 	/* First verify that we have no illegal param's */
451 	abort_flag = 0;
452 
453 	op_err = sctp_arethere_unrecognized_parameters(m,
454 	    (offset + sizeof(struct sctp_init_chunk)),
455 	    &abort_flag, (struct sctp_chunkhdr *)cp, &nat_friendly);
456 	if (abort_flag) {
457 		/* Send an abort and notify peer */
458 		sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
459 		*abort_no_unlock = 1;
460 		return (-1);
461 	}
462 	asoc = &stcb->asoc;
463 	asoc->peer_supports_nat = (uint8_t)nat_friendly;
464 	/* process the peer's parameters in the INIT-ACK */
465 	retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb);
466 	if (retval < 0) {
467 		return (retval);
468 	}
469 	initack_limit = offset + ntohs(cp->ch.chunk_length);
470 	/* load all addresses */
471 	if ((retval = sctp_load_addresses_from_init(stcb, m,
472 	    (offset + sizeof(struct sctp_init_chunk)), initack_limit,
473 	    src, dst, NULL, stcb->asoc.port))) {
474 		op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
475 		    "Problem with address parameters");
476 		SCTPDBG(SCTP_DEBUG_INPUT1,
477 		    "Load addresses from INIT causes an abort %d\n",
478 		    retval);
479 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
480 		    src, dst, sh, op_err,
481 		    mflowtype, mflowid,
482 		    vrf_id, net->port);
483 		*abort_no_unlock = 1;
484 		return (-1);
485 	}
486 	/* if the peer doesn't support asconf, flush the asconf queue */
487 	if (asoc->asconf_supported == 0) {
488 		struct sctp_asconf_addr *param, *nparam;
489 
490 		TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
491 			TAILQ_REMOVE(&asoc->asconf_queue, param, next);
492 			SCTP_FREE(param, SCTP_M_ASC_ADDR);
493 		}
494 	}
495 
496 	stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
497 	    stcb->asoc.local_hmacs);
498 	if (op_err) {
499 		sctp_queue_op_err(stcb, op_err);
500 		/* queuing will steal away the mbuf chain to the out queue */
501 		op_err = NULL;
502 	}
503 	/* extract the cookie and queue it to "echo" it back... */
504 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
505 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
506 		    stcb->asoc.overall_error_count,
507 		    0,
508 		    SCTP_FROM_SCTP_INPUT,
509 		    __LINE__);
510 	}
511 	stcb->asoc.overall_error_count = 0;
512 	net->error_count = 0;
513 
514 	/*
515 	 * Cancel the INIT timer, We do this first before queueing the
516 	 * cookie. We always cancel at the primary to assue that we are
517 	 * canceling the timer started by the INIT which always goes to the
518 	 * primary.
519 	 */
520 	sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
521 	    asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
522 
523 	/* calculate the RTO */
524 	net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered,
525 	    SCTP_RTT_FROM_NON_DATA);
526 	retval = sctp_send_cookie_echo(m, offset, stcb, net);
527 	if (retval < 0) {
528 		/*
529 		 * No cookie, we probably should send a op error. But in any
530 		 * case if there is no cookie in the INIT-ACK, we can
531 		 * abandon the peer, its broke.
532 		 */
533 		if (retval == -3) {
534 			uint16_t len;
535 
536 			len = (uint16_t)(sizeof(struct sctp_error_missing_param) + sizeof(uint16_t));
537 			/* We abort with an error of missing mandatory param */
538 			op_err = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
539 			if (op_err != NULL) {
540 				struct sctp_error_missing_param *cause;
541 
542 				SCTP_BUF_LEN(op_err) = len;
543 				cause = mtod(op_err, struct sctp_error_missing_param *);
544 				/* Subtract the reserved param */
545 				cause->cause.code = htons(SCTP_CAUSE_MISSING_PARAM);
546 				cause->cause.length = htons(len);
547 				cause->num_missing_params = htonl(1);
548 				cause->type[0] = htons(SCTP_STATE_COOKIE);
549 			}
550 			sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
551 			    src, dst, sh, op_err,
552 			    mflowtype, mflowid,
553 			    vrf_id, net->port);
554 			*abort_no_unlock = 1;
555 		}
556 		return (retval);
557 	}
558 
559 	return (0);
560 }
561 
562 static void
563 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
564     struct sctp_tcb *stcb, struct sctp_nets *net)
565 {
566 	union sctp_sockstore store;
567 	struct sctp_nets *r_net, *f_net;
568 	struct timeval tv;
569 	int req_prim = 0;
570 	uint16_t old_error_counter;
571 
572 	if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
573 		/* Invalid length */
574 		return;
575 	}
576 
577 	memset(&store, 0, sizeof(store));
578 	switch (cp->heartbeat.hb_info.addr_family) {
579 #ifdef INET
580 	case AF_INET:
581 		if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
582 			store.sin.sin_family = cp->heartbeat.hb_info.addr_family;
583 			store.sin.sin_len = cp->heartbeat.hb_info.addr_len;
584 			store.sin.sin_port = stcb->rport;
585 			memcpy(&store.sin.sin_addr, cp->heartbeat.hb_info.address,
586 			    sizeof(store.sin.sin_addr));
587 		} else {
588 			return;
589 		}
590 		break;
591 #endif
592 #ifdef INET6
593 	case AF_INET6:
594 		if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
595 			store.sin6.sin6_family = cp->heartbeat.hb_info.addr_family;
596 			store.sin6.sin6_len = cp->heartbeat.hb_info.addr_len;
597 			store.sin6.sin6_port = stcb->rport;
598 			memcpy(&store.sin6.sin6_addr, cp->heartbeat.hb_info.address, sizeof(struct in6_addr));
599 		} else {
600 			return;
601 		}
602 		break;
603 #endif
604 	default:
605 		return;
606 	}
607 	r_net = sctp_findnet(stcb, &store.sa);
608 	if (r_net == NULL) {
609 		SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
610 		return;
611 	}
612 	if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
613 	    (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
614 	    (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
615 		/*
616 		 * If the its a HB and it's random value is correct when can
617 		 * confirm the destination.
618 		 */
619 		r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
620 		if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
621 			stcb->asoc.primary_destination = r_net;
622 			r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
623 			f_net = TAILQ_FIRST(&stcb->asoc.nets);
624 			if (f_net != r_net) {
625 				/*
626 				 * first one on the list is NOT the primary
627 				 * sctp_cmpaddr() is much more efficient if
628 				 * the primary is the first on the list,
629 				 * make it so.
630 				 */
631 				TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
632 				TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
633 			}
634 			req_prim = 1;
635 		}
636 		sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
637 		    stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
638 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb,
639 		    r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
640 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
641 	}
642 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
643 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
644 		    stcb->asoc.overall_error_count,
645 		    0,
646 		    SCTP_FROM_SCTP_INPUT,
647 		    __LINE__);
648 	}
649 	stcb->asoc.overall_error_count = 0;
650 	old_error_counter = r_net->error_count;
651 	r_net->error_count = 0;
652 	r_net->hb_responded = 1;
653 	tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
654 	tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
655 	/* Now lets do a RTO with this */
656 	r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv,
657 	    SCTP_RTT_FROM_NON_DATA);
658 	if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) {
659 		r_net->dest_state |= SCTP_ADDR_REACHABLE;
660 		sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
661 		    0, (void *)r_net, SCTP_SO_NOT_LOCKED);
662 	}
663 	if (r_net->dest_state & SCTP_ADDR_PF) {
664 		r_net->dest_state &= ~SCTP_ADDR_PF;
665 		stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
666 	}
667 	if (old_error_counter > 0) {
668 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
669 		    stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_5);
670 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
671 	}
672 	if (r_net == stcb->asoc.primary_destination) {
673 		if (stcb->asoc.alternate) {
674 			/* release the alternate, primary is good */
675 			sctp_free_remote_addr(stcb->asoc.alternate);
676 			stcb->asoc.alternate = NULL;
677 		}
678 	}
679 	/* Mobility adaptation */
680 	if (req_prim) {
681 		if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
682 		    SCTP_MOBILITY_BASE) ||
683 		    sctp_is_mobility_feature_on(stcb->sctp_ep,
684 		    SCTP_MOBILITY_FASTHANDOFF)) &&
685 		    sctp_is_mobility_feature_on(stcb->sctp_ep,
686 		    SCTP_MOBILITY_PRIM_DELETED)) {
687 
688 			sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED,
689 			    stcb->sctp_ep, stcb, NULL,
690 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
691 			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
692 			    SCTP_MOBILITY_FASTHANDOFF)) {
693 				sctp_assoc_immediate_retrans(stcb,
694 				    stcb->asoc.primary_destination);
695 			}
696 			if (sctp_is_mobility_feature_on(stcb->sctp_ep,
697 			    SCTP_MOBILITY_BASE)) {
698 				sctp_move_chunks_from_net(stcb,
699 				    stcb->asoc.deleted_primary);
700 			}
701 			sctp_delete_prim_timer(stcb->sctp_ep, stcb,
702 			    stcb->asoc.deleted_primary);
703 		}
704 	}
705 }
706 
707 static int
708 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
709 {
710 	/*
711 	 * return 0 means we want you to proceed with the abort non-zero
712 	 * means no abort processing
713 	 */
714 	struct sctpasochead *head;
715 
716 	if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) ||
717 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
718 		atomic_add_int(&stcb->asoc.refcnt, 1);
719 		SCTP_TCB_UNLOCK(stcb);
720 		SCTP_INP_INFO_WLOCK();
721 		SCTP_TCB_LOCK(stcb);
722 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
723 	}
724 	if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
725 		/* generate a new vtag and send init */
726 		LIST_REMOVE(stcb, sctp_asocs);
727 		stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
728 		head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
729 		/*
730 		 * put it in the bucket in the vtag hash of assoc's for the
731 		 * system
732 		 */
733 		LIST_INSERT_HEAD(head, stcb, sctp_asocs);
734 		sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
735 		SCTP_INP_INFO_WUNLOCK();
736 		return (1);
737 	}
738 	if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED) {
739 		/*
740 		 * treat like a case where the cookie expired i.e.: - dump
741 		 * current cookie. - generate a new vtag. - resend init.
742 		 */
743 		/* generate a new vtag and send init */
744 		LIST_REMOVE(stcb, sctp_asocs);
745 		stcb->asoc.state &= ~SCTP_STATE_COOKIE_ECHOED;
746 		stcb->asoc.state |= SCTP_STATE_COOKIE_WAIT;
747 		sctp_stop_all_cookie_timers(stcb);
748 		sctp_toss_old_cookies(stcb, &stcb->asoc);
749 		stcb->asoc.my_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
750 		head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
751 		/*
752 		 * put it in the bucket in the vtag hash of assoc's for the
753 		 * system
754 		 */
755 		LIST_INSERT_HEAD(head, stcb, sctp_asocs);
756 		sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
757 		SCTP_INP_INFO_WUNLOCK();
758 		return (1);
759 	}
760 	return (0);
761 }
762 
763 static int
764 sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
765     struct sctp_nets *net)
766 {
767 	/*
768 	 * return 0 means we want you to proceed with the abort non-zero
769 	 * means no abort processing
770 	 */
771 	if (stcb->asoc.auth_supported == 0) {
772 		SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
773 		return (0);
774 	}
775 	sctp_asconf_send_nat_state_update(stcb, net);
776 	return (1);
777 }
778 
779 
780 /* Returns 1 if the stcb was aborted, 0 otherwise */
781 static int
782 sctp_handle_abort(struct sctp_abort_chunk *abort,
783     struct sctp_tcb *stcb, struct sctp_nets *net)
784 {
785 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
786 	struct socket *so;
787 #endif
788 	uint16_t len;
789 	uint16_t error;
790 
791 	SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
792 	if (stcb == NULL)
793 		return (0);
794 
795 	len = ntohs(abort->ch.chunk_length);
796 	if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_error_cause)) {
797 		/*
798 		 * Need to check the cause codes for our two magic nat
799 		 * aborts which don't kill the assoc necessarily.
800 		 */
801 		struct sctp_error_cause *cause;
802 
803 		cause = (struct sctp_error_cause *)(abort + 1);
804 		error = ntohs(cause->code);
805 		if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) {
806 			SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
807 			    abort->ch.chunk_flags);
808 			if (sctp_handle_nat_colliding_state(stcb)) {
809 				return (0);
810 			}
811 		} else if (error == SCTP_CAUSE_NAT_MISSING_STATE) {
812 			SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
813 			    abort->ch.chunk_flags);
814 			if (sctp_handle_nat_missing_state(stcb, net)) {
815 				return (0);
816 			}
817 		}
818 	} else {
819 		error = 0;
820 	}
821 	/* stop any receive timers */
822 	sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net,
823 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_7);
824 	/* notify user of the abort and clean up... */
825 	sctp_abort_notification(stcb, 1, error, abort, SCTP_SO_NOT_LOCKED);
826 	/* free the tcb */
827 	SCTP_STAT_INCR_COUNTER32(sctps_aborted);
828 	if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
829 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
830 		SCTP_STAT_DECR_GAUGE32(sctps_currestab);
831 	}
832 #ifdef SCTP_ASOCLOG_OF_TSNS
833 	sctp_print_out_track_log(stcb);
834 #endif
835 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
836 	so = SCTP_INP_SO(stcb->sctp_ep);
837 	atomic_add_int(&stcb->asoc.refcnt, 1);
838 	SCTP_TCB_UNLOCK(stcb);
839 	SCTP_SOCKET_LOCK(so, 1);
840 	SCTP_TCB_LOCK(stcb);
841 	atomic_subtract_int(&stcb->asoc.refcnt, 1);
842 #endif
843 	stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
844 	(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
845 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
846 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
847 	SCTP_SOCKET_UNLOCK(so, 1);
848 #endif
849 	SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
850 	return (1);
851 }
852 
853 static void
854 sctp_start_net_timers(struct sctp_tcb *stcb)
855 {
856 	uint32_t cnt_hb_sent;
857 	struct sctp_nets *net;
858 
859 	cnt_hb_sent = 0;
860 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
861 		/*
862 		 * For each network start: 1) A pmtu timer. 2) A HB timer 3)
863 		 * If the dest in unconfirmed send a hb as well if under
864 		 * max_hb_burst have been sent.
865 		 */
866 		sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
867 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
868 		if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
869 		    (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
870 			sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
871 			cnt_hb_sent++;
872 		}
873 	}
874 	if (cnt_hb_sent) {
875 		sctp_chunk_output(stcb->sctp_ep, stcb,
876 		    SCTP_OUTPUT_FROM_COOKIE_ACK,
877 		    SCTP_SO_NOT_LOCKED);
878 	}
879 }
880 
881 
882 static void
883 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
884     struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
885 {
886 	struct sctp_association *asoc;
887 	int some_on_streamwheel;
888 	int old_state;
889 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
890 	struct socket *so;
891 #endif
892 
893 	SCTPDBG(SCTP_DEBUG_INPUT2,
894 	    "sctp_handle_shutdown: handling SHUTDOWN\n");
895 	if (stcb == NULL)
896 		return;
897 	asoc = &stcb->asoc;
898 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
899 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
900 		return;
901 	}
902 	if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
903 		/* Shutdown NOT the expected size */
904 		return;
905 	}
906 	old_state = SCTP_GET_STATE(asoc);
907 	sctp_update_acked(stcb, cp, abort_flag);
908 	if (*abort_flag) {
909 		return;
910 	}
911 	if (asoc->control_pdapi) {
912 		/*
913 		 * With a normal shutdown we assume the end of last record.
914 		 */
915 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
916 		if (asoc->control_pdapi->on_strm_q) {
917 			struct sctp_stream_in *strm;
918 
919 			strm = &asoc->strmin[asoc->control_pdapi->sinfo_stream];
920 			if (asoc->control_pdapi->on_strm_q == SCTP_ON_UNORDERED) {
921 				/* Unordered */
922 				TAILQ_REMOVE(&strm->uno_inqueue, asoc->control_pdapi, next_instrm);
923 				asoc->control_pdapi->on_strm_q = 0;
924 			} else if (asoc->control_pdapi->on_strm_q == SCTP_ON_ORDERED) {
925 				/* Ordered */
926 				TAILQ_REMOVE(&strm->inqueue, asoc->control_pdapi, next_instrm);
927 				asoc->control_pdapi->on_strm_q = 0;
928 #ifdef INVARIANTS
929 			} else {
930 				panic("Unknown state on ctrl:%p on_strm_q:%d",
931 				    asoc->control_pdapi,
932 				    asoc->control_pdapi->on_strm_q);
933 #endif
934 			}
935 		}
936 		asoc->control_pdapi->end_added = 1;
937 		asoc->control_pdapi->pdapi_aborted = 1;
938 		asoc->control_pdapi = NULL;
939 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
940 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
941 		so = SCTP_INP_SO(stcb->sctp_ep);
942 		atomic_add_int(&stcb->asoc.refcnt, 1);
943 		SCTP_TCB_UNLOCK(stcb);
944 		SCTP_SOCKET_LOCK(so, 1);
945 		SCTP_TCB_LOCK(stcb);
946 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
947 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
948 			/* assoc was freed while we were unlocked */
949 			SCTP_SOCKET_UNLOCK(so, 1);
950 			return;
951 		}
952 #endif
953 		if (stcb->sctp_socket) {
954 			sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
955 		}
956 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
957 		SCTP_SOCKET_UNLOCK(so, 1);
958 #endif
959 	}
960 	/* goto SHUTDOWN_RECEIVED state to block new requests */
961 	if (stcb->sctp_socket) {
962 		if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
963 		    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
964 		    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
965 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_RECEIVED);
966 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
967 			/*
968 			 * notify upper layer that peer has initiated a
969 			 * shutdown
970 			 */
971 			sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
972 
973 			/* reset time */
974 			(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
975 		}
976 	}
977 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
978 		/*
979 		 * stop the shutdown timer, since we WILL move to
980 		 * SHUTDOWN-ACK-SENT.
981 		 */
982 		sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
983 		    net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
984 	}
985 	/* Now is there unsent data on a stream somewhere? */
986 	some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
987 
988 	if (!TAILQ_EMPTY(&asoc->send_queue) ||
989 	    !TAILQ_EMPTY(&asoc->sent_queue) ||
990 	    some_on_streamwheel) {
991 		/* By returning we will push more data out */
992 		return;
993 	} else {
994 		/* no outstanding data to send, so move on... */
995 		/* send SHUTDOWN-ACK */
996 		/* move to SHUTDOWN-ACK-SENT state */
997 		if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
998 		    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
999 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1000 		}
1001 		SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
1002 		if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
1003 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
1004 			sctp_stop_timers_for_shutdown(stcb);
1005 			sctp_send_shutdown_ack(stcb, net);
1006 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
1007 			    stcb->sctp_ep, stcb, net);
1008 		} else if (old_state == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1009 			sctp_send_shutdown_ack(stcb, net);
1010 		}
1011 	}
1012 }
1013 
1014 static void
1015 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
1016     struct sctp_tcb *stcb,
1017     struct sctp_nets *net)
1018 {
1019 	struct sctp_association *asoc;
1020 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1021 	struct socket *so;
1022 
1023 	so = SCTP_INP_SO(stcb->sctp_ep);
1024 #endif
1025 	SCTPDBG(SCTP_DEBUG_INPUT2,
1026 	    "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
1027 	if (stcb == NULL)
1028 		return;
1029 
1030 	asoc = &stcb->asoc;
1031 	/* process according to association state */
1032 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
1033 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
1034 		/* unexpected SHUTDOWN-ACK... do OOTB handling... */
1035 		sctp_send_shutdown_complete(stcb, net, 1);
1036 		SCTP_TCB_UNLOCK(stcb);
1037 		return;
1038 	}
1039 	if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
1040 	    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
1041 		/* unexpected SHUTDOWN-ACK... so ignore... */
1042 		SCTP_TCB_UNLOCK(stcb);
1043 		return;
1044 	}
1045 	if (asoc->control_pdapi) {
1046 		/*
1047 		 * With a normal shutdown we assume the end of last record.
1048 		 */
1049 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
1050 		asoc->control_pdapi->end_added = 1;
1051 		asoc->control_pdapi->pdapi_aborted = 1;
1052 		asoc->control_pdapi = NULL;
1053 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
1054 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1055 		atomic_add_int(&stcb->asoc.refcnt, 1);
1056 		SCTP_TCB_UNLOCK(stcb);
1057 		SCTP_SOCKET_LOCK(so, 1);
1058 		SCTP_TCB_LOCK(stcb);
1059 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
1060 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1061 			/* assoc was freed while we were unlocked */
1062 			SCTP_SOCKET_UNLOCK(so, 1);
1063 			return;
1064 		}
1065 #endif
1066 		sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1067 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1068 		SCTP_SOCKET_UNLOCK(so, 1);
1069 #endif
1070 	}
1071 #ifdef INVARIANTS
1072 	if (!TAILQ_EMPTY(&asoc->send_queue) ||
1073 	    !TAILQ_EMPTY(&asoc->sent_queue) ||
1074 	    sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
1075 		panic("Queues are not empty when handling SHUTDOWN-ACK");
1076 	}
1077 #endif
1078 	/* stop the timer */
1079 	sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net,
1080 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
1081 	/* send SHUTDOWN-COMPLETE */
1082 	sctp_send_shutdown_complete(stcb, net, 0);
1083 	/* notify upper layer protocol */
1084 	if (stcb->sctp_socket) {
1085 		if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1086 		    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1087 			stcb->sctp_socket->so_snd.sb_cc = 0;
1088 		}
1089 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
1090 	}
1091 	SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
1092 	/* free the TCB but first save off the ep */
1093 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1094 	atomic_add_int(&stcb->asoc.refcnt, 1);
1095 	SCTP_TCB_UNLOCK(stcb);
1096 	SCTP_SOCKET_LOCK(so, 1);
1097 	SCTP_TCB_LOCK(stcb);
1098 	atomic_subtract_int(&stcb->asoc.refcnt, 1);
1099 #endif
1100 	(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1101 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1102 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1103 	SCTP_SOCKET_UNLOCK(so, 1);
1104 #endif
1105 }
1106 
1107 static void
1108 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, uint8_t chunk_type,
1109     struct sctp_nets *net)
1110 {
1111 	switch (chunk_type) {
1112 	case SCTP_ASCONF_ACK:
1113 	case SCTP_ASCONF:
1114 		sctp_asconf_cleanup(stcb, net);
1115 		break;
1116 	case SCTP_IFORWARD_CUM_TSN:
1117 	case SCTP_FORWARD_CUM_TSN:
1118 		stcb->asoc.prsctp_supported = 0;
1119 		break;
1120 	default:
1121 		SCTPDBG(SCTP_DEBUG_INPUT2,
1122 		    "Peer does not support chunk type %d (0x%x).\n",
1123 		    chunk_type, chunk_type);
1124 		break;
1125 	}
1126 }
1127 
1128 /*
1129  * Skip past the param header and then we will find the param that caused the
1130  * problem.  There are a number of param's in a ASCONF OR the prsctp param
1131  * these will turn of specific features.
1132  * XXX: Is this the right thing to do?
1133  */
1134 static void
1135 sctp_process_unrecog_param(struct sctp_tcb *stcb, uint16_t parameter_type)
1136 {
1137 	switch (parameter_type) {
1138 		/* pr-sctp draft */
1139 	case SCTP_PRSCTP_SUPPORTED:
1140 		stcb->asoc.prsctp_supported = 0;
1141 		break;
1142 	case SCTP_SUPPORTED_CHUNK_EXT:
1143 		break;
1144 		/* draft-ietf-tsvwg-addip-sctp */
1145 	case SCTP_HAS_NAT_SUPPORT:
1146 		stcb->asoc.peer_supports_nat = 0;
1147 		break;
1148 	case SCTP_ADD_IP_ADDRESS:
1149 	case SCTP_DEL_IP_ADDRESS:
1150 	case SCTP_SET_PRIM_ADDR:
1151 		stcb->asoc.asconf_supported = 0;
1152 		break;
1153 	case SCTP_SUCCESS_REPORT:
1154 	case SCTP_ERROR_CAUSE_IND:
1155 		SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1156 		SCTPDBG(SCTP_DEBUG_INPUT2,
1157 		    "Turning off ASCONF to this strange peer\n");
1158 		stcb->asoc.asconf_supported = 0;
1159 		break;
1160 	default:
1161 		SCTPDBG(SCTP_DEBUG_INPUT2,
1162 		    "Peer does not support param type %d (0x%x)??\n",
1163 		    parameter_type, parameter_type);
1164 		break;
1165 	}
1166 }
1167 
1168 static int
1169 sctp_handle_error(struct sctp_chunkhdr *ch,
1170     struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
1171 {
1172 	struct sctp_error_cause *cause;
1173 	struct sctp_association *asoc;
1174 	uint32_t remaining_length, adjust;
1175 	uint16_t code, cause_code, cause_length;
1176 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1177 	struct socket *so;
1178 #endif
1179 
1180 	/* parse through all of the errors and process */
1181 	asoc = &stcb->asoc;
1182 	cause = (struct sctp_error_cause *)((caddr_t)ch +
1183 	    sizeof(struct sctp_chunkhdr));
1184 	remaining_length = ntohs(ch->chunk_length);
1185 	if (remaining_length > limit) {
1186 		remaining_length = limit;
1187 	}
1188 	if (remaining_length >= sizeof(struct sctp_chunkhdr)) {
1189 		remaining_length -= sizeof(struct sctp_chunkhdr);
1190 	} else {
1191 		remaining_length = 0;
1192 	}
1193 	code = 0;
1194 	while (remaining_length >= sizeof(struct sctp_error_cause)) {
1195 		/* Process an Error Cause */
1196 		cause_code = ntohs(cause->code);
1197 		cause_length = ntohs(cause->length);
1198 		if ((cause_length > remaining_length) || (cause_length == 0)) {
1199 			/* Invalid cause length, possibly due to truncation. */
1200 			SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in cause - bytes left: %u cause length: %u\n",
1201 			    remaining_length, cause_length);
1202 			return (0);
1203 		}
1204 		if (code == 0) {
1205 			/* report the first error cause */
1206 			code = cause_code;
1207 		}
1208 		switch (cause_code) {
1209 		case SCTP_CAUSE_INVALID_STREAM:
1210 		case SCTP_CAUSE_MISSING_PARAM:
1211 		case SCTP_CAUSE_INVALID_PARAM:
1212 		case SCTP_CAUSE_NO_USER_DATA:
1213 			SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %u back? We have a bug :/ (or do they?)\n",
1214 			    cause_code);
1215 			break;
1216 		case SCTP_CAUSE_NAT_COLLIDING_STATE:
1217 			SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags: %x\n",
1218 			    ch->chunk_flags);
1219 			if (sctp_handle_nat_colliding_state(stcb)) {
1220 				return (0);
1221 			}
1222 			break;
1223 		case SCTP_CAUSE_NAT_MISSING_STATE:
1224 			SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags: %x\n",
1225 			    ch->chunk_flags);
1226 			if (sctp_handle_nat_missing_state(stcb, net)) {
1227 				return (0);
1228 			}
1229 			break;
1230 		case SCTP_CAUSE_STALE_COOKIE:
1231 			/*
1232 			 * We only act if we have echoed a cookie and are
1233 			 * waiting.
1234 			 */
1235 			if ((cause_length >= sizeof(struct sctp_error_stale_cookie)) &&
1236 			    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
1237 				struct sctp_error_stale_cookie *stale_cookie;
1238 
1239 				stale_cookie = (struct sctp_error_stale_cookie *)cause;
1240 				asoc->cookie_preserve_req = ntohl(stale_cookie->stale_time);
1241 				/* Double it to be more robust on RTX */
1242 				if (asoc->cookie_preserve_req <= UINT32_MAX / 2) {
1243 					asoc->cookie_preserve_req *= 2;
1244 				} else {
1245 					asoc->cookie_preserve_req = UINT32_MAX;
1246 				}
1247 				asoc->stale_cookie_count++;
1248 				if (asoc->stale_cookie_count >
1249 				    asoc->max_init_times) {
1250 					sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
1251 					/* now free the asoc */
1252 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1253 					so = SCTP_INP_SO(stcb->sctp_ep);
1254 					atomic_add_int(&stcb->asoc.refcnt, 1);
1255 					SCTP_TCB_UNLOCK(stcb);
1256 					SCTP_SOCKET_LOCK(so, 1);
1257 					SCTP_TCB_LOCK(stcb);
1258 					atomic_subtract_int(&stcb->asoc.refcnt, 1);
1259 #endif
1260 					(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1261 					    SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1262 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1263 					SCTP_SOCKET_UNLOCK(so, 1);
1264 #endif
1265 					return (-1);
1266 				}
1267 				/* blast back to INIT state */
1268 				sctp_toss_old_cookies(stcb, &stcb->asoc);
1269 				asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
1270 				asoc->state |= SCTP_STATE_COOKIE_WAIT;
1271 				sctp_stop_all_cookie_timers(stcb);
1272 				sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1273 			}
1274 			break;
1275 		case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1276 			/*
1277 			 * Nothing we can do here, we don't do hostname
1278 			 * addresses so if the peer does not like my IPv6
1279 			 * (or IPv4 for that matter) it does not matter. If
1280 			 * they don't support that type of address, they can
1281 			 * NOT possibly get that packet type... i.e. with no
1282 			 * IPv6 you can't receive a IPv6 packet. so we can
1283 			 * safely ignore this one. If we ever added support
1284 			 * for HOSTNAME Addresses, then we would need to do
1285 			 * something here.
1286 			 */
1287 			break;
1288 		case SCTP_CAUSE_UNRECOG_CHUNK:
1289 			if (cause_length >= sizeof(struct sctp_error_unrecognized_chunk)) {
1290 				struct sctp_error_unrecognized_chunk *unrec_chunk;
1291 
1292 				unrec_chunk = (struct sctp_error_unrecognized_chunk *)cause;
1293 				sctp_process_unrecog_chunk(stcb, unrec_chunk->ch.chunk_type, net);
1294 			}
1295 			break;
1296 		case SCTP_CAUSE_UNRECOG_PARAM:
1297 			/* XXX: We only consider the first parameter */
1298 			if (cause_length >= sizeof(struct sctp_error_cause) + sizeof(struct sctp_paramhdr)) {
1299 				struct sctp_paramhdr *unrec_parameter;
1300 
1301 				unrec_parameter = (struct sctp_paramhdr *)(cause + 1);
1302 				sctp_process_unrecog_param(stcb, ntohs(unrec_parameter->param_type));
1303 			}
1304 			break;
1305 		case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1306 			/*
1307 			 * We ignore this since the timer will drive out a
1308 			 * new cookie anyway and there timer will drive us
1309 			 * to send a SHUTDOWN_COMPLETE. We can't send one
1310 			 * here since we don't have their tag.
1311 			 */
1312 			break;
1313 		case SCTP_CAUSE_DELETING_LAST_ADDR:
1314 		case SCTP_CAUSE_RESOURCE_SHORTAGE:
1315 		case SCTP_CAUSE_DELETING_SRC_ADDR:
1316 			/*
1317 			 * We should NOT get these here, but in a
1318 			 * ASCONF-ACK.
1319 			 */
1320 			SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a error cause with code %u.\n",
1321 			    cause_code);
1322 			break;
1323 		case SCTP_CAUSE_OUT_OF_RESC:
1324 			/*
1325 			 * And what, pray tell do we do with the fact that
1326 			 * the peer is out of resources? Not really sure we
1327 			 * could do anything but abort. I suspect this
1328 			 * should have came WITH an abort instead of in a
1329 			 * OP-ERROR.
1330 			 */
1331 			break;
1332 		default:
1333 			SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown code 0x%x\n",
1334 			    cause_code);
1335 			break;
1336 		}
1337 		adjust = SCTP_SIZE32(cause_length);
1338 		if (remaining_length >= adjust) {
1339 			remaining_length -= adjust;
1340 		} else {
1341 			remaining_length = 0;
1342 		}
1343 		cause = (struct sctp_error_cause *)((caddr_t)cause + adjust);
1344 	}
1345 	sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, code, ch, SCTP_SO_NOT_LOCKED);
1346 	return (0);
1347 }
1348 
1349 static int
1350 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1351     struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
1352     struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1353     struct sctp_nets *net, int *abort_no_unlock,
1354     uint8_t mflowtype, uint32_t mflowid,
1355     uint32_t vrf_id)
1356 {
1357 	struct sctp_init_ack *init_ack;
1358 	struct mbuf *op_err;
1359 
1360 	SCTPDBG(SCTP_DEBUG_INPUT2,
1361 	    "sctp_handle_init_ack: handling INIT-ACK\n");
1362 
1363 	if (stcb == NULL) {
1364 		SCTPDBG(SCTP_DEBUG_INPUT2,
1365 		    "sctp_handle_init_ack: TCB is null\n");
1366 		return (-1);
1367 	}
1368 	if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1369 		/* Invalid length */
1370 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1371 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1372 		    src, dst, sh, op_err,
1373 		    mflowtype, mflowid,
1374 		    vrf_id, net->port);
1375 		*abort_no_unlock = 1;
1376 		return (-1);
1377 	}
1378 	init_ack = &cp->init;
1379 	/* validate parameters */
1380 	if (init_ack->initiate_tag == 0) {
1381 		/* protocol error... send an abort */
1382 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1383 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1384 		    src, dst, sh, op_err,
1385 		    mflowtype, mflowid,
1386 		    vrf_id, net->port);
1387 		*abort_no_unlock = 1;
1388 		return (-1);
1389 	}
1390 	if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1391 		/* protocol error... send an abort */
1392 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1393 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1394 		    src, dst, sh, op_err,
1395 		    mflowtype, mflowid,
1396 		    vrf_id, net->port);
1397 		*abort_no_unlock = 1;
1398 		return (-1);
1399 	}
1400 	if (init_ack->num_inbound_streams == 0) {
1401 		/* protocol error... send an abort */
1402 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1403 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1404 		    src, dst, sh, op_err,
1405 		    mflowtype, mflowid,
1406 		    vrf_id, net->port);
1407 		*abort_no_unlock = 1;
1408 		return (-1);
1409 	}
1410 	if (init_ack->num_outbound_streams == 0) {
1411 		/* protocol error... send an abort */
1412 		op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1413 		sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1414 		    src, dst, sh, op_err,
1415 		    mflowtype, mflowid,
1416 		    vrf_id, net->port);
1417 		*abort_no_unlock = 1;
1418 		return (-1);
1419 	}
1420 	/* process according to association state... */
1421 	switch (stcb->asoc.state & SCTP_STATE_MASK) {
1422 	case SCTP_STATE_COOKIE_WAIT:
1423 		/* this is the expected state for this chunk */
1424 		/* process the INIT-ACK parameters */
1425 		if (stcb->asoc.primary_destination->dest_state &
1426 		    SCTP_ADDR_UNCONFIRMED) {
1427 			/*
1428 			 * The primary is where we sent the INIT, we can
1429 			 * always consider it confirmed when the INIT-ACK is
1430 			 * returned. Do this before we load addresses
1431 			 * though.
1432 			 */
1433 			stcb->asoc.primary_destination->dest_state &=
1434 			    ~SCTP_ADDR_UNCONFIRMED;
1435 			sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1436 			    stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1437 		}
1438 		if (sctp_process_init_ack(m, iphlen, offset, src, dst, sh, cp, stcb,
1439 		    net, abort_no_unlock,
1440 		    mflowtype, mflowid,
1441 		    vrf_id) < 0) {
1442 			/* error in parsing parameters */
1443 			return (-1);
1444 		}
1445 		/* update our state */
1446 		SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1447 		SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_ECHOED);
1448 
1449 		/* reset the RTO calc */
1450 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1451 			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1452 			    stcb->asoc.overall_error_count,
1453 			    0,
1454 			    SCTP_FROM_SCTP_INPUT,
1455 			    __LINE__);
1456 		}
1457 		stcb->asoc.overall_error_count = 0;
1458 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1459 		/*
1460 		 * collapse the init timer back in case of a exponential
1461 		 * backoff
1462 		 */
1463 		sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1464 		    stcb, net);
1465 		/*
1466 		 * the send at the end of the inbound data processing will
1467 		 * cause the cookie to be sent
1468 		 */
1469 		break;
1470 	case SCTP_STATE_SHUTDOWN_SENT:
1471 		/* incorrect state... discard */
1472 		break;
1473 	case SCTP_STATE_COOKIE_ECHOED:
1474 		/* incorrect state... discard */
1475 		break;
1476 	case SCTP_STATE_OPEN:
1477 		/* incorrect state... discard */
1478 		break;
1479 	case SCTP_STATE_EMPTY:
1480 	case SCTP_STATE_INUSE:
1481 	default:
1482 		/* incorrect state... discard */
1483 		return (-1);
1484 		break;
1485 	}
1486 	SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1487 	return (0);
1488 }
1489 
1490 static struct sctp_tcb *
1491 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1492     struct sockaddr *src, struct sockaddr *dst,
1493     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1494     struct sctp_inpcb *inp, struct sctp_nets **netp,
1495     struct sockaddr *init_src, int *notification,
1496     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1497     uint8_t mflowtype, uint32_t mflowid,
1498     uint32_t vrf_id, uint16_t port);
1499 
1500 
1501 /*
1502  * handle a state cookie for an existing association m: input packet mbuf
1503  * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1504  * "split" mbuf and the cookie signature does not exist offset: offset into
1505  * mbuf to the cookie-echo chunk
1506  */
1507 static struct sctp_tcb *
1508 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1509     struct sockaddr *src, struct sockaddr *dst,
1510     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1511     struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
1512     struct sockaddr *init_src, int *notification,
1513     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1514     uint8_t mflowtype, uint32_t mflowid,
1515     uint32_t vrf_id, uint16_t port)
1516 {
1517 	struct sctp_association *asoc;
1518 	struct sctp_init_chunk *init_cp, init_buf;
1519 	struct sctp_init_ack_chunk *initack_cp, initack_buf;
1520 	struct sctp_nets *net;
1521 	struct mbuf *op_err;
1522 	struct timeval old;
1523 	int init_offset, initack_offset, i;
1524 	int retval;
1525 	int spec_flag = 0;
1526 	uint32_t how_indx;
1527 #if defined(SCTP_DETAILED_STR_STATS)
1528 	int j;
1529 #endif
1530 
1531 	net = *netp;
1532 	/* I know that the TCB is non-NULL from the caller */
1533 	asoc = &stcb->asoc;
1534 	for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1535 		if (asoc->cookie_how[how_indx] == 0)
1536 			break;
1537 	}
1538 	if (how_indx < sizeof(asoc->cookie_how)) {
1539 		asoc->cookie_how[how_indx] = 1;
1540 	}
1541 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1542 		/* SHUTDOWN came in after sending INIT-ACK */
1543 		sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1544 		op_err = sctp_generate_cause(SCTP_CAUSE_COOKIE_IN_SHUTDOWN, "");
1545 		sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
1546 		    mflowtype, mflowid, inp->fibnum,
1547 		    vrf_id, net->port);
1548 		if (how_indx < sizeof(asoc->cookie_how))
1549 			asoc->cookie_how[how_indx] = 2;
1550 		return (NULL);
1551 	}
1552 	/*
1553 	 * find and validate the INIT chunk in the cookie (peer's info) the
1554 	 * INIT should start after the cookie-echo header struct (chunk
1555 	 * header, state cookie header struct)
1556 	 */
1557 	init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1558 
1559 	init_cp = (struct sctp_init_chunk *)
1560 	    sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1561 	    (uint8_t *)&init_buf);
1562 	if (init_cp == NULL) {
1563 		/* could not pull a INIT chunk in cookie */
1564 		return (NULL);
1565 	}
1566 	if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1567 		return (NULL);
1568 	}
1569 	/*
1570 	 * find and validate the INIT-ACK chunk in the cookie (my info) the
1571 	 * INIT-ACK follows the INIT chunk
1572 	 */
1573 	initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1574 	initack_cp = (struct sctp_init_ack_chunk *)
1575 	    sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1576 	    (uint8_t *)&initack_buf);
1577 	if (initack_cp == NULL) {
1578 		/* could not pull INIT-ACK chunk in cookie */
1579 		return (NULL);
1580 	}
1581 	if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1582 		return (NULL);
1583 	}
1584 	if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1585 	    (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1586 		/*
1587 		 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1588 		 * to get into the OPEN state
1589 		 */
1590 		if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1591 			/*-
1592 			 * Opps, this means that we somehow generated two vtag's
1593 			 * the same. I.e. we did:
1594 			 *  Us               Peer
1595 			 *   <---INIT(tag=a)------
1596 			 *   ----INIT-ACK(tag=t)-->
1597 			 *   ----INIT(tag=t)------> *1
1598 			 *   <---INIT-ACK(tag=a)---
1599                          *   <----CE(tag=t)------------- *2
1600 			 *
1601 			 * At point *1 we should be generating a different
1602 			 * tag t'. Which means we would throw away the CE and send
1603 			 * ours instead. Basically this is case C (throw away side).
1604 			 */
1605 			if (how_indx < sizeof(asoc->cookie_how))
1606 				asoc->cookie_how[how_indx] = 17;
1607 			return (NULL);
1608 
1609 		}
1610 		switch (SCTP_GET_STATE(asoc)) {
1611 		case SCTP_STATE_COOKIE_WAIT:
1612 		case SCTP_STATE_COOKIE_ECHOED:
1613 			/*
1614 			 * INIT was sent but got a COOKIE_ECHO with the
1615 			 * correct tags... just accept it...but we must
1616 			 * process the init so that we can make sure we have
1617 			 * the right seq no's.
1618 			 */
1619 			/* First we must process the INIT !! */
1620 			retval = sctp_process_init(init_cp, stcb);
1621 			if (retval < 0) {
1622 				if (how_indx < sizeof(asoc->cookie_how))
1623 					asoc->cookie_how[how_indx] = 3;
1624 				return (NULL);
1625 			}
1626 			/* we have already processed the INIT so no problem */
1627 			sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp,
1628 			    stcb, net,
1629 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1630 			sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp,
1631 			    stcb, net,
1632 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1633 			/* update current state */
1634 			if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1635 				SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1636 			else
1637 				SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1638 
1639 			SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1640 			if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1641 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1642 				    stcb->sctp_ep, stcb, asoc->primary_destination);
1643 			}
1644 			SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1645 			sctp_stop_all_cookie_timers(stcb);
1646 			if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1647 			    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1648 			    (!SCTP_IS_LISTENING(inp))) {
1649 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1650 				struct socket *so;
1651 #endif
1652 				/*
1653 				 * Here is where collision would go if we
1654 				 * did a connect() and instead got a
1655 				 * init/init-ack/cookie done before the
1656 				 * init-ack came back..
1657 				 */
1658 				stcb->sctp_ep->sctp_flags |=
1659 				    SCTP_PCB_FLAGS_CONNECTED;
1660 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1661 				so = SCTP_INP_SO(stcb->sctp_ep);
1662 				atomic_add_int(&stcb->asoc.refcnt, 1);
1663 				SCTP_TCB_UNLOCK(stcb);
1664 				SCTP_SOCKET_LOCK(so, 1);
1665 				SCTP_TCB_LOCK(stcb);
1666 				atomic_add_int(&stcb->asoc.refcnt, -1);
1667 				if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1668 					SCTP_SOCKET_UNLOCK(so, 1);
1669 					return (NULL);
1670 				}
1671 #endif
1672 				soisconnected(stcb->sctp_socket);
1673 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1674 				SCTP_SOCKET_UNLOCK(so, 1);
1675 #endif
1676 			}
1677 			/* notify upper layer */
1678 			*notification = SCTP_NOTIFY_ASSOC_UP;
1679 			/*
1680 			 * since we did not send a HB make sure we don't
1681 			 * double things
1682 			 */
1683 			old.tv_sec = cookie->time_entered.tv_sec;
1684 			old.tv_usec = cookie->time_entered.tv_usec;
1685 			net->hb_responded = 1;
1686 			net->RTO = sctp_calculate_rto(stcb, asoc, net,
1687 			    &old,
1688 			    SCTP_RTT_FROM_NON_DATA);
1689 
1690 			if (stcb->asoc.sctp_autoclose_ticks &&
1691 			    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1692 				sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1693 				    inp, stcb, NULL);
1694 			}
1695 			break;
1696 		default:
1697 			/*
1698 			 * we're in the OPEN state (or beyond), so peer must
1699 			 * have simply lost the COOKIE-ACK
1700 			 */
1701 			break;
1702 		}		/* end switch */
1703 		sctp_stop_all_cookie_timers(stcb);
1704 		/*
1705 		 * We ignore the return code here.. not sure if we should
1706 		 * somehow abort.. but we do have an existing asoc. This
1707 		 * really should not fail.
1708 		 */
1709 		if (sctp_load_addresses_from_init(stcb, m,
1710 		    init_offset + sizeof(struct sctp_init_chunk),
1711 		    initack_offset, src, dst, init_src, stcb->asoc.port)) {
1712 			if (how_indx < sizeof(asoc->cookie_how))
1713 				asoc->cookie_how[how_indx] = 4;
1714 			return (NULL);
1715 		}
1716 		/* respond with a COOKIE-ACK */
1717 		sctp_toss_old_cookies(stcb, asoc);
1718 		sctp_send_cookie_ack(stcb);
1719 		if (how_indx < sizeof(asoc->cookie_how))
1720 			asoc->cookie_how[how_indx] = 5;
1721 		return (stcb);
1722 	}
1723 
1724 	if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1725 	    ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1726 	    cookie->tie_tag_my_vtag == 0 &&
1727 	    cookie->tie_tag_peer_vtag == 0) {
1728 		/*
1729 		 * case C in Section 5.2.4 Table 2: XMOO silently discard
1730 		 */
1731 		if (how_indx < sizeof(asoc->cookie_how))
1732 			asoc->cookie_how[how_indx] = 6;
1733 		return (NULL);
1734 	}
1735 	/*
1736 	 * If nat support, and the below and stcb is established, send back
1737 	 * a ABORT(colliding state) if we are established.
1738 	 */
1739 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) &&
1740 	    (asoc->peer_supports_nat) &&
1741 	    ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1742 	    ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1743 	    (asoc->peer_vtag == 0)))) {
1744 		/*
1745 		 * Special case - Peer's support nat. We may have two init's
1746 		 * that we gave out the same tag on since one was not
1747 		 * established.. i.e. we get INIT from host-1 behind the nat
1748 		 * and we respond tag-a, we get a INIT from host-2 behind
1749 		 * the nat and we get tag-a again. Then we bring up host-1
1750 		 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1751 		 * Now we have colliding state. We must send an abort here
1752 		 * with colliding state indication.
1753 		 */
1754 		op_err = sctp_generate_cause(SCTP_CAUSE_NAT_COLLIDING_STATE, "");
1755 		sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
1756 		    mflowtype, mflowid, inp->fibnum,
1757 		    vrf_id, port);
1758 		return (NULL);
1759 	}
1760 	if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1761 	    ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1762 	    (asoc->peer_vtag == 0))) {
1763 		/*
1764 		 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1765 		 * should be ok, re-accept peer info
1766 		 */
1767 		if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1768 			/*
1769 			 * Extension of case C. If we hit this, then the
1770 			 * random number generator returned the same vtag
1771 			 * when we first sent our INIT-ACK and when we later
1772 			 * sent our INIT. The side with the seq numbers that
1773 			 * are different will be the one that normnally
1774 			 * would have hit case C. This in effect "extends"
1775 			 * our vtags in this collision case to be 64 bits.
1776 			 * The same collision could occur aka you get both
1777 			 * vtag and seq number the same twice in a row.. but
1778 			 * is much less likely. If it did happen then we
1779 			 * would proceed through and bring up the assoc.. we
1780 			 * may end up with the wrong stream setup however..
1781 			 * which would be bad.. but there is no way to
1782 			 * tell.. until we send on a stream that does not
1783 			 * exist :-)
1784 			 */
1785 			if (how_indx < sizeof(asoc->cookie_how))
1786 				asoc->cookie_how[how_indx] = 7;
1787 
1788 			return (NULL);
1789 		}
1790 		if (how_indx < sizeof(asoc->cookie_how))
1791 			asoc->cookie_how[how_indx] = 8;
1792 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1793 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1794 		sctp_stop_all_cookie_timers(stcb);
1795 		/*
1796 		 * since we did not send a HB make sure we don't double
1797 		 * things
1798 		 */
1799 		net->hb_responded = 1;
1800 		if (stcb->asoc.sctp_autoclose_ticks &&
1801 		    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1802 			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1803 			    NULL);
1804 		}
1805 		asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1806 		asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1807 
1808 		if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1809 			/*
1810 			 * Ok the peer probably discarded our data (if we
1811 			 * echoed a cookie+data). So anything on the
1812 			 * sent_queue should be marked for retransmit, we
1813 			 * may not get something to kick us so it COULD
1814 			 * still take a timeout to move these.. but it can't
1815 			 * hurt to mark them.
1816 			 */
1817 			struct sctp_tmit_chunk *chk;
1818 
1819 			TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1820 				if (chk->sent < SCTP_DATAGRAM_RESEND) {
1821 					chk->sent = SCTP_DATAGRAM_RESEND;
1822 					sctp_flight_size_decrease(chk);
1823 					sctp_total_flight_decrease(stcb, chk);
1824 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1825 					spec_flag++;
1826 				}
1827 			}
1828 
1829 		}
1830 		/* process the INIT info (peer's info) */
1831 		retval = sctp_process_init(init_cp, stcb);
1832 		if (retval < 0) {
1833 			if (how_indx < sizeof(asoc->cookie_how))
1834 				asoc->cookie_how[how_indx] = 9;
1835 			return (NULL);
1836 		}
1837 		if (sctp_load_addresses_from_init(stcb, m,
1838 		    init_offset + sizeof(struct sctp_init_chunk),
1839 		    initack_offset, src, dst, init_src, stcb->asoc.port)) {
1840 			if (how_indx < sizeof(asoc->cookie_how))
1841 				asoc->cookie_how[how_indx] = 10;
1842 			return (NULL);
1843 		}
1844 		if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1845 		    (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1846 			*notification = SCTP_NOTIFY_ASSOC_UP;
1847 
1848 			if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1849 			    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1850 			    (!SCTP_IS_LISTENING(inp))) {
1851 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1852 				struct socket *so;
1853 #endif
1854 				stcb->sctp_ep->sctp_flags |=
1855 				    SCTP_PCB_FLAGS_CONNECTED;
1856 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1857 				so = SCTP_INP_SO(stcb->sctp_ep);
1858 				atomic_add_int(&stcb->asoc.refcnt, 1);
1859 				SCTP_TCB_UNLOCK(stcb);
1860 				SCTP_SOCKET_LOCK(so, 1);
1861 				SCTP_TCB_LOCK(stcb);
1862 				atomic_add_int(&stcb->asoc.refcnt, -1);
1863 				if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1864 					SCTP_SOCKET_UNLOCK(so, 1);
1865 					return (NULL);
1866 				}
1867 #endif
1868 				soisconnected(stcb->sctp_socket);
1869 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1870 				SCTP_SOCKET_UNLOCK(so, 1);
1871 #endif
1872 			}
1873 			if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)
1874 				SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1875 			else
1876 				SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1877 			SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1878 		} else if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1879 			SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1880 		} else {
1881 			SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1882 		}
1883 		SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1884 		if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1885 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1886 			    stcb->sctp_ep, stcb, asoc->primary_destination);
1887 		}
1888 		sctp_stop_all_cookie_timers(stcb);
1889 		sctp_toss_old_cookies(stcb, asoc);
1890 		sctp_send_cookie_ack(stcb);
1891 		if (spec_flag) {
1892 			/*
1893 			 * only if we have retrans set do we do this. What
1894 			 * this call does is get only the COOKIE-ACK out and
1895 			 * then when we return the normal call to
1896 			 * sctp_chunk_output will get the retrans out behind
1897 			 * this.
1898 			 */
1899 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
1900 		}
1901 		if (how_indx < sizeof(asoc->cookie_how))
1902 			asoc->cookie_how[how_indx] = 11;
1903 
1904 		return (stcb);
1905 	}
1906 	if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1907 	    ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1908 	    cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1909 	    cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1910 	    cookie->tie_tag_peer_vtag != 0) {
1911 		struct sctpasochead *head;
1912 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1913 		struct socket *so;
1914 #endif
1915 
1916 		if (asoc->peer_supports_nat) {
1917 			/*
1918 			 * This is a gross gross hack. Just call the
1919 			 * cookie_new code since we are allowing a duplicate
1920 			 * association. I hope this works...
1921 			 */
1922 			return (sctp_process_cookie_new(m, iphlen, offset, src, dst,
1923 			    sh, cookie, cookie_len,
1924 			    inp, netp, init_src, notification,
1925 			    auth_skipped, auth_offset, auth_len,
1926 			    mflowtype, mflowid,
1927 			    vrf_id, port));
1928 		}
1929 		/*
1930 		 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1931 		 */
1932 		/* temp code */
1933 		if (how_indx < sizeof(asoc->cookie_how))
1934 			asoc->cookie_how[how_indx] = 12;
1935 		sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net,
1936 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1937 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1938 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
1939 
1940 		/* notify upper layer */
1941 		*notification = SCTP_NOTIFY_ASSOC_RESTART;
1942 		atomic_add_int(&stcb->asoc.refcnt, 1);
1943 		if ((SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN) &&
1944 		    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1945 		    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
1946 			SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1947 		}
1948 		if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
1949 			SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1950 		} else if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1951 			SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1952 		}
1953 		if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1954 			SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1955 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1956 			    stcb->sctp_ep, stcb, asoc->primary_destination);
1957 
1958 		} else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1959 			/* move to OPEN state, if not in SHUTDOWN_SENT */
1960 			SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
1961 		}
1962 		asoc->pre_open_streams =
1963 		    ntohs(initack_cp->init.num_outbound_streams);
1964 		asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1965 		asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1966 		asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1967 
1968 		asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1969 
1970 		asoc->str_reset_seq_in = asoc->init_seq_number;
1971 
1972 		asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1973 		if (asoc->mapping_array) {
1974 			memset(asoc->mapping_array, 0,
1975 			    asoc->mapping_array_size);
1976 		}
1977 		if (asoc->nr_mapping_array) {
1978 			memset(asoc->nr_mapping_array, 0,
1979 			    asoc->mapping_array_size);
1980 		}
1981 		SCTP_TCB_UNLOCK(stcb);
1982 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1983 		so = SCTP_INP_SO(stcb->sctp_ep);
1984 		SCTP_SOCKET_LOCK(so, 1);
1985 #endif
1986 		SCTP_INP_INFO_WLOCK();
1987 		SCTP_INP_WLOCK(stcb->sctp_ep);
1988 		SCTP_TCB_LOCK(stcb);
1989 		atomic_add_int(&stcb->asoc.refcnt, -1);
1990 		/* send up all the data */
1991 		SCTP_TCB_SEND_LOCK(stcb);
1992 
1993 		sctp_report_all_outbound(stcb, 0, 1, SCTP_SO_LOCKED);
1994 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1995 			stcb->asoc.strmout[i].chunks_on_queues = 0;
1996 #if defined(SCTP_DETAILED_STR_STATS)
1997 			for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1998 				asoc->strmout[i].abandoned_sent[j] = 0;
1999 				asoc->strmout[i].abandoned_unsent[j] = 0;
2000 			}
2001 #else
2002 			asoc->strmout[i].abandoned_sent[0] = 0;
2003 			asoc->strmout[i].abandoned_unsent[0] = 0;
2004 #endif
2005 			stcb->asoc.strmout[i].sid = i;
2006 			stcb->asoc.strmout[i].next_mid_ordered = 0;
2007 			stcb->asoc.strmout[i].next_mid_unordered = 0;
2008 			stcb->asoc.strmout[i].last_msg_incomplete = 0;
2009 		}
2010 		/* process the INIT-ACK info (my info) */
2011 		asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2012 		asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2013 
2014 		/* pull from vtag hash */
2015 		LIST_REMOVE(stcb, sctp_asocs);
2016 		/* re-insert to new vtag position */
2017 		head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
2018 		    SCTP_BASE_INFO(hashasocmark))];
2019 		/*
2020 		 * put it in the bucket in the vtag hash of assoc's for the
2021 		 * system
2022 		 */
2023 		LIST_INSERT_HEAD(head, stcb, sctp_asocs);
2024 
2025 		SCTP_TCB_SEND_UNLOCK(stcb);
2026 		SCTP_INP_WUNLOCK(stcb->sctp_ep);
2027 		SCTP_INP_INFO_WUNLOCK();
2028 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2029 		SCTP_SOCKET_UNLOCK(so, 1);
2030 #endif
2031 		asoc->total_flight = 0;
2032 		asoc->total_flight_count = 0;
2033 		/* process the INIT info (peer's info) */
2034 		retval = sctp_process_init(init_cp, stcb);
2035 		if (retval < 0) {
2036 			if (how_indx < sizeof(asoc->cookie_how))
2037 				asoc->cookie_how[how_indx] = 13;
2038 
2039 			return (NULL);
2040 		}
2041 		/*
2042 		 * since we did not send a HB make sure we don't double
2043 		 * things
2044 		 */
2045 		net->hb_responded = 1;
2046 
2047 		if (sctp_load_addresses_from_init(stcb, m,
2048 		    init_offset + sizeof(struct sctp_init_chunk),
2049 		    initack_offset, src, dst, init_src, stcb->asoc.port)) {
2050 			if (how_indx < sizeof(asoc->cookie_how))
2051 				asoc->cookie_how[how_indx] = 14;
2052 
2053 			return (NULL);
2054 		}
2055 		/* respond with a COOKIE-ACK */
2056 		sctp_stop_all_cookie_timers(stcb);
2057 		sctp_toss_old_cookies(stcb, asoc);
2058 		sctp_send_cookie_ack(stcb);
2059 		if (how_indx < sizeof(asoc->cookie_how))
2060 			asoc->cookie_how[how_indx] = 15;
2061 
2062 		return (stcb);
2063 	}
2064 	if (how_indx < sizeof(asoc->cookie_how))
2065 		asoc->cookie_how[how_indx] = 16;
2066 	/* all other cases... */
2067 	return (NULL);
2068 }
2069 
2070 
2071 /*
2072  * handle a state cookie for a new association m: input packet mbuf chain--
2073  * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
2074  * and the cookie signature does not exist offset: offset into mbuf to the
2075  * cookie-echo chunk length: length of the cookie chunk to: where the init
2076  * was from returns a new TCB
2077  */
2078 static struct sctp_tcb *
2079 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
2080     struct sockaddr *src, struct sockaddr *dst,
2081     struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
2082     struct sctp_inpcb *inp, struct sctp_nets **netp,
2083     struct sockaddr *init_src, int *notification,
2084     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2085     uint8_t mflowtype, uint32_t mflowid,
2086     uint32_t vrf_id, uint16_t port)
2087 {
2088 	struct sctp_tcb *stcb;
2089 	struct sctp_init_chunk *init_cp, init_buf;
2090 	struct sctp_init_ack_chunk *initack_cp, initack_buf;
2091 	union sctp_sockstore store;
2092 	struct sctp_association *asoc;
2093 	int init_offset, initack_offset, initack_limit;
2094 	int retval;
2095 	int error = 0;
2096 	uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
2097 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2098 	struct socket *so;
2099 
2100 	so = SCTP_INP_SO(inp);
2101 #endif
2102 
2103 	/*
2104 	 * find and validate the INIT chunk in the cookie (peer's info) the
2105 	 * INIT should start after the cookie-echo header struct (chunk
2106 	 * header, state cookie header struct)
2107 	 */
2108 	init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
2109 	init_cp = (struct sctp_init_chunk *)
2110 	    sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
2111 	    (uint8_t *)&init_buf);
2112 	if (init_cp == NULL) {
2113 		/* could not pull a INIT chunk in cookie */
2114 		SCTPDBG(SCTP_DEBUG_INPUT1,
2115 		    "process_cookie_new: could not pull INIT chunk hdr\n");
2116 		return (NULL);
2117 	}
2118 	if (init_cp->ch.chunk_type != SCTP_INITIATION) {
2119 		SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
2120 		return (NULL);
2121 	}
2122 	initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
2123 	/*
2124 	 * find and validate the INIT-ACK chunk in the cookie (my info) the
2125 	 * INIT-ACK follows the INIT chunk
2126 	 */
2127 	initack_cp = (struct sctp_init_ack_chunk *)
2128 	    sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
2129 	    (uint8_t *)&initack_buf);
2130 	if (initack_cp == NULL) {
2131 		/* could not pull INIT-ACK chunk in cookie */
2132 		SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2133 		return (NULL);
2134 	}
2135 	if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2136 		return (NULL);
2137 	}
2138 	/*
2139 	 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2140 	 * "initack_limit" value.  This is because the chk_length field
2141 	 * includes the length of the cookie, but the cookie is omitted when
2142 	 * the INIT and INIT_ACK are tacked onto the cookie...
2143 	 */
2144 	initack_limit = offset + cookie_len;
2145 
2146 	/*
2147 	 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2148 	 * and popluate
2149 	 */
2150 
2151 	/*
2152 	 * Here we do a trick, we set in NULL for the proc/thread argument.
2153 	 * We do this since in effect we only use the p argument when the
2154 	 * socket is unbound and we must do an implicit bind. Since we are
2155 	 * getting a cookie, we cannot be unbound.
2156 	 */
2157 	stcb = sctp_aloc_assoc(inp, init_src, &error,
2158 	    ntohl(initack_cp->init.initiate_tag), vrf_id,
2159 	    ntohs(initack_cp->init.num_outbound_streams),
2160 	    port,
2161 	    (struct thread *)NULL
2162 	    );
2163 	if (stcb == NULL) {
2164 		struct mbuf *op_err;
2165 
2166 		/* memory problem? */
2167 		SCTPDBG(SCTP_DEBUG_INPUT1,
2168 		    "process_cookie_new: no room for another TCB!\n");
2169 		op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2170 		sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2171 		    src, dst, sh, op_err,
2172 		    mflowtype, mflowid,
2173 		    vrf_id, port);
2174 		return (NULL);
2175 	}
2176 	/* get the correct sctp_nets */
2177 	if (netp)
2178 		*netp = sctp_findnet(stcb, init_src);
2179 
2180 	asoc = &stcb->asoc;
2181 	/* get scope variables out of cookie */
2182 	asoc->scope.ipv4_local_scope = cookie->ipv4_scope;
2183 	asoc->scope.site_scope = cookie->site_scope;
2184 	asoc->scope.local_scope = cookie->local_scope;
2185 	asoc->scope.loopback_scope = cookie->loopback_scope;
2186 
2187 	if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2188 	    (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2189 		struct mbuf *op_err;
2190 
2191 		/*
2192 		 * Houston we have a problem. The EP changed while the
2193 		 * cookie was in flight. Only recourse is to abort the
2194 		 * association.
2195 		 */
2196 		op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2197 		sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2198 		    src, dst, sh, op_err,
2199 		    mflowtype, mflowid,
2200 		    vrf_id, port);
2201 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2202 		atomic_add_int(&stcb->asoc.refcnt, 1);
2203 		SCTP_TCB_UNLOCK(stcb);
2204 		SCTP_SOCKET_LOCK(so, 1);
2205 		SCTP_TCB_LOCK(stcb);
2206 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
2207 #endif
2208 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2209 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2210 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2211 		SCTP_SOCKET_UNLOCK(so, 1);
2212 #endif
2213 		return (NULL);
2214 	}
2215 	/* process the INIT-ACK info (my info) */
2216 	asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2217 	asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2218 	asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2219 	asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2220 	asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2221 	asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2222 	asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2223 	asoc->str_reset_seq_in = asoc->init_seq_number;
2224 
2225 	asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2226 
2227 	/* process the INIT info (peer's info) */
2228 	if (netp)
2229 		retval = sctp_process_init(init_cp, stcb);
2230 	else
2231 		retval = 0;
2232 	if (retval < 0) {
2233 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2234 		atomic_add_int(&stcb->asoc.refcnt, 1);
2235 		SCTP_TCB_UNLOCK(stcb);
2236 		SCTP_SOCKET_LOCK(so, 1);
2237 		SCTP_TCB_LOCK(stcb);
2238 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
2239 #endif
2240 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2241 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2242 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2243 		SCTP_SOCKET_UNLOCK(so, 1);
2244 #endif
2245 		return (NULL);
2246 	}
2247 	/* load all addresses */
2248 	if (sctp_load_addresses_from_init(stcb, m,
2249 	    init_offset + sizeof(struct sctp_init_chunk), initack_offset,
2250 	    src, dst, init_src, port)) {
2251 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2252 		atomic_add_int(&stcb->asoc.refcnt, 1);
2253 		SCTP_TCB_UNLOCK(stcb);
2254 		SCTP_SOCKET_LOCK(so, 1);
2255 		SCTP_TCB_LOCK(stcb);
2256 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
2257 #endif
2258 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2259 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2260 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2261 		SCTP_SOCKET_UNLOCK(so, 1);
2262 #endif
2263 		return (NULL);
2264 	}
2265 	/*
2266 	 * verify any preceding AUTH chunk that was skipped
2267 	 */
2268 	/* pull the local authentication parameters from the cookie/init-ack */
2269 	sctp_auth_get_cookie_params(stcb, m,
2270 	    initack_offset + sizeof(struct sctp_init_ack_chunk),
2271 	    initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2272 	if (auth_skipped) {
2273 		struct sctp_auth_chunk *auth;
2274 
2275 		auth = (struct sctp_auth_chunk *)
2276 		    sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2277 		if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2278 			/* auth HMAC failed, dump the assoc and packet */
2279 			SCTPDBG(SCTP_DEBUG_AUTH1,
2280 			    "COOKIE-ECHO: AUTH failed\n");
2281 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2282 			atomic_add_int(&stcb->asoc.refcnt, 1);
2283 			SCTP_TCB_UNLOCK(stcb);
2284 			SCTP_SOCKET_LOCK(so, 1);
2285 			SCTP_TCB_LOCK(stcb);
2286 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
2287 #endif
2288 			(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2289 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_21);
2290 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2291 			SCTP_SOCKET_UNLOCK(so, 1);
2292 #endif
2293 			return (NULL);
2294 		} else {
2295 			/* remaining chunks checked... good to go */
2296 			stcb->asoc.authenticated = 1;
2297 		}
2298 	}
2299 
2300 	/*
2301 	 * if we're doing ASCONFs, check to see if we have any new local
2302 	 * addresses that need to get added to the peer (eg. addresses
2303 	 * changed while cookie echo in flight).  This needs to be done
2304 	 * after we go to the OPEN state to do the correct asconf
2305 	 * processing. else, make sure we have the correct addresses in our
2306 	 * lists
2307 	 */
2308 
2309 	/* warning, we re-use sin, sin6, sa_store here! */
2310 	/* pull in local_address (our "from" address) */
2311 	switch (cookie->laddr_type) {
2312 #ifdef INET
2313 	case SCTP_IPV4_ADDRESS:
2314 		/* source addr is IPv4 */
2315 		memset(&store.sin, 0, sizeof(struct sockaddr_in));
2316 		store.sin.sin_family = AF_INET;
2317 		store.sin.sin_len = sizeof(struct sockaddr_in);
2318 		store.sin.sin_addr.s_addr = cookie->laddress[0];
2319 		break;
2320 #endif
2321 #ifdef INET6
2322 	case SCTP_IPV6_ADDRESS:
2323 		/* source addr is IPv6 */
2324 		memset(&store.sin6, 0, sizeof(struct sockaddr_in6));
2325 		store.sin6.sin6_family = AF_INET6;
2326 		store.sin6.sin6_len = sizeof(struct sockaddr_in6);
2327 		store.sin6.sin6_scope_id = cookie->scope_id;
2328 		memcpy(&store.sin6.sin6_addr, cookie->laddress, sizeof(struct in6_addr));
2329 		break;
2330 #endif
2331 	default:
2332 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2333 		atomic_add_int(&stcb->asoc.refcnt, 1);
2334 		SCTP_TCB_UNLOCK(stcb);
2335 		SCTP_SOCKET_LOCK(so, 1);
2336 		SCTP_TCB_LOCK(stcb);
2337 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
2338 #endif
2339 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2340 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
2341 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2342 		SCTP_SOCKET_UNLOCK(so, 1);
2343 #endif
2344 		return (NULL);
2345 	}
2346 
2347 	/* update current state */
2348 	SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2349 	SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2350 	if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2351 		sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2352 		    stcb->sctp_ep, stcb, asoc->primary_destination);
2353 	}
2354 	sctp_stop_all_cookie_timers(stcb);
2355 	SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2356 	SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2357 
2358 	/* set up to notify upper layer */
2359 	*notification = SCTP_NOTIFY_ASSOC_UP;
2360 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2361 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2362 	    (!SCTP_IS_LISTENING(inp))) {
2363 		/*
2364 		 * This is an endpoint that called connect() how it got a
2365 		 * cookie that is NEW is a bit of a mystery. It must be that
2366 		 * the INIT was sent, but before it got there.. a complete
2367 		 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2368 		 * should have went to the other code.. not here.. oh well..
2369 		 * a bit of protection is worth having..
2370 		 */
2371 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2372 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2373 		atomic_add_int(&stcb->asoc.refcnt, 1);
2374 		SCTP_TCB_UNLOCK(stcb);
2375 		SCTP_SOCKET_LOCK(so, 1);
2376 		SCTP_TCB_LOCK(stcb);
2377 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
2378 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2379 			SCTP_SOCKET_UNLOCK(so, 1);
2380 			return (NULL);
2381 		}
2382 #endif
2383 		soisconnected(stcb->sctp_socket);
2384 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2385 		SCTP_SOCKET_UNLOCK(so, 1);
2386 #endif
2387 	} else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2388 	    (SCTP_IS_LISTENING(inp))) {
2389 		/*
2390 		 * We don't want to do anything with this one. Since it is
2391 		 * the listening guy. The timer will get started for
2392 		 * accepted connections in the caller.
2393 		 */
2394 		;
2395 	}
2396 	/* since we did not send a HB make sure we don't double things */
2397 	if ((netp) && (*netp))
2398 		(*netp)->hb_responded = 1;
2399 
2400 	if (stcb->asoc.sctp_autoclose_ticks &&
2401 	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2402 		sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2403 	}
2404 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2405 	if ((netp != NULL) && (*netp != NULL)) {
2406 		struct timeval old;
2407 
2408 		/* calculate the RTT and set the encaps port */
2409 		old.tv_sec = cookie->time_entered.tv_sec;
2410 		old.tv_usec = cookie->time_entered.tv_usec;
2411 		(*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
2412 		    &old, SCTP_RTT_FROM_NON_DATA);
2413 	}
2414 	/* respond with a COOKIE-ACK */
2415 	sctp_send_cookie_ack(stcb);
2416 
2417 	/*
2418 	 * check the address lists for any ASCONFs that need to be sent
2419 	 * AFTER the cookie-ack is sent
2420 	 */
2421 	sctp_check_address_list(stcb, m,
2422 	    initack_offset + sizeof(struct sctp_init_ack_chunk),
2423 	    initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2424 	    &store.sa, cookie->local_scope, cookie->site_scope,
2425 	    cookie->ipv4_scope, cookie->loopback_scope);
2426 
2427 
2428 	return (stcb);
2429 }
2430 
2431 /*
2432  * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2433  * we NEED to make sure we are not already using the vtag. If so we
2434  * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2435 	head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2436 							    SCTP_BASE_INFO(hashasocmark))];
2437 	LIST_FOREACH(stcb, head, sctp_asocs) {
2438 	        if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep))  {
2439 		       -- SEND ABORT - TRY AGAIN --
2440 		}
2441 	}
2442 */
2443 
2444 /*
2445  * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2446  * existing (non-NULL) TCB
2447  */
2448 static struct mbuf *
2449 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2450     struct sockaddr *src, struct sockaddr *dst,
2451     struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2452     struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2453     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2454     struct sctp_tcb **locked_tcb,
2455     uint8_t mflowtype, uint32_t mflowid,
2456     uint32_t vrf_id, uint16_t port)
2457 {
2458 	struct sctp_state_cookie *cookie;
2459 	struct sctp_tcb *l_stcb = *stcb;
2460 	struct sctp_inpcb *l_inp;
2461 	struct sockaddr *to;
2462 	struct sctp_pcb *ep;
2463 	struct mbuf *m_sig;
2464 	uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2465 	uint8_t *sig;
2466 	uint8_t cookie_ok = 0;
2467 	unsigned int sig_offset, cookie_offset;
2468 	unsigned int cookie_len;
2469 	struct timeval now;
2470 	struct timeval time_expires;
2471 	int notification = 0;
2472 	struct sctp_nets *netl;
2473 	int had_a_existing_tcb = 0;
2474 	int send_int_conf = 0;
2475 #ifdef INET
2476 	struct sockaddr_in sin;
2477 #endif
2478 #ifdef INET6
2479 	struct sockaddr_in6 sin6;
2480 #endif
2481 
2482 	SCTPDBG(SCTP_DEBUG_INPUT2,
2483 	    "sctp_handle_cookie: handling COOKIE-ECHO\n");
2484 
2485 	if (inp_p == NULL) {
2486 		return (NULL);
2487 	}
2488 	cookie = &cp->cookie;
2489 	cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2490 	cookie_len = ntohs(cp->ch.chunk_length);
2491 
2492 	if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2493 	    sizeof(struct sctp_init_chunk) +
2494 	    sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2495 		/* cookie too small */
2496 		return (NULL);
2497 	}
2498 	if ((cookie->peerport != sh->src_port) ||
2499 	    (cookie->myport != sh->dest_port) ||
2500 	    (cookie->my_vtag != sh->v_tag)) {
2501 		/*
2502 		 * invalid ports or bad tag.  Note that we always leave the
2503 		 * v_tag in the header in network order and when we stored
2504 		 * it in the my_vtag slot we also left it in network order.
2505 		 * This maintains the match even though it may be in the
2506 		 * opposite byte order of the machine :->
2507 		 */
2508 		return (NULL);
2509 	}
2510 	/*
2511 	 * split off the signature into its own mbuf (since it should not be
2512 	 * calculated in the sctp_hmac_m() call).
2513 	 */
2514 	sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2515 	m_sig = m_split(m, sig_offset, M_NOWAIT);
2516 	if (m_sig == NULL) {
2517 		/* out of memory or ?? */
2518 		return (NULL);
2519 	}
2520 #ifdef SCTP_MBUF_LOGGING
2521 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2522 		sctp_log_mbc(m_sig, SCTP_MBUF_SPLIT);
2523 	}
2524 #endif
2525 
2526 	/*
2527 	 * compute the signature/digest for the cookie
2528 	 */
2529 	ep = &(*inp_p)->sctp_ep;
2530 	l_inp = *inp_p;
2531 	if (l_stcb) {
2532 		SCTP_TCB_UNLOCK(l_stcb);
2533 	}
2534 	SCTP_INP_RLOCK(l_inp);
2535 	if (l_stcb) {
2536 		SCTP_TCB_LOCK(l_stcb);
2537 	}
2538 	/* which cookie is it? */
2539 	if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2540 	    (ep->current_secret_number != ep->last_secret_number)) {
2541 		/* it's the old cookie */
2542 		(void)sctp_hmac_m(SCTP_HMAC,
2543 		    (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2544 		    SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2545 	} else {
2546 		/* it's the current cookie */
2547 		(void)sctp_hmac_m(SCTP_HMAC,
2548 		    (uint8_t *)ep->secret_key[(int)ep->current_secret_number],
2549 		    SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2550 	}
2551 	/* get the signature */
2552 	SCTP_INP_RUNLOCK(l_inp);
2553 	sig = (uint8_t *)sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *)&tmp_sig);
2554 	if (sig == NULL) {
2555 		/* couldn't find signature */
2556 		sctp_m_freem(m_sig);
2557 		return (NULL);
2558 	}
2559 	/* compare the received digest with the computed digest */
2560 	if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2561 		/* try the old cookie? */
2562 		if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2563 		    (ep->current_secret_number != ep->last_secret_number)) {
2564 			/* compute digest with old */
2565 			(void)sctp_hmac_m(SCTP_HMAC,
2566 			    (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2567 			    SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2568 			/* compare */
2569 			if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2570 				cookie_ok = 1;
2571 		}
2572 	} else {
2573 		cookie_ok = 1;
2574 	}
2575 
2576 	/*
2577 	 * Now before we continue we must reconstruct our mbuf so that
2578 	 * normal processing of any other chunks will work.
2579 	 */
2580 	{
2581 		struct mbuf *m_at;
2582 
2583 		m_at = m;
2584 		while (SCTP_BUF_NEXT(m_at) != NULL) {
2585 			m_at = SCTP_BUF_NEXT(m_at);
2586 		}
2587 		SCTP_BUF_NEXT(m_at) = m_sig;
2588 	}
2589 
2590 	if (cookie_ok == 0) {
2591 		SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2592 		SCTPDBG(SCTP_DEBUG_INPUT2,
2593 		    "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2594 		    (uint32_t)offset, cookie_offset, sig_offset);
2595 		return (NULL);
2596 	}
2597 
2598 	/*
2599 	 * check the cookie timestamps to be sure it's not stale
2600 	 */
2601 	(void)SCTP_GETTIME_TIMEVAL(&now);
2602 	/* Expire time is in Ticks, so we convert to seconds */
2603 	time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2604 	time_expires.tv_usec = cookie->time_entered.tv_usec;
2605 	if (timevalcmp(&now, &time_expires, >)) {
2606 		/* cookie is stale! */
2607 		struct mbuf *op_err;
2608 		struct sctp_error_stale_cookie *cause;
2609 		struct timeval diff;
2610 		uint32_t staleness;
2611 
2612 		op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_stale_cookie),
2613 		    0, M_NOWAIT, 1, MT_DATA);
2614 		if (op_err == NULL) {
2615 			/* FOOBAR */
2616 			return (NULL);
2617 		}
2618 		/* Set the len */
2619 		SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_stale_cookie);
2620 		cause = mtod(op_err, struct sctp_error_stale_cookie *);
2621 		cause->cause.code = htons(SCTP_CAUSE_STALE_COOKIE);
2622 		cause->cause.length = htons((sizeof(struct sctp_paramhdr) +
2623 		    (sizeof(uint32_t))));
2624 		diff = now;
2625 		timevalsub(&diff, &time_expires);
2626 		if ((uint32_t)diff.tv_sec > UINT32_MAX / 1000000) {
2627 			staleness = UINT32_MAX;
2628 		} else {
2629 			staleness = diff.tv_sec * 1000000;
2630 		}
2631 		if (UINT32_MAX - staleness >= (uint32_t)diff.tv_usec) {
2632 			staleness += diff.tv_usec;
2633 		} else {
2634 			staleness = UINT32_MAX;
2635 		}
2636 		cause->stale_time = htonl(staleness);
2637 		sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
2638 		    mflowtype, mflowid, l_inp->fibnum,
2639 		    vrf_id, port);
2640 		return (NULL);
2641 	}
2642 	/*
2643 	 * Now we must see with the lookup address if we have an existing
2644 	 * asoc. This will only happen if we were in the COOKIE-WAIT state
2645 	 * and a INIT collided with us and somewhere the peer sent the
2646 	 * cookie on another address besides the single address our assoc
2647 	 * had for him. In this case we will have one of the tie-tags set at
2648 	 * least AND the address field in the cookie can be used to look it
2649 	 * up.
2650 	 */
2651 	to = NULL;
2652 	switch (cookie->addr_type) {
2653 #ifdef INET6
2654 	case SCTP_IPV6_ADDRESS:
2655 		memset(&sin6, 0, sizeof(sin6));
2656 		sin6.sin6_family = AF_INET6;
2657 		sin6.sin6_len = sizeof(sin6);
2658 		sin6.sin6_port = sh->src_port;
2659 		sin6.sin6_scope_id = cookie->scope_id;
2660 		memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2661 		    sizeof(sin6.sin6_addr.s6_addr));
2662 		to = (struct sockaddr *)&sin6;
2663 		break;
2664 #endif
2665 #ifdef INET
2666 	case SCTP_IPV4_ADDRESS:
2667 		memset(&sin, 0, sizeof(sin));
2668 		sin.sin_family = AF_INET;
2669 		sin.sin_len = sizeof(sin);
2670 		sin.sin_port = sh->src_port;
2671 		sin.sin_addr.s_addr = cookie->address[0];
2672 		to = (struct sockaddr *)&sin;
2673 		break;
2674 #endif
2675 	default:
2676 		/* This should not happen */
2677 		return (NULL);
2678 	}
2679 	if (*stcb == NULL) {
2680 		/* Yep, lets check */
2681 		*stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
2682 		if (*stcb == NULL) {
2683 			/*
2684 			 * We should have only got back the same inp. If we
2685 			 * got back a different ep we have a problem. The
2686 			 * original findep got back l_inp and now
2687 			 */
2688 			if (l_inp != *inp_p) {
2689 				SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2690 			}
2691 		} else {
2692 			if (*locked_tcb == NULL) {
2693 				/*
2694 				 * In this case we found the assoc only
2695 				 * after we locked the create lock. This
2696 				 * means we are in a colliding case and we
2697 				 * must make sure that we unlock the tcb if
2698 				 * its one of the cases where we throw away
2699 				 * the incoming packets.
2700 				 */
2701 				*locked_tcb = *stcb;
2702 
2703 				/*
2704 				 * We must also increment the inp ref count
2705 				 * since the ref_count flags was set when we
2706 				 * did not find the TCB, now we found it
2707 				 * which reduces the refcount.. we must
2708 				 * raise it back out to balance it all :-)
2709 				 */
2710 				SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2711 				if ((*stcb)->sctp_ep != l_inp) {
2712 					SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2713 					    (void *)(*stcb)->sctp_ep, (void *)l_inp);
2714 				}
2715 			}
2716 		}
2717 	}
2718 
2719 	cookie_len -= SCTP_SIGNATURE_SIZE;
2720 	if (*stcb == NULL) {
2721 		/* this is the "normal" case... get a new TCB */
2722 		*stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
2723 		    cookie, cookie_len, *inp_p,
2724 		    netp, to, &notification,
2725 		    auth_skipped, auth_offset, auth_len,
2726 		    mflowtype, mflowid,
2727 		    vrf_id, port);
2728 	} else {
2729 		/* this is abnormal... cookie-echo on existing TCB */
2730 		had_a_existing_tcb = 1;
2731 		*stcb = sctp_process_cookie_existing(m, iphlen, offset,
2732 		    src, dst, sh,
2733 		    cookie, cookie_len, *inp_p, *stcb, netp, to,
2734 		    &notification, auth_skipped, auth_offset, auth_len,
2735 		    mflowtype, mflowid,
2736 		    vrf_id, port);
2737 	}
2738 
2739 	if (*stcb == NULL) {
2740 		/* still no TCB... must be bad cookie-echo */
2741 		return (NULL);
2742 	}
2743 	if (*netp != NULL) {
2744 		(*netp)->flowtype = mflowtype;
2745 		(*netp)->flowid = mflowid;
2746 	}
2747 	/*
2748 	 * Ok, we built an association so confirm the address we sent the
2749 	 * INIT-ACK to.
2750 	 */
2751 	netl = sctp_findnet(*stcb, to);
2752 	/*
2753 	 * This code should in theory NOT run but
2754 	 */
2755 	if (netl == NULL) {
2756 		/* TSNH! Huh, why do I need to add this address here? */
2757 		if (sctp_add_remote_addr(*stcb, to, NULL, port,
2758 		    SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
2759 			return (NULL);
2760 		}
2761 		netl = sctp_findnet(*stcb, to);
2762 	}
2763 	if (netl) {
2764 		if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2765 			netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2766 			(void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2767 			    netl);
2768 			send_int_conf = 1;
2769 		}
2770 	}
2771 	sctp_start_net_timers(*stcb);
2772 	if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2773 		if (!had_a_existing_tcb ||
2774 		    (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2775 			/*
2776 			 * If we have a NEW cookie or the connect never
2777 			 * reached the connected state during collision we
2778 			 * must do the TCP accept thing.
2779 			 */
2780 			struct socket *so, *oso;
2781 			struct sctp_inpcb *inp;
2782 
2783 			if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2784 				/*
2785 				 * For a restart we will keep the same
2786 				 * socket, no need to do anything. I THINK!!
2787 				 */
2788 				sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2789 				if (send_int_conf) {
2790 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2791 					    (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2792 				}
2793 				return (m);
2794 			}
2795 			oso = (*inp_p)->sctp_socket;
2796 			atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2797 			SCTP_TCB_UNLOCK((*stcb));
2798 			CURVNET_SET(oso->so_vnet);
2799 			so = sonewconn(oso, 0
2800 			    );
2801 			CURVNET_RESTORE();
2802 			SCTP_TCB_LOCK((*stcb));
2803 			atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2804 
2805 			if (so == NULL) {
2806 				struct mbuf *op_err;
2807 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2808 				struct socket *pcb_so;
2809 #endif
2810 				/* Too many sockets */
2811 				SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2812 				op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2813 				sctp_abort_association(*inp_p, NULL, m, iphlen,
2814 				    src, dst, sh, op_err,
2815 				    mflowtype, mflowid,
2816 				    vrf_id, port);
2817 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2818 				pcb_so = SCTP_INP_SO(*inp_p);
2819 				atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2820 				SCTP_TCB_UNLOCK((*stcb));
2821 				SCTP_SOCKET_LOCK(pcb_so, 1);
2822 				SCTP_TCB_LOCK((*stcb));
2823 				atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2824 #endif
2825 				(void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC,
2826 				    SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
2827 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2828 				SCTP_SOCKET_UNLOCK(pcb_so, 1);
2829 #endif
2830 				return (NULL);
2831 			}
2832 			inp = (struct sctp_inpcb *)so->so_pcb;
2833 			SCTP_INP_INCR_REF(inp);
2834 			/*
2835 			 * We add the unbound flag here so that if we get an
2836 			 * soabort() before we get the move_pcb done, we
2837 			 * will properly cleanup.
2838 			 */
2839 			inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2840 			    SCTP_PCB_FLAGS_CONNECTED |
2841 			    SCTP_PCB_FLAGS_IN_TCPPOOL |
2842 			    SCTP_PCB_FLAGS_UNBOUND |
2843 			    (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2844 			    SCTP_PCB_FLAGS_DONT_WAKE);
2845 			inp->sctp_features = (*inp_p)->sctp_features;
2846 			inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2847 			inp->sctp_socket = so;
2848 			inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2849 			inp->max_cwnd = (*inp_p)->max_cwnd;
2850 			inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2851 			inp->ecn_supported = (*inp_p)->ecn_supported;
2852 			inp->prsctp_supported = (*inp_p)->prsctp_supported;
2853 			inp->auth_supported = (*inp_p)->auth_supported;
2854 			inp->asconf_supported = (*inp_p)->asconf_supported;
2855 			inp->reconfig_supported = (*inp_p)->reconfig_supported;
2856 			inp->nrsack_supported = (*inp_p)->nrsack_supported;
2857 			inp->pktdrop_supported = (*inp_p)->pktdrop_supported;
2858 			inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2859 			inp->sctp_context = (*inp_p)->sctp_context;
2860 			inp->local_strreset_support = (*inp_p)->local_strreset_support;
2861 			inp->fibnum = (*inp_p)->fibnum;
2862 			inp->inp_starting_point_for_iterator = NULL;
2863 			/*
2864 			 * copy in the authentication parameters from the
2865 			 * original endpoint
2866 			 */
2867 			if (inp->sctp_ep.local_hmacs)
2868 				sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2869 			inp->sctp_ep.local_hmacs =
2870 			    sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2871 			if (inp->sctp_ep.local_auth_chunks)
2872 				sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2873 			inp->sctp_ep.local_auth_chunks =
2874 			    sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2875 
2876 			/*
2877 			 * Now we must move it from one hash table to
2878 			 * another and get the tcb in the right place.
2879 			 */
2880 
2881 			/*
2882 			 * This is where the one-2-one socket is put into
2883 			 * the accept state waiting for the accept!
2884 			 */
2885 			if (*stcb) {
2886 				(*stcb)->asoc.state |= SCTP_STATE_IN_ACCEPT_QUEUE;
2887 			}
2888 			sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2889 
2890 			atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2891 			SCTP_TCB_UNLOCK((*stcb));
2892 
2893 			sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2894 			    0);
2895 			SCTP_TCB_LOCK((*stcb));
2896 			atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2897 
2898 
2899 			/*
2900 			 * now we must check to see if we were aborted while
2901 			 * the move was going on and the lock/unlock
2902 			 * happened.
2903 			 */
2904 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2905 				/*
2906 				 * yep it was, we leave the assoc attached
2907 				 * to the socket since the sctp_inpcb_free()
2908 				 * call will send an abort for us.
2909 				 */
2910 				SCTP_INP_DECR_REF(inp);
2911 				return (NULL);
2912 			}
2913 			SCTP_INP_DECR_REF(inp);
2914 			/* Switch over to the new guy */
2915 			*inp_p = inp;
2916 			sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2917 			if (send_int_conf) {
2918 				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2919 				    (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2920 			}
2921 
2922 			/*
2923 			 * Pull it from the incomplete queue and wake the
2924 			 * guy
2925 			 */
2926 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2927 			atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2928 			SCTP_TCB_UNLOCK((*stcb));
2929 			SCTP_SOCKET_LOCK(so, 1);
2930 #endif
2931 			soisconnected(so);
2932 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2933 			SCTP_TCB_LOCK((*stcb));
2934 			atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2935 			SCTP_SOCKET_UNLOCK(so, 1);
2936 #endif
2937 			return (m);
2938 		}
2939 	}
2940 	if (notification) {
2941 		sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2942 	}
2943 	if (send_int_conf) {
2944 		sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2945 		    (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2946 	}
2947 	return (m);
2948 }
2949 
2950 static void
2951 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2952     struct sctp_tcb *stcb, struct sctp_nets *net)
2953 {
2954 	/* cp must not be used, others call this without a c-ack :-) */
2955 	struct sctp_association *asoc;
2956 
2957 	SCTPDBG(SCTP_DEBUG_INPUT2,
2958 	    "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2959 	if ((stcb == NULL) || (net == NULL)) {
2960 		return;
2961 	}
2962 
2963 	asoc = &stcb->asoc;
2964 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
2965 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2966 		    asoc->overall_error_count,
2967 		    0,
2968 		    SCTP_FROM_SCTP_INPUT,
2969 		    __LINE__);
2970 	}
2971 	asoc->overall_error_count = 0;
2972 	sctp_stop_all_cookie_timers(stcb);
2973 	/* process according to association state */
2974 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2975 		/* state change only needed when I am in right state */
2976 		SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2977 		SCTP_SET_STATE(asoc, SCTP_STATE_OPEN);
2978 		sctp_start_net_timers(stcb);
2979 		if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2980 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2981 			    stcb->sctp_ep, stcb, asoc->primary_destination);
2982 
2983 		}
2984 		/* update RTO */
2985 		SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2986 		SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2987 		if (asoc->overall_error_count == 0) {
2988 			net->RTO = sctp_calculate_rto(stcb, asoc, net,
2989 			    &asoc->time_entered,
2990 			    SCTP_RTT_FROM_NON_DATA);
2991 		}
2992 		(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2993 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2994 		if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2995 		    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2996 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2997 			struct socket *so;
2998 
2999 #endif
3000 			stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
3001 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3002 			so = SCTP_INP_SO(stcb->sctp_ep);
3003 			atomic_add_int(&stcb->asoc.refcnt, 1);
3004 			SCTP_TCB_UNLOCK(stcb);
3005 			SCTP_SOCKET_LOCK(so, 1);
3006 			SCTP_TCB_LOCK(stcb);
3007 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
3008 #endif
3009 			if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
3010 				soisconnected(stcb->sctp_socket);
3011 			}
3012 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3013 			SCTP_SOCKET_UNLOCK(so, 1);
3014 #endif
3015 		}
3016 		/*
3017 		 * since we did not send a HB make sure we don't double
3018 		 * things
3019 		 */
3020 		net->hb_responded = 1;
3021 
3022 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3023 			/*
3024 			 * We don't need to do the asconf thing, nor hb or
3025 			 * autoclose if the socket is closed.
3026 			 */
3027 			goto closed_socket;
3028 		}
3029 
3030 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
3031 		    stcb, net);
3032 
3033 
3034 		if (stcb->asoc.sctp_autoclose_ticks &&
3035 		    sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
3036 			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
3037 			    stcb->sctp_ep, stcb, NULL);
3038 		}
3039 		/*
3040 		 * send ASCONF if parameters are pending and ASCONFs are
3041 		 * allowed (eg. addresses changed when init/cookie echo were
3042 		 * in flight)
3043 		 */
3044 		if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
3045 		    (stcb->asoc.asconf_supported == 1) &&
3046 		    (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
3047 #ifdef SCTP_TIMER_BASED_ASCONF
3048 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
3049 			    stcb->sctp_ep, stcb,
3050 			    stcb->asoc.primary_destination);
3051 #else
3052 			sctp_send_asconf(stcb, stcb->asoc.primary_destination,
3053 			    SCTP_ADDR_NOT_LOCKED);
3054 #endif
3055 		}
3056 	}
3057 closed_socket:
3058 	/* Toss the cookie if I can */
3059 	sctp_toss_old_cookies(stcb, asoc);
3060 	if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3061 		/* Restart the timer if we have pending data */
3062 		struct sctp_tmit_chunk *chk;
3063 
3064 		chk = TAILQ_FIRST(&asoc->sent_queue);
3065 		sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
3066 	}
3067 }
3068 
3069 static void
3070 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
3071     struct sctp_tcb *stcb)
3072 {
3073 	struct sctp_nets *net;
3074 	struct sctp_tmit_chunk *lchk;
3075 	struct sctp_ecne_chunk bkup;
3076 	uint8_t override_bit;
3077 	uint32_t tsn, window_data_tsn;
3078 	int len;
3079 	unsigned int pkt_cnt;
3080 
3081 	len = ntohs(cp->ch.chunk_length);
3082 	if ((len != sizeof(struct sctp_ecne_chunk)) &&
3083 	    (len != sizeof(struct old_sctp_ecne_chunk))) {
3084 		return;
3085 	}
3086 	if (len == sizeof(struct old_sctp_ecne_chunk)) {
3087 		/* Its the old format */
3088 		memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
3089 		bkup.num_pkts_since_cwr = htonl(1);
3090 		cp = &bkup;
3091 	}
3092 	SCTP_STAT_INCR(sctps_recvecne);
3093 	tsn = ntohl(cp->tsn);
3094 	pkt_cnt = ntohl(cp->num_pkts_since_cwr);
3095 	lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
3096 	if (lchk == NULL) {
3097 		window_data_tsn = stcb->asoc.sending_seq - 1;
3098 	} else {
3099 		window_data_tsn = lchk->rec.data.tsn;
3100 	}
3101 
3102 	/* Find where it was sent to if possible. */
3103 	net = NULL;
3104 	TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
3105 		if (lchk->rec.data.tsn == tsn) {
3106 			net = lchk->whoTo;
3107 			net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
3108 			break;
3109 		}
3110 		if (SCTP_TSN_GT(lchk->rec.data.tsn, tsn)) {
3111 			break;
3112 		}
3113 	}
3114 	if (net == NULL) {
3115 		/*
3116 		 * What to do. A previous send of a CWR was possibly lost.
3117 		 * See how old it is, we may have it marked on the actual
3118 		 * net.
3119 		 */
3120 		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3121 			if (tsn == net->last_cwr_tsn) {
3122 				/* Found him, send it off */
3123 				break;
3124 			}
3125 		}
3126 		if (net == NULL) {
3127 			/*
3128 			 * If we reach here, we need to send a special CWR
3129 			 * that says hey, we did this a long time ago and
3130 			 * you lost the response.
3131 			 */
3132 			net = TAILQ_FIRST(&stcb->asoc.nets);
3133 			if (net == NULL) {
3134 				/* TSNH */
3135 				return;
3136 			}
3137 			override_bit = SCTP_CWR_REDUCE_OVERRIDE;
3138 		} else {
3139 			override_bit = 0;
3140 		}
3141 	} else {
3142 		override_bit = 0;
3143 	}
3144 	if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
3145 	    ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3146 		/*
3147 		 * JRS - Use the congestion control given in the pluggable
3148 		 * CC module
3149 		 */
3150 		stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
3151 		/*
3152 		 * We reduce once every RTT. So we will only lower cwnd at
3153 		 * the next sending seq i.e. the window_data_tsn
3154 		 */
3155 		net->cwr_window_tsn = window_data_tsn;
3156 		net->ecn_ce_pkt_cnt += pkt_cnt;
3157 		net->lost_cnt = pkt_cnt;
3158 		net->last_cwr_tsn = tsn;
3159 	} else {
3160 		override_bit |= SCTP_CWR_IN_SAME_WINDOW;
3161 		if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
3162 		    ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3163 			/*
3164 			 * Another loss in the same window update how many
3165 			 * marks/packets lost we have had.
3166 			 */
3167 			int cnt = 1;
3168 
3169 			if (pkt_cnt > net->lost_cnt) {
3170 				/* Should be the case */
3171 				cnt = (pkt_cnt - net->lost_cnt);
3172 				net->ecn_ce_pkt_cnt += cnt;
3173 			}
3174 			net->lost_cnt = pkt_cnt;
3175 			net->last_cwr_tsn = tsn;
3176 			/*
3177 			 * Most CC functions will ignore this call, since we
3178 			 * are in-window yet of the initial CE the peer saw.
3179 			 */
3180 			stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
3181 		}
3182 	}
3183 	/*
3184 	 * We always send a CWR this way if our previous one was lost our
3185 	 * peer will get an update, or if it is not time again to reduce we
3186 	 * still get the cwr to the peer. Note we set the override when we
3187 	 * could not find the TSN on the chunk or the destination network.
3188 	 */
3189 	sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
3190 }
3191 
3192 static void
3193 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3194 {
3195 	/*
3196 	 * Here we get a CWR from the peer. We must look in the outqueue and
3197 	 * make sure that we have a covered ECNE in the control chunk part.
3198 	 * If so remove it.
3199 	 */
3200 	struct sctp_tmit_chunk *chk, *nchk;
3201 	struct sctp_ecne_chunk *ecne;
3202 	int override;
3203 	uint32_t cwr_tsn;
3204 
3205 	cwr_tsn = ntohl(cp->tsn);
3206 	override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
3207 	TAILQ_FOREACH_SAFE(chk, &stcb->asoc.control_send_queue, sctp_next, nchk) {
3208 		if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3209 			continue;
3210 		}
3211 		if ((override == 0) && (chk->whoTo != net)) {
3212 			/* Must be from the right src unless override is set */
3213 			continue;
3214 		}
3215 		ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3216 		if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3217 			/* this covers this ECNE, we can remove it */
3218 			stcb->asoc.ecn_echo_cnt_onq--;
3219 			TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3220 			    sctp_next);
3221 			stcb->asoc.ctrl_queue_cnt--;
3222 			sctp_m_freem(chk->data);
3223 			chk->data = NULL;
3224 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3225 			if (override == 0) {
3226 				break;
3227 			}
3228 		}
3229 	}
3230 }
3231 
3232 static void
3233 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3234     struct sctp_tcb *stcb, struct sctp_nets *net)
3235 {
3236 	struct sctp_association *asoc;
3237 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3238 	struct socket *so;
3239 #endif
3240 
3241 	SCTPDBG(SCTP_DEBUG_INPUT2,
3242 	    "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3243 	if (stcb == NULL)
3244 		return;
3245 
3246 	asoc = &stcb->asoc;
3247 	/* process according to association state */
3248 	if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3249 		/* unexpected SHUTDOWN-COMPLETE... so ignore... */
3250 		SCTPDBG(SCTP_DEBUG_INPUT2,
3251 		    "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3252 		SCTP_TCB_UNLOCK(stcb);
3253 		return;
3254 	}
3255 	/* notify upper layer protocol */
3256 	if (stcb->sctp_socket) {
3257 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3258 	}
3259 #ifdef INVARIANTS
3260 	if (!TAILQ_EMPTY(&asoc->send_queue) ||
3261 	    !TAILQ_EMPTY(&asoc->sent_queue) ||
3262 	    sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
3263 		panic("Queues are not empty when handling SHUTDOWN-COMPLETE");
3264 	}
3265 #endif
3266 	/* stop the timer */
3267 	sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net,
3268 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3269 	SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3270 	/* free the TCB */
3271 	SCTPDBG(SCTP_DEBUG_INPUT2,
3272 	    "sctp_handle_shutdown_complete: calls free-asoc\n");
3273 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3274 	so = SCTP_INP_SO(stcb->sctp_ep);
3275 	atomic_add_int(&stcb->asoc.refcnt, 1);
3276 	SCTP_TCB_UNLOCK(stcb);
3277 	SCTP_SOCKET_LOCK(so, 1);
3278 	SCTP_TCB_LOCK(stcb);
3279 	atomic_subtract_int(&stcb->asoc.refcnt, 1);
3280 #endif
3281 	(void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
3282 	    SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3283 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3284 	SCTP_SOCKET_UNLOCK(so, 1);
3285 #endif
3286 	return;
3287 }
3288 
3289 static int
3290 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3291     struct sctp_nets *net, uint8_t flg)
3292 {
3293 	switch (desc->chunk_type) {
3294 	case SCTP_DATA:
3295 		/* find the tsn to resend (possibly */
3296 		{
3297 			uint32_t tsn;
3298 			struct sctp_tmit_chunk *tp1;
3299 
3300 			tsn = ntohl(desc->tsn_ifany);
3301 			TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3302 				if (tp1->rec.data.tsn == tsn) {
3303 					/* found it */
3304 					break;
3305 				}
3306 				if (SCTP_TSN_GT(tp1->rec.data.tsn, tsn)) {
3307 					/* not found */
3308 					tp1 = NULL;
3309 					break;
3310 				}
3311 			}
3312 			if (tp1 == NULL) {
3313 				/*
3314 				 * Do it the other way , aka without paying
3315 				 * attention to queue seq order.
3316 				 */
3317 				SCTP_STAT_INCR(sctps_pdrpdnfnd);
3318 				TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3319 					if (tp1->rec.data.tsn == tsn) {
3320 						/* found it */
3321 						break;
3322 					}
3323 				}
3324 			}
3325 			if (tp1 == NULL) {
3326 				SCTP_STAT_INCR(sctps_pdrptsnnf);
3327 			}
3328 			if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3329 				uint8_t *ddp;
3330 
3331 				if (((flg & SCTP_BADCRC) == 0) &&
3332 				    ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3333 					return (0);
3334 				}
3335 				if ((stcb->asoc.peers_rwnd == 0) &&
3336 				    ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3337 					SCTP_STAT_INCR(sctps_pdrpdiwnp);
3338 					return (0);
3339 				}
3340 				if (stcb->asoc.peers_rwnd == 0 &&
3341 				    (flg & SCTP_FROM_MIDDLE_BOX)) {
3342 					SCTP_STAT_INCR(sctps_pdrpdizrw);
3343 					return (0);
3344 				}
3345 				ddp = (uint8_t *)(mtod(tp1->data, caddr_t)+
3346 				    sizeof(struct sctp_data_chunk));
3347 				{
3348 					unsigned int iii;
3349 
3350 					for (iii = 0; iii < sizeof(desc->data_bytes);
3351 					    iii++) {
3352 						if (ddp[iii] != desc->data_bytes[iii]) {
3353 							SCTP_STAT_INCR(sctps_pdrpbadd);
3354 							return (-1);
3355 						}
3356 					}
3357 				}
3358 
3359 				if (tp1->do_rtt) {
3360 					/*
3361 					 * this guy had a RTO calculation
3362 					 * pending on it, cancel it
3363 					 */
3364 					if (tp1->whoTo->rto_needed == 0) {
3365 						tp1->whoTo->rto_needed = 1;
3366 					}
3367 					tp1->do_rtt = 0;
3368 				}
3369 				SCTP_STAT_INCR(sctps_pdrpmark);
3370 				if (tp1->sent != SCTP_DATAGRAM_RESEND)
3371 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3372 				/*
3373 				 * mark it as if we were doing a FR, since
3374 				 * we will be getting gap ack reports behind
3375 				 * the info from the router.
3376 				 */
3377 				tp1->rec.data.doing_fast_retransmit = 1;
3378 				/*
3379 				 * mark the tsn with what sequences can
3380 				 * cause a new FR.
3381 				 */
3382 				if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3383 					tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3384 				} else {
3385 					tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.tsn;
3386 				}
3387 
3388 				/* restart the timer */
3389 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3390 				    stcb, tp1->whoTo,
3391 				    SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3392 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3393 				    stcb, tp1->whoTo);
3394 
3395 				/* fix counts and things */
3396 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3397 					sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3398 					    tp1->whoTo->flight_size,
3399 					    tp1->book_size,
3400 					    (uint32_t)(uintptr_t)stcb,
3401 					    tp1->rec.data.tsn);
3402 				}
3403 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3404 					sctp_flight_size_decrease(tp1);
3405 					sctp_total_flight_decrease(stcb, tp1);
3406 				}
3407 				tp1->sent = SCTP_DATAGRAM_RESEND;
3408 			} {
3409 				/* audit code */
3410 				unsigned int audit;
3411 
3412 				audit = 0;
3413 				TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3414 					if (tp1->sent == SCTP_DATAGRAM_RESEND)
3415 						audit++;
3416 				}
3417 				TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3418 				    sctp_next) {
3419 					if (tp1->sent == SCTP_DATAGRAM_RESEND)
3420 						audit++;
3421 				}
3422 				if (audit != stcb->asoc.sent_queue_retran_cnt) {
3423 					SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3424 					    audit, stcb->asoc.sent_queue_retran_cnt);
3425 #ifndef SCTP_AUDITING_ENABLED
3426 					stcb->asoc.sent_queue_retran_cnt = audit;
3427 #endif
3428 				}
3429 			}
3430 		}
3431 		break;
3432 	case SCTP_ASCONF:
3433 		{
3434 			struct sctp_tmit_chunk *asconf;
3435 
3436 			TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3437 			    sctp_next) {
3438 				if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3439 					break;
3440 				}
3441 			}
3442 			if (asconf) {
3443 				if (asconf->sent != SCTP_DATAGRAM_RESEND)
3444 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3445 				asconf->sent = SCTP_DATAGRAM_RESEND;
3446 				asconf->snd_count--;
3447 			}
3448 		}
3449 		break;
3450 	case SCTP_INITIATION:
3451 		/* resend the INIT */
3452 		stcb->asoc.dropped_special_cnt++;
3453 		if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3454 			/*
3455 			 * If we can get it in, in a few attempts we do
3456 			 * this, otherwise we let the timer fire.
3457 			 */
3458 			sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3459 			    stcb, net,
3460 			    SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
3461 			sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3462 		}
3463 		break;
3464 	case SCTP_SELECTIVE_ACK:
3465 	case SCTP_NR_SELECTIVE_ACK:
3466 		/* resend the sack */
3467 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3468 		break;
3469 	case SCTP_HEARTBEAT_REQUEST:
3470 		/* resend a demand HB */
3471 		if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3472 			/*
3473 			 * Only retransmit if we KNOW we wont destroy the
3474 			 * tcb
3475 			 */
3476 			sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3477 		}
3478 		break;
3479 	case SCTP_SHUTDOWN:
3480 		sctp_send_shutdown(stcb, net);
3481 		break;
3482 	case SCTP_SHUTDOWN_ACK:
3483 		sctp_send_shutdown_ack(stcb, net);
3484 		break;
3485 	case SCTP_COOKIE_ECHO:
3486 		{
3487 			struct sctp_tmit_chunk *cookie;
3488 
3489 			cookie = NULL;
3490 			TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3491 			    sctp_next) {
3492 				if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3493 					break;
3494 				}
3495 			}
3496 			if (cookie) {
3497 				if (cookie->sent != SCTP_DATAGRAM_RESEND)
3498 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3499 				cookie->sent = SCTP_DATAGRAM_RESEND;
3500 				sctp_stop_all_cookie_timers(stcb);
3501 			}
3502 		}
3503 		break;
3504 	case SCTP_COOKIE_ACK:
3505 		sctp_send_cookie_ack(stcb);
3506 		break;
3507 	case SCTP_ASCONF_ACK:
3508 		/* resend last asconf ack */
3509 		sctp_send_asconf_ack(stcb);
3510 		break;
3511 	case SCTP_IFORWARD_CUM_TSN:
3512 	case SCTP_FORWARD_CUM_TSN:
3513 		send_forward_tsn(stcb, &stcb->asoc);
3514 		break;
3515 		/* can't do anything with these */
3516 	case SCTP_PACKET_DROPPED:
3517 	case SCTP_INITIATION_ACK:	/* this should not happen */
3518 	case SCTP_HEARTBEAT_ACK:
3519 	case SCTP_ABORT_ASSOCIATION:
3520 	case SCTP_OPERATION_ERROR:
3521 	case SCTP_SHUTDOWN_COMPLETE:
3522 	case SCTP_ECN_ECHO:
3523 	case SCTP_ECN_CWR:
3524 	default:
3525 		break;
3526 	}
3527 	return (0);
3528 }
3529 
3530 void
3531 sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3532 {
3533 	uint32_t i;
3534 	uint16_t temp;
3535 
3536 	/*
3537 	 * We set things to 0xffffffff since this is the last delivered
3538 	 * sequence and we will be sending in 0 after the reset.
3539 	 */
3540 
3541 	if (number_entries) {
3542 		for (i = 0; i < number_entries; i++) {
3543 			temp = ntohs(list[i]);
3544 			if (temp >= stcb->asoc.streamincnt) {
3545 				continue;
3546 			}
3547 			stcb->asoc.strmin[temp].last_mid_delivered = 0xffffffff;
3548 		}
3549 	} else {
3550 		list = NULL;
3551 		for (i = 0; i < stcb->asoc.streamincnt; i++) {
3552 			stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
3553 		}
3554 	}
3555 	sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3556 }
3557 
3558 static void
3559 sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3560 {
3561 	uint32_t i;
3562 	uint16_t temp;
3563 
3564 	if (number_entries > 0) {
3565 		for (i = 0; i < number_entries; i++) {
3566 			temp = ntohs(list[i]);
3567 			if (temp >= stcb->asoc.streamoutcnt) {
3568 				/* no such stream */
3569 				continue;
3570 			}
3571 			stcb->asoc.strmout[temp].next_mid_ordered = 0;
3572 			stcb->asoc.strmout[temp].next_mid_unordered = 0;
3573 		}
3574 	} else {
3575 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3576 			stcb->asoc.strmout[i].next_mid_ordered = 0;
3577 			stcb->asoc.strmout[i].next_mid_unordered = 0;
3578 		}
3579 	}
3580 	sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3581 }
3582 
3583 static void
3584 sctp_reset_clear_pending(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3585 {
3586 	uint32_t i;
3587 	uint16_t temp;
3588 
3589 	if (number_entries > 0) {
3590 		for (i = 0; i < number_entries; i++) {
3591 			temp = ntohs(list[i]);
3592 			if (temp >= stcb->asoc.streamoutcnt) {
3593 				/* no such stream */
3594 				continue;
3595 			}
3596 			stcb->asoc.strmout[temp].state = SCTP_STREAM_OPEN;
3597 		}
3598 	} else {
3599 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3600 			stcb->asoc.strmout[i].state = SCTP_STREAM_OPEN;
3601 		}
3602 	}
3603 }
3604 
3605 
3606 struct sctp_stream_reset_request *
3607 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3608 {
3609 	struct sctp_association *asoc;
3610 	struct sctp_chunkhdr *ch;
3611 	struct sctp_stream_reset_request *r;
3612 	struct sctp_tmit_chunk *chk;
3613 	int len, clen;
3614 
3615 	asoc = &stcb->asoc;
3616 	if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3617 		asoc->stream_reset_outstanding = 0;
3618 		return (NULL);
3619 	}
3620 	if (stcb->asoc.str_reset == NULL) {
3621 		asoc->stream_reset_outstanding = 0;
3622 		return (NULL);
3623 	}
3624 	chk = stcb->asoc.str_reset;
3625 	if (chk->data == NULL) {
3626 		return (NULL);
3627 	}
3628 	if (bchk) {
3629 		/* he wants a copy of the chk pointer */
3630 		*bchk = chk;
3631 	}
3632 	clen = chk->send_size;
3633 	ch = mtod(chk->data, struct sctp_chunkhdr *);
3634 	r = (struct sctp_stream_reset_request *)(ch + 1);
3635 	if (ntohl(r->request_seq) == seq) {
3636 		/* found it */
3637 		return (r);
3638 	}
3639 	len = SCTP_SIZE32(ntohs(r->ph.param_length));
3640 	if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3641 		/* move to the next one, there can only be a max of two */
3642 		r = (struct sctp_stream_reset_request *)((caddr_t)r + len);
3643 		if (ntohl(r->request_seq) == seq) {
3644 			return (r);
3645 		}
3646 	}
3647 	/* that seq is not here */
3648 	return (NULL);
3649 }
3650 
3651 static void
3652 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3653 {
3654 	struct sctp_association *asoc;
3655 	struct sctp_tmit_chunk *chk;
3656 
3657 	asoc = &stcb->asoc;
3658 	chk = asoc->str_reset;
3659 	if (chk == NULL) {
3660 		return;
3661 	}
3662 	asoc->str_reset = NULL;
3663 	sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb,
3664 	    chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28);
3665 	TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
3666 	asoc->ctrl_queue_cnt--;
3667 	if (chk->data) {
3668 		sctp_m_freem(chk->data);
3669 		chk->data = NULL;
3670 	}
3671 	sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3672 }
3673 
3674 
3675 static int
3676 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3677     uint32_t seq, uint32_t action,
3678     struct sctp_stream_reset_response *respin)
3679 {
3680 	uint16_t type;
3681 	int lparam_len;
3682 	struct sctp_association *asoc = &stcb->asoc;
3683 	struct sctp_tmit_chunk *chk;
3684 	struct sctp_stream_reset_request *req_param;
3685 	struct sctp_stream_reset_out_request *req_out_param;
3686 	struct sctp_stream_reset_in_request *req_in_param;
3687 	uint32_t number_entries;
3688 
3689 	if (asoc->stream_reset_outstanding == 0) {
3690 		/* duplicate */
3691 		return (0);
3692 	}
3693 	if (seq == stcb->asoc.str_reset_seq_out) {
3694 		req_param = sctp_find_stream_reset(stcb, seq, &chk);
3695 		if (req_param != NULL) {
3696 			stcb->asoc.str_reset_seq_out++;
3697 			type = ntohs(req_param->ph.param_type);
3698 			lparam_len = ntohs(req_param->ph.param_length);
3699 			if (type == SCTP_STR_RESET_OUT_REQUEST) {
3700 				int no_clear = 0;
3701 
3702 				req_out_param = (struct sctp_stream_reset_out_request *)req_param;
3703 				number_entries = (lparam_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3704 				asoc->stream_reset_out_is_outstanding = 0;
3705 				if (asoc->stream_reset_outstanding)
3706 					asoc->stream_reset_outstanding--;
3707 				if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3708 					/* do it */
3709 					sctp_reset_out_streams(stcb, number_entries, req_out_param->list_of_streams);
3710 				} else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3711 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3712 				} else if (action == SCTP_STREAM_RESET_RESULT_IN_PROGRESS) {
3713 					/*
3714 					 * Set it up so we don't stop
3715 					 * retransmitting
3716 					 */
3717 					asoc->stream_reset_outstanding++;
3718 					stcb->asoc.str_reset_seq_out--;
3719 					asoc->stream_reset_out_is_outstanding = 1;
3720 					no_clear = 1;
3721 				} else {
3722 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3723 				}
3724 				if (no_clear == 0) {
3725 					sctp_reset_clear_pending(stcb, number_entries, req_out_param->list_of_streams);
3726 				}
3727 			} else if (type == SCTP_STR_RESET_IN_REQUEST) {
3728 				req_in_param = (struct sctp_stream_reset_in_request *)req_param;
3729 				number_entries = (lparam_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3730 				if (asoc->stream_reset_outstanding)
3731 					asoc->stream_reset_outstanding--;
3732 				if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3733 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
3734 					    number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3735 				} else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3736 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
3737 					    number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3738 				}
3739 			} else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3740 				/* Ok we now may have more streams */
3741 				int num_stream;
3742 
3743 				num_stream = stcb->asoc.strm_pending_add_size;
3744 				if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3745 					/* TSNH */
3746 					num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3747 				}
3748 				stcb->asoc.strm_pending_add_size = 0;
3749 				if (asoc->stream_reset_outstanding)
3750 					asoc->stream_reset_outstanding--;
3751 				if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3752 					/* Put the new streams into effect */
3753 					int i;
3754 
3755 					for (i = asoc->streamoutcnt; i < (asoc->streamoutcnt + num_stream); i++) {
3756 						asoc->strmout[i].state = SCTP_STREAM_OPEN;
3757 					}
3758 					asoc->streamoutcnt += num_stream;
3759 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3760 				} else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3761 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3762 					    SCTP_STREAM_CHANGE_DENIED);
3763 				} else {
3764 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3765 					    SCTP_STREAM_CHANGE_FAILED);
3766 				}
3767 			} else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3768 				if (asoc->stream_reset_outstanding)
3769 					asoc->stream_reset_outstanding--;
3770 				if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3771 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3772 					    SCTP_STREAM_CHANGE_DENIED);
3773 				} else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3774 					sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3775 					    SCTP_STREAM_CHANGE_FAILED);
3776 				}
3777 			} else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3778 				/**
3779 				 * a) Adopt the new in tsn.
3780 				 * b) reset the map
3781 				 * c) Adopt the new out-tsn
3782 				 */
3783 				struct sctp_stream_reset_response_tsn *resp;
3784 				struct sctp_forward_tsn_chunk fwdtsn;
3785 				int abort_flag = 0;
3786 
3787 				if (respin == NULL) {
3788 					/* huh ? */
3789 					return (0);
3790 				}
3791 				if (ntohs(respin->ph.param_length) < sizeof(struct sctp_stream_reset_response_tsn)) {
3792 					return (0);
3793 				}
3794 				if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3795 					resp = (struct sctp_stream_reset_response_tsn *)respin;
3796 					asoc->stream_reset_outstanding--;
3797 					fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3798 					fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3799 					fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3800 					sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3801 					if (abort_flag) {
3802 						return (1);
3803 					}
3804 					stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3805 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3806 						sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3807 					}
3808 
3809 					stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3810 					stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3811 					memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3812 
3813 					stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3814 					memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3815 
3816 					stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3817 					stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3818 
3819 					sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3820 					sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3821 					sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3822 				} else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3823 					sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3824 					    SCTP_ASSOC_RESET_DENIED);
3825 				} else {
3826 					sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3827 					    SCTP_ASSOC_RESET_FAILED);
3828 				}
3829 			}
3830 			/* get rid of the request and get the request flags */
3831 			if (asoc->stream_reset_outstanding == 0) {
3832 				sctp_clean_up_stream_reset(stcb);
3833 			}
3834 		}
3835 	}
3836 	if (asoc->stream_reset_outstanding == 0) {
3837 		sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3838 	}
3839 	return (0);
3840 }
3841 
3842 static void
3843 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3844     struct sctp_tmit_chunk *chk,
3845     struct sctp_stream_reset_in_request *req, int trunc)
3846 {
3847 	uint32_t seq;
3848 	int len, i;
3849 	int number_entries;
3850 	uint16_t temp;
3851 
3852 	/*
3853 	 * peer wants me to send a str-reset to him for my outgoing seq's if
3854 	 * seq_in is right.
3855 	 */
3856 	struct sctp_association *asoc = &stcb->asoc;
3857 
3858 	seq = ntohl(req->request_seq);
3859 	if (asoc->str_reset_seq_in == seq) {
3860 		asoc->last_reset_action[1] = asoc->last_reset_action[0];
3861 		if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3862 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3863 		} else if (trunc) {
3864 			/* Can't do it, since they exceeded our buffer size  */
3865 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3866 		} else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3867 			len = ntohs(req->ph.param_length);
3868 			number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3869 			if (number_entries) {
3870 				for (i = 0; i < number_entries; i++) {
3871 					temp = ntohs(req->list_of_streams[i]);
3872 					if (temp >= stcb->asoc.streamoutcnt) {
3873 						asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3874 						goto bad_boy;
3875 					}
3876 					req->list_of_streams[i] = temp;
3877 				}
3878 				for (i = 0; i < number_entries; i++) {
3879 					if (stcb->asoc.strmout[req->list_of_streams[i]].state == SCTP_STREAM_OPEN) {
3880 						stcb->asoc.strmout[req->list_of_streams[i]].state = SCTP_STREAM_RESET_PENDING;
3881 					}
3882 				}
3883 			} else {
3884 				/* Its all */
3885 				for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3886 					if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN)
3887 						stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
3888 				}
3889 			}
3890 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3891 		} else {
3892 			/* Can't do it, since we have sent one out */
3893 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3894 		}
3895 bad_boy:
3896 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3897 		asoc->str_reset_seq_in++;
3898 	} else if (asoc->str_reset_seq_in - 1 == seq) {
3899 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3900 	} else if (asoc->str_reset_seq_in - 2 == seq) {
3901 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3902 	} else {
3903 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3904 	}
3905 	sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3906 }
3907 
3908 static int
3909 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3910     struct sctp_tmit_chunk *chk,
3911     struct sctp_stream_reset_tsn_request *req)
3912 {
3913 	/* reset all in and out and update the tsn */
3914 	/*
3915 	 * A) reset my str-seq's on in and out. B) Select a receive next,
3916 	 * and set cum-ack to it. Also process this selected number as a
3917 	 * fwd-tsn as well. C) set in the response my next sending seq.
3918 	 */
3919 	struct sctp_forward_tsn_chunk fwdtsn;
3920 	struct sctp_association *asoc = &stcb->asoc;
3921 	int abort_flag = 0;
3922 	uint32_t seq;
3923 
3924 	seq = ntohl(req->request_seq);
3925 	if (asoc->str_reset_seq_in == seq) {
3926 		asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
3927 		if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3928 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3929 		} else {
3930 			fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3931 			fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3932 			fwdtsn.ch.chunk_flags = 0;
3933 			fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3934 			sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3935 			if (abort_flag) {
3936 				return (1);
3937 			}
3938 			asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3939 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3940 				sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3941 			}
3942 			asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3943 			asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3944 			memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3945 			asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3946 			memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3947 			atomic_add_int(&asoc->sending_seq, 1);
3948 			/* save off historical data for retrans */
3949 			asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3950 			asoc->last_sending_seq[0] = asoc->sending_seq;
3951 			asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3952 			asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3953 			sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3954 			sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3955 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3956 			sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3957 		}
3958 		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3959 		    asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3960 		asoc->str_reset_seq_in++;
3961 	} else if (asoc->str_reset_seq_in - 1 == seq) {
3962 		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3963 		    asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3964 	} else if (asoc->str_reset_seq_in - 2 == seq) {
3965 		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3966 		    asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3967 	} else {
3968 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3969 	}
3970 	return (0);
3971 }
3972 
3973 static void
3974 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3975     struct sctp_tmit_chunk *chk,
3976     struct sctp_stream_reset_out_request *req, int trunc)
3977 {
3978 	uint32_t seq, tsn;
3979 	int number_entries, len;
3980 	struct sctp_association *asoc = &stcb->asoc;
3981 
3982 	seq = ntohl(req->request_seq);
3983 
3984 	/* now if its not a duplicate we process it */
3985 	if (asoc->str_reset_seq_in == seq) {
3986 		len = ntohs(req->ph.param_length);
3987 		number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3988 		/*
3989 		 * the sender is resetting, handle the list issue.. we must
3990 		 * a) verify if we can do the reset, if so no problem b) If
3991 		 * we can't do the reset we must copy the request. c) queue
3992 		 * it, and setup the data in processor to trigger it off
3993 		 * when needed and dequeue all the queued data.
3994 		 */
3995 		tsn = ntohl(req->send_reset_at_tsn);
3996 
3997 		/* move the reset action back one */
3998 		asoc->last_reset_action[1] = asoc->last_reset_action[0];
3999 		if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
4000 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4001 		} else if (trunc) {
4002 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4003 		} else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
4004 			/* we can do it now */
4005 			sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
4006 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4007 		} else {
4008 			/*
4009 			 * we must queue it up and thus wait for the TSN's
4010 			 * to arrive that are at or before tsn
4011 			 */
4012 			struct sctp_stream_reset_list *liste;
4013 			int siz;
4014 
4015 			siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
4016 			SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
4017 			    siz, SCTP_M_STRESET);
4018 			if (liste == NULL) {
4019 				/* gak out of memory */
4020 				asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4021 				sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4022 				return;
4023 			}
4024 			liste->seq = seq;
4025 			liste->tsn = tsn;
4026 			liste->number_entries = number_entries;
4027 			memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t));
4028 			TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
4029 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_IN_PROGRESS;
4030 		}
4031 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4032 		asoc->str_reset_seq_in++;
4033 	} else if ((asoc->str_reset_seq_in - 1) == seq) {
4034 		/*
4035 		 * one seq back, just echo back last action since my
4036 		 * response was lost.
4037 		 */
4038 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4039 	} else if ((asoc->str_reset_seq_in - 2) == seq) {
4040 		/*
4041 		 * two seq back, just echo back last action since my
4042 		 * response was lost.
4043 		 */
4044 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4045 	} else {
4046 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4047 	}
4048 }
4049 
4050 static void
4051 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4052     struct sctp_stream_reset_add_strm *str_add)
4053 {
4054 	/*
4055 	 * Peer is requesting to add more streams. If its within our
4056 	 * max-streams we will allow it.
4057 	 */
4058 	uint32_t num_stream, i;
4059 	uint32_t seq;
4060 	struct sctp_association *asoc = &stcb->asoc;
4061 	struct sctp_queued_to_read *ctl, *nctl;
4062 
4063 	/* Get the number. */
4064 	seq = ntohl(str_add->request_seq);
4065 	num_stream = ntohs(str_add->number_of_streams);
4066 	/* Now what would be the new total? */
4067 	if (asoc->str_reset_seq_in == seq) {
4068 		num_stream += stcb->asoc.streamincnt;
4069 		stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4070 		if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4071 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4072 		} else if ((num_stream > stcb->asoc.max_inbound_streams) ||
4073 		    (num_stream > 0xffff)) {
4074 			/* We must reject it they ask for to many */
4075 	denied:
4076 			stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4077 		} else {
4078 			/* Ok, we can do that :-) */
4079 			struct sctp_stream_in *oldstrm;
4080 
4081 			/* save off the old */
4082 			oldstrm = stcb->asoc.strmin;
4083 			SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
4084 			    (num_stream * sizeof(struct sctp_stream_in)),
4085 			    SCTP_M_STRMI);
4086 			if (stcb->asoc.strmin == NULL) {
4087 				stcb->asoc.strmin = oldstrm;
4088 				goto denied;
4089 			}
4090 			/* copy off the old data */
4091 			for (i = 0; i < stcb->asoc.streamincnt; i++) {
4092 				TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4093 				TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
4094 				stcb->asoc.strmin[i].sid = i;
4095 				stcb->asoc.strmin[i].last_mid_delivered = oldstrm[i].last_mid_delivered;
4096 				stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
4097 				stcb->asoc.strmin[i].pd_api_started = oldstrm[i].pd_api_started;
4098 				/* now anything on those queues? */
4099 				TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next_instrm, nctl) {
4100 					TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next_instrm);
4101 					TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next_instrm);
4102 				}
4103 				TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].uno_inqueue, next_instrm, nctl) {
4104 					TAILQ_REMOVE(&oldstrm[i].uno_inqueue, ctl, next_instrm);
4105 					TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].uno_inqueue, ctl, next_instrm);
4106 				}
4107 			}
4108 			/* Init the new streams */
4109 			for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
4110 				TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4111 				TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
4112 				stcb->asoc.strmin[i].sid = i;
4113 				stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
4114 				stcb->asoc.strmin[i].pd_api_started = 0;
4115 				stcb->asoc.strmin[i].delivery_started = 0;
4116 			}
4117 			SCTP_FREE(oldstrm, SCTP_M_STRMI);
4118 			/* update the size */
4119 			stcb->asoc.streamincnt = num_stream;
4120 			stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4121 			sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
4122 		}
4123 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4124 		asoc->str_reset_seq_in++;
4125 	} else if ((asoc->str_reset_seq_in - 1) == seq) {
4126 		/*
4127 		 * one seq back, just echo back last action since my
4128 		 * response was lost.
4129 		 */
4130 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4131 	} else if ((asoc->str_reset_seq_in - 2) == seq) {
4132 		/*
4133 		 * two seq back, just echo back last action since my
4134 		 * response was lost.
4135 		 */
4136 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4137 	} else {
4138 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4139 
4140 	}
4141 }
4142 
4143 static void
4144 sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4145     struct sctp_stream_reset_add_strm *str_add)
4146 {
4147 	/*
4148 	 * Peer is requesting to add more streams. If its within our
4149 	 * max-streams we will allow it.
4150 	 */
4151 	uint16_t num_stream;
4152 	uint32_t seq;
4153 	struct sctp_association *asoc = &stcb->asoc;
4154 
4155 	/* Get the number. */
4156 	seq = ntohl(str_add->request_seq);
4157 	num_stream = ntohs(str_add->number_of_streams);
4158 	/* Now what would be the new total? */
4159 	if (asoc->str_reset_seq_in == seq) {
4160 		stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4161 		if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4162 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4163 		} else if (stcb->asoc.stream_reset_outstanding) {
4164 			/* We must reject it we have something pending */
4165 			stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
4166 		} else {
4167 			/* Ok, we can do that :-) */
4168 			int mychk;
4169 
4170 			mychk = stcb->asoc.streamoutcnt;
4171 			mychk += num_stream;
4172 			if (mychk < 0x10000) {
4173 				stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4174 				if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 1, num_stream, 0, 1)) {
4175 					stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4176 				}
4177 			} else {
4178 				stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4179 			}
4180 		}
4181 		sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
4182 		asoc->str_reset_seq_in++;
4183 	} else if ((asoc->str_reset_seq_in - 1) == seq) {
4184 		/*
4185 		 * one seq back, just echo back last action since my
4186 		 * response was lost.
4187 		 */
4188 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4189 	} else if ((asoc->str_reset_seq_in - 2) == seq) {
4190 		/*
4191 		 * two seq back, just echo back last action since my
4192 		 * response was lost.
4193 		 */
4194 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4195 	} else {
4196 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4197 	}
4198 }
4199 
4200 #ifdef __GNUC__
4201 __attribute__((noinline))
4202 #endif
4203 static int
4204 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
4205     struct sctp_chunkhdr *ch_req)
4206 {
4207 	uint16_t remaining_length, param_len, ptype;
4208 	struct sctp_paramhdr pstore;
4209 	uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
4210 	uint32_t seq = 0;
4211 	int num_req = 0;
4212 	int trunc = 0;
4213 	struct sctp_tmit_chunk *chk;
4214 	struct sctp_chunkhdr *ch;
4215 	struct sctp_paramhdr *ph;
4216 	int ret_code = 0;
4217 	int num_param = 0;
4218 
4219 	/* now it may be a reset or a reset-response */
4220 	remaining_length = ntohs(ch_req->chunk_length) - sizeof(struct sctp_chunkhdr);
4221 
4222 	/* setup for adding the response */
4223 	sctp_alloc_a_chunk(stcb, chk);
4224 	if (chk == NULL) {
4225 		return (ret_code);
4226 	}
4227 	chk->copy_by_ref = 0;
4228 	chk->rec.chunk_id.id = SCTP_STREAM_RESET;
4229 	chk->rec.chunk_id.can_take_data = 0;
4230 	chk->flags = 0;
4231 	chk->asoc = &stcb->asoc;
4232 	chk->no_fr_allowed = 0;
4233 	chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
4234 	chk->book_size_scale = 0;
4235 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
4236 	if (chk->data == NULL) {
4237 strres_nochunk:
4238 		if (chk->data) {
4239 			sctp_m_freem(chk->data);
4240 			chk->data = NULL;
4241 		}
4242 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4243 		return (ret_code);
4244 	}
4245 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4246 
4247 	/* setup chunk parameters */
4248 	chk->sent = SCTP_DATAGRAM_UNSENT;
4249 	chk->snd_count = 0;
4250 	chk->whoTo = NULL;
4251 
4252 	ch = mtod(chk->data, struct sctp_chunkhdr *);
4253 	ch->chunk_type = SCTP_STREAM_RESET;
4254 	ch->chunk_flags = 0;
4255 	ch->chunk_length = htons(chk->send_size);
4256 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4257 	offset += sizeof(struct sctp_chunkhdr);
4258 	while (remaining_length >= sizeof(struct sctp_paramhdr)) {
4259 		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *)&pstore);
4260 		if (ph == NULL) {
4261 			/* TSNH */
4262 			break;
4263 		}
4264 		param_len = ntohs(ph->param_length);
4265 		if ((param_len > remaining_length) ||
4266 		    (param_len < (sizeof(struct sctp_paramhdr) + sizeof(uint32_t)))) {
4267 			/* bad parameter length */
4268 			break;
4269 		}
4270 		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, sizeof(cstore)),
4271 		    (uint8_t *)&cstore);
4272 		if (ph == NULL) {
4273 			/* TSNH */
4274 			break;
4275 		}
4276 		ptype = ntohs(ph->param_type);
4277 		num_param++;
4278 		if (param_len > sizeof(cstore)) {
4279 			trunc = 1;
4280 		} else {
4281 			trunc = 0;
4282 		}
4283 		if (num_param > SCTP_MAX_RESET_PARAMS) {
4284 			/* hit the max of parameters already sorry.. */
4285 			break;
4286 		}
4287 		if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4288 			struct sctp_stream_reset_out_request *req_out;
4289 
4290 			if (param_len < sizeof(struct sctp_stream_reset_out_request)) {
4291 				break;
4292 			}
4293 			req_out = (struct sctp_stream_reset_out_request *)ph;
4294 			num_req++;
4295 			if (stcb->asoc.stream_reset_outstanding) {
4296 				seq = ntohl(req_out->response_seq);
4297 				if (seq == stcb->asoc.str_reset_seq_out) {
4298 					/* implicit ack */
4299 					(void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4300 				}
4301 			}
4302 			sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4303 		} else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4304 			struct sctp_stream_reset_add_strm *str_add;
4305 
4306 			if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
4307 				break;
4308 			}
4309 			str_add = (struct sctp_stream_reset_add_strm *)ph;
4310 			num_req++;
4311 			sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4312 		} else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4313 			struct sctp_stream_reset_add_strm *str_add;
4314 
4315 			if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
4316 				break;
4317 			}
4318 			str_add = (struct sctp_stream_reset_add_strm *)ph;
4319 			num_req++;
4320 			sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4321 		} else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4322 			struct sctp_stream_reset_in_request *req_in;
4323 
4324 			num_req++;
4325 			req_in = (struct sctp_stream_reset_in_request *)ph;
4326 			sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4327 		} else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4328 			struct sctp_stream_reset_tsn_request *req_tsn;
4329 
4330 			num_req++;
4331 			req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4332 			if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4333 				ret_code = 1;
4334 				goto strres_nochunk;
4335 			}
4336 			/* no more */
4337 			break;
4338 		} else if (ptype == SCTP_STR_RESET_RESPONSE) {
4339 			struct sctp_stream_reset_response *resp;
4340 			uint32_t result;
4341 
4342 			if (param_len < sizeof(struct sctp_stream_reset_response)) {
4343 				break;
4344 			}
4345 			resp = (struct sctp_stream_reset_response *)ph;
4346 			seq = ntohl(resp->response_seq);
4347 			result = ntohl(resp->result);
4348 			if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4349 				ret_code = 1;
4350 				goto strres_nochunk;
4351 			}
4352 		} else {
4353 			break;
4354 		}
4355 		offset += SCTP_SIZE32(param_len);
4356 		if (remaining_length >= SCTP_SIZE32(param_len)) {
4357 			remaining_length -= SCTP_SIZE32(param_len);
4358 		} else {
4359 			remaining_length = 0;
4360 		}
4361 	}
4362 	if (num_req == 0) {
4363 		/* we have no response free the stuff */
4364 		goto strres_nochunk;
4365 	}
4366 	/* ok we have a chunk to link in */
4367 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4368 	    chk,
4369 	    sctp_next);
4370 	stcb->asoc.ctrl_queue_cnt++;
4371 	return (ret_code);
4372 }
4373 
4374 /*
4375  * Handle a router or endpoints report of a packet loss, there are two ways
4376  * to handle this, either we get the whole packet and must disect it
4377  * ourselves (possibly with truncation and or corruption) or it is a summary
4378  * from a middle box that did the disectting for us.
4379  */
4380 static void
4381 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4382     struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4383 {
4384 	uint32_t bottle_bw, on_queue;
4385 	uint16_t trunc_len;
4386 	unsigned int chlen;
4387 	unsigned int at;
4388 	struct sctp_chunk_desc desc;
4389 	struct sctp_chunkhdr *ch;
4390 
4391 	chlen = ntohs(cp->ch.chunk_length);
4392 	chlen -= sizeof(struct sctp_pktdrop_chunk);
4393 	/* XXX possible chlen underflow */
4394 	if (chlen == 0) {
4395 		ch = NULL;
4396 		if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4397 			SCTP_STAT_INCR(sctps_pdrpbwrpt);
4398 	} else {
4399 		ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4400 		chlen -= sizeof(struct sctphdr);
4401 		/* XXX possible chlen underflow */
4402 		memset(&desc, 0, sizeof(desc));
4403 	}
4404 	trunc_len = (uint16_t)ntohs(cp->trunc_len);
4405 	if (trunc_len > limit) {
4406 		trunc_len = limit;
4407 	}
4408 
4409 	/* now the chunks themselves */
4410 	while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4411 		desc.chunk_type = ch->chunk_type;
4412 		/* get amount we need to move */
4413 		at = ntohs(ch->chunk_length);
4414 		if (at < sizeof(struct sctp_chunkhdr)) {
4415 			/* corrupt chunk, maybe at the end? */
4416 			SCTP_STAT_INCR(sctps_pdrpcrupt);
4417 			break;
4418 		}
4419 		if (trunc_len == 0) {
4420 			/* we are supposed to have all of it */
4421 			if (at > chlen) {
4422 				/* corrupt skip it */
4423 				SCTP_STAT_INCR(sctps_pdrpcrupt);
4424 				break;
4425 			}
4426 		} else {
4427 			/* is there enough of it left ? */
4428 			if (desc.chunk_type == SCTP_DATA) {
4429 				if (chlen < (sizeof(struct sctp_data_chunk) +
4430 				    sizeof(desc.data_bytes))) {
4431 					break;
4432 				}
4433 			} else {
4434 				if (chlen < sizeof(struct sctp_chunkhdr)) {
4435 					break;
4436 				}
4437 			}
4438 		}
4439 		if (desc.chunk_type == SCTP_DATA) {
4440 			/* can we get out the tsn? */
4441 			if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4442 				SCTP_STAT_INCR(sctps_pdrpmbda);
4443 
4444 			if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4445 				/* yep */
4446 				struct sctp_data_chunk *dcp;
4447 				uint8_t *ddp;
4448 				unsigned int iii;
4449 
4450 				dcp = (struct sctp_data_chunk *)ch;
4451 				ddp = (uint8_t *)(dcp + 1);
4452 				for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4453 					desc.data_bytes[iii] = ddp[iii];
4454 				}
4455 				desc.tsn_ifany = dcp->dp.tsn;
4456 			} else {
4457 				/* nope we are done. */
4458 				SCTP_STAT_INCR(sctps_pdrpnedat);
4459 				break;
4460 			}
4461 		} else {
4462 			if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4463 				SCTP_STAT_INCR(sctps_pdrpmbct);
4464 		}
4465 
4466 		if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4467 			SCTP_STAT_INCR(sctps_pdrppdbrk);
4468 			break;
4469 		}
4470 		if (SCTP_SIZE32(at) > chlen) {
4471 			break;
4472 		}
4473 		chlen -= SCTP_SIZE32(at);
4474 		if (chlen < sizeof(struct sctp_chunkhdr)) {
4475 			/* done, none left */
4476 			break;
4477 		}
4478 		ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4479 	}
4480 	/* Now update any rwnd --- possibly */
4481 	if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4482 		/* From a peer, we get a rwnd report */
4483 		uint32_t a_rwnd;
4484 
4485 		SCTP_STAT_INCR(sctps_pdrpfehos);
4486 
4487 		bottle_bw = ntohl(cp->bottle_bw);
4488 		on_queue = ntohl(cp->current_onq);
4489 		if (bottle_bw && on_queue) {
4490 			/* a rwnd report is in here */
4491 			if (bottle_bw > on_queue)
4492 				a_rwnd = bottle_bw - on_queue;
4493 			else
4494 				a_rwnd = 0;
4495 
4496 			if (a_rwnd == 0)
4497 				stcb->asoc.peers_rwnd = 0;
4498 			else {
4499 				if (a_rwnd > stcb->asoc.total_flight) {
4500 					stcb->asoc.peers_rwnd =
4501 					    a_rwnd - stcb->asoc.total_flight;
4502 				} else {
4503 					stcb->asoc.peers_rwnd = 0;
4504 				}
4505 				if (stcb->asoc.peers_rwnd <
4506 				    stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4507 					/* SWS sender side engages */
4508 					stcb->asoc.peers_rwnd = 0;
4509 				}
4510 			}
4511 		}
4512 	} else {
4513 		SCTP_STAT_INCR(sctps_pdrpfmbox);
4514 	}
4515 
4516 	/* now middle boxes in sat networks get a cwnd bump */
4517 	if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4518 	    (stcb->asoc.sat_t3_loss_recovery == 0) &&
4519 	    (stcb->asoc.sat_network)) {
4520 		/*
4521 		 * This is debatable but for sat networks it makes sense
4522 		 * Note if a T3 timer has went off, we will prohibit any
4523 		 * changes to cwnd until we exit the t3 loss recovery.
4524 		 */
4525 		stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4526 		    net, cp, &bottle_bw, &on_queue);
4527 	}
4528 }
4529 
4530 /*
4531  * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4532  * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4533  * offset: offset into the mbuf chain to first chunkhdr - length: is the
4534  * length of the complete packet outputs: - length: modified to remaining
4535  * length after control processing - netp: modified to new sctp_nets after
4536  * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4537  * bad packet,...) otherwise return the tcb for this packet
4538  */
4539 #ifdef __GNUC__
4540 __attribute__((noinline))
4541 #endif
4542 static struct sctp_tcb *
4543 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4544     struct sockaddr *src, struct sockaddr *dst,
4545     struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4546     struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4547     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4548     uint32_t vrf_id, uint16_t port)
4549 {
4550 	struct sctp_association *asoc;
4551 	struct mbuf *op_err;
4552 	char msg[SCTP_DIAG_INFO_LEN];
4553 	uint32_t vtag_in;
4554 	int num_chunks = 0;	/* number of control chunks processed */
4555 	uint32_t chk_length, contiguous;
4556 	int ret;
4557 	int abort_no_unlock = 0;
4558 	int ecne_seen = 0;
4559 
4560 	/*
4561 	 * How big should this be, and should it be alloc'd? Lets try the
4562 	 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4563 	 * until we get into jumbo grams and such..
4564 	 */
4565 	uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4566 	int got_auth = 0;
4567 	uint32_t auth_offset = 0, auth_len = 0;
4568 	int auth_skipped = 0;
4569 	int asconf_cnt = 0;
4570 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4571 	struct socket *so;
4572 #endif
4573 
4574 	SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4575 	    iphlen, *offset, length, (void *)stcb);
4576 
4577 	if (stcb) {
4578 		SCTP_TCB_LOCK_ASSERT(stcb);
4579 	}
4580 	/* validate chunk header length... */
4581 	if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4582 		SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4583 		    ntohs(ch->chunk_length));
4584 		*offset = length;
4585 		return (stcb);
4586 	}
4587 	/*
4588 	 * validate the verification tag
4589 	 */
4590 	vtag_in = ntohl(sh->v_tag);
4591 
4592 	if (ch->chunk_type == SCTP_INITIATION) {
4593 		SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4594 		    ntohs(ch->chunk_length), vtag_in);
4595 		if (vtag_in != 0) {
4596 			/* protocol error- silently discard... */
4597 			SCTP_STAT_INCR(sctps_badvtag);
4598 			if (stcb != NULL) {
4599 				SCTP_TCB_UNLOCK(stcb);
4600 			}
4601 			return (NULL);
4602 		}
4603 	} else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4604 		/*
4605 		 * If there is no stcb, skip the AUTH chunk and process
4606 		 * later after a stcb is found (to validate the lookup was
4607 		 * valid.
4608 		 */
4609 		if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4610 		    (stcb == NULL) &&
4611 		    (inp->auth_supported == 1)) {
4612 			/* save this chunk for later processing */
4613 			auth_skipped = 1;
4614 			auth_offset = *offset;
4615 			auth_len = ntohs(ch->chunk_length);
4616 
4617 			/* (temporarily) move past this chunk */
4618 			*offset += SCTP_SIZE32(auth_len);
4619 			if (*offset >= length) {
4620 				/* no more data left in the mbuf chain */
4621 				*offset = length;
4622 				return (NULL);
4623 			}
4624 			ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4625 			    sizeof(struct sctp_chunkhdr), chunk_buf);
4626 		}
4627 		if (ch == NULL) {
4628 			/* Help */
4629 			*offset = length;
4630 			return (stcb);
4631 		}
4632 		if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4633 			goto process_control_chunks;
4634 		}
4635 		/*
4636 		 * first check if it's an ASCONF with an unknown src addr we
4637 		 * need to look inside to find the association
4638 		 */
4639 		if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4640 			struct sctp_chunkhdr *asconf_ch = ch;
4641 			uint32_t asconf_offset = 0, asconf_len = 0;
4642 
4643 			/* inp's refcount may be reduced */
4644 			SCTP_INP_INCR_REF(inp);
4645 
4646 			asconf_offset = *offset;
4647 			do {
4648 				asconf_len = ntohs(asconf_ch->chunk_length);
4649 				if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4650 					break;
4651 				stcb = sctp_findassociation_ep_asconf(m,
4652 				    *offset,
4653 				    dst,
4654 				    sh, &inp, netp, vrf_id);
4655 				if (stcb != NULL)
4656 					break;
4657 				asconf_offset += SCTP_SIZE32(asconf_len);
4658 				asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4659 				    sizeof(struct sctp_chunkhdr), chunk_buf);
4660 			} while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4661 			if (stcb == NULL) {
4662 				/*
4663 				 * reduce inp's refcount if not reduced in
4664 				 * sctp_findassociation_ep_asconf().
4665 				 */
4666 				SCTP_INP_DECR_REF(inp);
4667 			}
4668 
4669 			/* now go back and verify any auth chunk to be sure */
4670 			if (auth_skipped && (stcb != NULL)) {
4671 				struct sctp_auth_chunk *auth;
4672 
4673 				auth = (struct sctp_auth_chunk *)
4674 				    sctp_m_getptr(m, auth_offset,
4675 				    auth_len, chunk_buf);
4676 				got_auth = 1;
4677 				auth_skipped = 0;
4678 				if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4679 				    auth_offset)) {
4680 					/* auth HMAC failed so dump it */
4681 					*offset = length;
4682 					return (stcb);
4683 				} else {
4684 					/* remaining chunks are HMAC checked */
4685 					stcb->asoc.authenticated = 1;
4686 				}
4687 			}
4688 		}
4689 		if (stcb == NULL) {
4690 			snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4691 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4692 			    msg);
4693 			/* no association, so it's out of the blue... */
4694 			sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp, op_err,
4695 			    mflowtype, mflowid, inp->fibnum,
4696 			    vrf_id, port);
4697 			*offset = length;
4698 			return (NULL);
4699 		}
4700 		asoc = &stcb->asoc;
4701 		/* ABORT and SHUTDOWN can use either v_tag... */
4702 		if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4703 		    (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4704 		    (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4705 			/* Take the T-bit always into account. */
4706 			if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) &&
4707 			    (vtag_in == asoc->my_vtag)) ||
4708 			    (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) &&
4709 			    (asoc->peer_vtag != htonl(0)) &&
4710 			    (vtag_in == asoc->peer_vtag))) {
4711 				/* this is valid */
4712 			} else {
4713 				/* drop this packet... */
4714 				SCTP_STAT_INCR(sctps_badvtag);
4715 				if (stcb != NULL) {
4716 					SCTP_TCB_UNLOCK(stcb);
4717 				}
4718 				return (NULL);
4719 			}
4720 		} else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4721 			if (vtag_in != asoc->my_vtag) {
4722 				/*
4723 				 * this could be a stale SHUTDOWN-ACK or the
4724 				 * peer never got the SHUTDOWN-COMPLETE and
4725 				 * is still hung; we have started a new asoc
4726 				 * but it won't complete until the shutdown
4727 				 * is completed
4728 				 */
4729 				if (stcb != NULL) {
4730 					SCTP_TCB_UNLOCK(stcb);
4731 				}
4732 				snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4733 				op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4734 				    msg);
4735 				sctp_handle_ootb(m, iphlen, *offset, src, dst,
4736 				    sh, inp, op_err,
4737 				    mflowtype, mflowid, fibnum,
4738 				    vrf_id, port);
4739 				return (NULL);
4740 			}
4741 		} else {
4742 			/* for all other chunks, vtag must match */
4743 			if (vtag_in != asoc->my_vtag) {
4744 				/* invalid vtag... */
4745 				SCTPDBG(SCTP_DEBUG_INPUT3,
4746 				    "invalid vtag: %xh, expect %xh\n",
4747 				    vtag_in, asoc->my_vtag);
4748 				SCTP_STAT_INCR(sctps_badvtag);
4749 				if (stcb != NULL) {
4750 					SCTP_TCB_UNLOCK(stcb);
4751 				}
4752 				*offset = length;
4753 				return (NULL);
4754 			}
4755 		}
4756 	}			/* end if !SCTP_COOKIE_ECHO */
4757 	/*
4758 	 * process all control chunks...
4759 	 */
4760 	if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4761 	    (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4762 	    (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4763 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
4764 		/* implied cookie-ack.. we must have lost the ack */
4765 		sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4766 		    *netp);
4767 	}
4768 
4769 process_control_chunks:
4770 	while (IS_SCTP_CONTROL(ch)) {
4771 		/* validate chunk length */
4772 		chk_length = ntohs(ch->chunk_length);
4773 		SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4774 		    ch->chunk_type, chk_length);
4775 		SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4776 		if (chk_length < sizeof(*ch) ||
4777 		    (*offset + (int)chk_length) > length) {
4778 			*offset = length;
4779 			return (stcb);
4780 		}
4781 		SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4782 		/*
4783 		 * INIT and INIT-ACK only gets the init ack "header" portion
4784 		 * only because we don't have to process the peer's COOKIE.
4785 		 * All others get a complete chunk.
4786 		 */
4787 		switch (ch->chunk_type) {
4788 		case SCTP_INITIATION:
4789 			contiguous = sizeof(struct sctp_init_chunk);
4790 			break;
4791 		case SCTP_INITIATION_ACK:
4792 			contiguous = sizeof(struct sctp_init_ack_chunk);
4793 			break;
4794 		default:
4795 			contiguous = min(chk_length, sizeof(chunk_buf));
4796 			break;
4797 		}
4798 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4799 		    contiguous,
4800 		    chunk_buf);
4801 		if (ch == NULL) {
4802 			*offset = length;
4803 			if (stcb != NULL) {
4804 				SCTP_TCB_UNLOCK(stcb);
4805 			}
4806 			return (NULL);
4807 		}
4808 
4809 		num_chunks++;
4810 		/* Save off the last place we got a control from */
4811 		if (stcb != NULL) {
4812 			if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4813 				/*
4814 				 * allow last_control to be NULL if
4815 				 * ASCONF... ASCONF processing will find the
4816 				 * right net later
4817 				 */
4818 				if ((netp != NULL) && (*netp != NULL))
4819 					stcb->asoc.last_control_chunk_from = *netp;
4820 			}
4821 		}
4822 #ifdef SCTP_AUDITING_ENABLED
4823 		sctp_audit_log(0xB0, ch->chunk_type);
4824 #endif
4825 
4826 		/* check to see if this chunk required auth, but isn't */
4827 		if ((stcb != NULL) &&
4828 		    sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4829 		    !stcb->asoc.authenticated) {
4830 			/* "silently" ignore */
4831 			SCTP_STAT_INCR(sctps_recvauthmissing);
4832 			goto next_chunk;
4833 		}
4834 		switch (ch->chunk_type) {
4835 		case SCTP_INITIATION:
4836 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4837 			/* The INIT chunk must be the only chunk. */
4838 			if ((num_chunks > 1) ||
4839 			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4840 				/* RFC 4960 requires that no ABORT is sent */
4841 				*offset = length;
4842 				if (stcb != NULL) {
4843 					SCTP_TCB_UNLOCK(stcb);
4844 				}
4845 				return (NULL);
4846 			}
4847 			/* Honor our resource limit. */
4848 			if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4849 				op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4850 				sctp_abort_association(inp, stcb, m, iphlen,
4851 				    src, dst, sh, op_err,
4852 				    mflowtype, mflowid,
4853 				    vrf_id, port);
4854 				*offset = length;
4855 				return (NULL);
4856 			}
4857 			sctp_handle_init(m, iphlen, *offset, src, dst, sh,
4858 			    (struct sctp_init_chunk *)ch, inp,
4859 			    stcb, *netp, &abort_no_unlock,
4860 			    mflowtype, mflowid,
4861 			    vrf_id, port);
4862 			*offset = length;
4863 			if ((!abort_no_unlock) && (stcb != NULL)) {
4864 				SCTP_TCB_UNLOCK(stcb);
4865 			}
4866 			return (NULL);
4867 			break;
4868 		case SCTP_PAD_CHUNK:
4869 			break;
4870 		case SCTP_INITIATION_ACK:
4871 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT_ACK\n");
4872 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4873 				/* We are not interested anymore */
4874 				if ((stcb != NULL) && (stcb->asoc.total_output_queue_size)) {
4875 					;
4876 				} else {
4877 					*offset = length;
4878 					if (stcb != NULL) {
4879 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4880 						so = SCTP_INP_SO(inp);
4881 						atomic_add_int(&stcb->asoc.refcnt, 1);
4882 						SCTP_TCB_UNLOCK(stcb);
4883 						SCTP_SOCKET_LOCK(so, 1);
4884 						SCTP_TCB_LOCK(stcb);
4885 						atomic_subtract_int(&stcb->asoc.refcnt, 1);
4886 #endif
4887 						(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4888 						    SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
4889 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4890 						SCTP_SOCKET_UNLOCK(so, 1);
4891 #endif
4892 					}
4893 					return (NULL);
4894 				}
4895 			}
4896 			/* The INIT-ACK chunk must be the only chunk. */
4897 			if ((num_chunks > 1) ||
4898 			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4899 				*offset = length;
4900 				return (stcb);
4901 			}
4902 			if ((netp != NULL) && (*netp != NULL)) {
4903 				ret = sctp_handle_init_ack(m, iphlen, *offset,
4904 				    src, dst, sh,
4905 				    (struct sctp_init_ack_chunk *)ch,
4906 				    stcb, *netp,
4907 				    &abort_no_unlock,
4908 				    mflowtype, mflowid,
4909 				    vrf_id);
4910 			} else {
4911 				ret = -1;
4912 			}
4913 			*offset = length;
4914 			if (abort_no_unlock) {
4915 				return (NULL);
4916 			}
4917 			/*
4918 			 * Special case, I must call the output routine to
4919 			 * get the cookie echoed
4920 			 */
4921 			if ((stcb != NULL) && (ret == 0)) {
4922 				sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4923 			}
4924 			return (stcb);
4925 			break;
4926 		case SCTP_SELECTIVE_ACK:
4927 		case SCTP_NR_SELECTIVE_ACK:
4928 			{
4929 				int abort_now = 0;
4930 				uint32_t a_rwnd, cum_ack;
4931 				uint16_t num_seg, num_nr_seg, num_dup;
4932 				uint8_t flags;
4933 				int offset_seg, offset_dup;
4934 
4935 				SCTPDBG(SCTP_DEBUG_INPUT3, "%s\n",
4936 				    ch->chunk_type == SCTP_SELECTIVE_ACK ? "SCTP_SACK" : "SCTP_NR_SACK");
4937 				SCTP_STAT_INCR(sctps_recvsacks);
4938 				if (stcb == NULL) {
4939 					SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing %s chunk\n",
4940 					    (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK");
4941 					break;
4942 				}
4943 				if (ch->chunk_type == SCTP_SELECTIVE_ACK) {
4944 					if (chk_length < sizeof(struct sctp_sack_chunk)) {
4945 						SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4946 						break;
4947 					}
4948 				} else {
4949 					if (stcb->asoc.nrsack_supported == 0) {
4950 						goto unknown_chunk;
4951 					}
4952 					if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
4953 						SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR_SACK chunk, too small\n");
4954 						break;
4955 					}
4956 				}
4957 				if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4958 					/*-
4959 					 * If we have sent a shutdown-ack, we will pay no
4960 					 * attention to a sack sent in to us since
4961 					 * we don't care anymore.
4962 					 */
4963 					break;
4964 				}
4965 				flags = ch->chunk_flags;
4966 				if (ch->chunk_type == SCTP_SELECTIVE_ACK) {
4967 					struct sctp_sack_chunk *sack;
4968 
4969 					sack = (struct sctp_sack_chunk *)ch;
4970 					cum_ack = ntohl(sack->sack.cum_tsn_ack);
4971 					num_seg = ntohs(sack->sack.num_gap_ack_blks);
4972 					num_nr_seg = 0;
4973 					num_dup = ntohs(sack->sack.num_dup_tsns);
4974 					a_rwnd = ntohl(sack->sack.a_rwnd);
4975 					if (sizeof(struct sctp_sack_chunk) +
4976 					    num_seg * sizeof(struct sctp_gap_ack_block) +
4977 					    num_dup * sizeof(uint32_t) != chk_length) {
4978 						SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4979 						break;
4980 					}
4981 					offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4982 					offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4983 				} else {
4984 					struct sctp_nr_sack_chunk *nr_sack;
4985 
4986 					nr_sack = (struct sctp_nr_sack_chunk *)ch;
4987 					cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
4988 					num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
4989 					num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4990 					num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
4991 					a_rwnd = ntohl(nr_sack->nr_sack.a_rwnd);
4992 					if (sizeof(struct sctp_nr_sack_chunk) +
4993 					    (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
4994 					    num_dup * sizeof(uint32_t) != chk_length) {
4995 						SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
4996 						break;
4997 					}
4998 					offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
4999 					offset_dup = offset_seg + (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block);
5000 				}
5001 				SCTPDBG(SCTP_DEBUG_INPUT3, "%s process cum_ack:%x num_seg:%d a_rwnd:%d\n",
5002 				    (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK",
5003 				    cum_ack, num_seg, a_rwnd);
5004 				stcb->asoc.seen_a_sack_this_pkt = 1;
5005 				if ((stcb->asoc.pr_sctp_cnt == 0) &&
5006 				    (num_seg == 0) && (num_nr_seg == 0) &&
5007 				    SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
5008 				    (stcb->asoc.saw_sack_with_frags == 0) &&
5009 				    (stcb->asoc.saw_sack_with_nr_frags == 0) &&
5010 				    (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
5011 					/*
5012 					 * We have a SIMPLE sack having no
5013 					 * prior segments and data on sent
5014 					 * queue to be acked. Use the faster
5015 					 * path sack processing. We also
5016 					 * allow window update sacks with no
5017 					 * missing segments to go this way
5018 					 * too.
5019 					 */
5020 					sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
5021 					    &abort_now, ecne_seen);
5022 				} else {
5023 					if ((netp != NULL) && (*netp != NULL)) {
5024 						sctp_handle_sack(m, offset_seg, offset_dup, stcb,
5025 						    num_seg, num_nr_seg, num_dup, &abort_now, flags,
5026 						    cum_ack, a_rwnd, ecne_seen);
5027 					}
5028 				}
5029 				if (abort_now) {
5030 					/* ABORT signal from sack processing */
5031 					*offset = length;
5032 					return (NULL);
5033 				}
5034 				if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5035 				    TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5036 				    (stcb->asoc.stream_queue_cnt == 0)) {
5037 					sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
5038 				}
5039 				break;
5040 			}
5041 		case SCTP_HEARTBEAT_REQUEST:
5042 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
5043 			if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5044 				SCTP_STAT_INCR(sctps_recvheartbeat);
5045 				sctp_send_heartbeat_ack(stcb, m, *offset,
5046 				    chk_length, *netp);
5047 			}
5048 			break;
5049 		case SCTP_HEARTBEAT_ACK:
5050 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT_ACK\n");
5051 			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
5052 				/* Its not ours */
5053 				*offset = length;
5054 				return (stcb);
5055 			}
5056 			SCTP_STAT_INCR(sctps_recvheartbeatack);
5057 			if ((netp != NULL) && (*netp != NULL)) {
5058 				sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
5059 				    stcb, *netp);
5060 			}
5061 			break;
5062 		case SCTP_ABORT_ASSOCIATION:
5063 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
5064 			    (void *)stcb);
5065 			*offset = length;
5066 			if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5067 				if (sctp_handle_abort((struct sctp_abort_chunk *)ch, stcb, *netp)) {
5068 					return (NULL);
5069 				} else {
5070 					return (stcb);
5071 				}
5072 			} else {
5073 				return (NULL);
5074 			}
5075 			break;
5076 		case SCTP_SHUTDOWN:
5077 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
5078 			    (void *)stcb);
5079 			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
5080 				*offset = length;
5081 				return (stcb);
5082 			}
5083 			if ((netp != NULL) && (*netp != NULL)) {
5084 				int abort_flag = 0;
5085 
5086 				sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
5087 				    stcb, *netp, &abort_flag);
5088 				if (abort_flag) {
5089 					*offset = length;
5090 					return (NULL);
5091 				}
5092 			}
5093 			break;
5094 		case SCTP_SHUTDOWN_ACK:
5095 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_ACK, stcb %p\n", (void *)stcb);
5096 			if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5097 				sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
5098 			}
5099 			*offset = length;
5100 			return (NULL);
5101 			break;
5102 		case SCTP_OPERATION_ERROR:
5103 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP_ERR\n");
5104 			if ((stcb != NULL) && (netp != NULL) && (*netp != NULL) &&
5105 			    sctp_handle_error(ch, stcb, *netp, contiguous) < 0) {
5106 				*offset = length;
5107 				return (NULL);
5108 			}
5109 			break;
5110 		case SCTP_COOKIE_ECHO:
5111 			SCTPDBG(SCTP_DEBUG_INPUT3,
5112 			    "SCTP_COOKIE_ECHO, stcb %p\n", (void *)stcb);
5113 			if ((stcb != NULL) && (stcb->asoc.total_output_queue_size > 0)) {
5114 				;
5115 			} else {
5116 				if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5117 					/* We are not interested anymore */
5118 			abend:
5119 					if (stcb != NULL) {
5120 						SCTP_TCB_UNLOCK(stcb);
5121 					}
5122 					*offset = length;
5123 					return (NULL);
5124 				}
5125 			}
5126 			/*-
5127 			 * First are we accepting? We do this again here
5128 			 * since it is possible that a previous endpoint WAS
5129 			 * listening responded to a INIT-ACK and then
5130 			 * closed. We opened and bound.. and are now no
5131 			 * longer listening.
5132 			 *
5133 			 * XXXGL: notes on checking listen queue length.
5134 			 * 1) SCTP_IS_LISTENING() doesn't necessarily mean
5135 			 *    SOLISTENING(), because a listening "UDP type"
5136 			 *    socket isn't listening in terms of the socket
5137 			 *    layer.  It is a normal data flow socket, that
5138 			 *    can fork off new connections.  Thus, we should
5139 			 *    look into sol_qlen only in case we are !UDP.
5140 			 * 2) Checking sol_qlen in general requires locking
5141 			 *    the socket, and this code lacks that.
5142 			 */
5143 			if ((stcb == NULL) &&
5144 			    (!SCTP_IS_LISTENING(inp) ||
5145 			    (!(inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
5146 			    inp->sctp_socket->sol_qlen >= inp->sctp_socket->sol_qlimit))) {
5147 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5148 				    (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
5149 					op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
5150 					sctp_abort_association(inp, stcb, m, iphlen,
5151 					    src, dst, sh, op_err,
5152 					    mflowtype, mflowid,
5153 					    vrf_id, port);
5154 				}
5155 				*offset = length;
5156 				return (NULL);
5157 			} else {
5158 				struct mbuf *ret_buf;
5159 				struct sctp_inpcb *linp;
5160 
5161 				if (stcb) {
5162 					linp = NULL;
5163 				} else {
5164 					linp = inp;
5165 				}
5166 
5167 				if (linp != NULL) {
5168 					SCTP_ASOC_CREATE_LOCK(linp);
5169 					if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5170 					    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5171 						SCTP_ASOC_CREATE_UNLOCK(linp);
5172 						goto abend;
5173 					}
5174 				}
5175 
5176 				if (netp != NULL) {
5177 					struct sctp_tcb *locked_stcb;
5178 
5179 					locked_stcb = stcb;
5180 					ret_buf =
5181 					    sctp_handle_cookie_echo(m, iphlen,
5182 					    *offset,
5183 					    src, dst,
5184 					    sh,
5185 					    (struct sctp_cookie_echo_chunk *)ch,
5186 					    &inp, &stcb, netp,
5187 					    auth_skipped,
5188 					    auth_offset,
5189 					    auth_len,
5190 					    &locked_stcb,
5191 					    mflowtype,
5192 					    mflowid,
5193 					    vrf_id,
5194 					    port);
5195 					if ((locked_stcb != NULL) && (locked_stcb != stcb)) {
5196 						SCTP_TCB_UNLOCK(locked_stcb);
5197 					}
5198 					if (stcb != NULL) {
5199 						SCTP_TCB_LOCK_ASSERT(stcb);
5200 					}
5201 				} else {
5202 					ret_buf = NULL;
5203 				}
5204 				if (linp != NULL) {
5205 					SCTP_ASOC_CREATE_UNLOCK(linp);
5206 				}
5207 				if (ret_buf == NULL) {
5208 					if (stcb != NULL) {
5209 						SCTP_TCB_UNLOCK(stcb);
5210 					}
5211 					SCTPDBG(SCTP_DEBUG_INPUT3,
5212 					    "GAK, null buffer\n");
5213 					*offset = length;
5214 					return (NULL);
5215 				}
5216 				/* if AUTH skipped, see if it verified... */
5217 				if (auth_skipped) {
5218 					got_auth = 1;
5219 					auth_skipped = 0;
5220 				}
5221 				if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
5222 					/*
5223 					 * Restart the timer if we have
5224 					 * pending data
5225 					 */
5226 					struct sctp_tmit_chunk *chk;
5227 
5228 					chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
5229 					sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
5230 				}
5231 			}
5232 			break;
5233 		case SCTP_COOKIE_ACK:
5234 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE_ACK, stcb %p\n", (void *)stcb);
5235 			if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
5236 				return (stcb);
5237 			}
5238 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5239 				/* We are not interested anymore */
5240 				if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5241 					;
5242 				} else if (stcb) {
5243 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5244 					so = SCTP_INP_SO(inp);
5245 					atomic_add_int(&stcb->asoc.refcnt, 1);
5246 					SCTP_TCB_UNLOCK(stcb);
5247 					SCTP_SOCKET_LOCK(so, 1);
5248 					SCTP_TCB_LOCK(stcb);
5249 					atomic_subtract_int(&stcb->asoc.refcnt, 1);
5250 #endif
5251 					(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5252 					    SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
5253 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5254 					SCTP_SOCKET_UNLOCK(so, 1);
5255 #endif
5256 					*offset = length;
5257 					return (NULL);
5258 				}
5259 			}
5260 			if ((netp != NULL) && (*netp != NULL)) {
5261 				sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
5262 			}
5263 			break;
5264 		case SCTP_ECN_ECHO:
5265 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_ECHO\n");
5266 			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5267 				/* Its not ours */
5268 				*offset = length;
5269 				return (stcb);
5270 			}
5271 			if (stcb->asoc.ecn_supported == 0) {
5272 				goto unknown_chunk;
5273 			}
5274 			sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, stcb);
5275 			ecne_seen = 1;
5276 			break;
5277 		case SCTP_ECN_CWR:
5278 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_CWR\n");
5279 			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5280 				*offset = length;
5281 				return (stcb);
5282 			}
5283 			if (stcb->asoc.ecn_supported == 0) {
5284 				goto unknown_chunk;
5285 			}
5286 			sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5287 			break;
5288 		case SCTP_SHUTDOWN_COMPLETE:
5289 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_COMPLETE, stcb %p\n", (void *)stcb);
5290 			/* must be first and only chunk */
5291 			if ((num_chunks > 1) ||
5292 			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5293 				*offset = length;
5294 				return (stcb);
5295 			}
5296 			if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5297 				sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5298 				    stcb, *netp);
5299 			}
5300 			*offset = length;
5301 			return (NULL);
5302 			break;
5303 		case SCTP_ASCONF:
5304 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5305 			if (stcb != NULL) {
5306 				if (stcb->asoc.asconf_supported == 0) {
5307 					goto unknown_chunk;
5308 				}
5309 				sctp_handle_asconf(m, *offset, src,
5310 				    (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5311 				asconf_cnt++;
5312 			}
5313 			break;
5314 		case SCTP_ASCONF_ACK:
5315 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF_ACK\n");
5316 			if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5317 				/* Its not ours */
5318 				*offset = length;
5319 				return (stcb);
5320 			}
5321 			if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5322 				if (stcb->asoc.asconf_supported == 0) {
5323 					goto unknown_chunk;
5324 				}
5325 				/* He's alive so give him credit */
5326 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5327 					sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5328 					    stcb->asoc.overall_error_count,
5329 					    0,
5330 					    SCTP_FROM_SCTP_INPUT,
5331 					    __LINE__);
5332 				}
5333 				stcb->asoc.overall_error_count = 0;
5334 				sctp_handle_asconf_ack(m, *offset,
5335 				    (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5336 				if (abort_no_unlock)
5337 					return (NULL);
5338 			}
5339 			break;
5340 		case SCTP_FORWARD_CUM_TSN:
5341 		case SCTP_IFORWARD_CUM_TSN:
5342 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD_TSN\n");
5343 			if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5344 				/* Its not ours */
5345 				*offset = length;
5346 				return (stcb);
5347 			}
5348 
5349 			if (stcb != NULL) {
5350 				int abort_flag = 0;
5351 
5352 				if (stcb->asoc.prsctp_supported == 0) {
5353 					goto unknown_chunk;
5354 				}
5355 				*fwd_tsn_seen = 1;
5356 				if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5357 					/* We are not interested anymore */
5358 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5359 					so = SCTP_INP_SO(inp);
5360 					atomic_add_int(&stcb->asoc.refcnt, 1);
5361 					SCTP_TCB_UNLOCK(stcb);
5362 					SCTP_SOCKET_LOCK(so, 1);
5363 					SCTP_TCB_LOCK(stcb);
5364 					atomic_subtract_int(&stcb->asoc.refcnt, 1);
5365 #endif
5366 					(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5367 					    SCTP_FROM_SCTP_INPUT + SCTP_LOC_31);
5368 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5369 					SCTP_SOCKET_UNLOCK(so, 1);
5370 #endif
5371 					*offset = length;
5372 					return (NULL);
5373 				}
5374 				/*
5375 				 * For sending a SACK this looks like DATA
5376 				 * chunks.
5377 				 */
5378 				stcb->asoc.last_data_chunk_from = stcb->asoc.last_control_chunk_from;
5379 				sctp_handle_forward_tsn(stcb,
5380 				    (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5381 				if (abort_flag) {
5382 					*offset = length;
5383 					return (NULL);
5384 				}
5385 			}
5386 			break;
5387 		case SCTP_STREAM_RESET:
5388 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5389 			if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5390 				/* Its not ours */
5391 				*offset = length;
5392 				return (stcb);
5393 			}
5394 			if (stcb->asoc.reconfig_supported == 0) {
5395 				goto unknown_chunk;
5396 			}
5397 			if (sctp_handle_stream_reset(stcb, m, *offset, ch)) {
5398 				/* stop processing */
5399 				*offset = length;
5400 				return (NULL);
5401 			}
5402 			break;
5403 		case SCTP_PACKET_DROPPED:
5404 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5405 			/* re-get it all please */
5406 			if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5407 				/* Its not ours */
5408 				*offset = length;
5409 				return (stcb);
5410 			}
5411 
5412 			if ((ch != NULL) && (stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5413 				if (stcb->asoc.pktdrop_supported == 0) {
5414 					goto unknown_chunk;
5415 				}
5416 				sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5417 				    stcb, *netp,
5418 				    min(chk_length, contiguous));
5419 			}
5420 			break;
5421 		case SCTP_AUTHENTICATION:
5422 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5423 			if (stcb == NULL) {
5424 				/* save the first AUTH for later processing */
5425 				if (auth_skipped == 0) {
5426 					auth_offset = *offset;
5427 					auth_len = chk_length;
5428 					auth_skipped = 1;
5429 				}
5430 				/* skip this chunk (temporarily) */
5431 				goto next_chunk;
5432 			}
5433 			if (stcb->asoc.auth_supported == 0) {
5434 				goto unknown_chunk;
5435 			}
5436 			if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5437 			    (chk_length > (sizeof(struct sctp_auth_chunk) +
5438 			    SCTP_AUTH_DIGEST_LEN_MAX))) {
5439 				/* Its not ours */
5440 				*offset = length;
5441 				return (stcb);
5442 			}
5443 			if (got_auth == 1) {
5444 				/* skip this chunk... it's already auth'd */
5445 				goto next_chunk;
5446 			}
5447 			got_auth = 1;
5448 			if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5449 			    m, *offset)) {
5450 				/* auth HMAC failed so dump the packet */
5451 				*offset = length;
5452 				return (stcb);
5453 			} else {
5454 				/* remaining chunks are HMAC checked */
5455 				stcb->asoc.authenticated = 1;
5456 			}
5457 			break;
5458 
5459 		default:
5460 	unknown_chunk:
5461 			/* it's an unknown chunk! */
5462 			if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5463 				struct sctp_gen_error_cause *cause;
5464 				int len;
5465 
5466 				op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_gen_error_cause),
5467 				    0, M_NOWAIT, 1, MT_DATA);
5468 				if (op_err != NULL) {
5469 					len = min(SCTP_SIZE32(chk_length), (uint32_t)(length - *offset));
5470 					cause = mtod(op_err, struct sctp_gen_error_cause *);
5471 					cause->code = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5472 					cause->length = htons((uint16_t)(len + sizeof(struct sctp_gen_error_cause)));
5473 					SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
5474 					SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(m, *offset, len, M_NOWAIT);
5475 					if (SCTP_BUF_NEXT(op_err) != NULL) {
5476 #ifdef SCTP_MBUF_LOGGING
5477 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5478 							sctp_log_mbc(SCTP_BUF_NEXT(op_err), SCTP_MBUF_ICOPY);
5479 						}
5480 #endif
5481 						sctp_queue_op_err(stcb, op_err);
5482 					} else {
5483 						sctp_m_freem(op_err);
5484 					}
5485 				}
5486 			}
5487 			if ((ch->chunk_type & 0x80) == 0) {
5488 				/* discard this packet */
5489 				*offset = length;
5490 				return (stcb);
5491 			}	/* else skip this bad chunk and continue... */
5492 			break;
5493 		}		/* switch (ch->chunk_type) */
5494 
5495 
5496 next_chunk:
5497 		/* get the next chunk */
5498 		*offset += SCTP_SIZE32(chk_length);
5499 		if (*offset >= length) {
5500 			/* no more data left in the mbuf chain */
5501 			break;
5502 		}
5503 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5504 		    sizeof(struct sctp_chunkhdr), chunk_buf);
5505 		if (ch == NULL) {
5506 			*offset = length;
5507 			return (stcb);
5508 		}
5509 	}			/* while */
5510 
5511 	if ((asconf_cnt > 0) && (stcb != NULL)) {
5512 		sctp_send_asconf_ack(stcb);
5513 	}
5514 	return (stcb);
5515 }
5516 
5517 
5518 /*
5519  * common input chunk processing (v4 and v6)
5520  */
5521 void
5522 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
5523     struct sockaddr *src, struct sockaddr *dst,
5524     struct sctphdr *sh, struct sctp_chunkhdr *ch,
5525     uint8_t compute_crc,
5526     uint8_t ecn_bits,
5527     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
5528     uint32_t vrf_id, uint16_t port)
5529 {
5530 	uint32_t high_tsn;
5531 	int fwd_tsn_seen = 0, data_processed = 0;
5532 	struct mbuf *m = *mm, *op_err;
5533 	char msg[SCTP_DIAG_INFO_LEN];
5534 	int un_sent;
5535 	int cnt_ctrl_ready = 0;
5536 	struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
5537 	struct sctp_tcb *stcb = NULL;
5538 	struct sctp_nets *net = NULL;
5539 
5540 	SCTP_STAT_INCR(sctps_recvdatagrams);
5541 #ifdef SCTP_AUDITING_ENABLED
5542 	sctp_audit_log(0xE0, 1);
5543 	sctp_auditing(0, inp, stcb, net);
5544 #endif
5545 	if (compute_crc != 0) {
5546 		uint32_t check, calc_check;
5547 
5548 		check = sh->checksum;
5549 		sh->checksum = 0;
5550 		calc_check = sctp_calculate_cksum(m, iphlen);
5551 		sh->checksum = check;
5552 		if (calc_check != check) {
5553 			SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x  m:%p mlen:%d iphlen:%d\n",
5554 			    calc_check, check, (void *)m, length, iphlen);
5555 			stcb = sctp_findassociation_addr(m, offset, src, dst,
5556 			    sh, ch, &inp, &net, vrf_id);
5557 #if defined(INET) || defined(INET6)
5558 			if ((ch->chunk_type != SCTP_INITIATION) &&
5559 			    (net != NULL) && (net->port != port)) {
5560 				if (net->port == 0) {
5561 					/* UDP encapsulation turned on. */
5562 					net->mtu -= sizeof(struct udphdr);
5563 					if (stcb->asoc.smallest_mtu > net->mtu) {
5564 						sctp_pathmtu_adjustment(stcb, net->mtu);
5565 					}
5566 				} else if (port == 0) {
5567 					/* UDP encapsulation turned off. */
5568 					net->mtu += sizeof(struct udphdr);
5569 					/* XXX Update smallest_mtu */
5570 				}
5571 				net->port = port;
5572 			}
5573 #endif
5574 			if (net != NULL) {
5575 				net->flowtype = mflowtype;
5576 				net->flowid = mflowid;
5577 			}
5578 			if ((inp != NULL) && (stcb != NULL)) {
5579 				sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
5580 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5581 			} else if ((inp != NULL) && (stcb == NULL)) {
5582 				inp_decr = inp;
5583 			}
5584 			SCTP_STAT_INCR(sctps_badsum);
5585 			SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5586 			goto out;
5587 		}
5588 	}
5589 	/* Destination port of 0 is illegal, based on RFC4960. */
5590 	if (sh->dest_port == 0) {
5591 		SCTP_STAT_INCR(sctps_hdrops);
5592 		goto out;
5593 	}
5594 	stcb = sctp_findassociation_addr(m, offset, src, dst,
5595 	    sh, ch, &inp, &net, vrf_id);
5596 #if defined(INET) || defined(INET6)
5597 	if ((ch->chunk_type != SCTP_INITIATION) &&
5598 	    (net != NULL) && (net->port != port)) {
5599 		if (net->port == 0) {
5600 			/* UDP encapsulation turned on. */
5601 			net->mtu -= sizeof(struct udphdr);
5602 			if (stcb->asoc.smallest_mtu > net->mtu) {
5603 				sctp_pathmtu_adjustment(stcb, net->mtu);
5604 			}
5605 		} else if (port == 0) {
5606 			/* UDP encapsulation turned off. */
5607 			net->mtu += sizeof(struct udphdr);
5608 			/* XXX Update smallest_mtu */
5609 		}
5610 		net->port = port;
5611 	}
5612 #endif
5613 	if (net != NULL) {
5614 		net->flowtype = mflowtype;
5615 		net->flowid = mflowid;
5616 	}
5617 	if (inp == NULL) {
5618 		SCTP_STAT_INCR(sctps_noport);
5619 		if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
5620 			goto out;
5621 		}
5622 		if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5623 			sctp_send_shutdown_complete2(src, dst, sh,
5624 			    mflowtype, mflowid, fibnum,
5625 			    vrf_id, port);
5626 			goto out;
5627 		}
5628 		if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5629 			goto out;
5630 		}
5631 		if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
5632 			if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
5633 			    ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
5634 			    (ch->chunk_type != SCTP_INIT))) {
5635 				op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5636 				    "Out of the blue");
5637 				sctp_send_abort(m, iphlen, src, dst,
5638 				    sh, 0, op_err,
5639 				    mflowtype, mflowid, fibnum,
5640 				    vrf_id, port);
5641 			}
5642 		}
5643 		goto out;
5644 	} else if (stcb == NULL) {
5645 		inp_decr = inp;
5646 	}
5647 	SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5648 	    (void *)m, iphlen, offset, length, (void *)stcb);
5649 	if (stcb) {
5650 		/* always clear this before beginning a packet */
5651 		stcb->asoc.authenticated = 0;
5652 		stcb->asoc.seen_a_sack_this_pkt = 0;
5653 		SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5654 		    (void *)stcb, stcb->asoc.state);
5655 
5656 		if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5657 		    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5658 			/*-
5659 			 * If we hit here, we had a ref count
5660 			 * up when the assoc was aborted and the
5661 			 * timer is clearing out the assoc, we should
5662 			 * NOT respond to any packet.. its OOTB.
5663 			 */
5664 			SCTP_TCB_UNLOCK(stcb);
5665 			stcb = NULL;
5666 			snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5667 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5668 			    msg);
5669 			sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5670 			    mflowtype, mflowid, inp->fibnum,
5671 			    vrf_id, port);
5672 			goto out;
5673 		}
5674 
5675 	}
5676 	if (IS_SCTP_CONTROL(ch)) {
5677 		/* process the control portion of the SCTP packet */
5678 		/* sa_ignore NO_NULL_CHK */
5679 		stcb = sctp_process_control(m, iphlen, &offset, length,
5680 		    src, dst, sh, ch,
5681 		    inp, stcb, &net, &fwd_tsn_seen,
5682 		    mflowtype, mflowid, fibnum,
5683 		    vrf_id, port);
5684 		if (stcb) {
5685 			/*
5686 			 * This covers us if the cookie-echo was there and
5687 			 * it changes our INP.
5688 			 */
5689 			inp = stcb->sctp_ep;
5690 #if defined(INET) || defined(INET6)
5691 			if ((ch->chunk_type != SCTP_INITIATION) &&
5692 			    (net != NULL) && (net->port != port)) {
5693 				if (net->port == 0) {
5694 					/* UDP encapsulation turned on. */
5695 					net->mtu -= sizeof(struct udphdr);
5696 					if (stcb->asoc.smallest_mtu > net->mtu) {
5697 						sctp_pathmtu_adjustment(stcb, net->mtu);
5698 					}
5699 				} else if (port == 0) {
5700 					/* UDP encapsulation turned off. */
5701 					net->mtu += sizeof(struct udphdr);
5702 					/* XXX Update smallest_mtu */
5703 				}
5704 				net->port = port;
5705 			}
5706 #endif
5707 		}
5708 	} else {
5709 		/*
5710 		 * no control chunks, so pre-process DATA chunks (these
5711 		 * checks are taken care of by control processing)
5712 		 */
5713 
5714 		/*
5715 		 * if DATA only packet, and auth is required, then punt...
5716 		 * can't have authenticated without any AUTH (control)
5717 		 * chunks
5718 		 */
5719 		if ((stcb != NULL) &&
5720 		    sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5721 			/* "silently" ignore */
5722 			SCTP_STAT_INCR(sctps_recvauthmissing);
5723 			goto out;
5724 		}
5725 		if (stcb == NULL) {
5726 			/* out of the blue DATA chunk */
5727 			snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5728 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5729 			    msg);
5730 			sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5731 			    mflowtype, mflowid, fibnum,
5732 			    vrf_id, port);
5733 			goto out;
5734 		}
5735 		if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5736 			/* v_tag mismatch! */
5737 			SCTP_STAT_INCR(sctps_badvtag);
5738 			goto out;
5739 		}
5740 	}
5741 
5742 	if (stcb == NULL) {
5743 		/*
5744 		 * no valid TCB for this packet, or we found it's a bad
5745 		 * packet while processing control, or we're done with this
5746 		 * packet (done or skip rest of data), so we drop it...
5747 		 */
5748 		goto out;
5749 	}
5750 
5751 	/*
5752 	 * DATA chunk processing
5753 	 */
5754 	/* plow through the data chunks while length > offset */
5755 
5756 	/*
5757 	 * Rest should be DATA only.  Check authentication state if AUTH for
5758 	 * DATA is required.
5759 	 */
5760 	if ((length > offset) &&
5761 	    (stcb != NULL) &&
5762 	    sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5763 	    !stcb->asoc.authenticated) {
5764 		/* "silently" ignore */
5765 		SCTP_STAT_INCR(sctps_recvauthmissing);
5766 		SCTPDBG(SCTP_DEBUG_AUTH1,
5767 		    "Data chunk requires AUTH, skipped\n");
5768 		goto trigger_send;
5769 	}
5770 	if (length > offset) {
5771 		int retval;
5772 
5773 		/*
5774 		 * First check to make sure our state is correct. We would
5775 		 * not get here unless we really did have a tag, so we don't
5776 		 * abort if this happens, just dump the chunk silently.
5777 		 */
5778 		switch (SCTP_GET_STATE(&stcb->asoc)) {
5779 		case SCTP_STATE_COOKIE_ECHOED:
5780 			/*
5781 			 * we consider data with valid tags in this state
5782 			 * shows us the cookie-ack was lost. Imply it was
5783 			 * there.
5784 			 */
5785 			sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5786 			break;
5787 		case SCTP_STATE_COOKIE_WAIT:
5788 			/*
5789 			 * We consider OOTB any data sent during asoc setup.
5790 			 */
5791 			snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5792 			op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5793 			    msg);
5794 			sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5795 			    mflowtype, mflowid, inp->fibnum,
5796 			    vrf_id, port);
5797 			goto out;
5798 			/* sa_ignore NOTREACHED */
5799 			break;
5800 		case SCTP_STATE_EMPTY:	/* should not happen */
5801 		case SCTP_STATE_INUSE:	/* should not happen */
5802 		case SCTP_STATE_SHUTDOWN_RECEIVED:	/* This is a peer error */
5803 		case SCTP_STATE_SHUTDOWN_ACK_SENT:
5804 		default:
5805 			goto out;
5806 			/* sa_ignore NOTREACHED */
5807 			break;
5808 		case SCTP_STATE_OPEN:
5809 		case SCTP_STATE_SHUTDOWN_SENT:
5810 			break;
5811 		}
5812 		/* plow through the data chunks while length > offset */
5813 		retval = sctp_process_data(mm, iphlen, &offset, length,
5814 		    inp, stcb, net, &high_tsn);
5815 		if (retval == 2) {
5816 			/*
5817 			 * The association aborted, NO UNLOCK needed since
5818 			 * the association is destroyed.
5819 			 */
5820 			stcb = NULL;
5821 			goto out;
5822 		}
5823 		data_processed = 1;
5824 		/*
5825 		 * Anything important needs to have been m_copy'ed in
5826 		 * process_data
5827 		 */
5828 	}
5829 
5830 	/* take care of ecn */
5831 	if ((data_processed == 1) &&
5832 	    (stcb->asoc.ecn_supported == 1) &&
5833 	    ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5834 		/* Yep, we need to add a ECNE */
5835 		sctp_send_ecn_echo(stcb, net, high_tsn);
5836 	}
5837 
5838 	if ((data_processed == 0) && (fwd_tsn_seen)) {
5839 		int was_a_gap;
5840 		uint32_t highest_tsn;
5841 
5842 		if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
5843 			highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
5844 		} else {
5845 			highest_tsn = stcb->asoc.highest_tsn_inside_map;
5846 		}
5847 		was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
5848 		stcb->asoc.send_sack = 1;
5849 		sctp_sack_check(stcb, was_a_gap);
5850 	} else if (fwd_tsn_seen) {
5851 		stcb->asoc.send_sack = 1;
5852 	}
5853 	/* trigger send of any chunks in queue... */
5854 trigger_send:
5855 #ifdef SCTP_AUDITING_ENABLED
5856 	sctp_audit_log(0xE0, 2);
5857 	sctp_auditing(1, inp, stcb, net);
5858 #endif
5859 	SCTPDBG(SCTP_DEBUG_INPUT1,
5860 	    "Check for chunk output prw:%d tqe:%d tf=%d\n",
5861 	    stcb->asoc.peers_rwnd,
5862 	    TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5863 	    stcb->asoc.total_flight);
5864 	un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5865 	if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
5866 		cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
5867 	}
5868 	if (!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue) ||
5869 	    cnt_ctrl_ready ||
5870 	    stcb->asoc.trigger_reset ||
5871 	    ((un_sent) &&
5872 	    (stcb->asoc.peers_rwnd > 0 ||
5873 	    (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
5874 		SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5875 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5876 		SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5877 	}
5878 #ifdef SCTP_AUDITING_ENABLED
5879 	sctp_audit_log(0xE0, 3);
5880 	sctp_auditing(2, inp, stcb, net);
5881 #endif
5882 out:
5883 	if (stcb != NULL) {
5884 		SCTP_TCB_UNLOCK(stcb);
5885 	}
5886 	if (inp_decr != NULL) {
5887 		/* reduce ref-count */
5888 		SCTP_INP_WLOCK(inp_decr);
5889 		SCTP_INP_DECR_REF(inp_decr);
5890 		SCTP_INP_WUNLOCK(inp_decr);
5891 	}
5892 	return;
5893 }
5894 
5895 #ifdef INET
5896 void
5897 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
5898 {
5899 	struct mbuf *m;
5900 	int iphlen;
5901 	uint32_t vrf_id = 0;
5902 	uint8_t ecn_bits;
5903 	struct sockaddr_in src, dst;
5904 	struct ip *ip;
5905 	struct sctphdr *sh;
5906 	struct sctp_chunkhdr *ch;
5907 	int length, offset;
5908 	uint8_t compute_crc;
5909 	uint32_t mflowid;
5910 	uint8_t mflowtype;
5911 	uint16_t fibnum;
5912 
5913 	iphlen = off;
5914 	if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
5915 		SCTP_RELEASE_PKT(i_pak);
5916 		return;
5917 	}
5918 	m = SCTP_HEADER_TO_CHAIN(i_pak);
5919 #ifdef SCTP_MBUF_LOGGING
5920 	/* Log in any input mbufs */
5921 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5922 		sctp_log_mbc(m, SCTP_MBUF_INPUT);
5923 	}
5924 #endif
5925 #ifdef SCTP_PACKET_LOGGING
5926 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
5927 		sctp_packet_log(m);
5928 	}
5929 #endif
5930 	SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
5931 	    "sctp_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
5932 	    m->m_pkthdr.len,
5933 	    if_name(m->m_pkthdr.rcvif),
5934 	    (int)m->m_pkthdr.csum_flags, CSUM_BITS);
5935 	mflowid = m->m_pkthdr.flowid;
5936 	mflowtype = M_HASHTYPE_GET(m);
5937 	fibnum = M_GETFIB(m);
5938 	SCTP_STAT_INCR(sctps_recvpackets);
5939 	SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
5940 	/* Get IP, SCTP, and first chunk header together in the first mbuf. */
5941 	offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5942 	if (SCTP_BUF_LEN(m) < offset) {
5943 		if ((m = m_pullup(m, offset)) == NULL) {
5944 			SCTP_STAT_INCR(sctps_hdrops);
5945 			return;
5946 		}
5947 	}
5948 	ip = mtod(m, struct ip *);
5949 	sh = (struct sctphdr *)((caddr_t)ip + iphlen);
5950 	ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
5951 	offset -= sizeof(struct sctp_chunkhdr);
5952 	memset(&src, 0, sizeof(struct sockaddr_in));
5953 	src.sin_family = AF_INET;
5954 	src.sin_len = sizeof(struct sockaddr_in);
5955 	src.sin_port = sh->src_port;
5956 	src.sin_addr = ip->ip_src;
5957 	memset(&dst, 0, sizeof(struct sockaddr_in));
5958 	dst.sin_family = AF_INET;
5959 	dst.sin_len = sizeof(struct sockaddr_in);
5960 	dst.sin_port = sh->dest_port;
5961 	dst.sin_addr = ip->ip_dst;
5962 	length = ntohs(ip->ip_len);
5963 	/* Validate mbuf chain length with IP payload length. */
5964 	if (SCTP_HEADER_LEN(m) != length) {
5965 		SCTPDBG(SCTP_DEBUG_INPUT1,
5966 		    "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
5967 		SCTP_STAT_INCR(sctps_hdrops);
5968 		goto out;
5969 	}
5970 	/* SCTP does not allow broadcasts or multicasts */
5971 	if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
5972 		goto out;
5973 	}
5974 	if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
5975 		goto out;
5976 	}
5977 	ecn_bits = ip->ip_tos;
5978 	if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
5979 		SCTP_STAT_INCR(sctps_recvhwcrc);
5980 		compute_crc = 0;
5981 	} else {
5982 		SCTP_STAT_INCR(sctps_recvswcrc);
5983 		compute_crc = 1;
5984 	}
5985 	sctp_common_input_processing(&m, iphlen, offset, length,
5986 	    (struct sockaddr *)&src,
5987 	    (struct sockaddr *)&dst,
5988 	    sh, ch,
5989 	    compute_crc,
5990 	    ecn_bits,
5991 	    mflowtype, mflowid, fibnum,
5992 	    vrf_id, port);
5993 out:
5994 	if (m) {
5995 		sctp_m_freem(m);
5996 	}
5997 	return;
5998 }
5999 
6000 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6001 extern int *sctp_cpuarry;
6002 #endif
6003 
6004 int
6005 sctp_input(struct mbuf **mp, int *offp, int proto SCTP_UNUSED)
6006 {
6007 	struct mbuf *m;
6008 	int off;
6009 
6010 	m = *mp;
6011 	off = *offp;
6012 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6013 	if (mp_ncpus > 1) {
6014 		struct ip *ip;
6015 		struct sctphdr *sh;
6016 		int offset;
6017 		int cpu_to_use;
6018 		uint32_t flowid, tag;
6019 
6020 		if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
6021 			flowid = m->m_pkthdr.flowid;
6022 		} else {
6023 			/*
6024 			 * No flow id built by lower layers fix it so we
6025 			 * create one.
6026 			 */
6027 			offset = off + sizeof(struct sctphdr);
6028 			if (SCTP_BUF_LEN(m) < offset) {
6029 				if ((m = m_pullup(m, offset)) == NULL) {
6030 					SCTP_STAT_INCR(sctps_hdrops);
6031 					return (IPPROTO_DONE);
6032 				}
6033 			}
6034 			ip = mtod(m, struct ip *);
6035 			sh = (struct sctphdr *)((caddr_t)ip + off);
6036 			tag = htonl(sh->v_tag);
6037 			flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6038 			m->m_pkthdr.flowid = flowid;
6039 			M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE_HASH);
6040 		}
6041 		cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
6042 		sctp_queue_to_mcore(m, off, cpu_to_use);
6043 		return (IPPROTO_DONE);
6044 	}
6045 #endif
6046 	sctp_input_with_port(m, off, 0);
6047 	return (IPPROTO_DONE);
6048 }
6049 #endif
6050