xref: /freebsd/sys/netinet/sctputil.c (revision bdd4630c9a9cea64830f981fc897ac953c48892c)
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 			tot_out = 0;
740f8829a4aSRandall Stewart 			TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
741f8829a4aSRandall Stewart 				if ((chk->whoTo == lnet) &&
742f8829a4aSRandall Stewart 				    (chk->sent < SCTP_DATAGRAM_RESEND)) {
743f8829a4aSRandall Stewart 					tot_out += chk->book_size;
744f8829a4aSRandall Stewart 				}
745f8829a4aSRandall Stewart 			}
746f8829a4aSRandall Stewart 			if (lnet->flight_size != tot_out) {
747f31e6c7fSMichael Tuexen 				SCTP_PRINTF("net:%p flight was %d corrected to %d\n",
748dd294dceSMichael Tuexen 				    (void *)lnet, lnet->flight_size,
749ad81507eSRandall Stewart 				    tot_out);
750f8829a4aSRandall Stewart 				lnet->flight_size = tot_out;
751f8829a4aSRandall Stewart 			}
752f8829a4aSRandall Stewart 		}
753f8829a4aSRandall Stewart 	}
754f8829a4aSRandall Stewart 	if (rep) {
755f8829a4aSRandall Stewart 		sctp_print_audit_report();
756f8829a4aSRandall Stewart 	}
757f8829a4aSRandall Stewart }
758f8829a4aSRandall Stewart 
759f8829a4aSRandall Stewart void
760f8829a4aSRandall Stewart sctp_audit_log(uint8_t ev, uint8_t fd)
761f8829a4aSRandall Stewart {
762f8829a4aSRandall Stewart 
763f8829a4aSRandall Stewart 	sctp_audit_data[sctp_audit_indx][0] = ev;
764f8829a4aSRandall Stewart 	sctp_audit_data[sctp_audit_indx][1] = fd;
765f8829a4aSRandall Stewart 	sctp_audit_indx++;
766f8829a4aSRandall Stewart 	if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
767f8829a4aSRandall Stewart 		sctp_audit_indx = 0;
768f8829a4aSRandall Stewart 	}
769f8829a4aSRandall Stewart }
770f8829a4aSRandall Stewart 
771f8829a4aSRandall Stewart #endif
772f8829a4aSRandall Stewart 
773f8829a4aSRandall Stewart /*
77425ec3553SMichael Tuexen  * The conversion from time to ticks and vice versa is done by rounding
77525ec3553SMichael Tuexen  * upwards. This way we can test in the code the time to be positive and
77625ec3553SMichael Tuexen  * know that this corresponds to a positive number of ticks.
77725ec3553SMichael Tuexen  */
77825ec3553SMichael Tuexen 
77925ec3553SMichael Tuexen uint32_t
78025ec3553SMichael Tuexen sctp_msecs_to_ticks(uint32_t msecs)
78125ec3553SMichael Tuexen {
78225ec3553SMichael Tuexen 	uint64_t temp;
78325ec3553SMichael Tuexen 	uint32_t ticks;
78425ec3553SMichael Tuexen 
78525ec3553SMichael Tuexen 	if (hz == 1000) {
78625ec3553SMichael Tuexen 		ticks = msecs;
78725ec3553SMichael Tuexen 	} else {
78825ec3553SMichael Tuexen 		temp = (((uint64_t)msecs * hz) + 999) / 1000;
78925ec3553SMichael Tuexen 		if (temp > UINT32_MAX) {
79025ec3553SMichael Tuexen 			ticks = UINT32_MAX;
79125ec3553SMichael Tuexen 		} else {
79225ec3553SMichael Tuexen 			ticks = (uint32_t)temp;
79325ec3553SMichael Tuexen 		}
79425ec3553SMichael Tuexen 	}
79525ec3553SMichael Tuexen 	return (ticks);
79625ec3553SMichael Tuexen }
79725ec3553SMichael Tuexen 
79825ec3553SMichael Tuexen uint32_t
79925ec3553SMichael Tuexen sctp_ticks_to_msecs(uint32_t ticks)
80025ec3553SMichael Tuexen {
80125ec3553SMichael Tuexen 	uint64_t temp;
80225ec3553SMichael Tuexen 	uint32_t msecs;
80325ec3553SMichael Tuexen 
80425ec3553SMichael Tuexen 	if (hz == 1000) {
80525ec3553SMichael Tuexen 		msecs = ticks;
80625ec3553SMichael Tuexen 	} else {
80725ec3553SMichael Tuexen 		temp = (((uint64_t)ticks * 1000) + (hz - 1)) / hz;
80825ec3553SMichael Tuexen 		if (temp > UINT32_MAX) {
80925ec3553SMichael Tuexen 			msecs = UINT32_MAX;
81025ec3553SMichael Tuexen 		} else {
81125ec3553SMichael Tuexen 			msecs = (uint32_t)temp;
81225ec3553SMichael Tuexen 		}
81325ec3553SMichael Tuexen 	}
81425ec3553SMichael Tuexen 	return (msecs);
81525ec3553SMichael Tuexen }
81625ec3553SMichael Tuexen 
81725ec3553SMichael Tuexen uint32_t
81825ec3553SMichael Tuexen sctp_secs_to_ticks(uint32_t secs)
81925ec3553SMichael Tuexen {
82025ec3553SMichael Tuexen 	uint64_t temp;
82125ec3553SMichael Tuexen 	uint32_t ticks;
82225ec3553SMichael Tuexen 
82325ec3553SMichael Tuexen 	temp = (uint64_t)secs * hz;
82425ec3553SMichael Tuexen 	if (temp > UINT32_MAX) {
82525ec3553SMichael Tuexen 		ticks = UINT32_MAX;
82625ec3553SMichael Tuexen 	} else {
82725ec3553SMichael Tuexen 		ticks = (uint32_t)temp;
82825ec3553SMichael Tuexen 	}
82925ec3553SMichael Tuexen 	return (ticks);
83025ec3553SMichael Tuexen }
83125ec3553SMichael Tuexen 
83225ec3553SMichael Tuexen uint32_t
83325ec3553SMichael Tuexen sctp_ticks_to_secs(uint32_t ticks)
83425ec3553SMichael Tuexen {
83525ec3553SMichael Tuexen 	uint64_t temp;
83625ec3553SMichael Tuexen 	uint32_t secs;
83725ec3553SMichael Tuexen 
83825ec3553SMichael Tuexen 	temp = ((uint64_t)ticks + (hz - 1)) / hz;
83925ec3553SMichael Tuexen 	if (temp > UINT32_MAX) {
84025ec3553SMichael Tuexen 		secs = UINT32_MAX;
84125ec3553SMichael Tuexen 	} else {
84225ec3553SMichael Tuexen 		secs = (uint32_t)temp;
84325ec3553SMichael Tuexen 	}
84425ec3553SMichael Tuexen 	return (secs);
84525ec3553SMichael Tuexen }
84625ec3553SMichael Tuexen 
84725ec3553SMichael Tuexen /*
84812af6654SMichael Tuexen  * sctp_stop_timers_for_shutdown() should be called
84912af6654SMichael Tuexen  * when entering the SHUTDOWN_SENT or SHUTDOWN_ACK_SENT
85012af6654SMichael Tuexen  * state to make sure that all timers are stopped.
85112af6654SMichael Tuexen  */
85212af6654SMichael Tuexen void
85312af6654SMichael Tuexen sctp_stop_timers_for_shutdown(struct sctp_tcb *stcb)
85412af6654SMichael Tuexen {
8555555400aSMichael Tuexen 	struct sctp_inpcb *inp;
85612af6654SMichael Tuexen 	struct sctp_nets *net;
85712af6654SMichael Tuexen 
8585555400aSMichael Tuexen 	inp = stcb->sctp_ep;
85912af6654SMichael Tuexen 
8605555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_RECV, inp, stcb, NULL,
8615555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_12);
8625555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, inp, stcb, NULL,
8635555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_13);
8645555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, inp, stcb, NULL,
8655555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_14);
8665555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL,
8675555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_15);
8685555400aSMichael Tuexen 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
8695555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
8705555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_16);
8715555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
8725555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_17);
8735555400aSMichael Tuexen 	}
8745555400aSMichael Tuexen }
8755555400aSMichael Tuexen 
8765555400aSMichael Tuexen void
8778803350dSMichael Tuexen sctp_stop_association_timers(struct sctp_tcb *stcb, bool stop_assoc_kill_timer)
8785555400aSMichael Tuexen {
8795555400aSMichael Tuexen 	struct sctp_inpcb *inp;
8805555400aSMichael Tuexen 	struct sctp_nets *net;
8815555400aSMichael Tuexen 
8825555400aSMichael Tuexen 	inp = stcb->sctp_ep;
8835555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_RECV, inp, stcb, NULL,
8845555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_18);
8855555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, inp, stcb, NULL,
8865555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_19);
8878803350dSMichael Tuexen 	if (stop_assoc_kill_timer) {
8885555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL,
8895555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_20);
8905555400aSMichael Tuexen 	}
8915555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, inp, stcb, NULL,
8925555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_21);
8935555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL,
8945555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_22);
8955555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNGUARD, inp, stcb, NULL,
8965555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_23);
8975555400aSMichael Tuexen 	/* Mobility adaptation */
8985555400aSMichael Tuexen 	sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, inp, stcb, NULL,
8995555400aSMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_24);
9005555400aSMichael Tuexen 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
9015555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
9025555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_25);
9035555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net,
9045555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_26);
9055555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, inp, stcb, net,
9065555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_27);
9075555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net,
9085555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_28);
9095555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, inp, stcb, net,
9105555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_29);
9115555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
9125555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_30);
9135555400aSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
9145555400aSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_31);
91512af6654SMichael Tuexen 	}
91612af6654SMichael Tuexen }
91712af6654SMichael Tuexen 
91812af6654SMichael Tuexen /*
919589c42c2SMichael Tuexen  * A list of sizes based on typical mtu's, used only if next hop size not
920589c42c2SMichael Tuexen  * returned. These values MUST be multiples of 4 and MUST be ordered.
921f8829a4aSRandall Stewart  */
922437fc91aSMichael Tuexen static uint32_t sctp_mtu_sizes[] = {
923f8829a4aSRandall Stewart 	68,
924f8829a4aSRandall Stewart 	296,
925f8829a4aSRandall Stewart 	508,
926f8829a4aSRandall Stewart 	512,
927f8829a4aSRandall Stewart 	544,
928f8829a4aSRandall Stewart 	576,
929589c42c2SMichael Tuexen 	1004,
930f8829a4aSRandall Stewart 	1492,
931f8829a4aSRandall Stewart 	1500,
932f8829a4aSRandall Stewart 	1536,
933589c42c2SMichael Tuexen 	2000,
934f8829a4aSRandall Stewart 	2048,
935f8829a4aSRandall Stewart 	4352,
936f8829a4aSRandall Stewart 	4464,
93742078d5aSMichael Tuexen 	8168,
938589c42c2SMichael Tuexen 	17912,
939f8829a4aSRandall Stewart 	32000,
940589c42c2SMichael Tuexen 	65532
941f8829a4aSRandall Stewart };
942f8829a4aSRandall Stewart 
943f8829a4aSRandall Stewart /*
944589c42c2SMichael Tuexen  * Return the largest MTU in sctp_mtu_sizes smaller than val.
945589c42c2SMichael Tuexen  * If val is smaller than the minimum, just return the largest
946589c42c2SMichael Tuexen  * multiple of 4 smaller or equal to val.
947589c42c2SMichael Tuexen  * Ensure that the result is a multiple of 4.
948f8829a4aSRandall Stewart  */
949437fc91aSMichael Tuexen uint32_t
950b0471b4bSMichael Tuexen sctp_get_prev_mtu(uint32_t val)
951b0471b4bSMichael Tuexen {
952437fc91aSMichael Tuexen 	uint32_t i;
953437fc91aSMichael Tuexen 
954eef8d4a9SMichael Tuexen 	val &= 0xfffffffc;
955437fc91aSMichael Tuexen 	if (val <= sctp_mtu_sizes[0]) {
956437fc91aSMichael Tuexen 		return (val);
957437fc91aSMichael Tuexen 	}
958437fc91aSMichael Tuexen 	for (i = 1; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
959437fc91aSMichael Tuexen 		if (val <= sctp_mtu_sizes[i]) {
960f8829a4aSRandall Stewart 			break;
961f8829a4aSRandall Stewart 		}
962f8829a4aSRandall Stewart 	}
963589c42c2SMichael Tuexen 	KASSERT((sctp_mtu_sizes[i - 1] & 0x00000003) == 0,
964589c42c2SMichael Tuexen 	    ("sctp_mtu_sizes[%u] not a multiple of 4", i - 1));
965437fc91aSMichael Tuexen 	return (sctp_mtu_sizes[i - 1]);
966437fc91aSMichael Tuexen }
967437fc91aSMichael Tuexen 
968437fc91aSMichael Tuexen /*
969589c42c2SMichael Tuexen  * Return the smallest MTU in sctp_mtu_sizes larger than val.
970589c42c2SMichael Tuexen  * If val is larger than the maximum, just return the largest multiple of 4 smaller
971589c42c2SMichael Tuexen  * or equal to val.
972589c42c2SMichael Tuexen  * Ensure that the result is a multiple of 4.
973437fc91aSMichael Tuexen  */
974437fc91aSMichael Tuexen uint32_t
975b0471b4bSMichael Tuexen sctp_get_next_mtu(uint32_t val)
976b0471b4bSMichael Tuexen {
977437fc91aSMichael Tuexen 	/* select another MTU that is just bigger than this one */
978437fc91aSMichael Tuexen 	uint32_t i;
979437fc91aSMichael Tuexen 
980eef8d4a9SMichael Tuexen 	val &= 0xfffffffc;
981437fc91aSMichael Tuexen 	for (i = 0; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
982437fc91aSMichael Tuexen 		if (val < sctp_mtu_sizes[i]) {
983589c42c2SMichael Tuexen 			KASSERT((sctp_mtu_sizes[i] & 0x00000003) == 0,
984589c42c2SMichael Tuexen 			    ("sctp_mtu_sizes[%u] not a multiple of 4", i));
985437fc91aSMichael Tuexen 			return (sctp_mtu_sizes[i]);
986437fc91aSMichael Tuexen 		}
987437fc91aSMichael Tuexen 	}
988437fc91aSMichael Tuexen 	return (val);
989f8829a4aSRandall Stewart }
990f8829a4aSRandall Stewart 
991f8829a4aSRandall Stewart void
992f8829a4aSRandall Stewart sctp_fill_random_store(struct sctp_pcb *m)
993f8829a4aSRandall Stewart {
994f8829a4aSRandall Stewart 	/*
995f8829a4aSRandall Stewart 	 * Here we use the MD5/SHA-1 to hash with our good randomNumbers and
996f8829a4aSRandall Stewart 	 * our counter. The result becomes our good random numbers and we
997f8829a4aSRandall Stewart 	 * then setup to give these out. Note that we do no locking to
998f8829a4aSRandall Stewart 	 * protect this. This is ok, since if competing folks call this we
99917205eccSRandall Stewart 	 * will get more gobbled gook in the random store which is what we
1000f8829a4aSRandall Stewart 	 * want. There is a danger that two guys will use the same random
1001f8829a4aSRandall Stewart 	 * numbers, but thats ok too since that is random as well :->
1002f8829a4aSRandall Stewart 	 */
1003f8829a4aSRandall Stewart 	m->store_at = 0;
1004ad81507eSRandall Stewart 	(void)sctp_hmac(SCTP_HMAC, (uint8_t *)m->random_numbers,
1005f8829a4aSRandall Stewart 	    sizeof(m->random_numbers), (uint8_t *)&m->random_counter,
1006f8829a4aSRandall Stewart 	    sizeof(m->random_counter), (uint8_t *)m->random_store);
1007f8829a4aSRandall Stewart 	m->random_counter++;
1008f8829a4aSRandall Stewart }
1009f8829a4aSRandall Stewart 
1010f8829a4aSRandall Stewart uint32_t
1011b0471b4bSMichael Tuexen sctp_select_initial_TSN(struct sctp_pcb *inp)
1012b0471b4bSMichael Tuexen {
1013f8829a4aSRandall Stewart 	/*
1014f8829a4aSRandall Stewart 	 * A true implementation should use random selection process to get
1015f8829a4aSRandall Stewart 	 * the initial stream sequence number, using RFC1750 as a good
1016f8829a4aSRandall Stewart 	 * guideline
1017f8829a4aSRandall Stewart 	 */
1018139bc87fSRandall Stewart 	uint32_t x, *xp;
1019f8829a4aSRandall Stewart 	uint8_t *p;
1020851b7298SRandall Stewart 	int store_at, new_store;
1021f8829a4aSRandall Stewart 
1022851b7298SRandall Stewart 	if (inp->initial_sequence_debug != 0) {
1023f8829a4aSRandall Stewart 		uint32_t ret;
1024f8829a4aSRandall Stewart 
1025851b7298SRandall Stewart 		ret = inp->initial_sequence_debug;
1026851b7298SRandall Stewart 		inp->initial_sequence_debug++;
1027f8829a4aSRandall Stewart 		return (ret);
1028f8829a4aSRandall Stewart 	}
1029851b7298SRandall Stewart retry:
1030851b7298SRandall Stewart 	store_at = inp->store_at;
1031851b7298SRandall Stewart 	new_store = store_at + sizeof(uint32_t);
1032851b7298SRandall Stewart 	if (new_store >= (SCTP_SIGNATURE_SIZE - 3)) {
1033851b7298SRandall Stewart 		new_store = 0;
1034f8829a4aSRandall Stewart 	}
1035851b7298SRandall Stewart 	if (!atomic_cmpset_int(&inp->store_at, store_at, new_store)) {
1036851b7298SRandall Stewart 		goto retry;
1037851b7298SRandall Stewart 	}
1038851b7298SRandall Stewart 	if (new_store == 0) {
1039851b7298SRandall Stewart 		/* Refill the random store */
1040851b7298SRandall Stewart 		sctp_fill_random_store(inp);
1041851b7298SRandall Stewart 	}
1042851b7298SRandall Stewart 	p = &inp->random_store[store_at];
1043139bc87fSRandall Stewart 	xp = (uint32_t *)p;
1044f8829a4aSRandall Stewart 	x = *xp;
1045f8829a4aSRandall Stewart 	return (x);
1046f8829a4aSRandall Stewart }
1047f8829a4aSRandall Stewart 
1048f8829a4aSRandall Stewart uint32_t
1049b0471b4bSMichael Tuexen sctp_select_a_tag(struct sctp_inpcb *inp, uint16_t lport, uint16_t rport, int check)
1050b0471b4bSMichael Tuexen {
10517215cc1bSMichael Tuexen 	uint32_t x;
1052f8829a4aSRandall Stewart 	struct timeval now;
1053f8829a4aSRandall Stewart 
10547215cc1bSMichael Tuexen 	if (check) {
10556e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
10567215cc1bSMichael Tuexen 	}
10577215cc1bSMichael Tuexen 	for (;;) {
1058851b7298SRandall Stewart 		x = sctp_select_initial_TSN(&inp->sctp_ep);
1059f8829a4aSRandall Stewart 		if (x == 0) {
1060f8829a4aSRandall Stewart 			/* we never use 0 */
1061f8829a4aSRandall Stewart 			continue;
1062f8829a4aSRandall Stewart 		}
10637215cc1bSMichael Tuexen 		if (!check || sctp_is_vtag_good(x, lport, rport, &now)) {
10647215cc1bSMichael Tuexen 			break;
1065f8829a4aSRandall Stewart 		}
1066f8829a4aSRandall Stewart 	}
1067f8829a4aSRandall Stewart 	return (x);
1068f8829a4aSRandall Stewart }
1069f8829a4aSRandall Stewart 
1070e92c2a8dSMichael Tuexen int32_t
1071b0471b4bSMichael Tuexen sctp_map_assoc_state(int kernel_state)
1072b0471b4bSMichael Tuexen {
1073e92c2a8dSMichael Tuexen 	int32_t user_state;
1074e92c2a8dSMichael Tuexen 
1075e92c2a8dSMichael Tuexen 	if (kernel_state & SCTP_STATE_WAS_ABORTED) {
1076e92c2a8dSMichael Tuexen 		user_state = SCTP_CLOSED;
1077e92c2a8dSMichael Tuexen 	} else if (kernel_state & SCTP_STATE_SHUTDOWN_PENDING) {
1078e92c2a8dSMichael Tuexen 		user_state = SCTP_SHUTDOWN_PENDING;
1079e92c2a8dSMichael Tuexen 	} else {
1080e92c2a8dSMichael Tuexen 		switch (kernel_state & SCTP_STATE_MASK) {
1081e92c2a8dSMichael Tuexen 		case SCTP_STATE_EMPTY:
1082e92c2a8dSMichael Tuexen 			user_state = SCTP_CLOSED;
1083e92c2a8dSMichael Tuexen 			break;
1084e92c2a8dSMichael Tuexen 		case SCTP_STATE_INUSE:
1085e92c2a8dSMichael Tuexen 			user_state = SCTP_CLOSED;
1086e92c2a8dSMichael Tuexen 			break;
1087e92c2a8dSMichael Tuexen 		case SCTP_STATE_COOKIE_WAIT:
1088e92c2a8dSMichael Tuexen 			user_state = SCTP_COOKIE_WAIT;
1089e92c2a8dSMichael Tuexen 			break;
1090e92c2a8dSMichael Tuexen 		case SCTP_STATE_COOKIE_ECHOED:
1091e92c2a8dSMichael Tuexen 			user_state = SCTP_COOKIE_ECHOED;
1092e92c2a8dSMichael Tuexen 			break;
1093e92c2a8dSMichael Tuexen 		case SCTP_STATE_OPEN:
1094e92c2a8dSMichael Tuexen 			user_state = SCTP_ESTABLISHED;
1095e92c2a8dSMichael Tuexen 			break;
1096e92c2a8dSMichael Tuexen 		case SCTP_STATE_SHUTDOWN_SENT:
1097e92c2a8dSMichael Tuexen 			user_state = SCTP_SHUTDOWN_SENT;
1098e92c2a8dSMichael Tuexen 			break;
1099e92c2a8dSMichael Tuexen 		case SCTP_STATE_SHUTDOWN_RECEIVED:
1100e92c2a8dSMichael Tuexen 			user_state = SCTP_SHUTDOWN_RECEIVED;
1101e92c2a8dSMichael Tuexen 			break;
1102e92c2a8dSMichael Tuexen 		case SCTP_STATE_SHUTDOWN_ACK_SENT:
1103e92c2a8dSMichael Tuexen 			user_state = SCTP_SHUTDOWN_ACK_SENT;
1104e92c2a8dSMichael Tuexen 			break;
1105e92c2a8dSMichael Tuexen 		default:
1106e92c2a8dSMichael Tuexen 			user_state = SCTP_CLOSED;
1107e92c2a8dSMichael Tuexen 			break;
1108e92c2a8dSMichael Tuexen 		}
1109e92c2a8dSMichael Tuexen 	}
1110e92c2a8dSMichael Tuexen 	return (user_state);
1111e92c2a8dSMichael Tuexen }
1112e92c2a8dSMichael Tuexen 
1113f8829a4aSRandall Stewart int
1114a1cb341bSMichael Tuexen sctp_init_asoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1115c979034bSMichael Tuexen     uint32_t override_tag, uint32_t vrf_id, uint16_t o_strms)
1116f8829a4aSRandall Stewart {
11170696e120SRandall Stewart 	struct sctp_association *asoc;
11180696e120SRandall Stewart 
1119f8829a4aSRandall Stewart 	/*
1120f8829a4aSRandall Stewart 	 * Anything set to zero is taken care of by the allocation routine's
1121f8829a4aSRandall Stewart 	 * bzero
1122f8829a4aSRandall Stewart 	 */
1123f8829a4aSRandall Stewart 
1124f8829a4aSRandall Stewart 	/*
1125f8829a4aSRandall Stewart 	 * Up front select what scoping to apply on addresses I tell my peer
1126f8829a4aSRandall Stewart 	 * Not sure what to do with these right now, we will need to come up
1127f8829a4aSRandall Stewart 	 * with a way to set them. We may need to pass them through from the
1128f8829a4aSRandall Stewart 	 * caller in the sctp_aloc_assoc() function.
1129f8829a4aSRandall Stewart 	 */
1130f8829a4aSRandall Stewart 	int i;
1131f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
1132f0396ad1SMichael Tuexen 	int j;
1133f0396ad1SMichael Tuexen #endif
1134f0396ad1SMichael Tuexen 
11350696e120SRandall Stewart 	asoc = &stcb->asoc;
1136f8829a4aSRandall Stewart 	/* init all variables to a known value. */
1137839d21d6SMichael Tuexen 	SCTP_SET_STATE(stcb, SCTP_STATE_INUSE);
1138a1cb341bSMichael Tuexen 	asoc->max_burst = inp->sctp_ep.max_burst;
1139a1cb341bSMichael Tuexen 	asoc->fr_max_burst = inp->sctp_ep.fr_max_burst;
114025ec3553SMichael Tuexen 	asoc->heart_beat_delay = sctp_ticks_to_msecs(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
1141a1cb341bSMichael Tuexen 	asoc->cookie_life = inp->sctp_ep.def_cookie_life;
1142a1cb341bSMichael Tuexen 	asoc->sctp_cmt_on_off = inp->sctp_cmt_on_off;
1143f342355aSMichael Tuexen 	asoc->ecn_supported = inp->ecn_supported;
1144dd973b0eSMichael Tuexen 	asoc->prsctp_supported = inp->prsctp_supported;
1145c79bec9cSMichael Tuexen 	asoc->auth_supported = inp->auth_supported;
1146c79bec9cSMichael Tuexen 	asoc->asconf_supported = inp->asconf_supported;
1147317e00efSMichael Tuexen 	asoc->reconfig_supported = inp->reconfig_supported;
1148caea9879SMichael Tuexen 	asoc->nrsack_supported = inp->nrsack_supported;
1149cb9b8e6fSMichael Tuexen 	asoc->pktdrop_supported = inp->pktdrop_supported;
115044249214SRandall Stewart 	asoc->idata_supported = inp->idata_supported;
1151ca85e948SMichael Tuexen 	asoc->sctp_cmt_pf = (uint8_t)0;
1152a1cb341bSMichael Tuexen 	asoc->sctp_frag_point = inp->sctp_frag_point;
1153a1cb341bSMichael Tuexen 	asoc->sctp_features = inp->sctp_features;
1154a1cb341bSMichael Tuexen 	asoc->default_dscp = inp->sctp_ep.default_dscp;
115559b6d5beSMichael Tuexen 	asoc->max_cwnd = inp->max_cwnd;
115642551e99SRandall Stewart #ifdef INET6
1157a1cb341bSMichael Tuexen 	if (inp->sctp_ep.default_flowlabel) {
1158a1cb341bSMichael Tuexen 		asoc->default_flowlabel = inp->sctp_ep.default_flowlabel;
115958bdb691SMichael Tuexen 	} else {
1160a1cb341bSMichael Tuexen 		if (inp->ip_inp.inp.inp_flags & IN6P_AUTOFLOWLABEL) {
1161a1cb341bSMichael Tuexen 			asoc->default_flowlabel = sctp_select_initial_TSN(&inp->sctp_ep);
116258bdb691SMichael Tuexen 			asoc->default_flowlabel &= 0x000fffff;
116358bdb691SMichael Tuexen 			asoc->default_flowlabel |= 0x80000000;
116458bdb691SMichael Tuexen 		} else {
1165f8829a4aSRandall Stewart 			asoc->default_flowlabel = 0;
116658bdb691SMichael Tuexen 		}
116758bdb691SMichael Tuexen 	}
1168f8829a4aSRandall Stewart #endif
11699f22f500SRandall Stewart 	asoc->sb_send_resv = 0;
1170f8829a4aSRandall Stewart 	if (override_tag) {
1171f8829a4aSRandall Stewart 		asoc->my_vtag = override_tag;
1172f8829a4aSRandall Stewart 	} else {
1173a1cb341bSMichael Tuexen 		asoc->my_vtag = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
1174f8829a4aSRandall Stewart 	}
1175de0e935bSRandall Stewart 	/* Get the nonce tags */
1176a1cb341bSMichael Tuexen 	asoc->my_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
1177a1cb341bSMichael Tuexen 	asoc->peer_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
117842551e99SRandall Stewart 	asoc->vrf_id = vrf_id;
1179de0e935bSRandall Stewart 
118018e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
118118e198d3SRandall Stewart 	asoc->tsn_in_at = 0;
118218e198d3SRandall Stewart 	asoc->tsn_out_at = 0;
118318e198d3SRandall Stewart 	asoc->tsn_in_wrapped = 0;
118418e198d3SRandall Stewart 	asoc->tsn_out_wrapped = 0;
118518e198d3SRandall Stewart 	asoc->cumack_log_at = 0;
1186b201f536SRandall Stewart 	asoc->cumack_log_atsnt = 0;
118718e198d3SRandall Stewart #endif
118818e198d3SRandall Stewart #ifdef SCTP_FS_SPEC_LOG
118918e198d3SRandall Stewart 	asoc->fs_index = 0;
119018e198d3SRandall Stewart #endif
1191f8829a4aSRandall Stewart 	asoc->refcnt = 0;
1192f8829a4aSRandall Stewart 	asoc->assoc_up_sent = 0;
1193f8829a4aSRandall Stewart 	asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number = asoc->sending_seq =
1194a1cb341bSMichael Tuexen 	    sctp_select_initial_TSN(&inp->sctp_ep);
1195c54a18d2SRandall Stewart 	asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1196f8829a4aSRandall Stewart 	/* we are optimisitic here */
1197830d754dSRandall Stewart 	asoc->peer_supports_nat = 0;
1198f8829a4aSRandall Stewart 	asoc->sent_queue_retran_cnt = 0;
1199f8829a4aSRandall Stewart 
1200f8829a4aSRandall Stewart 	/* for CMT */
12018933fa13SRandall Stewart 	asoc->last_net_cmt_send_started = NULL;
1202f8829a4aSRandall Stewart 
1203f8829a4aSRandall Stewart 	/* This will need to be adjusted */
1204f8829a4aSRandall Stewart 	asoc->last_acked_seq = asoc->init_seq_number - 1;
1205f8829a4aSRandall Stewart 	asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1206f8829a4aSRandall Stewart 	asoc->asconf_seq_in = asoc->last_acked_seq;
1207f8829a4aSRandall Stewart 
1208f8829a4aSRandall Stewart 	/* here we are different, we hold the next one we expect */
1209f8829a4aSRandall Stewart 	asoc->str_reset_seq_in = asoc->last_acked_seq + 1;
1210f8829a4aSRandall Stewart 
1211a1cb341bSMichael Tuexen 	asoc->initial_init_rto_max = inp->sctp_ep.initial_init_rto_max;
1212a1cb341bSMichael Tuexen 	asoc->initial_rto = inp->sctp_ep.initial_rto;
1213f8829a4aSRandall Stewart 
121428a6addeSMichael Tuexen 	asoc->default_mtu = inp->sctp_ep.default_mtu;
1215a1cb341bSMichael Tuexen 	asoc->max_init_times = inp->sctp_ep.max_init_times;
1216a1cb341bSMichael Tuexen 	asoc->max_send_times = inp->sctp_ep.max_send_times;
1217a1cb341bSMichael Tuexen 	asoc->def_net_failure = inp->sctp_ep.def_net_failure;
1218a1cb341bSMichael Tuexen 	asoc->def_net_pf_threshold = inp->sctp_ep.def_net_pf_threshold;
1219f8829a4aSRandall Stewart 	asoc->free_chunk_cnt = 0;
1220f8829a4aSRandall Stewart 
1221f8829a4aSRandall Stewart 	asoc->iam_blocking = 0;
1222a1cb341bSMichael Tuexen 	asoc->context = inp->sctp_context;
1223a1cb341bSMichael Tuexen 	asoc->local_strreset_support = inp->local_strreset_support;
1224a1cb341bSMichael Tuexen 	asoc->def_send = inp->def_send;
122525ec3553SMichael Tuexen 	asoc->delayed_ack = sctp_ticks_to_msecs(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1226a1cb341bSMichael Tuexen 	asoc->sack_freq = inp->sctp_ep.sctp_sack_freq;
1227f8829a4aSRandall Stewart 	asoc->pr_sctp_cnt = 0;
1228f8829a4aSRandall Stewart 	asoc->total_output_queue_size = 0;
1229f8829a4aSRandall Stewart 
1230a1cb341bSMichael Tuexen 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1231a1cb341bSMichael Tuexen 		asoc->scope.ipv6_addr_legal = 1;
1232a1cb341bSMichael Tuexen 		if (SCTP_IPV6_V6ONLY(inp) == 0) {
1233a1cb341bSMichael Tuexen 			asoc->scope.ipv4_addr_legal = 1;
1234f8829a4aSRandall Stewart 		} else {
1235a1cb341bSMichael Tuexen 			asoc->scope.ipv4_addr_legal = 0;
1236f8829a4aSRandall Stewart 		}
1237f8829a4aSRandall Stewart 	} else {
1238a1cb341bSMichael Tuexen 		asoc->scope.ipv6_addr_legal = 0;
1239a1cb341bSMichael Tuexen 		asoc->scope.ipv4_addr_legal = 1;
1240f8829a4aSRandall Stewart 	}
1241f8829a4aSRandall Stewart 
1242a1cb341bSMichael Tuexen 	asoc->my_rwnd = max(SCTP_SB_LIMIT_RCV(inp->sctp_socket), SCTP_MINIMAL_RWND);
1243a1cb341bSMichael Tuexen 	asoc->peers_rwnd = SCTP_SB_LIMIT_RCV(inp->sctp_socket);
1244f8829a4aSRandall Stewart 
1245a1cb341bSMichael Tuexen 	asoc->smallest_mtu = inp->sctp_frag_point;
1246a1cb341bSMichael Tuexen 	asoc->minrto = inp->sctp_ep.sctp_minrto;
1247a1cb341bSMichael Tuexen 	asoc->maxrto = inp->sctp_ep.sctp_maxrto;
1248f8829a4aSRandall Stewart 
1249f8829a4aSRandall Stewart 	asoc->stream_locked_on = 0;
1250f8829a4aSRandall Stewart 	asoc->ecn_echo_cnt_onq = 0;
1251f8829a4aSRandall Stewart 	asoc->stream_locked = 0;
1252f8829a4aSRandall Stewart 
125342551e99SRandall Stewart 	asoc->send_sack = 1;
125442551e99SRandall Stewart 
125542551e99SRandall Stewart 	LIST_INIT(&asoc->sctp_restricted_addrs);
125642551e99SRandall Stewart 
1257f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->nets);
1258f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->pending_reply_queue);
12592afb3e84SRandall Stewart 	TAILQ_INIT(&asoc->asconf_ack_sent);
1260f8829a4aSRandall Stewart 	/* Setup to fill the hb random cache at first HB */
1261f8829a4aSRandall Stewart 	asoc->hb_random_idx = 4;
1262f8829a4aSRandall Stewart 
1263a1cb341bSMichael Tuexen 	asoc->sctp_autoclose_ticks = inp->sctp_ep.auto_close_time;
1264f8829a4aSRandall Stewart 
1265a1cb341bSMichael Tuexen 	stcb->asoc.congestion_control_module = inp->sctp_ep.sctp_default_cc_module;
1266a1cb341bSMichael Tuexen 	stcb->asoc.cc_functions = sctp_cc_functions[inp->sctp_ep.sctp_default_cc_module];
1267b54d3a6cSRandall Stewart 
1268a1cb341bSMichael Tuexen 	stcb->asoc.stream_scheduling_module = inp->sctp_ep.sctp_default_ss_module;
1269a1cb341bSMichael Tuexen 	stcb->asoc.ss_functions = sctp_ss_functions[inp->sctp_ep.sctp_default_ss_module];
1270f7a77f6fSMichael Tuexen 
1271b54d3a6cSRandall Stewart 	/*
1272f8829a4aSRandall Stewart 	 * Now the stream parameters, here we allocate space for all streams
1273f8829a4aSRandall Stewart 	 * that we request by default.
1274f8829a4aSRandall Stewart 	 */
1275ea44232bSRandall Stewart 	asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams =
1276c979034bSMichael Tuexen 	    o_strms;
1277f8829a4aSRandall Stewart 	SCTP_MALLOC(asoc->strmout, struct sctp_stream_out *,
1278f8829a4aSRandall Stewart 	    asoc->streamoutcnt * sizeof(struct sctp_stream_out),
1279207304d4SRandall Stewart 	    SCTP_M_STRMO);
1280f8829a4aSRandall Stewart 	if (asoc->strmout == NULL) {
1281f8829a4aSRandall Stewart 		/* big trouble no memory */
1282c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1283f8829a4aSRandall Stewart 		return (ENOMEM);
1284f8829a4aSRandall Stewart 	}
1285f8829a4aSRandall Stewart 	for (i = 0; i < asoc->streamoutcnt; i++) {
1286f8829a4aSRandall Stewart 		/*
1287f8829a4aSRandall Stewart 		 * inbound side must be set to 0xffff, also NOTE when we get
1288f8829a4aSRandall Stewart 		 * the INIT-ACK back (for INIT sender) we MUST reduce the
1289f8829a4aSRandall Stewart 		 * count (streamoutcnt) but first check if we sent to any of
1290f8829a4aSRandall Stewart 		 * the upper streams that were dropped (if some were). Those
1291f8829a4aSRandall Stewart 		 * that were dropped must be notified to the upper layer as
1292f8829a4aSRandall Stewart 		 * failed to send.
1293f8829a4aSRandall Stewart 		 */
1294f8829a4aSRandall Stewart 		TAILQ_INIT(&asoc->strmout[i].outqueue);
12957a051c0aSMichael Tuexen 		asoc->ss_functions.sctp_ss_init_stream(stcb, &asoc->strmout[i], NULL);
1296325c8c46SMichael Tuexen 		asoc->strmout[i].chunks_on_queues = 0;
1297f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
1298f0396ad1SMichael Tuexen 		for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1299f0396ad1SMichael Tuexen 			asoc->strmout[i].abandoned_sent[j] = 0;
1300f0396ad1SMichael Tuexen 			asoc->strmout[i].abandoned_unsent[j] = 0;
1301f0396ad1SMichael Tuexen 		}
1302f0396ad1SMichael Tuexen #else
1303f0396ad1SMichael Tuexen 		asoc->strmout[i].abandoned_sent[0] = 0;
1304f0396ad1SMichael Tuexen 		asoc->strmout[i].abandoned_unsent[0] = 0;
1305f0396ad1SMichael Tuexen #endif
13067a051c0aSMichael Tuexen 		asoc->strmout[i].next_mid_ordered = 0;
13077a051c0aSMichael Tuexen 		asoc->strmout[i].next_mid_unordered = 0;
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;
1311f8829a4aSRandall Stewart 	}
1312f7a77f6fSMichael Tuexen 	asoc->ss_functions.sctp_ss_init(stcb, asoc, 0);
1313f7a77f6fSMichael Tuexen 
1314f8829a4aSRandall Stewart 	/* Now the mapping array */
1315f8829a4aSRandall Stewart 	asoc->mapping_array_size = SCTP_INITIAL_MAPPING_ARRAY;
1316f8829a4aSRandall Stewart 	SCTP_MALLOC(asoc->mapping_array, uint8_t *, asoc->mapping_array_size,
1317207304d4SRandall Stewart 	    SCTP_M_MAP);
1318f8829a4aSRandall Stewart 	if (asoc->mapping_array == NULL) {
1319207304d4SRandall Stewart 		SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1320c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1321f8829a4aSRandall Stewart 		return (ENOMEM);
1322f8829a4aSRandall Stewart 	}
1323f8829a4aSRandall Stewart 	memset(asoc->mapping_array, 0, asoc->mapping_array_size);
1324b5c16493SMichael Tuexen 	SCTP_MALLOC(asoc->nr_mapping_array, uint8_t *, asoc->mapping_array_size,
1325830d754dSRandall Stewart 	    SCTP_M_MAP);
1326bf1be571SRandall Stewart 	if (asoc->nr_mapping_array == NULL) {
1327bf1be571SRandall Stewart 		SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1328bf1be571SRandall Stewart 		SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1329bf1be571SRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1330bf1be571SRandall Stewart 		return (ENOMEM);
1331bf1be571SRandall Stewart 	}
1332b5c16493SMichael Tuexen 	memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
1333830d754dSRandall Stewart 
1334f8829a4aSRandall Stewart 	/* Now the init of the other outqueues */
1335f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->free_chunks);
1336f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->control_send_queue);
1337c54a18d2SRandall Stewart 	TAILQ_INIT(&asoc->asconf_send_queue);
1338f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->send_queue);
1339f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->sent_queue);
1340f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->resetHead);
1341a1cb341bSMichael Tuexen 	asoc->max_inbound_streams = inp->sctp_ep.max_open_streams_intome;
1342f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->asconf_queue);
1343f8829a4aSRandall Stewart 	/* authentication fields */
1344f8829a4aSRandall Stewart 	asoc->authinfo.random = NULL;
1345830d754dSRandall Stewart 	asoc->authinfo.active_keyid = 0;
1346f8829a4aSRandall Stewart 	asoc->authinfo.assoc_key = NULL;
1347f8829a4aSRandall Stewart 	asoc->authinfo.assoc_keyid = 0;
1348f8829a4aSRandall Stewart 	asoc->authinfo.recv_key = NULL;
1349f8829a4aSRandall Stewart 	asoc->authinfo.recv_keyid = 0;
1350f8829a4aSRandall Stewart 	LIST_INIT(&asoc->shared_keys);
1351f42a358aSRandall Stewart 	asoc->marked_retrans = 0;
1352a1cb341bSMichael Tuexen 	asoc->port = inp->sctp_ep.port;
1353f42a358aSRandall Stewart 	asoc->timoinit = 0;
1354f42a358aSRandall Stewart 	asoc->timodata = 0;
1355f42a358aSRandall Stewart 	asoc->timosack = 0;
1356f42a358aSRandall Stewart 	asoc->timoshutdown = 0;
1357f42a358aSRandall Stewart 	asoc->timoheartbeat = 0;
1358f42a358aSRandall Stewart 	asoc->timocookie = 0;
1359f42a358aSRandall Stewart 	asoc->timoshutdownack = 0;
13606e55db54SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&asoc->start_time);
13616e55db54SRandall Stewart 	asoc->discontinuity_time = asoc->start_time;
1362f0396ad1SMichael Tuexen 	for (i = 0; i < SCTP_PR_SCTP_MAX + 1; i++) {
1363f0396ad1SMichael Tuexen 		asoc->abandoned_unsent[i] = 0;
1364f0396ad1SMichael Tuexen 		asoc->abandoned_sent[i] = 0;
1365f0396ad1SMichael Tuexen 	}
1366eacc51c5SRandall Stewart 	/*
1367eacc51c5SRandall Stewart 	 * sa_ignore MEMLEAK {memory is put in the assoc mapping array and
136877acdc25SRandall Stewart 	 * freed later when the association is freed.
1369eacc51c5SRandall Stewart 	 */
1370f8829a4aSRandall Stewart 	return (0);
1371f8829a4aSRandall Stewart }
1372f8829a4aSRandall Stewart 
13730e13104dSRandall Stewart void
13740e13104dSRandall Stewart sctp_print_mapping_array(struct sctp_association *asoc)
13750e13104dSRandall Stewart {
1376aed5947cSMichael Tuexen 	unsigned int i, limit;
13770e13104dSRandall Stewart 
1378cd3fd531SMichael Tuexen 	SCTP_PRINTF("Mapping array size: %d, baseTSN: %8.8x, cumAck: %8.8x, highestTSN: (%8.8x, %8.8x).\n",
13790e13104dSRandall Stewart 	    asoc->mapping_array_size,
13800e13104dSRandall Stewart 	    asoc->mapping_array_base_tsn,
13810e13104dSRandall Stewart 	    asoc->cumulative_tsn,
1382aed5947cSMichael Tuexen 	    asoc->highest_tsn_inside_map,
1383aed5947cSMichael Tuexen 	    asoc->highest_tsn_inside_nr_map);
1384aed5947cSMichael Tuexen 	for (limit = asoc->mapping_array_size; limit > 1; limit--) {
138560990c0cSMichael Tuexen 		if (asoc->mapping_array[limit - 1] != 0) {
138677acdc25SRandall Stewart 			break;
138777acdc25SRandall Stewart 		}
138877acdc25SRandall Stewart 	}
1389cd3fd531SMichael Tuexen 	SCTP_PRINTF("Renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
139077acdc25SRandall Stewart 	for (i = 0; i < limit; i++) {
1391cd3fd531SMichael Tuexen 		SCTP_PRINTF("%2.2x%c", asoc->mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
139277acdc25SRandall Stewart 	}
1393aed5947cSMichael Tuexen 	if (limit % 16)
1394cd3fd531SMichael Tuexen 		SCTP_PRINTF("\n");
1395aed5947cSMichael Tuexen 	for (limit = asoc->mapping_array_size; limit > 1; limit--) {
1396aed5947cSMichael Tuexen 		if (asoc->nr_mapping_array[limit - 1]) {
139777acdc25SRandall Stewart 			break;
139877acdc25SRandall Stewart 		}
139977acdc25SRandall Stewart 	}
1400cd3fd531SMichael Tuexen 	SCTP_PRINTF("Non renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
140177acdc25SRandall Stewart 	for (i = 0; i < limit; i++) {
1402cd3fd531SMichael Tuexen 		SCTP_PRINTF("%2.2x%c", asoc->nr_mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
14030e13104dSRandall Stewart 	}
1404aed5947cSMichael Tuexen 	if (limit % 16)
1405cd3fd531SMichael Tuexen 		SCTP_PRINTF("\n");
14060e13104dSRandall Stewart }
14070e13104dSRandall Stewart 
1408f8829a4aSRandall Stewart int
14090696e120SRandall Stewart sctp_expand_mapping_array(struct sctp_association *asoc, uint32_t needed)
1410f8829a4aSRandall Stewart {
1411f8829a4aSRandall Stewart 	/* mapping array needs to grow */
1412b5c16493SMichael Tuexen 	uint8_t *new_array1, *new_array2;
14130696e120SRandall Stewart 	uint32_t new_size;
1414f8829a4aSRandall Stewart 
14150696e120SRandall Stewart 	new_size = asoc->mapping_array_size + ((needed + 7) / 8 + SCTP_MAPPING_ARRAY_INCR);
1416b5c16493SMichael Tuexen 	SCTP_MALLOC(new_array1, uint8_t *, new_size, SCTP_M_MAP);
1417b5c16493SMichael Tuexen 	SCTP_MALLOC(new_array2, uint8_t *, new_size, SCTP_M_MAP);
1418b5c16493SMichael Tuexen 	if ((new_array1 == NULL) || (new_array2 == NULL)) {
1419f8829a4aSRandall Stewart 		/* can't get more, forget it */
1420b5c16493SMichael Tuexen 		SCTP_PRINTF("No memory for expansion of SCTP mapping array %d\n", new_size);
1421b5c16493SMichael Tuexen 		if (new_array1) {
1422b5c16493SMichael Tuexen 			SCTP_FREE(new_array1, SCTP_M_MAP);
1423b5c16493SMichael Tuexen 		}
1424b5c16493SMichael Tuexen 		if (new_array2) {
1425b5c16493SMichael Tuexen 			SCTP_FREE(new_array2, SCTP_M_MAP);
1426b5c16493SMichael Tuexen 		}
1427f8829a4aSRandall Stewart 		return (-1);
1428f8829a4aSRandall Stewart 	}
1429b5c16493SMichael Tuexen 	memset(new_array1, 0, new_size);
1430b5c16493SMichael Tuexen 	memset(new_array2, 0, new_size);
1431b5c16493SMichael Tuexen 	memcpy(new_array1, asoc->mapping_array, asoc->mapping_array_size);
1432b5c16493SMichael Tuexen 	memcpy(new_array2, asoc->nr_mapping_array, asoc->mapping_array_size);
1433207304d4SRandall Stewart 	SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1434830d754dSRandall Stewart 	SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
1435b5c16493SMichael Tuexen 	asoc->mapping_array = new_array1;
1436b5c16493SMichael Tuexen 	asoc->nr_mapping_array = new_array2;
1437b5c16493SMichael Tuexen 	asoc->mapping_array_size = new_size;
1438830d754dSRandall Stewart 	return (0);
1439830d754dSRandall Stewart }
1440830d754dSRandall Stewart 
144142551e99SRandall Stewart static void
144242551e99SRandall Stewart sctp_iterator_work(struct sctp_iterator *it)
144342551e99SRandall Stewart {
1444868b51f2SMichael Tuexen 	struct epoch_tracker et;
1445868b51f2SMichael Tuexen 	struct sctp_inpcb *tinp;
144642551e99SRandall Stewart 	int iteration_count = 0;
144742551e99SRandall Stewart 	int inp_skip = 0;
1448ec4c19fcSRandall Stewart 	int first_in = 1;
144942551e99SRandall Stewart 
1450868b51f2SMichael Tuexen 	NET_EPOCH_ENTER(et);
1451ec4c19fcSRandall Stewart 	SCTP_INP_INFO_RLOCK();
145242551e99SRandall Stewart 	SCTP_ITERATOR_LOCK();
1453dcb436c9SMichael Tuexen 	sctp_it_ctl.cur_it = it;
1454ad81507eSRandall Stewart 	if (it->inp) {
1455ec4c19fcSRandall Stewart 		SCTP_INP_RLOCK(it->inp);
145642551e99SRandall Stewart 		SCTP_INP_DECR_REF(it->inp);
1457ad81507eSRandall Stewart 	}
145842551e99SRandall Stewart 	if (it->inp == NULL) {
145942551e99SRandall Stewart 		/* iterator is complete */
146042551e99SRandall Stewart done_with_iterator:
1461dcb436c9SMichael Tuexen 		sctp_it_ctl.cur_it = NULL;
146242551e99SRandall Stewart 		SCTP_ITERATOR_UNLOCK();
1463ec4c19fcSRandall Stewart 		SCTP_INP_INFO_RUNLOCK();
146442551e99SRandall Stewart 		if (it->function_atend != NULL) {
146542551e99SRandall Stewart 			(*it->function_atend) (it->pointer, it->val);
146642551e99SRandall Stewart 		}
1467207304d4SRandall Stewart 		SCTP_FREE(it, SCTP_M_ITER);
1468868b51f2SMichael Tuexen 		NET_EPOCH_EXIT(et);
146942551e99SRandall Stewart 		return;
147042551e99SRandall Stewart 	}
147142551e99SRandall Stewart select_a_new_ep:
1472ec4c19fcSRandall Stewart 	if (first_in) {
1473ec4c19fcSRandall Stewart 		first_in = 0;
1474ec4c19fcSRandall Stewart 	} else {
1475f7517433SRandall Stewart 		SCTP_INP_RLOCK(it->inp);
1476ec4c19fcSRandall Stewart 	}
147742551e99SRandall Stewart 	while (((it->pcb_flags) &&
147842551e99SRandall Stewart 	    ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) ||
147942551e99SRandall Stewart 	    ((it->pcb_features) &&
148042551e99SRandall Stewart 	    ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) {
148142551e99SRandall Stewart 		/* endpoint flags or features don't match, so keep looking */
148242551e99SRandall Stewart 		if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1483f7517433SRandall Stewart 			SCTP_INP_RUNLOCK(it->inp);
148442551e99SRandall Stewart 			goto done_with_iterator;
148542551e99SRandall Stewart 		}
1486ec4c19fcSRandall Stewart 		tinp = it->inp;
148742551e99SRandall Stewart 		it->inp = LIST_NEXT(it->inp, sctp_list);
148883ed5080SMichael Tuexen 		it->stcb = NULL;
1489ec4c19fcSRandall Stewart 		SCTP_INP_RUNLOCK(tinp);
149042551e99SRandall Stewart 		if (it->inp == NULL) {
149142551e99SRandall Stewart 			goto done_with_iterator;
149242551e99SRandall Stewart 		}
149342551e99SRandall Stewart 		SCTP_INP_RLOCK(it->inp);
1494f7517433SRandall Stewart 	}
149542551e99SRandall Stewart 	/* now go through each assoc which is in the desired state */
149642551e99SRandall Stewart 	if (it->done_current_ep == 0) {
149742551e99SRandall Stewart 		if (it->function_inp != NULL)
149842551e99SRandall Stewart 			inp_skip = (*it->function_inp) (it->inp, it->pointer, it->val);
149942551e99SRandall Stewart 		it->done_current_ep = 1;
150042551e99SRandall Stewart 	}
150142551e99SRandall Stewart 	if (it->stcb == NULL) {
150242551e99SRandall Stewart 		/* run the per instance function */
150342551e99SRandall Stewart 		it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
150442551e99SRandall Stewart 	}
150542551e99SRandall Stewart 	if ((inp_skip) || it->stcb == NULL) {
150642551e99SRandall Stewart 		if (it->function_inp_end != NULL) {
150742551e99SRandall Stewart 			inp_skip = (*it->function_inp_end) (it->inp,
150842551e99SRandall Stewart 			    it->pointer,
150942551e99SRandall Stewart 			    it->val);
151042551e99SRandall Stewart 		}
151142551e99SRandall Stewart 		SCTP_INP_RUNLOCK(it->inp);
151242551e99SRandall Stewart 		goto no_stcb;
151342551e99SRandall Stewart 	}
151442551e99SRandall Stewart 	while (it->stcb) {
151542551e99SRandall Stewart 		SCTP_TCB_LOCK(it->stcb);
151642551e99SRandall Stewart 		if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
151742551e99SRandall Stewart 			/* not in the right state... keep looking */
151842551e99SRandall Stewart 			SCTP_TCB_UNLOCK(it->stcb);
151942551e99SRandall Stewart 			goto next_assoc;
152042551e99SRandall Stewart 		}
152142551e99SRandall Stewart 		/* see if we have limited out the iterator loop */
152242551e99SRandall Stewart 		iteration_count++;
152342551e99SRandall Stewart 		if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) {
152442551e99SRandall Stewart 			/* Pause to let others grab the lock */
152542551e99SRandall Stewart 			atomic_add_int(&it->stcb->asoc.refcnt, 1);
152642551e99SRandall Stewart 			SCTP_TCB_UNLOCK(it->stcb);
1527c4739e2fSRandall Stewart 			SCTP_INP_INCR_REF(it->inp);
152842551e99SRandall Stewart 			SCTP_INP_RUNLOCK(it->inp);
152942551e99SRandall Stewart 			SCTP_ITERATOR_UNLOCK();
1530ec4c19fcSRandall Stewart 			SCTP_INP_INFO_RUNLOCK();
1531ec4c19fcSRandall Stewart 			SCTP_INP_INFO_RLOCK();
153242551e99SRandall Stewart 			SCTP_ITERATOR_LOCK();
1533f7517433SRandall Stewart 			if (sctp_it_ctl.iterator_flags) {
1534f7517433SRandall Stewart 				/* We won't be staying here */
1535f7517433SRandall Stewart 				SCTP_INP_DECR_REF(it->inp);
1536f7517433SRandall Stewart 				atomic_add_int(&it->stcb->asoc.refcnt, -1);
1537f7517433SRandall Stewart 				if (sctp_it_ctl.iterator_flags &
1538f7517433SRandall Stewart 				    SCTP_ITERATOR_STOP_CUR_IT) {
1539f7517433SRandall Stewart 					sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_IT;
1540f7517433SRandall Stewart 					goto done_with_iterator;
1541f7517433SRandall Stewart 				}
1542f7517433SRandall Stewart 				if (sctp_it_ctl.iterator_flags &
1543f7517433SRandall Stewart 				    SCTP_ITERATOR_STOP_CUR_INP) {
1544f7517433SRandall Stewart 					sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_INP;
1545f7517433SRandall Stewart 					goto no_stcb;
1546f7517433SRandall Stewart 				}
1547f7517433SRandall Stewart 				/* If we reach here huh? */
1548cd3fd531SMichael Tuexen 				SCTP_PRINTF("Unknown it ctl flag %x\n",
1549f7517433SRandall Stewart 				    sctp_it_ctl.iterator_flags);
1550f7517433SRandall Stewart 				sctp_it_ctl.iterator_flags = 0;
1551f7517433SRandall Stewart 			}
155242551e99SRandall Stewart 			SCTP_INP_RLOCK(it->inp);
1553c4739e2fSRandall Stewart 			SCTP_INP_DECR_REF(it->inp);
155442551e99SRandall Stewart 			SCTP_TCB_LOCK(it->stcb);
155542551e99SRandall Stewart 			atomic_add_int(&it->stcb->asoc.refcnt, -1);
155642551e99SRandall Stewart 			iteration_count = 0;
155742551e99SRandall Stewart 		}
155883ed5080SMichael Tuexen 		KASSERT(it->inp == it->stcb->sctp_ep,
155983ed5080SMichael Tuexen 		    ("%s: stcb %p does not belong to inp %p, but inp %p",
156083ed5080SMichael Tuexen 		    __func__, it->stcb, it->inp, it->stcb->sctp_ep));
15610053ed28SMichael Tuexen 
156242551e99SRandall Stewart 		/* run function on this one */
156342551e99SRandall Stewart 		(*it->function_assoc) (it->inp, it->stcb, it->pointer, it->val);
156442551e99SRandall Stewart 
156542551e99SRandall Stewart 		/*
156642551e99SRandall Stewart 		 * we lie here, it really needs to have its own type but
156742551e99SRandall Stewart 		 * first I must verify that this won't effect things :-0
156842551e99SRandall Stewart 		 */
156942551e99SRandall Stewart 		if (it->no_chunk_output == 0)
1570ceaad40aSRandall Stewart 			sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
157142551e99SRandall Stewart 
157242551e99SRandall Stewart 		SCTP_TCB_UNLOCK(it->stcb);
157342551e99SRandall Stewart next_assoc:
157442551e99SRandall Stewart 		it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
157542551e99SRandall Stewart 		if (it->stcb == NULL) {
157642551e99SRandall Stewart 			/* Run last function */
157742551e99SRandall Stewart 			if (it->function_inp_end != NULL) {
157842551e99SRandall Stewart 				inp_skip = (*it->function_inp_end) (it->inp,
157942551e99SRandall Stewart 				    it->pointer,
158042551e99SRandall Stewart 				    it->val);
158142551e99SRandall Stewart 			}
158242551e99SRandall Stewart 		}
158342551e99SRandall Stewart 	}
158442551e99SRandall Stewart 	SCTP_INP_RUNLOCK(it->inp);
158542551e99SRandall Stewart no_stcb:
158642551e99SRandall Stewart 	/* done with all assocs on this endpoint, move on to next endpoint */
158742551e99SRandall Stewart 	it->done_current_ep = 0;
158842551e99SRandall Stewart 	if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
158942551e99SRandall Stewart 		it->inp = NULL;
159042551e99SRandall Stewart 	} else {
159142551e99SRandall Stewart 		it->inp = LIST_NEXT(it->inp, sctp_list);
159242551e99SRandall Stewart 	}
159383ed5080SMichael Tuexen 	it->stcb = NULL;
159442551e99SRandall Stewart 	if (it->inp == NULL) {
159542551e99SRandall Stewart 		goto done_with_iterator;
159642551e99SRandall Stewart 	}
159742551e99SRandall Stewart 	goto select_a_new_ep;
159842551e99SRandall Stewart }
159942551e99SRandall Stewart 
160042551e99SRandall Stewart void
160142551e99SRandall Stewart sctp_iterator_worker(void)
160242551e99SRandall Stewart {
1603397b1c94SMichael Tuexen 	struct sctp_iterator *it;
160442551e99SRandall Stewart 
160542551e99SRandall Stewart 	/* This function is called with the WQ lock in place */
1606f7517433SRandall Stewart 	sctp_it_ctl.iterator_running = 1;
1607397b1c94SMichael Tuexen 	while ((it = TAILQ_FIRST(&sctp_it_ctl.iteratorhead)) != NULL) {
160842551e99SRandall Stewart 		/* now lets work on this one */
1609f7517433SRandall Stewart 		TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
161042551e99SRandall Stewart 		SCTP_IPI_ITERATOR_WQ_UNLOCK();
1611f7517433SRandall Stewart 		CURVNET_SET(it->vn);
161242551e99SRandall Stewart 		sctp_iterator_work(it);
1613f7517433SRandall Stewart 		CURVNET_RESTORE();
161442551e99SRandall Stewart 		SCTP_IPI_ITERATOR_WQ_LOCK();
16153c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
161642551e99SRandall Stewart 	}
1617f7517433SRandall Stewart 	sctp_it_ctl.iterator_running = 0;
161842551e99SRandall Stewart 	return;
161942551e99SRandall Stewart }
162042551e99SRandall Stewart 
1621f8829a4aSRandall Stewart static void
1622f8829a4aSRandall Stewart sctp_handle_addr_wq(void)
1623f8829a4aSRandall Stewart {
1624f8829a4aSRandall Stewart 	/* deal with the ADDR wq from the rtsock calls */
16254a9ef3f8SMichael Tuexen 	struct sctp_laddr *wi, *nwi;
162642551e99SRandall Stewart 	struct sctp_asconf_iterator *asc;
1627f8829a4aSRandall Stewart 
162842551e99SRandall Stewart 	SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
1629207304d4SRandall Stewart 	    sizeof(struct sctp_asconf_iterator), SCTP_M_ASC_IT);
163042551e99SRandall Stewart 	if (asc == NULL) {
163142551e99SRandall Stewart 		/* Try later, no memory */
1632f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
1633f8829a4aSRandall Stewart 		    (struct sctp_inpcb *)NULL,
1634f8829a4aSRandall Stewart 		    (struct sctp_tcb *)NULL,
1635f8829a4aSRandall Stewart 		    (struct sctp_nets *)NULL);
163642551e99SRandall Stewart 		return;
1637f8829a4aSRandall Stewart 	}
163842551e99SRandall Stewart 	LIST_INIT(&asc->list_of_work);
163942551e99SRandall Stewart 	asc->cnt = 0;
1640f7517433SRandall Stewart 
16414a9ef3f8SMichael Tuexen 	LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
164242551e99SRandall Stewart 		LIST_REMOVE(wi, sctp_nxt_addr);
164342551e99SRandall Stewart 		LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
164442551e99SRandall Stewart 		asc->cnt++;
1645f8829a4aSRandall Stewart 	}
1646f7517433SRandall Stewart 
164742551e99SRandall Stewart 	if (asc->cnt == 0) {
1648207304d4SRandall Stewart 		SCTP_FREE(asc, SCTP_M_ASC_IT);
164942551e99SRandall Stewart 	} else {
16502b1c7de4SMichael Tuexen 		int ret;
16512b1c7de4SMichael Tuexen 
16522b1c7de4SMichael Tuexen 		ret = sctp_initiate_iterator(sctp_asconf_iterator_ep,
16531b649582SRandall Stewart 		    sctp_asconf_iterator_stcb,
165442551e99SRandall Stewart 		    NULL,	/* No ep end for boundall */
165542551e99SRandall Stewart 		    SCTP_PCB_FLAGS_BOUNDALL,
165642551e99SRandall Stewart 		    SCTP_PCB_ANY_FEATURES,
16571b649582SRandall Stewart 		    SCTP_ASOC_ANY_STATE,
16581b649582SRandall Stewart 		    (void *)asc, 0,
16591b649582SRandall Stewart 		    sctp_asconf_iterator_end, NULL, 0);
16602b1c7de4SMichael Tuexen 		if (ret) {
16612b1c7de4SMichael Tuexen 			SCTP_PRINTF("Failed to initiate iterator for handle_addr_wq\n");
1662b7b84c0eSMichael Tuexen 			/*
1663b7b84c0eSMichael Tuexen 			 * Freeing if we are stopping or put back on the
1664b7b84c0eSMichael Tuexen 			 * addr_wq.
1665b7b84c0eSMichael Tuexen 			 */
16662b1c7de4SMichael Tuexen 			if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
16672b1c7de4SMichael Tuexen 				sctp_asconf_iterator_end(asc, 0);
16682b1c7de4SMichael Tuexen 			} else {
16692b1c7de4SMichael Tuexen 				LIST_FOREACH(wi, &asc->list_of_work, sctp_nxt_addr) {
16702b1c7de4SMichael Tuexen 					LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
16712b1c7de4SMichael Tuexen 				}
16722b1c7de4SMichael Tuexen 				SCTP_FREE(asc, SCTP_M_ASC_IT);
16732b1c7de4SMichael Tuexen 			}
16742b1c7de4SMichael Tuexen 		}
167542551e99SRandall Stewart 	}
1676f8829a4aSRandall Stewart }
1677f8829a4aSRandall Stewart 
1678a412576eSMichael Tuexen /*-
1679a412576eSMichael Tuexen  * The following table shows which pointers for the inp, stcb, or net are
1680a412576eSMichael Tuexen  * stored for each timer after it was started.
1681a412576eSMichael Tuexen  *
1682a412576eSMichael Tuexen  *|Name                         |Timer                        |inp |stcb|net |
1683a412576eSMichael Tuexen  *|-----------------------------|-----------------------------|----|----|----|
1684a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_SEND         |net->rxt_timer               |Yes |Yes |Yes |
1685a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_INIT         |net->rxt_timer               |Yes |Yes |Yes |
1686a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_RECV         |stcb->asoc.dack_timer        |Yes |Yes |No  |
1687a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_SHUTDOWN     |net->rxt_timer               |Yes |Yes |Yes |
1688a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_HEARTBEAT    |net->hb_timer                |Yes |Yes |Yes |
1689a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_COOKIE       |net->rxt_timer               |Yes |Yes |Yes |
1690a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_NEWCOOKIE    |inp->sctp_ep.signature_change|Yes |No  |No  |
1691a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_PATHMTURAISE |net->pmtu_timer              |Yes |Yes |Yes |
1692a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_SHUTDOWNACK  |net->rxt_timer               |Yes |Yes |Yes |
1693a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_ASCONF       |stcb->asoc.asconf_timer      |Yes |Yes |Yes |
1694a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_SHUTDOWNGUARD|stcb->asoc.shut_guard_timer  |Yes |Yes |No  |
1695a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_AUTOCLOSE    |stcb->asoc.autoclose_timer   |Yes |Yes |No  |
1696a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_STRRESET     |stcb->asoc.strreset_timer    |Yes |Yes |No  |
1697a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_INPKILL      |inp->sctp_ep.signature_change|Yes |No  |No  |
1698a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_ASOCKILL     |stcb->asoc.strreset_timer    |Yes |Yes |No  |
1699a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_ADDR_WQ      |SCTP_BASE_INFO(addr_wq_timer)|No  |No  |No  |
1700a412576eSMichael Tuexen  *|SCTP_TIMER_TYPE_PRIM_DELETED |stcb->asoc.delete_prim_timer |Yes |Yes |No  |
1701a412576eSMichael Tuexen  */
1702a412576eSMichael Tuexen 
1703f8829a4aSRandall Stewart void
1704f8829a4aSRandall Stewart sctp_timeout_handler(void *t)
1705f8829a4aSRandall Stewart {
1706868b51f2SMichael Tuexen 	struct epoch_tracker et;
1707a412576eSMichael Tuexen 	struct timeval tv;
1708f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
1709f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
1710f8829a4aSRandall Stewart 	struct sctp_nets *net;
1711f8829a4aSRandall Stewart 	struct sctp_timer *tmr;
1712267dbe63SMichael Tuexen 	struct mbuf *op_err;
1713fa89f692SMichael Tuexen 	int type;
1714a412576eSMichael Tuexen 	int i, secret;
17158745f898SMichael Tuexen 	bool did_output, released_asoc_reference;
1716f8829a4aSRandall Stewart 
17178745f898SMichael Tuexen 	/*
17188745f898SMichael Tuexen 	 * If inp, stcb or net are not NULL, then references to these were
17198745f898SMichael Tuexen 	 * added when the timer was started, and must be released before
17208745f898SMichael Tuexen 	 * this function returns.
17218745f898SMichael Tuexen 	 */
1722f8829a4aSRandall Stewart 	tmr = (struct sctp_timer *)t;
1723f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)tmr->ep;
1724f8829a4aSRandall Stewart 	stcb = (struct sctp_tcb *)tmr->tcb;
1725f8829a4aSRandall Stewart 	net = (struct sctp_nets *)tmr->net;
17268518270eSMichael Tuexen 	CURVNET_SET((struct vnet *)tmr->vnet);
172704996cb7SMichael Tuexen 	NET_EPOCH_ENTER(et);
17288745f898SMichael Tuexen 	released_asoc_reference = false;
1729f8829a4aSRandall Stewart 
1730f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1731f8829a4aSRandall Stewart 	sctp_audit_log(0xF0, (uint8_t)tmr->type);
1732f8829a4aSRandall Stewart 	sctp_auditing(3, inp, stcb, net);
1733f8829a4aSRandall Stewart #endif
1734f8829a4aSRandall Stewart 
1735f8829a4aSRandall Stewart 	/* sanity checks... */
17362d87bacdSMichael Tuexen 	KASSERT(tmr->self == NULL || tmr->self == tmr,
1737f4cb790aSMichael Tuexen 	    ("sctp_timeout_handler: tmr->self corrupted"));
1738f4cb790aSMichael Tuexen 	KASSERT(SCTP_IS_TIMER_TYPE_VALID(tmr->type),
1739f4cb790aSMichael Tuexen 	    ("sctp_timeout_handler: invalid timer type %d", tmr->type));
1740a412576eSMichael Tuexen 	type = tmr->type;
1741f4cb790aSMichael Tuexen 	KASSERT(stcb == NULL || stcb->sctp_ep == inp,
1742f4cb790aSMichael Tuexen 	    ("sctp_timeout_handler of type %d: inp = %p, stcb->sctp_ep %p",
1743f4cb790aSMichael Tuexen 	    type, stcb, stcb->sctp_ep));
17447c63520cSMichael Tuexen 	tmr->stopped_from = 0xa001;
17458745f898SMichael Tuexen 	if ((stcb != NULL) && (stcb->asoc.state == SCTP_STATE_EMPTY)) {
1746a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
174723e3c088SMichael Tuexen 		    "Timer type %d handler exiting due to CLOSED association.\n",
1748a412576eSMichael Tuexen 		    type);
17498745f898SMichael Tuexen 		goto out_decr;
1750f8829a4aSRandall Stewart 	}
17517c63520cSMichael Tuexen 	tmr->stopped_from = 0xa002;
175237686ccfSMichael Tuexen 	SCTPDBG(SCTP_DEBUG_TIMER2, "Timer type %d goes off.\n", type);
1753139bc87fSRandall Stewart 	if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1754a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
175523e3c088SMichael Tuexen 		    "Timer type %d handler exiting due to not being active.\n",
1756a412576eSMichael Tuexen 		    type);
17578745f898SMichael Tuexen 		goto out_decr;
1758f8829a4aSRandall Stewart 	}
1759a5d547adSRandall Stewart 
17607c63520cSMichael Tuexen 	tmr->stopped_from = 0xa003;
1761f8829a4aSRandall Stewart 	if (stcb) {
1762f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
17638745f898SMichael Tuexen 		/*
17648745f898SMichael Tuexen 		 * Release reference so that association can be freed if
17658745f898SMichael Tuexen 		 * necessary below. This is safe now that we have acquired
17668745f898SMichael Tuexen 		 * the lock.
17678745f898SMichael Tuexen 		 */
176850cec919SRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, -1);
17698745f898SMichael Tuexen 		released_asoc_reference = true;
1770fa89f692SMichael Tuexen 		if ((type != SCTP_TIMER_TYPE_ASOCKILL) &&
17718745f898SMichael Tuexen 		    ((stcb->asoc.state == SCTP_STATE_EMPTY) ||
1772b54d3a6cSRandall Stewart 		    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED))) {
1773a412576eSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_TIMER2,
177423e3c088SMichael Tuexen 			    "Timer type %d handler exiting due to CLOSED association.\n",
1775a412576eSMichael Tuexen 			    type);
17768745f898SMichael Tuexen 			goto out;
1777b54d3a6cSRandall Stewart 		}
17782c62ba73SMichael Tuexen 	} else if (inp != NULL) {
17792c62ba73SMichael Tuexen 		SCTP_INP_WLOCK(inp);
17802c62ba73SMichael Tuexen 	} else {
17812c62ba73SMichael Tuexen 		SCTP_WQ_ADDR_LOCK();
1782f8829a4aSRandall Stewart 	}
178344b7479bSRandall Stewart 
178437686ccfSMichael Tuexen 	/* Record in stopped_from which timeout occurred. */
178537686ccfSMichael Tuexen 	tmr->stopped_from = type;
1786f8829a4aSRandall Stewart 	/* mark as being serviced now */
178744b7479bSRandall Stewart 	if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
178844b7479bSRandall Stewart 		/*
178944b7479bSRandall Stewart 		 * Callout has been rescheduled.
179044b7479bSRandall Stewart 		 */
17918745f898SMichael Tuexen 		goto out;
179244b7479bSRandall Stewart 	}
179344b7479bSRandall Stewart 	if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
179444b7479bSRandall Stewart 		/*
179544b7479bSRandall Stewart 		 * Not active, so no action.
179644b7479bSRandall Stewart 		 */
17978745f898SMichael Tuexen 		goto out;
179844b7479bSRandall Stewart 	}
1799139bc87fSRandall Stewart 	SCTP_OS_TIMER_DEACTIVATE(&tmr->timer);
1800f8829a4aSRandall Stewart 
1801f8829a4aSRandall Stewart 	/* call the handler for the appropriate timer type */
1802fa89f692SMichael Tuexen 	switch (type) {
1803f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SEND:
1804a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1805a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1806a412576eSMichael Tuexen 		    type, inp, stcb, net));
1807f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timodata);
1808f42a358aSRandall Stewart 		stcb->asoc.timodata++;
1809f8829a4aSRandall Stewart 		stcb->asoc.num_send_timers_up--;
1810f8829a4aSRandall Stewart 		if (stcb->asoc.num_send_timers_up < 0) {
1811f8829a4aSRandall Stewart 			stcb->asoc.num_send_timers_up = 0;
1812f8829a4aSRandall Stewart 		}
1813b54d3a6cSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
181460990c0cSMichael Tuexen 		if (sctp_t3rxt_timer(inp, stcb, net)) {
1815f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1816f8829a4aSRandall Stewart 
1817f8829a4aSRandall Stewart 			goto out_decr;
1818f8829a4aSRandall Stewart 		}
1819b54d3a6cSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1820f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1821f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1822f8829a4aSRandall Stewart #endif
1823ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
18248745f898SMichael Tuexen 		did_output = true;
1825f8829a4aSRandall Stewart 		if ((stcb->asoc.num_send_timers_up == 0) &&
18264a9ef3f8SMichael Tuexen 		    (stcb->asoc.sent_queue_cnt > 0)) {
1827f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *chk;
1828f8829a4aSRandall Stewart 
1829f8829a4aSRandall Stewart 			/*
1830efd5e692SMichael Tuexen 			 * Safeguard. If there on some on the sent queue
1831f8829a4aSRandall Stewart 			 * somewhere but no timers running something is
1832f8829a4aSRandall Stewart 			 * wrong... so we start a timer on the first chunk
1833f8829a4aSRandall Stewart 			 * on the send queue on whatever net it is sent to.
1834f8829a4aSRandall Stewart 			 */
1835efd5e692SMichael Tuexen 			TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1836efd5e692SMichael Tuexen 				if (chk->whoTo != NULL) {
1837efd5e692SMichael Tuexen 					break;
1838efd5e692SMichael Tuexen 				}
1839efd5e692SMichael Tuexen 			}
1840efd5e692SMichael Tuexen 			if (chk != NULL) {
1841efd5e692SMichael Tuexen 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
1842efd5e692SMichael Tuexen 			}
1843f8829a4aSRandall Stewart 		}
1844f8829a4aSRandall Stewart 		break;
1845f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
1846a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1847a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1848a412576eSMichael Tuexen 		    type, inp, stcb, net));
1849f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoinit);
1850f42a358aSRandall Stewart 		stcb->asoc.timoinit++;
1851f8829a4aSRandall Stewart 		if (sctp_t1init_timer(inp, stcb, net)) {
1852f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1853f8829a4aSRandall Stewart 			goto out_decr;
1854f8829a4aSRandall Stewart 		}
18558745f898SMichael Tuexen 		did_output = false;
1856f8829a4aSRandall Stewart 		break;
1857f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
1858a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net == NULL,
1859a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1860a412576eSMichael Tuexen 		    type, inp, stcb, net));
1861f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timosack);
1862f42a358aSRandall Stewart 		stcb->asoc.timosack++;
1863689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
1864f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1865a412576eSMichael Tuexen 		sctp_auditing(4, inp, stcb, NULL);
1866f8829a4aSRandall Stewart #endif
1867ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SACK_TMR, SCTP_SO_NOT_LOCKED);
18688745f898SMichael Tuexen 		did_output = true;
1869f8829a4aSRandall Stewart 		break;
1870f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
1871a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1872a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1873a412576eSMichael Tuexen 		    type, inp, stcb, net));
1874a412576eSMichael Tuexen 		SCTP_STAT_INCR(sctps_timoshutdown);
1875a412576eSMichael Tuexen 		stcb->asoc.timoshutdown++;
1876f8829a4aSRandall Stewart 		if (sctp_shutdown_timer(inp, stcb, net)) {
1877f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1878f8829a4aSRandall Stewart 			goto out_decr;
1879f8829a4aSRandall Stewart 		}
1880f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1881f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1882f8829a4aSRandall Stewart #endif
1883ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_NOT_LOCKED);
18848745f898SMichael Tuexen 		did_output = true;
1885f8829a4aSRandall Stewart 		break;
1886f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
1887a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1888a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1889a412576eSMichael Tuexen 		    type, inp, stcb, net));
1890f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoheartbeat);
1891f42a358aSRandall Stewart 		stcb->asoc.timoheartbeat++;
1892ca85e948SMichael Tuexen 		if (sctp_heartbeat_timer(inp, stcb, net)) {
1893f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1894f8829a4aSRandall Stewart 			goto out_decr;
1895f8829a4aSRandall Stewart 		}
1896f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1897ca85e948SMichael Tuexen 		sctp_auditing(4, inp, stcb, net);
1898f8829a4aSRandall Stewart #endif
1899ca85e948SMichael Tuexen 		if (!(net->dest_state & SCTP_ADDR_NOHB)) {
1900629749b6SMichael Tuexen 			sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1901ceaad40aSRandall Stewart 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_HB_TMR, SCTP_SO_NOT_LOCKED);
19028745f898SMichael Tuexen 			did_output = true;
19038745f898SMichael Tuexen 		} else {
19048745f898SMichael Tuexen 			did_output = false;
1905f8829a4aSRandall Stewart 		}
1906f8829a4aSRandall Stewart 		break;
1907f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
1908a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1909a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1910a412576eSMichael Tuexen 		    type, inp, stcb, net));
1911a412576eSMichael Tuexen 		SCTP_STAT_INCR(sctps_timocookie);
1912a412576eSMichael Tuexen 		stcb->asoc.timocookie++;
1913f8829a4aSRandall Stewart 		if (sctp_cookie_timer(inp, stcb, net)) {
1914f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1915f8829a4aSRandall Stewart 			goto out_decr;
1916f8829a4aSRandall Stewart 		}
1917f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1918f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1919f8829a4aSRandall Stewart #endif
1920f8829a4aSRandall Stewart 		/*
1921f8829a4aSRandall Stewart 		 * We consider T3 and Cookie timer pretty much the same with
1922f8829a4aSRandall Stewart 		 * respect to where from in chunk_output.
1923f8829a4aSRandall Stewart 		 */
1924ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
19258745f898SMichael Tuexen 		did_output = true;
1926f8829a4aSRandall Stewart 		break;
1927f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_NEWCOOKIE:
1928a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb == NULL && net == NULL,
1929a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1930a412576eSMichael Tuexen 		    type, inp, stcb, net));
1931f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timosecret);
19326e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&tv);
1933f8829a4aSRandall Stewart 		inp->sctp_ep.time_of_secret_change = tv.tv_sec;
1934f8829a4aSRandall Stewart 		inp->sctp_ep.last_secret_number =
1935f8829a4aSRandall Stewart 		    inp->sctp_ep.current_secret_number;
1936f8829a4aSRandall Stewart 		inp->sctp_ep.current_secret_number++;
1937f8829a4aSRandall Stewart 		if (inp->sctp_ep.current_secret_number >=
1938f8829a4aSRandall Stewart 		    SCTP_HOW_MANY_SECRETS) {
1939f8829a4aSRandall Stewart 			inp->sctp_ep.current_secret_number = 0;
1940f8829a4aSRandall Stewart 		}
1941f8829a4aSRandall Stewart 		secret = (int)inp->sctp_ep.current_secret_number;
1942f8829a4aSRandall Stewart 		for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1943f8829a4aSRandall Stewart 			inp->sctp_ep.secret_key[secret][i] =
1944f8829a4aSRandall Stewart 			    sctp_select_initial_TSN(&inp->sctp_ep);
1945f8829a4aSRandall Stewart 		}
19466fb7b4fbSMichael Tuexen 		sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
19478745f898SMichael Tuexen 		did_output = false;
1948f8829a4aSRandall Stewart 		break;
1949f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
1950a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1951a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1952a412576eSMichael Tuexen 		    type, inp, stcb, net));
1953f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timopathmtu);
1954f8829a4aSRandall Stewart 		sctp_pathmtu_timer(inp, stcb, net);
19558745f898SMichael Tuexen 		did_output = false;
1956f8829a4aSRandall Stewart 		break;
1957f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
1958a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1959a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1960a412576eSMichael Tuexen 		    type, inp, stcb, net));
1961f8829a4aSRandall Stewart 		if (sctp_shutdownack_timer(inp, stcb, net)) {
1962f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1963f8829a4aSRandall Stewart 			goto out_decr;
1964f8829a4aSRandall Stewart 		}
1965f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoshutdownack);
1966f42a358aSRandall Stewart 		stcb->asoc.timoshutdownack++;
1967f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1968f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1969f8829a4aSRandall Stewart #endif
1970ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_ACK_TMR, SCTP_SO_NOT_LOCKED);
19718745f898SMichael Tuexen 		did_output = true;
1972f8829a4aSRandall Stewart 		break;
1973f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASCONF:
1974a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net != NULL,
1975a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1976a412576eSMichael Tuexen 		    type, inp, stcb, net));
1977a412576eSMichael Tuexen 		SCTP_STAT_INCR(sctps_timoasconf);
1978f8829a4aSRandall Stewart 		if (sctp_asconf_timer(inp, stcb, net)) {
1979f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1980f8829a4aSRandall Stewart 			goto out_decr;
1981f8829a4aSRandall Stewart 		}
1982f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1983f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1984f8829a4aSRandall Stewart #endif
1985ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_ASCONF_TMR, SCTP_SO_NOT_LOCKED);
19868745f898SMichael Tuexen 		did_output = true;
1987f8829a4aSRandall Stewart 		break;
19880554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
1989a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net == NULL,
1990a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1991a412576eSMichael Tuexen 		    type, inp, stcb, net));
19920554e01dSMichael Tuexen 		SCTP_STAT_INCR(sctps_timoshutdownguard);
19930554e01dSMichael Tuexen 		op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
19940554e01dSMichael Tuexen 		    "Shutdown guard timer expired");
19950554e01dSMichael Tuexen 		sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
19960554e01dSMichael Tuexen 		/* no need to unlock on tcb its gone */
19970554e01dSMichael Tuexen 		goto out_decr;
1998f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_AUTOCLOSE:
1999a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net == NULL,
2000a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2001a412576eSMichael Tuexen 		    type, inp, stcb, net));
2002f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoautoclose);
2003a57fb68bSMichael Tuexen 		sctp_autoclose_timer(inp, stcb);
2004ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
20058745f898SMichael Tuexen 		did_output = true;
2006f8829a4aSRandall Stewart 		break;
20070554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_STRRESET:
2008a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net == NULL,
2009a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2010a412576eSMichael Tuexen 		    type, inp, stcb, net));
2011a412576eSMichael Tuexen 		SCTP_STAT_INCR(sctps_timostrmrst);
2012e95b3d7fSMichael Tuexen 		if (sctp_strreset_timer(inp, stcb)) {
20130554e01dSMichael Tuexen 			/* no need to unlock on tcb its gone */
20140554e01dSMichael Tuexen 			goto out_decr;
20150554e01dSMichael Tuexen 		}
20160554e01dSMichael Tuexen 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_TMR, SCTP_SO_NOT_LOCKED);
20178745f898SMichael Tuexen 		did_output = true;
20180554e01dSMichael Tuexen 		break;
20190554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_INPKILL:
2020a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb == NULL && net == NULL,
2021a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2022a412576eSMichael Tuexen 		    type, inp, stcb, net));
20230554e01dSMichael Tuexen 		SCTP_STAT_INCR(sctps_timoinpkill);
20240554e01dSMichael Tuexen 		/*
20250554e01dSMichael Tuexen 		 * special case, take away our increment since WE are the
20260554e01dSMichael Tuexen 		 * killer
20270554e01dSMichael Tuexen 		 */
20280554e01dSMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL,
20290554e01dSMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_3);
2030a412576eSMichael Tuexen 		SCTP_INP_DECR_REF(inp);
2031a412576eSMichael Tuexen 		SCTP_INP_WUNLOCK(inp);
20320554e01dSMichael Tuexen 		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
20330554e01dSMichael Tuexen 		    SCTP_CALLED_FROM_INPKILL_TIMER);
20340554e01dSMichael Tuexen 		inp = NULL;
2035*bdd4630cSMichael Tuexen 		goto out_decr;
2036f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASOCKILL:
2037a412576eSMichael Tuexen 		KASSERT(inp != NULL && stcb != NULL && net == NULL,
2038a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2039a412576eSMichael Tuexen 		    type, inp, stcb, net));
2040f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoassockill);
2041f8829a4aSRandall Stewart 		/* Can we free it yet? */
2042ba785902SMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL,
2043ba785902SMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_1);
2044ba785902SMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2045ba785902SMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_2);
2046f8829a4aSRandall Stewart 		/*
2047f8829a4aSRandall Stewart 		 * free asoc, always unlocks (or destroy's) so prevent
2048f8829a4aSRandall Stewart 		 * duplicate unlock or unlock of a free mtx :-0
2049f8829a4aSRandall Stewart 		 */
2050f8829a4aSRandall Stewart 		stcb = NULL;
2051*bdd4630cSMichael Tuexen 		goto out_decr;
20520554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ADDR_WQ:
2053a412576eSMichael Tuexen 		KASSERT(inp == NULL && stcb == NULL && net == NULL,
2054a412576eSMichael Tuexen 		    ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2055a412576eSMichael Tuexen 		    type, inp, stcb, net));
20560554e01dSMichael Tuexen 		sctp_handle_addr_wq();
20578745f898SMichael Tuexen 		did_output = true;
20580554e01dSMichael Tuexen 		break;
20590554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_PRIM_DELETED:
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_STAT_INCR(sctps_timodelprim);
2064a412576eSMichael Tuexen 		sctp_delete_prim_timer(inp, stcb);
20658745f898SMichael Tuexen 		did_output = false;
20660554e01dSMichael Tuexen 		break;
2067f8829a4aSRandall Stewart 	default:
20687522682eSMichael Tuexen #ifdef INVARIANTS
2069a412576eSMichael Tuexen 		panic("Unknown timer type %d", type);
20707522682eSMichael Tuexen #else
20718745f898SMichael Tuexen 		goto out;
20727522682eSMichael Tuexen #endif
207360990c0cSMichael Tuexen 	}
2074f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
2075fa89f692SMichael Tuexen 	sctp_audit_log(0xF1, (uint8_t)type);
20768745f898SMichael Tuexen 	if (inp != NULL)
2077f8829a4aSRandall Stewart 		sctp_auditing(5, inp, stcb, net);
2078f8829a4aSRandall Stewart #endif
20798745f898SMichael Tuexen 	if (did_output && (stcb != NULL)) {
2080f8829a4aSRandall Stewart 		/*
2081f8829a4aSRandall Stewart 		 * Now we need to clean up the control chunk chain if an
2082f8829a4aSRandall Stewart 		 * ECNE is on it. It must be marked as UNSENT again so next
2083f8829a4aSRandall Stewart 		 * call will continue to send it until such time that we get
2084f8829a4aSRandall Stewart 		 * a CWR, to remove it. It is, however, less likely that we
2085f8829a4aSRandall Stewart 		 * will find a ecn echo on the chain though.
2086f8829a4aSRandall Stewart 		 */
2087f8829a4aSRandall Stewart 		sctp_fix_ecn_echo(&stcb->asoc);
2088f8829a4aSRandall Stewart 	}
20898745f898SMichael Tuexen out:
20908745f898SMichael Tuexen 	if (stcb != NULL) {
2091f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
20922c62ba73SMichael Tuexen 	} else if (inp != NULL) {
20932c62ba73SMichael Tuexen 		SCTP_INP_WUNLOCK(inp);
20942c62ba73SMichael Tuexen 	} else {
20952c62ba73SMichael Tuexen 		SCTP_WQ_ADDR_UNLOCK();
2096f8829a4aSRandall Stewart 	}
20972c62ba73SMichael Tuexen 
2098f8829a4aSRandall Stewart out_decr:
20998745f898SMichael Tuexen 	/* These reference counts were incremented in sctp_timer_start(). */
21008745f898SMichael Tuexen 	if (inp != NULL) {
2101f8829a4aSRandall Stewart 		SCTP_INP_DECR_REF(inp);
2102f8829a4aSRandall Stewart 	}
21038745f898SMichael Tuexen 	if ((stcb != NULL) && !released_asoc_reference) {
21048745f898SMichael Tuexen 		atomic_add_int(&stcb->asoc.refcnt, -1);
21058745f898SMichael Tuexen 	}
21068745f898SMichael Tuexen 	if (net != NULL) {
21078745f898SMichael Tuexen 		sctp_free_remote_addr(net);
21088745f898SMichael Tuexen 	}
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 	if (stcb != NULL) {
2154f8829a4aSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
2155a412576eSMichael Tuexen 	} else if (inp != NULL) {
2156a412576eSMichael Tuexen 		SCTP_INP_WLOCK_ASSERT(inp);
2157a412576eSMichael Tuexen 	} else {
2158a412576eSMichael Tuexen 		SCTP_WQ_ADDR_LOCK_ASSERT();
2159a412576eSMichael Tuexen 	}
2160a412576eSMichael Tuexen 	if (stcb != NULL) {
2161a412576eSMichael Tuexen 		/*
2162a412576eSMichael Tuexen 		 * Don't restart timer on association that's about to be
2163a412576eSMichael Tuexen 		 * killed.
2164a412576eSMichael Tuexen 		 */
2165a412576eSMichael Tuexen 		if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
2166a412576eSMichael Tuexen 		    (t_type != SCTP_TIMER_TYPE_ASOCKILL)) {
2167a412576eSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_TIMER2,
216837686ccfSMichael Tuexen 			    "Timer type %d not started: inp=%p, stcb=%p, net=%p (stcb deleted).\n",
2169a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2170a412576eSMichael Tuexen 			return;
2171f8829a4aSRandall Stewart 		}
21729803f01cSMichael Tuexen 		/* Don't restart timer on net that's been removed. */
21739803f01cSMichael Tuexen 		if (net != NULL && (net->dest_state & SCTP_ADDR_BEING_DELETED)) {
2174a412576eSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_TIMER2,
217537686ccfSMichael Tuexen 			    "Timer type %d not started: inp=%p, stcb=%p, net=%p (net deleted).\n",
2176a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
21779803f01cSMichael Tuexen 			return;
21789803f01cSMichael Tuexen 		}
2179a412576eSMichael Tuexen 	}
2180f8829a4aSRandall Stewart 	switch (t_type) {
2181f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SEND:
2182a412576eSMichael Tuexen 		/* Here we use the RTO timer. */
2183a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2184a412576eSMichael Tuexen #ifdef INVARIANTS
2185a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2186a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2187a412576eSMichael Tuexen #else
2188ad81507eSRandall Stewart 			return;
2189a412576eSMichael Tuexen #endif
2190f8829a4aSRandall Stewart 		}
2191f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2192f8829a4aSRandall Stewart 		if (net->RTO == 0) {
219325ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2194f8829a4aSRandall Stewart 		} else {
219525ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
2196f8829a4aSRandall Stewart 		}
2197f8829a4aSRandall Stewart 		break;
2198f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
2199f8829a4aSRandall Stewart 		/*
2200f8829a4aSRandall Stewart 		 * Here we use the INIT timer default usually about 1
2201a412576eSMichael Tuexen 		 * second.
2202f8829a4aSRandall Stewart 		 */
2203a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2204a412576eSMichael Tuexen #ifdef INVARIANTS
2205a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2206a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2207a412576eSMichael Tuexen #else
2208ad81507eSRandall Stewart 			return;
2209a412576eSMichael Tuexen #endif
2210f8829a4aSRandall Stewart 		}
2211f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2212f8829a4aSRandall Stewart 		if (net->RTO == 0) {
221325ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2214f8829a4aSRandall Stewart 		} else {
221525ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
2216f8829a4aSRandall Stewart 		}
2217f8829a4aSRandall Stewart 		break;
2218f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
2219f8829a4aSRandall Stewart 		/*
2220a412576eSMichael Tuexen 		 * Here we use the Delayed-Ack timer value from the inp,
2221f8829a4aSRandall Stewart 		 * ususually about 200ms.
2222f8829a4aSRandall Stewart 		 */
2223a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2224a412576eSMichael Tuexen #ifdef INVARIANTS
2225a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2226a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2227a412576eSMichael Tuexen #else
2228ad81507eSRandall Stewart 			return;
2229a412576eSMichael Tuexen #endif
2230f8829a4aSRandall Stewart 		}
2231f8829a4aSRandall Stewart 		tmr = &stcb->asoc.dack_timer;
223225ec3553SMichael Tuexen 		to_ticks = sctp_msecs_to_ticks(stcb->asoc.delayed_ack);
2233f8829a4aSRandall Stewart 		break;
2234f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
2235f8829a4aSRandall Stewart 		/* Here we use the RTO of the destination. */
2236a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2237a412576eSMichael Tuexen #ifdef INVARIANTS
2238a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2239a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2240a412576eSMichael Tuexen #else
2241ad81507eSRandall Stewart 			return;
2242a412576eSMichael Tuexen #endif
2243f8829a4aSRandall Stewart 		}
2244a412576eSMichael Tuexen 		tmr = &net->rxt_timer;
2245f8829a4aSRandall Stewart 		if (net->RTO == 0) {
224625ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2247f8829a4aSRandall Stewart 		} else {
224825ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
2249f8829a4aSRandall Stewart 		}
2250f8829a4aSRandall Stewart 		break;
2251f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
2252f8829a4aSRandall Stewart 		/*
2253a412576eSMichael Tuexen 		 * The net is used here so that we can add in the RTO. Even
2254f8829a4aSRandall Stewart 		 * though we use a different timer. We also add the HB timer
2255f8829a4aSRandall Stewart 		 * PLUS a random jitter.
2256f8829a4aSRandall Stewart 		 */
2257a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2258a412576eSMichael Tuexen #ifdef INVARIANTS
2259a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2260a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2261a412576eSMichael Tuexen #else
2262ad81507eSRandall Stewart 			return;
2263a412576eSMichael Tuexen #endif
2264a412576eSMichael Tuexen 		}
2265ca85e948SMichael Tuexen 		if ((net->dest_state & SCTP_ADDR_NOHB) &&
2266ca85e948SMichael Tuexen 		    !(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
2267a412576eSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_TIMER2,
226837686ccfSMichael Tuexen 			    "Timer type %d not started: inp=%p, stcb=%p, net=%p.\n",
2269a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2270ad81507eSRandall Stewart 			return;
2271f8829a4aSRandall Stewart 		}
2272a412576eSMichael Tuexen 		tmr = &net->hb_timer;
2273f8829a4aSRandall Stewart 		if (net->RTO == 0) {
2274ca85e948SMichael Tuexen 			to_ticks = stcb->asoc.initial_rto;
2275f8829a4aSRandall Stewart 		} else {
2276ca85e948SMichael Tuexen 			to_ticks = net->RTO;
2277f8829a4aSRandall Stewart 		}
2278ca85e948SMichael Tuexen 		rndval = sctp_select_initial_TSN(&inp->sctp_ep);
2279ca85e948SMichael Tuexen 		jitter = rndval % to_ticks;
2280ca85e948SMichael Tuexen 		if (jitter >= (to_ticks >> 1)) {
2281ca85e948SMichael Tuexen 			to_ticks = to_ticks + (jitter - (to_ticks >> 1));
2282f8829a4aSRandall Stewart 		} else {
2283ca85e948SMichael Tuexen 			to_ticks = to_ticks - jitter;
2284f8829a4aSRandall Stewart 		}
2285ca85e948SMichael Tuexen 		if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
2286ca85e948SMichael Tuexen 		    !(net->dest_state & SCTP_ADDR_PF)) {
2287ca85e948SMichael Tuexen 			to_ticks += net->heart_beat_delay;
2288f8829a4aSRandall Stewart 		}
2289f8829a4aSRandall Stewart 		/*
2290a412576eSMichael Tuexen 		 * Now we must convert the to_ticks that are now in ms to
2291a412576eSMichael Tuexen 		 * ticks.
2292f8829a4aSRandall Stewart 		 */
229325ec3553SMichael Tuexen 		to_ticks = sctp_msecs_to_ticks(to_ticks);
2294f8829a4aSRandall Stewart 		break;
2295f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
2296f8829a4aSRandall Stewart 		/*
2297f8829a4aSRandall Stewart 		 * Here we can use the RTO timer from the network since one
2298a412576eSMichael Tuexen 		 * RTT was complete. If a retransmission happened then we
2299a412576eSMichael Tuexen 		 * will be using the RTO initial value.
2300f8829a4aSRandall Stewart 		 */
2301a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2302a412576eSMichael Tuexen #ifdef INVARIANTS
2303a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2304a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2305a412576eSMichael Tuexen #else
2306ad81507eSRandall Stewart 			return;
2307a412576eSMichael Tuexen #endif
2308f8829a4aSRandall Stewart 		}
2309a412576eSMichael Tuexen 		tmr = &net->rxt_timer;
2310f8829a4aSRandall Stewart 		if (net->RTO == 0) {
231125ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2312f8829a4aSRandall Stewart 		} else {
231325ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
2314f8829a4aSRandall Stewart 		}
2315f8829a4aSRandall Stewart 		break;
2316f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_NEWCOOKIE:
2317f8829a4aSRandall Stewart 		/*
2318a412576eSMichael Tuexen 		 * Nothing needed but the endpoint here ususually about 60
2319f8829a4aSRandall Stewart 		 * minutes.
2320f8829a4aSRandall Stewart 		 */
2321a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb != NULL) || (net != NULL)) {
2322a412576eSMichael Tuexen #ifdef INVARIANTS
2323a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2324a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2325a412576eSMichael Tuexen #else
2326a412576eSMichael Tuexen 			return;
2327a412576eSMichael Tuexen #endif
2328a412576eSMichael Tuexen 		}
2329f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
2330f8829a4aSRandall Stewart 		to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_SIGNATURE];
2331f8829a4aSRandall Stewart 		break;
2332f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
2333f8829a4aSRandall Stewart 		/*
2334a412576eSMichael Tuexen 		 * Here we use the value found in the EP for PMTUD,
2335a412576eSMichael Tuexen 		 * ususually about 10 minutes.
2336f8829a4aSRandall Stewart 		 */
2337a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2338a412576eSMichael Tuexen #ifdef INVARIANTS
2339a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2340a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2341a412576eSMichael Tuexen #else
2342ad81507eSRandall Stewart 			return;
2343a412576eSMichael Tuexen #endif
2344f8829a4aSRandall Stewart 		}
234580c79bbeSMichael Tuexen 		if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
2346a412576eSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_TIMER2,
234737686ccfSMichael Tuexen 			    "Timer type %d not started: inp=%p, stcb=%p, net=%p.\n",
2348a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
234980c79bbeSMichael Tuexen 			return;
235080c79bbeSMichael Tuexen 		}
2351f8829a4aSRandall Stewart 		tmr = &net->pmtu_timer;
2352a412576eSMichael Tuexen 		to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_PMTU];
2353f8829a4aSRandall Stewart 		break;
2354f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
2355a412576eSMichael Tuexen 		/* Here we use the RTO of the destination. */
2356a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2357a412576eSMichael Tuexen #ifdef INVARIANTS
2358a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2359a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2360a412576eSMichael Tuexen #else
2361ad81507eSRandall Stewart 			return;
2362a412576eSMichael Tuexen #endif
2363f8829a4aSRandall Stewart 		}
2364a412576eSMichael Tuexen 		tmr = &net->rxt_timer;
2365f8829a4aSRandall Stewart 		if (net->RTO == 0) {
236625ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2367f8829a4aSRandall Stewart 		} else {
236825ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
2369f8829a4aSRandall Stewart 		}
2370f8829a4aSRandall Stewart 		break;
23710554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ASCONF:
23720554e01dSMichael Tuexen 		/*
23730554e01dSMichael Tuexen 		 * Here the timer comes from the stcb but its value is from
23740554e01dSMichael Tuexen 		 * the net's RTO.
23750554e01dSMichael Tuexen 		 */
2376a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2377a412576eSMichael Tuexen #ifdef INVARIANTS
2378a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2379a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2380a412576eSMichael Tuexen #else
23810554e01dSMichael Tuexen 			return;
2382a412576eSMichael Tuexen #endif
23830554e01dSMichael Tuexen 		}
2384a412576eSMichael Tuexen 		tmr = &stcb->asoc.asconf_timer;
23850554e01dSMichael Tuexen 		if (net->RTO == 0) {
238625ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
23870554e01dSMichael Tuexen 		} else {
238825ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
23890554e01dSMichael Tuexen 		}
23900554e01dSMichael Tuexen 		break;
2391f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2392f8829a4aSRandall Stewart 		/*
2393f8829a4aSRandall Stewart 		 * Here we use the endpoints shutdown guard timer usually
2394f8829a4aSRandall Stewart 		 * about 3 minutes.
2395f8829a4aSRandall Stewart 		 */
2396a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2397a412576eSMichael Tuexen #ifdef INVARIANTS
2398a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2399a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2400a412576eSMichael Tuexen #else
2401ad81507eSRandall Stewart 			return;
2402a412576eSMichael Tuexen #endif
2403f8829a4aSRandall Stewart 		}
2404a412576eSMichael Tuexen 		tmr = &stcb->asoc.shut_guard_timer;
24052e2d6794SMichael Tuexen 		if (inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] == 0) {
240625ec3553SMichael Tuexen 			if (stcb->asoc.maxrto < UINT32_MAX / 5) {
240725ec3553SMichael Tuexen 				to_ticks = sctp_msecs_to_ticks(5 * stcb->asoc.maxrto);
240825ec3553SMichael Tuexen 			} else {
240925ec3553SMichael Tuexen 				to_ticks = sctp_msecs_to_ticks(UINT32_MAX);
241025ec3553SMichael Tuexen 			}
24112e2d6794SMichael Tuexen 		} else {
2412f8829a4aSRandall Stewart 			to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN];
24132e2d6794SMichael Tuexen 		}
2414f8829a4aSRandall Stewart 		break;
24150554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_AUTOCLOSE:
2416a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2417a412576eSMichael Tuexen #ifdef INVARIANTS
2418a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2419a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2420a412576eSMichael Tuexen #else
24210554e01dSMichael Tuexen 			return;
2422a412576eSMichael Tuexen #endif
24230554e01dSMichael Tuexen 		}
24240554e01dSMichael Tuexen 		tmr = &stcb->asoc.autoclose_timer;
2425a412576eSMichael Tuexen 		to_ticks = stcb->asoc.sctp_autoclose_ticks;
24260554e01dSMichael Tuexen 		break;
2427f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_STRRESET:
2428f8829a4aSRandall Stewart 		/*
24291b649582SRandall Stewart 		 * Here the timer comes from the stcb but its value is from
24301b649582SRandall Stewart 		 * the net's RTO.
2431f8829a4aSRandall Stewart 		 */
2432a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2433a412576eSMichael Tuexen #ifdef INVARIANTS
2434a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2435a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2436a412576eSMichael Tuexen #else
2437ad81507eSRandall Stewart 			return;
2438a412576eSMichael Tuexen #endif
2439f8829a4aSRandall Stewart 		}
2440a412576eSMichael Tuexen 		tmr = &stcb->asoc.strreset_timer;
2441f8829a4aSRandall Stewart 		if (net->RTO == 0) {
244225ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2443f8829a4aSRandall Stewart 		} else {
244425ec3553SMichael Tuexen 			to_ticks = sctp_msecs_to_ticks(net->RTO);
2445f8829a4aSRandall Stewart 		}
2446f8829a4aSRandall Stewart 		break;
24470554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_INPKILL:
2448f8829a4aSRandall Stewart 		/*
24490554e01dSMichael Tuexen 		 * The inp is setup to die. We re-use the signature_chage
24500554e01dSMichael Tuexen 		 * timer since that has stopped and we are in the GONE
24510554e01dSMichael Tuexen 		 * state.
2452f8829a4aSRandall Stewart 		 */
2453a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb != NULL) || (net != NULL)) {
2454a412576eSMichael Tuexen #ifdef INVARIANTS
2455a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2456a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2457a412576eSMichael Tuexen #else
2458a412576eSMichael Tuexen 			return;
2459a412576eSMichael Tuexen #endif
2460a412576eSMichael Tuexen 		}
24610554e01dSMichael Tuexen 		tmr = &inp->sctp_ep.signature_change;
246225ec3553SMichael Tuexen 		to_ticks = sctp_msecs_to_ticks(SCTP_INP_KILL_TIMEOUT);
24630554e01dSMichael Tuexen 		break;
24640554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ASOCKILL:
2465a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2466a412576eSMichael Tuexen #ifdef INVARIANTS
2467a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2468a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2469a412576eSMichael Tuexen #else
2470ad81507eSRandall Stewart 			return;
2471a412576eSMichael Tuexen #endif
2472f8829a4aSRandall Stewart 		}
24730554e01dSMichael Tuexen 		tmr = &stcb->asoc.strreset_timer;
247425ec3553SMichael Tuexen 		to_ticks = sctp_msecs_to_ticks(SCTP_ASOC_KILL_TIMEOUT);
24750554e01dSMichael Tuexen 		break;
24760554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ADDR_WQ:
2477a412576eSMichael Tuexen 		if ((inp != NULL) || (stcb != NULL) || (net != NULL)) {
2478a412576eSMichael Tuexen #ifdef INVARIANTS
2479a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2480a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2481a412576eSMichael Tuexen #else
2482a412576eSMichael Tuexen 			return;
2483a412576eSMichael Tuexen #endif
2484a412576eSMichael Tuexen 		}
24850554e01dSMichael Tuexen 		/* Only 1 tick away :-) */
24860554e01dSMichael Tuexen 		tmr = &SCTP_BASE_INFO(addr_wq_timer);
24870554e01dSMichael Tuexen 		to_ticks = SCTP_ADDRESS_TICK_DELAY;
2488f8829a4aSRandall Stewart 		break;
2489851b7298SRandall Stewart 	case SCTP_TIMER_TYPE_PRIM_DELETED:
2490a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2491a412576eSMichael Tuexen #ifdef INVARIANTS
2492a412576eSMichael Tuexen 			panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2493a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2494a412576eSMichael Tuexen #else
2495851b7298SRandall Stewart 			return;
2496a412576eSMichael Tuexen #endif
2497851b7298SRandall Stewart 		}
2498851b7298SRandall Stewart 		tmr = &stcb->asoc.delete_prim_timer;
249925ec3553SMichael Tuexen 		to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2500851b7298SRandall Stewart 		break;
2501f8829a4aSRandall Stewart 	default:
25027522682eSMichael Tuexen #ifdef INVARIANTS
2503a412576eSMichael Tuexen 		panic("Unknown timer type %d", t_type);
25047522682eSMichael Tuexen #else
25057522682eSMichael Tuexen 		return;
25067522682eSMichael Tuexen #endif
250760990c0cSMichael Tuexen 	}
2508a412576eSMichael Tuexen 	KASSERT(tmr != NULL, ("tmr is NULL for timer type %d", t_type));
2509a412576eSMichael Tuexen 	KASSERT(to_ticks > 0, ("to_ticks == 0 for timer type %d", t_type));
2510139bc87fSRandall Stewart 	if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
2511f8829a4aSRandall Stewart 		/*
2512a412576eSMichael Tuexen 		 * We do NOT allow you to have it already running. If it is,
2513a412576eSMichael Tuexen 		 * we leave the current one up unchanged.
2514f8829a4aSRandall Stewart 		 */
2515a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
251637686ccfSMichael Tuexen 		    "Timer type %d already running: inp=%p, stcb=%p, net=%p.\n",
2517a412576eSMichael Tuexen 		    t_type, inp, stcb, net);
2518ad81507eSRandall Stewart 		return;
2519f8829a4aSRandall Stewart 	}
2520a412576eSMichael Tuexen 	/* At this point we can proceed. */
2521f8829a4aSRandall Stewart 	if (t_type == SCTP_TIMER_TYPE_SEND) {
2522f8829a4aSRandall Stewart 		stcb->asoc.num_send_timers_up++;
2523f8829a4aSRandall Stewart 	}
2524a5d547adSRandall Stewart 	tmr->stopped_from = 0;
2525f8829a4aSRandall Stewart 	tmr->type = t_type;
2526f8829a4aSRandall Stewart 	tmr->ep = (void *)inp;
2527f8829a4aSRandall Stewart 	tmr->tcb = (void *)stcb;
2528a412576eSMichael Tuexen 	if (t_type == SCTP_TIMER_TYPE_STRRESET) {
2529a412576eSMichael Tuexen 		tmr->net = NULL;
2530a412576eSMichael Tuexen 	} else {
2531f8829a4aSRandall Stewart 		tmr->net = (void *)net;
2532a412576eSMichael Tuexen 	}
2533f8829a4aSRandall Stewart 	tmr->self = (void *)tmr;
25348518270eSMichael Tuexen 	tmr->vnet = (void *)curvnet;
2535c4739e2fSRandall Stewart 	tmr->ticks = sctp_get_tick_count();
2536a412576eSMichael Tuexen 	if (SCTP_OS_TIMER_START(&tmr->timer, to_ticks, sctp_timeout_handler, tmr) == 0) {
2537a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
253837686ccfSMichael Tuexen 		    "Timer type %d started: ticks=%u, inp=%p, stcb=%p, net=%p.\n",
2539a412576eSMichael Tuexen 		    t_type, to_ticks, inp, stcb, net);
25408745f898SMichael Tuexen 		/*
25418745f898SMichael Tuexen 		 * If this is a newly scheduled callout, as opposed to a
25428745f898SMichael Tuexen 		 * rescheduled one, increment relevant reference counts.
25438745f898SMichael Tuexen 		 */
25448745f898SMichael Tuexen 		if (tmr->ep != NULL) {
25458745f898SMichael Tuexen 			SCTP_INP_INCR_REF(inp);
25468745f898SMichael Tuexen 		}
25478745f898SMichael Tuexen 		if (tmr->tcb != NULL) {
25488745f898SMichael Tuexen 			atomic_add_int(&stcb->asoc.refcnt, 1);
25498745f898SMichael Tuexen 		}
25508745f898SMichael Tuexen 		if (tmr->net != NULL) {
25518745f898SMichael Tuexen 			atomic_add_int(&net->ref_count, 1);
25528745f898SMichael Tuexen 		}
2553a412576eSMichael Tuexen 	} else {
2554a412576eSMichael Tuexen 		/*
2555a412576eSMichael Tuexen 		 * This should not happen, since we checked for pending
2556a412576eSMichael Tuexen 		 * above.
2557a412576eSMichael Tuexen 		 */
2558a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
255937686ccfSMichael Tuexen 		    "Timer type %d restarted: ticks=%u, inp=%p, stcb=%p, net=%p.\n",
2560a412576eSMichael Tuexen 		    t_type, to_ticks, inp, stcb, net);
2561a412576eSMichael Tuexen 	}
2562ad81507eSRandall Stewart 	return;
2563f8829a4aSRandall Stewart }
2564f8829a4aSRandall Stewart 
2565a412576eSMichael Tuexen /*-
2566a412576eSMichael Tuexen  * The following table shows which parameters must be provided
2567a412576eSMichael Tuexen  * when calling sctp_timer_stop(). For parameters not being
2568a412576eSMichael Tuexen  * provided, NULL must be used.
2569a412576eSMichael Tuexen  *
2570a412576eSMichael Tuexen  * |Name                         |inp |stcb|net |
2571a412576eSMichael Tuexen  * |-----------------------------|----|----|----|
2572a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SEND         |Yes |Yes |Yes |
2573a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_INIT         |Yes |Yes |Yes |
2574a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_RECV         |Yes |Yes |No  |
2575a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SHUTDOWN     |Yes |Yes |Yes |
2576a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_HEARTBEAT    |Yes |Yes |Yes |
2577a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_COOKIE       |Yes |Yes |Yes |
2578a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_NEWCOOKIE    |Yes |No  |No  |
2579a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_PATHMTURAISE |Yes |Yes |Yes |
2580a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SHUTDOWNACK  |Yes |Yes |Yes |
2581a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_ASCONF       |Yes |Yes |No  |
2582a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_SHUTDOWNGUARD|Yes |Yes |No  |
2583a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_AUTOCLOSE    |Yes |Yes |No  |
2584a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_STRRESET     |Yes |Yes |No  |
2585a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_INPKILL      |Yes |No  |No  |
2586a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_ASOCKILL     |Yes |Yes |No  |
2587a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_ADDR_WQ      |No  |No  |No  |
2588a412576eSMichael Tuexen  * |SCTP_TIMER_TYPE_PRIM_DELETED |Yes |Yes |No  |
2589a412576eSMichael Tuexen  *
2590a412576eSMichael Tuexen  */
2591a412576eSMichael Tuexen 
25926e55db54SRandall Stewart void
2593f8829a4aSRandall Stewart sctp_timer_stop(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2594a5d547adSRandall Stewart     struct sctp_nets *net, uint32_t from)
2595f8829a4aSRandall Stewart {
2596f8829a4aSRandall Stewart 	struct sctp_timer *tmr;
2597f8829a4aSRandall Stewart 
2598f4cb790aSMichael Tuexen 	KASSERT(stcb == NULL || stcb->sctp_ep == inp,
2599f4cb790aSMichael Tuexen 	    ("sctp_timer_stop of type %d: inp = %p, stcb->sctp_ep %p",
2600f4cb790aSMichael Tuexen 	    t_type, stcb, stcb->sctp_ep));
2601a412576eSMichael Tuexen 	if (stcb != NULL) {
2602f8829a4aSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
2603a412576eSMichael Tuexen 	} else if (inp != NULL) {
2604a412576eSMichael Tuexen 		SCTP_INP_WLOCK_ASSERT(inp);
2605a412576eSMichael Tuexen 	} else {
2606a412576eSMichael Tuexen 		SCTP_WQ_ADDR_LOCK_ASSERT();
2607f8829a4aSRandall Stewart 	}
2608a412576eSMichael Tuexen 	tmr = NULL;
2609f8829a4aSRandall Stewart 	switch (t_type) {
2610f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SEND:
2611a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2612a412576eSMichael Tuexen #ifdef INVARIANTS
2613a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2614a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2615a412576eSMichael Tuexen #else
26166e55db54SRandall Stewart 			return;
2617a412576eSMichael Tuexen #endif
2618f8829a4aSRandall Stewart 		}
2619f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2620f8829a4aSRandall Stewart 		break;
2621f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
2622a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2623a412576eSMichael Tuexen #ifdef INVARIANTS
2624a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2625a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2626a412576eSMichael Tuexen #else
26276e55db54SRandall Stewart 			return;
2628a412576eSMichael Tuexen #endif
2629f8829a4aSRandall Stewart 		}
2630f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2631f8829a4aSRandall Stewart 		break;
2632f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
2633a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2634a412576eSMichael Tuexen #ifdef INVARIANTS
2635a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2636a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2637a412576eSMichael Tuexen #else
26386e55db54SRandall Stewart 			return;
2639a412576eSMichael Tuexen #endif
2640f8829a4aSRandall Stewart 		}
2641f8829a4aSRandall Stewart 		tmr = &stcb->asoc.dack_timer;
2642f8829a4aSRandall Stewart 		break;
2643f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
2644a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2645a412576eSMichael Tuexen #ifdef INVARIANTS
2646a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2647a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2648a412576eSMichael Tuexen #else
26496e55db54SRandall Stewart 			return;
2650a412576eSMichael Tuexen #endif
2651f8829a4aSRandall Stewart 		}
2652f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2653f8829a4aSRandall Stewart 		break;
2654f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
2655a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2656a412576eSMichael Tuexen #ifdef INVARIANTS
2657a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2658a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2659a412576eSMichael Tuexen #else
26606e55db54SRandall Stewart 			return;
2661a412576eSMichael Tuexen #endif
2662f8829a4aSRandall Stewart 		}
2663ca85e948SMichael Tuexen 		tmr = &net->hb_timer;
2664f8829a4aSRandall Stewart 		break;
2665f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
2666a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2667a412576eSMichael Tuexen #ifdef INVARIANTS
2668a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2669a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2670a412576eSMichael Tuexen #else
26716e55db54SRandall Stewart 			return;
2672a412576eSMichael Tuexen #endif
2673f8829a4aSRandall Stewart 		}
2674f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2675f8829a4aSRandall Stewart 		break;
2676f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_NEWCOOKIE:
2677a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb != NULL) || (net != NULL)) {
2678a412576eSMichael Tuexen #ifdef INVARIANTS
2679a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2680a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2681a412576eSMichael Tuexen #else
2682a412576eSMichael Tuexen 			return;
2683a412576eSMichael Tuexen #endif
2684a412576eSMichael Tuexen 		}
2685f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
2686f8829a4aSRandall Stewart 		break;
2687f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
2688a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2689a412576eSMichael Tuexen #ifdef INVARIANTS
2690a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2691a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2692a412576eSMichael Tuexen #else
26936e55db54SRandall Stewart 			return;
2694a412576eSMichael Tuexen #endif
2695f8829a4aSRandall Stewart 		}
2696f8829a4aSRandall Stewart 		tmr = &net->pmtu_timer;
2697f8829a4aSRandall Stewart 		break;
2698f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
2699a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2700a412576eSMichael Tuexen #ifdef INVARIANTS
2701a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2702a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2703a412576eSMichael Tuexen #else
27046e55db54SRandall Stewart 			return;
2705a412576eSMichael Tuexen #endif
2706f8829a4aSRandall Stewart 		}
2707f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2708f8829a4aSRandall Stewart 		break;
27090554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ASCONF:
2710a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2711a412576eSMichael Tuexen #ifdef INVARIANTS
2712a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2713a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2714a412576eSMichael Tuexen #else
27150554e01dSMichael Tuexen 			return;
2716a412576eSMichael Tuexen #endif
27170554e01dSMichael Tuexen 		}
27180554e01dSMichael Tuexen 		tmr = &stcb->asoc.asconf_timer;
27190554e01dSMichael Tuexen 		break;
2720f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2721a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2722a412576eSMichael Tuexen #ifdef INVARIANTS
2723a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2724a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2725a412576eSMichael Tuexen #else
27266e55db54SRandall Stewart 			return;
2727a412576eSMichael Tuexen #endif
2728f8829a4aSRandall Stewart 		}
2729f8829a4aSRandall Stewart 		tmr = &stcb->asoc.shut_guard_timer;
2730f8829a4aSRandall Stewart 		break;
27310554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_AUTOCLOSE:
2732a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2733a412576eSMichael Tuexen #ifdef INVARIANTS
2734a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2735a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2736a412576eSMichael Tuexen #else
27370554e01dSMichael Tuexen 			return;
2738a412576eSMichael Tuexen #endif
27390554e01dSMichael Tuexen 		}
27400554e01dSMichael Tuexen 		tmr = &stcb->asoc.autoclose_timer;
27410554e01dSMichael Tuexen 		break;
2742f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_STRRESET:
2743a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2744a412576eSMichael Tuexen #ifdef INVARIANTS
2745a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2746a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2747a412576eSMichael Tuexen #else
27486e55db54SRandall Stewart 			return;
2749a412576eSMichael Tuexen #endif
2750f8829a4aSRandall Stewart 		}
2751f8829a4aSRandall Stewart 		tmr = &stcb->asoc.strreset_timer;
2752f8829a4aSRandall Stewart 		break;
27530554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_INPKILL:
27540554e01dSMichael Tuexen 		/*
27550554e01dSMichael Tuexen 		 * The inp is setup to die. We re-use the signature_chage
27560554e01dSMichael Tuexen 		 * timer since that has stopped and we are in the GONE
27570554e01dSMichael Tuexen 		 * state.
27580554e01dSMichael Tuexen 		 */
2759a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb != NULL) || (net != NULL)) {
2760a412576eSMichael Tuexen #ifdef INVARIANTS
2761a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2762a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2763a412576eSMichael Tuexen #else
2764a412576eSMichael Tuexen 			return;
2765a412576eSMichael Tuexen #endif
2766a412576eSMichael Tuexen 		}
27670554e01dSMichael Tuexen 		tmr = &inp->sctp_ep.signature_change;
27680554e01dSMichael Tuexen 		break;
27690554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ASOCKILL:
2770a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2771a412576eSMichael Tuexen #ifdef INVARIANTS
2772a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2773a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2774a412576eSMichael Tuexen #else
27756e55db54SRandall Stewart 			return;
2776a412576eSMichael Tuexen #endif
2777f8829a4aSRandall Stewart 		}
27780554e01dSMichael Tuexen 		tmr = &stcb->asoc.strreset_timer;
27790554e01dSMichael Tuexen 		break;
27800554e01dSMichael Tuexen 	case SCTP_TIMER_TYPE_ADDR_WQ:
2781a412576eSMichael Tuexen 		if ((inp != NULL) || (stcb != NULL) || (net != NULL)) {
2782a412576eSMichael Tuexen #ifdef INVARIANTS
2783a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2784a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2785a412576eSMichael Tuexen #else
2786a412576eSMichael Tuexen 			return;
2787a412576eSMichael Tuexen #endif
2788a412576eSMichael Tuexen 		}
27890554e01dSMichael Tuexen 		tmr = &SCTP_BASE_INFO(addr_wq_timer);
2790f8829a4aSRandall Stewart 		break;
2791851b7298SRandall Stewart 	case SCTP_TIMER_TYPE_PRIM_DELETED:
2792a412576eSMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2793a412576eSMichael Tuexen #ifdef INVARIANTS
2794a412576eSMichael Tuexen 			panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2795a412576eSMichael Tuexen 			    t_type, inp, stcb, net);
2796a412576eSMichael Tuexen #else
2797851b7298SRandall Stewart 			return;
2798a412576eSMichael Tuexen #endif
2799851b7298SRandall Stewart 		}
2800851b7298SRandall Stewart 		tmr = &stcb->asoc.delete_prim_timer;
2801851b7298SRandall Stewart 		break;
2802f8829a4aSRandall Stewart 	default:
28037522682eSMichael Tuexen #ifdef INVARIANTS
2804a412576eSMichael Tuexen 		panic("Unknown timer type %d", t_type);
28057522682eSMichael Tuexen #else
28067522682eSMichael Tuexen 		return;
28077522682eSMichael Tuexen #endif
280860990c0cSMichael Tuexen 	}
2809a412576eSMichael Tuexen 	KASSERT(tmr != NULL, ("tmr is NULL for timer type %d", t_type));
2810a412576eSMichael Tuexen 	if ((tmr->type != SCTP_TIMER_TYPE_NONE) &&
2811a412576eSMichael Tuexen 	    (tmr->type != t_type)) {
2812f8829a4aSRandall Stewart 		/*
2813f8829a4aSRandall Stewart 		 * Ok we have a timer that is under joint use. Cookie timer
2814f8829a4aSRandall Stewart 		 * per chance with the SEND timer. We therefore are NOT
2815f8829a4aSRandall Stewart 		 * running the timer that the caller wants stopped.  So just
2816f8829a4aSRandall Stewart 		 * return.
2817f8829a4aSRandall Stewart 		 */
2818a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
281937686ccfSMichael Tuexen 		    "Shared timer type %d not running: inp=%p, stcb=%p, net=%p.\n",
2820a412576eSMichael Tuexen 		    t_type, inp, stcb, net);
28216e55db54SRandall Stewart 		return;
2822f8829a4aSRandall Stewart 	}
2823ad81507eSRandall Stewart 	if ((t_type == SCTP_TIMER_TYPE_SEND) && (stcb != NULL)) {
2824f8829a4aSRandall Stewart 		stcb->asoc.num_send_timers_up--;
2825f8829a4aSRandall Stewart 		if (stcb->asoc.num_send_timers_up < 0) {
2826f8829a4aSRandall Stewart 			stcb->asoc.num_send_timers_up = 0;
2827f8829a4aSRandall Stewart 		}
2828f8829a4aSRandall Stewart 	}
2829f8829a4aSRandall Stewart 	tmr->self = NULL;
2830a5d547adSRandall Stewart 	tmr->stopped_from = from;
2831a412576eSMichael Tuexen 	if (SCTP_OS_TIMER_STOP(&tmr->timer) == 1) {
2832a412576eSMichael Tuexen 		KASSERT(tmr->ep == inp,
2833a412576eSMichael Tuexen 		    ("sctp_timer_stop of type %d: inp = %p, tmr->inp = %p",
2834a412576eSMichael Tuexen 		    t_type, inp, tmr->ep));
2835a412576eSMichael Tuexen 		KASSERT(tmr->tcb == stcb,
2836a412576eSMichael Tuexen 		    ("sctp_timer_stop of type %d: stcb = %p, tmr->stcb = %p",
2837a412576eSMichael Tuexen 		    t_type, stcb, tmr->tcb));
2838a412576eSMichael Tuexen 		KASSERT(((t_type == SCTP_TIMER_TYPE_ASCONF) && (tmr->net != NULL)) ||
2839a412576eSMichael Tuexen 		    ((t_type != SCTP_TIMER_TYPE_ASCONF) && (tmr->net == net)),
2840a412576eSMichael Tuexen 		    ("sctp_timer_stop of type %d: net = %p, tmr->net = %p",
2841a412576eSMichael Tuexen 		    t_type, net, tmr->net));
2842a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
284337686ccfSMichael Tuexen 		    "Timer type %d stopped: inp=%p, stcb=%p, net=%p.\n",
2844a412576eSMichael Tuexen 		    t_type, inp, stcb, net);
28458745f898SMichael Tuexen 		/*
28468745f898SMichael Tuexen 		 * If the timer was actually stopped, decrement reference
28478745f898SMichael Tuexen 		 * counts that were incremented in sctp_timer_start().
28488745f898SMichael Tuexen 		 */
28498745f898SMichael Tuexen 		if (tmr->ep != NULL) {
28508745f898SMichael Tuexen 			SCTP_INP_DECR_REF(inp);
2851a412576eSMichael Tuexen 			tmr->ep = NULL;
28528745f898SMichael Tuexen 		}
28538745f898SMichael Tuexen 		if (tmr->tcb != NULL) {
28548745f898SMichael Tuexen 			atomic_add_int(&stcb->asoc.refcnt, -1);
2855a412576eSMichael Tuexen 			tmr->tcb = NULL;
28568745f898SMichael Tuexen 		}
28578745f898SMichael Tuexen 		if (tmr->net != NULL) {
28588745f898SMichael Tuexen 			/*
28598745f898SMichael Tuexen 			 * Can't use net, since it doesn't work for
28608745f898SMichael Tuexen 			 * SCTP_TIMER_TYPE_ASCONF.
28618745f898SMichael Tuexen 			 */
28628745f898SMichael Tuexen 			sctp_free_remote_addr((struct sctp_nets *)tmr->net);
2863a412576eSMichael Tuexen 			tmr->net = NULL;
28648745f898SMichael Tuexen 		}
2865a412576eSMichael Tuexen 	} else {
2866a412576eSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_TIMER2,
286737686ccfSMichael Tuexen 		    "Timer type %d not stopped: inp=%p, stcb=%p, net=%p.\n",
2868a412576eSMichael Tuexen 		    t_type, inp, stcb, net);
2869a412576eSMichael Tuexen 	}
28706e55db54SRandall Stewart 	return;
2871f8829a4aSRandall Stewart }
2872f8829a4aSRandall Stewart 
2873f8829a4aSRandall Stewart uint32_t
2874b0471b4bSMichael Tuexen sctp_calculate_len(struct mbuf *m)
2875b0471b4bSMichael Tuexen {
2876f8829a4aSRandall Stewart 	uint32_t tlen = 0;
2877f8829a4aSRandall Stewart 	struct mbuf *at;
2878f8829a4aSRandall Stewart 
2879f8829a4aSRandall Stewart 	at = m;
2880f8829a4aSRandall Stewart 	while (at) {
2881139bc87fSRandall Stewart 		tlen += SCTP_BUF_LEN(at);
2882139bc87fSRandall Stewart 		at = SCTP_BUF_NEXT(at);
2883f8829a4aSRandall Stewart 	}
2884f8829a4aSRandall Stewart 	return (tlen);
2885f8829a4aSRandall Stewart }
2886f8829a4aSRandall Stewart 
2887f8829a4aSRandall Stewart void
2888f8829a4aSRandall Stewart sctp_mtu_size_reset(struct sctp_inpcb *inp,
288944b7479bSRandall Stewart     struct sctp_association *asoc, uint32_t mtu)
2890f8829a4aSRandall Stewart {
2891f8829a4aSRandall Stewart 	/*
2892f8829a4aSRandall Stewart 	 * Reset the P-MTU size on this association, this involves changing
2893f8829a4aSRandall Stewart 	 * the asoc MTU, going through ANY chunk+overhead larger than mtu to
2894f8829a4aSRandall Stewart 	 * allow the DF flag to be cleared.
2895f8829a4aSRandall Stewart 	 */
2896f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
2897f8829a4aSRandall Stewart 	unsigned int eff_mtu, ovh;
2898f8829a4aSRandall Stewart 
2899f8829a4aSRandall Stewart 	asoc->smallest_mtu = mtu;
2900f8829a4aSRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2901f8829a4aSRandall Stewart 		ovh = SCTP_MIN_OVERHEAD;
2902f8829a4aSRandall Stewart 	} else {
2903f8829a4aSRandall Stewart 		ovh = SCTP_MIN_V4_OVERHEAD;
2904f8829a4aSRandall Stewart 	}
2905f8829a4aSRandall Stewart 	eff_mtu = mtu - ovh;
2906f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
2907f8829a4aSRandall Stewart 		if (chk->send_size > eff_mtu) {
2908f8829a4aSRandall Stewart 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2909f8829a4aSRandall Stewart 		}
2910f8829a4aSRandall Stewart 	}
2911f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
2912f8829a4aSRandall Stewart 		if (chk->send_size > eff_mtu) {
2913f8829a4aSRandall Stewart 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2914f8829a4aSRandall Stewart 		}
2915f8829a4aSRandall Stewart 	}
2916f8829a4aSRandall Stewart }
2917f8829a4aSRandall Stewart 
2918f8829a4aSRandall Stewart /*
291944f2a327SMichael Tuexen  * Given an association and starting time of the current RTT period, update
292044f2a327SMichael Tuexen  * RTO in number of msecs. net should point to the current network.
292144f2a327SMichael Tuexen  * Return 1, if an RTO update was performed, return 0 if no update was
292244f2a327SMichael Tuexen  * performed due to invalid starting point.
2923f8829a4aSRandall Stewart  */
2924899288aeSRandall Stewart 
292544f2a327SMichael Tuexen int
2926f8829a4aSRandall Stewart sctp_calculate_rto(struct sctp_tcb *stcb,
2927f8829a4aSRandall Stewart     struct sctp_association *asoc,
2928f8829a4aSRandall Stewart     struct sctp_nets *net,
29298c8e10b7SMichael Tuexen     struct timeval *old,
2930b0471b4bSMichael Tuexen     int rtt_from_sack)
2931b0471b4bSMichael Tuexen {
293244f2a327SMichael Tuexen 	struct timeval now;
293344f2a327SMichael Tuexen 	uint64_t rtt_us;	/* RTT in us */
2934be1d9176SMichael Tuexen 	int32_t rtt;		/* RTT in ms */
2935be1d9176SMichael Tuexen 	uint32_t new_rto;
2936f8829a4aSRandall Stewart 	int first_measure = 0;
2937f8829a4aSRandall Stewart 
2938f8829a4aSRandall Stewart 	/************************/
2939f8829a4aSRandall Stewart 	/* 1. calculate new RTT */
2940f8829a4aSRandall Stewart 	/************************/
2941f8829a4aSRandall Stewart 	/* get the current time */
2942299108c5SRandall Stewart 	if (stcb->asoc.use_precise_time) {
2943299108c5SRandall Stewart 		(void)SCTP_GETPTIME_TIMEVAL(&now);
2944299108c5SRandall Stewart 	} else {
29456e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
2946299108c5SRandall Stewart 	}
294744f2a327SMichael Tuexen 	if ((old->tv_sec > now.tv_sec) ||
294888116b7eSMichael Tuexen 	    ((old->tv_sec == now.tv_sec) && (old->tv_usec > now.tv_usec))) {
294944f2a327SMichael Tuexen 		/* The starting point is in the future. */
295044f2a327SMichael Tuexen 		return (0);
295144f2a327SMichael Tuexen 	}
2952be1d9176SMichael Tuexen 	timevalsub(&now, old);
295344f2a327SMichael Tuexen 	rtt_us = (uint64_t)1000000 * (uint64_t)now.tv_sec + (uint64_t)now.tv_usec;
295444f2a327SMichael Tuexen 	if (rtt_us > SCTP_RTO_UPPER_BOUND * 1000) {
295544f2a327SMichael Tuexen 		/* The RTT is larger than a sane value. */
295644f2a327SMichael Tuexen 		return (0);
295744f2a327SMichael Tuexen 	}
2958be1d9176SMichael Tuexen 	/* store the current RTT in us */
295944f2a327SMichael Tuexen 	net->rtt = rtt_us;
2960b60b0fe6SMichael Tuexen 	/* compute rtt in ms */
2961b60b0fe6SMichael Tuexen 	rtt = (int32_t)(net->rtt / 1000);
2962f79aab18SRandall Stewart 	if ((asoc->cc_functions.sctp_rtt_calculated) && (rtt_from_sack == SCTP_RTT_FROM_DATA)) {
2963b7b84c0eSMichael Tuexen 		/*
2964b7b84c0eSMichael Tuexen 		 * Tell the CC module that a new update has just occurred
2965b7b84c0eSMichael Tuexen 		 * from a sack
2966b7b84c0eSMichael Tuexen 		 */
2967f79aab18SRandall Stewart 		(*asoc->cc_functions.sctp_rtt_calculated) (stcb, net, &now);
2968f79aab18SRandall Stewart 	}
2969f79aab18SRandall Stewart 	/*
2970f79aab18SRandall Stewart 	 * Do we need to determine the lan? We do this only on sacks i.e.
2971f79aab18SRandall Stewart 	 * RTT being determined from data not non-data (HB/INIT->INITACK).
2972f79aab18SRandall Stewart 	 */
2973f79aab18SRandall Stewart 	if ((rtt_from_sack == SCTP_RTT_FROM_DATA) &&
2974be1d9176SMichael Tuexen 	    (net->lan_type == SCTP_LAN_UNKNOWN)) {
2975be1d9176SMichael Tuexen 		if (net->rtt > SCTP_LOCAL_LAN_RTT) {
2976899288aeSRandall Stewart 			net->lan_type = SCTP_LAN_INTERNET;
2977899288aeSRandall Stewart 		} else {
2978899288aeSRandall Stewart 			net->lan_type = SCTP_LAN_LOCAL;
2979899288aeSRandall Stewart 		}
2980899288aeSRandall Stewart 	}
29810053ed28SMichael Tuexen 
2982f8829a4aSRandall Stewart 	/***************************/
2983f8829a4aSRandall Stewart 	/* 2. update RTTVAR & SRTT */
2984f8829a4aSRandall Stewart 	/***************************/
2985be1d9176SMichael Tuexen 	/*-
2986be1d9176SMichael Tuexen 	 * Compute the scaled average lastsa and the
2987be1d9176SMichael Tuexen 	 * scaled variance lastsv as described in van Jacobson
2988be1d9176SMichael Tuexen 	 * Paper "Congestion Avoidance and Control", Annex A.
2989be1d9176SMichael Tuexen 	 *
2990be1d9176SMichael Tuexen 	 * (net->lastsa >> SCTP_RTT_SHIFT) is the srtt
299144f2a327SMichael Tuexen 	 * (net->lastsv >> SCTP_RTT_VAR_SHIFT) is the rttvar
2992be1d9176SMichael Tuexen 	 */
29939a972525SRandall Stewart 	if (net->RTO_measured) {
2994be1d9176SMichael Tuexen 		rtt -= (net->lastsa >> SCTP_RTT_SHIFT);
2995be1d9176SMichael Tuexen 		net->lastsa += rtt;
2996be1d9176SMichael Tuexen 		if (rtt < 0) {
2997be1d9176SMichael Tuexen 			rtt = -rtt;
2998be1d9176SMichael Tuexen 		}
2999be1d9176SMichael Tuexen 		rtt -= (net->lastsv >> SCTP_RTT_VAR_SHIFT);
3000be1d9176SMichael Tuexen 		net->lastsv += rtt;
3001b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
3002f8829a4aSRandall Stewart 			rto_logging(net, SCTP_LOG_RTTVAR);
300380fefe0aSRandall Stewart 		}
3004f8829a4aSRandall Stewart 	} else {
3005f8829a4aSRandall Stewart 		/* First RTO measurment */
30069a972525SRandall Stewart 		net->RTO_measured = 1;
3007f8829a4aSRandall Stewart 		first_measure = 1;
3008be1d9176SMichael Tuexen 		net->lastsa = rtt << SCTP_RTT_SHIFT;
3009be1d9176SMichael Tuexen 		net->lastsv = (rtt / 2) << SCTP_RTT_VAR_SHIFT;
3010b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
3011f8829a4aSRandall Stewart 			rto_logging(net, SCTP_LOG_INITIAL_RTT);
301280fefe0aSRandall Stewart 		}
3013f8829a4aSRandall Stewart 	}
3014be1d9176SMichael Tuexen 	if (net->lastsv == 0) {
3015be1d9176SMichael Tuexen 		net->lastsv = SCTP_CLOCK_GRANULARITY;
3016be1d9176SMichael Tuexen 	}
3017108df27cSRandall Stewart 	new_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
3018f8829a4aSRandall Stewart 	if ((new_rto > SCTP_SAT_NETWORK_MIN) &&
3019f8829a4aSRandall Stewart 	    (stcb->asoc.sat_network_lockout == 0)) {
3020f8829a4aSRandall Stewart 		stcb->asoc.sat_network = 1;
3021f8829a4aSRandall Stewart 	} else if ((!first_measure) && stcb->asoc.sat_network) {
3022f8829a4aSRandall Stewart 		stcb->asoc.sat_network = 0;
3023f8829a4aSRandall Stewart 		stcb->asoc.sat_network_lockout = 1;
3024f8829a4aSRandall Stewart 	}
3025f8829a4aSRandall Stewart 	/* bound it, per C6/C7 in Section 5.3.1 */
3026f8829a4aSRandall Stewart 	if (new_rto < stcb->asoc.minrto) {
3027f8829a4aSRandall Stewart 		new_rto = stcb->asoc.minrto;
3028f8829a4aSRandall Stewart 	}
3029f8829a4aSRandall Stewart 	if (new_rto > stcb->asoc.maxrto) {
3030f8829a4aSRandall Stewart 		new_rto = stcb->asoc.maxrto;
3031f8829a4aSRandall Stewart 	}
303244f2a327SMichael Tuexen 	net->RTO = new_rto;
303344f2a327SMichael Tuexen 	return (1);
3034f8829a4aSRandall Stewart }
3035f8829a4aSRandall Stewart 
3036f8829a4aSRandall Stewart /*
3037f8829a4aSRandall Stewart  * return a pointer to a contiguous piece of data from the given mbuf chain
3038f8829a4aSRandall Stewart  * starting at 'off' for 'len' bytes.  If the desired piece spans more than
3039f8829a4aSRandall Stewart  * one mbuf, a copy is made at 'ptr'. caller must ensure that the buffer size
3040f8829a4aSRandall Stewart  * is >= 'len' returns NULL if there there isn't 'len' bytes in the chain.
3041f8829a4aSRandall Stewart  */
304272fb6fdbSRandall Stewart caddr_t
3043f8829a4aSRandall Stewart sctp_m_getptr(struct mbuf *m, int off, int len, uint8_t *in_ptr)
3044f8829a4aSRandall Stewart {
3045f8829a4aSRandall Stewart 	uint32_t count;
3046f8829a4aSRandall Stewart 	uint8_t *ptr;
3047f8829a4aSRandall Stewart 
3048f8829a4aSRandall Stewart 	ptr = in_ptr;
3049f8829a4aSRandall Stewart 	if ((off < 0) || (len <= 0))
3050f8829a4aSRandall Stewart 		return (NULL);
3051f8829a4aSRandall Stewart 
3052f8829a4aSRandall Stewart 	/* find the desired start location */
3053f8829a4aSRandall Stewart 	while ((m != NULL) && (off > 0)) {
3054139bc87fSRandall Stewart 		if (off < SCTP_BUF_LEN(m))
3055f8829a4aSRandall Stewart 			break;
3056139bc87fSRandall Stewart 		off -= SCTP_BUF_LEN(m);
3057139bc87fSRandall Stewart 		m = SCTP_BUF_NEXT(m);
3058f8829a4aSRandall Stewart 	}
3059f8829a4aSRandall Stewart 	if (m == NULL)
3060f8829a4aSRandall Stewart 		return (NULL);
3061f8829a4aSRandall Stewart 
3062f8829a4aSRandall Stewart 	/* is the current mbuf large enough (eg. contiguous)? */
3063139bc87fSRandall Stewart 	if ((SCTP_BUF_LEN(m) - off) >= len) {
3064f8829a4aSRandall Stewart 		return (mtod(m, caddr_t)+off);
3065f8829a4aSRandall Stewart 	} else {
3066f8829a4aSRandall Stewart 		/* else, it spans more than one mbuf, so save a temp copy... */
3067f8829a4aSRandall Stewart 		while ((m != NULL) && (len > 0)) {
3068139bc87fSRandall Stewart 			count = min(SCTP_BUF_LEN(m) - off, len);
30695ba7f91fSMichael Tuexen 			memcpy(ptr, mtod(m, caddr_t)+off, count);
3070f8829a4aSRandall Stewart 			len -= count;
3071f8829a4aSRandall Stewart 			ptr += count;
3072f8829a4aSRandall Stewart 			off = 0;
3073139bc87fSRandall Stewart 			m = SCTP_BUF_NEXT(m);
3074f8829a4aSRandall Stewart 		}
3075f8829a4aSRandall Stewart 		if ((m == NULL) && (len > 0))
3076f8829a4aSRandall Stewart 			return (NULL);
3077f8829a4aSRandall Stewart 		else
3078f8829a4aSRandall Stewart 			return ((caddr_t)in_ptr);
3079f8829a4aSRandall Stewart 	}
3080f8829a4aSRandall Stewart }
3081f8829a4aSRandall Stewart 
3082f8829a4aSRandall Stewart struct sctp_paramhdr *
3083f8829a4aSRandall Stewart sctp_get_next_param(struct mbuf *m,
3084f8829a4aSRandall Stewart     int offset,
3085f8829a4aSRandall Stewart     struct sctp_paramhdr *pull,
3086f8829a4aSRandall Stewart     int pull_limit)
3087f8829a4aSRandall Stewart {
3088f8829a4aSRandall Stewart 	/* This just provides a typed signature to Peter's Pull routine */
3089f8829a4aSRandall Stewart 	return ((struct sctp_paramhdr *)sctp_m_getptr(m, offset, pull_limit,
3090f8829a4aSRandall Stewart 	    (uint8_t *)pull));
3091f8829a4aSRandall Stewart }
3092f8829a4aSRandall Stewart 
3093ce11b842SMichael Tuexen struct mbuf *
3094f8829a4aSRandall Stewart sctp_add_pad_tombuf(struct mbuf *m, int padlen)
3095f8829a4aSRandall Stewart {
3096ce11b842SMichael Tuexen 	struct mbuf *m_last;
3097ce11b842SMichael Tuexen 	caddr_t dp;
3098f8829a4aSRandall Stewart 
3099f8829a4aSRandall Stewart 	if (padlen > 3) {
3100ce11b842SMichael Tuexen 		return (NULL);
3101f8829a4aSRandall Stewart 	}
310241eee555SRandall Stewart 	if (padlen <= M_TRAILINGSPACE(m)) {
3103f8829a4aSRandall Stewart 		/*
3104f8829a4aSRandall Stewart 		 * The easy way. We hope the majority of the time we hit
3105f8829a4aSRandall Stewart 		 * here :)
3106f8829a4aSRandall Stewart 		 */
3107ce11b842SMichael Tuexen 		m_last = m;
3108f8829a4aSRandall Stewart 	} else {
3109ce11b842SMichael Tuexen 		/* Hard way we must grow the mbuf chain */
3110ce11b842SMichael Tuexen 		m_last = sctp_get_mbuf_for_msg(padlen, 0, M_NOWAIT, 1, MT_DATA);
3111ce11b842SMichael Tuexen 		if (m_last == NULL) {
3112ce11b842SMichael Tuexen 			return (NULL);
3113f8829a4aSRandall Stewart 		}
3114ce11b842SMichael Tuexen 		SCTP_BUF_LEN(m_last) = 0;
3115ce11b842SMichael Tuexen 		SCTP_BUF_NEXT(m_last) = NULL;
3116ce11b842SMichael Tuexen 		SCTP_BUF_NEXT(m) = m_last;
3117f8829a4aSRandall Stewart 	}
3118ce11b842SMichael Tuexen 	dp = mtod(m_last, caddr_t)+SCTP_BUF_LEN(m_last);
3119ce11b842SMichael Tuexen 	SCTP_BUF_LEN(m_last) += padlen;
3120ce11b842SMichael Tuexen 	memset(dp, 0, padlen);
3121ce11b842SMichael Tuexen 	return (m_last);
3122f8829a4aSRandall Stewart }
3123f8829a4aSRandall Stewart 
3124ce11b842SMichael Tuexen struct mbuf *
3125f8829a4aSRandall Stewart sctp_pad_lastmbuf(struct mbuf *m, int padval, struct mbuf *last_mbuf)
3126f8829a4aSRandall Stewart {
3127f8829a4aSRandall Stewart 	/* find the last mbuf in chain and pad it */
3128f8829a4aSRandall Stewart 	struct mbuf *m_at;
3129f8829a4aSRandall Stewart 
3130ce11b842SMichael Tuexen 	if (last_mbuf != NULL) {
3131f8829a4aSRandall Stewart 		return (sctp_add_pad_tombuf(last_mbuf, padval));
3132f8829a4aSRandall Stewart 	} else {
313317267b32SMichael Tuexen 		for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3134139bc87fSRandall Stewart 			if (SCTP_BUF_NEXT(m_at) == NULL) {
3135f8829a4aSRandall Stewart 				return (sctp_add_pad_tombuf(m_at, padval));
3136f8829a4aSRandall Stewart 			}
3137f8829a4aSRandall Stewart 		}
3138f8829a4aSRandall Stewart 	}
3139ce11b842SMichael Tuexen 	return (NULL);
3140f8829a4aSRandall Stewart }
3141f8829a4aSRandall Stewart 
3142f8829a4aSRandall Stewart static void
3143c5b5675dSMichael Tuexen sctp_notify_assoc_change(uint16_t state, struct sctp_tcb *stcb,
314428397ac1SMichael Tuexen     uint16_t error, struct sctp_abort_chunk *abort, uint8_t from_peer, int so_locked)
3145f8829a4aSRandall Stewart {
3146f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3147f8829a4aSRandall Stewart 	struct sctp_assoc_change *sac;
3148f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
31499a8e3088SMichael Tuexen 	unsigned int notif_len;
31509a8e3088SMichael Tuexen 	uint16_t abort_len;
3151e06b67c7SMichael Tuexen 	unsigned int i;
3152ceaad40aSRandall Stewart 
315359713bbfSMichael Tuexen 	if (stcb == NULL) {
315459713bbfSMichael Tuexen 		return;
315559713bbfSMichael Tuexen 	}
315658411b08SMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT)) {
31579a8e3088SMichael Tuexen 		notif_len = (unsigned int)sizeof(struct sctp_assoc_change);
3158a2b42326SMichael Tuexen 		if (abort != NULL) {
3159c9eb4473SMichael Tuexen 			abort_len = ntohs(abort->ch.chunk_length);
31609669e724SMichael Tuexen 			/*
31619669e724SMichael Tuexen 			 * Only SCTP_CHUNK_BUFFER_SIZE are guaranteed to be
316245d41de5SMichael Tuexen 			 * contiguous.
31639669e724SMichael Tuexen 			 */
31649669e724SMichael Tuexen 			if (abort_len > SCTP_CHUNK_BUFFER_SIZE) {
31659669e724SMichael Tuexen 				abort_len = SCTP_CHUNK_BUFFER_SIZE;
31669669e724SMichael Tuexen 			}
3167a2b42326SMichael Tuexen 		} else {
3168a2b42326SMichael Tuexen 			abort_len = 0;
3169c5b5675dSMichael Tuexen 		}
3170a2b42326SMichael Tuexen 		if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
3171a2b42326SMichael Tuexen 			notif_len += SCTP_ASSOC_SUPPORTS_MAX;
3172a2b42326SMichael Tuexen 		} else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
3173a2b42326SMichael Tuexen 			notif_len += abort_len;
3174a2b42326SMichael Tuexen 		}
3175eb1b1807SGleb Smirnoff 		m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3176a2b42326SMichael Tuexen 		if (m_notify == NULL) {
3177a2b42326SMichael Tuexen 			/* Retry with smaller value. */
31789a8e3088SMichael Tuexen 			notif_len = (unsigned int)sizeof(struct sctp_assoc_change);
3179eb1b1807SGleb Smirnoff 			m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3180a2b42326SMichael Tuexen 			if (m_notify == NULL) {
318158411b08SMichael Tuexen 				goto set_error;
3182a2b42326SMichael Tuexen 			}
3183a2b42326SMichael Tuexen 		}
3184a2b42326SMichael Tuexen 		SCTP_BUF_NEXT(m_notify) = NULL;
3185f8829a4aSRandall Stewart 		sac = mtod(m_notify, struct sctp_assoc_change *);
3186e432298aSXin LI 		memset(sac, 0, notif_len);
3187f8829a4aSRandall Stewart 		sac->sac_type = SCTP_ASSOC_CHANGE;
3188f8829a4aSRandall Stewart 		sac->sac_flags = 0;
3189f8829a4aSRandall Stewart 		sac->sac_length = sizeof(struct sctp_assoc_change);
3190c5b5675dSMichael Tuexen 		sac->sac_state = state;
3191f8829a4aSRandall Stewart 		sac->sac_error = error;
3192f8829a4aSRandall Stewart 		/* XXX verify these stream counts */
3193f8829a4aSRandall Stewart 		sac->sac_outbound_streams = stcb->asoc.streamoutcnt;
3194f8829a4aSRandall Stewart 		sac->sac_inbound_streams = stcb->asoc.streamincnt;
3195f8829a4aSRandall Stewart 		sac->sac_assoc_id = sctp_get_associd(stcb);
3196a2b42326SMichael Tuexen 		if (notif_len > sizeof(struct sctp_assoc_change)) {
3197c5b5675dSMichael Tuexen 			if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
3198e06b67c7SMichael Tuexen 				i = 0;
3199c79bec9cSMichael Tuexen 				if (stcb->asoc.prsctp_supported == 1) {
3200e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_PR;
3201e06b67c7SMichael Tuexen 				}
3202c79bec9cSMichael Tuexen 				if (stcb->asoc.auth_supported == 1) {
3203e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_AUTH;
3204e06b67c7SMichael Tuexen 				}
3205c79bec9cSMichael Tuexen 				if (stcb->asoc.asconf_supported == 1) {
3206e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_ASCONF;
3207e06b67c7SMichael Tuexen 				}
320844249214SRandall Stewart 				if (stcb->asoc.idata_supported == 1) {
320944249214SRandall Stewart 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_INTERLEAVING;
321044249214SRandall Stewart 				}
3211e06b67c7SMichael Tuexen 				sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_MULTIBUF;
3212c79bec9cSMichael Tuexen 				if (stcb->asoc.reconfig_supported == 1) {
3213e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_RE_CONFIG;
3214e06b67c7SMichael Tuexen 				}
3215e06b67c7SMichael Tuexen 				sac->sac_length += i;
3216a2b42326SMichael Tuexen 			} else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
3217a2b42326SMichael Tuexen 				memcpy(sac->sac_info, abort, abort_len);
3218a2b42326SMichael Tuexen 				sac->sac_length += abort_len;
3219a2b42326SMichael Tuexen 			}
3220c5b5675dSMichael Tuexen 		}
3221e06b67c7SMichael Tuexen 		SCTP_BUF_LEN(m_notify) = sac->sac_length;
3222f8829a4aSRandall Stewart 		control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
32237215cc1bSMichael Tuexen 		    0, 0, stcb->asoc.context, 0, 0, 0,
3224f8829a4aSRandall Stewart 		    m_notify);
322558411b08SMichael Tuexen 		if (control != NULL) {
3226139bc87fSRandall Stewart 			control->length = SCTP_BUF_LEN(m_notify);
322728cd0699SMichael Tuexen 			control->spec_flags = M_NOTIFICATION;
3228f8829a4aSRandall Stewart 			/* not that we need this */
3229f8829a4aSRandall Stewart 			control->tail_mbuf = m_notify;
3230f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep, stcb,
3231f8829a4aSRandall Stewart 			    control,
3232cfde3ff7SRandall Stewart 			    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD,
3233cfde3ff7SRandall Stewart 			    so_locked);
323458411b08SMichael Tuexen 		} else {
323558411b08SMichael Tuexen 			sctp_m_freem(m_notify);
323658411b08SMichael Tuexen 		}
323758411b08SMichael Tuexen 	}
323858411b08SMichael Tuexen 	/*
323958411b08SMichael Tuexen 	 * For 1-to-1 style sockets, we send up and error when an ABORT
324058411b08SMichael Tuexen 	 * comes in.
324158411b08SMichael Tuexen 	 */
324258411b08SMichael Tuexen set_error:
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))) {
3246e045904fSMichael Tuexen 		SOCK_LOCK(stcb->sctp_socket);
3247410a3b1eSMichael Tuexen 		if (from_peer) {
3248839d21d6SMichael Tuexen 			if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) {
324958411b08SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNREFUSED);
325058411b08SMichael Tuexen 				stcb->sctp_socket->so_error = ECONNREFUSED;
325158411b08SMichael Tuexen 			} else {
325258411b08SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
325358411b08SMichael Tuexen 				stcb->sctp_socket->so_error = ECONNRESET;
325458411b08SMichael Tuexen 			}
3255410a3b1eSMichael Tuexen 		} else {
3256839d21d6SMichael Tuexen 			if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
3257839d21d6SMichael Tuexen 			    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
3258553bb068SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ETIMEDOUT);
3259553bb068SMichael Tuexen 				stcb->sctp_socket->so_error = ETIMEDOUT;
3260553bb068SMichael Tuexen 			} else {
3261410a3b1eSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNABORTED);
3262410a3b1eSMichael Tuexen 				stcb->sctp_socket->so_error = ECONNABORTED;
3263410a3b1eSMichael Tuexen 			}
326458411b08SMichael Tuexen 		}
32653acfe1e1SGleb Smirnoff 		SOCK_UNLOCK(stcb->sctp_socket);
3266553bb068SMichael Tuexen 	}
326758411b08SMichael Tuexen 	/* Wake ANY sleepers */
326858411b08SMichael Tuexen 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
326958411b08SMichael Tuexen 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
327058411b08SMichael Tuexen 	    ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
32713acfe1e1SGleb Smirnoff 		socantrcvmore(stcb->sctp_socket);
327258411b08SMichael Tuexen 	}
327358411b08SMichael Tuexen 	sorwakeup(stcb->sctp_socket);
327458411b08SMichael Tuexen 	sowwakeup(stcb->sctp_socket);
3275f8829a4aSRandall Stewart }
3276f8829a4aSRandall Stewart 
3277f8829a4aSRandall Stewart static void
3278f8829a4aSRandall Stewart sctp_notify_peer_addr_change(struct sctp_tcb *stcb, uint32_t state,
327928397ac1SMichael Tuexen     struct sockaddr *sa, uint32_t error, int so_locked)
3280f8829a4aSRandall Stewart {
3281f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3282f8829a4aSRandall Stewart 	struct sctp_paddr_change *spc;
3283f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3284f8829a4aSRandall Stewart 
328560990c0cSMichael Tuexen 	if ((stcb == NULL) ||
328660990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT)) {
3287f8829a4aSRandall Stewart 		/* event not enabled */
3288f8829a4aSRandall Stewart 		return;
3289830d754dSRandall Stewart 	}
3290eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_paddr_change), 0, M_NOWAIT, 1, MT_DATA);
3291f8829a4aSRandall Stewart 	if (m_notify == NULL)
3292f8829a4aSRandall Stewart 		return;
3293139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3294f8829a4aSRandall Stewart 	spc = mtod(m_notify, struct sctp_paddr_change *);
329556711f94SMichael Tuexen 	memset(spc, 0, sizeof(struct sctp_paddr_change));
3296f8829a4aSRandall Stewart 	spc->spc_type = SCTP_PEER_ADDR_CHANGE;
3297f8829a4aSRandall Stewart 	spc->spc_flags = 0;
3298f8829a4aSRandall Stewart 	spc->spc_length = sizeof(struct sctp_paddr_change);
32995e2c2d87SRandall Stewart 	switch (sa->sa_family) {
3300ea5eba11SMichael Tuexen #ifdef INET
33015e2c2d87SRandall Stewart 	case AF_INET:
3302d59107f7SMichael Tuexen #ifdef INET6
3303d59107f7SMichael Tuexen 		if (sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
3304d59107f7SMichael Tuexen 			in6_sin_2_v4mapsin6((struct sockaddr_in *)sa,
3305d59107f7SMichael Tuexen 			    (struct sockaddr_in6 *)&spc->spc_aaddr);
3306d59107f7SMichael Tuexen 		} else {
3307f8829a4aSRandall Stewart 			memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
3308d59107f7SMichael Tuexen 		}
3309d59107f7SMichael Tuexen #else
3310d59107f7SMichael Tuexen 		memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
3311d59107f7SMichael Tuexen #endif
33125e2c2d87SRandall Stewart 		break;
3313ea5eba11SMichael Tuexen #endif
33145e2c2d87SRandall Stewart #ifdef INET6
33155e2c2d87SRandall Stewart 	case AF_INET6:
33165e2c2d87SRandall Stewart 		{
3317f42a358aSRandall Stewart 			struct sockaddr_in6 *sin6;
3318f42a358aSRandall Stewart 
3319f8829a4aSRandall Stewart 			memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in6));
3320f42a358aSRandall Stewart 
3321f42a358aSRandall Stewart 			sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
3322f42a358aSRandall Stewart 			if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
332342551e99SRandall Stewart 				if (sin6->sin6_scope_id == 0) {
332442551e99SRandall Stewart 					/* recover scope_id for user */
3325f42a358aSRandall Stewart 					(void)sa6_recoverscope(sin6);
332642551e99SRandall Stewart 				} else {
332742551e99SRandall Stewart 					/* clear embedded scope_id for user */
332842551e99SRandall Stewart 					in6_clearscope(&sin6->sin6_addr);
332942551e99SRandall Stewart 				}
3330f42a358aSRandall Stewart 			}
33315e2c2d87SRandall Stewart 			break;
33325e2c2d87SRandall Stewart 		}
33335e2c2d87SRandall Stewart #endif
33345e2c2d87SRandall Stewart 	default:
33355e2c2d87SRandall Stewart 		/* TSNH */
33365e2c2d87SRandall Stewart 		break;
3337f8829a4aSRandall Stewart 	}
3338f8829a4aSRandall Stewart 	spc->spc_state = state;
3339f8829a4aSRandall Stewart 	spc->spc_error = error;
3340f8829a4aSRandall Stewart 	spc->spc_assoc_id = sctp_get_associd(stcb);
3341f8829a4aSRandall Stewart 
3342139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_paddr_change);
3343139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3344f8829a4aSRandall Stewart 
3345f8829a4aSRandall Stewart 	/* append to socket */
3346f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
33477215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3348f8829a4aSRandall Stewart 	    m_notify);
3349f8829a4aSRandall Stewart 	if (control == NULL) {
3350f8829a4aSRandall Stewart 		/* no memory */
3351f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3352f8829a4aSRandall Stewart 		return;
3353f8829a4aSRandall Stewart 	}
3354139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3355139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3356f8829a4aSRandall Stewart 	/* not that we need this */
3357f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3358f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3359f8829a4aSRandall Stewart 	    control,
3360cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1,
3361cfde3ff7SRandall Stewart 	    SCTP_READ_LOCK_NOT_HELD,
33623cb3567dSMichael Tuexen 	    so_locked);
3363f8829a4aSRandall Stewart }
3364f8829a4aSRandall Stewart 
3365f8829a4aSRandall Stewart static void
33661edc9dbaSMichael Tuexen sctp_notify_send_failed(struct sctp_tcb *stcb, uint8_t sent, uint32_t error,
336728397ac1SMichael Tuexen     struct sctp_tmit_chunk *chk, int so_locked)
3368f8829a4aSRandall Stewart {
3369830d754dSRandall Stewart 	struct mbuf *m_notify;
3370f8829a4aSRandall Stewart 	struct sctp_send_failed *ssf;
33719935403aSMichael Tuexen 	struct sctp_send_failed_event *ssfe;
3372f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3373ab337314SMichael Tuexen 	struct sctp_chunkhdr *chkhdr;
3374ab337314SMichael Tuexen 	int notifhdr_len, chk_len, chkhdr_len, padding_len, payload_len;
3375f8829a4aSRandall Stewart 
337660990c0cSMichael Tuexen 	if ((stcb == NULL) ||
33779935403aSMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
33789935403aSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
3379f8829a4aSRandall Stewart 		/* event not enabled */
3380f8829a4aSRandall Stewart 		return;
3381830d754dSRandall Stewart 	}
33820053ed28SMichael Tuexen 
33839935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3384ab337314SMichael Tuexen 		notifhdr_len = sizeof(struct sctp_send_failed_event);
33859935403aSMichael Tuexen 	} else {
3386ab337314SMichael Tuexen 		notifhdr_len = sizeof(struct sctp_send_failed);
33879935403aSMichael Tuexen 	}
3388ab337314SMichael Tuexen 	m_notify = sctp_get_mbuf_for_msg(notifhdr_len, 0, M_NOWAIT, 1, MT_DATA);
3389f8829a4aSRandall Stewart 	if (m_notify == NULL)
3390f8829a4aSRandall Stewart 		/* no space left */
3391f8829a4aSRandall Stewart 		return;
3392ab337314SMichael Tuexen 	SCTP_BUF_LEN(m_notify) = notifhdr_len;
3393ab337314SMichael Tuexen 	if (stcb->asoc.idata_supported) {
3394ab337314SMichael Tuexen 		chkhdr_len = sizeof(struct sctp_idata_chunk);
3395ab337314SMichael Tuexen 	} else {
3396ab337314SMichael Tuexen 		chkhdr_len = sizeof(struct sctp_data_chunk);
3397ab337314SMichael Tuexen 	}
3398ab337314SMichael Tuexen 	/* Use some defaults in case we can't access the chunk header */
3399ab337314SMichael Tuexen 	if (chk->send_size >= chkhdr_len) {
3400ab337314SMichael Tuexen 		payload_len = chk->send_size - chkhdr_len;
3401ab337314SMichael Tuexen 	} else {
3402ab337314SMichael Tuexen 		payload_len = 0;
3403ab337314SMichael Tuexen 	}
3404ab337314SMichael Tuexen 	padding_len = 0;
3405ab337314SMichael Tuexen 	if (chk->data != NULL) {
3406ab337314SMichael Tuexen 		chkhdr = mtod(chk->data, struct sctp_chunkhdr *);
3407ab337314SMichael Tuexen 		if (chkhdr != NULL) {
3408ab337314SMichael Tuexen 			chk_len = ntohs(chkhdr->chunk_length);
3409ab337314SMichael Tuexen 			if ((chk_len >= chkhdr_len) &&
3410ab337314SMichael Tuexen 			    (chk->send_size >= chk_len) &&
3411ab337314SMichael Tuexen 			    (chk->send_size - chk_len < 4)) {
3412ab337314SMichael Tuexen 				padding_len = chk->send_size - chk_len;
3413ab337314SMichael Tuexen 				payload_len = chk->send_size - chkhdr_len - padding_len;
3414ab337314SMichael Tuexen 			}
3415ab337314SMichael Tuexen 		}
3416ab337314SMichael Tuexen 	}
34179935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
34189935403aSMichael Tuexen 		ssfe = mtod(m_notify, struct sctp_send_failed_event *);
3419ab337314SMichael Tuexen 		memset(ssfe, 0, notifhdr_len);
34209935403aSMichael Tuexen 		ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
34211edc9dbaSMichael Tuexen 		if (sent) {
34229935403aSMichael Tuexen 			ssfe->ssfe_flags = SCTP_DATA_SENT;
34231edc9dbaSMichael Tuexen 		} else {
34241edc9dbaSMichael Tuexen 			ssfe->ssfe_flags = SCTP_DATA_UNSENT;
34251edc9dbaSMichael Tuexen 		}
3426ab337314SMichael Tuexen 		ssfe->ssfe_length = (uint32_t)(notifhdr_len + payload_len);
34279935403aSMichael Tuexen 		ssfe->ssfe_error = error;
34289935403aSMichael Tuexen 		/* not exactly what the user sent in, but should be close :) */
342949656eefSMichael Tuexen 		ssfe->ssfe_info.snd_sid = chk->rec.data.sid;
34309935403aSMichael Tuexen 		ssfe->ssfe_info.snd_flags = chk->rec.data.rcv_flags;
343149656eefSMichael Tuexen 		ssfe->ssfe_info.snd_ppid = chk->rec.data.ppid;
34329935403aSMichael Tuexen 		ssfe->ssfe_info.snd_context = chk->rec.data.context;
34339935403aSMichael Tuexen 		ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
34349935403aSMichael Tuexen 		ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
34359935403aSMichael Tuexen 	} else {
3436f8829a4aSRandall Stewart 		ssf = mtod(m_notify, struct sctp_send_failed *);
3437ab337314SMichael Tuexen 		memset(ssf, 0, notifhdr_len);
3438f8829a4aSRandall Stewart 		ssf->ssf_type = SCTP_SEND_FAILED;
34391edc9dbaSMichael Tuexen 		if (sent) {
3440f8829a4aSRandall Stewart 			ssf->ssf_flags = SCTP_DATA_SENT;
34411edc9dbaSMichael Tuexen 		} else {
34421edc9dbaSMichael Tuexen 			ssf->ssf_flags = SCTP_DATA_UNSENT;
34431edc9dbaSMichael Tuexen 		}
3444ab337314SMichael Tuexen 		ssf->ssf_length = (uint32_t)(notifhdr_len + payload_len);
3445f8829a4aSRandall Stewart 		ssf->ssf_error = error;
3446f8829a4aSRandall Stewart 		/* not exactly what the user sent in, but should be close :) */
344749656eefSMichael Tuexen 		ssf->ssf_info.sinfo_stream = chk->rec.data.sid;
344849656eefSMichael Tuexen 		ssf->ssf_info.sinfo_ssn = (uint16_t)chk->rec.data.mid;
3449f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_flags = chk->rec.data.rcv_flags;
345049656eefSMichael Tuexen 		ssf->ssf_info.sinfo_ppid = chk->rec.data.ppid;
3451f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_context = chk->rec.data.context;
3452f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3453f8829a4aSRandall Stewart 		ssf->ssf_assoc_id = sctp_get_associd(stcb);
34549935403aSMichael Tuexen 	}
3455ab337314SMichael Tuexen 	if (chk->data != NULL) {
3456ab337314SMichael Tuexen 		/* Trim off the sctp chunk header (it should be there) */
3457ab337314SMichael Tuexen 		if (chk->send_size == chkhdr_len + payload_len + padding_len) {
3458ab337314SMichael Tuexen 			m_adj(chk->data, chkhdr_len);
3459ab337314SMichael Tuexen 			m_adj(chk->data, -padding_len);
3460830d754dSRandall Stewart 			sctp_mbuf_crush(chk->data);
3461ab337314SMichael Tuexen 			chk->send_size -= (chkhdr_len + padding_len);
3462830d754dSRandall Stewart 		}
3463830d754dSRandall Stewart 	}
3464810ec536SMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = chk->data;
3465f8829a4aSRandall Stewart 	/* Steal off the mbuf */
3466f8829a4aSRandall Stewart 	chk->data = NULL;
3467f8829a4aSRandall Stewart 	/*
3468f8829a4aSRandall Stewart 	 * For this case, we check the actual socket buffer, since the assoc
3469f8829a4aSRandall Stewart 	 * is going away we don't want to overfill the socket buffer for a
3470f8829a4aSRandall Stewart 	 * non-reader
3471f8829a4aSRandall Stewart 	 */
3472139bc87fSRandall Stewart 	if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3473f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3474f8829a4aSRandall Stewart 		return;
3475f8829a4aSRandall Stewart 	}
3476f8829a4aSRandall Stewart 	/* append to socket */
3477f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
34787215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3479f8829a4aSRandall Stewart 	    m_notify);
3480f8829a4aSRandall Stewart 	if (control == NULL) {
3481f8829a4aSRandall Stewart 		/* no memory */
3482f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3483f8829a4aSRandall Stewart 		return;
3484f8829a4aSRandall Stewart 	}
348528cd0699SMichael Tuexen 	control->length = SCTP_BUF_LEN(m_notify);
3486139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
348728cd0699SMichael Tuexen 	/* not that we need this */
348828cd0699SMichael Tuexen 	control->tail_mbuf = m_notify;
3489f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3490f8829a4aSRandall Stewart 	    control,
3491cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1,
3492cfde3ff7SRandall Stewart 	    SCTP_READ_LOCK_NOT_HELD,
3493cfde3ff7SRandall Stewart 	    so_locked);
3494f8829a4aSRandall Stewart }
3495f8829a4aSRandall Stewart 
3496f8829a4aSRandall Stewart static void
3497f8829a4aSRandall Stewart sctp_notify_send_failed2(struct sctp_tcb *stcb, uint32_t error,
349828397ac1SMichael Tuexen     struct sctp_stream_queue_pending *sp, int so_locked)
3499f8829a4aSRandall Stewart {
3500f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3501f8829a4aSRandall Stewart 	struct sctp_send_failed *ssf;
35029935403aSMichael Tuexen 	struct sctp_send_failed_event *ssfe;
3503f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3504ab337314SMichael Tuexen 	int notifhdr_len;
3505f8829a4aSRandall Stewart 
350660990c0cSMichael Tuexen 	if ((stcb == NULL) ||
35079935403aSMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
35089935403aSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
3509f8829a4aSRandall Stewart 		/* event not enabled */
3510f8829a4aSRandall Stewart 		return;
3511830d754dSRandall Stewart 	}
35129935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3513ab337314SMichael Tuexen 		notifhdr_len = sizeof(struct sctp_send_failed_event);
35149935403aSMichael Tuexen 	} else {
3515ab337314SMichael Tuexen 		notifhdr_len = sizeof(struct sctp_send_failed);
35169935403aSMichael Tuexen 	}
3517ab337314SMichael Tuexen 	m_notify = sctp_get_mbuf_for_msg(notifhdr_len, 0, M_NOWAIT, 1, MT_DATA);
35189935403aSMichael Tuexen 	if (m_notify == NULL) {
3519f8829a4aSRandall Stewart 		/* no space left */
3520f8829a4aSRandall Stewart 		return;
35219935403aSMichael Tuexen 	}
3522ab337314SMichael Tuexen 	SCTP_BUF_LEN(m_notify) = notifhdr_len;
35239935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
35249935403aSMichael Tuexen 		ssfe = mtod(m_notify, struct sctp_send_failed_event *);
3525ab337314SMichael Tuexen 		memset(ssfe, 0, notifhdr_len);
3526ad83c8a5SMichael Tuexen 		ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
35279935403aSMichael Tuexen 		ssfe->ssfe_flags = SCTP_DATA_UNSENT;
3528ab337314SMichael Tuexen 		ssfe->ssfe_length = (uint32_t)(notifhdr_len + sp->length);
35299935403aSMichael Tuexen 		ssfe->ssfe_error = error;
35309935403aSMichael Tuexen 		/* not exactly what the user sent in, but should be close :) */
353149656eefSMichael Tuexen 		ssfe->ssfe_info.snd_sid = sp->sid;
35329935403aSMichael Tuexen 		if (sp->some_taken) {
35339935403aSMichael Tuexen 			ssfe->ssfe_info.snd_flags = SCTP_DATA_LAST_FRAG;
35349935403aSMichael Tuexen 		} else {
35359935403aSMichael Tuexen 			ssfe->ssfe_info.snd_flags = SCTP_DATA_NOT_FRAG;
35369935403aSMichael Tuexen 		}
35379935403aSMichael Tuexen 		ssfe->ssfe_info.snd_ppid = sp->ppid;
35389935403aSMichael Tuexen 		ssfe->ssfe_info.snd_context = sp->context;
35399935403aSMichael Tuexen 		ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
35409935403aSMichael Tuexen 		ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
35419935403aSMichael Tuexen 	} else {
3542f8829a4aSRandall Stewart 		ssf = mtod(m_notify, struct sctp_send_failed *);
3543ab337314SMichael Tuexen 		memset(ssf, 0, notifhdr_len);
3544f8829a4aSRandall Stewart 		ssf->ssf_type = SCTP_SEND_FAILED;
3545f8829a4aSRandall Stewart 		ssf->ssf_flags = SCTP_DATA_UNSENT;
3546ab337314SMichael Tuexen 		ssf->ssf_length = (uint32_t)(notifhdr_len + sp->length);
3547f8829a4aSRandall Stewart 		ssf->ssf_error = error;
3548f8829a4aSRandall Stewart 		/* not exactly what the user sent in, but should be close :) */
354949656eefSMichael Tuexen 		ssf->ssf_info.sinfo_stream = sp->sid;
3550f3b05218SMichael Tuexen 		ssf->ssf_info.sinfo_ssn = 0;
3551fc14de76SRandall Stewart 		if (sp->some_taken) {
3552fc14de76SRandall Stewart 			ssf->ssf_info.sinfo_flags = SCTP_DATA_LAST_FRAG;
3553fc14de76SRandall Stewart 		} else {
3554fc14de76SRandall Stewart 			ssf->ssf_info.sinfo_flags = SCTP_DATA_NOT_FRAG;
3555fc14de76SRandall Stewart 		}
3556f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_ppid = sp->ppid;
3557f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_context = sp->context;
3558f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3559f8829a4aSRandall Stewart 		ssf->ssf_assoc_id = sctp_get_associd(stcb);
35609935403aSMichael Tuexen 	}
35619935403aSMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = sp->data;
3562f8829a4aSRandall Stewart 
3563f8829a4aSRandall Stewart 	/* Steal off the mbuf */
3564f8829a4aSRandall Stewart 	sp->data = NULL;
3565f8829a4aSRandall Stewart 	/*
3566f8829a4aSRandall Stewart 	 * For this case, we check the actual socket buffer, since the assoc
3567f8829a4aSRandall Stewart 	 * is going away we don't want to overfill the socket buffer for a
3568f8829a4aSRandall Stewart 	 * non-reader
3569f8829a4aSRandall Stewart 	 */
3570139bc87fSRandall Stewart 	if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3571f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3572f8829a4aSRandall Stewart 		return;
3573f8829a4aSRandall Stewart 	}
3574f8829a4aSRandall Stewart 	/* append to socket */
3575f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
35767215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3577f8829a4aSRandall Stewart 	    m_notify);
3578f8829a4aSRandall Stewart 	if (control == NULL) {
3579f8829a4aSRandall Stewart 		/* no memory */
3580f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3581f8829a4aSRandall Stewart 		return;
3582f8829a4aSRandall Stewart 	}
358328cd0699SMichael Tuexen 	control->length = SCTP_BUF_LEN(m_notify);
3584139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
358528cd0699SMichael Tuexen 	/* not that we need this */
358628cd0699SMichael Tuexen 	control->tail_mbuf = m_notify;
3587f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3588f8829a4aSRandall Stewart 	    control,
3589cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3590f8829a4aSRandall Stewart }
3591f8829a4aSRandall Stewart 
3592f8829a4aSRandall Stewart static void
35937215cc1bSMichael Tuexen sctp_notify_adaptation_layer(struct sctp_tcb *stcb)
3594f8829a4aSRandall Stewart {
3595f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3596f8829a4aSRandall Stewart 	struct sctp_adaptation_event *sai;
3597f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3598f8829a4aSRandall Stewart 
359960990c0cSMichael Tuexen 	if ((stcb == NULL) ||
360060990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT)) {
3601f8829a4aSRandall Stewart 		/* event not enabled */
3602f8829a4aSRandall Stewart 		return;
3603830d754dSRandall Stewart 	}
36040053ed28SMichael Tuexen 
3605eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 0, M_NOWAIT, 1, MT_DATA);
3606f8829a4aSRandall Stewart 	if (m_notify == NULL)
3607f8829a4aSRandall Stewart 		/* no space left */
3608f8829a4aSRandall Stewart 		return;
3609139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3610f8829a4aSRandall Stewart 	sai = mtod(m_notify, struct sctp_adaptation_event *);
3611e432298aSXin LI 	memset(sai, 0, sizeof(struct sctp_adaptation_event));
3612f8829a4aSRandall Stewart 	sai->sai_type = SCTP_ADAPTATION_INDICATION;
3613f8829a4aSRandall Stewart 	sai->sai_flags = 0;
3614f8829a4aSRandall Stewart 	sai->sai_length = sizeof(struct sctp_adaptation_event);
36152afb3e84SRandall Stewart 	sai->sai_adaptation_ind = stcb->asoc.peers_adaptation;
3616f8829a4aSRandall Stewart 	sai->sai_assoc_id = sctp_get_associd(stcb);
3617f8829a4aSRandall Stewart 
3618139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_adaptation_event);
3619139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3620f8829a4aSRandall Stewart 
3621f8829a4aSRandall Stewart 	/* append to socket */
3622f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
36237215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3624f8829a4aSRandall Stewart 	    m_notify);
3625f8829a4aSRandall Stewart 	if (control == NULL) {
3626f8829a4aSRandall Stewart 		/* no memory */
3627f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3628f8829a4aSRandall Stewart 		return;
3629f8829a4aSRandall Stewart 	}
3630139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3631139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3632f8829a4aSRandall Stewart 	/* not that we need this */
3633f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3634f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3635f8829a4aSRandall Stewart 	    control,
3636cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3637f8829a4aSRandall Stewart }
3638f8829a4aSRandall Stewart 
363903b0b021SRandall Stewart /* This always must be called with the read-queue LOCKED in the INP */
3640810ec536SMichael Tuexen static void
36412dad8a55SRandall Stewart sctp_notify_partial_delivery_indication(struct sctp_tcb *stcb, uint32_t error,
364228397ac1SMichael Tuexen     uint32_t val, int so_locked)
3643f8829a4aSRandall Stewart {
3644f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3645f8829a4aSRandall Stewart 	struct sctp_pdapi_event *pdapi;
3646f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
364703b0b021SRandall Stewart 	struct sockbuf *sb;
3648f8829a4aSRandall Stewart 
364960990c0cSMichael Tuexen 	if ((stcb == NULL) ||
365060990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_PDAPIEVNT)) {
3651f8829a4aSRandall Stewart 		/* event not enabled */
3652f8829a4aSRandall Stewart 		return;
3653830d754dSRandall Stewart 	}
3654cd1386abSMichael Tuexen 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
3655cd1386abSMichael Tuexen 		return;
3656cd1386abSMichael Tuexen 	}
36570053ed28SMichael Tuexen 
3658eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_pdapi_event), 0, M_NOWAIT, 1, MT_DATA);
3659f8829a4aSRandall Stewart 	if (m_notify == NULL)
3660f8829a4aSRandall Stewart 		/* no space left */
3661f8829a4aSRandall Stewart 		return;
3662139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3663f8829a4aSRandall Stewart 	pdapi = mtod(m_notify, struct sctp_pdapi_event *);
3664e432298aSXin LI 	memset(pdapi, 0, sizeof(struct sctp_pdapi_event));
3665f8829a4aSRandall Stewart 	pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
3666f8829a4aSRandall Stewart 	pdapi->pdapi_flags = 0;
3667f8829a4aSRandall Stewart 	pdapi->pdapi_length = sizeof(struct sctp_pdapi_event);
3668f8829a4aSRandall Stewart 	pdapi->pdapi_indication = error;
36699a6142d8SRandall Stewart 	pdapi->pdapi_stream = (val >> 16);
36709a6142d8SRandall Stewart 	pdapi->pdapi_seq = (val & 0x0000ffff);
3671f8829a4aSRandall Stewart 	pdapi->pdapi_assoc_id = sctp_get_associd(stcb);
3672f8829a4aSRandall Stewart 
3673139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_pdapi_event);
3674139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3675f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
36767215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3677f8829a4aSRandall Stewart 	    m_notify);
3678f8829a4aSRandall Stewart 	if (control == NULL) {
3679f8829a4aSRandall Stewart 		/* no memory */
3680f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3681f8829a4aSRandall Stewart 		return;
3682f8829a4aSRandall Stewart 	}
3683139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
368428cd0699SMichael Tuexen 	control->spec_flags = M_NOTIFICATION;
3685f8829a4aSRandall Stewart 	/* not that we need this */
3686f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
368703b0b021SRandall Stewart 	sb = &stcb->sctp_socket->so_rcv;
3688b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3689139bc87fSRandall Stewart 		sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m_notify));
369080fefe0aSRandall Stewart 	}
369103b0b021SRandall Stewart 	sctp_sballoc(stcb, sb, m_notify);
3692b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
369303b0b021SRandall Stewart 		sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
369480fefe0aSRandall Stewart 	}
369503b0b021SRandall Stewart 	control->end_added = 1;
369603b0b021SRandall Stewart 	if (stcb->asoc.control_pdapi)
369703b0b021SRandall Stewart 		TAILQ_INSERT_AFTER(&stcb->sctp_ep->read_queue, stcb->asoc.control_pdapi, control, next);
369803b0b021SRandall Stewart 	else {
369903b0b021SRandall Stewart 		/* we really should not see this case */
370003b0b021SRandall Stewart 		TAILQ_INSERT_TAIL(&stcb->sctp_ep->read_queue, control, next);
370103b0b021SRandall Stewart 	}
370203b0b021SRandall Stewart 	if (stcb->sctp_ep && stcb->sctp_socket) {
370303b0b021SRandall Stewart 		/* This should always be the case */
370403b0b021SRandall Stewart 		sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
3705f8829a4aSRandall Stewart 	}
3706f8829a4aSRandall Stewart }
3707f8829a4aSRandall Stewart 
3708f8829a4aSRandall Stewart static void
3709f8829a4aSRandall Stewart sctp_notify_shutdown_event(struct sctp_tcb *stcb)
3710f8829a4aSRandall Stewart {
3711f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3712f8829a4aSRandall Stewart 	struct sctp_shutdown_event *sse;
3713f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3714f8829a4aSRandall Stewart 
3715f8829a4aSRandall Stewart 	/*
3716f8829a4aSRandall Stewart 	 * For TCP model AND UDP connected sockets we will send an error up
3717f8829a4aSRandall Stewart 	 * when an SHUTDOWN completes
3718f8829a4aSRandall Stewart 	 */
3719f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3720f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3721f8829a4aSRandall Stewart 		/* mark socket closed for read/write and wakeup! */
3722f8829a4aSRandall Stewart 		socantsendmore(stcb->sctp_socket);
3723f8829a4aSRandall Stewart 	}
3724e2e7c62eSMichael Tuexen 	if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)) {
3725f8829a4aSRandall Stewart 		/* event not enabled */
3726f8829a4aSRandall Stewart 		return;
3727830d754dSRandall Stewart 	}
37280053ed28SMichael Tuexen 
3729eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_event), 0, M_NOWAIT, 1, MT_DATA);
3730f8829a4aSRandall Stewart 	if (m_notify == NULL)
3731f8829a4aSRandall Stewart 		/* no space left */
3732f8829a4aSRandall Stewart 		return;
3733f8829a4aSRandall Stewart 	sse = mtod(m_notify, struct sctp_shutdown_event *);
3734e432298aSXin LI 	memset(sse, 0, sizeof(struct sctp_shutdown_event));
3735f8829a4aSRandall Stewart 	sse->sse_type = SCTP_SHUTDOWN_EVENT;
3736f8829a4aSRandall Stewart 	sse->sse_flags = 0;
3737f8829a4aSRandall Stewart 	sse->sse_length = sizeof(struct sctp_shutdown_event);
3738f8829a4aSRandall Stewart 	sse->sse_assoc_id = sctp_get_associd(stcb);
3739f8829a4aSRandall Stewart 
3740139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_shutdown_event);
3741139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3742f8829a4aSRandall Stewart 
3743f8829a4aSRandall Stewart 	/* append to socket */
3744f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
37457215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3746f8829a4aSRandall Stewart 	    m_notify);
3747f8829a4aSRandall Stewart 	if (control == NULL) {
3748f8829a4aSRandall Stewart 		/* no memory */
3749f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3750f8829a4aSRandall Stewart 		return;
3751f8829a4aSRandall Stewart 	}
3752139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
375328cd0699SMichael Tuexen 	control->spec_flags = M_NOTIFICATION;
3754f8829a4aSRandall Stewart 	/* not that we need this */
3755f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3756f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3757f8829a4aSRandall Stewart 	    control,
3758cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3759f8829a4aSRandall Stewart }
3760f8829a4aSRandall Stewart 
3761f8829a4aSRandall Stewart static void
3762830d754dSRandall Stewart sctp_notify_sender_dry_event(struct sctp_tcb *stcb,
376328397ac1SMichael Tuexen     int so_locked)
3764830d754dSRandall Stewart {
3765830d754dSRandall Stewart 	struct mbuf *m_notify;
3766830d754dSRandall Stewart 	struct sctp_sender_dry_event *event;
3767830d754dSRandall Stewart 	struct sctp_queued_to_read *control;
3768830d754dSRandall Stewart 
376960990c0cSMichael Tuexen 	if ((stcb == NULL) ||
377060990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DRYEVNT)) {
3771830d754dSRandall Stewart 		/* event not enabled */
3772830d754dSRandall Stewart 		return;
3773830d754dSRandall Stewart 	}
37740053ed28SMichael Tuexen 
3775eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_sender_dry_event), 0, M_NOWAIT, 1, MT_DATA);
3776830d754dSRandall Stewart 	if (m_notify == NULL) {
3777830d754dSRandall Stewart 		/* no space left */
3778830d754dSRandall Stewart 		return;
3779830d754dSRandall Stewart 	}
3780830d754dSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3781830d754dSRandall Stewart 	event = mtod(m_notify, struct sctp_sender_dry_event *);
3782e432298aSXin LI 	memset(event, 0, sizeof(struct sctp_sender_dry_event));
3783830d754dSRandall Stewart 	event->sender_dry_type = SCTP_SENDER_DRY_EVENT;
3784830d754dSRandall Stewart 	event->sender_dry_flags = 0;
3785830d754dSRandall Stewart 	event->sender_dry_length = sizeof(struct sctp_sender_dry_event);
3786830d754dSRandall Stewart 	event->sender_dry_assoc_id = sctp_get_associd(stcb);
3787830d754dSRandall Stewart 
3788830d754dSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_sender_dry_event);
3789830d754dSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3790830d754dSRandall Stewart 
3791830d754dSRandall Stewart 	/* append to socket */
3792830d754dSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
37937215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
37947215cc1bSMichael Tuexen 	    m_notify);
3795830d754dSRandall Stewart 	if (control == NULL) {
3796830d754dSRandall Stewart 		/* no memory */
3797830d754dSRandall Stewart 		sctp_m_freem(m_notify);
3798830d754dSRandall Stewart 		return;
3799830d754dSRandall Stewart 	}
3800830d754dSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3801830d754dSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3802830d754dSRandall Stewart 	/* not that we need this */
3803830d754dSRandall Stewart 	control->tail_mbuf = m_notify;
3804830d754dSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3805cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3806830d754dSRandall Stewart }
3807830d754dSRandall Stewart 
3808c4e848b7SRandall Stewart void
3809c4e848b7SRandall Stewart sctp_notify_stream_reset_add(struct sctp_tcb *stcb, uint16_t numberin, uint16_t numberout, int flag)
3810ea44232bSRandall Stewart {
3811ea44232bSRandall Stewart 	struct mbuf *m_notify;
3812ea44232bSRandall Stewart 	struct sctp_queued_to_read *control;
3813c4e848b7SRandall Stewart 	struct sctp_stream_change_event *stradd;
3814ea44232bSRandall Stewart 
38158c501e51SMichael Tuexen 	if ((stcb == NULL) ||
38168c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_CHANGEEVNT))) {
3817ea44232bSRandall Stewart 		/* event not enabled */
3818ea44232bSRandall Stewart 		return;
3819ea44232bSRandall Stewart 	}
3820c4e848b7SRandall Stewart 	if ((stcb->asoc.peer_req_out) && flag) {
3821c4e848b7SRandall Stewart 		/* Peer made the request, don't tell the local user */
3822c4e848b7SRandall Stewart 		stcb->asoc.peer_req_out = 0;
3823c4e848b7SRandall Stewart 		return;
3824c4e848b7SRandall Stewart 	}
3825c4e848b7SRandall Stewart 	stcb->asoc.peer_req_out = 0;
3826e432298aSXin LI 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_stream_change_event), 0, M_NOWAIT, 1, MT_DATA);
3827ea44232bSRandall Stewart 	if (m_notify == NULL)
3828ea44232bSRandall Stewart 		/* no space left */
3829ea44232bSRandall Stewart 		return;
3830ea44232bSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3831c4e848b7SRandall Stewart 	stradd = mtod(m_notify, struct sctp_stream_change_event *);
3832e432298aSXin LI 	memset(stradd, 0, sizeof(struct sctp_stream_change_event));
3833c4e848b7SRandall Stewart 	stradd->strchange_type = SCTP_STREAM_CHANGE_EVENT;
3834c4e848b7SRandall Stewart 	stradd->strchange_flags = flag;
3835e432298aSXin LI 	stradd->strchange_length = sizeof(struct sctp_stream_change_event);
3836c4e848b7SRandall Stewart 	stradd->strchange_assoc_id = sctp_get_associd(stcb);
3837c4e848b7SRandall Stewart 	stradd->strchange_instrms = numberin;
3838c4e848b7SRandall Stewart 	stradd->strchange_outstrms = numberout;
3839e432298aSXin LI 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_stream_change_event);
3840ea44232bSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3841ea44232bSRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3842ea44232bSRandall Stewart 		/* no space */
3843ea44232bSRandall Stewart 		sctp_m_freem(m_notify);
3844ea44232bSRandall Stewart 		return;
3845ea44232bSRandall Stewart 	}
3846ea44232bSRandall Stewart 	/* append to socket */
3847ea44232bSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
38487215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3849ea44232bSRandall Stewart 	    m_notify);
3850ea44232bSRandall Stewart 	if (control == NULL) {
3851ea44232bSRandall Stewart 		/* no memory */
3852ea44232bSRandall Stewart 		sctp_m_freem(m_notify);
3853ea44232bSRandall Stewart 		return;
3854ea44232bSRandall Stewart 	}
3855ea44232bSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
385628cd0699SMichael Tuexen 	control->spec_flags = M_NOTIFICATION;
3857ea44232bSRandall Stewart 	/* not that we need this */
3858ea44232bSRandall Stewart 	control->tail_mbuf = m_notify;
3859ea44232bSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3860ea44232bSRandall Stewart 	    control,
3861cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3862ea44232bSRandall Stewart }
3863ea44232bSRandall Stewart 
3864c4e848b7SRandall Stewart void
3865c4e848b7SRandall Stewart sctp_notify_stream_reset_tsn(struct sctp_tcb *stcb, uint32_t sending_tsn, uint32_t recv_tsn, int flag)
3866c4e848b7SRandall Stewart {
3867c4e848b7SRandall Stewart 	struct mbuf *m_notify;
3868c4e848b7SRandall Stewart 	struct sctp_queued_to_read *control;
3869c4e848b7SRandall Stewart 	struct sctp_assoc_reset_event *strasoc;
3870c4e848b7SRandall Stewart 
38718c501e51SMichael Tuexen 	if ((stcb == NULL) ||
38728c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ASSOC_RESETEVNT))) {
3873c4e848b7SRandall Stewart 		/* event not enabled */
3874c4e848b7SRandall Stewart 		return;
3875c4e848b7SRandall Stewart 	}
3876e432298aSXin LI 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_assoc_reset_event), 0, M_NOWAIT, 1, MT_DATA);
3877c4e848b7SRandall Stewart 	if (m_notify == NULL)
3878c4e848b7SRandall Stewart 		/* no space left */
3879c4e848b7SRandall Stewart 		return;
3880c4e848b7SRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3881c4e848b7SRandall Stewart 	strasoc = mtod(m_notify, struct sctp_assoc_reset_event *);
3882e432298aSXin LI 	memset(strasoc, 0, sizeof(struct sctp_assoc_reset_event));
3883c4e848b7SRandall Stewart 	strasoc->assocreset_type = SCTP_ASSOC_RESET_EVENT;
3884c4e848b7SRandall Stewart 	strasoc->assocreset_flags = flag;
3885e432298aSXin LI 	strasoc->assocreset_length = sizeof(struct sctp_assoc_reset_event);
3886c4e848b7SRandall Stewart 	strasoc->assocreset_assoc_id = sctp_get_associd(stcb);
3887c4e848b7SRandall Stewart 	strasoc->assocreset_local_tsn = sending_tsn;
3888c4e848b7SRandall Stewart 	strasoc->assocreset_remote_tsn = recv_tsn;
3889e432298aSXin LI 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_assoc_reset_event);
3890c4e848b7SRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3891c4e848b7SRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3892c4e848b7SRandall Stewart 		/* no space */
3893c4e848b7SRandall Stewart 		sctp_m_freem(m_notify);
3894c4e848b7SRandall Stewart 		return;
3895c4e848b7SRandall Stewart 	}
3896c4e848b7SRandall Stewart 	/* append to socket */
3897c4e848b7SRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3898c4e848b7SRandall Stewart 	    0, 0, stcb->asoc.context, 0, 0, 0,
3899c4e848b7SRandall Stewart 	    m_notify);
3900c4e848b7SRandall Stewart 	if (control == NULL) {
3901c4e848b7SRandall Stewart 		/* no memory */
3902c4e848b7SRandall Stewart 		sctp_m_freem(m_notify);
3903c4e848b7SRandall Stewart 		return;
3904c4e848b7SRandall Stewart 	}
3905c4e848b7SRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
390628cd0699SMichael Tuexen 	control->spec_flags = M_NOTIFICATION;
3907c4e848b7SRandall Stewart 	/* not that we need this */
3908c4e848b7SRandall Stewart 	control->tail_mbuf = m_notify;
3909c4e848b7SRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3910c4e848b7SRandall Stewart 	    control,
3911c4e848b7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3912c4e848b7SRandall Stewart }
3913c4e848b7SRandall Stewart 
3914830d754dSRandall Stewart static void
3915f8829a4aSRandall Stewart sctp_notify_stream_reset(struct sctp_tcb *stcb,
3916f8829a4aSRandall Stewart     int number_entries, uint16_t *list, int flag)
3917f8829a4aSRandall Stewart {
3918f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3919f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3920f8829a4aSRandall Stewart 	struct sctp_stream_reset_event *strreset;
3921f8829a4aSRandall Stewart 	int len;
3922f8829a4aSRandall Stewart 
39238c501e51SMichael Tuexen 	if ((stcb == NULL) ||
39248c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT))) {
3925f8829a4aSRandall Stewart 		/* event not enabled */
3926f8829a4aSRandall Stewart 		return;
3927830d754dSRandall Stewart 	}
39280053ed28SMichael Tuexen 
3929eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
3930f8829a4aSRandall Stewart 	if (m_notify == NULL)
3931f8829a4aSRandall Stewart 		/* no space left */
3932f8829a4aSRandall Stewart 		return;
3933139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3934f8829a4aSRandall Stewart 	len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
3935f8829a4aSRandall Stewart 	if (len > M_TRAILINGSPACE(m_notify)) {
3936f8829a4aSRandall Stewart 		/* never enough room */
3937f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3938f8829a4aSRandall Stewart 		return;
3939f8829a4aSRandall Stewart 	}
3940f8829a4aSRandall Stewart 	strreset = mtod(m_notify, struct sctp_stream_reset_event *);
3941e432298aSXin LI 	memset(strreset, 0, len);
3942f8829a4aSRandall Stewart 	strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
3943c4e848b7SRandall Stewart 	strreset->strreset_flags = flag;
3944f8829a4aSRandall Stewart 	strreset->strreset_length = len;
3945f8829a4aSRandall Stewart 	strreset->strreset_assoc_id = sctp_get_associd(stcb);
3946f8829a4aSRandall Stewart 	if (number_entries) {
3947f8829a4aSRandall Stewart 		int i;
3948f8829a4aSRandall Stewart 
3949f8829a4aSRandall Stewart 		for (i = 0; i < number_entries; i++) {
3950c4e848b7SRandall Stewart 			strreset->strreset_stream_list[i] = ntohs(list[i]);
3951f8829a4aSRandall Stewart 		}
3952f8829a4aSRandall Stewart 	}
3953139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = len;
3954139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3955139bc87fSRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3956f8829a4aSRandall Stewart 		/* no space */
3957f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3958f8829a4aSRandall Stewart 		return;
3959f8829a4aSRandall Stewart 	}
3960f8829a4aSRandall Stewart 	/* append to socket */
3961f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
39627215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3963f8829a4aSRandall Stewart 	    m_notify);
3964f8829a4aSRandall Stewart 	if (control == NULL) {
3965f8829a4aSRandall Stewart 		/* no memory */
3966f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3967f8829a4aSRandall Stewart 		return;
3968f8829a4aSRandall Stewart 	}
3969139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
397028cd0699SMichael Tuexen 	control->spec_flags = M_NOTIFICATION;
3971f8829a4aSRandall Stewart 	/* not that we need this */
3972f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3973f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3974f8829a4aSRandall Stewart 	    control,
3975cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3976f8829a4aSRandall Stewart }
3977f8829a4aSRandall Stewart 
3978389b1b11SMichael Tuexen static void
3979389b1b11SMichael Tuexen sctp_notify_remote_error(struct sctp_tcb *stcb, uint16_t error, struct sctp_error_chunk *chunk)
3980389b1b11SMichael Tuexen {
3981389b1b11SMichael Tuexen 	struct mbuf *m_notify;
3982389b1b11SMichael Tuexen 	struct sctp_remote_error *sre;
3983389b1b11SMichael Tuexen 	struct sctp_queued_to_read *control;
39849a8e3088SMichael Tuexen 	unsigned int notif_len;
39859a8e3088SMichael Tuexen 	uint16_t chunk_len;
3986389b1b11SMichael Tuexen 
3987389b1b11SMichael Tuexen 	if ((stcb == NULL) ||
3988389b1b11SMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPEERERR)) {
3989389b1b11SMichael Tuexen 		return;
3990389b1b11SMichael Tuexen 	}
3991389b1b11SMichael Tuexen 	if (chunk != NULL) {
3992c9eb4473SMichael Tuexen 		chunk_len = ntohs(chunk->ch.chunk_length);
39939669e724SMichael Tuexen 		/*
39949669e724SMichael Tuexen 		 * Only SCTP_CHUNK_BUFFER_SIZE are guaranteed to be
399545d41de5SMichael Tuexen 		 * contiguous.
39969669e724SMichael Tuexen 		 */
39979669e724SMichael Tuexen 		if (chunk_len > SCTP_CHUNK_BUFFER_SIZE) {
39989669e724SMichael Tuexen 			chunk_len = SCTP_CHUNK_BUFFER_SIZE;
39999669e724SMichael Tuexen 		}
4000389b1b11SMichael Tuexen 	} else {
4001389b1b11SMichael Tuexen 		chunk_len = 0;
4002389b1b11SMichael Tuexen 	}
40039a8e3088SMichael Tuexen 	notif_len = (unsigned int)(sizeof(struct sctp_remote_error) + chunk_len);
4004eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
4005389b1b11SMichael Tuexen 	if (m_notify == NULL) {
4006389b1b11SMichael Tuexen 		/* Retry with smaller value. */
40079a8e3088SMichael Tuexen 		notif_len = (unsigned int)sizeof(struct sctp_remote_error);
4008eb1b1807SGleb Smirnoff 		m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
4009389b1b11SMichael Tuexen 		if (m_notify == NULL) {
4010389b1b11SMichael Tuexen 			return;
4011389b1b11SMichael Tuexen 		}
4012389b1b11SMichael Tuexen 	}
4013389b1b11SMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = NULL;
4014389b1b11SMichael Tuexen 	sre = mtod(m_notify, struct sctp_remote_error *);
401556711f94SMichael Tuexen 	memset(sre, 0, notif_len);
4016389b1b11SMichael Tuexen 	sre->sre_type = SCTP_REMOTE_ERROR;
4017389b1b11SMichael Tuexen 	sre->sre_flags = 0;
4018389b1b11SMichael Tuexen 	sre->sre_length = sizeof(struct sctp_remote_error);
4019389b1b11SMichael Tuexen 	sre->sre_error = error;
4020389b1b11SMichael Tuexen 	sre->sre_assoc_id = sctp_get_associd(stcb);
4021389b1b11SMichael Tuexen 	if (notif_len > sizeof(struct sctp_remote_error)) {
4022389b1b11SMichael Tuexen 		memcpy(sre->sre_data, chunk, chunk_len);
4023389b1b11SMichael Tuexen 		sre->sre_length += chunk_len;
4024389b1b11SMichael Tuexen 	}
4025389b1b11SMichael Tuexen 	SCTP_BUF_LEN(m_notify) = sre->sre_length;
4026389b1b11SMichael Tuexen 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
4027389b1b11SMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
4028389b1b11SMichael Tuexen 	    m_notify);
4029389b1b11SMichael Tuexen 	if (control != NULL) {
4030389b1b11SMichael Tuexen 		control->length = SCTP_BUF_LEN(m_notify);
403128cd0699SMichael Tuexen 		control->spec_flags = M_NOTIFICATION;
4032389b1b11SMichael Tuexen 		/* not that we need this */
4033389b1b11SMichael Tuexen 		control->tail_mbuf = m_notify;
4034389b1b11SMichael Tuexen 		sctp_add_to_readq(stcb->sctp_ep, stcb,
4035389b1b11SMichael Tuexen 		    control,
4036389b1b11SMichael Tuexen 		    &stcb->sctp_socket->so_rcv, 1,
4037389b1b11SMichael Tuexen 		    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
4038389b1b11SMichael Tuexen 	} else {
4039389b1b11SMichael Tuexen 		sctp_m_freem(m_notify);
4040389b1b11SMichael Tuexen 	}
4041389b1b11SMichael Tuexen }
4042389b1b11SMichael Tuexen 
4043f8829a4aSRandall Stewart void
4044f8829a4aSRandall Stewart sctp_ulp_notify(uint32_t notification, struct sctp_tcb *stcb,
404528397ac1SMichael Tuexen     uint32_t error, void *data, int so_locked)
4046f8829a4aSRandall Stewart {
4047830d754dSRandall Stewart 	if ((stcb == NULL) ||
4048830d754dSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4049f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4050830d754dSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
4051830d754dSRandall Stewart 		/* If the socket is gone we are out of here */
4052f8829a4aSRandall Stewart 		return;
4053f8829a4aSRandall Stewart 	}
4054a99b6783SRandall Stewart 	if (stcb->sctp_socket->so_rcv.sb_state & SBS_CANTRCVMORE) {
4055a99b6783SRandall Stewart 		return;
4056a99b6783SRandall Stewart 	}
4057839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
4058839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
405917205eccSRandall Stewart 		if ((notification == SCTP_NOTIFY_INTERFACE_DOWN) ||
406017205eccSRandall Stewart 		    (notification == SCTP_NOTIFY_INTERFACE_UP) ||
406117205eccSRandall Stewart 		    (notification == SCTP_NOTIFY_INTERFACE_CONFIRMED)) {
406217205eccSRandall Stewart 			/* Don't report these in front states */
406317205eccSRandall Stewart 			return;
406417205eccSRandall Stewart 		}
406517205eccSRandall Stewart 	}
4066f8829a4aSRandall Stewart 	switch (notification) {
4067f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_UP:
4068f8829a4aSRandall Stewart 		if (stcb->asoc.assoc_up_sent == 0) {
4069410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_UP, stcb, error, NULL, 0, so_locked);
4070f8829a4aSRandall Stewart 			stcb->asoc.assoc_up_sent = 1;
4071f8829a4aSRandall Stewart 		}
40722afb3e84SRandall Stewart 		if (stcb->asoc.adaptation_needed && (stcb->asoc.adaptation_sent == 0)) {
40737215cc1bSMichael Tuexen 			sctp_notify_adaptation_layer(stcb);
40742afb3e84SRandall Stewart 		}
4075c79bec9cSMichael Tuexen 		if (stcb->asoc.auth_supported == 0) {
4076830d754dSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
4077830d754dSRandall Stewart 			    NULL, so_locked);
4078830d754dSRandall Stewart 		}
4079f8829a4aSRandall Stewart 		break;
4080f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_DOWN:
4081410a3b1eSMichael Tuexen 		sctp_notify_assoc_change(SCTP_SHUTDOWN_COMP, stcb, error, NULL, 0, so_locked);
4082f8829a4aSRandall Stewart 		break;
4083f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_DOWN:
4084f8829a4aSRandall Stewart 		{
4085f8829a4aSRandall Stewart 			struct sctp_nets *net;
4086f8829a4aSRandall Stewart 
4087f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
4088f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_UNREACHABLE,
40893cb3567dSMichael Tuexen 			    (struct sockaddr *)&net->ro._l_addr, error, so_locked);
4090f8829a4aSRandall Stewart 			break;
4091f8829a4aSRandall Stewart 		}
4092f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_UP:
4093f8829a4aSRandall Stewart 		{
4094f8829a4aSRandall Stewart 			struct sctp_nets *net;
4095f8829a4aSRandall Stewart 
4096f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
4097f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_AVAILABLE,
40983cb3567dSMichael Tuexen 			    (struct sockaddr *)&net->ro._l_addr, error, so_locked);
4099f8829a4aSRandall Stewart 			break;
4100f8829a4aSRandall Stewart 		}
4101f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_CONFIRMED:
4102f8829a4aSRandall Stewart 		{
4103f8829a4aSRandall Stewart 			struct sctp_nets *net;
4104f8829a4aSRandall Stewart 
4105f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
4106f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_CONFIRMED,
41073cb3567dSMichael Tuexen 			    (struct sockaddr *)&net->ro._l_addr, error, so_locked);
4108f8829a4aSRandall Stewart 			break;
4109f8829a4aSRandall Stewart 		}
4110f8829a4aSRandall Stewart 	case SCTP_NOTIFY_SPECIAL_SP_FAIL:
4111f8829a4aSRandall Stewart 		sctp_notify_send_failed2(stcb, error,
4112ceaad40aSRandall Stewart 		    (struct sctp_stream_queue_pending *)data, so_locked);
4113f8829a4aSRandall Stewart 		break;
41141edc9dbaSMichael Tuexen 	case SCTP_NOTIFY_SENT_DG_FAIL:
41151edc9dbaSMichael Tuexen 		sctp_notify_send_failed(stcb, 1, error,
41161edc9dbaSMichael Tuexen 		    (struct sctp_tmit_chunk *)data, so_locked);
41171edc9dbaSMichael Tuexen 		break;
41181edc9dbaSMichael Tuexen 	case SCTP_NOTIFY_UNSENT_DG_FAIL:
41191edc9dbaSMichael Tuexen 		sctp_notify_send_failed(stcb, 0, error,
4120ceaad40aSRandall Stewart 		    (struct sctp_tmit_chunk *)data, so_locked);
4121f8829a4aSRandall Stewart 		break;
4122f8829a4aSRandall Stewart 	case SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION:
41239a6142d8SRandall Stewart 		{
41249a6142d8SRandall Stewart 			uint32_t val;
41259a6142d8SRandall Stewart 
41269a6142d8SRandall Stewart 			val = *((uint32_t *)data);
41279a6142d8SRandall Stewart 
4128810ec536SMichael Tuexen 			sctp_notify_partial_delivery_indication(stcb, error, val, so_locked);
4129f8829a4aSRandall Stewart 			break;
4130810ec536SMichael Tuexen 		}
4131410a3b1eSMichael Tuexen 	case SCTP_NOTIFY_ASSOC_LOC_ABORTED:
4132839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
4133839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
4134410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 0, so_locked);
4135c105859eSRandall Stewart 		} else {
4136410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 0, so_locked);
4137410a3b1eSMichael Tuexen 		}
4138410a3b1eSMichael Tuexen 		break;
4139410a3b1eSMichael Tuexen 	case SCTP_NOTIFY_ASSOC_REM_ABORTED:
4140839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
4141839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
4142410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 1, so_locked);
4143410a3b1eSMichael Tuexen 		} else {
4144410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 1, so_locked);
4145c105859eSRandall Stewart 		}
4146f8829a4aSRandall Stewart 		break;
4147f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_RESTART:
4148410a3b1eSMichael Tuexen 		sctp_notify_assoc_change(SCTP_RESTART, stcb, error, NULL, 0, so_locked);
4149c79bec9cSMichael Tuexen 		if (stcb->asoc.auth_supported == 0) {
4150830d754dSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
4151830d754dSRandall Stewart 			    NULL, so_locked);
4152830d754dSRandall Stewart 		}
4153f8829a4aSRandall Stewart 		break;
4154f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_SEND:
4155d7714577SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *)data), SCTP_STREAM_RESET_OUTGOING_SSN);
4156f8829a4aSRandall Stewart 		break;
4157f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_RECV:
4158d7714577SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *)data), SCTP_STREAM_RESET_INCOMING);
4159f8829a4aSRandall Stewart 		break;
4160f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_FAILED_OUT:
4161c4e848b7SRandall Stewart 		sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
4162d7714577SMichael Tuexen 		    (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_FAILED));
4163f8829a4aSRandall Stewart 		break;
4164d4260646SMichael Tuexen 	case SCTP_NOTIFY_STR_RESET_DENIED_OUT:
4165d4260646SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
4166d4260646SMichael Tuexen 		    (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_DENIED));
4167d4260646SMichael Tuexen 		break;
4168f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_FAILED_IN:
4169c4e848b7SRandall Stewart 		sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
4170d7714577SMichael Tuexen 		    (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_FAILED));
4171f8829a4aSRandall Stewart 		break;
4172d4260646SMichael Tuexen 	case SCTP_NOTIFY_STR_RESET_DENIED_IN:
4173d4260646SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *)data),
4174d4260646SMichael Tuexen 		    (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_DENIED));
4175d4260646SMichael Tuexen 		break;
4176f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_ADD_IP:
4177f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_ADDED, data,
41783cb3567dSMichael Tuexen 		    error, so_locked);
4179f8829a4aSRandall Stewart 		break;
4180f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_DELETE_IP:
4181f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_REMOVED, data,
41823cb3567dSMichael Tuexen 		    error, so_locked);
4183f8829a4aSRandall Stewart 		break;
4184f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_SET_PRIMARY:
4185f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_MADE_PRIM, data,
41863cb3567dSMichael Tuexen 		    error, so_locked);
4187f8829a4aSRandall Stewart 		break;
4188f8829a4aSRandall Stewart 	case SCTP_NOTIFY_PEER_SHUTDOWN:
4189f8829a4aSRandall Stewart 		sctp_notify_shutdown_event(stcb);
4190f8829a4aSRandall Stewart 		break;
4191f8829a4aSRandall Stewart 	case SCTP_NOTIFY_AUTH_NEW_KEY:
419278f28045SMichael Tuexen 		sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY, error,
4193830d754dSRandall Stewart 		    (uint16_t)(uintptr_t)data,
4194830d754dSRandall Stewart 		    so_locked);
4195f8829a4aSRandall Stewart 		break;
4196830d754dSRandall Stewart 	case SCTP_NOTIFY_AUTH_FREE_KEY:
4197830d754dSRandall Stewart 		sctp_notify_authentication(stcb, SCTP_AUTH_FREE_KEY, error,
4198830d754dSRandall Stewart 		    (uint16_t)(uintptr_t)data,
4199830d754dSRandall Stewart 		    so_locked);
4200f8829a4aSRandall Stewart 		break;
4201830d754dSRandall Stewart 	case SCTP_NOTIFY_NO_PEER_AUTH:
4202830d754dSRandall Stewart 		sctp_notify_authentication(stcb, SCTP_AUTH_NO_AUTH, error,
4203830d754dSRandall Stewart 		    (uint16_t)(uintptr_t)data,
4204830d754dSRandall Stewart 		    so_locked);
4205830d754dSRandall Stewart 		break;
4206830d754dSRandall Stewart 	case SCTP_NOTIFY_SENDER_DRY:
4207830d754dSRandall Stewart 		sctp_notify_sender_dry_event(stcb, so_locked);
4208830d754dSRandall Stewart 		break;
4209389b1b11SMichael Tuexen 	case SCTP_NOTIFY_REMOTE_ERROR:
4210389b1b11SMichael Tuexen 		sctp_notify_remote_error(stcb, error, data);
4211389b1b11SMichael Tuexen 		break;
4212f8829a4aSRandall Stewart 	default:
4213ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_UTIL1, "%s: unknown notification %xh (%u)\n",
42146e9c45e0SMichael Tuexen 		    __func__, notification, notification);
4215f8829a4aSRandall Stewart 		break;
4216f8829a4aSRandall Stewart 	}			/* end switch */
4217f8829a4aSRandall Stewart }
4218f8829a4aSRandall Stewart 
4219f8829a4aSRandall Stewart void
4220f5d30f7fSMichael Tuexen sctp_report_all_outbound(struct sctp_tcb *stcb, uint16_t error, int so_locked)
4221f8829a4aSRandall Stewart {
4222f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4223f8829a4aSRandall Stewart 	struct sctp_stream_out *outs;
42244a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
42254a9ef3f8SMichael Tuexen 	struct sctp_stream_queue_pending *sp, *nsp;
42267f34832bSRandall Stewart 	int i;
4227f8829a4aSRandall Stewart 
4228ad81507eSRandall Stewart 	if (stcb == NULL) {
4229ad81507eSRandall Stewart 		return;
4230ad81507eSRandall Stewart 	}
42314a9ef3f8SMichael Tuexen 	asoc = &stcb->asoc;
42324a9ef3f8SMichael Tuexen 	if (asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4233478fbccbSRandall Stewart 		/* already being freed */
4234478fbccbSRandall Stewart 		return;
4235478fbccbSRandall Stewart 	}
4236f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4237f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
42384a9ef3f8SMichael Tuexen 	    (asoc->state & SCTP_STATE_CLOSED_SOCKET)) {
4239f8829a4aSRandall Stewart 		return;
4240f8829a4aSRandall Stewart 	}
4241f8829a4aSRandall Stewart 	/* now through all the gunk freeing chunks */
4242d00aff5dSRandall Stewart 	/* sent queue SHOULD be empty */
42434a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
4244d00aff5dSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
4245d00aff5dSRandall Stewart 		asoc->sent_queue_cnt--;
4246325c8c46SMichael Tuexen 		if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
424749656eefSMichael Tuexen 			if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
424849656eefSMichael Tuexen 				asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
4249a7ad6026SMichael Tuexen #ifdef INVARIANTS
4250a7ad6026SMichael Tuexen 			} else {
425149656eefSMichael Tuexen 				panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
4252a7ad6026SMichael Tuexen #endif
4253a7ad6026SMichael Tuexen 			}
4254a7ad6026SMichael Tuexen 		}
42550c0982b8SRandall Stewart 		if (chk->data != NULL) {
4256d00aff5dSRandall Stewart 			sctp_free_bufspace(stcb, asoc, chk, 1);
42571edc9dbaSMichael Tuexen 			sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
42581edc9dbaSMichael Tuexen 			    error, chk, so_locked);
4259810ec536SMichael Tuexen 			if (chk->data) {
4260d00aff5dSRandall Stewart 				sctp_m_freem(chk->data);
4261d00aff5dSRandall Stewart 				chk->data = NULL;
4262d00aff5dSRandall Stewart 			}
4263810ec536SMichael Tuexen 		}
4264689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, so_locked);
4265d00aff5dSRandall Stewart 		/* sa_ignore FREED_MEMORY */
4266d00aff5dSRandall Stewart 	}
4267d00aff5dSRandall Stewart 	/* pending send queue SHOULD be empty */
42684a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
4269d00aff5dSRandall Stewart 		TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
4270d00aff5dSRandall Stewart 		asoc->send_queue_cnt--;
427149656eefSMichael Tuexen 		if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
427249656eefSMichael Tuexen 			asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
4273a7ad6026SMichael Tuexen #ifdef INVARIANTS
4274a7ad6026SMichael Tuexen 		} else {
427549656eefSMichael Tuexen 			panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
4276a7ad6026SMichael Tuexen #endif
4277a7ad6026SMichael Tuexen 		}
42780c0982b8SRandall Stewart 		if (chk->data != NULL) {
4279d00aff5dSRandall Stewart 			sctp_free_bufspace(stcb, asoc, chk, 1);
42801edc9dbaSMichael Tuexen 			sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
42811edc9dbaSMichael Tuexen 			    error, chk, so_locked);
4282810ec536SMichael Tuexen 			if (chk->data) {
4283d00aff5dSRandall Stewart 				sctp_m_freem(chk->data);
4284d00aff5dSRandall Stewart 				chk->data = NULL;
4285d00aff5dSRandall Stewart 			}
4286810ec536SMichael Tuexen 		}
4287689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, so_locked);
4288d00aff5dSRandall Stewart 		/* sa_ignore FREED_MEMORY */
4289d00aff5dSRandall Stewart 	}
42904a9ef3f8SMichael Tuexen 	for (i = 0; i < asoc->streamoutcnt; i++) {
42917f34832bSRandall Stewart 		/* For each stream */
42924a9ef3f8SMichael Tuexen 		outs = &asoc->strmout[i];
42937f34832bSRandall Stewart 		/* clean up any sends there */
42944a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
42954d58b0c3SMichael Tuexen 			atomic_subtract_int(&asoc->stream_queue_cnt, 1);
4296f8829a4aSRandall Stewart 			TAILQ_REMOVE(&outs->outqueue, sp, next);
4297d9707e43SMichael Tuexen 			stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, 1);
4298f8829a4aSRandall Stewart 			sctp_free_spbufspace(stcb, asoc, sp);
4299478fbccbSRandall Stewart 			if (sp->data) {
4300f8829a4aSRandall Stewart 				sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
43011edc9dbaSMichael Tuexen 				    error, (void *)sp, so_locked);
4302f8829a4aSRandall Stewart 				if (sp->data) {
4303f8829a4aSRandall Stewart 					sctp_m_freem(sp->data);
4304f8829a4aSRandall Stewart 					sp->data = NULL;
4305d07b2ac6SMichael Tuexen 					sp->tail_mbuf = NULL;
4306d07b2ac6SMichael Tuexen 					sp->length = 0;
4307f8829a4aSRandall Stewart 				}
4308478fbccbSRandall Stewart 			}
43099eea4a2dSMichael Tuexen 			if (sp->net) {
4310f8829a4aSRandall Stewart 				sctp_free_remote_addr(sp->net);
4311f8829a4aSRandall Stewart 				sp->net = NULL;
43129eea4a2dSMichael Tuexen 			}
4313f8829a4aSRandall Stewart 			/* Free the chunk */
4314689e6a5fSMichael Tuexen 			sctp_free_a_strmoq(stcb, sp, so_locked);
43153c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
4316f8829a4aSRandall Stewart 		}
4317f8829a4aSRandall Stewart 	}
4318ad81507eSRandall Stewart }
4319f8829a4aSRandall Stewart 
4320f8829a4aSRandall Stewart void
4321410a3b1eSMichael Tuexen sctp_abort_notification(struct sctp_tcb *stcb, uint8_t from_peer, uint16_t error,
432228397ac1SMichael Tuexen     struct sctp_abort_chunk *abort, int so_locked)
4323f8829a4aSRandall Stewart {
4324ad81507eSRandall Stewart 	if (stcb == NULL) {
4325ad81507eSRandall Stewart 		return;
4326ad81507eSRandall Stewart 	}
4327c55b70ceSMichael Tuexen 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4328c55b70ceSMichael Tuexen 	    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4329c55b70ceSMichael Tuexen 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
4330c55b70ceSMichael Tuexen 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_WAS_ABORTED;
4331c55b70ceSMichael Tuexen 	}
4332f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4333f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4334f8829a4aSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
4335f8829a4aSRandall Stewart 		return;
4336f8829a4aSRandall Stewart 	}
4337f5d30f7fSMichael Tuexen 	SCTP_TCB_SEND_LOCK(stcb);
4338f5d30f7fSMichael Tuexen 	SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_WAS_ABORTED);
4339f8829a4aSRandall Stewart 	/* Tell them we lost the asoc */
4340f5d30f7fSMichael Tuexen 	sctp_report_all_outbound(stcb, error, so_locked);
4341f5d30f7fSMichael Tuexen 	SCTP_TCB_SEND_UNLOCK(stcb);
4342410a3b1eSMichael Tuexen 	if (from_peer) {
4343410a3b1eSMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_REM_ABORTED, stcb, error, abort, so_locked);
4344410a3b1eSMichael Tuexen 	} else {
4345410a3b1eSMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_LOC_ABORTED, stcb, error, abort, so_locked);
4346410a3b1eSMichael Tuexen 	}
4347f8829a4aSRandall Stewart }
4348f8829a4aSRandall Stewart 
4349f8829a4aSRandall Stewart void
4350f8829a4aSRandall Stewart sctp_abort_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4351b1754ad1SMichael Tuexen     struct mbuf *m, int iphlen,
4352b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
4353b1754ad1SMichael Tuexen     struct sctphdr *sh, struct mbuf *op_err,
4354457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
4355c54a18d2SRandall Stewart     uint32_t vrf_id, uint16_t port)
4356f8829a4aSRandall Stewart {
4357f8829a4aSRandall Stewart 	uint32_t vtag;
4358ceaad40aSRandall Stewart 
4359f8829a4aSRandall Stewart 	vtag = 0;
4360f8829a4aSRandall Stewart 	if (stcb != NULL) {
4361f8829a4aSRandall Stewart 		vtag = stcb->asoc.peer_vtag;
436217205eccSRandall Stewart 		vrf_id = stcb->asoc.vrf_id;
4363f8829a4aSRandall Stewart 	}
4364b1754ad1SMichael Tuexen 	sctp_send_abort(m, iphlen, src, dst, sh, vtag, op_err,
4365d089f9b9SMichael Tuexen 	    mflowtype, mflowid, inp->fibnum,
4366f30ac432SMichael Tuexen 	    vrf_id, port);
4367f8829a4aSRandall Stewart 	if (stcb != NULL) {
4368884d8c53SMichael Tuexen 		/* We have a TCB to abort, send notification too */
4369884d8c53SMichael Tuexen 		sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
4370f8829a4aSRandall Stewart 		/* Ok, now lets free it */
43710271d0cdSMichael Tuexen 		SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4372839d21d6SMichael Tuexen 		if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
4373839d21d6SMichael Tuexen 		    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
43740271d0cdSMichael Tuexen 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
43750271d0cdSMichael Tuexen 		}
4376ba785902SMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4377ba785902SMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_4);
4378f8829a4aSRandall Stewart 	}
4379f8829a4aSRandall Stewart }
4380f1f73e57SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
4381f1f73e57SRandall Stewart void
4382f1f73e57SRandall Stewart sctp_print_out_track_log(struct sctp_tcb *stcb)
4383f1f73e57SRandall Stewart {
438418e198d3SRandall Stewart #ifdef NOSIY_PRINTS
4385f1f73e57SRandall Stewart 	int i;
4386f1f73e57SRandall Stewart 
4387ad81507eSRandall Stewart 	SCTP_PRINTF("Last ep reason:%x\n", stcb->sctp_ep->last_abort_code);
4388ad81507eSRandall Stewart 	SCTP_PRINTF("IN bound TSN log-aaa\n");
4389f1f73e57SRandall Stewart 	if ((stcb->asoc.tsn_in_at == 0) && (stcb->asoc.tsn_in_wrapped == 0)) {
4390ad81507eSRandall Stewart 		SCTP_PRINTF("None rcvd\n");
4391f1f73e57SRandall Stewart 		goto none_in;
4392f1f73e57SRandall Stewart 	}
4393f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_in_wrapped) {
4394f1f73e57SRandall Stewart 		for (i = stcb->asoc.tsn_in_at; i < SCTP_TSN_LOG_SIZE; i++) {
4395ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4396f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].tsn,
4397f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].strm,
4398f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].seq,
4399f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].flgs,
4400f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].sz);
4401f1f73e57SRandall Stewart 		}
4402f1f73e57SRandall Stewart 	}
4403f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_in_at) {
4404f1f73e57SRandall Stewart 		for (i = 0; i < stcb->asoc.tsn_in_at; i++) {
4405ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4406f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].tsn,
4407f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].strm,
4408f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].seq,
4409f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].flgs,
4410f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].sz);
4411f1f73e57SRandall Stewart 		}
4412f1f73e57SRandall Stewart 	}
4413f1f73e57SRandall Stewart none_in:
4414ad81507eSRandall Stewart 	SCTP_PRINTF("OUT bound TSN log-aaa\n");
4415ad81507eSRandall Stewart 	if ((stcb->asoc.tsn_out_at == 0) &&
4416ad81507eSRandall Stewart 	    (stcb->asoc.tsn_out_wrapped == 0)) {
4417ad81507eSRandall Stewart 		SCTP_PRINTF("None sent\n");
4418f1f73e57SRandall Stewart 	}
4419f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_out_wrapped) {
4420f1f73e57SRandall Stewart 		for (i = stcb->asoc.tsn_out_at; i < SCTP_TSN_LOG_SIZE; i++) {
4421ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4422f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].tsn,
4423f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].strm,
4424f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].seq,
4425f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].flgs,
4426f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].sz);
4427f1f73e57SRandall Stewart 		}
4428f1f73e57SRandall Stewart 	}
4429f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_out_at) {
4430f1f73e57SRandall Stewart 		for (i = 0; i < stcb->asoc.tsn_out_at; i++) {
4431ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4432f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].tsn,
4433f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].strm,
4434f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].seq,
4435f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].flgs,
4436f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].sz);
4437f1f73e57SRandall Stewart 		}
4438f1f73e57SRandall Stewart 	}
443918e198d3SRandall Stewart #endif
4440f1f73e57SRandall Stewart }
4441f1f73e57SRandall Stewart #endif
4442f1f73e57SRandall Stewart 
4443f8829a4aSRandall Stewart void
4444f8829a4aSRandall Stewart sctp_abort_an_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4445a2b42326SMichael Tuexen     struct mbuf *op_err,
444628397ac1SMichael Tuexen     int so_locked)
4447f8829a4aSRandall Stewart {
4448ceaad40aSRandall Stewart 
4449f8829a4aSRandall Stewart 	if (stcb == NULL) {
4450f8829a4aSRandall Stewart 		/* Got to have a TCB */
4451f8829a4aSRandall Stewart 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4452fe1831e0SMichael Tuexen 			if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4453b0552ae2SRandall Stewart 				sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4454b0552ae2SRandall Stewart 				    SCTP_CALLED_DIRECTLY_NOCMPSET);
4455f8829a4aSRandall Stewart 			}
4456f8829a4aSRandall Stewart 		}
4457f8829a4aSRandall Stewart 		return;
4458f8829a4aSRandall Stewart 	}
4459f8829a4aSRandall Stewart 	/* notify the peer */
4460ceaad40aSRandall Stewart 	sctp_send_abort_tcb(stcb, op_err, so_locked);
4461f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4462839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
4463839d21d6SMichael Tuexen 	    (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4464f8829a4aSRandall Stewart 		SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4465f8829a4aSRandall Stewart 	}
4466884d8c53SMichael Tuexen 	/* notify the ulp */
4467884d8c53SMichael Tuexen 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
4468884d8c53SMichael Tuexen 		sctp_abort_notification(stcb, 0, 0, NULL, so_locked);
4469884d8c53SMichael Tuexen 	}
4470f8829a4aSRandall Stewart 	/* now free the asoc */
4471f1f73e57SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
4472f1f73e57SRandall Stewart 	sctp_print_out_track_log(stcb);
4473f1f73e57SRandall Stewart #endif
4474ba785902SMichael Tuexen 	(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4475ba785902SMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_5);
4476f8829a4aSRandall Stewart }
4477f8829a4aSRandall Stewart 
4478f8829a4aSRandall Stewart void
4479b1754ad1SMichael Tuexen sctp_handle_ootb(struct mbuf *m, int iphlen, int offset,
4480b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
4481b1754ad1SMichael Tuexen     struct sctphdr *sh, struct sctp_inpcb *inp,
4482ff1ffd74SMichael Tuexen     struct mbuf *cause,
4483d089f9b9SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4484f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
4485f8829a4aSRandall Stewart {
4486f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch, chunk_buf;
4487f8829a4aSRandall Stewart 	unsigned int chk_length;
4488c58e60beSMichael Tuexen 	int contains_init_chunk;
4489f8829a4aSRandall Stewart 
4490f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue);
4491f8829a4aSRandall Stewart 	/* Generate a TO address for future reference */
4492f8829a4aSRandall Stewart 	if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4493fe1831e0SMichael Tuexen 		if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4494b0552ae2SRandall Stewart 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4495b0552ae2SRandall Stewart 			    SCTP_CALLED_DIRECTLY_NOCMPSET);
4496f8829a4aSRandall Stewart 		}
4497f8829a4aSRandall Stewart 	}
4498c58e60beSMichael Tuexen 	contains_init_chunk = 0;
4499f8829a4aSRandall Stewart 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4500f8829a4aSRandall Stewart 	    sizeof(*ch), (uint8_t *)&chunk_buf);
4501f8829a4aSRandall Stewart 	while (ch != NULL) {
4502f8829a4aSRandall Stewart 		chk_length = ntohs(ch->chunk_length);
4503f8829a4aSRandall Stewart 		if (chk_length < sizeof(*ch)) {
4504f8829a4aSRandall Stewart 			/* break to abort land */
4505f8829a4aSRandall Stewart 			break;
4506f8829a4aSRandall Stewart 		}
4507f8829a4aSRandall Stewart 		switch (ch->chunk_type) {
4508c58e60beSMichael Tuexen 		case SCTP_INIT:
4509c58e60beSMichael Tuexen 			contains_init_chunk = 1;
4510c58e60beSMichael Tuexen 			break;
4511f8829a4aSRandall Stewart 		case SCTP_PACKET_DROPPED:
4512f8829a4aSRandall Stewart 			/* we don't respond to pkt-dropped */
4513f8829a4aSRandall Stewart 			return;
4514f8829a4aSRandall Stewart 		case SCTP_ABORT_ASSOCIATION:
4515f8829a4aSRandall Stewart 			/* we don't respond with an ABORT to an ABORT */
4516f8829a4aSRandall Stewart 			return;
4517f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_COMPLETE:
4518f8829a4aSRandall Stewart 			/*
4519f8829a4aSRandall Stewart 			 * we ignore it since we are not waiting for it and
4520f8829a4aSRandall Stewart 			 * peer is gone
4521f8829a4aSRandall Stewart 			 */
4522f8829a4aSRandall Stewart 			return;
4523f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_ACK:
4524b1754ad1SMichael Tuexen 			sctp_send_shutdown_complete2(src, dst, sh,
4525d089f9b9SMichael Tuexen 			    mflowtype, mflowid, fibnum,
4526f30ac432SMichael Tuexen 			    vrf_id, port);
4527f8829a4aSRandall Stewart 			return;
4528f8829a4aSRandall Stewart 		default:
4529f8829a4aSRandall Stewart 			break;
4530f8829a4aSRandall Stewart 		}
4531f8829a4aSRandall Stewart 		offset += SCTP_SIZE32(chk_length);
4532f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4533f8829a4aSRandall Stewart 		    sizeof(*ch), (uint8_t *)&chunk_buf);
4534f8829a4aSRandall Stewart 	}
4535c58e60beSMichael Tuexen 	if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
4536c58e60beSMichael Tuexen 	    ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
4537c58e60beSMichael Tuexen 	    (contains_init_chunk == 0))) {
4538ff1ffd74SMichael Tuexen 		sctp_send_abort(m, iphlen, src, dst, sh, 0, cause,
4539d089f9b9SMichael Tuexen 		    mflowtype, mflowid, fibnum,
4540f30ac432SMichael Tuexen 		    vrf_id, port);
4541f8829a4aSRandall Stewart 	}
4542c58e60beSMichael Tuexen }
4543f8829a4aSRandall Stewart 
4544f8829a4aSRandall Stewart /*
4545f8829a4aSRandall Stewart  * check the inbound datagram to make sure there is not an abort inside it,
4546f8829a4aSRandall Stewart  * if there is return 1, else return 0.
4547f8829a4aSRandall Stewart  */
4548f8829a4aSRandall Stewart int
4549f8829a4aSRandall Stewart sctp_is_there_an_abort_here(struct mbuf *m, int iphlen, uint32_t *vtagfill)
4550f8829a4aSRandall Stewart {
4551f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
4552f8829a4aSRandall Stewart 	struct sctp_init_chunk *init_chk, chunk_buf;
4553f8829a4aSRandall Stewart 	int offset;
4554f8829a4aSRandall Stewart 	unsigned int chk_length;
4555f8829a4aSRandall Stewart 
4556f8829a4aSRandall Stewart 	offset = iphlen + sizeof(struct sctphdr);
4557f8829a4aSRandall Stewart 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch),
4558f8829a4aSRandall Stewart 	    (uint8_t *)&chunk_buf);
4559f8829a4aSRandall Stewart 	while (ch != NULL) {
4560f8829a4aSRandall Stewart 		chk_length = ntohs(ch->chunk_length);
4561f8829a4aSRandall Stewart 		if (chk_length < sizeof(*ch)) {
4562f8829a4aSRandall Stewart 			/* packet is probably corrupt */
4563f8829a4aSRandall Stewart 			break;
4564f8829a4aSRandall Stewart 		}
4565f8829a4aSRandall Stewart 		/* we seem to be ok, is it an abort? */
4566f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) {
4567f8829a4aSRandall Stewart 			/* yep, tell them */
4568f8829a4aSRandall Stewart 			return (1);
4569f8829a4aSRandall Stewart 		}
4570f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_INITIATION) {
4571f8829a4aSRandall Stewart 			/* need to update the Vtag */
4572f8829a4aSRandall Stewart 			init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4573f8829a4aSRandall Stewart 			    offset, sizeof(*init_chk), (uint8_t *)&chunk_buf);
4574f8829a4aSRandall Stewart 			if (init_chk != NULL) {
4575f8829a4aSRandall Stewart 				*vtagfill = ntohl(init_chk->init.initiate_tag);
4576f8829a4aSRandall Stewart 			}
4577f8829a4aSRandall Stewart 		}
4578f8829a4aSRandall Stewart 		/* Nope, move to the next chunk */
4579f8829a4aSRandall Stewart 		offset += SCTP_SIZE32(chk_length);
4580f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4581f8829a4aSRandall Stewart 		    sizeof(*ch), (uint8_t *)&chunk_buf);
4582f8829a4aSRandall Stewart 	}
4583f8829a4aSRandall Stewart 	return (0);
4584f8829a4aSRandall Stewart }
4585f8829a4aSRandall Stewart 
4586f8829a4aSRandall Stewart /*
4587f8829a4aSRandall Stewart  * currently (2/02), ifa_addr embeds scope_id's and don't have sin6_scope_id
4588f8829a4aSRandall Stewart  * set (i.e. it's 0) so, create this function to compare link local scopes
4589f8829a4aSRandall Stewart  */
45905e2c2d87SRandall Stewart #ifdef INET6
4591f8829a4aSRandall Stewart uint32_t
4592b0471b4bSMichael Tuexen sctp_is_same_scope(struct sockaddr_in6 *addr1, struct sockaddr_in6 *addr2)
4593b0471b4bSMichael Tuexen {
4594f8829a4aSRandall Stewart 	struct sockaddr_in6 a, b;
4595f8829a4aSRandall Stewart 
4596f8829a4aSRandall Stewart 	/* save copies */
4597f8829a4aSRandall Stewart 	a = *addr1;
4598f8829a4aSRandall Stewart 	b = *addr2;
4599f8829a4aSRandall Stewart 
4600f8829a4aSRandall Stewart 	if (a.sin6_scope_id == 0)
4601f8829a4aSRandall Stewart 		if (sa6_recoverscope(&a)) {
4602f8829a4aSRandall Stewart 			/* can't get scope, so can't match */
4603f8829a4aSRandall Stewart 			return (0);
4604f8829a4aSRandall Stewart 		}
4605f8829a4aSRandall Stewart 	if (b.sin6_scope_id == 0)
4606f8829a4aSRandall Stewart 		if (sa6_recoverscope(&b)) {
4607f8829a4aSRandall Stewart 			/* can't get scope, so can't match */
4608f8829a4aSRandall Stewart 			return (0);
4609f8829a4aSRandall Stewart 		}
4610f8829a4aSRandall Stewart 	if (a.sin6_scope_id != b.sin6_scope_id)
4611f8829a4aSRandall Stewart 		return (0);
4612f8829a4aSRandall Stewart 
4613f8829a4aSRandall Stewart 	return (1);
4614f8829a4aSRandall Stewart }
4615f8829a4aSRandall Stewart 
4616f8829a4aSRandall Stewart /*
4617f8829a4aSRandall Stewart  * returns a sockaddr_in6 with embedded scope recovered and removed
4618f8829a4aSRandall Stewart  */
4619f8829a4aSRandall Stewart struct sockaddr_in6 *
4620f8829a4aSRandall Stewart sctp_recover_scope(struct sockaddr_in6 *addr, struct sockaddr_in6 *store)
4621f8829a4aSRandall Stewart {
4622f8829a4aSRandall Stewart 	/* check and strip embedded scope junk */
4623f8829a4aSRandall Stewart 	if (addr->sin6_family == AF_INET6) {
4624f8829a4aSRandall Stewart 		if (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr)) {
4625f8829a4aSRandall Stewart 			if (addr->sin6_scope_id == 0) {
4626f8829a4aSRandall Stewart 				*store = *addr;
4627f8829a4aSRandall Stewart 				if (!sa6_recoverscope(store)) {
4628f8829a4aSRandall Stewart 					/* use the recovered scope */
4629f8829a4aSRandall Stewart 					addr = store;
4630f8829a4aSRandall Stewart 				}
4631f42a358aSRandall Stewart 			} else {
4632f8829a4aSRandall Stewart 				/* else, return the original "to" addr */
4633f42a358aSRandall Stewart 				in6_clearscope(&addr->sin6_addr);
4634f8829a4aSRandall Stewart 			}
4635f8829a4aSRandall Stewart 		}
4636f8829a4aSRandall Stewart 	}
4637f8829a4aSRandall Stewart 	return (addr);
4638f8829a4aSRandall Stewart }
46395e2c2d87SRandall Stewart #endif
46405e2c2d87SRandall Stewart 
4641f8829a4aSRandall Stewart /*
4642f8829a4aSRandall Stewart  * are the two addresses the same?  currently a "scopeless" check returns: 1
4643f8829a4aSRandall Stewart  * if same, 0 if not
4644f8829a4aSRandall Stewart  */
464572fb6fdbSRandall Stewart int
4646f8829a4aSRandall Stewart sctp_cmpaddr(struct sockaddr *sa1, struct sockaddr *sa2)
4647f8829a4aSRandall Stewart {
4648f8829a4aSRandall Stewart 
4649f8829a4aSRandall Stewart 	/* must be valid */
4650f8829a4aSRandall Stewart 	if (sa1 == NULL || sa2 == NULL)
4651f8829a4aSRandall Stewart 		return (0);
4652f8829a4aSRandall Stewart 
4653f8829a4aSRandall Stewart 	/* must be the same family */
4654f8829a4aSRandall Stewart 	if (sa1->sa_family != sa2->sa_family)
4655f8829a4aSRandall Stewart 		return (0);
4656f8829a4aSRandall Stewart 
46575e2c2d87SRandall Stewart 	switch (sa1->sa_family) {
46585e2c2d87SRandall Stewart #ifdef INET6
46595e2c2d87SRandall Stewart 	case AF_INET6:
46605e2c2d87SRandall Stewart 		{
4661f8829a4aSRandall Stewart 			/* IPv6 addresses */
4662f8829a4aSRandall Stewart 			struct sockaddr_in6 *sin6_1, *sin6_2;
4663f8829a4aSRandall Stewart 
4664f8829a4aSRandall Stewart 			sin6_1 = (struct sockaddr_in6 *)sa1;
4665f8829a4aSRandall Stewart 			sin6_2 = (struct sockaddr_in6 *)sa2;
4666c54a18d2SRandall Stewart 			return (SCTP6_ARE_ADDR_EQUAL(sin6_1,
4667c54a18d2SRandall Stewart 			    sin6_2));
46685e2c2d87SRandall Stewart 		}
46695e2c2d87SRandall Stewart #endif
4670ea5eba11SMichael Tuexen #ifdef INET
46715e2c2d87SRandall Stewart 	case AF_INET:
46725e2c2d87SRandall Stewart 		{
4673f8829a4aSRandall Stewart 			/* IPv4 addresses */
4674f8829a4aSRandall Stewart 			struct sockaddr_in *sin_1, *sin_2;
4675f8829a4aSRandall Stewart 
4676f8829a4aSRandall Stewart 			sin_1 = (struct sockaddr_in *)sa1;
4677f8829a4aSRandall Stewart 			sin_2 = (struct sockaddr_in *)sa2;
4678f8829a4aSRandall Stewart 			return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr);
46795e2c2d87SRandall Stewart 		}
4680ea5eba11SMichael Tuexen #endif
46815e2c2d87SRandall Stewart 	default:
4682f8829a4aSRandall Stewart 		/* we don't do these... */
4683f8829a4aSRandall Stewart 		return (0);
4684f8829a4aSRandall Stewart 	}
4685f8829a4aSRandall Stewart }
4686f8829a4aSRandall Stewart 
4687f8829a4aSRandall Stewart void
4688f8829a4aSRandall Stewart sctp_print_address(struct sockaddr *sa)
4689f8829a4aSRandall Stewart {
46905e2c2d87SRandall Stewart #ifdef INET6
46917d32aa0cSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
46925e2c2d87SRandall Stewart #endif
46935e2c2d87SRandall Stewart 
46945e2c2d87SRandall Stewart 	switch (sa->sa_family) {
46955e2c2d87SRandall Stewart #ifdef INET6
46965e2c2d87SRandall Stewart 	case AF_INET6:
46975e2c2d87SRandall Stewart 		{
4698ad81507eSRandall Stewart 			struct sockaddr_in6 *sin6;
4699ad81507eSRandall Stewart 
4700f8829a4aSRandall Stewart 			sin6 = (struct sockaddr_in6 *)sa;
4701ad81507eSRandall Stewart 			SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n",
47027d32aa0cSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &sin6->sin6_addr),
47037d32aa0cSBjoern A. Zeeb 			    ntohs(sin6->sin6_port),
4704f8829a4aSRandall Stewart 			    sin6->sin6_scope_id);
47055e2c2d87SRandall Stewart 			break;
47065e2c2d87SRandall Stewart 		}
47075e2c2d87SRandall Stewart #endif
4708ea5eba11SMichael Tuexen #ifdef INET
47095e2c2d87SRandall Stewart 	case AF_INET:
47105e2c2d87SRandall Stewart 		{
4711f8829a4aSRandall Stewart 			struct sockaddr_in *sin;
4712f8829a4aSRandall Stewart 			unsigned char *p;
4713f8829a4aSRandall Stewart 
4714f8829a4aSRandall Stewart 			sin = (struct sockaddr_in *)sa;
4715f8829a4aSRandall Stewart 			p = (unsigned char *)&sin->sin_addr;
4716ad81507eSRandall Stewart 			SCTP_PRINTF("IPv4 address: %u.%u.%u.%u:%d\n",
4717f8829a4aSRandall Stewart 			    p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
47185e2c2d87SRandall Stewart 			break;
47195e2c2d87SRandall Stewart 		}
4720ea5eba11SMichael Tuexen #endif
47215e2c2d87SRandall Stewart 	default:
4722ad81507eSRandall Stewart 		SCTP_PRINTF("?\n");
47235e2c2d87SRandall Stewart 		break;
4724f8829a4aSRandall Stewart 	}
4725f8829a4aSRandall Stewart }
4726f8829a4aSRandall Stewart 
4727f8829a4aSRandall Stewart void
4728f8829a4aSRandall Stewart sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
4729f8829a4aSRandall Stewart     struct sctp_inpcb *new_inp,
4730d06c82f1SRandall Stewart     struct sctp_tcb *stcb,
4731d06c82f1SRandall Stewart     int waitflags)
4732f8829a4aSRandall Stewart {
4733f8829a4aSRandall Stewart 	/*
4734f8829a4aSRandall Stewart 	 * go through our old INP and pull off any control structures that
4735f8829a4aSRandall Stewart 	 * belong to stcb and move then to the new inp.
4736f8829a4aSRandall Stewart 	 */
4737f8829a4aSRandall Stewart 	struct socket *old_so, *new_so;
4738f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control, *nctl;
4739f8829a4aSRandall Stewart 	struct sctp_readhead tmp_queue;
4740f8829a4aSRandall Stewart 	struct mbuf *m;
4741bff64a4dSRandall Stewart 	int error = 0;
4742f8829a4aSRandall Stewart 
4743f8829a4aSRandall Stewart 	old_so = old_inp->sctp_socket;
4744f8829a4aSRandall Stewart 	new_so = new_inp->sctp_socket;
4745f8829a4aSRandall Stewart 	TAILQ_INIT(&tmp_queue);
4746d06c82f1SRandall Stewart 	error = sblock(&old_so->so_rcv, waitflags);
4747f8829a4aSRandall Stewart 	if (error) {
4748f8829a4aSRandall Stewart 		/*
4749f8829a4aSRandall Stewart 		 * Gak, can't get sblock, we have a problem. data will be
4750f8829a4aSRandall Stewart 		 * left stranded.. and we don't dare look at it since the
4751f8829a4aSRandall Stewart 		 * other thread may be reading something. Oh well, its a
4752f8829a4aSRandall Stewart 		 * screwed up app that does a peeloff OR a accept while
4753f8829a4aSRandall Stewart 		 * reading from the main socket... actually its only the
4754f8829a4aSRandall Stewart 		 * peeloff() case, since I think read will fail on a
4755f8829a4aSRandall Stewart 		 * listening socket..
4756f8829a4aSRandall Stewart 		 */
4757f8829a4aSRandall Stewart 		return;
4758f8829a4aSRandall Stewart 	}
4759f8829a4aSRandall Stewart 	/* lock the socket buffers */
4760f8829a4aSRandall Stewart 	SCTP_INP_READ_LOCK(old_inp);
47614a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(control, &old_inp->read_queue, next, nctl) {
4762f8829a4aSRandall Stewart 		/* Pull off all for out target stcb */
4763f8829a4aSRandall Stewart 		if (control->stcb == stcb) {
4764f8829a4aSRandall Stewart 			/* remove it we want it */
4765f8829a4aSRandall Stewart 			TAILQ_REMOVE(&old_inp->read_queue, control, next);
4766f8829a4aSRandall Stewart 			TAILQ_INSERT_TAIL(&tmp_queue, control, next);
4767f8829a4aSRandall Stewart 			m = control->data;
4768f8829a4aSRandall Stewart 			while (m) {
4769b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4770139bc87fSRandall Stewart 					sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
477180fefe0aSRandall Stewart 				}
4772f8829a4aSRandall Stewart 				sctp_sbfree(control, stcb, &old_so->so_rcv, m);
4773b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4774f8829a4aSRandall Stewart 					sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
477580fefe0aSRandall Stewart 				}
4776139bc87fSRandall Stewart 				m = SCTP_BUF_NEXT(m);
4777f8829a4aSRandall Stewart 			}
4778f8829a4aSRandall Stewart 		}
4779f8829a4aSRandall Stewart 	}
4780f8829a4aSRandall Stewart 	SCTP_INP_READ_UNLOCK(old_inp);
4781f8829a4aSRandall Stewart 	/* Remove the sb-lock on the old socket */
4782f8829a4aSRandall Stewart 
4783f8829a4aSRandall Stewart 	sbunlock(&old_so->so_rcv);
4784f8829a4aSRandall Stewart 	/* Now we move them over to the new socket buffer */
4785f8829a4aSRandall Stewart 	SCTP_INP_READ_LOCK(new_inp);
47864a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(control, &tmp_queue, next, nctl) {
4787f8829a4aSRandall Stewart 		TAILQ_INSERT_TAIL(&new_inp->read_queue, control, next);
4788f8829a4aSRandall Stewart 		m = control->data;
4789f8829a4aSRandall Stewart 		while (m) {
4790b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4791139bc87fSRandall Stewart 				sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
479280fefe0aSRandall Stewart 			}
4793f8829a4aSRandall Stewart 			sctp_sballoc(stcb, &new_so->so_rcv, m);
4794b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4795f8829a4aSRandall Stewart 				sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
479680fefe0aSRandall Stewart 			}
4797139bc87fSRandall Stewart 			m = SCTP_BUF_NEXT(m);
4798f8829a4aSRandall Stewart 		}
4799f8829a4aSRandall Stewart 	}
4800f8829a4aSRandall Stewart 	SCTP_INP_READ_UNLOCK(new_inp);
4801f8829a4aSRandall Stewart }
4802f8829a4aSRandall Stewart 
4803f8829a4aSRandall Stewart void
4804b1deed45SMichael Tuexen sctp_wakeup_the_read_socket(struct sctp_inpcb *inp,
4805b1deed45SMichael Tuexen     struct sctp_tcb *stcb,
4806b1deed45SMichael Tuexen     int so_locked
4807b1deed45SMichael Tuexen     SCTP_UNUSED
4808b1deed45SMichael Tuexen )
480944249214SRandall Stewart {
4810b1deed45SMichael Tuexen 	if ((inp != NULL) && (inp->sctp_socket != NULL)) {
481144249214SRandall Stewart 		sctp_sorwakeup(inp, inp->sctp_socket);
481244249214SRandall Stewart 	}
481344249214SRandall Stewart }
481444249214SRandall Stewart 
481544249214SRandall Stewart void
4816f8829a4aSRandall Stewart sctp_add_to_readq(struct sctp_inpcb *inp,
4817f8829a4aSRandall Stewart     struct sctp_tcb *stcb,
4818f8829a4aSRandall Stewart     struct sctp_queued_to_read *control,
4819f8829a4aSRandall Stewart     struct sockbuf *sb,
4820ceaad40aSRandall Stewart     int end,
4821cfde3ff7SRandall Stewart     int inp_read_lock_held,
482228397ac1SMichael Tuexen     int so_locked)
4823f8829a4aSRandall Stewart {
4824f8829a4aSRandall Stewart 	/*
4825f8829a4aSRandall Stewart 	 * Here we must place the control on the end of the socket read
48264e88d37aSMichael Tuexen 	 * queue AND increment sb_cc so that select will work properly on
4827f8829a4aSRandall Stewart 	 * read.
4828f8829a4aSRandall Stewart 	 */
4829f8829a4aSRandall Stewart 	struct mbuf *m, *prev = NULL;
4830f8829a4aSRandall Stewart 
483103b0b021SRandall Stewart 	if (inp == NULL) {
483203b0b021SRandall Stewart 		/* Gak, TSNH!! */
4833a5d547adSRandall Stewart #ifdef INVARIANTS
483403b0b021SRandall Stewart 		panic("Gak, inp NULL on add_to_readq");
483503b0b021SRandall Stewart #endif
483603b0b021SRandall Stewart 		return;
483703b0b021SRandall Stewart 	}
4838cfde3ff7SRandall Stewart 	if (inp_read_lock_held == 0)
4839f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(inp);
4840cd1386abSMichael Tuexen 	if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
48418a3cfbffSMichael Tuexen 		if (!control->on_strm_q) {
4842cd1386abSMichael Tuexen 			sctp_free_remote_addr(control->whoFrom);
4843cd1386abSMichael Tuexen 			if (control->data) {
4844cd1386abSMichael Tuexen 				sctp_m_freem(control->data);
4845cd1386abSMichael Tuexen 				control->data = NULL;
4846cd1386abSMichael Tuexen 			}
484744249214SRandall Stewart 			sctp_free_a_readq(stcb, control);
48488a3cfbffSMichael Tuexen 		}
4849cd1386abSMichael Tuexen 		if (inp_read_lock_held == 0)
4850cd1386abSMichael Tuexen 			SCTP_INP_READ_UNLOCK(inp);
4851cd1386abSMichael Tuexen 		return;
4852cd1386abSMichael Tuexen 	}
485342551e99SRandall Stewart 	if (!(control->spec_flags & M_NOTIFICATION)) {
4854a5d547adSRandall Stewart 		atomic_add_int(&inp->total_recvs, 1);
485542551e99SRandall Stewart 		if (!control->do_not_ref_stcb) {
4856a5d547adSRandall Stewart 			atomic_add_int(&stcb->total_recvs, 1);
485742551e99SRandall Stewart 		}
485842551e99SRandall Stewart 	}
4859f8829a4aSRandall Stewart 	m = control->data;
4860f8829a4aSRandall Stewart 	control->held_length = 0;
4861f8829a4aSRandall Stewart 	control->length = 0;
4862f8829a4aSRandall Stewart 	while (m) {
4863139bc87fSRandall Stewart 		if (SCTP_BUF_LEN(m) == 0) {
4864f8829a4aSRandall Stewart 			/* Skip mbufs with NO length */
4865f8829a4aSRandall Stewart 			if (prev == NULL) {
4866f8829a4aSRandall Stewart 				/* First one */
4867f8829a4aSRandall Stewart 				control->data = sctp_m_free(m);
4868f8829a4aSRandall Stewart 				m = control->data;
4869f8829a4aSRandall Stewart 			} else {
4870139bc87fSRandall Stewart 				SCTP_BUF_NEXT(prev) = sctp_m_free(m);
4871139bc87fSRandall Stewart 				m = SCTP_BUF_NEXT(prev);
4872f8829a4aSRandall Stewart 			}
4873f8829a4aSRandall Stewart 			if (m == NULL) {
4874c2ede4b3SMartin Blapp 				control->tail_mbuf = prev;
4875f8829a4aSRandall Stewart 			}
4876f8829a4aSRandall Stewart 			continue;
4877f8829a4aSRandall Stewart 		}
4878f8829a4aSRandall Stewart 		prev = m;
4879b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4880139bc87fSRandall Stewart 			sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
488180fefe0aSRandall Stewart 		}
4882f8829a4aSRandall Stewart 		sctp_sballoc(stcb, sb, m);
4883b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4884f8829a4aSRandall Stewart 			sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
488580fefe0aSRandall Stewart 		}
4886139bc87fSRandall Stewart 		atomic_add_int(&control->length, SCTP_BUF_LEN(m));
4887139bc87fSRandall Stewart 		m = SCTP_BUF_NEXT(m);
4888f8829a4aSRandall Stewart 	}
4889f8829a4aSRandall Stewart 	if (prev != NULL) {
4890f8829a4aSRandall Stewart 		control->tail_mbuf = prev;
4891f8829a4aSRandall Stewart 	} else {
4892139bc87fSRandall Stewart 		/* Everything got collapsed out?? */
48938a3cfbffSMichael Tuexen 		if (!control->on_strm_q) {
4894cd1386abSMichael Tuexen 			sctp_free_remote_addr(control->whoFrom);
489544249214SRandall Stewart 			sctp_free_a_readq(stcb, control);
48968a3cfbffSMichael Tuexen 		}
4897cfde3ff7SRandall Stewart 		if (inp_read_lock_held == 0)
489847a490cbSMichael Tuexen 			SCTP_INP_READ_UNLOCK(inp);
4899f8829a4aSRandall Stewart 		return;
4900f8829a4aSRandall Stewart 	}
4901f8829a4aSRandall Stewart 	if (end) {
4902f8829a4aSRandall Stewart 		control->end_added = 1;
4903f8829a4aSRandall Stewart 	}
4904f8829a4aSRandall Stewart 	TAILQ_INSERT_TAIL(&inp->read_queue, control, next);
490544249214SRandall Stewart 	control->on_read_q = 1;
4906cfde3ff7SRandall Stewart 	if (inp_read_lock_held == 0)
4907f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
4908f8829a4aSRandall Stewart 	if (inp && inp->sctp_socket) {
4909b1deed45SMichael Tuexen 		sctp_wakeup_the_read_socket(inp, stcb, so_locked);
4910f8829a4aSRandall Stewart 	}
4911f8829a4aSRandall Stewart }
4912f8829a4aSRandall Stewart 
4913f8829a4aSRandall Stewart /*************HOLD THIS COMMENT FOR PATCH FILE OF
4914f8829a4aSRandall Stewart  *************ALTERNATE ROUTING CODE
4915f8829a4aSRandall Stewart  */
4916f8829a4aSRandall Stewart 
4917f8829a4aSRandall Stewart /*************HOLD THIS COMMENT FOR END OF PATCH FILE OF
4918f8829a4aSRandall Stewart  *************ALTERNATE ROUTING CODE
4919f8829a4aSRandall Stewart  */
4920f8829a4aSRandall Stewart 
4921f8829a4aSRandall Stewart struct mbuf *
4922ff1ffd74SMichael Tuexen sctp_generate_cause(uint16_t code, char *info)
4923f8829a4aSRandall Stewart {
4924f8829a4aSRandall Stewart 	struct mbuf *m;
4925ff1ffd74SMichael Tuexen 	struct sctp_gen_error_cause *cause;
49269a8e3088SMichael Tuexen 	size_t info_len;
49279a8e3088SMichael Tuexen 	uint16_t len;
4928f8829a4aSRandall Stewart 
4929ff1ffd74SMichael Tuexen 	if ((code == 0) || (info == NULL)) {
4930ff1ffd74SMichael Tuexen 		return (NULL);
4931ff1ffd74SMichael Tuexen 	}
4932ff1ffd74SMichael Tuexen 	info_len = strlen(info);
49339a8e3088SMichael Tuexen 	if (info_len > (SCTP_MAX_CAUSE_LENGTH - sizeof(struct sctp_paramhdr))) {
49349a8e3088SMichael Tuexen 		return (NULL);
49359a8e3088SMichael Tuexen 	}
49369a8e3088SMichael Tuexen 	len = (uint16_t)(sizeof(struct sctp_paramhdr) + info_len);
4937ff1ffd74SMichael Tuexen 	m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
4938ff1ffd74SMichael Tuexen 	if (m != NULL) {
4939ff1ffd74SMichael Tuexen 		SCTP_BUF_LEN(m) = len;
4940ff1ffd74SMichael Tuexen 		cause = mtod(m, struct sctp_gen_error_cause *);
4941ff1ffd74SMichael Tuexen 		cause->code = htons(code);
49429a8e3088SMichael Tuexen 		cause->length = htons(len);
4943ff1ffd74SMichael Tuexen 		memcpy(cause->info, info, info_len);
4944f8829a4aSRandall Stewart 	}
4945f8829a4aSRandall Stewart 	return (m);
4946f8829a4aSRandall Stewart }
4947f8829a4aSRandall Stewart 
494832451da4SMichael Tuexen struct mbuf *
494932451da4SMichael Tuexen sctp_generate_no_user_data_cause(uint32_t tsn)
495032451da4SMichael Tuexen {
495132451da4SMichael Tuexen 	struct mbuf *m;
495232451da4SMichael Tuexen 	struct sctp_error_no_user_data *no_user_data_cause;
49539a8e3088SMichael Tuexen 	uint16_t len;
495432451da4SMichael Tuexen 
49559a8e3088SMichael Tuexen 	len = (uint16_t)sizeof(struct sctp_error_no_user_data);
495632451da4SMichael Tuexen 	m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
495732451da4SMichael Tuexen 	if (m != NULL) {
495832451da4SMichael Tuexen 		SCTP_BUF_LEN(m) = len;
495932451da4SMichael Tuexen 		no_user_data_cause = mtod(m, struct sctp_error_no_user_data *);
496032451da4SMichael Tuexen 		no_user_data_cause->cause.code = htons(SCTP_CAUSE_NO_USER_DATA);
49619a8e3088SMichael Tuexen 		no_user_data_cause->cause.length = htons(len);
49628b9c95f4SMichael Tuexen 		no_user_data_cause->tsn = htonl(tsn);
496332451da4SMichael Tuexen 	}
496432451da4SMichael Tuexen 	return (m);
496532451da4SMichael Tuexen }
496632451da4SMichael Tuexen 
4967f8829a4aSRandall Stewart #ifdef SCTP_MBCNT_LOGGING
4968f8829a4aSRandall Stewart void
4969f8829a4aSRandall Stewart sctp_free_bufspace(struct sctp_tcb *stcb, struct sctp_association *asoc,
4970f8829a4aSRandall Stewart     struct sctp_tmit_chunk *tp1, int chk_cnt)
4971f8829a4aSRandall Stewart {
4972f8829a4aSRandall Stewart 	if (tp1->data == NULL) {
4973f8829a4aSRandall Stewart 		return;
4974f8829a4aSRandall Stewart 	}
4975f8829a4aSRandall Stewart 	asoc->chunks_on_out_queue -= chk_cnt;
4976b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBCNT_LOGGING_ENABLE) {
4977f8829a4aSRandall Stewart 		sctp_log_mbcnt(SCTP_LOG_MBCNT_DECREASE,
4978f8829a4aSRandall Stewart 		    asoc->total_output_queue_size,
4979f8829a4aSRandall Stewart 		    tp1->book_size,
4980f8829a4aSRandall Stewart 		    0,
4981f8829a4aSRandall Stewart 		    tp1->mbcnt);
498280fefe0aSRandall Stewart 	}
4983f8829a4aSRandall Stewart 	if (asoc->total_output_queue_size >= tp1->book_size) {
498444b7479bSRandall Stewart 		atomic_add_int(&asoc->total_output_queue_size, -tp1->book_size);
4985f8829a4aSRandall Stewart 	} else {
4986f8829a4aSRandall Stewart 		asoc->total_output_queue_size = 0;
4987f8829a4aSRandall Stewart 	}
4988f8829a4aSRandall Stewart 
4989f8829a4aSRandall Stewart 	if (stcb->sctp_socket && (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) ||
4990f8829a4aSRandall Stewart 	    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) {
49914e88d37aSMichael Tuexen 		if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) {
49924e88d37aSMichael Tuexen 			stcb->sctp_socket->so_snd.sb_cc -= tp1->book_size;
4993f8829a4aSRandall Stewart 		} else {
49944e88d37aSMichael Tuexen 			stcb->sctp_socket->so_snd.sb_cc = 0;
4995f8829a4aSRandall Stewart 		}
4996f8829a4aSRandall Stewart 	}
4997f8829a4aSRandall Stewart }
4998f8829a4aSRandall Stewart 
4999f8829a4aSRandall Stewart #endif
5000f8829a4aSRandall Stewart 
5001f8829a4aSRandall Stewart int
5002f8829a4aSRandall Stewart sctp_release_pr_sctp_chunk(struct sctp_tcb *stcb, struct sctp_tmit_chunk *tp1,
500328397ac1SMichael Tuexen     uint8_t sent, int so_locked)
5004f8829a4aSRandall Stewart {
50050c0982b8SRandall Stewart 	struct sctp_stream_out *strq;
50064a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk = NULL, *tp2;
50070c0982b8SRandall Stewart 	struct sctp_stream_queue_pending *sp;
500849656eefSMichael Tuexen 	uint32_t mid;
500949656eefSMichael Tuexen 	uint16_t sid;
50100c0982b8SRandall Stewart 	uint8_t foundeom = 0;
5011f8829a4aSRandall Stewart 	int ret_sz = 0;
5012f8829a4aSRandall Stewart 	int notdone;
50130c0982b8SRandall Stewart 	int do_wakeup_routine = 0;
5014f8829a4aSRandall Stewart 
501549656eefSMichael Tuexen 	sid = tp1->rec.data.sid;
501649656eefSMichael Tuexen 	mid = tp1->rec.data.mid;
5017f0396ad1SMichael Tuexen 	if (sent || !(tp1->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG)) {
5018f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_sent[0]++;
5019f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
502049656eefSMichael Tuexen 		stcb->asoc.strmout[sid].abandoned_sent[0]++;
5021f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
5022ad15e154SMichael Tuexen 		stcb->asoc.strmout[sid].abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
5023f0396ad1SMichael Tuexen #endif
5024f0396ad1SMichael Tuexen 	} else {
5025f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_unsent[0]++;
5026f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
502749656eefSMichael Tuexen 		stcb->asoc.strmout[sid].abandoned_unsent[0]++;
5028f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
5029ad15e154SMichael Tuexen 		stcb->asoc.strmout[sid].abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
5030f0396ad1SMichael Tuexen #endif
5031f0396ad1SMichael Tuexen 	}
5032f8829a4aSRandall Stewart 	do {
5033f8829a4aSRandall Stewart 		ret_sz += tp1->book_size;
50340c0982b8SRandall Stewart 		if (tp1->data != NULL) {
50358933fa13SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5036830d754dSRandall Stewart 				sctp_flight_size_decrease(tp1);
5037830d754dSRandall Stewart 				sctp_total_flight_decrease(stcb, tp1);
50388933fa13SRandall Stewart 			}
50398933fa13SRandall Stewart 			sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
50400c0982b8SRandall Stewart 			stcb->asoc.peers_rwnd += tp1->send_size;
50410c0982b8SRandall Stewart 			stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
50421edc9dbaSMichael Tuexen 			if (sent) {
50431edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
50441edc9dbaSMichael Tuexen 			} else {
50451edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
50461edc9dbaSMichael Tuexen 			}
50472f99457bSMichael Tuexen 			if (tp1->data) {
5048f8829a4aSRandall Stewart 				sctp_m_freem(tp1->data);
5049f8829a4aSRandall Stewart 				tp1->data = NULL;
50502f99457bSMichael Tuexen 			}
50510c0982b8SRandall Stewart 			do_wakeup_routine = 1;
5052f8829a4aSRandall Stewart 			if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
5053f8829a4aSRandall Stewart 				stcb->asoc.sent_queue_cnt_removeable--;
5054f8829a4aSRandall Stewart 			}
5055f8829a4aSRandall Stewart 		}
50568933fa13SRandall Stewart 		tp1->sent = SCTP_FORWARD_TSN_SKIP;
5057f8829a4aSRandall Stewart 		if ((tp1->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) ==
5058f8829a4aSRandall Stewart 		    SCTP_DATA_NOT_FRAG) {
5059f8829a4aSRandall Stewart 			/* not frag'ed we ae done   */
5060f8829a4aSRandall Stewart 			notdone = 0;
5061f8829a4aSRandall Stewart 			foundeom = 1;
5062f8829a4aSRandall Stewart 		} else if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
5063f8829a4aSRandall Stewart 			/* end of frag, we are done */
5064f8829a4aSRandall Stewart 			notdone = 0;
5065f8829a4aSRandall Stewart 			foundeom = 1;
5066f8829a4aSRandall Stewart 		} else {
5067f8829a4aSRandall Stewart 			/*
5068f8829a4aSRandall Stewart 			 * Its a begin or middle piece, we must mark all of
5069f8829a4aSRandall Stewart 			 * it
5070f8829a4aSRandall Stewart 			 */
5071f8829a4aSRandall Stewart 			notdone = 1;
5072f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
5073f8829a4aSRandall Stewart 		}
5074f8829a4aSRandall Stewart 	} while (tp1 && notdone);
50750c0982b8SRandall Stewart 	if (foundeom == 0) {
5076f8829a4aSRandall Stewart 		/*
5077f8829a4aSRandall Stewart 		 * The multi-part message was scattered across the send and
5078f8829a4aSRandall Stewart 		 * sent queue.
5079f8829a4aSRandall Stewart 		 */
50804a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(tp1, &stcb->asoc.send_queue, sctp_next, tp2) {
508149656eefSMichael Tuexen 			if ((tp1->rec.data.sid != sid) ||
508249656eefSMichael Tuexen 			    (!SCTP_MID_EQ(stcb->asoc.idata_supported, tp1->rec.data.mid, mid))) {
50834a9ef3f8SMichael Tuexen 				break;
50844a9ef3f8SMichael Tuexen 			}
50850c0982b8SRandall Stewart 			/*
50860c0982b8SRandall Stewart 			 * save to chk in case we have some on stream out
50870c0982b8SRandall Stewart 			 * queue. If so and we have an un-transmitted one we
50880c0982b8SRandall Stewart 			 * don't have to fudge the TSN.
50890c0982b8SRandall Stewart 			 */
50900c0982b8SRandall Stewart 			chk = tp1;
50910c0982b8SRandall Stewart 			ret_sz += tp1->book_size;
50920c0982b8SRandall Stewart 			sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
50931edc9dbaSMichael Tuexen 			if (sent) {
50941edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
50951edc9dbaSMichael Tuexen 			} else {
50961edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
50971edc9dbaSMichael Tuexen 			}
50982f99457bSMichael Tuexen 			if (tp1->data) {
50990c0982b8SRandall Stewart 				sctp_m_freem(tp1->data);
51002f99457bSMichael Tuexen 				tp1->data = NULL;
51012f99457bSMichael Tuexen 			}
51028933fa13SRandall Stewart 			/* No flight involved here book the size to 0 */
51038933fa13SRandall Stewart 			tp1->book_size = 0;
51040c0982b8SRandall Stewart 			if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
51050c0982b8SRandall Stewart 				foundeom = 1;
5106f8829a4aSRandall Stewart 			}
51070c0982b8SRandall Stewart 			do_wakeup_routine = 1;
51080c0982b8SRandall Stewart 			tp1->sent = SCTP_FORWARD_TSN_SKIP;
51090c0982b8SRandall Stewart 			TAILQ_REMOVE(&stcb->asoc.send_queue, tp1, sctp_next);
5110b7b84c0eSMichael Tuexen 			/*
5111b7b84c0eSMichael Tuexen 			 * on to the sent queue so we can wait for it to be
5112b7b84c0eSMichael Tuexen 			 * passed by.
5113b7b84c0eSMichael Tuexen 			 */
51140c0982b8SRandall Stewart 			TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, tp1,
51150c0982b8SRandall Stewart 			    sctp_next);
51160c0982b8SRandall Stewart 			stcb->asoc.send_queue_cnt--;
51170c0982b8SRandall Stewart 			stcb->asoc.sent_queue_cnt++;
51180c0982b8SRandall Stewart 		}
51190c0982b8SRandall Stewart 	}
51200c0982b8SRandall Stewart 	if (foundeom == 0) {
51210c0982b8SRandall Stewart 		/*
51220c0982b8SRandall Stewart 		 * Still no eom found. That means there is stuff left on the
51230c0982b8SRandall Stewart 		 * stream out queue.. yuck.
51240c0982b8SRandall Stewart 		 */
51250c0982b8SRandall Stewart 		SCTP_TCB_SEND_LOCK(stcb);
512649656eefSMichael Tuexen 		strq = &stcb->asoc.strmout[sid];
5127f3b05218SMichael Tuexen 		sp = TAILQ_FIRST(&strq->outqueue);
5128f3b05218SMichael Tuexen 		if (sp != NULL) {
51290c0982b8SRandall Stewart 			sp->discard_rest = 1;
51300c0982b8SRandall Stewart 			/*
5131f3b05218SMichael Tuexen 			 * We may need to put a chunk on the queue that
5132f3b05218SMichael Tuexen 			 * holds the TSN that would have been sent with the
5133f3b05218SMichael Tuexen 			 * LAST bit.
51340c0982b8SRandall Stewart 			 */
51350c0982b8SRandall Stewart 			if (chk == NULL) {
51360c0982b8SRandall Stewart 				/* Yep, we have to */
51370c0982b8SRandall Stewart 				sctp_alloc_a_chunk(stcb, chk);
51380c0982b8SRandall Stewart 				if (chk == NULL) {
51390c0982b8SRandall Stewart 					/*
5140f3b05218SMichael Tuexen 					 * we are hosed. All we can do is
5141f3b05218SMichael Tuexen 					 * nothing.. which will cause an
5142f3b05218SMichael Tuexen 					 * abort if the peer is paying
51430c0982b8SRandall Stewart 					 * attention.
51440c0982b8SRandall Stewart 					 */
51450c0982b8SRandall Stewart 					goto oh_well;
51460c0982b8SRandall Stewart 				}
51470c0982b8SRandall Stewart 				memset(chk, 0, sizeof(*chk));
514863d5b568SMichael Tuexen 				chk->rec.data.rcv_flags = 0;
51490c0982b8SRandall Stewart 				chk->sent = SCTP_FORWARD_TSN_SKIP;
51500c0982b8SRandall Stewart 				chk->asoc = &stcb->asoc;
515163d5b568SMichael Tuexen 				if (stcb->asoc.idata_supported == 0) {
515263d5b568SMichael Tuexen 					if (sp->sinfo_flags & SCTP_UNORDERED) {
515349656eefSMichael Tuexen 						chk->rec.data.mid = 0;
515463d5b568SMichael Tuexen 					} else {
515549656eefSMichael Tuexen 						chk->rec.data.mid = strq->next_mid_ordered;
515663d5b568SMichael Tuexen 					}
515763d5b568SMichael Tuexen 				} else {
515863d5b568SMichael Tuexen 					if (sp->sinfo_flags & SCTP_UNORDERED) {
515949656eefSMichael Tuexen 						chk->rec.data.mid = strq->next_mid_unordered;
516063d5b568SMichael Tuexen 					} else {
516149656eefSMichael Tuexen 						chk->rec.data.mid = strq->next_mid_ordered;
516263d5b568SMichael Tuexen 					}
516363d5b568SMichael Tuexen 				}
516449656eefSMichael Tuexen 				chk->rec.data.sid = sp->sid;
516549656eefSMichael Tuexen 				chk->rec.data.ppid = sp->ppid;
51660c0982b8SRandall Stewart 				chk->rec.data.context = sp->context;
51670c0982b8SRandall Stewart 				chk->flags = sp->act_flags;
51687fd5b436SMichael Tuexen 				chk->whoTo = NULL;
516949656eefSMichael Tuexen 				chk->rec.data.tsn = atomic_fetchadd_int(&stcb->asoc.sending_seq, 1);
51707fd5b436SMichael Tuexen 				strq->chunks_on_queues++;
51710c0982b8SRandall Stewart 				TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, chk, sctp_next);
51720c0982b8SRandall Stewart 				stcb->asoc.sent_queue_cnt++;
51738933fa13SRandall Stewart 				stcb->asoc.pr_sctp_cnt++;
51740c0982b8SRandall Stewart 			}
517563d5b568SMichael Tuexen 			chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
5176d1ea5fa9SMichael Tuexen 			if (sp->sinfo_flags & SCTP_UNORDERED) {
5177d1ea5fa9SMichael Tuexen 				chk->rec.data.rcv_flags |= SCTP_DATA_UNORDERED;
5178d1ea5fa9SMichael Tuexen 			}
517963d5b568SMichael Tuexen 			if (stcb->asoc.idata_supported == 0) {
518063d5b568SMichael Tuexen 				if ((sp->sinfo_flags & SCTP_UNORDERED) == 0) {
518163d5b568SMichael Tuexen 					strq->next_mid_ordered++;
518263d5b568SMichael Tuexen 				}
518363d5b568SMichael Tuexen 			} else {
518463d5b568SMichael Tuexen 				if (sp->sinfo_flags & SCTP_UNORDERED) {
518563d5b568SMichael Tuexen 					strq->next_mid_unordered++;
518663d5b568SMichael Tuexen 				} else {
518763d5b568SMichael Tuexen 					strq->next_mid_ordered++;
518863d5b568SMichael Tuexen 				}
518963d5b568SMichael Tuexen 			}
51900c0982b8SRandall Stewart 	oh_well:
51910c0982b8SRandall Stewart 			if (sp->data) {
51920c0982b8SRandall Stewart 				/*
5193f3b05218SMichael Tuexen 				 * Pull any data to free up the SB and allow
5194f3b05218SMichael Tuexen 				 * sender to "add more" while we will throw
5195f3b05218SMichael Tuexen 				 * away :-)
51960c0982b8SRandall Stewart 				 */
5197f3b05218SMichael Tuexen 				sctp_free_spbufspace(stcb, &stcb->asoc, sp);
51980c0982b8SRandall Stewart 				ret_sz += sp->length;
51990c0982b8SRandall Stewart 				do_wakeup_routine = 1;
52000c0982b8SRandall Stewart 				sp->some_taken = 1;
52010c0982b8SRandall Stewart 				sctp_m_freem(sp->data);
52020c0982b8SRandall Stewart 				sp->data = NULL;
52030c0982b8SRandall Stewart 				sp->tail_mbuf = NULL;
5204d07b2ac6SMichael Tuexen 				sp->length = 0;
52050c0982b8SRandall Stewart 			}
52060c0982b8SRandall Stewart 		}
52070c0982b8SRandall Stewart 		SCTP_TCB_SEND_UNLOCK(stcb);
52080c0982b8SRandall Stewart 	}
52090c0982b8SRandall Stewart 	if (do_wakeup_routine) {
52100c0982b8SRandall Stewart 		sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
5211f8829a4aSRandall Stewart 	}
5212f8829a4aSRandall Stewart 	return (ret_sz);
5213f8829a4aSRandall Stewart }
5214f8829a4aSRandall Stewart 
5215f8829a4aSRandall Stewart /*
5216f8829a4aSRandall Stewart  * checks to see if the given address, sa, is one that is currently known by
5217f8829a4aSRandall Stewart  * the kernel note: can't distinguish the same address on multiple interfaces
5218f8829a4aSRandall Stewart  * and doesn't handle multiple addresses with different zone/scope id's note:
5219f8829a4aSRandall Stewart  * ifa_ifwithaddr() compares the entire sockaddr struct
5220f8829a4aSRandall Stewart  */
522142551e99SRandall Stewart struct sctp_ifa *
522280fefe0aSRandall Stewart sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr,
522380fefe0aSRandall Stewart     int holds_lock)
5224f8829a4aSRandall Stewart {
522542551e99SRandall Stewart 	struct sctp_laddr *laddr;
5226f8829a4aSRandall Stewart 
5227ad81507eSRandall Stewart 	if (holds_lock == 0) {
522842551e99SRandall Stewart 		SCTP_INP_RLOCK(inp);
5229ad81507eSRandall Stewart 	}
52300053ed28SMichael Tuexen 
523142551e99SRandall Stewart 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
523242551e99SRandall Stewart 		if (laddr->ifa == NULL)
5233f8829a4aSRandall Stewart 			continue;
523442551e99SRandall Stewart 		if (addr->sa_family != laddr->ifa->address.sa.sa_family)
523542551e99SRandall Stewart 			continue;
5236e6194c2eSMichael Tuexen #ifdef INET
523742551e99SRandall Stewart 		if (addr->sa_family == AF_INET) {
523842551e99SRandall Stewart 			if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
523942551e99SRandall Stewart 			    laddr->ifa->address.sin.sin_addr.s_addr) {
524042551e99SRandall Stewart 				/* found him. */
524142551e99SRandall Stewart 				break;
524242551e99SRandall Stewart 			}
52435e2c2d87SRandall Stewart 		}
5244e6194c2eSMichael Tuexen #endif
52455e2c2d87SRandall Stewart #ifdef INET6
52465e2c2d87SRandall Stewart 		if (addr->sa_family == AF_INET6) {
5247c54a18d2SRandall Stewart 			if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5248c54a18d2SRandall Stewart 			    &laddr->ifa->address.sin6)) {
524942551e99SRandall Stewart 				/* found him. */
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 	}
52587a3f60e7SMichael Tuexen 	if (laddr != NULL) {
525944710431SMichael Tuexen 		return (laddr->ifa);
52607a3f60e7SMichael Tuexen 	} else {
52617a3f60e7SMichael Tuexen 		return (NULL);
52627a3f60e7SMichael Tuexen 	}
526342551e99SRandall Stewart }
5264f8829a4aSRandall Stewart 
52656a27c376SRandall Stewart uint32_t
5266b0471b4bSMichael Tuexen sctp_get_ifa_hash_val(struct sockaddr *addr)
5267b0471b4bSMichael Tuexen {
5268ea5eba11SMichael Tuexen 	switch (addr->sa_family) {
5269ea5eba11SMichael Tuexen #ifdef INET
5270ea5eba11SMichael Tuexen 	case AF_INET:
5271ea5eba11SMichael Tuexen 		{
52726a27c376SRandall Stewart 			struct sockaddr_in *sin;
52736a27c376SRandall Stewart 
52746a27c376SRandall Stewart 			sin = (struct sockaddr_in *)addr;
52756a27c376SRandall Stewart 			return (sin->sin_addr.s_addr ^ (sin->sin_addr.s_addr >> 16));
5276ea5eba11SMichael Tuexen 		}
5277ea5eba11SMichael Tuexen #endif
5278ea5eba11SMichael Tuexen #ifdef INET6
52792c2e3218SMichael Tuexen 	case AF_INET6:
5280ea5eba11SMichael Tuexen 		{
52816a27c376SRandall Stewart 			struct sockaddr_in6 *sin6;
52826a27c376SRandall Stewart 			uint32_t hash_of_addr;
52836a27c376SRandall Stewart 
52846a27c376SRandall Stewart 			sin6 = (struct sockaddr_in6 *)addr;
52856a27c376SRandall Stewart 			hash_of_addr = (sin6->sin6_addr.s6_addr32[0] +
52866a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[1] +
52876a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[2] +
52886a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[3]);
52896a27c376SRandall Stewart 			hash_of_addr = (hash_of_addr ^ (hash_of_addr >> 16));
52906a27c376SRandall Stewart 			return (hash_of_addr);
52916a27c376SRandall Stewart 		}
5292ea5eba11SMichael Tuexen #endif
5293ea5eba11SMichael Tuexen 	default:
5294ea5eba11SMichael Tuexen 		break;
5295ea5eba11SMichael Tuexen 	}
52966a27c376SRandall Stewart 	return (0);
52976a27c376SRandall Stewart }
52986a27c376SRandall Stewart 
529942551e99SRandall Stewart struct sctp_ifa *
530042551e99SRandall Stewart sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock)
530142551e99SRandall Stewart {
530242551e99SRandall Stewart 	struct sctp_ifa *sctp_ifap;
530342551e99SRandall Stewart 	struct sctp_vrf *vrf;
53046a27c376SRandall Stewart 	struct sctp_ifalist *hash_head;
53056a27c376SRandall Stewart 	uint32_t hash_of_addr;
530642551e99SRandall Stewart 
53077f0ad227SMichael Tuexen 	if (holds_lock == 0) {
5308c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RLOCK();
53097f0ad227SMichael Tuexen 	} else {
53107f0ad227SMichael Tuexen 		SCTP_IPI_ADDR_LOCK_ASSERT();
53117f0ad227SMichael Tuexen 	}
531242551e99SRandall Stewart 
5313bff64a4dSRandall Stewart 	vrf = sctp_find_vrf(vrf_id);
5314bff64a4dSRandall Stewart 	if (vrf == NULL) {
5315bff64a4dSRandall Stewart 		if (holds_lock == 0)
5316c99efcf6SRandall Stewart 			SCTP_IPI_ADDR_RUNLOCK();
5317bff64a4dSRandall Stewart 		return (NULL);
5318bff64a4dSRandall Stewart 	}
53190053ed28SMichael Tuexen 
5320bff64a4dSRandall Stewart 	hash_of_addr = sctp_get_ifa_hash_val(addr);
5321bff64a4dSRandall Stewart 
532217205eccSRandall Stewart 	hash_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
5323bff64a4dSRandall Stewart 	if (hash_head == NULL) {
5324ad81507eSRandall Stewart 		SCTP_PRINTF("hash_of_addr:%x mask:%x table:%x - ",
5325c99efcf6SRandall Stewart 		    hash_of_addr, (uint32_t)vrf->vrf_addr_hashmark,
5326c99efcf6SRandall Stewart 		    (uint32_t)(hash_of_addr & vrf->vrf_addr_hashmark));
5327bff64a4dSRandall Stewart 		sctp_print_address(addr);
5328ad81507eSRandall Stewart 		SCTP_PRINTF("No such bucket for address\n");
5329bff64a4dSRandall Stewart 		if (holds_lock == 0)
5330c99efcf6SRandall Stewart 			SCTP_IPI_ADDR_RUNLOCK();
5331bff64a4dSRandall Stewart 
5332bff64a4dSRandall Stewart 		return (NULL);
5333bff64a4dSRandall Stewart 	}
53346a27c376SRandall Stewart 	LIST_FOREACH(sctp_ifap, hash_head, next_bucket) {
53356a27c376SRandall Stewart 		if (addr->sa_family != sctp_ifap->address.sa.sa_family)
53366a27c376SRandall Stewart 			continue;
5337e6194c2eSMichael Tuexen #ifdef INET
53386a27c376SRandall Stewart 		if (addr->sa_family == AF_INET) {
53396a27c376SRandall Stewart 			if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
53406a27c376SRandall Stewart 			    sctp_ifap->address.sin.sin_addr.s_addr) {
53416a27c376SRandall Stewart 				/* found him. */
53426a27c376SRandall Stewart 				break;
53436a27c376SRandall Stewart 			}
53445e2c2d87SRandall Stewart 		}
5345e6194c2eSMichael Tuexen #endif
53465e2c2d87SRandall Stewart #ifdef INET6
53475e2c2d87SRandall Stewart 		if (addr->sa_family == AF_INET6) {
5348c54a18d2SRandall Stewart 			if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5349c54a18d2SRandall Stewart 			    &sctp_ifap->address.sin6)) {
53506a27c376SRandall Stewart 				/* found him. */
53516a27c376SRandall Stewart 				break;
53526a27c376SRandall Stewart 			}
535342551e99SRandall Stewart 		}
53545e2c2d87SRandall Stewart #endif
535542551e99SRandall Stewart 	}
535642551e99SRandall Stewart 	if (holds_lock == 0)
5357c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RUNLOCK();
535844710431SMichael Tuexen 	return (sctp_ifap);
5359f8829a4aSRandall Stewart }
5360f8829a4aSRandall Stewart 
5361f8829a4aSRandall Stewart static void
53624c9179adSRandall Stewart sctp_user_rcvd(struct sctp_tcb *stcb, uint32_t *freed_so_far, int hold_rlock,
5363f8829a4aSRandall Stewart     uint32_t rwnd_req)
5364f8829a4aSRandall Stewart {
5365f8829a4aSRandall Stewart 	/* User pulled some data, do we need a rwnd update? */
5366868b51f2SMichael Tuexen 	struct epoch_tracker et;
5367f8829a4aSRandall Stewart 	int r_unlocked = 0;
5368f8829a4aSRandall Stewart 	uint32_t dif, rwnd;
5369f8829a4aSRandall Stewart 	struct socket *so = NULL;
5370f8829a4aSRandall Stewart 
5371f8829a4aSRandall Stewart 	if (stcb == NULL)
5372f8829a4aSRandall Stewart 		return;
5373f8829a4aSRandall Stewart 
537450cec919SRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, 1);
5375f8829a4aSRandall Stewart 
5376839d21d6SMichael Tuexen 	if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
537761a21880SMichael Tuexen 	    (stcb->asoc.state & (SCTP_STATE_ABOUT_TO_BE_FREED | SCTP_STATE_SHUTDOWN_RECEIVED))) {
5378f8829a4aSRandall Stewart 		/* Pre-check If we are freeing no update */
5379f8829a4aSRandall Stewart 		goto no_lock;
5380f8829a4aSRandall Stewart 	}
5381f8829a4aSRandall Stewart 	SCTP_INP_INCR_REF(stcb->sctp_ep);
5382f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5383f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5384f8829a4aSRandall Stewart 		goto out;
5385f8829a4aSRandall Stewart 	}
5386f8829a4aSRandall Stewart 	so = stcb->sctp_socket;
5387f8829a4aSRandall Stewart 	if (so == NULL) {
5388f8829a4aSRandall Stewart 		goto out;
5389f8829a4aSRandall Stewart 	}
5390f8829a4aSRandall Stewart 	atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far);
5391f8829a4aSRandall Stewart 	/* Have you have freed enough to look */
5392f8829a4aSRandall Stewart 	*freed_so_far = 0;
5393f8829a4aSRandall Stewart 	/* Yep, its worth a look and the lock overhead */
5394f8829a4aSRandall Stewart 
5395f8829a4aSRandall Stewart 	/* Figure out what the rwnd would be */
5396f8829a4aSRandall Stewart 	rwnd = sctp_calc_rwnd(stcb, &stcb->asoc);
5397f8829a4aSRandall Stewart 	if (rwnd >= stcb->asoc.my_last_reported_rwnd) {
5398f8829a4aSRandall Stewart 		dif = rwnd - stcb->asoc.my_last_reported_rwnd;
5399f8829a4aSRandall Stewart 	} else {
5400f8829a4aSRandall Stewart 		dif = 0;
5401f8829a4aSRandall Stewart 	}
5402f8829a4aSRandall Stewart 	if (dif >= rwnd_req) {
5403f8829a4aSRandall Stewart 		if (hold_rlock) {
5404f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5405f8829a4aSRandall Stewart 			r_unlocked = 1;
5406f8829a4aSRandall Stewart 		}
5407f8829a4aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5408f8829a4aSRandall Stewart 			/*
5409f8829a4aSRandall Stewart 			 * One last check before we allow the guy possibly
5410f8829a4aSRandall Stewart 			 * to get in. There is a race, where the guy has not
5411f8829a4aSRandall Stewart 			 * reached the gate. In that case
5412f8829a4aSRandall Stewart 			 */
5413f8829a4aSRandall Stewart 			goto out;
5414f8829a4aSRandall Stewart 		}
5415f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
5416f8829a4aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5417f8829a4aSRandall Stewart 			/* No reports here */
5418f8829a4aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
5419f8829a4aSRandall Stewart 			goto out;
5420f8829a4aSRandall Stewart 		}
5421f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_wu_sacks_sent);
5422868b51f2SMichael Tuexen 		NET_EPOCH_ENTER(et);
5423689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_LOCKED);
5424830d754dSRandall Stewart 
5425f8829a4aSRandall Stewart 		sctp_chunk_output(stcb->sctp_ep, stcb,
5426ceaad40aSRandall Stewart 		    SCTP_OUTPUT_FROM_USR_RCVD, SCTP_SO_LOCKED);
5427f8829a4aSRandall Stewart 		/* make sure no timer is running */
5428868b51f2SMichael Tuexen 		NET_EPOCH_EXIT(et);
5429ba785902SMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
5430ba785902SMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_6);
5431f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
5432f8829a4aSRandall Stewart 	} else {
5433f8829a4aSRandall Stewart 		/* Update how much we have pending */
5434f8829a4aSRandall Stewart 		stcb->freed_by_sorcv_sincelast = dif;
5435f8829a4aSRandall Stewart 	}
5436f8829a4aSRandall Stewart out:
5437f8829a4aSRandall Stewart 	if (so && r_unlocked && hold_rlock) {
5438f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
5439f8829a4aSRandall Stewart 	}
54400053ed28SMichael Tuexen 
5441f8829a4aSRandall Stewart 	SCTP_INP_DECR_REF(stcb->sctp_ep);
5442f8829a4aSRandall Stewart no_lock:
544350cec919SRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, -1);
5444f8829a4aSRandall Stewart 	return;
5445f8829a4aSRandall Stewart }
5446f8829a4aSRandall Stewart 
5447f8829a4aSRandall Stewart int
5448f8829a4aSRandall Stewart sctp_sorecvmsg(struct socket *so,
5449f8829a4aSRandall Stewart     struct uio *uio,
5450f8829a4aSRandall Stewart     struct mbuf **mp,
5451f8829a4aSRandall Stewart     struct sockaddr *from,
5452f8829a4aSRandall Stewart     int fromlen,
5453f8829a4aSRandall Stewart     int *msg_flags,
5454f8829a4aSRandall Stewart     struct sctp_sndrcvinfo *sinfo,
5455f8829a4aSRandall Stewart     int filling_sinfo)
5456f8829a4aSRandall Stewart {
5457f8829a4aSRandall Stewart 	/*
5458f8829a4aSRandall Stewart 	 * MSG flags we will look at MSG_DONTWAIT - non-blocking IO.
5459f8829a4aSRandall Stewart 	 * MSG_PEEK - Look don't touch :-D (only valid with OUT mbuf copy
5460f8829a4aSRandall Stewart 	 * mp=NULL thus uio is the copy method to userland) MSG_WAITALL - ??
5461f8829a4aSRandall Stewart 	 * On the way out we may send out any combination of:
5462f8829a4aSRandall Stewart 	 * MSG_NOTIFICATION MSG_EOR
5463f8829a4aSRandall Stewart 	 *
5464f8829a4aSRandall Stewart 	 */
5465f8829a4aSRandall Stewart 	struct sctp_inpcb *inp = NULL;
546658e6eeefSMichael Tuexen 	ssize_t my_len = 0;
546758e6eeefSMichael Tuexen 	ssize_t cp_len = 0;
54680d3cf13dSMichael Tuexen 	int error = 0;
5469f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL;
547094b0d969SMichael Tuexen 	struct mbuf *m = NULL;
5471f8829a4aSRandall Stewart 	struct sctp_tcb *stcb = NULL;
5472f8829a4aSRandall Stewart 	int wakeup_read_socket = 0;
5473f8829a4aSRandall Stewart 	int freecnt_applied = 0;
5474f8829a4aSRandall Stewart 	int out_flags = 0, in_flags = 0;
5475f8829a4aSRandall Stewart 	int block_allowed = 1;
54764c9179adSRandall Stewart 	uint32_t freed_so_far = 0;
547758e6eeefSMichael Tuexen 	ssize_t copied_so_far = 0;
547893164cf9SRandall Stewart 	int in_eeor_mode = 0;
5479f8829a4aSRandall Stewart 	int no_rcv_needed = 0;
5480f8829a4aSRandall Stewart 	uint32_t rwnd_req = 0;
5481f8829a4aSRandall Stewart 	int hold_sblock = 0;
5482f8829a4aSRandall Stewart 	int hold_rlock = 0;
54839a8e3088SMichael Tuexen 	ssize_t slen = 0;
54844c9179adSRandall Stewart 	uint32_t held_length = 0;
54857abab911SRobert Watson 	int sockbuf_lock = 0;
5486f8829a4aSRandall Stewart 
548717205eccSRandall Stewart 	if (uio == NULL) {
5488c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
548917205eccSRandall Stewart 		return (EINVAL);
549017205eccSRandall Stewart 	}
54910053ed28SMichael Tuexen 
5492f8829a4aSRandall Stewart 	if (msg_flags) {
5493f8829a4aSRandall Stewart 		in_flags = *msg_flags;
5494c105859eSRandall Stewart 		if (in_flags & MSG_PEEK)
5495c105859eSRandall Stewart 			SCTP_STAT_INCR(sctps_read_peeks);
5496f8829a4aSRandall Stewart 	} else {
5497f8829a4aSRandall Stewart 		in_flags = 0;
5498f8829a4aSRandall Stewart 	}
5499f8829a4aSRandall Stewart 	slen = uio->uio_resid;
550017205eccSRandall Stewart 
5501f8829a4aSRandall Stewart 	/* Pull in and set up our int flags */
5502f8829a4aSRandall Stewart 	if (in_flags & MSG_OOB) {
5503f8829a4aSRandall Stewart 		/* Out of band's NOT supported */
5504f8829a4aSRandall Stewart 		return (EOPNOTSUPP);
5505f8829a4aSRandall Stewart 	}
5506f8829a4aSRandall Stewart 	if ((in_flags & MSG_PEEK) && (mp != NULL)) {
5507c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5508f8829a4aSRandall Stewart 		return (EINVAL);
5509f8829a4aSRandall Stewart 	}
5510f8829a4aSRandall Stewart 	if ((in_flags & (MSG_DONTWAIT
5511f8829a4aSRandall Stewart 	    | MSG_NBIO
5512f8829a4aSRandall Stewart 	    )) ||
551342551e99SRandall Stewart 	    SCTP_SO_IS_NBIO(so)) {
5514f8829a4aSRandall Stewart 		block_allowed = 0;
5515f8829a4aSRandall Stewart 	}
5516f8829a4aSRandall Stewart 	/* setup the endpoint */
5517f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
5518f8829a4aSRandall Stewart 	if (inp == NULL) {
5519c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
5520f8829a4aSRandall Stewart 		return (EFAULT);
5521f8829a4aSRandall Stewart 	}
552262c1ff9cSRandall Stewart 	rwnd_req = (SCTP_SB_LIMIT_RCV(so) >> SCTP_RWND_HIWAT_SHIFT);
5523f8829a4aSRandall Stewart 	/* Must be at least a MTU's worth */
5524f8829a4aSRandall Stewart 	if (rwnd_req < SCTP_MIN_RWND)
5525f8829a4aSRandall Stewart 		rwnd_req = SCTP_MIN_RWND;
5526f8829a4aSRandall Stewart 	in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
5527b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5528f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SORECV_ENTER,
55299a8e3088SMichael Tuexen 		    rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, (uint32_t)uio->uio_resid);
553080fefe0aSRandall Stewart 	}
5531b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5532f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SORECV_ENTERPL,
55339a8e3088SMichael Tuexen 		    rwnd_req, block_allowed, so->so_rcv.sb_cc, (uint32_t)uio->uio_resid);
553480fefe0aSRandall Stewart 	}
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 #ifdef SCTP_MBUF_LOGGING
6413f8829a4aSRandall Stewart struct mbuf *
6414f8829a4aSRandall Stewart sctp_m_free(struct mbuf *m)
6415f8829a4aSRandall Stewart {
6416b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6417f8829a4aSRandall Stewart 		sctp_log_mb(m, SCTP_MBUF_IFREE);
6418f8829a4aSRandall Stewart 	}
6419f8829a4aSRandall Stewart 	return (m_free(m));
6420f8829a4aSRandall Stewart }
6421f8829a4aSRandall Stewart 
6422f8829a4aSRandall Stewart void
6423f8829a4aSRandall Stewart sctp_m_freem(struct mbuf *mb)
6424f8829a4aSRandall Stewart {
6425f8829a4aSRandall Stewart 	while (mb != NULL)
6426f8829a4aSRandall Stewart 		mb = sctp_m_free(mb);
6427f8829a4aSRandall Stewart }
6428f8829a4aSRandall Stewart 
6429f8829a4aSRandall Stewart #endif
6430f8829a4aSRandall Stewart 
643142551e99SRandall Stewart int
643242551e99SRandall Stewart sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id)
643342551e99SRandall Stewart {
643442551e99SRandall Stewart 	/*
643542551e99SRandall Stewart 	 * Given a local address. For all associations that holds the
643642551e99SRandall Stewart 	 * address, request a peer-set-primary.
643742551e99SRandall Stewart 	 */
643842551e99SRandall Stewart 	struct sctp_ifa *ifa;
643942551e99SRandall Stewart 	struct sctp_laddr *wi;
644042551e99SRandall Stewart 
64417f0ad227SMichael Tuexen 	ifa = sctp_find_ifa_by_addr(sa, vrf_id, SCTP_ADDR_NOT_LOCKED);
644242551e99SRandall Stewart 	if (ifa == NULL) {
6443c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EADDRNOTAVAIL);
644442551e99SRandall Stewart 		return (EADDRNOTAVAIL);
644542551e99SRandall Stewart 	}
644642551e99SRandall Stewart 	/*
644742551e99SRandall Stewart 	 * Now that we have the ifa we must awaken the iterator with this
644842551e99SRandall Stewart 	 * message.
644942551e99SRandall Stewart 	 */
6450b3f1ea41SRandall Stewart 	wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
645142551e99SRandall Stewart 	if (wi == NULL) {
6452c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
645342551e99SRandall Stewart 		return (ENOMEM);
645442551e99SRandall Stewart 	}
645542551e99SRandall Stewart 	/* Now incr the count and int wi structure */
645642551e99SRandall Stewart 	SCTP_INCR_LADDR_COUNT();
64575ba7f91fSMichael Tuexen 	memset(wi, 0, sizeof(*wi));
6458d61a0ae0SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
645942551e99SRandall Stewart 	wi->ifa = ifa;
646042551e99SRandall Stewart 	wi->action = SCTP_SET_PRIM_ADDR;
646142551e99SRandall Stewart 	atomic_add_int(&ifa->refcount, 1);
646242551e99SRandall Stewart 
646342551e99SRandall Stewart 	/* Now add it to the work queue */
6464f7517433SRandall Stewart 	SCTP_WQ_ADDR_LOCK();
646542551e99SRandall Stewart 	/*
646642551e99SRandall Stewart 	 * Should this really be a tailq? As it is we will process the
646742551e99SRandall Stewart 	 * newest first :-0
646842551e99SRandall Stewart 	 */
6469b3f1ea41SRandall Stewart 	LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
647042551e99SRandall Stewart 	sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
647142551e99SRandall Stewart 	    (struct sctp_inpcb *)NULL,
647242551e99SRandall Stewart 	    (struct sctp_tcb *)NULL,
647342551e99SRandall Stewart 	    (struct sctp_nets *)NULL);
64742c62ba73SMichael Tuexen 	SCTP_WQ_ADDR_UNLOCK();
647542551e99SRandall Stewart 	return (0);
647642551e99SRandall Stewart }
647742551e99SRandall Stewart 
6478f8829a4aSRandall Stewart int
647917205eccSRandall Stewart sctp_soreceive(struct socket *so,
648017205eccSRandall Stewart     struct sockaddr **psa,
648117205eccSRandall Stewart     struct uio *uio,
648217205eccSRandall Stewart     struct mbuf **mp0,
648317205eccSRandall Stewart     struct mbuf **controlp,
648417205eccSRandall Stewart     int *flagsp)
6485f8829a4aSRandall Stewart {
6486f8829a4aSRandall Stewart 	int error, fromlen;
6487f8829a4aSRandall Stewart 	uint8_t sockbuf[256];
6488f8829a4aSRandall Stewart 	struct sockaddr *from;
6489f8829a4aSRandall Stewart 	struct sctp_extrcvinfo sinfo;
6490f8829a4aSRandall Stewart 	int filling_sinfo = 1;
649146bf534cSMichael Tuexen 	int flags;
6492f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
6493f8829a4aSRandall Stewart 
6494f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
6495f8829a4aSRandall Stewart 	/* pickup the assoc we are reading from */
6496f8829a4aSRandall Stewart 	if (inp == NULL) {
6497c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6498f8829a4aSRandall Stewart 		return (EINVAL);
6499f8829a4aSRandall Stewart 	}
6500e2e7c62eSMichael Tuexen 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) &&
6501e2e7c62eSMichael Tuexen 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
6502e2e7c62eSMichael Tuexen 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) ||
6503f8829a4aSRandall Stewart 	    (controlp == NULL)) {
6504f8829a4aSRandall Stewart 		/* user does not want the sndrcv ctl */
6505f8829a4aSRandall Stewart 		filling_sinfo = 0;
6506f8829a4aSRandall Stewart 	}
6507f8829a4aSRandall Stewart 	if (psa) {
6508f8829a4aSRandall Stewart 		from = (struct sockaddr *)sockbuf;
6509f8829a4aSRandall Stewart 		fromlen = sizeof(sockbuf);
6510f8829a4aSRandall Stewart 		from->sa_len = 0;
6511f8829a4aSRandall Stewart 	} else {
6512f8829a4aSRandall Stewart 		from = NULL;
6513f8829a4aSRandall Stewart 		fromlen = 0;
6514f8829a4aSRandall Stewart 	}
6515f8829a4aSRandall Stewart 
6516e432298aSXin LI 	if (filling_sinfo) {
6517e432298aSXin LI 		memset(&sinfo, 0, sizeof(struct sctp_extrcvinfo));
6518e432298aSXin LI 	}
651946bf534cSMichael Tuexen 	if (flagsp != NULL) {
652046bf534cSMichael Tuexen 		flags = *flagsp;
652146bf534cSMichael Tuexen 	} else {
652246bf534cSMichael Tuexen 		flags = 0;
652346bf534cSMichael Tuexen 	}
652446bf534cSMichael Tuexen 	error = sctp_sorecvmsg(so, uio, mp0, from, fromlen, &flags,
6525f8829a4aSRandall Stewart 	    (struct sctp_sndrcvinfo *)&sinfo, filling_sinfo);
652646bf534cSMichael Tuexen 	if (flagsp != NULL) {
652746bf534cSMichael Tuexen 		*flagsp = flags;
652846bf534cSMichael Tuexen 	}
6529e432298aSXin LI 	if (controlp != NULL) {
6530f8829a4aSRandall Stewart 		/* copy back the sinfo in a CMSG format */
653146bf534cSMichael Tuexen 		if (filling_sinfo && ((flags & MSG_NOTIFICATION) == 0)) {
6532f8829a4aSRandall Stewart 			*controlp = sctp_build_ctl_nchunk(inp,
6533f8829a4aSRandall Stewart 			    (struct sctp_sndrcvinfo *)&sinfo);
653446bf534cSMichael Tuexen 		} else {
6535f8829a4aSRandall Stewart 			*controlp = NULL;
6536f8829a4aSRandall Stewart 		}
653746bf534cSMichael Tuexen 	}
6538f8829a4aSRandall Stewart 	if (psa) {
6539f8829a4aSRandall Stewart 		/* copy back the address info */
6540f8829a4aSRandall Stewart 		if (from && from->sa_len) {
6541f8829a4aSRandall Stewart 			*psa = sodupsockaddr(from, M_NOWAIT);
6542f8829a4aSRandall Stewart 		} else {
6543f8829a4aSRandall Stewart 			*psa = NULL;
6544f8829a4aSRandall Stewart 		}
6545f8829a4aSRandall Stewart 	}
6546f8829a4aSRandall Stewart 	return (error);
6547f8829a4aSRandall Stewart }
654817205eccSRandall Stewart 
654917205eccSRandall Stewart int
6550d61a0ae0SRandall Stewart sctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr,
6551d61a0ae0SRandall Stewart     int totaddr, int *error)
655217205eccSRandall Stewart {
655317205eccSRandall Stewart 	int added = 0;
655417205eccSRandall Stewart 	int i;
655517205eccSRandall Stewart 	struct sctp_inpcb *inp;
655617205eccSRandall Stewart 	struct sockaddr *sa;
655717205eccSRandall Stewart 	size_t incr = 0;
655892776dfdSMichael Tuexen #ifdef INET
655992776dfdSMichael Tuexen 	struct sockaddr_in *sin;
656092776dfdSMichael Tuexen #endif
656192776dfdSMichael Tuexen #ifdef INET6
656292776dfdSMichael Tuexen 	struct sockaddr_in6 *sin6;
656392776dfdSMichael Tuexen #endif
656492776dfdSMichael Tuexen 
656517205eccSRandall Stewart 	sa = addr;
656617205eccSRandall Stewart 	inp = stcb->sctp_ep;
656717205eccSRandall Stewart 	*error = 0;
656817205eccSRandall Stewart 	for (i = 0; i < totaddr; i++) {
6569ea5eba11SMichael Tuexen 		switch (sa->sa_family) {
6570ea5eba11SMichael Tuexen #ifdef INET
6571ea5eba11SMichael Tuexen 		case AF_INET:
657217205eccSRandall Stewart 			incr = sizeof(struct sockaddr_in);
657392776dfdSMichael Tuexen 			sin = (struct sockaddr_in *)sa;
657492776dfdSMichael Tuexen 			if ((sin->sin_addr.s_addr == INADDR_ANY) ||
657592776dfdSMichael Tuexen 			    (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
657692776dfdSMichael Tuexen 			    IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
657792776dfdSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6578ba785902SMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6579ba785902SMichael Tuexen 				    SCTP_FROM_SCTPUTIL + SCTP_LOC_7);
658092776dfdSMichael Tuexen 				*error = EINVAL;
658192776dfdSMichael Tuexen 				goto out_now;
658292776dfdSMichael Tuexen 			}
65837154bf4aSMichael Tuexen 			if (sctp_add_remote_addr(stcb, sa, NULL, stcb->asoc.port,
65847154bf4aSMichael Tuexen 			    SCTP_DONOT_SETSCOPE,
65857154bf4aSMichael Tuexen 			    SCTP_ADDR_IS_CONFIRMED)) {
658617205eccSRandall Stewart 				/* assoc gone no un-lock */
6587c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6588ba785902SMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6589ba785902SMichael Tuexen 				    SCTP_FROM_SCTPUTIL + SCTP_LOC_8);
659017205eccSRandall Stewart 				*error = ENOBUFS;
659117205eccSRandall Stewart 				goto out_now;
659217205eccSRandall Stewart 			}
659317205eccSRandall Stewart 			added++;
6594ea5eba11SMichael Tuexen 			break;
6595ea5eba11SMichael Tuexen #endif
6596ea5eba11SMichael Tuexen #ifdef INET6
6597ea5eba11SMichael Tuexen 		case AF_INET6:
659817205eccSRandall Stewart 			incr = sizeof(struct sockaddr_in6);
659992776dfdSMichael Tuexen 			sin6 = (struct sockaddr_in6 *)sa;
660092776dfdSMichael Tuexen 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
660192776dfdSMichael Tuexen 			    IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
660292776dfdSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6603ba785902SMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6604ba785902SMichael Tuexen 				    SCTP_FROM_SCTPUTIL + SCTP_LOC_9);
660592776dfdSMichael Tuexen 				*error = EINVAL;
660692776dfdSMichael Tuexen 				goto out_now;
660792776dfdSMichael Tuexen 			}
66087154bf4aSMichael Tuexen 			if (sctp_add_remote_addr(stcb, sa, NULL, stcb->asoc.port,
66097154bf4aSMichael Tuexen 			    SCTP_DONOT_SETSCOPE,
66107154bf4aSMichael Tuexen 			    SCTP_ADDR_IS_CONFIRMED)) {
661117205eccSRandall Stewart 				/* assoc gone no un-lock */
6612c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6613ba785902SMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6614ba785902SMichael Tuexen 				    SCTP_FROM_SCTPUTIL + SCTP_LOC_10);
661517205eccSRandall Stewart 				*error = ENOBUFS;
661617205eccSRandall Stewart 				goto out_now;
661717205eccSRandall Stewart 			}
661817205eccSRandall Stewart 			added++;
6619ea5eba11SMichael Tuexen 			break;
6620ea5eba11SMichael Tuexen #endif
6621ea5eba11SMichael Tuexen 		default:
6622ea5eba11SMichael Tuexen 			break;
662317205eccSRandall Stewart 		}
662417205eccSRandall Stewart 		sa = (struct sockaddr *)((caddr_t)sa + incr);
662517205eccSRandall Stewart 	}
662617205eccSRandall Stewart out_now:
662717205eccSRandall Stewart 	return (added);
662817205eccSRandall Stewart }
662917205eccSRandall Stewart 
6630fc26bf71SMichael Tuexen int
6631d61a0ae0SRandall Stewart sctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr,
6632fc26bf71SMichael Tuexen     unsigned int totaddr,
6633fc26bf71SMichael Tuexen     unsigned int *num_v4, unsigned int *num_v6,
6634fc26bf71SMichael Tuexen     unsigned int limit)
663517205eccSRandall Stewart {
663617205eccSRandall Stewart 	struct sockaddr *sa;
6637fc26bf71SMichael Tuexen 	struct sctp_tcb *stcb;
66389a8e3088SMichael Tuexen 	unsigned int incr, at, i;
663917205eccSRandall Stewart 
6640e1949767SMichael Tuexen 	at = 0;
664117205eccSRandall Stewart 	sa = addr;
6642fc26bf71SMichael Tuexen 	*num_v6 = *num_v4 = 0;
664317205eccSRandall Stewart 	/* account and validate addresses */
6644fc26bf71SMichael Tuexen 	if (totaddr == 0) {
6645fc26bf71SMichael Tuexen 		return (EINVAL);
6646fc26bf71SMichael Tuexen 	}
6647fc26bf71SMichael Tuexen 	for (i = 0; i < totaddr; i++) {
6648fc26bf71SMichael Tuexen 		if (at + sizeof(struct sockaddr) > limit) {
6649fc26bf71SMichael Tuexen 			return (EINVAL);
6650fc26bf71SMichael Tuexen 		}
6651ea5eba11SMichael Tuexen 		switch (sa->sa_family) {
6652ea5eba11SMichael Tuexen #ifdef INET
6653ea5eba11SMichael Tuexen 		case AF_INET:
6654e1949767SMichael Tuexen 			incr = (unsigned int)sizeof(struct sockaddr_in);
6655d61a0ae0SRandall Stewart 			if (sa->sa_len != incr) {
6656fc26bf71SMichael Tuexen 				return (EINVAL);
6657d61a0ae0SRandall Stewart 			}
66589a8e3088SMichael Tuexen 			(*num_v4) += 1;
6659ea5eba11SMichael Tuexen 			break;
6660ea5eba11SMichael Tuexen #endif
6661ea5eba11SMichael Tuexen #ifdef INET6
6662ea5eba11SMichael Tuexen 		case AF_INET6:
6663ea5eba11SMichael Tuexen 			{
666417205eccSRandall Stewart 				struct sockaddr_in6 *sin6;
666517205eccSRandall Stewart 
666617205eccSRandall Stewart 				sin6 = (struct sockaddr_in6 *)sa;
666717205eccSRandall Stewart 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
666817205eccSRandall Stewart 					/* Must be non-mapped for connectx */
6669fc26bf71SMichael Tuexen 					return (EINVAL);
667017205eccSRandall Stewart 				}
6671e1949767SMichael Tuexen 				incr = (unsigned int)sizeof(struct sockaddr_in6);
6672d61a0ae0SRandall Stewart 				if (sa->sa_len != incr) {
6673fc26bf71SMichael Tuexen 					return (EINVAL);
6674d61a0ae0SRandall Stewart 				}
66759a8e3088SMichael Tuexen 				(*num_v6) += 1;
6676ea5eba11SMichael Tuexen 				break;
6677ea5eba11SMichael Tuexen 			}
6678ea5eba11SMichael Tuexen #endif
6679ea5eba11SMichael Tuexen 		default:
6680fc26bf71SMichael Tuexen 			return (EINVAL);
668117205eccSRandall Stewart 		}
6682fc26bf71SMichael Tuexen 		if ((at + incr) > limit) {
6683fc26bf71SMichael Tuexen 			return (EINVAL);
6684ea5eba11SMichael Tuexen 		}
6685d61a0ae0SRandall Stewart 		SCTP_INP_INCR_REF(inp);
668617205eccSRandall Stewart 		stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
668717205eccSRandall Stewart 		if (stcb != NULL) {
6688fc26bf71SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
6689fc26bf71SMichael Tuexen 			return (EALREADY);
6690d61a0ae0SRandall Stewart 		} else {
6691d61a0ae0SRandall Stewart 			SCTP_INP_DECR_REF(inp);
669217205eccSRandall Stewart 		}
6693fc26bf71SMichael Tuexen 		at += incr;
669417205eccSRandall Stewart 		sa = (struct sockaddr *)((caddr_t)sa + incr);
669517205eccSRandall Stewart 	}
6696fc26bf71SMichael Tuexen 	return (0);
669717205eccSRandall Stewart }
669835918f85SRandall Stewart 
669935918f85SRandall Stewart /*
670035918f85SRandall Stewart  * sctp_bindx(ADD) for one address.
670135918f85SRandall Stewart  * assumes all arguments are valid/checked by caller.
670235918f85SRandall Stewart  */
670335918f85SRandall Stewart void
670435918f85SRandall Stewart sctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp,
67057621bd5eSMichael Tuexen     struct sockaddr *sa, uint32_t vrf_id, int *error,
67067621bd5eSMichael Tuexen     void *p)
670735918f85SRandall Stewart {
6708d59107f7SMichael Tuexen #if defined(INET) && defined(INET6)
670935918f85SRandall Stewart 	struct sockaddr_in sin;
67105e2c2d87SRandall Stewart #endif
67117621bd5eSMichael Tuexen #ifdef INET6
67127621bd5eSMichael Tuexen 	struct sockaddr_in6 *sin6;
67137621bd5eSMichael Tuexen #endif
67147621bd5eSMichael Tuexen #ifdef INET
67157621bd5eSMichael Tuexen 	struct sockaddr_in *sinp;
67167621bd5eSMichael Tuexen #endif
67177621bd5eSMichael Tuexen 	struct sockaddr *addr_to_use;
67187621bd5eSMichael Tuexen 	struct sctp_inpcb *lep;
67197621bd5eSMichael Tuexen 	uint16_t port;
67205e2c2d87SRandall Stewart 
672135918f85SRandall Stewart 	/* see if we're bound all already! */
672235918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6723c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
672435918f85SRandall Stewart 		*error = EINVAL;
672535918f85SRandall Stewart 		return;
672635918f85SRandall Stewart 	}
67277621bd5eSMichael Tuexen 	switch (sa->sa_family) {
6728ea5eba11SMichael Tuexen #ifdef INET6
67297621bd5eSMichael Tuexen 	case AF_INET6:
673035918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6731c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
673235918f85SRandall Stewart 			*error = EINVAL;
673335918f85SRandall Stewart 			return;
673435918f85SRandall Stewart 		}
6735db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6736db4fd95bSRandall Stewart 			/* can only bind v6 on PF_INET6 sockets */
6737c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6738db4fd95bSRandall Stewart 			*error = EINVAL;
6739db4fd95bSRandall Stewart 			return;
6740db4fd95bSRandall Stewart 		}
67417621bd5eSMichael Tuexen 		sin6 = (struct sockaddr_in6 *)sa;
67427621bd5eSMichael Tuexen 		port = sin6->sin6_port;
6743d59107f7SMichael Tuexen #ifdef INET
674435918f85SRandall Stewart 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6745db4fd95bSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6746db4fd95bSRandall Stewart 			    SCTP_IPV6_V6ONLY(inp)) {
6747db4fd95bSRandall Stewart 				/* can't bind v4-mapped on PF_INET sockets */
6748c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6749db4fd95bSRandall Stewart 				*error = EINVAL;
6750db4fd95bSRandall Stewart 				return;
6751db4fd95bSRandall Stewart 			}
675235918f85SRandall Stewart 			in6_sin6_2_sin(&sin, sin6);
67537621bd5eSMichael Tuexen 			addr_to_use = (struct sockaddr *)&sin;
67547621bd5eSMichael Tuexen 		} else {
67557621bd5eSMichael Tuexen 			addr_to_use = sa;
675635918f85SRandall Stewart 		}
67575087b6e7SMichael Tuexen #else
67585087b6e7SMichael Tuexen 		addr_to_use = sa;
6759d59107f7SMichael Tuexen #endif
67607621bd5eSMichael Tuexen 		break;
676135918f85SRandall Stewart #endif
6762ea5eba11SMichael Tuexen #ifdef INET
67637621bd5eSMichael Tuexen 	case AF_INET:
676435918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in)) {
6765c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
676635918f85SRandall Stewart 			*error = EINVAL;
676735918f85SRandall Stewart 			return;
676835918f85SRandall Stewart 		}
6769db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6770db4fd95bSRandall Stewart 		    SCTP_IPV6_V6ONLY(inp)) {
6771db4fd95bSRandall Stewart 			/* can't bind v4 on PF_INET sockets */
6772c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6773db4fd95bSRandall Stewart 			*error = EINVAL;
6774db4fd95bSRandall Stewart 			return;
6775db4fd95bSRandall Stewart 		}
67767621bd5eSMichael Tuexen 		sinp = (struct sockaddr_in *)sa;
67777621bd5eSMichael Tuexen 		port = sinp->sin_port;
67787621bd5eSMichael Tuexen 		addr_to_use = sa;
67797621bd5eSMichael Tuexen 		break;
6780ea5eba11SMichael Tuexen #endif
67817621bd5eSMichael Tuexen 	default:
67827621bd5eSMichael Tuexen 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
67837621bd5eSMichael Tuexen 		*error = EINVAL;
67847621bd5eSMichael Tuexen 		return;
67857621bd5eSMichael Tuexen 	}
678635918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
678735918f85SRandall Stewart 		if (p == NULL) {
678835918f85SRandall Stewart 			/* Can't get proc for Net/Open BSD */
6789c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
679035918f85SRandall Stewart 			*error = EINVAL;
679135918f85SRandall Stewart 			return;
679235918f85SRandall Stewart 		}
67937621bd5eSMichael Tuexen 		*error = sctp_inpcb_bind(so, addr_to_use, NULL, p);
679435918f85SRandall Stewart 		return;
679535918f85SRandall Stewart 	}
67967621bd5eSMichael Tuexen 	/* Validate the incoming port. */
67977621bd5eSMichael Tuexen 	if ((port != 0) && (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 	}
68027621bd5eSMichael Tuexen 	lep = sctp_pcb_findep(addr_to_use, 1, 0, vrf_id);
68037621bd5eSMichael Tuexen 	if (lep == NULL) {
68047621bd5eSMichael Tuexen 		/* add the address */
68057621bd5eSMichael Tuexen 		*error = sctp_addr_mgmt_ep_sa(inp, addr_to_use,
68067a9dbc33SMichael Tuexen 		    SCTP_ADD_IP_ADDRESS, vrf_id);
680735918f85SRandall Stewart 	} else {
68087621bd5eSMichael Tuexen 		if (lep != inp) {
680935918f85SRandall Stewart 			*error = EADDRINUSE;
681035918f85SRandall Stewart 		}
68117621bd5eSMichael Tuexen 		SCTP_INP_DECR_REF(lep);
681235918f85SRandall Stewart 	}
681335918f85SRandall Stewart }
681435918f85SRandall Stewart 
681535918f85SRandall Stewart /*
681635918f85SRandall Stewart  * sctp_bindx(DELETE) for one address.
681735918f85SRandall Stewart  * assumes all arguments are valid/checked by caller.
681835918f85SRandall Stewart  */
681935918f85SRandall Stewart void
68207215cc1bSMichael Tuexen sctp_bindx_delete_address(struct sctp_inpcb *inp,
68217621bd5eSMichael Tuexen     struct sockaddr *sa, uint32_t vrf_id, int *error)
682235918f85SRandall Stewart {
68237621bd5eSMichael Tuexen 	struct sockaddr *addr_to_use;
6824d59107f7SMichael Tuexen #if defined(INET) && defined(INET6)
68257621bd5eSMichael Tuexen 	struct sockaddr_in6 *sin6;
682635918f85SRandall Stewart 	struct sockaddr_in sin;
68275e2c2d87SRandall Stewart #endif
68285e2c2d87SRandall Stewart 
682935918f85SRandall Stewart 	/* see if we're bound all already! */
683035918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6831c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
683235918f85SRandall Stewart 		*error = EINVAL;
683335918f85SRandall Stewart 		return;
683435918f85SRandall Stewart 	}
68357621bd5eSMichael Tuexen 	switch (sa->sa_family) {
6836e0e00a4dSMichael Tuexen #ifdef INET6
68377621bd5eSMichael Tuexen 	case AF_INET6:
683835918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6839c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
684035918f85SRandall Stewart 			*error = EINVAL;
684135918f85SRandall Stewart 			return;
684235918f85SRandall Stewart 		}
6843db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6844db4fd95bSRandall Stewart 			/* can only bind v6 on PF_INET6 sockets */
6845c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6846db4fd95bSRandall Stewart 			*error = EINVAL;
6847db4fd95bSRandall Stewart 			return;
6848db4fd95bSRandall Stewart 		}
6849d59107f7SMichael Tuexen #ifdef INET
68507621bd5eSMichael Tuexen 		sin6 = (struct sockaddr_in6 *)sa;
685135918f85SRandall Stewart 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6852db4fd95bSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6853db4fd95bSRandall Stewart 			    SCTP_IPV6_V6ONLY(inp)) {
6854db4fd95bSRandall Stewart 				/* can't bind mapped-v4 on PF_INET sockets */
6855c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6856db4fd95bSRandall Stewart 				*error = EINVAL;
6857db4fd95bSRandall Stewart 				return;
6858db4fd95bSRandall Stewart 			}
685935918f85SRandall Stewart 			in6_sin6_2_sin(&sin, sin6);
68607621bd5eSMichael Tuexen 			addr_to_use = (struct sockaddr *)&sin;
68617621bd5eSMichael Tuexen 		} else {
68627621bd5eSMichael Tuexen 			addr_to_use = sa;
686335918f85SRandall Stewart 		}
6864171edd21SMichael Tuexen #else
6865171edd21SMichael Tuexen 		addr_to_use = sa;
6866d59107f7SMichael Tuexen #endif
68677621bd5eSMichael Tuexen 		break;
686835918f85SRandall Stewart #endif
6869ea5eba11SMichael Tuexen #ifdef INET
68707621bd5eSMichael Tuexen 	case AF_INET:
687135918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in)) {
6872c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
687335918f85SRandall Stewart 			*error = EINVAL;
687435918f85SRandall Stewart 			return;
687535918f85SRandall Stewart 		}
6876db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6877db4fd95bSRandall Stewart 		    SCTP_IPV6_V6ONLY(inp)) {
6878db4fd95bSRandall Stewart 			/* can't bind 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 		}
68837621bd5eSMichael Tuexen 		addr_to_use = sa;
68847621bd5eSMichael Tuexen 		break;
6885ea5eba11SMichael Tuexen #endif
68867621bd5eSMichael Tuexen 	default:
68877621bd5eSMichael Tuexen 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
68887621bd5eSMichael Tuexen 		*error = EINVAL;
68897621bd5eSMichael Tuexen 		return;
689035918f85SRandall Stewart 	}
68917621bd5eSMichael Tuexen 	/* No lock required mgmt_ep_sa does its own locking. */
68927621bd5eSMichael Tuexen 	*error = sctp_addr_mgmt_ep_sa(inp, addr_to_use, SCTP_DEL_IP_ADDRESS,
68937621bd5eSMichael Tuexen 	    vrf_id);
689435918f85SRandall Stewart }
68951b649582SRandall Stewart 
68961b649582SRandall Stewart /*
68971b649582SRandall Stewart  * returns the valid local address count for an assoc, taking into account
68981b649582SRandall Stewart  * all scoping rules
68991b649582SRandall Stewart  */
69001b649582SRandall Stewart int
69011b649582SRandall Stewart sctp_local_addr_count(struct sctp_tcb *stcb)
69021b649582SRandall Stewart {
6903b54ddf22SMichael Tuexen 	int loopback_scope;
6904b54ddf22SMichael Tuexen #if defined(INET)
6905b54ddf22SMichael Tuexen 	int ipv4_local_scope, ipv4_addr_legal;
6906b54ddf22SMichael Tuexen #endif
6907b54ddf22SMichael Tuexen #if defined(INET6)
6908b54ddf22SMichael Tuexen 	int local_scope, site_scope, ipv6_addr_legal;
6909b54ddf22SMichael Tuexen #endif
69101b649582SRandall Stewart 	struct sctp_vrf *vrf;
69111b649582SRandall Stewart 	struct sctp_ifn *sctp_ifn;
69121b649582SRandall Stewart 	struct sctp_ifa *sctp_ifa;
69131b649582SRandall Stewart 	int count = 0;
69141b649582SRandall Stewart 
69151b649582SRandall Stewart 	/* Turn on all the appropriate scopes */
6916a1cb341bSMichael Tuexen 	loopback_scope = stcb->asoc.scope.loopback_scope;
6917b54ddf22SMichael Tuexen #if defined(INET)
6918a1cb341bSMichael Tuexen 	ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
6919b54ddf22SMichael Tuexen 	ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
6920b54ddf22SMichael Tuexen #endif
6921b54ddf22SMichael Tuexen #if defined(INET6)
6922a1cb341bSMichael Tuexen 	local_scope = stcb->asoc.scope.local_scope;
6923a1cb341bSMichael Tuexen 	site_scope = stcb->asoc.scope.site_scope;
6924a1cb341bSMichael Tuexen 	ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
6925b54ddf22SMichael Tuexen #endif
6926c99efcf6SRandall Stewart 	SCTP_IPI_ADDR_RLOCK();
69271b649582SRandall Stewart 	vrf = sctp_find_vrf(stcb->asoc.vrf_id);
69281b649582SRandall Stewart 	if (vrf == NULL) {
69291b649582SRandall Stewart 		/* no vrf, no addresses */
6930c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RUNLOCK();
69311b649582SRandall Stewart 		return (0);
69321b649582SRandall Stewart 	}
69330053ed28SMichael Tuexen 
69341b649582SRandall Stewart 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
69351b649582SRandall Stewart 		/*
69361b649582SRandall Stewart 		 * bound all case: go through all ifns on the vrf
69371b649582SRandall Stewart 		 */
69381b649582SRandall Stewart 		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
69391b649582SRandall Stewart 			if ((loopback_scope == 0) &&
69401b649582SRandall Stewart 			    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
69411b649582SRandall Stewart 				continue;
69421b649582SRandall Stewart 			}
69431b649582SRandall Stewart 			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
69441b649582SRandall Stewart 				if (sctp_is_addr_restricted(stcb, sctp_ifa))
69451b649582SRandall Stewart 					continue;
69465e2c2d87SRandall Stewart 				switch (sctp_ifa->address.sa.sa_family) {
6947ea5eba11SMichael Tuexen #ifdef INET
69485e2c2d87SRandall Stewart 				case AF_INET:
69495e2c2d87SRandall Stewart 					if (ipv4_addr_legal) {
69501b649582SRandall Stewart 						struct sockaddr_in *sin;
69511b649582SRandall Stewart 
695224aaac8dSMichael Tuexen 						sin = &sctp_ifa->address.sin;
69531b649582SRandall Stewart 						if (sin->sin_addr.s_addr == 0) {
6954b7b84c0eSMichael Tuexen 							/*
6955b7b84c0eSMichael Tuexen 							 * skip unspecified
6956b7b84c0eSMichael Tuexen 							 * addrs
6957b7b84c0eSMichael Tuexen 							 */
69581b649582SRandall Stewart 							continue;
69591b649582SRandall Stewart 						}
69606ba22f19SMichael Tuexen 						if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
69616ba22f19SMichael Tuexen 						    &sin->sin_addr) != 0) {
69626ba22f19SMichael Tuexen 							continue;
69636ba22f19SMichael Tuexen 						}
69641b649582SRandall Stewart 						if ((ipv4_local_scope == 0) &&
69651b649582SRandall Stewart 						    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
69661b649582SRandall Stewart 							continue;
69671b649582SRandall Stewart 						}
69681b649582SRandall Stewart 						/* count this one */
69691b649582SRandall Stewart 						count++;
69705e2c2d87SRandall Stewart 					} else {
69715e2c2d87SRandall Stewart 						continue;
69725e2c2d87SRandall Stewart 					}
69735e2c2d87SRandall Stewart 					break;
6974ea5eba11SMichael Tuexen #endif
69755e2c2d87SRandall Stewart #ifdef INET6
69765e2c2d87SRandall Stewart 				case AF_INET6:
69775e2c2d87SRandall Stewart 					if (ipv6_addr_legal) {
69781b649582SRandall Stewart 						struct sockaddr_in6 *sin6;
69791b649582SRandall Stewart 
698024aaac8dSMichael Tuexen 						sin6 = &sctp_ifa->address.sin6;
69811b649582SRandall Stewart 						if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
69821b649582SRandall Stewart 							continue;
69831b649582SRandall Stewart 						}
69846ba22f19SMichael Tuexen 						if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
69856ba22f19SMichael Tuexen 						    &sin6->sin6_addr) != 0) {
69866ba22f19SMichael Tuexen 							continue;
69876ba22f19SMichael Tuexen 						}
69881b649582SRandall Stewart 						if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
69891b649582SRandall Stewart 							if (local_scope == 0)
69901b649582SRandall Stewart 								continue;
69911b649582SRandall Stewart 							if (sin6->sin6_scope_id == 0) {
69921b649582SRandall Stewart 								if (sa6_recoverscope(sin6) != 0)
69931b649582SRandall Stewart 									/*
69945e2c2d87SRandall Stewart 									 *
69955e2c2d87SRandall Stewart 									 * bad
69965b495f17SMichael Tuexen 									 * link
69975e2c2d87SRandall Stewart 									 *
69985b495f17SMichael Tuexen 									 * local
69995e2c2d87SRandall Stewart 									 *
70005b495f17SMichael Tuexen 									 * address
70015b495f17SMichael Tuexen 									 */
70021b649582SRandall Stewart 									continue;
70031b649582SRandall Stewart 							}
70041b649582SRandall Stewart 						}
70051b649582SRandall Stewart 						if ((site_scope == 0) &&
70061b649582SRandall Stewart 						    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
70071b649582SRandall Stewart 							continue;
70081b649582SRandall Stewart 						}
70091b649582SRandall Stewart 						/* count this one */
70101b649582SRandall Stewart 						count++;
70111b649582SRandall Stewart 					}
70125e2c2d87SRandall Stewart 					break;
70135e2c2d87SRandall Stewart #endif
70145e2c2d87SRandall Stewart 				default:
70155e2c2d87SRandall Stewart 					/* TSNH */
70165e2c2d87SRandall Stewart 					break;
70175e2c2d87SRandall Stewart 				}
70181b649582SRandall Stewart 			}
70191b649582SRandall Stewart 		}
70201b649582SRandall Stewart 	} else {
70211b649582SRandall Stewart 		/*
70221b649582SRandall Stewart 		 * subset bound case
70231b649582SRandall Stewart 		 */
70241b649582SRandall Stewart 		struct sctp_laddr *laddr;
70251b649582SRandall Stewart 
70261b649582SRandall Stewart 		LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list,
70271b649582SRandall Stewart 		    sctp_nxt_addr) {
70281b649582SRandall Stewart 			if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
70291b649582SRandall Stewart 				continue;
70301b649582SRandall Stewart 			}
70311b649582SRandall Stewart 			/* count this one */
70321b649582SRandall Stewart 			count++;
70331b649582SRandall Stewart 		}
70341b649582SRandall Stewart 	}
7035c99efcf6SRandall Stewart 	SCTP_IPI_ADDR_RUNLOCK();
70361b649582SRandall Stewart 	return (count);
70371b649582SRandall Stewart }
7038c4739e2fSRandall Stewart 
7039c4739e2fSRandall Stewart #if defined(SCTP_LOCAL_TRACE_BUF)
7040c4739e2fSRandall Stewart 
7041c4739e2fSRandall Stewart void
7042b27a6b7dSRandall 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)
7043c4739e2fSRandall Stewart {
7044b27a6b7dSRandall Stewart 	uint32_t saveindex, newindex;
7045c4739e2fSRandall Stewart 
7046c4739e2fSRandall Stewart 	do {
7047b3f1ea41SRandall Stewart 		saveindex = SCTP_BASE_SYSCTL(sctp_log).index;
7048c4739e2fSRandall Stewart 		if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
7049c4739e2fSRandall Stewart 			newindex = 1;
7050c4739e2fSRandall Stewart 		} else {
7051c4739e2fSRandall Stewart 			newindex = saveindex + 1;
7052c4739e2fSRandall Stewart 		}
7053b3f1ea41SRandall Stewart 	} while (atomic_cmpset_int(&SCTP_BASE_SYSCTL(sctp_log).index, saveindex, newindex) == 0);
7054c4739e2fSRandall Stewart 	if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
7055c4739e2fSRandall Stewart 		saveindex = 0;
7056c4739e2fSRandall Stewart 	}
7057b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].timestamp = SCTP_GET_CYCLECOUNT;
7058b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].subsys = subsys;
7059b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[0] = a;
7060b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[1] = b;
7061b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[2] = c;
7062b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[3] = d;
7063b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[4] = e;
7064b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[5] = f;
7065c4739e2fSRandall Stewart }
7066c4739e2fSRandall Stewart 
7067c4739e2fSRandall Stewart #endif
7068a99b6783SRandall Stewart static void
70697cca1775SRandall Stewart sctp_recv_udp_tunneled_packet(struct mbuf *m, int off, struct inpcb *inp,
707081d3ec17SBryan Venteicher     const struct sockaddr *sa SCTP_UNUSED, void *ctx SCTP_UNUSED)
7071a99b6783SRandall Stewart {
7072a99b6783SRandall Stewart 	struct ip *iph;
70733a51a264SMichael Tuexen #ifdef INET6
70743a51a264SMichael Tuexen 	struct ip6_hdr *ip6;
70753a51a264SMichael Tuexen #endif
7076a99b6783SRandall Stewart 	struct mbuf *sp, *last;
7077a99b6783SRandall Stewart 	struct udphdr *uhdr;
7078285052f0SMichael Tuexen 	uint16_t port;
7079a99b6783SRandall Stewart 
7080a99b6783SRandall Stewart 	if ((m->m_flags & M_PKTHDR) == 0) {
7081a99b6783SRandall Stewart 		/* Can't handle one that is not a pkt hdr */
7082a99b6783SRandall Stewart 		goto out;
7083a99b6783SRandall Stewart 	}
7084285052f0SMichael Tuexen 	/* Pull the src port */
7085a99b6783SRandall Stewart 	iph = mtod(m, struct ip *);
7086a99b6783SRandall Stewart 	uhdr = (struct udphdr *)((caddr_t)iph + off);
7087a99b6783SRandall Stewart 	port = uhdr->uh_sport;
7088285052f0SMichael Tuexen 	/*
7089285052f0SMichael Tuexen 	 * Split out the mbuf chain. Leave the IP header in m, place the
7090285052f0SMichael Tuexen 	 * rest in the sp.
7091285052f0SMichael Tuexen 	 */
7092eb1b1807SGleb Smirnoff 	sp = m_split(m, off, M_NOWAIT);
7093a99b6783SRandall Stewart 	if (sp == NULL) {
7094a99b6783SRandall Stewart 		/* Gak, drop packet, we can't do a split */
7095a99b6783SRandall Stewart 		goto out;
7096a99b6783SRandall Stewart 	}
7097285052f0SMichael Tuexen 	if (sp->m_pkthdr.len < sizeof(struct udphdr) + sizeof(struct sctphdr)) {
7098285052f0SMichael Tuexen 		/* Gak, packet can't have an SCTP header in it - too small */
7099a99b6783SRandall Stewart 		m_freem(sp);
7100a99b6783SRandall Stewart 		goto out;
7101a99b6783SRandall Stewart 	}
7102285052f0SMichael Tuexen 	/* Now pull up the UDP header and SCTP header together */
7103285052f0SMichael Tuexen 	sp = m_pullup(sp, sizeof(struct udphdr) + sizeof(struct sctphdr));
7104a99b6783SRandall Stewart 	if (sp == NULL) {
7105a99b6783SRandall Stewart 		/* Gak pullup failed */
7106a99b6783SRandall Stewart 		goto out;
7107a99b6783SRandall Stewart 	}
7108285052f0SMichael Tuexen 	/* Trim out the UDP header */
7109a99b6783SRandall Stewart 	m_adj(sp, sizeof(struct udphdr));
7110a99b6783SRandall Stewart 
7111a99b6783SRandall Stewart 	/* Now reconstruct the mbuf chain */
7112285052f0SMichael Tuexen 	for (last = m; last->m_next; last = last->m_next);
7113a99b6783SRandall Stewart 	last->m_next = sp;
7114a99b6783SRandall Stewart 	m->m_pkthdr.len += sp->m_pkthdr.len;
711552f175beSMichael Tuexen 	/*
711652f175beSMichael Tuexen 	 * The CSUM_DATA_VALID flags indicates that the HW checked the UDP
711752f175beSMichael Tuexen 	 * checksum and it was valid. Since CSUM_DATA_VALID ==
711852f175beSMichael Tuexen 	 * CSUM_SCTP_VALID this would imply that the HW also verified the
711952f175beSMichael Tuexen 	 * SCTP checksum. Therefore, clear the bit.
712052f175beSMichael Tuexen 	 */
712152f175beSMichael Tuexen 	SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
712252f175beSMichael Tuexen 	    "sctp_recv_udp_tunneled_packet(): Packet of length %d received on %s with csum_flags 0x%b.\n",
712352f175beSMichael Tuexen 	    m->m_pkthdr.len,
712452f175beSMichael Tuexen 	    if_name(m->m_pkthdr.rcvif),
712552f175beSMichael Tuexen 	    (int)m->m_pkthdr.csum_flags, CSUM_BITS);
712652f175beSMichael Tuexen 	m->m_pkthdr.csum_flags &= ~CSUM_DATA_VALID;
7127a99b6783SRandall Stewart 	iph = mtod(m, struct ip *);
7128a99b6783SRandall Stewart 	switch (iph->ip_v) {
7129e6194c2eSMichael Tuexen #ifdef INET
7130a99b6783SRandall Stewart 	case IPVERSION:
713109c1c856SMichael Tuexen 		iph->ip_len = htons(ntohs(iph->ip_len) - sizeof(struct udphdr));
7132a99b6783SRandall Stewart 		sctp_input_with_port(m, off, port);
7133a99b6783SRandall Stewart 		break;
7134e6194c2eSMichael Tuexen #endif
7135a99b6783SRandall Stewart #ifdef INET6
7136a99b6783SRandall Stewart 	case IPV6_VERSION >> 4:
71373a51a264SMichael Tuexen 		ip6 = mtod(m, struct ip6_hdr *);
71383a51a264SMichael Tuexen 		ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - sizeof(struct udphdr));
71393a51a264SMichael Tuexen 		sctp6_input_with_port(&m, &off, port);
7140a99b6783SRandall Stewart 		break;
7141a99b6783SRandall Stewart #endif
7142a99b6783SRandall Stewart 	default:
7143285052f0SMichael Tuexen 		goto out;
7144a99b6783SRandall Stewart 		break;
7145a99b6783SRandall Stewart 	}
7146a99b6783SRandall Stewart 	return;
7147a99b6783SRandall Stewart out:
7148a99b6783SRandall Stewart 	m_freem(m);
7149a99b6783SRandall Stewart }
7150c54a18d2SRandall Stewart 
7151fd7af143SMichael Tuexen #ifdef INET
7152fd7af143SMichael Tuexen static void
7153fd7af143SMichael Tuexen sctp_recv_icmp_tunneled_packet(int cmd, struct sockaddr *sa, void *vip, void *ctx SCTP_UNUSED)
7154fd7af143SMichael Tuexen {
7155fd7af143SMichael Tuexen 	struct ip *outer_ip, *inner_ip;
7156fd7af143SMichael Tuexen 	struct sctphdr *sh;
7157fd7af143SMichael Tuexen 	struct icmp *icmp;
7158fd7af143SMichael Tuexen 	struct udphdr *udp;
7159fd7af143SMichael Tuexen 	struct sctp_inpcb *inp;
7160fd7af143SMichael Tuexen 	struct sctp_tcb *stcb;
7161fd7af143SMichael Tuexen 	struct sctp_nets *net;
7162fd7af143SMichael Tuexen 	struct sctp_init_chunk *ch;
7163fd7af143SMichael Tuexen 	struct sockaddr_in src, dst;
7164fd7af143SMichael Tuexen 	uint8_t type, code;
7165fd7af143SMichael Tuexen 
7166fd7af143SMichael Tuexen 	inner_ip = (struct ip *)vip;
7167fd7af143SMichael Tuexen 	icmp = (struct icmp *)((caddr_t)inner_ip -
7168fd7af143SMichael Tuexen 	    (sizeof(struct icmp) - sizeof(struct ip)));
7169fd7af143SMichael Tuexen 	outer_ip = (struct ip *)((caddr_t)icmp - sizeof(struct ip));
7170fd7af143SMichael Tuexen 	if (ntohs(outer_ip->ip_len) <
7171fd7af143SMichael Tuexen 	    sizeof(struct ip) + 8 + (inner_ip->ip_hl << 2) + sizeof(struct udphdr) + 8) {
7172fd7af143SMichael Tuexen 		return;
7173fd7af143SMichael Tuexen 	}
7174fd7af143SMichael Tuexen 	udp = (struct udphdr *)((caddr_t)inner_ip + (inner_ip->ip_hl << 2));
7175fd7af143SMichael Tuexen 	sh = (struct sctphdr *)(udp + 1);
7176fd7af143SMichael Tuexen 	memset(&src, 0, sizeof(struct sockaddr_in));
7177fd7af143SMichael Tuexen 	src.sin_family = AF_INET;
7178fd7af143SMichael Tuexen 	src.sin_len = sizeof(struct sockaddr_in);
7179fd7af143SMichael Tuexen 	src.sin_port = sh->src_port;
7180fd7af143SMichael Tuexen 	src.sin_addr = inner_ip->ip_src;
7181fd7af143SMichael Tuexen 	memset(&dst, 0, sizeof(struct sockaddr_in));
7182fd7af143SMichael Tuexen 	dst.sin_family = AF_INET;
7183fd7af143SMichael Tuexen 	dst.sin_len = sizeof(struct sockaddr_in);
7184fd7af143SMichael Tuexen 	dst.sin_port = sh->dest_port;
7185fd7af143SMichael Tuexen 	dst.sin_addr = inner_ip->ip_dst;
7186fd7af143SMichael Tuexen 	/*
7187fd7af143SMichael Tuexen 	 * 'dst' holds the dest of the packet that failed to be sent. 'src'
7188fd7af143SMichael Tuexen 	 * holds our local endpoint address. Thus we reverse the dst and the
7189fd7af143SMichael Tuexen 	 * src in the lookup.
7190fd7af143SMichael Tuexen 	 */
7191fd7af143SMichael Tuexen 	inp = NULL;
7192fd7af143SMichael Tuexen 	net = NULL;
7193fd7af143SMichael Tuexen 	stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
7194fd7af143SMichael Tuexen 	    (struct sockaddr *)&src,
7195fd7af143SMichael Tuexen 	    &inp, &net, 1,
7196fd7af143SMichael Tuexen 	    SCTP_DEFAULT_VRFID);
7197fd7af143SMichael Tuexen 	if ((stcb != NULL) &&
7198fd7af143SMichael Tuexen 	    (net != NULL) &&
719955b8cd93SMichael Tuexen 	    (inp != NULL)) {
7200fd7af143SMichael Tuexen 		/* Check the UDP port numbers */
7201fd7af143SMichael Tuexen 		if ((udp->uh_dport != net->port) ||
7202fd7af143SMichael Tuexen 		    (udp->uh_sport != htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)))) {
7203fd7af143SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
7204fd7af143SMichael Tuexen 			return;
7205fd7af143SMichael Tuexen 		}
7206fd7af143SMichael Tuexen 		/* Check the verification tag */
7207fd7af143SMichael Tuexen 		if (ntohl(sh->v_tag) != 0) {
7208fd7af143SMichael Tuexen 			/*
7209fd7af143SMichael Tuexen 			 * This must be the verification tag used for
7210fd7af143SMichael Tuexen 			 * sending out packets. We don't consider packets
7211fd7af143SMichael Tuexen 			 * reflecting the verification tag.
7212fd7af143SMichael Tuexen 			 */
7213fd7af143SMichael Tuexen 			if (ntohl(sh->v_tag) != stcb->asoc.peer_vtag) {
7214fd7af143SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
7215fd7af143SMichael Tuexen 				return;
7216fd7af143SMichael Tuexen 			}
7217fd7af143SMichael Tuexen 		} else {
7218fd7af143SMichael Tuexen 			if (ntohs(outer_ip->ip_len) >=
7219fd7af143SMichael Tuexen 			    sizeof(struct ip) +
7220fd7af143SMichael Tuexen 			    8 + (inner_ip->ip_hl << 2) + 8 + 20) {
7221fd7af143SMichael Tuexen 				/*
7222fd7af143SMichael Tuexen 				 * In this case we can check if we got an
7223fd7af143SMichael Tuexen 				 * INIT chunk and if the initiate tag
7224fd7af143SMichael Tuexen 				 * matches.
7225fd7af143SMichael Tuexen 				 */
7226fd7af143SMichael Tuexen 				ch = (struct sctp_init_chunk *)(sh + 1);
7227fd7af143SMichael Tuexen 				if ((ch->ch.chunk_type != SCTP_INITIATION) ||
7228fd7af143SMichael Tuexen 				    (ntohl(ch->init.initiate_tag) != stcb->asoc.my_vtag)) {
7229fd7af143SMichael Tuexen 					SCTP_TCB_UNLOCK(stcb);
7230fd7af143SMichael Tuexen 					return;
7231fd7af143SMichael Tuexen 				}
7232fd7af143SMichael Tuexen 			} else {
7233fd7af143SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
7234fd7af143SMichael Tuexen 				return;
7235fd7af143SMichael Tuexen 			}
7236fd7af143SMichael Tuexen 		}
7237fd7af143SMichael Tuexen 		type = icmp->icmp_type;
7238fd7af143SMichael Tuexen 		code = icmp->icmp_code;
72393c3f9e2aSMichael Tuexen 		if ((type == ICMP_UNREACH) &&
72403c3f9e2aSMichael Tuexen 		    (code == ICMP_UNREACH_PORT)) {
7241fd7af143SMichael Tuexen 			code = ICMP_UNREACH_PROTOCOL;
7242fd7af143SMichael Tuexen 		}
7243fd7af143SMichael Tuexen 		sctp_notify(inp, stcb, net, type, code,
7244fd7af143SMichael Tuexen 		    ntohs(inner_ip->ip_len),
72456ebfa5eeSMichael Tuexen 		    (uint32_t)ntohs(icmp->icmp_nextmtu));
7246fd7af143SMichael Tuexen 	} else {
7247fd7af143SMichael Tuexen 		if ((stcb == NULL) && (inp != NULL)) {
7248fd7af143SMichael Tuexen 			/* reduce ref-count */
7249fd7af143SMichael Tuexen 			SCTP_INP_WLOCK(inp);
7250fd7af143SMichael Tuexen 			SCTP_INP_DECR_REF(inp);
7251fd7af143SMichael Tuexen 			SCTP_INP_WUNLOCK(inp);
7252fd7af143SMichael Tuexen 		}
7253fd7af143SMichael Tuexen 		if (stcb) {
7254fd7af143SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
7255fd7af143SMichael Tuexen 		}
7256fd7af143SMichael Tuexen 	}
7257fd7af143SMichael Tuexen 	return;
7258fd7af143SMichael Tuexen }
7259fd7af143SMichael Tuexen #endif
7260fd7af143SMichael Tuexen 
7261fd7af143SMichael Tuexen #ifdef INET6
7262fd7af143SMichael Tuexen static void
7263fd7af143SMichael Tuexen sctp_recv_icmp6_tunneled_packet(int cmd, struct sockaddr *sa, void *d, void *ctx SCTP_UNUSED)
7264fd7af143SMichael Tuexen {
7265fd7af143SMichael Tuexen 	struct ip6ctlparam *ip6cp;
7266fd7af143SMichael Tuexen 	struct sctp_inpcb *inp;
7267fd7af143SMichael Tuexen 	struct sctp_tcb *stcb;
7268fd7af143SMichael Tuexen 	struct sctp_nets *net;
7269fd7af143SMichael Tuexen 	struct sctphdr sh;
7270fd7af143SMichael Tuexen 	struct udphdr udp;
7271fd7af143SMichael Tuexen 	struct sockaddr_in6 src, dst;
7272fd7af143SMichael Tuexen 	uint8_t type, code;
7273fd7af143SMichael Tuexen 
7274fd7af143SMichael Tuexen 	ip6cp = (struct ip6ctlparam *)d;
7275fd7af143SMichael Tuexen 	/*
7276fd7af143SMichael Tuexen 	 * XXX: We assume that when IPV6 is non NULL, M and OFF are valid.
7277fd7af143SMichael Tuexen 	 */
7278fd7af143SMichael Tuexen 	if (ip6cp->ip6c_m == NULL) {
7279fd7af143SMichael Tuexen 		return;
7280fd7af143SMichael Tuexen 	}
7281fd7af143SMichael Tuexen 	/*
7282fd7af143SMichael Tuexen 	 * Check if we can safely examine the ports and the verification tag
7283fd7af143SMichael Tuexen 	 * of the SCTP common header.
7284fd7af143SMichael Tuexen 	 */
7285fd7af143SMichael Tuexen 	if (ip6cp->ip6c_m->m_pkthdr.len <
7286fd7af143SMichael Tuexen 	    ip6cp->ip6c_off + sizeof(struct udphdr) + offsetof(struct sctphdr, checksum)) {
7287fd7af143SMichael Tuexen 		return;
7288fd7af143SMichael Tuexen 	}
7289fd7af143SMichael Tuexen 	/* Copy out the UDP header. */
7290fd7af143SMichael Tuexen 	memset(&udp, 0, sizeof(struct udphdr));
7291fd7af143SMichael Tuexen 	m_copydata(ip6cp->ip6c_m,
7292fd7af143SMichael Tuexen 	    ip6cp->ip6c_off,
7293fd7af143SMichael Tuexen 	    sizeof(struct udphdr),
7294fd7af143SMichael Tuexen 	    (caddr_t)&udp);
7295fd7af143SMichael Tuexen 	/* Copy out the port numbers and the verification tag. */
7296fd7af143SMichael Tuexen 	memset(&sh, 0, sizeof(struct sctphdr));
7297fd7af143SMichael Tuexen 	m_copydata(ip6cp->ip6c_m,
7298fd7af143SMichael Tuexen 	    ip6cp->ip6c_off + sizeof(struct udphdr),
7299fd7af143SMichael Tuexen 	    sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
7300fd7af143SMichael Tuexen 	    (caddr_t)&sh);
7301fd7af143SMichael Tuexen 	memset(&src, 0, sizeof(struct sockaddr_in6));
7302fd7af143SMichael Tuexen 	src.sin6_family = AF_INET6;
7303fd7af143SMichael Tuexen 	src.sin6_len = sizeof(struct sockaddr_in6);
7304fd7af143SMichael Tuexen 	src.sin6_port = sh.src_port;
7305fd7af143SMichael Tuexen 	src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
7306fd7af143SMichael Tuexen 	if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
7307fd7af143SMichael Tuexen 		return;
7308fd7af143SMichael Tuexen 	}
7309fd7af143SMichael Tuexen 	memset(&dst, 0, sizeof(struct sockaddr_in6));
7310fd7af143SMichael Tuexen 	dst.sin6_family = AF_INET6;
7311fd7af143SMichael Tuexen 	dst.sin6_len = sizeof(struct sockaddr_in6);
7312fd7af143SMichael Tuexen 	dst.sin6_port = sh.dest_port;
7313fd7af143SMichael Tuexen 	dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
7314fd7af143SMichael Tuexen 	if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
7315fd7af143SMichael Tuexen 		return;
7316fd7af143SMichael Tuexen 	}
7317fd7af143SMichael Tuexen 	inp = NULL;
7318fd7af143SMichael Tuexen 	net = NULL;
7319fd7af143SMichael Tuexen 	stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
7320fd7af143SMichael Tuexen 	    (struct sockaddr *)&src,
7321fd7af143SMichael Tuexen 	    &inp, &net, 1, SCTP_DEFAULT_VRFID);
7322fd7af143SMichael Tuexen 	if ((stcb != NULL) &&
7323fd7af143SMichael Tuexen 	    (net != NULL) &&
732455b8cd93SMichael Tuexen 	    (inp != NULL)) {
7325fd7af143SMichael Tuexen 		/* Check the UDP port numbers */
7326fd7af143SMichael Tuexen 		if ((udp.uh_dport != net->port) ||
7327fd7af143SMichael Tuexen 		    (udp.uh_sport != htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)))) {
7328fd7af143SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
7329fd7af143SMichael Tuexen 			return;
7330fd7af143SMichael Tuexen 		}
7331fd7af143SMichael Tuexen 		/* Check the verification tag */
7332fd7af143SMichael Tuexen 		if (ntohl(sh.v_tag) != 0) {
7333fd7af143SMichael Tuexen 			/*
7334fd7af143SMichael Tuexen 			 * This must be the verification tag used for
7335fd7af143SMichael Tuexen 			 * sending out packets. We don't consider packets
7336fd7af143SMichael Tuexen 			 * reflecting the verification tag.
7337fd7af143SMichael Tuexen 			 */
7338fd7af143SMichael Tuexen 			if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
7339fd7af143SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
7340fd7af143SMichael Tuexen 				return;
7341fd7af143SMichael Tuexen 			}
7342fd7af143SMichael Tuexen 		} else {
7343fd7af143SMichael Tuexen 			if (ip6cp->ip6c_m->m_pkthdr.len >=
7344fd7af143SMichael Tuexen 			    ip6cp->ip6c_off + sizeof(struct udphdr) +
7345fd7af143SMichael Tuexen 			    sizeof(struct sctphdr) +
7346fd7af143SMichael Tuexen 			    sizeof(struct sctp_chunkhdr) +
7347fd7af143SMichael Tuexen 			    offsetof(struct sctp_init, a_rwnd)) {
7348fd7af143SMichael Tuexen 				/*
7349fd7af143SMichael Tuexen 				 * In this case we can check if we got an
7350fd7af143SMichael Tuexen 				 * INIT chunk and if the initiate tag
7351fd7af143SMichael Tuexen 				 * matches.
7352fd7af143SMichael Tuexen 				 */
7353fd7af143SMichael Tuexen 				uint32_t initiate_tag;
7354fd7af143SMichael Tuexen 				uint8_t chunk_type;
7355fd7af143SMichael Tuexen 
7356fd7af143SMichael Tuexen 				m_copydata(ip6cp->ip6c_m,
7357fd7af143SMichael Tuexen 				    ip6cp->ip6c_off +
7358fd7af143SMichael Tuexen 				    sizeof(struct udphdr) +
7359fd7af143SMichael Tuexen 				    sizeof(struct sctphdr),
7360fd7af143SMichael Tuexen 				    sizeof(uint8_t),
7361fd7af143SMichael Tuexen 				    (caddr_t)&chunk_type);
7362fd7af143SMichael Tuexen 				m_copydata(ip6cp->ip6c_m,
7363fd7af143SMichael Tuexen 				    ip6cp->ip6c_off +
7364fd7af143SMichael Tuexen 				    sizeof(struct udphdr) +
7365fd7af143SMichael Tuexen 				    sizeof(struct sctphdr) +
7366fd7af143SMichael Tuexen 				    sizeof(struct sctp_chunkhdr),
7367fd7af143SMichael Tuexen 				    sizeof(uint32_t),
7368fd7af143SMichael Tuexen 				    (caddr_t)&initiate_tag);
7369fd7af143SMichael Tuexen 				if ((chunk_type != SCTP_INITIATION) ||
7370fd7af143SMichael Tuexen 				    (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
7371fd7af143SMichael Tuexen 					SCTP_TCB_UNLOCK(stcb);
7372fd7af143SMichael Tuexen 					return;
7373fd7af143SMichael Tuexen 				}
7374fd7af143SMichael Tuexen 			} else {
7375fd7af143SMichael Tuexen 				SCTP_TCB_UNLOCK(stcb);
7376fd7af143SMichael Tuexen 				return;
7377fd7af143SMichael Tuexen 			}
7378fd7af143SMichael Tuexen 		}
7379fd7af143SMichael Tuexen 		type = ip6cp->ip6c_icmp6->icmp6_type;
7380fd7af143SMichael Tuexen 		code = ip6cp->ip6c_icmp6->icmp6_code;
7381fd7af143SMichael Tuexen 		if ((type == ICMP6_DST_UNREACH) &&
7382fd7af143SMichael Tuexen 		    (code == ICMP6_DST_UNREACH_NOPORT)) {
7383fd7af143SMichael Tuexen 			type = ICMP6_PARAM_PROB;
7384fd7af143SMichael Tuexen 			code = ICMP6_PARAMPROB_NEXTHEADER;
7385fd7af143SMichael Tuexen 		}
7386fd7af143SMichael Tuexen 		sctp6_notify(inp, stcb, net, type, code,
73876ebfa5eeSMichael Tuexen 		    ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
7388fd7af143SMichael Tuexen 	} else {
7389fd7af143SMichael Tuexen 		if ((stcb == NULL) && (inp != NULL)) {
7390fd7af143SMichael Tuexen 			/* reduce inp's ref-count */
7391fd7af143SMichael Tuexen 			SCTP_INP_WLOCK(inp);
7392fd7af143SMichael Tuexen 			SCTP_INP_DECR_REF(inp);
7393fd7af143SMichael Tuexen 			SCTP_INP_WUNLOCK(inp);
7394fd7af143SMichael Tuexen 		}
7395fd7af143SMichael Tuexen 		if (stcb) {
7396fd7af143SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
7397fd7af143SMichael Tuexen 		}
7398fd7af143SMichael Tuexen 	}
7399fd7af143SMichael Tuexen }
7400fd7af143SMichael Tuexen #endif
7401fd7af143SMichael Tuexen 
7402c54a18d2SRandall Stewart void
7403c54a18d2SRandall Stewart sctp_over_udp_stop(void)
7404c54a18d2SRandall Stewart {
7405a99b6783SRandall Stewart 	/*
7406a99b6783SRandall Stewart 	 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
7407a99b6783SRandall Stewart 	 * for writting!
7408a99b6783SRandall Stewart 	 */
74093a51a264SMichael Tuexen #ifdef INET
74103a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
74113a51a264SMichael Tuexen 		soclose(SCTP_BASE_INFO(udp4_tun_socket));
74123a51a264SMichael Tuexen 		SCTP_BASE_INFO(udp4_tun_socket) = NULL;
7413c54a18d2SRandall Stewart 	}
74143a51a264SMichael Tuexen #endif
74153a51a264SMichael Tuexen #ifdef INET6
74163a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
74173a51a264SMichael Tuexen 		soclose(SCTP_BASE_INFO(udp6_tun_socket));
74183a51a264SMichael Tuexen 		SCTP_BASE_INFO(udp6_tun_socket) = NULL;
74193a51a264SMichael Tuexen 	}
74203a51a264SMichael Tuexen #endif
7421a99b6783SRandall Stewart }
7422ea5eba11SMichael Tuexen 
7423c54a18d2SRandall Stewart int
7424c54a18d2SRandall Stewart sctp_over_udp_start(void)
7425c54a18d2SRandall Stewart {
7426a99b6783SRandall Stewart 	uint16_t port;
7427a99b6783SRandall Stewart 	int ret;
74283a51a264SMichael Tuexen #ifdef INET
74293a51a264SMichael Tuexen 	struct sockaddr_in sin;
74303a51a264SMichael Tuexen #endif
74313a51a264SMichael Tuexen #ifdef INET6
74323a51a264SMichael Tuexen 	struct sockaddr_in6 sin6;
74333a51a264SMichael Tuexen #endif
7434a99b6783SRandall Stewart 	/*
7435a99b6783SRandall Stewart 	 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
7436a99b6783SRandall Stewart 	 * for writting!
7437a99b6783SRandall Stewart 	 */
7438a99b6783SRandall Stewart 	port = SCTP_BASE_SYSCTL(sctp_udp_tunneling_port);
74393a51a264SMichael Tuexen 	if (ntohs(port) == 0) {
7440a99b6783SRandall Stewart 		/* Must have a port set */
7441a99b6783SRandall Stewart 		return (EINVAL);
7442a99b6783SRandall Stewart 	}
74433a51a264SMichael Tuexen #ifdef INET
74443a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
7445a99b6783SRandall Stewart 		/* Already running -- must stop first */
7446a99b6783SRandall Stewart 		return (EALREADY);
7447a99b6783SRandall Stewart 	}
74483a51a264SMichael Tuexen #endif
74493a51a264SMichael Tuexen #ifdef INET6
74503a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
74513a51a264SMichael Tuexen 		/* Already running -- must stop first */
74523a51a264SMichael Tuexen 		return (EALREADY);
7453a99b6783SRandall Stewart 	}
74543a51a264SMichael Tuexen #endif
74553a51a264SMichael Tuexen #ifdef INET
74563a51a264SMichael Tuexen 	if ((ret = socreate(PF_INET, &SCTP_BASE_INFO(udp4_tun_socket),
74573a51a264SMichael Tuexen 	    SOCK_DGRAM, IPPROTO_UDP,
74583a51a264SMichael Tuexen 	    curthread->td_ucred, curthread))) {
7459a99b6783SRandall Stewart 		sctp_over_udp_stop();
7460a99b6783SRandall Stewart 		return (ret);
7461a99b6783SRandall Stewart 	}
74623a51a264SMichael Tuexen 	/* Call the special UDP hook. */
74633a51a264SMichael Tuexen 	if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp4_tun_socket),
7464fd7af143SMichael Tuexen 	    sctp_recv_udp_tunneled_packet,
7465fd7af143SMichael Tuexen 	    sctp_recv_icmp_tunneled_packet,
7466fd7af143SMichael Tuexen 	    NULL))) {
74673a51a264SMichael Tuexen 		sctp_over_udp_stop();
74683a51a264SMichael Tuexen 		return (ret);
74693a51a264SMichael Tuexen 	}
74703a51a264SMichael Tuexen 	/* Ok, we have a socket, bind it to the port. */
74713a51a264SMichael Tuexen 	memset(&sin, 0, sizeof(struct sockaddr_in));
74723a51a264SMichael Tuexen 	sin.sin_len = sizeof(struct sockaddr_in);
74733a51a264SMichael Tuexen 	sin.sin_family = AF_INET;
74743a51a264SMichael Tuexen 	sin.sin_port = htons(port);
74753a51a264SMichael Tuexen 	if ((ret = sobind(SCTP_BASE_INFO(udp4_tun_socket),
74763a51a264SMichael Tuexen 	    (struct sockaddr *)&sin, curthread))) {
74773a51a264SMichael Tuexen 		sctp_over_udp_stop();
74783a51a264SMichael Tuexen 		return (ret);
74793a51a264SMichael Tuexen 	}
74803a51a264SMichael Tuexen #endif
74813a51a264SMichael Tuexen #ifdef INET6
74823a51a264SMichael Tuexen 	if ((ret = socreate(PF_INET6, &SCTP_BASE_INFO(udp6_tun_socket),
74833a51a264SMichael Tuexen 	    SOCK_DGRAM, IPPROTO_UDP,
74843a51a264SMichael Tuexen 	    curthread->td_ucred, curthread))) {
74853a51a264SMichael Tuexen 		sctp_over_udp_stop();
74863a51a264SMichael Tuexen 		return (ret);
74873a51a264SMichael Tuexen 	}
74883a51a264SMichael Tuexen 	/* Call the special UDP hook. */
74893a51a264SMichael Tuexen 	if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp6_tun_socket),
7490fd7af143SMichael Tuexen 	    sctp_recv_udp_tunneled_packet,
7491fd7af143SMichael Tuexen 	    sctp_recv_icmp6_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(&sin6, 0, sizeof(struct sockaddr_in6));
74983a51a264SMichael Tuexen 	sin6.sin6_len = sizeof(struct sockaddr_in6);
74993a51a264SMichael Tuexen 	sin6.sin6_family = AF_INET6;
75003a51a264SMichael Tuexen 	sin6.sin6_port = htons(port);
75013a51a264SMichael Tuexen 	if ((ret = sobind(SCTP_BASE_INFO(udp6_tun_socket),
75023a51a264SMichael Tuexen 	    (struct sockaddr *)&sin6, curthread))) {
75033a51a264SMichael Tuexen 		sctp_over_udp_stop();
75043a51a264SMichael Tuexen 		return (ret);
75053a51a264SMichael Tuexen 	}
75063a51a264SMichael Tuexen #endif
7507a99b6783SRandall Stewart 	return (0);
7508c54a18d2SRandall Stewart }
750910e0318aSMichael Tuexen 
751010e0318aSMichael Tuexen /*
751110e0318aSMichael Tuexen  * sctp_min_mtu ()returns the minimum of all non-zero arguments.
751210e0318aSMichael Tuexen  * If all arguments are zero, zero is returned.
751310e0318aSMichael Tuexen  */
751410e0318aSMichael Tuexen uint32_t
7515b0471b4bSMichael Tuexen sctp_min_mtu(uint32_t mtu1, uint32_t mtu2, uint32_t mtu3)
7516b0471b4bSMichael Tuexen {
751710e0318aSMichael Tuexen 	if (mtu1 > 0) {
751810e0318aSMichael Tuexen 		if (mtu2 > 0) {
751910e0318aSMichael Tuexen 			if (mtu3 > 0) {
752010e0318aSMichael Tuexen 				return (min(mtu1, min(mtu2, mtu3)));
752110e0318aSMichael Tuexen 			} else {
752210e0318aSMichael Tuexen 				return (min(mtu1, mtu2));
752310e0318aSMichael Tuexen 			}
752410e0318aSMichael Tuexen 		} else {
752510e0318aSMichael Tuexen 			if (mtu3 > 0) {
752610e0318aSMichael Tuexen 				return (min(mtu1, mtu3));
752710e0318aSMichael Tuexen 			} else {
752810e0318aSMichael Tuexen 				return (mtu1);
752910e0318aSMichael Tuexen 			}
753010e0318aSMichael Tuexen 		}
753110e0318aSMichael Tuexen 	} else {
753210e0318aSMichael Tuexen 		if (mtu2 > 0) {
753310e0318aSMichael Tuexen 			if (mtu3 > 0) {
753410e0318aSMichael Tuexen 				return (min(mtu2, mtu3));
753510e0318aSMichael Tuexen 			} else {
753610e0318aSMichael Tuexen 				return (mtu2);
753710e0318aSMichael Tuexen 			}
753810e0318aSMichael Tuexen 		} else {
753910e0318aSMichael Tuexen 			return (mtu3);
754010e0318aSMichael Tuexen 		}
754110e0318aSMichael Tuexen 	}
754210e0318aSMichael Tuexen }
754310e0318aSMichael Tuexen 
754410e0318aSMichael Tuexen void
754510e0318aSMichael Tuexen sctp_hc_set_mtu(union sctp_sockstore *addr, uint16_t fibnum, uint32_t mtu)
754610e0318aSMichael Tuexen {
754710e0318aSMichael Tuexen 	struct in_conninfo inc;
754810e0318aSMichael Tuexen 
754910e0318aSMichael Tuexen 	memset(&inc, 0, sizeof(struct in_conninfo));
755010e0318aSMichael Tuexen 	inc.inc_fibnum = fibnum;
755110e0318aSMichael Tuexen 	switch (addr->sa.sa_family) {
755210e0318aSMichael Tuexen #ifdef INET
755310e0318aSMichael Tuexen 	case AF_INET:
755410e0318aSMichael Tuexen 		inc.inc_faddr = addr->sin.sin_addr;
755510e0318aSMichael Tuexen 		break;
755610e0318aSMichael Tuexen #endif
755710e0318aSMichael Tuexen #ifdef INET6
755810e0318aSMichael Tuexen 	case AF_INET6:
755910e0318aSMichael Tuexen 		inc.inc_flags |= INC_ISIPV6;
756010e0318aSMichael Tuexen 		inc.inc6_faddr = addr->sin6.sin6_addr;
756110e0318aSMichael Tuexen 		break;
756210e0318aSMichael Tuexen #endif
756310e0318aSMichael Tuexen 	default:
756410e0318aSMichael Tuexen 		return;
756510e0318aSMichael Tuexen 	}
756610e0318aSMichael Tuexen 	tcp_hc_updatemtu(&inc, (u_long)mtu);
756710e0318aSMichael Tuexen }
756810e0318aSMichael Tuexen 
756910e0318aSMichael Tuexen uint32_t
7570b0471b4bSMichael Tuexen sctp_hc_get_mtu(union sctp_sockstore *addr, uint16_t fibnum)
7571b0471b4bSMichael Tuexen {
757210e0318aSMichael Tuexen 	struct in_conninfo inc;
757310e0318aSMichael Tuexen 
757410e0318aSMichael Tuexen 	memset(&inc, 0, sizeof(struct in_conninfo));
757510e0318aSMichael Tuexen 	inc.inc_fibnum = fibnum;
757610e0318aSMichael Tuexen 	switch (addr->sa.sa_family) {
757710e0318aSMichael Tuexen #ifdef INET
757810e0318aSMichael Tuexen 	case AF_INET:
757910e0318aSMichael Tuexen 		inc.inc_faddr = addr->sin.sin_addr;
758010e0318aSMichael Tuexen 		break;
758110e0318aSMichael Tuexen #endif
758210e0318aSMichael Tuexen #ifdef INET6
758310e0318aSMichael Tuexen 	case AF_INET6:
758410e0318aSMichael Tuexen 		inc.inc_flags |= INC_ISIPV6;
758510e0318aSMichael Tuexen 		inc.inc6_faddr = addr->sin6.sin6_addr;
758610e0318aSMichael Tuexen 		break;
758710e0318aSMichael Tuexen #endif
758810e0318aSMichael Tuexen 	default:
758910e0318aSMichael Tuexen 		return (0);
759010e0318aSMichael Tuexen 	}
759110e0318aSMichael Tuexen 	return ((uint32_t)tcp_hc_getmtu(&inc));
759210e0318aSMichael Tuexen }
75936ef849e6SMichael Tuexen 
75941a0b0216SMichael Tuexen void
75951a0b0216SMichael Tuexen sctp_set_state(struct sctp_tcb *stcb, int new_state)
75961a0b0216SMichael Tuexen {
75971e88cc8bSMichael Tuexen #if defined(KDTRACE_HOOKS)
75981e88cc8bSMichael Tuexen 	int old_state = stcb->asoc.state;
75991e88cc8bSMichael Tuexen #endif
76001e88cc8bSMichael Tuexen 
76011a0b0216SMichael Tuexen 	KASSERT((new_state & ~SCTP_STATE_MASK) == 0,
76021a0b0216SMichael Tuexen 	    ("sctp_set_state: Can't set substate (new_state = %x)",
76031a0b0216SMichael Tuexen 	    new_state));
76041a0b0216SMichael Tuexen 	stcb->asoc.state = (stcb->asoc.state & ~SCTP_STATE_MASK) | new_state;
76051a0b0216SMichael Tuexen 	if ((new_state == SCTP_STATE_SHUTDOWN_RECEIVED) ||
76061a0b0216SMichael Tuexen 	    (new_state == SCTP_STATE_SHUTDOWN_SENT) ||
76071a0b0216SMichael Tuexen 	    (new_state == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
76081a0b0216SMichael Tuexen 		SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
76091a0b0216SMichael Tuexen 	}
76101e88cc8bSMichael Tuexen #if defined(KDTRACE_HOOKS)
76111e88cc8bSMichael Tuexen 	if (((old_state & SCTP_STATE_MASK) != new_state) &&
76121e88cc8bSMichael Tuexen 	    !(((old_state & SCTP_STATE_MASK) == SCTP_STATE_EMPTY) &&
76131e88cc8bSMichael Tuexen 	    (new_state == SCTP_STATE_INUSE))) {
76141e88cc8bSMichael Tuexen 		SCTP_PROBE6(state__change, NULL, stcb, NULL, stcb, NULL, old_state);
76151e88cc8bSMichael Tuexen 	}
76161e88cc8bSMichael Tuexen #endif
76171a0b0216SMichael Tuexen }
76181a0b0216SMichael Tuexen 
76191a0b0216SMichael Tuexen void
76201a0b0216SMichael Tuexen sctp_add_substate(struct sctp_tcb *stcb, int substate)
76211a0b0216SMichael Tuexen {
76221e88cc8bSMichael Tuexen #if defined(KDTRACE_HOOKS)
76231e88cc8bSMichael Tuexen 	int old_state = stcb->asoc.state;
76241e88cc8bSMichael Tuexen #endif
76251e88cc8bSMichael Tuexen 
76261a0b0216SMichael Tuexen 	KASSERT((substate & SCTP_STATE_MASK) == 0,
76271a0b0216SMichael Tuexen 	    ("sctp_add_substate: Can't set state (substate = %x)",
76281a0b0216SMichael Tuexen 	    substate));
76291a0b0216SMichael Tuexen 	stcb->asoc.state |= substate;
76301e88cc8bSMichael Tuexen #if defined(KDTRACE_HOOKS)
76311e88cc8bSMichael Tuexen 	if (((substate & SCTP_STATE_ABOUT_TO_BE_FREED) &&
76321e88cc8bSMichael Tuexen 	    ((old_state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) ||
76331e88cc8bSMichael Tuexen 	    ((substate & SCTP_STATE_SHUTDOWN_PENDING) &&
76341e88cc8bSMichael Tuexen 	    ((old_state & SCTP_STATE_SHUTDOWN_PENDING) == 0))) {
76351e88cc8bSMichael Tuexen 		SCTP_PROBE6(state__change, NULL, stcb, NULL, stcb, NULL, old_state);
76361e88cc8bSMichael Tuexen 	}
76371e88cc8bSMichael Tuexen #endif
76381a0b0216SMichael Tuexen }
7639