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