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