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