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