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