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