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