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