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