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