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