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