xref: /freebsd/sys/netinet/sctputil.c (revision 4be807c4d6854aa0158ffdadd276a7c7a8967188)
1f8829a4aSRandall Stewart /*-
2830d754dSRandall Stewart  * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3807aad63SMichael Tuexen  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4807aad63SMichael Tuexen  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
5f8829a4aSRandall Stewart  *
6f8829a4aSRandall Stewart  * Redistribution and use in source and binary forms, with or without
7f8829a4aSRandall Stewart  * modification, are permitted provided that the following conditions are met:
8f8829a4aSRandall Stewart  *
9f8829a4aSRandall Stewart  * a) Redistributions of source code must retain the above copyright notice,
10f8829a4aSRandall Stewart  *    this list of conditions and the following disclaimer.
11f8829a4aSRandall Stewart  *
12f8829a4aSRandall Stewart  * b) Redistributions in binary form must reproduce the above copyright
13f8829a4aSRandall Stewart  *    notice, this list of conditions and the following disclaimer in
14f8829a4aSRandall Stewart  *    the documentation and/or other materials provided with the distribution.
15f8829a4aSRandall Stewart  *
16f8829a4aSRandall Stewart  * c) Neither the name of Cisco Systems, Inc. nor the names of its
17f8829a4aSRandall Stewart  *    contributors may be used to endorse or promote products derived
18f8829a4aSRandall Stewart  *    from this software without specific prior written permission.
19f8829a4aSRandall Stewart  *
20f8829a4aSRandall Stewart  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21f8829a4aSRandall Stewart  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22f8829a4aSRandall Stewart  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23f8829a4aSRandall Stewart  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24f8829a4aSRandall Stewart  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25f8829a4aSRandall Stewart  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26f8829a4aSRandall Stewart  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27f8829a4aSRandall Stewart  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28f8829a4aSRandall Stewart  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29f8829a4aSRandall Stewart  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30f8829a4aSRandall Stewart  * THE POSSIBILITY OF SUCH DAMAGE.
31f8829a4aSRandall Stewart  */
32f8829a4aSRandall Stewart 
33f8829a4aSRandall Stewart #include <sys/cdefs.h>
34f8829a4aSRandall Stewart __FBSDID("$FreeBSD$");
35f8829a4aSRandall Stewart 
36f8829a4aSRandall Stewart #include <netinet/sctp_os.h>
37f8829a4aSRandall Stewart #include <netinet/sctp_pcb.h>
38f8829a4aSRandall Stewart #include <netinet/sctputil.h>
39f8829a4aSRandall Stewart #include <netinet/sctp_var.h>
4042551e99SRandall Stewart #include <netinet/sctp_sysctl.h>
41f8829a4aSRandall Stewart #ifdef INET6
423a51a264SMichael Tuexen #include <netinet6/sctp6_var.h>
43f8829a4aSRandall Stewart #endif
44f8829a4aSRandall Stewart #include <netinet/sctp_header.h>
45f8829a4aSRandall Stewart #include <netinet/sctp_output.h>
46f8829a4aSRandall Stewart #include <netinet/sctp_uio.h>
47f8829a4aSRandall Stewart #include <netinet/sctp_timer.h>
48f8829a4aSRandall Stewart #include <netinet/sctp_indata.h>/* for sctp_deliver_data() */
49f8829a4aSRandall Stewart #include <netinet/sctp_auth.h>
50f8829a4aSRandall Stewart #include <netinet/sctp_asconf.h>
51f7517433SRandall Stewart #include <netinet/sctp_bsd_addr.h>
523a51a264SMichael Tuexen #include <netinet/udp.h>
533a51a264SMichael Tuexen #include <netinet/udp_var.h>
543a51a264SMichael Tuexen #include <sys/proc.h>
55f8829a4aSRandall Stewart 
56f8829a4aSRandall Stewart 
57b9e7085aSRandall Stewart #ifndef KTR_SCTP
58b9e7085aSRandall Stewart #define KTR_SCTP KTR_SUBSYS
5980fefe0aSRandall Stewart #endif
60f8829a4aSRandall Stewart 
610e9a9c10SMichael Tuexen extern struct sctp_cc_functions sctp_cc_functions[];
62f7a77f6fSMichael Tuexen extern struct sctp_ss_functions sctp_ss_functions[];
630e9a9c10SMichael Tuexen 
64f8829a4aSRandall Stewart void
65dcb68fbaSMichael Tuexen sctp_sblog(struct sockbuf *sb, struct sctp_tcb *stcb, int from, int incr)
66f8829a4aSRandall Stewart {
6780fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
68f8829a4aSRandall Stewart 
6980fefe0aSRandall Stewart 	sctp_clog.x.sb.stcb = stcb;
704e88d37aSMichael Tuexen 	sctp_clog.x.sb.so_sbcc = sb->sb_cc;
71f8829a4aSRandall Stewart 	if (stcb)
724e88d37aSMichael Tuexen 		sctp_clog.x.sb.stcb_sbcc = stcb->asoc.sb_cc;
73f8829a4aSRandall Stewart 	else
7480fefe0aSRandall Stewart 		sctp_clog.x.sb.stcb_sbcc = 0;
7580fefe0aSRandall Stewart 	sctp_clog.x.sb.incr = incr;
76c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
7780fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_SB,
7880fefe0aSRandall Stewart 	    from,
7980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
8080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
8180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
8280fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
83f8829a4aSRandall Stewart }
84f8829a4aSRandall Stewart 
85f8829a4aSRandall Stewart void
86f8829a4aSRandall Stewart sctp_log_closing(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int16_t loc)
87f8829a4aSRandall Stewart {
8880fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
89f8829a4aSRandall Stewart 
9080fefe0aSRandall Stewart 	sctp_clog.x.close.inp = (void *)inp;
9180fefe0aSRandall Stewart 	sctp_clog.x.close.sctp_flags = inp->sctp_flags;
92f8829a4aSRandall Stewart 	if (stcb) {
9380fefe0aSRandall Stewart 		sctp_clog.x.close.stcb = (void *)stcb;
9480fefe0aSRandall Stewart 		sctp_clog.x.close.state = (uint16_t) stcb->asoc.state;
95f8829a4aSRandall Stewart 	} else {
9680fefe0aSRandall Stewart 		sctp_clog.x.close.stcb = 0;
9780fefe0aSRandall Stewart 		sctp_clog.x.close.state = 0;
98f8829a4aSRandall Stewart 	}
9980fefe0aSRandall Stewart 	sctp_clog.x.close.loc = loc;
100c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
10180fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_CLOSE,
10280fefe0aSRandall Stewart 	    0,
10380fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
10480fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
10580fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
10680fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
107f8829a4aSRandall Stewart }
108f8829a4aSRandall Stewart 
109f8829a4aSRandall Stewart void
110f8829a4aSRandall Stewart rto_logging(struct sctp_nets *net, int from)
111f8829a4aSRandall Stewart {
11280fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
113f8829a4aSRandall Stewart 
114bfefd190SRandall Stewart 	memset(&sctp_clog, 0, sizeof(sctp_clog));
11580fefe0aSRandall Stewart 	sctp_clog.x.rto.net = (void *)net;
116be1d9176SMichael Tuexen 	sctp_clog.x.rto.rtt = net->rtt / 1000;
117c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
11880fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_RTT,
11980fefe0aSRandall Stewart 	    from,
12080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
12180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
12280fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
12380fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
124f8829a4aSRandall Stewart }
125f8829a4aSRandall Stewart 
126f8829a4aSRandall Stewart void
1276a91f103SRandall Stewart sctp_log_strm_del_alt(struct sctp_tcb *stcb, uint32_t tsn, uint16_t sseq, uint16_t stream, int from)
128f8829a4aSRandall Stewart {
12980fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
130f8829a4aSRandall Stewart 
13180fefe0aSRandall Stewart 	sctp_clog.x.strlog.stcb = stcb;
13280fefe0aSRandall Stewart 	sctp_clog.x.strlog.n_tsn = tsn;
13380fefe0aSRandall Stewart 	sctp_clog.x.strlog.n_sseq = sseq;
13480fefe0aSRandall Stewart 	sctp_clog.x.strlog.e_tsn = 0;
13580fefe0aSRandall Stewart 	sctp_clog.x.strlog.e_sseq = 0;
13680fefe0aSRandall Stewart 	sctp_clog.x.strlog.strm = stream;
137c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
13880fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_STRM,
13980fefe0aSRandall Stewart 	    from,
14080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
14180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
14280fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
14380fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
144f8829a4aSRandall Stewart }
145f8829a4aSRandall Stewart 
146f8829a4aSRandall Stewart void
147f8829a4aSRandall Stewart sctp_log_nagle_event(struct sctp_tcb *stcb, int action)
148f8829a4aSRandall Stewart {
14980fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
150f8829a4aSRandall Stewart 
15180fefe0aSRandall Stewart 	sctp_clog.x.nagle.stcb = (void *)stcb;
15280fefe0aSRandall Stewart 	sctp_clog.x.nagle.total_flight = stcb->asoc.total_flight;
15380fefe0aSRandall Stewart 	sctp_clog.x.nagle.total_in_queue = stcb->asoc.total_output_queue_size;
15480fefe0aSRandall Stewart 	sctp_clog.x.nagle.count_in_queue = stcb->asoc.chunks_on_out_queue;
15580fefe0aSRandall Stewart 	sctp_clog.x.nagle.count_in_flight = stcb->asoc.total_flight_count;
156c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
15780fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_NAGLE,
15880fefe0aSRandall Stewart 	    action,
15980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
16080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
16180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
16280fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
163f8829a4aSRandall Stewart }
164f8829a4aSRandall Stewart 
165f8829a4aSRandall Stewart void
166f8829a4aSRandall Stewart sctp_log_sack(uint32_t old_cumack, uint32_t cumack, uint32_t tsn, uint16_t gaps, uint16_t dups, int from)
167f8829a4aSRandall Stewart {
16880fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
169f8829a4aSRandall Stewart 
17080fefe0aSRandall Stewart 	sctp_clog.x.sack.cumack = cumack;
17180fefe0aSRandall Stewart 	sctp_clog.x.sack.oldcumack = old_cumack;
17280fefe0aSRandall Stewart 	sctp_clog.x.sack.tsn = tsn;
17380fefe0aSRandall Stewart 	sctp_clog.x.sack.numGaps = gaps;
17480fefe0aSRandall Stewart 	sctp_clog.x.sack.numDups = dups;
175c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
17680fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_SACK,
17780fefe0aSRandall Stewart 	    from,
17880fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
17980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
18080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
18180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
182f8829a4aSRandall Stewart }
183f8829a4aSRandall Stewart 
184f8829a4aSRandall Stewart void
185f8829a4aSRandall Stewart sctp_log_map(uint32_t map, uint32_t cum, uint32_t high, int from)
186f8829a4aSRandall Stewart {
18780fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
188f8829a4aSRandall Stewart 
189bfefd190SRandall Stewart 	memset(&sctp_clog, 0, sizeof(sctp_clog));
19080fefe0aSRandall Stewart 	sctp_clog.x.map.base = map;
19180fefe0aSRandall Stewart 	sctp_clog.x.map.cum = cum;
19280fefe0aSRandall Stewart 	sctp_clog.x.map.high = high;
193c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
19480fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_MAP,
19580fefe0aSRandall Stewart 	    from,
19680fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
19780fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
19880fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
19980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
200f8829a4aSRandall Stewart }
201f8829a4aSRandall Stewart 
202f8829a4aSRandall Stewart void
203dcb68fbaSMichael Tuexen sctp_log_fr(uint32_t biggest_tsn, uint32_t biggest_new_tsn, uint32_t tsn, int from)
204f8829a4aSRandall Stewart {
20580fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
206f8829a4aSRandall Stewart 
207bfefd190SRandall Stewart 	memset(&sctp_clog, 0, sizeof(sctp_clog));
20880fefe0aSRandall Stewart 	sctp_clog.x.fr.largest_tsn = biggest_tsn;
20980fefe0aSRandall Stewart 	sctp_clog.x.fr.largest_new_tsn = biggest_new_tsn;
21080fefe0aSRandall Stewart 	sctp_clog.x.fr.tsn = tsn;
211c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
21280fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_FR,
21380fefe0aSRandall Stewart 	    from,
21480fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
21580fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
21680fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
21780fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
218f8829a4aSRandall Stewart }
219f8829a4aSRandall Stewart 
220*4be807c4SMichael Tuexen #ifdef SCTP_MBUF_LOGGING
221f8829a4aSRandall Stewart void
222f8829a4aSRandall Stewart sctp_log_mb(struct mbuf *m, int from)
223f8829a4aSRandall Stewart {
22480fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
225f8829a4aSRandall Stewart 
22680fefe0aSRandall Stewart 	sctp_clog.x.mb.mp = m;
22780fefe0aSRandall Stewart 	sctp_clog.x.mb.mbuf_flags = (uint8_t) (SCTP_BUF_GET_FLAGS(m));
22880fefe0aSRandall Stewart 	sctp_clog.x.mb.size = (uint16_t) (SCTP_BUF_LEN(m));
22980fefe0aSRandall Stewart 	sctp_clog.x.mb.data = SCTP_BUF_AT(m, 0);
230139bc87fSRandall Stewart 	if (SCTP_BUF_IS_EXTENDED(m)) {
23180fefe0aSRandall Stewart 		sctp_clog.x.mb.ext = SCTP_BUF_EXTEND_BASE(m);
23280fefe0aSRandall Stewart 		sctp_clog.x.mb.refcnt = (uint8_t) (SCTP_BUF_EXTEND_REFCNT(m));
233f8829a4aSRandall Stewart 	} else {
23480fefe0aSRandall Stewart 		sctp_clog.x.mb.ext = 0;
23580fefe0aSRandall Stewart 		sctp_clog.x.mb.refcnt = 0;
236f8829a4aSRandall Stewart 	}
237c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
23880fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_MBUF,
23980fefe0aSRandall Stewart 	    from,
24080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
24180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
24280fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
24380fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
244f8829a4aSRandall Stewart }
245f8829a4aSRandall Stewart 
246f8829a4aSRandall Stewart void
247*4be807c4SMichael Tuexen sctp_log_mbc(struct mbuf *m, int from)
248*4be807c4SMichael Tuexen {
249*4be807c4SMichael Tuexen 	struct mbuf *mat;
250*4be807c4SMichael Tuexen 
251*4be807c4SMichael Tuexen 	for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
252*4be807c4SMichael Tuexen 		sctp_log_mb(mat, from);
253*4be807c4SMichael Tuexen 	}
254*4be807c4SMichael Tuexen }
255*4be807c4SMichael Tuexen 
256*4be807c4SMichael Tuexen #endif
257*4be807c4SMichael Tuexen 
258*4be807c4SMichael Tuexen void
259dcb68fbaSMichael Tuexen sctp_log_strm_del(struct sctp_queued_to_read *control, struct sctp_queued_to_read *poschk, int from)
260f8829a4aSRandall Stewart {
26180fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
262f8829a4aSRandall Stewart 
263f8829a4aSRandall Stewart 	if (control == NULL) {
264ad81507eSRandall Stewart 		SCTP_PRINTF("Gak log of NULL?\n");
265f8829a4aSRandall Stewart 		return;
266f8829a4aSRandall Stewart 	}
26780fefe0aSRandall Stewart 	sctp_clog.x.strlog.stcb = control->stcb;
26880fefe0aSRandall Stewart 	sctp_clog.x.strlog.n_tsn = control->sinfo_tsn;
26980fefe0aSRandall Stewart 	sctp_clog.x.strlog.n_sseq = control->sinfo_ssn;
27080fefe0aSRandall Stewart 	sctp_clog.x.strlog.strm = control->sinfo_stream;
271f8829a4aSRandall Stewart 	if (poschk != NULL) {
27280fefe0aSRandall Stewart 		sctp_clog.x.strlog.e_tsn = poschk->sinfo_tsn;
27380fefe0aSRandall Stewart 		sctp_clog.x.strlog.e_sseq = poschk->sinfo_ssn;
274f8829a4aSRandall Stewart 	} else {
27580fefe0aSRandall Stewart 		sctp_clog.x.strlog.e_tsn = 0;
27680fefe0aSRandall Stewart 		sctp_clog.x.strlog.e_sseq = 0;
277f8829a4aSRandall Stewart 	}
278c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
27980fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_STRM,
28080fefe0aSRandall Stewart 	    from,
28180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
28280fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
28380fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
28480fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
285f8829a4aSRandall Stewart }
286f8829a4aSRandall Stewart 
287f8829a4aSRandall Stewart void
288f8829a4aSRandall Stewart sctp_log_cwnd(struct sctp_tcb *stcb, struct sctp_nets *net, int augment, uint8_t from)
289f8829a4aSRandall Stewart {
29080fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
291f8829a4aSRandall Stewart 
29280fefe0aSRandall Stewart 	sctp_clog.x.cwnd.net = net;
293f8829a4aSRandall Stewart 	if (stcb->asoc.send_queue_cnt > 255)
29480fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_send = 255;
295f8829a4aSRandall Stewart 	else
29680fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
297f8829a4aSRandall Stewart 	if (stcb->asoc.stream_queue_cnt > 255)
29880fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_str = 255;
299f8829a4aSRandall Stewart 	else
30080fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
301f8829a4aSRandall Stewart 
302f8829a4aSRandall Stewart 	if (net) {
30380fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cwnd_new_value = net->cwnd;
30480fefe0aSRandall Stewart 		sctp_clog.x.cwnd.inflight = net->flight_size;
30580fefe0aSRandall Stewart 		sctp_clog.x.cwnd.pseudo_cumack = net->pseudo_cumack;
30680fefe0aSRandall Stewart 		sctp_clog.x.cwnd.meets_pseudo_cumack = net->new_pseudo_cumack;
30780fefe0aSRandall Stewart 		sctp_clog.x.cwnd.need_new_pseudo_cumack = net->find_pseudo_cumack;
308f8829a4aSRandall Stewart 	}
309f8829a4aSRandall Stewart 	if (SCTP_CWNDLOG_PRESEND == from) {
31080fefe0aSRandall Stewart 		sctp_clog.x.cwnd.meets_pseudo_cumack = stcb->asoc.peers_rwnd;
311f8829a4aSRandall Stewart 	}
31280fefe0aSRandall Stewart 	sctp_clog.x.cwnd.cwnd_augment = augment;
313c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
31480fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_CWND,
31580fefe0aSRandall Stewart 	    from,
31680fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
31780fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
31880fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
31980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
320f8829a4aSRandall Stewart }
321f8829a4aSRandall Stewart 
322f8829a4aSRandall Stewart void
323f8829a4aSRandall Stewart sctp_log_lock(struct sctp_inpcb *inp, struct sctp_tcb *stcb, uint8_t from)
324f8829a4aSRandall Stewart {
32580fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
326f8829a4aSRandall Stewart 
327bfefd190SRandall Stewart 	memset(&sctp_clog, 0, sizeof(sctp_clog));
32803b0b021SRandall Stewart 	if (inp) {
32980fefe0aSRandall Stewart 		sctp_clog.x.lock.sock = (void *)inp->sctp_socket;
33003b0b021SRandall Stewart 
33103b0b021SRandall Stewart 	} else {
33280fefe0aSRandall Stewart 		sctp_clog.x.lock.sock = (void *)NULL;
33303b0b021SRandall Stewart 	}
33480fefe0aSRandall Stewart 	sctp_clog.x.lock.inp = (void *)inp;
335f8829a4aSRandall Stewart 	if (stcb) {
33680fefe0aSRandall Stewart 		sctp_clog.x.lock.tcb_lock = mtx_owned(&stcb->tcb_mtx);
337f8829a4aSRandall Stewart 	} else {
33880fefe0aSRandall Stewart 		sctp_clog.x.lock.tcb_lock = SCTP_LOCK_UNKNOWN;
339f8829a4aSRandall Stewart 	}
340f8829a4aSRandall Stewart 	if (inp) {
34180fefe0aSRandall Stewart 		sctp_clog.x.lock.inp_lock = mtx_owned(&inp->inp_mtx);
34280fefe0aSRandall Stewart 		sctp_clog.x.lock.create_lock = mtx_owned(&inp->inp_create_mtx);
343f8829a4aSRandall Stewart 	} else {
34480fefe0aSRandall Stewart 		sctp_clog.x.lock.inp_lock = SCTP_LOCK_UNKNOWN;
34580fefe0aSRandall Stewart 		sctp_clog.x.lock.create_lock = SCTP_LOCK_UNKNOWN;
346f8829a4aSRandall Stewart 	}
347b3f1ea41SRandall Stewart 	sctp_clog.x.lock.info_lock = rw_wowned(&SCTP_BASE_INFO(ipi_ep_mtx));
34852129fcdSRandall Stewart 	if (inp && (inp->sctp_socket)) {
34980fefe0aSRandall Stewart 		sctp_clog.x.lock.sock_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
35080fefe0aSRandall Stewart 		sctp_clog.x.lock.sockrcvbuf_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
35180fefe0aSRandall Stewart 		sctp_clog.x.lock.socksndbuf_lock = mtx_owned(&(inp->sctp_socket->so_snd.sb_mtx));
352f8829a4aSRandall Stewart 	} else {
35380fefe0aSRandall Stewart 		sctp_clog.x.lock.sock_lock = SCTP_LOCK_UNKNOWN;
35480fefe0aSRandall Stewart 		sctp_clog.x.lock.sockrcvbuf_lock = SCTP_LOCK_UNKNOWN;
35580fefe0aSRandall Stewart 		sctp_clog.x.lock.socksndbuf_lock = SCTP_LOCK_UNKNOWN;
356f8829a4aSRandall Stewart 	}
357c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
35880fefe0aSRandall Stewart 	    SCTP_LOG_LOCK_EVENT,
35980fefe0aSRandall Stewart 	    from,
36080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
36180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
36280fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
36380fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
364f8829a4aSRandall Stewart }
365f8829a4aSRandall Stewart 
366f8829a4aSRandall Stewart void
367f8829a4aSRandall Stewart sctp_log_maxburst(struct sctp_tcb *stcb, struct sctp_nets *net, int error, int burst, uint8_t from)
368f8829a4aSRandall Stewart {
36980fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
370f8829a4aSRandall Stewart 
371bfefd190SRandall Stewart 	memset(&sctp_clog, 0, sizeof(sctp_clog));
37280fefe0aSRandall Stewart 	sctp_clog.x.cwnd.net = net;
37380fefe0aSRandall Stewart 	sctp_clog.x.cwnd.cwnd_new_value = error;
37480fefe0aSRandall Stewart 	sctp_clog.x.cwnd.inflight = net->flight_size;
37580fefe0aSRandall Stewart 	sctp_clog.x.cwnd.cwnd_augment = burst;
376f8829a4aSRandall Stewart 	if (stcb->asoc.send_queue_cnt > 255)
37780fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_send = 255;
378f8829a4aSRandall Stewart 	else
37980fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
380f8829a4aSRandall Stewart 	if (stcb->asoc.stream_queue_cnt > 255)
38180fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_str = 255;
382f8829a4aSRandall Stewart 	else
38380fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
384c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
38580fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_MAXBURST,
38680fefe0aSRandall Stewart 	    from,
38780fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
38880fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
38980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
39080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
391f8829a4aSRandall Stewart }
392f8829a4aSRandall Stewart 
393f8829a4aSRandall Stewart void
394f8829a4aSRandall Stewart sctp_log_rwnd(uint8_t from, uint32_t peers_rwnd, uint32_t snd_size, uint32_t overhead)
395f8829a4aSRandall Stewart {
39680fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
397f8829a4aSRandall Stewart 
39880fefe0aSRandall Stewart 	sctp_clog.x.rwnd.rwnd = peers_rwnd;
39980fefe0aSRandall Stewart 	sctp_clog.x.rwnd.send_size = snd_size;
40080fefe0aSRandall Stewart 	sctp_clog.x.rwnd.overhead = overhead;
40180fefe0aSRandall Stewart 	sctp_clog.x.rwnd.new_rwnd = 0;
402c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
40380fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_RWND,
40480fefe0aSRandall Stewart 	    from,
40580fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
40680fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
40780fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
40880fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
409f8829a4aSRandall Stewart }
410f8829a4aSRandall Stewart 
411f8829a4aSRandall Stewart void
412f8829a4aSRandall Stewart sctp_log_rwnd_set(uint8_t from, uint32_t peers_rwnd, uint32_t flight_size, uint32_t overhead, uint32_t a_rwndval)
413f8829a4aSRandall Stewart {
41480fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
415f8829a4aSRandall Stewart 
41680fefe0aSRandall Stewart 	sctp_clog.x.rwnd.rwnd = peers_rwnd;
41780fefe0aSRandall Stewart 	sctp_clog.x.rwnd.send_size = flight_size;
41880fefe0aSRandall Stewart 	sctp_clog.x.rwnd.overhead = overhead;
41980fefe0aSRandall Stewart 	sctp_clog.x.rwnd.new_rwnd = a_rwndval;
420c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
42180fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_RWND,
42280fefe0aSRandall Stewart 	    from,
42380fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
42480fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
42580fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
42680fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
427f8829a4aSRandall Stewart }
428f8829a4aSRandall Stewart 
429*4be807c4SMichael Tuexen #ifdef SCTP_MBCNT_LOGGING
430*4be807c4SMichael Tuexen static void
431f8829a4aSRandall Stewart sctp_log_mbcnt(uint8_t from, uint32_t total_oq, uint32_t book, uint32_t total_mbcnt_q, uint32_t mbcnt)
432f8829a4aSRandall Stewart {
43380fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
434f8829a4aSRandall Stewart 
43580fefe0aSRandall Stewart 	sctp_clog.x.mbcnt.total_queue_size = total_oq;
43680fefe0aSRandall Stewart 	sctp_clog.x.mbcnt.size_change = book;
43780fefe0aSRandall Stewart 	sctp_clog.x.mbcnt.total_queue_mb_size = total_mbcnt_q;
43880fefe0aSRandall Stewart 	sctp_clog.x.mbcnt.mbcnt_change = mbcnt;
439c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
44080fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_MBCNT,
44180fefe0aSRandall Stewart 	    from,
44280fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
44380fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
44480fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
44580fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
446f8829a4aSRandall Stewart }
447f8829a4aSRandall Stewart 
448*4be807c4SMichael Tuexen #endif
449*4be807c4SMichael Tuexen 
450f8829a4aSRandall Stewart void
451f8829a4aSRandall Stewart sctp_misc_ints(uint8_t from, uint32_t a, uint32_t b, uint32_t c, uint32_t d)
452f8829a4aSRandall Stewart {
453c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
45480fefe0aSRandall Stewart 	    SCTP_LOG_MISC_EVENT,
45580fefe0aSRandall Stewart 	    from,
45680fefe0aSRandall Stewart 	    a, b, c, d);
457f8829a4aSRandall Stewart }
458f8829a4aSRandall Stewart 
459f8829a4aSRandall Stewart void
4607215cc1bSMichael Tuexen sctp_wakeup_log(struct sctp_tcb *stcb, uint32_t wake_cnt, int from)
461f8829a4aSRandall Stewart {
46280fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
463f8829a4aSRandall Stewart 
46480fefe0aSRandall Stewart 	sctp_clog.x.wake.stcb = (void *)stcb;
46580fefe0aSRandall Stewart 	sctp_clog.x.wake.wake_cnt = wake_cnt;
46680fefe0aSRandall Stewart 	sctp_clog.x.wake.flight = stcb->asoc.total_flight_count;
46780fefe0aSRandall Stewart 	sctp_clog.x.wake.send_q = stcb->asoc.send_queue_cnt;
46880fefe0aSRandall Stewart 	sctp_clog.x.wake.sent_q = stcb->asoc.sent_queue_cnt;
469f8829a4aSRandall Stewart 
470f8829a4aSRandall Stewart 	if (stcb->asoc.stream_queue_cnt < 0xff)
47180fefe0aSRandall Stewart 		sctp_clog.x.wake.stream_qcnt = (uint8_t) stcb->asoc.stream_queue_cnt;
472f8829a4aSRandall Stewart 	else
47380fefe0aSRandall Stewart 		sctp_clog.x.wake.stream_qcnt = 0xff;
474f8829a4aSRandall Stewart 
475f8829a4aSRandall Stewart 	if (stcb->asoc.chunks_on_out_queue < 0xff)
47680fefe0aSRandall Stewart 		sctp_clog.x.wake.chunks_on_oque = (uint8_t) stcb->asoc.chunks_on_out_queue;
477f8829a4aSRandall Stewart 	else
47880fefe0aSRandall Stewart 		sctp_clog.x.wake.chunks_on_oque = 0xff;
479f8829a4aSRandall Stewart 
48080fefe0aSRandall Stewart 	sctp_clog.x.wake.sctpflags = 0;
481f8829a4aSRandall Stewart 	/* set in the defered mode stuff */
482f8829a4aSRandall Stewart 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE)
48380fefe0aSRandall Stewart 		sctp_clog.x.wake.sctpflags |= 1;
484f8829a4aSRandall Stewart 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT)
48580fefe0aSRandall Stewart 		sctp_clog.x.wake.sctpflags |= 2;
486f8829a4aSRandall Stewart 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT)
48780fefe0aSRandall Stewart 		sctp_clog.x.wake.sctpflags |= 4;
488f8829a4aSRandall Stewart 	/* what about the sb */
489f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
490f8829a4aSRandall Stewart 		struct socket *so = stcb->sctp_socket;
491f8829a4aSRandall Stewart 
49280fefe0aSRandall Stewart 		sctp_clog.x.wake.sbflags = (uint8_t) ((so->so_snd.sb_flags & 0x00ff));
493f8829a4aSRandall Stewart 	} else {
49480fefe0aSRandall Stewart 		sctp_clog.x.wake.sbflags = 0xff;
495f8829a4aSRandall Stewart 	}
496c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
49780fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_WAKE,
49880fefe0aSRandall Stewart 	    from,
49980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
50080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
50180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
50280fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
503f8829a4aSRandall Stewart }
504f8829a4aSRandall Stewart 
505f8829a4aSRandall Stewart void
5067215cc1bSMichael Tuexen sctp_log_block(uint8_t from, struct sctp_association *asoc, int sendlen)
507f8829a4aSRandall Stewart {
50880fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
509f8829a4aSRandall Stewart 
51080fefe0aSRandall Stewart 	sctp_clog.x.blk.onsb = asoc->total_output_queue_size;
51180fefe0aSRandall Stewart 	sctp_clog.x.blk.send_sent_qcnt = (uint16_t) (asoc->send_queue_cnt + asoc->sent_queue_cnt);
51280fefe0aSRandall Stewart 	sctp_clog.x.blk.peer_rwnd = asoc->peers_rwnd;
51380fefe0aSRandall Stewart 	sctp_clog.x.blk.stream_qcnt = (uint16_t) asoc->stream_queue_cnt;
51480fefe0aSRandall Stewart 	sctp_clog.x.blk.chunks_on_oque = (uint16_t) asoc->chunks_on_out_queue;
51580fefe0aSRandall Stewart 	sctp_clog.x.blk.flight_size = (uint16_t) (asoc->total_flight / 1024);
51680fefe0aSRandall Stewart 	sctp_clog.x.blk.sndlen = sendlen;
517c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
51880fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_BLOCK,
51980fefe0aSRandall Stewart 	    from,
52080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
52180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
52280fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
52380fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
524f8829a4aSRandall Stewart }
525f8829a4aSRandall Stewart 
526f8829a4aSRandall Stewart int
5277215cc1bSMichael Tuexen sctp_fill_stat_log(void *optval SCTP_UNUSED, size_t *optsize SCTP_UNUSED)
528f8829a4aSRandall Stewart {
52980fefe0aSRandall Stewart 	/* May need to fix this if ktrdump does not work */
530f8829a4aSRandall Stewart 	return (0);
531f8829a4aSRandall Stewart }
532f8829a4aSRandall Stewart 
533f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
534f8829a4aSRandall Stewart uint8_t sctp_audit_data[SCTP_AUDIT_SIZE][2];
535f8829a4aSRandall Stewart static int sctp_audit_indx = 0;
536f8829a4aSRandall Stewart 
537f8829a4aSRandall Stewart static
538f8829a4aSRandall Stewart void
539f8829a4aSRandall Stewart sctp_print_audit_report(void)
540f8829a4aSRandall Stewart {
541f8829a4aSRandall Stewart 	int i;
542f8829a4aSRandall Stewart 	int cnt;
543f8829a4aSRandall Stewart 
544f8829a4aSRandall Stewart 	cnt = 0;
545f8829a4aSRandall Stewart 	for (i = sctp_audit_indx; i < SCTP_AUDIT_SIZE; i++) {
546f8829a4aSRandall Stewart 		if ((sctp_audit_data[i][0] == 0xe0) &&
547f8829a4aSRandall Stewart 		    (sctp_audit_data[i][1] == 0x01)) {
548f8829a4aSRandall Stewart 			cnt = 0;
549ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
550f8829a4aSRandall Stewart 		} else if (sctp_audit_data[i][0] == 0xf0) {
551f8829a4aSRandall Stewart 			cnt = 0;
552ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
553f8829a4aSRandall Stewart 		} else if ((sctp_audit_data[i][0] == 0xc0) &&
554f8829a4aSRandall Stewart 		    (sctp_audit_data[i][1] == 0x01)) {
555ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
556f8829a4aSRandall Stewart 			cnt = 0;
557f8829a4aSRandall Stewart 		}
558ad81507eSRandall Stewart 		SCTP_PRINTF("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0],
559f8829a4aSRandall Stewart 		    (uint32_t) sctp_audit_data[i][1]);
560f8829a4aSRandall Stewart 		cnt++;
561f8829a4aSRandall Stewart 		if ((cnt % 14) == 0)
562ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
563f8829a4aSRandall Stewart 	}
564f8829a4aSRandall Stewart 	for (i = 0; i < sctp_audit_indx; i++) {
565f8829a4aSRandall Stewart 		if ((sctp_audit_data[i][0] == 0xe0) &&
566f8829a4aSRandall Stewart 		    (sctp_audit_data[i][1] == 0x01)) {
567f8829a4aSRandall Stewart 			cnt = 0;
568ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
569f8829a4aSRandall Stewart 		} else if (sctp_audit_data[i][0] == 0xf0) {
570f8829a4aSRandall Stewart 			cnt = 0;
571ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
572f8829a4aSRandall Stewart 		} else if ((sctp_audit_data[i][0] == 0xc0) &&
573f8829a4aSRandall Stewart 		    (sctp_audit_data[i][1] == 0x01)) {
574ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
575f8829a4aSRandall Stewart 			cnt = 0;
576f8829a4aSRandall Stewart 		}
577ad81507eSRandall Stewart 		SCTP_PRINTF("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0],
578f8829a4aSRandall Stewart 		    (uint32_t) sctp_audit_data[i][1]);
579f8829a4aSRandall Stewart 		cnt++;
580f8829a4aSRandall Stewart 		if ((cnt % 14) == 0)
581ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
582f8829a4aSRandall Stewart 	}
583ad81507eSRandall Stewart 	SCTP_PRINTF("\n");
584f8829a4aSRandall Stewart }
585f8829a4aSRandall Stewart 
586f8829a4aSRandall Stewart void
587f8829a4aSRandall Stewart sctp_auditing(int from, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
588f8829a4aSRandall Stewart     struct sctp_nets *net)
589f8829a4aSRandall Stewart {
590f8829a4aSRandall Stewart 	int resend_cnt, tot_out, rep, tot_book_cnt;
591f8829a4aSRandall Stewart 	struct sctp_nets *lnet;
592f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
593f8829a4aSRandall Stewart 
594f8829a4aSRandall Stewart 	sctp_audit_data[sctp_audit_indx][0] = 0xAA;
595f8829a4aSRandall Stewart 	sctp_audit_data[sctp_audit_indx][1] = 0x000000ff & from;
596f8829a4aSRandall Stewart 	sctp_audit_indx++;
597f8829a4aSRandall Stewart 	if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
598f8829a4aSRandall Stewart 		sctp_audit_indx = 0;
599f8829a4aSRandall Stewart 	}
600f8829a4aSRandall Stewart 	if (inp == NULL) {
601f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xAF;
602f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] = 0x01;
603f8829a4aSRandall Stewart 		sctp_audit_indx++;
604f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
605f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
606f8829a4aSRandall Stewart 		}
607f8829a4aSRandall Stewart 		return;
608f8829a4aSRandall Stewart 	}
609f8829a4aSRandall Stewart 	if (stcb == NULL) {
610f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xAF;
611f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] = 0x02;
612f8829a4aSRandall Stewart 		sctp_audit_indx++;
613f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
614f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
615f8829a4aSRandall Stewart 		}
616f8829a4aSRandall Stewart 		return;
617f8829a4aSRandall Stewart 	}
618f8829a4aSRandall Stewart 	sctp_audit_data[sctp_audit_indx][0] = 0xA1;
619f8829a4aSRandall Stewart 	sctp_audit_data[sctp_audit_indx][1] =
620f8829a4aSRandall Stewart 	    (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
621f8829a4aSRandall Stewart 	sctp_audit_indx++;
622f8829a4aSRandall Stewart 	if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
623f8829a4aSRandall Stewart 		sctp_audit_indx = 0;
624f8829a4aSRandall Stewart 	}
625f8829a4aSRandall Stewart 	rep = 0;
626f8829a4aSRandall Stewart 	tot_book_cnt = 0;
627f8829a4aSRandall Stewart 	resend_cnt = tot_out = 0;
628f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
629f8829a4aSRandall Stewart 		if (chk->sent == SCTP_DATAGRAM_RESEND) {
630f8829a4aSRandall Stewart 			resend_cnt++;
631f8829a4aSRandall Stewart 		} else if (chk->sent < SCTP_DATAGRAM_RESEND) {
632f8829a4aSRandall Stewart 			tot_out += chk->book_size;
633f8829a4aSRandall Stewart 			tot_book_cnt++;
634f8829a4aSRandall Stewart 		}
635f8829a4aSRandall Stewart 	}
636f8829a4aSRandall Stewart 	if (resend_cnt != stcb->asoc.sent_queue_retran_cnt) {
637f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xAF;
638f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] = 0xA1;
639f8829a4aSRandall Stewart 		sctp_audit_indx++;
640f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
641f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
642f8829a4aSRandall Stewart 		}
643ad81507eSRandall Stewart 		SCTP_PRINTF("resend_cnt:%d asoc-tot:%d\n",
644f8829a4aSRandall Stewart 		    resend_cnt, stcb->asoc.sent_queue_retran_cnt);
645f8829a4aSRandall Stewart 		rep = 1;
646f8829a4aSRandall Stewart 		stcb->asoc.sent_queue_retran_cnt = resend_cnt;
647f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xA2;
648f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] =
649f8829a4aSRandall Stewart 		    (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
650f8829a4aSRandall Stewart 		sctp_audit_indx++;
651f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
652f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
653f8829a4aSRandall Stewart 		}
654f8829a4aSRandall Stewart 	}
655f8829a4aSRandall Stewart 	if (tot_out != stcb->asoc.total_flight) {
656f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xAF;
657f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] = 0xA2;
658f8829a4aSRandall Stewart 		sctp_audit_indx++;
659f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
660f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
661f8829a4aSRandall Stewart 		}
662f8829a4aSRandall Stewart 		rep = 1;
663ad81507eSRandall Stewart 		SCTP_PRINTF("tot_flt:%d asoc_tot:%d\n", tot_out,
664f8829a4aSRandall Stewart 		    (int)stcb->asoc.total_flight);
665f8829a4aSRandall Stewart 		stcb->asoc.total_flight = tot_out;
666f8829a4aSRandall Stewart 	}
667f8829a4aSRandall Stewart 	if (tot_book_cnt != stcb->asoc.total_flight_count) {
668f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xAF;
669f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] = 0xA5;
670f8829a4aSRandall Stewart 		sctp_audit_indx++;
671f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
672f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
673f8829a4aSRandall Stewart 		}
674f8829a4aSRandall Stewart 		rep = 1;
675f31e6c7fSMichael Tuexen 		SCTP_PRINTF("tot_flt_book:%d\n", tot_book_cnt);
676f8829a4aSRandall Stewart 
677f8829a4aSRandall Stewart 		stcb->asoc.total_flight_count = tot_book_cnt;
678f8829a4aSRandall Stewart 	}
679f8829a4aSRandall Stewart 	tot_out = 0;
680f8829a4aSRandall Stewart 	TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
681f8829a4aSRandall Stewart 		tot_out += lnet->flight_size;
682f8829a4aSRandall Stewart 	}
683f8829a4aSRandall Stewart 	if (tot_out != stcb->asoc.total_flight) {
684f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xAF;
685f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] = 0xA3;
686f8829a4aSRandall Stewart 		sctp_audit_indx++;
687f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
688f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
689f8829a4aSRandall Stewart 		}
690f8829a4aSRandall Stewart 		rep = 1;
691ad81507eSRandall Stewart 		SCTP_PRINTF("real flight:%d net total was %d\n",
692f8829a4aSRandall Stewart 		    stcb->asoc.total_flight, tot_out);
693f8829a4aSRandall Stewart 		/* now corrective action */
694f8829a4aSRandall Stewart 		TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
695f8829a4aSRandall Stewart 
696f8829a4aSRandall Stewart 			tot_out = 0;
697f8829a4aSRandall Stewart 			TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
698f8829a4aSRandall Stewart 				if ((chk->whoTo == lnet) &&
699f8829a4aSRandall Stewart 				    (chk->sent < SCTP_DATAGRAM_RESEND)) {
700f8829a4aSRandall Stewart 					tot_out += chk->book_size;
701f8829a4aSRandall Stewart 				}
702f8829a4aSRandall Stewart 			}
703f8829a4aSRandall Stewart 			if (lnet->flight_size != tot_out) {
704f31e6c7fSMichael Tuexen 				SCTP_PRINTF("net:%p flight was %d corrected to %d\n",
705dd294dceSMichael Tuexen 				    (void *)lnet, lnet->flight_size,
706ad81507eSRandall Stewart 				    tot_out);
707f8829a4aSRandall Stewart 				lnet->flight_size = tot_out;
708f8829a4aSRandall Stewart 			}
709f8829a4aSRandall Stewart 		}
710f8829a4aSRandall Stewart 	}
711f8829a4aSRandall Stewart 	if (rep) {
712f8829a4aSRandall Stewart 		sctp_print_audit_report();
713f8829a4aSRandall Stewart 	}
714f8829a4aSRandall Stewart }
715f8829a4aSRandall Stewart 
716f8829a4aSRandall Stewart void
717f8829a4aSRandall Stewart sctp_audit_log(uint8_t ev, uint8_t fd)
718f8829a4aSRandall Stewart {
719f8829a4aSRandall Stewart 
720f8829a4aSRandall Stewart 	sctp_audit_data[sctp_audit_indx][0] = ev;
721f8829a4aSRandall Stewart 	sctp_audit_data[sctp_audit_indx][1] = fd;
722f8829a4aSRandall Stewart 	sctp_audit_indx++;
723f8829a4aSRandall Stewart 	if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
724f8829a4aSRandall Stewart 		sctp_audit_indx = 0;
725f8829a4aSRandall Stewart 	}
726f8829a4aSRandall Stewart }
727f8829a4aSRandall Stewart 
728f8829a4aSRandall Stewart #endif
729f8829a4aSRandall Stewart 
730f8829a4aSRandall Stewart /*
73112af6654SMichael Tuexen  * sctp_stop_timers_for_shutdown() should be called
73212af6654SMichael Tuexen  * when entering the SHUTDOWN_SENT or SHUTDOWN_ACK_SENT
73312af6654SMichael Tuexen  * state to make sure that all timers are stopped.
73412af6654SMichael Tuexen  */
73512af6654SMichael Tuexen void
73612af6654SMichael Tuexen sctp_stop_timers_for_shutdown(struct sctp_tcb *stcb)
73712af6654SMichael Tuexen {
73812af6654SMichael Tuexen 	struct sctp_association *asoc;
73912af6654SMichael Tuexen 	struct sctp_nets *net;
74012af6654SMichael Tuexen 
74112af6654SMichael Tuexen 	asoc = &stcb->asoc;
74212af6654SMichael Tuexen 
74312af6654SMichael Tuexen 	(void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
74412af6654SMichael Tuexen 	(void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
74512af6654SMichael Tuexen 	(void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
74612af6654SMichael Tuexen 	(void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
74712af6654SMichael Tuexen 	(void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
74812af6654SMichael Tuexen 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
74912af6654SMichael Tuexen 		(void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
750ca85e948SMichael Tuexen 		(void)SCTP_OS_TIMER_STOP(&net->hb_timer.timer);
75112af6654SMichael Tuexen 	}
75212af6654SMichael Tuexen }
75312af6654SMichael Tuexen 
75412af6654SMichael Tuexen /*
755f8829a4aSRandall Stewart  * a list of sizes based on typical mtu's, used only if next hop size not
756f8829a4aSRandall Stewart  * returned.
757f8829a4aSRandall Stewart  */
758437fc91aSMichael Tuexen static uint32_t sctp_mtu_sizes[] = {
759f8829a4aSRandall Stewart 	68,
760f8829a4aSRandall Stewart 	296,
761f8829a4aSRandall Stewart 	508,
762f8829a4aSRandall Stewart 	512,
763f8829a4aSRandall Stewart 	544,
764f8829a4aSRandall Stewart 	576,
765f8829a4aSRandall Stewart 	1006,
766f8829a4aSRandall Stewart 	1492,
767f8829a4aSRandall Stewart 	1500,
768f8829a4aSRandall Stewart 	1536,
769f8829a4aSRandall Stewart 	2002,
770f8829a4aSRandall Stewart 	2048,
771f8829a4aSRandall Stewart 	4352,
772f8829a4aSRandall Stewart 	4464,
773f8829a4aSRandall Stewart 	8166,
774f8829a4aSRandall Stewart 	17914,
775f8829a4aSRandall Stewart 	32000,
776f8829a4aSRandall Stewart 	65535
777f8829a4aSRandall Stewart };
778f8829a4aSRandall Stewart 
779f8829a4aSRandall Stewart /*
780437fc91aSMichael Tuexen  * Return the largest MTU smaller than val. If there is no
781437fc91aSMichael Tuexen  * entry, just return val.
782f8829a4aSRandall Stewart  */
783437fc91aSMichael Tuexen uint32_t
784437fc91aSMichael Tuexen sctp_get_prev_mtu(uint32_t val)
785437fc91aSMichael Tuexen {
786437fc91aSMichael Tuexen 	uint32_t i;
787437fc91aSMichael Tuexen 
788437fc91aSMichael Tuexen 	if (val <= sctp_mtu_sizes[0]) {
789437fc91aSMichael Tuexen 		return (val);
790437fc91aSMichael Tuexen 	}
791437fc91aSMichael Tuexen 	for (i = 1; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
792437fc91aSMichael Tuexen 		if (val <= sctp_mtu_sizes[i]) {
793f8829a4aSRandall Stewart 			break;
794f8829a4aSRandall Stewart 		}
795f8829a4aSRandall Stewart 	}
796437fc91aSMichael Tuexen 	return (sctp_mtu_sizes[i - 1]);
797437fc91aSMichael Tuexen }
798437fc91aSMichael Tuexen 
799437fc91aSMichael Tuexen /*
800437fc91aSMichael Tuexen  * Return the smallest MTU larger than val. If there is no
801437fc91aSMichael Tuexen  * entry, just return val.
802437fc91aSMichael Tuexen  */
803437fc91aSMichael Tuexen uint32_t
8047215cc1bSMichael Tuexen sctp_get_next_mtu(uint32_t val)
805437fc91aSMichael Tuexen {
806437fc91aSMichael Tuexen 	/* select another MTU that is just bigger than this one */
807437fc91aSMichael Tuexen 	uint32_t i;
808437fc91aSMichael Tuexen 
809437fc91aSMichael Tuexen 	for (i = 0; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
810437fc91aSMichael Tuexen 		if (val < sctp_mtu_sizes[i]) {
811437fc91aSMichael Tuexen 			return (sctp_mtu_sizes[i]);
812437fc91aSMichael Tuexen 		}
813437fc91aSMichael Tuexen 	}
814437fc91aSMichael Tuexen 	return (val);
815f8829a4aSRandall Stewart }
816f8829a4aSRandall Stewart 
817f8829a4aSRandall Stewart void
818f8829a4aSRandall Stewart sctp_fill_random_store(struct sctp_pcb *m)
819f8829a4aSRandall Stewart {
820f8829a4aSRandall Stewart 	/*
821f8829a4aSRandall Stewart 	 * Here we use the MD5/SHA-1 to hash with our good randomNumbers and
822f8829a4aSRandall Stewart 	 * our counter. The result becomes our good random numbers and we
823f8829a4aSRandall Stewart 	 * then setup to give these out. Note that we do no locking to
824f8829a4aSRandall Stewart 	 * protect this. This is ok, since if competing folks call this we
82517205eccSRandall Stewart 	 * will get more gobbled gook in the random store which is what we
826f8829a4aSRandall Stewart 	 * want. There is a danger that two guys will use the same random
827f8829a4aSRandall Stewart 	 * numbers, but thats ok too since that is random as well :->
828f8829a4aSRandall Stewart 	 */
829f8829a4aSRandall Stewart 	m->store_at = 0;
830ad81507eSRandall Stewart 	(void)sctp_hmac(SCTP_HMAC, (uint8_t *) m->random_numbers,
831f8829a4aSRandall Stewart 	    sizeof(m->random_numbers), (uint8_t *) & m->random_counter,
832f8829a4aSRandall Stewart 	    sizeof(m->random_counter), (uint8_t *) m->random_store);
833f8829a4aSRandall Stewart 	m->random_counter++;
834f8829a4aSRandall Stewart }
835f8829a4aSRandall Stewart 
836f8829a4aSRandall Stewart uint32_t
837851b7298SRandall Stewart sctp_select_initial_TSN(struct sctp_pcb *inp)
838f8829a4aSRandall Stewart {
839f8829a4aSRandall Stewart 	/*
840f8829a4aSRandall Stewart 	 * A true implementation should use random selection process to get
841f8829a4aSRandall Stewart 	 * the initial stream sequence number, using RFC1750 as a good
842f8829a4aSRandall Stewart 	 * guideline
843f8829a4aSRandall Stewart 	 */
844139bc87fSRandall Stewart 	uint32_t x, *xp;
845f8829a4aSRandall Stewart 	uint8_t *p;
846851b7298SRandall Stewart 	int store_at, new_store;
847f8829a4aSRandall Stewart 
848851b7298SRandall Stewart 	if (inp->initial_sequence_debug != 0) {
849f8829a4aSRandall Stewart 		uint32_t ret;
850f8829a4aSRandall Stewart 
851851b7298SRandall Stewart 		ret = inp->initial_sequence_debug;
852851b7298SRandall Stewart 		inp->initial_sequence_debug++;
853f8829a4aSRandall Stewart 		return (ret);
854f8829a4aSRandall Stewart 	}
855851b7298SRandall Stewart retry:
856851b7298SRandall Stewart 	store_at = inp->store_at;
857851b7298SRandall Stewart 	new_store = store_at + sizeof(uint32_t);
858851b7298SRandall Stewart 	if (new_store >= (SCTP_SIGNATURE_SIZE - 3)) {
859851b7298SRandall Stewart 		new_store = 0;
860f8829a4aSRandall Stewart 	}
861851b7298SRandall Stewart 	if (!atomic_cmpset_int(&inp->store_at, store_at, new_store)) {
862851b7298SRandall Stewart 		goto retry;
863851b7298SRandall Stewart 	}
864851b7298SRandall Stewart 	if (new_store == 0) {
865851b7298SRandall Stewart 		/* Refill the random store */
866851b7298SRandall Stewart 		sctp_fill_random_store(inp);
867851b7298SRandall Stewart 	}
868851b7298SRandall Stewart 	p = &inp->random_store[store_at];
869139bc87fSRandall Stewart 	xp = (uint32_t *) p;
870f8829a4aSRandall Stewart 	x = *xp;
871f8829a4aSRandall Stewart 	return (x);
872f8829a4aSRandall Stewart }
873f8829a4aSRandall Stewart 
874f8829a4aSRandall Stewart uint32_t
8757215cc1bSMichael Tuexen sctp_select_a_tag(struct sctp_inpcb *inp, uint16_t lport, uint16_t rport, int check)
876f8829a4aSRandall Stewart {
8777215cc1bSMichael Tuexen 	uint32_t x;
878f8829a4aSRandall Stewart 	struct timeval now;
879f8829a4aSRandall Stewart 
8807215cc1bSMichael Tuexen 	if (check) {
8816e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
8827215cc1bSMichael Tuexen 	}
8837215cc1bSMichael Tuexen 	for (;;) {
884851b7298SRandall Stewart 		x = sctp_select_initial_TSN(&inp->sctp_ep);
885f8829a4aSRandall Stewart 		if (x == 0) {
886f8829a4aSRandall Stewart 			/* we never use 0 */
887f8829a4aSRandall Stewart 			continue;
888f8829a4aSRandall Stewart 		}
8897215cc1bSMichael Tuexen 		if (!check || sctp_is_vtag_good(x, lport, rport, &now)) {
8907215cc1bSMichael Tuexen 			break;
891f8829a4aSRandall Stewart 		}
892f8829a4aSRandall Stewart 	}
893f8829a4aSRandall Stewart 	return (x);
894f8829a4aSRandall Stewart }
895f8829a4aSRandall Stewart 
896f8829a4aSRandall Stewart int
897a1cb341bSMichael Tuexen sctp_init_asoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
898b5c16493SMichael Tuexen     uint32_t override_tag, uint32_t vrf_id)
899f8829a4aSRandall Stewart {
9000696e120SRandall Stewart 	struct sctp_association *asoc;
9010696e120SRandall Stewart 
902f8829a4aSRandall Stewart 	/*
903f8829a4aSRandall Stewart 	 * Anything set to zero is taken care of by the allocation routine's
904f8829a4aSRandall Stewart 	 * bzero
905f8829a4aSRandall Stewart 	 */
906f8829a4aSRandall Stewart 
907f8829a4aSRandall Stewart 	/*
908f8829a4aSRandall Stewart 	 * Up front select what scoping to apply on addresses I tell my peer
909f8829a4aSRandall Stewart 	 * Not sure what to do with these right now, we will need to come up
910f8829a4aSRandall Stewart 	 * with a way to set them. We may need to pass them through from the
911f8829a4aSRandall Stewart 	 * caller in the sctp_aloc_assoc() function.
912f8829a4aSRandall Stewart 	 */
913f8829a4aSRandall Stewart 	int i;
914f8829a4aSRandall Stewart 
915f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
916f0396ad1SMichael Tuexen 	int j;
917f0396ad1SMichael Tuexen 
918f0396ad1SMichael Tuexen #endif
919f0396ad1SMichael Tuexen 
9200696e120SRandall Stewart 	asoc = &stcb->asoc;
921f8829a4aSRandall Stewart 	/* init all variables to a known value. */
922c4739e2fSRandall Stewart 	SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_INUSE);
923a1cb341bSMichael Tuexen 	asoc->max_burst = inp->sctp_ep.max_burst;
924a1cb341bSMichael Tuexen 	asoc->fr_max_burst = inp->sctp_ep.fr_max_burst;
925a1cb341bSMichael Tuexen 	asoc->heart_beat_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
926a1cb341bSMichael Tuexen 	asoc->cookie_life = inp->sctp_ep.def_cookie_life;
927a1cb341bSMichael Tuexen 	asoc->sctp_cmt_on_off = inp->sctp_cmt_on_off;
928f342355aSMichael Tuexen 	asoc->ecn_supported = inp->ecn_supported;
929dd973b0eSMichael Tuexen 	asoc->prsctp_supported = inp->prsctp_supported;
930c79bec9cSMichael Tuexen 	asoc->auth_supported = inp->auth_supported;
931c79bec9cSMichael Tuexen 	asoc->asconf_supported = inp->asconf_supported;
932317e00efSMichael Tuexen 	asoc->reconfig_supported = inp->reconfig_supported;
933caea9879SMichael Tuexen 	asoc->nrsack_supported = inp->nrsack_supported;
934cb9b8e6fSMichael Tuexen 	asoc->pktdrop_supported = inp->pktdrop_supported;
935ca85e948SMichael Tuexen 	asoc->sctp_cmt_pf = (uint8_t) 0;
936a1cb341bSMichael Tuexen 	asoc->sctp_frag_point = inp->sctp_frag_point;
937a1cb341bSMichael Tuexen 	asoc->sctp_features = inp->sctp_features;
938a1cb341bSMichael Tuexen 	asoc->default_dscp = inp->sctp_ep.default_dscp;
93942551e99SRandall Stewart #ifdef INET6
940a1cb341bSMichael Tuexen 	if (inp->sctp_ep.default_flowlabel) {
941a1cb341bSMichael Tuexen 		asoc->default_flowlabel = inp->sctp_ep.default_flowlabel;
94258bdb691SMichael Tuexen 	} else {
943a1cb341bSMichael Tuexen 		if (inp->ip_inp.inp.inp_flags & IN6P_AUTOFLOWLABEL) {
944a1cb341bSMichael Tuexen 			asoc->default_flowlabel = sctp_select_initial_TSN(&inp->sctp_ep);
94558bdb691SMichael Tuexen 			asoc->default_flowlabel &= 0x000fffff;
94658bdb691SMichael Tuexen 			asoc->default_flowlabel |= 0x80000000;
94758bdb691SMichael Tuexen 		} else {
948f8829a4aSRandall Stewart 			asoc->default_flowlabel = 0;
94958bdb691SMichael Tuexen 		}
95058bdb691SMichael Tuexen 	}
951f8829a4aSRandall Stewart #endif
9529f22f500SRandall Stewart 	asoc->sb_send_resv = 0;
953f8829a4aSRandall Stewart 	if (override_tag) {
954f8829a4aSRandall Stewart 		asoc->my_vtag = override_tag;
955f8829a4aSRandall Stewart 	} else {
956a1cb341bSMichael Tuexen 		asoc->my_vtag = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
957f8829a4aSRandall Stewart 	}
958de0e935bSRandall Stewart 	/* Get the nonce tags */
959a1cb341bSMichael Tuexen 	asoc->my_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
960a1cb341bSMichael Tuexen 	asoc->peer_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
96142551e99SRandall Stewart 	asoc->vrf_id = vrf_id;
962de0e935bSRandall Stewart 
96318e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
96418e198d3SRandall Stewart 	asoc->tsn_in_at = 0;
96518e198d3SRandall Stewart 	asoc->tsn_out_at = 0;
96618e198d3SRandall Stewart 	asoc->tsn_in_wrapped = 0;
96718e198d3SRandall Stewart 	asoc->tsn_out_wrapped = 0;
96818e198d3SRandall Stewart 	asoc->cumack_log_at = 0;
969b201f536SRandall Stewart 	asoc->cumack_log_atsnt = 0;
97018e198d3SRandall Stewart #endif
97118e198d3SRandall Stewart #ifdef SCTP_FS_SPEC_LOG
97218e198d3SRandall Stewart 	asoc->fs_index = 0;
97318e198d3SRandall Stewart #endif
974f8829a4aSRandall Stewart 	asoc->refcnt = 0;
975f8829a4aSRandall Stewart 	asoc->assoc_up_sent = 0;
976f8829a4aSRandall Stewart 	asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number = asoc->sending_seq =
977a1cb341bSMichael Tuexen 	    sctp_select_initial_TSN(&inp->sctp_ep);
978c54a18d2SRandall Stewart 	asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
979f8829a4aSRandall Stewart 	/* we are optimisitic here */
980830d754dSRandall Stewart 	asoc->peer_supports_nat = 0;
981f8829a4aSRandall Stewart 	asoc->sent_queue_retran_cnt = 0;
982f8829a4aSRandall Stewart 
983f8829a4aSRandall Stewart 	/* for CMT */
9848933fa13SRandall Stewart 	asoc->last_net_cmt_send_started = NULL;
985f8829a4aSRandall Stewart 
986f8829a4aSRandall Stewart 	/* This will need to be adjusted */
987f8829a4aSRandall Stewart 	asoc->last_acked_seq = asoc->init_seq_number - 1;
988f8829a4aSRandall Stewart 	asoc->advanced_peer_ack_point = asoc->last_acked_seq;
989f8829a4aSRandall Stewart 	asoc->asconf_seq_in = asoc->last_acked_seq;
990f8829a4aSRandall Stewart 
991f8829a4aSRandall Stewart 	/* here we are different, we hold the next one we expect */
992f8829a4aSRandall Stewart 	asoc->str_reset_seq_in = asoc->last_acked_seq + 1;
993f8829a4aSRandall Stewart 
994a1cb341bSMichael Tuexen 	asoc->initial_init_rto_max = inp->sctp_ep.initial_init_rto_max;
995a1cb341bSMichael Tuexen 	asoc->initial_rto = inp->sctp_ep.initial_rto;
996f8829a4aSRandall Stewart 
997a1cb341bSMichael Tuexen 	asoc->max_init_times = inp->sctp_ep.max_init_times;
998a1cb341bSMichael Tuexen 	asoc->max_send_times = inp->sctp_ep.max_send_times;
999a1cb341bSMichael Tuexen 	asoc->def_net_failure = inp->sctp_ep.def_net_failure;
1000a1cb341bSMichael Tuexen 	asoc->def_net_pf_threshold = inp->sctp_ep.def_net_pf_threshold;
1001f8829a4aSRandall Stewart 	asoc->free_chunk_cnt = 0;
1002f8829a4aSRandall Stewart 
1003f8829a4aSRandall Stewart 	asoc->iam_blocking = 0;
1004a1cb341bSMichael Tuexen 	asoc->context = inp->sctp_context;
1005a1cb341bSMichael Tuexen 	asoc->local_strreset_support = inp->local_strreset_support;
1006a1cb341bSMichael Tuexen 	asoc->def_send = inp->def_send;
1007a1cb341bSMichael Tuexen 	asoc->delayed_ack = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1008a1cb341bSMichael Tuexen 	asoc->sack_freq = inp->sctp_ep.sctp_sack_freq;
1009f8829a4aSRandall Stewart 	asoc->pr_sctp_cnt = 0;
1010f8829a4aSRandall Stewart 	asoc->total_output_queue_size = 0;
1011f8829a4aSRandall Stewart 
1012a1cb341bSMichael Tuexen 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1013a1cb341bSMichael Tuexen 		asoc->scope.ipv6_addr_legal = 1;
1014a1cb341bSMichael Tuexen 		if (SCTP_IPV6_V6ONLY(inp) == 0) {
1015a1cb341bSMichael Tuexen 			asoc->scope.ipv4_addr_legal = 1;
1016f8829a4aSRandall Stewart 		} else {
1017a1cb341bSMichael Tuexen 			asoc->scope.ipv4_addr_legal = 0;
1018f8829a4aSRandall Stewart 		}
1019f8829a4aSRandall Stewart 	} else {
1020a1cb341bSMichael Tuexen 		asoc->scope.ipv6_addr_legal = 0;
1021a1cb341bSMichael Tuexen 		asoc->scope.ipv4_addr_legal = 1;
1022f8829a4aSRandall Stewart 	}
1023f8829a4aSRandall Stewart 
1024a1cb341bSMichael Tuexen 	asoc->my_rwnd = max(SCTP_SB_LIMIT_RCV(inp->sctp_socket), SCTP_MINIMAL_RWND);
1025a1cb341bSMichael Tuexen 	asoc->peers_rwnd = SCTP_SB_LIMIT_RCV(inp->sctp_socket);
1026f8829a4aSRandall Stewart 
1027a1cb341bSMichael Tuexen 	asoc->smallest_mtu = inp->sctp_frag_point;
1028a1cb341bSMichael Tuexen 	asoc->minrto = inp->sctp_ep.sctp_minrto;
1029a1cb341bSMichael Tuexen 	asoc->maxrto = inp->sctp_ep.sctp_maxrto;
1030f8829a4aSRandall Stewart 
1031f8829a4aSRandall Stewart 	asoc->locked_on_sending = NULL;
1032f8829a4aSRandall Stewart 	asoc->stream_locked_on = 0;
1033f8829a4aSRandall Stewart 	asoc->ecn_echo_cnt_onq = 0;
1034f8829a4aSRandall Stewart 	asoc->stream_locked = 0;
1035f8829a4aSRandall Stewart 
103642551e99SRandall Stewart 	asoc->send_sack = 1;
103742551e99SRandall Stewart 
103842551e99SRandall Stewart 	LIST_INIT(&asoc->sctp_restricted_addrs);
103942551e99SRandall Stewart 
1040f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->nets);
1041f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->pending_reply_queue);
10422afb3e84SRandall Stewart 	TAILQ_INIT(&asoc->asconf_ack_sent);
1043f8829a4aSRandall Stewart 	/* Setup to fill the hb random cache at first HB */
1044f8829a4aSRandall Stewart 	asoc->hb_random_idx = 4;
1045f8829a4aSRandall Stewart 
1046a1cb341bSMichael Tuexen 	asoc->sctp_autoclose_ticks = inp->sctp_ep.auto_close_time;
1047f8829a4aSRandall Stewart 
1048a1cb341bSMichael Tuexen 	stcb->asoc.congestion_control_module = inp->sctp_ep.sctp_default_cc_module;
1049a1cb341bSMichael Tuexen 	stcb->asoc.cc_functions = sctp_cc_functions[inp->sctp_ep.sctp_default_cc_module];
1050b54d3a6cSRandall Stewart 
1051a1cb341bSMichael Tuexen 	stcb->asoc.stream_scheduling_module = inp->sctp_ep.sctp_default_ss_module;
1052a1cb341bSMichael Tuexen 	stcb->asoc.ss_functions = sctp_ss_functions[inp->sctp_ep.sctp_default_ss_module];
1053f7a77f6fSMichael Tuexen 
1054b54d3a6cSRandall Stewart 	/*
1055f8829a4aSRandall Stewart 	 * Now the stream parameters, here we allocate space for all streams
1056f8829a4aSRandall Stewart 	 * that we request by default.
1057f8829a4aSRandall Stewart 	 */
1058ea44232bSRandall Stewart 	asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams =
1059a1cb341bSMichael Tuexen 	    inp->sctp_ep.pre_open_stream_count;
1060f8829a4aSRandall Stewart 	SCTP_MALLOC(asoc->strmout, struct sctp_stream_out *,
1061f8829a4aSRandall Stewart 	    asoc->streamoutcnt * sizeof(struct sctp_stream_out),
1062207304d4SRandall Stewart 	    SCTP_M_STRMO);
1063f8829a4aSRandall Stewart 	if (asoc->strmout == NULL) {
1064f8829a4aSRandall Stewart 		/* big trouble no memory */
1065c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1066f8829a4aSRandall Stewart 		return (ENOMEM);
1067f8829a4aSRandall Stewart 	}
1068f8829a4aSRandall Stewart 	for (i = 0; i < asoc->streamoutcnt; i++) {
1069f8829a4aSRandall Stewart 		/*
1070f8829a4aSRandall Stewart 		 * inbound side must be set to 0xffff, also NOTE when we get
1071f8829a4aSRandall Stewart 		 * the INIT-ACK back (for INIT sender) we MUST reduce the
1072f8829a4aSRandall Stewart 		 * count (streamoutcnt) but first check if we sent to any of
1073f8829a4aSRandall Stewart 		 * the upper streams that were dropped (if some were). Those
1074f8829a4aSRandall Stewart 		 * that were dropped must be notified to the upper layer as
1075f8829a4aSRandall Stewart 		 * failed to send.
1076f8829a4aSRandall Stewart 		 */
1077f3b05218SMichael Tuexen 		asoc->strmout[i].next_sequence_send = 0x0;
1078f8829a4aSRandall Stewart 		TAILQ_INIT(&asoc->strmout[i].outqueue);
1079325c8c46SMichael Tuexen 		asoc->strmout[i].chunks_on_queues = 0;
1080f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
1081f0396ad1SMichael Tuexen 		for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1082f0396ad1SMichael Tuexen 			asoc->strmout[i].abandoned_sent[j] = 0;
1083f0396ad1SMichael Tuexen 			asoc->strmout[i].abandoned_unsent[j] = 0;
1084f0396ad1SMichael Tuexen 		}
1085f0396ad1SMichael Tuexen #else
1086f0396ad1SMichael Tuexen 		asoc->strmout[i].abandoned_sent[0] = 0;
1087f0396ad1SMichael Tuexen 		asoc->strmout[i].abandoned_unsent[0] = 0;
1088f0396ad1SMichael Tuexen #endif
1089f8829a4aSRandall Stewart 		asoc->strmout[i].stream_no = i;
1090f8829a4aSRandall Stewart 		asoc->strmout[i].last_msg_incomplete = 0;
1091252f7f93SMichael Tuexen 		asoc->ss_functions.sctp_ss_init_stream(&asoc->strmout[i], NULL);
1092f8829a4aSRandall Stewart 	}
1093f7a77f6fSMichael Tuexen 	asoc->ss_functions.sctp_ss_init(stcb, asoc, 0);
1094f7a77f6fSMichael Tuexen 
1095f8829a4aSRandall Stewart 	/* Now the mapping array */
1096f8829a4aSRandall Stewart 	asoc->mapping_array_size = SCTP_INITIAL_MAPPING_ARRAY;
1097f8829a4aSRandall Stewart 	SCTP_MALLOC(asoc->mapping_array, uint8_t *, asoc->mapping_array_size,
1098207304d4SRandall Stewart 	    SCTP_M_MAP);
1099f8829a4aSRandall Stewart 	if (asoc->mapping_array == NULL) {
1100207304d4SRandall Stewart 		SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1101c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1102f8829a4aSRandall Stewart 		return (ENOMEM);
1103f8829a4aSRandall Stewart 	}
1104f8829a4aSRandall Stewart 	memset(asoc->mapping_array, 0, asoc->mapping_array_size);
1105b5c16493SMichael Tuexen 	SCTP_MALLOC(asoc->nr_mapping_array, uint8_t *, asoc->mapping_array_size,
1106830d754dSRandall Stewart 	    SCTP_M_MAP);
1107bf1be571SRandall Stewart 	if (asoc->nr_mapping_array == NULL) {
1108bf1be571SRandall Stewart 		SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1109bf1be571SRandall Stewart 		SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1110bf1be571SRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1111bf1be571SRandall Stewart 		return (ENOMEM);
1112bf1be571SRandall Stewart 	}
1113b5c16493SMichael Tuexen 	memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
1114830d754dSRandall Stewart 
1115f8829a4aSRandall Stewart 	/* Now the init of the other outqueues */
1116f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->free_chunks);
1117f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->control_send_queue);
1118c54a18d2SRandall Stewart 	TAILQ_INIT(&asoc->asconf_send_queue);
1119f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->send_queue);
1120f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->sent_queue);
1121f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->reasmqueue);
1122f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->resetHead);
1123a1cb341bSMichael Tuexen 	asoc->max_inbound_streams = inp->sctp_ep.max_open_streams_intome;
1124f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->asconf_queue);
1125f8829a4aSRandall Stewart 	/* authentication fields */
1126f8829a4aSRandall Stewart 	asoc->authinfo.random = NULL;
1127830d754dSRandall Stewart 	asoc->authinfo.active_keyid = 0;
1128f8829a4aSRandall Stewart 	asoc->authinfo.assoc_key = NULL;
1129f8829a4aSRandall Stewart 	asoc->authinfo.assoc_keyid = 0;
1130f8829a4aSRandall Stewart 	asoc->authinfo.recv_key = NULL;
1131f8829a4aSRandall Stewart 	asoc->authinfo.recv_keyid = 0;
1132f8829a4aSRandall Stewart 	LIST_INIT(&asoc->shared_keys);
1133f42a358aSRandall Stewart 	asoc->marked_retrans = 0;
1134a1cb341bSMichael Tuexen 	asoc->port = inp->sctp_ep.port;
1135f42a358aSRandall Stewart 	asoc->timoinit = 0;
1136f42a358aSRandall Stewart 	asoc->timodata = 0;
1137f42a358aSRandall Stewart 	asoc->timosack = 0;
1138f42a358aSRandall Stewart 	asoc->timoshutdown = 0;
1139f42a358aSRandall Stewart 	asoc->timoheartbeat = 0;
1140f42a358aSRandall Stewart 	asoc->timocookie = 0;
1141f42a358aSRandall Stewart 	asoc->timoshutdownack = 0;
11426e55db54SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&asoc->start_time);
11436e55db54SRandall Stewart 	asoc->discontinuity_time = asoc->start_time;
1144f0396ad1SMichael Tuexen 	for (i = 0; i < SCTP_PR_SCTP_MAX + 1; i++) {
1145f0396ad1SMichael Tuexen 		asoc->abandoned_unsent[i] = 0;
1146f0396ad1SMichael Tuexen 		asoc->abandoned_sent[i] = 0;
1147f0396ad1SMichael Tuexen 	}
1148eacc51c5SRandall Stewart 	/*
1149eacc51c5SRandall Stewart 	 * sa_ignore MEMLEAK {memory is put in the assoc mapping array and
115077acdc25SRandall Stewart 	 * freed later when the association is freed.
1151eacc51c5SRandall Stewart 	 */
1152f8829a4aSRandall Stewart 	return (0);
1153f8829a4aSRandall Stewart }
1154f8829a4aSRandall Stewart 
11550e13104dSRandall Stewart void
11560e13104dSRandall Stewart sctp_print_mapping_array(struct sctp_association *asoc)
11570e13104dSRandall Stewart {
1158aed5947cSMichael Tuexen 	unsigned int i, limit;
11590e13104dSRandall Stewart 
1160cd3fd531SMichael Tuexen 	SCTP_PRINTF("Mapping array size: %d, baseTSN: %8.8x, cumAck: %8.8x, highestTSN: (%8.8x, %8.8x).\n",
11610e13104dSRandall Stewart 	    asoc->mapping_array_size,
11620e13104dSRandall Stewart 	    asoc->mapping_array_base_tsn,
11630e13104dSRandall Stewart 	    asoc->cumulative_tsn,
1164aed5947cSMichael Tuexen 	    asoc->highest_tsn_inside_map,
1165aed5947cSMichael Tuexen 	    asoc->highest_tsn_inside_nr_map);
1166aed5947cSMichael Tuexen 	for (limit = asoc->mapping_array_size; limit > 1; limit--) {
116760990c0cSMichael Tuexen 		if (asoc->mapping_array[limit - 1] != 0) {
116877acdc25SRandall Stewart 			break;
116977acdc25SRandall Stewart 		}
117077acdc25SRandall Stewart 	}
1171cd3fd531SMichael Tuexen 	SCTP_PRINTF("Renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
117277acdc25SRandall Stewart 	for (i = 0; i < limit; i++) {
1173cd3fd531SMichael Tuexen 		SCTP_PRINTF("%2.2x%c", asoc->mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
117477acdc25SRandall Stewart 	}
1175aed5947cSMichael Tuexen 	if (limit % 16)
1176cd3fd531SMichael Tuexen 		SCTP_PRINTF("\n");
1177aed5947cSMichael Tuexen 	for (limit = asoc->mapping_array_size; limit > 1; limit--) {
1178aed5947cSMichael Tuexen 		if (asoc->nr_mapping_array[limit - 1]) {
117977acdc25SRandall Stewart 			break;
118077acdc25SRandall Stewart 		}
118177acdc25SRandall Stewart 	}
1182cd3fd531SMichael Tuexen 	SCTP_PRINTF("Non renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
118377acdc25SRandall Stewart 	for (i = 0; i < limit; i++) {
1184cd3fd531SMichael Tuexen 		SCTP_PRINTF("%2.2x%c", asoc->nr_mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
11850e13104dSRandall Stewart 	}
1186aed5947cSMichael Tuexen 	if (limit % 16)
1187cd3fd531SMichael Tuexen 		SCTP_PRINTF("\n");
11880e13104dSRandall Stewart }
11890e13104dSRandall Stewart 
1190f8829a4aSRandall Stewart int
11910696e120SRandall Stewart sctp_expand_mapping_array(struct sctp_association *asoc, uint32_t needed)
1192f8829a4aSRandall Stewart {
1193f8829a4aSRandall Stewart 	/* mapping array needs to grow */
1194b5c16493SMichael Tuexen 	uint8_t *new_array1, *new_array2;
11950696e120SRandall Stewart 	uint32_t new_size;
1196f8829a4aSRandall Stewart 
11970696e120SRandall Stewart 	new_size = asoc->mapping_array_size + ((needed + 7) / 8 + SCTP_MAPPING_ARRAY_INCR);
1198b5c16493SMichael Tuexen 	SCTP_MALLOC(new_array1, uint8_t *, new_size, SCTP_M_MAP);
1199b5c16493SMichael Tuexen 	SCTP_MALLOC(new_array2, uint8_t *, new_size, SCTP_M_MAP);
1200b5c16493SMichael Tuexen 	if ((new_array1 == NULL) || (new_array2 == NULL)) {
1201f8829a4aSRandall Stewart 		/* can't get more, forget it */
1202b5c16493SMichael Tuexen 		SCTP_PRINTF("No memory for expansion of SCTP mapping array %d\n", new_size);
1203b5c16493SMichael Tuexen 		if (new_array1) {
1204b5c16493SMichael Tuexen 			SCTP_FREE(new_array1, SCTP_M_MAP);
1205b5c16493SMichael Tuexen 		}
1206b5c16493SMichael Tuexen 		if (new_array2) {
1207b5c16493SMichael Tuexen 			SCTP_FREE(new_array2, SCTP_M_MAP);
1208b5c16493SMichael Tuexen 		}
1209f8829a4aSRandall Stewart 		return (-1);
1210f8829a4aSRandall Stewart 	}
1211b5c16493SMichael Tuexen 	memset(new_array1, 0, new_size);
1212b5c16493SMichael Tuexen 	memset(new_array2, 0, new_size);
1213b5c16493SMichael Tuexen 	memcpy(new_array1, asoc->mapping_array, asoc->mapping_array_size);
1214b5c16493SMichael Tuexen 	memcpy(new_array2, asoc->nr_mapping_array, asoc->mapping_array_size);
1215207304d4SRandall Stewart 	SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1216830d754dSRandall Stewart 	SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
1217b5c16493SMichael Tuexen 	asoc->mapping_array = new_array1;
1218b5c16493SMichael Tuexen 	asoc->nr_mapping_array = new_array2;
1219b5c16493SMichael Tuexen 	asoc->mapping_array_size = new_size;
1220830d754dSRandall Stewart 	return (0);
1221830d754dSRandall Stewart }
1222830d754dSRandall Stewart 
12238933fa13SRandall Stewart 
122442551e99SRandall Stewart static void
122542551e99SRandall Stewart sctp_iterator_work(struct sctp_iterator *it)
122642551e99SRandall Stewart {
122742551e99SRandall Stewart 	int iteration_count = 0;
122842551e99SRandall Stewart 	int inp_skip = 0;
1229ec4c19fcSRandall Stewart 	int first_in = 1;
1230ec4c19fcSRandall Stewart 	struct sctp_inpcb *tinp;
123142551e99SRandall Stewart 
1232ec4c19fcSRandall Stewart 	SCTP_INP_INFO_RLOCK();
123342551e99SRandall Stewart 	SCTP_ITERATOR_LOCK();
1234ad81507eSRandall Stewart 	if (it->inp) {
1235ec4c19fcSRandall Stewart 		SCTP_INP_RLOCK(it->inp);
123642551e99SRandall Stewart 		SCTP_INP_DECR_REF(it->inp);
1237ad81507eSRandall Stewart 	}
123842551e99SRandall Stewart 	if (it->inp == NULL) {
123942551e99SRandall Stewart 		/* iterator is complete */
124042551e99SRandall Stewart done_with_iterator:
124142551e99SRandall Stewart 		SCTP_ITERATOR_UNLOCK();
1242ec4c19fcSRandall Stewart 		SCTP_INP_INFO_RUNLOCK();
124342551e99SRandall Stewart 		if (it->function_atend != NULL) {
124442551e99SRandall Stewart 			(*it->function_atend) (it->pointer, it->val);
124542551e99SRandall Stewart 		}
1246207304d4SRandall Stewart 		SCTP_FREE(it, SCTP_M_ITER);
124742551e99SRandall Stewart 		return;
124842551e99SRandall Stewart 	}
124942551e99SRandall Stewart select_a_new_ep:
1250ec4c19fcSRandall Stewart 	if (first_in) {
1251ec4c19fcSRandall Stewart 		first_in = 0;
1252ec4c19fcSRandall Stewart 	} else {
1253f7517433SRandall Stewart 		SCTP_INP_RLOCK(it->inp);
1254ec4c19fcSRandall Stewart 	}
125542551e99SRandall Stewart 	while (((it->pcb_flags) &&
125642551e99SRandall Stewart 	    ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) ||
125742551e99SRandall Stewart 	    ((it->pcb_features) &&
125842551e99SRandall Stewart 	    ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) {
125942551e99SRandall Stewart 		/* endpoint flags or features don't match, so keep looking */
126042551e99SRandall Stewart 		if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1261f7517433SRandall Stewart 			SCTP_INP_RUNLOCK(it->inp);
126242551e99SRandall Stewart 			goto done_with_iterator;
126342551e99SRandall Stewart 		}
1264ec4c19fcSRandall Stewart 		tinp = it->inp;
126542551e99SRandall Stewart 		it->inp = LIST_NEXT(it->inp, sctp_list);
1266ec4c19fcSRandall Stewart 		SCTP_INP_RUNLOCK(tinp);
126742551e99SRandall Stewart 		if (it->inp == NULL) {
126842551e99SRandall Stewart 			goto done_with_iterator;
126942551e99SRandall Stewart 		}
127042551e99SRandall Stewart 		SCTP_INP_RLOCK(it->inp);
1271f7517433SRandall Stewart 	}
127242551e99SRandall Stewart 	/* now go through each assoc which is in the desired state */
127342551e99SRandall Stewart 	if (it->done_current_ep == 0) {
127442551e99SRandall Stewart 		if (it->function_inp != NULL)
127542551e99SRandall Stewart 			inp_skip = (*it->function_inp) (it->inp, it->pointer, it->val);
127642551e99SRandall Stewart 		it->done_current_ep = 1;
127742551e99SRandall Stewart 	}
127842551e99SRandall Stewart 	if (it->stcb == NULL) {
127942551e99SRandall Stewart 		/* run the per instance function */
128042551e99SRandall Stewart 		it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
128142551e99SRandall Stewart 	}
128242551e99SRandall Stewart 	if ((inp_skip) || it->stcb == NULL) {
128342551e99SRandall Stewart 		if (it->function_inp_end != NULL) {
128442551e99SRandall Stewart 			inp_skip = (*it->function_inp_end) (it->inp,
128542551e99SRandall Stewart 			    it->pointer,
128642551e99SRandall Stewart 			    it->val);
128742551e99SRandall Stewart 		}
128842551e99SRandall Stewart 		SCTP_INP_RUNLOCK(it->inp);
128942551e99SRandall Stewart 		goto no_stcb;
129042551e99SRandall Stewart 	}
129142551e99SRandall Stewart 	while (it->stcb) {
129242551e99SRandall Stewart 		SCTP_TCB_LOCK(it->stcb);
129342551e99SRandall Stewart 		if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
129442551e99SRandall Stewart 			/* not in the right state... keep looking */
129542551e99SRandall Stewart 			SCTP_TCB_UNLOCK(it->stcb);
129642551e99SRandall Stewart 			goto next_assoc;
129742551e99SRandall Stewart 		}
129842551e99SRandall Stewart 		/* see if we have limited out the iterator loop */
129942551e99SRandall Stewart 		iteration_count++;
130042551e99SRandall Stewart 		if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) {
130142551e99SRandall Stewart 			/* Pause to let others grab the lock */
130242551e99SRandall Stewart 			atomic_add_int(&it->stcb->asoc.refcnt, 1);
130342551e99SRandall Stewart 			SCTP_TCB_UNLOCK(it->stcb);
1304c4739e2fSRandall Stewart 			SCTP_INP_INCR_REF(it->inp);
130542551e99SRandall Stewart 			SCTP_INP_RUNLOCK(it->inp);
130642551e99SRandall Stewart 			SCTP_ITERATOR_UNLOCK();
1307ec4c19fcSRandall Stewart 			SCTP_INP_INFO_RUNLOCK();
1308ec4c19fcSRandall Stewart 			SCTP_INP_INFO_RLOCK();
130942551e99SRandall Stewart 			SCTP_ITERATOR_LOCK();
1310f7517433SRandall Stewart 			if (sctp_it_ctl.iterator_flags) {
1311f7517433SRandall Stewart 				/* We won't be staying here */
1312f7517433SRandall Stewart 				SCTP_INP_DECR_REF(it->inp);
1313f7517433SRandall Stewart 				atomic_add_int(&it->stcb->asoc.refcnt, -1);
1314f7517433SRandall Stewart 				if (sctp_it_ctl.iterator_flags &
1315f7517433SRandall Stewart 				    SCTP_ITERATOR_STOP_CUR_IT) {
1316f7517433SRandall Stewart 					sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_IT;
1317f7517433SRandall Stewart 					goto done_with_iterator;
1318f7517433SRandall Stewart 				}
1319f7517433SRandall Stewart 				if (sctp_it_ctl.iterator_flags &
1320f7517433SRandall Stewart 				    SCTP_ITERATOR_STOP_CUR_INP) {
1321f7517433SRandall Stewart 					sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_INP;
1322f7517433SRandall Stewart 					goto no_stcb;
1323f7517433SRandall Stewart 				}
1324f7517433SRandall Stewart 				/* If we reach here huh? */
1325cd3fd531SMichael Tuexen 				SCTP_PRINTF("Unknown it ctl flag %x\n",
1326f7517433SRandall Stewart 				    sctp_it_ctl.iterator_flags);
1327f7517433SRandall Stewart 				sctp_it_ctl.iterator_flags = 0;
1328f7517433SRandall Stewart 			}
132942551e99SRandall Stewart 			SCTP_INP_RLOCK(it->inp);
1330c4739e2fSRandall Stewart 			SCTP_INP_DECR_REF(it->inp);
133142551e99SRandall Stewart 			SCTP_TCB_LOCK(it->stcb);
133242551e99SRandall Stewart 			atomic_add_int(&it->stcb->asoc.refcnt, -1);
133342551e99SRandall Stewart 			iteration_count = 0;
133442551e99SRandall Stewart 		}
133542551e99SRandall Stewart 		/* run function on this one */
133642551e99SRandall Stewart 		(*it->function_assoc) (it->inp, it->stcb, it->pointer, it->val);
133742551e99SRandall Stewart 
133842551e99SRandall Stewart 		/*
133942551e99SRandall Stewart 		 * we lie here, it really needs to have its own type but
134042551e99SRandall Stewart 		 * first I must verify that this won't effect things :-0
134142551e99SRandall Stewart 		 */
134242551e99SRandall Stewart 		if (it->no_chunk_output == 0)
1343ceaad40aSRandall Stewart 			sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
134442551e99SRandall Stewart 
134542551e99SRandall Stewart 		SCTP_TCB_UNLOCK(it->stcb);
134642551e99SRandall Stewart next_assoc:
134742551e99SRandall Stewart 		it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
134842551e99SRandall Stewart 		if (it->stcb == NULL) {
134942551e99SRandall Stewart 			/* Run last function */
135042551e99SRandall Stewart 			if (it->function_inp_end != NULL) {
135142551e99SRandall Stewart 				inp_skip = (*it->function_inp_end) (it->inp,
135242551e99SRandall Stewart 				    it->pointer,
135342551e99SRandall Stewart 				    it->val);
135442551e99SRandall Stewart 			}
135542551e99SRandall Stewart 		}
135642551e99SRandall Stewart 	}
135742551e99SRandall Stewart 	SCTP_INP_RUNLOCK(it->inp);
135842551e99SRandall Stewart no_stcb:
135942551e99SRandall Stewart 	/* done with all assocs on this endpoint, move on to next endpoint */
136042551e99SRandall Stewart 	it->done_current_ep = 0;
136142551e99SRandall Stewart 	if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
136242551e99SRandall Stewart 		it->inp = NULL;
136342551e99SRandall Stewart 	} else {
136442551e99SRandall Stewart 		it->inp = LIST_NEXT(it->inp, sctp_list);
136542551e99SRandall Stewart 	}
136642551e99SRandall Stewart 	if (it->inp == NULL) {
136742551e99SRandall Stewart 		goto done_with_iterator;
136842551e99SRandall Stewart 	}
136942551e99SRandall Stewart 	goto select_a_new_ep;
137042551e99SRandall Stewart }
137142551e99SRandall Stewart 
137242551e99SRandall Stewart void
137342551e99SRandall Stewart sctp_iterator_worker(void)
137442551e99SRandall Stewart {
13754a9ef3f8SMichael Tuexen 	struct sctp_iterator *it, *nit;
137642551e99SRandall Stewart 
137742551e99SRandall Stewart 	/* This function is called with the WQ lock in place */
137842551e99SRandall Stewart 
1379f7517433SRandall Stewart 	sctp_it_ctl.iterator_running = 1;
13804a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
13814a9ef3f8SMichael Tuexen 		sctp_it_ctl.cur_it = it;
138242551e99SRandall Stewart 		/* now lets work on this one */
1383f7517433SRandall Stewart 		TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
138442551e99SRandall Stewart 		SCTP_IPI_ITERATOR_WQ_UNLOCK();
1385f7517433SRandall Stewart 		CURVNET_SET(it->vn);
138642551e99SRandall Stewart 		sctp_iterator_work(it);
1387f79aab18SRandall Stewart 		sctp_it_ctl.cur_it = NULL;
1388f7517433SRandall Stewart 		CURVNET_RESTORE();
138942551e99SRandall Stewart 		SCTP_IPI_ITERATOR_WQ_LOCK();
13903c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
139142551e99SRandall Stewart 	}
1392f7517433SRandall Stewart 	sctp_it_ctl.iterator_running = 0;
139342551e99SRandall Stewart 	return;
139442551e99SRandall Stewart }
139542551e99SRandall Stewart 
1396f8829a4aSRandall Stewart 
1397f8829a4aSRandall Stewart static void
1398f8829a4aSRandall Stewart sctp_handle_addr_wq(void)
1399f8829a4aSRandall Stewart {
1400f8829a4aSRandall Stewart 	/* deal with the ADDR wq from the rtsock calls */
14014a9ef3f8SMichael Tuexen 	struct sctp_laddr *wi, *nwi;
140242551e99SRandall Stewart 	struct sctp_asconf_iterator *asc;
1403f8829a4aSRandall Stewart 
140442551e99SRandall Stewart 	SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
1405207304d4SRandall Stewart 	    sizeof(struct sctp_asconf_iterator), SCTP_M_ASC_IT);
140642551e99SRandall Stewart 	if (asc == NULL) {
140742551e99SRandall Stewart 		/* Try later, no memory */
1408f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
1409f8829a4aSRandall Stewart 		    (struct sctp_inpcb *)NULL,
1410f8829a4aSRandall Stewart 		    (struct sctp_tcb *)NULL,
1411f8829a4aSRandall Stewart 		    (struct sctp_nets *)NULL);
141242551e99SRandall Stewart 		return;
1413f8829a4aSRandall Stewart 	}
141442551e99SRandall Stewart 	LIST_INIT(&asc->list_of_work);
141542551e99SRandall Stewart 	asc->cnt = 0;
1416f7517433SRandall Stewart 
1417f7517433SRandall Stewart 	SCTP_WQ_ADDR_LOCK();
14184a9ef3f8SMichael Tuexen 	LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
141942551e99SRandall Stewart 		LIST_REMOVE(wi, sctp_nxt_addr);
142042551e99SRandall Stewart 		LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
142142551e99SRandall Stewart 		asc->cnt++;
1422f8829a4aSRandall Stewart 	}
1423f7517433SRandall Stewart 	SCTP_WQ_ADDR_UNLOCK();
1424f7517433SRandall Stewart 
142542551e99SRandall Stewart 	if (asc->cnt == 0) {
1426207304d4SRandall Stewart 		SCTP_FREE(asc, SCTP_M_ASC_IT);
142742551e99SRandall Stewart 	} else {
14281b649582SRandall Stewart 		(void)sctp_initiate_iterator(sctp_asconf_iterator_ep,
14291b649582SRandall Stewart 		    sctp_asconf_iterator_stcb,
143042551e99SRandall Stewart 		    NULL,	/* No ep end for boundall */
143142551e99SRandall Stewart 		    SCTP_PCB_FLAGS_BOUNDALL,
143242551e99SRandall Stewart 		    SCTP_PCB_ANY_FEATURES,
14331b649582SRandall Stewart 		    SCTP_ASOC_ANY_STATE,
14341b649582SRandall Stewart 		    (void *)asc, 0,
14351b649582SRandall Stewart 		    sctp_asconf_iterator_end, NULL, 0);
143642551e99SRandall Stewart 	}
1437f8829a4aSRandall Stewart }
1438f8829a4aSRandall Stewart 
1439f8829a4aSRandall Stewart void
1440f8829a4aSRandall Stewart sctp_timeout_handler(void *t)
1441f8829a4aSRandall Stewart {
1442f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
1443f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
1444f8829a4aSRandall Stewart 	struct sctp_nets *net;
1445f8829a4aSRandall Stewart 	struct sctp_timer *tmr;
1446ceaad40aSRandall Stewart 
1447ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1448ceaad40aSRandall Stewart 	struct socket *so;
1449ceaad40aSRandall Stewart 
1450ceaad40aSRandall Stewart #endif
1451d61374e1SRandall Stewart 	int did_output, type;
1452f8829a4aSRandall Stewart 
1453f8829a4aSRandall Stewart 	tmr = (struct sctp_timer *)t;
1454f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)tmr->ep;
1455f8829a4aSRandall Stewart 	stcb = (struct sctp_tcb *)tmr->tcb;
1456f8829a4aSRandall Stewart 	net = (struct sctp_nets *)tmr->net;
14578518270eSMichael Tuexen 	CURVNET_SET((struct vnet *)tmr->vnet);
1458f8829a4aSRandall Stewart 	did_output = 1;
1459f8829a4aSRandall Stewart 
1460f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1461f8829a4aSRandall Stewart 	sctp_audit_log(0xF0, (uint8_t) tmr->type);
1462f8829a4aSRandall Stewart 	sctp_auditing(3, inp, stcb, net);
1463f8829a4aSRandall Stewart #endif
1464f8829a4aSRandall Stewart 
1465f8829a4aSRandall Stewart 	/* sanity checks... */
1466f8829a4aSRandall Stewart 	if (tmr->self != (void *)tmr) {
1467f8829a4aSRandall Stewart 		/*
1468ad81507eSRandall Stewart 		 * SCTP_PRINTF("Stale SCTP timer fired (%p), ignoring...\n",
1469dd294dceSMichael Tuexen 		 * (void *)tmr);
1470f8829a4aSRandall Stewart 		 */
14718518270eSMichael Tuexen 		CURVNET_RESTORE();
1472f8829a4aSRandall Stewart 		return;
1473f8829a4aSRandall Stewart 	}
1474a5d547adSRandall Stewart 	tmr->stopped_from = 0xa001;
1475f8829a4aSRandall Stewart 	if (!SCTP_IS_TIMER_TYPE_VALID(tmr->type)) {
1476f8829a4aSRandall Stewart 		/*
1477ad81507eSRandall Stewart 		 * SCTP_PRINTF("SCTP timer fired with invalid type: 0x%x\n",
1478f8829a4aSRandall Stewart 		 * tmr->type);
1479f8829a4aSRandall Stewart 		 */
14808518270eSMichael Tuexen 		CURVNET_RESTORE();
1481f8829a4aSRandall Stewart 		return;
1482f8829a4aSRandall Stewart 	}
1483a5d547adSRandall Stewart 	tmr->stopped_from = 0xa002;
1484f8829a4aSRandall Stewart 	if ((tmr->type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL)) {
14858518270eSMichael Tuexen 		CURVNET_RESTORE();
1486f8829a4aSRandall Stewart 		return;
1487f8829a4aSRandall Stewart 	}
1488f8829a4aSRandall Stewart 	/* if this is an iterator timeout, get the struct and clear inp */
1489a5d547adSRandall Stewart 	tmr->stopped_from = 0xa003;
1490d61374e1SRandall Stewart 	type = tmr->type;
1491f8829a4aSRandall Stewart 	if (inp) {
1492f8829a4aSRandall Stewart 		SCTP_INP_INCR_REF(inp);
1493aa1808b7SMichael Tuexen 		if ((inp->sctp_socket == NULL) &&
1494f8829a4aSRandall Stewart 		    ((tmr->type != SCTP_TIMER_TYPE_INPKILL) &&
1495810ec536SMichael Tuexen 		    (tmr->type != SCTP_TIMER_TYPE_INIT) &&
1496a1e13272SRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_SEND) &&
1497a1e13272SRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_RECV) &&
1498a1e13272SRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_HEARTBEAT) &&
1499f8829a4aSRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_SHUTDOWN) &&
1500f8829a4aSRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNACK) &&
1501f8829a4aSRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNGUARD) &&
1502f8829a4aSRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_ASOCKILL))
1503f8829a4aSRandall Stewart 		    ) {
1504f8829a4aSRandall Stewart 			SCTP_INP_DECR_REF(inp);
15058518270eSMichael Tuexen 			CURVNET_RESTORE();
1506f8829a4aSRandall Stewart 			return;
1507f8829a4aSRandall Stewart 		}
1508f8829a4aSRandall Stewart 	}
1509a5d547adSRandall Stewart 	tmr->stopped_from = 0xa004;
1510f8829a4aSRandall Stewart 	if (stcb) {
1511c105859eSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
1512f8829a4aSRandall Stewart 		if (stcb->asoc.state == 0) {
1513c105859eSRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, -1);
1514f8829a4aSRandall Stewart 			if (inp) {
1515f8829a4aSRandall Stewart 				SCTP_INP_DECR_REF(inp);
1516f8829a4aSRandall Stewart 			}
15178518270eSMichael Tuexen 			CURVNET_RESTORE();
1518f8829a4aSRandall Stewart 			return;
1519f8829a4aSRandall Stewart 		}
1520f8829a4aSRandall Stewart 	}
1521a5d547adSRandall Stewart 	tmr->stopped_from = 0xa005;
1522ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_TIMER1, "Timer type %d goes off\n", tmr->type);
1523139bc87fSRandall Stewart 	if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1524f8829a4aSRandall Stewart 		if (inp) {
1525f8829a4aSRandall Stewart 			SCTP_INP_DECR_REF(inp);
1526f8829a4aSRandall Stewart 		}
1527207304d4SRandall Stewart 		if (stcb) {
1528207304d4SRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, -1);
1529207304d4SRandall Stewart 		}
15308518270eSMichael Tuexen 		CURVNET_RESTORE();
1531f8829a4aSRandall Stewart 		return;
1532f8829a4aSRandall Stewart 	}
1533a5d547adSRandall Stewart 	tmr->stopped_from = 0xa006;
1534a5d547adSRandall Stewart 
1535f8829a4aSRandall Stewart 	if (stcb) {
1536f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
153750cec919SRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, -1);
1538b54d3a6cSRandall Stewart 		if ((tmr->type != SCTP_TIMER_TYPE_ASOCKILL) &&
1539b54d3a6cSRandall Stewart 		    ((stcb->asoc.state == 0) ||
1540b54d3a6cSRandall Stewart 		    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED))) {
1541b54d3a6cSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
1542b54d3a6cSRandall Stewart 			if (inp) {
1543b54d3a6cSRandall Stewart 				SCTP_INP_DECR_REF(inp);
1544b54d3a6cSRandall Stewart 			}
15458518270eSMichael Tuexen 			CURVNET_RESTORE();
1546b54d3a6cSRandall Stewart 			return;
1547b54d3a6cSRandall Stewart 		}
1548f8829a4aSRandall Stewart 	}
154944b7479bSRandall Stewart 	/* record in stopped what t-o occured */
155044b7479bSRandall Stewart 	tmr->stopped_from = tmr->type;
155144b7479bSRandall Stewart 
1552f8829a4aSRandall Stewart 	/* mark as being serviced now */
155344b7479bSRandall Stewart 	if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
155444b7479bSRandall Stewart 		/*
155544b7479bSRandall Stewart 		 * Callout has been rescheduled.
155644b7479bSRandall Stewart 		 */
155744b7479bSRandall Stewart 		goto get_out;
155844b7479bSRandall Stewart 	}
155944b7479bSRandall Stewart 	if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
156044b7479bSRandall Stewart 		/*
156144b7479bSRandall Stewart 		 * Not active, so no action.
156244b7479bSRandall Stewart 		 */
156344b7479bSRandall Stewart 		goto get_out;
156444b7479bSRandall Stewart 	}
1565139bc87fSRandall Stewart 	SCTP_OS_TIMER_DEACTIVATE(&tmr->timer);
1566f8829a4aSRandall Stewart 
1567f8829a4aSRandall Stewart 	/* call the handler for the appropriate timer type */
1568f8829a4aSRandall Stewart 	switch (tmr->type) {
1569d61a0ae0SRandall Stewart 	case SCTP_TIMER_TYPE_ZERO_COPY:
1570eacc51c5SRandall Stewart 		if (inp == NULL) {
1571eacc51c5SRandall Stewart 			break;
1572eacc51c5SRandall Stewart 		}
1573d61a0ae0SRandall Stewart 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
1574d61a0ae0SRandall Stewart 			SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
1575d61a0ae0SRandall Stewart 		}
1576d61a0ae0SRandall Stewart 		break;
1577ad21a364SRandall Stewart 	case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
1578eacc51c5SRandall Stewart 		if (inp == NULL) {
1579eacc51c5SRandall Stewart 			break;
1580eacc51c5SRandall Stewart 		}
1581ad21a364SRandall Stewart 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
1582ad21a364SRandall Stewart 			SCTP_ZERO_COPY_SENDQ_EVENT(inp, inp->sctp_socket);
1583ad21a364SRandall Stewart 		}
1584ad21a364SRandall Stewart 		break;
1585f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ADDR_WQ:
1586f8829a4aSRandall Stewart 		sctp_handle_addr_wq();
1587f8829a4aSRandall Stewart 		break;
1588f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SEND:
1589ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1590ad81507eSRandall Stewart 			break;
1591ad81507eSRandall Stewart 		}
1592f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timodata);
1593f42a358aSRandall Stewart 		stcb->asoc.timodata++;
1594f8829a4aSRandall Stewart 		stcb->asoc.num_send_timers_up--;
1595f8829a4aSRandall Stewart 		if (stcb->asoc.num_send_timers_up < 0) {
1596f8829a4aSRandall Stewart 			stcb->asoc.num_send_timers_up = 0;
1597f8829a4aSRandall Stewart 		}
1598b54d3a6cSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
159960990c0cSMichael Tuexen 		if (sctp_t3rxt_timer(inp, stcb, net)) {
1600f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1601f8829a4aSRandall Stewart 
1602f8829a4aSRandall Stewart 			goto out_decr;
1603f8829a4aSRandall Stewart 		}
1604b54d3a6cSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1605f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1606f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1607f8829a4aSRandall Stewart #endif
1608ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1609f8829a4aSRandall Stewart 		if ((stcb->asoc.num_send_timers_up == 0) &&
16104a9ef3f8SMichael Tuexen 		    (stcb->asoc.sent_queue_cnt > 0)) {
1611f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *chk;
1612f8829a4aSRandall Stewart 
1613f8829a4aSRandall Stewart 			/*
1614f8829a4aSRandall Stewart 			 * safeguard. If there on some on the sent queue
1615f8829a4aSRandall Stewart 			 * somewhere but no timers running something is
1616f8829a4aSRandall Stewart 			 * wrong... so we start a timer on the first chunk
1617f8829a4aSRandall Stewart 			 * on the send queue on whatever net it is sent to.
1618f8829a4aSRandall Stewart 			 */
1619f8829a4aSRandall Stewart 			chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
1620f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb,
1621f8829a4aSRandall Stewart 			    chk->whoTo);
1622f8829a4aSRandall Stewart 		}
1623f8829a4aSRandall Stewart 		break;
1624f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
1625ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1626ad81507eSRandall Stewart 			break;
1627ad81507eSRandall Stewart 		}
1628f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoinit);
1629f42a358aSRandall Stewart 		stcb->asoc.timoinit++;
1630f8829a4aSRandall Stewart 		if (sctp_t1init_timer(inp, stcb, net)) {
1631f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1632f8829a4aSRandall Stewart 			goto out_decr;
1633f8829a4aSRandall Stewart 		}
1634f8829a4aSRandall Stewart 		/* We do output but not here */
1635f8829a4aSRandall Stewart 		did_output = 0;
1636f8829a4aSRandall Stewart 		break;
1637f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
1638ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1639ad81507eSRandall Stewart 			break;
1640ca85e948SMichael Tuexen 		}
1641f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timosack);
1642f42a358aSRandall Stewart 		stcb->asoc.timosack++;
1643689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
1644f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1645f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1646f8829a4aSRandall Stewart #endif
1647ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SACK_TMR, SCTP_SO_NOT_LOCKED);
1648f8829a4aSRandall Stewart 		break;
1649f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
1650ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1651ad81507eSRandall Stewart 			break;
1652ad81507eSRandall Stewart 		}
1653f8829a4aSRandall Stewart 		if (sctp_shutdown_timer(inp, stcb, net)) {
1654f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1655f8829a4aSRandall Stewart 			goto out_decr;
1656f8829a4aSRandall Stewart 		}
1657f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoshutdown);
1658f42a358aSRandall Stewart 		stcb->asoc.timoshutdown++;
1659f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1660f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1661f8829a4aSRandall Stewart #endif
1662ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_NOT_LOCKED);
1663f8829a4aSRandall Stewart 		break;
1664f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
1665ca85e948SMichael Tuexen 		if ((stcb == NULL) || (inp == NULL) || (net == NULL)) {
1666ad81507eSRandall Stewart 			break;
1667ad81507eSRandall Stewart 		}
1668f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoheartbeat);
1669f42a358aSRandall Stewart 		stcb->asoc.timoheartbeat++;
1670ca85e948SMichael Tuexen 		if (sctp_heartbeat_timer(inp, stcb, net)) {
1671f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1672f8829a4aSRandall Stewart 			goto out_decr;
1673f8829a4aSRandall Stewart 		}
1674f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1675ca85e948SMichael Tuexen 		sctp_auditing(4, inp, stcb, net);
1676f8829a4aSRandall Stewart #endif
1677ca85e948SMichael Tuexen 		if (!(net->dest_state & SCTP_ADDR_NOHB)) {
1678629749b6SMichael Tuexen 			sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1679ceaad40aSRandall Stewart 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_HB_TMR, SCTP_SO_NOT_LOCKED);
1680f8829a4aSRandall Stewart 		}
1681f8829a4aSRandall Stewart 		break;
1682f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
1683ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1684ad81507eSRandall Stewart 			break;
1685ad81507eSRandall Stewart 		}
1686f8829a4aSRandall Stewart 		if (sctp_cookie_timer(inp, stcb, net)) {
1687f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1688f8829a4aSRandall Stewart 			goto out_decr;
1689f8829a4aSRandall Stewart 		}
1690f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timocookie);
1691f42a358aSRandall Stewart 		stcb->asoc.timocookie++;
1692f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1693f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1694f8829a4aSRandall Stewart #endif
1695f8829a4aSRandall Stewart 		/*
1696f8829a4aSRandall Stewart 		 * We consider T3 and Cookie timer pretty much the same with
1697f8829a4aSRandall Stewart 		 * respect to where from in chunk_output.
1698f8829a4aSRandall Stewart 		 */
1699ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1700f8829a4aSRandall Stewart 		break;
1701f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_NEWCOOKIE:
1702f8829a4aSRandall Stewart 		{
1703f8829a4aSRandall Stewart 			struct timeval tv;
1704f8829a4aSRandall Stewart 			int i, secret;
1705f8829a4aSRandall Stewart 
1706ad81507eSRandall Stewart 			if (inp == NULL) {
1707ad81507eSRandall Stewart 				break;
1708ad81507eSRandall Stewart 			}
1709f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_timosecret);
17106e55db54SRandall Stewart 			(void)SCTP_GETTIME_TIMEVAL(&tv);
1711f8829a4aSRandall Stewart 			SCTP_INP_WLOCK(inp);
1712f8829a4aSRandall Stewart 			inp->sctp_ep.time_of_secret_change = tv.tv_sec;
1713f8829a4aSRandall Stewart 			inp->sctp_ep.last_secret_number =
1714f8829a4aSRandall Stewart 			    inp->sctp_ep.current_secret_number;
1715f8829a4aSRandall Stewart 			inp->sctp_ep.current_secret_number++;
1716f8829a4aSRandall Stewart 			if (inp->sctp_ep.current_secret_number >=
1717f8829a4aSRandall Stewart 			    SCTP_HOW_MANY_SECRETS) {
1718f8829a4aSRandall Stewart 				inp->sctp_ep.current_secret_number = 0;
1719f8829a4aSRandall Stewart 			}
1720f8829a4aSRandall Stewart 			secret = (int)inp->sctp_ep.current_secret_number;
1721f8829a4aSRandall Stewart 			for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1722f8829a4aSRandall Stewart 				inp->sctp_ep.secret_key[secret][i] =
1723f8829a4aSRandall Stewart 				    sctp_select_initial_TSN(&inp->sctp_ep);
1724f8829a4aSRandall Stewart 			}
1725f8829a4aSRandall Stewart 			SCTP_INP_WUNLOCK(inp);
1726f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, stcb, net);
1727f8829a4aSRandall Stewart 		}
1728f8829a4aSRandall Stewart 		did_output = 0;
1729f8829a4aSRandall Stewart 		break;
1730f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
1731ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1732ad81507eSRandall Stewart 			break;
1733ad81507eSRandall Stewart 		}
1734f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timopathmtu);
1735f8829a4aSRandall Stewart 		sctp_pathmtu_timer(inp, stcb, net);
1736f8829a4aSRandall Stewart 		did_output = 0;
1737f8829a4aSRandall Stewart 		break;
1738f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
1739ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1740ad81507eSRandall Stewart 			break;
1741ad81507eSRandall Stewart 		}
1742f8829a4aSRandall Stewart 		if (sctp_shutdownack_timer(inp, stcb, net)) {
1743f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1744f8829a4aSRandall Stewart 			goto out_decr;
1745f8829a4aSRandall Stewart 		}
1746f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoshutdownack);
1747f42a358aSRandall Stewart 		stcb->asoc.timoshutdownack++;
1748f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1749f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1750f8829a4aSRandall Stewart #endif
1751ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_ACK_TMR, SCTP_SO_NOT_LOCKED);
1752f8829a4aSRandall Stewart 		break;
1753f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
1754ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1755ad81507eSRandall Stewart 			break;
1756ad81507eSRandall Stewart 		}
1757f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoshutdownguard);
1758a2b42326SMichael Tuexen 		sctp_abort_an_association(inp, stcb, NULL, SCTP_SO_NOT_LOCKED);
1759f8829a4aSRandall Stewart 		/* no need to unlock on tcb its gone */
1760f8829a4aSRandall Stewart 		goto out_decr;
1761f8829a4aSRandall Stewart 
1762f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_STRRESET:
1763ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1764ad81507eSRandall Stewart 			break;
1765ad81507eSRandall Stewart 		}
1766f8829a4aSRandall Stewart 		if (sctp_strreset_timer(inp, stcb, net)) {
1767f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1768f8829a4aSRandall Stewart 			goto out_decr;
1769f8829a4aSRandall Stewart 		}
1770f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timostrmrst);
1771ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_TMR, SCTP_SO_NOT_LOCKED);
1772f8829a4aSRandall Stewart 		break;
1773f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASCONF:
1774ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1775ad81507eSRandall Stewart 			break;
1776ad81507eSRandall Stewart 		}
1777f8829a4aSRandall Stewart 		if (sctp_asconf_timer(inp, stcb, net)) {
1778f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1779f8829a4aSRandall Stewart 			goto out_decr;
1780f8829a4aSRandall Stewart 		}
1781f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoasconf);
1782f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1783f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1784f8829a4aSRandall Stewart #endif
1785ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_ASCONF_TMR, SCTP_SO_NOT_LOCKED);
1786f8829a4aSRandall Stewart 		break;
1787851b7298SRandall Stewart 	case SCTP_TIMER_TYPE_PRIM_DELETED:
1788851b7298SRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1789851b7298SRandall Stewart 			break;
1790851b7298SRandall Stewart 		}
179104ee05e8SRandall Stewart 		sctp_delete_prim_timer(inp, stcb, net);
1792851b7298SRandall Stewart 		SCTP_STAT_INCR(sctps_timodelprim);
1793851b7298SRandall Stewart 		break;
1794f8829a4aSRandall Stewart 
1795f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_AUTOCLOSE:
1796ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1797ad81507eSRandall Stewart 			break;
1798ad81507eSRandall Stewart 		}
1799f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoautoclose);
1800f8829a4aSRandall Stewart 		sctp_autoclose_timer(inp, stcb, net);
1801ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
1802f8829a4aSRandall Stewart 		did_output = 0;
1803f8829a4aSRandall Stewart 		break;
1804f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASOCKILL:
1805ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1806ad81507eSRandall Stewart 			break;
1807ad81507eSRandall Stewart 		}
1808f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoassockill);
1809f8829a4aSRandall Stewart 		/* Can we free it yet? */
1810f8829a4aSRandall Stewart 		SCTP_INP_DECR_REF(inp);
1811a5d547adSRandall Stewart 		sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_1);
1812ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1813ceaad40aSRandall Stewart 		so = SCTP_INP_SO(inp);
1814ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
1815ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
1816ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
1817ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
1818ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
1819ceaad40aSRandall Stewart #endif
1820c4739e2fSRandall Stewart 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_2);
1821ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1822ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
1823ceaad40aSRandall Stewart #endif
1824f8829a4aSRandall Stewart 		/*
1825f8829a4aSRandall Stewart 		 * free asoc, always unlocks (or destroy's) so prevent
1826f8829a4aSRandall Stewart 		 * duplicate unlock or unlock of a free mtx :-0
1827f8829a4aSRandall Stewart 		 */
1828f8829a4aSRandall Stewart 		stcb = NULL;
1829f8829a4aSRandall Stewart 		goto out_no_decr;
1830f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INPKILL:
1831f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoinpkill);
1832ad81507eSRandall Stewart 		if (inp == NULL) {
1833ad81507eSRandall Stewart 			break;
1834ad81507eSRandall Stewart 		}
1835f8829a4aSRandall Stewart 		/*
1836f8829a4aSRandall Stewart 		 * special case, take away our increment since WE are the
1837f8829a4aSRandall Stewart 		 * killer
1838f8829a4aSRandall Stewart 		 */
1839f8829a4aSRandall Stewart 		SCTP_INP_DECR_REF(inp);
1840a5d547adSRandall Stewart 		sctp_timer_stop(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_3);
1841b0552ae2SRandall Stewart 		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
18420c7dc840SRandall Stewart 		    SCTP_CALLED_FROM_INPKILL_TIMER);
1843d61374e1SRandall Stewart 		inp = NULL;
1844f8829a4aSRandall Stewart 		goto out_no_decr;
1845f8829a4aSRandall Stewart 	default:
1846ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER1, "sctp_timeout_handler:unknown timer %d\n",
1847f8829a4aSRandall Stewart 		    tmr->type);
1848f8829a4aSRandall Stewart 		break;
184960990c0cSMichael Tuexen 	}
1850f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1851f8829a4aSRandall Stewart 	sctp_audit_log(0xF1, (uint8_t) tmr->type);
1852f8829a4aSRandall Stewart 	if (inp)
1853f8829a4aSRandall Stewart 		sctp_auditing(5, inp, stcb, net);
1854f8829a4aSRandall Stewart #endif
1855f8829a4aSRandall Stewart 	if ((did_output) && stcb) {
1856f8829a4aSRandall Stewart 		/*
1857f8829a4aSRandall Stewart 		 * Now we need to clean up the control chunk chain if an
1858f8829a4aSRandall Stewart 		 * ECNE is on it. It must be marked as UNSENT again so next
1859f8829a4aSRandall Stewart 		 * call will continue to send it until such time that we get
1860f8829a4aSRandall Stewart 		 * a CWR, to remove it. It is, however, less likely that we
1861f8829a4aSRandall Stewart 		 * will find a ecn echo on the chain though.
1862f8829a4aSRandall Stewart 		 */
1863f8829a4aSRandall Stewart 		sctp_fix_ecn_echo(&stcb->asoc);
1864f8829a4aSRandall Stewart 	}
186544b7479bSRandall Stewart get_out:
1866f8829a4aSRandall Stewart 	if (stcb) {
1867f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
1868f8829a4aSRandall Stewart 	}
1869f8829a4aSRandall Stewart out_decr:
1870f8829a4aSRandall Stewart 	if (inp) {
1871f8829a4aSRandall Stewart 		SCTP_INP_DECR_REF(inp);
1872f8829a4aSRandall Stewart 	}
1873f8829a4aSRandall Stewart out_no_decr:
1874ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_TIMER1, "Timer now complete (type %d)\n",
1875d61374e1SRandall Stewart 	    type);
18768518270eSMichael Tuexen 	CURVNET_RESTORE();
1877f8829a4aSRandall Stewart }
1878f8829a4aSRandall Stewart 
1879ad81507eSRandall Stewart void
1880f8829a4aSRandall Stewart sctp_timer_start(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1881f8829a4aSRandall Stewart     struct sctp_nets *net)
1882f8829a4aSRandall Stewart {
1883ca85e948SMichael Tuexen 	uint32_t to_ticks;
1884f8829a4aSRandall Stewart 	struct sctp_timer *tmr;
1885f8829a4aSRandall Stewart 
1886139bc87fSRandall Stewart 	if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL))
1887ad81507eSRandall Stewart 		return;
1888f8829a4aSRandall Stewart 
1889f8829a4aSRandall Stewart 	tmr = NULL;
1890f8829a4aSRandall Stewart 	if (stcb) {
1891f8829a4aSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1892f8829a4aSRandall Stewart 	}
1893f8829a4aSRandall Stewart 	switch (t_type) {
1894d61a0ae0SRandall Stewart 	case SCTP_TIMER_TYPE_ZERO_COPY:
1895d61a0ae0SRandall Stewart 		tmr = &inp->sctp_ep.zero_copy_timer;
1896d61a0ae0SRandall Stewart 		to_ticks = SCTP_ZERO_COPY_TICK_DELAY;
1897d61a0ae0SRandall Stewart 		break;
1898ad21a364SRandall Stewart 	case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
1899ad21a364SRandall Stewart 		tmr = &inp->sctp_ep.zero_copy_sendq_timer;
1900ad21a364SRandall Stewart 		to_ticks = SCTP_ZERO_COPY_SENDQ_TICK_DELAY;
1901ad21a364SRandall Stewart 		break;
1902f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ADDR_WQ:
1903f8829a4aSRandall Stewart 		/* Only 1 tick away :-) */
1904b3f1ea41SRandall Stewart 		tmr = &SCTP_BASE_INFO(addr_wq_timer);
190542551e99SRandall Stewart 		to_ticks = SCTP_ADDRESS_TICK_DELAY;
1906f8829a4aSRandall Stewart 		break;
1907f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SEND:
1908f8829a4aSRandall Stewart 		/* Here we use the RTO timer */
1909f8829a4aSRandall Stewart 		{
1910f8829a4aSRandall Stewart 			int rto_val;
1911f8829a4aSRandall Stewart 
1912f8829a4aSRandall Stewart 			if ((stcb == NULL) || (net == NULL)) {
1913ad81507eSRandall Stewart 				return;
1914f8829a4aSRandall Stewart 			}
1915f8829a4aSRandall Stewart 			tmr = &net->rxt_timer;
1916f8829a4aSRandall Stewart 			if (net->RTO == 0) {
1917f8829a4aSRandall Stewart 				rto_val = stcb->asoc.initial_rto;
1918f8829a4aSRandall Stewart 			} else {
1919f8829a4aSRandall Stewart 				rto_val = net->RTO;
1920f8829a4aSRandall Stewart 			}
1921f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(rto_val);
1922f8829a4aSRandall Stewart 		}
1923f8829a4aSRandall Stewart 		break;
1924f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
1925f8829a4aSRandall Stewart 		/*
1926f8829a4aSRandall Stewart 		 * Here we use the INIT timer default usually about 1
1927f8829a4aSRandall Stewart 		 * minute.
1928f8829a4aSRandall Stewart 		 */
1929f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
1930ad81507eSRandall Stewart 			return;
1931f8829a4aSRandall Stewart 		}
1932f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
1933f8829a4aSRandall Stewart 		if (net->RTO == 0) {
1934f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1935f8829a4aSRandall Stewart 		} else {
1936f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
1937f8829a4aSRandall Stewart 		}
1938f8829a4aSRandall Stewart 		break;
1939f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
1940f8829a4aSRandall Stewart 		/*
1941f8829a4aSRandall Stewart 		 * Here we use the Delayed-Ack timer value from the inp
1942f8829a4aSRandall Stewart 		 * ususually about 200ms.
1943f8829a4aSRandall Stewart 		 */
1944f8829a4aSRandall Stewart 		if (stcb == NULL) {
1945ad81507eSRandall Stewart 			return;
1946f8829a4aSRandall Stewart 		}
1947f8829a4aSRandall Stewart 		tmr = &stcb->asoc.dack_timer;
1948f8829a4aSRandall Stewart 		to_ticks = MSEC_TO_TICKS(stcb->asoc.delayed_ack);
1949f8829a4aSRandall Stewart 		break;
1950f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
1951f8829a4aSRandall Stewart 		/* Here we use the RTO of the destination. */
1952f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
1953ad81507eSRandall Stewart 			return;
1954f8829a4aSRandall Stewart 		}
1955f8829a4aSRandall Stewart 		if (net->RTO == 0) {
1956f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1957f8829a4aSRandall Stewart 		} else {
1958f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
1959f8829a4aSRandall Stewart 		}
1960f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
1961f8829a4aSRandall Stewart 		break;
1962f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
1963f8829a4aSRandall Stewart 		/*
1964f8829a4aSRandall Stewart 		 * the net is used here so that we can add in the RTO. Even
1965f8829a4aSRandall Stewart 		 * though we use a different timer. We also add the HB timer
1966f8829a4aSRandall Stewart 		 * PLUS a random jitter.
1967f8829a4aSRandall Stewart 		 */
1968f3ba71beSMichael Tuexen 		if ((stcb == NULL) || (net == NULL)) {
1969ad81507eSRandall Stewart 			return;
1970ad81507eSRandall Stewart 		} else {
1971f8829a4aSRandall Stewart 			uint32_t rndval;
1972ca85e948SMichael Tuexen 			uint32_t jitter;
1973f8829a4aSRandall Stewart 
1974ca85e948SMichael Tuexen 			if ((net->dest_state & SCTP_ADDR_NOHB) &&
1975ca85e948SMichael Tuexen 			    !(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
1976ad81507eSRandall Stewart 				return;
1977f8829a4aSRandall Stewart 			}
1978f8829a4aSRandall Stewart 			if (net->RTO == 0) {
1979ca85e948SMichael Tuexen 				to_ticks = stcb->asoc.initial_rto;
1980f8829a4aSRandall Stewart 			} else {
1981ca85e948SMichael Tuexen 				to_ticks = net->RTO;
1982f8829a4aSRandall Stewart 			}
1983ca85e948SMichael Tuexen 			rndval = sctp_select_initial_TSN(&inp->sctp_ep);
1984ca85e948SMichael Tuexen 			jitter = rndval % to_ticks;
1985ca85e948SMichael Tuexen 			if (jitter >= (to_ticks >> 1)) {
1986ca85e948SMichael Tuexen 				to_ticks = to_ticks + (jitter - (to_ticks >> 1));
1987f8829a4aSRandall Stewart 			} else {
1988ca85e948SMichael Tuexen 				to_ticks = to_ticks - jitter;
1989f8829a4aSRandall Stewart 			}
1990ca85e948SMichael Tuexen 			if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
1991ca85e948SMichael Tuexen 			    !(net->dest_state & SCTP_ADDR_PF)) {
1992ca85e948SMichael Tuexen 				to_ticks += net->heart_beat_delay;
1993f8829a4aSRandall Stewart 			}
1994f8829a4aSRandall Stewart 			/*
1995f8829a4aSRandall Stewart 			 * Now we must convert the to_ticks that are now in
1996f8829a4aSRandall Stewart 			 * ms to ticks.
1997f8829a4aSRandall Stewart 			 */
1998f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(to_ticks);
1999ca85e948SMichael Tuexen 			tmr = &net->hb_timer;
2000f8829a4aSRandall Stewart 		}
2001f8829a4aSRandall Stewart 		break;
2002f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
2003f8829a4aSRandall Stewart 		/*
2004f8829a4aSRandall Stewart 		 * Here we can use the RTO timer from the network since one
2005f8829a4aSRandall Stewart 		 * RTT was compelete. If a retran happened then we will be
2006f8829a4aSRandall Stewart 		 * using the RTO initial value.
2007f8829a4aSRandall Stewart 		 */
2008f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
2009ad81507eSRandall Stewart 			return;
2010f8829a4aSRandall Stewart 		}
2011f8829a4aSRandall Stewart 		if (net->RTO == 0) {
2012f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2013f8829a4aSRandall Stewart 		} else {
2014f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
2015f8829a4aSRandall Stewart 		}
2016f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2017f8829a4aSRandall Stewart 		break;
2018f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_NEWCOOKIE:
2019f8829a4aSRandall Stewart 		/*
2020f8829a4aSRandall Stewart 		 * nothing needed but the endpoint here ususually about 60
2021f8829a4aSRandall Stewart 		 * minutes.
2022f8829a4aSRandall Stewart 		 */
2023f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
2024f8829a4aSRandall Stewart 		to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_SIGNATURE];
2025f8829a4aSRandall Stewart 		break;
2026f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASOCKILL:
2027f8829a4aSRandall Stewart 		if (stcb == NULL) {
2028ad81507eSRandall Stewart 			return;
2029f8829a4aSRandall Stewart 		}
2030f8829a4aSRandall Stewart 		tmr = &stcb->asoc.strreset_timer;
2031f8829a4aSRandall Stewart 		to_ticks = MSEC_TO_TICKS(SCTP_ASOC_KILL_TIMEOUT);
2032f8829a4aSRandall Stewart 		break;
2033f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INPKILL:
2034f8829a4aSRandall Stewart 		/*
2035f8829a4aSRandall Stewart 		 * The inp is setup to die. We re-use the signature_chage
2036f8829a4aSRandall Stewart 		 * timer since that has stopped and we are in the GONE
2037f8829a4aSRandall Stewart 		 * state.
2038f8829a4aSRandall Stewart 		 */
2039f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
2040f8829a4aSRandall Stewart 		to_ticks = MSEC_TO_TICKS(SCTP_INP_KILL_TIMEOUT);
2041f8829a4aSRandall Stewart 		break;
2042f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
2043f8829a4aSRandall Stewart 		/*
2044f8829a4aSRandall Stewart 		 * Here we use the value found in the EP for PMTU ususually
2045f8829a4aSRandall Stewart 		 * about 10 minutes.
2046f8829a4aSRandall Stewart 		 */
2047f3ba71beSMichael Tuexen 		if ((stcb == NULL) || (net == NULL)) {
2048ad81507eSRandall Stewart 			return;
2049f8829a4aSRandall Stewart 		}
205080c79bbeSMichael Tuexen 		if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
205180c79bbeSMichael Tuexen 			return;
205280c79bbeSMichael Tuexen 		}
2053f8829a4aSRandall Stewart 		to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_PMTU];
2054f8829a4aSRandall Stewart 		tmr = &net->pmtu_timer;
2055f8829a4aSRandall Stewart 		break;
2056f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
2057f8829a4aSRandall Stewart 		/* Here we use the RTO of the destination */
2058f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
2059ad81507eSRandall Stewart 			return;
2060f8829a4aSRandall Stewart 		}
2061f8829a4aSRandall Stewart 		if (net->RTO == 0) {
2062f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2063f8829a4aSRandall Stewart 		} else {
2064f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
2065f8829a4aSRandall Stewart 		}
2066f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2067f8829a4aSRandall Stewart 		break;
2068f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2069f8829a4aSRandall Stewart 		/*
2070f8829a4aSRandall Stewart 		 * Here we use the endpoints shutdown guard timer usually
2071f8829a4aSRandall Stewart 		 * about 3 minutes.
2072f8829a4aSRandall Stewart 		 */
2073f3ba71beSMichael Tuexen 		if (stcb == NULL) {
2074ad81507eSRandall Stewart 			return;
2075f8829a4aSRandall Stewart 		}
2076f8829a4aSRandall Stewart 		to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN];
2077f8829a4aSRandall Stewart 		tmr = &stcb->asoc.shut_guard_timer;
2078f8829a4aSRandall Stewart 		break;
2079f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_STRRESET:
2080f8829a4aSRandall Stewart 		/*
20811b649582SRandall Stewart 		 * Here the timer comes from the stcb but its value is from
20821b649582SRandall Stewart 		 * the net's RTO.
2083f8829a4aSRandall Stewart 		 */
2084f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
2085ad81507eSRandall Stewart 			return;
2086f8829a4aSRandall Stewart 		}
2087f8829a4aSRandall Stewart 		if (net->RTO == 0) {
2088f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2089f8829a4aSRandall Stewart 		} else {
2090f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
2091f8829a4aSRandall Stewart 		}
2092f8829a4aSRandall Stewart 		tmr = &stcb->asoc.strreset_timer;
2093f8829a4aSRandall Stewart 		break;
2094f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASCONF:
2095f8829a4aSRandall Stewart 		/*
20961b649582SRandall Stewart 		 * Here the timer comes from the stcb but its value is from
20971b649582SRandall Stewart 		 * the net's RTO.
2098f8829a4aSRandall Stewart 		 */
2099f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
2100ad81507eSRandall Stewart 			return;
2101f8829a4aSRandall Stewart 		}
2102f8829a4aSRandall Stewart 		if (net->RTO == 0) {
2103f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2104f8829a4aSRandall Stewart 		} else {
2105f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
2106f8829a4aSRandall Stewart 		}
2107f8829a4aSRandall Stewart 		tmr = &stcb->asoc.asconf_timer;
2108f8829a4aSRandall Stewart 		break;
2109851b7298SRandall Stewart 	case SCTP_TIMER_TYPE_PRIM_DELETED:
2110851b7298SRandall Stewart 		if ((stcb == NULL) || (net != NULL)) {
2111851b7298SRandall Stewart 			return;
2112851b7298SRandall Stewart 		}
2113851b7298SRandall Stewart 		to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2114851b7298SRandall Stewart 		tmr = &stcb->asoc.delete_prim_timer;
2115851b7298SRandall Stewart 		break;
2116f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_AUTOCLOSE:
2117f8829a4aSRandall Stewart 		if (stcb == NULL) {
2118ad81507eSRandall Stewart 			return;
2119f8829a4aSRandall Stewart 		}
2120f8829a4aSRandall Stewart 		if (stcb->asoc.sctp_autoclose_ticks == 0) {
2121f8829a4aSRandall Stewart 			/*
2122f8829a4aSRandall Stewart 			 * Really an error since stcb is NOT set to
2123f8829a4aSRandall Stewart 			 * autoclose
2124f8829a4aSRandall Stewart 			 */
2125ad81507eSRandall Stewart 			return;
2126f8829a4aSRandall Stewart 		}
2127f8829a4aSRandall Stewart 		to_ticks = stcb->asoc.sctp_autoclose_ticks;
2128f8829a4aSRandall Stewart 		tmr = &stcb->asoc.autoclose_timer;
2129f8829a4aSRandall Stewart 		break;
2130f8829a4aSRandall Stewart 	default:
2131ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2132ad81507eSRandall Stewart 		    __FUNCTION__, t_type);
2133ad81507eSRandall Stewart 		return;
2134f8829a4aSRandall Stewart 		break;
213560990c0cSMichael Tuexen 	}
2136f8829a4aSRandall Stewart 	if ((to_ticks <= 0) || (tmr == NULL)) {
2137ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER1, "%s: %d:software error to_ticks:%d tmr:%p not set ??\n",
2138dd294dceSMichael Tuexen 		    __FUNCTION__, t_type, to_ticks, (void *)tmr);
2139ad81507eSRandall Stewart 		return;
2140f8829a4aSRandall Stewart 	}
2141139bc87fSRandall Stewart 	if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
2142f8829a4aSRandall Stewart 		/*
2143f8829a4aSRandall Stewart 		 * we do NOT allow you to have it already running. if it is
2144f8829a4aSRandall Stewart 		 * we leave the current one up unchanged
2145f8829a4aSRandall Stewart 		 */
2146ad81507eSRandall Stewart 		return;
2147f8829a4aSRandall Stewart 	}
2148f8829a4aSRandall Stewart 	/* At this point we can proceed */
2149f8829a4aSRandall Stewart 	if (t_type == SCTP_TIMER_TYPE_SEND) {
2150f8829a4aSRandall Stewart 		stcb->asoc.num_send_timers_up++;
2151f8829a4aSRandall Stewart 	}
2152a5d547adSRandall Stewart 	tmr->stopped_from = 0;
2153f8829a4aSRandall Stewart 	tmr->type = t_type;
2154f8829a4aSRandall Stewart 	tmr->ep = (void *)inp;
2155f8829a4aSRandall Stewart 	tmr->tcb = (void *)stcb;
2156f8829a4aSRandall Stewart 	tmr->net = (void *)net;
2157f8829a4aSRandall Stewart 	tmr->self = (void *)tmr;
21588518270eSMichael Tuexen 	tmr->vnet = (void *)curvnet;
2159c4739e2fSRandall Stewart 	tmr->ticks = sctp_get_tick_count();
2160ad81507eSRandall Stewart 	(void)SCTP_OS_TIMER_START(&tmr->timer, to_ticks, sctp_timeout_handler, tmr);
2161ad81507eSRandall Stewart 	return;
2162f8829a4aSRandall Stewart }
2163f8829a4aSRandall Stewart 
21646e55db54SRandall Stewart void
2165f8829a4aSRandall Stewart sctp_timer_stop(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2166a5d547adSRandall Stewart     struct sctp_nets *net, uint32_t from)
2167f8829a4aSRandall Stewart {
2168f8829a4aSRandall Stewart 	struct sctp_timer *tmr;
2169f8829a4aSRandall Stewart 
2170f8829a4aSRandall Stewart 	if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) &&
2171f8829a4aSRandall Stewart 	    (inp == NULL))
21726e55db54SRandall Stewart 		return;
2173f8829a4aSRandall Stewart 
2174f8829a4aSRandall Stewart 	tmr = NULL;
2175f8829a4aSRandall Stewart 	if (stcb) {
2176f8829a4aSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
2177f8829a4aSRandall Stewart 	}
2178f8829a4aSRandall Stewart 	switch (t_type) {
2179d61a0ae0SRandall Stewart 	case SCTP_TIMER_TYPE_ZERO_COPY:
2180d61a0ae0SRandall Stewart 		tmr = &inp->sctp_ep.zero_copy_timer;
2181d61a0ae0SRandall Stewart 		break;
2182ad21a364SRandall Stewart 	case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
2183ad21a364SRandall Stewart 		tmr = &inp->sctp_ep.zero_copy_sendq_timer;
2184ad21a364SRandall Stewart 		break;
2185f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ADDR_WQ:
2186b3f1ea41SRandall Stewart 		tmr = &SCTP_BASE_INFO(addr_wq_timer);
2187f8829a4aSRandall Stewart 		break;
2188f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SEND:
2189f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
21906e55db54SRandall Stewart 			return;
2191f8829a4aSRandall Stewart 		}
2192f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2193f8829a4aSRandall Stewart 		break;
2194f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
2195f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
21966e55db54SRandall Stewart 			return;
2197f8829a4aSRandall Stewart 		}
2198f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2199f8829a4aSRandall Stewart 		break;
2200f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
2201f8829a4aSRandall Stewart 		if (stcb == NULL) {
22026e55db54SRandall Stewart 			return;
2203f8829a4aSRandall Stewart 		}
2204f8829a4aSRandall Stewart 		tmr = &stcb->asoc.dack_timer;
2205f8829a4aSRandall Stewart 		break;
2206f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
2207f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
22086e55db54SRandall Stewart 			return;
2209f8829a4aSRandall Stewart 		}
2210f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2211f8829a4aSRandall Stewart 		break;
2212f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
2213ca85e948SMichael Tuexen 		if ((stcb == NULL) || (net == NULL)) {
22146e55db54SRandall Stewart 			return;
2215f8829a4aSRandall Stewart 		}
2216ca85e948SMichael Tuexen 		tmr = &net->hb_timer;
2217f8829a4aSRandall Stewart 		break;
2218f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
2219f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
22206e55db54SRandall Stewart 			return;
2221f8829a4aSRandall Stewart 		}
2222f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2223f8829a4aSRandall Stewart 		break;
2224f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_NEWCOOKIE:
2225f8829a4aSRandall Stewart 		/* nothing needed but the endpoint here */
2226f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
2227f8829a4aSRandall Stewart 		/*
2228f8829a4aSRandall Stewart 		 * We re-use the newcookie timer for the INP kill timer. We
2229f8829a4aSRandall Stewart 		 * must assure that we do not kill it by accident.
2230f8829a4aSRandall Stewart 		 */
2231f8829a4aSRandall Stewart 		break;
2232f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASOCKILL:
2233f8829a4aSRandall Stewart 		/*
2234f8829a4aSRandall Stewart 		 * Stop the asoc kill timer.
2235f8829a4aSRandall Stewart 		 */
2236f8829a4aSRandall Stewart 		if (stcb == NULL) {
22376e55db54SRandall Stewart 			return;
2238f8829a4aSRandall Stewart 		}
2239f8829a4aSRandall Stewart 		tmr = &stcb->asoc.strreset_timer;
2240f8829a4aSRandall Stewart 		break;
2241f8829a4aSRandall Stewart 
2242f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INPKILL:
2243f8829a4aSRandall Stewart 		/*
2244f8829a4aSRandall Stewart 		 * The inp is setup to die. We re-use the signature_chage
2245f8829a4aSRandall Stewart 		 * timer since that has stopped and we are in the GONE
2246f8829a4aSRandall Stewart 		 * state.
2247f8829a4aSRandall Stewart 		 */
2248f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
2249f8829a4aSRandall Stewart 		break;
2250f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
2251f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
22526e55db54SRandall Stewart 			return;
2253f8829a4aSRandall Stewart 		}
2254f8829a4aSRandall Stewart 		tmr = &net->pmtu_timer;
2255f8829a4aSRandall Stewart 		break;
2256f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
2257f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
22586e55db54SRandall Stewart 			return;
2259f8829a4aSRandall Stewart 		}
2260f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2261f8829a4aSRandall Stewart 		break;
2262f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2263f8829a4aSRandall Stewart 		if (stcb == NULL) {
22646e55db54SRandall Stewart 			return;
2265f8829a4aSRandall Stewart 		}
2266f8829a4aSRandall Stewart 		tmr = &stcb->asoc.shut_guard_timer;
2267f8829a4aSRandall Stewart 		break;
2268f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_STRRESET:
2269f8829a4aSRandall Stewart 		if (stcb == NULL) {
22706e55db54SRandall Stewart 			return;
2271f8829a4aSRandall Stewart 		}
2272f8829a4aSRandall Stewart 		tmr = &stcb->asoc.strreset_timer;
2273f8829a4aSRandall Stewart 		break;
2274f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASCONF:
2275f8829a4aSRandall Stewart 		if (stcb == NULL) {
22766e55db54SRandall Stewart 			return;
2277f8829a4aSRandall Stewart 		}
2278f8829a4aSRandall Stewart 		tmr = &stcb->asoc.asconf_timer;
2279f8829a4aSRandall Stewart 		break;
2280851b7298SRandall Stewart 	case SCTP_TIMER_TYPE_PRIM_DELETED:
2281851b7298SRandall Stewart 		if (stcb == NULL) {
2282851b7298SRandall Stewart 			return;
2283851b7298SRandall Stewart 		}
2284851b7298SRandall Stewart 		tmr = &stcb->asoc.delete_prim_timer;
2285851b7298SRandall Stewart 		break;
2286f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_AUTOCLOSE:
2287f8829a4aSRandall Stewart 		if (stcb == NULL) {
22886e55db54SRandall Stewart 			return;
2289f8829a4aSRandall Stewart 		}
2290f8829a4aSRandall Stewart 		tmr = &stcb->asoc.autoclose_timer;
2291f8829a4aSRandall Stewart 		break;
2292f8829a4aSRandall Stewart 	default:
2293ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2294ad81507eSRandall Stewart 		    __FUNCTION__, t_type);
2295f8829a4aSRandall Stewart 		break;
229660990c0cSMichael Tuexen 	}
2297f8829a4aSRandall Stewart 	if (tmr == NULL) {
22986e55db54SRandall Stewart 		return;
2299f8829a4aSRandall Stewart 	}
2300f8829a4aSRandall Stewart 	if ((tmr->type != t_type) && tmr->type) {
2301f8829a4aSRandall Stewart 		/*
2302f8829a4aSRandall Stewart 		 * Ok we have a timer that is under joint use. Cookie timer
2303f8829a4aSRandall Stewart 		 * per chance with the SEND timer. We therefore are NOT
2304f8829a4aSRandall Stewart 		 * running the timer that the caller wants stopped.  So just
2305f8829a4aSRandall Stewart 		 * return.
2306f8829a4aSRandall Stewart 		 */
23076e55db54SRandall Stewart 		return;
2308f8829a4aSRandall Stewart 	}
2309ad81507eSRandall Stewart 	if ((t_type == SCTP_TIMER_TYPE_SEND) && (stcb != NULL)) {
2310f8829a4aSRandall Stewart 		stcb->asoc.num_send_timers_up--;
2311f8829a4aSRandall Stewart 		if (stcb->asoc.num_send_timers_up < 0) {
2312f8829a4aSRandall Stewart 			stcb->asoc.num_send_timers_up = 0;
2313f8829a4aSRandall Stewart 		}
2314f8829a4aSRandall Stewart 	}
2315f8829a4aSRandall Stewart 	tmr->self = NULL;
2316a5d547adSRandall Stewart 	tmr->stopped_from = from;
23176e55db54SRandall Stewart 	(void)SCTP_OS_TIMER_STOP(&tmr->timer);
23186e55db54SRandall Stewart 	return;
2319f8829a4aSRandall Stewart }
2320f8829a4aSRandall Stewart 
2321f8829a4aSRandall Stewart uint32_t
2322f8829a4aSRandall Stewart sctp_calculate_len(struct mbuf *m)
2323f8829a4aSRandall Stewart {
2324f8829a4aSRandall Stewart 	uint32_t tlen = 0;
2325f8829a4aSRandall Stewart 	struct mbuf *at;
2326f8829a4aSRandall Stewart 
2327f8829a4aSRandall Stewart 	at = m;
2328f8829a4aSRandall Stewart 	while (at) {
2329139bc87fSRandall Stewart 		tlen += SCTP_BUF_LEN(at);
2330139bc87fSRandall Stewart 		at = SCTP_BUF_NEXT(at);
2331f8829a4aSRandall Stewart 	}
2332f8829a4aSRandall Stewart 	return (tlen);
2333f8829a4aSRandall Stewart }
2334f8829a4aSRandall Stewart 
2335f8829a4aSRandall Stewart void
2336f8829a4aSRandall Stewart sctp_mtu_size_reset(struct sctp_inpcb *inp,
233744b7479bSRandall Stewart     struct sctp_association *asoc, uint32_t mtu)
2338f8829a4aSRandall Stewart {
2339f8829a4aSRandall Stewart 	/*
2340f8829a4aSRandall Stewart 	 * Reset the P-MTU size on this association, this involves changing
2341f8829a4aSRandall Stewart 	 * the asoc MTU, going through ANY chunk+overhead larger than mtu to
2342f8829a4aSRandall Stewart 	 * allow the DF flag to be cleared.
2343f8829a4aSRandall Stewart 	 */
2344f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
2345f8829a4aSRandall Stewart 	unsigned int eff_mtu, ovh;
2346f8829a4aSRandall Stewart 
2347f8829a4aSRandall Stewart 	asoc->smallest_mtu = mtu;
2348f8829a4aSRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2349f8829a4aSRandall Stewart 		ovh = SCTP_MIN_OVERHEAD;
2350f8829a4aSRandall Stewart 	} else {
2351f8829a4aSRandall Stewart 		ovh = SCTP_MIN_V4_OVERHEAD;
2352f8829a4aSRandall Stewart 	}
2353f8829a4aSRandall Stewart 	eff_mtu = mtu - ovh;
2354f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
2355f8829a4aSRandall Stewart 		if (chk->send_size > eff_mtu) {
2356f8829a4aSRandall Stewart 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2357f8829a4aSRandall Stewart 		}
2358f8829a4aSRandall Stewart 	}
2359f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
2360f8829a4aSRandall Stewart 		if (chk->send_size > eff_mtu) {
2361f8829a4aSRandall Stewart 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2362f8829a4aSRandall Stewart 		}
2363f8829a4aSRandall Stewart 	}
2364f8829a4aSRandall Stewart }
2365f8829a4aSRandall Stewart 
2366f8829a4aSRandall Stewart 
2367f8829a4aSRandall Stewart /*
2368f8829a4aSRandall Stewart  * given an association and starting time of the current RTT period return
2369f42a358aSRandall Stewart  * RTO in number of msecs net should point to the current network
2370f8829a4aSRandall Stewart  */
2371899288aeSRandall Stewart 
2372f8829a4aSRandall Stewart uint32_t
2373f8829a4aSRandall Stewart sctp_calculate_rto(struct sctp_tcb *stcb,
2374f8829a4aSRandall Stewart     struct sctp_association *asoc,
2375f8829a4aSRandall Stewart     struct sctp_nets *net,
237618e198d3SRandall Stewart     struct timeval *told,
2377f79aab18SRandall Stewart     int safe, int rtt_from_sack)
2378f8829a4aSRandall Stewart {
237918e198d3SRandall Stewart 	/*-
2380f8829a4aSRandall Stewart 	 * given an association and the starting time of the current RTT
2381f42a358aSRandall Stewart 	 * period (in value1/value2) return RTO in number of msecs.
2382f8829a4aSRandall Stewart 	 */
2383be1d9176SMichael Tuexen 	int32_t rtt;		/* RTT in ms */
2384be1d9176SMichael Tuexen 	uint32_t new_rto;
2385f8829a4aSRandall Stewart 	int first_measure = 0;
238618e198d3SRandall Stewart 	struct timeval now, then, *old;
2387f8829a4aSRandall Stewart 
238818e198d3SRandall Stewart 	/* Copy it out for sparc64 */
238918e198d3SRandall Stewart 	if (safe == sctp_align_unsafe_makecopy) {
239018e198d3SRandall Stewart 		old = &then;
239118e198d3SRandall Stewart 		memcpy(&then, told, sizeof(struct timeval));
239218e198d3SRandall Stewart 	} else if (safe == sctp_align_safe_nocopy) {
239318e198d3SRandall Stewart 		old = told;
239418e198d3SRandall Stewart 	} else {
239518e198d3SRandall Stewart 		/* error */
239618e198d3SRandall Stewart 		SCTP_PRINTF("Huh, bad rto calc call\n");
239718e198d3SRandall Stewart 		return (0);
239818e198d3SRandall Stewart 	}
2399f8829a4aSRandall Stewart 	/************************/
2400f8829a4aSRandall Stewart 	/* 1. calculate new RTT */
2401f8829a4aSRandall Stewart 	/************************/
2402f8829a4aSRandall Stewart 	/* get the current time */
2403299108c5SRandall Stewart 	if (stcb->asoc.use_precise_time) {
2404299108c5SRandall Stewart 		(void)SCTP_GETPTIME_TIMEVAL(&now);
2405299108c5SRandall Stewart 	} else {
24066e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
2407299108c5SRandall Stewart 	}
2408be1d9176SMichael Tuexen 	timevalsub(&now, old);
2409be1d9176SMichael Tuexen 	/* store the current RTT in us */
241081eb4e63SMichael Tuexen 	net->rtt = (uint64_t) 1000000 *(uint64_t) now.tv_sec +
2411be1d9176SMichael Tuexen 	        (uint64_t) now.tv_usec;
2412be1d9176SMichael Tuexen 
2413b60b0fe6SMichael Tuexen 	/* compute rtt in ms */
2414b60b0fe6SMichael Tuexen 	rtt = (int32_t) (net->rtt / 1000);
2415f79aab18SRandall Stewart 	if ((asoc->cc_functions.sctp_rtt_calculated) && (rtt_from_sack == SCTP_RTT_FROM_DATA)) {
2416f79aab18SRandall Stewart 		/*
2417f79aab18SRandall Stewart 		 * Tell the CC module that a new update has just occurred
2418f79aab18SRandall Stewart 		 * from a sack
2419f79aab18SRandall Stewart 		 */
2420f79aab18SRandall Stewart 		(*asoc->cc_functions.sctp_rtt_calculated) (stcb, net, &now);
2421f79aab18SRandall Stewart 	}
2422f79aab18SRandall Stewart 	/*
2423f79aab18SRandall Stewart 	 * Do we need to determine the lan? We do this only on sacks i.e.
2424f79aab18SRandall Stewart 	 * RTT being determined from data not non-data (HB/INIT->INITACK).
2425f79aab18SRandall Stewart 	 */
2426f79aab18SRandall Stewart 	if ((rtt_from_sack == SCTP_RTT_FROM_DATA) &&
2427be1d9176SMichael Tuexen 	    (net->lan_type == SCTP_LAN_UNKNOWN)) {
2428be1d9176SMichael Tuexen 		if (net->rtt > SCTP_LOCAL_LAN_RTT) {
2429899288aeSRandall Stewart 			net->lan_type = SCTP_LAN_INTERNET;
2430899288aeSRandall Stewart 		} else {
2431899288aeSRandall Stewart 			net->lan_type = SCTP_LAN_LOCAL;
2432899288aeSRandall Stewart 		}
2433899288aeSRandall Stewart 	}
2434f8829a4aSRandall Stewart 	/***************************/
2435f8829a4aSRandall Stewart 	/* 2. update RTTVAR & SRTT */
2436f8829a4aSRandall Stewart 	/***************************/
2437be1d9176SMichael Tuexen 	/*-
2438be1d9176SMichael Tuexen 	 * Compute the scaled average lastsa and the
2439be1d9176SMichael Tuexen 	 * scaled variance lastsv as described in van Jacobson
2440be1d9176SMichael Tuexen 	 * Paper "Congestion Avoidance and Control", Annex A.
2441be1d9176SMichael Tuexen 	 *
2442be1d9176SMichael Tuexen 	 * (net->lastsa >> SCTP_RTT_SHIFT) is the srtt
2443be1d9176SMichael Tuexen 	 * (net->lastsa >> SCTP_RTT_VAR_SHIFT) is the rttvar
2444be1d9176SMichael Tuexen 	 */
24459a972525SRandall Stewart 	if (net->RTO_measured) {
2446be1d9176SMichael Tuexen 		rtt -= (net->lastsa >> SCTP_RTT_SHIFT);
2447be1d9176SMichael Tuexen 		net->lastsa += rtt;
2448be1d9176SMichael Tuexen 		if (rtt < 0) {
2449be1d9176SMichael Tuexen 			rtt = -rtt;
2450be1d9176SMichael Tuexen 		}
2451be1d9176SMichael Tuexen 		rtt -= (net->lastsv >> SCTP_RTT_VAR_SHIFT);
2452be1d9176SMichael Tuexen 		net->lastsv += rtt;
2453b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2454f8829a4aSRandall Stewart 			rto_logging(net, SCTP_LOG_RTTVAR);
245580fefe0aSRandall Stewart 		}
2456f8829a4aSRandall Stewart 	} else {
2457f8829a4aSRandall Stewart 		/* First RTO measurment */
24589a972525SRandall Stewart 		net->RTO_measured = 1;
2459f8829a4aSRandall Stewart 		first_measure = 1;
2460be1d9176SMichael Tuexen 		net->lastsa = rtt << SCTP_RTT_SHIFT;
2461be1d9176SMichael Tuexen 		net->lastsv = (rtt / 2) << SCTP_RTT_VAR_SHIFT;
2462b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2463f8829a4aSRandall Stewart 			rto_logging(net, SCTP_LOG_INITIAL_RTT);
246480fefe0aSRandall Stewart 		}
2465f8829a4aSRandall Stewart 	}
2466be1d9176SMichael Tuexen 	if (net->lastsv == 0) {
2467be1d9176SMichael Tuexen 		net->lastsv = SCTP_CLOCK_GRANULARITY;
2468be1d9176SMichael Tuexen 	}
2469108df27cSRandall Stewart 	new_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
2470f8829a4aSRandall Stewart 	if ((new_rto > SCTP_SAT_NETWORK_MIN) &&
2471f8829a4aSRandall Stewart 	    (stcb->asoc.sat_network_lockout == 0)) {
2472f8829a4aSRandall Stewart 		stcb->asoc.sat_network = 1;
2473f8829a4aSRandall Stewart 	} else if ((!first_measure) && stcb->asoc.sat_network) {
2474f8829a4aSRandall Stewart 		stcb->asoc.sat_network = 0;
2475f8829a4aSRandall Stewart 		stcb->asoc.sat_network_lockout = 1;
2476f8829a4aSRandall Stewart 	}
2477f8829a4aSRandall Stewart 	/* bound it, per C6/C7 in Section 5.3.1 */
2478f8829a4aSRandall Stewart 	if (new_rto < stcb->asoc.minrto) {
2479f8829a4aSRandall Stewart 		new_rto = stcb->asoc.minrto;
2480f8829a4aSRandall Stewart 	}
2481f8829a4aSRandall Stewart 	if (new_rto > stcb->asoc.maxrto) {
2482f8829a4aSRandall Stewart 		new_rto = stcb->asoc.maxrto;
2483f8829a4aSRandall Stewart 	}
24845e54f665SRandall Stewart 	/* we are now returning the RTO */
24855e54f665SRandall Stewart 	return (new_rto);
2486f8829a4aSRandall Stewart }
2487f8829a4aSRandall Stewart 
2488f8829a4aSRandall Stewart /*
2489f8829a4aSRandall Stewart  * return a pointer to a contiguous piece of data from the given mbuf chain
2490f8829a4aSRandall Stewart  * starting at 'off' for 'len' bytes.  If the desired piece spans more than
2491f8829a4aSRandall Stewart  * one mbuf, a copy is made at 'ptr'. caller must ensure that the buffer size
2492f8829a4aSRandall Stewart  * is >= 'len' returns NULL if there there isn't 'len' bytes in the chain.
2493f8829a4aSRandall Stewart  */
249472fb6fdbSRandall Stewart caddr_t
2495f8829a4aSRandall Stewart sctp_m_getptr(struct mbuf *m, int off, int len, uint8_t * in_ptr)
2496f8829a4aSRandall Stewart {
2497f8829a4aSRandall Stewart 	uint32_t count;
2498f8829a4aSRandall Stewart 	uint8_t *ptr;
2499f8829a4aSRandall Stewart 
2500f8829a4aSRandall Stewart 	ptr = in_ptr;
2501f8829a4aSRandall Stewart 	if ((off < 0) || (len <= 0))
2502f8829a4aSRandall Stewart 		return (NULL);
2503f8829a4aSRandall Stewart 
2504f8829a4aSRandall Stewart 	/* find the desired start location */
2505f8829a4aSRandall Stewart 	while ((m != NULL) && (off > 0)) {
2506139bc87fSRandall Stewart 		if (off < SCTP_BUF_LEN(m))
2507f8829a4aSRandall Stewart 			break;
2508139bc87fSRandall Stewart 		off -= SCTP_BUF_LEN(m);
2509139bc87fSRandall Stewart 		m = SCTP_BUF_NEXT(m);
2510f8829a4aSRandall Stewart 	}
2511f8829a4aSRandall Stewart 	if (m == NULL)
2512f8829a4aSRandall Stewart 		return (NULL);
2513f8829a4aSRandall Stewart 
2514f8829a4aSRandall Stewart 	/* is the current mbuf large enough (eg. contiguous)? */
2515139bc87fSRandall Stewart 	if ((SCTP_BUF_LEN(m) - off) >= len) {
2516f8829a4aSRandall Stewart 		return (mtod(m, caddr_t)+off);
2517f8829a4aSRandall Stewart 	} else {
2518f8829a4aSRandall Stewart 		/* else, it spans more than one mbuf, so save a temp copy... */
2519f8829a4aSRandall Stewart 		while ((m != NULL) && (len > 0)) {
2520139bc87fSRandall Stewart 			count = min(SCTP_BUF_LEN(m) - off, len);
2521f8829a4aSRandall Stewart 			bcopy(mtod(m, caddr_t)+off, ptr, count);
2522f8829a4aSRandall Stewart 			len -= count;
2523f8829a4aSRandall Stewart 			ptr += count;
2524f8829a4aSRandall Stewart 			off = 0;
2525139bc87fSRandall Stewart 			m = SCTP_BUF_NEXT(m);
2526f8829a4aSRandall Stewart 		}
2527f8829a4aSRandall Stewart 		if ((m == NULL) && (len > 0))
2528f8829a4aSRandall Stewart 			return (NULL);
2529f8829a4aSRandall Stewart 		else
2530f8829a4aSRandall Stewart 			return ((caddr_t)in_ptr);
2531f8829a4aSRandall Stewart 	}
2532f8829a4aSRandall Stewart }
2533f8829a4aSRandall Stewart 
2534f8829a4aSRandall Stewart 
253544b7479bSRandall Stewart 
2536f8829a4aSRandall Stewart struct sctp_paramhdr *
2537f8829a4aSRandall Stewart sctp_get_next_param(struct mbuf *m,
2538f8829a4aSRandall Stewart     int offset,
2539f8829a4aSRandall Stewart     struct sctp_paramhdr *pull,
2540f8829a4aSRandall Stewart     int pull_limit)
2541f8829a4aSRandall Stewart {
2542f8829a4aSRandall Stewart 	/* This just provides a typed signature to Peter's Pull routine */
2543f8829a4aSRandall Stewart 	return ((struct sctp_paramhdr *)sctp_m_getptr(m, offset, pull_limit,
2544f8829a4aSRandall Stewart 	    (uint8_t *) pull));
2545f8829a4aSRandall Stewart }
2546f8829a4aSRandall Stewart 
2547f8829a4aSRandall Stewart 
2548ce11b842SMichael Tuexen struct mbuf *
2549f8829a4aSRandall Stewart sctp_add_pad_tombuf(struct mbuf *m, int padlen)
2550f8829a4aSRandall Stewart {
2551ce11b842SMichael Tuexen 	struct mbuf *m_last;
2552ce11b842SMichael Tuexen 	caddr_t dp;
2553f8829a4aSRandall Stewart 
2554f8829a4aSRandall Stewart 	if (padlen > 3) {
2555ce11b842SMichael Tuexen 		return (NULL);
2556f8829a4aSRandall Stewart 	}
255741eee555SRandall Stewart 	if (padlen <= M_TRAILINGSPACE(m)) {
2558f8829a4aSRandall Stewart 		/*
2559f8829a4aSRandall Stewart 		 * The easy way. We hope the majority of the time we hit
2560f8829a4aSRandall Stewart 		 * here :)
2561f8829a4aSRandall Stewart 		 */
2562ce11b842SMichael Tuexen 		m_last = m;
2563f8829a4aSRandall Stewart 	} else {
2564ce11b842SMichael Tuexen 		/* Hard way we must grow the mbuf chain */
2565ce11b842SMichael Tuexen 		m_last = sctp_get_mbuf_for_msg(padlen, 0, M_NOWAIT, 1, MT_DATA);
2566ce11b842SMichael Tuexen 		if (m_last == NULL) {
2567ce11b842SMichael Tuexen 			return (NULL);
2568f8829a4aSRandall Stewart 		}
2569ce11b842SMichael Tuexen 		SCTP_BUF_LEN(m_last) = 0;
2570ce11b842SMichael Tuexen 		SCTP_BUF_NEXT(m_last) = NULL;
2571ce11b842SMichael Tuexen 		SCTP_BUF_NEXT(m) = m_last;
2572f8829a4aSRandall Stewart 	}
2573ce11b842SMichael Tuexen 	dp = mtod(m_last, caddr_t)+SCTP_BUF_LEN(m_last);
2574ce11b842SMichael Tuexen 	SCTP_BUF_LEN(m_last) += padlen;
2575ce11b842SMichael Tuexen 	memset(dp, 0, padlen);
2576ce11b842SMichael Tuexen 	return (m_last);
2577f8829a4aSRandall Stewart }
2578f8829a4aSRandall Stewart 
2579ce11b842SMichael Tuexen struct mbuf *
2580f8829a4aSRandall Stewart sctp_pad_lastmbuf(struct mbuf *m, int padval, struct mbuf *last_mbuf)
2581f8829a4aSRandall Stewart {
2582f8829a4aSRandall Stewart 	/* find the last mbuf in chain and pad it */
2583f8829a4aSRandall Stewart 	struct mbuf *m_at;
2584f8829a4aSRandall Stewart 
2585ce11b842SMichael Tuexen 	if (last_mbuf != NULL) {
2586f8829a4aSRandall Stewart 		return (sctp_add_pad_tombuf(last_mbuf, padval));
2587f8829a4aSRandall Stewart 	} else {
258817267b32SMichael Tuexen 		for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
2589139bc87fSRandall Stewart 			if (SCTP_BUF_NEXT(m_at) == NULL) {
2590f8829a4aSRandall Stewart 				return (sctp_add_pad_tombuf(m_at, padval));
2591f8829a4aSRandall Stewart 			}
2592f8829a4aSRandall Stewart 		}
2593f8829a4aSRandall Stewart 	}
2594ce11b842SMichael Tuexen 	return (NULL);
2595f8829a4aSRandall Stewart }
2596f8829a4aSRandall Stewart 
2597f8829a4aSRandall Stewart static void
2598c5b5675dSMichael Tuexen sctp_notify_assoc_change(uint16_t state, struct sctp_tcb *stcb,
2599410a3b1eSMichael Tuexen     uint16_t error, struct sctp_abort_chunk *abort, uint8_t from_peer, int so_locked
2600ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2601ceaad40aSRandall Stewart     SCTP_UNUSED
2602ceaad40aSRandall Stewart #endif
2603ceaad40aSRandall Stewart )
2604f8829a4aSRandall Stewart {
2605f8829a4aSRandall Stewart 	struct mbuf *m_notify;
2606f8829a4aSRandall Stewart 	struct sctp_assoc_change *sac;
2607f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
2608a2b42326SMichael Tuexen 	size_t notif_len, abort_len;
2609e06b67c7SMichael Tuexen 	unsigned int i;
2610f8829a4aSRandall Stewart 
2611ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2612ceaad40aSRandall Stewart 	struct socket *so;
2613ceaad40aSRandall Stewart 
2614ceaad40aSRandall Stewart #endif
2615ceaad40aSRandall Stewart 
261658411b08SMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT)) {
2617a2b42326SMichael Tuexen 		notif_len = sizeof(struct sctp_assoc_change);
2618a2b42326SMichael Tuexen 		if (abort != NULL) {
2619c9eb4473SMichael Tuexen 			abort_len = ntohs(abort->ch.chunk_length);
2620a2b42326SMichael Tuexen 		} else {
2621a2b42326SMichael Tuexen 			abort_len = 0;
2622c5b5675dSMichael Tuexen 		}
2623a2b42326SMichael Tuexen 		if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
2624a2b42326SMichael Tuexen 			notif_len += SCTP_ASSOC_SUPPORTS_MAX;
2625a2b42326SMichael Tuexen 		} else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
2626a2b42326SMichael Tuexen 			notif_len += abort_len;
2627a2b42326SMichael Tuexen 		}
2628eb1b1807SGleb Smirnoff 		m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
2629a2b42326SMichael Tuexen 		if (m_notify == NULL) {
2630a2b42326SMichael Tuexen 			/* Retry with smaller value. */
2631a2b42326SMichael Tuexen 			notif_len = sizeof(struct sctp_assoc_change);
2632eb1b1807SGleb Smirnoff 			m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
2633a2b42326SMichael Tuexen 			if (m_notify == NULL) {
263458411b08SMichael Tuexen 				goto set_error;
2635a2b42326SMichael Tuexen 			}
2636a2b42326SMichael Tuexen 		}
2637a2b42326SMichael Tuexen 		SCTP_BUF_NEXT(m_notify) = NULL;
2638f8829a4aSRandall Stewart 		sac = mtod(m_notify, struct sctp_assoc_change *);
2639e432298aSXin LI 		memset(sac, 0, notif_len);
2640f8829a4aSRandall Stewart 		sac->sac_type = SCTP_ASSOC_CHANGE;
2641f8829a4aSRandall Stewart 		sac->sac_flags = 0;
2642f8829a4aSRandall Stewart 		sac->sac_length = sizeof(struct sctp_assoc_change);
2643c5b5675dSMichael Tuexen 		sac->sac_state = state;
2644f8829a4aSRandall Stewart 		sac->sac_error = error;
2645f8829a4aSRandall Stewart 		/* XXX verify these stream counts */
2646f8829a4aSRandall Stewart 		sac->sac_outbound_streams = stcb->asoc.streamoutcnt;
2647f8829a4aSRandall Stewart 		sac->sac_inbound_streams = stcb->asoc.streamincnt;
2648f8829a4aSRandall Stewart 		sac->sac_assoc_id = sctp_get_associd(stcb);
2649a2b42326SMichael Tuexen 		if (notif_len > sizeof(struct sctp_assoc_change)) {
2650c5b5675dSMichael Tuexen 			if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
2651e06b67c7SMichael Tuexen 				i = 0;
2652c79bec9cSMichael Tuexen 				if (stcb->asoc.prsctp_supported == 1) {
2653e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_PR;
2654e06b67c7SMichael Tuexen 				}
2655c79bec9cSMichael Tuexen 				if (stcb->asoc.auth_supported == 1) {
2656e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_AUTH;
2657e06b67c7SMichael Tuexen 				}
2658c79bec9cSMichael Tuexen 				if (stcb->asoc.asconf_supported == 1) {
2659e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_ASCONF;
2660e06b67c7SMichael Tuexen 				}
2661e06b67c7SMichael Tuexen 				sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_MULTIBUF;
2662c79bec9cSMichael Tuexen 				if (stcb->asoc.reconfig_supported == 1) {
2663e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_RE_CONFIG;
2664e06b67c7SMichael Tuexen 				}
2665e06b67c7SMichael Tuexen 				sac->sac_length += i;
2666a2b42326SMichael Tuexen 			} else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
2667a2b42326SMichael Tuexen 				memcpy(sac->sac_info, abort, abort_len);
2668a2b42326SMichael Tuexen 				sac->sac_length += abort_len;
2669a2b42326SMichael Tuexen 			}
2670c5b5675dSMichael Tuexen 		}
2671e06b67c7SMichael Tuexen 		SCTP_BUF_LEN(m_notify) = sac->sac_length;
2672f8829a4aSRandall Stewart 		control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
26737215cc1bSMichael Tuexen 		    0, 0, stcb->asoc.context, 0, 0, 0,
2674f8829a4aSRandall Stewart 		    m_notify);
267558411b08SMichael Tuexen 		if (control != NULL) {
2676139bc87fSRandall Stewart 			control->length = SCTP_BUF_LEN(m_notify);
2677f8829a4aSRandall Stewart 			/* not that we need this */
2678f8829a4aSRandall Stewart 			control->tail_mbuf = m_notify;
2679139bc87fSRandall Stewart 			control->spec_flags = M_NOTIFICATION;
2680f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep, stcb,
2681f8829a4aSRandall Stewart 			    control,
2682cfde3ff7SRandall Stewart 			    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD,
2683cfde3ff7SRandall Stewart 			    so_locked);
268458411b08SMichael Tuexen 		} else {
268558411b08SMichael Tuexen 			sctp_m_freem(m_notify);
268658411b08SMichael Tuexen 		}
268758411b08SMichael Tuexen 	}
268858411b08SMichael Tuexen 	/*
268958411b08SMichael Tuexen 	 * For 1-to-1 style sockets, we send up and error when an ABORT
269058411b08SMichael Tuexen 	 * comes in.
269158411b08SMichael Tuexen 	 */
269258411b08SMichael Tuexen set_error:
269358411b08SMichael Tuexen 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
269458411b08SMichael Tuexen 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
269558411b08SMichael Tuexen 	    ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
2696e045904fSMichael Tuexen 		SOCK_LOCK(stcb->sctp_socket);
2697410a3b1eSMichael Tuexen 		if (from_peer) {
269858411b08SMichael Tuexen 			if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
269958411b08SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNREFUSED);
270058411b08SMichael Tuexen 				stcb->sctp_socket->so_error = ECONNREFUSED;
270158411b08SMichael Tuexen 			} else {
270258411b08SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
270358411b08SMichael Tuexen 				stcb->sctp_socket->so_error = ECONNRESET;
270458411b08SMichael Tuexen 			}
2705410a3b1eSMichael Tuexen 		} else {
2706553bb068SMichael Tuexen 			if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) ||
2707553bb068SMichael Tuexen 			    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
2708553bb068SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ETIMEDOUT);
2709553bb068SMichael Tuexen 				stcb->sctp_socket->so_error = ETIMEDOUT;
2710553bb068SMichael Tuexen 			} else {
2711410a3b1eSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNABORTED);
2712410a3b1eSMichael Tuexen 				stcb->sctp_socket->so_error = ECONNABORTED;
2713410a3b1eSMichael Tuexen 			}
271458411b08SMichael Tuexen 		}
2715553bb068SMichael Tuexen 	}
271658411b08SMichael Tuexen 	/* Wake ANY sleepers */
2717ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2718ceaad40aSRandall Stewart 	so = SCTP_INP_SO(stcb->sctp_ep);
2719ceaad40aSRandall Stewart 	if (!so_locked) {
2720ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
2721ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
2722ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
2723ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
2724ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
2725ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2726ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
2727ceaad40aSRandall Stewart 			return;
2728ceaad40aSRandall Stewart 		}
2729ceaad40aSRandall Stewart 	}
2730ceaad40aSRandall Stewart #endif
273158411b08SMichael Tuexen 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
273258411b08SMichael Tuexen 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
273358411b08SMichael Tuexen 	    ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
2734e045904fSMichael Tuexen 		socantrcvmore_locked(stcb->sctp_socket);
273558411b08SMichael Tuexen 	}
273658411b08SMichael Tuexen 	sorwakeup(stcb->sctp_socket);
273758411b08SMichael Tuexen 	sowwakeup(stcb->sctp_socket);
2738ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2739ceaad40aSRandall Stewart 	if (!so_locked) {
2740ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
2741ceaad40aSRandall Stewart 	}
2742ceaad40aSRandall Stewart #endif
2743f8829a4aSRandall Stewart }
2744f8829a4aSRandall Stewart 
2745f8829a4aSRandall Stewart static void
2746f8829a4aSRandall Stewart sctp_notify_peer_addr_change(struct sctp_tcb *stcb, uint32_t state,
2747f8829a4aSRandall Stewart     struct sockaddr *sa, uint32_t error)
2748f8829a4aSRandall Stewart {
2749f8829a4aSRandall Stewart 	struct mbuf *m_notify;
2750f8829a4aSRandall Stewart 	struct sctp_paddr_change *spc;
2751f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
2752f8829a4aSRandall Stewart 
275360990c0cSMichael Tuexen 	if ((stcb == NULL) ||
275460990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT)) {
2755f8829a4aSRandall Stewart 		/* event not enabled */
2756f8829a4aSRandall Stewart 		return;
2757830d754dSRandall Stewart 	}
2758eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_paddr_change), 0, M_NOWAIT, 1, MT_DATA);
2759f8829a4aSRandall Stewart 	if (m_notify == NULL)
2760f8829a4aSRandall Stewart 		return;
2761139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
2762f8829a4aSRandall Stewart 	spc = mtod(m_notify, struct sctp_paddr_change *);
276356711f94SMichael Tuexen 	memset(spc, 0, sizeof(struct sctp_paddr_change));
2764f8829a4aSRandall Stewart 	spc->spc_type = SCTP_PEER_ADDR_CHANGE;
2765f8829a4aSRandall Stewart 	spc->spc_flags = 0;
2766f8829a4aSRandall Stewart 	spc->spc_length = sizeof(struct sctp_paddr_change);
27675e2c2d87SRandall Stewart 	switch (sa->sa_family) {
2768ea5eba11SMichael Tuexen #ifdef INET
27695e2c2d87SRandall Stewart 	case AF_INET:
2770d59107f7SMichael Tuexen #ifdef INET6
2771d59107f7SMichael Tuexen 		if (sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2772d59107f7SMichael Tuexen 			in6_sin_2_v4mapsin6((struct sockaddr_in *)sa,
2773d59107f7SMichael Tuexen 			    (struct sockaddr_in6 *)&spc->spc_aaddr);
2774d59107f7SMichael Tuexen 		} else {
2775f8829a4aSRandall Stewart 			memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
2776d59107f7SMichael Tuexen 		}
2777d59107f7SMichael Tuexen #else
2778d59107f7SMichael Tuexen 		memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
2779d59107f7SMichael Tuexen #endif
27805e2c2d87SRandall Stewart 		break;
2781ea5eba11SMichael Tuexen #endif
27825e2c2d87SRandall Stewart #ifdef INET6
27835e2c2d87SRandall Stewart 	case AF_INET6:
27845e2c2d87SRandall Stewart 		{
2785f42a358aSRandall Stewart 			struct sockaddr_in6 *sin6;
2786f42a358aSRandall Stewart 
2787f8829a4aSRandall Stewart 			memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in6));
2788f42a358aSRandall Stewart 
2789f42a358aSRandall Stewart 			sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
2790f42a358aSRandall Stewart 			if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
279142551e99SRandall Stewart 				if (sin6->sin6_scope_id == 0) {
279242551e99SRandall Stewart 					/* recover scope_id for user */
2793f42a358aSRandall Stewart 					(void)sa6_recoverscope(sin6);
279442551e99SRandall Stewart 				} else {
279542551e99SRandall Stewart 					/* clear embedded scope_id for user */
279642551e99SRandall Stewart 					in6_clearscope(&sin6->sin6_addr);
279742551e99SRandall Stewart 				}
2798f42a358aSRandall Stewart 			}
27995e2c2d87SRandall Stewart 			break;
28005e2c2d87SRandall Stewart 		}
28015e2c2d87SRandall Stewart #endif
28025e2c2d87SRandall Stewart 	default:
28035e2c2d87SRandall Stewart 		/* TSNH */
28045e2c2d87SRandall Stewart 		break;
2805f8829a4aSRandall Stewart 	}
2806f8829a4aSRandall Stewart 	spc->spc_state = state;
2807f8829a4aSRandall Stewart 	spc->spc_error = error;
2808f8829a4aSRandall Stewart 	spc->spc_assoc_id = sctp_get_associd(stcb);
2809f8829a4aSRandall Stewart 
2810139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_paddr_change);
2811139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
2812f8829a4aSRandall Stewart 
2813f8829a4aSRandall Stewart 	/* append to socket */
2814f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
28157215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
2816f8829a4aSRandall Stewart 	    m_notify);
2817f8829a4aSRandall Stewart 	if (control == NULL) {
2818f8829a4aSRandall Stewart 		/* no memory */
2819f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
2820f8829a4aSRandall Stewart 		return;
2821f8829a4aSRandall Stewart 	}
2822139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
2823139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
2824f8829a4aSRandall Stewart 	/* not that we need this */
2825f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
2826f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
2827f8829a4aSRandall Stewart 	    control,
2828cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1,
2829cfde3ff7SRandall Stewart 	    SCTP_READ_LOCK_NOT_HELD,
2830cfde3ff7SRandall Stewart 	    SCTP_SO_NOT_LOCKED);
2831f8829a4aSRandall Stewart }
2832f8829a4aSRandall Stewart 
2833f8829a4aSRandall Stewart 
2834f8829a4aSRandall Stewart static void
28351edc9dbaSMichael Tuexen sctp_notify_send_failed(struct sctp_tcb *stcb, uint8_t sent, uint32_t error,
2836ceaad40aSRandall Stewart     struct sctp_tmit_chunk *chk, int so_locked
2837ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2838ceaad40aSRandall Stewart     SCTP_UNUSED
2839ceaad40aSRandall Stewart #endif
2840ceaad40aSRandall Stewart )
2841f8829a4aSRandall Stewart {
2842830d754dSRandall Stewart 	struct mbuf *m_notify;
2843f8829a4aSRandall Stewart 	struct sctp_send_failed *ssf;
28449935403aSMichael Tuexen 	struct sctp_send_failed_event *ssfe;
2845f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
2846f8829a4aSRandall Stewart 	int length;
2847f8829a4aSRandall Stewart 
284860990c0cSMichael Tuexen 	if ((stcb == NULL) ||
28499935403aSMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
28509935403aSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
2851f8829a4aSRandall Stewart 		/* event not enabled */
2852f8829a4aSRandall Stewart 		return;
2853830d754dSRandall Stewart 	}
28549935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
28559935403aSMichael Tuexen 		length = sizeof(struct sctp_send_failed_event);
28569935403aSMichael Tuexen 	} else {
28579935403aSMichael Tuexen 		length = sizeof(struct sctp_send_failed);
28589935403aSMichael Tuexen 	}
2859eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(length, 0, M_NOWAIT, 1, MT_DATA);
2860f8829a4aSRandall Stewart 	if (m_notify == NULL)
2861f8829a4aSRandall Stewart 		/* no space left */
2862f8829a4aSRandall Stewart 		return;
2863139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
28649935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
28659935403aSMichael Tuexen 		ssfe = mtod(m_notify, struct sctp_send_failed_event *);
2866e432298aSXin LI 		memset(ssfe, 0, length);
28679935403aSMichael Tuexen 		ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
28681edc9dbaSMichael Tuexen 		if (sent) {
28699935403aSMichael Tuexen 			ssfe->ssfe_flags = SCTP_DATA_SENT;
28701edc9dbaSMichael Tuexen 		} else {
28711edc9dbaSMichael Tuexen 			ssfe->ssfe_flags = SCTP_DATA_UNSENT;
28721edc9dbaSMichael Tuexen 		}
2873e432298aSXin LI 		length += chk->send_size;
2874e432298aSXin LI 		length -= sizeof(struct sctp_data_chunk);
28759935403aSMichael Tuexen 		ssfe->ssfe_length = length;
28769935403aSMichael Tuexen 		ssfe->ssfe_error = error;
28779935403aSMichael Tuexen 		/* not exactly what the user sent in, but should be close :) */
28789935403aSMichael Tuexen 		ssfe->ssfe_info.snd_sid = chk->rec.data.stream_number;
28799935403aSMichael Tuexen 		ssfe->ssfe_info.snd_flags = chk->rec.data.rcv_flags;
28809935403aSMichael Tuexen 		ssfe->ssfe_info.snd_ppid = chk->rec.data.payloadtype;
28819935403aSMichael Tuexen 		ssfe->ssfe_info.snd_context = chk->rec.data.context;
28829935403aSMichael Tuexen 		ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
28839935403aSMichael Tuexen 		ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
28849935403aSMichael Tuexen 		SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed_event);
28859935403aSMichael Tuexen 	} else {
2886f8829a4aSRandall Stewart 		ssf = mtod(m_notify, struct sctp_send_failed *);
2887e432298aSXin LI 		memset(ssf, 0, length);
2888f8829a4aSRandall Stewart 		ssf->ssf_type = SCTP_SEND_FAILED;
28891edc9dbaSMichael Tuexen 		if (sent) {
2890f8829a4aSRandall Stewart 			ssf->ssf_flags = SCTP_DATA_SENT;
28911edc9dbaSMichael Tuexen 		} else {
28921edc9dbaSMichael Tuexen 			ssf->ssf_flags = SCTP_DATA_UNSENT;
28931edc9dbaSMichael Tuexen 		}
2894e432298aSXin LI 		length += chk->send_size;
2895e432298aSXin LI 		length -= sizeof(struct sctp_data_chunk);
2896f8829a4aSRandall Stewart 		ssf->ssf_length = length;
2897f8829a4aSRandall Stewart 		ssf->ssf_error = error;
2898f8829a4aSRandall Stewart 		/* not exactly what the user sent in, but should be close :) */
2899d00aff5dSRandall Stewart 		bzero(&ssf->ssf_info, sizeof(ssf->ssf_info));
2900f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_stream = chk->rec.data.stream_number;
2901f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_ssn = chk->rec.data.stream_seq;
2902f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_flags = chk->rec.data.rcv_flags;
2903f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_ppid = chk->rec.data.payloadtype;
2904f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_context = chk->rec.data.context;
2905f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
2906f8829a4aSRandall Stewart 		ssf->ssf_assoc_id = sctp_get_associd(stcb);
29079935403aSMichael Tuexen 		SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed);
29089935403aSMichael Tuexen 	}
2909830d754dSRandall Stewart 	if (chk->data) {
2910830d754dSRandall Stewart 		/*
2911830d754dSRandall Stewart 		 * trim off the sctp chunk header(it should be there)
2912830d754dSRandall Stewart 		 */
2913830d754dSRandall Stewart 		if (chk->send_size >= sizeof(struct sctp_data_chunk)) {
2914830d754dSRandall Stewart 			m_adj(chk->data, sizeof(struct sctp_data_chunk));
2915830d754dSRandall Stewart 			sctp_mbuf_crush(chk->data);
2916830d754dSRandall Stewart 			chk->send_size -= sizeof(struct sctp_data_chunk);
2917830d754dSRandall Stewart 		}
2918830d754dSRandall Stewart 	}
2919810ec536SMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = chk->data;
2920f8829a4aSRandall Stewart 	/* Steal off the mbuf */
2921f8829a4aSRandall Stewart 	chk->data = NULL;
2922f8829a4aSRandall Stewart 	/*
2923f8829a4aSRandall Stewart 	 * For this case, we check the actual socket buffer, since the assoc
2924f8829a4aSRandall Stewart 	 * is going away we don't want to overfill the socket buffer for a
2925f8829a4aSRandall Stewart 	 * non-reader
2926f8829a4aSRandall Stewart 	 */
2927139bc87fSRandall Stewart 	if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
2928f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
2929f8829a4aSRandall Stewart 		return;
2930f8829a4aSRandall Stewart 	}
2931f8829a4aSRandall Stewart 	/* append to socket */
2932f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
29337215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
2934f8829a4aSRandall Stewart 	    m_notify);
2935f8829a4aSRandall Stewart 	if (control == NULL) {
2936f8829a4aSRandall Stewart 		/* no memory */
2937f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
2938f8829a4aSRandall Stewart 		return;
2939f8829a4aSRandall Stewart 	}
2940139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
2941f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
2942f8829a4aSRandall Stewart 	    control,
2943cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1,
2944cfde3ff7SRandall Stewart 	    SCTP_READ_LOCK_NOT_HELD,
2945cfde3ff7SRandall Stewart 	    so_locked);
2946f8829a4aSRandall Stewart }
2947f8829a4aSRandall Stewart 
2948f8829a4aSRandall Stewart 
2949f8829a4aSRandall Stewart static void
2950f8829a4aSRandall Stewart sctp_notify_send_failed2(struct sctp_tcb *stcb, uint32_t error,
2951ceaad40aSRandall Stewart     struct sctp_stream_queue_pending *sp, int so_locked
2952ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2953ceaad40aSRandall Stewart     SCTP_UNUSED
2954ceaad40aSRandall Stewart #endif
2955ceaad40aSRandall Stewart )
2956f8829a4aSRandall Stewart {
2957f8829a4aSRandall Stewart 	struct mbuf *m_notify;
2958f8829a4aSRandall Stewart 	struct sctp_send_failed *ssf;
29599935403aSMichael Tuexen 	struct sctp_send_failed_event *ssfe;
2960f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
2961f8829a4aSRandall Stewart 	int length;
2962f8829a4aSRandall Stewart 
296360990c0cSMichael Tuexen 	if ((stcb == NULL) ||
29649935403aSMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
29659935403aSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
2966f8829a4aSRandall Stewart 		/* event not enabled */
2967f8829a4aSRandall Stewart 		return;
2968830d754dSRandall Stewart 	}
29699935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
29709935403aSMichael Tuexen 		length = sizeof(struct sctp_send_failed_event);
29719935403aSMichael Tuexen 	} else {
29729935403aSMichael Tuexen 		length = sizeof(struct sctp_send_failed);
29739935403aSMichael Tuexen 	}
2974eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(length, 0, M_NOWAIT, 1, MT_DATA);
29759935403aSMichael Tuexen 	if (m_notify == NULL) {
2976f8829a4aSRandall Stewart 		/* no space left */
2977f8829a4aSRandall Stewart 		return;
29789935403aSMichael Tuexen 	}
2979139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
29809935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
29819935403aSMichael Tuexen 		ssfe = mtod(m_notify, struct sctp_send_failed_event *);
2982e432298aSXin LI 		memset(ssfe, 0, length);
2983ad83c8a5SMichael Tuexen 		ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
29849935403aSMichael Tuexen 		ssfe->ssfe_flags = SCTP_DATA_UNSENT;
2985e432298aSXin LI 		length += sp->length;
29869935403aSMichael Tuexen 		ssfe->ssfe_length = length;
29879935403aSMichael Tuexen 		ssfe->ssfe_error = error;
29889935403aSMichael Tuexen 		/* not exactly what the user sent in, but should be close :) */
29899935403aSMichael Tuexen 		ssfe->ssfe_info.snd_sid = sp->stream;
29909935403aSMichael Tuexen 		if (sp->some_taken) {
29919935403aSMichael Tuexen 			ssfe->ssfe_info.snd_flags = SCTP_DATA_LAST_FRAG;
29929935403aSMichael Tuexen 		} else {
29939935403aSMichael Tuexen 			ssfe->ssfe_info.snd_flags = SCTP_DATA_NOT_FRAG;
29949935403aSMichael Tuexen 		}
29959935403aSMichael Tuexen 		ssfe->ssfe_info.snd_ppid = sp->ppid;
29969935403aSMichael Tuexen 		ssfe->ssfe_info.snd_context = sp->context;
29979935403aSMichael Tuexen 		ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
29989935403aSMichael Tuexen 		ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
29999935403aSMichael Tuexen 		SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed_event);
30009935403aSMichael Tuexen 	} else {
3001f8829a4aSRandall Stewart 		ssf = mtod(m_notify, struct sctp_send_failed *);
3002e432298aSXin LI 		memset(ssf, 0, length);
3003f8829a4aSRandall Stewart 		ssf->ssf_type = SCTP_SEND_FAILED;
3004f8829a4aSRandall Stewart 		ssf->ssf_flags = SCTP_DATA_UNSENT;
3005e432298aSXin LI 		length += sp->length;
3006f8829a4aSRandall Stewart 		ssf->ssf_length = length;
3007f8829a4aSRandall Stewart 		ssf->ssf_error = error;
3008f8829a4aSRandall Stewart 		/* not exactly what the user sent in, but should be close :) */
3009f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_stream = sp->stream;
3010f3b05218SMichael Tuexen 		ssf->ssf_info.sinfo_ssn = 0;
3011fc14de76SRandall Stewart 		if (sp->some_taken) {
3012fc14de76SRandall Stewart 			ssf->ssf_info.sinfo_flags = SCTP_DATA_LAST_FRAG;
3013fc14de76SRandall Stewart 		} else {
3014fc14de76SRandall Stewart 			ssf->ssf_info.sinfo_flags = SCTP_DATA_NOT_FRAG;
3015fc14de76SRandall Stewart 		}
3016f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_ppid = sp->ppid;
3017f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_context = sp->context;
3018f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3019f8829a4aSRandall Stewart 		ssf->ssf_assoc_id = sctp_get_associd(stcb);
3020139bc87fSRandall Stewart 		SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed);
30219935403aSMichael Tuexen 	}
30229935403aSMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = sp->data;
3023f8829a4aSRandall Stewart 
3024f8829a4aSRandall Stewart 	/* Steal off the mbuf */
3025f8829a4aSRandall Stewart 	sp->data = NULL;
3026f8829a4aSRandall Stewart 	/*
3027f8829a4aSRandall Stewart 	 * For this case, we check the actual socket buffer, since the assoc
3028f8829a4aSRandall Stewart 	 * is going away we don't want to overfill the socket buffer for a
3029f8829a4aSRandall Stewart 	 * non-reader
3030f8829a4aSRandall Stewart 	 */
3031139bc87fSRandall Stewart 	if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3032f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3033f8829a4aSRandall Stewart 		return;
3034f8829a4aSRandall Stewart 	}
3035f8829a4aSRandall Stewart 	/* append to socket */
3036f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
30377215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3038f8829a4aSRandall Stewart 	    m_notify);
3039f8829a4aSRandall Stewart 	if (control == NULL) {
3040f8829a4aSRandall Stewart 		/* no memory */
3041f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3042f8829a4aSRandall Stewart 		return;
3043f8829a4aSRandall Stewart 	}
3044139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3045f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3046f8829a4aSRandall Stewart 	    control,
3047cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3048f8829a4aSRandall Stewart }
3049f8829a4aSRandall Stewart 
3050f8829a4aSRandall Stewart 
3051f8829a4aSRandall Stewart 
3052f8829a4aSRandall Stewart static void
30537215cc1bSMichael Tuexen sctp_notify_adaptation_layer(struct sctp_tcb *stcb)
3054f8829a4aSRandall Stewart {
3055f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3056f8829a4aSRandall Stewart 	struct sctp_adaptation_event *sai;
3057f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3058f8829a4aSRandall Stewart 
305960990c0cSMichael Tuexen 	if ((stcb == NULL) ||
306060990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT)) {
3061f8829a4aSRandall Stewart 		/* event not enabled */
3062f8829a4aSRandall Stewart 		return;
3063830d754dSRandall Stewart 	}
3064eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 0, M_NOWAIT, 1, MT_DATA);
3065f8829a4aSRandall Stewart 	if (m_notify == NULL)
3066f8829a4aSRandall Stewart 		/* no space left */
3067f8829a4aSRandall Stewart 		return;
3068139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3069f8829a4aSRandall Stewart 	sai = mtod(m_notify, struct sctp_adaptation_event *);
3070e432298aSXin LI 	memset(sai, 0, sizeof(struct sctp_adaptation_event));
3071f8829a4aSRandall Stewart 	sai->sai_type = SCTP_ADAPTATION_INDICATION;
3072f8829a4aSRandall Stewart 	sai->sai_flags = 0;
3073f8829a4aSRandall Stewart 	sai->sai_length = sizeof(struct sctp_adaptation_event);
30742afb3e84SRandall Stewart 	sai->sai_adaptation_ind = stcb->asoc.peers_adaptation;
3075f8829a4aSRandall Stewart 	sai->sai_assoc_id = sctp_get_associd(stcb);
3076f8829a4aSRandall Stewart 
3077139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_adaptation_event);
3078139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3079f8829a4aSRandall Stewart 
3080f8829a4aSRandall Stewart 	/* append to socket */
3081f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
30827215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3083f8829a4aSRandall Stewart 	    m_notify);
3084f8829a4aSRandall Stewart 	if (control == NULL) {
3085f8829a4aSRandall Stewart 		/* no memory */
3086f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3087f8829a4aSRandall Stewart 		return;
3088f8829a4aSRandall Stewart 	}
3089139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3090139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3091f8829a4aSRandall Stewart 	/* not that we need this */
3092f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3093f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3094f8829a4aSRandall Stewart 	    control,
3095cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3096f8829a4aSRandall Stewart }
3097f8829a4aSRandall Stewart 
309803b0b021SRandall Stewart /* This always must be called with the read-queue LOCKED in the INP */
3099810ec536SMichael Tuexen static void
31002dad8a55SRandall Stewart sctp_notify_partial_delivery_indication(struct sctp_tcb *stcb, uint32_t error,
3101810ec536SMichael Tuexen     uint32_t val, int so_locked
3102810ec536SMichael Tuexen #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3103810ec536SMichael Tuexen     SCTP_UNUSED
3104810ec536SMichael Tuexen #endif
3105810ec536SMichael Tuexen )
3106f8829a4aSRandall Stewart {
3107f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3108f8829a4aSRandall Stewart 	struct sctp_pdapi_event *pdapi;
3109f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
311003b0b021SRandall Stewart 	struct sockbuf *sb;
3111f8829a4aSRandall Stewart 
311260990c0cSMichael Tuexen 	if ((stcb == NULL) ||
311360990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_PDAPIEVNT)) {
3114f8829a4aSRandall Stewart 		/* event not enabled */
3115f8829a4aSRandall Stewart 		return;
3116830d754dSRandall Stewart 	}
3117cd1386abSMichael Tuexen 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
3118cd1386abSMichael Tuexen 		return;
3119cd1386abSMichael Tuexen 	}
3120eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_pdapi_event), 0, M_NOWAIT, 1, MT_DATA);
3121f8829a4aSRandall Stewart 	if (m_notify == NULL)
3122f8829a4aSRandall Stewart 		/* no space left */
3123f8829a4aSRandall Stewart 		return;
3124139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3125f8829a4aSRandall Stewart 	pdapi = mtod(m_notify, struct sctp_pdapi_event *);
3126e432298aSXin LI 	memset(pdapi, 0, sizeof(struct sctp_pdapi_event));
3127f8829a4aSRandall Stewart 	pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
3128f8829a4aSRandall Stewart 	pdapi->pdapi_flags = 0;
3129f8829a4aSRandall Stewart 	pdapi->pdapi_length = sizeof(struct sctp_pdapi_event);
3130f8829a4aSRandall Stewart 	pdapi->pdapi_indication = error;
31319a6142d8SRandall Stewart 	pdapi->pdapi_stream = (val >> 16);
31329a6142d8SRandall Stewart 	pdapi->pdapi_seq = (val & 0x0000ffff);
3133f8829a4aSRandall Stewart 	pdapi->pdapi_assoc_id = sctp_get_associd(stcb);
3134f8829a4aSRandall Stewart 
3135139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_pdapi_event);
3136139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3137f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
31387215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3139f8829a4aSRandall Stewart 	    m_notify);
3140f8829a4aSRandall Stewart 	if (control == NULL) {
3141f8829a4aSRandall Stewart 		/* no memory */
3142f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3143f8829a4aSRandall Stewart 		return;
3144f8829a4aSRandall Stewart 	}
3145139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3146139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3147f8829a4aSRandall Stewart 	/* not that we need this */
3148f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
314903b0b021SRandall Stewart 	control->held_length = 0;
315003b0b021SRandall Stewart 	control->length = 0;
315103b0b021SRandall Stewart 	sb = &stcb->sctp_socket->so_rcv;
3152b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3153139bc87fSRandall Stewart 		sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m_notify));
315480fefe0aSRandall Stewart 	}
315503b0b021SRandall Stewart 	sctp_sballoc(stcb, sb, m_notify);
3156b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
315703b0b021SRandall Stewart 		sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
315880fefe0aSRandall Stewart 	}
3159139bc87fSRandall Stewart 	atomic_add_int(&control->length, SCTP_BUF_LEN(m_notify));
316003b0b021SRandall Stewart 	control->end_added = 1;
316103b0b021SRandall Stewart 	if (stcb->asoc.control_pdapi)
316203b0b021SRandall Stewart 		TAILQ_INSERT_AFTER(&stcb->sctp_ep->read_queue, stcb->asoc.control_pdapi, control, next);
316303b0b021SRandall Stewart 	else {
316403b0b021SRandall Stewart 		/* we really should not see this case */
316503b0b021SRandall Stewart 		TAILQ_INSERT_TAIL(&stcb->sctp_ep->read_queue, control, next);
316603b0b021SRandall Stewart 	}
316703b0b021SRandall Stewart 	if (stcb->sctp_ep && stcb->sctp_socket) {
316803b0b021SRandall Stewart 		/* This should always be the case */
3169810ec536SMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3170810ec536SMichael Tuexen 		struct socket *so;
3171810ec536SMichael Tuexen 
3172810ec536SMichael Tuexen 		so = SCTP_INP_SO(stcb->sctp_ep);
3173810ec536SMichael Tuexen 		if (!so_locked) {
3174810ec536SMichael Tuexen 			atomic_add_int(&stcb->asoc.refcnt, 1);
3175810ec536SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
3176810ec536SMichael Tuexen 			SCTP_SOCKET_LOCK(so, 1);
3177810ec536SMichael Tuexen 			SCTP_TCB_LOCK(stcb);
3178810ec536SMichael Tuexen 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
3179810ec536SMichael Tuexen 			if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3180810ec536SMichael Tuexen 				SCTP_SOCKET_UNLOCK(so, 1);
3181810ec536SMichael Tuexen 				return;
3182810ec536SMichael Tuexen 			}
3183810ec536SMichael Tuexen 		}
3184810ec536SMichael Tuexen #endif
318503b0b021SRandall Stewart 		sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
3186810ec536SMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3187810ec536SMichael Tuexen 		if (!so_locked) {
3188810ec536SMichael Tuexen 			SCTP_SOCKET_UNLOCK(so, 1);
3189810ec536SMichael Tuexen 		}
3190810ec536SMichael Tuexen #endif
3191f8829a4aSRandall Stewart 	}
3192f8829a4aSRandall Stewart }
3193f8829a4aSRandall Stewart 
3194f8829a4aSRandall Stewart static void
3195f8829a4aSRandall Stewart sctp_notify_shutdown_event(struct sctp_tcb *stcb)
3196f8829a4aSRandall Stewart {
3197f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3198f8829a4aSRandall Stewart 	struct sctp_shutdown_event *sse;
3199f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3200f8829a4aSRandall Stewart 
3201f8829a4aSRandall Stewart 	/*
3202f8829a4aSRandall Stewart 	 * For TCP model AND UDP connected sockets we will send an error up
3203f8829a4aSRandall Stewart 	 * when an SHUTDOWN completes
3204f8829a4aSRandall Stewart 	 */
3205f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3206f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3207f8829a4aSRandall Stewart 		/* mark socket closed for read/write and wakeup! */
3208ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3209ceaad40aSRandall Stewart 		struct socket *so;
3210ceaad40aSRandall Stewart 
3211ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
3212ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
3213ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3214ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
3215ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
3216ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
3217ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3218ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
3219ceaad40aSRandall Stewart 			return;
3220ceaad40aSRandall Stewart 		}
3221ceaad40aSRandall Stewart #endif
3222f8829a4aSRandall Stewart 		socantsendmore(stcb->sctp_socket);
3223ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3224ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
3225ceaad40aSRandall Stewart #endif
3226f8829a4aSRandall Stewart 	}
3227e2e7c62eSMichael Tuexen 	if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)) {
3228f8829a4aSRandall Stewart 		/* event not enabled */
3229f8829a4aSRandall Stewart 		return;
3230830d754dSRandall Stewart 	}
3231eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_event), 0, M_NOWAIT, 1, MT_DATA);
3232f8829a4aSRandall Stewart 	if (m_notify == NULL)
3233f8829a4aSRandall Stewart 		/* no space left */
3234f8829a4aSRandall Stewart 		return;
3235f8829a4aSRandall Stewart 	sse = mtod(m_notify, struct sctp_shutdown_event *);
3236e432298aSXin LI 	memset(sse, 0, sizeof(struct sctp_shutdown_event));
3237f8829a4aSRandall Stewart 	sse->sse_type = SCTP_SHUTDOWN_EVENT;
3238f8829a4aSRandall Stewart 	sse->sse_flags = 0;
3239f8829a4aSRandall Stewart 	sse->sse_length = sizeof(struct sctp_shutdown_event);
3240f8829a4aSRandall Stewart 	sse->sse_assoc_id = sctp_get_associd(stcb);
3241f8829a4aSRandall Stewart 
3242139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_shutdown_event);
3243139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3244f8829a4aSRandall Stewart 
3245f8829a4aSRandall Stewart 	/* append to socket */
3246f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
32477215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3248f8829a4aSRandall Stewart 	    m_notify);
3249f8829a4aSRandall Stewart 	if (control == NULL) {
3250f8829a4aSRandall Stewart 		/* no memory */
3251f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3252f8829a4aSRandall Stewart 		return;
3253f8829a4aSRandall Stewart 	}
3254139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3255139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3256f8829a4aSRandall Stewart 	/* not that we need this */
3257f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3258f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3259f8829a4aSRandall Stewart 	    control,
3260cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3261f8829a4aSRandall Stewart }
3262f8829a4aSRandall Stewart 
3263f8829a4aSRandall Stewart static void
3264830d754dSRandall Stewart sctp_notify_sender_dry_event(struct sctp_tcb *stcb,
3265830d754dSRandall Stewart     int so_locked
3266830d754dSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3267830d754dSRandall Stewart     SCTP_UNUSED
3268830d754dSRandall Stewart #endif
3269830d754dSRandall Stewart )
3270830d754dSRandall Stewart {
3271830d754dSRandall Stewart 	struct mbuf *m_notify;
3272830d754dSRandall Stewart 	struct sctp_sender_dry_event *event;
3273830d754dSRandall Stewart 	struct sctp_queued_to_read *control;
3274830d754dSRandall Stewart 
327560990c0cSMichael Tuexen 	if ((stcb == NULL) ||
327660990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DRYEVNT)) {
3277830d754dSRandall Stewart 		/* event not enabled */
3278830d754dSRandall Stewart 		return;
3279830d754dSRandall Stewart 	}
3280eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_sender_dry_event), 0, M_NOWAIT, 1, MT_DATA);
3281830d754dSRandall Stewart 	if (m_notify == NULL) {
3282830d754dSRandall Stewart 		/* no space left */
3283830d754dSRandall Stewart 		return;
3284830d754dSRandall Stewart 	}
3285830d754dSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3286830d754dSRandall Stewart 	event = mtod(m_notify, struct sctp_sender_dry_event *);
3287e432298aSXin LI 	memset(event, 0, sizeof(struct sctp_sender_dry_event));
3288830d754dSRandall Stewart 	event->sender_dry_type = SCTP_SENDER_DRY_EVENT;
3289830d754dSRandall Stewart 	event->sender_dry_flags = 0;
3290830d754dSRandall Stewart 	event->sender_dry_length = sizeof(struct sctp_sender_dry_event);
3291830d754dSRandall Stewart 	event->sender_dry_assoc_id = sctp_get_associd(stcb);
3292830d754dSRandall Stewart 
3293830d754dSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_sender_dry_event);
3294830d754dSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3295830d754dSRandall Stewart 
3296830d754dSRandall Stewart 	/* append to socket */
3297830d754dSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
32987215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
32997215cc1bSMichael Tuexen 	    m_notify);
3300830d754dSRandall Stewart 	if (control == NULL) {
3301830d754dSRandall Stewart 		/* no memory */
3302830d754dSRandall Stewart 		sctp_m_freem(m_notify);
3303830d754dSRandall Stewart 		return;
3304830d754dSRandall Stewart 	}
3305830d754dSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3306830d754dSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3307830d754dSRandall Stewart 	/* not that we need this */
3308830d754dSRandall Stewart 	control->tail_mbuf = m_notify;
3309830d754dSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3310cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3311830d754dSRandall Stewart }
3312830d754dSRandall Stewart 
3313ea44232bSRandall Stewart 
3314c4e848b7SRandall Stewart void
3315c4e848b7SRandall Stewart sctp_notify_stream_reset_add(struct sctp_tcb *stcb, uint16_t numberin, uint16_t numberout, int flag)
3316ea44232bSRandall Stewart {
3317ea44232bSRandall Stewart 	struct mbuf *m_notify;
3318ea44232bSRandall Stewart 	struct sctp_queued_to_read *control;
3319c4e848b7SRandall Stewart 	struct sctp_stream_change_event *stradd;
3320ea44232bSRandall Stewart 
33218c501e51SMichael Tuexen 	if ((stcb == NULL) ||
33228c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_CHANGEEVNT))) {
3323ea44232bSRandall Stewart 		/* event not enabled */
3324ea44232bSRandall Stewart 		return;
3325ea44232bSRandall Stewart 	}
3326c4e848b7SRandall Stewart 	if ((stcb->asoc.peer_req_out) && flag) {
3327c4e848b7SRandall Stewart 		/* Peer made the request, don't tell the local user */
3328c4e848b7SRandall Stewart 		stcb->asoc.peer_req_out = 0;
3329c4e848b7SRandall Stewart 		return;
3330c4e848b7SRandall Stewart 	}
3331c4e848b7SRandall Stewart 	stcb->asoc.peer_req_out = 0;
3332e432298aSXin LI 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_stream_change_event), 0, M_NOWAIT, 1, MT_DATA);
3333ea44232bSRandall Stewart 	if (m_notify == NULL)
3334ea44232bSRandall Stewart 		/* no space left */
3335ea44232bSRandall Stewart 		return;
3336ea44232bSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3337c4e848b7SRandall Stewart 	stradd = mtod(m_notify, struct sctp_stream_change_event *);
3338e432298aSXin LI 	memset(stradd, 0, sizeof(struct sctp_stream_change_event));
3339c4e848b7SRandall Stewart 	stradd->strchange_type = SCTP_STREAM_CHANGE_EVENT;
3340c4e848b7SRandall Stewart 	stradd->strchange_flags = flag;
3341e432298aSXin LI 	stradd->strchange_length = sizeof(struct sctp_stream_change_event);
3342c4e848b7SRandall Stewart 	stradd->strchange_assoc_id = sctp_get_associd(stcb);
3343c4e848b7SRandall Stewart 	stradd->strchange_instrms = numberin;
3344c4e848b7SRandall Stewart 	stradd->strchange_outstrms = numberout;
3345e432298aSXin LI 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_stream_change_event);
3346ea44232bSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3347ea44232bSRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3348ea44232bSRandall Stewart 		/* no space */
3349ea44232bSRandall Stewart 		sctp_m_freem(m_notify);
3350ea44232bSRandall Stewart 		return;
3351ea44232bSRandall Stewart 	}
3352ea44232bSRandall Stewart 	/* append to socket */
3353ea44232bSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
33547215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3355ea44232bSRandall Stewart 	    m_notify);
3356ea44232bSRandall Stewart 	if (control == NULL) {
3357ea44232bSRandall Stewart 		/* no memory */
3358ea44232bSRandall Stewart 		sctp_m_freem(m_notify);
3359ea44232bSRandall Stewart 		return;
3360ea44232bSRandall Stewart 	}
3361ea44232bSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3362ea44232bSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3363ea44232bSRandall Stewart 	/* not that we need this */
3364ea44232bSRandall Stewart 	control->tail_mbuf = m_notify;
3365ea44232bSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3366ea44232bSRandall Stewart 	    control,
3367cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3368ea44232bSRandall Stewart }
3369ea44232bSRandall Stewart 
3370c4e848b7SRandall Stewart void
3371c4e848b7SRandall Stewart sctp_notify_stream_reset_tsn(struct sctp_tcb *stcb, uint32_t sending_tsn, uint32_t recv_tsn, int flag)
3372c4e848b7SRandall Stewart {
3373c4e848b7SRandall Stewart 	struct mbuf *m_notify;
3374c4e848b7SRandall Stewart 	struct sctp_queued_to_read *control;
3375c4e848b7SRandall Stewart 	struct sctp_assoc_reset_event *strasoc;
3376c4e848b7SRandall Stewart 
33778c501e51SMichael Tuexen 	if ((stcb == NULL) ||
33788c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ASSOC_RESETEVNT))) {
3379c4e848b7SRandall Stewart 		/* event not enabled */
3380c4e848b7SRandall Stewart 		return;
3381c4e848b7SRandall Stewart 	}
3382e432298aSXin LI 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_assoc_reset_event), 0, M_NOWAIT, 1, MT_DATA);
3383c4e848b7SRandall Stewart 	if (m_notify == NULL)
3384c4e848b7SRandall Stewart 		/* no space left */
3385c4e848b7SRandall Stewart 		return;
3386c4e848b7SRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3387c4e848b7SRandall Stewart 	strasoc = mtod(m_notify, struct sctp_assoc_reset_event *);
3388e432298aSXin LI 	memset(strasoc, 0, sizeof(struct sctp_assoc_reset_event));
3389c4e848b7SRandall Stewart 	strasoc->assocreset_type = SCTP_ASSOC_RESET_EVENT;
3390c4e848b7SRandall Stewart 	strasoc->assocreset_flags = flag;
3391e432298aSXin LI 	strasoc->assocreset_length = sizeof(struct sctp_assoc_reset_event);
3392c4e848b7SRandall Stewart 	strasoc->assocreset_assoc_id = sctp_get_associd(stcb);
3393c4e848b7SRandall Stewart 	strasoc->assocreset_local_tsn = sending_tsn;
3394c4e848b7SRandall Stewart 	strasoc->assocreset_remote_tsn = recv_tsn;
3395e432298aSXin LI 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_assoc_reset_event);
3396c4e848b7SRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3397c4e848b7SRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3398c4e848b7SRandall Stewart 		/* no space */
3399c4e848b7SRandall Stewart 		sctp_m_freem(m_notify);
3400c4e848b7SRandall Stewart 		return;
3401c4e848b7SRandall Stewart 	}
3402c4e848b7SRandall Stewart 	/* append to socket */
3403c4e848b7SRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3404c4e848b7SRandall Stewart 	    0, 0, stcb->asoc.context, 0, 0, 0,
3405c4e848b7SRandall Stewart 	    m_notify);
3406c4e848b7SRandall Stewart 	if (control == NULL) {
3407c4e848b7SRandall Stewart 		/* no memory */
3408c4e848b7SRandall Stewart 		sctp_m_freem(m_notify);
3409c4e848b7SRandall Stewart 		return;
3410c4e848b7SRandall Stewart 	}
3411c4e848b7SRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3412c4e848b7SRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3413c4e848b7SRandall Stewart 	/* not that we need this */
3414c4e848b7SRandall Stewart 	control->tail_mbuf = m_notify;
3415c4e848b7SRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3416c4e848b7SRandall Stewart 	    control,
3417c4e848b7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3418c4e848b7SRandall Stewart }
3419c4e848b7SRandall Stewart 
3420c4e848b7SRandall Stewart 
3421ea44232bSRandall Stewart 
3422830d754dSRandall Stewart static void
3423f8829a4aSRandall Stewart sctp_notify_stream_reset(struct sctp_tcb *stcb,
3424f8829a4aSRandall Stewart     int number_entries, uint16_t * list, int flag)
3425f8829a4aSRandall Stewart {
3426f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3427f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3428f8829a4aSRandall Stewart 	struct sctp_stream_reset_event *strreset;
3429f8829a4aSRandall Stewart 	int len;
3430f8829a4aSRandall Stewart 
34318c501e51SMichael Tuexen 	if ((stcb == NULL) ||
34328c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT))) {
3433f8829a4aSRandall Stewart 		/* event not enabled */
3434f8829a4aSRandall Stewart 		return;
3435830d754dSRandall Stewart 	}
3436eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
3437f8829a4aSRandall Stewart 	if (m_notify == NULL)
3438f8829a4aSRandall Stewart 		/* no space left */
3439f8829a4aSRandall Stewart 		return;
3440139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3441f8829a4aSRandall Stewart 	len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
3442f8829a4aSRandall Stewart 	if (len > M_TRAILINGSPACE(m_notify)) {
3443f8829a4aSRandall Stewart 		/* never enough room */
3444f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3445f8829a4aSRandall Stewart 		return;
3446f8829a4aSRandall Stewart 	}
3447f8829a4aSRandall Stewart 	strreset = mtod(m_notify, struct sctp_stream_reset_event *);
3448e432298aSXin LI 	memset(strreset, 0, len);
3449f8829a4aSRandall Stewart 	strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
3450c4e848b7SRandall Stewart 	strreset->strreset_flags = flag;
3451f8829a4aSRandall Stewart 	strreset->strreset_length = len;
3452f8829a4aSRandall Stewart 	strreset->strreset_assoc_id = sctp_get_associd(stcb);
3453f8829a4aSRandall Stewart 	if (number_entries) {
3454f8829a4aSRandall Stewart 		int i;
3455f8829a4aSRandall Stewart 
3456f8829a4aSRandall Stewart 		for (i = 0; i < number_entries; i++) {
3457c4e848b7SRandall Stewart 			strreset->strreset_stream_list[i] = ntohs(list[i]);
3458f8829a4aSRandall Stewart 		}
3459f8829a4aSRandall Stewart 	}
3460139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = len;
3461139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3462139bc87fSRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3463f8829a4aSRandall Stewart 		/* no space */
3464f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3465f8829a4aSRandall Stewart 		return;
3466f8829a4aSRandall Stewart 	}
3467f8829a4aSRandall Stewart 	/* append to socket */
3468f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
34697215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3470f8829a4aSRandall Stewart 	    m_notify);
3471f8829a4aSRandall Stewart 	if (control == NULL) {
3472f8829a4aSRandall Stewart 		/* no memory */
3473f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3474f8829a4aSRandall Stewart 		return;
3475f8829a4aSRandall Stewart 	}
3476139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3477139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3478f8829a4aSRandall Stewart 	/* not that we need this */
3479f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3480f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3481f8829a4aSRandall Stewart 	    control,
3482cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3483f8829a4aSRandall Stewart }
3484f8829a4aSRandall Stewart 
3485f8829a4aSRandall Stewart 
3486389b1b11SMichael Tuexen static void
3487389b1b11SMichael Tuexen sctp_notify_remote_error(struct sctp_tcb *stcb, uint16_t error, struct sctp_error_chunk *chunk)
3488389b1b11SMichael Tuexen {
3489389b1b11SMichael Tuexen 	struct mbuf *m_notify;
3490389b1b11SMichael Tuexen 	struct sctp_remote_error *sre;
3491389b1b11SMichael Tuexen 	struct sctp_queued_to_read *control;
3492389b1b11SMichael Tuexen 	size_t notif_len, chunk_len;
3493389b1b11SMichael Tuexen 
3494389b1b11SMichael Tuexen 	if ((stcb == NULL) ||
3495389b1b11SMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPEERERR)) {
3496389b1b11SMichael Tuexen 		return;
3497389b1b11SMichael Tuexen 	}
3498389b1b11SMichael Tuexen 	if (chunk != NULL) {
3499c9eb4473SMichael Tuexen 		chunk_len = ntohs(chunk->ch.chunk_length);
3500389b1b11SMichael Tuexen 	} else {
3501389b1b11SMichael Tuexen 		chunk_len = 0;
3502389b1b11SMichael Tuexen 	}
3503389b1b11SMichael Tuexen 	notif_len = sizeof(struct sctp_remote_error) + chunk_len;
3504eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3505389b1b11SMichael Tuexen 	if (m_notify == NULL) {
3506389b1b11SMichael Tuexen 		/* Retry with smaller value. */
3507389b1b11SMichael Tuexen 		notif_len = sizeof(struct sctp_remote_error);
3508eb1b1807SGleb Smirnoff 		m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3509389b1b11SMichael Tuexen 		if (m_notify == NULL) {
3510389b1b11SMichael Tuexen 			return;
3511389b1b11SMichael Tuexen 		}
3512389b1b11SMichael Tuexen 	}
3513389b1b11SMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = NULL;
3514389b1b11SMichael Tuexen 	sre = mtod(m_notify, struct sctp_remote_error *);
351556711f94SMichael Tuexen 	memset(sre, 0, notif_len);
3516389b1b11SMichael Tuexen 	sre->sre_type = SCTP_REMOTE_ERROR;
3517389b1b11SMichael Tuexen 	sre->sre_flags = 0;
3518389b1b11SMichael Tuexen 	sre->sre_length = sizeof(struct sctp_remote_error);
3519389b1b11SMichael Tuexen 	sre->sre_error = error;
3520389b1b11SMichael Tuexen 	sre->sre_assoc_id = sctp_get_associd(stcb);
3521389b1b11SMichael Tuexen 	if (notif_len > sizeof(struct sctp_remote_error)) {
3522389b1b11SMichael Tuexen 		memcpy(sre->sre_data, chunk, chunk_len);
3523389b1b11SMichael Tuexen 		sre->sre_length += chunk_len;
3524389b1b11SMichael Tuexen 	}
3525389b1b11SMichael Tuexen 	SCTP_BUF_LEN(m_notify) = sre->sre_length;
3526389b1b11SMichael Tuexen 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3527389b1b11SMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3528389b1b11SMichael Tuexen 	    m_notify);
3529389b1b11SMichael Tuexen 	if (control != NULL) {
3530389b1b11SMichael Tuexen 		control->length = SCTP_BUF_LEN(m_notify);
3531389b1b11SMichael Tuexen 		/* not that we need this */
3532389b1b11SMichael Tuexen 		control->tail_mbuf = m_notify;
3533389b1b11SMichael Tuexen 		control->spec_flags = M_NOTIFICATION;
3534389b1b11SMichael Tuexen 		sctp_add_to_readq(stcb->sctp_ep, stcb,
3535389b1b11SMichael Tuexen 		    control,
3536389b1b11SMichael Tuexen 		    &stcb->sctp_socket->so_rcv, 1,
3537389b1b11SMichael Tuexen 		    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3538389b1b11SMichael Tuexen 	} else {
3539389b1b11SMichael Tuexen 		sctp_m_freem(m_notify);
3540389b1b11SMichael Tuexen 	}
3541389b1b11SMichael Tuexen }
3542389b1b11SMichael Tuexen 
3543389b1b11SMichael Tuexen 
3544f8829a4aSRandall Stewart void
3545f8829a4aSRandall Stewart sctp_ulp_notify(uint32_t notification, struct sctp_tcb *stcb,
3546ceaad40aSRandall Stewart     uint32_t error, void *data, int so_locked
3547ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3548ceaad40aSRandall Stewart     SCTP_UNUSED
3549ceaad40aSRandall Stewart #endif
3550ceaad40aSRandall Stewart )
3551f8829a4aSRandall Stewart {
3552830d754dSRandall Stewart 	if ((stcb == NULL) ||
3553830d754dSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3554f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3555830d754dSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3556830d754dSRandall Stewart 		/* If the socket is gone we are out of here */
3557f8829a4aSRandall Stewart 		return;
3558f8829a4aSRandall Stewart 	}
3559a99b6783SRandall Stewart 	if (stcb->sctp_socket->so_rcv.sb_state & SBS_CANTRCVMORE) {
3560a99b6783SRandall Stewart 		return;
3561a99b6783SRandall Stewart 	}
3562fb4a67d2SMichael Tuexen 	if ((stcb->asoc.state & SCTP_STATE_COOKIE_WAIT) ||
3563fb4a67d2SMichael Tuexen 	    (stcb->asoc.state & SCTP_STATE_COOKIE_ECHOED)) {
356417205eccSRandall Stewart 		if ((notification == SCTP_NOTIFY_INTERFACE_DOWN) ||
356517205eccSRandall Stewart 		    (notification == SCTP_NOTIFY_INTERFACE_UP) ||
356617205eccSRandall Stewart 		    (notification == SCTP_NOTIFY_INTERFACE_CONFIRMED)) {
356717205eccSRandall Stewart 			/* Don't report these in front states */
356817205eccSRandall Stewart 			return;
356917205eccSRandall Stewart 		}
357017205eccSRandall Stewart 	}
3571f8829a4aSRandall Stewart 	switch (notification) {
3572f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_UP:
3573f8829a4aSRandall Stewart 		if (stcb->asoc.assoc_up_sent == 0) {
3574410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_UP, stcb, error, NULL, 0, so_locked);
3575f8829a4aSRandall Stewart 			stcb->asoc.assoc_up_sent = 1;
3576f8829a4aSRandall Stewart 		}
35772afb3e84SRandall Stewart 		if (stcb->asoc.adaptation_needed && (stcb->asoc.adaptation_sent == 0)) {
35787215cc1bSMichael Tuexen 			sctp_notify_adaptation_layer(stcb);
35792afb3e84SRandall Stewart 		}
3580c79bec9cSMichael Tuexen 		if (stcb->asoc.auth_supported == 0) {
3581830d754dSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3582830d754dSRandall Stewart 			    NULL, so_locked);
3583830d754dSRandall Stewart 		}
3584f8829a4aSRandall Stewart 		break;
3585f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_DOWN:
3586410a3b1eSMichael Tuexen 		sctp_notify_assoc_change(SCTP_SHUTDOWN_COMP, stcb, error, NULL, 0, so_locked);
3587f8829a4aSRandall Stewart 		break;
3588f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_DOWN:
3589f8829a4aSRandall Stewart 		{
3590f8829a4aSRandall Stewart 			struct sctp_nets *net;
3591f8829a4aSRandall Stewart 
3592f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
3593f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_UNREACHABLE,
3594f8829a4aSRandall Stewart 			    (struct sockaddr *)&net->ro._l_addr, error);
3595f8829a4aSRandall Stewart 			break;
3596f8829a4aSRandall Stewart 		}
3597f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_UP:
3598f8829a4aSRandall Stewart 		{
3599f8829a4aSRandall Stewart 			struct sctp_nets *net;
3600f8829a4aSRandall Stewart 
3601f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
3602f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_AVAILABLE,
3603f8829a4aSRandall Stewart 			    (struct sockaddr *)&net->ro._l_addr, error);
3604f8829a4aSRandall Stewart 			break;
3605f8829a4aSRandall Stewart 		}
3606f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_CONFIRMED:
3607f8829a4aSRandall Stewart 		{
3608f8829a4aSRandall Stewart 			struct sctp_nets *net;
3609f8829a4aSRandall Stewart 
3610f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
3611f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_CONFIRMED,
3612f8829a4aSRandall Stewart 			    (struct sockaddr *)&net->ro._l_addr, error);
3613f8829a4aSRandall Stewart 			break;
3614f8829a4aSRandall Stewart 		}
3615f8829a4aSRandall Stewart 	case SCTP_NOTIFY_SPECIAL_SP_FAIL:
3616f8829a4aSRandall Stewart 		sctp_notify_send_failed2(stcb, error,
3617ceaad40aSRandall Stewart 		    (struct sctp_stream_queue_pending *)data, so_locked);
3618f8829a4aSRandall Stewart 		break;
36191edc9dbaSMichael Tuexen 	case SCTP_NOTIFY_SENT_DG_FAIL:
36201edc9dbaSMichael Tuexen 		sctp_notify_send_failed(stcb, 1, error,
36211edc9dbaSMichael Tuexen 		    (struct sctp_tmit_chunk *)data, so_locked);
36221edc9dbaSMichael Tuexen 		break;
36231edc9dbaSMichael Tuexen 	case SCTP_NOTIFY_UNSENT_DG_FAIL:
36241edc9dbaSMichael Tuexen 		sctp_notify_send_failed(stcb, 0, error,
3625ceaad40aSRandall Stewart 		    (struct sctp_tmit_chunk *)data, so_locked);
3626f8829a4aSRandall Stewart 		break;
3627f8829a4aSRandall Stewart 	case SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION:
36289a6142d8SRandall Stewart 		{
36299a6142d8SRandall Stewart 			uint32_t val;
36309a6142d8SRandall Stewart 
36319a6142d8SRandall Stewart 			val = *((uint32_t *) data);
36329a6142d8SRandall Stewart 
3633810ec536SMichael Tuexen 			sctp_notify_partial_delivery_indication(stcb, error, val, so_locked);
3634f8829a4aSRandall Stewart 			break;
3635810ec536SMichael Tuexen 		}
3636410a3b1eSMichael Tuexen 	case SCTP_NOTIFY_ASSOC_LOC_ABORTED:
3637fb4a67d2SMichael Tuexen 		if (((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_WAIT) ||
3638fb4a67d2SMichael Tuexen 		    ((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_ECHOED)) {
3639410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 0, so_locked);
3640c105859eSRandall Stewart 		} else {
3641410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 0, so_locked);
3642410a3b1eSMichael Tuexen 		}
3643410a3b1eSMichael Tuexen 		break;
3644410a3b1eSMichael Tuexen 	case SCTP_NOTIFY_ASSOC_REM_ABORTED:
3645fb4a67d2SMichael Tuexen 		if (((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_WAIT) ||
3646fb4a67d2SMichael Tuexen 		    ((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_ECHOED)) {
3647410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 1, so_locked);
3648410a3b1eSMichael Tuexen 		} else {
3649410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 1, so_locked);
3650c105859eSRandall Stewart 		}
3651f8829a4aSRandall Stewart 		break;
3652f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_RESTART:
3653410a3b1eSMichael Tuexen 		sctp_notify_assoc_change(SCTP_RESTART, stcb, error, NULL, 0, so_locked);
3654c79bec9cSMichael Tuexen 		if (stcb->asoc.auth_supported == 0) {
3655830d754dSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3656830d754dSRandall Stewart 			    NULL, so_locked);
3657830d754dSRandall Stewart 		}
3658f8829a4aSRandall Stewart 		break;
3659f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_SEND:
3660d7714577SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STREAM_RESET_OUTGOING_SSN);
3661f8829a4aSRandall Stewart 		break;
3662f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_RECV:
3663d7714577SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STREAM_RESET_INCOMING);
3664f8829a4aSRandall Stewart 		break;
3665f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_FAILED_OUT:
3666c4e848b7SRandall Stewart 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
3667d7714577SMichael Tuexen 		    (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_FAILED));
3668f8829a4aSRandall Stewart 		break;
3669d4260646SMichael Tuexen 	case SCTP_NOTIFY_STR_RESET_DENIED_OUT:
3670d4260646SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
3671d4260646SMichael Tuexen 		    (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_DENIED));
3672d4260646SMichael Tuexen 		break;
3673f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_FAILED_IN:
3674c4e848b7SRandall Stewart 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
3675d7714577SMichael Tuexen 		    (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_FAILED));
3676f8829a4aSRandall Stewart 		break;
3677d4260646SMichael Tuexen 	case SCTP_NOTIFY_STR_RESET_DENIED_IN:
3678d4260646SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
3679d4260646SMichael Tuexen 		    (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_DENIED));
3680d4260646SMichael Tuexen 		break;
3681f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_ADD_IP:
3682f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_ADDED, data,
3683f8829a4aSRandall Stewart 		    error);
3684f8829a4aSRandall Stewart 		break;
3685f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_DELETE_IP:
3686f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_REMOVED, data,
3687f8829a4aSRandall Stewart 		    error);
3688f8829a4aSRandall Stewart 		break;
3689f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_SET_PRIMARY:
3690f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_MADE_PRIM, data,
3691f8829a4aSRandall Stewart 		    error);
3692f8829a4aSRandall Stewart 		break;
3693f8829a4aSRandall Stewart 	case SCTP_NOTIFY_PEER_SHUTDOWN:
3694f8829a4aSRandall Stewart 		sctp_notify_shutdown_event(stcb);
3695f8829a4aSRandall Stewart 		break;
3696f8829a4aSRandall Stewart 	case SCTP_NOTIFY_AUTH_NEW_KEY:
369778f28045SMichael Tuexen 		sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY, error,
3698830d754dSRandall Stewart 		    (uint16_t) (uintptr_t) data,
3699830d754dSRandall Stewart 		    so_locked);
3700f8829a4aSRandall Stewart 		break;
3701830d754dSRandall Stewart 	case SCTP_NOTIFY_AUTH_FREE_KEY:
3702830d754dSRandall Stewart 		sctp_notify_authentication(stcb, SCTP_AUTH_FREE_KEY, error,
3703830d754dSRandall Stewart 		    (uint16_t) (uintptr_t) data,
3704830d754dSRandall Stewart 		    so_locked);
3705f8829a4aSRandall Stewart 		break;
3706830d754dSRandall Stewart 	case SCTP_NOTIFY_NO_PEER_AUTH:
3707830d754dSRandall Stewart 		sctp_notify_authentication(stcb, SCTP_AUTH_NO_AUTH, error,
3708830d754dSRandall Stewart 		    (uint16_t) (uintptr_t) data,
3709830d754dSRandall Stewart 		    so_locked);
3710830d754dSRandall Stewart 		break;
3711830d754dSRandall Stewart 	case SCTP_NOTIFY_SENDER_DRY:
3712830d754dSRandall Stewart 		sctp_notify_sender_dry_event(stcb, so_locked);
3713830d754dSRandall Stewart 		break;
3714389b1b11SMichael Tuexen 	case SCTP_NOTIFY_REMOTE_ERROR:
3715389b1b11SMichael Tuexen 		sctp_notify_remote_error(stcb, error, data);
3716389b1b11SMichael Tuexen 		break;
3717f8829a4aSRandall Stewart 	default:
3718ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_UTIL1, "%s: unknown notification %xh (%u)\n",
3719ad81507eSRandall Stewart 		    __FUNCTION__, notification, notification);
3720f8829a4aSRandall Stewart 		break;
3721f8829a4aSRandall Stewart 	}			/* end switch */
3722f8829a4aSRandall Stewart }
3723f8829a4aSRandall Stewart 
3724f8829a4aSRandall Stewart void
37251edc9dbaSMichael Tuexen sctp_report_all_outbound(struct sctp_tcb *stcb, uint16_t error, int holds_lock, int so_locked
3726ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3727ceaad40aSRandall Stewart     SCTP_UNUSED
3728ceaad40aSRandall Stewart #endif
3729ceaad40aSRandall Stewart )
3730f8829a4aSRandall Stewart {
3731f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3732f8829a4aSRandall Stewart 	struct sctp_stream_out *outs;
37334a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
37344a9ef3f8SMichael Tuexen 	struct sctp_stream_queue_pending *sp, *nsp;
37357f34832bSRandall Stewart 	int i;
3736f8829a4aSRandall Stewart 
3737ad81507eSRandall Stewart 	if (stcb == NULL) {
3738ad81507eSRandall Stewart 		return;
3739ad81507eSRandall Stewart 	}
37404a9ef3f8SMichael Tuexen 	asoc = &stcb->asoc;
37414a9ef3f8SMichael Tuexen 	if (asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3742478fbccbSRandall Stewart 		/* already being freed */
3743478fbccbSRandall Stewart 		return;
3744478fbccbSRandall Stewart 	}
3745f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3746f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
37474a9ef3f8SMichael Tuexen 	    (asoc->state & SCTP_STATE_CLOSED_SOCKET)) {
3748f8829a4aSRandall Stewart 		return;
3749f8829a4aSRandall Stewart 	}
3750f8829a4aSRandall Stewart 	/* now through all the gunk freeing chunks */
3751ad81507eSRandall Stewart 	if (holds_lock == 0) {
37527f34832bSRandall Stewart 		SCTP_TCB_SEND_LOCK(stcb);
3753ad81507eSRandall Stewart 	}
3754d00aff5dSRandall Stewart 	/* sent queue SHOULD be empty */
37554a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
3756d00aff5dSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3757d00aff5dSRandall Stewart 		asoc->sent_queue_cnt--;
3758325c8c46SMichael Tuexen 		if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
3759a7ad6026SMichael Tuexen 			if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) {
3760a7ad6026SMichael Tuexen 				asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--;
3761a7ad6026SMichael Tuexen #ifdef INVARIANTS
3762a7ad6026SMichael Tuexen 			} else {
3763a7ad6026SMichael Tuexen 				panic("No chunks on the queues for sid %u.", chk->rec.data.stream_number);
3764a7ad6026SMichael Tuexen #endif
3765a7ad6026SMichael Tuexen 			}
3766a7ad6026SMichael Tuexen 		}
37670c0982b8SRandall Stewart 		if (chk->data != NULL) {
3768d00aff5dSRandall Stewart 			sctp_free_bufspace(stcb, asoc, chk, 1);
37691edc9dbaSMichael Tuexen 			sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
37701edc9dbaSMichael Tuexen 			    error, chk, so_locked);
3771810ec536SMichael Tuexen 			if (chk->data) {
3772d00aff5dSRandall Stewart 				sctp_m_freem(chk->data);
3773d00aff5dSRandall Stewart 				chk->data = NULL;
3774d00aff5dSRandall Stewart 			}
3775810ec536SMichael Tuexen 		}
3776689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, so_locked);
3777d00aff5dSRandall Stewart 		/* sa_ignore FREED_MEMORY */
3778d00aff5dSRandall Stewart 	}
3779d00aff5dSRandall Stewart 	/* pending send queue SHOULD be empty */
37804a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
3781d00aff5dSRandall Stewart 		TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3782d00aff5dSRandall Stewart 		asoc->send_queue_cnt--;
3783a7ad6026SMichael Tuexen 		if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) {
3784a7ad6026SMichael Tuexen 			asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--;
3785a7ad6026SMichael Tuexen #ifdef INVARIANTS
3786a7ad6026SMichael Tuexen 		} else {
3787a7ad6026SMichael Tuexen 			panic("No chunks on the queues for sid %u.", chk->rec.data.stream_number);
3788a7ad6026SMichael Tuexen #endif
3789a7ad6026SMichael Tuexen 		}
37900c0982b8SRandall Stewart 		if (chk->data != NULL) {
3791d00aff5dSRandall Stewart 			sctp_free_bufspace(stcb, asoc, chk, 1);
37921edc9dbaSMichael Tuexen 			sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
37931edc9dbaSMichael Tuexen 			    error, chk, so_locked);
3794810ec536SMichael Tuexen 			if (chk->data) {
3795d00aff5dSRandall Stewart 				sctp_m_freem(chk->data);
3796d00aff5dSRandall Stewart 				chk->data = NULL;
3797d00aff5dSRandall Stewart 			}
3798810ec536SMichael Tuexen 		}
3799689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, so_locked);
3800d00aff5dSRandall Stewart 		/* sa_ignore FREED_MEMORY */
3801d00aff5dSRandall Stewart 	}
38024a9ef3f8SMichael Tuexen 	for (i = 0; i < asoc->streamoutcnt; i++) {
38037f34832bSRandall Stewart 		/* For each stream */
38044a9ef3f8SMichael Tuexen 		outs = &asoc->strmout[i];
38057f34832bSRandall Stewart 		/* clean up any sends there */
38064a9ef3f8SMichael Tuexen 		asoc->locked_on_sending = NULL;
38074a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
38084a9ef3f8SMichael Tuexen 			asoc->stream_queue_cnt--;
3809f8829a4aSRandall Stewart 			TAILQ_REMOVE(&outs->outqueue, sp, next);
3810f8829a4aSRandall Stewart 			sctp_free_spbufspace(stcb, asoc, sp);
3811478fbccbSRandall Stewart 			if (sp->data) {
3812f8829a4aSRandall Stewart 				sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
38131edc9dbaSMichael Tuexen 				    error, (void *)sp, so_locked);
3814f8829a4aSRandall Stewart 				if (sp->data) {
3815f8829a4aSRandall Stewart 					sctp_m_freem(sp->data);
3816f8829a4aSRandall Stewart 					sp->data = NULL;
3817d07b2ac6SMichael Tuexen 					sp->tail_mbuf = NULL;
3818d07b2ac6SMichael Tuexen 					sp->length = 0;
3819f8829a4aSRandall Stewart 				}
3820478fbccbSRandall Stewart 			}
38219eea4a2dSMichael Tuexen 			if (sp->net) {
3822f8829a4aSRandall Stewart 				sctp_free_remote_addr(sp->net);
3823f8829a4aSRandall Stewart 				sp->net = NULL;
38249eea4a2dSMichael Tuexen 			}
3825f8829a4aSRandall Stewart 			/* Free the chunk */
3826689e6a5fSMichael Tuexen 			sctp_free_a_strmoq(stcb, sp, so_locked);
38273c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
3828f8829a4aSRandall Stewart 		}
3829f8829a4aSRandall Stewart 	}
3830f8829a4aSRandall Stewart 
3831ad81507eSRandall Stewart 	if (holds_lock == 0) {
38327f34832bSRandall Stewart 		SCTP_TCB_SEND_UNLOCK(stcb);
3833f8829a4aSRandall Stewart 	}
3834ad81507eSRandall Stewart }
3835f8829a4aSRandall Stewart 
3836f8829a4aSRandall Stewart void
3837410a3b1eSMichael Tuexen sctp_abort_notification(struct sctp_tcb *stcb, uint8_t from_peer, uint16_t error,
3838a2b42326SMichael Tuexen     struct sctp_abort_chunk *abort, int so_locked
3839ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3840ceaad40aSRandall Stewart     SCTP_UNUSED
3841ceaad40aSRandall Stewart #endif
3842ceaad40aSRandall Stewart )
3843f8829a4aSRandall Stewart {
3844ad81507eSRandall Stewart 	if (stcb == NULL) {
3845ad81507eSRandall Stewart 		return;
3846ad81507eSRandall Stewart 	}
3847c55b70ceSMichael Tuexen 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3848c55b70ceSMichael Tuexen 	    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3849c55b70ceSMichael Tuexen 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
3850c55b70ceSMichael Tuexen 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_WAS_ABORTED;
3851c55b70ceSMichael Tuexen 	}
3852f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3853f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3854f8829a4aSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3855f8829a4aSRandall Stewart 		return;
3856f8829a4aSRandall Stewart 	}
3857f8829a4aSRandall Stewart 	/* Tell them we lost the asoc */
38581edc9dbaSMichael Tuexen 	sctp_report_all_outbound(stcb, error, 1, so_locked);
3859410a3b1eSMichael Tuexen 	if (from_peer) {
3860410a3b1eSMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_REM_ABORTED, stcb, error, abort, so_locked);
3861410a3b1eSMichael Tuexen 	} else {
3862410a3b1eSMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_LOC_ABORTED, stcb, error, abort, so_locked);
3863410a3b1eSMichael Tuexen 	}
3864f8829a4aSRandall Stewart }
3865f8829a4aSRandall Stewart 
3866f8829a4aSRandall Stewart void
3867f8829a4aSRandall Stewart sctp_abort_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3868b1754ad1SMichael Tuexen     struct mbuf *m, int iphlen,
3869b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
3870b1754ad1SMichael Tuexen     struct sctphdr *sh, struct mbuf *op_err,
3871457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
3872c54a18d2SRandall Stewart     uint32_t vrf_id, uint16_t port)
3873f8829a4aSRandall Stewart {
3874f8829a4aSRandall Stewart 	uint32_t vtag;
3875f8829a4aSRandall Stewart 
3876ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3877ceaad40aSRandall Stewart 	struct socket *so;
3878ceaad40aSRandall Stewart 
3879ceaad40aSRandall Stewart #endif
3880ceaad40aSRandall Stewart 
3881f8829a4aSRandall Stewart 	vtag = 0;
3882f8829a4aSRandall Stewart 	if (stcb != NULL) {
3883f8829a4aSRandall Stewart 		/* We have a TCB to abort, send notification too */
3884f8829a4aSRandall Stewart 		vtag = stcb->asoc.peer_vtag;
3885410a3b1eSMichael Tuexen 		sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
388617205eccSRandall Stewart 		/* get the assoc vrf id and table id */
388717205eccSRandall Stewart 		vrf_id = stcb->asoc.vrf_id;
388863981c2bSRandall Stewart 		stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
3889f8829a4aSRandall Stewart 	}
3890b1754ad1SMichael Tuexen 	sctp_send_abort(m, iphlen, src, dst, sh, vtag, op_err,
3891457b4b88SMichael Tuexen 	    mflowtype, mflowid,
3892f30ac432SMichael Tuexen 	    vrf_id, port);
3893f8829a4aSRandall Stewart 	if (stcb != NULL) {
3894f8829a4aSRandall Stewart 		/* Ok, now lets free it */
3895ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3896ceaad40aSRandall Stewart 		so = SCTP_INP_SO(inp);
3897ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
3898ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3899ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
3900ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
3901ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
3902ceaad40aSRandall Stewart #endif
39030271d0cdSMichael Tuexen 		SCTP_STAT_INCR_COUNTER32(sctps_aborted);
39040271d0cdSMichael Tuexen 		if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
39050271d0cdSMichael Tuexen 		    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
39060271d0cdSMichael Tuexen 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
39070271d0cdSMichael Tuexen 		}
3908c4739e2fSRandall Stewart 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_4);
3909ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3910ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
3911ceaad40aSRandall Stewart #endif
3912f8829a4aSRandall Stewart 	}
3913f8829a4aSRandall Stewart }
3914f8829a4aSRandall Stewart 
3915f1f73e57SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
3916f1f73e57SRandall Stewart void
3917f1f73e57SRandall Stewart sctp_print_out_track_log(struct sctp_tcb *stcb)
3918f1f73e57SRandall Stewart {
391918e198d3SRandall Stewart #ifdef NOSIY_PRINTS
3920f1f73e57SRandall Stewart 	int i;
3921f1f73e57SRandall Stewart 
3922ad81507eSRandall Stewart 	SCTP_PRINTF("Last ep reason:%x\n", stcb->sctp_ep->last_abort_code);
3923ad81507eSRandall Stewart 	SCTP_PRINTF("IN bound TSN log-aaa\n");
3924f1f73e57SRandall Stewart 	if ((stcb->asoc.tsn_in_at == 0) && (stcb->asoc.tsn_in_wrapped == 0)) {
3925ad81507eSRandall Stewart 		SCTP_PRINTF("None rcvd\n");
3926f1f73e57SRandall Stewart 		goto none_in;
3927f1f73e57SRandall Stewart 	}
3928f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_in_wrapped) {
3929f1f73e57SRandall Stewart 		for (i = stcb->asoc.tsn_in_at; i < SCTP_TSN_LOG_SIZE; i++) {
3930ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3931f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].tsn,
3932f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].strm,
3933f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].seq,
3934f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].flgs,
3935f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].sz);
3936f1f73e57SRandall Stewart 		}
3937f1f73e57SRandall Stewart 	}
3938f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_in_at) {
3939f1f73e57SRandall Stewart 		for (i = 0; i < stcb->asoc.tsn_in_at; i++) {
3940ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3941f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].tsn,
3942f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].strm,
3943f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].seq,
3944f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].flgs,
3945f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].sz);
3946f1f73e57SRandall Stewart 		}
3947f1f73e57SRandall Stewart 	}
3948f1f73e57SRandall Stewart none_in:
3949ad81507eSRandall Stewart 	SCTP_PRINTF("OUT bound TSN log-aaa\n");
3950ad81507eSRandall Stewart 	if ((stcb->asoc.tsn_out_at == 0) &&
3951ad81507eSRandall Stewart 	    (stcb->asoc.tsn_out_wrapped == 0)) {
3952ad81507eSRandall Stewart 		SCTP_PRINTF("None sent\n");
3953f1f73e57SRandall Stewart 	}
3954f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_out_wrapped) {
3955f1f73e57SRandall Stewart 		for (i = stcb->asoc.tsn_out_at; i < SCTP_TSN_LOG_SIZE; i++) {
3956ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3957f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].tsn,
3958f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].strm,
3959f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].seq,
3960f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].flgs,
3961f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].sz);
3962f1f73e57SRandall Stewart 		}
3963f1f73e57SRandall Stewart 	}
3964f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_out_at) {
3965f1f73e57SRandall Stewart 		for (i = 0; i < stcb->asoc.tsn_out_at; i++) {
3966ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3967f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].tsn,
3968f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].strm,
3969f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].seq,
3970f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].flgs,
3971f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].sz);
3972f1f73e57SRandall Stewart 		}
3973f1f73e57SRandall Stewart 	}
397418e198d3SRandall Stewart #endif
3975f1f73e57SRandall Stewart }
3976f1f73e57SRandall Stewart 
3977f1f73e57SRandall Stewart #endif
3978f1f73e57SRandall Stewart 
3979f8829a4aSRandall Stewart void
3980f8829a4aSRandall Stewart sctp_abort_an_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3981a2b42326SMichael Tuexen     struct mbuf *op_err,
3982ceaad40aSRandall Stewart     int so_locked
3983ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3984ceaad40aSRandall Stewart     SCTP_UNUSED
3985ceaad40aSRandall Stewart #endif
3986ceaad40aSRandall Stewart )
3987f8829a4aSRandall Stewart {
3988ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3989ceaad40aSRandall Stewart 	struct socket *so;
3990ceaad40aSRandall Stewart 
3991ceaad40aSRandall Stewart #endif
3992ceaad40aSRandall Stewart 
3993ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3994ceaad40aSRandall Stewart 	so = SCTP_INP_SO(inp);
3995ceaad40aSRandall Stewart #endif
3996f8829a4aSRandall Stewart 	if (stcb == NULL) {
3997f8829a4aSRandall Stewart 		/* Got to have a TCB */
3998f8829a4aSRandall Stewart 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3999fe1831e0SMichael Tuexen 			if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4000b0552ae2SRandall Stewart 				sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4001b0552ae2SRandall Stewart 				    SCTP_CALLED_DIRECTLY_NOCMPSET);
4002f8829a4aSRandall Stewart 			}
4003f8829a4aSRandall Stewart 		}
4004f8829a4aSRandall Stewart 		return;
400563981c2bSRandall Stewart 	} else {
400663981c2bSRandall Stewart 		stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
4007f8829a4aSRandall Stewart 	}
4008f8829a4aSRandall Stewart 	/* notify the ulp */
4009a2b42326SMichael Tuexen 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
4010410a3b1eSMichael Tuexen 		sctp_abort_notification(stcb, 0, 0, NULL, so_locked);
4011a2b42326SMichael Tuexen 	}
4012f8829a4aSRandall Stewart 	/* notify the peer */
4013ceaad40aSRandall Stewart 	sctp_send_abort_tcb(stcb, op_err, so_locked);
4014f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4015f8829a4aSRandall Stewart 	if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
4016f8829a4aSRandall Stewart 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4017f8829a4aSRandall Stewart 		SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4018f8829a4aSRandall Stewart 	}
4019f8829a4aSRandall Stewart 	/* now free the asoc */
4020f1f73e57SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
4021f1f73e57SRandall Stewart 	sctp_print_out_track_log(stcb);
4022f1f73e57SRandall Stewart #endif
4023ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4024ceaad40aSRandall Stewart 	if (!so_locked) {
4025ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
4026ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
4027ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
4028ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4029ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4030ceaad40aSRandall Stewart 	}
4031ceaad40aSRandall Stewart #endif
4032c4739e2fSRandall Stewart 	(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_5);
4033ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4034ceaad40aSRandall Stewart 	if (!so_locked) {
4035ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4036ceaad40aSRandall Stewart 	}
4037ceaad40aSRandall Stewart #endif
4038f8829a4aSRandall Stewart }
4039f8829a4aSRandall Stewart 
4040f8829a4aSRandall Stewart void
4041b1754ad1SMichael Tuexen sctp_handle_ootb(struct mbuf *m, int iphlen, int offset,
4042b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
4043b1754ad1SMichael Tuexen     struct sctphdr *sh, struct sctp_inpcb *inp,
4044ff1ffd74SMichael Tuexen     struct mbuf *cause,
4045457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
4046f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
4047f8829a4aSRandall Stewart {
4048f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch, chunk_buf;
4049f8829a4aSRandall Stewart 	unsigned int chk_length;
4050c58e60beSMichael Tuexen 	int contains_init_chunk;
4051f8829a4aSRandall Stewart 
4052f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue);
4053f8829a4aSRandall Stewart 	/* Generate a TO address for future reference */
4054f8829a4aSRandall Stewart 	if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4055fe1831e0SMichael Tuexen 		if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4056b0552ae2SRandall Stewart 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4057b0552ae2SRandall Stewart 			    SCTP_CALLED_DIRECTLY_NOCMPSET);
4058f8829a4aSRandall Stewart 		}
4059f8829a4aSRandall Stewart 	}
4060c58e60beSMichael Tuexen 	contains_init_chunk = 0;
4061f8829a4aSRandall Stewart 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4062f8829a4aSRandall Stewart 	    sizeof(*ch), (uint8_t *) & chunk_buf);
4063f8829a4aSRandall Stewart 	while (ch != NULL) {
4064f8829a4aSRandall Stewart 		chk_length = ntohs(ch->chunk_length);
4065f8829a4aSRandall Stewart 		if (chk_length < sizeof(*ch)) {
4066f8829a4aSRandall Stewart 			/* break to abort land */
4067f8829a4aSRandall Stewart 			break;
4068f8829a4aSRandall Stewart 		}
4069f8829a4aSRandall Stewart 		switch (ch->chunk_type) {
4070c58e60beSMichael Tuexen 		case SCTP_INIT:
4071c58e60beSMichael Tuexen 			contains_init_chunk = 1;
4072c58e60beSMichael Tuexen 			break;
4073f8829a4aSRandall Stewart 		case SCTP_PACKET_DROPPED:
4074f8829a4aSRandall Stewart 			/* we don't respond to pkt-dropped */
4075f8829a4aSRandall Stewart 			return;
4076f8829a4aSRandall Stewart 		case SCTP_ABORT_ASSOCIATION:
4077f8829a4aSRandall Stewart 			/* we don't respond with an ABORT to an ABORT */
4078f8829a4aSRandall Stewart 			return;
4079f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_COMPLETE:
4080f8829a4aSRandall Stewart 			/*
4081f8829a4aSRandall Stewart 			 * we ignore it since we are not waiting for it and
4082f8829a4aSRandall Stewart 			 * peer is gone
4083f8829a4aSRandall Stewart 			 */
4084f8829a4aSRandall Stewart 			return;
4085f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_ACK:
4086b1754ad1SMichael Tuexen 			sctp_send_shutdown_complete2(src, dst, sh,
4087457b4b88SMichael Tuexen 			    mflowtype, mflowid,
4088f30ac432SMichael Tuexen 			    vrf_id, port);
4089f8829a4aSRandall Stewart 			return;
4090f8829a4aSRandall Stewart 		default:
4091f8829a4aSRandall Stewart 			break;
4092f8829a4aSRandall Stewart 		}
4093f8829a4aSRandall Stewart 		offset += SCTP_SIZE32(chk_length);
4094f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4095f8829a4aSRandall Stewart 		    sizeof(*ch), (uint8_t *) & chunk_buf);
4096f8829a4aSRandall Stewart 	}
4097c58e60beSMichael Tuexen 	if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
4098c58e60beSMichael Tuexen 	    ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
4099c58e60beSMichael Tuexen 	    (contains_init_chunk == 0))) {
4100ff1ffd74SMichael Tuexen 		sctp_send_abort(m, iphlen, src, dst, sh, 0, cause,
4101457b4b88SMichael Tuexen 		    mflowtype, mflowid,
4102f30ac432SMichael Tuexen 		    vrf_id, port);
4103f8829a4aSRandall Stewart 	}
4104c58e60beSMichael Tuexen }
4105f8829a4aSRandall Stewart 
4106f8829a4aSRandall Stewart /*
4107f8829a4aSRandall Stewart  * check the inbound datagram to make sure there is not an abort inside it,
4108f8829a4aSRandall Stewart  * if there is return 1, else return 0.
4109f8829a4aSRandall Stewart  */
4110f8829a4aSRandall Stewart int
4111f8829a4aSRandall Stewart sctp_is_there_an_abort_here(struct mbuf *m, int iphlen, uint32_t * vtagfill)
4112f8829a4aSRandall Stewart {
4113f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
4114f8829a4aSRandall Stewart 	struct sctp_init_chunk *init_chk, chunk_buf;
4115f8829a4aSRandall Stewart 	int offset;
4116f8829a4aSRandall Stewart 	unsigned int chk_length;
4117f8829a4aSRandall Stewart 
4118f8829a4aSRandall Stewart 	offset = iphlen + sizeof(struct sctphdr);
4119f8829a4aSRandall Stewart 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch),
4120f8829a4aSRandall Stewart 	    (uint8_t *) & chunk_buf);
4121f8829a4aSRandall Stewart 	while (ch != NULL) {
4122f8829a4aSRandall Stewart 		chk_length = ntohs(ch->chunk_length);
4123f8829a4aSRandall Stewart 		if (chk_length < sizeof(*ch)) {
4124f8829a4aSRandall Stewart 			/* packet is probably corrupt */
4125f8829a4aSRandall Stewart 			break;
4126f8829a4aSRandall Stewart 		}
4127f8829a4aSRandall Stewart 		/* we seem to be ok, is it an abort? */
4128f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) {
4129f8829a4aSRandall Stewart 			/* yep, tell them */
4130f8829a4aSRandall Stewart 			return (1);
4131f8829a4aSRandall Stewart 		}
4132f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_INITIATION) {
4133f8829a4aSRandall Stewart 			/* need to update the Vtag */
4134f8829a4aSRandall Stewart 			init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4135f8829a4aSRandall Stewart 			    offset, sizeof(*init_chk), (uint8_t *) & chunk_buf);
4136f8829a4aSRandall Stewart 			if (init_chk != NULL) {
4137f8829a4aSRandall Stewart 				*vtagfill = ntohl(init_chk->init.initiate_tag);
4138f8829a4aSRandall Stewart 			}
4139f8829a4aSRandall Stewart 		}
4140f8829a4aSRandall Stewart 		/* Nope, move to the next chunk */
4141f8829a4aSRandall Stewart 		offset += SCTP_SIZE32(chk_length);
4142f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4143f8829a4aSRandall Stewart 		    sizeof(*ch), (uint8_t *) & chunk_buf);
4144f8829a4aSRandall Stewart 	}
4145f8829a4aSRandall Stewart 	return (0);
4146f8829a4aSRandall Stewart }
4147f8829a4aSRandall Stewart 
4148f8829a4aSRandall Stewart /*
4149f8829a4aSRandall Stewart  * currently (2/02), ifa_addr embeds scope_id's and don't have sin6_scope_id
4150f8829a4aSRandall Stewart  * set (i.e. it's 0) so, create this function to compare link local scopes
4151f8829a4aSRandall Stewart  */
41525e2c2d87SRandall Stewart #ifdef INET6
4153f8829a4aSRandall Stewart uint32_t
4154f8829a4aSRandall Stewart sctp_is_same_scope(struct sockaddr_in6 *addr1, struct sockaddr_in6 *addr2)
4155f8829a4aSRandall Stewart {
4156f8829a4aSRandall Stewart 	struct sockaddr_in6 a, b;
4157f8829a4aSRandall Stewart 
4158f8829a4aSRandall Stewart 	/* save copies */
4159f8829a4aSRandall Stewart 	a = *addr1;
4160f8829a4aSRandall Stewart 	b = *addr2;
4161f8829a4aSRandall Stewart 
4162f8829a4aSRandall Stewart 	if (a.sin6_scope_id == 0)
4163f8829a4aSRandall Stewart 		if (sa6_recoverscope(&a)) {
4164f8829a4aSRandall Stewart 			/* can't get scope, so can't match */
4165f8829a4aSRandall Stewart 			return (0);
4166f8829a4aSRandall Stewart 		}
4167f8829a4aSRandall Stewart 	if (b.sin6_scope_id == 0)
4168f8829a4aSRandall Stewart 		if (sa6_recoverscope(&b)) {
4169f8829a4aSRandall Stewart 			/* can't get scope, so can't match */
4170f8829a4aSRandall Stewart 			return (0);
4171f8829a4aSRandall Stewart 		}
4172f8829a4aSRandall Stewart 	if (a.sin6_scope_id != b.sin6_scope_id)
4173f8829a4aSRandall Stewart 		return (0);
4174f8829a4aSRandall Stewart 
4175f8829a4aSRandall Stewart 	return (1);
4176f8829a4aSRandall Stewart }
4177f8829a4aSRandall Stewart 
4178f8829a4aSRandall Stewart /*
4179f8829a4aSRandall Stewart  * returns a sockaddr_in6 with embedded scope recovered and removed
4180f8829a4aSRandall Stewart  */
4181f8829a4aSRandall Stewart struct sockaddr_in6 *
4182f8829a4aSRandall Stewart sctp_recover_scope(struct sockaddr_in6 *addr, struct sockaddr_in6 *store)
4183f8829a4aSRandall Stewart {
4184f8829a4aSRandall Stewart 	/* check and strip embedded scope junk */
4185f8829a4aSRandall Stewart 	if (addr->sin6_family == AF_INET6) {
4186f8829a4aSRandall Stewart 		if (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr)) {
4187f8829a4aSRandall Stewart 			if (addr->sin6_scope_id == 0) {
4188f8829a4aSRandall Stewart 				*store = *addr;
4189f8829a4aSRandall Stewart 				if (!sa6_recoverscope(store)) {
4190f8829a4aSRandall Stewart 					/* use the recovered scope */
4191f8829a4aSRandall Stewart 					addr = store;
4192f8829a4aSRandall Stewart 				}
4193f42a358aSRandall Stewart 			} else {
4194f8829a4aSRandall Stewart 				/* else, return the original "to" addr */
4195f42a358aSRandall Stewart 				in6_clearscope(&addr->sin6_addr);
4196f8829a4aSRandall Stewart 			}
4197f8829a4aSRandall Stewart 		}
4198f8829a4aSRandall Stewart 	}
4199f8829a4aSRandall Stewart 	return (addr);
4200f8829a4aSRandall Stewart }
4201f8829a4aSRandall Stewart 
42025e2c2d87SRandall Stewart #endif
42035e2c2d87SRandall Stewart 
4204f8829a4aSRandall Stewart /*
4205f8829a4aSRandall Stewart  * are the two addresses the same?  currently a "scopeless" check returns: 1
4206f8829a4aSRandall Stewart  * if same, 0 if not
4207f8829a4aSRandall Stewart  */
420872fb6fdbSRandall Stewart int
4209f8829a4aSRandall Stewart sctp_cmpaddr(struct sockaddr *sa1, struct sockaddr *sa2)
4210f8829a4aSRandall Stewart {
4211f8829a4aSRandall Stewart 
4212f8829a4aSRandall Stewart 	/* must be valid */
4213f8829a4aSRandall Stewart 	if (sa1 == NULL || sa2 == NULL)
4214f8829a4aSRandall Stewart 		return (0);
4215f8829a4aSRandall Stewart 
4216f8829a4aSRandall Stewart 	/* must be the same family */
4217f8829a4aSRandall Stewart 	if (sa1->sa_family != sa2->sa_family)
4218f8829a4aSRandall Stewart 		return (0);
4219f8829a4aSRandall Stewart 
42205e2c2d87SRandall Stewart 	switch (sa1->sa_family) {
42215e2c2d87SRandall Stewart #ifdef INET6
42225e2c2d87SRandall Stewart 	case AF_INET6:
42235e2c2d87SRandall Stewart 		{
4224f8829a4aSRandall Stewart 			/* IPv6 addresses */
4225f8829a4aSRandall Stewart 			struct sockaddr_in6 *sin6_1, *sin6_2;
4226f8829a4aSRandall Stewart 
4227f8829a4aSRandall Stewart 			sin6_1 = (struct sockaddr_in6 *)sa1;
4228f8829a4aSRandall Stewart 			sin6_2 = (struct sockaddr_in6 *)sa2;
4229c54a18d2SRandall Stewart 			return (SCTP6_ARE_ADDR_EQUAL(sin6_1,
4230c54a18d2SRandall Stewart 			    sin6_2));
42315e2c2d87SRandall Stewart 		}
42325e2c2d87SRandall Stewart #endif
4233ea5eba11SMichael Tuexen #ifdef INET
42345e2c2d87SRandall Stewart 	case AF_INET:
42355e2c2d87SRandall Stewart 		{
4236f8829a4aSRandall Stewart 			/* IPv4 addresses */
4237f8829a4aSRandall Stewart 			struct sockaddr_in *sin_1, *sin_2;
4238f8829a4aSRandall Stewart 
4239f8829a4aSRandall Stewart 			sin_1 = (struct sockaddr_in *)sa1;
4240f8829a4aSRandall Stewart 			sin_2 = (struct sockaddr_in *)sa2;
4241f8829a4aSRandall Stewart 			return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr);
42425e2c2d87SRandall Stewart 		}
4243ea5eba11SMichael Tuexen #endif
42445e2c2d87SRandall Stewart 	default:
4245f8829a4aSRandall Stewart 		/* we don't do these... */
4246f8829a4aSRandall Stewart 		return (0);
4247f8829a4aSRandall Stewart 	}
4248f8829a4aSRandall Stewart }
4249f8829a4aSRandall Stewart 
4250f8829a4aSRandall Stewart void
4251f8829a4aSRandall Stewart sctp_print_address(struct sockaddr *sa)
4252f8829a4aSRandall Stewart {
42535e2c2d87SRandall Stewart #ifdef INET6
42547d32aa0cSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
4255f8829a4aSRandall Stewart 
42565e2c2d87SRandall Stewart #endif
42575e2c2d87SRandall Stewart 
42585e2c2d87SRandall Stewart 	switch (sa->sa_family) {
42595e2c2d87SRandall Stewart #ifdef INET6
42605e2c2d87SRandall Stewart 	case AF_INET6:
42615e2c2d87SRandall Stewart 		{
4262ad81507eSRandall Stewart 			struct sockaddr_in6 *sin6;
4263ad81507eSRandall Stewart 
4264f8829a4aSRandall Stewart 			sin6 = (struct sockaddr_in6 *)sa;
4265ad81507eSRandall Stewart 			SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n",
42667d32aa0cSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &sin6->sin6_addr),
42677d32aa0cSBjoern A. Zeeb 			    ntohs(sin6->sin6_port),
4268f8829a4aSRandall Stewart 			    sin6->sin6_scope_id);
42695e2c2d87SRandall Stewart 			break;
42705e2c2d87SRandall Stewart 		}
42715e2c2d87SRandall Stewart #endif
4272ea5eba11SMichael Tuexen #ifdef INET
42735e2c2d87SRandall Stewart 	case AF_INET:
42745e2c2d87SRandall Stewart 		{
4275f8829a4aSRandall Stewart 			struct sockaddr_in *sin;
4276f8829a4aSRandall Stewart 			unsigned char *p;
4277f8829a4aSRandall Stewart 
4278f8829a4aSRandall Stewart 			sin = (struct sockaddr_in *)sa;
4279f8829a4aSRandall Stewart 			p = (unsigned char *)&sin->sin_addr;
4280ad81507eSRandall Stewart 			SCTP_PRINTF("IPv4 address: %u.%u.%u.%u:%d\n",
4281f8829a4aSRandall Stewart 			    p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
42825e2c2d87SRandall Stewart 			break;
42835e2c2d87SRandall Stewart 		}
4284ea5eba11SMichael Tuexen #endif
42855e2c2d87SRandall Stewart 	default:
4286ad81507eSRandall Stewart 		SCTP_PRINTF("?\n");
42875e2c2d87SRandall Stewart 		break;
4288f8829a4aSRandall Stewart 	}
4289f8829a4aSRandall Stewart }
4290f8829a4aSRandall Stewart 
4291f8829a4aSRandall Stewart void
4292f8829a4aSRandall Stewart sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
4293f8829a4aSRandall Stewart     struct sctp_inpcb *new_inp,
4294d06c82f1SRandall Stewart     struct sctp_tcb *stcb,
4295d06c82f1SRandall Stewart     int waitflags)
4296f8829a4aSRandall Stewart {
4297f8829a4aSRandall Stewart 	/*
4298f8829a4aSRandall Stewart 	 * go through our old INP and pull off any control structures that
4299f8829a4aSRandall Stewart 	 * belong to stcb and move then to the new inp.
4300f8829a4aSRandall Stewart 	 */
4301f8829a4aSRandall Stewart 	struct socket *old_so, *new_so;
4302f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control, *nctl;
4303f8829a4aSRandall Stewart 	struct sctp_readhead tmp_queue;
4304f8829a4aSRandall Stewart 	struct mbuf *m;
4305bff64a4dSRandall Stewart 	int error = 0;
4306f8829a4aSRandall Stewart 
4307f8829a4aSRandall Stewart 	old_so = old_inp->sctp_socket;
4308f8829a4aSRandall Stewart 	new_so = new_inp->sctp_socket;
4309f8829a4aSRandall Stewart 	TAILQ_INIT(&tmp_queue);
4310d06c82f1SRandall Stewart 	error = sblock(&old_so->so_rcv, waitflags);
4311f8829a4aSRandall Stewart 	if (error) {
4312f8829a4aSRandall Stewart 		/*
4313f8829a4aSRandall Stewart 		 * Gak, can't get sblock, we have a problem. data will be
4314f8829a4aSRandall Stewart 		 * left stranded.. and we don't dare look at it since the
4315f8829a4aSRandall Stewart 		 * other thread may be reading something. Oh well, its a
4316f8829a4aSRandall Stewart 		 * screwed up app that does a peeloff OR a accept while
4317f8829a4aSRandall Stewart 		 * reading from the main socket... actually its only the
4318f8829a4aSRandall Stewart 		 * peeloff() case, since I think read will fail on a
4319f8829a4aSRandall Stewart 		 * listening socket..
4320f8829a4aSRandall Stewart 		 */
4321f8829a4aSRandall Stewart 		return;
4322f8829a4aSRandall Stewart 	}
4323f8829a4aSRandall Stewart 	/* lock the socket buffers */
4324f8829a4aSRandall Stewart 	SCTP_INP_READ_LOCK(old_inp);
43254a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(control, &old_inp->read_queue, next, nctl) {
4326f8829a4aSRandall Stewart 		/* Pull off all for out target stcb */
4327f8829a4aSRandall Stewart 		if (control->stcb == stcb) {
4328f8829a4aSRandall Stewart 			/* remove it we want it */
4329f8829a4aSRandall Stewart 			TAILQ_REMOVE(&old_inp->read_queue, control, next);
4330f8829a4aSRandall Stewart 			TAILQ_INSERT_TAIL(&tmp_queue, control, next);
4331f8829a4aSRandall Stewart 			m = control->data;
4332f8829a4aSRandall Stewart 			while (m) {
4333b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4334139bc87fSRandall Stewart 					sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
433580fefe0aSRandall Stewart 				}
4336f8829a4aSRandall Stewart 				sctp_sbfree(control, stcb, &old_so->so_rcv, m);
4337b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4338f8829a4aSRandall Stewart 					sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
433980fefe0aSRandall Stewart 				}
4340139bc87fSRandall Stewart 				m = SCTP_BUF_NEXT(m);
4341f8829a4aSRandall Stewart 			}
4342f8829a4aSRandall Stewart 		}
4343f8829a4aSRandall Stewart 	}
4344f8829a4aSRandall Stewart 	SCTP_INP_READ_UNLOCK(old_inp);
4345f8829a4aSRandall Stewart 	/* Remove the sb-lock on the old socket */
4346f8829a4aSRandall Stewart 
4347f8829a4aSRandall Stewart 	sbunlock(&old_so->so_rcv);
4348f8829a4aSRandall Stewart 	/* Now we move them over to the new socket buffer */
4349f8829a4aSRandall Stewart 	SCTP_INP_READ_LOCK(new_inp);
43504a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(control, &tmp_queue, next, nctl) {
4351f8829a4aSRandall Stewart 		TAILQ_INSERT_TAIL(&new_inp->read_queue, control, next);
4352f8829a4aSRandall Stewart 		m = control->data;
4353f8829a4aSRandall Stewart 		while (m) {
4354b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4355139bc87fSRandall Stewart 				sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
435680fefe0aSRandall Stewart 			}
4357f8829a4aSRandall Stewart 			sctp_sballoc(stcb, &new_so->so_rcv, m);
4358b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4359f8829a4aSRandall Stewart 				sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
436080fefe0aSRandall Stewart 			}
4361139bc87fSRandall Stewart 			m = SCTP_BUF_NEXT(m);
4362f8829a4aSRandall Stewart 		}
4363f8829a4aSRandall Stewart 	}
4364f8829a4aSRandall Stewart 	SCTP_INP_READ_UNLOCK(new_inp);
4365f8829a4aSRandall Stewart }
4366f8829a4aSRandall Stewart 
4367f8829a4aSRandall Stewart void
4368f8829a4aSRandall Stewart sctp_add_to_readq(struct sctp_inpcb *inp,
4369f8829a4aSRandall Stewart     struct sctp_tcb *stcb,
4370f8829a4aSRandall Stewart     struct sctp_queued_to_read *control,
4371f8829a4aSRandall Stewart     struct sockbuf *sb,
4372ceaad40aSRandall Stewart     int end,
4373cfde3ff7SRandall Stewart     int inp_read_lock_held,
4374ceaad40aSRandall Stewart     int so_locked
4375ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4376ceaad40aSRandall Stewart     SCTP_UNUSED
4377ceaad40aSRandall Stewart #endif
4378ceaad40aSRandall Stewart )
4379f8829a4aSRandall Stewart {
4380f8829a4aSRandall Stewart 	/*
4381f8829a4aSRandall Stewart 	 * Here we must place the control on the end of the socket read
43824e88d37aSMichael Tuexen 	 * queue AND increment sb_cc so that select will work properly on
4383f8829a4aSRandall Stewart 	 * read.
4384f8829a4aSRandall Stewart 	 */
4385f8829a4aSRandall Stewart 	struct mbuf *m, *prev = NULL;
4386f8829a4aSRandall Stewart 
438703b0b021SRandall Stewart 	if (inp == NULL) {
438803b0b021SRandall Stewart 		/* Gak, TSNH!! */
4389a5d547adSRandall Stewart #ifdef INVARIANTS
439003b0b021SRandall Stewart 		panic("Gak, inp NULL on add_to_readq");
439103b0b021SRandall Stewart #endif
439203b0b021SRandall Stewart 		return;
439303b0b021SRandall Stewart 	}
4394cfde3ff7SRandall Stewart 	if (inp_read_lock_held == 0)
4395f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(inp);
4396cd1386abSMichael Tuexen 	if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
4397cd1386abSMichael Tuexen 		sctp_free_remote_addr(control->whoFrom);
4398cd1386abSMichael Tuexen 		if (control->data) {
4399cd1386abSMichael Tuexen 			sctp_m_freem(control->data);
4400cd1386abSMichael Tuexen 			control->data = NULL;
4401cd1386abSMichael Tuexen 		}
4402cd1386abSMichael Tuexen 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), control);
4403cd1386abSMichael Tuexen 		if (inp_read_lock_held == 0)
4404cd1386abSMichael Tuexen 			SCTP_INP_READ_UNLOCK(inp);
4405cd1386abSMichael Tuexen 		return;
4406cd1386abSMichael Tuexen 	}
440742551e99SRandall Stewart 	if (!(control->spec_flags & M_NOTIFICATION)) {
4408a5d547adSRandall Stewart 		atomic_add_int(&inp->total_recvs, 1);
440942551e99SRandall Stewart 		if (!control->do_not_ref_stcb) {
4410a5d547adSRandall Stewart 			atomic_add_int(&stcb->total_recvs, 1);
441142551e99SRandall Stewart 		}
441242551e99SRandall Stewart 	}
4413f8829a4aSRandall Stewart 	m = control->data;
4414f8829a4aSRandall Stewart 	control->held_length = 0;
4415f8829a4aSRandall Stewart 	control->length = 0;
4416f8829a4aSRandall Stewart 	while (m) {
4417139bc87fSRandall Stewart 		if (SCTP_BUF_LEN(m) == 0) {
4418f8829a4aSRandall Stewart 			/* Skip mbufs with NO length */
4419f8829a4aSRandall Stewart 			if (prev == NULL) {
4420f8829a4aSRandall Stewart 				/* First one */
4421f8829a4aSRandall Stewart 				control->data = sctp_m_free(m);
4422f8829a4aSRandall Stewart 				m = control->data;
4423f8829a4aSRandall Stewart 			} else {
4424139bc87fSRandall Stewart 				SCTP_BUF_NEXT(prev) = sctp_m_free(m);
4425139bc87fSRandall Stewart 				m = SCTP_BUF_NEXT(prev);
4426f8829a4aSRandall Stewart 			}
4427f8829a4aSRandall Stewart 			if (m == NULL) {
4428c2ede4b3SMartin Blapp 				control->tail_mbuf = prev;
4429f8829a4aSRandall Stewart 			}
4430f8829a4aSRandall Stewart 			continue;
4431f8829a4aSRandall Stewart 		}
4432f8829a4aSRandall Stewart 		prev = m;
4433b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4434139bc87fSRandall Stewart 			sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
443580fefe0aSRandall Stewart 		}
4436f8829a4aSRandall Stewart 		sctp_sballoc(stcb, sb, m);
4437b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4438f8829a4aSRandall Stewart 			sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
443980fefe0aSRandall Stewart 		}
4440139bc87fSRandall Stewart 		atomic_add_int(&control->length, SCTP_BUF_LEN(m));
4441139bc87fSRandall Stewart 		m = SCTP_BUF_NEXT(m);
4442f8829a4aSRandall Stewart 	}
4443f8829a4aSRandall Stewart 	if (prev != NULL) {
4444f8829a4aSRandall Stewart 		control->tail_mbuf = prev;
4445f8829a4aSRandall Stewart 	} else {
4446139bc87fSRandall Stewart 		/* Everything got collapsed out?? */
4447cd1386abSMichael Tuexen 		sctp_free_remote_addr(control->whoFrom);
4448cd1386abSMichael Tuexen 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), control);
4449cfde3ff7SRandall Stewart 		if (inp_read_lock_held == 0)
445047a490cbSMichael Tuexen 			SCTP_INP_READ_UNLOCK(inp);
4451f8829a4aSRandall Stewart 		return;
4452f8829a4aSRandall Stewart 	}
4453f8829a4aSRandall Stewart 	if (end) {
4454f8829a4aSRandall Stewart 		control->end_added = 1;
4455f8829a4aSRandall Stewart 	}
4456f8829a4aSRandall Stewart 	TAILQ_INSERT_TAIL(&inp->read_queue, control, next);
4457cfde3ff7SRandall Stewart 	if (inp_read_lock_held == 0)
4458f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
4459f8829a4aSRandall Stewart 	if (inp && inp->sctp_socket) {
446017205eccSRandall Stewart 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
446117205eccSRandall Stewart 			SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
4462ceaad40aSRandall Stewart 		} else {
4463ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4464ceaad40aSRandall Stewart 			struct socket *so;
4465ceaad40aSRandall Stewart 
4466ceaad40aSRandall Stewart 			so = SCTP_INP_SO(inp);
4467ceaad40aSRandall Stewart 			if (!so_locked) {
446860990c0cSMichael Tuexen 				if (stcb) {
4469ceaad40aSRandall Stewart 					atomic_add_int(&stcb->asoc.refcnt, 1);
4470ceaad40aSRandall Stewart 					SCTP_TCB_UNLOCK(stcb);
447160990c0cSMichael Tuexen 				}
4472ceaad40aSRandall Stewart 				SCTP_SOCKET_LOCK(so, 1);
447360990c0cSMichael Tuexen 				if (stcb) {
4474ceaad40aSRandall Stewart 					SCTP_TCB_LOCK(stcb);
4475ceaad40aSRandall Stewart 					atomic_subtract_int(&stcb->asoc.refcnt, 1);
447660990c0cSMichael Tuexen 				}
4477ceaad40aSRandall Stewart 				if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4478ceaad40aSRandall Stewart 					SCTP_SOCKET_UNLOCK(so, 1);
4479ceaad40aSRandall Stewart 					return;
4480ceaad40aSRandall Stewart 				}
4481ceaad40aSRandall Stewart 			}
4482ceaad40aSRandall Stewart #endif
4483f8829a4aSRandall Stewart 			sctp_sorwakeup(inp, inp->sctp_socket);
4484ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4485ceaad40aSRandall Stewart 			if (!so_locked) {
4486ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
4487ceaad40aSRandall Stewart 			}
4488ceaad40aSRandall Stewart #endif
4489ceaad40aSRandall Stewart 		}
4490f8829a4aSRandall Stewart 	}
4491f8829a4aSRandall Stewart }
4492f8829a4aSRandall Stewart 
4493f8829a4aSRandall Stewart 
4494f8829a4aSRandall Stewart int
4495f8829a4aSRandall Stewart sctp_append_to_readq(struct sctp_inpcb *inp,
4496f8829a4aSRandall Stewart     struct sctp_tcb *stcb,
4497f8829a4aSRandall Stewart     struct sctp_queued_to_read *control,
4498f8829a4aSRandall Stewart     struct mbuf *m,
4499f8829a4aSRandall Stewart     int end,
4500f8829a4aSRandall Stewart     int ctls_cumack,
4501f8829a4aSRandall Stewart     struct sockbuf *sb)
4502f8829a4aSRandall Stewart {
4503f8829a4aSRandall Stewart 	/*
4504f8829a4aSRandall Stewart 	 * A partial delivery API event is underway. OR we are appending on
4505f8829a4aSRandall Stewart 	 * the reassembly queue.
4506f8829a4aSRandall Stewart 	 *
4507f8829a4aSRandall Stewart 	 * If PDAPI this means we need to add m to the end of the data.
45084e88d37aSMichael Tuexen 	 * Increase the length in the control AND increment the sb_cc.
4509f8829a4aSRandall Stewart 	 * Otherwise sb is NULL and all we need to do is put it at the end
4510f8829a4aSRandall Stewart 	 * of the mbuf chain.
4511f8829a4aSRandall Stewart 	 */
4512f8829a4aSRandall Stewart 	int len = 0;
4513f8829a4aSRandall Stewart 	struct mbuf *mm, *tail = NULL, *prev = NULL;
4514f8829a4aSRandall Stewart 
4515f8829a4aSRandall Stewart 	if (inp) {
4516f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(inp);
4517f8829a4aSRandall Stewart 	}
4518f8829a4aSRandall Stewart 	if (control == NULL) {
4519f8829a4aSRandall Stewart get_out:
4520f8829a4aSRandall Stewart 		if (inp) {
4521f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
4522f8829a4aSRandall Stewart 		}
4523f8829a4aSRandall Stewart 		return (-1);
4524f8829a4aSRandall Stewart 	}
4525cd1386abSMichael Tuexen 	if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ)) {
4526cd1386abSMichael Tuexen 		SCTP_INP_READ_UNLOCK(inp);
452760990c0cSMichael Tuexen 		return (0);
4528cd1386abSMichael Tuexen 	}
4529139bc87fSRandall Stewart 	if (control->end_added) {
4530f8829a4aSRandall Stewart 		/* huh this one is complete? */
4531f8829a4aSRandall Stewart 		goto get_out;
4532f8829a4aSRandall Stewart 	}
4533f8829a4aSRandall Stewart 	mm = m;
4534f8829a4aSRandall Stewart 	if (mm == NULL) {
4535f8829a4aSRandall Stewart 		goto get_out;
4536f8829a4aSRandall Stewart 	}
4537f8829a4aSRandall Stewart 	while (mm) {
4538139bc87fSRandall Stewart 		if (SCTP_BUF_LEN(mm) == 0) {
4539f8829a4aSRandall Stewart 			/* Skip mbufs with NO lenght */
4540f8829a4aSRandall Stewart 			if (prev == NULL) {
4541f8829a4aSRandall Stewart 				/* First one */
4542f8829a4aSRandall Stewart 				m = sctp_m_free(mm);
4543f8829a4aSRandall Stewart 				mm = m;
4544f8829a4aSRandall Stewart 			} else {
4545139bc87fSRandall Stewart 				SCTP_BUF_NEXT(prev) = sctp_m_free(mm);
4546139bc87fSRandall Stewart 				mm = SCTP_BUF_NEXT(prev);
4547f8829a4aSRandall Stewart 			}
4548f8829a4aSRandall Stewart 			continue;
4549f8829a4aSRandall Stewart 		}
4550f8829a4aSRandall Stewart 		prev = mm;
4551139bc87fSRandall Stewart 		len += SCTP_BUF_LEN(mm);
4552f8829a4aSRandall Stewart 		if (sb) {
4553b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4554139bc87fSRandall Stewart 				sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(mm));
455580fefe0aSRandall Stewart 			}
4556f8829a4aSRandall Stewart 			sctp_sballoc(stcb, sb, mm);
4557b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4558f8829a4aSRandall Stewart 				sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
455980fefe0aSRandall Stewart 			}
4560f8829a4aSRandall Stewart 		}
4561139bc87fSRandall Stewart 		mm = SCTP_BUF_NEXT(mm);
4562f8829a4aSRandall Stewart 	}
4563f8829a4aSRandall Stewart 	if (prev) {
4564f8829a4aSRandall Stewart 		tail = prev;
4565f8829a4aSRandall Stewart 	} else {
4566f8829a4aSRandall Stewart 		/* Really there should always be a prev */
4567f8829a4aSRandall Stewart 		if (m == NULL) {
4568f8829a4aSRandall Stewart 			/* Huh nothing left? */
4569a5d547adSRandall Stewart #ifdef INVARIANTS
4570f8829a4aSRandall Stewart 			panic("Nothing left to add?");
4571f8829a4aSRandall Stewart #else
4572f8829a4aSRandall Stewart 			goto get_out;
4573f8829a4aSRandall Stewart #endif
4574f8829a4aSRandall Stewart 		}
4575f8829a4aSRandall Stewart 		tail = m;
4576f8829a4aSRandall Stewart 	}
4577f8829a4aSRandall Stewart 	if (control->tail_mbuf) {
4578f8829a4aSRandall Stewart 		/* append */
4579139bc87fSRandall Stewart 		SCTP_BUF_NEXT(control->tail_mbuf) = m;
4580f8829a4aSRandall Stewart 		control->tail_mbuf = tail;
4581f8829a4aSRandall Stewart 	} else {
4582f8829a4aSRandall Stewart 		/* nothing there */
4583a5d547adSRandall Stewart #ifdef INVARIANTS
4584f8829a4aSRandall Stewart 		if (control->data != NULL) {
4585f8829a4aSRandall Stewart 			panic("This should NOT happen");
4586f8829a4aSRandall Stewart 		}
4587f8829a4aSRandall Stewart #endif
4588f8829a4aSRandall Stewart 		control->data = m;
4589f8829a4aSRandall Stewart 		control->tail_mbuf = tail;
4590f8829a4aSRandall Stewart 	}
459118e198d3SRandall Stewart 	atomic_add_int(&control->length, len);
459218e198d3SRandall Stewart 	if (end) {
459318e198d3SRandall Stewart 		/* message is complete */
459418e198d3SRandall Stewart 		if (stcb && (control == stcb->asoc.control_pdapi)) {
459518e198d3SRandall Stewart 			stcb->asoc.control_pdapi = NULL;
459618e198d3SRandall Stewart 		}
459718e198d3SRandall Stewart 		control->held_length = 0;
459818e198d3SRandall Stewart 		control->end_added = 1;
459918e198d3SRandall Stewart 	}
4600ad81507eSRandall Stewart 	if (stcb == NULL) {
4601ad81507eSRandall Stewart 		control->do_not_ref_stcb = 1;
4602ad81507eSRandall Stewart 	}
4603f8829a4aSRandall Stewart 	/*
4604f8829a4aSRandall Stewart 	 * When we are appending in partial delivery, the cum-ack is used
4605f8829a4aSRandall Stewart 	 * for the actual pd-api highest tsn on this mbuf. The true cum-ack
4606f8829a4aSRandall Stewart 	 * is populated in the outbound sinfo structure from the true cumack
4607f8829a4aSRandall Stewart 	 * if the association exists...
4608f8829a4aSRandall Stewart 	 */
4609f8829a4aSRandall Stewart 	control->sinfo_tsn = control->sinfo_cumtsn = ctls_cumack;
4610f8829a4aSRandall Stewart 	if (inp) {
4611f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
4612f8829a4aSRandall Stewart 	}
4613f8829a4aSRandall Stewart 	if (inp && inp->sctp_socket) {
461417205eccSRandall Stewart 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
461517205eccSRandall Stewart 			SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
4616ceaad40aSRandall Stewart 		} else {
4617ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4618ceaad40aSRandall Stewart 			struct socket *so;
4619ceaad40aSRandall Stewart 
4620ceaad40aSRandall Stewart 			so = SCTP_INP_SO(inp);
462160990c0cSMichael Tuexen 			if (stcb) {
4622ceaad40aSRandall Stewart 				atomic_add_int(&stcb->asoc.refcnt, 1);
4623ceaad40aSRandall Stewart 				SCTP_TCB_UNLOCK(stcb);
462460990c0cSMichael Tuexen 			}
4625ceaad40aSRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
462660990c0cSMichael Tuexen 			if (stcb) {
4627ceaad40aSRandall Stewart 				SCTP_TCB_LOCK(stcb);
4628ceaad40aSRandall Stewart 				atomic_subtract_int(&stcb->asoc.refcnt, 1);
462960990c0cSMichael Tuexen 			}
4630ceaad40aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4631ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
4632ceaad40aSRandall Stewart 				return (0);
4633ceaad40aSRandall Stewart 			}
4634ceaad40aSRandall Stewart #endif
4635f8829a4aSRandall Stewart 			sctp_sorwakeup(inp, inp->sctp_socket);
4636ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4637ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
4638ceaad40aSRandall Stewart #endif
4639ceaad40aSRandall Stewart 		}
4640f8829a4aSRandall Stewart 	}
4641f8829a4aSRandall Stewart 	return (0);
4642f8829a4aSRandall Stewart }
4643f8829a4aSRandall Stewart 
4644f8829a4aSRandall Stewart 
4645f8829a4aSRandall Stewart 
4646f8829a4aSRandall Stewart /*************HOLD THIS COMMENT FOR PATCH FILE OF
4647f8829a4aSRandall Stewart  *************ALTERNATE ROUTING CODE
4648f8829a4aSRandall Stewart  */
4649f8829a4aSRandall Stewart 
4650f8829a4aSRandall Stewart /*************HOLD THIS COMMENT FOR END OF PATCH FILE OF
4651f8829a4aSRandall Stewart  *************ALTERNATE ROUTING CODE
4652f8829a4aSRandall Stewart  */
4653f8829a4aSRandall Stewart 
4654f8829a4aSRandall Stewart struct mbuf *
4655ff1ffd74SMichael Tuexen sctp_generate_cause(uint16_t code, char *info)
4656f8829a4aSRandall Stewart {
4657f8829a4aSRandall Stewart 	struct mbuf *m;
4658ff1ffd74SMichael Tuexen 	struct sctp_gen_error_cause *cause;
4659ff1ffd74SMichael Tuexen 	size_t info_len, len;
4660f8829a4aSRandall Stewart 
4661ff1ffd74SMichael Tuexen 	if ((code == 0) || (info == NULL)) {
4662ff1ffd74SMichael Tuexen 		return (NULL);
4663ff1ffd74SMichael Tuexen 	}
4664ff1ffd74SMichael Tuexen 	info_len = strlen(info);
4665ff1ffd74SMichael Tuexen 	len = sizeof(struct sctp_paramhdr) + info_len;
4666ff1ffd74SMichael Tuexen 	m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
4667ff1ffd74SMichael Tuexen 	if (m != NULL) {
4668ff1ffd74SMichael Tuexen 		SCTP_BUF_LEN(m) = len;
4669ff1ffd74SMichael Tuexen 		cause = mtod(m, struct sctp_gen_error_cause *);
4670ff1ffd74SMichael Tuexen 		cause->code = htons(code);
4671ff1ffd74SMichael Tuexen 		cause->length = htons((uint16_t) len);
4672ff1ffd74SMichael Tuexen 		memcpy(cause->info, info, info_len);
4673f8829a4aSRandall Stewart 	}
4674f8829a4aSRandall Stewart 	return (m);
4675f8829a4aSRandall Stewart }
4676f8829a4aSRandall Stewart 
467732451da4SMichael Tuexen struct mbuf *
467832451da4SMichael Tuexen sctp_generate_no_user_data_cause(uint32_t tsn)
467932451da4SMichael Tuexen {
468032451da4SMichael Tuexen 	struct mbuf *m;
468132451da4SMichael Tuexen 	struct sctp_error_no_user_data *no_user_data_cause;
468232451da4SMichael Tuexen 	size_t len;
468332451da4SMichael Tuexen 
468432451da4SMichael Tuexen 	len = sizeof(struct sctp_error_no_user_data);
468532451da4SMichael Tuexen 	m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
468632451da4SMichael Tuexen 	if (m != NULL) {
468732451da4SMichael Tuexen 		SCTP_BUF_LEN(m) = len;
468832451da4SMichael Tuexen 		no_user_data_cause = mtod(m, struct sctp_error_no_user_data *);
468932451da4SMichael Tuexen 		no_user_data_cause->cause.code = htons(SCTP_CAUSE_NO_USER_DATA);
469032451da4SMichael Tuexen 		no_user_data_cause->cause.length = htons((uint16_t) len);
469132451da4SMichael Tuexen 		no_user_data_cause->tsn = tsn;	/* tsn is passed in as NBO */
469232451da4SMichael Tuexen 	}
469332451da4SMichael Tuexen 	return (m);
469432451da4SMichael Tuexen }
469532451da4SMichael Tuexen 
4696f8829a4aSRandall Stewart #ifdef SCTP_MBCNT_LOGGING
4697f8829a4aSRandall Stewart void
4698f8829a4aSRandall Stewart sctp_free_bufspace(struct sctp_tcb *stcb, struct sctp_association *asoc,
4699f8829a4aSRandall Stewart     struct sctp_tmit_chunk *tp1, int chk_cnt)
4700f8829a4aSRandall Stewart {
4701f8829a4aSRandall Stewart 	if (tp1->data == NULL) {
4702f8829a4aSRandall Stewart 		return;
4703f8829a4aSRandall Stewart 	}
4704f8829a4aSRandall Stewart 	asoc->chunks_on_out_queue -= chk_cnt;
4705b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBCNT_LOGGING_ENABLE) {
4706f8829a4aSRandall Stewart 		sctp_log_mbcnt(SCTP_LOG_MBCNT_DECREASE,
4707f8829a4aSRandall Stewart 		    asoc->total_output_queue_size,
4708f8829a4aSRandall Stewart 		    tp1->book_size,
4709f8829a4aSRandall Stewart 		    0,
4710f8829a4aSRandall Stewart 		    tp1->mbcnt);
471180fefe0aSRandall Stewart 	}
4712f8829a4aSRandall Stewart 	if (asoc->total_output_queue_size >= tp1->book_size) {
471344b7479bSRandall Stewart 		atomic_add_int(&asoc->total_output_queue_size, -tp1->book_size);
4714f8829a4aSRandall Stewart 	} else {
4715f8829a4aSRandall Stewart 		asoc->total_output_queue_size = 0;
4716f8829a4aSRandall Stewart 	}
4717f8829a4aSRandall Stewart 
4718f8829a4aSRandall Stewart 	if (stcb->sctp_socket && (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) ||
4719f8829a4aSRandall Stewart 	    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) {
47204e88d37aSMichael Tuexen 		if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) {
47214e88d37aSMichael Tuexen 			stcb->sctp_socket->so_snd.sb_cc -= tp1->book_size;
4722f8829a4aSRandall Stewart 		} else {
47234e88d37aSMichael Tuexen 			stcb->sctp_socket->so_snd.sb_cc = 0;
4724f8829a4aSRandall Stewart 
4725f8829a4aSRandall Stewart 		}
4726f8829a4aSRandall Stewart 	}
4727f8829a4aSRandall Stewart }
4728f8829a4aSRandall Stewart 
4729f8829a4aSRandall Stewart #endif
4730f8829a4aSRandall Stewart 
4731f8829a4aSRandall Stewart int
4732f8829a4aSRandall Stewart sctp_release_pr_sctp_chunk(struct sctp_tcb *stcb, struct sctp_tmit_chunk *tp1,
47331edc9dbaSMichael Tuexen     uint8_t sent, int so_locked
4734ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4735ceaad40aSRandall Stewart     SCTP_UNUSED
4736ceaad40aSRandall Stewart #endif
4737ceaad40aSRandall Stewart )
4738f8829a4aSRandall Stewart {
47390c0982b8SRandall Stewart 	struct sctp_stream_out *strq;
47404a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk = NULL, *tp2;
47410c0982b8SRandall Stewart 	struct sctp_stream_queue_pending *sp;
47420c0982b8SRandall Stewart 	uint16_t stream = 0, seq = 0;
47430c0982b8SRandall Stewart 	uint8_t foundeom = 0;
4744f8829a4aSRandall Stewart 	int ret_sz = 0;
4745f8829a4aSRandall Stewart 	int notdone;
47460c0982b8SRandall Stewart 	int do_wakeup_routine = 0;
4747f8829a4aSRandall Stewart 
47480c0982b8SRandall Stewart 	stream = tp1->rec.data.stream_number;
47490c0982b8SRandall Stewart 	seq = tp1->rec.data.stream_seq;
4750f0396ad1SMichael Tuexen 	if (sent || !(tp1->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG)) {
4751f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_sent[0]++;
4752f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
4753f0396ad1SMichael Tuexen 		stcb->asoc.strmout[stream].abandoned_sent[0]++;
4754f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
4755f0396ad1SMichael Tuexen 		stcb->asoc.strmout[stream].abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
4756f0396ad1SMichael Tuexen #endif
4757f0396ad1SMichael Tuexen 	} else {
4758f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_unsent[0]++;
4759f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
4760f0396ad1SMichael Tuexen 		stcb->asoc.strmout[stream].abandoned_unsent[0]++;
4761f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
4762f0396ad1SMichael Tuexen 		stcb->asoc.strmout[stream].abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
4763f0396ad1SMichael Tuexen #endif
4764f0396ad1SMichael Tuexen 	}
4765f8829a4aSRandall Stewart 	do {
4766f8829a4aSRandall Stewart 		ret_sz += tp1->book_size;
47670c0982b8SRandall Stewart 		if (tp1->data != NULL) {
47688933fa13SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4769830d754dSRandall Stewart 				sctp_flight_size_decrease(tp1);
4770830d754dSRandall Stewart 				sctp_total_flight_decrease(stcb, tp1);
47718933fa13SRandall Stewart 			}
47728933fa13SRandall Stewart 			sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
47730c0982b8SRandall Stewart 			stcb->asoc.peers_rwnd += tp1->send_size;
47740c0982b8SRandall Stewart 			stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
47751edc9dbaSMichael Tuexen 			if (sent) {
47761edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
47771edc9dbaSMichael Tuexen 			} else {
47781edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
47791edc9dbaSMichael Tuexen 			}
47802f99457bSMichael Tuexen 			if (tp1->data) {
4781f8829a4aSRandall Stewart 				sctp_m_freem(tp1->data);
4782f8829a4aSRandall Stewart 				tp1->data = NULL;
47832f99457bSMichael Tuexen 			}
47840c0982b8SRandall Stewart 			do_wakeup_routine = 1;
4785f8829a4aSRandall Stewart 			if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
4786f8829a4aSRandall Stewart 				stcb->asoc.sent_queue_cnt_removeable--;
4787f8829a4aSRandall Stewart 			}
4788f8829a4aSRandall Stewart 		}
47898933fa13SRandall Stewart 		tp1->sent = SCTP_FORWARD_TSN_SKIP;
4790f8829a4aSRandall Stewart 		if ((tp1->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) ==
4791f8829a4aSRandall Stewart 		    SCTP_DATA_NOT_FRAG) {
4792f8829a4aSRandall Stewart 			/* not frag'ed we ae done   */
4793f8829a4aSRandall Stewart 			notdone = 0;
4794f8829a4aSRandall Stewart 			foundeom = 1;
4795f8829a4aSRandall Stewart 		} else if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
4796f8829a4aSRandall Stewart 			/* end of frag, we are done */
4797f8829a4aSRandall Stewart 			notdone = 0;
4798f8829a4aSRandall Stewart 			foundeom = 1;
4799f8829a4aSRandall Stewart 		} else {
4800f8829a4aSRandall Stewart 			/*
4801f8829a4aSRandall Stewart 			 * Its a begin or middle piece, we must mark all of
4802f8829a4aSRandall Stewart 			 * it
4803f8829a4aSRandall Stewart 			 */
4804f8829a4aSRandall Stewart 			notdone = 1;
4805f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
4806f8829a4aSRandall Stewart 		}
4807f8829a4aSRandall Stewart 	} while (tp1 && notdone);
48080c0982b8SRandall Stewart 	if (foundeom == 0) {
4809f8829a4aSRandall Stewart 		/*
4810f8829a4aSRandall Stewart 		 * The multi-part message was scattered across the send and
4811f8829a4aSRandall Stewart 		 * sent queue.
4812f8829a4aSRandall Stewart 		 */
48134a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(tp1, &stcb->asoc.send_queue, sctp_next, tp2) {
48144a9ef3f8SMichael Tuexen 			if ((tp1->rec.data.stream_number != stream) ||
48154a9ef3f8SMichael Tuexen 			    (tp1->rec.data.stream_seq != seq)) {
48164a9ef3f8SMichael Tuexen 				break;
48174a9ef3f8SMichael Tuexen 			}
48180c0982b8SRandall Stewart 			/*
48190c0982b8SRandall Stewart 			 * save to chk in case we have some on stream out
48200c0982b8SRandall Stewart 			 * queue. If so and we have an un-transmitted one we
48210c0982b8SRandall Stewart 			 * don't have to fudge the TSN.
48220c0982b8SRandall Stewart 			 */
48230c0982b8SRandall Stewart 			chk = tp1;
48240c0982b8SRandall Stewart 			ret_sz += tp1->book_size;
48250c0982b8SRandall Stewart 			sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
48261edc9dbaSMichael Tuexen 			if (sent) {
48271edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
48281edc9dbaSMichael Tuexen 			} else {
48291edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
48301edc9dbaSMichael Tuexen 			}
48312f99457bSMichael Tuexen 			if (tp1->data) {
48320c0982b8SRandall Stewart 				sctp_m_freem(tp1->data);
48332f99457bSMichael Tuexen 				tp1->data = NULL;
48342f99457bSMichael Tuexen 			}
48358933fa13SRandall Stewart 			/* No flight involved here book the size to 0 */
48368933fa13SRandall Stewart 			tp1->book_size = 0;
48370c0982b8SRandall Stewart 			if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
48380c0982b8SRandall Stewart 				foundeom = 1;
4839f8829a4aSRandall Stewart 			}
48400c0982b8SRandall Stewart 			do_wakeup_routine = 1;
48410c0982b8SRandall Stewart 			tp1->sent = SCTP_FORWARD_TSN_SKIP;
48420c0982b8SRandall Stewart 			TAILQ_REMOVE(&stcb->asoc.send_queue, tp1, sctp_next);
48430c0982b8SRandall Stewart 			/*
48440c0982b8SRandall Stewart 			 * on to the sent queue so we can wait for it to be
48450c0982b8SRandall Stewart 			 * passed by.
48460c0982b8SRandall Stewart 			 */
48470c0982b8SRandall Stewart 			TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, tp1,
48480c0982b8SRandall Stewart 			    sctp_next);
48490c0982b8SRandall Stewart 			stcb->asoc.send_queue_cnt--;
48500c0982b8SRandall Stewart 			stcb->asoc.sent_queue_cnt++;
48510c0982b8SRandall Stewart 		}
48520c0982b8SRandall Stewart 	}
48530c0982b8SRandall Stewart 	if (foundeom == 0) {
48540c0982b8SRandall Stewart 		/*
48550c0982b8SRandall Stewart 		 * Still no eom found. That means there is stuff left on the
48560c0982b8SRandall Stewart 		 * stream out queue.. yuck.
48570c0982b8SRandall Stewart 		 */
48580c0982b8SRandall Stewart 		SCTP_TCB_SEND_LOCK(stcb);
4859f3b05218SMichael Tuexen 		strq = &stcb->asoc.strmout[stream];
4860f3b05218SMichael Tuexen 		sp = TAILQ_FIRST(&strq->outqueue);
4861f3b05218SMichael Tuexen 		if (sp != NULL) {
48620c0982b8SRandall Stewart 			sp->discard_rest = 1;
48630c0982b8SRandall Stewart 			/*
4864f3b05218SMichael Tuexen 			 * We may need to put a chunk on the queue that
4865f3b05218SMichael Tuexen 			 * holds the TSN that would have been sent with the
4866f3b05218SMichael Tuexen 			 * LAST bit.
48670c0982b8SRandall Stewart 			 */
48680c0982b8SRandall Stewart 			if (chk == NULL) {
48690c0982b8SRandall Stewart 				/* Yep, we have to */
48700c0982b8SRandall Stewart 				sctp_alloc_a_chunk(stcb, chk);
48710c0982b8SRandall Stewart 				if (chk == NULL) {
48720c0982b8SRandall Stewart 					/*
4873f3b05218SMichael Tuexen 					 * we are hosed. All we can do is
4874f3b05218SMichael Tuexen 					 * nothing.. which will cause an
4875f3b05218SMichael Tuexen 					 * abort if the peer is paying
48760c0982b8SRandall Stewart 					 * attention.
48770c0982b8SRandall Stewart 					 */
48780c0982b8SRandall Stewart 					goto oh_well;
48790c0982b8SRandall Stewart 				}
48800c0982b8SRandall Stewart 				memset(chk, 0, sizeof(*chk));
48810c0982b8SRandall Stewart 				chk->rec.data.rcv_flags = SCTP_DATA_LAST_FRAG;
48820c0982b8SRandall Stewart 				chk->sent = SCTP_FORWARD_TSN_SKIP;
48830c0982b8SRandall Stewart 				chk->asoc = &stcb->asoc;
4884f3b05218SMichael Tuexen 				chk->rec.data.stream_seq = strq->next_sequence_send;
48850c0982b8SRandall Stewart 				chk->rec.data.stream_number = sp->stream;
48860c0982b8SRandall Stewart 				chk->rec.data.payloadtype = sp->ppid;
48870c0982b8SRandall Stewart 				chk->rec.data.context = sp->context;
48880c0982b8SRandall Stewart 				chk->flags = sp->act_flags;
48899eea4a2dSMichael Tuexen 				if (sp->net)
48900c0982b8SRandall Stewart 					chk->whoTo = sp->net;
48919eea4a2dSMichael Tuexen 				else
48929eea4a2dSMichael Tuexen 					chk->whoTo = stcb->asoc.primary_destination;
48930c0982b8SRandall Stewart 				atomic_add_int(&chk->whoTo->ref_count, 1);
48940c0982b8SRandall Stewart 				chk->rec.data.TSN_seq = atomic_fetchadd_int(&stcb->asoc.sending_seq, 1);
48950c0982b8SRandall Stewart 				stcb->asoc.pr_sctp_cnt++;
48960c0982b8SRandall Stewart 				TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, chk, sctp_next);
48970c0982b8SRandall Stewart 				stcb->asoc.sent_queue_cnt++;
48988933fa13SRandall Stewart 				stcb->asoc.pr_sctp_cnt++;
48990c0982b8SRandall Stewart 			} else {
49000c0982b8SRandall Stewart 				chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
49010c0982b8SRandall Stewart 			}
4902f3b05218SMichael Tuexen 			strq->next_sequence_send++;
49030c0982b8SRandall Stewart 	oh_well:
49040c0982b8SRandall Stewart 			if (sp->data) {
49050c0982b8SRandall Stewart 				/*
4906f3b05218SMichael Tuexen 				 * Pull any data to free up the SB and allow
4907f3b05218SMichael Tuexen 				 * sender to "add more" while we will throw
4908f3b05218SMichael Tuexen 				 * away :-)
49090c0982b8SRandall Stewart 				 */
4910f3b05218SMichael Tuexen 				sctp_free_spbufspace(stcb, &stcb->asoc, sp);
49110c0982b8SRandall Stewart 				ret_sz += sp->length;
49120c0982b8SRandall Stewart 				do_wakeup_routine = 1;
49130c0982b8SRandall Stewart 				sp->some_taken = 1;
49140c0982b8SRandall Stewart 				sctp_m_freem(sp->data);
49150c0982b8SRandall Stewart 				sp->data = NULL;
49160c0982b8SRandall Stewart 				sp->tail_mbuf = NULL;
4917d07b2ac6SMichael Tuexen 				sp->length = 0;
49180c0982b8SRandall Stewart 			}
49190c0982b8SRandall Stewart 		}
49200c0982b8SRandall Stewart 		SCTP_TCB_SEND_UNLOCK(stcb);
49210c0982b8SRandall Stewart 	}
49220c0982b8SRandall Stewart 	if (do_wakeup_routine) {
49230c0982b8SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
49248933fa13SRandall Stewart 		struct socket *so;
49258933fa13SRandall Stewart 
49260c0982b8SRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
49270c0982b8SRandall Stewart 		if (!so_locked) {
49280c0982b8SRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, 1);
49290c0982b8SRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
49300c0982b8SRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
49310c0982b8SRandall Stewart 			SCTP_TCB_LOCK(stcb);
49320c0982b8SRandall Stewart 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
49330c0982b8SRandall Stewart 			if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
49340c0982b8SRandall Stewart 				/* assoc was freed while we were unlocked */
49350c0982b8SRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
49360c0982b8SRandall Stewart 				return (ret_sz);
49370c0982b8SRandall Stewart 			}
49380c0982b8SRandall Stewart 		}
49390c0982b8SRandall Stewart #endif
49400c0982b8SRandall Stewart 		sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
49410c0982b8SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
49420c0982b8SRandall Stewart 		if (!so_locked) {
49430c0982b8SRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
49440c0982b8SRandall Stewart 		}
49450c0982b8SRandall Stewart #endif
4946f8829a4aSRandall Stewart 	}
4947f8829a4aSRandall Stewart 	return (ret_sz);
4948f8829a4aSRandall Stewart }
4949f8829a4aSRandall Stewart 
4950f8829a4aSRandall Stewart /*
4951f8829a4aSRandall Stewart  * checks to see if the given address, sa, is one that is currently known by
4952f8829a4aSRandall Stewart  * the kernel note: can't distinguish the same address on multiple interfaces
4953f8829a4aSRandall Stewart  * and doesn't handle multiple addresses with different zone/scope id's note:
4954f8829a4aSRandall Stewart  * ifa_ifwithaddr() compares the entire sockaddr struct
4955f8829a4aSRandall Stewart  */
495642551e99SRandall Stewart struct sctp_ifa *
495780fefe0aSRandall Stewart sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr,
495880fefe0aSRandall Stewart     int holds_lock)
4959f8829a4aSRandall Stewart {
496042551e99SRandall Stewart 	struct sctp_laddr *laddr;
4961f8829a4aSRandall Stewart 
4962ad81507eSRandall Stewart 	if (holds_lock == 0) {
496342551e99SRandall Stewart 		SCTP_INP_RLOCK(inp);
4964ad81507eSRandall Stewart 	}
496542551e99SRandall Stewart 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
496642551e99SRandall Stewart 		if (laddr->ifa == NULL)
4967f8829a4aSRandall Stewart 			continue;
496842551e99SRandall Stewart 		if (addr->sa_family != laddr->ifa->address.sa.sa_family)
496942551e99SRandall Stewart 			continue;
4970e6194c2eSMichael Tuexen #ifdef INET
497142551e99SRandall Stewart 		if (addr->sa_family == AF_INET) {
497242551e99SRandall Stewart 			if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
497342551e99SRandall Stewart 			    laddr->ifa->address.sin.sin_addr.s_addr) {
497442551e99SRandall Stewart 				/* found him. */
4975ad81507eSRandall Stewart 				if (holds_lock == 0) {
497642551e99SRandall Stewart 					SCTP_INP_RUNLOCK(inp);
4977ad81507eSRandall Stewart 				}
497842551e99SRandall Stewart 				return (laddr->ifa);
497942551e99SRandall Stewart 				break;
498042551e99SRandall Stewart 			}
49815e2c2d87SRandall Stewart 		}
4982e6194c2eSMichael Tuexen #endif
49835e2c2d87SRandall Stewart #ifdef INET6
49845e2c2d87SRandall Stewart 		if (addr->sa_family == AF_INET6) {
4985c54a18d2SRandall Stewart 			if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
4986c54a18d2SRandall Stewart 			    &laddr->ifa->address.sin6)) {
498742551e99SRandall Stewart 				/* found him. */
4988ad81507eSRandall Stewart 				if (holds_lock == 0) {
498942551e99SRandall Stewart 					SCTP_INP_RUNLOCK(inp);
4990ad81507eSRandall Stewart 				}
499142551e99SRandall Stewart 				return (laddr->ifa);
499242551e99SRandall Stewart 				break;
499342551e99SRandall Stewart 			}
499442551e99SRandall Stewart 		}
49955e2c2d87SRandall Stewart #endif
499642551e99SRandall Stewart 	}
4997ad81507eSRandall Stewart 	if (holds_lock == 0) {
499842551e99SRandall Stewart 		SCTP_INP_RUNLOCK(inp);
4999ad81507eSRandall Stewart 	}
500042551e99SRandall Stewart 	return (NULL);
500142551e99SRandall Stewart }
5002f8829a4aSRandall Stewart 
50036a27c376SRandall Stewart uint32_t
50046a27c376SRandall Stewart sctp_get_ifa_hash_val(struct sockaddr *addr)
50056a27c376SRandall Stewart {
5006ea5eba11SMichael Tuexen 	switch (addr->sa_family) {
5007ea5eba11SMichael Tuexen #ifdef INET
5008ea5eba11SMichael Tuexen 	case AF_INET:
5009ea5eba11SMichael Tuexen 		{
50106a27c376SRandall Stewart 			struct sockaddr_in *sin;
50116a27c376SRandall Stewart 
50126a27c376SRandall Stewart 			sin = (struct sockaddr_in *)addr;
50136a27c376SRandall Stewart 			return (sin->sin_addr.s_addr ^ (sin->sin_addr.s_addr >> 16));
5014ea5eba11SMichael Tuexen 		}
5015ea5eba11SMichael Tuexen #endif
5016ea5eba11SMichael Tuexen #ifdef INET6
50172c2e3218SMichael Tuexen 	case AF_INET6:
5018ea5eba11SMichael Tuexen 		{
50196a27c376SRandall Stewart 			struct sockaddr_in6 *sin6;
50206a27c376SRandall Stewart 			uint32_t hash_of_addr;
50216a27c376SRandall Stewart 
50226a27c376SRandall Stewart 			sin6 = (struct sockaddr_in6 *)addr;
50236a27c376SRandall Stewart 			hash_of_addr = (sin6->sin6_addr.s6_addr32[0] +
50246a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[1] +
50256a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[2] +
50266a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[3]);
50276a27c376SRandall Stewart 			hash_of_addr = (hash_of_addr ^ (hash_of_addr >> 16));
50286a27c376SRandall Stewart 			return (hash_of_addr);
50296a27c376SRandall Stewart 		}
5030ea5eba11SMichael Tuexen #endif
5031ea5eba11SMichael Tuexen 	default:
5032ea5eba11SMichael Tuexen 		break;
5033ea5eba11SMichael Tuexen 	}
50346a27c376SRandall Stewart 	return (0);
50356a27c376SRandall Stewart }
50366a27c376SRandall Stewart 
503742551e99SRandall Stewart struct sctp_ifa *
503842551e99SRandall Stewart sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock)
503942551e99SRandall Stewart {
504042551e99SRandall Stewart 	struct sctp_ifa *sctp_ifap;
504142551e99SRandall Stewart 	struct sctp_vrf *vrf;
50426a27c376SRandall Stewart 	struct sctp_ifalist *hash_head;
50436a27c376SRandall Stewart 	uint32_t hash_of_addr;
504442551e99SRandall Stewart 
504542551e99SRandall Stewart 	if (holds_lock == 0)
5046c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RLOCK();
504742551e99SRandall Stewart 
5048bff64a4dSRandall Stewart 	vrf = sctp_find_vrf(vrf_id);
5049bff64a4dSRandall Stewart 	if (vrf == NULL) {
5050df6e0cc3SRandall Stewart stage_right:
5051bff64a4dSRandall Stewart 		if (holds_lock == 0)
5052c99efcf6SRandall Stewart 			SCTP_IPI_ADDR_RUNLOCK();
5053bff64a4dSRandall Stewart 		return (NULL);
5054bff64a4dSRandall Stewart 	}
5055bff64a4dSRandall Stewart 	hash_of_addr = sctp_get_ifa_hash_val(addr);
5056bff64a4dSRandall Stewart 
505717205eccSRandall Stewart 	hash_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
5058bff64a4dSRandall Stewart 	if (hash_head == NULL) {
5059ad81507eSRandall Stewart 		SCTP_PRINTF("hash_of_addr:%x mask:%x table:%x - ",
5060c99efcf6SRandall Stewart 		    hash_of_addr, (uint32_t) vrf->vrf_addr_hashmark,
5061c99efcf6SRandall Stewart 		    (uint32_t) (hash_of_addr & vrf->vrf_addr_hashmark));
5062bff64a4dSRandall Stewart 		sctp_print_address(addr);
5063ad81507eSRandall Stewart 		SCTP_PRINTF("No such bucket for address\n");
5064bff64a4dSRandall Stewart 		if (holds_lock == 0)
5065c99efcf6SRandall Stewart 			SCTP_IPI_ADDR_RUNLOCK();
5066bff64a4dSRandall Stewart 
5067bff64a4dSRandall Stewart 		return (NULL);
5068bff64a4dSRandall Stewart 	}
50696a27c376SRandall Stewart 	LIST_FOREACH(sctp_ifap, hash_head, next_bucket) {
5070bff64a4dSRandall Stewart 		if (sctp_ifap == NULL) {
5071df6e0cc3SRandall Stewart #ifdef INVARIANTS
5072bff64a4dSRandall Stewart 			panic("Huh LIST_FOREACH corrupt");
5073df6e0cc3SRandall Stewart 			goto stage_right;
5074df6e0cc3SRandall Stewart #else
5075df6e0cc3SRandall Stewart 			SCTP_PRINTF("LIST corrupt of sctp_ifap's?\n");
5076df6e0cc3SRandall Stewart 			goto stage_right;
5077df6e0cc3SRandall Stewart #endif
5078bff64a4dSRandall Stewart 		}
50796a27c376SRandall Stewart 		if (addr->sa_family != sctp_ifap->address.sa.sa_family)
50806a27c376SRandall Stewart 			continue;
5081e6194c2eSMichael Tuexen #ifdef INET
50826a27c376SRandall Stewart 		if (addr->sa_family == AF_INET) {
50836a27c376SRandall Stewart 			if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
50846a27c376SRandall Stewart 			    sctp_ifap->address.sin.sin_addr.s_addr) {
50856a27c376SRandall Stewart 				/* found him. */
508642551e99SRandall Stewart 				if (holds_lock == 0)
5087c99efcf6SRandall Stewart 					SCTP_IPI_ADDR_RUNLOCK();
508842551e99SRandall Stewart 				return (sctp_ifap);
50896a27c376SRandall Stewart 				break;
50906a27c376SRandall Stewart 			}
50915e2c2d87SRandall Stewart 		}
5092e6194c2eSMichael Tuexen #endif
50935e2c2d87SRandall Stewart #ifdef INET6
50945e2c2d87SRandall Stewart 		if (addr->sa_family == AF_INET6) {
5095c54a18d2SRandall Stewart 			if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5096c54a18d2SRandall Stewart 			    &sctp_ifap->address.sin6)) {
50976a27c376SRandall Stewart 				/* found him. */
50986a27c376SRandall Stewart 				if (holds_lock == 0)
5099c99efcf6SRandall Stewart 					SCTP_IPI_ADDR_RUNLOCK();
51006a27c376SRandall Stewart 				return (sctp_ifap);
51016a27c376SRandall Stewart 				break;
51026a27c376SRandall Stewart 			}
510342551e99SRandall Stewart 		}
51045e2c2d87SRandall Stewart #endif
510542551e99SRandall Stewart 	}
510642551e99SRandall Stewart 	if (holds_lock == 0)
5107c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RUNLOCK();
5108f8829a4aSRandall Stewart 	return (NULL);
5109f8829a4aSRandall Stewart }
5110f8829a4aSRandall Stewart 
5111f8829a4aSRandall Stewart static void
51124c9179adSRandall Stewart sctp_user_rcvd(struct sctp_tcb *stcb, uint32_t * freed_so_far, int hold_rlock,
5113f8829a4aSRandall Stewart     uint32_t rwnd_req)
5114f8829a4aSRandall Stewart {
5115f8829a4aSRandall Stewart 	/* User pulled some data, do we need a rwnd update? */
5116f8829a4aSRandall Stewart 	int r_unlocked = 0;
5117f8829a4aSRandall Stewart 	uint32_t dif, rwnd;
5118f8829a4aSRandall Stewart 	struct socket *so = NULL;
5119f8829a4aSRandall Stewart 
5120f8829a4aSRandall Stewart 	if (stcb == NULL)
5121f8829a4aSRandall Stewart 		return;
5122f8829a4aSRandall Stewart 
512350cec919SRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, 1);
5124f8829a4aSRandall Stewart 
512562c1ff9cSRandall Stewart 	if (stcb->asoc.state & (SCTP_STATE_ABOUT_TO_BE_FREED |
512662c1ff9cSRandall Stewart 	    SCTP_STATE_SHUTDOWN_RECEIVED |
51274c9179adSRandall Stewart 	    SCTP_STATE_SHUTDOWN_ACK_SENT)) {
5128f8829a4aSRandall Stewart 		/* Pre-check If we are freeing no update */
5129f8829a4aSRandall Stewart 		goto no_lock;
5130f8829a4aSRandall Stewart 	}
5131f8829a4aSRandall Stewart 	SCTP_INP_INCR_REF(stcb->sctp_ep);
5132f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5133f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5134f8829a4aSRandall Stewart 		goto out;
5135f8829a4aSRandall Stewart 	}
5136f8829a4aSRandall Stewart 	so = stcb->sctp_socket;
5137f8829a4aSRandall Stewart 	if (so == NULL) {
5138f8829a4aSRandall Stewart 		goto out;
5139f8829a4aSRandall Stewart 	}
5140f8829a4aSRandall Stewart 	atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far);
5141f8829a4aSRandall Stewart 	/* Have you have freed enough to look */
5142f8829a4aSRandall Stewart 	*freed_so_far = 0;
5143f8829a4aSRandall Stewart 	/* Yep, its worth a look and the lock overhead */
5144f8829a4aSRandall Stewart 
5145f8829a4aSRandall Stewart 	/* Figure out what the rwnd would be */
5146f8829a4aSRandall Stewart 	rwnd = sctp_calc_rwnd(stcb, &stcb->asoc);
5147f8829a4aSRandall Stewart 	if (rwnd >= stcb->asoc.my_last_reported_rwnd) {
5148f8829a4aSRandall Stewart 		dif = rwnd - stcb->asoc.my_last_reported_rwnd;
5149f8829a4aSRandall Stewart 	} else {
5150f8829a4aSRandall Stewart 		dif = 0;
5151f8829a4aSRandall Stewart 	}
5152f8829a4aSRandall Stewart 	if (dif >= rwnd_req) {
5153f8829a4aSRandall Stewart 		if (hold_rlock) {
5154f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5155f8829a4aSRandall Stewart 			r_unlocked = 1;
5156f8829a4aSRandall Stewart 		}
5157f8829a4aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5158f8829a4aSRandall Stewart 			/*
5159f8829a4aSRandall Stewart 			 * One last check before we allow the guy possibly
5160f8829a4aSRandall Stewart 			 * to get in. There is a race, where the guy has not
5161f8829a4aSRandall Stewart 			 * reached the gate. In that case
5162f8829a4aSRandall Stewart 			 */
5163f8829a4aSRandall Stewart 			goto out;
5164f8829a4aSRandall Stewart 		}
5165f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
5166f8829a4aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5167f8829a4aSRandall Stewart 			/* No reports here */
5168f8829a4aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
5169f8829a4aSRandall Stewart 			goto out;
5170f8829a4aSRandall Stewart 		}
5171f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_wu_sacks_sent);
5172689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_LOCKED);
5173830d754dSRandall Stewart 
5174f8829a4aSRandall Stewart 		sctp_chunk_output(stcb->sctp_ep, stcb,
5175ceaad40aSRandall Stewart 		    SCTP_OUTPUT_FROM_USR_RCVD, SCTP_SO_LOCKED);
5176f8829a4aSRandall Stewart 		/* make sure no timer is running */
5177a5d547adSRandall Stewart 		sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_6);
5178f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
5179f8829a4aSRandall Stewart 	} else {
5180f8829a4aSRandall Stewart 		/* Update how much we have pending */
5181f8829a4aSRandall Stewart 		stcb->freed_by_sorcv_sincelast = dif;
5182f8829a4aSRandall Stewart 	}
5183f8829a4aSRandall Stewart out:
5184f8829a4aSRandall Stewart 	if (so && r_unlocked && hold_rlock) {
5185f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
5186f8829a4aSRandall Stewart 	}
5187f8829a4aSRandall Stewart 	SCTP_INP_DECR_REF(stcb->sctp_ep);
5188f8829a4aSRandall Stewart no_lock:
518950cec919SRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, -1);
5190f8829a4aSRandall Stewart 	return;
5191f8829a4aSRandall Stewart }
5192f8829a4aSRandall Stewart 
5193f8829a4aSRandall Stewart int
5194f8829a4aSRandall Stewart sctp_sorecvmsg(struct socket *so,
5195f8829a4aSRandall Stewart     struct uio *uio,
5196f8829a4aSRandall Stewart     struct mbuf **mp,
5197f8829a4aSRandall Stewart     struct sockaddr *from,
5198f8829a4aSRandall Stewart     int fromlen,
5199f8829a4aSRandall Stewart     int *msg_flags,
5200f8829a4aSRandall Stewart     struct sctp_sndrcvinfo *sinfo,
5201f8829a4aSRandall Stewart     int filling_sinfo)
5202f8829a4aSRandall Stewart {
5203f8829a4aSRandall Stewart 	/*
5204f8829a4aSRandall Stewart 	 * MSG flags we will look at MSG_DONTWAIT - non-blocking IO.
5205f8829a4aSRandall Stewart 	 * MSG_PEEK - Look don't touch :-D (only valid with OUT mbuf copy
5206f8829a4aSRandall Stewart 	 * mp=NULL thus uio is the copy method to userland) MSG_WAITALL - ??
5207f8829a4aSRandall Stewart 	 * On the way out we may send out any combination of:
5208f8829a4aSRandall Stewart 	 * MSG_NOTIFICATION MSG_EOR
5209f8829a4aSRandall Stewart 	 *
5210f8829a4aSRandall Stewart 	 */
5211f8829a4aSRandall Stewart 	struct sctp_inpcb *inp = NULL;
5212f8829a4aSRandall Stewart 	int my_len = 0;
5213f8829a4aSRandall Stewart 	int cp_len = 0, error = 0;
5214f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL;
521594b0d969SMichael Tuexen 	struct mbuf *m = NULL;
5216f8829a4aSRandall Stewart 	struct sctp_tcb *stcb = NULL;
5217f8829a4aSRandall Stewart 	int wakeup_read_socket = 0;
5218f8829a4aSRandall Stewart 	int freecnt_applied = 0;
5219f8829a4aSRandall Stewart 	int out_flags = 0, in_flags = 0;
5220f8829a4aSRandall Stewart 	int block_allowed = 1;
52214c9179adSRandall Stewart 	uint32_t freed_so_far = 0;
522281aca91aSRandall Stewart 	uint32_t copied_so_far = 0;
522393164cf9SRandall Stewart 	int in_eeor_mode = 0;
5224f8829a4aSRandall Stewart 	int no_rcv_needed = 0;
5225f8829a4aSRandall Stewart 	uint32_t rwnd_req = 0;
5226f8829a4aSRandall Stewart 	int hold_sblock = 0;
5227f8829a4aSRandall Stewart 	int hold_rlock = 0;
522842551e99SRandall Stewart 	int slen = 0;
52294c9179adSRandall Stewart 	uint32_t held_length = 0;
52307abab911SRobert Watson 	int sockbuf_lock = 0;
5231f8829a4aSRandall Stewart 
523217205eccSRandall Stewart 	if (uio == NULL) {
5233c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
523417205eccSRandall Stewart 		return (EINVAL);
523517205eccSRandall Stewart 	}
5236f8829a4aSRandall Stewart 	if (msg_flags) {
5237f8829a4aSRandall Stewart 		in_flags = *msg_flags;
5238c105859eSRandall Stewart 		if (in_flags & MSG_PEEK)
5239c105859eSRandall Stewart 			SCTP_STAT_INCR(sctps_read_peeks);
5240f8829a4aSRandall Stewart 	} else {
5241f8829a4aSRandall Stewart 		in_flags = 0;
5242f8829a4aSRandall Stewart 	}
5243f8829a4aSRandall Stewart 	slen = uio->uio_resid;
524417205eccSRandall Stewart 
5245f8829a4aSRandall Stewart 	/* Pull in and set up our int flags */
5246f8829a4aSRandall Stewart 	if (in_flags & MSG_OOB) {
5247f8829a4aSRandall Stewart 		/* Out of band's NOT supported */
5248f8829a4aSRandall Stewart 		return (EOPNOTSUPP);
5249f8829a4aSRandall Stewart 	}
5250f8829a4aSRandall Stewart 	if ((in_flags & MSG_PEEK) && (mp != NULL)) {
5251c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5252f8829a4aSRandall Stewart 		return (EINVAL);
5253f8829a4aSRandall Stewart 	}
5254f8829a4aSRandall Stewart 	if ((in_flags & (MSG_DONTWAIT
5255f8829a4aSRandall Stewart 	    | MSG_NBIO
5256f8829a4aSRandall Stewart 	    )) ||
525742551e99SRandall Stewart 	    SCTP_SO_IS_NBIO(so)) {
5258f8829a4aSRandall Stewart 		block_allowed = 0;
5259f8829a4aSRandall Stewart 	}
5260f8829a4aSRandall Stewart 	/* setup the endpoint */
5261f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
5262f8829a4aSRandall Stewart 	if (inp == NULL) {
5263c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
5264f8829a4aSRandall Stewart 		return (EFAULT);
5265f8829a4aSRandall Stewart 	}
526662c1ff9cSRandall Stewart 	rwnd_req = (SCTP_SB_LIMIT_RCV(so) >> SCTP_RWND_HIWAT_SHIFT);
5267f8829a4aSRandall Stewart 	/* Must be at least a MTU's worth */
5268f8829a4aSRandall Stewart 	if (rwnd_req < SCTP_MIN_RWND)
5269f8829a4aSRandall Stewart 		rwnd_req = SCTP_MIN_RWND;
5270f8829a4aSRandall Stewart 	in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
5271b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5272f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SORECV_ENTER,
52734e88d37aSMichael Tuexen 		    rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, uio->uio_resid);
527480fefe0aSRandall Stewart 	}
5275b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5276f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SORECV_ENTERPL,
52774e88d37aSMichael Tuexen 		    rwnd_req, block_allowed, so->so_rcv.sb_cc, uio->uio_resid);
527880fefe0aSRandall Stewart 	}
5279265de5bbSRobert Watson 	error = sblock(&so->so_rcv, (block_allowed ? SBL_WAIT : 0));
5280f8829a4aSRandall Stewart 	if (error) {
5281f8829a4aSRandall Stewart 		goto release_unlocked;
5282f8829a4aSRandall Stewart 	}
52838e1e6e5fSMateusz Guzik 	sockbuf_lock = 1;
5284f8829a4aSRandall Stewart restart:
52857abab911SRobert Watson 
5286f8829a4aSRandall Stewart 
5287f8829a4aSRandall Stewart restart_nosblocks:
5288f8829a4aSRandall Stewart 	if (hold_sblock == 0) {
5289f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&so->so_rcv);
5290f8829a4aSRandall Stewart 		hold_sblock = 1;
5291f8829a4aSRandall Stewart 	}
5292f8829a4aSRandall Stewart 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5293f8829a4aSRandall Stewart 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5294f8829a4aSRandall Stewart 		goto out;
5295f8829a4aSRandall Stewart 	}
52964e88d37aSMichael Tuexen 	if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) && (so->so_rcv.sb_cc == 0)) {
5297f8829a4aSRandall Stewart 		if (so->so_error) {
5298f8829a4aSRandall Stewart 			error = so->so_error;
529944b7479bSRandall Stewart 			if ((in_flags & MSG_PEEK) == 0)
530044b7479bSRandall Stewart 				so->so_error = 0;
53019f22f500SRandall Stewart 			goto out;
5302f8829a4aSRandall Stewart 		} else {
53034e88d37aSMichael Tuexen 			if (so->so_rcv.sb_cc == 0) {
53047924093fSRandall Stewart 				/* indicate EOF */
53057924093fSRandall Stewart 				error = 0;
5306f8829a4aSRandall Stewart 				goto out;
5307f8829a4aSRandall Stewart 			}
53089f22f500SRandall Stewart 		}
53099f22f500SRandall Stewart 	}
53104e88d37aSMichael Tuexen 	if ((so->so_rcv.sb_cc <= held_length) && block_allowed) {
5311f8829a4aSRandall Stewart 		/* we need to wait for data */
53124e88d37aSMichael Tuexen 		if ((so->so_rcv.sb_cc == 0) &&
5313f8829a4aSRandall Stewart 		    ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5314f8829a4aSRandall Stewart 		    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
5315f8829a4aSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
5316f8829a4aSRandall Stewart 				/*
5317f8829a4aSRandall Stewart 				 * For active open side clear flags for
5318f8829a4aSRandall Stewart 				 * re-use passive open is blocked by
5319f8829a4aSRandall Stewart 				 * connect.
5320f8829a4aSRandall Stewart 				 */
5321f8829a4aSRandall Stewart 				if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
5322f8829a4aSRandall Stewart 					/*
5323f8829a4aSRandall Stewart 					 * You were aborted, passive side
5324f8829a4aSRandall Stewart 					 * always hits here
5325f8829a4aSRandall Stewart 					 */
5326c4739e2fSRandall Stewart 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
5327f8829a4aSRandall Stewart 					error = ECONNRESET;
5328f8829a4aSRandall Stewart 				}
5329f8829a4aSRandall Stewart 				so->so_state &= ~(SS_ISCONNECTING |
5330f8829a4aSRandall Stewart 				    SS_ISDISCONNECTING |
5331f8829a4aSRandall Stewart 				    SS_ISCONFIRMING |
5332f8829a4aSRandall Stewart 				    SS_ISCONNECTED);
5333f8829a4aSRandall Stewart 				if (error == 0) {
5334f8829a4aSRandall Stewart 					if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5335c4739e2fSRandall Stewart 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5336f8829a4aSRandall Stewart 						error = ENOTCONN;
5337f8829a4aSRandall Stewart 					}
5338f8829a4aSRandall Stewart 				}
5339f8829a4aSRandall Stewart 				goto out;
5340f8829a4aSRandall Stewart 			}
5341f8829a4aSRandall Stewart 		}
5342f8829a4aSRandall Stewart 		error = sbwait(&so->so_rcv);
5343f8829a4aSRandall Stewart 		if (error) {
5344f8829a4aSRandall Stewart 			goto out;
5345f8829a4aSRandall Stewart 		}
5346f8829a4aSRandall Stewart 		held_length = 0;
5347f8829a4aSRandall Stewart 		goto restart_nosblocks;
53484e88d37aSMichael Tuexen 	} else if (so->so_rcv.sb_cc == 0) {
534944b7479bSRandall Stewart 		if (so->so_error) {
535044b7479bSRandall Stewart 			error = so->so_error;
535144b7479bSRandall Stewart 			if ((in_flags & MSG_PEEK) == 0)
535244b7479bSRandall Stewart 				so->so_error = 0;
535344b7479bSRandall Stewart 		} else {
535444b7479bSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
535544b7479bSRandall Stewart 			    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
535644b7479bSRandall Stewart 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
535744b7479bSRandall Stewart 					/*
535844b7479bSRandall Stewart 					 * For active open side clear flags
535944b7479bSRandall Stewart 					 * for re-use passive open is
536044b7479bSRandall Stewart 					 * blocked by connect.
536144b7479bSRandall Stewart 					 */
536244b7479bSRandall Stewart 					if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
536344b7479bSRandall Stewart 						/*
536444b7479bSRandall Stewart 						 * You were aborted, passive
536544b7479bSRandall Stewart 						 * side always hits here
536644b7479bSRandall Stewart 						 */
5367c4739e2fSRandall Stewart 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
536844b7479bSRandall Stewart 						error = ECONNRESET;
536944b7479bSRandall Stewart 					}
537044b7479bSRandall Stewart 					so->so_state &= ~(SS_ISCONNECTING |
537144b7479bSRandall Stewart 					    SS_ISDISCONNECTING |
537244b7479bSRandall Stewart 					    SS_ISCONFIRMING |
537344b7479bSRandall Stewart 					    SS_ISCONNECTED);
537444b7479bSRandall Stewart 					if (error == 0) {
537544b7479bSRandall Stewart 						if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5376c4739e2fSRandall Stewart 							SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
537744b7479bSRandall Stewart 							error = ENOTCONN;
537844b7479bSRandall Stewart 						}
537944b7479bSRandall Stewart 					}
538044b7479bSRandall Stewart 					goto out;
538144b7479bSRandall Stewart 				}
538244b7479bSRandall Stewart 			}
5383c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EWOULDBLOCK);
5384f8829a4aSRandall Stewart 			error = EWOULDBLOCK;
538544b7479bSRandall Stewart 		}
5386f8829a4aSRandall Stewart 		goto out;
5387f8829a4aSRandall Stewart 	}
5388d06c82f1SRandall Stewart 	if (hold_sblock == 1) {
5389d06c82f1SRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
5390d06c82f1SRandall Stewart 		hold_sblock = 0;
5391d06c82f1SRandall Stewart 	}
5392f8829a4aSRandall Stewart 	/* we possibly have data we can read */
53933c503c28SRandall Stewart 	/* sa_ignore FREED_MEMORY */
5394f8829a4aSRandall Stewart 	control = TAILQ_FIRST(&inp->read_queue);
5395f8829a4aSRandall Stewart 	if (control == NULL) {
5396f8829a4aSRandall Stewart 		/*
5397f8829a4aSRandall Stewart 		 * This could be happening since the appender did the
5398f8829a4aSRandall Stewart 		 * increment but as not yet did the tailq insert onto the
5399f8829a4aSRandall Stewart 		 * read_queue
5400f8829a4aSRandall Stewart 		 */
5401f8829a4aSRandall Stewart 		if (hold_rlock == 0) {
5402f8829a4aSRandall Stewart 			SCTP_INP_READ_LOCK(inp);
5403f8829a4aSRandall Stewart 		}
5404f8829a4aSRandall Stewart 		control = TAILQ_FIRST(&inp->read_queue);
54054e88d37aSMichael Tuexen 		if ((control == NULL) && (so->so_rcv.sb_cc != 0)) {
5406a5d547adSRandall Stewart #ifdef INVARIANTS
5407f8829a4aSRandall Stewart 			panic("Huh, its non zero and nothing on control?");
5408f8829a4aSRandall Stewart #endif
54094e88d37aSMichael Tuexen 			so->so_rcv.sb_cc = 0;
5410f8829a4aSRandall Stewart 		}
5411f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
5412f8829a4aSRandall Stewart 		hold_rlock = 0;
5413f8829a4aSRandall Stewart 		goto restart;
5414f8829a4aSRandall Stewart 	}
5415f8829a4aSRandall Stewart 	if ((control->length == 0) &&
5416f8829a4aSRandall Stewart 	    (control->do_not_ref_stcb)) {
5417f8829a4aSRandall Stewart 		/*
5418f8829a4aSRandall Stewart 		 * Clean up code for freeing assoc that left behind a
5419f8829a4aSRandall Stewart 		 * pdapi.. maybe a peer in EEOR that just closed after
5420f8829a4aSRandall Stewart 		 * sending and never indicated a EOR.
5421f8829a4aSRandall Stewart 		 */
5422f8829a4aSRandall Stewart 		if (hold_rlock == 0) {
5423f8829a4aSRandall Stewart 			hold_rlock = 1;
5424f8829a4aSRandall Stewart 			SCTP_INP_READ_LOCK(inp);
5425f8829a4aSRandall Stewart 		}
5426f8829a4aSRandall Stewart 		control->held_length = 0;
5427f8829a4aSRandall Stewart 		if (control->data) {
5428f8829a4aSRandall Stewart 			/* Hmm there is data here .. fix */
54294c9179adSRandall Stewart 			struct mbuf *m_tmp;
5430f8829a4aSRandall Stewart 			int cnt = 0;
5431f8829a4aSRandall Stewart 
54324c9179adSRandall Stewart 			m_tmp = control->data;
54334c9179adSRandall Stewart 			while (m_tmp) {
54344c9179adSRandall Stewart 				cnt += SCTP_BUF_LEN(m_tmp);
54354c9179adSRandall Stewart 				if (SCTP_BUF_NEXT(m_tmp) == NULL) {
54364c9179adSRandall Stewart 					control->tail_mbuf = m_tmp;
5437f8829a4aSRandall Stewart 					control->end_added = 1;
5438f8829a4aSRandall Stewart 				}
54394c9179adSRandall Stewart 				m_tmp = SCTP_BUF_NEXT(m_tmp);
5440f8829a4aSRandall Stewart 			}
5441f8829a4aSRandall Stewart 			control->length = cnt;
5442f8829a4aSRandall Stewart 		} else {
5443f8829a4aSRandall Stewart 			/* remove it */
5444f8829a4aSRandall Stewart 			TAILQ_REMOVE(&inp->read_queue, control, next);
5445f8829a4aSRandall Stewart 			/* Add back any hiddend data */
5446f8829a4aSRandall Stewart 			sctp_free_remote_addr(control->whoFrom);
5447f8829a4aSRandall Stewart 			sctp_free_a_readq(stcb, control);
5448f8829a4aSRandall Stewart 		}
5449f8829a4aSRandall Stewart 		if (hold_rlock) {
5450f8829a4aSRandall Stewart 			hold_rlock = 0;
5451f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
5452f8829a4aSRandall Stewart 		}
5453f8829a4aSRandall Stewart 		goto restart;
5454f8829a4aSRandall Stewart 	}
5455810ec536SMichael Tuexen 	if ((control->length == 0) &&
5456810ec536SMichael Tuexen 	    (control->end_added == 1)) {
5457810ec536SMichael Tuexen 		/*
5458810ec536SMichael Tuexen 		 * Do we also need to check for (control->pdapi_aborted ==
5459810ec536SMichael Tuexen 		 * 1)?
5460810ec536SMichael Tuexen 		 */
5461810ec536SMichael Tuexen 		if (hold_rlock == 0) {
5462810ec536SMichael Tuexen 			hold_rlock = 1;
5463810ec536SMichael Tuexen 			SCTP_INP_READ_LOCK(inp);
5464810ec536SMichael Tuexen 		}
5465810ec536SMichael Tuexen 		TAILQ_REMOVE(&inp->read_queue, control, next);
5466810ec536SMichael Tuexen 		if (control->data) {
5467810ec536SMichael Tuexen #ifdef INVARIANTS
5468810ec536SMichael Tuexen 			panic("control->data not null but control->length == 0");
5469810ec536SMichael Tuexen #else
5470810ec536SMichael Tuexen 			SCTP_PRINTF("Strange, data left in the control buffer. Cleaning up.\n");
5471810ec536SMichael Tuexen 			sctp_m_freem(control->data);
5472810ec536SMichael Tuexen 			control->data = NULL;
5473810ec536SMichael Tuexen #endif
5474810ec536SMichael Tuexen 		}
5475810ec536SMichael Tuexen 		if (control->aux_data) {
5476810ec536SMichael Tuexen 			sctp_m_free(control->aux_data);
5477810ec536SMichael Tuexen 			control->aux_data = NULL;
5478810ec536SMichael Tuexen 		}
5479810ec536SMichael Tuexen 		sctp_free_remote_addr(control->whoFrom);
5480810ec536SMichael Tuexen 		sctp_free_a_readq(stcb, control);
5481810ec536SMichael Tuexen 		if (hold_rlock) {
5482810ec536SMichael Tuexen 			hold_rlock = 0;
5483810ec536SMichael Tuexen 			SCTP_INP_READ_UNLOCK(inp);
5484810ec536SMichael Tuexen 		}
5485810ec536SMichael Tuexen 		goto restart;
5486810ec536SMichael Tuexen 	}
5487f8829a4aSRandall Stewart 	if (control->length == 0) {
5488f8829a4aSRandall Stewart 		if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
5489f8829a4aSRandall Stewart 		    (filling_sinfo)) {
5490f8829a4aSRandall Stewart 			/* find a more suitable one then this */
5491f8829a4aSRandall Stewart 			ctl = TAILQ_NEXT(control, next);
5492f8829a4aSRandall Stewart 			while (ctl) {
54939a6142d8SRandall Stewart 				if ((ctl->stcb != control->stcb) && (ctl->length) &&
54949a6142d8SRandall Stewart 				    (ctl->some_taken ||
54956114cd96SRandall Stewart 				    (ctl->spec_flags & M_NOTIFICATION) ||
54969a6142d8SRandall Stewart 				    ((ctl->do_not_ref_stcb == 0) &&
54979a6142d8SRandall Stewart 				    (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))
54989a6142d8SRandall Stewart 				    ) {
54999a6142d8SRandall Stewart 					/*-
55009a6142d8SRandall Stewart 					 * If we have a different TCB next, and there is data
55019a6142d8SRandall Stewart 					 * present. If we have already taken some (pdapi), OR we can
55029a6142d8SRandall Stewart 					 * ref the tcb and no delivery as started on this stream, we
550317205eccSRandall Stewart 					 * take it. Note we allow a notification on a different
550417205eccSRandall Stewart 					 * assoc to be delivered..
55059a6142d8SRandall Stewart 					 */
55069a6142d8SRandall Stewart 					control = ctl;
55079a6142d8SRandall Stewart 					goto found_one;
55089a6142d8SRandall Stewart 				} else if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) &&
55099a6142d8SRandall Stewart 					    (ctl->length) &&
55109a6142d8SRandall Stewart 					    ((ctl->some_taken) ||
55119a6142d8SRandall Stewart 					    ((ctl->do_not_ref_stcb == 0) &&
551217205eccSRandall Stewart 					    ((ctl->spec_flags & M_NOTIFICATION) == 0) &&
5513b5c16493SMichael Tuexen 				    (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))) {
55149a6142d8SRandall Stewart 					/*-
55159a6142d8SRandall Stewart 					 * If we have the same tcb, and there is data present, and we
55169a6142d8SRandall Stewart 					 * have the strm interleave feature present. Then if we have
55179a6142d8SRandall Stewart 					 * taken some (pdapi) or we can refer to tht tcb AND we have
55189a6142d8SRandall Stewart 					 * not started a delivery for this stream, we can take it.
551917205eccSRandall Stewart 					 * Note we do NOT allow a notificaiton on the same assoc to
552017205eccSRandall Stewart 					 * be delivered.
55219a6142d8SRandall Stewart 					 */
5522f8829a4aSRandall Stewart 					control = ctl;
5523f8829a4aSRandall Stewart 					goto found_one;
5524f8829a4aSRandall Stewart 				}
5525f8829a4aSRandall Stewart 				ctl = TAILQ_NEXT(ctl, next);
5526f8829a4aSRandall Stewart 			}
5527f8829a4aSRandall Stewart 		}
5528f8829a4aSRandall Stewart 		/*
5529f8829a4aSRandall Stewart 		 * if we reach here, not suitable replacement is available
55304e88d37aSMichael Tuexen 		 * <or> fragment interleave is NOT on. So stuff the sb_cc
5531f8829a4aSRandall Stewart 		 * into the our held count, and its time to sleep again.
5532f8829a4aSRandall Stewart 		 */
55334e88d37aSMichael Tuexen 		held_length = so->so_rcv.sb_cc;
55344e88d37aSMichael Tuexen 		control->held_length = so->so_rcv.sb_cc;
5535f8829a4aSRandall Stewart 		goto restart;
5536f8829a4aSRandall Stewart 	}
5537f8829a4aSRandall Stewart 	/* Clear the held length since there is something to read */
5538f8829a4aSRandall Stewart 	control->held_length = 0;
5539f8829a4aSRandall Stewart 	if (hold_rlock) {
5540f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
5541f8829a4aSRandall Stewart 		hold_rlock = 0;
5542f8829a4aSRandall Stewart 	}
5543f8829a4aSRandall Stewart found_one:
5544f8829a4aSRandall Stewart 	/*
5545f8829a4aSRandall Stewart 	 * If we reach here, control has a some data for us to read off.
5546f8829a4aSRandall Stewart 	 * Note that stcb COULD be NULL.
5547f8829a4aSRandall Stewart 	 */
55489c04b296SRandall Stewart 	control->some_taken++;
5549f8829a4aSRandall Stewart 	if (hold_sblock) {
5550f8829a4aSRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
5551f8829a4aSRandall Stewart 		hold_sblock = 0;
5552f8829a4aSRandall Stewart 	}
5553f8829a4aSRandall Stewart 	stcb = control->stcb;
5554f8829a4aSRandall Stewart 	if (stcb) {
55550696e120SRandall Stewart 		if ((control->do_not_ref_stcb == 0) &&
55560696e120SRandall Stewart 		    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
555750cec919SRandall Stewart 			if (freecnt_applied == 0)
5558f8829a4aSRandall Stewart 				stcb = NULL;
5559f8829a4aSRandall Stewart 		} else if (control->do_not_ref_stcb == 0) {
5560f8829a4aSRandall Stewart 			/* you can't free it on me please */
5561f8829a4aSRandall Stewart 			/*
5562f8829a4aSRandall Stewart 			 * The lock on the socket buffer protects us so the
5563f8829a4aSRandall Stewart 			 * free code will stop. But since we used the
5564f8829a4aSRandall Stewart 			 * socketbuf lock and the sender uses the tcb_lock
5565f8829a4aSRandall Stewart 			 * to increment, we need to use the atomic add to
5566f8829a4aSRandall Stewart 			 * the refcnt
5567f8829a4aSRandall Stewart 			 */
5568d55b0b1bSRandall Stewart 			if (freecnt_applied) {
5569d55b0b1bSRandall Stewart #ifdef INVARIANTS
5570207304d4SRandall Stewart 				panic("refcnt already incremented");
5571d55b0b1bSRandall Stewart #else
5572cd3fd531SMichael Tuexen 				SCTP_PRINTF("refcnt already incremented?\n");
5573d55b0b1bSRandall Stewart #endif
5574d55b0b1bSRandall Stewart 			} else {
557550cec919SRandall Stewart 				atomic_add_int(&stcb->asoc.refcnt, 1);
5576f8829a4aSRandall Stewart 				freecnt_applied = 1;
5577d55b0b1bSRandall Stewart 			}
5578f8829a4aSRandall Stewart 			/*
5579f8829a4aSRandall Stewart 			 * Setup to remember how much we have not yet told
5580f8829a4aSRandall Stewart 			 * the peer our rwnd has opened up. Note we grab the
5581f8829a4aSRandall Stewart 			 * value from the tcb from last time. Note too that
55820696e120SRandall Stewart 			 * sack sending clears this when a sack is sent,
5583f8829a4aSRandall Stewart 			 * which is fine. Once we hit the rwnd_req, we then
5584f8829a4aSRandall Stewart 			 * will go to the sctp_user_rcvd() that will not
5585f8829a4aSRandall Stewart 			 * lock until it KNOWs it MUST send a WUP-SACK.
5586f8829a4aSRandall Stewart 			 */
5587f8829a4aSRandall Stewart 			freed_so_far = stcb->freed_by_sorcv_sincelast;
5588f8829a4aSRandall Stewart 			stcb->freed_by_sorcv_sincelast = 0;
5589f8829a4aSRandall Stewart 		}
5590f8829a4aSRandall Stewart 	}
55916114cd96SRandall Stewart 	if (stcb &&
55926114cd96SRandall Stewart 	    ((control->spec_flags & M_NOTIFICATION) == 0) &&
55936114cd96SRandall Stewart 	    control->do_not_ref_stcb == 0) {
5594d06c82f1SRandall Stewart 		stcb->asoc.strmin[control->sinfo_stream].delivery_started = 1;
5595d06c82f1SRandall Stewart 	}
5596f8829a4aSRandall Stewart 	/* First lets get off the sinfo and sockaddr info */
5597f8829a4aSRandall Stewart 	if ((sinfo) && filling_sinfo) {
5598f8829a4aSRandall Stewart 		memcpy(sinfo, control, sizeof(struct sctp_nonpad_sndrcvinfo));
5599f8829a4aSRandall Stewart 		nxt = TAILQ_NEXT(control, next);
5600e2e7c62eSMichael Tuexen 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
5601e2e7c62eSMichael Tuexen 		    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) {
5602f8829a4aSRandall Stewart 			struct sctp_extrcvinfo *s_extra;
5603f8829a4aSRandall Stewart 
5604f8829a4aSRandall Stewart 			s_extra = (struct sctp_extrcvinfo *)sinfo;
56059a6142d8SRandall Stewart 			if ((nxt) &&
56069a6142d8SRandall Stewart 			    (nxt->length)) {
56079a6142d8SRandall Stewart 				s_extra->sreinfo_next_flags = SCTP_NEXT_MSG_AVAIL;
5608f8829a4aSRandall Stewart 				if (nxt->sinfo_flags & SCTP_UNORDERED) {
56099a6142d8SRandall Stewart 					s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_IS_UNORDERED;
5610f8829a4aSRandall Stewart 				}
5611f42a358aSRandall Stewart 				if (nxt->spec_flags & M_NOTIFICATION) {
56129a6142d8SRandall Stewart 					s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_IS_NOTIFICATION;
5613f42a358aSRandall Stewart 				}
56149a6142d8SRandall Stewart 				s_extra->sreinfo_next_aid = nxt->sinfo_assoc_id;
56159a6142d8SRandall Stewart 				s_extra->sreinfo_next_length = nxt->length;
56169a6142d8SRandall Stewart 				s_extra->sreinfo_next_ppid = nxt->sinfo_ppid;
56179a6142d8SRandall Stewart 				s_extra->sreinfo_next_stream = nxt->sinfo_stream;
5618f8829a4aSRandall Stewart 				if (nxt->tail_mbuf != NULL) {
5619139bc87fSRandall Stewart 					if (nxt->end_added) {
56209a6142d8SRandall Stewart 						s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_ISCOMPLETE;
5621f8829a4aSRandall Stewart 					}
5622f8829a4aSRandall Stewart 				}
5623f8829a4aSRandall Stewart 			} else {
5624f8829a4aSRandall Stewart 				/*
5625f8829a4aSRandall Stewart 				 * we explicitly 0 this, since the memcpy
5626f8829a4aSRandall Stewart 				 * got some other things beyond the older
5627f8829a4aSRandall Stewart 				 * sinfo_ that is on the control's structure
5628f8829a4aSRandall Stewart 				 * :-D
5629f8829a4aSRandall Stewart 				 */
56309a6142d8SRandall Stewart 				nxt = NULL;
56319a6142d8SRandall Stewart 				s_extra->sreinfo_next_flags = SCTP_NO_NEXT_MSG;
56329a6142d8SRandall Stewart 				s_extra->sreinfo_next_aid = 0;
56339a6142d8SRandall Stewart 				s_extra->sreinfo_next_length = 0;
56349a6142d8SRandall Stewart 				s_extra->sreinfo_next_ppid = 0;
56359a6142d8SRandall Stewart 				s_extra->sreinfo_next_stream = 0;
5636f8829a4aSRandall Stewart 			}
5637f8829a4aSRandall Stewart 		}
5638f8829a4aSRandall Stewart 		/*
5639f8829a4aSRandall Stewart 		 * update off the real current cum-ack, if we have an stcb.
5640f8829a4aSRandall Stewart 		 */
56410696e120SRandall Stewart 		if ((control->do_not_ref_stcb == 0) && stcb)
5642f8829a4aSRandall Stewart 			sinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
5643f8829a4aSRandall Stewart 		/*
5644f8829a4aSRandall Stewart 		 * mask off the high bits, we keep the actual chunk bits in
5645f8829a4aSRandall Stewart 		 * there.
5646f8829a4aSRandall Stewart 		 */
5647f8829a4aSRandall Stewart 		sinfo->sinfo_flags &= 0x00ff;
56485f26a41dSRandall Stewart 		if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) {
56495f26a41dSRandall Stewart 			sinfo->sinfo_flags |= SCTP_UNORDERED;
56505f26a41dSRandall Stewart 		}
5651f8829a4aSRandall Stewart 	}
565218e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
565318e198d3SRandall Stewart 	{
565418e198d3SRandall Stewart 		int index, newindex;
565518e198d3SRandall Stewart 		struct sctp_pcbtsn_rlog *entry;
565618e198d3SRandall Stewart 
565718e198d3SRandall Stewart 		do {
565818e198d3SRandall Stewart 			index = inp->readlog_index;
565918e198d3SRandall Stewart 			newindex = index + 1;
566018e198d3SRandall Stewart 			if (newindex >= SCTP_READ_LOG_SIZE) {
566118e198d3SRandall Stewart 				newindex = 0;
566218e198d3SRandall Stewart 			}
566318e198d3SRandall Stewart 		} while (atomic_cmpset_int(&inp->readlog_index, index, newindex) == 0);
566418e198d3SRandall Stewart 		entry = &inp->readlog[index];
566518e198d3SRandall Stewart 		entry->vtag = control->sinfo_assoc_id;
566618e198d3SRandall Stewart 		entry->strm = control->sinfo_stream;
566718e198d3SRandall Stewart 		entry->seq = control->sinfo_ssn;
566818e198d3SRandall Stewart 		entry->sz = control->length;
566918e198d3SRandall Stewart 		entry->flgs = control->sinfo_flags;
567018e198d3SRandall Stewart 	}
567118e198d3SRandall Stewart #endif
5672d59107f7SMichael Tuexen 	if ((fromlen > 0) && (from != NULL)) {
5673d59107f7SMichael Tuexen 		union sctp_sockstore store;
5674d59107f7SMichael Tuexen 		size_t len;
5675d59107f7SMichael Tuexen 
5676b5b6e5c2SMichael Tuexen 		switch (control->whoFrom->ro._l_addr.sa.sa_family) {
5677b5b6e5c2SMichael Tuexen #ifdef INET6
5678b5b6e5c2SMichael Tuexen 		case AF_INET6:
5679d59107f7SMichael Tuexen 			len = sizeof(struct sockaddr_in6);
5680d59107f7SMichael Tuexen 			store.sin6 = control->whoFrom->ro._l_addr.sin6;
5681d59107f7SMichael Tuexen 			store.sin6.sin6_port = control->port_from;
5682b5b6e5c2SMichael Tuexen 			break;
5683f8829a4aSRandall Stewart #endif
5684b5b6e5c2SMichael Tuexen #ifdef INET
5685b5b6e5c2SMichael Tuexen 		case AF_INET:
5686d59107f7SMichael Tuexen #ifdef INET6
5687d59107f7SMichael Tuexen 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
5688d59107f7SMichael Tuexen 				len = sizeof(struct sockaddr_in6);
5689d59107f7SMichael Tuexen 				in6_sin_2_v4mapsin6(&control->whoFrom->ro._l_addr.sin,
5690d59107f7SMichael Tuexen 				    &store.sin6);
5691d59107f7SMichael Tuexen 				store.sin6.sin6_port = control->port_from;
5692d59107f7SMichael Tuexen 			} else {
5693d59107f7SMichael Tuexen 				len = sizeof(struct sockaddr_in);
5694d59107f7SMichael Tuexen 				store.sin = control->whoFrom->ro._l_addr.sin;
5695d59107f7SMichael Tuexen 				store.sin.sin_port = control->port_from;
5696d59107f7SMichael Tuexen 			}
5697d59107f7SMichael Tuexen #else
5698d59107f7SMichael Tuexen 			len = sizeof(struct sockaddr_in);
5699d59107f7SMichael Tuexen 			store.sin = control->whoFrom->ro._l_addr.sin;
5700d59107f7SMichael Tuexen 			store.sin.sin_port = control->port_from;
5701d59107f7SMichael Tuexen #endif
5702b5b6e5c2SMichael Tuexen 			break;
5703b5b6e5c2SMichael Tuexen #endif
5704b5b6e5c2SMichael Tuexen 		default:
5705d59107f7SMichael Tuexen 			len = 0;
5706b5b6e5c2SMichael Tuexen 			break;
5707b5b6e5c2SMichael Tuexen 		}
5708d59107f7SMichael Tuexen 		memcpy(from, &store, min((size_t)fromlen, len));
5709e0e00a4dSMichael Tuexen #ifdef INET6
5710f8829a4aSRandall Stewart 		{
5711b5b6e5c2SMichael Tuexen 			struct sockaddr_in6 lsa6, *from6;
5712f8829a4aSRandall Stewart 
5713b5b6e5c2SMichael Tuexen 			from6 = (struct sockaddr_in6 *)from;
5714b5b6e5c2SMichael Tuexen 			sctp_recover_scope_mac(from6, (&lsa6));
5715f8829a4aSRandall Stewart 		}
5716f8829a4aSRandall Stewart #endif
5717f8829a4aSRandall Stewart 	}
5718f8829a4aSRandall Stewart 	/* now copy out what data we can */
5719f8829a4aSRandall Stewart 	if (mp == NULL) {
5720f8829a4aSRandall Stewart 		/* copy out each mbuf in the chain up to length */
5721f8829a4aSRandall Stewart get_more_data:
5722f8829a4aSRandall Stewart 		m = control->data;
5723f8829a4aSRandall Stewart 		while (m) {
5724f8829a4aSRandall Stewart 			/* Move out all we can */
5725f8829a4aSRandall Stewart 			cp_len = (int)uio->uio_resid;
5726139bc87fSRandall Stewart 			my_len = (int)SCTP_BUF_LEN(m);
5727f8829a4aSRandall Stewart 			if (cp_len > my_len) {
5728f8829a4aSRandall Stewart 				/* not enough in this buf */
5729f8829a4aSRandall Stewart 				cp_len = my_len;
5730f8829a4aSRandall Stewart 			}
5731f8829a4aSRandall Stewart 			if (hold_rlock) {
5732f8829a4aSRandall Stewart 				SCTP_INP_READ_UNLOCK(inp);
5733f8829a4aSRandall Stewart 				hold_rlock = 0;
5734f8829a4aSRandall Stewart 			}
5735f8829a4aSRandall Stewart 			if (cp_len > 0)
5736f8829a4aSRandall Stewart 				error = uiomove(mtod(m, char *), cp_len, uio);
5737f8829a4aSRandall Stewart 			/* re-read */
5738f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5739f8829a4aSRandall Stewart 				goto release;
5740f8829a4aSRandall Stewart 			}
57410696e120SRandall Stewart 			if ((control->do_not_ref_stcb == 0) && stcb &&
5742f8829a4aSRandall Stewart 			    stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5743f8829a4aSRandall Stewart 				no_rcv_needed = 1;
5744f8829a4aSRandall Stewart 			}
5745f8829a4aSRandall Stewart 			if (error) {
5746f8829a4aSRandall Stewart 				/* error we are out of here */
5747f8829a4aSRandall Stewart 				goto release;
5748f8829a4aSRandall Stewart 			}
5749139bc87fSRandall Stewart 			if ((SCTP_BUF_NEXT(m) == NULL) &&
5750139bc87fSRandall Stewart 			    (cp_len >= SCTP_BUF_LEN(m)) &&
5751f8829a4aSRandall Stewart 			    ((control->end_added == 0) ||
57520696e120SRandall Stewart 			    (control->end_added &&
57530696e120SRandall Stewart 			    (TAILQ_NEXT(control, next) == NULL)))
5754f8829a4aSRandall Stewart 			    ) {
5755f8829a4aSRandall Stewart 				SCTP_INP_READ_LOCK(inp);
5756f8829a4aSRandall Stewart 				hold_rlock = 1;
5757f8829a4aSRandall Stewart 			}
5758139bc87fSRandall Stewart 			if (cp_len == SCTP_BUF_LEN(m)) {
5759139bc87fSRandall Stewart 				if ((SCTP_BUF_NEXT(m) == NULL) &&
5760139bc87fSRandall Stewart 				    (control->end_added)) {
5761f8829a4aSRandall Stewart 					out_flags |= MSG_EOR;
576252129fcdSRandall Stewart 					if ((control->do_not_ref_stcb == 0) &&
576352129fcdSRandall Stewart 					    (control->stcb != NULL) &&
576452129fcdSRandall Stewart 					    ((control->spec_flags & M_NOTIFICATION) == 0))
5765ee7f9857SRandall Stewart 						control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5766f8829a4aSRandall Stewart 				}
5767139bc87fSRandall Stewart 				if (control->spec_flags & M_NOTIFICATION) {
5768f8829a4aSRandall Stewart 					out_flags |= MSG_NOTIFICATION;
5769f8829a4aSRandall Stewart 				}
5770f8829a4aSRandall Stewart 				/* we ate up the mbuf */
5771f8829a4aSRandall Stewart 				if (in_flags & MSG_PEEK) {
5772f8829a4aSRandall Stewart 					/* just looking */
5773139bc87fSRandall Stewart 					m = SCTP_BUF_NEXT(m);
5774f8829a4aSRandall Stewart 					copied_so_far += cp_len;
5775f8829a4aSRandall Stewart 				} else {
5776f8829a4aSRandall Stewart 					/* dispose of the mbuf */
5777b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5778f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv,
5779139bc87fSRandall Stewart 						    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
578080fefe0aSRandall Stewart 					}
5781f8829a4aSRandall Stewart 					sctp_sbfree(control, stcb, &so->so_rcv, m);
5782b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5783f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv,
5784f8829a4aSRandall Stewart 						    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
578580fefe0aSRandall Stewart 					}
5786f8829a4aSRandall Stewart 					copied_so_far += cp_len;
5787f8829a4aSRandall Stewart 					freed_so_far += cp_len;
5788c4739e2fSRandall Stewart 					freed_so_far += MSIZE;
578918e198d3SRandall Stewart 					atomic_subtract_int(&control->length, cp_len);
5790f8829a4aSRandall Stewart 					control->data = sctp_m_free(m);
5791f8829a4aSRandall Stewart 					m = control->data;
5792f8829a4aSRandall Stewart 					/*
5793f8829a4aSRandall Stewart 					 * been through it all, must hold sb
5794f8829a4aSRandall Stewart 					 * lock ok to null tail
5795f8829a4aSRandall Stewart 					 */
5796f8829a4aSRandall Stewart 					if (control->data == NULL) {
5797a5d547adSRandall Stewart #ifdef INVARIANTS
5798f8829a4aSRandall Stewart 						if ((control->end_added == 0) ||
5799f8829a4aSRandall Stewart 						    (TAILQ_NEXT(control, next) == NULL)) {
5800f8829a4aSRandall Stewart 							/*
5801f8829a4aSRandall Stewart 							 * If the end is not
5802f8829a4aSRandall Stewart 							 * added, OR the
5803f8829a4aSRandall Stewart 							 * next is NOT null
5804f8829a4aSRandall Stewart 							 * we MUST have the
5805f8829a4aSRandall Stewart 							 * lock.
5806f8829a4aSRandall Stewart 							 */
5807f8829a4aSRandall Stewart 							if (mtx_owned(&inp->inp_rdata_mtx) == 0) {
5808f8829a4aSRandall Stewart 								panic("Hmm we don't own the lock?");
5809f8829a4aSRandall Stewart 							}
5810f8829a4aSRandall Stewart 						}
5811f8829a4aSRandall Stewart #endif
5812f8829a4aSRandall Stewart 						control->tail_mbuf = NULL;
5813a5d547adSRandall Stewart #ifdef INVARIANTS
5814f8829a4aSRandall Stewart 						if ((control->end_added) && ((out_flags & MSG_EOR) == 0)) {
5815f8829a4aSRandall Stewart 							panic("end_added, nothing left and no MSG_EOR");
5816f8829a4aSRandall Stewart 						}
5817f8829a4aSRandall Stewart #endif
5818f8829a4aSRandall Stewart 					}
5819f8829a4aSRandall Stewart 				}
5820f8829a4aSRandall Stewart 			} else {
5821f8829a4aSRandall Stewart 				/* Do we need to trim the mbuf? */
5822139bc87fSRandall Stewart 				if (control->spec_flags & M_NOTIFICATION) {
5823f8829a4aSRandall Stewart 					out_flags |= MSG_NOTIFICATION;
5824f8829a4aSRandall Stewart 				}
5825f8829a4aSRandall Stewart 				if ((in_flags & MSG_PEEK) == 0) {
5826139bc87fSRandall Stewart 					SCTP_BUF_RESV_UF(m, cp_len);
5827139bc87fSRandall Stewart 					SCTP_BUF_LEN(m) -= cp_len;
5828b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5829f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, cp_len);
583080fefe0aSRandall Stewart 					}
58314e88d37aSMichael Tuexen 					atomic_subtract_int(&so->so_rcv.sb_cc, cp_len);
58320696e120SRandall Stewart 					if ((control->do_not_ref_stcb == 0) &&
58330696e120SRandall Stewart 					    stcb) {
58344e88d37aSMichael Tuexen 						atomic_subtract_int(&stcb->asoc.sb_cc, cp_len);
5835f8829a4aSRandall Stewart 					}
5836f8829a4aSRandall Stewart 					copied_so_far += cp_len;
5837f8829a4aSRandall Stewart 					freed_so_far += cp_len;
5838c4739e2fSRandall Stewart 					freed_so_far += MSIZE;
5839b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5840f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb,
5841f8829a4aSRandall Stewart 						    SCTP_LOG_SBRESULT, 0);
584280fefe0aSRandall Stewart 					}
584318e198d3SRandall Stewart 					atomic_subtract_int(&control->length, cp_len);
5844f8829a4aSRandall Stewart 				} else {
5845f8829a4aSRandall Stewart 					copied_so_far += cp_len;
5846f8829a4aSRandall Stewart 				}
5847f8829a4aSRandall Stewart 			}
5848d61a0ae0SRandall Stewart 			if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) {
5849f8829a4aSRandall Stewart 				break;
5850f8829a4aSRandall Stewart 			}
5851f8829a4aSRandall Stewart 			if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5852f8829a4aSRandall Stewart 			    (control->do_not_ref_stcb == 0) &&
5853f8829a4aSRandall Stewart 			    (freed_so_far >= rwnd_req)) {
5854f8829a4aSRandall Stewart 				sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5855f8829a4aSRandall Stewart 			}
5856f8829a4aSRandall Stewart 		}		/* end while(m) */
5857f8829a4aSRandall Stewart 		/*
5858f8829a4aSRandall Stewart 		 * At this point we have looked at it all and we either have
5859f8829a4aSRandall Stewart 		 * a MSG_EOR/or read all the user wants... <OR>
5860f8829a4aSRandall Stewart 		 * control->length == 0.
5861f8829a4aSRandall Stewart 		 */
5862d61a0ae0SRandall Stewart 		if ((out_flags & MSG_EOR) && ((in_flags & MSG_PEEK) == 0)) {
5863f8829a4aSRandall Stewart 			/* we are done with this control */
5864f8829a4aSRandall Stewart 			if (control->length == 0) {
5865f8829a4aSRandall Stewart 				if (control->data) {
5866a5d547adSRandall Stewart #ifdef INVARIANTS
5867f8829a4aSRandall Stewart 					panic("control->data not null at read eor?");
5868f8829a4aSRandall Stewart #else
5869ad81507eSRandall Stewart 					SCTP_PRINTF("Strange, data left in the control buffer .. invarients would panic?\n");
5870f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
5871f8829a4aSRandall Stewart 					control->data = NULL;
5872f8829a4aSRandall Stewart #endif
5873f8829a4aSRandall Stewart 				}
5874f8829a4aSRandall Stewart 		done_with_control:
5875f8829a4aSRandall Stewart 				if (TAILQ_NEXT(control, next) == NULL) {
5876f8829a4aSRandall Stewart 					/*
5877f8829a4aSRandall Stewart 					 * If we don't have a next we need a
5878b201f536SRandall Stewart 					 * lock, if there is a next
5879b201f536SRandall Stewart 					 * interrupt is filling ahead of us
5880b201f536SRandall Stewart 					 * and we don't need a lock to
5881b201f536SRandall Stewart 					 * remove this guy (which is the
5882b201f536SRandall Stewart 					 * head of the queue).
5883f8829a4aSRandall Stewart 					 */
5884f8829a4aSRandall Stewart 					if (hold_rlock == 0) {
5885f8829a4aSRandall Stewart 						SCTP_INP_READ_LOCK(inp);
5886f8829a4aSRandall Stewart 						hold_rlock = 1;
5887f8829a4aSRandall Stewart 					}
5888f8829a4aSRandall Stewart 				}
5889f8829a4aSRandall Stewart 				TAILQ_REMOVE(&inp->read_queue, control, next);
5890f8829a4aSRandall Stewart 				/* Add back any hiddend data */
5891f8829a4aSRandall Stewart 				if (control->held_length) {
5892f8829a4aSRandall Stewart 					held_length = 0;
5893f8829a4aSRandall Stewart 					control->held_length = 0;
5894f8829a4aSRandall Stewart 					wakeup_read_socket = 1;
5895f8829a4aSRandall Stewart 				}
589617205eccSRandall Stewart 				if (control->aux_data) {
589717205eccSRandall Stewart 					sctp_m_free(control->aux_data);
589817205eccSRandall Stewart 					control->aux_data = NULL;
589917205eccSRandall Stewart 				}
5900f8829a4aSRandall Stewart 				no_rcv_needed = control->do_not_ref_stcb;
5901f8829a4aSRandall Stewart 				sctp_free_remote_addr(control->whoFrom);
5902f8829a4aSRandall Stewart 				control->data = NULL;
5903f8829a4aSRandall Stewart 				sctp_free_a_readq(stcb, control);
5904f8829a4aSRandall Stewart 				control = NULL;
59050696e120SRandall Stewart 				if ((freed_so_far >= rwnd_req) &&
59060696e120SRandall Stewart 				    (no_rcv_needed == 0))
5907f8829a4aSRandall Stewart 					sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5908f8829a4aSRandall Stewart 
5909f8829a4aSRandall Stewart 			} else {
5910f8829a4aSRandall Stewart 				/*
5911f8829a4aSRandall Stewart 				 * The user did not read all of this
5912f8829a4aSRandall Stewart 				 * message, turn off the returned MSG_EOR
5913f8829a4aSRandall Stewart 				 * since we are leaving more behind on the
5914f8829a4aSRandall Stewart 				 * control to read.
5915f8829a4aSRandall Stewart 				 */
5916a5d547adSRandall Stewart #ifdef INVARIANTS
59170696e120SRandall Stewart 				if (control->end_added &&
59180696e120SRandall Stewart 				    (control->data == NULL) &&
5919f8829a4aSRandall Stewart 				    (control->tail_mbuf == NULL)) {
5920f8829a4aSRandall Stewart 					panic("Gak, control->length is corrupt?");
5921f8829a4aSRandall Stewart 				}
5922f8829a4aSRandall Stewart #endif
5923f8829a4aSRandall Stewart 				no_rcv_needed = control->do_not_ref_stcb;
5924f8829a4aSRandall Stewart 				out_flags &= ~MSG_EOR;
5925f8829a4aSRandall Stewart 			}
5926f8829a4aSRandall Stewart 		}
5927f8829a4aSRandall Stewart 		if (out_flags & MSG_EOR) {
5928f8829a4aSRandall Stewart 			goto release;
5929f8829a4aSRandall Stewart 		}
5930f8829a4aSRandall Stewart 		if ((uio->uio_resid == 0) ||
593104aab884SMichael Tuexen 		    ((in_eeor_mode) &&
593204aab884SMichael Tuexen 		    (copied_so_far >= (uint32_t) max(so->so_rcv.sb_lowat, 1)))) {
5933f8829a4aSRandall Stewart 			goto release;
5934f8829a4aSRandall Stewart 		}
5935f8829a4aSRandall Stewart 		/*
5936f8829a4aSRandall Stewart 		 * If I hit here the receiver wants more and this message is
5937f8829a4aSRandall Stewart 		 * NOT done (pd-api). So two questions. Can we block? if not
5938f8829a4aSRandall Stewart 		 * we are done. Did the user NOT set MSG_WAITALL?
5939f8829a4aSRandall Stewart 		 */
5940f8829a4aSRandall Stewart 		if (block_allowed == 0) {
5941f8829a4aSRandall Stewart 			goto release;
5942f8829a4aSRandall Stewart 		}
5943f8829a4aSRandall Stewart 		/*
5944f8829a4aSRandall Stewart 		 * We need to wait for more data a few things: - We don't
5945f8829a4aSRandall Stewart 		 * sbunlock() so we don't get someone else reading. - We
5946f8829a4aSRandall Stewart 		 * must be sure to account for the case where what is added
5947f8829a4aSRandall Stewart 		 * is NOT to our control when we wakeup.
5948f8829a4aSRandall Stewart 		 */
5949f8829a4aSRandall Stewart 
5950f8829a4aSRandall Stewart 		/*
5951f8829a4aSRandall Stewart 		 * Do we need to tell the transport a rwnd update might be
5952f8829a4aSRandall Stewart 		 * needed before we go to sleep?
5953f8829a4aSRandall Stewart 		 */
5954f8829a4aSRandall Stewart 		if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5955f8829a4aSRandall Stewart 		    ((freed_so_far >= rwnd_req) &&
5956f8829a4aSRandall Stewart 		    (control->do_not_ref_stcb == 0) &&
5957f8829a4aSRandall Stewart 		    (no_rcv_needed == 0))) {
5958f8829a4aSRandall Stewart 			sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5959f8829a4aSRandall Stewart 		}
5960f8829a4aSRandall Stewart wait_some_more:
596144b7479bSRandall Stewart 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
5962f8829a4aSRandall Stewart 			goto release;
5963f8829a4aSRandall Stewart 		}
5964f8829a4aSRandall Stewart 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)
5965f8829a4aSRandall Stewart 			goto release;
5966f8829a4aSRandall Stewart 
5967f8829a4aSRandall Stewart 		if (hold_rlock == 1) {
5968f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
5969f8829a4aSRandall Stewart 			hold_rlock = 0;
5970f8829a4aSRandall Stewart 		}
5971f8829a4aSRandall Stewart 		if (hold_sblock == 0) {
5972f8829a4aSRandall Stewart 			SOCKBUF_LOCK(&so->so_rcv);
5973f8829a4aSRandall Stewart 			hold_sblock = 1;
5974f8829a4aSRandall Stewart 		}
5975851b7298SRandall Stewart 		if ((copied_so_far) && (control->length == 0) &&
5976b5c16493SMichael Tuexen 		    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE))) {
5977851b7298SRandall Stewart 			goto release;
5978851b7298SRandall Stewart 		}
59794e88d37aSMichael Tuexen 		if (so->so_rcv.sb_cc <= control->held_length) {
5980f8829a4aSRandall Stewart 			error = sbwait(&so->so_rcv);
5981f8829a4aSRandall Stewart 			if (error) {
5982f8829a4aSRandall Stewart 				goto release;
5983f8829a4aSRandall Stewart 			}
5984f8829a4aSRandall Stewart 			control->held_length = 0;
5985f8829a4aSRandall Stewart 		}
5986f8829a4aSRandall Stewart 		if (hold_sblock) {
5987f8829a4aSRandall Stewart 			SOCKBUF_UNLOCK(&so->so_rcv);
5988f8829a4aSRandall Stewart 			hold_sblock = 0;
5989f8829a4aSRandall Stewart 		}
5990f8829a4aSRandall Stewart 		if (control->length == 0) {
5991f8829a4aSRandall Stewart 			/* still nothing here */
5992f8829a4aSRandall Stewart 			if (control->end_added == 1) {
5993f8829a4aSRandall Stewart 				/* he aborted, or is done i.e.did a shutdown */
5994f8829a4aSRandall Stewart 				out_flags |= MSG_EOR;
59959a6142d8SRandall Stewart 				if (control->pdapi_aborted) {
59966114cd96SRandall Stewart 					if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5997ee7f9857SRandall Stewart 						control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
59989a6142d8SRandall Stewart 
599903b0b021SRandall Stewart 					out_flags |= MSG_TRUNC;
60009a6142d8SRandall Stewart 				} else {
60016114cd96SRandall Stewart 					if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
6002ee7f9857SRandall Stewart 						control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
60039a6142d8SRandall Stewart 				}
6004f8829a4aSRandall Stewart 				goto done_with_control;
6005f8829a4aSRandall Stewart 			}
60064e88d37aSMichael Tuexen 			if (so->so_rcv.sb_cc > held_length) {
60074e88d37aSMichael Tuexen 				control->held_length = so->so_rcv.sb_cc;
6008f8829a4aSRandall Stewart 				held_length = 0;
6009f8829a4aSRandall Stewart 			}
6010f8829a4aSRandall Stewart 			goto wait_some_more;
6011f8829a4aSRandall Stewart 		} else if (control->data == NULL) {
601250cec919SRandall Stewart 			/*
601350cec919SRandall Stewart 			 * we must re-sync since data is probably being
601450cec919SRandall Stewart 			 * added
601550cec919SRandall Stewart 			 */
601650cec919SRandall Stewart 			SCTP_INP_READ_LOCK(inp);
601750cec919SRandall Stewart 			if ((control->length > 0) && (control->data == NULL)) {
601850cec919SRandall Stewart 				/*
601950cec919SRandall Stewart 				 * big trouble.. we have the lock and its
602050cec919SRandall Stewart 				 * corrupt?
602150cec919SRandall Stewart 				 */
60229c04b296SRandall Stewart #ifdef INVARIANTS
6023f8829a4aSRandall Stewart 				panic("Impossible data==NULL length !=0");
60249c04b296SRandall Stewart #endif
60259c04b296SRandall Stewart 				out_flags |= MSG_EOR;
60269c04b296SRandall Stewart 				out_flags |= MSG_TRUNC;
60279c04b296SRandall Stewart 				control->length = 0;
60289c04b296SRandall Stewart 				SCTP_INP_READ_UNLOCK(inp);
60299c04b296SRandall Stewart 				goto done_with_control;
6030f8829a4aSRandall Stewart 			}
603150cec919SRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
603250cec919SRandall Stewart 			/* We will fall around to get more data */
603350cec919SRandall Stewart 		}
6034f8829a4aSRandall Stewart 		goto get_more_data;
6035f8829a4aSRandall Stewart 	} else {
603617205eccSRandall Stewart 		/*-
603717205eccSRandall Stewart 		 * Give caller back the mbuf chain,
603817205eccSRandall Stewart 		 * store in uio_resid the length
6039f8829a4aSRandall Stewart 		 */
604017205eccSRandall Stewart 		wakeup_read_socket = 0;
6041f8829a4aSRandall Stewart 		if ((control->end_added == 0) ||
6042f8829a4aSRandall Stewart 		    (TAILQ_NEXT(control, next) == NULL)) {
6043f8829a4aSRandall Stewart 			/* Need to get rlock */
6044f8829a4aSRandall Stewart 			if (hold_rlock == 0) {
6045f8829a4aSRandall Stewart 				SCTP_INP_READ_LOCK(inp);
6046f8829a4aSRandall Stewart 				hold_rlock = 1;
6047f8829a4aSRandall Stewart 			}
6048f8829a4aSRandall Stewart 		}
6049139bc87fSRandall Stewart 		if (control->end_added) {
6050f8829a4aSRandall Stewart 			out_flags |= MSG_EOR;
605160990c0cSMichael Tuexen 			if ((control->do_not_ref_stcb == 0) &&
605260990c0cSMichael Tuexen 			    (control->stcb != NULL) &&
605360990c0cSMichael Tuexen 			    ((control->spec_flags & M_NOTIFICATION) == 0))
6054ee7f9857SRandall Stewart 				control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6055f8829a4aSRandall Stewart 		}
6056139bc87fSRandall Stewart 		if (control->spec_flags & M_NOTIFICATION) {
6057f8829a4aSRandall Stewart 			out_flags |= MSG_NOTIFICATION;
6058f8829a4aSRandall Stewart 		}
605917205eccSRandall Stewart 		uio->uio_resid = control->length;
6060f8829a4aSRandall Stewart 		*mp = control->data;
6061f8829a4aSRandall Stewart 		m = control->data;
6062f8829a4aSRandall Stewart 		while (m) {
6063b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6064f8829a4aSRandall Stewart 				sctp_sblog(&so->so_rcv,
6065139bc87fSRandall Stewart 				    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
606680fefe0aSRandall Stewart 			}
6067f8829a4aSRandall Stewart 			sctp_sbfree(control, stcb, &so->so_rcv, m);
6068139bc87fSRandall Stewart 			freed_so_far += SCTP_BUF_LEN(m);
6069c4739e2fSRandall Stewart 			freed_so_far += MSIZE;
6070b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6071f8829a4aSRandall Stewart 				sctp_sblog(&so->so_rcv,
6072f8829a4aSRandall Stewart 				    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
607380fefe0aSRandall Stewart 			}
6074139bc87fSRandall Stewart 			m = SCTP_BUF_NEXT(m);
6075f8829a4aSRandall Stewart 		}
6076f8829a4aSRandall Stewart 		control->data = control->tail_mbuf = NULL;
6077f8829a4aSRandall Stewart 		control->length = 0;
6078f8829a4aSRandall Stewart 		if (out_flags & MSG_EOR) {
6079f8829a4aSRandall Stewart 			/* Done with this control */
6080f8829a4aSRandall Stewart 			goto done_with_control;
6081f8829a4aSRandall Stewart 		}
6082f8829a4aSRandall Stewart 	}
6083f8829a4aSRandall Stewart release:
6084f8829a4aSRandall Stewart 	if (hold_rlock == 1) {
6085f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
6086f8829a4aSRandall Stewart 		hold_rlock = 0;
6087f8829a4aSRandall Stewart 	}
60887abab911SRobert Watson 	if (hold_sblock == 1) {
60897abab911SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
60907abab911SRobert Watson 		hold_sblock = 0;
6091f8829a4aSRandall Stewart 	}
6092f8829a4aSRandall Stewart 	sbunlock(&so->so_rcv);
60937abab911SRobert Watson 	sockbuf_lock = 0;
6094f8829a4aSRandall Stewart 
6095f8829a4aSRandall Stewart release_unlocked:
6096f8829a4aSRandall Stewart 	if (hold_sblock) {
6097f8829a4aSRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
6098f8829a4aSRandall Stewart 		hold_sblock = 0;
6099f8829a4aSRandall Stewart 	}
6100f8829a4aSRandall Stewart 	if ((stcb) && (in_flags & MSG_PEEK) == 0) {
6101f8829a4aSRandall Stewart 		if ((freed_so_far >= rwnd_req) &&
6102f8829a4aSRandall Stewart 		    (control && (control->do_not_ref_stcb == 0)) &&
6103f8829a4aSRandall Stewart 		    (no_rcv_needed == 0))
6104f8829a4aSRandall Stewart 			sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6105f8829a4aSRandall Stewart 	}
6106f8829a4aSRandall Stewart out:
61071b9f62a0SRandall Stewart 	if (msg_flags) {
61081b9f62a0SRandall Stewart 		*msg_flags = out_flags;
61091b9f62a0SRandall Stewart 	}
61109a6142d8SRandall Stewart 	if (((out_flags & MSG_EOR) == 0) &&
61119a6142d8SRandall Stewart 	    ((in_flags & MSG_PEEK) == 0) &&
61129a6142d8SRandall Stewart 	    (sinfo) &&
6113e2e7c62eSMichael Tuexen 	    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
6114e2e7c62eSMichael Tuexen 	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO))) {
61159a6142d8SRandall Stewart 		struct sctp_extrcvinfo *s_extra;
61169a6142d8SRandall Stewart 
61179a6142d8SRandall Stewart 		s_extra = (struct sctp_extrcvinfo *)sinfo;
61189a6142d8SRandall Stewart 		s_extra->sreinfo_next_flags = SCTP_NO_NEXT_MSG;
61199a6142d8SRandall Stewart 	}
6120f8829a4aSRandall Stewart 	if (hold_rlock == 1) {
6121f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
6122f8829a4aSRandall Stewart 	}
6123f8829a4aSRandall Stewart 	if (hold_sblock) {
6124f8829a4aSRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
6125f8829a4aSRandall Stewart 	}
61267abab911SRobert Watson 	if (sockbuf_lock) {
61277abab911SRobert Watson 		sbunlock(&so->so_rcv);
61287abab911SRobert Watson 	}
612950cec919SRandall Stewart 	if (freecnt_applied) {
6130f8829a4aSRandall Stewart 		/*
6131f8829a4aSRandall Stewart 		 * The lock on the socket buffer protects us so the free
6132f8829a4aSRandall Stewart 		 * code will stop. But since we used the socketbuf lock and
6133f8829a4aSRandall Stewart 		 * the sender uses the tcb_lock to increment, we need to use
6134f8829a4aSRandall Stewart 		 * the atomic add to the refcnt.
6135f8829a4aSRandall Stewart 		 */
613650cec919SRandall Stewart 		if (stcb == NULL) {
6137df6e0cc3SRandall Stewart #ifdef INVARIANTS
613850cec919SRandall Stewart 			panic("stcb for refcnt has gone NULL?");
6139df6e0cc3SRandall Stewart 			goto stage_left;
6140df6e0cc3SRandall Stewart #else
6141df6e0cc3SRandall Stewart 			goto stage_left;
6142df6e0cc3SRandall Stewart #endif
614350cec919SRandall Stewart 		}
614450cec919SRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, -1);
6145f8829a4aSRandall Stewart 		/* Save the value back for next time */
6146f8829a4aSRandall Stewart 		stcb->freed_by_sorcv_sincelast = freed_so_far;
6147f8829a4aSRandall Stewart 	}
6148b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
6149f8829a4aSRandall Stewart 		if (stcb) {
6150f8829a4aSRandall Stewart 			sctp_misc_ints(SCTP_SORECV_DONE,
6151f8829a4aSRandall Stewart 			    freed_so_far,
6152f8829a4aSRandall Stewart 			    ((uio) ? (slen - uio->uio_resid) : slen),
6153f8829a4aSRandall Stewart 			    stcb->asoc.my_rwnd,
61544e88d37aSMichael Tuexen 			    so->so_rcv.sb_cc);
6155f8829a4aSRandall Stewart 		} else {
6156f8829a4aSRandall Stewart 			sctp_misc_ints(SCTP_SORECV_DONE,
6157f8829a4aSRandall Stewart 			    freed_so_far,
6158f8829a4aSRandall Stewart 			    ((uio) ? (slen - uio->uio_resid) : slen),
6159f8829a4aSRandall Stewart 			    0,
61604e88d37aSMichael Tuexen 			    so->so_rcv.sb_cc);
6161f8829a4aSRandall Stewart 		}
616280fefe0aSRandall Stewart 	}
6163df6e0cc3SRandall Stewart stage_left:
6164f8829a4aSRandall Stewart 	if (wakeup_read_socket) {
6165f8829a4aSRandall Stewart 		sctp_sorwakeup(inp, so);
6166f8829a4aSRandall Stewart 	}
6167f8829a4aSRandall Stewart 	return (error);
6168f8829a4aSRandall Stewart }
6169f8829a4aSRandall Stewart 
6170f8829a4aSRandall Stewart 
6171f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
6172f8829a4aSRandall Stewart struct mbuf *
6173f8829a4aSRandall Stewart sctp_m_free(struct mbuf *m)
6174f8829a4aSRandall Stewart {
6175b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6176f8829a4aSRandall Stewart 		sctp_log_mb(m, SCTP_MBUF_IFREE);
6177f8829a4aSRandall Stewart 	}
6178f8829a4aSRandall Stewart 	return (m_free(m));
6179f8829a4aSRandall Stewart }
6180f8829a4aSRandall Stewart 
6181f8829a4aSRandall Stewart void
6182f8829a4aSRandall Stewart sctp_m_freem(struct mbuf *mb)
6183f8829a4aSRandall Stewart {
6184f8829a4aSRandall Stewart 	while (mb != NULL)
6185f8829a4aSRandall Stewart 		mb = sctp_m_free(mb);
6186f8829a4aSRandall Stewart }
6187f8829a4aSRandall Stewart 
6188f8829a4aSRandall Stewart #endif
6189f8829a4aSRandall Stewart 
619042551e99SRandall Stewart int
619142551e99SRandall Stewart sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id)
619242551e99SRandall Stewart {
619342551e99SRandall Stewart 	/*
619442551e99SRandall Stewart 	 * Given a local address. For all associations that holds the
619542551e99SRandall Stewart 	 * address, request a peer-set-primary.
619642551e99SRandall Stewart 	 */
619742551e99SRandall Stewart 	struct sctp_ifa *ifa;
619842551e99SRandall Stewart 	struct sctp_laddr *wi;
619942551e99SRandall Stewart 
620042551e99SRandall Stewart 	ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0);
620142551e99SRandall Stewart 	if (ifa == NULL) {
6202c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EADDRNOTAVAIL);
620342551e99SRandall Stewart 		return (EADDRNOTAVAIL);
620442551e99SRandall Stewart 	}
620542551e99SRandall Stewart 	/*
620642551e99SRandall Stewart 	 * Now that we have the ifa we must awaken the iterator with this
620742551e99SRandall Stewart 	 * message.
620842551e99SRandall Stewart 	 */
6209b3f1ea41SRandall Stewart 	wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
621042551e99SRandall Stewart 	if (wi == NULL) {
6211c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
621242551e99SRandall Stewart 		return (ENOMEM);
621342551e99SRandall Stewart 	}
621442551e99SRandall Stewart 	/* Now incr the count and int wi structure */
621542551e99SRandall Stewart 	SCTP_INCR_LADDR_COUNT();
621642551e99SRandall Stewart 	bzero(wi, sizeof(*wi));
6217d61a0ae0SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
621842551e99SRandall Stewart 	wi->ifa = ifa;
621942551e99SRandall Stewart 	wi->action = SCTP_SET_PRIM_ADDR;
622042551e99SRandall Stewart 	atomic_add_int(&ifa->refcount, 1);
622142551e99SRandall Stewart 
622242551e99SRandall Stewart 	/* Now add it to the work queue */
6223f7517433SRandall Stewart 	SCTP_WQ_ADDR_LOCK();
622442551e99SRandall Stewart 	/*
622542551e99SRandall Stewart 	 * Should this really be a tailq? As it is we will process the
622642551e99SRandall Stewart 	 * newest first :-0
622742551e99SRandall Stewart 	 */
6228b3f1ea41SRandall Stewart 	LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
6229f7517433SRandall Stewart 	SCTP_WQ_ADDR_UNLOCK();
623042551e99SRandall Stewart 	sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
623142551e99SRandall Stewart 	    (struct sctp_inpcb *)NULL,
623242551e99SRandall Stewart 	    (struct sctp_tcb *)NULL,
623342551e99SRandall Stewart 	    (struct sctp_nets *)NULL);
623442551e99SRandall Stewart 	return (0);
623542551e99SRandall Stewart }
623642551e99SRandall Stewart 
623742551e99SRandall Stewart 
6238f8829a4aSRandall Stewart int
623917205eccSRandall Stewart sctp_soreceive(struct socket *so,
624017205eccSRandall Stewart     struct sockaddr **psa,
624117205eccSRandall Stewart     struct uio *uio,
624217205eccSRandall Stewart     struct mbuf **mp0,
624317205eccSRandall Stewart     struct mbuf **controlp,
624417205eccSRandall Stewart     int *flagsp)
6245f8829a4aSRandall Stewart {
6246f8829a4aSRandall Stewart 	int error, fromlen;
6247f8829a4aSRandall Stewart 	uint8_t sockbuf[256];
6248f8829a4aSRandall Stewart 	struct sockaddr *from;
6249f8829a4aSRandall Stewart 	struct sctp_extrcvinfo sinfo;
6250f8829a4aSRandall Stewart 	int filling_sinfo = 1;
6251f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
6252f8829a4aSRandall Stewart 
6253f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
6254f8829a4aSRandall Stewart 	/* pickup the assoc we are reading from */
6255f8829a4aSRandall Stewart 	if (inp == NULL) {
6256c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6257f8829a4aSRandall Stewart 		return (EINVAL);
6258f8829a4aSRandall Stewart 	}
6259e2e7c62eSMichael Tuexen 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) &&
6260e2e7c62eSMichael Tuexen 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
6261e2e7c62eSMichael Tuexen 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) ||
6262f8829a4aSRandall Stewart 	    (controlp == NULL)) {
6263f8829a4aSRandall Stewart 		/* user does not want the sndrcv ctl */
6264f8829a4aSRandall Stewart 		filling_sinfo = 0;
6265f8829a4aSRandall Stewart 	}
6266f8829a4aSRandall Stewart 	if (psa) {
6267f8829a4aSRandall Stewart 		from = (struct sockaddr *)sockbuf;
6268f8829a4aSRandall Stewart 		fromlen = sizeof(sockbuf);
6269f8829a4aSRandall Stewart 		from->sa_len = 0;
6270f8829a4aSRandall Stewart 	} else {
6271f8829a4aSRandall Stewart 		from = NULL;
6272f8829a4aSRandall Stewart 		fromlen = 0;
6273f8829a4aSRandall Stewart 	}
6274f8829a4aSRandall Stewart 
6275e432298aSXin LI 	if (filling_sinfo) {
6276e432298aSXin LI 		memset(&sinfo, 0, sizeof(struct sctp_extrcvinfo));
6277e432298aSXin LI 	}
6278f8829a4aSRandall Stewart 	error = sctp_sorecvmsg(so, uio, mp0, from, fromlen, flagsp,
6279f8829a4aSRandall Stewart 	    (struct sctp_sndrcvinfo *)&sinfo, filling_sinfo);
6280e432298aSXin LI 	if (controlp != NULL) {
6281f8829a4aSRandall Stewart 		/* copy back the sinfo in a CMSG format */
6282f8829a4aSRandall Stewart 		if (filling_sinfo)
6283f8829a4aSRandall Stewart 			*controlp = sctp_build_ctl_nchunk(inp,
6284f8829a4aSRandall Stewart 			    (struct sctp_sndrcvinfo *)&sinfo);
6285f8829a4aSRandall Stewart 		else
6286f8829a4aSRandall Stewart 			*controlp = NULL;
6287f8829a4aSRandall Stewart 	}
6288f8829a4aSRandall Stewart 	if (psa) {
6289f8829a4aSRandall Stewart 		/* copy back the address info */
6290f8829a4aSRandall Stewart 		if (from && from->sa_len) {
6291f8829a4aSRandall Stewart 			*psa = sodupsockaddr(from, M_NOWAIT);
6292f8829a4aSRandall Stewart 		} else {
6293f8829a4aSRandall Stewart 			*psa = NULL;
6294f8829a4aSRandall Stewart 		}
6295f8829a4aSRandall Stewart 	}
6296f8829a4aSRandall Stewart 	return (error);
6297f8829a4aSRandall Stewart }
629817205eccSRandall Stewart 
629917205eccSRandall Stewart 
630017205eccSRandall Stewart 
630117205eccSRandall Stewart 
630217205eccSRandall Stewart 
630317205eccSRandall Stewart int
6304d61a0ae0SRandall Stewart sctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr,
6305d61a0ae0SRandall Stewart     int totaddr, int *error)
630617205eccSRandall Stewart {
630717205eccSRandall Stewart 	int added = 0;
630817205eccSRandall Stewart 	int i;
630917205eccSRandall Stewart 	struct sctp_inpcb *inp;
631017205eccSRandall Stewart 	struct sockaddr *sa;
631117205eccSRandall Stewart 	size_t incr = 0;
631217205eccSRandall Stewart 
631392776dfdSMichael Tuexen #ifdef INET
631492776dfdSMichael Tuexen 	struct sockaddr_in *sin;
631592776dfdSMichael Tuexen 
631692776dfdSMichael Tuexen #endif
631792776dfdSMichael Tuexen #ifdef INET6
631892776dfdSMichael Tuexen 	struct sockaddr_in6 *sin6;
631992776dfdSMichael Tuexen 
632092776dfdSMichael Tuexen #endif
632192776dfdSMichael Tuexen 
632217205eccSRandall Stewart 	sa = addr;
632317205eccSRandall Stewart 	inp = stcb->sctp_ep;
632417205eccSRandall Stewart 	*error = 0;
632517205eccSRandall Stewart 	for (i = 0; i < totaddr; i++) {
6326ea5eba11SMichael Tuexen 		switch (sa->sa_family) {
6327ea5eba11SMichael Tuexen #ifdef INET
6328ea5eba11SMichael Tuexen 		case AF_INET:
632917205eccSRandall Stewart 			incr = sizeof(struct sockaddr_in);
633092776dfdSMichael Tuexen 			sin = (struct sockaddr_in *)sa;
633192776dfdSMichael Tuexen 			if ((sin->sin_addr.s_addr == INADDR_ANY) ||
633292776dfdSMichael Tuexen 			    (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
633392776dfdSMichael Tuexen 			    IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
633492776dfdSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
633592776dfdSMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
633692776dfdSMichael Tuexen 				*error = EINVAL;
633792776dfdSMichael Tuexen 				goto out_now;
633892776dfdSMichael Tuexen 			}
6339ca85e948SMichael Tuexen 			if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
634017205eccSRandall Stewart 				/* assoc gone no un-lock */
6341c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6342c4739e2fSRandall Stewart 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
634317205eccSRandall Stewart 				*error = ENOBUFS;
634417205eccSRandall Stewart 				goto out_now;
634517205eccSRandall Stewart 			}
634617205eccSRandall Stewart 			added++;
6347ea5eba11SMichael Tuexen 			break;
6348ea5eba11SMichael Tuexen #endif
6349ea5eba11SMichael Tuexen #ifdef INET6
6350ea5eba11SMichael Tuexen 		case AF_INET6:
635117205eccSRandall Stewart 			incr = sizeof(struct sockaddr_in6);
635292776dfdSMichael Tuexen 			sin6 = (struct sockaddr_in6 *)sa;
635392776dfdSMichael Tuexen 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
635492776dfdSMichael Tuexen 			    IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
635592776dfdSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
635692776dfdSMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_8);
635792776dfdSMichael Tuexen 				*error = EINVAL;
635892776dfdSMichael Tuexen 				goto out_now;
635992776dfdSMichael Tuexen 			}
6360ca85e948SMichael Tuexen 			if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
636117205eccSRandall Stewart 				/* assoc gone no un-lock */
6362c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6363c4739e2fSRandall Stewart 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_8);
636417205eccSRandall Stewart 				*error = ENOBUFS;
636517205eccSRandall Stewart 				goto out_now;
636617205eccSRandall Stewart 			}
636717205eccSRandall Stewart 			added++;
6368ea5eba11SMichael Tuexen 			break;
6369ea5eba11SMichael Tuexen #endif
6370ea5eba11SMichael Tuexen 		default:
6371ea5eba11SMichael Tuexen 			break;
637217205eccSRandall Stewart 		}
637317205eccSRandall Stewart 		sa = (struct sockaddr *)((caddr_t)sa + incr);
637417205eccSRandall Stewart 	}
637517205eccSRandall Stewart out_now:
637617205eccSRandall Stewart 	return (added);
637717205eccSRandall Stewart }
637817205eccSRandall Stewart 
637917205eccSRandall Stewart struct sctp_tcb *
6380d61a0ae0SRandall Stewart sctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr,
6381d61a0ae0SRandall Stewart     int *totaddr, int *num_v4, int *num_v6, int *error,
6382d61a0ae0SRandall Stewart     int limit, int *bad_addr)
638317205eccSRandall Stewart {
638417205eccSRandall Stewart 	struct sockaddr *sa;
638517205eccSRandall Stewart 	struct sctp_tcb *stcb = NULL;
638617205eccSRandall Stewart 	size_t incr, at, i;
638717205eccSRandall Stewart 
638817205eccSRandall Stewart 	at = incr = 0;
638917205eccSRandall Stewart 	sa = addr;
6390ea5eba11SMichael Tuexen 
639117205eccSRandall Stewart 	*error = *num_v6 = *num_v4 = 0;
639217205eccSRandall Stewart 	/* account and validate addresses */
63934c9179adSRandall Stewart 	for (i = 0; i < (size_t)*totaddr; i++) {
6394ea5eba11SMichael Tuexen 		switch (sa->sa_family) {
6395ea5eba11SMichael Tuexen #ifdef INET
6396ea5eba11SMichael Tuexen 		case AF_INET:
639717205eccSRandall Stewart 			(*num_v4) += 1;
639817205eccSRandall Stewart 			incr = sizeof(struct sockaddr_in);
6399d61a0ae0SRandall Stewart 			if (sa->sa_len != incr) {
6400c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6401d61a0ae0SRandall Stewart 				*error = EINVAL;
6402d61a0ae0SRandall Stewart 				*bad_addr = 1;
6403d61a0ae0SRandall Stewart 				return (NULL);
6404d61a0ae0SRandall Stewart 			}
6405ea5eba11SMichael Tuexen 			break;
6406ea5eba11SMichael Tuexen #endif
6407ea5eba11SMichael Tuexen #ifdef INET6
6408ea5eba11SMichael Tuexen 		case AF_INET6:
6409ea5eba11SMichael Tuexen 			{
641017205eccSRandall Stewart 				struct sockaddr_in6 *sin6;
641117205eccSRandall Stewart 
641217205eccSRandall Stewart 				sin6 = (struct sockaddr_in6 *)sa;
641317205eccSRandall Stewart 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
641417205eccSRandall Stewart 					/* Must be non-mapped for connectx */
6415c4739e2fSRandall Stewart 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
641617205eccSRandall Stewart 					*error = EINVAL;
6417d61a0ae0SRandall Stewart 					*bad_addr = 1;
641817205eccSRandall Stewart 					return (NULL);
641917205eccSRandall Stewart 				}
642017205eccSRandall Stewart 				(*num_v6) += 1;
642117205eccSRandall Stewart 				incr = sizeof(struct sockaddr_in6);
6422d61a0ae0SRandall Stewart 				if (sa->sa_len != incr) {
6423c4739e2fSRandall Stewart 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6424d61a0ae0SRandall Stewart 					*error = EINVAL;
6425d61a0ae0SRandall Stewart 					*bad_addr = 1;
6426d61a0ae0SRandall Stewart 					return (NULL);
6427d61a0ae0SRandall Stewart 				}
6428ea5eba11SMichael Tuexen 				break;
6429ea5eba11SMichael Tuexen 			}
6430ea5eba11SMichael Tuexen #endif
6431ea5eba11SMichael Tuexen 		default:
643217205eccSRandall Stewart 			*totaddr = i;
643317205eccSRandall Stewart 			/* we are done */
643417205eccSRandall Stewart 			break;
643517205eccSRandall Stewart 		}
6436ea5eba11SMichael Tuexen 		if (i == (size_t)*totaddr) {
6437ea5eba11SMichael Tuexen 			break;
6438ea5eba11SMichael Tuexen 		}
6439d61a0ae0SRandall Stewart 		SCTP_INP_INCR_REF(inp);
644017205eccSRandall Stewart 		stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
644117205eccSRandall Stewart 		if (stcb != NULL) {
644217205eccSRandall Stewart 			/* Already have or am bring up an association */
644317205eccSRandall Stewart 			return (stcb);
6444d61a0ae0SRandall Stewart 		} else {
6445d61a0ae0SRandall Stewart 			SCTP_INP_DECR_REF(inp);
644617205eccSRandall Stewart 		}
64474c9179adSRandall Stewart 		if ((at + incr) > (size_t)limit) {
644817205eccSRandall Stewart 			*totaddr = i;
644917205eccSRandall Stewart 			break;
645017205eccSRandall Stewart 		}
645117205eccSRandall Stewart 		sa = (struct sockaddr *)((caddr_t)sa + incr);
645217205eccSRandall Stewart 	}
645317205eccSRandall Stewart 	return ((struct sctp_tcb *)NULL);
645417205eccSRandall Stewart }
645535918f85SRandall Stewart 
645635918f85SRandall Stewart /*
645735918f85SRandall Stewart  * sctp_bindx(ADD) for one address.
645835918f85SRandall Stewart  * assumes all arguments are valid/checked by caller.
645935918f85SRandall Stewart  */
646035918f85SRandall Stewart void
646135918f85SRandall Stewart sctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp,
646235918f85SRandall Stewart     struct sockaddr *sa, sctp_assoc_t assoc_id,
646335918f85SRandall Stewart     uint32_t vrf_id, int *error, void *p)
646435918f85SRandall Stewart {
646535918f85SRandall Stewart 	struct sockaddr *addr_touse;
64665e2c2d87SRandall Stewart 
6467d59107f7SMichael Tuexen #if defined(INET) && defined(INET6)
646835918f85SRandall Stewart 	struct sockaddr_in sin;
646935918f85SRandall Stewart 
64705e2c2d87SRandall Stewart #endif
64715e2c2d87SRandall Stewart 
647235918f85SRandall Stewart 	/* see if we're bound all already! */
647335918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6474c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
647535918f85SRandall Stewart 		*error = EINVAL;
647635918f85SRandall Stewart 		return;
647735918f85SRandall Stewart 	}
647835918f85SRandall Stewart 	addr_touse = sa;
6479ea5eba11SMichael Tuexen #ifdef INET6
648035918f85SRandall Stewart 	if (sa->sa_family == AF_INET6) {
6481d59107f7SMichael Tuexen #ifdef INET
648235918f85SRandall Stewart 		struct sockaddr_in6 *sin6;
648335918f85SRandall Stewart 
6484d59107f7SMichael Tuexen #endif
648535918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6486c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
648735918f85SRandall Stewart 			*error = EINVAL;
648835918f85SRandall Stewart 			return;
648935918f85SRandall Stewart 		}
6490db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6491db4fd95bSRandall Stewart 			/* can only bind v6 on PF_INET6 sockets */
6492c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6493db4fd95bSRandall Stewart 			*error = EINVAL;
6494db4fd95bSRandall Stewart 			return;
6495db4fd95bSRandall Stewart 		}
6496d59107f7SMichael Tuexen #ifdef INET
649735918f85SRandall Stewart 		sin6 = (struct sockaddr_in6 *)addr_touse;
649835918f85SRandall Stewart 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6499db4fd95bSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6500db4fd95bSRandall Stewart 			    SCTP_IPV6_V6ONLY(inp)) {
6501db4fd95bSRandall Stewart 				/* can't bind v4-mapped on PF_INET sockets */
6502c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6503db4fd95bSRandall Stewart 				*error = EINVAL;
6504db4fd95bSRandall Stewart 				return;
6505db4fd95bSRandall Stewart 			}
650635918f85SRandall Stewart 			in6_sin6_2_sin(&sin, sin6);
650735918f85SRandall Stewart 			addr_touse = (struct sockaddr *)&sin;
650835918f85SRandall Stewart 		}
6509d59107f7SMichael Tuexen #endif
651035918f85SRandall Stewart 	}
651135918f85SRandall Stewart #endif
6512ea5eba11SMichael Tuexen #ifdef INET
651335918f85SRandall Stewart 	if (sa->sa_family == AF_INET) {
651435918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in)) {
6515c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
651635918f85SRandall Stewart 			*error = EINVAL;
651735918f85SRandall Stewart 			return;
651835918f85SRandall Stewart 		}
6519db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6520db4fd95bSRandall Stewart 		    SCTP_IPV6_V6ONLY(inp)) {
6521db4fd95bSRandall Stewart 			/* can't bind v4 on PF_INET sockets */
6522c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6523db4fd95bSRandall Stewart 			*error = EINVAL;
6524db4fd95bSRandall Stewart 			return;
6525db4fd95bSRandall Stewart 		}
652635918f85SRandall Stewart 	}
6527ea5eba11SMichael Tuexen #endif
652835918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
652935918f85SRandall Stewart 		if (p == NULL) {
653035918f85SRandall Stewart 			/* Can't get proc for Net/Open BSD */
6531c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
653235918f85SRandall Stewart 			*error = EINVAL;
653335918f85SRandall Stewart 			return;
653435918f85SRandall Stewart 		}
65351b649582SRandall Stewart 		*error = sctp_inpcb_bind(so, addr_touse, NULL, p);
653635918f85SRandall Stewart 		return;
653735918f85SRandall Stewart 	}
653835918f85SRandall Stewart 	/*
653935918f85SRandall Stewart 	 * No locks required here since bind and mgmt_ep_sa all do their own
654035918f85SRandall Stewart 	 * locking. If we do something for the FIX: below we may need to
654135918f85SRandall Stewart 	 * lock in that case.
654235918f85SRandall Stewart 	 */
654335918f85SRandall Stewart 	if (assoc_id == 0) {
654435918f85SRandall Stewart 		/* add the address */
654535918f85SRandall Stewart 		struct sctp_inpcb *lep;
654697c76f10SRandall Stewart 		struct sockaddr_in *lsin = (struct sockaddr_in *)addr_touse;
654735918f85SRandall Stewart 
654897c76f10SRandall Stewart 		/* validate the incoming port */
654997c76f10SRandall Stewart 		if ((lsin->sin_port != 0) &&
655097c76f10SRandall Stewart 		    (lsin->sin_port != inp->sctp_lport)) {
6551c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
655297c76f10SRandall Stewart 			*error = EINVAL;
655397c76f10SRandall Stewart 			return;
655497c76f10SRandall Stewart 		} else {
655597c76f10SRandall Stewart 			/* user specified 0 port, set it to existing port */
655697c76f10SRandall Stewart 			lsin->sin_port = inp->sctp_lport;
655797c76f10SRandall Stewart 		}
655897c76f10SRandall Stewart 
655935918f85SRandall Stewart 		lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id);
656035918f85SRandall Stewart 		if (lep != NULL) {
656135918f85SRandall Stewart 			/*
656235918f85SRandall Stewart 			 * We must decrement the refcount since we have the
656335918f85SRandall Stewart 			 * ep already and are binding. No remove going on
656435918f85SRandall Stewart 			 * here.
656535918f85SRandall Stewart 			 */
65666d9e8f2bSRandall Stewart 			SCTP_INP_DECR_REF(lep);
656735918f85SRandall Stewart 		}
656835918f85SRandall Stewart 		if (lep == inp) {
656935918f85SRandall Stewart 			/* already bound to it.. ok */
657035918f85SRandall Stewart 			return;
657135918f85SRandall Stewart 		} else if (lep == NULL) {
657235918f85SRandall Stewart 			((struct sockaddr_in *)addr_touse)->sin_port = 0;
657335918f85SRandall Stewart 			*error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
657435918f85SRandall Stewart 			    SCTP_ADD_IP_ADDRESS,
657580fefe0aSRandall Stewart 			    vrf_id, NULL);
657635918f85SRandall Stewart 		} else {
657735918f85SRandall Stewart 			*error = EADDRINUSE;
657835918f85SRandall Stewart 		}
657935918f85SRandall Stewart 		if (*error)
658035918f85SRandall Stewart 			return;
658135918f85SRandall Stewart 	} else {
658235918f85SRandall Stewart 		/*
658335918f85SRandall Stewart 		 * FIX: decide whether we allow assoc based bindx
658435918f85SRandall Stewart 		 */
658535918f85SRandall Stewart 	}
658635918f85SRandall Stewart }
658735918f85SRandall Stewart 
658835918f85SRandall Stewart /*
658935918f85SRandall Stewart  * sctp_bindx(DELETE) for one address.
659035918f85SRandall Stewart  * assumes all arguments are valid/checked by caller.
659135918f85SRandall Stewart  */
659235918f85SRandall Stewart void
65937215cc1bSMichael Tuexen sctp_bindx_delete_address(struct sctp_inpcb *inp,
659435918f85SRandall Stewart     struct sockaddr *sa, sctp_assoc_t assoc_id,
659535918f85SRandall Stewart     uint32_t vrf_id, int *error)
659635918f85SRandall Stewart {
659735918f85SRandall Stewart 	struct sockaddr *addr_touse;
65985e2c2d87SRandall Stewart 
6599d59107f7SMichael Tuexen #if defined(INET) && defined(INET6)
660035918f85SRandall Stewart 	struct sockaddr_in sin;
660135918f85SRandall Stewart 
66025e2c2d87SRandall Stewart #endif
66035e2c2d87SRandall Stewart 
660435918f85SRandall Stewart 	/* see if we're bound all already! */
660535918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6606c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
660735918f85SRandall Stewart 		*error = EINVAL;
660835918f85SRandall Stewart 		return;
660935918f85SRandall Stewart 	}
661035918f85SRandall Stewart 	addr_touse = sa;
6611e0e00a4dSMichael Tuexen #ifdef INET6
661235918f85SRandall Stewart 	if (sa->sa_family == AF_INET6) {
6613d59107f7SMichael Tuexen #ifdef INET
661435918f85SRandall Stewart 		struct sockaddr_in6 *sin6;
661535918f85SRandall Stewart 
6616d59107f7SMichael Tuexen #endif
6617d59107f7SMichael Tuexen 
661835918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6619c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
662035918f85SRandall Stewart 			*error = EINVAL;
662135918f85SRandall Stewart 			return;
662235918f85SRandall Stewart 		}
6623db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6624db4fd95bSRandall Stewart 			/* can only bind v6 on PF_INET6 sockets */
6625c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6626db4fd95bSRandall Stewart 			*error = EINVAL;
6627db4fd95bSRandall Stewart 			return;
6628db4fd95bSRandall Stewart 		}
6629d59107f7SMichael Tuexen #ifdef INET
663035918f85SRandall Stewart 		sin6 = (struct sockaddr_in6 *)addr_touse;
663135918f85SRandall Stewart 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6632db4fd95bSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6633db4fd95bSRandall Stewart 			    SCTP_IPV6_V6ONLY(inp)) {
6634db4fd95bSRandall Stewart 				/* can't bind mapped-v4 on PF_INET sockets */
6635c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6636db4fd95bSRandall Stewart 				*error = EINVAL;
6637db4fd95bSRandall Stewart 				return;
6638db4fd95bSRandall Stewart 			}
663935918f85SRandall Stewart 			in6_sin6_2_sin(&sin, sin6);
664035918f85SRandall Stewart 			addr_touse = (struct sockaddr *)&sin;
664135918f85SRandall Stewart 		}
6642d59107f7SMichael Tuexen #endif
664335918f85SRandall Stewart 	}
664435918f85SRandall Stewart #endif
6645ea5eba11SMichael Tuexen #ifdef INET
664635918f85SRandall Stewart 	if (sa->sa_family == AF_INET) {
664735918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in)) {
6648c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
664935918f85SRandall Stewart 			*error = EINVAL;
665035918f85SRandall Stewart 			return;
665135918f85SRandall Stewart 		}
6652db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6653db4fd95bSRandall Stewart 		    SCTP_IPV6_V6ONLY(inp)) {
6654db4fd95bSRandall Stewart 			/* can't bind v4 on PF_INET sockets */
6655c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6656db4fd95bSRandall Stewart 			*error = EINVAL;
6657db4fd95bSRandall Stewart 			return;
6658db4fd95bSRandall Stewart 		}
665935918f85SRandall Stewart 	}
6660ea5eba11SMichael Tuexen #endif
666135918f85SRandall Stewart 	/*
666235918f85SRandall Stewart 	 * No lock required mgmt_ep_sa does its own locking. If the FIX:
666335918f85SRandall Stewart 	 * below is ever changed we may need to lock before calling
666435918f85SRandall Stewart 	 * association level binding.
666535918f85SRandall Stewart 	 */
666635918f85SRandall Stewart 	if (assoc_id == 0) {
666735918f85SRandall Stewart 		/* delete the address */
666835918f85SRandall Stewart 		*error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
666935918f85SRandall Stewart 		    SCTP_DEL_IP_ADDRESS,
667080fefe0aSRandall Stewart 		    vrf_id, NULL);
667135918f85SRandall Stewart 	} else {
667235918f85SRandall Stewart 		/*
667335918f85SRandall Stewart 		 * FIX: decide whether we allow assoc based bindx
667435918f85SRandall Stewart 		 */
667535918f85SRandall Stewart 	}
667635918f85SRandall Stewart }
66771b649582SRandall Stewart 
66781b649582SRandall Stewart /*
66791b649582SRandall Stewart  * returns the valid local address count for an assoc, taking into account
66801b649582SRandall Stewart  * all scoping rules
66811b649582SRandall Stewart  */
66821b649582SRandall Stewart int
66831b649582SRandall Stewart sctp_local_addr_count(struct sctp_tcb *stcb)
66841b649582SRandall Stewart {
6685b54ddf22SMichael Tuexen 	int loopback_scope;
6686b54ddf22SMichael Tuexen 
6687b54ddf22SMichael Tuexen #if defined(INET)
6688b54ddf22SMichael Tuexen 	int ipv4_local_scope, ipv4_addr_legal;
6689b54ddf22SMichael Tuexen 
6690b54ddf22SMichael Tuexen #endif
6691b54ddf22SMichael Tuexen #if defined (INET6)
6692b54ddf22SMichael Tuexen 	int local_scope, site_scope, ipv6_addr_legal;
6693b54ddf22SMichael Tuexen 
6694b54ddf22SMichael Tuexen #endif
66951b649582SRandall Stewart 	struct sctp_vrf *vrf;
66961b649582SRandall Stewart 	struct sctp_ifn *sctp_ifn;
66971b649582SRandall Stewart 	struct sctp_ifa *sctp_ifa;
66981b649582SRandall Stewart 	int count = 0;
66991b649582SRandall Stewart 
67001b649582SRandall Stewart 	/* Turn on all the appropriate scopes */
6701a1cb341bSMichael Tuexen 	loopback_scope = stcb->asoc.scope.loopback_scope;
6702b54ddf22SMichael Tuexen #if defined(INET)
6703a1cb341bSMichael Tuexen 	ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
6704b54ddf22SMichael Tuexen 	ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
6705b54ddf22SMichael Tuexen #endif
6706b54ddf22SMichael Tuexen #if defined(INET6)
6707a1cb341bSMichael Tuexen 	local_scope = stcb->asoc.scope.local_scope;
6708a1cb341bSMichael Tuexen 	site_scope = stcb->asoc.scope.site_scope;
6709a1cb341bSMichael Tuexen 	ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
6710b54ddf22SMichael Tuexen #endif
6711c99efcf6SRandall Stewart 	SCTP_IPI_ADDR_RLOCK();
67121b649582SRandall Stewart 	vrf = sctp_find_vrf(stcb->asoc.vrf_id);
67131b649582SRandall Stewart 	if (vrf == NULL) {
67141b649582SRandall Stewart 		/* no vrf, no addresses */
6715c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RUNLOCK();
67161b649582SRandall Stewart 		return (0);
67171b649582SRandall Stewart 	}
67181b649582SRandall Stewart 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
67191b649582SRandall Stewart 		/*
67201b649582SRandall Stewart 		 * bound all case: go through all ifns on the vrf
67211b649582SRandall Stewart 		 */
67221b649582SRandall Stewart 		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
67231b649582SRandall Stewart 			if ((loopback_scope == 0) &&
67241b649582SRandall Stewart 			    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
67251b649582SRandall Stewart 				continue;
67261b649582SRandall Stewart 			}
67271b649582SRandall Stewart 			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
67281b649582SRandall Stewart 				if (sctp_is_addr_restricted(stcb, sctp_ifa))
67291b649582SRandall Stewart 					continue;
67305e2c2d87SRandall Stewart 				switch (sctp_ifa->address.sa.sa_family) {
6731ea5eba11SMichael Tuexen #ifdef INET
67325e2c2d87SRandall Stewart 				case AF_INET:
67335e2c2d87SRandall Stewart 					if (ipv4_addr_legal) {
67341b649582SRandall Stewart 						struct sockaddr_in *sin;
67351b649582SRandall Stewart 
673624aaac8dSMichael Tuexen 						sin = &sctp_ifa->address.sin;
67371b649582SRandall Stewart 						if (sin->sin_addr.s_addr == 0) {
67385e2c2d87SRandall Stewart 							/*
67395e2c2d87SRandall Stewart 							 * skip unspecified
67405e2c2d87SRandall Stewart 							 * addrs
67415e2c2d87SRandall Stewart 							 */
67421b649582SRandall Stewart 							continue;
67431b649582SRandall Stewart 						}
67446ba22f19SMichael Tuexen 						if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
67456ba22f19SMichael Tuexen 						    &sin->sin_addr) != 0) {
67466ba22f19SMichael Tuexen 							continue;
67476ba22f19SMichael Tuexen 						}
67481b649582SRandall Stewart 						if ((ipv4_local_scope == 0) &&
67491b649582SRandall Stewart 						    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
67501b649582SRandall Stewart 							continue;
67511b649582SRandall Stewart 						}
67521b649582SRandall Stewart 						/* count this one */
67531b649582SRandall Stewart 						count++;
67545e2c2d87SRandall Stewart 					} else {
67555e2c2d87SRandall Stewart 						continue;
67565e2c2d87SRandall Stewart 					}
67575e2c2d87SRandall Stewart 					break;
6758ea5eba11SMichael Tuexen #endif
67595e2c2d87SRandall Stewart #ifdef INET6
67605e2c2d87SRandall Stewart 				case AF_INET6:
67615e2c2d87SRandall Stewart 					if (ipv6_addr_legal) {
67621b649582SRandall Stewart 						struct sockaddr_in6 *sin6;
67631b649582SRandall Stewart 
676424aaac8dSMichael Tuexen 						sin6 = &sctp_ifa->address.sin6;
67651b649582SRandall Stewart 						if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
67661b649582SRandall Stewart 							continue;
67671b649582SRandall Stewart 						}
67686ba22f19SMichael Tuexen 						if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
67696ba22f19SMichael Tuexen 						    &sin6->sin6_addr) != 0) {
67706ba22f19SMichael Tuexen 							continue;
67716ba22f19SMichael Tuexen 						}
67721b649582SRandall Stewart 						if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
67731b649582SRandall Stewart 							if (local_scope == 0)
67741b649582SRandall Stewart 								continue;
67751b649582SRandall Stewart 							if (sin6->sin6_scope_id == 0) {
67761b649582SRandall Stewart 								if (sa6_recoverscope(sin6) != 0)
67771b649582SRandall Stewart 									/*
67785e2c2d87SRandall Stewart 									 *
67795e2c2d87SRandall Stewart 									 * bad
67805e2c2d87SRandall Stewart 									 *
67815e2c2d87SRandall Stewart 									 * li
67825e2c2d87SRandall Stewart 									 * nk
67835e2c2d87SRandall Stewart 									 *
67845e2c2d87SRandall Stewart 									 * loc
67855e2c2d87SRandall Stewart 									 * al
67865e2c2d87SRandall Stewart 									 *
67875e2c2d87SRandall Stewart 									 * add
67885e2c2d87SRandall Stewart 									 * re
67895e2c2d87SRandall Stewart 									 * ss
67905e2c2d87SRandall Stewart 									 * */
67911b649582SRandall Stewart 									continue;
67921b649582SRandall Stewart 							}
67931b649582SRandall Stewart 						}
67941b649582SRandall Stewart 						if ((site_scope == 0) &&
67951b649582SRandall Stewart 						    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
67961b649582SRandall Stewart 							continue;
67971b649582SRandall Stewart 						}
67981b649582SRandall Stewart 						/* count this one */
67991b649582SRandall Stewart 						count++;
68001b649582SRandall Stewart 					}
68015e2c2d87SRandall Stewart 					break;
68025e2c2d87SRandall Stewart #endif
68035e2c2d87SRandall Stewart 				default:
68045e2c2d87SRandall Stewart 					/* TSNH */
68055e2c2d87SRandall Stewart 					break;
68065e2c2d87SRandall Stewart 				}
68071b649582SRandall Stewart 			}
68081b649582SRandall Stewart 		}
68091b649582SRandall Stewart 	} else {
68101b649582SRandall Stewart 		/*
68111b649582SRandall Stewart 		 * subset bound case
68121b649582SRandall Stewart 		 */
68131b649582SRandall Stewart 		struct sctp_laddr *laddr;
68141b649582SRandall Stewart 
68151b649582SRandall Stewart 		LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list,
68161b649582SRandall Stewart 		    sctp_nxt_addr) {
68171b649582SRandall Stewart 			if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
68181b649582SRandall Stewart 				continue;
68191b649582SRandall Stewart 			}
68201b649582SRandall Stewart 			/* count this one */
68211b649582SRandall Stewart 			count++;
68221b649582SRandall Stewart 		}
68231b649582SRandall Stewart 	}
6824c99efcf6SRandall Stewart 	SCTP_IPI_ADDR_RUNLOCK();
68251b649582SRandall Stewart 	return (count);
68261b649582SRandall Stewart }
6827c4739e2fSRandall Stewart 
6828c4739e2fSRandall Stewart #if defined(SCTP_LOCAL_TRACE_BUF)
6829c4739e2fSRandall Stewart 
6830c4739e2fSRandall Stewart void
6831b27a6b7dSRandall 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)
6832c4739e2fSRandall Stewart {
6833b27a6b7dSRandall Stewart 	uint32_t saveindex, newindex;
6834c4739e2fSRandall Stewart 
6835c4739e2fSRandall Stewart 	do {
6836b3f1ea41SRandall Stewart 		saveindex = SCTP_BASE_SYSCTL(sctp_log).index;
6837c4739e2fSRandall Stewart 		if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
6838c4739e2fSRandall Stewart 			newindex = 1;
6839c4739e2fSRandall Stewart 		} else {
6840c4739e2fSRandall Stewart 			newindex = saveindex + 1;
6841c4739e2fSRandall Stewart 		}
6842b3f1ea41SRandall Stewart 	} while (atomic_cmpset_int(&SCTP_BASE_SYSCTL(sctp_log).index, saveindex, newindex) == 0);
6843c4739e2fSRandall Stewart 	if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
6844c4739e2fSRandall Stewart 		saveindex = 0;
6845c4739e2fSRandall Stewart 	}
6846b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].timestamp = SCTP_GET_CYCLECOUNT;
6847b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].subsys = subsys;
6848b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[0] = a;
6849b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[1] = b;
6850b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[2] = c;
6851b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[3] = d;
6852b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[4] = e;
6853b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[5] = f;
6854c4739e2fSRandall Stewart }
6855c4739e2fSRandall Stewart 
6856c4739e2fSRandall Stewart #endif
6857a99b6783SRandall Stewart static void
685881d3ec17SBryan Venteicher sctp_recv_udp_tunneled_packet(struct mbuf *m, int off, struct inpcb *ignored,
685981d3ec17SBryan Venteicher     const struct sockaddr *sa SCTP_UNUSED, void *ctx SCTP_UNUSED)
6860a99b6783SRandall Stewart {
6861a99b6783SRandall Stewart 	struct ip *iph;
68623a51a264SMichael Tuexen 
68633a51a264SMichael Tuexen #ifdef INET6
68643a51a264SMichael Tuexen 	struct ip6_hdr *ip6;
68653a51a264SMichael Tuexen 
68663a51a264SMichael Tuexen #endif
6867a99b6783SRandall Stewart 	struct mbuf *sp, *last;
6868a99b6783SRandall Stewart 	struct udphdr *uhdr;
6869285052f0SMichael Tuexen 	uint16_t port;
6870a99b6783SRandall Stewart 
6871a99b6783SRandall Stewart 	if ((m->m_flags & M_PKTHDR) == 0) {
6872a99b6783SRandall Stewart 		/* Can't handle one that is not a pkt hdr */
6873a99b6783SRandall Stewart 		goto out;
6874a99b6783SRandall Stewart 	}
6875285052f0SMichael Tuexen 	/* Pull the src port */
6876a99b6783SRandall Stewart 	iph = mtod(m, struct ip *);
6877a99b6783SRandall Stewart 	uhdr = (struct udphdr *)((caddr_t)iph + off);
6878a99b6783SRandall Stewart 	port = uhdr->uh_sport;
6879285052f0SMichael Tuexen 	/*
6880285052f0SMichael Tuexen 	 * Split out the mbuf chain. Leave the IP header in m, place the
6881285052f0SMichael Tuexen 	 * rest in the sp.
6882285052f0SMichael Tuexen 	 */
6883eb1b1807SGleb Smirnoff 	sp = m_split(m, off, M_NOWAIT);
6884a99b6783SRandall Stewart 	if (sp == NULL) {
6885a99b6783SRandall Stewart 		/* Gak, drop packet, we can't do a split */
6886a99b6783SRandall Stewart 		goto out;
6887a99b6783SRandall Stewart 	}
6888285052f0SMichael Tuexen 	if (sp->m_pkthdr.len < sizeof(struct udphdr) + sizeof(struct sctphdr)) {
6889285052f0SMichael Tuexen 		/* Gak, packet can't have an SCTP header in it - too small */
6890a99b6783SRandall Stewart 		m_freem(sp);
6891a99b6783SRandall Stewart 		goto out;
6892a99b6783SRandall Stewart 	}
6893285052f0SMichael Tuexen 	/* Now pull up the UDP header and SCTP header together */
6894285052f0SMichael Tuexen 	sp = m_pullup(sp, sizeof(struct udphdr) + sizeof(struct sctphdr));
6895a99b6783SRandall Stewart 	if (sp == NULL) {
6896a99b6783SRandall Stewart 		/* Gak pullup failed */
6897a99b6783SRandall Stewart 		goto out;
6898a99b6783SRandall Stewart 	}
6899285052f0SMichael Tuexen 	/* Trim out the UDP header */
6900a99b6783SRandall Stewart 	m_adj(sp, sizeof(struct udphdr));
6901a99b6783SRandall Stewart 
6902a99b6783SRandall Stewart 	/* Now reconstruct the mbuf chain */
6903285052f0SMichael Tuexen 	for (last = m; last->m_next; last = last->m_next);
6904a99b6783SRandall Stewart 	last->m_next = sp;
6905a99b6783SRandall Stewart 	m->m_pkthdr.len += sp->m_pkthdr.len;
6906a99b6783SRandall Stewart 	iph = mtod(m, struct ip *);
6907a99b6783SRandall Stewart 	switch (iph->ip_v) {
6908e6194c2eSMichael Tuexen #ifdef INET
6909a99b6783SRandall Stewart 	case IPVERSION:
691009c1c856SMichael Tuexen 		iph->ip_len = htons(ntohs(iph->ip_len) - sizeof(struct udphdr));
6911a99b6783SRandall Stewart 		sctp_input_with_port(m, off, port);
6912a99b6783SRandall Stewart 		break;
6913e6194c2eSMichael Tuexen #endif
6914a99b6783SRandall Stewart #ifdef INET6
6915a99b6783SRandall Stewart 	case IPV6_VERSION >> 4:
69163a51a264SMichael Tuexen 		ip6 = mtod(m, struct ip6_hdr *);
69173a51a264SMichael Tuexen 		ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - sizeof(struct udphdr));
69183a51a264SMichael Tuexen 		sctp6_input_with_port(&m, &off, port);
6919a99b6783SRandall Stewart 		break;
6920a99b6783SRandall Stewart #endif
6921a99b6783SRandall Stewart 	default:
6922285052f0SMichael Tuexen 		goto out;
6923a99b6783SRandall Stewart 		break;
6924a99b6783SRandall Stewart 	}
6925a99b6783SRandall Stewart 	return;
6926a99b6783SRandall Stewart out:
6927a99b6783SRandall Stewart 	m_freem(m);
6928a99b6783SRandall Stewart }
6929c54a18d2SRandall Stewart 
6930c54a18d2SRandall Stewart void
6931c54a18d2SRandall Stewart sctp_over_udp_stop(void)
6932c54a18d2SRandall Stewart {
6933a99b6783SRandall Stewart 	/*
6934a99b6783SRandall Stewart 	 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
6935a99b6783SRandall Stewart 	 * for writting!
6936a99b6783SRandall Stewart 	 */
69373a51a264SMichael Tuexen #ifdef INET
69383a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
69393a51a264SMichael Tuexen 		soclose(SCTP_BASE_INFO(udp4_tun_socket));
69403a51a264SMichael Tuexen 		SCTP_BASE_INFO(udp4_tun_socket) = NULL;
6941c54a18d2SRandall Stewart 	}
69423a51a264SMichael Tuexen #endif
69433a51a264SMichael Tuexen #ifdef INET6
69443a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
69453a51a264SMichael Tuexen 		soclose(SCTP_BASE_INFO(udp6_tun_socket));
69463a51a264SMichael Tuexen 		SCTP_BASE_INFO(udp6_tun_socket) = NULL;
69473a51a264SMichael Tuexen 	}
69483a51a264SMichael Tuexen #endif
6949a99b6783SRandall Stewart }
6950ea5eba11SMichael Tuexen 
6951c54a18d2SRandall Stewart int
6952c54a18d2SRandall Stewart sctp_over_udp_start(void)
6953c54a18d2SRandall Stewart {
6954a99b6783SRandall Stewart 	uint16_t port;
6955a99b6783SRandall Stewart 	int ret;
6956a99b6783SRandall Stewart 
69573a51a264SMichael Tuexen #ifdef INET
69583a51a264SMichael Tuexen 	struct sockaddr_in sin;
69593a51a264SMichael Tuexen 
69603a51a264SMichael Tuexen #endif
69613a51a264SMichael Tuexen #ifdef INET6
69623a51a264SMichael Tuexen 	struct sockaddr_in6 sin6;
69633a51a264SMichael Tuexen 
69643a51a264SMichael Tuexen #endif
6965a99b6783SRandall Stewart 	/*
6966a99b6783SRandall Stewart 	 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
6967a99b6783SRandall Stewart 	 * for writting!
6968a99b6783SRandall Stewart 	 */
6969a99b6783SRandall Stewart 	port = SCTP_BASE_SYSCTL(sctp_udp_tunneling_port);
69703a51a264SMichael Tuexen 	if (ntohs(port) == 0) {
6971a99b6783SRandall Stewart 		/* Must have a port set */
6972a99b6783SRandall Stewart 		return (EINVAL);
6973a99b6783SRandall Stewart 	}
69743a51a264SMichael Tuexen #ifdef INET
69753a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
6976a99b6783SRandall Stewart 		/* Already running -- must stop first */
6977a99b6783SRandall Stewart 		return (EALREADY);
6978a99b6783SRandall Stewart 	}
69793a51a264SMichael Tuexen #endif
69803a51a264SMichael Tuexen #ifdef INET6
69813a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
69823a51a264SMichael Tuexen 		/* Already running -- must stop first */
69833a51a264SMichael Tuexen 		return (EALREADY);
6984a99b6783SRandall Stewart 	}
69853a51a264SMichael Tuexen #endif
69863a51a264SMichael Tuexen #ifdef INET
69873a51a264SMichael Tuexen 	if ((ret = socreate(PF_INET, &SCTP_BASE_INFO(udp4_tun_socket),
69883a51a264SMichael Tuexen 	    SOCK_DGRAM, IPPROTO_UDP,
69893a51a264SMichael Tuexen 	    curthread->td_ucred, curthread))) {
6990a99b6783SRandall Stewart 		sctp_over_udp_stop();
6991a99b6783SRandall Stewart 		return (ret);
6992a99b6783SRandall Stewart 	}
69933a51a264SMichael Tuexen 	/* Call the special UDP hook. */
69943a51a264SMichael Tuexen 	if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp4_tun_socket),
699581d3ec17SBryan Venteicher 	    sctp_recv_udp_tunneled_packet, NULL))) {
69963a51a264SMichael Tuexen 		sctp_over_udp_stop();
69973a51a264SMichael Tuexen 		return (ret);
69983a51a264SMichael Tuexen 	}
69993a51a264SMichael Tuexen 	/* Ok, we have a socket, bind it to the port. */
70003a51a264SMichael Tuexen 	memset(&sin, 0, sizeof(struct sockaddr_in));
70013a51a264SMichael Tuexen 	sin.sin_len = sizeof(struct sockaddr_in);
70023a51a264SMichael Tuexen 	sin.sin_family = AF_INET;
70033a51a264SMichael Tuexen 	sin.sin_port = htons(port);
70043a51a264SMichael Tuexen 	if ((ret = sobind(SCTP_BASE_INFO(udp4_tun_socket),
70053a51a264SMichael Tuexen 	    (struct sockaddr *)&sin, curthread))) {
70063a51a264SMichael Tuexen 		sctp_over_udp_stop();
70073a51a264SMichael Tuexen 		return (ret);
70083a51a264SMichael Tuexen 	}
70093a51a264SMichael Tuexen #endif
70103a51a264SMichael Tuexen #ifdef INET6
70113a51a264SMichael Tuexen 	if ((ret = socreate(PF_INET6, &SCTP_BASE_INFO(udp6_tun_socket),
70123a51a264SMichael Tuexen 	    SOCK_DGRAM, IPPROTO_UDP,
70133a51a264SMichael Tuexen 	    curthread->td_ucred, curthread))) {
70143a51a264SMichael Tuexen 		sctp_over_udp_stop();
70153a51a264SMichael Tuexen 		return (ret);
70163a51a264SMichael Tuexen 	}
70173a51a264SMichael Tuexen 	/* Call the special UDP hook. */
70183a51a264SMichael Tuexen 	if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp6_tun_socket),
701981d3ec17SBryan Venteicher 	    sctp_recv_udp_tunneled_packet, NULL))) {
70203a51a264SMichael Tuexen 		sctp_over_udp_stop();
70213a51a264SMichael Tuexen 		return (ret);
70223a51a264SMichael Tuexen 	}
70233a51a264SMichael Tuexen 	/* Ok, we have a socket, bind it to the port. */
70243a51a264SMichael Tuexen 	memset(&sin6, 0, sizeof(struct sockaddr_in6));
70253a51a264SMichael Tuexen 	sin6.sin6_len = sizeof(struct sockaddr_in6);
70263a51a264SMichael Tuexen 	sin6.sin6_family = AF_INET6;
70273a51a264SMichael Tuexen 	sin6.sin6_port = htons(port);
70283a51a264SMichael Tuexen 	if ((ret = sobind(SCTP_BASE_INFO(udp6_tun_socket),
70293a51a264SMichael Tuexen 	    (struct sockaddr *)&sin6, curthread))) {
70303a51a264SMichael Tuexen 		sctp_over_udp_stop();
70313a51a264SMichael Tuexen 		return (ret);
70323a51a264SMichael Tuexen 	}
70333a51a264SMichael Tuexen #endif
7034a99b6783SRandall Stewart 	return (0);
7035c54a18d2SRandall Stewart }
7036