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