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