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