xref: /freebsd/sys/netinet/sctp_input.c (revision 282a3889ebf826db9839be296ff1dd903f6d6d6e)
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 	is_there_another:
190 			/* sa_ignore FREED_MEMORY */
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 
1624 	/*
1625 	 * Here we do a trick, we set in NULL for the proc/thread argument.
1626 	 * We do this since in effect we only use the p argument when the
1627 	 * socket is unbound and we must do an implicit bind. Since we are
1628 	 * getting a cookie, we cannot be unbound.
1629 	 */
1630 	stcb = sctp_aloc_assoc(inp, init_src, 0, &error,
1631 	    ntohl(initack_cp->init.initiate_tag), vrf_id,
1632 	    (struct thread *)NULL
1633 	    );
1634 	if (stcb == NULL) {
1635 		struct mbuf *op_err;
1636 
1637 		/* memory problem? */
1638 		SCTPDBG(SCTP_DEBUG_INPUT1,
1639 		    "process_cookie_new: no room for another TCB!\n");
1640 		op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1641 
1642 		sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
1643 		    sh, op_err, vrf_id);
1644 		return (NULL);
1645 	}
1646 	/* get the correct sctp_nets */
1647 	if (netp)
1648 		*netp = sctp_findnet(stcb, init_src);
1649 
1650 	asoc = &stcb->asoc;
1651 	/* get scope variables out of cookie */
1652 	asoc->ipv4_local_scope = cookie->ipv4_scope;
1653 	asoc->site_scope = cookie->site_scope;
1654 	asoc->local_scope = cookie->local_scope;
1655 	asoc->loopback_scope = cookie->loopback_scope;
1656 
1657 	if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) ||
1658 	    (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) {
1659 		struct mbuf *op_err;
1660 
1661 		/*
1662 		 * Houston we have a problem. The EP changed while the
1663 		 * cookie was in flight. Only recourse is to abort the
1664 		 * association.
1665 		 */
1666 		atomic_add_int(&stcb->asoc.refcnt, 1);
1667 		op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1668 		sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
1669 		    sh, op_err, vrf_id);
1670 		sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
1671 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1672 		atomic_add_int(&stcb->asoc.refcnt, -1);
1673 		return (NULL);
1674 	}
1675 	/* process the INIT-ACK info (my info) */
1676 	old_tag = asoc->my_vtag;
1677 	asoc->assoc_id = asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1678 	asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1679 	asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1680 	asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1681 	asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1682 	asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1683 	asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1684 	asoc->str_reset_seq_in = asoc->init_seq_number;
1685 
1686 	asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1687 
1688 	/* process the INIT info (peer's info) */
1689 	if (netp)
1690 		retval = sctp_process_init(init_cp, stcb, *netp);
1691 	else
1692 		retval = 0;
1693 	if (retval < 0) {
1694 		atomic_add_int(&stcb->asoc.refcnt, 1);
1695 		sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1696 		atomic_add_int(&stcb->asoc.refcnt, -1);
1697 		return (NULL);
1698 	}
1699 	/* load all addresses */
1700 	if (sctp_load_addresses_from_init(stcb, m, iphlen,
1701 	    init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh,
1702 	    init_src)) {
1703 		atomic_add_int(&stcb->asoc.refcnt, 1);
1704 		sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
1705 		atomic_add_int(&stcb->asoc.refcnt, -1);
1706 		return (NULL);
1707 	}
1708 	/*
1709 	 * verify any preceding AUTH chunk that was skipped
1710 	 */
1711 	/* pull the local authentication parameters from the cookie/init-ack */
1712 	sctp_auth_get_cookie_params(stcb, m,
1713 	    initack_offset + sizeof(struct sctp_init_ack_chunk),
1714 	    initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
1715 	if (auth_skipped) {
1716 		struct sctp_auth_chunk *auth;
1717 
1718 		auth = (struct sctp_auth_chunk *)
1719 		    sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
1720 		if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
1721 			/* auth HMAC failed, dump the assoc and packet */
1722 			SCTPDBG(SCTP_DEBUG_AUTH1,
1723 			    "COOKIE-ECHO: AUTH failed\n");
1724 			atomic_add_int(&stcb->asoc.refcnt, 1);
1725 			sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
1726 			atomic_add_int(&stcb->asoc.refcnt, -1);
1727 			return (NULL);
1728 		} else {
1729 			/* remaining chunks checked... good to go */
1730 			stcb->asoc.authenticated = 1;
1731 		}
1732 	}
1733 	/* update current state */
1734 	SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
1735 	if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1736 		asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING;
1737 		sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1738 		    stcb->sctp_ep, stcb, asoc->primary_destination);
1739 	} else {
1740 		asoc->state = SCTP_STATE_OPEN;
1741 	}
1742 	sctp_stop_all_cookie_timers(stcb);
1743 	SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
1744 	SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1745 
1746 	/*
1747 	 * if we're doing ASCONFs, check to see if we have any new local
1748 	 * addresses that need to get added to the peer (eg. addresses
1749 	 * changed while cookie echo in flight).  This needs to be done
1750 	 * after we go to the OPEN state to do the correct asconf
1751 	 * processing. else, make sure we have the correct addresses in our
1752 	 * lists
1753 	 */
1754 
1755 	/* warning, we re-use sin, sin6, sa_store here! */
1756 	/* pull in local_address (our "from" address) */
1757 	if (cookie->laddr_type == SCTP_IPV4_ADDRESS) {
1758 		/* source addr is IPv4 */
1759 		sin = (struct sockaddr_in *)initack_src;
1760 		memset(sin, 0, sizeof(*sin));
1761 		sin->sin_family = AF_INET;
1762 		sin->sin_len = sizeof(struct sockaddr_in);
1763 		sin->sin_addr.s_addr = cookie->laddress[0];
1764 	} else if (cookie->laddr_type == SCTP_IPV6_ADDRESS) {
1765 		/* source addr is IPv6 */
1766 		sin6 = (struct sockaddr_in6 *)initack_src;
1767 		memset(sin6, 0, sizeof(*sin6));
1768 		sin6->sin6_family = AF_INET6;
1769 		sin6->sin6_len = sizeof(struct sockaddr_in6);
1770 		sin6->sin6_scope_id = cookie->scope_id;
1771 		memcpy(&sin6->sin6_addr, cookie->laddress,
1772 		    sizeof(sin6->sin6_addr));
1773 	} else {
1774 		atomic_add_int(&stcb->asoc.refcnt, 1);
1775 		sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
1776 		atomic_add_int(&stcb->asoc.refcnt, -1);
1777 		return (NULL);
1778 	}
1779 
1780 	sctp_check_address_list(stcb, m,
1781 	    initack_offset + sizeof(struct sctp_init_ack_chunk),
1782 	    initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
1783 	    initack_src, cookie->local_scope, cookie->site_scope,
1784 	    cookie->ipv4_scope, cookie->loopback_scope);
1785 
1786 
1787 	/* set up to notify upper layer */
1788 	*notification = SCTP_NOTIFY_ASSOC_UP;
1789 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1790 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1791 	    (inp->sctp_socket->so_qlimit == 0)) {
1792 		/*
1793 		 * This is an endpoint that called connect() how it got a
1794 		 * cookie that is NEW is a bit of a mystery. It must be that
1795 		 * the INIT was sent, but before it got there.. a complete
1796 		 * INIT/INIT-ACK/COOKIE arrived. But of course then it
1797 		 * should have went to the other code.. not here.. oh well..
1798 		 * a bit of protection is worth having..
1799 		 */
1800 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1801 		soisconnected(stcb->sctp_ep->sctp_socket);
1802 	} else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1803 	    (inp->sctp_socket->so_qlimit)) {
1804 		/*
1805 		 * We don't want to do anything with this one. Since it is
1806 		 * the listening guy. The timer will get started for
1807 		 * accepted connections in the caller.
1808 		 */
1809 		;
1810 	}
1811 	/* since we did not send a HB make sure we don't double things */
1812 	if ((netp) && (*netp))
1813 		(*netp)->hb_responded = 1;
1814 
1815 	if (stcb->asoc.sctp_autoclose_ticks &&
1816 	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1817 		sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
1818 	}
1819 	/* respond with a COOKIE-ACK */
1820 	/* calculate the RTT */
1821 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1822 	if ((netp) && (*netp)) {
1823 		(*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
1824 		    &cookie->time_entered, sctp_align_unsafe_makecopy);
1825 	}
1826 	sctp_send_cookie_ack(stcb);
1827 	return (stcb);
1828 }
1829 
1830 
1831 /*
1832  * handles a COOKIE-ECHO message stcb: modified to either a new or left as
1833  * existing (non-NULL) TCB
1834  */
1835 static struct mbuf *
1836 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
1837     struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
1838     struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
1839     int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1840     struct sctp_tcb **locked_tcb, uint32_t vrf_id)
1841 {
1842 	struct sctp_state_cookie *cookie;
1843 	struct sockaddr_in6 sin6;
1844 	struct sockaddr_in sin;
1845 	struct sctp_tcb *l_stcb = *stcb;
1846 	struct sctp_inpcb *l_inp;
1847 	struct sockaddr *to;
1848 	sctp_assoc_t sac_restart_id;
1849 	struct sctp_pcb *ep;
1850 	struct mbuf *m_sig;
1851 	uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
1852 	uint8_t *sig;
1853 	uint8_t cookie_ok = 0;
1854 	unsigned int size_of_pkt, sig_offset, cookie_offset;
1855 	unsigned int cookie_len;
1856 	struct timeval now;
1857 	struct timeval time_expires;
1858 	struct sockaddr_storage dest_store;
1859 	struct sockaddr *localep_sa = (struct sockaddr *)&dest_store;
1860 	struct ip *iph;
1861 	int notification = 0;
1862 	struct sctp_nets *netl;
1863 	int had_a_existing_tcb = 0;
1864 
1865 	SCTPDBG(SCTP_DEBUG_INPUT2,
1866 	    "sctp_handle_cookie: handling COOKIE-ECHO\n");
1867 
1868 	if (inp_p == NULL) {
1869 		return (NULL);
1870 	}
1871 	/* First get the destination address setup too. */
1872 	iph = mtod(m, struct ip *);
1873 	if (iph->ip_v == IPVERSION) {
1874 		/* its IPv4 */
1875 		struct sockaddr_in *lsin;
1876 
1877 		lsin = (struct sockaddr_in *)(localep_sa);
1878 		memset(lsin, 0, sizeof(*lsin));
1879 		lsin->sin_family = AF_INET;
1880 		lsin->sin_len = sizeof(*lsin);
1881 		lsin->sin_port = sh->dest_port;
1882 		lsin->sin_addr.s_addr = iph->ip_dst.s_addr;
1883 		size_of_pkt = SCTP_GET_IPV4_LENGTH(iph);
1884 	} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1885 		/* its IPv6 */
1886 		struct ip6_hdr *ip6;
1887 		struct sockaddr_in6 *lsin6;
1888 
1889 		lsin6 = (struct sockaddr_in6 *)(localep_sa);
1890 		memset(lsin6, 0, sizeof(*lsin6));
1891 		lsin6->sin6_family = AF_INET6;
1892 		lsin6->sin6_len = sizeof(struct sockaddr_in6);
1893 		ip6 = mtod(m, struct ip6_hdr *);
1894 		lsin6->sin6_port = sh->dest_port;
1895 		lsin6->sin6_addr = ip6->ip6_dst;
1896 		size_of_pkt = SCTP_GET_IPV6_LENGTH(ip6) + iphlen;
1897 	} else {
1898 		return (NULL);
1899 	}
1900 
1901 	cookie = &cp->cookie;
1902 	cookie_offset = offset + sizeof(struct sctp_chunkhdr);
1903 	cookie_len = ntohs(cp->ch.chunk_length);
1904 
1905 	if ((cookie->peerport != sh->src_port) &&
1906 	    (cookie->myport != sh->dest_port) &&
1907 	    (cookie->my_vtag != sh->v_tag)) {
1908 		/*
1909 		 * invalid ports or bad tag.  Note that we always leave the
1910 		 * v_tag in the header in network order and when we stored
1911 		 * it in the my_vtag slot we also left it in network order.
1912 		 * This maintains the match even though it may be in the
1913 		 * opposite byte order of the machine :->
1914 		 */
1915 		return (NULL);
1916 	}
1917 	if (cookie_len > size_of_pkt ||
1918 	    cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
1919 	    sizeof(struct sctp_init_chunk) +
1920 	    sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
1921 		/* cookie too long!  or too small */
1922 		return (NULL);
1923 	}
1924 	/*
1925 	 * split off the signature into its own mbuf (since it should not be
1926 	 * calculated in the sctp_hmac_m() call).
1927 	 */
1928 	sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
1929 	if (sig_offset > size_of_pkt) {
1930 		/* packet not correct size! */
1931 		/* XXX this may already be accounted for earlier... */
1932 		return (NULL);
1933 	}
1934 	m_sig = m_split(m, sig_offset, M_DONTWAIT);
1935 	if (m_sig == NULL) {
1936 		/* out of memory or ?? */
1937 		return (NULL);
1938 	}
1939 	/*
1940 	 * compute the signature/digest for the cookie
1941 	 */
1942 	ep = &(*inp_p)->sctp_ep;
1943 	l_inp = *inp_p;
1944 	if (l_stcb) {
1945 		SCTP_TCB_UNLOCK(l_stcb);
1946 	}
1947 	SCTP_INP_RLOCK(l_inp);
1948 	if (l_stcb) {
1949 		SCTP_TCB_LOCK(l_stcb);
1950 	}
1951 	/* which cookie is it? */
1952 	if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
1953 	    (ep->current_secret_number != ep->last_secret_number)) {
1954 		/* it's the old cookie */
1955 		(void)sctp_hmac_m(SCTP_HMAC,
1956 		    (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
1957 		    SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
1958 	} else {
1959 		/* it's the current cookie */
1960 		(void)sctp_hmac_m(SCTP_HMAC,
1961 		    (uint8_t *) ep->secret_key[(int)ep->current_secret_number],
1962 		    SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
1963 	}
1964 	/* get the signature */
1965 	SCTP_INP_RUNLOCK(l_inp);
1966 	sig = (uint8_t *) sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *) & tmp_sig);
1967 	if (sig == NULL) {
1968 		/* couldn't find signature */
1969 		sctp_m_freem(m_sig);
1970 		return (NULL);
1971 	}
1972 	/* compare the received digest with the computed digest */
1973 	if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
1974 		/* try the old cookie? */
1975 		if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
1976 		    (ep->current_secret_number != ep->last_secret_number)) {
1977 			/* compute digest with old */
1978 			(void)sctp_hmac_m(SCTP_HMAC,
1979 			    (uint8_t *) ep->secret_key[(int)ep->last_secret_number],
1980 			    SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
1981 			/* compare */
1982 			if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
1983 				cookie_ok = 1;
1984 		}
1985 	} else {
1986 		cookie_ok = 1;
1987 	}
1988 
1989 	/*
1990 	 * Now before we continue we must reconstruct our mbuf so that
1991 	 * normal processing of any other chunks will work.
1992 	 */
1993 	{
1994 		struct mbuf *m_at;
1995 
1996 		m_at = m;
1997 		while (SCTP_BUF_NEXT(m_at) != NULL) {
1998 			m_at = SCTP_BUF_NEXT(m_at);
1999 		}
2000 		SCTP_BUF_NEXT(m_at) = m_sig;
2001 	}
2002 
2003 	if (cookie_ok == 0) {
2004 		SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2005 		SCTPDBG(SCTP_DEBUG_INPUT2,
2006 		    "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2007 		    (uint32_t) offset, cookie_offset, sig_offset);
2008 		return (NULL);
2009 	}
2010 	/*
2011 	 * check the cookie timestamps to be sure it's not stale
2012 	 */
2013 	(void)SCTP_GETTIME_TIMEVAL(&now);
2014 	/* Expire time is in Ticks, so we convert to seconds */
2015 	time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2016 	time_expires.tv_usec = cookie->time_entered.tv_usec;
2017 	if (timevalcmp(&now, &time_expires, >)) {
2018 		/* cookie is stale! */
2019 		struct mbuf *op_err;
2020 		struct sctp_stale_cookie_msg *scm;
2021 		uint32_t tim;
2022 
2023 		op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_stale_cookie_msg),
2024 		    0, M_DONTWAIT, 1, MT_DATA);
2025 		if (op_err == NULL) {
2026 			/* FOOBAR */
2027 			return (NULL);
2028 		}
2029 		/* pre-reserve some space */
2030 		SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
2031 		SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
2032 		SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
2033 
2034 		/* Set the len */
2035 		SCTP_BUF_LEN(op_err) = sizeof(struct sctp_stale_cookie_msg);
2036 		scm = mtod(op_err, struct sctp_stale_cookie_msg *);
2037 		scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE);
2038 		scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) +
2039 		    (sizeof(uint32_t))));
2040 		/* seconds to usec */
2041 		tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
2042 		/* add in usec */
2043 		if (tim == 0)
2044 			tim = now.tv_usec - cookie->time_entered.tv_usec;
2045 		scm->time_usec = htonl(tim);
2046 		sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag,
2047 		    vrf_id);
2048 		return (NULL);
2049 	}
2050 	/*
2051 	 * Now we must see with the lookup address if we have an existing
2052 	 * asoc. This will only happen if we were in the COOKIE-WAIT state
2053 	 * and a INIT collided with us and somewhere the peer sent the
2054 	 * cookie on another address besides the single address our assoc
2055 	 * had for him. In this case we will have one of the tie-tags set at
2056 	 * least AND the address field in the cookie can be used to look it
2057 	 * up.
2058 	 */
2059 	to = NULL;
2060 	if (cookie->addr_type == SCTP_IPV6_ADDRESS) {
2061 		memset(&sin6, 0, sizeof(sin6));
2062 		sin6.sin6_family = AF_INET6;
2063 		sin6.sin6_len = sizeof(sin6);
2064 		sin6.sin6_port = sh->src_port;
2065 		sin6.sin6_scope_id = cookie->scope_id;
2066 		memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2067 		    sizeof(sin6.sin6_addr.s6_addr));
2068 		to = (struct sockaddr *)&sin6;
2069 	} else if (cookie->addr_type == SCTP_IPV4_ADDRESS) {
2070 		memset(&sin, 0, sizeof(sin));
2071 		sin.sin_family = AF_INET;
2072 		sin.sin_len = sizeof(sin);
2073 		sin.sin_port = sh->src_port;
2074 		sin.sin_addr.s_addr = cookie->address[0];
2075 		to = (struct sockaddr *)&sin;
2076 	} else {
2077 		/* This should not happen */
2078 		return (NULL);
2079 	}
2080 	if ((*stcb == NULL) && to) {
2081 		/* Yep, lets check */
2082 		*stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL);
2083 		if (*stcb == NULL) {
2084 			/*
2085 			 * We should have only got back the same inp. If we
2086 			 * got back a different ep we have a problem. The
2087 			 * original findep got back l_inp and now
2088 			 */
2089 			if (l_inp != *inp_p) {
2090 				SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2091 			}
2092 		} else {
2093 			if (*locked_tcb == NULL) {
2094 				/*
2095 				 * In this case we found the assoc only
2096 				 * after we locked the create lock. This
2097 				 * means we are in a colliding case and we
2098 				 * must make sure that we unlock the tcb if
2099 				 * its one of the cases where we throw away
2100 				 * the incoming packets.
2101 				 */
2102 				*locked_tcb = *stcb;
2103 
2104 				/*
2105 				 * We must also increment the inp ref count
2106 				 * since the ref_count flags was set when we
2107 				 * did not find the TCB, now we found it
2108 				 * which reduces the refcount.. we must
2109 				 * raise it back out to balance it all :-)
2110 				 */
2111 				SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2112 				if ((*stcb)->sctp_ep != l_inp) {
2113 					SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2114 					    (*stcb)->sctp_ep, l_inp);
2115 				}
2116 			}
2117 		}
2118 	}
2119 	if (to == NULL)
2120 		return (NULL);
2121 
2122 	cookie_len -= SCTP_SIGNATURE_SIZE;
2123 	if (*stcb == NULL) {
2124 		/* this is the "normal" case... get a new TCB */
2125 		*stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie,
2126 		    cookie_len, *inp_p, netp, to, &notification,
2127 		    auth_skipped, auth_offset, auth_len, vrf_id);
2128 	} else {
2129 		/* this is abnormal... cookie-echo on existing TCB */
2130 		had_a_existing_tcb = 1;
2131 		*stcb = sctp_process_cookie_existing(m, iphlen, offset, sh,
2132 		    cookie, cookie_len, *inp_p, *stcb, *netp, to,
2133 		    &notification, &sac_restart_id, vrf_id);
2134 	}
2135 
2136 	if (*stcb == NULL) {
2137 		/* still no TCB... must be bad cookie-echo */
2138 		return (NULL);
2139 	}
2140 	/*
2141 	 * Ok, we built an association so confirm the address we sent the
2142 	 * INIT-ACK to.
2143 	 */
2144 	netl = sctp_findnet(*stcb, to);
2145 	/*
2146 	 * This code should in theory NOT run but
2147 	 */
2148 	if (netl == NULL) {
2149 		/* TSNH! Huh, why do I need to add this address here? */
2150 		int ret;
2151 
2152 		ret = sctp_add_remote_addr(*stcb, to, SCTP_DONOT_SETSCOPE,
2153 		    SCTP_IN_COOKIE_PROC);
2154 		netl = sctp_findnet(*stcb, to);
2155 	}
2156 	if (netl) {
2157 		if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2158 			netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2159 			(void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2160 			    netl);
2161 			sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2162 			    (*stcb), 0, (void *)netl);
2163 		}
2164 	}
2165 	if (*stcb) {
2166 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, *inp_p,
2167 		    *stcb, NULL);
2168 	}
2169 	if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2170 		if (!had_a_existing_tcb ||
2171 		    (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2172 			/*
2173 			 * If we have a NEW cookie or the connect never
2174 			 * reached the connected state during collision we
2175 			 * must do the TCP accept thing.
2176 			 */
2177 			struct socket *so, *oso;
2178 			struct sctp_inpcb *inp;
2179 
2180 			if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2181 				/*
2182 				 * For a restart we will keep the same
2183 				 * socket, no need to do anything. I THINK!!
2184 				 */
2185 				sctp_ulp_notify(notification, *stcb, 0, (void *)&sac_restart_id);
2186 				return (m);
2187 			}
2188 			oso = (*inp_p)->sctp_socket;
2189 			/*
2190 			 * We do this to keep the sockets side happy durin
2191 			 * the sonewcon ONLY.
2192 			 */
2193 			NET_LOCK_GIANT();
2194 			SCTP_TCB_UNLOCK((*stcb));
2195 			so = sonewconn(oso, 0
2196 			    );
2197 			NET_UNLOCK_GIANT();
2198 			SCTP_INP_WLOCK((*stcb)->sctp_ep);
2199 			SCTP_TCB_LOCK((*stcb));
2200 			SCTP_INP_WUNLOCK((*stcb)->sctp_ep);
2201 			if (so == NULL) {
2202 				struct mbuf *op_err;
2203 
2204 				/* Too many sockets */
2205 				SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2206 				op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2207 				sctp_abort_association(*inp_p, NULL, m, iphlen,
2208 				    sh, op_err, vrf_id);
2209 				sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2210 				return (NULL);
2211 			}
2212 			inp = (struct sctp_inpcb *)so->so_pcb;
2213 			SCTP_INP_INCR_REF(inp);
2214 			/*
2215 			 * We add the unbound flag here so that if we get an
2216 			 * soabort() before we get the move_pcb done, we
2217 			 * will properly cleanup.
2218 			 */
2219 			inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2220 			    SCTP_PCB_FLAGS_CONNECTED |
2221 			    SCTP_PCB_FLAGS_IN_TCPPOOL |
2222 			    SCTP_PCB_FLAGS_UNBOUND |
2223 			    (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2224 			    SCTP_PCB_FLAGS_DONT_WAKE);
2225 			inp->sctp_features = (*inp_p)->sctp_features;
2226 			inp->sctp_socket = so;
2227 			inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2228 			inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2229 			inp->sctp_context = (*inp_p)->sctp_context;
2230 			inp->inp_starting_point_for_iterator = NULL;
2231 			/*
2232 			 * copy in the authentication parameters from the
2233 			 * original endpoint
2234 			 */
2235 			if (inp->sctp_ep.local_hmacs)
2236 				sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2237 			inp->sctp_ep.local_hmacs =
2238 			    sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2239 			if (inp->sctp_ep.local_auth_chunks)
2240 				sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2241 			inp->sctp_ep.local_auth_chunks =
2242 			    sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2243 			(void)sctp_copy_skeylist(&(*inp_p)->sctp_ep.shared_keys,
2244 			    &inp->sctp_ep.shared_keys);
2245 
2246 			/*
2247 			 * Now we must move it from one hash table to
2248 			 * another and get the tcb in the right place.
2249 			 */
2250 			sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2251 
2252 			atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2253 			SCTP_TCB_UNLOCK((*stcb));
2254 
2255 			sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb, M_NOWAIT);
2256 			SCTP_TCB_LOCK((*stcb));
2257 			atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2258 
2259 
2260 			/*
2261 			 * now we must check to see if we were aborted while
2262 			 * the move was going on and the lock/unlock
2263 			 * happened.
2264 			 */
2265 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2266 				/*
2267 				 * yep it was, we leave the assoc attached
2268 				 * to the socket since the sctp_inpcb_free()
2269 				 * call will send an abort for us.
2270 				 */
2271 				SCTP_INP_DECR_REF(inp);
2272 				return (NULL);
2273 			}
2274 			SCTP_INP_DECR_REF(inp);
2275 			/* Switch over to the new guy */
2276 			*inp_p = inp;
2277 			sctp_ulp_notify(notification, *stcb, 0, NULL);
2278 
2279 			/*
2280 			 * Pull it from the incomplete queue and wake the
2281 			 * guy
2282 			 */
2283 			soisconnected(so);
2284 			return (m);
2285 		}
2286 	}
2287 	if ((notification) && ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
2288 		sctp_ulp_notify(notification, *stcb, 0, NULL);
2289 	}
2290 	return (m);
2291 }
2292 
2293 static void
2294 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp,
2295     struct sctp_tcb *stcb, struct sctp_nets *net)
2296 {
2297 	/* cp must not be used, others call this without a c-ack :-) */
2298 	struct sctp_association *asoc;
2299 
2300 	SCTPDBG(SCTP_DEBUG_INPUT2,
2301 	    "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2302 	if (stcb == NULL)
2303 		return;
2304 
2305 	asoc = &stcb->asoc;
2306 
2307 	sctp_stop_all_cookie_timers(stcb);
2308 	/* process according to association state */
2309 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2310 		/* state change only needed when I am in right state */
2311 		SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2312 		if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2313 			asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING;
2314 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2315 			    stcb->sctp_ep, stcb, asoc->primary_destination);
2316 
2317 		} else {
2318 			asoc->state = SCTP_STATE_OPEN;
2319 		}
2320 		/* update RTO */
2321 		SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2322 		SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2323 		if (asoc->overall_error_count == 0) {
2324 			net->RTO = sctp_calculate_rto(stcb, asoc, net,
2325 			    &asoc->time_entered, sctp_align_safe_nocopy);
2326 		}
2327 		(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2328 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL);
2329 		if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2330 		    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2331 			stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2332 			soisconnected(stcb->sctp_ep->sctp_socket);
2333 		}
2334 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2335 		    stcb, net);
2336 		/*
2337 		 * since we did not send a HB make sure we don't double
2338 		 * things
2339 		 */
2340 		net->hb_responded = 1;
2341 
2342 		if (stcb->asoc.sctp_autoclose_ticks &&
2343 		    sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2344 			sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2345 			    stcb->sctp_ep, stcb, NULL);
2346 		}
2347 		/*
2348 		 * set ASCONF timer if ASCONFs are pending and allowed (eg.
2349 		 * addresses changed when init/cookie echo in flight)
2350 		 */
2351 		if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
2352 		    (stcb->asoc.peer_supports_asconf) &&
2353 		    (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
2354 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2355 			    stcb->sctp_ep, stcb,
2356 			    stcb->asoc.primary_destination);
2357 		}
2358 	}
2359 	/* Toss the cookie if I can */
2360 	sctp_toss_old_cookies(stcb, asoc);
2361 	if (!TAILQ_EMPTY(&asoc->sent_queue)) {
2362 		/* Restart the timer if we have pending data */
2363 		struct sctp_tmit_chunk *chk;
2364 
2365 		chk = TAILQ_FIRST(&asoc->sent_queue);
2366 		if (chk) {
2367 			sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
2368 			    stcb, chk->whoTo);
2369 		}
2370 	}
2371 }
2372 
2373 static void
2374 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
2375     struct sctp_tcb *stcb)
2376 {
2377 	struct sctp_nets *net;
2378 	struct sctp_tmit_chunk *lchk;
2379 	uint32_t tsn;
2380 
2381 	if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_ecne_chunk)) {
2382 		return;
2383 	}
2384 	SCTP_STAT_INCR(sctps_recvecne);
2385 	tsn = ntohl(cp->tsn);
2386 	/* ECN Nonce stuff: need a resync and disable the nonce sum check */
2387 	/* Also we make sure we disable the nonce_wait */
2388 	lchk = TAILQ_FIRST(&stcb->asoc.send_queue);
2389 	if (lchk == NULL) {
2390 		stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq;
2391 	} else {
2392 		stcb->asoc.nonce_resync_tsn = lchk->rec.data.TSN_seq;
2393 	}
2394 	stcb->asoc.nonce_wait_for_ecne = 0;
2395 	stcb->asoc.nonce_sum_check = 0;
2396 
2397 	/* Find where it was sent, if possible */
2398 	net = NULL;
2399 	lchk = TAILQ_FIRST(&stcb->asoc.sent_queue);
2400 	while (lchk) {
2401 		if (lchk->rec.data.TSN_seq == tsn) {
2402 			net = lchk->whoTo;
2403 			break;
2404 		}
2405 		if (compare_with_wrap(lchk->rec.data.TSN_seq, tsn, MAX_SEQ))
2406 			break;
2407 		lchk = TAILQ_NEXT(lchk, sctp_next);
2408 	}
2409 	if (net == NULL)
2410 		/* default is we use the primary */
2411 		net = stcb->asoc.primary_destination;
2412 
2413 	if (compare_with_wrap(tsn, stcb->asoc.last_cwr_tsn, MAX_TSN)) {
2414 		/*
2415 		 * JRS - Use the congestion control given in the pluggable
2416 		 * CC module
2417 		 */
2418 		stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net);
2419 		/*
2420 		 * we reduce once every RTT. So we will only lower cwnd at
2421 		 * the next sending seq i.e. the resync_tsn.
2422 		 */
2423 		stcb->asoc.last_cwr_tsn = stcb->asoc.nonce_resync_tsn;
2424 	}
2425 	/*
2426 	 * We always send a CWR this way if our previous one was lost our
2427 	 * peer will get an update, or if it is not time again to reduce we
2428 	 * still get the cwr to the peer.
2429 	 */
2430 	sctp_send_cwr(stcb, net, tsn);
2431 }
2432 
2433 static void
2434 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb)
2435 {
2436 	/*
2437 	 * Here we get a CWR from the peer. We must look in the outqueue and
2438 	 * make sure that we have a covered ECNE in teh control chunk part.
2439 	 * If so remove it.
2440 	 */
2441 	struct sctp_tmit_chunk *chk;
2442 	struct sctp_ecne_chunk *ecne;
2443 
2444 	TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
2445 		if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
2446 			continue;
2447 		}
2448 		/*
2449 		 * Look for and remove if it is the right TSN. Since there
2450 		 * is only ONE ECNE on the control queue at any one time we
2451 		 * don't need to worry about more than one!
2452 		 */
2453 		ecne = mtod(chk->data, struct sctp_ecne_chunk *);
2454 		if (compare_with_wrap(ntohl(cp->tsn), ntohl(ecne->tsn),
2455 		    MAX_TSN) || (cp->tsn == ecne->tsn)) {
2456 			/* this covers this ECNE, we can remove it */
2457 			stcb->asoc.ecn_echo_cnt_onq--;
2458 			TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
2459 			    sctp_next);
2460 			if (chk->data) {
2461 				sctp_m_freem(chk->data);
2462 				chk->data = NULL;
2463 			}
2464 			stcb->asoc.ctrl_queue_cnt--;
2465 			sctp_free_a_chunk(stcb, chk);
2466 			break;
2467 		}
2468 	}
2469 }
2470 
2471 static void
2472 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp,
2473     struct sctp_tcb *stcb, struct sctp_nets *net)
2474 {
2475 	struct sctp_association *asoc;
2476 
2477 	SCTPDBG(SCTP_DEBUG_INPUT2,
2478 	    "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
2479 	if (stcb == NULL)
2480 		return;
2481 
2482 	asoc = &stcb->asoc;
2483 	/* process according to association state */
2484 	if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
2485 		/* unexpected SHUTDOWN-COMPLETE... so ignore... */
2486 		SCTP_TCB_UNLOCK(stcb);
2487 		return;
2488 	}
2489 	/* notify upper layer protocol */
2490 	if (stcb->sctp_socket) {
2491 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL);
2492 		/* are the queues empty? they should be */
2493 		if (!TAILQ_EMPTY(&asoc->send_queue) ||
2494 		    !TAILQ_EMPTY(&asoc->sent_queue) ||
2495 		    !TAILQ_EMPTY(&asoc->out_wheel)) {
2496 			sctp_report_all_outbound(stcb, 0);
2497 		}
2498 	}
2499 	/* stop the timer */
2500 	sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
2501 	SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
2502 	/* free the TCB */
2503 	sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
2504 	return;
2505 }
2506 
2507 static int
2508 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
2509     struct sctp_nets *net, uint8_t flg)
2510 {
2511 	switch (desc->chunk_type) {
2512 		case SCTP_DATA:
2513 		/* find the tsn to resend (possibly */
2514 		{
2515 			uint32_t tsn;
2516 			struct sctp_tmit_chunk *tp1;
2517 
2518 			tsn = ntohl(desc->tsn_ifany);
2519 			tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
2520 			while (tp1) {
2521 				if (tp1->rec.data.TSN_seq == tsn) {
2522 					/* found it */
2523 					break;
2524 				}
2525 				if (compare_with_wrap(tp1->rec.data.TSN_seq, tsn,
2526 				    MAX_TSN)) {
2527 					/* not found */
2528 					tp1 = NULL;
2529 					break;
2530 				}
2531 				tp1 = TAILQ_NEXT(tp1, sctp_next);
2532 			}
2533 			if (tp1 == NULL) {
2534 				/*
2535 				 * Do it the other way , aka without paying
2536 				 * attention to queue seq order.
2537 				 */
2538 				SCTP_STAT_INCR(sctps_pdrpdnfnd);
2539 				tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
2540 				while (tp1) {
2541 					if (tp1->rec.data.TSN_seq == tsn) {
2542 						/* found it */
2543 						break;
2544 					}
2545 					tp1 = TAILQ_NEXT(tp1, sctp_next);
2546 				}
2547 			}
2548 			if (tp1 == NULL) {
2549 				SCTP_STAT_INCR(sctps_pdrptsnnf);
2550 			}
2551 			if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
2552 				uint8_t *ddp;
2553 
2554 				if ((stcb->asoc.peers_rwnd == 0) &&
2555 				    ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
2556 					SCTP_STAT_INCR(sctps_pdrpdiwnp);
2557 					return (0);
2558 				}
2559 				if (stcb->asoc.peers_rwnd == 0 &&
2560 				    (flg & SCTP_FROM_MIDDLE_BOX)) {
2561 					SCTP_STAT_INCR(sctps_pdrpdizrw);
2562 					return (0);
2563 				}
2564 				ddp = (uint8_t *) (mtod(tp1->data, caddr_t)+
2565 				    sizeof(struct sctp_data_chunk));
2566 				{
2567 					unsigned int iii;
2568 
2569 					for (iii = 0; iii < sizeof(desc->data_bytes);
2570 					    iii++) {
2571 						if (ddp[iii] != desc->data_bytes[iii]) {
2572 							SCTP_STAT_INCR(sctps_pdrpbadd);
2573 							return (-1);
2574 						}
2575 					}
2576 				}
2577 				/*
2578 				 * We zero out the nonce so resync not
2579 				 * needed
2580 				 */
2581 				tp1->rec.data.ect_nonce = 0;
2582 
2583 				if (tp1->do_rtt) {
2584 					/*
2585 					 * this guy had a RTO calculation
2586 					 * pending on it, cancel it
2587 					 */
2588 					tp1->do_rtt = 0;
2589 				}
2590 				SCTP_STAT_INCR(sctps_pdrpmark);
2591 				if (tp1->sent != SCTP_DATAGRAM_RESEND)
2592 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
2593 				tp1->sent = SCTP_DATAGRAM_RESEND;
2594 				/*
2595 				 * mark it as if we were doing a FR, since
2596 				 * we will be getting gap ack reports behind
2597 				 * the info from the router.
2598 				 */
2599 				tp1->rec.data.doing_fast_retransmit = 1;
2600 				/*
2601 				 * mark the tsn with what sequences can
2602 				 * cause a new FR.
2603 				 */
2604 				if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
2605 					tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
2606 				} else {
2607 					tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
2608 				}
2609 
2610 				/* restart the timer */
2611 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
2612 				    stcb, tp1->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
2613 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
2614 				    stcb, tp1->whoTo);
2615 
2616 				/* fix counts and things */
2617 				if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
2618 					sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
2619 					    tp1->whoTo->flight_size,
2620 					    tp1->book_size,
2621 					    (uintptr_t) stcb,
2622 					    tp1->rec.data.TSN_seq);
2623 				}
2624 				sctp_flight_size_decrease(tp1);
2625 				sctp_total_flight_decrease(stcb, tp1);
2626 			} {
2627 				/* audit code */
2628 				unsigned int audit;
2629 
2630 				audit = 0;
2631 				TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
2632 					if (tp1->sent == SCTP_DATAGRAM_RESEND)
2633 						audit++;
2634 				}
2635 				TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
2636 				    sctp_next) {
2637 					if (tp1->sent == SCTP_DATAGRAM_RESEND)
2638 						audit++;
2639 				}
2640 				if (audit != stcb->asoc.sent_queue_retran_cnt) {
2641 					SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
2642 					    audit, stcb->asoc.sent_queue_retran_cnt);
2643 #ifndef SCTP_AUDITING_ENABLED
2644 					stcb->asoc.sent_queue_retran_cnt = audit;
2645 #endif
2646 				}
2647 			}
2648 		}
2649 		break;
2650 	case SCTP_ASCONF:
2651 		{
2652 			struct sctp_tmit_chunk *asconf;
2653 
2654 			TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
2655 			    sctp_next) {
2656 				if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
2657 					break;
2658 				}
2659 			}
2660 			if (asconf) {
2661 				if (asconf->sent != SCTP_DATAGRAM_RESEND)
2662 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
2663 				asconf->sent = SCTP_DATAGRAM_RESEND;
2664 				asconf->snd_count--;
2665 			}
2666 		}
2667 		break;
2668 	case SCTP_INITIATION:
2669 		/* resend the INIT */
2670 		stcb->asoc.dropped_special_cnt++;
2671 		if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
2672 			/*
2673 			 * If we can get it in, in a few attempts we do
2674 			 * this, otherwise we let the timer fire.
2675 			 */
2676 			sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
2677 			    stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
2678 			sctp_send_initiate(stcb->sctp_ep, stcb);
2679 		}
2680 		break;
2681 	case SCTP_SELECTIVE_ACK:
2682 		/* resend the sack */
2683 		sctp_send_sack(stcb);
2684 		break;
2685 	case SCTP_HEARTBEAT_REQUEST:
2686 		/* resend a demand HB */
2687 		if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
2688 			/*
2689 			 * Only retransmit if we KNOW we wont destroy the
2690 			 * tcb
2691 			 */
2692 			(void)sctp_send_hb(stcb, 1, net);
2693 		}
2694 		break;
2695 	case SCTP_SHUTDOWN:
2696 		sctp_send_shutdown(stcb, net);
2697 		break;
2698 	case SCTP_SHUTDOWN_ACK:
2699 		sctp_send_shutdown_ack(stcb, net);
2700 		break;
2701 	case SCTP_COOKIE_ECHO:
2702 		{
2703 			struct sctp_tmit_chunk *cookie;
2704 
2705 			cookie = NULL;
2706 			TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
2707 			    sctp_next) {
2708 				if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
2709 					break;
2710 				}
2711 			}
2712 			if (cookie) {
2713 				if (cookie->sent != SCTP_DATAGRAM_RESEND)
2714 					sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
2715 				cookie->sent = SCTP_DATAGRAM_RESEND;
2716 				sctp_stop_all_cookie_timers(stcb);
2717 			}
2718 		}
2719 		break;
2720 	case SCTP_COOKIE_ACK:
2721 		sctp_send_cookie_ack(stcb);
2722 		break;
2723 	case SCTP_ASCONF_ACK:
2724 		/* resend last asconf ack */
2725 		sctp_send_asconf_ack(stcb, 1);
2726 		break;
2727 	case SCTP_FORWARD_CUM_TSN:
2728 		send_forward_tsn(stcb, &stcb->asoc);
2729 		break;
2730 		/* can't do anything with these */
2731 	case SCTP_PACKET_DROPPED:
2732 	case SCTP_INITIATION_ACK:	/* this should not happen */
2733 	case SCTP_HEARTBEAT_ACK:
2734 	case SCTP_ABORT_ASSOCIATION:
2735 	case SCTP_OPERATION_ERROR:
2736 	case SCTP_SHUTDOWN_COMPLETE:
2737 	case SCTP_ECN_ECHO:
2738 	case SCTP_ECN_CWR:
2739 	default:
2740 		break;
2741 	}
2742 	return (0);
2743 }
2744 
2745 void
2746 sctp_reset_in_stream(struct sctp_tcb *stcb, int number_entries, uint16_t * list)
2747 {
2748 	int i;
2749 	uint16_t temp;
2750 
2751 	/*
2752 	 * We set things to 0xffff since this is the last delivered sequence
2753 	 * and we will be sending in 0 after the reset.
2754 	 */
2755 
2756 	if (number_entries) {
2757 		for (i = 0; i < number_entries; i++) {
2758 			temp = ntohs(list[i]);
2759 			if (temp >= stcb->asoc.streamincnt) {
2760 				continue;
2761 			}
2762 			stcb->asoc.strmin[temp].last_sequence_delivered = 0xffff;
2763 		}
2764 	} else {
2765 		list = NULL;
2766 		for (i = 0; i < stcb->asoc.streamincnt; i++) {
2767 			stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
2768 		}
2769 	}
2770 	sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list);
2771 }
2772 
2773 static void
2774 sctp_reset_out_streams(struct sctp_tcb *stcb, int number_entries, uint16_t * list)
2775 {
2776 	int i;
2777 
2778 	if (number_entries == 0) {
2779 		for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
2780 			stcb->asoc.strmout[i].next_sequence_sent = 0;
2781 		}
2782 	} else if (number_entries) {
2783 		for (i = 0; i < number_entries; i++) {
2784 			uint16_t temp;
2785 
2786 			temp = ntohs(list[i]);
2787 			if (temp >= stcb->asoc.streamoutcnt) {
2788 				/* no such stream */
2789 				continue;
2790 			}
2791 			stcb->asoc.strmout[temp].next_sequence_sent = 0;
2792 		}
2793 	}
2794 	sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list);
2795 }
2796 
2797 
2798 struct sctp_stream_reset_out_request *
2799 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
2800 {
2801 	struct sctp_association *asoc;
2802 	struct sctp_stream_reset_out_req *req;
2803 	struct sctp_stream_reset_out_request *r;
2804 	struct sctp_tmit_chunk *chk;
2805 	int len, clen;
2806 
2807 	asoc = &stcb->asoc;
2808 	if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
2809 		asoc->stream_reset_outstanding = 0;
2810 		return (NULL);
2811 	}
2812 	if (stcb->asoc.str_reset == NULL) {
2813 		asoc->stream_reset_outstanding = 0;
2814 		return (NULL);
2815 	}
2816 	chk = stcb->asoc.str_reset;
2817 	if (chk->data == NULL) {
2818 		return (NULL);
2819 	}
2820 	if (bchk) {
2821 		/* he wants a copy of the chk pointer */
2822 		*bchk = chk;
2823 	}
2824 	clen = chk->send_size;
2825 	req = mtod(chk->data, struct sctp_stream_reset_out_req *);
2826 	r = &req->sr_req;
2827 	if (ntohl(r->request_seq) == seq) {
2828 		/* found it */
2829 		return (r);
2830 	}
2831 	len = SCTP_SIZE32(ntohs(r->ph.param_length));
2832 	if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
2833 		/* move to the next one, there can only be a max of two */
2834 		r = (struct sctp_stream_reset_out_request *)((caddr_t)r + len);
2835 		if (ntohl(r->request_seq) == seq) {
2836 			return (r);
2837 		}
2838 	}
2839 	/* that seq is not here */
2840 	return (NULL);
2841 }
2842 
2843 static void
2844 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
2845 {
2846 	struct sctp_association *asoc;
2847 	struct sctp_tmit_chunk *chk = stcb->asoc.str_reset;
2848 
2849 	if (stcb->asoc.str_reset == NULL) {
2850 		return;
2851 	}
2852 	asoc = &stcb->asoc;
2853 
2854 	sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
2855 	TAILQ_REMOVE(&asoc->control_send_queue,
2856 	    chk,
2857 	    sctp_next);
2858 	if (chk->data) {
2859 		sctp_m_freem(chk->data);
2860 		chk->data = NULL;
2861 	}
2862 	asoc->ctrl_queue_cnt--;
2863 	sctp_free_a_chunk(stcb, chk);
2864 	stcb->asoc.str_reset = NULL;
2865 }
2866 
2867 
2868 static int
2869 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
2870     uint32_t seq, uint32_t action,
2871     struct sctp_stream_reset_response *respin)
2872 {
2873 	uint16_t type;
2874 	int lparm_len;
2875 	struct sctp_association *asoc = &stcb->asoc;
2876 	struct sctp_tmit_chunk *chk;
2877 	struct sctp_stream_reset_out_request *srparam;
2878 	int number_entries;
2879 
2880 	if (asoc->stream_reset_outstanding == 0) {
2881 		/* duplicate */
2882 		return (0);
2883 	}
2884 	if (seq == stcb->asoc.str_reset_seq_out) {
2885 		srparam = sctp_find_stream_reset(stcb, seq, &chk);
2886 		if (srparam) {
2887 			stcb->asoc.str_reset_seq_out++;
2888 			type = ntohs(srparam->ph.param_type);
2889 			lparm_len = ntohs(srparam->ph.param_length);
2890 			if (type == SCTP_STR_RESET_OUT_REQUEST) {
2891 				number_entries = (lparm_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
2892 				asoc->stream_reset_out_is_outstanding = 0;
2893 				if (asoc->stream_reset_outstanding)
2894 					asoc->stream_reset_outstanding--;
2895 				if (action == SCTP_STREAM_RESET_PERFORMED) {
2896 					/* do it */
2897 					sctp_reset_out_streams(stcb, number_entries, srparam->list_of_streams);
2898 				} else {
2899 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, srparam->list_of_streams);
2900 				}
2901 			} else if (type == SCTP_STR_RESET_IN_REQUEST) {
2902 				/* Answered my request */
2903 				number_entries = (lparm_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
2904 				if (asoc->stream_reset_outstanding)
2905 					asoc->stream_reset_outstanding--;
2906 				if (action != SCTP_STREAM_RESET_PERFORMED) {
2907 					sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb, number_entries, srparam->list_of_streams);
2908 				}
2909 			} else if (type == SCTP_STR_RESET_TSN_REQUEST) {
2910 				/**
2911 				 * a) Adopt the new in tsn.
2912 				 * b) reset the map
2913 				 * c) Adopt the new out-tsn
2914 				 */
2915 				struct sctp_stream_reset_response_tsn *resp;
2916 				struct sctp_forward_tsn_chunk fwdtsn;
2917 				int abort_flag = 0;
2918 
2919 				if (respin == NULL) {
2920 					/* huh ? */
2921 					return (0);
2922 				}
2923 				if (action == SCTP_STREAM_RESET_PERFORMED) {
2924 					resp = (struct sctp_stream_reset_response_tsn *)respin;
2925 					asoc->stream_reset_outstanding--;
2926 					fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
2927 					fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
2928 					fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
2929 					sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
2930 					if (abort_flag) {
2931 						return (1);
2932 					}
2933 					stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
2934 					stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
2935 					stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
2936 					memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
2937 					stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
2938 					stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
2939 
2940 					sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
2941 					sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
2942 
2943 				}
2944 			}
2945 			/* get rid of the request and get the request flags */
2946 			if (asoc->stream_reset_outstanding == 0) {
2947 				sctp_clean_up_stream_reset(stcb);
2948 			}
2949 		}
2950 	}
2951 	return (0);
2952 }
2953 
2954 static void
2955 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
2956     struct sctp_tmit_chunk *chk,
2957     struct sctp_stream_reset_in_request *req, int trunc)
2958 {
2959 	uint32_t seq;
2960 	int len, i;
2961 	int number_entries;
2962 	uint16_t temp;
2963 
2964 	/*
2965 	 * peer wants me to send a str-reset to him for my outgoing seq's if
2966 	 * seq_in is right.
2967 	 */
2968 	struct sctp_association *asoc = &stcb->asoc;
2969 
2970 	seq = ntohl(req->request_seq);
2971 	if (asoc->str_reset_seq_in == seq) {
2972 		if (trunc) {
2973 			/* Can't do it, since they exceeded our buffer size  */
2974 			asoc->last_reset_action[1] = asoc->last_reset_action[0];
2975 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED;
2976 			sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
2977 		} else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
2978 			len = ntohs(req->ph.param_length);
2979 			number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
2980 			for (i = 0; i < number_entries; i++) {
2981 				temp = ntohs(req->list_of_streams[i]);
2982 				req->list_of_streams[i] = temp;
2983 			}
2984 			/* move the reset action back one */
2985 			asoc->last_reset_action[1] = asoc->last_reset_action[0];
2986 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
2987 			sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams,
2988 			    asoc->str_reset_seq_out,
2989 			    seq, (asoc->sending_seq - 1));
2990 			asoc->stream_reset_out_is_outstanding = 1;
2991 			asoc->str_reset = chk;
2992 			sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
2993 			stcb->asoc.stream_reset_outstanding++;
2994 		} else {
2995 			/* Can't do it, since we have sent one out */
2996 			asoc->last_reset_action[1] = asoc->last_reset_action[0];
2997 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_TRY_LATER;
2998 			sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
2999 		}
3000 		asoc->str_reset_seq_in++;
3001 	} else if (asoc->str_reset_seq_in - 1 == seq) {
3002 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3003 	} else if (asoc->str_reset_seq_in - 2 == seq) {
3004 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3005 	} else {
3006 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3007 	}
3008 }
3009 
3010 static int
3011 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3012     struct sctp_tmit_chunk *chk,
3013     struct sctp_stream_reset_tsn_request *req)
3014 {
3015 	/* reset all in and out and update the tsn */
3016 	/*
3017 	 * A) reset my str-seq's on in and out. B) Select a receive next,
3018 	 * and set cum-ack to it. Also process this selected number as a
3019 	 * fwd-tsn as well. C) set in the response my next sending seq.
3020 	 */
3021 	struct sctp_forward_tsn_chunk fwdtsn;
3022 	struct sctp_association *asoc = &stcb->asoc;
3023 	int abort_flag = 0;
3024 	uint32_t seq;
3025 
3026 	seq = ntohl(req->request_seq);
3027 	if (asoc->str_reset_seq_in == seq) {
3028 		fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3029 		fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3030 		fwdtsn.ch.chunk_flags = 0;
3031 		fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3032 		sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3033 		if (abort_flag) {
3034 			return (1);
3035 		}
3036 		stcb->asoc.highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3037 		stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3038 		stcb->asoc.mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1;
3039 		memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3040 		atomic_add_int(&stcb->asoc.sending_seq, 1);
3041 		/* save off historical data for retrans */
3042 		stcb->asoc.last_sending_seq[1] = stcb->asoc.last_sending_seq[0];
3043 		stcb->asoc.last_sending_seq[0] = stcb->asoc.sending_seq;
3044 		stcb->asoc.last_base_tsnsent[1] = stcb->asoc.last_base_tsnsent[0];
3045 		stcb->asoc.last_base_tsnsent[0] = stcb->asoc.mapping_array_base_tsn;
3046 
3047 		sctp_add_stream_reset_result_tsn(chk,
3048 		    ntohl(req->request_seq),
3049 		    SCTP_STREAM_RESET_PERFORMED,
3050 		    stcb->asoc.sending_seq,
3051 		    stcb->asoc.mapping_array_base_tsn);
3052 		sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3053 		sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3054 		stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3055 		stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3056 
3057 		asoc->str_reset_seq_in++;
3058 	} else if (asoc->str_reset_seq_in - 1 == seq) {
3059 		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3060 		    stcb->asoc.last_sending_seq[0],
3061 		    stcb->asoc.last_base_tsnsent[0]
3062 		    );
3063 	} else if (asoc->str_reset_seq_in - 2 == seq) {
3064 		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3065 		    stcb->asoc.last_sending_seq[1],
3066 		    stcb->asoc.last_base_tsnsent[1]
3067 		    );
3068 	} else {
3069 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3070 	}
3071 	return (0);
3072 }
3073 
3074 static void
3075 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3076     struct sctp_tmit_chunk *chk,
3077     struct sctp_stream_reset_out_request *req, int trunc)
3078 {
3079 	uint32_t seq, tsn;
3080 	int number_entries, len;
3081 	struct sctp_association *asoc = &stcb->asoc;
3082 
3083 	seq = ntohl(req->request_seq);
3084 
3085 	/* now if its not a duplicate we process it */
3086 	if (asoc->str_reset_seq_in == seq) {
3087 		len = ntohs(req->ph.param_length);
3088 		number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3089 		/*
3090 		 * the sender is resetting, handle the list issue.. we must
3091 		 * a) verify if we can do the reset, if so no problem b) If
3092 		 * we can't do the reset we must copy the request. c) queue
3093 		 * it, and setup the data in processor to trigger it off
3094 		 * when needed and dequeue all the queued data.
3095 		 */
3096 		tsn = ntohl(req->send_reset_at_tsn);
3097 
3098 		/* move the reset action back one */
3099 		asoc->last_reset_action[1] = asoc->last_reset_action[0];
3100 		if (trunc) {
3101 			sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED);
3102 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED;
3103 		} else if ((tsn == asoc->cumulative_tsn) ||
3104 		    (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN))) {
3105 			/* we can do it now */
3106 			sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3107 			sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED);
3108 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3109 		} else {
3110 			/*
3111 			 * we must queue it up and thus wait for the TSN's
3112 			 * to arrive that are at or before tsn
3113 			 */
3114 			struct sctp_stream_reset_list *liste;
3115 			int siz;
3116 
3117 			siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3118 			SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3119 			    siz, SCTP_M_STRESET);
3120 			if (liste == NULL) {
3121 				/* gak out of memory */
3122 				sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED);
3123 				asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED;
3124 				return;
3125 			}
3126 			liste->tsn = tsn;
3127 			liste->number_entries = number_entries;
3128 			memcpy(&liste->req, req,
3129 			    (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t))));
3130 			TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
3131 			sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED);
3132 			asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3133 		}
3134 		asoc->str_reset_seq_in++;
3135 	} else if ((asoc->str_reset_seq_in - 1) == seq) {
3136 		/*
3137 		 * one seq back, just echo back last action since my
3138 		 * response was lost.
3139 		 */
3140 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3141 	} else if ((asoc->str_reset_seq_in - 2) == seq) {
3142 		/*
3143 		 * two seq back, just echo back last action since my
3144 		 * response was lost.
3145 		 */
3146 		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3147 	} else {
3148 		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3149 	}
3150 }
3151 
3152 #ifdef __GNUC__
3153 __attribute__((noinline))
3154 #endif
3155 	static int
3156 	    sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3157         struct sctp_stream_reset_out_req *sr_req)
3158 {
3159 	int chk_length, param_len, ptype;
3160 	struct sctp_paramhdr pstore;
3161 	uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
3162 
3163 	uint32_t seq;
3164 	int num_req = 0;
3165 	int trunc = 0;
3166 	struct sctp_tmit_chunk *chk;
3167 	struct sctp_chunkhdr *ch;
3168 	struct sctp_paramhdr *ph;
3169 	int ret_code = 0;
3170 	int num_param = 0;
3171 
3172 	/* now it may be a reset or a reset-response */
3173 	chk_length = ntohs(sr_req->ch.chunk_length);
3174 
3175 	/* setup for adding the response */
3176 	sctp_alloc_a_chunk(stcb, chk);
3177 	if (chk == NULL) {
3178 		return (ret_code);
3179 	}
3180 	chk->rec.chunk_id.id = SCTP_STREAM_RESET;
3181 	chk->rec.chunk_id.can_take_data = 0;
3182 	chk->asoc = &stcb->asoc;
3183 	chk->no_fr_allowed = 0;
3184 	chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
3185 	chk->book_size_scale = 0;
3186 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
3187 	if (chk->data == NULL) {
3188 strres_nochunk:
3189 		if (chk->data) {
3190 			sctp_m_freem(chk->data);
3191 			chk->data = NULL;
3192 		}
3193 		sctp_free_a_chunk(stcb, chk);
3194 		return (ret_code);
3195 	}
3196 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
3197 
3198 	/* setup chunk parameters */
3199 	chk->sent = SCTP_DATAGRAM_UNSENT;
3200 	chk->snd_count = 0;
3201 	chk->whoTo = stcb->asoc.primary_destination;
3202 	atomic_add_int(&chk->whoTo->ref_count, 1);
3203 
3204 	ch = mtod(chk->data, struct sctp_chunkhdr *);
3205 	ch->chunk_type = SCTP_STREAM_RESET;
3206 	ch->chunk_flags = 0;
3207 	ch->chunk_length = htons(chk->send_size);
3208 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
3209 	offset += sizeof(struct sctp_chunkhdr);
3210 	while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) {
3211 		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore);
3212 		if (ph == NULL)
3213 			break;
3214 		param_len = ntohs(ph->param_length);
3215 		if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) {
3216 			/* bad param */
3217 			break;
3218 		}
3219 		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)),
3220 		    (uint8_t *) & cstore);
3221 		ptype = ntohs(ph->param_type);
3222 		num_param++;
3223 		if (param_len > (int)sizeof(cstore)) {
3224 			trunc = 1;
3225 		} else {
3226 			trunc = 0;
3227 		}
3228 
3229 		if (num_param > SCTP_MAX_RESET_PARAMS) {
3230 			/* hit the max of parameters already sorry.. */
3231 			break;
3232 		}
3233 		if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
3234 			struct sctp_stream_reset_out_request *req_out;
3235 
3236 			req_out = (struct sctp_stream_reset_out_request *)ph;
3237 			num_req++;
3238 			if (stcb->asoc.stream_reset_outstanding) {
3239 				seq = ntohl(req_out->response_seq);
3240 				if (seq == stcb->asoc.str_reset_seq_out) {
3241 					/* implicit ack */
3242 					(void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_PERFORMED, NULL);
3243 				}
3244 			}
3245 			sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
3246 		} else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
3247 			struct sctp_stream_reset_in_request *req_in;
3248 
3249 			num_req++;
3250 
3251 			req_in = (struct sctp_stream_reset_in_request *)ph;
3252 
3253 			sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
3254 		} else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
3255 			struct sctp_stream_reset_tsn_request *req_tsn;
3256 
3257 			num_req++;
3258 			req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
3259 
3260 			if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
3261 				ret_code = 1;
3262 				goto strres_nochunk;
3263 			}
3264 			/* no more */
3265 			break;
3266 		} else if (ptype == SCTP_STR_RESET_RESPONSE) {
3267 			struct sctp_stream_reset_response *resp;
3268 			uint32_t result;
3269 
3270 			resp = (struct sctp_stream_reset_response *)ph;
3271 			seq = ntohl(resp->response_seq);
3272 			result = ntohl(resp->result);
3273 			if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
3274 				ret_code = 1;
3275 				goto strres_nochunk;
3276 			}
3277 		} else {
3278 			break;
3279 		}
3280 		offset += SCTP_SIZE32(param_len);
3281 		chk_length -= SCTP_SIZE32(param_len);
3282 	}
3283 	if (num_req == 0) {
3284 		/* we have no response free the stuff */
3285 		goto strres_nochunk;
3286 	}
3287 	/* ok we have a chunk to link in */
3288 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
3289 	    chk,
3290 	    sctp_next);
3291 	stcb->asoc.ctrl_queue_cnt++;
3292 	return (ret_code);
3293 }
3294 
3295 /*
3296  * Handle a router or endpoints report of a packet loss, there are two ways
3297  * to handle this, either we get the whole packet and must disect it
3298  * ourselves (possibly with truncation and or corruption) or it is a summary
3299  * from a middle box that did the disectting for us.
3300  */
3301 static void
3302 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
3303     struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
3304 {
3305 	uint32_t bottle_bw, on_queue;
3306 	uint16_t trunc_len;
3307 	unsigned int chlen;
3308 	unsigned int at;
3309 	struct sctp_chunk_desc desc;
3310 	struct sctp_chunkhdr *ch;
3311 
3312 	chlen = ntohs(cp->ch.chunk_length);
3313 	chlen -= sizeof(struct sctp_pktdrop_chunk);
3314 	/* XXX possible chlen underflow */
3315 	if (chlen == 0) {
3316 		ch = NULL;
3317 		if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
3318 			SCTP_STAT_INCR(sctps_pdrpbwrpt);
3319 	} else {
3320 		ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
3321 		chlen -= sizeof(struct sctphdr);
3322 		/* XXX possible chlen underflow */
3323 		memset(&desc, 0, sizeof(desc));
3324 	}
3325 	trunc_len = (uint16_t) ntohs(cp->trunc_len);
3326 	if (trunc_len > limit) {
3327 		trunc_len = limit;
3328 	}
3329 	/* now the chunks themselves */
3330 	while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
3331 		desc.chunk_type = ch->chunk_type;
3332 		/* get amount we need to move */
3333 		at = ntohs(ch->chunk_length);
3334 		if (at < sizeof(struct sctp_chunkhdr)) {
3335 			/* corrupt chunk, maybe at the end? */
3336 			SCTP_STAT_INCR(sctps_pdrpcrupt);
3337 			break;
3338 		}
3339 		if (trunc_len == 0) {
3340 			/* we are supposed to have all of it */
3341 			if (at > chlen) {
3342 				/* corrupt skip it */
3343 				SCTP_STAT_INCR(sctps_pdrpcrupt);
3344 				break;
3345 			}
3346 		} else {
3347 			/* is there enough of it left ? */
3348 			if (desc.chunk_type == SCTP_DATA) {
3349 				if (chlen < (sizeof(struct sctp_data_chunk) +
3350 				    sizeof(desc.data_bytes))) {
3351 					break;
3352 				}
3353 			} else {
3354 				if (chlen < sizeof(struct sctp_chunkhdr)) {
3355 					break;
3356 				}
3357 			}
3358 		}
3359 		if (desc.chunk_type == SCTP_DATA) {
3360 			/* can we get out the tsn? */
3361 			if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
3362 				SCTP_STAT_INCR(sctps_pdrpmbda);
3363 
3364 			if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
3365 				/* yep */
3366 				struct sctp_data_chunk *dcp;
3367 				uint8_t *ddp;
3368 				unsigned int iii;
3369 
3370 				dcp = (struct sctp_data_chunk *)ch;
3371 				ddp = (uint8_t *) (dcp + 1);
3372 				for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
3373 					desc.data_bytes[iii] = ddp[iii];
3374 				}
3375 				desc.tsn_ifany = dcp->dp.tsn;
3376 			} else {
3377 				/* nope we are done. */
3378 				SCTP_STAT_INCR(sctps_pdrpnedat);
3379 				break;
3380 			}
3381 		} else {
3382 			if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
3383 				SCTP_STAT_INCR(sctps_pdrpmbct);
3384 		}
3385 
3386 		if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
3387 			SCTP_STAT_INCR(sctps_pdrppdbrk);
3388 			break;
3389 		}
3390 		if (SCTP_SIZE32(at) > chlen) {
3391 			break;
3392 		}
3393 		chlen -= SCTP_SIZE32(at);
3394 		if (chlen < sizeof(struct sctp_chunkhdr)) {
3395 			/* done, none left */
3396 			break;
3397 		}
3398 		ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
3399 	}
3400 	/* Now update any rwnd --- possibly */
3401 	if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
3402 		/* From a peer, we get a rwnd report */
3403 		uint32_t a_rwnd;
3404 
3405 		SCTP_STAT_INCR(sctps_pdrpfehos);
3406 
3407 		bottle_bw = ntohl(cp->bottle_bw);
3408 		on_queue = ntohl(cp->current_onq);
3409 		if (bottle_bw && on_queue) {
3410 			/* a rwnd report is in here */
3411 			if (bottle_bw > on_queue)
3412 				a_rwnd = bottle_bw - on_queue;
3413 			else
3414 				a_rwnd = 0;
3415 
3416 			if (a_rwnd == 0)
3417 				stcb->asoc.peers_rwnd = 0;
3418 			else {
3419 				if (a_rwnd > stcb->asoc.total_flight) {
3420 					stcb->asoc.peers_rwnd =
3421 					    a_rwnd - stcb->asoc.total_flight;
3422 				} else {
3423 					stcb->asoc.peers_rwnd = 0;
3424 				}
3425 				if (stcb->asoc.peers_rwnd <
3426 				    stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
3427 					/* SWS sender side engages */
3428 					stcb->asoc.peers_rwnd = 0;
3429 				}
3430 			}
3431 		}
3432 	} else {
3433 		SCTP_STAT_INCR(sctps_pdrpfmbox);
3434 	}
3435 
3436 	/* now middle boxes in sat networks get a cwnd bump */
3437 	if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
3438 	    (stcb->asoc.sat_t3_loss_recovery == 0) &&
3439 	    (stcb->asoc.sat_network)) {
3440 		/*
3441 		 * This is debateable but for sat networks it makes sense
3442 		 * Note if a T3 timer has went off, we will prohibit any
3443 		 * changes to cwnd until we exit the t3 loss recovery.
3444 		 */
3445 		stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
3446 		    net, cp, &bottle_bw, &on_queue);
3447 	}
3448 }
3449 
3450 /*
3451  * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
3452  * still contain IP/SCTP header - stcb: is the tcb found for this packet -
3453  * offset: offset into the mbuf chain to first chunkhdr - length: is the
3454  * length of the complete packet outputs: - length: modified to remaining
3455  * length after control processing - netp: modified to new sctp_nets after
3456  * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
3457  * bad packet,...) otherwise return the tcb for this packet
3458  */
3459 #ifdef __GNUC__
3460 __attribute__((noinline))
3461 #endif
3462 	static struct sctp_tcb *
3463 	         sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
3464              struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
3465              struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
3466              uint32_t vrf_id)
3467 {
3468 	struct sctp_association *asoc;
3469 	uint32_t vtag_in;
3470 	int num_chunks = 0;	/* number of control chunks processed */
3471 	uint32_t chk_length;
3472 	int ret;
3473 	int abort_no_unlock = 0;
3474 
3475 	/*
3476 	 * How big should this be, and should it be alloc'd? Lets try the
3477 	 * d-mtu-ceiling for now (2k) and that should hopefully work ...
3478 	 * until we get into jumbo grams and such..
3479 	 */
3480 	uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
3481 	struct sctp_tcb *locked_tcb = stcb;
3482 	int got_auth = 0;
3483 	uint32_t auth_offset = 0, auth_len = 0;
3484 	int auth_skipped = 0;
3485 
3486 	SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
3487 	    iphlen, *offset, length, stcb);
3488 
3489 	/* validate chunk header length... */
3490 	if (ntohs(ch->chunk_length) < sizeof(*ch)) {
3491 		SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
3492 		    ntohs(ch->chunk_length));
3493 		if (locked_tcb) {
3494 			SCTP_TCB_UNLOCK(locked_tcb);
3495 		}
3496 		return (NULL);
3497 	}
3498 	/*
3499 	 * validate the verification tag
3500 	 */
3501 	vtag_in = ntohl(sh->v_tag);
3502 
3503 	if (locked_tcb) {
3504 		SCTP_TCB_LOCK_ASSERT(locked_tcb);
3505 	}
3506 	if (ch->chunk_type == SCTP_INITIATION) {
3507 		SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
3508 		    ntohs(ch->chunk_length), vtag_in);
3509 		if (vtag_in != 0) {
3510 			/* protocol error- silently discard... */
3511 			SCTP_STAT_INCR(sctps_badvtag);
3512 			if (locked_tcb) {
3513 				SCTP_TCB_UNLOCK(locked_tcb);
3514 			}
3515 			return (NULL);
3516 		}
3517 	} else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
3518 		/*
3519 		 * If there is no stcb, skip the AUTH chunk and process
3520 		 * later after a stcb is found (to validate the lookup was
3521 		 * valid.
3522 		 */
3523 		if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
3524 		    (stcb == NULL) && !sctp_auth_disable) {
3525 			/* save this chunk for later processing */
3526 			auth_skipped = 1;
3527 			auth_offset = *offset;
3528 			auth_len = ntohs(ch->chunk_length);
3529 
3530 			/* (temporarily) move past this chunk */
3531 			*offset += SCTP_SIZE32(auth_len);
3532 			if (*offset >= length) {
3533 				/* no more data left in the mbuf chain */
3534 				*offset = length;
3535 				if (locked_tcb) {
3536 					SCTP_TCB_UNLOCK(locked_tcb);
3537 				}
3538 				return (NULL);
3539 			}
3540 			ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3541 			    sizeof(struct sctp_chunkhdr), chunk_buf);
3542 		}
3543 		if (ch == NULL) {
3544 			/* Help */
3545 			*offset = length;
3546 			if (locked_tcb) {
3547 				SCTP_TCB_UNLOCK(locked_tcb);
3548 			}
3549 			return (NULL);
3550 		}
3551 		if (ch->chunk_type == SCTP_COOKIE_ECHO) {
3552 			goto process_control_chunks;
3553 		}
3554 		/*
3555 		 * first check if it's an ASCONF with an unknown src addr we
3556 		 * need to look inside to find the association
3557 		 */
3558 		if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
3559 			/* inp's refcount may be reduced */
3560 			SCTP_INP_INCR_REF(inp);
3561 
3562 			stcb = sctp_findassociation_ep_asconf(m, iphlen,
3563 			    *offset, sh, &inp, netp);
3564 			if (stcb == NULL) {
3565 				/*
3566 				 * reduce inp's refcount if not reduced in
3567 				 * sctp_findassociation_ep_asconf().
3568 				 */
3569 				SCTP_INP_DECR_REF(inp);
3570 			}
3571 			/* now go back and verify any auth chunk to be sure */
3572 			if (auth_skipped && (stcb != NULL)) {
3573 				struct sctp_auth_chunk *auth;
3574 
3575 				auth = (struct sctp_auth_chunk *)
3576 				    sctp_m_getptr(m, auth_offset,
3577 				    auth_len, chunk_buf);
3578 				got_auth = 1;
3579 				auth_skipped = 0;
3580 				if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
3581 				    auth_offset)) {
3582 					/* auth HMAC failed so dump it */
3583 					*offset = length;
3584 					if (locked_tcb) {
3585 						SCTP_TCB_UNLOCK(locked_tcb);
3586 					}
3587 					return (NULL);
3588 				} else {
3589 					/* remaining chunks are HMAC checked */
3590 					stcb->asoc.authenticated = 1;
3591 				}
3592 			}
3593 		}
3594 		if (stcb == NULL) {
3595 			/* no association, so it's out of the blue... */
3596 			sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL,
3597 			    vrf_id);
3598 			*offset = length;
3599 			if (locked_tcb) {
3600 				SCTP_TCB_UNLOCK(locked_tcb);
3601 			}
3602 			return (NULL);
3603 		}
3604 		asoc = &stcb->asoc;
3605 		/* ABORT and SHUTDOWN can use either v_tag... */
3606 		if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
3607 		    (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
3608 		    (ch->chunk_type == SCTP_PACKET_DROPPED)) {
3609 			if ((vtag_in == asoc->my_vtag) ||
3610 			    ((ch->chunk_flags & SCTP_HAD_NO_TCB) &&
3611 			    (vtag_in == asoc->peer_vtag))) {
3612 				/* this is valid */
3613 			} else {
3614 				/* drop this packet... */
3615 				SCTP_STAT_INCR(sctps_badvtag);
3616 				if (locked_tcb) {
3617 					SCTP_TCB_UNLOCK(locked_tcb);
3618 				}
3619 				return (NULL);
3620 			}
3621 		} else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
3622 			if (vtag_in != asoc->my_vtag) {
3623 				/*
3624 				 * this could be a stale SHUTDOWN-ACK or the
3625 				 * peer never got the SHUTDOWN-COMPLETE and
3626 				 * is still hung; we have started a new asoc
3627 				 * but it won't complete until the shutdown
3628 				 * is completed
3629 				 */
3630 				if (locked_tcb) {
3631 					SCTP_TCB_UNLOCK(locked_tcb);
3632 				}
3633 				sctp_handle_ootb(m, iphlen, *offset, sh, inp,
3634 				    NULL, vrf_id);
3635 				return (NULL);
3636 			}
3637 		} else {
3638 			/* for all other chunks, vtag must match */
3639 			if (vtag_in != asoc->my_vtag) {
3640 				/* invalid vtag... */
3641 				SCTPDBG(SCTP_DEBUG_INPUT3,
3642 				    "invalid vtag: %xh, expect %xh\n",
3643 				    vtag_in, asoc->my_vtag);
3644 				SCTP_STAT_INCR(sctps_badvtag);
3645 				if (locked_tcb) {
3646 					SCTP_TCB_UNLOCK(locked_tcb);
3647 				}
3648 				*offset = length;
3649 				return (NULL);
3650 			}
3651 		}
3652 	}			/* end if !SCTP_COOKIE_ECHO */
3653 	/*
3654 	 * process all control chunks...
3655 	 */
3656 	if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
3657 	    (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
3658 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
3659 		/* implied cookie-ack.. we must have lost the ack */
3660 		stcb->asoc.overall_error_count = 0;
3661 		sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
3662 		    *netp);
3663 	}
3664 process_control_chunks:
3665 	while (IS_SCTP_CONTROL(ch)) {
3666 		/* validate chunk length */
3667 		chk_length = ntohs(ch->chunk_length);
3668 		SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
3669 		    ch->chunk_type, chk_length);
3670 		SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
3671 		if (chk_length < sizeof(*ch) ||
3672 		    (*offset + (int)chk_length) > length) {
3673 			*offset = length;
3674 			if (locked_tcb) {
3675 				SCTP_TCB_UNLOCK(locked_tcb);
3676 			}
3677 			return (NULL);
3678 		}
3679 		SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
3680 		/*
3681 		 * INIT-ACK only gets the init ack "header" portion only
3682 		 * because we don't have to process the peer's COOKIE. All
3683 		 * others get a complete chunk.
3684 		 */
3685 		if ((ch->chunk_type == SCTP_INITIATION_ACK) ||
3686 		    (ch->chunk_type == SCTP_INITIATION)) {
3687 			/* get an init-ack chunk */
3688 			ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3689 			    sizeof(struct sctp_init_ack_chunk), chunk_buf);
3690 			if (ch == NULL) {
3691 				*offset = length;
3692 				if (locked_tcb) {
3693 					SCTP_TCB_UNLOCK(locked_tcb);
3694 				}
3695 				return (NULL);
3696 			}
3697 		} else {
3698 			/* For cookies and all other chunks. */
3699 			if (chk_length > sizeof(chunk_buf)) {
3700 				/*
3701 				 * use just the size of the chunk buffer so
3702 				 * the front part of our chunks fit in
3703 				 * contiguous space up to the chunk buffer
3704 				 * size (508 bytes). For chunks that need to
3705 				 * get more than that they must use the
3706 				 * sctp_m_getptr() function or other means
3707 				 * (e.g. know how to parse mbuf chains).
3708 				 * Cookies do this already.
3709 				 */
3710 				ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3711 				    (sizeof(chunk_buf) - 4),
3712 				    chunk_buf);
3713 				if (ch == NULL) {
3714 					*offset = length;
3715 					if (locked_tcb) {
3716 						SCTP_TCB_UNLOCK(locked_tcb);
3717 					}
3718 					return (NULL);
3719 				}
3720 			} else {
3721 				/* We can fit it all */
3722 				ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3723 				    chk_length, chunk_buf);
3724 				if (ch == NULL) {
3725 					SCTP_PRINTF("sctp_process_control: Can't get the all data....\n");
3726 					*offset = length;
3727 					if (locked_tcb) {
3728 						SCTP_TCB_UNLOCK(locked_tcb);
3729 					}
3730 					return (NULL);
3731 				}
3732 			}
3733 		}
3734 		num_chunks++;
3735 		/* Save off the last place we got a control from */
3736 		if (stcb != NULL) {
3737 			if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
3738 				/*
3739 				 * allow last_control to be NULL if
3740 				 * ASCONF... ASCONF processing will find the
3741 				 * right net later
3742 				 */
3743 				if ((netp != NULL) && (*netp != NULL))
3744 					stcb->asoc.last_control_chunk_from = *netp;
3745 			}
3746 		}
3747 #ifdef SCTP_AUDITING_ENABLED
3748 		sctp_audit_log(0xB0, ch->chunk_type);
3749 #endif
3750 
3751 		/* check to see if this chunk required auth, but isn't */
3752 		if ((stcb != NULL) && !sctp_auth_disable &&
3753 		    sctp_auth_is_required_chunk(ch->chunk_type,
3754 		    stcb->asoc.local_auth_chunks) &&
3755 		    !stcb->asoc.authenticated) {
3756 			/* "silently" ignore */
3757 			SCTP_STAT_INCR(sctps_recvauthmissing);
3758 			goto next_chunk;
3759 		}
3760 		switch (ch->chunk_type) {
3761 		case SCTP_INITIATION:
3762 			/* must be first and only chunk */
3763 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
3764 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3765 				/* We are not interested anymore? */
3766 				if ((stcb) && (stcb->asoc.total_output_queue_size)) {
3767 					/*
3768 					 * collision case where we are
3769 					 * sending to them too
3770 					 */
3771 					;
3772 				} else {
3773 					if (locked_tcb) {
3774 						SCTP_TCB_UNLOCK(locked_tcb);
3775 					}
3776 					*offset = length;
3777 					return (NULL);
3778 				}
3779 			}
3780 			if ((num_chunks > 1) ||
3781 			    (sctp_strict_init && (length - *offset > (int)SCTP_SIZE32(chk_length)))) {
3782 				*offset = length;
3783 				if (locked_tcb) {
3784 					SCTP_TCB_UNLOCK(locked_tcb);
3785 				}
3786 				return (NULL);
3787 			}
3788 			if ((stcb != NULL) &&
3789 			    (SCTP_GET_STATE(&stcb->asoc) ==
3790 			    SCTP_STATE_SHUTDOWN_ACK_SENT)) {
3791 				sctp_send_shutdown_ack(stcb,
3792 				    stcb->asoc.primary_destination);
3793 				*offset = length;
3794 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC);
3795 				if (locked_tcb) {
3796 					SCTP_TCB_UNLOCK(locked_tcb);
3797 				}
3798 				return (NULL);
3799 			}
3800 			if (netp) {
3801 				sctp_handle_init(m, iphlen, *offset, sh,
3802 				    (struct sctp_init_chunk *)ch, inp,
3803 				    stcb, *netp, &abort_no_unlock, vrf_id);
3804 			}
3805 			if (abort_no_unlock)
3806 				return (NULL);
3807 
3808 			*offset = length;
3809 			if (locked_tcb) {
3810 				SCTP_TCB_UNLOCK(locked_tcb);
3811 			}
3812 			return (NULL);
3813 			break;
3814 		case SCTP_PAD_CHUNK:
3815 			break;
3816 		case SCTP_INITIATION_ACK:
3817 			/* must be first and only chunk */
3818 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n");
3819 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3820 				/* We are not interested anymore */
3821 				if ((stcb) && (stcb->asoc.total_output_queue_size)) {
3822 					;
3823 				} else {
3824 					if (locked_tcb) {
3825 						SCTP_TCB_UNLOCK(locked_tcb);
3826 					}
3827 					*offset = length;
3828 					if (stcb) {
3829 						sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
3830 					}
3831 					return (NULL);
3832 				}
3833 			}
3834 			if ((num_chunks > 1) ||
3835 			    (sctp_strict_init && (length - *offset > (int)SCTP_SIZE32(chk_length)))) {
3836 				*offset = length;
3837 				if (locked_tcb) {
3838 					SCTP_TCB_UNLOCK(locked_tcb);
3839 				}
3840 				return (NULL);
3841 			}
3842 			if ((netp) && (*netp)) {
3843 				ret = sctp_handle_init_ack(m, iphlen, *offset, sh,
3844 				    (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id);
3845 			} else {
3846 				ret = -1;
3847 			}
3848 			/*
3849 			 * Special case, I must call the output routine to
3850 			 * get the cookie echoed
3851 			 */
3852 			if (abort_no_unlock)
3853 				return (NULL);
3854 
3855 			if ((stcb) && ret == 0)
3856 				sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC);
3857 			*offset = length;
3858 			if (locked_tcb) {
3859 				SCTP_TCB_UNLOCK(locked_tcb);
3860 			}
3861 			return (NULL);
3862 			break;
3863 		case SCTP_SELECTIVE_ACK:
3864 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n");
3865 			SCTP_STAT_INCR(sctps_recvsacks);
3866 			{
3867 				struct sctp_sack_chunk *sack;
3868 				int abort_now = 0;
3869 				uint32_t a_rwnd, cum_ack;
3870 				uint16_t num_seg;
3871 				int nonce_sum_flag;
3872 
3873 				if ((stcb == NULL) || (chk_length < sizeof(struct sctp_sack_chunk))) {
3874 					SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on sack chunk, too small\n");
3875 					*offset = length;
3876 					if (locked_tcb) {
3877 						SCTP_TCB_UNLOCK(locked_tcb);
3878 					}
3879 					return (NULL);
3880 				}
3881 				sack = (struct sctp_sack_chunk *)ch;
3882 				nonce_sum_flag = ch->chunk_flags & SCTP_SACK_NONCE_SUM;
3883 				cum_ack = ntohl(sack->sack.cum_tsn_ack);
3884 				num_seg = ntohs(sack->sack.num_gap_ack_blks);
3885 				a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd);
3886 				SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
3887 				    cum_ack,
3888 				    num_seg,
3889 				    a_rwnd
3890 				    );
3891 				stcb->asoc.seen_a_sack_this_pkt = 1;
3892 				if ((stcb->asoc.pr_sctp_cnt == 0) &&
3893 				    (num_seg == 0) &&
3894 				    ((compare_with_wrap(cum_ack, stcb->asoc.last_acked_seq, MAX_TSN)) ||
3895 				    (cum_ack == stcb->asoc.last_acked_seq)) &&
3896 				    (stcb->asoc.saw_sack_with_frags == 0) &&
3897 				    (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
3898 				    ) {
3899 					/*
3900 					 * We have a SIMPLE sack having no
3901 					 * prior segments and data on sent
3902 					 * queue to be acked.. Use the
3903 					 * faster path sack processing. We
3904 					 * also allow window update sacks
3905 					 * with no missing segments to go
3906 					 * this way too.
3907 					 */
3908 					sctp_express_handle_sack(stcb, cum_ack, a_rwnd, nonce_sum_flag,
3909 					    &abort_now);
3910 				} else {
3911 					if (netp && *netp)
3912 						sctp_handle_sack(m, *offset,
3913 						    sack, stcb, *netp, &abort_now, chk_length, a_rwnd);
3914 				}
3915 				if (abort_now) {
3916 					/* ABORT signal from sack processing */
3917 					*offset = length;
3918 					return (NULL);
3919 				}
3920 			}
3921 			break;
3922 		case SCTP_HEARTBEAT_REQUEST:
3923 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
3924 			if ((stcb) && netp && *netp) {
3925 				SCTP_STAT_INCR(sctps_recvheartbeat);
3926 				sctp_send_heartbeat_ack(stcb, m, *offset,
3927 				    chk_length, *netp);
3928 
3929 				/* He's alive so give him credit */
3930 				stcb->asoc.overall_error_count = 0;
3931 			}
3932 			break;
3933 		case SCTP_HEARTBEAT_ACK:
3934 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n");
3935 			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
3936 				/* Its not ours */
3937 				*offset = length;
3938 				if (locked_tcb) {
3939 					SCTP_TCB_UNLOCK(locked_tcb);
3940 				}
3941 				return (NULL);
3942 			}
3943 			/* He's alive so give him credit */
3944 			stcb->asoc.overall_error_count = 0;
3945 			SCTP_STAT_INCR(sctps_recvheartbeatack);
3946 			if (netp && *netp)
3947 				sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
3948 				    stcb, *netp);
3949 			break;
3950 		case SCTP_ABORT_ASSOCIATION:
3951 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
3952 			    stcb);
3953 			if ((stcb) && netp && *netp)
3954 				sctp_handle_abort((struct sctp_abort_chunk *)ch,
3955 				    stcb, *netp);
3956 			*offset = length;
3957 			return (NULL);
3958 			break;
3959 		case SCTP_SHUTDOWN:
3960 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
3961 			    stcb);
3962 			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
3963 				*offset = length;
3964 				if (locked_tcb) {
3965 					SCTP_TCB_UNLOCK(locked_tcb);
3966 				}
3967 				return (NULL);
3968 
3969 			}
3970 			if (netp && *netp) {
3971 				int abort_flag = 0;
3972 
3973 				sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
3974 				    stcb, *netp, &abort_flag);
3975 				if (abort_flag) {
3976 					*offset = length;
3977 					return (NULL);
3978 				}
3979 			}
3980 			break;
3981 		case SCTP_SHUTDOWN_ACK:
3982 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb);
3983 			if ((stcb) && (netp) && (*netp))
3984 				sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
3985 			*offset = length;
3986 			return (NULL);
3987 			break;
3988 
3989 		case SCTP_OPERATION_ERROR:
3990 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n");
3991 			if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) {
3992 
3993 				*offset = length;
3994 				return (NULL);
3995 			}
3996 			break;
3997 		case SCTP_COOKIE_ECHO:
3998 			SCTPDBG(SCTP_DEBUG_INPUT3,
3999 			    "SCTP_COOKIE-ECHO, stcb %p\n", stcb);
4000 			if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4001 				;
4002 			} else {
4003 				if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4004 					/* We are not interested anymore */
4005 					*offset = length;
4006 					return (NULL);
4007 				}
4008 			}
4009 			/*
4010 			 * First are we accepting? We do this again here
4011 			 * sincen it is possible that a previous endpoint
4012 			 * WAS listening responded to a INIT-ACK and then
4013 			 * closed. We opened and bound.. and are now no
4014 			 * longer listening.
4015 			 */
4016 			if (inp->sctp_socket->so_qlimit == 0) {
4017 				if ((stcb) && (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4018 					/*
4019 					 * special case, is this a retran'd
4020 					 * COOKIE-ECHO or a restarting assoc
4021 					 * that is a peeled off or
4022 					 * one-to-one style socket.
4023 					 */
4024 					goto process_cookie_anyway;
4025 				}
4026 				sctp_abort_association(inp, stcb, m, iphlen,
4027 				    sh, NULL, vrf_id);
4028 				*offset = length;
4029 				return (NULL);
4030 			} else if (inp->sctp_socket->so_qlimit) {
4031 				/* we are accepting so check limits like TCP */
4032 				if (inp->sctp_socket->so_qlen >
4033 				    inp->sctp_socket->so_qlimit) {
4034 					/* no space */
4035 					struct mbuf *oper;
4036 					struct sctp_paramhdr *phdr;
4037 
4038 					if (sctp_abort_if_one_2_one_hits_limit) {
4039 						oper = NULL;
4040 						oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
4041 						    0, M_DONTWAIT, 1, MT_DATA);
4042 						if (oper) {
4043 							SCTP_BUF_LEN(oper) =
4044 							    sizeof(struct sctp_paramhdr);
4045 							phdr = mtod(oper,
4046 							    struct sctp_paramhdr *);
4047 							phdr->param_type =
4048 							    htons(SCTP_CAUSE_OUT_OF_RESC);
4049 							phdr->param_length =
4050 							    htons(sizeof(struct sctp_paramhdr));
4051 						}
4052 						sctp_abort_association(inp, stcb, m,
4053 						    iphlen, sh, oper, vrf_id);
4054 					}
4055 					*offset = length;
4056 					return (NULL);
4057 				}
4058 			}
4059 	process_cookie_anyway:
4060 			{
4061 				struct mbuf *ret_buf;
4062 				struct sctp_inpcb *linp;
4063 
4064 				if (stcb) {
4065 					linp = NULL;
4066 				} else {
4067 					linp = inp;
4068 				}
4069 
4070 				if (linp) {
4071 					SCTP_ASOC_CREATE_LOCK(linp);
4072 				}
4073 				if (netp) {
4074 					ret_buf =
4075 					    sctp_handle_cookie_echo(m, iphlen,
4076 					    *offset, sh,
4077 					    (struct sctp_cookie_echo_chunk *)ch,
4078 					    &inp, &stcb, netp,
4079 					    auth_skipped,
4080 					    auth_offset,
4081 					    auth_len,
4082 					    &locked_tcb,
4083 					    vrf_id);
4084 				} else {
4085 					ret_buf = NULL;
4086 				}
4087 				if (linp) {
4088 					SCTP_ASOC_CREATE_UNLOCK(linp);
4089 				}
4090 				if (ret_buf == NULL) {
4091 					if (locked_tcb) {
4092 						SCTP_TCB_UNLOCK(locked_tcb);
4093 					}
4094 					SCTPDBG(SCTP_DEBUG_INPUT3,
4095 					    "GAK, null buffer\n");
4096 					auth_skipped = 0;
4097 					*offset = length;
4098 					return (NULL);
4099 				}
4100 				/* if AUTH skipped, see if it verified... */
4101 				if (auth_skipped) {
4102 					got_auth = 1;
4103 					auth_skipped = 0;
4104 				}
4105 				if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
4106 					/*
4107 					 * Restart the timer if we have
4108 					 * pending data
4109 					 */
4110 					struct sctp_tmit_chunk *chk;
4111 
4112 					chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
4113 					if (chk) {
4114 						sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4115 						    stcb->sctp_ep, stcb,
4116 						    chk->whoTo);
4117 					}
4118 				}
4119 			}
4120 			break;
4121 		case SCTP_COOKIE_ACK:
4122 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb);
4123 			if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
4124 				if (locked_tcb) {
4125 					SCTP_TCB_UNLOCK(locked_tcb);
4126 				}
4127 				return (NULL);
4128 			}
4129 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4130 				/* We are not interested anymore */
4131 				if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4132 					;
4133 				} else if (stcb) {
4134 					sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
4135 					*offset = length;
4136 					return (NULL);
4137 				}
4138 			}
4139 			/* He's alive so give him credit */
4140 			if ((stcb) && netp && *netp) {
4141 				stcb->asoc.overall_error_count = 0;
4142 				sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
4143 			}
4144 			break;
4145 		case SCTP_ECN_ECHO:
4146 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n");
4147 			/* He's alive so give him credit */
4148 			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
4149 				/* Its not ours */
4150 				if (locked_tcb) {
4151 					SCTP_TCB_UNLOCK(locked_tcb);
4152 				}
4153 				*offset = length;
4154 				return (NULL);
4155 			}
4156 			if (stcb) {
4157 				stcb->asoc.overall_error_count = 0;
4158 				sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
4159 				    stcb);
4160 			}
4161 			break;
4162 		case SCTP_ECN_CWR:
4163 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n");
4164 			/* He's alive so give him credit */
4165 			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
4166 				/* Its not ours */
4167 				if (locked_tcb) {
4168 					SCTP_TCB_UNLOCK(locked_tcb);
4169 				}
4170 				*offset = length;
4171 				return (NULL);
4172 			}
4173 			if (stcb) {
4174 				stcb->asoc.overall_error_count = 0;
4175 				sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb);
4176 			}
4177 			break;
4178 		case SCTP_SHUTDOWN_COMPLETE:
4179 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb);
4180 			/* must be first and only chunk */
4181 			if ((num_chunks > 1) ||
4182 			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4183 				*offset = length;
4184 				if (locked_tcb) {
4185 					SCTP_TCB_UNLOCK(locked_tcb);
4186 				}
4187 				return (NULL);
4188 			}
4189 			if ((stcb) && netp && *netp) {
4190 				sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
4191 				    stcb, *netp);
4192 			}
4193 			*offset = length;
4194 			return (NULL);
4195 			break;
4196 		case SCTP_ASCONF:
4197 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
4198 			/* He's alive so give him credit */
4199 			if (stcb) {
4200 				stcb->asoc.overall_error_count = 0;
4201 				sctp_handle_asconf(m, *offset,
4202 				    (struct sctp_asconf_chunk *)ch, stcb);
4203 			}
4204 			break;
4205 		case SCTP_ASCONF_ACK:
4206 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n");
4207 			if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
4208 				/* Its not ours */
4209 				if (locked_tcb) {
4210 					SCTP_TCB_UNLOCK(locked_tcb);
4211 				}
4212 				*offset = length;
4213 				return (NULL);
4214 			}
4215 			if ((stcb) && netp && *netp) {
4216 				/* He's alive so give him credit */
4217 				stcb->asoc.overall_error_count = 0;
4218 				sctp_handle_asconf_ack(m, *offset,
4219 				    (struct sctp_asconf_ack_chunk *)ch, stcb, *netp);
4220 			}
4221 			break;
4222 		case SCTP_FORWARD_CUM_TSN:
4223 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n");
4224 			if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
4225 				/* Its not ours */
4226 				if (locked_tcb) {
4227 					SCTP_TCB_UNLOCK(locked_tcb);
4228 				}
4229 				*offset = length;
4230 				return (NULL);
4231 			}
4232 			/* He's alive so give him credit */
4233 			if (stcb) {
4234 				int abort_flag = 0;
4235 
4236 				stcb->asoc.overall_error_count = 0;
4237 				*fwd_tsn_seen = 1;
4238 				if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4239 					/* We are not interested anymore */
4240 					sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
4241 					*offset = length;
4242 					return (NULL);
4243 				}
4244 				sctp_handle_forward_tsn(stcb,
4245 				    (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
4246 				if (abort_flag) {
4247 					*offset = length;
4248 					return (NULL);
4249 				} else {
4250 					stcb->asoc.overall_error_count = 0;
4251 				}
4252 
4253 			}
4254 			break;
4255 		case SCTP_STREAM_RESET:
4256 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
4257 			if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
4258 				/* Its not ours */
4259 				if (locked_tcb) {
4260 					SCTP_TCB_UNLOCK(locked_tcb);
4261 				}
4262 				*offset = length;
4263 				return (NULL);
4264 			}
4265 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4266 				/* We are not interested anymore */
4267 				sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
4268 				*offset = length;
4269 				return (NULL);
4270 			}
4271 			if (stcb->asoc.peer_supports_strreset == 0) {
4272 				/*
4273 				 * hmm, peer should have announced this, but
4274 				 * we will turn it on since he is sending us
4275 				 * a stream reset.
4276 				 */
4277 				stcb->asoc.peer_supports_strreset = 1;
4278 			}
4279 			if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) {
4280 				/* stop processing */
4281 				*offset = length;
4282 				return (NULL);
4283 			}
4284 			break;
4285 		case SCTP_PACKET_DROPPED:
4286 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
4287 			/* re-get it all please */
4288 			if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
4289 				/* Its not ours */
4290 				if (locked_tcb) {
4291 					SCTP_TCB_UNLOCK(locked_tcb);
4292 				}
4293 				*offset = length;
4294 				return (NULL);
4295 			}
4296 			if (ch && (stcb) && netp && (*netp)) {
4297 				sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
4298 				    stcb, *netp,
4299 				    min(chk_length, (sizeof(chunk_buf) - 4)));
4300 
4301 			}
4302 			break;
4303 
4304 		case SCTP_AUTHENTICATION:
4305 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
4306 			if (sctp_auth_disable)
4307 				goto unknown_chunk;
4308 
4309 			if (stcb == NULL) {
4310 				/* save the first AUTH for later processing */
4311 				if (auth_skipped == 0) {
4312 					auth_offset = *offset;
4313 					auth_len = chk_length;
4314 					auth_skipped = 1;
4315 				}
4316 				/* skip this chunk (temporarily) */
4317 				goto next_chunk;
4318 			}
4319 			if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
4320 			    (chk_length > (sizeof(struct sctp_auth_chunk) +
4321 			    SCTP_AUTH_DIGEST_LEN_MAX))) {
4322 				/* Its not ours */
4323 				if (locked_tcb) {
4324 					SCTP_TCB_UNLOCK(locked_tcb);
4325 				}
4326 				*offset = length;
4327 				return (NULL);
4328 			}
4329 			if (got_auth == 1) {
4330 				/* skip this chunk... it's already auth'd */
4331 				goto next_chunk;
4332 			}
4333 			got_auth = 1;
4334 			if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
4335 			    m, *offset)) {
4336 				/* auth HMAC failed so dump the packet */
4337 				*offset = length;
4338 				return (stcb);
4339 			} else {
4340 				/* remaining chunks are HMAC checked */
4341 				stcb->asoc.authenticated = 1;
4342 			}
4343 			break;
4344 
4345 		default:
4346 	unknown_chunk:
4347 			/* it's an unknown chunk! */
4348 			if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
4349 				struct mbuf *mm;
4350 				struct sctp_paramhdr *phd;
4351 
4352 				mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
4353 				    0, M_DONTWAIT, 1, MT_DATA);
4354 				if (mm) {
4355 					phd = mtod(mm, struct sctp_paramhdr *);
4356 					/*
4357 					 * We cheat and use param type since
4358 					 * we did not bother to define a
4359 					 * error cause struct. They are the
4360 					 * same basic format with different
4361 					 * names.
4362 					 */
4363 					phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK);
4364 					phd->param_length = htons(chk_length + sizeof(*phd));
4365 					SCTP_BUF_LEN(mm) = sizeof(*phd);
4366 					SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length),
4367 					    M_DONTWAIT);
4368 					if (SCTP_BUF_NEXT(mm)) {
4369 						sctp_queue_op_err(stcb, mm);
4370 					} else {
4371 						sctp_m_freem(mm);
4372 					}
4373 				}
4374 			}
4375 			if ((ch->chunk_type & 0x80) == 0) {
4376 				/* discard this packet */
4377 				*offset = length;
4378 				return (stcb);
4379 			}	/* else skip this bad chunk and continue... */
4380 			break;
4381 		}		/* switch (ch->chunk_type) */
4382 
4383 
4384 next_chunk:
4385 		/* get the next chunk */
4386 		*offset += SCTP_SIZE32(chk_length);
4387 		if (*offset >= length) {
4388 			/* no more data left in the mbuf chain */
4389 			break;
4390 		}
4391 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4392 		    sizeof(struct sctp_chunkhdr), chunk_buf);
4393 		if (ch == NULL) {
4394 			if (locked_tcb) {
4395 				SCTP_TCB_UNLOCK(locked_tcb);
4396 			}
4397 			*offset = length;
4398 			return (NULL);
4399 		}
4400 	}			/* while */
4401 	return (stcb);
4402 }
4403 
4404 
4405 /*
4406  * Process the ECN bits we have something set so we must look to see if it is
4407  * ECN(0) or ECN(1) or CE
4408  */
4409 static void
4410 sctp_process_ecn_marked_a(struct sctp_tcb *stcb, struct sctp_nets *net,
4411     uint8_t ecn_bits)
4412 {
4413 	if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) {
4414 		;
4415 	} else if ((ecn_bits & SCTP_ECT1_BIT) == SCTP_ECT1_BIT) {
4416 		/*
4417 		 * we only add to the nonce sum for ECT1, ECT0 does not
4418 		 * change the NS bit (that we have yet to find a way to send
4419 		 * it yet).
4420 		 */
4421 
4422 		/* ECN Nonce stuff */
4423 		stcb->asoc.receiver_nonce_sum++;
4424 		stcb->asoc.receiver_nonce_sum &= SCTP_SACK_NONCE_SUM;
4425 
4426 		/*
4427 		 * Drag up the last_echo point if cumack is larger since we
4428 		 * don't want the point falling way behind by more than
4429 		 * 2^^31 and then having it be incorrect.
4430 		 */
4431 		if (compare_with_wrap(stcb->asoc.cumulative_tsn,
4432 		    stcb->asoc.last_echo_tsn, MAX_TSN)) {
4433 			stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn;
4434 		}
4435 	} else if ((ecn_bits & SCTP_ECT0_BIT) == SCTP_ECT0_BIT) {
4436 		/*
4437 		 * Drag up the last_echo point if cumack is larger since we
4438 		 * don't want the point falling way behind by more than
4439 		 * 2^^31 and then having it be incorrect.
4440 		 */
4441 		if (compare_with_wrap(stcb->asoc.cumulative_tsn,
4442 		    stcb->asoc.last_echo_tsn, MAX_TSN)) {
4443 			stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn;
4444 		}
4445 	}
4446 }
4447 
4448 static void
4449 sctp_process_ecn_marked_b(struct sctp_tcb *stcb, struct sctp_nets *net,
4450     uint32_t high_tsn, uint8_t ecn_bits)
4451 {
4452 	if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) {
4453 		/*
4454 		 * we possibly must notify the sender that a congestion
4455 		 * window reduction is in order. We do this by adding a ECNE
4456 		 * chunk to the output chunk queue. The incoming CWR will
4457 		 * remove this chunk.
4458 		 */
4459 		if (compare_with_wrap(high_tsn, stcb->asoc.last_echo_tsn,
4460 		    MAX_TSN)) {
4461 			/* Yep, we need to add a ECNE */
4462 			sctp_send_ecn_echo(stcb, net, high_tsn);
4463 			stcb->asoc.last_echo_tsn = high_tsn;
4464 		}
4465 	}
4466 }
4467 
4468 #ifdef INVARIANTS
4469 static void
4470 sctp_validate_no_locks(struct sctp_inpcb *inp)
4471 {
4472 	struct sctp_tcb *stcb;
4473 
4474 	LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4475 		if (mtx_owned(&stcb->tcb_mtx)) {
4476 			panic("Own lock on stcb at return from input");
4477 		}
4478 	}
4479 }
4480 
4481 #endif
4482 
4483 /*
4484  * common input chunk processing (v4 and v6)
4485  */
4486 void
4487 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset,
4488     int length, struct sctphdr *sh, struct sctp_chunkhdr *ch,
4489     struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net,
4490     uint8_t ecn_bits, uint32_t vrf_id)
4491 {
4492 	/*
4493 	 * Control chunk processing
4494 	 */
4495 	uint32_t high_tsn;
4496 	int fwd_tsn_seen = 0, data_processed = 0;
4497 	struct mbuf *m = *mm;
4498 	int abort_flag = 0;
4499 	int un_sent;
4500 
4501 	SCTP_STAT_INCR(sctps_recvdatagrams);
4502 #ifdef SCTP_AUDITING_ENABLED
4503 	sctp_audit_log(0xE0, 1);
4504 	sctp_auditing(0, inp, stcb, net);
4505 #endif
4506 
4507 	SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d\n",
4508 	    m, iphlen, offset);
4509 
4510 	if (stcb) {
4511 		/* always clear this before beginning a packet */
4512 		stcb->asoc.authenticated = 0;
4513 		stcb->asoc.seen_a_sack_this_pkt = 0;
4514 	}
4515 	if (IS_SCTP_CONTROL(ch)) {
4516 		/* process the control portion of the SCTP packet */
4517 		/* sa_ignore NO_NULL_CHK */
4518 		stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch,
4519 		    inp, stcb, &net, &fwd_tsn_seen, vrf_id);
4520 		if (stcb) {
4521 			/*
4522 			 * This covers us if the cookie-echo was there and
4523 			 * it changes our INP.
4524 			 */
4525 			inp = stcb->sctp_ep;
4526 		}
4527 	} else {
4528 		/*
4529 		 * no control chunks, so pre-process DATA chunks (these
4530 		 * checks are taken care of by control processing)
4531 		 */
4532 
4533 		/*
4534 		 * if DATA only packet, and auth is required, then punt...
4535 		 * can't have authenticated without any AUTH (control)
4536 		 * chunks
4537 		 */
4538 		if ((stcb != NULL) && !sctp_auth_disable &&
4539 		    sctp_auth_is_required_chunk(SCTP_DATA,
4540 		    stcb->asoc.local_auth_chunks)) {
4541 			/* "silently" ignore */
4542 			SCTP_STAT_INCR(sctps_recvauthmissing);
4543 			SCTP_TCB_UNLOCK(stcb);
4544 			goto out_now;
4545 		}
4546 		if (stcb == NULL) {
4547 			/* out of the blue DATA chunk */
4548 			sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL,
4549 			    vrf_id);
4550 			goto out_now;
4551 		}
4552 		if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
4553 			/* v_tag mismatch! */
4554 			SCTP_STAT_INCR(sctps_badvtag);
4555 			SCTP_TCB_UNLOCK(stcb);
4556 			goto out_now;
4557 		}
4558 	}
4559 
4560 	if (stcb == NULL) {
4561 		/*
4562 		 * no valid TCB for this packet, or we found it's a bad
4563 		 * packet while processing control, or we're done with this
4564 		 * packet (done or skip rest of data), so we drop it...
4565 		 */
4566 		goto out_now;
4567 	}
4568 	/*
4569 	 * DATA chunk processing
4570 	 */
4571 	/* plow through the data chunks while length > offset */
4572 
4573 	/*
4574 	 * Rest should be DATA only.  Check authentication state if AUTH for
4575 	 * DATA is required.
4576 	 */
4577 	if ((length > offset) && (stcb != NULL) && !sctp_auth_disable &&
4578 	    sctp_auth_is_required_chunk(SCTP_DATA,
4579 	    stcb->asoc.local_auth_chunks) &&
4580 	    !stcb->asoc.authenticated) {
4581 		/* "silently" ignore */
4582 		SCTP_STAT_INCR(sctps_recvauthmissing);
4583 		SCTPDBG(SCTP_DEBUG_AUTH1,
4584 		    "Data chunk requires AUTH, skipped\n");
4585 		goto trigger_send;
4586 	}
4587 	if (length > offset) {
4588 		int retval;
4589 
4590 		/*
4591 		 * First check to make sure our state is correct. We would
4592 		 * not get here unless we really did have a tag, so we don't
4593 		 * abort if this happens, just dump the chunk silently.
4594 		 */
4595 		switch (SCTP_GET_STATE(&stcb->asoc)) {
4596 		case SCTP_STATE_COOKIE_ECHOED:
4597 			/*
4598 			 * we consider data with valid tags in this state
4599 			 * shows us the cookie-ack was lost. Imply it was
4600 			 * there.
4601 			 */
4602 			stcb->asoc.overall_error_count = 0;
4603 			sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
4604 			break;
4605 		case SCTP_STATE_COOKIE_WAIT:
4606 			/*
4607 			 * We consider OOTB any data sent during asoc setup.
4608 			 */
4609 			sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL,
4610 			    vrf_id);
4611 			SCTP_TCB_UNLOCK(stcb);
4612 			goto out_now;
4613 			/* sa_ignore NOTREACHED */
4614 			break;
4615 		case SCTP_STATE_EMPTY:	/* should not happen */
4616 		case SCTP_STATE_INUSE:	/* should not happen */
4617 		case SCTP_STATE_SHUTDOWN_RECEIVED:	/* This is a peer error */
4618 		case SCTP_STATE_SHUTDOWN_ACK_SENT:
4619 		default:
4620 			SCTP_TCB_UNLOCK(stcb);
4621 			goto out_now;
4622 			/* sa_ignore NOTREACHED */
4623 			break;
4624 		case SCTP_STATE_OPEN:
4625 		case SCTP_STATE_SHUTDOWN_SENT:
4626 			break;
4627 		}
4628 		/* take care of ECN, part 1. */
4629 		if (stcb->asoc.ecn_allowed &&
4630 		    (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) {
4631 			sctp_process_ecn_marked_a(stcb, net, ecn_bits);
4632 		}
4633 		/* plow through the data chunks while length > offset */
4634 		retval = sctp_process_data(mm, iphlen, &offset, length, sh,
4635 		    inp, stcb, net, &high_tsn);
4636 		if (retval == 2) {
4637 			/*
4638 			 * The association aborted, NO UNLOCK needed since
4639 			 * the association is destroyed.
4640 			 */
4641 			goto out_now;
4642 		}
4643 		data_processed = 1;
4644 		if (retval == 0) {
4645 			/* take care of ecn part 2. */
4646 			if (stcb->asoc.ecn_allowed &&
4647 			    (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) {
4648 				sctp_process_ecn_marked_b(stcb, net, high_tsn,
4649 				    ecn_bits);
4650 			}
4651 		}
4652 		/*
4653 		 * Anything important needs to have been m_copy'ed in
4654 		 * process_data
4655 		 */
4656 	}
4657 	if ((data_processed == 0) && (fwd_tsn_seen)) {
4658 		int was_a_gap = 0;
4659 
4660 		if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
4661 		    stcb->asoc.cumulative_tsn, MAX_TSN)) {
4662 			/* there was a gap before this data was processed */
4663 			was_a_gap = 1;
4664 		}
4665 		sctp_sack_check(stcb, 1, was_a_gap, &abort_flag);
4666 		if (abort_flag) {
4667 			/* Again, we aborted so NO UNLOCK needed */
4668 			goto out_now;
4669 		}
4670 	}
4671 	/* trigger send of any chunks in queue... */
4672 trigger_send:
4673 #ifdef SCTP_AUDITING_ENABLED
4674 	sctp_audit_log(0xE0, 2);
4675 	sctp_auditing(1, inp, stcb, net);
4676 #endif
4677 	SCTPDBG(SCTP_DEBUG_INPUT1,
4678 	    "Check for chunk output prw:%d tqe:%d tf=%d\n",
4679 	    stcb->asoc.peers_rwnd,
4680 	    TAILQ_EMPTY(&stcb->asoc.control_send_queue),
4681 	    stcb->asoc.total_flight);
4682 	un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
4683 
4684 	if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue) ||
4685 	    ((un_sent) &&
4686 	    (stcb->asoc.peers_rwnd > 0 ||
4687 	    (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
4688 		SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
4689 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC);
4690 		SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
4691 	}
4692 #ifdef SCTP_AUDITING_ENABLED
4693 	sctp_audit_log(0xE0, 3);
4694 	sctp_auditing(2, inp, stcb, net);
4695 #endif
4696 	SCTP_TCB_UNLOCK(stcb);
4697 out_now:
4698 #ifdef INVARIANTS
4699 	sctp_validate_no_locks(inp);
4700 #endif
4701 	return;
4702 }
4703 
4704 
4705 
4706 void
4707 sctp_input(i_pak, off)
4708 	struct mbuf *i_pak;
4709 	int off;
4710 
4711 {
4712 #ifdef SCTP_MBUF_LOGGING
4713 	struct mbuf *mat;
4714 
4715 #endif
4716 	struct mbuf *m;
4717 	int iphlen;
4718 	uint32_t vrf_id = 0;
4719 	uint8_t ecn_bits;
4720 	struct ip *ip;
4721 	struct sctphdr *sh;
4722 	struct sctp_inpcb *inp = NULL;
4723 
4724 	uint32_t check, calc_check;
4725 	struct sctp_nets *net;
4726 	struct sctp_tcb *stcb = NULL;
4727 	struct sctp_chunkhdr *ch;
4728 	int refcount_up = 0;
4729 	int length, mlen, offset;
4730 
4731 
4732 	if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
4733 		SCTP_RELEASE_PKT(i_pak);
4734 		return;
4735 	}
4736 	mlen = SCTP_HEADER_LEN(i_pak);
4737 	iphlen = off;
4738 	m = SCTP_HEADER_TO_CHAIN(i_pak);
4739 
4740 	net = NULL;
4741 	SCTP_STAT_INCR(sctps_recvpackets);
4742 	SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
4743 
4744 
4745 #ifdef SCTP_MBUF_LOGGING
4746 	/* Log in any input mbufs */
4747 	if (sctp_logging_level & SCTP_MBUF_LOGGING_ENABLE) {
4748 		mat = m;
4749 		while (mat) {
4750 			if (SCTP_BUF_IS_EXTENDED(mat)) {
4751 				sctp_log_mb(mat, SCTP_MBUF_INPUT);
4752 			}
4753 			mat = SCTP_BUF_NEXT(mat);
4754 		}
4755 	}
4756 #endif
4757 #ifdef  SCTP_PACKET_LOGGING
4758 	if (sctp_logging_level & SCTP_LAST_PACKET_TRACING)
4759 		sctp_packet_log(m, mlen);
4760 #endif
4761 	/*
4762 	 * Must take out the iphlen, since mlen expects this (only effect lb
4763 	 * case)
4764 	 */
4765 	mlen -= iphlen;
4766 
4767 	/*
4768 	 * Get IP, SCTP, and first chunk header together in first mbuf.
4769 	 */
4770 	ip = mtod(m, struct ip *);
4771 	offset = iphlen + sizeof(*sh) + sizeof(*ch);
4772 	if (SCTP_BUF_LEN(m) < offset) {
4773 		if ((m = m_pullup(m, offset)) == 0) {
4774 			SCTP_STAT_INCR(sctps_hdrops);
4775 			return;
4776 		}
4777 		ip = mtod(m, struct ip *);
4778 	}
4779 	sh = (struct sctphdr *)((caddr_t)ip + iphlen);
4780 	ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh));
4781 	SCTPDBG(SCTP_DEBUG_INPUT1,
4782 	    "sctp_input() length:%d iphlen:%d\n", mlen, iphlen);
4783 
4784 	/* SCTP does not allow broadcasts or multicasts */
4785 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
4786 		goto bad;
4787 	}
4788 	if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) {
4789 		/*
4790 		 * We only look at broadcast if its a front state, All
4791 		 * others we will not have a tcb for anyway.
4792 		 */
4793 		goto bad;
4794 	}
4795 	/* validate SCTP checksum */
4796 	if ((sctp_no_csum_on_loopback == 0) || !SCTP_IS_IT_LOOPBACK(m)) {
4797 		/*
4798 		 * we do NOT validate things from the loopback if the sysctl
4799 		 * is set to 1.
4800 		 */
4801 		check = sh->checksum;	/* save incoming checksum */
4802 		if ((check == 0) && (sctp_no_csum_on_loopback)) {
4803 			/*
4804 			 * special hook for where we got a local address
4805 			 * somehow routed across a non IFT_LOOP type
4806 			 * interface
4807 			 */
4808 			if (ip->ip_src.s_addr == ip->ip_dst.s_addr)
4809 				goto sctp_skip_csum_4;
4810 		}
4811 		sh->checksum = 0;	/* prepare for calc */
4812 		calc_check = sctp_calculate_sum(m, &mlen, iphlen);
4813 		if (calc_check != check) {
4814 			SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x  m:%p mlen:%d iphlen:%d\n",
4815 			    calc_check, check, m, mlen, iphlen);
4816 
4817 			stcb = sctp_findassociation_addr(m, iphlen,
4818 			    offset - sizeof(*ch),
4819 			    sh, ch, &inp, &net,
4820 			    vrf_id);
4821 			if ((inp) && (stcb)) {
4822 				sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
4823 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR);
4824 			} else if ((inp != NULL) && (stcb == NULL)) {
4825 				refcount_up = 1;
4826 			}
4827 			SCTP_STAT_INCR(sctps_badsum);
4828 			SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
4829 			goto bad;
4830 		}
4831 		sh->checksum = calc_check;
4832 	}
4833 sctp_skip_csum_4:
4834 	/* destination port of 0 is illegal, based on RFC2960. */
4835 	if (sh->dest_port == 0) {
4836 		SCTP_STAT_INCR(sctps_hdrops);
4837 		goto bad;
4838 	}
4839 	/* validate mbuf chain length with IP payload length */
4840 	if (mlen < (ip->ip_len - iphlen)) {
4841 		SCTP_STAT_INCR(sctps_hdrops);
4842 		goto bad;
4843 	}
4844 	/*
4845 	 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants
4846 	 * IP/SCTP/first chunk header...
4847 	 */
4848 	stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
4849 	    sh, ch, &inp, &net, vrf_id);
4850 	/* inp's ref-count increased && stcb locked */
4851 	if (inp == NULL) {
4852 		struct sctp_init_chunk *init_chk, chunk_buf;
4853 
4854 		SCTP_STAT_INCR(sctps_noport);
4855 #ifdef ICMP_BANDLIM
4856 		/*
4857 		 * we use the bandwidth limiting to protect against sending
4858 		 * too many ABORTS all at once. In this case these count the
4859 		 * same as an ICMP message.
4860 		 */
4861 		if (badport_bandlim(0) < 0)
4862 			goto bad;
4863 #endif				/* ICMP_BANDLIM */
4864 		SCTPDBG(SCTP_DEBUG_INPUT1,
4865 		    "Sending a ABORT from packet entry!\n");
4866 		if (ch->chunk_type == SCTP_INITIATION) {
4867 			/*
4868 			 * we do a trick here to get the INIT tag, dig in
4869 			 * and get the tag from the INIT and put it in the
4870 			 * common header.
4871 			 */
4872 			init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4873 			    iphlen + sizeof(*sh), sizeof(*init_chk),
4874 			    (uint8_t *) & chunk_buf);
4875 			if (init_chk != NULL)
4876 				sh->v_tag = init_chk->init.initiate_tag;
4877 		}
4878 		if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4879 			sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id);
4880 			goto bad;
4881 		}
4882 		if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
4883 			goto bad;
4884 		}
4885 		if (ch->chunk_type != SCTP_ABORT_ASSOCIATION)
4886 			sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id);
4887 		goto bad;
4888 	} else if (stcb == NULL) {
4889 		refcount_up = 1;
4890 	}
4891 #ifdef IPSEC
4892 	/*
4893 	 * I very much doubt any of the IPSEC stuff will work but I have no
4894 	 * idea, so I will leave it in place.
4895 	 */
4896 	if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) {
4897 		ipsec4stat.in_polvio++;
4898 		SCTP_STAT_INCR(sctps_hdrops);
4899 		goto bad;
4900 	}
4901 #endif				/* IPSEC */
4902 
4903 	/*
4904 	 * common chunk processing
4905 	 */
4906 	length = ip->ip_len + iphlen;
4907 	offset -= sizeof(struct sctp_chunkhdr);
4908 
4909 	ecn_bits = ip->ip_tos;
4910 
4911 	/* sa_ignore NO_NULL_CHK */
4912 	sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
4913 	    inp, stcb, net, ecn_bits, vrf_id);
4914 	/* inp's ref-count reduced && stcb unlocked */
4915 	if (m) {
4916 		sctp_m_freem(m);
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 	return;
4925 bad:
4926 	if (stcb) {
4927 		SCTP_TCB_UNLOCK(stcb);
4928 	}
4929 	if ((inp) && (refcount_up)) {
4930 		/* reduce ref-count */
4931 		SCTP_INP_WLOCK(inp);
4932 		SCTP_INP_DECR_REF(inp);
4933 		SCTP_INP_WUNLOCK(inp);
4934 	}
4935 	if (m) {
4936 		sctp_m_freem(m);
4937 	}
4938 	return;
4939 }
4940