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