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