sctp_input.c revision 188067
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 188067 2009-02-03 11:04:03Z 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->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_TSN_REQUEST) {
3444				/**
3445				 * a) Adopt the new in tsn.
3446				 * b) reset the map
3447				 * c) Adopt the new out-tsn
3448				 */
3449				struct sctp_stream_reset_response_tsn *resp;
3450				struct sctp_forward_tsn_chunk fwdtsn;
3451				int abort_flag = 0;
3452
3453				if (respin == NULL) {
3454					/* huh ? */
3455					return (0);
3456				}
3457				if (action == SCTP_STREAM_RESET_PERFORMED) {
3458					resp = (struct sctp_stream_reset_response_tsn *)respin;
3459					asoc->stream_reset_outstanding--;
3460					fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3461					fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3462					fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3463					sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3464					if (abort_flag) {
3465						return (1);
3466					}
3467					stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3468					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3469						sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3470					}
3471					stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3472					stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3473					memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3474
3475					/*
3476					 * EY 05/13/08 - nr_sack: to keep
3477					 * nr_mapping array be consistent
3478					 * with mapping_array
3479					 */
3480					if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack) {
3481						stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3482						stcb->asoc.nr_mapping_array_base_tsn = stcb->asoc.mapping_array_base_tsn;
3483						memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.nr_mapping_array_size);
3484					}
3485					stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3486					stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3487
3488					sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3489					sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3490
3491				}
3492			}
3493			/* get rid of the request and get the request flags */
3494			if (asoc->stream_reset_outstanding == 0) {
3495				sctp_clean_up_stream_reset(stcb);
3496			}
3497		}
3498	}
3499	return (0);
3500}
3501
3502static void
3503sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3504    struct sctp_tmit_chunk *chk,
3505    struct sctp_stream_reset_in_request *req, int trunc)
3506{
3507	uint32_t seq;
3508	int len, i;
3509	int number_entries;
3510	uint16_t temp;
3511
3512	/*
3513	 * peer wants me to send a str-reset to him for my outgoing seq's if
3514	 * seq_in is right.
3515	 */
3516	struct sctp_association *asoc = &stcb->asoc;
3517
3518	seq = ntohl(req->request_seq);
3519	if (asoc->str_reset_seq_in == seq) {
3520		if (trunc) {
3521			/* Can't do it, since they exceeded our buffer size  */
3522			asoc->last_reset_action[1] = asoc->last_reset_action[0];
3523			asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED;
3524			sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3525		} else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3526			len = ntohs(req->ph.param_length);
3527			number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3528			for (i = 0; i < number_entries; i++) {
3529				temp = ntohs(req->list_of_streams[i]);
3530				req->list_of_streams[i] = temp;
3531			}
3532			/* move the reset action back one */
3533			asoc->last_reset_action[1] = asoc->last_reset_action[0];
3534			asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3535			sctp_add_stream_reset_out(chk, number_entries, req->list_of_streams,
3536			    asoc->str_reset_seq_out,
3537			    seq, (asoc->sending_seq - 1));
3538			asoc->stream_reset_out_is_outstanding = 1;
3539			asoc->str_reset = chk;
3540			sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
3541			stcb->asoc.stream_reset_outstanding++;
3542		} else {
3543			/* Can't do it, since we have sent one out */
3544			asoc->last_reset_action[1] = asoc->last_reset_action[0];
3545			asoc->last_reset_action[0] = SCTP_STREAM_RESET_TRY_LATER;
3546			sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3547		}
3548		asoc->str_reset_seq_in++;
3549	} else if (asoc->str_reset_seq_in - 1 == seq) {
3550		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3551	} else if (asoc->str_reset_seq_in - 2 == seq) {
3552		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3553	} else {
3554		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3555	}
3556}
3557
3558static int
3559sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3560    struct sctp_tmit_chunk *chk,
3561    struct sctp_stream_reset_tsn_request *req)
3562{
3563	/* reset all in and out and update the tsn */
3564	/*
3565	 * A) reset my str-seq's on in and out. B) Select a receive next,
3566	 * and set cum-ack to it. Also process this selected number as a
3567	 * fwd-tsn as well. C) set in the response my next sending seq.
3568	 */
3569	struct sctp_forward_tsn_chunk fwdtsn;
3570	struct sctp_association *asoc = &stcb->asoc;
3571	int abort_flag = 0;
3572	uint32_t seq;
3573
3574	seq = ntohl(req->request_seq);
3575	if (asoc->str_reset_seq_in == seq) {
3576		fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3577		fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3578		fwdtsn.ch.chunk_flags = 0;
3579		fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3580		sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3581		if (abort_flag) {
3582			return (1);
3583		}
3584		stcb->asoc.highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3585		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3586			sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3587		}
3588		stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3589		stcb->asoc.mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1;
3590		memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3591		/*
3592		 * EY 05/13/08 -nr_sack: to keep nr_mapping array consistent
3593		 * with mapping array
3594		 */
3595		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack) {
3596			stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3597			stcb->asoc.nr_mapping_array_base_tsn = stcb->asoc.highest_tsn_inside_map + 1;
3598			memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.nr_mapping_array_size);
3599		}
3600		atomic_add_int(&stcb->asoc.sending_seq, 1);
3601		/* save off historical data for retrans */
3602		stcb->asoc.last_sending_seq[1] = stcb->asoc.last_sending_seq[0];
3603		stcb->asoc.last_sending_seq[0] = stcb->asoc.sending_seq;
3604		stcb->asoc.last_base_tsnsent[1] = stcb->asoc.last_base_tsnsent[0];
3605		stcb->asoc.last_base_tsnsent[0] = stcb->asoc.mapping_array_base_tsn;
3606
3607		sctp_add_stream_reset_result_tsn(chk,
3608		    ntohl(req->request_seq),
3609		    SCTP_STREAM_RESET_PERFORMED,
3610		    stcb->asoc.sending_seq,
3611		    stcb->asoc.mapping_array_base_tsn);
3612		sctp_reset_out_streams(stcb, 0, (uint16_t *) NULL);
3613		sctp_reset_in_stream(stcb, 0, (uint16_t *) NULL);
3614		stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
3615		stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3616
3617		asoc->str_reset_seq_in++;
3618	} else if (asoc->str_reset_seq_in - 1 == seq) {
3619		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3620		    stcb->asoc.last_sending_seq[0],
3621		    stcb->asoc.last_base_tsnsent[0]
3622		    );
3623	} else if (asoc->str_reset_seq_in - 2 == seq) {
3624		sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3625		    stcb->asoc.last_sending_seq[1],
3626		    stcb->asoc.last_base_tsnsent[1]
3627		    );
3628	} else {
3629		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3630	}
3631	return (0);
3632}
3633
3634static void
3635sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3636    struct sctp_tmit_chunk *chk,
3637    struct sctp_stream_reset_out_request *req, int trunc)
3638{
3639	uint32_t seq, tsn;
3640	int number_entries, len;
3641	struct sctp_association *asoc = &stcb->asoc;
3642
3643	seq = ntohl(req->request_seq);
3644
3645	/* now if its not a duplicate we process it */
3646	if (asoc->str_reset_seq_in == seq) {
3647		len = ntohs(req->ph.param_length);
3648		number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3649		/*
3650		 * the sender is resetting, handle the list issue.. we must
3651		 * a) verify if we can do the reset, if so no problem b) If
3652		 * we can't do the reset we must copy the request. c) queue
3653		 * it, and setup the data in processor to trigger it off
3654		 * when needed and dequeue all the queued data.
3655		 */
3656		tsn = ntohl(req->send_reset_at_tsn);
3657
3658		/* move the reset action back one */
3659		asoc->last_reset_action[1] = asoc->last_reset_action[0];
3660		if (trunc) {
3661			sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED);
3662			asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED;
3663		} else if ((tsn == asoc->cumulative_tsn) ||
3664		    (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN))) {
3665			/* we can do it now */
3666			sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3667			sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED);
3668			asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3669		} else {
3670			/*
3671			 * we must queue it up and thus wait for the TSN's
3672			 * to arrive that are at or before tsn
3673			 */
3674			struct sctp_stream_reset_list *liste;
3675			int siz;
3676
3677			siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
3678			SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
3679			    siz, SCTP_M_STRESET);
3680			if (liste == NULL) {
3681				/* gak out of memory */
3682				sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_DENIED);
3683				asoc->last_reset_action[0] = SCTP_STREAM_RESET_DENIED;
3684				return;
3685			}
3686			liste->tsn = tsn;
3687			liste->number_entries = number_entries;
3688			memcpy(&liste->req, req,
3689			    (sizeof(struct sctp_stream_reset_out_request) + (number_entries * sizeof(uint16_t))));
3690			TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
3691			sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_PERFORMED);
3692			asoc->last_reset_action[0] = SCTP_STREAM_RESET_PERFORMED;
3693		}
3694		asoc->str_reset_seq_in++;
3695	} else if ((asoc->str_reset_seq_in - 1) == seq) {
3696		/*
3697		 * one seq back, just echo back last action since my
3698		 * response was lost.
3699		 */
3700		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3701	} else if ((asoc->str_reset_seq_in - 2) == seq) {
3702		/*
3703		 * two seq back, just echo back last action since my
3704		 * response was lost.
3705		 */
3706		sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3707	} else {
3708		sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_BAD_SEQNO);
3709	}
3710}
3711
3712#ifdef __GNUC__
3713__attribute__((noinline))
3714#endif
3715	static int
3716	    sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
3717        struct sctp_stream_reset_out_req *sr_req)
3718{
3719	int chk_length, param_len, ptype;
3720	struct sctp_paramhdr pstore;
3721	uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
3722
3723	uint32_t seq;
3724	int num_req = 0;
3725	int trunc = 0;
3726	struct sctp_tmit_chunk *chk;
3727	struct sctp_chunkhdr *ch;
3728	struct sctp_paramhdr *ph;
3729	int ret_code = 0;
3730	int num_param = 0;
3731
3732	/* now it may be a reset or a reset-response */
3733	chk_length = ntohs(sr_req->ch.chunk_length);
3734
3735	/* setup for adding the response */
3736	sctp_alloc_a_chunk(stcb, chk);
3737	if (chk == NULL) {
3738		return (ret_code);
3739	}
3740	chk->rec.chunk_id.id = SCTP_STREAM_RESET;
3741	chk->rec.chunk_id.can_take_data = 0;
3742	chk->asoc = &stcb->asoc;
3743	chk->no_fr_allowed = 0;
3744	chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
3745	chk->book_size_scale = 0;
3746	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
3747	if (chk->data == NULL) {
3748strres_nochunk:
3749		if (chk->data) {
3750			sctp_m_freem(chk->data);
3751			chk->data = NULL;
3752		}
3753		sctp_free_a_chunk(stcb, chk);
3754		return (ret_code);
3755	}
3756	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
3757
3758	/* setup chunk parameters */
3759	chk->sent = SCTP_DATAGRAM_UNSENT;
3760	chk->snd_count = 0;
3761	chk->whoTo = stcb->asoc.primary_destination;
3762	atomic_add_int(&chk->whoTo->ref_count, 1);
3763
3764	ch = mtod(chk->data, struct sctp_chunkhdr *);
3765	ch->chunk_type = SCTP_STREAM_RESET;
3766	ch->chunk_flags = 0;
3767	ch->chunk_length = htons(chk->send_size);
3768	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
3769	offset += sizeof(struct sctp_chunkhdr);
3770	while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_tsn_request)) {
3771		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *) & pstore);
3772		if (ph == NULL)
3773			break;
3774		param_len = ntohs(ph->param_length);
3775		if (param_len < (int)sizeof(struct sctp_stream_reset_tsn_request)) {
3776			/* bad param */
3777			break;
3778		}
3779		ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, (int)sizeof(cstore)),
3780		    (uint8_t *) & cstore);
3781		ptype = ntohs(ph->param_type);
3782		num_param++;
3783		if (param_len > (int)sizeof(cstore)) {
3784			trunc = 1;
3785		} else {
3786			trunc = 0;
3787		}
3788
3789		if (num_param > SCTP_MAX_RESET_PARAMS) {
3790			/* hit the max of parameters already sorry.. */
3791			break;
3792		}
3793		if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
3794			struct sctp_stream_reset_out_request *req_out;
3795
3796			req_out = (struct sctp_stream_reset_out_request *)ph;
3797			num_req++;
3798			if (stcb->asoc.stream_reset_outstanding) {
3799				seq = ntohl(req_out->response_seq);
3800				if (seq == stcb->asoc.str_reset_seq_out) {
3801					/* implicit ack */
3802					(void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_PERFORMED, NULL);
3803				}
3804			}
3805			sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
3806		} else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
3807			struct sctp_stream_reset_in_request *req_in;
3808
3809			num_req++;
3810
3811			req_in = (struct sctp_stream_reset_in_request *)ph;
3812
3813			sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
3814		} else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
3815			struct sctp_stream_reset_tsn_request *req_tsn;
3816
3817			num_req++;
3818			req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
3819
3820			if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
3821				ret_code = 1;
3822				goto strres_nochunk;
3823			}
3824			/* no more */
3825			break;
3826		} else if (ptype == SCTP_STR_RESET_RESPONSE) {
3827			struct sctp_stream_reset_response *resp;
3828			uint32_t result;
3829
3830			resp = (struct sctp_stream_reset_response *)ph;
3831			seq = ntohl(resp->response_seq);
3832			result = ntohl(resp->result);
3833			if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
3834				ret_code = 1;
3835				goto strres_nochunk;
3836			}
3837		} else {
3838			break;
3839		}
3840		offset += SCTP_SIZE32(param_len);
3841		chk_length -= SCTP_SIZE32(param_len);
3842	}
3843	if (num_req == 0) {
3844		/* we have no response free the stuff */
3845		goto strres_nochunk;
3846	}
3847	/* ok we have a chunk to link in */
3848	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
3849	    chk,
3850	    sctp_next);
3851	stcb->asoc.ctrl_queue_cnt++;
3852	return (ret_code);
3853}
3854
3855/*
3856 * Handle a router or endpoints report of a packet loss, there are two ways
3857 * to handle this, either we get the whole packet and must disect it
3858 * ourselves (possibly with truncation and or corruption) or it is a summary
3859 * from a middle box that did the disectting for us.
3860 */
3861static void
3862sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
3863    struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
3864{
3865	uint32_t bottle_bw, on_queue;
3866	uint16_t trunc_len;
3867	unsigned int chlen;
3868	unsigned int at;
3869	struct sctp_chunk_desc desc;
3870	struct sctp_chunkhdr *ch;
3871
3872	chlen = ntohs(cp->ch.chunk_length);
3873	chlen -= sizeof(struct sctp_pktdrop_chunk);
3874	/* XXX possible chlen underflow */
3875	if (chlen == 0) {
3876		ch = NULL;
3877		if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
3878			SCTP_STAT_INCR(sctps_pdrpbwrpt);
3879	} else {
3880		ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
3881		chlen -= sizeof(struct sctphdr);
3882		/* XXX possible chlen underflow */
3883		memset(&desc, 0, sizeof(desc));
3884	}
3885	trunc_len = (uint16_t) ntohs(cp->trunc_len);
3886	if (trunc_len > limit) {
3887		trunc_len = limit;
3888	}
3889	/* now the chunks themselves */
3890	while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
3891		desc.chunk_type = ch->chunk_type;
3892		/* get amount we need to move */
3893		at = ntohs(ch->chunk_length);
3894		if (at < sizeof(struct sctp_chunkhdr)) {
3895			/* corrupt chunk, maybe at the end? */
3896			SCTP_STAT_INCR(sctps_pdrpcrupt);
3897			break;
3898		}
3899		if (trunc_len == 0) {
3900			/* we are supposed to have all of it */
3901			if (at > chlen) {
3902				/* corrupt skip it */
3903				SCTP_STAT_INCR(sctps_pdrpcrupt);
3904				break;
3905			}
3906		} else {
3907			/* is there enough of it left ? */
3908			if (desc.chunk_type == SCTP_DATA) {
3909				if (chlen < (sizeof(struct sctp_data_chunk) +
3910				    sizeof(desc.data_bytes))) {
3911					break;
3912				}
3913			} else {
3914				if (chlen < sizeof(struct sctp_chunkhdr)) {
3915					break;
3916				}
3917			}
3918		}
3919		if (desc.chunk_type == SCTP_DATA) {
3920			/* can we get out the tsn? */
3921			if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
3922				SCTP_STAT_INCR(sctps_pdrpmbda);
3923
3924			if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
3925				/* yep */
3926				struct sctp_data_chunk *dcp;
3927				uint8_t *ddp;
3928				unsigned int iii;
3929
3930				dcp = (struct sctp_data_chunk *)ch;
3931				ddp = (uint8_t *) (dcp + 1);
3932				for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
3933					desc.data_bytes[iii] = ddp[iii];
3934				}
3935				desc.tsn_ifany = dcp->dp.tsn;
3936			} else {
3937				/* nope we are done. */
3938				SCTP_STAT_INCR(sctps_pdrpnedat);
3939				break;
3940			}
3941		} else {
3942			if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
3943				SCTP_STAT_INCR(sctps_pdrpmbct);
3944		}
3945
3946		if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
3947			SCTP_STAT_INCR(sctps_pdrppdbrk);
3948			break;
3949		}
3950		if (SCTP_SIZE32(at) > chlen) {
3951			break;
3952		}
3953		chlen -= SCTP_SIZE32(at);
3954		if (chlen < sizeof(struct sctp_chunkhdr)) {
3955			/* done, none left */
3956			break;
3957		}
3958		ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
3959	}
3960	/* Now update any rwnd --- possibly */
3961	if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
3962		/* From a peer, we get a rwnd report */
3963		uint32_t a_rwnd;
3964
3965		SCTP_STAT_INCR(sctps_pdrpfehos);
3966
3967		bottle_bw = ntohl(cp->bottle_bw);
3968		on_queue = ntohl(cp->current_onq);
3969		if (bottle_bw && on_queue) {
3970			/* a rwnd report is in here */
3971			if (bottle_bw > on_queue)
3972				a_rwnd = bottle_bw - on_queue;
3973			else
3974				a_rwnd = 0;
3975
3976			if (a_rwnd == 0)
3977				stcb->asoc.peers_rwnd = 0;
3978			else {
3979				if (a_rwnd > stcb->asoc.total_flight) {
3980					stcb->asoc.peers_rwnd =
3981					    a_rwnd - stcb->asoc.total_flight;
3982				} else {
3983					stcb->asoc.peers_rwnd = 0;
3984				}
3985				if (stcb->asoc.peers_rwnd <
3986				    stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
3987					/* SWS sender side engages */
3988					stcb->asoc.peers_rwnd = 0;
3989				}
3990			}
3991		}
3992	} else {
3993		SCTP_STAT_INCR(sctps_pdrpfmbox);
3994	}
3995
3996	/* now middle boxes in sat networks get a cwnd bump */
3997	if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
3998	    (stcb->asoc.sat_t3_loss_recovery == 0) &&
3999	    (stcb->asoc.sat_network)) {
4000		/*
4001		 * This is debateable but for sat networks it makes sense
4002		 * Note if a T3 timer has went off, we will prohibit any
4003		 * changes to cwnd until we exit the t3 loss recovery.
4004		 */
4005		stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4006		    net, cp, &bottle_bw, &on_queue);
4007	}
4008}
4009
4010/*
4011 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4012 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4013 * offset: offset into the mbuf chain to first chunkhdr - length: is the
4014 * length of the complete packet outputs: - length: modified to remaining
4015 * length after control processing - netp: modified to new sctp_nets after
4016 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4017 * bad packet,...) otherwise return the tcb for this packet
4018 */
4019#ifdef __GNUC__
4020__attribute__((noinline))
4021#endif
4022	static struct sctp_tcb *
4023	         sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4024             struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4025             struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4026             uint32_t vrf_id, uint16_t port)
4027{
4028	struct sctp_association *asoc;
4029	uint32_t vtag_in;
4030	int num_chunks = 0;	/* number of control chunks processed */
4031	uint32_t chk_length;
4032	int ret;
4033	int abort_no_unlock = 0;
4034
4035	/*
4036	 * How big should this be, and should it be alloc'd? Lets try the
4037	 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4038	 * until we get into jumbo grams and such..
4039	 */
4040	uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4041	struct sctp_tcb *locked_tcb = stcb;
4042	int got_auth = 0;
4043	uint32_t auth_offset = 0, auth_len = 0;
4044	int auth_skipped = 0;
4045	int asconf_cnt = 0;
4046
4047#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4048	struct socket *so;
4049
4050#endif
4051
4052	SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4053	    iphlen, *offset, length, stcb);
4054
4055	/* validate chunk header length... */
4056	if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4057		SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4058		    ntohs(ch->chunk_length));
4059		if (locked_tcb) {
4060			SCTP_TCB_UNLOCK(locked_tcb);
4061		}
4062		return (NULL);
4063	}
4064	/*
4065	 * validate the verification tag
4066	 */
4067	vtag_in = ntohl(sh->v_tag);
4068
4069	if (locked_tcb) {
4070		SCTP_TCB_LOCK_ASSERT(locked_tcb);
4071	}
4072	if (ch->chunk_type == SCTP_INITIATION) {
4073		SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4074		    ntohs(ch->chunk_length), vtag_in);
4075		if (vtag_in != 0) {
4076			/* protocol error- silently discard... */
4077			SCTP_STAT_INCR(sctps_badvtag);
4078			if (locked_tcb) {
4079				SCTP_TCB_UNLOCK(locked_tcb);
4080			}
4081			return (NULL);
4082		}
4083	} else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4084		/*
4085		 * If there is no stcb, skip the AUTH chunk and process
4086		 * later after a stcb is found (to validate the lookup was
4087		 * valid.
4088		 */
4089		if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4090		    (stcb == NULL) &&
4091		    !SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4092			/* save this chunk for later processing */
4093			auth_skipped = 1;
4094			auth_offset = *offset;
4095			auth_len = ntohs(ch->chunk_length);
4096
4097			/* (temporarily) move past this chunk */
4098			*offset += SCTP_SIZE32(auth_len);
4099			if (*offset >= length) {
4100				/* no more data left in the mbuf chain */
4101				*offset = length;
4102				if (locked_tcb) {
4103					SCTP_TCB_UNLOCK(locked_tcb);
4104				}
4105				return (NULL);
4106			}
4107			ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4108			    sizeof(struct sctp_chunkhdr), chunk_buf);
4109		}
4110		if (ch == NULL) {
4111			/* Help */
4112			*offset = length;
4113			if (locked_tcb) {
4114				SCTP_TCB_UNLOCK(locked_tcb);
4115			}
4116			return (NULL);
4117		}
4118		if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4119			goto process_control_chunks;
4120		}
4121		/*
4122		 * first check if it's an ASCONF with an unknown src addr we
4123		 * need to look inside to find the association
4124		 */
4125		if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4126			struct sctp_chunkhdr *asconf_ch = ch;
4127			uint32_t asconf_offset = 0, asconf_len = 0;
4128
4129			/* inp's refcount may be reduced */
4130			SCTP_INP_INCR_REF(inp);
4131
4132			asconf_offset = *offset;
4133			do {
4134				asconf_len = ntohs(asconf_ch->chunk_length);
4135				if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4136					break;
4137				stcb = sctp_findassociation_ep_asconf(m, iphlen,
4138				    *offset, sh, &inp, netp, vrf_id);
4139				if (stcb != NULL)
4140					break;
4141				asconf_offset += SCTP_SIZE32(asconf_len);
4142				asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4143				    sizeof(struct sctp_chunkhdr), chunk_buf);
4144			} while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4145			if (stcb == NULL) {
4146				/*
4147				 * reduce inp's refcount if not reduced in
4148				 * sctp_findassociation_ep_asconf().
4149				 */
4150				SCTP_INP_DECR_REF(inp);
4151			} else {
4152				locked_tcb = stcb;
4153			}
4154
4155			/* now go back and verify any auth chunk to be sure */
4156			if (auth_skipped && (stcb != NULL)) {
4157				struct sctp_auth_chunk *auth;
4158
4159				auth = (struct sctp_auth_chunk *)
4160				    sctp_m_getptr(m, auth_offset,
4161				    auth_len, chunk_buf);
4162				got_auth = 1;
4163				auth_skipped = 0;
4164				if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4165				    auth_offset)) {
4166					/* auth HMAC failed so dump it */
4167					*offset = length;
4168					if (locked_tcb) {
4169						SCTP_TCB_UNLOCK(locked_tcb);
4170					}
4171					return (NULL);
4172				} else {
4173					/* remaining chunks are HMAC checked */
4174					stcb->asoc.authenticated = 1;
4175				}
4176			}
4177		}
4178		if (stcb == NULL) {
4179			/* no association, so it's out of the blue... */
4180			sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL,
4181			    vrf_id, port);
4182			*offset = length;
4183			if (locked_tcb) {
4184				SCTP_TCB_UNLOCK(locked_tcb);
4185			}
4186			return (NULL);
4187		}
4188		asoc = &stcb->asoc;
4189		/* ABORT and SHUTDOWN can use either v_tag... */
4190		if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4191		    (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4192		    (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4193			if ((vtag_in == asoc->my_vtag) ||
4194			    ((ch->chunk_flags & SCTP_HAD_NO_TCB) &&
4195			    (vtag_in == asoc->peer_vtag))) {
4196				/* this is valid */
4197			} else {
4198				/* drop this packet... */
4199				SCTP_STAT_INCR(sctps_badvtag);
4200				if (locked_tcb) {
4201					SCTP_TCB_UNLOCK(locked_tcb);
4202				}
4203				return (NULL);
4204			}
4205		} else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4206			if (vtag_in != asoc->my_vtag) {
4207				/*
4208				 * this could be a stale SHUTDOWN-ACK or the
4209				 * peer never got the SHUTDOWN-COMPLETE and
4210				 * is still hung; we have started a new asoc
4211				 * but it won't complete until the shutdown
4212				 * is completed
4213				 */
4214				if (locked_tcb) {
4215					SCTP_TCB_UNLOCK(locked_tcb);
4216				}
4217				sctp_handle_ootb(m, iphlen, *offset, sh, inp,
4218				    NULL, vrf_id, port);
4219				return (NULL);
4220			}
4221		} else {
4222			/* for all other chunks, vtag must match */
4223			if (vtag_in != asoc->my_vtag) {
4224				/* invalid vtag... */
4225				SCTPDBG(SCTP_DEBUG_INPUT3,
4226				    "invalid vtag: %xh, expect %xh\n",
4227				    vtag_in, asoc->my_vtag);
4228				SCTP_STAT_INCR(sctps_badvtag);
4229				if (locked_tcb) {
4230					SCTP_TCB_UNLOCK(locked_tcb);
4231				}
4232				*offset = length;
4233				return (NULL);
4234			}
4235		}
4236	}			/* end if !SCTP_COOKIE_ECHO */
4237	/*
4238	 * process all control chunks...
4239	 */
4240	if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4241	/* EY */
4242	    (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4243	    (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4244	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
4245		/* implied cookie-ack.. we must have lost the ack */
4246		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4247			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4248			    stcb->asoc.overall_error_count,
4249			    0,
4250			    SCTP_FROM_SCTP_INPUT,
4251			    __LINE__);
4252		}
4253		stcb->asoc.overall_error_count = 0;
4254		sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4255		    *netp);
4256	}
4257process_control_chunks:
4258	while (IS_SCTP_CONTROL(ch)) {
4259		/* validate chunk length */
4260		chk_length = ntohs(ch->chunk_length);
4261		SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4262		    ch->chunk_type, chk_length);
4263		SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4264		if (chk_length < sizeof(*ch) ||
4265		    (*offset + (int)chk_length) > length) {
4266			*offset = length;
4267			if (locked_tcb) {
4268				SCTP_TCB_UNLOCK(locked_tcb);
4269			}
4270			return (NULL);
4271		}
4272		SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4273		/*
4274		 * INIT-ACK only gets the init ack "header" portion only
4275		 * because we don't have to process the peer's COOKIE. All
4276		 * others get a complete chunk.
4277		 */
4278		if ((ch->chunk_type == SCTP_INITIATION_ACK) ||
4279		    (ch->chunk_type == SCTP_INITIATION)) {
4280			/* get an init-ack chunk */
4281			ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4282			    sizeof(struct sctp_init_ack_chunk), chunk_buf);
4283			if (ch == NULL) {
4284				*offset = length;
4285				if (locked_tcb) {
4286					SCTP_TCB_UNLOCK(locked_tcb);
4287				}
4288				return (NULL);
4289			}
4290		} else {
4291			/* For cookies and all other chunks. */
4292			if (chk_length > sizeof(chunk_buf)) {
4293				/*
4294				 * use just the size of the chunk buffer so
4295				 * the front part of our chunks fit in
4296				 * contiguous space up to the chunk buffer
4297				 * size (508 bytes). For chunks that need to
4298				 * get more than that they must use the
4299				 * sctp_m_getptr() function or other means
4300				 * (e.g. know how to parse mbuf chains).
4301				 * Cookies do this already.
4302				 */
4303				ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4304				    (sizeof(chunk_buf) - 4),
4305				    chunk_buf);
4306				if (ch == NULL) {
4307					*offset = length;
4308					if (locked_tcb) {
4309						SCTP_TCB_UNLOCK(locked_tcb);
4310					}
4311					return (NULL);
4312				}
4313			} else {
4314				/* We can fit it all */
4315				ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4316				    chk_length, chunk_buf);
4317				if (ch == NULL) {
4318					SCTP_PRINTF("sctp_process_control: Can't get the all data....\n");
4319					*offset = length;
4320					if (locked_tcb) {
4321						SCTP_TCB_UNLOCK(locked_tcb);
4322					}
4323					return (NULL);
4324				}
4325			}
4326		}
4327		num_chunks++;
4328		/* Save off the last place we got a control from */
4329		if (stcb != NULL) {
4330			if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4331				/*
4332				 * allow last_control to be NULL if
4333				 * ASCONF... ASCONF processing will find the
4334				 * right net later
4335				 */
4336				if ((netp != NULL) && (*netp != NULL))
4337					stcb->asoc.last_control_chunk_from = *netp;
4338			}
4339		}
4340#ifdef SCTP_AUDITING_ENABLED
4341		sctp_audit_log(0xB0, ch->chunk_type);
4342#endif
4343
4344		/* check to see if this chunk required auth, but isn't */
4345		if ((stcb != NULL) &&
4346		    !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
4347		    sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4348		    !stcb->asoc.authenticated) {
4349			/* "silently" ignore */
4350			SCTP_STAT_INCR(sctps_recvauthmissing);
4351			goto next_chunk;
4352		}
4353		switch (ch->chunk_type) {
4354		case SCTP_INITIATION:
4355			/* must be first and only chunk */
4356			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4357			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4358				/* We are not interested anymore? */
4359				if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4360					/*
4361					 * collision case where we are
4362					 * sending to them too
4363					 */
4364					;
4365				} else {
4366					if (locked_tcb) {
4367						SCTP_TCB_UNLOCK(locked_tcb);
4368					}
4369					*offset = length;
4370					return (NULL);
4371				}
4372			}
4373			if ((chk_length > SCTP_LARGEST_INIT_ACCEPTED) ||
4374			    (num_chunks > 1) ||
4375			    (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) {
4376				*offset = length;
4377				if (locked_tcb) {
4378					SCTP_TCB_UNLOCK(locked_tcb);
4379				}
4380				return (NULL);
4381			}
4382			if ((stcb != NULL) &&
4383			    (SCTP_GET_STATE(&stcb->asoc) ==
4384			    SCTP_STATE_SHUTDOWN_ACK_SENT)) {
4385				sctp_send_shutdown_ack(stcb,
4386				    stcb->asoc.primary_destination);
4387				*offset = length;
4388				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4389				if (locked_tcb) {
4390					SCTP_TCB_UNLOCK(locked_tcb);
4391				}
4392				return (NULL);
4393			}
4394			if (netp) {
4395				sctp_handle_init(m, iphlen, *offset, sh,
4396				    (struct sctp_init_chunk *)ch, inp,
4397				    stcb, *netp, &abort_no_unlock, vrf_id, port);
4398			}
4399			if (abort_no_unlock)
4400				return (NULL);
4401
4402			*offset = length;
4403			if (locked_tcb) {
4404				SCTP_TCB_UNLOCK(locked_tcb);
4405			}
4406			return (NULL);
4407			break;
4408		case SCTP_PAD_CHUNK:
4409			break;
4410		case SCTP_INITIATION_ACK:
4411			/* must be first and only chunk */
4412			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n");
4413			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4414				/* We are not interested anymore */
4415				if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4416					;
4417				} else {
4418					if (locked_tcb) {
4419						SCTP_TCB_UNLOCK(locked_tcb);
4420					}
4421					*offset = length;
4422					if (stcb) {
4423#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4424						so = SCTP_INP_SO(inp);
4425						atomic_add_int(&stcb->asoc.refcnt, 1);
4426						SCTP_TCB_UNLOCK(stcb);
4427						SCTP_SOCKET_LOCK(so, 1);
4428						SCTP_TCB_LOCK(stcb);
4429						atomic_subtract_int(&stcb->asoc.refcnt, 1);
4430#endif
4431						(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
4432#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4433						SCTP_SOCKET_UNLOCK(so, 1);
4434#endif
4435					}
4436					return (NULL);
4437				}
4438			}
4439			if ((num_chunks > 1) ||
4440			    (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) {
4441				*offset = length;
4442				if (locked_tcb) {
4443					SCTP_TCB_UNLOCK(locked_tcb);
4444				}
4445				return (NULL);
4446			}
4447			if ((netp) && (*netp)) {
4448				ret = sctp_handle_init_ack(m, iphlen, *offset, sh,
4449				    (struct sctp_init_ack_chunk *)ch, stcb, *netp, &abort_no_unlock, vrf_id);
4450			} else {
4451				ret = -1;
4452			}
4453			/*
4454			 * Special case, I must call the output routine to
4455			 * get the cookie echoed
4456			 */
4457			if (abort_no_unlock)
4458				return (NULL);
4459
4460			if ((stcb) && ret == 0)
4461				sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4462			*offset = length;
4463			if (locked_tcb) {
4464				SCTP_TCB_UNLOCK(locked_tcb);
4465			}
4466			return (NULL);
4467			break;
4468		case SCTP_SELECTIVE_ACK:
4469			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK\n");
4470			SCTP_STAT_INCR(sctps_recvsacks);
4471			{
4472				struct sctp_sack_chunk *sack;
4473				int abort_now = 0;
4474				uint32_t a_rwnd, cum_ack;
4475				uint16_t num_seg;
4476				int nonce_sum_flag;
4477
4478				if ((stcb == NULL) || (chk_length < sizeof(struct sctp_sack_chunk))) {
4479					SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on sack chunk, too small\n");
4480					*offset = length;
4481					if (locked_tcb) {
4482						SCTP_TCB_UNLOCK(locked_tcb);
4483					}
4484					return (NULL);
4485				}
4486				if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4487					/*-
4488					 * If we have sent a shutdown-ack, we will pay no
4489					 * attention to a sack sent in to us since
4490					 * we don't care anymore.
4491					 */
4492					break;
4493				}
4494				sack = (struct sctp_sack_chunk *)ch;
4495				nonce_sum_flag = ch->chunk_flags & SCTP_SACK_NONCE_SUM;
4496				cum_ack = ntohl(sack->sack.cum_tsn_ack);
4497				num_seg = ntohs(sack->sack.num_gap_ack_blks);
4498				a_rwnd = (uint32_t) ntohl(sack->sack.a_rwnd);
4499				SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4500				    cum_ack,
4501				    num_seg,
4502				    a_rwnd
4503				    );
4504				stcb->asoc.seen_a_sack_this_pkt = 1;
4505				if ((stcb->asoc.pr_sctp_cnt == 0) &&
4506				    (num_seg == 0) &&
4507				    ((compare_with_wrap(cum_ack, stcb->asoc.last_acked_seq, MAX_TSN)) ||
4508				    (cum_ack == stcb->asoc.last_acked_seq)) &&
4509				    (stcb->asoc.saw_sack_with_frags == 0) &&
4510				    (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
4511				    ) {
4512					/*
4513					 * We have a SIMPLE sack having no
4514					 * prior segments and data on sent
4515					 * queue to be acked.. Use the
4516					 * faster path sack processing. We
4517					 * also allow window update sacks
4518					 * with no missing segments to go
4519					 * this way too.
4520					 */
4521					sctp_express_handle_sack(stcb, cum_ack, a_rwnd, nonce_sum_flag,
4522					    &abort_now);
4523				} else {
4524					if (netp && *netp)
4525						sctp_handle_sack(m, *offset,
4526						    sack, stcb, *netp, &abort_now, chk_length, a_rwnd);
4527				}
4528				if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4529				    TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4530				    (stcb->asoc.stream_queue_cnt == 0)) {
4531					sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4532				}
4533				if (abort_now) {
4534					/* ABORT signal from sack processing */
4535					*offset = length;
4536					return (NULL);
4537				}
4538			}
4539			break;
4540			/*
4541			 * EY - nr_sack:  If the received chunk is an
4542			 * nr_sack chunk
4543			 */
4544		case SCTP_NR_SELECTIVE_ACK:
4545			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK\n");
4546			SCTP_STAT_INCR(sctps_recvsacks);
4547			{
4548				struct sctp_nr_sack_chunk *nr_sack;
4549				int abort_now = 0;
4550				uint32_t a_rwnd, cum_ack;
4551				uint16_t num_seg, num_nr_seg;
4552				int nonce_sum_flag, all_bit;
4553
4554				if ((stcb == NULL) || (chk_length < sizeof(struct sctp_nr_sack_chunk))) {
4555					SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on nr_sack chunk, too small\n");
4556			ignore_nr_sack:
4557					*offset = length;
4558					if (locked_tcb) {
4559						SCTP_TCB_UNLOCK(locked_tcb);
4560					}
4561					return (NULL);
4562				}
4563				/*
4564				 * EY nr_sacks have not been negotiated but
4565				 * the peer end sent an nr_sack, silently
4566				 * discard the chunk
4567				 */
4568				if (!(SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack)) {
4569					goto unknown_chunk;
4570				}
4571				if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4572					/*-
4573					 * If we have sent a shutdown-ack, we will pay no
4574					 * attention to a sack sent in to us since
4575					 * we don't care anymore.
4576					 */
4577					goto ignore_nr_sack;
4578				}
4579				nr_sack = (struct sctp_nr_sack_chunk *)ch;
4580				nonce_sum_flag = ch->chunk_flags & SCTP_SACK_NONCE_SUM;
4581				all_bit = ch->chunk_flags & SCTP_NR_SACK_ALL_BIT;
4582
4583				cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
4584				num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
4585				/*
4586				 * EY -if All bit  is set, then there are as
4587				 * many gaps as nr_gaps
4588				 */
4589				if (all_bit) {
4590					num_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4591				}
4592				num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4593				a_rwnd = (uint32_t) ntohl(nr_sack->nr_sack.a_rwnd);
4594				SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_NR_SACK process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4595				    cum_ack,
4596				    num_seg,
4597				    a_rwnd
4598				    );
4599				stcb->asoc.seen_a_sack_this_pkt = 1;
4600				if ((stcb->asoc.pr_sctp_cnt == 0) &&
4601				    (num_seg == 0) &&
4602				    ((compare_with_wrap(cum_ack, stcb->asoc.last_acked_seq, MAX_TSN)) ||
4603				    (cum_ack == stcb->asoc.last_acked_seq)) &&
4604				    (stcb->asoc.saw_sack_with_frags == 0) &&
4605				    (!TAILQ_EMPTY(&stcb->asoc.sent_queue))
4606				    ) {
4607					/*
4608					 * We have a SIMPLE sack having no
4609					 * prior segments and data on sent
4610					 * queue to be acked.. Use the
4611					 * faster path sack processing. We
4612					 * also allow window update sacks
4613					 * with no missing segments to go
4614					 * this way too.
4615					 */
4616					sctp_express_handle_nr_sack(stcb, cum_ack, a_rwnd, nonce_sum_flag,
4617					    &abort_now);
4618				} else {
4619					if (netp && *netp)
4620						sctp_handle_nr_sack(m, *offset,
4621						    nr_sack, stcb, *netp, &abort_now, chk_length, a_rwnd);
4622				}
4623				if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4624				    TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4625				    (stcb->asoc.stream_queue_cnt == 0)) {
4626					sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
4627				}
4628				if (abort_now) {
4629					/* ABORT signal from sack processing */
4630					*offset = length;
4631					return (NULL);
4632				}
4633			}
4634			break;
4635
4636		case SCTP_HEARTBEAT_REQUEST:
4637			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
4638			if ((stcb) && netp && *netp) {
4639				SCTP_STAT_INCR(sctps_recvheartbeat);
4640				sctp_send_heartbeat_ack(stcb, m, *offset,
4641				    chk_length, *netp);
4642
4643				/* He's alive so give him credit */
4644				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4645					sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4646					    stcb->asoc.overall_error_count,
4647					    0,
4648					    SCTP_FROM_SCTP_INPUT,
4649					    __LINE__);
4650				}
4651				stcb->asoc.overall_error_count = 0;
4652			}
4653			break;
4654		case SCTP_HEARTBEAT_ACK:
4655			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT-ACK\n");
4656			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
4657				/* Its not ours */
4658				*offset = length;
4659				if (locked_tcb) {
4660					SCTP_TCB_UNLOCK(locked_tcb);
4661				}
4662				return (NULL);
4663			}
4664			/* He's alive so give him credit */
4665			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4666				sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4667				    stcb->asoc.overall_error_count,
4668				    0,
4669				    SCTP_FROM_SCTP_INPUT,
4670				    __LINE__);
4671			}
4672			stcb->asoc.overall_error_count = 0;
4673			SCTP_STAT_INCR(sctps_recvheartbeatack);
4674			if (netp && *netp)
4675				sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
4676				    stcb, *netp);
4677			break;
4678		case SCTP_ABORT_ASSOCIATION:
4679			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
4680			    stcb);
4681			if ((stcb) && netp && *netp)
4682				sctp_handle_abort((struct sctp_abort_chunk *)ch,
4683				    stcb, *netp);
4684			*offset = length;
4685			return (NULL);
4686			break;
4687		case SCTP_SHUTDOWN:
4688			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
4689			    stcb);
4690			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
4691				*offset = length;
4692				if (locked_tcb) {
4693					SCTP_TCB_UNLOCK(locked_tcb);
4694				}
4695				return (NULL);
4696			}
4697			if (netp && *netp) {
4698				int abort_flag = 0;
4699
4700				sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
4701				    stcb, *netp, &abort_flag);
4702				if (abort_flag) {
4703					*offset = length;
4704					return (NULL);
4705				}
4706			}
4707			break;
4708		case SCTP_SHUTDOWN_ACK:
4709			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-ACK, stcb %p\n", stcb);
4710			if ((stcb) && (netp) && (*netp))
4711				sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
4712			*offset = length;
4713			return (NULL);
4714			break;
4715
4716		case SCTP_OPERATION_ERROR:
4717			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n");
4718			if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) {
4719
4720				*offset = length;
4721				return (NULL);
4722			}
4723			break;
4724		case SCTP_COOKIE_ECHO:
4725			SCTPDBG(SCTP_DEBUG_INPUT3,
4726			    "SCTP_COOKIE-ECHO, stcb %p\n", stcb);
4727			if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4728				;
4729			} else {
4730				if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4731					/* We are not interested anymore */
4732					*offset = length;
4733					return (NULL);
4734				}
4735			}
4736			/*
4737			 * First are we accepting? We do this again here
4738			 * sincen it is possible that a previous endpoint
4739			 * WAS listening responded to a INIT-ACK and then
4740			 * closed. We opened and bound.. and are now no
4741			 * longer listening.
4742			 */
4743
4744			if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) {
4745				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4746				    (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
4747					struct mbuf *oper;
4748					struct sctp_paramhdr *phdr;
4749
4750					oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
4751					    0, M_DONTWAIT, 1, MT_DATA);
4752					if (oper) {
4753						SCTP_BUF_LEN(oper) =
4754						    sizeof(struct sctp_paramhdr);
4755						phdr = mtod(oper,
4756						    struct sctp_paramhdr *);
4757						phdr->param_type =
4758						    htons(SCTP_CAUSE_OUT_OF_RESC);
4759						phdr->param_length =
4760						    htons(sizeof(struct sctp_paramhdr));
4761					}
4762					sctp_abort_association(inp, stcb, m,
4763					    iphlen, sh, oper, vrf_id, port);
4764				}
4765				*offset = length;
4766				return (NULL);
4767			} else {
4768				struct mbuf *ret_buf;
4769				struct sctp_inpcb *linp;
4770
4771				if (stcb) {
4772					linp = NULL;
4773				} else {
4774					linp = inp;
4775				}
4776
4777				if (linp) {
4778					SCTP_ASOC_CREATE_LOCK(linp);
4779				}
4780				if (netp) {
4781					ret_buf =
4782					    sctp_handle_cookie_echo(m, iphlen,
4783					    *offset, sh,
4784					    (struct sctp_cookie_echo_chunk *)ch,
4785					    &inp, &stcb, netp,
4786					    auth_skipped,
4787					    auth_offset,
4788					    auth_len,
4789					    &locked_tcb,
4790					    vrf_id,
4791					    port);
4792				} else {
4793					ret_buf = NULL;
4794				}
4795				if (linp) {
4796					SCTP_ASOC_CREATE_UNLOCK(linp);
4797				}
4798				if (ret_buf == NULL) {
4799					if (locked_tcb) {
4800						SCTP_TCB_UNLOCK(locked_tcb);
4801					}
4802					SCTPDBG(SCTP_DEBUG_INPUT3,
4803					    "GAK, null buffer\n");
4804					auth_skipped = 0;
4805					*offset = length;
4806					return (NULL);
4807				}
4808				/* if AUTH skipped, see if it verified... */
4809				if (auth_skipped) {
4810					got_auth = 1;
4811					auth_skipped = 0;
4812				}
4813				if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
4814					/*
4815					 * Restart the timer if we have
4816					 * pending data
4817					 */
4818					struct sctp_tmit_chunk *chk;
4819
4820					chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
4821					if (chk) {
4822						sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4823						    stcb->sctp_ep, stcb,
4824						    chk->whoTo);
4825					}
4826				}
4827			}
4828			break;
4829		case SCTP_COOKIE_ACK:
4830			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE-ACK, stcb %p\n", stcb);
4831			if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
4832				if (locked_tcb) {
4833					SCTP_TCB_UNLOCK(locked_tcb);
4834				}
4835				return (NULL);
4836			}
4837			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4838				/* We are not interested anymore */
4839				if ((stcb) && (stcb->asoc.total_output_queue_size)) {
4840					;
4841				} else if (stcb) {
4842#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4843					so = SCTP_INP_SO(inp);
4844					atomic_add_int(&stcb->asoc.refcnt, 1);
4845					SCTP_TCB_UNLOCK(stcb);
4846					SCTP_SOCKET_LOCK(so, 1);
4847					SCTP_TCB_LOCK(stcb);
4848					atomic_subtract_int(&stcb->asoc.refcnt, 1);
4849#endif
4850					(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
4851#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4852					SCTP_SOCKET_UNLOCK(so, 1);
4853#endif
4854					*offset = length;
4855					return (NULL);
4856				}
4857			}
4858			/* He's alive so give him credit */
4859			if ((stcb) && netp && *netp) {
4860				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4861					sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4862					    stcb->asoc.overall_error_count,
4863					    0,
4864					    SCTP_FROM_SCTP_INPUT,
4865					    __LINE__);
4866				}
4867				stcb->asoc.overall_error_count = 0;
4868				sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
4869			}
4870			break;
4871		case SCTP_ECN_ECHO:
4872			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-ECHO\n");
4873			/* He's alive so give him credit */
4874			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
4875				/* Its not ours */
4876				if (locked_tcb) {
4877					SCTP_TCB_UNLOCK(locked_tcb);
4878				}
4879				*offset = length;
4880				return (NULL);
4881			}
4882			if (stcb) {
4883				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4884					sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4885					    stcb->asoc.overall_error_count,
4886					    0,
4887					    SCTP_FROM_SCTP_INPUT,
4888					    __LINE__);
4889				}
4890				stcb->asoc.overall_error_count = 0;
4891				sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
4892				    stcb);
4893			}
4894			break;
4895		case SCTP_ECN_CWR:
4896			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN-CWR\n");
4897			/* He's alive so give him credit */
4898			if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
4899				/* Its not ours */
4900				if (locked_tcb) {
4901					SCTP_TCB_UNLOCK(locked_tcb);
4902				}
4903				*offset = length;
4904				return (NULL);
4905			}
4906			if (stcb) {
4907				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4908					sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4909					    stcb->asoc.overall_error_count,
4910					    0,
4911					    SCTP_FROM_SCTP_INPUT,
4912					    __LINE__);
4913				}
4914				stcb->asoc.overall_error_count = 0;
4915				sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb);
4916			}
4917			break;
4918		case SCTP_SHUTDOWN_COMPLETE:
4919			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN-COMPLETE, stcb %p\n", stcb);
4920			/* must be first and only chunk */
4921			if ((num_chunks > 1) ||
4922			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4923				*offset = length;
4924				if (locked_tcb) {
4925					SCTP_TCB_UNLOCK(locked_tcb);
4926				}
4927				return (NULL);
4928			}
4929			if ((stcb) && netp && *netp) {
4930				sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
4931				    stcb, *netp);
4932			}
4933			*offset = length;
4934			return (NULL);
4935			break;
4936		case SCTP_ASCONF:
4937			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
4938			/* He's alive so give him credit */
4939			if (stcb) {
4940				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4941					sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4942					    stcb->asoc.overall_error_count,
4943					    0,
4944					    SCTP_FROM_SCTP_INPUT,
4945					    __LINE__);
4946				}
4947				stcb->asoc.overall_error_count = 0;
4948				sctp_handle_asconf(m, *offset,
4949				    (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
4950				asconf_cnt++;
4951			}
4952			break;
4953		case SCTP_ASCONF_ACK:
4954			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF-ACK\n");
4955			if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
4956				/* Its not ours */
4957				if (locked_tcb) {
4958					SCTP_TCB_UNLOCK(locked_tcb);
4959				}
4960				*offset = length;
4961				return (NULL);
4962			}
4963			if ((stcb) && netp && *netp) {
4964				/* He's alive so give him credit */
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_asconf_ack(m, *offset,
4974				    (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
4975				if (abort_no_unlock)
4976					return (NULL);
4977			}
4978			break;
4979		case SCTP_FORWARD_CUM_TSN:
4980			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD-TSN\n");
4981			if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
4982				/* Its not ours */
4983				if (locked_tcb) {
4984					SCTP_TCB_UNLOCK(locked_tcb);
4985				}
4986				*offset = length;
4987				return (NULL);
4988			}
4989			/* He's alive so give him credit */
4990			if (stcb) {
4991				int abort_flag = 0;
4992
4993				stcb->asoc.overall_error_count = 0;
4994				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4995					sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4996					    stcb->asoc.overall_error_count,
4997					    0,
4998					    SCTP_FROM_SCTP_INPUT,
4999					    __LINE__);
5000				}
5001				*fwd_tsn_seen = 1;
5002				if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5003					/* We are not interested anymore */
5004#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5005					so = SCTP_INP_SO(inp);
5006					atomic_add_int(&stcb->asoc.refcnt, 1);
5007					SCTP_TCB_UNLOCK(stcb);
5008					SCTP_SOCKET_LOCK(so, 1);
5009					SCTP_TCB_LOCK(stcb);
5010					atomic_subtract_int(&stcb->asoc.refcnt, 1);
5011#endif
5012					(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
5013#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5014					SCTP_SOCKET_UNLOCK(so, 1);
5015#endif
5016					*offset = length;
5017					return (NULL);
5018				}
5019				sctp_handle_forward_tsn(stcb,
5020				    (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5021				if (abort_flag) {
5022					*offset = length;
5023					return (NULL);
5024				} else {
5025					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5026						sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5027						    stcb->asoc.overall_error_count,
5028						    0,
5029						    SCTP_FROM_SCTP_INPUT,
5030						    __LINE__);
5031					}
5032					stcb->asoc.overall_error_count = 0;
5033				}
5034
5035			}
5036			break;
5037		case SCTP_STREAM_RESET:
5038			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5039			if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5040				/* Its not ours */
5041				if (locked_tcb) {
5042					SCTP_TCB_UNLOCK(locked_tcb);
5043				}
5044				*offset = length;
5045				return (NULL);
5046			}
5047			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5048				/* We are not interested anymore */
5049#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5050				so = SCTP_INP_SO(inp);
5051				atomic_add_int(&stcb->asoc.refcnt, 1);
5052				SCTP_TCB_UNLOCK(stcb);
5053				SCTP_SOCKET_LOCK(so, 1);
5054				SCTP_TCB_LOCK(stcb);
5055				atomic_subtract_int(&stcb->asoc.refcnt, 1);
5056#endif
5057				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
5058#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5059				SCTP_SOCKET_UNLOCK(so, 1);
5060#endif
5061				*offset = length;
5062				return (NULL);
5063			}
5064			if (stcb->asoc.peer_supports_strreset == 0) {
5065				/*
5066				 * hmm, peer should have announced this, but
5067				 * we will turn it on since he is sending us
5068				 * a stream reset.
5069				 */
5070				stcb->asoc.peer_supports_strreset = 1;
5071			}
5072			if (sctp_handle_stream_reset(stcb, m, *offset, (struct sctp_stream_reset_out_req *)ch)) {
5073				/* stop processing */
5074				*offset = length;
5075				return (NULL);
5076			}
5077			break;
5078		case SCTP_PACKET_DROPPED:
5079			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5080			/* re-get it all please */
5081			if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5082				/* Its not ours */
5083				if (locked_tcb) {
5084					SCTP_TCB_UNLOCK(locked_tcb);
5085				}
5086				*offset = length;
5087				return (NULL);
5088			}
5089			if (ch && (stcb) && netp && (*netp)) {
5090				sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5091				    stcb, *netp,
5092				    min(chk_length, (sizeof(chunk_buf) - 4)));
5093
5094			}
5095			break;
5096
5097		case SCTP_AUTHENTICATION:
5098			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5099			if (SCTP_BASE_SYSCTL(sctp_auth_disable))
5100				goto unknown_chunk;
5101
5102			if (stcb == NULL) {
5103				/* save the first AUTH for later processing */
5104				if (auth_skipped == 0) {
5105					auth_offset = *offset;
5106					auth_len = chk_length;
5107					auth_skipped = 1;
5108				}
5109				/* skip this chunk (temporarily) */
5110				goto next_chunk;
5111			}
5112			if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5113			    (chk_length > (sizeof(struct sctp_auth_chunk) +
5114			    SCTP_AUTH_DIGEST_LEN_MAX))) {
5115				/* Its not ours */
5116				if (locked_tcb) {
5117					SCTP_TCB_UNLOCK(locked_tcb);
5118				}
5119				*offset = length;
5120				return (NULL);
5121			}
5122			if (got_auth == 1) {
5123				/* skip this chunk... it's already auth'd */
5124				goto next_chunk;
5125			}
5126			got_auth = 1;
5127			if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5128			    m, *offset)) {
5129				/* auth HMAC failed so dump the packet */
5130				*offset = length;
5131				return (stcb);
5132			} else {
5133				/* remaining chunks are HMAC checked */
5134				stcb->asoc.authenticated = 1;
5135			}
5136			break;
5137
5138		default:
5139	unknown_chunk:
5140			/* it's an unknown chunk! */
5141			if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5142				struct mbuf *mm;
5143				struct sctp_paramhdr *phd;
5144
5145				mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
5146				    0, M_DONTWAIT, 1, MT_DATA);
5147				if (mm) {
5148					phd = mtod(mm, struct sctp_paramhdr *);
5149					/*
5150					 * We cheat and use param type since
5151					 * we did not bother to define a
5152					 * error cause struct. They are the
5153					 * same basic format with different
5154					 * names.
5155					 */
5156					phd->param_type = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5157					phd->param_length = htons(chk_length + sizeof(*phd));
5158					SCTP_BUF_LEN(mm) = sizeof(*phd);
5159					SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset, SCTP_SIZE32(chk_length),
5160					    M_DONTWAIT);
5161					if (SCTP_BUF_NEXT(mm)) {
5162#ifdef SCTP_MBUF_LOGGING
5163						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5164							struct mbuf *mat;
5165
5166							mat = SCTP_BUF_NEXT(mm);
5167							while (mat) {
5168								if (SCTP_BUF_IS_EXTENDED(mat)) {
5169									sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5170								}
5171								mat = SCTP_BUF_NEXT(mat);
5172							}
5173						}
5174#endif
5175						sctp_queue_op_err(stcb, mm);
5176					} else {
5177						sctp_m_freem(mm);
5178					}
5179				}
5180			}
5181			if ((ch->chunk_type & 0x80) == 0) {
5182				/* discard this packet */
5183				*offset = length;
5184				return (stcb);
5185			}	/* else skip this bad chunk and continue... */
5186			break;
5187		}		/* switch (ch->chunk_type) */
5188
5189
5190next_chunk:
5191		/* get the next chunk */
5192		*offset += SCTP_SIZE32(chk_length);
5193		if (*offset >= length) {
5194			/* no more data left in the mbuf chain */
5195			break;
5196		}
5197		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5198		    sizeof(struct sctp_chunkhdr), chunk_buf);
5199		if (ch == NULL) {
5200			if (locked_tcb) {
5201				SCTP_TCB_UNLOCK(locked_tcb);
5202			}
5203			*offset = length;
5204			return (NULL);
5205		}
5206	}			/* while */
5207
5208	if (asconf_cnt > 0 && stcb != NULL) {
5209		sctp_send_asconf_ack(stcb);
5210	}
5211	return (stcb);
5212}
5213
5214
5215/*
5216 * Process the ECN bits we have something set so we must look to see if it is
5217 * ECN(0) or ECN(1) or CE
5218 */
5219static void
5220sctp_process_ecn_marked_a(struct sctp_tcb *stcb, struct sctp_nets *net,
5221    uint8_t ecn_bits)
5222{
5223	if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) {
5224		;
5225	} else if ((ecn_bits & SCTP_ECT1_BIT) == SCTP_ECT1_BIT) {
5226		/*
5227		 * we only add to the nonce sum for ECT1, ECT0 does not
5228		 * change the NS bit (that we have yet to find a way to send
5229		 * it yet).
5230		 */
5231
5232		/* ECN Nonce stuff */
5233		stcb->asoc.receiver_nonce_sum++;
5234		stcb->asoc.receiver_nonce_sum &= SCTP_SACK_NONCE_SUM;
5235
5236		/*
5237		 * Drag up the last_echo point if cumack is larger since we
5238		 * don't want the point falling way behind by more than
5239		 * 2^^31 and then having it be incorrect.
5240		 */
5241		if (compare_with_wrap(stcb->asoc.cumulative_tsn,
5242		    stcb->asoc.last_echo_tsn, MAX_TSN)) {
5243			stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn;
5244		}
5245	} else if ((ecn_bits & SCTP_ECT0_BIT) == SCTP_ECT0_BIT) {
5246		/*
5247		 * Drag up the last_echo point if cumack is larger since we
5248		 * don't want the point falling way behind by more than
5249		 * 2^^31 and then having it be incorrect.
5250		 */
5251		if (compare_with_wrap(stcb->asoc.cumulative_tsn,
5252		    stcb->asoc.last_echo_tsn, MAX_TSN)) {
5253			stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn;
5254		}
5255	}
5256}
5257
5258static void
5259sctp_process_ecn_marked_b(struct sctp_tcb *stcb, struct sctp_nets *net,
5260    uint32_t high_tsn, uint8_t ecn_bits)
5261{
5262	if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) {
5263		/*
5264		 * we possibly must notify the sender that a congestion
5265		 * window reduction is in order. We do this by adding a ECNE
5266		 * chunk to the output chunk queue. The incoming CWR will
5267		 * remove this chunk.
5268		 */
5269		if (compare_with_wrap(high_tsn, stcb->asoc.last_echo_tsn,
5270		    MAX_TSN)) {
5271			/* Yep, we need to add a ECNE */
5272			sctp_send_ecn_echo(stcb, net, high_tsn);
5273			stcb->asoc.last_echo_tsn = high_tsn;
5274		}
5275	}
5276}
5277
5278#ifdef INVARIANTS
5279static void
5280sctp_validate_no_locks(struct sctp_inpcb *inp)
5281{
5282	struct sctp_tcb *stcb;
5283
5284	LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5285		if (mtx_owned(&stcb->tcb_mtx)) {
5286			panic("Own lock on stcb at return from input");
5287		}
5288	}
5289}
5290
5291#endif
5292
5293/*
5294 * common input chunk processing (v4 and v6)
5295 */
5296void
5297sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset,
5298    int length, struct sctphdr *sh, struct sctp_chunkhdr *ch,
5299    struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net,
5300    uint8_t ecn_bits, uint32_t vrf_id, uint16_t port)
5301{
5302	/*
5303	 * Control chunk processing
5304	 */
5305	uint32_t high_tsn;
5306	int fwd_tsn_seen = 0, data_processed = 0;
5307	struct mbuf *m = *mm;
5308	int abort_flag = 0;
5309	int un_sent;
5310
5311	SCTP_STAT_INCR(sctps_recvdatagrams);
5312#ifdef SCTP_AUDITING_ENABLED
5313	sctp_audit_log(0xE0, 1);
5314	sctp_auditing(0, inp, stcb, net);
5315#endif
5316
5317	SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5318	    m, iphlen, offset, length, stcb);
5319	if (stcb) {
5320		/* always clear this before beginning a packet */
5321		stcb->asoc.authenticated = 0;
5322		stcb->asoc.seen_a_sack_this_pkt = 0;
5323		SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5324		    stcb, stcb->asoc.state);
5325
5326		if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5327		    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5328			/*-
5329			 * If we hit here, we had a ref count
5330			 * up when the assoc was aborted and the
5331			 * timer is clearing out the assoc, we should
5332			 * NOT respond to any packet.. its OOTB.
5333			 */
5334			SCTP_TCB_UNLOCK(stcb);
5335			sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL,
5336			    vrf_id, port);
5337			goto out_now;
5338		}
5339	}
5340	if (IS_SCTP_CONTROL(ch)) {
5341		/* process the control portion of the SCTP packet */
5342		/* sa_ignore NO_NULL_CHK */
5343		stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch,
5344		    inp, stcb, &net, &fwd_tsn_seen, vrf_id, port);
5345		if (stcb) {
5346			/*
5347			 * This covers us if the cookie-echo was there and
5348			 * it changes our INP.
5349			 */
5350			inp = stcb->sctp_ep;
5351			if ((net) && (port)) {
5352				if (net->port == 0) {
5353					sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr));
5354				}
5355				net->port = port;
5356			}
5357		}
5358	} else {
5359		/*
5360		 * no control chunks, so pre-process DATA chunks (these
5361		 * checks are taken care of by control processing)
5362		 */
5363
5364		/*
5365		 * if DATA only packet, and auth is required, then punt...
5366		 * can't have authenticated without any AUTH (control)
5367		 * chunks
5368		 */
5369		if ((stcb != NULL) &&
5370		    !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5371		    sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5372			/* "silently" ignore */
5373			SCTP_STAT_INCR(sctps_recvauthmissing);
5374			SCTP_TCB_UNLOCK(stcb);
5375			goto out_now;
5376		}
5377		if (stcb == NULL) {
5378			/* out of the blue DATA chunk */
5379			sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL,
5380			    vrf_id, port);
5381			goto out_now;
5382		}
5383		if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5384			/* v_tag mismatch! */
5385			SCTP_STAT_INCR(sctps_badvtag);
5386			SCTP_TCB_UNLOCK(stcb);
5387			goto out_now;
5388		}
5389	}
5390
5391	if (stcb == NULL) {
5392		/*
5393		 * no valid TCB for this packet, or we found it's a bad
5394		 * packet while processing control, or we're done with this
5395		 * packet (done or skip rest of data), so we drop it...
5396		 */
5397		goto out_now;
5398	}
5399	/*
5400	 * DATA chunk processing
5401	 */
5402	/* plow through the data chunks while length > offset */
5403
5404	/*
5405	 * Rest should be DATA only.  Check authentication state if AUTH for
5406	 * DATA is required.
5407	 */
5408	if ((length > offset) &&
5409	    (stcb != NULL) &&
5410	    !SCTP_BASE_SYSCTL(sctp_auth_disable) &&
5411	    sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5412	    !stcb->asoc.authenticated) {
5413		/* "silently" ignore */
5414		SCTP_STAT_INCR(sctps_recvauthmissing);
5415		SCTPDBG(SCTP_DEBUG_AUTH1,
5416		    "Data chunk requires AUTH, skipped\n");
5417		goto trigger_send;
5418	}
5419	if (length > offset) {
5420		int retval;
5421
5422		/*
5423		 * First check to make sure our state is correct. We would
5424		 * not get here unless we really did have a tag, so we don't
5425		 * abort if this happens, just dump the chunk silently.
5426		 */
5427		switch (SCTP_GET_STATE(&stcb->asoc)) {
5428		case SCTP_STATE_COOKIE_ECHOED:
5429			/*
5430			 * we consider data with valid tags in this state
5431			 * shows us the cookie-ack was lost. Imply it was
5432			 * there.
5433			 */
5434			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5435				sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5436				    stcb->asoc.overall_error_count,
5437				    0,
5438				    SCTP_FROM_SCTP_INPUT,
5439				    __LINE__);
5440			}
5441			stcb->asoc.overall_error_count = 0;
5442			sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5443			break;
5444		case SCTP_STATE_COOKIE_WAIT:
5445			/*
5446			 * We consider OOTB any data sent during asoc setup.
5447			 */
5448			sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL,
5449			    vrf_id, port);
5450			SCTP_TCB_UNLOCK(stcb);
5451			goto out_now;
5452			/* sa_ignore NOTREACHED */
5453			break;
5454		case SCTP_STATE_EMPTY:	/* should not happen */
5455		case SCTP_STATE_INUSE:	/* should not happen */
5456		case SCTP_STATE_SHUTDOWN_RECEIVED:	/* This is a peer error */
5457		case SCTP_STATE_SHUTDOWN_ACK_SENT:
5458		default:
5459			SCTP_TCB_UNLOCK(stcb);
5460			goto out_now;
5461			/* sa_ignore NOTREACHED */
5462			break;
5463		case SCTP_STATE_OPEN:
5464		case SCTP_STATE_SHUTDOWN_SENT:
5465			break;
5466		}
5467		/* take care of ECN, part 1. */
5468		if (stcb->asoc.ecn_allowed &&
5469		    (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) {
5470			sctp_process_ecn_marked_a(stcb, net, ecn_bits);
5471		}
5472		/* plow through the data chunks while length > offset */
5473		retval = sctp_process_data(mm, iphlen, &offset, length, sh,
5474		    inp, stcb, net, &high_tsn);
5475		if (retval == 2) {
5476			/*
5477			 * The association aborted, NO UNLOCK needed since
5478			 * the association is destroyed.
5479			 */
5480			goto out_now;
5481		}
5482		data_processed = 1;
5483		if (retval == 0) {
5484			/* take care of ecn part 2. */
5485			if (stcb->asoc.ecn_allowed &&
5486			    (ecn_bits & (SCTP_ECT0_BIT | SCTP_ECT1_BIT))) {
5487				sctp_process_ecn_marked_b(stcb, net, high_tsn,
5488				    ecn_bits);
5489			}
5490		}
5491		/*
5492		 * Anything important needs to have been m_copy'ed in
5493		 * process_data
5494		 */
5495	}
5496	if ((data_processed == 0) && (fwd_tsn_seen)) {
5497		int was_a_gap = 0;
5498
5499		if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
5500		    stcb->asoc.cumulative_tsn, MAX_TSN)) {
5501			/* there was a gap before this data was processed */
5502			was_a_gap = 1;
5503		}
5504		sctp_sack_check(stcb, 1, was_a_gap, &abort_flag);
5505		if (abort_flag) {
5506			/* Again, we aborted so NO UNLOCK needed */
5507			goto out_now;
5508		}
5509	}
5510	/* trigger send of any chunks in queue... */
5511trigger_send:
5512#ifdef SCTP_AUDITING_ENABLED
5513	sctp_audit_log(0xE0, 2);
5514	sctp_auditing(1, inp, stcb, net);
5515#endif
5516	SCTPDBG(SCTP_DEBUG_INPUT1,
5517	    "Check for chunk output prw:%d tqe:%d tf=%d\n",
5518	    stcb->asoc.peers_rwnd,
5519	    TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5520	    stcb->asoc.total_flight);
5521	un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5522
5523	if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue) ||
5524	    ((un_sent) &&
5525	    (stcb->asoc.peers_rwnd > 0 ||
5526	    (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
5527		SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5528		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5529		SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5530	}
5531#ifdef SCTP_AUDITING_ENABLED
5532	sctp_audit_log(0xE0, 3);
5533	sctp_auditing(2, inp, stcb, net);
5534#endif
5535	SCTP_TCB_UNLOCK(stcb);
5536out_now:
5537#ifdef INVARIANTS
5538	sctp_validate_no_locks(inp);
5539#endif
5540	return;
5541}
5542
5543#if 0
5544static void
5545sctp_print_mbuf_chain(struct mbuf *m)
5546{
5547	for (; m; m = SCTP_BUF_NEXT(m)) {
5548		printf("%p: m_len = %ld\n", m, SCTP_BUF_LEN(m));
5549		if (SCTP_BUF_IS_EXTENDED(m))
5550			printf("%p: extend_size = %d\n", m, SCTP_BUF_EXTEND_SIZE(m));
5551	}
5552}
5553
5554#endif
5555
5556void
5557sctp_input_with_port(i_pak, off, port)
5558	struct mbuf *i_pak;
5559	int off;
5560	uint16_t port;
5561{
5562#ifdef SCTP_MBUF_LOGGING
5563	struct mbuf *mat;
5564
5565#endif
5566	struct mbuf *m;
5567	int iphlen;
5568	uint32_t vrf_id = 0;
5569	uint8_t ecn_bits;
5570	struct ip *ip;
5571	struct sctphdr *sh;
5572	struct sctp_inpcb *inp = NULL;
5573
5574	uint32_t check, calc_check;
5575	struct sctp_nets *net;
5576	struct sctp_tcb *stcb = NULL;
5577	struct sctp_chunkhdr *ch;
5578	int refcount_up = 0;
5579	int length, mlen, offset;
5580
5581	if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
5582		SCTP_RELEASE_PKT(i_pak);
5583		return;
5584	}
5585	mlen = SCTP_HEADER_LEN(i_pak);
5586	iphlen = off;
5587	m = SCTP_HEADER_TO_CHAIN(i_pak);
5588
5589	net = NULL;
5590	SCTP_STAT_INCR(sctps_recvpackets);
5591	SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
5592
5593
5594#ifdef SCTP_MBUF_LOGGING
5595	/* Log in any input mbufs */
5596	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5597		mat = m;
5598		while (mat) {
5599			if (SCTP_BUF_IS_EXTENDED(mat)) {
5600				sctp_log_mb(mat, SCTP_MBUF_INPUT);
5601			}
5602			mat = SCTP_BUF_NEXT(mat);
5603		}
5604	}
5605#endif
5606#ifdef  SCTP_PACKET_LOGGING
5607	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
5608		sctp_packet_log(m, mlen);
5609#endif
5610	/*
5611	 * Must take out the iphlen, since mlen expects this (only effect lb
5612	 * case)
5613	 */
5614	mlen -= iphlen;
5615
5616	/*
5617	 * Get IP, SCTP, and first chunk header together in first mbuf.
5618	 */
5619	ip = mtod(m, struct ip *);
5620	offset = iphlen + sizeof(*sh) + sizeof(*ch);
5621	if (SCTP_BUF_LEN(m) < offset) {
5622		if ((m = m_pullup(m, offset)) == 0) {
5623			SCTP_STAT_INCR(sctps_hdrops);
5624			return;
5625		}
5626		ip = mtod(m, struct ip *);
5627	}
5628	/* validate mbuf chain length with IP payload length */
5629	if (mlen < (SCTP_GET_IPV4_LENGTH(ip) - iphlen)) {
5630		SCTP_STAT_INCR(sctps_hdrops);
5631		goto bad;
5632	}
5633	sh = (struct sctphdr *)((caddr_t)ip + iphlen);
5634	ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh));
5635	SCTPDBG(SCTP_DEBUG_INPUT1,
5636	    "sctp_input() length:%d iphlen:%d\n", mlen, iphlen);
5637
5638	/* SCTP does not allow broadcasts or multicasts */
5639	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
5640		goto bad;
5641	}
5642	if (SCTP_IS_IT_BROADCAST(ip->ip_dst, m)) {
5643		/*
5644		 * We only look at broadcast if its a front state, All
5645		 * others we will not have a tcb for anyway.
5646		 */
5647		goto bad;
5648	}
5649	/* validate SCTP checksum */
5650	SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
5651	    "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
5652	    m->m_pkthdr.len,
5653	    if_name(m->m_pkthdr.rcvif),
5654	    m->m_pkthdr.csum_flags);
5655	if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
5656		SCTP_STAT_INCR(sctps_recvhwcrc);
5657		goto sctp_skip_csum_4;
5658	}
5659	check = sh->checksum;	/* save incoming checksum */
5660	if ((check == 0) && (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback)) &&
5661	    ((ip->ip_src.s_addr == ip->ip_dst.s_addr) ||
5662	    (SCTP_IS_IT_LOOPBACK(m)))
5663	    ) {
5664		SCTP_STAT_INCR(sctps_recvnocrc);
5665		goto sctp_skip_csum_4;
5666	}
5667	sh->checksum = 0;	/* prepare for calc */
5668	calc_check = sctp_calculate_cksum(m, iphlen);
5669	SCTP_STAT_INCR(sctps_recvswcrc);
5670	if (calc_check != check) {
5671		SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x  m:%p mlen:%d iphlen:%d\n",
5672		    calc_check, check, m, mlen, iphlen);
5673
5674		stcb = sctp_findassociation_addr(m, iphlen,
5675		    offset - sizeof(*ch),
5676		    sh, ch, &inp, &net,
5677		    vrf_id);
5678		if ((net) && (port)) {
5679			if (net->port == 0) {
5680				sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr));
5681			}
5682			net->port = port;
5683		}
5684		if ((inp) && (stcb)) {
5685			sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
5686			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5687		} else if ((inp != NULL) && (stcb == NULL)) {
5688			refcount_up = 1;
5689		}
5690		SCTP_STAT_INCR(sctps_badsum);
5691		SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5692		goto bad;
5693	}
5694	sh->checksum = calc_check;
5695sctp_skip_csum_4:
5696	/* destination port of 0 is illegal, based on RFC2960. */
5697	if (sh->dest_port == 0) {
5698		SCTP_STAT_INCR(sctps_hdrops);
5699		goto bad;
5700	}
5701	/*
5702	 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants
5703	 * IP/SCTP/first chunk header...
5704	 */
5705	stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
5706	    sh, ch, &inp, &net, vrf_id);
5707	if ((net) && (port)) {
5708		if (net->port == 0) {
5709			sctp_pathmtu_adjustment(inp, stcb, net, net->mtu - sizeof(struct udphdr));
5710		}
5711		net->port = port;
5712	}
5713	/* inp's ref-count increased && stcb locked */
5714	if (inp == NULL) {
5715		struct sctp_init_chunk *init_chk, chunk_buf;
5716
5717		SCTP_STAT_INCR(sctps_noport);
5718#ifdef ICMP_BANDLIM
5719		/*
5720		 * we use the bandwidth limiting to protect against sending
5721		 * too many ABORTS all at once. In this case these count the
5722		 * same as an ICMP message.
5723		 */
5724		if (badport_bandlim(0) < 0)
5725			goto bad;
5726#endif				/* ICMP_BANDLIM */
5727		SCTPDBG(SCTP_DEBUG_INPUT1,
5728		    "Sending a ABORT from packet entry!\n");
5729		if (ch->chunk_type == SCTP_INITIATION) {
5730			/*
5731			 * we do a trick here to get the INIT tag, dig in
5732			 * and get the tag from the INIT and put it in the
5733			 * common header.
5734			 */
5735			init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
5736			    iphlen + sizeof(*sh), sizeof(*init_chk),
5737			    (uint8_t *) & chunk_buf);
5738			if (init_chk != NULL)
5739				sh->v_tag = init_chk->init.initiate_tag;
5740		}
5741		if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5742			sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, port);
5743			goto bad;
5744		}
5745		if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5746			goto bad;
5747		}
5748		if (ch->chunk_type != SCTP_ABORT_ASSOCIATION)
5749			sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port);
5750		goto bad;
5751	} else if (stcb == NULL) {
5752		refcount_up = 1;
5753	}
5754#ifdef IPSEC
5755	/*
5756	 * I very much doubt any of the IPSEC stuff will work but I have no
5757	 * idea, so I will leave it in place.
5758	 */
5759	if (inp && ipsec4_in_reject(m, &inp->ip_inp.inp)) {
5760		MODULE_GLOBAL(MOD_IPSEC, ipsec4stat).in_polvio++;
5761		SCTP_STAT_INCR(sctps_hdrops);
5762		goto bad;
5763	}
5764#endif				/* IPSEC */
5765
5766	/*
5767	 * common chunk processing
5768	 */
5769	length = ip->ip_len + iphlen;
5770	offset -= sizeof(struct sctp_chunkhdr);
5771
5772	ecn_bits = ip->ip_tos;
5773
5774	/* sa_ignore NO_NULL_CHK */
5775	sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
5776	    inp, stcb, net, ecn_bits, vrf_id, port);
5777	/* inp's ref-count reduced && stcb unlocked */
5778	if (m) {
5779		sctp_m_freem(m);
5780	}
5781	if ((inp) && (refcount_up)) {
5782		/* reduce ref-count */
5783		SCTP_INP_DECR_REF(inp);
5784	}
5785	return;
5786bad:
5787	if (stcb) {
5788		SCTP_TCB_UNLOCK(stcb);
5789	}
5790	if ((inp) && (refcount_up)) {
5791		/* reduce ref-count */
5792		SCTP_INP_DECR_REF(inp);
5793	}
5794	if (m) {
5795		sctp_m_freem(m);
5796	}
5797	return;
5798}
5799void
5800sctp_input(i_pak, off)
5801	struct mbuf *i_pak;
5802	int off;
5803{
5804	sctp_input_with_port(i_pak, off, 0);
5805}
5806