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