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