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