xref: /freebsd/sys/netinet/sctputil.c (revision 28397ac1ed48c42b0c92635e62895015f787c6e8)
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 
65b9e7085aSRandall Stewart #ifndef KTR_SCTP
66b9e7085aSRandall Stewart #define KTR_SCTP KTR_SUBSYS
6780fefe0aSRandall Stewart #endif
68f8829a4aSRandall Stewart 
69ed654363SMichael Tuexen extern const struct sctp_cc_functions sctp_cc_functions[];
70ed654363SMichael Tuexen extern const struct sctp_ss_functions sctp_ss_functions[];
710e9a9c10SMichael Tuexen 
72f8829a4aSRandall Stewart void
73dcb68fbaSMichael Tuexen sctp_sblog(struct sockbuf *sb, struct sctp_tcb *stcb, int from, int incr)
74f8829a4aSRandall Stewart {
75c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
76c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
77f8829a4aSRandall Stewart 
7880fefe0aSRandall Stewart 	sctp_clog.x.sb.stcb = stcb;
794e88d37aSMichael Tuexen 	sctp_clog.x.sb.so_sbcc = sb->sb_cc;
80f8829a4aSRandall Stewart 	if (stcb)
814e88d37aSMichael Tuexen 		sctp_clog.x.sb.stcb_sbcc = stcb->asoc.sb_cc;
82f8829a4aSRandall Stewart 	else
8380fefe0aSRandall Stewart 		sctp_clog.x.sb.stcb_sbcc = 0;
8480fefe0aSRandall Stewart 	sctp_clog.x.sb.incr = incr;
85c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
8680fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_SB,
8780fefe0aSRandall Stewart 	    from,
8880fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
8980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
9080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
9180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
92c692df45SMichael Tuexen #endif
93f8829a4aSRandall Stewart }
94f8829a4aSRandall Stewart 
95f8829a4aSRandall Stewart void
96f8829a4aSRandall Stewart sctp_log_closing(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int16_t loc)
97f8829a4aSRandall Stewart {
98c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
99c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
100f8829a4aSRandall Stewart 
10180fefe0aSRandall Stewart 	sctp_clog.x.close.inp = (void *)inp;
10280fefe0aSRandall Stewart 	sctp_clog.x.close.sctp_flags = inp->sctp_flags;
103f8829a4aSRandall Stewart 	if (stcb) {
10480fefe0aSRandall Stewart 		sctp_clog.x.close.stcb = (void *)stcb;
10580fefe0aSRandall Stewart 		sctp_clog.x.close.state = (uint16_t)stcb->asoc.state;
106f8829a4aSRandall Stewart 	} else {
10780fefe0aSRandall Stewart 		sctp_clog.x.close.stcb = 0;
10880fefe0aSRandall Stewart 		sctp_clog.x.close.state = 0;
109f8829a4aSRandall Stewart 	}
11080fefe0aSRandall Stewart 	sctp_clog.x.close.loc = loc;
111c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
11280fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_CLOSE,
11380fefe0aSRandall Stewart 	    0,
11480fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
11580fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
11680fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
11780fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
118c692df45SMichael Tuexen #endif
119f8829a4aSRandall Stewart }
120f8829a4aSRandall Stewart 
121f8829a4aSRandall Stewart void
122f8829a4aSRandall Stewart rto_logging(struct sctp_nets *net, int from)
123f8829a4aSRandall Stewart {
124c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
125c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
126f8829a4aSRandall Stewart 
127bfefd190SRandall Stewart 	memset(&sctp_clog, 0, sizeof(sctp_clog));
12880fefe0aSRandall Stewart 	sctp_clog.x.rto.net = (void *)net;
129be1d9176SMichael Tuexen 	sctp_clog.x.rto.rtt = net->rtt / 1000;
130c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
13180fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_RTT,
13280fefe0aSRandall Stewart 	    from,
13380fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
13480fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
13580fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
13680fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
137c692df45SMichael Tuexen #endif
138f8829a4aSRandall Stewart }
139f8829a4aSRandall Stewart 
140f8829a4aSRandall Stewart void
1416a91f103SRandall Stewart sctp_log_strm_del_alt(struct sctp_tcb *stcb, uint32_t tsn, uint16_t sseq, uint16_t stream, int from)
142f8829a4aSRandall Stewart {
143c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
144c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
145f8829a4aSRandall Stewart 
14680fefe0aSRandall Stewart 	sctp_clog.x.strlog.stcb = stcb;
14780fefe0aSRandall Stewart 	sctp_clog.x.strlog.n_tsn = tsn;
14880fefe0aSRandall Stewart 	sctp_clog.x.strlog.n_sseq = sseq;
14980fefe0aSRandall Stewart 	sctp_clog.x.strlog.e_tsn = 0;
15080fefe0aSRandall Stewart 	sctp_clog.x.strlog.e_sseq = 0;
15180fefe0aSRandall Stewart 	sctp_clog.x.strlog.strm = stream;
152c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
15380fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_STRM,
15480fefe0aSRandall Stewart 	    from,
15580fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
15680fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
15780fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
15880fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
159c692df45SMichael Tuexen #endif
160f8829a4aSRandall Stewart }
161f8829a4aSRandall Stewart 
162f8829a4aSRandall Stewart void
163f8829a4aSRandall Stewart sctp_log_nagle_event(struct sctp_tcb *stcb, int action)
164f8829a4aSRandall Stewart {
165c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
166c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
167f8829a4aSRandall Stewart 
16880fefe0aSRandall Stewart 	sctp_clog.x.nagle.stcb = (void *)stcb;
16980fefe0aSRandall Stewart 	sctp_clog.x.nagle.total_flight = stcb->asoc.total_flight;
17080fefe0aSRandall Stewart 	sctp_clog.x.nagle.total_in_queue = stcb->asoc.total_output_queue_size;
17180fefe0aSRandall Stewart 	sctp_clog.x.nagle.count_in_queue = stcb->asoc.chunks_on_out_queue;
17280fefe0aSRandall Stewart 	sctp_clog.x.nagle.count_in_flight = stcb->asoc.total_flight_count;
173c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
17480fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_NAGLE,
17580fefe0aSRandall Stewart 	    action,
17680fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
17780fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
17880fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
17980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
180c692df45SMichael Tuexen #endif
181f8829a4aSRandall Stewart }
182f8829a4aSRandall Stewart 
183f8829a4aSRandall Stewart void
184f8829a4aSRandall Stewart sctp_log_sack(uint32_t old_cumack, uint32_t cumack, uint32_t tsn, uint16_t gaps, uint16_t dups, int from)
185f8829a4aSRandall Stewart {
186c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
187c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
188f8829a4aSRandall Stewart 
18980fefe0aSRandall Stewart 	sctp_clog.x.sack.cumack = cumack;
19080fefe0aSRandall Stewart 	sctp_clog.x.sack.oldcumack = old_cumack;
19180fefe0aSRandall Stewart 	sctp_clog.x.sack.tsn = tsn;
19280fefe0aSRandall Stewart 	sctp_clog.x.sack.numGaps = gaps;
19380fefe0aSRandall Stewart 	sctp_clog.x.sack.numDups = dups;
194c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
19580fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_SACK,
19680fefe0aSRandall Stewart 	    from,
19780fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
19880fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
19980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
20080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
201c692df45SMichael Tuexen #endif
202f8829a4aSRandall Stewart }
203f8829a4aSRandall Stewart 
204f8829a4aSRandall Stewart void
205f8829a4aSRandall Stewart sctp_log_map(uint32_t map, uint32_t cum, uint32_t high, int from)
206f8829a4aSRandall Stewart {
207c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
208c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
209f8829a4aSRandall Stewart 
210bfefd190SRandall Stewart 	memset(&sctp_clog, 0, sizeof(sctp_clog));
21180fefe0aSRandall Stewart 	sctp_clog.x.map.base = map;
21280fefe0aSRandall Stewart 	sctp_clog.x.map.cum = cum;
21380fefe0aSRandall Stewart 	sctp_clog.x.map.high = high;
214c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
21580fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_MAP,
21680fefe0aSRandall Stewart 	    from,
21780fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
21880fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
21980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
22080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
221c692df45SMichael Tuexen #endif
222f8829a4aSRandall Stewart }
223f8829a4aSRandall Stewart 
224f8829a4aSRandall Stewart void
225dcb68fbaSMichael Tuexen sctp_log_fr(uint32_t biggest_tsn, uint32_t biggest_new_tsn, uint32_t tsn, int from)
226f8829a4aSRandall Stewart {
227c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
228c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
229f8829a4aSRandall Stewart 
230bfefd190SRandall Stewart 	memset(&sctp_clog, 0, sizeof(sctp_clog));
23180fefe0aSRandall Stewart 	sctp_clog.x.fr.largest_tsn = biggest_tsn;
23280fefe0aSRandall Stewart 	sctp_clog.x.fr.largest_new_tsn = biggest_new_tsn;
23380fefe0aSRandall Stewart 	sctp_clog.x.fr.tsn = tsn;
234c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
23580fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_FR,
23680fefe0aSRandall Stewart 	    from,
23780fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
23880fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
23980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
24080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
241c692df45SMichael Tuexen #endif
242f8829a4aSRandall Stewart }
243f8829a4aSRandall Stewart 
2444be807c4SMichael Tuexen #ifdef SCTP_MBUF_LOGGING
245f8829a4aSRandall Stewart void
246f8829a4aSRandall Stewart sctp_log_mb(struct mbuf *m, int from)
247f8829a4aSRandall Stewart {
248c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
249c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
250f8829a4aSRandall Stewart 
25180fefe0aSRandall Stewart 	sctp_clog.x.mb.mp = m;
25280fefe0aSRandall Stewart 	sctp_clog.x.mb.mbuf_flags = (uint8_t)(SCTP_BUF_GET_FLAGS(m));
25380fefe0aSRandall Stewart 	sctp_clog.x.mb.size = (uint16_t)(SCTP_BUF_LEN(m));
25480fefe0aSRandall Stewart 	sctp_clog.x.mb.data = SCTP_BUF_AT(m, 0);
255139bc87fSRandall Stewart 	if (SCTP_BUF_IS_EXTENDED(m)) {
25680fefe0aSRandall Stewart 		sctp_clog.x.mb.ext = SCTP_BUF_EXTEND_BASE(m);
25780fefe0aSRandall Stewart 		sctp_clog.x.mb.refcnt = (uint8_t)(SCTP_BUF_EXTEND_REFCNT(m));
258f8829a4aSRandall Stewart 	} else {
25980fefe0aSRandall Stewart 		sctp_clog.x.mb.ext = 0;
26080fefe0aSRandall Stewart 		sctp_clog.x.mb.refcnt = 0;
261f8829a4aSRandall Stewart 	}
262c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
26380fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_MBUF,
26480fefe0aSRandall Stewart 	    from,
26580fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
26680fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
26780fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
26880fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
269c692df45SMichael Tuexen #endif
270f8829a4aSRandall Stewart }
271f8829a4aSRandall Stewart 
272f8829a4aSRandall Stewart void
2734be807c4SMichael Tuexen sctp_log_mbc(struct mbuf *m, int from)
2744be807c4SMichael Tuexen {
2754be807c4SMichael Tuexen 	struct mbuf *mat;
2764be807c4SMichael Tuexen 
2774be807c4SMichael Tuexen 	for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
2784be807c4SMichael Tuexen 		sctp_log_mb(mat, from);
2794be807c4SMichael Tuexen 	}
2804be807c4SMichael Tuexen }
2814be807c4SMichael Tuexen #endif
2824be807c4SMichael Tuexen 
2834be807c4SMichael Tuexen void
284dcb68fbaSMichael Tuexen sctp_log_strm_del(struct sctp_queued_to_read *control, struct sctp_queued_to_read *poschk, int from)
285f8829a4aSRandall Stewart {
286c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
287c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
288f8829a4aSRandall Stewart 
289f8829a4aSRandall Stewart 	if (control == NULL) {
290ad81507eSRandall Stewart 		SCTP_PRINTF("Gak log of NULL?\n");
291f8829a4aSRandall Stewart 		return;
292f8829a4aSRandall Stewart 	}
29380fefe0aSRandall Stewart 	sctp_clog.x.strlog.stcb = control->stcb;
29480fefe0aSRandall Stewart 	sctp_clog.x.strlog.n_tsn = control->sinfo_tsn;
29549656eefSMichael Tuexen 	sctp_clog.x.strlog.n_sseq = (uint16_t)control->mid;
29680fefe0aSRandall Stewart 	sctp_clog.x.strlog.strm = control->sinfo_stream;
297f8829a4aSRandall Stewart 	if (poschk != NULL) {
29880fefe0aSRandall Stewart 		sctp_clog.x.strlog.e_tsn = poschk->sinfo_tsn;
29949656eefSMichael Tuexen 		sctp_clog.x.strlog.e_sseq = (uint16_t)poschk->mid;
300f8829a4aSRandall Stewart 	} else {
30180fefe0aSRandall Stewart 		sctp_clog.x.strlog.e_tsn = 0;
30280fefe0aSRandall Stewart 		sctp_clog.x.strlog.e_sseq = 0;
303f8829a4aSRandall Stewart 	}
304c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
30580fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_STRM,
30680fefe0aSRandall Stewart 	    from,
30780fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
30880fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
30980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
31080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
311c692df45SMichael Tuexen #endif
312f8829a4aSRandall Stewart }
313f8829a4aSRandall Stewart 
314f8829a4aSRandall Stewart void
315f8829a4aSRandall Stewart sctp_log_cwnd(struct sctp_tcb *stcb, struct sctp_nets *net, int augment, uint8_t from)
316f8829a4aSRandall Stewart {
317c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
318c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
319f8829a4aSRandall Stewart 
32080fefe0aSRandall Stewart 	sctp_clog.x.cwnd.net = net;
321f8829a4aSRandall Stewart 	if (stcb->asoc.send_queue_cnt > 255)
32280fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_send = 255;
323f8829a4aSRandall Stewart 	else
32480fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
325f8829a4aSRandall Stewart 	if (stcb->asoc.stream_queue_cnt > 255)
32680fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_str = 255;
327f8829a4aSRandall Stewart 	else
32880fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
329f8829a4aSRandall Stewart 
330f8829a4aSRandall Stewart 	if (net) {
33180fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cwnd_new_value = net->cwnd;
33280fefe0aSRandall Stewart 		sctp_clog.x.cwnd.inflight = net->flight_size;
33380fefe0aSRandall Stewart 		sctp_clog.x.cwnd.pseudo_cumack = net->pseudo_cumack;
33480fefe0aSRandall Stewart 		sctp_clog.x.cwnd.meets_pseudo_cumack = net->new_pseudo_cumack;
33580fefe0aSRandall Stewart 		sctp_clog.x.cwnd.need_new_pseudo_cumack = net->find_pseudo_cumack;
336f8829a4aSRandall Stewart 	}
337f8829a4aSRandall Stewart 	if (SCTP_CWNDLOG_PRESEND == from) {
33880fefe0aSRandall Stewart 		sctp_clog.x.cwnd.meets_pseudo_cumack = stcb->asoc.peers_rwnd;
339f8829a4aSRandall Stewart 	}
34080fefe0aSRandall Stewart 	sctp_clog.x.cwnd.cwnd_augment = augment;
341c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
34280fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_CWND,
34380fefe0aSRandall Stewart 	    from,
34480fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
34580fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
34680fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
34780fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
348c692df45SMichael Tuexen #endif
349f8829a4aSRandall Stewart }
350f8829a4aSRandall Stewart 
351f8829a4aSRandall Stewart void
352f8829a4aSRandall Stewart sctp_log_lock(struct sctp_inpcb *inp, struct sctp_tcb *stcb, uint8_t from)
353f8829a4aSRandall Stewart {
354c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
355c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
356f8829a4aSRandall Stewart 
357bfefd190SRandall Stewart 	memset(&sctp_clog, 0, sizeof(sctp_clog));
35803b0b021SRandall Stewart 	if (inp) {
35980fefe0aSRandall Stewart 		sctp_clog.x.lock.sock = (void *)inp->sctp_socket;
36003b0b021SRandall Stewart 
36103b0b021SRandall Stewart 	} else {
36280fefe0aSRandall Stewart 		sctp_clog.x.lock.sock = (void *)NULL;
36303b0b021SRandall Stewart 	}
36480fefe0aSRandall Stewart 	sctp_clog.x.lock.inp = (void *)inp;
365f8829a4aSRandall Stewart 	if (stcb) {
36680fefe0aSRandall Stewart 		sctp_clog.x.lock.tcb_lock = mtx_owned(&stcb->tcb_mtx);
367f8829a4aSRandall Stewart 	} else {
36880fefe0aSRandall Stewart 		sctp_clog.x.lock.tcb_lock = SCTP_LOCK_UNKNOWN;
369f8829a4aSRandall Stewart 	}
370f8829a4aSRandall Stewart 	if (inp) {
37180fefe0aSRandall Stewart 		sctp_clog.x.lock.inp_lock = mtx_owned(&inp->inp_mtx);
37280fefe0aSRandall Stewart 		sctp_clog.x.lock.create_lock = mtx_owned(&inp->inp_create_mtx);
373f8829a4aSRandall Stewart 	} else {
37480fefe0aSRandall Stewart 		sctp_clog.x.lock.inp_lock = SCTP_LOCK_UNKNOWN;
37580fefe0aSRandall Stewart 		sctp_clog.x.lock.create_lock = SCTP_LOCK_UNKNOWN;
376f8829a4aSRandall Stewart 	}
377b3f1ea41SRandall Stewart 	sctp_clog.x.lock.info_lock = rw_wowned(&SCTP_BASE_INFO(ipi_ep_mtx));
37852129fcdSRandall Stewart 	if (inp && (inp->sctp_socket)) {
37980fefe0aSRandall Stewart 		sctp_clog.x.lock.sock_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
38080fefe0aSRandall Stewart 		sctp_clog.x.lock.sockrcvbuf_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
38180fefe0aSRandall Stewart 		sctp_clog.x.lock.socksndbuf_lock = mtx_owned(&(inp->sctp_socket->so_snd.sb_mtx));
382f8829a4aSRandall Stewart 	} else {
38380fefe0aSRandall Stewart 		sctp_clog.x.lock.sock_lock = SCTP_LOCK_UNKNOWN;
38480fefe0aSRandall Stewart 		sctp_clog.x.lock.sockrcvbuf_lock = SCTP_LOCK_UNKNOWN;
38580fefe0aSRandall Stewart 		sctp_clog.x.lock.socksndbuf_lock = SCTP_LOCK_UNKNOWN;
386f8829a4aSRandall Stewart 	}
387c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
38880fefe0aSRandall Stewart 	    SCTP_LOG_LOCK_EVENT,
38980fefe0aSRandall Stewart 	    from,
39080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
39180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
39280fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
39380fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
394c692df45SMichael Tuexen #endif
395f8829a4aSRandall Stewart }
396f8829a4aSRandall Stewart 
397f8829a4aSRandall Stewart void
398f8829a4aSRandall Stewart sctp_log_maxburst(struct sctp_tcb *stcb, struct sctp_nets *net, int error, int burst, uint8_t from)
399f8829a4aSRandall Stewart {
400c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
401c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
402f8829a4aSRandall Stewart 
403bfefd190SRandall Stewart 	memset(&sctp_clog, 0, sizeof(sctp_clog));
40480fefe0aSRandall Stewart 	sctp_clog.x.cwnd.net = net;
40580fefe0aSRandall Stewart 	sctp_clog.x.cwnd.cwnd_new_value = error;
40680fefe0aSRandall Stewart 	sctp_clog.x.cwnd.inflight = net->flight_size;
40780fefe0aSRandall Stewart 	sctp_clog.x.cwnd.cwnd_augment = burst;
408f8829a4aSRandall Stewart 	if (stcb->asoc.send_queue_cnt > 255)
40980fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_send = 255;
410f8829a4aSRandall Stewart 	else
41180fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
412f8829a4aSRandall Stewart 	if (stcb->asoc.stream_queue_cnt > 255)
41380fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_str = 255;
414f8829a4aSRandall Stewart 	else
41580fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
416c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
41780fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_MAXBURST,
41880fefe0aSRandall Stewart 	    from,
41980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
42080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
42180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
42280fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
423c692df45SMichael Tuexen #endif
424f8829a4aSRandall Stewart }
425f8829a4aSRandall Stewart 
426f8829a4aSRandall Stewart void
427f8829a4aSRandall Stewart sctp_log_rwnd(uint8_t from, uint32_t peers_rwnd, uint32_t snd_size, uint32_t overhead)
428f8829a4aSRandall Stewart {
429c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
430c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
431f8829a4aSRandall Stewart 
43280fefe0aSRandall Stewart 	sctp_clog.x.rwnd.rwnd = peers_rwnd;
43380fefe0aSRandall Stewart 	sctp_clog.x.rwnd.send_size = snd_size;
43480fefe0aSRandall Stewart 	sctp_clog.x.rwnd.overhead = overhead;
43580fefe0aSRandall Stewart 	sctp_clog.x.rwnd.new_rwnd = 0;
436c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
43780fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_RWND,
43880fefe0aSRandall Stewart 	    from,
43980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
44080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
44180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
44280fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
443c692df45SMichael Tuexen #endif
444f8829a4aSRandall Stewart }
445f8829a4aSRandall Stewart 
446f8829a4aSRandall Stewart void
447f8829a4aSRandall Stewart sctp_log_rwnd_set(uint8_t from, uint32_t peers_rwnd, uint32_t flight_size, uint32_t overhead, uint32_t a_rwndval)
448f8829a4aSRandall Stewart {
449c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
450c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
451f8829a4aSRandall Stewart 
45280fefe0aSRandall Stewart 	sctp_clog.x.rwnd.rwnd = peers_rwnd;
45380fefe0aSRandall Stewart 	sctp_clog.x.rwnd.send_size = flight_size;
45480fefe0aSRandall Stewart 	sctp_clog.x.rwnd.overhead = overhead;
45580fefe0aSRandall Stewart 	sctp_clog.x.rwnd.new_rwnd = a_rwndval;
456c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
45780fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_RWND,
45880fefe0aSRandall Stewart 	    from,
45980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
46080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
46180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
46280fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
463c692df45SMichael Tuexen #endif
464f8829a4aSRandall Stewart }
465f8829a4aSRandall Stewart 
4664be807c4SMichael Tuexen #ifdef SCTP_MBCNT_LOGGING
4674be807c4SMichael Tuexen static void
468f8829a4aSRandall Stewart sctp_log_mbcnt(uint8_t from, uint32_t total_oq, uint32_t book, uint32_t total_mbcnt_q, uint32_t mbcnt)
469f8829a4aSRandall Stewart {
470c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
471c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
472f8829a4aSRandall Stewart 
47380fefe0aSRandall Stewart 	sctp_clog.x.mbcnt.total_queue_size = total_oq;
47480fefe0aSRandall Stewart 	sctp_clog.x.mbcnt.size_change = book;
47580fefe0aSRandall Stewart 	sctp_clog.x.mbcnt.total_queue_mb_size = total_mbcnt_q;
47680fefe0aSRandall Stewart 	sctp_clog.x.mbcnt.mbcnt_change = mbcnt;
477c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
47880fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_MBCNT,
47980fefe0aSRandall Stewart 	    from,
48080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
48180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
48280fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
48380fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
484c692df45SMichael Tuexen #endif
485f8829a4aSRandall Stewart }
4864be807c4SMichael Tuexen #endif
4874be807c4SMichael Tuexen 
488f8829a4aSRandall Stewart void
489f8829a4aSRandall Stewart sctp_misc_ints(uint8_t from, uint32_t a, uint32_t b, uint32_t c, uint32_t d)
490f8829a4aSRandall Stewart {
491c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
492c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
49380fefe0aSRandall Stewart 	    SCTP_LOG_MISC_EVENT,
49480fefe0aSRandall Stewart 	    from,
49580fefe0aSRandall Stewart 	    a, b, c, d);
496c692df45SMichael Tuexen #endif
497f8829a4aSRandall Stewart }
498f8829a4aSRandall Stewart 
499f8829a4aSRandall Stewart void
5007215cc1bSMichael Tuexen sctp_wakeup_log(struct sctp_tcb *stcb, uint32_t wake_cnt, int from)
501f8829a4aSRandall Stewart {
502c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
503c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
504f8829a4aSRandall Stewart 
50580fefe0aSRandall Stewart 	sctp_clog.x.wake.stcb = (void *)stcb;
50680fefe0aSRandall Stewart 	sctp_clog.x.wake.wake_cnt = wake_cnt;
50780fefe0aSRandall Stewart 	sctp_clog.x.wake.flight = stcb->asoc.total_flight_count;
50880fefe0aSRandall Stewart 	sctp_clog.x.wake.send_q = stcb->asoc.send_queue_cnt;
50980fefe0aSRandall Stewart 	sctp_clog.x.wake.sent_q = stcb->asoc.sent_queue_cnt;
510f8829a4aSRandall Stewart 
511f8829a4aSRandall Stewart 	if (stcb->asoc.stream_queue_cnt < 0xff)
51280fefe0aSRandall Stewart 		sctp_clog.x.wake.stream_qcnt = (uint8_t)stcb->asoc.stream_queue_cnt;
513f8829a4aSRandall Stewart 	else
51480fefe0aSRandall Stewart 		sctp_clog.x.wake.stream_qcnt = 0xff;
515f8829a4aSRandall Stewart 
516f8829a4aSRandall Stewart 	if (stcb->asoc.chunks_on_out_queue < 0xff)
51780fefe0aSRandall Stewart 		sctp_clog.x.wake.chunks_on_oque = (uint8_t)stcb->asoc.chunks_on_out_queue;
518f8829a4aSRandall Stewart 	else
51980fefe0aSRandall Stewart 		sctp_clog.x.wake.chunks_on_oque = 0xff;
520f8829a4aSRandall Stewart 
52180fefe0aSRandall Stewart 	sctp_clog.x.wake.sctpflags = 0;
522f8829a4aSRandall Stewart 	/* set in the defered mode stuff */
523f8829a4aSRandall Stewart 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE)
52480fefe0aSRandall Stewart 		sctp_clog.x.wake.sctpflags |= 1;
525f8829a4aSRandall Stewart 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT)
52680fefe0aSRandall Stewart 		sctp_clog.x.wake.sctpflags |= 2;
527f8829a4aSRandall Stewart 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT)
52880fefe0aSRandall Stewart 		sctp_clog.x.wake.sctpflags |= 4;
529f8829a4aSRandall Stewart 	/* what about the sb */
530f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
531f8829a4aSRandall Stewart 		struct socket *so = stcb->sctp_socket;
532f8829a4aSRandall Stewart 
53380fefe0aSRandall Stewart 		sctp_clog.x.wake.sbflags = (uint8_t)((so->so_snd.sb_flags & 0x00ff));
534f8829a4aSRandall Stewart 	} else {
53580fefe0aSRandall Stewart 		sctp_clog.x.wake.sbflags = 0xff;
536f8829a4aSRandall Stewart 	}
537c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
53880fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_WAKE,
53980fefe0aSRandall Stewart 	    from,
54080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
54180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
54280fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
54380fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
544c692df45SMichael Tuexen #endif
545f8829a4aSRandall Stewart }
546f8829a4aSRandall Stewart 
547f8829a4aSRandall Stewart void
54858e6eeefSMichael Tuexen sctp_log_block(uint8_t from, struct sctp_association *asoc, ssize_t sendlen)
549f8829a4aSRandall Stewart {
550c692df45SMichael Tuexen #if defined(SCTP_LOCAL_TRACE_BUF)
551c692df45SMichael Tuexen 	struct sctp_cwnd_log sctp_clog;
552f8829a4aSRandall Stewart 
55380fefe0aSRandall Stewart 	sctp_clog.x.blk.onsb = asoc->total_output_queue_size;
55480fefe0aSRandall Stewart 	sctp_clog.x.blk.send_sent_qcnt = (uint16_t)(asoc->send_queue_cnt + asoc->sent_queue_cnt);
55580fefe0aSRandall Stewart 	sctp_clog.x.blk.peer_rwnd = asoc->peers_rwnd;
55680fefe0aSRandall Stewart 	sctp_clog.x.blk.stream_qcnt = (uint16_t)asoc->stream_queue_cnt;
55780fefe0aSRandall Stewart 	sctp_clog.x.blk.chunks_on_oque = (uint16_t)asoc->chunks_on_out_queue;
55880fefe0aSRandall Stewart 	sctp_clog.x.blk.flight_size = (uint16_t)(asoc->total_flight / 1024);
5599a8e3088SMichael Tuexen 	sctp_clog.x.blk.sndlen = (uint32_t)sendlen;
560c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
56180fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_BLOCK,
56280fefe0aSRandall Stewart 	    from,
56380fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
56480fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
56580fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
56680fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
567c692df45SMichael Tuexen #endif
568f8829a4aSRandall Stewart }
569f8829a4aSRandall Stewart 
570f8829a4aSRandall Stewart int
5717215cc1bSMichael Tuexen sctp_fill_stat_log(void *optval SCTP_UNUSED, size_t *optsize SCTP_UNUSED)
572f8829a4aSRandall Stewart {
57380fefe0aSRandall Stewart 	/* May need to fix this if ktrdump does not work */
574f8829a4aSRandall Stewart 	return (0);
575f8829a4aSRandall Stewart }
576f8829a4aSRandall Stewart 
577f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
578f8829a4aSRandall Stewart uint8_t sctp_audit_data[SCTP_AUDIT_SIZE][2];
579f8829a4aSRandall Stewart static int sctp_audit_indx = 0;
580f8829a4aSRandall Stewart 
581f8829a4aSRandall Stewart static
582f8829a4aSRandall Stewart void
583f8829a4aSRandall Stewart sctp_print_audit_report(void)
584f8829a4aSRandall Stewart {
585f8829a4aSRandall Stewart 	int i;
586f8829a4aSRandall Stewart 	int cnt;
587f8829a4aSRandall Stewart 
588f8829a4aSRandall Stewart 	cnt = 0;
589f8829a4aSRandall Stewart 	for (i = sctp_audit_indx; i < SCTP_AUDIT_SIZE; i++) {
590f8829a4aSRandall Stewart 		if ((sctp_audit_data[i][0] == 0xe0) &&
591f8829a4aSRandall Stewart 		    (sctp_audit_data[i][1] == 0x01)) {
592f8829a4aSRandall Stewart 			cnt = 0;
593ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
594f8829a4aSRandall Stewart 		} else if (sctp_audit_data[i][0] == 0xf0) {
595f8829a4aSRandall Stewart 			cnt = 0;
596ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
597f8829a4aSRandall Stewart 		} else if ((sctp_audit_data[i][0] == 0xc0) &&
598f8829a4aSRandall Stewart 		    (sctp_audit_data[i][1] == 0x01)) {
599ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
600f8829a4aSRandall Stewart 			cnt = 0;
601f8829a4aSRandall Stewart 		}
602ad81507eSRandall Stewart 		SCTP_PRINTF("%2.2x%2.2x ", (uint32_t)sctp_audit_data[i][0],
603f8829a4aSRandall Stewart 		    (uint32_t)sctp_audit_data[i][1]);
604f8829a4aSRandall Stewart 		cnt++;
605f8829a4aSRandall Stewart 		if ((cnt % 14) == 0)
606ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
607f8829a4aSRandall Stewart 	}
608f8829a4aSRandall Stewart 	for (i = 0; i < sctp_audit_indx; i++) {
609f8829a4aSRandall Stewart 		if ((sctp_audit_data[i][0] == 0xe0) &&
610f8829a4aSRandall Stewart 		    (sctp_audit_data[i][1] == 0x01)) {
611f8829a4aSRandall Stewart 			cnt = 0;
612ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
613f8829a4aSRandall Stewart 		} else if (sctp_audit_data[i][0] == 0xf0) {
614f8829a4aSRandall Stewart 			cnt = 0;
615ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
616f8829a4aSRandall Stewart 		} else if ((sctp_audit_data[i][0] == 0xc0) &&
617f8829a4aSRandall Stewart 		    (sctp_audit_data[i][1] == 0x01)) {
618ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
619f8829a4aSRandall Stewart 			cnt = 0;
620f8829a4aSRandall Stewart 		}
621ad81507eSRandall Stewart 		SCTP_PRINTF("%2.2x%2.2x ", (uint32_t)sctp_audit_data[i][0],
622f8829a4aSRandall Stewart 		    (uint32_t)sctp_audit_data[i][1]);
623f8829a4aSRandall Stewart 		cnt++;
624f8829a4aSRandall Stewart 		if ((cnt % 14) == 0)
625ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
626f8829a4aSRandall Stewart 	}
627ad81507eSRandall Stewart 	SCTP_PRINTF("\n");
628f8829a4aSRandall Stewart }
629f8829a4aSRandall Stewart 
630f8829a4aSRandall Stewart void
631f8829a4aSRandall Stewart sctp_auditing(int from, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
632f8829a4aSRandall Stewart     struct sctp_nets *net)
633f8829a4aSRandall Stewart {
634f8829a4aSRandall Stewart 	int resend_cnt, tot_out, rep, tot_book_cnt;
635f8829a4aSRandall Stewart 	struct sctp_nets *lnet;
636f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
637f8829a4aSRandall Stewart 
638f8829a4aSRandall Stewart 	sctp_audit_data[sctp_audit_indx][0] = 0xAA;
639f8829a4aSRandall Stewart 	sctp_audit_data[sctp_audit_indx][1] = 0x000000ff & from;
640f8829a4aSRandall Stewart 	sctp_audit_indx++;
641f8829a4aSRandall Stewart 	if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
642f8829a4aSRandall Stewart 		sctp_audit_indx = 0;
643f8829a4aSRandall Stewart 	}
644f8829a4aSRandall Stewart 	if (inp == NULL) {
645f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xAF;
646f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] = 0x01;
647f8829a4aSRandall Stewart 		sctp_audit_indx++;
648f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
649f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
650f8829a4aSRandall Stewart 		}
651f8829a4aSRandall Stewart 		return;
652f8829a4aSRandall Stewart 	}
653f8829a4aSRandall Stewart 	if (stcb == NULL) {
654f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xAF;
655f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] = 0x02;
656f8829a4aSRandall Stewart 		sctp_audit_indx++;
657f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
658f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
659f8829a4aSRandall Stewart 		}
660f8829a4aSRandall Stewart 		return;
661f8829a4aSRandall Stewart 	}
662f8829a4aSRandall Stewart 	sctp_audit_data[sctp_audit_indx][0] = 0xA1;
663f8829a4aSRandall Stewart 	sctp_audit_data[sctp_audit_indx][1] =
664f8829a4aSRandall Stewart 	    (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
665f8829a4aSRandall Stewart 	sctp_audit_indx++;
666f8829a4aSRandall Stewart 	if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
667f8829a4aSRandall Stewart 		sctp_audit_indx = 0;
668f8829a4aSRandall Stewart 	}
669f8829a4aSRandall Stewart 	rep = 0;
670f8829a4aSRandall Stewart 	tot_book_cnt = 0;
671f8829a4aSRandall Stewart 	resend_cnt = tot_out = 0;
672f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
673f8829a4aSRandall Stewart 		if (chk->sent == SCTP_DATAGRAM_RESEND) {
674f8829a4aSRandall Stewart 			resend_cnt++;
675f8829a4aSRandall Stewart 		} else if (chk->sent < SCTP_DATAGRAM_RESEND) {
676f8829a4aSRandall Stewart 			tot_out += chk->book_size;
677f8829a4aSRandall Stewart 			tot_book_cnt++;
678f8829a4aSRandall Stewart 		}
679f8829a4aSRandall Stewart 	}
680f8829a4aSRandall Stewart 	if (resend_cnt != stcb->asoc.sent_queue_retran_cnt) {
681f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xAF;
682f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] = 0xA1;
683f8829a4aSRandall Stewart 		sctp_audit_indx++;
684f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
685f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
686f8829a4aSRandall Stewart 		}
687ad81507eSRandall Stewart 		SCTP_PRINTF("resend_cnt:%d asoc-tot:%d\n",
688f8829a4aSRandall Stewart 		    resend_cnt, stcb->asoc.sent_queue_retran_cnt);
689f8829a4aSRandall Stewart 		rep = 1;
690f8829a4aSRandall Stewart 		stcb->asoc.sent_queue_retran_cnt = resend_cnt;
691f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xA2;
692f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] =
693f8829a4aSRandall Stewart 		    (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
694f8829a4aSRandall Stewart 		sctp_audit_indx++;
695f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
696f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
697f8829a4aSRandall Stewart 		}
698f8829a4aSRandall Stewart 	}
699f8829a4aSRandall Stewart 	if (tot_out != stcb->asoc.total_flight) {
700f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xAF;
701f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] = 0xA2;
702f8829a4aSRandall Stewart 		sctp_audit_indx++;
703f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
704f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
705f8829a4aSRandall Stewart 		}
706f8829a4aSRandall Stewart 		rep = 1;
707ad81507eSRandall Stewart 		SCTP_PRINTF("tot_flt:%d asoc_tot:%d\n", tot_out,
708f8829a4aSRandall Stewart 		    (int)stcb->asoc.total_flight);
709f8829a4aSRandall Stewart 		stcb->asoc.total_flight = tot_out;
710f8829a4aSRandall Stewart 	}
711f8829a4aSRandall Stewart 	if (tot_book_cnt != stcb->asoc.total_flight_count) {
712f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xAF;
713f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] = 0xA5;
714f8829a4aSRandall Stewart 		sctp_audit_indx++;
715f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
716f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
717f8829a4aSRandall Stewart 		}
718f8829a4aSRandall Stewart 		rep = 1;
719f31e6c7fSMichael Tuexen 		SCTP_PRINTF("tot_flt_book:%d\n", tot_book_cnt);
720f8829a4aSRandall Stewart 
721f8829a4aSRandall Stewart 		stcb->asoc.total_flight_count = tot_book_cnt;
722f8829a4aSRandall Stewart 	}
723f8829a4aSRandall Stewart 	tot_out = 0;
724f8829a4aSRandall Stewart 	TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
725f8829a4aSRandall Stewart 		tot_out += lnet->flight_size;
726f8829a4aSRandall Stewart 	}
727f8829a4aSRandall Stewart 	if (tot_out != stcb->asoc.total_flight) {
728f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xAF;
729f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] = 0xA3;
730f8829a4aSRandall Stewart 		sctp_audit_indx++;
731f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
732f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
733f8829a4aSRandall Stewart 		}
734f8829a4aSRandall Stewart 		rep = 1;
735ad81507eSRandall Stewart 		SCTP_PRINTF("real flight:%d net total was %d\n",
736f8829a4aSRandall Stewart 		    stcb->asoc.total_flight, tot_out);
737f8829a4aSRandall Stewart 		/* now corrective action */
738f8829a4aSRandall Stewart 		TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
739f8829a4aSRandall Stewart 
740f8829a4aSRandall Stewart 			tot_out = 0;
741f8829a4aSRandall Stewart 			TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
742f8829a4aSRandall Stewart 				if ((chk->whoTo == lnet) &&
743f8829a4aSRandall Stewart 				    (chk->sent < SCTP_DATAGRAM_RESEND)) {
744f8829a4aSRandall Stewart 					tot_out += chk->book_size;
745f8829a4aSRandall Stewart 				}
746f8829a4aSRandall Stewart 			}
747f8829a4aSRandall Stewart 			if (lnet->flight_size != tot_out) {
748f31e6c7fSMichael Tuexen 				SCTP_PRINTF("net:%p flight was %d corrected to %d\n",
749dd294dceSMichael Tuexen 				    (void *)lnet, lnet->flight_size,
750ad81507eSRandall Stewart 				    tot_out);
751f8829a4aSRandall Stewart 				lnet->flight_size = tot_out;
752f8829a4aSRandall Stewart 			}
753f8829a4aSRandall Stewart 		}
754f8829a4aSRandall Stewart 	}
755f8829a4aSRandall Stewart 	if (rep) {
756f8829a4aSRandall Stewart 		sctp_print_audit_report();
757f8829a4aSRandall Stewart 	}
758f8829a4aSRandall Stewart }
759f8829a4aSRandall Stewart 
760f8829a4aSRandall Stewart void
761f8829a4aSRandall Stewart sctp_audit_log(uint8_t ev, uint8_t fd)
762f8829a4aSRandall Stewart {
763f8829a4aSRandall Stewart 
764f8829a4aSRandall Stewart 	sctp_audit_data[sctp_audit_indx][0] = ev;
765f8829a4aSRandall Stewart 	sctp_audit_data[sctp_audit_indx][1] = fd;
766f8829a4aSRandall Stewart 	sctp_audit_indx++;
767f8829a4aSRandall Stewart 	if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
768f8829a4aSRandall Stewart 		sctp_audit_indx = 0;
769f8829a4aSRandall Stewart 	}
770f8829a4aSRandall Stewart }
771f8829a4aSRandall Stewart 
772f8829a4aSRandall Stewart #endif
773f8829a4aSRandall Stewart 
774f8829a4aSRandall Stewart /*
77525ec3553SMichael Tuexen  * The conversion from time to ticks and vice versa is done by rounding
77625ec3553SMichael Tuexen  * upwards. This way we can test in the code the time to be positive and
77725ec3553SMichael Tuexen  * know that this corresponds to a positive number of ticks.
77825ec3553SMichael Tuexen  */
77925ec3553SMichael Tuexen 
78025ec3553SMichael Tuexen uint32_t
78125ec3553SMichael Tuexen sctp_msecs_to_ticks(uint32_t msecs)
78225ec3553SMichael Tuexen {
78325ec3553SMichael Tuexen 	uint64_t temp;
78425ec3553SMichael Tuexen 	uint32_t ticks;
78525ec3553SMichael Tuexen 
78625ec3553SMichael Tuexen 	if (hz == 1000) {
78725ec3553SMichael Tuexen 		ticks = msecs;
78825ec3553SMichael Tuexen 	} else {
78925ec3553SMichael Tuexen 		temp = (((uint64_t)msecs * hz) + 999) / 1000;
79025ec3553SMichael Tuexen 		if (temp > UINT32_MAX) {
79125ec3553SMichael Tuexen 			ticks = UINT32_MAX;
79225ec3553SMichael Tuexen 		} else {
79325ec3553SMichael Tuexen 			ticks = (uint32_t)temp;
79425ec3553SMichael Tuexen 		}
79525ec3553SMichael Tuexen 	}
79625ec3553SMichael Tuexen 	return (ticks);
79725ec3553SMichael Tuexen }
79825ec3553SMichael Tuexen 
79925ec3553SMichael Tuexen uint32_t
80025ec3553SMichael Tuexen sctp_ticks_to_msecs(uint32_t ticks)
80125ec3553SMichael Tuexen {
80225ec3553SMichael Tuexen 	uint64_t temp;
80325ec3553SMichael Tuexen 	uint32_t msecs;
80425ec3553SMichael Tuexen 
80525ec3553SMichael Tuexen 	if (hz == 1000) {
80625ec3553SMichael Tuexen 		msecs = ticks;
80725ec3553SMichael Tuexen 	} else {
80825ec3553SMichael Tuexen 		temp = (((uint64_t)ticks * 1000) + (hz - 1)) / hz;
80925ec3553SMichael Tuexen 		if (temp > UINT32_MAX) {
81025ec3553SMichael Tuexen 			msecs = UINT32_MAX;
81125ec3553SMichael Tuexen 		} else {
81225ec3553SMichael Tuexen 			msecs = (uint32_t)temp;
81325ec3553SMichael Tuexen 		}
81425ec3553SMichael Tuexen 	}
81525ec3553SMichael Tuexen 	return (msecs);
81625ec3553SMichael Tuexen }
81725ec3553SMichael Tuexen 
81825ec3553SMichael Tuexen uint32_t
81925ec3553SMichael Tuexen sctp_secs_to_ticks(uint32_t secs)
82025ec3553SMichael Tuexen {
82125ec3553SMichael Tuexen 	uint64_t temp;
82225ec3553SMichael Tuexen 	uint32_t ticks;
82325ec3553SMichael Tuexen 
82425ec3553SMichael Tuexen 	temp = (uint64_t)secs * hz;
82525ec3553SMichael Tuexen 	if (temp > UINT32_MAX) {
82625ec3553SMichael Tuexen 		ticks = UINT32_MAX;
82725ec3553SMichael Tuexen 	} else {
82825ec3553SMichael Tuexen 		ticks = (uint32_t)temp;
82925ec3553SMichael Tuexen 	}
83025ec3553SMichael Tuexen 	return (ticks);
83125ec3553SMichael Tuexen }
83225ec3553SMichael Tuexen 
83325ec3553SMichael Tuexen uint32_t
83425ec3553SMichael Tuexen sctp_ticks_to_secs(uint32_t ticks)
83525ec3553SMichael Tuexen {
83625ec3553SMichael Tuexen 	uint64_t temp;
83725ec3553SMichael Tuexen 	uint32_t secs;
83825ec3553SMichael Tuexen 
83925ec3553SMichael Tuexen 	temp = ((uint64_t)ticks + (hz - 1)) / hz;
84025ec3553SMichael Tuexen 	if (temp > UINT32_MAX) {
84125ec3553SMichael Tuexen 		secs = UINT32_MAX;
84225ec3553SMichael Tuexen 	} else {
84325ec3553SMichael Tuexen 		secs = (uint32_t)temp;
84425ec3553SMichael Tuexen 	}
84525ec3553SMichael Tuexen 	return (secs);
84625ec3553SMichael Tuexen }
84725ec3553SMichael Tuexen 
84825ec3553SMichael Tuexen /*
84912af6654SMichael Tuexen  * sctp_stop_timers_for_shutdown() should be called
85012af6654SMichael Tuexen  * when entering the SHUTDOWN_SENT or SHUTDOWN_ACK_SENT
85112af6654SMichael Tuexen  * state to make sure that all timers are stopped.
85212af6654SMichael Tuexen  */
85312af6654SMichael Tuexen void
85412af6654SMichael Tuexen sctp_stop_timers_for_shutdown(struct sctp_tcb *stcb)
85512af6654SMichael Tuexen {
8565555400aSMichael Tuexen 	struct sctp_inpcb *inp;
85712af6654SMichael Tuexen 	struct sctp_nets *net;
85812af6654SMichael Tuexen 
8595555400aSMichael Tuexen 	inp = stcb->sctp_ep;
86012af6654SMichael Tuexen 
8615555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_RECV, inp, stcb, NULL,
8625555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_12);
8635555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, inp, stcb, NULL,
8645555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_13);
8655555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, inp, stcb, NULL,
8665555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_14);
8675555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL,
8685555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_15);
8695555400aSMichael Tuexen 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
8705555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
8715555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_16);
8725555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
8735555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_17);
8745555400aSMichael Tuexen 	}
8755555400aSMichael Tuexen }
8765555400aSMichael Tuexen 
8775555400aSMichael Tuexen void
8788803350dSMichael Tuexen sctp_stop_association_timers(struct sctp_tcb *stcb, bool stop_assoc_kill_timer)
8795555400aSMichael Tuexen {
8805555400aSMichael Tuexen 	struct sctp_inpcb *inp;
8815555400aSMichael Tuexen 	struct sctp_nets *net;
8825555400aSMichael Tuexen 
8835555400aSMichael Tuexen 	inp = stcb->sctp_ep;
8845555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_RECV, inp, stcb, NULL,
8855555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_18);
8865555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, inp, stcb, NULL,
8875555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_19);
8888803350dSMichael Tuexen 	if (stop_assoc_kill_timer) {
8895555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL,
8905555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_20);
8915555400aSMichael Tuexen 	}
8925555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, inp, stcb, NULL,
8935555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_21);
8945555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL,
8955555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_22);
8965555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNGUARD, inp, stcb, NULL,
8975555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_23);
8985555400aSMichael Tuexen 	/* Mobility adaptation */
8995555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, inp, stcb, NULL,
9005555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_24);
9015555400aSMichael Tuexen 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
9025555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
9035555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_25);
9045555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net,
9055555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_26);
9065555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, inp, stcb, net,
9075555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_27);
9085555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net,
9095555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_28);
9105555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, inp, stcb, net,
9115555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_29);
9125555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
9135555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_30);
9145555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
9155555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_31);
91612af6654SMichael Tuexen 	}
91712af6654SMichael Tuexen }
91812af6654SMichael Tuexen 
91912af6654SMichael Tuexen /*
920589c42c2SMichael Tuexen  * A list of sizes based on typical mtu's, used only if next hop size not
921589c42c2SMichael Tuexen  * returned. These values MUST be multiples of 4 and MUST be ordered.
922f8829a4aSRandall Stewart  */
923437fc91aSMichael Tuexen static uint32_t sctp_mtu_sizes[] = {
924f8829a4aSRandall Stewart 	68,
925f8829a4aSRandall Stewart 	296,
926f8829a4aSRandall Stewart 	508,
927f8829a4aSRandall Stewart 	512,
928f8829a4aSRandall Stewart 	544,
929f8829a4aSRandall Stewart 	576,
930589c42c2SMichael Tuexen 	1004,
931f8829a4aSRandall Stewart 	1492,
932f8829a4aSRandall Stewart 	1500,
933f8829a4aSRandall Stewart 	1536,
934589c42c2SMichael Tuexen 	2000,
935f8829a4aSRandall Stewart 	2048,
936f8829a4aSRandall Stewart 	4352,
937f8829a4aSRandall Stewart 	4464,
93842078d5aSMichael Tuexen 	8168,
939589c42c2SMichael Tuexen 	17912,
940f8829a4aSRandall Stewart 	32000,
941589c42c2SMichael Tuexen 	65532
942f8829a4aSRandall Stewart };
943f8829a4aSRandall Stewart 
944f8829a4aSRandall Stewart /*
945589c42c2SMichael Tuexen  * Return the largest MTU in sctp_mtu_sizes smaller than val.
946589c42c2SMichael Tuexen  * If val is smaller than the minimum, just return the largest
947589c42c2SMichael Tuexen  * multiple of 4 smaller or equal to val.
948589c42c2SMichael Tuexen  * Ensure that the result is a multiple of 4.
949f8829a4aSRandall Stewart  */
950437fc91aSMichael Tuexen uint32_t
951b0471b4bSMichael Tuexen sctp_get_prev_mtu(uint32_t val)
952b0471b4bSMichael Tuexen {
953437fc91aSMichael Tuexen 	uint32_t i;
954437fc91aSMichael Tuexen 
955eef8d4a9SMichael Tuexen 	val &= 0xfffffffc;
956437fc91aSMichael Tuexen 	if (val <= sctp_mtu_sizes[0]) {
957437fc91aSMichael Tuexen 		return (val);
958437fc91aSMichael Tuexen 	}
959437fc91aSMichael Tuexen 	for (i = 1; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
960437fc91aSMichael Tuexen 		if (val <= sctp_mtu_sizes[i]) {
961f8829a4aSRandall Stewart 			break;
962f8829a4aSRandall Stewart 		}
963f8829a4aSRandall Stewart 	}
964589c42c2SMichael Tuexen 	KASSERT((sctp_mtu_sizes[i - 1] & 0x00000003) == 0,
965589c42c2SMichael Tuexen 	    ("sctp_mtu_sizes[%u] not a multiple of 4", i - 1));
966437fc91aSMichael Tuexen 	return (sctp_mtu_sizes[i - 1]);
967437fc91aSMichael Tuexen }
968437fc91aSMichael Tuexen 
969437fc91aSMichael Tuexen /*
970589c42c2SMichael Tuexen  * Return the smallest MTU in sctp_mtu_sizes larger than val.
971589c42c2SMichael Tuexen  * If val is larger than the maximum, just return the largest multiple of 4 smaller
972589c42c2SMichael Tuexen  * or equal to val.
973589c42c2SMichael Tuexen  * Ensure that the result is a multiple of 4.
974437fc91aSMichael Tuexen  */
975437fc91aSMichael Tuexen uint32_t
976b0471b4bSMichael Tuexen sctp_get_next_mtu(uint32_t val)
977b0471b4bSMichael Tuexen {
978437fc91aSMichael Tuexen 	/* select another MTU that is just bigger than this one */
979437fc91aSMichael Tuexen 	uint32_t i;
980437fc91aSMichael Tuexen 
981eef8d4a9SMichael Tuexen 	val &= 0xfffffffc;
982437fc91aSMichael Tuexen 	for (i = 0; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
983437fc91aSMichael Tuexen 		if (val < sctp_mtu_sizes[i]) {
984589c42c2SMichael Tuexen 			KASSERT((sctp_mtu_sizes[i] & 0x00000003) == 0,
985589c42c2SMichael Tuexen 			    ("sctp_mtu_sizes[%u] not a multiple of 4", i));
986437fc91aSMichael Tuexen 			return (sctp_mtu_sizes[i]);
987437fc91aSMichael Tuexen 		}
988437fc91aSMichael Tuexen 	}
989437fc91aSMichael Tuexen 	return (val);
990f8829a4aSRandall Stewart }
991f8829a4aSRandall Stewart 
992f8829a4aSRandall Stewart void
993f8829a4aSRandall Stewart sctp_fill_random_store(struct sctp_pcb *m)
994f8829a4aSRandall Stewart {
995f8829a4aSRandall Stewart 	/*
996f8829a4aSRandall Stewart 	 * Here we use the MD5/SHA-1 to hash with our good randomNumbers and
997f8829a4aSRandall Stewart 	 * our counter. The result becomes our good random numbers and we
998f8829a4aSRandall Stewart 	 * then setup to give these out. Note that we do no locking to
999f8829a4aSRandall Stewart 	 * protect this. This is ok, since if competing folks call this we
100017205eccSRandall Stewart 	 * will get more gobbled gook in the random store which is what we
1001f8829a4aSRandall Stewart 	 * want. There is a danger that two guys will use the same random
1002f8829a4aSRandall Stewart 	 * numbers, but thats ok too since that is random as well :->
1003f8829a4aSRandall Stewart 	 */
1004f8829a4aSRandall Stewart 	m->store_at = 0;
1005ad81507eSRandall Stewart 	(void)sctp_hmac(SCTP_HMAC, (uint8_t *)m->random_numbers,
1006f8829a4aSRandall Stewart 	    sizeof(m->random_numbers), (uint8_t *)&m->random_counter,
1007f8829a4aSRandall Stewart 	    sizeof(m->random_counter), (uint8_t *)m->random_store);
1008f8829a4aSRandall Stewart 	m->random_counter++;
1009f8829a4aSRandall Stewart }
1010f8829a4aSRandall Stewart 
1011f8829a4aSRandall Stewart uint32_t
1012b0471b4bSMichael Tuexen sctp_select_initial_TSN(struct sctp_pcb *inp)
1013b0471b4bSMichael Tuexen {
1014f8829a4aSRandall Stewart 	/*
1015f8829a4aSRandall Stewart 	 * A true implementation should use random selection process to get
1016f8829a4aSRandall Stewart 	 * the initial stream sequence number, using RFC1750 as a good
1017f8829a4aSRandall Stewart 	 * guideline
1018f8829a4aSRandall Stewart 	 */
1019139bc87fSRandall Stewart 	uint32_t x, *xp;
1020f8829a4aSRandall Stewart 	uint8_t *p;
1021851b7298SRandall Stewart 	int store_at, new_store;
1022f8829a4aSRandall Stewart 
1023851b7298SRandall Stewart 	if (inp->initial_sequence_debug != 0) {
1024f8829a4aSRandall Stewart 		uint32_t ret;
1025f8829a4aSRandall Stewart 
1026851b7298SRandall Stewart 		ret = inp->initial_sequence_debug;
1027851b7298SRandall Stewart 		inp->initial_sequence_debug++;
1028f8829a4aSRandall Stewart 		return (ret);
1029f8829a4aSRandall Stewart 	}
1030851b7298SRandall Stewart retry:
1031851b7298SRandall Stewart 	store_at = inp->store_at;
1032851b7298SRandall Stewart 	new_store = store_at + sizeof(uint32_t);
1033851b7298SRandall Stewart 	if (new_store >= (SCTP_SIGNATURE_SIZE - 3)) {
1034851b7298SRandall Stewart 		new_store = 0;
1035f8829a4aSRandall Stewart 	}
1036851b7298SRandall Stewart 	if (!atomic_cmpset_int(&inp->store_at, store_at, new_store)) {
1037851b7298SRandall Stewart 		goto retry;
1038851b7298SRandall Stewart 	}
1039851b7298SRandall Stewart 	if (new_store == 0) {
1040851b7298SRandall Stewart 		/* Refill the random store */
1041851b7298SRandall Stewart 		sctp_fill_random_store(inp);
1042851b7298SRandall Stewart 	}
1043851b7298SRandall Stewart 	p = &inp->random_store[store_at];
1044139bc87fSRandall Stewart 	xp = (uint32_t *)p;
1045f8829a4aSRandall Stewart 	x = *xp;
1046f8829a4aSRandall Stewart 	return (x);
1047f8829a4aSRandall Stewart }
1048f8829a4aSRandall Stewart 
1049f8829a4aSRandall Stewart uint32_t
1050b0471b4bSMichael Tuexen sctp_select_a_tag(struct sctp_inpcb *inp, uint16_t lport, uint16_t rport, int check)
1051b0471b4bSMichael Tuexen {
10527215cc1bSMichael Tuexen 	uint32_t x;
1053f8829a4aSRandall Stewart 	struct timeval now;
1054f8829a4aSRandall Stewart 
10557215cc1bSMichael Tuexen 	if (check) {
10566e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
10577215cc1bSMichael Tuexen 	}
10587215cc1bSMichael Tuexen 	for (;;) {
1059851b7298SRandall Stewart 		x = sctp_select_initial_TSN(&inp->sctp_ep);
1060f8829a4aSRandall Stewart 		if (x == 0) {
1061f8829a4aSRandall Stewart 			/* we never use 0 */
1062f8829a4aSRandall Stewart 			continue;
1063f8829a4aSRandall Stewart 		}
10647215cc1bSMichael Tuexen 		if (!check || sctp_is_vtag_good(x, lport, rport, &now)) {
10657215cc1bSMichael Tuexen 			break;
1066f8829a4aSRandall Stewart 		}
1067f8829a4aSRandall Stewart 	}
1068f8829a4aSRandall Stewart 	return (x);
1069f8829a4aSRandall Stewart }
1070f8829a4aSRandall Stewart 
1071e92c2a8dSMichael Tuexen int32_t
1072b0471b4bSMichael Tuexen sctp_map_assoc_state(int kernel_state)
1073b0471b4bSMichael Tuexen {
1074e92c2a8dSMichael Tuexen 	int32_t user_state;
1075e92c2a8dSMichael Tuexen 
1076e92c2a8dSMichael Tuexen 	if (kernel_state & SCTP_STATE_WAS_ABORTED) {
1077e92c2a8dSMichael Tuexen 		user_state = SCTP_CLOSED;
1078e92c2a8dSMichael Tuexen 	} else if (kernel_state & SCTP_STATE_SHUTDOWN_PENDING) {
1079e92c2a8dSMichael Tuexen 		user_state = SCTP_SHUTDOWN_PENDING;
1080e92c2a8dSMichael Tuexen 	} else {
1081e92c2a8dSMichael Tuexen 		switch (kernel_state & SCTP_STATE_MASK) {
1082e92c2a8dSMichael Tuexen 		case SCTP_STATE_EMPTY:
1083e92c2a8dSMichael Tuexen 			user_state = SCTP_CLOSED;
1084e92c2a8dSMichael Tuexen 			break;
1085e92c2a8dSMichael Tuexen 		case SCTP_STATE_INUSE:
1086e92c2a8dSMichael Tuexen 			user_state = SCTP_CLOSED;
1087e92c2a8dSMichael Tuexen 			break;
1088e92c2a8dSMichael Tuexen 		case SCTP_STATE_COOKIE_WAIT:
1089e92c2a8dSMichael Tuexen 			user_state = SCTP_COOKIE_WAIT;
1090e92c2a8dSMichael Tuexen 			break;
1091e92c2a8dSMichael Tuexen 		case SCTP_STATE_COOKIE_ECHOED:
1092e92c2a8dSMichael Tuexen 			user_state = SCTP_COOKIE_ECHOED;
1093e92c2a8dSMichael Tuexen 			break;
1094e92c2a8dSMichael Tuexen 		case SCTP_STATE_OPEN:
1095e92c2a8dSMichael Tuexen 			user_state = SCTP_ESTABLISHED;
1096e92c2a8dSMichael Tuexen 			break;
1097e92c2a8dSMichael Tuexen 		case SCTP_STATE_SHUTDOWN_SENT:
1098e92c2a8dSMichael Tuexen 			user_state = SCTP_SHUTDOWN_SENT;
1099e92c2a8dSMichael Tuexen 			break;
1100e92c2a8dSMichael Tuexen 		case SCTP_STATE_SHUTDOWN_RECEIVED:
1101e92c2a8dSMichael Tuexen 			user_state = SCTP_SHUTDOWN_RECEIVED;
1102e92c2a8dSMichael Tuexen 			break;
1103e92c2a8dSMichael Tuexen 		case SCTP_STATE_SHUTDOWN_ACK_SENT:
1104e92c2a8dSMichael Tuexen 			user_state = SCTP_SHUTDOWN_ACK_SENT;
1105e92c2a8dSMichael Tuexen 			break;
1106e92c2a8dSMichael Tuexen 		default:
1107e92c2a8dSMichael Tuexen 			user_state = SCTP_CLOSED;
1108e92c2a8dSMichael Tuexen 			break;
1109e92c2a8dSMichael Tuexen 		}
1110e92c2a8dSMichael Tuexen 	}
1111e92c2a8dSMichael Tuexen 	return (user_state);
1112e92c2a8dSMichael Tuexen }
1113e92c2a8dSMichael Tuexen 
1114f8829a4aSRandall Stewart int
1115a1cb341bSMichael Tuexen sctp_init_asoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1116c979034bSMichael Tuexen     uint32_t override_tag, uint32_t vrf_id, uint16_t o_strms)
1117f8829a4aSRandall Stewart {
11180696e120SRandall Stewart 	struct sctp_association *asoc;
11190696e120SRandall Stewart 
1120f8829a4aSRandall Stewart 	/*
1121f8829a4aSRandall Stewart 	 * Anything set to zero is taken care of by the allocation routine's
1122f8829a4aSRandall Stewart 	 * bzero
1123f8829a4aSRandall Stewart 	 */
1124f8829a4aSRandall Stewart 
1125f8829a4aSRandall Stewart 	/*
1126f8829a4aSRandall Stewart 	 * Up front select what scoping to apply on addresses I tell my peer
1127f8829a4aSRandall Stewart 	 * Not sure what to do with these right now, we will need to come up
1128f8829a4aSRandall Stewart 	 * with a way to set them. We may need to pass them through from the
1129f8829a4aSRandall Stewart 	 * caller in the sctp_aloc_assoc() function.
1130f8829a4aSRandall Stewart 	 */
1131f8829a4aSRandall Stewart 	int i;
1132f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
1133f0396ad1SMichael Tuexen 	int j;
1134f0396ad1SMichael Tuexen #endif
1135f0396ad1SMichael Tuexen 
11360696e120SRandall Stewart 	asoc = &stcb->asoc;
1137f8829a4aSRandall Stewart 	/* init all variables to a known value. */
1138839d21d6SMichael Tuexen 	SCTP_SET_STATE(stcb, SCTP_STATE_INUSE);
1139a1cb341bSMichael Tuexen 	asoc->max_burst = inp->sctp_ep.max_burst;
1140a1cb341bSMichael Tuexen 	asoc->fr_max_burst = inp->sctp_ep.fr_max_burst;
114125ec3553SMichael Tuexen 	asoc->heart_beat_delay = sctp_ticks_to_msecs(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
1142a1cb341bSMichael Tuexen 	asoc->cookie_life = inp->sctp_ep.def_cookie_life;
1143a1cb341bSMichael Tuexen 	asoc->sctp_cmt_on_off = inp->sctp_cmt_on_off;
1144f342355aSMichael Tuexen 	asoc->ecn_supported = inp->ecn_supported;
1145dd973b0eSMichael Tuexen 	asoc->prsctp_supported = inp->prsctp_supported;
1146c79bec9cSMichael Tuexen 	asoc->auth_supported = inp->auth_supported;
1147c79bec9cSMichael Tuexen 	asoc->asconf_supported = inp->asconf_supported;
1148317e00efSMichael Tuexen 	asoc->reconfig_supported = inp->reconfig_supported;
1149caea9879SMichael Tuexen 	asoc->nrsack_supported = inp->nrsack_supported;
1150cb9b8e6fSMichael Tuexen 	asoc->pktdrop_supported = inp->pktdrop_supported;
115144249214SRandall Stewart 	asoc->idata_supported = inp->idata_supported;
1152ca85e948SMichael Tuexen 	asoc->sctp_cmt_pf = (uint8_t)0;
1153a1cb341bSMichael Tuexen 	asoc->sctp_frag_point = inp->sctp_frag_point;
1154a1cb341bSMichael Tuexen 	asoc->sctp_features = inp->sctp_features;
1155a1cb341bSMichael Tuexen 	asoc->default_dscp = inp->sctp_ep.default_dscp;
115659b6d5beSMichael Tuexen 	asoc->max_cwnd = inp->max_cwnd;
115742551e99SRandall Stewart #ifdef INET6
1158a1cb341bSMichael Tuexen 	if (inp->sctp_ep.default_flowlabel) {
1159a1cb341bSMichael Tuexen 		asoc->default_flowlabel = inp->sctp_ep.default_flowlabel;
116058bdb691SMichael Tuexen 	} else {
1161a1cb341bSMichael Tuexen 		if (inp->ip_inp.inp.inp_flags & IN6P_AUTOFLOWLABEL) {
1162a1cb341bSMichael Tuexen 			asoc->default_flowlabel = sctp_select_initial_TSN(&inp->sctp_ep);
116358bdb691SMichael Tuexen 			asoc->default_flowlabel &= 0x000fffff;
116458bdb691SMichael Tuexen 			asoc->default_flowlabel |= 0x80000000;
116558bdb691SMichael Tuexen 		} else {
1166f8829a4aSRandall Stewart 			asoc->default_flowlabel = 0;
116758bdb691SMichael Tuexen 		}
116858bdb691SMichael Tuexen 	}
1169f8829a4aSRandall Stewart #endif
11709f22f500SRandall Stewart 	asoc->sb_send_resv = 0;
1171f8829a4aSRandall Stewart 	if (override_tag) {
1172f8829a4aSRandall Stewart 		asoc->my_vtag = override_tag;
1173f8829a4aSRandall Stewart 	} else {
1174a1cb341bSMichael Tuexen 		asoc->my_vtag = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
1175f8829a4aSRandall Stewart 	}
1176de0e935bSRandall Stewart 	/* Get the nonce tags */
1177a1cb341bSMichael Tuexen 	asoc->my_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
1178a1cb341bSMichael Tuexen 	asoc->peer_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
117942551e99SRandall Stewart 	asoc->vrf_id = vrf_id;
1180de0e935bSRandall Stewart 
118118e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
118218e198d3SRandall Stewart 	asoc->tsn_in_at = 0;
118318e198d3SRandall Stewart 	asoc->tsn_out_at = 0;
118418e198d3SRandall Stewart 	asoc->tsn_in_wrapped = 0;
118518e198d3SRandall Stewart 	asoc->tsn_out_wrapped = 0;
118618e198d3SRandall Stewart 	asoc->cumack_log_at = 0;
1187b201f536SRandall Stewart 	asoc->cumack_log_atsnt = 0;
118818e198d3SRandall Stewart #endif
118918e198d3SRandall Stewart #ifdef SCTP_FS_SPEC_LOG
119018e198d3SRandall Stewart 	asoc->fs_index = 0;
119118e198d3SRandall Stewart #endif
1192f8829a4aSRandall Stewart 	asoc->refcnt = 0;
1193f8829a4aSRandall Stewart 	asoc->assoc_up_sent = 0;
1194f8829a4aSRandall Stewart 	asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number = asoc->sending_seq =
1195a1cb341bSMichael Tuexen 	    sctp_select_initial_TSN(&inp->sctp_ep);
1196c54a18d2SRandall Stewart 	asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1197f8829a4aSRandall Stewart 	/* we are optimisitic here */
1198830d754dSRandall Stewart 	asoc->peer_supports_nat = 0;
1199f8829a4aSRandall Stewart 	asoc->sent_queue_retran_cnt = 0;
1200f8829a4aSRandall Stewart 
1201f8829a4aSRandall Stewart 	/* for CMT */
12028933fa13SRandall Stewart 	asoc->last_net_cmt_send_started = NULL;
1203f8829a4aSRandall Stewart 
1204f8829a4aSRandall Stewart 	/* This will need to be adjusted */
1205f8829a4aSRandall Stewart 	asoc->last_acked_seq = asoc->init_seq_number - 1;
1206f8829a4aSRandall Stewart 	asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1207f8829a4aSRandall Stewart 	asoc->asconf_seq_in = asoc->last_acked_seq;
1208f8829a4aSRandall Stewart 
1209f8829a4aSRandall Stewart 	/* here we are different, we hold the next one we expect */
1210f8829a4aSRandall Stewart 	asoc->str_reset_seq_in = asoc->last_acked_seq + 1;
1211f8829a4aSRandall Stewart 
1212a1cb341bSMichael Tuexen 	asoc->initial_init_rto_max = inp->sctp_ep.initial_init_rto_max;
1213a1cb341bSMichael Tuexen 	asoc->initial_rto = inp->sctp_ep.initial_rto;
1214f8829a4aSRandall Stewart 
121528a6addeSMichael Tuexen 	asoc->default_mtu = inp->sctp_ep.default_mtu;
1216a1cb341bSMichael Tuexen 	asoc->max_init_times = inp->sctp_ep.max_init_times;
1217a1cb341bSMichael Tuexen 	asoc->max_send_times = inp->sctp_ep.max_send_times;
1218a1cb341bSMichael Tuexen 	asoc->def_net_failure = inp->sctp_ep.def_net_failure;
1219a1cb341bSMichael Tuexen 	asoc->def_net_pf_threshold = inp->sctp_ep.def_net_pf_threshold;
1220f8829a4aSRandall Stewart 	asoc->free_chunk_cnt = 0;
1221f8829a4aSRandall Stewart 
1222f8829a4aSRandall Stewart 	asoc->iam_blocking = 0;
1223a1cb341bSMichael Tuexen 	asoc->context = inp->sctp_context;
1224a1cb341bSMichael Tuexen 	asoc->local_strreset_support = inp->local_strreset_support;
1225a1cb341bSMichael Tuexen 	asoc->def_send = inp->def_send;
122625ec3553SMichael Tuexen 	asoc->delayed_ack = sctp_ticks_to_msecs(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1227a1cb341bSMichael Tuexen 	asoc->sack_freq = inp->sctp_ep.sctp_sack_freq;
1228f8829a4aSRandall Stewart 	asoc->pr_sctp_cnt = 0;
1229f8829a4aSRandall Stewart 	asoc->total_output_queue_size = 0;
1230f8829a4aSRandall Stewart 
1231a1cb341bSMichael Tuexen 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1232a1cb341bSMichael Tuexen 		asoc->scope.ipv6_addr_legal = 1;
1233a1cb341bSMichael Tuexen 		if (SCTP_IPV6_V6ONLY(inp) == 0) {
1234a1cb341bSMichael Tuexen 			asoc->scope.ipv4_addr_legal = 1;
1235f8829a4aSRandall Stewart 		} else {
1236a1cb341bSMichael Tuexen 			asoc->scope.ipv4_addr_legal = 0;
1237f8829a4aSRandall Stewart 		}
1238f8829a4aSRandall Stewart 	} else {
1239a1cb341bSMichael Tuexen 		asoc->scope.ipv6_addr_legal = 0;
1240a1cb341bSMichael Tuexen 		asoc->scope.ipv4_addr_legal = 1;
1241f8829a4aSRandall Stewart 	}
1242f8829a4aSRandall Stewart 
1243a1cb341bSMichael Tuexen 	asoc->my_rwnd = max(SCTP_SB_LIMIT_RCV(inp->sctp_socket), SCTP_MINIMAL_RWND);
1244a1cb341bSMichael Tuexen 	asoc->peers_rwnd = SCTP_SB_LIMIT_RCV(inp->sctp_socket);
1245f8829a4aSRandall Stewart 
1246a1cb341bSMichael Tuexen 	asoc->smallest_mtu = inp->sctp_frag_point;
1247a1cb341bSMichael Tuexen 	asoc->minrto = inp->sctp_ep.sctp_minrto;
1248a1cb341bSMichael Tuexen 	asoc->maxrto = inp->sctp_ep.sctp_maxrto;
1249f8829a4aSRandall Stewart 
1250f8829a4aSRandall Stewart 	asoc->stream_locked_on = 0;
1251f8829a4aSRandall Stewart 	asoc->ecn_echo_cnt_onq = 0;
1252f8829a4aSRandall Stewart 	asoc->stream_locked = 0;
1253f8829a4aSRandall Stewart 
125442551e99SRandall Stewart 	asoc->send_sack = 1;
125542551e99SRandall Stewart 
125642551e99SRandall Stewart 	LIST_INIT(&asoc->sctp_restricted_addrs);
125742551e99SRandall Stewart 
1258f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->nets);
1259f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->pending_reply_queue);
12602afb3e84SRandall Stewart 	TAILQ_INIT(&asoc->asconf_ack_sent);
1261f8829a4aSRandall Stewart 	/* Setup to fill the hb random cache at first HB */
1262f8829a4aSRandall Stewart 	asoc->hb_random_idx = 4;
1263f8829a4aSRandall Stewart 
1264a1cb341bSMichael Tuexen 	asoc->sctp_autoclose_ticks = inp->sctp_ep.auto_close_time;
1265f8829a4aSRandall Stewart 
1266a1cb341bSMichael Tuexen 	stcb->asoc.congestion_control_module = inp->sctp_ep.sctp_default_cc_module;
1267a1cb341bSMichael Tuexen 	stcb->asoc.cc_functions = sctp_cc_functions[inp->sctp_ep.sctp_default_cc_module];
1268b54d3a6cSRandall Stewart 
1269a1cb341bSMichael Tuexen 	stcb->asoc.stream_scheduling_module = inp->sctp_ep.sctp_default_ss_module;
1270a1cb341bSMichael Tuexen 	stcb->asoc.ss_functions = sctp_ss_functions[inp->sctp_ep.sctp_default_ss_module];
1271f7a77f6fSMichael Tuexen 
1272b54d3a6cSRandall Stewart 	/*
1273f8829a4aSRandall Stewart 	 * Now the stream parameters, here we allocate space for all streams
1274f8829a4aSRandall Stewart 	 * that we request by default.
1275f8829a4aSRandall Stewart 	 */
1276ea44232bSRandall Stewart 	asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams =
1277c979034bSMichael Tuexen 	    o_strms;
1278f8829a4aSRandall Stewart 	SCTP_MALLOC(asoc->strmout, struct sctp_stream_out *,
1279f8829a4aSRandall Stewart 	    asoc->streamoutcnt * sizeof(struct sctp_stream_out),
1280207304d4SRandall Stewart 	    SCTP_M_STRMO);
1281f8829a4aSRandall Stewart 	if (asoc->strmout == NULL) {
1282f8829a4aSRandall Stewart 		/* big trouble no memory */
1283c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1284f8829a4aSRandall Stewart 		return (ENOMEM);
1285f8829a4aSRandall Stewart 	}
1286f8829a4aSRandall Stewart 	for (i = 0; i < asoc->streamoutcnt; i++) {
1287f8829a4aSRandall Stewart 		/*
1288f8829a4aSRandall Stewart 		 * inbound side must be set to 0xffff, also NOTE when we get
1289f8829a4aSRandall Stewart 		 * the INIT-ACK back (for INIT sender) we MUST reduce the
1290f8829a4aSRandall Stewart 		 * count (streamoutcnt) but first check if we sent to any of
1291f8829a4aSRandall Stewart 		 * the upper streams that were dropped (if some were). Those
1292f8829a4aSRandall Stewart 		 * that were dropped must be notified to the upper layer as
1293f8829a4aSRandall Stewart 		 * failed to send.
1294f8829a4aSRandall Stewart 		 */
129563d5b568SMichael Tuexen 		asoc->strmout[i].next_mid_ordered = 0;
129663d5b568SMichael Tuexen 		asoc->strmout[i].next_mid_unordered = 0;
1297f8829a4aSRandall Stewart 		TAILQ_INIT(&asoc->strmout[i].outqueue);
1298325c8c46SMichael Tuexen 		asoc->strmout[i].chunks_on_queues = 0;
1299f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
1300f0396ad1SMichael Tuexen 		for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1301f0396ad1SMichael Tuexen 			asoc->strmout[i].abandoned_sent[j] = 0;
1302f0396ad1SMichael Tuexen 			asoc->strmout[i].abandoned_unsent[j] = 0;
1303f0396ad1SMichael Tuexen 		}
1304f0396ad1SMichael Tuexen #else
1305f0396ad1SMichael Tuexen 		asoc->strmout[i].abandoned_sent[0] = 0;
1306f0396ad1SMichael Tuexen 		asoc->strmout[i].abandoned_unsent[0] = 0;
1307f0396ad1SMichael Tuexen #endif
130849656eefSMichael Tuexen 		asoc->strmout[i].sid = i;
1309f8829a4aSRandall Stewart 		asoc->strmout[i].last_msg_incomplete = 0;
13107cca1775SRandall Stewart 		asoc->strmout[i].state = SCTP_STREAM_OPENING;
1311d1ea5fa9SMichael Tuexen 		asoc->ss_functions.sctp_ss_init_stream(stcb, &asoc->strmout[i], NULL);
1312f8829a4aSRandall Stewart 	}
1313f7a77f6fSMichael Tuexen 	asoc->ss_functions.sctp_ss_init(stcb, asoc, 0);
1314f7a77f6fSMichael Tuexen 
1315f8829a4aSRandall Stewart 	/* Now the mapping array */
1316f8829a4aSRandall Stewart 	asoc->mapping_array_size = SCTP_INITIAL_MAPPING_ARRAY;
1317f8829a4aSRandall Stewart 	SCTP_MALLOC(asoc->mapping_array, uint8_t *, asoc->mapping_array_size,
1318207304d4SRandall Stewart 	    SCTP_M_MAP);
1319f8829a4aSRandall Stewart 	if (asoc->mapping_array == NULL) {
1320207304d4SRandall Stewart 		SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1321c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1322f8829a4aSRandall Stewart 		return (ENOMEM);
1323f8829a4aSRandall Stewart 	}
1324f8829a4aSRandall Stewart 	memset(asoc->mapping_array, 0, asoc->mapping_array_size);
1325b5c16493SMichael Tuexen 	SCTP_MALLOC(asoc->nr_mapping_array, uint8_t *, asoc->mapping_array_size,
1326830d754dSRandall Stewart 	    SCTP_M_MAP);
1327bf1be571SRandall Stewart 	if (asoc->nr_mapping_array == NULL) {
1328bf1be571SRandall Stewart 		SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1329bf1be571SRandall Stewart 		SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1330bf1be571SRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1331bf1be571SRandall Stewart 		return (ENOMEM);
1332bf1be571SRandall Stewart 	}
1333b5c16493SMichael Tuexen 	memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
1334830d754dSRandall Stewart 
1335f8829a4aSRandall Stewart 	/* Now the init of the other outqueues */
1336f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->free_chunks);
1337f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->control_send_queue);
1338c54a18d2SRandall Stewart 	TAILQ_INIT(&asoc->asconf_send_queue);
1339f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->send_queue);
1340f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->sent_queue);
1341f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->resetHead);
1342a1cb341bSMichael Tuexen 	asoc->max_inbound_streams = inp->sctp_ep.max_open_streams_intome;
1343f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->asconf_queue);
1344f8829a4aSRandall Stewart 	/* authentication fields */
1345f8829a4aSRandall Stewart 	asoc->authinfo.random = NULL;
1346830d754dSRandall Stewart 	asoc->authinfo.active_keyid = 0;
1347f8829a4aSRandall Stewart 	asoc->authinfo.assoc_key = NULL;
1348f8829a4aSRandall Stewart 	asoc->authinfo.assoc_keyid = 0;
1349f8829a4aSRandall Stewart 	asoc->authinfo.recv_key = NULL;
1350f8829a4aSRandall Stewart 	asoc->authinfo.recv_keyid = 0;
1351f8829a4aSRandall Stewart 	LIST_INIT(&asoc->shared_keys);
1352f42a358aSRandall Stewart 	asoc->marked_retrans = 0;
1353a1cb341bSMichael Tuexen 	asoc->port = inp->sctp_ep.port;
1354f42a358aSRandall Stewart 	asoc->timoinit = 0;
1355f42a358aSRandall Stewart 	asoc->timodata = 0;
1356f42a358aSRandall Stewart 	asoc->timosack = 0;
1357f42a358aSRandall Stewart 	asoc->timoshutdown = 0;
1358f42a358aSRandall Stewart 	asoc->timoheartbeat = 0;
1359f42a358aSRandall Stewart 	asoc->timocookie = 0;
1360f42a358aSRandall Stewart 	asoc->timoshutdownack = 0;
13616e55db54SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&asoc->start_time);
13626e55db54SRandall Stewart 	asoc->discontinuity_time = asoc->start_time;
1363f0396ad1SMichael Tuexen 	for (i = 0; i < SCTP_PR_SCTP_MAX + 1; i++) {
1364f0396ad1SMichael Tuexen 		asoc->abandoned_unsent[i] = 0;
1365f0396ad1SMichael Tuexen 		asoc->abandoned_sent[i] = 0;
1366f0396ad1SMichael Tuexen 	}
1367eacc51c5SRandall Stewart 	/*
1368eacc51c5SRandall Stewart 	 * sa_ignore MEMLEAK {memory is put in the assoc mapping array and
136977acdc25SRandall Stewart 	 * freed later when the association is freed.
1370eacc51c5SRandall Stewart 	 */
1371f8829a4aSRandall Stewart 	return (0);
1372f8829a4aSRandall Stewart }
1373f8829a4aSRandall Stewart 
13740e13104dSRandall Stewart void
13750e13104dSRandall Stewart sctp_print_mapping_array(struct sctp_association *asoc)
13760e13104dSRandall Stewart {
1377aed5947cSMichael Tuexen 	unsigned int i, limit;
13780e13104dSRandall Stewart 
1379cd3fd531SMichael Tuexen 	SCTP_PRINTF("Mapping array size: %d, baseTSN: %8.8x, cumAck: %8.8x, highestTSN: (%8.8x, %8.8x).\n",
13800e13104dSRandall Stewart 	    asoc->mapping_array_size,
13810e13104dSRandall Stewart 	    asoc->mapping_array_base_tsn,
13820e13104dSRandall Stewart 	    asoc->cumulative_tsn,
1383aed5947cSMichael Tuexen 	    asoc->highest_tsn_inside_map,
1384aed5947cSMichael Tuexen 	    asoc->highest_tsn_inside_nr_map);
1385aed5947cSMichael Tuexen 	for (limit = asoc->mapping_array_size; limit > 1; limit--) {
138660990c0cSMichael Tuexen 		if (asoc->mapping_array[limit - 1] != 0) {
138777acdc25SRandall Stewart 			break;
138877acdc25SRandall Stewart 		}
138977acdc25SRandall Stewart 	}
1390cd3fd531SMichael Tuexen 	SCTP_PRINTF("Renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
139177acdc25SRandall Stewart 	for (i = 0; i < limit; i++) {
1392cd3fd531SMichael Tuexen 		SCTP_PRINTF("%2.2x%c", asoc->mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
139377acdc25SRandall Stewart 	}
1394aed5947cSMichael Tuexen 	if (limit % 16)
1395cd3fd531SMichael Tuexen 		SCTP_PRINTF("\n");
1396aed5947cSMichael Tuexen 	for (limit = asoc->mapping_array_size; limit > 1; limit--) {
1397aed5947cSMichael Tuexen 		if (asoc->nr_mapping_array[limit - 1]) {
139877acdc25SRandall Stewart 			break;
139977acdc25SRandall Stewart 		}
140077acdc25SRandall Stewart 	}
1401cd3fd531SMichael Tuexen 	SCTP_PRINTF("Non renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
140277acdc25SRandall Stewart 	for (i = 0; i < limit; i++) {
1403cd3fd531SMichael Tuexen 		SCTP_PRINTF("%2.2x%c", asoc->nr_mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
14040e13104dSRandall Stewart 	}
1405aed5947cSMichael Tuexen 	if (limit % 16)
1406cd3fd531SMichael Tuexen 		SCTP_PRINTF("\n");
14070e13104dSRandall Stewart }
14080e13104dSRandall Stewart 
1409f8829a4aSRandall Stewart int
14100696e120SRandall Stewart sctp_expand_mapping_array(struct sctp_association *asoc, uint32_t needed)
1411f8829a4aSRandall Stewart {
1412f8829a4aSRandall Stewart 	/* mapping array needs to grow */
1413b5c16493SMichael Tuexen 	uint8_t *new_array1, *new_array2;
14140696e120SRandall Stewart 	uint32_t new_size;
1415f8829a4aSRandall Stewart 
14160696e120SRandall Stewart 	new_size = asoc->mapping_array_size + ((needed + 7) / 8 + SCTP_MAPPING_ARRAY_INCR);
1417b5c16493SMichael Tuexen 	SCTP_MALLOC(new_array1, uint8_t *, new_size, SCTP_M_MAP);
1418b5c16493SMichael Tuexen 	SCTP_MALLOC(new_array2, uint8_t *, new_size, SCTP_M_MAP);
1419b5c16493SMichael Tuexen 	if ((new_array1 == NULL) || (new_array2 == NULL)) {
1420f8829a4aSRandall Stewart 		/* can't get more, forget it */
1421b5c16493SMichael Tuexen 		SCTP_PRINTF("No memory for expansion of SCTP mapping array %d\n", new_size);
1422b5c16493SMichael Tuexen 		if (new_array1) {
1423b5c16493SMichael Tuexen 			SCTP_FREE(new_array1, SCTP_M_MAP);
1424b5c16493SMichael Tuexen 		}
1425b5c16493SMichael Tuexen 		if (new_array2) {
1426b5c16493SMichael Tuexen 			SCTP_FREE(new_array2, SCTP_M_MAP);
1427b5c16493SMichael Tuexen 		}
1428f8829a4aSRandall Stewart 		return (-1);
1429f8829a4aSRandall Stewart 	}
1430b5c16493SMichael Tuexen 	memset(new_array1, 0, new_size);
1431b5c16493SMichael Tuexen 	memset(new_array2, 0, new_size);
1432b5c16493SMichael Tuexen 	memcpy(new_array1, asoc->mapping_array, asoc->mapping_array_size);
1433b5c16493SMichael Tuexen 	memcpy(new_array2, asoc->nr_mapping_array, asoc->mapping_array_size);
1434207304d4SRandall Stewart 	SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1435830d754dSRandall Stewart 	SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
1436b5c16493SMichael Tuexen 	asoc->mapping_array = new_array1;
1437b5c16493SMichael Tuexen 	asoc->nr_mapping_array = new_array2;
1438b5c16493SMichael Tuexen 	asoc->mapping_array_size = new_size;
1439830d754dSRandall Stewart 	return (0);
1440830d754dSRandall Stewart }
1441830d754dSRandall Stewart 
14428933fa13SRandall Stewart 
144342551e99SRandall Stewart static void
144442551e99SRandall Stewart sctp_iterator_work(struct sctp_iterator *it)
144542551e99SRandall Stewart {
1446868b51f2SMichael Tuexen 	struct epoch_tracker et;
1447868b51f2SMichael Tuexen 	struct sctp_inpcb *tinp;
144842551e99SRandall Stewart 	int iteration_count = 0;
144942551e99SRandall Stewart 	int inp_skip = 0;
1450ec4c19fcSRandall Stewart 	int first_in = 1;
145142551e99SRandall Stewart 
1452868b51f2SMichael Tuexen 	NET_EPOCH_ENTER(et);
1453ec4c19fcSRandall Stewart 	SCTP_INP_INFO_RLOCK();
145442551e99SRandall Stewart 	SCTP_ITERATOR_LOCK();
1455dcb436c9SMichael Tuexen 	sctp_it_ctl.cur_it = it;
1456ad81507eSRandall Stewart 	if (it->inp) {
1457ec4c19fcSRandall Stewart 		SCTP_INP_RLOCK(it->inp);
145842551e99SRandall Stewart 		SCTP_INP_DECR_REF(it->inp);
1459ad81507eSRandall Stewart 	}
146042551e99SRandall Stewart 	if (it->inp == NULL) {
146142551e99SRandall Stewart 		/* iterator is complete */
146242551e99SRandall Stewart done_with_iterator:
1463dcb436c9SMichael Tuexen 		sctp_it_ctl.cur_it = NULL;
146442551e99SRandall Stewart 		SCTP_ITERATOR_UNLOCK();
1465ec4c19fcSRandall Stewart 		SCTP_INP_INFO_RUNLOCK();
146642551e99SRandall Stewart 		if (it->function_atend != NULL) {
146742551e99SRandall Stewart 			(*it->function_atend) (it->pointer, it->val);
146842551e99SRandall Stewart 		}
1469207304d4SRandall Stewart 		SCTP_FREE(it, SCTP_M_ITER);
1470868b51f2SMichael Tuexen 		NET_EPOCH_EXIT(et);
147142551e99SRandall Stewart 		return;
147242551e99SRandall Stewart 	}
147342551e99SRandall Stewart select_a_new_ep:
1474ec4c19fcSRandall Stewart 	if (first_in) {
1475ec4c19fcSRandall Stewart 		first_in = 0;
1476ec4c19fcSRandall Stewart 	} else {
1477f7517433SRandall Stewart 		SCTP_INP_RLOCK(it->inp);
1478ec4c19fcSRandall Stewart 	}
147942551e99SRandall Stewart 	while (((it->pcb_flags) &&
148042551e99SRandall Stewart 	    ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) ||
148142551e99SRandall Stewart 	    ((it->pcb_features) &&
148242551e99SRandall Stewart 	    ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) {
148342551e99SRandall Stewart 		/* endpoint flags or features don't match, so keep looking */
148442551e99SRandall Stewart 		if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1485f7517433SRandall Stewart 			SCTP_INP_RUNLOCK(it->inp);
148642551e99SRandall Stewart 			goto done_with_iterator;
148742551e99SRandall Stewart 		}
1488ec4c19fcSRandall Stewart 		tinp = it->inp;
148942551e99SRandall Stewart 		it->inp = LIST_NEXT(it->inp, sctp_list);
149083ed5080SMichael Tuexen 		it->stcb = NULL;
1491ec4c19fcSRandall Stewart 		SCTP_INP_RUNLOCK(tinp);
149242551e99SRandall Stewart 		if (it->inp == NULL) {
149342551e99SRandall Stewart 			goto done_with_iterator;
149442551e99SRandall Stewart 		}
149542551e99SRandall Stewart 		SCTP_INP_RLOCK(it->inp);
1496f7517433SRandall Stewart 	}
149742551e99SRandall Stewart 	/* now go through each assoc which is in the desired state */
149842551e99SRandall Stewart 	if (it->done_current_ep == 0) {
149942551e99SRandall Stewart 		if (it->function_inp != NULL)
150042551e99SRandall Stewart 			inp_skip = (*it->function_inp) (it->inp, it->pointer, it->val);
150142551e99SRandall Stewart 		it->done_current_ep = 1;
150242551e99SRandall Stewart 	}
150342551e99SRandall Stewart 	if (it->stcb == NULL) {
150442551e99SRandall Stewart 		/* run the per instance function */
150542551e99SRandall Stewart 		it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
150642551e99SRandall Stewart 	}
150742551e99SRandall Stewart 	if ((inp_skip) || it->stcb == NULL) {
150842551e99SRandall Stewart 		if (it->function_inp_end != NULL) {
150942551e99SRandall Stewart 			inp_skip = (*it->function_inp_end) (it->inp,
151042551e99SRandall Stewart 			    it->pointer,
151142551e99SRandall Stewart 			    it->val);
151242551e99SRandall Stewart 		}
151342551e99SRandall Stewart 		SCTP_INP_RUNLOCK(it->inp);
151442551e99SRandall Stewart 		goto no_stcb;
151542551e99SRandall Stewart 	}
151642551e99SRandall Stewart 	while (it->stcb) {
151742551e99SRandall Stewart 		SCTP_TCB_LOCK(it->stcb);
151842551e99SRandall Stewart 		if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
151942551e99SRandall Stewart 			/* not in the right state... keep looking */
152042551e99SRandall Stewart 			SCTP_TCB_UNLOCK(it->stcb);
152142551e99SRandall Stewart 			goto next_assoc;
152242551e99SRandall Stewart 		}
152342551e99SRandall Stewart 		/* see if we have limited out the iterator loop */
152442551e99SRandall Stewart 		iteration_count++;
152542551e99SRandall Stewart 		if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) {
152642551e99SRandall Stewart 			/* Pause to let others grab the lock */
152742551e99SRandall Stewart 			atomic_add_int(&it->stcb->asoc.refcnt, 1);
152842551e99SRandall Stewart 			SCTP_TCB_UNLOCK(it->stcb);
1529c4739e2fSRandall Stewart 			SCTP_INP_INCR_REF(it->inp);
153042551e99SRandall Stewart 			SCTP_INP_RUNLOCK(it->inp);
153142551e99SRandall Stewart 			SCTP_ITERATOR_UNLOCK();
1532ec4c19fcSRandall Stewart 			SCTP_INP_INFO_RUNLOCK();
1533ec4c19fcSRandall Stewart 			SCTP_INP_INFO_RLOCK();
153442551e99SRandall Stewart 			SCTP_ITERATOR_LOCK();
1535f7517433SRandall Stewart 			if (sctp_it_ctl.iterator_flags) {
1536f7517433SRandall Stewart 				/* We won't be staying here */
1537f7517433SRandall Stewart 				SCTP_INP_DECR_REF(it->inp);
1538f7517433SRandall Stewart 				atomic_add_int(&it->stcb->asoc.refcnt, -1);
1539f7517433SRandall Stewart 				if (sctp_it_ctl.iterator_flags &
1540f7517433SRandall Stewart 				    SCTP_ITERATOR_STOP_CUR_IT) {
1541f7517433SRandall Stewart 					sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_IT;
1542f7517433SRandall Stewart 					goto done_with_iterator;
1543f7517433SRandall Stewart 				}
1544f7517433SRandall Stewart 				if (sctp_it_ctl.iterator_flags &
1545f7517433SRandall Stewart 				    SCTP_ITERATOR_STOP_CUR_INP) {
1546f7517433SRandall Stewart 					sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_INP;
1547f7517433SRandall Stewart 					goto no_stcb;
1548f7517433SRandall Stewart 				}
1549f7517433SRandall Stewart 				/* If we reach here huh? */
1550cd3fd531SMichael Tuexen 				SCTP_PRINTF("Unknown it ctl flag %x\n",
1551f7517433SRandall Stewart 				    sctp_it_ctl.iterator_flags);
1552f7517433SRandall Stewart 				sctp_it_ctl.iterator_flags = 0;
1553f7517433SRandall Stewart 			}
155442551e99SRandall Stewart 			SCTP_INP_RLOCK(it->inp);
1555c4739e2fSRandall Stewart 			SCTP_INP_DECR_REF(it->inp);
155642551e99SRandall Stewart 			SCTP_TCB_LOCK(it->stcb);
155742551e99SRandall Stewart 			atomic_add_int(&it->stcb->asoc.refcnt, -1);
155842551e99SRandall Stewart 			iteration_count = 0;
155942551e99SRandall Stewart 		}
156083ed5080SMichael Tuexen 		KASSERT(it->inp == it->stcb->sctp_ep,
156183ed5080SMichael Tuexen 		    ("%s: stcb %p does not belong to inp %p, but inp %p",
156283ed5080SMichael Tuexen 		    __func__, it->stcb, it->inp, it->stcb->sctp_ep));
15630053ed28SMichael Tuexen 
156442551e99SRandall Stewart 		/* run function on this one */
156542551e99SRandall Stewart 		(*it->function_assoc) (it->inp, it->stcb, it->pointer, it->val);
156642551e99SRandall Stewart 
156742551e99SRandall Stewart 		/*
156842551e99SRandall Stewart 		 * we lie here, it really needs to have its own type but
156942551e99SRandall Stewart 		 * first I must verify that this won't effect things :-0
157042551e99SRandall Stewart 		 */
157142551e99SRandall Stewart 		if (it->no_chunk_output == 0)
1572ceaad40aSRandall Stewart 			sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
157342551e99SRandall Stewart 
157442551e99SRandall Stewart 		SCTP_TCB_UNLOCK(it->stcb);
157542551e99SRandall Stewart next_assoc:
157642551e99SRandall Stewart 		it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
157742551e99SRandall Stewart 		if (it->stcb == NULL) {
157842551e99SRandall Stewart 			/* Run last function */
157942551e99SRandall Stewart 			if (it->function_inp_end != NULL) {
158042551e99SRandall Stewart 				inp_skip = (*it->function_inp_end) (it->inp,
158142551e99SRandall Stewart 				    it->pointer,
158242551e99SRandall Stewart 				    it->val);
158342551e99SRandall Stewart 			}
158442551e99SRandall Stewart 		}
158542551e99SRandall Stewart 	}
158642551e99SRandall Stewart 	SCTP_INP_RUNLOCK(it->inp);
158742551e99SRandall Stewart no_stcb:
158842551e99SRandall Stewart 	/* done with all assocs on this endpoint, move on to next endpoint */
158942551e99SRandall Stewart 	it->done_current_ep = 0;
159042551e99SRandall Stewart 	if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
159142551e99SRandall Stewart 		it->inp = NULL;
159242551e99SRandall Stewart 	} else {
159342551e99SRandall Stewart 		it->inp = LIST_NEXT(it->inp, sctp_list);
159442551e99SRandall Stewart 	}
159583ed5080SMichael Tuexen 	it->stcb = NULL;
159642551e99SRandall Stewart 	if (it->inp == NULL) {
159742551e99SRandall Stewart 		goto done_with_iterator;
159842551e99SRandall Stewart 	}
159942551e99SRandall Stewart 	goto select_a_new_ep;
160042551e99SRandall Stewart }
160142551e99SRandall Stewart 
160242551e99SRandall Stewart void
160342551e99SRandall Stewart sctp_iterator_worker(void)
160442551e99SRandall Stewart {
1605397b1c94SMichael Tuexen 	struct sctp_iterator *it;
160642551e99SRandall Stewart 
160742551e99SRandall Stewart 	/* This function is called with the WQ lock in place */
1608f7517433SRandall Stewart 	sctp_it_ctl.iterator_running = 1;
1609397b1c94SMichael Tuexen 	while ((it = TAILQ_FIRST(&sctp_it_ctl.iteratorhead)) != NULL) {
161042551e99SRandall Stewart 		/* now lets work on this one */
1611f7517433SRandall Stewart 		TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
161242551e99SRandall Stewart 		SCTP_IPI_ITERATOR_WQ_UNLOCK();
1613f7517433SRandall Stewart 		CURVNET_SET(it->vn);
161442551e99SRandall Stewart 		sctp_iterator_work(it);
1615f7517433SRandall Stewart 		CURVNET_RESTORE();
161642551e99SRandall Stewart 		SCTP_IPI_ITERATOR_WQ_LOCK();
16173c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
161842551e99SRandall Stewart 	}
1619f7517433SRandall Stewart 	sctp_it_ctl.iterator_running = 0;
162042551e99SRandall Stewart 	return;
162142551e99SRandall Stewart }
162242551e99SRandall Stewart 
1623f8829a4aSRandall Stewart 
1624f8829a4aSRandall Stewart static void
1625f8829a4aSRandall Stewart sctp_handle_addr_wq(void)
1626f8829a4aSRandall Stewart {
1627f8829a4aSRandall Stewart 	/* deal with the ADDR wq from the rtsock calls */
16284a9ef3f8SMichael Tuexen 	struct sctp_laddr *wi, *nwi;
162942551e99SRandall Stewart 	struct sctp_asconf_iterator *asc;
1630f8829a4aSRandall Stewart 
163142551e99SRandall Stewart 	SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
1632207304d4SRandall Stewart 	    sizeof(struct sctp_asconf_iterator), SCTP_M_ASC_IT);
163342551e99SRandall Stewart 	if (asc == NULL) {
163442551e99SRandall Stewart 		/* Try later, no memory */
1635f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
1636f8829a4aSRandall Stewart 		    (struct sctp_inpcb *)NULL,
1637f8829a4aSRandall Stewart 		    (struct sctp_tcb *)NULL,
1638f8829a4aSRandall Stewart 		    (struct sctp_nets *)NULL);
163942551e99SRandall Stewart 		return;
1640f8829a4aSRandall Stewart 	}
164142551e99SRandall Stewart 	LIST_INIT(&asc->list_of_work);
164242551e99SRandall Stewart 	asc->cnt = 0;
1643f7517433SRandall Stewart 
16444a9ef3f8SMichael Tuexen 	LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
164542551e99SRandall Stewart 		LIST_REMOVE(wi, sctp_nxt_addr);
164642551e99SRandall Stewart 		LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
164742551e99SRandall Stewart 		asc->cnt++;
1648f8829a4aSRandall Stewart 	}
1649f7517433SRandall Stewart 
165042551e99SRandall Stewart 	if (asc->cnt == 0) {
1651207304d4SRandall Stewart 		SCTP_FREE(asc, SCTP_M_ASC_IT);
165242551e99SRandall Stewart 	} else {
16532b1c7de4SMichael Tuexen 		int ret;
16542b1c7de4SMichael Tuexen 
16552b1c7de4SMichael Tuexen 		ret = sctp_initiate_iterator(sctp_asconf_iterator_ep,
16561b649582SRandall Stewart 		    sctp_asconf_iterator_stcb,
165742551e99SRandall Stewart 		    NULL,	/* No ep end for boundall */
165842551e99SRandall Stewart 		    SCTP_PCB_FLAGS_BOUNDALL,
165942551e99SRandall Stewart 		    SCTP_PCB_ANY_FEATURES,
16601b649582SRandall Stewart 		    SCTP_ASOC_ANY_STATE,
16611b649582SRandall Stewart 		    (void *)asc, 0,
16621b649582SRandall Stewart 		    sctp_asconf_iterator_end, NULL, 0);
16632b1c7de4SMichael Tuexen 		if (ret) {
16642b1c7de4SMichael Tuexen 			SCTP_PRINTF("Failed to initiate iterator for handle_addr_wq\n");
1665b7b84c0eSMichael Tuexen 			/*
1666b7b84c0eSMichael Tuexen 			 * Freeing if we are stopping or put back on the
1667b7b84c0eSMichael Tuexen 			 * addr_wq.
1668b7b84c0eSMichael Tuexen 			 */
16692b1c7de4SMichael Tuexen 			if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
16702b1c7de4SMichael Tuexen 				sctp_asconf_iterator_end(asc, 0);
16712b1c7de4SMichael Tuexen 			} else {
16722b1c7de4SMichael Tuexen 				LIST_FOREACH(wi, &asc->list_of_work, sctp_nxt_addr) {
16732b1c7de4SMichael Tuexen 					LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
16742b1c7de4SMichael Tuexen 				}
16752b1c7de4SMichael Tuexen 				SCTP_FREE(asc, SCTP_M_ASC_IT);
16762b1c7de4SMichael Tuexen 			}
16772b1c7de4SMichael Tuexen 		}
167842551e99SRandall Stewart 	}
1679f8829a4aSRandall Stewart }
1680f8829a4aSRandall Stewart 
1681a412576eSMichael Tuexen /*-
1682a412576eSMichael Tuexen  * The following table shows which pointers for the inp, stcb, or net are
1683a412576eSMichael Tuexen  * stored for each timer after it was started.
1684a412576eSMichael Tuexen  *
1685a412576eSMichael Tuexen  *|Name                         |Timer                        |inp |stcb|net |
1686a412576eSMichael Tuexen  *|-----------------------------|-----------------------------|----|----|----|
1687a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_SEND         |net->rxt_timer               |Yes |Yes |Yes |
1688a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_INIT         |net->rxt_timer               |Yes |Yes |Yes |
1689a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_RECV         |stcb->asoc.dack_timer        |Yes |Yes |No  |
1690a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_SHUTDOWN     |net->rxt_timer               |Yes |Yes |Yes |
1691a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_HEARTBEAT    |net->hb_timer                |Yes |Yes |Yes |
1692a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_COOKIE       |net->rxt_timer               |Yes |Yes |Yes |
1693a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_NEWCOOKIE    |inp->sctp_ep.signature_change|Yes |No  |No  |
1694a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_PATHMTURAISE |net->pmtu_timer              |Yes |Yes |Yes |
1695a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_SHUTDOWNACK  |net->rxt_timer               |Yes |Yes |Yes |
1696a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_ASCONF       |stcb->asoc.asconf_timer      |Yes |Yes |Yes |
1697a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_SHUTDOWNGUARD|stcb->asoc.shut_guard_timer  |Yes |Yes |No  |
1698a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_AUTOCLOSE    |stcb->asoc.autoclose_timer   |Yes |Yes |No  |
1699a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_STRRESET     |stcb->asoc.strreset_timer    |Yes |Yes |No  |
1700a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_INPKILL      |inp->sctp_ep.signature_change|Yes |No  |No  |
1701a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_ASOCKILL     |stcb->asoc.strreset_timer    |Yes |Yes |No  |
1702a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_ADDR_WQ      |SCTP_BASE_INFO(addr_wq_timer)|No  |No  |No  |
1703a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_PRIM_DELETED |stcb->asoc.delete_prim_timer |Yes |Yes |No  |
1704a412576eSMichael Tuexen  */
1705a412576eSMichael Tuexen 
1706f8829a4aSRandall Stewart void
1707f8829a4aSRandall Stewart sctp_timeout_handler(void *t)
1708f8829a4aSRandall Stewart {
1709868b51f2SMichael Tuexen 	struct epoch_tracker et;
1710a412576eSMichael Tuexen 	struct timeval tv;
1711f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
1712f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
1713f8829a4aSRandall Stewart 	struct sctp_nets *net;
1714f8829a4aSRandall Stewart 	struct sctp_timer *tmr;
1715267dbe63SMichael Tuexen 	struct mbuf *op_err;
1716548f47a8SMichael Tuexen 	int did_output;
1717fa89f692SMichael Tuexen 	int type;
1718a412576eSMichael Tuexen 	int i, secret;
1719f8829a4aSRandall Stewart 
1720f8829a4aSRandall Stewart 	tmr = (struct sctp_timer *)t;
1721f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)tmr->ep;
1722f8829a4aSRandall Stewart 	stcb = (struct sctp_tcb *)tmr->tcb;
1723f8829a4aSRandall Stewart 	net = (struct sctp_nets *)tmr->net;
17248518270eSMichael Tuexen 	CURVNET_SET((struct vnet *)tmr->vnet);
1725f8829a4aSRandall Stewart 	did_output = 1;
1726f8829a4aSRandall Stewart 
1727f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1728f8829a4aSRandall Stewart 	sctp_audit_log(0xF0, (uint8_t)tmr->type);
1729f8829a4aSRandall Stewart 	sctp_auditing(3, inp, stcb, net);
1730f8829a4aSRandall Stewart #endif
1731f8829a4aSRandall Stewart 
1732f8829a4aSRandall Stewart 	/* sanity checks... */
1733f4cb790aSMichael Tuexen 	KASSERT(tmr->self == tmr,
1734f4cb790aSMichael Tuexen 	    ("sctp_timeout_handler: tmr->self corrupted"));
1735f4cb790aSMichael Tuexen 	KASSERT(SCTP_IS_TIMER_TYPE_VALID(tmr->type),
1736f4cb790aSMichael Tuexen 	    ("sctp_timeout_handler: invalid timer type %d", tmr->type));
1737a412576eSMichael Tuexen 	type = tmr->type;
1738f4cb790aSMichael Tuexen 	KASSERT(stcb == NULL || stcb->sctp_ep == inp,
1739f4cb790aSMichael Tuexen 	    ("sctp_timeout_handler of type %d: inp = %p, stcb->sctp_ep %p",
1740f4cb790aSMichael Tuexen 	    type, stcb, stcb->sctp_ep));
1741f8829a4aSRandall Stewart 	if (inp) {
1742f8829a4aSRandall Stewart 		SCTP_INP_INCR_REF(inp);
1743f8829a4aSRandall Stewart 	}
17447c63520cSMichael Tuexen 	tmr->stopped_from = 0xa001;
1745f8829a4aSRandall Stewart 	if (stcb) {
1746c105859eSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
1747f8829a4aSRandall Stewart 		if (stcb->asoc.state == 0) {
1748c105859eSRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, -1);
1749f8829a4aSRandall Stewart 			if (inp) {
1750f8829a4aSRandall Stewart 				SCTP_INP_DECR_REF(inp);
1751f8829a4aSRandall Stewart 			}
1752a412576eSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_TIMER2,
175323e3c088SMichael Tuexen 			    "Timer type %d handler exiting due to CLOSED association.\n",
1754a412576eSMichael Tuexen 			    type);
1755e056fafdSMichael Tuexen 			CURVNET_RESTORE();
1756f8829a4aSRandall Stewart 			return;
1757f8829a4aSRandall Stewart 		}
1758f8829a4aSRandall Stewart 	}
17597c63520cSMichael Tuexen 	tmr->stopped_from = 0xa002;
176037686ccfSMichael Tuexen 	SCTPDBG(SCTP_DEBUG_TIMER2, "Timer type %d goes off.\n", type);
1761139bc87fSRandall Stewart 	if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1762f8829a4aSRandall Stewart 		if (inp) {
1763f8829a4aSRandall Stewart 			SCTP_INP_DECR_REF(inp);
1764f8829a4aSRandall Stewart 		}
1765207304d4SRandall Stewart 		if (stcb) {
1766207304d4SRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, -1);
1767207304d4SRandall Stewart 		}
1768a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
176923e3c088SMichael Tuexen 		    "Timer type %d handler exiting due to not being active.\n",
1770a412576eSMichael Tuexen 		    type);
1771e056fafdSMichael Tuexen 		CURVNET_RESTORE();
1772f8829a4aSRandall Stewart 		return;
1773f8829a4aSRandall Stewart 	}
1774a5d547adSRandall Stewart 
17757c63520cSMichael Tuexen 	tmr->stopped_from = 0xa003;
1776f8829a4aSRandall Stewart 	if (stcb) {
1777f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
177850cec919SRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, -1);
1779fa89f692SMichael Tuexen 		if ((type != SCTP_TIMER_TYPE_ASOCKILL) &&
1780b54d3a6cSRandall Stewart 		    ((stcb->asoc.state == 0) ||
1781b54d3a6cSRandall Stewart 		    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED))) {
1782b54d3a6cSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
1783b54d3a6cSRandall Stewart 			if (inp) {
1784b54d3a6cSRandall Stewart 				SCTP_INP_DECR_REF(inp);
1785b54d3a6cSRandall Stewart 			}
1786a412576eSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_TIMER2,
178723e3c088SMichael Tuexen 			    "Timer type %d handler exiting due to CLOSED association.\n",
1788a412576eSMichael Tuexen 			    type);
1789e056fafdSMichael Tuexen 			CURVNET_RESTORE();
1790b54d3a6cSRandall Stewart 			return;
1791b54d3a6cSRandall Stewart 		}
17922c62ba73SMichael Tuexen 	} else if (inp != NULL) {
17932c62ba73SMichael Tuexen 		SCTP_INP_WLOCK(inp);
17942c62ba73SMichael Tuexen 	} else {
17952c62ba73SMichael Tuexen 		SCTP_WQ_ADDR_LOCK();
1796f8829a4aSRandall Stewart 	}
179744b7479bSRandall Stewart 
179837686ccfSMichael Tuexen 	/* Record in stopped_from which timeout occurred. */
179937686ccfSMichael Tuexen 	tmr->stopped_from = type;
1800868b51f2SMichael Tuexen 	NET_EPOCH_ENTER(et);
1801f8829a4aSRandall Stewart 	/* mark as being serviced now */
180244b7479bSRandall Stewart 	if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
180344b7479bSRandall Stewart 		/*
180444b7479bSRandall Stewart 		 * Callout has been rescheduled.
180544b7479bSRandall Stewart 		 */
180644b7479bSRandall Stewart 		goto get_out;
180744b7479bSRandall Stewart 	}
180844b7479bSRandall Stewart 	if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
180944b7479bSRandall Stewart 		/*
181044b7479bSRandall Stewart 		 * Not active, so no action.
181144b7479bSRandall Stewart 		 */
181244b7479bSRandall Stewart 		goto get_out;
181344b7479bSRandall Stewart 	}
1814139bc87fSRandall Stewart 	SCTP_OS_TIMER_DEACTIVATE(&tmr->timer);
1815f8829a4aSRandall Stewart 
1816f8829a4aSRandall Stewart 	/* call the handler for the appropriate timer type */
1817fa89f692SMichael Tuexen 	switch (type) {
1818f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SEND:
1819a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1820a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1821a412576eSMichael Tuexen 		    type, inp, stcb, net));
1822f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timodata);
1823f42a358aSRandall Stewart 		stcb->asoc.timodata++;
1824f8829a4aSRandall Stewart 		stcb->asoc.num_send_timers_up--;
1825f8829a4aSRandall Stewart 		if (stcb->asoc.num_send_timers_up < 0) {
1826f8829a4aSRandall Stewart 			stcb->asoc.num_send_timers_up = 0;
1827f8829a4aSRandall Stewart 		}
1828b54d3a6cSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
182960990c0cSMichael Tuexen 		if (sctp_t3rxt_timer(inp, stcb, net)) {
1830f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1831f8829a4aSRandall Stewart 
1832f8829a4aSRandall Stewart 			goto out_decr;
1833f8829a4aSRandall Stewart 		}
1834b54d3a6cSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1835f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1836f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1837f8829a4aSRandall Stewart #endif
1838ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1839f8829a4aSRandall Stewart 		if ((stcb->asoc.num_send_timers_up == 0) &&
18404a9ef3f8SMichael Tuexen 		    (stcb->asoc.sent_queue_cnt > 0)) {
1841f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *chk;
1842f8829a4aSRandall Stewart 
1843f8829a4aSRandall Stewart 			/*
1844efd5e692SMichael Tuexen 			 * Safeguard. If there on some on the sent queue
1845f8829a4aSRandall Stewart 			 * somewhere but no timers running something is
1846f8829a4aSRandall Stewart 			 * wrong... so we start a timer on the first chunk
1847f8829a4aSRandall Stewart 			 * on the send queue on whatever net it is sent to.
1848f8829a4aSRandall Stewart 			 */
1849efd5e692SMichael Tuexen 			TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1850efd5e692SMichael Tuexen 				if (chk->whoTo != NULL) {
1851efd5e692SMichael Tuexen 					break;
1852efd5e692SMichael Tuexen 				}
1853efd5e692SMichael Tuexen 			}
1854efd5e692SMichael Tuexen 			if (chk != NULL) {
1855efd5e692SMichael Tuexen 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
1856efd5e692SMichael Tuexen 			}
1857f8829a4aSRandall Stewart 		}
1858f8829a4aSRandall Stewart 		break;
1859f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
1860a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1861a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1862a412576eSMichael Tuexen 		    type, inp, stcb, net));
1863f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoinit);
1864f42a358aSRandall Stewart 		stcb->asoc.timoinit++;
1865f8829a4aSRandall Stewart 		if (sctp_t1init_timer(inp, stcb, net)) {
1866f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1867f8829a4aSRandall Stewart 			goto out_decr;
1868f8829a4aSRandall Stewart 		}
1869f8829a4aSRandall Stewart 		/* We do output but not here */
1870f8829a4aSRandall Stewart 		did_output = 0;
1871f8829a4aSRandall Stewart 		break;
1872f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
1873a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net == NULL,
1874a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1875a412576eSMichael Tuexen 		    type, inp, stcb, net));
1876f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timosack);
1877f42a358aSRandall Stewart 		stcb->asoc.timosack++;
1878689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
1879f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1880a412576eSMichael Tuexen 		sctp_auditing(4, inp, stcb, NULL);
1881f8829a4aSRandall Stewart #endif
1882ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SACK_TMR, SCTP_SO_NOT_LOCKED);
1883f8829a4aSRandall Stewart 		break;
1884f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
1885a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1886a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1887a412576eSMichael Tuexen 		    type, inp, stcb, net));
1888a412576eSMichael Tuexen 		SCTP_STAT_INCR(sctps_timoshutdown);
1889a412576eSMichael Tuexen 		stcb->asoc.timoshutdown++;
1890f8829a4aSRandall Stewart 		if (sctp_shutdown_timer(inp, stcb, net)) {
1891f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1892f8829a4aSRandall Stewart 			goto out_decr;
1893f8829a4aSRandall Stewart 		}
1894f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1895f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1896f8829a4aSRandall Stewart #endif
1897ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_NOT_LOCKED);
1898f8829a4aSRandall Stewart 		break;
1899f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
1900a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1901a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1902a412576eSMichael Tuexen 		    type, inp, stcb, net));
1903f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoheartbeat);
1904f42a358aSRandall Stewart 		stcb->asoc.timoheartbeat++;
1905ca85e948SMichael Tuexen 		if (sctp_heartbeat_timer(inp, stcb, net)) {
1906f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1907f8829a4aSRandall Stewart 			goto out_decr;
1908f8829a4aSRandall Stewart 		}
1909f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1910ca85e948SMichael Tuexen 		sctp_auditing(4, inp, stcb, net);
1911f8829a4aSRandall Stewart #endif
1912ca85e948SMichael Tuexen 		if (!(net->dest_state & SCTP_ADDR_NOHB)) {
1913629749b6SMichael Tuexen 			sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1914ceaad40aSRandall Stewart 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_HB_TMR, SCTP_SO_NOT_LOCKED);
1915f8829a4aSRandall Stewart 		}
1916f8829a4aSRandall Stewart 		break;
1917f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
1918a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1919a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1920a412576eSMichael Tuexen 		    type, inp, stcb, net));
1921a412576eSMichael Tuexen 		SCTP_STAT_INCR(sctps_timocookie);
1922a412576eSMichael Tuexen 		stcb->asoc.timocookie++;
1923f8829a4aSRandall Stewart 		if (sctp_cookie_timer(inp, stcb, net)) {
1924f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1925f8829a4aSRandall Stewart 			goto out_decr;
1926f8829a4aSRandall Stewart 		}
1927f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1928f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1929f8829a4aSRandall Stewart #endif
1930f8829a4aSRandall Stewart 		/*
1931f8829a4aSRandall Stewart 		 * We consider T3 and Cookie timer pretty much the same with
1932f8829a4aSRandall Stewart 		 * respect to where from in chunk_output.
1933f8829a4aSRandall Stewart 		 */
1934ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1935f8829a4aSRandall Stewart 		break;
1936f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_NEWCOOKIE:
1937a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb == NULL && net == NULL,
1938a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1939a412576eSMichael Tuexen 		    type, inp, stcb, net));
1940f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timosecret);
19416e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&tv);
1942f8829a4aSRandall Stewart 		inp->sctp_ep.time_of_secret_change = tv.tv_sec;
1943f8829a4aSRandall Stewart 		inp->sctp_ep.last_secret_number =
1944f8829a4aSRandall Stewart 		    inp->sctp_ep.current_secret_number;
1945f8829a4aSRandall Stewart 		inp->sctp_ep.current_secret_number++;
1946f8829a4aSRandall Stewart 		if (inp->sctp_ep.current_secret_number >=
1947f8829a4aSRandall Stewart 		    SCTP_HOW_MANY_SECRETS) {
1948f8829a4aSRandall Stewart 			inp->sctp_ep.current_secret_number = 0;
1949f8829a4aSRandall Stewart 		}
1950f8829a4aSRandall Stewart 		secret = (int)inp->sctp_ep.current_secret_number;
1951f8829a4aSRandall Stewart 		for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1952f8829a4aSRandall Stewart 			inp->sctp_ep.secret_key[secret][i] =
1953f8829a4aSRandall Stewart 			    sctp_select_initial_TSN(&inp->sctp_ep);
1954f8829a4aSRandall Stewart 		}
19556fb7b4fbSMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
1956f8829a4aSRandall Stewart 		did_output = 0;
1957f8829a4aSRandall Stewart 		break;
1958f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
1959a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1960a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1961a412576eSMichael Tuexen 		    type, inp, stcb, net));
1962f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timopathmtu);
1963f8829a4aSRandall Stewart 		sctp_pathmtu_timer(inp, stcb, net);
1964f8829a4aSRandall Stewart 		did_output = 0;
1965f8829a4aSRandall Stewart 		break;
1966f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
1967a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1968a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1969a412576eSMichael Tuexen 		    type, inp, stcb, net));
1970f8829a4aSRandall Stewart 		if (sctp_shutdownack_timer(inp, stcb, net)) {
1971f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1972f8829a4aSRandall Stewart 			goto out_decr;
1973f8829a4aSRandall Stewart 		}
1974f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoshutdownack);
1975f42a358aSRandall Stewart 		stcb->asoc.timoshutdownack++;
1976f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1977f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1978f8829a4aSRandall Stewart #endif
1979ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_ACK_TMR, SCTP_SO_NOT_LOCKED);
1980f8829a4aSRandall Stewart 		break;
1981f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASCONF:
1982a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1983a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1984a412576eSMichael Tuexen 		    type, inp, stcb, net));
1985a412576eSMichael Tuexen 		SCTP_STAT_INCR(sctps_timoasconf);
1986f8829a4aSRandall Stewart 		if (sctp_asconf_timer(inp, stcb, net)) {
1987f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1988f8829a4aSRandall Stewart 			goto out_decr;
1989f8829a4aSRandall Stewart 		}
1990f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1991f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1992f8829a4aSRandall Stewart #endif
1993ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_ASCONF_TMR, SCTP_SO_NOT_LOCKED);
1994f8829a4aSRandall Stewart 		break;
19950554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
1996a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net == NULL,
1997a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1998a412576eSMichael Tuexen 		    type, inp, stcb, net));
19990554e01dSMichael Tuexen 		SCTP_STAT_INCR(sctps_timoshutdownguard);
20000554e01dSMichael Tuexen 		op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
20010554e01dSMichael Tuexen 		    "Shutdown guard timer expired");
20020554e01dSMichael Tuexen 		sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
20030554e01dSMichael Tuexen 		/* no need to unlock on tcb its gone */
20040554e01dSMichael Tuexen 		goto out_decr;
2005f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_AUTOCLOSE:
2006a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net == NULL,
2007a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2008a412576eSMichael Tuexen 		    type, inp, stcb, net));
2009f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoautoclose);
2010a57fb68bSMichael Tuexen 		sctp_autoclose_timer(inp, stcb);
2011ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
2012f8829a4aSRandall Stewart 		did_output = 0;
2013f8829a4aSRandall Stewart 		break;
20140554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_STRRESET:
2015a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net == NULL,
2016a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2017a412576eSMichael Tuexen 		    type, inp, stcb, net));
2018a412576eSMichael Tuexen 		SCTP_STAT_INCR(sctps_timostrmrst);
2019e95b3d7fSMichael Tuexen 		if (sctp_strreset_timer(inp, stcb)) {
20200554e01dSMichael Tuexen 			/* no need to unlock on tcb its gone */
20210554e01dSMichael Tuexen 			goto out_decr;
20220554e01dSMichael Tuexen 		}
20230554e01dSMichael Tuexen 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_TMR, SCTP_SO_NOT_LOCKED);
20240554e01dSMichael Tuexen 		break;
20250554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_INPKILL:
2026a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb == NULL && net == NULL,
2027a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2028a412576eSMichael Tuexen 		    type, inp, stcb, net));
20290554e01dSMichael Tuexen 		SCTP_STAT_INCR(sctps_timoinpkill);
20300554e01dSMichael Tuexen 		/*
20310554e01dSMichael Tuexen 		 * special case, take away our increment since WE are the
20320554e01dSMichael Tuexen 		 * killer
20330554e01dSMichael Tuexen 		 */
20340554e01dSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL,
20350554e01dSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_3);
2036a412576eSMichael Tuexen 		SCTP_INP_DECR_REF(inp);
2037a412576eSMichael Tuexen 		SCTP_INP_WUNLOCK(inp);
20380554e01dSMichael Tuexen 		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
20390554e01dSMichael Tuexen 		    SCTP_CALLED_FROM_INPKILL_TIMER);
20400554e01dSMichael Tuexen 		inp = NULL;
20410554e01dSMichael Tuexen 		goto out_no_decr;
2042f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASOCKILL:
2043a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net == NULL,
2044a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2045a412576eSMichael Tuexen 		    type, inp, stcb, net));
2046f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoassockill);
2047f8829a4aSRandall Stewart 		/* Can we free it yet? */
2048f8829a4aSRandall Stewart 		SCTP_INP_DECR_REF(inp);
2049ba785902SMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL,
2050ba785902SMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_1);
2051ba785902SMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2052ba785902SMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_2);
2053f8829a4aSRandall Stewart 		/*
2054f8829a4aSRandall Stewart 		 * free asoc, always unlocks (or destroy's) so prevent
2055f8829a4aSRandall Stewart 		 * duplicate unlock or unlock of a free mtx :-0
2056f8829a4aSRandall Stewart 		 */
2057f8829a4aSRandall Stewart 		stcb = NULL;
2058f8829a4aSRandall Stewart 		goto out_no_decr;
20590554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ADDR_WQ:
2060a412576eSMichael Tuexen 		KASSERT(inp == NULL && stcb == NULL && net == NULL,
2061a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2062a412576eSMichael Tuexen 		    type, inp, stcb, net));
20630554e01dSMichael Tuexen 		sctp_handle_addr_wq();
20640554e01dSMichael Tuexen 		break;
20650554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_PRIM_DELETED:
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_STAT_INCR(sctps_timodelprim);
2070a412576eSMichael Tuexen 		sctp_delete_prim_timer(inp, stcb);
20710554e01dSMichael Tuexen 		break;
2072f8829a4aSRandall Stewart 	default:
20737522682eSMichael Tuexen #ifdef INVARIANTS
2074a412576eSMichael Tuexen 		panic("Unknown timer type %d", type);
20757522682eSMichael Tuexen #else
20767522682eSMichael Tuexen 		goto get_out;
20777522682eSMichael Tuexen #endif
207860990c0cSMichael Tuexen 	}
2079f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
2080fa89f692SMichael Tuexen 	sctp_audit_log(0xF1, (uint8_t)type);
2081f8829a4aSRandall Stewart 	if (inp)
2082f8829a4aSRandall Stewart 		sctp_auditing(5, inp, stcb, net);
2083f8829a4aSRandall Stewart #endif
2084f8829a4aSRandall Stewart 	if ((did_output) && stcb) {
2085f8829a4aSRandall Stewart 		/*
2086f8829a4aSRandall Stewart 		 * Now we need to clean up the control chunk chain if an
2087f8829a4aSRandall Stewart 		 * ECNE is on it. It must be marked as UNSENT again so next
2088f8829a4aSRandall Stewart 		 * call will continue to send it until such time that we get
2089f8829a4aSRandall Stewart 		 * a CWR, to remove it. It is, however, less likely that we
2090f8829a4aSRandall Stewart 		 * will find a ecn echo on the chain though.
2091f8829a4aSRandall Stewart 		 */
2092f8829a4aSRandall Stewart 		sctp_fix_ecn_echo(&stcb->asoc);
2093f8829a4aSRandall Stewart 	}
209444b7479bSRandall Stewart get_out:
2095f8829a4aSRandall Stewart 	if (stcb) {
2096f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
20972c62ba73SMichael Tuexen 	} else if (inp != NULL) {
20982c62ba73SMichael Tuexen 		SCTP_INP_WUNLOCK(inp);
20992c62ba73SMichael Tuexen 	} else {
21002c62ba73SMichael Tuexen 		SCTP_WQ_ADDR_UNLOCK();
2101f8829a4aSRandall Stewart 	}
21022c62ba73SMichael Tuexen 
2103f8829a4aSRandall Stewart out_decr:
2104f8829a4aSRandall Stewart 	if (inp) {
2105f8829a4aSRandall Stewart 		SCTP_INP_DECR_REF(inp);
2106f8829a4aSRandall Stewart 	}
21070053ed28SMichael Tuexen 
2108f8829a4aSRandall Stewart out_no_decr:
210923e3c088SMichael Tuexen 	SCTPDBG(SCTP_DEBUG_TIMER2, "Timer type %d handler finished.\n", type);
21108518270eSMichael Tuexen 	CURVNET_RESTORE();
2111868b51f2SMichael Tuexen 	NET_EPOCH_EXIT(et);
2112f8829a4aSRandall Stewart }
2113f8829a4aSRandall Stewart 
2114a412576eSMichael Tuexen /*-
2115a412576eSMichael Tuexen  * The following table shows which parameters must be provided
2116a412576eSMichael Tuexen  * when calling sctp_timer_start(). For parameters not being
2117a412576eSMichael Tuexen  * provided, NULL must be used.
2118a412576eSMichael Tuexen  *
2119a412576eSMichael Tuexen  * |Name                         |inp |stcb|net |
2120a412576eSMichael Tuexen  * |-----------------------------|----|----|----|
2121a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SEND         |Yes |Yes |Yes |
2122a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_INIT         |Yes |Yes |Yes |
2123a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_RECV         |Yes |Yes |No  |
2124a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SHUTDOWN     |Yes |Yes |Yes |
2125a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_HEARTBEAT    |Yes |Yes |Yes |
2126a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_COOKIE       |Yes |Yes |Yes |
2127a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_NEWCOOKIE    |Yes |No  |No  |
2128a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_PATHMTURAISE |Yes |Yes |Yes |
2129a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SHUTDOWNACK  |Yes |Yes |Yes |
2130a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_ASCONF       |Yes |Yes |Yes |
2131a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SHUTDOWNGUARD|Yes |Yes |No  |
2132a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_AUTOCLOSE    |Yes |Yes |No  |
2133a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_STRRESET     |Yes |Yes |Yes |
2134a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_INPKILL      |Yes |No  |No  |
2135a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_ASOCKILL     |Yes |Yes |No  |
2136a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_ADDR_WQ      |No  |No  |No  |
2137a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_PRIM_DELETED |Yes |Yes |No  |
2138a412576eSMichael Tuexen  *
2139a412576eSMichael Tuexen  */
2140a412576eSMichael Tuexen 
2141ad81507eSRandall Stewart void
2142f8829a4aSRandall Stewart sctp_timer_start(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2143f8829a4aSRandall Stewart     struct sctp_nets *net)
2144f8829a4aSRandall Stewart {
2145f8829a4aSRandall Stewart 	struct sctp_timer *tmr;
2146a412576eSMichael Tuexen 	uint32_t to_ticks;
2147a412576eSMichael Tuexen 	uint32_t rndval, jitter;
2148f8829a4aSRandall Stewart 
2149f4cb790aSMichael Tuexen 	KASSERT(stcb == NULL || stcb->sctp_ep == inp,
2150f4cb790aSMichael Tuexen 	    ("sctp_timer_start of type %d: inp = %p, stcb->sctp_ep %p",
2151f4cb790aSMichael Tuexen 	    t_type, stcb, stcb->sctp_ep));
2152f8829a4aSRandall Stewart 	tmr = NULL;
2153a412576eSMichael Tuexen 	to_ticks = 0;
2154a412576eSMichael Tuexen 	if (stcb != NULL) {
2155f8829a4aSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
2156a412576eSMichael Tuexen 	} else if (inp != NULL) {
2157a412576eSMichael Tuexen 		SCTP_INP_WLOCK_ASSERT(inp);
2158a412576eSMichael Tuexen 	} else {
2159a412576eSMichael Tuexen 		SCTP_WQ_ADDR_LOCK_ASSERT();
2160a412576eSMichael Tuexen 	}
2161a412576eSMichael Tuexen 	if (stcb != NULL) {
2162a412576eSMichael Tuexen 		/*
2163a412576eSMichael Tuexen 		 * Don't restart timer on association that's about to be
2164a412576eSMichael Tuexen 		 * killed.
2165a412576eSMichael Tuexen 		 */
2166a412576eSMichael Tuexen 		if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
2167a412576eSMichael Tuexen 		    (t_type != SCTP_TIMER_TYPE_ASOCKILL)) {
2168a412576eSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_TIMER2,
216937686ccfSMichael Tuexen 			    "Timer type %d not started: inp=%p, stcb=%p, net=%p (stcb deleted).\n",
2170a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2171a412576eSMichael Tuexen 			return;
2172f8829a4aSRandall Stewart 		}
21739803f01cSMichael Tuexen 		/* Don't restart timer on net that's been removed. */
21749803f01cSMichael Tuexen 		if (net != NULL && (net->dest_state & SCTP_ADDR_BEING_DELETED)) {
2175a412576eSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_TIMER2,
217637686ccfSMichael Tuexen 			    "Timer type %d not started: inp=%p, stcb=%p, net=%p (net deleted).\n",
2177a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
21789803f01cSMichael Tuexen 			return;
21799803f01cSMichael Tuexen 		}
2180a412576eSMichael Tuexen 	}
2181f8829a4aSRandall Stewart 	switch (t_type) {
2182f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SEND:
2183a412576eSMichael Tuexen 		/* Here we use the RTO timer. */
2184a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2185a412576eSMichael Tuexen #ifdef INVARIANTS
2186a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2187a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2188a412576eSMichael Tuexen #else
2189ad81507eSRandall Stewart 			return;
2190a412576eSMichael Tuexen #endif
2191f8829a4aSRandall Stewart 		}
2192f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2193f8829a4aSRandall Stewart 		if (net->RTO == 0) {
219425ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2195f8829a4aSRandall Stewart 		} else {
219625ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
2197f8829a4aSRandall Stewart 		}
2198f8829a4aSRandall Stewart 		break;
2199f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
2200f8829a4aSRandall Stewart 		/*
2201f8829a4aSRandall Stewart 		 * Here we use the INIT timer default usually about 1
2202a412576eSMichael Tuexen 		 * second.
2203f8829a4aSRandall Stewart 		 */
2204a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2205a412576eSMichael Tuexen #ifdef INVARIANTS
2206a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2207a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2208a412576eSMichael Tuexen #else
2209ad81507eSRandall Stewart 			return;
2210a412576eSMichael Tuexen #endif
2211f8829a4aSRandall Stewart 		}
2212f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2213f8829a4aSRandall Stewart 		if (net->RTO == 0) {
221425ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2215f8829a4aSRandall Stewart 		} else {
221625ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
2217f8829a4aSRandall Stewart 		}
2218f8829a4aSRandall Stewart 		break;
2219f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
2220f8829a4aSRandall Stewart 		/*
2221a412576eSMichael Tuexen 		 * Here we use the Delayed-Ack timer value from the inp,
2222f8829a4aSRandall Stewart 		 * ususually about 200ms.
2223f8829a4aSRandall Stewart 		 */
2224a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2225a412576eSMichael Tuexen #ifdef INVARIANTS
2226a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2227a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2228a412576eSMichael Tuexen #else
2229ad81507eSRandall Stewart 			return;
2230a412576eSMichael Tuexen #endif
2231f8829a4aSRandall Stewart 		}
2232f8829a4aSRandall Stewart 		tmr = &stcb->asoc.dack_timer;
223325ec3553SMichael Tuexen 		to_ticks = sctp_msecs_to_ticks(stcb->asoc.delayed_ack);
2234f8829a4aSRandall Stewart 		break;
2235f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
2236f8829a4aSRandall Stewart 		/* Here we use the RTO of the destination. */
2237a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2238a412576eSMichael Tuexen #ifdef INVARIANTS
2239a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2240a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2241a412576eSMichael Tuexen #else
2242ad81507eSRandall Stewart 			return;
2243a412576eSMichael Tuexen #endif
2244f8829a4aSRandall Stewart 		}
2245a412576eSMichael Tuexen 		tmr = &net->rxt_timer;
2246f8829a4aSRandall Stewart 		if (net->RTO == 0) {
224725ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2248f8829a4aSRandall Stewart 		} else {
224925ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
2250f8829a4aSRandall Stewart 		}
2251f8829a4aSRandall Stewart 		break;
2252f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
2253f8829a4aSRandall Stewart 		/*
2254a412576eSMichael Tuexen 		 * The net is used here so that we can add in the RTO. Even
2255f8829a4aSRandall Stewart 		 * though we use a different timer. We also add the HB timer
2256f8829a4aSRandall Stewart 		 * PLUS a random jitter.
2257f8829a4aSRandall Stewart 		 */
2258a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2259a412576eSMichael Tuexen #ifdef INVARIANTS
2260a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2261a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2262a412576eSMichael Tuexen #else
2263ad81507eSRandall Stewart 			return;
2264a412576eSMichael Tuexen #endif
2265a412576eSMichael Tuexen 		}
2266ca85e948SMichael Tuexen 		if ((net->dest_state & SCTP_ADDR_NOHB) &&
2267ca85e948SMichael Tuexen 		    !(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
2268a412576eSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_TIMER2,
226937686ccfSMichael Tuexen 			    "Timer type %d not started: inp=%p, stcb=%p, net=%p.\n",
2270a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2271ad81507eSRandall Stewart 			return;
2272f8829a4aSRandall Stewart 		}
2273a412576eSMichael Tuexen 		tmr = &net->hb_timer;
2274f8829a4aSRandall Stewart 		if (net->RTO == 0) {
2275ca85e948SMichael Tuexen 			to_ticks = stcb->asoc.initial_rto;
2276f8829a4aSRandall Stewart 		} else {
2277ca85e948SMichael Tuexen 			to_ticks = net->RTO;
2278f8829a4aSRandall Stewart 		}
2279ca85e948SMichael Tuexen 		rndval = sctp_select_initial_TSN(&inp->sctp_ep);
2280ca85e948SMichael Tuexen 		jitter = rndval % to_ticks;
2281ca85e948SMichael Tuexen 		if (jitter >= (to_ticks >> 1)) {
2282ca85e948SMichael Tuexen 			to_ticks = to_ticks + (jitter - (to_ticks >> 1));
2283f8829a4aSRandall Stewart 		} else {
2284ca85e948SMichael Tuexen 			to_ticks = to_ticks - jitter;
2285f8829a4aSRandall Stewart 		}
2286ca85e948SMichael Tuexen 		if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
2287ca85e948SMichael Tuexen 		    !(net->dest_state & SCTP_ADDR_PF)) {
2288ca85e948SMichael Tuexen 			to_ticks += net->heart_beat_delay;
2289f8829a4aSRandall Stewart 		}
2290f8829a4aSRandall Stewart 		/*
2291a412576eSMichael Tuexen 		 * Now we must convert the to_ticks that are now in ms to
2292a412576eSMichael Tuexen 		 * ticks.
2293f8829a4aSRandall Stewart 		 */
229425ec3553SMichael Tuexen 		to_ticks = sctp_msecs_to_ticks(to_ticks);
2295f8829a4aSRandall Stewart 		break;
2296f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
2297f8829a4aSRandall Stewart 		/*
2298f8829a4aSRandall Stewart 		 * Here we can use the RTO timer from the network since one
2299a412576eSMichael Tuexen 		 * RTT was complete. If a retransmission happened then we
2300a412576eSMichael Tuexen 		 * will be using the RTO initial value.
2301f8829a4aSRandall Stewart 		 */
2302a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2303a412576eSMichael Tuexen #ifdef INVARIANTS
2304a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2305a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2306a412576eSMichael Tuexen #else
2307ad81507eSRandall Stewart 			return;
2308a412576eSMichael Tuexen #endif
2309f8829a4aSRandall Stewart 		}
2310a412576eSMichael Tuexen 		tmr = &net->rxt_timer;
2311f8829a4aSRandall Stewart 		if (net->RTO == 0) {
231225ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2313f8829a4aSRandall Stewart 		} else {
231425ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
2315f8829a4aSRandall Stewart 		}
2316f8829a4aSRandall Stewart 		break;
2317f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_NEWCOOKIE:
2318f8829a4aSRandall Stewart 		/*
2319a412576eSMichael Tuexen 		 * Nothing needed but the endpoint here ususually about 60
2320f8829a4aSRandall Stewart 		 * minutes.
2321f8829a4aSRandall Stewart 		 */
2322a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb != NULL) || (net != NULL)) {
2323a412576eSMichael Tuexen #ifdef INVARIANTS
2324a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2325a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2326a412576eSMichael Tuexen #else
2327a412576eSMichael Tuexen 			return;
2328a412576eSMichael Tuexen #endif
2329a412576eSMichael Tuexen 		}
2330f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
2331f8829a4aSRandall Stewart 		to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_SIGNATURE];
2332f8829a4aSRandall Stewart 		break;
2333f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
2334f8829a4aSRandall Stewart 		/*
2335a412576eSMichael Tuexen 		 * Here we use the value found in the EP for PMTUD,
2336a412576eSMichael Tuexen 		 * ususually about 10 minutes.
2337f8829a4aSRandall Stewart 		 */
2338a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2339a412576eSMichael Tuexen #ifdef INVARIANTS
2340a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2341a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2342a412576eSMichael Tuexen #else
2343ad81507eSRandall Stewart 			return;
2344a412576eSMichael Tuexen #endif
2345f8829a4aSRandall Stewart 		}
234680c79bbeSMichael Tuexen 		if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
2347a412576eSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_TIMER2,
234837686ccfSMichael Tuexen 			    "Timer type %d not started: inp=%p, stcb=%p, net=%p.\n",
2349a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
235080c79bbeSMichael Tuexen 			return;
235180c79bbeSMichael Tuexen 		}
2352f8829a4aSRandall Stewart 		tmr = &net->pmtu_timer;
2353a412576eSMichael Tuexen 		to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_PMTU];
2354f8829a4aSRandall Stewart 		break;
2355f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
2356a412576eSMichael Tuexen 		/* Here we use the RTO of the destination. */
2357a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2358a412576eSMichael Tuexen #ifdef INVARIANTS
2359a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2360a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2361a412576eSMichael Tuexen #else
2362ad81507eSRandall Stewart 			return;
2363a412576eSMichael Tuexen #endif
2364f8829a4aSRandall Stewart 		}
2365a412576eSMichael Tuexen 		tmr = &net->rxt_timer;
2366f8829a4aSRandall Stewart 		if (net->RTO == 0) {
236725ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2368f8829a4aSRandall Stewart 		} else {
236925ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
2370f8829a4aSRandall Stewart 		}
2371f8829a4aSRandall Stewart 		break;
23720554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ASCONF:
23730554e01dSMichael Tuexen 		/*
23740554e01dSMichael Tuexen 		 * Here the timer comes from the stcb but its value is from
23750554e01dSMichael Tuexen 		 * the net's RTO.
23760554e01dSMichael Tuexen 		 */
2377a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2378a412576eSMichael Tuexen #ifdef INVARIANTS
2379a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2380a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2381a412576eSMichael Tuexen #else
23820554e01dSMichael Tuexen 			return;
2383a412576eSMichael Tuexen #endif
23840554e01dSMichael Tuexen 		}
2385a412576eSMichael Tuexen 		tmr = &stcb->asoc.asconf_timer;
23860554e01dSMichael Tuexen 		if (net->RTO == 0) {
238725ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
23880554e01dSMichael Tuexen 		} else {
238925ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
23900554e01dSMichael Tuexen 		}
23910554e01dSMichael Tuexen 		break;
2392f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2393f8829a4aSRandall Stewart 		/*
2394f8829a4aSRandall Stewart 		 * Here we use the endpoints shutdown guard timer usually
2395f8829a4aSRandall Stewart 		 * about 3 minutes.
2396f8829a4aSRandall Stewart 		 */
2397a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2398a412576eSMichael Tuexen #ifdef INVARIANTS
2399a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2400a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2401a412576eSMichael Tuexen #else
2402ad81507eSRandall Stewart 			return;
2403a412576eSMichael Tuexen #endif
2404f8829a4aSRandall Stewart 		}
2405a412576eSMichael Tuexen 		tmr = &stcb->asoc.shut_guard_timer;
24062e2d6794SMichael Tuexen 		if (inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] == 0) {
240725ec3553SMichael Tuexen 			if (stcb->asoc.maxrto < UINT32_MAX / 5) {
240825ec3553SMichael Tuexen 				to_ticks = sctp_msecs_to_ticks(5 * stcb->asoc.maxrto);
240925ec3553SMichael Tuexen 			} else {
241025ec3553SMichael Tuexen 				to_ticks = sctp_msecs_to_ticks(UINT32_MAX);
241125ec3553SMichael Tuexen 			}
24122e2d6794SMichael Tuexen 		} else {
2413f8829a4aSRandall Stewart 			to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN];
24142e2d6794SMichael Tuexen 		}
2415f8829a4aSRandall Stewart 		break;
24160554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_AUTOCLOSE:
2417a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2418a412576eSMichael Tuexen #ifdef INVARIANTS
2419a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2420a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2421a412576eSMichael Tuexen #else
24220554e01dSMichael Tuexen 			return;
2423a412576eSMichael Tuexen #endif
24240554e01dSMichael Tuexen 		}
24250554e01dSMichael Tuexen 		tmr = &stcb->asoc.autoclose_timer;
2426a412576eSMichael Tuexen 		to_ticks = stcb->asoc.sctp_autoclose_ticks;
24270554e01dSMichael Tuexen 		break;
2428f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_STRRESET:
2429f8829a4aSRandall Stewart 		/*
24301b649582SRandall Stewart 		 * Here the timer comes from the stcb but its value is from
24311b649582SRandall Stewart 		 * the net's RTO.
2432f8829a4aSRandall Stewart 		 */
2433a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2434a412576eSMichael Tuexen #ifdef INVARIANTS
2435a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2436a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2437a412576eSMichael Tuexen #else
2438ad81507eSRandall Stewart 			return;
2439a412576eSMichael Tuexen #endif
2440f8829a4aSRandall Stewart 		}
2441a412576eSMichael Tuexen 		tmr = &stcb->asoc.strreset_timer;
2442f8829a4aSRandall Stewart 		if (net->RTO == 0) {
244325ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2444f8829a4aSRandall Stewart 		} else {
244525ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
2446f8829a4aSRandall Stewart 		}
2447f8829a4aSRandall Stewart 		break;
24480554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_INPKILL:
2449f8829a4aSRandall Stewart 		/*
24500554e01dSMichael Tuexen 		 * The inp is setup to die. We re-use the signature_chage
24510554e01dSMichael Tuexen 		 * timer since that has stopped and we are in the GONE
24520554e01dSMichael Tuexen 		 * state.
2453f8829a4aSRandall Stewart 		 */
2454a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb != NULL) || (net != NULL)) {
2455a412576eSMichael Tuexen #ifdef INVARIANTS
2456a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2457a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2458a412576eSMichael Tuexen #else
2459a412576eSMichael Tuexen 			return;
2460a412576eSMichael Tuexen #endif
2461a412576eSMichael Tuexen 		}
24620554e01dSMichael Tuexen 		tmr = &inp->sctp_ep.signature_change;
246325ec3553SMichael Tuexen 		to_ticks = sctp_msecs_to_ticks(SCTP_INP_KILL_TIMEOUT);
24640554e01dSMichael Tuexen 		break;
24650554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ASOCKILL:
2466a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2467a412576eSMichael Tuexen #ifdef INVARIANTS
2468a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2469a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2470a412576eSMichael Tuexen #else
2471ad81507eSRandall Stewart 			return;
2472a412576eSMichael Tuexen #endif
2473f8829a4aSRandall Stewart 		}
24740554e01dSMichael Tuexen 		tmr = &stcb->asoc.strreset_timer;
247525ec3553SMichael Tuexen 		to_ticks = sctp_msecs_to_ticks(SCTP_ASOC_KILL_TIMEOUT);
24760554e01dSMichael Tuexen 		break;
24770554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ADDR_WQ:
2478a412576eSMichael Tuexen 		if ((inp != NULL) || (stcb != NULL) || (net != NULL)) {
2479a412576eSMichael Tuexen #ifdef INVARIANTS
2480a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2481a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2482a412576eSMichael Tuexen #else
2483a412576eSMichael Tuexen 			return;
2484a412576eSMichael Tuexen #endif
2485a412576eSMichael Tuexen 		}
24860554e01dSMichael Tuexen 		/* Only 1 tick away :-) */
24870554e01dSMichael Tuexen 		tmr = &SCTP_BASE_INFO(addr_wq_timer);
24880554e01dSMichael Tuexen 		to_ticks = SCTP_ADDRESS_TICK_DELAY;
2489f8829a4aSRandall Stewart 		break;
2490851b7298SRandall Stewart 	case SCTP_TIMER_TYPE_PRIM_DELETED:
2491a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2492a412576eSMichael Tuexen #ifdef INVARIANTS
2493a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2494a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2495a412576eSMichael Tuexen #else
2496851b7298SRandall Stewart 			return;
2497a412576eSMichael Tuexen #endif
2498851b7298SRandall Stewart 		}
2499851b7298SRandall Stewart 		tmr = &stcb->asoc.delete_prim_timer;
250025ec3553SMichael Tuexen 		to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2501851b7298SRandall Stewart 		break;
2502f8829a4aSRandall Stewart 	default:
25037522682eSMichael Tuexen #ifdef INVARIANTS
2504a412576eSMichael Tuexen 		panic("Unknown timer type %d", t_type);
25057522682eSMichael Tuexen #else
25067522682eSMichael Tuexen 		return;
25077522682eSMichael Tuexen #endif
250860990c0cSMichael Tuexen 	}
2509a412576eSMichael Tuexen 	KASSERT(tmr != NULL, ("tmr is NULL for timer type %d", t_type));
2510a412576eSMichael Tuexen 	KASSERT(to_ticks > 0, ("to_ticks == 0 for timer type %d", t_type));
2511139bc87fSRandall Stewart 	if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
2512f8829a4aSRandall Stewart 		/*
2513a412576eSMichael Tuexen 		 * We do NOT allow you to have it already running. If it is,
2514a412576eSMichael Tuexen 		 * we leave the current one up unchanged.
2515f8829a4aSRandall Stewart 		 */
2516a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
251737686ccfSMichael Tuexen 		    "Timer type %d already running: inp=%p, stcb=%p, net=%p.\n",
2518a412576eSMichael Tuexen 		    t_type, inp, stcb, net);
2519ad81507eSRandall Stewart 		return;
2520f8829a4aSRandall Stewart 	}
2521a412576eSMichael Tuexen 	/* At this point we can proceed. */
2522f8829a4aSRandall Stewart 	if (t_type == SCTP_TIMER_TYPE_SEND) {
2523f8829a4aSRandall Stewart 		stcb->asoc.num_send_timers_up++;
2524f8829a4aSRandall Stewart 	}
2525a5d547adSRandall Stewart 	tmr->stopped_from = 0;
2526f8829a4aSRandall Stewart 	tmr->type = t_type;
2527f8829a4aSRandall Stewart 	tmr->ep = (void *)inp;
2528f8829a4aSRandall Stewart 	tmr->tcb = (void *)stcb;
2529a412576eSMichael Tuexen 	if (t_type == SCTP_TIMER_TYPE_STRRESET) {
2530a412576eSMichael Tuexen 		tmr->net = NULL;
2531a412576eSMichael Tuexen 	} else {
2532f8829a4aSRandall Stewart 		tmr->net = (void *)net;
2533a412576eSMichael Tuexen 	}
2534f8829a4aSRandall Stewart 	tmr->self = (void *)tmr;
25358518270eSMichael Tuexen 	tmr->vnet = (void *)curvnet;
2536c4739e2fSRandall Stewart 	tmr->ticks = sctp_get_tick_count();
2537a412576eSMichael Tuexen 	if (SCTP_OS_TIMER_START(&tmr->timer, to_ticks, sctp_timeout_handler, tmr) == 0) {
2538a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
253937686ccfSMichael Tuexen 		    "Timer type %d started: ticks=%u, inp=%p, stcb=%p, net=%p.\n",
2540a412576eSMichael Tuexen 		    t_type, to_ticks, inp, stcb, net);
2541a412576eSMichael Tuexen 	} else {
2542a412576eSMichael Tuexen 		/*
2543a412576eSMichael Tuexen 		 * This should not happen, since we checked for pending
2544a412576eSMichael Tuexen 		 * above.
2545a412576eSMichael Tuexen 		 */
2546a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
254737686ccfSMichael Tuexen 		    "Timer type %d restarted: ticks=%u, inp=%p, stcb=%p, net=%p.\n",
2548a412576eSMichael Tuexen 		    t_type, to_ticks, inp, stcb, net);
2549a412576eSMichael Tuexen 	}
2550ad81507eSRandall Stewart 	return;
2551f8829a4aSRandall Stewart }
2552f8829a4aSRandall Stewart 
2553a412576eSMichael Tuexen /*-
2554a412576eSMichael Tuexen  * The following table shows which parameters must be provided
2555a412576eSMichael Tuexen  * when calling sctp_timer_stop(). For parameters not being
2556a412576eSMichael Tuexen  * provided, NULL must be used.
2557a412576eSMichael Tuexen  *
2558a412576eSMichael Tuexen  * |Name                         |inp |stcb|net |
2559a412576eSMichael Tuexen  * |-----------------------------|----|----|----|
2560a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SEND         |Yes |Yes |Yes |
2561a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_INIT         |Yes |Yes |Yes |
2562a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_RECV         |Yes |Yes |No  |
2563a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SHUTDOWN     |Yes |Yes |Yes |
2564a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_HEARTBEAT    |Yes |Yes |Yes |
2565a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_COOKIE       |Yes |Yes |Yes |
2566a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_NEWCOOKIE    |Yes |No  |No  |
2567a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_PATHMTURAISE |Yes |Yes |Yes |
2568a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SHUTDOWNACK  |Yes |Yes |Yes |
2569a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_ASCONF       |Yes |Yes |No  |
2570a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SHUTDOWNGUARD|Yes |Yes |No  |
2571a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_AUTOCLOSE    |Yes |Yes |No  |
2572a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_STRRESET     |Yes |Yes |No  |
2573a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_INPKILL      |Yes |No  |No  |
2574a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_ASOCKILL     |Yes |Yes |No  |
2575a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_ADDR_WQ      |No  |No  |No  |
2576a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_PRIM_DELETED |Yes |Yes |No  |
2577a412576eSMichael Tuexen  *
2578a412576eSMichael Tuexen  */
2579a412576eSMichael Tuexen 
25806e55db54SRandall Stewart void
2581f8829a4aSRandall Stewart sctp_timer_stop(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2582a5d547adSRandall Stewart     struct sctp_nets *net, uint32_t from)
2583f8829a4aSRandall Stewart {
2584f8829a4aSRandall Stewart 	struct sctp_timer *tmr;
2585f8829a4aSRandall Stewart 
2586f4cb790aSMichael Tuexen 	KASSERT(stcb == NULL || stcb->sctp_ep == inp,
2587f4cb790aSMichael Tuexen 	    ("sctp_timer_stop of type %d: inp = %p, stcb->sctp_ep %p",
2588f4cb790aSMichael Tuexen 	    t_type, stcb, stcb->sctp_ep));
2589a412576eSMichael Tuexen 	if (stcb != NULL) {
2590f8829a4aSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
2591a412576eSMichael Tuexen 	} else if (inp != NULL) {
2592a412576eSMichael Tuexen 		SCTP_INP_WLOCK_ASSERT(inp);
2593a412576eSMichael Tuexen 	} else {
2594a412576eSMichael Tuexen 		SCTP_WQ_ADDR_LOCK_ASSERT();
2595f8829a4aSRandall Stewart 	}
2596a412576eSMichael Tuexen 	tmr = NULL;
2597f8829a4aSRandall Stewart 	switch (t_type) {
2598f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SEND:
2599a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2600a412576eSMichael Tuexen #ifdef INVARIANTS
2601a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2602a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2603a412576eSMichael Tuexen #else
26046e55db54SRandall Stewart 			return;
2605a412576eSMichael Tuexen #endif
2606f8829a4aSRandall Stewart 		}
2607f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2608f8829a4aSRandall Stewart 		break;
2609f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
2610a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2611a412576eSMichael Tuexen #ifdef INVARIANTS
2612a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2613a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2614a412576eSMichael Tuexen #else
26156e55db54SRandall Stewart 			return;
2616a412576eSMichael Tuexen #endif
2617f8829a4aSRandall Stewart 		}
2618f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2619f8829a4aSRandall Stewart 		break;
2620f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
2621a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2622a412576eSMichael Tuexen #ifdef INVARIANTS
2623a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2624a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2625a412576eSMichael Tuexen #else
26266e55db54SRandall Stewart 			return;
2627a412576eSMichael Tuexen #endif
2628f8829a4aSRandall Stewart 		}
2629f8829a4aSRandall Stewart 		tmr = &stcb->asoc.dack_timer;
2630f8829a4aSRandall Stewart 		break;
2631f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
2632a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2633a412576eSMichael Tuexen #ifdef INVARIANTS
2634a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2635a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2636a412576eSMichael Tuexen #else
26376e55db54SRandall Stewart 			return;
2638a412576eSMichael Tuexen #endif
2639f8829a4aSRandall Stewart 		}
2640f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2641f8829a4aSRandall Stewart 		break;
2642f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
2643a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2644a412576eSMichael Tuexen #ifdef INVARIANTS
2645a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2646a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2647a412576eSMichael Tuexen #else
26486e55db54SRandall Stewart 			return;
2649a412576eSMichael Tuexen #endif
2650f8829a4aSRandall Stewart 		}
2651ca85e948SMichael Tuexen 		tmr = &net->hb_timer;
2652f8829a4aSRandall Stewart 		break;
2653f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
2654a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2655a412576eSMichael Tuexen #ifdef INVARIANTS
2656a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2657a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2658a412576eSMichael Tuexen #else
26596e55db54SRandall Stewart 			return;
2660a412576eSMichael Tuexen #endif
2661f8829a4aSRandall Stewart 		}
2662f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2663f8829a4aSRandall Stewart 		break;
2664f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_NEWCOOKIE:
2665a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb != NULL) || (net != NULL)) {
2666a412576eSMichael Tuexen #ifdef INVARIANTS
2667a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2668a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2669a412576eSMichael Tuexen #else
2670a412576eSMichael Tuexen 			return;
2671a412576eSMichael Tuexen #endif
2672a412576eSMichael Tuexen 		}
2673f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
2674f8829a4aSRandall Stewart 		break;
2675f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
2676a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2677a412576eSMichael Tuexen #ifdef INVARIANTS
2678a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2679a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2680a412576eSMichael Tuexen #else
26816e55db54SRandall Stewart 			return;
2682a412576eSMichael Tuexen #endif
2683f8829a4aSRandall Stewart 		}
2684f8829a4aSRandall Stewart 		tmr = &net->pmtu_timer;
2685f8829a4aSRandall Stewart 		break;
2686f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
2687a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2688a412576eSMichael Tuexen #ifdef INVARIANTS
2689a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2690a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2691a412576eSMichael Tuexen #else
26926e55db54SRandall Stewart 			return;
2693a412576eSMichael Tuexen #endif
2694f8829a4aSRandall Stewart 		}
2695f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2696f8829a4aSRandall Stewart 		break;
26970554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ASCONF:
2698a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2699a412576eSMichael Tuexen #ifdef INVARIANTS
2700a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2701a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2702a412576eSMichael Tuexen #else
27030554e01dSMichael Tuexen 			return;
2704a412576eSMichael Tuexen #endif
27050554e01dSMichael Tuexen 		}
27060554e01dSMichael Tuexen 		tmr = &stcb->asoc.asconf_timer;
27070554e01dSMichael Tuexen 		break;
2708f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2709a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2710a412576eSMichael Tuexen #ifdef INVARIANTS
2711a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2712a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2713a412576eSMichael Tuexen #else
27146e55db54SRandall Stewart 			return;
2715a412576eSMichael Tuexen #endif
2716f8829a4aSRandall Stewart 		}
2717f8829a4aSRandall Stewart 		tmr = &stcb->asoc.shut_guard_timer;
2718f8829a4aSRandall Stewart 		break;
27190554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_AUTOCLOSE:
2720a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2721a412576eSMichael Tuexen #ifdef INVARIANTS
2722a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2723a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2724a412576eSMichael Tuexen #else
27250554e01dSMichael Tuexen 			return;
2726a412576eSMichael Tuexen #endif
27270554e01dSMichael Tuexen 		}
27280554e01dSMichael Tuexen 		tmr = &stcb->asoc.autoclose_timer;
27290554e01dSMichael Tuexen 		break;
2730f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_STRRESET:
2731a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2732a412576eSMichael Tuexen #ifdef INVARIANTS
2733a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2734a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2735a412576eSMichael Tuexen #else
27366e55db54SRandall Stewart 			return;
2737a412576eSMichael Tuexen #endif
2738f8829a4aSRandall Stewart 		}
2739f8829a4aSRandall Stewart 		tmr = &stcb->asoc.strreset_timer;
2740f8829a4aSRandall Stewart 		break;
27410554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_INPKILL:
27420554e01dSMichael Tuexen 		/*
27430554e01dSMichael Tuexen 		 * The inp is setup to die. We re-use the signature_chage
27440554e01dSMichael Tuexen 		 * timer since that has stopped and we are in the GONE
27450554e01dSMichael Tuexen 		 * state.
27460554e01dSMichael Tuexen 		 */
2747a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb != NULL) || (net != NULL)) {
2748a412576eSMichael Tuexen #ifdef INVARIANTS
2749a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2750a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2751a412576eSMichael Tuexen #else
2752a412576eSMichael Tuexen 			return;
2753a412576eSMichael Tuexen #endif
2754a412576eSMichael Tuexen 		}
27550554e01dSMichael Tuexen 		tmr = &inp->sctp_ep.signature_change;
27560554e01dSMichael Tuexen 		break;
27570554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ASOCKILL:
2758a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2759a412576eSMichael Tuexen #ifdef INVARIANTS
2760a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2761a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2762a412576eSMichael Tuexen #else
27636e55db54SRandall Stewart 			return;
2764a412576eSMichael Tuexen #endif
2765f8829a4aSRandall Stewart 		}
27660554e01dSMichael Tuexen 		tmr = &stcb->asoc.strreset_timer;
27670554e01dSMichael Tuexen 		break;
27680554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ADDR_WQ:
2769a412576eSMichael Tuexen 		if ((inp != NULL) || (stcb != NULL) || (net != NULL)) {
2770a412576eSMichael Tuexen #ifdef INVARIANTS
2771a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2772a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2773a412576eSMichael Tuexen #else
2774a412576eSMichael Tuexen 			return;
2775a412576eSMichael Tuexen #endif
2776a412576eSMichael Tuexen 		}
27770554e01dSMichael Tuexen 		tmr = &SCTP_BASE_INFO(addr_wq_timer);
2778f8829a4aSRandall Stewart 		break;
2779851b7298SRandall Stewart 	case SCTP_TIMER_TYPE_PRIM_DELETED:
2780a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2781a412576eSMichael Tuexen #ifdef INVARIANTS
2782a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2783a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2784a412576eSMichael Tuexen #else
2785851b7298SRandall Stewart 			return;
2786a412576eSMichael Tuexen #endif
2787851b7298SRandall Stewart 		}
2788851b7298SRandall Stewart 		tmr = &stcb->asoc.delete_prim_timer;
2789851b7298SRandall Stewart 		break;
2790f8829a4aSRandall Stewart 	default:
27917522682eSMichael Tuexen #ifdef INVARIANTS
2792a412576eSMichael Tuexen 		panic("Unknown timer type %d", t_type);
27937522682eSMichael Tuexen #else
27947522682eSMichael Tuexen 		return;
27957522682eSMichael Tuexen #endif
279660990c0cSMichael Tuexen 	}
2797a412576eSMichael Tuexen 	KASSERT(tmr != NULL, ("tmr is NULL for timer type %d", t_type));
2798a412576eSMichael Tuexen 	if ((tmr->type != SCTP_TIMER_TYPE_NONE) &&
2799a412576eSMichael Tuexen 	    (tmr->type != t_type)) {
2800f8829a4aSRandall Stewart 		/*
2801f8829a4aSRandall Stewart 		 * Ok we have a timer that is under joint use. Cookie timer
2802f8829a4aSRandall Stewart 		 * per chance with the SEND timer. We therefore are NOT
2803f8829a4aSRandall Stewart 		 * running the timer that the caller wants stopped.  So just
2804f8829a4aSRandall Stewart 		 * return.
2805f8829a4aSRandall Stewart 		 */
2806a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
280737686ccfSMichael Tuexen 		    "Shared timer type %d not running: inp=%p, stcb=%p, net=%p.\n",
2808a412576eSMichael Tuexen 		    t_type, inp, stcb, net);
28096e55db54SRandall Stewart 		return;
2810f8829a4aSRandall Stewart 	}
2811ad81507eSRandall Stewart 	if ((t_type == SCTP_TIMER_TYPE_SEND) && (stcb != NULL)) {
2812f8829a4aSRandall Stewart 		stcb->asoc.num_send_timers_up--;
2813f8829a4aSRandall Stewart 		if (stcb->asoc.num_send_timers_up < 0) {
2814f8829a4aSRandall Stewart 			stcb->asoc.num_send_timers_up = 0;
2815f8829a4aSRandall Stewart 		}
2816f8829a4aSRandall Stewart 	}
2817f8829a4aSRandall Stewart 	tmr->self = NULL;
2818a5d547adSRandall Stewart 	tmr->stopped_from = from;
2819a412576eSMichael Tuexen 	if (SCTP_OS_TIMER_STOP(&tmr->timer) == 1) {
2820a412576eSMichael Tuexen 		KASSERT(tmr->ep == inp,
2821a412576eSMichael Tuexen 		    ("sctp_timer_stop of type %d: inp = %p, tmr->inp = %p",
2822a412576eSMichael Tuexen 		    t_type, inp, tmr->ep));
2823a412576eSMichael Tuexen 		KASSERT(tmr->tcb == stcb,
2824a412576eSMichael Tuexen 		    ("sctp_timer_stop of type %d: stcb = %p, tmr->stcb = %p",
2825a412576eSMichael Tuexen 		    t_type, stcb, tmr->tcb));
2826a412576eSMichael Tuexen 		KASSERT(((t_type == SCTP_TIMER_TYPE_ASCONF) && (tmr->net != NULL)) ||
2827a412576eSMichael Tuexen 		    ((t_type != SCTP_TIMER_TYPE_ASCONF) && (tmr->net == net)),
2828a412576eSMichael Tuexen 		    ("sctp_timer_stop of type %d: net = %p, tmr->net = %p",
2829a412576eSMichael Tuexen 		    t_type, net, tmr->net));
2830a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
283137686ccfSMichael Tuexen 		    "Timer type %d stopped: inp=%p, stcb=%p, net=%p.\n",
2832a412576eSMichael Tuexen 		    t_type, inp, stcb, net);
2833a412576eSMichael Tuexen 		tmr->ep = NULL;
2834a412576eSMichael Tuexen 		tmr->tcb = NULL;
2835a412576eSMichael Tuexen 		tmr->net = NULL;
2836a412576eSMichael Tuexen 	} else {
2837a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
283837686ccfSMichael Tuexen 		    "Timer type %d not stopped: inp=%p, stcb=%p, net=%p.\n",
2839a412576eSMichael Tuexen 		    t_type, inp, stcb, net);
2840a412576eSMichael Tuexen 	}
28416e55db54SRandall Stewart 	return;
2842f8829a4aSRandall Stewart }
2843f8829a4aSRandall Stewart 
2844f8829a4aSRandall Stewart uint32_t
2845b0471b4bSMichael Tuexen sctp_calculate_len(struct mbuf *m)
2846b0471b4bSMichael Tuexen {
2847f8829a4aSRandall Stewart 	uint32_t tlen = 0;
2848f8829a4aSRandall Stewart 	struct mbuf *at;
2849f8829a4aSRandall Stewart 
2850f8829a4aSRandall Stewart 	at = m;
2851f8829a4aSRandall Stewart 	while (at) {
2852139bc87fSRandall Stewart 		tlen += SCTP_BUF_LEN(at);
2853139bc87fSRandall Stewart 		at = SCTP_BUF_NEXT(at);
2854f8829a4aSRandall Stewart 	}
2855f8829a4aSRandall Stewart 	return (tlen);
2856f8829a4aSRandall Stewart }
2857f8829a4aSRandall Stewart 
2858f8829a4aSRandall Stewart void
2859f8829a4aSRandall Stewart sctp_mtu_size_reset(struct sctp_inpcb *inp,
286044b7479bSRandall Stewart     struct sctp_association *asoc, uint32_t mtu)
2861f8829a4aSRandall Stewart {
2862f8829a4aSRandall Stewart 	/*
2863f8829a4aSRandall Stewart 	 * Reset the P-MTU size on this association, this involves changing
2864f8829a4aSRandall Stewart 	 * the asoc MTU, going through ANY chunk+overhead larger than mtu to
2865f8829a4aSRandall Stewart 	 * allow the DF flag to be cleared.
2866f8829a4aSRandall Stewart 	 */
2867f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
2868f8829a4aSRandall Stewart 	unsigned int eff_mtu, ovh;
2869f8829a4aSRandall Stewart 
2870f8829a4aSRandall Stewart 	asoc->smallest_mtu = mtu;
2871f8829a4aSRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2872f8829a4aSRandall Stewart 		ovh = SCTP_MIN_OVERHEAD;
2873f8829a4aSRandall Stewart 	} else {
2874f8829a4aSRandall Stewart 		ovh = SCTP_MIN_V4_OVERHEAD;
2875f8829a4aSRandall Stewart 	}
2876f8829a4aSRandall Stewart 	eff_mtu = mtu - ovh;
2877f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
2878f8829a4aSRandall Stewart 		if (chk->send_size > eff_mtu) {
2879f8829a4aSRandall Stewart 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2880f8829a4aSRandall Stewart 		}
2881f8829a4aSRandall Stewart 	}
2882f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
2883f8829a4aSRandall Stewart 		if (chk->send_size > eff_mtu) {
2884f8829a4aSRandall Stewart 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2885f8829a4aSRandall Stewart 		}
2886f8829a4aSRandall Stewart 	}
2887f8829a4aSRandall Stewart }
2888f8829a4aSRandall Stewart 
2889f8829a4aSRandall Stewart 
2890f8829a4aSRandall Stewart /*
289144f2a327SMichael Tuexen  * Given an association and starting time of the current RTT period, update
289244f2a327SMichael Tuexen  * RTO in number of msecs. net should point to the current network.
289344f2a327SMichael Tuexen  * Return 1, if an RTO update was performed, return 0 if no update was
289444f2a327SMichael Tuexen  * performed due to invalid starting point.
2895f8829a4aSRandall Stewart  */
2896899288aeSRandall Stewart 
289744f2a327SMichael Tuexen int
2898f8829a4aSRandall Stewart sctp_calculate_rto(struct sctp_tcb *stcb,
2899f8829a4aSRandall Stewart     struct sctp_association *asoc,
2900f8829a4aSRandall Stewart     struct sctp_nets *net,
29018c8e10b7SMichael Tuexen     struct timeval *old,
2902b0471b4bSMichael Tuexen     int rtt_from_sack)
2903b0471b4bSMichael Tuexen {
290444f2a327SMichael Tuexen 	struct timeval now;
290544f2a327SMichael Tuexen 	uint64_t rtt_us;	/* RTT in us */
2906be1d9176SMichael Tuexen 	int32_t rtt;		/* RTT in ms */
2907be1d9176SMichael Tuexen 	uint32_t new_rto;
2908f8829a4aSRandall Stewart 	int first_measure = 0;
2909f8829a4aSRandall Stewart 
2910f8829a4aSRandall Stewart 	/************************/
2911f8829a4aSRandall Stewart 	/* 1. calculate new RTT */
2912f8829a4aSRandall Stewart 	/************************/
2913f8829a4aSRandall Stewart 	/* get the current time */
2914299108c5SRandall Stewart 	if (stcb->asoc.use_precise_time) {
2915299108c5SRandall Stewart 		(void)SCTP_GETPTIME_TIMEVAL(&now);
2916299108c5SRandall Stewart 	} else {
29176e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
2918299108c5SRandall Stewart 	}
291944f2a327SMichael Tuexen 	if ((old->tv_sec > now.tv_sec) ||
292088116b7eSMichael Tuexen 	    ((old->tv_sec == now.tv_sec) && (old->tv_usec > now.tv_usec))) {
292144f2a327SMichael Tuexen 		/* The starting point is in the future. */
292244f2a327SMichael Tuexen 		return (0);
292344f2a327SMichael Tuexen 	}
2924be1d9176SMichael Tuexen 	timevalsub(&now, old);
292544f2a327SMichael Tuexen 	rtt_us = (uint64_t)1000000 * (uint64_t)now.tv_sec + (uint64_t)now.tv_usec;
292644f2a327SMichael Tuexen 	if (rtt_us > SCTP_RTO_UPPER_BOUND * 1000) {
292744f2a327SMichael Tuexen 		/* The RTT is larger than a sane value. */
292844f2a327SMichael Tuexen 		return (0);
292944f2a327SMichael Tuexen 	}
2930be1d9176SMichael Tuexen 	/* store the current RTT in us */
293144f2a327SMichael Tuexen 	net->rtt = rtt_us;
2932b60b0fe6SMichael Tuexen 	/* compute rtt in ms */
2933b60b0fe6SMichael Tuexen 	rtt = (int32_t)(net->rtt / 1000);
2934f79aab18SRandall Stewart 	if ((asoc->cc_functions.sctp_rtt_calculated) && (rtt_from_sack == SCTP_RTT_FROM_DATA)) {
2935b7b84c0eSMichael Tuexen 		/*
2936b7b84c0eSMichael Tuexen 		 * Tell the CC module that a new update has just occurred
2937b7b84c0eSMichael Tuexen 		 * from a sack
2938b7b84c0eSMichael Tuexen 		 */
2939f79aab18SRandall Stewart 		(*asoc->cc_functions.sctp_rtt_calculated) (stcb, net, &now);
2940f79aab18SRandall Stewart 	}
2941f79aab18SRandall Stewart 	/*
2942f79aab18SRandall Stewart 	 * Do we need to determine the lan? We do this only on sacks i.e.
2943f79aab18SRandall Stewart 	 * RTT being determined from data not non-data (HB/INIT->INITACK).
2944f79aab18SRandall Stewart 	 */
2945f79aab18SRandall Stewart 	if ((rtt_from_sack == SCTP_RTT_FROM_DATA) &&
2946be1d9176SMichael Tuexen 	    (net->lan_type == SCTP_LAN_UNKNOWN)) {
2947be1d9176SMichael Tuexen 		if (net->rtt > SCTP_LOCAL_LAN_RTT) {
2948899288aeSRandall Stewart 			net->lan_type = SCTP_LAN_INTERNET;
2949899288aeSRandall Stewart 		} else {
2950899288aeSRandall Stewart 			net->lan_type = SCTP_LAN_LOCAL;
2951899288aeSRandall Stewart 		}
2952899288aeSRandall Stewart 	}
29530053ed28SMichael Tuexen 
2954f8829a4aSRandall Stewart 	/***************************/
2955f8829a4aSRandall Stewart 	/* 2. update RTTVAR & SRTT */
2956f8829a4aSRandall Stewart 	/***************************/
2957be1d9176SMichael Tuexen 	/*-
2958be1d9176SMichael Tuexen 	 * Compute the scaled average lastsa and the
2959be1d9176SMichael Tuexen 	 * scaled variance lastsv as described in van Jacobson
2960be1d9176SMichael Tuexen 	 * Paper "Congestion Avoidance and Control", Annex A.
2961be1d9176SMichael Tuexen 	 *
2962be1d9176SMichael Tuexen 	 * (net->lastsa >> SCTP_RTT_SHIFT) is the srtt
296344f2a327SMichael Tuexen 	 * (net->lastsv >> SCTP_RTT_VAR_SHIFT) is the rttvar
2964be1d9176SMichael Tuexen 	 */
29659a972525SRandall Stewart 	if (net->RTO_measured) {
2966be1d9176SMichael Tuexen 		rtt -= (net->lastsa >> SCTP_RTT_SHIFT);
2967be1d9176SMichael Tuexen 		net->lastsa += rtt;
2968be1d9176SMichael Tuexen 		if (rtt < 0) {
2969be1d9176SMichael Tuexen 			rtt = -rtt;
2970be1d9176SMichael Tuexen 		}
2971be1d9176SMichael Tuexen 		rtt -= (net->lastsv >> SCTP_RTT_VAR_SHIFT);
2972be1d9176SMichael Tuexen 		net->lastsv += rtt;
2973b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2974f8829a4aSRandall Stewart 			rto_logging(net, SCTP_LOG_RTTVAR);
297580fefe0aSRandall Stewart 		}
2976f8829a4aSRandall Stewart 	} else {
2977f8829a4aSRandall Stewart 		/* First RTO measurment */
29789a972525SRandall Stewart 		net->RTO_measured = 1;
2979f8829a4aSRandall Stewart 		first_measure = 1;
2980be1d9176SMichael Tuexen 		net->lastsa = rtt << SCTP_RTT_SHIFT;
2981be1d9176SMichael Tuexen 		net->lastsv = (rtt / 2) << SCTP_RTT_VAR_SHIFT;
2982b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2983f8829a4aSRandall Stewart 			rto_logging(net, SCTP_LOG_INITIAL_RTT);
298480fefe0aSRandall Stewart 		}
2985f8829a4aSRandall Stewart 	}
2986be1d9176SMichael Tuexen 	if (net->lastsv == 0) {
2987be1d9176SMichael Tuexen 		net->lastsv = SCTP_CLOCK_GRANULARITY;
2988be1d9176SMichael Tuexen 	}
2989108df27cSRandall Stewart 	new_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
2990f8829a4aSRandall Stewart 	if ((new_rto > SCTP_SAT_NETWORK_MIN) &&
2991f8829a4aSRandall Stewart 	    (stcb->asoc.sat_network_lockout == 0)) {
2992f8829a4aSRandall Stewart 		stcb->asoc.sat_network = 1;
2993f8829a4aSRandall Stewart 	} else if ((!first_measure) && stcb->asoc.sat_network) {
2994f8829a4aSRandall Stewart 		stcb->asoc.sat_network = 0;
2995f8829a4aSRandall Stewart 		stcb->asoc.sat_network_lockout = 1;
2996f8829a4aSRandall Stewart 	}
2997f8829a4aSRandall Stewart 	/* bound it, per C6/C7 in Section 5.3.1 */
2998f8829a4aSRandall Stewart 	if (new_rto < stcb->asoc.minrto) {
2999f8829a4aSRandall Stewart 		new_rto = stcb->asoc.minrto;
3000f8829a4aSRandall Stewart 	}
3001f8829a4aSRandall Stewart 	if (new_rto > stcb->asoc.maxrto) {
3002f8829a4aSRandall Stewart 		new_rto = stcb->asoc.maxrto;
3003f8829a4aSRandall Stewart 	}
300444f2a327SMichael Tuexen 	net->RTO = new_rto;
300544f2a327SMichael Tuexen 	return (1);
3006f8829a4aSRandall Stewart }
3007f8829a4aSRandall Stewart 
3008f8829a4aSRandall Stewart /*
3009f8829a4aSRandall Stewart  * return a pointer to a contiguous piece of data from the given mbuf chain
3010f8829a4aSRandall Stewart  * starting at 'off' for 'len' bytes.  If the desired piece spans more than
3011f8829a4aSRandall Stewart  * one mbuf, a copy is made at 'ptr'. caller must ensure that the buffer size
3012f8829a4aSRandall Stewart  * is >= 'len' returns NULL if there there isn't 'len' bytes in the chain.
3013f8829a4aSRandall Stewart  */
301472fb6fdbSRandall Stewart caddr_t
3015f8829a4aSRandall Stewart sctp_m_getptr(struct mbuf *m, int off, int len, uint8_t *in_ptr)
3016f8829a4aSRandall Stewart {
3017f8829a4aSRandall Stewart 	uint32_t count;
3018f8829a4aSRandall Stewart 	uint8_t *ptr;
3019f8829a4aSRandall Stewart 
3020f8829a4aSRandall Stewart 	ptr = in_ptr;
3021f8829a4aSRandall Stewart 	if ((off < 0) || (len <= 0))
3022f8829a4aSRandall Stewart 		return (NULL);
3023f8829a4aSRandall Stewart 
3024f8829a4aSRandall Stewart 	/* find the desired start location */
3025f8829a4aSRandall Stewart 	while ((m != NULL) && (off > 0)) {
3026139bc87fSRandall Stewart 		if (off < SCTP_BUF_LEN(m))
3027f8829a4aSRandall Stewart 			break;
3028139bc87fSRandall Stewart 		off -= SCTP_BUF_LEN(m);
3029139bc87fSRandall Stewart 		m = SCTP_BUF_NEXT(m);
3030f8829a4aSRandall Stewart 	}
3031f8829a4aSRandall Stewart 	if (m == NULL)
3032f8829a4aSRandall Stewart 		return (NULL);
3033f8829a4aSRandall Stewart 
3034f8829a4aSRandall Stewart 	/* is the current mbuf large enough (eg. contiguous)? */
3035139bc87fSRandall Stewart 	if ((SCTP_BUF_LEN(m) - off) >= len) {
3036f8829a4aSRandall Stewart 		return (mtod(m, caddr_t)+off);
3037f8829a4aSRandall Stewart 	} else {
3038f8829a4aSRandall Stewart 		/* else, it spans more than one mbuf, so save a temp copy... */
3039f8829a4aSRandall Stewart 		while ((m != NULL) && (len > 0)) {
3040139bc87fSRandall Stewart 			count = min(SCTP_BUF_LEN(m) - off, len);
30415ba7f91fSMichael Tuexen 			memcpy(ptr, mtod(m, caddr_t)+off, count);
3042f8829a4aSRandall Stewart 			len -= count;
3043f8829a4aSRandall Stewart 			ptr += count;
3044f8829a4aSRandall Stewart 			off = 0;
3045139bc87fSRandall Stewart 			m = SCTP_BUF_NEXT(m);
3046f8829a4aSRandall Stewart 		}
3047f8829a4aSRandall Stewart 		if ((m == NULL) && (len > 0))
3048f8829a4aSRandall Stewart 			return (NULL);
3049f8829a4aSRandall Stewart 		else
3050f8829a4aSRandall Stewart 			return ((caddr_t)in_ptr);
3051f8829a4aSRandall Stewart 	}
3052f8829a4aSRandall Stewart }
3053f8829a4aSRandall Stewart 
3054f8829a4aSRandall Stewart 
305544b7479bSRandall Stewart 
3056f8829a4aSRandall Stewart struct sctp_paramhdr *
3057f8829a4aSRandall Stewart sctp_get_next_param(struct mbuf *m,
3058f8829a4aSRandall Stewart     int offset,
3059f8829a4aSRandall Stewart     struct sctp_paramhdr *pull,
3060f8829a4aSRandall Stewart     int pull_limit)
3061f8829a4aSRandall Stewart {
3062f8829a4aSRandall Stewart 	/* This just provides a typed signature to Peter's Pull routine */
3063f8829a4aSRandall Stewart 	return ((struct sctp_paramhdr *)sctp_m_getptr(m, offset, pull_limit,
3064f8829a4aSRandall Stewart 	    (uint8_t *)pull));
3065f8829a4aSRandall Stewart }
3066f8829a4aSRandall Stewart 
3067f8829a4aSRandall Stewart 
3068ce11b842SMichael Tuexen struct mbuf *
3069f8829a4aSRandall Stewart sctp_add_pad_tombuf(struct mbuf *m, int padlen)
3070f8829a4aSRandall Stewart {
3071ce11b842SMichael Tuexen 	struct mbuf *m_last;
3072ce11b842SMichael Tuexen 	caddr_t dp;
3073f8829a4aSRandall Stewart 
3074f8829a4aSRandall Stewart 	if (padlen > 3) {
3075ce11b842SMichael Tuexen 		return (NULL);
3076f8829a4aSRandall Stewart 	}
307741eee555SRandall Stewart 	if (padlen <= M_TRAILINGSPACE(m)) {
3078f8829a4aSRandall Stewart 		/*
3079f8829a4aSRandall Stewart 		 * The easy way. We hope the majority of the time we hit
3080f8829a4aSRandall Stewart 		 * here :)
3081f8829a4aSRandall Stewart 		 */
3082ce11b842SMichael Tuexen 		m_last = m;
3083f8829a4aSRandall Stewart 	} else {
3084ce11b842SMichael Tuexen 		/* Hard way we must grow the mbuf chain */
3085ce11b842SMichael Tuexen 		m_last = sctp_get_mbuf_for_msg(padlen, 0, M_NOWAIT, 1, MT_DATA);
3086ce11b842SMichael Tuexen 		if (m_last == NULL) {
3087ce11b842SMichael Tuexen 			return (NULL);
3088f8829a4aSRandall Stewart 		}
3089ce11b842SMichael Tuexen 		SCTP_BUF_LEN(m_last) = 0;
3090ce11b842SMichael Tuexen 		SCTP_BUF_NEXT(m_last) = NULL;
3091ce11b842SMichael Tuexen 		SCTP_BUF_NEXT(m) = m_last;
3092f8829a4aSRandall Stewart 	}
3093ce11b842SMichael Tuexen 	dp = mtod(m_last, caddr_t)+SCTP_BUF_LEN(m_last);
3094ce11b842SMichael Tuexen 	SCTP_BUF_LEN(m_last) += padlen;
3095ce11b842SMichael Tuexen 	memset(dp, 0, padlen);
3096ce11b842SMichael Tuexen 	return (m_last);
3097f8829a4aSRandall Stewart }
3098f8829a4aSRandall Stewart 
3099ce11b842SMichael Tuexen struct mbuf *
3100f8829a4aSRandall Stewart sctp_pad_lastmbuf(struct mbuf *m, int padval, struct mbuf *last_mbuf)
3101f8829a4aSRandall Stewart {
3102f8829a4aSRandall Stewart 	/* find the last mbuf in chain and pad it */
3103f8829a4aSRandall Stewart 	struct mbuf *m_at;
3104f8829a4aSRandall Stewart 
3105ce11b842SMichael Tuexen 	if (last_mbuf != NULL) {
3106f8829a4aSRandall Stewart 		return (sctp_add_pad_tombuf(last_mbuf, padval));
3107f8829a4aSRandall Stewart 	} else {
310817267b32SMichael Tuexen 		for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3109139bc87fSRandall Stewart 			if (SCTP_BUF_NEXT(m_at) == NULL) {
3110f8829a4aSRandall Stewart 				return (sctp_add_pad_tombuf(m_at, padval));
3111f8829a4aSRandall Stewart 			}
3112f8829a4aSRandall Stewart 		}
3113f8829a4aSRandall Stewart 	}
3114ce11b842SMichael Tuexen 	return (NULL);
3115f8829a4aSRandall Stewart }
3116f8829a4aSRandall Stewart 
3117f8829a4aSRandall Stewart static void
3118c5b5675dSMichael Tuexen sctp_notify_assoc_change(uint16_t state, struct sctp_tcb *stcb,
3119*28397ac1SMichael Tuexen     uint16_t error, struct sctp_abort_chunk *abort, uint8_t from_peer, int so_locked)
3120f8829a4aSRandall Stewart {
3121f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3122f8829a4aSRandall Stewart 	struct sctp_assoc_change *sac;
3123f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
31249a8e3088SMichael Tuexen 	unsigned int notif_len;
31259a8e3088SMichael Tuexen 	uint16_t abort_len;
3126e06b67c7SMichael Tuexen 	unsigned int i;
3127ceaad40aSRandall Stewart 
312859713bbfSMichael Tuexen 	if (stcb == NULL) {
312959713bbfSMichael Tuexen 		return;
313059713bbfSMichael Tuexen 	}
313158411b08SMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT)) {
31329a8e3088SMichael Tuexen 		notif_len = (unsigned int)sizeof(struct sctp_assoc_change);
3133a2b42326SMichael Tuexen 		if (abort != NULL) {
3134c9eb4473SMichael Tuexen 			abort_len = ntohs(abort->ch.chunk_length);
31359669e724SMichael Tuexen 			/*
31369669e724SMichael Tuexen 			 * Only SCTP_CHUNK_BUFFER_SIZE are guaranteed to be
313745d41de5SMichael Tuexen 			 * contiguous.
31389669e724SMichael Tuexen 			 */
31399669e724SMichael Tuexen 			if (abort_len > SCTP_CHUNK_BUFFER_SIZE) {
31409669e724SMichael Tuexen 				abort_len = SCTP_CHUNK_BUFFER_SIZE;
31419669e724SMichael Tuexen 			}
3142a2b42326SMichael Tuexen 		} else {
3143a2b42326SMichael Tuexen 			abort_len = 0;
3144c5b5675dSMichael Tuexen 		}
3145a2b42326SMichael Tuexen 		if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
3146a2b42326SMichael Tuexen 			notif_len += SCTP_ASSOC_SUPPORTS_MAX;
3147a2b42326SMichael Tuexen 		} else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
3148a2b42326SMichael Tuexen 			notif_len += abort_len;
3149a2b42326SMichael Tuexen 		}
3150eb1b1807SGleb Smirnoff 		m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3151a2b42326SMichael Tuexen 		if (m_notify == NULL) {
3152a2b42326SMichael Tuexen 			/* Retry with smaller value. */
31539a8e3088SMichael Tuexen 			notif_len = (unsigned int)sizeof(struct sctp_assoc_change);
3154eb1b1807SGleb Smirnoff 			m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3155a2b42326SMichael Tuexen 			if (m_notify == NULL) {
315658411b08SMichael Tuexen 				goto set_error;
3157a2b42326SMichael Tuexen 			}
3158a2b42326SMichael Tuexen 		}
3159a2b42326SMichael Tuexen 		SCTP_BUF_NEXT(m_notify) = NULL;
3160f8829a4aSRandall Stewart 		sac = mtod(m_notify, struct sctp_assoc_change *);
3161e432298aSXin LI 		memset(sac, 0, notif_len);
3162f8829a4aSRandall Stewart 		sac->sac_type = SCTP_ASSOC_CHANGE;
3163f8829a4aSRandall Stewart 		sac->sac_flags = 0;
3164f8829a4aSRandall Stewart 		sac->sac_length = sizeof(struct sctp_assoc_change);
3165c5b5675dSMichael Tuexen 		sac->sac_state = state;
3166f8829a4aSRandall Stewart 		sac->sac_error = error;
3167f8829a4aSRandall Stewart 		/* XXX verify these stream counts */
3168f8829a4aSRandall Stewart 		sac->sac_outbound_streams = stcb->asoc.streamoutcnt;
3169f8829a4aSRandall Stewart 		sac->sac_inbound_streams = stcb->asoc.streamincnt;
3170f8829a4aSRandall Stewart 		sac->sac_assoc_id = sctp_get_associd(stcb);
3171a2b42326SMichael Tuexen 		if (notif_len > sizeof(struct sctp_assoc_change)) {
3172c5b5675dSMichael Tuexen 			if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
3173e06b67c7SMichael Tuexen 				i = 0;
3174c79bec9cSMichael Tuexen 				if (stcb->asoc.prsctp_supported == 1) {
3175e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_PR;
3176e06b67c7SMichael Tuexen 				}
3177c79bec9cSMichael Tuexen 				if (stcb->asoc.auth_supported == 1) {
3178e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_AUTH;
3179e06b67c7SMichael Tuexen 				}
3180c79bec9cSMichael Tuexen 				if (stcb->asoc.asconf_supported == 1) {
3181e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_ASCONF;
3182e06b67c7SMichael Tuexen 				}
318344249214SRandall Stewart 				if (stcb->asoc.idata_supported == 1) {
318444249214SRandall Stewart 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_INTERLEAVING;
318544249214SRandall Stewart 				}
3186e06b67c7SMichael Tuexen 				sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_MULTIBUF;
3187c79bec9cSMichael Tuexen 				if (stcb->asoc.reconfig_supported == 1) {
3188e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_RE_CONFIG;
3189e06b67c7SMichael Tuexen 				}
3190e06b67c7SMichael Tuexen 				sac->sac_length += i;
3191a2b42326SMichael Tuexen 			} else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
3192a2b42326SMichael Tuexen 				memcpy(sac->sac_info, abort, abort_len);
3193a2b42326SMichael Tuexen 				sac->sac_length += abort_len;
3194a2b42326SMichael Tuexen 			}
3195c5b5675dSMichael Tuexen 		}
3196e06b67c7SMichael Tuexen 		SCTP_BUF_LEN(m_notify) = sac->sac_length;
3197f8829a4aSRandall Stewart 		control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
31987215cc1bSMichael Tuexen 		    0, 0, stcb->asoc.context, 0, 0, 0,
3199f8829a4aSRandall Stewart 		    m_notify);
320058411b08SMichael Tuexen 		if (control != NULL) {
3201139bc87fSRandall Stewart 			control->length = SCTP_BUF_LEN(m_notify);
320228cd0699SMichael Tuexen 			control->spec_flags = M_NOTIFICATION;
3203f8829a4aSRandall Stewart 			/* not that we need this */
3204f8829a4aSRandall Stewart 			control->tail_mbuf = m_notify;
3205f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep, stcb,
3206f8829a4aSRandall Stewart 			    control,
3207cfde3ff7SRandall Stewart 			    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD,
3208cfde3ff7SRandall Stewart 			    so_locked);
320958411b08SMichael Tuexen 		} else {
321058411b08SMichael Tuexen 			sctp_m_freem(m_notify);
321158411b08SMichael Tuexen 		}
321258411b08SMichael Tuexen 	}
321358411b08SMichael Tuexen 	/*
321458411b08SMichael Tuexen 	 * For 1-to-1 style sockets, we send up and error when an ABORT
321558411b08SMichael Tuexen 	 * comes in.
321658411b08SMichael Tuexen 	 */
321758411b08SMichael Tuexen set_error:
321858411b08SMichael Tuexen 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
321958411b08SMichael Tuexen 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
322058411b08SMichael Tuexen 	    ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
3221e045904fSMichael Tuexen 		SOCK_LOCK(stcb->sctp_socket);
3222410a3b1eSMichael Tuexen 		if (from_peer) {
3223839d21d6SMichael Tuexen 			if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) {
322458411b08SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNREFUSED);
322558411b08SMichael Tuexen 				stcb->sctp_socket->so_error = ECONNREFUSED;
322658411b08SMichael Tuexen 			} else {
322758411b08SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
322858411b08SMichael Tuexen 				stcb->sctp_socket->so_error = ECONNRESET;
322958411b08SMichael Tuexen 			}
3230410a3b1eSMichael Tuexen 		} else {
3231839d21d6SMichael Tuexen 			if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
3232839d21d6SMichael Tuexen 			    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
3233553bb068SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ETIMEDOUT);
3234553bb068SMichael Tuexen 				stcb->sctp_socket->so_error = ETIMEDOUT;
3235553bb068SMichael Tuexen 			} else {
3236410a3b1eSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNABORTED);
3237410a3b1eSMichael Tuexen 				stcb->sctp_socket->so_error = ECONNABORTED;
3238410a3b1eSMichael Tuexen 			}
323958411b08SMichael Tuexen 		}
32403acfe1e1SGleb Smirnoff 		SOCK_UNLOCK(stcb->sctp_socket);
3241553bb068SMichael Tuexen 	}
324258411b08SMichael Tuexen 	/* Wake ANY sleepers */
324358411b08SMichael Tuexen 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
324458411b08SMichael Tuexen 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
324558411b08SMichael Tuexen 	    ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
32463acfe1e1SGleb Smirnoff 		socantrcvmore(stcb->sctp_socket);
324758411b08SMichael Tuexen 	}
324858411b08SMichael Tuexen 	sorwakeup(stcb->sctp_socket);
324958411b08SMichael Tuexen 	sowwakeup(stcb->sctp_socket);
3250f8829a4aSRandall Stewart }
3251f8829a4aSRandall Stewart 
3252f8829a4aSRandall Stewart static void
3253f8829a4aSRandall Stewart sctp_notify_peer_addr_change(struct sctp_tcb *stcb, uint32_t state,
3254*28397ac1SMichael Tuexen     struct sockaddr *sa, uint32_t error, int so_locked)
3255f8829a4aSRandall Stewart {
3256f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3257f8829a4aSRandall Stewart 	struct sctp_paddr_change *spc;
3258f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3259f8829a4aSRandall Stewart 
326060990c0cSMichael Tuexen 	if ((stcb == NULL) ||
326160990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT)) {
3262f8829a4aSRandall Stewart 		/* event not enabled */
3263f8829a4aSRandall Stewart 		return;
3264830d754dSRandall Stewart 	}
3265eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_paddr_change), 0, M_NOWAIT, 1, MT_DATA);
3266f8829a4aSRandall Stewart 	if (m_notify == NULL)
3267f8829a4aSRandall Stewart 		return;
3268139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3269f8829a4aSRandall Stewart 	spc = mtod(m_notify, struct sctp_paddr_change *);
327056711f94SMichael Tuexen 	memset(spc, 0, sizeof(struct sctp_paddr_change));
3271f8829a4aSRandall Stewart 	spc->spc_type = SCTP_PEER_ADDR_CHANGE;
3272f8829a4aSRandall Stewart 	spc->spc_flags = 0;
3273f8829a4aSRandall Stewart 	spc->spc_length = sizeof(struct sctp_paddr_change);
32745e2c2d87SRandall Stewart 	switch (sa->sa_family) {
3275ea5eba11SMichael Tuexen #ifdef INET
32765e2c2d87SRandall Stewart 	case AF_INET:
3277d59107f7SMichael Tuexen #ifdef INET6
3278d59107f7SMichael Tuexen 		if (sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
3279d59107f7SMichael Tuexen 			in6_sin_2_v4mapsin6((struct sockaddr_in *)sa,
3280d59107f7SMichael Tuexen 			    (struct sockaddr_in6 *)&spc->spc_aaddr);
3281d59107f7SMichael Tuexen 		} else {
3282f8829a4aSRandall Stewart 			memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
3283d59107f7SMichael Tuexen 		}
3284d59107f7SMichael Tuexen #else
3285d59107f7SMichael Tuexen 		memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
3286d59107f7SMichael Tuexen #endif
32875e2c2d87SRandall Stewart 		break;
3288ea5eba11SMichael Tuexen #endif
32895e2c2d87SRandall Stewart #ifdef INET6
32905e2c2d87SRandall Stewart 	case AF_INET6:
32915e2c2d87SRandall Stewart 		{
3292f42a358aSRandall Stewart 			struct sockaddr_in6 *sin6;
3293f42a358aSRandall Stewart 
3294f8829a4aSRandall Stewart 			memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in6));
3295f42a358aSRandall Stewart 
3296f42a358aSRandall Stewart 			sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
3297f42a358aSRandall Stewart 			if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
329842551e99SRandall Stewart 				if (sin6->sin6_scope_id == 0) {
329942551e99SRandall Stewart 					/* recover scope_id for user */
3300f42a358aSRandall Stewart 					(void)sa6_recoverscope(sin6);
330142551e99SRandall Stewart 				} else {
330242551e99SRandall Stewart 					/* clear embedded scope_id for user */
330342551e99SRandall Stewart 					in6_clearscope(&sin6->sin6_addr);
330442551e99SRandall Stewart 				}
3305f42a358aSRandall Stewart 			}
33065e2c2d87SRandall Stewart 			break;
33075e2c2d87SRandall Stewart 		}
33085e2c2d87SRandall Stewart #endif
33095e2c2d87SRandall Stewart 	default:
33105e2c2d87SRandall Stewart 		/* TSNH */
33115e2c2d87SRandall Stewart 		break;
3312f8829a4aSRandall Stewart 	}
3313f8829a4aSRandall Stewart 	spc->spc_state = state;
3314f8829a4aSRandall Stewart 	spc->spc_error = error;
3315f8829a4aSRandall Stewart 	spc->spc_assoc_id = sctp_get_associd(stcb);
3316f8829a4aSRandall Stewart 
3317139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_paddr_change);
3318139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3319f8829a4aSRandall Stewart 
3320f8829a4aSRandall Stewart 	/* append to socket */
3321f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
33227215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3323f8829a4aSRandall Stewart 	    m_notify);
3324f8829a4aSRandall Stewart 	if (control == NULL) {
3325f8829a4aSRandall Stewart 		/* no memory */
3326f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3327f8829a4aSRandall Stewart 		return;
3328f8829a4aSRandall Stewart 	}
3329139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3330139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3331f8829a4aSRandall Stewart 	/* not that we need this */
3332f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3333f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3334f8829a4aSRandall Stewart 	    control,
3335cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1,
3336cfde3ff7SRandall Stewart 	    SCTP_READ_LOCK_NOT_HELD,
33373cb3567dSMichael Tuexen 	    so_locked);
3338f8829a4aSRandall Stewart }
3339f8829a4aSRandall Stewart 
3340f8829a4aSRandall Stewart 
3341f8829a4aSRandall Stewart static void
33421edc9dbaSMichael Tuexen sctp_notify_send_failed(struct sctp_tcb *stcb, uint8_t sent, uint32_t error,
3343*28397ac1SMichael Tuexen     struct sctp_tmit_chunk *chk, int so_locked)
3344f8829a4aSRandall Stewart {
3345830d754dSRandall Stewart 	struct mbuf *m_notify;
3346f8829a4aSRandall Stewart 	struct sctp_send_failed *ssf;
33479935403aSMichael Tuexen 	struct sctp_send_failed_event *ssfe;
3348f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3349ab337314SMichael Tuexen 	struct sctp_chunkhdr *chkhdr;
3350ab337314SMichael Tuexen 	int notifhdr_len, chk_len, chkhdr_len, padding_len, payload_len;
3351f8829a4aSRandall Stewart 
335260990c0cSMichael Tuexen 	if ((stcb == NULL) ||
33539935403aSMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
33549935403aSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
3355f8829a4aSRandall Stewart 		/* event not enabled */
3356f8829a4aSRandall Stewart 		return;
3357830d754dSRandall Stewart 	}
33580053ed28SMichael Tuexen 
33599935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3360ab337314SMichael Tuexen 		notifhdr_len = sizeof(struct sctp_send_failed_event);
33619935403aSMichael Tuexen 	} else {
3362ab337314SMichael Tuexen 		notifhdr_len = sizeof(struct sctp_send_failed);
33639935403aSMichael Tuexen 	}
3364ab337314SMichael Tuexen 	m_notify = sctp_get_mbuf_for_msg(notifhdr_len, 0, M_NOWAIT, 1, MT_DATA);
3365f8829a4aSRandall Stewart 	if (m_notify == NULL)
3366f8829a4aSRandall Stewart 		/* no space left */
3367f8829a4aSRandall Stewart 		return;
3368ab337314SMichael Tuexen 	SCTP_BUF_LEN(m_notify) = notifhdr_len;
3369ab337314SMichael Tuexen 	if (stcb->asoc.idata_supported) {
3370ab337314SMichael Tuexen 		chkhdr_len = sizeof(struct sctp_idata_chunk);
3371ab337314SMichael Tuexen 	} else {
3372ab337314SMichael Tuexen 		chkhdr_len = sizeof(struct sctp_data_chunk);
3373ab337314SMichael Tuexen 	}
3374ab337314SMichael Tuexen 	/* Use some defaults in case we can't access the chunk header */
3375ab337314SMichael Tuexen 	if (chk->send_size >= chkhdr_len) {
3376ab337314SMichael Tuexen 		payload_len = chk->send_size - chkhdr_len;
3377ab337314SMichael Tuexen 	} else {
3378ab337314SMichael Tuexen 		payload_len = 0;
3379ab337314SMichael Tuexen 	}
3380ab337314SMichael Tuexen 	padding_len = 0;
3381ab337314SMichael Tuexen 	if (chk->data != NULL) {
3382ab337314SMichael Tuexen 		chkhdr = mtod(chk->data, struct sctp_chunkhdr *);
3383ab337314SMichael Tuexen 		if (chkhdr != NULL) {
3384ab337314SMichael Tuexen 			chk_len = ntohs(chkhdr->chunk_length);
3385ab337314SMichael Tuexen 			if ((chk_len >= chkhdr_len) &&
3386ab337314SMichael Tuexen 			    (chk->send_size >= chk_len) &&
3387ab337314SMichael Tuexen 			    (chk->send_size - chk_len < 4)) {
3388ab337314SMichael Tuexen 				padding_len = chk->send_size - chk_len;
3389ab337314SMichael Tuexen 				payload_len = chk->send_size - chkhdr_len - padding_len;
3390ab337314SMichael Tuexen 			}
3391ab337314SMichael Tuexen 		}
3392ab337314SMichael Tuexen 	}
33939935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
33949935403aSMichael Tuexen 		ssfe = mtod(m_notify, struct sctp_send_failed_event *);
3395ab337314SMichael Tuexen 		memset(ssfe, 0, notifhdr_len);
33969935403aSMichael Tuexen 		ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
33971edc9dbaSMichael Tuexen 		if (sent) {
33989935403aSMichael Tuexen 			ssfe->ssfe_flags = SCTP_DATA_SENT;
33991edc9dbaSMichael Tuexen 		} else {
34001edc9dbaSMichael Tuexen 			ssfe->ssfe_flags = SCTP_DATA_UNSENT;
34011edc9dbaSMichael Tuexen 		}
3402ab337314SMichael Tuexen 		ssfe->ssfe_length = (uint32_t)(notifhdr_len + payload_len);
34039935403aSMichael Tuexen 		ssfe->ssfe_error = error;
34049935403aSMichael Tuexen 		/* not exactly what the user sent in, but should be close :) */
340549656eefSMichael Tuexen 		ssfe->ssfe_info.snd_sid = chk->rec.data.sid;
34069935403aSMichael Tuexen 		ssfe->ssfe_info.snd_flags = chk->rec.data.rcv_flags;
340749656eefSMichael Tuexen 		ssfe->ssfe_info.snd_ppid = chk->rec.data.ppid;
34089935403aSMichael Tuexen 		ssfe->ssfe_info.snd_context = chk->rec.data.context;
34099935403aSMichael Tuexen 		ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
34109935403aSMichael Tuexen 		ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
34119935403aSMichael Tuexen 	} else {
3412f8829a4aSRandall Stewart 		ssf = mtod(m_notify, struct sctp_send_failed *);
3413ab337314SMichael Tuexen 		memset(ssf, 0, notifhdr_len);
3414f8829a4aSRandall Stewart 		ssf->ssf_type = SCTP_SEND_FAILED;
34151edc9dbaSMichael Tuexen 		if (sent) {
3416f8829a4aSRandall Stewart 			ssf->ssf_flags = SCTP_DATA_SENT;
34171edc9dbaSMichael Tuexen 		} else {
34181edc9dbaSMichael Tuexen 			ssf->ssf_flags = SCTP_DATA_UNSENT;
34191edc9dbaSMichael Tuexen 		}
3420ab337314SMichael Tuexen 		ssf->ssf_length = (uint32_t)(notifhdr_len + payload_len);
3421f8829a4aSRandall Stewart 		ssf->ssf_error = error;
3422f8829a4aSRandall Stewart 		/* not exactly what the user sent in, but should be close :) */
342349656eefSMichael Tuexen 		ssf->ssf_info.sinfo_stream = chk->rec.data.sid;
342449656eefSMichael Tuexen 		ssf->ssf_info.sinfo_ssn = (uint16_t)chk->rec.data.mid;
3425f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_flags = chk->rec.data.rcv_flags;
342649656eefSMichael Tuexen 		ssf->ssf_info.sinfo_ppid = chk->rec.data.ppid;
3427f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_context = chk->rec.data.context;
3428f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3429f8829a4aSRandall Stewart 		ssf->ssf_assoc_id = sctp_get_associd(stcb);
34309935403aSMichael Tuexen 	}
3431ab337314SMichael Tuexen 	if (chk->data != NULL) {
3432ab337314SMichael Tuexen 		/* Trim off the sctp chunk header (it should be there) */
3433ab337314SMichael Tuexen 		if (chk->send_size == chkhdr_len + payload_len + padding_len) {
3434ab337314SMichael Tuexen 			m_adj(chk->data, chkhdr_len);
3435ab337314SMichael Tuexen 			m_adj(chk->data, -padding_len);
3436830d754dSRandall Stewart 			sctp_mbuf_crush(chk->data);
3437ab337314SMichael Tuexen 			chk->send_size -= (chkhdr_len + padding_len);
3438830d754dSRandall Stewart 		}
3439830d754dSRandall Stewart 	}
3440810ec536SMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = chk->data;
3441f8829a4aSRandall Stewart 	/* Steal off the mbuf */
3442f8829a4aSRandall Stewart 	chk->data = NULL;
3443f8829a4aSRandall Stewart 	/*
3444f8829a4aSRandall Stewart 	 * For this case, we check the actual socket buffer, since the assoc
3445f8829a4aSRandall Stewart 	 * is going away we don't want to overfill the socket buffer for a
3446f8829a4aSRandall Stewart 	 * non-reader
3447f8829a4aSRandall Stewart 	 */
3448139bc87fSRandall Stewart 	if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3449f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3450f8829a4aSRandall Stewart 		return;
3451f8829a4aSRandall Stewart 	}
3452f8829a4aSRandall Stewart 	/* append to socket */
3453f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
34547215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3455f8829a4aSRandall Stewart 	    m_notify);
3456f8829a4aSRandall Stewart 	if (control == NULL) {
3457f8829a4aSRandall Stewart 		/* no memory */
3458f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3459f8829a4aSRandall Stewart 		return;
3460f8829a4aSRandall Stewart 	}
346128cd0699SMichael Tuexen 	control->length = SCTP_BUF_LEN(m_notify);
3462139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
346328cd0699SMichael Tuexen 	/* not that we need this */
346428cd0699SMichael Tuexen 	control->tail_mbuf = m_notify;
3465f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3466f8829a4aSRandall Stewart 	    control,
3467cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1,
3468cfde3ff7SRandall Stewart 	    SCTP_READ_LOCK_NOT_HELD,
3469cfde3ff7SRandall Stewart 	    so_locked);
3470f8829a4aSRandall Stewart }
3471f8829a4aSRandall Stewart 
3472f8829a4aSRandall Stewart 
3473f8829a4aSRandall Stewart static void
3474f8829a4aSRandall Stewart sctp_notify_send_failed2(struct sctp_tcb *stcb, uint32_t error,
3475*28397ac1SMichael Tuexen     struct sctp_stream_queue_pending *sp, int so_locked)
3476f8829a4aSRandall Stewart {
3477f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3478f8829a4aSRandall Stewart 	struct sctp_send_failed *ssf;
34799935403aSMichael Tuexen 	struct sctp_send_failed_event *ssfe;
3480f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3481ab337314SMichael Tuexen 	int notifhdr_len;
3482f8829a4aSRandall Stewart 
348360990c0cSMichael Tuexen 	if ((stcb == NULL) ||
34849935403aSMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
34859935403aSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
3486f8829a4aSRandall Stewart 		/* event not enabled */
3487f8829a4aSRandall Stewart 		return;
3488830d754dSRandall Stewart 	}
34899935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3490ab337314SMichael Tuexen 		notifhdr_len = sizeof(struct sctp_send_failed_event);
34919935403aSMichael Tuexen 	} else {
3492ab337314SMichael Tuexen 		notifhdr_len = sizeof(struct sctp_send_failed);
34939935403aSMichael Tuexen 	}
3494ab337314SMichael Tuexen 	m_notify = sctp_get_mbuf_for_msg(notifhdr_len, 0, M_NOWAIT, 1, MT_DATA);
34959935403aSMichael Tuexen 	if (m_notify == NULL) {
3496f8829a4aSRandall Stewart 		/* no space left */
3497f8829a4aSRandall Stewart 		return;
34989935403aSMichael Tuexen 	}
3499ab337314SMichael Tuexen 	SCTP_BUF_LEN(m_notify) = notifhdr_len;
35009935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
35019935403aSMichael Tuexen 		ssfe = mtod(m_notify, struct sctp_send_failed_event *);
3502ab337314SMichael Tuexen 		memset(ssfe, 0, notifhdr_len);
3503ad83c8a5SMichael Tuexen 		ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
35049935403aSMichael Tuexen 		ssfe->ssfe_flags = SCTP_DATA_UNSENT;
3505ab337314SMichael Tuexen 		ssfe->ssfe_length = (uint32_t)(notifhdr_len + sp->length);
35069935403aSMichael Tuexen 		ssfe->ssfe_error = error;
35079935403aSMichael Tuexen 		/* not exactly what the user sent in, but should be close :) */
350849656eefSMichael Tuexen 		ssfe->ssfe_info.snd_sid = sp->sid;
35099935403aSMichael Tuexen 		if (sp->some_taken) {
35109935403aSMichael Tuexen 			ssfe->ssfe_info.snd_flags = SCTP_DATA_LAST_FRAG;
35119935403aSMichael Tuexen 		} else {
35129935403aSMichael Tuexen 			ssfe->ssfe_info.snd_flags = SCTP_DATA_NOT_FRAG;
35139935403aSMichael Tuexen 		}
35149935403aSMichael Tuexen 		ssfe->ssfe_info.snd_ppid = sp->ppid;
35159935403aSMichael Tuexen 		ssfe->ssfe_info.snd_context = sp->context;
35169935403aSMichael Tuexen 		ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
35179935403aSMichael Tuexen 		ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
35189935403aSMichael Tuexen 	} else {
3519f8829a4aSRandall Stewart 		ssf = mtod(m_notify, struct sctp_send_failed *);
3520ab337314SMichael Tuexen 		memset(ssf, 0, notifhdr_len);
3521f8829a4aSRandall Stewart 		ssf->ssf_type = SCTP_SEND_FAILED;
3522f8829a4aSRandall Stewart 		ssf->ssf_flags = SCTP_DATA_UNSENT;
3523ab337314SMichael Tuexen 		ssf->ssf_length = (uint32_t)(notifhdr_len + sp->length);
3524f8829a4aSRandall Stewart 		ssf->ssf_error = error;
3525f8829a4aSRandall Stewart 		/* not exactly what the user sent in, but should be close :) */
352649656eefSMichael Tuexen 		ssf->ssf_info.sinfo_stream = sp->sid;
3527f3b05218SMichael Tuexen 		ssf->ssf_info.sinfo_ssn = 0;
3528fc14de76SRandall Stewart 		if (sp->some_taken) {
3529fc14de76SRandall Stewart 			ssf->ssf_info.sinfo_flags = SCTP_DATA_LAST_FRAG;
3530fc14de76SRandall Stewart 		} else {
3531fc14de76SRandall Stewart 			ssf->ssf_info.sinfo_flags = SCTP_DATA_NOT_FRAG;
3532fc14de76SRandall Stewart 		}
3533f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_ppid = sp->ppid;
3534f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_context = sp->context;
3535f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3536f8829a4aSRandall Stewart 		ssf->ssf_assoc_id = sctp_get_associd(stcb);
35379935403aSMichael Tuexen 	}
35389935403aSMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = sp->data;
3539f8829a4aSRandall Stewart 
3540f8829a4aSRandall Stewart 	/* Steal off the mbuf */
3541f8829a4aSRandall Stewart 	sp->data = NULL;
3542f8829a4aSRandall Stewart 	/*
3543f8829a4aSRandall Stewart 	 * For this case, we check the actual socket buffer, since the assoc
3544f8829a4aSRandall Stewart 	 * is going away we don't want to overfill the socket buffer for a
3545f8829a4aSRandall Stewart 	 * non-reader
3546f8829a4aSRandall Stewart 	 */
3547139bc87fSRandall Stewart 	if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3548f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3549f8829a4aSRandall Stewart 		return;
3550f8829a4aSRandall Stewart 	}
3551f8829a4aSRandall Stewart 	/* append to socket */
3552f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
35537215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3554f8829a4aSRandall Stewart 	    m_notify);
3555f8829a4aSRandall Stewart 	if (control == NULL) {
3556f8829a4aSRandall Stewart 		/* no memory */
3557f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3558f8829a4aSRandall Stewart 		return;
3559f8829a4aSRandall Stewart 	}
356028cd0699SMichael Tuexen 	control->length = SCTP_BUF_LEN(m_notify);
3561139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
356228cd0699SMichael Tuexen 	/* not that we need this */
356328cd0699SMichael Tuexen 	control->tail_mbuf = m_notify;
3564f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3565f8829a4aSRandall Stewart 	    control,
3566cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3567f8829a4aSRandall Stewart }
3568f8829a4aSRandall Stewart 
3569f8829a4aSRandall Stewart 
3570f8829a4aSRandall Stewart 
3571f8829a4aSRandall Stewart static void
35727215cc1bSMichael Tuexen sctp_notify_adaptation_layer(struct sctp_tcb *stcb)
3573f8829a4aSRandall Stewart {
3574f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3575f8829a4aSRandall Stewart 	struct sctp_adaptation_event *sai;
3576f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3577f8829a4aSRandall Stewart 
357860990c0cSMichael Tuexen 	if ((stcb == NULL) ||
357960990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT)) {
3580f8829a4aSRandall Stewart 		/* event not enabled */
3581f8829a4aSRandall Stewart 		return;
3582830d754dSRandall Stewart 	}
35830053ed28SMichael Tuexen 
3584eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 0, M_NOWAIT, 1, MT_DATA);
3585f8829a4aSRandall Stewart 	if (m_notify == NULL)
3586f8829a4aSRandall Stewart 		/* no space left */
3587f8829a4aSRandall Stewart 		return;
3588139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3589f8829a4aSRandall Stewart 	sai = mtod(m_notify, struct sctp_adaptation_event *);
3590e432298aSXin LI 	memset(sai, 0, sizeof(struct sctp_adaptation_event));
3591f8829a4aSRandall Stewart 	sai->sai_type = SCTP_ADAPTATION_INDICATION;
3592f8829a4aSRandall Stewart 	sai->sai_flags = 0;
3593f8829a4aSRandall Stewart 	sai->sai_length = sizeof(struct sctp_adaptation_event);
35942afb3e84SRandall Stewart 	sai->sai_adaptation_ind = stcb->asoc.peers_adaptation;
3595f8829a4aSRandall Stewart 	sai->sai_assoc_id = sctp_get_associd(stcb);
3596f8829a4aSRandall Stewart 
3597139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_adaptation_event);
3598139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3599f8829a4aSRandall Stewart 
3600f8829a4aSRandall Stewart 	/* append to socket */
3601f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
36027215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3603f8829a4aSRandall Stewart 	    m_notify);
3604f8829a4aSRandall Stewart 	if (control == NULL) {
3605f8829a4aSRandall Stewart 		/* no memory */
3606f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3607f8829a4aSRandall Stewart 		return;
3608f8829a4aSRandall Stewart 	}
3609139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3610139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3611f8829a4aSRandall Stewart 	/* not that we need this */
3612f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3613f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3614f8829a4aSRandall Stewart 	    control,
3615cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3616f8829a4aSRandall Stewart }
3617f8829a4aSRandall Stewart 
361803b0b021SRandall Stewart /* This always must be called with the read-queue LOCKED in the INP */
3619810ec536SMichael Tuexen static void
36202dad8a55SRandall Stewart sctp_notify_partial_delivery_indication(struct sctp_tcb *stcb, uint32_t error,
3621*28397ac1SMichael Tuexen     uint32_t val, int so_locked)
3622f8829a4aSRandall Stewart {
3623f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3624f8829a4aSRandall Stewart 	struct sctp_pdapi_event *pdapi;
3625f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
362603b0b021SRandall Stewart 	struct sockbuf *sb;
3627f8829a4aSRandall Stewart 
362860990c0cSMichael Tuexen 	if ((stcb == NULL) ||
362960990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_PDAPIEVNT)) {
3630f8829a4aSRandall Stewart 		/* event not enabled */
3631f8829a4aSRandall Stewart 		return;
3632830d754dSRandall Stewart 	}
3633cd1386abSMichael Tuexen 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
3634cd1386abSMichael Tuexen 		return;
3635cd1386abSMichael Tuexen 	}
36360053ed28SMichael Tuexen 
3637eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_pdapi_event), 0, M_NOWAIT, 1, MT_DATA);
3638f8829a4aSRandall Stewart 	if (m_notify == NULL)
3639f8829a4aSRandall Stewart 		/* no space left */
3640f8829a4aSRandall Stewart 		return;
3641139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3642f8829a4aSRandall Stewart 	pdapi = mtod(m_notify, struct sctp_pdapi_event *);
3643e432298aSXin LI 	memset(pdapi, 0, sizeof(struct sctp_pdapi_event));
3644f8829a4aSRandall Stewart 	pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
3645f8829a4aSRandall Stewart 	pdapi->pdapi_flags = 0;
3646f8829a4aSRandall Stewart 	pdapi->pdapi_length = sizeof(struct sctp_pdapi_event);
3647f8829a4aSRandall Stewart 	pdapi->pdapi_indication = error;
36489a6142d8SRandall Stewart 	pdapi->pdapi_stream = (val >> 16);
36499a6142d8SRandall Stewart 	pdapi->pdapi_seq = (val & 0x0000ffff);
3650f8829a4aSRandall Stewart 	pdapi->pdapi_assoc_id = sctp_get_associd(stcb);
3651f8829a4aSRandall Stewart 
3652139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_pdapi_event);
3653139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3654f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
36557215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3656f8829a4aSRandall Stewart 	    m_notify);
3657f8829a4aSRandall Stewart 	if (control == NULL) {
3658f8829a4aSRandall Stewart 		/* no memory */
3659f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3660f8829a4aSRandall Stewart 		return;
3661f8829a4aSRandall Stewart 	}
3662139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
366328cd0699SMichael Tuexen 	control->spec_flags = M_NOTIFICATION;
3664f8829a4aSRandall Stewart 	/* not that we need this */
3665f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
366603b0b021SRandall Stewart 	sb = &stcb->sctp_socket->so_rcv;
3667b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3668139bc87fSRandall Stewart 		sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m_notify));
366980fefe0aSRandall Stewart 	}
367003b0b021SRandall Stewart 	sctp_sballoc(stcb, sb, m_notify);
3671b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
367203b0b021SRandall Stewart 		sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
367380fefe0aSRandall Stewart 	}
367403b0b021SRandall Stewart 	control->end_added = 1;
367503b0b021SRandall Stewart 	if (stcb->asoc.control_pdapi)
367603b0b021SRandall Stewart 		TAILQ_INSERT_AFTER(&stcb->sctp_ep->read_queue, stcb->asoc.control_pdapi, control, next);
367703b0b021SRandall Stewart 	else {
367803b0b021SRandall Stewart 		/* we really should not see this case */
367903b0b021SRandall Stewart 		TAILQ_INSERT_TAIL(&stcb->sctp_ep->read_queue, control, next);
368003b0b021SRandall Stewart 	}
368103b0b021SRandall Stewart 	if (stcb->sctp_ep && stcb->sctp_socket) {
368203b0b021SRandall Stewart 		/* This should always be the case */
368303b0b021SRandall Stewart 		sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
3684f8829a4aSRandall Stewart 	}
3685f8829a4aSRandall Stewart }
3686f8829a4aSRandall Stewart 
3687f8829a4aSRandall Stewart static void
3688f8829a4aSRandall Stewart sctp_notify_shutdown_event(struct sctp_tcb *stcb)
3689f8829a4aSRandall Stewart {
3690f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3691f8829a4aSRandall Stewart 	struct sctp_shutdown_event *sse;
3692f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3693f8829a4aSRandall Stewart 
3694f8829a4aSRandall Stewart 	/*
3695f8829a4aSRandall Stewart 	 * For TCP model AND UDP connected sockets we will send an error up
3696f8829a4aSRandall Stewart 	 * when an SHUTDOWN completes
3697f8829a4aSRandall Stewart 	 */
3698f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3699f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3700f8829a4aSRandall Stewart 		/* mark socket closed for read/write and wakeup! */
3701f8829a4aSRandall Stewart 		socantsendmore(stcb->sctp_socket);
3702f8829a4aSRandall Stewart 	}
3703e2e7c62eSMichael Tuexen 	if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)) {
3704f8829a4aSRandall Stewart 		/* event not enabled */
3705f8829a4aSRandall Stewart 		return;
3706830d754dSRandall Stewart 	}
37070053ed28SMichael Tuexen 
3708eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_event), 0, M_NOWAIT, 1, MT_DATA);
3709f8829a4aSRandall Stewart 	if (m_notify == NULL)
3710f8829a4aSRandall Stewart 		/* no space left */
3711f8829a4aSRandall Stewart 		return;
3712f8829a4aSRandall Stewart 	sse = mtod(m_notify, struct sctp_shutdown_event *);
3713e432298aSXin LI 	memset(sse, 0, sizeof(struct sctp_shutdown_event));
3714f8829a4aSRandall Stewart 	sse->sse_type = SCTP_SHUTDOWN_EVENT;
3715f8829a4aSRandall Stewart 	sse->sse_flags = 0;
3716f8829a4aSRandall Stewart 	sse->sse_length = sizeof(struct sctp_shutdown_event);
3717f8829a4aSRandall Stewart 	sse->sse_assoc_id = sctp_get_associd(stcb);
3718f8829a4aSRandall Stewart 
3719139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_shutdown_event);
3720139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3721f8829a4aSRandall Stewart 
3722f8829a4aSRandall Stewart 	/* append to socket */
3723f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
37247215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3725f8829a4aSRandall Stewart 	    m_notify);
3726f8829a4aSRandall Stewart 	if (control == NULL) {
3727f8829a4aSRandall Stewart 		/* no memory */
3728f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3729f8829a4aSRandall Stewart 		return;
3730f8829a4aSRandall Stewart 	}
3731139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
373228cd0699SMichael Tuexen 	control->spec_flags = M_NOTIFICATION;
3733f8829a4aSRandall Stewart 	/* not that we need this */
3734f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3735f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3736f8829a4aSRandall Stewart 	    control,
3737cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3738f8829a4aSRandall Stewart }
3739f8829a4aSRandall Stewart 
3740f8829a4aSRandall Stewart static void
3741830d754dSRandall Stewart sctp_notify_sender_dry_event(struct sctp_tcb *stcb,
3742*28397ac1SMichael Tuexen     int so_locked)
3743830d754dSRandall Stewart {
3744830d754dSRandall Stewart 	struct mbuf *m_notify;
3745830d754dSRandall Stewart 	struct sctp_sender_dry_event *event;
3746830d754dSRandall Stewart 	struct sctp_queued_to_read *control;
3747830d754dSRandall Stewart 
374860990c0cSMichael Tuexen 	if ((stcb == NULL) ||
374960990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DRYEVNT)) {
3750830d754dSRandall Stewart 		/* event not enabled */
3751830d754dSRandall Stewart 		return;
3752830d754dSRandall Stewart 	}
37530053ed28SMichael Tuexen 
3754eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_sender_dry_event), 0, M_NOWAIT, 1, MT_DATA);
3755830d754dSRandall Stewart 	if (m_notify == NULL) {
3756830d754dSRandall Stewart 		/* no space left */
3757830d754dSRandall Stewart 		return;
3758830d754dSRandall Stewart 	}
3759830d754dSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3760830d754dSRandall Stewart 	event = mtod(m_notify, struct sctp_sender_dry_event *);
3761e432298aSXin LI 	memset(event, 0, sizeof(struct sctp_sender_dry_event));
3762830d754dSRandall Stewart 	event->sender_dry_type = SCTP_SENDER_DRY_EVENT;
3763830d754dSRandall Stewart 	event->sender_dry_flags = 0;
3764830d754dSRandall Stewart 	event->sender_dry_length = sizeof(struct sctp_sender_dry_event);
3765830d754dSRandall Stewart 	event->sender_dry_assoc_id = sctp_get_associd(stcb);
3766830d754dSRandall Stewart 
3767830d754dSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_sender_dry_event);
3768830d754dSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3769830d754dSRandall Stewart 
3770830d754dSRandall Stewart 	/* append to socket */
3771830d754dSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
37727215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
37737215cc1bSMichael Tuexen 	    m_notify);
3774830d754dSRandall Stewart 	if (control == NULL) {
3775830d754dSRandall Stewart 		/* no memory */
3776830d754dSRandall Stewart 		sctp_m_freem(m_notify);
3777830d754dSRandall Stewart 		return;
3778830d754dSRandall Stewart 	}
3779830d754dSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3780830d754dSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3781830d754dSRandall Stewart 	/* not that we need this */
3782830d754dSRandall Stewart 	control->tail_mbuf = m_notify;
3783830d754dSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3784cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3785830d754dSRandall Stewart }
3786830d754dSRandall Stewart 
3787ea44232bSRandall Stewart 
3788c4e848b7SRandall Stewart void
3789c4e848b7SRandall Stewart sctp_notify_stream_reset_add(struct sctp_tcb *stcb, uint16_t numberin, uint16_t numberout, int flag)
3790ea44232bSRandall Stewart {
3791ea44232bSRandall Stewart 	struct mbuf *m_notify;
3792ea44232bSRandall Stewart 	struct sctp_queued_to_read *control;
3793c4e848b7SRandall Stewart 	struct sctp_stream_change_event *stradd;
3794ea44232bSRandall Stewart 
37958c501e51SMichael Tuexen 	if ((stcb == NULL) ||
37968c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_CHANGEEVNT))) {
3797ea44232bSRandall Stewart 		/* event not enabled */
3798ea44232bSRandall Stewart 		return;
3799ea44232bSRandall Stewart 	}
3800c4e848b7SRandall Stewart 	if ((stcb->asoc.peer_req_out) && flag) {
3801c4e848b7SRandall Stewart 		/* Peer made the request, don't tell the local user */
3802c4e848b7SRandall Stewart 		stcb->asoc.peer_req_out = 0;
3803c4e848b7SRandall Stewart 		return;
3804c4e848b7SRandall Stewart 	}
3805c4e848b7SRandall Stewart 	stcb->asoc.peer_req_out = 0;
3806e432298aSXin LI 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_stream_change_event), 0, M_NOWAIT, 1, MT_DATA);
3807ea44232bSRandall Stewart 	if (m_notify == NULL)
3808ea44232bSRandall Stewart 		/* no space left */
3809ea44232bSRandall Stewart 		return;
3810ea44232bSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3811c4e848b7SRandall Stewart 	stradd = mtod(m_notify, struct sctp_stream_change_event *);
3812e432298aSXin LI 	memset(stradd, 0, sizeof(struct sctp_stream_change_event));
3813c4e848b7SRandall Stewart 	stradd->strchange_type = SCTP_STREAM_CHANGE_EVENT;
3814c4e848b7SRandall Stewart 	stradd->strchange_flags = flag;
3815e432298aSXin LI 	stradd->strchange_length = sizeof(struct sctp_stream_change_event);
3816c4e848b7SRandall Stewart 	stradd->strchange_assoc_id = sctp_get_associd(stcb);
3817c4e848b7SRandall Stewart 	stradd->strchange_instrms = numberin;
3818c4e848b7SRandall Stewart 	stradd->strchange_outstrms = numberout;
3819e432298aSXin LI 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_stream_change_event);
3820ea44232bSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3821ea44232bSRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3822ea44232bSRandall Stewart 		/* no space */
3823ea44232bSRandall Stewart 		sctp_m_freem(m_notify);
3824ea44232bSRandall Stewart 		return;
3825ea44232bSRandall Stewart 	}
3826ea44232bSRandall Stewart 	/* append to socket */
3827ea44232bSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
38287215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3829ea44232bSRandall Stewart 	    m_notify);
3830ea44232bSRandall Stewart 	if (control == NULL) {
3831ea44232bSRandall Stewart 		/* no memory */
3832ea44232bSRandall Stewart 		sctp_m_freem(m_notify);
3833ea44232bSRandall Stewart 		return;
3834ea44232bSRandall Stewart 	}
3835ea44232bSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
383628cd0699SMichael Tuexen 	control->spec_flags = M_NOTIFICATION;
3837ea44232bSRandall Stewart 	/* not that we need this */
3838ea44232bSRandall Stewart 	control->tail_mbuf = m_notify;
3839ea44232bSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3840ea44232bSRandall Stewart 	    control,
3841cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3842ea44232bSRandall Stewart }
3843ea44232bSRandall Stewart 
3844c4e848b7SRandall Stewart void
3845c4e848b7SRandall Stewart sctp_notify_stream_reset_tsn(struct sctp_tcb *stcb, uint32_t sending_tsn, uint32_t recv_tsn, int flag)
3846c4e848b7SRandall Stewart {
3847c4e848b7SRandall Stewart 	struct mbuf *m_notify;
3848c4e848b7SRandall Stewart 	struct sctp_queued_to_read *control;
3849c4e848b7SRandall Stewart 	struct sctp_assoc_reset_event *strasoc;
3850c4e848b7SRandall Stewart 
38518c501e51SMichael Tuexen 	if ((stcb == NULL) ||
38528c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ASSOC_RESETEVNT))) {
3853c4e848b7SRandall Stewart 		/* event not enabled */
3854c4e848b7SRandall Stewart 		return;
3855c4e848b7SRandall Stewart 	}
3856e432298aSXin LI 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_assoc_reset_event), 0, M_NOWAIT, 1, MT_DATA);
3857c4e848b7SRandall Stewart 	if (m_notify == NULL)
3858c4e848b7SRandall Stewart 		/* no space left */
3859c4e848b7SRandall Stewart 		return;
3860c4e848b7SRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3861c4e848b7SRandall Stewart 	strasoc = mtod(m_notify, struct sctp_assoc_reset_event *);
3862e432298aSXin LI 	memset(strasoc, 0, sizeof(struct sctp_assoc_reset_event));
3863c4e848b7SRandall Stewart 	strasoc->assocreset_type = SCTP_ASSOC_RESET_EVENT;
3864c4e848b7SRandall Stewart 	strasoc->assocreset_flags = flag;
3865e432298aSXin LI 	strasoc->assocreset_length = sizeof(struct sctp_assoc_reset_event);
3866c4e848b7SRandall Stewart 	strasoc->assocreset_assoc_id = sctp_get_associd(stcb);
3867c4e848b7SRandall Stewart 	strasoc->assocreset_local_tsn = sending_tsn;
3868c4e848b7SRandall Stewart 	strasoc->assocreset_remote_tsn = recv_tsn;
3869e432298aSXin LI 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_assoc_reset_event);
3870c4e848b7SRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3871c4e848b7SRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3872c4e848b7SRandall Stewart 		/* no space */
3873c4e848b7SRandall Stewart 		sctp_m_freem(m_notify);
3874c4e848b7SRandall Stewart 		return;
3875c4e848b7SRandall Stewart 	}
3876c4e848b7SRandall Stewart 	/* append to socket */
3877c4e848b7SRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3878c4e848b7SRandall Stewart 	    0, 0, stcb->asoc.context, 0, 0, 0,
3879c4e848b7SRandall Stewart 	    m_notify);
3880c4e848b7SRandall Stewart 	if (control == NULL) {
3881c4e848b7SRandall Stewart 		/* no memory */
3882c4e848b7SRandall Stewart 		sctp_m_freem(m_notify);
3883c4e848b7SRandall Stewart 		return;
3884c4e848b7SRandall Stewart 	}
3885c4e848b7SRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
388628cd0699SMichael Tuexen 	control->spec_flags = M_NOTIFICATION;
3887c4e848b7SRandall Stewart 	/* not that we need this */
3888c4e848b7SRandall Stewart 	control->tail_mbuf = m_notify;
3889c4e848b7SRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3890c4e848b7SRandall Stewart 	    control,
3891c4e848b7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3892c4e848b7SRandall Stewart }
3893c4e848b7SRandall Stewart 
3894c4e848b7SRandall Stewart 
3895ea44232bSRandall Stewart 
3896830d754dSRandall Stewart static void
3897f8829a4aSRandall Stewart sctp_notify_stream_reset(struct sctp_tcb *stcb,
3898f8829a4aSRandall Stewart     int number_entries, uint16_t *list, int flag)
3899f8829a4aSRandall Stewart {
3900f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3901f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3902f8829a4aSRandall Stewart 	struct sctp_stream_reset_event *strreset;
3903f8829a4aSRandall Stewart 	int len;
3904f8829a4aSRandall Stewart 
39058c501e51SMichael Tuexen 	if ((stcb == NULL) ||
39068c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT))) {
3907f8829a4aSRandall Stewart 		/* event not enabled */
3908f8829a4aSRandall Stewart 		return;
3909830d754dSRandall Stewart 	}
39100053ed28SMichael Tuexen 
3911eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
3912f8829a4aSRandall Stewart 	if (m_notify == NULL)
3913f8829a4aSRandall Stewart 		/* no space left */
3914f8829a4aSRandall Stewart 		return;
3915139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3916f8829a4aSRandall Stewart 	len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
3917f8829a4aSRandall Stewart 	if (len > M_TRAILINGSPACE(m_notify)) {
3918f8829a4aSRandall Stewart 		/* never enough room */
3919f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3920f8829a4aSRandall Stewart 		return;
3921f8829a4aSRandall Stewart 	}
3922f8829a4aSRandall Stewart 	strreset = mtod(m_notify, struct sctp_stream_reset_event *);
3923e432298aSXin LI 	memset(strreset, 0, len);
3924f8829a4aSRandall Stewart 	strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
3925c4e848b7SRandall Stewart 	strreset->strreset_flags = flag;
3926f8829a4aSRandall Stewart 	strreset->strreset_length = len;
3927f8829a4aSRandall Stewart 	strreset->strreset_assoc_id = sctp_get_associd(stcb);
3928f8829a4aSRandall Stewart 	if (number_entries) {
3929f8829a4aSRandall Stewart 		int i;
3930f8829a4aSRandall Stewart 
3931f8829a4aSRandall Stewart 		for (i = 0; i < number_entries; i++) {
3932c4e848b7SRandall Stewart 			strreset->strreset_stream_list[i] = ntohs(list[i]);
3933f8829a4aSRandall Stewart 		}
3934f8829a4aSRandall Stewart 	}
3935139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = len;
3936139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3937139bc87fSRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3938f8829a4aSRandall Stewart 		/* no space */
3939f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3940f8829a4aSRandall Stewart 		return;
3941f8829a4aSRandall Stewart 	}
3942f8829a4aSRandall Stewart 	/* append to socket */
3943f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
39447215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3945f8829a4aSRandall Stewart 	    m_notify);
3946f8829a4aSRandall Stewart 	if (control == NULL) {
3947f8829a4aSRandall Stewart 		/* no memory */
3948f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3949f8829a4aSRandall Stewart 		return;
3950f8829a4aSRandall Stewart 	}
3951139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
395228cd0699SMichael Tuexen 	control->spec_flags = M_NOTIFICATION;
3953f8829a4aSRandall Stewart 	/* not that we need this */
3954f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3955f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3956f8829a4aSRandall Stewart 	    control,
3957cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3958f8829a4aSRandall Stewart }
3959f8829a4aSRandall Stewart 
3960f8829a4aSRandall Stewart 
3961389b1b11SMichael Tuexen static void
3962389b1b11SMichael Tuexen sctp_notify_remote_error(struct sctp_tcb *stcb, uint16_t error, struct sctp_error_chunk *chunk)
3963389b1b11SMichael Tuexen {
3964389b1b11SMichael Tuexen 	struct mbuf *m_notify;
3965389b1b11SMichael Tuexen 	struct sctp_remote_error *sre;
3966389b1b11SMichael Tuexen 	struct sctp_queued_to_read *control;
39679a8e3088SMichael Tuexen 	unsigned int notif_len;
39689a8e3088SMichael Tuexen 	uint16_t chunk_len;
3969389b1b11SMichael Tuexen 
3970389b1b11SMichael Tuexen 	if ((stcb == NULL) ||
3971389b1b11SMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPEERERR)) {
3972389b1b11SMichael Tuexen 		return;
3973389b1b11SMichael Tuexen 	}
3974389b1b11SMichael Tuexen 	if (chunk != NULL) {
3975c9eb4473SMichael Tuexen 		chunk_len = ntohs(chunk->ch.chunk_length);
39769669e724SMichael Tuexen 		/*
39779669e724SMichael Tuexen 		 * Only SCTP_CHUNK_BUFFER_SIZE are guaranteed to be
397845d41de5SMichael Tuexen 		 * contiguous.
39799669e724SMichael Tuexen 		 */
39809669e724SMichael Tuexen 		if (chunk_len > SCTP_CHUNK_BUFFER_SIZE) {
39819669e724SMichael Tuexen 			chunk_len = SCTP_CHUNK_BUFFER_SIZE;
39829669e724SMichael Tuexen 		}
3983389b1b11SMichael Tuexen 	} else {
3984389b1b11SMichael Tuexen 		chunk_len = 0;
3985389b1b11SMichael Tuexen 	}
39869a8e3088SMichael Tuexen 	notif_len = (unsigned int)(sizeof(struct sctp_remote_error) + chunk_len);
3987eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3988389b1b11SMichael Tuexen 	if (m_notify == NULL) {
3989389b1b11SMichael Tuexen 		/* Retry with smaller value. */
39909a8e3088SMichael Tuexen 		notif_len = (unsigned int)sizeof(struct sctp_remote_error);
3991eb1b1807SGleb Smirnoff 		m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3992389b1b11SMichael Tuexen 		if (m_notify == NULL) {
3993389b1b11SMichael Tuexen 			return;
3994389b1b11SMichael Tuexen 		}
3995389b1b11SMichael Tuexen 	}
3996389b1b11SMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = NULL;
3997389b1b11SMichael Tuexen 	sre = mtod(m_notify, struct sctp_remote_error *);
399856711f94SMichael Tuexen 	memset(sre, 0, notif_len);
3999389b1b11SMichael Tuexen 	sre->sre_type = SCTP_REMOTE_ERROR;
4000389b1b11SMichael Tuexen 	sre->sre_flags = 0;
4001389b1b11SMichael Tuexen 	sre->sre_length = sizeof(struct sctp_remote_error);
4002389b1b11SMichael Tuexen 	sre->sre_error = error;
4003389b1b11SMichael Tuexen 	sre->sre_assoc_id = sctp_get_associd(stcb);
4004389b1b11SMichael Tuexen 	if (notif_len > sizeof(struct sctp_remote_error)) {
4005389b1b11SMichael Tuexen 		memcpy(sre->sre_data, chunk, chunk_len);
4006389b1b11SMichael Tuexen 		sre->sre_length += chunk_len;
4007389b1b11SMichael Tuexen 	}
4008389b1b11SMichael Tuexen 	SCTP_BUF_LEN(m_notify) = sre->sre_length;
4009389b1b11SMichael Tuexen 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
4010389b1b11SMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
4011389b1b11SMichael Tuexen 	    m_notify);
4012389b1b11SMichael Tuexen 	if (control != NULL) {
4013389b1b11SMichael Tuexen 		control->length = SCTP_BUF_LEN(m_notify);
401428cd0699SMichael Tuexen 		control->spec_flags = M_NOTIFICATION;
4015389b1b11SMichael Tuexen 		/* not that we need this */
4016389b1b11SMichael Tuexen 		control->tail_mbuf = m_notify;
4017389b1b11SMichael Tuexen 		sctp_add_to_readq(stcb->sctp_ep, stcb,
4018389b1b11SMichael Tuexen 		    control,
4019389b1b11SMichael Tuexen 		    &stcb->sctp_socket->so_rcv, 1,
4020389b1b11SMichael Tuexen 		    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
4021389b1b11SMichael Tuexen 	} else {
4022389b1b11SMichael Tuexen 		sctp_m_freem(m_notify);
4023389b1b11SMichael Tuexen 	}
4024389b1b11SMichael Tuexen }
4025389b1b11SMichael Tuexen 
4026389b1b11SMichael Tuexen 
4027f8829a4aSRandall Stewart void
4028f8829a4aSRandall Stewart sctp_ulp_notify(uint32_t notification, struct sctp_tcb *stcb,
4029*28397ac1SMichael Tuexen     uint32_t error, void *data, int so_locked)
4030f8829a4aSRandall Stewart {
4031830d754dSRandall Stewart 	if ((stcb == NULL) ||
4032830d754dSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4033f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4034830d754dSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
4035830d754dSRandall Stewart 		/* If the socket is gone we are out of here */
4036f8829a4aSRandall Stewart 		return;
4037f8829a4aSRandall Stewart 	}
4038a99b6783SRandall Stewart 	if (stcb->sctp_socket->so_rcv.sb_state & SBS_CANTRCVMORE) {
4039a99b6783SRandall Stewart 		return;
4040a99b6783SRandall Stewart 	}
4041839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
4042839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
404317205eccSRandall Stewart 		if ((notification == SCTP_NOTIFY_INTERFACE_DOWN) ||
404417205eccSRandall Stewart 		    (notification == SCTP_NOTIFY_INTERFACE_UP) ||
404517205eccSRandall Stewart 		    (notification == SCTP_NOTIFY_INTERFACE_CONFIRMED)) {
404617205eccSRandall Stewart 			/* Don't report these in front states */
404717205eccSRandall Stewart 			return;
404817205eccSRandall Stewart 		}
404917205eccSRandall Stewart 	}
4050f8829a4aSRandall Stewart 	switch (notification) {
4051f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_UP:
4052f8829a4aSRandall Stewart 		if (stcb->asoc.assoc_up_sent == 0) {
4053410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_UP, stcb, error, NULL, 0, so_locked);
4054f8829a4aSRandall Stewart 			stcb->asoc.assoc_up_sent = 1;
4055f8829a4aSRandall Stewart 		}
40562afb3e84SRandall Stewart 		if (stcb->asoc.adaptation_needed && (stcb->asoc.adaptation_sent == 0)) {
40577215cc1bSMichael Tuexen 			sctp_notify_adaptation_layer(stcb);
40582afb3e84SRandall Stewart 		}
4059c79bec9cSMichael Tuexen 		if (stcb->asoc.auth_supported == 0) {
4060830d754dSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
4061830d754dSRandall Stewart 			    NULL, so_locked);
4062830d754dSRandall Stewart 		}
4063f8829a4aSRandall Stewart 		break;
4064f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_DOWN:
4065410a3b1eSMichael Tuexen 		sctp_notify_assoc_change(SCTP_SHUTDOWN_COMP, stcb, error, NULL, 0, so_locked);
4066f8829a4aSRandall Stewart 		break;
4067f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_DOWN:
4068f8829a4aSRandall Stewart 		{
4069f8829a4aSRandall Stewart 			struct sctp_nets *net;
4070f8829a4aSRandall Stewart 
4071f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
4072f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_UNREACHABLE,
40733cb3567dSMichael Tuexen 			    (struct sockaddr *)&net->ro._l_addr, error, so_locked);
4074f8829a4aSRandall Stewart 			break;
4075f8829a4aSRandall Stewart 		}
4076f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_UP:
4077f8829a4aSRandall Stewart 		{
4078f8829a4aSRandall Stewart 			struct sctp_nets *net;
4079f8829a4aSRandall Stewart 
4080f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
4081f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_AVAILABLE,
40823cb3567dSMichael Tuexen 			    (struct sockaddr *)&net->ro._l_addr, error, so_locked);
4083f8829a4aSRandall Stewart 			break;
4084f8829a4aSRandall Stewart 		}
4085f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_CONFIRMED:
4086f8829a4aSRandall Stewart 		{
4087f8829a4aSRandall Stewart 			struct sctp_nets *net;
4088f8829a4aSRandall Stewart 
4089f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
4090f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_CONFIRMED,
40913cb3567dSMichael Tuexen 			    (struct sockaddr *)&net->ro._l_addr, error, so_locked);
4092f8829a4aSRandall Stewart 			break;
4093f8829a4aSRandall Stewart 		}
4094f8829a4aSRandall Stewart 	case SCTP_NOTIFY_SPECIAL_SP_FAIL:
4095f8829a4aSRandall Stewart 		sctp_notify_send_failed2(stcb, error,
4096ceaad40aSRandall Stewart 		    (struct sctp_stream_queue_pending *)data, so_locked);
4097f8829a4aSRandall Stewart 		break;
40981edc9dbaSMichael Tuexen 	case SCTP_NOTIFY_SENT_DG_FAIL:
40991edc9dbaSMichael Tuexen 		sctp_notify_send_failed(stcb, 1, error,
41001edc9dbaSMichael Tuexen 		    (struct sctp_tmit_chunk *)data, so_locked);
41011edc9dbaSMichael Tuexen 		break;
41021edc9dbaSMichael Tuexen 	case SCTP_NOTIFY_UNSENT_DG_FAIL:
41031edc9dbaSMichael Tuexen 		sctp_notify_send_failed(stcb, 0, error,
4104ceaad40aSRandall Stewart 		    (struct sctp_tmit_chunk *)data, so_locked);
4105f8829a4aSRandall Stewart 		break;
4106f8829a4aSRandall Stewart 	case SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION:
41079a6142d8SRandall Stewart 		{
41089a6142d8SRandall Stewart 			uint32_t val;
41099a6142d8SRandall Stewart 
41109a6142d8SRandall Stewart 			val = *((uint32_t *)data);
41119a6142d8SRandall Stewart 
4112810ec536SMichael Tuexen 			sctp_notify_partial_delivery_indication(stcb, error, val, so_locked);
4113f8829a4aSRandall Stewart 			break;
4114810ec536SMichael Tuexen 		}
4115410a3b1eSMichael Tuexen 	case SCTP_NOTIFY_ASSOC_LOC_ABORTED:
4116839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
4117839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
4118410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 0, so_locked);
4119c105859eSRandall Stewart 		} else {
4120410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 0, so_locked);
4121410a3b1eSMichael Tuexen 		}
4122410a3b1eSMichael Tuexen 		break;
4123410a3b1eSMichael Tuexen 	case SCTP_NOTIFY_ASSOC_REM_ABORTED:
4124839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
4125839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
4126410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 1, so_locked);
4127410a3b1eSMichael Tuexen 		} else {
4128410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 1, so_locked);
4129c105859eSRandall Stewart 		}
4130f8829a4aSRandall Stewart 		break;
4131f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_RESTART:
4132410a3b1eSMichael Tuexen 		sctp_notify_assoc_change(SCTP_RESTART, stcb, error, NULL, 0, so_locked);
4133c79bec9cSMichael Tuexen 		if (stcb->asoc.auth_supported == 0) {
4134830d754dSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
4135830d754dSRandall Stewart 			    NULL, so_locked);
4136830d754dSRandall Stewart 		}
4137f8829a4aSRandall Stewart 		break;
4138f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_SEND:
4139d7714577SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *)data), SCTP_STREAM_RESET_OUTGOING_SSN);
4140f8829a4aSRandall Stewart 		break;
4141f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_RECV:
4142d7714577SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *)data), SCTP_STREAM_RESET_INCOMING);
4143f8829a4aSRandall Stewart 		break;
4144f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_FAILED_OUT:
4145c4e848b7SRandall Stewart 		sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
4146d7714577SMichael Tuexen 		    (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_FAILED));
4147f8829a4aSRandall Stewart 		break;
4148d4260646SMichael Tuexen 	case SCTP_NOTIFY_STR_RESET_DENIED_OUT:
4149d4260646SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
4150d4260646SMichael Tuexen 		    (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_DENIED));
4151d4260646SMichael Tuexen 		break;
4152f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_FAILED_IN:
4153c4e848b7SRandall Stewart 		sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
4154d7714577SMichael Tuexen 		    (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_FAILED));
4155f8829a4aSRandall Stewart 		break;
4156d4260646SMichael Tuexen 	case SCTP_NOTIFY_STR_RESET_DENIED_IN:
4157d4260646SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
4158d4260646SMichael Tuexen 		    (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_DENIED));
4159d4260646SMichael Tuexen 		break;
4160f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_ADD_IP:
4161f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_ADDED, data,
41623cb3567dSMichael Tuexen 		    error, so_locked);
4163f8829a4aSRandall Stewart 		break;
4164f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_DELETE_IP:
4165f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_REMOVED, data,
41663cb3567dSMichael Tuexen 		    error, so_locked);
4167f8829a4aSRandall Stewart 		break;
4168f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_SET_PRIMARY:
4169f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_MADE_PRIM, data,
41703cb3567dSMichael Tuexen 		    error, so_locked);
4171f8829a4aSRandall Stewart 		break;
4172f8829a4aSRandall Stewart 	case SCTP_NOTIFY_PEER_SHUTDOWN:
4173f8829a4aSRandall Stewart 		sctp_notify_shutdown_event(stcb);
4174f8829a4aSRandall Stewart 		break;
4175f8829a4aSRandall Stewart 	case SCTP_NOTIFY_AUTH_NEW_KEY:
417678f28045SMichael Tuexen 		sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY, error,
4177830d754dSRandall Stewart 		    (uint16_t)(uintptr_t)data,
4178830d754dSRandall Stewart 		    so_locked);
4179f8829a4aSRandall Stewart 		break;
4180830d754dSRandall Stewart 	case SCTP_NOTIFY_AUTH_FREE_KEY:
4181830d754dSRandall Stewart 		sctp_notify_authentication(stcb, SCTP_AUTH_FREE_KEY, error,
4182830d754dSRandall Stewart 		    (uint16_t)(uintptr_t)data,
4183830d754dSRandall Stewart 		    so_locked);
4184f8829a4aSRandall Stewart 		break;
4185830d754dSRandall Stewart 	case SCTP_NOTIFY_NO_PEER_AUTH:
4186830d754dSRandall Stewart 		sctp_notify_authentication(stcb, SCTP_AUTH_NO_AUTH, error,
4187830d754dSRandall Stewart 		    (uint16_t)(uintptr_t)data,
4188830d754dSRandall Stewart 		    so_locked);
4189830d754dSRandall Stewart 		break;
4190830d754dSRandall Stewart 	case SCTP_NOTIFY_SENDER_DRY:
4191830d754dSRandall Stewart 		sctp_notify_sender_dry_event(stcb, so_locked);
4192830d754dSRandall Stewart 		break;
4193389b1b11SMichael Tuexen 	case SCTP_NOTIFY_REMOTE_ERROR:
4194389b1b11SMichael Tuexen 		sctp_notify_remote_error(stcb, error, data);
4195389b1b11SMichael Tuexen 		break;
4196f8829a4aSRandall Stewart 	default:
4197ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_UTIL1, "%s: unknown notification %xh (%u)\n",
41986e9c45e0SMichael Tuexen 		    __func__, notification, notification);
4199f8829a4aSRandall Stewart 		break;
4200f8829a4aSRandall Stewart 	}			/* end switch */
4201f8829a4aSRandall Stewart }
4202f8829a4aSRandall Stewart 
4203f8829a4aSRandall Stewart void
4204*28397ac1SMichael Tuexen sctp_report_all_outbound(struct sctp_tcb *stcb, uint16_t error, int holds_lock, int so_locked)
4205f8829a4aSRandall Stewart {
4206f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4207f8829a4aSRandall Stewart 	struct sctp_stream_out *outs;
42084a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
42094a9ef3f8SMichael Tuexen 	struct sctp_stream_queue_pending *sp, *nsp;
42107f34832bSRandall Stewart 	int i;
4211f8829a4aSRandall Stewart 
4212ad81507eSRandall Stewart 	if (stcb == NULL) {
4213ad81507eSRandall Stewart 		return;
4214ad81507eSRandall Stewart 	}
42154a9ef3f8SMichael Tuexen 	asoc = &stcb->asoc;
42164a9ef3f8SMichael Tuexen 	if (asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4217478fbccbSRandall Stewart 		/* already being freed */
4218478fbccbSRandall Stewart 		return;
4219478fbccbSRandall Stewart 	}
4220f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4221f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
42224a9ef3f8SMichael Tuexen 	    (asoc->state & SCTP_STATE_CLOSED_SOCKET)) {
4223f8829a4aSRandall Stewart 		return;
4224f8829a4aSRandall Stewart 	}
4225f8829a4aSRandall Stewart 	/* now through all the gunk freeing chunks */
4226ad81507eSRandall Stewart 	if (holds_lock == 0) {
42277f34832bSRandall Stewart 		SCTP_TCB_SEND_LOCK(stcb);
4228ad81507eSRandall Stewart 	}
4229d00aff5dSRandall Stewart 	/* sent queue SHOULD be empty */
42304a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
4231d00aff5dSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
4232d00aff5dSRandall Stewart 		asoc->sent_queue_cnt--;
4233325c8c46SMichael Tuexen 		if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
423449656eefSMichael Tuexen 			if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
423549656eefSMichael Tuexen 				asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
4236a7ad6026SMichael Tuexen #ifdef INVARIANTS
4237a7ad6026SMichael Tuexen 			} else {
423849656eefSMichael Tuexen 				panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
4239a7ad6026SMichael Tuexen #endif
4240a7ad6026SMichael Tuexen 			}
4241a7ad6026SMichael Tuexen 		}
42420c0982b8SRandall Stewart 		if (chk->data != NULL) {
4243d00aff5dSRandall Stewart 			sctp_free_bufspace(stcb, asoc, chk, 1);
42441edc9dbaSMichael Tuexen 			sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
42451edc9dbaSMichael Tuexen 			    error, chk, so_locked);
4246810ec536SMichael Tuexen 			if (chk->data) {
4247d00aff5dSRandall Stewart 				sctp_m_freem(chk->data);
4248d00aff5dSRandall Stewart 				chk->data = NULL;
4249d00aff5dSRandall Stewart 			}
4250810ec536SMichael Tuexen 		}
4251689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, so_locked);
4252d00aff5dSRandall Stewart 		/* sa_ignore FREED_MEMORY */
4253d00aff5dSRandall Stewart 	}
4254d00aff5dSRandall Stewart 	/* pending send queue SHOULD be empty */
42554a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
4256d00aff5dSRandall Stewart 		TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
4257d00aff5dSRandall Stewart 		asoc->send_queue_cnt--;
425849656eefSMichael Tuexen 		if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
425949656eefSMichael Tuexen 			asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
4260a7ad6026SMichael Tuexen #ifdef INVARIANTS
4261a7ad6026SMichael Tuexen 		} else {
426249656eefSMichael Tuexen 			panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
4263a7ad6026SMichael Tuexen #endif
4264a7ad6026SMichael Tuexen 		}
42650c0982b8SRandall Stewart 		if (chk->data != NULL) {
4266d00aff5dSRandall Stewart 			sctp_free_bufspace(stcb, asoc, chk, 1);
42671edc9dbaSMichael Tuexen 			sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
42681edc9dbaSMichael Tuexen 			    error, chk, so_locked);
4269810ec536SMichael Tuexen 			if (chk->data) {
4270d00aff5dSRandall Stewart 				sctp_m_freem(chk->data);
4271d00aff5dSRandall Stewart 				chk->data = NULL;
4272d00aff5dSRandall Stewart 			}
4273810ec536SMichael Tuexen 		}
4274689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, so_locked);
4275d00aff5dSRandall Stewart 		/* sa_ignore FREED_MEMORY */
4276d00aff5dSRandall Stewart 	}
42774a9ef3f8SMichael Tuexen 	for (i = 0; i < asoc->streamoutcnt; i++) {
42787f34832bSRandall Stewart 		/* For each stream */
42794a9ef3f8SMichael Tuexen 		outs = &asoc->strmout[i];
42807f34832bSRandall Stewart 		/* clean up any sends there */
42814a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
42824d58b0c3SMichael Tuexen 			atomic_subtract_int(&asoc->stream_queue_cnt, 1);
4283f8829a4aSRandall Stewart 			TAILQ_REMOVE(&outs->outqueue, sp, next);
4284d9707e43SMichael Tuexen 			stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, 1);
4285f8829a4aSRandall Stewart 			sctp_free_spbufspace(stcb, asoc, sp);
4286478fbccbSRandall Stewart 			if (sp->data) {
4287f8829a4aSRandall Stewart 				sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
42881edc9dbaSMichael Tuexen 				    error, (void *)sp, so_locked);
4289f8829a4aSRandall Stewart 				if (sp->data) {
4290f8829a4aSRandall Stewart 					sctp_m_freem(sp->data);
4291f8829a4aSRandall Stewart 					sp->data = NULL;
4292d07b2ac6SMichael Tuexen 					sp->tail_mbuf = NULL;
4293d07b2ac6SMichael Tuexen 					sp->length = 0;
4294f8829a4aSRandall Stewart 				}
4295478fbccbSRandall Stewart 			}
42969eea4a2dSMichael Tuexen 			if (sp->net) {
4297f8829a4aSRandall Stewart 				sctp_free_remote_addr(sp->net);
4298f8829a4aSRandall Stewart 				sp->net = NULL;
42999eea4a2dSMichael Tuexen 			}
4300f8829a4aSRandall Stewart 			/* Free the chunk */
4301689e6a5fSMichael Tuexen 			sctp_free_a_strmoq(stcb, sp, so_locked);
43023c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
4303f8829a4aSRandall Stewart 		}
4304f8829a4aSRandall Stewart 	}
4305f8829a4aSRandall Stewart 
4306ad81507eSRandall Stewart 	if (holds_lock == 0) {
43077f34832bSRandall Stewart 		SCTP_TCB_SEND_UNLOCK(stcb);
4308f8829a4aSRandall Stewart 	}
4309ad81507eSRandall Stewart }
4310f8829a4aSRandall Stewart 
4311f8829a4aSRandall Stewart void
4312410a3b1eSMichael Tuexen sctp_abort_notification(struct sctp_tcb *stcb, uint8_t from_peer, uint16_t error,
4313*28397ac1SMichael Tuexen     struct sctp_abort_chunk *abort, int so_locked)
4314f8829a4aSRandall Stewart {
4315ad81507eSRandall Stewart 	if (stcb == NULL) {
4316ad81507eSRandall Stewart 		return;
4317ad81507eSRandall Stewart 	}
4318c55b70ceSMichael Tuexen 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4319c55b70ceSMichael Tuexen 	    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4320c55b70ceSMichael Tuexen 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
4321c55b70ceSMichael Tuexen 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_WAS_ABORTED;
4322c55b70ceSMichael Tuexen 	}
4323f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4324f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4325f8829a4aSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
4326f8829a4aSRandall Stewart 		return;
4327f8829a4aSRandall Stewart 	}
4328f8829a4aSRandall Stewart 	/* Tell them we lost the asoc */
43296982c0faSMichael Tuexen 	sctp_report_all_outbound(stcb, error, 0, so_locked);
4330410a3b1eSMichael Tuexen 	if (from_peer) {
4331410a3b1eSMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_REM_ABORTED, stcb, error, abort, so_locked);
4332410a3b1eSMichael Tuexen 	} else {
4333410a3b1eSMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_LOC_ABORTED, stcb, error, abort, so_locked);
4334410a3b1eSMichael Tuexen 	}
4335f8829a4aSRandall Stewart }
4336f8829a4aSRandall Stewart 
4337f8829a4aSRandall Stewart void
4338f8829a4aSRandall Stewart sctp_abort_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4339b1754ad1SMichael Tuexen     struct mbuf *m, int iphlen,
4340b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
4341b1754ad1SMichael Tuexen     struct sctphdr *sh, struct mbuf *op_err,
4342457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
4343c54a18d2SRandall Stewart     uint32_t vrf_id, uint16_t port)
4344f8829a4aSRandall Stewart {
4345f8829a4aSRandall Stewart 	uint32_t vtag;
4346ceaad40aSRandall Stewart 
4347f8829a4aSRandall Stewart 	vtag = 0;
4348f8829a4aSRandall Stewart 	if (stcb != NULL) {
4349f8829a4aSRandall Stewart 		vtag = stcb->asoc.peer_vtag;
435017205eccSRandall Stewart 		vrf_id = stcb->asoc.vrf_id;
4351f8829a4aSRandall Stewart 	}
4352b1754ad1SMichael Tuexen 	sctp_send_abort(m, iphlen, src, dst, sh, vtag, op_err,
4353d089f9b9SMichael Tuexen 	    mflowtype, mflowid, inp->fibnum,
4354f30ac432SMichael Tuexen 	    vrf_id, port);
4355f8829a4aSRandall Stewart 	if (stcb != NULL) {
4356884d8c53SMichael Tuexen 		/* We have a TCB to abort, send notification too */
4357884d8c53SMichael Tuexen 		sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
4358839d21d6SMichael Tuexen 		SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_WAS_ABORTED);
4359f8829a4aSRandall Stewart 		/* Ok, now lets free it */
43600271d0cdSMichael Tuexen 		SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4361839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
4362839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
43630271d0cdSMichael Tuexen 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
43640271d0cdSMichael Tuexen 		}
4365ba785902SMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4366ba785902SMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_4);
4367f8829a4aSRandall Stewart 	}
4368f8829a4aSRandall Stewart }
4369f1f73e57SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
4370f1f73e57SRandall Stewart void
4371f1f73e57SRandall Stewart sctp_print_out_track_log(struct sctp_tcb *stcb)
4372f1f73e57SRandall Stewart {
437318e198d3SRandall Stewart #ifdef NOSIY_PRINTS
4374f1f73e57SRandall Stewart 	int i;
4375f1f73e57SRandall Stewart 
4376ad81507eSRandall Stewart 	SCTP_PRINTF("Last ep reason:%x\n", stcb->sctp_ep->last_abort_code);
4377ad81507eSRandall Stewart 	SCTP_PRINTF("IN bound TSN log-aaa\n");
4378f1f73e57SRandall Stewart 	if ((stcb->asoc.tsn_in_at == 0) && (stcb->asoc.tsn_in_wrapped == 0)) {
4379ad81507eSRandall Stewart 		SCTP_PRINTF("None rcvd\n");
4380f1f73e57SRandall Stewart 		goto none_in;
4381f1f73e57SRandall Stewart 	}
4382f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_in_wrapped) {
4383f1f73e57SRandall Stewart 		for (i = stcb->asoc.tsn_in_at; i < SCTP_TSN_LOG_SIZE; i++) {
4384ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4385f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].tsn,
4386f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].strm,
4387f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].seq,
4388f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].flgs,
4389f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].sz);
4390f1f73e57SRandall Stewart 		}
4391f1f73e57SRandall Stewart 	}
4392f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_in_at) {
4393f1f73e57SRandall Stewart 		for (i = 0; i < stcb->asoc.tsn_in_at; i++) {
4394ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4395f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].tsn,
4396f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].strm,
4397f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].seq,
4398f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].flgs,
4399f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].sz);
4400f1f73e57SRandall Stewart 		}
4401f1f73e57SRandall Stewart 	}
4402f1f73e57SRandall Stewart none_in:
4403ad81507eSRandall Stewart 	SCTP_PRINTF("OUT bound TSN log-aaa\n");
4404ad81507eSRandall Stewart 	if ((stcb->asoc.tsn_out_at == 0) &&
4405ad81507eSRandall Stewart 	    (stcb->asoc.tsn_out_wrapped == 0)) {
4406ad81507eSRandall Stewart 		SCTP_PRINTF("None sent\n");
4407f1f73e57SRandall Stewart 	}
4408f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_out_wrapped) {
4409f1f73e57SRandall Stewart 		for (i = stcb->asoc.tsn_out_at; i < SCTP_TSN_LOG_SIZE; i++) {
4410ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4411f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].tsn,
4412f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].strm,
4413f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].seq,
4414f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].flgs,
4415f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].sz);
4416f1f73e57SRandall Stewart 		}
4417f1f73e57SRandall Stewart 	}
4418f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_out_at) {
4419f1f73e57SRandall Stewart 		for (i = 0; i < stcb->asoc.tsn_out_at; i++) {
4420ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4421f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].tsn,
4422f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].strm,
4423f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].seq,
4424f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].flgs,
4425f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].sz);
4426f1f73e57SRandall Stewart 		}
4427f1f73e57SRandall Stewart 	}
442818e198d3SRandall Stewart #endif
4429f1f73e57SRandall Stewart }
4430f1f73e57SRandall Stewart #endif
4431f1f73e57SRandall Stewart 
4432f8829a4aSRandall Stewart void
4433f8829a4aSRandall Stewart sctp_abort_an_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4434a2b42326SMichael Tuexen     struct mbuf *op_err,
4435*28397ac1SMichael Tuexen     int so_locked)
4436f8829a4aSRandall Stewart {
4437ceaad40aSRandall Stewart 
4438f8829a4aSRandall Stewart 	if (stcb == NULL) {
4439f8829a4aSRandall Stewart 		/* Got to have a TCB */
4440f8829a4aSRandall Stewart 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4441fe1831e0SMichael Tuexen 			if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4442b0552ae2SRandall Stewart 				sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4443b0552ae2SRandall Stewart 				    SCTP_CALLED_DIRECTLY_NOCMPSET);
4444f8829a4aSRandall Stewart 			}
4445f8829a4aSRandall Stewart 		}
4446f8829a4aSRandall Stewart 		return;
444763981c2bSRandall Stewart 	} else {
4448839d21d6SMichael Tuexen 		SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_WAS_ABORTED);
4449f8829a4aSRandall Stewart 	}
4450f8829a4aSRandall Stewart 	/* notify the peer */
4451ceaad40aSRandall Stewart 	sctp_send_abort_tcb(stcb, op_err, so_locked);
4452f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4453839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
4454839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4455f8829a4aSRandall Stewart 		SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4456f8829a4aSRandall Stewart 	}
4457884d8c53SMichael Tuexen 	/* notify the ulp */
4458884d8c53SMichael Tuexen 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
4459884d8c53SMichael Tuexen 		sctp_abort_notification(stcb, 0, 0, NULL, so_locked);
4460884d8c53SMichael Tuexen 	}
4461f8829a4aSRandall Stewart 	/* now free the asoc */
4462f1f73e57SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
4463f1f73e57SRandall Stewart 	sctp_print_out_track_log(stcb);
4464f1f73e57SRandall Stewart #endif
4465ba785902SMichael Tuexen 	(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4466ba785902SMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_5);
4467f8829a4aSRandall Stewart }
4468f8829a4aSRandall Stewart 
4469f8829a4aSRandall Stewart void
4470b1754ad1SMichael Tuexen sctp_handle_ootb(struct mbuf *m, int iphlen, int offset,
4471b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
4472b1754ad1SMichael Tuexen     struct sctphdr *sh, struct sctp_inpcb *inp,
4473ff1ffd74SMichael Tuexen     struct mbuf *cause,
4474d089f9b9SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4475f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
4476f8829a4aSRandall Stewart {
4477f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch, chunk_buf;
4478f8829a4aSRandall Stewart 	unsigned int chk_length;
4479c58e60beSMichael Tuexen 	int contains_init_chunk;
4480f8829a4aSRandall Stewart 
4481f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue);
4482f8829a4aSRandall Stewart 	/* Generate a TO address for future reference */
4483f8829a4aSRandall Stewart 	if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4484fe1831e0SMichael Tuexen 		if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4485b0552ae2SRandall Stewart 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4486b0552ae2SRandall Stewart 			    SCTP_CALLED_DIRECTLY_NOCMPSET);
4487f8829a4aSRandall Stewart 		}
4488f8829a4aSRandall Stewart 	}
4489c58e60beSMichael Tuexen 	contains_init_chunk = 0;
4490f8829a4aSRandall Stewart 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4491f8829a4aSRandall Stewart 	    sizeof(*ch), (uint8_t *)&chunk_buf);
4492f8829a4aSRandall Stewart 	while (ch != NULL) {
4493f8829a4aSRandall Stewart 		chk_length = ntohs(ch->chunk_length);
4494f8829a4aSRandall Stewart 		if (chk_length < sizeof(*ch)) {
4495f8829a4aSRandall Stewart 			/* break to abort land */
4496f8829a4aSRandall Stewart 			break;
4497f8829a4aSRandall Stewart 		}
4498f8829a4aSRandall Stewart 		switch (ch->chunk_type) {
4499c58e60beSMichael Tuexen 		case SCTP_INIT:
4500c58e60beSMichael Tuexen 			contains_init_chunk = 1;
4501c58e60beSMichael Tuexen 			break;
4502f8829a4aSRandall Stewart 		case SCTP_PACKET_DROPPED:
4503f8829a4aSRandall Stewart 			/* we don't respond to pkt-dropped */
4504f8829a4aSRandall Stewart 			return;
4505f8829a4aSRandall Stewart 		case SCTP_ABORT_ASSOCIATION:
4506f8829a4aSRandall Stewart 			/* we don't respond with an ABORT to an ABORT */
4507f8829a4aSRandall Stewart 			return;
4508f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_COMPLETE:
4509f8829a4aSRandall Stewart 			/*
4510f8829a4aSRandall Stewart 			 * we ignore it since we are not waiting for it and
4511f8829a4aSRandall Stewart 			 * peer is gone
4512f8829a4aSRandall Stewart 			 */
4513f8829a4aSRandall Stewart 			return;
4514f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_ACK:
4515b1754ad1SMichael Tuexen 			sctp_send_shutdown_complete2(src, dst, sh,
4516d089f9b9SMichael Tuexen 			    mflowtype, mflowid, fibnum,
4517f30ac432SMichael Tuexen 			    vrf_id, port);
4518f8829a4aSRandall Stewart 			return;
4519f8829a4aSRandall Stewart 		default:
4520f8829a4aSRandall Stewart 			break;
4521f8829a4aSRandall Stewart 		}
4522f8829a4aSRandall Stewart 		offset += SCTP_SIZE32(chk_length);
4523f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4524f8829a4aSRandall Stewart 		    sizeof(*ch), (uint8_t *)&chunk_buf);
4525f8829a4aSRandall Stewart 	}
4526c58e60beSMichael Tuexen 	if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
4527c58e60beSMichael Tuexen 	    ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
4528c58e60beSMichael Tuexen 	    (contains_init_chunk == 0))) {
4529ff1ffd74SMichael Tuexen 		sctp_send_abort(m, iphlen, src, dst, sh, 0, cause,
4530d089f9b9SMichael Tuexen 		    mflowtype, mflowid, fibnum,
4531f30ac432SMichael Tuexen 		    vrf_id, port);
4532f8829a4aSRandall Stewart 	}
4533c58e60beSMichael Tuexen }
4534f8829a4aSRandall Stewart 
4535f8829a4aSRandall Stewart /*
4536f8829a4aSRandall Stewart  * check the inbound datagram to make sure there is not an abort inside it,
4537f8829a4aSRandall Stewart  * if there is return 1, else return 0.
4538f8829a4aSRandall Stewart  */
4539f8829a4aSRandall Stewart int
4540f8829a4aSRandall Stewart sctp_is_there_an_abort_here(struct mbuf *m, int iphlen, uint32_t *vtagfill)
4541f8829a4aSRandall Stewart {
4542f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
4543f8829a4aSRandall Stewart 	struct sctp_init_chunk *init_chk, chunk_buf;
4544f8829a4aSRandall Stewart 	int offset;
4545f8829a4aSRandall Stewart 	unsigned int chk_length;
4546f8829a4aSRandall Stewart 
4547f8829a4aSRandall Stewart 	offset = iphlen + sizeof(struct sctphdr);
4548f8829a4aSRandall Stewart 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch),
4549f8829a4aSRandall Stewart 	    (uint8_t *)&chunk_buf);
4550f8829a4aSRandall Stewart 	while (ch != NULL) {
4551f8829a4aSRandall Stewart 		chk_length = ntohs(ch->chunk_length);
4552f8829a4aSRandall Stewart 		if (chk_length < sizeof(*ch)) {
4553f8829a4aSRandall Stewart 			/* packet is probably corrupt */
4554f8829a4aSRandall Stewart 			break;
4555f8829a4aSRandall Stewart 		}
4556f8829a4aSRandall Stewart 		/* we seem to be ok, is it an abort? */
4557f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) {
4558f8829a4aSRandall Stewart 			/* yep, tell them */
4559f8829a4aSRandall Stewart 			return (1);
4560f8829a4aSRandall Stewart 		}
4561f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_INITIATION) {
4562f8829a4aSRandall Stewart 			/* need to update the Vtag */
4563f8829a4aSRandall Stewart 			init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4564f8829a4aSRandall Stewart 			    offset, sizeof(*init_chk), (uint8_t *)&chunk_buf);
4565f8829a4aSRandall Stewart 			if (init_chk != NULL) {
4566f8829a4aSRandall Stewart 				*vtagfill = ntohl(init_chk->init.initiate_tag);
4567f8829a4aSRandall Stewart 			}
4568f8829a4aSRandall Stewart 		}
4569f8829a4aSRandall Stewart 		/* Nope, move to the next chunk */
4570f8829a4aSRandall Stewart 		offset += SCTP_SIZE32(chk_length);
4571f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4572f8829a4aSRandall Stewart 		    sizeof(*ch), (uint8_t *)&chunk_buf);
4573f8829a4aSRandall Stewart 	}
4574f8829a4aSRandall Stewart 	return (0);
4575f8829a4aSRandall Stewart }
4576f8829a4aSRandall Stewart 
4577f8829a4aSRandall Stewart /*
4578f8829a4aSRandall Stewart  * currently (2/02), ifa_addr embeds scope_id's and don't have sin6_scope_id
4579f8829a4aSRandall Stewart  * set (i.e. it's 0) so, create this function to compare link local scopes
4580f8829a4aSRandall Stewart  */
45815e2c2d87SRandall Stewart #ifdef INET6
4582f8829a4aSRandall Stewart uint32_t
4583b0471b4bSMichael Tuexen sctp_is_same_scope(struct sockaddr_in6 *addr1, struct sockaddr_in6 *addr2)
4584b0471b4bSMichael Tuexen {
4585f8829a4aSRandall Stewart 	struct sockaddr_in6 a, b;
4586f8829a4aSRandall Stewart 
4587f8829a4aSRandall Stewart 	/* save copies */
4588f8829a4aSRandall Stewart 	a = *addr1;
4589f8829a4aSRandall Stewart 	b = *addr2;
4590f8829a4aSRandall Stewart 
4591f8829a4aSRandall Stewart 	if (a.sin6_scope_id == 0)
4592f8829a4aSRandall Stewart 		if (sa6_recoverscope(&a)) {
4593f8829a4aSRandall Stewart 			/* can't get scope, so can't match */
4594f8829a4aSRandall Stewart 			return (0);
4595f8829a4aSRandall Stewart 		}
4596f8829a4aSRandall Stewart 	if (b.sin6_scope_id == 0)
4597f8829a4aSRandall Stewart 		if (sa6_recoverscope(&b)) {
4598f8829a4aSRandall Stewart 			/* can't get scope, so can't match */
4599f8829a4aSRandall Stewart 			return (0);
4600f8829a4aSRandall Stewart 		}
4601f8829a4aSRandall Stewart 	if (a.sin6_scope_id != b.sin6_scope_id)
4602f8829a4aSRandall Stewart 		return (0);
4603f8829a4aSRandall Stewart 
4604f8829a4aSRandall Stewart 	return (1);
4605f8829a4aSRandall Stewart }
4606f8829a4aSRandall Stewart 
4607f8829a4aSRandall Stewart /*
4608f8829a4aSRandall Stewart  * returns a sockaddr_in6 with embedded scope recovered and removed
4609f8829a4aSRandall Stewart  */
4610f8829a4aSRandall Stewart struct sockaddr_in6 *
4611f8829a4aSRandall Stewart sctp_recover_scope(struct sockaddr_in6 *addr, struct sockaddr_in6 *store)
4612f8829a4aSRandall Stewart {
4613f8829a4aSRandall Stewart 	/* check and strip embedded scope junk */
4614f8829a4aSRandall Stewart 	if (addr->sin6_family == AF_INET6) {
4615f8829a4aSRandall Stewart 		if (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr)) {
4616f8829a4aSRandall Stewart 			if (addr->sin6_scope_id == 0) {
4617f8829a4aSRandall Stewart 				*store = *addr;
4618f8829a4aSRandall Stewart 				if (!sa6_recoverscope(store)) {
4619f8829a4aSRandall Stewart 					/* use the recovered scope */
4620f8829a4aSRandall Stewart 					addr = store;
4621f8829a4aSRandall Stewart 				}
4622f42a358aSRandall Stewart 			} else {
4623f8829a4aSRandall Stewart 				/* else, return the original "to" addr */
4624f42a358aSRandall Stewart 				in6_clearscope(&addr->sin6_addr);
4625f8829a4aSRandall Stewart 			}
4626f8829a4aSRandall Stewart 		}
4627f8829a4aSRandall Stewart 	}
4628f8829a4aSRandall Stewart 	return (addr);
4629f8829a4aSRandall Stewart }
46305e2c2d87SRandall Stewart #endif
46315e2c2d87SRandall Stewart 
4632f8829a4aSRandall Stewart /*
4633f8829a4aSRandall Stewart  * are the two addresses the same?  currently a "scopeless" check returns: 1
4634f8829a4aSRandall Stewart  * if same, 0 if not
4635f8829a4aSRandall Stewart  */
463672fb6fdbSRandall Stewart int
4637f8829a4aSRandall Stewart sctp_cmpaddr(struct sockaddr *sa1, struct sockaddr *sa2)
4638f8829a4aSRandall Stewart {
4639f8829a4aSRandall Stewart 
4640f8829a4aSRandall Stewart 	/* must be valid */
4641f8829a4aSRandall Stewart 	if (sa1 == NULL || sa2 == NULL)
4642f8829a4aSRandall Stewart 		return (0);
4643f8829a4aSRandall Stewart 
4644f8829a4aSRandall Stewart 	/* must be the same family */
4645f8829a4aSRandall Stewart 	if (sa1->sa_family != sa2->sa_family)
4646f8829a4aSRandall Stewart 		return (0);
4647f8829a4aSRandall Stewart 
46485e2c2d87SRandall Stewart 	switch (sa1->sa_family) {
46495e2c2d87SRandall Stewart #ifdef INET6
46505e2c2d87SRandall Stewart 	case AF_INET6:
46515e2c2d87SRandall Stewart 		{
4652f8829a4aSRandall Stewart 			/* IPv6 addresses */
4653f8829a4aSRandall Stewart 			struct sockaddr_in6 *sin6_1, *sin6_2;
4654f8829a4aSRandall Stewart 
4655f8829a4aSRandall Stewart 			sin6_1 = (struct sockaddr_in6 *)sa1;
4656f8829a4aSRandall Stewart 			sin6_2 = (struct sockaddr_in6 *)sa2;
4657c54a18d2SRandall Stewart 			return (SCTP6_ARE_ADDR_EQUAL(sin6_1,
4658c54a18d2SRandall Stewart 			    sin6_2));
46595e2c2d87SRandall Stewart 		}
46605e2c2d87SRandall Stewart #endif
4661ea5eba11SMichael Tuexen #ifdef INET
46625e2c2d87SRandall Stewart 	case AF_INET:
46635e2c2d87SRandall Stewart 		{
4664f8829a4aSRandall Stewart 			/* IPv4 addresses */
4665f8829a4aSRandall Stewart 			struct sockaddr_in *sin_1, *sin_2;
4666f8829a4aSRandall Stewart 
4667f8829a4aSRandall Stewart 			sin_1 = (struct sockaddr_in *)sa1;
4668f8829a4aSRandall Stewart 			sin_2 = (struct sockaddr_in *)sa2;
4669f8829a4aSRandall Stewart 			return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr);
46705e2c2d87SRandall Stewart 		}
4671ea5eba11SMichael Tuexen #endif
46725e2c2d87SRandall Stewart 	default:
4673f8829a4aSRandall Stewart 		/* we don't do these... */
4674f8829a4aSRandall Stewart 		return (0);
4675f8829a4aSRandall Stewart 	}
4676f8829a4aSRandall Stewart }
4677f8829a4aSRandall Stewart 
4678f8829a4aSRandall Stewart void
4679f8829a4aSRandall Stewart sctp_print_address(struct sockaddr *sa)
4680f8829a4aSRandall Stewart {
46815e2c2d87SRandall Stewart #ifdef INET6
46827d32aa0cSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
46835e2c2d87SRandall Stewart #endif
46845e2c2d87SRandall Stewart 
46855e2c2d87SRandall Stewart 	switch (sa->sa_family) {
46865e2c2d87SRandall Stewart #ifdef INET6
46875e2c2d87SRandall Stewart 	case AF_INET6:
46885e2c2d87SRandall Stewart 		{
4689ad81507eSRandall Stewart 			struct sockaddr_in6 *sin6;
4690ad81507eSRandall Stewart 
4691f8829a4aSRandall Stewart 			sin6 = (struct sockaddr_in6 *)sa;
4692ad81507eSRandall Stewart 			SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n",
46937d32aa0cSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &sin6->sin6_addr),
46947d32aa0cSBjoern A. Zeeb 			    ntohs(sin6->sin6_port),
4695f8829a4aSRandall Stewart 			    sin6->sin6_scope_id);
46965e2c2d87SRandall Stewart 			break;
46975e2c2d87SRandall Stewart 		}
46985e2c2d87SRandall Stewart #endif
4699ea5eba11SMichael Tuexen #ifdef INET
47005e2c2d87SRandall Stewart 	case AF_INET:
47015e2c2d87SRandall Stewart 		{
4702f8829a4aSRandall Stewart 			struct sockaddr_in *sin;
4703f8829a4aSRandall Stewart 			unsigned char *p;
4704f8829a4aSRandall Stewart 
4705f8829a4aSRandall Stewart 			sin = (struct sockaddr_in *)sa;
4706f8829a4aSRandall Stewart 			p = (unsigned char *)&sin->sin_addr;
4707ad81507eSRandall Stewart 			SCTP_PRINTF("IPv4 address: %u.%u.%u.%u:%d\n",
4708f8829a4aSRandall Stewart 			    p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
47095e2c2d87SRandall Stewart 			break;
47105e2c2d87SRandall Stewart 		}
4711ea5eba11SMichael Tuexen #endif
47125e2c2d87SRandall Stewart 	default:
4713ad81507eSRandall Stewart 		SCTP_PRINTF("?\n");
47145e2c2d87SRandall Stewart 		break;
4715f8829a4aSRandall Stewart 	}
4716f8829a4aSRandall Stewart }
4717f8829a4aSRandall Stewart 
4718f8829a4aSRandall Stewart void
4719f8829a4aSRandall Stewart sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
4720f8829a4aSRandall Stewart     struct sctp_inpcb *new_inp,
4721d06c82f1SRandall Stewart     struct sctp_tcb *stcb,
4722d06c82f1SRandall Stewart     int waitflags)
4723f8829a4aSRandall Stewart {
4724f8829a4aSRandall Stewart 	/*
4725f8829a4aSRandall Stewart 	 * go through our old INP and pull off any control structures that
4726f8829a4aSRandall Stewart 	 * belong to stcb and move then to the new inp.
4727f8829a4aSRandall Stewart 	 */
4728f8829a4aSRandall Stewart 	struct socket *old_so, *new_so;
4729f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control, *nctl;
4730f8829a4aSRandall Stewart 	struct sctp_readhead tmp_queue;
4731f8829a4aSRandall Stewart 	struct mbuf *m;
4732bff64a4dSRandall Stewart 	int error = 0;
4733f8829a4aSRandall Stewart 
4734f8829a4aSRandall Stewart 	old_so = old_inp->sctp_socket;
4735f8829a4aSRandall Stewart 	new_so = new_inp->sctp_socket;
4736f8829a4aSRandall Stewart 	TAILQ_INIT(&tmp_queue);
4737d06c82f1SRandall Stewart 	error = sblock(&old_so->so_rcv, waitflags);
4738f8829a4aSRandall Stewart 	if (error) {
4739f8829a4aSRandall Stewart 		/*
4740f8829a4aSRandall Stewart 		 * Gak, can't get sblock, we have a problem. data will be
4741f8829a4aSRandall Stewart 		 * left stranded.. and we don't dare look at it since the
4742f8829a4aSRandall Stewart 		 * other thread may be reading something. Oh well, its a
4743f8829a4aSRandall Stewart 		 * screwed up app that does a peeloff OR a accept while
4744f8829a4aSRandall Stewart 		 * reading from the main socket... actually its only the
4745f8829a4aSRandall Stewart 		 * peeloff() case, since I think read will fail on a
4746f8829a4aSRandall Stewart 		 * listening socket..
4747f8829a4aSRandall Stewart 		 */
4748f8829a4aSRandall Stewart 		return;
4749f8829a4aSRandall Stewart 	}
4750f8829a4aSRandall Stewart 	/* lock the socket buffers */
4751f8829a4aSRandall Stewart 	SCTP_INP_READ_LOCK(old_inp);
47524a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(control, &old_inp->read_queue, next, nctl) {
4753f8829a4aSRandall Stewart 		/* Pull off all for out target stcb */
4754f8829a4aSRandall Stewart 		if (control->stcb == stcb) {
4755f8829a4aSRandall Stewart 			/* remove it we want it */
4756f8829a4aSRandall Stewart 			TAILQ_REMOVE(&old_inp->read_queue, control, next);
4757f8829a4aSRandall Stewart 			TAILQ_INSERT_TAIL(&tmp_queue, control, next);
4758f8829a4aSRandall Stewart 			m = control->data;
4759f8829a4aSRandall Stewart 			while (m) {
4760b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4761139bc87fSRandall Stewart 					sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
476280fefe0aSRandall Stewart 				}
4763f8829a4aSRandall Stewart 				sctp_sbfree(control, stcb, &old_so->so_rcv, m);
4764b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4765f8829a4aSRandall Stewart 					sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
476680fefe0aSRandall Stewart 				}
4767139bc87fSRandall Stewart 				m = SCTP_BUF_NEXT(m);
4768f8829a4aSRandall Stewart 			}
4769f8829a4aSRandall Stewart 		}
4770f8829a4aSRandall Stewart 	}
4771f8829a4aSRandall Stewart 	SCTP_INP_READ_UNLOCK(old_inp);
4772f8829a4aSRandall Stewart 	/* Remove the sb-lock on the old socket */
4773f8829a4aSRandall Stewart 
4774f8829a4aSRandall Stewart 	sbunlock(&old_so->so_rcv);
4775f8829a4aSRandall Stewart 	/* Now we move them over to the new socket buffer */
4776f8829a4aSRandall Stewart 	SCTP_INP_READ_LOCK(new_inp);
47774a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(control, &tmp_queue, next, nctl) {
4778f8829a4aSRandall Stewart 		TAILQ_INSERT_TAIL(&new_inp->read_queue, control, next);
4779f8829a4aSRandall Stewart 		m = control->data;
4780f8829a4aSRandall Stewart 		while (m) {
4781b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4782139bc87fSRandall Stewart 				sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
478380fefe0aSRandall Stewart 			}
4784f8829a4aSRandall Stewart 			sctp_sballoc(stcb, &new_so->so_rcv, m);
4785b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4786f8829a4aSRandall Stewart 				sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
478780fefe0aSRandall Stewart 			}
4788139bc87fSRandall Stewart 			m = SCTP_BUF_NEXT(m);
4789f8829a4aSRandall Stewart 		}
4790f8829a4aSRandall Stewart 	}
4791f8829a4aSRandall Stewart 	SCTP_INP_READ_UNLOCK(new_inp);
4792f8829a4aSRandall Stewart }
4793f8829a4aSRandall Stewart 
4794f8829a4aSRandall Stewart void
4795b1deed45SMichael Tuexen sctp_wakeup_the_read_socket(struct sctp_inpcb *inp,
4796b1deed45SMichael Tuexen     struct sctp_tcb *stcb,
4797b1deed45SMichael Tuexen     int so_locked
4798b1deed45SMichael Tuexen     SCTP_UNUSED
4799b1deed45SMichael Tuexen )
480044249214SRandall Stewart {
4801b1deed45SMichael Tuexen 	if ((inp != NULL) && (inp->sctp_socket != NULL)) {
480244249214SRandall Stewart 		sctp_sorwakeup(inp, inp->sctp_socket);
480344249214SRandall Stewart 	}
480444249214SRandall Stewart }
480544249214SRandall Stewart 
480644249214SRandall Stewart void
4807f8829a4aSRandall Stewart sctp_add_to_readq(struct sctp_inpcb *inp,
4808f8829a4aSRandall Stewart     struct sctp_tcb *stcb,
4809f8829a4aSRandall Stewart     struct sctp_queued_to_read *control,
4810f8829a4aSRandall Stewart     struct sockbuf *sb,
4811ceaad40aSRandall Stewart     int end,
4812cfde3ff7SRandall Stewart     int inp_read_lock_held,
4813*28397ac1SMichael Tuexen     int so_locked)
4814f8829a4aSRandall Stewart {
4815f8829a4aSRandall Stewart 	/*
4816f8829a4aSRandall Stewart 	 * Here we must place the control on the end of the socket read
48174e88d37aSMichael Tuexen 	 * queue AND increment sb_cc so that select will work properly on
4818f8829a4aSRandall Stewart 	 * read.
4819f8829a4aSRandall Stewart 	 */
4820f8829a4aSRandall Stewart 	struct mbuf *m, *prev = NULL;
4821f8829a4aSRandall Stewart 
482203b0b021SRandall Stewart 	if (inp == NULL) {
482303b0b021SRandall Stewart 		/* Gak, TSNH!! */
4824a5d547adSRandall Stewart #ifdef INVARIANTS
482503b0b021SRandall Stewart 		panic("Gak, inp NULL on add_to_readq");
482603b0b021SRandall Stewart #endif
482703b0b021SRandall Stewart 		return;
482803b0b021SRandall Stewart 	}
4829cfde3ff7SRandall Stewart 	if (inp_read_lock_held == 0)
4830f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(inp);
4831cd1386abSMichael Tuexen 	if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
48328a3cfbffSMichael Tuexen 		if (!control->on_strm_q) {
4833cd1386abSMichael Tuexen 			sctp_free_remote_addr(control->whoFrom);
4834cd1386abSMichael Tuexen 			if (control->data) {
4835cd1386abSMichael Tuexen 				sctp_m_freem(control->data);
4836cd1386abSMichael Tuexen 				control->data = NULL;
4837cd1386abSMichael Tuexen 			}
483844249214SRandall Stewart 			sctp_free_a_readq(stcb, control);
48398a3cfbffSMichael Tuexen 		}
4840cd1386abSMichael Tuexen 		if (inp_read_lock_held == 0)
4841cd1386abSMichael Tuexen 			SCTP_INP_READ_UNLOCK(inp);
4842cd1386abSMichael Tuexen 		return;
4843cd1386abSMichael Tuexen 	}
484442551e99SRandall Stewart 	if (!(control->spec_flags & M_NOTIFICATION)) {
4845a5d547adSRandall Stewart 		atomic_add_int(&inp->total_recvs, 1);
484642551e99SRandall Stewart 		if (!control->do_not_ref_stcb) {
4847a5d547adSRandall Stewart 			atomic_add_int(&stcb->total_recvs, 1);
484842551e99SRandall Stewart 		}
484942551e99SRandall Stewart 	}
4850f8829a4aSRandall Stewart 	m = control->data;
4851f8829a4aSRandall Stewart 	control->held_length = 0;
4852f8829a4aSRandall Stewart 	control->length = 0;
4853f8829a4aSRandall Stewart 	while (m) {
4854139bc87fSRandall Stewart 		if (SCTP_BUF_LEN(m) == 0) {
4855f8829a4aSRandall Stewart 			/* Skip mbufs with NO length */
4856f8829a4aSRandall Stewart 			if (prev == NULL) {
4857f8829a4aSRandall Stewart 				/* First one */
4858f8829a4aSRandall Stewart 				control->data = sctp_m_free(m);
4859f8829a4aSRandall Stewart 				m = control->data;
4860f8829a4aSRandall Stewart 			} else {
4861139bc87fSRandall Stewart 				SCTP_BUF_NEXT(prev) = sctp_m_free(m);
4862139bc87fSRandall Stewart 				m = SCTP_BUF_NEXT(prev);
4863f8829a4aSRandall Stewart 			}
4864f8829a4aSRandall Stewart 			if (m == NULL) {
4865c2ede4b3SMartin Blapp 				control->tail_mbuf = prev;
4866f8829a4aSRandall Stewart 			}
4867f8829a4aSRandall Stewart 			continue;
4868f8829a4aSRandall Stewart 		}
4869f8829a4aSRandall Stewart 		prev = m;
4870b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4871139bc87fSRandall Stewart 			sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
487280fefe0aSRandall Stewart 		}
4873f8829a4aSRandall Stewart 		sctp_sballoc(stcb, sb, m);
4874b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4875f8829a4aSRandall Stewart 			sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
487680fefe0aSRandall Stewart 		}
4877139bc87fSRandall Stewart 		atomic_add_int(&control->length, SCTP_BUF_LEN(m));
4878139bc87fSRandall Stewart 		m = SCTP_BUF_NEXT(m);
4879f8829a4aSRandall Stewart 	}
4880f8829a4aSRandall Stewart 	if (prev != NULL) {
4881f8829a4aSRandall Stewart 		control->tail_mbuf = prev;
4882f8829a4aSRandall Stewart 	} else {
4883139bc87fSRandall Stewart 		/* Everything got collapsed out?? */
48848a3cfbffSMichael Tuexen 		if (!control->on_strm_q) {
4885cd1386abSMichael Tuexen 			sctp_free_remote_addr(control->whoFrom);
488644249214SRandall Stewart 			sctp_free_a_readq(stcb, control);
48878a3cfbffSMichael Tuexen 		}
4888cfde3ff7SRandall Stewart 		if (inp_read_lock_held == 0)
488947a490cbSMichael Tuexen 			SCTP_INP_READ_UNLOCK(inp);
4890f8829a4aSRandall Stewart 		return;
4891f8829a4aSRandall Stewart 	}
4892f8829a4aSRandall Stewart 	if (end) {
4893f8829a4aSRandall Stewart 		control->end_added = 1;
4894f8829a4aSRandall Stewart 	}
4895f8829a4aSRandall Stewart 	TAILQ_INSERT_TAIL(&inp->read_queue, control, next);
489644249214SRandall Stewart 	control->on_read_q = 1;
4897cfde3ff7SRandall Stewart 	if (inp_read_lock_held == 0)
4898f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
4899f8829a4aSRandall Stewart 	if (inp && inp->sctp_socket) {
4900b1deed45SMichael Tuexen 		sctp_wakeup_the_read_socket(inp, stcb, so_locked);
4901f8829a4aSRandall Stewart 	}
4902f8829a4aSRandall Stewart }
4903f8829a4aSRandall Stewart 
4904f8829a4aSRandall Stewart /*************HOLD THIS COMMENT FOR PATCH FILE OF
4905f8829a4aSRandall Stewart  *************ALTERNATE ROUTING CODE
4906f8829a4aSRandall Stewart  */
4907f8829a4aSRandall Stewart 
4908f8829a4aSRandall Stewart /*************HOLD THIS COMMENT FOR END OF PATCH FILE OF
4909f8829a4aSRandall Stewart  *************ALTERNATE ROUTING CODE
4910f8829a4aSRandall Stewart  */
4911f8829a4aSRandall Stewart 
4912f8829a4aSRandall Stewart struct mbuf *
4913ff1ffd74SMichael Tuexen sctp_generate_cause(uint16_t code, char *info)
4914f8829a4aSRandall Stewart {
4915f8829a4aSRandall Stewart 	struct mbuf *m;
4916ff1ffd74SMichael Tuexen 	struct sctp_gen_error_cause *cause;
49179a8e3088SMichael Tuexen 	size_t info_len;
49189a8e3088SMichael Tuexen 	uint16_t len;
4919f8829a4aSRandall Stewart 
4920ff1ffd74SMichael Tuexen 	if ((code == 0) || (info == NULL)) {
4921ff1ffd74SMichael Tuexen 		return (NULL);
4922ff1ffd74SMichael Tuexen 	}
4923ff1ffd74SMichael Tuexen 	info_len = strlen(info);
49249a8e3088SMichael Tuexen 	if (info_len > (SCTP_MAX_CAUSE_LENGTH - sizeof(struct sctp_paramhdr))) {
49259a8e3088SMichael Tuexen 		return (NULL);
49269a8e3088SMichael Tuexen 	}
49279a8e3088SMichael Tuexen 	len = (uint16_t)(sizeof(struct sctp_paramhdr) + info_len);
4928ff1ffd74SMichael Tuexen 	m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
4929ff1ffd74SMichael Tuexen 	if (m != NULL) {
4930ff1ffd74SMichael Tuexen 		SCTP_BUF_LEN(m) = len;
4931ff1ffd74SMichael Tuexen 		cause = mtod(m, struct sctp_gen_error_cause *);
4932ff1ffd74SMichael Tuexen 		cause->code = htons(code);
49339a8e3088SMichael Tuexen 		cause->length = htons(len);
4934ff1ffd74SMichael Tuexen 		memcpy(cause->info, info, info_len);
4935f8829a4aSRandall Stewart 	}
4936f8829a4aSRandall Stewart 	return (m);
4937f8829a4aSRandall Stewart }
4938f8829a4aSRandall Stewart 
493932451da4SMichael Tuexen struct mbuf *
494032451da4SMichael Tuexen sctp_generate_no_user_data_cause(uint32_t tsn)
494132451da4SMichael Tuexen {
494232451da4SMichael Tuexen 	struct mbuf *m;
494332451da4SMichael Tuexen 	struct sctp_error_no_user_data *no_user_data_cause;
49449a8e3088SMichael Tuexen 	uint16_t len;
494532451da4SMichael Tuexen 
49469a8e3088SMichael Tuexen 	len = (uint16_t)sizeof(struct sctp_error_no_user_data);
494732451da4SMichael Tuexen 	m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
494832451da4SMichael Tuexen 	if (m != NULL) {
494932451da4SMichael Tuexen 		SCTP_BUF_LEN(m) = len;
495032451da4SMichael Tuexen 		no_user_data_cause = mtod(m, struct sctp_error_no_user_data *);
495132451da4SMichael Tuexen 		no_user_data_cause->cause.code = htons(SCTP_CAUSE_NO_USER_DATA);
49529a8e3088SMichael Tuexen 		no_user_data_cause->cause.length = htons(len);
49538b9c95f4SMichael Tuexen 		no_user_data_cause->tsn = htonl(tsn);
495432451da4SMichael Tuexen 	}
495532451da4SMichael Tuexen 	return (m);
495632451da4SMichael Tuexen }
495732451da4SMichael Tuexen 
4958f8829a4aSRandall Stewart #ifdef SCTP_MBCNT_LOGGING
4959f8829a4aSRandall Stewart void
4960f8829a4aSRandall Stewart sctp_free_bufspace(struct sctp_tcb *stcb, struct sctp_association *asoc,
4961f8829a4aSRandall Stewart     struct sctp_tmit_chunk *tp1, int chk_cnt)
4962f8829a4aSRandall Stewart {
4963f8829a4aSRandall Stewart 	if (tp1->data == NULL) {
4964f8829a4aSRandall Stewart 		return;
4965f8829a4aSRandall Stewart 	}
4966f8829a4aSRandall Stewart 	asoc->chunks_on_out_queue -= chk_cnt;
4967b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBCNT_LOGGING_ENABLE) {
4968f8829a4aSRandall Stewart 		sctp_log_mbcnt(SCTP_LOG_MBCNT_DECREASE,
4969f8829a4aSRandall Stewart 		    asoc->total_output_queue_size,
4970f8829a4aSRandall Stewart 		    tp1->book_size,
4971f8829a4aSRandall Stewart 		    0,
4972f8829a4aSRandall Stewart 		    tp1->mbcnt);
497380fefe0aSRandall Stewart 	}
4974f8829a4aSRandall Stewart 	if (asoc->total_output_queue_size >= tp1->book_size) {
497544b7479bSRandall Stewart 		atomic_add_int(&asoc->total_output_queue_size, -tp1->book_size);
4976f8829a4aSRandall Stewart 	} else {
4977f8829a4aSRandall Stewart 		asoc->total_output_queue_size = 0;
4978f8829a4aSRandall Stewart 	}
4979f8829a4aSRandall Stewart 
4980f8829a4aSRandall Stewart 	if (stcb->sctp_socket && (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) ||
4981f8829a4aSRandall Stewart 	    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) {
49824e88d37aSMichael Tuexen 		if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) {
49834e88d37aSMichael Tuexen 			stcb->sctp_socket->so_snd.sb_cc -= tp1->book_size;
4984f8829a4aSRandall Stewart 		} else {
49854e88d37aSMichael Tuexen 			stcb->sctp_socket->so_snd.sb_cc = 0;
4986f8829a4aSRandall Stewart 
4987f8829a4aSRandall Stewart 		}
4988f8829a4aSRandall Stewart 	}
4989f8829a4aSRandall Stewart }
4990f8829a4aSRandall Stewart 
4991f8829a4aSRandall Stewart #endif
4992f8829a4aSRandall Stewart 
4993f8829a4aSRandall Stewart int
4994f8829a4aSRandall Stewart sctp_release_pr_sctp_chunk(struct sctp_tcb *stcb, struct sctp_tmit_chunk *tp1,
4995*28397ac1SMichael Tuexen     uint8_t sent, int so_locked)
4996f8829a4aSRandall Stewart {
49970c0982b8SRandall Stewart 	struct sctp_stream_out *strq;
49984a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk = NULL, *tp2;
49990c0982b8SRandall Stewart 	struct sctp_stream_queue_pending *sp;
500049656eefSMichael Tuexen 	uint32_t mid;
500149656eefSMichael Tuexen 	uint16_t sid;
50020c0982b8SRandall Stewart 	uint8_t foundeom = 0;
5003f8829a4aSRandall Stewart 	int ret_sz = 0;
5004f8829a4aSRandall Stewart 	int notdone;
50050c0982b8SRandall Stewart 	int do_wakeup_routine = 0;
5006f8829a4aSRandall Stewart 
500749656eefSMichael Tuexen 	sid = tp1->rec.data.sid;
500849656eefSMichael Tuexen 	mid = tp1->rec.data.mid;
5009f0396ad1SMichael Tuexen 	if (sent || !(tp1->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG)) {
5010f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_sent[0]++;
5011f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
501249656eefSMichael Tuexen 		stcb->asoc.strmout[sid].abandoned_sent[0]++;
5013f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
5014ad15e154SMichael Tuexen 		stcb->asoc.strmout[sid].abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
5015f0396ad1SMichael Tuexen #endif
5016f0396ad1SMichael Tuexen 	} else {
5017f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_unsent[0]++;
5018f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
501949656eefSMichael Tuexen 		stcb->asoc.strmout[sid].abandoned_unsent[0]++;
5020f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
5021ad15e154SMichael Tuexen 		stcb->asoc.strmout[sid].abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
5022f0396ad1SMichael Tuexen #endif
5023f0396ad1SMichael Tuexen 	}
5024f8829a4aSRandall Stewart 	do {
5025f8829a4aSRandall Stewart 		ret_sz += tp1->book_size;
50260c0982b8SRandall Stewart 		if (tp1->data != NULL) {
50278933fa13SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5028830d754dSRandall Stewart 				sctp_flight_size_decrease(tp1);
5029830d754dSRandall Stewart 				sctp_total_flight_decrease(stcb, tp1);
50308933fa13SRandall Stewart 			}
50318933fa13SRandall Stewart 			sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
50320c0982b8SRandall Stewart 			stcb->asoc.peers_rwnd += tp1->send_size;
50330c0982b8SRandall Stewart 			stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
50341edc9dbaSMichael Tuexen 			if (sent) {
50351edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
50361edc9dbaSMichael Tuexen 			} else {
50371edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
50381edc9dbaSMichael Tuexen 			}
50392f99457bSMichael Tuexen 			if (tp1->data) {
5040f8829a4aSRandall Stewart 				sctp_m_freem(tp1->data);
5041f8829a4aSRandall Stewart 				tp1->data = NULL;
50422f99457bSMichael Tuexen 			}
50430c0982b8SRandall Stewart 			do_wakeup_routine = 1;
5044f8829a4aSRandall Stewart 			if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
5045f8829a4aSRandall Stewart 				stcb->asoc.sent_queue_cnt_removeable--;
5046f8829a4aSRandall Stewart 			}
5047f8829a4aSRandall Stewart 		}
50488933fa13SRandall Stewart 		tp1->sent = SCTP_FORWARD_TSN_SKIP;
5049f8829a4aSRandall Stewart 		if ((tp1->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) ==
5050f8829a4aSRandall Stewart 		    SCTP_DATA_NOT_FRAG) {
5051f8829a4aSRandall Stewart 			/* not frag'ed we ae done   */
5052f8829a4aSRandall Stewart 			notdone = 0;
5053f8829a4aSRandall Stewart 			foundeom = 1;
5054f8829a4aSRandall Stewart 		} else if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
5055f8829a4aSRandall Stewart 			/* end of frag, we are done */
5056f8829a4aSRandall Stewart 			notdone = 0;
5057f8829a4aSRandall Stewart 			foundeom = 1;
5058f8829a4aSRandall Stewart 		} else {
5059f8829a4aSRandall Stewart 			/*
5060f8829a4aSRandall Stewart 			 * Its a begin or middle piece, we must mark all of
5061f8829a4aSRandall Stewart 			 * it
5062f8829a4aSRandall Stewart 			 */
5063f8829a4aSRandall Stewart 			notdone = 1;
5064f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
5065f8829a4aSRandall Stewart 		}
5066f8829a4aSRandall Stewart 	} while (tp1 && notdone);
50670c0982b8SRandall Stewart 	if (foundeom == 0) {
5068f8829a4aSRandall Stewart 		/*
5069f8829a4aSRandall Stewart 		 * The multi-part message was scattered across the send and
5070f8829a4aSRandall Stewart 		 * sent queue.
5071f8829a4aSRandall Stewart 		 */
50724a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(tp1, &stcb->asoc.send_queue, sctp_next, tp2) {
507349656eefSMichael Tuexen 			if ((tp1->rec.data.sid != sid) ||
507449656eefSMichael Tuexen 			    (!SCTP_MID_EQ(stcb->asoc.idata_supported, tp1->rec.data.mid, mid))) {
50754a9ef3f8SMichael Tuexen 				break;
50764a9ef3f8SMichael Tuexen 			}
50770c0982b8SRandall Stewart 			/*
50780c0982b8SRandall Stewart 			 * save to chk in case we have some on stream out
50790c0982b8SRandall Stewart 			 * queue. If so and we have an un-transmitted one we
50800c0982b8SRandall Stewart 			 * don't have to fudge the TSN.
50810c0982b8SRandall Stewart 			 */
50820c0982b8SRandall Stewart 			chk = tp1;
50830c0982b8SRandall Stewart 			ret_sz += tp1->book_size;
50840c0982b8SRandall Stewart 			sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
50851edc9dbaSMichael Tuexen 			if (sent) {
50861edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
50871edc9dbaSMichael Tuexen 			} else {
50881edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
50891edc9dbaSMichael Tuexen 			}
50902f99457bSMichael Tuexen 			if (tp1->data) {
50910c0982b8SRandall Stewart 				sctp_m_freem(tp1->data);
50922f99457bSMichael Tuexen 				tp1->data = NULL;
50932f99457bSMichael Tuexen 			}
50948933fa13SRandall Stewart 			/* No flight involved here book the size to 0 */
50958933fa13SRandall Stewart 			tp1->book_size = 0;
50960c0982b8SRandall Stewart 			if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
50970c0982b8SRandall Stewart 				foundeom = 1;
5098f8829a4aSRandall Stewart 			}
50990c0982b8SRandall Stewart 			do_wakeup_routine = 1;
51000c0982b8SRandall Stewart 			tp1->sent = SCTP_FORWARD_TSN_SKIP;
51010c0982b8SRandall Stewart 			TAILQ_REMOVE(&stcb->asoc.send_queue, tp1, sctp_next);
5102b7b84c0eSMichael Tuexen 			/*
5103b7b84c0eSMichael Tuexen 			 * on to the sent queue so we can wait for it to be
5104b7b84c0eSMichael Tuexen 			 * passed by.
5105b7b84c0eSMichael Tuexen 			 */
51060c0982b8SRandall Stewart 			TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, tp1,
51070c0982b8SRandall Stewart 			    sctp_next);
51080c0982b8SRandall Stewart 			stcb->asoc.send_queue_cnt--;
51090c0982b8SRandall Stewart 			stcb->asoc.sent_queue_cnt++;
51100c0982b8SRandall Stewart 		}
51110c0982b8SRandall Stewart 	}
51120c0982b8SRandall Stewart 	if (foundeom == 0) {
51130c0982b8SRandall Stewart 		/*
51140c0982b8SRandall Stewart 		 * Still no eom found. That means there is stuff left on the
51150c0982b8SRandall Stewart 		 * stream out queue.. yuck.
51160c0982b8SRandall Stewart 		 */
51170c0982b8SRandall Stewart 		SCTP_TCB_SEND_LOCK(stcb);
511849656eefSMichael Tuexen 		strq = &stcb->asoc.strmout[sid];
5119f3b05218SMichael Tuexen 		sp = TAILQ_FIRST(&strq->outqueue);
5120f3b05218SMichael Tuexen 		if (sp != NULL) {
51210c0982b8SRandall Stewart 			sp->discard_rest = 1;
51220c0982b8SRandall Stewart 			/*
5123f3b05218SMichael Tuexen 			 * We may need to put a chunk on the queue that
5124f3b05218SMichael Tuexen 			 * holds the TSN that would have been sent with the
5125f3b05218SMichael Tuexen 			 * LAST bit.
51260c0982b8SRandall Stewart 			 */
51270c0982b8SRandall Stewart 			if (chk == NULL) {
51280c0982b8SRandall Stewart 				/* Yep, we have to */
51290c0982b8SRandall Stewart 				sctp_alloc_a_chunk(stcb, chk);
51300c0982b8SRandall Stewart 				if (chk == NULL) {
51310c0982b8SRandall Stewart 					/*
5132f3b05218SMichael Tuexen 					 * we are hosed. All we can do is
5133f3b05218SMichael Tuexen 					 * nothing.. which will cause an
5134f3b05218SMichael Tuexen 					 * abort if the peer is paying
51350c0982b8SRandall Stewart 					 * attention.
51360c0982b8SRandall Stewart 					 */
51370c0982b8SRandall Stewart 					goto oh_well;
51380c0982b8SRandall Stewart 				}
51390c0982b8SRandall Stewart 				memset(chk, 0, sizeof(*chk));
514063d5b568SMichael Tuexen 				chk->rec.data.rcv_flags = 0;
51410c0982b8SRandall Stewart 				chk->sent = SCTP_FORWARD_TSN_SKIP;
51420c0982b8SRandall Stewart 				chk->asoc = &stcb->asoc;
514363d5b568SMichael Tuexen 				if (stcb->asoc.idata_supported == 0) {
514463d5b568SMichael Tuexen 					if (sp->sinfo_flags & SCTP_UNORDERED) {
514549656eefSMichael Tuexen 						chk->rec.data.mid = 0;
514663d5b568SMichael Tuexen 					} else {
514749656eefSMichael Tuexen 						chk->rec.data.mid = strq->next_mid_ordered;
514863d5b568SMichael Tuexen 					}
514963d5b568SMichael Tuexen 				} else {
515063d5b568SMichael Tuexen 					if (sp->sinfo_flags & SCTP_UNORDERED) {
515149656eefSMichael Tuexen 						chk->rec.data.mid = strq->next_mid_unordered;
515263d5b568SMichael Tuexen 					} else {
515349656eefSMichael Tuexen 						chk->rec.data.mid = strq->next_mid_ordered;
515463d5b568SMichael Tuexen 					}
515563d5b568SMichael Tuexen 				}
515649656eefSMichael Tuexen 				chk->rec.data.sid = sp->sid;
515749656eefSMichael Tuexen 				chk->rec.data.ppid = sp->ppid;
51580c0982b8SRandall Stewart 				chk->rec.data.context = sp->context;
51590c0982b8SRandall Stewart 				chk->flags = sp->act_flags;
51607fd5b436SMichael Tuexen 				chk->whoTo = NULL;
516149656eefSMichael Tuexen 				chk->rec.data.tsn = atomic_fetchadd_int(&stcb->asoc.sending_seq, 1);
51627fd5b436SMichael Tuexen 				strq->chunks_on_queues++;
51630c0982b8SRandall Stewart 				TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, chk, sctp_next);
51640c0982b8SRandall Stewart 				stcb->asoc.sent_queue_cnt++;
51658933fa13SRandall Stewart 				stcb->asoc.pr_sctp_cnt++;
51660c0982b8SRandall Stewart 			}
516763d5b568SMichael Tuexen 			chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
5168d1ea5fa9SMichael Tuexen 			if (sp->sinfo_flags & SCTP_UNORDERED) {
5169d1ea5fa9SMichael Tuexen 				chk->rec.data.rcv_flags |= SCTP_DATA_UNORDERED;
5170d1ea5fa9SMichael Tuexen 			}
517163d5b568SMichael Tuexen 			if (stcb->asoc.idata_supported == 0) {
517263d5b568SMichael Tuexen 				if ((sp->sinfo_flags & SCTP_UNORDERED) == 0) {
517363d5b568SMichael Tuexen 					strq->next_mid_ordered++;
517463d5b568SMichael Tuexen 				}
517563d5b568SMichael Tuexen 			} else {
517663d5b568SMichael Tuexen 				if (sp->sinfo_flags & SCTP_UNORDERED) {
517763d5b568SMichael Tuexen 					strq->next_mid_unordered++;
517863d5b568SMichael Tuexen 				} else {
517963d5b568SMichael Tuexen 					strq->next_mid_ordered++;
518063d5b568SMichael Tuexen 				}
518163d5b568SMichael Tuexen 			}
51820c0982b8SRandall Stewart 	oh_well:
51830c0982b8SRandall Stewart 			if (sp->data) {
51840c0982b8SRandall Stewart 				/*
5185f3b05218SMichael Tuexen 				 * Pull any data to free up the SB and allow
5186f3b05218SMichael Tuexen 				 * sender to "add more" while we will throw
5187f3b05218SMichael Tuexen 				 * away :-)
51880c0982b8SRandall Stewart 				 */
5189f3b05218SMichael Tuexen 				sctp_free_spbufspace(stcb, &stcb->asoc, sp);
51900c0982b8SRandall Stewart 				ret_sz += sp->length;
51910c0982b8SRandall Stewart 				do_wakeup_routine = 1;
51920c0982b8SRandall Stewart 				sp->some_taken = 1;
51930c0982b8SRandall Stewart 				sctp_m_freem(sp->data);
51940c0982b8SRandall Stewart 				sp->data = NULL;
51950c0982b8SRandall Stewart 				sp->tail_mbuf = NULL;
5196d07b2ac6SMichael Tuexen 				sp->length = 0;
51970c0982b8SRandall Stewart 			}
51980c0982b8SRandall Stewart 		}
51990c0982b8SRandall Stewart 		SCTP_TCB_SEND_UNLOCK(stcb);
52000c0982b8SRandall Stewart 	}
52010c0982b8SRandall Stewart 	if (do_wakeup_routine) {
52020c0982b8SRandall Stewart 		sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
5203f8829a4aSRandall Stewart 	}
5204f8829a4aSRandall Stewart 	return (ret_sz);
5205f8829a4aSRandall Stewart }
5206f8829a4aSRandall Stewart 
5207f8829a4aSRandall Stewart /*
5208f8829a4aSRandall Stewart  * checks to see if the given address, sa, is one that is currently known by
5209f8829a4aSRandall Stewart  * the kernel note: can't distinguish the same address on multiple interfaces
5210f8829a4aSRandall Stewart  * and doesn't handle multiple addresses with different zone/scope id's note:
5211f8829a4aSRandall Stewart  * ifa_ifwithaddr() compares the entire sockaddr struct
5212f8829a4aSRandall Stewart  */
521342551e99SRandall Stewart struct sctp_ifa *
521480fefe0aSRandall Stewart sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr,
521580fefe0aSRandall Stewart     int holds_lock)
5216f8829a4aSRandall Stewart {
521742551e99SRandall Stewart 	struct sctp_laddr *laddr;
5218f8829a4aSRandall Stewart 
5219ad81507eSRandall Stewart 	if (holds_lock == 0) {
522042551e99SRandall Stewart 		SCTP_INP_RLOCK(inp);
5221ad81507eSRandall Stewart 	}
52220053ed28SMichael Tuexen 
522342551e99SRandall Stewart 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
522442551e99SRandall Stewart 		if (laddr->ifa == NULL)
5225f8829a4aSRandall Stewart 			continue;
522642551e99SRandall Stewart 		if (addr->sa_family != laddr->ifa->address.sa.sa_family)
522742551e99SRandall Stewart 			continue;
5228e6194c2eSMichael Tuexen #ifdef INET
522942551e99SRandall Stewart 		if (addr->sa_family == AF_INET) {
523042551e99SRandall Stewart 			if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
523142551e99SRandall Stewart 			    laddr->ifa->address.sin.sin_addr.s_addr) {
523242551e99SRandall Stewart 				/* found him. */
5233ad81507eSRandall Stewart 				if (holds_lock == 0) {
523442551e99SRandall Stewart 					SCTP_INP_RUNLOCK(inp);
5235ad81507eSRandall Stewart 				}
523642551e99SRandall Stewart 				return (laddr->ifa);
523742551e99SRandall Stewart 				break;
523842551e99SRandall Stewart 			}
52395e2c2d87SRandall Stewart 		}
5240e6194c2eSMichael Tuexen #endif
52415e2c2d87SRandall Stewart #ifdef INET6
52425e2c2d87SRandall Stewart 		if (addr->sa_family == AF_INET6) {
5243c54a18d2SRandall Stewart 			if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5244c54a18d2SRandall Stewart 			    &laddr->ifa->address.sin6)) {
524542551e99SRandall Stewart 				/* found him. */
5246ad81507eSRandall Stewart 				if (holds_lock == 0) {
524742551e99SRandall Stewart 					SCTP_INP_RUNLOCK(inp);
5248ad81507eSRandall Stewart 				}
524942551e99SRandall Stewart 				return (laddr->ifa);
525042551e99SRandall Stewart 				break;
525142551e99SRandall Stewart 			}
525242551e99SRandall Stewart 		}
52535e2c2d87SRandall Stewart #endif
525442551e99SRandall Stewart 	}
5255ad81507eSRandall Stewart 	if (holds_lock == 0) {
525642551e99SRandall Stewart 		SCTP_INP_RUNLOCK(inp);
5257ad81507eSRandall Stewart 	}
525842551e99SRandall Stewart 	return (NULL);
525942551e99SRandall Stewart }
5260f8829a4aSRandall Stewart 
52616a27c376SRandall Stewart uint32_t
5262b0471b4bSMichael Tuexen sctp_get_ifa_hash_val(struct sockaddr *addr)
5263b0471b4bSMichael Tuexen {
5264ea5eba11SMichael Tuexen 	switch (addr->sa_family) {
5265ea5eba11SMichael Tuexen #ifdef INET
5266ea5eba11SMichael Tuexen 	case AF_INET:
5267ea5eba11SMichael Tuexen 		{
52686a27c376SRandall Stewart 			struct sockaddr_in *sin;
52696a27c376SRandall Stewart 
52706a27c376SRandall Stewart 			sin = (struct sockaddr_in *)addr;
52716a27c376SRandall Stewart 			return (sin->sin_addr.s_addr ^ (sin->sin_addr.s_addr >> 16));
5272ea5eba11SMichael Tuexen 		}
5273ea5eba11SMichael Tuexen #endif
5274ea5eba11SMichael Tuexen #ifdef INET6
52752c2e3218SMichael Tuexen 	case AF_INET6:
5276ea5eba11SMichael Tuexen 		{
52776a27c376SRandall Stewart 			struct sockaddr_in6 *sin6;
52786a27c376SRandall Stewart 			uint32_t hash_of_addr;
52796a27c376SRandall Stewart 
52806a27c376SRandall Stewart 			sin6 = (struct sockaddr_in6 *)addr;
52816a27c376SRandall Stewart 			hash_of_addr = (sin6->sin6_addr.s6_addr32[0] +
52826a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[1] +
52836a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[2] +
52846a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[3]);
52856a27c376SRandall Stewart 			hash_of_addr = (hash_of_addr ^ (hash_of_addr >> 16));
52866a27c376SRandall Stewart 			return (hash_of_addr);
52876a27c376SRandall Stewart 		}
5288ea5eba11SMichael Tuexen #endif
5289ea5eba11SMichael Tuexen 	default:
5290ea5eba11SMichael Tuexen 		break;
5291ea5eba11SMichael Tuexen 	}
52926a27c376SRandall Stewart 	return (0);
52936a27c376SRandall Stewart }
52946a27c376SRandall Stewart 
529542551e99SRandall Stewart struct sctp_ifa *
529642551e99SRandall Stewart sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock)
529742551e99SRandall Stewart {
529842551e99SRandall Stewart 	struct sctp_ifa *sctp_ifap;
529942551e99SRandall Stewart 	struct sctp_vrf *vrf;
53006a27c376SRandall Stewart 	struct sctp_ifalist *hash_head;
53016a27c376SRandall Stewart 	uint32_t hash_of_addr;
530242551e99SRandall Stewart 
530342551e99SRandall Stewart 	if (holds_lock == 0)
5304c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RLOCK();
530542551e99SRandall Stewart 
5306bff64a4dSRandall Stewart 	vrf = sctp_find_vrf(vrf_id);
5307bff64a4dSRandall Stewart 	if (vrf == NULL) {
5308bff64a4dSRandall Stewart 		if (holds_lock == 0)
5309c99efcf6SRandall Stewart 			SCTP_IPI_ADDR_RUNLOCK();
5310bff64a4dSRandall Stewart 		return (NULL);
5311bff64a4dSRandall Stewart 	}
53120053ed28SMichael Tuexen 
5313bff64a4dSRandall Stewart 	hash_of_addr = sctp_get_ifa_hash_val(addr);
5314bff64a4dSRandall Stewart 
531517205eccSRandall Stewart 	hash_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
5316bff64a4dSRandall Stewart 	if (hash_head == NULL) {
5317ad81507eSRandall Stewart 		SCTP_PRINTF("hash_of_addr:%x mask:%x table:%x - ",
5318c99efcf6SRandall Stewart 		    hash_of_addr, (uint32_t)vrf->vrf_addr_hashmark,
5319c99efcf6SRandall Stewart 		    (uint32_t)(hash_of_addr & vrf->vrf_addr_hashmark));
5320bff64a4dSRandall Stewart 		sctp_print_address(addr);
5321ad81507eSRandall Stewart 		SCTP_PRINTF("No such bucket for address\n");
5322bff64a4dSRandall Stewart 		if (holds_lock == 0)
5323c99efcf6SRandall Stewart 			SCTP_IPI_ADDR_RUNLOCK();
5324bff64a4dSRandall Stewart 
5325bff64a4dSRandall Stewart 		return (NULL);
5326bff64a4dSRandall Stewart 	}
53276a27c376SRandall Stewart 	LIST_FOREACH(sctp_ifap, hash_head, next_bucket) {
53286a27c376SRandall Stewart 		if (addr->sa_family != sctp_ifap->address.sa.sa_family)
53296a27c376SRandall Stewart 			continue;
5330e6194c2eSMichael Tuexen #ifdef INET
53316a27c376SRandall Stewart 		if (addr->sa_family == AF_INET) {
53326a27c376SRandall Stewart 			if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
53336a27c376SRandall Stewart 			    sctp_ifap->address.sin.sin_addr.s_addr) {
53346a27c376SRandall Stewart 				/* found him. */
533542551e99SRandall Stewart 				if (holds_lock == 0)
5336c99efcf6SRandall Stewart 					SCTP_IPI_ADDR_RUNLOCK();
533742551e99SRandall Stewart 				return (sctp_ifap);
53386a27c376SRandall Stewart 				break;
53396a27c376SRandall Stewart 			}
53405e2c2d87SRandall Stewart 		}
5341e6194c2eSMichael Tuexen #endif
53425e2c2d87SRandall Stewart #ifdef INET6
53435e2c2d87SRandall Stewart 		if (addr->sa_family == AF_INET6) {
5344c54a18d2SRandall Stewart 			if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5345c54a18d2SRandall Stewart 			    &sctp_ifap->address.sin6)) {
53466a27c376SRandall Stewart 				/* found him. */
53476a27c376SRandall Stewart 				if (holds_lock == 0)
5348c99efcf6SRandall Stewart 					SCTP_IPI_ADDR_RUNLOCK();
53496a27c376SRandall Stewart 				return (sctp_ifap);
53506a27c376SRandall Stewart 				break;
53516a27c376SRandall Stewart 			}
535242551e99SRandall Stewart 		}
53535e2c2d87SRandall Stewart #endif
535442551e99SRandall Stewart 	}
535542551e99SRandall Stewart 	if (holds_lock == 0)
5356c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RUNLOCK();
5357f8829a4aSRandall Stewart 	return (NULL);
5358f8829a4aSRandall Stewart }
5359f8829a4aSRandall Stewart 
5360f8829a4aSRandall Stewart static void
53614c9179adSRandall Stewart sctp_user_rcvd(struct sctp_tcb *stcb, uint32_t *freed_so_far, int hold_rlock,
5362f8829a4aSRandall Stewart     uint32_t rwnd_req)
5363f8829a4aSRandall Stewart {
5364f8829a4aSRandall Stewart 	/* User pulled some data, do we need a rwnd update? */
5365868b51f2SMichael Tuexen 	struct epoch_tracker et;
5366f8829a4aSRandall Stewart 	int r_unlocked = 0;
5367f8829a4aSRandall Stewart 	uint32_t dif, rwnd;
5368f8829a4aSRandall Stewart 	struct socket *so = NULL;
5369f8829a4aSRandall Stewart 
5370f8829a4aSRandall Stewart 	if (stcb == NULL)
5371f8829a4aSRandall Stewart 		return;
5372f8829a4aSRandall Stewart 
537350cec919SRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, 1);
5374f8829a4aSRandall Stewart 
5375839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
537661a21880SMichael Tuexen 	    (stcb->asoc.state & (SCTP_STATE_ABOUT_TO_BE_FREED | SCTP_STATE_SHUTDOWN_RECEIVED))) {
5377f8829a4aSRandall Stewart 		/* Pre-check If we are freeing no update */
5378f8829a4aSRandall Stewart 		goto no_lock;
5379f8829a4aSRandall Stewart 	}
5380f8829a4aSRandall Stewart 	SCTP_INP_INCR_REF(stcb->sctp_ep);
5381f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5382f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5383f8829a4aSRandall Stewart 		goto out;
5384f8829a4aSRandall Stewart 	}
5385f8829a4aSRandall Stewart 	so = stcb->sctp_socket;
5386f8829a4aSRandall Stewart 	if (so == NULL) {
5387f8829a4aSRandall Stewart 		goto out;
5388f8829a4aSRandall Stewart 	}
5389f8829a4aSRandall Stewart 	atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far);
5390f8829a4aSRandall Stewart 	/* Have you have freed enough to look */
5391f8829a4aSRandall Stewart 	*freed_so_far = 0;
5392f8829a4aSRandall Stewart 	/* Yep, its worth a look and the lock overhead */
5393f8829a4aSRandall Stewart 
5394f8829a4aSRandall Stewart 	/* Figure out what the rwnd would be */
5395f8829a4aSRandall Stewart 	rwnd = sctp_calc_rwnd(stcb, &stcb->asoc);
5396f8829a4aSRandall Stewart 	if (rwnd >= stcb->asoc.my_last_reported_rwnd) {
5397f8829a4aSRandall Stewart 		dif = rwnd - stcb->asoc.my_last_reported_rwnd;
5398f8829a4aSRandall Stewart 	} else {
5399f8829a4aSRandall Stewart 		dif = 0;
5400f8829a4aSRandall Stewart 	}
5401f8829a4aSRandall Stewart 	if (dif >= rwnd_req) {
5402f8829a4aSRandall Stewart 		if (hold_rlock) {
5403f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5404f8829a4aSRandall Stewart 			r_unlocked = 1;
5405f8829a4aSRandall Stewart 		}
5406f8829a4aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5407f8829a4aSRandall Stewart 			/*
5408f8829a4aSRandall Stewart 			 * One last check before we allow the guy possibly
5409f8829a4aSRandall Stewart 			 * to get in. There is a race, where the guy has not
5410f8829a4aSRandall Stewart 			 * reached the gate. In that case
5411f8829a4aSRandall Stewart 			 */
5412f8829a4aSRandall Stewart 			goto out;
5413f8829a4aSRandall Stewart 		}
5414f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
5415f8829a4aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5416f8829a4aSRandall Stewart 			/* No reports here */
5417f8829a4aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
5418f8829a4aSRandall Stewart 			goto out;
5419f8829a4aSRandall Stewart 		}
5420f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_wu_sacks_sent);
5421868b51f2SMichael Tuexen 		NET_EPOCH_ENTER(et);
5422689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_LOCKED);
5423830d754dSRandall Stewart 
5424f8829a4aSRandall Stewart 		sctp_chunk_output(stcb->sctp_ep, stcb,
5425ceaad40aSRandall Stewart 		    SCTP_OUTPUT_FROM_USR_RCVD, SCTP_SO_LOCKED);
5426f8829a4aSRandall Stewart 		/* make sure no timer is running */
5427868b51f2SMichael Tuexen 		NET_EPOCH_EXIT(et);
5428ba785902SMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
5429ba785902SMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_6);
5430f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
5431f8829a4aSRandall Stewart 	} else {
5432f8829a4aSRandall Stewart 		/* Update how much we have pending */
5433f8829a4aSRandall Stewart 		stcb->freed_by_sorcv_sincelast = dif;
5434f8829a4aSRandall Stewart 	}
5435f8829a4aSRandall Stewart out:
5436f8829a4aSRandall Stewart 	if (so && r_unlocked && hold_rlock) {
5437f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
5438f8829a4aSRandall Stewart 	}
54390053ed28SMichael Tuexen 
5440f8829a4aSRandall Stewart 	SCTP_INP_DECR_REF(stcb->sctp_ep);
5441f8829a4aSRandall Stewart no_lock:
544250cec919SRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, -1);
5443f8829a4aSRandall Stewart 	return;
5444f8829a4aSRandall Stewart }
5445f8829a4aSRandall Stewart 
5446f8829a4aSRandall Stewart int
5447f8829a4aSRandall Stewart sctp_sorecvmsg(struct socket *so,
5448f8829a4aSRandall Stewart     struct uio *uio,
5449f8829a4aSRandall Stewart     struct mbuf **mp,
5450f8829a4aSRandall Stewart     struct sockaddr *from,
5451f8829a4aSRandall Stewart     int fromlen,
5452f8829a4aSRandall Stewart     int *msg_flags,
5453f8829a4aSRandall Stewart     struct sctp_sndrcvinfo *sinfo,
5454f8829a4aSRandall Stewart     int filling_sinfo)
5455f8829a4aSRandall Stewart {
5456f8829a4aSRandall Stewart 	/*
5457f8829a4aSRandall Stewart 	 * MSG flags we will look at MSG_DONTWAIT - non-blocking IO.
5458f8829a4aSRandall Stewart 	 * MSG_PEEK - Look don't touch :-D (only valid with OUT mbuf copy
5459f8829a4aSRandall Stewart 	 * mp=NULL thus uio is the copy method to userland) MSG_WAITALL - ??
5460f8829a4aSRandall Stewart 	 * On the way out we may send out any combination of:
5461f8829a4aSRandall Stewart 	 * MSG_NOTIFICATION MSG_EOR
5462f8829a4aSRandall Stewart 	 *
5463f8829a4aSRandall Stewart 	 */
5464f8829a4aSRandall Stewart 	struct sctp_inpcb *inp = NULL;
546558e6eeefSMichael Tuexen 	ssize_t my_len = 0;
546658e6eeefSMichael Tuexen 	ssize_t cp_len = 0;
54670d3cf13dSMichael Tuexen 	int error = 0;
5468f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL;
546994b0d969SMichael Tuexen 	struct mbuf *m = NULL;
5470f8829a4aSRandall Stewart 	struct sctp_tcb *stcb = NULL;
5471f8829a4aSRandall Stewart 	int wakeup_read_socket = 0;
5472f8829a4aSRandall Stewart 	int freecnt_applied = 0;
5473f8829a4aSRandall Stewart 	int out_flags = 0, in_flags = 0;
5474f8829a4aSRandall Stewart 	int block_allowed = 1;
54754c9179adSRandall Stewart 	uint32_t freed_so_far = 0;
547658e6eeefSMichael Tuexen 	ssize_t copied_so_far = 0;
547793164cf9SRandall Stewart 	int in_eeor_mode = 0;
5478f8829a4aSRandall Stewart 	int no_rcv_needed = 0;
5479f8829a4aSRandall Stewart 	uint32_t rwnd_req = 0;
5480f8829a4aSRandall Stewart 	int hold_sblock = 0;
5481f8829a4aSRandall Stewart 	int hold_rlock = 0;
54829a8e3088SMichael Tuexen 	ssize_t slen = 0;
54834c9179adSRandall Stewart 	uint32_t held_length = 0;
54847abab911SRobert Watson 	int sockbuf_lock = 0;
5485f8829a4aSRandall Stewart 
548617205eccSRandall Stewart 	if (uio == NULL) {
5487c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
548817205eccSRandall Stewart 		return (EINVAL);
548917205eccSRandall Stewart 	}
54900053ed28SMichael Tuexen 
5491f8829a4aSRandall Stewart 	if (msg_flags) {
5492f8829a4aSRandall Stewart 		in_flags = *msg_flags;
5493c105859eSRandall Stewart 		if (in_flags & MSG_PEEK)
5494c105859eSRandall Stewart 			SCTP_STAT_INCR(sctps_read_peeks);
5495f8829a4aSRandall Stewart 	} else {
5496f8829a4aSRandall Stewart 		in_flags = 0;
5497f8829a4aSRandall Stewart 	}
5498f8829a4aSRandall Stewart 	slen = uio->uio_resid;
549917205eccSRandall Stewart 
5500f8829a4aSRandall Stewart 	/* Pull in and set up our int flags */
5501f8829a4aSRandall Stewart 	if (in_flags & MSG_OOB) {
5502f8829a4aSRandall Stewart 		/* Out of band's NOT supported */
5503f8829a4aSRandall Stewart 		return (EOPNOTSUPP);
5504f8829a4aSRandall Stewart 	}
5505f8829a4aSRandall Stewart 	if ((in_flags & MSG_PEEK) && (mp != NULL)) {
5506c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5507f8829a4aSRandall Stewart 		return (EINVAL);
5508f8829a4aSRandall Stewart 	}
5509f8829a4aSRandall Stewart 	if ((in_flags & (MSG_DONTWAIT
5510f8829a4aSRandall Stewart 	    | MSG_NBIO
5511f8829a4aSRandall Stewart 	    )) ||
551242551e99SRandall Stewart 	    SCTP_SO_IS_NBIO(so)) {
5513f8829a4aSRandall Stewart 		block_allowed = 0;
5514f8829a4aSRandall Stewart 	}
5515f8829a4aSRandall Stewart 	/* setup the endpoint */
5516f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
5517f8829a4aSRandall Stewart 	if (inp == NULL) {
5518c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
5519f8829a4aSRandall Stewart 		return (EFAULT);
5520f8829a4aSRandall Stewart 	}
552162c1ff9cSRandall Stewart 	rwnd_req = (SCTP_SB_LIMIT_RCV(so) >> SCTP_RWND_HIWAT_SHIFT);
5522f8829a4aSRandall Stewart 	/* Must be at least a MTU's worth */
5523f8829a4aSRandall Stewart 	if (rwnd_req < SCTP_MIN_RWND)
5524f8829a4aSRandall Stewart 		rwnd_req = SCTP_MIN_RWND;
5525f8829a4aSRandall Stewart 	in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
5526b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5527f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SORECV_ENTER,
55289a8e3088SMichael Tuexen 		    rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, (uint32_t)uio->uio_resid);
552980fefe0aSRandall Stewart 	}
5530b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5531f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SORECV_ENTERPL,
55329a8e3088SMichael Tuexen 		    rwnd_req, block_allowed, so->so_rcv.sb_cc, (uint32_t)uio->uio_resid);
553380fefe0aSRandall Stewart 	}
55340053ed28SMichael Tuexen 
55350053ed28SMichael Tuexen 
5536265de5bbSRobert Watson 	error = sblock(&so->so_rcv, (block_allowed ? SBL_WAIT : 0));
5537f8829a4aSRandall Stewart 	if (error) {
5538f8829a4aSRandall Stewart 		goto release_unlocked;
5539f8829a4aSRandall Stewart 	}
55408e1e6e5fSMateusz Guzik 	sockbuf_lock = 1;
5541f8829a4aSRandall Stewart restart:
55427abab911SRobert Watson 
5543f8829a4aSRandall Stewart restart_nosblocks:
5544f8829a4aSRandall Stewart 	if (hold_sblock == 0) {
5545f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&so->so_rcv);
5546f8829a4aSRandall Stewart 		hold_sblock = 1;
5547f8829a4aSRandall Stewart 	}
5548f8829a4aSRandall Stewart 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5549f8829a4aSRandall Stewart 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5550f8829a4aSRandall Stewart 		goto out;
5551f8829a4aSRandall Stewart 	}
55524e88d37aSMichael Tuexen 	if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) && (so->so_rcv.sb_cc == 0)) {
5553f8829a4aSRandall Stewart 		if (so->so_error) {
5554f8829a4aSRandall Stewart 			error = so->so_error;
555544b7479bSRandall Stewart 			if ((in_flags & MSG_PEEK) == 0)
555644b7479bSRandall Stewart 				so->so_error = 0;
55579f22f500SRandall Stewart 			goto out;
5558f8829a4aSRandall Stewart 		} else {
55594e88d37aSMichael Tuexen 			if (so->so_rcv.sb_cc == 0) {
55607924093fSRandall Stewart 				/* indicate EOF */
55617924093fSRandall Stewart 				error = 0;
5562f8829a4aSRandall Stewart 				goto out;
5563f8829a4aSRandall Stewart 			}
55649f22f500SRandall Stewart 		}
55659f22f500SRandall Stewart 	}
55669de217ceSMichael Tuexen 	if (so->so_rcv.sb_cc <= held_length) {
55679de217ceSMichael Tuexen 		if (so->so_error) {
55689de217ceSMichael Tuexen 			error = so->so_error;
55699de217ceSMichael Tuexen 			if ((in_flags & MSG_PEEK) == 0) {
55709de217ceSMichael Tuexen 				so->so_error = 0;
55719de217ceSMichael Tuexen 			}
55729de217ceSMichael Tuexen 			goto out;
55739de217ceSMichael Tuexen 		}
55744e88d37aSMichael Tuexen 		if ((so->so_rcv.sb_cc == 0) &&
5575f8829a4aSRandall Stewart 		    ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5576f8829a4aSRandall Stewart 		    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
5577f8829a4aSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
5578f8829a4aSRandall Stewart 				/*
5579f8829a4aSRandall Stewart 				 * For active open side clear flags for
5580f8829a4aSRandall Stewart 				 * re-use passive open is blocked by
5581f8829a4aSRandall Stewart 				 * connect.
5582f8829a4aSRandall Stewart 				 */
5583f8829a4aSRandall Stewart 				if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
5584b7b84c0eSMichael Tuexen 					/*
5585b7b84c0eSMichael Tuexen 					 * You were aborted, passive side
5586b7b84c0eSMichael Tuexen 					 * always hits here
5587b7b84c0eSMichael Tuexen 					 */
5588c4739e2fSRandall Stewart 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
5589f8829a4aSRandall Stewart 					error = ECONNRESET;
5590f8829a4aSRandall Stewart 				}
5591f8829a4aSRandall Stewart 				so->so_state &= ~(SS_ISCONNECTING |
5592f8829a4aSRandall Stewart 				    SS_ISDISCONNECTING |
5593f8829a4aSRandall Stewart 				    SS_ISCONFIRMING |
5594f8829a4aSRandall Stewart 				    SS_ISCONNECTED);
5595f8829a4aSRandall Stewart 				if (error == 0) {
5596f8829a4aSRandall Stewart 					if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5597c4739e2fSRandall Stewart 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5598f8829a4aSRandall Stewart 						error = ENOTCONN;
5599f8829a4aSRandall Stewart 					}
5600f8829a4aSRandall Stewart 				}
5601f8829a4aSRandall Stewart 				goto out;
5602f8829a4aSRandall Stewart 			}
5603f8829a4aSRandall Stewart 		}
56049de217ceSMichael Tuexen 		if (block_allowed) {
5605f8829a4aSRandall Stewart 			error = sbwait(&so->so_rcv);
5606f8829a4aSRandall Stewart 			if (error) {
5607f8829a4aSRandall Stewart 				goto out;
5608f8829a4aSRandall Stewart 			}
5609f8829a4aSRandall Stewart 			held_length = 0;
5610f8829a4aSRandall Stewart 			goto restart_nosblocks;
561144b7479bSRandall Stewart 		} else {
5612c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EWOULDBLOCK);
5613f8829a4aSRandall Stewart 			error = EWOULDBLOCK;
5614f8829a4aSRandall Stewart 			goto out;
5615f8829a4aSRandall Stewart 		}
56169de217ceSMichael Tuexen 	}
5617d06c82f1SRandall Stewart 	if (hold_sblock == 1) {
5618d06c82f1SRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
5619d06c82f1SRandall Stewart 		hold_sblock = 0;
5620d06c82f1SRandall Stewart 	}
5621f8829a4aSRandall Stewart 	/* we possibly have data we can read */
56223c503c28SRandall Stewart 	/* sa_ignore FREED_MEMORY */
5623f8829a4aSRandall Stewart 	control = TAILQ_FIRST(&inp->read_queue);
5624f8829a4aSRandall Stewart 	if (control == NULL) {
5625f8829a4aSRandall Stewart 		/*
5626f8829a4aSRandall Stewart 		 * This could be happening since the appender did the
5627f8829a4aSRandall Stewart 		 * increment but as not yet did the tailq insert onto the
5628f8829a4aSRandall Stewart 		 * read_queue
5629f8829a4aSRandall Stewart 		 */
5630f8829a4aSRandall Stewart 		if (hold_rlock == 0) {
5631f8829a4aSRandall Stewart 			SCTP_INP_READ_LOCK(inp);
5632f8829a4aSRandall Stewart 		}
5633f8829a4aSRandall Stewart 		control = TAILQ_FIRST(&inp->read_queue);
56344e88d37aSMichael Tuexen 		if ((control == NULL) && (so->so_rcv.sb_cc != 0)) {
5635a5d547adSRandall Stewart #ifdef INVARIANTS
5636f8829a4aSRandall Stewart 			panic("Huh, its non zero and nothing on control?");
5637f8829a4aSRandall Stewart #endif
56384e88d37aSMichael Tuexen 			so->so_rcv.sb_cc = 0;
5639f8829a4aSRandall Stewart 		}
5640f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
5641f8829a4aSRandall Stewart 		hold_rlock = 0;
5642f8829a4aSRandall Stewart 		goto restart;
5643f8829a4aSRandall Stewart 	}
56440053ed28SMichael Tuexen 
5645f8829a4aSRandall Stewart 	if ((control->length == 0) &&
5646f8829a4aSRandall Stewart 	    (control->do_not_ref_stcb)) {
5647f8829a4aSRandall Stewart 		/*
5648f8829a4aSRandall Stewart 		 * Clean up code for freeing assoc that left behind a
5649f8829a4aSRandall Stewart 		 * pdapi.. maybe a peer in EEOR that just closed after
5650f8829a4aSRandall Stewart 		 * sending and never indicated a EOR.
5651f8829a4aSRandall Stewart 		 */
5652f8829a4aSRandall Stewart 		if (hold_rlock == 0) {
5653f8829a4aSRandall Stewart 			hold_rlock = 1;
5654f8829a4aSRandall Stewart 			SCTP_INP_READ_LOCK(inp);
5655f8829a4aSRandall Stewart 		}
5656f8829a4aSRandall Stewart 		control->held_length = 0;
5657f8829a4aSRandall Stewart 		if (control->data) {
5658f8829a4aSRandall Stewart 			/* Hmm there is data here .. fix */
56594c9179adSRandall Stewart 			struct mbuf *m_tmp;
5660f8829a4aSRandall Stewart 			int cnt = 0;
5661f8829a4aSRandall Stewart 
56624c9179adSRandall Stewart 			m_tmp = control->data;
56634c9179adSRandall Stewart 			while (m_tmp) {
56644c9179adSRandall Stewart 				cnt += SCTP_BUF_LEN(m_tmp);
56654c9179adSRandall Stewart 				if (SCTP_BUF_NEXT(m_tmp) == NULL) {
56664c9179adSRandall Stewart 					control->tail_mbuf = m_tmp;
5667f8829a4aSRandall Stewart 					control->end_added = 1;
5668f8829a4aSRandall Stewart 				}
56694c9179adSRandall Stewart 				m_tmp = SCTP_BUF_NEXT(m_tmp);
5670f8829a4aSRandall Stewart 			}
5671f8829a4aSRandall Stewart 			control->length = cnt;
5672f8829a4aSRandall Stewart 		} else {
5673f8829a4aSRandall Stewart 			/* remove it */
5674f8829a4aSRandall Stewart 			TAILQ_REMOVE(&inp->read_queue, control, next);
5675f8829a4aSRandall Stewart 			/* Add back any hiddend data */
5676f8829a4aSRandall Stewart 			sctp_free_remote_addr(control->whoFrom);
5677f8829a4aSRandall Stewart 			sctp_free_a_readq(stcb, control);
5678f8829a4aSRandall Stewart 		}
5679f8829a4aSRandall Stewart 		if (hold_rlock) {
5680f8829a4aSRandall Stewart 			hold_rlock = 0;
5681f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
5682f8829a4aSRandall Stewart 		}
5683f8829a4aSRandall Stewart 		goto restart;
5684f8829a4aSRandall Stewart 	}
5685810ec536SMichael Tuexen 	if ((control->length == 0) &&
5686810ec536SMichael Tuexen 	    (control->end_added == 1)) {
5687b7b84c0eSMichael Tuexen 		/*
5688b7b84c0eSMichael Tuexen 		 * Do we also need to check for (control->pdapi_aborted ==
5689b7b84c0eSMichael Tuexen 		 * 1)?
5690b7b84c0eSMichael Tuexen 		 */
5691810ec536SMichael Tuexen 		if (hold_rlock == 0) {
5692810ec536SMichael Tuexen 			hold_rlock = 1;
5693810ec536SMichael Tuexen 			SCTP_INP_READ_LOCK(inp);
5694810ec536SMichael Tuexen 		}
5695810ec536SMichael Tuexen 		TAILQ_REMOVE(&inp->read_queue, control, next);
5696810ec536SMichael Tuexen 		if (control->data) {
5697810ec536SMichael Tuexen #ifdef INVARIANTS
5698810ec536SMichael Tuexen 			panic("control->data not null but control->length == 0");
5699810ec536SMichael Tuexen #else
5700810ec536SMichael Tuexen 			SCTP_PRINTF("Strange, data left in the control buffer. Cleaning up.\n");
5701810ec536SMichael Tuexen 			sctp_m_freem(control->data);
5702810ec536SMichael Tuexen 			control->data = NULL;
5703810ec536SMichael Tuexen #endif
5704810ec536SMichael Tuexen 		}
5705810ec536SMichael Tuexen 		if (control->aux_data) {
5706810ec536SMichael Tuexen 			sctp_m_free(control->aux_data);
5707810ec536SMichael Tuexen 			control->aux_data = NULL;
5708810ec536SMichael Tuexen 		}
570998d5fd97SMichael Tuexen #ifdef INVARIANTS
571044249214SRandall Stewart 		if (control->on_strm_q) {
571144249214SRandall Stewart 			panic("About to free ctl:%p so:%p and its in %d",
571244249214SRandall Stewart 			    control, so, control->on_strm_q);
571344249214SRandall Stewart 		}
571498d5fd97SMichael Tuexen #endif
5715810ec536SMichael Tuexen 		sctp_free_remote_addr(control->whoFrom);
5716810ec536SMichael Tuexen 		sctp_free_a_readq(stcb, control);
5717810ec536SMichael Tuexen 		if (hold_rlock) {
5718810ec536SMichael Tuexen 			hold_rlock = 0;
5719810ec536SMichael Tuexen 			SCTP_INP_READ_UNLOCK(inp);
5720810ec536SMichael Tuexen 		}
5721810ec536SMichael Tuexen 		goto restart;
5722810ec536SMichael Tuexen 	}
5723f8829a4aSRandall Stewart 	if (control->length == 0) {
5724f8829a4aSRandall Stewart 		if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
5725f8829a4aSRandall Stewart 		    (filling_sinfo)) {
5726f8829a4aSRandall Stewart 			/* find a more suitable one then this */
5727f8829a4aSRandall Stewart 			ctl = TAILQ_NEXT(control, next);
5728f8829a4aSRandall Stewart 			while (ctl) {
57299a6142d8SRandall Stewart 				if ((ctl->stcb != control->stcb) && (ctl->length) &&
57309a6142d8SRandall Stewart 				    (ctl->some_taken ||
57316114cd96SRandall Stewart 				    (ctl->spec_flags & M_NOTIFICATION) ||
57329a6142d8SRandall Stewart 				    ((ctl->do_not_ref_stcb == 0) &&
57339a6142d8SRandall Stewart 				    (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))
57349a6142d8SRandall Stewart 				    ) {
57359a6142d8SRandall Stewart 					/*-
57369a6142d8SRandall Stewart 					 * If we have a different TCB next, and there is data
57379a6142d8SRandall Stewart 					 * present. If we have already taken some (pdapi), OR we can
57389a6142d8SRandall Stewart 					 * ref the tcb and no delivery as started on this stream, we
573917205eccSRandall Stewart 					 * take it. Note we allow a notification on a different
574017205eccSRandall Stewart 					 * assoc to be delivered..
57419a6142d8SRandall Stewart 					 */
57429a6142d8SRandall Stewart 					control = ctl;
57439a6142d8SRandall Stewart 					goto found_one;
57449a6142d8SRandall Stewart 				} else if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) &&
57459a6142d8SRandall Stewart 					    (ctl->length) &&
57469a6142d8SRandall Stewart 					    ((ctl->some_taken) ||
57479a6142d8SRandall Stewart 					    ((ctl->do_not_ref_stcb == 0) &&
574817205eccSRandall Stewart 					    ((ctl->spec_flags & M_NOTIFICATION) == 0) &&
5749b5c16493SMichael Tuexen 				    (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))) {
57509a6142d8SRandall Stewart 					/*-
57519a6142d8SRandall Stewart 					 * If we have the same tcb, and there is data present, and we
57529a6142d8SRandall Stewart 					 * have the strm interleave feature present. Then if we have
57539a6142d8SRandall Stewart 					 * taken some (pdapi) or we can refer to tht tcb AND we have
57549a6142d8SRandall Stewart 					 * not started a delivery for this stream, we can take it.
575517205eccSRandall Stewart 					 * Note we do NOT allow a notificaiton on the same assoc to
575617205eccSRandall Stewart 					 * be delivered.
57579a6142d8SRandall Stewart 					 */
5758f8829a4aSRandall Stewart 					control = ctl;
5759f8829a4aSRandall Stewart 					goto found_one;
5760f8829a4aSRandall Stewart 				}
5761f8829a4aSRandall Stewart 				ctl = TAILQ_NEXT(ctl, next);
5762f8829a4aSRandall Stewart 			}
5763f8829a4aSRandall Stewart 		}
5764f8829a4aSRandall Stewart 		/*
5765f8829a4aSRandall Stewart 		 * if we reach here, not suitable replacement is available
57664e88d37aSMichael Tuexen 		 * <or> fragment interleave is NOT on. So stuff the sb_cc
5767f8829a4aSRandall Stewart 		 * into the our held count, and its time to sleep again.
5768f8829a4aSRandall Stewart 		 */
57694e88d37aSMichael Tuexen 		held_length = so->so_rcv.sb_cc;
57704e88d37aSMichael Tuexen 		control->held_length = so->so_rcv.sb_cc;
5771f8829a4aSRandall Stewart 		goto restart;
5772f8829a4aSRandall Stewart 	}
5773f8829a4aSRandall Stewart 	/* Clear the held length since there is something to read */
5774f8829a4aSRandall Stewart 	control->held_length = 0;
5775f8829a4aSRandall Stewart found_one:
5776f8829a4aSRandall Stewart 	/*
5777f8829a4aSRandall Stewart 	 * If we reach here, control has a some data for us to read off.
5778f8829a4aSRandall Stewart 	 * Note that stcb COULD be NULL.
5779f8829a4aSRandall Stewart 	 */
57809c5ca6f2SMichael Tuexen 	if (hold_rlock == 0) {
57819c5ca6f2SMichael Tuexen 		hold_rlock = 1;
57829c5ca6f2SMichael Tuexen 		SCTP_INP_READ_LOCK(inp);
5783f8829a4aSRandall Stewart 	}
57849c5ca6f2SMichael Tuexen 	control->some_taken++;
5785f8829a4aSRandall Stewart 	stcb = control->stcb;
5786f8829a4aSRandall Stewart 	if (stcb) {
57870696e120SRandall Stewart 		if ((control->do_not_ref_stcb == 0) &&
57880696e120SRandall Stewart 		    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
578950cec919SRandall Stewart 			if (freecnt_applied == 0)
5790f8829a4aSRandall Stewart 				stcb = NULL;
5791f8829a4aSRandall Stewart 		} else if (control->do_not_ref_stcb == 0) {
5792f8829a4aSRandall Stewart 			/* you can't free it on me please */
5793f8829a4aSRandall Stewart 			/*
5794f8829a4aSRandall Stewart 			 * The lock on the socket buffer protects us so the
5795f8829a4aSRandall Stewart 			 * free code will stop. But since we used the
5796f8829a4aSRandall Stewart 			 * socketbuf lock and the sender uses the tcb_lock
5797f8829a4aSRandall Stewart 			 * to increment, we need to use the atomic add to
5798f8829a4aSRandall Stewart 			 * the refcnt
5799f8829a4aSRandall Stewart 			 */
5800d55b0b1bSRandall Stewart 			if (freecnt_applied) {
5801d55b0b1bSRandall Stewart #ifdef INVARIANTS
5802207304d4SRandall Stewart 				panic("refcnt already incremented");
5803d55b0b1bSRandall Stewart #else
5804cd3fd531SMichael Tuexen 				SCTP_PRINTF("refcnt already incremented?\n");
5805d55b0b1bSRandall Stewart #endif
5806d55b0b1bSRandall Stewart 			} else {
580750cec919SRandall Stewart 				atomic_add_int(&stcb->asoc.refcnt, 1);
5808f8829a4aSRandall Stewart 				freecnt_applied = 1;
5809d55b0b1bSRandall Stewart 			}
5810f8829a4aSRandall Stewart 			/*
5811f8829a4aSRandall Stewart 			 * Setup to remember how much we have not yet told
5812f8829a4aSRandall Stewart 			 * the peer our rwnd has opened up. Note we grab the
5813f8829a4aSRandall Stewart 			 * value from the tcb from last time. Note too that
58140696e120SRandall Stewart 			 * sack sending clears this when a sack is sent,
5815f8829a4aSRandall Stewart 			 * which is fine. Once we hit the rwnd_req, we then
5816f8829a4aSRandall Stewart 			 * will go to the sctp_user_rcvd() that will not
5817f8829a4aSRandall Stewart 			 * lock until it KNOWs it MUST send a WUP-SACK.
5818f8829a4aSRandall Stewart 			 */
581958e6eeefSMichael Tuexen 			freed_so_far = (uint32_t)stcb->freed_by_sorcv_sincelast;
5820f8829a4aSRandall Stewart 			stcb->freed_by_sorcv_sincelast = 0;
5821f8829a4aSRandall Stewart 		}
5822f8829a4aSRandall Stewart 	}
58236114cd96SRandall Stewart 	if (stcb &&
58246114cd96SRandall Stewart 	    ((control->spec_flags & M_NOTIFICATION) == 0) &&
58256114cd96SRandall Stewart 	    control->do_not_ref_stcb == 0) {
5826d06c82f1SRandall Stewart 		stcb->asoc.strmin[control->sinfo_stream].delivery_started = 1;
5827d06c82f1SRandall Stewart 	}
58280053ed28SMichael Tuexen 
5829f8829a4aSRandall Stewart 	/* First lets get off the sinfo and sockaddr info */
58305f05199cSMichael Tuexen 	if ((sinfo != NULL) && (filling_sinfo != 0)) {
58315f05199cSMichael Tuexen 		sinfo->sinfo_stream = control->sinfo_stream;
583249656eefSMichael Tuexen 		sinfo->sinfo_ssn = (uint16_t)control->mid;
58335f05199cSMichael Tuexen 		sinfo->sinfo_flags = control->sinfo_flags;
58345f05199cSMichael Tuexen 		sinfo->sinfo_ppid = control->sinfo_ppid;
58355f05199cSMichael Tuexen 		sinfo->sinfo_context = control->sinfo_context;
58365f05199cSMichael Tuexen 		sinfo->sinfo_timetolive = control->sinfo_timetolive;
58375f05199cSMichael Tuexen 		sinfo->sinfo_tsn = control->sinfo_tsn;
58385f05199cSMichael Tuexen 		sinfo->sinfo_cumtsn = control->sinfo_cumtsn;
58395f05199cSMichael Tuexen 		sinfo->sinfo_assoc_id = control->sinfo_assoc_id;
5840f8829a4aSRandall Stewart 		nxt = TAILQ_NEXT(control, next);
5841e2e7c62eSMichael Tuexen 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
5842e2e7c62eSMichael Tuexen 		    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) {
5843f8829a4aSRandall Stewart 			struct sctp_extrcvinfo *s_extra;
5844f8829a4aSRandall Stewart 
5845f8829a4aSRandall Stewart 			s_extra = (struct sctp_extrcvinfo *)sinfo;
58469a6142d8SRandall Stewart 			if ((nxt) &&
58479a6142d8SRandall Stewart 			    (nxt->length)) {
5848b70b526dSMichael Tuexen 				s_extra->serinfo_next_flags = SCTP_NEXT_MSG_AVAIL;
5849f8829a4aSRandall Stewart 				if (nxt->sinfo_flags & SCTP_UNORDERED) {
5850b70b526dSMichael Tuexen 					s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_IS_UNORDERED;
5851f8829a4aSRandall Stewart 				}
5852f42a358aSRandall Stewart 				if (nxt->spec_flags & M_NOTIFICATION) {
5853b70b526dSMichael Tuexen 					s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_IS_NOTIFICATION;
5854f42a358aSRandall Stewart 				}
5855b70b526dSMichael Tuexen 				s_extra->serinfo_next_aid = nxt->sinfo_assoc_id;
5856b70b526dSMichael Tuexen 				s_extra->serinfo_next_length = nxt->length;
5857b70b526dSMichael Tuexen 				s_extra->serinfo_next_ppid = nxt->sinfo_ppid;
5858b70b526dSMichael Tuexen 				s_extra->serinfo_next_stream = nxt->sinfo_stream;
5859f8829a4aSRandall Stewart 				if (nxt->tail_mbuf != NULL) {
5860139bc87fSRandall Stewart 					if (nxt->end_added) {
5861b70b526dSMichael Tuexen 						s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_ISCOMPLETE;
5862f8829a4aSRandall Stewart 					}
5863f8829a4aSRandall Stewart 				}
5864f8829a4aSRandall Stewart 			} else {
5865f8829a4aSRandall Stewart 				/*
5866f8829a4aSRandall Stewart 				 * we explicitly 0 this, since the memcpy
5867f8829a4aSRandall Stewart 				 * got some other things beyond the older
5868f8829a4aSRandall Stewart 				 * sinfo_ that is on the control's structure
5869f8829a4aSRandall Stewart 				 * :-D
5870f8829a4aSRandall Stewart 				 */
58719a6142d8SRandall Stewart 				nxt = NULL;
5872b70b526dSMichael Tuexen 				s_extra->serinfo_next_flags = SCTP_NO_NEXT_MSG;
5873b70b526dSMichael Tuexen 				s_extra->serinfo_next_aid = 0;
5874b70b526dSMichael Tuexen 				s_extra->serinfo_next_length = 0;
5875b70b526dSMichael Tuexen 				s_extra->serinfo_next_ppid = 0;
5876b70b526dSMichael Tuexen 				s_extra->serinfo_next_stream = 0;
5877f8829a4aSRandall Stewart 			}
5878f8829a4aSRandall Stewart 		}
5879f8829a4aSRandall Stewart 		/*
5880f8829a4aSRandall Stewart 		 * update off the real current cum-ack, if we have an stcb.
5881f8829a4aSRandall Stewart 		 */
58820696e120SRandall Stewart 		if ((control->do_not_ref_stcb == 0) && stcb)
5883f8829a4aSRandall Stewart 			sinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
5884f8829a4aSRandall Stewart 		/*
5885f8829a4aSRandall Stewart 		 * mask off the high bits, we keep the actual chunk bits in
5886f8829a4aSRandall Stewart 		 * there.
5887f8829a4aSRandall Stewart 		 */
5888f8829a4aSRandall Stewart 		sinfo->sinfo_flags &= 0x00ff;
58895f26a41dSRandall Stewart 		if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) {
58905f26a41dSRandall Stewart 			sinfo->sinfo_flags |= SCTP_UNORDERED;
58915f26a41dSRandall Stewart 		}
5892f8829a4aSRandall Stewart 	}
589318e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
589418e198d3SRandall Stewart 	{
589518e198d3SRandall Stewart 		int index, newindex;
589618e198d3SRandall Stewart 		struct sctp_pcbtsn_rlog *entry;
589718e198d3SRandall Stewart 
589818e198d3SRandall Stewart 		do {
589918e198d3SRandall Stewart 			index = inp->readlog_index;
590018e198d3SRandall Stewart 			newindex = index + 1;
590118e198d3SRandall Stewart 			if (newindex >= SCTP_READ_LOG_SIZE) {
590218e198d3SRandall Stewart 				newindex = 0;
590318e198d3SRandall Stewart 			}
590418e198d3SRandall Stewart 		} while (atomic_cmpset_int(&inp->readlog_index, index, newindex) == 0);
590518e198d3SRandall Stewart 		entry = &inp->readlog[index];
590618e198d3SRandall Stewart 		entry->vtag = control->sinfo_assoc_id;
590718e198d3SRandall Stewart 		entry->strm = control->sinfo_stream;
590849656eefSMichael Tuexen 		entry->seq = (uint16_t)control->mid;
590918e198d3SRandall Stewart 		entry->sz = control->length;
591018e198d3SRandall Stewart 		entry->flgs = control->sinfo_flags;
591118e198d3SRandall Stewart 	}
591218e198d3SRandall Stewart #endif
5913d59107f7SMichael Tuexen 	if ((fromlen > 0) && (from != NULL)) {
5914d59107f7SMichael Tuexen 		union sctp_sockstore store;
5915d59107f7SMichael Tuexen 		size_t len;
5916d59107f7SMichael Tuexen 
5917b5b6e5c2SMichael Tuexen 		switch (control->whoFrom->ro._l_addr.sa.sa_family) {
5918b5b6e5c2SMichael Tuexen #ifdef INET6
5919b5b6e5c2SMichael Tuexen 		case AF_INET6:
5920d59107f7SMichael Tuexen 			len = sizeof(struct sockaddr_in6);
5921d59107f7SMichael Tuexen 			store.sin6 = control->whoFrom->ro._l_addr.sin6;
5922d59107f7SMichael Tuexen 			store.sin6.sin6_port = control->port_from;
5923b5b6e5c2SMichael Tuexen 			break;
5924f8829a4aSRandall Stewart #endif
5925b5b6e5c2SMichael Tuexen #ifdef INET
5926b5b6e5c2SMichael Tuexen 		case AF_INET:
5927d59107f7SMichael Tuexen #ifdef INET6
5928d59107f7SMichael Tuexen 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
5929d59107f7SMichael Tuexen 				len = sizeof(struct sockaddr_in6);
5930d59107f7SMichael Tuexen 				in6_sin_2_v4mapsin6(&control->whoFrom->ro._l_addr.sin,
5931d59107f7SMichael Tuexen 				    &store.sin6);
5932d59107f7SMichael Tuexen 				store.sin6.sin6_port = control->port_from;
5933d59107f7SMichael Tuexen 			} else {
5934d59107f7SMichael Tuexen 				len = sizeof(struct sockaddr_in);
5935d59107f7SMichael Tuexen 				store.sin = control->whoFrom->ro._l_addr.sin;
5936d59107f7SMichael Tuexen 				store.sin.sin_port = control->port_from;
5937d59107f7SMichael Tuexen 			}
5938d59107f7SMichael Tuexen #else
5939d59107f7SMichael Tuexen 			len = sizeof(struct sockaddr_in);
5940d59107f7SMichael Tuexen 			store.sin = control->whoFrom->ro._l_addr.sin;
5941d59107f7SMichael Tuexen 			store.sin.sin_port = control->port_from;
5942d59107f7SMichael Tuexen #endif
5943b5b6e5c2SMichael Tuexen 			break;
5944b5b6e5c2SMichael Tuexen #endif
5945b5b6e5c2SMichael Tuexen 		default:
5946d59107f7SMichael Tuexen 			len = 0;
5947b5b6e5c2SMichael Tuexen 			break;
5948b5b6e5c2SMichael Tuexen 		}
5949d59107f7SMichael Tuexen 		memcpy(from, &store, min((size_t)fromlen, len));
5950e0e00a4dSMichael Tuexen #ifdef INET6
5951f8829a4aSRandall Stewart 		{
5952b5b6e5c2SMichael Tuexen 			struct sockaddr_in6 lsa6, *from6;
5953f8829a4aSRandall Stewart 
5954b5b6e5c2SMichael Tuexen 			from6 = (struct sockaddr_in6 *)from;
5955b5b6e5c2SMichael Tuexen 			sctp_recover_scope_mac(from6, (&lsa6));
5956f8829a4aSRandall Stewart 		}
5957f8829a4aSRandall Stewart #endif
5958f8829a4aSRandall Stewart 	}
59599c5ca6f2SMichael Tuexen 	if (hold_rlock) {
59609c5ca6f2SMichael Tuexen 		SCTP_INP_READ_UNLOCK(inp);
59619c5ca6f2SMichael Tuexen 		hold_rlock = 0;
59629c5ca6f2SMichael Tuexen 	}
59639c5ca6f2SMichael Tuexen 	if (hold_sblock) {
59649c5ca6f2SMichael Tuexen 		SOCKBUF_UNLOCK(&so->so_rcv);
59659c5ca6f2SMichael Tuexen 		hold_sblock = 0;
59669c5ca6f2SMichael Tuexen 	}
5967f8829a4aSRandall Stewart 	/* now copy out what data we can */
5968f8829a4aSRandall Stewart 	if (mp == NULL) {
5969f8829a4aSRandall Stewart 		/* copy out each mbuf in the chain up to length */
5970f8829a4aSRandall Stewart get_more_data:
5971f8829a4aSRandall Stewart 		m = control->data;
5972f8829a4aSRandall Stewart 		while (m) {
5973f8829a4aSRandall Stewart 			/* Move out all we can */
59740d3cf13dSMichael Tuexen 			cp_len = uio->uio_resid;
59750d3cf13dSMichael Tuexen 			my_len = SCTP_BUF_LEN(m);
5976f8829a4aSRandall Stewart 			if (cp_len > my_len) {
5977f8829a4aSRandall Stewart 				/* not enough in this buf */
5978f8829a4aSRandall Stewart 				cp_len = my_len;
5979f8829a4aSRandall Stewart 			}
5980f8829a4aSRandall Stewart 			if (hold_rlock) {
5981f8829a4aSRandall Stewart 				SCTP_INP_READ_UNLOCK(inp);
5982f8829a4aSRandall Stewart 				hold_rlock = 0;
5983f8829a4aSRandall Stewart 			}
5984f8829a4aSRandall Stewart 			if (cp_len > 0)
598558e6eeefSMichael Tuexen 				error = uiomove(mtod(m, char *), (int)cp_len, uio);
5986f8829a4aSRandall Stewart 			/* re-read */
5987f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5988f8829a4aSRandall Stewart 				goto release;
5989f8829a4aSRandall Stewart 			}
59900053ed28SMichael Tuexen 
59910696e120SRandall Stewart 			if ((control->do_not_ref_stcb == 0) && stcb &&
5992f8829a4aSRandall Stewart 			    stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5993f8829a4aSRandall Stewart 				no_rcv_needed = 1;
5994f8829a4aSRandall Stewart 			}
5995f8829a4aSRandall Stewart 			if (error) {
5996f8829a4aSRandall Stewart 				/* error we are out of here */
5997f8829a4aSRandall Stewart 				goto release;
5998f8829a4aSRandall Stewart 			}
5999f8829a4aSRandall Stewart 			SCTP_INP_READ_LOCK(inp);
6000f8829a4aSRandall Stewart 			hold_rlock = 1;
6001139bc87fSRandall Stewart 			if (cp_len == SCTP_BUF_LEN(m)) {
6002139bc87fSRandall Stewart 				if ((SCTP_BUF_NEXT(m) == NULL) &&
6003139bc87fSRandall Stewart 				    (control->end_added)) {
6004f8829a4aSRandall Stewart 					out_flags |= MSG_EOR;
600552129fcdSRandall Stewart 					if ((control->do_not_ref_stcb == 0) &&
600652129fcdSRandall Stewart 					    (control->stcb != NULL) &&
600752129fcdSRandall Stewart 					    ((control->spec_flags & M_NOTIFICATION) == 0))
6008ee7f9857SRandall Stewart 						control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6009f8829a4aSRandall Stewart 				}
6010139bc87fSRandall Stewart 				if (control->spec_flags & M_NOTIFICATION) {
6011f8829a4aSRandall Stewart 					out_flags |= MSG_NOTIFICATION;
6012f8829a4aSRandall Stewart 				}
6013f8829a4aSRandall Stewart 				/* we ate up the mbuf */
6014f8829a4aSRandall Stewart 				if (in_flags & MSG_PEEK) {
6015f8829a4aSRandall Stewart 					/* just looking */
6016139bc87fSRandall Stewart 					m = SCTP_BUF_NEXT(m);
6017f8829a4aSRandall Stewart 					copied_so_far += cp_len;
6018f8829a4aSRandall Stewart 				} else {
6019f8829a4aSRandall Stewart 					/* dispose of the mbuf */
6020b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6021f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv,
6022139bc87fSRandall Stewart 						    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
602380fefe0aSRandall Stewart 					}
6024f8829a4aSRandall Stewart 					sctp_sbfree(control, stcb, &so->so_rcv, m);
6025b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6026f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv,
6027f8829a4aSRandall Stewart 						    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
602880fefe0aSRandall Stewart 					}
6029f8829a4aSRandall Stewart 					copied_so_far += cp_len;
603058e6eeefSMichael Tuexen 					freed_so_far += (uint32_t)cp_len;
6031c4739e2fSRandall Stewart 					freed_so_far += MSIZE;
603218e198d3SRandall Stewart 					atomic_subtract_int(&control->length, cp_len);
6033f8829a4aSRandall Stewart 					control->data = sctp_m_free(m);
6034f8829a4aSRandall Stewart 					m = control->data;
6035b7b84c0eSMichael Tuexen 					/*
6036b7b84c0eSMichael Tuexen 					 * been through it all, must hold sb
6037b7b84c0eSMichael Tuexen 					 * lock ok to null tail
6038b7b84c0eSMichael Tuexen 					 */
6039f8829a4aSRandall Stewart 					if (control->data == NULL) {
6040a5d547adSRandall Stewart #ifdef INVARIANTS
6041f8829a4aSRandall Stewart 						if ((control->end_added == 0) ||
6042f8829a4aSRandall Stewart 						    (TAILQ_NEXT(control, next) == NULL)) {
6043f8829a4aSRandall Stewart 							/*
6044f8829a4aSRandall Stewart 							 * If the end is not
6045f8829a4aSRandall Stewart 							 * added, OR the
6046f8829a4aSRandall Stewart 							 * next is NOT null
6047f8829a4aSRandall Stewart 							 * we MUST have the
6048f8829a4aSRandall Stewart 							 * lock.
6049f8829a4aSRandall Stewart 							 */
6050f8829a4aSRandall Stewart 							if (mtx_owned(&inp->inp_rdata_mtx) == 0) {
6051f8829a4aSRandall Stewart 								panic("Hmm we don't own the lock?");
6052f8829a4aSRandall Stewart 							}
6053f8829a4aSRandall Stewart 						}
6054f8829a4aSRandall Stewart #endif
6055f8829a4aSRandall Stewart 						control->tail_mbuf = NULL;
6056a5d547adSRandall Stewart #ifdef INVARIANTS
6057f8829a4aSRandall Stewart 						if ((control->end_added) && ((out_flags & MSG_EOR) == 0)) {
6058f8829a4aSRandall Stewart 							panic("end_added, nothing left and no MSG_EOR");
6059f8829a4aSRandall Stewart 						}
6060f8829a4aSRandall Stewart #endif
6061f8829a4aSRandall Stewart 					}
6062f8829a4aSRandall Stewart 				}
6063f8829a4aSRandall Stewart 			} else {
6064f8829a4aSRandall Stewart 				/* Do we need to trim the mbuf? */
6065139bc87fSRandall Stewart 				if (control->spec_flags & M_NOTIFICATION) {
6066f8829a4aSRandall Stewart 					out_flags |= MSG_NOTIFICATION;
6067f8829a4aSRandall Stewart 				}
6068f8829a4aSRandall Stewart 				if ((in_flags & MSG_PEEK) == 0) {
6069139bc87fSRandall Stewart 					SCTP_BUF_RESV_UF(m, cp_len);
607058e6eeefSMichael Tuexen 					SCTP_BUF_LEN(m) -= (int)cp_len;
6071b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
607258e6eeefSMichael Tuexen 						sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, (int)cp_len);
607380fefe0aSRandall Stewart 					}
60744e88d37aSMichael Tuexen 					atomic_subtract_int(&so->so_rcv.sb_cc, cp_len);
60750696e120SRandall Stewart 					if ((control->do_not_ref_stcb == 0) &&
60760696e120SRandall Stewart 					    stcb) {
60774e88d37aSMichael Tuexen 						atomic_subtract_int(&stcb->asoc.sb_cc, cp_len);
6078f8829a4aSRandall Stewart 					}
6079f8829a4aSRandall Stewart 					copied_so_far += cp_len;
608058e6eeefSMichael Tuexen 					freed_so_far += (uint32_t)cp_len;
6081c4739e2fSRandall Stewart 					freed_so_far += MSIZE;
6082b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6083f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb,
6084f8829a4aSRandall Stewart 						    SCTP_LOG_SBRESULT, 0);
608580fefe0aSRandall Stewart 					}
608618e198d3SRandall Stewart 					atomic_subtract_int(&control->length, cp_len);
6087f8829a4aSRandall Stewart 				} else {
6088f8829a4aSRandall Stewart 					copied_so_far += cp_len;
6089f8829a4aSRandall Stewart 				}
6090f8829a4aSRandall Stewart 			}
6091d61a0ae0SRandall Stewart 			if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) {
6092f8829a4aSRandall Stewart 				break;
6093f8829a4aSRandall Stewart 			}
6094f8829a4aSRandall Stewart 			if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
6095f8829a4aSRandall Stewart 			    (control->do_not_ref_stcb == 0) &&
6096f8829a4aSRandall Stewart 			    (freed_so_far >= rwnd_req)) {
6097f8829a4aSRandall Stewart 				sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6098f8829a4aSRandall Stewart 			}
6099f8829a4aSRandall Stewart 		}		/* end while(m) */
6100f8829a4aSRandall Stewart 		/*
6101f8829a4aSRandall Stewart 		 * At this point we have looked at it all and we either have
6102f8829a4aSRandall Stewart 		 * a MSG_EOR/or read all the user wants... <OR>
6103f8829a4aSRandall Stewart 		 * control->length == 0.
6104f8829a4aSRandall Stewart 		 */
6105d61a0ae0SRandall Stewart 		if ((out_flags & MSG_EOR) && ((in_flags & MSG_PEEK) == 0)) {
6106f8829a4aSRandall Stewart 			/* we are done with this control */
6107f8829a4aSRandall Stewart 			if (control->length == 0) {
6108f8829a4aSRandall Stewart 				if (control->data) {
6109a5d547adSRandall Stewart #ifdef INVARIANTS
6110f8829a4aSRandall Stewart 					panic("control->data not null at read eor?");
6111f8829a4aSRandall Stewart #else
6112ad81507eSRandall Stewart 					SCTP_PRINTF("Strange, data left in the control buffer .. invarients would panic?\n");
6113f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
6114f8829a4aSRandall Stewart 					control->data = NULL;
6115f8829a4aSRandall Stewart #endif
6116f8829a4aSRandall Stewart 				}
6117f8829a4aSRandall Stewart 		done_with_control:
6118f8829a4aSRandall Stewart 				if (hold_rlock == 0) {
6119f8829a4aSRandall Stewart 					SCTP_INP_READ_LOCK(inp);
6120f8829a4aSRandall Stewart 					hold_rlock = 1;
6121f8829a4aSRandall Stewart 				}
6122f8829a4aSRandall Stewart 				TAILQ_REMOVE(&inp->read_queue, control, next);
6123f8829a4aSRandall Stewart 				/* Add back any hiddend data */
6124f8829a4aSRandall Stewart 				if (control->held_length) {
6125f8829a4aSRandall Stewart 					held_length = 0;
6126f8829a4aSRandall Stewart 					control->held_length = 0;
6127f8829a4aSRandall Stewart 					wakeup_read_socket = 1;
6128f8829a4aSRandall Stewart 				}
612917205eccSRandall Stewart 				if (control->aux_data) {
613017205eccSRandall Stewart 					sctp_m_free(control->aux_data);
613117205eccSRandall Stewart 					control->aux_data = NULL;
613217205eccSRandall Stewart 				}
6133f8829a4aSRandall Stewart 				no_rcv_needed = control->do_not_ref_stcb;
6134f8829a4aSRandall Stewart 				sctp_free_remote_addr(control->whoFrom);
6135f8829a4aSRandall Stewart 				control->data = NULL;
613698d5fd97SMichael Tuexen #ifdef INVARIANTS
613744249214SRandall Stewart 				if (control->on_strm_q) {
613844249214SRandall Stewart 					panic("About to free ctl:%p so:%p and its in %d",
613944249214SRandall Stewart 					    control, so, control->on_strm_q);
614044249214SRandall Stewart 				}
614198d5fd97SMichael Tuexen #endif
6142f8829a4aSRandall Stewart 				sctp_free_a_readq(stcb, control);
6143f8829a4aSRandall Stewart 				control = NULL;
61440696e120SRandall Stewart 				if ((freed_so_far >= rwnd_req) &&
61450696e120SRandall Stewart 				    (no_rcv_needed == 0))
6146f8829a4aSRandall Stewart 					sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6147f8829a4aSRandall Stewart 
6148f8829a4aSRandall Stewart 			} else {
6149f8829a4aSRandall Stewart 				/*
6150f8829a4aSRandall Stewart 				 * The user did not read all of this
6151f8829a4aSRandall Stewart 				 * message, turn off the returned MSG_EOR
6152f8829a4aSRandall Stewart 				 * since we are leaving more behind on the
6153f8829a4aSRandall Stewart 				 * control to read.
6154f8829a4aSRandall Stewart 				 */
6155a5d547adSRandall Stewart #ifdef INVARIANTS
61560696e120SRandall Stewart 				if (control->end_added &&
61570696e120SRandall Stewart 				    (control->data == NULL) &&
6158f8829a4aSRandall Stewart 				    (control->tail_mbuf == NULL)) {
6159f8829a4aSRandall Stewart 					panic("Gak, control->length is corrupt?");
6160f8829a4aSRandall Stewart 				}
6161f8829a4aSRandall Stewart #endif
6162f8829a4aSRandall Stewart 				no_rcv_needed = control->do_not_ref_stcb;
6163f8829a4aSRandall Stewart 				out_flags &= ~MSG_EOR;
6164f8829a4aSRandall Stewart 			}
6165f8829a4aSRandall Stewart 		}
6166f8829a4aSRandall Stewart 		if (out_flags & MSG_EOR) {
6167f8829a4aSRandall Stewart 			goto release;
6168f8829a4aSRandall Stewart 		}
6169f8829a4aSRandall Stewart 		if ((uio->uio_resid == 0) ||
617004aab884SMichael Tuexen 		    ((in_eeor_mode) &&
617143ecbff2SMichael Tuexen 		    (copied_so_far >= max(so->so_rcv.sb_lowat, 1)))) {
6172f8829a4aSRandall Stewart 			goto release;
6173f8829a4aSRandall Stewart 		}
6174f8829a4aSRandall Stewart 		/*
6175f8829a4aSRandall Stewart 		 * If I hit here the receiver wants more and this message is
6176f8829a4aSRandall Stewart 		 * NOT done (pd-api). So two questions. Can we block? if not
6177f8829a4aSRandall Stewart 		 * we are done. Did the user NOT set MSG_WAITALL?
6178f8829a4aSRandall Stewart 		 */
6179f8829a4aSRandall Stewart 		if (block_allowed == 0) {
6180f8829a4aSRandall Stewart 			goto release;
6181f8829a4aSRandall Stewart 		}
6182f8829a4aSRandall Stewart 		/*
6183f8829a4aSRandall Stewart 		 * We need to wait for more data a few things: - We don't
6184f8829a4aSRandall Stewart 		 * sbunlock() so we don't get someone else reading. - We
6185f8829a4aSRandall Stewart 		 * must be sure to account for the case where what is added
6186f8829a4aSRandall Stewart 		 * is NOT to our control when we wakeup.
6187f8829a4aSRandall Stewart 		 */
6188f8829a4aSRandall Stewart 
6189f8829a4aSRandall Stewart 		/*
6190f8829a4aSRandall Stewart 		 * Do we need to tell the transport a rwnd update might be
6191f8829a4aSRandall Stewart 		 * needed before we go to sleep?
6192f8829a4aSRandall Stewart 		 */
6193f8829a4aSRandall Stewart 		if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
6194f8829a4aSRandall Stewart 		    ((freed_so_far >= rwnd_req) &&
6195f8829a4aSRandall Stewart 		    (control->do_not_ref_stcb == 0) &&
6196f8829a4aSRandall Stewart 		    (no_rcv_needed == 0))) {
6197f8829a4aSRandall Stewart 			sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6198f8829a4aSRandall Stewart 		}
6199f8829a4aSRandall Stewart wait_some_more:
620044b7479bSRandall Stewart 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
6201f8829a4aSRandall Stewart 			goto release;
6202f8829a4aSRandall Stewart 		}
62030053ed28SMichael Tuexen 
6204f8829a4aSRandall Stewart 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)
6205f8829a4aSRandall Stewart 			goto release;
6206f8829a4aSRandall Stewart 
6207f8829a4aSRandall Stewart 		if (hold_rlock == 1) {
6208f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
6209f8829a4aSRandall Stewart 			hold_rlock = 0;
6210f8829a4aSRandall Stewart 		}
6211f8829a4aSRandall Stewart 		if (hold_sblock == 0) {
6212f8829a4aSRandall Stewart 			SOCKBUF_LOCK(&so->so_rcv);
6213f8829a4aSRandall Stewart 			hold_sblock = 1;
6214f8829a4aSRandall Stewart 		}
6215851b7298SRandall Stewart 		if ((copied_so_far) && (control->length == 0) &&
6216b5c16493SMichael Tuexen 		    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE))) {
6217851b7298SRandall Stewart 			goto release;
6218851b7298SRandall Stewart 		}
62194e88d37aSMichael Tuexen 		if (so->so_rcv.sb_cc <= control->held_length) {
6220f8829a4aSRandall Stewart 			error = sbwait(&so->so_rcv);
6221f8829a4aSRandall Stewart 			if (error) {
6222f8829a4aSRandall Stewart 				goto release;
6223f8829a4aSRandall Stewart 			}
6224f8829a4aSRandall Stewart 			control->held_length = 0;
6225f8829a4aSRandall Stewart 		}
6226f8829a4aSRandall Stewart 		if (hold_sblock) {
6227f8829a4aSRandall Stewart 			SOCKBUF_UNLOCK(&so->so_rcv);
6228f8829a4aSRandall Stewart 			hold_sblock = 0;
6229f8829a4aSRandall Stewart 		}
6230f8829a4aSRandall Stewart 		if (control->length == 0) {
6231f8829a4aSRandall Stewart 			/* still nothing here */
6232f8829a4aSRandall Stewart 			if (control->end_added == 1) {
6233f8829a4aSRandall Stewart 				/* he aborted, or is done i.e.did a shutdown */
6234f8829a4aSRandall Stewart 				out_flags |= MSG_EOR;
62359a6142d8SRandall Stewart 				if (control->pdapi_aborted) {
62366114cd96SRandall Stewart 					if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
6237ee7f9857SRandall Stewart 						control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
62389a6142d8SRandall Stewart 
623903b0b021SRandall Stewart 					out_flags |= MSG_TRUNC;
62409a6142d8SRandall Stewart 				} else {
62416114cd96SRandall Stewart 					if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
6242ee7f9857SRandall Stewart 						control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
62439a6142d8SRandall Stewart 				}
6244f8829a4aSRandall Stewart 				goto done_with_control;
6245f8829a4aSRandall Stewart 			}
62464e88d37aSMichael Tuexen 			if (so->so_rcv.sb_cc > held_length) {
62474e88d37aSMichael Tuexen 				control->held_length = so->so_rcv.sb_cc;
6248f8829a4aSRandall Stewart 				held_length = 0;
6249f8829a4aSRandall Stewart 			}
6250f8829a4aSRandall Stewart 			goto wait_some_more;
6251f8829a4aSRandall Stewart 		} else if (control->data == NULL) {
625250cec919SRandall Stewart 			/*
625350cec919SRandall Stewart 			 * we must re-sync since data is probably being
625450cec919SRandall Stewart 			 * added
625550cec919SRandall Stewart 			 */
625650cec919SRandall Stewart 			SCTP_INP_READ_LOCK(inp);
625750cec919SRandall Stewart 			if ((control->length > 0) && (control->data == NULL)) {
6258b7b84c0eSMichael Tuexen 				/*
6259b7b84c0eSMichael Tuexen 				 * big trouble.. we have the lock and its
6260b7b84c0eSMichael Tuexen 				 * corrupt?
6261b7b84c0eSMichael Tuexen 				 */
62629c04b296SRandall Stewart #ifdef INVARIANTS
62639d18771fSRandall Stewart 				panic("Impossible data==NULL length !=0");
62649c04b296SRandall Stewart #endif
62659c04b296SRandall Stewart 				out_flags |= MSG_EOR;
62669c04b296SRandall Stewart 				out_flags |= MSG_TRUNC;
62679c04b296SRandall Stewart 				control->length = 0;
62689c04b296SRandall Stewart 				SCTP_INP_READ_UNLOCK(inp);
62699c04b296SRandall Stewart 				goto done_with_control;
6270f8829a4aSRandall Stewart 			}
627150cec919SRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
627250cec919SRandall Stewart 			/* We will fall around to get more data */
627350cec919SRandall Stewart 		}
6274f8829a4aSRandall Stewart 		goto get_more_data;
6275f8829a4aSRandall Stewart 	} else {
627617205eccSRandall Stewart 		/*-
627717205eccSRandall Stewart 		 * Give caller back the mbuf chain,
627817205eccSRandall Stewart 		 * store in uio_resid the length
6279f8829a4aSRandall Stewart 		 */
628017205eccSRandall Stewart 		wakeup_read_socket = 0;
6281f8829a4aSRandall Stewart 		if ((control->end_added == 0) ||
6282f8829a4aSRandall Stewart 		    (TAILQ_NEXT(control, next) == NULL)) {
6283f8829a4aSRandall Stewart 			/* Need to get rlock */
6284f8829a4aSRandall Stewart 			if (hold_rlock == 0) {
6285f8829a4aSRandall Stewart 				SCTP_INP_READ_LOCK(inp);
6286f8829a4aSRandall Stewart 				hold_rlock = 1;
6287f8829a4aSRandall Stewart 			}
6288f8829a4aSRandall Stewart 		}
6289139bc87fSRandall Stewart 		if (control->end_added) {
6290f8829a4aSRandall Stewart 			out_flags |= MSG_EOR;
629160990c0cSMichael Tuexen 			if ((control->do_not_ref_stcb == 0) &&
629260990c0cSMichael Tuexen 			    (control->stcb != NULL) &&
629360990c0cSMichael Tuexen 			    ((control->spec_flags & M_NOTIFICATION) == 0))
6294ee7f9857SRandall Stewart 				control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6295f8829a4aSRandall Stewart 		}
6296139bc87fSRandall Stewart 		if (control->spec_flags & M_NOTIFICATION) {
6297f8829a4aSRandall Stewart 			out_flags |= MSG_NOTIFICATION;
6298f8829a4aSRandall Stewart 		}
629917205eccSRandall Stewart 		uio->uio_resid = control->length;
6300f8829a4aSRandall Stewart 		*mp = control->data;
6301f8829a4aSRandall Stewart 		m = control->data;
6302f8829a4aSRandall Stewart 		while (m) {
6303b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6304f8829a4aSRandall Stewart 				sctp_sblog(&so->so_rcv,
6305139bc87fSRandall Stewart 				    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
630680fefe0aSRandall Stewart 			}
6307f8829a4aSRandall Stewart 			sctp_sbfree(control, stcb, &so->so_rcv, m);
630858e6eeefSMichael Tuexen 			freed_so_far += (uint32_t)SCTP_BUF_LEN(m);
6309c4739e2fSRandall Stewart 			freed_so_far += MSIZE;
6310b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6311f8829a4aSRandall Stewart 				sctp_sblog(&so->so_rcv,
6312f8829a4aSRandall Stewart 				    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
631380fefe0aSRandall Stewart 			}
6314139bc87fSRandall Stewart 			m = SCTP_BUF_NEXT(m);
6315f8829a4aSRandall Stewart 		}
6316f8829a4aSRandall Stewart 		control->data = control->tail_mbuf = NULL;
6317f8829a4aSRandall Stewart 		control->length = 0;
6318f8829a4aSRandall Stewart 		if (out_flags & MSG_EOR) {
6319f8829a4aSRandall Stewart 			/* Done with this control */
6320f8829a4aSRandall Stewart 			goto done_with_control;
6321f8829a4aSRandall Stewart 		}
6322f8829a4aSRandall Stewart 	}
6323f8829a4aSRandall Stewart release:
6324f8829a4aSRandall Stewart 	if (hold_rlock == 1) {
6325f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
6326f8829a4aSRandall Stewart 		hold_rlock = 0;
6327f8829a4aSRandall Stewart 	}
63287abab911SRobert Watson 	if (hold_sblock == 1) {
63297abab911SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
63307abab911SRobert Watson 		hold_sblock = 0;
6331f8829a4aSRandall Stewart 	}
63320053ed28SMichael Tuexen 
6333f8829a4aSRandall Stewart 	sbunlock(&so->so_rcv);
63347abab911SRobert Watson 	sockbuf_lock = 0;
6335f8829a4aSRandall Stewart 
6336f8829a4aSRandall Stewart release_unlocked:
6337f8829a4aSRandall Stewart 	if (hold_sblock) {
6338f8829a4aSRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
6339f8829a4aSRandall Stewart 		hold_sblock = 0;
6340f8829a4aSRandall Stewart 	}
6341f8829a4aSRandall Stewart 	if ((stcb) && (in_flags & MSG_PEEK) == 0) {
6342f8829a4aSRandall Stewart 		if ((freed_so_far >= rwnd_req) &&
6343f8829a4aSRandall Stewart 		    (control && (control->do_not_ref_stcb == 0)) &&
6344f8829a4aSRandall Stewart 		    (no_rcv_needed == 0))
6345f8829a4aSRandall Stewart 			sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6346f8829a4aSRandall Stewart 	}
6347f8829a4aSRandall Stewart out:
63481b9f62a0SRandall Stewart 	if (msg_flags) {
63491b9f62a0SRandall Stewart 		*msg_flags = out_flags;
63501b9f62a0SRandall Stewart 	}
63519a6142d8SRandall Stewart 	if (((out_flags & MSG_EOR) == 0) &&
63529a6142d8SRandall Stewart 	    ((in_flags & MSG_PEEK) == 0) &&
63539a6142d8SRandall Stewart 	    (sinfo) &&
6354e2e7c62eSMichael Tuexen 	    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
6355e2e7c62eSMichael Tuexen 	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO))) {
63569a6142d8SRandall Stewart 		struct sctp_extrcvinfo *s_extra;
63579a6142d8SRandall Stewart 
63589a6142d8SRandall Stewart 		s_extra = (struct sctp_extrcvinfo *)sinfo;
6359b70b526dSMichael Tuexen 		s_extra->serinfo_next_flags = SCTP_NO_NEXT_MSG;
63609a6142d8SRandall Stewart 	}
6361f8829a4aSRandall Stewart 	if (hold_rlock == 1) {
6362f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
6363f8829a4aSRandall Stewart 	}
6364f8829a4aSRandall Stewart 	if (hold_sblock) {
6365f8829a4aSRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
6366f8829a4aSRandall Stewart 	}
63677abab911SRobert Watson 	if (sockbuf_lock) {
63687abab911SRobert Watson 		sbunlock(&so->so_rcv);
63697abab911SRobert Watson 	}
63700053ed28SMichael Tuexen 
637150cec919SRandall Stewart 	if (freecnt_applied) {
6372f8829a4aSRandall Stewart 		/*
6373f8829a4aSRandall Stewart 		 * The lock on the socket buffer protects us so the free
6374f8829a4aSRandall Stewart 		 * code will stop. But since we used the socketbuf lock and
6375f8829a4aSRandall Stewart 		 * the sender uses the tcb_lock to increment, we need to use
6376f8829a4aSRandall Stewart 		 * the atomic add to the refcnt.
6377f8829a4aSRandall Stewart 		 */
637850cec919SRandall Stewart 		if (stcb == NULL) {
6379df6e0cc3SRandall Stewart #ifdef INVARIANTS
638050cec919SRandall Stewart 			panic("stcb for refcnt has gone NULL?");
6381df6e0cc3SRandall Stewart 			goto stage_left;
6382df6e0cc3SRandall Stewart #else
6383df6e0cc3SRandall Stewart 			goto stage_left;
6384df6e0cc3SRandall Stewart #endif
638550cec919SRandall Stewart 		}
6386f8829a4aSRandall Stewart 		/* Save the value back for next time */
6387f8829a4aSRandall Stewart 		stcb->freed_by_sorcv_sincelast = freed_so_far;
6388cf46caceSMichael Tuexen 		atomic_add_int(&stcb->asoc.refcnt, -1);
6389f8829a4aSRandall Stewart 	}
6390b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
6391f8829a4aSRandall Stewart 		if (stcb) {
6392f8829a4aSRandall Stewart 			sctp_misc_ints(SCTP_SORECV_DONE,
6393f8829a4aSRandall Stewart 			    freed_so_far,
63949a8e3088SMichael Tuexen 			    (uint32_t)((uio) ? (slen - uio->uio_resid) : slen),
6395f8829a4aSRandall Stewart 			    stcb->asoc.my_rwnd,
63964e88d37aSMichael Tuexen 			    so->so_rcv.sb_cc);
6397f8829a4aSRandall Stewart 		} else {
6398f8829a4aSRandall Stewart 			sctp_misc_ints(SCTP_SORECV_DONE,
6399f8829a4aSRandall Stewart 			    freed_so_far,
64009a8e3088SMichael Tuexen 			    (uint32_t)((uio) ? (slen - uio->uio_resid) : slen),
6401f8829a4aSRandall Stewart 			    0,
64024e88d37aSMichael Tuexen 			    so->so_rcv.sb_cc);
6403f8829a4aSRandall Stewart 		}
640480fefe0aSRandall Stewart 	}
6405df6e0cc3SRandall Stewart stage_left:
6406f8829a4aSRandall Stewart 	if (wakeup_read_socket) {
6407f8829a4aSRandall Stewart 		sctp_sorwakeup(inp, so);
6408f8829a4aSRandall Stewart 	}
6409f8829a4aSRandall Stewart 	return (error);
6410f8829a4aSRandall Stewart }
6411f8829a4aSRandall Stewart 
6412f8829a4aSRandall Stewart 
6413f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
6414f8829a4aSRandall Stewart struct mbuf *
6415f8829a4aSRandall Stewart sctp_m_free(struct mbuf *m)
6416f8829a4aSRandall Stewart {
6417b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6418f8829a4aSRandall Stewart 		sctp_log_mb(m, SCTP_MBUF_IFREE);
6419f8829a4aSRandall Stewart 	}
6420f8829a4aSRandall Stewart 	return (m_free(m));
6421f8829a4aSRandall Stewart }
6422f8829a4aSRandall Stewart 
6423f8829a4aSRandall Stewart void
6424f8829a4aSRandall Stewart sctp_m_freem(struct mbuf *mb)
6425f8829a4aSRandall Stewart {
6426f8829a4aSRandall Stewart 	while (mb != NULL)
6427f8829a4aSRandall Stewart 		mb = sctp_m_free(mb);
6428f8829a4aSRandall Stewart }
6429f8829a4aSRandall Stewart 
6430f8829a4aSRandall Stewart #endif
6431f8829a4aSRandall Stewart 
643242551e99SRandall Stewart int
643342551e99SRandall Stewart sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id)
643442551e99SRandall Stewart {
643542551e99SRandall Stewart 	/*
643642551e99SRandall Stewart 	 * Given a local address. For all associations that holds the
643742551e99SRandall Stewart 	 * address, request a peer-set-primary.
643842551e99SRandall Stewart 	 */
643942551e99SRandall Stewart 	struct sctp_ifa *ifa;
644042551e99SRandall Stewart 	struct sctp_laddr *wi;
644142551e99SRandall Stewart 
644242551e99SRandall Stewart 	ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0);
644342551e99SRandall Stewart 	if (ifa == NULL) {
6444c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EADDRNOTAVAIL);
644542551e99SRandall Stewart 		return (EADDRNOTAVAIL);
644642551e99SRandall Stewart 	}
644742551e99SRandall Stewart 	/*
644842551e99SRandall Stewart 	 * Now that we have the ifa we must awaken the iterator with this
644942551e99SRandall Stewart 	 * message.
645042551e99SRandall Stewart 	 */
6451b3f1ea41SRandall Stewart 	wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
645242551e99SRandall Stewart 	if (wi == NULL) {
6453c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
645442551e99SRandall Stewart 		return (ENOMEM);
645542551e99SRandall Stewart 	}
645642551e99SRandall Stewart 	/* Now incr the count and int wi structure */
645742551e99SRandall Stewart 	SCTP_INCR_LADDR_COUNT();
64585ba7f91fSMichael Tuexen 	memset(wi, 0, sizeof(*wi));
6459d61a0ae0SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
646042551e99SRandall Stewart 	wi->ifa = ifa;
646142551e99SRandall Stewart 	wi->action = SCTP_SET_PRIM_ADDR;
646242551e99SRandall Stewart 	atomic_add_int(&ifa->refcount, 1);
646342551e99SRandall Stewart 
646442551e99SRandall Stewart 	/* Now add it to the work queue */
6465f7517433SRandall Stewart 	SCTP_WQ_ADDR_LOCK();
646642551e99SRandall Stewart 	/*
646742551e99SRandall Stewart 	 * Should this really be a tailq? As it is we will process the
646842551e99SRandall Stewart 	 * newest first :-0
646942551e99SRandall Stewart 	 */
6470b3f1ea41SRandall Stewart 	LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
647142551e99SRandall Stewart 	sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
647242551e99SRandall Stewart 	    (struct sctp_inpcb *)NULL,
647342551e99SRandall Stewart 	    (struct sctp_tcb *)NULL,
647442551e99SRandall Stewart 	    (struct sctp_nets *)NULL);
64752c62ba73SMichael Tuexen 	SCTP_WQ_ADDR_UNLOCK();
647642551e99SRandall Stewart 	return (0);
647742551e99SRandall Stewart }
647842551e99SRandall Stewart 
647942551e99SRandall Stewart 
6480f8829a4aSRandall Stewart int
648117205eccSRandall Stewart sctp_soreceive(struct socket *so,
648217205eccSRandall Stewart     struct sockaddr **psa,
648317205eccSRandall Stewart     struct uio *uio,
648417205eccSRandall Stewart     struct mbuf **mp0,
648517205eccSRandall Stewart     struct mbuf **controlp,
648617205eccSRandall Stewart     int *flagsp)
6487f8829a4aSRandall Stewart {
6488f8829a4aSRandall Stewart 	int error, fromlen;
6489f8829a4aSRandall Stewart 	uint8_t sockbuf[256];
6490f8829a4aSRandall Stewart 	struct sockaddr *from;
6491f8829a4aSRandall Stewart 	struct sctp_extrcvinfo sinfo;
6492f8829a4aSRandall Stewart 	int filling_sinfo = 1;
649346bf534cSMichael Tuexen 	int flags;
6494f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
6495f8829a4aSRandall Stewart 
6496f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
6497f8829a4aSRandall Stewart 	/* pickup the assoc we are reading from */
6498f8829a4aSRandall Stewart 	if (inp == NULL) {
6499c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6500f8829a4aSRandall Stewart 		return (EINVAL);
6501f8829a4aSRandall Stewart 	}
6502e2e7c62eSMichael Tuexen 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) &&
6503e2e7c62eSMichael Tuexen 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
6504e2e7c62eSMichael Tuexen 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) ||
6505f8829a4aSRandall Stewart 	    (controlp == NULL)) {
6506f8829a4aSRandall Stewart 		/* user does not want the sndrcv ctl */
6507f8829a4aSRandall Stewart 		filling_sinfo = 0;
6508f8829a4aSRandall Stewart 	}
6509f8829a4aSRandall Stewart 	if (psa) {
6510f8829a4aSRandall Stewart 		from = (struct sockaddr *)sockbuf;
6511f8829a4aSRandall Stewart 		fromlen = sizeof(sockbuf);
6512f8829a4aSRandall Stewart 		from->sa_len = 0;
6513f8829a4aSRandall Stewart 	} else {
6514f8829a4aSRandall Stewart 		from = NULL;
6515f8829a4aSRandall Stewart 		fromlen = 0;
6516f8829a4aSRandall Stewart 	}
6517f8829a4aSRandall Stewart 
6518e432298aSXin LI 	if (filling_sinfo) {
6519e432298aSXin LI 		memset(&sinfo, 0, sizeof(struct sctp_extrcvinfo));
6520e432298aSXin LI 	}
652146bf534cSMichael Tuexen 	if (flagsp != NULL) {
652246bf534cSMichael Tuexen 		flags = *flagsp;
652346bf534cSMichael Tuexen 	} else {
652446bf534cSMichael Tuexen 		flags = 0;
652546bf534cSMichael Tuexen 	}
652646bf534cSMichael Tuexen 	error = sctp_sorecvmsg(so, uio, mp0, from, fromlen, &flags,
6527f8829a4aSRandall Stewart 	    (struct sctp_sndrcvinfo *)&sinfo, filling_sinfo);
652846bf534cSMichael Tuexen 	if (flagsp != NULL) {
652946bf534cSMichael Tuexen 		*flagsp = flags;
653046bf534cSMichael Tuexen 	}
6531e432298aSXin LI 	if (controlp != NULL) {
6532f8829a4aSRandall Stewart 		/* copy back the sinfo in a CMSG format */
653346bf534cSMichael Tuexen 		if (filling_sinfo && ((flags & MSG_NOTIFICATION) == 0)) {
6534f8829a4aSRandall Stewart 			*controlp = sctp_build_ctl_nchunk(inp,
6535f8829a4aSRandall Stewart 			    (struct sctp_sndrcvinfo *)&sinfo);
653646bf534cSMichael Tuexen 		} else {
6537f8829a4aSRandall Stewart 			*controlp = NULL;
6538f8829a4aSRandall Stewart 		}
653946bf534cSMichael Tuexen 	}
6540f8829a4aSRandall Stewart 	if (psa) {
6541f8829a4aSRandall Stewart 		/* copy back the address info */
6542f8829a4aSRandall Stewart 		if (from && from->sa_len) {
6543f8829a4aSRandall Stewart 			*psa = sodupsockaddr(from, M_NOWAIT);
6544f8829a4aSRandall Stewart 		} else {
6545f8829a4aSRandall Stewart 			*psa = NULL;
6546f8829a4aSRandall Stewart 		}
6547f8829a4aSRandall Stewart 	}
6548f8829a4aSRandall Stewart 	return (error);
6549f8829a4aSRandall Stewart }
655017205eccSRandall Stewart 
655117205eccSRandall Stewart 
655217205eccSRandall Stewart 
655317205eccSRandall Stewart 
655417205eccSRandall Stewart 
655517205eccSRandall Stewart int
6556d61a0ae0SRandall Stewart sctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr,
6557d61a0ae0SRandall Stewart     int totaddr, int *error)
655817205eccSRandall Stewart {
655917205eccSRandall Stewart 	int added = 0;
656017205eccSRandall Stewart 	int i;
656117205eccSRandall Stewart 	struct sctp_inpcb *inp;
656217205eccSRandall Stewart 	struct sockaddr *sa;
656317205eccSRandall Stewart 	size_t incr = 0;
656492776dfdSMichael Tuexen #ifdef INET
656592776dfdSMichael Tuexen 	struct sockaddr_in *sin;
656692776dfdSMichael Tuexen #endif
656792776dfdSMichael Tuexen #ifdef INET6
656892776dfdSMichael Tuexen 	struct sockaddr_in6 *sin6;
656992776dfdSMichael Tuexen #endif
657092776dfdSMichael Tuexen 
657117205eccSRandall Stewart 	sa = addr;
657217205eccSRandall Stewart 	inp = stcb->sctp_ep;
657317205eccSRandall Stewart 	*error = 0;
657417205eccSRandall Stewart 	for (i = 0; i < totaddr; i++) {
6575ea5eba11SMichael Tuexen 		switch (sa->sa_family) {
6576ea5eba11SMichael Tuexen #ifdef INET
6577ea5eba11SMichael Tuexen 		case AF_INET:
657817205eccSRandall Stewart 			incr = sizeof(struct sockaddr_in);
657992776dfdSMichael Tuexen 			sin = (struct sockaddr_in *)sa;
658092776dfdSMichael Tuexen 			if ((sin->sin_addr.s_addr == INADDR_ANY) ||
658192776dfdSMichael Tuexen 			    (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
658292776dfdSMichael Tuexen 			    IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
658392776dfdSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6584ba785902SMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6585ba785902SMichael Tuexen 				    SCTP_FROM_SCTPUTIL + SCTP_LOC_7);
658692776dfdSMichael Tuexen 				*error = EINVAL;
658792776dfdSMichael Tuexen 				goto out_now;
658892776dfdSMichael Tuexen 			}
65897154bf4aSMichael Tuexen 			if (sctp_add_remote_addr(stcb, sa, NULL, stcb->asoc.port,
65907154bf4aSMichael Tuexen 			    SCTP_DONOT_SETSCOPE,
65917154bf4aSMichael Tuexen 			    SCTP_ADDR_IS_CONFIRMED)) {
659217205eccSRandall Stewart 				/* assoc gone no un-lock */
6593c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6594ba785902SMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6595ba785902SMichael Tuexen 				    SCTP_FROM_SCTPUTIL + SCTP_LOC_8);
659617205eccSRandall Stewart 				*error = ENOBUFS;
659717205eccSRandall Stewart 				goto out_now;
659817205eccSRandall Stewart 			}
659917205eccSRandall Stewart 			added++;
6600ea5eba11SMichael Tuexen 			break;
6601ea5eba11SMichael Tuexen #endif
6602ea5eba11SMichael Tuexen #ifdef INET6
6603ea5eba11SMichael Tuexen 		case AF_INET6:
660417205eccSRandall Stewart 			incr = sizeof(struct sockaddr_in6);
660592776dfdSMichael Tuexen 			sin6 = (struct sockaddr_in6 *)sa;
660692776dfdSMichael Tuexen 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
660792776dfdSMichael Tuexen 			    IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
660892776dfdSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6609ba785902SMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6610ba785902SMichael Tuexen 				    SCTP_FROM_SCTPUTIL + SCTP_LOC_9);
661192776dfdSMichael Tuexen 				*error = EINVAL;
661292776dfdSMichael Tuexen 				goto out_now;
661392776dfdSMichael Tuexen 			}
66147154bf4aSMichael Tuexen 			if (sctp_add_remote_addr(stcb, sa, NULL, stcb->asoc.port,
66157154bf4aSMichael Tuexen 			    SCTP_DONOT_SETSCOPE,
66167154bf4aSMichael Tuexen 			    SCTP_ADDR_IS_CONFIRMED)) {
661717205eccSRandall Stewart 				/* assoc gone no un-lock */
6618c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6619ba785902SMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6620ba785902SMichael Tuexen 				    SCTP_FROM_SCTPUTIL + SCTP_LOC_10);
662117205eccSRandall Stewart 				*error = ENOBUFS;
662217205eccSRandall Stewart 				goto out_now;
662317205eccSRandall Stewart 			}
662417205eccSRandall Stewart 			added++;
6625ea5eba11SMichael Tuexen 			break;
6626ea5eba11SMichael Tuexen #endif
6627ea5eba11SMichael Tuexen 		default:
6628ea5eba11SMichael Tuexen 			break;
662917205eccSRandall Stewart 		}
663017205eccSRandall Stewart 		sa = (struct sockaddr *)((caddr_t)sa + incr);
663117205eccSRandall Stewart 	}
663217205eccSRandall Stewart out_now:
663317205eccSRandall Stewart 	return (added);
663417205eccSRandall Stewart }
663517205eccSRandall Stewart 
6636fc26bf71SMichael Tuexen int
6637d61a0ae0SRandall Stewart sctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr,
6638fc26bf71SMichael Tuexen     unsigned int totaddr,
6639fc26bf71SMichael Tuexen     unsigned int *num_v4, unsigned int *num_v6,
6640fc26bf71SMichael Tuexen     unsigned int limit)
664117205eccSRandall Stewart {
664217205eccSRandall Stewart 	struct sockaddr *sa;
6643fc26bf71SMichael Tuexen 	struct sctp_tcb *stcb;
66449a8e3088SMichael Tuexen 	unsigned int incr, at, i;
664517205eccSRandall Stewart 
6646e1949767SMichael Tuexen 	at = 0;
664717205eccSRandall Stewart 	sa = addr;
6648fc26bf71SMichael Tuexen 	*num_v6 = *num_v4 = 0;
664917205eccSRandall Stewart 	/* account and validate addresses */
6650fc26bf71SMichael Tuexen 	if (totaddr == 0) {
6651fc26bf71SMichael Tuexen 		return (EINVAL);
6652fc26bf71SMichael Tuexen 	}
6653fc26bf71SMichael Tuexen 	for (i = 0; i < totaddr; i++) {
6654fc26bf71SMichael Tuexen 		if (at + sizeof(struct sockaddr) > limit) {
6655fc26bf71SMichael Tuexen 			return (EINVAL);
6656fc26bf71SMichael Tuexen 		}
6657ea5eba11SMichael Tuexen 		switch (sa->sa_family) {
6658ea5eba11SMichael Tuexen #ifdef INET
6659ea5eba11SMichael Tuexen 		case AF_INET:
6660e1949767SMichael Tuexen 			incr = (unsigned int)sizeof(struct sockaddr_in);
6661d61a0ae0SRandall Stewart 			if (sa->sa_len != incr) {
6662fc26bf71SMichael Tuexen 				return (EINVAL);
6663d61a0ae0SRandall Stewart 			}
66649a8e3088SMichael Tuexen 			(*num_v4) += 1;
6665ea5eba11SMichael Tuexen 			break;
6666ea5eba11SMichael Tuexen #endif
6667ea5eba11SMichael Tuexen #ifdef INET6
6668ea5eba11SMichael Tuexen 		case AF_INET6:
6669ea5eba11SMichael Tuexen 			{
667017205eccSRandall Stewart 				struct sockaddr_in6 *sin6;
667117205eccSRandall Stewart 
667217205eccSRandall Stewart 				sin6 = (struct sockaddr_in6 *)sa;
667317205eccSRandall Stewart 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
667417205eccSRandall Stewart 					/* Must be non-mapped for connectx */
6675fc26bf71SMichael Tuexen 					return (EINVAL);
667617205eccSRandall Stewart 				}
6677e1949767SMichael Tuexen 				incr = (unsigned int)sizeof(struct sockaddr_in6);
6678d61a0ae0SRandall Stewart 				if (sa->sa_len != incr) {
6679fc26bf71SMichael Tuexen 					return (EINVAL);
6680d61a0ae0SRandall Stewart 				}
66819a8e3088SMichael Tuexen 				(*num_v6) += 1;
6682ea5eba11SMichael Tuexen 				break;
6683ea5eba11SMichael Tuexen 			}
6684ea5eba11SMichael Tuexen #endif
6685ea5eba11SMichael Tuexen 		default:
6686fc26bf71SMichael Tuexen 			return (EINVAL);
668717205eccSRandall Stewart 		}
6688fc26bf71SMichael Tuexen 		if ((at + incr) > limit) {
6689fc26bf71SMichael Tuexen 			return (EINVAL);
6690ea5eba11SMichael Tuexen 		}
6691d61a0ae0SRandall Stewart 		SCTP_INP_INCR_REF(inp);
669217205eccSRandall Stewart 		stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
669317205eccSRandall Stewart 		if (stcb != NULL) {
6694fc26bf71SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
6695fc26bf71SMichael Tuexen 			return (EALREADY);
6696d61a0ae0SRandall Stewart 		} else {
6697d61a0ae0SRandall Stewart 			SCTP_INP_DECR_REF(inp);
669817205eccSRandall Stewart 		}
6699fc26bf71SMichael Tuexen 		at += incr;
670017205eccSRandall Stewart 		sa = (struct sockaddr *)((caddr_t)sa + incr);
670117205eccSRandall Stewart 	}
6702fc26bf71SMichael Tuexen 	return (0);
670317205eccSRandall Stewart }
670435918f85SRandall Stewart 
670535918f85SRandall Stewart /*
670635918f85SRandall Stewart  * sctp_bindx(ADD) for one address.
670735918f85SRandall Stewart  * assumes all arguments are valid/checked by caller.
670835918f85SRandall Stewart  */
670935918f85SRandall Stewart void
671035918f85SRandall Stewart sctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp,
671135918f85SRandall Stewart     struct sockaddr *sa, sctp_assoc_t assoc_id,
671235918f85SRandall Stewart     uint32_t vrf_id, int *error, void *p)
671335918f85SRandall Stewart {
671435918f85SRandall Stewart 	struct sockaddr *addr_touse;
6715d59107f7SMichael Tuexen #if defined(INET) && defined(INET6)
671635918f85SRandall Stewart 	struct sockaddr_in sin;
67175e2c2d87SRandall Stewart #endif
67185e2c2d87SRandall Stewart 
671935918f85SRandall Stewart 	/* see if we're bound all already! */
672035918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6721c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
672235918f85SRandall Stewart 		*error = EINVAL;
672335918f85SRandall Stewart 		return;
672435918f85SRandall Stewart 	}
672535918f85SRandall Stewart 	addr_touse = sa;
6726ea5eba11SMichael Tuexen #ifdef INET6
672735918f85SRandall Stewart 	if (sa->sa_family == AF_INET6) {
6728d59107f7SMichael Tuexen #ifdef INET
672935918f85SRandall Stewart 		struct sockaddr_in6 *sin6;
673035918f85SRandall Stewart 
6731d59107f7SMichael Tuexen #endif
673235918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6733c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
673435918f85SRandall Stewart 			*error = EINVAL;
673535918f85SRandall Stewart 			return;
673635918f85SRandall Stewart 		}
6737db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6738db4fd95bSRandall Stewart 			/* can only bind v6 on PF_INET6 sockets */
6739c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6740db4fd95bSRandall Stewart 			*error = EINVAL;
6741db4fd95bSRandall Stewart 			return;
6742db4fd95bSRandall Stewart 		}
6743d59107f7SMichael Tuexen #ifdef INET
674435918f85SRandall Stewart 		sin6 = (struct sockaddr_in6 *)addr_touse;
674535918f85SRandall Stewart 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6746db4fd95bSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6747db4fd95bSRandall Stewart 			    SCTP_IPV6_V6ONLY(inp)) {
6748db4fd95bSRandall Stewart 				/* can't bind v4-mapped on PF_INET sockets */
6749c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6750db4fd95bSRandall Stewart 				*error = EINVAL;
6751db4fd95bSRandall Stewart 				return;
6752db4fd95bSRandall Stewart 			}
675335918f85SRandall Stewart 			in6_sin6_2_sin(&sin, sin6);
675435918f85SRandall Stewart 			addr_touse = (struct sockaddr *)&sin;
675535918f85SRandall Stewart 		}
6756d59107f7SMichael Tuexen #endif
675735918f85SRandall Stewart 	}
675835918f85SRandall Stewart #endif
6759ea5eba11SMichael Tuexen #ifdef INET
676035918f85SRandall Stewart 	if (sa->sa_family == AF_INET) {
676135918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in)) {
6762c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
676335918f85SRandall Stewart 			*error = EINVAL;
676435918f85SRandall Stewart 			return;
676535918f85SRandall Stewart 		}
6766db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6767db4fd95bSRandall Stewart 		    SCTP_IPV6_V6ONLY(inp)) {
6768db4fd95bSRandall Stewart 			/* can't bind v4 on PF_INET sockets */
6769c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6770db4fd95bSRandall Stewart 			*error = EINVAL;
6771db4fd95bSRandall Stewart 			return;
6772db4fd95bSRandall Stewart 		}
677335918f85SRandall Stewart 	}
6774ea5eba11SMichael Tuexen #endif
677535918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
677635918f85SRandall Stewart 		if (p == NULL) {
677735918f85SRandall Stewart 			/* Can't get proc for Net/Open BSD */
6778c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
677935918f85SRandall Stewart 			*error = EINVAL;
678035918f85SRandall Stewart 			return;
678135918f85SRandall Stewart 		}
67821b649582SRandall Stewart 		*error = sctp_inpcb_bind(so, addr_touse, NULL, p);
678335918f85SRandall Stewart 		return;
678435918f85SRandall Stewart 	}
678535918f85SRandall Stewart 	/*
678635918f85SRandall Stewart 	 * No locks required here since bind and mgmt_ep_sa all do their own
678735918f85SRandall Stewart 	 * locking. If we do something for the FIX: below we may need to
678835918f85SRandall Stewart 	 * lock in that case.
678935918f85SRandall Stewart 	 */
679035918f85SRandall Stewart 	if (assoc_id == 0) {
679135918f85SRandall Stewart 		/* add the address */
679235918f85SRandall Stewart 		struct sctp_inpcb *lep;
679397c76f10SRandall Stewart 		struct sockaddr_in *lsin = (struct sockaddr_in *)addr_touse;
679435918f85SRandall Stewart 
679597c76f10SRandall Stewart 		/* validate the incoming port */
679697c76f10SRandall Stewart 		if ((lsin->sin_port != 0) &&
679797c76f10SRandall Stewart 		    (lsin->sin_port != inp->sctp_lport)) {
6798c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
679997c76f10SRandall Stewart 			*error = EINVAL;
680097c76f10SRandall Stewart 			return;
680197c76f10SRandall Stewart 		} else {
680297c76f10SRandall Stewart 			/* user specified 0 port, set it to existing port */
680397c76f10SRandall Stewart 			lsin->sin_port = inp->sctp_lport;
680497c76f10SRandall Stewart 		}
680597c76f10SRandall Stewart 
680635918f85SRandall Stewart 		lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id);
680735918f85SRandall Stewart 		if (lep != NULL) {
680835918f85SRandall Stewart 			/*
680935918f85SRandall Stewart 			 * We must decrement the refcount since we have the
681035918f85SRandall Stewart 			 * ep already and are binding. No remove going on
681135918f85SRandall Stewart 			 * here.
681235918f85SRandall Stewart 			 */
68136d9e8f2bSRandall Stewart 			SCTP_INP_DECR_REF(lep);
681435918f85SRandall Stewart 		}
681535918f85SRandall Stewart 		if (lep == inp) {
681635918f85SRandall Stewart 			/* already bound to it.. ok */
681735918f85SRandall Stewart 			return;
681835918f85SRandall Stewart 		} else if (lep == NULL) {
681935918f85SRandall Stewart 			((struct sockaddr_in *)addr_touse)->sin_port = 0;
682035918f85SRandall Stewart 			*error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
682135918f85SRandall Stewart 			    SCTP_ADD_IP_ADDRESS,
682280fefe0aSRandall Stewart 			    vrf_id, NULL);
682335918f85SRandall Stewart 		} else {
682435918f85SRandall Stewart 			*error = EADDRINUSE;
682535918f85SRandall Stewart 		}
682635918f85SRandall Stewart 		if (*error)
682735918f85SRandall Stewart 			return;
682835918f85SRandall Stewart 	} else {
682935918f85SRandall Stewart 		/*
683035918f85SRandall Stewart 		 * FIX: decide whether we allow assoc based bindx
683135918f85SRandall Stewart 		 */
683235918f85SRandall Stewart 	}
683335918f85SRandall Stewart }
683435918f85SRandall Stewart 
683535918f85SRandall Stewart /*
683635918f85SRandall Stewart  * sctp_bindx(DELETE) for one address.
683735918f85SRandall Stewart  * assumes all arguments are valid/checked by caller.
683835918f85SRandall Stewart  */
683935918f85SRandall Stewart void
68407215cc1bSMichael Tuexen sctp_bindx_delete_address(struct sctp_inpcb *inp,
684135918f85SRandall Stewart     struct sockaddr *sa, sctp_assoc_t assoc_id,
684235918f85SRandall Stewart     uint32_t vrf_id, int *error)
684335918f85SRandall Stewart {
684435918f85SRandall Stewart 	struct sockaddr *addr_touse;
6845d59107f7SMichael Tuexen #if defined(INET) && defined(INET6)
684635918f85SRandall Stewart 	struct sockaddr_in sin;
68475e2c2d87SRandall Stewart #endif
68485e2c2d87SRandall Stewart 
684935918f85SRandall Stewart 	/* see if we're bound all already! */
685035918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6851c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
685235918f85SRandall Stewart 		*error = EINVAL;
685335918f85SRandall Stewart 		return;
685435918f85SRandall Stewart 	}
685535918f85SRandall Stewart 	addr_touse = sa;
6856e0e00a4dSMichael Tuexen #ifdef INET6
685735918f85SRandall Stewart 	if (sa->sa_family == AF_INET6) {
6858d59107f7SMichael Tuexen #ifdef INET
685935918f85SRandall Stewart 		struct sockaddr_in6 *sin6;
6860d59107f7SMichael Tuexen #endif
6861d59107f7SMichael Tuexen 
686235918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6863c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
686435918f85SRandall Stewart 			*error = EINVAL;
686535918f85SRandall Stewart 			return;
686635918f85SRandall Stewart 		}
6867db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6868db4fd95bSRandall Stewart 			/* can only bind v6 on PF_INET6 sockets */
6869c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6870db4fd95bSRandall Stewart 			*error = EINVAL;
6871db4fd95bSRandall Stewart 			return;
6872db4fd95bSRandall Stewart 		}
6873d59107f7SMichael Tuexen #ifdef INET
687435918f85SRandall Stewart 		sin6 = (struct sockaddr_in6 *)addr_touse;
687535918f85SRandall Stewart 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6876db4fd95bSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6877db4fd95bSRandall Stewart 			    SCTP_IPV6_V6ONLY(inp)) {
6878db4fd95bSRandall Stewart 				/* can't bind mapped-v4 on PF_INET sockets */
6879c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6880db4fd95bSRandall Stewart 				*error = EINVAL;
6881db4fd95bSRandall Stewart 				return;
6882db4fd95bSRandall Stewart 			}
688335918f85SRandall Stewart 			in6_sin6_2_sin(&sin, sin6);
688435918f85SRandall Stewart 			addr_touse = (struct sockaddr *)&sin;
688535918f85SRandall Stewart 		}
6886d59107f7SMichael Tuexen #endif
688735918f85SRandall Stewart 	}
688835918f85SRandall Stewart #endif
6889ea5eba11SMichael Tuexen #ifdef INET
689035918f85SRandall Stewart 	if (sa->sa_family == AF_INET) {
689135918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in)) {
6892c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
689335918f85SRandall Stewart 			*error = EINVAL;
689435918f85SRandall Stewart 			return;
689535918f85SRandall Stewart 		}
6896db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6897db4fd95bSRandall Stewart 		    SCTP_IPV6_V6ONLY(inp)) {
6898db4fd95bSRandall Stewart 			/* can't bind v4 on PF_INET sockets */
6899c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6900db4fd95bSRandall Stewart 			*error = EINVAL;
6901db4fd95bSRandall Stewart 			return;
6902db4fd95bSRandall Stewart 		}
690335918f85SRandall Stewart 	}
6904ea5eba11SMichael Tuexen #endif
690535918f85SRandall Stewart 	/*
690635918f85SRandall Stewart 	 * No lock required mgmt_ep_sa does its own locking. If the FIX:
690735918f85SRandall Stewart 	 * below is ever changed we may need to lock before calling
690835918f85SRandall Stewart 	 * association level binding.
690935918f85SRandall Stewart 	 */
691035918f85SRandall Stewart 	if (assoc_id == 0) {
691135918f85SRandall Stewart 		/* delete the address */
691235918f85SRandall Stewart 		*error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
691335918f85SRandall Stewart 		    SCTP_DEL_IP_ADDRESS,
691480fefe0aSRandall Stewart 		    vrf_id, NULL);
691535918f85SRandall Stewart 	} else {
691635918f85SRandall Stewart 		/*
691735918f85SRandall Stewart 		 * FIX: decide whether we allow assoc based bindx
691835918f85SRandall Stewart 		 */
691935918f85SRandall Stewart 	}
692035918f85SRandall Stewart }
69211b649582SRandall Stewart 
69221b649582SRandall Stewart /*
69231b649582SRandall Stewart  * returns the valid local address count for an assoc, taking into account
69241b649582SRandall Stewart  * all scoping rules
69251b649582SRandall Stewart  */
69261b649582SRandall Stewart int
69271b649582SRandall Stewart sctp_local_addr_count(struct sctp_tcb *stcb)
69281b649582SRandall Stewart {
6929b54ddf22SMichael Tuexen 	int loopback_scope;
6930b54ddf22SMichael Tuexen #if defined(INET)
6931b54ddf22SMichael Tuexen 	int ipv4_local_scope, ipv4_addr_legal;
6932b54ddf22SMichael Tuexen #endif
6933b54ddf22SMichael Tuexen #if defined (INET6)
6934b54ddf22SMichael Tuexen 	int local_scope, site_scope, ipv6_addr_legal;
6935b54ddf22SMichael Tuexen #endif
69361b649582SRandall Stewart 	struct sctp_vrf *vrf;
69371b649582SRandall Stewart 	struct sctp_ifn *sctp_ifn;
69381b649582SRandall Stewart 	struct sctp_ifa *sctp_ifa;
69391b649582SRandall Stewart 	int count = 0;
69401b649582SRandall Stewart 
69411b649582SRandall Stewart 	/* Turn on all the appropriate scopes */
6942a1cb341bSMichael Tuexen 	loopback_scope = stcb->asoc.scope.loopback_scope;
6943b54ddf22SMichael Tuexen #if defined(INET)
6944a1cb341bSMichael Tuexen 	ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
6945b54ddf22SMichael Tuexen 	ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
6946b54ddf22SMichael Tuexen #endif
6947b54ddf22SMichael Tuexen #if defined(INET6)
6948a1cb341bSMichael Tuexen 	local_scope = stcb->asoc.scope.local_scope;
6949a1cb341bSMichael Tuexen 	site_scope = stcb->asoc.scope.site_scope;
6950a1cb341bSMichael Tuexen 	ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
6951b54ddf22SMichael Tuexen #endif
6952c99efcf6SRandall Stewart 	SCTP_IPI_ADDR_RLOCK();
69531b649582SRandall Stewart 	vrf = sctp_find_vrf(stcb->asoc.vrf_id);
69541b649582SRandall Stewart 	if (vrf == NULL) {
69551b649582SRandall Stewart 		/* no vrf, no addresses */
6956c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RUNLOCK();
69571b649582SRandall Stewart 		return (0);
69581b649582SRandall Stewart 	}
69590053ed28SMichael Tuexen 
69601b649582SRandall Stewart 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
69611b649582SRandall Stewart 		/*
69621b649582SRandall Stewart 		 * bound all case: go through all ifns on the vrf
69631b649582SRandall Stewart 		 */
69641b649582SRandall Stewart 		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
69651b649582SRandall Stewart 			if ((loopback_scope == 0) &&
69661b649582SRandall Stewart 			    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
69671b649582SRandall Stewart 				continue;
69681b649582SRandall Stewart 			}
69691b649582SRandall Stewart 			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
69701b649582SRandall Stewart 				if (sctp_is_addr_restricted(stcb, sctp_ifa))
69711b649582SRandall Stewart 					continue;
69725e2c2d87SRandall Stewart 				switch (sctp_ifa->address.sa.sa_family) {
6973ea5eba11SMichael Tuexen #ifdef INET
69745e2c2d87SRandall Stewart 				case AF_INET:
69755e2c2d87SRandall Stewart 					if (ipv4_addr_legal) {
69761b649582SRandall Stewart 						struct sockaddr_in *sin;
69771b649582SRandall Stewart 
697824aaac8dSMichael Tuexen 						sin = &sctp_ifa->address.sin;
69791b649582SRandall Stewart 						if (sin->sin_addr.s_addr == 0) {
6980b7b84c0eSMichael Tuexen 							/*
6981b7b84c0eSMichael Tuexen 							 * skip unspecified
6982b7b84c0eSMichael Tuexen 							 * addrs
6983b7b84c0eSMichael Tuexen 							 */
69841b649582SRandall Stewart 							continue;
69851b649582SRandall Stewart 						}
69866ba22f19SMichael Tuexen 						if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
69876ba22f19SMichael Tuexen 						    &sin->sin_addr) != 0) {
69886ba22f19SMichael Tuexen 							continue;
69896ba22f19SMichael Tuexen 						}
69901b649582SRandall Stewart 						if ((ipv4_local_scope == 0) &&
69911b649582SRandall Stewart 						    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
69921b649582SRandall Stewart 							continue;
69931b649582SRandall Stewart 						}
69941b649582SRandall Stewart 						/* count this one */
69951b649582SRandall Stewart 						count++;
69965e2c2d87SRandall Stewart 					} else {
69975e2c2d87SRandall Stewart 						continue;
69985e2c2d87SRandall Stewart 					}
69995e2c2d87SRandall Stewart 					break;
7000ea5eba11SMichael Tuexen #endif
70015e2c2d87SRandall Stewart #ifdef INET6
70025e2c2d87SRandall Stewart 				case AF_INET6:
70035e2c2d87SRandall Stewart 					if (ipv6_addr_legal) {
70041b649582SRandall Stewart 						struct sockaddr_in6 *sin6;
70051b649582SRandall Stewart 
700624aaac8dSMichael Tuexen 						sin6 = &sctp_ifa->address.sin6;
70071b649582SRandall Stewart 						if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
70081b649582SRandall Stewart 							continue;
70091b649582SRandall Stewart 						}
70106ba22f19SMichael Tuexen 						if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
70116ba22f19SMichael Tuexen 						    &sin6->sin6_addr) != 0) {
70126ba22f19SMichael Tuexen 							continue;
70136ba22f19SMichael Tuexen 						}
70141b649582SRandall Stewart 						if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
70151b649582SRandall Stewart 							if (local_scope == 0)
70161b649582SRandall Stewart 								continue;
70171b649582SRandall Stewart 							if (sin6->sin6_scope_id == 0) {
70181b649582SRandall Stewart 								if (sa6_recoverscope(sin6) != 0)
70191b649582SRandall Stewart 									/*
70205e2c2d87SRandall Stewart 									 *
70215e2c2d87SRandall Stewart 									 * bad
70225b495f17SMichael Tuexen 									 * link
70235e2c2d87SRandall Stewart 									 *
70245b495f17SMichael Tuexen 									 * local
70255e2c2d87SRandall Stewart 									 *
70265b495f17SMichael Tuexen 									 * address
70275b495f17SMichael Tuexen 									 */
70281b649582SRandall Stewart 									continue;
70291b649582SRandall Stewart 							}
70301b649582SRandall Stewart 						}
70311b649582SRandall Stewart 						if ((site_scope == 0) &&
70321b649582SRandall Stewart 						    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
70331b649582SRandall Stewart 							continue;
70341b649582SRandall Stewart 						}
70351b649582SRandall Stewart 						/* count this one */
70361b649582SRandall Stewart 						count++;
70371b649582SRandall Stewart 					}
70385e2c2d87SRandall Stewart 					break;
70395e2c2d87SRandall Stewart #endif
70405e2c2d87SRandall Stewart 				default:
70415e2c2d87SRandall Stewart 					/* TSNH */
70425e2c2d87SRandall Stewart 					break;
70435e2c2d87SRandall Stewart 				}
70441b649582SRandall Stewart 			}
70451b649582SRandall Stewart 		}
70461b649582SRandall Stewart 	} else {
70471b649582SRandall Stewart 		/*
70481b649582SRandall Stewart 		 * subset bound case
70491b649582SRandall Stewart 		 */
70501b649582SRandall Stewart 		struct sctp_laddr *laddr;
70511b649582SRandall Stewart 
70521b649582SRandall Stewart 		LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list,
70531b649582SRandall Stewart 		    sctp_nxt_addr) {
70541b649582SRandall Stewart 			if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
70551b649582SRandall Stewart 				continue;
70561b649582SRandall Stewart 			}
70571b649582SRandall Stewart 			/* count this one */
70581b649582SRandall Stewart 			count++;
70591b649582SRandall Stewart 		}
70601b649582SRandall Stewart 	}
7061c99efcf6SRandall Stewart 	SCTP_IPI_ADDR_RUNLOCK();
70621b649582SRandall Stewart 	return (count);
70631b649582SRandall Stewart }
7064c4739e2fSRandall Stewart 
7065c4739e2fSRandall Stewart #if defined(SCTP_LOCAL_TRACE_BUF)
7066c4739e2fSRandall Stewart 
7067c4739e2fSRandall Stewart void
7068b27a6b7dSRandall 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)
7069c4739e2fSRandall Stewart {
7070b27a6b7dSRandall Stewart 	uint32_t saveindex, newindex;
7071c4739e2fSRandall Stewart 
7072c4739e2fSRandall Stewart 	do {
7073b3f1ea41SRandall Stewart 		saveindex = SCTP_BASE_SYSCTL(sctp_log).index;
7074c4739e2fSRandall Stewart 		if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
7075c4739e2fSRandall Stewart 			newindex = 1;
7076c4739e2fSRandall Stewart 		} else {
7077c4739e2fSRandall Stewart 			newindex = saveindex + 1;
7078c4739e2fSRandall Stewart 		}
7079b3f1ea41SRandall Stewart 	} while (atomic_cmpset_int(&SCTP_BASE_SYSCTL(sctp_log).index, saveindex, newindex) == 0);
7080c4739e2fSRandall Stewart 	if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
7081c4739e2fSRandall Stewart 		saveindex = 0;
7082c4739e2fSRandall Stewart 	}
7083b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].timestamp = SCTP_GET_CYCLECOUNT;
7084b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].subsys = subsys;
7085b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[0] = a;
7086b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[1] = b;
7087b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[2] = c;
7088b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[3] = d;
7089b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[4] = e;
7090b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[5] = f;
7091c4739e2fSRandall Stewart }
7092c4739e2fSRandall Stewart 
7093c4739e2fSRandall Stewart #endif
7094a99b6783SRandall Stewart static void
70957cca1775SRandall Stewart sctp_recv_udp_tunneled_packet(struct mbuf *m, int off, struct inpcb *inp,
709681d3ec17SBryan Venteicher     const struct sockaddr *sa SCTP_UNUSED, void *ctx SCTP_UNUSED)
7097a99b6783SRandall Stewart {
7098a99b6783SRandall Stewart 	struct ip *iph;
70993a51a264SMichael Tuexen #ifdef INET6
71003a51a264SMichael Tuexen 	struct ip6_hdr *ip6;
71013a51a264SMichael Tuexen #endif
7102a99b6783SRandall Stewart 	struct mbuf *sp, *last;
7103a99b6783SRandall Stewart 	struct udphdr *uhdr;
7104285052f0SMichael Tuexen 	uint16_t port;
7105a99b6783SRandall Stewart 
7106a99b6783SRandall Stewart 	if ((m->m_flags & M_PKTHDR) == 0) {
7107a99b6783SRandall Stewart 		/* Can't handle one that is not a pkt hdr */
7108a99b6783SRandall Stewart 		goto out;
7109a99b6783SRandall Stewart 	}
7110285052f0SMichael Tuexen 	/* Pull the src port */
7111a99b6783SRandall Stewart 	iph = mtod(m, struct ip *);
7112a99b6783SRandall Stewart 	uhdr = (struct udphdr *)((caddr_t)iph + off);
7113a99b6783SRandall Stewart 	port = uhdr->uh_sport;
7114285052f0SMichael Tuexen 	/*
7115285052f0SMichael Tuexen 	 * Split out the mbuf chain. Leave the IP header in m, place the
7116285052f0SMichael Tuexen 	 * rest in the sp.
7117285052f0SMichael Tuexen 	 */
7118eb1b1807SGleb Smirnoff 	sp = m_split(m, off, M_NOWAIT);
7119a99b6783SRandall Stewart 	if (sp == NULL) {
7120a99b6783SRandall Stewart 		/* Gak, drop packet, we can't do a split */
7121a99b6783SRandall Stewart 		goto out;
7122a99b6783SRandall Stewart 	}
7123285052f0SMichael Tuexen 	if (sp->m_pkthdr.len < sizeof(struct udphdr) + sizeof(struct sctphdr)) {
7124285052f0SMichael Tuexen 		/* Gak, packet can't have an SCTP header in it - too small */
7125a99b6783SRandall Stewart 		m_freem(sp);
7126a99b6783SRandall Stewart 		goto out;
7127a99b6783SRandall Stewart 	}
7128285052f0SMichael Tuexen 	/* Now pull up the UDP header and SCTP header together */
7129285052f0SMichael Tuexen 	sp = m_pullup(sp, sizeof(struct udphdr) + sizeof(struct sctphdr));
7130a99b6783SRandall Stewart 	if (sp == NULL) {
7131a99b6783SRandall Stewart 		/* Gak pullup failed */
7132a99b6783SRandall Stewart 		goto out;
7133a99b6783SRandall Stewart 	}
7134285052f0SMichael Tuexen 	/* Trim out the UDP header */
7135a99b6783SRandall Stewart 	m_adj(sp, sizeof(struct udphdr));
7136a99b6783SRandall Stewart 
7137a99b6783SRandall Stewart 	/* Now reconstruct the mbuf chain */
7138285052f0SMichael Tuexen 	for (last = m; last->m_next; last = last->m_next);
7139a99b6783SRandall Stewart 	last->m_next = sp;
7140a99b6783SRandall Stewart 	m->m_pkthdr.len += sp->m_pkthdr.len;
714152f175beSMichael Tuexen 	/*
714252f175beSMichael Tuexen 	 * The CSUM_DATA_VALID flags indicates that the HW checked the UDP
714352f175beSMichael Tuexen 	 * checksum and it was valid. Since CSUM_DATA_VALID ==
714452f175beSMichael Tuexen 	 * CSUM_SCTP_VALID this would imply that the HW also verified the
714552f175beSMichael Tuexen 	 * SCTP checksum. Therefore, clear the bit.
714652f175beSMichael Tuexen 	 */
714752f175beSMichael Tuexen 	SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
714852f175beSMichael Tuexen 	    "sctp_recv_udp_tunneled_packet(): Packet of length %d received on %s with csum_flags 0x%b.\n",
714952f175beSMichael Tuexen 	    m->m_pkthdr.len,
715052f175beSMichael Tuexen 	    if_name(m->m_pkthdr.rcvif),
715152f175beSMichael Tuexen 	    (int)m->m_pkthdr.csum_flags, CSUM_BITS);
715252f175beSMichael Tuexen 	m->m_pkthdr.csum_flags &= ~CSUM_DATA_VALID;
7153a99b6783SRandall Stewart 	iph = mtod(m, struct ip *);
7154a99b6783SRandall Stewart 	switch (iph->ip_v) {
7155e6194c2eSMichael Tuexen #ifdef INET
7156a99b6783SRandall Stewart 	case IPVERSION:
715709c1c856SMichael Tuexen 		iph->ip_len = htons(ntohs(iph->ip_len) - sizeof(struct udphdr));
7158a99b6783SRandall Stewart 		sctp_input_with_port(m, off, port);
7159a99b6783SRandall Stewart 		break;
7160e6194c2eSMichael Tuexen #endif
7161a99b6783SRandall Stewart #ifdef INET6
7162a99b6783SRandall Stewart 	case IPV6_VERSION >> 4:
71633a51a264SMichael Tuexen 		ip6 = mtod(m, struct ip6_hdr *);
71643a51a264SMichael Tuexen 		ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - sizeof(struct udphdr));
71653a51a264SMichael Tuexen 		sctp6_input_with_port(&m, &off, port);
7166a99b6783SRandall Stewart 		break;
7167a99b6783SRandall Stewart #endif
7168a99b6783SRandall Stewart 	default:
7169285052f0SMichael Tuexen 		goto out;
7170a99b6783SRandall Stewart 		break;
7171a99b6783SRandall Stewart 	}
7172a99b6783SRandall Stewart 	return;
7173a99b6783SRandall Stewart out:
7174a99b6783SRandall Stewart 	m_freem(m);
7175a99b6783SRandall Stewart }
7176c54a18d2SRandall Stewart 
7177fd7af143SMichael Tuexen #ifdef INET
7178fd7af143SMichael Tuexen static void
7179fd7af143SMichael Tuexen sctp_recv_icmp_tunneled_packet(int cmd, struct sockaddr *sa, void *vip, void *ctx SCTP_UNUSED)
7180fd7af143SMichael Tuexen {
7181fd7af143SMichael Tuexen 	struct ip *outer_ip, *inner_ip;
7182fd7af143SMichael Tuexen 	struct sctphdr *sh;
7183fd7af143SMichael Tuexen 	struct icmp *icmp;
7184fd7af143SMichael Tuexen 	struct udphdr *udp;
7185fd7af143SMichael Tuexen 	struct sctp_inpcb *inp;
7186fd7af143SMichael Tuexen 	struct sctp_tcb *stcb;
7187fd7af143SMichael Tuexen 	struct sctp_nets *net;
7188fd7af143SMichael Tuexen 	struct sctp_init_chunk *ch;
7189fd7af143SMichael Tuexen 	struct sockaddr_in src, dst;
7190fd7af143SMichael Tuexen 	uint8_t type, code;
7191fd7af143SMichael Tuexen 
7192fd7af143SMichael Tuexen 	inner_ip = (struct ip *)vip;
7193fd7af143SMichael Tuexen 	icmp = (struct icmp *)((caddr_t)inner_ip -
7194fd7af143SMichael Tuexen 	    (sizeof(struct icmp) - sizeof(struct ip)));
7195fd7af143SMichael Tuexen 	outer_ip = (struct ip *)((caddr_t)icmp - sizeof(struct ip));
7196fd7af143SMichael Tuexen 	if (ntohs(outer_ip->ip_len) <
7197fd7af143SMichael Tuexen 	    sizeof(struct ip) + 8 + (inner_ip->ip_hl << 2) + sizeof(struct udphdr) + 8) {
7198fd7af143SMichael Tuexen 		return;
7199fd7af143SMichael Tuexen 	}
7200fd7af143SMichael Tuexen 	udp = (struct udphdr *)((caddr_t)inner_ip + (inner_ip->ip_hl << 2));
7201fd7af143SMichael Tuexen 	sh = (struct sctphdr *)(udp + 1);
7202fd7af143SMichael Tuexen 	memset(&src, 0, sizeof(struct sockaddr_in));
7203fd7af143SMichael Tuexen 	src.sin_family = AF_INET;
7204fd7af143SMichael Tuexen 	src.sin_len = sizeof(struct sockaddr_in);
7205fd7af143SMichael Tuexen 	src.sin_port = sh->src_port;
7206fd7af143SMichael Tuexen 	src.sin_addr = inner_ip->ip_src;
7207fd7af143SMichael Tuexen 	memset(&dst, 0, sizeof(struct sockaddr_in));
7208fd7af143SMichael Tuexen 	dst.sin_family = AF_INET;
7209fd7af143SMichael Tuexen 	dst.sin_len = sizeof(struct sockaddr_in);
7210fd7af143SMichael Tuexen 	dst.sin_port = sh->dest_port;
7211fd7af143SMichael Tuexen 	dst.sin_addr = inner_ip->ip_dst;
7212fd7af143SMichael Tuexen 	/*
7213fd7af143SMichael Tuexen 	 * 'dst' holds the dest of the packet that failed to be sent. 'src'
7214fd7af143SMichael Tuexen 	 * holds our local endpoint address. Thus we reverse the dst and the
7215fd7af143SMichael Tuexen 	 * src in the lookup.
7216fd7af143SMichael Tuexen 	 */
7217fd7af143SMichael Tuexen 	inp = NULL;
7218fd7af143SMichael Tuexen 	net = NULL;
7219fd7af143SMichael Tuexen 	stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
7220fd7af143SMichael Tuexen 	    (struct sockaddr *)&src,
7221fd7af143SMichael Tuexen 	    &inp, &net, 1,
7222fd7af143SMichael Tuexen 	    SCTP_DEFAULT_VRFID);
7223fd7af143SMichael Tuexen 	if ((stcb != NULL) &&
7224fd7af143SMichael Tuexen 	    (net != NULL) &&
722555b8cd93SMichael Tuexen 	    (inp != NULL)) {
7226fd7af143SMichael Tuexen 		/* Check the UDP port numbers */
7227fd7af143SMichael Tuexen 		if ((udp->uh_dport != net->port) ||
7228fd7af143SMichael Tuexen 		    (udp->uh_sport != htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)))) {
7229fd7af143SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
7230fd7af143SMichael Tuexen 			return;
7231fd7af143SMichael Tuexen 		}
7232fd7af143SMichael Tuexen 		/* Check the verification tag */
7233fd7af143SMichael Tuexen 		if (ntohl(sh->v_tag) != 0) {
7234fd7af143SMichael Tuexen 			/*
7235fd7af143SMichael Tuexen 			 * This must be the verification tag used for
7236fd7af143SMichael Tuexen 			 * sending out packets. We don't consider packets
7237fd7af143SMichael Tuexen 			 * reflecting the verification tag.
7238fd7af143SMichael Tuexen 			 */
7239fd7af143SMichael Tuexen 			if (ntohl(sh->v_tag) != stcb->asoc.peer_vtag) {
7240fd7af143SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
7241fd7af143SMichael Tuexen 				return;
7242fd7af143SMichael Tuexen 			}
7243fd7af143SMichael Tuexen 		} else {
7244fd7af143SMichael Tuexen 			if (ntohs(outer_ip->ip_len) >=
7245fd7af143SMichael Tuexen 			    sizeof(struct ip) +
7246fd7af143SMichael Tuexen 			    8 + (inner_ip->ip_hl << 2) + 8 + 20) {
7247fd7af143SMichael Tuexen 				/*
7248fd7af143SMichael Tuexen 				 * In this case we can check if we got an
7249fd7af143SMichael Tuexen 				 * INIT chunk and if the initiate tag
7250fd7af143SMichael Tuexen 				 * matches.
7251fd7af143SMichael Tuexen 				 */
7252fd7af143SMichael Tuexen 				ch = (struct sctp_init_chunk *)(sh + 1);
7253fd7af143SMichael Tuexen 				if ((ch->ch.chunk_type != SCTP_INITIATION) ||
7254fd7af143SMichael Tuexen 				    (ntohl(ch->init.initiate_tag) != stcb->asoc.my_vtag)) {
7255fd7af143SMichael Tuexen 					SCTP_TCB_UNLOCK(stcb);
7256fd7af143SMichael Tuexen 					return;
7257fd7af143SMichael Tuexen 				}
7258fd7af143SMichael Tuexen 			} else {
7259fd7af143SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
7260fd7af143SMichael Tuexen 				return;
7261fd7af143SMichael Tuexen 			}
7262fd7af143SMichael Tuexen 		}
7263fd7af143SMichael Tuexen 		type = icmp->icmp_type;
7264fd7af143SMichael Tuexen 		code = icmp->icmp_code;
72653c3f9e2aSMichael Tuexen 		if ((type == ICMP_UNREACH) &&
72663c3f9e2aSMichael Tuexen 		    (code == ICMP_UNREACH_PORT)) {
7267fd7af143SMichael Tuexen 			code = ICMP_UNREACH_PROTOCOL;
7268fd7af143SMichael Tuexen 		}
7269fd7af143SMichael Tuexen 		sctp_notify(inp, stcb, net, type, code,
7270fd7af143SMichael Tuexen 		    ntohs(inner_ip->ip_len),
72716ebfa5eeSMichael Tuexen 		    (uint32_t)ntohs(icmp->icmp_nextmtu));
7272fd7af143SMichael Tuexen 	} else {
7273fd7af143SMichael Tuexen 		if ((stcb == NULL) && (inp != NULL)) {
7274fd7af143SMichael Tuexen 			/* reduce ref-count */
7275fd7af143SMichael Tuexen 			SCTP_INP_WLOCK(inp);
7276fd7af143SMichael Tuexen 			SCTP_INP_DECR_REF(inp);
7277fd7af143SMichael Tuexen 			SCTP_INP_WUNLOCK(inp);
7278fd7af143SMichael Tuexen 		}
7279fd7af143SMichael Tuexen 		if (stcb) {
7280fd7af143SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
7281fd7af143SMichael Tuexen 		}
7282fd7af143SMichael Tuexen 	}
7283fd7af143SMichael Tuexen 	return;
7284fd7af143SMichael Tuexen }
7285fd7af143SMichael Tuexen #endif
7286fd7af143SMichael Tuexen 
7287fd7af143SMichael Tuexen #ifdef INET6
7288fd7af143SMichael Tuexen static void
7289fd7af143SMichael Tuexen sctp_recv_icmp6_tunneled_packet(int cmd, struct sockaddr *sa, void *d, void *ctx SCTP_UNUSED)
7290fd7af143SMichael Tuexen {
7291fd7af143SMichael Tuexen 	struct ip6ctlparam *ip6cp;
7292fd7af143SMichael Tuexen 	struct sctp_inpcb *inp;
7293fd7af143SMichael Tuexen 	struct sctp_tcb *stcb;
7294fd7af143SMichael Tuexen 	struct sctp_nets *net;
7295fd7af143SMichael Tuexen 	struct sctphdr sh;
7296fd7af143SMichael Tuexen 	struct udphdr udp;
7297fd7af143SMichael Tuexen 	struct sockaddr_in6 src, dst;
7298fd7af143SMichael Tuexen 	uint8_t type, code;
7299fd7af143SMichael Tuexen 
7300fd7af143SMichael Tuexen 	ip6cp = (struct ip6ctlparam *)d;
7301fd7af143SMichael Tuexen 	/*
7302fd7af143SMichael Tuexen 	 * XXX: We assume that when IPV6 is non NULL, M and OFF are valid.
7303fd7af143SMichael Tuexen 	 */
7304fd7af143SMichael Tuexen 	if (ip6cp->ip6c_m == NULL) {
7305fd7af143SMichael Tuexen 		return;
7306fd7af143SMichael Tuexen 	}
7307fd7af143SMichael Tuexen 	/*
7308fd7af143SMichael Tuexen 	 * Check if we can safely examine the ports and the verification tag
7309fd7af143SMichael Tuexen 	 * of the SCTP common header.
7310fd7af143SMichael Tuexen 	 */
7311fd7af143SMichael Tuexen 	if (ip6cp->ip6c_m->m_pkthdr.len <
7312fd7af143SMichael Tuexen 	    ip6cp->ip6c_off + sizeof(struct udphdr) + offsetof(struct sctphdr, checksum)) {
7313fd7af143SMichael Tuexen 		return;
7314fd7af143SMichael Tuexen 	}
7315fd7af143SMichael Tuexen 	/* Copy out the UDP header. */
7316fd7af143SMichael Tuexen 	memset(&udp, 0, sizeof(struct udphdr));
7317fd7af143SMichael Tuexen 	m_copydata(ip6cp->ip6c_m,
7318fd7af143SMichael Tuexen 	    ip6cp->ip6c_off,
7319fd7af143SMichael Tuexen 	    sizeof(struct udphdr),
7320fd7af143SMichael Tuexen 	    (caddr_t)&udp);
7321fd7af143SMichael Tuexen 	/* Copy out the port numbers and the verification tag. */
7322fd7af143SMichael Tuexen 	memset(&sh, 0, sizeof(struct sctphdr));
7323fd7af143SMichael Tuexen 	m_copydata(ip6cp->ip6c_m,
7324fd7af143SMichael Tuexen 	    ip6cp->ip6c_off + sizeof(struct udphdr),
7325fd7af143SMichael Tuexen 	    sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
7326fd7af143SMichael Tuexen 	    (caddr_t)&sh);
7327fd7af143SMichael Tuexen 	memset(&src, 0, sizeof(struct sockaddr_in6));
7328fd7af143SMichael Tuexen 	src.sin6_family = AF_INET6;
7329fd7af143SMichael Tuexen 	src.sin6_len = sizeof(struct sockaddr_in6);
7330fd7af143SMichael Tuexen 	src.sin6_port = sh.src_port;
7331fd7af143SMichael Tuexen 	src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
7332fd7af143SMichael Tuexen 	if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
7333fd7af143SMichael Tuexen 		return;
7334fd7af143SMichael Tuexen 	}
7335fd7af143SMichael Tuexen 	memset(&dst, 0, sizeof(struct sockaddr_in6));
7336fd7af143SMichael Tuexen 	dst.sin6_family = AF_INET6;
7337fd7af143SMichael Tuexen 	dst.sin6_len = sizeof(struct sockaddr_in6);
7338fd7af143SMichael Tuexen 	dst.sin6_port = sh.dest_port;
7339fd7af143SMichael Tuexen 	dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
7340fd7af143SMichael Tuexen 	if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
7341fd7af143SMichael Tuexen 		return;
7342fd7af143SMichael Tuexen 	}
7343fd7af143SMichael Tuexen 	inp = NULL;
7344fd7af143SMichael Tuexen 	net = NULL;
7345fd7af143SMichael Tuexen 	stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
7346fd7af143SMichael Tuexen 	    (struct sockaddr *)&src,
7347fd7af143SMichael Tuexen 	    &inp, &net, 1, SCTP_DEFAULT_VRFID);
7348fd7af143SMichael Tuexen 	if ((stcb != NULL) &&
7349fd7af143SMichael Tuexen 	    (net != NULL) &&
735055b8cd93SMichael Tuexen 	    (inp != NULL)) {
7351fd7af143SMichael Tuexen 		/* Check the UDP port numbers */
7352fd7af143SMichael Tuexen 		if ((udp.uh_dport != net->port) ||
7353fd7af143SMichael Tuexen 		    (udp.uh_sport != htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)))) {
7354fd7af143SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
7355fd7af143SMichael Tuexen 			return;
7356fd7af143SMichael Tuexen 		}
7357fd7af143SMichael Tuexen 		/* Check the verification tag */
7358fd7af143SMichael Tuexen 		if (ntohl(sh.v_tag) != 0) {
7359fd7af143SMichael Tuexen 			/*
7360fd7af143SMichael Tuexen 			 * This must be the verification tag used for
7361fd7af143SMichael Tuexen 			 * sending out packets. We don't consider packets
7362fd7af143SMichael Tuexen 			 * reflecting the verification tag.
7363fd7af143SMichael Tuexen 			 */
7364fd7af143SMichael Tuexen 			if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
7365fd7af143SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
7366fd7af143SMichael Tuexen 				return;
7367fd7af143SMichael Tuexen 			}
7368fd7af143SMichael Tuexen 		} else {
7369fd7af143SMichael Tuexen 			if (ip6cp->ip6c_m->m_pkthdr.len >=
7370fd7af143SMichael Tuexen 			    ip6cp->ip6c_off + sizeof(struct udphdr) +
7371fd7af143SMichael Tuexen 			    sizeof(struct sctphdr) +
7372fd7af143SMichael Tuexen 			    sizeof(struct sctp_chunkhdr) +
7373fd7af143SMichael Tuexen 			    offsetof(struct sctp_init, a_rwnd)) {
7374fd7af143SMichael Tuexen 				/*
7375fd7af143SMichael Tuexen 				 * In this case we can check if we got an
7376fd7af143SMichael Tuexen 				 * INIT chunk and if the initiate tag
7377fd7af143SMichael Tuexen 				 * matches.
7378fd7af143SMichael Tuexen 				 */
7379fd7af143SMichael Tuexen 				uint32_t initiate_tag;
7380fd7af143SMichael Tuexen 				uint8_t chunk_type;
7381fd7af143SMichael Tuexen 
7382fd7af143SMichael Tuexen 				m_copydata(ip6cp->ip6c_m,
7383fd7af143SMichael Tuexen 				    ip6cp->ip6c_off +
7384fd7af143SMichael Tuexen 				    sizeof(struct udphdr) +
7385fd7af143SMichael Tuexen 				    sizeof(struct sctphdr),
7386fd7af143SMichael Tuexen 				    sizeof(uint8_t),
7387fd7af143SMichael Tuexen 				    (caddr_t)&chunk_type);
7388fd7af143SMichael Tuexen 				m_copydata(ip6cp->ip6c_m,
7389fd7af143SMichael Tuexen 				    ip6cp->ip6c_off +
7390fd7af143SMichael Tuexen 				    sizeof(struct udphdr) +
7391fd7af143SMichael Tuexen 				    sizeof(struct sctphdr) +
7392fd7af143SMichael Tuexen 				    sizeof(struct sctp_chunkhdr),
7393fd7af143SMichael Tuexen 				    sizeof(uint32_t),
7394fd7af143SMichael Tuexen 				    (caddr_t)&initiate_tag);
7395fd7af143SMichael Tuexen 				if ((chunk_type != SCTP_INITIATION) ||
7396fd7af143SMichael Tuexen 				    (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
7397fd7af143SMichael Tuexen 					SCTP_TCB_UNLOCK(stcb);
7398fd7af143SMichael Tuexen 					return;
7399fd7af143SMichael Tuexen 				}
7400fd7af143SMichael Tuexen 			} else {
7401fd7af143SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
7402fd7af143SMichael Tuexen 				return;
7403fd7af143SMichael Tuexen 			}
7404fd7af143SMichael Tuexen 		}
7405fd7af143SMichael Tuexen 		type = ip6cp->ip6c_icmp6->icmp6_type;
7406fd7af143SMichael Tuexen 		code = ip6cp->ip6c_icmp6->icmp6_code;
7407fd7af143SMichael Tuexen 		if ((type == ICMP6_DST_UNREACH) &&
7408fd7af143SMichael Tuexen 		    (code == ICMP6_DST_UNREACH_NOPORT)) {
7409fd7af143SMichael Tuexen 			type = ICMP6_PARAM_PROB;
7410fd7af143SMichael Tuexen 			code = ICMP6_PARAMPROB_NEXTHEADER;
7411fd7af143SMichael Tuexen 		}
7412fd7af143SMichael Tuexen 		sctp6_notify(inp, stcb, net, type, code,
74136ebfa5eeSMichael Tuexen 		    ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
7414fd7af143SMichael Tuexen 	} else {
7415fd7af143SMichael Tuexen 		if ((stcb == NULL) && (inp != NULL)) {
7416fd7af143SMichael Tuexen 			/* reduce inp's ref-count */
7417fd7af143SMichael Tuexen 			SCTP_INP_WLOCK(inp);
7418fd7af143SMichael Tuexen 			SCTP_INP_DECR_REF(inp);
7419fd7af143SMichael Tuexen 			SCTP_INP_WUNLOCK(inp);
7420fd7af143SMichael Tuexen 		}
7421fd7af143SMichael Tuexen 		if (stcb) {
7422fd7af143SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
7423fd7af143SMichael Tuexen 		}
7424fd7af143SMichael Tuexen 	}
7425fd7af143SMichael Tuexen }
7426fd7af143SMichael Tuexen #endif
7427fd7af143SMichael Tuexen 
7428c54a18d2SRandall Stewart void
7429c54a18d2SRandall Stewart sctp_over_udp_stop(void)
7430c54a18d2SRandall Stewart {
7431a99b6783SRandall Stewart 	/*
7432a99b6783SRandall Stewart 	 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
7433a99b6783SRandall Stewart 	 * for writting!
7434a99b6783SRandall Stewart 	 */
74353a51a264SMichael Tuexen #ifdef INET
74363a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
74373a51a264SMichael Tuexen 		soclose(SCTP_BASE_INFO(udp4_tun_socket));
74383a51a264SMichael Tuexen 		SCTP_BASE_INFO(udp4_tun_socket) = NULL;
7439c54a18d2SRandall Stewart 	}
74403a51a264SMichael Tuexen #endif
74413a51a264SMichael Tuexen #ifdef INET6
74423a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
74433a51a264SMichael Tuexen 		soclose(SCTP_BASE_INFO(udp6_tun_socket));
74443a51a264SMichael Tuexen 		SCTP_BASE_INFO(udp6_tun_socket) = NULL;
74453a51a264SMichael Tuexen 	}
74463a51a264SMichael Tuexen #endif
7447a99b6783SRandall Stewart }
7448ea5eba11SMichael Tuexen 
7449c54a18d2SRandall Stewart int
7450c54a18d2SRandall Stewart sctp_over_udp_start(void)
7451c54a18d2SRandall Stewart {
7452a99b6783SRandall Stewart 	uint16_t port;
7453a99b6783SRandall Stewart 	int ret;
74543a51a264SMichael Tuexen #ifdef INET
74553a51a264SMichael Tuexen 	struct sockaddr_in sin;
74563a51a264SMichael Tuexen #endif
74573a51a264SMichael Tuexen #ifdef INET6
74583a51a264SMichael Tuexen 	struct sockaddr_in6 sin6;
74593a51a264SMichael Tuexen #endif
7460a99b6783SRandall Stewart 	/*
7461a99b6783SRandall Stewart 	 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
7462a99b6783SRandall Stewart 	 * for writting!
7463a99b6783SRandall Stewart 	 */
7464a99b6783SRandall Stewart 	port = SCTP_BASE_SYSCTL(sctp_udp_tunneling_port);
74653a51a264SMichael Tuexen 	if (ntohs(port) == 0) {
7466a99b6783SRandall Stewart 		/* Must have a port set */
7467a99b6783SRandall Stewart 		return (EINVAL);
7468a99b6783SRandall Stewart 	}
74693a51a264SMichael Tuexen #ifdef INET
74703a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
7471a99b6783SRandall Stewart 		/* Already running -- must stop first */
7472a99b6783SRandall Stewart 		return (EALREADY);
7473a99b6783SRandall Stewart 	}
74743a51a264SMichael Tuexen #endif
74753a51a264SMichael Tuexen #ifdef INET6
74763a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
74773a51a264SMichael Tuexen 		/* Already running -- must stop first */
74783a51a264SMichael Tuexen 		return (EALREADY);
7479a99b6783SRandall Stewart 	}
74803a51a264SMichael Tuexen #endif
74813a51a264SMichael Tuexen #ifdef INET
74823a51a264SMichael Tuexen 	if ((ret = socreate(PF_INET, &SCTP_BASE_INFO(udp4_tun_socket),
74833a51a264SMichael Tuexen 	    SOCK_DGRAM, IPPROTO_UDP,
74843a51a264SMichael Tuexen 	    curthread->td_ucred, curthread))) {
7485a99b6783SRandall Stewart 		sctp_over_udp_stop();
7486a99b6783SRandall Stewart 		return (ret);
7487a99b6783SRandall Stewart 	}
74883a51a264SMichael Tuexen 	/* Call the special UDP hook. */
74893a51a264SMichael Tuexen 	if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp4_tun_socket),
7490fd7af143SMichael Tuexen 	    sctp_recv_udp_tunneled_packet,
7491fd7af143SMichael Tuexen 	    sctp_recv_icmp_tunneled_packet,
7492fd7af143SMichael Tuexen 	    NULL))) {
74933a51a264SMichael Tuexen 		sctp_over_udp_stop();
74943a51a264SMichael Tuexen 		return (ret);
74953a51a264SMichael Tuexen 	}
74963a51a264SMichael Tuexen 	/* Ok, we have a socket, bind it to the port. */
74973a51a264SMichael Tuexen 	memset(&sin, 0, sizeof(struct sockaddr_in));
74983a51a264SMichael Tuexen 	sin.sin_len = sizeof(struct sockaddr_in);
74993a51a264SMichael Tuexen 	sin.sin_family = AF_INET;
75003a51a264SMichael Tuexen 	sin.sin_port = htons(port);
75013a51a264SMichael Tuexen 	if ((ret = sobind(SCTP_BASE_INFO(udp4_tun_socket),
75023a51a264SMichael Tuexen 	    (struct sockaddr *)&sin, curthread))) {
75033a51a264SMichael Tuexen 		sctp_over_udp_stop();
75043a51a264SMichael Tuexen 		return (ret);
75053a51a264SMichael Tuexen 	}
75063a51a264SMichael Tuexen #endif
75073a51a264SMichael Tuexen #ifdef INET6
75083a51a264SMichael Tuexen 	if ((ret = socreate(PF_INET6, &SCTP_BASE_INFO(udp6_tun_socket),
75093a51a264SMichael Tuexen 	    SOCK_DGRAM, IPPROTO_UDP,
75103a51a264SMichael Tuexen 	    curthread->td_ucred, curthread))) {
75113a51a264SMichael Tuexen 		sctp_over_udp_stop();
75123a51a264SMichael Tuexen 		return (ret);
75133a51a264SMichael Tuexen 	}
75143a51a264SMichael Tuexen 	/* Call the special UDP hook. */
75153a51a264SMichael Tuexen 	if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp6_tun_socket),
7516fd7af143SMichael Tuexen 	    sctp_recv_udp_tunneled_packet,
7517fd7af143SMichael Tuexen 	    sctp_recv_icmp6_tunneled_packet,
7518fd7af143SMichael Tuexen 	    NULL))) {
75193a51a264SMichael Tuexen 		sctp_over_udp_stop();
75203a51a264SMichael Tuexen 		return (ret);
75213a51a264SMichael Tuexen 	}
75223a51a264SMichael Tuexen 	/* Ok, we have a socket, bind it to the port. */
75233a51a264SMichael Tuexen 	memset(&sin6, 0, sizeof(struct sockaddr_in6));
75243a51a264SMichael Tuexen 	sin6.sin6_len = sizeof(struct sockaddr_in6);
75253a51a264SMichael Tuexen 	sin6.sin6_family = AF_INET6;
75263a51a264SMichael Tuexen 	sin6.sin6_port = htons(port);
75273a51a264SMichael Tuexen 	if ((ret = sobind(SCTP_BASE_INFO(udp6_tun_socket),
75283a51a264SMichael Tuexen 	    (struct sockaddr *)&sin6, curthread))) {
75293a51a264SMichael Tuexen 		sctp_over_udp_stop();
75303a51a264SMichael Tuexen 		return (ret);
75313a51a264SMichael Tuexen 	}
75323a51a264SMichael Tuexen #endif
7533a99b6783SRandall Stewart 	return (0);
7534c54a18d2SRandall Stewart }
753510e0318aSMichael Tuexen 
753610e0318aSMichael Tuexen /*
753710e0318aSMichael Tuexen  * sctp_min_mtu ()returns the minimum of all non-zero arguments.
753810e0318aSMichael Tuexen  * If all arguments are zero, zero is returned.
753910e0318aSMichael Tuexen  */
754010e0318aSMichael Tuexen uint32_t
7541b0471b4bSMichael Tuexen sctp_min_mtu(uint32_t mtu1, uint32_t mtu2, uint32_t mtu3)
7542b0471b4bSMichael Tuexen {
754310e0318aSMichael Tuexen 	if (mtu1 > 0) {
754410e0318aSMichael Tuexen 		if (mtu2 > 0) {
754510e0318aSMichael Tuexen 			if (mtu3 > 0) {
754610e0318aSMichael Tuexen 				return (min(mtu1, min(mtu2, mtu3)));
754710e0318aSMichael Tuexen 			} else {
754810e0318aSMichael Tuexen 				return (min(mtu1, mtu2));
754910e0318aSMichael Tuexen 			}
755010e0318aSMichael Tuexen 		} else {
755110e0318aSMichael Tuexen 			if (mtu3 > 0) {
755210e0318aSMichael Tuexen 				return (min(mtu1, mtu3));
755310e0318aSMichael Tuexen 			} else {
755410e0318aSMichael Tuexen 				return (mtu1);
755510e0318aSMichael Tuexen 			}
755610e0318aSMichael Tuexen 		}
755710e0318aSMichael Tuexen 	} else {
755810e0318aSMichael Tuexen 		if (mtu2 > 0) {
755910e0318aSMichael Tuexen 			if (mtu3 > 0) {
756010e0318aSMichael Tuexen 				return (min(mtu2, mtu3));
756110e0318aSMichael Tuexen 			} else {
756210e0318aSMichael Tuexen 				return (mtu2);
756310e0318aSMichael Tuexen 			}
756410e0318aSMichael Tuexen 		} else {
756510e0318aSMichael Tuexen 			return (mtu3);
756610e0318aSMichael Tuexen 		}
756710e0318aSMichael Tuexen 	}
756810e0318aSMichael Tuexen }
756910e0318aSMichael Tuexen 
757010e0318aSMichael Tuexen void
757110e0318aSMichael Tuexen sctp_hc_set_mtu(union sctp_sockstore *addr, uint16_t fibnum, uint32_t mtu)
757210e0318aSMichael Tuexen {
757310e0318aSMichael Tuexen 	struct in_conninfo inc;
757410e0318aSMichael Tuexen 
757510e0318aSMichael Tuexen 	memset(&inc, 0, sizeof(struct in_conninfo));
757610e0318aSMichael Tuexen 	inc.inc_fibnum = fibnum;
757710e0318aSMichael Tuexen 	switch (addr->sa.sa_family) {
757810e0318aSMichael Tuexen #ifdef INET
757910e0318aSMichael Tuexen 	case AF_INET:
758010e0318aSMichael Tuexen 		inc.inc_faddr = addr->sin.sin_addr;
758110e0318aSMichael Tuexen 		break;
758210e0318aSMichael Tuexen #endif
758310e0318aSMichael Tuexen #ifdef INET6
758410e0318aSMichael Tuexen 	case AF_INET6:
758510e0318aSMichael Tuexen 		inc.inc_flags |= INC_ISIPV6;
758610e0318aSMichael Tuexen 		inc.inc6_faddr = addr->sin6.sin6_addr;
758710e0318aSMichael Tuexen 		break;
758810e0318aSMichael Tuexen #endif
758910e0318aSMichael Tuexen 	default:
759010e0318aSMichael Tuexen 		return;
759110e0318aSMichael Tuexen 	}
759210e0318aSMichael Tuexen 	tcp_hc_updatemtu(&inc, (u_long)mtu);
759310e0318aSMichael Tuexen }
759410e0318aSMichael Tuexen 
759510e0318aSMichael Tuexen uint32_t
7596b0471b4bSMichael Tuexen sctp_hc_get_mtu(union sctp_sockstore *addr, uint16_t fibnum)
7597b0471b4bSMichael Tuexen {
759810e0318aSMichael Tuexen 	struct in_conninfo inc;
759910e0318aSMichael Tuexen 
760010e0318aSMichael Tuexen 	memset(&inc, 0, sizeof(struct in_conninfo));
760110e0318aSMichael Tuexen 	inc.inc_fibnum = fibnum;
760210e0318aSMichael Tuexen 	switch (addr->sa.sa_family) {
760310e0318aSMichael Tuexen #ifdef INET
760410e0318aSMichael Tuexen 	case AF_INET:
760510e0318aSMichael Tuexen 		inc.inc_faddr = addr->sin.sin_addr;
760610e0318aSMichael Tuexen 		break;
760710e0318aSMichael Tuexen #endif
760810e0318aSMichael Tuexen #ifdef INET6
760910e0318aSMichael Tuexen 	case AF_INET6:
761010e0318aSMichael Tuexen 		inc.inc_flags |= INC_ISIPV6;
761110e0318aSMichael Tuexen 		inc.inc6_faddr = addr->sin6.sin6_addr;
761210e0318aSMichael Tuexen 		break;
761310e0318aSMichael Tuexen #endif
761410e0318aSMichael Tuexen 	default:
761510e0318aSMichael Tuexen 		return (0);
761610e0318aSMichael Tuexen 	}
761710e0318aSMichael Tuexen 	return ((uint32_t)tcp_hc_getmtu(&inc));
761810e0318aSMichael Tuexen }
76196ef849e6SMichael Tuexen 
76201a0b0216SMichael Tuexen void
76211a0b0216SMichael Tuexen sctp_set_state(struct sctp_tcb *stcb, int new_state)
76221a0b0216SMichael Tuexen {
76231e88cc8bSMichael Tuexen #if defined(KDTRACE_HOOKS)
76241e88cc8bSMichael Tuexen 	int old_state = stcb->asoc.state;
76251e88cc8bSMichael Tuexen #endif
76261e88cc8bSMichael Tuexen 
76271a0b0216SMichael Tuexen 	KASSERT((new_state & ~SCTP_STATE_MASK) == 0,
76281a0b0216SMichael Tuexen 	    ("sctp_set_state: Can't set substate (new_state = %x)",
76291a0b0216SMichael Tuexen 	    new_state));
76301a0b0216SMichael Tuexen 	stcb->asoc.state = (stcb->asoc.state & ~SCTP_STATE_MASK) | new_state;
76311a0b0216SMichael Tuexen 	if ((new_state == SCTP_STATE_SHUTDOWN_RECEIVED) ||
76321a0b0216SMichael Tuexen 	    (new_state == SCTP_STATE_SHUTDOWN_SENT) ||
76331a0b0216SMichael Tuexen 	    (new_state == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
76341a0b0216SMichael Tuexen 		SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
76351a0b0216SMichael Tuexen 	}
76361e88cc8bSMichael Tuexen #if defined(KDTRACE_HOOKS)
76371e88cc8bSMichael Tuexen 	if (((old_state & SCTP_STATE_MASK) != new_state) &&
76381e88cc8bSMichael Tuexen 	    !(((old_state & SCTP_STATE_MASK) == SCTP_STATE_EMPTY) &&
76391e88cc8bSMichael Tuexen 	    (new_state == SCTP_STATE_INUSE))) {
76401e88cc8bSMichael Tuexen 		SCTP_PROBE6(state__change, NULL, stcb, NULL, stcb, NULL, old_state);
76411e88cc8bSMichael Tuexen 	}
76421e88cc8bSMichael Tuexen #endif
76431a0b0216SMichael Tuexen }
76441a0b0216SMichael Tuexen 
76451a0b0216SMichael Tuexen void
76461a0b0216SMichael Tuexen sctp_add_substate(struct sctp_tcb *stcb, int substate)
76471a0b0216SMichael Tuexen {
76481e88cc8bSMichael Tuexen #if defined(KDTRACE_HOOKS)
76491e88cc8bSMichael Tuexen 	int old_state = stcb->asoc.state;
76501e88cc8bSMichael Tuexen #endif
76511e88cc8bSMichael Tuexen 
76521a0b0216SMichael Tuexen 	KASSERT((substate & SCTP_STATE_MASK) == 0,
76531a0b0216SMichael Tuexen 	    ("sctp_add_substate: Can't set state (substate = %x)",
76541a0b0216SMichael Tuexen 	    substate));
76551a0b0216SMichael Tuexen 	stcb->asoc.state |= substate;
76561e88cc8bSMichael Tuexen #if defined(KDTRACE_HOOKS)
76571e88cc8bSMichael Tuexen 	if (((substate & SCTP_STATE_ABOUT_TO_BE_FREED) &&
76581e88cc8bSMichael Tuexen 	    ((old_state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) ||
76591e88cc8bSMichael Tuexen 	    ((substate & SCTP_STATE_SHUTDOWN_PENDING) &&
76601e88cc8bSMichael Tuexen 	    ((old_state & SCTP_STATE_SHUTDOWN_PENDING) == 0))) {
76611e88cc8bSMichael Tuexen 		SCTP_PROBE6(state__change, NULL, stcb, NULL, stcb, NULL, old_state);
76621e88cc8bSMichael Tuexen 	}
76631e88cc8bSMichael Tuexen #endif
76641a0b0216SMichael Tuexen }
7665