xref: /freebsd/sys/netinet/sctputil.c (revision efd5e69291940ae00eb5aaeac839926e78abb6ed)
1f8829a4aSRandall Stewart /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4830d754dSRandall Stewart  * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
5807aad63SMichael Tuexen  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6807aad63SMichael Tuexen  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7f8829a4aSRandall Stewart  *
8f8829a4aSRandall Stewart  * Redistribution and use in source and binary forms, with or without
9f8829a4aSRandall Stewart  * modification, are permitted provided that the following conditions are met:
10f8829a4aSRandall Stewart  *
11f8829a4aSRandall Stewart  * a) Redistributions of source code must retain the above copyright notice,
12f8829a4aSRandall Stewart  *    this list of conditions and the following disclaimer.
13f8829a4aSRandall Stewart  *
14f8829a4aSRandall Stewart  * b) Redistributions in binary form must reproduce the above copyright
15f8829a4aSRandall Stewart  *    notice, this list of conditions and the following disclaimer in
16f8829a4aSRandall Stewart  *    the documentation and/or other materials provided with the distribution.
17f8829a4aSRandall Stewart  *
18f8829a4aSRandall Stewart  * c) Neither the name of Cisco Systems, Inc. nor the names of its
19f8829a4aSRandall Stewart  *    contributors may be used to endorse or promote products derived
20f8829a4aSRandall Stewart  *    from this software without specific prior written permission.
21f8829a4aSRandall Stewart  *
22f8829a4aSRandall Stewart  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23f8829a4aSRandall Stewart  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24f8829a4aSRandall Stewart  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25f8829a4aSRandall Stewart  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26f8829a4aSRandall Stewart  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27f8829a4aSRandall Stewart  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28f8829a4aSRandall Stewart  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29f8829a4aSRandall Stewart  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30f8829a4aSRandall Stewart  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31f8829a4aSRandall Stewart  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32f8829a4aSRandall Stewart  * THE POSSIBILITY OF SUCH DAMAGE.
33f8829a4aSRandall Stewart  */
34f8829a4aSRandall Stewart 
35f8829a4aSRandall Stewart #include <sys/cdefs.h>
36f8829a4aSRandall Stewart __FBSDID("$FreeBSD$");
37f8829a4aSRandall Stewart 
38f8829a4aSRandall Stewart #include <netinet/sctp_os.h>
39f8829a4aSRandall Stewart #include <netinet/sctp_pcb.h>
40f8829a4aSRandall Stewart #include <netinet/sctputil.h>
41f8829a4aSRandall Stewart #include <netinet/sctp_var.h>
4242551e99SRandall Stewart #include <netinet/sctp_sysctl.h>
43f8829a4aSRandall Stewart #ifdef INET6
443a51a264SMichael Tuexen #include <netinet6/sctp6_var.h>
45f8829a4aSRandall Stewart #endif
46f8829a4aSRandall Stewart #include <netinet/sctp_header.h>
47f8829a4aSRandall Stewart #include <netinet/sctp_output.h>
48f8829a4aSRandall Stewart #include <netinet/sctp_uio.h>
49f8829a4aSRandall Stewart #include <netinet/sctp_timer.h>
5046bf534cSMichael Tuexen #include <netinet/sctp_indata.h>
51f8829a4aSRandall Stewart #include <netinet/sctp_auth.h>
52f8829a4aSRandall Stewart #include <netinet/sctp_asconf.h>
53f7517433SRandall Stewart #include <netinet/sctp_bsd_addr.h>
54776cd558SMichael Tuexen #include <netinet/sctp_kdtrace.h>
5510e0318aSMichael Tuexen #if defined(INET6) || defined(INET)
5610e0318aSMichael Tuexen #include <netinet/tcp_var.h>
5710e0318aSMichael Tuexen #endif
583a51a264SMichael Tuexen #include <netinet/udp.h>
593a51a264SMichael Tuexen #include <netinet/udp_var.h>
603a51a264SMichael Tuexen #include <sys/proc.h>
61fd7af143SMichael Tuexen #ifdef INET6
62fd7af143SMichael Tuexen #include <netinet/icmp6.h>
63fd7af143SMichael Tuexen #endif
64f8829a4aSRandall Stewart 
65f8829a4aSRandall Stewart 
66b9e7085aSRandall Stewart #ifndef KTR_SCTP
67b9e7085aSRandall Stewart #define KTR_SCTP KTR_SUBSYS
6880fefe0aSRandall Stewart #endif
69f8829a4aSRandall Stewart 
70ed654363SMichael Tuexen extern const struct sctp_cc_functions sctp_cc_functions[];
71ed654363SMichael Tuexen extern const struct sctp_ss_functions sctp_ss_functions[];
720e9a9c10SMichael Tuexen 
73f8829a4aSRandall Stewart void
74dcb68fbaSMichael Tuexen sctp_sblog(struct sockbuf *sb, struct sctp_tcb *stcb, int from, int incr)
75f8829a4aSRandall Stewart {
76c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
77c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
78f8829a4aSRandall Stewart 
7980fefe0aSRandall Stewart 	sctp_clog.x.sb.stcb = stcb;
804e88d37aSMichael Tuexen 	sctp_clog.x.sb.so_sbcc = sb->sb_cc;
81f8829a4aSRandall Stewart 	if (stcb)
824e88d37aSMichael Tuexen 		sctp_clog.x.sb.stcb_sbcc = stcb->asoc.sb_cc;
83f8829a4aSRandall Stewart 	else
8480fefe0aSRandall Stewart 		sctp_clog.x.sb.stcb_sbcc = 0;
8580fefe0aSRandall Stewart 	sctp_clog.x.sb.incr = incr;
86c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
8780fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_SB,
8880fefe0aSRandall Stewart 	    from,
8980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
9080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
9180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
9280fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
93c692df45SMichael Tuexen #endif
94f8829a4aSRandall Stewart }
95f8829a4aSRandall Stewart 
96f8829a4aSRandall Stewart void
97f8829a4aSRandall Stewart sctp_log_closing(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int16_t loc)
98f8829a4aSRandall Stewart {
99c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
100c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
101f8829a4aSRandall Stewart 
10280fefe0aSRandall Stewart 	sctp_clog.x.close.inp = (void *)inp;
10380fefe0aSRandall Stewart 	sctp_clog.x.close.sctp_flags = inp->sctp_flags;
104f8829a4aSRandall Stewart 	if (stcb) {
10580fefe0aSRandall Stewart 		sctp_clog.x.close.stcb = (void *)stcb;
10680fefe0aSRandall Stewart 		sctp_clog.x.close.state = (uint16_t)stcb->asoc.state;
107f8829a4aSRandall Stewart 	} else {
10880fefe0aSRandall Stewart 		sctp_clog.x.close.stcb = 0;
10980fefe0aSRandall Stewart 		sctp_clog.x.close.state = 0;
110f8829a4aSRandall Stewart 	}
11180fefe0aSRandall Stewart 	sctp_clog.x.close.loc = loc;
112c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
11380fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_CLOSE,
11480fefe0aSRandall Stewart 	    0,
11580fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
11680fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
11780fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
11880fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
119c692df45SMichael Tuexen #endif
120f8829a4aSRandall Stewart }
121f8829a4aSRandall Stewart 
122f8829a4aSRandall Stewart void
123f8829a4aSRandall Stewart rto_logging(struct sctp_nets *net, int from)
124f8829a4aSRandall Stewart {
125c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
126c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
127f8829a4aSRandall Stewart 
128bfefd190SRandall Stewart 	memset(&sctp_clog, 0, sizeof(sctp_clog));
12980fefe0aSRandall Stewart 	sctp_clog.x.rto.net = (void *)net;
130be1d9176SMichael Tuexen 	sctp_clog.x.rto.rtt = net->rtt / 1000;
131c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
13280fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_RTT,
13380fefe0aSRandall Stewart 	    from,
13480fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
13580fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
13680fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
13780fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
138c692df45SMichael Tuexen #endif
139f8829a4aSRandall Stewart }
140f8829a4aSRandall Stewart 
141f8829a4aSRandall Stewart void
1426a91f103SRandall Stewart sctp_log_strm_del_alt(struct sctp_tcb *stcb, uint32_t tsn, uint16_t sseq, uint16_t stream, int from)
143f8829a4aSRandall Stewart {
144c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
145c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
146f8829a4aSRandall Stewart 
14780fefe0aSRandall Stewart 	sctp_clog.x.strlog.stcb = stcb;
14880fefe0aSRandall Stewart 	sctp_clog.x.strlog.n_tsn = tsn;
14980fefe0aSRandall Stewart 	sctp_clog.x.strlog.n_sseq = sseq;
15080fefe0aSRandall Stewart 	sctp_clog.x.strlog.e_tsn = 0;
15180fefe0aSRandall Stewart 	sctp_clog.x.strlog.e_sseq = 0;
15280fefe0aSRandall Stewart 	sctp_clog.x.strlog.strm = stream;
153c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
15480fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_STRM,
15580fefe0aSRandall Stewart 	    from,
15680fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
15780fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
15880fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
15980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
160c692df45SMichael Tuexen #endif
161f8829a4aSRandall Stewart }
162f8829a4aSRandall Stewart 
163f8829a4aSRandall Stewart void
164f8829a4aSRandall Stewart sctp_log_nagle_event(struct sctp_tcb *stcb, int action)
165f8829a4aSRandall Stewart {
166c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
167c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
168f8829a4aSRandall Stewart 
16980fefe0aSRandall Stewart 	sctp_clog.x.nagle.stcb = (void *)stcb;
17080fefe0aSRandall Stewart 	sctp_clog.x.nagle.total_flight = stcb->asoc.total_flight;
17180fefe0aSRandall Stewart 	sctp_clog.x.nagle.total_in_queue = stcb->asoc.total_output_queue_size;
17280fefe0aSRandall Stewart 	sctp_clog.x.nagle.count_in_queue = stcb->asoc.chunks_on_out_queue;
17380fefe0aSRandall Stewart 	sctp_clog.x.nagle.count_in_flight = stcb->asoc.total_flight_count;
174c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
17580fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_NAGLE,
17680fefe0aSRandall Stewart 	    action,
17780fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
17880fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
17980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
18080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
181c692df45SMichael Tuexen #endif
182f8829a4aSRandall Stewart }
183f8829a4aSRandall Stewart 
184f8829a4aSRandall Stewart void
185f8829a4aSRandall Stewart sctp_log_sack(uint32_t old_cumack, uint32_t cumack, uint32_t tsn, uint16_t gaps, uint16_t dups, int from)
186f8829a4aSRandall Stewart {
187c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
188c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
189f8829a4aSRandall Stewart 
19080fefe0aSRandall Stewart 	sctp_clog.x.sack.cumack = cumack;
19180fefe0aSRandall Stewart 	sctp_clog.x.sack.oldcumack = old_cumack;
19280fefe0aSRandall Stewart 	sctp_clog.x.sack.tsn = tsn;
19380fefe0aSRandall Stewart 	sctp_clog.x.sack.numGaps = gaps;
19480fefe0aSRandall Stewart 	sctp_clog.x.sack.numDups = dups;
195c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
19680fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_SACK,
19780fefe0aSRandall Stewart 	    from,
19880fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
19980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
20080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
20180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
202c692df45SMichael Tuexen #endif
203f8829a4aSRandall Stewart }
204f8829a4aSRandall Stewart 
205f8829a4aSRandall Stewart void
206f8829a4aSRandall Stewart sctp_log_map(uint32_t map, uint32_t cum, uint32_t high, int from)
207f8829a4aSRandall Stewart {
208c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
209c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
210f8829a4aSRandall Stewart 
211bfefd190SRandall Stewart 	memset(&sctp_clog, 0, sizeof(sctp_clog));
21280fefe0aSRandall Stewart 	sctp_clog.x.map.base = map;
21380fefe0aSRandall Stewart 	sctp_clog.x.map.cum = cum;
21480fefe0aSRandall Stewart 	sctp_clog.x.map.high = high;
215c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
21680fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_MAP,
21780fefe0aSRandall Stewart 	    from,
21880fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
21980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
22080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
22180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
222c692df45SMichael Tuexen #endif
223f8829a4aSRandall Stewart }
224f8829a4aSRandall Stewart 
225f8829a4aSRandall Stewart void
226dcb68fbaSMichael Tuexen sctp_log_fr(uint32_t biggest_tsn, uint32_t biggest_new_tsn, uint32_t tsn, int from)
227f8829a4aSRandall Stewart {
228c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
229c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
230f8829a4aSRandall Stewart 
231bfefd190SRandall Stewart 	memset(&sctp_clog, 0, sizeof(sctp_clog));
23280fefe0aSRandall Stewart 	sctp_clog.x.fr.largest_tsn = biggest_tsn;
23380fefe0aSRandall Stewart 	sctp_clog.x.fr.largest_new_tsn = biggest_new_tsn;
23480fefe0aSRandall Stewart 	sctp_clog.x.fr.tsn = tsn;
235c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
23680fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_FR,
23780fefe0aSRandall Stewart 	    from,
23880fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
23980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
24080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
24180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
242c692df45SMichael Tuexen #endif
243f8829a4aSRandall Stewart }
244f8829a4aSRandall Stewart 
2454be807c4SMichael Tuexen #ifdef SCTP_MBUF_LOGGING
246f8829a4aSRandall Stewart void
247f8829a4aSRandall Stewart sctp_log_mb(struct mbuf *m, int from)
248f8829a4aSRandall Stewart {
249c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
250c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
251f8829a4aSRandall Stewart 
25280fefe0aSRandall Stewart 	sctp_clog.x.mb.mp = m;
25380fefe0aSRandall Stewart 	sctp_clog.x.mb.mbuf_flags = (uint8_t)(SCTP_BUF_GET_FLAGS(m));
25480fefe0aSRandall Stewart 	sctp_clog.x.mb.size = (uint16_t)(SCTP_BUF_LEN(m));
25580fefe0aSRandall Stewart 	sctp_clog.x.mb.data = SCTP_BUF_AT(m, 0);
256139bc87fSRandall Stewart 	if (SCTP_BUF_IS_EXTENDED(m)) {
25780fefe0aSRandall Stewart 		sctp_clog.x.mb.ext = SCTP_BUF_EXTEND_BASE(m);
25880fefe0aSRandall Stewart 		sctp_clog.x.mb.refcnt = (uint8_t)(SCTP_BUF_EXTEND_REFCNT(m));
259f8829a4aSRandall Stewart 	} else {
26080fefe0aSRandall Stewart 		sctp_clog.x.mb.ext = 0;
26180fefe0aSRandall Stewart 		sctp_clog.x.mb.refcnt = 0;
262f8829a4aSRandall Stewart 	}
263c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
26480fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_MBUF,
26580fefe0aSRandall Stewart 	    from,
26680fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
26780fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
26880fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
26980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
270c692df45SMichael Tuexen #endif
271f8829a4aSRandall Stewart }
272f8829a4aSRandall Stewart 
273f8829a4aSRandall Stewart void
2744be807c4SMichael Tuexen sctp_log_mbc(struct mbuf *m, int from)
2754be807c4SMichael Tuexen {
2764be807c4SMichael Tuexen 	struct mbuf *mat;
2774be807c4SMichael Tuexen 
2784be807c4SMichael Tuexen 	for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
2794be807c4SMichael Tuexen 		sctp_log_mb(mat, from);
2804be807c4SMichael Tuexen 	}
2814be807c4SMichael Tuexen }
2824be807c4SMichael Tuexen #endif
2834be807c4SMichael Tuexen 
2844be807c4SMichael Tuexen void
285dcb68fbaSMichael Tuexen sctp_log_strm_del(struct sctp_queued_to_read *control, struct sctp_queued_to_read *poschk, int from)
286f8829a4aSRandall Stewart {
287c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
288c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
289f8829a4aSRandall Stewart 
290f8829a4aSRandall Stewart 	if (control == NULL) {
291ad81507eSRandall Stewart 		SCTP_PRINTF("Gak log of NULL?\n");
292f8829a4aSRandall Stewart 		return;
293f8829a4aSRandall Stewart 	}
29480fefe0aSRandall Stewart 	sctp_clog.x.strlog.stcb = control->stcb;
29580fefe0aSRandall Stewart 	sctp_clog.x.strlog.n_tsn = control->sinfo_tsn;
29649656eefSMichael Tuexen 	sctp_clog.x.strlog.n_sseq = (uint16_t)control->mid;
29780fefe0aSRandall Stewart 	sctp_clog.x.strlog.strm = control->sinfo_stream;
298f8829a4aSRandall Stewart 	if (poschk != NULL) {
29980fefe0aSRandall Stewart 		sctp_clog.x.strlog.e_tsn = poschk->sinfo_tsn;
30049656eefSMichael Tuexen 		sctp_clog.x.strlog.e_sseq = (uint16_t)poschk->mid;
301f8829a4aSRandall Stewart 	} else {
30280fefe0aSRandall Stewart 		sctp_clog.x.strlog.e_tsn = 0;
30380fefe0aSRandall Stewart 		sctp_clog.x.strlog.e_sseq = 0;
304f8829a4aSRandall Stewart 	}
305c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
30680fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_STRM,
30780fefe0aSRandall Stewart 	    from,
30880fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
30980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
31080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
31180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
312c692df45SMichael Tuexen #endif
313f8829a4aSRandall Stewart }
314f8829a4aSRandall Stewart 
315f8829a4aSRandall Stewart void
316f8829a4aSRandall Stewart sctp_log_cwnd(struct sctp_tcb *stcb, struct sctp_nets *net, int augment, uint8_t from)
317f8829a4aSRandall Stewart {
318c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
319c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
320f8829a4aSRandall Stewart 
32180fefe0aSRandall Stewart 	sctp_clog.x.cwnd.net = net;
322f8829a4aSRandall Stewart 	if (stcb->asoc.send_queue_cnt > 255)
32380fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_send = 255;
324f8829a4aSRandall Stewart 	else
32580fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
326f8829a4aSRandall Stewart 	if (stcb->asoc.stream_queue_cnt > 255)
32780fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_str = 255;
328f8829a4aSRandall Stewart 	else
32980fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
330f8829a4aSRandall Stewart 
331f8829a4aSRandall Stewart 	if (net) {
33280fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cwnd_new_value = net->cwnd;
33380fefe0aSRandall Stewart 		sctp_clog.x.cwnd.inflight = net->flight_size;
33480fefe0aSRandall Stewart 		sctp_clog.x.cwnd.pseudo_cumack = net->pseudo_cumack;
33580fefe0aSRandall Stewart 		sctp_clog.x.cwnd.meets_pseudo_cumack = net->new_pseudo_cumack;
33680fefe0aSRandall Stewart 		sctp_clog.x.cwnd.need_new_pseudo_cumack = net->find_pseudo_cumack;
337f8829a4aSRandall Stewart 	}
338f8829a4aSRandall Stewart 	if (SCTP_CWNDLOG_PRESEND == from) {
33980fefe0aSRandall Stewart 		sctp_clog.x.cwnd.meets_pseudo_cumack = stcb->asoc.peers_rwnd;
340f8829a4aSRandall Stewart 	}
34180fefe0aSRandall Stewart 	sctp_clog.x.cwnd.cwnd_augment = augment;
342c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
34380fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_CWND,
34480fefe0aSRandall Stewart 	    from,
34580fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
34680fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
34780fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
34880fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
349c692df45SMichael Tuexen #endif
350f8829a4aSRandall Stewart }
351f8829a4aSRandall Stewart 
352f8829a4aSRandall Stewart void
353f8829a4aSRandall Stewart sctp_log_lock(struct sctp_inpcb *inp, struct sctp_tcb *stcb, uint8_t from)
354f8829a4aSRandall Stewart {
355c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
356c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
357f8829a4aSRandall Stewart 
358bfefd190SRandall Stewart 	memset(&sctp_clog, 0, sizeof(sctp_clog));
35903b0b021SRandall Stewart 	if (inp) {
36080fefe0aSRandall Stewart 		sctp_clog.x.lock.sock = (void *)inp->sctp_socket;
36103b0b021SRandall Stewart 
36203b0b021SRandall Stewart 	} else {
36380fefe0aSRandall Stewart 		sctp_clog.x.lock.sock = (void *)NULL;
36403b0b021SRandall Stewart 	}
36580fefe0aSRandall Stewart 	sctp_clog.x.lock.inp = (void *)inp;
366f8829a4aSRandall Stewart 	if (stcb) {
36780fefe0aSRandall Stewart 		sctp_clog.x.lock.tcb_lock = mtx_owned(&stcb->tcb_mtx);
368f8829a4aSRandall Stewart 	} else {
36980fefe0aSRandall Stewart 		sctp_clog.x.lock.tcb_lock = SCTP_LOCK_UNKNOWN;
370f8829a4aSRandall Stewart 	}
371f8829a4aSRandall Stewart 	if (inp) {
37280fefe0aSRandall Stewart 		sctp_clog.x.lock.inp_lock = mtx_owned(&inp->inp_mtx);
37380fefe0aSRandall Stewart 		sctp_clog.x.lock.create_lock = mtx_owned(&inp->inp_create_mtx);
374f8829a4aSRandall Stewart 	} else {
37580fefe0aSRandall Stewart 		sctp_clog.x.lock.inp_lock = SCTP_LOCK_UNKNOWN;
37680fefe0aSRandall Stewart 		sctp_clog.x.lock.create_lock = SCTP_LOCK_UNKNOWN;
377f8829a4aSRandall Stewart 	}
378b3f1ea41SRandall Stewart 	sctp_clog.x.lock.info_lock = rw_wowned(&SCTP_BASE_INFO(ipi_ep_mtx));
37952129fcdSRandall Stewart 	if (inp && (inp->sctp_socket)) {
38080fefe0aSRandall Stewart 		sctp_clog.x.lock.sock_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
38180fefe0aSRandall Stewart 		sctp_clog.x.lock.sockrcvbuf_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
38280fefe0aSRandall Stewart 		sctp_clog.x.lock.socksndbuf_lock = mtx_owned(&(inp->sctp_socket->so_snd.sb_mtx));
383f8829a4aSRandall Stewart 	} else {
38480fefe0aSRandall Stewart 		sctp_clog.x.lock.sock_lock = SCTP_LOCK_UNKNOWN;
38580fefe0aSRandall Stewart 		sctp_clog.x.lock.sockrcvbuf_lock = SCTP_LOCK_UNKNOWN;
38680fefe0aSRandall Stewart 		sctp_clog.x.lock.socksndbuf_lock = SCTP_LOCK_UNKNOWN;
387f8829a4aSRandall Stewart 	}
388c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
38980fefe0aSRandall Stewart 	    SCTP_LOG_LOCK_EVENT,
39080fefe0aSRandall Stewart 	    from,
39180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
39280fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
39380fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
39480fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
395c692df45SMichael Tuexen #endif
396f8829a4aSRandall Stewart }
397f8829a4aSRandall Stewart 
398f8829a4aSRandall Stewart void
399f8829a4aSRandall Stewart sctp_log_maxburst(struct sctp_tcb *stcb, struct sctp_nets *net, int error, int burst, uint8_t from)
400f8829a4aSRandall Stewart {
401c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
402c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
403f8829a4aSRandall Stewart 
404bfefd190SRandall Stewart 	memset(&sctp_clog, 0, sizeof(sctp_clog));
40580fefe0aSRandall Stewart 	sctp_clog.x.cwnd.net = net;
40680fefe0aSRandall Stewart 	sctp_clog.x.cwnd.cwnd_new_value = error;
40780fefe0aSRandall Stewart 	sctp_clog.x.cwnd.inflight = net->flight_size;
40880fefe0aSRandall Stewart 	sctp_clog.x.cwnd.cwnd_augment = burst;
409f8829a4aSRandall Stewart 	if (stcb->asoc.send_queue_cnt > 255)
41080fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_send = 255;
411f8829a4aSRandall Stewart 	else
41280fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
413f8829a4aSRandall Stewart 	if (stcb->asoc.stream_queue_cnt > 255)
41480fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_str = 255;
415f8829a4aSRandall Stewart 	else
41680fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
417c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
41880fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_MAXBURST,
41980fefe0aSRandall Stewart 	    from,
42080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
42180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
42280fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
42380fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
424c692df45SMichael Tuexen #endif
425f8829a4aSRandall Stewart }
426f8829a4aSRandall Stewart 
427f8829a4aSRandall Stewart void
428f8829a4aSRandall Stewart sctp_log_rwnd(uint8_t from, uint32_t peers_rwnd, uint32_t snd_size, uint32_t overhead)
429f8829a4aSRandall Stewart {
430c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
431c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
432f8829a4aSRandall Stewart 
43380fefe0aSRandall Stewart 	sctp_clog.x.rwnd.rwnd = peers_rwnd;
43480fefe0aSRandall Stewart 	sctp_clog.x.rwnd.send_size = snd_size;
43580fefe0aSRandall Stewart 	sctp_clog.x.rwnd.overhead = overhead;
43680fefe0aSRandall Stewart 	sctp_clog.x.rwnd.new_rwnd = 0;
437c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
43880fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_RWND,
43980fefe0aSRandall Stewart 	    from,
44080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
44180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
44280fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
44380fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
444c692df45SMichael Tuexen #endif
445f8829a4aSRandall Stewart }
446f8829a4aSRandall Stewart 
447f8829a4aSRandall Stewart void
448f8829a4aSRandall Stewart sctp_log_rwnd_set(uint8_t from, uint32_t peers_rwnd, uint32_t flight_size, uint32_t overhead, uint32_t a_rwndval)
449f8829a4aSRandall Stewart {
450c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
451c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
452f8829a4aSRandall Stewart 
45380fefe0aSRandall Stewart 	sctp_clog.x.rwnd.rwnd = peers_rwnd;
45480fefe0aSRandall Stewart 	sctp_clog.x.rwnd.send_size = flight_size;
45580fefe0aSRandall Stewart 	sctp_clog.x.rwnd.overhead = overhead;
45680fefe0aSRandall Stewart 	sctp_clog.x.rwnd.new_rwnd = a_rwndval;
457c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
45880fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_RWND,
45980fefe0aSRandall Stewart 	    from,
46080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
46180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
46280fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
46380fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
464c692df45SMichael Tuexen #endif
465f8829a4aSRandall Stewart }
466f8829a4aSRandall Stewart 
4674be807c4SMichael Tuexen #ifdef SCTP_MBCNT_LOGGING
4684be807c4SMichael Tuexen static void
469f8829a4aSRandall Stewart sctp_log_mbcnt(uint8_t from, uint32_t total_oq, uint32_t book, uint32_t total_mbcnt_q, uint32_t mbcnt)
470f8829a4aSRandall Stewart {
471c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
472c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
473f8829a4aSRandall Stewart 
47480fefe0aSRandall Stewart 	sctp_clog.x.mbcnt.total_queue_size = total_oq;
47580fefe0aSRandall Stewart 	sctp_clog.x.mbcnt.size_change = book;
47680fefe0aSRandall Stewart 	sctp_clog.x.mbcnt.total_queue_mb_size = total_mbcnt_q;
47780fefe0aSRandall Stewart 	sctp_clog.x.mbcnt.mbcnt_change = mbcnt;
478c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
47980fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_MBCNT,
48080fefe0aSRandall Stewart 	    from,
48180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
48280fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
48380fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
48480fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
485c692df45SMichael Tuexen #endif
486f8829a4aSRandall Stewart }
4874be807c4SMichael Tuexen #endif
4884be807c4SMichael Tuexen 
489f8829a4aSRandall Stewart void
490f8829a4aSRandall Stewart sctp_misc_ints(uint8_t from, uint32_t a, uint32_t b, uint32_t c, uint32_t d)
491f8829a4aSRandall Stewart {
492c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
493c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
49480fefe0aSRandall Stewart 	    SCTP_LOG_MISC_EVENT,
49580fefe0aSRandall Stewart 	    from,
49680fefe0aSRandall Stewart 	    a, b, c, d);
497c692df45SMichael Tuexen #endif
498f8829a4aSRandall Stewart }
499f8829a4aSRandall Stewart 
500f8829a4aSRandall Stewart void
5017215cc1bSMichael Tuexen sctp_wakeup_log(struct sctp_tcb *stcb, uint32_t wake_cnt, int from)
502f8829a4aSRandall Stewart {
503c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
504c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
505f8829a4aSRandall Stewart 
50680fefe0aSRandall Stewart 	sctp_clog.x.wake.stcb = (void *)stcb;
50780fefe0aSRandall Stewart 	sctp_clog.x.wake.wake_cnt = wake_cnt;
50880fefe0aSRandall Stewart 	sctp_clog.x.wake.flight = stcb->asoc.total_flight_count;
50980fefe0aSRandall Stewart 	sctp_clog.x.wake.send_q = stcb->asoc.send_queue_cnt;
51080fefe0aSRandall Stewart 	sctp_clog.x.wake.sent_q = stcb->asoc.sent_queue_cnt;
511f8829a4aSRandall Stewart 
512f8829a4aSRandall Stewart 	if (stcb->asoc.stream_queue_cnt < 0xff)
51380fefe0aSRandall Stewart 		sctp_clog.x.wake.stream_qcnt = (uint8_t)stcb->asoc.stream_queue_cnt;
514f8829a4aSRandall Stewart 	else
51580fefe0aSRandall Stewart 		sctp_clog.x.wake.stream_qcnt = 0xff;
516f8829a4aSRandall Stewart 
517f8829a4aSRandall Stewart 	if (stcb->asoc.chunks_on_out_queue < 0xff)
51880fefe0aSRandall Stewart 		sctp_clog.x.wake.chunks_on_oque = (uint8_t)stcb->asoc.chunks_on_out_queue;
519f8829a4aSRandall Stewart 	else
52080fefe0aSRandall Stewart 		sctp_clog.x.wake.chunks_on_oque = 0xff;
521f8829a4aSRandall Stewart 
52280fefe0aSRandall Stewart 	sctp_clog.x.wake.sctpflags = 0;
523f8829a4aSRandall Stewart 	/* set in the defered mode stuff */
524f8829a4aSRandall Stewart 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE)
52580fefe0aSRandall Stewart 		sctp_clog.x.wake.sctpflags |= 1;
526f8829a4aSRandall Stewart 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT)
52780fefe0aSRandall Stewart 		sctp_clog.x.wake.sctpflags |= 2;
528f8829a4aSRandall Stewart 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT)
52980fefe0aSRandall Stewart 		sctp_clog.x.wake.sctpflags |= 4;
530f8829a4aSRandall Stewart 	/* what about the sb */
531f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
532f8829a4aSRandall Stewart 		struct socket *so = stcb->sctp_socket;
533f8829a4aSRandall Stewart 
53480fefe0aSRandall Stewart 		sctp_clog.x.wake.sbflags = (uint8_t)((so->so_snd.sb_flags & 0x00ff));
535f8829a4aSRandall Stewart 	} else {
53680fefe0aSRandall Stewart 		sctp_clog.x.wake.sbflags = 0xff;
537f8829a4aSRandall Stewart 	}
538c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
53980fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_WAKE,
54080fefe0aSRandall Stewart 	    from,
54180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
54280fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
54380fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
54480fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
545c692df45SMichael Tuexen #endif
546f8829a4aSRandall Stewart }
547f8829a4aSRandall Stewart 
548f8829a4aSRandall Stewart void
54958e6eeefSMichael Tuexen sctp_log_block(uint8_t from, struct sctp_association *asoc, ssize_t sendlen)
550f8829a4aSRandall Stewart {
551c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
552c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
553f8829a4aSRandall Stewart 
55480fefe0aSRandall Stewart 	sctp_clog.x.blk.onsb = asoc->total_output_queue_size;
55580fefe0aSRandall Stewart 	sctp_clog.x.blk.send_sent_qcnt = (uint16_t)(asoc->send_queue_cnt + asoc->sent_queue_cnt);
55680fefe0aSRandall Stewart 	sctp_clog.x.blk.peer_rwnd = asoc->peers_rwnd;
55780fefe0aSRandall Stewart 	sctp_clog.x.blk.stream_qcnt = (uint16_t)asoc->stream_queue_cnt;
55880fefe0aSRandall Stewart 	sctp_clog.x.blk.chunks_on_oque = (uint16_t)asoc->chunks_on_out_queue;
55980fefe0aSRandall Stewart 	sctp_clog.x.blk.flight_size = (uint16_t)(asoc->total_flight / 1024);
5609a8e3088SMichael Tuexen 	sctp_clog.x.blk.sndlen = (uint32_t)sendlen;
561c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
56280fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_BLOCK,
56380fefe0aSRandall Stewart 	    from,
56480fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
56580fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
56680fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
56780fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
568c692df45SMichael Tuexen #endif
569f8829a4aSRandall Stewart }
570f8829a4aSRandall Stewart 
571f8829a4aSRandall Stewart int
5727215cc1bSMichael Tuexen sctp_fill_stat_log(void *optval SCTP_UNUSED, size_t *optsize SCTP_UNUSED)
573f8829a4aSRandall Stewart {
57480fefe0aSRandall Stewart 	/* May need to fix this if ktrdump does not work */
575f8829a4aSRandall Stewart 	return (0);
576f8829a4aSRandall Stewart }
577f8829a4aSRandall Stewart 
578f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
579f8829a4aSRandall Stewart uint8_t sctp_audit_data[SCTP_AUDIT_SIZE][2];
580f8829a4aSRandall Stewart static int sctp_audit_indx = 0;
581f8829a4aSRandall Stewart 
582f8829a4aSRandall Stewart static
583f8829a4aSRandall Stewart void
584f8829a4aSRandall Stewart sctp_print_audit_report(void)
585f8829a4aSRandall Stewart {
586f8829a4aSRandall Stewart 	int i;
587f8829a4aSRandall Stewart 	int cnt;
588f8829a4aSRandall Stewart 
589f8829a4aSRandall Stewart 	cnt = 0;
590f8829a4aSRandall Stewart 	for (i = sctp_audit_indx; i < SCTP_AUDIT_SIZE; i++) {
591f8829a4aSRandall Stewart 		if ((sctp_audit_data[i][0] == 0xe0) &&
592f8829a4aSRandall Stewart 		    (sctp_audit_data[i][1] == 0x01)) {
593f8829a4aSRandall Stewart 			cnt = 0;
594ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
595f8829a4aSRandall Stewart 		} else if (sctp_audit_data[i][0] == 0xf0) {
596f8829a4aSRandall Stewart 			cnt = 0;
597ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
598f8829a4aSRandall Stewart 		} else if ((sctp_audit_data[i][0] == 0xc0) &&
599f8829a4aSRandall Stewart 		    (sctp_audit_data[i][1] == 0x01)) {
600ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
601f8829a4aSRandall Stewart 			cnt = 0;
602f8829a4aSRandall Stewart 		}
603ad81507eSRandall Stewart 		SCTP_PRINTF("%2.2x%2.2x ", (uint32_t)sctp_audit_data[i][0],
604f8829a4aSRandall Stewart 		    (uint32_t)sctp_audit_data[i][1]);
605f8829a4aSRandall Stewart 		cnt++;
606f8829a4aSRandall Stewart 		if ((cnt % 14) == 0)
607ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
608f8829a4aSRandall Stewart 	}
609f8829a4aSRandall Stewart 	for (i = 0; i < sctp_audit_indx; i++) {
610f8829a4aSRandall Stewart 		if ((sctp_audit_data[i][0] == 0xe0) &&
611f8829a4aSRandall Stewart 		    (sctp_audit_data[i][1] == 0x01)) {
612f8829a4aSRandall Stewart 			cnt = 0;
613ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
614f8829a4aSRandall Stewart 		} else if (sctp_audit_data[i][0] == 0xf0) {
615f8829a4aSRandall Stewart 			cnt = 0;
616ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
617f8829a4aSRandall Stewart 		} else if ((sctp_audit_data[i][0] == 0xc0) &&
618f8829a4aSRandall Stewart 		    (sctp_audit_data[i][1] == 0x01)) {
619ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
620f8829a4aSRandall Stewart 			cnt = 0;
621f8829a4aSRandall Stewart 		}
622ad81507eSRandall Stewart 		SCTP_PRINTF("%2.2x%2.2x ", (uint32_t)sctp_audit_data[i][0],
623f8829a4aSRandall Stewart 		    (uint32_t)sctp_audit_data[i][1]);
624f8829a4aSRandall Stewart 		cnt++;
625f8829a4aSRandall Stewart 		if ((cnt % 14) == 0)
626ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
627f8829a4aSRandall Stewart 	}
628ad81507eSRandall Stewart 	SCTP_PRINTF("\n");
629f8829a4aSRandall Stewart }
630f8829a4aSRandall Stewart 
631f8829a4aSRandall Stewart void
632f8829a4aSRandall Stewart sctp_auditing(int from, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
633f8829a4aSRandall Stewart     struct sctp_nets *net)
634f8829a4aSRandall Stewart {
635f8829a4aSRandall Stewart 	int resend_cnt, tot_out, rep, tot_book_cnt;
636f8829a4aSRandall Stewart 	struct sctp_nets *lnet;
637f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
638f8829a4aSRandall Stewart 
639f8829a4aSRandall Stewart 	sctp_audit_data[sctp_audit_indx][0] = 0xAA;
640f8829a4aSRandall Stewart 	sctp_audit_data[sctp_audit_indx][1] = 0x000000ff & from;
641f8829a4aSRandall Stewart 	sctp_audit_indx++;
642f8829a4aSRandall Stewart 	if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
643f8829a4aSRandall Stewart 		sctp_audit_indx = 0;
644f8829a4aSRandall Stewart 	}
645f8829a4aSRandall Stewart 	if (inp == NULL) {
646f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xAF;
647f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] = 0x01;
648f8829a4aSRandall Stewart 		sctp_audit_indx++;
649f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
650f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
651f8829a4aSRandall Stewart 		}
652f8829a4aSRandall Stewart 		return;
653f8829a4aSRandall Stewart 	}
654f8829a4aSRandall Stewart 	if (stcb == NULL) {
655f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xAF;
656f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] = 0x02;
657f8829a4aSRandall Stewart 		sctp_audit_indx++;
658f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
659f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
660f8829a4aSRandall Stewart 		}
661f8829a4aSRandall Stewart 		return;
662f8829a4aSRandall Stewart 	}
663f8829a4aSRandall Stewart 	sctp_audit_data[sctp_audit_indx][0] = 0xA1;
664f8829a4aSRandall Stewart 	sctp_audit_data[sctp_audit_indx][1] =
665f8829a4aSRandall Stewart 	    (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
666f8829a4aSRandall Stewart 	sctp_audit_indx++;
667f8829a4aSRandall Stewart 	if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
668f8829a4aSRandall Stewart 		sctp_audit_indx = 0;
669f8829a4aSRandall Stewart 	}
670f8829a4aSRandall Stewart 	rep = 0;
671f8829a4aSRandall Stewart 	tot_book_cnt = 0;
672f8829a4aSRandall Stewart 	resend_cnt = tot_out = 0;
673f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
674f8829a4aSRandall Stewart 		if (chk->sent == SCTP_DATAGRAM_RESEND) {
675f8829a4aSRandall Stewart 			resend_cnt++;
676f8829a4aSRandall Stewart 		} else if (chk->sent < SCTP_DATAGRAM_RESEND) {
677f8829a4aSRandall Stewart 			tot_out += chk->book_size;
678f8829a4aSRandall Stewart 			tot_book_cnt++;
679f8829a4aSRandall Stewart 		}
680f8829a4aSRandall Stewart 	}
681f8829a4aSRandall Stewart 	if (resend_cnt != stcb->asoc.sent_queue_retran_cnt) {
682f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xAF;
683f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] = 0xA1;
684f8829a4aSRandall Stewart 		sctp_audit_indx++;
685f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
686f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
687f8829a4aSRandall Stewart 		}
688ad81507eSRandall Stewart 		SCTP_PRINTF("resend_cnt:%d asoc-tot:%d\n",
689f8829a4aSRandall Stewart 		    resend_cnt, stcb->asoc.sent_queue_retran_cnt);
690f8829a4aSRandall Stewart 		rep = 1;
691f8829a4aSRandall Stewart 		stcb->asoc.sent_queue_retran_cnt = resend_cnt;
692f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xA2;
693f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] =
694f8829a4aSRandall Stewart 		    (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
695f8829a4aSRandall Stewart 		sctp_audit_indx++;
696f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
697f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
698f8829a4aSRandall Stewart 		}
699f8829a4aSRandall Stewart 	}
700f8829a4aSRandall Stewart 	if (tot_out != stcb->asoc.total_flight) {
701f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xAF;
702f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] = 0xA2;
703f8829a4aSRandall Stewart 		sctp_audit_indx++;
704f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
705f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
706f8829a4aSRandall Stewart 		}
707f8829a4aSRandall Stewart 		rep = 1;
708ad81507eSRandall Stewart 		SCTP_PRINTF("tot_flt:%d asoc_tot:%d\n", tot_out,
709f8829a4aSRandall Stewart 		    (int)stcb->asoc.total_flight);
710f8829a4aSRandall Stewart 		stcb->asoc.total_flight = tot_out;
711f8829a4aSRandall Stewart 	}
712f8829a4aSRandall Stewart 	if (tot_book_cnt != stcb->asoc.total_flight_count) {
713f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xAF;
714f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] = 0xA5;
715f8829a4aSRandall Stewart 		sctp_audit_indx++;
716f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
717f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
718f8829a4aSRandall Stewart 		}
719f8829a4aSRandall Stewart 		rep = 1;
720f31e6c7fSMichael Tuexen 		SCTP_PRINTF("tot_flt_book:%d\n", tot_book_cnt);
721f8829a4aSRandall Stewart 
722f8829a4aSRandall Stewart 		stcb->asoc.total_flight_count = tot_book_cnt;
723f8829a4aSRandall Stewart 	}
724f8829a4aSRandall Stewart 	tot_out = 0;
725f8829a4aSRandall Stewart 	TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
726f8829a4aSRandall Stewart 		tot_out += lnet->flight_size;
727f8829a4aSRandall Stewart 	}
728f8829a4aSRandall Stewart 	if (tot_out != stcb->asoc.total_flight) {
729f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xAF;
730f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] = 0xA3;
731f8829a4aSRandall Stewart 		sctp_audit_indx++;
732f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
733f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
734f8829a4aSRandall Stewart 		}
735f8829a4aSRandall Stewart 		rep = 1;
736ad81507eSRandall Stewart 		SCTP_PRINTF("real flight:%d net total was %d\n",
737f8829a4aSRandall Stewart 		    stcb->asoc.total_flight, tot_out);
738f8829a4aSRandall Stewart 		/* now corrective action */
739f8829a4aSRandall Stewart 		TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
740f8829a4aSRandall Stewart 
741f8829a4aSRandall Stewart 			tot_out = 0;
742f8829a4aSRandall Stewart 			TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
743f8829a4aSRandall Stewart 				if ((chk->whoTo == lnet) &&
744f8829a4aSRandall Stewart 				    (chk->sent < SCTP_DATAGRAM_RESEND)) {
745f8829a4aSRandall Stewart 					tot_out += chk->book_size;
746f8829a4aSRandall Stewart 				}
747f8829a4aSRandall Stewart 			}
748f8829a4aSRandall Stewart 			if (lnet->flight_size != tot_out) {
749f31e6c7fSMichael Tuexen 				SCTP_PRINTF("net:%p flight was %d corrected to %d\n",
750dd294dceSMichael Tuexen 				    (void *)lnet, lnet->flight_size,
751ad81507eSRandall Stewart 				    tot_out);
752f8829a4aSRandall Stewart 				lnet->flight_size = tot_out;
753f8829a4aSRandall Stewart 			}
754f8829a4aSRandall Stewart 		}
755f8829a4aSRandall Stewart 	}
756f8829a4aSRandall Stewart 	if (rep) {
757f8829a4aSRandall Stewart 		sctp_print_audit_report();
758f8829a4aSRandall Stewart 	}
759f8829a4aSRandall Stewart }
760f8829a4aSRandall Stewart 
761f8829a4aSRandall Stewart void
762f8829a4aSRandall Stewart sctp_audit_log(uint8_t ev, uint8_t fd)
763f8829a4aSRandall Stewart {
764f8829a4aSRandall Stewart 
765f8829a4aSRandall Stewart 	sctp_audit_data[sctp_audit_indx][0] = ev;
766f8829a4aSRandall Stewart 	sctp_audit_data[sctp_audit_indx][1] = fd;
767f8829a4aSRandall Stewart 	sctp_audit_indx++;
768f8829a4aSRandall Stewart 	if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
769f8829a4aSRandall Stewart 		sctp_audit_indx = 0;
770f8829a4aSRandall Stewart 	}
771f8829a4aSRandall Stewart }
772f8829a4aSRandall Stewart 
773f8829a4aSRandall Stewart #endif
774f8829a4aSRandall Stewart 
775f8829a4aSRandall Stewart /*
77625ec3553SMichael Tuexen  * The conversion from time to ticks and vice versa is done by rounding
77725ec3553SMichael Tuexen  * upwards. This way we can test in the code the time to be positive and
77825ec3553SMichael Tuexen  * know that this corresponds to a positive number of ticks.
77925ec3553SMichael Tuexen  */
78025ec3553SMichael Tuexen 
78125ec3553SMichael Tuexen uint32_t
78225ec3553SMichael Tuexen sctp_msecs_to_ticks(uint32_t msecs)
78325ec3553SMichael Tuexen {
78425ec3553SMichael Tuexen 	uint64_t temp;
78525ec3553SMichael Tuexen 	uint32_t ticks;
78625ec3553SMichael Tuexen 
78725ec3553SMichael Tuexen 	if (hz == 1000) {
78825ec3553SMichael Tuexen 		ticks = msecs;
78925ec3553SMichael Tuexen 	} else {
79025ec3553SMichael Tuexen 		temp = (((uint64_t)msecs * hz) + 999) / 1000;
79125ec3553SMichael Tuexen 		if (temp > UINT32_MAX) {
79225ec3553SMichael Tuexen 			ticks = UINT32_MAX;
79325ec3553SMichael Tuexen 		} else {
79425ec3553SMichael Tuexen 			ticks = (uint32_t)temp;
79525ec3553SMichael Tuexen 		}
79625ec3553SMichael Tuexen 	}
79725ec3553SMichael Tuexen 	return (ticks);
79825ec3553SMichael Tuexen }
79925ec3553SMichael Tuexen 
80025ec3553SMichael Tuexen uint32_t
80125ec3553SMichael Tuexen sctp_ticks_to_msecs(uint32_t ticks)
80225ec3553SMichael Tuexen {
80325ec3553SMichael Tuexen 	uint64_t temp;
80425ec3553SMichael Tuexen 	uint32_t msecs;
80525ec3553SMichael Tuexen 
80625ec3553SMichael Tuexen 	if (hz == 1000) {
80725ec3553SMichael Tuexen 		msecs = ticks;
80825ec3553SMichael Tuexen 	} else {
80925ec3553SMichael Tuexen 		temp = (((uint64_t)ticks * 1000) + (hz - 1)) / hz;
81025ec3553SMichael Tuexen 		if (temp > UINT32_MAX) {
81125ec3553SMichael Tuexen 			msecs = UINT32_MAX;
81225ec3553SMichael Tuexen 		} else {
81325ec3553SMichael Tuexen 			msecs = (uint32_t)temp;
81425ec3553SMichael Tuexen 		}
81525ec3553SMichael Tuexen 	}
81625ec3553SMichael Tuexen 	return (msecs);
81725ec3553SMichael Tuexen }
81825ec3553SMichael Tuexen 
81925ec3553SMichael Tuexen uint32_t
82025ec3553SMichael Tuexen sctp_secs_to_ticks(uint32_t secs)
82125ec3553SMichael Tuexen {
82225ec3553SMichael Tuexen 	uint64_t temp;
82325ec3553SMichael Tuexen 	uint32_t ticks;
82425ec3553SMichael Tuexen 
82525ec3553SMichael Tuexen 	temp = (uint64_t)secs * hz;
82625ec3553SMichael Tuexen 	if (temp > UINT32_MAX) {
82725ec3553SMichael Tuexen 		ticks = UINT32_MAX;
82825ec3553SMichael Tuexen 	} else {
82925ec3553SMichael Tuexen 		ticks = (uint32_t)temp;
83025ec3553SMichael Tuexen 	}
83125ec3553SMichael Tuexen 	return (ticks);
83225ec3553SMichael Tuexen }
83325ec3553SMichael Tuexen 
83425ec3553SMichael Tuexen uint32_t
83525ec3553SMichael Tuexen sctp_ticks_to_secs(uint32_t ticks)
83625ec3553SMichael Tuexen {
83725ec3553SMichael Tuexen 	uint64_t temp;
83825ec3553SMichael Tuexen 	uint32_t secs;
83925ec3553SMichael Tuexen 
84025ec3553SMichael Tuexen 	temp = ((uint64_t)ticks + (hz - 1)) / hz;
84125ec3553SMichael Tuexen 	if (temp > UINT32_MAX) {
84225ec3553SMichael Tuexen 		secs = UINT32_MAX;
84325ec3553SMichael Tuexen 	} else {
84425ec3553SMichael Tuexen 		secs = (uint32_t)temp;
84525ec3553SMichael Tuexen 	}
84625ec3553SMichael Tuexen 	return (secs);
84725ec3553SMichael Tuexen }
84825ec3553SMichael Tuexen 
84925ec3553SMichael Tuexen /*
85012af6654SMichael Tuexen  * sctp_stop_timers_for_shutdown() should be called
85112af6654SMichael Tuexen  * when entering the SHUTDOWN_SENT or SHUTDOWN_ACK_SENT
85212af6654SMichael Tuexen  * state to make sure that all timers are stopped.
85312af6654SMichael Tuexen  */
85412af6654SMichael Tuexen void
85512af6654SMichael Tuexen sctp_stop_timers_for_shutdown(struct sctp_tcb *stcb)
85612af6654SMichael Tuexen {
8575555400aSMichael Tuexen 	struct sctp_inpcb *inp;
85812af6654SMichael Tuexen 	struct sctp_nets *net;
85912af6654SMichael Tuexen 
8605555400aSMichael Tuexen 	inp = stcb->sctp_ep;
86112af6654SMichael Tuexen 
8625555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_RECV, inp, stcb, NULL,
8635555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_12);
8645555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, inp, stcb, NULL,
8655555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_13);
8665555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, inp, stcb, NULL,
8675555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_14);
8685555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL,
8695555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_15);
8705555400aSMichael Tuexen 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
8715555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
8725555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_16);
8735555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
8745555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_17);
8755555400aSMichael Tuexen 	}
8765555400aSMichael Tuexen }
8775555400aSMichael Tuexen 
8785555400aSMichael Tuexen void
8798803350dSMichael Tuexen sctp_stop_association_timers(struct sctp_tcb *stcb, bool stop_assoc_kill_timer)
8805555400aSMichael Tuexen {
8815555400aSMichael Tuexen 	struct sctp_inpcb *inp;
8825555400aSMichael Tuexen 	struct sctp_nets *net;
8835555400aSMichael Tuexen 
8845555400aSMichael Tuexen 	inp = stcb->sctp_ep;
8855555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_RECV, inp, stcb, NULL,
8865555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_18);
8875555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, inp, stcb, NULL,
8885555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_19);
8898803350dSMichael Tuexen 	if (stop_assoc_kill_timer) {
8905555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL,
8915555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_20);
8925555400aSMichael Tuexen 	}
8935555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, inp, stcb, NULL,
8945555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_21);
8955555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL,
8965555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_22);
8975555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNGUARD, inp, stcb, NULL,
8985555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_23);
8995555400aSMichael Tuexen 	/* Mobility adaptation */
9005555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, inp, stcb, NULL,
9015555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_24);
9025555400aSMichael Tuexen 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
9035555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
9045555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_25);
9055555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net,
9065555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_26);
9075555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, inp, stcb, net,
9085555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_27);
9095555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net,
9105555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_28);
9115555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, inp, stcb, net,
9125555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_29);
9135555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
9145555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_30);
9155555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
9165555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_31);
91712af6654SMichael Tuexen 	}
91812af6654SMichael Tuexen }
91912af6654SMichael Tuexen 
92012af6654SMichael Tuexen /*
921589c42c2SMichael Tuexen  * A list of sizes based on typical mtu's, used only if next hop size not
922589c42c2SMichael Tuexen  * returned. These values MUST be multiples of 4 and MUST be ordered.
923f8829a4aSRandall Stewart  */
924437fc91aSMichael Tuexen static uint32_t sctp_mtu_sizes[] = {
925f8829a4aSRandall Stewart 	68,
926f8829a4aSRandall Stewart 	296,
927f8829a4aSRandall Stewart 	508,
928f8829a4aSRandall Stewart 	512,
929f8829a4aSRandall Stewart 	544,
930f8829a4aSRandall Stewart 	576,
931589c42c2SMichael Tuexen 	1004,
932f8829a4aSRandall Stewart 	1492,
933f8829a4aSRandall Stewart 	1500,
934f8829a4aSRandall Stewart 	1536,
935589c42c2SMichael Tuexen 	2000,
936f8829a4aSRandall Stewart 	2048,
937f8829a4aSRandall Stewart 	4352,
938f8829a4aSRandall Stewart 	4464,
93942078d5aSMichael Tuexen 	8168,
940589c42c2SMichael Tuexen 	17912,
941f8829a4aSRandall Stewart 	32000,
942589c42c2SMichael Tuexen 	65532
943f8829a4aSRandall Stewart };
944f8829a4aSRandall Stewart 
945f8829a4aSRandall Stewart /*
946589c42c2SMichael Tuexen  * Return the largest MTU in sctp_mtu_sizes smaller than val.
947589c42c2SMichael Tuexen  * If val is smaller than the minimum, just return the largest
948589c42c2SMichael Tuexen  * multiple of 4 smaller or equal to val.
949589c42c2SMichael Tuexen  * Ensure that the result is a multiple of 4.
950f8829a4aSRandall Stewart  */
951437fc91aSMichael Tuexen uint32_t
952b0471b4bSMichael Tuexen sctp_get_prev_mtu(uint32_t val)
953b0471b4bSMichael Tuexen {
954437fc91aSMichael Tuexen 	uint32_t i;
955437fc91aSMichael Tuexen 
956eef8d4a9SMichael Tuexen 	val &= 0xfffffffc;
957437fc91aSMichael Tuexen 	if (val <= sctp_mtu_sizes[0]) {
958437fc91aSMichael Tuexen 		return (val);
959437fc91aSMichael Tuexen 	}
960437fc91aSMichael Tuexen 	for (i = 1; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
961437fc91aSMichael Tuexen 		if (val <= sctp_mtu_sizes[i]) {
962f8829a4aSRandall Stewart 			break;
963f8829a4aSRandall Stewart 		}
964f8829a4aSRandall Stewart 	}
965589c42c2SMichael Tuexen 	KASSERT((sctp_mtu_sizes[i - 1] & 0x00000003) == 0,
966589c42c2SMichael Tuexen 	    ("sctp_mtu_sizes[%u] not a multiple of 4", i - 1));
967437fc91aSMichael Tuexen 	return (sctp_mtu_sizes[i - 1]);
968437fc91aSMichael Tuexen }
969437fc91aSMichael Tuexen 
970437fc91aSMichael Tuexen /*
971589c42c2SMichael Tuexen  * Return the smallest MTU in sctp_mtu_sizes larger than val.
972589c42c2SMichael Tuexen  * If val is larger than the maximum, just return the largest multiple of 4 smaller
973589c42c2SMichael Tuexen  * or equal to val.
974589c42c2SMichael Tuexen  * Ensure that the result is a multiple of 4.
975437fc91aSMichael Tuexen  */
976437fc91aSMichael Tuexen uint32_t
977b0471b4bSMichael Tuexen sctp_get_next_mtu(uint32_t val)
978b0471b4bSMichael Tuexen {
979437fc91aSMichael Tuexen 	/* select another MTU that is just bigger than this one */
980437fc91aSMichael Tuexen 	uint32_t i;
981437fc91aSMichael Tuexen 
982eef8d4a9SMichael Tuexen 	val &= 0xfffffffc;
983437fc91aSMichael Tuexen 	for (i = 0; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
984437fc91aSMichael Tuexen 		if (val < sctp_mtu_sizes[i]) {
985589c42c2SMichael Tuexen 			KASSERT((sctp_mtu_sizes[i] & 0x00000003) == 0,
986589c42c2SMichael Tuexen 			    ("sctp_mtu_sizes[%u] not a multiple of 4", i));
987437fc91aSMichael Tuexen 			return (sctp_mtu_sizes[i]);
988437fc91aSMichael Tuexen 		}
989437fc91aSMichael Tuexen 	}
990437fc91aSMichael Tuexen 	return (val);
991f8829a4aSRandall Stewart }
992f8829a4aSRandall Stewart 
993f8829a4aSRandall Stewart void
994f8829a4aSRandall Stewart sctp_fill_random_store(struct sctp_pcb *m)
995f8829a4aSRandall Stewart {
996f8829a4aSRandall Stewart 	/*
997f8829a4aSRandall Stewart 	 * Here we use the MD5/SHA-1 to hash with our good randomNumbers and
998f8829a4aSRandall Stewart 	 * our counter. The result becomes our good random numbers and we
999f8829a4aSRandall Stewart 	 * then setup to give these out. Note that we do no locking to
1000f8829a4aSRandall Stewart 	 * protect this. This is ok, since if competing folks call this we
100117205eccSRandall Stewart 	 * will get more gobbled gook in the random store which is what we
1002f8829a4aSRandall Stewart 	 * want. There is a danger that two guys will use the same random
1003f8829a4aSRandall Stewart 	 * numbers, but thats ok too since that is random as well :->
1004f8829a4aSRandall Stewart 	 */
1005f8829a4aSRandall Stewart 	m->store_at = 0;
1006ad81507eSRandall Stewart 	(void)sctp_hmac(SCTP_HMAC, (uint8_t *)m->random_numbers,
1007f8829a4aSRandall Stewart 	    sizeof(m->random_numbers), (uint8_t *)&m->random_counter,
1008f8829a4aSRandall Stewart 	    sizeof(m->random_counter), (uint8_t *)m->random_store);
1009f8829a4aSRandall Stewart 	m->random_counter++;
1010f8829a4aSRandall Stewart }
1011f8829a4aSRandall Stewart 
1012f8829a4aSRandall Stewart uint32_t
1013b0471b4bSMichael Tuexen sctp_select_initial_TSN(struct sctp_pcb *inp)
1014b0471b4bSMichael Tuexen {
1015f8829a4aSRandall Stewart 	/*
1016f8829a4aSRandall Stewart 	 * A true implementation should use random selection process to get
1017f8829a4aSRandall Stewart 	 * the initial stream sequence number, using RFC1750 as a good
1018f8829a4aSRandall Stewart 	 * guideline
1019f8829a4aSRandall Stewart 	 */
1020139bc87fSRandall Stewart 	uint32_t x, *xp;
1021f8829a4aSRandall Stewart 	uint8_t *p;
1022851b7298SRandall Stewart 	int store_at, new_store;
1023f8829a4aSRandall Stewart 
1024851b7298SRandall Stewart 	if (inp->initial_sequence_debug != 0) {
1025f8829a4aSRandall Stewart 		uint32_t ret;
1026f8829a4aSRandall Stewart 
1027851b7298SRandall Stewart 		ret = inp->initial_sequence_debug;
1028851b7298SRandall Stewart 		inp->initial_sequence_debug++;
1029f8829a4aSRandall Stewart 		return (ret);
1030f8829a4aSRandall Stewart 	}
1031851b7298SRandall Stewart retry:
1032851b7298SRandall Stewart 	store_at = inp->store_at;
1033851b7298SRandall Stewart 	new_store = store_at + sizeof(uint32_t);
1034851b7298SRandall Stewart 	if (new_store >= (SCTP_SIGNATURE_SIZE - 3)) {
1035851b7298SRandall Stewart 		new_store = 0;
1036f8829a4aSRandall Stewart 	}
1037851b7298SRandall Stewart 	if (!atomic_cmpset_int(&inp->store_at, store_at, new_store)) {
1038851b7298SRandall Stewart 		goto retry;
1039851b7298SRandall Stewart 	}
1040851b7298SRandall Stewart 	if (new_store == 0) {
1041851b7298SRandall Stewart 		/* Refill the random store */
1042851b7298SRandall Stewart 		sctp_fill_random_store(inp);
1043851b7298SRandall Stewart 	}
1044851b7298SRandall Stewart 	p = &inp->random_store[store_at];
1045139bc87fSRandall Stewart 	xp = (uint32_t *)p;
1046f8829a4aSRandall Stewart 	x = *xp;
1047f8829a4aSRandall Stewart 	return (x);
1048f8829a4aSRandall Stewart }
1049f8829a4aSRandall Stewart 
1050f8829a4aSRandall Stewart uint32_t
1051b0471b4bSMichael Tuexen sctp_select_a_tag(struct sctp_inpcb *inp, uint16_t lport, uint16_t rport, int check)
1052b0471b4bSMichael Tuexen {
10537215cc1bSMichael Tuexen 	uint32_t x;
1054f8829a4aSRandall Stewart 	struct timeval now;
1055f8829a4aSRandall Stewart 
10567215cc1bSMichael Tuexen 	if (check) {
10576e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
10587215cc1bSMichael Tuexen 	}
10597215cc1bSMichael Tuexen 	for (;;) {
1060851b7298SRandall Stewart 		x = sctp_select_initial_TSN(&inp->sctp_ep);
1061f8829a4aSRandall Stewart 		if (x == 0) {
1062f8829a4aSRandall Stewart 			/* we never use 0 */
1063f8829a4aSRandall Stewart 			continue;
1064f8829a4aSRandall Stewart 		}
10657215cc1bSMichael Tuexen 		if (!check || sctp_is_vtag_good(x, lport, rport, &now)) {
10667215cc1bSMichael Tuexen 			break;
1067f8829a4aSRandall Stewart 		}
1068f8829a4aSRandall Stewart 	}
1069f8829a4aSRandall Stewart 	return (x);
1070f8829a4aSRandall Stewart }
1071f8829a4aSRandall Stewart 
1072e92c2a8dSMichael Tuexen int32_t
1073b0471b4bSMichael Tuexen sctp_map_assoc_state(int kernel_state)
1074b0471b4bSMichael Tuexen {
1075e92c2a8dSMichael Tuexen 	int32_t user_state;
1076e92c2a8dSMichael Tuexen 
1077e92c2a8dSMichael Tuexen 	if (kernel_state & SCTP_STATE_WAS_ABORTED) {
1078e92c2a8dSMichael Tuexen 		user_state = SCTP_CLOSED;
1079e92c2a8dSMichael Tuexen 	} else if (kernel_state & SCTP_STATE_SHUTDOWN_PENDING) {
1080e92c2a8dSMichael Tuexen 		user_state = SCTP_SHUTDOWN_PENDING;
1081e92c2a8dSMichael Tuexen 	} else {
1082e92c2a8dSMichael Tuexen 		switch (kernel_state & SCTP_STATE_MASK) {
1083e92c2a8dSMichael Tuexen 		case SCTP_STATE_EMPTY:
1084e92c2a8dSMichael Tuexen 			user_state = SCTP_CLOSED;
1085e92c2a8dSMichael Tuexen 			break;
1086e92c2a8dSMichael Tuexen 		case SCTP_STATE_INUSE:
1087e92c2a8dSMichael Tuexen 			user_state = SCTP_CLOSED;
1088e92c2a8dSMichael Tuexen 			break;
1089e92c2a8dSMichael Tuexen 		case SCTP_STATE_COOKIE_WAIT:
1090e92c2a8dSMichael Tuexen 			user_state = SCTP_COOKIE_WAIT;
1091e92c2a8dSMichael Tuexen 			break;
1092e92c2a8dSMichael Tuexen 		case SCTP_STATE_COOKIE_ECHOED:
1093e92c2a8dSMichael Tuexen 			user_state = SCTP_COOKIE_ECHOED;
1094e92c2a8dSMichael Tuexen 			break;
1095e92c2a8dSMichael Tuexen 		case SCTP_STATE_OPEN:
1096e92c2a8dSMichael Tuexen 			user_state = SCTP_ESTABLISHED;
1097e92c2a8dSMichael Tuexen 			break;
1098e92c2a8dSMichael Tuexen 		case SCTP_STATE_SHUTDOWN_SENT:
1099e92c2a8dSMichael Tuexen 			user_state = SCTP_SHUTDOWN_SENT;
1100e92c2a8dSMichael Tuexen 			break;
1101e92c2a8dSMichael Tuexen 		case SCTP_STATE_SHUTDOWN_RECEIVED:
1102e92c2a8dSMichael Tuexen 			user_state = SCTP_SHUTDOWN_RECEIVED;
1103e92c2a8dSMichael Tuexen 			break;
1104e92c2a8dSMichael Tuexen 		case SCTP_STATE_SHUTDOWN_ACK_SENT:
1105e92c2a8dSMichael Tuexen 			user_state = SCTP_SHUTDOWN_ACK_SENT;
1106e92c2a8dSMichael Tuexen 			break;
1107e92c2a8dSMichael Tuexen 		default:
1108e92c2a8dSMichael Tuexen 			user_state = SCTP_CLOSED;
1109e92c2a8dSMichael Tuexen 			break;
1110e92c2a8dSMichael Tuexen 		}
1111e92c2a8dSMichael Tuexen 	}
1112e92c2a8dSMichael Tuexen 	return (user_state);
1113e92c2a8dSMichael Tuexen }
1114e92c2a8dSMichael Tuexen 
1115f8829a4aSRandall Stewart int
1116a1cb341bSMichael Tuexen sctp_init_asoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1117c979034bSMichael Tuexen     uint32_t override_tag, uint32_t vrf_id, uint16_t o_strms)
1118f8829a4aSRandall Stewart {
11190696e120SRandall Stewart 	struct sctp_association *asoc;
11200696e120SRandall Stewart 
1121f8829a4aSRandall Stewart 	/*
1122f8829a4aSRandall Stewart 	 * Anything set to zero is taken care of by the allocation routine's
1123f8829a4aSRandall Stewart 	 * bzero
1124f8829a4aSRandall Stewart 	 */
1125f8829a4aSRandall Stewart 
1126f8829a4aSRandall Stewart 	/*
1127f8829a4aSRandall Stewart 	 * Up front select what scoping to apply on addresses I tell my peer
1128f8829a4aSRandall Stewart 	 * Not sure what to do with these right now, we will need to come up
1129f8829a4aSRandall Stewart 	 * with a way to set them. We may need to pass them through from the
1130f8829a4aSRandall Stewart 	 * caller in the sctp_aloc_assoc() function.
1131f8829a4aSRandall Stewart 	 */
1132f8829a4aSRandall Stewart 	int i;
1133f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
1134f0396ad1SMichael Tuexen 	int j;
1135f0396ad1SMichael Tuexen #endif
1136f0396ad1SMichael Tuexen 
11370696e120SRandall Stewart 	asoc = &stcb->asoc;
1138f8829a4aSRandall Stewart 	/* init all variables to a known value. */
1139839d21d6SMichael Tuexen 	SCTP_SET_STATE(stcb, SCTP_STATE_INUSE);
1140a1cb341bSMichael Tuexen 	asoc->max_burst = inp->sctp_ep.max_burst;
1141a1cb341bSMichael Tuexen 	asoc->fr_max_burst = inp->sctp_ep.fr_max_burst;
114225ec3553SMichael Tuexen 	asoc->heart_beat_delay = sctp_ticks_to_msecs(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
1143a1cb341bSMichael Tuexen 	asoc->cookie_life = inp->sctp_ep.def_cookie_life;
1144a1cb341bSMichael Tuexen 	asoc->sctp_cmt_on_off = inp->sctp_cmt_on_off;
1145f342355aSMichael Tuexen 	asoc->ecn_supported = inp->ecn_supported;
1146dd973b0eSMichael Tuexen 	asoc->prsctp_supported = inp->prsctp_supported;
114744249214SRandall Stewart 	asoc->idata_supported = inp->idata_supported;
1148c79bec9cSMichael Tuexen 	asoc->auth_supported = inp->auth_supported;
1149c79bec9cSMichael Tuexen 	asoc->asconf_supported = inp->asconf_supported;
1150317e00efSMichael Tuexen 	asoc->reconfig_supported = inp->reconfig_supported;
1151caea9879SMichael Tuexen 	asoc->nrsack_supported = inp->nrsack_supported;
1152cb9b8e6fSMichael Tuexen 	asoc->pktdrop_supported = inp->pktdrop_supported;
115344249214SRandall Stewart 	asoc->idata_supported = inp->idata_supported;
1154ca85e948SMichael Tuexen 	asoc->sctp_cmt_pf = (uint8_t)0;
1155a1cb341bSMichael Tuexen 	asoc->sctp_frag_point = inp->sctp_frag_point;
1156a1cb341bSMichael Tuexen 	asoc->sctp_features = inp->sctp_features;
1157a1cb341bSMichael Tuexen 	asoc->default_dscp = inp->sctp_ep.default_dscp;
115859b6d5beSMichael Tuexen 	asoc->max_cwnd = inp->max_cwnd;
115942551e99SRandall Stewart #ifdef INET6
1160a1cb341bSMichael Tuexen 	if (inp->sctp_ep.default_flowlabel) {
1161a1cb341bSMichael Tuexen 		asoc->default_flowlabel = inp->sctp_ep.default_flowlabel;
116258bdb691SMichael Tuexen 	} else {
1163a1cb341bSMichael Tuexen 		if (inp->ip_inp.inp.inp_flags & IN6P_AUTOFLOWLABEL) {
1164a1cb341bSMichael Tuexen 			asoc->default_flowlabel = sctp_select_initial_TSN(&inp->sctp_ep);
116558bdb691SMichael Tuexen 			asoc->default_flowlabel &= 0x000fffff;
116658bdb691SMichael Tuexen 			asoc->default_flowlabel |= 0x80000000;
116758bdb691SMichael Tuexen 		} else {
1168f8829a4aSRandall Stewart 			asoc->default_flowlabel = 0;
116958bdb691SMichael Tuexen 		}
117058bdb691SMichael Tuexen 	}
1171f8829a4aSRandall Stewart #endif
11729f22f500SRandall Stewart 	asoc->sb_send_resv = 0;
1173f8829a4aSRandall Stewart 	if (override_tag) {
1174f8829a4aSRandall Stewart 		asoc->my_vtag = override_tag;
1175f8829a4aSRandall Stewart 	} else {
1176a1cb341bSMichael Tuexen 		asoc->my_vtag = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
1177f8829a4aSRandall Stewart 	}
1178de0e935bSRandall Stewart 	/* Get the nonce tags */
1179a1cb341bSMichael Tuexen 	asoc->my_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
1180a1cb341bSMichael Tuexen 	asoc->peer_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
118142551e99SRandall Stewart 	asoc->vrf_id = vrf_id;
1182de0e935bSRandall Stewart 
118318e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
118418e198d3SRandall Stewart 	asoc->tsn_in_at = 0;
118518e198d3SRandall Stewart 	asoc->tsn_out_at = 0;
118618e198d3SRandall Stewart 	asoc->tsn_in_wrapped = 0;
118718e198d3SRandall Stewart 	asoc->tsn_out_wrapped = 0;
118818e198d3SRandall Stewart 	asoc->cumack_log_at = 0;
1189b201f536SRandall Stewart 	asoc->cumack_log_atsnt = 0;
119018e198d3SRandall Stewart #endif
119118e198d3SRandall Stewart #ifdef SCTP_FS_SPEC_LOG
119218e198d3SRandall Stewart 	asoc->fs_index = 0;
119318e198d3SRandall Stewart #endif
1194f8829a4aSRandall Stewart 	asoc->refcnt = 0;
1195f8829a4aSRandall Stewart 	asoc->assoc_up_sent = 0;
1196f8829a4aSRandall Stewart 	asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number = asoc->sending_seq =
1197a1cb341bSMichael Tuexen 	    sctp_select_initial_TSN(&inp->sctp_ep);
1198c54a18d2SRandall Stewart 	asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1199f8829a4aSRandall Stewart 	/* we are optimisitic here */
1200830d754dSRandall Stewart 	asoc->peer_supports_nat = 0;
1201f8829a4aSRandall Stewart 	asoc->sent_queue_retran_cnt = 0;
1202f8829a4aSRandall Stewart 
1203f8829a4aSRandall Stewart 	/* for CMT */
12048933fa13SRandall Stewart 	asoc->last_net_cmt_send_started = NULL;
1205f8829a4aSRandall Stewart 
1206f8829a4aSRandall Stewart 	/* This will need to be adjusted */
1207f8829a4aSRandall Stewart 	asoc->last_acked_seq = asoc->init_seq_number - 1;
1208f8829a4aSRandall Stewart 	asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1209f8829a4aSRandall Stewart 	asoc->asconf_seq_in = asoc->last_acked_seq;
1210f8829a4aSRandall Stewart 
1211f8829a4aSRandall Stewart 	/* here we are different, we hold the next one we expect */
1212f8829a4aSRandall Stewart 	asoc->str_reset_seq_in = asoc->last_acked_seq + 1;
1213f8829a4aSRandall Stewart 
1214a1cb341bSMichael Tuexen 	asoc->initial_init_rto_max = inp->sctp_ep.initial_init_rto_max;
1215a1cb341bSMichael Tuexen 	asoc->initial_rto = inp->sctp_ep.initial_rto;
1216f8829a4aSRandall Stewart 
121728a6addeSMichael Tuexen 	asoc->default_mtu = inp->sctp_ep.default_mtu;
1218a1cb341bSMichael Tuexen 	asoc->max_init_times = inp->sctp_ep.max_init_times;
1219a1cb341bSMichael Tuexen 	asoc->max_send_times = inp->sctp_ep.max_send_times;
1220a1cb341bSMichael Tuexen 	asoc->def_net_failure = inp->sctp_ep.def_net_failure;
1221a1cb341bSMichael Tuexen 	asoc->def_net_pf_threshold = inp->sctp_ep.def_net_pf_threshold;
1222f8829a4aSRandall Stewart 	asoc->free_chunk_cnt = 0;
1223f8829a4aSRandall Stewart 
1224f8829a4aSRandall Stewart 	asoc->iam_blocking = 0;
1225a1cb341bSMichael Tuexen 	asoc->context = inp->sctp_context;
1226a1cb341bSMichael Tuexen 	asoc->local_strreset_support = inp->local_strreset_support;
1227a1cb341bSMichael Tuexen 	asoc->def_send = inp->def_send;
122825ec3553SMichael Tuexen 	asoc->delayed_ack = sctp_ticks_to_msecs(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1229a1cb341bSMichael Tuexen 	asoc->sack_freq = inp->sctp_ep.sctp_sack_freq;
1230f8829a4aSRandall Stewart 	asoc->pr_sctp_cnt = 0;
1231f8829a4aSRandall Stewart 	asoc->total_output_queue_size = 0;
1232f8829a4aSRandall Stewart 
1233a1cb341bSMichael Tuexen 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1234a1cb341bSMichael Tuexen 		asoc->scope.ipv6_addr_legal = 1;
1235a1cb341bSMichael Tuexen 		if (SCTP_IPV6_V6ONLY(inp) == 0) {
1236a1cb341bSMichael Tuexen 			asoc->scope.ipv4_addr_legal = 1;
1237f8829a4aSRandall Stewart 		} else {
1238a1cb341bSMichael Tuexen 			asoc->scope.ipv4_addr_legal = 0;
1239f8829a4aSRandall Stewart 		}
1240f8829a4aSRandall Stewart 	} else {
1241a1cb341bSMichael Tuexen 		asoc->scope.ipv6_addr_legal = 0;
1242a1cb341bSMichael Tuexen 		asoc->scope.ipv4_addr_legal = 1;
1243f8829a4aSRandall Stewart 	}
1244f8829a4aSRandall Stewart 
1245a1cb341bSMichael Tuexen 	asoc->my_rwnd = max(SCTP_SB_LIMIT_RCV(inp->sctp_socket), SCTP_MINIMAL_RWND);
1246a1cb341bSMichael Tuexen 	asoc->peers_rwnd = SCTP_SB_LIMIT_RCV(inp->sctp_socket);
1247f8829a4aSRandall Stewart 
1248a1cb341bSMichael Tuexen 	asoc->smallest_mtu = inp->sctp_frag_point;
1249a1cb341bSMichael Tuexen 	asoc->minrto = inp->sctp_ep.sctp_minrto;
1250a1cb341bSMichael Tuexen 	asoc->maxrto = inp->sctp_ep.sctp_maxrto;
1251f8829a4aSRandall Stewart 
1252f8829a4aSRandall Stewart 	asoc->stream_locked_on = 0;
1253f8829a4aSRandall Stewart 	asoc->ecn_echo_cnt_onq = 0;
1254f8829a4aSRandall Stewart 	asoc->stream_locked = 0;
1255f8829a4aSRandall Stewart 
125642551e99SRandall Stewart 	asoc->send_sack = 1;
125742551e99SRandall Stewart 
125842551e99SRandall Stewart 	LIST_INIT(&asoc->sctp_restricted_addrs);
125942551e99SRandall Stewart 
1260f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->nets);
1261f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->pending_reply_queue);
12622afb3e84SRandall Stewart 	TAILQ_INIT(&asoc->asconf_ack_sent);
1263f8829a4aSRandall Stewart 	/* Setup to fill the hb random cache at first HB */
1264f8829a4aSRandall Stewart 	asoc->hb_random_idx = 4;
1265f8829a4aSRandall Stewart 
1266a1cb341bSMichael Tuexen 	asoc->sctp_autoclose_ticks = inp->sctp_ep.auto_close_time;
1267f8829a4aSRandall Stewart 
1268a1cb341bSMichael Tuexen 	stcb->asoc.congestion_control_module = inp->sctp_ep.sctp_default_cc_module;
1269a1cb341bSMichael Tuexen 	stcb->asoc.cc_functions = sctp_cc_functions[inp->sctp_ep.sctp_default_cc_module];
1270b54d3a6cSRandall Stewart 
1271a1cb341bSMichael Tuexen 	stcb->asoc.stream_scheduling_module = inp->sctp_ep.sctp_default_ss_module;
1272a1cb341bSMichael Tuexen 	stcb->asoc.ss_functions = sctp_ss_functions[inp->sctp_ep.sctp_default_ss_module];
1273f7a77f6fSMichael Tuexen 
1274b54d3a6cSRandall Stewart 	/*
1275f8829a4aSRandall Stewart 	 * Now the stream parameters, here we allocate space for all streams
1276f8829a4aSRandall Stewart 	 * that we request by default.
1277f8829a4aSRandall Stewart 	 */
1278ea44232bSRandall Stewart 	asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams =
1279c979034bSMichael Tuexen 	    o_strms;
1280f8829a4aSRandall Stewart 	SCTP_MALLOC(asoc->strmout, struct sctp_stream_out *,
1281f8829a4aSRandall Stewart 	    asoc->streamoutcnt * sizeof(struct sctp_stream_out),
1282207304d4SRandall Stewart 	    SCTP_M_STRMO);
1283f8829a4aSRandall Stewart 	if (asoc->strmout == NULL) {
1284f8829a4aSRandall Stewart 		/* big trouble no memory */
1285c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1286f8829a4aSRandall Stewart 		return (ENOMEM);
1287f8829a4aSRandall Stewart 	}
1288f8829a4aSRandall Stewart 	for (i = 0; i < asoc->streamoutcnt; i++) {
1289f8829a4aSRandall Stewart 		/*
1290f8829a4aSRandall Stewart 		 * inbound side must be set to 0xffff, also NOTE when we get
1291f8829a4aSRandall Stewart 		 * the INIT-ACK back (for INIT sender) we MUST reduce the
1292f8829a4aSRandall Stewart 		 * count (streamoutcnt) but first check if we sent to any of
1293f8829a4aSRandall Stewart 		 * the upper streams that were dropped (if some were). Those
1294f8829a4aSRandall Stewart 		 * that were dropped must be notified to the upper layer as
1295f8829a4aSRandall Stewart 		 * failed to send.
1296f8829a4aSRandall Stewart 		 */
129763d5b568SMichael Tuexen 		asoc->strmout[i].next_mid_ordered = 0;
129863d5b568SMichael Tuexen 		asoc->strmout[i].next_mid_unordered = 0;
1299f8829a4aSRandall Stewart 		TAILQ_INIT(&asoc->strmout[i].outqueue);
1300325c8c46SMichael Tuexen 		asoc->strmout[i].chunks_on_queues = 0;
1301f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
1302f0396ad1SMichael Tuexen 		for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1303f0396ad1SMichael Tuexen 			asoc->strmout[i].abandoned_sent[j] = 0;
1304f0396ad1SMichael Tuexen 			asoc->strmout[i].abandoned_unsent[j] = 0;
1305f0396ad1SMichael Tuexen 		}
1306f0396ad1SMichael Tuexen #else
1307f0396ad1SMichael Tuexen 		asoc->strmout[i].abandoned_sent[0] = 0;
1308f0396ad1SMichael Tuexen 		asoc->strmout[i].abandoned_unsent[0] = 0;
1309f0396ad1SMichael Tuexen #endif
131049656eefSMichael Tuexen 		asoc->strmout[i].sid = i;
1311f8829a4aSRandall Stewart 		asoc->strmout[i].last_msg_incomplete = 0;
13127cca1775SRandall Stewart 		asoc->strmout[i].state = SCTP_STREAM_OPENING;
1313d1ea5fa9SMichael Tuexen 		asoc->ss_functions.sctp_ss_init_stream(stcb, &asoc->strmout[i], NULL);
1314f8829a4aSRandall Stewart 	}
1315f7a77f6fSMichael Tuexen 	asoc->ss_functions.sctp_ss_init(stcb, asoc, 0);
1316f7a77f6fSMichael Tuexen 
1317f8829a4aSRandall Stewart 	/* Now the mapping array */
1318f8829a4aSRandall Stewart 	asoc->mapping_array_size = SCTP_INITIAL_MAPPING_ARRAY;
1319f8829a4aSRandall Stewart 	SCTP_MALLOC(asoc->mapping_array, uint8_t *, asoc->mapping_array_size,
1320207304d4SRandall Stewart 	    SCTP_M_MAP);
1321f8829a4aSRandall Stewart 	if (asoc->mapping_array == NULL) {
1322207304d4SRandall Stewart 		SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1323c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1324f8829a4aSRandall Stewart 		return (ENOMEM);
1325f8829a4aSRandall Stewart 	}
1326f8829a4aSRandall Stewart 	memset(asoc->mapping_array, 0, asoc->mapping_array_size);
1327b5c16493SMichael Tuexen 	SCTP_MALLOC(asoc->nr_mapping_array, uint8_t *, asoc->mapping_array_size,
1328830d754dSRandall Stewart 	    SCTP_M_MAP);
1329bf1be571SRandall Stewart 	if (asoc->nr_mapping_array == NULL) {
1330bf1be571SRandall Stewart 		SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1331bf1be571SRandall Stewart 		SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1332bf1be571SRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1333bf1be571SRandall Stewart 		return (ENOMEM);
1334bf1be571SRandall Stewart 	}
1335b5c16493SMichael Tuexen 	memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
1336830d754dSRandall Stewart 
1337f8829a4aSRandall Stewart 	/* Now the init of the other outqueues */
1338f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->free_chunks);
1339f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->control_send_queue);
1340c54a18d2SRandall Stewart 	TAILQ_INIT(&asoc->asconf_send_queue);
1341f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->send_queue);
1342f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->sent_queue);
1343f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->resetHead);
1344a1cb341bSMichael Tuexen 	asoc->max_inbound_streams = inp->sctp_ep.max_open_streams_intome;
1345f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->asconf_queue);
1346f8829a4aSRandall Stewart 	/* authentication fields */
1347f8829a4aSRandall Stewart 	asoc->authinfo.random = NULL;
1348830d754dSRandall Stewart 	asoc->authinfo.active_keyid = 0;
1349f8829a4aSRandall Stewart 	asoc->authinfo.assoc_key = NULL;
1350f8829a4aSRandall Stewart 	asoc->authinfo.assoc_keyid = 0;
1351f8829a4aSRandall Stewart 	asoc->authinfo.recv_key = NULL;
1352f8829a4aSRandall Stewart 	asoc->authinfo.recv_keyid = 0;
1353f8829a4aSRandall Stewart 	LIST_INIT(&asoc->shared_keys);
1354f42a358aSRandall Stewart 	asoc->marked_retrans = 0;
1355a1cb341bSMichael Tuexen 	asoc->port = inp->sctp_ep.port;
1356f42a358aSRandall Stewart 	asoc->timoinit = 0;
1357f42a358aSRandall Stewart 	asoc->timodata = 0;
1358f42a358aSRandall Stewart 	asoc->timosack = 0;
1359f42a358aSRandall Stewart 	asoc->timoshutdown = 0;
1360f42a358aSRandall Stewart 	asoc->timoheartbeat = 0;
1361f42a358aSRandall Stewart 	asoc->timocookie = 0;
1362f42a358aSRandall Stewart 	asoc->timoshutdownack = 0;
13636e55db54SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&asoc->start_time);
13646e55db54SRandall Stewart 	asoc->discontinuity_time = asoc->start_time;
1365f0396ad1SMichael Tuexen 	for (i = 0; i < SCTP_PR_SCTP_MAX + 1; i++) {
1366f0396ad1SMichael Tuexen 		asoc->abandoned_unsent[i] = 0;
1367f0396ad1SMichael Tuexen 		asoc->abandoned_sent[i] = 0;
1368f0396ad1SMichael Tuexen 	}
1369eacc51c5SRandall Stewart 	/*
1370eacc51c5SRandall Stewart 	 * sa_ignore MEMLEAK {memory is put in the assoc mapping array and
137177acdc25SRandall Stewart 	 * freed later when the association is freed.
1372eacc51c5SRandall Stewart 	 */
1373f8829a4aSRandall Stewart 	return (0);
1374f8829a4aSRandall Stewart }
1375f8829a4aSRandall Stewart 
13760e13104dSRandall Stewart void
13770e13104dSRandall Stewart sctp_print_mapping_array(struct sctp_association *asoc)
13780e13104dSRandall Stewart {
1379aed5947cSMichael Tuexen 	unsigned int i, limit;
13800e13104dSRandall Stewart 
1381cd3fd531SMichael Tuexen 	SCTP_PRINTF("Mapping array size: %d, baseTSN: %8.8x, cumAck: %8.8x, highestTSN: (%8.8x, %8.8x).\n",
13820e13104dSRandall Stewart 	    asoc->mapping_array_size,
13830e13104dSRandall Stewart 	    asoc->mapping_array_base_tsn,
13840e13104dSRandall Stewart 	    asoc->cumulative_tsn,
1385aed5947cSMichael Tuexen 	    asoc->highest_tsn_inside_map,
1386aed5947cSMichael Tuexen 	    asoc->highest_tsn_inside_nr_map);
1387aed5947cSMichael Tuexen 	for (limit = asoc->mapping_array_size; limit > 1; limit--) {
138860990c0cSMichael Tuexen 		if (asoc->mapping_array[limit - 1] != 0) {
138977acdc25SRandall Stewart 			break;
139077acdc25SRandall Stewart 		}
139177acdc25SRandall Stewart 	}
1392cd3fd531SMichael Tuexen 	SCTP_PRINTF("Renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
139377acdc25SRandall Stewart 	for (i = 0; i < limit; i++) {
1394cd3fd531SMichael Tuexen 		SCTP_PRINTF("%2.2x%c", asoc->mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
139577acdc25SRandall Stewart 	}
1396aed5947cSMichael Tuexen 	if (limit % 16)
1397cd3fd531SMichael Tuexen 		SCTP_PRINTF("\n");
1398aed5947cSMichael Tuexen 	for (limit = asoc->mapping_array_size; limit > 1; limit--) {
1399aed5947cSMichael Tuexen 		if (asoc->nr_mapping_array[limit - 1]) {
140077acdc25SRandall Stewart 			break;
140177acdc25SRandall Stewart 		}
140277acdc25SRandall Stewart 	}
1403cd3fd531SMichael Tuexen 	SCTP_PRINTF("Non renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
140477acdc25SRandall Stewart 	for (i = 0; i < limit; i++) {
1405cd3fd531SMichael Tuexen 		SCTP_PRINTF("%2.2x%c", asoc->nr_mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
14060e13104dSRandall Stewart 	}
1407aed5947cSMichael Tuexen 	if (limit % 16)
1408cd3fd531SMichael Tuexen 		SCTP_PRINTF("\n");
14090e13104dSRandall Stewart }
14100e13104dSRandall Stewart 
1411f8829a4aSRandall Stewart int
14120696e120SRandall Stewart sctp_expand_mapping_array(struct sctp_association *asoc, uint32_t needed)
1413f8829a4aSRandall Stewart {
1414f8829a4aSRandall Stewart 	/* mapping array needs to grow */
1415b5c16493SMichael Tuexen 	uint8_t *new_array1, *new_array2;
14160696e120SRandall Stewart 	uint32_t new_size;
1417f8829a4aSRandall Stewart 
14180696e120SRandall Stewart 	new_size = asoc->mapping_array_size + ((needed + 7) / 8 + SCTP_MAPPING_ARRAY_INCR);
1419b5c16493SMichael Tuexen 	SCTP_MALLOC(new_array1, uint8_t *, new_size, SCTP_M_MAP);
1420b5c16493SMichael Tuexen 	SCTP_MALLOC(new_array2, uint8_t *, new_size, SCTP_M_MAP);
1421b5c16493SMichael Tuexen 	if ((new_array1 == NULL) || (new_array2 == NULL)) {
1422f8829a4aSRandall Stewart 		/* can't get more, forget it */
1423b5c16493SMichael Tuexen 		SCTP_PRINTF("No memory for expansion of SCTP mapping array %d\n", new_size);
1424b5c16493SMichael Tuexen 		if (new_array1) {
1425b5c16493SMichael Tuexen 			SCTP_FREE(new_array1, SCTP_M_MAP);
1426b5c16493SMichael Tuexen 		}
1427b5c16493SMichael Tuexen 		if (new_array2) {
1428b5c16493SMichael Tuexen 			SCTP_FREE(new_array2, SCTP_M_MAP);
1429b5c16493SMichael Tuexen 		}
1430f8829a4aSRandall Stewart 		return (-1);
1431f8829a4aSRandall Stewart 	}
1432b5c16493SMichael Tuexen 	memset(new_array1, 0, new_size);
1433b5c16493SMichael Tuexen 	memset(new_array2, 0, new_size);
1434b5c16493SMichael Tuexen 	memcpy(new_array1, asoc->mapping_array, asoc->mapping_array_size);
1435b5c16493SMichael Tuexen 	memcpy(new_array2, asoc->nr_mapping_array, asoc->mapping_array_size);
1436207304d4SRandall Stewart 	SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1437830d754dSRandall Stewart 	SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
1438b5c16493SMichael Tuexen 	asoc->mapping_array = new_array1;
1439b5c16493SMichael Tuexen 	asoc->nr_mapping_array = new_array2;
1440b5c16493SMichael Tuexen 	asoc->mapping_array_size = new_size;
1441830d754dSRandall Stewart 	return (0);
1442830d754dSRandall Stewart }
1443830d754dSRandall Stewart 
14448933fa13SRandall Stewart 
144542551e99SRandall Stewart static void
144642551e99SRandall Stewart sctp_iterator_work(struct sctp_iterator *it)
144742551e99SRandall Stewart {
1448868b51f2SMichael Tuexen 	struct epoch_tracker et;
1449868b51f2SMichael Tuexen 	struct sctp_inpcb *tinp;
145042551e99SRandall Stewart 	int iteration_count = 0;
145142551e99SRandall Stewart 	int inp_skip = 0;
1452ec4c19fcSRandall Stewart 	int first_in = 1;
145342551e99SRandall Stewart 
1454868b51f2SMichael Tuexen 	NET_EPOCH_ENTER(et);
1455ec4c19fcSRandall Stewart 	SCTP_INP_INFO_RLOCK();
145642551e99SRandall Stewart 	SCTP_ITERATOR_LOCK();
1457dcb436c9SMichael Tuexen 	sctp_it_ctl.cur_it = it;
1458ad81507eSRandall Stewart 	if (it->inp) {
1459ec4c19fcSRandall Stewart 		SCTP_INP_RLOCK(it->inp);
146042551e99SRandall Stewart 		SCTP_INP_DECR_REF(it->inp);
1461ad81507eSRandall Stewart 	}
146242551e99SRandall Stewart 	if (it->inp == NULL) {
146342551e99SRandall Stewart 		/* iterator is complete */
146442551e99SRandall Stewart done_with_iterator:
1465dcb436c9SMichael Tuexen 		sctp_it_ctl.cur_it = NULL;
146642551e99SRandall Stewart 		SCTP_ITERATOR_UNLOCK();
1467ec4c19fcSRandall Stewart 		SCTP_INP_INFO_RUNLOCK();
146842551e99SRandall Stewart 		if (it->function_atend != NULL) {
146942551e99SRandall Stewart 			(*it->function_atend) (it->pointer, it->val);
147042551e99SRandall Stewart 		}
1471207304d4SRandall Stewart 		SCTP_FREE(it, SCTP_M_ITER);
1472868b51f2SMichael Tuexen 		NET_EPOCH_EXIT(et);
147342551e99SRandall Stewart 		return;
147442551e99SRandall Stewart 	}
147542551e99SRandall Stewart select_a_new_ep:
1476ec4c19fcSRandall Stewart 	if (first_in) {
1477ec4c19fcSRandall Stewart 		first_in = 0;
1478ec4c19fcSRandall Stewart 	} else {
1479f7517433SRandall Stewart 		SCTP_INP_RLOCK(it->inp);
1480ec4c19fcSRandall Stewart 	}
148142551e99SRandall Stewart 	while (((it->pcb_flags) &&
148242551e99SRandall Stewart 	    ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) ||
148342551e99SRandall Stewart 	    ((it->pcb_features) &&
148442551e99SRandall Stewart 	    ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) {
148542551e99SRandall Stewart 		/* endpoint flags or features don't match, so keep looking */
148642551e99SRandall Stewart 		if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1487f7517433SRandall Stewart 			SCTP_INP_RUNLOCK(it->inp);
148842551e99SRandall Stewart 			goto done_with_iterator;
148942551e99SRandall Stewart 		}
1490ec4c19fcSRandall Stewart 		tinp = it->inp;
149142551e99SRandall Stewart 		it->inp = LIST_NEXT(it->inp, sctp_list);
1492ec4c19fcSRandall Stewart 		SCTP_INP_RUNLOCK(tinp);
149342551e99SRandall Stewart 		if (it->inp == NULL) {
149442551e99SRandall Stewart 			goto done_with_iterator;
149542551e99SRandall Stewart 		}
149642551e99SRandall Stewart 		SCTP_INP_RLOCK(it->inp);
1497f7517433SRandall Stewart 	}
149842551e99SRandall Stewart 	/* now go through each assoc which is in the desired state */
149942551e99SRandall Stewart 	if (it->done_current_ep == 0) {
150042551e99SRandall Stewart 		if (it->function_inp != NULL)
150142551e99SRandall Stewart 			inp_skip = (*it->function_inp) (it->inp, it->pointer, it->val);
150242551e99SRandall Stewart 		it->done_current_ep = 1;
150342551e99SRandall Stewart 	}
150442551e99SRandall Stewart 	if (it->stcb == NULL) {
150542551e99SRandall Stewart 		/* run the per instance function */
150642551e99SRandall Stewart 		it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
150742551e99SRandall Stewart 	}
150842551e99SRandall Stewart 	if ((inp_skip) || it->stcb == NULL) {
150942551e99SRandall Stewart 		if (it->function_inp_end != NULL) {
151042551e99SRandall Stewart 			inp_skip = (*it->function_inp_end) (it->inp,
151142551e99SRandall Stewart 			    it->pointer,
151242551e99SRandall Stewart 			    it->val);
151342551e99SRandall Stewart 		}
151442551e99SRandall Stewart 		SCTP_INP_RUNLOCK(it->inp);
151542551e99SRandall Stewart 		goto no_stcb;
151642551e99SRandall Stewart 	}
151742551e99SRandall Stewart 	while (it->stcb) {
151842551e99SRandall Stewart 		SCTP_TCB_LOCK(it->stcb);
151942551e99SRandall Stewart 		if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
152042551e99SRandall Stewart 			/* not in the right state... keep looking */
152142551e99SRandall Stewart 			SCTP_TCB_UNLOCK(it->stcb);
152242551e99SRandall Stewart 			goto next_assoc;
152342551e99SRandall Stewart 		}
152442551e99SRandall Stewart 		/* see if we have limited out the iterator loop */
152542551e99SRandall Stewart 		iteration_count++;
152642551e99SRandall Stewart 		if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) {
152742551e99SRandall Stewart 			/* Pause to let others grab the lock */
152842551e99SRandall Stewart 			atomic_add_int(&it->stcb->asoc.refcnt, 1);
152942551e99SRandall Stewart 			SCTP_TCB_UNLOCK(it->stcb);
1530c4739e2fSRandall Stewart 			SCTP_INP_INCR_REF(it->inp);
153142551e99SRandall Stewart 			SCTP_INP_RUNLOCK(it->inp);
153242551e99SRandall Stewart 			SCTP_ITERATOR_UNLOCK();
1533ec4c19fcSRandall Stewart 			SCTP_INP_INFO_RUNLOCK();
1534ec4c19fcSRandall Stewart 			SCTP_INP_INFO_RLOCK();
153542551e99SRandall Stewart 			SCTP_ITERATOR_LOCK();
1536f7517433SRandall Stewart 			if (sctp_it_ctl.iterator_flags) {
1537f7517433SRandall Stewart 				/* We won't be staying here */
1538f7517433SRandall Stewart 				SCTP_INP_DECR_REF(it->inp);
1539f7517433SRandall Stewart 				atomic_add_int(&it->stcb->asoc.refcnt, -1);
1540f7517433SRandall Stewart 				if (sctp_it_ctl.iterator_flags &
1541f7517433SRandall Stewart 				    SCTP_ITERATOR_STOP_CUR_IT) {
1542f7517433SRandall Stewart 					sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_IT;
1543f7517433SRandall Stewart 					goto done_with_iterator;
1544f7517433SRandall Stewart 				}
1545f7517433SRandall Stewart 				if (sctp_it_ctl.iterator_flags &
1546f7517433SRandall Stewart 				    SCTP_ITERATOR_STOP_CUR_INP) {
1547f7517433SRandall Stewart 					sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_INP;
1548f7517433SRandall Stewart 					goto no_stcb;
1549f7517433SRandall Stewart 				}
1550f7517433SRandall Stewart 				/* If we reach here huh? */
1551cd3fd531SMichael Tuexen 				SCTP_PRINTF("Unknown it ctl flag %x\n",
1552f7517433SRandall Stewart 				    sctp_it_ctl.iterator_flags);
1553f7517433SRandall Stewart 				sctp_it_ctl.iterator_flags = 0;
1554f7517433SRandall Stewart 			}
155542551e99SRandall Stewart 			SCTP_INP_RLOCK(it->inp);
1556c4739e2fSRandall Stewart 			SCTP_INP_DECR_REF(it->inp);
155742551e99SRandall Stewart 			SCTP_TCB_LOCK(it->stcb);
155842551e99SRandall Stewart 			atomic_add_int(&it->stcb->asoc.refcnt, -1);
155942551e99SRandall Stewart 			iteration_count = 0;
156042551e99SRandall Stewart 		}
15610053ed28SMichael Tuexen 
156242551e99SRandall Stewart 		/* run function on this one */
156342551e99SRandall Stewart 		(*it->function_assoc) (it->inp, it->stcb, it->pointer, it->val);
156442551e99SRandall Stewart 
156542551e99SRandall Stewart 		/*
156642551e99SRandall Stewart 		 * we lie here, it really needs to have its own type but
156742551e99SRandall Stewart 		 * first I must verify that this won't effect things :-0
156842551e99SRandall Stewart 		 */
156942551e99SRandall Stewart 		if (it->no_chunk_output == 0)
1570ceaad40aSRandall Stewart 			sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
157142551e99SRandall Stewart 
157242551e99SRandall Stewart 		SCTP_TCB_UNLOCK(it->stcb);
157342551e99SRandall Stewart next_assoc:
157442551e99SRandall Stewart 		it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
157542551e99SRandall Stewart 		if (it->stcb == NULL) {
157642551e99SRandall Stewart 			/* Run last function */
157742551e99SRandall Stewart 			if (it->function_inp_end != NULL) {
157842551e99SRandall Stewart 				inp_skip = (*it->function_inp_end) (it->inp,
157942551e99SRandall Stewart 				    it->pointer,
158042551e99SRandall Stewart 				    it->val);
158142551e99SRandall Stewart 			}
158242551e99SRandall Stewart 		}
158342551e99SRandall Stewart 	}
158442551e99SRandall Stewart 	SCTP_INP_RUNLOCK(it->inp);
158542551e99SRandall Stewart no_stcb:
158642551e99SRandall Stewart 	/* done with all assocs on this endpoint, move on to next endpoint */
158742551e99SRandall Stewart 	it->done_current_ep = 0;
158842551e99SRandall Stewart 	if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
158942551e99SRandall Stewart 		it->inp = NULL;
159042551e99SRandall Stewart 	} else {
159142551e99SRandall Stewart 		it->inp = LIST_NEXT(it->inp, sctp_list);
159242551e99SRandall Stewart 	}
159342551e99SRandall Stewart 	if (it->inp == NULL) {
159442551e99SRandall Stewart 		goto done_with_iterator;
159542551e99SRandall Stewart 	}
159642551e99SRandall Stewart 	goto select_a_new_ep;
159742551e99SRandall Stewart }
159842551e99SRandall Stewart 
159942551e99SRandall Stewart void
160042551e99SRandall Stewart sctp_iterator_worker(void)
160142551e99SRandall Stewart {
1602397b1c94SMichael Tuexen 	struct sctp_iterator *it;
160342551e99SRandall Stewart 
160442551e99SRandall Stewart 	/* This function is called with the WQ lock in place */
1605f7517433SRandall Stewart 	sctp_it_ctl.iterator_running = 1;
1606397b1c94SMichael Tuexen 	while ((it = TAILQ_FIRST(&sctp_it_ctl.iteratorhead)) != NULL) {
160742551e99SRandall Stewart 		/* now lets work on this one */
1608f7517433SRandall Stewart 		TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
160942551e99SRandall Stewart 		SCTP_IPI_ITERATOR_WQ_UNLOCK();
1610f7517433SRandall Stewart 		CURVNET_SET(it->vn);
161142551e99SRandall Stewart 		sctp_iterator_work(it);
1612f7517433SRandall Stewart 		CURVNET_RESTORE();
161342551e99SRandall Stewart 		SCTP_IPI_ITERATOR_WQ_LOCK();
16143c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
161542551e99SRandall Stewart 	}
1616f7517433SRandall Stewart 	sctp_it_ctl.iterator_running = 0;
161742551e99SRandall Stewart 	return;
161842551e99SRandall Stewart }
161942551e99SRandall Stewart 
1620f8829a4aSRandall Stewart 
1621f8829a4aSRandall Stewart static void
1622f8829a4aSRandall Stewart sctp_handle_addr_wq(void)
1623f8829a4aSRandall Stewart {
1624f8829a4aSRandall Stewart 	/* deal with the ADDR wq from the rtsock calls */
16254a9ef3f8SMichael Tuexen 	struct sctp_laddr *wi, *nwi;
162642551e99SRandall Stewart 	struct sctp_asconf_iterator *asc;
1627f8829a4aSRandall Stewart 
162842551e99SRandall Stewart 	SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
1629207304d4SRandall Stewart 	    sizeof(struct sctp_asconf_iterator), SCTP_M_ASC_IT);
163042551e99SRandall Stewart 	if (asc == NULL) {
163142551e99SRandall Stewart 		/* Try later, no memory */
1632f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
1633f8829a4aSRandall Stewart 		    (struct sctp_inpcb *)NULL,
1634f8829a4aSRandall Stewart 		    (struct sctp_tcb *)NULL,
1635f8829a4aSRandall Stewart 		    (struct sctp_nets *)NULL);
163642551e99SRandall Stewart 		return;
1637f8829a4aSRandall Stewart 	}
163842551e99SRandall Stewart 	LIST_INIT(&asc->list_of_work);
163942551e99SRandall Stewart 	asc->cnt = 0;
1640f7517433SRandall Stewart 
16414a9ef3f8SMichael Tuexen 	LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
164242551e99SRandall Stewart 		LIST_REMOVE(wi, sctp_nxt_addr);
164342551e99SRandall Stewart 		LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
164442551e99SRandall Stewart 		asc->cnt++;
1645f8829a4aSRandall Stewart 	}
1646f7517433SRandall Stewart 
164742551e99SRandall Stewart 	if (asc->cnt == 0) {
1648207304d4SRandall Stewart 		SCTP_FREE(asc, SCTP_M_ASC_IT);
164942551e99SRandall Stewart 	} else {
16502b1c7de4SMichael Tuexen 		int ret;
16512b1c7de4SMichael Tuexen 
16522b1c7de4SMichael Tuexen 		ret = sctp_initiate_iterator(sctp_asconf_iterator_ep,
16531b649582SRandall Stewart 		    sctp_asconf_iterator_stcb,
165442551e99SRandall Stewart 		    NULL,	/* No ep end for boundall */
165542551e99SRandall Stewart 		    SCTP_PCB_FLAGS_BOUNDALL,
165642551e99SRandall Stewart 		    SCTP_PCB_ANY_FEATURES,
16571b649582SRandall Stewart 		    SCTP_ASOC_ANY_STATE,
16581b649582SRandall Stewart 		    (void *)asc, 0,
16591b649582SRandall Stewart 		    sctp_asconf_iterator_end, NULL, 0);
16602b1c7de4SMichael Tuexen 		if (ret) {
16612b1c7de4SMichael Tuexen 			SCTP_PRINTF("Failed to initiate iterator for handle_addr_wq\n");
1662b7b84c0eSMichael Tuexen 			/*
1663b7b84c0eSMichael Tuexen 			 * Freeing if we are stopping or put back on the
1664b7b84c0eSMichael Tuexen 			 * addr_wq.
1665b7b84c0eSMichael Tuexen 			 */
16662b1c7de4SMichael Tuexen 			if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
16672b1c7de4SMichael Tuexen 				sctp_asconf_iterator_end(asc, 0);
16682b1c7de4SMichael Tuexen 			} else {
16692b1c7de4SMichael Tuexen 				LIST_FOREACH(wi, &asc->list_of_work, sctp_nxt_addr) {
16702b1c7de4SMichael Tuexen 					LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
16712b1c7de4SMichael Tuexen 				}
16722b1c7de4SMichael Tuexen 				SCTP_FREE(asc, SCTP_M_ASC_IT);
16732b1c7de4SMichael Tuexen 			}
16742b1c7de4SMichael Tuexen 		}
167542551e99SRandall Stewart 	}
1676f8829a4aSRandall Stewart }
1677f8829a4aSRandall Stewart 
1678a412576eSMichael Tuexen /*-
1679a412576eSMichael Tuexen  * The following table shows which pointers for the inp, stcb, or net are
1680a412576eSMichael Tuexen  * stored for each timer after it was started.
1681a412576eSMichael Tuexen  *
1682a412576eSMichael Tuexen  *|Name                         |Timer                        |inp |stcb|net |
1683a412576eSMichael Tuexen  *|-----------------------------|-----------------------------|----|----|----|
1684a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_SEND         |net->rxt_timer               |Yes |Yes |Yes |
1685a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_INIT         |net->rxt_timer               |Yes |Yes |Yes |
1686a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_RECV         |stcb->asoc.dack_timer        |Yes |Yes |No  |
1687a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_SHUTDOWN     |net->rxt_timer               |Yes |Yes |Yes |
1688a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_HEARTBEAT    |net->hb_timer                |Yes |Yes |Yes |
1689a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_COOKIE       |net->rxt_timer               |Yes |Yes |Yes |
1690a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_NEWCOOKIE    |inp->sctp_ep.signature_change|Yes |No  |No  |
1691a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_PATHMTURAISE |net->pmtu_timer              |Yes |Yes |Yes |
1692a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_SHUTDOWNACK  |net->rxt_timer               |Yes |Yes |Yes |
1693a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_ASCONF       |stcb->asoc.asconf_timer      |Yes |Yes |Yes |
1694a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_SHUTDOWNGUARD|stcb->asoc.shut_guard_timer  |Yes |Yes |No  |
1695a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_AUTOCLOSE    |stcb->asoc.autoclose_timer   |Yes |Yes |No  |
1696a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_STRRESET     |stcb->asoc.strreset_timer    |Yes |Yes |No  |
1697a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_INPKILL      |inp->sctp_ep.signature_change|Yes |No  |No  |
1698a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_ASOCKILL     |stcb->asoc.strreset_timer    |Yes |Yes |No  |
1699a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_ADDR_WQ      |SCTP_BASE_INFO(addr_wq_timer)|No  |No  |No  |
1700a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_PRIM_DELETED |stcb->asoc.delete_prim_timer |Yes |Yes |No  |
1701a412576eSMichael Tuexen  */
1702a412576eSMichael Tuexen 
1703f8829a4aSRandall Stewart void
1704f8829a4aSRandall Stewart sctp_timeout_handler(void *t)
1705f8829a4aSRandall Stewart {
1706868b51f2SMichael Tuexen 	struct epoch_tracker et;
1707a412576eSMichael Tuexen 	struct timeval tv;
1708f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
1709f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
1710f8829a4aSRandall Stewart 	struct sctp_nets *net;
1711f8829a4aSRandall Stewart 	struct sctp_timer *tmr;
1712267dbe63SMichael Tuexen 	struct mbuf *op_err;
1713ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1714ceaad40aSRandall Stewart 	struct socket *so;
1715ceaad40aSRandall Stewart #endif
1716548f47a8SMichael Tuexen 	int did_output;
1717fa89f692SMichael Tuexen 	int type;
1718a412576eSMichael Tuexen 	int i, secret;
1719f8829a4aSRandall Stewart 
1720f8829a4aSRandall Stewart 	tmr = (struct sctp_timer *)t;
1721f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)tmr->ep;
1722f8829a4aSRandall Stewart 	stcb = (struct sctp_tcb *)tmr->tcb;
1723f8829a4aSRandall Stewart 	net = (struct sctp_nets *)tmr->net;
17248518270eSMichael Tuexen 	CURVNET_SET((struct vnet *)tmr->vnet);
1725f8829a4aSRandall Stewart 	did_output = 1;
1726f8829a4aSRandall Stewart 
1727f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1728f8829a4aSRandall Stewart 	sctp_audit_log(0xF0, (uint8_t)tmr->type);
1729f8829a4aSRandall Stewart 	sctp_auditing(3, inp, stcb, net);
1730f8829a4aSRandall Stewart #endif
1731f8829a4aSRandall Stewart 
1732f8829a4aSRandall Stewart 	/* sanity checks... */
1733f4cb790aSMichael Tuexen 	KASSERT(tmr->self == tmr,
1734f4cb790aSMichael Tuexen 	    ("sctp_timeout_handler: tmr->self corrupted"));
1735f4cb790aSMichael Tuexen 	KASSERT(SCTP_IS_TIMER_TYPE_VALID(tmr->type),
1736f4cb790aSMichael Tuexen 	    ("sctp_timeout_handler: invalid timer type %d", tmr->type));
1737a412576eSMichael Tuexen 	type = tmr->type;
1738f4cb790aSMichael Tuexen 	KASSERT(stcb == NULL || stcb->sctp_ep == inp,
1739f4cb790aSMichael Tuexen 	    ("sctp_timeout_handler of type %d: inp = %p, stcb->sctp_ep %p",
1740f4cb790aSMichael Tuexen 	    type, stcb, stcb->sctp_ep));
1741f8829a4aSRandall Stewart 	if (inp) {
1742f8829a4aSRandall Stewart 		SCTP_INP_INCR_REF(inp);
1743f8829a4aSRandall Stewart 	}
17447c63520cSMichael Tuexen 	tmr->stopped_from = 0xa001;
1745f8829a4aSRandall Stewart 	if (stcb) {
1746c105859eSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
1747f8829a4aSRandall Stewart 		if (stcb->asoc.state == 0) {
1748c105859eSRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, -1);
1749f8829a4aSRandall Stewart 			if (inp) {
1750f8829a4aSRandall Stewart 				SCTP_INP_DECR_REF(inp);
1751f8829a4aSRandall Stewart 			}
1752a412576eSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_TIMER2,
175323e3c088SMichael Tuexen 			    "Timer type %d handler exiting due to CLOSED association.\n",
1754a412576eSMichael Tuexen 			    type);
1755e056fafdSMichael Tuexen 			CURVNET_RESTORE();
1756f8829a4aSRandall Stewart 			return;
1757f8829a4aSRandall Stewart 		}
1758f8829a4aSRandall Stewart 	}
17597c63520cSMichael Tuexen 	tmr->stopped_from = 0xa002;
176037686ccfSMichael Tuexen 	SCTPDBG(SCTP_DEBUG_TIMER2, "Timer type %d goes off.\n", type);
1761139bc87fSRandall Stewart 	if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1762f8829a4aSRandall Stewart 		if (inp) {
1763f8829a4aSRandall Stewart 			SCTP_INP_DECR_REF(inp);
1764f8829a4aSRandall Stewart 		}
1765207304d4SRandall Stewart 		if (stcb) {
1766207304d4SRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, -1);
1767207304d4SRandall Stewart 		}
1768a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
176923e3c088SMichael Tuexen 		    "Timer type %d handler exiting due to not being active.\n",
1770a412576eSMichael Tuexen 		    type);
1771e056fafdSMichael Tuexen 		CURVNET_RESTORE();
1772f8829a4aSRandall Stewart 		return;
1773f8829a4aSRandall Stewart 	}
1774a5d547adSRandall Stewart 
17757c63520cSMichael Tuexen 	tmr->stopped_from = 0xa003;
1776f8829a4aSRandall Stewart 	if (stcb) {
1777f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
177850cec919SRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, -1);
1779fa89f692SMichael Tuexen 		if ((type != SCTP_TIMER_TYPE_ASOCKILL) &&
1780b54d3a6cSRandall Stewart 		    ((stcb->asoc.state == 0) ||
1781b54d3a6cSRandall Stewart 		    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED))) {
1782b54d3a6cSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
1783b54d3a6cSRandall Stewart 			if (inp) {
1784b54d3a6cSRandall Stewart 				SCTP_INP_DECR_REF(inp);
1785b54d3a6cSRandall Stewart 			}
1786a412576eSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_TIMER2,
178723e3c088SMichael Tuexen 			    "Timer type %d handler exiting due to CLOSED association.\n",
1788a412576eSMichael Tuexen 			    type);
1789e056fafdSMichael Tuexen 			CURVNET_RESTORE();
1790b54d3a6cSRandall Stewart 			return;
1791b54d3a6cSRandall Stewart 		}
17922c62ba73SMichael Tuexen 	} else if (inp != NULL) {
17932c62ba73SMichael Tuexen 		SCTP_INP_WLOCK(inp);
17942c62ba73SMichael Tuexen 	} else {
17952c62ba73SMichael Tuexen 		SCTP_WQ_ADDR_LOCK();
1796f8829a4aSRandall Stewart 	}
179744b7479bSRandall Stewart 
179837686ccfSMichael Tuexen 	/* Record in stopped_from which timeout occurred. */
179937686ccfSMichael Tuexen 	tmr->stopped_from = type;
1800868b51f2SMichael Tuexen 	NET_EPOCH_ENTER(et);
1801f8829a4aSRandall Stewart 	/* mark as being serviced now */
180244b7479bSRandall Stewart 	if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
180344b7479bSRandall Stewart 		/*
180444b7479bSRandall Stewart 		 * Callout has been rescheduled.
180544b7479bSRandall Stewart 		 */
180644b7479bSRandall Stewart 		goto get_out;
180744b7479bSRandall Stewart 	}
180844b7479bSRandall Stewart 	if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
180944b7479bSRandall Stewart 		/*
181044b7479bSRandall Stewart 		 * Not active, so no action.
181144b7479bSRandall Stewart 		 */
181244b7479bSRandall Stewart 		goto get_out;
181344b7479bSRandall Stewart 	}
1814139bc87fSRandall Stewart 	SCTP_OS_TIMER_DEACTIVATE(&tmr->timer);
1815f8829a4aSRandall Stewart 
1816f8829a4aSRandall Stewart 	/* call the handler for the appropriate timer type */
1817fa89f692SMichael Tuexen 	switch (type) {
1818f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SEND:
1819a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1820a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1821a412576eSMichael Tuexen 		    type, inp, stcb, net));
1822f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timodata);
1823f42a358aSRandall Stewart 		stcb->asoc.timodata++;
1824f8829a4aSRandall Stewart 		stcb->asoc.num_send_timers_up--;
1825f8829a4aSRandall Stewart 		if (stcb->asoc.num_send_timers_up < 0) {
1826f8829a4aSRandall Stewart 			stcb->asoc.num_send_timers_up = 0;
1827f8829a4aSRandall Stewart 		}
1828b54d3a6cSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
182960990c0cSMichael Tuexen 		if (sctp_t3rxt_timer(inp, stcb, net)) {
1830f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1831f8829a4aSRandall Stewart 
1832f8829a4aSRandall Stewart 			goto out_decr;
1833f8829a4aSRandall Stewart 		}
1834b54d3a6cSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1835f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1836f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1837f8829a4aSRandall Stewart #endif
1838ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1839f8829a4aSRandall Stewart 		if ((stcb->asoc.num_send_timers_up == 0) &&
18404a9ef3f8SMichael Tuexen 		    (stcb->asoc.sent_queue_cnt > 0)) {
1841f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *chk;
1842f8829a4aSRandall Stewart 
1843f8829a4aSRandall Stewart 			/*
1844*efd5e692SMichael Tuexen 			 * Safeguard. If there on some on the sent queue
1845f8829a4aSRandall Stewart 			 * somewhere but no timers running something is
1846f8829a4aSRandall Stewart 			 * wrong... so we start a timer on the first chunk
1847f8829a4aSRandall Stewart 			 * on the send queue on whatever net it is sent to.
1848f8829a4aSRandall Stewart 			 */
1849*efd5e692SMichael Tuexen 			TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1850*efd5e692SMichael Tuexen 				if (chk->whoTo != NULL) {
1851*efd5e692SMichael Tuexen 					break;
1852*efd5e692SMichael Tuexen 				}
1853*efd5e692SMichael Tuexen 			}
1854*efd5e692SMichael Tuexen 			if (chk != NULL) {
1855*efd5e692SMichael Tuexen 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
1856*efd5e692SMichael Tuexen 			}
1857f8829a4aSRandall Stewart 		}
1858f8829a4aSRandall Stewart 		break;
1859f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
1860a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1861a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1862a412576eSMichael Tuexen 		    type, inp, stcb, net));
1863f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoinit);
1864f42a358aSRandall Stewart 		stcb->asoc.timoinit++;
1865f8829a4aSRandall Stewart 		if (sctp_t1init_timer(inp, stcb, net)) {
1866f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1867f8829a4aSRandall Stewart 			goto out_decr;
1868f8829a4aSRandall Stewart 		}
1869f8829a4aSRandall Stewart 		/* We do output but not here */
1870f8829a4aSRandall Stewart 		did_output = 0;
1871f8829a4aSRandall Stewart 		break;
1872f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
1873a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net == NULL,
1874a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1875a412576eSMichael Tuexen 		    type, inp, stcb, net));
1876f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timosack);
1877f42a358aSRandall Stewart 		stcb->asoc.timosack++;
1878689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
1879f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1880a412576eSMichael Tuexen 		sctp_auditing(4, inp, stcb, NULL);
1881f8829a4aSRandall Stewart #endif
1882ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SACK_TMR, SCTP_SO_NOT_LOCKED);
1883f8829a4aSRandall Stewart 		break;
1884f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
1885a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1886a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1887a412576eSMichael Tuexen 		    type, inp, stcb, net));
1888a412576eSMichael Tuexen 		SCTP_STAT_INCR(sctps_timoshutdown);
1889a412576eSMichael Tuexen 		stcb->asoc.timoshutdown++;
1890f8829a4aSRandall Stewart 		if (sctp_shutdown_timer(inp, stcb, net)) {
1891f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1892f8829a4aSRandall Stewart 			goto out_decr;
1893f8829a4aSRandall Stewart 		}
1894f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1895f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1896f8829a4aSRandall Stewart #endif
1897ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_NOT_LOCKED);
1898f8829a4aSRandall Stewart 		break;
1899f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
1900a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1901a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1902a412576eSMichael Tuexen 		    type, inp, stcb, net));
1903f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoheartbeat);
1904f42a358aSRandall Stewart 		stcb->asoc.timoheartbeat++;
1905ca85e948SMichael Tuexen 		if (sctp_heartbeat_timer(inp, stcb, net)) {
1906f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1907f8829a4aSRandall Stewart 			goto out_decr;
1908f8829a4aSRandall Stewart 		}
1909f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1910ca85e948SMichael Tuexen 		sctp_auditing(4, inp, stcb, net);
1911f8829a4aSRandall Stewart #endif
1912ca85e948SMichael Tuexen 		if (!(net->dest_state & SCTP_ADDR_NOHB)) {
1913629749b6SMichael Tuexen 			sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1914ceaad40aSRandall Stewart 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_HB_TMR, SCTP_SO_NOT_LOCKED);
1915f8829a4aSRandall Stewart 		}
1916f8829a4aSRandall Stewart 		break;
1917f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
1918a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1919a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1920a412576eSMichael Tuexen 		    type, inp, stcb, net));
1921a412576eSMichael Tuexen 		SCTP_STAT_INCR(sctps_timocookie);
1922a412576eSMichael Tuexen 		stcb->asoc.timocookie++;
1923f8829a4aSRandall Stewart 		if (sctp_cookie_timer(inp, stcb, net)) {
1924f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1925f8829a4aSRandall Stewart 			goto out_decr;
1926f8829a4aSRandall Stewart 		}
1927f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1928f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1929f8829a4aSRandall Stewart #endif
1930f8829a4aSRandall Stewart 		/*
1931f8829a4aSRandall Stewart 		 * We consider T3 and Cookie timer pretty much the same with
1932f8829a4aSRandall Stewart 		 * respect to where from in chunk_output.
1933f8829a4aSRandall Stewart 		 */
1934ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1935f8829a4aSRandall Stewart 		break;
1936f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_NEWCOOKIE:
1937a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb == NULL && net == NULL,
1938a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1939a412576eSMichael Tuexen 		    type, inp, stcb, net));
1940f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timosecret);
19416e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&tv);
1942f8829a4aSRandall Stewart 		inp->sctp_ep.time_of_secret_change = tv.tv_sec;
1943f8829a4aSRandall Stewart 		inp->sctp_ep.last_secret_number =
1944f8829a4aSRandall Stewart 		    inp->sctp_ep.current_secret_number;
1945f8829a4aSRandall Stewart 		inp->sctp_ep.current_secret_number++;
1946f8829a4aSRandall Stewart 		if (inp->sctp_ep.current_secret_number >=
1947f8829a4aSRandall Stewart 		    SCTP_HOW_MANY_SECRETS) {
1948f8829a4aSRandall Stewart 			inp->sctp_ep.current_secret_number = 0;
1949f8829a4aSRandall Stewart 		}
1950f8829a4aSRandall Stewart 		secret = (int)inp->sctp_ep.current_secret_number;
1951f8829a4aSRandall Stewart 		for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1952f8829a4aSRandall Stewart 			inp->sctp_ep.secret_key[secret][i] =
1953f8829a4aSRandall Stewart 			    sctp_select_initial_TSN(&inp->sctp_ep);
1954f8829a4aSRandall Stewart 		}
19556fb7b4fbSMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
1956f8829a4aSRandall Stewart 		did_output = 0;
1957f8829a4aSRandall Stewart 		break;
1958f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
1959a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1960a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1961a412576eSMichael Tuexen 		    type, inp, stcb, net));
1962f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timopathmtu);
1963f8829a4aSRandall Stewart 		sctp_pathmtu_timer(inp, stcb, net);
1964f8829a4aSRandall Stewart 		did_output = 0;
1965f8829a4aSRandall Stewart 		break;
1966f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
1967a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1968a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1969a412576eSMichael Tuexen 		    type, inp, stcb, net));
1970f8829a4aSRandall Stewart 		if (sctp_shutdownack_timer(inp, stcb, net)) {
1971f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1972f8829a4aSRandall Stewart 			goto out_decr;
1973f8829a4aSRandall Stewart 		}
1974f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoshutdownack);
1975f42a358aSRandall Stewart 		stcb->asoc.timoshutdownack++;
1976f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1977f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1978f8829a4aSRandall Stewart #endif
1979ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_ACK_TMR, SCTP_SO_NOT_LOCKED);
1980f8829a4aSRandall Stewart 		break;
1981f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASCONF:
1982a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1983a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1984a412576eSMichael Tuexen 		    type, inp, stcb, net));
1985a412576eSMichael Tuexen 		SCTP_STAT_INCR(sctps_timoasconf);
1986f8829a4aSRandall Stewart 		if (sctp_asconf_timer(inp, stcb, net)) {
1987f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1988f8829a4aSRandall Stewart 			goto out_decr;
1989f8829a4aSRandall Stewart 		}
1990f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1991f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1992f8829a4aSRandall Stewart #endif
1993ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_ASCONF_TMR, SCTP_SO_NOT_LOCKED);
1994f8829a4aSRandall Stewart 		break;
19950554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
1996a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net == NULL,
1997a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1998a412576eSMichael Tuexen 		    type, inp, stcb, net));
19990554e01dSMichael Tuexen 		SCTP_STAT_INCR(sctps_timoshutdownguard);
20000554e01dSMichael Tuexen 		op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
20010554e01dSMichael Tuexen 		    "Shutdown guard timer expired");
20020554e01dSMichael Tuexen 		sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
20030554e01dSMichael Tuexen 		/* no need to unlock on tcb its gone */
20040554e01dSMichael Tuexen 		goto out_decr;
2005f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_AUTOCLOSE:
2006a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net == NULL,
2007a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2008a412576eSMichael Tuexen 		    type, inp, stcb, net));
2009f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoautoclose);
2010a57fb68bSMichael Tuexen 		sctp_autoclose_timer(inp, stcb);
2011ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
2012f8829a4aSRandall Stewart 		did_output = 0;
2013f8829a4aSRandall Stewart 		break;
20140554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_STRRESET:
2015a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net == NULL,
2016a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2017a412576eSMichael Tuexen 		    type, inp, stcb, net));
2018a412576eSMichael Tuexen 		SCTP_STAT_INCR(sctps_timostrmrst);
2019e95b3d7fSMichael Tuexen 		if (sctp_strreset_timer(inp, stcb)) {
20200554e01dSMichael Tuexen 			/* no need to unlock on tcb its gone */
20210554e01dSMichael Tuexen 			goto out_decr;
20220554e01dSMichael Tuexen 		}
20230554e01dSMichael Tuexen 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_TMR, SCTP_SO_NOT_LOCKED);
20240554e01dSMichael Tuexen 		break;
20250554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_INPKILL:
2026a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb == NULL && net == NULL,
2027a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2028a412576eSMichael Tuexen 		    type, inp, stcb, net));
20290554e01dSMichael Tuexen 		SCTP_STAT_INCR(sctps_timoinpkill);
20300554e01dSMichael Tuexen 		/*
20310554e01dSMichael Tuexen 		 * special case, take away our increment since WE are the
20320554e01dSMichael Tuexen 		 * killer
20330554e01dSMichael Tuexen 		 */
20340554e01dSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL,
20350554e01dSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_3);
2036a412576eSMichael Tuexen 		SCTP_INP_DECR_REF(inp);
2037a412576eSMichael Tuexen 		SCTP_INP_WUNLOCK(inp);
20380554e01dSMichael Tuexen 		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
20390554e01dSMichael Tuexen 		    SCTP_CALLED_FROM_INPKILL_TIMER);
20400554e01dSMichael Tuexen 		inp = NULL;
20410554e01dSMichael Tuexen 		goto out_no_decr;
2042f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASOCKILL:
2043a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net == NULL,
2044a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2045a412576eSMichael Tuexen 		    type, inp, stcb, net));
2046f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoassockill);
2047f8829a4aSRandall Stewart 		/* Can we free it yet? */
2048f8829a4aSRandall Stewart 		SCTP_INP_DECR_REF(inp);
2049ba785902SMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL,
2050ba785902SMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_1);
2051ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2052ceaad40aSRandall Stewart 		so = SCTP_INP_SO(inp);
2053ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
2054ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
2055ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
2056ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
2057ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
2058ceaad40aSRandall Stewart #endif
2059ba785902SMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2060ba785902SMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_2);
2061ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2062ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
2063ceaad40aSRandall Stewart #endif
2064f8829a4aSRandall Stewart 		/*
2065f8829a4aSRandall Stewart 		 * free asoc, always unlocks (or destroy's) so prevent
2066f8829a4aSRandall Stewart 		 * duplicate unlock or unlock of a free mtx :-0
2067f8829a4aSRandall Stewart 		 */
2068f8829a4aSRandall Stewart 		stcb = NULL;
2069f8829a4aSRandall Stewart 		goto out_no_decr;
20700554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ADDR_WQ:
2071a412576eSMichael Tuexen 		KASSERT(inp == NULL && stcb == NULL && net == NULL,
2072a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2073a412576eSMichael Tuexen 		    type, inp, stcb, net));
20740554e01dSMichael Tuexen 		sctp_handle_addr_wq();
20750554e01dSMichael Tuexen 		break;
20760554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_PRIM_DELETED:
2077a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net == NULL,
2078a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2079a412576eSMichael Tuexen 		    type, inp, stcb, net));
20800554e01dSMichael Tuexen 		SCTP_STAT_INCR(sctps_timodelprim);
2081a412576eSMichael Tuexen 		sctp_delete_prim_timer(inp, stcb);
20820554e01dSMichael Tuexen 		break;
2083f8829a4aSRandall Stewart 	default:
20847522682eSMichael Tuexen #ifdef INVARIANTS
2085a412576eSMichael Tuexen 		panic("Unknown timer type %d", type);
20867522682eSMichael Tuexen #else
20877522682eSMichael Tuexen 		goto get_out;
20887522682eSMichael Tuexen #endif
208960990c0cSMichael Tuexen 	}
2090f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
2091fa89f692SMichael Tuexen 	sctp_audit_log(0xF1, (uint8_t)type);
2092f8829a4aSRandall Stewart 	if (inp)
2093f8829a4aSRandall Stewart 		sctp_auditing(5, inp, stcb, net);
2094f8829a4aSRandall Stewart #endif
2095f8829a4aSRandall Stewart 	if ((did_output) && stcb) {
2096f8829a4aSRandall Stewart 		/*
2097f8829a4aSRandall Stewart 		 * Now we need to clean up the control chunk chain if an
2098f8829a4aSRandall Stewart 		 * ECNE is on it. It must be marked as UNSENT again so next
2099f8829a4aSRandall Stewart 		 * call will continue to send it until such time that we get
2100f8829a4aSRandall Stewart 		 * a CWR, to remove it. It is, however, less likely that we
2101f8829a4aSRandall Stewart 		 * will find a ecn echo on the chain though.
2102f8829a4aSRandall Stewart 		 */
2103f8829a4aSRandall Stewart 		sctp_fix_ecn_echo(&stcb->asoc);
2104f8829a4aSRandall Stewart 	}
210544b7479bSRandall Stewart get_out:
2106f8829a4aSRandall Stewart 	if (stcb) {
2107f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
21082c62ba73SMichael Tuexen 	} else if (inp != NULL) {
21092c62ba73SMichael Tuexen 		SCTP_INP_WUNLOCK(inp);
21102c62ba73SMichael Tuexen 	} else {
21112c62ba73SMichael Tuexen 		SCTP_WQ_ADDR_UNLOCK();
2112f8829a4aSRandall Stewart 	}
21132c62ba73SMichael Tuexen 
2114f8829a4aSRandall Stewart out_decr:
2115f8829a4aSRandall Stewart 	if (inp) {
2116f8829a4aSRandall Stewart 		SCTP_INP_DECR_REF(inp);
2117f8829a4aSRandall Stewart 	}
21180053ed28SMichael Tuexen 
2119f8829a4aSRandall Stewart out_no_decr:
212023e3c088SMichael Tuexen 	SCTPDBG(SCTP_DEBUG_TIMER2, "Timer type %d handler finished.\n", type);
21218518270eSMichael Tuexen 	CURVNET_RESTORE();
2122868b51f2SMichael Tuexen 	NET_EPOCH_EXIT(et);
2123f8829a4aSRandall Stewart }
2124f8829a4aSRandall Stewart 
2125a412576eSMichael Tuexen /*-
2126a412576eSMichael Tuexen  * The following table shows which parameters must be provided
2127a412576eSMichael Tuexen  * when calling sctp_timer_start(). For parameters not being
2128a412576eSMichael Tuexen  * provided, NULL must be used.
2129a412576eSMichael Tuexen  *
2130a412576eSMichael Tuexen  * |Name                         |inp |stcb|net |
2131a412576eSMichael Tuexen  * |-----------------------------|----|----|----|
2132a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SEND         |Yes |Yes |Yes |
2133a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_INIT         |Yes |Yes |Yes |
2134a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_RECV         |Yes |Yes |No  |
2135a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SHUTDOWN     |Yes |Yes |Yes |
2136a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_HEARTBEAT    |Yes |Yes |Yes |
2137a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_COOKIE       |Yes |Yes |Yes |
2138a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_NEWCOOKIE    |Yes |No  |No  |
2139a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_PATHMTURAISE |Yes |Yes |Yes |
2140a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SHUTDOWNACK  |Yes |Yes |Yes |
2141a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_ASCONF       |Yes |Yes |Yes |
2142a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SHUTDOWNGUARD|Yes |Yes |No  |
2143a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_AUTOCLOSE    |Yes |Yes |No  |
2144a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_STRRESET     |Yes |Yes |Yes |
2145a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_INPKILL      |Yes |No  |No  |
2146a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_ASOCKILL     |Yes |Yes |No  |
2147a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_ADDR_WQ      |No  |No  |No  |
2148a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_PRIM_DELETED |Yes |Yes |No  |
2149a412576eSMichael Tuexen  *
2150a412576eSMichael Tuexen  */
2151a412576eSMichael Tuexen 
2152ad81507eSRandall Stewart void
2153f8829a4aSRandall Stewart sctp_timer_start(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2154f8829a4aSRandall Stewart     struct sctp_nets *net)
2155f8829a4aSRandall Stewart {
2156f8829a4aSRandall Stewart 	struct sctp_timer *tmr;
2157a412576eSMichael Tuexen 	uint32_t to_ticks;
2158a412576eSMichael Tuexen 	uint32_t rndval, jitter;
2159f8829a4aSRandall Stewart 
2160f4cb790aSMichael Tuexen 	KASSERT(stcb == NULL || stcb->sctp_ep == inp,
2161f4cb790aSMichael Tuexen 	    ("sctp_timer_start of type %d: inp = %p, stcb->sctp_ep %p",
2162f4cb790aSMichael Tuexen 	    t_type, stcb, stcb->sctp_ep));
2163f8829a4aSRandall Stewart 	tmr = NULL;
2164a412576eSMichael Tuexen 	to_ticks = 0;
2165a412576eSMichael Tuexen 	if (stcb != NULL) {
2166f8829a4aSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
2167a412576eSMichael Tuexen 	} else if (inp != NULL) {
2168a412576eSMichael Tuexen 		SCTP_INP_WLOCK_ASSERT(inp);
2169a412576eSMichael Tuexen 	} else {
2170a412576eSMichael Tuexen 		SCTP_WQ_ADDR_LOCK_ASSERT();
2171a412576eSMichael Tuexen 	}
2172a412576eSMichael Tuexen 	if (stcb != NULL) {
2173a412576eSMichael Tuexen 		/*
2174a412576eSMichael Tuexen 		 * Don't restart timer on association that's about to be
2175a412576eSMichael Tuexen 		 * killed.
2176a412576eSMichael Tuexen 		 */
2177a412576eSMichael Tuexen 		if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
2178a412576eSMichael Tuexen 		    (t_type != SCTP_TIMER_TYPE_ASOCKILL)) {
2179a412576eSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_TIMER2,
218037686ccfSMichael Tuexen 			    "Timer type %d not started: inp=%p, stcb=%p, net=%p (stcb deleted).\n",
2181a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2182a412576eSMichael Tuexen 			return;
2183f8829a4aSRandall Stewart 		}
21849803f01cSMichael Tuexen 		/* Don't restart timer on net that's been removed. */
21859803f01cSMichael Tuexen 		if (net != NULL && (net->dest_state & SCTP_ADDR_BEING_DELETED)) {
2186a412576eSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_TIMER2,
218737686ccfSMichael Tuexen 			    "Timer type %d not started: inp=%p, stcb=%p, net=%p (net deleted).\n",
2188a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
21899803f01cSMichael Tuexen 			return;
21909803f01cSMichael Tuexen 		}
2191a412576eSMichael Tuexen 	}
2192f8829a4aSRandall Stewart 	switch (t_type) {
2193f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SEND:
2194a412576eSMichael Tuexen 		/* Here we use the RTO timer. */
2195a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2196a412576eSMichael Tuexen #ifdef INVARIANTS
2197a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2198a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2199a412576eSMichael Tuexen #else
2200ad81507eSRandall Stewart 			return;
2201a412576eSMichael Tuexen #endif
2202f8829a4aSRandall Stewart 		}
2203f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2204f8829a4aSRandall Stewart 		if (net->RTO == 0) {
220525ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2206f8829a4aSRandall Stewart 		} else {
220725ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
2208f8829a4aSRandall Stewart 		}
2209f8829a4aSRandall Stewart 		break;
2210f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
2211f8829a4aSRandall Stewart 		/*
2212f8829a4aSRandall Stewart 		 * Here we use the INIT timer default usually about 1
2213a412576eSMichael Tuexen 		 * second.
2214f8829a4aSRandall Stewart 		 */
2215a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2216a412576eSMichael Tuexen #ifdef INVARIANTS
2217a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2218a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2219a412576eSMichael Tuexen #else
2220ad81507eSRandall Stewart 			return;
2221a412576eSMichael Tuexen #endif
2222f8829a4aSRandall Stewart 		}
2223f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2224f8829a4aSRandall Stewart 		if (net->RTO == 0) {
222525ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2226f8829a4aSRandall Stewart 		} else {
222725ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
2228f8829a4aSRandall Stewart 		}
2229f8829a4aSRandall Stewart 		break;
2230f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
2231f8829a4aSRandall Stewart 		/*
2232a412576eSMichael Tuexen 		 * Here we use the Delayed-Ack timer value from the inp,
2233f8829a4aSRandall Stewart 		 * ususually about 200ms.
2234f8829a4aSRandall Stewart 		 */
2235a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2236a412576eSMichael Tuexen #ifdef INVARIANTS
2237a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2238a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2239a412576eSMichael Tuexen #else
2240ad81507eSRandall Stewart 			return;
2241a412576eSMichael Tuexen #endif
2242f8829a4aSRandall Stewart 		}
2243f8829a4aSRandall Stewart 		tmr = &stcb->asoc.dack_timer;
224425ec3553SMichael Tuexen 		to_ticks = sctp_msecs_to_ticks(stcb->asoc.delayed_ack);
2245f8829a4aSRandall Stewart 		break;
2246f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
2247f8829a4aSRandall Stewart 		/* Here we use the RTO of the destination. */
2248a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2249a412576eSMichael Tuexen #ifdef INVARIANTS
2250a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2251a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2252a412576eSMichael Tuexen #else
2253ad81507eSRandall Stewart 			return;
2254a412576eSMichael Tuexen #endif
2255f8829a4aSRandall Stewart 		}
2256a412576eSMichael Tuexen 		tmr = &net->rxt_timer;
2257f8829a4aSRandall Stewart 		if (net->RTO == 0) {
225825ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2259f8829a4aSRandall Stewart 		} else {
226025ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
2261f8829a4aSRandall Stewart 		}
2262f8829a4aSRandall Stewart 		break;
2263f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
2264f8829a4aSRandall Stewart 		/*
2265a412576eSMichael Tuexen 		 * The net is used here so that we can add in the RTO. Even
2266f8829a4aSRandall Stewart 		 * though we use a different timer. We also add the HB timer
2267f8829a4aSRandall Stewart 		 * PLUS a random jitter.
2268f8829a4aSRandall Stewart 		 */
2269a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2270a412576eSMichael Tuexen #ifdef INVARIANTS
2271a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2272a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2273a412576eSMichael Tuexen #else
2274ad81507eSRandall Stewart 			return;
2275a412576eSMichael Tuexen #endif
2276a412576eSMichael Tuexen 		}
2277ca85e948SMichael Tuexen 		if ((net->dest_state & SCTP_ADDR_NOHB) &&
2278ca85e948SMichael Tuexen 		    !(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
2279a412576eSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_TIMER2,
228037686ccfSMichael Tuexen 			    "Timer type %d not started: inp=%p, stcb=%p, net=%p.\n",
2281a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2282ad81507eSRandall Stewart 			return;
2283f8829a4aSRandall Stewart 		}
2284a412576eSMichael Tuexen 		tmr = &net->hb_timer;
2285f8829a4aSRandall Stewart 		if (net->RTO == 0) {
2286ca85e948SMichael Tuexen 			to_ticks = stcb->asoc.initial_rto;
2287f8829a4aSRandall Stewart 		} else {
2288ca85e948SMichael Tuexen 			to_ticks = net->RTO;
2289f8829a4aSRandall Stewart 		}
2290ca85e948SMichael Tuexen 		rndval = sctp_select_initial_TSN(&inp->sctp_ep);
2291ca85e948SMichael Tuexen 		jitter = rndval % to_ticks;
2292ca85e948SMichael Tuexen 		if (jitter >= (to_ticks >> 1)) {
2293ca85e948SMichael Tuexen 			to_ticks = to_ticks + (jitter - (to_ticks >> 1));
2294f8829a4aSRandall Stewart 		} else {
2295ca85e948SMichael Tuexen 			to_ticks = to_ticks - jitter;
2296f8829a4aSRandall Stewart 		}
2297ca85e948SMichael Tuexen 		if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
2298ca85e948SMichael Tuexen 		    !(net->dest_state & SCTP_ADDR_PF)) {
2299ca85e948SMichael Tuexen 			to_ticks += net->heart_beat_delay;
2300f8829a4aSRandall Stewart 		}
2301f8829a4aSRandall Stewart 		/*
2302a412576eSMichael Tuexen 		 * Now we must convert the to_ticks that are now in ms to
2303a412576eSMichael Tuexen 		 * ticks.
2304f8829a4aSRandall Stewart 		 */
230525ec3553SMichael Tuexen 		to_ticks = sctp_msecs_to_ticks(to_ticks);
2306f8829a4aSRandall Stewart 		break;
2307f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
2308f8829a4aSRandall Stewart 		/*
2309f8829a4aSRandall Stewart 		 * Here we can use the RTO timer from the network since one
2310a412576eSMichael Tuexen 		 * RTT was complete. If a retransmission happened then we
2311a412576eSMichael Tuexen 		 * will be using the RTO initial value.
2312f8829a4aSRandall Stewart 		 */
2313a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2314a412576eSMichael Tuexen #ifdef INVARIANTS
2315a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2316a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2317a412576eSMichael Tuexen #else
2318ad81507eSRandall Stewart 			return;
2319a412576eSMichael Tuexen #endif
2320f8829a4aSRandall Stewart 		}
2321a412576eSMichael Tuexen 		tmr = &net->rxt_timer;
2322f8829a4aSRandall Stewart 		if (net->RTO == 0) {
232325ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2324f8829a4aSRandall Stewart 		} else {
232525ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
2326f8829a4aSRandall Stewart 		}
2327f8829a4aSRandall Stewart 		break;
2328f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_NEWCOOKIE:
2329f8829a4aSRandall Stewart 		/*
2330a412576eSMichael Tuexen 		 * Nothing needed but the endpoint here ususually about 60
2331f8829a4aSRandall Stewart 		 * minutes.
2332f8829a4aSRandall Stewart 		 */
2333a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb != NULL) || (net != NULL)) {
2334a412576eSMichael Tuexen #ifdef INVARIANTS
2335a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2336a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2337a412576eSMichael Tuexen #else
2338a412576eSMichael Tuexen 			return;
2339a412576eSMichael Tuexen #endif
2340a412576eSMichael Tuexen 		}
2341f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
2342f8829a4aSRandall Stewart 		to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_SIGNATURE];
2343f8829a4aSRandall Stewart 		break;
2344f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
2345f8829a4aSRandall Stewart 		/*
2346a412576eSMichael Tuexen 		 * Here we use the value found in the EP for PMTUD,
2347a412576eSMichael Tuexen 		 * ususually about 10 minutes.
2348f8829a4aSRandall Stewart 		 */
2349a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2350a412576eSMichael Tuexen #ifdef INVARIANTS
2351a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2352a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2353a412576eSMichael Tuexen #else
2354ad81507eSRandall Stewart 			return;
2355a412576eSMichael Tuexen #endif
2356f8829a4aSRandall Stewart 		}
235780c79bbeSMichael Tuexen 		if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
2358a412576eSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_TIMER2,
235937686ccfSMichael Tuexen 			    "Timer type %d not started: inp=%p, stcb=%p, net=%p.\n",
2360a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
236180c79bbeSMichael Tuexen 			return;
236280c79bbeSMichael Tuexen 		}
2363f8829a4aSRandall Stewart 		tmr = &net->pmtu_timer;
2364a412576eSMichael Tuexen 		to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_PMTU];
2365f8829a4aSRandall Stewart 		break;
2366f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
2367a412576eSMichael Tuexen 		/* Here we use the RTO of the destination. */
2368a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2369a412576eSMichael Tuexen #ifdef INVARIANTS
2370a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2371a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2372a412576eSMichael Tuexen #else
2373ad81507eSRandall Stewart 			return;
2374a412576eSMichael Tuexen #endif
2375f8829a4aSRandall Stewart 		}
2376a412576eSMichael Tuexen 		tmr = &net->rxt_timer;
2377f8829a4aSRandall Stewart 		if (net->RTO == 0) {
237825ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2379f8829a4aSRandall Stewart 		} else {
238025ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
2381f8829a4aSRandall Stewart 		}
2382f8829a4aSRandall Stewart 		break;
23830554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ASCONF:
23840554e01dSMichael Tuexen 		/*
23850554e01dSMichael Tuexen 		 * Here the timer comes from the stcb but its value is from
23860554e01dSMichael Tuexen 		 * the net's RTO.
23870554e01dSMichael Tuexen 		 */
2388a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2389a412576eSMichael Tuexen #ifdef INVARIANTS
2390a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2391a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2392a412576eSMichael Tuexen #else
23930554e01dSMichael Tuexen 			return;
2394a412576eSMichael Tuexen #endif
23950554e01dSMichael Tuexen 		}
2396a412576eSMichael Tuexen 		tmr = &stcb->asoc.asconf_timer;
23970554e01dSMichael Tuexen 		if (net->RTO == 0) {
239825ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
23990554e01dSMichael Tuexen 		} else {
240025ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
24010554e01dSMichael Tuexen 		}
24020554e01dSMichael Tuexen 		break;
2403f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2404f8829a4aSRandall Stewart 		/*
2405f8829a4aSRandall Stewart 		 * Here we use the endpoints shutdown guard timer usually
2406f8829a4aSRandall Stewart 		 * about 3 minutes.
2407f8829a4aSRandall Stewart 		 */
2408a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2409a412576eSMichael Tuexen #ifdef INVARIANTS
2410a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2411a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2412a412576eSMichael Tuexen #else
2413ad81507eSRandall Stewart 			return;
2414a412576eSMichael Tuexen #endif
2415f8829a4aSRandall Stewart 		}
2416a412576eSMichael Tuexen 		tmr = &stcb->asoc.shut_guard_timer;
24172e2d6794SMichael Tuexen 		if (inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] == 0) {
241825ec3553SMichael Tuexen 			if (stcb->asoc.maxrto < UINT32_MAX / 5) {
241925ec3553SMichael Tuexen 				to_ticks = sctp_msecs_to_ticks(5 * stcb->asoc.maxrto);
242025ec3553SMichael Tuexen 			} else {
242125ec3553SMichael Tuexen 				to_ticks = sctp_msecs_to_ticks(UINT32_MAX);
242225ec3553SMichael Tuexen 			}
24232e2d6794SMichael Tuexen 		} else {
2424f8829a4aSRandall Stewart 			to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN];
24252e2d6794SMichael Tuexen 		}
2426f8829a4aSRandall Stewart 		break;
24270554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_AUTOCLOSE:
2428a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2429a412576eSMichael Tuexen #ifdef INVARIANTS
2430a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2431a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2432a412576eSMichael Tuexen #else
24330554e01dSMichael Tuexen 			return;
2434a412576eSMichael Tuexen #endif
24350554e01dSMichael Tuexen 		}
24360554e01dSMichael Tuexen 		tmr = &stcb->asoc.autoclose_timer;
2437a412576eSMichael Tuexen 		to_ticks = stcb->asoc.sctp_autoclose_ticks;
24380554e01dSMichael Tuexen 		break;
2439f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_STRRESET:
2440f8829a4aSRandall Stewart 		/*
24411b649582SRandall Stewart 		 * Here the timer comes from the stcb but its value is from
24421b649582SRandall Stewart 		 * the net's RTO.
2443f8829a4aSRandall Stewart 		 */
2444a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2445a412576eSMichael Tuexen #ifdef INVARIANTS
2446a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2447a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2448a412576eSMichael Tuexen #else
2449ad81507eSRandall Stewart 			return;
2450a412576eSMichael Tuexen #endif
2451f8829a4aSRandall Stewart 		}
2452a412576eSMichael Tuexen 		tmr = &stcb->asoc.strreset_timer;
2453f8829a4aSRandall Stewart 		if (net->RTO == 0) {
245425ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2455f8829a4aSRandall Stewart 		} else {
245625ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
2457f8829a4aSRandall Stewart 		}
2458f8829a4aSRandall Stewart 		break;
24590554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_INPKILL:
2460f8829a4aSRandall Stewart 		/*
24610554e01dSMichael Tuexen 		 * The inp is setup to die. We re-use the signature_chage
24620554e01dSMichael Tuexen 		 * timer since that has stopped and we are in the GONE
24630554e01dSMichael Tuexen 		 * state.
2464f8829a4aSRandall Stewart 		 */
2465a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb != NULL) || (net != NULL)) {
2466a412576eSMichael Tuexen #ifdef INVARIANTS
2467a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2468a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2469a412576eSMichael Tuexen #else
2470a412576eSMichael Tuexen 			return;
2471a412576eSMichael Tuexen #endif
2472a412576eSMichael Tuexen 		}
24730554e01dSMichael Tuexen 		tmr = &inp->sctp_ep.signature_change;
247425ec3553SMichael Tuexen 		to_ticks = sctp_msecs_to_ticks(SCTP_INP_KILL_TIMEOUT);
24750554e01dSMichael Tuexen 		break;
24760554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ASOCKILL:
2477a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2478a412576eSMichael Tuexen #ifdef INVARIANTS
2479a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2480a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2481a412576eSMichael Tuexen #else
2482ad81507eSRandall Stewart 			return;
2483a412576eSMichael Tuexen #endif
2484f8829a4aSRandall Stewart 		}
24850554e01dSMichael Tuexen 		tmr = &stcb->asoc.strreset_timer;
248625ec3553SMichael Tuexen 		to_ticks = sctp_msecs_to_ticks(SCTP_ASOC_KILL_TIMEOUT);
24870554e01dSMichael Tuexen 		break;
24880554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ADDR_WQ:
2489a412576eSMichael Tuexen 		if ((inp != NULL) || (stcb != NULL) || (net != NULL)) {
2490a412576eSMichael Tuexen #ifdef INVARIANTS
2491a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2492a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2493a412576eSMichael Tuexen #else
2494a412576eSMichael Tuexen 			return;
2495a412576eSMichael Tuexen #endif
2496a412576eSMichael Tuexen 		}
24970554e01dSMichael Tuexen 		/* Only 1 tick away :-) */
24980554e01dSMichael Tuexen 		tmr = &SCTP_BASE_INFO(addr_wq_timer);
24990554e01dSMichael Tuexen 		to_ticks = SCTP_ADDRESS_TICK_DELAY;
2500f8829a4aSRandall Stewart 		break;
2501851b7298SRandall Stewart 	case SCTP_TIMER_TYPE_PRIM_DELETED:
2502a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2503a412576eSMichael Tuexen #ifdef INVARIANTS
2504a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2505a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2506a412576eSMichael Tuexen #else
2507851b7298SRandall Stewart 			return;
2508a412576eSMichael Tuexen #endif
2509851b7298SRandall Stewart 		}
2510851b7298SRandall Stewart 		tmr = &stcb->asoc.delete_prim_timer;
251125ec3553SMichael Tuexen 		to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2512851b7298SRandall Stewart 		break;
2513f8829a4aSRandall Stewart 	default:
25147522682eSMichael Tuexen #ifdef INVARIANTS
2515a412576eSMichael Tuexen 		panic("Unknown timer type %d", t_type);
25167522682eSMichael Tuexen #else
25177522682eSMichael Tuexen 		return;
25187522682eSMichael Tuexen #endif
251960990c0cSMichael Tuexen 	}
2520a412576eSMichael Tuexen 	KASSERT(tmr != NULL, ("tmr is NULL for timer type %d", t_type));
2521a412576eSMichael Tuexen 	KASSERT(to_ticks > 0, ("to_ticks == 0 for timer type %d", t_type));
2522139bc87fSRandall Stewart 	if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
2523f8829a4aSRandall Stewart 		/*
2524a412576eSMichael Tuexen 		 * We do NOT allow you to have it already running. If it is,
2525a412576eSMichael Tuexen 		 * we leave the current one up unchanged.
2526f8829a4aSRandall Stewart 		 */
2527a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
252837686ccfSMichael Tuexen 		    "Timer type %d already running: inp=%p, stcb=%p, net=%p.\n",
2529a412576eSMichael Tuexen 		    t_type, inp, stcb, net);
2530ad81507eSRandall Stewart 		return;
2531f8829a4aSRandall Stewart 	}
2532a412576eSMichael Tuexen 	/* At this point we can proceed. */
2533f8829a4aSRandall Stewart 	if (t_type == SCTP_TIMER_TYPE_SEND) {
2534f8829a4aSRandall Stewart 		stcb->asoc.num_send_timers_up++;
2535f8829a4aSRandall Stewart 	}
2536a5d547adSRandall Stewart 	tmr->stopped_from = 0;
2537f8829a4aSRandall Stewart 	tmr->type = t_type;
2538f8829a4aSRandall Stewart 	tmr->ep = (void *)inp;
2539f8829a4aSRandall Stewart 	tmr->tcb = (void *)stcb;
2540a412576eSMichael Tuexen 	if (t_type == SCTP_TIMER_TYPE_STRRESET) {
2541a412576eSMichael Tuexen 		tmr->net = NULL;
2542a412576eSMichael Tuexen 	} else {
2543f8829a4aSRandall Stewart 		tmr->net = (void *)net;
2544a412576eSMichael Tuexen 	}
2545f8829a4aSRandall Stewart 	tmr->self = (void *)tmr;
25468518270eSMichael Tuexen 	tmr->vnet = (void *)curvnet;
2547c4739e2fSRandall Stewart 	tmr->ticks = sctp_get_tick_count();
2548a412576eSMichael Tuexen 	if (SCTP_OS_TIMER_START(&tmr->timer, to_ticks, sctp_timeout_handler, tmr) == 0) {
2549a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
255037686ccfSMichael Tuexen 		    "Timer type %d started: ticks=%u, inp=%p, stcb=%p, net=%p.\n",
2551a412576eSMichael Tuexen 		    t_type, to_ticks, inp, stcb, net);
2552a412576eSMichael Tuexen 	} else {
2553a412576eSMichael Tuexen 		/*
2554a412576eSMichael Tuexen 		 * This should not happen, since we checked for pending
2555a412576eSMichael Tuexen 		 * above.
2556a412576eSMichael Tuexen 		 */
2557a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
255837686ccfSMichael Tuexen 		    "Timer type %d restarted: ticks=%u, inp=%p, stcb=%p, net=%p.\n",
2559a412576eSMichael Tuexen 		    t_type, to_ticks, inp, stcb, net);
2560a412576eSMichael Tuexen 	}
2561ad81507eSRandall Stewart 	return;
2562f8829a4aSRandall Stewart }
2563f8829a4aSRandall Stewart 
2564a412576eSMichael Tuexen /*-
2565a412576eSMichael Tuexen  * The following table shows which parameters must be provided
2566a412576eSMichael Tuexen  * when calling sctp_timer_stop(). For parameters not being
2567a412576eSMichael Tuexen  * provided, NULL must be used.
2568a412576eSMichael Tuexen  *
2569a412576eSMichael Tuexen  * |Name                         |inp |stcb|net |
2570a412576eSMichael Tuexen  * |-----------------------------|----|----|----|
2571a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SEND         |Yes |Yes |Yes |
2572a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_INIT         |Yes |Yes |Yes |
2573a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_RECV         |Yes |Yes |No  |
2574a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SHUTDOWN     |Yes |Yes |Yes |
2575a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_HEARTBEAT    |Yes |Yes |Yes |
2576a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_COOKIE       |Yes |Yes |Yes |
2577a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_NEWCOOKIE    |Yes |No  |No  |
2578a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_PATHMTURAISE |Yes |Yes |Yes |
2579a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SHUTDOWNACK  |Yes |Yes |Yes |
2580a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_ASCONF       |Yes |Yes |No  |
2581a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SHUTDOWNGUARD|Yes |Yes |No  |
2582a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_AUTOCLOSE    |Yes |Yes |No  |
2583a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_STRRESET     |Yes |Yes |No  |
2584a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_INPKILL      |Yes |No  |No  |
2585a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_ASOCKILL     |Yes |Yes |No  |
2586a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_ADDR_WQ      |No  |No  |No  |
2587a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_PRIM_DELETED |Yes |Yes |No  |
2588a412576eSMichael Tuexen  *
2589a412576eSMichael Tuexen  */
2590a412576eSMichael Tuexen 
25916e55db54SRandall Stewart void
2592f8829a4aSRandall Stewart sctp_timer_stop(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2593a5d547adSRandall Stewart     struct sctp_nets *net, uint32_t from)
2594f8829a4aSRandall Stewart {
2595f8829a4aSRandall Stewart 	struct sctp_timer *tmr;
2596f8829a4aSRandall Stewart 
2597f4cb790aSMichael Tuexen 	KASSERT(stcb == NULL || stcb->sctp_ep == inp,
2598f4cb790aSMichael Tuexen 	    ("sctp_timer_stop of type %d: inp = %p, stcb->sctp_ep %p",
2599f4cb790aSMichael Tuexen 	    t_type, stcb, stcb->sctp_ep));
2600a412576eSMichael Tuexen 	if (stcb != NULL) {
2601f8829a4aSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
2602a412576eSMichael Tuexen 	} else if (inp != NULL) {
2603a412576eSMichael Tuexen 		SCTP_INP_WLOCK_ASSERT(inp);
2604a412576eSMichael Tuexen 	} else {
2605a412576eSMichael Tuexen 		SCTP_WQ_ADDR_LOCK_ASSERT();
2606f8829a4aSRandall Stewart 	}
2607a412576eSMichael Tuexen 	tmr = NULL;
2608f8829a4aSRandall Stewart 	switch (t_type) {
2609f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SEND:
2610a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2611a412576eSMichael Tuexen #ifdef INVARIANTS
2612a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2613a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2614a412576eSMichael Tuexen #else
26156e55db54SRandall Stewart 			return;
2616a412576eSMichael Tuexen #endif
2617f8829a4aSRandall Stewart 		}
2618f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2619f8829a4aSRandall Stewart 		break;
2620f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
2621a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2622a412576eSMichael Tuexen #ifdef INVARIANTS
2623a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2624a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2625a412576eSMichael Tuexen #else
26266e55db54SRandall Stewart 			return;
2627a412576eSMichael Tuexen #endif
2628f8829a4aSRandall Stewart 		}
2629f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2630f8829a4aSRandall Stewart 		break;
2631f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
2632a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2633a412576eSMichael Tuexen #ifdef INVARIANTS
2634a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2635a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2636a412576eSMichael Tuexen #else
26376e55db54SRandall Stewart 			return;
2638a412576eSMichael Tuexen #endif
2639f8829a4aSRandall Stewart 		}
2640f8829a4aSRandall Stewart 		tmr = &stcb->asoc.dack_timer;
2641f8829a4aSRandall Stewart 		break;
2642f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
2643a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2644a412576eSMichael Tuexen #ifdef INVARIANTS
2645a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2646a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2647a412576eSMichael Tuexen #else
26486e55db54SRandall Stewart 			return;
2649a412576eSMichael Tuexen #endif
2650f8829a4aSRandall Stewart 		}
2651f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2652f8829a4aSRandall Stewart 		break;
2653f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
2654a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2655a412576eSMichael Tuexen #ifdef INVARIANTS
2656a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2657a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2658a412576eSMichael Tuexen #else
26596e55db54SRandall Stewart 			return;
2660a412576eSMichael Tuexen #endif
2661f8829a4aSRandall Stewart 		}
2662ca85e948SMichael Tuexen 		tmr = &net->hb_timer;
2663f8829a4aSRandall Stewart 		break;
2664f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
2665a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2666a412576eSMichael Tuexen #ifdef INVARIANTS
2667a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2668a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2669a412576eSMichael Tuexen #else
26706e55db54SRandall Stewart 			return;
2671a412576eSMichael Tuexen #endif
2672f8829a4aSRandall Stewart 		}
2673f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2674f8829a4aSRandall Stewart 		break;
2675f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_NEWCOOKIE:
2676a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb != NULL) || (net != NULL)) {
2677a412576eSMichael Tuexen #ifdef INVARIANTS
2678a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2679a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2680a412576eSMichael Tuexen #else
2681a412576eSMichael Tuexen 			return;
2682a412576eSMichael Tuexen #endif
2683a412576eSMichael Tuexen 		}
2684f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
2685f8829a4aSRandall Stewart 		break;
2686f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
2687a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2688a412576eSMichael Tuexen #ifdef INVARIANTS
2689a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2690a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2691a412576eSMichael Tuexen #else
26926e55db54SRandall Stewart 			return;
2693a412576eSMichael Tuexen #endif
2694f8829a4aSRandall Stewart 		}
2695f8829a4aSRandall Stewart 		tmr = &net->pmtu_timer;
2696f8829a4aSRandall Stewart 		break;
2697f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
2698a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2699a412576eSMichael Tuexen #ifdef INVARIANTS
2700a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2701a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2702a412576eSMichael Tuexen #else
27036e55db54SRandall Stewart 			return;
2704a412576eSMichael Tuexen #endif
2705f8829a4aSRandall Stewart 		}
2706f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2707f8829a4aSRandall Stewart 		break;
27080554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ASCONF:
2709a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2710a412576eSMichael Tuexen #ifdef INVARIANTS
2711a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2712a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2713a412576eSMichael Tuexen #else
27140554e01dSMichael Tuexen 			return;
2715a412576eSMichael Tuexen #endif
27160554e01dSMichael Tuexen 		}
27170554e01dSMichael Tuexen 		tmr = &stcb->asoc.asconf_timer;
27180554e01dSMichael Tuexen 		break;
2719f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2720a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2721a412576eSMichael Tuexen #ifdef INVARIANTS
2722a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2723a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2724a412576eSMichael Tuexen #else
27256e55db54SRandall Stewart 			return;
2726a412576eSMichael Tuexen #endif
2727f8829a4aSRandall Stewart 		}
2728f8829a4aSRandall Stewart 		tmr = &stcb->asoc.shut_guard_timer;
2729f8829a4aSRandall Stewart 		break;
27300554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_AUTOCLOSE:
2731a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2732a412576eSMichael Tuexen #ifdef INVARIANTS
2733a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2734a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2735a412576eSMichael Tuexen #else
27360554e01dSMichael Tuexen 			return;
2737a412576eSMichael Tuexen #endif
27380554e01dSMichael Tuexen 		}
27390554e01dSMichael Tuexen 		tmr = &stcb->asoc.autoclose_timer;
27400554e01dSMichael Tuexen 		break;
2741f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_STRRESET:
2742a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2743a412576eSMichael Tuexen #ifdef INVARIANTS
2744a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2745a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2746a412576eSMichael Tuexen #else
27476e55db54SRandall Stewart 			return;
2748a412576eSMichael Tuexen #endif
2749f8829a4aSRandall Stewart 		}
2750f8829a4aSRandall Stewart 		tmr = &stcb->asoc.strreset_timer;
2751f8829a4aSRandall Stewart 		break;
27520554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_INPKILL:
27530554e01dSMichael Tuexen 		/*
27540554e01dSMichael Tuexen 		 * The inp is setup to die. We re-use the signature_chage
27550554e01dSMichael Tuexen 		 * timer since that has stopped and we are in the GONE
27560554e01dSMichael Tuexen 		 * state.
27570554e01dSMichael Tuexen 		 */
2758a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb != NULL) || (net != NULL)) {
2759a412576eSMichael Tuexen #ifdef INVARIANTS
2760a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2761a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2762a412576eSMichael Tuexen #else
2763a412576eSMichael Tuexen 			return;
2764a412576eSMichael Tuexen #endif
2765a412576eSMichael Tuexen 		}
27660554e01dSMichael Tuexen 		tmr = &inp->sctp_ep.signature_change;
27670554e01dSMichael Tuexen 		break;
27680554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ASOCKILL:
2769a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2770a412576eSMichael Tuexen #ifdef INVARIANTS
2771a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2772a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2773a412576eSMichael Tuexen #else
27746e55db54SRandall Stewart 			return;
2775a412576eSMichael Tuexen #endif
2776f8829a4aSRandall Stewart 		}
27770554e01dSMichael Tuexen 		tmr = &stcb->asoc.strreset_timer;
27780554e01dSMichael Tuexen 		break;
27790554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ADDR_WQ:
2780a412576eSMichael Tuexen 		if ((inp != NULL) || (stcb != NULL) || (net != NULL)) {
2781a412576eSMichael Tuexen #ifdef INVARIANTS
2782a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2783a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2784a412576eSMichael Tuexen #else
2785a412576eSMichael Tuexen 			return;
2786a412576eSMichael Tuexen #endif
2787a412576eSMichael Tuexen 		}
27880554e01dSMichael Tuexen 		tmr = &SCTP_BASE_INFO(addr_wq_timer);
2789f8829a4aSRandall Stewart 		break;
2790851b7298SRandall Stewart 	case SCTP_TIMER_TYPE_PRIM_DELETED:
2791a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2792a412576eSMichael Tuexen #ifdef INVARIANTS
2793a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2794a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2795a412576eSMichael Tuexen #else
2796851b7298SRandall Stewart 			return;
2797a412576eSMichael Tuexen #endif
2798851b7298SRandall Stewart 		}
2799851b7298SRandall Stewart 		tmr = &stcb->asoc.delete_prim_timer;
2800851b7298SRandall Stewart 		break;
2801f8829a4aSRandall Stewart 	default:
28027522682eSMichael Tuexen #ifdef INVARIANTS
2803a412576eSMichael Tuexen 		panic("Unknown timer type %d", t_type);
28047522682eSMichael Tuexen #else
28057522682eSMichael Tuexen 		return;
28067522682eSMichael Tuexen #endif
280760990c0cSMichael Tuexen 	}
2808a412576eSMichael Tuexen 	KASSERT(tmr != NULL, ("tmr is NULL for timer type %d", t_type));
2809a412576eSMichael Tuexen 	if ((tmr->type != SCTP_TIMER_TYPE_NONE) &&
2810a412576eSMichael Tuexen 	    (tmr->type != t_type)) {
2811f8829a4aSRandall Stewart 		/*
2812f8829a4aSRandall Stewart 		 * Ok we have a timer that is under joint use. Cookie timer
2813f8829a4aSRandall Stewart 		 * per chance with the SEND timer. We therefore are NOT
2814f8829a4aSRandall Stewart 		 * running the timer that the caller wants stopped.  So just
2815f8829a4aSRandall Stewart 		 * return.
2816f8829a4aSRandall Stewart 		 */
2817a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
281837686ccfSMichael Tuexen 		    "Shared timer type %d not running: inp=%p, stcb=%p, net=%p.\n",
2819a412576eSMichael Tuexen 		    t_type, inp, stcb, net);
28206e55db54SRandall Stewart 		return;
2821f8829a4aSRandall Stewart 	}
2822ad81507eSRandall Stewart 	if ((t_type == SCTP_TIMER_TYPE_SEND) && (stcb != NULL)) {
2823f8829a4aSRandall Stewart 		stcb->asoc.num_send_timers_up--;
2824f8829a4aSRandall Stewart 		if (stcb->asoc.num_send_timers_up < 0) {
2825f8829a4aSRandall Stewart 			stcb->asoc.num_send_timers_up = 0;
2826f8829a4aSRandall Stewart 		}
2827f8829a4aSRandall Stewart 	}
2828f8829a4aSRandall Stewart 	tmr->self = NULL;
2829a5d547adSRandall Stewart 	tmr->stopped_from = from;
2830a412576eSMichael Tuexen 	if (SCTP_OS_TIMER_STOP(&tmr->timer) == 1) {
2831a412576eSMichael Tuexen 		KASSERT(tmr->ep == inp,
2832a412576eSMichael Tuexen 		    ("sctp_timer_stop of type %d: inp = %p, tmr->inp = %p",
2833a412576eSMichael Tuexen 		    t_type, inp, tmr->ep));
2834a412576eSMichael Tuexen 		KASSERT(tmr->tcb == stcb,
2835a412576eSMichael Tuexen 		    ("sctp_timer_stop of type %d: stcb = %p, tmr->stcb = %p",
2836a412576eSMichael Tuexen 		    t_type, stcb, tmr->tcb));
2837a412576eSMichael Tuexen 		KASSERT(((t_type == SCTP_TIMER_TYPE_ASCONF) && (tmr->net != NULL)) ||
2838a412576eSMichael Tuexen 		    ((t_type != SCTP_TIMER_TYPE_ASCONF) && (tmr->net == net)),
2839a412576eSMichael Tuexen 		    ("sctp_timer_stop of type %d: net = %p, tmr->net = %p",
2840a412576eSMichael Tuexen 		    t_type, net, tmr->net));
2841a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
284237686ccfSMichael Tuexen 		    "Timer type %d stopped: inp=%p, stcb=%p, net=%p.\n",
2843a412576eSMichael Tuexen 		    t_type, inp, stcb, net);
2844a412576eSMichael Tuexen 		tmr->ep = NULL;
2845a412576eSMichael Tuexen 		tmr->tcb = NULL;
2846a412576eSMichael Tuexen 		tmr->net = NULL;
2847a412576eSMichael Tuexen 	} else {
2848a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
284937686ccfSMichael Tuexen 		    "Timer type %d not stopped: inp=%p, stcb=%p, net=%p.\n",
2850a412576eSMichael Tuexen 		    t_type, inp, stcb, net);
2851a412576eSMichael Tuexen 	}
28526e55db54SRandall Stewart 	return;
2853f8829a4aSRandall Stewart }
2854f8829a4aSRandall Stewart 
2855f8829a4aSRandall Stewart uint32_t
2856b0471b4bSMichael Tuexen sctp_calculate_len(struct mbuf *m)
2857b0471b4bSMichael Tuexen {
2858f8829a4aSRandall Stewart 	uint32_t tlen = 0;
2859f8829a4aSRandall Stewart 	struct mbuf *at;
2860f8829a4aSRandall Stewart 
2861f8829a4aSRandall Stewart 	at = m;
2862f8829a4aSRandall Stewart 	while (at) {
2863139bc87fSRandall Stewart 		tlen += SCTP_BUF_LEN(at);
2864139bc87fSRandall Stewart 		at = SCTP_BUF_NEXT(at);
2865f8829a4aSRandall Stewart 	}
2866f8829a4aSRandall Stewart 	return (tlen);
2867f8829a4aSRandall Stewart }
2868f8829a4aSRandall Stewart 
2869f8829a4aSRandall Stewart void
2870f8829a4aSRandall Stewart sctp_mtu_size_reset(struct sctp_inpcb *inp,
287144b7479bSRandall Stewart     struct sctp_association *asoc, uint32_t mtu)
2872f8829a4aSRandall Stewart {
2873f8829a4aSRandall Stewart 	/*
2874f8829a4aSRandall Stewart 	 * Reset the P-MTU size on this association, this involves changing
2875f8829a4aSRandall Stewart 	 * the asoc MTU, going through ANY chunk+overhead larger than mtu to
2876f8829a4aSRandall Stewart 	 * allow the DF flag to be cleared.
2877f8829a4aSRandall Stewart 	 */
2878f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
2879f8829a4aSRandall Stewart 	unsigned int eff_mtu, ovh;
2880f8829a4aSRandall Stewart 
2881f8829a4aSRandall Stewart 	asoc->smallest_mtu = mtu;
2882f8829a4aSRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2883f8829a4aSRandall Stewart 		ovh = SCTP_MIN_OVERHEAD;
2884f8829a4aSRandall Stewart 	} else {
2885f8829a4aSRandall Stewart 		ovh = SCTP_MIN_V4_OVERHEAD;
2886f8829a4aSRandall Stewart 	}
2887f8829a4aSRandall Stewart 	eff_mtu = mtu - ovh;
2888f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
2889f8829a4aSRandall Stewart 		if (chk->send_size > eff_mtu) {
2890f8829a4aSRandall Stewart 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2891f8829a4aSRandall Stewart 		}
2892f8829a4aSRandall Stewart 	}
2893f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
2894f8829a4aSRandall Stewart 		if (chk->send_size > eff_mtu) {
2895f8829a4aSRandall Stewart 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2896f8829a4aSRandall Stewart 		}
2897f8829a4aSRandall Stewart 	}
2898f8829a4aSRandall Stewart }
2899f8829a4aSRandall Stewart 
2900f8829a4aSRandall Stewart 
2901f8829a4aSRandall Stewart /*
290244f2a327SMichael Tuexen  * Given an association and starting time of the current RTT period, update
290344f2a327SMichael Tuexen  * RTO in number of msecs. net should point to the current network.
290444f2a327SMichael Tuexen  * Return 1, if an RTO update was performed, return 0 if no update was
290544f2a327SMichael Tuexen  * performed due to invalid starting point.
2906f8829a4aSRandall Stewart  */
2907899288aeSRandall Stewart 
290844f2a327SMichael Tuexen int
2909f8829a4aSRandall Stewart sctp_calculate_rto(struct sctp_tcb *stcb,
2910f8829a4aSRandall Stewart     struct sctp_association *asoc,
2911f8829a4aSRandall Stewart     struct sctp_nets *net,
29128c8e10b7SMichael Tuexen     struct timeval *old,
2913b0471b4bSMichael Tuexen     int rtt_from_sack)
2914b0471b4bSMichael Tuexen {
291544f2a327SMichael Tuexen 	struct timeval now;
291644f2a327SMichael Tuexen 	uint64_t rtt_us;	/* RTT in us */
2917be1d9176SMichael Tuexen 	int32_t rtt;		/* RTT in ms */
2918be1d9176SMichael Tuexen 	uint32_t new_rto;
2919f8829a4aSRandall Stewart 	int first_measure = 0;
2920f8829a4aSRandall Stewart 
2921f8829a4aSRandall Stewart 	/************************/
2922f8829a4aSRandall Stewart 	/* 1. calculate new RTT */
2923f8829a4aSRandall Stewart 	/************************/
2924f8829a4aSRandall Stewart 	/* get the current time */
2925299108c5SRandall Stewart 	if (stcb->asoc.use_precise_time) {
2926299108c5SRandall Stewart 		(void)SCTP_GETPTIME_TIMEVAL(&now);
2927299108c5SRandall Stewart 	} else {
29286e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
2929299108c5SRandall Stewart 	}
293044f2a327SMichael Tuexen 	if ((old->tv_sec > now.tv_sec) ||
293144f2a327SMichael Tuexen 	    ((old->tv_sec == now.tv_sec) && (old->tv_sec > now.tv_sec))) {
293244f2a327SMichael Tuexen 		/* The starting point is in the future. */
293344f2a327SMichael Tuexen 		return (0);
293444f2a327SMichael Tuexen 	}
2935be1d9176SMichael Tuexen 	timevalsub(&now, old);
293644f2a327SMichael Tuexen 	rtt_us = (uint64_t)1000000 * (uint64_t)now.tv_sec + (uint64_t)now.tv_usec;
293744f2a327SMichael Tuexen 	if (rtt_us > SCTP_RTO_UPPER_BOUND * 1000) {
293844f2a327SMichael Tuexen 		/* The RTT is larger than a sane value. */
293944f2a327SMichael Tuexen 		return (0);
294044f2a327SMichael Tuexen 	}
2941be1d9176SMichael Tuexen 	/* store the current RTT in us */
294244f2a327SMichael Tuexen 	net->rtt = rtt_us;
2943b60b0fe6SMichael Tuexen 	/* compute rtt in ms */
2944b60b0fe6SMichael Tuexen 	rtt = (int32_t)(net->rtt / 1000);
2945f79aab18SRandall Stewart 	if ((asoc->cc_functions.sctp_rtt_calculated) && (rtt_from_sack == SCTP_RTT_FROM_DATA)) {
2946b7b84c0eSMichael Tuexen 		/*
2947b7b84c0eSMichael Tuexen 		 * Tell the CC module that a new update has just occurred
2948b7b84c0eSMichael Tuexen 		 * from a sack
2949b7b84c0eSMichael Tuexen 		 */
2950f79aab18SRandall Stewart 		(*asoc->cc_functions.sctp_rtt_calculated) (stcb, net, &now);
2951f79aab18SRandall Stewart 	}
2952f79aab18SRandall Stewart 	/*
2953f79aab18SRandall Stewart 	 * Do we need to determine the lan? We do this only on sacks i.e.
2954f79aab18SRandall Stewart 	 * RTT being determined from data not non-data (HB/INIT->INITACK).
2955f79aab18SRandall Stewart 	 */
2956f79aab18SRandall Stewart 	if ((rtt_from_sack == SCTP_RTT_FROM_DATA) &&
2957be1d9176SMichael Tuexen 	    (net->lan_type == SCTP_LAN_UNKNOWN)) {
2958be1d9176SMichael Tuexen 		if (net->rtt > SCTP_LOCAL_LAN_RTT) {
2959899288aeSRandall Stewart 			net->lan_type = SCTP_LAN_INTERNET;
2960899288aeSRandall Stewart 		} else {
2961899288aeSRandall Stewart 			net->lan_type = SCTP_LAN_LOCAL;
2962899288aeSRandall Stewart 		}
2963899288aeSRandall Stewart 	}
29640053ed28SMichael Tuexen 
2965f8829a4aSRandall Stewart 	/***************************/
2966f8829a4aSRandall Stewart 	/* 2. update RTTVAR & SRTT */
2967f8829a4aSRandall Stewart 	/***************************/
2968be1d9176SMichael Tuexen 	/*-
2969be1d9176SMichael Tuexen 	 * Compute the scaled average lastsa and the
2970be1d9176SMichael Tuexen 	 * scaled variance lastsv as described in van Jacobson
2971be1d9176SMichael Tuexen 	 * Paper "Congestion Avoidance and Control", Annex A.
2972be1d9176SMichael Tuexen 	 *
2973be1d9176SMichael Tuexen 	 * (net->lastsa >> SCTP_RTT_SHIFT) is the srtt
297444f2a327SMichael Tuexen 	 * (net->lastsv >> SCTP_RTT_VAR_SHIFT) is the rttvar
2975be1d9176SMichael Tuexen 	 */
29769a972525SRandall Stewart 	if (net->RTO_measured) {
2977be1d9176SMichael Tuexen 		rtt -= (net->lastsa >> SCTP_RTT_SHIFT);
2978be1d9176SMichael Tuexen 		net->lastsa += rtt;
2979be1d9176SMichael Tuexen 		if (rtt < 0) {
2980be1d9176SMichael Tuexen 			rtt = -rtt;
2981be1d9176SMichael Tuexen 		}
2982be1d9176SMichael Tuexen 		rtt -= (net->lastsv >> SCTP_RTT_VAR_SHIFT);
2983be1d9176SMichael Tuexen 		net->lastsv += rtt;
2984b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2985f8829a4aSRandall Stewart 			rto_logging(net, SCTP_LOG_RTTVAR);
298680fefe0aSRandall Stewart 		}
2987f8829a4aSRandall Stewart 	} else {
2988f8829a4aSRandall Stewart 		/* First RTO measurment */
29899a972525SRandall Stewart 		net->RTO_measured = 1;
2990f8829a4aSRandall Stewart 		first_measure = 1;
2991be1d9176SMichael Tuexen 		net->lastsa = rtt << SCTP_RTT_SHIFT;
2992be1d9176SMichael Tuexen 		net->lastsv = (rtt / 2) << SCTP_RTT_VAR_SHIFT;
2993b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2994f8829a4aSRandall Stewart 			rto_logging(net, SCTP_LOG_INITIAL_RTT);
299580fefe0aSRandall Stewart 		}
2996f8829a4aSRandall Stewart 	}
2997be1d9176SMichael Tuexen 	if (net->lastsv == 0) {
2998be1d9176SMichael Tuexen 		net->lastsv = SCTP_CLOCK_GRANULARITY;
2999be1d9176SMichael Tuexen 	}
3000108df27cSRandall Stewart 	new_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
3001f8829a4aSRandall Stewart 	if ((new_rto > SCTP_SAT_NETWORK_MIN) &&
3002f8829a4aSRandall Stewart 	    (stcb->asoc.sat_network_lockout == 0)) {
3003f8829a4aSRandall Stewart 		stcb->asoc.sat_network = 1;
3004f8829a4aSRandall Stewart 	} else if ((!first_measure) && stcb->asoc.sat_network) {
3005f8829a4aSRandall Stewart 		stcb->asoc.sat_network = 0;
3006f8829a4aSRandall Stewart 		stcb->asoc.sat_network_lockout = 1;
3007f8829a4aSRandall Stewart 	}
3008f8829a4aSRandall Stewart 	/* bound it, per C6/C7 in Section 5.3.1 */
3009f8829a4aSRandall Stewart 	if (new_rto < stcb->asoc.minrto) {
3010f8829a4aSRandall Stewart 		new_rto = stcb->asoc.minrto;
3011f8829a4aSRandall Stewart 	}
3012f8829a4aSRandall Stewart 	if (new_rto > stcb->asoc.maxrto) {
3013f8829a4aSRandall Stewart 		new_rto = stcb->asoc.maxrto;
3014f8829a4aSRandall Stewart 	}
301544f2a327SMichael Tuexen 	net->RTO = new_rto;
301644f2a327SMichael Tuexen 	return (1);
3017f8829a4aSRandall Stewart }
3018f8829a4aSRandall Stewart 
3019f8829a4aSRandall Stewart /*
3020f8829a4aSRandall Stewart  * return a pointer to a contiguous piece of data from the given mbuf chain
3021f8829a4aSRandall Stewart  * starting at 'off' for 'len' bytes.  If the desired piece spans more than
3022f8829a4aSRandall Stewart  * one mbuf, a copy is made at 'ptr'. caller must ensure that the buffer size
3023f8829a4aSRandall Stewart  * is >= 'len' returns NULL if there there isn't 'len' bytes in the chain.
3024f8829a4aSRandall Stewart  */
302572fb6fdbSRandall Stewart caddr_t
3026f8829a4aSRandall Stewart sctp_m_getptr(struct mbuf *m, int off, int len, uint8_t *in_ptr)
3027f8829a4aSRandall Stewart {
3028f8829a4aSRandall Stewart 	uint32_t count;
3029f8829a4aSRandall Stewart 	uint8_t *ptr;
3030f8829a4aSRandall Stewart 
3031f8829a4aSRandall Stewart 	ptr = in_ptr;
3032f8829a4aSRandall Stewart 	if ((off < 0) || (len <= 0))
3033f8829a4aSRandall Stewart 		return (NULL);
3034f8829a4aSRandall Stewart 
3035f8829a4aSRandall Stewart 	/* find the desired start location */
3036f8829a4aSRandall Stewart 	while ((m != NULL) && (off > 0)) {
3037139bc87fSRandall Stewart 		if (off < SCTP_BUF_LEN(m))
3038f8829a4aSRandall Stewart 			break;
3039139bc87fSRandall Stewart 		off -= SCTP_BUF_LEN(m);
3040139bc87fSRandall Stewart 		m = SCTP_BUF_NEXT(m);
3041f8829a4aSRandall Stewart 	}
3042f8829a4aSRandall Stewart 	if (m == NULL)
3043f8829a4aSRandall Stewart 		return (NULL);
3044f8829a4aSRandall Stewart 
3045f8829a4aSRandall Stewart 	/* is the current mbuf large enough (eg. contiguous)? */
3046139bc87fSRandall Stewart 	if ((SCTP_BUF_LEN(m) - off) >= len) {
3047f8829a4aSRandall Stewart 		return (mtod(m, caddr_t)+off);
3048f8829a4aSRandall Stewart 	} else {
3049f8829a4aSRandall Stewart 		/* else, it spans more than one mbuf, so save a temp copy... */
3050f8829a4aSRandall Stewart 		while ((m != NULL) && (len > 0)) {
3051139bc87fSRandall Stewart 			count = min(SCTP_BUF_LEN(m) - off, len);
30525ba7f91fSMichael Tuexen 			memcpy(ptr, mtod(m, caddr_t)+off, count);
3053f8829a4aSRandall Stewart 			len -= count;
3054f8829a4aSRandall Stewart 			ptr += count;
3055f8829a4aSRandall Stewart 			off = 0;
3056139bc87fSRandall Stewart 			m = SCTP_BUF_NEXT(m);
3057f8829a4aSRandall Stewart 		}
3058f8829a4aSRandall Stewart 		if ((m == NULL) && (len > 0))
3059f8829a4aSRandall Stewart 			return (NULL);
3060f8829a4aSRandall Stewart 		else
3061f8829a4aSRandall Stewart 			return ((caddr_t)in_ptr);
3062f8829a4aSRandall Stewart 	}
3063f8829a4aSRandall Stewart }
3064f8829a4aSRandall Stewart 
3065f8829a4aSRandall Stewart 
306644b7479bSRandall Stewart 
3067f8829a4aSRandall Stewart struct sctp_paramhdr *
3068f8829a4aSRandall Stewart sctp_get_next_param(struct mbuf *m,
3069f8829a4aSRandall Stewart     int offset,
3070f8829a4aSRandall Stewart     struct sctp_paramhdr *pull,
3071f8829a4aSRandall Stewart     int pull_limit)
3072f8829a4aSRandall Stewart {
3073f8829a4aSRandall Stewart 	/* This just provides a typed signature to Peter's Pull routine */
3074f8829a4aSRandall Stewart 	return ((struct sctp_paramhdr *)sctp_m_getptr(m, offset, pull_limit,
3075f8829a4aSRandall Stewart 	    (uint8_t *)pull));
3076f8829a4aSRandall Stewart }
3077f8829a4aSRandall Stewart 
3078f8829a4aSRandall Stewart 
3079ce11b842SMichael Tuexen struct mbuf *
3080f8829a4aSRandall Stewart sctp_add_pad_tombuf(struct mbuf *m, int padlen)
3081f8829a4aSRandall Stewart {
3082ce11b842SMichael Tuexen 	struct mbuf *m_last;
3083ce11b842SMichael Tuexen 	caddr_t dp;
3084f8829a4aSRandall Stewart 
3085f8829a4aSRandall Stewart 	if (padlen > 3) {
3086ce11b842SMichael Tuexen 		return (NULL);
3087f8829a4aSRandall Stewart 	}
308841eee555SRandall Stewart 	if (padlen <= M_TRAILINGSPACE(m)) {
3089f8829a4aSRandall Stewart 		/*
3090f8829a4aSRandall Stewart 		 * The easy way. We hope the majority of the time we hit
3091f8829a4aSRandall Stewart 		 * here :)
3092f8829a4aSRandall Stewart 		 */
3093ce11b842SMichael Tuexen 		m_last = m;
3094f8829a4aSRandall Stewart 	} else {
3095ce11b842SMichael Tuexen 		/* Hard way we must grow the mbuf chain */
3096ce11b842SMichael Tuexen 		m_last = sctp_get_mbuf_for_msg(padlen, 0, M_NOWAIT, 1, MT_DATA);
3097ce11b842SMichael Tuexen 		if (m_last == NULL) {
3098ce11b842SMichael Tuexen 			return (NULL);
3099f8829a4aSRandall Stewart 		}
3100ce11b842SMichael Tuexen 		SCTP_BUF_LEN(m_last) = 0;
3101ce11b842SMichael Tuexen 		SCTP_BUF_NEXT(m_last) = NULL;
3102ce11b842SMichael Tuexen 		SCTP_BUF_NEXT(m) = m_last;
3103f8829a4aSRandall Stewart 	}
3104ce11b842SMichael Tuexen 	dp = mtod(m_last, caddr_t)+SCTP_BUF_LEN(m_last);
3105ce11b842SMichael Tuexen 	SCTP_BUF_LEN(m_last) += padlen;
3106ce11b842SMichael Tuexen 	memset(dp, 0, padlen);
3107ce11b842SMichael Tuexen 	return (m_last);
3108f8829a4aSRandall Stewart }
3109f8829a4aSRandall Stewart 
3110ce11b842SMichael Tuexen struct mbuf *
3111f8829a4aSRandall Stewart sctp_pad_lastmbuf(struct mbuf *m, int padval, struct mbuf *last_mbuf)
3112f8829a4aSRandall Stewart {
3113f8829a4aSRandall Stewart 	/* find the last mbuf in chain and pad it */
3114f8829a4aSRandall Stewart 	struct mbuf *m_at;
3115f8829a4aSRandall Stewart 
3116ce11b842SMichael Tuexen 	if (last_mbuf != NULL) {
3117f8829a4aSRandall Stewart 		return (sctp_add_pad_tombuf(last_mbuf, padval));
3118f8829a4aSRandall Stewart 	} else {
311917267b32SMichael Tuexen 		for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3120139bc87fSRandall Stewart 			if (SCTP_BUF_NEXT(m_at) == NULL) {
3121f8829a4aSRandall Stewart 				return (sctp_add_pad_tombuf(m_at, padval));
3122f8829a4aSRandall Stewart 			}
3123f8829a4aSRandall Stewart 		}
3124f8829a4aSRandall Stewart 	}
3125ce11b842SMichael Tuexen 	return (NULL);
3126f8829a4aSRandall Stewart }
3127f8829a4aSRandall Stewart 
3128f8829a4aSRandall Stewart static void
3129c5b5675dSMichael Tuexen sctp_notify_assoc_change(uint16_t state, struct sctp_tcb *stcb,
3130410a3b1eSMichael Tuexen     uint16_t error, struct sctp_abort_chunk *abort, uint8_t from_peer, int so_locked
3131ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3132ceaad40aSRandall Stewart     SCTP_UNUSED
3133ceaad40aSRandall Stewart #endif
3134ceaad40aSRandall Stewart )
3135f8829a4aSRandall Stewart {
3136f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3137f8829a4aSRandall Stewart 	struct sctp_assoc_change *sac;
3138f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
31399a8e3088SMichael Tuexen 	unsigned int notif_len;
31409a8e3088SMichael Tuexen 	uint16_t abort_len;
3141e06b67c7SMichael Tuexen 	unsigned int i;
3142ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3143ceaad40aSRandall Stewart 	struct socket *so;
3144ceaad40aSRandall Stewart #endif
3145ceaad40aSRandall Stewart 
314659713bbfSMichael Tuexen 	if (stcb == NULL) {
314759713bbfSMichael Tuexen 		return;
314859713bbfSMichael Tuexen 	}
314958411b08SMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT)) {
31509a8e3088SMichael Tuexen 		notif_len = (unsigned int)sizeof(struct sctp_assoc_change);
3151a2b42326SMichael Tuexen 		if (abort != NULL) {
3152c9eb4473SMichael Tuexen 			abort_len = ntohs(abort->ch.chunk_length);
31539669e724SMichael Tuexen 			/*
31549669e724SMichael Tuexen 			 * Only SCTP_CHUNK_BUFFER_SIZE are guaranteed to be
315545d41de5SMichael Tuexen 			 * contiguous.
31569669e724SMichael Tuexen 			 */
31579669e724SMichael Tuexen 			if (abort_len > SCTP_CHUNK_BUFFER_SIZE) {
31589669e724SMichael Tuexen 				abort_len = SCTP_CHUNK_BUFFER_SIZE;
31599669e724SMichael Tuexen 			}
3160a2b42326SMichael Tuexen 		} else {
3161a2b42326SMichael Tuexen 			abort_len = 0;
3162c5b5675dSMichael Tuexen 		}
3163a2b42326SMichael Tuexen 		if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
3164a2b42326SMichael Tuexen 			notif_len += SCTP_ASSOC_SUPPORTS_MAX;
3165a2b42326SMichael Tuexen 		} else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
3166a2b42326SMichael Tuexen 			notif_len += abort_len;
3167a2b42326SMichael Tuexen 		}
3168eb1b1807SGleb Smirnoff 		m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3169a2b42326SMichael Tuexen 		if (m_notify == NULL) {
3170a2b42326SMichael Tuexen 			/* Retry with smaller value. */
31719a8e3088SMichael Tuexen 			notif_len = (unsigned int)sizeof(struct sctp_assoc_change);
3172eb1b1807SGleb Smirnoff 			m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3173a2b42326SMichael Tuexen 			if (m_notify == NULL) {
317458411b08SMichael Tuexen 				goto set_error;
3175a2b42326SMichael Tuexen 			}
3176a2b42326SMichael Tuexen 		}
3177a2b42326SMichael Tuexen 		SCTP_BUF_NEXT(m_notify) = NULL;
3178f8829a4aSRandall Stewart 		sac = mtod(m_notify, struct sctp_assoc_change *);
3179e432298aSXin LI 		memset(sac, 0, notif_len);
3180f8829a4aSRandall Stewart 		sac->sac_type = SCTP_ASSOC_CHANGE;
3181f8829a4aSRandall Stewart 		sac->sac_flags = 0;
3182f8829a4aSRandall Stewart 		sac->sac_length = sizeof(struct sctp_assoc_change);
3183c5b5675dSMichael Tuexen 		sac->sac_state = state;
3184f8829a4aSRandall Stewart 		sac->sac_error = error;
3185f8829a4aSRandall Stewart 		/* XXX verify these stream counts */
3186f8829a4aSRandall Stewart 		sac->sac_outbound_streams = stcb->asoc.streamoutcnt;
3187f8829a4aSRandall Stewart 		sac->sac_inbound_streams = stcb->asoc.streamincnt;
3188f8829a4aSRandall Stewart 		sac->sac_assoc_id = sctp_get_associd(stcb);
3189a2b42326SMichael Tuexen 		if (notif_len > sizeof(struct sctp_assoc_change)) {
3190c5b5675dSMichael Tuexen 			if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
3191e06b67c7SMichael Tuexen 				i = 0;
3192c79bec9cSMichael Tuexen 				if (stcb->asoc.prsctp_supported == 1) {
3193e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_PR;
3194e06b67c7SMichael Tuexen 				}
3195c79bec9cSMichael Tuexen 				if (stcb->asoc.auth_supported == 1) {
3196e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_AUTH;
3197e06b67c7SMichael Tuexen 				}
3198c79bec9cSMichael Tuexen 				if (stcb->asoc.asconf_supported == 1) {
3199e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_ASCONF;
3200e06b67c7SMichael Tuexen 				}
320144249214SRandall Stewart 				if (stcb->asoc.idata_supported == 1) {
320244249214SRandall Stewart 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_INTERLEAVING;
320344249214SRandall Stewart 				}
3204e06b67c7SMichael Tuexen 				sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_MULTIBUF;
3205c79bec9cSMichael Tuexen 				if (stcb->asoc.reconfig_supported == 1) {
3206e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_RE_CONFIG;
3207e06b67c7SMichael Tuexen 				}
3208e06b67c7SMichael Tuexen 				sac->sac_length += i;
3209a2b42326SMichael Tuexen 			} else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
3210a2b42326SMichael Tuexen 				memcpy(sac->sac_info, abort, abort_len);
3211a2b42326SMichael Tuexen 				sac->sac_length += abort_len;
3212a2b42326SMichael Tuexen 			}
3213c5b5675dSMichael Tuexen 		}
3214e06b67c7SMichael Tuexen 		SCTP_BUF_LEN(m_notify) = sac->sac_length;
3215f8829a4aSRandall Stewart 		control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
32167215cc1bSMichael Tuexen 		    0, 0, stcb->asoc.context, 0, 0, 0,
3217f8829a4aSRandall Stewart 		    m_notify);
321858411b08SMichael Tuexen 		if (control != NULL) {
3219139bc87fSRandall Stewart 			control->length = SCTP_BUF_LEN(m_notify);
322028cd0699SMichael Tuexen 			control->spec_flags = M_NOTIFICATION;
3221f8829a4aSRandall Stewart 			/* not that we need this */
3222f8829a4aSRandall Stewart 			control->tail_mbuf = m_notify;
3223f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep, stcb,
3224f8829a4aSRandall Stewart 			    control,
3225cfde3ff7SRandall Stewart 			    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD,
3226cfde3ff7SRandall Stewart 			    so_locked);
322758411b08SMichael Tuexen 		} else {
322858411b08SMichael Tuexen 			sctp_m_freem(m_notify);
322958411b08SMichael Tuexen 		}
323058411b08SMichael Tuexen 	}
323158411b08SMichael Tuexen 	/*
323258411b08SMichael Tuexen 	 * For 1-to-1 style sockets, we send up and error when an ABORT
323358411b08SMichael Tuexen 	 * comes in.
323458411b08SMichael Tuexen 	 */
323558411b08SMichael Tuexen set_error:
323658411b08SMichael Tuexen 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
323758411b08SMichael Tuexen 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
323858411b08SMichael Tuexen 	    ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
3239e045904fSMichael Tuexen 		SOCK_LOCK(stcb->sctp_socket);
3240410a3b1eSMichael Tuexen 		if (from_peer) {
3241839d21d6SMichael Tuexen 			if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) {
324258411b08SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNREFUSED);
324358411b08SMichael Tuexen 				stcb->sctp_socket->so_error = ECONNREFUSED;
324458411b08SMichael Tuexen 			} else {
324558411b08SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
324658411b08SMichael Tuexen 				stcb->sctp_socket->so_error = ECONNRESET;
324758411b08SMichael Tuexen 			}
3248410a3b1eSMichael Tuexen 		} else {
3249839d21d6SMichael Tuexen 			if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
3250839d21d6SMichael Tuexen 			    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
3251553bb068SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ETIMEDOUT);
3252553bb068SMichael Tuexen 				stcb->sctp_socket->so_error = ETIMEDOUT;
3253553bb068SMichael Tuexen 			} else {
3254410a3b1eSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNABORTED);
3255410a3b1eSMichael Tuexen 				stcb->sctp_socket->so_error = ECONNABORTED;
3256410a3b1eSMichael Tuexen 			}
325758411b08SMichael Tuexen 		}
32583acfe1e1SGleb Smirnoff 		SOCK_UNLOCK(stcb->sctp_socket);
3259553bb068SMichael Tuexen 	}
326058411b08SMichael Tuexen 	/* Wake ANY sleepers */
3261ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3262ceaad40aSRandall Stewart 	so = SCTP_INP_SO(stcb->sctp_ep);
3263ceaad40aSRandall Stewart 	if (!so_locked) {
3264ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
3265ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3266ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
3267ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
3268ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
3269ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3270ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
3271ceaad40aSRandall Stewart 			return;
3272ceaad40aSRandall Stewart 		}
3273ceaad40aSRandall Stewart 	}
3274ceaad40aSRandall Stewart #endif
327558411b08SMichael Tuexen 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
327658411b08SMichael Tuexen 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
327758411b08SMichael Tuexen 	    ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
32783acfe1e1SGleb Smirnoff 		socantrcvmore(stcb->sctp_socket);
327958411b08SMichael Tuexen 	}
328058411b08SMichael Tuexen 	sorwakeup(stcb->sctp_socket);
328158411b08SMichael Tuexen 	sowwakeup(stcb->sctp_socket);
3282ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3283ceaad40aSRandall Stewart 	if (!so_locked) {
3284ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
3285ceaad40aSRandall Stewart 	}
3286ceaad40aSRandall Stewart #endif
3287f8829a4aSRandall Stewart }
3288f8829a4aSRandall Stewart 
3289f8829a4aSRandall Stewart static void
3290f8829a4aSRandall Stewart sctp_notify_peer_addr_change(struct sctp_tcb *stcb, uint32_t state,
32913cb3567dSMichael Tuexen     struct sockaddr *sa, uint32_t error, int so_locked
32923cb3567dSMichael Tuexen #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
32933cb3567dSMichael Tuexen     SCTP_UNUSED
32943cb3567dSMichael Tuexen #endif
32953cb3567dSMichael Tuexen )
3296f8829a4aSRandall Stewart {
3297f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3298f8829a4aSRandall Stewart 	struct sctp_paddr_change *spc;
3299f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3300f8829a4aSRandall Stewart 
330160990c0cSMichael Tuexen 	if ((stcb == NULL) ||
330260990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT)) {
3303f8829a4aSRandall Stewart 		/* event not enabled */
3304f8829a4aSRandall Stewart 		return;
3305830d754dSRandall Stewart 	}
3306eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_paddr_change), 0, M_NOWAIT, 1, MT_DATA);
3307f8829a4aSRandall Stewart 	if (m_notify == NULL)
3308f8829a4aSRandall Stewart 		return;
3309139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3310f8829a4aSRandall Stewart 	spc = mtod(m_notify, struct sctp_paddr_change *);
331156711f94SMichael Tuexen 	memset(spc, 0, sizeof(struct sctp_paddr_change));
3312f8829a4aSRandall Stewart 	spc->spc_type = SCTP_PEER_ADDR_CHANGE;
3313f8829a4aSRandall Stewart 	spc->spc_flags = 0;
3314f8829a4aSRandall Stewart 	spc->spc_length = sizeof(struct sctp_paddr_change);
33155e2c2d87SRandall Stewart 	switch (sa->sa_family) {
3316ea5eba11SMichael Tuexen #ifdef INET
33175e2c2d87SRandall Stewart 	case AF_INET:
3318d59107f7SMichael Tuexen #ifdef INET6
3319d59107f7SMichael Tuexen 		if (sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
3320d59107f7SMichael Tuexen 			in6_sin_2_v4mapsin6((struct sockaddr_in *)sa,
3321d59107f7SMichael Tuexen 			    (struct sockaddr_in6 *)&spc->spc_aaddr);
3322d59107f7SMichael Tuexen 		} else {
3323f8829a4aSRandall Stewart 			memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
3324d59107f7SMichael Tuexen 		}
3325d59107f7SMichael Tuexen #else
3326d59107f7SMichael Tuexen 		memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
3327d59107f7SMichael Tuexen #endif
33285e2c2d87SRandall Stewart 		break;
3329ea5eba11SMichael Tuexen #endif
33305e2c2d87SRandall Stewart #ifdef INET6
33315e2c2d87SRandall Stewart 	case AF_INET6:
33325e2c2d87SRandall Stewart 		{
3333f42a358aSRandall Stewart 			struct sockaddr_in6 *sin6;
3334f42a358aSRandall Stewart 
3335f8829a4aSRandall Stewart 			memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in6));
3336f42a358aSRandall Stewart 
3337f42a358aSRandall Stewart 			sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
3338f42a358aSRandall Stewart 			if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
333942551e99SRandall Stewart 				if (sin6->sin6_scope_id == 0) {
334042551e99SRandall Stewart 					/* recover scope_id for user */
3341f42a358aSRandall Stewart 					(void)sa6_recoverscope(sin6);
334242551e99SRandall Stewart 				} else {
334342551e99SRandall Stewart 					/* clear embedded scope_id for user */
334442551e99SRandall Stewart 					in6_clearscope(&sin6->sin6_addr);
334542551e99SRandall Stewart 				}
3346f42a358aSRandall Stewart 			}
33475e2c2d87SRandall Stewart 			break;
33485e2c2d87SRandall Stewart 		}
33495e2c2d87SRandall Stewart #endif
33505e2c2d87SRandall Stewart 	default:
33515e2c2d87SRandall Stewart 		/* TSNH */
33525e2c2d87SRandall Stewart 		break;
3353f8829a4aSRandall Stewart 	}
3354f8829a4aSRandall Stewart 	spc->spc_state = state;
3355f8829a4aSRandall Stewart 	spc->spc_error = error;
3356f8829a4aSRandall Stewart 	spc->spc_assoc_id = sctp_get_associd(stcb);
3357f8829a4aSRandall Stewart 
3358139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_paddr_change);
3359139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3360f8829a4aSRandall Stewart 
3361f8829a4aSRandall Stewart 	/* append to socket */
3362f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
33637215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3364f8829a4aSRandall Stewart 	    m_notify);
3365f8829a4aSRandall Stewart 	if (control == NULL) {
3366f8829a4aSRandall Stewart 		/* no memory */
3367f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3368f8829a4aSRandall Stewart 		return;
3369f8829a4aSRandall Stewart 	}
3370139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3371139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3372f8829a4aSRandall Stewart 	/* not that we need this */
3373f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3374f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3375f8829a4aSRandall Stewart 	    control,
3376cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1,
3377cfde3ff7SRandall Stewart 	    SCTP_READ_LOCK_NOT_HELD,
33783cb3567dSMichael Tuexen 	    so_locked);
3379f8829a4aSRandall Stewart }
3380f8829a4aSRandall Stewart 
3381f8829a4aSRandall Stewart 
3382f8829a4aSRandall Stewart static void
33831edc9dbaSMichael Tuexen sctp_notify_send_failed(struct sctp_tcb *stcb, uint8_t sent, uint32_t error,
3384ceaad40aSRandall Stewart     struct sctp_tmit_chunk *chk, int so_locked
3385ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3386ceaad40aSRandall Stewart     SCTP_UNUSED
3387ceaad40aSRandall Stewart #endif
3388ceaad40aSRandall Stewart )
3389f8829a4aSRandall Stewart {
3390830d754dSRandall Stewart 	struct mbuf *m_notify;
3391f8829a4aSRandall Stewart 	struct sctp_send_failed *ssf;
33929935403aSMichael Tuexen 	struct sctp_send_failed_event *ssfe;
3393f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3394ab337314SMichael Tuexen 	struct sctp_chunkhdr *chkhdr;
3395ab337314SMichael Tuexen 	int notifhdr_len, chk_len, chkhdr_len, padding_len, payload_len;
3396f8829a4aSRandall Stewart 
339760990c0cSMichael Tuexen 	if ((stcb == NULL) ||
33989935403aSMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
33999935403aSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
3400f8829a4aSRandall Stewart 		/* event not enabled */
3401f8829a4aSRandall Stewart 		return;
3402830d754dSRandall Stewart 	}
34030053ed28SMichael Tuexen 
34049935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3405ab337314SMichael Tuexen 		notifhdr_len = sizeof(struct sctp_send_failed_event);
34069935403aSMichael Tuexen 	} else {
3407ab337314SMichael Tuexen 		notifhdr_len = sizeof(struct sctp_send_failed);
34089935403aSMichael Tuexen 	}
3409ab337314SMichael Tuexen 	m_notify = sctp_get_mbuf_for_msg(notifhdr_len, 0, M_NOWAIT, 1, MT_DATA);
3410f8829a4aSRandall Stewart 	if (m_notify == NULL)
3411f8829a4aSRandall Stewart 		/* no space left */
3412f8829a4aSRandall Stewart 		return;
3413ab337314SMichael Tuexen 	SCTP_BUF_LEN(m_notify) = notifhdr_len;
3414ab337314SMichael Tuexen 	if (stcb->asoc.idata_supported) {
3415ab337314SMichael Tuexen 		chkhdr_len = sizeof(struct sctp_idata_chunk);
3416ab337314SMichael Tuexen 	} else {
3417ab337314SMichael Tuexen 		chkhdr_len = sizeof(struct sctp_data_chunk);
3418ab337314SMichael Tuexen 	}
3419ab337314SMichael Tuexen 	/* Use some defaults in case we can't access the chunk header */
3420ab337314SMichael Tuexen 	if (chk->send_size >= chkhdr_len) {
3421ab337314SMichael Tuexen 		payload_len = chk->send_size - chkhdr_len;
3422ab337314SMichael Tuexen 	} else {
3423ab337314SMichael Tuexen 		payload_len = 0;
3424ab337314SMichael Tuexen 	}
3425ab337314SMichael Tuexen 	padding_len = 0;
3426ab337314SMichael Tuexen 	if (chk->data != NULL) {
3427ab337314SMichael Tuexen 		chkhdr = mtod(chk->data, struct sctp_chunkhdr *);
3428ab337314SMichael Tuexen 		if (chkhdr != NULL) {
3429ab337314SMichael Tuexen 			chk_len = ntohs(chkhdr->chunk_length);
3430ab337314SMichael Tuexen 			if ((chk_len >= chkhdr_len) &&
3431ab337314SMichael Tuexen 			    (chk->send_size >= chk_len) &&
3432ab337314SMichael Tuexen 			    (chk->send_size - chk_len < 4)) {
3433ab337314SMichael Tuexen 				padding_len = chk->send_size - chk_len;
3434ab337314SMichael Tuexen 				payload_len = chk->send_size - chkhdr_len - padding_len;
3435ab337314SMichael Tuexen 			}
3436ab337314SMichael Tuexen 		}
3437ab337314SMichael Tuexen 	}
34389935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
34399935403aSMichael Tuexen 		ssfe = mtod(m_notify, struct sctp_send_failed_event *);
3440ab337314SMichael Tuexen 		memset(ssfe, 0, notifhdr_len);
34419935403aSMichael Tuexen 		ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
34421edc9dbaSMichael Tuexen 		if (sent) {
34439935403aSMichael Tuexen 			ssfe->ssfe_flags = SCTP_DATA_SENT;
34441edc9dbaSMichael Tuexen 		} else {
34451edc9dbaSMichael Tuexen 			ssfe->ssfe_flags = SCTP_DATA_UNSENT;
34461edc9dbaSMichael Tuexen 		}
3447ab337314SMichael Tuexen 		ssfe->ssfe_length = (uint32_t)(notifhdr_len + payload_len);
34489935403aSMichael Tuexen 		ssfe->ssfe_error = error;
34499935403aSMichael Tuexen 		/* not exactly what the user sent in, but should be close :) */
345049656eefSMichael Tuexen 		ssfe->ssfe_info.snd_sid = chk->rec.data.sid;
34519935403aSMichael Tuexen 		ssfe->ssfe_info.snd_flags = chk->rec.data.rcv_flags;
345249656eefSMichael Tuexen 		ssfe->ssfe_info.snd_ppid = chk->rec.data.ppid;
34539935403aSMichael Tuexen 		ssfe->ssfe_info.snd_context = chk->rec.data.context;
34549935403aSMichael Tuexen 		ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
34559935403aSMichael Tuexen 		ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
34569935403aSMichael Tuexen 	} else {
3457f8829a4aSRandall Stewart 		ssf = mtod(m_notify, struct sctp_send_failed *);
3458ab337314SMichael Tuexen 		memset(ssf, 0, notifhdr_len);
3459f8829a4aSRandall Stewart 		ssf->ssf_type = SCTP_SEND_FAILED;
34601edc9dbaSMichael Tuexen 		if (sent) {
3461f8829a4aSRandall Stewart 			ssf->ssf_flags = SCTP_DATA_SENT;
34621edc9dbaSMichael Tuexen 		} else {
34631edc9dbaSMichael Tuexen 			ssf->ssf_flags = SCTP_DATA_UNSENT;
34641edc9dbaSMichael Tuexen 		}
3465ab337314SMichael Tuexen 		ssf->ssf_length = (uint32_t)(notifhdr_len + payload_len);
3466f8829a4aSRandall Stewart 		ssf->ssf_error = error;
3467f8829a4aSRandall Stewart 		/* not exactly what the user sent in, but should be close :) */
346849656eefSMichael Tuexen 		ssf->ssf_info.sinfo_stream = chk->rec.data.sid;
346949656eefSMichael Tuexen 		ssf->ssf_info.sinfo_ssn = (uint16_t)chk->rec.data.mid;
3470f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_flags = chk->rec.data.rcv_flags;
347149656eefSMichael Tuexen 		ssf->ssf_info.sinfo_ppid = chk->rec.data.ppid;
3472f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_context = chk->rec.data.context;
3473f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3474f8829a4aSRandall Stewart 		ssf->ssf_assoc_id = sctp_get_associd(stcb);
34759935403aSMichael Tuexen 	}
3476ab337314SMichael Tuexen 	if (chk->data != NULL) {
3477ab337314SMichael Tuexen 		/* Trim off the sctp chunk header (it should be there) */
3478ab337314SMichael Tuexen 		if (chk->send_size == chkhdr_len + payload_len + padding_len) {
3479ab337314SMichael Tuexen 			m_adj(chk->data, chkhdr_len);
3480ab337314SMichael Tuexen 			m_adj(chk->data, -padding_len);
3481830d754dSRandall Stewart 			sctp_mbuf_crush(chk->data);
3482ab337314SMichael Tuexen 			chk->send_size -= (chkhdr_len + padding_len);
3483830d754dSRandall Stewart 		}
3484830d754dSRandall Stewart 	}
3485810ec536SMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = chk->data;
3486f8829a4aSRandall Stewart 	/* Steal off the mbuf */
3487f8829a4aSRandall Stewart 	chk->data = NULL;
3488f8829a4aSRandall Stewart 	/*
3489f8829a4aSRandall Stewart 	 * For this case, we check the actual socket buffer, since the assoc
3490f8829a4aSRandall Stewart 	 * is going away we don't want to overfill the socket buffer for a
3491f8829a4aSRandall Stewart 	 * non-reader
3492f8829a4aSRandall Stewart 	 */
3493139bc87fSRandall Stewart 	if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3494f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3495f8829a4aSRandall Stewart 		return;
3496f8829a4aSRandall Stewart 	}
3497f8829a4aSRandall Stewart 	/* append to socket */
3498f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
34997215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3500f8829a4aSRandall Stewart 	    m_notify);
3501f8829a4aSRandall Stewart 	if (control == NULL) {
3502f8829a4aSRandall Stewart 		/* no memory */
3503f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3504f8829a4aSRandall Stewart 		return;
3505f8829a4aSRandall Stewart 	}
350628cd0699SMichael Tuexen 	control->length = SCTP_BUF_LEN(m_notify);
3507139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
350828cd0699SMichael Tuexen 	/* not that we need this */
350928cd0699SMichael Tuexen 	control->tail_mbuf = m_notify;
3510f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3511f8829a4aSRandall Stewart 	    control,
3512cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1,
3513cfde3ff7SRandall Stewart 	    SCTP_READ_LOCK_NOT_HELD,
3514cfde3ff7SRandall Stewart 	    so_locked);
3515f8829a4aSRandall Stewart }
3516f8829a4aSRandall Stewart 
3517f8829a4aSRandall Stewart 
3518f8829a4aSRandall Stewart static void
3519f8829a4aSRandall Stewart sctp_notify_send_failed2(struct sctp_tcb *stcb, uint32_t error,
3520ceaad40aSRandall Stewart     struct sctp_stream_queue_pending *sp, int so_locked
3521ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3522ceaad40aSRandall Stewart     SCTP_UNUSED
3523ceaad40aSRandall Stewart #endif
3524ceaad40aSRandall Stewart )
3525f8829a4aSRandall Stewart {
3526f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3527f8829a4aSRandall Stewart 	struct sctp_send_failed *ssf;
35289935403aSMichael Tuexen 	struct sctp_send_failed_event *ssfe;
3529f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3530ab337314SMichael Tuexen 	int notifhdr_len;
3531f8829a4aSRandall Stewart 
353260990c0cSMichael Tuexen 	if ((stcb == NULL) ||
35339935403aSMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
35349935403aSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
3535f8829a4aSRandall Stewart 		/* event not enabled */
3536f8829a4aSRandall Stewart 		return;
3537830d754dSRandall Stewart 	}
35389935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3539ab337314SMichael Tuexen 		notifhdr_len = sizeof(struct sctp_send_failed_event);
35409935403aSMichael Tuexen 	} else {
3541ab337314SMichael Tuexen 		notifhdr_len = sizeof(struct sctp_send_failed);
35429935403aSMichael Tuexen 	}
3543ab337314SMichael Tuexen 	m_notify = sctp_get_mbuf_for_msg(notifhdr_len, 0, M_NOWAIT, 1, MT_DATA);
35449935403aSMichael Tuexen 	if (m_notify == NULL) {
3545f8829a4aSRandall Stewart 		/* no space left */
3546f8829a4aSRandall Stewart 		return;
35479935403aSMichael Tuexen 	}
3548ab337314SMichael Tuexen 	SCTP_BUF_LEN(m_notify) = notifhdr_len;
35499935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
35509935403aSMichael Tuexen 		ssfe = mtod(m_notify, struct sctp_send_failed_event *);
3551ab337314SMichael Tuexen 		memset(ssfe, 0, notifhdr_len);
3552ad83c8a5SMichael Tuexen 		ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
35539935403aSMichael Tuexen 		ssfe->ssfe_flags = SCTP_DATA_UNSENT;
3554ab337314SMichael Tuexen 		ssfe->ssfe_length = (uint32_t)(notifhdr_len + sp->length);
35559935403aSMichael Tuexen 		ssfe->ssfe_error = error;
35569935403aSMichael Tuexen 		/* not exactly what the user sent in, but should be close :) */
355749656eefSMichael Tuexen 		ssfe->ssfe_info.snd_sid = sp->sid;
35589935403aSMichael Tuexen 		if (sp->some_taken) {
35599935403aSMichael Tuexen 			ssfe->ssfe_info.snd_flags = SCTP_DATA_LAST_FRAG;
35609935403aSMichael Tuexen 		} else {
35619935403aSMichael Tuexen 			ssfe->ssfe_info.snd_flags = SCTP_DATA_NOT_FRAG;
35629935403aSMichael Tuexen 		}
35639935403aSMichael Tuexen 		ssfe->ssfe_info.snd_ppid = sp->ppid;
35649935403aSMichael Tuexen 		ssfe->ssfe_info.snd_context = sp->context;
35659935403aSMichael Tuexen 		ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
35669935403aSMichael Tuexen 		ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
35679935403aSMichael Tuexen 	} else {
3568f8829a4aSRandall Stewart 		ssf = mtod(m_notify, struct sctp_send_failed *);
3569ab337314SMichael Tuexen 		memset(ssf, 0, notifhdr_len);
3570f8829a4aSRandall Stewart 		ssf->ssf_type = SCTP_SEND_FAILED;
3571f8829a4aSRandall Stewart 		ssf->ssf_flags = SCTP_DATA_UNSENT;
3572ab337314SMichael Tuexen 		ssf->ssf_length = (uint32_t)(notifhdr_len + sp->length);
3573f8829a4aSRandall Stewart 		ssf->ssf_error = error;
3574f8829a4aSRandall Stewart 		/* not exactly what the user sent in, but should be close :) */
357549656eefSMichael Tuexen 		ssf->ssf_info.sinfo_stream = sp->sid;
3576f3b05218SMichael Tuexen 		ssf->ssf_info.sinfo_ssn = 0;
3577fc14de76SRandall Stewart 		if (sp->some_taken) {
3578fc14de76SRandall Stewart 			ssf->ssf_info.sinfo_flags = SCTP_DATA_LAST_FRAG;
3579fc14de76SRandall Stewart 		} else {
3580fc14de76SRandall Stewart 			ssf->ssf_info.sinfo_flags = SCTP_DATA_NOT_FRAG;
3581fc14de76SRandall Stewart 		}
3582f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_ppid = sp->ppid;
3583f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_context = sp->context;
3584f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3585f8829a4aSRandall Stewart 		ssf->ssf_assoc_id = sctp_get_associd(stcb);
35869935403aSMichael Tuexen 	}
35879935403aSMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = sp->data;
3588f8829a4aSRandall Stewart 
3589f8829a4aSRandall Stewart 	/* Steal off the mbuf */
3590f8829a4aSRandall Stewart 	sp->data = NULL;
3591f8829a4aSRandall Stewart 	/*
3592f8829a4aSRandall Stewart 	 * For this case, we check the actual socket buffer, since the assoc
3593f8829a4aSRandall Stewart 	 * is going away we don't want to overfill the socket buffer for a
3594f8829a4aSRandall Stewart 	 * non-reader
3595f8829a4aSRandall Stewart 	 */
3596139bc87fSRandall Stewart 	if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3597f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3598f8829a4aSRandall Stewart 		return;
3599f8829a4aSRandall Stewart 	}
3600f8829a4aSRandall Stewart 	/* append to socket */
3601f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
36027215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3603f8829a4aSRandall Stewart 	    m_notify);
3604f8829a4aSRandall Stewart 	if (control == NULL) {
3605f8829a4aSRandall Stewart 		/* no memory */
3606f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3607f8829a4aSRandall Stewart 		return;
3608f8829a4aSRandall Stewart 	}
360928cd0699SMichael Tuexen 	control->length = SCTP_BUF_LEN(m_notify);
3610139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
361128cd0699SMichael Tuexen 	/* not that we need this */
361228cd0699SMichael Tuexen 	control->tail_mbuf = m_notify;
3613f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3614f8829a4aSRandall Stewart 	    control,
3615cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3616f8829a4aSRandall Stewart }
3617f8829a4aSRandall Stewart 
3618f8829a4aSRandall Stewart 
3619f8829a4aSRandall Stewart 
3620f8829a4aSRandall Stewart static void
36217215cc1bSMichael Tuexen sctp_notify_adaptation_layer(struct sctp_tcb *stcb)
3622f8829a4aSRandall Stewart {
3623f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3624f8829a4aSRandall Stewart 	struct sctp_adaptation_event *sai;
3625f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3626f8829a4aSRandall Stewart 
362760990c0cSMichael Tuexen 	if ((stcb == NULL) ||
362860990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT)) {
3629f8829a4aSRandall Stewart 		/* event not enabled */
3630f8829a4aSRandall Stewart 		return;
3631830d754dSRandall Stewart 	}
36320053ed28SMichael Tuexen 
3633eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 0, M_NOWAIT, 1, MT_DATA);
3634f8829a4aSRandall Stewart 	if (m_notify == NULL)
3635f8829a4aSRandall Stewart 		/* no space left */
3636f8829a4aSRandall Stewart 		return;
3637139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3638f8829a4aSRandall Stewart 	sai = mtod(m_notify, struct sctp_adaptation_event *);
3639e432298aSXin LI 	memset(sai, 0, sizeof(struct sctp_adaptation_event));
3640f8829a4aSRandall Stewart 	sai->sai_type = SCTP_ADAPTATION_INDICATION;
3641f8829a4aSRandall Stewart 	sai->sai_flags = 0;
3642f8829a4aSRandall Stewart 	sai->sai_length = sizeof(struct sctp_adaptation_event);
36432afb3e84SRandall Stewart 	sai->sai_adaptation_ind = stcb->asoc.peers_adaptation;
3644f8829a4aSRandall Stewart 	sai->sai_assoc_id = sctp_get_associd(stcb);
3645f8829a4aSRandall Stewart 
3646139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_adaptation_event);
3647139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3648f8829a4aSRandall Stewart 
3649f8829a4aSRandall Stewart 	/* append to socket */
3650f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
36517215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3652f8829a4aSRandall Stewart 	    m_notify);
3653f8829a4aSRandall Stewart 	if (control == NULL) {
3654f8829a4aSRandall Stewart 		/* no memory */
3655f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3656f8829a4aSRandall Stewart 		return;
3657f8829a4aSRandall Stewart 	}
3658139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3659139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3660f8829a4aSRandall Stewart 	/* not that we need this */
3661f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3662f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3663f8829a4aSRandall Stewart 	    control,
3664cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3665f8829a4aSRandall Stewart }
3666f8829a4aSRandall Stewart 
366703b0b021SRandall Stewart /* This always must be called with the read-queue LOCKED in the INP */
3668810ec536SMichael Tuexen static void
36692dad8a55SRandall Stewart sctp_notify_partial_delivery_indication(struct sctp_tcb *stcb, uint32_t error,
3670810ec536SMichael Tuexen     uint32_t val, int so_locked
3671810ec536SMichael Tuexen #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3672810ec536SMichael Tuexen     SCTP_UNUSED
3673810ec536SMichael Tuexen #endif
3674810ec536SMichael Tuexen )
3675f8829a4aSRandall Stewart {
3676f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3677f8829a4aSRandall Stewart 	struct sctp_pdapi_event *pdapi;
3678f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
367903b0b021SRandall Stewart 	struct sockbuf *sb;
3680f8829a4aSRandall Stewart 
368160990c0cSMichael Tuexen 	if ((stcb == NULL) ||
368260990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_PDAPIEVNT)) {
3683f8829a4aSRandall Stewart 		/* event not enabled */
3684f8829a4aSRandall Stewart 		return;
3685830d754dSRandall Stewart 	}
3686cd1386abSMichael Tuexen 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
3687cd1386abSMichael Tuexen 		return;
3688cd1386abSMichael Tuexen 	}
36890053ed28SMichael Tuexen 
3690eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_pdapi_event), 0, M_NOWAIT, 1, MT_DATA);
3691f8829a4aSRandall Stewart 	if (m_notify == NULL)
3692f8829a4aSRandall Stewart 		/* no space left */
3693f8829a4aSRandall Stewart 		return;
3694139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3695f8829a4aSRandall Stewart 	pdapi = mtod(m_notify, struct sctp_pdapi_event *);
3696e432298aSXin LI 	memset(pdapi, 0, sizeof(struct sctp_pdapi_event));
3697f8829a4aSRandall Stewart 	pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
3698f8829a4aSRandall Stewart 	pdapi->pdapi_flags = 0;
3699f8829a4aSRandall Stewart 	pdapi->pdapi_length = sizeof(struct sctp_pdapi_event);
3700f8829a4aSRandall Stewart 	pdapi->pdapi_indication = error;
37019a6142d8SRandall Stewart 	pdapi->pdapi_stream = (val >> 16);
37029a6142d8SRandall Stewart 	pdapi->pdapi_seq = (val & 0x0000ffff);
3703f8829a4aSRandall Stewart 	pdapi->pdapi_assoc_id = sctp_get_associd(stcb);
3704f8829a4aSRandall Stewart 
3705139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_pdapi_event);
3706139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3707f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
37087215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3709f8829a4aSRandall Stewart 	    m_notify);
3710f8829a4aSRandall Stewart 	if (control == NULL) {
3711f8829a4aSRandall Stewart 		/* no memory */
3712f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3713f8829a4aSRandall Stewart 		return;
3714f8829a4aSRandall Stewart 	}
3715139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
371628cd0699SMichael Tuexen 	control->spec_flags = M_NOTIFICATION;
3717f8829a4aSRandall Stewart 	/* not that we need this */
3718f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
371903b0b021SRandall Stewart 	sb = &stcb->sctp_socket->so_rcv;
3720b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3721139bc87fSRandall Stewart 		sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m_notify));
372280fefe0aSRandall Stewart 	}
372303b0b021SRandall Stewart 	sctp_sballoc(stcb, sb, m_notify);
3724b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
372503b0b021SRandall Stewart 		sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
372680fefe0aSRandall Stewart 	}
372703b0b021SRandall Stewart 	control->end_added = 1;
372803b0b021SRandall Stewart 	if (stcb->asoc.control_pdapi)
372903b0b021SRandall Stewart 		TAILQ_INSERT_AFTER(&stcb->sctp_ep->read_queue, stcb->asoc.control_pdapi, control, next);
373003b0b021SRandall Stewart 	else {
373103b0b021SRandall Stewart 		/* we really should not see this case */
373203b0b021SRandall Stewart 		TAILQ_INSERT_TAIL(&stcb->sctp_ep->read_queue, control, next);
373303b0b021SRandall Stewart 	}
373403b0b021SRandall Stewart 	if (stcb->sctp_ep && stcb->sctp_socket) {
373503b0b021SRandall Stewart 		/* This should always be the case */
3736810ec536SMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3737810ec536SMichael Tuexen 		struct socket *so;
3738810ec536SMichael Tuexen 
3739810ec536SMichael Tuexen 		so = SCTP_INP_SO(stcb->sctp_ep);
3740810ec536SMichael Tuexen 		if (!so_locked) {
3741810ec536SMichael Tuexen 			atomic_add_int(&stcb->asoc.refcnt, 1);
3742810ec536SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
3743810ec536SMichael Tuexen 			SCTP_SOCKET_LOCK(so, 1);
3744810ec536SMichael Tuexen 			SCTP_TCB_LOCK(stcb);
3745810ec536SMichael Tuexen 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
3746810ec536SMichael Tuexen 			if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3747810ec536SMichael Tuexen 				SCTP_SOCKET_UNLOCK(so, 1);
3748810ec536SMichael Tuexen 				return;
3749810ec536SMichael Tuexen 			}
3750810ec536SMichael Tuexen 		}
3751810ec536SMichael Tuexen #endif
375203b0b021SRandall Stewart 		sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
3753810ec536SMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3754810ec536SMichael Tuexen 		if (!so_locked) {
3755810ec536SMichael Tuexen 			SCTP_SOCKET_UNLOCK(so, 1);
3756810ec536SMichael Tuexen 		}
3757810ec536SMichael Tuexen #endif
3758f8829a4aSRandall Stewart 	}
3759f8829a4aSRandall Stewart }
3760f8829a4aSRandall Stewart 
3761f8829a4aSRandall Stewart static void
3762f8829a4aSRandall Stewart sctp_notify_shutdown_event(struct sctp_tcb *stcb)
3763f8829a4aSRandall Stewart {
3764f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3765f8829a4aSRandall Stewart 	struct sctp_shutdown_event *sse;
3766f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3767f8829a4aSRandall Stewart 
3768f8829a4aSRandall Stewart 	/*
3769f8829a4aSRandall Stewart 	 * For TCP model AND UDP connected sockets we will send an error up
3770f8829a4aSRandall Stewart 	 * when an SHUTDOWN completes
3771f8829a4aSRandall Stewart 	 */
3772f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3773f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3774f8829a4aSRandall Stewart 		/* mark socket closed for read/write and wakeup! */
3775ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3776ceaad40aSRandall Stewart 		struct socket *so;
3777ceaad40aSRandall Stewart 
3778ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
3779ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
3780ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3781ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
3782ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
3783ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
3784ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3785ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
3786ceaad40aSRandall Stewart 			return;
3787ceaad40aSRandall Stewart 		}
3788ceaad40aSRandall Stewart #endif
3789f8829a4aSRandall Stewart 		socantsendmore(stcb->sctp_socket);
3790ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3791ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
3792ceaad40aSRandall Stewart #endif
3793f8829a4aSRandall Stewart 	}
3794e2e7c62eSMichael Tuexen 	if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)) {
3795f8829a4aSRandall Stewart 		/* event not enabled */
3796f8829a4aSRandall Stewart 		return;
3797830d754dSRandall Stewart 	}
37980053ed28SMichael Tuexen 
3799eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_event), 0, M_NOWAIT, 1, MT_DATA);
3800f8829a4aSRandall Stewart 	if (m_notify == NULL)
3801f8829a4aSRandall Stewart 		/* no space left */
3802f8829a4aSRandall Stewart 		return;
3803f8829a4aSRandall Stewart 	sse = mtod(m_notify, struct sctp_shutdown_event *);
3804e432298aSXin LI 	memset(sse, 0, sizeof(struct sctp_shutdown_event));
3805f8829a4aSRandall Stewart 	sse->sse_type = SCTP_SHUTDOWN_EVENT;
3806f8829a4aSRandall Stewart 	sse->sse_flags = 0;
3807f8829a4aSRandall Stewart 	sse->sse_length = sizeof(struct sctp_shutdown_event);
3808f8829a4aSRandall Stewart 	sse->sse_assoc_id = sctp_get_associd(stcb);
3809f8829a4aSRandall Stewart 
3810139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_shutdown_event);
3811139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3812f8829a4aSRandall Stewart 
3813f8829a4aSRandall Stewart 	/* append to socket */
3814f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
38157215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3816f8829a4aSRandall Stewart 	    m_notify);
3817f8829a4aSRandall Stewart 	if (control == NULL) {
3818f8829a4aSRandall Stewart 		/* no memory */
3819f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3820f8829a4aSRandall Stewart 		return;
3821f8829a4aSRandall Stewart 	}
3822139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
382328cd0699SMichael Tuexen 	control->spec_flags = M_NOTIFICATION;
3824f8829a4aSRandall Stewart 	/* not that we need this */
3825f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3826f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3827f8829a4aSRandall Stewart 	    control,
3828cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3829f8829a4aSRandall Stewart }
3830f8829a4aSRandall Stewart 
3831f8829a4aSRandall Stewart static void
3832830d754dSRandall Stewart sctp_notify_sender_dry_event(struct sctp_tcb *stcb,
3833830d754dSRandall Stewart     int so_locked
3834830d754dSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3835830d754dSRandall Stewart     SCTP_UNUSED
3836830d754dSRandall Stewart #endif
3837830d754dSRandall Stewart )
3838830d754dSRandall Stewart {
3839830d754dSRandall Stewart 	struct mbuf *m_notify;
3840830d754dSRandall Stewart 	struct sctp_sender_dry_event *event;
3841830d754dSRandall Stewart 	struct sctp_queued_to_read *control;
3842830d754dSRandall Stewart 
384360990c0cSMichael Tuexen 	if ((stcb == NULL) ||
384460990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DRYEVNT)) {
3845830d754dSRandall Stewart 		/* event not enabled */
3846830d754dSRandall Stewart 		return;
3847830d754dSRandall Stewart 	}
38480053ed28SMichael Tuexen 
3849eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_sender_dry_event), 0, M_NOWAIT, 1, MT_DATA);
3850830d754dSRandall Stewart 	if (m_notify == NULL) {
3851830d754dSRandall Stewart 		/* no space left */
3852830d754dSRandall Stewart 		return;
3853830d754dSRandall Stewart 	}
3854830d754dSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3855830d754dSRandall Stewart 	event = mtod(m_notify, struct sctp_sender_dry_event *);
3856e432298aSXin LI 	memset(event, 0, sizeof(struct sctp_sender_dry_event));
3857830d754dSRandall Stewart 	event->sender_dry_type = SCTP_SENDER_DRY_EVENT;
3858830d754dSRandall Stewart 	event->sender_dry_flags = 0;
3859830d754dSRandall Stewart 	event->sender_dry_length = sizeof(struct sctp_sender_dry_event);
3860830d754dSRandall Stewart 	event->sender_dry_assoc_id = sctp_get_associd(stcb);
3861830d754dSRandall Stewart 
3862830d754dSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_sender_dry_event);
3863830d754dSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3864830d754dSRandall Stewart 
3865830d754dSRandall Stewart 	/* append to socket */
3866830d754dSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
38677215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
38687215cc1bSMichael Tuexen 	    m_notify);
3869830d754dSRandall Stewart 	if (control == NULL) {
3870830d754dSRandall Stewart 		/* no memory */
3871830d754dSRandall Stewart 		sctp_m_freem(m_notify);
3872830d754dSRandall Stewart 		return;
3873830d754dSRandall Stewart 	}
3874830d754dSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3875830d754dSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3876830d754dSRandall Stewart 	/* not that we need this */
3877830d754dSRandall Stewart 	control->tail_mbuf = m_notify;
3878830d754dSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3879cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3880830d754dSRandall Stewart }
3881830d754dSRandall Stewart 
3882ea44232bSRandall Stewart 
3883c4e848b7SRandall Stewart void
3884c4e848b7SRandall Stewart sctp_notify_stream_reset_add(struct sctp_tcb *stcb, uint16_t numberin, uint16_t numberout, int flag)
3885ea44232bSRandall Stewart {
3886ea44232bSRandall Stewart 	struct mbuf *m_notify;
3887ea44232bSRandall Stewart 	struct sctp_queued_to_read *control;
3888c4e848b7SRandall Stewart 	struct sctp_stream_change_event *stradd;
3889ea44232bSRandall Stewart 
38908c501e51SMichael Tuexen 	if ((stcb == NULL) ||
38918c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_CHANGEEVNT))) {
3892ea44232bSRandall Stewart 		/* event not enabled */
3893ea44232bSRandall Stewart 		return;
3894ea44232bSRandall Stewart 	}
3895c4e848b7SRandall Stewart 	if ((stcb->asoc.peer_req_out) && flag) {
3896c4e848b7SRandall Stewart 		/* Peer made the request, don't tell the local user */
3897c4e848b7SRandall Stewart 		stcb->asoc.peer_req_out = 0;
3898c4e848b7SRandall Stewart 		return;
3899c4e848b7SRandall Stewart 	}
3900c4e848b7SRandall Stewart 	stcb->asoc.peer_req_out = 0;
3901e432298aSXin LI 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_stream_change_event), 0, M_NOWAIT, 1, MT_DATA);
3902ea44232bSRandall Stewart 	if (m_notify == NULL)
3903ea44232bSRandall Stewart 		/* no space left */
3904ea44232bSRandall Stewart 		return;
3905ea44232bSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3906c4e848b7SRandall Stewart 	stradd = mtod(m_notify, struct sctp_stream_change_event *);
3907e432298aSXin LI 	memset(stradd, 0, sizeof(struct sctp_stream_change_event));
3908c4e848b7SRandall Stewart 	stradd->strchange_type = SCTP_STREAM_CHANGE_EVENT;
3909c4e848b7SRandall Stewart 	stradd->strchange_flags = flag;
3910e432298aSXin LI 	stradd->strchange_length = sizeof(struct sctp_stream_change_event);
3911c4e848b7SRandall Stewart 	stradd->strchange_assoc_id = sctp_get_associd(stcb);
3912c4e848b7SRandall Stewart 	stradd->strchange_instrms = numberin;
3913c4e848b7SRandall Stewart 	stradd->strchange_outstrms = numberout;
3914e432298aSXin LI 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_stream_change_event);
3915ea44232bSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3916ea44232bSRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3917ea44232bSRandall Stewart 		/* no space */
3918ea44232bSRandall Stewart 		sctp_m_freem(m_notify);
3919ea44232bSRandall Stewart 		return;
3920ea44232bSRandall Stewart 	}
3921ea44232bSRandall Stewart 	/* append to socket */
3922ea44232bSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
39237215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3924ea44232bSRandall Stewart 	    m_notify);
3925ea44232bSRandall Stewart 	if (control == NULL) {
3926ea44232bSRandall Stewart 		/* no memory */
3927ea44232bSRandall Stewart 		sctp_m_freem(m_notify);
3928ea44232bSRandall Stewart 		return;
3929ea44232bSRandall Stewart 	}
3930ea44232bSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
393128cd0699SMichael Tuexen 	control->spec_flags = M_NOTIFICATION;
3932ea44232bSRandall Stewart 	/* not that we need this */
3933ea44232bSRandall Stewart 	control->tail_mbuf = m_notify;
3934ea44232bSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3935ea44232bSRandall Stewart 	    control,
3936cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3937ea44232bSRandall Stewart }
3938ea44232bSRandall Stewart 
3939c4e848b7SRandall Stewart void
3940c4e848b7SRandall Stewart sctp_notify_stream_reset_tsn(struct sctp_tcb *stcb, uint32_t sending_tsn, uint32_t recv_tsn, int flag)
3941c4e848b7SRandall Stewart {
3942c4e848b7SRandall Stewart 	struct mbuf *m_notify;
3943c4e848b7SRandall Stewart 	struct sctp_queued_to_read *control;
3944c4e848b7SRandall Stewart 	struct sctp_assoc_reset_event *strasoc;
3945c4e848b7SRandall Stewart 
39468c501e51SMichael Tuexen 	if ((stcb == NULL) ||
39478c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ASSOC_RESETEVNT))) {
3948c4e848b7SRandall Stewart 		/* event not enabled */
3949c4e848b7SRandall Stewart 		return;
3950c4e848b7SRandall Stewart 	}
3951e432298aSXin LI 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_assoc_reset_event), 0, M_NOWAIT, 1, MT_DATA);
3952c4e848b7SRandall Stewart 	if (m_notify == NULL)
3953c4e848b7SRandall Stewart 		/* no space left */
3954c4e848b7SRandall Stewart 		return;
3955c4e848b7SRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3956c4e848b7SRandall Stewart 	strasoc = mtod(m_notify, struct sctp_assoc_reset_event *);
3957e432298aSXin LI 	memset(strasoc, 0, sizeof(struct sctp_assoc_reset_event));
3958c4e848b7SRandall Stewart 	strasoc->assocreset_type = SCTP_ASSOC_RESET_EVENT;
3959c4e848b7SRandall Stewart 	strasoc->assocreset_flags = flag;
3960e432298aSXin LI 	strasoc->assocreset_length = sizeof(struct sctp_assoc_reset_event);
3961c4e848b7SRandall Stewart 	strasoc->assocreset_assoc_id = sctp_get_associd(stcb);
3962c4e848b7SRandall Stewart 	strasoc->assocreset_local_tsn = sending_tsn;
3963c4e848b7SRandall Stewart 	strasoc->assocreset_remote_tsn = recv_tsn;
3964e432298aSXin LI 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_assoc_reset_event);
3965c4e848b7SRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3966c4e848b7SRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3967c4e848b7SRandall Stewart 		/* no space */
3968c4e848b7SRandall Stewart 		sctp_m_freem(m_notify);
3969c4e848b7SRandall Stewart 		return;
3970c4e848b7SRandall Stewart 	}
3971c4e848b7SRandall Stewart 	/* append to socket */
3972c4e848b7SRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3973c4e848b7SRandall Stewart 	    0, 0, stcb->asoc.context, 0, 0, 0,
3974c4e848b7SRandall Stewart 	    m_notify);
3975c4e848b7SRandall Stewart 	if (control == NULL) {
3976c4e848b7SRandall Stewart 		/* no memory */
3977c4e848b7SRandall Stewart 		sctp_m_freem(m_notify);
3978c4e848b7SRandall Stewart 		return;
3979c4e848b7SRandall Stewart 	}
3980c4e848b7SRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
398128cd0699SMichael Tuexen 	control->spec_flags = M_NOTIFICATION;
3982c4e848b7SRandall Stewart 	/* not that we need this */
3983c4e848b7SRandall Stewart 	control->tail_mbuf = m_notify;
3984c4e848b7SRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3985c4e848b7SRandall Stewart 	    control,
3986c4e848b7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3987c4e848b7SRandall Stewart }
3988c4e848b7SRandall Stewart 
3989c4e848b7SRandall Stewart 
3990ea44232bSRandall Stewart 
3991830d754dSRandall Stewart static void
3992f8829a4aSRandall Stewart sctp_notify_stream_reset(struct sctp_tcb *stcb,
3993f8829a4aSRandall Stewart     int number_entries, uint16_t *list, int flag)
3994f8829a4aSRandall Stewart {
3995f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3996f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3997f8829a4aSRandall Stewart 	struct sctp_stream_reset_event *strreset;
3998f8829a4aSRandall Stewart 	int len;
3999f8829a4aSRandall Stewart 
40008c501e51SMichael Tuexen 	if ((stcb == NULL) ||
40018c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT))) {
4002f8829a4aSRandall Stewart 		/* event not enabled */
4003f8829a4aSRandall Stewart 		return;
4004830d754dSRandall Stewart 	}
40050053ed28SMichael Tuexen 
4006eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
4007f8829a4aSRandall Stewart 	if (m_notify == NULL)
4008f8829a4aSRandall Stewart 		/* no space left */
4009f8829a4aSRandall Stewart 		return;
4010139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
4011f8829a4aSRandall Stewart 	len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
4012f8829a4aSRandall Stewart 	if (len > M_TRAILINGSPACE(m_notify)) {
4013f8829a4aSRandall Stewart 		/* never enough room */
4014f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
4015f8829a4aSRandall Stewart 		return;
4016f8829a4aSRandall Stewart 	}
4017f8829a4aSRandall Stewart 	strreset = mtod(m_notify, struct sctp_stream_reset_event *);
4018e432298aSXin LI 	memset(strreset, 0, len);
4019f8829a4aSRandall Stewart 	strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
4020c4e848b7SRandall Stewart 	strreset->strreset_flags = flag;
4021f8829a4aSRandall Stewart 	strreset->strreset_length = len;
4022f8829a4aSRandall Stewart 	strreset->strreset_assoc_id = sctp_get_associd(stcb);
4023f8829a4aSRandall Stewart 	if (number_entries) {
4024f8829a4aSRandall Stewart 		int i;
4025f8829a4aSRandall Stewart 
4026f8829a4aSRandall Stewart 		for (i = 0; i < number_entries; i++) {
4027c4e848b7SRandall Stewart 			strreset->strreset_stream_list[i] = ntohs(list[i]);
4028f8829a4aSRandall Stewart 		}
4029f8829a4aSRandall Stewart 	}
4030139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = len;
4031139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
4032139bc87fSRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
4033f8829a4aSRandall Stewart 		/* no space */
4034f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
4035f8829a4aSRandall Stewart 		return;
4036f8829a4aSRandall Stewart 	}
4037f8829a4aSRandall Stewart 	/* append to socket */
4038f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
40397215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
4040f8829a4aSRandall Stewart 	    m_notify);
4041f8829a4aSRandall Stewart 	if (control == NULL) {
4042f8829a4aSRandall Stewart 		/* no memory */
4043f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
4044f8829a4aSRandall Stewart 		return;
4045f8829a4aSRandall Stewart 	}
4046139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
404728cd0699SMichael Tuexen 	control->spec_flags = M_NOTIFICATION;
4048f8829a4aSRandall Stewart 	/* not that we need this */
4049f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
4050f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
4051f8829a4aSRandall Stewart 	    control,
4052cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
4053f8829a4aSRandall Stewart }
4054f8829a4aSRandall Stewart 
4055f8829a4aSRandall Stewart 
4056389b1b11SMichael Tuexen static void
4057389b1b11SMichael Tuexen sctp_notify_remote_error(struct sctp_tcb *stcb, uint16_t error, struct sctp_error_chunk *chunk)
4058389b1b11SMichael Tuexen {
4059389b1b11SMichael Tuexen 	struct mbuf *m_notify;
4060389b1b11SMichael Tuexen 	struct sctp_remote_error *sre;
4061389b1b11SMichael Tuexen 	struct sctp_queued_to_read *control;
40629a8e3088SMichael Tuexen 	unsigned int notif_len;
40639a8e3088SMichael Tuexen 	uint16_t chunk_len;
4064389b1b11SMichael Tuexen 
4065389b1b11SMichael Tuexen 	if ((stcb == NULL) ||
4066389b1b11SMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPEERERR)) {
4067389b1b11SMichael Tuexen 		return;
4068389b1b11SMichael Tuexen 	}
4069389b1b11SMichael Tuexen 	if (chunk != NULL) {
4070c9eb4473SMichael Tuexen 		chunk_len = ntohs(chunk->ch.chunk_length);
40719669e724SMichael Tuexen 		/*
40729669e724SMichael Tuexen 		 * Only SCTP_CHUNK_BUFFER_SIZE are guaranteed to be
407345d41de5SMichael Tuexen 		 * contiguous.
40749669e724SMichael Tuexen 		 */
40759669e724SMichael Tuexen 		if (chunk_len > SCTP_CHUNK_BUFFER_SIZE) {
40769669e724SMichael Tuexen 			chunk_len = SCTP_CHUNK_BUFFER_SIZE;
40779669e724SMichael Tuexen 		}
4078389b1b11SMichael Tuexen 	} else {
4079389b1b11SMichael Tuexen 		chunk_len = 0;
4080389b1b11SMichael Tuexen 	}
40819a8e3088SMichael Tuexen 	notif_len = (unsigned int)(sizeof(struct sctp_remote_error) + chunk_len);
4082eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
4083389b1b11SMichael Tuexen 	if (m_notify == NULL) {
4084389b1b11SMichael Tuexen 		/* Retry with smaller value. */
40859a8e3088SMichael Tuexen 		notif_len = (unsigned int)sizeof(struct sctp_remote_error);
4086eb1b1807SGleb Smirnoff 		m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
4087389b1b11SMichael Tuexen 		if (m_notify == NULL) {
4088389b1b11SMichael Tuexen 			return;
4089389b1b11SMichael Tuexen 		}
4090389b1b11SMichael Tuexen 	}
4091389b1b11SMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = NULL;
4092389b1b11SMichael Tuexen 	sre = mtod(m_notify, struct sctp_remote_error *);
409356711f94SMichael Tuexen 	memset(sre, 0, notif_len);
4094389b1b11SMichael Tuexen 	sre->sre_type = SCTP_REMOTE_ERROR;
4095389b1b11SMichael Tuexen 	sre->sre_flags = 0;
4096389b1b11SMichael Tuexen 	sre->sre_length = sizeof(struct sctp_remote_error);
4097389b1b11SMichael Tuexen 	sre->sre_error = error;
4098389b1b11SMichael Tuexen 	sre->sre_assoc_id = sctp_get_associd(stcb);
4099389b1b11SMichael Tuexen 	if (notif_len > sizeof(struct sctp_remote_error)) {
4100389b1b11SMichael Tuexen 		memcpy(sre->sre_data, chunk, chunk_len);
4101389b1b11SMichael Tuexen 		sre->sre_length += chunk_len;
4102389b1b11SMichael Tuexen 	}
4103389b1b11SMichael Tuexen 	SCTP_BUF_LEN(m_notify) = sre->sre_length;
4104389b1b11SMichael Tuexen 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
4105389b1b11SMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
4106389b1b11SMichael Tuexen 	    m_notify);
4107389b1b11SMichael Tuexen 	if (control != NULL) {
4108389b1b11SMichael Tuexen 		control->length = SCTP_BUF_LEN(m_notify);
410928cd0699SMichael Tuexen 		control->spec_flags = M_NOTIFICATION;
4110389b1b11SMichael Tuexen 		/* not that we need this */
4111389b1b11SMichael Tuexen 		control->tail_mbuf = m_notify;
4112389b1b11SMichael Tuexen 		sctp_add_to_readq(stcb->sctp_ep, stcb,
4113389b1b11SMichael Tuexen 		    control,
4114389b1b11SMichael Tuexen 		    &stcb->sctp_socket->so_rcv, 1,
4115389b1b11SMichael Tuexen 		    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
4116389b1b11SMichael Tuexen 	} else {
4117389b1b11SMichael Tuexen 		sctp_m_freem(m_notify);
4118389b1b11SMichael Tuexen 	}
4119389b1b11SMichael Tuexen }
4120389b1b11SMichael Tuexen 
4121389b1b11SMichael Tuexen 
4122f8829a4aSRandall Stewart void
4123f8829a4aSRandall Stewart sctp_ulp_notify(uint32_t notification, struct sctp_tcb *stcb,
4124ceaad40aSRandall Stewart     uint32_t error, void *data, int so_locked
4125ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4126ceaad40aSRandall Stewart     SCTP_UNUSED
4127ceaad40aSRandall Stewart #endif
4128ceaad40aSRandall Stewart )
4129f8829a4aSRandall Stewart {
4130830d754dSRandall Stewart 	if ((stcb == NULL) ||
4131830d754dSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4132f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4133830d754dSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
4134830d754dSRandall Stewart 		/* If the socket is gone we are out of here */
4135f8829a4aSRandall Stewart 		return;
4136f8829a4aSRandall Stewart 	}
4137a99b6783SRandall Stewart 	if (stcb->sctp_socket->so_rcv.sb_state & SBS_CANTRCVMORE) {
4138a99b6783SRandall Stewart 		return;
4139a99b6783SRandall Stewart 	}
4140839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
4141839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
414217205eccSRandall Stewart 		if ((notification == SCTP_NOTIFY_INTERFACE_DOWN) ||
414317205eccSRandall Stewart 		    (notification == SCTP_NOTIFY_INTERFACE_UP) ||
414417205eccSRandall Stewart 		    (notification == SCTP_NOTIFY_INTERFACE_CONFIRMED)) {
414517205eccSRandall Stewart 			/* Don't report these in front states */
414617205eccSRandall Stewart 			return;
414717205eccSRandall Stewart 		}
414817205eccSRandall Stewart 	}
4149f8829a4aSRandall Stewart 	switch (notification) {
4150f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_UP:
4151f8829a4aSRandall Stewart 		if (stcb->asoc.assoc_up_sent == 0) {
4152410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_UP, stcb, error, NULL, 0, so_locked);
4153f8829a4aSRandall Stewart 			stcb->asoc.assoc_up_sent = 1;
4154f8829a4aSRandall Stewart 		}
41552afb3e84SRandall Stewart 		if (stcb->asoc.adaptation_needed && (stcb->asoc.adaptation_sent == 0)) {
41567215cc1bSMichael Tuexen 			sctp_notify_adaptation_layer(stcb);
41572afb3e84SRandall Stewart 		}
4158c79bec9cSMichael Tuexen 		if (stcb->asoc.auth_supported == 0) {
4159830d754dSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
4160830d754dSRandall Stewart 			    NULL, so_locked);
4161830d754dSRandall Stewart 		}
4162f8829a4aSRandall Stewart 		break;
4163f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_DOWN:
4164410a3b1eSMichael Tuexen 		sctp_notify_assoc_change(SCTP_SHUTDOWN_COMP, stcb, error, NULL, 0, so_locked);
4165f8829a4aSRandall Stewart 		break;
4166f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_DOWN:
4167f8829a4aSRandall Stewart 		{
4168f8829a4aSRandall Stewart 			struct sctp_nets *net;
4169f8829a4aSRandall Stewart 
4170f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
4171f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_UNREACHABLE,
41723cb3567dSMichael Tuexen 			    (struct sockaddr *)&net->ro._l_addr, error, so_locked);
4173f8829a4aSRandall Stewart 			break;
4174f8829a4aSRandall Stewart 		}
4175f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_UP:
4176f8829a4aSRandall Stewart 		{
4177f8829a4aSRandall Stewart 			struct sctp_nets *net;
4178f8829a4aSRandall Stewart 
4179f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
4180f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_AVAILABLE,
41813cb3567dSMichael Tuexen 			    (struct sockaddr *)&net->ro._l_addr, error, so_locked);
4182f8829a4aSRandall Stewart 			break;
4183f8829a4aSRandall Stewart 		}
4184f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_CONFIRMED:
4185f8829a4aSRandall Stewart 		{
4186f8829a4aSRandall Stewart 			struct sctp_nets *net;
4187f8829a4aSRandall Stewart 
4188f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
4189f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_CONFIRMED,
41903cb3567dSMichael Tuexen 			    (struct sockaddr *)&net->ro._l_addr, error, so_locked);
4191f8829a4aSRandall Stewart 			break;
4192f8829a4aSRandall Stewart 		}
4193f8829a4aSRandall Stewart 	case SCTP_NOTIFY_SPECIAL_SP_FAIL:
4194f8829a4aSRandall Stewart 		sctp_notify_send_failed2(stcb, error,
4195ceaad40aSRandall Stewart 		    (struct sctp_stream_queue_pending *)data, so_locked);
4196f8829a4aSRandall Stewart 		break;
41971edc9dbaSMichael Tuexen 	case SCTP_NOTIFY_SENT_DG_FAIL:
41981edc9dbaSMichael Tuexen 		sctp_notify_send_failed(stcb, 1, error,
41991edc9dbaSMichael Tuexen 		    (struct sctp_tmit_chunk *)data, so_locked);
42001edc9dbaSMichael Tuexen 		break;
42011edc9dbaSMichael Tuexen 	case SCTP_NOTIFY_UNSENT_DG_FAIL:
42021edc9dbaSMichael Tuexen 		sctp_notify_send_failed(stcb, 0, error,
4203ceaad40aSRandall Stewart 		    (struct sctp_tmit_chunk *)data, so_locked);
4204f8829a4aSRandall Stewart 		break;
4205f8829a4aSRandall Stewart 	case SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION:
42069a6142d8SRandall Stewart 		{
42079a6142d8SRandall Stewart 			uint32_t val;
42089a6142d8SRandall Stewart 
42099a6142d8SRandall Stewart 			val = *((uint32_t *)data);
42109a6142d8SRandall Stewart 
4211810ec536SMichael Tuexen 			sctp_notify_partial_delivery_indication(stcb, error, val, so_locked);
4212f8829a4aSRandall Stewart 			break;
4213810ec536SMichael Tuexen 		}
4214410a3b1eSMichael Tuexen 	case SCTP_NOTIFY_ASSOC_LOC_ABORTED:
4215839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
4216839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
4217410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 0, so_locked);
4218c105859eSRandall Stewart 		} else {
4219410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 0, so_locked);
4220410a3b1eSMichael Tuexen 		}
4221410a3b1eSMichael Tuexen 		break;
4222410a3b1eSMichael Tuexen 	case SCTP_NOTIFY_ASSOC_REM_ABORTED:
4223839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
4224839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
4225410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 1, so_locked);
4226410a3b1eSMichael Tuexen 		} else {
4227410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 1, so_locked);
4228c105859eSRandall Stewart 		}
4229f8829a4aSRandall Stewart 		break;
4230f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_RESTART:
4231410a3b1eSMichael Tuexen 		sctp_notify_assoc_change(SCTP_RESTART, stcb, error, NULL, 0, so_locked);
4232c79bec9cSMichael Tuexen 		if (stcb->asoc.auth_supported == 0) {
4233830d754dSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
4234830d754dSRandall Stewart 			    NULL, so_locked);
4235830d754dSRandall Stewart 		}
4236f8829a4aSRandall Stewart 		break;
4237f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_SEND:
4238d7714577SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *)data), SCTP_STREAM_RESET_OUTGOING_SSN);
4239f8829a4aSRandall Stewart 		break;
4240f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_RECV:
4241d7714577SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *)data), SCTP_STREAM_RESET_INCOMING);
4242f8829a4aSRandall Stewart 		break;
4243f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_FAILED_OUT:
4244c4e848b7SRandall Stewart 		sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
4245d7714577SMichael Tuexen 		    (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_FAILED));
4246f8829a4aSRandall Stewart 		break;
4247d4260646SMichael Tuexen 	case SCTP_NOTIFY_STR_RESET_DENIED_OUT:
4248d4260646SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
4249d4260646SMichael Tuexen 		    (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_DENIED));
4250d4260646SMichael Tuexen 		break;
4251f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_FAILED_IN:
4252c4e848b7SRandall Stewart 		sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
4253d7714577SMichael Tuexen 		    (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_FAILED));
4254f8829a4aSRandall Stewart 		break;
4255d4260646SMichael Tuexen 	case SCTP_NOTIFY_STR_RESET_DENIED_IN:
4256d4260646SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
4257d4260646SMichael Tuexen 		    (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_DENIED));
4258d4260646SMichael Tuexen 		break;
4259f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_ADD_IP:
4260f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_ADDED, data,
42613cb3567dSMichael Tuexen 		    error, so_locked);
4262f8829a4aSRandall Stewart 		break;
4263f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_DELETE_IP:
4264f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_REMOVED, data,
42653cb3567dSMichael Tuexen 		    error, so_locked);
4266f8829a4aSRandall Stewart 		break;
4267f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_SET_PRIMARY:
4268f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_MADE_PRIM, data,
42693cb3567dSMichael Tuexen 		    error, so_locked);
4270f8829a4aSRandall Stewart 		break;
4271f8829a4aSRandall Stewart 	case SCTP_NOTIFY_PEER_SHUTDOWN:
4272f8829a4aSRandall Stewart 		sctp_notify_shutdown_event(stcb);
4273f8829a4aSRandall Stewart 		break;
4274f8829a4aSRandall Stewart 	case SCTP_NOTIFY_AUTH_NEW_KEY:
427578f28045SMichael Tuexen 		sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY, error,
4276830d754dSRandall Stewart 		    (uint16_t)(uintptr_t)data,
4277830d754dSRandall Stewart 		    so_locked);
4278f8829a4aSRandall Stewart 		break;
4279830d754dSRandall Stewart 	case SCTP_NOTIFY_AUTH_FREE_KEY:
4280830d754dSRandall Stewart 		sctp_notify_authentication(stcb, SCTP_AUTH_FREE_KEY, error,
4281830d754dSRandall Stewart 		    (uint16_t)(uintptr_t)data,
4282830d754dSRandall Stewart 		    so_locked);
4283f8829a4aSRandall Stewart 		break;
4284830d754dSRandall Stewart 	case SCTP_NOTIFY_NO_PEER_AUTH:
4285830d754dSRandall Stewart 		sctp_notify_authentication(stcb, SCTP_AUTH_NO_AUTH, error,
4286830d754dSRandall Stewart 		    (uint16_t)(uintptr_t)data,
4287830d754dSRandall Stewart 		    so_locked);
4288830d754dSRandall Stewart 		break;
4289830d754dSRandall Stewart 	case SCTP_NOTIFY_SENDER_DRY:
4290830d754dSRandall Stewart 		sctp_notify_sender_dry_event(stcb, so_locked);
4291830d754dSRandall Stewart 		break;
4292389b1b11SMichael Tuexen 	case SCTP_NOTIFY_REMOTE_ERROR:
4293389b1b11SMichael Tuexen 		sctp_notify_remote_error(stcb, error, data);
4294389b1b11SMichael Tuexen 		break;
4295f8829a4aSRandall Stewart 	default:
4296ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_UTIL1, "%s: unknown notification %xh (%u)\n",
42976e9c45e0SMichael Tuexen 		    __func__, notification, notification);
4298f8829a4aSRandall Stewart 		break;
4299f8829a4aSRandall Stewart 	}			/* end switch */
4300f8829a4aSRandall Stewart }
4301f8829a4aSRandall Stewart 
4302f8829a4aSRandall Stewart void
43031edc9dbaSMichael Tuexen sctp_report_all_outbound(struct sctp_tcb *stcb, uint16_t error, int holds_lock, int so_locked
4304ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4305ceaad40aSRandall Stewart     SCTP_UNUSED
4306ceaad40aSRandall Stewart #endif
4307ceaad40aSRandall Stewart )
4308f8829a4aSRandall Stewart {
4309f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4310f8829a4aSRandall Stewart 	struct sctp_stream_out *outs;
43114a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
43124a9ef3f8SMichael Tuexen 	struct sctp_stream_queue_pending *sp, *nsp;
43137f34832bSRandall Stewart 	int i;
4314f8829a4aSRandall Stewart 
4315ad81507eSRandall Stewart 	if (stcb == NULL) {
4316ad81507eSRandall Stewart 		return;
4317ad81507eSRandall Stewart 	}
43184a9ef3f8SMichael Tuexen 	asoc = &stcb->asoc;
43194a9ef3f8SMichael Tuexen 	if (asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4320478fbccbSRandall Stewart 		/* already being freed */
4321478fbccbSRandall Stewart 		return;
4322478fbccbSRandall Stewart 	}
4323f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4324f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
43254a9ef3f8SMichael Tuexen 	    (asoc->state & SCTP_STATE_CLOSED_SOCKET)) {
4326f8829a4aSRandall Stewart 		return;
4327f8829a4aSRandall Stewart 	}
4328f8829a4aSRandall Stewart 	/* now through all the gunk freeing chunks */
4329ad81507eSRandall Stewart 	if (holds_lock == 0) {
43307f34832bSRandall Stewart 		SCTP_TCB_SEND_LOCK(stcb);
4331ad81507eSRandall Stewart 	}
4332d00aff5dSRandall Stewart 	/* sent queue SHOULD be empty */
43334a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
4334d00aff5dSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
4335d00aff5dSRandall Stewart 		asoc->sent_queue_cnt--;
4336325c8c46SMichael Tuexen 		if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
433749656eefSMichael Tuexen 			if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
433849656eefSMichael Tuexen 				asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
4339a7ad6026SMichael Tuexen #ifdef INVARIANTS
4340a7ad6026SMichael Tuexen 			} else {
434149656eefSMichael Tuexen 				panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
4342a7ad6026SMichael Tuexen #endif
4343a7ad6026SMichael Tuexen 			}
4344a7ad6026SMichael Tuexen 		}
43450c0982b8SRandall Stewart 		if (chk->data != NULL) {
4346d00aff5dSRandall Stewart 			sctp_free_bufspace(stcb, asoc, chk, 1);
43471edc9dbaSMichael Tuexen 			sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
43481edc9dbaSMichael Tuexen 			    error, chk, so_locked);
4349810ec536SMichael Tuexen 			if (chk->data) {
4350d00aff5dSRandall Stewart 				sctp_m_freem(chk->data);
4351d00aff5dSRandall Stewart 				chk->data = NULL;
4352d00aff5dSRandall Stewart 			}
4353810ec536SMichael Tuexen 		}
4354689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, so_locked);
4355d00aff5dSRandall Stewart 		/* sa_ignore FREED_MEMORY */
4356d00aff5dSRandall Stewart 	}
4357d00aff5dSRandall Stewart 	/* pending send queue SHOULD be empty */
43584a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
4359d00aff5dSRandall Stewart 		TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
4360d00aff5dSRandall Stewart 		asoc->send_queue_cnt--;
436149656eefSMichael Tuexen 		if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
436249656eefSMichael Tuexen 			asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
4363a7ad6026SMichael Tuexen #ifdef INVARIANTS
4364a7ad6026SMichael Tuexen 		} else {
436549656eefSMichael Tuexen 			panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
4366a7ad6026SMichael Tuexen #endif
4367a7ad6026SMichael Tuexen 		}
43680c0982b8SRandall Stewart 		if (chk->data != NULL) {
4369d00aff5dSRandall Stewart 			sctp_free_bufspace(stcb, asoc, chk, 1);
43701edc9dbaSMichael Tuexen 			sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
43711edc9dbaSMichael Tuexen 			    error, chk, so_locked);
4372810ec536SMichael Tuexen 			if (chk->data) {
4373d00aff5dSRandall Stewart 				sctp_m_freem(chk->data);
4374d00aff5dSRandall Stewart 				chk->data = NULL;
4375d00aff5dSRandall Stewart 			}
4376810ec536SMichael Tuexen 		}
4377689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, so_locked);
4378d00aff5dSRandall Stewart 		/* sa_ignore FREED_MEMORY */
4379d00aff5dSRandall Stewart 	}
43804a9ef3f8SMichael Tuexen 	for (i = 0; i < asoc->streamoutcnt; i++) {
43817f34832bSRandall Stewart 		/* For each stream */
43824a9ef3f8SMichael Tuexen 		outs = &asoc->strmout[i];
43837f34832bSRandall Stewart 		/* clean up any sends there */
43844a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
43854d58b0c3SMichael Tuexen 			atomic_subtract_int(&asoc->stream_queue_cnt, 1);
4386f8829a4aSRandall Stewart 			TAILQ_REMOVE(&outs->outqueue, sp, next);
4387d9707e43SMichael Tuexen 			stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, 1);
4388f8829a4aSRandall Stewart 			sctp_free_spbufspace(stcb, asoc, sp);
4389478fbccbSRandall Stewart 			if (sp->data) {
4390f8829a4aSRandall Stewart 				sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
43911edc9dbaSMichael Tuexen 				    error, (void *)sp, so_locked);
4392f8829a4aSRandall Stewart 				if (sp->data) {
4393f8829a4aSRandall Stewart 					sctp_m_freem(sp->data);
4394f8829a4aSRandall Stewart 					sp->data = NULL;
4395d07b2ac6SMichael Tuexen 					sp->tail_mbuf = NULL;
4396d07b2ac6SMichael Tuexen 					sp->length = 0;
4397f8829a4aSRandall Stewart 				}
4398478fbccbSRandall Stewart 			}
43999eea4a2dSMichael Tuexen 			if (sp->net) {
4400f8829a4aSRandall Stewart 				sctp_free_remote_addr(sp->net);
4401f8829a4aSRandall Stewart 				sp->net = NULL;
44029eea4a2dSMichael Tuexen 			}
4403f8829a4aSRandall Stewart 			/* Free the chunk */
4404689e6a5fSMichael Tuexen 			sctp_free_a_strmoq(stcb, sp, so_locked);
44053c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
4406f8829a4aSRandall Stewart 		}
4407f8829a4aSRandall Stewart 	}
4408f8829a4aSRandall Stewart 
4409ad81507eSRandall Stewart 	if (holds_lock == 0) {
44107f34832bSRandall Stewart 		SCTP_TCB_SEND_UNLOCK(stcb);
4411f8829a4aSRandall Stewart 	}
4412ad81507eSRandall Stewart }
4413f8829a4aSRandall Stewart 
4414f8829a4aSRandall Stewart void
4415410a3b1eSMichael Tuexen sctp_abort_notification(struct sctp_tcb *stcb, uint8_t from_peer, uint16_t error,
4416a2b42326SMichael Tuexen     struct sctp_abort_chunk *abort, int so_locked
4417ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4418ceaad40aSRandall Stewart     SCTP_UNUSED
4419ceaad40aSRandall Stewart #endif
4420ceaad40aSRandall Stewart )
4421f8829a4aSRandall Stewart {
4422ad81507eSRandall Stewart 	if (stcb == NULL) {
4423ad81507eSRandall Stewart 		return;
4424ad81507eSRandall Stewart 	}
4425c55b70ceSMichael Tuexen 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4426c55b70ceSMichael Tuexen 	    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4427c55b70ceSMichael Tuexen 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
4428c55b70ceSMichael Tuexen 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_WAS_ABORTED;
4429c55b70ceSMichael Tuexen 	}
4430f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4431f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4432f8829a4aSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
4433f8829a4aSRandall Stewart 		return;
4434f8829a4aSRandall Stewart 	}
4435f8829a4aSRandall Stewart 	/* Tell them we lost the asoc */
44366982c0faSMichael Tuexen 	sctp_report_all_outbound(stcb, error, 0, so_locked);
4437410a3b1eSMichael Tuexen 	if (from_peer) {
4438410a3b1eSMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_REM_ABORTED, stcb, error, abort, so_locked);
4439410a3b1eSMichael Tuexen 	} else {
4440410a3b1eSMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_LOC_ABORTED, stcb, error, abort, so_locked);
4441410a3b1eSMichael Tuexen 	}
4442f8829a4aSRandall Stewart }
4443f8829a4aSRandall Stewart 
4444f8829a4aSRandall Stewart void
4445f8829a4aSRandall Stewart sctp_abort_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4446b1754ad1SMichael Tuexen     struct mbuf *m, int iphlen,
4447b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
4448b1754ad1SMichael Tuexen     struct sctphdr *sh, struct mbuf *op_err,
4449457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
4450c54a18d2SRandall Stewart     uint32_t vrf_id, uint16_t port)
4451f8829a4aSRandall Stewart {
4452f8829a4aSRandall Stewart 	uint32_t vtag;
4453ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4454ceaad40aSRandall Stewart 	struct socket *so;
4455ceaad40aSRandall Stewart #endif
4456ceaad40aSRandall Stewart 
4457f8829a4aSRandall Stewart 	vtag = 0;
4458f8829a4aSRandall Stewart 	if (stcb != NULL) {
4459f8829a4aSRandall Stewart 		vtag = stcb->asoc.peer_vtag;
446017205eccSRandall Stewart 		vrf_id = stcb->asoc.vrf_id;
4461f8829a4aSRandall Stewart 	}
4462b1754ad1SMichael Tuexen 	sctp_send_abort(m, iphlen, src, dst, sh, vtag, op_err,
4463d089f9b9SMichael Tuexen 	    mflowtype, mflowid, inp->fibnum,
4464f30ac432SMichael Tuexen 	    vrf_id, port);
4465f8829a4aSRandall Stewart 	if (stcb != NULL) {
4466884d8c53SMichael Tuexen 		/* We have a TCB to abort, send notification too */
4467884d8c53SMichael Tuexen 		sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
4468839d21d6SMichael Tuexen 		SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_WAS_ABORTED);
4469f8829a4aSRandall Stewart 		/* Ok, now lets free it */
4470ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4471ceaad40aSRandall Stewart 		so = SCTP_INP_SO(inp);
4472ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
4473ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
4474ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
4475ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4476ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4477ceaad40aSRandall Stewart #endif
44780271d0cdSMichael Tuexen 		SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4479839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
4480839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
44810271d0cdSMichael Tuexen 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
44820271d0cdSMichael Tuexen 		}
4483ba785902SMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4484ba785902SMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_4);
4485ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4486ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4487ceaad40aSRandall Stewart #endif
4488f8829a4aSRandall Stewart 	}
4489f8829a4aSRandall Stewart }
4490f1f73e57SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
4491f1f73e57SRandall Stewart void
4492f1f73e57SRandall Stewart sctp_print_out_track_log(struct sctp_tcb *stcb)
4493f1f73e57SRandall Stewart {
449418e198d3SRandall Stewart #ifdef NOSIY_PRINTS
4495f1f73e57SRandall Stewart 	int i;
4496f1f73e57SRandall Stewart 
4497ad81507eSRandall Stewart 	SCTP_PRINTF("Last ep reason:%x\n", stcb->sctp_ep->last_abort_code);
4498ad81507eSRandall Stewart 	SCTP_PRINTF("IN bound TSN log-aaa\n");
4499f1f73e57SRandall Stewart 	if ((stcb->asoc.tsn_in_at == 0) && (stcb->asoc.tsn_in_wrapped == 0)) {
4500ad81507eSRandall Stewart 		SCTP_PRINTF("None rcvd\n");
4501f1f73e57SRandall Stewart 		goto none_in;
4502f1f73e57SRandall Stewart 	}
4503f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_in_wrapped) {
4504f1f73e57SRandall Stewart 		for (i = stcb->asoc.tsn_in_at; i < SCTP_TSN_LOG_SIZE; i++) {
4505ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4506f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].tsn,
4507f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].strm,
4508f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].seq,
4509f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].flgs,
4510f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].sz);
4511f1f73e57SRandall Stewart 		}
4512f1f73e57SRandall Stewart 	}
4513f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_in_at) {
4514f1f73e57SRandall Stewart 		for (i = 0; i < stcb->asoc.tsn_in_at; i++) {
4515ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4516f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].tsn,
4517f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].strm,
4518f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].seq,
4519f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].flgs,
4520f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].sz);
4521f1f73e57SRandall Stewart 		}
4522f1f73e57SRandall Stewart 	}
4523f1f73e57SRandall Stewart none_in:
4524ad81507eSRandall Stewart 	SCTP_PRINTF("OUT bound TSN log-aaa\n");
4525ad81507eSRandall Stewart 	if ((stcb->asoc.tsn_out_at == 0) &&
4526ad81507eSRandall Stewart 	    (stcb->asoc.tsn_out_wrapped == 0)) {
4527ad81507eSRandall Stewart 		SCTP_PRINTF("None sent\n");
4528f1f73e57SRandall Stewart 	}
4529f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_out_wrapped) {
4530f1f73e57SRandall Stewart 		for (i = stcb->asoc.tsn_out_at; i < SCTP_TSN_LOG_SIZE; i++) {
4531ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4532f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].tsn,
4533f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].strm,
4534f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].seq,
4535f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].flgs,
4536f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].sz);
4537f1f73e57SRandall Stewart 		}
4538f1f73e57SRandall Stewart 	}
4539f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_out_at) {
4540f1f73e57SRandall Stewart 		for (i = 0; i < stcb->asoc.tsn_out_at; i++) {
4541ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4542f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].tsn,
4543f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].strm,
4544f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].seq,
4545f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].flgs,
4546f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].sz);
4547f1f73e57SRandall Stewart 		}
4548f1f73e57SRandall Stewart 	}
454918e198d3SRandall Stewart #endif
4550f1f73e57SRandall Stewart }
4551f1f73e57SRandall Stewart #endif
4552f1f73e57SRandall Stewart 
4553f8829a4aSRandall Stewart void
4554f8829a4aSRandall Stewart sctp_abort_an_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4555a2b42326SMichael Tuexen     struct mbuf *op_err,
4556ceaad40aSRandall Stewart     int so_locked
4557ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4558ceaad40aSRandall Stewart     SCTP_UNUSED
4559ceaad40aSRandall Stewart #endif
4560ceaad40aSRandall Stewart )
4561f8829a4aSRandall Stewart {
4562ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4563ceaad40aSRandall Stewart 	struct socket *so;
4564ceaad40aSRandall Stewart #endif
4565ceaad40aSRandall Stewart 
4566ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4567ceaad40aSRandall Stewart 	so = SCTP_INP_SO(inp);
4568ceaad40aSRandall Stewart #endif
4569f8829a4aSRandall Stewart 	if (stcb == NULL) {
4570f8829a4aSRandall Stewart 		/* Got to have a TCB */
4571f8829a4aSRandall Stewart 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4572fe1831e0SMichael Tuexen 			if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4573b0552ae2SRandall Stewart 				sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4574b0552ae2SRandall Stewart 				    SCTP_CALLED_DIRECTLY_NOCMPSET);
4575f8829a4aSRandall Stewart 			}
4576f8829a4aSRandall Stewart 		}
4577f8829a4aSRandall Stewart 		return;
457863981c2bSRandall Stewart 	} else {
4579839d21d6SMichael Tuexen 		SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_WAS_ABORTED);
4580f8829a4aSRandall Stewart 	}
4581f8829a4aSRandall Stewart 	/* notify the peer */
4582ceaad40aSRandall Stewart 	sctp_send_abort_tcb(stcb, op_err, so_locked);
4583f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4584839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
4585839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4586f8829a4aSRandall Stewart 		SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4587f8829a4aSRandall Stewart 	}
4588884d8c53SMichael Tuexen 	/* notify the ulp */
4589884d8c53SMichael Tuexen 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
4590884d8c53SMichael Tuexen 		sctp_abort_notification(stcb, 0, 0, NULL, so_locked);
4591884d8c53SMichael Tuexen 	}
4592f8829a4aSRandall Stewart 	/* now free the asoc */
4593f1f73e57SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
4594f1f73e57SRandall Stewart 	sctp_print_out_track_log(stcb);
4595f1f73e57SRandall Stewart #endif
4596ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4597ceaad40aSRandall Stewart 	if (!so_locked) {
4598ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
4599ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
4600ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
4601ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4602ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4603ceaad40aSRandall Stewart 	}
4604ceaad40aSRandall Stewart #endif
4605ba785902SMichael Tuexen 	(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4606ba785902SMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_5);
4607ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4608ceaad40aSRandall Stewart 	if (!so_locked) {
4609ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4610ceaad40aSRandall Stewart 	}
4611ceaad40aSRandall Stewart #endif
4612f8829a4aSRandall Stewart }
4613f8829a4aSRandall Stewart 
4614f8829a4aSRandall Stewart void
4615b1754ad1SMichael Tuexen sctp_handle_ootb(struct mbuf *m, int iphlen, int offset,
4616b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
4617b1754ad1SMichael Tuexen     struct sctphdr *sh, struct sctp_inpcb *inp,
4618ff1ffd74SMichael Tuexen     struct mbuf *cause,
4619d089f9b9SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4620f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
4621f8829a4aSRandall Stewart {
4622f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch, chunk_buf;
4623f8829a4aSRandall Stewart 	unsigned int chk_length;
4624c58e60beSMichael Tuexen 	int contains_init_chunk;
4625f8829a4aSRandall Stewart 
4626f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue);
4627f8829a4aSRandall Stewart 	/* Generate a TO address for future reference */
4628f8829a4aSRandall Stewart 	if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4629fe1831e0SMichael Tuexen 		if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4630b0552ae2SRandall Stewart 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4631b0552ae2SRandall Stewart 			    SCTP_CALLED_DIRECTLY_NOCMPSET);
4632f8829a4aSRandall Stewart 		}
4633f8829a4aSRandall Stewart 	}
4634c58e60beSMichael Tuexen 	contains_init_chunk = 0;
4635f8829a4aSRandall Stewart 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4636f8829a4aSRandall Stewart 	    sizeof(*ch), (uint8_t *)&chunk_buf);
4637f8829a4aSRandall Stewart 	while (ch != NULL) {
4638f8829a4aSRandall Stewart 		chk_length = ntohs(ch->chunk_length);
4639f8829a4aSRandall Stewart 		if (chk_length < sizeof(*ch)) {
4640f8829a4aSRandall Stewart 			/* break to abort land */
4641f8829a4aSRandall Stewart 			break;
4642f8829a4aSRandall Stewart 		}
4643f8829a4aSRandall Stewart 		switch (ch->chunk_type) {
4644c58e60beSMichael Tuexen 		case SCTP_INIT:
4645c58e60beSMichael Tuexen 			contains_init_chunk = 1;
4646c58e60beSMichael Tuexen 			break;
4647f8829a4aSRandall Stewart 		case SCTP_PACKET_DROPPED:
4648f8829a4aSRandall Stewart 			/* we don't respond to pkt-dropped */
4649f8829a4aSRandall Stewart 			return;
4650f8829a4aSRandall Stewart 		case SCTP_ABORT_ASSOCIATION:
4651f8829a4aSRandall Stewart 			/* we don't respond with an ABORT to an ABORT */
4652f8829a4aSRandall Stewart 			return;
4653f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_COMPLETE:
4654f8829a4aSRandall Stewart 			/*
4655f8829a4aSRandall Stewart 			 * we ignore it since we are not waiting for it and
4656f8829a4aSRandall Stewart 			 * peer is gone
4657f8829a4aSRandall Stewart 			 */
4658f8829a4aSRandall Stewart 			return;
4659f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_ACK:
4660b1754ad1SMichael Tuexen 			sctp_send_shutdown_complete2(src, dst, sh,
4661d089f9b9SMichael Tuexen 			    mflowtype, mflowid, fibnum,
4662f30ac432SMichael Tuexen 			    vrf_id, port);
4663f8829a4aSRandall Stewart 			return;
4664f8829a4aSRandall Stewart 		default:
4665f8829a4aSRandall Stewart 			break;
4666f8829a4aSRandall Stewart 		}
4667f8829a4aSRandall Stewart 		offset += SCTP_SIZE32(chk_length);
4668f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4669f8829a4aSRandall Stewart 		    sizeof(*ch), (uint8_t *)&chunk_buf);
4670f8829a4aSRandall Stewart 	}
4671c58e60beSMichael Tuexen 	if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
4672c58e60beSMichael Tuexen 	    ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
4673c58e60beSMichael Tuexen 	    (contains_init_chunk == 0))) {
4674ff1ffd74SMichael Tuexen 		sctp_send_abort(m, iphlen, src, dst, sh, 0, cause,
4675d089f9b9SMichael Tuexen 		    mflowtype, mflowid, fibnum,
4676f30ac432SMichael Tuexen 		    vrf_id, port);
4677f8829a4aSRandall Stewart 	}
4678c58e60beSMichael Tuexen }
4679f8829a4aSRandall Stewart 
4680f8829a4aSRandall Stewart /*
4681f8829a4aSRandall Stewart  * check the inbound datagram to make sure there is not an abort inside it,
4682f8829a4aSRandall Stewart  * if there is return 1, else return 0.
4683f8829a4aSRandall Stewart  */
4684f8829a4aSRandall Stewart int
4685f8829a4aSRandall Stewart sctp_is_there_an_abort_here(struct mbuf *m, int iphlen, uint32_t *vtagfill)
4686f8829a4aSRandall Stewart {
4687f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
4688f8829a4aSRandall Stewart 	struct sctp_init_chunk *init_chk, chunk_buf;
4689f8829a4aSRandall Stewart 	int offset;
4690f8829a4aSRandall Stewart 	unsigned int chk_length;
4691f8829a4aSRandall Stewart 
4692f8829a4aSRandall Stewart 	offset = iphlen + sizeof(struct sctphdr);
4693f8829a4aSRandall Stewart 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch),
4694f8829a4aSRandall Stewart 	    (uint8_t *)&chunk_buf);
4695f8829a4aSRandall Stewart 	while (ch != NULL) {
4696f8829a4aSRandall Stewart 		chk_length = ntohs(ch->chunk_length);
4697f8829a4aSRandall Stewart 		if (chk_length < sizeof(*ch)) {
4698f8829a4aSRandall Stewart 			/* packet is probably corrupt */
4699f8829a4aSRandall Stewart 			break;
4700f8829a4aSRandall Stewart 		}
4701f8829a4aSRandall Stewart 		/* we seem to be ok, is it an abort? */
4702f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) {
4703f8829a4aSRandall Stewart 			/* yep, tell them */
4704f8829a4aSRandall Stewart 			return (1);
4705f8829a4aSRandall Stewart 		}
4706f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_INITIATION) {
4707f8829a4aSRandall Stewart 			/* need to update the Vtag */
4708f8829a4aSRandall Stewart 			init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4709f8829a4aSRandall Stewart 			    offset, sizeof(*init_chk), (uint8_t *)&chunk_buf);
4710f8829a4aSRandall Stewart 			if (init_chk != NULL) {
4711f8829a4aSRandall Stewart 				*vtagfill = ntohl(init_chk->init.initiate_tag);
4712f8829a4aSRandall Stewart 			}
4713f8829a4aSRandall Stewart 		}
4714f8829a4aSRandall Stewart 		/* Nope, move to the next chunk */
4715f8829a4aSRandall Stewart 		offset += SCTP_SIZE32(chk_length);
4716f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4717f8829a4aSRandall Stewart 		    sizeof(*ch), (uint8_t *)&chunk_buf);
4718f8829a4aSRandall Stewart 	}
4719f8829a4aSRandall Stewart 	return (0);
4720f8829a4aSRandall Stewart }
4721f8829a4aSRandall Stewart 
4722f8829a4aSRandall Stewart /*
4723f8829a4aSRandall Stewart  * currently (2/02), ifa_addr embeds scope_id's and don't have sin6_scope_id
4724f8829a4aSRandall Stewart  * set (i.e. it's 0) so, create this function to compare link local scopes
4725f8829a4aSRandall Stewart  */
47265e2c2d87SRandall Stewart #ifdef INET6
4727f8829a4aSRandall Stewart uint32_t
4728b0471b4bSMichael Tuexen sctp_is_same_scope(struct sockaddr_in6 *addr1, struct sockaddr_in6 *addr2)
4729b0471b4bSMichael Tuexen {
4730f8829a4aSRandall Stewart 	struct sockaddr_in6 a, b;
4731f8829a4aSRandall Stewart 
4732f8829a4aSRandall Stewart 	/* save copies */
4733f8829a4aSRandall Stewart 	a = *addr1;
4734f8829a4aSRandall Stewart 	b = *addr2;
4735f8829a4aSRandall Stewart 
4736f8829a4aSRandall Stewart 	if (a.sin6_scope_id == 0)
4737f8829a4aSRandall Stewart 		if (sa6_recoverscope(&a)) {
4738f8829a4aSRandall Stewart 			/* can't get scope, so can't match */
4739f8829a4aSRandall Stewart 			return (0);
4740f8829a4aSRandall Stewart 		}
4741f8829a4aSRandall Stewart 	if (b.sin6_scope_id == 0)
4742f8829a4aSRandall Stewart 		if (sa6_recoverscope(&b)) {
4743f8829a4aSRandall Stewart 			/* can't get scope, so can't match */
4744f8829a4aSRandall Stewart 			return (0);
4745f8829a4aSRandall Stewart 		}
4746f8829a4aSRandall Stewart 	if (a.sin6_scope_id != b.sin6_scope_id)
4747f8829a4aSRandall Stewart 		return (0);
4748f8829a4aSRandall Stewart 
4749f8829a4aSRandall Stewart 	return (1);
4750f8829a4aSRandall Stewart }
4751f8829a4aSRandall Stewart 
4752f8829a4aSRandall Stewart /*
4753f8829a4aSRandall Stewart  * returns a sockaddr_in6 with embedded scope recovered and removed
4754f8829a4aSRandall Stewart  */
4755f8829a4aSRandall Stewart struct sockaddr_in6 *
4756f8829a4aSRandall Stewart sctp_recover_scope(struct sockaddr_in6 *addr, struct sockaddr_in6 *store)
4757f8829a4aSRandall Stewart {
4758f8829a4aSRandall Stewart 	/* check and strip embedded scope junk */
4759f8829a4aSRandall Stewart 	if (addr->sin6_family == AF_INET6) {
4760f8829a4aSRandall Stewart 		if (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr)) {
4761f8829a4aSRandall Stewart 			if (addr->sin6_scope_id == 0) {
4762f8829a4aSRandall Stewart 				*store = *addr;
4763f8829a4aSRandall Stewart 				if (!sa6_recoverscope(store)) {
4764f8829a4aSRandall Stewart 					/* use the recovered scope */
4765f8829a4aSRandall Stewart 					addr = store;
4766f8829a4aSRandall Stewart 				}
4767f42a358aSRandall Stewart 			} else {
4768f8829a4aSRandall Stewart 				/* else, return the original "to" addr */
4769f42a358aSRandall Stewart 				in6_clearscope(&addr->sin6_addr);
4770f8829a4aSRandall Stewart 			}
4771f8829a4aSRandall Stewart 		}
4772f8829a4aSRandall Stewart 	}
4773f8829a4aSRandall Stewart 	return (addr);
4774f8829a4aSRandall Stewart }
47755e2c2d87SRandall Stewart #endif
47765e2c2d87SRandall Stewart 
4777f8829a4aSRandall Stewart /*
4778f8829a4aSRandall Stewart  * are the two addresses the same?  currently a "scopeless" check returns: 1
4779f8829a4aSRandall Stewart  * if same, 0 if not
4780f8829a4aSRandall Stewart  */
478172fb6fdbSRandall Stewart int
4782f8829a4aSRandall Stewart sctp_cmpaddr(struct sockaddr *sa1, struct sockaddr *sa2)
4783f8829a4aSRandall Stewart {
4784f8829a4aSRandall Stewart 
4785f8829a4aSRandall Stewart 	/* must be valid */
4786f8829a4aSRandall Stewart 	if (sa1 == NULL || sa2 == NULL)
4787f8829a4aSRandall Stewart 		return (0);
4788f8829a4aSRandall Stewart 
4789f8829a4aSRandall Stewart 	/* must be the same family */
4790f8829a4aSRandall Stewart 	if (sa1->sa_family != sa2->sa_family)
4791f8829a4aSRandall Stewart 		return (0);
4792f8829a4aSRandall Stewart 
47935e2c2d87SRandall Stewart 	switch (sa1->sa_family) {
47945e2c2d87SRandall Stewart #ifdef INET6
47955e2c2d87SRandall Stewart 	case AF_INET6:
47965e2c2d87SRandall Stewart 		{
4797f8829a4aSRandall Stewart 			/* IPv6 addresses */
4798f8829a4aSRandall Stewart 			struct sockaddr_in6 *sin6_1, *sin6_2;
4799f8829a4aSRandall Stewart 
4800f8829a4aSRandall Stewart 			sin6_1 = (struct sockaddr_in6 *)sa1;
4801f8829a4aSRandall Stewart 			sin6_2 = (struct sockaddr_in6 *)sa2;
4802c54a18d2SRandall Stewart 			return (SCTP6_ARE_ADDR_EQUAL(sin6_1,
4803c54a18d2SRandall Stewart 			    sin6_2));
48045e2c2d87SRandall Stewart 		}
48055e2c2d87SRandall Stewart #endif
4806ea5eba11SMichael Tuexen #ifdef INET
48075e2c2d87SRandall Stewart 	case AF_INET:
48085e2c2d87SRandall Stewart 		{
4809f8829a4aSRandall Stewart 			/* IPv4 addresses */
4810f8829a4aSRandall Stewart 			struct sockaddr_in *sin_1, *sin_2;
4811f8829a4aSRandall Stewart 
4812f8829a4aSRandall Stewart 			sin_1 = (struct sockaddr_in *)sa1;
4813f8829a4aSRandall Stewart 			sin_2 = (struct sockaddr_in *)sa2;
4814f8829a4aSRandall Stewart 			return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr);
48155e2c2d87SRandall Stewart 		}
4816ea5eba11SMichael Tuexen #endif
48175e2c2d87SRandall Stewart 	default:
4818f8829a4aSRandall Stewart 		/* we don't do these... */
4819f8829a4aSRandall Stewart 		return (0);
4820f8829a4aSRandall Stewart 	}
4821f8829a4aSRandall Stewart }
4822f8829a4aSRandall Stewart 
4823f8829a4aSRandall Stewart void
4824f8829a4aSRandall Stewart sctp_print_address(struct sockaddr *sa)
4825f8829a4aSRandall Stewart {
48265e2c2d87SRandall Stewart #ifdef INET6
48277d32aa0cSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
48285e2c2d87SRandall Stewart #endif
48295e2c2d87SRandall Stewart 
48305e2c2d87SRandall Stewart 	switch (sa->sa_family) {
48315e2c2d87SRandall Stewart #ifdef INET6
48325e2c2d87SRandall Stewart 	case AF_INET6:
48335e2c2d87SRandall Stewart 		{
4834ad81507eSRandall Stewart 			struct sockaddr_in6 *sin6;
4835ad81507eSRandall Stewart 
4836f8829a4aSRandall Stewart 			sin6 = (struct sockaddr_in6 *)sa;
4837ad81507eSRandall Stewart 			SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n",
48387d32aa0cSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &sin6->sin6_addr),
48397d32aa0cSBjoern A. Zeeb 			    ntohs(sin6->sin6_port),
4840f8829a4aSRandall Stewart 			    sin6->sin6_scope_id);
48415e2c2d87SRandall Stewart 			break;
48425e2c2d87SRandall Stewart 		}
48435e2c2d87SRandall Stewart #endif
4844ea5eba11SMichael Tuexen #ifdef INET
48455e2c2d87SRandall Stewart 	case AF_INET:
48465e2c2d87SRandall Stewart 		{
4847f8829a4aSRandall Stewart 			struct sockaddr_in *sin;
4848f8829a4aSRandall Stewart 			unsigned char *p;
4849f8829a4aSRandall Stewart 
4850f8829a4aSRandall Stewart 			sin = (struct sockaddr_in *)sa;
4851f8829a4aSRandall Stewart 			p = (unsigned char *)&sin->sin_addr;
4852ad81507eSRandall Stewart 			SCTP_PRINTF("IPv4 address: %u.%u.%u.%u:%d\n",
4853f8829a4aSRandall Stewart 			    p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
48545e2c2d87SRandall Stewart 			break;
48555e2c2d87SRandall Stewart 		}
4856ea5eba11SMichael Tuexen #endif
48575e2c2d87SRandall Stewart 	default:
4858ad81507eSRandall Stewart 		SCTP_PRINTF("?\n");
48595e2c2d87SRandall Stewart 		break;
4860f8829a4aSRandall Stewart 	}
4861f8829a4aSRandall Stewart }
4862f8829a4aSRandall Stewart 
4863f8829a4aSRandall Stewart void
4864f8829a4aSRandall Stewart sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
4865f8829a4aSRandall Stewart     struct sctp_inpcb *new_inp,
4866d06c82f1SRandall Stewart     struct sctp_tcb *stcb,
4867d06c82f1SRandall Stewart     int waitflags)
4868f8829a4aSRandall Stewart {
4869f8829a4aSRandall Stewart 	/*
4870f8829a4aSRandall Stewart 	 * go through our old INP and pull off any control structures that
4871f8829a4aSRandall Stewart 	 * belong to stcb and move then to the new inp.
4872f8829a4aSRandall Stewart 	 */
4873f8829a4aSRandall Stewart 	struct socket *old_so, *new_so;
4874f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control, *nctl;
4875f8829a4aSRandall Stewart 	struct sctp_readhead tmp_queue;
4876f8829a4aSRandall Stewart 	struct mbuf *m;
4877bff64a4dSRandall Stewart 	int error = 0;
4878f8829a4aSRandall Stewart 
4879f8829a4aSRandall Stewart 	old_so = old_inp->sctp_socket;
4880f8829a4aSRandall Stewart 	new_so = new_inp->sctp_socket;
4881f8829a4aSRandall Stewart 	TAILQ_INIT(&tmp_queue);
4882d06c82f1SRandall Stewart 	error = sblock(&old_so->so_rcv, waitflags);
4883f8829a4aSRandall Stewart 	if (error) {
4884f8829a4aSRandall Stewart 		/*
4885f8829a4aSRandall Stewart 		 * Gak, can't get sblock, we have a problem. data will be
4886f8829a4aSRandall Stewart 		 * left stranded.. and we don't dare look at it since the
4887f8829a4aSRandall Stewart 		 * other thread may be reading something. Oh well, its a
4888f8829a4aSRandall Stewart 		 * screwed up app that does a peeloff OR a accept while
4889f8829a4aSRandall Stewart 		 * reading from the main socket... actually its only the
4890f8829a4aSRandall Stewart 		 * peeloff() case, since I think read will fail on a
4891f8829a4aSRandall Stewart 		 * listening socket..
4892f8829a4aSRandall Stewart 		 */
4893f8829a4aSRandall Stewart 		return;
4894f8829a4aSRandall Stewart 	}
4895f8829a4aSRandall Stewart 	/* lock the socket buffers */
4896f8829a4aSRandall Stewart 	SCTP_INP_READ_LOCK(old_inp);
48974a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(control, &old_inp->read_queue, next, nctl) {
4898f8829a4aSRandall Stewart 		/* Pull off all for out target stcb */
4899f8829a4aSRandall Stewart 		if (control->stcb == stcb) {
4900f8829a4aSRandall Stewart 			/* remove it we want it */
4901f8829a4aSRandall Stewart 			TAILQ_REMOVE(&old_inp->read_queue, control, next);
4902f8829a4aSRandall Stewart 			TAILQ_INSERT_TAIL(&tmp_queue, control, next);
4903f8829a4aSRandall Stewart 			m = control->data;
4904f8829a4aSRandall Stewart 			while (m) {
4905b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4906139bc87fSRandall Stewart 					sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
490780fefe0aSRandall Stewart 				}
4908f8829a4aSRandall Stewart 				sctp_sbfree(control, stcb, &old_so->so_rcv, m);
4909b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4910f8829a4aSRandall Stewart 					sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
491180fefe0aSRandall Stewart 				}
4912139bc87fSRandall Stewart 				m = SCTP_BUF_NEXT(m);
4913f8829a4aSRandall Stewart 			}
4914f8829a4aSRandall Stewart 		}
4915f8829a4aSRandall Stewart 	}
4916f8829a4aSRandall Stewart 	SCTP_INP_READ_UNLOCK(old_inp);
4917f8829a4aSRandall Stewart 	/* Remove the sb-lock on the old socket */
4918f8829a4aSRandall Stewart 
4919f8829a4aSRandall Stewart 	sbunlock(&old_so->so_rcv);
4920f8829a4aSRandall Stewart 	/* Now we move them over to the new socket buffer */
4921f8829a4aSRandall Stewart 	SCTP_INP_READ_LOCK(new_inp);
49224a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(control, &tmp_queue, next, nctl) {
4923f8829a4aSRandall Stewart 		TAILQ_INSERT_TAIL(&new_inp->read_queue, control, next);
4924f8829a4aSRandall Stewart 		m = control->data;
4925f8829a4aSRandall Stewart 		while (m) {
4926b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4927139bc87fSRandall Stewart 				sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
492880fefe0aSRandall Stewart 			}
4929f8829a4aSRandall Stewart 			sctp_sballoc(stcb, &new_so->so_rcv, m);
4930b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4931f8829a4aSRandall Stewart 				sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
493280fefe0aSRandall Stewart 			}
4933139bc87fSRandall Stewart 			m = SCTP_BUF_NEXT(m);
4934f8829a4aSRandall Stewart 		}
4935f8829a4aSRandall Stewart 	}
4936f8829a4aSRandall Stewart 	SCTP_INP_READ_UNLOCK(new_inp);
4937f8829a4aSRandall Stewart }
4938f8829a4aSRandall Stewart 
4939f8829a4aSRandall Stewart void
4940b1deed45SMichael Tuexen sctp_wakeup_the_read_socket(struct sctp_inpcb *inp,
4941b1deed45SMichael Tuexen     struct sctp_tcb *stcb,
4942b1deed45SMichael Tuexen     int so_locked
4943b1deed45SMichael Tuexen #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4944b1deed45SMichael Tuexen     SCTP_UNUSED
4945b1deed45SMichael Tuexen #endif
4946b1deed45SMichael Tuexen )
494744249214SRandall Stewart {
4948b1deed45SMichael Tuexen 	if ((inp != NULL) && (inp->sctp_socket != NULL)) {
494944249214SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
495044249214SRandall Stewart 		struct socket *so;
495144249214SRandall Stewart 
495244249214SRandall Stewart 		so = SCTP_INP_SO(inp);
495344249214SRandall Stewart 		if (!so_locked) {
495444249214SRandall Stewart 			if (stcb) {
495544249214SRandall Stewart 				atomic_add_int(&stcb->asoc.refcnt, 1);
495644249214SRandall Stewart 				SCTP_TCB_UNLOCK(stcb);
495744249214SRandall Stewart 			}
495844249214SRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
495944249214SRandall Stewart 			if (stcb) {
496044249214SRandall Stewart 				SCTP_TCB_LOCK(stcb);
496144249214SRandall Stewart 				atomic_subtract_int(&stcb->asoc.refcnt, 1);
496244249214SRandall Stewart 			}
496344249214SRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
496444249214SRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
496544249214SRandall Stewart 				return;
496644249214SRandall Stewart 			}
496744249214SRandall Stewart 		}
496844249214SRandall Stewart #endif
496944249214SRandall Stewart 		sctp_sorwakeup(inp, inp->sctp_socket);
497044249214SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
497144249214SRandall Stewart 		if (!so_locked) {
497244249214SRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
497344249214SRandall Stewart 		}
497444249214SRandall Stewart #endif
497544249214SRandall Stewart 	}
497644249214SRandall Stewart }
497744249214SRandall Stewart 
497844249214SRandall Stewart void
4979f8829a4aSRandall Stewart sctp_add_to_readq(struct sctp_inpcb *inp,
4980f8829a4aSRandall Stewart     struct sctp_tcb *stcb,
4981f8829a4aSRandall Stewart     struct sctp_queued_to_read *control,
4982f8829a4aSRandall Stewart     struct sockbuf *sb,
4983ceaad40aSRandall Stewart     int end,
4984cfde3ff7SRandall Stewart     int inp_read_lock_held,
4985ceaad40aSRandall Stewart     int so_locked
4986ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4987ceaad40aSRandall Stewart     SCTP_UNUSED
4988ceaad40aSRandall Stewart #endif
4989ceaad40aSRandall Stewart )
4990f8829a4aSRandall Stewart {
4991f8829a4aSRandall Stewart 	/*
4992f8829a4aSRandall Stewart 	 * Here we must place the control on the end of the socket read
49934e88d37aSMichael Tuexen 	 * queue AND increment sb_cc so that select will work properly on
4994f8829a4aSRandall Stewart 	 * read.
4995f8829a4aSRandall Stewart 	 */
4996f8829a4aSRandall Stewart 	struct mbuf *m, *prev = NULL;
4997f8829a4aSRandall Stewart 
499803b0b021SRandall Stewart 	if (inp == NULL) {
499903b0b021SRandall Stewart 		/* Gak, TSNH!! */
5000a5d547adSRandall Stewart #ifdef INVARIANTS
500103b0b021SRandall Stewart 		panic("Gak, inp NULL on add_to_readq");
500203b0b021SRandall Stewart #endif
500303b0b021SRandall Stewart 		return;
500403b0b021SRandall Stewart 	}
5005cfde3ff7SRandall Stewart 	if (inp_read_lock_held == 0)
5006f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(inp);
5007cd1386abSMichael Tuexen 	if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
50088a3cfbffSMichael Tuexen 		if (!control->on_strm_q) {
5009cd1386abSMichael Tuexen 			sctp_free_remote_addr(control->whoFrom);
5010cd1386abSMichael Tuexen 			if (control->data) {
5011cd1386abSMichael Tuexen 				sctp_m_freem(control->data);
5012cd1386abSMichael Tuexen 				control->data = NULL;
5013cd1386abSMichael Tuexen 			}
501444249214SRandall Stewart 			sctp_free_a_readq(stcb, control);
50158a3cfbffSMichael Tuexen 		}
5016cd1386abSMichael Tuexen 		if (inp_read_lock_held == 0)
5017cd1386abSMichael Tuexen 			SCTP_INP_READ_UNLOCK(inp);
5018cd1386abSMichael Tuexen 		return;
5019cd1386abSMichael Tuexen 	}
502042551e99SRandall Stewart 	if (!(control->spec_flags & M_NOTIFICATION)) {
5021a5d547adSRandall Stewart 		atomic_add_int(&inp->total_recvs, 1);
502242551e99SRandall Stewart 		if (!control->do_not_ref_stcb) {
5023a5d547adSRandall Stewart 			atomic_add_int(&stcb->total_recvs, 1);
502442551e99SRandall Stewart 		}
502542551e99SRandall Stewart 	}
5026f8829a4aSRandall Stewart 	m = control->data;
5027f8829a4aSRandall Stewart 	control->held_length = 0;
5028f8829a4aSRandall Stewart 	control->length = 0;
5029f8829a4aSRandall Stewart 	while (m) {
5030139bc87fSRandall Stewart 		if (SCTP_BUF_LEN(m) == 0) {
5031f8829a4aSRandall Stewart 			/* Skip mbufs with NO length */
5032f8829a4aSRandall Stewart 			if (prev == NULL) {
5033f8829a4aSRandall Stewart 				/* First one */
5034f8829a4aSRandall Stewart 				control->data = sctp_m_free(m);
5035f8829a4aSRandall Stewart 				m = control->data;
5036f8829a4aSRandall Stewart 			} else {
5037139bc87fSRandall Stewart 				SCTP_BUF_NEXT(prev) = sctp_m_free(m);
5038139bc87fSRandall Stewart 				m = SCTP_BUF_NEXT(prev);
5039f8829a4aSRandall Stewart 			}
5040f8829a4aSRandall Stewart 			if (m == NULL) {
5041c2ede4b3SMartin Blapp 				control->tail_mbuf = prev;
5042f8829a4aSRandall Stewart 			}
5043f8829a4aSRandall Stewart 			continue;
5044f8829a4aSRandall Stewart 		}
5045f8829a4aSRandall Stewart 		prev = m;
5046b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5047139bc87fSRandall Stewart 			sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
504880fefe0aSRandall Stewart 		}
5049f8829a4aSRandall Stewart 		sctp_sballoc(stcb, sb, m);
5050b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5051f8829a4aSRandall Stewart 			sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
505280fefe0aSRandall Stewart 		}
5053139bc87fSRandall Stewart 		atomic_add_int(&control->length, SCTP_BUF_LEN(m));
5054139bc87fSRandall Stewart 		m = SCTP_BUF_NEXT(m);
5055f8829a4aSRandall Stewart 	}
5056f8829a4aSRandall Stewart 	if (prev != NULL) {
5057f8829a4aSRandall Stewart 		control->tail_mbuf = prev;
5058f8829a4aSRandall Stewart 	} else {
5059139bc87fSRandall Stewart 		/* Everything got collapsed out?? */
50608a3cfbffSMichael Tuexen 		if (!control->on_strm_q) {
5061cd1386abSMichael Tuexen 			sctp_free_remote_addr(control->whoFrom);
506244249214SRandall Stewart 			sctp_free_a_readq(stcb, control);
50638a3cfbffSMichael Tuexen 		}
5064cfde3ff7SRandall Stewart 		if (inp_read_lock_held == 0)
506547a490cbSMichael Tuexen 			SCTP_INP_READ_UNLOCK(inp);
5066f8829a4aSRandall Stewart 		return;
5067f8829a4aSRandall Stewart 	}
5068f8829a4aSRandall Stewart 	if (end) {
5069f8829a4aSRandall Stewart 		control->end_added = 1;
5070f8829a4aSRandall Stewart 	}
5071f8829a4aSRandall Stewart 	TAILQ_INSERT_TAIL(&inp->read_queue, control, next);
507244249214SRandall Stewart 	control->on_read_q = 1;
5073cfde3ff7SRandall Stewart 	if (inp_read_lock_held == 0)
5074f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
5075f8829a4aSRandall Stewart 	if (inp && inp->sctp_socket) {
5076b1deed45SMichael Tuexen 		sctp_wakeup_the_read_socket(inp, stcb, so_locked);
5077f8829a4aSRandall Stewart 	}
5078f8829a4aSRandall Stewart }
5079f8829a4aSRandall Stewart 
5080f8829a4aSRandall Stewart /*************HOLD THIS COMMENT FOR PATCH FILE OF
5081f8829a4aSRandall Stewart  *************ALTERNATE ROUTING CODE
5082f8829a4aSRandall Stewart  */
5083f8829a4aSRandall Stewart 
5084f8829a4aSRandall Stewart /*************HOLD THIS COMMENT FOR END OF PATCH FILE OF
5085f8829a4aSRandall Stewart  *************ALTERNATE ROUTING CODE
5086f8829a4aSRandall Stewart  */
5087f8829a4aSRandall Stewart 
5088f8829a4aSRandall Stewart struct mbuf *
5089ff1ffd74SMichael Tuexen sctp_generate_cause(uint16_t code, char *info)
5090f8829a4aSRandall Stewart {
5091f8829a4aSRandall Stewart 	struct mbuf *m;
5092ff1ffd74SMichael Tuexen 	struct sctp_gen_error_cause *cause;
50939a8e3088SMichael Tuexen 	size_t info_len;
50949a8e3088SMichael Tuexen 	uint16_t len;
5095f8829a4aSRandall Stewart 
5096ff1ffd74SMichael Tuexen 	if ((code == 0) || (info == NULL)) {
5097ff1ffd74SMichael Tuexen 		return (NULL);
5098ff1ffd74SMichael Tuexen 	}
5099ff1ffd74SMichael Tuexen 	info_len = strlen(info);
51009a8e3088SMichael Tuexen 	if (info_len > (SCTP_MAX_CAUSE_LENGTH - sizeof(struct sctp_paramhdr))) {
51019a8e3088SMichael Tuexen 		return (NULL);
51029a8e3088SMichael Tuexen 	}
51039a8e3088SMichael Tuexen 	len = (uint16_t)(sizeof(struct sctp_paramhdr) + info_len);
5104ff1ffd74SMichael Tuexen 	m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
5105ff1ffd74SMichael Tuexen 	if (m != NULL) {
5106ff1ffd74SMichael Tuexen 		SCTP_BUF_LEN(m) = len;
5107ff1ffd74SMichael Tuexen 		cause = mtod(m, struct sctp_gen_error_cause *);
5108ff1ffd74SMichael Tuexen 		cause->code = htons(code);
51099a8e3088SMichael Tuexen 		cause->length = htons(len);
5110ff1ffd74SMichael Tuexen 		memcpy(cause->info, info, info_len);
5111f8829a4aSRandall Stewart 	}
5112f8829a4aSRandall Stewart 	return (m);
5113f8829a4aSRandall Stewart }
5114f8829a4aSRandall Stewart 
511532451da4SMichael Tuexen struct mbuf *
511632451da4SMichael Tuexen sctp_generate_no_user_data_cause(uint32_t tsn)
511732451da4SMichael Tuexen {
511832451da4SMichael Tuexen 	struct mbuf *m;
511932451da4SMichael Tuexen 	struct sctp_error_no_user_data *no_user_data_cause;
51209a8e3088SMichael Tuexen 	uint16_t len;
512132451da4SMichael Tuexen 
51229a8e3088SMichael Tuexen 	len = (uint16_t)sizeof(struct sctp_error_no_user_data);
512332451da4SMichael Tuexen 	m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
512432451da4SMichael Tuexen 	if (m != NULL) {
512532451da4SMichael Tuexen 		SCTP_BUF_LEN(m) = len;
512632451da4SMichael Tuexen 		no_user_data_cause = mtod(m, struct sctp_error_no_user_data *);
512732451da4SMichael Tuexen 		no_user_data_cause->cause.code = htons(SCTP_CAUSE_NO_USER_DATA);
51289a8e3088SMichael Tuexen 		no_user_data_cause->cause.length = htons(len);
51298b9c95f4SMichael Tuexen 		no_user_data_cause->tsn = htonl(tsn);
513032451da4SMichael Tuexen 	}
513132451da4SMichael Tuexen 	return (m);
513232451da4SMichael Tuexen }
513332451da4SMichael Tuexen 
5134f8829a4aSRandall Stewart #ifdef SCTP_MBCNT_LOGGING
5135f8829a4aSRandall Stewart void
5136f8829a4aSRandall Stewart sctp_free_bufspace(struct sctp_tcb *stcb, struct sctp_association *asoc,
5137f8829a4aSRandall Stewart     struct sctp_tmit_chunk *tp1, int chk_cnt)
5138f8829a4aSRandall Stewart {
5139f8829a4aSRandall Stewart 	if (tp1->data == NULL) {
5140f8829a4aSRandall Stewart 		return;
5141f8829a4aSRandall Stewart 	}
5142f8829a4aSRandall Stewart 	asoc->chunks_on_out_queue -= chk_cnt;
5143b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBCNT_LOGGING_ENABLE) {
5144f8829a4aSRandall Stewart 		sctp_log_mbcnt(SCTP_LOG_MBCNT_DECREASE,
5145f8829a4aSRandall Stewart 		    asoc->total_output_queue_size,
5146f8829a4aSRandall Stewart 		    tp1->book_size,
5147f8829a4aSRandall Stewart 		    0,
5148f8829a4aSRandall Stewart 		    tp1->mbcnt);
514980fefe0aSRandall Stewart 	}
5150f8829a4aSRandall Stewart 	if (asoc->total_output_queue_size >= tp1->book_size) {
515144b7479bSRandall Stewart 		atomic_add_int(&asoc->total_output_queue_size, -tp1->book_size);
5152f8829a4aSRandall Stewart 	} else {
5153f8829a4aSRandall Stewart 		asoc->total_output_queue_size = 0;
5154f8829a4aSRandall Stewart 	}
5155f8829a4aSRandall Stewart 
5156f8829a4aSRandall Stewart 	if (stcb->sctp_socket && (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) ||
5157f8829a4aSRandall Stewart 	    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) {
51584e88d37aSMichael Tuexen 		if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) {
51594e88d37aSMichael Tuexen 			stcb->sctp_socket->so_snd.sb_cc -= tp1->book_size;
5160f8829a4aSRandall Stewart 		} else {
51614e88d37aSMichael Tuexen 			stcb->sctp_socket->so_snd.sb_cc = 0;
5162f8829a4aSRandall Stewart 
5163f8829a4aSRandall Stewart 		}
5164f8829a4aSRandall Stewart 	}
5165f8829a4aSRandall Stewart }
5166f8829a4aSRandall Stewart 
5167f8829a4aSRandall Stewart #endif
5168f8829a4aSRandall Stewart 
5169f8829a4aSRandall Stewart int
5170f8829a4aSRandall Stewart sctp_release_pr_sctp_chunk(struct sctp_tcb *stcb, struct sctp_tmit_chunk *tp1,
51711edc9dbaSMichael Tuexen     uint8_t sent, int so_locked
5172ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
5173ceaad40aSRandall Stewart     SCTP_UNUSED
5174ceaad40aSRandall Stewart #endif
5175ceaad40aSRandall Stewart )
5176f8829a4aSRandall Stewart {
51770c0982b8SRandall Stewart 	struct sctp_stream_out *strq;
51784a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk = NULL, *tp2;
51790c0982b8SRandall Stewart 	struct sctp_stream_queue_pending *sp;
518049656eefSMichael Tuexen 	uint32_t mid;
518149656eefSMichael Tuexen 	uint16_t sid;
51820c0982b8SRandall Stewart 	uint8_t foundeom = 0;
5183f8829a4aSRandall Stewart 	int ret_sz = 0;
5184f8829a4aSRandall Stewart 	int notdone;
51850c0982b8SRandall Stewart 	int do_wakeup_routine = 0;
5186f8829a4aSRandall Stewart 
518749656eefSMichael Tuexen 	sid = tp1->rec.data.sid;
518849656eefSMichael Tuexen 	mid = tp1->rec.data.mid;
5189f0396ad1SMichael Tuexen 	if (sent || !(tp1->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG)) {
5190f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_sent[0]++;
5191f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
519249656eefSMichael Tuexen 		stcb->asoc.strmout[sid].abandoned_sent[0]++;
5193f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
5194ad15e154SMichael Tuexen 		stcb->asoc.strmout[sid].abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
5195f0396ad1SMichael Tuexen #endif
5196f0396ad1SMichael Tuexen 	} else {
5197f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_unsent[0]++;
5198f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
519949656eefSMichael Tuexen 		stcb->asoc.strmout[sid].abandoned_unsent[0]++;
5200f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
5201ad15e154SMichael Tuexen 		stcb->asoc.strmout[sid].abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
5202f0396ad1SMichael Tuexen #endif
5203f0396ad1SMichael Tuexen 	}
5204f8829a4aSRandall Stewart 	do {
5205f8829a4aSRandall Stewart 		ret_sz += tp1->book_size;
52060c0982b8SRandall Stewart 		if (tp1->data != NULL) {
52078933fa13SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5208830d754dSRandall Stewart 				sctp_flight_size_decrease(tp1);
5209830d754dSRandall Stewart 				sctp_total_flight_decrease(stcb, tp1);
52108933fa13SRandall Stewart 			}
52118933fa13SRandall Stewart 			sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
52120c0982b8SRandall Stewart 			stcb->asoc.peers_rwnd += tp1->send_size;
52130c0982b8SRandall Stewart 			stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
52141edc9dbaSMichael Tuexen 			if (sent) {
52151edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
52161edc9dbaSMichael Tuexen 			} else {
52171edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
52181edc9dbaSMichael Tuexen 			}
52192f99457bSMichael Tuexen 			if (tp1->data) {
5220f8829a4aSRandall Stewart 				sctp_m_freem(tp1->data);
5221f8829a4aSRandall Stewart 				tp1->data = NULL;
52222f99457bSMichael Tuexen 			}
52230c0982b8SRandall Stewart 			do_wakeup_routine = 1;
5224f8829a4aSRandall Stewart 			if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
5225f8829a4aSRandall Stewart 				stcb->asoc.sent_queue_cnt_removeable--;
5226f8829a4aSRandall Stewart 			}
5227f8829a4aSRandall Stewart 		}
52288933fa13SRandall Stewart 		tp1->sent = SCTP_FORWARD_TSN_SKIP;
5229f8829a4aSRandall Stewart 		if ((tp1->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) ==
5230f8829a4aSRandall Stewart 		    SCTP_DATA_NOT_FRAG) {
5231f8829a4aSRandall Stewart 			/* not frag'ed we ae done   */
5232f8829a4aSRandall Stewart 			notdone = 0;
5233f8829a4aSRandall Stewart 			foundeom = 1;
5234f8829a4aSRandall Stewart 		} else if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
5235f8829a4aSRandall Stewart 			/* end of frag, we are done */
5236f8829a4aSRandall Stewart 			notdone = 0;
5237f8829a4aSRandall Stewart 			foundeom = 1;
5238f8829a4aSRandall Stewart 		} else {
5239f8829a4aSRandall Stewart 			/*
5240f8829a4aSRandall Stewart 			 * Its a begin or middle piece, we must mark all of
5241f8829a4aSRandall Stewart 			 * it
5242f8829a4aSRandall Stewart 			 */
5243f8829a4aSRandall Stewart 			notdone = 1;
5244f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
5245f8829a4aSRandall Stewart 		}
5246f8829a4aSRandall Stewart 	} while (tp1 && notdone);
52470c0982b8SRandall Stewart 	if (foundeom == 0) {
5248f8829a4aSRandall Stewart 		/*
5249f8829a4aSRandall Stewart 		 * The multi-part message was scattered across the send and
5250f8829a4aSRandall Stewart 		 * sent queue.
5251f8829a4aSRandall Stewart 		 */
52524a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(tp1, &stcb->asoc.send_queue, sctp_next, tp2) {
525349656eefSMichael Tuexen 			if ((tp1->rec.data.sid != sid) ||
525449656eefSMichael Tuexen 			    (!SCTP_MID_EQ(stcb->asoc.idata_supported, tp1->rec.data.mid, mid))) {
52554a9ef3f8SMichael Tuexen 				break;
52564a9ef3f8SMichael Tuexen 			}
52570c0982b8SRandall Stewart 			/*
52580c0982b8SRandall Stewart 			 * save to chk in case we have some on stream out
52590c0982b8SRandall Stewart 			 * queue. If so and we have an un-transmitted one we
52600c0982b8SRandall Stewart 			 * don't have to fudge the TSN.
52610c0982b8SRandall Stewart 			 */
52620c0982b8SRandall Stewart 			chk = tp1;
52630c0982b8SRandall Stewart 			ret_sz += tp1->book_size;
52640c0982b8SRandall Stewart 			sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
52651edc9dbaSMichael Tuexen 			if (sent) {
52661edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
52671edc9dbaSMichael Tuexen 			} else {
52681edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
52691edc9dbaSMichael Tuexen 			}
52702f99457bSMichael Tuexen 			if (tp1->data) {
52710c0982b8SRandall Stewart 				sctp_m_freem(tp1->data);
52722f99457bSMichael Tuexen 				tp1->data = NULL;
52732f99457bSMichael Tuexen 			}
52748933fa13SRandall Stewart 			/* No flight involved here book the size to 0 */
52758933fa13SRandall Stewart 			tp1->book_size = 0;
52760c0982b8SRandall Stewart 			if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
52770c0982b8SRandall Stewart 				foundeom = 1;
5278f8829a4aSRandall Stewart 			}
52790c0982b8SRandall Stewart 			do_wakeup_routine = 1;
52800c0982b8SRandall Stewart 			tp1->sent = SCTP_FORWARD_TSN_SKIP;
52810c0982b8SRandall Stewart 			TAILQ_REMOVE(&stcb->asoc.send_queue, tp1, sctp_next);
5282b7b84c0eSMichael Tuexen 			/*
5283b7b84c0eSMichael Tuexen 			 * on to the sent queue so we can wait for it to be
5284b7b84c0eSMichael Tuexen 			 * passed by.
5285b7b84c0eSMichael Tuexen 			 */
52860c0982b8SRandall Stewart 			TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, tp1,
52870c0982b8SRandall Stewart 			    sctp_next);
52880c0982b8SRandall Stewart 			stcb->asoc.send_queue_cnt--;
52890c0982b8SRandall Stewart 			stcb->asoc.sent_queue_cnt++;
52900c0982b8SRandall Stewart 		}
52910c0982b8SRandall Stewart 	}
52920c0982b8SRandall Stewart 	if (foundeom == 0) {
52930c0982b8SRandall Stewart 		/*
52940c0982b8SRandall Stewart 		 * Still no eom found. That means there is stuff left on the
52950c0982b8SRandall Stewart 		 * stream out queue.. yuck.
52960c0982b8SRandall Stewart 		 */
52970c0982b8SRandall Stewart 		SCTP_TCB_SEND_LOCK(stcb);
529849656eefSMichael Tuexen 		strq = &stcb->asoc.strmout[sid];
5299f3b05218SMichael Tuexen 		sp = TAILQ_FIRST(&strq->outqueue);
5300f3b05218SMichael Tuexen 		if (sp != NULL) {
53010c0982b8SRandall Stewart 			sp->discard_rest = 1;
53020c0982b8SRandall Stewart 			/*
5303f3b05218SMichael Tuexen 			 * We may need to put a chunk on the queue that
5304f3b05218SMichael Tuexen 			 * holds the TSN that would have been sent with the
5305f3b05218SMichael Tuexen 			 * LAST bit.
53060c0982b8SRandall Stewart 			 */
53070c0982b8SRandall Stewart 			if (chk == NULL) {
53080c0982b8SRandall Stewart 				/* Yep, we have to */
53090c0982b8SRandall Stewart 				sctp_alloc_a_chunk(stcb, chk);
53100c0982b8SRandall Stewart 				if (chk == NULL) {
53110c0982b8SRandall Stewart 					/*
5312f3b05218SMichael Tuexen 					 * we are hosed. All we can do is
5313f3b05218SMichael Tuexen 					 * nothing.. which will cause an
5314f3b05218SMichael Tuexen 					 * abort if the peer is paying
53150c0982b8SRandall Stewart 					 * attention.
53160c0982b8SRandall Stewart 					 */
53170c0982b8SRandall Stewart 					goto oh_well;
53180c0982b8SRandall Stewart 				}
53190c0982b8SRandall Stewart 				memset(chk, 0, sizeof(*chk));
532063d5b568SMichael Tuexen 				chk->rec.data.rcv_flags = 0;
53210c0982b8SRandall Stewart 				chk->sent = SCTP_FORWARD_TSN_SKIP;
53220c0982b8SRandall Stewart 				chk->asoc = &stcb->asoc;
532363d5b568SMichael Tuexen 				if (stcb->asoc.idata_supported == 0) {
532463d5b568SMichael Tuexen 					if (sp->sinfo_flags & SCTP_UNORDERED) {
532549656eefSMichael Tuexen 						chk->rec.data.mid = 0;
532663d5b568SMichael Tuexen 					} else {
532749656eefSMichael Tuexen 						chk->rec.data.mid = strq->next_mid_ordered;
532863d5b568SMichael Tuexen 					}
532963d5b568SMichael Tuexen 				} else {
533063d5b568SMichael Tuexen 					if (sp->sinfo_flags & SCTP_UNORDERED) {
533149656eefSMichael Tuexen 						chk->rec.data.mid = strq->next_mid_unordered;
533263d5b568SMichael Tuexen 					} else {
533349656eefSMichael Tuexen 						chk->rec.data.mid = strq->next_mid_ordered;
533463d5b568SMichael Tuexen 					}
533563d5b568SMichael Tuexen 				}
533649656eefSMichael Tuexen 				chk->rec.data.sid = sp->sid;
533749656eefSMichael Tuexen 				chk->rec.data.ppid = sp->ppid;
53380c0982b8SRandall Stewart 				chk->rec.data.context = sp->context;
53390c0982b8SRandall Stewart 				chk->flags = sp->act_flags;
53407fd5b436SMichael Tuexen 				chk->whoTo = NULL;
534149656eefSMichael Tuexen 				chk->rec.data.tsn = atomic_fetchadd_int(&stcb->asoc.sending_seq, 1);
53427fd5b436SMichael Tuexen 				strq->chunks_on_queues++;
53430c0982b8SRandall Stewart 				TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, chk, sctp_next);
53440c0982b8SRandall Stewart 				stcb->asoc.sent_queue_cnt++;
53458933fa13SRandall Stewart 				stcb->asoc.pr_sctp_cnt++;
53460c0982b8SRandall Stewart 			}
534763d5b568SMichael Tuexen 			chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
5348d1ea5fa9SMichael Tuexen 			if (sp->sinfo_flags & SCTP_UNORDERED) {
5349d1ea5fa9SMichael Tuexen 				chk->rec.data.rcv_flags |= SCTP_DATA_UNORDERED;
5350d1ea5fa9SMichael Tuexen 			}
535163d5b568SMichael Tuexen 			if (stcb->asoc.idata_supported == 0) {
535263d5b568SMichael Tuexen 				if ((sp->sinfo_flags & SCTP_UNORDERED) == 0) {
535363d5b568SMichael Tuexen 					strq->next_mid_ordered++;
535463d5b568SMichael Tuexen 				}
535563d5b568SMichael Tuexen 			} else {
535663d5b568SMichael Tuexen 				if (sp->sinfo_flags & SCTP_UNORDERED) {
535763d5b568SMichael Tuexen 					strq->next_mid_unordered++;
535863d5b568SMichael Tuexen 				} else {
535963d5b568SMichael Tuexen 					strq->next_mid_ordered++;
536063d5b568SMichael Tuexen 				}
536163d5b568SMichael Tuexen 			}
53620c0982b8SRandall Stewart 	oh_well:
53630c0982b8SRandall Stewart 			if (sp->data) {
53640c0982b8SRandall Stewart 				/*
5365f3b05218SMichael Tuexen 				 * Pull any data to free up the SB and allow
5366f3b05218SMichael Tuexen 				 * sender to "add more" while we will throw
5367f3b05218SMichael Tuexen 				 * away :-)
53680c0982b8SRandall Stewart 				 */
5369f3b05218SMichael Tuexen 				sctp_free_spbufspace(stcb, &stcb->asoc, sp);
53700c0982b8SRandall Stewart 				ret_sz += sp->length;
53710c0982b8SRandall Stewart 				do_wakeup_routine = 1;
53720c0982b8SRandall Stewart 				sp->some_taken = 1;
53730c0982b8SRandall Stewart 				sctp_m_freem(sp->data);
53740c0982b8SRandall Stewart 				sp->data = NULL;
53750c0982b8SRandall Stewart 				sp->tail_mbuf = NULL;
5376d07b2ac6SMichael Tuexen 				sp->length = 0;
53770c0982b8SRandall Stewart 			}
53780c0982b8SRandall Stewart 		}
53790c0982b8SRandall Stewart 		SCTP_TCB_SEND_UNLOCK(stcb);
53800c0982b8SRandall Stewart 	}
53810c0982b8SRandall Stewart 	if (do_wakeup_routine) {
53820c0982b8SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
53838933fa13SRandall Stewart 		struct socket *so;
53848933fa13SRandall Stewart 
53850c0982b8SRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
53860c0982b8SRandall Stewart 		if (!so_locked) {
53870c0982b8SRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, 1);
53880c0982b8SRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
53890c0982b8SRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
53900c0982b8SRandall Stewart 			SCTP_TCB_LOCK(stcb);
53910c0982b8SRandall Stewart 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
53920c0982b8SRandall Stewart 			if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
53930c0982b8SRandall Stewart 				/* assoc was freed while we were unlocked */
53940c0982b8SRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
53950c0982b8SRandall Stewart 				return (ret_sz);
53960c0982b8SRandall Stewart 			}
53970c0982b8SRandall Stewart 		}
53980c0982b8SRandall Stewart #endif
53990c0982b8SRandall Stewart 		sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
54000c0982b8SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
54010c0982b8SRandall Stewart 		if (!so_locked) {
54020c0982b8SRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
54030c0982b8SRandall Stewart 		}
54040c0982b8SRandall Stewart #endif
5405f8829a4aSRandall Stewart 	}
5406f8829a4aSRandall Stewart 	return (ret_sz);
5407f8829a4aSRandall Stewart }
5408f8829a4aSRandall Stewart 
5409f8829a4aSRandall Stewart /*
5410f8829a4aSRandall Stewart  * checks to see if the given address, sa, is one that is currently known by
5411f8829a4aSRandall Stewart  * the kernel note: can't distinguish the same address on multiple interfaces
5412f8829a4aSRandall Stewart  * and doesn't handle multiple addresses with different zone/scope id's note:
5413f8829a4aSRandall Stewart  * ifa_ifwithaddr() compares the entire sockaddr struct
5414f8829a4aSRandall Stewart  */
541542551e99SRandall Stewart struct sctp_ifa *
541680fefe0aSRandall Stewart sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr,
541780fefe0aSRandall Stewart     int holds_lock)
5418f8829a4aSRandall Stewart {
541942551e99SRandall Stewart 	struct sctp_laddr *laddr;
5420f8829a4aSRandall Stewart 
5421ad81507eSRandall Stewart 	if (holds_lock == 0) {
542242551e99SRandall Stewart 		SCTP_INP_RLOCK(inp);
5423ad81507eSRandall Stewart 	}
54240053ed28SMichael Tuexen 
542542551e99SRandall Stewart 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
542642551e99SRandall Stewart 		if (laddr->ifa == NULL)
5427f8829a4aSRandall Stewart 			continue;
542842551e99SRandall Stewart 		if (addr->sa_family != laddr->ifa->address.sa.sa_family)
542942551e99SRandall Stewart 			continue;
5430e6194c2eSMichael Tuexen #ifdef INET
543142551e99SRandall Stewart 		if (addr->sa_family == AF_INET) {
543242551e99SRandall Stewart 			if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
543342551e99SRandall Stewart 			    laddr->ifa->address.sin.sin_addr.s_addr) {
543442551e99SRandall Stewart 				/* found him. */
5435ad81507eSRandall Stewart 				if (holds_lock == 0) {
543642551e99SRandall Stewart 					SCTP_INP_RUNLOCK(inp);
5437ad81507eSRandall Stewart 				}
543842551e99SRandall Stewart 				return (laddr->ifa);
543942551e99SRandall Stewart 				break;
544042551e99SRandall Stewart 			}
54415e2c2d87SRandall Stewart 		}
5442e6194c2eSMichael Tuexen #endif
54435e2c2d87SRandall Stewart #ifdef INET6
54445e2c2d87SRandall Stewart 		if (addr->sa_family == AF_INET6) {
5445c54a18d2SRandall Stewart 			if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5446c54a18d2SRandall Stewart 			    &laddr->ifa->address.sin6)) {
544742551e99SRandall Stewart 				/* found him. */
5448ad81507eSRandall Stewart 				if (holds_lock == 0) {
544942551e99SRandall Stewart 					SCTP_INP_RUNLOCK(inp);
5450ad81507eSRandall Stewart 				}
545142551e99SRandall Stewart 				return (laddr->ifa);
545242551e99SRandall Stewart 				break;
545342551e99SRandall Stewart 			}
545442551e99SRandall Stewart 		}
54555e2c2d87SRandall Stewart #endif
545642551e99SRandall Stewart 	}
5457ad81507eSRandall Stewart 	if (holds_lock == 0) {
545842551e99SRandall Stewart 		SCTP_INP_RUNLOCK(inp);
5459ad81507eSRandall Stewart 	}
546042551e99SRandall Stewart 	return (NULL);
546142551e99SRandall Stewart }
5462f8829a4aSRandall Stewart 
54636a27c376SRandall Stewart uint32_t
5464b0471b4bSMichael Tuexen sctp_get_ifa_hash_val(struct sockaddr *addr)
5465b0471b4bSMichael Tuexen {
5466ea5eba11SMichael Tuexen 	switch (addr->sa_family) {
5467ea5eba11SMichael Tuexen #ifdef INET
5468ea5eba11SMichael Tuexen 	case AF_INET:
5469ea5eba11SMichael Tuexen 		{
54706a27c376SRandall Stewart 			struct sockaddr_in *sin;
54716a27c376SRandall Stewart 
54726a27c376SRandall Stewart 			sin = (struct sockaddr_in *)addr;
54736a27c376SRandall Stewart 			return (sin->sin_addr.s_addr ^ (sin->sin_addr.s_addr >> 16));
5474ea5eba11SMichael Tuexen 		}
5475ea5eba11SMichael Tuexen #endif
5476ea5eba11SMichael Tuexen #ifdef INET6
54772c2e3218SMichael Tuexen 	case AF_INET6:
5478ea5eba11SMichael Tuexen 		{
54796a27c376SRandall Stewart 			struct sockaddr_in6 *sin6;
54806a27c376SRandall Stewart 			uint32_t hash_of_addr;
54816a27c376SRandall Stewart 
54826a27c376SRandall Stewart 			sin6 = (struct sockaddr_in6 *)addr;
54836a27c376SRandall Stewart 			hash_of_addr = (sin6->sin6_addr.s6_addr32[0] +
54846a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[1] +
54856a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[2] +
54866a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[3]);
54876a27c376SRandall Stewart 			hash_of_addr = (hash_of_addr ^ (hash_of_addr >> 16));
54886a27c376SRandall Stewart 			return (hash_of_addr);
54896a27c376SRandall Stewart 		}
5490ea5eba11SMichael Tuexen #endif
5491ea5eba11SMichael Tuexen 	default:
5492ea5eba11SMichael Tuexen 		break;
5493ea5eba11SMichael Tuexen 	}
54946a27c376SRandall Stewart 	return (0);
54956a27c376SRandall Stewart }
54966a27c376SRandall Stewart 
549742551e99SRandall Stewart struct sctp_ifa *
549842551e99SRandall Stewart sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock)
549942551e99SRandall Stewart {
550042551e99SRandall Stewart 	struct sctp_ifa *sctp_ifap;
550142551e99SRandall Stewart 	struct sctp_vrf *vrf;
55026a27c376SRandall Stewart 	struct sctp_ifalist *hash_head;
55036a27c376SRandall Stewart 	uint32_t hash_of_addr;
550442551e99SRandall Stewart 
550542551e99SRandall Stewart 	if (holds_lock == 0)
5506c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RLOCK();
550742551e99SRandall Stewart 
5508bff64a4dSRandall Stewart 	vrf = sctp_find_vrf(vrf_id);
5509bff64a4dSRandall Stewart 	if (vrf == NULL) {
5510bff64a4dSRandall Stewart 		if (holds_lock == 0)
5511c99efcf6SRandall Stewart 			SCTP_IPI_ADDR_RUNLOCK();
5512bff64a4dSRandall Stewart 		return (NULL);
5513bff64a4dSRandall Stewart 	}
55140053ed28SMichael Tuexen 
5515bff64a4dSRandall Stewart 	hash_of_addr = sctp_get_ifa_hash_val(addr);
5516bff64a4dSRandall Stewart 
551717205eccSRandall Stewart 	hash_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
5518bff64a4dSRandall Stewart 	if (hash_head == NULL) {
5519ad81507eSRandall Stewart 		SCTP_PRINTF("hash_of_addr:%x mask:%x table:%x - ",
5520c99efcf6SRandall Stewart 		    hash_of_addr, (uint32_t)vrf->vrf_addr_hashmark,
5521c99efcf6SRandall Stewart 		    (uint32_t)(hash_of_addr & vrf->vrf_addr_hashmark));
5522bff64a4dSRandall Stewart 		sctp_print_address(addr);
5523ad81507eSRandall Stewart 		SCTP_PRINTF("No such bucket for address\n");
5524bff64a4dSRandall Stewart 		if (holds_lock == 0)
5525c99efcf6SRandall Stewart 			SCTP_IPI_ADDR_RUNLOCK();
5526bff64a4dSRandall Stewart 
5527bff64a4dSRandall Stewart 		return (NULL);
5528bff64a4dSRandall Stewart 	}
55296a27c376SRandall Stewart 	LIST_FOREACH(sctp_ifap, hash_head, next_bucket) {
55306a27c376SRandall Stewart 		if (addr->sa_family != sctp_ifap->address.sa.sa_family)
55316a27c376SRandall Stewart 			continue;
5532e6194c2eSMichael Tuexen #ifdef INET
55336a27c376SRandall Stewart 		if (addr->sa_family == AF_INET) {
55346a27c376SRandall Stewart 			if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
55356a27c376SRandall Stewart 			    sctp_ifap->address.sin.sin_addr.s_addr) {
55366a27c376SRandall Stewart 				/* found him. */
553742551e99SRandall Stewart 				if (holds_lock == 0)
5538c99efcf6SRandall Stewart 					SCTP_IPI_ADDR_RUNLOCK();
553942551e99SRandall Stewart 				return (sctp_ifap);
55406a27c376SRandall Stewart 				break;
55416a27c376SRandall Stewart 			}
55425e2c2d87SRandall Stewart 		}
5543e6194c2eSMichael Tuexen #endif
55445e2c2d87SRandall Stewart #ifdef INET6
55455e2c2d87SRandall Stewart 		if (addr->sa_family == AF_INET6) {
5546c54a18d2SRandall Stewart 			if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5547c54a18d2SRandall Stewart 			    &sctp_ifap->address.sin6)) {
55486a27c376SRandall Stewart 				/* found him. */
55496a27c376SRandall Stewart 				if (holds_lock == 0)
5550c99efcf6SRandall Stewart 					SCTP_IPI_ADDR_RUNLOCK();
55516a27c376SRandall Stewart 				return (sctp_ifap);
55526a27c376SRandall Stewart 				break;
55536a27c376SRandall Stewart 			}
555442551e99SRandall Stewart 		}
55555e2c2d87SRandall Stewart #endif
555642551e99SRandall Stewart 	}
555742551e99SRandall Stewart 	if (holds_lock == 0)
5558c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RUNLOCK();
5559f8829a4aSRandall Stewart 	return (NULL);
5560f8829a4aSRandall Stewart }
5561f8829a4aSRandall Stewart 
5562f8829a4aSRandall Stewart static void
55634c9179adSRandall Stewart sctp_user_rcvd(struct sctp_tcb *stcb, uint32_t *freed_so_far, int hold_rlock,
5564f8829a4aSRandall Stewart     uint32_t rwnd_req)
5565f8829a4aSRandall Stewart {
5566f8829a4aSRandall Stewart 	/* User pulled some data, do we need a rwnd update? */
5567868b51f2SMichael Tuexen 	struct epoch_tracker et;
5568f8829a4aSRandall Stewart 	int r_unlocked = 0;
5569f8829a4aSRandall Stewart 	uint32_t dif, rwnd;
5570f8829a4aSRandall Stewart 	struct socket *so = NULL;
5571f8829a4aSRandall Stewart 
5572f8829a4aSRandall Stewart 	if (stcb == NULL)
5573f8829a4aSRandall Stewart 		return;
5574f8829a4aSRandall Stewart 
557550cec919SRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, 1);
5576f8829a4aSRandall Stewart 
5577839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
557861a21880SMichael Tuexen 	    (stcb->asoc.state & (SCTP_STATE_ABOUT_TO_BE_FREED | SCTP_STATE_SHUTDOWN_RECEIVED))) {
5579f8829a4aSRandall Stewart 		/* Pre-check If we are freeing no update */
5580f8829a4aSRandall Stewart 		goto no_lock;
5581f8829a4aSRandall Stewart 	}
5582f8829a4aSRandall Stewart 	SCTP_INP_INCR_REF(stcb->sctp_ep);
5583f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5584f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5585f8829a4aSRandall Stewart 		goto out;
5586f8829a4aSRandall Stewart 	}
5587f8829a4aSRandall Stewart 	so = stcb->sctp_socket;
5588f8829a4aSRandall Stewart 	if (so == NULL) {
5589f8829a4aSRandall Stewart 		goto out;
5590f8829a4aSRandall Stewart 	}
5591f8829a4aSRandall Stewart 	atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far);
5592f8829a4aSRandall Stewart 	/* Have you have freed enough to look */
5593f8829a4aSRandall Stewart 	*freed_so_far = 0;
5594f8829a4aSRandall Stewart 	/* Yep, its worth a look and the lock overhead */
5595f8829a4aSRandall Stewart 
5596f8829a4aSRandall Stewart 	/* Figure out what the rwnd would be */
5597f8829a4aSRandall Stewart 	rwnd = sctp_calc_rwnd(stcb, &stcb->asoc);
5598f8829a4aSRandall Stewart 	if (rwnd >= stcb->asoc.my_last_reported_rwnd) {
5599f8829a4aSRandall Stewart 		dif = rwnd - stcb->asoc.my_last_reported_rwnd;
5600f8829a4aSRandall Stewart 	} else {
5601f8829a4aSRandall Stewart 		dif = 0;
5602f8829a4aSRandall Stewart 	}
5603f8829a4aSRandall Stewart 	if (dif >= rwnd_req) {
5604f8829a4aSRandall Stewart 		if (hold_rlock) {
5605f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5606f8829a4aSRandall Stewart 			r_unlocked = 1;
5607f8829a4aSRandall Stewart 		}
5608f8829a4aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5609f8829a4aSRandall Stewart 			/*
5610f8829a4aSRandall Stewart 			 * One last check before we allow the guy possibly
5611f8829a4aSRandall Stewart 			 * to get in. There is a race, where the guy has not
5612f8829a4aSRandall Stewart 			 * reached the gate. In that case
5613f8829a4aSRandall Stewart 			 */
5614f8829a4aSRandall Stewart 			goto out;
5615f8829a4aSRandall Stewart 		}
5616f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
5617f8829a4aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5618f8829a4aSRandall Stewart 			/* No reports here */
5619f8829a4aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
5620f8829a4aSRandall Stewart 			goto out;
5621f8829a4aSRandall Stewart 		}
5622f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_wu_sacks_sent);
5623868b51f2SMichael Tuexen 		NET_EPOCH_ENTER(et);
5624689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_LOCKED);
5625830d754dSRandall Stewart 
5626f8829a4aSRandall Stewart 		sctp_chunk_output(stcb->sctp_ep, stcb,
5627ceaad40aSRandall Stewart 		    SCTP_OUTPUT_FROM_USR_RCVD, SCTP_SO_LOCKED);
5628f8829a4aSRandall Stewart 		/* make sure no timer is running */
5629868b51f2SMichael Tuexen 		NET_EPOCH_EXIT(et);
5630ba785902SMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
5631ba785902SMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_6);
5632f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
5633f8829a4aSRandall Stewart 	} else {
5634f8829a4aSRandall Stewart 		/* Update how much we have pending */
5635f8829a4aSRandall Stewart 		stcb->freed_by_sorcv_sincelast = dif;
5636f8829a4aSRandall Stewart 	}
5637f8829a4aSRandall Stewart out:
5638f8829a4aSRandall Stewart 	if (so && r_unlocked && hold_rlock) {
5639f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
5640f8829a4aSRandall Stewart 	}
56410053ed28SMichael Tuexen 
5642f8829a4aSRandall Stewart 	SCTP_INP_DECR_REF(stcb->sctp_ep);
5643f8829a4aSRandall Stewart no_lock:
564450cec919SRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, -1);
5645f8829a4aSRandall Stewart 	return;
5646f8829a4aSRandall Stewart }
5647f8829a4aSRandall Stewart 
5648f8829a4aSRandall Stewart int
5649f8829a4aSRandall Stewart sctp_sorecvmsg(struct socket *so,
5650f8829a4aSRandall Stewart     struct uio *uio,
5651f8829a4aSRandall Stewart     struct mbuf **mp,
5652f8829a4aSRandall Stewart     struct sockaddr *from,
5653f8829a4aSRandall Stewart     int fromlen,
5654f8829a4aSRandall Stewart     int *msg_flags,
5655f8829a4aSRandall Stewart     struct sctp_sndrcvinfo *sinfo,
5656f8829a4aSRandall Stewart     int filling_sinfo)
5657f8829a4aSRandall Stewart {
5658f8829a4aSRandall Stewart 	/*
5659f8829a4aSRandall Stewart 	 * MSG flags we will look at MSG_DONTWAIT - non-blocking IO.
5660f8829a4aSRandall Stewart 	 * MSG_PEEK - Look don't touch :-D (only valid with OUT mbuf copy
5661f8829a4aSRandall Stewart 	 * mp=NULL thus uio is the copy method to userland) MSG_WAITALL - ??
5662f8829a4aSRandall Stewart 	 * On the way out we may send out any combination of:
5663f8829a4aSRandall Stewart 	 * MSG_NOTIFICATION MSG_EOR
5664f8829a4aSRandall Stewart 	 *
5665f8829a4aSRandall Stewart 	 */
5666f8829a4aSRandall Stewart 	struct sctp_inpcb *inp = NULL;
566758e6eeefSMichael Tuexen 	ssize_t my_len = 0;
566858e6eeefSMichael Tuexen 	ssize_t cp_len = 0;
56690d3cf13dSMichael Tuexen 	int error = 0;
5670f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL;
567194b0d969SMichael Tuexen 	struct mbuf *m = NULL;
5672f8829a4aSRandall Stewart 	struct sctp_tcb *stcb = NULL;
5673f8829a4aSRandall Stewart 	int wakeup_read_socket = 0;
5674f8829a4aSRandall Stewart 	int freecnt_applied = 0;
5675f8829a4aSRandall Stewart 	int out_flags = 0, in_flags = 0;
5676f8829a4aSRandall Stewart 	int block_allowed = 1;
56774c9179adSRandall Stewart 	uint32_t freed_so_far = 0;
567858e6eeefSMichael Tuexen 	ssize_t copied_so_far = 0;
567993164cf9SRandall Stewart 	int in_eeor_mode = 0;
5680f8829a4aSRandall Stewart 	int no_rcv_needed = 0;
5681f8829a4aSRandall Stewart 	uint32_t rwnd_req = 0;
5682f8829a4aSRandall Stewart 	int hold_sblock = 0;
5683f8829a4aSRandall Stewart 	int hold_rlock = 0;
56849a8e3088SMichael Tuexen 	ssize_t slen = 0;
56854c9179adSRandall Stewart 	uint32_t held_length = 0;
56867abab911SRobert Watson 	int sockbuf_lock = 0;
5687f8829a4aSRandall Stewart 
568817205eccSRandall Stewart 	if (uio == NULL) {
5689c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
569017205eccSRandall Stewart 		return (EINVAL);
569117205eccSRandall Stewart 	}
56920053ed28SMichael Tuexen 
5693f8829a4aSRandall Stewart 	if (msg_flags) {
5694f8829a4aSRandall Stewart 		in_flags = *msg_flags;
5695c105859eSRandall Stewart 		if (in_flags & MSG_PEEK)
5696c105859eSRandall Stewart 			SCTP_STAT_INCR(sctps_read_peeks);
5697f8829a4aSRandall Stewart 	} else {
5698f8829a4aSRandall Stewart 		in_flags = 0;
5699f8829a4aSRandall Stewart 	}
5700f8829a4aSRandall Stewart 	slen = uio->uio_resid;
570117205eccSRandall Stewart 
5702f8829a4aSRandall Stewart 	/* Pull in and set up our int flags */
5703f8829a4aSRandall Stewart 	if (in_flags & MSG_OOB) {
5704f8829a4aSRandall Stewart 		/* Out of band's NOT supported */
5705f8829a4aSRandall Stewart 		return (EOPNOTSUPP);
5706f8829a4aSRandall Stewart 	}
5707f8829a4aSRandall Stewart 	if ((in_flags & MSG_PEEK) && (mp != NULL)) {
5708c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5709f8829a4aSRandall Stewart 		return (EINVAL);
5710f8829a4aSRandall Stewart 	}
5711f8829a4aSRandall Stewart 	if ((in_flags & (MSG_DONTWAIT
5712f8829a4aSRandall Stewart 	    | MSG_NBIO
5713f8829a4aSRandall Stewart 	    )) ||
571442551e99SRandall Stewart 	    SCTP_SO_IS_NBIO(so)) {
5715f8829a4aSRandall Stewart 		block_allowed = 0;
5716f8829a4aSRandall Stewart 	}
5717f8829a4aSRandall Stewart 	/* setup the endpoint */
5718f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
5719f8829a4aSRandall Stewart 	if (inp == NULL) {
5720c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
5721f8829a4aSRandall Stewart 		return (EFAULT);
5722f8829a4aSRandall Stewart 	}
572362c1ff9cSRandall Stewart 	rwnd_req = (SCTP_SB_LIMIT_RCV(so) >> SCTP_RWND_HIWAT_SHIFT);
5724f8829a4aSRandall Stewart 	/* Must be at least a MTU's worth */
5725f8829a4aSRandall Stewart 	if (rwnd_req < SCTP_MIN_RWND)
5726f8829a4aSRandall Stewart 		rwnd_req = SCTP_MIN_RWND;
5727f8829a4aSRandall Stewart 	in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
5728b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5729f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SORECV_ENTER,
57309a8e3088SMichael Tuexen 		    rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, (uint32_t)uio->uio_resid);
573180fefe0aSRandall Stewart 	}
5732b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5733f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SORECV_ENTERPL,
57349a8e3088SMichael Tuexen 		    rwnd_req, block_allowed, so->so_rcv.sb_cc, (uint32_t)uio->uio_resid);
573580fefe0aSRandall Stewart 	}
57360053ed28SMichael Tuexen 
57370053ed28SMichael Tuexen 
5738265de5bbSRobert Watson 	error = sblock(&so->so_rcv, (block_allowed ? SBL_WAIT : 0));
5739f8829a4aSRandall Stewart 	if (error) {
5740f8829a4aSRandall Stewart 		goto release_unlocked;
5741f8829a4aSRandall Stewart 	}
57428e1e6e5fSMateusz Guzik 	sockbuf_lock = 1;
5743f8829a4aSRandall Stewart restart:
57447abab911SRobert Watson 
5745f8829a4aSRandall Stewart 
5746f8829a4aSRandall Stewart restart_nosblocks:
5747f8829a4aSRandall Stewart 	if (hold_sblock == 0) {
5748f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&so->so_rcv);
5749f8829a4aSRandall Stewart 		hold_sblock = 1;
5750f8829a4aSRandall Stewart 	}
5751f8829a4aSRandall Stewart 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5752f8829a4aSRandall Stewart 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5753f8829a4aSRandall Stewart 		goto out;
5754f8829a4aSRandall Stewart 	}
57554e88d37aSMichael Tuexen 	if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) && (so->so_rcv.sb_cc == 0)) {
5756f8829a4aSRandall Stewart 		if (so->so_error) {
5757f8829a4aSRandall Stewart 			error = so->so_error;
575844b7479bSRandall Stewart 			if ((in_flags & MSG_PEEK) == 0)
575944b7479bSRandall Stewart 				so->so_error = 0;
57609f22f500SRandall Stewart 			goto out;
5761f8829a4aSRandall Stewart 		} else {
57624e88d37aSMichael Tuexen 			if (so->so_rcv.sb_cc == 0) {
57637924093fSRandall Stewart 				/* indicate EOF */
57647924093fSRandall Stewart 				error = 0;
5765f8829a4aSRandall Stewart 				goto out;
5766f8829a4aSRandall Stewart 			}
57679f22f500SRandall Stewart 		}
57689f22f500SRandall Stewart 	}
57699de217ceSMichael Tuexen 	if (so->so_rcv.sb_cc <= held_length) {
57709de217ceSMichael Tuexen 		if (so->so_error) {
57719de217ceSMichael Tuexen 			error = so->so_error;
57729de217ceSMichael Tuexen 			if ((in_flags & MSG_PEEK) == 0) {
57739de217ceSMichael Tuexen 				so->so_error = 0;
57749de217ceSMichael Tuexen 			}
57759de217ceSMichael Tuexen 			goto out;
57769de217ceSMichael Tuexen 		}
57774e88d37aSMichael Tuexen 		if ((so->so_rcv.sb_cc == 0) &&
5778f8829a4aSRandall Stewart 		    ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5779f8829a4aSRandall Stewart 		    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
5780f8829a4aSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
5781f8829a4aSRandall Stewart 				/*
5782f8829a4aSRandall Stewart 				 * For active open side clear flags for
5783f8829a4aSRandall Stewart 				 * re-use passive open is blocked by
5784f8829a4aSRandall Stewart 				 * connect.
5785f8829a4aSRandall Stewart 				 */
5786f8829a4aSRandall Stewart 				if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
5787b7b84c0eSMichael Tuexen 					/*
5788b7b84c0eSMichael Tuexen 					 * You were aborted, passive side
5789b7b84c0eSMichael Tuexen 					 * always hits here
5790b7b84c0eSMichael Tuexen 					 */
5791c4739e2fSRandall Stewart 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
5792f8829a4aSRandall Stewart 					error = ECONNRESET;
5793f8829a4aSRandall Stewart 				}
5794f8829a4aSRandall Stewart 				so->so_state &= ~(SS_ISCONNECTING |
5795f8829a4aSRandall Stewart 				    SS_ISDISCONNECTING |
5796f8829a4aSRandall Stewart 				    SS_ISCONFIRMING |
5797f8829a4aSRandall Stewart 				    SS_ISCONNECTED);
5798f8829a4aSRandall Stewart 				if (error == 0) {
5799f8829a4aSRandall Stewart 					if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5800c4739e2fSRandall Stewart 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5801f8829a4aSRandall Stewart 						error = ENOTCONN;
5802f8829a4aSRandall Stewart 					}
5803f8829a4aSRandall Stewart 				}
5804f8829a4aSRandall Stewart 				goto out;
5805f8829a4aSRandall Stewart 			}
5806f8829a4aSRandall Stewart 		}
58079de217ceSMichael Tuexen 		if (block_allowed) {
5808f8829a4aSRandall Stewart 			error = sbwait(&so->so_rcv);
5809f8829a4aSRandall Stewart 			if (error) {
5810f8829a4aSRandall Stewart 				goto out;
5811f8829a4aSRandall Stewart 			}
5812f8829a4aSRandall Stewart 			held_length = 0;
5813f8829a4aSRandall Stewart 			goto restart_nosblocks;
581444b7479bSRandall Stewart 		} else {
5815c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EWOULDBLOCK);
5816f8829a4aSRandall Stewart 			error = EWOULDBLOCK;
5817f8829a4aSRandall Stewart 			goto out;
5818f8829a4aSRandall Stewart 		}
58199de217ceSMichael Tuexen 	}
5820d06c82f1SRandall Stewart 	if (hold_sblock == 1) {
5821d06c82f1SRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
5822d06c82f1SRandall Stewart 		hold_sblock = 0;
5823d06c82f1SRandall Stewart 	}
5824f8829a4aSRandall Stewart 	/* we possibly have data we can read */
58253c503c28SRandall Stewart 	/* sa_ignore FREED_MEMORY */
5826f8829a4aSRandall Stewart 	control = TAILQ_FIRST(&inp->read_queue);
5827f8829a4aSRandall Stewart 	if (control == NULL) {
5828f8829a4aSRandall Stewart 		/*
5829f8829a4aSRandall Stewart 		 * This could be happening since the appender did the
5830f8829a4aSRandall Stewart 		 * increment but as not yet did the tailq insert onto the
5831f8829a4aSRandall Stewart 		 * read_queue
5832f8829a4aSRandall Stewart 		 */
5833f8829a4aSRandall Stewart 		if (hold_rlock == 0) {
5834f8829a4aSRandall Stewart 			SCTP_INP_READ_LOCK(inp);
5835f8829a4aSRandall Stewart 		}
5836f8829a4aSRandall Stewart 		control = TAILQ_FIRST(&inp->read_queue);
58374e88d37aSMichael Tuexen 		if ((control == NULL) && (so->so_rcv.sb_cc != 0)) {
5838a5d547adSRandall Stewart #ifdef INVARIANTS
5839f8829a4aSRandall Stewart 			panic("Huh, its non zero and nothing on control?");
5840f8829a4aSRandall Stewart #endif
58414e88d37aSMichael Tuexen 			so->so_rcv.sb_cc = 0;
5842f8829a4aSRandall Stewart 		}
5843f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
5844f8829a4aSRandall Stewart 		hold_rlock = 0;
5845f8829a4aSRandall Stewart 		goto restart;
5846f8829a4aSRandall Stewart 	}
58470053ed28SMichael Tuexen 
5848f8829a4aSRandall Stewart 	if ((control->length == 0) &&
5849f8829a4aSRandall Stewart 	    (control->do_not_ref_stcb)) {
5850f8829a4aSRandall Stewart 		/*
5851f8829a4aSRandall Stewart 		 * Clean up code for freeing assoc that left behind a
5852f8829a4aSRandall Stewart 		 * pdapi.. maybe a peer in EEOR that just closed after
5853f8829a4aSRandall Stewart 		 * sending and never indicated a EOR.
5854f8829a4aSRandall Stewart 		 */
5855f8829a4aSRandall Stewart 		if (hold_rlock == 0) {
5856f8829a4aSRandall Stewart 			hold_rlock = 1;
5857f8829a4aSRandall Stewart 			SCTP_INP_READ_LOCK(inp);
5858f8829a4aSRandall Stewart 		}
5859f8829a4aSRandall Stewart 		control->held_length = 0;
5860f8829a4aSRandall Stewart 		if (control->data) {
5861f8829a4aSRandall Stewart 			/* Hmm there is data here .. fix */
58624c9179adSRandall Stewart 			struct mbuf *m_tmp;
5863f8829a4aSRandall Stewart 			int cnt = 0;
5864f8829a4aSRandall Stewart 
58654c9179adSRandall Stewart 			m_tmp = control->data;
58664c9179adSRandall Stewart 			while (m_tmp) {
58674c9179adSRandall Stewart 				cnt += SCTP_BUF_LEN(m_tmp);
58684c9179adSRandall Stewart 				if (SCTP_BUF_NEXT(m_tmp) == NULL) {
58694c9179adSRandall Stewart 					control->tail_mbuf = m_tmp;
5870f8829a4aSRandall Stewart 					control->end_added = 1;
5871f8829a4aSRandall Stewart 				}
58724c9179adSRandall Stewart 				m_tmp = SCTP_BUF_NEXT(m_tmp);
5873f8829a4aSRandall Stewart 			}
5874f8829a4aSRandall Stewart 			control->length = cnt;
5875f8829a4aSRandall Stewart 		} else {
5876f8829a4aSRandall Stewart 			/* remove it */
5877f8829a4aSRandall Stewart 			TAILQ_REMOVE(&inp->read_queue, control, next);
5878f8829a4aSRandall Stewart 			/* Add back any hiddend data */
5879f8829a4aSRandall Stewart 			sctp_free_remote_addr(control->whoFrom);
5880f8829a4aSRandall Stewart 			sctp_free_a_readq(stcb, control);
5881f8829a4aSRandall Stewart 		}
5882f8829a4aSRandall Stewart 		if (hold_rlock) {
5883f8829a4aSRandall Stewart 			hold_rlock = 0;
5884f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
5885f8829a4aSRandall Stewart 		}
5886f8829a4aSRandall Stewart 		goto restart;
5887f8829a4aSRandall Stewart 	}
5888810ec536SMichael Tuexen 	if ((control->length == 0) &&
5889810ec536SMichael Tuexen 	    (control->end_added == 1)) {
5890b7b84c0eSMichael Tuexen 		/*
5891b7b84c0eSMichael Tuexen 		 * Do we also need to check for (control->pdapi_aborted ==
5892b7b84c0eSMichael Tuexen 		 * 1)?
5893b7b84c0eSMichael Tuexen 		 */
5894810ec536SMichael Tuexen 		if (hold_rlock == 0) {
5895810ec536SMichael Tuexen 			hold_rlock = 1;
5896810ec536SMichael Tuexen 			SCTP_INP_READ_LOCK(inp);
5897810ec536SMichael Tuexen 		}
5898810ec536SMichael Tuexen 		TAILQ_REMOVE(&inp->read_queue, control, next);
5899810ec536SMichael Tuexen 		if (control->data) {
5900810ec536SMichael Tuexen #ifdef INVARIANTS
5901810ec536SMichael Tuexen 			panic("control->data not null but control->length == 0");
5902810ec536SMichael Tuexen #else
5903810ec536SMichael Tuexen 			SCTP_PRINTF("Strange, data left in the control buffer. Cleaning up.\n");
5904810ec536SMichael Tuexen 			sctp_m_freem(control->data);
5905810ec536SMichael Tuexen 			control->data = NULL;
5906810ec536SMichael Tuexen #endif
5907810ec536SMichael Tuexen 		}
5908810ec536SMichael Tuexen 		if (control->aux_data) {
5909810ec536SMichael Tuexen 			sctp_m_free(control->aux_data);
5910810ec536SMichael Tuexen 			control->aux_data = NULL;
5911810ec536SMichael Tuexen 		}
591298d5fd97SMichael Tuexen #ifdef INVARIANTS
591344249214SRandall Stewart 		if (control->on_strm_q) {
591444249214SRandall Stewart 			panic("About to free ctl:%p so:%p and its in %d",
591544249214SRandall Stewart 			    control, so, control->on_strm_q);
591644249214SRandall Stewart 		}
591798d5fd97SMichael Tuexen #endif
5918810ec536SMichael Tuexen 		sctp_free_remote_addr(control->whoFrom);
5919810ec536SMichael Tuexen 		sctp_free_a_readq(stcb, control);
5920810ec536SMichael Tuexen 		if (hold_rlock) {
5921810ec536SMichael Tuexen 			hold_rlock = 0;
5922810ec536SMichael Tuexen 			SCTP_INP_READ_UNLOCK(inp);
5923810ec536SMichael Tuexen 		}
5924810ec536SMichael Tuexen 		goto restart;
5925810ec536SMichael Tuexen 	}
5926f8829a4aSRandall Stewart 	if (control->length == 0) {
5927f8829a4aSRandall Stewart 		if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
5928f8829a4aSRandall Stewart 		    (filling_sinfo)) {
5929f8829a4aSRandall Stewart 			/* find a more suitable one then this */
5930f8829a4aSRandall Stewart 			ctl = TAILQ_NEXT(control, next);
5931f8829a4aSRandall Stewart 			while (ctl) {
59329a6142d8SRandall Stewart 				if ((ctl->stcb != control->stcb) && (ctl->length) &&
59339a6142d8SRandall Stewart 				    (ctl->some_taken ||
59346114cd96SRandall Stewart 				    (ctl->spec_flags & M_NOTIFICATION) ||
59359a6142d8SRandall Stewart 				    ((ctl->do_not_ref_stcb == 0) &&
59369a6142d8SRandall Stewart 				    (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))
59379a6142d8SRandall Stewart 				    ) {
59389a6142d8SRandall Stewart 					/*-
59399a6142d8SRandall Stewart 					 * If we have a different TCB next, and there is data
59409a6142d8SRandall Stewart 					 * present. If we have already taken some (pdapi), OR we can
59419a6142d8SRandall Stewart 					 * ref the tcb and no delivery as started on this stream, we
594217205eccSRandall Stewart 					 * take it. Note we allow a notification on a different
594317205eccSRandall Stewart 					 * assoc to be delivered..
59449a6142d8SRandall Stewart 					 */
59459a6142d8SRandall Stewart 					control = ctl;
59469a6142d8SRandall Stewart 					goto found_one;
59479a6142d8SRandall Stewart 				} else if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) &&
59489a6142d8SRandall Stewart 					    (ctl->length) &&
59499a6142d8SRandall Stewart 					    ((ctl->some_taken) ||
59509a6142d8SRandall Stewart 					    ((ctl->do_not_ref_stcb == 0) &&
595117205eccSRandall Stewart 					    ((ctl->spec_flags & M_NOTIFICATION) == 0) &&
5952b5c16493SMichael Tuexen 				    (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))) {
59539a6142d8SRandall Stewart 					/*-
59549a6142d8SRandall Stewart 					 * If we have the same tcb, and there is data present, and we
59559a6142d8SRandall Stewart 					 * have the strm interleave feature present. Then if we have
59569a6142d8SRandall Stewart 					 * taken some (pdapi) or we can refer to tht tcb AND we have
59579a6142d8SRandall Stewart 					 * not started a delivery for this stream, we can take it.
595817205eccSRandall Stewart 					 * Note we do NOT allow a notificaiton on the same assoc to
595917205eccSRandall Stewart 					 * be delivered.
59609a6142d8SRandall Stewart 					 */
5961f8829a4aSRandall Stewart 					control = ctl;
5962f8829a4aSRandall Stewart 					goto found_one;
5963f8829a4aSRandall Stewart 				}
5964f8829a4aSRandall Stewart 				ctl = TAILQ_NEXT(ctl, next);
5965f8829a4aSRandall Stewart 			}
5966f8829a4aSRandall Stewart 		}
5967f8829a4aSRandall Stewart 		/*
5968f8829a4aSRandall Stewart 		 * if we reach here, not suitable replacement is available
59694e88d37aSMichael Tuexen 		 * <or> fragment interleave is NOT on. So stuff the sb_cc
5970f8829a4aSRandall Stewart 		 * into the our held count, and its time to sleep again.
5971f8829a4aSRandall Stewart 		 */
59724e88d37aSMichael Tuexen 		held_length = so->so_rcv.sb_cc;
59734e88d37aSMichael Tuexen 		control->held_length = so->so_rcv.sb_cc;
5974f8829a4aSRandall Stewart 		goto restart;
5975f8829a4aSRandall Stewart 	}
5976f8829a4aSRandall Stewart 	/* Clear the held length since there is something to read */
5977f8829a4aSRandall Stewart 	control->held_length = 0;
5978f8829a4aSRandall Stewart found_one:
5979f8829a4aSRandall Stewart 	/*
5980f8829a4aSRandall Stewart 	 * If we reach here, control has a some data for us to read off.
5981f8829a4aSRandall Stewart 	 * Note that stcb COULD be NULL.
5982f8829a4aSRandall Stewart 	 */
59839c5ca6f2SMichael Tuexen 	if (hold_rlock == 0) {
59849c5ca6f2SMichael Tuexen 		hold_rlock = 1;
59859c5ca6f2SMichael Tuexen 		SCTP_INP_READ_LOCK(inp);
5986f8829a4aSRandall Stewart 	}
59879c5ca6f2SMichael Tuexen 	control->some_taken++;
5988f8829a4aSRandall Stewart 	stcb = control->stcb;
5989f8829a4aSRandall Stewart 	if (stcb) {
59900696e120SRandall Stewart 		if ((control->do_not_ref_stcb == 0) &&
59910696e120SRandall Stewart 		    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
599250cec919SRandall Stewart 			if (freecnt_applied == 0)
5993f8829a4aSRandall Stewart 				stcb = NULL;
5994f8829a4aSRandall Stewart 		} else if (control->do_not_ref_stcb == 0) {
5995f8829a4aSRandall Stewart 			/* you can't free it on me please */
5996f8829a4aSRandall Stewart 			/*
5997f8829a4aSRandall Stewart 			 * The lock on the socket buffer protects us so the
5998f8829a4aSRandall Stewart 			 * free code will stop. But since we used the
5999f8829a4aSRandall Stewart 			 * socketbuf lock and the sender uses the tcb_lock
6000f8829a4aSRandall Stewart 			 * to increment, we need to use the atomic add to
6001f8829a4aSRandall Stewart 			 * the refcnt
6002f8829a4aSRandall Stewart 			 */
6003d55b0b1bSRandall Stewart 			if (freecnt_applied) {
6004d55b0b1bSRandall Stewart #ifdef INVARIANTS
6005207304d4SRandall Stewart 				panic("refcnt already incremented");
6006d55b0b1bSRandall Stewart #else
6007cd3fd531SMichael Tuexen 				SCTP_PRINTF("refcnt already incremented?\n");
6008d55b0b1bSRandall Stewart #endif
6009d55b0b1bSRandall Stewart 			} else {
601050cec919SRandall Stewart 				atomic_add_int(&stcb->asoc.refcnt, 1);
6011f8829a4aSRandall Stewart 				freecnt_applied = 1;
6012d55b0b1bSRandall Stewart 			}
6013f8829a4aSRandall Stewart 			/*
6014f8829a4aSRandall Stewart 			 * Setup to remember how much we have not yet told
6015f8829a4aSRandall Stewart 			 * the peer our rwnd has opened up. Note we grab the
6016f8829a4aSRandall Stewart 			 * value from the tcb from last time. Note too that
60170696e120SRandall Stewart 			 * sack sending clears this when a sack is sent,
6018f8829a4aSRandall Stewart 			 * which is fine. Once we hit the rwnd_req, we then
6019f8829a4aSRandall Stewart 			 * will go to the sctp_user_rcvd() that will not
6020f8829a4aSRandall Stewart 			 * lock until it KNOWs it MUST send a WUP-SACK.
6021f8829a4aSRandall Stewart 			 */
602258e6eeefSMichael Tuexen 			freed_so_far = (uint32_t)stcb->freed_by_sorcv_sincelast;
6023f8829a4aSRandall Stewart 			stcb->freed_by_sorcv_sincelast = 0;
6024f8829a4aSRandall Stewart 		}
6025f8829a4aSRandall Stewart 	}
60266114cd96SRandall Stewart 	if (stcb &&
60276114cd96SRandall Stewart 	    ((control->spec_flags & M_NOTIFICATION) == 0) &&
60286114cd96SRandall Stewart 	    control->do_not_ref_stcb == 0) {
6029d06c82f1SRandall Stewart 		stcb->asoc.strmin[control->sinfo_stream].delivery_started = 1;
6030d06c82f1SRandall Stewart 	}
60310053ed28SMichael Tuexen 
6032f8829a4aSRandall Stewart 	/* First lets get off the sinfo and sockaddr info */
60335f05199cSMichael Tuexen 	if ((sinfo != NULL) && (filling_sinfo != 0)) {
60345f05199cSMichael Tuexen 		sinfo->sinfo_stream = control->sinfo_stream;
603549656eefSMichael Tuexen 		sinfo->sinfo_ssn = (uint16_t)control->mid;
60365f05199cSMichael Tuexen 		sinfo->sinfo_flags = control->sinfo_flags;
60375f05199cSMichael Tuexen 		sinfo->sinfo_ppid = control->sinfo_ppid;
60385f05199cSMichael Tuexen 		sinfo->sinfo_context = control->sinfo_context;
60395f05199cSMichael Tuexen 		sinfo->sinfo_timetolive = control->sinfo_timetolive;
60405f05199cSMichael Tuexen 		sinfo->sinfo_tsn = control->sinfo_tsn;
60415f05199cSMichael Tuexen 		sinfo->sinfo_cumtsn = control->sinfo_cumtsn;
60425f05199cSMichael Tuexen 		sinfo->sinfo_assoc_id = control->sinfo_assoc_id;
6043f8829a4aSRandall Stewart 		nxt = TAILQ_NEXT(control, next);
6044e2e7c62eSMichael Tuexen 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
6045e2e7c62eSMichael Tuexen 		    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) {
6046f8829a4aSRandall Stewart 			struct sctp_extrcvinfo *s_extra;
6047f8829a4aSRandall Stewart 
6048f8829a4aSRandall Stewart 			s_extra = (struct sctp_extrcvinfo *)sinfo;
60499a6142d8SRandall Stewart 			if ((nxt) &&
60509a6142d8SRandall Stewart 			    (nxt->length)) {
6051b70b526dSMichael Tuexen 				s_extra->serinfo_next_flags = SCTP_NEXT_MSG_AVAIL;
6052f8829a4aSRandall Stewart 				if (nxt->sinfo_flags & SCTP_UNORDERED) {
6053b70b526dSMichael Tuexen 					s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_IS_UNORDERED;
6054f8829a4aSRandall Stewart 				}
6055f42a358aSRandall Stewart 				if (nxt->spec_flags & M_NOTIFICATION) {
6056b70b526dSMichael Tuexen 					s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_IS_NOTIFICATION;
6057f42a358aSRandall Stewart 				}
6058b70b526dSMichael Tuexen 				s_extra->serinfo_next_aid = nxt->sinfo_assoc_id;
6059b70b526dSMichael Tuexen 				s_extra->serinfo_next_length = nxt->length;
6060b70b526dSMichael Tuexen 				s_extra->serinfo_next_ppid = nxt->sinfo_ppid;
6061b70b526dSMichael Tuexen 				s_extra->serinfo_next_stream = nxt->sinfo_stream;
6062f8829a4aSRandall Stewart 				if (nxt->tail_mbuf != NULL) {
6063139bc87fSRandall Stewart 					if (nxt->end_added) {
6064b70b526dSMichael Tuexen 						s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_ISCOMPLETE;
6065f8829a4aSRandall Stewart 					}
6066f8829a4aSRandall Stewart 				}
6067f8829a4aSRandall Stewart 			} else {
6068f8829a4aSRandall Stewart 				/*
6069f8829a4aSRandall Stewart 				 * we explicitly 0 this, since the memcpy
6070f8829a4aSRandall Stewart 				 * got some other things beyond the older
6071f8829a4aSRandall Stewart 				 * sinfo_ that is on the control's structure
6072f8829a4aSRandall Stewart 				 * :-D
6073f8829a4aSRandall Stewart 				 */
60749a6142d8SRandall Stewart 				nxt = NULL;
6075b70b526dSMichael Tuexen 				s_extra->serinfo_next_flags = SCTP_NO_NEXT_MSG;
6076b70b526dSMichael Tuexen 				s_extra->serinfo_next_aid = 0;
6077b70b526dSMichael Tuexen 				s_extra->serinfo_next_length = 0;
6078b70b526dSMichael Tuexen 				s_extra->serinfo_next_ppid = 0;
6079b70b526dSMichael Tuexen 				s_extra->serinfo_next_stream = 0;
6080f8829a4aSRandall Stewart 			}
6081f8829a4aSRandall Stewart 		}
6082f8829a4aSRandall Stewart 		/*
6083f8829a4aSRandall Stewart 		 * update off the real current cum-ack, if we have an stcb.
6084f8829a4aSRandall Stewart 		 */
60850696e120SRandall Stewart 		if ((control->do_not_ref_stcb == 0) && stcb)
6086f8829a4aSRandall Stewart 			sinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
6087f8829a4aSRandall Stewart 		/*
6088f8829a4aSRandall Stewart 		 * mask off the high bits, we keep the actual chunk bits in
6089f8829a4aSRandall Stewart 		 * there.
6090f8829a4aSRandall Stewart 		 */
6091f8829a4aSRandall Stewart 		sinfo->sinfo_flags &= 0x00ff;
60925f26a41dSRandall Stewart 		if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) {
60935f26a41dSRandall Stewart 			sinfo->sinfo_flags |= SCTP_UNORDERED;
60945f26a41dSRandall Stewart 		}
6095f8829a4aSRandall Stewart 	}
609618e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
609718e198d3SRandall Stewart 	{
609818e198d3SRandall Stewart 		int index, newindex;
609918e198d3SRandall Stewart 		struct sctp_pcbtsn_rlog *entry;
610018e198d3SRandall Stewart 
610118e198d3SRandall Stewart 		do {
610218e198d3SRandall Stewart 			index = inp->readlog_index;
610318e198d3SRandall Stewart 			newindex = index + 1;
610418e198d3SRandall Stewart 			if (newindex >= SCTP_READ_LOG_SIZE) {
610518e198d3SRandall Stewart 				newindex = 0;
610618e198d3SRandall Stewart 			}
610718e198d3SRandall Stewart 		} while (atomic_cmpset_int(&inp->readlog_index, index, newindex) == 0);
610818e198d3SRandall Stewart 		entry = &inp->readlog[index];
610918e198d3SRandall Stewart 		entry->vtag = control->sinfo_assoc_id;
611018e198d3SRandall Stewart 		entry->strm = control->sinfo_stream;
611149656eefSMichael Tuexen 		entry->seq = (uint16_t)control->mid;
611218e198d3SRandall Stewart 		entry->sz = control->length;
611318e198d3SRandall Stewart 		entry->flgs = control->sinfo_flags;
611418e198d3SRandall Stewart 	}
611518e198d3SRandall Stewart #endif
6116d59107f7SMichael Tuexen 	if ((fromlen > 0) && (from != NULL)) {
6117d59107f7SMichael Tuexen 		union sctp_sockstore store;
6118d59107f7SMichael Tuexen 		size_t len;
6119d59107f7SMichael Tuexen 
6120b5b6e5c2SMichael Tuexen 		switch (control->whoFrom->ro._l_addr.sa.sa_family) {
6121b5b6e5c2SMichael Tuexen #ifdef INET6
6122b5b6e5c2SMichael Tuexen 		case AF_INET6:
6123d59107f7SMichael Tuexen 			len = sizeof(struct sockaddr_in6);
6124d59107f7SMichael Tuexen 			store.sin6 = control->whoFrom->ro._l_addr.sin6;
6125d59107f7SMichael Tuexen 			store.sin6.sin6_port = control->port_from;
6126b5b6e5c2SMichael Tuexen 			break;
6127f8829a4aSRandall Stewart #endif
6128b5b6e5c2SMichael Tuexen #ifdef INET
6129b5b6e5c2SMichael Tuexen 		case AF_INET:
6130d59107f7SMichael Tuexen #ifdef INET6
6131d59107f7SMichael Tuexen 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
6132d59107f7SMichael Tuexen 				len = sizeof(struct sockaddr_in6);
6133d59107f7SMichael Tuexen 				in6_sin_2_v4mapsin6(&control->whoFrom->ro._l_addr.sin,
6134d59107f7SMichael Tuexen 				    &store.sin6);
6135d59107f7SMichael Tuexen 				store.sin6.sin6_port = control->port_from;
6136d59107f7SMichael Tuexen 			} else {
6137d59107f7SMichael Tuexen 				len = sizeof(struct sockaddr_in);
6138d59107f7SMichael Tuexen 				store.sin = control->whoFrom->ro._l_addr.sin;
6139d59107f7SMichael Tuexen 				store.sin.sin_port = control->port_from;
6140d59107f7SMichael Tuexen 			}
6141d59107f7SMichael Tuexen #else
6142d59107f7SMichael Tuexen 			len = sizeof(struct sockaddr_in);
6143d59107f7SMichael Tuexen 			store.sin = control->whoFrom->ro._l_addr.sin;
6144d59107f7SMichael Tuexen 			store.sin.sin_port = control->port_from;
6145d59107f7SMichael Tuexen #endif
6146b5b6e5c2SMichael Tuexen 			break;
6147b5b6e5c2SMichael Tuexen #endif
6148b5b6e5c2SMichael Tuexen 		default:
6149d59107f7SMichael Tuexen 			len = 0;
6150b5b6e5c2SMichael Tuexen 			break;
6151b5b6e5c2SMichael Tuexen 		}
6152d59107f7SMichael Tuexen 		memcpy(from, &store, min((size_t)fromlen, len));
6153e0e00a4dSMichael Tuexen #ifdef INET6
6154f8829a4aSRandall Stewart 		{
6155b5b6e5c2SMichael Tuexen 			struct sockaddr_in6 lsa6, *from6;
6156f8829a4aSRandall Stewart 
6157b5b6e5c2SMichael Tuexen 			from6 = (struct sockaddr_in6 *)from;
6158b5b6e5c2SMichael Tuexen 			sctp_recover_scope_mac(from6, (&lsa6));
6159f8829a4aSRandall Stewart 		}
6160f8829a4aSRandall Stewart #endif
6161f8829a4aSRandall Stewart 	}
61629c5ca6f2SMichael Tuexen 	if (hold_rlock) {
61639c5ca6f2SMichael Tuexen 		SCTP_INP_READ_UNLOCK(inp);
61649c5ca6f2SMichael Tuexen 		hold_rlock = 0;
61659c5ca6f2SMichael Tuexen 	}
61669c5ca6f2SMichael Tuexen 	if (hold_sblock) {
61679c5ca6f2SMichael Tuexen 		SOCKBUF_UNLOCK(&so->so_rcv);
61689c5ca6f2SMichael Tuexen 		hold_sblock = 0;
61699c5ca6f2SMichael Tuexen 	}
6170f8829a4aSRandall Stewart 	/* now copy out what data we can */
6171f8829a4aSRandall Stewart 	if (mp == NULL) {
6172f8829a4aSRandall Stewart 		/* copy out each mbuf in the chain up to length */
6173f8829a4aSRandall Stewart get_more_data:
6174f8829a4aSRandall Stewart 		m = control->data;
6175f8829a4aSRandall Stewart 		while (m) {
6176f8829a4aSRandall Stewart 			/* Move out all we can */
61770d3cf13dSMichael Tuexen 			cp_len = uio->uio_resid;
61780d3cf13dSMichael Tuexen 			my_len = SCTP_BUF_LEN(m);
6179f8829a4aSRandall Stewart 			if (cp_len > my_len) {
6180f8829a4aSRandall Stewart 				/* not enough in this buf */
6181f8829a4aSRandall Stewart 				cp_len = my_len;
6182f8829a4aSRandall Stewart 			}
6183f8829a4aSRandall Stewart 			if (hold_rlock) {
6184f8829a4aSRandall Stewart 				SCTP_INP_READ_UNLOCK(inp);
6185f8829a4aSRandall Stewart 				hold_rlock = 0;
6186f8829a4aSRandall Stewart 			}
6187f8829a4aSRandall Stewart 			if (cp_len > 0)
618858e6eeefSMichael Tuexen 				error = uiomove(mtod(m, char *), (int)cp_len, uio);
6189f8829a4aSRandall Stewart 			/* re-read */
6190f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
6191f8829a4aSRandall Stewart 				goto release;
6192f8829a4aSRandall Stewart 			}
61930053ed28SMichael Tuexen 
61940696e120SRandall Stewart 			if ((control->do_not_ref_stcb == 0) && stcb &&
6195f8829a4aSRandall Stewart 			    stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
6196f8829a4aSRandall Stewart 				no_rcv_needed = 1;
6197f8829a4aSRandall Stewart 			}
6198f8829a4aSRandall Stewart 			if (error) {
6199f8829a4aSRandall Stewart 				/* error we are out of here */
6200f8829a4aSRandall Stewart 				goto release;
6201f8829a4aSRandall Stewart 			}
6202f8829a4aSRandall Stewart 			SCTP_INP_READ_LOCK(inp);
6203f8829a4aSRandall Stewart 			hold_rlock = 1;
6204139bc87fSRandall Stewart 			if (cp_len == SCTP_BUF_LEN(m)) {
6205139bc87fSRandall Stewart 				if ((SCTP_BUF_NEXT(m) == NULL) &&
6206139bc87fSRandall Stewart 				    (control->end_added)) {
6207f8829a4aSRandall Stewart 					out_flags |= MSG_EOR;
620852129fcdSRandall Stewart 					if ((control->do_not_ref_stcb == 0) &&
620952129fcdSRandall Stewart 					    (control->stcb != NULL) &&
621052129fcdSRandall Stewart 					    ((control->spec_flags & M_NOTIFICATION) == 0))
6211ee7f9857SRandall Stewart 						control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6212f8829a4aSRandall Stewart 				}
6213139bc87fSRandall Stewart 				if (control->spec_flags & M_NOTIFICATION) {
6214f8829a4aSRandall Stewart 					out_flags |= MSG_NOTIFICATION;
6215f8829a4aSRandall Stewart 				}
6216f8829a4aSRandall Stewart 				/* we ate up the mbuf */
6217f8829a4aSRandall Stewart 				if (in_flags & MSG_PEEK) {
6218f8829a4aSRandall Stewart 					/* just looking */
6219139bc87fSRandall Stewart 					m = SCTP_BUF_NEXT(m);
6220f8829a4aSRandall Stewart 					copied_so_far += cp_len;
6221f8829a4aSRandall Stewart 				} else {
6222f8829a4aSRandall Stewart 					/* dispose of the mbuf */
6223b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6224f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv,
6225139bc87fSRandall Stewart 						    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
622680fefe0aSRandall Stewart 					}
6227f8829a4aSRandall Stewart 					sctp_sbfree(control, stcb, &so->so_rcv, m);
6228b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6229f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv,
6230f8829a4aSRandall Stewart 						    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
623180fefe0aSRandall Stewart 					}
6232f8829a4aSRandall Stewart 					copied_so_far += cp_len;
623358e6eeefSMichael Tuexen 					freed_so_far += (uint32_t)cp_len;
6234c4739e2fSRandall Stewart 					freed_so_far += MSIZE;
623518e198d3SRandall Stewart 					atomic_subtract_int(&control->length, cp_len);
6236f8829a4aSRandall Stewart 					control->data = sctp_m_free(m);
6237f8829a4aSRandall Stewart 					m = control->data;
6238b7b84c0eSMichael Tuexen 					/*
6239b7b84c0eSMichael Tuexen 					 * been through it all, must hold sb
6240b7b84c0eSMichael Tuexen 					 * lock ok to null tail
6241b7b84c0eSMichael Tuexen 					 */
6242f8829a4aSRandall Stewart 					if (control->data == NULL) {
6243a5d547adSRandall Stewart #ifdef INVARIANTS
6244f8829a4aSRandall Stewart 						if ((control->end_added == 0) ||
6245f8829a4aSRandall Stewart 						    (TAILQ_NEXT(control, next) == NULL)) {
6246f8829a4aSRandall Stewart 							/*
6247f8829a4aSRandall Stewart 							 * If the end is not
6248f8829a4aSRandall Stewart 							 * added, OR the
6249f8829a4aSRandall Stewart 							 * next is NOT null
6250f8829a4aSRandall Stewart 							 * we MUST have the
6251f8829a4aSRandall Stewart 							 * lock.
6252f8829a4aSRandall Stewart 							 */
6253f8829a4aSRandall Stewart 							if (mtx_owned(&inp->inp_rdata_mtx) == 0) {
6254f8829a4aSRandall Stewart 								panic("Hmm we don't own the lock?");
6255f8829a4aSRandall Stewart 							}
6256f8829a4aSRandall Stewart 						}
6257f8829a4aSRandall Stewart #endif
6258f8829a4aSRandall Stewart 						control->tail_mbuf = NULL;
6259a5d547adSRandall Stewart #ifdef INVARIANTS
6260f8829a4aSRandall Stewart 						if ((control->end_added) && ((out_flags & MSG_EOR) == 0)) {
6261f8829a4aSRandall Stewart 							panic("end_added, nothing left and no MSG_EOR");
6262f8829a4aSRandall Stewart 						}
6263f8829a4aSRandall Stewart #endif
6264f8829a4aSRandall Stewart 					}
6265f8829a4aSRandall Stewart 				}
6266f8829a4aSRandall Stewart 			} else {
6267f8829a4aSRandall Stewart 				/* Do we need to trim the mbuf? */
6268139bc87fSRandall Stewart 				if (control->spec_flags & M_NOTIFICATION) {
6269f8829a4aSRandall Stewart 					out_flags |= MSG_NOTIFICATION;
6270f8829a4aSRandall Stewart 				}
6271f8829a4aSRandall Stewart 				if ((in_flags & MSG_PEEK) == 0) {
6272139bc87fSRandall Stewart 					SCTP_BUF_RESV_UF(m, cp_len);
627358e6eeefSMichael Tuexen 					SCTP_BUF_LEN(m) -= (int)cp_len;
6274b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
627558e6eeefSMichael Tuexen 						sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, (int)cp_len);
627680fefe0aSRandall Stewart 					}
62774e88d37aSMichael Tuexen 					atomic_subtract_int(&so->so_rcv.sb_cc, cp_len);
62780696e120SRandall Stewart 					if ((control->do_not_ref_stcb == 0) &&
62790696e120SRandall Stewart 					    stcb) {
62804e88d37aSMichael Tuexen 						atomic_subtract_int(&stcb->asoc.sb_cc, cp_len);
6281f8829a4aSRandall Stewart 					}
6282f8829a4aSRandall Stewart 					copied_so_far += cp_len;
628358e6eeefSMichael Tuexen 					freed_so_far += (uint32_t)cp_len;
6284c4739e2fSRandall Stewart 					freed_so_far += MSIZE;
6285b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6286f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb,
6287f8829a4aSRandall Stewart 						    SCTP_LOG_SBRESULT, 0);
628880fefe0aSRandall Stewart 					}
628918e198d3SRandall Stewart 					atomic_subtract_int(&control->length, cp_len);
6290f8829a4aSRandall Stewart 				} else {
6291f8829a4aSRandall Stewart 					copied_so_far += cp_len;
6292f8829a4aSRandall Stewart 				}
6293f8829a4aSRandall Stewart 			}
6294d61a0ae0SRandall Stewart 			if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) {
6295f8829a4aSRandall Stewart 				break;
6296f8829a4aSRandall Stewart 			}
6297f8829a4aSRandall Stewart 			if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
6298f8829a4aSRandall Stewart 			    (control->do_not_ref_stcb == 0) &&
6299f8829a4aSRandall Stewart 			    (freed_so_far >= rwnd_req)) {
6300f8829a4aSRandall Stewart 				sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6301f8829a4aSRandall Stewart 			}
6302f8829a4aSRandall Stewart 		}		/* end while(m) */
6303f8829a4aSRandall Stewart 		/*
6304f8829a4aSRandall Stewart 		 * At this point we have looked at it all and we either have
6305f8829a4aSRandall Stewart 		 * a MSG_EOR/or read all the user wants... <OR>
6306f8829a4aSRandall Stewart 		 * control->length == 0.
6307f8829a4aSRandall Stewart 		 */
6308d61a0ae0SRandall Stewart 		if ((out_flags & MSG_EOR) && ((in_flags & MSG_PEEK) == 0)) {
6309f8829a4aSRandall Stewart 			/* we are done with this control */
6310f8829a4aSRandall Stewart 			if (control->length == 0) {
6311f8829a4aSRandall Stewart 				if (control->data) {
6312a5d547adSRandall Stewart #ifdef INVARIANTS
6313f8829a4aSRandall Stewart 					panic("control->data not null at read eor?");
6314f8829a4aSRandall Stewart #else
6315ad81507eSRandall Stewart 					SCTP_PRINTF("Strange, data left in the control buffer .. invarients would panic?\n");
6316f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
6317f8829a4aSRandall Stewart 					control->data = NULL;
6318f8829a4aSRandall Stewart #endif
6319f8829a4aSRandall Stewart 				}
6320f8829a4aSRandall Stewart 		done_with_control:
6321f8829a4aSRandall Stewart 				if (hold_rlock == 0) {
6322f8829a4aSRandall Stewart 					SCTP_INP_READ_LOCK(inp);
6323f8829a4aSRandall Stewart 					hold_rlock = 1;
6324f8829a4aSRandall Stewart 				}
6325f8829a4aSRandall Stewart 				TAILQ_REMOVE(&inp->read_queue, control, next);
6326f8829a4aSRandall Stewart 				/* Add back any hiddend data */
6327f8829a4aSRandall Stewart 				if (control->held_length) {
6328f8829a4aSRandall Stewart 					held_length = 0;
6329f8829a4aSRandall Stewart 					control->held_length = 0;
6330f8829a4aSRandall Stewart 					wakeup_read_socket = 1;
6331f8829a4aSRandall Stewart 				}
633217205eccSRandall Stewart 				if (control->aux_data) {
633317205eccSRandall Stewart 					sctp_m_free(control->aux_data);
633417205eccSRandall Stewart 					control->aux_data = NULL;
633517205eccSRandall Stewart 				}
6336f8829a4aSRandall Stewart 				no_rcv_needed = control->do_not_ref_stcb;
6337f8829a4aSRandall Stewart 				sctp_free_remote_addr(control->whoFrom);
6338f8829a4aSRandall Stewart 				control->data = NULL;
633998d5fd97SMichael Tuexen #ifdef INVARIANTS
634044249214SRandall Stewart 				if (control->on_strm_q) {
634144249214SRandall Stewart 					panic("About to free ctl:%p so:%p and its in %d",
634244249214SRandall Stewart 					    control, so, control->on_strm_q);
634344249214SRandall Stewart 				}
634498d5fd97SMichael Tuexen #endif
6345f8829a4aSRandall Stewart 				sctp_free_a_readq(stcb, control);
6346f8829a4aSRandall Stewart 				control = NULL;
63470696e120SRandall Stewart 				if ((freed_so_far >= rwnd_req) &&
63480696e120SRandall Stewart 				    (no_rcv_needed == 0))
6349f8829a4aSRandall Stewart 					sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6350f8829a4aSRandall Stewart 
6351f8829a4aSRandall Stewart 			} else {
6352f8829a4aSRandall Stewart 				/*
6353f8829a4aSRandall Stewart 				 * The user did not read all of this
6354f8829a4aSRandall Stewart 				 * message, turn off the returned MSG_EOR
6355f8829a4aSRandall Stewart 				 * since we are leaving more behind on the
6356f8829a4aSRandall Stewart 				 * control to read.
6357f8829a4aSRandall Stewart 				 */
6358a5d547adSRandall Stewart #ifdef INVARIANTS
63590696e120SRandall Stewart 				if (control->end_added &&
63600696e120SRandall Stewart 				    (control->data == NULL) &&
6361f8829a4aSRandall Stewart 				    (control->tail_mbuf == NULL)) {
6362f8829a4aSRandall Stewart 					panic("Gak, control->length is corrupt?");
6363f8829a4aSRandall Stewart 				}
6364f8829a4aSRandall Stewart #endif
6365f8829a4aSRandall Stewart 				no_rcv_needed = control->do_not_ref_stcb;
6366f8829a4aSRandall Stewart 				out_flags &= ~MSG_EOR;
6367f8829a4aSRandall Stewart 			}
6368f8829a4aSRandall Stewart 		}
6369f8829a4aSRandall Stewart 		if (out_flags & MSG_EOR) {
6370f8829a4aSRandall Stewart 			goto release;
6371f8829a4aSRandall Stewart 		}
6372f8829a4aSRandall Stewart 		if ((uio->uio_resid == 0) ||
637304aab884SMichael Tuexen 		    ((in_eeor_mode) &&
637443ecbff2SMichael Tuexen 		    (copied_so_far >= max(so->so_rcv.sb_lowat, 1)))) {
6375f8829a4aSRandall Stewart 			goto release;
6376f8829a4aSRandall Stewart 		}
6377f8829a4aSRandall Stewart 		/*
6378f8829a4aSRandall Stewart 		 * If I hit here the receiver wants more and this message is
6379f8829a4aSRandall Stewart 		 * NOT done (pd-api). So two questions. Can we block? if not
6380f8829a4aSRandall Stewart 		 * we are done. Did the user NOT set MSG_WAITALL?
6381f8829a4aSRandall Stewart 		 */
6382f8829a4aSRandall Stewart 		if (block_allowed == 0) {
6383f8829a4aSRandall Stewart 			goto release;
6384f8829a4aSRandall Stewart 		}
6385f8829a4aSRandall Stewart 		/*
6386f8829a4aSRandall Stewart 		 * We need to wait for more data a few things: - We don't
6387f8829a4aSRandall Stewart 		 * sbunlock() so we don't get someone else reading. - We
6388f8829a4aSRandall Stewart 		 * must be sure to account for the case where what is added
6389f8829a4aSRandall Stewart 		 * is NOT to our control when we wakeup.
6390f8829a4aSRandall Stewart 		 */
6391f8829a4aSRandall Stewart 
6392f8829a4aSRandall Stewart 		/*
6393f8829a4aSRandall Stewart 		 * Do we need to tell the transport a rwnd update might be
6394f8829a4aSRandall Stewart 		 * needed before we go to sleep?
6395f8829a4aSRandall Stewart 		 */
6396f8829a4aSRandall Stewart 		if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
6397f8829a4aSRandall Stewart 		    ((freed_so_far >= rwnd_req) &&
6398f8829a4aSRandall Stewart 		    (control->do_not_ref_stcb == 0) &&
6399f8829a4aSRandall Stewart 		    (no_rcv_needed == 0))) {
6400f8829a4aSRandall Stewart 			sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6401f8829a4aSRandall Stewart 		}
6402f8829a4aSRandall Stewart wait_some_more:
640344b7479bSRandall Stewart 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
6404f8829a4aSRandall Stewart 			goto release;
6405f8829a4aSRandall Stewart 		}
64060053ed28SMichael Tuexen 
6407f8829a4aSRandall Stewart 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)
6408f8829a4aSRandall Stewart 			goto release;
6409f8829a4aSRandall Stewart 
6410f8829a4aSRandall Stewart 		if (hold_rlock == 1) {
6411f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
6412f8829a4aSRandall Stewart 			hold_rlock = 0;
6413f8829a4aSRandall Stewart 		}
6414f8829a4aSRandall Stewart 		if (hold_sblock == 0) {
6415f8829a4aSRandall Stewart 			SOCKBUF_LOCK(&so->so_rcv);
6416f8829a4aSRandall Stewart 			hold_sblock = 1;
6417f8829a4aSRandall Stewart 		}
6418851b7298SRandall Stewart 		if ((copied_so_far) && (control->length == 0) &&
6419b5c16493SMichael Tuexen 		    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE))) {
6420851b7298SRandall Stewart 			goto release;
6421851b7298SRandall Stewart 		}
64224e88d37aSMichael Tuexen 		if (so->so_rcv.sb_cc <= control->held_length) {
6423f8829a4aSRandall Stewart 			error = sbwait(&so->so_rcv);
6424f8829a4aSRandall Stewart 			if (error) {
6425f8829a4aSRandall Stewart 				goto release;
6426f8829a4aSRandall Stewart 			}
6427f8829a4aSRandall Stewart 			control->held_length = 0;
6428f8829a4aSRandall Stewart 		}
6429f8829a4aSRandall Stewart 		if (hold_sblock) {
6430f8829a4aSRandall Stewart 			SOCKBUF_UNLOCK(&so->so_rcv);
6431f8829a4aSRandall Stewart 			hold_sblock = 0;
6432f8829a4aSRandall Stewart 		}
6433f8829a4aSRandall Stewart 		if (control->length == 0) {
6434f8829a4aSRandall Stewart 			/* still nothing here */
6435f8829a4aSRandall Stewart 			if (control->end_added == 1) {
6436f8829a4aSRandall Stewart 				/* he aborted, or is done i.e.did a shutdown */
6437f8829a4aSRandall Stewart 				out_flags |= MSG_EOR;
64389a6142d8SRandall Stewart 				if (control->pdapi_aborted) {
64396114cd96SRandall Stewart 					if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
6440ee7f9857SRandall Stewart 						control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
64419a6142d8SRandall Stewart 
644203b0b021SRandall Stewart 					out_flags |= MSG_TRUNC;
64439a6142d8SRandall Stewart 				} else {
64446114cd96SRandall Stewart 					if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
6445ee7f9857SRandall Stewart 						control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
64469a6142d8SRandall Stewart 				}
6447f8829a4aSRandall Stewart 				goto done_with_control;
6448f8829a4aSRandall Stewart 			}
64494e88d37aSMichael Tuexen 			if (so->so_rcv.sb_cc > held_length) {
64504e88d37aSMichael Tuexen 				control->held_length = so->so_rcv.sb_cc;
6451f8829a4aSRandall Stewart 				held_length = 0;
6452f8829a4aSRandall Stewart 			}
6453f8829a4aSRandall Stewart 			goto wait_some_more;
6454f8829a4aSRandall Stewart 		} else if (control->data == NULL) {
645550cec919SRandall Stewart 			/*
645650cec919SRandall Stewart 			 * we must re-sync since data is probably being
645750cec919SRandall Stewart 			 * added
645850cec919SRandall Stewart 			 */
645950cec919SRandall Stewart 			SCTP_INP_READ_LOCK(inp);
646050cec919SRandall Stewart 			if ((control->length > 0) && (control->data == NULL)) {
6461b7b84c0eSMichael Tuexen 				/*
6462b7b84c0eSMichael Tuexen 				 * big trouble.. we have the lock and its
6463b7b84c0eSMichael Tuexen 				 * corrupt?
6464b7b84c0eSMichael Tuexen 				 */
64659c04b296SRandall Stewart #ifdef INVARIANTS
64669d18771fSRandall Stewart 				panic("Impossible data==NULL length !=0");
64679c04b296SRandall Stewart #endif
64689c04b296SRandall Stewart 				out_flags |= MSG_EOR;
64699c04b296SRandall Stewart 				out_flags |= MSG_TRUNC;
64709c04b296SRandall Stewart 				control->length = 0;
64719c04b296SRandall Stewart 				SCTP_INP_READ_UNLOCK(inp);
64729c04b296SRandall Stewart 				goto done_with_control;
6473f8829a4aSRandall Stewart 			}
647450cec919SRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
647550cec919SRandall Stewart 			/* We will fall around to get more data */
647650cec919SRandall Stewart 		}
6477f8829a4aSRandall Stewart 		goto get_more_data;
6478f8829a4aSRandall Stewart 	} else {
647917205eccSRandall Stewart 		/*-
648017205eccSRandall Stewart 		 * Give caller back the mbuf chain,
648117205eccSRandall Stewart 		 * store in uio_resid the length
6482f8829a4aSRandall Stewart 		 */
648317205eccSRandall Stewart 		wakeup_read_socket = 0;
6484f8829a4aSRandall Stewart 		if ((control->end_added == 0) ||
6485f8829a4aSRandall Stewart 		    (TAILQ_NEXT(control, next) == NULL)) {
6486f8829a4aSRandall Stewart 			/* Need to get rlock */
6487f8829a4aSRandall Stewart 			if (hold_rlock == 0) {
6488f8829a4aSRandall Stewart 				SCTP_INP_READ_LOCK(inp);
6489f8829a4aSRandall Stewart 				hold_rlock = 1;
6490f8829a4aSRandall Stewart 			}
6491f8829a4aSRandall Stewart 		}
6492139bc87fSRandall Stewart 		if (control->end_added) {
6493f8829a4aSRandall Stewart 			out_flags |= MSG_EOR;
649460990c0cSMichael Tuexen 			if ((control->do_not_ref_stcb == 0) &&
649560990c0cSMichael Tuexen 			    (control->stcb != NULL) &&
649660990c0cSMichael Tuexen 			    ((control->spec_flags & M_NOTIFICATION) == 0))
6497ee7f9857SRandall Stewart 				control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6498f8829a4aSRandall Stewart 		}
6499139bc87fSRandall Stewart 		if (control->spec_flags & M_NOTIFICATION) {
6500f8829a4aSRandall Stewart 			out_flags |= MSG_NOTIFICATION;
6501f8829a4aSRandall Stewart 		}
650217205eccSRandall Stewart 		uio->uio_resid = control->length;
6503f8829a4aSRandall Stewart 		*mp = control->data;
6504f8829a4aSRandall Stewart 		m = control->data;
6505f8829a4aSRandall Stewart 		while (m) {
6506b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6507f8829a4aSRandall Stewart 				sctp_sblog(&so->so_rcv,
6508139bc87fSRandall Stewart 				    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
650980fefe0aSRandall Stewart 			}
6510f8829a4aSRandall Stewart 			sctp_sbfree(control, stcb, &so->so_rcv, m);
651158e6eeefSMichael Tuexen 			freed_so_far += (uint32_t)SCTP_BUF_LEN(m);
6512c4739e2fSRandall Stewart 			freed_so_far += MSIZE;
6513b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6514f8829a4aSRandall Stewart 				sctp_sblog(&so->so_rcv,
6515f8829a4aSRandall Stewart 				    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
651680fefe0aSRandall Stewart 			}
6517139bc87fSRandall Stewart 			m = SCTP_BUF_NEXT(m);
6518f8829a4aSRandall Stewart 		}
6519f8829a4aSRandall Stewart 		control->data = control->tail_mbuf = NULL;
6520f8829a4aSRandall Stewart 		control->length = 0;
6521f8829a4aSRandall Stewart 		if (out_flags & MSG_EOR) {
6522f8829a4aSRandall Stewart 			/* Done with this control */
6523f8829a4aSRandall Stewart 			goto done_with_control;
6524f8829a4aSRandall Stewart 		}
6525f8829a4aSRandall Stewart 	}
6526f8829a4aSRandall Stewart release:
6527f8829a4aSRandall Stewart 	if (hold_rlock == 1) {
6528f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
6529f8829a4aSRandall Stewart 		hold_rlock = 0;
6530f8829a4aSRandall Stewart 	}
65317abab911SRobert Watson 	if (hold_sblock == 1) {
65327abab911SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
65337abab911SRobert Watson 		hold_sblock = 0;
6534f8829a4aSRandall Stewart 	}
65350053ed28SMichael Tuexen 
6536f8829a4aSRandall Stewart 	sbunlock(&so->so_rcv);
65377abab911SRobert Watson 	sockbuf_lock = 0;
6538f8829a4aSRandall Stewart 
6539f8829a4aSRandall Stewart release_unlocked:
6540f8829a4aSRandall Stewart 	if (hold_sblock) {
6541f8829a4aSRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
6542f8829a4aSRandall Stewart 		hold_sblock = 0;
6543f8829a4aSRandall Stewart 	}
6544f8829a4aSRandall Stewart 	if ((stcb) && (in_flags & MSG_PEEK) == 0) {
6545f8829a4aSRandall Stewart 		if ((freed_so_far >= rwnd_req) &&
6546f8829a4aSRandall Stewart 		    (control && (control->do_not_ref_stcb == 0)) &&
6547f8829a4aSRandall Stewart 		    (no_rcv_needed == 0))
6548f8829a4aSRandall Stewart 			sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6549f8829a4aSRandall Stewart 	}
6550f8829a4aSRandall Stewart out:
65511b9f62a0SRandall Stewart 	if (msg_flags) {
65521b9f62a0SRandall Stewart 		*msg_flags = out_flags;
65531b9f62a0SRandall Stewart 	}
65549a6142d8SRandall Stewart 	if (((out_flags & MSG_EOR) == 0) &&
65559a6142d8SRandall Stewart 	    ((in_flags & MSG_PEEK) == 0) &&
65569a6142d8SRandall Stewart 	    (sinfo) &&
6557e2e7c62eSMichael Tuexen 	    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
6558e2e7c62eSMichael Tuexen 	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO))) {
65599a6142d8SRandall Stewart 		struct sctp_extrcvinfo *s_extra;
65609a6142d8SRandall Stewart 
65619a6142d8SRandall Stewart 		s_extra = (struct sctp_extrcvinfo *)sinfo;
6562b70b526dSMichael Tuexen 		s_extra->serinfo_next_flags = SCTP_NO_NEXT_MSG;
65639a6142d8SRandall Stewart 	}
6564f8829a4aSRandall Stewart 	if (hold_rlock == 1) {
6565f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
6566f8829a4aSRandall Stewart 	}
6567f8829a4aSRandall Stewart 	if (hold_sblock) {
6568f8829a4aSRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
6569f8829a4aSRandall Stewart 	}
65707abab911SRobert Watson 	if (sockbuf_lock) {
65717abab911SRobert Watson 		sbunlock(&so->so_rcv);
65727abab911SRobert Watson 	}
65730053ed28SMichael Tuexen 
657450cec919SRandall Stewart 	if (freecnt_applied) {
6575f8829a4aSRandall Stewart 		/*
6576f8829a4aSRandall Stewart 		 * The lock on the socket buffer protects us so the free
6577f8829a4aSRandall Stewart 		 * code will stop. But since we used the socketbuf lock and
6578f8829a4aSRandall Stewart 		 * the sender uses the tcb_lock to increment, we need to use
6579f8829a4aSRandall Stewart 		 * the atomic add to the refcnt.
6580f8829a4aSRandall Stewart 		 */
658150cec919SRandall Stewart 		if (stcb == NULL) {
6582df6e0cc3SRandall Stewart #ifdef INVARIANTS
658350cec919SRandall Stewart 			panic("stcb for refcnt has gone NULL?");
6584df6e0cc3SRandall Stewart 			goto stage_left;
6585df6e0cc3SRandall Stewart #else
6586df6e0cc3SRandall Stewart 			goto stage_left;
6587df6e0cc3SRandall Stewart #endif
658850cec919SRandall Stewart 		}
6589f8829a4aSRandall Stewart 		/* Save the value back for next time */
6590f8829a4aSRandall Stewart 		stcb->freed_by_sorcv_sincelast = freed_so_far;
6591cf46caceSMichael Tuexen 		atomic_add_int(&stcb->asoc.refcnt, -1);
6592f8829a4aSRandall Stewart 	}
6593b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
6594f8829a4aSRandall Stewart 		if (stcb) {
6595f8829a4aSRandall Stewart 			sctp_misc_ints(SCTP_SORECV_DONE,
6596f8829a4aSRandall Stewart 			    freed_so_far,
65979a8e3088SMichael Tuexen 			    (uint32_t)((uio) ? (slen - uio->uio_resid) : slen),
6598f8829a4aSRandall Stewart 			    stcb->asoc.my_rwnd,
65994e88d37aSMichael Tuexen 			    so->so_rcv.sb_cc);
6600f8829a4aSRandall Stewart 		} else {
6601f8829a4aSRandall Stewart 			sctp_misc_ints(SCTP_SORECV_DONE,
6602f8829a4aSRandall Stewart 			    freed_so_far,
66039a8e3088SMichael Tuexen 			    (uint32_t)((uio) ? (slen - uio->uio_resid) : slen),
6604f8829a4aSRandall Stewart 			    0,
66054e88d37aSMichael Tuexen 			    so->so_rcv.sb_cc);
6606f8829a4aSRandall Stewart 		}
660780fefe0aSRandall Stewart 	}
6608df6e0cc3SRandall Stewart stage_left:
6609f8829a4aSRandall Stewart 	if (wakeup_read_socket) {
6610f8829a4aSRandall Stewart 		sctp_sorwakeup(inp, so);
6611f8829a4aSRandall Stewart 	}
6612f8829a4aSRandall Stewart 	return (error);
6613f8829a4aSRandall Stewart }
6614f8829a4aSRandall Stewart 
6615f8829a4aSRandall Stewart 
6616f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
6617f8829a4aSRandall Stewart struct mbuf *
6618f8829a4aSRandall Stewart sctp_m_free(struct mbuf *m)
6619f8829a4aSRandall Stewart {
6620b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6621f8829a4aSRandall Stewart 		sctp_log_mb(m, SCTP_MBUF_IFREE);
6622f8829a4aSRandall Stewart 	}
6623f8829a4aSRandall Stewart 	return (m_free(m));
6624f8829a4aSRandall Stewart }
6625f8829a4aSRandall Stewart 
6626f8829a4aSRandall Stewart void
6627f8829a4aSRandall Stewart sctp_m_freem(struct mbuf *mb)
6628f8829a4aSRandall Stewart {
6629f8829a4aSRandall Stewart 	while (mb != NULL)
6630f8829a4aSRandall Stewart 		mb = sctp_m_free(mb);
6631f8829a4aSRandall Stewart }
6632f8829a4aSRandall Stewart 
6633f8829a4aSRandall Stewart #endif
6634f8829a4aSRandall Stewart 
663542551e99SRandall Stewart int
663642551e99SRandall Stewart sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id)
663742551e99SRandall Stewart {
663842551e99SRandall Stewart 	/*
663942551e99SRandall Stewart 	 * Given a local address. For all associations that holds the
664042551e99SRandall Stewart 	 * address, request a peer-set-primary.
664142551e99SRandall Stewart 	 */
664242551e99SRandall Stewart 	struct sctp_ifa *ifa;
664342551e99SRandall Stewart 	struct sctp_laddr *wi;
664442551e99SRandall Stewart 
664542551e99SRandall Stewart 	ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0);
664642551e99SRandall Stewart 	if (ifa == NULL) {
6647c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EADDRNOTAVAIL);
664842551e99SRandall Stewart 		return (EADDRNOTAVAIL);
664942551e99SRandall Stewart 	}
665042551e99SRandall Stewart 	/*
665142551e99SRandall Stewart 	 * Now that we have the ifa we must awaken the iterator with this
665242551e99SRandall Stewart 	 * message.
665342551e99SRandall Stewart 	 */
6654b3f1ea41SRandall Stewart 	wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
665542551e99SRandall Stewart 	if (wi == NULL) {
6656c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
665742551e99SRandall Stewart 		return (ENOMEM);
665842551e99SRandall Stewart 	}
665942551e99SRandall Stewart 	/* Now incr the count and int wi structure */
666042551e99SRandall Stewart 	SCTP_INCR_LADDR_COUNT();
66615ba7f91fSMichael Tuexen 	memset(wi, 0, sizeof(*wi));
6662d61a0ae0SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
666342551e99SRandall Stewart 	wi->ifa = ifa;
666442551e99SRandall Stewart 	wi->action = SCTP_SET_PRIM_ADDR;
666542551e99SRandall Stewart 	atomic_add_int(&ifa->refcount, 1);
666642551e99SRandall Stewart 
666742551e99SRandall Stewart 	/* Now add it to the work queue */
6668f7517433SRandall Stewart 	SCTP_WQ_ADDR_LOCK();
666942551e99SRandall Stewart 	/*
667042551e99SRandall Stewart 	 * Should this really be a tailq? As it is we will process the
667142551e99SRandall Stewart 	 * newest first :-0
667242551e99SRandall Stewart 	 */
6673b3f1ea41SRandall Stewart 	LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
667442551e99SRandall Stewart 	sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
667542551e99SRandall Stewart 	    (struct sctp_inpcb *)NULL,
667642551e99SRandall Stewart 	    (struct sctp_tcb *)NULL,
667742551e99SRandall Stewart 	    (struct sctp_nets *)NULL);
66782c62ba73SMichael Tuexen 	SCTP_WQ_ADDR_UNLOCK();
667942551e99SRandall Stewart 	return (0);
668042551e99SRandall Stewart }
668142551e99SRandall Stewart 
668242551e99SRandall Stewart 
6683f8829a4aSRandall Stewart int
668417205eccSRandall Stewart sctp_soreceive(struct socket *so,
668517205eccSRandall Stewart     struct sockaddr **psa,
668617205eccSRandall Stewart     struct uio *uio,
668717205eccSRandall Stewart     struct mbuf **mp0,
668817205eccSRandall Stewart     struct mbuf **controlp,
668917205eccSRandall Stewart     int *flagsp)
6690f8829a4aSRandall Stewart {
6691f8829a4aSRandall Stewart 	int error, fromlen;
6692f8829a4aSRandall Stewart 	uint8_t sockbuf[256];
6693f8829a4aSRandall Stewart 	struct sockaddr *from;
6694f8829a4aSRandall Stewart 	struct sctp_extrcvinfo sinfo;
6695f8829a4aSRandall Stewart 	int filling_sinfo = 1;
669646bf534cSMichael Tuexen 	int flags;
6697f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
6698f8829a4aSRandall Stewart 
6699f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
6700f8829a4aSRandall Stewart 	/* pickup the assoc we are reading from */
6701f8829a4aSRandall Stewart 	if (inp == NULL) {
6702c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6703f8829a4aSRandall Stewart 		return (EINVAL);
6704f8829a4aSRandall Stewart 	}
6705e2e7c62eSMichael Tuexen 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) &&
6706e2e7c62eSMichael Tuexen 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
6707e2e7c62eSMichael Tuexen 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) ||
6708f8829a4aSRandall Stewart 	    (controlp == NULL)) {
6709f8829a4aSRandall Stewart 		/* user does not want the sndrcv ctl */
6710f8829a4aSRandall Stewart 		filling_sinfo = 0;
6711f8829a4aSRandall Stewart 	}
6712f8829a4aSRandall Stewart 	if (psa) {
6713f8829a4aSRandall Stewart 		from = (struct sockaddr *)sockbuf;
6714f8829a4aSRandall Stewart 		fromlen = sizeof(sockbuf);
6715f8829a4aSRandall Stewart 		from->sa_len = 0;
6716f8829a4aSRandall Stewart 	} else {
6717f8829a4aSRandall Stewart 		from = NULL;
6718f8829a4aSRandall Stewart 		fromlen = 0;
6719f8829a4aSRandall Stewart 	}
6720f8829a4aSRandall Stewart 
6721e432298aSXin LI 	if (filling_sinfo) {
6722e432298aSXin LI 		memset(&sinfo, 0, sizeof(struct sctp_extrcvinfo));
6723e432298aSXin LI 	}
672446bf534cSMichael Tuexen 	if (flagsp != NULL) {
672546bf534cSMichael Tuexen 		flags = *flagsp;
672646bf534cSMichael Tuexen 	} else {
672746bf534cSMichael Tuexen 		flags = 0;
672846bf534cSMichael Tuexen 	}
672946bf534cSMichael Tuexen 	error = sctp_sorecvmsg(so, uio, mp0, from, fromlen, &flags,
6730f8829a4aSRandall Stewart 	    (struct sctp_sndrcvinfo *)&sinfo, filling_sinfo);
673146bf534cSMichael Tuexen 	if (flagsp != NULL) {
673246bf534cSMichael Tuexen 		*flagsp = flags;
673346bf534cSMichael Tuexen 	}
6734e432298aSXin LI 	if (controlp != NULL) {
6735f8829a4aSRandall Stewart 		/* copy back the sinfo in a CMSG format */
673646bf534cSMichael Tuexen 		if (filling_sinfo && ((flags & MSG_NOTIFICATION) == 0)) {
6737f8829a4aSRandall Stewart 			*controlp = sctp_build_ctl_nchunk(inp,
6738f8829a4aSRandall Stewart 			    (struct sctp_sndrcvinfo *)&sinfo);
673946bf534cSMichael Tuexen 		} else {
6740f8829a4aSRandall Stewart 			*controlp = NULL;
6741f8829a4aSRandall Stewart 		}
674246bf534cSMichael Tuexen 	}
6743f8829a4aSRandall Stewart 	if (psa) {
6744f8829a4aSRandall Stewart 		/* copy back the address info */
6745f8829a4aSRandall Stewart 		if (from && from->sa_len) {
6746f8829a4aSRandall Stewart 			*psa = sodupsockaddr(from, M_NOWAIT);
6747f8829a4aSRandall Stewart 		} else {
6748f8829a4aSRandall Stewart 			*psa = NULL;
6749f8829a4aSRandall Stewart 		}
6750f8829a4aSRandall Stewart 	}
6751f8829a4aSRandall Stewart 	return (error);
6752f8829a4aSRandall Stewart }
675317205eccSRandall Stewart 
675417205eccSRandall Stewart 
675517205eccSRandall Stewart 
675617205eccSRandall Stewart 
675717205eccSRandall Stewart 
675817205eccSRandall Stewart int
6759d61a0ae0SRandall Stewart sctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr,
6760d61a0ae0SRandall Stewart     int totaddr, int *error)
676117205eccSRandall Stewart {
676217205eccSRandall Stewart 	int added = 0;
676317205eccSRandall Stewart 	int i;
676417205eccSRandall Stewart 	struct sctp_inpcb *inp;
676517205eccSRandall Stewart 	struct sockaddr *sa;
676617205eccSRandall Stewart 	size_t incr = 0;
676792776dfdSMichael Tuexen #ifdef INET
676892776dfdSMichael Tuexen 	struct sockaddr_in *sin;
676992776dfdSMichael Tuexen #endif
677092776dfdSMichael Tuexen #ifdef INET6
677192776dfdSMichael Tuexen 	struct sockaddr_in6 *sin6;
677292776dfdSMichael Tuexen #endif
677392776dfdSMichael Tuexen 
677417205eccSRandall Stewart 	sa = addr;
677517205eccSRandall Stewart 	inp = stcb->sctp_ep;
677617205eccSRandall Stewart 	*error = 0;
677717205eccSRandall Stewart 	for (i = 0; i < totaddr; i++) {
6778ea5eba11SMichael Tuexen 		switch (sa->sa_family) {
6779ea5eba11SMichael Tuexen #ifdef INET
6780ea5eba11SMichael Tuexen 		case AF_INET:
678117205eccSRandall Stewart 			incr = sizeof(struct sockaddr_in);
678292776dfdSMichael Tuexen 			sin = (struct sockaddr_in *)sa;
678392776dfdSMichael Tuexen 			if ((sin->sin_addr.s_addr == INADDR_ANY) ||
678492776dfdSMichael Tuexen 			    (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
678592776dfdSMichael Tuexen 			    IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
678692776dfdSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6787ba785902SMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6788ba785902SMichael Tuexen 				    SCTP_FROM_SCTPUTIL + SCTP_LOC_7);
678992776dfdSMichael Tuexen 				*error = EINVAL;
679092776dfdSMichael Tuexen 				goto out_now;
679192776dfdSMichael Tuexen 			}
67927154bf4aSMichael Tuexen 			if (sctp_add_remote_addr(stcb, sa, NULL, stcb->asoc.port,
67937154bf4aSMichael Tuexen 			    SCTP_DONOT_SETSCOPE,
67947154bf4aSMichael Tuexen 			    SCTP_ADDR_IS_CONFIRMED)) {
679517205eccSRandall Stewart 				/* assoc gone no un-lock */
6796c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6797ba785902SMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6798ba785902SMichael Tuexen 				    SCTP_FROM_SCTPUTIL + SCTP_LOC_8);
679917205eccSRandall Stewart 				*error = ENOBUFS;
680017205eccSRandall Stewart 				goto out_now;
680117205eccSRandall Stewart 			}
680217205eccSRandall Stewart 			added++;
6803ea5eba11SMichael Tuexen 			break;
6804ea5eba11SMichael Tuexen #endif
6805ea5eba11SMichael Tuexen #ifdef INET6
6806ea5eba11SMichael Tuexen 		case AF_INET6:
680717205eccSRandall Stewart 			incr = sizeof(struct sockaddr_in6);
680892776dfdSMichael Tuexen 			sin6 = (struct sockaddr_in6 *)sa;
680992776dfdSMichael Tuexen 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
681092776dfdSMichael Tuexen 			    IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
681192776dfdSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6812ba785902SMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6813ba785902SMichael Tuexen 				    SCTP_FROM_SCTPUTIL + SCTP_LOC_9);
681492776dfdSMichael Tuexen 				*error = EINVAL;
681592776dfdSMichael Tuexen 				goto out_now;
681692776dfdSMichael Tuexen 			}
68177154bf4aSMichael Tuexen 			if (sctp_add_remote_addr(stcb, sa, NULL, stcb->asoc.port,
68187154bf4aSMichael Tuexen 			    SCTP_DONOT_SETSCOPE,
68197154bf4aSMichael Tuexen 			    SCTP_ADDR_IS_CONFIRMED)) {
682017205eccSRandall Stewart 				/* assoc gone no un-lock */
6821c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6822ba785902SMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6823ba785902SMichael Tuexen 				    SCTP_FROM_SCTPUTIL + SCTP_LOC_10);
682417205eccSRandall Stewart 				*error = ENOBUFS;
682517205eccSRandall Stewart 				goto out_now;
682617205eccSRandall Stewart 			}
682717205eccSRandall Stewart 			added++;
6828ea5eba11SMichael Tuexen 			break;
6829ea5eba11SMichael Tuexen #endif
6830ea5eba11SMichael Tuexen 		default:
6831ea5eba11SMichael Tuexen 			break;
683217205eccSRandall Stewart 		}
683317205eccSRandall Stewart 		sa = (struct sockaddr *)((caddr_t)sa + incr);
683417205eccSRandall Stewart 	}
683517205eccSRandall Stewart out_now:
683617205eccSRandall Stewart 	return (added);
683717205eccSRandall Stewart }
683817205eccSRandall Stewart 
6839fc26bf71SMichael Tuexen int
6840d61a0ae0SRandall Stewart sctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr,
6841fc26bf71SMichael Tuexen     unsigned int totaddr,
6842fc26bf71SMichael Tuexen     unsigned int *num_v4, unsigned int *num_v6,
6843fc26bf71SMichael Tuexen     unsigned int limit)
684417205eccSRandall Stewart {
684517205eccSRandall Stewart 	struct sockaddr *sa;
6846fc26bf71SMichael Tuexen 	struct sctp_tcb *stcb;
68479a8e3088SMichael Tuexen 	unsigned int incr, at, i;
684817205eccSRandall Stewart 
6849e1949767SMichael Tuexen 	at = 0;
685017205eccSRandall Stewart 	sa = addr;
6851fc26bf71SMichael Tuexen 	*num_v6 = *num_v4 = 0;
685217205eccSRandall Stewart 	/* account and validate addresses */
6853fc26bf71SMichael Tuexen 	if (totaddr == 0) {
6854fc26bf71SMichael Tuexen 		return (EINVAL);
6855fc26bf71SMichael Tuexen 	}
6856fc26bf71SMichael Tuexen 	for (i = 0; i < totaddr; i++) {
6857fc26bf71SMichael Tuexen 		if (at + sizeof(struct sockaddr) > limit) {
6858fc26bf71SMichael Tuexen 			return (EINVAL);
6859fc26bf71SMichael Tuexen 		}
6860ea5eba11SMichael Tuexen 		switch (sa->sa_family) {
6861ea5eba11SMichael Tuexen #ifdef INET
6862ea5eba11SMichael Tuexen 		case AF_INET:
6863e1949767SMichael Tuexen 			incr = (unsigned int)sizeof(struct sockaddr_in);
6864d61a0ae0SRandall Stewart 			if (sa->sa_len != incr) {
6865fc26bf71SMichael Tuexen 				return (EINVAL);
6866d61a0ae0SRandall Stewart 			}
68679a8e3088SMichael Tuexen 			(*num_v4) += 1;
6868ea5eba11SMichael Tuexen 			break;
6869ea5eba11SMichael Tuexen #endif
6870ea5eba11SMichael Tuexen #ifdef INET6
6871ea5eba11SMichael Tuexen 		case AF_INET6:
6872ea5eba11SMichael Tuexen 			{
687317205eccSRandall Stewart 				struct sockaddr_in6 *sin6;
687417205eccSRandall Stewart 
687517205eccSRandall Stewart 				sin6 = (struct sockaddr_in6 *)sa;
687617205eccSRandall Stewart 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
687717205eccSRandall Stewart 					/* Must be non-mapped for connectx */
6878fc26bf71SMichael Tuexen 					return (EINVAL);
687917205eccSRandall Stewart 				}
6880e1949767SMichael Tuexen 				incr = (unsigned int)sizeof(struct sockaddr_in6);
6881d61a0ae0SRandall Stewart 				if (sa->sa_len != incr) {
6882fc26bf71SMichael Tuexen 					return (EINVAL);
6883d61a0ae0SRandall Stewart 				}
68849a8e3088SMichael Tuexen 				(*num_v6) += 1;
6885ea5eba11SMichael Tuexen 				break;
6886ea5eba11SMichael Tuexen 			}
6887ea5eba11SMichael Tuexen #endif
6888ea5eba11SMichael Tuexen 		default:
6889fc26bf71SMichael Tuexen 			return (EINVAL);
689017205eccSRandall Stewart 		}
6891fc26bf71SMichael Tuexen 		if ((at + incr) > limit) {
6892fc26bf71SMichael Tuexen 			return (EINVAL);
6893ea5eba11SMichael Tuexen 		}
6894d61a0ae0SRandall Stewart 		SCTP_INP_INCR_REF(inp);
689517205eccSRandall Stewart 		stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
689617205eccSRandall Stewart 		if (stcb != NULL) {
6897fc26bf71SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
6898fc26bf71SMichael Tuexen 			return (EALREADY);
6899d61a0ae0SRandall Stewart 		} else {
6900d61a0ae0SRandall Stewart 			SCTP_INP_DECR_REF(inp);
690117205eccSRandall Stewart 		}
6902fc26bf71SMichael Tuexen 		at += incr;
690317205eccSRandall Stewart 		sa = (struct sockaddr *)((caddr_t)sa + incr);
690417205eccSRandall Stewart 	}
6905fc26bf71SMichael Tuexen 	return (0);
690617205eccSRandall Stewart }
690735918f85SRandall Stewart 
690835918f85SRandall Stewart /*
690935918f85SRandall Stewart  * sctp_bindx(ADD) for one address.
691035918f85SRandall Stewart  * assumes all arguments are valid/checked by caller.
691135918f85SRandall Stewart  */
691235918f85SRandall Stewart void
691335918f85SRandall Stewart sctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp,
691435918f85SRandall Stewart     struct sockaddr *sa, sctp_assoc_t assoc_id,
691535918f85SRandall Stewart     uint32_t vrf_id, int *error, void *p)
691635918f85SRandall Stewart {
691735918f85SRandall Stewart 	struct sockaddr *addr_touse;
6918d59107f7SMichael Tuexen #if defined(INET) && defined(INET6)
691935918f85SRandall Stewart 	struct sockaddr_in sin;
69205e2c2d87SRandall Stewart #endif
69215e2c2d87SRandall Stewart 
692235918f85SRandall Stewart 	/* see if we're bound all already! */
692335918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6924c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
692535918f85SRandall Stewart 		*error = EINVAL;
692635918f85SRandall Stewart 		return;
692735918f85SRandall Stewart 	}
692835918f85SRandall Stewart 	addr_touse = sa;
6929ea5eba11SMichael Tuexen #ifdef INET6
693035918f85SRandall Stewart 	if (sa->sa_family == AF_INET6) {
6931d59107f7SMichael Tuexen #ifdef INET
693235918f85SRandall Stewart 		struct sockaddr_in6 *sin6;
693335918f85SRandall Stewart 
6934d59107f7SMichael Tuexen #endif
693535918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6936c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
693735918f85SRandall Stewart 			*error = EINVAL;
693835918f85SRandall Stewart 			return;
693935918f85SRandall Stewart 		}
6940db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6941db4fd95bSRandall Stewart 			/* can only bind v6 on PF_INET6 sockets */
6942c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6943db4fd95bSRandall Stewart 			*error = EINVAL;
6944db4fd95bSRandall Stewart 			return;
6945db4fd95bSRandall Stewart 		}
6946d59107f7SMichael Tuexen #ifdef INET
694735918f85SRandall Stewart 		sin6 = (struct sockaddr_in6 *)addr_touse;
694835918f85SRandall Stewart 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6949db4fd95bSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6950db4fd95bSRandall Stewart 			    SCTP_IPV6_V6ONLY(inp)) {
6951db4fd95bSRandall Stewart 				/* can't bind v4-mapped on PF_INET sockets */
6952c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6953db4fd95bSRandall Stewart 				*error = EINVAL;
6954db4fd95bSRandall Stewart 				return;
6955db4fd95bSRandall Stewart 			}
695635918f85SRandall Stewart 			in6_sin6_2_sin(&sin, sin6);
695735918f85SRandall Stewart 			addr_touse = (struct sockaddr *)&sin;
695835918f85SRandall Stewart 		}
6959d59107f7SMichael Tuexen #endif
696035918f85SRandall Stewart 	}
696135918f85SRandall Stewart #endif
6962ea5eba11SMichael Tuexen #ifdef INET
696335918f85SRandall Stewart 	if (sa->sa_family == AF_INET) {
696435918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in)) {
6965c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
696635918f85SRandall Stewart 			*error = EINVAL;
696735918f85SRandall Stewart 			return;
696835918f85SRandall Stewart 		}
6969db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6970db4fd95bSRandall Stewart 		    SCTP_IPV6_V6ONLY(inp)) {
6971db4fd95bSRandall Stewart 			/* can't bind v4 on PF_INET sockets */
6972c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6973db4fd95bSRandall Stewart 			*error = EINVAL;
6974db4fd95bSRandall Stewart 			return;
6975db4fd95bSRandall Stewart 		}
697635918f85SRandall Stewart 	}
6977ea5eba11SMichael Tuexen #endif
697835918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
697935918f85SRandall Stewart 		if (p == NULL) {
698035918f85SRandall Stewart 			/* Can't get proc for Net/Open BSD */
6981c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
698235918f85SRandall Stewart 			*error = EINVAL;
698335918f85SRandall Stewart 			return;
698435918f85SRandall Stewart 		}
69851b649582SRandall Stewart 		*error = sctp_inpcb_bind(so, addr_touse, NULL, p);
698635918f85SRandall Stewart 		return;
698735918f85SRandall Stewart 	}
698835918f85SRandall Stewart 	/*
698935918f85SRandall Stewart 	 * No locks required here since bind and mgmt_ep_sa all do their own
699035918f85SRandall Stewart 	 * locking. If we do something for the FIX: below we may need to
699135918f85SRandall Stewart 	 * lock in that case.
699235918f85SRandall Stewart 	 */
699335918f85SRandall Stewart 	if (assoc_id == 0) {
699435918f85SRandall Stewart 		/* add the address */
699535918f85SRandall Stewart 		struct sctp_inpcb *lep;
699697c76f10SRandall Stewart 		struct sockaddr_in *lsin = (struct sockaddr_in *)addr_touse;
699735918f85SRandall Stewart 
699897c76f10SRandall Stewart 		/* validate the incoming port */
699997c76f10SRandall Stewart 		if ((lsin->sin_port != 0) &&
700097c76f10SRandall Stewart 		    (lsin->sin_port != inp->sctp_lport)) {
7001c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
700297c76f10SRandall Stewart 			*error = EINVAL;
700397c76f10SRandall Stewart 			return;
700497c76f10SRandall Stewart 		} else {
700597c76f10SRandall Stewart 			/* user specified 0 port, set it to existing port */
700697c76f10SRandall Stewart 			lsin->sin_port = inp->sctp_lport;
700797c76f10SRandall Stewart 		}
700897c76f10SRandall Stewart 
700935918f85SRandall Stewart 		lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id);
701035918f85SRandall Stewart 		if (lep != NULL) {
701135918f85SRandall Stewart 			/*
701235918f85SRandall Stewart 			 * We must decrement the refcount since we have the
701335918f85SRandall Stewart 			 * ep already and are binding. No remove going on
701435918f85SRandall Stewart 			 * here.
701535918f85SRandall Stewart 			 */
70166d9e8f2bSRandall Stewart 			SCTP_INP_DECR_REF(lep);
701735918f85SRandall Stewart 		}
701835918f85SRandall Stewart 		if (lep == inp) {
701935918f85SRandall Stewart 			/* already bound to it.. ok */
702035918f85SRandall Stewart 			return;
702135918f85SRandall Stewart 		} else if (lep == NULL) {
702235918f85SRandall Stewart 			((struct sockaddr_in *)addr_touse)->sin_port = 0;
702335918f85SRandall Stewart 			*error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
702435918f85SRandall Stewart 			    SCTP_ADD_IP_ADDRESS,
702580fefe0aSRandall Stewart 			    vrf_id, NULL);
702635918f85SRandall Stewart 		} else {
702735918f85SRandall Stewart 			*error = EADDRINUSE;
702835918f85SRandall Stewart 		}
702935918f85SRandall Stewart 		if (*error)
703035918f85SRandall Stewart 			return;
703135918f85SRandall Stewart 	} else {
703235918f85SRandall Stewart 		/*
703335918f85SRandall Stewart 		 * FIX: decide whether we allow assoc based bindx
703435918f85SRandall Stewart 		 */
703535918f85SRandall Stewart 	}
703635918f85SRandall Stewart }
703735918f85SRandall Stewart 
703835918f85SRandall Stewart /*
703935918f85SRandall Stewart  * sctp_bindx(DELETE) for one address.
704035918f85SRandall Stewart  * assumes all arguments are valid/checked by caller.
704135918f85SRandall Stewart  */
704235918f85SRandall Stewart void
70437215cc1bSMichael Tuexen sctp_bindx_delete_address(struct sctp_inpcb *inp,
704435918f85SRandall Stewart     struct sockaddr *sa, sctp_assoc_t assoc_id,
704535918f85SRandall Stewart     uint32_t vrf_id, int *error)
704635918f85SRandall Stewart {
704735918f85SRandall Stewart 	struct sockaddr *addr_touse;
7048d59107f7SMichael Tuexen #if defined(INET) && defined(INET6)
704935918f85SRandall Stewart 	struct sockaddr_in sin;
70505e2c2d87SRandall Stewart #endif
70515e2c2d87SRandall Stewart 
705235918f85SRandall Stewart 	/* see if we're bound all already! */
705335918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
7054c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
705535918f85SRandall Stewart 		*error = EINVAL;
705635918f85SRandall Stewart 		return;
705735918f85SRandall Stewart 	}
705835918f85SRandall Stewart 	addr_touse = sa;
7059e0e00a4dSMichael Tuexen #ifdef INET6
706035918f85SRandall Stewart 	if (sa->sa_family == AF_INET6) {
7061d59107f7SMichael Tuexen #ifdef INET
706235918f85SRandall Stewart 		struct sockaddr_in6 *sin6;
7063d59107f7SMichael Tuexen #endif
7064d59107f7SMichael Tuexen 
706535918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in6)) {
7066c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
706735918f85SRandall Stewart 			*error = EINVAL;
706835918f85SRandall Stewart 			return;
706935918f85SRandall Stewart 		}
7070db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
7071db4fd95bSRandall Stewart 			/* can only bind v6 on PF_INET6 sockets */
7072c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7073db4fd95bSRandall Stewart 			*error = EINVAL;
7074db4fd95bSRandall Stewart 			return;
7075db4fd95bSRandall Stewart 		}
7076d59107f7SMichael Tuexen #ifdef INET
707735918f85SRandall Stewart 		sin6 = (struct sockaddr_in6 *)addr_touse;
707835918f85SRandall Stewart 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
7079db4fd95bSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
7080db4fd95bSRandall Stewart 			    SCTP_IPV6_V6ONLY(inp)) {
7081db4fd95bSRandall Stewart 				/* can't bind mapped-v4 on PF_INET sockets */
7082c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7083db4fd95bSRandall Stewart 				*error = EINVAL;
7084db4fd95bSRandall Stewart 				return;
7085db4fd95bSRandall Stewart 			}
708635918f85SRandall Stewart 			in6_sin6_2_sin(&sin, sin6);
708735918f85SRandall Stewart 			addr_touse = (struct sockaddr *)&sin;
708835918f85SRandall Stewart 		}
7089d59107f7SMichael Tuexen #endif
709035918f85SRandall Stewart 	}
709135918f85SRandall Stewart #endif
7092ea5eba11SMichael Tuexen #ifdef INET
709335918f85SRandall Stewart 	if (sa->sa_family == AF_INET) {
709435918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in)) {
7095c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
709635918f85SRandall Stewart 			*error = EINVAL;
709735918f85SRandall Stewart 			return;
709835918f85SRandall Stewart 		}
7099db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
7100db4fd95bSRandall Stewart 		    SCTP_IPV6_V6ONLY(inp)) {
7101db4fd95bSRandall Stewart 			/* can't bind v4 on PF_INET sockets */
7102c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7103db4fd95bSRandall Stewart 			*error = EINVAL;
7104db4fd95bSRandall Stewart 			return;
7105db4fd95bSRandall Stewart 		}
710635918f85SRandall Stewart 	}
7107ea5eba11SMichael Tuexen #endif
710835918f85SRandall Stewart 	/*
710935918f85SRandall Stewart 	 * No lock required mgmt_ep_sa does its own locking. If the FIX:
711035918f85SRandall Stewart 	 * below is ever changed we may need to lock before calling
711135918f85SRandall Stewart 	 * association level binding.
711235918f85SRandall Stewart 	 */
711335918f85SRandall Stewart 	if (assoc_id == 0) {
711435918f85SRandall Stewart 		/* delete the address */
711535918f85SRandall Stewart 		*error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
711635918f85SRandall Stewart 		    SCTP_DEL_IP_ADDRESS,
711780fefe0aSRandall Stewart 		    vrf_id, NULL);
711835918f85SRandall Stewart 	} else {
711935918f85SRandall Stewart 		/*
712035918f85SRandall Stewart 		 * FIX: decide whether we allow assoc based bindx
712135918f85SRandall Stewart 		 */
712235918f85SRandall Stewart 	}
712335918f85SRandall Stewart }
71241b649582SRandall Stewart 
71251b649582SRandall Stewart /*
71261b649582SRandall Stewart  * returns the valid local address count for an assoc, taking into account
71271b649582SRandall Stewart  * all scoping rules
71281b649582SRandall Stewart  */
71291b649582SRandall Stewart int
71301b649582SRandall Stewart sctp_local_addr_count(struct sctp_tcb *stcb)
71311b649582SRandall Stewart {
7132b54ddf22SMichael Tuexen 	int loopback_scope;
7133b54ddf22SMichael Tuexen #if defined(INET)
7134b54ddf22SMichael Tuexen 	int ipv4_local_scope, ipv4_addr_legal;
7135b54ddf22SMichael Tuexen #endif
7136b54ddf22SMichael Tuexen #if defined (INET6)
7137b54ddf22SMichael Tuexen 	int local_scope, site_scope, ipv6_addr_legal;
7138b54ddf22SMichael Tuexen #endif
71391b649582SRandall Stewart 	struct sctp_vrf *vrf;
71401b649582SRandall Stewart 	struct sctp_ifn *sctp_ifn;
71411b649582SRandall Stewart 	struct sctp_ifa *sctp_ifa;
71421b649582SRandall Stewart 	int count = 0;
71431b649582SRandall Stewart 
71441b649582SRandall Stewart 	/* Turn on all the appropriate scopes */
7145a1cb341bSMichael Tuexen 	loopback_scope = stcb->asoc.scope.loopback_scope;
7146b54ddf22SMichael Tuexen #if defined(INET)
7147a1cb341bSMichael Tuexen 	ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
7148b54ddf22SMichael Tuexen 	ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
7149b54ddf22SMichael Tuexen #endif
7150b54ddf22SMichael Tuexen #if defined(INET6)
7151a1cb341bSMichael Tuexen 	local_scope = stcb->asoc.scope.local_scope;
7152a1cb341bSMichael Tuexen 	site_scope = stcb->asoc.scope.site_scope;
7153a1cb341bSMichael Tuexen 	ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
7154b54ddf22SMichael Tuexen #endif
7155c99efcf6SRandall Stewart 	SCTP_IPI_ADDR_RLOCK();
71561b649582SRandall Stewart 	vrf = sctp_find_vrf(stcb->asoc.vrf_id);
71571b649582SRandall Stewart 	if (vrf == NULL) {
71581b649582SRandall Stewart 		/* no vrf, no addresses */
7159c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RUNLOCK();
71601b649582SRandall Stewart 		return (0);
71611b649582SRandall Stewart 	}
71620053ed28SMichael Tuexen 
71631b649582SRandall Stewart 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
71641b649582SRandall Stewart 		/*
71651b649582SRandall Stewart 		 * bound all case: go through all ifns on the vrf
71661b649582SRandall Stewart 		 */
71671b649582SRandall Stewart 		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
71681b649582SRandall Stewart 			if ((loopback_scope == 0) &&
71691b649582SRandall Stewart 			    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
71701b649582SRandall Stewart 				continue;
71711b649582SRandall Stewart 			}
71721b649582SRandall Stewart 			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
71731b649582SRandall Stewart 				if (sctp_is_addr_restricted(stcb, sctp_ifa))
71741b649582SRandall Stewart 					continue;
71755e2c2d87SRandall Stewart 				switch (sctp_ifa->address.sa.sa_family) {
7176ea5eba11SMichael Tuexen #ifdef INET
71775e2c2d87SRandall Stewart 				case AF_INET:
71785e2c2d87SRandall Stewart 					if (ipv4_addr_legal) {
71791b649582SRandall Stewart 						struct sockaddr_in *sin;
71801b649582SRandall Stewart 
718124aaac8dSMichael Tuexen 						sin = &sctp_ifa->address.sin;
71821b649582SRandall Stewart 						if (sin->sin_addr.s_addr == 0) {
7183b7b84c0eSMichael Tuexen 							/*
7184b7b84c0eSMichael Tuexen 							 * skip unspecified
7185b7b84c0eSMichael Tuexen 							 * addrs
7186b7b84c0eSMichael Tuexen 							 */
71871b649582SRandall Stewart 							continue;
71881b649582SRandall Stewart 						}
71896ba22f19SMichael Tuexen 						if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
71906ba22f19SMichael Tuexen 						    &sin->sin_addr) != 0) {
71916ba22f19SMichael Tuexen 							continue;
71926ba22f19SMichael Tuexen 						}
71931b649582SRandall Stewart 						if ((ipv4_local_scope == 0) &&
71941b649582SRandall Stewart 						    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
71951b649582SRandall Stewart 							continue;
71961b649582SRandall Stewart 						}
71971b649582SRandall Stewart 						/* count this one */
71981b649582SRandall Stewart 						count++;
71995e2c2d87SRandall Stewart 					} else {
72005e2c2d87SRandall Stewart 						continue;
72015e2c2d87SRandall Stewart 					}
72025e2c2d87SRandall Stewart 					break;
7203ea5eba11SMichael Tuexen #endif
72045e2c2d87SRandall Stewart #ifdef INET6
72055e2c2d87SRandall Stewart 				case AF_INET6:
72065e2c2d87SRandall Stewart 					if (ipv6_addr_legal) {
72071b649582SRandall Stewart 						struct sockaddr_in6 *sin6;
72081b649582SRandall Stewart 
720924aaac8dSMichael Tuexen 						sin6 = &sctp_ifa->address.sin6;
72101b649582SRandall Stewart 						if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
72111b649582SRandall Stewart 							continue;
72121b649582SRandall Stewart 						}
72136ba22f19SMichael Tuexen 						if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
72146ba22f19SMichael Tuexen 						    &sin6->sin6_addr) != 0) {
72156ba22f19SMichael Tuexen 							continue;
72166ba22f19SMichael Tuexen 						}
72171b649582SRandall Stewart 						if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
72181b649582SRandall Stewart 							if (local_scope == 0)
72191b649582SRandall Stewart 								continue;
72201b649582SRandall Stewart 							if (sin6->sin6_scope_id == 0) {
72211b649582SRandall Stewart 								if (sa6_recoverscope(sin6) != 0)
72221b649582SRandall Stewart 									/*
72235e2c2d87SRandall Stewart 									 *
72245e2c2d87SRandall Stewart 									 * bad
72255b495f17SMichael Tuexen 									 * link
72265e2c2d87SRandall Stewart 									 *
72275b495f17SMichael Tuexen 									 * local
72285e2c2d87SRandall Stewart 									 *
72295b495f17SMichael Tuexen 									 * address
72305b495f17SMichael Tuexen 									 */
72311b649582SRandall Stewart 									continue;
72321b649582SRandall Stewart 							}
72331b649582SRandall Stewart 						}
72341b649582SRandall Stewart 						if ((site_scope == 0) &&
72351b649582SRandall Stewart 						    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
72361b649582SRandall Stewart 							continue;
72371b649582SRandall Stewart 						}
72381b649582SRandall Stewart 						/* count this one */
72391b649582SRandall Stewart 						count++;
72401b649582SRandall Stewart 					}
72415e2c2d87SRandall Stewart 					break;
72425e2c2d87SRandall Stewart #endif
72435e2c2d87SRandall Stewart 				default:
72445e2c2d87SRandall Stewart 					/* TSNH */
72455e2c2d87SRandall Stewart 					break;
72465e2c2d87SRandall Stewart 				}
72471b649582SRandall Stewart 			}
72481b649582SRandall Stewart 		}
72491b649582SRandall Stewart 	} else {
72501b649582SRandall Stewart 		/*
72511b649582SRandall Stewart 		 * subset bound case
72521b649582SRandall Stewart 		 */
72531b649582SRandall Stewart 		struct sctp_laddr *laddr;
72541b649582SRandall Stewart 
72551b649582SRandall Stewart 		LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list,
72561b649582SRandall Stewart 		    sctp_nxt_addr) {
72571b649582SRandall Stewart 			if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
72581b649582SRandall Stewart 				continue;
72591b649582SRandall Stewart 			}
72601b649582SRandall Stewart 			/* count this one */
72611b649582SRandall Stewart 			count++;
72621b649582SRandall Stewart 		}
72631b649582SRandall Stewart 	}
7264c99efcf6SRandall Stewart 	SCTP_IPI_ADDR_RUNLOCK();
72651b649582SRandall Stewart 	return (count);
72661b649582SRandall Stewart }
7267c4739e2fSRandall Stewart 
7268c4739e2fSRandall Stewart #if defined(SCTP_LOCAL_TRACE_BUF)
7269c4739e2fSRandall Stewart 
7270c4739e2fSRandall Stewart void
7271b27a6b7dSRandall Stewart sctp_log_trace(uint32_t subsys, const char *str SCTP_UNUSED, uint32_t a, uint32_t b, uint32_t c, uint32_t d, uint32_t e, uint32_t f)
7272c4739e2fSRandall Stewart {
7273b27a6b7dSRandall Stewart 	uint32_t saveindex, newindex;
7274c4739e2fSRandall Stewart 
7275c4739e2fSRandall Stewart 	do {
7276b3f1ea41SRandall Stewart 		saveindex = SCTP_BASE_SYSCTL(sctp_log).index;
7277c4739e2fSRandall Stewart 		if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
7278c4739e2fSRandall Stewart 			newindex = 1;
7279c4739e2fSRandall Stewart 		} else {
7280c4739e2fSRandall Stewart 			newindex = saveindex + 1;
7281c4739e2fSRandall Stewart 		}
7282b3f1ea41SRandall Stewart 	} while (atomic_cmpset_int(&SCTP_BASE_SYSCTL(sctp_log).index, saveindex, newindex) == 0);
7283c4739e2fSRandall Stewart 	if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
7284c4739e2fSRandall Stewart 		saveindex = 0;
7285c4739e2fSRandall Stewart 	}
7286b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].timestamp = SCTP_GET_CYCLECOUNT;
7287b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].subsys = subsys;
7288b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[0] = a;
7289b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[1] = b;
7290b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[2] = c;
7291b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[3] = d;
7292b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[4] = e;
7293b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[5] = f;
7294c4739e2fSRandall Stewart }
7295c4739e2fSRandall Stewart 
7296c4739e2fSRandall Stewart #endif
7297a99b6783SRandall Stewart static void
72987cca1775SRandall Stewart sctp_recv_udp_tunneled_packet(struct mbuf *m, int off, struct inpcb *inp,
729981d3ec17SBryan Venteicher     const struct sockaddr *sa SCTP_UNUSED, void *ctx SCTP_UNUSED)
7300a99b6783SRandall Stewart {
7301a99b6783SRandall Stewart 	struct ip *iph;
73023a51a264SMichael Tuexen #ifdef INET6
73033a51a264SMichael Tuexen 	struct ip6_hdr *ip6;
73043a51a264SMichael Tuexen #endif
7305a99b6783SRandall Stewart 	struct mbuf *sp, *last;
7306a99b6783SRandall Stewart 	struct udphdr *uhdr;
7307285052f0SMichael Tuexen 	uint16_t port;
7308a99b6783SRandall Stewart 
7309a99b6783SRandall Stewart 	if ((m->m_flags & M_PKTHDR) == 0) {
7310a99b6783SRandall Stewart 		/* Can't handle one that is not a pkt hdr */
7311a99b6783SRandall Stewart 		goto out;
7312a99b6783SRandall Stewart 	}
7313285052f0SMichael Tuexen 	/* Pull the src port */
7314a99b6783SRandall Stewart 	iph = mtod(m, struct ip *);
7315a99b6783SRandall Stewart 	uhdr = (struct udphdr *)((caddr_t)iph + off);
7316a99b6783SRandall Stewart 	port = uhdr->uh_sport;
7317285052f0SMichael Tuexen 	/*
7318285052f0SMichael Tuexen 	 * Split out the mbuf chain. Leave the IP header in m, place the
7319285052f0SMichael Tuexen 	 * rest in the sp.
7320285052f0SMichael Tuexen 	 */
7321eb1b1807SGleb Smirnoff 	sp = m_split(m, off, M_NOWAIT);
7322a99b6783SRandall Stewart 	if (sp == NULL) {
7323a99b6783SRandall Stewart 		/* Gak, drop packet, we can't do a split */
7324a99b6783SRandall Stewart 		goto out;
7325a99b6783SRandall Stewart 	}
7326285052f0SMichael Tuexen 	if (sp->m_pkthdr.len < sizeof(struct udphdr) + sizeof(struct sctphdr)) {
7327285052f0SMichael Tuexen 		/* Gak, packet can't have an SCTP header in it - too small */
7328a99b6783SRandall Stewart 		m_freem(sp);
7329a99b6783SRandall Stewart 		goto out;
7330a99b6783SRandall Stewart 	}
7331285052f0SMichael Tuexen 	/* Now pull up the UDP header and SCTP header together */
7332285052f0SMichael Tuexen 	sp = m_pullup(sp, sizeof(struct udphdr) + sizeof(struct sctphdr));
7333a99b6783SRandall Stewart 	if (sp == NULL) {
7334a99b6783SRandall Stewart 		/* Gak pullup failed */
7335a99b6783SRandall Stewart 		goto out;
7336a99b6783SRandall Stewart 	}
7337285052f0SMichael Tuexen 	/* Trim out the UDP header */
7338a99b6783SRandall Stewart 	m_adj(sp, sizeof(struct udphdr));
7339a99b6783SRandall Stewart 
7340a99b6783SRandall Stewart 	/* Now reconstruct the mbuf chain */
7341285052f0SMichael Tuexen 	for (last = m; last->m_next; last = last->m_next);
7342a99b6783SRandall Stewart 	last->m_next = sp;
7343a99b6783SRandall Stewart 	m->m_pkthdr.len += sp->m_pkthdr.len;
734452f175beSMichael Tuexen 	/*
734552f175beSMichael Tuexen 	 * The CSUM_DATA_VALID flags indicates that the HW checked the UDP
734652f175beSMichael Tuexen 	 * checksum and it was valid. Since CSUM_DATA_VALID ==
734752f175beSMichael Tuexen 	 * CSUM_SCTP_VALID this would imply that the HW also verified the
734852f175beSMichael Tuexen 	 * SCTP checksum. Therefore, clear the bit.
734952f175beSMichael Tuexen 	 */
735052f175beSMichael Tuexen 	SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
735152f175beSMichael Tuexen 	    "sctp_recv_udp_tunneled_packet(): Packet of length %d received on %s with csum_flags 0x%b.\n",
735252f175beSMichael Tuexen 	    m->m_pkthdr.len,
735352f175beSMichael Tuexen 	    if_name(m->m_pkthdr.rcvif),
735452f175beSMichael Tuexen 	    (int)m->m_pkthdr.csum_flags, CSUM_BITS);
735552f175beSMichael Tuexen 	m->m_pkthdr.csum_flags &= ~CSUM_DATA_VALID;
7356a99b6783SRandall Stewart 	iph = mtod(m, struct ip *);
7357a99b6783SRandall Stewart 	switch (iph->ip_v) {
7358e6194c2eSMichael Tuexen #ifdef INET
7359a99b6783SRandall Stewart 	case IPVERSION:
736009c1c856SMichael Tuexen 		iph->ip_len = htons(ntohs(iph->ip_len) - sizeof(struct udphdr));
7361a99b6783SRandall Stewart 		sctp_input_with_port(m, off, port);
7362a99b6783SRandall Stewart 		break;
7363e6194c2eSMichael Tuexen #endif
7364a99b6783SRandall Stewart #ifdef INET6
7365a99b6783SRandall Stewart 	case IPV6_VERSION >> 4:
73663a51a264SMichael Tuexen 		ip6 = mtod(m, struct ip6_hdr *);
73673a51a264SMichael Tuexen 		ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - sizeof(struct udphdr));
73683a51a264SMichael Tuexen 		sctp6_input_with_port(&m, &off, port);
7369a99b6783SRandall Stewart 		break;
7370a99b6783SRandall Stewart #endif
7371a99b6783SRandall Stewart 	default:
7372285052f0SMichael Tuexen 		goto out;
7373a99b6783SRandall Stewart 		break;
7374a99b6783SRandall Stewart 	}
7375a99b6783SRandall Stewart 	return;
7376a99b6783SRandall Stewart out:
7377a99b6783SRandall Stewart 	m_freem(m);
7378a99b6783SRandall Stewart }
7379c54a18d2SRandall Stewart 
7380fd7af143SMichael Tuexen #ifdef INET
7381fd7af143SMichael Tuexen static void
7382fd7af143SMichael Tuexen sctp_recv_icmp_tunneled_packet(int cmd, struct sockaddr *sa, void *vip, void *ctx SCTP_UNUSED)
7383fd7af143SMichael Tuexen {
7384fd7af143SMichael Tuexen 	struct ip *outer_ip, *inner_ip;
7385fd7af143SMichael Tuexen 	struct sctphdr *sh;
7386fd7af143SMichael Tuexen 	struct icmp *icmp;
7387fd7af143SMichael Tuexen 	struct udphdr *udp;
7388fd7af143SMichael Tuexen 	struct sctp_inpcb *inp;
7389fd7af143SMichael Tuexen 	struct sctp_tcb *stcb;
7390fd7af143SMichael Tuexen 	struct sctp_nets *net;
7391fd7af143SMichael Tuexen 	struct sctp_init_chunk *ch;
7392fd7af143SMichael Tuexen 	struct sockaddr_in src, dst;
7393fd7af143SMichael Tuexen 	uint8_t type, code;
7394fd7af143SMichael Tuexen 
7395fd7af143SMichael Tuexen 	inner_ip = (struct ip *)vip;
7396fd7af143SMichael Tuexen 	icmp = (struct icmp *)((caddr_t)inner_ip -
7397fd7af143SMichael Tuexen 	    (sizeof(struct icmp) - sizeof(struct ip)));
7398fd7af143SMichael Tuexen 	outer_ip = (struct ip *)((caddr_t)icmp - sizeof(struct ip));
7399fd7af143SMichael Tuexen 	if (ntohs(outer_ip->ip_len) <
7400fd7af143SMichael Tuexen 	    sizeof(struct ip) + 8 + (inner_ip->ip_hl << 2) + sizeof(struct udphdr) + 8) {
7401fd7af143SMichael Tuexen 		return;
7402fd7af143SMichael Tuexen 	}
7403fd7af143SMichael Tuexen 	udp = (struct udphdr *)((caddr_t)inner_ip + (inner_ip->ip_hl << 2));
7404fd7af143SMichael Tuexen 	sh = (struct sctphdr *)(udp + 1);
7405fd7af143SMichael Tuexen 	memset(&src, 0, sizeof(struct sockaddr_in));
7406fd7af143SMichael Tuexen 	src.sin_family = AF_INET;
7407fd7af143SMichael Tuexen 	src.sin_len = sizeof(struct sockaddr_in);
7408fd7af143SMichael Tuexen 	src.sin_port = sh->src_port;
7409fd7af143SMichael Tuexen 	src.sin_addr = inner_ip->ip_src;
7410fd7af143SMichael Tuexen 	memset(&dst, 0, sizeof(struct sockaddr_in));
7411fd7af143SMichael Tuexen 	dst.sin_family = AF_INET;
7412fd7af143SMichael Tuexen 	dst.sin_len = sizeof(struct sockaddr_in);
7413fd7af143SMichael Tuexen 	dst.sin_port = sh->dest_port;
7414fd7af143SMichael Tuexen 	dst.sin_addr = inner_ip->ip_dst;
7415fd7af143SMichael Tuexen 	/*
7416fd7af143SMichael Tuexen 	 * 'dst' holds the dest of the packet that failed to be sent. 'src'
7417fd7af143SMichael Tuexen 	 * holds our local endpoint address. Thus we reverse the dst and the
7418fd7af143SMichael Tuexen 	 * src in the lookup.
7419fd7af143SMichael Tuexen 	 */
7420fd7af143SMichael Tuexen 	inp = NULL;
7421fd7af143SMichael Tuexen 	net = NULL;
7422fd7af143SMichael Tuexen 	stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
7423fd7af143SMichael Tuexen 	    (struct sockaddr *)&src,
7424fd7af143SMichael Tuexen 	    &inp, &net, 1,
7425fd7af143SMichael Tuexen 	    SCTP_DEFAULT_VRFID);
7426fd7af143SMichael Tuexen 	if ((stcb != NULL) &&
7427fd7af143SMichael Tuexen 	    (net != NULL) &&
742855b8cd93SMichael Tuexen 	    (inp != NULL)) {
7429fd7af143SMichael Tuexen 		/* Check the UDP port numbers */
7430fd7af143SMichael Tuexen 		if ((udp->uh_dport != net->port) ||
7431fd7af143SMichael Tuexen 		    (udp->uh_sport != htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)))) {
7432fd7af143SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
7433fd7af143SMichael Tuexen 			return;
7434fd7af143SMichael Tuexen 		}
7435fd7af143SMichael Tuexen 		/* Check the verification tag */
7436fd7af143SMichael Tuexen 		if (ntohl(sh->v_tag) != 0) {
7437fd7af143SMichael Tuexen 			/*
7438fd7af143SMichael Tuexen 			 * This must be the verification tag used for
7439fd7af143SMichael Tuexen 			 * sending out packets. We don't consider packets
7440fd7af143SMichael Tuexen 			 * reflecting the verification tag.
7441fd7af143SMichael Tuexen 			 */
7442fd7af143SMichael Tuexen 			if (ntohl(sh->v_tag) != stcb->asoc.peer_vtag) {
7443fd7af143SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
7444fd7af143SMichael Tuexen 				return;
7445fd7af143SMichael Tuexen 			}
7446fd7af143SMichael Tuexen 		} else {
7447fd7af143SMichael Tuexen 			if (ntohs(outer_ip->ip_len) >=
7448fd7af143SMichael Tuexen 			    sizeof(struct ip) +
7449fd7af143SMichael Tuexen 			    8 + (inner_ip->ip_hl << 2) + 8 + 20) {
7450fd7af143SMichael Tuexen 				/*
7451fd7af143SMichael Tuexen 				 * In this case we can check if we got an
7452fd7af143SMichael Tuexen 				 * INIT chunk and if the initiate tag
7453fd7af143SMichael Tuexen 				 * matches.
7454fd7af143SMichael Tuexen 				 */
7455fd7af143SMichael Tuexen 				ch = (struct sctp_init_chunk *)(sh + 1);
7456fd7af143SMichael Tuexen 				if ((ch->ch.chunk_type != SCTP_INITIATION) ||
7457fd7af143SMichael Tuexen 				    (ntohl(ch->init.initiate_tag) != stcb->asoc.my_vtag)) {
7458fd7af143SMichael Tuexen 					SCTP_TCB_UNLOCK(stcb);
7459fd7af143SMichael Tuexen 					return;
7460fd7af143SMichael Tuexen 				}
7461fd7af143SMichael Tuexen 			} else {
7462fd7af143SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
7463fd7af143SMichael Tuexen 				return;
7464fd7af143SMichael Tuexen 			}
7465fd7af143SMichael Tuexen 		}
7466fd7af143SMichael Tuexen 		type = icmp->icmp_type;
7467fd7af143SMichael Tuexen 		code = icmp->icmp_code;
74683c3f9e2aSMichael Tuexen 		if ((type == ICMP_UNREACH) &&
74693c3f9e2aSMichael Tuexen 		    (code == ICMP_UNREACH_PORT)) {
7470fd7af143SMichael Tuexen 			code = ICMP_UNREACH_PROTOCOL;
7471fd7af143SMichael Tuexen 		}
7472fd7af143SMichael Tuexen 		sctp_notify(inp, stcb, net, type, code,
7473fd7af143SMichael Tuexen 		    ntohs(inner_ip->ip_len),
74746ebfa5eeSMichael Tuexen 		    (uint32_t)ntohs(icmp->icmp_nextmtu));
7475fd7af143SMichael Tuexen 	} else {
7476fd7af143SMichael Tuexen 		if ((stcb == NULL) && (inp != NULL)) {
7477fd7af143SMichael Tuexen 			/* reduce ref-count */
7478fd7af143SMichael Tuexen 			SCTP_INP_WLOCK(inp);
7479fd7af143SMichael Tuexen 			SCTP_INP_DECR_REF(inp);
7480fd7af143SMichael Tuexen 			SCTP_INP_WUNLOCK(inp);
7481fd7af143SMichael Tuexen 		}
7482fd7af143SMichael Tuexen 		if (stcb) {
7483fd7af143SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
7484fd7af143SMichael Tuexen 		}
7485fd7af143SMichael Tuexen 	}
7486fd7af143SMichael Tuexen 	return;
7487fd7af143SMichael Tuexen }
7488fd7af143SMichael Tuexen #endif
7489fd7af143SMichael Tuexen 
7490fd7af143SMichael Tuexen #ifdef INET6
7491fd7af143SMichael Tuexen static void
7492fd7af143SMichael Tuexen sctp_recv_icmp6_tunneled_packet(int cmd, struct sockaddr *sa, void *d, void *ctx SCTP_UNUSED)
7493fd7af143SMichael Tuexen {
7494fd7af143SMichael Tuexen 	struct ip6ctlparam *ip6cp;
7495fd7af143SMichael Tuexen 	struct sctp_inpcb *inp;
7496fd7af143SMichael Tuexen 	struct sctp_tcb *stcb;
7497fd7af143SMichael Tuexen 	struct sctp_nets *net;
7498fd7af143SMichael Tuexen 	struct sctphdr sh;
7499fd7af143SMichael Tuexen 	struct udphdr udp;
7500fd7af143SMichael Tuexen 	struct sockaddr_in6 src, dst;
7501fd7af143SMichael Tuexen 	uint8_t type, code;
7502fd7af143SMichael Tuexen 
7503fd7af143SMichael Tuexen 	ip6cp = (struct ip6ctlparam *)d;
7504fd7af143SMichael Tuexen 	/*
7505fd7af143SMichael Tuexen 	 * XXX: We assume that when IPV6 is non NULL, M and OFF are valid.
7506fd7af143SMichael Tuexen 	 */
7507fd7af143SMichael Tuexen 	if (ip6cp->ip6c_m == NULL) {
7508fd7af143SMichael Tuexen 		return;
7509fd7af143SMichael Tuexen 	}
7510fd7af143SMichael Tuexen 	/*
7511fd7af143SMichael Tuexen 	 * Check if we can safely examine the ports and the verification tag
7512fd7af143SMichael Tuexen 	 * of the SCTP common header.
7513fd7af143SMichael Tuexen 	 */
7514fd7af143SMichael Tuexen 	if (ip6cp->ip6c_m->m_pkthdr.len <
7515fd7af143SMichael Tuexen 	    ip6cp->ip6c_off + sizeof(struct udphdr) + offsetof(struct sctphdr, checksum)) {
7516fd7af143SMichael Tuexen 		return;
7517fd7af143SMichael Tuexen 	}
7518fd7af143SMichael Tuexen 	/* Copy out the UDP header. */
7519fd7af143SMichael Tuexen 	memset(&udp, 0, sizeof(struct udphdr));
7520fd7af143SMichael Tuexen 	m_copydata(ip6cp->ip6c_m,
7521fd7af143SMichael Tuexen 	    ip6cp->ip6c_off,
7522fd7af143SMichael Tuexen 	    sizeof(struct udphdr),
7523fd7af143SMichael Tuexen 	    (caddr_t)&udp);
7524fd7af143SMichael Tuexen 	/* Copy out the port numbers and the verification tag. */
7525fd7af143SMichael Tuexen 	memset(&sh, 0, sizeof(struct sctphdr));
7526fd7af143SMichael Tuexen 	m_copydata(ip6cp->ip6c_m,
7527fd7af143SMichael Tuexen 	    ip6cp->ip6c_off + sizeof(struct udphdr),
7528fd7af143SMichael Tuexen 	    sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
7529fd7af143SMichael Tuexen 	    (caddr_t)&sh);
7530fd7af143SMichael Tuexen 	memset(&src, 0, sizeof(struct sockaddr_in6));
7531fd7af143SMichael Tuexen 	src.sin6_family = AF_INET6;
7532fd7af143SMichael Tuexen 	src.sin6_len = sizeof(struct sockaddr_in6);
7533fd7af143SMichael Tuexen 	src.sin6_port = sh.src_port;
7534fd7af143SMichael Tuexen 	src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
7535fd7af143SMichael Tuexen 	if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
7536fd7af143SMichael Tuexen 		return;
7537fd7af143SMichael Tuexen 	}
7538fd7af143SMichael Tuexen 	memset(&dst, 0, sizeof(struct sockaddr_in6));
7539fd7af143SMichael Tuexen 	dst.sin6_family = AF_INET6;
7540fd7af143SMichael Tuexen 	dst.sin6_len = sizeof(struct sockaddr_in6);
7541fd7af143SMichael Tuexen 	dst.sin6_port = sh.dest_port;
7542fd7af143SMichael Tuexen 	dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
7543fd7af143SMichael Tuexen 	if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
7544fd7af143SMichael Tuexen 		return;
7545fd7af143SMichael Tuexen 	}
7546fd7af143SMichael Tuexen 	inp = NULL;
7547fd7af143SMichael Tuexen 	net = NULL;
7548fd7af143SMichael Tuexen 	stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
7549fd7af143SMichael Tuexen 	    (struct sockaddr *)&src,
7550fd7af143SMichael Tuexen 	    &inp, &net, 1, SCTP_DEFAULT_VRFID);
7551fd7af143SMichael Tuexen 	if ((stcb != NULL) &&
7552fd7af143SMichael Tuexen 	    (net != NULL) &&
755355b8cd93SMichael Tuexen 	    (inp != NULL)) {
7554fd7af143SMichael Tuexen 		/* Check the UDP port numbers */
7555fd7af143SMichael Tuexen 		if ((udp.uh_dport != net->port) ||
7556fd7af143SMichael Tuexen 		    (udp.uh_sport != htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)))) {
7557fd7af143SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
7558fd7af143SMichael Tuexen 			return;
7559fd7af143SMichael Tuexen 		}
7560fd7af143SMichael Tuexen 		/* Check the verification tag */
7561fd7af143SMichael Tuexen 		if (ntohl(sh.v_tag) != 0) {
7562fd7af143SMichael Tuexen 			/*
7563fd7af143SMichael Tuexen 			 * This must be the verification tag used for
7564fd7af143SMichael Tuexen 			 * sending out packets. We don't consider packets
7565fd7af143SMichael Tuexen 			 * reflecting the verification tag.
7566fd7af143SMichael Tuexen 			 */
7567fd7af143SMichael Tuexen 			if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
7568fd7af143SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
7569fd7af143SMichael Tuexen 				return;
7570fd7af143SMichael Tuexen 			}
7571fd7af143SMichael Tuexen 		} else {
7572fd7af143SMichael Tuexen 			if (ip6cp->ip6c_m->m_pkthdr.len >=
7573fd7af143SMichael Tuexen 			    ip6cp->ip6c_off + sizeof(struct udphdr) +
7574fd7af143SMichael Tuexen 			    sizeof(struct sctphdr) +
7575fd7af143SMichael Tuexen 			    sizeof(struct sctp_chunkhdr) +
7576fd7af143SMichael Tuexen 			    offsetof(struct sctp_init, a_rwnd)) {
7577fd7af143SMichael Tuexen 				/*
7578fd7af143SMichael Tuexen 				 * In this case we can check if we got an
7579fd7af143SMichael Tuexen 				 * INIT chunk and if the initiate tag
7580fd7af143SMichael Tuexen 				 * matches.
7581fd7af143SMichael Tuexen 				 */
7582fd7af143SMichael Tuexen 				uint32_t initiate_tag;
7583fd7af143SMichael Tuexen 				uint8_t chunk_type;
7584fd7af143SMichael Tuexen 
7585fd7af143SMichael Tuexen 				m_copydata(ip6cp->ip6c_m,
7586fd7af143SMichael Tuexen 				    ip6cp->ip6c_off +
7587fd7af143SMichael Tuexen 				    sizeof(struct udphdr) +
7588fd7af143SMichael Tuexen 				    sizeof(struct sctphdr),
7589fd7af143SMichael Tuexen 				    sizeof(uint8_t),
7590fd7af143SMichael Tuexen 				    (caddr_t)&chunk_type);
7591fd7af143SMichael Tuexen 				m_copydata(ip6cp->ip6c_m,
7592fd7af143SMichael Tuexen 				    ip6cp->ip6c_off +
7593fd7af143SMichael Tuexen 				    sizeof(struct udphdr) +
7594fd7af143SMichael Tuexen 				    sizeof(struct sctphdr) +
7595fd7af143SMichael Tuexen 				    sizeof(struct sctp_chunkhdr),
7596fd7af143SMichael Tuexen 				    sizeof(uint32_t),
7597fd7af143SMichael Tuexen 				    (caddr_t)&initiate_tag);
7598fd7af143SMichael Tuexen 				if ((chunk_type != SCTP_INITIATION) ||
7599fd7af143SMichael Tuexen 				    (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
7600fd7af143SMichael Tuexen 					SCTP_TCB_UNLOCK(stcb);
7601fd7af143SMichael Tuexen 					return;
7602fd7af143SMichael Tuexen 				}
7603fd7af143SMichael Tuexen 			} else {
7604fd7af143SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
7605fd7af143SMichael Tuexen 				return;
7606fd7af143SMichael Tuexen 			}
7607fd7af143SMichael Tuexen 		}
7608fd7af143SMichael Tuexen 		type = ip6cp->ip6c_icmp6->icmp6_type;
7609fd7af143SMichael Tuexen 		code = ip6cp->ip6c_icmp6->icmp6_code;
7610fd7af143SMichael Tuexen 		if ((type == ICMP6_DST_UNREACH) &&
7611fd7af143SMichael Tuexen 		    (code == ICMP6_DST_UNREACH_NOPORT)) {
7612fd7af143SMichael Tuexen 			type = ICMP6_PARAM_PROB;
7613fd7af143SMichael Tuexen 			code = ICMP6_PARAMPROB_NEXTHEADER;
7614fd7af143SMichael Tuexen 		}
7615fd7af143SMichael Tuexen 		sctp6_notify(inp, stcb, net, type, code,
76166ebfa5eeSMichael Tuexen 		    ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
7617fd7af143SMichael Tuexen 	} else {
7618fd7af143SMichael Tuexen 		if ((stcb == NULL) && (inp != NULL)) {
7619fd7af143SMichael Tuexen 			/* reduce inp's ref-count */
7620fd7af143SMichael Tuexen 			SCTP_INP_WLOCK(inp);
7621fd7af143SMichael Tuexen 			SCTP_INP_DECR_REF(inp);
7622fd7af143SMichael Tuexen 			SCTP_INP_WUNLOCK(inp);
7623fd7af143SMichael Tuexen 		}
7624fd7af143SMichael Tuexen 		if (stcb) {
7625fd7af143SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
7626fd7af143SMichael Tuexen 		}
7627fd7af143SMichael Tuexen 	}
7628fd7af143SMichael Tuexen }
7629fd7af143SMichael Tuexen #endif
7630fd7af143SMichael Tuexen 
7631c54a18d2SRandall Stewart void
7632c54a18d2SRandall Stewart sctp_over_udp_stop(void)
7633c54a18d2SRandall Stewart {
7634a99b6783SRandall Stewart 	/*
7635a99b6783SRandall Stewart 	 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
7636a99b6783SRandall Stewart 	 * for writting!
7637a99b6783SRandall Stewart 	 */
76383a51a264SMichael Tuexen #ifdef INET
76393a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
76403a51a264SMichael Tuexen 		soclose(SCTP_BASE_INFO(udp4_tun_socket));
76413a51a264SMichael Tuexen 		SCTP_BASE_INFO(udp4_tun_socket) = NULL;
7642c54a18d2SRandall Stewart 	}
76433a51a264SMichael Tuexen #endif
76443a51a264SMichael Tuexen #ifdef INET6
76453a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
76463a51a264SMichael Tuexen 		soclose(SCTP_BASE_INFO(udp6_tun_socket));
76473a51a264SMichael Tuexen 		SCTP_BASE_INFO(udp6_tun_socket) = NULL;
76483a51a264SMichael Tuexen 	}
76493a51a264SMichael Tuexen #endif
7650a99b6783SRandall Stewart }
7651ea5eba11SMichael Tuexen 
7652c54a18d2SRandall Stewart int
7653c54a18d2SRandall Stewart sctp_over_udp_start(void)
7654c54a18d2SRandall Stewart {
7655a99b6783SRandall Stewart 	uint16_t port;
7656a99b6783SRandall Stewart 	int ret;
76573a51a264SMichael Tuexen #ifdef INET
76583a51a264SMichael Tuexen 	struct sockaddr_in sin;
76593a51a264SMichael Tuexen #endif
76603a51a264SMichael Tuexen #ifdef INET6
76613a51a264SMichael Tuexen 	struct sockaddr_in6 sin6;
76623a51a264SMichael Tuexen #endif
7663a99b6783SRandall Stewart 	/*
7664a99b6783SRandall Stewart 	 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
7665a99b6783SRandall Stewart 	 * for writting!
7666a99b6783SRandall Stewart 	 */
7667a99b6783SRandall Stewart 	port = SCTP_BASE_SYSCTL(sctp_udp_tunneling_port);
76683a51a264SMichael Tuexen 	if (ntohs(port) == 0) {
7669a99b6783SRandall Stewart 		/* Must have a port set */
7670a99b6783SRandall Stewart 		return (EINVAL);
7671a99b6783SRandall Stewart 	}
76723a51a264SMichael Tuexen #ifdef INET
76733a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
7674a99b6783SRandall Stewart 		/* Already running -- must stop first */
7675a99b6783SRandall Stewart 		return (EALREADY);
7676a99b6783SRandall Stewart 	}
76773a51a264SMichael Tuexen #endif
76783a51a264SMichael Tuexen #ifdef INET6
76793a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
76803a51a264SMichael Tuexen 		/* Already running -- must stop first */
76813a51a264SMichael Tuexen 		return (EALREADY);
7682a99b6783SRandall Stewart 	}
76833a51a264SMichael Tuexen #endif
76843a51a264SMichael Tuexen #ifdef INET
76853a51a264SMichael Tuexen 	if ((ret = socreate(PF_INET, &SCTP_BASE_INFO(udp4_tun_socket),
76863a51a264SMichael Tuexen 	    SOCK_DGRAM, IPPROTO_UDP,
76873a51a264SMichael Tuexen 	    curthread->td_ucred, curthread))) {
7688a99b6783SRandall Stewart 		sctp_over_udp_stop();
7689a99b6783SRandall Stewart 		return (ret);
7690a99b6783SRandall Stewart 	}
76913a51a264SMichael Tuexen 	/* Call the special UDP hook. */
76923a51a264SMichael Tuexen 	if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp4_tun_socket),
7693fd7af143SMichael Tuexen 	    sctp_recv_udp_tunneled_packet,
7694fd7af143SMichael Tuexen 	    sctp_recv_icmp_tunneled_packet,
7695fd7af143SMichael Tuexen 	    NULL))) {
76963a51a264SMichael Tuexen 		sctp_over_udp_stop();
76973a51a264SMichael Tuexen 		return (ret);
76983a51a264SMichael Tuexen 	}
76993a51a264SMichael Tuexen 	/* Ok, we have a socket, bind it to the port. */
77003a51a264SMichael Tuexen 	memset(&sin, 0, sizeof(struct sockaddr_in));
77013a51a264SMichael Tuexen 	sin.sin_len = sizeof(struct sockaddr_in);
77023a51a264SMichael Tuexen 	sin.sin_family = AF_INET;
77033a51a264SMichael Tuexen 	sin.sin_port = htons(port);
77043a51a264SMichael Tuexen 	if ((ret = sobind(SCTP_BASE_INFO(udp4_tun_socket),
77053a51a264SMichael Tuexen 	    (struct sockaddr *)&sin, curthread))) {
77063a51a264SMichael Tuexen 		sctp_over_udp_stop();
77073a51a264SMichael Tuexen 		return (ret);
77083a51a264SMichael Tuexen 	}
77093a51a264SMichael Tuexen #endif
77103a51a264SMichael Tuexen #ifdef INET6
77113a51a264SMichael Tuexen 	if ((ret = socreate(PF_INET6, &SCTP_BASE_INFO(udp6_tun_socket),
77123a51a264SMichael Tuexen 	    SOCK_DGRAM, IPPROTO_UDP,
77133a51a264SMichael Tuexen 	    curthread->td_ucred, curthread))) {
77143a51a264SMichael Tuexen 		sctp_over_udp_stop();
77153a51a264SMichael Tuexen 		return (ret);
77163a51a264SMichael Tuexen 	}
77173a51a264SMichael Tuexen 	/* Call the special UDP hook. */
77183a51a264SMichael Tuexen 	if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp6_tun_socket),
7719fd7af143SMichael Tuexen 	    sctp_recv_udp_tunneled_packet,
7720fd7af143SMichael Tuexen 	    sctp_recv_icmp6_tunneled_packet,
7721fd7af143SMichael Tuexen 	    NULL))) {
77223a51a264SMichael Tuexen 		sctp_over_udp_stop();
77233a51a264SMichael Tuexen 		return (ret);
77243a51a264SMichael Tuexen 	}
77253a51a264SMichael Tuexen 	/* Ok, we have a socket, bind it to the port. */
77263a51a264SMichael Tuexen 	memset(&sin6, 0, sizeof(struct sockaddr_in6));
77273a51a264SMichael Tuexen 	sin6.sin6_len = sizeof(struct sockaddr_in6);
77283a51a264SMichael Tuexen 	sin6.sin6_family = AF_INET6;
77293a51a264SMichael Tuexen 	sin6.sin6_port = htons(port);
77303a51a264SMichael Tuexen 	if ((ret = sobind(SCTP_BASE_INFO(udp6_tun_socket),
77313a51a264SMichael Tuexen 	    (struct sockaddr *)&sin6, curthread))) {
77323a51a264SMichael Tuexen 		sctp_over_udp_stop();
77333a51a264SMichael Tuexen 		return (ret);
77343a51a264SMichael Tuexen 	}
77353a51a264SMichael Tuexen #endif
7736a99b6783SRandall Stewart 	return (0);
7737c54a18d2SRandall Stewart }
773810e0318aSMichael Tuexen 
773910e0318aSMichael Tuexen /*
774010e0318aSMichael Tuexen  * sctp_min_mtu ()returns the minimum of all non-zero arguments.
774110e0318aSMichael Tuexen  * If all arguments are zero, zero is returned.
774210e0318aSMichael Tuexen  */
774310e0318aSMichael Tuexen uint32_t
7744b0471b4bSMichael Tuexen sctp_min_mtu(uint32_t mtu1, uint32_t mtu2, uint32_t mtu3)
7745b0471b4bSMichael Tuexen {
774610e0318aSMichael Tuexen 	if (mtu1 > 0) {
774710e0318aSMichael Tuexen 		if (mtu2 > 0) {
774810e0318aSMichael Tuexen 			if (mtu3 > 0) {
774910e0318aSMichael Tuexen 				return (min(mtu1, min(mtu2, mtu3)));
775010e0318aSMichael Tuexen 			} else {
775110e0318aSMichael Tuexen 				return (min(mtu1, mtu2));
775210e0318aSMichael Tuexen 			}
775310e0318aSMichael Tuexen 		} else {
775410e0318aSMichael Tuexen 			if (mtu3 > 0) {
775510e0318aSMichael Tuexen 				return (min(mtu1, mtu3));
775610e0318aSMichael Tuexen 			} else {
775710e0318aSMichael Tuexen 				return (mtu1);
775810e0318aSMichael Tuexen 			}
775910e0318aSMichael Tuexen 		}
776010e0318aSMichael Tuexen 	} else {
776110e0318aSMichael Tuexen 		if (mtu2 > 0) {
776210e0318aSMichael Tuexen 			if (mtu3 > 0) {
776310e0318aSMichael Tuexen 				return (min(mtu2, mtu3));
776410e0318aSMichael Tuexen 			} else {
776510e0318aSMichael Tuexen 				return (mtu2);
776610e0318aSMichael Tuexen 			}
776710e0318aSMichael Tuexen 		} else {
776810e0318aSMichael Tuexen 			return (mtu3);
776910e0318aSMichael Tuexen 		}
777010e0318aSMichael Tuexen 	}
777110e0318aSMichael Tuexen }
777210e0318aSMichael Tuexen 
777310e0318aSMichael Tuexen void
777410e0318aSMichael Tuexen sctp_hc_set_mtu(union sctp_sockstore *addr, uint16_t fibnum, uint32_t mtu)
777510e0318aSMichael Tuexen {
777610e0318aSMichael Tuexen 	struct in_conninfo inc;
777710e0318aSMichael Tuexen 
777810e0318aSMichael Tuexen 	memset(&inc, 0, sizeof(struct in_conninfo));
777910e0318aSMichael Tuexen 	inc.inc_fibnum = fibnum;
778010e0318aSMichael Tuexen 	switch (addr->sa.sa_family) {
778110e0318aSMichael Tuexen #ifdef INET
778210e0318aSMichael Tuexen 	case AF_INET:
778310e0318aSMichael Tuexen 		inc.inc_faddr = addr->sin.sin_addr;
778410e0318aSMichael Tuexen 		break;
778510e0318aSMichael Tuexen #endif
778610e0318aSMichael Tuexen #ifdef INET6
778710e0318aSMichael Tuexen 	case AF_INET6:
778810e0318aSMichael Tuexen 		inc.inc_flags |= INC_ISIPV6;
778910e0318aSMichael Tuexen 		inc.inc6_faddr = addr->sin6.sin6_addr;
779010e0318aSMichael Tuexen 		break;
779110e0318aSMichael Tuexen #endif
779210e0318aSMichael Tuexen 	default:
779310e0318aSMichael Tuexen 		return;
779410e0318aSMichael Tuexen 	}
779510e0318aSMichael Tuexen 	tcp_hc_updatemtu(&inc, (u_long)mtu);
779610e0318aSMichael Tuexen }
779710e0318aSMichael Tuexen 
779810e0318aSMichael Tuexen uint32_t
7799b0471b4bSMichael Tuexen sctp_hc_get_mtu(union sctp_sockstore *addr, uint16_t fibnum)
7800b0471b4bSMichael Tuexen {
780110e0318aSMichael Tuexen 	struct in_conninfo inc;
780210e0318aSMichael Tuexen 
780310e0318aSMichael Tuexen 	memset(&inc, 0, sizeof(struct in_conninfo));
780410e0318aSMichael Tuexen 	inc.inc_fibnum = fibnum;
780510e0318aSMichael Tuexen 	switch (addr->sa.sa_family) {
780610e0318aSMichael Tuexen #ifdef INET
780710e0318aSMichael Tuexen 	case AF_INET:
780810e0318aSMichael Tuexen 		inc.inc_faddr = addr->sin.sin_addr;
780910e0318aSMichael Tuexen 		break;
781010e0318aSMichael Tuexen #endif
781110e0318aSMichael Tuexen #ifdef INET6
781210e0318aSMichael Tuexen 	case AF_INET6:
781310e0318aSMichael Tuexen 		inc.inc_flags |= INC_ISIPV6;
781410e0318aSMichael Tuexen 		inc.inc6_faddr = addr->sin6.sin6_addr;
781510e0318aSMichael Tuexen 		break;
781610e0318aSMichael Tuexen #endif
781710e0318aSMichael Tuexen 	default:
781810e0318aSMichael Tuexen 		return (0);
781910e0318aSMichael Tuexen 	}
782010e0318aSMichael Tuexen 	return ((uint32_t)tcp_hc_getmtu(&inc));
782110e0318aSMichael Tuexen }
78226ef849e6SMichael Tuexen 
78231a0b0216SMichael Tuexen void
78241a0b0216SMichael Tuexen sctp_set_state(struct sctp_tcb *stcb, int new_state)
78251a0b0216SMichael Tuexen {
78261e88cc8bSMichael Tuexen #if defined(KDTRACE_HOOKS)
78271e88cc8bSMichael Tuexen 	int old_state = stcb->asoc.state;
78281e88cc8bSMichael Tuexen #endif
78291e88cc8bSMichael Tuexen 
78301a0b0216SMichael Tuexen 	KASSERT((new_state & ~SCTP_STATE_MASK) == 0,
78311a0b0216SMichael Tuexen 	    ("sctp_set_state: Can't set substate (new_state = %x)",
78321a0b0216SMichael Tuexen 	    new_state));
78331a0b0216SMichael Tuexen 	stcb->asoc.state = (stcb->asoc.state & ~SCTP_STATE_MASK) | new_state;
78341a0b0216SMichael Tuexen 	if ((new_state == SCTP_STATE_SHUTDOWN_RECEIVED) ||
78351a0b0216SMichael Tuexen 	    (new_state == SCTP_STATE_SHUTDOWN_SENT) ||
78361a0b0216SMichael Tuexen 	    (new_state == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
78371a0b0216SMichael Tuexen 		SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
78381a0b0216SMichael Tuexen 	}
78391e88cc8bSMichael Tuexen #if defined(KDTRACE_HOOKS)
78401e88cc8bSMichael Tuexen 	if (((old_state & SCTP_STATE_MASK) != new_state) &&
78411e88cc8bSMichael Tuexen 	    !(((old_state & SCTP_STATE_MASK) == SCTP_STATE_EMPTY) &&
78421e88cc8bSMichael Tuexen 	    (new_state == SCTP_STATE_INUSE))) {
78431e88cc8bSMichael Tuexen 		SCTP_PROBE6(state__change, NULL, stcb, NULL, stcb, NULL, old_state);
78441e88cc8bSMichael Tuexen 	}
78451e88cc8bSMichael Tuexen #endif
78461a0b0216SMichael Tuexen }
78471a0b0216SMichael Tuexen 
78481a0b0216SMichael Tuexen void
78491a0b0216SMichael Tuexen sctp_add_substate(struct sctp_tcb *stcb, int substate)
78501a0b0216SMichael Tuexen {
78511e88cc8bSMichael Tuexen #if defined(KDTRACE_HOOKS)
78521e88cc8bSMichael Tuexen 	int old_state = stcb->asoc.state;
78531e88cc8bSMichael Tuexen #endif
78541e88cc8bSMichael Tuexen 
78551a0b0216SMichael Tuexen 	KASSERT((substate & SCTP_STATE_MASK) == 0,
78561a0b0216SMichael Tuexen 	    ("sctp_add_substate: Can't set state (substate = %x)",
78571a0b0216SMichael Tuexen 	    substate));
78581a0b0216SMichael Tuexen 	stcb->asoc.state |= substate;
78591e88cc8bSMichael Tuexen #if defined(KDTRACE_HOOKS)
78601e88cc8bSMichael Tuexen 	if (((substate & SCTP_STATE_ABOUT_TO_BE_FREED) &&
78611e88cc8bSMichael Tuexen 	    ((old_state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) ||
78621e88cc8bSMichael Tuexen 	    ((substate & SCTP_STATE_SHUTDOWN_PENDING) &&
78631e88cc8bSMichael Tuexen 	    ((old_state & SCTP_STATE_SHUTDOWN_PENDING) == 0))) {
78641e88cc8bSMichael Tuexen 		SCTP_PROBE6(state__change, NULL, stcb, NULL, stcb, NULL, old_state);
78651e88cc8bSMichael Tuexen 	}
78661e88cc8bSMichael Tuexen #endif
78671a0b0216SMichael Tuexen }
7868