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