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