xref: /freebsd/sys/netinet/sctputil.c (revision f4cb790a35be65c61ed19e5c69a81f8e19c22a1f)
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... */
1733*f4cb790aSMichael Tuexen 	KASSERT(tmr->self == tmr,
1734*f4cb790aSMichael Tuexen 	    ("sctp_timeout_handler: tmr->self corrupted"));
1735*f4cb790aSMichael Tuexen 	KASSERT(SCTP_IS_TIMER_TYPE_VALID(tmr->type),
1736*f4cb790aSMichael Tuexen 	    ("sctp_timeout_handler: invalid timer type %d", tmr->type));
1737a412576eSMichael Tuexen 	type = tmr->type;
1738*f4cb790aSMichael Tuexen 	KASSERT(stcb == NULL || stcb->sctp_ep == inp,
1739*f4cb790aSMichael Tuexen 	    ("sctp_timeout_handler of type %d: inp = %p, stcb->sctp_ep %p",
1740*f4cb790aSMichael 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 			/*
1844f8829a4aSRandall Stewart 			 * 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 			 */
1849f8829a4aSRandall Stewart 			chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
1850f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb,
1851f8829a4aSRandall Stewart 			    chk->whoTo);
1852f8829a4aSRandall Stewart 		}
1853f8829a4aSRandall Stewart 		break;
1854f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
1855a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1856a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1857a412576eSMichael Tuexen 		    type, inp, stcb, net));
1858f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoinit);
1859f42a358aSRandall Stewart 		stcb->asoc.timoinit++;
1860f8829a4aSRandall Stewart 		if (sctp_t1init_timer(inp, stcb, net)) {
1861f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1862f8829a4aSRandall Stewart 			goto out_decr;
1863f8829a4aSRandall Stewart 		}
1864f8829a4aSRandall Stewart 		/* We do output but not here */
1865f8829a4aSRandall Stewart 		did_output = 0;
1866f8829a4aSRandall Stewart 		break;
1867f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
1868a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net == NULL,
1869a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1870a412576eSMichael Tuexen 		    type, inp, stcb, net));
1871f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timosack);
1872f42a358aSRandall Stewart 		stcb->asoc.timosack++;
1873689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
1874f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1875a412576eSMichael Tuexen 		sctp_auditing(4, inp, stcb, NULL);
1876f8829a4aSRandall Stewart #endif
1877ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SACK_TMR, SCTP_SO_NOT_LOCKED);
1878f8829a4aSRandall Stewart 		break;
1879f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
1880a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1881a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1882a412576eSMichael Tuexen 		    type, inp, stcb, net));
1883a412576eSMichael Tuexen 		SCTP_STAT_INCR(sctps_timoshutdown);
1884a412576eSMichael Tuexen 		stcb->asoc.timoshutdown++;
1885f8829a4aSRandall Stewart 		if (sctp_shutdown_timer(inp, stcb, net)) {
1886f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1887f8829a4aSRandall Stewart 			goto out_decr;
1888f8829a4aSRandall Stewart 		}
1889f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1890f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1891f8829a4aSRandall Stewart #endif
1892ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_NOT_LOCKED);
1893f8829a4aSRandall Stewart 		break;
1894f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
1895a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1896a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1897a412576eSMichael Tuexen 		    type, inp, stcb, net));
1898f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoheartbeat);
1899f42a358aSRandall Stewart 		stcb->asoc.timoheartbeat++;
1900ca85e948SMichael Tuexen 		if (sctp_heartbeat_timer(inp, stcb, net)) {
1901f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1902f8829a4aSRandall Stewart 			goto out_decr;
1903f8829a4aSRandall Stewart 		}
1904f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1905ca85e948SMichael Tuexen 		sctp_auditing(4, inp, stcb, net);
1906f8829a4aSRandall Stewart #endif
1907ca85e948SMichael Tuexen 		if (!(net->dest_state & SCTP_ADDR_NOHB)) {
1908629749b6SMichael Tuexen 			sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1909ceaad40aSRandall Stewart 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_HB_TMR, SCTP_SO_NOT_LOCKED);
1910f8829a4aSRandall Stewart 		}
1911f8829a4aSRandall Stewart 		break;
1912f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
1913a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1914a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1915a412576eSMichael Tuexen 		    type, inp, stcb, net));
1916a412576eSMichael Tuexen 		SCTP_STAT_INCR(sctps_timocookie);
1917a412576eSMichael Tuexen 		stcb->asoc.timocookie++;
1918f8829a4aSRandall Stewart 		if (sctp_cookie_timer(inp, stcb, net)) {
1919f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1920f8829a4aSRandall Stewart 			goto out_decr;
1921f8829a4aSRandall Stewart 		}
1922f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1923f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1924f8829a4aSRandall Stewart #endif
1925f8829a4aSRandall Stewart 		/*
1926f8829a4aSRandall Stewart 		 * We consider T3 and Cookie timer pretty much the same with
1927f8829a4aSRandall Stewart 		 * respect to where from in chunk_output.
1928f8829a4aSRandall Stewart 		 */
1929ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1930f8829a4aSRandall Stewart 		break;
1931f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_NEWCOOKIE:
1932a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb == NULL && net == NULL,
1933a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1934a412576eSMichael Tuexen 		    type, inp, stcb, net));
1935f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timosecret);
19366e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&tv);
1937f8829a4aSRandall Stewart 		inp->sctp_ep.time_of_secret_change = tv.tv_sec;
1938f8829a4aSRandall Stewart 		inp->sctp_ep.last_secret_number =
1939f8829a4aSRandall Stewart 		    inp->sctp_ep.current_secret_number;
1940f8829a4aSRandall Stewart 		inp->sctp_ep.current_secret_number++;
1941f8829a4aSRandall Stewart 		if (inp->sctp_ep.current_secret_number >=
1942f8829a4aSRandall Stewart 		    SCTP_HOW_MANY_SECRETS) {
1943f8829a4aSRandall Stewart 			inp->sctp_ep.current_secret_number = 0;
1944f8829a4aSRandall Stewart 		}
1945f8829a4aSRandall Stewart 		secret = (int)inp->sctp_ep.current_secret_number;
1946f8829a4aSRandall Stewart 		for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1947f8829a4aSRandall Stewart 			inp->sctp_ep.secret_key[secret][i] =
1948f8829a4aSRandall Stewart 			    sctp_select_initial_TSN(&inp->sctp_ep);
1949f8829a4aSRandall Stewart 		}
19506fb7b4fbSMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
1951f8829a4aSRandall Stewart 		did_output = 0;
1952f8829a4aSRandall Stewart 		break;
1953f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
1954a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1955a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1956a412576eSMichael Tuexen 		    type, inp, stcb, net));
1957f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timopathmtu);
1958f8829a4aSRandall Stewart 		sctp_pathmtu_timer(inp, stcb, net);
1959f8829a4aSRandall Stewart 		did_output = 0;
1960f8829a4aSRandall Stewart 		break;
1961f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
1962a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1963a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1964a412576eSMichael Tuexen 		    type, inp, stcb, net));
1965f8829a4aSRandall Stewart 		if (sctp_shutdownack_timer(inp, stcb, net)) {
1966f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1967f8829a4aSRandall Stewart 			goto out_decr;
1968f8829a4aSRandall Stewart 		}
1969f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoshutdownack);
1970f42a358aSRandall Stewart 		stcb->asoc.timoshutdownack++;
1971f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1972f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1973f8829a4aSRandall Stewart #endif
1974ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_ACK_TMR, SCTP_SO_NOT_LOCKED);
1975f8829a4aSRandall Stewart 		break;
1976f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASCONF:
1977a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1978a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1979a412576eSMichael Tuexen 		    type, inp, stcb, net));
1980a412576eSMichael Tuexen 		SCTP_STAT_INCR(sctps_timoasconf);
1981f8829a4aSRandall Stewart 		if (sctp_asconf_timer(inp, stcb, net)) {
1982f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1983f8829a4aSRandall Stewart 			goto out_decr;
1984f8829a4aSRandall Stewart 		}
1985f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1986f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1987f8829a4aSRandall Stewart #endif
1988ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_ASCONF_TMR, SCTP_SO_NOT_LOCKED);
1989f8829a4aSRandall Stewart 		break;
19900554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
1991a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net == NULL,
1992a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1993a412576eSMichael Tuexen 		    type, inp, stcb, net));
19940554e01dSMichael Tuexen 		SCTP_STAT_INCR(sctps_timoshutdownguard);
19950554e01dSMichael Tuexen 		op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
19960554e01dSMichael Tuexen 		    "Shutdown guard timer expired");
19970554e01dSMichael Tuexen 		sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
19980554e01dSMichael Tuexen 		/* no need to unlock on tcb its gone */
19990554e01dSMichael Tuexen 		goto out_decr;
2000f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_AUTOCLOSE:
2001a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net == NULL,
2002a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2003a412576eSMichael Tuexen 		    type, inp, stcb, net));
2004f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoautoclose);
2005a57fb68bSMichael Tuexen 		sctp_autoclose_timer(inp, stcb);
2006ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
2007f8829a4aSRandall Stewart 		did_output = 0;
2008f8829a4aSRandall Stewart 		break;
20090554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_STRRESET:
2010a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net == NULL,
2011a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2012a412576eSMichael Tuexen 		    type, inp, stcb, net));
2013a412576eSMichael Tuexen 		SCTP_STAT_INCR(sctps_timostrmrst);
2014e95b3d7fSMichael Tuexen 		if (sctp_strreset_timer(inp, stcb)) {
20150554e01dSMichael Tuexen 			/* no need to unlock on tcb its gone */
20160554e01dSMichael Tuexen 			goto out_decr;
20170554e01dSMichael Tuexen 		}
20180554e01dSMichael Tuexen 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_TMR, SCTP_SO_NOT_LOCKED);
20190554e01dSMichael Tuexen 		break;
20200554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_INPKILL:
2021a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb == NULL && net == NULL,
2022a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2023a412576eSMichael Tuexen 		    type, inp, stcb, net));
20240554e01dSMichael Tuexen 		SCTP_STAT_INCR(sctps_timoinpkill);
20250554e01dSMichael Tuexen 		/*
20260554e01dSMichael Tuexen 		 * special case, take away our increment since WE are the
20270554e01dSMichael Tuexen 		 * killer
20280554e01dSMichael Tuexen 		 */
20290554e01dSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL,
20300554e01dSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_3);
2031a412576eSMichael Tuexen 		SCTP_INP_DECR_REF(inp);
2032a412576eSMichael Tuexen 		SCTP_INP_WUNLOCK(inp);
20330554e01dSMichael Tuexen 		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
20340554e01dSMichael Tuexen 		    SCTP_CALLED_FROM_INPKILL_TIMER);
20350554e01dSMichael Tuexen 		inp = NULL;
20360554e01dSMichael Tuexen 		goto out_no_decr;
2037f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASOCKILL:
2038a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net == NULL,
2039a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2040a412576eSMichael Tuexen 		    type, inp, stcb, net));
2041f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoassockill);
2042f8829a4aSRandall Stewart 		/* Can we free it yet? */
2043f8829a4aSRandall Stewart 		SCTP_INP_DECR_REF(inp);
2044ba785902SMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL,
2045ba785902SMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_1);
2046ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2047ceaad40aSRandall Stewart 		so = SCTP_INP_SO(inp);
2048ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
2049ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
2050ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
2051ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
2052ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
2053ceaad40aSRandall Stewart #endif
2054ba785902SMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2055ba785902SMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_2);
2056ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2057ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
2058ceaad40aSRandall Stewart #endif
2059f8829a4aSRandall Stewart 		/*
2060f8829a4aSRandall Stewart 		 * free asoc, always unlocks (or destroy's) so prevent
2061f8829a4aSRandall Stewart 		 * duplicate unlock or unlock of a free mtx :-0
2062f8829a4aSRandall Stewart 		 */
2063f8829a4aSRandall Stewart 		stcb = NULL;
2064f8829a4aSRandall Stewart 		goto out_no_decr;
20650554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ADDR_WQ:
2066a412576eSMichael Tuexen 		KASSERT(inp == NULL && stcb == NULL && net == NULL,
2067a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2068a412576eSMichael Tuexen 		    type, inp, stcb, net));
20690554e01dSMichael Tuexen 		sctp_handle_addr_wq();
20700554e01dSMichael Tuexen 		break;
20710554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_PRIM_DELETED:
2072a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net == NULL,
2073a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2074a412576eSMichael Tuexen 		    type, inp, stcb, net));
20750554e01dSMichael Tuexen 		SCTP_STAT_INCR(sctps_timodelprim);
2076a412576eSMichael Tuexen 		sctp_delete_prim_timer(inp, stcb);
20770554e01dSMichael Tuexen 		break;
2078f8829a4aSRandall Stewart 	default:
20797522682eSMichael Tuexen #ifdef INVARIANTS
2080a412576eSMichael Tuexen 		panic("Unknown timer type %d", type);
20817522682eSMichael Tuexen #else
20827522682eSMichael Tuexen 		goto get_out;
20837522682eSMichael Tuexen #endif
208460990c0cSMichael Tuexen 	}
2085f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
2086fa89f692SMichael Tuexen 	sctp_audit_log(0xF1, (uint8_t)type);
2087f8829a4aSRandall Stewart 	if (inp)
2088f8829a4aSRandall Stewart 		sctp_auditing(5, inp, stcb, net);
2089f8829a4aSRandall Stewart #endif
2090f8829a4aSRandall Stewart 	if ((did_output) && stcb) {
2091f8829a4aSRandall Stewart 		/*
2092f8829a4aSRandall Stewart 		 * Now we need to clean up the control chunk chain if an
2093f8829a4aSRandall Stewart 		 * ECNE is on it. It must be marked as UNSENT again so next
2094f8829a4aSRandall Stewart 		 * call will continue to send it until such time that we get
2095f8829a4aSRandall Stewart 		 * a CWR, to remove it. It is, however, less likely that we
2096f8829a4aSRandall Stewart 		 * will find a ecn echo on the chain though.
2097f8829a4aSRandall Stewart 		 */
2098f8829a4aSRandall Stewart 		sctp_fix_ecn_echo(&stcb->asoc);
2099f8829a4aSRandall Stewart 	}
210044b7479bSRandall Stewart get_out:
2101f8829a4aSRandall Stewart 	if (stcb) {
2102f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
21032c62ba73SMichael Tuexen 	} else if (inp != NULL) {
21042c62ba73SMichael Tuexen 		SCTP_INP_WUNLOCK(inp);
21052c62ba73SMichael Tuexen 	} else {
21062c62ba73SMichael Tuexen 		SCTP_WQ_ADDR_UNLOCK();
2107f8829a4aSRandall Stewart 	}
21082c62ba73SMichael Tuexen 
2109f8829a4aSRandall Stewart out_decr:
2110f8829a4aSRandall Stewart 	if (inp) {
2111f8829a4aSRandall Stewart 		SCTP_INP_DECR_REF(inp);
2112f8829a4aSRandall Stewart 	}
21130053ed28SMichael Tuexen 
2114f8829a4aSRandall Stewart out_no_decr:
211523e3c088SMichael Tuexen 	SCTPDBG(SCTP_DEBUG_TIMER2, "Timer type %d handler finished.\n", type);
21168518270eSMichael Tuexen 	CURVNET_RESTORE();
2117868b51f2SMichael Tuexen 	NET_EPOCH_EXIT(et);
2118f8829a4aSRandall Stewart }
2119f8829a4aSRandall Stewart 
2120a412576eSMichael Tuexen /*-
2121a412576eSMichael Tuexen  * The following table shows which parameters must be provided
2122a412576eSMichael Tuexen  * when calling sctp_timer_start(). For parameters not being
2123a412576eSMichael Tuexen  * provided, NULL must be used.
2124a412576eSMichael Tuexen  *
2125a412576eSMichael Tuexen  * |Name                         |inp |stcb|net |
2126a412576eSMichael Tuexen  * |-----------------------------|----|----|----|
2127a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SEND         |Yes |Yes |Yes |
2128a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_INIT         |Yes |Yes |Yes |
2129a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_RECV         |Yes |Yes |No  |
2130a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SHUTDOWN     |Yes |Yes |Yes |
2131a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_HEARTBEAT    |Yes |Yes |Yes |
2132a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_COOKIE       |Yes |Yes |Yes |
2133a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_NEWCOOKIE    |Yes |No  |No  |
2134a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_PATHMTURAISE |Yes |Yes |Yes |
2135a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SHUTDOWNACK  |Yes |Yes |Yes |
2136a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_ASCONF       |Yes |Yes |Yes |
2137a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SHUTDOWNGUARD|Yes |Yes |No  |
2138a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_AUTOCLOSE    |Yes |Yes |No  |
2139a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_STRRESET     |Yes |Yes |Yes |
2140a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_INPKILL      |Yes |No  |No  |
2141a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_ASOCKILL     |Yes |Yes |No  |
2142a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_ADDR_WQ      |No  |No  |No  |
2143a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_PRIM_DELETED |Yes |Yes |No  |
2144a412576eSMichael Tuexen  *
2145a412576eSMichael Tuexen  */
2146a412576eSMichael Tuexen 
2147ad81507eSRandall Stewart void
2148f8829a4aSRandall Stewart sctp_timer_start(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2149f8829a4aSRandall Stewart     struct sctp_nets *net)
2150f8829a4aSRandall Stewart {
2151f8829a4aSRandall Stewart 	struct sctp_timer *tmr;
2152a412576eSMichael Tuexen 	uint32_t to_ticks;
2153a412576eSMichael Tuexen 	uint32_t rndval, jitter;
2154f8829a4aSRandall Stewart 
2155*f4cb790aSMichael Tuexen 	KASSERT(stcb == NULL || stcb->sctp_ep == inp,
2156*f4cb790aSMichael Tuexen 	    ("sctp_timer_start of type %d: inp = %p, stcb->sctp_ep %p",
2157*f4cb790aSMichael Tuexen 	    t_type, stcb, stcb->sctp_ep));
2158f8829a4aSRandall Stewart 	tmr = NULL;
2159a412576eSMichael Tuexen 	to_ticks = 0;
2160a412576eSMichael Tuexen 	if (stcb != NULL) {
2161f8829a4aSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
2162a412576eSMichael Tuexen 	} else if (inp != NULL) {
2163a412576eSMichael Tuexen 		SCTP_INP_WLOCK_ASSERT(inp);
2164a412576eSMichael Tuexen 	} else {
2165a412576eSMichael Tuexen 		SCTP_WQ_ADDR_LOCK_ASSERT();
2166a412576eSMichael Tuexen 	}
2167a412576eSMichael Tuexen 	if (stcb != NULL) {
2168a412576eSMichael Tuexen 		/*
2169a412576eSMichael Tuexen 		 * Don't restart timer on association that's about to be
2170a412576eSMichael Tuexen 		 * killed.
2171a412576eSMichael Tuexen 		 */
2172a412576eSMichael Tuexen 		if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
2173a412576eSMichael Tuexen 		    (t_type != SCTP_TIMER_TYPE_ASOCKILL)) {
2174a412576eSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_TIMER2,
217537686ccfSMichael Tuexen 			    "Timer type %d not started: inp=%p, stcb=%p, net=%p (stcb deleted).\n",
2176a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2177a412576eSMichael Tuexen 			return;
2178f8829a4aSRandall Stewart 		}
21799803f01cSMichael Tuexen 		/* Don't restart timer on net that's been removed. */
21809803f01cSMichael Tuexen 		if (net != NULL && (net->dest_state & SCTP_ADDR_BEING_DELETED)) {
2181a412576eSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_TIMER2,
218237686ccfSMichael Tuexen 			    "Timer type %d not started: inp=%p, stcb=%p, net=%p (net deleted).\n",
2183a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
21849803f01cSMichael Tuexen 			return;
21859803f01cSMichael Tuexen 		}
2186a412576eSMichael Tuexen 	}
2187f8829a4aSRandall Stewart 	switch (t_type) {
2188f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SEND:
2189a412576eSMichael Tuexen 		/* Here we use the RTO timer. */
2190a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2191a412576eSMichael Tuexen #ifdef INVARIANTS
2192a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2193a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2194a412576eSMichael Tuexen #else
2195ad81507eSRandall Stewart 			return;
2196a412576eSMichael Tuexen #endif
2197f8829a4aSRandall Stewart 		}
2198f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2199f8829a4aSRandall Stewart 		if (net->RTO == 0) {
220025ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2201f8829a4aSRandall Stewart 		} else {
220225ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
2203f8829a4aSRandall Stewart 		}
2204f8829a4aSRandall Stewart 		break;
2205f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
2206f8829a4aSRandall Stewart 		/*
2207f8829a4aSRandall Stewart 		 * Here we use the INIT timer default usually about 1
2208a412576eSMichael Tuexen 		 * second.
2209f8829a4aSRandall Stewart 		 */
2210a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2211a412576eSMichael Tuexen #ifdef INVARIANTS
2212a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2213a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2214a412576eSMichael Tuexen #else
2215ad81507eSRandall Stewart 			return;
2216a412576eSMichael Tuexen #endif
2217f8829a4aSRandall Stewart 		}
2218f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2219f8829a4aSRandall Stewart 		if (net->RTO == 0) {
222025ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2221f8829a4aSRandall Stewart 		} else {
222225ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
2223f8829a4aSRandall Stewart 		}
2224f8829a4aSRandall Stewart 		break;
2225f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
2226f8829a4aSRandall Stewart 		/*
2227a412576eSMichael Tuexen 		 * Here we use the Delayed-Ack timer value from the inp,
2228f8829a4aSRandall Stewart 		 * ususually about 200ms.
2229f8829a4aSRandall Stewart 		 */
2230a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2231a412576eSMichael Tuexen #ifdef INVARIANTS
2232a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2233a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2234a412576eSMichael Tuexen #else
2235ad81507eSRandall Stewart 			return;
2236a412576eSMichael Tuexen #endif
2237f8829a4aSRandall Stewart 		}
2238f8829a4aSRandall Stewart 		tmr = &stcb->asoc.dack_timer;
223925ec3553SMichael Tuexen 		to_ticks = sctp_msecs_to_ticks(stcb->asoc.delayed_ack);
2240f8829a4aSRandall Stewart 		break;
2241f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
2242f8829a4aSRandall Stewart 		/* Here we use the RTO of the destination. */
2243a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2244a412576eSMichael Tuexen #ifdef INVARIANTS
2245a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2246a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2247a412576eSMichael Tuexen #else
2248ad81507eSRandall Stewart 			return;
2249a412576eSMichael Tuexen #endif
2250f8829a4aSRandall Stewart 		}
2251a412576eSMichael Tuexen 		tmr = &net->rxt_timer;
2252f8829a4aSRandall Stewart 		if (net->RTO == 0) {
225325ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2254f8829a4aSRandall Stewart 		} else {
225525ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
2256f8829a4aSRandall Stewart 		}
2257f8829a4aSRandall Stewart 		break;
2258f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
2259f8829a4aSRandall Stewart 		/*
2260a412576eSMichael Tuexen 		 * The net is used here so that we can add in the RTO. Even
2261f8829a4aSRandall Stewart 		 * though we use a different timer. We also add the HB timer
2262f8829a4aSRandall Stewart 		 * PLUS a random jitter.
2263f8829a4aSRandall Stewart 		 */
2264a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2265a412576eSMichael Tuexen #ifdef INVARIANTS
2266a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2267a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2268a412576eSMichael Tuexen #else
2269ad81507eSRandall Stewart 			return;
2270a412576eSMichael Tuexen #endif
2271a412576eSMichael Tuexen 		}
2272ca85e948SMichael Tuexen 		if ((net->dest_state & SCTP_ADDR_NOHB) &&
2273ca85e948SMichael Tuexen 		    !(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
2274a412576eSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_TIMER2,
227537686ccfSMichael Tuexen 			    "Timer type %d not started: inp=%p, stcb=%p, net=%p.\n",
2276a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2277ad81507eSRandall Stewart 			return;
2278f8829a4aSRandall Stewart 		}
2279a412576eSMichael Tuexen 		tmr = &net->hb_timer;
2280f8829a4aSRandall Stewart 		if (net->RTO == 0) {
2281ca85e948SMichael Tuexen 			to_ticks = stcb->asoc.initial_rto;
2282f8829a4aSRandall Stewart 		} else {
2283ca85e948SMichael Tuexen 			to_ticks = net->RTO;
2284f8829a4aSRandall Stewart 		}
2285ca85e948SMichael Tuexen 		rndval = sctp_select_initial_TSN(&inp->sctp_ep);
2286ca85e948SMichael Tuexen 		jitter = rndval % to_ticks;
2287ca85e948SMichael Tuexen 		if (jitter >= (to_ticks >> 1)) {
2288ca85e948SMichael Tuexen 			to_ticks = to_ticks + (jitter - (to_ticks >> 1));
2289f8829a4aSRandall Stewart 		} else {
2290ca85e948SMichael Tuexen 			to_ticks = to_ticks - jitter;
2291f8829a4aSRandall Stewart 		}
2292ca85e948SMichael Tuexen 		if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
2293ca85e948SMichael Tuexen 		    !(net->dest_state & SCTP_ADDR_PF)) {
2294ca85e948SMichael Tuexen 			to_ticks += net->heart_beat_delay;
2295f8829a4aSRandall Stewart 		}
2296f8829a4aSRandall Stewart 		/*
2297a412576eSMichael Tuexen 		 * Now we must convert the to_ticks that are now in ms to
2298a412576eSMichael Tuexen 		 * ticks.
2299f8829a4aSRandall Stewart 		 */
230025ec3553SMichael Tuexen 		to_ticks = sctp_msecs_to_ticks(to_ticks);
2301f8829a4aSRandall Stewart 		break;
2302f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
2303f8829a4aSRandall Stewart 		/*
2304f8829a4aSRandall Stewart 		 * Here we can use the RTO timer from the network since one
2305a412576eSMichael Tuexen 		 * RTT was complete. If a retransmission happened then we
2306a412576eSMichael Tuexen 		 * will be using the RTO initial value.
2307f8829a4aSRandall Stewart 		 */
2308a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2309a412576eSMichael Tuexen #ifdef INVARIANTS
2310a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2311a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2312a412576eSMichael Tuexen #else
2313ad81507eSRandall Stewart 			return;
2314a412576eSMichael Tuexen #endif
2315f8829a4aSRandall Stewart 		}
2316a412576eSMichael Tuexen 		tmr = &net->rxt_timer;
2317f8829a4aSRandall Stewart 		if (net->RTO == 0) {
231825ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2319f8829a4aSRandall Stewart 		} else {
232025ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
2321f8829a4aSRandall Stewart 		}
2322f8829a4aSRandall Stewart 		break;
2323f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_NEWCOOKIE:
2324f8829a4aSRandall Stewart 		/*
2325a412576eSMichael Tuexen 		 * Nothing needed but the endpoint here ususually about 60
2326f8829a4aSRandall Stewart 		 * minutes.
2327f8829a4aSRandall Stewart 		 */
2328a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb != NULL) || (net != NULL)) {
2329a412576eSMichael Tuexen #ifdef INVARIANTS
2330a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2331a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2332a412576eSMichael Tuexen #else
2333a412576eSMichael Tuexen 			return;
2334a412576eSMichael Tuexen #endif
2335a412576eSMichael Tuexen 		}
2336f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
2337f8829a4aSRandall Stewart 		to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_SIGNATURE];
2338f8829a4aSRandall Stewart 		break;
2339f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
2340f8829a4aSRandall Stewart 		/*
2341a412576eSMichael Tuexen 		 * Here we use the value found in the EP for PMTUD,
2342a412576eSMichael Tuexen 		 * ususually about 10 minutes.
2343f8829a4aSRandall Stewart 		 */
2344a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2345a412576eSMichael Tuexen #ifdef INVARIANTS
2346a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2347a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2348a412576eSMichael Tuexen #else
2349ad81507eSRandall Stewart 			return;
2350a412576eSMichael Tuexen #endif
2351f8829a4aSRandall Stewart 		}
235280c79bbeSMichael Tuexen 		if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
2353a412576eSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_TIMER2,
235437686ccfSMichael Tuexen 			    "Timer type %d not started: inp=%p, stcb=%p, net=%p.\n",
2355a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
235680c79bbeSMichael Tuexen 			return;
235780c79bbeSMichael Tuexen 		}
2358f8829a4aSRandall Stewart 		tmr = &net->pmtu_timer;
2359a412576eSMichael Tuexen 		to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_PMTU];
2360f8829a4aSRandall Stewart 		break;
2361f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
2362a412576eSMichael Tuexen 		/* Here we use the RTO of the destination. */
2363a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2364a412576eSMichael Tuexen #ifdef INVARIANTS
2365a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2366a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2367a412576eSMichael Tuexen #else
2368ad81507eSRandall Stewart 			return;
2369a412576eSMichael Tuexen #endif
2370f8829a4aSRandall Stewart 		}
2371a412576eSMichael Tuexen 		tmr = &net->rxt_timer;
2372f8829a4aSRandall Stewart 		if (net->RTO == 0) {
237325ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2374f8829a4aSRandall Stewart 		} else {
237525ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
2376f8829a4aSRandall Stewart 		}
2377f8829a4aSRandall Stewart 		break;
23780554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ASCONF:
23790554e01dSMichael Tuexen 		/*
23800554e01dSMichael Tuexen 		 * Here the timer comes from the stcb but its value is from
23810554e01dSMichael Tuexen 		 * the net's RTO.
23820554e01dSMichael Tuexen 		 */
2383a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2384a412576eSMichael Tuexen #ifdef INVARIANTS
2385a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2386a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2387a412576eSMichael Tuexen #else
23880554e01dSMichael Tuexen 			return;
2389a412576eSMichael Tuexen #endif
23900554e01dSMichael Tuexen 		}
2391a412576eSMichael Tuexen 		tmr = &stcb->asoc.asconf_timer;
23920554e01dSMichael Tuexen 		if (net->RTO == 0) {
239325ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
23940554e01dSMichael Tuexen 		} else {
239525ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
23960554e01dSMichael Tuexen 		}
23970554e01dSMichael Tuexen 		break;
2398f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2399f8829a4aSRandall Stewart 		/*
2400f8829a4aSRandall Stewart 		 * Here we use the endpoints shutdown guard timer usually
2401f8829a4aSRandall Stewart 		 * about 3 minutes.
2402f8829a4aSRandall Stewart 		 */
2403a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2404a412576eSMichael Tuexen #ifdef INVARIANTS
2405a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2406a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2407a412576eSMichael Tuexen #else
2408ad81507eSRandall Stewart 			return;
2409a412576eSMichael Tuexen #endif
2410f8829a4aSRandall Stewart 		}
2411a412576eSMichael Tuexen 		tmr = &stcb->asoc.shut_guard_timer;
24122e2d6794SMichael Tuexen 		if (inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] == 0) {
241325ec3553SMichael Tuexen 			if (stcb->asoc.maxrto < UINT32_MAX / 5) {
241425ec3553SMichael Tuexen 				to_ticks = sctp_msecs_to_ticks(5 * stcb->asoc.maxrto);
241525ec3553SMichael Tuexen 			} else {
241625ec3553SMichael Tuexen 				to_ticks = sctp_msecs_to_ticks(UINT32_MAX);
241725ec3553SMichael Tuexen 			}
24182e2d6794SMichael Tuexen 		} else {
2419f8829a4aSRandall Stewart 			to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN];
24202e2d6794SMichael Tuexen 		}
2421f8829a4aSRandall Stewart 		break;
24220554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_AUTOCLOSE:
2423a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2424a412576eSMichael Tuexen #ifdef INVARIANTS
2425a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2426a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2427a412576eSMichael Tuexen #else
24280554e01dSMichael Tuexen 			return;
2429a412576eSMichael Tuexen #endif
24300554e01dSMichael Tuexen 		}
24310554e01dSMichael Tuexen 		tmr = &stcb->asoc.autoclose_timer;
2432a412576eSMichael Tuexen 		to_ticks = stcb->asoc.sctp_autoclose_ticks;
24330554e01dSMichael Tuexen 		break;
2434f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_STRRESET:
2435f8829a4aSRandall Stewart 		/*
24361b649582SRandall Stewart 		 * Here the timer comes from the stcb but its value is from
24371b649582SRandall Stewart 		 * the net's RTO.
2438f8829a4aSRandall Stewart 		 */
2439a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2440a412576eSMichael Tuexen #ifdef INVARIANTS
2441a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2442a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2443a412576eSMichael Tuexen #else
2444ad81507eSRandall Stewart 			return;
2445a412576eSMichael Tuexen #endif
2446f8829a4aSRandall Stewart 		}
2447a412576eSMichael Tuexen 		tmr = &stcb->asoc.strreset_timer;
2448f8829a4aSRandall Stewart 		if (net->RTO == 0) {
244925ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2450f8829a4aSRandall Stewart 		} else {
245125ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
2452f8829a4aSRandall Stewart 		}
2453f8829a4aSRandall Stewart 		break;
24540554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_INPKILL:
2455f8829a4aSRandall Stewart 		/*
24560554e01dSMichael Tuexen 		 * The inp is setup to die. We re-use the signature_chage
24570554e01dSMichael Tuexen 		 * timer since that has stopped and we are in the GONE
24580554e01dSMichael Tuexen 		 * state.
2459f8829a4aSRandall Stewart 		 */
2460a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb != NULL) || (net != NULL)) {
2461a412576eSMichael Tuexen #ifdef INVARIANTS
2462a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2463a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2464a412576eSMichael Tuexen #else
2465a412576eSMichael Tuexen 			return;
2466a412576eSMichael Tuexen #endif
2467a412576eSMichael Tuexen 		}
24680554e01dSMichael Tuexen 		tmr = &inp->sctp_ep.signature_change;
246925ec3553SMichael Tuexen 		to_ticks = sctp_msecs_to_ticks(SCTP_INP_KILL_TIMEOUT);
24700554e01dSMichael Tuexen 		break;
24710554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ASOCKILL:
2472a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2473a412576eSMichael Tuexen #ifdef INVARIANTS
2474a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2475a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2476a412576eSMichael Tuexen #else
2477ad81507eSRandall Stewart 			return;
2478a412576eSMichael Tuexen #endif
2479f8829a4aSRandall Stewart 		}
24800554e01dSMichael Tuexen 		tmr = &stcb->asoc.strreset_timer;
248125ec3553SMichael Tuexen 		to_ticks = sctp_msecs_to_ticks(SCTP_ASOC_KILL_TIMEOUT);
24820554e01dSMichael Tuexen 		break;
24830554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ADDR_WQ:
2484a412576eSMichael Tuexen 		if ((inp != NULL) || (stcb != NULL) || (net != NULL)) {
2485a412576eSMichael Tuexen #ifdef INVARIANTS
2486a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2487a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2488a412576eSMichael Tuexen #else
2489a412576eSMichael Tuexen 			return;
2490a412576eSMichael Tuexen #endif
2491a412576eSMichael Tuexen 		}
24920554e01dSMichael Tuexen 		/* Only 1 tick away :-) */
24930554e01dSMichael Tuexen 		tmr = &SCTP_BASE_INFO(addr_wq_timer);
24940554e01dSMichael Tuexen 		to_ticks = SCTP_ADDRESS_TICK_DELAY;
2495f8829a4aSRandall Stewart 		break;
2496851b7298SRandall Stewart 	case SCTP_TIMER_TYPE_PRIM_DELETED:
2497a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2498a412576eSMichael Tuexen #ifdef INVARIANTS
2499a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2500a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2501a412576eSMichael Tuexen #else
2502851b7298SRandall Stewart 			return;
2503a412576eSMichael Tuexen #endif
2504851b7298SRandall Stewart 		}
2505851b7298SRandall Stewart 		tmr = &stcb->asoc.delete_prim_timer;
250625ec3553SMichael Tuexen 		to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2507851b7298SRandall Stewart 		break;
2508f8829a4aSRandall Stewart 	default:
25097522682eSMichael Tuexen #ifdef INVARIANTS
2510a412576eSMichael Tuexen 		panic("Unknown timer type %d", t_type);
25117522682eSMichael Tuexen #else
25127522682eSMichael Tuexen 		return;
25137522682eSMichael Tuexen #endif
251460990c0cSMichael Tuexen 	}
2515a412576eSMichael Tuexen 	KASSERT(tmr != NULL, ("tmr is NULL for timer type %d", t_type));
2516a412576eSMichael Tuexen 	KASSERT(to_ticks > 0, ("to_ticks == 0 for timer type %d", t_type));
2517139bc87fSRandall Stewart 	if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
2518f8829a4aSRandall Stewart 		/*
2519a412576eSMichael Tuexen 		 * We do NOT allow you to have it already running. If it is,
2520a412576eSMichael Tuexen 		 * we leave the current one up unchanged.
2521f8829a4aSRandall Stewart 		 */
2522a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
252337686ccfSMichael Tuexen 		    "Timer type %d already running: inp=%p, stcb=%p, net=%p.\n",
2524a412576eSMichael Tuexen 		    t_type, inp, stcb, net);
2525ad81507eSRandall Stewart 		return;
2526f8829a4aSRandall Stewart 	}
2527a412576eSMichael Tuexen 	/* At this point we can proceed. */
2528f8829a4aSRandall Stewart 	if (t_type == SCTP_TIMER_TYPE_SEND) {
2529f8829a4aSRandall Stewart 		stcb->asoc.num_send_timers_up++;
2530f8829a4aSRandall Stewart 	}
2531a5d547adSRandall Stewart 	tmr->stopped_from = 0;
2532f8829a4aSRandall Stewart 	tmr->type = t_type;
2533f8829a4aSRandall Stewart 	tmr->ep = (void *)inp;
2534f8829a4aSRandall Stewart 	tmr->tcb = (void *)stcb;
2535a412576eSMichael Tuexen 	if (t_type == SCTP_TIMER_TYPE_STRRESET) {
2536a412576eSMichael Tuexen 		tmr->net = NULL;
2537a412576eSMichael Tuexen 	} else {
2538f8829a4aSRandall Stewart 		tmr->net = (void *)net;
2539a412576eSMichael Tuexen 	}
2540f8829a4aSRandall Stewart 	tmr->self = (void *)tmr;
25418518270eSMichael Tuexen 	tmr->vnet = (void *)curvnet;
2542c4739e2fSRandall Stewart 	tmr->ticks = sctp_get_tick_count();
2543a412576eSMichael Tuexen 	if (SCTP_OS_TIMER_START(&tmr->timer, to_ticks, sctp_timeout_handler, tmr) == 0) {
2544a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
254537686ccfSMichael Tuexen 		    "Timer type %d started: ticks=%u, inp=%p, stcb=%p, net=%p.\n",
2546a412576eSMichael Tuexen 		    t_type, to_ticks, inp, stcb, net);
2547a412576eSMichael Tuexen 	} else {
2548a412576eSMichael Tuexen 		/*
2549a412576eSMichael Tuexen 		 * This should not happen, since we checked for pending
2550a412576eSMichael Tuexen 		 * above.
2551a412576eSMichael Tuexen 		 */
2552a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
255337686ccfSMichael Tuexen 		    "Timer type %d restarted: ticks=%u, inp=%p, stcb=%p, net=%p.\n",
2554a412576eSMichael Tuexen 		    t_type, to_ticks, inp, stcb, net);
2555a412576eSMichael Tuexen 	}
2556ad81507eSRandall Stewart 	return;
2557f8829a4aSRandall Stewart }
2558f8829a4aSRandall Stewart 
2559a412576eSMichael Tuexen /*-
2560a412576eSMichael Tuexen  * The following table shows which parameters must be provided
2561a412576eSMichael Tuexen  * when calling sctp_timer_stop(). For parameters not being
2562a412576eSMichael Tuexen  * provided, NULL must be used.
2563a412576eSMichael Tuexen  *
2564a412576eSMichael Tuexen  * |Name                         |inp |stcb|net |
2565a412576eSMichael Tuexen  * |-----------------------------|----|----|----|
2566a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SEND         |Yes |Yes |Yes |
2567a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_INIT         |Yes |Yes |Yes |
2568a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_RECV         |Yes |Yes |No  |
2569a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SHUTDOWN     |Yes |Yes |Yes |
2570a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_HEARTBEAT    |Yes |Yes |Yes |
2571a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_COOKIE       |Yes |Yes |Yes |
2572a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_NEWCOOKIE    |Yes |No  |No  |
2573a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_PATHMTURAISE |Yes |Yes |Yes |
2574a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SHUTDOWNACK  |Yes |Yes |Yes |
2575a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_ASCONF       |Yes |Yes |No  |
2576a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SHUTDOWNGUARD|Yes |Yes |No  |
2577a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_AUTOCLOSE    |Yes |Yes |No  |
2578a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_STRRESET     |Yes |Yes |No  |
2579a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_INPKILL      |Yes |No  |No  |
2580a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_ASOCKILL     |Yes |Yes |No  |
2581a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_ADDR_WQ      |No  |No  |No  |
2582a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_PRIM_DELETED |Yes |Yes |No  |
2583a412576eSMichael Tuexen  *
2584a412576eSMichael Tuexen  */
2585a412576eSMichael Tuexen 
25866e55db54SRandall Stewart void
2587f8829a4aSRandall Stewart sctp_timer_stop(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2588a5d547adSRandall Stewart     struct sctp_nets *net, uint32_t from)
2589f8829a4aSRandall Stewart {
2590f8829a4aSRandall Stewart 	struct sctp_timer *tmr;
2591f8829a4aSRandall Stewart 
2592*f4cb790aSMichael Tuexen 	KASSERT(stcb == NULL || stcb->sctp_ep == inp,
2593*f4cb790aSMichael Tuexen 	    ("sctp_timer_stop of type %d: inp = %p, stcb->sctp_ep %p",
2594*f4cb790aSMichael Tuexen 	    t_type, stcb, stcb->sctp_ep));
2595a412576eSMichael Tuexen 	if (stcb != NULL) {
2596f8829a4aSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
2597a412576eSMichael Tuexen 	} else if (inp != NULL) {
2598a412576eSMichael Tuexen 		SCTP_INP_WLOCK_ASSERT(inp);
2599a412576eSMichael Tuexen 	} else {
2600a412576eSMichael Tuexen 		SCTP_WQ_ADDR_LOCK_ASSERT();
2601f8829a4aSRandall Stewart 	}
2602a412576eSMichael Tuexen 	tmr = NULL;
2603f8829a4aSRandall Stewart 	switch (t_type) {
2604f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SEND:
2605a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2606a412576eSMichael Tuexen #ifdef INVARIANTS
2607a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2608a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2609a412576eSMichael Tuexen #else
26106e55db54SRandall Stewart 			return;
2611a412576eSMichael Tuexen #endif
2612f8829a4aSRandall Stewart 		}
2613f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2614f8829a4aSRandall Stewart 		break;
2615f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
2616a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2617a412576eSMichael Tuexen #ifdef INVARIANTS
2618a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2619a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2620a412576eSMichael Tuexen #else
26216e55db54SRandall Stewart 			return;
2622a412576eSMichael Tuexen #endif
2623f8829a4aSRandall Stewart 		}
2624f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2625f8829a4aSRandall Stewart 		break;
2626f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
2627a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2628a412576eSMichael Tuexen #ifdef INVARIANTS
2629a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2630a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2631a412576eSMichael Tuexen #else
26326e55db54SRandall Stewart 			return;
2633a412576eSMichael Tuexen #endif
2634f8829a4aSRandall Stewart 		}
2635f8829a4aSRandall Stewart 		tmr = &stcb->asoc.dack_timer;
2636f8829a4aSRandall Stewart 		break;
2637f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
2638a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2639a412576eSMichael Tuexen #ifdef INVARIANTS
2640a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2641a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2642a412576eSMichael Tuexen #else
26436e55db54SRandall Stewart 			return;
2644a412576eSMichael Tuexen #endif
2645f8829a4aSRandall Stewart 		}
2646f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2647f8829a4aSRandall Stewart 		break;
2648f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
2649a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2650a412576eSMichael Tuexen #ifdef INVARIANTS
2651a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2652a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2653a412576eSMichael Tuexen #else
26546e55db54SRandall Stewart 			return;
2655a412576eSMichael Tuexen #endif
2656f8829a4aSRandall Stewart 		}
2657ca85e948SMichael Tuexen 		tmr = &net->hb_timer;
2658f8829a4aSRandall Stewart 		break;
2659f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
2660a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2661a412576eSMichael Tuexen #ifdef INVARIANTS
2662a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2663a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2664a412576eSMichael Tuexen #else
26656e55db54SRandall Stewart 			return;
2666a412576eSMichael Tuexen #endif
2667f8829a4aSRandall Stewart 		}
2668f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2669f8829a4aSRandall Stewart 		break;
2670f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_NEWCOOKIE:
2671a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb != NULL) || (net != NULL)) {
2672a412576eSMichael Tuexen #ifdef INVARIANTS
2673a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2674a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2675a412576eSMichael Tuexen #else
2676a412576eSMichael Tuexen 			return;
2677a412576eSMichael Tuexen #endif
2678a412576eSMichael Tuexen 		}
2679f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
2680f8829a4aSRandall Stewart 		break;
2681f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
2682a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2683a412576eSMichael Tuexen #ifdef INVARIANTS
2684a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2685a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2686a412576eSMichael Tuexen #else
26876e55db54SRandall Stewart 			return;
2688a412576eSMichael Tuexen #endif
2689f8829a4aSRandall Stewart 		}
2690f8829a4aSRandall Stewart 		tmr = &net->pmtu_timer;
2691f8829a4aSRandall Stewart 		break;
2692f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
2693a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2694a412576eSMichael Tuexen #ifdef INVARIANTS
2695a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2696a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2697a412576eSMichael Tuexen #else
26986e55db54SRandall Stewart 			return;
2699a412576eSMichael Tuexen #endif
2700f8829a4aSRandall Stewart 		}
2701f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2702f8829a4aSRandall Stewart 		break;
27030554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ASCONF:
2704a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2705a412576eSMichael Tuexen #ifdef INVARIANTS
2706a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2707a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2708a412576eSMichael Tuexen #else
27090554e01dSMichael Tuexen 			return;
2710a412576eSMichael Tuexen #endif
27110554e01dSMichael Tuexen 		}
27120554e01dSMichael Tuexen 		tmr = &stcb->asoc.asconf_timer;
27130554e01dSMichael Tuexen 		break;
2714f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2715a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2716a412576eSMichael Tuexen #ifdef INVARIANTS
2717a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2718a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2719a412576eSMichael Tuexen #else
27206e55db54SRandall Stewart 			return;
2721a412576eSMichael Tuexen #endif
2722f8829a4aSRandall Stewart 		}
2723f8829a4aSRandall Stewart 		tmr = &stcb->asoc.shut_guard_timer;
2724f8829a4aSRandall Stewart 		break;
27250554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_AUTOCLOSE:
2726a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2727a412576eSMichael Tuexen #ifdef INVARIANTS
2728a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2729a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2730a412576eSMichael Tuexen #else
27310554e01dSMichael Tuexen 			return;
2732a412576eSMichael Tuexen #endif
27330554e01dSMichael Tuexen 		}
27340554e01dSMichael Tuexen 		tmr = &stcb->asoc.autoclose_timer;
27350554e01dSMichael Tuexen 		break;
2736f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_STRRESET:
2737a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2738a412576eSMichael Tuexen #ifdef INVARIANTS
2739a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2740a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2741a412576eSMichael Tuexen #else
27426e55db54SRandall Stewart 			return;
2743a412576eSMichael Tuexen #endif
2744f8829a4aSRandall Stewart 		}
2745f8829a4aSRandall Stewart 		tmr = &stcb->asoc.strreset_timer;
2746f8829a4aSRandall Stewart 		break;
27470554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_INPKILL:
27480554e01dSMichael Tuexen 		/*
27490554e01dSMichael Tuexen 		 * The inp is setup to die. We re-use the signature_chage
27500554e01dSMichael Tuexen 		 * timer since that has stopped and we are in the GONE
27510554e01dSMichael Tuexen 		 * state.
27520554e01dSMichael Tuexen 		 */
2753a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb != NULL) || (net != NULL)) {
2754a412576eSMichael Tuexen #ifdef INVARIANTS
2755a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2756a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2757a412576eSMichael Tuexen #else
2758a412576eSMichael Tuexen 			return;
2759a412576eSMichael Tuexen #endif
2760a412576eSMichael Tuexen 		}
27610554e01dSMichael Tuexen 		tmr = &inp->sctp_ep.signature_change;
27620554e01dSMichael Tuexen 		break;
27630554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ASOCKILL:
2764a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2765a412576eSMichael Tuexen #ifdef INVARIANTS
2766a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2767a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2768a412576eSMichael Tuexen #else
27696e55db54SRandall Stewart 			return;
2770a412576eSMichael Tuexen #endif
2771f8829a4aSRandall Stewart 		}
27720554e01dSMichael Tuexen 		tmr = &stcb->asoc.strreset_timer;
27730554e01dSMichael Tuexen 		break;
27740554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ADDR_WQ:
2775a412576eSMichael Tuexen 		if ((inp != NULL) || (stcb != NULL) || (net != NULL)) {
2776a412576eSMichael Tuexen #ifdef INVARIANTS
2777a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2778a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2779a412576eSMichael Tuexen #else
2780a412576eSMichael Tuexen 			return;
2781a412576eSMichael Tuexen #endif
2782a412576eSMichael Tuexen 		}
27830554e01dSMichael Tuexen 		tmr = &SCTP_BASE_INFO(addr_wq_timer);
2784f8829a4aSRandall Stewart 		break;
2785851b7298SRandall Stewart 	case SCTP_TIMER_TYPE_PRIM_DELETED:
2786a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2787a412576eSMichael Tuexen #ifdef INVARIANTS
2788a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2789a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2790a412576eSMichael Tuexen #else
2791851b7298SRandall Stewart 			return;
2792a412576eSMichael Tuexen #endif
2793851b7298SRandall Stewart 		}
2794851b7298SRandall Stewart 		tmr = &stcb->asoc.delete_prim_timer;
2795851b7298SRandall Stewart 		break;
2796f8829a4aSRandall Stewart 	default:
27977522682eSMichael Tuexen #ifdef INVARIANTS
2798a412576eSMichael Tuexen 		panic("Unknown timer type %d", t_type);
27997522682eSMichael Tuexen #else
28007522682eSMichael Tuexen 		return;
28017522682eSMichael Tuexen #endif
280260990c0cSMichael Tuexen 	}
2803a412576eSMichael Tuexen 	KASSERT(tmr != NULL, ("tmr is NULL for timer type %d", t_type));
2804a412576eSMichael Tuexen 	if ((tmr->type != SCTP_TIMER_TYPE_NONE) &&
2805a412576eSMichael Tuexen 	    (tmr->type != t_type)) {
2806f8829a4aSRandall Stewart 		/*
2807f8829a4aSRandall Stewart 		 * Ok we have a timer that is under joint use. Cookie timer
2808f8829a4aSRandall Stewart 		 * per chance with the SEND timer. We therefore are NOT
2809f8829a4aSRandall Stewart 		 * running the timer that the caller wants stopped.  So just
2810f8829a4aSRandall Stewart 		 * return.
2811f8829a4aSRandall Stewart 		 */
2812a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
281337686ccfSMichael Tuexen 		    "Shared timer type %d not running: inp=%p, stcb=%p, net=%p.\n",
2814a412576eSMichael Tuexen 		    t_type, inp, stcb, net);
28156e55db54SRandall Stewart 		return;
2816f8829a4aSRandall Stewart 	}
2817ad81507eSRandall Stewart 	if ((t_type == SCTP_TIMER_TYPE_SEND) && (stcb != NULL)) {
2818f8829a4aSRandall Stewart 		stcb->asoc.num_send_timers_up--;
2819f8829a4aSRandall Stewart 		if (stcb->asoc.num_send_timers_up < 0) {
2820f8829a4aSRandall Stewart 			stcb->asoc.num_send_timers_up = 0;
2821f8829a4aSRandall Stewart 		}
2822f8829a4aSRandall Stewart 	}
2823f8829a4aSRandall Stewart 	tmr->self = NULL;
2824a5d547adSRandall Stewart 	tmr->stopped_from = from;
2825a412576eSMichael Tuexen 	if (SCTP_OS_TIMER_STOP(&tmr->timer) == 1) {
2826a412576eSMichael Tuexen 		KASSERT(tmr->ep == inp,
2827a412576eSMichael Tuexen 		    ("sctp_timer_stop of type %d: inp = %p, tmr->inp = %p",
2828a412576eSMichael Tuexen 		    t_type, inp, tmr->ep));
2829a412576eSMichael Tuexen 		KASSERT(tmr->tcb == stcb,
2830a412576eSMichael Tuexen 		    ("sctp_timer_stop of type %d: stcb = %p, tmr->stcb = %p",
2831a412576eSMichael Tuexen 		    t_type, stcb, tmr->tcb));
2832a412576eSMichael Tuexen 		KASSERT(((t_type == SCTP_TIMER_TYPE_ASCONF) && (tmr->net != NULL)) ||
2833a412576eSMichael Tuexen 		    ((t_type != SCTP_TIMER_TYPE_ASCONF) && (tmr->net == net)),
2834a412576eSMichael Tuexen 		    ("sctp_timer_stop of type %d: net = %p, tmr->net = %p",
2835a412576eSMichael Tuexen 		    t_type, net, tmr->net));
2836a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
283737686ccfSMichael Tuexen 		    "Timer type %d stopped: inp=%p, stcb=%p, net=%p.\n",
2838a412576eSMichael Tuexen 		    t_type, inp, stcb, net);
2839a412576eSMichael Tuexen 		tmr->ep = NULL;
2840a412576eSMichael Tuexen 		tmr->tcb = NULL;
2841a412576eSMichael Tuexen 		tmr->net = NULL;
2842a412576eSMichael Tuexen 	} else {
2843a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
284437686ccfSMichael Tuexen 		    "Timer type %d not stopped: inp=%p, stcb=%p, net=%p.\n",
2845a412576eSMichael Tuexen 		    t_type, inp, stcb, net);
2846a412576eSMichael Tuexen 	}
28476e55db54SRandall Stewart 	return;
2848f8829a4aSRandall Stewart }
2849f8829a4aSRandall Stewart 
2850f8829a4aSRandall Stewart uint32_t
2851b0471b4bSMichael Tuexen sctp_calculate_len(struct mbuf *m)
2852b0471b4bSMichael Tuexen {
2853f8829a4aSRandall Stewart 	uint32_t tlen = 0;
2854f8829a4aSRandall Stewart 	struct mbuf *at;
2855f8829a4aSRandall Stewart 
2856f8829a4aSRandall Stewart 	at = m;
2857f8829a4aSRandall Stewart 	while (at) {
2858139bc87fSRandall Stewart 		tlen += SCTP_BUF_LEN(at);
2859139bc87fSRandall Stewart 		at = SCTP_BUF_NEXT(at);
2860f8829a4aSRandall Stewart 	}
2861f8829a4aSRandall Stewart 	return (tlen);
2862f8829a4aSRandall Stewart }
2863f8829a4aSRandall Stewart 
2864f8829a4aSRandall Stewart void
2865f8829a4aSRandall Stewart sctp_mtu_size_reset(struct sctp_inpcb *inp,
286644b7479bSRandall Stewart     struct sctp_association *asoc, uint32_t mtu)
2867f8829a4aSRandall Stewart {
2868f8829a4aSRandall Stewart 	/*
2869f8829a4aSRandall Stewart 	 * Reset the P-MTU size on this association, this involves changing
2870f8829a4aSRandall Stewart 	 * the asoc MTU, going through ANY chunk+overhead larger than mtu to
2871f8829a4aSRandall Stewart 	 * allow the DF flag to be cleared.
2872f8829a4aSRandall Stewart 	 */
2873f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
2874f8829a4aSRandall Stewart 	unsigned int eff_mtu, ovh;
2875f8829a4aSRandall Stewart 
2876f8829a4aSRandall Stewart 	asoc->smallest_mtu = mtu;
2877f8829a4aSRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2878f8829a4aSRandall Stewart 		ovh = SCTP_MIN_OVERHEAD;
2879f8829a4aSRandall Stewart 	} else {
2880f8829a4aSRandall Stewart 		ovh = SCTP_MIN_V4_OVERHEAD;
2881f8829a4aSRandall Stewart 	}
2882f8829a4aSRandall Stewart 	eff_mtu = mtu - ovh;
2883f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
2884f8829a4aSRandall Stewart 		if (chk->send_size > eff_mtu) {
2885f8829a4aSRandall Stewart 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2886f8829a4aSRandall Stewart 		}
2887f8829a4aSRandall Stewart 	}
2888f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->sent_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 }
2894f8829a4aSRandall Stewart 
2895f8829a4aSRandall Stewart 
2896f8829a4aSRandall Stewart /*
289744f2a327SMichael Tuexen  * Given an association and starting time of the current RTT period, update
289844f2a327SMichael Tuexen  * RTO in number of msecs. net should point to the current network.
289944f2a327SMichael Tuexen  * Return 1, if an RTO update was performed, return 0 if no update was
290044f2a327SMichael Tuexen  * performed due to invalid starting point.
2901f8829a4aSRandall Stewart  */
2902899288aeSRandall Stewart 
290344f2a327SMichael Tuexen int
2904f8829a4aSRandall Stewart sctp_calculate_rto(struct sctp_tcb *stcb,
2905f8829a4aSRandall Stewart     struct sctp_association *asoc,
2906f8829a4aSRandall Stewart     struct sctp_nets *net,
29078c8e10b7SMichael Tuexen     struct timeval *old,
2908b0471b4bSMichael Tuexen     int rtt_from_sack)
2909b0471b4bSMichael Tuexen {
291044f2a327SMichael Tuexen 	struct timeval now;
291144f2a327SMichael Tuexen 	uint64_t rtt_us;	/* RTT in us */
2912be1d9176SMichael Tuexen 	int32_t rtt;		/* RTT in ms */
2913be1d9176SMichael Tuexen 	uint32_t new_rto;
2914f8829a4aSRandall Stewart 	int first_measure = 0;
2915f8829a4aSRandall Stewart 
2916f8829a4aSRandall Stewart 	/************************/
2917f8829a4aSRandall Stewart 	/* 1. calculate new RTT */
2918f8829a4aSRandall Stewart 	/************************/
2919f8829a4aSRandall Stewart 	/* get the current time */
2920299108c5SRandall Stewart 	if (stcb->asoc.use_precise_time) {
2921299108c5SRandall Stewart 		(void)SCTP_GETPTIME_TIMEVAL(&now);
2922299108c5SRandall Stewart 	} else {
29236e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
2924299108c5SRandall Stewart 	}
292544f2a327SMichael Tuexen 	if ((old->tv_sec > now.tv_sec) ||
292644f2a327SMichael Tuexen 	    ((old->tv_sec == now.tv_sec) && (old->tv_sec > now.tv_sec))) {
292744f2a327SMichael Tuexen 		/* The starting point is in the future. */
292844f2a327SMichael Tuexen 		return (0);
292944f2a327SMichael Tuexen 	}
2930be1d9176SMichael Tuexen 	timevalsub(&now, old);
293144f2a327SMichael Tuexen 	rtt_us = (uint64_t)1000000 * (uint64_t)now.tv_sec + (uint64_t)now.tv_usec;
293244f2a327SMichael Tuexen 	if (rtt_us > SCTP_RTO_UPPER_BOUND * 1000) {
293344f2a327SMichael Tuexen 		/* The RTT is larger than a sane value. */
293444f2a327SMichael Tuexen 		return (0);
293544f2a327SMichael Tuexen 	}
2936be1d9176SMichael Tuexen 	/* store the current RTT in us */
293744f2a327SMichael Tuexen 	net->rtt = rtt_us;
2938b60b0fe6SMichael Tuexen 	/* compute rtt in ms */
2939b60b0fe6SMichael Tuexen 	rtt = (int32_t)(net->rtt / 1000);
2940f79aab18SRandall Stewart 	if ((asoc->cc_functions.sctp_rtt_calculated) && (rtt_from_sack == SCTP_RTT_FROM_DATA)) {
2941b7b84c0eSMichael Tuexen 		/*
2942b7b84c0eSMichael Tuexen 		 * Tell the CC module that a new update has just occurred
2943b7b84c0eSMichael Tuexen 		 * from a sack
2944b7b84c0eSMichael Tuexen 		 */
2945f79aab18SRandall Stewart 		(*asoc->cc_functions.sctp_rtt_calculated) (stcb, net, &now);
2946f79aab18SRandall Stewart 	}
2947f79aab18SRandall Stewart 	/*
2948f79aab18SRandall Stewart 	 * Do we need to determine the lan? We do this only on sacks i.e.
2949f79aab18SRandall Stewart 	 * RTT being determined from data not non-data (HB/INIT->INITACK).
2950f79aab18SRandall Stewart 	 */
2951f79aab18SRandall Stewart 	if ((rtt_from_sack == SCTP_RTT_FROM_DATA) &&
2952be1d9176SMichael Tuexen 	    (net->lan_type == SCTP_LAN_UNKNOWN)) {
2953be1d9176SMichael Tuexen 		if (net->rtt > SCTP_LOCAL_LAN_RTT) {
2954899288aeSRandall Stewart 			net->lan_type = SCTP_LAN_INTERNET;
2955899288aeSRandall Stewart 		} else {
2956899288aeSRandall Stewart 			net->lan_type = SCTP_LAN_LOCAL;
2957899288aeSRandall Stewart 		}
2958899288aeSRandall Stewart 	}
29590053ed28SMichael Tuexen 
2960f8829a4aSRandall Stewart 	/***************************/
2961f8829a4aSRandall Stewart 	/* 2. update RTTVAR & SRTT */
2962f8829a4aSRandall Stewart 	/***************************/
2963be1d9176SMichael Tuexen 	/*-
2964be1d9176SMichael Tuexen 	 * Compute the scaled average lastsa and the
2965be1d9176SMichael Tuexen 	 * scaled variance lastsv as described in van Jacobson
2966be1d9176SMichael Tuexen 	 * Paper "Congestion Avoidance and Control", Annex A.
2967be1d9176SMichael Tuexen 	 *
2968be1d9176SMichael Tuexen 	 * (net->lastsa >> SCTP_RTT_SHIFT) is the srtt
296944f2a327SMichael Tuexen 	 * (net->lastsv >> SCTP_RTT_VAR_SHIFT) is the rttvar
2970be1d9176SMichael Tuexen 	 */
29719a972525SRandall Stewart 	if (net->RTO_measured) {
2972be1d9176SMichael Tuexen 		rtt -= (net->lastsa >> SCTP_RTT_SHIFT);
2973be1d9176SMichael Tuexen 		net->lastsa += rtt;
2974be1d9176SMichael Tuexen 		if (rtt < 0) {
2975be1d9176SMichael Tuexen 			rtt = -rtt;
2976be1d9176SMichael Tuexen 		}
2977be1d9176SMichael Tuexen 		rtt -= (net->lastsv >> SCTP_RTT_VAR_SHIFT);
2978be1d9176SMichael Tuexen 		net->lastsv += rtt;
2979b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2980f8829a4aSRandall Stewart 			rto_logging(net, SCTP_LOG_RTTVAR);
298180fefe0aSRandall Stewart 		}
2982f8829a4aSRandall Stewart 	} else {
2983f8829a4aSRandall Stewart 		/* First RTO measurment */
29849a972525SRandall Stewart 		net->RTO_measured = 1;
2985f8829a4aSRandall Stewart 		first_measure = 1;
2986be1d9176SMichael Tuexen 		net->lastsa = rtt << SCTP_RTT_SHIFT;
2987be1d9176SMichael Tuexen 		net->lastsv = (rtt / 2) << SCTP_RTT_VAR_SHIFT;
2988b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2989f8829a4aSRandall Stewart 			rto_logging(net, SCTP_LOG_INITIAL_RTT);
299080fefe0aSRandall Stewart 		}
2991f8829a4aSRandall Stewart 	}
2992be1d9176SMichael Tuexen 	if (net->lastsv == 0) {
2993be1d9176SMichael Tuexen 		net->lastsv = SCTP_CLOCK_GRANULARITY;
2994be1d9176SMichael Tuexen 	}
2995108df27cSRandall Stewart 	new_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
2996f8829a4aSRandall Stewart 	if ((new_rto > SCTP_SAT_NETWORK_MIN) &&
2997f8829a4aSRandall Stewart 	    (stcb->asoc.sat_network_lockout == 0)) {
2998f8829a4aSRandall Stewart 		stcb->asoc.sat_network = 1;
2999f8829a4aSRandall Stewart 	} else if ((!first_measure) && stcb->asoc.sat_network) {
3000f8829a4aSRandall Stewart 		stcb->asoc.sat_network = 0;
3001f8829a4aSRandall Stewart 		stcb->asoc.sat_network_lockout = 1;
3002f8829a4aSRandall Stewart 	}
3003f8829a4aSRandall Stewart 	/* bound it, per C6/C7 in Section 5.3.1 */
3004f8829a4aSRandall Stewart 	if (new_rto < stcb->asoc.minrto) {
3005f8829a4aSRandall Stewart 		new_rto = stcb->asoc.minrto;
3006f8829a4aSRandall Stewart 	}
3007f8829a4aSRandall Stewart 	if (new_rto > stcb->asoc.maxrto) {
3008f8829a4aSRandall Stewart 		new_rto = stcb->asoc.maxrto;
3009f8829a4aSRandall Stewart 	}
301044f2a327SMichael Tuexen 	net->RTO = new_rto;
301144f2a327SMichael Tuexen 	return (1);
3012f8829a4aSRandall Stewart }
3013f8829a4aSRandall Stewart 
3014f8829a4aSRandall Stewart /*
3015f8829a4aSRandall Stewart  * return a pointer to a contiguous piece of data from the given mbuf chain
3016f8829a4aSRandall Stewart  * starting at 'off' for 'len' bytes.  If the desired piece spans more than
3017f8829a4aSRandall Stewart  * one mbuf, a copy is made at 'ptr'. caller must ensure that the buffer size
3018f8829a4aSRandall Stewart  * is >= 'len' returns NULL if there there isn't 'len' bytes in the chain.
3019f8829a4aSRandall Stewart  */
302072fb6fdbSRandall Stewart caddr_t
3021f8829a4aSRandall Stewart sctp_m_getptr(struct mbuf *m, int off, int len, uint8_t *in_ptr)
3022f8829a4aSRandall Stewart {
3023f8829a4aSRandall Stewart 	uint32_t count;
3024f8829a4aSRandall Stewart 	uint8_t *ptr;
3025f8829a4aSRandall Stewart 
3026f8829a4aSRandall Stewart 	ptr = in_ptr;
3027f8829a4aSRandall Stewart 	if ((off < 0) || (len <= 0))
3028f8829a4aSRandall Stewart 		return (NULL);
3029f8829a4aSRandall Stewart 
3030f8829a4aSRandall Stewart 	/* find the desired start location */
3031f8829a4aSRandall Stewart 	while ((m != NULL) && (off > 0)) {
3032139bc87fSRandall Stewart 		if (off < SCTP_BUF_LEN(m))
3033f8829a4aSRandall Stewart 			break;
3034139bc87fSRandall Stewart 		off -= SCTP_BUF_LEN(m);
3035139bc87fSRandall Stewart 		m = SCTP_BUF_NEXT(m);
3036f8829a4aSRandall Stewart 	}
3037f8829a4aSRandall Stewart 	if (m == NULL)
3038f8829a4aSRandall Stewart 		return (NULL);
3039f8829a4aSRandall Stewart 
3040f8829a4aSRandall Stewart 	/* is the current mbuf large enough (eg. contiguous)? */
3041139bc87fSRandall Stewart 	if ((SCTP_BUF_LEN(m) - off) >= len) {
3042f8829a4aSRandall Stewart 		return (mtod(m, caddr_t)+off);
3043f8829a4aSRandall Stewart 	} else {
3044f8829a4aSRandall Stewart 		/* else, it spans more than one mbuf, so save a temp copy... */
3045f8829a4aSRandall Stewart 		while ((m != NULL) && (len > 0)) {
3046139bc87fSRandall Stewart 			count = min(SCTP_BUF_LEN(m) - off, len);
30475ba7f91fSMichael Tuexen 			memcpy(ptr, mtod(m, caddr_t)+off, count);
3048f8829a4aSRandall Stewart 			len -= count;
3049f8829a4aSRandall Stewart 			ptr += count;
3050f8829a4aSRandall Stewart 			off = 0;
3051139bc87fSRandall Stewart 			m = SCTP_BUF_NEXT(m);
3052f8829a4aSRandall Stewart 		}
3053f8829a4aSRandall Stewart 		if ((m == NULL) && (len > 0))
3054f8829a4aSRandall Stewart 			return (NULL);
3055f8829a4aSRandall Stewart 		else
3056f8829a4aSRandall Stewart 			return ((caddr_t)in_ptr);
3057f8829a4aSRandall Stewart 	}
3058f8829a4aSRandall Stewart }
3059f8829a4aSRandall Stewart 
3060f8829a4aSRandall Stewart 
306144b7479bSRandall Stewart 
3062f8829a4aSRandall Stewart struct sctp_paramhdr *
3063f8829a4aSRandall Stewart sctp_get_next_param(struct mbuf *m,
3064f8829a4aSRandall Stewart     int offset,
3065f8829a4aSRandall Stewart     struct sctp_paramhdr *pull,
3066f8829a4aSRandall Stewart     int pull_limit)
3067f8829a4aSRandall Stewart {
3068f8829a4aSRandall Stewart 	/* This just provides a typed signature to Peter's Pull routine */
3069f8829a4aSRandall Stewart 	return ((struct sctp_paramhdr *)sctp_m_getptr(m, offset, pull_limit,
3070f8829a4aSRandall Stewart 	    (uint8_t *)pull));
3071f8829a4aSRandall Stewart }
3072f8829a4aSRandall Stewart 
3073f8829a4aSRandall Stewart 
3074ce11b842SMichael Tuexen struct mbuf *
3075f8829a4aSRandall Stewart sctp_add_pad_tombuf(struct mbuf *m, int padlen)
3076f8829a4aSRandall Stewart {
3077ce11b842SMichael Tuexen 	struct mbuf *m_last;
3078ce11b842SMichael Tuexen 	caddr_t dp;
3079f8829a4aSRandall Stewart 
3080f8829a4aSRandall Stewart 	if (padlen > 3) {
3081ce11b842SMichael Tuexen 		return (NULL);
3082f8829a4aSRandall Stewart 	}
308341eee555SRandall Stewart 	if (padlen <= M_TRAILINGSPACE(m)) {
3084f8829a4aSRandall Stewart 		/*
3085f8829a4aSRandall Stewart 		 * The easy way. We hope the majority of the time we hit
3086f8829a4aSRandall Stewart 		 * here :)
3087f8829a4aSRandall Stewart 		 */
3088ce11b842SMichael Tuexen 		m_last = m;
3089f8829a4aSRandall Stewart 	} else {
3090ce11b842SMichael Tuexen 		/* Hard way we must grow the mbuf chain */
3091ce11b842SMichael Tuexen 		m_last = sctp_get_mbuf_for_msg(padlen, 0, M_NOWAIT, 1, MT_DATA);
3092ce11b842SMichael Tuexen 		if (m_last == NULL) {
3093ce11b842SMichael Tuexen 			return (NULL);
3094f8829a4aSRandall Stewart 		}
3095ce11b842SMichael Tuexen 		SCTP_BUF_LEN(m_last) = 0;
3096ce11b842SMichael Tuexen 		SCTP_BUF_NEXT(m_last) = NULL;
3097ce11b842SMichael Tuexen 		SCTP_BUF_NEXT(m) = m_last;
3098f8829a4aSRandall Stewart 	}
3099ce11b842SMichael Tuexen 	dp = mtod(m_last, caddr_t)+SCTP_BUF_LEN(m_last);
3100ce11b842SMichael Tuexen 	SCTP_BUF_LEN(m_last) += padlen;
3101ce11b842SMichael Tuexen 	memset(dp, 0, padlen);
3102ce11b842SMichael Tuexen 	return (m_last);
3103f8829a4aSRandall Stewart }
3104f8829a4aSRandall Stewart 
3105ce11b842SMichael Tuexen struct mbuf *
3106f8829a4aSRandall Stewart sctp_pad_lastmbuf(struct mbuf *m, int padval, struct mbuf *last_mbuf)
3107f8829a4aSRandall Stewart {
3108f8829a4aSRandall Stewart 	/* find the last mbuf in chain and pad it */
3109f8829a4aSRandall Stewart 	struct mbuf *m_at;
3110f8829a4aSRandall Stewart 
3111ce11b842SMichael Tuexen 	if (last_mbuf != NULL) {
3112f8829a4aSRandall Stewart 		return (sctp_add_pad_tombuf(last_mbuf, padval));
3113f8829a4aSRandall Stewart 	} else {
311417267b32SMichael Tuexen 		for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3115139bc87fSRandall Stewart 			if (SCTP_BUF_NEXT(m_at) == NULL) {
3116f8829a4aSRandall Stewart 				return (sctp_add_pad_tombuf(m_at, padval));
3117f8829a4aSRandall Stewart 			}
3118f8829a4aSRandall Stewart 		}
3119f8829a4aSRandall Stewart 	}
3120ce11b842SMichael Tuexen 	return (NULL);
3121f8829a4aSRandall Stewart }
3122f8829a4aSRandall Stewart 
3123f8829a4aSRandall Stewart static void
3124c5b5675dSMichael Tuexen sctp_notify_assoc_change(uint16_t state, struct sctp_tcb *stcb,
3125410a3b1eSMichael Tuexen     uint16_t error, struct sctp_abort_chunk *abort, uint8_t from_peer, int so_locked
3126ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3127ceaad40aSRandall Stewart     SCTP_UNUSED
3128ceaad40aSRandall Stewart #endif
3129ceaad40aSRandall Stewart )
3130f8829a4aSRandall Stewart {
3131f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3132f8829a4aSRandall Stewart 	struct sctp_assoc_change *sac;
3133f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
31349a8e3088SMichael Tuexen 	unsigned int notif_len;
31359a8e3088SMichael Tuexen 	uint16_t abort_len;
3136e06b67c7SMichael Tuexen 	unsigned int i;
3137ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3138ceaad40aSRandall Stewart 	struct socket *so;
3139ceaad40aSRandall Stewart #endif
3140ceaad40aSRandall Stewart 
314159713bbfSMichael Tuexen 	if (stcb == NULL) {
314259713bbfSMichael Tuexen 		return;
314359713bbfSMichael Tuexen 	}
314458411b08SMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT)) {
31459a8e3088SMichael Tuexen 		notif_len = (unsigned int)sizeof(struct sctp_assoc_change);
3146a2b42326SMichael Tuexen 		if (abort != NULL) {
3147c9eb4473SMichael Tuexen 			abort_len = ntohs(abort->ch.chunk_length);
31489669e724SMichael Tuexen 			/*
31499669e724SMichael Tuexen 			 * Only SCTP_CHUNK_BUFFER_SIZE are guaranteed to be
315045d41de5SMichael Tuexen 			 * contiguous.
31519669e724SMichael Tuexen 			 */
31529669e724SMichael Tuexen 			if (abort_len > SCTP_CHUNK_BUFFER_SIZE) {
31539669e724SMichael Tuexen 				abort_len = SCTP_CHUNK_BUFFER_SIZE;
31549669e724SMichael Tuexen 			}
3155a2b42326SMichael Tuexen 		} else {
3156a2b42326SMichael Tuexen 			abort_len = 0;
3157c5b5675dSMichael Tuexen 		}
3158a2b42326SMichael Tuexen 		if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
3159a2b42326SMichael Tuexen 			notif_len += SCTP_ASSOC_SUPPORTS_MAX;
3160a2b42326SMichael Tuexen 		} else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
3161a2b42326SMichael Tuexen 			notif_len += abort_len;
3162a2b42326SMichael Tuexen 		}
3163eb1b1807SGleb Smirnoff 		m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3164a2b42326SMichael Tuexen 		if (m_notify == NULL) {
3165a2b42326SMichael Tuexen 			/* Retry with smaller value. */
31669a8e3088SMichael Tuexen 			notif_len = (unsigned int)sizeof(struct sctp_assoc_change);
3167eb1b1807SGleb Smirnoff 			m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3168a2b42326SMichael Tuexen 			if (m_notify == NULL) {
316958411b08SMichael Tuexen 				goto set_error;
3170a2b42326SMichael Tuexen 			}
3171a2b42326SMichael Tuexen 		}
3172a2b42326SMichael Tuexen 		SCTP_BUF_NEXT(m_notify) = NULL;
3173f8829a4aSRandall Stewart 		sac = mtod(m_notify, struct sctp_assoc_change *);
3174e432298aSXin LI 		memset(sac, 0, notif_len);
3175f8829a4aSRandall Stewart 		sac->sac_type = SCTP_ASSOC_CHANGE;
3176f8829a4aSRandall Stewart 		sac->sac_flags = 0;
3177f8829a4aSRandall Stewart 		sac->sac_length = sizeof(struct sctp_assoc_change);
3178c5b5675dSMichael Tuexen 		sac->sac_state = state;
3179f8829a4aSRandall Stewart 		sac->sac_error = error;
3180f8829a4aSRandall Stewart 		/* XXX verify these stream counts */
3181f8829a4aSRandall Stewart 		sac->sac_outbound_streams = stcb->asoc.streamoutcnt;
3182f8829a4aSRandall Stewart 		sac->sac_inbound_streams = stcb->asoc.streamincnt;
3183f8829a4aSRandall Stewart 		sac->sac_assoc_id = sctp_get_associd(stcb);
3184a2b42326SMichael Tuexen 		if (notif_len > sizeof(struct sctp_assoc_change)) {
3185c5b5675dSMichael Tuexen 			if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
3186e06b67c7SMichael Tuexen 				i = 0;
3187c79bec9cSMichael Tuexen 				if (stcb->asoc.prsctp_supported == 1) {
3188e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_PR;
3189e06b67c7SMichael Tuexen 				}
3190c79bec9cSMichael Tuexen 				if (stcb->asoc.auth_supported == 1) {
3191e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_AUTH;
3192e06b67c7SMichael Tuexen 				}
3193c79bec9cSMichael Tuexen 				if (stcb->asoc.asconf_supported == 1) {
3194e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_ASCONF;
3195e06b67c7SMichael Tuexen 				}
319644249214SRandall Stewart 				if (stcb->asoc.idata_supported == 1) {
319744249214SRandall Stewart 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_INTERLEAVING;
319844249214SRandall Stewart 				}
3199e06b67c7SMichael Tuexen 				sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_MULTIBUF;
3200c79bec9cSMichael Tuexen 				if (stcb->asoc.reconfig_supported == 1) {
3201e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_RE_CONFIG;
3202e06b67c7SMichael Tuexen 				}
3203e06b67c7SMichael Tuexen 				sac->sac_length += i;
3204a2b42326SMichael Tuexen 			} else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
3205a2b42326SMichael Tuexen 				memcpy(sac->sac_info, abort, abort_len);
3206a2b42326SMichael Tuexen 				sac->sac_length += abort_len;
3207a2b42326SMichael Tuexen 			}
3208c5b5675dSMichael Tuexen 		}
3209e06b67c7SMichael Tuexen 		SCTP_BUF_LEN(m_notify) = sac->sac_length;
3210f8829a4aSRandall Stewart 		control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
32117215cc1bSMichael Tuexen 		    0, 0, stcb->asoc.context, 0, 0, 0,
3212f8829a4aSRandall Stewart 		    m_notify);
321358411b08SMichael Tuexen 		if (control != NULL) {
3214139bc87fSRandall Stewart 			control->length = SCTP_BUF_LEN(m_notify);
321528cd0699SMichael Tuexen 			control->spec_flags = M_NOTIFICATION;
3216f8829a4aSRandall Stewart 			/* not that we need this */
3217f8829a4aSRandall Stewart 			control->tail_mbuf = m_notify;
3218f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep, stcb,
3219f8829a4aSRandall Stewart 			    control,
3220cfde3ff7SRandall Stewart 			    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD,
3221cfde3ff7SRandall Stewart 			    so_locked);
322258411b08SMichael Tuexen 		} else {
322358411b08SMichael Tuexen 			sctp_m_freem(m_notify);
322458411b08SMichael Tuexen 		}
322558411b08SMichael Tuexen 	}
322658411b08SMichael Tuexen 	/*
322758411b08SMichael Tuexen 	 * For 1-to-1 style sockets, we send up and error when an ABORT
322858411b08SMichael Tuexen 	 * comes in.
322958411b08SMichael Tuexen 	 */
323058411b08SMichael Tuexen set_error:
323158411b08SMichael Tuexen 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
323258411b08SMichael Tuexen 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
323358411b08SMichael Tuexen 	    ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
3234e045904fSMichael Tuexen 		SOCK_LOCK(stcb->sctp_socket);
3235410a3b1eSMichael Tuexen 		if (from_peer) {
3236839d21d6SMichael Tuexen 			if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) {
323758411b08SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNREFUSED);
323858411b08SMichael Tuexen 				stcb->sctp_socket->so_error = ECONNREFUSED;
323958411b08SMichael Tuexen 			} else {
324058411b08SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
324158411b08SMichael Tuexen 				stcb->sctp_socket->so_error = ECONNRESET;
324258411b08SMichael Tuexen 			}
3243410a3b1eSMichael Tuexen 		} else {
3244839d21d6SMichael Tuexen 			if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
3245839d21d6SMichael Tuexen 			    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
3246553bb068SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ETIMEDOUT);
3247553bb068SMichael Tuexen 				stcb->sctp_socket->so_error = ETIMEDOUT;
3248553bb068SMichael Tuexen 			} else {
3249410a3b1eSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNABORTED);
3250410a3b1eSMichael Tuexen 				stcb->sctp_socket->so_error = ECONNABORTED;
3251410a3b1eSMichael Tuexen 			}
325258411b08SMichael Tuexen 		}
32533acfe1e1SGleb Smirnoff 		SOCK_UNLOCK(stcb->sctp_socket);
3254553bb068SMichael Tuexen 	}
325558411b08SMichael Tuexen 	/* Wake ANY sleepers */
3256ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3257ceaad40aSRandall Stewart 	so = SCTP_INP_SO(stcb->sctp_ep);
3258ceaad40aSRandall Stewart 	if (!so_locked) {
3259ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
3260ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3261ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
3262ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
3263ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
3264ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3265ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
3266ceaad40aSRandall Stewart 			return;
3267ceaad40aSRandall Stewart 		}
3268ceaad40aSRandall Stewart 	}
3269ceaad40aSRandall Stewart #endif
327058411b08SMichael Tuexen 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
327158411b08SMichael Tuexen 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
327258411b08SMichael Tuexen 	    ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
32733acfe1e1SGleb Smirnoff 		socantrcvmore(stcb->sctp_socket);
327458411b08SMichael Tuexen 	}
327558411b08SMichael Tuexen 	sorwakeup(stcb->sctp_socket);
327658411b08SMichael Tuexen 	sowwakeup(stcb->sctp_socket);
3277ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3278ceaad40aSRandall Stewart 	if (!so_locked) {
3279ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
3280ceaad40aSRandall Stewart 	}
3281ceaad40aSRandall Stewart #endif
3282f8829a4aSRandall Stewart }
3283f8829a4aSRandall Stewart 
3284f8829a4aSRandall Stewart static void
3285f8829a4aSRandall Stewart sctp_notify_peer_addr_change(struct sctp_tcb *stcb, uint32_t state,
32863cb3567dSMichael Tuexen     struct sockaddr *sa, uint32_t error, int so_locked
32873cb3567dSMichael Tuexen #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
32883cb3567dSMichael Tuexen     SCTP_UNUSED
32893cb3567dSMichael Tuexen #endif
32903cb3567dSMichael Tuexen )
3291f8829a4aSRandall Stewart {
3292f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3293f8829a4aSRandall Stewart 	struct sctp_paddr_change *spc;
3294f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3295f8829a4aSRandall Stewart 
329660990c0cSMichael Tuexen 	if ((stcb == NULL) ||
329760990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT)) {
3298f8829a4aSRandall Stewart 		/* event not enabled */
3299f8829a4aSRandall Stewart 		return;
3300830d754dSRandall Stewart 	}
3301eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_paddr_change), 0, M_NOWAIT, 1, MT_DATA);
3302f8829a4aSRandall Stewart 	if (m_notify == NULL)
3303f8829a4aSRandall Stewart 		return;
3304139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3305f8829a4aSRandall Stewart 	spc = mtod(m_notify, struct sctp_paddr_change *);
330656711f94SMichael Tuexen 	memset(spc, 0, sizeof(struct sctp_paddr_change));
3307f8829a4aSRandall Stewart 	spc->spc_type = SCTP_PEER_ADDR_CHANGE;
3308f8829a4aSRandall Stewart 	spc->spc_flags = 0;
3309f8829a4aSRandall Stewart 	spc->spc_length = sizeof(struct sctp_paddr_change);
33105e2c2d87SRandall Stewart 	switch (sa->sa_family) {
3311ea5eba11SMichael Tuexen #ifdef INET
33125e2c2d87SRandall Stewart 	case AF_INET:
3313d59107f7SMichael Tuexen #ifdef INET6
3314d59107f7SMichael Tuexen 		if (sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
3315d59107f7SMichael Tuexen 			in6_sin_2_v4mapsin6((struct sockaddr_in *)sa,
3316d59107f7SMichael Tuexen 			    (struct sockaddr_in6 *)&spc->spc_aaddr);
3317d59107f7SMichael Tuexen 		} else {
3318f8829a4aSRandall Stewart 			memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
3319d59107f7SMichael Tuexen 		}
3320d59107f7SMichael Tuexen #else
3321d59107f7SMichael Tuexen 		memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
3322d59107f7SMichael Tuexen #endif
33235e2c2d87SRandall Stewart 		break;
3324ea5eba11SMichael Tuexen #endif
33255e2c2d87SRandall Stewart #ifdef INET6
33265e2c2d87SRandall Stewart 	case AF_INET6:
33275e2c2d87SRandall Stewart 		{
3328f42a358aSRandall Stewart 			struct sockaddr_in6 *sin6;
3329f42a358aSRandall Stewart 
3330f8829a4aSRandall Stewart 			memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in6));
3331f42a358aSRandall Stewart 
3332f42a358aSRandall Stewart 			sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
3333f42a358aSRandall Stewart 			if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
333442551e99SRandall Stewart 				if (sin6->sin6_scope_id == 0) {
333542551e99SRandall Stewart 					/* recover scope_id for user */
3336f42a358aSRandall Stewart 					(void)sa6_recoverscope(sin6);
333742551e99SRandall Stewart 				} else {
333842551e99SRandall Stewart 					/* clear embedded scope_id for user */
333942551e99SRandall Stewart 					in6_clearscope(&sin6->sin6_addr);
334042551e99SRandall Stewart 				}
3341f42a358aSRandall Stewart 			}
33425e2c2d87SRandall Stewart 			break;
33435e2c2d87SRandall Stewart 		}
33445e2c2d87SRandall Stewart #endif
33455e2c2d87SRandall Stewart 	default:
33465e2c2d87SRandall Stewart 		/* TSNH */
33475e2c2d87SRandall Stewart 		break;
3348f8829a4aSRandall Stewart 	}
3349f8829a4aSRandall Stewart 	spc->spc_state = state;
3350f8829a4aSRandall Stewart 	spc->spc_error = error;
3351f8829a4aSRandall Stewart 	spc->spc_assoc_id = sctp_get_associd(stcb);
3352f8829a4aSRandall Stewart 
3353139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_paddr_change);
3354139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3355f8829a4aSRandall Stewart 
3356f8829a4aSRandall Stewart 	/* append to socket */
3357f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
33587215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3359f8829a4aSRandall Stewart 	    m_notify);
3360f8829a4aSRandall Stewart 	if (control == NULL) {
3361f8829a4aSRandall Stewart 		/* no memory */
3362f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3363f8829a4aSRandall Stewart 		return;
3364f8829a4aSRandall Stewart 	}
3365139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3366139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3367f8829a4aSRandall Stewart 	/* not that we need this */
3368f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3369f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3370f8829a4aSRandall Stewart 	    control,
3371cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1,
3372cfde3ff7SRandall Stewart 	    SCTP_READ_LOCK_NOT_HELD,
33733cb3567dSMichael Tuexen 	    so_locked);
3374f8829a4aSRandall Stewart }
3375f8829a4aSRandall Stewart 
3376f8829a4aSRandall Stewart 
3377f8829a4aSRandall Stewart static void
33781edc9dbaSMichael Tuexen sctp_notify_send_failed(struct sctp_tcb *stcb, uint8_t sent, uint32_t error,
3379ceaad40aSRandall Stewart     struct sctp_tmit_chunk *chk, int so_locked
3380ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3381ceaad40aSRandall Stewart     SCTP_UNUSED
3382ceaad40aSRandall Stewart #endif
3383ceaad40aSRandall Stewart )
3384f8829a4aSRandall Stewart {
3385830d754dSRandall Stewart 	struct mbuf *m_notify;
3386f8829a4aSRandall Stewart 	struct sctp_send_failed *ssf;
33879935403aSMichael Tuexen 	struct sctp_send_failed_event *ssfe;
3388f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3389ab337314SMichael Tuexen 	struct sctp_chunkhdr *chkhdr;
3390ab337314SMichael Tuexen 	int notifhdr_len, chk_len, chkhdr_len, padding_len, payload_len;
3391f8829a4aSRandall Stewart 
339260990c0cSMichael Tuexen 	if ((stcb == NULL) ||
33939935403aSMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
33949935403aSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
3395f8829a4aSRandall Stewart 		/* event not enabled */
3396f8829a4aSRandall Stewart 		return;
3397830d754dSRandall Stewart 	}
33980053ed28SMichael Tuexen 
33999935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3400ab337314SMichael Tuexen 		notifhdr_len = sizeof(struct sctp_send_failed_event);
34019935403aSMichael Tuexen 	} else {
3402ab337314SMichael Tuexen 		notifhdr_len = sizeof(struct sctp_send_failed);
34039935403aSMichael Tuexen 	}
3404ab337314SMichael Tuexen 	m_notify = sctp_get_mbuf_for_msg(notifhdr_len, 0, M_NOWAIT, 1, MT_DATA);
3405f8829a4aSRandall Stewart 	if (m_notify == NULL)
3406f8829a4aSRandall Stewart 		/* no space left */
3407f8829a4aSRandall Stewart 		return;
3408ab337314SMichael Tuexen 	SCTP_BUF_LEN(m_notify) = notifhdr_len;
3409ab337314SMichael Tuexen 	if (stcb->asoc.idata_supported) {
3410ab337314SMichael Tuexen 		chkhdr_len = sizeof(struct sctp_idata_chunk);
3411ab337314SMichael Tuexen 	} else {
3412ab337314SMichael Tuexen 		chkhdr_len = sizeof(struct sctp_data_chunk);
3413ab337314SMichael Tuexen 	}
3414ab337314SMichael Tuexen 	/* Use some defaults in case we can't access the chunk header */
3415ab337314SMichael Tuexen 	if (chk->send_size >= chkhdr_len) {
3416ab337314SMichael Tuexen 		payload_len = chk->send_size - chkhdr_len;
3417ab337314SMichael Tuexen 	} else {
3418ab337314SMichael Tuexen 		payload_len = 0;
3419ab337314SMichael Tuexen 	}
3420ab337314SMichael Tuexen 	padding_len = 0;
3421ab337314SMichael Tuexen 	if (chk->data != NULL) {
3422ab337314SMichael Tuexen 		chkhdr = mtod(chk->data, struct sctp_chunkhdr *);
3423ab337314SMichael Tuexen 		if (chkhdr != NULL) {
3424ab337314SMichael Tuexen 			chk_len = ntohs(chkhdr->chunk_length);
3425ab337314SMichael Tuexen 			if ((chk_len >= chkhdr_len) &&
3426ab337314SMichael Tuexen 			    (chk->send_size >= chk_len) &&
3427ab337314SMichael Tuexen 			    (chk->send_size - chk_len < 4)) {
3428ab337314SMichael Tuexen 				padding_len = chk->send_size - chk_len;
3429ab337314SMichael Tuexen 				payload_len = chk->send_size - chkhdr_len - padding_len;
3430ab337314SMichael Tuexen 			}
3431ab337314SMichael Tuexen 		}
3432ab337314SMichael Tuexen 	}
34339935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
34349935403aSMichael Tuexen 		ssfe = mtod(m_notify, struct sctp_send_failed_event *);
3435ab337314SMichael Tuexen 		memset(ssfe, 0, notifhdr_len);
34369935403aSMichael Tuexen 		ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
34371edc9dbaSMichael Tuexen 		if (sent) {
34389935403aSMichael Tuexen 			ssfe->ssfe_flags = SCTP_DATA_SENT;
34391edc9dbaSMichael Tuexen 		} else {
34401edc9dbaSMichael Tuexen 			ssfe->ssfe_flags = SCTP_DATA_UNSENT;
34411edc9dbaSMichael Tuexen 		}
3442ab337314SMichael Tuexen 		ssfe->ssfe_length = (uint32_t)(notifhdr_len + payload_len);
34439935403aSMichael Tuexen 		ssfe->ssfe_error = error;
34449935403aSMichael Tuexen 		/* not exactly what the user sent in, but should be close :) */
344549656eefSMichael Tuexen 		ssfe->ssfe_info.snd_sid = chk->rec.data.sid;
34469935403aSMichael Tuexen 		ssfe->ssfe_info.snd_flags = chk->rec.data.rcv_flags;
344749656eefSMichael Tuexen 		ssfe->ssfe_info.snd_ppid = chk->rec.data.ppid;
34489935403aSMichael Tuexen 		ssfe->ssfe_info.snd_context = chk->rec.data.context;
34499935403aSMichael Tuexen 		ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
34509935403aSMichael Tuexen 		ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
34519935403aSMichael Tuexen 	} else {
3452f8829a4aSRandall Stewart 		ssf = mtod(m_notify, struct sctp_send_failed *);
3453ab337314SMichael Tuexen 		memset(ssf, 0, notifhdr_len);
3454f8829a4aSRandall Stewart 		ssf->ssf_type = SCTP_SEND_FAILED;
34551edc9dbaSMichael Tuexen 		if (sent) {
3456f8829a4aSRandall Stewart 			ssf->ssf_flags = SCTP_DATA_SENT;
34571edc9dbaSMichael Tuexen 		} else {
34581edc9dbaSMichael Tuexen 			ssf->ssf_flags = SCTP_DATA_UNSENT;
34591edc9dbaSMichael Tuexen 		}
3460ab337314SMichael Tuexen 		ssf->ssf_length = (uint32_t)(notifhdr_len + payload_len);
3461f8829a4aSRandall Stewart 		ssf->ssf_error = error;
3462f8829a4aSRandall Stewart 		/* not exactly what the user sent in, but should be close :) */
346349656eefSMichael Tuexen 		ssf->ssf_info.sinfo_stream = chk->rec.data.sid;
346449656eefSMichael Tuexen 		ssf->ssf_info.sinfo_ssn = (uint16_t)chk->rec.data.mid;
3465f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_flags = chk->rec.data.rcv_flags;
346649656eefSMichael Tuexen 		ssf->ssf_info.sinfo_ppid = chk->rec.data.ppid;
3467f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_context = chk->rec.data.context;
3468f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3469f8829a4aSRandall Stewart 		ssf->ssf_assoc_id = sctp_get_associd(stcb);
34709935403aSMichael Tuexen 	}
3471ab337314SMichael Tuexen 	if (chk->data != NULL) {
3472ab337314SMichael Tuexen 		/* Trim off the sctp chunk header (it should be there) */
3473ab337314SMichael Tuexen 		if (chk->send_size == chkhdr_len + payload_len + padding_len) {
3474ab337314SMichael Tuexen 			m_adj(chk->data, chkhdr_len);
3475ab337314SMichael Tuexen 			m_adj(chk->data, -padding_len);
3476830d754dSRandall Stewart 			sctp_mbuf_crush(chk->data);
3477ab337314SMichael Tuexen 			chk->send_size -= (chkhdr_len + padding_len);
3478830d754dSRandall Stewart 		}
3479830d754dSRandall Stewart 	}
3480810ec536SMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = chk->data;
3481f8829a4aSRandall Stewart 	/* Steal off the mbuf */
3482f8829a4aSRandall Stewart 	chk->data = NULL;
3483f8829a4aSRandall Stewart 	/*
3484f8829a4aSRandall Stewart 	 * For this case, we check the actual socket buffer, since the assoc
3485f8829a4aSRandall Stewart 	 * is going away we don't want to overfill the socket buffer for a
3486f8829a4aSRandall Stewart 	 * non-reader
3487f8829a4aSRandall Stewart 	 */
3488139bc87fSRandall Stewart 	if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3489f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3490f8829a4aSRandall Stewart 		return;
3491f8829a4aSRandall Stewart 	}
3492f8829a4aSRandall Stewart 	/* append to socket */
3493f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
34947215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3495f8829a4aSRandall Stewart 	    m_notify);
3496f8829a4aSRandall Stewart 	if (control == NULL) {
3497f8829a4aSRandall Stewart 		/* no memory */
3498f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3499f8829a4aSRandall Stewart 		return;
3500f8829a4aSRandall Stewart 	}
350128cd0699SMichael Tuexen 	control->length = SCTP_BUF_LEN(m_notify);
3502139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
350328cd0699SMichael Tuexen 	/* not that we need this */
350428cd0699SMichael Tuexen 	control->tail_mbuf = m_notify;
3505f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3506f8829a4aSRandall Stewart 	    control,
3507cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1,
3508cfde3ff7SRandall Stewart 	    SCTP_READ_LOCK_NOT_HELD,
3509cfde3ff7SRandall Stewart 	    so_locked);
3510f8829a4aSRandall Stewart }
3511f8829a4aSRandall Stewart 
3512f8829a4aSRandall Stewart 
3513f8829a4aSRandall Stewart static void
3514f8829a4aSRandall Stewart sctp_notify_send_failed2(struct sctp_tcb *stcb, uint32_t error,
3515ceaad40aSRandall Stewart     struct sctp_stream_queue_pending *sp, int so_locked
3516ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3517ceaad40aSRandall Stewart     SCTP_UNUSED
3518ceaad40aSRandall Stewart #endif
3519ceaad40aSRandall Stewart )
3520f8829a4aSRandall Stewart {
3521f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3522f8829a4aSRandall Stewart 	struct sctp_send_failed *ssf;
35239935403aSMichael Tuexen 	struct sctp_send_failed_event *ssfe;
3524f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3525ab337314SMichael Tuexen 	int notifhdr_len;
3526f8829a4aSRandall Stewart 
352760990c0cSMichael Tuexen 	if ((stcb == NULL) ||
35289935403aSMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
35299935403aSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
3530f8829a4aSRandall Stewart 		/* event not enabled */
3531f8829a4aSRandall Stewart 		return;
3532830d754dSRandall Stewart 	}
35339935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3534ab337314SMichael Tuexen 		notifhdr_len = sizeof(struct sctp_send_failed_event);
35359935403aSMichael Tuexen 	} else {
3536ab337314SMichael Tuexen 		notifhdr_len = sizeof(struct sctp_send_failed);
35379935403aSMichael Tuexen 	}
3538ab337314SMichael Tuexen 	m_notify = sctp_get_mbuf_for_msg(notifhdr_len, 0, M_NOWAIT, 1, MT_DATA);
35399935403aSMichael Tuexen 	if (m_notify == NULL) {
3540f8829a4aSRandall Stewart 		/* no space left */
3541f8829a4aSRandall Stewart 		return;
35429935403aSMichael Tuexen 	}
3543ab337314SMichael Tuexen 	SCTP_BUF_LEN(m_notify) = notifhdr_len;
35449935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
35459935403aSMichael Tuexen 		ssfe = mtod(m_notify, struct sctp_send_failed_event *);
3546ab337314SMichael Tuexen 		memset(ssfe, 0, notifhdr_len);
3547ad83c8a5SMichael Tuexen 		ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
35489935403aSMichael Tuexen 		ssfe->ssfe_flags = SCTP_DATA_UNSENT;
3549ab337314SMichael Tuexen 		ssfe->ssfe_length = (uint32_t)(notifhdr_len + sp->length);
35509935403aSMichael Tuexen 		ssfe->ssfe_error = error;
35519935403aSMichael Tuexen 		/* not exactly what the user sent in, but should be close :) */
355249656eefSMichael Tuexen 		ssfe->ssfe_info.snd_sid = sp->sid;
35539935403aSMichael Tuexen 		if (sp->some_taken) {
35549935403aSMichael Tuexen 			ssfe->ssfe_info.snd_flags = SCTP_DATA_LAST_FRAG;
35559935403aSMichael Tuexen 		} else {
35569935403aSMichael Tuexen 			ssfe->ssfe_info.snd_flags = SCTP_DATA_NOT_FRAG;
35579935403aSMichael Tuexen 		}
35589935403aSMichael Tuexen 		ssfe->ssfe_info.snd_ppid = sp->ppid;
35599935403aSMichael Tuexen 		ssfe->ssfe_info.snd_context = sp->context;
35609935403aSMichael Tuexen 		ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
35619935403aSMichael Tuexen 		ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
35629935403aSMichael Tuexen 	} else {
3563f8829a4aSRandall Stewart 		ssf = mtod(m_notify, struct sctp_send_failed *);
3564ab337314SMichael Tuexen 		memset(ssf, 0, notifhdr_len);
3565f8829a4aSRandall Stewart 		ssf->ssf_type = SCTP_SEND_FAILED;
3566f8829a4aSRandall Stewart 		ssf->ssf_flags = SCTP_DATA_UNSENT;
3567ab337314SMichael Tuexen 		ssf->ssf_length = (uint32_t)(notifhdr_len + sp->length);
3568f8829a4aSRandall Stewart 		ssf->ssf_error = error;
3569f8829a4aSRandall Stewart 		/* not exactly what the user sent in, but should be close :) */
357049656eefSMichael Tuexen 		ssf->ssf_info.sinfo_stream = sp->sid;
3571f3b05218SMichael Tuexen 		ssf->ssf_info.sinfo_ssn = 0;
3572fc14de76SRandall Stewart 		if (sp->some_taken) {
3573fc14de76SRandall Stewart 			ssf->ssf_info.sinfo_flags = SCTP_DATA_LAST_FRAG;
3574fc14de76SRandall Stewart 		} else {
3575fc14de76SRandall Stewart 			ssf->ssf_info.sinfo_flags = SCTP_DATA_NOT_FRAG;
3576fc14de76SRandall Stewart 		}
3577f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_ppid = sp->ppid;
3578f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_context = sp->context;
3579f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3580f8829a4aSRandall Stewart 		ssf->ssf_assoc_id = sctp_get_associd(stcb);
35819935403aSMichael Tuexen 	}
35829935403aSMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = sp->data;
3583f8829a4aSRandall Stewart 
3584f8829a4aSRandall Stewart 	/* Steal off the mbuf */
3585f8829a4aSRandall Stewart 	sp->data = NULL;
3586f8829a4aSRandall Stewart 	/*
3587f8829a4aSRandall Stewart 	 * For this case, we check the actual socket buffer, since the assoc
3588f8829a4aSRandall Stewart 	 * is going away we don't want to overfill the socket buffer for a
3589f8829a4aSRandall Stewart 	 * non-reader
3590f8829a4aSRandall Stewart 	 */
3591139bc87fSRandall Stewart 	if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3592f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3593f8829a4aSRandall Stewart 		return;
3594f8829a4aSRandall Stewart 	}
3595f8829a4aSRandall Stewart 	/* append to socket */
3596f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
35977215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3598f8829a4aSRandall Stewart 	    m_notify);
3599f8829a4aSRandall Stewart 	if (control == NULL) {
3600f8829a4aSRandall Stewart 		/* no memory */
3601f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3602f8829a4aSRandall Stewart 		return;
3603f8829a4aSRandall Stewart 	}
360428cd0699SMichael Tuexen 	control->length = SCTP_BUF_LEN(m_notify);
3605139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
360628cd0699SMichael Tuexen 	/* not that we need this */
360728cd0699SMichael Tuexen 	control->tail_mbuf = m_notify;
3608f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3609f8829a4aSRandall Stewart 	    control,
3610cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3611f8829a4aSRandall Stewart }
3612f8829a4aSRandall Stewart 
3613f8829a4aSRandall Stewart 
3614f8829a4aSRandall Stewart 
3615f8829a4aSRandall Stewart static void
36167215cc1bSMichael Tuexen sctp_notify_adaptation_layer(struct sctp_tcb *stcb)
3617f8829a4aSRandall Stewart {
3618f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3619f8829a4aSRandall Stewart 	struct sctp_adaptation_event *sai;
3620f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3621f8829a4aSRandall Stewart 
362260990c0cSMichael Tuexen 	if ((stcb == NULL) ||
362360990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT)) {
3624f8829a4aSRandall Stewart 		/* event not enabled */
3625f8829a4aSRandall Stewart 		return;
3626830d754dSRandall Stewart 	}
36270053ed28SMichael Tuexen 
3628eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 0, M_NOWAIT, 1, MT_DATA);
3629f8829a4aSRandall Stewart 	if (m_notify == NULL)
3630f8829a4aSRandall Stewart 		/* no space left */
3631f8829a4aSRandall Stewart 		return;
3632139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3633f8829a4aSRandall Stewart 	sai = mtod(m_notify, struct sctp_adaptation_event *);
3634e432298aSXin LI 	memset(sai, 0, sizeof(struct sctp_adaptation_event));
3635f8829a4aSRandall Stewart 	sai->sai_type = SCTP_ADAPTATION_INDICATION;
3636f8829a4aSRandall Stewart 	sai->sai_flags = 0;
3637f8829a4aSRandall Stewart 	sai->sai_length = sizeof(struct sctp_adaptation_event);
36382afb3e84SRandall Stewart 	sai->sai_adaptation_ind = stcb->asoc.peers_adaptation;
3639f8829a4aSRandall Stewart 	sai->sai_assoc_id = sctp_get_associd(stcb);
3640f8829a4aSRandall Stewart 
3641139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_adaptation_event);
3642139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3643f8829a4aSRandall Stewart 
3644f8829a4aSRandall Stewart 	/* append to socket */
3645f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
36467215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3647f8829a4aSRandall Stewart 	    m_notify);
3648f8829a4aSRandall Stewart 	if (control == NULL) {
3649f8829a4aSRandall Stewart 		/* no memory */
3650f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3651f8829a4aSRandall Stewart 		return;
3652f8829a4aSRandall Stewart 	}
3653139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3654139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3655f8829a4aSRandall Stewart 	/* not that we need this */
3656f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3657f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3658f8829a4aSRandall Stewart 	    control,
3659cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3660f8829a4aSRandall Stewart }
3661f8829a4aSRandall Stewart 
366203b0b021SRandall Stewart /* This always must be called with the read-queue LOCKED in the INP */
3663810ec536SMichael Tuexen static void
36642dad8a55SRandall Stewart sctp_notify_partial_delivery_indication(struct sctp_tcb *stcb, uint32_t error,
3665810ec536SMichael Tuexen     uint32_t val, int so_locked
3666810ec536SMichael Tuexen #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3667810ec536SMichael Tuexen     SCTP_UNUSED
3668810ec536SMichael Tuexen #endif
3669810ec536SMichael Tuexen )
3670f8829a4aSRandall Stewart {
3671f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3672f8829a4aSRandall Stewart 	struct sctp_pdapi_event *pdapi;
3673f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
367403b0b021SRandall Stewart 	struct sockbuf *sb;
3675f8829a4aSRandall Stewart 
367660990c0cSMichael Tuexen 	if ((stcb == NULL) ||
367760990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_PDAPIEVNT)) {
3678f8829a4aSRandall Stewart 		/* event not enabled */
3679f8829a4aSRandall Stewart 		return;
3680830d754dSRandall Stewart 	}
3681cd1386abSMichael Tuexen 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
3682cd1386abSMichael Tuexen 		return;
3683cd1386abSMichael Tuexen 	}
36840053ed28SMichael Tuexen 
3685eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_pdapi_event), 0, M_NOWAIT, 1, MT_DATA);
3686f8829a4aSRandall Stewart 	if (m_notify == NULL)
3687f8829a4aSRandall Stewart 		/* no space left */
3688f8829a4aSRandall Stewart 		return;
3689139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3690f8829a4aSRandall Stewart 	pdapi = mtod(m_notify, struct sctp_pdapi_event *);
3691e432298aSXin LI 	memset(pdapi, 0, sizeof(struct sctp_pdapi_event));
3692f8829a4aSRandall Stewart 	pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
3693f8829a4aSRandall Stewart 	pdapi->pdapi_flags = 0;
3694f8829a4aSRandall Stewart 	pdapi->pdapi_length = sizeof(struct sctp_pdapi_event);
3695f8829a4aSRandall Stewart 	pdapi->pdapi_indication = error;
36969a6142d8SRandall Stewart 	pdapi->pdapi_stream = (val >> 16);
36979a6142d8SRandall Stewart 	pdapi->pdapi_seq = (val & 0x0000ffff);
3698f8829a4aSRandall Stewart 	pdapi->pdapi_assoc_id = sctp_get_associd(stcb);
3699f8829a4aSRandall Stewart 
3700139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_pdapi_event);
3701139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3702f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
37037215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3704f8829a4aSRandall Stewart 	    m_notify);
3705f8829a4aSRandall Stewart 	if (control == NULL) {
3706f8829a4aSRandall Stewart 		/* no memory */
3707f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3708f8829a4aSRandall Stewart 		return;
3709f8829a4aSRandall Stewart 	}
3710139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
371128cd0699SMichael Tuexen 	control->spec_flags = M_NOTIFICATION;
3712f8829a4aSRandall Stewart 	/* not that we need this */
3713f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
371403b0b021SRandall Stewart 	sb = &stcb->sctp_socket->so_rcv;
3715b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3716139bc87fSRandall Stewart 		sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m_notify));
371780fefe0aSRandall Stewart 	}
371803b0b021SRandall Stewart 	sctp_sballoc(stcb, sb, m_notify);
3719b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
372003b0b021SRandall Stewart 		sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
372180fefe0aSRandall Stewart 	}
372203b0b021SRandall Stewart 	control->end_added = 1;
372303b0b021SRandall Stewart 	if (stcb->asoc.control_pdapi)
372403b0b021SRandall Stewart 		TAILQ_INSERT_AFTER(&stcb->sctp_ep->read_queue, stcb->asoc.control_pdapi, control, next);
372503b0b021SRandall Stewart 	else {
372603b0b021SRandall Stewart 		/* we really should not see this case */
372703b0b021SRandall Stewart 		TAILQ_INSERT_TAIL(&stcb->sctp_ep->read_queue, control, next);
372803b0b021SRandall Stewart 	}
372903b0b021SRandall Stewart 	if (stcb->sctp_ep && stcb->sctp_socket) {
373003b0b021SRandall Stewart 		/* This should always be the case */
3731810ec536SMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3732810ec536SMichael Tuexen 		struct socket *so;
3733810ec536SMichael Tuexen 
3734810ec536SMichael Tuexen 		so = SCTP_INP_SO(stcb->sctp_ep);
3735810ec536SMichael Tuexen 		if (!so_locked) {
3736810ec536SMichael Tuexen 			atomic_add_int(&stcb->asoc.refcnt, 1);
3737810ec536SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
3738810ec536SMichael Tuexen 			SCTP_SOCKET_LOCK(so, 1);
3739810ec536SMichael Tuexen 			SCTP_TCB_LOCK(stcb);
3740810ec536SMichael Tuexen 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
3741810ec536SMichael Tuexen 			if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3742810ec536SMichael Tuexen 				SCTP_SOCKET_UNLOCK(so, 1);
3743810ec536SMichael Tuexen 				return;
3744810ec536SMichael Tuexen 			}
3745810ec536SMichael Tuexen 		}
3746810ec536SMichael Tuexen #endif
374703b0b021SRandall Stewart 		sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
3748810ec536SMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3749810ec536SMichael Tuexen 		if (!so_locked) {
3750810ec536SMichael Tuexen 			SCTP_SOCKET_UNLOCK(so, 1);
3751810ec536SMichael Tuexen 		}
3752810ec536SMichael Tuexen #endif
3753f8829a4aSRandall Stewart 	}
3754f8829a4aSRandall Stewart }
3755f8829a4aSRandall Stewart 
3756f8829a4aSRandall Stewart static void
3757f8829a4aSRandall Stewart sctp_notify_shutdown_event(struct sctp_tcb *stcb)
3758f8829a4aSRandall Stewart {
3759f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3760f8829a4aSRandall Stewart 	struct sctp_shutdown_event *sse;
3761f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3762f8829a4aSRandall Stewart 
3763f8829a4aSRandall Stewart 	/*
3764f8829a4aSRandall Stewart 	 * For TCP model AND UDP connected sockets we will send an error up
3765f8829a4aSRandall Stewart 	 * when an SHUTDOWN completes
3766f8829a4aSRandall Stewart 	 */
3767f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3768f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3769f8829a4aSRandall Stewart 		/* mark socket closed for read/write and wakeup! */
3770ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3771ceaad40aSRandall Stewart 		struct socket *so;
3772ceaad40aSRandall Stewart 
3773ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
3774ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
3775ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3776ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
3777ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
3778ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
3779ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3780ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
3781ceaad40aSRandall Stewart 			return;
3782ceaad40aSRandall Stewart 		}
3783ceaad40aSRandall Stewart #endif
3784f8829a4aSRandall Stewart 		socantsendmore(stcb->sctp_socket);
3785ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3786ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
3787ceaad40aSRandall Stewart #endif
3788f8829a4aSRandall Stewart 	}
3789e2e7c62eSMichael Tuexen 	if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)) {
3790f8829a4aSRandall Stewart 		/* event not enabled */
3791f8829a4aSRandall Stewart 		return;
3792830d754dSRandall Stewart 	}
37930053ed28SMichael Tuexen 
3794eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_event), 0, M_NOWAIT, 1, MT_DATA);
3795f8829a4aSRandall Stewart 	if (m_notify == NULL)
3796f8829a4aSRandall Stewart 		/* no space left */
3797f8829a4aSRandall Stewart 		return;
3798f8829a4aSRandall Stewart 	sse = mtod(m_notify, struct sctp_shutdown_event *);
3799e432298aSXin LI 	memset(sse, 0, sizeof(struct sctp_shutdown_event));
3800f8829a4aSRandall Stewart 	sse->sse_type = SCTP_SHUTDOWN_EVENT;
3801f8829a4aSRandall Stewart 	sse->sse_flags = 0;
3802f8829a4aSRandall Stewart 	sse->sse_length = sizeof(struct sctp_shutdown_event);
3803f8829a4aSRandall Stewart 	sse->sse_assoc_id = sctp_get_associd(stcb);
3804f8829a4aSRandall Stewart 
3805139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_shutdown_event);
3806139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3807f8829a4aSRandall Stewart 
3808f8829a4aSRandall Stewart 	/* append to socket */
3809f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
38107215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3811f8829a4aSRandall Stewart 	    m_notify);
3812f8829a4aSRandall Stewart 	if (control == NULL) {
3813f8829a4aSRandall Stewart 		/* no memory */
3814f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3815f8829a4aSRandall Stewart 		return;
3816f8829a4aSRandall Stewart 	}
3817139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
381828cd0699SMichael Tuexen 	control->spec_flags = M_NOTIFICATION;
3819f8829a4aSRandall Stewart 	/* not that we need this */
3820f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3821f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3822f8829a4aSRandall Stewart 	    control,
3823cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3824f8829a4aSRandall Stewart }
3825f8829a4aSRandall Stewart 
3826f8829a4aSRandall Stewart static void
3827830d754dSRandall Stewart sctp_notify_sender_dry_event(struct sctp_tcb *stcb,
3828830d754dSRandall Stewart     int so_locked
3829830d754dSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3830830d754dSRandall Stewart     SCTP_UNUSED
3831830d754dSRandall Stewart #endif
3832830d754dSRandall Stewart )
3833830d754dSRandall Stewart {
3834830d754dSRandall Stewart 	struct mbuf *m_notify;
3835830d754dSRandall Stewart 	struct sctp_sender_dry_event *event;
3836830d754dSRandall Stewart 	struct sctp_queued_to_read *control;
3837830d754dSRandall Stewart 
383860990c0cSMichael Tuexen 	if ((stcb == NULL) ||
383960990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DRYEVNT)) {
3840830d754dSRandall Stewart 		/* event not enabled */
3841830d754dSRandall Stewart 		return;
3842830d754dSRandall Stewart 	}
38430053ed28SMichael Tuexen 
3844eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_sender_dry_event), 0, M_NOWAIT, 1, MT_DATA);
3845830d754dSRandall Stewart 	if (m_notify == NULL) {
3846830d754dSRandall Stewart 		/* no space left */
3847830d754dSRandall Stewart 		return;
3848830d754dSRandall Stewart 	}
3849830d754dSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3850830d754dSRandall Stewart 	event = mtod(m_notify, struct sctp_sender_dry_event *);
3851e432298aSXin LI 	memset(event, 0, sizeof(struct sctp_sender_dry_event));
3852830d754dSRandall Stewart 	event->sender_dry_type = SCTP_SENDER_DRY_EVENT;
3853830d754dSRandall Stewart 	event->sender_dry_flags = 0;
3854830d754dSRandall Stewart 	event->sender_dry_length = sizeof(struct sctp_sender_dry_event);
3855830d754dSRandall Stewart 	event->sender_dry_assoc_id = sctp_get_associd(stcb);
3856830d754dSRandall Stewart 
3857830d754dSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_sender_dry_event);
3858830d754dSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3859830d754dSRandall Stewart 
3860830d754dSRandall Stewart 	/* append to socket */
3861830d754dSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
38627215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
38637215cc1bSMichael Tuexen 	    m_notify);
3864830d754dSRandall Stewart 	if (control == NULL) {
3865830d754dSRandall Stewart 		/* no memory */
3866830d754dSRandall Stewart 		sctp_m_freem(m_notify);
3867830d754dSRandall Stewart 		return;
3868830d754dSRandall Stewart 	}
3869830d754dSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3870830d754dSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3871830d754dSRandall Stewart 	/* not that we need this */
3872830d754dSRandall Stewart 	control->tail_mbuf = m_notify;
3873830d754dSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3874cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3875830d754dSRandall Stewart }
3876830d754dSRandall Stewart 
3877ea44232bSRandall Stewart 
3878c4e848b7SRandall Stewart void
3879c4e848b7SRandall Stewart sctp_notify_stream_reset_add(struct sctp_tcb *stcb, uint16_t numberin, uint16_t numberout, int flag)
3880ea44232bSRandall Stewart {
3881ea44232bSRandall Stewart 	struct mbuf *m_notify;
3882ea44232bSRandall Stewart 	struct sctp_queued_to_read *control;
3883c4e848b7SRandall Stewart 	struct sctp_stream_change_event *stradd;
3884ea44232bSRandall Stewart 
38858c501e51SMichael Tuexen 	if ((stcb == NULL) ||
38868c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_CHANGEEVNT))) {
3887ea44232bSRandall Stewart 		/* event not enabled */
3888ea44232bSRandall Stewart 		return;
3889ea44232bSRandall Stewart 	}
3890c4e848b7SRandall Stewart 	if ((stcb->asoc.peer_req_out) && flag) {
3891c4e848b7SRandall Stewart 		/* Peer made the request, don't tell the local user */
3892c4e848b7SRandall Stewart 		stcb->asoc.peer_req_out = 0;
3893c4e848b7SRandall Stewart 		return;
3894c4e848b7SRandall Stewart 	}
3895c4e848b7SRandall Stewart 	stcb->asoc.peer_req_out = 0;
3896e432298aSXin LI 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_stream_change_event), 0, M_NOWAIT, 1, MT_DATA);
3897ea44232bSRandall Stewart 	if (m_notify == NULL)
3898ea44232bSRandall Stewart 		/* no space left */
3899ea44232bSRandall Stewart 		return;
3900ea44232bSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3901c4e848b7SRandall Stewart 	stradd = mtod(m_notify, struct sctp_stream_change_event *);
3902e432298aSXin LI 	memset(stradd, 0, sizeof(struct sctp_stream_change_event));
3903c4e848b7SRandall Stewart 	stradd->strchange_type = SCTP_STREAM_CHANGE_EVENT;
3904c4e848b7SRandall Stewart 	stradd->strchange_flags = flag;
3905e432298aSXin LI 	stradd->strchange_length = sizeof(struct sctp_stream_change_event);
3906c4e848b7SRandall Stewart 	stradd->strchange_assoc_id = sctp_get_associd(stcb);
3907c4e848b7SRandall Stewart 	stradd->strchange_instrms = numberin;
3908c4e848b7SRandall Stewart 	stradd->strchange_outstrms = numberout;
3909e432298aSXin LI 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_stream_change_event);
3910ea44232bSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3911ea44232bSRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3912ea44232bSRandall Stewart 		/* no space */
3913ea44232bSRandall Stewart 		sctp_m_freem(m_notify);
3914ea44232bSRandall Stewart 		return;
3915ea44232bSRandall Stewart 	}
3916ea44232bSRandall Stewart 	/* append to socket */
3917ea44232bSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
39187215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3919ea44232bSRandall Stewart 	    m_notify);
3920ea44232bSRandall Stewart 	if (control == NULL) {
3921ea44232bSRandall Stewart 		/* no memory */
3922ea44232bSRandall Stewart 		sctp_m_freem(m_notify);
3923ea44232bSRandall Stewart 		return;
3924ea44232bSRandall Stewart 	}
3925ea44232bSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
392628cd0699SMichael Tuexen 	control->spec_flags = M_NOTIFICATION;
3927ea44232bSRandall Stewart 	/* not that we need this */
3928ea44232bSRandall Stewart 	control->tail_mbuf = m_notify;
3929ea44232bSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3930ea44232bSRandall Stewart 	    control,
3931cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3932ea44232bSRandall Stewart }
3933ea44232bSRandall Stewart 
3934c4e848b7SRandall Stewart void
3935c4e848b7SRandall Stewart sctp_notify_stream_reset_tsn(struct sctp_tcb *stcb, uint32_t sending_tsn, uint32_t recv_tsn, int flag)
3936c4e848b7SRandall Stewart {
3937c4e848b7SRandall Stewart 	struct mbuf *m_notify;
3938c4e848b7SRandall Stewart 	struct sctp_queued_to_read *control;
3939c4e848b7SRandall Stewart 	struct sctp_assoc_reset_event *strasoc;
3940c4e848b7SRandall Stewart 
39418c501e51SMichael Tuexen 	if ((stcb == NULL) ||
39428c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ASSOC_RESETEVNT))) {
3943c4e848b7SRandall Stewart 		/* event not enabled */
3944c4e848b7SRandall Stewart 		return;
3945c4e848b7SRandall Stewart 	}
3946e432298aSXin LI 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_assoc_reset_event), 0, M_NOWAIT, 1, MT_DATA);
3947c4e848b7SRandall Stewart 	if (m_notify == NULL)
3948c4e848b7SRandall Stewart 		/* no space left */
3949c4e848b7SRandall Stewart 		return;
3950c4e848b7SRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3951c4e848b7SRandall Stewart 	strasoc = mtod(m_notify, struct sctp_assoc_reset_event *);
3952e432298aSXin LI 	memset(strasoc, 0, sizeof(struct sctp_assoc_reset_event));
3953c4e848b7SRandall Stewart 	strasoc->assocreset_type = SCTP_ASSOC_RESET_EVENT;
3954c4e848b7SRandall Stewart 	strasoc->assocreset_flags = flag;
3955e432298aSXin LI 	strasoc->assocreset_length = sizeof(struct sctp_assoc_reset_event);
3956c4e848b7SRandall Stewart 	strasoc->assocreset_assoc_id = sctp_get_associd(stcb);
3957c4e848b7SRandall Stewart 	strasoc->assocreset_local_tsn = sending_tsn;
3958c4e848b7SRandall Stewart 	strasoc->assocreset_remote_tsn = recv_tsn;
3959e432298aSXin LI 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_assoc_reset_event);
3960c4e848b7SRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3961c4e848b7SRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3962c4e848b7SRandall Stewart 		/* no space */
3963c4e848b7SRandall Stewart 		sctp_m_freem(m_notify);
3964c4e848b7SRandall Stewart 		return;
3965c4e848b7SRandall Stewart 	}
3966c4e848b7SRandall Stewart 	/* append to socket */
3967c4e848b7SRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3968c4e848b7SRandall Stewart 	    0, 0, stcb->asoc.context, 0, 0, 0,
3969c4e848b7SRandall Stewart 	    m_notify);
3970c4e848b7SRandall Stewart 	if (control == NULL) {
3971c4e848b7SRandall Stewart 		/* no memory */
3972c4e848b7SRandall Stewart 		sctp_m_freem(m_notify);
3973c4e848b7SRandall Stewart 		return;
3974c4e848b7SRandall Stewart 	}
3975c4e848b7SRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
397628cd0699SMichael Tuexen 	control->spec_flags = M_NOTIFICATION;
3977c4e848b7SRandall Stewart 	/* not that we need this */
3978c4e848b7SRandall Stewart 	control->tail_mbuf = m_notify;
3979c4e848b7SRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3980c4e848b7SRandall Stewart 	    control,
3981c4e848b7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3982c4e848b7SRandall Stewart }
3983c4e848b7SRandall Stewart 
3984c4e848b7SRandall Stewart 
3985ea44232bSRandall Stewart 
3986830d754dSRandall Stewart static void
3987f8829a4aSRandall Stewart sctp_notify_stream_reset(struct sctp_tcb *stcb,
3988f8829a4aSRandall Stewart     int number_entries, uint16_t *list, int flag)
3989f8829a4aSRandall Stewart {
3990f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3991f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3992f8829a4aSRandall Stewart 	struct sctp_stream_reset_event *strreset;
3993f8829a4aSRandall Stewart 	int len;
3994f8829a4aSRandall Stewart 
39958c501e51SMichael Tuexen 	if ((stcb == NULL) ||
39968c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT))) {
3997f8829a4aSRandall Stewart 		/* event not enabled */
3998f8829a4aSRandall Stewart 		return;
3999830d754dSRandall Stewart 	}
40000053ed28SMichael Tuexen 
4001eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
4002f8829a4aSRandall Stewart 	if (m_notify == NULL)
4003f8829a4aSRandall Stewart 		/* no space left */
4004f8829a4aSRandall Stewart 		return;
4005139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
4006f8829a4aSRandall Stewart 	len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
4007f8829a4aSRandall Stewart 	if (len > M_TRAILINGSPACE(m_notify)) {
4008f8829a4aSRandall Stewart 		/* never enough room */
4009f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
4010f8829a4aSRandall Stewart 		return;
4011f8829a4aSRandall Stewart 	}
4012f8829a4aSRandall Stewart 	strreset = mtod(m_notify, struct sctp_stream_reset_event *);
4013e432298aSXin LI 	memset(strreset, 0, len);
4014f8829a4aSRandall Stewart 	strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
4015c4e848b7SRandall Stewart 	strreset->strreset_flags = flag;
4016f8829a4aSRandall Stewart 	strreset->strreset_length = len;
4017f8829a4aSRandall Stewart 	strreset->strreset_assoc_id = sctp_get_associd(stcb);
4018f8829a4aSRandall Stewart 	if (number_entries) {
4019f8829a4aSRandall Stewart 		int i;
4020f8829a4aSRandall Stewart 
4021f8829a4aSRandall Stewart 		for (i = 0; i < number_entries; i++) {
4022c4e848b7SRandall Stewart 			strreset->strreset_stream_list[i] = ntohs(list[i]);
4023f8829a4aSRandall Stewart 		}
4024f8829a4aSRandall Stewart 	}
4025139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = len;
4026139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
4027139bc87fSRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
4028f8829a4aSRandall Stewart 		/* no space */
4029f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
4030f8829a4aSRandall Stewart 		return;
4031f8829a4aSRandall Stewart 	}
4032f8829a4aSRandall Stewart 	/* append to socket */
4033f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
40347215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
4035f8829a4aSRandall Stewart 	    m_notify);
4036f8829a4aSRandall Stewart 	if (control == NULL) {
4037f8829a4aSRandall Stewart 		/* no memory */
4038f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
4039f8829a4aSRandall Stewart 		return;
4040f8829a4aSRandall Stewart 	}
4041139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
404228cd0699SMichael Tuexen 	control->spec_flags = M_NOTIFICATION;
4043f8829a4aSRandall Stewart 	/* not that we need this */
4044f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
4045f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
4046f8829a4aSRandall Stewart 	    control,
4047cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
4048f8829a4aSRandall Stewart }
4049f8829a4aSRandall Stewart 
4050f8829a4aSRandall Stewart 
4051389b1b11SMichael Tuexen static void
4052389b1b11SMichael Tuexen sctp_notify_remote_error(struct sctp_tcb *stcb, uint16_t error, struct sctp_error_chunk *chunk)
4053389b1b11SMichael Tuexen {
4054389b1b11SMichael Tuexen 	struct mbuf *m_notify;
4055389b1b11SMichael Tuexen 	struct sctp_remote_error *sre;
4056389b1b11SMichael Tuexen 	struct sctp_queued_to_read *control;
40579a8e3088SMichael Tuexen 	unsigned int notif_len;
40589a8e3088SMichael Tuexen 	uint16_t chunk_len;
4059389b1b11SMichael Tuexen 
4060389b1b11SMichael Tuexen 	if ((stcb == NULL) ||
4061389b1b11SMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPEERERR)) {
4062389b1b11SMichael Tuexen 		return;
4063389b1b11SMichael Tuexen 	}
4064389b1b11SMichael Tuexen 	if (chunk != NULL) {
4065c9eb4473SMichael Tuexen 		chunk_len = ntohs(chunk->ch.chunk_length);
40669669e724SMichael Tuexen 		/*
40679669e724SMichael Tuexen 		 * Only SCTP_CHUNK_BUFFER_SIZE are guaranteed to be
406845d41de5SMichael Tuexen 		 * contiguous.
40699669e724SMichael Tuexen 		 */
40709669e724SMichael Tuexen 		if (chunk_len > SCTP_CHUNK_BUFFER_SIZE) {
40719669e724SMichael Tuexen 			chunk_len = SCTP_CHUNK_BUFFER_SIZE;
40729669e724SMichael Tuexen 		}
4073389b1b11SMichael Tuexen 	} else {
4074389b1b11SMichael Tuexen 		chunk_len = 0;
4075389b1b11SMichael Tuexen 	}
40769a8e3088SMichael Tuexen 	notif_len = (unsigned int)(sizeof(struct sctp_remote_error) + chunk_len);
4077eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
4078389b1b11SMichael Tuexen 	if (m_notify == NULL) {
4079389b1b11SMichael Tuexen 		/* Retry with smaller value. */
40809a8e3088SMichael Tuexen 		notif_len = (unsigned int)sizeof(struct sctp_remote_error);
4081eb1b1807SGleb Smirnoff 		m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
4082389b1b11SMichael Tuexen 		if (m_notify == NULL) {
4083389b1b11SMichael Tuexen 			return;
4084389b1b11SMichael Tuexen 		}
4085389b1b11SMichael Tuexen 	}
4086389b1b11SMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = NULL;
4087389b1b11SMichael Tuexen 	sre = mtod(m_notify, struct sctp_remote_error *);
408856711f94SMichael Tuexen 	memset(sre, 0, notif_len);
4089389b1b11SMichael Tuexen 	sre->sre_type = SCTP_REMOTE_ERROR;
4090389b1b11SMichael Tuexen 	sre->sre_flags = 0;
4091389b1b11SMichael Tuexen 	sre->sre_length = sizeof(struct sctp_remote_error);
4092389b1b11SMichael Tuexen 	sre->sre_error = error;
4093389b1b11SMichael Tuexen 	sre->sre_assoc_id = sctp_get_associd(stcb);
4094389b1b11SMichael Tuexen 	if (notif_len > sizeof(struct sctp_remote_error)) {
4095389b1b11SMichael Tuexen 		memcpy(sre->sre_data, chunk, chunk_len);
4096389b1b11SMichael Tuexen 		sre->sre_length += chunk_len;
4097389b1b11SMichael Tuexen 	}
4098389b1b11SMichael Tuexen 	SCTP_BUF_LEN(m_notify) = sre->sre_length;
4099389b1b11SMichael Tuexen 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
4100389b1b11SMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
4101389b1b11SMichael Tuexen 	    m_notify);
4102389b1b11SMichael Tuexen 	if (control != NULL) {
4103389b1b11SMichael Tuexen 		control->length = SCTP_BUF_LEN(m_notify);
410428cd0699SMichael Tuexen 		control->spec_flags = M_NOTIFICATION;
4105389b1b11SMichael Tuexen 		/* not that we need this */
4106389b1b11SMichael Tuexen 		control->tail_mbuf = m_notify;
4107389b1b11SMichael Tuexen 		sctp_add_to_readq(stcb->sctp_ep, stcb,
4108389b1b11SMichael Tuexen 		    control,
4109389b1b11SMichael Tuexen 		    &stcb->sctp_socket->so_rcv, 1,
4110389b1b11SMichael Tuexen 		    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
4111389b1b11SMichael Tuexen 	} else {
4112389b1b11SMichael Tuexen 		sctp_m_freem(m_notify);
4113389b1b11SMichael Tuexen 	}
4114389b1b11SMichael Tuexen }
4115389b1b11SMichael Tuexen 
4116389b1b11SMichael Tuexen 
4117f8829a4aSRandall Stewart void
4118f8829a4aSRandall Stewart sctp_ulp_notify(uint32_t notification, struct sctp_tcb *stcb,
4119ceaad40aSRandall Stewart     uint32_t error, void *data, int so_locked
4120ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4121ceaad40aSRandall Stewart     SCTP_UNUSED
4122ceaad40aSRandall Stewart #endif
4123ceaad40aSRandall Stewart )
4124f8829a4aSRandall Stewart {
4125830d754dSRandall Stewart 	if ((stcb == NULL) ||
4126830d754dSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4127f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4128830d754dSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
4129830d754dSRandall Stewart 		/* If the socket is gone we are out of here */
4130f8829a4aSRandall Stewart 		return;
4131f8829a4aSRandall Stewart 	}
4132a99b6783SRandall Stewart 	if (stcb->sctp_socket->so_rcv.sb_state & SBS_CANTRCVMORE) {
4133a99b6783SRandall Stewart 		return;
4134a99b6783SRandall Stewart 	}
4135839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
4136839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
413717205eccSRandall Stewart 		if ((notification == SCTP_NOTIFY_INTERFACE_DOWN) ||
413817205eccSRandall Stewart 		    (notification == SCTP_NOTIFY_INTERFACE_UP) ||
413917205eccSRandall Stewart 		    (notification == SCTP_NOTIFY_INTERFACE_CONFIRMED)) {
414017205eccSRandall Stewart 			/* Don't report these in front states */
414117205eccSRandall Stewart 			return;
414217205eccSRandall Stewart 		}
414317205eccSRandall Stewart 	}
4144f8829a4aSRandall Stewart 	switch (notification) {
4145f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_UP:
4146f8829a4aSRandall Stewart 		if (stcb->asoc.assoc_up_sent == 0) {
4147410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_UP, stcb, error, NULL, 0, so_locked);
4148f8829a4aSRandall Stewart 			stcb->asoc.assoc_up_sent = 1;
4149f8829a4aSRandall Stewart 		}
41502afb3e84SRandall Stewart 		if (stcb->asoc.adaptation_needed && (stcb->asoc.adaptation_sent == 0)) {
41517215cc1bSMichael Tuexen 			sctp_notify_adaptation_layer(stcb);
41522afb3e84SRandall Stewart 		}
4153c79bec9cSMichael Tuexen 		if (stcb->asoc.auth_supported == 0) {
4154830d754dSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
4155830d754dSRandall Stewart 			    NULL, so_locked);
4156830d754dSRandall Stewart 		}
4157f8829a4aSRandall Stewart 		break;
4158f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_DOWN:
4159410a3b1eSMichael Tuexen 		sctp_notify_assoc_change(SCTP_SHUTDOWN_COMP, stcb, error, NULL, 0, so_locked);
4160f8829a4aSRandall Stewart 		break;
4161f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_DOWN:
4162f8829a4aSRandall Stewart 		{
4163f8829a4aSRandall Stewart 			struct sctp_nets *net;
4164f8829a4aSRandall Stewart 
4165f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
4166f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_UNREACHABLE,
41673cb3567dSMichael Tuexen 			    (struct sockaddr *)&net->ro._l_addr, error, so_locked);
4168f8829a4aSRandall Stewart 			break;
4169f8829a4aSRandall Stewart 		}
4170f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_UP:
4171f8829a4aSRandall Stewart 		{
4172f8829a4aSRandall Stewart 			struct sctp_nets *net;
4173f8829a4aSRandall Stewart 
4174f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
4175f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_AVAILABLE,
41763cb3567dSMichael Tuexen 			    (struct sockaddr *)&net->ro._l_addr, error, so_locked);
4177f8829a4aSRandall Stewart 			break;
4178f8829a4aSRandall Stewart 		}
4179f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_CONFIRMED:
4180f8829a4aSRandall Stewart 		{
4181f8829a4aSRandall Stewart 			struct sctp_nets *net;
4182f8829a4aSRandall Stewart 
4183f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
4184f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_CONFIRMED,
41853cb3567dSMichael Tuexen 			    (struct sockaddr *)&net->ro._l_addr, error, so_locked);
4186f8829a4aSRandall Stewart 			break;
4187f8829a4aSRandall Stewart 		}
4188f8829a4aSRandall Stewart 	case SCTP_NOTIFY_SPECIAL_SP_FAIL:
4189f8829a4aSRandall Stewart 		sctp_notify_send_failed2(stcb, error,
4190ceaad40aSRandall Stewart 		    (struct sctp_stream_queue_pending *)data, so_locked);
4191f8829a4aSRandall Stewart 		break;
41921edc9dbaSMichael Tuexen 	case SCTP_NOTIFY_SENT_DG_FAIL:
41931edc9dbaSMichael Tuexen 		sctp_notify_send_failed(stcb, 1, error,
41941edc9dbaSMichael Tuexen 		    (struct sctp_tmit_chunk *)data, so_locked);
41951edc9dbaSMichael Tuexen 		break;
41961edc9dbaSMichael Tuexen 	case SCTP_NOTIFY_UNSENT_DG_FAIL:
41971edc9dbaSMichael Tuexen 		sctp_notify_send_failed(stcb, 0, error,
4198ceaad40aSRandall Stewart 		    (struct sctp_tmit_chunk *)data, so_locked);
4199f8829a4aSRandall Stewart 		break;
4200f8829a4aSRandall Stewart 	case SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION:
42019a6142d8SRandall Stewart 		{
42029a6142d8SRandall Stewart 			uint32_t val;
42039a6142d8SRandall Stewart 
42049a6142d8SRandall Stewart 			val = *((uint32_t *)data);
42059a6142d8SRandall Stewart 
4206810ec536SMichael Tuexen 			sctp_notify_partial_delivery_indication(stcb, error, val, so_locked);
4207f8829a4aSRandall Stewart 			break;
4208810ec536SMichael Tuexen 		}
4209410a3b1eSMichael Tuexen 	case SCTP_NOTIFY_ASSOC_LOC_ABORTED:
4210839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
4211839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
4212410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 0, so_locked);
4213c105859eSRandall Stewart 		} else {
4214410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 0, so_locked);
4215410a3b1eSMichael Tuexen 		}
4216410a3b1eSMichael Tuexen 		break;
4217410a3b1eSMichael Tuexen 	case SCTP_NOTIFY_ASSOC_REM_ABORTED:
4218839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
4219839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
4220410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 1, so_locked);
4221410a3b1eSMichael Tuexen 		} else {
4222410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 1, so_locked);
4223c105859eSRandall Stewart 		}
4224f8829a4aSRandall Stewart 		break;
4225f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_RESTART:
4226410a3b1eSMichael Tuexen 		sctp_notify_assoc_change(SCTP_RESTART, stcb, error, NULL, 0, so_locked);
4227c79bec9cSMichael Tuexen 		if (stcb->asoc.auth_supported == 0) {
4228830d754dSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
4229830d754dSRandall Stewart 			    NULL, so_locked);
4230830d754dSRandall Stewart 		}
4231f8829a4aSRandall Stewart 		break;
4232f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_SEND:
4233d7714577SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *)data), SCTP_STREAM_RESET_OUTGOING_SSN);
4234f8829a4aSRandall Stewart 		break;
4235f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_RECV:
4236d7714577SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *)data), SCTP_STREAM_RESET_INCOMING);
4237f8829a4aSRandall Stewart 		break;
4238f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_FAILED_OUT:
4239c4e848b7SRandall Stewart 		sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
4240d7714577SMichael Tuexen 		    (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_FAILED));
4241f8829a4aSRandall Stewart 		break;
4242d4260646SMichael Tuexen 	case SCTP_NOTIFY_STR_RESET_DENIED_OUT:
4243d4260646SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
4244d4260646SMichael Tuexen 		    (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_DENIED));
4245d4260646SMichael Tuexen 		break;
4246f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_FAILED_IN:
4247c4e848b7SRandall Stewart 		sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
4248d7714577SMichael Tuexen 		    (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_FAILED));
4249f8829a4aSRandall Stewart 		break;
4250d4260646SMichael Tuexen 	case SCTP_NOTIFY_STR_RESET_DENIED_IN:
4251d4260646SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
4252d4260646SMichael Tuexen 		    (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_DENIED));
4253d4260646SMichael Tuexen 		break;
4254f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_ADD_IP:
4255f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_ADDED, data,
42563cb3567dSMichael Tuexen 		    error, so_locked);
4257f8829a4aSRandall Stewart 		break;
4258f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_DELETE_IP:
4259f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_REMOVED, data,
42603cb3567dSMichael Tuexen 		    error, so_locked);
4261f8829a4aSRandall Stewart 		break;
4262f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_SET_PRIMARY:
4263f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_MADE_PRIM, data,
42643cb3567dSMichael Tuexen 		    error, so_locked);
4265f8829a4aSRandall Stewart 		break;
4266f8829a4aSRandall Stewart 	case SCTP_NOTIFY_PEER_SHUTDOWN:
4267f8829a4aSRandall Stewart 		sctp_notify_shutdown_event(stcb);
4268f8829a4aSRandall Stewart 		break;
4269f8829a4aSRandall Stewart 	case SCTP_NOTIFY_AUTH_NEW_KEY:
427078f28045SMichael Tuexen 		sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY, error,
4271830d754dSRandall Stewart 		    (uint16_t)(uintptr_t)data,
4272830d754dSRandall Stewart 		    so_locked);
4273f8829a4aSRandall Stewart 		break;
4274830d754dSRandall Stewart 	case SCTP_NOTIFY_AUTH_FREE_KEY:
4275830d754dSRandall Stewart 		sctp_notify_authentication(stcb, SCTP_AUTH_FREE_KEY, error,
4276830d754dSRandall Stewart 		    (uint16_t)(uintptr_t)data,
4277830d754dSRandall Stewart 		    so_locked);
4278f8829a4aSRandall Stewart 		break;
4279830d754dSRandall Stewart 	case SCTP_NOTIFY_NO_PEER_AUTH:
4280830d754dSRandall Stewart 		sctp_notify_authentication(stcb, SCTP_AUTH_NO_AUTH, error,
4281830d754dSRandall Stewart 		    (uint16_t)(uintptr_t)data,
4282830d754dSRandall Stewart 		    so_locked);
4283830d754dSRandall Stewart 		break;
4284830d754dSRandall Stewart 	case SCTP_NOTIFY_SENDER_DRY:
4285830d754dSRandall Stewart 		sctp_notify_sender_dry_event(stcb, so_locked);
4286830d754dSRandall Stewart 		break;
4287389b1b11SMichael Tuexen 	case SCTP_NOTIFY_REMOTE_ERROR:
4288389b1b11SMichael Tuexen 		sctp_notify_remote_error(stcb, error, data);
4289389b1b11SMichael Tuexen 		break;
4290f8829a4aSRandall Stewart 	default:
4291ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_UTIL1, "%s: unknown notification %xh (%u)\n",
42926e9c45e0SMichael Tuexen 		    __func__, notification, notification);
4293f8829a4aSRandall Stewart 		break;
4294f8829a4aSRandall Stewart 	}			/* end switch */
4295f8829a4aSRandall Stewart }
4296f8829a4aSRandall Stewart 
4297f8829a4aSRandall Stewart void
42981edc9dbaSMichael Tuexen sctp_report_all_outbound(struct sctp_tcb *stcb, uint16_t error, int holds_lock, int so_locked
4299ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4300ceaad40aSRandall Stewart     SCTP_UNUSED
4301ceaad40aSRandall Stewart #endif
4302ceaad40aSRandall Stewart )
4303f8829a4aSRandall Stewart {
4304f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4305f8829a4aSRandall Stewart 	struct sctp_stream_out *outs;
43064a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
43074a9ef3f8SMichael Tuexen 	struct sctp_stream_queue_pending *sp, *nsp;
43087f34832bSRandall Stewart 	int i;
4309f8829a4aSRandall Stewart 
4310ad81507eSRandall Stewart 	if (stcb == NULL) {
4311ad81507eSRandall Stewart 		return;
4312ad81507eSRandall Stewart 	}
43134a9ef3f8SMichael Tuexen 	asoc = &stcb->asoc;
43144a9ef3f8SMichael Tuexen 	if (asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4315478fbccbSRandall Stewart 		/* already being freed */
4316478fbccbSRandall Stewart 		return;
4317478fbccbSRandall Stewart 	}
4318f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4319f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
43204a9ef3f8SMichael Tuexen 	    (asoc->state & SCTP_STATE_CLOSED_SOCKET)) {
4321f8829a4aSRandall Stewart 		return;
4322f8829a4aSRandall Stewart 	}
4323f8829a4aSRandall Stewart 	/* now through all the gunk freeing chunks */
4324ad81507eSRandall Stewart 	if (holds_lock == 0) {
43257f34832bSRandall Stewart 		SCTP_TCB_SEND_LOCK(stcb);
4326ad81507eSRandall Stewart 	}
4327d00aff5dSRandall Stewart 	/* sent queue SHOULD be empty */
43284a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
4329d00aff5dSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
4330d00aff5dSRandall Stewart 		asoc->sent_queue_cnt--;
4331325c8c46SMichael Tuexen 		if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
433249656eefSMichael Tuexen 			if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
433349656eefSMichael Tuexen 				asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
4334a7ad6026SMichael Tuexen #ifdef INVARIANTS
4335a7ad6026SMichael Tuexen 			} else {
433649656eefSMichael Tuexen 				panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
4337a7ad6026SMichael Tuexen #endif
4338a7ad6026SMichael Tuexen 			}
4339a7ad6026SMichael Tuexen 		}
43400c0982b8SRandall Stewart 		if (chk->data != NULL) {
4341d00aff5dSRandall Stewart 			sctp_free_bufspace(stcb, asoc, chk, 1);
43421edc9dbaSMichael Tuexen 			sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
43431edc9dbaSMichael Tuexen 			    error, chk, so_locked);
4344810ec536SMichael Tuexen 			if (chk->data) {
4345d00aff5dSRandall Stewart 				sctp_m_freem(chk->data);
4346d00aff5dSRandall Stewart 				chk->data = NULL;
4347d00aff5dSRandall Stewart 			}
4348810ec536SMichael Tuexen 		}
4349689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, so_locked);
4350d00aff5dSRandall Stewart 		/* sa_ignore FREED_MEMORY */
4351d00aff5dSRandall Stewart 	}
4352d00aff5dSRandall Stewart 	/* pending send queue SHOULD be empty */
43534a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
4354d00aff5dSRandall Stewart 		TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
4355d00aff5dSRandall Stewart 		asoc->send_queue_cnt--;
435649656eefSMichael Tuexen 		if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
435749656eefSMichael Tuexen 			asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
4358a7ad6026SMichael Tuexen #ifdef INVARIANTS
4359a7ad6026SMichael Tuexen 		} else {
436049656eefSMichael Tuexen 			panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
4361a7ad6026SMichael Tuexen #endif
4362a7ad6026SMichael Tuexen 		}
43630c0982b8SRandall Stewart 		if (chk->data != NULL) {
4364d00aff5dSRandall Stewart 			sctp_free_bufspace(stcb, asoc, chk, 1);
43651edc9dbaSMichael Tuexen 			sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
43661edc9dbaSMichael Tuexen 			    error, chk, so_locked);
4367810ec536SMichael Tuexen 			if (chk->data) {
4368d00aff5dSRandall Stewart 				sctp_m_freem(chk->data);
4369d00aff5dSRandall Stewart 				chk->data = NULL;
4370d00aff5dSRandall Stewart 			}
4371810ec536SMichael Tuexen 		}
4372689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, so_locked);
4373d00aff5dSRandall Stewart 		/* sa_ignore FREED_MEMORY */
4374d00aff5dSRandall Stewart 	}
43754a9ef3f8SMichael Tuexen 	for (i = 0; i < asoc->streamoutcnt; i++) {
43767f34832bSRandall Stewart 		/* For each stream */
43774a9ef3f8SMichael Tuexen 		outs = &asoc->strmout[i];
43787f34832bSRandall Stewart 		/* clean up any sends there */
43794a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
43804d58b0c3SMichael Tuexen 			atomic_subtract_int(&asoc->stream_queue_cnt, 1);
4381f8829a4aSRandall Stewart 			TAILQ_REMOVE(&outs->outqueue, sp, next);
4382d9707e43SMichael Tuexen 			stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, 1);
4383f8829a4aSRandall Stewart 			sctp_free_spbufspace(stcb, asoc, sp);
4384478fbccbSRandall Stewart 			if (sp->data) {
4385f8829a4aSRandall Stewart 				sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
43861edc9dbaSMichael Tuexen 				    error, (void *)sp, so_locked);
4387f8829a4aSRandall Stewart 				if (sp->data) {
4388f8829a4aSRandall Stewart 					sctp_m_freem(sp->data);
4389f8829a4aSRandall Stewart 					sp->data = NULL;
4390d07b2ac6SMichael Tuexen 					sp->tail_mbuf = NULL;
4391d07b2ac6SMichael Tuexen 					sp->length = 0;
4392f8829a4aSRandall Stewart 				}
4393478fbccbSRandall Stewart 			}
43949eea4a2dSMichael Tuexen 			if (sp->net) {
4395f8829a4aSRandall Stewart 				sctp_free_remote_addr(sp->net);
4396f8829a4aSRandall Stewart 				sp->net = NULL;
43979eea4a2dSMichael Tuexen 			}
4398f8829a4aSRandall Stewart 			/* Free the chunk */
4399689e6a5fSMichael Tuexen 			sctp_free_a_strmoq(stcb, sp, so_locked);
44003c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
4401f8829a4aSRandall Stewart 		}
4402f8829a4aSRandall Stewart 	}
4403f8829a4aSRandall Stewart 
4404ad81507eSRandall Stewart 	if (holds_lock == 0) {
44057f34832bSRandall Stewart 		SCTP_TCB_SEND_UNLOCK(stcb);
4406f8829a4aSRandall Stewart 	}
4407ad81507eSRandall Stewart }
4408f8829a4aSRandall Stewart 
4409f8829a4aSRandall Stewart void
4410410a3b1eSMichael Tuexen sctp_abort_notification(struct sctp_tcb *stcb, uint8_t from_peer, uint16_t error,
4411a2b42326SMichael Tuexen     struct sctp_abort_chunk *abort, int so_locked
4412ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4413ceaad40aSRandall Stewart     SCTP_UNUSED
4414ceaad40aSRandall Stewart #endif
4415ceaad40aSRandall Stewart )
4416f8829a4aSRandall Stewart {
4417ad81507eSRandall Stewart 	if (stcb == NULL) {
4418ad81507eSRandall Stewart 		return;
4419ad81507eSRandall Stewart 	}
4420c55b70ceSMichael Tuexen 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4421c55b70ceSMichael Tuexen 	    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4422c55b70ceSMichael Tuexen 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
4423c55b70ceSMichael Tuexen 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_WAS_ABORTED;
4424c55b70ceSMichael Tuexen 	}
4425f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4426f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4427f8829a4aSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
4428f8829a4aSRandall Stewart 		return;
4429f8829a4aSRandall Stewart 	}
4430f8829a4aSRandall Stewart 	/* Tell them we lost the asoc */
44316982c0faSMichael Tuexen 	sctp_report_all_outbound(stcb, error, 0, so_locked);
4432410a3b1eSMichael Tuexen 	if (from_peer) {
4433410a3b1eSMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_REM_ABORTED, stcb, error, abort, so_locked);
4434410a3b1eSMichael Tuexen 	} else {
4435410a3b1eSMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_LOC_ABORTED, stcb, error, abort, so_locked);
4436410a3b1eSMichael Tuexen 	}
4437f8829a4aSRandall Stewart }
4438f8829a4aSRandall Stewart 
4439f8829a4aSRandall Stewart void
4440f8829a4aSRandall Stewart sctp_abort_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4441b1754ad1SMichael Tuexen     struct mbuf *m, int iphlen,
4442b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
4443b1754ad1SMichael Tuexen     struct sctphdr *sh, struct mbuf *op_err,
4444457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
4445c54a18d2SRandall Stewart     uint32_t vrf_id, uint16_t port)
4446f8829a4aSRandall Stewart {
4447f8829a4aSRandall Stewart 	uint32_t vtag;
4448ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4449ceaad40aSRandall Stewart 	struct socket *so;
4450ceaad40aSRandall Stewart #endif
4451ceaad40aSRandall Stewart 
4452f8829a4aSRandall Stewart 	vtag = 0;
4453f8829a4aSRandall Stewart 	if (stcb != NULL) {
4454f8829a4aSRandall Stewart 		vtag = stcb->asoc.peer_vtag;
445517205eccSRandall Stewart 		vrf_id = stcb->asoc.vrf_id;
4456f8829a4aSRandall Stewart 	}
4457b1754ad1SMichael Tuexen 	sctp_send_abort(m, iphlen, src, dst, sh, vtag, op_err,
4458d089f9b9SMichael Tuexen 	    mflowtype, mflowid, inp->fibnum,
4459f30ac432SMichael Tuexen 	    vrf_id, port);
4460f8829a4aSRandall Stewart 	if (stcb != NULL) {
4461884d8c53SMichael Tuexen 		/* We have a TCB to abort, send notification too */
4462884d8c53SMichael Tuexen 		sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
4463839d21d6SMichael Tuexen 		SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_WAS_ABORTED);
4464f8829a4aSRandall Stewart 		/* Ok, now lets free it */
4465ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4466ceaad40aSRandall Stewart 		so = SCTP_INP_SO(inp);
4467ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
4468ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
4469ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
4470ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4471ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4472ceaad40aSRandall Stewart #endif
44730271d0cdSMichael Tuexen 		SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4474839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
4475839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
44760271d0cdSMichael Tuexen 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
44770271d0cdSMichael Tuexen 		}
4478ba785902SMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4479ba785902SMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_4);
4480ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4481ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4482ceaad40aSRandall Stewart #endif
4483f8829a4aSRandall Stewart 	}
4484f8829a4aSRandall Stewart }
4485f1f73e57SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
4486f1f73e57SRandall Stewart void
4487f1f73e57SRandall Stewart sctp_print_out_track_log(struct sctp_tcb *stcb)
4488f1f73e57SRandall Stewart {
448918e198d3SRandall Stewart #ifdef NOSIY_PRINTS
4490f1f73e57SRandall Stewart 	int i;
4491f1f73e57SRandall Stewart 
4492ad81507eSRandall Stewart 	SCTP_PRINTF("Last ep reason:%x\n", stcb->sctp_ep->last_abort_code);
4493ad81507eSRandall Stewart 	SCTP_PRINTF("IN bound TSN log-aaa\n");
4494f1f73e57SRandall Stewart 	if ((stcb->asoc.tsn_in_at == 0) && (stcb->asoc.tsn_in_wrapped == 0)) {
4495ad81507eSRandall Stewart 		SCTP_PRINTF("None rcvd\n");
4496f1f73e57SRandall Stewart 		goto none_in;
4497f1f73e57SRandall Stewart 	}
4498f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_in_wrapped) {
4499f1f73e57SRandall Stewart 		for (i = stcb->asoc.tsn_in_at; i < SCTP_TSN_LOG_SIZE; i++) {
4500ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4501f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].tsn,
4502f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].strm,
4503f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].seq,
4504f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].flgs,
4505f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].sz);
4506f1f73e57SRandall Stewart 		}
4507f1f73e57SRandall Stewart 	}
4508f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_in_at) {
4509f1f73e57SRandall Stewart 		for (i = 0; i < stcb->asoc.tsn_in_at; i++) {
4510ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4511f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].tsn,
4512f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].strm,
4513f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].seq,
4514f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].flgs,
4515f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].sz);
4516f1f73e57SRandall Stewart 		}
4517f1f73e57SRandall Stewart 	}
4518f1f73e57SRandall Stewart none_in:
4519ad81507eSRandall Stewart 	SCTP_PRINTF("OUT bound TSN log-aaa\n");
4520ad81507eSRandall Stewart 	if ((stcb->asoc.tsn_out_at == 0) &&
4521ad81507eSRandall Stewart 	    (stcb->asoc.tsn_out_wrapped == 0)) {
4522ad81507eSRandall Stewart 		SCTP_PRINTF("None sent\n");
4523f1f73e57SRandall Stewart 	}
4524f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_out_wrapped) {
4525f1f73e57SRandall Stewart 		for (i = stcb->asoc.tsn_out_at; i < SCTP_TSN_LOG_SIZE; i++) {
4526ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4527f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].tsn,
4528f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].strm,
4529f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].seq,
4530f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].flgs,
4531f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].sz);
4532f1f73e57SRandall Stewart 		}
4533f1f73e57SRandall Stewart 	}
4534f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_out_at) {
4535f1f73e57SRandall Stewart 		for (i = 0; i < stcb->asoc.tsn_out_at; i++) {
4536ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4537f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].tsn,
4538f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].strm,
4539f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].seq,
4540f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].flgs,
4541f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].sz);
4542f1f73e57SRandall Stewart 		}
4543f1f73e57SRandall Stewart 	}
454418e198d3SRandall Stewart #endif
4545f1f73e57SRandall Stewart }
4546f1f73e57SRandall Stewart #endif
4547f1f73e57SRandall Stewart 
4548f8829a4aSRandall Stewart void
4549f8829a4aSRandall Stewart sctp_abort_an_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4550a2b42326SMichael Tuexen     struct mbuf *op_err,
4551ceaad40aSRandall Stewart     int so_locked
4552ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4553ceaad40aSRandall Stewart     SCTP_UNUSED
4554ceaad40aSRandall Stewart #endif
4555ceaad40aSRandall Stewart )
4556f8829a4aSRandall Stewart {
4557ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4558ceaad40aSRandall Stewart 	struct socket *so;
4559ceaad40aSRandall Stewart #endif
4560ceaad40aSRandall Stewart 
4561ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4562ceaad40aSRandall Stewart 	so = SCTP_INP_SO(inp);
4563ceaad40aSRandall Stewart #endif
4564f8829a4aSRandall Stewart 	if (stcb == NULL) {
4565f8829a4aSRandall Stewart 		/* Got to have a TCB */
4566f8829a4aSRandall Stewart 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4567fe1831e0SMichael Tuexen 			if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4568b0552ae2SRandall Stewart 				sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4569b0552ae2SRandall Stewart 				    SCTP_CALLED_DIRECTLY_NOCMPSET);
4570f8829a4aSRandall Stewart 			}
4571f8829a4aSRandall Stewart 		}
4572f8829a4aSRandall Stewart 		return;
457363981c2bSRandall Stewart 	} else {
4574839d21d6SMichael Tuexen 		SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_WAS_ABORTED);
4575f8829a4aSRandall Stewart 	}
4576f8829a4aSRandall Stewart 	/* notify the peer */
4577ceaad40aSRandall Stewart 	sctp_send_abort_tcb(stcb, op_err, so_locked);
4578f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4579839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
4580839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4581f8829a4aSRandall Stewart 		SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4582f8829a4aSRandall Stewart 	}
4583884d8c53SMichael Tuexen 	/* notify the ulp */
4584884d8c53SMichael Tuexen 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
4585884d8c53SMichael Tuexen 		sctp_abort_notification(stcb, 0, 0, NULL, so_locked);
4586884d8c53SMichael Tuexen 	}
4587f8829a4aSRandall Stewart 	/* now free the asoc */
4588f1f73e57SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
4589f1f73e57SRandall Stewart 	sctp_print_out_track_log(stcb);
4590f1f73e57SRandall Stewart #endif
4591ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4592ceaad40aSRandall Stewart 	if (!so_locked) {
4593ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
4594ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
4595ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
4596ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4597ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4598ceaad40aSRandall Stewart 	}
4599ceaad40aSRandall Stewart #endif
4600ba785902SMichael Tuexen 	(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4601ba785902SMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_5);
4602ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4603ceaad40aSRandall Stewart 	if (!so_locked) {
4604ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4605ceaad40aSRandall Stewart 	}
4606ceaad40aSRandall Stewart #endif
4607f8829a4aSRandall Stewart }
4608f8829a4aSRandall Stewart 
4609f8829a4aSRandall Stewart void
4610b1754ad1SMichael Tuexen sctp_handle_ootb(struct mbuf *m, int iphlen, int offset,
4611b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
4612b1754ad1SMichael Tuexen     struct sctphdr *sh, struct sctp_inpcb *inp,
4613ff1ffd74SMichael Tuexen     struct mbuf *cause,
4614d089f9b9SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4615f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
4616f8829a4aSRandall Stewart {
4617f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch, chunk_buf;
4618f8829a4aSRandall Stewart 	unsigned int chk_length;
4619c58e60beSMichael Tuexen 	int contains_init_chunk;
4620f8829a4aSRandall Stewart 
4621f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue);
4622f8829a4aSRandall Stewart 	/* Generate a TO address for future reference */
4623f8829a4aSRandall Stewart 	if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4624fe1831e0SMichael Tuexen 		if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4625b0552ae2SRandall Stewart 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4626b0552ae2SRandall Stewart 			    SCTP_CALLED_DIRECTLY_NOCMPSET);
4627f8829a4aSRandall Stewart 		}
4628f8829a4aSRandall Stewart 	}
4629c58e60beSMichael Tuexen 	contains_init_chunk = 0;
4630f8829a4aSRandall Stewart 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4631f8829a4aSRandall Stewart 	    sizeof(*ch), (uint8_t *)&chunk_buf);
4632f8829a4aSRandall Stewart 	while (ch != NULL) {
4633f8829a4aSRandall Stewart 		chk_length = ntohs(ch->chunk_length);
4634f8829a4aSRandall Stewart 		if (chk_length < sizeof(*ch)) {
4635f8829a4aSRandall Stewart 			/* break to abort land */
4636f8829a4aSRandall Stewart 			break;
4637f8829a4aSRandall Stewart 		}
4638f8829a4aSRandall Stewart 		switch (ch->chunk_type) {
4639c58e60beSMichael Tuexen 		case SCTP_INIT:
4640c58e60beSMichael Tuexen 			contains_init_chunk = 1;
4641c58e60beSMichael Tuexen 			break;
4642f8829a4aSRandall Stewart 		case SCTP_PACKET_DROPPED:
4643f8829a4aSRandall Stewart 			/* we don't respond to pkt-dropped */
4644f8829a4aSRandall Stewart 			return;
4645f8829a4aSRandall Stewart 		case SCTP_ABORT_ASSOCIATION:
4646f8829a4aSRandall Stewart 			/* we don't respond with an ABORT to an ABORT */
4647f8829a4aSRandall Stewart 			return;
4648f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_COMPLETE:
4649f8829a4aSRandall Stewart 			/*
4650f8829a4aSRandall Stewart 			 * we ignore it since we are not waiting for it and
4651f8829a4aSRandall Stewart 			 * peer is gone
4652f8829a4aSRandall Stewart 			 */
4653f8829a4aSRandall Stewart 			return;
4654f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_ACK:
4655b1754ad1SMichael Tuexen 			sctp_send_shutdown_complete2(src, dst, sh,
4656d089f9b9SMichael Tuexen 			    mflowtype, mflowid, fibnum,
4657f30ac432SMichael Tuexen 			    vrf_id, port);
4658f8829a4aSRandall Stewart 			return;
4659f8829a4aSRandall Stewart 		default:
4660f8829a4aSRandall Stewart 			break;
4661f8829a4aSRandall Stewart 		}
4662f8829a4aSRandall Stewart 		offset += SCTP_SIZE32(chk_length);
4663f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4664f8829a4aSRandall Stewart 		    sizeof(*ch), (uint8_t *)&chunk_buf);
4665f8829a4aSRandall Stewart 	}
4666c58e60beSMichael Tuexen 	if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
4667c58e60beSMichael Tuexen 	    ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
4668c58e60beSMichael Tuexen 	    (contains_init_chunk == 0))) {
4669ff1ffd74SMichael Tuexen 		sctp_send_abort(m, iphlen, src, dst, sh, 0, cause,
4670d089f9b9SMichael Tuexen 		    mflowtype, mflowid, fibnum,
4671f30ac432SMichael Tuexen 		    vrf_id, port);
4672f8829a4aSRandall Stewart 	}
4673c58e60beSMichael Tuexen }
4674f8829a4aSRandall Stewart 
4675f8829a4aSRandall Stewart /*
4676f8829a4aSRandall Stewart  * check the inbound datagram to make sure there is not an abort inside it,
4677f8829a4aSRandall Stewart  * if there is return 1, else return 0.
4678f8829a4aSRandall Stewart  */
4679f8829a4aSRandall Stewart int
4680f8829a4aSRandall Stewart sctp_is_there_an_abort_here(struct mbuf *m, int iphlen, uint32_t *vtagfill)
4681f8829a4aSRandall Stewart {
4682f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
4683f8829a4aSRandall Stewart 	struct sctp_init_chunk *init_chk, chunk_buf;
4684f8829a4aSRandall Stewart 	int offset;
4685f8829a4aSRandall Stewart 	unsigned int chk_length;
4686f8829a4aSRandall Stewart 
4687f8829a4aSRandall Stewart 	offset = iphlen + sizeof(struct sctphdr);
4688f8829a4aSRandall Stewart 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch),
4689f8829a4aSRandall Stewart 	    (uint8_t *)&chunk_buf);
4690f8829a4aSRandall Stewart 	while (ch != NULL) {
4691f8829a4aSRandall Stewart 		chk_length = ntohs(ch->chunk_length);
4692f8829a4aSRandall Stewart 		if (chk_length < sizeof(*ch)) {
4693f8829a4aSRandall Stewart 			/* packet is probably corrupt */
4694f8829a4aSRandall Stewart 			break;
4695f8829a4aSRandall Stewart 		}
4696f8829a4aSRandall Stewart 		/* we seem to be ok, is it an abort? */
4697f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) {
4698f8829a4aSRandall Stewart 			/* yep, tell them */
4699f8829a4aSRandall Stewart 			return (1);
4700f8829a4aSRandall Stewart 		}
4701f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_INITIATION) {
4702f8829a4aSRandall Stewart 			/* need to update the Vtag */
4703f8829a4aSRandall Stewart 			init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4704f8829a4aSRandall Stewart 			    offset, sizeof(*init_chk), (uint8_t *)&chunk_buf);
4705f8829a4aSRandall Stewart 			if (init_chk != NULL) {
4706f8829a4aSRandall Stewart 				*vtagfill = ntohl(init_chk->init.initiate_tag);
4707f8829a4aSRandall Stewart 			}
4708f8829a4aSRandall Stewart 		}
4709f8829a4aSRandall Stewart 		/* Nope, move to the next chunk */
4710f8829a4aSRandall Stewart 		offset += SCTP_SIZE32(chk_length);
4711f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4712f8829a4aSRandall Stewart 		    sizeof(*ch), (uint8_t *)&chunk_buf);
4713f8829a4aSRandall Stewart 	}
4714f8829a4aSRandall Stewart 	return (0);
4715f8829a4aSRandall Stewart }
4716f8829a4aSRandall Stewart 
4717f8829a4aSRandall Stewart /*
4718f8829a4aSRandall Stewart  * currently (2/02), ifa_addr embeds scope_id's and don't have sin6_scope_id
4719f8829a4aSRandall Stewart  * set (i.e. it's 0) so, create this function to compare link local scopes
4720f8829a4aSRandall Stewart  */
47215e2c2d87SRandall Stewart #ifdef INET6
4722f8829a4aSRandall Stewart uint32_t
4723b0471b4bSMichael Tuexen sctp_is_same_scope(struct sockaddr_in6 *addr1, struct sockaddr_in6 *addr2)
4724b0471b4bSMichael Tuexen {
4725f8829a4aSRandall Stewart 	struct sockaddr_in6 a, b;
4726f8829a4aSRandall Stewart 
4727f8829a4aSRandall Stewart 	/* save copies */
4728f8829a4aSRandall Stewart 	a = *addr1;
4729f8829a4aSRandall Stewart 	b = *addr2;
4730f8829a4aSRandall Stewart 
4731f8829a4aSRandall Stewart 	if (a.sin6_scope_id == 0)
4732f8829a4aSRandall Stewart 		if (sa6_recoverscope(&a)) {
4733f8829a4aSRandall Stewart 			/* can't get scope, so can't match */
4734f8829a4aSRandall Stewart 			return (0);
4735f8829a4aSRandall Stewart 		}
4736f8829a4aSRandall Stewart 	if (b.sin6_scope_id == 0)
4737f8829a4aSRandall Stewart 		if (sa6_recoverscope(&b)) {
4738f8829a4aSRandall Stewart 			/* can't get scope, so can't match */
4739f8829a4aSRandall Stewart 			return (0);
4740f8829a4aSRandall Stewart 		}
4741f8829a4aSRandall Stewart 	if (a.sin6_scope_id != b.sin6_scope_id)
4742f8829a4aSRandall Stewart 		return (0);
4743f8829a4aSRandall Stewart 
4744f8829a4aSRandall Stewart 	return (1);
4745f8829a4aSRandall Stewart }
4746f8829a4aSRandall Stewart 
4747f8829a4aSRandall Stewart /*
4748f8829a4aSRandall Stewart  * returns a sockaddr_in6 with embedded scope recovered and removed
4749f8829a4aSRandall Stewart  */
4750f8829a4aSRandall Stewart struct sockaddr_in6 *
4751f8829a4aSRandall Stewart sctp_recover_scope(struct sockaddr_in6 *addr, struct sockaddr_in6 *store)
4752f8829a4aSRandall Stewart {
4753f8829a4aSRandall Stewart 	/* check and strip embedded scope junk */
4754f8829a4aSRandall Stewart 	if (addr->sin6_family == AF_INET6) {
4755f8829a4aSRandall Stewart 		if (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr)) {
4756f8829a4aSRandall Stewart 			if (addr->sin6_scope_id == 0) {
4757f8829a4aSRandall Stewart 				*store = *addr;
4758f8829a4aSRandall Stewart 				if (!sa6_recoverscope(store)) {
4759f8829a4aSRandall Stewart 					/* use the recovered scope */
4760f8829a4aSRandall Stewart 					addr = store;
4761f8829a4aSRandall Stewart 				}
4762f42a358aSRandall Stewart 			} else {
4763f8829a4aSRandall Stewart 				/* else, return the original "to" addr */
4764f42a358aSRandall Stewart 				in6_clearscope(&addr->sin6_addr);
4765f8829a4aSRandall Stewart 			}
4766f8829a4aSRandall Stewart 		}
4767f8829a4aSRandall Stewart 	}
4768f8829a4aSRandall Stewart 	return (addr);
4769f8829a4aSRandall Stewart }
47705e2c2d87SRandall Stewart #endif
47715e2c2d87SRandall Stewart 
4772f8829a4aSRandall Stewart /*
4773f8829a4aSRandall Stewart  * are the two addresses the same?  currently a "scopeless" check returns: 1
4774f8829a4aSRandall Stewart  * if same, 0 if not
4775f8829a4aSRandall Stewart  */
477672fb6fdbSRandall Stewart int
4777f8829a4aSRandall Stewart sctp_cmpaddr(struct sockaddr *sa1, struct sockaddr *sa2)
4778f8829a4aSRandall Stewart {
4779f8829a4aSRandall Stewart 
4780f8829a4aSRandall Stewart 	/* must be valid */
4781f8829a4aSRandall Stewart 	if (sa1 == NULL || sa2 == NULL)
4782f8829a4aSRandall Stewart 		return (0);
4783f8829a4aSRandall Stewart 
4784f8829a4aSRandall Stewart 	/* must be the same family */
4785f8829a4aSRandall Stewart 	if (sa1->sa_family != sa2->sa_family)
4786f8829a4aSRandall Stewart 		return (0);
4787f8829a4aSRandall Stewart 
47885e2c2d87SRandall Stewart 	switch (sa1->sa_family) {
47895e2c2d87SRandall Stewart #ifdef INET6
47905e2c2d87SRandall Stewart 	case AF_INET6:
47915e2c2d87SRandall Stewart 		{
4792f8829a4aSRandall Stewart 			/* IPv6 addresses */
4793f8829a4aSRandall Stewart 			struct sockaddr_in6 *sin6_1, *sin6_2;
4794f8829a4aSRandall Stewart 
4795f8829a4aSRandall Stewart 			sin6_1 = (struct sockaddr_in6 *)sa1;
4796f8829a4aSRandall Stewart 			sin6_2 = (struct sockaddr_in6 *)sa2;
4797c54a18d2SRandall Stewart 			return (SCTP6_ARE_ADDR_EQUAL(sin6_1,
4798c54a18d2SRandall Stewart 			    sin6_2));
47995e2c2d87SRandall Stewart 		}
48005e2c2d87SRandall Stewart #endif
4801ea5eba11SMichael Tuexen #ifdef INET
48025e2c2d87SRandall Stewart 	case AF_INET:
48035e2c2d87SRandall Stewart 		{
4804f8829a4aSRandall Stewart 			/* IPv4 addresses */
4805f8829a4aSRandall Stewart 			struct sockaddr_in *sin_1, *sin_2;
4806f8829a4aSRandall Stewart 
4807f8829a4aSRandall Stewart 			sin_1 = (struct sockaddr_in *)sa1;
4808f8829a4aSRandall Stewart 			sin_2 = (struct sockaddr_in *)sa2;
4809f8829a4aSRandall Stewart 			return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr);
48105e2c2d87SRandall Stewart 		}
4811ea5eba11SMichael Tuexen #endif
48125e2c2d87SRandall Stewart 	default:
4813f8829a4aSRandall Stewart 		/* we don't do these... */
4814f8829a4aSRandall Stewart 		return (0);
4815f8829a4aSRandall Stewart 	}
4816f8829a4aSRandall Stewart }
4817f8829a4aSRandall Stewart 
4818f8829a4aSRandall Stewart void
4819f8829a4aSRandall Stewart sctp_print_address(struct sockaddr *sa)
4820f8829a4aSRandall Stewart {
48215e2c2d87SRandall Stewart #ifdef INET6
48227d32aa0cSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
48235e2c2d87SRandall Stewart #endif
48245e2c2d87SRandall Stewart 
48255e2c2d87SRandall Stewart 	switch (sa->sa_family) {
48265e2c2d87SRandall Stewart #ifdef INET6
48275e2c2d87SRandall Stewart 	case AF_INET6:
48285e2c2d87SRandall Stewart 		{
4829ad81507eSRandall Stewart 			struct sockaddr_in6 *sin6;
4830ad81507eSRandall Stewart 
4831f8829a4aSRandall Stewart 			sin6 = (struct sockaddr_in6 *)sa;
4832ad81507eSRandall Stewart 			SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n",
48337d32aa0cSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &sin6->sin6_addr),
48347d32aa0cSBjoern A. Zeeb 			    ntohs(sin6->sin6_port),
4835f8829a4aSRandall Stewart 			    sin6->sin6_scope_id);
48365e2c2d87SRandall Stewart 			break;
48375e2c2d87SRandall Stewart 		}
48385e2c2d87SRandall Stewart #endif
4839ea5eba11SMichael Tuexen #ifdef INET
48405e2c2d87SRandall Stewart 	case AF_INET:
48415e2c2d87SRandall Stewart 		{
4842f8829a4aSRandall Stewart 			struct sockaddr_in *sin;
4843f8829a4aSRandall Stewart 			unsigned char *p;
4844f8829a4aSRandall Stewart 
4845f8829a4aSRandall Stewart 			sin = (struct sockaddr_in *)sa;
4846f8829a4aSRandall Stewart 			p = (unsigned char *)&sin->sin_addr;
4847ad81507eSRandall Stewart 			SCTP_PRINTF("IPv4 address: %u.%u.%u.%u:%d\n",
4848f8829a4aSRandall Stewart 			    p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
48495e2c2d87SRandall Stewart 			break;
48505e2c2d87SRandall Stewart 		}
4851ea5eba11SMichael Tuexen #endif
48525e2c2d87SRandall Stewart 	default:
4853ad81507eSRandall Stewart 		SCTP_PRINTF("?\n");
48545e2c2d87SRandall Stewart 		break;
4855f8829a4aSRandall Stewart 	}
4856f8829a4aSRandall Stewart }
4857f8829a4aSRandall Stewart 
4858f8829a4aSRandall Stewart void
4859f8829a4aSRandall Stewart sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
4860f8829a4aSRandall Stewart     struct sctp_inpcb *new_inp,
4861d06c82f1SRandall Stewart     struct sctp_tcb *stcb,
4862d06c82f1SRandall Stewart     int waitflags)
4863f8829a4aSRandall Stewart {
4864f8829a4aSRandall Stewart 	/*
4865f8829a4aSRandall Stewart 	 * go through our old INP and pull off any control structures that
4866f8829a4aSRandall Stewart 	 * belong to stcb and move then to the new inp.
4867f8829a4aSRandall Stewart 	 */
4868f8829a4aSRandall Stewart 	struct socket *old_so, *new_so;
4869f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control, *nctl;
4870f8829a4aSRandall Stewart 	struct sctp_readhead tmp_queue;
4871f8829a4aSRandall Stewart 	struct mbuf *m;
4872bff64a4dSRandall Stewart 	int error = 0;
4873f8829a4aSRandall Stewart 
4874f8829a4aSRandall Stewart 	old_so = old_inp->sctp_socket;
4875f8829a4aSRandall Stewart 	new_so = new_inp->sctp_socket;
4876f8829a4aSRandall Stewart 	TAILQ_INIT(&tmp_queue);
4877d06c82f1SRandall Stewart 	error = sblock(&old_so->so_rcv, waitflags);
4878f8829a4aSRandall Stewart 	if (error) {
4879f8829a4aSRandall Stewart 		/*
4880f8829a4aSRandall Stewart 		 * Gak, can't get sblock, we have a problem. data will be
4881f8829a4aSRandall Stewart 		 * left stranded.. and we don't dare look at it since the
4882f8829a4aSRandall Stewart 		 * other thread may be reading something. Oh well, its a
4883f8829a4aSRandall Stewart 		 * screwed up app that does a peeloff OR a accept while
4884f8829a4aSRandall Stewart 		 * reading from the main socket... actually its only the
4885f8829a4aSRandall Stewart 		 * peeloff() case, since I think read will fail on a
4886f8829a4aSRandall Stewart 		 * listening socket..
4887f8829a4aSRandall Stewart 		 */
4888f8829a4aSRandall Stewart 		return;
4889f8829a4aSRandall Stewart 	}
4890f8829a4aSRandall Stewart 	/* lock the socket buffers */
4891f8829a4aSRandall Stewart 	SCTP_INP_READ_LOCK(old_inp);
48924a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(control, &old_inp->read_queue, next, nctl) {
4893f8829a4aSRandall Stewart 		/* Pull off all for out target stcb */
4894f8829a4aSRandall Stewart 		if (control->stcb == stcb) {
4895f8829a4aSRandall Stewart 			/* remove it we want it */
4896f8829a4aSRandall Stewart 			TAILQ_REMOVE(&old_inp->read_queue, control, next);
4897f8829a4aSRandall Stewart 			TAILQ_INSERT_TAIL(&tmp_queue, control, next);
4898f8829a4aSRandall Stewart 			m = control->data;
4899f8829a4aSRandall Stewart 			while (m) {
4900b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4901139bc87fSRandall Stewart 					sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
490280fefe0aSRandall Stewart 				}
4903f8829a4aSRandall Stewart 				sctp_sbfree(control, stcb, &old_so->so_rcv, m);
4904b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4905f8829a4aSRandall Stewart 					sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
490680fefe0aSRandall Stewart 				}
4907139bc87fSRandall Stewart 				m = SCTP_BUF_NEXT(m);
4908f8829a4aSRandall Stewart 			}
4909f8829a4aSRandall Stewart 		}
4910f8829a4aSRandall Stewart 	}
4911f8829a4aSRandall Stewart 	SCTP_INP_READ_UNLOCK(old_inp);
4912f8829a4aSRandall Stewart 	/* Remove the sb-lock on the old socket */
4913f8829a4aSRandall Stewart 
4914f8829a4aSRandall Stewart 	sbunlock(&old_so->so_rcv);
4915f8829a4aSRandall Stewart 	/* Now we move them over to the new socket buffer */
4916f8829a4aSRandall Stewart 	SCTP_INP_READ_LOCK(new_inp);
49174a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(control, &tmp_queue, next, nctl) {
4918f8829a4aSRandall Stewart 		TAILQ_INSERT_TAIL(&new_inp->read_queue, control, next);
4919f8829a4aSRandall Stewart 		m = control->data;
4920f8829a4aSRandall Stewart 		while (m) {
4921b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4922139bc87fSRandall Stewart 				sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
492380fefe0aSRandall Stewart 			}
4924f8829a4aSRandall Stewart 			sctp_sballoc(stcb, &new_so->so_rcv, m);
4925b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4926f8829a4aSRandall Stewart 				sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
492780fefe0aSRandall Stewart 			}
4928139bc87fSRandall Stewart 			m = SCTP_BUF_NEXT(m);
4929f8829a4aSRandall Stewart 		}
4930f8829a4aSRandall Stewart 	}
4931f8829a4aSRandall Stewart 	SCTP_INP_READ_UNLOCK(new_inp);
4932f8829a4aSRandall Stewart }
4933f8829a4aSRandall Stewart 
4934f8829a4aSRandall Stewart void
4935b1deed45SMichael Tuexen sctp_wakeup_the_read_socket(struct sctp_inpcb *inp,
4936b1deed45SMichael Tuexen     struct sctp_tcb *stcb,
4937b1deed45SMichael Tuexen     int so_locked
4938b1deed45SMichael Tuexen #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4939b1deed45SMichael Tuexen     SCTP_UNUSED
4940b1deed45SMichael Tuexen #endif
4941b1deed45SMichael Tuexen )
494244249214SRandall Stewart {
4943b1deed45SMichael Tuexen 	if ((inp != NULL) && (inp->sctp_socket != NULL)) {
494444249214SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
494544249214SRandall Stewart 		struct socket *so;
494644249214SRandall Stewart 
494744249214SRandall Stewart 		so = SCTP_INP_SO(inp);
494844249214SRandall Stewart 		if (!so_locked) {
494944249214SRandall Stewart 			if (stcb) {
495044249214SRandall Stewart 				atomic_add_int(&stcb->asoc.refcnt, 1);
495144249214SRandall Stewart 				SCTP_TCB_UNLOCK(stcb);
495244249214SRandall Stewart 			}
495344249214SRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
495444249214SRandall Stewart 			if (stcb) {
495544249214SRandall Stewart 				SCTP_TCB_LOCK(stcb);
495644249214SRandall Stewart 				atomic_subtract_int(&stcb->asoc.refcnt, 1);
495744249214SRandall Stewart 			}
495844249214SRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
495944249214SRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
496044249214SRandall Stewart 				return;
496144249214SRandall Stewart 			}
496244249214SRandall Stewart 		}
496344249214SRandall Stewart #endif
496444249214SRandall Stewart 		sctp_sorwakeup(inp, inp->sctp_socket);
496544249214SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
496644249214SRandall Stewart 		if (!so_locked) {
496744249214SRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
496844249214SRandall Stewart 		}
496944249214SRandall Stewart #endif
497044249214SRandall Stewart 	}
497144249214SRandall Stewart }
497244249214SRandall Stewart 
497344249214SRandall Stewart void
4974f8829a4aSRandall Stewart sctp_add_to_readq(struct sctp_inpcb *inp,
4975f8829a4aSRandall Stewart     struct sctp_tcb *stcb,
4976f8829a4aSRandall Stewart     struct sctp_queued_to_read *control,
4977f8829a4aSRandall Stewart     struct sockbuf *sb,
4978ceaad40aSRandall Stewart     int end,
4979cfde3ff7SRandall Stewart     int inp_read_lock_held,
4980ceaad40aSRandall Stewart     int so_locked
4981ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4982ceaad40aSRandall Stewart     SCTP_UNUSED
4983ceaad40aSRandall Stewart #endif
4984ceaad40aSRandall Stewart )
4985f8829a4aSRandall Stewart {
4986f8829a4aSRandall Stewart 	/*
4987f8829a4aSRandall Stewart 	 * Here we must place the control on the end of the socket read
49884e88d37aSMichael Tuexen 	 * queue AND increment sb_cc so that select will work properly on
4989f8829a4aSRandall Stewart 	 * read.
4990f8829a4aSRandall Stewart 	 */
4991f8829a4aSRandall Stewart 	struct mbuf *m, *prev = NULL;
4992f8829a4aSRandall Stewart 
499303b0b021SRandall Stewart 	if (inp == NULL) {
499403b0b021SRandall Stewart 		/* Gak, TSNH!! */
4995a5d547adSRandall Stewart #ifdef INVARIANTS
499603b0b021SRandall Stewart 		panic("Gak, inp NULL on add_to_readq");
499703b0b021SRandall Stewart #endif
499803b0b021SRandall Stewart 		return;
499903b0b021SRandall Stewart 	}
5000cfde3ff7SRandall Stewart 	if (inp_read_lock_held == 0)
5001f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(inp);
5002cd1386abSMichael Tuexen 	if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
50038a3cfbffSMichael Tuexen 		if (!control->on_strm_q) {
5004cd1386abSMichael Tuexen 			sctp_free_remote_addr(control->whoFrom);
5005cd1386abSMichael Tuexen 			if (control->data) {
5006cd1386abSMichael Tuexen 				sctp_m_freem(control->data);
5007cd1386abSMichael Tuexen 				control->data = NULL;
5008cd1386abSMichael Tuexen 			}
500944249214SRandall Stewart 			sctp_free_a_readq(stcb, control);
50108a3cfbffSMichael Tuexen 		}
5011cd1386abSMichael Tuexen 		if (inp_read_lock_held == 0)
5012cd1386abSMichael Tuexen 			SCTP_INP_READ_UNLOCK(inp);
5013cd1386abSMichael Tuexen 		return;
5014cd1386abSMichael Tuexen 	}
501542551e99SRandall Stewart 	if (!(control->spec_flags & M_NOTIFICATION)) {
5016a5d547adSRandall Stewart 		atomic_add_int(&inp->total_recvs, 1);
501742551e99SRandall Stewart 		if (!control->do_not_ref_stcb) {
5018a5d547adSRandall Stewart 			atomic_add_int(&stcb->total_recvs, 1);
501942551e99SRandall Stewart 		}
502042551e99SRandall Stewart 	}
5021f8829a4aSRandall Stewart 	m = control->data;
5022f8829a4aSRandall Stewart 	control->held_length = 0;
5023f8829a4aSRandall Stewart 	control->length = 0;
5024f8829a4aSRandall Stewart 	while (m) {
5025139bc87fSRandall Stewart 		if (SCTP_BUF_LEN(m) == 0) {
5026f8829a4aSRandall Stewart 			/* Skip mbufs with NO length */
5027f8829a4aSRandall Stewart 			if (prev == NULL) {
5028f8829a4aSRandall Stewart 				/* First one */
5029f8829a4aSRandall Stewart 				control->data = sctp_m_free(m);
5030f8829a4aSRandall Stewart 				m = control->data;
5031f8829a4aSRandall Stewart 			} else {
5032139bc87fSRandall Stewart 				SCTP_BUF_NEXT(prev) = sctp_m_free(m);
5033139bc87fSRandall Stewart 				m = SCTP_BUF_NEXT(prev);
5034f8829a4aSRandall Stewart 			}
5035f8829a4aSRandall Stewart 			if (m == NULL) {
5036c2ede4b3SMartin Blapp 				control->tail_mbuf = prev;
5037f8829a4aSRandall Stewart 			}
5038f8829a4aSRandall Stewart 			continue;
5039f8829a4aSRandall Stewart 		}
5040f8829a4aSRandall Stewart 		prev = m;
5041b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5042139bc87fSRandall Stewart 			sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
504380fefe0aSRandall Stewart 		}
5044f8829a4aSRandall Stewart 		sctp_sballoc(stcb, sb, m);
5045b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5046f8829a4aSRandall Stewart 			sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
504780fefe0aSRandall Stewart 		}
5048139bc87fSRandall Stewart 		atomic_add_int(&control->length, SCTP_BUF_LEN(m));
5049139bc87fSRandall Stewart 		m = SCTP_BUF_NEXT(m);
5050f8829a4aSRandall Stewart 	}
5051f8829a4aSRandall Stewart 	if (prev != NULL) {
5052f8829a4aSRandall Stewart 		control->tail_mbuf = prev;
5053f8829a4aSRandall Stewart 	} else {
5054139bc87fSRandall Stewart 		/* Everything got collapsed out?? */
50558a3cfbffSMichael Tuexen 		if (!control->on_strm_q) {
5056cd1386abSMichael Tuexen 			sctp_free_remote_addr(control->whoFrom);
505744249214SRandall Stewart 			sctp_free_a_readq(stcb, control);
50588a3cfbffSMichael Tuexen 		}
5059cfde3ff7SRandall Stewart 		if (inp_read_lock_held == 0)
506047a490cbSMichael Tuexen 			SCTP_INP_READ_UNLOCK(inp);
5061f8829a4aSRandall Stewart 		return;
5062f8829a4aSRandall Stewart 	}
5063f8829a4aSRandall Stewart 	if (end) {
5064f8829a4aSRandall Stewart 		control->end_added = 1;
5065f8829a4aSRandall Stewart 	}
5066f8829a4aSRandall Stewart 	TAILQ_INSERT_TAIL(&inp->read_queue, control, next);
506744249214SRandall Stewart 	control->on_read_q = 1;
5068cfde3ff7SRandall Stewart 	if (inp_read_lock_held == 0)
5069f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
5070f8829a4aSRandall Stewart 	if (inp && inp->sctp_socket) {
5071b1deed45SMichael Tuexen 		sctp_wakeup_the_read_socket(inp, stcb, so_locked);
5072f8829a4aSRandall Stewart 	}
5073f8829a4aSRandall Stewart }
5074f8829a4aSRandall Stewart 
5075f8829a4aSRandall Stewart /*************HOLD THIS COMMENT FOR PATCH FILE OF
5076f8829a4aSRandall Stewart  *************ALTERNATE ROUTING CODE
5077f8829a4aSRandall Stewart  */
5078f8829a4aSRandall Stewart 
5079f8829a4aSRandall Stewart /*************HOLD THIS COMMENT FOR END OF PATCH FILE OF
5080f8829a4aSRandall Stewart  *************ALTERNATE ROUTING CODE
5081f8829a4aSRandall Stewart  */
5082f8829a4aSRandall Stewart 
5083f8829a4aSRandall Stewart struct mbuf *
5084ff1ffd74SMichael Tuexen sctp_generate_cause(uint16_t code, char *info)
5085f8829a4aSRandall Stewart {
5086f8829a4aSRandall Stewart 	struct mbuf *m;
5087ff1ffd74SMichael Tuexen 	struct sctp_gen_error_cause *cause;
50889a8e3088SMichael Tuexen 	size_t info_len;
50899a8e3088SMichael Tuexen 	uint16_t len;
5090f8829a4aSRandall Stewart 
5091ff1ffd74SMichael Tuexen 	if ((code == 0) || (info == NULL)) {
5092ff1ffd74SMichael Tuexen 		return (NULL);
5093ff1ffd74SMichael Tuexen 	}
5094ff1ffd74SMichael Tuexen 	info_len = strlen(info);
50959a8e3088SMichael Tuexen 	if (info_len > (SCTP_MAX_CAUSE_LENGTH - sizeof(struct sctp_paramhdr))) {
50969a8e3088SMichael Tuexen 		return (NULL);
50979a8e3088SMichael Tuexen 	}
50989a8e3088SMichael Tuexen 	len = (uint16_t)(sizeof(struct sctp_paramhdr) + info_len);
5099ff1ffd74SMichael Tuexen 	m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
5100ff1ffd74SMichael Tuexen 	if (m != NULL) {
5101ff1ffd74SMichael Tuexen 		SCTP_BUF_LEN(m) = len;
5102ff1ffd74SMichael Tuexen 		cause = mtod(m, struct sctp_gen_error_cause *);
5103ff1ffd74SMichael Tuexen 		cause->code = htons(code);
51049a8e3088SMichael Tuexen 		cause->length = htons(len);
5105ff1ffd74SMichael Tuexen 		memcpy(cause->info, info, info_len);
5106f8829a4aSRandall Stewart 	}
5107f8829a4aSRandall Stewart 	return (m);
5108f8829a4aSRandall Stewart }
5109f8829a4aSRandall Stewart 
511032451da4SMichael Tuexen struct mbuf *
511132451da4SMichael Tuexen sctp_generate_no_user_data_cause(uint32_t tsn)
511232451da4SMichael Tuexen {
511332451da4SMichael Tuexen 	struct mbuf *m;
511432451da4SMichael Tuexen 	struct sctp_error_no_user_data *no_user_data_cause;
51159a8e3088SMichael Tuexen 	uint16_t len;
511632451da4SMichael Tuexen 
51179a8e3088SMichael Tuexen 	len = (uint16_t)sizeof(struct sctp_error_no_user_data);
511832451da4SMichael Tuexen 	m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
511932451da4SMichael Tuexen 	if (m != NULL) {
512032451da4SMichael Tuexen 		SCTP_BUF_LEN(m) = len;
512132451da4SMichael Tuexen 		no_user_data_cause = mtod(m, struct sctp_error_no_user_data *);
512232451da4SMichael Tuexen 		no_user_data_cause->cause.code = htons(SCTP_CAUSE_NO_USER_DATA);
51239a8e3088SMichael Tuexen 		no_user_data_cause->cause.length = htons(len);
51248b9c95f4SMichael Tuexen 		no_user_data_cause->tsn = htonl(tsn);
512532451da4SMichael Tuexen 	}
512632451da4SMichael Tuexen 	return (m);
512732451da4SMichael Tuexen }
512832451da4SMichael Tuexen 
5129f8829a4aSRandall Stewart #ifdef SCTP_MBCNT_LOGGING
5130f8829a4aSRandall Stewart void
5131f8829a4aSRandall Stewart sctp_free_bufspace(struct sctp_tcb *stcb, struct sctp_association *asoc,
5132f8829a4aSRandall Stewart     struct sctp_tmit_chunk *tp1, int chk_cnt)
5133f8829a4aSRandall Stewart {
5134f8829a4aSRandall Stewart 	if (tp1->data == NULL) {
5135f8829a4aSRandall Stewart 		return;
5136f8829a4aSRandall Stewart 	}
5137f8829a4aSRandall Stewart 	asoc->chunks_on_out_queue -= chk_cnt;
5138b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBCNT_LOGGING_ENABLE) {
5139f8829a4aSRandall Stewart 		sctp_log_mbcnt(SCTP_LOG_MBCNT_DECREASE,
5140f8829a4aSRandall Stewart 		    asoc->total_output_queue_size,
5141f8829a4aSRandall Stewart 		    tp1->book_size,
5142f8829a4aSRandall Stewart 		    0,
5143f8829a4aSRandall Stewart 		    tp1->mbcnt);
514480fefe0aSRandall Stewart 	}
5145f8829a4aSRandall Stewart 	if (asoc->total_output_queue_size >= tp1->book_size) {
514644b7479bSRandall Stewart 		atomic_add_int(&asoc->total_output_queue_size, -tp1->book_size);
5147f8829a4aSRandall Stewart 	} else {
5148f8829a4aSRandall Stewart 		asoc->total_output_queue_size = 0;
5149f8829a4aSRandall Stewart 	}
5150f8829a4aSRandall Stewart 
5151f8829a4aSRandall Stewart 	if (stcb->sctp_socket && (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) ||
5152f8829a4aSRandall Stewart 	    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) {
51534e88d37aSMichael Tuexen 		if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) {
51544e88d37aSMichael Tuexen 			stcb->sctp_socket->so_snd.sb_cc -= tp1->book_size;
5155f8829a4aSRandall Stewart 		} else {
51564e88d37aSMichael Tuexen 			stcb->sctp_socket->so_snd.sb_cc = 0;
5157f8829a4aSRandall Stewart 
5158f8829a4aSRandall Stewart 		}
5159f8829a4aSRandall Stewart 	}
5160f8829a4aSRandall Stewart }
5161f8829a4aSRandall Stewart 
5162f8829a4aSRandall Stewart #endif
5163f8829a4aSRandall Stewart 
5164f8829a4aSRandall Stewart int
5165f8829a4aSRandall Stewart sctp_release_pr_sctp_chunk(struct sctp_tcb *stcb, struct sctp_tmit_chunk *tp1,
51661edc9dbaSMichael Tuexen     uint8_t sent, int so_locked
5167ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
5168ceaad40aSRandall Stewart     SCTP_UNUSED
5169ceaad40aSRandall Stewart #endif
5170ceaad40aSRandall Stewart )
5171f8829a4aSRandall Stewart {
51720c0982b8SRandall Stewart 	struct sctp_stream_out *strq;
51734a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk = NULL, *tp2;
51740c0982b8SRandall Stewart 	struct sctp_stream_queue_pending *sp;
517549656eefSMichael Tuexen 	uint32_t mid;
517649656eefSMichael Tuexen 	uint16_t sid;
51770c0982b8SRandall Stewart 	uint8_t foundeom = 0;
5178f8829a4aSRandall Stewart 	int ret_sz = 0;
5179f8829a4aSRandall Stewart 	int notdone;
51800c0982b8SRandall Stewart 	int do_wakeup_routine = 0;
5181f8829a4aSRandall Stewart 
518249656eefSMichael Tuexen 	sid = tp1->rec.data.sid;
518349656eefSMichael Tuexen 	mid = tp1->rec.data.mid;
5184f0396ad1SMichael Tuexen 	if (sent || !(tp1->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG)) {
5185f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_sent[0]++;
5186f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
518749656eefSMichael Tuexen 		stcb->asoc.strmout[sid].abandoned_sent[0]++;
5188f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
5189ad15e154SMichael Tuexen 		stcb->asoc.strmout[sid].abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
5190f0396ad1SMichael Tuexen #endif
5191f0396ad1SMichael Tuexen 	} else {
5192f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_unsent[0]++;
5193f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
519449656eefSMichael Tuexen 		stcb->asoc.strmout[sid].abandoned_unsent[0]++;
5195f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
5196ad15e154SMichael Tuexen 		stcb->asoc.strmout[sid].abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
5197f0396ad1SMichael Tuexen #endif
5198f0396ad1SMichael Tuexen 	}
5199f8829a4aSRandall Stewart 	do {
5200f8829a4aSRandall Stewart 		ret_sz += tp1->book_size;
52010c0982b8SRandall Stewart 		if (tp1->data != NULL) {
52028933fa13SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5203830d754dSRandall Stewart 				sctp_flight_size_decrease(tp1);
5204830d754dSRandall Stewart 				sctp_total_flight_decrease(stcb, tp1);
52058933fa13SRandall Stewart 			}
52068933fa13SRandall Stewart 			sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
52070c0982b8SRandall Stewart 			stcb->asoc.peers_rwnd += tp1->send_size;
52080c0982b8SRandall Stewart 			stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
52091edc9dbaSMichael Tuexen 			if (sent) {
52101edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
52111edc9dbaSMichael Tuexen 			} else {
52121edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
52131edc9dbaSMichael Tuexen 			}
52142f99457bSMichael Tuexen 			if (tp1->data) {
5215f8829a4aSRandall Stewart 				sctp_m_freem(tp1->data);
5216f8829a4aSRandall Stewart 				tp1->data = NULL;
52172f99457bSMichael Tuexen 			}
52180c0982b8SRandall Stewart 			do_wakeup_routine = 1;
5219f8829a4aSRandall Stewart 			if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
5220f8829a4aSRandall Stewart 				stcb->asoc.sent_queue_cnt_removeable--;
5221f8829a4aSRandall Stewart 			}
5222f8829a4aSRandall Stewart 		}
52238933fa13SRandall Stewart 		tp1->sent = SCTP_FORWARD_TSN_SKIP;
5224f8829a4aSRandall Stewart 		if ((tp1->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) ==
5225f8829a4aSRandall Stewart 		    SCTP_DATA_NOT_FRAG) {
5226f8829a4aSRandall Stewart 			/* not frag'ed we ae done   */
5227f8829a4aSRandall Stewart 			notdone = 0;
5228f8829a4aSRandall Stewart 			foundeom = 1;
5229f8829a4aSRandall Stewart 		} else if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
5230f8829a4aSRandall Stewart 			/* end of frag, we are done */
5231f8829a4aSRandall Stewart 			notdone = 0;
5232f8829a4aSRandall Stewart 			foundeom = 1;
5233f8829a4aSRandall Stewart 		} else {
5234f8829a4aSRandall Stewart 			/*
5235f8829a4aSRandall Stewart 			 * Its a begin or middle piece, we must mark all of
5236f8829a4aSRandall Stewart 			 * it
5237f8829a4aSRandall Stewart 			 */
5238f8829a4aSRandall Stewart 			notdone = 1;
5239f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
5240f8829a4aSRandall Stewart 		}
5241f8829a4aSRandall Stewart 	} while (tp1 && notdone);
52420c0982b8SRandall Stewart 	if (foundeom == 0) {
5243f8829a4aSRandall Stewart 		/*
5244f8829a4aSRandall Stewart 		 * The multi-part message was scattered across the send and
5245f8829a4aSRandall Stewart 		 * sent queue.
5246f8829a4aSRandall Stewart 		 */
52474a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(tp1, &stcb->asoc.send_queue, sctp_next, tp2) {
524849656eefSMichael Tuexen 			if ((tp1->rec.data.sid != sid) ||
524949656eefSMichael Tuexen 			    (!SCTP_MID_EQ(stcb->asoc.idata_supported, tp1->rec.data.mid, mid))) {
52504a9ef3f8SMichael Tuexen 				break;
52514a9ef3f8SMichael Tuexen 			}
52520c0982b8SRandall Stewart 			/*
52530c0982b8SRandall Stewart 			 * save to chk in case we have some on stream out
52540c0982b8SRandall Stewart 			 * queue. If so and we have an un-transmitted one we
52550c0982b8SRandall Stewart 			 * don't have to fudge the TSN.
52560c0982b8SRandall Stewart 			 */
52570c0982b8SRandall Stewart 			chk = tp1;
52580c0982b8SRandall Stewart 			ret_sz += tp1->book_size;
52590c0982b8SRandall Stewart 			sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
52601edc9dbaSMichael Tuexen 			if (sent) {
52611edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
52621edc9dbaSMichael Tuexen 			} else {
52631edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
52641edc9dbaSMichael Tuexen 			}
52652f99457bSMichael Tuexen 			if (tp1->data) {
52660c0982b8SRandall Stewart 				sctp_m_freem(tp1->data);
52672f99457bSMichael Tuexen 				tp1->data = NULL;
52682f99457bSMichael Tuexen 			}
52698933fa13SRandall Stewart 			/* No flight involved here book the size to 0 */
52708933fa13SRandall Stewart 			tp1->book_size = 0;
52710c0982b8SRandall Stewart 			if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
52720c0982b8SRandall Stewart 				foundeom = 1;
5273f8829a4aSRandall Stewart 			}
52740c0982b8SRandall Stewart 			do_wakeup_routine = 1;
52750c0982b8SRandall Stewart 			tp1->sent = SCTP_FORWARD_TSN_SKIP;
52760c0982b8SRandall Stewart 			TAILQ_REMOVE(&stcb->asoc.send_queue, tp1, sctp_next);
5277b7b84c0eSMichael Tuexen 			/*
5278b7b84c0eSMichael Tuexen 			 * on to the sent queue so we can wait for it to be
5279b7b84c0eSMichael Tuexen 			 * passed by.
5280b7b84c0eSMichael Tuexen 			 */
52810c0982b8SRandall Stewart 			TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, tp1,
52820c0982b8SRandall Stewart 			    sctp_next);
52830c0982b8SRandall Stewart 			stcb->asoc.send_queue_cnt--;
52840c0982b8SRandall Stewart 			stcb->asoc.sent_queue_cnt++;
52850c0982b8SRandall Stewart 		}
52860c0982b8SRandall Stewart 	}
52870c0982b8SRandall Stewart 	if (foundeom == 0) {
52880c0982b8SRandall Stewart 		/*
52890c0982b8SRandall Stewart 		 * Still no eom found. That means there is stuff left on the
52900c0982b8SRandall Stewart 		 * stream out queue.. yuck.
52910c0982b8SRandall Stewart 		 */
52920c0982b8SRandall Stewart 		SCTP_TCB_SEND_LOCK(stcb);
529349656eefSMichael Tuexen 		strq = &stcb->asoc.strmout[sid];
5294f3b05218SMichael Tuexen 		sp = TAILQ_FIRST(&strq->outqueue);
5295f3b05218SMichael Tuexen 		if (sp != NULL) {
52960c0982b8SRandall Stewart 			sp->discard_rest = 1;
52970c0982b8SRandall Stewart 			/*
5298f3b05218SMichael Tuexen 			 * We may need to put a chunk on the queue that
5299f3b05218SMichael Tuexen 			 * holds the TSN that would have been sent with the
5300f3b05218SMichael Tuexen 			 * LAST bit.
53010c0982b8SRandall Stewart 			 */
53020c0982b8SRandall Stewart 			if (chk == NULL) {
53030c0982b8SRandall Stewart 				/* Yep, we have to */
53040c0982b8SRandall Stewart 				sctp_alloc_a_chunk(stcb, chk);
53050c0982b8SRandall Stewart 				if (chk == NULL) {
53060c0982b8SRandall Stewart 					/*
5307f3b05218SMichael Tuexen 					 * we are hosed. All we can do is
5308f3b05218SMichael Tuexen 					 * nothing.. which will cause an
5309f3b05218SMichael Tuexen 					 * abort if the peer is paying
53100c0982b8SRandall Stewart 					 * attention.
53110c0982b8SRandall Stewart 					 */
53120c0982b8SRandall Stewart 					goto oh_well;
53130c0982b8SRandall Stewart 				}
53140c0982b8SRandall Stewart 				memset(chk, 0, sizeof(*chk));
531563d5b568SMichael Tuexen 				chk->rec.data.rcv_flags = 0;
53160c0982b8SRandall Stewart 				chk->sent = SCTP_FORWARD_TSN_SKIP;
53170c0982b8SRandall Stewart 				chk->asoc = &stcb->asoc;
531863d5b568SMichael Tuexen 				if (stcb->asoc.idata_supported == 0) {
531963d5b568SMichael Tuexen 					if (sp->sinfo_flags & SCTP_UNORDERED) {
532049656eefSMichael Tuexen 						chk->rec.data.mid = 0;
532163d5b568SMichael Tuexen 					} else {
532249656eefSMichael Tuexen 						chk->rec.data.mid = strq->next_mid_ordered;
532363d5b568SMichael Tuexen 					}
532463d5b568SMichael Tuexen 				} else {
532563d5b568SMichael Tuexen 					if (sp->sinfo_flags & SCTP_UNORDERED) {
532649656eefSMichael Tuexen 						chk->rec.data.mid = strq->next_mid_unordered;
532763d5b568SMichael Tuexen 					} else {
532849656eefSMichael Tuexen 						chk->rec.data.mid = strq->next_mid_ordered;
532963d5b568SMichael Tuexen 					}
533063d5b568SMichael Tuexen 				}
533149656eefSMichael Tuexen 				chk->rec.data.sid = sp->sid;
533249656eefSMichael Tuexen 				chk->rec.data.ppid = sp->ppid;
53330c0982b8SRandall Stewart 				chk->rec.data.context = sp->context;
53340c0982b8SRandall Stewart 				chk->flags = sp->act_flags;
53357fd5b436SMichael Tuexen 				chk->whoTo = NULL;
533649656eefSMichael Tuexen 				chk->rec.data.tsn = atomic_fetchadd_int(&stcb->asoc.sending_seq, 1);
53377fd5b436SMichael Tuexen 				strq->chunks_on_queues++;
53380c0982b8SRandall Stewart 				TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, chk, sctp_next);
53390c0982b8SRandall Stewart 				stcb->asoc.sent_queue_cnt++;
53408933fa13SRandall Stewart 				stcb->asoc.pr_sctp_cnt++;
53410c0982b8SRandall Stewart 			}
534263d5b568SMichael Tuexen 			chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
5343d1ea5fa9SMichael Tuexen 			if (sp->sinfo_flags & SCTP_UNORDERED) {
5344d1ea5fa9SMichael Tuexen 				chk->rec.data.rcv_flags |= SCTP_DATA_UNORDERED;
5345d1ea5fa9SMichael Tuexen 			}
534663d5b568SMichael Tuexen 			if (stcb->asoc.idata_supported == 0) {
534763d5b568SMichael Tuexen 				if ((sp->sinfo_flags & SCTP_UNORDERED) == 0) {
534863d5b568SMichael Tuexen 					strq->next_mid_ordered++;
534963d5b568SMichael Tuexen 				}
535063d5b568SMichael Tuexen 			} else {
535163d5b568SMichael Tuexen 				if (sp->sinfo_flags & SCTP_UNORDERED) {
535263d5b568SMichael Tuexen 					strq->next_mid_unordered++;
535363d5b568SMichael Tuexen 				} else {
535463d5b568SMichael Tuexen 					strq->next_mid_ordered++;
535563d5b568SMichael Tuexen 				}
535663d5b568SMichael Tuexen 			}
53570c0982b8SRandall Stewart 	oh_well:
53580c0982b8SRandall Stewart 			if (sp->data) {
53590c0982b8SRandall Stewart 				/*
5360f3b05218SMichael Tuexen 				 * Pull any data to free up the SB and allow
5361f3b05218SMichael Tuexen 				 * sender to "add more" while we will throw
5362f3b05218SMichael Tuexen 				 * away :-)
53630c0982b8SRandall Stewart 				 */
5364f3b05218SMichael Tuexen 				sctp_free_spbufspace(stcb, &stcb->asoc, sp);
53650c0982b8SRandall Stewart 				ret_sz += sp->length;
53660c0982b8SRandall Stewart 				do_wakeup_routine = 1;
53670c0982b8SRandall Stewart 				sp->some_taken = 1;
53680c0982b8SRandall Stewart 				sctp_m_freem(sp->data);
53690c0982b8SRandall Stewart 				sp->data = NULL;
53700c0982b8SRandall Stewart 				sp->tail_mbuf = NULL;
5371d07b2ac6SMichael Tuexen 				sp->length = 0;
53720c0982b8SRandall Stewart 			}
53730c0982b8SRandall Stewart 		}
53740c0982b8SRandall Stewart 		SCTP_TCB_SEND_UNLOCK(stcb);
53750c0982b8SRandall Stewart 	}
53760c0982b8SRandall Stewart 	if (do_wakeup_routine) {
53770c0982b8SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
53788933fa13SRandall Stewart 		struct socket *so;
53798933fa13SRandall Stewart 
53800c0982b8SRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
53810c0982b8SRandall Stewart 		if (!so_locked) {
53820c0982b8SRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, 1);
53830c0982b8SRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
53840c0982b8SRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
53850c0982b8SRandall Stewart 			SCTP_TCB_LOCK(stcb);
53860c0982b8SRandall Stewart 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
53870c0982b8SRandall Stewart 			if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
53880c0982b8SRandall Stewart 				/* assoc was freed while we were unlocked */
53890c0982b8SRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
53900c0982b8SRandall Stewart 				return (ret_sz);
53910c0982b8SRandall Stewart 			}
53920c0982b8SRandall Stewart 		}
53930c0982b8SRandall Stewart #endif
53940c0982b8SRandall Stewart 		sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
53950c0982b8SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
53960c0982b8SRandall Stewart 		if (!so_locked) {
53970c0982b8SRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
53980c0982b8SRandall Stewart 		}
53990c0982b8SRandall Stewart #endif
5400f8829a4aSRandall Stewart 	}
5401f8829a4aSRandall Stewart 	return (ret_sz);
5402f8829a4aSRandall Stewart }
5403f8829a4aSRandall Stewart 
5404f8829a4aSRandall Stewart /*
5405f8829a4aSRandall Stewart  * checks to see if the given address, sa, is one that is currently known by
5406f8829a4aSRandall Stewart  * the kernel note: can't distinguish the same address on multiple interfaces
5407f8829a4aSRandall Stewart  * and doesn't handle multiple addresses with different zone/scope id's note:
5408f8829a4aSRandall Stewart  * ifa_ifwithaddr() compares the entire sockaddr struct
5409f8829a4aSRandall Stewart  */
541042551e99SRandall Stewart struct sctp_ifa *
541180fefe0aSRandall Stewart sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr,
541280fefe0aSRandall Stewart     int holds_lock)
5413f8829a4aSRandall Stewart {
541442551e99SRandall Stewart 	struct sctp_laddr *laddr;
5415f8829a4aSRandall Stewart 
5416ad81507eSRandall Stewart 	if (holds_lock == 0) {
541742551e99SRandall Stewart 		SCTP_INP_RLOCK(inp);
5418ad81507eSRandall Stewart 	}
54190053ed28SMichael Tuexen 
542042551e99SRandall Stewart 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
542142551e99SRandall Stewart 		if (laddr->ifa == NULL)
5422f8829a4aSRandall Stewart 			continue;
542342551e99SRandall Stewart 		if (addr->sa_family != laddr->ifa->address.sa.sa_family)
542442551e99SRandall Stewart 			continue;
5425e6194c2eSMichael Tuexen #ifdef INET
542642551e99SRandall Stewart 		if (addr->sa_family == AF_INET) {
542742551e99SRandall Stewart 			if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
542842551e99SRandall Stewart 			    laddr->ifa->address.sin.sin_addr.s_addr) {
542942551e99SRandall Stewart 				/* found him. */
5430ad81507eSRandall Stewart 				if (holds_lock == 0) {
543142551e99SRandall Stewart 					SCTP_INP_RUNLOCK(inp);
5432ad81507eSRandall Stewart 				}
543342551e99SRandall Stewart 				return (laddr->ifa);
543442551e99SRandall Stewart 				break;
543542551e99SRandall Stewart 			}
54365e2c2d87SRandall Stewart 		}
5437e6194c2eSMichael Tuexen #endif
54385e2c2d87SRandall Stewart #ifdef INET6
54395e2c2d87SRandall Stewart 		if (addr->sa_family == AF_INET6) {
5440c54a18d2SRandall Stewart 			if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5441c54a18d2SRandall Stewart 			    &laddr->ifa->address.sin6)) {
544242551e99SRandall Stewart 				/* found him. */
5443ad81507eSRandall Stewart 				if (holds_lock == 0) {
544442551e99SRandall Stewart 					SCTP_INP_RUNLOCK(inp);
5445ad81507eSRandall Stewart 				}
544642551e99SRandall Stewart 				return (laddr->ifa);
544742551e99SRandall Stewart 				break;
544842551e99SRandall Stewart 			}
544942551e99SRandall Stewart 		}
54505e2c2d87SRandall Stewart #endif
545142551e99SRandall Stewart 	}
5452ad81507eSRandall Stewart 	if (holds_lock == 0) {
545342551e99SRandall Stewart 		SCTP_INP_RUNLOCK(inp);
5454ad81507eSRandall Stewart 	}
545542551e99SRandall Stewart 	return (NULL);
545642551e99SRandall Stewart }
5457f8829a4aSRandall Stewart 
54586a27c376SRandall Stewart uint32_t
5459b0471b4bSMichael Tuexen sctp_get_ifa_hash_val(struct sockaddr *addr)
5460b0471b4bSMichael Tuexen {
5461ea5eba11SMichael Tuexen 	switch (addr->sa_family) {
5462ea5eba11SMichael Tuexen #ifdef INET
5463ea5eba11SMichael Tuexen 	case AF_INET:
5464ea5eba11SMichael Tuexen 		{
54656a27c376SRandall Stewart 			struct sockaddr_in *sin;
54666a27c376SRandall Stewart 
54676a27c376SRandall Stewart 			sin = (struct sockaddr_in *)addr;
54686a27c376SRandall Stewart 			return (sin->sin_addr.s_addr ^ (sin->sin_addr.s_addr >> 16));
5469ea5eba11SMichael Tuexen 		}
5470ea5eba11SMichael Tuexen #endif
5471ea5eba11SMichael Tuexen #ifdef INET6
54722c2e3218SMichael Tuexen 	case AF_INET6:
5473ea5eba11SMichael Tuexen 		{
54746a27c376SRandall Stewart 			struct sockaddr_in6 *sin6;
54756a27c376SRandall Stewart 			uint32_t hash_of_addr;
54766a27c376SRandall Stewart 
54776a27c376SRandall Stewart 			sin6 = (struct sockaddr_in6 *)addr;
54786a27c376SRandall Stewart 			hash_of_addr = (sin6->sin6_addr.s6_addr32[0] +
54796a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[1] +
54806a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[2] +
54816a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[3]);
54826a27c376SRandall Stewart 			hash_of_addr = (hash_of_addr ^ (hash_of_addr >> 16));
54836a27c376SRandall Stewart 			return (hash_of_addr);
54846a27c376SRandall Stewart 		}
5485ea5eba11SMichael Tuexen #endif
5486ea5eba11SMichael Tuexen 	default:
5487ea5eba11SMichael Tuexen 		break;
5488ea5eba11SMichael Tuexen 	}
54896a27c376SRandall Stewart 	return (0);
54906a27c376SRandall Stewart }
54916a27c376SRandall Stewart 
549242551e99SRandall Stewart struct sctp_ifa *
549342551e99SRandall Stewart sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock)
549442551e99SRandall Stewart {
549542551e99SRandall Stewart 	struct sctp_ifa *sctp_ifap;
549642551e99SRandall Stewart 	struct sctp_vrf *vrf;
54976a27c376SRandall Stewart 	struct sctp_ifalist *hash_head;
54986a27c376SRandall Stewart 	uint32_t hash_of_addr;
549942551e99SRandall Stewart 
550042551e99SRandall Stewart 	if (holds_lock == 0)
5501c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RLOCK();
550242551e99SRandall Stewart 
5503bff64a4dSRandall Stewart 	vrf = sctp_find_vrf(vrf_id);
5504bff64a4dSRandall Stewart 	if (vrf == NULL) {
5505bff64a4dSRandall Stewart 		if (holds_lock == 0)
5506c99efcf6SRandall Stewart 			SCTP_IPI_ADDR_RUNLOCK();
5507bff64a4dSRandall Stewart 		return (NULL);
5508bff64a4dSRandall Stewart 	}
55090053ed28SMichael Tuexen 
5510bff64a4dSRandall Stewart 	hash_of_addr = sctp_get_ifa_hash_val(addr);
5511bff64a4dSRandall Stewart 
551217205eccSRandall Stewart 	hash_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
5513bff64a4dSRandall Stewart 	if (hash_head == NULL) {
5514ad81507eSRandall Stewart 		SCTP_PRINTF("hash_of_addr:%x mask:%x table:%x - ",
5515c99efcf6SRandall Stewart 		    hash_of_addr, (uint32_t)vrf->vrf_addr_hashmark,
5516c99efcf6SRandall Stewart 		    (uint32_t)(hash_of_addr & vrf->vrf_addr_hashmark));
5517bff64a4dSRandall Stewart 		sctp_print_address(addr);
5518ad81507eSRandall Stewart 		SCTP_PRINTF("No such bucket for address\n");
5519bff64a4dSRandall Stewart 		if (holds_lock == 0)
5520c99efcf6SRandall Stewart 			SCTP_IPI_ADDR_RUNLOCK();
5521bff64a4dSRandall Stewart 
5522bff64a4dSRandall Stewart 		return (NULL);
5523bff64a4dSRandall Stewart 	}
55246a27c376SRandall Stewart 	LIST_FOREACH(sctp_ifap, hash_head, next_bucket) {
55256a27c376SRandall Stewart 		if (addr->sa_family != sctp_ifap->address.sa.sa_family)
55266a27c376SRandall Stewart 			continue;
5527e6194c2eSMichael Tuexen #ifdef INET
55286a27c376SRandall Stewart 		if (addr->sa_family == AF_INET) {
55296a27c376SRandall Stewart 			if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
55306a27c376SRandall Stewart 			    sctp_ifap->address.sin.sin_addr.s_addr) {
55316a27c376SRandall Stewart 				/* found him. */
553242551e99SRandall Stewart 				if (holds_lock == 0)
5533c99efcf6SRandall Stewart 					SCTP_IPI_ADDR_RUNLOCK();
553442551e99SRandall Stewart 				return (sctp_ifap);
55356a27c376SRandall Stewart 				break;
55366a27c376SRandall Stewart 			}
55375e2c2d87SRandall Stewart 		}
5538e6194c2eSMichael Tuexen #endif
55395e2c2d87SRandall Stewart #ifdef INET6
55405e2c2d87SRandall Stewart 		if (addr->sa_family == AF_INET6) {
5541c54a18d2SRandall Stewart 			if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5542c54a18d2SRandall Stewart 			    &sctp_ifap->address.sin6)) {
55436a27c376SRandall Stewart 				/* found him. */
55446a27c376SRandall Stewart 				if (holds_lock == 0)
5545c99efcf6SRandall Stewart 					SCTP_IPI_ADDR_RUNLOCK();
55466a27c376SRandall Stewart 				return (sctp_ifap);
55476a27c376SRandall Stewart 				break;
55486a27c376SRandall Stewart 			}
554942551e99SRandall Stewart 		}
55505e2c2d87SRandall Stewart #endif
555142551e99SRandall Stewart 	}
555242551e99SRandall Stewart 	if (holds_lock == 0)
5553c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RUNLOCK();
5554f8829a4aSRandall Stewart 	return (NULL);
5555f8829a4aSRandall Stewart }
5556f8829a4aSRandall Stewart 
5557f8829a4aSRandall Stewart static void
55584c9179adSRandall Stewart sctp_user_rcvd(struct sctp_tcb *stcb, uint32_t *freed_so_far, int hold_rlock,
5559f8829a4aSRandall Stewart     uint32_t rwnd_req)
5560f8829a4aSRandall Stewart {
5561f8829a4aSRandall Stewart 	/* User pulled some data, do we need a rwnd update? */
5562868b51f2SMichael Tuexen 	struct epoch_tracker et;
5563f8829a4aSRandall Stewart 	int r_unlocked = 0;
5564f8829a4aSRandall Stewart 	uint32_t dif, rwnd;
5565f8829a4aSRandall Stewart 	struct socket *so = NULL;
5566f8829a4aSRandall Stewart 
5567f8829a4aSRandall Stewart 	if (stcb == NULL)
5568f8829a4aSRandall Stewart 		return;
5569f8829a4aSRandall Stewart 
557050cec919SRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, 1);
5571f8829a4aSRandall Stewart 
5572839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
557361a21880SMichael Tuexen 	    (stcb->asoc.state & (SCTP_STATE_ABOUT_TO_BE_FREED | SCTP_STATE_SHUTDOWN_RECEIVED))) {
5574f8829a4aSRandall Stewart 		/* Pre-check If we are freeing no update */
5575f8829a4aSRandall Stewart 		goto no_lock;
5576f8829a4aSRandall Stewart 	}
5577f8829a4aSRandall Stewart 	SCTP_INP_INCR_REF(stcb->sctp_ep);
5578f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5579f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5580f8829a4aSRandall Stewart 		goto out;
5581f8829a4aSRandall Stewart 	}
5582f8829a4aSRandall Stewart 	so = stcb->sctp_socket;
5583f8829a4aSRandall Stewart 	if (so == NULL) {
5584f8829a4aSRandall Stewart 		goto out;
5585f8829a4aSRandall Stewart 	}
5586f8829a4aSRandall Stewart 	atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far);
5587f8829a4aSRandall Stewart 	/* Have you have freed enough to look */
5588f8829a4aSRandall Stewart 	*freed_so_far = 0;
5589f8829a4aSRandall Stewart 	/* Yep, its worth a look and the lock overhead */
5590f8829a4aSRandall Stewart 
5591f8829a4aSRandall Stewart 	/* Figure out what the rwnd would be */
5592f8829a4aSRandall Stewart 	rwnd = sctp_calc_rwnd(stcb, &stcb->asoc);
5593f8829a4aSRandall Stewart 	if (rwnd >= stcb->asoc.my_last_reported_rwnd) {
5594f8829a4aSRandall Stewart 		dif = rwnd - stcb->asoc.my_last_reported_rwnd;
5595f8829a4aSRandall Stewart 	} else {
5596f8829a4aSRandall Stewart 		dif = 0;
5597f8829a4aSRandall Stewart 	}
5598f8829a4aSRandall Stewart 	if (dif >= rwnd_req) {
5599f8829a4aSRandall Stewart 		if (hold_rlock) {
5600f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5601f8829a4aSRandall Stewart 			r_unlocked = 1;
5602f8829a4aSRandall Stewart 		}
5603f8829a4aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5604f8829a4aSRandall Stewart 			/*
5605f8829a4aSRandall Stewart 			 * One last check before we allow the guy possibly
5606f8829a4aSRandall Stewart 			 * to get in. There is a race, where the guy has not
5607f8829a4aSRandall Stewart 			 * reached the gate. In that case
5608f8829a4aSRandall Stewart 			 */
5609f8829a4aSRandall Stewart 			goto out;
5610f8829a4aSRandall Stewart 		}
5611f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
5612f8829a4aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5613f8829a4aSRandall Stewart 			/* No reports here */
5614f8829a4aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
5615f8829a4aSRandall Stewart 			goto out;
5616f8829a4aSRandall Stewart 		}
5617f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_wu_sacks_sent);
5618868b51f2SMichael Tuexen 		NET_EPOCH_ENTER(et);
5619689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_LOCKED);
5620830d754dSRandall Stewart 
5621f8829a4aSRandall Stewart 		sctp_chunk_output(stcb->sctp_ep, stcb,
5622ceaad40aSRandall Stewart 		    SCTP_OUTPUT_FROM_USR_RCVD, SCTP_SO_LOCKED);
5623f8829a4aSRandall Stewart 		/* make sure no timer is running */
5624868b51f2SMichael Tuexen 		NET_EPOCH_EXIT(et);
5625ba785902SMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
5626ba785902SMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_6);
5627f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
5628f8829a4aSRandall Stewart 	} else {
5629f8829a4aSRandall Stewart 		/* Update how much we have pending */
5630f8829a4aSRandall Stewart 		stcb->freed_by_sorcv_sincelast = dif;
5631f8829a4aSRandall Stewart 	}
5632f8829a4aSRandall Stewart out:
5633f8829a4aSRandall Stewart 	if (so && r_unlocked && hold_rlock) {
5634f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
5635f8829a4aSRandall Stewart 	}
56360053ed28SMichael Tuexen 
5637f8829a4aSRandall Stewart 	SCTP_INP_DECR_REF(stcb->sctp_ep);
5638f8829a4aSRandall Stewart no_lock:
563950cec919SRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, -1);
5640f8829a4aSRandall Stewart 	return;
5641f8829a4aSRandall Stewart }
5642f8829a4aSRandall Stewart 
5643f8829a4aSRandall Stewart int
5644f8829a4aSRandall Stewart sctp_sorecvmsg(struct socket *so,
5645f8829a4aSRandall Stewart     struct uio *uio,
5646f8829a4aSRandall Stewart     struct mbuf **mp,
5647f8829a4aSRandall Stewart     struct sockaddr *from,
5648f8829a4aSRandall Stewart     int fromlen,
5649f8829a4aSRandall Stewart     int *msg_flags,
5650f8829a4aSRandall Stewart     struct sctp_sndrcvinfo *sinfo,
5651f8829a4aSRandall Stewart     int filling_sinfo)
5652f8829a4aSRandall Stewart {
5653f8829a4aSRandall Stewart 	/*
5654f8829a4aSRandall Stewart 	 * MSG flags we will look at MSG_DONTWAIT - non-blocking IO.
5655f8829a4aSRandall Stewart 	 * MSG_PEEK - Look don't touch :-D (only valid with OUT mbuf copy
5656f8829a4aSRandall Stewart 	 * mp=NULL thus uio is the copy method to userland) MSG_WAITALL - ??
5657f8829a4aSRandall Stewart 	 * On the way out we may send out any combination of:
5658f8829a4aSRandall Stewart 	 * MSG_NOTIFICATION MSG_EOR
5659f8829a4aSRandall Stewart 	 *
5660f8829a4aSRandall Stewart 	 */
5661f8829a4aSRandall Stewart 	struct sctp_inpcb *inp = NULL;
566258e6eeefSMichael Tuexen 	ssize_t my_len = 0;
566358e6eeefSMichael Tuexen 	ssize_t cp_len = 0;
56640d3cf13dSMichael Tuexen 	int error = 0;
5665f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL;
566694b0d969SMichael Tuexen 	struct mbuf *m = NULL;
5667f8829a4aSRandall Stewart 	struct sctp_tcb *stcb = NULL;
5668f8829a4aSRandall Stewart 	int wakeup_read_socket = 0;
5669f8829a4aSRandall Stewart 	int freecnt_applied = 0;
5670f8829a4aSRandall Stewart 	int out_flags = 0, in_flags = 0;
5671f8829a4aSRandall Stewart 	int block_allowed = 1;
56724c9179adSRandall Stewart 	uint32_t freed_so_far = 0;
567358e6eeefSMichael Tuexen 	ssize_t copied_so_far = 0;
567493164cf9SRandall Stewart 	int in_eeor_mode = 0;
5675f8829a4aSRandall Stewart 	int no_rcv_needed = 0;
5676f8829a4aSRandall Stewart 	uint32_t rwnd_req = 0;
5677f8829a4aSRandall Stewart 	int hold_sblock = 0;
5678f8829a4aSRandall Stewart 	int hold_rlock = 0;
56799a8e3088SMichael Tuexen 	ssize_t slen = 0;
56804c9179adSRandall Stewart 	uint32_t held_length = 0;
56817abab911SRobert Watson 	int sockbuf_lock = 0;
5682f8829a4aSRandall Stewart 
568317205eccSRandall Stewart 	if (uio == NULL) {
5684c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
568517205eccSRandall Stewart 		return (EINVAL);
568617205eccSRandall Stewart 	}
56870053ed28SMichael Tuexen 
5688f8829a4aSRandall Stewart 	if (msg_flags) {
5689f8829a4aSRandall Stewart 		in_flags = *msg_flags;
5690c105859eSRandall Stewart 		if (in_flags & MSG_PEEK)
5691c105859eSRandall Stewart 			SCTP_STAT_INCR(sctps_read_peeks);
5692f8829a4aSRandall Stewart 	} else {
5693f8829a4aSRandall Stewart 		in_flags = 0;
5694f8829a4aSRandall Stewart 	}
5695f8829a4aSRandall Stewart 	slen = uio->uio_resid;
569617205eccSRandall Stewart 
5697f8829a4aSRandall Stewart 	/* Pull in and set up our int flags */
5698f8829a4aSRandall Stewart 	if (in_flags & MSG_OOB) {
5699f8829a4aSRandall Stewart 		/* Out of band's NOT supported */
5700f8829a4aSRandall Stewart 		return (EOPNOTSUPP);
5701f8829a4aSRandall Stewart 	}
5702f8829a4aSRandall Stewart 	if ((in_flags & MSG_PEEK) && (mp != NULL)) {
5703c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5704f8829a4aSRandall Stewart 		return (EINVAL);
5705f8829a4aSRandall Stewart 	}
5706f8829a4aSRandall Stewart 	if ((in_flags & (MSG_DONTWAIT
5707f8829a4aSRandall Stewart 	    | MSG_NBIO
5708f8829a4aSRandall Stewart 	    )) ||
570942551e99SRandall Stewart 	    SCTP_SO_IS_NBIO(so)) {
5710f8829a4aSRandall Stewart 		block_allowed = 0;
5711f8829a4aSRandall Stewart 	}
5712f8829a4aSRandall Stewart 	/* setup the endpoint */
5713f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
5714f8829a4aSRandall Stewart 	if (inp == NULL) {
5715c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
5716f8829a4aSRandall Stewart 		return (EFAULT);
5717f8829a4aSRandall Stewart 	}
571862c1ff9cSRandall Stewart 	rwnd_req = (SCTP_SB_LIMIT_RCV(so) >> SCTP_RWND_HIWAT_SHIFT);
5719f8829a4aSRandall Stewart 	/* Must be at least a MTU's worth */
5720f8829a4aSRandall Stewart 	if (rwnd_req < SCTP_MIN_RWND)
5721f8829a4aSRandall Stewart 		rwnd_req = SCTP_MIN_RWND;
5722f8829a4aSRandall Stewart 	in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
5723b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5724f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SORECV_ENTER,
57259a8e3088SMichael Tuexen 		    rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, (uint32_t)uio->uio_resid);
572680fefe0aSRandall Stewart 	}
5727b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5728f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SORECV_ENTERPL,
57299a8e3088SMichael Tuexen 		    rwnd_req, block_allowed, so->so_rcv.sb_cc, (uint32_t)uio->uio_resid);
573080fefe0aSRandall Stewart 	}
57310053ed28SMichael Tuexen 
57320053ed28SMichael Tuexen 
5733265de5bbSRobert Watson 	error = sblock(&so->so_rcv, (block_allowed ? SBL_WAIT : 0));
5734f8829a4aSRandall Stewart 	if (error) {
5735f8829a4aSRandall Stewart 		goto release_unlocked;
5736f8829a4aSRandall Stewart 	}
57378e1e6e5fSMateusz Guzik 	sockbuf_lock = 1;
5738f8829a4aSRandall Stewart restart:
57397abab911SRobert Watson 
5740f8829a4aSRandall Stewart 
5741f8829a4aSRandall Stewart restart_nosblocks:
5742f8829a4aSRandall Stewart 	if (hold_sblock == 0) {
5743f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&so->so_rcv);
5744f8829a4aSRandall Stewart 		hold_sblock = 1;
5745f8829a4aSRandall Stewart 	}
5746f8829a4aSRandall Stewart 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5747f8829a4aSRandall Stewart 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5748f8829a4aSRandall Stewart 		goto out;
5749f8829a4aSRandall Stewart 	}
57504e88d37aSMichael Tuexen 	if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) && (so->so_rcv.sb_cc == 0)) {
5751f8829a4aSRandall Stewart 		if (so->so_error) {
5752f8829a4aSRandall Stewart 			error = so->so_error;
575344b7479bSRandall Stewart 			if ((in_flags & MSG_PEEK) == 0)
575444b7479bSRandall Stewart 				so->so_error = 0;
57559f22f500SRandall Stewart 			goto out;
5756f8829a4aSRandall Stewart 		} else {
57574e88d37aSMichael Tuexen 			if (so->so_rcv.sb_cc == 0) {
57587924093fSRandall Stewart 				/* indicate EOF */
57597924093fSRandall Stewart 				error = 0;
5760f8829a4aSRandall Stewart 				goto out;
5761f8829a4aSRandall Stewart 			}
57629f22f500SRandall Stewart 		}
57639f22f500SRandall Stewart 	}
57649de217ceSMichael Tuexen 	if (so->so_rcv.sb_cc <= held_length) {
57659de217ceSMichael Tuexen 		if (so->so_error) {
57669de217ceSMichael Tuexen 			error = so->so_error;
57679de217ceSMichael Tuexen 			if ((in_flags & MSG_PEEK) == 0) {
57689de217ceSMichael Tuexen 				so->so_error = 0;
57699de217ceSMichael Tuexen 			}
57709de217ceSMichael Tuexen 			goto out;
57719de217ceSMichael Tuexen 		}
57724e88d37aSMichael Tuexen 		if ((so->so_rcv.sb_cc == 0) &&
5773f8829a4aSRandall Stewart 		    ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5774f8829a4aSRandall Stewart 		    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
5775f8829a4aSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
5776f8829a4aSRandall Stewart 				/*
5777f8829a4aSRandall Stewart 				 * For active open side clear flags for
5778f8829a4aSRandall Stewart 				 * re-use passive open is blocked by
5779f8829a4aSRandall Stewart 				 * connect.
5780f8829a4aSRandall Stewart 				 */
5781f8829a4aSRandall Stewart 				if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
5782b7b84c0eSMichael Tuexen 					/*
5783b7b84c0eSMichael Tuexen 					 * You were aborted, passive side
5784b7b84c0eSMichael Tuexen 					 * always hits here
5785b7b84c0eSMichael Tuexen 					 */
5786c4739e2fSRandall Stewart 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
5787f8829a4aSRandall Stewart 					error = ECONNRESET;
5788f8829a4aSRandall Stewart 				}
5789f8829a4aSRandall Stewart 				so->so_state &= ~(SS_ISCONNECTING |
5790f8829a4aSRandall Stewart 				    SS_ISDISCONNECTING |
5791f8829a4aSRandall Stewart 				    SS_ISCONFIRMING |
5792f8829a4aSRandall Stewart 				    SS_ISCONNECTED);
5793f8829a4aSRandall Stewart 				if (error == 0) {
5794f8829a4aSRandall Stewart 					if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5795c4739e2fSRandall Stewart 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5796f8829a4aSRandall Stewart 						error = ENOTCONN;
5797f8829a4aSRandall Stewart 					}
5798f8829a4aSRandall Stewart 				}
5799f8829a4aSRandall Stewart 				goto out;
5800f8829a4aSRandall Stewart 			}
5801f8829a4aSRandall Stewart 		}
58029de217ceSMichael Tuexen 		if (block_allowed) {
5803f8829a4aSRandall Stewart 			error = sbwait(&so->so_rcv);
5804f8829a4aSRandall Stewart 			if (error) {
5805f8829a4aSRandall Stewart 				goto out;
5806f8829a4aSRandall Stewart 			}
5807f8829a4aSRandall Stewart 			held_length = 0;
5808f8829a4aSRandall Stewart 			goto restart_nosblocks;
580944b7479bSRandall Stewart 		} else {
5810c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EWOULDBLOCK);
5811f8829a4aSRandall Stewart 			error = EWOULDBLOCK;
5812f8829a4aSRandall Stewart 			goto out;
5813f8829a4aSRandall Stewart 		}
58149de217ceSMichael Tuexen 	}
5815d06c82f1SRandall Stewart 	if (hold_sblock == 1) {
5816d06c82f1SRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
5817d06c82f1SRandall Stewart 		hold_sblock = 0;
5818d06c82f1SRandall Stewart 	}
5819f8829a4aSRandall Stewart 	/* we possibly have data we can read */
58203c503c28SRandall Stewart 	/* sa_ignore FREED_MEMORY */
5821f8829a4aSRandall Stewart 	control = TAILQ_FIRST(&inp->read_queue);
5822f8829a4aSRandall Stewart 	if (control == NULL) {
5823f8829a4aSRandall Stewart 		/*
5824f8829a4aSRandall Stewart 		 * This could be happening since the appender did the
5825f8829a4aSRandall Stewart 		 * increment but as not yet did the tailq insert onto the
5826f8829a4aSRandall Stewart 		 * read_queue
5827f8829a4aSRandall Stewart 		 */
5828f8829a4aSRandall Stewart 		if (hold_rlock == 0) {
5829f8829a4aSRandall Stewart 			SCTP_INP_READ_LOCK(inp);
5830f8829a4aSRandall Stewart 		}
5831f8829a4aSRandall Stewart 		control = TAILQ_FIRST(&inp->read_queue);
58324e88d37aSMichael Tuexen 		if ((control == NULL) && (so->so_rcv.sb_cc != 0)) {
5833a5d547adSRandall Stewart #ifdef INVARIANTS
5834f8829a4aSRandall Stewart 			panic("Huh, its non zero and nothing on control?");
5835f8829a4aSRandall Stewart #endif
58364e88d37aSMichael Tuexen 			so->so_rcv.sb_cc = 0;
5837f8829a4aSRandall Stewart 		}
5838f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
5839f8829a4aSRandall Stewart 		hold_rlock = 0;
5840f8829a4aSRandall Stewart 		goto restart;
5841f8829a4aSRandall Stewart 	}
58420053ed28SMichael Tuexen 
5843f8829a4aSRandall Stewart 	if ((control->length == 0) &&
5844f8829a4aSRandall Stewart 	    (control->do_not_ref_stcb)) {
5845f8829a4aSRandall Stewart 		/*
5846f8829a4aSRandall Stewart 		 * Clean up code for freeing assoc that left behind a
5847f8829a4aSRandall Stewart 		 * pdapi.. maybe a peer in EEOR that just closed after
5848f8829a4aSRandall Stewart 		 * sending and never indicated a EOR.
5849f8829a4aSRandall Stewart 		 */
5850f8829a4aSRandall Stewart 		if (hold_rlock == 0) {
5851f8829a4aSRandall Stewart 			hold_rlock = 1;
5852f8829a4aSRandall Stewart 			SCTP_INP_READ_LOCK(inp);
5853f8829a4aSRandall Stewart 		}
5854f8829a4aSRandall Stewart 		control->held_length = 0;
5855f8829a4aSRandall Stewart 		if (control->data) {
5856f8829a4aSRandall Stewart 			/* Hmm there is data here .. fix */
58574c9179adSRandall Stewart 			struct mbuf *m_tmp;
5858f8829a4aSRandall Stewart 			int cnt = 0;
5859f8829a4aSRandall Stewart 
58604c9179adSRandall Stewart 			m_tmp = control->data;
58614c9179adSRandall Stewart 			while (m_tmp) {
58624c9179adSRandall Stewart 				cnt += SCTP_BUF_LEN(m_tmp);
58634c9179adSRandall Stewart 				if (SCTP_BUF_NEXT(m_tmp) == NULL) {
58644c9179adSRandall Stewart 					control->tail_mbuf = m_tmp;
5865f8829a4aSRandall Stewart 					control->end_added = 1;
5866f8829a4aSRandall Stewart 				}
58674c9179adSRandall Stewart 				m_tmp = SCTP_BUF_NEXT(m_tmp);
5868f8829a4aSRandall Stewart 			}
5869f8829a4aSRandall Stewart 			control->length = cnt;
5870f8829a4aSRandall Stewart 		} else {
5871f8829a4aSRandall Stewart 			/* remove it */
5872f8829a4aSRandall Stewart 			TAILQ_REMOVE(&inp->read_queue, control, next);
5873f8829a4aSRandall Stewart 			/* Add back any hiddend data */
5874f8829a4aSRandall Stewart 			sctp_free_remote_addr(control->whoFrom);
5875f8829a4aSRandall Stewart 			sctp_free_a_readq(stcb, control);
5876f8829a4aSRandall Stewart 		}
5877f8829a4aSRandall Stewart 		if (hold_rlock) {
5878f8829a4aSRandall Stewart 			hold_rlock = 0;
5879f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
5880f8829a4aSRandall Stewart 		}
5881f8829a4aSRandall Stewart 		goto restart;
5882f8829a4aSRandall Stewart 	}
5883810ec536SMichael Tuexen 	if ((control->length == 0) &&
5884810ec536SMichael Tuexen 	    (control->end_added == 1)) {
5885b7b84c0eSMichael Tuexen 		/*
5886b7b84c0eSMichael Tuexen 		 * Do we also need to check for (control->pdapi_aborted ==
5887b7b84c0eSMichael Tuexen 		 * 1)?
5888b7b84c0eSMichael Tuexen 		 */
5889810ec536SMichael Tuexen 		if (hold_rlock == 0) {
5890810ec536SMichael Tuexen 			hold_rlock = 1;
5891810ec536SMichael Tuexen 			SCTP_INP_READ_LOCK(inp);
5892810ec536SMichael Tuexen 		}
5893810ec536SMichael Tuexen 		TAILQ_REMOVE(&inp->read_queue, control, next);
5894810ec536SMichael Tuexen 		if (control->data) {
5895810ec536SMichael Tuexen #ifdef INVARIANTS
5896810ec536SMichael Tuexen 			panic("control->data not null but control->length == 0");
5897810ec536SMichael Tuexen #else
5898810ec536SMichael Tuexen 			SCTP_PRINTF("Strange, data left in the control buffer. Cleaning up.\n");
5899810ec536SMichael Tuexen 			sctp_m_freem(control->data);
5900810ec536SMichael Tuexen 			control->data = NULL;
5901810ec536SMichael Tuexen #endif
5902810ec536SMichael Tuexen 		}
5903810ec536SMichael Tuexen 		if (control->aux_data) {
5904810ec536SMichael Tuexen 			sctp_m_free(control->aux_data);
5905810ec536SMichael Tuexen 			control->aux_data = NULL;
5906810ec536SMichael Tuexen 		}
590798d5fd97SMichael Tuexen #ifdef INVARIANTS
590844249214SRandall Stewart 		if (control->on_strm_q) {
590944249214SRandall Stewart 			panic("About to free ctl:%p so:%p and its in %d",
591044249214SRandall Stewart 			    control, so, control->on_strm_q);
591144249214SRandall Stewart 		}
591298d5fd97SMichael Tuexen #endif
5913810ec536SMichael Tuexen 		sctp_free_remote_addr(control->whoFrom);
5914810ec536SMichael Tuexen 		sctp_free_a_readq(stcb, control);
5915810ec536SMichael Tuexen 		if (hold_rlock) {
5916810ec536SMichael Tuexen 			hold_rlock = 0;
5917810ec536SMichael Tuexen 			SCTP_INP_READ_UNLOCK(inp);
5918810ec536SMichael Tuexen 		}
5919810ec536SMichael Tuexen 		goto restart;
5920810ec536SMichael Tuexen 	}
5921f8829a4aSRandall Stewart 	if (control->length == 0) {
5922f8829a4aSRandall Stewart 		if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
5923f8829a4aSRandall Stewart 		    (filling_sinfo)) {
5924f8829a4aSRandall Stewart 			/* find a more suitable one then this */
5925f8829a4aSRandall Stewart 			ctl = TAILQ_NEXT(control, next);
5926f8829a4aSRandall Stewart 			while (ctl) {
59279a6142d8SRandall Stewart 				if ((ctl->stcb != control->stcb) && (ctl->length) &&
59289a6142d8SRandall Stewart 				    (ctl->some_taken ||
59296114cd96SRandall Stewart 				    (ctl->spec_flags & M_NOTIFICATION) ||
59309a6142d8SRandall Stewart 				    ((ctl->do_not_ref_stcb == 0) &&
59319a6142d8SRandall Stewart 				    (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))
59329a6142d8SRandall Stewart 				    ) {
59339a6142d8SRandall Stewart 					/*-
59349a6142d8SRandall Stewart 					 * If we have a different TCB next, and there is data
59359a6142d8SRandall Stewart 					 * present. If we have already taken some (pdapi), OR we can
59369a6142d8SRandall Stewart 					 * ref the tcb and no delivery as started on this stream, we
593717205eccSRandall Stewart 					 * take it. Note we allow a notification on a different
593817205eccSRandall Stewart 					 * assoc to be delivered..
59399a6142d8SRandall Stewart 					 */
59409a6142d8SRandall Stewart 					control = ctl;
59419a6142d8SRandall Stewart 					goto found_one;
59429a6142d8SRandall Stewart 				} else if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) &&
59439a6142d8SRandall Stewart 					    (ctl->length) &&
59449a6142d8SRandall Stewart 					    ((ctl->some_taken) ||
59459a6142d8SRandall Stewart 					    ((ctl->do_not_ref_stcb == 0) &&
594617205eccSRandall Stewart 					    ((ctl->spec_flags & M_NOTIFICATION) == 0) &&
5947b5c16493SMichael Tuexen 				    (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))) {
59489a6142d8SRandall Stewart 					/*-
59499a6142d8SRandall Stewart 					 * If we have the same tcb, and there is data present, and we
59509a6142d8SRandall Stewart 					 * have the strm interleave feature present. Then if we have
59519a6142d8SRandall Stewart 					 * taken some (pdapi) or we can refer to tht tcb AND we have
59529a6142d8SRandall Stewart 					 * not started a delivery for this stream, we can take it.
595317205eccSRandall Stewart 					 * Note we do NOT allow a notificaiton on the same assoc to
595417205eccSRandall Stewart 					 * be delivered.
59559a6142d8SRandall Stewart 					 */
5956f8829a4aSRandall Stewart 					control = ctl;
5957f8829a4aSRandall Stewart 					goto found_one;
5958f8829a4aSRandall Stewart 				}
5959f8829a4aSRandall Stewart 				ctl = TAILQ_NEXT(ctl, next);
5960f8829a4aSRandall Stewart 			}
5961f8829a4aSRandall Stewart 		}
5962f8829a4aSRandall Stewart 		/*
5963f8829a4aSRandall Stewart 		 * if we reach here, not suitable replacement is available
59644e88d37aSMichael Tuexen 		 * <or> fragment interleave is NOT on. So stuff the sb_cc
5965f8829a4aSRandall Stewart 		 * into the our held count, and its time to sleep again.
5966f8829a4aSRandall Stewart 		 */
59674e88d37aSMichael Tuexen 		held_length = so->so_rcv.sb_cc;
59684e88d37aSMichael Tuexen 		control->held_length = so->so_rcv.sb_cc;
5969f8829a4aSRandall Stewart 		goto restart;
5970f8829a4aSRandall Stewart 	}
5971f8829a4aSRandall Stewart 	/* Clear the held length since there is something to read */
5972f8829a4aSRandall Stewart 	control->held_length = 0;
5973f8829a4aSRandall Stewart found_one:
5974f8829a4aSRandall Stewart 	/*
5975f8829a4aSRandall Stewart 	 * If we reach here, control has a some data for us to read off.
5976f8829a4aSRandall Stewart 	 * Note that stcb COULD be NULL.
5977f8829a4aSRandall Stewart 	 */
59789c5ca6f2SMichael Tuexen 	if (hold_rlock == 0) {
59799c5ca6f2SMichael Tuexen 		hold_rlock = 1;
59809c5ca6f2SMichael Tuexen 		SCTP_INP_READ_LOCK(inp);
5981f8829a4aSRandall Stewart 	}
59829c5ca6f2SMichael Tuexen 	control->some_taken++;
5983f8829a4aSRandall Stewart 	stcb = control->stcb;
5984f8829a4aSRandall Stewart 	if (stcb) {
59850696e120SRandall Stewart 		if ((control->do_not_ref_stcb == 0) &&
59860696e120SRandall Stewart 		    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
598750cec919SRandall Stewart 			if (freecnt_applied == 0)
5988f8829a4aSRandall Stewart 				stcb = NULL;
5989f8829a4aSRandall Stewart 		} else if (control->do_not_ref_stcb == 0) {
5990f8829a4aSRandall Stewart 			/* you can't free it on me please */
5991f8829a4aSRandall Stewart 			/*
5992f8829a4aSRandall Stewart 			 * The lock on the socket buffer protects us so the
5993f8829a4aSRandall Stewart 			 * free code will stop. But since we used the
5994f8829a4aSRandall Stewart 			 * socketbuf lock and the sender uses the tcb_lock
5995f8829a4aSRandall Stewart 			 * to increment, we need to use the atomic add to
5996f8829a4aSRandall Stewart 			 * the refcnt
5997f8829a4aSRandall Stewart 			 */
5998d55b0b1bSRandall Stewart 			if (freecnt_applied) {
5999d55b0b1bSRandall Stewart #ifdef INVARIANTS
6000207304d4SRandall Stewart 				panic("refcnt already incremented");
6001d55b0b1bSRandall Stewart #else
6002cd3fd531SMichael Tuexen 				SCTP_PRINTF("refcnt already incremented?\n");
6003d55b0b1bSRandall Stewart #endif
6004d55b0b1bSRandall Stewart 			} else {
600550cec919SRandall Stewart 				atomic_add_int(&stcb->asoc.refcnt, 1);
6006f8829a4aSRandall Stewart 				freecnt_applied = 1;
6007d55b0b1bSRandall Stewart 			}
6008f8829a4aSRandall Stewart 			/*
6009f8829a4aSRandall Stewart 			 * Setup to remember how much we have not yet told
6010f8829a4aSRandall Stewart 			 * the peer our rwnd has opened up. Note we grab the
6011f8829a4aSRandall Stewart 			 * value from the tcb from last time. Note too that
60120696e120SRandall Stewart 			 * sack sending clears this when a sack is sent,
6013f8829a4aSRandall Stewart 			 * which is fine. Once we hit the rwnd_req, we then
6014f8829a4aSRandall Stewart 			 * will go to the sctp_user_rcvd() that will not
6015f8829a4aSRandall Stewart 			 * lock until it KNOWs it MUST send a WUP-SACK.
6016f8829a4aSRandall Stewart 			 */
601758e6eeefSMichael Tuexen 			freed_so_far = (uint32_t)stcb->freed_by_sorcv_sincelast;
6018f8829a4aSRandall Stewart 			stcb->freed_by_sorcv_sincelast = 0;
6019f8829a4aSRandall Stewart 		}
6020f8829a4aSRandall Stewart 	}
60216114cd96SRandall Stewart 	if (stcb &&
60226114cd96SRandall Stewart 	    ((control->spec_flags & M_NOTIFICATION) == 0) &&
60236114cd96SRandall Stewart 	    control->do_not_ref_stcb == 0) {
6024d06c82f1SRandall Stewart 		stcb->asoc.strmin[control->sinfo_stream].delivery_started = 1;
6025d06c82f1SRandall Stewart 	}
60260053ed28SMichael Tuexen 
6027f8829a4aSRandall Stewart 	/* First lets get off the sinfo and sockaddr info */
60285f05199cSMichael Tuexen 	if ((sinfo != NULL) && (filling_sinfo != 0)) {
60295f05199cSMichael Tuexen 		sinfo->sinfo_stream = control->sinfo_stream;
603049656eefSMichael Tuexen 		sinfo->sinfo_ssn = (uint16_t)control->mid;
60315f05199cSMichael Tuexen 		sinfo->sinfo_flags = control->sinfo_flags;
60325f05199cSMichael Tuexen 		sinfo->sinfo_ppid = control->sinfo_ppid;
60335f05199cSMichael Tuexen 		sinfo->sinfo_context = control->sinfo_context;
60345f05199cSMichael Tuexen 		sinfo->sinfo_timetolive = control->sinfo_timetolive;
60355f05199cSMichael Tuexen 		sinfo->sinfo_tsn = control->sinfo_tsn;
60365f05199cSMichael Tuexen 		sinfo->sinfo_cumtsn = control->sinfo_cumtsn;
60375f05199cSMichael Tuexen 		sinfo->sinfo_assoc_id = control->sinfo_assoc_id;
6038f8829a4aSRandall Stewart 		nxt = TAILQ_NEXT(control, next);
6039e2e7c62eSMichael Tuexen 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
6040e2e7c62eSMichael Tuexen 		    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) {
6041f8829a4aSRandall Stewart 			struct sctp_extrcvinfo *s_extra;
6042f8829a4aSRandall Stewart 
6043f8829a4aSRandall Stewart 			s_extra = (struct sctp_extrcvinfo *)sinfo;
60449a6142d8SRandall Stewart 			if ((nxt) &&
60459a6142d8SRandall Stewart 			    (nxt->length)) {
6046b70b526dSMichael Tuexen 				s_extra->serinfo_next_flags = SCTP_NEXT_MSG_AVAIL;
6047f8829a4aSRandall Stewart 				if (nxt->sinfo_flags & SCTP_UNORDERED) {
6048b70b526dSMichael Tuexen 					s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_IS_UNORDERED;
6049f8829a4aSRandall Stewart 				}
6050f42a358aSRandall Stewart 				if (nxt->spec_flags & M_NOTIFICATION) {
6051b70b526dSMichael Tuexen 					s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_IS_NOTIFICATION;
6052f42a358aSRandall Stewart 				}
6053b70b526dSMichael Tuexen 				s_extra->serinfo_next_aid = nxt->sinfo_assoc_id;
6054b70b526dSMichael Tuexen 				s_extra->serinfo_next_length = nxt->length;
6055b70b526dSMichael Tuexen 				s_extra->serinfo_next_ppid = nxt->sinfo_ppid;
6056b70b526dSMichael Tuexen 				s_extra->serinfo_next_stream = nxt->sinfo_stream;
6057f8829a4aSRandall Stewart 				if (nxt->tail_mbuf != NULL) {
6058139bc87fSRandall Stewart 					if (nxt->end_added) {
6059b70b526dSMichael Tuexen 						s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_ISCOMPLETE;
6060f8829a4aSRandall Stewart 					}
6061f8829a4aSRandall Stewart 				}
6062f8829a4aSRandall Stewart 			} else {
6063f8829a4aSRandall Stewart 				/*
6064f8829a4aSRandall Stewart 				 * we explicitly 0 this, since the memcpy
6065f8829a4aSRandall Stewart 				 * got some other things beyond the older
6066f8829a4aSRandall Stewart 				 * sinfo_ that is on the control's structure
6067f8829a4aSRandall Stewart 				 * :-D
6068f8829a4aSRandall Stewart 				 */
60699a6142d8SRandall Stewart 				nxt = NULL;
6070b70b526dSMichael Tuexen 				s_extra->serinfo_next_flags = SCTP_NO_NEXT_MSG;
6071b70b526dSMichael Tuexen 				s_extra->serinfo_next_aid = 0;
6072b70b526dSMichael Tuexen 				s_extra->serinfo_next_length = 0;
6073b70b526dSMichael Tuexen 				s_extra->serinfo_next_ppid = 0;
6074b70b526dSMichael Tuexen 				s_extra->serinfo_next_stream = 0;
6075f8829a4aSRandall Stewart 			}
6076f8829a4aSRandall Stewart 		}
6077f8829a4aSRandall Stewart 		/*
6078f8829a4aSRandall Stewart 		 * update off the real current cum-ack, if we have an stcb.
6079f8829a4aSRandall Stewart 		 */
60800696e120SRandall Stewart 		if ((control->do_not_ref_stcb == 0) && stcb)
6081f8829a4aSRandall Stewart 			sinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
6082f8829a4aSRandall Stewart 		/*
6083f8829a4aSRandall Stewart 		 * mask off the high bits, we keep the actual chunk bits in
6084f8829a4aSRandall Stewart 		 * there.
6085f8829a4aSRandall Stewart 		 */
6086f8829a4aSRandall Stewart 		sinfo->sinfo_flags &= 0x00ff;
60875f26a41dSRandall Stewart 		if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) {
60885f26a41dSRandall Stewart 			sinfo->sinfo_flags |= SCTP_UNORDERED;
60895f26a41dSRandall Stewart 		}
6090f8829a4aSRandall Stewart 	}
609118e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
609218e198d3SRandall Stewart 	{
609318e198d3SRandall Stewart 		int index, newindex;
609418e198d3SRandall Stewart 		struct sctp_pcbtsn_rlog *entry;
609518e198d3SRandall Stewart 
609618e198d3SRandall Stewart 		do {
609718e198d3SRandall Stewart 			index = inp->readlog_index;
609818e198d3SRandall Stewart 			newindex = index + 1;
609918e198d3SRandall Stewart 			if (newindex >= SCTP_READ_LOG_SIZE) {
610018e198d3SRandall Stewart 				newindex = 0;
610118e198d3SRandall Stewart 			}
610218e198d3SRandall Stewart 		} while (atomic_cmpset_int(&inp->readlog_index, index, newindex) == 0);
610318e198d3SRandall Stewart 		entry = &inp->readlog[index];
610418e198d3SRandall Stewart 		entry->vtag = control->sinfo_assoc_id;
610518e198d3SRandall Stewart 		entry->strm = control->sinfo_stream;
610649656eefSMichael Tuexen 		entry->seq = (uint16_t)control->mid;
610718e198d3SRandall Stewart 		entry->sz = control->length;
610818e198d3SRandall Stewart 		entry->flgs = control->sinfo_flags;
610918e198d3SRandall Stewart 	}
611018e198d3SRandall Stewart #endif
6111d59107f7SMichael Tuexen 	if ((fromlen > 0) && (from != NULL)) {
6112d59107f7SMichael Tuexen 		union sctp_sockstore store;
6113d59107f7SMichael Tuexen 		size_t len;
6114d59107f7SMichael Tuexen 
6115b5b6e5c2SMichael Tuexen 		switch (control->whoFrom->ro._l_addr.sa.sa_family) {
6116b5b6e5c2SMichael Tuexen #ifdef INET6
6117b5b6e5c2SMichael Tuexen 		case AF_INET6:
6118d59107f7SMichael Tuexen 			len = sizeof(struct sockaddr_in6);
6119d59107f7SMichael Tuexen 			store.sin6 = control->whoFrom->ro._l_addr.sin6;
6120d59107f7SMichael Tuexen 			store.sin6.sin6_port = control->port_from;
6121b5b6e5c2SMichael Tuexen 			break;
6122f8829a4aSRandall Stewart #endif
6123b5b6e5c2SMichael Tuexen #ifdef INET
6124b5b6e5c2SMichael Tuexen 		case AF_INET:
6125d59107f7SMichael Tuexen #ifdef INET6
6126d59107f7SMichael Tuexen 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
6127d59107f7SMichael Tuexen 				len = sizeof(struct sockaddr_in6);
6128d59107f7SMichael Tuexen 				in6_sin_2_v4mapsin6(&control->whoFrom->ro._l_addr.sin,
6129d59107f7SMichael Tuexen 				    &store.sin6);
6130d59107f7SMichael Tuexen 				store.sin6.sin6_port = control->port_from;
6131d59107f7SMichael Tuexen 			} else {
6132d59107f7SMichael Tuexen 				len = sizeof(struct sockaddr_in);
6133d59107f7SMichael Tuexen 				store.sin = control->whoFrom->ro._l_addr.sin;
6134d59107f7SMichael Tuexen 				store.sin.sin_port = control->port_from;
6135d59107f7SMichael Tuexen 			}
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 #endif
6141b5b6e5c2SMichael Tuexen 			break;
6142b5b6e5c2SMichael Tuexen #endif
6143b5b6e5c2SMichael Tuexen 		default:
6144d59107f7SMichael Tuexen 			len = 0;
6145b5b6e5c2SMichael Tuexen 			break;
6146b5b6e5c2SMichael Tuexen 		}
6147d59107f7SMichael Tuexen 		memcpy(from, &store, min((size_t)fromlen, len));
6148e0e00a4dSMichael Tuexen #ifdef INET6
6149f8829a4aSRandall Stewart 		{
6150b5b6e5c2SMichael Tuexen 			struct sockaddr_in6 lsa6, *from6;
6151f8829a4aSRandall Stewart 
6152b5b6e5c2SMichael Tuexen 			from6 = (struct sockaddr_in6 *)from;
6153b5b6e5c2SMichael Tuexen 			sctp_recover_scope_mac(from6, (&lsa6));
6154f8829a4aSRandall Stewart 		}
6155f8829a4aSRandall Stewart #endif
6156f8829a4aSRandall Stewart 	}
61579c5ca6f2SMichael Tuexen 	if (hold_rlock) {
61589c5ca6f2SMichael Tuexen 		SCTP_INP_READ_UNLOCK(inp);
61599c5ca6f2SMichael Tuexen 		hold_rlock = 0;
61609c5ca6f2SMichael Tuexen 	}
61619c5ca6f2SMichael Tuexen 	if (hold_sblock) {
61629c5ca6f2SMichael Tuexen 		SOCKBUF_UNLOCK(&so->so_rcv);
61639c5ca6f2SMichael Tuexen 		hold_sblock = 0;
61649c5ca6f2SMichael Tuexen 	}
6165f8829a4aSRandall Stewart 	/* now copy out what data we can */
6166f8829a4aSRandall Stewart 	if (mp == NULL) {
6167f8829a4aSRandall Stewart 		/* copy out each mbuf in the chain up to length */
6168f8829a4aSRandall Stewart get_more_data:
6169f8829a4aSRandall Stewart 		m = control->data;
6170f8829a4aSRandall Stewart 		while (m) {
6171f8829a4aSRandall Stewart 			/* Move out all we can */
61720d3cf13dSMichael Tuexen 			cp_len = uio->uio_resid;
61730d3cf13dSMichael Tuexen 			my_len = SCTP_BUF_LEN(m);
6174f8829a4aSRandall Stewart 			if (cp_len > my_len) {
6175f8829a4aSRandall Stewart 				/* not enough in this buf */
6176f8829a4aSRandall Stewart 				cp_len = my_len;
6177f8829a4aSRandall Stewart 			}
6178f8829a4aSRandall Stewart 			if (hold_rlock) {
6179f8829a4aSRandall Stewart 				SCTP_INP_READ_UNLOCK(inp);
6180f8829a4aSRandall Stewart 				hold_rlock = 0;
6181f8829a4aSRandall Stewart 			}
6182f8829a4aSRandall Stewart 			if (cp_len > 0)
618358e6eeefSMichael Tuexen 				error = uiomove(mtod(m, char *), (int)cp_len, uio);
6184f8829a4aSRandall Stewart 			/* re-read */
6185f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
6186f8829a4aSRandall Stewart 				goto release;
6187f8829a4aSRandall Stewart 			}
61880053ed28SMichael Tuexen 
61890696e120SRandall Stewart 			if ((control->do_not_ref_stcb == 0) && stcb &&
6190f8829a4aSRandall Stewart 			    stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
6191f8829a4aSRandall Stewart 				no_rcv_needed = 1;
6192f8829a4aSRandall Stewart 			}
6193f8829a4aSRandall Stewart 			if (error) {
6194f8829a4aSRandall Stewart 				/* error we are out of here */
6195f8829a4aSRandall Stewart 				goto release;
6196f8829a4aSRandall Stewart 			}
6197f8829a4aSRandall Stewart 			SCTP_INP_READ_LOCK(inp);
6198f8829a4aSRandall Stewart 			hold_rlock = 1;
6199139bc87fSRandall Stewart 			if (cp_len == SCTP_BUF_LEN(m)) {
6200139bc87fSRandall Stewart 				if ((SCTP_BUF_NEXT(m) == NULL) &&
6201139bc87fSRandall Stewart 				    (control->end_added)) {
6202f8829a4aSRandall Stewart 					out_flags |= MSG_EOR;
620352129fcdSRandall Stewart 					if ((control->do_not_ref_stcb == 0) &&
620452129fcdSRandall Stewart 					    (control->stcb != NULL) &&
620552129fcdSRandall Stewart 					    ((control->spec_flags & M_NOTIFICATION) == 0))
6206ee7f9857SRandall Stewart 						control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6207f8829a4aSRandall Stewart 				}
6208139bc87fSRandall Stewart 				if (control->spec_flags & M_NOTIFICATION) {
6209f8829a4aSRandall Stewart 					out_flags |= MSG_NOTIFICATION;
6210f8829a4aSRandall Stewart 				}
6211f8829a4aSRandall Stewart 				/* we ate up the mbuf */
6212f8829a4aSRandall Stewart 				if (in_flags & MSG_PEEK) {
6213f8829a4aSRandall Stewart 					/* just looking */
6214139bc87fSRandall Stewart 					m = SCTP_BUF_NEXT(m);
6215f8829a4aSRandall Stewart 					copied_so_far += cp_len;
6216f8829a4aSRandall Stewart 				} else {
6217f8829a4aSRandall Stewart 					/* dispose of the mbuf */
6218b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6219f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv,
6220139bc87fSRandall Stewart 						    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
622180fefe0aSRandall Stewart 					}
6222f8829a4aSRandall Stewart 					sctp_sbfree(control, stcb, &so->so_rcv, m);
6223b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6224f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv,
6225f8829a4aSRandall Stewart 						    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
622680fefe0aSRandall Stewart 					}
6227f8829a4aSRandall Stewart 					copied_so_far += cp_len;
622858e6eeefSMichael Tuexen 					freed_so_far += (uint32_t)cp_len;
6229c4739e2fSRandall Stewart 					freed_so_far += MSIZE;
623018e198d3SRandall Stewart 					atomic_subtract_int(&control->length, cp_len);
6231f8829a4aSRandall Stewart 					control->data = sctp_m_free(m);
6232f8829a4aSRandall Stewart 					m = control->data;
6233b7b84c0eSMichael Tuexen 					/*
6234b7b84c0eSMichael Tuexen 					 * been through it all, must hold sb
6235b7b84c0eSMichael Tuexen 					 * lock ok to null tail
6236b7b84c0eSMichael Tuexen 					 */
6237f8829a4aSRandall Stewart 					if (control->data == NULL) {
6238a5d547adSRandall Stewart #ifdef INVARIANTS
6239f8829a4aSRandall Stewart 						if ((control->end_added == 0) ||
6240f8829a4aSRandall Stewart 						    (TAILQ_NEXT(control, next) == NULL)) {
6241f8829a4aSRandall Stewart 							/*
6242f8829a4aSRandall Stewart 							 * If the end is not
6243f8829a4aSRandall Stewart 							 * added, OR the
6244f8829a4aSRandall Stewart 							 * next is NOT null
6245f8829a4aSRandall Stewart 							 * we MUST have the
6246f8829a4aSRandall Stewart 							 * lock.
6247f8829a4aSRandall Stewart 							 */
6248f8829a4aSRandall Stewart 							if (mtx_owned(&inp->inp_rdata_mtx) == 0) {
6249f8829a4aSRandall Stewart 								panic("Hmm we don't own the lock?");
6250f8829a4aSRandall Stewart 							}
6251f8829a4aSRandall Stewart 						}
6252f8829a4aSRandall Stewart #endif
6253f8829a4aSRandall Stewart 						control->tail_mbuf = NULL;
6254a5d547adSRandall Stewart #ifdef INVARIANTS
6255f8829a4aSRandall Stewart 						if ((control->end_added) && ((out_flags & MSG_EOR) == 0)) {
6256f8829a4aSRandall Stewart 							panic("end_added, nothing left and no MSG_EOR");
6257f8829a4aSRandall Stewart 						}
6258f8829a4aSRandall Stewart #endif
6259f8829a4aSRandall Stewart 					}
6260f8829a4aSRandall Stewart 				}
6261f8829a4aSRandall Stewart 			} else {
6262f8829a4aSRandall Stewart 				/* Do we need to trim the mbuf? */
6263139bc87fSRandall Stewart 				if (control->spec_flags & M_NOTIFICATION) {
6264f8829a4aSRandall Stewart 					out_flags |= MSG_NOTIFICATION;
6265f8829a4aSRandall Stewart 				}
6266f8829a4aSRandall Stewart 				if ((in_flags & MSG_PEEK) == 0) {
6267139bc87fSRandall Stewart 					SCTP_BUF_RESV_UF(m, cp_len);
626858e6eeefSMichael Tuexen 					SCTP_BUF_LEN(m) -= (int)cp_len;
6269b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
627058e6eeefSMichael Tuexen 						sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, (int)cp_len);
627180fefe0aSRandall Stewart 					}
62724e88d37aSMichael Tuexen 					atomic_subtract_int(&so->so_rcv.sb_cc, cp_len);
62730696e120SRandall Stewart 					if ((control->do_not_ref_stcb == 0) &&
62740696e120SRandall Stewart 					    stcb) {
62754e88d37aSMichael Tuexen 						atomic_subtract_int(&stcb->asoc.sb_cc, cp_len);
6276f8829a4aSRandall Stewart 					}
6277f8829a4aSRandall Stewart 					copied_so_far += cp_len;
627858e6eeefSMichael Tuexen 					freed_so_far += (uint32_t)cp_len;
6279c4739e2fSRandall Stewart 					freed_so_far += MSIZE;
6280b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6281f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb,
6282f8829a4aSRandall Stewart 						    SCTP_LOG_SBRESULT, 0);
628380fefe0aSRandall Stewart 					}
628418e198d3SRandall Stewart 					atomic_subtract_int(&control->length, cp_len);
6285f8829a4aSRandall Stewart 				} else {
6286f8829a4aSRandall Stewart 					copied_so_far += cp_len;
6287f8829a4aSRandall Stewart 				}
6288f8829a4aSRandall Stewart 			}
6289d61a0ae0SRandall Stewart 			if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) {
6290f8829a4aSRandall Stewart 				break;
6291f8829a4aSRandall Stewart 			}
6292f8829a4aSRandall Stewart 			if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
6293f8829a4aSRandall Stewart 			    (control->do_not_ref_stcb == 0) &&
6294f8829a4aSRandall Stewart 			    (freed_so_far >= rwnd_req)) {
6295f8829a4aSRandall Stewart 				sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6296f8829a4aSRandall Stewart 			}
6297f8829a4aSRandall Stewart 		}		/* end while(m) */
6298f8829a4aSRandall Stewart 		/*
6299f8829a4aSRandall Stewart 		 * At this point we have looked at it all and we either have
6300f8829a4aSRandall Stewart 		 * a MSG_EOR/or read all the user wants... <OR>
6301f8829a4aSRandall Stewart 		 * control->length == 0.
6302f8829a4aSRandall Stewart 		 */
6303d61a0ae0SRandall Stewart 		if ((out_flags & MSG_EOR) && ((in_flags & MSG_PEEK) == 0)) {
6304f8829a4aSRandall Stewart 			/* we are done with this control */
6305f8829a4aSRandall Stewart 			if (control->length == 0) {
6306f8829a4aSRandall Stewart 				if (control->data) {
6307a5d547adSRandall Stewart #ifdef INVARIANTS
6308f8829a4aSRandall Stewart 					panic("control->data not null at read eor?");
6309f8829a4aSRandall Stewart #else
6310ad81507eSRandall Stewart 					SCTP_PRINTF("Strange, data left in the control buffer .. invarients would panic?\n");
6311f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
6312f8829a4aSRandall Stewart 					control->data = NULL;
6313f8829a4aSRandall Stewart #endif
6314f8829a4aSRandall Stewart 				}
6315f8829a4aSRandall Stewart 		done_with_control:
6316f8829a4aSRandall Stewart 				if (hold_rlock == 0) {
6317f8829a4aSRandall Stewart 					SCTP_INP_READ_LOCK(inp);
6318f8829a4aSRandall Stewart 					hold_rlock = 1;
6319f8829a4aSRandall Stewart 				}
6320f8829a4aSRandall Stewart 				TAILQ_REMOVE(&inp->read_queue, control, next);
6321f8829a4aSRandall Stewart 				/* Add back any hiddend data */
6322f8829a4aSRandall Stewart 				if (control->held_length) {
6323f8829a4aSRandall Stewart 					held_length = 0;
6324f8829a4aSRandall Stewart 					control->held_length = 0;
6325f8829a4aSRandall Stewart 					wakeup_read_socket = 1;
6326f8829a4aSRandall Stewart 				}
632717205eccSRandall Stewart 				if (control->aux_data) {
632817205eccSRandall Stewart 					sctp_m_free(control->aux_data);
632917205eccSRandall Stewart 					control->aux_data = NULL;
633017205eccSRandall Stewart 				}
6331f8829a4aSRandall Stewart 				no_rcv_needed = control->do_not_ref_stcb;
6332f8829a4aSRandall Stewart 				sctp_free_remote_addr(control->whoFrom);
6333f8829a4aSRandall Stewart 				control->data = NULL;
633498d5fd97SMichael Tuexen #ifdef INVARIANTS
633544249214SRandall Stewart 				if (control->on_strm_q) {
633644249214SRandall Stewart 					panic("About to free ctl:%p so:%p and its in %d",
633744249214SRandall Stewart 					    control, so, control->on_strm_q);
633844249214SRandall Stewart 				}
633998d5fd97SMichael Tuexen #endif
6340f8829a4aSRandall Stewart 				sctp_free_a_readq(stcb, control);
6341f8829a4aSRandall Stewart 				control = NULL;
63420696e120SRandall Stewart 				if ((freed_so_far >= rwnd_req) &&
63430696e120SRandall Stewart 				    (no_rcv_needed == 0))
6344f8829a4aSRandall Stewart 					sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6345f8829a4aSRandall Stewart 
6346f8829a4aSRandall Stewart 			} else {
6347f8829a4aSRandall Stewart 				/*
6348f8829a4aSRandall Stewart 				 * The user did not read all of this
6349f8829a4aSRandall Stewart 				 * message, turn off the returned MSG_EOR
6350f8829a4aSRandall Stewart 				 * since we are leaving more behind on the
6351f8829a4aSRandall Stewart 				 * control to read.
6352f8829a4aSRandall Stewart 				 */
6353a5d547adSRandall Stewart #ifdef INVARIANTS
63540696e120SRandall Stewart 				if (control->end_added &&
63550696e120SRandall Stewart 				    (control->data == NULL) &&
6356f8829a4aSRandall Stewart 				    (control->tail_mbuf == NULL)) {
6357f8829a4aSRandall Stewart 					panic("Gak, control->length is corrupt?");
6358f8829a4aSRandall Stewart 				}
6359f8829a4aSRandall Stewart #endif
6360f8829a4aSRandall Stewart 				no_rcv_needed = control->do_not_ref_stcb;
6361f8829a4aSRandall Stewart 				out_flags &= ~MSG_EOR;
6362f8829a4aSRandall Stewart 			}
6363f8829a4aSRandall Stewart 		}
6364f8829a4aSRandall Stewart 		if (out_flags & MSG_EOR) {
6365f8829a4aSRandall Stewart 			goto release;
6366f8829a4aSRandall Stewart 		}
6367f8829a4aSRandall Stewart 		if ((uio->uio_resid == 0) ||
636804aab884SMichael Tuexen 		    ((in_eeor_mode) &&
636943ecbff2SMichael Tuexen 		    (copied_so_far >= max(so->so_rcv.sb_lowat, 1)))) {
6370f8829a4aSRandall Stewart 			goto release;
6371f8829a4aSRandall Stewart 		}
6372f8829a4aSRandall Stewart 		/*
6373f8829a4aSRandall Stewart 		 * If I hit here the receiver wants more and this message is
6374f8829a4aSRandall Stewart 		 * NOT done (pd-api). So two questions. Can we block? if not
6375f8829a4aSRandall Stewart 		 * we are done. Did the user NOT set MSG_WAITALL?
6376f8829a4aSRandall Stewart 		 */
6377f8829a4aSRandall Stewart 		if (block_allowed == 0) {
6378f8829a4aSRandall Stewart 			goto release;
6379f8829a4aSRandall Stewart 		}
6380f8829a4aSRandall Stewart 		/*
6381f8829a4aSRandall Stewart 		 * We need to wait for more data a few things: - We don't
6382f8829a4aSRandall Stewart 		 * sbunlock() so we don't get someone else reading. - We
6383f8829a4aSRandall Stewart 		 * must be sure to account for the case where what is added
6384f8829a4aSRandall Stewart 		 * is NOT to our control when we wakeup.
6385f8829a4aSRandall Stewart 		 */
6386f8829a4aSRandall Stewart 
6387f8829a4aSRandall Stewart 		/*
6388f8829a4aSRandall Stewart 		 * Do we need to tell the transport a rwnd update might be
6389f8829a4aSRandall Stewart 		 * needed before we go to sleep?
6390f8829a4aSRandall Stewart 		 */
6391f8829a4aSRandall Stewart 		if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
6392f8829a4aSRandall Stewart 		    ((freed_so_far >= rwnd_req) &&
6393f8829a4aSRandall Stewart 		    (control->do_not_ref_stcb == 0) &&
6394f8829a4aSRandall Stewart 		    (no_rcv_needed == 0))) {
6395f8829a4aSRandall Stewart 			sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6396f8829a4aSRandall Stewart 		}
6397f8829a4aSRandall Stewart wait_some_more:
639844b7479bSRandall Stewart 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
6399f8829a4aSRandall Stewart 			goto release;
6400f8829a4aSRandall Stewart 		}
64010053ed28SMichael Tuexen 
6402f8829a4aSRandall Stewart 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)
6403f8829a4aSRandall Stewart 			goto release;
6404f8829a4aSRandall Stewart 
6405f8829a4aSRandall Stewart 		if (hold_rlock == 1) {
6406f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
6407f8829a4aSRandall Stewart 			hold_rlock = 0;
6408f8829a4aSRandall Stewart 		}
6409f8829a4aSRandall Stewart 		if (hold_sblock == 0) {
6410f8829a4aSRandall Stewart 			SOCKBUF_LOCK(&so->so_rcv);
6411f8829a4aSRandall Stewart 			hold_sblock = 1;
6412f8829a4aSRandall Stewart 		}
6413851b7298SRandall Stewart 		if ((copied_so_far) && (control->length == 0) &&
6414b5c16493SMichael Tuexen 		    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE))) {
6415851b7298SRandall Stewart 			goto release;
6416851b7298SRandall Stewart 		}
64174e88d37aSMichael Tuexen 		if (so->so_rcv.sb_cc <= control->held_length) {
6418f8829a4aSRandall Stewart 			error = sbwait(&so->so_rcv);
6419f8829a4aSRandall Stewart 			if (error) {
6420f8829a4aSRandall Stewart 				goto release;
6421f8829a4aSRandall Stewart 			}
6422f8829a4aSRandall Stewart 			control->held_length = 0;
6423f8829a4aSRandall Stewart 		}
6424f8829a4aSRandall Stewart 		if (hold_sblock) {
6425f8829a4aSRandall Stewart 			SOCKBUF_UNLOCK(&so->so_rcv);
6426f8829a4aSRandall Stewart 			hold_sblock = 0;
6427f8829a4aSRandall Stewart 		}
6428f8829a4aSRandall Stewart 		if (control->length == 0) {
6429f8829a4aSRandall Stewart 			/* still nothing here */
6430f8829a4aSRandall Stewart 			if (control->end_added == 1) {
6431f8829a4aSRandall Stewart 				/* he aborted, or is done i.e.did a shutdown */
6432f8829a4aSRandall Stewart 				out_flags |= MSG_EOR;
64339a6142d8SRandall Stewart 				if (control->pdapi_aborted) {
64346114cd96SRandall Stewart 					if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
6435ee7f9857SRandall Stewart 						control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
64369a6142d8SRandall Stewart 
643703b0b021SRandall Stewart 					out_flags |= MSG_TRUNC;
64389a6142d8SRandall Stewart 				} else {
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 				}
6442f8829a4aSRandall Stewart 				goto done_with_control;
6443f8829a4aSRandall Stewart 			}
64444e88d37aSMichael Tuexen 			if (so->so_rcv.sb_cc > held_length) {
64454e88d37aSMichael Tuexen 				control->held_length = so->so_rcv.sb_cc;
6446f8829a4aSRandall Stewart 				held_length = 0;
6447f8829a4aSRandall Stewart 			}
6448f8829a4aSRandall Stewart 			goto wait_some_more;
6449f8829a4aSRandall Stewart 		} else if (control->data == NULL) {
645050cec919SRandall Stewart 			/*
645150cec919SRandall Stewart 			 * we must re-sync since data is probably being
645250cec919SRandall Stewart 			 * added
645350cec919SRandall Stewart 			 */
645450cec919SRandall Stewart 			SCTP_INP_READ_LOCK(inp);
645550cec919SRandall Stewart 			if ((control->length > 0) && (control->data == NULL)) {
6456b7b84c0eSMichael Tuexen 				/*
6457b7b84c0eSMichael Tuexen 				 * big trouble.. we have the lock and its
6458b7b84c0eSMichael Tuexen 				 * corrupt?
6459b7b84c0eSMichael Tuexen 				 */
64609c04b296SRandall Stewart #ifdef INVARIANTS
64619d18771fSRandall Stewart 				panic("Impossible data==NULL length !=0");
64629c04b296SRandall Stewart #endif
64639c04b296SRandall Stewart 				out_flags |= MSG_EOR;
64649c04b296SRandall Stewart 				out_flags |= MSG_TRUNC;
64659c04b296SRandall Stewart 				control->length = 0;
64669c04b296SRandall Stewart 				SCTP_INP_READ_UNLOCK(inp);
64679c04b296SRandall Stewart 				goto done_with_control;
6468f8829a4aSRandall Stewart 			}
646950cec919SRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
647050cec919SRandall Stewart 			/* We will fall around to get more data */
647150cec919SRandall Stewart 		}
6472f8829a4aSRandall Stewart 		goto get_more_data;
6473f8829a4aSRandall Stewart 	} else {
647417205eccSRandall Stewart 		/*-
647517205eccSRandall Stewart 		 * Give caller back the mbuf chain,
647617205eccSRandall Stewart 		 * store in uio_resid the length
6477f8829a4aSRandall Stewart 		 */
647817205eccSRandall Stewart 		wakeup_read_socket = 0;
6479f8829a4aSRandall Stewart 		if ((control->end_added == 0) ||
6480f8829a4aSRandall Stewart 		    (TAILQ_NEXT(control, next) == NULL)) {
6481f8829a4aSRandall Stewart 			/* Need to get rlock */
6482f8829a4aSRandall Stewart 			if (hold_rlock == 0) {
6483f8829a4aSRandall Stewart 				SCTP_INP_READ_LOCK(inp);
6484f8829a4aSRandall Stewart 				hold_rlock = 1;
6485f8829a4aSRandall Stewart 			}
6486f8829a4aSRandall Stewart 		}
6487139bc87fSRandall Stewart 		if (control->end_added) {
6488f8829a4aSRandall Stewart 			out_flags |= MSG_EOR;
648960990c0cSMichael Tuexen 			if ((control->do_not_ref_stcb == 0) &&
649060990c0cSMichael Tuexen 			    (control->stcb != NULL) &&
649160990c0cSMichael Tuexen 			    ((control->spec_flags & M_NOTIFICATION) == 0))
6492ee7f9857SRandall Stewart 				control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6493f8829a4aSRandall Stewart 		}
6494139bc87fSRandall Stewart 		if (control->spec_flags & M_NOTIFICATION) {
6495f8829a4aSRandall Stewart 			out_flags |= MSG_NOTIFICATION;
6496f8829a4aSRandall Stewart 		}
649717205eccSRandall Stewart 		uio->uio_resid = control->length;
6498f8829a4aSRandall Stewart 		*mp = control->data;
6499f8829a4aSRandall Stewart 		m = control->data;
6500f8829a4aSRandall Stewart 		while (m) {
6501b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6502f8829a4aSRandall Stewart 				sctp_sblog(&so->so_rcv,
6503139bc87fSRandall Stewart 				    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
650480fefe0aSRandall Stewart 			}
6505f8829a4aSRandall Stewart 			sctp_sbfree(control, stcb, &so->so_rcv, m);
650658e6eeefSMichael Tuexen 			freed_so_far += (uint32_t)SCTP_BUF_LEN(m);
6507c4739e2fSRandall Stewart 			freed_so_far += MSIZE;
6508b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6509f8829a4aSRandall Stewart 				sctp_sblog(&so->so_rcv,
6510f8829a4aSRandall Stewart 				    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
651180fefe0aSRandall Stewart 			}
6512139bc87fSRandall Stewart 			m = SCTP_BUF_NEXT(m);
6513f8829a4aSRandall Stewart 		}
6514f8829a4aSRandall Stewart 		control->data = control->tail_mbuf = NULL;
6515f8829a4aSRandall Stewart 		control->length = 0;
6516f8829a4aSRandall Stewart 		if (out_flags & MSG_EOR) {
6517f8829a4aSRandall Stewart 			/* Done with this control */
6518f8829a4aSRandall Stewart 			goto done_with_control;
6519f8829a4aSRandall Stewart 		}
6520f8829a4aSRandall Stewart 	}
6521f8829a4aSRandall Stewart release:
6522f8829a4aSRandall Stewart 	if (hold_rlock == 1) {
6523f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
6524f8829a4aSRandall Stewart 		hold_rlock = 0;
6525f8829a4aSRandall Stewart 	}
65267abab911SRobert Watson 	if (hold_sblock == 1) {
65277abab911SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
65287abab911SRobert Watson 		hold_sblock = 0;
6529f8829a4aSRandall Stewart 	}
65300053ed28SMichael Tuexen 
6531f8829a4aSRandall Stewart 	sbunlock(&so->so_rcv);
65327abab911SRobert Watson 	sockbuf_lock = 0;
6533f8829a4aSRandall Stewart 
6534f8829a4aSRandall Stewart release_unlocked:
6535f8829a4aSRandall Stewart 	if (hold_sblock) {
6536f8829a4aSRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
6537f8829a4aSRandall Stewart 		hold_sblock = 0;
6538f8829a4aSRandall Stewart 	}
6539f8829a4aSRandall Stewart 	if ((stcb) && (in_flags & MSG_PEEK) == 0) {
6540f8829a4aSRandall Stewart 		if ((freed_so_far >= rwnd_req) &&
6541f8829a4aSRandall Stewart 		    (control && (control->do_not_ref_stcb == 0)) &&
6542f8829a4aSRandall Stewart 		    (no_rcv_needed == 0))
6543f8829a4aSRandall Stewart 			sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6544f8829a4aSRandall Stewart 	}
6545f8829a4aSRandall Stewart out:
65461b9f62a0SRandall Stewart 	if (msg_flags) {
65471b9f62a0SRandall Stewart 		*msg_flags = out_flags;
65481b9f62a0SRandall Stewart 	}
65499a6142d8SRandall Stewart 	if (((out_flags & MSG_EOR) == 0) &&
65509a6142d8SRandall Stewart 	    ((in_flags & MSG_PEEK) == 0) &&
65519a6142d8SRandall Stewart 	    (sinfo) &&
6552e2e7c62eSMichael Tuexen 	    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
6553e2e7c62eSMichael Tuexen 	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO))) {
65549a6142d8SRandall Stewart 		struct sctp_extrcvinfo *s_extra;
65559a6142d8SRandall Stewart 
65569a6142d8SRandall Stewart 		s_extra = (struct sctp_extrcvinfo *)sinfo;
6557b70b526dSMichael Tuexen 		s_extra->serinfo_next_flags = SCTP_NO_NEXT_MSG;
65589a6142d8SRandall Stewart 	}
6559f8829a4aSRandall Stewart 	if (hold_rlock == 1) {
6560f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
6561f8829a4aSRandall Stewart 	}
6562f8829a4aSRandall Stewart 	if (hold_sblock) {
6563f8829a4aSRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
6564f8829a4aSRandall Stewart 	}
65657abab911SRobert Watson 	if (sockbuf_lock) {
65667abab911SRobert Watson 		sbunlock(&so->so_rcv);
65677abab911SRobert Watson 	}
65680053ed28SMichael Tuexen 
656950cec919SRandall Stewart 	if (freecnt_applied) {
6570f8829a4aSRandall Stewart 		/*
6571f8829a4aSRandall Stewart 		 * The lock on the socket buffer protects us so the free
6572f8829a4aSRandall Stewart 		 * code will stop. But since we used the socketbuf lock and
6573f8829a4aSRandall Stewart 		 * the sender uses the tcb_lock to increment, we need to use
6574f8829a4aSRandall Stewart 		 * the atomic add to the refcnt.
6575f8829a4aSRandall Stewart 		 */
657650cec919SRandall Stewart 		if (stcb == NULL) {
6577df6e0cc3SRandall Stewart #ifdef INVARIANTS
657850cec919SRandall Stewart 			panic("stcb for refcnt has gone NULL?");
6579df6e0cc3SRandall Stewart 			goto stage_left;
6580df6e0cc3SRandall Stewart #else
6581df6e0cc3SRandall Stewart 			goto stage_left;
6582df6e0cc3SRandall Stewart #endif
658350cec919SRandall Stewart 		}
6584f8829a4aSRandall Stewart 		/* Save the value back for next time */
6585f8829a4aSRandall Stewart 		stcb->freed_by_sorcv_sincelast = freed_so_far;
6586cf46caceSMichael Tuexen 		atomic_add_int(&stcb->asoc.refcnt, -1);
6587f8829a4aSRandall Stewart 	}
6588b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
6589f8829a4aSRandall Stewart 		if (stcb) {
6590f8829a4aSRandall Stewart 			sctp_misc_ints(SCTP_SORECV_DONE,
6591f8829a4aSRandall Stewart 			    freed_so_far,
65929a8e3088SMichael Tuexen 			    (uint32_t)((uio) ? (slen - uio->uio_resid) : slen),
6593f8829a4aSRandall Stewart 			    stcb->asoc.my_rwnd,
65944e88d37aSMichael Tuexen 			    so->so_rcv.sb_cc);
6595f8829a4aSRandall Stewart 		} else {
6596f8829a4aSRandall Stewart 			sctp_misc_ints(SCTP_SORECV_DONE,
6597f8829a4aSRandall Stewart 			    freed_so_far,
65989a8e3088SMichael Tuexen 			    (uint32_t)((uio) ? (slen - uio->uio_resid) : slen),
6599f8829a4aSRandall Stewart 			    0,
66004e88d37aSMichael Tuexen 			    so->so_rcv.sb_cc);
6601f8829a4aSRandall Stewart 		}
660280fefe0aSRandall Stewart 	}
6603df6e0cc3SRandall Stewart stage_left:
6604f8829a4aSRandall Stewart 	if (wakeup_read_socket) {
6605f8829a4aSRandall Stewart 		sctp_sorwakeup(inp, so);
6606f8829a4aSRandall Stewart 	}
6607f8829a4aSRandall Stewart 	return (error);
6608f8829a4aSRandall Stewart }
6609f8829a4aSRandall Stewart 
6610f8829a4aSRandall Stewart 
6611f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
6612f8829a4aSRandall Stewart struct mbuf *
6613f8829a4aSRandall Stewart sctp_m_free(struct mbuf *m)
6614f8829a4aSRandall Stewart {
6615b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6616f8829a4aSRandall Stewart 		sctp_log_mb(m, SCTP_MBUF_IFREE);
6617f8829a4aSRandall Stewart 	}
6618f8829a4aSRandall Stewart 	return (m_free(m));
6619f8829a4aSRandall Stewart }
6620f8829a4aSRandall Stewart 
6621f8829a4aSRandall Stewart void
6622f8829a4aSRandall Stewart sctp_m_freem(struct mbuf *mb)
6623f8829a4aSRandall Stewart {
6624f8829a4aSRandall Stewart 	while (mb != NULL)
6625f8829a4aSRandall Stewart 		mb = sctp_m_free(mb);
6626f8829a4aSRandall Stewart }
6627f8829a4aSRandall Stewart 
6628f8829a4aSRandall Stewart #endif
6629f8829a4aSRandall Stewart 
663042551e99SRandall Stewart int
663142551e99SRandall Stewart sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id)
663242551e99SRandall Stewart {
663342551e99SRandall Stewart 	/*
663442551e99SRandall Stewart 	 * Given a local address. For all associations that holds the
663542551e99SRandall Stewart 	 * address, request a peer-set-primary.
663642551e99SRandall Stewart 	 */
663742551e99SRandall Stewart 	struct sctp_ifa *ifa;
663842551e99SRandall Stewart 	struct sctp_laddr *wi;
663942551e99SRandall Stewart 
664042551e99SRandall Stewart 	ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0);
664142551e99SRandall Stewart 	if (ifa == NULL) {
6642c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EADDRNOTAVAIL);
664342551e99SRandall Stewart 		return (EADDRNOTAVAIL);
664442551e99SRandall Stewart 	}
664542551e99SRandall Stewart 	/*
664642551e99SRandall Stewart 	 * Now that we have the ifa we must awaken the iterator with this
664742551e99SRandall Stewart 	 * message.
664842551e99SRandall Stewart 	 */
6649b3f1ea41SRandall Stewart 	wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
665042551e99SRandall Stewart 	if (wi == NULL) {
6651c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
665242551e99SRandall Stewart 		return (ENOMEM);
665342551e99SRandall Stewart 	}
665442551e99SRandall Stewart 	/* Now incr the count and int wi structure */
665542551e99SRandall Stewart 	SCTP_INCR_LADDR_COUNT();
66565ba7f91fSMichael Tuexen 	memset(wi, 0, sizeof(*wi));
6657d61a0ae0SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
665842551e99SRandall Stewart 	wi->ifa = ifa;
665942551e99SRandall Stewart 	wi->action = SCTP_SET_PRIM_ADDR;
666042551e99SRandall Stewart 	atomic_add_int(&ifa->refcount, 1);
666142551e99SRandall Stewart 
666242551e99SRandall Stewart 	/* Now add it to the work queue */
6663f7517433SRandall Stewart 	SCTP_WQ_ADDR_LOCK();
666442551e99SRandall Stewart 	/*
666542551e99SRandall Stewart 	 * Should this really be a tailq? As it is we will process the
666642551e99SRandall Stewart 	 * newest first :-0
666742551e99SRandall Stewart 	 */
6668b3f1ea41SRandall Stewart 	LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
666942551e99SRandall Stewart 	sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
667042551e99SRandall Stewart 	    (struct sctp_inpcb *)NULL,
667142551e99SRandall Stewart 	    (struct sctp_tcb *)NULL,
667242551e99SRandall Stewart 	    (struct sctp_nets *)NULL);
66732c62ba73SMichael Tuexen 	SCTP_WQ_ADDR_UNLOCK();
667442551e99SRandall Stewart 	return (0);
667542551e99SRandall Stewart }
667642551e99SRandall Stewart 
667742551e99SRandall Stewart 
6678f8829a4aSRandall Stewart int
667917205eccSRandall Stewart sctp_soreceive(struct socket *so,
668017205eccSRandall Stewart     struct sockaddr **psa,
668117205eccSRandall Stewart     struct uio *uio,
668217205eccSRandall Stewart     struct mbuf **mp0,
668317205eccSRandall Stewart     struct mbuf **controlp,
668417205eccSRandall Stewart     int *flagsp)
6685f8829a4aSRandall Stewart {
6686f8829a4aSRandall Stewart 	int error, fromlen;
6687f8829a4aSRandall Stewart 	uint8_t sockbuf[256];
6688f8829a4aSRandall Stewart 	struct sockaddr *from;
6689f8829a4aSRandall Stewart 	struct sctp_extrcvinfo sinfo;
6690f8829a4aSRandall Stewart 	int filling_sinfo = 1;
669146bf534cSMichael Tuexen 	int flags;
6692f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
6693f8829a4aSRandall Stewart 
6694f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
6695f8829a4aSRandall Stewart 	/* pickup the assoc we are reading from */
6696f8829a4aSRandall Stewart 	if (inp == NULL) {
6697c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6698f8829a4aSRandall Stewart 		return (EINVAL);
6699f8829a4aSRandall Stewart 	}
6700e2e7c62eSMichael Tuexen 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) &&
6701e2e7c62eSMichael Tuexen 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
6702e2e7c62eSMichael Tuexen 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) ||
6703f8829a4aSRandall Stewart 	    (controlp == NULL)) {
6704f8829a4aSRandall Stewart 		/* user does not want the sndrcv ctl */
6705f8829a4aSRandall Stewart 		filling_sinfo = 0;
6706f8829a4aSRandall Stewart 	}
6707f8829a4aSRandall Stewart 	if (psa) {
6708f8829a4aSRandall Stewart 		from = (struct sockaddr *)sockbuf;
6709f8829a4aSRandall Stewart 		fromlen = sizeof(sockbuf);
6710f8829a4aSRandall Stewart 		from->sa_len = 0;
6711f8829a4aSRandall Stewart 	} else {
6712f8829a4aSRandall Stewart 		from = NULL;
6713f8829a4aSRandall Stewart 		fromlen = 0;
6714f8829a4aSRandall Stewart 	}
6715f8829a4aSRandall Stewart 
6716e432298aSXin LI 	if (filling_sinfo) {
6717e432298aSXin LI 		memset(&sinfo, 0, sizeof(struct sctp_extrcvinfo));
6718e432298aSXin LI 	}
671946bf534cSMichael Tuexen 	if (flagsp != NULL) {
672046bf534cSMichael Tuexen 		flags = *flagsp;
672146bf534cSMichael Tuexen 	} else {
672246bf534cSMichael Tuexen 		flags = 0;
672346bf534cSMichael Tuexen 	}
672446bf534cSMichael Tuexen 	error = sctp_sorecvmsg(so, uio, mp0, from, fromlen, &flags,
6725f8829a4aSRandall Stewart 	    (struct sctp_sndrcvinfo *)&sinfo, filling_sinfo);
672646bf534cSMichael Tuexen 	if (flagsp != NULL) {
672746bf534cSMichael Tuexen 		*flagsp = flags;
672846bf534cSMichael Tuexen 	}
6729e432298aSXin LI 	if (controlp != NULL) {
6730f8829a4aSRandall Stewart 		/* copy back the sinfo in a CMSG format */
673146bf534cSMichael Tuexen 		if (filling_sinfo && ((flags & MSG_NOTIFICATION) == 0)) {
6732f8829a4aSRandall Stewart 			*controlp = sctp_build_ctl_nchunk(inp,
6733f8829a4aSRandall Stewart 			    (struct sctp_sndrcvinfo *)&sinfo);
673446bf534cSMichael Tuexen 		} else {
6735f8829a4aSRandall Stewart 			*controlp = NULL;
6736f8829a4aSRandall Stewart 		}
673746bf534cSMichael Tuexen 	}
6738f8829a4aSRandall Stewart 	if (psa) {
6739f8829a4aSRandall Stewart 		/* copy back the address info */
6740f8829a4aSRandall Stewart 		if (from && from->sa_len) {
6741f8829a4aSRandall Stewart 			*psa = sodupsockaddr(from, M_NOWAIT);
6742f8829a4aSRandall Stewart 		} else {
6743f8829a4aSRandall Stewart 			*psa = NULL;
6744f8829a4aSRandall Stewart 		}
6745f8829a4aSRandall Stewart 	}
6746f8829a4aSRandall Stewart 	return (error);
6747f8829a4aSRandall Stewart }
674817205eccSRandall Stewart 
674917205eccSRandall Stewart 
675017205eccSRandall Stewart 
675117205eccSRandall Stewart 
675217205eccSRandall Stewart 
675317205eccSRandall Stewart int
6754d61a0ae0SRandall Stewart sctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr,
6755d61a0ae0SRandall Stewart     int totaddr, int *error)
675617205eccSRandall Stewart {
675717205eccSRandall Stewart 	int added = 0;
675817205eccSRandall Stewart 	int i;
675917205eccSRandall Stewart 	struct sctp_inpcb *inp;
676017205eccSRandall Stewart 	struct sockaddr *sa;
676117205eccSRandall Stewart 	size_t incr = 0;
676292776dfdSMichael Tuexen #ifdef INET
676392776dfdSMichael Tuexen 	struct sockaddr_in *sin;
676492776dfdSMichael Tuexen #endif
676592776dfdSMichael Tuexen #ifdef INET6
676692776dfdSMichael Tuexen 	struct sockaddr_in6 *sin6;
676792776dfdSMichael Tuexen #endif
676892776dfdSMichael Tuexen 
676917205eccSRandall Stewart 	sa = addr;
677017205eccSRandall Stewart 	inp = stcb->sctp_ep;
677117205eccSRandall Stewart 	*error = 0;
677217205eccSRandall Stewart 	for (i = 0; i < totaddr; i++) {
6773ea5eba11SMichael Tuexen 		switch (sa->sa_family) {
6774ea5eba11SMichael Tuexen #ifdef INET
6775ea5eba11SMichael Tuexen 		case AF_INET:
677617205eccSRandall Stewart 			incr = sizeof(struct sockaddr_in);
677792776dfdSMichael Tuexen 			sin = (struct sockaddr_in *)sa;
677892776dfdSMichael Tuexen 			if ((sin->sin_addr.s_addr == INADDR_ANY) ||
677992776dfdSMichael Tuexen 			    (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
678092776dfdSMichael Tuexen 			    IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
678192776dfdSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6782ba785902SMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6783ba785902SMichael Tuexen 				    SCTP_FROM_SCTPUTIL + SCTP_LOC_7);
678492776dfdSMichael Tuexen 				*error = EINVAL;
678592776dfdSMichael Tuexen 				goto out_now;
678692776dfdSMichael Tuexen 			}
67877154bf4aSMichael Tuexen 			if (sctp_add_remote_addr(stcb, sa, NULL, stcb->asoc.port,
67887154bf4aSMichael Tuexen 			    SCTP_DONOT_SETSCOPE,
67897154bf4aSMichael Tuexen 			    SCTP_ADDR_IS_CONFIRMED)) {
679017205eccSRandall Stewart 				/* assoc gone no un-lock */
6791c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6792ba785902SMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6793ba785902SMichael Tuexen 				    SCTP_FROM_SCTPUTIL + SCTP_LOC_8);
679417205eccSRandall Stewart 				*error = ENOBUFS;
679517205eccSRandall Stewart 				goto out_now;
679617205eccSRandall Stewart 			}
679717205eccSRandall Stewart 			added++;
6798ea5eba11SMichael Tuexen 			break;
6799ea5eba11SMichael Tuexen #endif
6800ea5eba11SMichael Tuexen #ifdef INET6
6801ea5eba11SMichael Tuexen 		case AF_INET6:
680217205eccSRandall Stewart 			incr = sizeof(struct sockaddr_in6);
680392776dfdSMichael Tuexen 			sin6 = (struct sockaddr_in6 *)sa;
680492776dfdSMichael Tuexen 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
680592776dfdSMichael Tuexen 			    IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
680692776dfdSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6807ba785902SMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6808ba785902SMichael Tuexen 				    SCTP_FROM_SCTPUTIL + SCTP_LOC_9);
680992776dfdSMichael Tuexen 				*error = EINVAL;
681092776dfdSMichael Tuexen 				goto out_now;
681192776dfdSMichael Tuexen 			}
68127154bf4aSMichael Tuexen 			if (sctp_add_remote_addr(stcb, sa, NULL, stcb->asoc.port,
68137154bf4aSMichael Tuexen 			    SCTP_DONOT_SETSCOPE,
68147154bf4aSMichael Tuexen 			    SCTP_ADDR_IS_CONFIRMED)) {
681517205eccSRandall Stewart 				/* assoc gone no un-lock */
6816c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6817ba785902SMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6818ba785902SMichael Tuexen 				    SCTP_FROM_SCTPUTIL + SCTP_LOC_10);
681917205eccSRandall Stewart 				*error = ENOBUFS;
682017205eccSRandall Stewart 				goto out_now;
682117205eccSRandall Stewart 			}
682217205eccSRandall Stewart 			added++;
6823ea5eba11SMichael Tuexen 			break;
6824ea5eba11SMichael Tuexen #endif
6825ea5eba11SMichael Tuexen 		default:
6826ea5eba11SMichael Tuexen 			break;
682717205eccSRandall Stewart 		}
682817205eccSRandall Stewart 		sa = (struct sockaddr *)((caddr_t)sa + incr);
682917205eccSRandall Stewart 	}
683017205eccSRandall Stewart out_now:
683117205eccSRandall Stewart 	return (added);
683217205eccSRandall Stewart }
683317205eccSRandall Stewart 
6834fc26bf71SMichael Tuexen int
6835d61a0ae0SRandall Stewart sctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr,
6836fc26bf71SMichael Tuexen     unsigned int totaddr,
6837fc26bf71SMichael Tuexen     unsigned int *num_v4, unsigned int *num_v6,
6838fc26bf71SMichael Tuexen     unsigned int limit)
683917205eccSRandall Stewart {
684017205eccSRandall Stewart 	struct sockaddr *sa;
6841fc26bf71SMichael Tuexen 	struct sctp_tcb *stcb;
68429a8e3088SMichael Tuexen 	unsigned int incr, at, i;
684317205eccSRandall Stewart 
6844e1949767SMichael Tuexen 	at = 0;
684517205eccSRandall Stewart 	sa = addr;
6846fc26bf71SMichael Tuexen 	*num_v6 = *num_v4 = 0;
684717205eccSRandall Stewart 	/* account and validate addresses */
6848fc26bf71SMichael Tuexen 	if (totaddr == 0) {
6849fc26bf71SMichael Tuexen 		return (EINVAL);
6850fc26bf71SMichael Tuexen 	}
6851fc26bf71SMichael Tuexen 	for (i = 0; i < totaddr; i++) {
6852fc26bf71SMichael Tuexen 		if (at + sizeof(struct sockaddr) > limit) {
6853fc26bf71SMichael Tuexen 			return (EINVAL);
6854fc26bf71SMichael Tuexen 		}
6855ea5eba11SMichael Tuexen 		switch (sa->sa_family) {
6856ea5eba11SMichael Tuexen #ifdef INET
6857ea5eba11SMichael Tuexen 		case AF_INET:
6858e1949767SMichael Tuexen 			incr = (unsigned int)sizeof(struct sockaddr_in);
6859d61a0ae0SRandall Stewart 			if (sa->sa_len != incr) {
6860fc26bf71SMichael Tuexen 				return (EINVAL);
6861d61a0ae0SRandall Stewart 			}
68629a8e3088SMichael Tuexen 			(*num_v4) += 1;
6863ea5eba11SMichael Tuexen 			break;
6864ea5eba11SMichael Tuexen #endif
6865ea5eba11SMichael Tuexen #ifdef INET6
6866ea5eba11SMichael Tuexen 		case AF_INET6:
6867ea5eba11SMichael Tuexen 			{
686817205eccSRandall Stewart 				struct sockaddr_in6 *sin6;
686917205eccSRandall Stewart 
687017205eccSRandall Stewart 				sin6 = (struct sockaddr_in6 *)sa;
687117205eccSRandall Stewart 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
687217205eccSRandall Stewart 					/* Must be non-mapped for connectx */
6873fc26bf71SMichael Tuexen 					return (EINVAL);
687417205eccSRandall Stewart 				}
6875e1949767SMichael Tuexen 				incr = (unsigned int)sizeof(struct sockaddr_in6);
6876d61a0ae0SRandall Stewart 				if (sa->sa_len != incr) {
6877fc26bf71SMichael Tuexen 					return (EINVAL);
6878d61a0ae0SRandall Stewart 				}
68799a8e3088SMichael Tuexen 				(*num_v6) += 1;
6880ea5eba11SMichael Tuexen 				break;
6881ea5eba11SMichael Tuexen 			}
6882ea5eba11SMichael Tuexen #endif
6883ea5eba11SMichael Tuexen 		default:
6884fc26bf71SMichael Tuexen 			return (EINVAL);
688517205eccSRandall Stewart 		}
6886fc26bf71SMichael Tuexen 		if ((at + incr) > limit) {
6887fc26bf71SMichael Tuexen 			return (EINVAL);
6888ea5eba11SMichael Tuexen 		}
6889d61a0ae0SRandall Stewart 		SCTP_INP_INCR_REF(inp);
689017205eccSRandall Stewart 		stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
689117205eccSRandall Stewart 		if (stcb != NULL) {
6892fc26bf71SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
6893fc26bf71SMichael Tuexen 			return (EALREADY);
6894d61a0ae0SRandall Stewart 		} else {
6895d61a0ae0SRandall Stewart 			SCTP_INP_DECR_REF(inp);
689617205eccSRandall Stewart 		}
6897fc26bf71SMichael Tuexen 		at += incr;
689817205eccSRandall Stewart 		sa = (struct sockaddr *)((caddr_t)sa + incr);
689917205eccSRandall Stewart 	}
6900fc26bf71SMichael Tuexen 	return (0);
690117205eccSRandall Stewart }
690235918f85SRandall Stewart 
690335918f85SRandall Stewart /*
690435918f85SRandall Stewart  * sctp_bindx(ADD) for one address.
690535918f85SRandall Stewart  * assumes all arguments are valid/checked by caller.
690635918f85SRandall Stewart  */
690735918f85SRandall Stewart void
690835918f85SRandall Stewart sctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp,
690935918f85SRandall Stewart     struct sockaddr *sa, sctp_assoc_t assoc_id,
691035918f85SRandall Stewart     uint32_t vrf_id, int *error, void *p)
691135918f85SRandall Stewart {
691235918f85SRandall Stewart 	struct sockaddr *addr_touse;
6913d59107f7SMichael Tuexen #if defined(INET) && defined(INET6)
691435918f85SRandall Stewart 	struct sockaddr_in sin;
69155e2c2d87SRandall Stewart #endif
69165e2c2d87SRandall Stewart 
691735918f85SRandall Stewart 	/* see if we're bound all already! */
691835918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6919c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
692035918f85SRandall Stewart 		*error = EINVAL;
692135918f85SRandall Stewart 		return;
692235918f85SRandall Stewart 	}
692335918f85SRandall Stewart 	addr_touse = sa;
6924ea5eba11SMichael Tuexen #ifdef INET6
692535918f85SRandall Stewart 	if (sa->sa_family == AF_INET6) {
6926d59107f7SMichael Tuexen #ifdef INET
692735918f85SRandall Stewart 		struct sockaddr_in6 *sin6;
692835918f85SRandall Stewart 
6929d59107f7SMichael Tuexen #endif
693035918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6931c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
693235918f85SRandall Stewart 			*error = EINVAL;
693335918f85SRandall Stewart 			return;
693435918f85SRandall Stewart 		}
6935db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6936db4fd95bSRandall Stewart 			/* can only bind v6 on PF_INET6 sockets */
6937c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6938db4fd95bSRandall Stewart 			*error = EINVAL;
6939db4fd95bSRandall Stewart 			return;
6940db4fd95bSRandall Stewart 		}
6941d59107f7SMichael Tuexen #ifdef INET
694235918f85SRandall Stewart 		sin6 = (struct sockaddr_in6 *)addr_touse;
694335918f85SRandall Stewart 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6944db4fd95bSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6945db4fd95bSRandall Stewart 			    SCTP_IPV6_V6ONLY(inp)) {
6946db4fd95bSRandall Stewart 				/* can't bind v4-mapped on PF_INET sockets */
6947c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6948db4fd95bSRandall Stewart 				*error = EINVAL;
6949db4fd95bSRandall Stewart 				return;
6950db4fd95bSRandall Stewart 			}
695135918f85SRandall Stewart 			in6_sin6_2_sin(&sin, sin6);
695235918f85SRandall Stewart 			addr_touse = (struct sockaddr *)&sin;
695335918f85SRandall Stewart 		}
6954d59107f7SMichael Tuexen #endif
695535918f85SRandall Stewart 	}
695635918f85SRandall Stewart #endif
6957ea5eba11SMichael Tuexen #ifdef INET
695835918f85SRandall Stewart 	if (sa->sa_family == AF_INET) {
695935918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in)) {
6960c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
696135918f85SRandall Stewart 			*error = EINVAL;
696235918f85SRandall Stewart 			return;
696335918f85SRandall Stewart 		}
6964db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6965db4fd95bSRandall Stewart 		    SCTP_IPV6_V6ONLY(inp)) {
6966db4fd95bSRandall Stewart 			/* can't bind v4 on PF_INET sockets */
6967c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6968db4fd95bSRandall Stewart 			*error = EINVAL;
6969db4fd95bSRandall Stewart 			return;
6970db4fd95bSRandall Stewart 		}
697135918f85SRandall Stewart 	}
6972ea5eba11SMichael Tuexen #endif
697335918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
697435918f85SRandall Stewart 		if (p == NULL) {
697535918f85SRandall Stewart 			/* Can't get proc for Net/Open BSD */
6976c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
697735918f85SRandall Stewart 			*error = EINVAL;
697835918f85SRandall Stewart 			return;
697935918f85SRandall Stewart 		}
69801b649582SRandall Stewart 		*error = sctp_inpcb_bind(so, addr_touse, NULL, p);
698135918f85SRandall Stewart 		return;
698235918f85SRandall Stewart 	}
698335918f85SRandall Stewart 	/*
698435918f85SRandall Stewart 	 * No locks required here since bind and mgmt_ep_sa all do their own
698535918f85SRandall Stewart 	 * locking. If we do something for the FIX: below we may need to
698635918f85SRandall Stewart 	 * lock in that case.
698735918f85SRandall Stewart 	 */
698835918f85SRandall Stewart 	if (assoc_id == 0) {
698935918f85SRandall Stewart 		/* add the address */
699035918f85SRandall Stewart 		struct sctp_inpcb *lep;
699197c76f10SRandall Stewart 		struct sockaddr_in *lsin = (struct sockaddr_in *)addr_touse;
699235918f85SRandall Stewart 
699397c76f10SRandall Stewart 		/* validate the incoming port */
699497c76f10SRandall Stewart 		if ((lsin->sin_port != 0) &&
699597c76f10SRandall Stewart 		    (lsin->sin_port != inp->sctp_lport)) {
6996c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
699797c76f10SRandall Stewart 			*error = EINVAL;
699897c76f10SRandall Stewart 			return;
699997c76f10SRandall Stewart 		} else {
700097c76f10SRandall Stewart 			/* user specified 0 port, set it to existing port */
700197c76f10SRandall Stewart 			lsin->sin_port = inp->sctp_lport;
700297c76f10SRandall Stewart 		}
700397c76f10SRandall Stewart 
700435918f85SRandall Stewart 		lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id);
700535918f85SRandall Stewart 		if (lep != NULL) {
700635918f85SRandall Stewart 			/*
700735918f85SRandall Stewart 			 * We must decrement the refcount since we have the
700835918f85SRandall Stewart 			 * ep already and are binding. No remove going on
700935918f85SRandall Stewart 			 * here.
701035918f85SRandall Stewart 			 */
70116d9e8f2bSRandall Stewart 			SCTP_INP_DECR_REF(lep);
701235918f85SRandall Stewart 		}
701335918f85SRandall Stewart 		if (lep == inp) {
701435918f85SRandall Stewart 			/* already bound to it.. ok */
701535918f85SRandall Stewart 			return;
701635918f85SRandall Stewart 		} else if (lep == NULL) {
701735918f85SRandall Stewart 			((struct sockaddr_in *)addr_touse)->sin_port = 0;
701835918f85SRandall Stewart 			*error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
701935918f85SRandall Stewart 			    SCTP_ADD_IP_ADDRESS,
702080fefe0aSRandall Stewart 			    vrf_id, NULL);
702135918f85SRandall Stewart 		} else {
702235918f85SRandall Stewart 			*error = EADDRINUSE;
702335918f85SRandall Stewart 		}
702435918f85SRandall Stewart 		if (*error)
702535918f85SRandall Stewart 			return;
702635918f85SRandall Stewart 	} else {
702735918f85SRandall Stewart 		/*
702835918f85SRandall Stewart 		 * FIX: decide whether we allow assoc based bindx
702935918f85SRandall Stewart 		 */
703035918f85SRandall Stewart 	}
703135918f85SRandall Stewart }
703235918f85SRandall Stewart 
703335918f85SRandall Stewart /*
703435918f85SRandall Stewart  * sctp_bindx(DELETE) for one address.
703535918f85SRandall Stewart  * assumes all arguments are valid/checked by caller.
703635918f85SRandall Stewart  */
703735918f85SRandall Stewart void
70387215cc1bSMichael Tuexen sctp_bindx_delete_address(struct sctp_inpcb *inp,
703935918f85SRandall Stewart     struct sockaddr *sa, sctp_assoc_t assoc_id,
704035918f85SRandall Stewart     uint32_t vrf_id, int *error)
704135918f85SRandall Stewart {
704235918f85SRandall Stewart 	struct sockaddr *addr_touse;
7043d59107f7SMichael Tuexen #if defined(INET) && defined(INET6)
704435918f85SRandall Stewart 	struct sockaddr_in sin;
70455e2c2d87SRandall Stewart #endif
70465e2c2d87SRandall Stewart 
704735918f85SRandall Stewart 	/* see if we're bound all already! */
704835918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
7049c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
705035918f85SRandall Stewart 		*error = EINVAL;
705135918f85SRandall Stewart 		return;
705235918f85SRandall Stewart 	}
705335918f85SRandall Stewart 	addr_touse = sa;
7054e0e00a4dSMichael Tuexen #ifdef INET6
705535918f85SRandall Stewart 	if (sa->sa_family == AF_INET6) {
7056d59107f7SMichael Tuexen #ifdef INET
705735918f85SRandall Stewart 		struct sockaddr_in6 *sin6;
7058d59107f7SMichael Tuexen #endif
7059d59107f7SMichael Tuexen 
706035918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in6)) {
7061c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
706235918f85SRandall Stewart 			*error = EINVAL;
706335918f85SRandall Stewart 			return;
706435918f85SRandall Stewart 		}
7065db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
7066db4fd95bSRandall Stewart 			/* can only bind v6 on PF_INET6 sockets */
7067c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7068db4fd95bSRandall Stewart 			*error = EINVAL;
7069db4fd95bSRandall Stewart 			return;
7070db4fd95bSRandall Stewart 		}
7071d59107f7SMichael Tuexen #ifdef INET
707235918f85SRandall Stewart 		sin6 = (struct sockaddr_in6 *)addr_touse;
707335918f85SRandall Stewart 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
7074db4fd95bSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
7075db4fd95bSRandall Stewart 			    SCTP_IPV6_V6ONLY(inp)) {
7076db4fd95bSRandall Stewart 				/* can't bind mapped-v4 on PF_INET sockets */
7077c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7078db4fd95bSRandall Stewart 				*error = EINVAL;
7079db4fd95bSRandall Stewart 				return;
7080db4fd95bSRandall Stewart 			}
708135918f85SRandall Stewart 			in6_sin6_2_sin(&sin, sin6);
708235918f85SRandall Stewart 			addr_touse = (struct sockaddr *)&sin;
708335918f85SRandall Stewart 		}
7084d59107f7SMichael Tuexen #endif
708535918f85SRandall Stewart 	}
708635918f85SRandall Stewart #endif
7087ea5eba11SMichael Tuexen #ifdef INET
708835918f85SRandall Stewart 	if (sa->sa_family == AF_INET) {
708935918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in)) {
7090c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
709135918f85SRandall Stewart 			*error = EINVAL;
709235918f85SRandall Stewart 			return;
709335918f85SRandall Stewart 		}
7094db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
7095db4fd95bSRandall Stewart 		    SCTP_IPV6_V6ONLY(inp)) {
7096db4fd95bSRandall Stewart 			/* can't bind v4 on PF_INET sockets */
7097c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7098db4fd95bSRandall Stewart 			*error = EINVAL;
7099db4fd95bSRandall Stewart 			return;
7100db4fd95bSRandall Stewart 		}
710135918f85SRandall Stewart 	}
7102ea5eba11SMichael Tuexen #endif
710335918f85SRandall Stewart 	/*
710435918f85SRandall Stewart 	 * No lock required mgmt_ep_sa does its own locking. If the FIX:
710535918f85SRandall Stewart 	 * below is ever changed we may need to lock before calling
710635918f85SRandall Stewart 	 * association level binding.
710735918f85SRandall Stewart 	 */
710835918f85SRandall Stewart 	if (assoc_id == 0) {
710935918f85SRandall Stewart 		/* delete the address */
711035918f85SRandall Stewart 		*error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
711135918f85SRandall Stewart 		    SCTP_DEL_IP_ADDRESS,
711280fefe0aSRandall Stewart 		    vrf_id, NULL);
711335918f85SRandall Stewart 	} else {
711435918f85SRandall Stewart 		/*
711535918f85SRandall Stewart 		 * FIX: decide whether we allow assoc based bindx
711635918f85SRandall Stewart 		 */
711735918f85SRandall Stewart 	}
711835918f85SRandall Stewart }
71191b649582SRandall Stewart 
71201b649582SRandall Stewart /*
71211b649582SRandall Stewart  * returns the valid local address count for an assoc, taking into account
71221b649582SRandall Stewart  * all scoping rules
71231b649582SRandall Stewart  */
71241b649582SRandall Stewart int
71251b649582SRandall Stewart sctp_local_addr_count(struct sctp_tcb *stcb)
71261b649582SRandall Stewart {
7127b54ddf22SMichael Tuexen 	int loopback_scope;
7128b54ddf22SMichael Tuexen #if defined(INET)
7129b54ddf22SMichael Tuexen 	int ipv4_local_scope, ipv4_addr_legal;
7130b54ddf22SMichael Tuexen #endif
7131b54ddf22SMichael Tuexen #if defined (INET6)
7132b54ddf22SMichael Tuexen 	int local_scope, site_scope, ipv6_addr_legal;
7133b54ddf22SMichael Tuexen #endif
71341b649582SRandall Stewart 	struct sctp_vrf *vrf;
71351b649582SRandall Stewart 	struct sctp_ifn *sctp_ifn;
71361b649582SRandall Stewart 	struct sctp_ifa *sctp_ifa;
71371b649582SRandall Stewart 	int count = 0;
71381b649582SRandall Stewart 
71391b649582SRandall Stewart 	/* Turn on all the appropriate scopes */
7140a1cb341bSMichael Tuexen 	loopback_scope = stcb->asoc.scope.loopback_scope;
7141b54ddf22SMichael Tuexen #if defined(INET)
7142a1cb341bSMichael Tuexen 	ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
7143b54ddf22SMichael Tuexen 	ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
7144b54ddf22SMichael Tuexen #endif
7145b54ddf22SMichael Tuexen #if defined(INET6)
7146a1cb341bSMichael Tuexen 	local_scope = stcb->asoc.scope.local_scope;
7147a1cb341bSMichael Tuexen 	site_scope = stcb->asoc.scope.site_scope;
7148a1cb341bSMichael Tuexen 	ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
7149b54ddf22SMichael Tuexen #endif
7150c99efcf6SRandall Stewart 	SCTP_IPI_ADDR_RLOCK();
71511b649582SRandall Stewart 	vrf = sctp_find_vrf(stcb->asoc.vrf_id);
71521b649582SRandall Stewart 	if (vrf == NULL) {
71531b649582SRandall Stewart 		/* no vrf, no addresses */
7154c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RUNLOCK();
71551b649582SRandall Stewart 		return (0);
71561b649582SRandall Stewart 	}
71570053ed28SMichael Tuexen 
71581b649582SRandall Stewart 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
71591b649582SRandall Stewart 		/*
71601b649582SRandall Stewart 		 * bound all case: go through all ifns on the vrf
71611b649582SRandall Stewart 		 */
71621b649582SRandall Stewart 		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
71631b649582SRandall Stewart 			if ((loopback_scope == 0) &&
71641b649582SRandall Stewart 			    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
71651b649582SRandall Stewart 				continue;
71661b649582SRandall Stewart 			}
71671b649582SRandall Stewart 			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
71681b649582SRandall Stewart 				if (sctp_is_addr_restricted(stcb, sctp_ifa))
71691b649582SRandall Stewart 					continue;
71705e2c2d87SRandall Stewart 				switch (sctp_ifa->address.sa.sa_family) {
7171ea5eba11SMichael Tuexen #ifdef INET
71725e2c2d87SRandall Stewart 				case AF_INET:
71735e2c2d87SRandall Stewart 					if (ipv4_addr_legal) {
71741b649582SRandall Stewart 						struct sockaddr_in *sin;
71751b649582SRandall Stewart 
717624aaac8dSMichael Tuexen 						sin = &sctp_ifa->address.sin;
71771b649582SRandall Stewart 						if (sin->sin_addr.s_addr == 0) {
7178b7b84c0eSMichael Tuexen 							/*
7179b7b84c0eSMichael Tuexen 							 * skip unspecified
7180b7b84c0eSMichael Tuexen 							 * addrs
7181b7b84c0eSMichael Tuexen 							 */
71821b649582SRandall Stewart 							continue;
71831b649582SRandall Stewart 						}
71846ba22f19SMichael Tuexen 						if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
71856ba22f19SMichael Tuexen 						    &sin->sin_addr) != 0) {
71866ba22f19SMichael Tuexen 							continue;
71876ba22f19SMichael Tuexen 						}
71881b649582SRandall Stewart 						if ((ipv4_local_scope == 0) &&
71891b649582SRandall Stewart 						    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
71901b649582SRandall Stewart 							continue;
71911b649582SRandall Stewart 						}
71921b649582SRandall Stewart 						/* count this one */
71931b649582SRandall Stewart 						count++;
71945e2c2d87SRandall Stewart 					} else {
71955e2c2d87SRandall Stewart 						continue;
71965e2c2d87SRandall Stewart 					}
71975e2c2d87SRandall Stewart 					break;
7198ea5eba11SMichael Tuexen #endif
71995e2c2d87SRandall Stewart #ifdef INET6
72005e2c2d87SRandall Stewart 				case AF_INET6:
72015e2c2d87SRandall Stewart 					if (ipv6_addr_legal) {
72021b649582SRandall Stewart 						struct sockaddr_in6 *sin6;
72031b649582SRandall Stewart 
720424aaac8dSMichael Tuexen 						sin6 = &sctp_ifa->address.sin6;
72051b649582SRandall Stewart 						if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
72061b649582SRandall Stewart 							continue;
72071b649582SRandall Stewart 						}
72086ba22f19SMichael Tuexen 						if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
72096ba22f19SMichael Tuexen 						    &sin6->sin6_addr) != 0) {
72106ba22f19SMichael Tuexen 							continue;
72116ba22f19SMichael Tuexen 						}
72121b649582SRandall Stewart 						if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
72131b649582SRandall Stewart 							if (local_scope == 0)
72141b649582SRandall Stewart 								continue;
72151b649582SRandall Stewart 							if (sin6->sin6_scope_id == 0) {
72161b649582SRandall Stewart 								if (sa6_recoverscope(sin6) != 0)
72171b649582SRandall Stewart 									/*
72185e2c2d87SRandall Stewart 									 *
72195e2c2d87SRandall Stewart 									 * bad
72205b495f17SMichael Tuexen 									 * link
72215e2c2d87SRandall Stewart 									 *
72225b495f17SMichael Tuexen 									 * local
72235e2c2d87SRandall Stewart 									 *
72245b495f17SMichael Tuexen 									 * address
72255b495f17SMichael Tuexen 									 */
72261b649582SRandall Stewart 									continue;
72271b649582SRandall Stewart 							}
72281b649582SRandall Stewart 						}
72291b649582SRandall Stewart 						if ((site_scope == 0) &&
72301b649582SRandall Stewart 						    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
72311b649582SRandall Stewart 							continue;
72321b649582SRandall Stewart 						}
72331b649582SRandall Stewart 						/* count this one */
72341b649582SRandall Stewart 						count++;
72351b649582SRandall Stewart 					}
72365e2c2d87SRandall Stewart 					break;
72375e2c2d87SRandall Stewart #endif
72385e2c2d87SRandall Stewart 				default:
72395e2c2d87SRandall Stewart 					/* TSNH */
72405e2c2d87SRandall Stewart 					break;
72415e2c2d87SRandall Stewart 				}
72421b649582SRandall Stewart 			}
72431b649582SRandall Stewart 		}
72441b649582SRandall Stewart 	} else {
72451b649582SRandall Stewart 		/*
72461b649582SRandall Stewart 		 * subset bound case
72471b649582SRandall Stewart 		 */
72481b649582SRandall Stewart 		struct sctp_laddr *laddr;
72491b649582SRandall Stewart 
72501b649582SRandall Stewart 		LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list,
72511b649582SRandall Stewart 		    sctp_nxt_addr) {
72521b649582SRandall Stewart 			if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
72531b649582SRandall Stewart 				continue;
72541b649582SRandall Stewart 			}
72551b649582SRandall Stewart 			/* count this one */
72561b649582SRandall Stewart 			count++;
72571b649582SRandall Stewart 		}
72581b649582SRandall Stewart 	}
7259c99efcf6SRandall Stewart 	SCTP_IPI_ADDR_RUNLOCK();
72601b649582SRandall Stewart 	return (count);
72611b649582SRandall Stewart }
7262c4739e2fSRandall Stewart 
7263c4739e2fSRandall Stewart #if defined(SCTP_LOCAL_TRACE_BUF)
7264c4739e2fSRandall Stewart 
7265c4739e2fSRandall Stewart void
7266b27a6b7dSRandall 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)
7267c4739e2fSRandall Stewart {
7268b27a6b7dSRandall Stewart 	uint32_t saveindex, newindex;
7269c4739e2fSRandall Stewart 
7270c4739e2fSRandall Stewart 	do {
7271b3f1ea41SRandall Stewart 		saveindex = SCTP_BASE_SYSCTL(sctp_log).index;
7272c4739e2fSRandall Stewart 		if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
7273c4739e2fSRandall Stewart 			newindex = 1;
7274c4739e2fSRandall Stewart 		} else {
7275c4739e2fSRandall Stewart 			newindex = saveindex + 1;
7276c4739e2fSRandall Stewart 		}
7277b3f1ea41SRandall Stewart 	} while (atomic_cmpset_int(&SCTP_BASE_SYSCTL(sctp_log).index, saveindex, newindex) == 0);
7278c4739e2fSRandall Stewart 	if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
7279c4739e2fSRandall Stewart 		saveindex = 0;
7280c4739e2fSRandall Stewart 	}
7281b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].timestamp = SCTP_GET_CYCLECOUNT;
7282b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].subsys = subsys;
7283b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[0] = a;
7284b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[1] = b;
7285b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[2] = c;
7286b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[3] = d;
7287b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[4] = e;
7288b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[5] = f;
7289c4739e2fSRandall Stewart }
7290c4739e2fSRandall Stewart 
7291c4739e2fSRandall Stewart #endif
7292a99b6783SRandall Stewart static void
72937cca1775SRandall Stewart sctp_recv_udp_tunneled_packet(struct mbuf *m, int off, struct inpcb *inp,
729481d3ec17SBryan Venteicher     const struct sockaddr *sa SCTP_UNUSED, void *ctx SCTP_UNUSED)
7295a99b6783SRandall Stewart {
7296a99b6783SRandall Stewart 	struct ip *iph;
72973a51a264SMichael Tuexen #ifdef INET6
72983a51a264SMichael Tuexen 	struct ip6_hdr *ip6;
72993a51a264SMichael Tuexen #endif
7300a99b6783SRandall Stewart 	struct mbuf *sp, *last;
7301a99b6783SRandall Stewart 	struct udphdr *uhdr;
7302285052f0SMichael Tuexen 	uint16_t port;
7303a99b6783SRandall Stewart 
7304a99b6783SRandall Stewart 	if ((m->m_flags & M_PKTHDR) == 0) {
7305a99b6783SRandall Stewart 		/* Can't handle one that is not a pkt hdr */
7306a99b6783SRandall Stewart 		goto out;
7307a99b6783SRandall Stewart 	}
7308285052f0SMichael Tuexen 	/* Pull the src port */
7309a99b6783SRandall Stewart 	iph = mtod(m, struct ip *);
7310a99b6783SRandall Stewart 	uhdr = (struct udphdr *)((caddr_t)iph + off);
7311a99b6783SRandall Stewart 	port = uhdr->uh_sport;
7312285052f0SMichael Tuexen 	/*
7313285052f0SMichael Tuexen 	 * Split out the mbuf chain. Leave the IP header in m, place the
7314285052f0SMichael Tuexen 	 * rest in the sp.
7315285052f0SMichael Tuexen 	 */
7316eb1b1807SGleb Smirnoff 	sp = m_split(m, off, M_NOWAIT);
7317a99b6783SRandall Stewart 	if (sp == NULL) {
7318a99b6783SRandall Stewart 		/* Gak, drop packet, we can't do a split */
7319a99b6783SRandall Stewart 		goto out;
7320a99b6783SRandall Stewart 	}
7321285052f0SMichael Tuexen 	if (sp->m_pkthdr.len < sizeof(struct udphdr) + sizeof(struct sctphdr)) {
7322285052f0SMichael Tuexen 		/* Gak, packet can't have an SCTP header in it - too small */
7323a99b6783SRandall Stewart 		m_freem(sp);
7324a99b6783SRandall Stewart 		goto out;
7325a99b6783SRandall Stewart 	}
7326285052f0SMichael Tuexen 	/* Now pull up the UDP header and SCTP header together */
7327285052f0SMichael Tuexen 	sp = m_pullup(sp, sizeof(struct udphdr) + sizeof(struct sctphdr));
7328a99b6783SRandall Stewart 	if (sp == NULL) {
7329a99b6783SRandall Stewart 		/* Gak pullup failed */
7330a99b6783SRandall Stewart 		goto out;
7331a99b6783SRandall Stewart 	}
7332285052f0SMichael Tuexen 	/* Trim out the UDP header */
7333a99b6783SRandall Stewart 	m_adj(sp, sizeof(struct udphdr));
7334a99b6783SRandall Stewart 
7335a99b6783SRandall Stewart 	/* Now reconstruct the mbuf chain */
7336285052f0SMichael Tuexen 	for (last = m; last->m_next; last = last->m_next);
7337a99b6783SRandall Stewart 	last->m_next = sp;
7338a99b6783SRandall Stewart 	m->m_pkthdr.len += sp->m_pkthdr.len;
733952f175beSMichael Tuexen 	/*
734052f175beSMichael Tuexen 	 * The CSUM_DATA_VALID flags indicates that the HW checked the UDP
734152f175beSMichael Tuexen 	 * checksum and it was valid. Since CSUM_DATA_VALID ==
734252f175beSMichael Tuexen 	 * CSUM_SCTP_VALID this would imply that the HW also verified the
734352f175beSMichael Tuexen 	 * SCTP checksum. Therefore, clear the bit.
734452f175beSMichael Tuexen 	 */
734552f175beSMichael Tuexen 	SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
734652f175beSMichael Tuexen 	    "sctp_recv_udp_tunneled_packet(): Packet of length %d received on %s with csum_flags 0x%b.\n",
734752f175beSMichael Tuexen 	    m->m_pkthdr.len,
734852f175beSMichael Tuexen 	    if_name(m->m_pkthdr.rcvif),
734952f175beSMichael Tuexen 	    (int)m->m_pkthdr.csum_flags, CSUM_BITS);
735052f175beSMichael Tuexen 	m->m_pkthdr.csum_flags &= ~CSUM_DATA_VALID;
7351a99b6783SRandall Stewart 	iph = mtod(m, struct ip *);
7352a99b6783SRandall Stewart 	switch (iph->ip_v) {
7353e6194c2eSMichael Tuexen #ifdef INET
7354a99b6783SRandall Stewart 	case IPVERSION:
735509c1c856SMichael Tuexen 		iph->ip_len = htons(ntohs(iph->ip_len) - sizeof(struct udphdr));
7356a99b6783SRandall Stewart 		sctp_input_with_port(m, off, port);
7357a99b6783SRandall Stewart 		break;
7358e6194c2eSMichael Tuexen #endif
7359a99b6783SRandall Stewart #ifdef INET6
7360a99b6783SRandall Stewart 	case IPV6_VERSION >> 4:
73613a51a264SMichael Tuexen 		ip6 = mtod(m, struct ip6_hdr *);
73623a51a264SMichael Tuexen 		ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - sizeof(struct udphdr));
73633a51a264SMichael Tuexen 		sctp6_input_with_port(&m, &off, port);
7364a99b6783SRandall Stewart 		break;
7365a99b6783SRandall Stewart #endif
7366a99b6783SRandall Stewart 	default:
7367285052f0SMichael Tuexen 		goto out;
7368a99b6783SRandall Stewart 		break;
7369a99b6783SRandall Stewart 	}
7370a99b6783SRandall Stewart 	return;
7371a99b6783SRandall Stewart out:
7372a99b6783SRandall Stewart 	m_freem(m);
7373a99b6783SRandall Stewart }
7374c54a18d2SRandall Stewart 
7375fd7af143SMichael Tuexen #ifdef INET
7376fd7af143SMichael Tuexen static void
7377fd7af143SMichael Tuexen sctp_recv_icmp_tunneled_packet(int cmd, struct sockaddr *sa, void *vip, void *ctx SCTP_UNUSED)
7378fd7af143SMichael Tuexen {
7379fd7af143SMichael Tuexen 	struct ip *outer_ip, *inner_ip;
7380fd7af143SMichael Tuexen 	struct sctphdr *sh;
7381fd7af143SMichael Tuexen 	struct icmp *icmp;
7382fd7af143SMichael Tuexen 	struct udphdr *udp;
7383fd7af143SMichael Tuexen 	struct sctp_inpcb *inp;
7384fd7af143SMichael Tuexen 	struct sctp_tcb *stcb;
7385fd7af143SMichael Tuexen 	struct sctp_nets *net;
7386fd7af143SMichael Tuexen 	struct sctp_init_chunk *ch;
7387fd7af143SMichael Tuexen 	struct sockaddr_in src, dst;
7388fd7af143SMichael Tuexen 	uint8_t type, code;
7389fd7af143SMichael Tuexen 
7390fd7af143SMichael Tuexen 	inner_ip = (struct ip *)vip;
7391fd7af143SMichael Tuexen 	icmp = (struct icmp *)((caddr_t)inner_ip -
7392fd7af143SMichael Tuexen 	    (sizeof(struct icmp) - sizeof(struct ip)));
7393fd7af143SMichael Tuexen 	outer_ip = (struct ip *)((caddr_t)icmp - sizeof(struct ip));
7394fd7af143SMichael Tuexen 	if (ntohs(outer_ip->ip_len) <
7395fd7af143SMichael Tuexen 	    sizeof(struct ip) + 8 + (inner_ip->ip_hl << 2) + sizeof(struct udphdr) + 8) {
7396fd7af143SMichael Tuexen 		return;
7397fd7af143SMichael Tuexen 	}
7398fd7af143SMichael Tuexen 	udp = (struct udphdr *)((caddr_t)inner_ip + (inner_ip->ip_hl << 2));
7399fd7af143SMichael Tuexen 	sh = (struct sctphdr *)(udp + 1);
7400fd7af143SMichael Tuexen 	memset(&src, 0, sizeof(struct sockaddr_in));
7401fd7af143SMichael Tuexen 	src.sin_family = AF_INET;
7402fd7af143SMichael Tuexen 	src.sin_len = sizeof(struct sockaddr_in);
7403fd7af143SMichael Tuexen 	src.sin_port = sh->src_port;
7404fd7af143SMichael Tuexen 	src.sin_addr = inner_ip->ip_src;
7405fd7af143SMichael Tuexen 	memset(&dst, 0, sizeof(struct sockaddr_in));
7406fd7af143SMichael Tuexen 	dst.sin_family = AF_INET;
7407fd7af143SMichael Tuexen 	dst.sin_len = sizeof(struct sockaddr_in);
7408fd7af143SMichael Tuexen 	dst.sin_port = sh->dest_port;
7409fd7af143SMichael Tuexen 	dst.sin_addr = inner_ip->ip_dst;
7410fd7af143SMichael Tuexen 	/*
7411fd7af143SMichael Tuexen 	 * 'dst' holds the dest of the packet that failed to be sent. 'src'
7412fd7af143SMichael Tuexen 	 * holds our local endpoint address. Thus we reverse the dst and the
7413fd7af143SMichael Tuexen 	 * src in the lookup.
7414fd7af143SMichael Tuexen 	 */
7415fd7af143SMichael Tuexen 	inp = NULL;
7416fd7af143SMichael Tuexen 	net = NULL;
7417fd7af143SMichael Tuexen 	stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
7418fd7af143SMichael Tuexen 	    (struct sockaddr *)&src,
7419fd7af143SMichael Tuexen 	    &inp, &net, 1,
7420fd7af143SMichael Tuexen 	    SCTP_DEFAULT_VRFID);
7421fd7af143SMichael Tuexen 	if ((stcb != NULL) &&
7422fd7af143SMichael Tuexen 	    (net != NULL) &&
742355b8cd93SMichael Tuexen 	    (inp != NULL)) {
7424fd7af143SMichael Tuexen 		/* Check the UDP port numbers */
7425fd7af143SMichael Tuexen 		if ((udp->uh_dport != net->port) ||
7426fd7af143SMichael Tuexen 		    (udp->uh_sport != htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)))) {
7427fd7af143SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
7428fd7af143SMichael Tuexen 			return;
7429fd7af143SMichael Tuexen 		}
7430fd7af143SMichael Tuexen 		/* Check the verification tag */
7431fd7af143SMichael Tuexen 		if (ntohl(sh->v_tag) != 0) {
7432fd7af143SMichael Tuexen 			/*
7433fd7af143SMichael Tuexen 			 * This must be the verification tag used for
7434fd7af143SMichael Tuexen 			 * sending out packets. We don't consider packets
7435fd7af143SMichael Tuexen 			 * reflecting the verification tag.
7436fd7af143SMichael Tuexen 			 */
7437fd7af143SMichael Tuexen 			if (ntohl(sh->v_tag) != stcb->asoc.peer_vtag) {
7438fd7af143SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
7439fd7af143SMichael Tuexen 				return;
7440fd7af143SMichael Tuexen 			}
7441fd7af143SMichael Tuexen 		} else {
7442fd7af143SMichael Tuexen 			if (ntohs(outer_ip->ip_len) >=
7443fd7af143SMichael Tuexen 			    sizeof(struct ip) +
7444fd7af143SMichael Tuexen 			    8 + (inner_ip->ip_hl << 2) + 8 + 20) {
7445fd7af143SMichael Tuexen 				/*
7446fd7af143SMichael Tuexen 				 * In this case we can check if we got an
7447fd7af143SMichael Tuexen 				 * INIT chunk and if the initiate tag
7448fd7af143SMichael Tuexen 				 * matches.
7449fd7af143SMichael Tuexen 				 */
7450fd7af143SMichael Tuexen 				ch = (struct sctp_init_chunk *)(sh + 1);
7451fd7af143SMichael Tuexen 				if ((ch->ch.chunk_type != SCTP_INITIATION) ||
7452fd7af143SMichael Tuexen 				    (ntohl(ch->init.initiate_tag) != stcb->asoc.my_vtag)) {
7453fd7af143SMichael Tuexen 					SCTP_TCB_UNLOCK(stcb);
7454fd7af143SMichael Tuexen 					return;
7455fd7af143SMichael Tuexen 				}
7456fd7af143SMichael Tuexen 			} else {
7457fd7af143SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
7458fd7af143SMichael Tuexen 				return;
7459fd7af143SMichael Tuexen 			}
7460fd7af143SMichael Tuexen 		}
7461fd7af143SMichael Tuexen 		type = icmp->icmp_type;
7462fd7af143SMichael Tuexen 		code = icmp->icmp_code;
74633c3f9e2aSMichael Tuexen 		if ((type == ICMP_UNREACH) &&
74643c3f9e2aSMichael Tuexen 		    (code == ICMP_UNREACH_PORT)) {
7465fd7af143SMichael Tuexen 			code = ICMP_UNREACH_PROTOCOL;
7466fd7af143SMichael Tuexen 		}
7467fd7af143SMichael Tuexen 		sctp_notify(inp, stcb, net, type, code,
7468fd7af143SMichael Tuexen 		    ntohs(inner_ip->ip_len),
74696ebfa5eeSMichael Tuexen 		    (uint32_t)ntohs(icmp->icmp_nextmtu));
7470fd7af143SMichael Tuexen 	} else {
7471fd7af143SMichael Tuexen 		if ((stcb == NULL) && (inp != NULL)) {
7472fd7af143SMichael Tuexen 			/* reduce ref-count */
7473fd7af143SMichael Tuexen 			SCTP_INP_WLOCK(inp);
7474fd7af143SMichael Tuexen 			SCTP_INP_DECR_REF(inp);
7475fd7af143SMichael Tuexen 			SCTP_INP_WUNLOCK(inp);
7476fd7af143SMichael Tuexen 		}
7477fd7af143SMichael Tuexen 		if (stcb) {
7478fd7af143SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
7479fd7af143SMichael Tuexen 		}
7480fd7af143SMichael Tuexen 	}
7481fd7af143SMichael Tuexen 	return;
7482fd7af143SMichael Tuexen }
7483fd7af143SMichael Tuexen #endif
7484fd7af143SMichael Tuexen 
7485fd7af143SMichael Tuexen #ifdef INET6
7486fd7af143SMichael Tuexen static void
7487fd7af143SMichael Tuexen sctp_recv_icmp6_tunneled_packet(int cmd, struct sockaddr *sa, void *d, void *ctx SCTP_UNUSED)
7488fd7af143SMichael Tuexen {
7489fd7af143SMichael Tuexen 	struct ip6ctlparam *ip6cp;
7490fd7af143SMichael Tuexen 	struct sctp_inpcb *inp;
7491fd7af143SMichael Tuexen 	struct sctp_tcb *stcb;
7492fd7af143SMichael Tuexen 	struct sctp_nets *net;
7493fd7af143SMichael Tuexen 	struct sctphdr sh;
7494fd7af143SMichael Tuexen 	struct udphdr udp;
7495fd7af143SMichael Tuexen 	struct sockaddr_in6 src, dst;
7496fd7af143SMichael Tuexen 	uint8_t type, code;
7497fd7af143SMichael Tuexen 
7498fd7af143SMichael Tuexen 	ip6cp = (struct ip6ctlparam *)d;
7499fd7af143SMichael Tuexen 	/*
7500fd7af143SMichael Tuexen 	 * XXX: We assume that when IPV6 is non NULL, M and OFF are valid.
7501fd7af143SMichael Tuexen 	 */
7502fd7af143SMichael Tuexen 	if (ip6cp->ip6c_m == NULL) {
7503fd7af143SMichael Tuexen 		return;
7504fd7af143SMichael Tuexen 	}
7505fd7af143SMichael Tuexen 	/*
7506fd7af143SMichael Tuexen 	 * Check if we can safely examine the ports and the verification tag
7507fd7af143SMichael Tuexen 	 * of the SCTP common header.
7508fd7af143SMichael Tuexen 	 */
7509fd7af143SMichael Tuexen 	if (ip6cp->ip6c_m->m_pkthdr.len <
7510fd7af143SMichael Tuexen 	    ip6cp->ip6c_off + sizeof(struct udphdr) + offsetof(struct sctphdr, checksum)) {
7511fd7af143SMichael Tuexen 		return;
7512fd7af143SMichael Tuexen 	}
7513fd7af143SMichael Tuexen 	/* Copy out the UDP header. */
7514fd7af143SMichael Tuexen 	memset(&udp, 0, sizeof(struct udphdr));
7515fd7af143SMichael Tuexen 	m_copydata(ip6cp->ip6c_m,
7516fd7af143SMichael Tuexen 	    ip6cp->ip6c_off,
7517fd7af143SMichael Tuexen 	    sizeof(struct udphdr),
7518fd7af143SMichael Tuexen 	    (caddr_t)&udp);
7519fd7af143SMichael Tuexen 	/* Copy out the port numbers and the verification tag. */
7520fd7af143SMichael Tuexen 	memset(&sh, 0, sizeof(struct sctphdr));
7521fd7af143SMichael Tuexen 	m_copydata(ip6cp->ip6c_m,
7522fd7af143SMichael Tuexen 	    ip6cp->ip6c_off + sizeof(struct udphdr),
7523fd7af143SMichael Tuexen 	    sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
7524fd7af143SMichael Tuexen 	    (caddr_t)&sh);
7525fd7af143SMichael Tuexen 	memset(&src, 0, sizeof(struct sockaddr_in6));
7526fd7af143SMichael Tuexen 	src.sin6_family = AF_INET6;
7527fd7af143SMichael Tuexen 	src.sin6_len = sizeof(struct sockaddr_in6);
7528fd7af143SMichael Tuexen 	src.sin6_port = sh.src_port;
7529fd7af143SMichael Tuexen 	src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
7530fd7af143SMichael Tuexen 	if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
7531fd7af143SMichael Tuexen 		return;
7532fd7af143SMichael Tuexen 	}
7533fd7af143SMichael Tuexen 	memset(&dst, 0, sizeof(struct sockaddr_in6));
7534fd7af143SMichael Tuexen 	dst.sin6_family = AF_INET6;
7535fd7af143SMichael Tuexen 	dst.sin6_len = sizeof(struct sockaddr_in6);
7536fd7af143SMichael Tuexen 	dst.sin6_port = sh.dest_port;
7537fd7af143SMichael Tuexen 	dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
7538fd7af143SMichael Tuexen 	if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
7539fd7af143SMichael Tuexen 		return;
7540fd7af143SMichael Tuexen 	}
7541fd7af143SMichael Tuexen 	inp = NULL;
7542fd7af143SMichael Tuexen 	net = NULL;
7543fd7af143SMichael Tuexen 	stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
7544fd7af143SMichael Tuexen 	    (struct sockaddr *)&src,
7545fd7af143SMichael Tuexen 	    &inp, &net, 1, SCTP_DEFAULT_VRFID);
7546fd7af143SMichael Tuexen 	if ((stcb != NULL) &&
7547fd7af143SMichael Tuexen 	    (net != NULL) &&
754855b8cd93SMichael Tuexen 	    (inp != NULL)) {
7549fd7af143SMichael Tuexen 		/* Check the UDP port numbers */
7550fd7af143SMichael Tuexen 		if ((udp.uh_dport != net->port) ||
7551fd7af143SMichael Tuexen 		    (udp.uh_sport != htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)))) {
7552fd7af143SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
7553fd7af143SMichael Tuexen 			return;
7554fd7af143SMichael Tuexen 		}
7555fd7af143SMichael Tuexen 		/* Check the verification tag */
7556fd7af143SMichael Tuexen 		if (ntohl(sh.v_tag) != 0) {
7557fd7af143SMichael Tuexen 			/*
7558fd7af143SMichael Tuexen 			 * This must be the verification tag used for
7559fd7af143SMichael Tuexen 			 * sending out packets. We don't consider packets
7560fd7af143SMichael Tuexen 			 * reflecting the verification tag.
7561fd7af143SMichael Tuexen 			 */
7562fd7af143SMichael Tuexen 			if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
7563fd7af143SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
7564fd7af143SMichael Tuexen 				return;
7565fd7af143SMichael Tuexen 			}
7566fd7af143SMichael Tuexen 		} else {
7567fd7af143SMichael Tuexen 			if (ip6cp->ip6c_m->m_pkthdr.len >=
7568fd7af143SMichael Tuexen 			    ip6cp->ip6c_off + sizeof(struct udphdr) +
7569fd7af143SMichael Tuexen 			    sizeof(struct sctphdr) +
7570fd7af143SMichael Tuexen 			    sizeof(struct sctp_chunkhdr) +
7571fd7af143SMichael Tuexen 			    offsetof(struct sctp_init, a_rwnd)) {
7572fd7af143SMichael Tuexen 				/*
7573fd7af143SMichael Tuexen 				 * In this case we can check if we got an
7574fd7af143SMichael Tuexen 				 * INIT chunk and if the initiate tag
7575fd7af143SMichael Tuexen 				 * matches.
7576fd7af143SMichael Tuexen 				 */
7577fd7af143SMichael Tuexen 				uint32_t initiate_tag;
7578fd7af143SMichael Tuexen 				uint8_t chunk_type;
7579fd7af143SMichael Tuexen 
7580fd7af143SMichael Tuexen 				m_copydata(ip6cp->ip6c_m,
7581fd7af143SMichael Tuexen 				    ip6cp->ip6c_off +
7582fd7af143SMichael Tuexen 				    sizeof(struct udphdr) +
7583fd7af143SMichael Tuexen 				    sizeof(struct sctphdr),
7584fd7af143SMichael Tuexen 				    sizeof(uint8_t),
7585fd7af143SMichael Tuexen 				    (caddr_t)&chunk_type);
7586fd7af143SMichael Tuexen 				m_copydata(ip6cp->ip6c_m,
7587fd7af143SMichael Tuexen 				    ip6cp->ip6c_off +
7588fd7af143SMichael Tuexen 				    sizeof(struct udphdr) +
7589fd7af143SMichael Tuexen 				    sizeof(struct sctphdr) +
7590fd7af143SMichael Tuexen 				    sizeof(struct sctp_chunkhdr),
7591fd7af143SMichael Tuexen 				    sizeof(uint32_t),
7592fd7af143SMichael Tuexen 				    (caddr_t)&initiate_tag);
7593fd7af143SMichael Tuexen 				if ((chunk_type != SCTP_INITIATION) ||
7594fd7af143SMichael Tuexen 				    (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
7595fd7af143SMichael Tuexen 					SCTP_TCB_UNLOCK(stcb);
7596fd7af143SMichael Tuexen 					return;
7597fd7af143SMichael Tuexen 				}
7598fd7af143SMichael Tuexen 			} else {
7599fd7af143SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
7600fd7af143SMichael Tuexen 				return;
7601fd7af143SMichael Tuexen 			}
7602fd7af143SMichael Tuexen 		}
7603fd7af143SMichael Tuexen 		type = ip6cp->ip6c_icmp6->icmp6_type;
7604fd7af143SMichael Tuexen 		code = ip6cp->ip6c_icmp6->icmp6_code;
7605fd7af143SMichael Tuexen 		if ((type == ICMP6_DST_UNREACH) &&
7606fd7af143SMichael Tuexen 		    (code == ICMP6_DST_UNREACH_NOPORT)) {
7607fd7af143SMichael Tuexen 			type = ICMP6_PARAM_PROB;
7608fd7af143SMichael Tuexen 			code = ICMP6_PARAMPROB_NEXTHEADER;
7609fd7af143SMichael Tuexen 		}
7610fd7af143SMichael Tuexen 		sctp6_notify(inp, stcb, net, type, code,
76116ebfa5eeSMichael Tuexen 		    ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
7612fd7af143SMichael Tuexen 	} else {
7613fd7af143SMichael Tuexen 		if ((stcb == NULL) && (inp != NULL)) {
7614fd7af143SMichael Tuexen 			/* reduce inp's ref-count */
7615fd7af143SMichael Tuexen 			SCTP_INP_WLOCK(inp);
7616fd7af143SMichael Tuexen 			SCTP_INP_DECR_REF(inp);
7617fd7af143SMichael Tuexen 			SCTP_INP_WUNLOCK(inp);
7618fd7af143SMichael Tuexen 		}
7619fd7af143SMichael Tuexen 		if (stcb) {
7620fd7af143SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
7621fd7af143SMichael Tuexen 		}
7622fd7af143SMichael Tuexen 	}
7623fd7af143SMichael Tuexen }
7624fd7af143SMichael Tuexen #endif
7625fd7af143SMichael Tuexen 
7626c54a18d2SRandall Stewart void
7627c54a18d2SRandall Stewart sctp_over_udp_stop(void)
7628c54a18d2SRandall Stewart {
7629a99b6783SRandall Stewart 	/*
7630a99b6783SRandall Stewart 	 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
7631a99b6783SRandall Stewart 	 * for writting!
7632a99b6783SRandall Stewart 	 */
76333a51a264SMichael Tuexen #ifdef INET
76343a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
76353a51a264SMichael Tuexen 		soclose(SCTP_BASE_INFO(udp4_tun_socket));
76363a51a264SMichael Tuexen 		SCTP_BASE_INFO(udp4_tun_socket) = NULL;
7637c54a18d2SRandall Stewart 	}
76383a51a264SMichael Tuexen #endif
76393a51a264SMichael Tuexen #ifdef INET6
76403a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
76413a51a264SMichael Tuexen 		soclose(SCTP_BASE_INFO(udp6_tun_socket));
76423a51a264SMichael Tuexen 		SCTP_BASE_INFO(udp6_tun_socket) = NULL;
76433a51a264SMichael Tuexen 	}
76443a51a264SMichael Tuexen #endif
7645a99b6783SRandall Stewart }
7646ea5eba11SMichael Tuexen 
7647c54a18d2SRandall Stewart int
7648c54a18d2SRandall Stewart sctp_over_udp_start(void)
7649c54a18d2SRandall Stewart {
7650a99b6783SRandall Stewart 	uint16_t port;
7651a99b6783SRandall Stewart 	int ret;
76523a51a264SMichael Tuexen #ifdef INET
76533a51a264SMichael Tuexen 	struct sockaddr_in sin;
76543a51a264SMichael Tuexen #endif
76553a51a264SMichael Tuexen #ifdef INET6
76563a51a264SMichael Tuexen 	struct sockaddr_in6 sin6;
76573a51a264SMichael Tuexen #endif
7658a99b6783SRandall Stewart 	/*
7659a99b6783SRandall Stewart 	 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
7660a99b6783SRandall Stewart 	 * for writting!
7661a99b6783SRandall Stewart 	 */
7662a99b6783SRandall Stewart 	port = SCTP_BASE_SYSCTL(sctp_udp_tunneling_port);
76633a51a264SMichael Tuexen 	if (ntohs(port) == 0) {
7664a99b6783SRandall Stewart 		/* Must have a port set */
7665a99b6783SRandall Stewart 		return (EINVAL);
7666a99b6783SRandall Stewart 	}
76673a51a264SMichael Tuexen #ifdef INET
76683a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
7669a99b6783SRandall Stewart 		/* Already running -- must stop first */
7670a99b6783SRandall Stewart 		return (EALREADY);
7671a99b6783SRandall Stewart 	}
76723a51a264SMichael Tuexen #endif
76733a51a264SMichael Tuexen #ifdef INET6
76743a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
76753a51a264SMichael Tuexen 		/* Already running -- must stop first */
76763a51a264SMichael Tuexen 		return (EALREADY);
7677a99b6783SRandall Stewart 	}
76783a51a264SMichael Tuexen #endif
76793a51a264SMichael Tuexen #ifdef INET
76803a51a264SMichael Tuexen 	if ((ret = socreate(PF_INET, &SCTP_BASE_INFO(udp4_tun_socket),
76813a51a264SMichael Tuexen 	    SOCK_DGRAM, IPPROTO_UDP,
76823a51a264SMichael Tuexen 	    curthread->td_ucred, curthread))) {
7683a99b6783SRandall Stewart 		sctp_over_udp_stop();
7684a99b6783SRandall Stewart 		return (ret);
7685a99b6783SRandall Stewart 	}
76863a51a264SMichael Tuexen 	/* Call the special UDP hook. */
76873a51a264SMichael Tuexen 	if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp4_tun_socket),
7688fd7af143SMichael Tuexen 	    sctp_recv_udp_tunneled_packet,
7689fd7af143SMichael Tuexen 	    sctp_recv_icmp_tunneled_packet,
7690fd7af143SMichael Tuexen 	    NULL))) {
76913a51a264SMichael Tuexen 		sctp_over_udp_stop();
76923a51a264SMichael Tuexen 		return (ret);
76933a51a264SMichael Tuexen 	}
76943a51a264SMichael Tuexen 	/* Ok, we have a socket, bind it to the port. */
76953a51a264SMichael Tuexen 	memset(&sin, 0, sizeof(struct sockaddr_in));
76963a51a264SMichael Tuexen 	sin.sin_len = sizeof(struct sockaddr_in);
76973a51a264SMichael Tuexen 	sin.sin_family = AF_INET;
76983a51a264SMichael Tuexen 	sin.sin_port = htons(port);
76993a51a264SMichael Tuexen 	if ((ret = sobind(SCTP_BASE_INFO(udp4_tun_socket),
77003a51a264SMichael Tuexen 	    (struct sockaddr *)&sin, curthread))) {
77013a51a264SMichael Tuexen 		sctp_over_udp_stop();
77023a51a264SMichael Tuexen 		return (ret);
77033a51a264SMichael Tuexen 	}
77043a51a264SMichael Tuexen #endif
77053a51a264SMichael Tuexen #ifdef INET6
77063a51a264SMichael Tuexen 	if ((ret = socreate(PF_INET6, &SCTP_BASE_INFO(udp6_tun_socket),
77073a51a264SMichael Tuexen 	    SOCK_DGRAM, IPPROTO_UDP,
77083a51a264SMichael Tuexen 	    curthread->td_ucred, curthread))) {
77093a51a264SMichael Tuexen 		sctp_over_udp_stop();
77103a51a264SMichael Tuexen 		return (ret);
77113a51a264SMichael Tuexen 	}
77123a51a264SMichael Tuexen 	/* Call the special UDP hook. */
77133a51a264SMichael Tuexen 	if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp6_tun_socket),
7714fd7af143SMichael Tuexen 	    sctp_recv_udp_tunneled_packet,
7715fd7af143SMichael Tuexen 	    sctp_recv_icmp6_tunneled_packet,
7716fd7af143SMichael Tuexen 	    NULL))) {
77173a51a264SMichael Tuexen 		sctp_over_udp_stop();
77183a51a264SMichael Tuexen 		return (ret);
77193a51a264SMichael Tuexen 	}
77203a51a264SMichael Tuexen 	/* Ok, we have a socket, bind it to the port. */
77213a51a264SMichael Tuexen 	memset(&sin6, 0, sizeof(struct sockaddr_in6));
77223a51a264SMichael Tuexen 	sin6.sin6_len = sizeof(struct sockaddr_in6);
77233a51a264SMichael Tuexen 	sin6.sin6_family = AF_INET6;
77243a51a264SMichael Tuexen 	sin6.sin6_port = htons(port);
77253a51a264SMichael Tuexen 	if ((ret = sobind(SCTP_BASE_INFO(udp6_tun_socket),
77263a51a264SMichael Tuexen 	    (struct sockaddr *)&sin6, curthread))) {
77273a51a264SMichael Tuexen 		sctp_over_udp_stop();
77283a51a264SMichael Tuexen 		return (ret);
77293a51a264SMichael Tuexen 	}
77303a51a264SMichael Tuexen #endif
7731a99b6783SRandall Stewart 	return (0);
7732c54a18d2SRandall Stewart }
773310e0318aSMichael Tuexen 
773410e0318aSMichael Tuexen /*
773510e0318aSMichael Tuexen  * sctp_min_mtu ()returns the minimum of all non-zero arguments.
773610e0318aSMichael Tuexen  * If all arguments are zero, zero is returned.
773710e0318aSMichael Tuexen  */
773810e0318aSMichael Tuexen uint32_t
7739b0471b4bSMichael Tuexen sctp_min_mtu(uint32_t mtu1, uint32_t mtu2, uint32_t mtu3)
7740b0471b4bSMichael Tuexen {
774110e0318aSMichael Tuexen 	if (mtu1 > 0) {
774210e0318aSMichael Tuexen 		if (mtu2 > 0) {
774310e0318aSMichael Tuexen 			if (mtu3 > 0) {
774410e0318aSMichael Tuexen 				return (min(mtu1, min(mtu2, mtu3)));
774510e0318aSMichael Tuexen 			} else {
774610e0318aSMichael Tuexen 				return (min(mtu1, mtu2));
774710e0318aSMichael Tuexen 			}
774810e0318aSMichael Tuexen 		} else {
774910e0318aSMichael Tuexen 			if (mtu3 > 0) {
775010e0318aSMichael Tuexen 				return (min(mtu1, mtu3));
775110e0318aSMichael Tuexen 			} else {
775210e0318aSMichael Tuexen 				return (mtu1);
775310e0318aSMichael Tuexen 			}
775410e0318aSMichael Tuexen 		}
775510e0318aSMichael Tuexen 	} else {
775610e0318aSMichael Tuexen 		if (mtu2 > 0) {
775710e0318aSMichael Tuexen 			if (mtu3 > 0) {
775810e0318aSMichael Tuexen 				return (min(mtu2, mtu3));
775910e0318aSMichael Tuexen 			} else {
776010e0318aSMichael Tuexen 				return (mtu2);
776110e0318aSMichael Tuexen 			}
776210e0318aSMichael Tuexen 		} else {
776310e0318aSMichael Tuexen 			return (mtu3);
776410e0318aSMichael Tuexen 		}
776510e0318aSMichael Tuexen 	}
776610e0318aSMichael Tuexen }
776710e0318aSMichael Tuexen 
776810e0318aSMichael Tuexen void
776910e0318aSMichael Tuexen sctp_hc_set_mtu(union sctp_sockstore *addr, uint16_t fibnum, uint32_t mtu)
777010e0318aSMichael Tuexen {
777110e0318aSMichael Tuexen 	struct in_conninfo inc;
777210e0318aSMichael Tuexen 
777310e0318aSMichael Tuexen 	memset(&inc, 0, sizeof(struct in_conninfo));
777410e0318aSMichael Tuexen 	inc.inc_fibnum = fibnum;
777510e0318aSMichael Tuexen 	switch (addr->sa.sa_family) {
777610e0318aSMichael Tuexen #ifdef INET
777710e0318aSMichael Tuexen 	case AF_INET:
777810e0318aSMichael Tuexen 		inc.inc_faddr = addr->sin.sin_addr;
777910e0318aSMichael Tuexen 		break;
778010e0318aSMichael Tuexen #endif
778110e0318aSMichael Tuexen #ifdef INET6
778210e0318aSMichael Tuexen 	case AF_INET6:
778310e0318aSMichael Tuexen 		inc.inc_flags |= INC_ISIPV6;
778410e0318aSMichael Tuexen 		inc.inc6_faddr = addr->sin6.sin6_addr;
778510e0318aSMichael Tuexen 		break;
778610e0318aSMichael Tuexen #endif
778710e0318aSMichael Tuexen 	default:
778810e0318aSMichael Tuexen 		return;
778910e0318aSMichael Tuexen 	}
779010e0318aSMichael Tuexen 	tcp_hc_updatemtu(&inc, (u_long)mtu);
779110e0318aSMichael Tuexen }
779210e0318aSMichael Tuexen 
779310e0318aSMichael Tuexen uint32_t
7794b0471b4bSMichael Tuexen sctp_hc_get_mtu(union sctp_sockstore *addr, uint16_t fibnum)
7795b0471b4bSMichael Tuexen {
779610e0318aSMichael Tuexen 	struct in_conninfo inc;
779710e0318aSMichael Tuexen 
779810e0318aSMichael Tuexen 	memset(&inc, 0, sizeof(struct in_conninfo));
779910e0318aSMichael Tuexen 	inc.inc_fibnum = fibnum;
780010e0318aSMichael Tuexen 	switch (addr->sa.sa_family) {
780110e0318aSMichael Tuexen #ifdef INET
780210e0318aSMichael Tuexen 	case AF_INET:
780310e0318aSMichael Tuexen 		inc.inc_faddr = addr->sin.sin_addr;
780410e0318aSMichael Tuexen 		break;
780510e0318aSMichael Tuexen #endif
780610e0318aSMichael Tuexen #ifdef INET6
780710e0318aSMichael Tuexen 	case AF_INET6:
780810e0318aSMichael Tuexen 		inc.inc_flags |= INC_ISIPV6;
780910e0318aSMichael Tuexen 		inc.inc6_faddr = addr->sin6.sin6_addr;
781010e0318aSMichael Tuexen 		break;
781110e0318aSMichael Tuexen #endif
781210e0318aSMichael Tuexen 	default:
781310e0318aSMichael Tuexen 		return (0);
781410e0318aSMichael Tuexen 	}
781510e0318aSMichael Tuexen 	return ((uint32_t)tcp_hc_getmtu(&inc));
781610e0318aSMichael Tuexen }
78176ef849e6SMichael Tuexen 
78181a0b0216SMichael Tuexen void
78191a0b0216SMichael Tuexen sctp_set_state(struct sctp_tcb *stcb, int new_state)
78201a0b0216SMichael Tuexen {
78211e88cc8bSMichael Tuexen #if defined(KDTRACE_HOOKS)
78221e88cc8bSMichael Tuexen 	int old_state = stcb->asoc.state;
78231e88cc8bSMichael Tuexen #endif
78241e88cc8bSMichael Tuexen 
78251a0b0216SMichael Tuexen 	KASSERT((new_state & ~SCTP_STATE_MASK) == 0,
78261a0b0216SMichael Tuexen 	    ("sctp_set_state: Can't set substate (new_state = %x)",
78271a0b0216SMichael Tuexen 	    new_state));
78281a0b0216SMichael Tuexen 	stcb->asoc.state = (stcb->asoc.state & ~SCTP_STATE_MASK) | new_state;
78291a0b0216SMichael Tuexen 	if ((new_state == SCTP_STATE_SHUTDOWN_RECEIVED) ||
78301a0b0216SMichael Tuexen 	    (new_state == SCTP_STATE_SHUTDOWN_SENT) ||
78311a0b0216SMichael Tuexen 	    (new_state == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
78321a0b0216SMichael Tuexen 		SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
78331a0b0216SMichael Tuexen 	}
78341e88cc8bSMichael Tuexen #if defined(KDTRACE_HOOKS)
78351e88cc8bSMichael Tuexen 	if (((old_state & SCTP_STATE_MASK) != new_state) &&
78361e88cc8bSMichael Tuexen 	    !(((old_state & SCTP_STATE_MASK) == SCTP_STATE_EMPTY) &&
78371e88cc8bSMichael Tuexen 	    (new_state == SCTP_STATE_INUSE))) {
78381e88cc8bSMichael Tuexen 		SCTP_PROBE6(state__change, NULL, stcb, NULL, stcb, NULL, old_state);
78391e88cc8bSMichael Tuexen 	}
78401e88cc8bSMichael Tuexen #endif
78411a0b0216SMichael Tuexen }
78421a0b0216SMichael Tuexen 
78431a0b0216SMichael Tuexen void
78441a0b0216SMichael Tuexen sctp_add_substate(struct sctp_tcb *stcb, int substate)
78451a0b0216SMichael Tuexen {
78461e88cc8bSMichael Tuexen #if defined(KDTRACE_HOOKS)
78471e88cc8bSMichael Tuexen 	int old_state = stcb->asoc.state;
78481e88cc8bSMichael Tuexen #endif
78491e88cc8bSMichael Tuexen 
78501a0b0216SMichael Tuexen 	KASSERT((substate & SCTP_STATE_MASK) == 0,
78511a0b0216SMichael Tuexen 	    ("sctp_add_substate: Can't set state (substate = %x)",
78521a0b0216SMichael Tuexen 	    substate));
78531a0b0216SMichael Tuexen 	stcb->asoc.state |= substate;
78541e88cc8bSMichael Tuexen #if defined(KDTRACE_HOOKS)
78551e88cc8bSMichael Tuexen 	if (((substate & SCTP_STATE_ABOUT_TO_BE_FREED) &&
78561e88cc8bSMichael Tuexen 	    ((old_state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) ||
78571e88cc8bSMichael Tuexen 	    ((substate & SCTP_STATE_SHUTDOWN_PENDING) &&
78581e88cc8bSMichael Tuexen 	    ((old_state & SCTP_STATE_SHUTDOWN_PENDING) == 0))) {
78591e88cc8bSMichael Tuexen 		SCTP_PROBE6(state__change, NULL, stcb, NULL, stcb, NULL, old_state);
78601e88cc8bSMichael Tuexen 	}
78611e88cc8bSMichael Tuexen #endif
78621a0b0216SMichael Tuexen }
7863