xref: /freebsd/sys/netinet/sctputil.c (revision 2e2d67945af94a8e25d2d5c791ccf3da6528834f)
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 
2204be807c4SMichael 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
2474be807c4SMichael Tuexen sctp_log_mbc(struct mbuf *m, int from)
2484be807c4SMichael Tuexen {
2494be807c4SMichael Tuexen 	struct mbuf *mat;
2504be807c4SMichael Tuexen 
2514be807c4SMichael Tuexen 	for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
2524be807c4SMichael Tuexen 		sctp_log_mb(mat, from);
2534be807c4SMichael Tuexen 	}
2544be807c4SMichael Tuexen }
2554be807c4SMichael Tuexen 
2564be807c4SMichael Tuexen #endif
2574be807c4SMichael Tuexen 
2584be807c4SMichael 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 
4294be807c4SMichael Tuexen #ifdef SCTP_MBCNT_LOGGING
4304be807c4SMichael 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 
4484be807c4SMichael Tuexen #endif
4494be807c4SMichael 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 
896e92c2a8dSMichael Tuexen int32_t
897e92c2a8dSMichael Tuexen sctp_map_assoc_state(int kernel_state)
898e92c2a8dSMichael Tuexen {
899e92c2a8dSMichael Tuexen 	int32_t user_state;
900e92c2a8dSMichael Tuexen 
901e92c2a8dSMichael Tuexen 	if (kernel_state & SCTP_STATE_WAS_ABORTED) {
902e92c2a8dSMichael Tuexen 		user_state = SCTP_CLOSED;
903e92c2a8dSMichael Tuexen 	} else if (kernel_state & SCTP_STATE_SHUTDOWN_PENDING) {
904e92c2a8dSMichael Tuexen 		user_state = SCTP_SHUTDOWN_PENDING;
905e92c2a8dSMichael Tuexen 	} else {
906e92c2a8dSMichael Tuexen 		switch (kernel_state & SCTP_STATE_MASK) {
907e92c2a8dSMichael Tuexen 		case SCTP_STATE_EMPTY:
908e92c2a8dSMichael Tuexen 			user_state = SCTP_CLOSED;
909e92c2a8dSMichael Tuexen 			break;
910e92c2a8dSMichael Tuexen 		case SCTP_STATE_INUSE:
911e92c2a8dSMichael Tuexen 			user_state = SCTP_CLOSED;
912e92c2a8dSMichael Tuexen 			break;
913e92c2a8dSMichael Tuexen 		case SCTP_STATE_COOKIE_WAIT:
914e92c2a8dSMichael Tuexen 			user_state = SCTP_COOKIE_WAIT;
915e92c2a8dSMichael Tuexen 			break;
916e92c2a8dSMichael Tuexen 		case SCTP_STATE_COOKIE_ECHOED:
917e92c2a8dSMichael Tuexen 			user_state = SCTP_COOKIE_ECHOED;
918e92c2a8dSMichael Tuexen 			break;
919e92c2a8dSMichael Tuexen 		case SCTP_STATE_OPEN:
920e92c2a8dSMichael Tuexen 			user_state = SCTP_ESTABLISHED;
921e92c2a8dSMichael Tuexen 			break;
922e92c2a8dSMichael Tuexen 		case SCTP_STATE_SHUTDOWN_SENT:
923e92c2a8dSMichael Tuexen 			user_state = SCTP_SHUTDOWN_SENT;
924e92c2a8dSMichael Tuexen 			break;
925e92c2a8dSMichael Tuexen 		case SCTP_STATE_SHUTDOWN_RECEIVED:
926e92c2a8dSMichael Tuexen 			user_state = SCTP_SHUTDOWN_RECEIVED;
927e92c2a8dSMichael Tuexen 			break;
928e92c2a8dSMichael Tuexen 		case SCTP_STATE_SHUTDOWN_ACK_SENT:
929e92c2a8dSMichael Tuexen 			user_state = SCTP_SHUTDOWN_ACK_SENT;
930e92c2a8dSMichael Tuexen 			break;
931e92c2a8dSMichael Tuexen 		default:
932e92c2a8dSMichael Tuexen 			user_state = SCTP_CLOSED;
933e92c2a8dSMichael Tuexen 			break;
934e92c2a8dSMichael Tuexen 		}
935e92c2a8dSMichael Tuexen 	}
936e92c2a8dSMichael Tuexen 	return (user_state);
937e92c2a8dSMichael Tuexen }
938e92c2a8dSMichael Tuexen 
939f8829a4aSRandall Stewart int
940a1cb341bSMichael Tuexen sctp_init_asoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
941b5c16493SMichael Tuexen     uint32_t override_tag, uint32_t vrf_id)
942f8829a4aSRandall Stewart {
9430696e120SRandall Stewart 	struct sctp_association *asoc;
9440696e120SRandall Stewart 
945f8829a4aSRandall Stewart 	/*
946f8829a4aSRandall Stewart 	 * Anything set to zero is taken care of by the allocation routine's
947f8829a4aSRandall Stewart 	 * bzero
948f8829a4aSRandall Stewart 	 */
949f8829a4aSRandall Stewart 
950f8829a4aSRandall Stewart 	/*
951f8829a4aSRandall Stewart 	 * Up front select what scoping to apply on addresses I tell my peer
952f8829a4aSRandall Stewart 	 * Not sure what to do with these right now, we will need to come up
953f8829a4aSRandall Stewart 	 * with a way to set them. We may need to pass them through from the
954f8829a4aSRandall Stewart 	 * caller in the sctp_aloc_assoc() function.
955f8829a4aSRandall Stewart 	 */
956f8829a4aSRandall Stewart 	int i;
957f8829a4aSRandall Stewart 
958f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
959f0396ad1SMichael Tuexen 	int j;
960f0396ad1SMichael Tuexen 
961f0396ad1SMichael Tuexen #endif
962f0396ad1SMichael Tuexen 
9630696e120SRandall Stewart 	asoc = &stcb->asoc;
964f8829a4aSRandall Stewart 	/* init all variables to a known value. */
965c4739e2fSRandall Stewart 	SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_INUSE);
966a1cb341bSMichael Tuexen 	asoc->max_burst = inp->sctp_ep.max_burst;
967a1cb341bSMichael Tuexen 	asoc->fr_max_burst = inp->sctp_ep.fr_max_burst;
968a1cb341bSMichael Tuexen 	asoc->heart_beat_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
969a1cb341bSMichael Tuexen 	asoc->cookie_life = inp->sctp_ep.def_cookie_life;
970a1cb341bSMichael Tuexen 	asoc->sctp_cmt_on_off = inp->sctp_cmt_on_off;
971f342355aSMichael Tuexen 	asoc->ecn_supported = inp->ecn_supported;
972dd973b0eSMichael Tuexen 	asoc->prsctp_supported = inp->prsctp_supported;
973c79bec9cSMichael Tuexen 	asoc->auth_supported = inp->auth_supported;
974c79bec9cSMichael Tuexen 	asoc->asconf_supported = inp->asconf_supported;
975317e00efSMichael Tuexen 	asoc->reconfig_supported = inp->reconfig_supported;
976caea9879SMichael Tuexen 	asoc->nrsack_supported = inp->nrsack_supported;
977cb9b8e6fSMichael Tuexen 	asoc->pktdrop_supported = inp->pktdrop_supported;
978ca85e948SMichael Tuexen 	asoc->sctp_cmt_pf = (uint8_t) 0;
979a1cb341bSMichael Tuexen 	asoc->sctp_frag_point = inp->sctp_frag_point;
980a1cb341bSMichael Tuexen 	asoc->sctp_features = inp->sctp_features;
981a1cb341bSMichael Tuexen 	asoc->default_dscp = inp->sctp_ep.default_dscp;
98259b6d5beSMichael Tuexen 	asoc->max_cwnd = inp->max_cwnd;
98342551e99SRandall Stewart #ifdef INET6
984a1cb341bSMichael Tuexen 	if (inp->sctp_ep.default_flowlabel) {
985a1cb341bSMichael Tuexen 		asoc->default_flowlabel = inp->sctp_ep.default_flowlabel;
98658bdb691SMichael Tuexen 	} else {
987a1cb341bSMichael Tuexen 		if (inp->ip_inp.inp.inp_flags & IN6P_AUTOFLOWLABEL) {
988a1cb341bSMichael Tuexen 			asoc->default_flowlabel = sctp_select_initial_TSN(&inp->sctp_ep);
98958bdb691SMichael Tuexen 			asoc->default_flowlabel &= 0x000fffff;
99058bdb691SMichael Tuexen 			asoc->default_flowlabel |= 0x80000000;
99158bdb691SMichael Tuexen 		} else {
992f8829a4aSRandall Stewart 			asoc->default_flowlabel = 0;
99358bdb691SMichael Tuexen 		}
99458bdb691SMichael Tuexen 	}
995f8829a4aSRandall Stewart #endif
9969f22f500SRandall Stewart 	asoc->sb_send_resv = 0;
997f8829a4aSRandall Stewart 	if (override_tag) {
998f8829a4aSRandall Stewart 		asoc->my_vtag = override_tag;
999f8829a4aSRandall Stewart 	} else {
1000a1cb341bSMichael Tuexen 		asoc->my_vtag = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
1001f8829a4aSRandall Stewart 	}
1002de0e935bSRandall Stewart 	/* Get the nonce tags */
1003a1cb341bSMichael Tuexen 	asoc->my_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
1004a1cb341bSMichael Tuexen 	asoc->peer_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
100542551e99SRandall Stewart 	asoc->vrf_id = vrf_id;
1006de0e935bSRandall Stewart 
100718e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
100818e198d3SRandall Stewart 	asoc->tsn_in_at = 0;
100918e198d3SRandall Stewart 	asoc->tsn_out_at = 0;
101018e198d3SRandall Stewart 	asoc->tsn_in_wrapped = 0;
101118e198d3SRandall Stewart 	asoc->tsn_out_wrapped = 0;
101218e198d3SRandall Stewart 	asoc->cumack_log_at = 0;
1013b201f536SRandall Stewart 	asoc->cumack_log_atsnt = 0;
101418e198d3SRandall Stewart #endif
101518e198d3SRandall Stewart #ifdef SCTP_FS_SPEC_LOG
101618e198d3SRandall Stewart 	asoc->fs_index = 0;
101718e198d3SRandall Stewart #endif
1018f8829a4aSRandall Stewart 	asoc->refcnt = 0;
1019f8829a4aSRandall Stewart 	asoc->assoc_up_sent = 0;
1020f8829a4aSRandall Stewart 	asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number = asoc->sending_seq =
1021a1cb341bSMichael Tuexen 	    sctp_select_initial_TSN(&inp->sctp_ep);
1022c54a18d2SRandall Stewart 	asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1023f8829a4aSRandall Stewart 	/* we are optimisitic here */
1024830d754dSRandall Stewart 	asoc->peer_supports_nat = 0;
1025f8829a4aSRandall Stewart 	asoc->sent_queue_retran_cnt = 0;
1026f8829a4aSRandall Stewart 
1027f8829a4aSRandall Stewart 	/* for CMT */
10288933fa13SRandall Stewart 	asoc->last_net_cmt_send_started = NULL;
1029f8829a4aSRandall Stewart 
1030f8829a4aSRandall Stewart 	/* This will need to be adjusted */
1031f8829a4aSRandall Stewart 	asoc->last_acked_seq = asoc->init_seq_number - 1;
1032f8829a4aSRandall Stewart 	asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1033f8829a4aSRandall Stewart 	asoc->asconf_seq_in = asoc->last_acked_seq;
1034f8829a4aSRandall Stewart 
1035f8829a4aSRandall Stewart 	/* here we are different, we hold the next one we expect */
1036f8829a4aSRandall Stewart 	asoc->str_reset_seq_in = asoc->last_acked_seq + 1;
1037f8829a4aSRandall Stewart 
1038a1cb341bSMichael Tuexen 	asoc->initial_init_rto_max = inp->sctp_ep.initial_init_rto_max;
1039a1cb341bSMichael Tuexen 	asoc->initial_rto = inp->sctp_ep.initial_rto;
1040f8829a4aSRandall Stewart 
1041a1cb341bSMichael Tuexen 	asoc->max_init_times = inp->sctp_ep.max_init_times;
1042a1cb341bSMichael Tuexen 	asoc->max_send_times = inp->sctp_ep.max_send_times;
1043a1cb341bSMichael Tuexen 	asoc->def_net_failure = inp->sctp_ep.def_net_failure;
1044a1cb341bSMichael Tuexen 	asoc->def_net_pf_threshold = inp->sctp_ep.def_net_pf_threshold;
1045f8829a4aSRandall Stewart 	asoc->free_chunk_cnt = 0;
1046f8829a4aSRandall Stewart 
1047f8829a4aSRandall Stewart 	asoc->iam_blocking = 0;
1048a1cb341bSMichael Tuexen 	asoc->context = inp->sctp_context;
1049a1cb341bSMichael Tuexen 	asoc->local_strreset_support = inp->local_strreset_support;
1050a1cb341bSMichael Tuexen 	asoc->def_send = inp->def_send;
1051a1cb341bSMichael Tuexen 	asoc->delayed_ack = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1052a1cb341bSMichael Tuexen 	asoc->sack_freq = inp->sctp_ep.sctp_sack_freq;
1053f8829a4aSRandall Stewart 	asoc->pr_sctp_cnt = 0;
1054f8829a4aSRandall Stewart 	asoc->total_output_queue_size = 0;
1055f8829a4aSRandall Stewart 
1056a1cb341bSMichael Tuexen 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1057a1cb341bSMichael Tuexen 		asoc->scope.ipv6_addr_legal = 1;
1058a1cb341bSMichael Tuexen 		if (SCTP_IPV6_V6ONLY(inp) == 0) {
1059a1cb341bSMichael Tuexen 			asoc->scope.ipv4_addr_legal = 1;
1060f8829a4aSRandall Stewart 		} else {
1061a1cb341bSMichael Tuexen 			asoc->scope.ipv4_addr_legal = 0;
1062f8829a4aSRandall Stewart 		}
1063f8829a4aSRandall Stewart 	} else {
1064a1cb341bSMichael Tuexen 		asoc->scope.ipv6_addr_legal = 0;
1065a1cb341bSMichael Tuexen 		asoc->scope.ipv4_addr_legal = 1;
1066f8829a4aSRandall Stewart 	}
1067f8829a4aSRandall Stewart 
1068a1cb341bSMichael Tuexen 	asoc->my_rwnd = max(SCTP_SB_LIMIT_RCV(inp->sctp_socket), SCTP_MINIMAL_RWND);
1069a1cb341bSMichael Tuexen 	asoc->peers_rwnd = SCTP_SB_LIMIT_RCV(inp->sctp_socket);
1070f8829a4aSRandall Stewart 
1071a1cb341bSMichael Tuexen 	asoc->smallest_mtu = inp->sctp_frag_point;
1072a1cb341bSMichael Tuexen 	asoc->minrto = inp->sctp_ep.sctp_minrto;
1073a1cb341bSMichael Tuexen 	asoc->maxrto = inp->sctp_ep.sctp_maxrto;
1074f8829a4aSRandall Stewart 
1075f8829a4aSRandall Stewart 	asoc->locked_on_sending = NULL;
1076f8829a4aSRandall Stewart 	asoc->stream_locked_on = 0;
1077f8829a4aSRandall Stewart 	asoc->ecn_echo_cnt_onq = 0;
1078f8829a4aSRandall Stewart 	asoc->stream_locked = 0;
1079f8829a4aSRandall Stewart 
108042551e99SRandall Stewart 	asoc->send_sack = 1;
108142551e99SRandall Stewart 
108242551e99SRandall Stewart 	LIST_INIT(&asoc->sctp_restricted_addrs);
108342551e99SRandall Stewart 
1084f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->nets);
1085f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->pending_reply_queue);
10862afb3e84SRandall Stewart 	TAILQ_INIT(&asoc->asconf_ack_sent);
1087f8829a4aSRandall Stewart 	/* Setup to fill the hb random cache at first HB */
1088f8829a4aSRandall Stewart 	asoc->hb_random_idx = 4;
1089f8829a4aSRandall Stewart 
1090a1cb341bSMichael Tuexen 	asoc->sctp_autoclose_ticks = inp->sctp_ep.auto_close_time;
1091f8829a4aSRandall Stewart 
1092a1cb341bSMichael Tuexen 	stcb->asoc.congestion_control_module = inp->sctp_ep.sctp_default_cc_module;
1093a1cb341bSMichael Tuexen 	stcb->asoc.cc_functions = sctp_cc_functions[inp->sctp_ep.sctp_default_cc_module];
1094b54d3a6cSRandall Stewart 
1095a1cb341bSMichael Tuexen 	stcb->asoc.stream_scheduling_module = inp->sctp_ep.sctp_default_ss_module;
1096a1cb341bSMichael Tuexen 	stcb->asoc.ss_functions = sctp_ss_functions[inp->sctp_ep.sctp_default_ss_module];
1097f7a77f6fSMichael Tuexen 
1098b54d3a6cSRandall Stewart 	/*
1099f8829a4aSRandall Stewart 	 * Now the stream parameters, here we allocate space for all streams
1100f8829a4aSRandall Stewart 	 * that we request by default.
1101f8829a4aSRandall Stewart 	 */
1102ea44232bSRandall Stewart 	asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams =
1103a1cb341bSMichael Tuexen 	    inp->sctp_ep.pre_open_stream_count;
1104f8829a4aSRandall Stewart 	SCTP_MALLOC(asoc->strmout, struct sctp_stream_out *,
1105f8829a4aSRandall Stewart 	    asoc->streamoutcnt * sizeof(struct sctp_stream_out),
1106207304d4SRandall Stewart 	    SCTP_M_STRMO);
1107f8829a4aSRandall Stewart 	if (asoc->strmout == NULL) {
1108f8829a4aSRandall Stewart 		/* big trouble no memory */
1109c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1110f8829a4aSRandall Stewart 		return (ENOMEM);
1111f8829a4aSRandall Stewart 	}
1112f8829a4aSRandall Stewart 	for (i = 0; i < asoc->streamoutcnt; i++) {
1113f8829a4aSRandall Stewart 		/*
1114f8829a4aSRandall Stewart 		 * inbound side must be set to 0xffff, also NOTE when we get
1115f8829a4aSRandall Stewart 		 * the INIT-ACK back (for INIT sender) we MUST reduce the
1116f8829a4aSRandall Stewart 		 * count (streamoutcnt) but first check if we sent to any of
1117f8829a4aSRandall Stewart 		 * the upper streams that were dropped (if some were). Those
1118f8829a4aSRandall Stewart 		 * that were dropped must be notified to the upper layer as
1119f8829a4aSRandall Stewart 		 * failed to send.
1120f8829a4aSRandall Stewart 		 */
1121f3b05218SMichael Tuexen 		asoc->strmout[i].next_sequence_send = 0x0;
1122f8829a4aSRandall Stewart 		TAILQ_INIT(&asoc->strmout[i].outqueue);
1123325c8c46SMichael Tuexen 		asoc->strmout[i].chunks_on_queues = 0;
1124f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
1125f0396ad1SMichael Tuexen 		for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1126f0396ad1SMichael Tuexen 			asoc->strmout[i].abandoned_sent[j] = 0;
1127f0396ad1SMichael Tuexen 			asoc->strmout[i].abandoned_unsent[j] = 0;
1128f0396ad1SMichael Tuexen 		}
1129f0396ad1SMichael Tuexen #else
1130f0396ad1SMichael Tuexen 		asoc->strmout[i].abandoned_sent[0] = 0;
1131f0396ad1SMichael Tuexen 		asoc->strmout[i].abandoned_unsent[0] = 0;
1132f0396ad1SMichael Tuexen #endif
1133f8829a4aSRandall Stewart 		asoc->strmout[i].stream_no = i;
1134f8829a4aSRandall Stewart 		asoc->strmout[i].last_msg_incomplete = 0;
11357cca1775SRandall Stewart 		asoc->strmout[i].state = SCTP_STREAM_OPENING;
1136252f7f93SMichael Tuexen 		asoc->ss_functions.sctp_ss_init_stream(&asoc->strmout[i], NULL);
1137f8829a4aSRandall Stewart 	}
1138f7a77f6fSMichael Tuexen 	asoc->ss_functions.sctp_ss_init(stcb, asoc, 0);
1139f7a77f6fSMichael Tuexen 
1140f8829a4aSRandall Stewart 	/* Now the mapping array */
1141f8829a4aSRandall Stewart 	asoc->mapping_array_size = SCTP_INITIAL_MAPPING_ARRAY;
1142f8829a4aSRandall Stewart 	SCTP_MALLOC(asoc->mapping_array, uint8_t *, asoc->mapping_array_size,
1143207304d4SRandall Stewart 	    SCTP_M_MAP);
1144f8829a4aSRandall Stewart 	if (asoc->mapping_array == NULL) {
1145207304d4SRandall Stewart 		SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1146c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1147f8829a4aSRandall Stewart 		return (ENOMEM);
1148f8829a4aSRandall Stewart 	}
1149f8829a4aSRandall Stewart 	memset(asoc->mapping_array, 0, asoc->mapping_array_size);
1150b5c16493SMichael Tuexen 	SCTP_MALLOC(asoc->nr_mapping_array, uint8_t *, asoc->mapping_array_size,
1151830d754dSRandall Stewart 	    SCTP_M_MAP);
1152bf1be571SRandall Stewart 	if (asoc->nr_mapping_array == NULL) {
1153bf1be571SRandall Stewart 		SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1154bf1be571SRandall Stewart 		SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1155bf1be571SRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1156bf1be571SRandall Stewart 		return (ENOMEM);
1157bf1be571SRandall Stewart 	}
1158b5c16493SMichael Tuexen 	memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
1159830d754dSRandall Stewart 
1160f8829a4aSRandall Stewart 	/* Now the init of the other outqueues */
1161f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->free_chunks);
1162f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->control_send_queue);
1163c54a18d2SRandall Stewart 	TAILQ_INIT(&asoc->asconf_send_queue);
1164f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->send_queue);
1165f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->sent_queue);
1166f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->reasmqueue);
1167f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->resetHead);
1168a1cb341bSMichael Tuexen 	asoc->max_inbound_streams = inp->sctp_ep.max_open_streams_intome;
1169f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->asconf_queue);
1170f8829a4aSRandall Stewart 	/* authentication fields */
1171f8829a4aSRandall Stewart 	asoc->authinfo.random = NULL;
1172830d754dSRandall Stewart 	asoc->authinfo.active_keyid = 0;
1173f8829a4aSRandall Stewart 	asoc->authinfo.assoc_key = NULL;
1174f8829a4aSRandall Stewart 	asoc->authinfo.assoc_keyid = 0;
1175f8829a4aSRandall Stewart 	asoc->authinfo.recv_key = NULL;
1176f8829a4aSRandall Stewart 	asoc->authinfo.recv_keyid = 0;
1177f8829a4aSRandall Stewart 	LIST_INIT(&asoc->shared_keys);
1178f42a358aSRandall Stewart 	asoc->marked_retrans = 0;
1179a1cb341bSMichael Tuexen 	asoc->port = inp->sctp_ep.port;
1180f42a358aSRandall Stewart 	asoc->timoinit = 0;
1181f42a358aSRandall Stewart 	asoc->timodata = 0;
1182f42a358aSRandall Stewart 	asoc->timosack = 0;
1183f42a358aSRandall Stewart 	asoc->timoshutdown = 0;
1184f42a358aSRandall Stewart 	asoc->timoheartbeat = 0;
1185f42a358aSRandall Stewart 	asoc->timocookie = 0;
1186f42a358aSRandall Stewart 	asoc->timoshutdownack = 0;
11876e55db54SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&asoc->start_time);
11886e55db54SRandall Stewart 	asoc->discontinuity_time = asoc->start_time;
1189f0396ad1SMichael Tuexen 	for (i = 0; i < SCTP_PR_SCTP_MAX + 1; i++) {
1190f0396ad1SMichael Tuexen 		asoc->abandoned_unsent[i] = 0;
1191f0396ad1SMichael Tuexen 		asoc->abandoned_sent[i] = 0;
1192f0396ad1SMichael Tuexen 	}
1193eacc51c5SRandall Stewart 	/*
1194eacc51c5SRandall Stewart 	 * sa_ignore MEMLEAK {memory is put in the assoc mapping array and
119577acdc25SRandall Stewart 	 * freed later when the association is freed.
1196eacc51c5SRandall Stewart 	 */
1197f8829a4aSRandall Stewart 	return (0);
1198f8829a4aSRandall Stewart }
1199f8829a4aSRandall Stewart 
12000e13104dSRandall Stewart void
12010e13104dSRandall Stewart sctp_print_mapping_array(struct sctp_association *asoc)
12020e13104dSRandall Stewart {
1203aed5947cSMichael Tuexen 	unsigned int i, limit;
12040e13104dSRandall Stewart 
1205cd3fd531SMichael Tuexen 	SCTP_PRINTF("Mapping array size: %d, baseTSN: %8.8x, cumAck: %8.8x, highestTSN: (%8.8x, %8.8x).\n",
12060e13104dSRandall Stewart 	    asoc->mapping_array_size,
12070e13104dSRandall Stewart 	    asoc->mapping_array_base_tsn,
12080e13104dSRandall Stewart 	    asoc->cumulative_tsn,
1209aed5947cSMichael Tuexen 	    asoc->highest_tsn_inside_map,
1210aed5947cSMichael Tuexen 	    asoc->highest_tsn_inside_nr_map);
1211aed5947cSMichael Tuexen 	for (limit = asoc->mapping_array_size; limit > 1; limit--) {
121260990c0cSMichael Tuexen 		if (asoc->mapping_array[limit - 1] != 0) {
121377acdc25SRandall Stewart 			break;
121477acdc25SRandall Stewart 		}
121577acdc25SRandall Stewart 	}
1216cd3fd531SMichael Tuexen 	SCTP_PRINTF("Renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
121777acdc25SRandall Stewart 	for (i = 0; i < limit; i++) {
1218cd3fd531SMichael Tuexen 		SCTP_PRINTF("%2.2x%c", asoc->mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
121977acdc25SRandall Stewart 	}
1220aed5947cSMichael Tuexen 	if (limit % 16)
1221cd3fd531SMichael Tuexen 		SCTP_PRINTF("\n");
1222aed5947cSMichael Tuexen 	for (limit = asoc->mapping_array_size; limit > 1; limit--) {
1223aed5947cSMichael Tuexen 		if (asoc->nr_mapping_array[limit - 1]) {
122477acdc25SRandall Stewart 			break;
122577acdc25SRandall Stewart 		}
122677acdc25SRandall Stewart 	}
1227cd3fd531SMichael Tuexen 	SCTP_PRINTF("Non renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
122877acdc25SRandall Stewart 	for (i = 0; i < limit; i++) {
1229cd3fd531SMichael Tuexen 		SCTP_PRINTF("%2.2x%c", asoc->nr_mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
12300e13104dSRandall Stewart 	}
1231aed5947cSMichael Tuexen 	if (limit % 16)
1232cd3fd531SMichael Tuexen 		SCTP_PRINTF("\n");
12330e13104dSRandall Stewart }
12340e13104dSRandall Stewart 
1235f8829a4aSRandall Stewart int
12360696e120SRandall Stewart sctp_expand_mapping_array(struct sctp_association *asoc, uint32_t needed)
1237f8829a4aSRandall Stewart {
1238f8829a4aSRandall Stewart 	/* mapping array needs to grow */
1239b5c16493SMichael Tuexen 	uint8_t *new_array1, *new_array2;
12400696e120SRandall Stewart 	uint32_t new_size;
1241f8829a4aSRandall Stewart 
12420696e120SRandall Stewart 	new_size = asoc->mapping_array_size + ((needed + 7) / 8 + SCTP_MAPPING_ARRAY_INCR);
1243b5c16493SMichael Tuexen 	SCTP_MALLOC(new_array1, uint8_t *, new_size, SCTP_M_MAP);
1244b5c16493SMichael Tuexen 	SCTP_MALLOC(new_array2, uint8_t *, new_size, SCTP_M_MAP);
1245b5c16493SMichael Tuexen 	if ((new_array1 == NULL) || (new_array2 == NULL)) {
1246f8829a4aSRandall Stewart 		/* can't get more, forget it */
1247b5c16493SMichael Tuexen 		SCTP_PRINTF("No memory for expansion of SCTP mapping array %d\n", new_size);
1248b5c16493SMichael Tuexen 		if (new_array1) {
1249b5c16493SMichael Tuexen 			SCTP_FREE(new_array1, SCTP_M_MAP);
1250b5c16493SMichael Tuexen 		}
1251b5c16493SMichael Tuexen 		if (new_array2) {
1252b5c16493SMichael Tuexen 			SCTP_FREE(new_array2, SCTP_M_MAP);
1253b5c16493SMichael Tuexen 		}
1254f8829a4aSRandall Stewart 		return (-1);
1255f8829a4aSRandall Stewart 	}
1256b5c16493SMichael Tuexen 	memset(new_array1, 0, new_size);
1257b5c16493SMichael Tuexen 	memset(new_array2, 0, new_size);
1258b5c16493SMichael Tuexen 	memcpy(new_array1, asoc->mapping_array, asoc->mapping_array_size);
1259b5c16493SMichael Tuexen 	memcpy(new_array2, asoc->nr_mapping_array, asoc->mapping_array_size);
1260207304d4SRandall Stewart 	SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1261830d754dSRandall Stewart 	SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
1262b5c16493SMichael Tuexen 	asoc->mapping_array = new_array1;
1263b5c16493SMichael Tuexen 	asoc->nr_mapping_array = new_array2;
1264b5c16493SMichael Tuexen 	asoc->mapping_array_size = new_size;
1265830d754dSRandall Stewart 	return (0);
1266830d754dSRandall Stewart }
1267830d754dSRandall Stewart 
12688933fa13SRandall Stewart 
126942551e99SRandall Stewart static void
127042551e99SRandall Stewart sctp_iterator_work(struct sctp_iterator *it)
127142551e99SRandall Stewart {
127242551e99SRandall Stewart 	int iteration_count = 0;
127342551e99SRandall Stewart 	int inp_skip = 0;
1274ec4c19fcSRandall Stewart 	int first_in = 1;
1275ec4c19fcSRandall Stewart 	struct sctp_inpcb *tinp;
127642551e99SRandall Stewart 
1277ec4c19fcSRandall Stewart 	SCTP_INP_INFO_RLOCK();
127842551e99SRandall Stewart 	SCTP_ITERATOR_LOCK();
1279ad81507eSRandall Stewart 	if (it->inp) {
1280ec4c19fcSRandall Stewart 		SCTP_INP_RLOCK(it->inp);
128142551e99SRandall Stewart 		SCTP_INP_DECR_REF(it->inp);
1282ad81507eSRandall Stewart 	}
128342551e99SRandall Stewart 	if (it->inp == NULL) {
128442551e99SRandall Stewart 		/* iterator is complete */
128542551e99SRandall Stewart done_with_iterator:
128642551e99SRandall Stewart 		SCTP_ITERATOR_UNLOCK();
1287ec4c19fcSRandall Stewart 		SCTP_INP_INFO_RUNLOCK();
128842551e99SRandall Stewart 		if (it->function_atend != NULL) {
128942551e99SRandall Stewart 			(*it->function_atend) (it->pointer, it->val);
129042551e99SRandall Stewart 		}
1291207304d4SRandall Stewart 		SCTP_FREE(it, SCTP_M_ITER);
129242551e99SRandall Stewart 		return;
129342551e99SRandall Stewart 	}
129442551e99SRandall Stewart select_a_new_ep:
1295ec4c19fcSRandall Stewart 	if (first_in) {
1296ec4c19fcSRandall Stewart 		first_in = 0;
1297ec4c19fcSRandall Stewart 	} else {
1298f7517433SRandall Stewart 		SCTP_INP_RLOCK(it->inp);
1299ec4c19fcSRandall Stewart 	}
130042551e99SRandall Stewart 	while (((it->pcb_flags) &&
130142551e99SRandall Stewart 	    ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) ||
130242551e99SRandall Stewart 	    ((it->pcb_features) &&
130342551e99SRandall Stewart 	    ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) {
130442551e99SRandall Stewart 		/* endpoint flags or features don't match, so keep looking */
130542551e99SRandall Stewart 		if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1306f7517433SRandall Stewart 			SCTP_INP_RUNLOCK(it->inp);
130742551e99SRandall Stewart 			goto done_with_iterator;
130842551e99SRandall Stewart 		}
1309ec4c19fcSRandall Stewart 		tinp = it->inp;
131042551e99SRandall Stewart 		it->inp = LIST_NEXT(it->inp, sctp_list);
1311ec4c19fcSRandall Stewart 		SCTP_INP_RUNLOCK(tinp);
131242551e99SRandall Stewart 		if (it->inp == NULL) {
131342551e99SRandall Stewart 			goto done_with_iterator;
131442551e99SRandall Stewart 		}
131542551e99SRandall Stewart 		SCTP_INP_RLOCK(it->inp);
1316f7517433SRandall Stewart 	}
131742551e99SRandall Stewart 	/* now go through each assoc which is in the desired state */
131842551e99SRandall Stewart 	if (it->done_current_ep == 0) {
131942551e99SRandall Stewart 		if (it->function_inp != NULL)
132042551e99SRandall Stewart 			inp_skip = (*it->function_inp) (it->inp, it->pointer, it->val);
132142551e99SRandall Stewart 		it->done_current_ep = 1;
132242551e99SRandall Stewart 	}
132342551e99SRandall Stewart 	if (it->stcb == NULL) {
132442551e99SRandall Stewart 		/* run the per instance function */
132542551e99SRandall Stewart 		it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
132642551e99SRandall Stewart 	}
132742551e99SRandall Stewart 	if ((inp_skip) || it->stcb == NULL) {
132842551e99SRandall Stewart 		if (it->function_inp_end != NULL) {
132942551e99SRandall Stewart 			inp_skip = (*it->function_inp_end) (it->inp,
133042551e99SRandall Stewart 			    it->pointer,
133142551e99SRandall Stewart 			    it->val);
133242551e99SRandall Stewart 		}
133342551e99SRandall Stewart 		SCTP_INP_RUNLOCK(it->inp);
133442551e99SRandall Stewart 		goto no_stcb;
133542551e99SRandall Stewart 	}
133642551e99SRandall Stewart 	while (it->stcb) {
133742551e99SRandall Stewart 		SCTP_TCB_LOCK(it->stcb);
133842551e99SRandall Stewart 		if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
133942551e99SRandall Stewart 			/* not in the right state... keep looking */
134042551e99SRandall Stewart 			SCTP_TCB_UNLOCK(it->stcb);
134142551e99SRandall Stewart 			goto next_assoc;
134242551e99SRandall Stewart 		}
134342551e99SRandall Stewart 		/* see if we have limited out the iterator loop */
134442551e99SRandall Stewart 		iteration_count++;
134542551e99SRandall Stewart 		if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) {
134642551e99SRandall Stewart 			/* Pause to let others grab the lock */
134742551e99SRandall Stewart 			atomic_add_int(&it->stcb->asoc.refcnt, 1);
134842551e99SRandall Stewart 			SCTP_TCB_UNLOCK(it->stcb);
1349c4739e2fSRandall Stewart 			SCTP_INP_INCR_REF(it->inp);
135042551e99SRandall Stewart 			SCTP_INP_RUNLOCK(it->inp);
135142551e99SRandall Stewart 			SCTP_ITERATOR_UNLOCK();
1352ec4c19fcSRandall Stewart 			SCTP_INP_INFO_RUNLOCK();
1353ec4c19fcSRandall Stewart 			SCTP_INP_INFO_RLOCK();
135442551e99SRandall Stewart 			SCTP_ITERATOR_LOCK();
1355f7517433SRandall Stewart 			if (sctp_it_ctl.iterator_flags) {
1356f7517433SRandall Stewart 				/* We won't be staying here */
1357f7517433SRandall Stewart 				SCTP_INP_DECR_REF(it->inp);
1358f7517433SRandall Stewart 				atomic_add_int(&it->stcb->asoc.refcnt, -1);
1359f7517433SRandall Stewart 				if (sctp_it_ctl.iterator_flags &
1360f7517433SRandall Stewart 				    SCTP_ITERATOR_STOP_CUR_IT) {
1361f7517433SRandall Stewart 					sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_IT;
1362f7517433SRandall Stewart 					goto done_with_iterator;
1363f7517433SRandall Stewart 				}
1364f7517433SRandall Stewart 				if (sctp_it_ctl.iterator_flags &
1365f7517433SRandall Stewart 				    SCTP_ITERATOR_STOP_CUR_INP) {
1366f7517433SRandall Stewart 					sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_INP;
1367f7517433SRandall Stewart 					goto no_stcb;
1368f7517433SRandall Stewart 				}
1369f7517433SRandall Stewart 				/* If we reach here huh? */
1370cd3fd531SMichael Tuexen 				SCTP_PRINTF("Unknown it ctl flag %x\n",
1371f7517433SRandall Stewart 				    sctp_it_ctl.iterator_flags);
1372f7517433SRandall Stewart 				sctp_it_ctl.iterator_flags = 0;
1373f7517433SRandall Stewart 			}
137442551e99SRandall Stewart 			SCTP_INP_RLOCK(it->inp);
1375c4739e2fSRandall Stewart 			SCTP_INP_DECR_REF(it->inp);
137642551e99SRandall Stewart 			SCTP_TCB_LOCK(it->stcb);
137742551e99SRandall Stewart 			atomic_add_int(&it->stcb->asoc.refcnt, -1);
137842551e99SRandall Stewart 			iteration_count = 0;
137942551e99SRandall Stewart 		}
138042551e99SRandall Stewart 		/* run function on this one */
138142551e99SRandall Stewart 		(*it->function_assoc) (it->inp, it->stcb, it->pointer, it->val);
138242551e99SRandall Stewart 
138342551e99SRandall Stewart 		/*
138442551e99SRandall Stewart 		 * we lie here, it really needs to have its own type but
138542551e99SRandall Stewart 		 * first I must verify that this won't effect things :-0
138642551e99SRandall Stewart 		 */
138742551e99SRandall Stewart 		if (it->no_chunk_output == 0)
1388ceaad40aSRandall Stewart 			sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
138942551e99SRandall Stewart 
139042551e99SRandall Stewart 		SCTP_TCB_UNLOCK(it->stcb);
139142551e99SRandall Stewart next_assoc:
139242551e99SRandall Stewart 		it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
139342551e99SRandall Stewart 		if (it->stcb == NULL) {
139442551e99SRandall Stewart 			/* Run last function */
139542551e99SRandall Stewart 			if (it->function_inp_end != NULL) {
139642551e99SRandall Stewart 				inp_skip = (*it->function_inp_end) (it->inp,
139742551e99SRandall Stewart 				    it->pointer,
139842551e99SRandall Stewart 				    it->val);
139942551e99SRandall Stewart 			}
140042551e99SRandall Stewart 		}
140142551e99SRandall Stewart 	}
140242551e99SRandall Stewart 	SCTP_INP_RUNLOCK(it->inp);
140342551e99SRandall Stewart no_stcb:
140442551e99SRandall Stewart 	/* done with all assocs on this endpoint, move on to next endpoint */
140542551e99SRandall Stewart 	it->done_current_ep = 0;
140642551e99SRandall Stewart 	if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
140742551e99SRandall Stewart 		it->inp = NULL;
140842551e99SRandall Stewart 	} else {
140942551e99SRandall Stewart 		it->inp = LIST_NEXT(it->inp, sctp_list);
141042551e99SRandall Stewart 	}
141142551e99SRandall Stewart 	if (it->inp == NULL) {
141242551e99SRandall Stewart 		goto done_with_iterator;
141342551e99SRandall Stewart 	}
141442551e99SRandall Stewart 	goto select_a_new_ep;
141542551e99SRandall Stewart }
141642551e99SRandall Stewart 
141742551e99SRandall Stewart void
141842551e99SRandall Stewart sctp_iterator_worker(void)
141942551e99SRandall Stewart {
14204a9ef3f8SMichael Tuexen 	struct sctp_iterator *it, *nit;
142142551e99SRandall Stewart 
142242551e99SRandall Stewart 	/* This function is called with the WQ lock in place */
142342551e99SRandall Stewart 
1424f7517433SRandall Stewart 	sctp_it_ctl.iterator_running = 1;
14254a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
14264a9ef3f8SMichael Tuexen 		sctp_it_ctl.cur_it = it;
142742551e99SRandall Stewart 		/* now lets work on this one */
1428f7517433SRandall Stewart 		TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
142942551e99SRandall Stewart 		SCTP_IPI_ITERATOR_WQ_UNLOCK();
1430f7517433SRandall Stewart 		CURVNET_SET(it->vn);
143142551e99SRandall Stewart 		sctp_iterator_work(it);
1432f79aab18SRandall Stewart 		sctp_it_ctl.cur_it = NULL;
1433f7517433SRandall Stewart 		CURVNET_RESTORE();
143442551e99SRandall Stewart 		SCTP_IPI_ITERATOR_WQ_LOCK();
14353c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
143642551e99SRandall Stewart 	}
1437f7517433SRandall Stewart 	sctp_it_ctl.iterator_running = 0;
143842551e99SRandall Stewart 	return;
143942551e99SRandall Stewart }
144042551e99SRandall Stewart 
1441f8829a4aSRandall Stewart 
1442f8829a4aSRandall Stewart static void
1443f8829a4aSRandall Stewart sctp_handle_addr_wq(void)
1444f8829a4aSRandall Stewart {
1445f8829a4aSRandall Stewart 	/* deal with the ADDR wq from the rtsock calls */
14464a9ef3f8SMichael Tuexen 	struct sctp_laddr *wi, *nwi;
144742551e99SRandall Stewart 	struct sctp_asconf_iterator *asc;
1448f8829a4aSRandall Stewart 
144942551e99SRandall Stewart 	SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
1450207304d4SRandall Stewart 	    sizeof(struct sctp_asconf_iterator), SCTP_M_ASC_IT);
145142551e99SRandall Stewart 	if (asc == NULL) {
145242551e99SRandall Stewart 		/* Try later, no memory */
1453f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
1454f8829a4aSRandall Stewart 		    (struct sctp_inpcb *)NULL,
1455f8829a4aSRandall Stewart 		    (struct sctp_tcb *)NULL,
1456f8829a4aSRandall Stewart 		    (struct sctp_nets *)NULL);
145742551e99SRandall Stewart 		return;
1458f8829a4aSRandall Stewart 	}
145942551e99SRandall Stewart 	LIST_INIT(&asc->list_of_work);
146042551e99SRandall Stewart 	asc->cnt = 0;
1461f7517433SRandall Stewart 
1462f7517433SRandall Stewart 	SCTP_WQ_ADDR_LOCK();
14634a9ef3f8SMichael Tuexen 	LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
146442551e99SRandall Stewart 		LIST_REMOVE(wi, sctp_nxt_addr);
146542551e99SRandall Stewart 		LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
146642551e99SRandall Stewart 		asc->cnt++;
1467f8829a4aSRandall Stewart 	}
1468f7517433SRandall Stewart 	SCTP_WQ_ADDR_UNLOCK();
1469f7517433SRandall Stewart 
147042551e99SRandall Stewart 	if (asc->cnt == 0) {
1471207304d4SRandall Stewart 		SCTP_FREE(asc, SCTP_M_ASC_IT);
147242551e99SRandall Stewart 	} else {
14731b649582SRandall Stewart 		(void)sctp_initiate_iterator(sctp_asconf_iterator_ep,
14741b649582SRandall Stewart 		    sctp_asconf_iterator_stcb,
147542551e99SRandall Stewart 		    NULL,	/* No ep end for boundall */
147642551e99SRandall Stewart 		    SCTP_PCB_FLAGS_BOUNDALL,
147742551e99SRandall Stewart 		    SCTP_PCB_ANY_FEATURES,
14781b649582SRandall Stewart 		    SCTP_ASOC_ANY_STATE,
14791b649582SRandall Stewart 		    (void *)asc, 0,
14801b649582SRandall Stewart 		    sctp_asconf_iterator_end, NULL, 0);
148142551e99SRandall Stewart 	}
1482f8829a4aSRandall Stewart }
1483f8829a4aSRandall Stewart 
1484f8829a4aSRandall Stewart void
1485f8829a4aSRandall Stewart sctp_timeout_handler(void *t)
1486f8829a4aSRandall Stewart {
1487f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
1488f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
1489f8829a4aSRandall Stewart 	struct sctp_nets *net;
1490f8829a4aSRandall Stewart 	struct sctp_timer *tmr;
1491267dbe63SMichael Tuexen 	struct mbuf *op_err;
1492ceaad40aSRandall Stewart 
1493ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1494ceaad40aSRandall Stewart 	struct socket *so;
1495ceaad40aSRandall Stewart 
1496ceaad40aSRandall Stewart #endif
1497548f47a8SMichael Tuexen 	int did_output;
1498f8829a4aSRandall Stewart 
1499f8829a4aSRandall Stewart 	tmr = (struct sctp_timer *)t;
1500f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)tmr->ep;
1501f8829a4aSRandall Stewart 	stcb = (struct sctp_tcb *)tmr->tcb;
1502f8829a4aSRandall Stewart 	net = (struct sctp_nets *)tmr->net;
15038518270eSMichael Tuexen 	CURVNET_SET((struct vnet *)tmr->vnet);
1504f8829a4aSRandall Stewart 	did_output = 1;
1505f8829a4aSRandall Stewart 
1506f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1507f8829a4aSRandall Stewart 	sctp_audit_log(0xF0, (uint8_t) tmr->type);
1508f8829a4aSRandall Stewart 	sctp_auditing(3, inp, stcb, net);
1509f8829a4aSRandall Stewart #endif
1510f8829a4aSRandall Stewart 
1511f8829a4aSRandall Stewart 	/* sanity checks... */
1512f8829a4aSRandall Stewart 	if (tmr->self != (void *)tmr) {
1513f8829a4aSRandall Stewart 		/*
1514ad81507eSRandall Stewart 		 * SCTP_PRINTF("Stale SCTP timer fired (%p), ignoring...\n",
1515dd294dceSMichael Tuexen 		 * (void *)tmr);
1516f8829a4aSRandall Stewart 		 */
15178518270eSMichael Tuexen 		CURVNET_RESTORE();
1518f8829a4aSRandall Stewart 		return;
1519f8829a4aSRandall Stewart 	}
1520a5d547adSRandall Stewart 	tmr->stopped_from = 0xa001;
1521f8829a4aSRandall Stewart 	if (!SCTP_IS_TIMER_TYPE_VALID(tmr->type)) {
1522f8829a4aSRandall Stewart 		/*
1523ad81507eSRandall Stewart 		 * SCTP_PRINTF("SCTP timer fired with invalid type: 0x%x\n",
1524f8829a4aSRandall Stewart 		 * tmr->type);
1525f8829a4aSRandall Stewart 		 */
15268518270eSMichael Tuexen 		CURVNET_RESTORE();
1527f8829a4aSRandall Stewart 		return;
1528f8829a4aSRandall Stewart 	}
1529a5d547adSRandall Stewart 	tmr->stopped_from = 0xa002;
1530f8829a4aSRandall Stewart 	if ((tmr->type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL)) {
15318518270eSMichael Tuexen 		CURVNET_RESTORE();
1532f8829a4aSRandall Stewart 		return;
1533f8829a4aSRandall Stewart 	}
1534f8829a4aSRandall Stewart 	/* if this is an iterator timeout, get the struct and clear inp */
1535a5d547adSRandall Stewart 	tmr->stopped_from = 0xa003;
1536f8829a4aSRandall Stewart 	if (inp) {
1537f8829a4aSRandall Stewart 		SCTP_INP_INCR_REF(inp);
1538aa1808b7SMichael Tuexen 		if ((inp->sctp_socket == NULL) &&
1539f8829a4aSRandall Stewart 		    ((tmr->type != SCTP_TIMER_TYPE_INPKILL) &&
1540810ec536SMichael Tuexen 		    (tmr->type != SCTP_TIMER_TYPE_INIT) &&
1541a1e13272SRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_SEND) &&
1542a1e13272SRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_RECV) &&
1543a1e13272SRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_HEARTBEAT) &&
1544f8829a4aSRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_SHUTDOWN) &&
1545f8829a4aSRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNACK) &&
1546f8829a4aSRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNGUARD) &&
1547f8829a4aSRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_ASOCKILL))
1548f8829a4aSRandall Stewart 		    ) {
1549f8829a4aSRandall Stewart 			SCTP_INP_DECR_REF(inp);
15508518270eSMichael Tuexen 			CURVNET_RESTORE();
1551f8829a4aSRandall Stewart 			return;
1552f8829a4aSRandall Stewart 		}
1553f8829a4aSRandall Stewart 	}
1554a5d547adSRandall Stewart 	tmr->stopped_from = 0xa004;
1555f8829a4aSRandall Stewart 	if (stcb) {
1556c105859eSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
1557f8829a4aSRandall Stewart 		if (stcb->asoc.state == 0) {
1558c105859eSRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, -1);
1559f8829a4aSRandall Stewart 			if (inp) {
1560f8829a4aSRandall Stewart 				SCTP_INP_DECR_REF(inp);
1561f8829a4aSRandall Stewart 			}
15628518270eSMichael Tuexen 			CURVNET_RESTORE();
1563f8829a4aSRandall Stewart 			return;
1564f8829a4aSRandall Stewart 		}
1565f8829a4aSRandall Stewart 	}
1566a5d547adSRandall Stewart 	tmr->stopped_from = 0xa005;
1567ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_TIMER1, "Timer type %d goes off\n", tmr->type);
1568139bc87fSRandall Stewart 	if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1569f8829a4aSRandall Stewart 		if (inp) {
1570f8829a4aSRandall Stewart 			SCTP_INP_DECR_REF(inp);
1571f8829a4aSRandall Stewart 		}
1572207304d4SRandall Stewart 		if (stcb) {
1573207304d4SRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, -1);
1574207304d4SRandall Stewart 		}
15758518270eSMichael Tuexen 		CURVNET_RESTORE();
1576f8829a4aSRandall Stewart 		return;
1577f8829a4aSRandall Stewart 	}
1578a5d547adSRandall Stewart 	tmr->stopped_from = 0xa006;
1579a5d547adSRandall Stewart 
1580f8829a4aSRandall Stewart 	if (stcb) {
1581f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
158250cec919SRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, -1);
1583b54d3a6cSRandall Stewart 		if ((tmr->type != SCTP_TIMER_TYPE_ASOCKILL) &&
1584b54d3a6cSRandall Stewart 		    ((stcb->asoc.state == 0) ||
1585b54d3a6cSRandall Stewart 		    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED))) {
1586b54d3a6cSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
1587b54d3a6cSRandall Stewart 			if (inp) {
1588b54d3a6cSRandall Stewart 				SCTP_INP_DECR_REF(inp);
1589b54d3a6cSRandall Stewart 			}
15908518270eSMichael Tuexen 			CURVNET_RESTORE();
1591b54d3a6cSRandall Stewart 			return;
1592b54d3a6cSRandall Stewart 		}
1593f8829a4aSRandall Stewart 	}
159444b7479bSRandall Stewart 	/* record in stopped what t-o occured */
159544b7479bSRandall Stewart 	tmr->stopped_from = tmr->type;
159644b7479bSRandall Stewart 
1597f8829a4aSRandall Stewart 	/* mark as being serviced now */
159844b7479bSRandall Stewart 	if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
159944b7479bSRandall Stewart 		/*
160044b7479bSRandall Stewart 		 * Callout has been rescheduled.
160144b7479bSRandall Stewart 		 */
160244b7479bSRandall Stewart 		goto get_out;
160344b7479bSRandall Stewart 	}
160444b7479bSRandall Stewart 	if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
160544b7479bSRandall Stewart 		/*
160644b7479bSRandall Stewart 		 * Not active, so no action.
160744b7479bSRandall Stewart 		 */
160844b7479bSRandall Stewart 		goto get_out;
160944b7479bSRandall Stewart 	}
1610139bc87fSRandall Stewart 	SCTP_OS_TIMER_DEACTIVATE(&tmr->timer);
1611f8829a4aSRandall Stewart 
1612f8829a4aSRandall Stewart 	/* call the handler for the appropriate timer type */
1613f8829a4aSRandall Stewart 	switch (tmr->type) {
1614d61a0ae0SRandall Stewart 	case SCTP_TIMER_TYPE_ZERO_COPY:
1615eacc51c5SRandall Stewart 		if (inp == NULL) {
1616eacc51c5SRandall Stewart 			break;
1617eacc51c5SRandall Stewart 		}
1618d61a0ae0SRandall Stewart 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
1619d61a0ae0SRandall Stewart 			SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
1620d61a0ae0SRandall Stewart 		}
1621d61a0ae0SRandall Stewart 		break;
1622ad21a364SRandall Stewart 	case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
1623eacc51c5SRandall Stewart 		if (inp == NULL) {
1624eacc51c5SRandall Stewart 			break;
1625eacc51c5SRandall Stewart 		}
1626ad21a364SRandall Stewart 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
1627ad21a364SRandall Stewart 			SCTP_ZERO_COPY_SENDQ_EVENT(inp, inp->sctp_socket);
1628ad21a364SRandall Stewart 		}
1629ad21a364SRandall Stewart 		break;
1630f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ADDR_WQ:
1631f8829a4aSRandall Stewart 		sctp_handle_addr_wq();
1632f8829a4aSRandall Stewart 		break;
1633f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SEND:
1634ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1635ad81507eSRandall Stewart 			break;
1636ad81507eSRandall Stewart 		}
1637f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timodata);
1638f42a358aSRandall Stewart 		stcb->asoc.timodata++;
1639f8829a4aSRandall Stewart 		stcb->asoc.num_send_timers_up--;
1640f8829a4aSRandall Stewart 		if (stcb->asoc.num_send_timers_up < 0) {
1641f8829a4aSRandall Stewart 			stcb->asoc.num_send_timers_up = 0;
1642f8829a4aSRandall Stewart 		}
1643b54d3a6cSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
164460990c0cSMichael Tuexen 		if (sctp_t3rxt_timer(inp, stcb, net)) {
1645f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1646f8829a4aSRandall Stewart 
1647f8829a4aSRandall Stewart 			goto out_decr;
1648f8829a4aSRandall Stewart 		}
1649b54d3a6cSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1650f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1651f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1652f8829a4aSRandall Stewart #endif
1653ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1654f8829a4aSRandall Stewart 		if ((stcb->asoc.num_send_timers_up == 0) &&
16554a9ef3f8SMichael Tuexen 		    (stcb->asoc.sent_queue_cnt > 0)) {
1656f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *chk;
1657f8829a4aSRandall Stewart 
1658f8829a4aSRandall Stewart 			/*
1659f8829a4aSRandall Stewart 			 * safeguard. If there on some on the sent queue
1660f8829a4aSRandall Stewart 			 * somewhere but no timers running something is
1661f8829a4aSRandall Stewart 			 * wrong... so we start a timer on the first chunk
1662f8829a4aSRandall Stewart 			 * on the send queue on whatever net it is sent to.
1663f8829a4aSRandall Stewart 			 */
1664f8829a4aSRandall Stewart 			chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
1665f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb,
1666f8829a4aSRandall Stewart 			    chk->whoTo);
1667f8829a4aSRandall Stewart 		}
1668f8829a4aSRandall Stewart 		break;
1669f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
1670ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1671ad81507eSRandall Stewart 			break;
1672ad81507eSRandall Stewart 		}
1673f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoinit);
1674f42a358aSRandall Stewart 		stcb->asoc.timoinit++;
1675f8829a4aSRandall Stewart 		if (sctp_t1init_timer(inp, stcb, net)) {
1676f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1677f8829a4aSRandall Stewart 			goto out_decr;
1678f8829a4aSRandall Stewart 		}
1679f8829a4aSRandall Stewart 		/* We do output but not here */
1680f8829a4aSRandall Stewart 		did_output = 0;
1681f8829a4aSRandall Stewart 		break;
1682f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
1683ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1684ad81507eSRandall Stewart 			break;
1685ca85e948SMichael Tuexen 		}
1686f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timosack);
1687f42a358aSRandall Stewart 		stcb->asoc.timosack++;
1688689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
1689f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1690f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1691f8829a4aSRandall Stewart #endif
1692ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SACK_TMR, SCTP_SO_NOT_LOCKED);
1693f8829a4aSRandall Stewart 		break;
1694f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
1695ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1696ad81507eSRandall Stewart 			break;
1697ad81507eSRandall Stewart 		}
1698f8829a4aSRandall Stewart 		if (sctp_shutdown_timer(inp, stcb, net)) {
1699f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1700f8829a4aSRandall Stewart 			goto out_decr;
1701f8829a4aSRandall Stewart 		}
1702f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoshutdown);
1703f42a358aSRandall Stewart 		stcb->asoc.timoshutdown++;
1704f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1705f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1706f8829a4aSRandall Stewart #endif
1707ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_NOT_LOCKED);
1708f8829a4aSRandall Stewart 		break;
1709f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
1710ca85e948SMichael Tuexen 		if ((stcb == NULL) || (inp == NULL) || (net == NULL)) {
1711ad81507eSRandall Stewart 			break;
1712ad81507eSRandall Stewart 		}
1713f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoheartbeat);
1714f42a358aSRandall Stewart 		stcb->asoc.timoheartbeat++;
1715ca85e948SMichael Tuexen 		if (sctp_heartbeat_timer(inp, stcb, net)) {
1716f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1717f8829a4aSRandall Stewart 			goto out_decr;
1718f8829a4aSRandall Stewart 		}
1719f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1720ca85e948SMichael Tuexen 		sctp_auditing(4, inp, stcb, net);
1721f8829a4aSRandall Stewart #endif
1722ca85e948SMichael Tuexen 		if (!(net->dest_state & SCTP_ADDR_NOHB)) {
1723629749b6SMichael Tuexen 			sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1724ceaad40aSRandall Stewart 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_HB_TMR, SCTP_SO_NOT_LOCKED);
1725f8829a4aSRandall Stewart 		}
1726f8829a4aSRandall Stewart 		break;
1727f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
1728ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1729ad81507eSRandall Stewart 			break;
1730ad81507eSRandall Stewart 		}
1731f8829a4aSRandall Stewart 		if (sctp_cookie_timer(inp, stcb, net)) {
1732f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1733f8829a4aSRandall Stewart 			goto out_decr;
1734f8829a4aSRandall Stewart 		}
1735f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timocookie);
1736f42a358aSRandall Stewart 		stcb->asoc.timocookie++;
1737f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1738f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1739f8829a4aSRandall Stewart #endif
1740f8829a4aSRandall Stewart 		/*
1741f8829a4aSRandall Stewart 		 * We consider T3 and Cookie timer pretty much the same with
1742f8829a4aSRandall Stewart 		 * respect to where from in chunk_output.
1743f8829a4aSRandall Stewart 		 */
1744ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1745f8829a4aSRandall Stewart 		break;
1746f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_NEWCOOKIE:
1747f8829a4aSRandall Stewart 		{
1748f8829a4aSRandall Stewart 			struct timeval tv;
1749f8829a4aSRandall Stewart 			int i, secret;
1750f8829a4aSRandall Stewart 
1751ad81507eSRandall Stewart 			if (inp == NULL) {
1752ad81507eSRandall Stewart 				break;
1753ad81507eSRandall Stewart 			}
1754f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_timosecret);
17556e55db54SRandall Stewart 			(void)SCTP_GETTIME_TIMEVAL(&tv);
1756f8829a4aSRandall Stewart 			SCTP_INP_WLOCK(inp);
1757f8829a4aSRandall Stewart 			inp->sctp_ep.time_of_secret_change = tv.tv_sec;
1758f8829a4aSRandall Stewart 			inp->sctp_ep.last_secret_number =
1759f8829a4aSRandall Stewart 			    inp->sctp_ep.current_secret_number;
1760f8829a4aSRandall Stewart 			inp->sctp_ep.current_secret_number++;
1761f8829a4aSRandall Stewart 			if (inp->sctp_ep.current_secret_number >=
1762f8829a4aSRandall Stewart 			    SCTP_HOW_MANY_SECRETS) {
1763f8829a4aSRandall Stewart 				inp->sctp_ep.current_secret_number = 0;
1764f8829a4aSRandall Stewart 			}
1765f8829a4aSRandall Stewart 			secret = (int)inp->sctp_ep.current_secret_number;
1766f8829a4aSRandall Stewart 			for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1767f8829a4aSRandall Stewart 				inp->sctp_ep.secret_key[secret][i] =
1768f8829a4aSRandall Stewart 				    sctp_select_initial_TSN(&inp->sctp_ep);
1769f8829a4aSRandall Stewart 			}
1770f8829a4aSRandall Stewart 			SCTP_INP_WUNLOCK(inp);
1771f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, stcb, net);
1772f8829a4aSRandall Stewart 		}
1773f8829a4aSRandall Stewart 		did_output = 0;
1774f8829a4aSRandall Stewart 		break;
1775f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
1776ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1777ad81507eSRandall Stewart 			break;
1778ad81507eSRandall Stewart 		}
1779f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timopathmtu);
1780f8829a4aSRandall Stewart 		sctp_pathmtu_timer(inp, stcb, net);
1781f8829a4aSRandall Stewart 		did_output = 0;
1782f8829a4aSRandall Stewart 		break;
1783f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
1784ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1785ad81507eSRandall Stewart 			break;
1786ad81507eSRandall Stewart 		}
1787f8829a4aSRandall Stewart 		if (sctp_shutdownack_timer(inp, stcb, net)) {
1788f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1789f8829a4aSRandall Stewart 			goto out_decr;
1790f8829a4aSRandall Stewart 		}
1791f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoshutdownack);
1792f42a358aSRandall Stewart 		stcb->asoc.timoshutdownack++;
1793f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1794f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1795f8829a4aSRandall Stewart #endif
1796ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_ACK_TMR, SCTP_SO_NOT_LOCKED);
1797f8829a4aSRandall Stewart 		break;
1798f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
1799ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1800ad81507eSRandall Stewart 			break;
1801ad81507eSRandall Stewart 		}
1802f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoshutdownguard);
1803267dbe63SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
1804267dbe63SMichael Tuexen 		    "Shutdown guard timer expired");
1805267dbe63SMichael Tuexen 		sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
1806f8829a4aSRandall Stewart 		/* no need to unlock on tcb its gone */
1807f8829a4aSRandall Stewart 		goto out_decr;
1808f8829a4aSRandall Stewart 
1809f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_STRRESET:
1810ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1811ad81507eSRandall Stewart 			break;
1812ad81507eSRandall Stewart 		}
1813f8829a4aSRandall Stewart 		if (sctp_strreset_timer(inp, stcb, net)) {
1814f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1815f8829a4aSRandall Stewart 			goto out_decr;
1816f8829a4aSRandall Stewart 		}
1817f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timostrmrst);
1818ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_TMR, SCTP_SO_NOT_LOCKED);
1819f8829a4aSRandall Stewart 		break;
1820f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASCONF:
1821ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1822ad81507eSRandall Stewart 			break;
1823ad81507eSRandall Stewart 		}
1824f8829a4aSRandall Stewart 		if (sctp_asconf_timer(inp, stcb, net)) {
1825f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1826f8829a4aSRandall Stewart 			goto out_decr;
1827f8829a4aSRandall Stewart 		}
1828f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoasconf);
1829f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1830f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1831f8829a4aSRandall Stewart #endif
1832ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_ASCONF_TMR, SCTP_SO_NOT_LOCKED);
1833f8829a4aSRandall Stewart 		break;
1834851b7298SRandall Stewart 	case SCTP_TIMER_TYPE_PRIM_DELETED:
1835851b7298SRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1836851b7298SRandall Stewart 			break;
1837851b7298SRandall Stewart 		}
183804ee05e8SRandall Stewart 		sctp_delete_prim_timer(inp, stcb, net);
1839851b7298SRandall Stewart 		SCTP_STAT_INCR(sctps_timodelprim);
1840851b7298SRandall Stewart 		break;
1841f8829a4aSRandall Stewart 
1842f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_AUTOCLOSE:
1843ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1844ad81507eSRandall Stewart 			break;
1845ad81507eSRandall Stewart 		}
1846f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoautoclose);
1847f8829a4aSRandall Stewart 		sctp_autoclose_timer(inp, stcb, net);
1848ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
1849f8829a4aSRandall Stewart 		did_output = 0;
1850f8829a4aSRandall Stewart 		break;
1851f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASOCKILL:
1852ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1853ad81507eSRandall Stewart 			break;
1854ad81507eSRandall Stewart 		}
1855f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoassockill);
1856f8829a4aSRandall Stewart 		/* Can we free it yet? */
1857f8829a4aSRandall Stewart 		SCTP_INP_DECR_REF(inp);
1858ba785902SMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL,
1859ba785902SMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_1);
1860ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1861ceaad40aSRandall Stewart 		so = SCTP_INP_SO(inp);
1862ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
1863ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
1864ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
1865ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
1866ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
1867ceaad40aSRandall Stewart #endif
1868ba785902SMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
1869ba785902SMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_2);
1870ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1871ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
1872ceaad40aSRandall Stewart #endif
1873f8829a4aSRandall Stewart 		/*
1874f8829a4aSRandall Stewart 		 * free asoc, always unlocks (or destroy's) so prevent
1875f8829a4aSRandall Stewart 		 * duplicate unlock or unlock of a free mtx :-0
1876f8829a4aSRandall Stewart 		 */
1877f8829a4aSRandall Stewart 		stcb = NULL;
1878f8829a4aSRandall Stewart 		goto out_no_decr;
1879f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INPKILL:
1880f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoinpkill);
1881ad81507eSRandall Stewart 		if (inp == NULL) {
1882ad81507eSRandall Stewart 			break;
1883ad81507eSRandall Stewart 		}
1884f8829a4aSRandall Stewart 		/*
1885f8829a4aSRandall Stewart 		 * special case, take away our increment since WE are the
1886f8829a4aSRandall Stewart 		 * killer
1887f8829a4aSRandall Stewart 		 */
1888f8829a4aSRandall Stewart 		SCTP_INP_DECR_REF(inp);
1889ba785902SMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL,
1890ba785902SMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_3);
1891b0552ae2SRandall Stewart 		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
18920c7dc840SRandall Stewart 		    SCTP_CALLED_FROM_INPKILL_TIMER);
1893d61374e1SRandall Stewart 		inp = NULL;
1894f8829a4aSRandall Stewart 		goto out_no_decr;
1895f8829a4aSRandall Stewart 	default:
1896ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER1, "sctp_timeout_handler:unknown timer %d\n",
1897f8829a4aSRandall Stewart 		    tmr->type);
1898f8829a4aSRandall Stewart 		break;
189960990c0cSMichael Tuexen 	}
1900f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1901f8829a4aSRandall Stewart 	sctp_audit_log(0xF1, (uint8_t) tmr->type);
1902f8829a4aSRandall Stewart 	if (inp)
1903f8829a4aSRandall Stewart 		sctp_auditing(5, inp, stcb, net);
1904f8829a4aSRandall Stewart #endif
1905f8829a4aSRandall Stewart 	if ((did_output) && stcb) {
1906f8829a4aSRandall Stewart 		/*
1907f8829a4aSRandall Stewart 		 * Now we need to clean up the control chunk chain if an
1908f8829a4aSRandall Stewart 		 * ECNE is on it. It must be marked as UNSENT again so next
1909f8829a4aSRandall Stewart 		 * call will continue to send it until such time that we get
1910f8829a4aSRandall Stewart 		 * a CWR, to remove it. It is, however, less likely that we
1911f8829a4aSRandall Stewart 		 * will find a ecn echo on the chain though.
1912f8829a4aSRandall Stewart 		 */
1913f8829a4aSRandall Stewart 		sctp_fix_ecn_echo(&stcb->asoc);
1914f8829a4aSRandall Stewart 	}
191544b7479bSRandall Stewart get_out:
1916f8829a4aSRandall Stewart 	if (stcb) {
1917f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
1918f8829a4aSRandall Stewart 	}
1919f8829a4aSRandall Stewart out_decr:
1920f8829a4aSRandall Stewart 	if (inp) {
1921f8829a4aSRandall Stewart 		SCTP_INP_DECR_REF(inp);
1922f8829a4aSRandall Stewart 	}
1923f8829a4aSRandall Stewart out_no_decr:
1924ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_TIMER1, "Timer now complete (type %d)\n",
1925548f47a8SMichael Tuexen 	    tmr->type);
19268518270eSMichael Tuexen 	CURVNET_RESTORE();
1927f8829a4aSRandall Stewart }
1928f8829a4aSRandall Stewart 
1929ad81507eSRandall Stewart void
1930f8829a4aSRandall Stewart sctp_timer_start(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1931f8829a4aSRandall Stewart     struct sctp_nets *net)
1932f8829a4aSRandall Stewart {
1933ca85e948SMichael Tuexen 	uint32_t to_ticks;
1934f8829a4aSRandall Stewart 	struct sctp_timer *tmr;
1935f8829a4aSRandall Stewart 
1936139bc87fSRandall Stewart 	if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL))
1937ad81507eSRandall Stewart 		return;
1938f8829a4aSRandall Stewart 
1939f8829a4aSRandall Stewart 	tmr = NULL;
1940f8829a4aSRandall Stewart 	if (stcb) {
1941f8829a4aSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1942f8829a4aSRandall Stewart 	}
1943f8829a4aSRandall Stewart 	switch (t_type) {
1944d61a0ae0SRandall Stewart 	case SCTP_TIMER_TYPE_ZERO_COPY:
1945d61a0ae0SRandall Stewart 		tmr = &inp->sctp_ep.zero_copy_timer;
1946d61a0ae0SRandall Stewart 		to_ticks = SCTP_ZERO_COPY_TICK_DELAY;
1947d61a0ae0SRandall Stewart 		break;
1948ad21a364SRandall Stewart 	case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
1949ad21a364SRandall Stewart 		tmr = &inp->sctp_ep.zero_copy_sendq_timer;
1950ad21a364SRandall Stewart 		to_ticks = SCTP_ZERO_COPY_SENDQ_TICK_DELAY;
1951ad21a364SRandall Stewart 		break;
1952f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ADDR_WQ:
1953f8829a4aSRandall Stewart 		/* Only 1 tick away :-) */
1954b3f1ea41SRandall Stewart 		tmr = &SCTP_BASE_INFO(addr_wq_timer);
195542551e99SRandall Stewart 		to_ticks = SCTP_ADDRESS_TICK_DELAY;
1956f8829a4aSRandall Stewart 		break;
1957f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SEND:
1958f8829a4aSRandall Stewart 		/* Here we use the RTO timer */
1959f8829a4aSRandall Stewart 		{
1960f8829a4aSRandall Stewart 			int rto_val;
1961f8829a4aSRandall Stewart 
1962f8829a4aSRandall Stewart 			if ((stcb == NULL) || (net == NULL)) {
1963ad81507eSRandall Stewart 				return;
1964f8829a4aSRandall Stewart 			}
1965f8829a4aSRandall Stewart 			tmr = &net->rxt_timer;
1966f8829a4aSRandall Stewart 			if (net->RTO == 0) {
1967f8829a4aSRandall Stewart 				rto_val = stcb->asoc.initial_rto;
1968f8829a4aSRandall Stewart 			} else {
1969f8829a4aSRandall Stewart 				rto_val = net->RTO;
1970f8829a4aSRandall Stewart 			}
1971f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(rto_val);
1972f8829a4aSRandall Stewart 		}
1973f8829a4aSRandall Stewart 		break;
1974f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
1975f8829a4aSRandall Stewart 		/*
1976f8829a4aSRandall Stewart 		 * Here we use the INIT timer default usually about 1
1977f8829a4aSRandall Stewart 		 * minute.
1978f8829a4aSRandall Stewart 		 */
1979f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
1980ad81507eSRandall Stewart 			return;
1981f8829a4aSRandall Stewart 		}
1982f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
1983f8829a4aSRandall Stewart 		if (net->RTO == 0) {
1984f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1985f8829a4aSRandall Stewart 		} else {
1986f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
1987f8829a4aSRandall Stewart 		}
1988f8829a4aSRandall Stewart 		break;
1989f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
1990f8829a4aSRandall Stewart 		/*
1991f8829a4aSRandall Stewart 		 * Here we use the Delayed-Ack timer value from the inp
1992f8829a4aSRandall Stewart 		 * ususually about 200ms.
1993f8829a4aSRandall Stewart 		 */
1994f8829a4aSRandall Stewart 		if (stcb == NULL) {
1995ad81507eSRandall Stewart 			return;
1996f8829a4aSRandall Stewart 		}
1997f8829a4aSRandall Stewart 		tmr = &stcb->asoc.dack_timer;
1998f8829a4aSRandall Stewart 		to_ticks = MSEC_TO_TICKS(stcb->asoc.delayed_ack);
1999f8829a4aSRandall Stewart 		break;
2000f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
2001f8829a4aSRandall Stewart 		/* Here we use the RTO of the destination. */
2002f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
2003ad81507eSRandall Stewart 			return;
2004f8829a4aSRandall Stewart 		}
2005f8829a4aSRandall Stewart 		if (net->RTO == 0) {
2006f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2007f8829a4aSRandall Stewart 		} else {
2008f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
2009f8829a4aSRandall Stewart 		}
2010f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2011f8829a4aSRandall Stewart 		break;
2012f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
2013f8829a4aSRandall Stewart 		/*
2014f8829a4aSRandall Stewart 		 * the net is used here so that we can add in the RTO. Even
2015f8829a4aSRandall Stewart 		 * though we use a different timer. We also add the HB timer
2016f8829a4aSRandall Stewart 		 * PLUS a random jitter.
2017f8829a4aSRandall Stewart 		 */
2018f3ba71beSMichael Tuexen 		if ((stcb == NULL) || (net == NULL)) {
2019ad81507eSRandall Stewart 			return;
2020ad81507eSRandall Stewart 		} else {
2021f8829a4aSRandall Stewart 			uint32_t rndval;
2022ca85e948SMichael Tuexen 			uint32_t jitter;
2023f8829a4aSRandall Stewart 
2024ca85e948SMichael Tuexen 			if ((net->dest_state & SCTP_ADDR_NOHB) &&
2025ca85e948SMichael Tuexen 			    !(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
2026ad81507eSRandall Stewart 				return;
2027f8829a4aSRandall Stewart 			}
2028f8829a4aSRandall Stewart 			if (net->RTO == 0) {
2029ca85e948SMichael Tuexen 				to_ticks = stcb->asoc.initial_rto;
2030f8829a4aSRandall Stewart 			} else {
2031ca85e948SMichael Tuexen 				to_ticks = net->RTO;
2032f8829a4aSRandall Stewart 			}
2033ca85e948SMichael Tuexen 			rndval = sctp_select_initial_TSN(&inp->sctp_ep);
2034ca85e948SMichael Tuexen 			jitter = rndval % to_ticks;
2035ca85e948SMichael Tuexen 			if (jitter >= (to_ticks >> 1)) {
2036ca85e948SMichael Tuexen 				to_ticks = to_ticks + (jitter - (to_ticks >> 1));
2037f8829a4aSRandall Stewart 			} else {
2038ca85e948SMichael Tuexen 				to_ticks = to_ticks - jitter;
2039f8829a4aSRandall Stewart 			}
2040ca85e948SMichael Tuexen 			if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
2041ca85e948SMichael Tuexen 			    !(net->dest_state & SCTP_ADDR_PF)) {
2042ca85e948SMichael Tuexen 				to_ticks += net->heart_beat_delay;
2043f8829a4aSRandall Stewart 			}
2044f8829a4aSRandall Stewart 			/*
2045f8829a4aSRandall Stewart 			 * Now we must convert the to_ticks that are now in
2046f8829a4aSRandall Stewart 			 * ms to ticks.
2047f8829a4aSRandall Stewart 			 */
2048f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(to_ticks);
2049ca85e948SMichael Tuexen 			tmr = &net->hb_timer;
2050f8829a4aSRandall Stewart 		}
2051f8829a4aSRandall Stewart 		break;
2052f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
2053f8829a4aSRandall Stewart 		/*
2054f8829a4aSRandall Stewart 		 * Here we can use the RTO timer from the network since one
2055f8829a4aSRandall Stewart 		 * RTT was compelete. If a retran happened then we will be
2056f8829a4aSRandall Stewart 		 * using the RTO initial value.
2057f8829a4aSRandall Stewart 		 */
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_NEWCOOKIE:
2069f8829a4aSRandall Stewart 		/*
2070f8829a4aSRandall Stewart 		 * nothing needed but the endpoint here ususually about 60
2071f8829a4aSRandall Stewart 		 * minutes.
2072f8829a4aSRandall Stewart 		 */
2073f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
2074f8829a4aSRandall Stewart 		to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_SIGNATURE];
2075f8829a4aSRandall Stewart 		break;
2076f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASOCKILL:
2077f8829a4aSRandall Stewart 		if (stcb == NULL) {
2078ad81507eSRandall Stewart 			return;
2079f8829a4aSRandall Stewart 		}
2080f8829a4aSRandall Stewart 		tmr = &stcb->asoc.strreset_timer;
2081f8829a4aSRandall Stewart 		to_ticks = MSEC_TO_TICKS(SCTP_ASOC_KILL_TIMEOUT);
2082f8829a4aSRandall Stewart 		break;
2083f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INPKILL:
2084f8829a4aSRandall Stewart 		/*
2085f8829a4aSRandall Stewart 		 * The inp is setup to die. We re-use the signature_chage
2086f8829a4aSRandall Stewart 		 * timer since that has stopped and we are in the GONE
2087f8829a4aSRandall Stewart 		 * state.
2088f8829a4aSRandall Stewart 		 */
2089f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
2090f8829a4aSRandall Stewart 		to_ticks = MSEC_TO_TICKS(SCTP_INP_KILL_TIMEOUT);
2091f8829a4aSRandall Stewart 		break;
2092f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
2093f8829a4aSRandall Stewart 		/*
2094f8829a4aSRandall Stewart 		 * Here we use the value found in the EP for PMTU ususually
2095f8829a4aSRandall Stewart 		 * about 10 minutes.
2096f8829a4aSRandall Stewart 		 */
2097f3ba71beSMichael Tuexen 		if ((stcb == NULL) || (net == NULL)) {
2098ad81507eSRandall Stewart 			return;
2099f8829a4aSRandall Stewart 		}
210080c79bbeSMichael Tuexen 		if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
210180c79bbeSMichael Tuexen 			return;
210280c79bbeSMichael Tuexen 		}
2103f8829a4aSRandall Stewart 		to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_PMTU];
2104f8829a4aSRandall Stewart 		tmr = &net->pmtu_timer;
2105f8829a4aSRandall Stewart 		break;
2106f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
2107f8829a4aSRandall Stewart 		/* Here we use the RTO of the destination */
2108f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
2109ad81507eSRandall Stewart 			return;
2110f8829a4aSRandall Stewart 		}
2111f8829a4aSRandall Stewart 		if (net->RTO == 0) {
2112f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2113f8829a4aSRandall Stewart 		} else {
2114f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
2115f8829a4aSRandall Stewart 		}
2116f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2117f8829a4aSRandall Stewart 		break;
2118f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2119f8829a4aSRandall Stewart 		/*
2120f8829a4aSRandall Stewart 		 * Here we use the endpoints shutdown guard timer usually
2121f8829a4aSRandall Stewart 		 * about 3 minutes.
2122f8829a4aSRandall Stewart 		 */
2123f3ba71beSMichael Tuexen 		if (stcb == NULL) {
2124ad81507eSRandall Stewart 			return;
2125f8829a4aSRandall Stewart 		}
2126*2e2d6794SMichael Tuexen 		if (inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] == 0) {
2127*2e2d6794SMichael Tuexen 			to_ticks = 5 * MSEC_TO_TICKS(stcb->asoc.maxrto);
2128*2e2d6794SMichael Tuexen 		} else {
2129f8829a4aSRandall Stewart 			to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN];
2130*2e2d6794SMichael Tuexen 		}
2131f8829a4aSRandall Stewart 		tmr = &stcb->asoc.shut_guard_timer;
2132f8829a4aSRandall Stewart 		break;
2133f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_STRRESET:
2134f8829a4aSRandall Stewart 		/*
21351b649582SRandall Stewart 		 * Here the timer comes from the stcb but its value is from
21361b649582SRandall Stewart 		 * the net's RTO.
2137f8829a4aSRandall Stewart 		 */
2138f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
2139ad81507eSRandall Stewart 			return;
2140f8829a4aSRandall Stewart 		}
2141f8829a4aSRandall Stewart 		if (net->RTO == 0) {
2142f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2143f8829a4aSRandall Stewart 		} else {
2144f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
2145f8829a4aSRandall Stewart 		}
2146f8829a4aSRandall Stewart 		tmr = &stcb->asoc.strreset_timer;
2147f8829a4aSRandall Stewart 		break;
2148f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASCONF:
2149f8829a4aSRandall Stewart 		/*
21501b649582SRandall Stewart 		 * Here the timer comes from the stcb but its value is from
21511b649582SRandall Stewart 		 * the net's RTO.
2152f8829a4aSRandall Stewart 		 */
2153f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
2154ad81507eSRandall Stewart 			return;
2155f8829a4aSRandall Stewart 		}
2156f8829a4aSRandall Stewart 		if (net->RTO == 0) {
2157f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2158f8829a4aSRandall Stewart 		} else {
2159f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
2160f8829a4aSRandall Stewart 		}
2161f8829a4aSRandall Stewart 		tmr = &stcb->asoc.asconf_timer;
2162f8829a4aSRandall Stewart 		break;
2163851b7298SRandall Stewart 	case SCTP_TIMER_TYPE_PRIM_DELETED:
2164851b7298SRandall Stewart 		if ((stcb == NULL) || (net != NULL)) {
2165851b7298SRandall Stewart 			return;
2166851b7298SRandall Stewart 		}
2167851b7298SRandall Stewart 		to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2168851b7298SRandall Stewart 		tmr = &stcb->asoc.delete_prim_timer;
2169851b7298SRandall Stewart 		break;
2170f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_AUTOCLOSE:
2171f8829a4aSRandall Stewart 		if (stcb == NULL) {
2172ad81507eSRandall Stewart 			return;
2173f8829a4aSRandall Stewart 		}
2174f8829a4aSRandall Stewart 		if (stcb->asoc.sctp_autoclose_ticks == 0) {
2175f8829a4aSRandall Stewart 			/*
2176f8829a4aSRandall Stewart 			 * Really an error since stcb is NOT set to
2177f8829a4aSRandall Stewart 			 * autoclose
2178f8829a4aSRandall Stewart 			 */
2179ad81507eSRandall Stewart 			return;
2180f8829a4aSRandall Stewart 		}
2181f8829a4aSRandall Stewart 		to_ticks = stcb->asoc.sctp_autoclose_ticks;
2182f8829a4aSRandall Stewart 		tmr = &stcb->asoc.autoclose_timer;
2183f8829a4aSRandall Stewart 		break;
2184f8829a4aSRandall Stewart 	default:
2185ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2186ad81507eSRandall Stewart 		    __FUNCTION__, t_type);
2187ad81507eSRandall Stewart 		return;
2188f8829a4aSRandall Stewart 		break;
218960990c0cSMichael Tuexen 	}
2190f8829a4aSRandall Stewart 	if ((to_ticks <= 0) || (tmr == NULL)) {
2191ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER1, "%s: %d:software error to_ticks:%d tmr:%p not set ??\n",
2192dd294dceSMichael Tuexen 		    __FUNCTION__, t_type, to_ticks, (void *)tmr);
2193ad81507eSRandall Stewart 		return;
2194f8829a4aSRandall Stewart 	}
2195139bc87fSRandall Stewart 	if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
2196f8829a4aSRandall Stewart 		/*
2197f8829a4aSRandall Stewart 		 * we do NOT allow you to have it already running. if it is
2198f8829a4aSRandall Stewart 		 * we leave the current one up unchanged
2199f8829a4aSRandall Stewart 		 */
2200ad81507eSRandall Stewart 		return;
2201f8829a4aSRandall Stewart 	}
2202f8829a4aSRandall Stewart 	/* At this point we can proceed */
2203f8829a4aSRandall Stewart 	if (t_type == SCTP_TIMER_TYPE_SEND) {
2204f8829a4aSRandall Stewart 		stcb->asoc.num_send_timers_up++;
2205f8829a4aSRandall Stewart 	}
2206a5d547adSRandall Stewart 	tmr->stopped_from = 0;
2207f8829a4aSRandall Stewart 	tmr->type = t_type;
2208f8829a4aSRandall Stewart 	tmr->ep = (void *)inp;
2209f8829a4aSRandall Stewart 	tmr->tcb = (void *)stcb;
2210f8829a4aSRandall Stewart 	tmr->net = (void *)net;
2211f8829a4aSRandall Stewart 	tmr->self = (void *)tmr;
22128518270eSMichael Tuexen 	tmr->vnet = (void *)curvnet;
2213c4739e2fSRandall Stewart 	tmr->ticks = sctp_get_tick_count();
2214ad81507eSRandall Stewart 	(void)SCTP_OS_TIMER_START(&tmr->timer, to_ticks, sctp_timeout_handler, tmr);
2215ad81507eSRandall Stewart 	return;
2216f8829a4aSRandall Stewart }
2217f8829a4aSRandall Stewart 
22186e55db54SRandall Stewart void
2219f8829a4aSRandall Stewart sctp_timer_stop(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2220a5d547adSRandall Stewart     struct sctp_nets *net, uint32_t from)
2221f8829a4aSRandall Stewart {
2222f8829a4aSRandall Stewart 	struct sctp_timer *tmr;
2223f8829a4aSRandall Stewart 
2224f8829a4aSRandall Stewart 	if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) &&
2225f8829a4aSRandall Stewart 	    (inp == NULL))
22266e55db54SRandall Stewart 		return;
2227f8829a4aSRandall Stewart 
2228f8829a4aSRandall Stewart 	tmr = NULL;
2229f8829a4aSRandall Stewart 	if (stcb) {
2230f8829a4aSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
2231f8829a4aSRandall Stewart 	}
2232f8829a4aSRandall Stewart 	switch (t_type) {
2233d61a0ae0SRandall Stewart 	case SCTP_TIMER_TYPE_ZERO_COPY:
2234d61a0ae0SRandall Stewart 		tmr = &inp->sctp_ep.zero_copy_timer;
2235d61a0ae0SRandall Stewart 		break;
2236ad21a364SRandall Stewart 	case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
2237ad21a364SRandall Stewart 		tmr = &inp->sctp_ep.zero_copy_sendq_timer;
2238ad21a364SRandall Stewart 		break;
2239f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ADDR_WQ:
2240b3f1ea41SRandall Stewart 		tmr = &SCTP_BASE_INFO(addr_wq_timer);
2241f8829a4aSRandall Stewart 		break;
2242f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SEND:
2243f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
22446e55db54SRandall Stewart 			return;
2245f8829a4aSRandall Stewart 		}
2246f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2247f8829a4aSRandall Stewart 		break;
2248f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
2249f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
22506e55db54SRandall Stewart 			return;
2251f8829a4aSRandall Stewart 		}
2252f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2253f8829a4aSRandall Stewart 		break;
2254f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
2255f8829a4aSRandall Stewart 		if (stcb == NULL) {
22566e55db54SRandall Stewart 			return;
2257f8829a4aSRandall Stewart 		}
2258f8829a4aSRandall Stewart 		tmr = &stcb->asoc.dack_timer;
2259f8829a4aSRandall Stewart 		break;
2260f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
2261f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
22626e55db54SRandall Stewart 			return;
2263f8829a4aSRandall Stewart 		}
2264f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2265f8829a4aSRandall Stewart 		break;
2266f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
2267ca85e948SMichael Tuexen 		if ((stcb == NULL) || (net == NULL)) {
22686e55db54SRandall Stewart 			return;
2269f8829a4aSRandall Stewart 		}
2270ca85e948SMichael Tuexen 		tmr = &net->hb_timer;
2271f8829a4aSRandall Stewart 		break;
2272f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
2273f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
22746e55db54SRandall Stewart 			return;
2275f8829a4aSRandall Stewart 		}
2276f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2277f8829a4aSRandall Stewart 		break;
2278f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_NEWCOOKIE:
2279f8829a4aSRandall Stewart 		/* nothing needed but the endpoint here */
2280f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
2281f8829a4aSRandall Stewart 		/*
2282f8829a4aSRandall Stewart 		 * We re-use the newcookie timer for the INP kill timer. We
2283f8829a4aSRandall Stewart 		 * must assure that we do not kill it by accident.
2284f8829a4aSRandall Stewart 		 */
2285f8829a4aSRandall Stewart 		break;
2286f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASOCKILL:
2287f8829a4aSRandall Stewart 		/*
2288f8829a4aSRandall Stewart 		 * Stop the asoc kill timer.
2289f8829a4aSRandall Stewart 		 */
2290f8829a4aSRandall Stewart 		if (stcb == NULL) {
22916e55db54SRandall Stewart 			return;
2292f8829a4aSRandall Stewart 		}
2293f8829a4aSRandall Stewart 		tmr = &stcb->asoc.strreset_timer;
2294f8829a4aSRandall Stewart 		break;
2295f8829a4aSRandall Stewart 
2296f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INPKILL:
2297f8829a4aSRandall Stewart 		/*
2298f8829a4aSRandall Stewart 		 * The inp is setup to die. We re-use the signature_chage
2299f8829a4aSRandall Stewart 		 * timer since that has stopped and we are in the GONE
2300f8829a4aSRandall Stewart 		 * state.
2301f8829a4aSRandall Stewart 		 */
2302f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
2303f8829a4aSRandall Stewart 		break;
2304f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
2305f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
23066e55db54SRandall Stewart 			return;
2307f8829a4aSRandall Stewart 		}
2308f8829a4aSRandall Stewart 		tmr = &net->pmtu_timer;
2309f8829a4aSRandall Stewart 		break;
2310f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
2311f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
23126e55db54SRandall Stewart 			return;
2313f8829a4aSRandall Stewart 		}
2314f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2315f8829a4aSRandall Stewart 		break;
2316f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2317f8829a4aSRandall Stewart 		if (stcb == NULL) {
23186e55db54SRandall Stewart 			return;
2319f8829a4aSRandall Stewart 		}
2320f8829a4aSRandall Stewart 		tmr = &stcb->asoc.shut_guard_timer;
2321f8829a4aSRandall Stewart 		break;
2322f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_STRRESET:
2323f8829a4aSRandall Stewart 		if (stcb == NULL) {
23246e55db54SRandall Stewart 			return;
2325f8829a4aSRandall Stewart 		}
2326f8829a4aSRandall Stewart 		tmr = &stcb->asoc.strreset_timer;
2327f8829a4aSRandall Stewart 		break;
2328f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASCONF:
2329f8829a4aSRandall Stewart 		if (stcb == NULL) {
23306e55db54SRandall Stewart 			return;
2331f8829a4aSRandall Stewart 		}
2332f8829a4aSRandall Stewart 		tmr = &stcb->asoc.asconf_timer;
2333f8829a4aSRandall Stewart 		break;
2334851b7298SRandall Stewart 	case SCTP_TIMER_TYPE_PRIM_DELETED:
2335851b7298SRandall Stewart 		if (stcb == NULL) {
2336851b7298SRandall Stewart 			return;
2337851b7298SRandall Stewart 		}
2338851b7298SRandall Stewart 		tmr = &stcb->asoc.delete_prim_timer;
2339851b7298SRandall Stewart 		break;
2340f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_AUTOCLOSE:
2341f8829a4aSRandall Stewart 		if (stcb == NULL) {
23426e55db54SRandall Stewart 			return;
2343f8829a4aSRandall Stewart 		}
2344f8829a4aSRandall Stewart 		tmr = &stcb->asoc.autoclose_timer;
2345f8829a4aSRandall Stewart 		break;
2346f8829a4aSRandall Stewart 	default:
2347ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2348ad81507eSRandall Stewart 		    __FUNCTION__, t_type);
2349f8829a4aSRandall Stewart 		break;
235060990c0cSMichael Tuexen 	}
2351f8829a4aSRandall Stewart 	if (tmr == NULL) {
23526e55db54SRandall Stewart 		return;
2353f8829a4aSRandall Stewart 	}
2354f8829a4aSRandall Stewart 	if ((tmr->type != t_type) && tmr->type) {
2355f8829a4aSRandall Stewart 		/*
2356f8829a4aSRandall Stewart 		 * Ok we have a timer that is under joint use. Cookie timer
2357f8829a4aSRandall Stewart 		 * per chance with the SEND timer. We therefore are NOT
2358f8829a4aSRandall Stewart 		 * running the timer that the caller wants stopped.  So just
2359f8829a4aSRandall Stewart 		 * return.
2360f8829a4aSRandall Stewart 		 */
23616e55db54SRandall Stewart 		return;
2362f8829a4aSRandall Stewart 	}
2363ad81507eSRandall Stewart 	if ((t_type == SCTP_TIMER_TYPE_SEND) && (stcb != NULL)) {
2364f8829a4aSRandall Stewart 		stcb->asoc.num_send_timers_up--;
2365f8829a4aSRandall Stewart 		if (stcb->asoc.num_send_timers_up < 0) {
2366f8829a4aSRandall Stewart 			stcb->asoc.num_send_timers_up = 0;
2367f8829a4aSRandall Stewart 		}
2368f8829a4aSRandall Stewart 	}
2369f8829a4aSRandall Stewart 	tmr->self = NULL;
2370a5d547adSRandall Stewart 	tmr->stopped_from = from;
23716e55db54SRandall Stewart 	(void)SCTP_OS_TIMER_STOP(&tmr->timer);
23726e55db54SRandall Stewart 	return;
2373f8829a4aSRandall Stewart }
2374f8829a4aSRandall Stewart 
2375f8829a4aSRandall Stewart uint32_t
2376f8829a4aSRandall Stewart sctp_calculate_len(struct mbuf *m)
2377f8829a4aSRandall Stewart {
2378f8829a4aSRandall Stewart 	uint32_t tlen = 0;
2379f8829a4aSRandall Stewart 	struct mbuf *at;
2380f8829a4aSRandall Stewart 
2381f8829a4aSRandall Stewart 	at = m;
2382f8829a4aSRandall Stewart 	while (at) {
2383139bc87fSRandall Stewart 		tlen += SCTP_BUF_LEN(at);
2384139bc87fSRandall Stewart 		at = SCTP_BUF_NEXT(at);
2385f8829a4aSRandall Stewart 	}
2386f8829a4aSRandall Stewart 	return (tlen);
2387f8829a4aSRandall Stewart }
2388f8829a4aSRandall Stewart 
2389f8829a4aSRandall Stewart void
2390f8829a4aSRandall Stewart sctp_mtu_size_reset(struct sctp_inpcb *inp,
239144b7479bSRandall Stewart     struct sctp_association *asoc, uint32_t mtu)
2392f8829a4aSRandall Stewart {
2393f8829a4aSRandall Stewart 	/*
2394f8829a4aSRandall Stewart 	 * Reset the P-MTU size on this association, this involves changing
2395f8829a4aSRandall Stewart 	 * the asoc MTU, going through ANY chunk+overhead larger than mtu to
2396f8829a4aSRandall Stewart 	 * allow the DF flag to be cleared.
2397f8829a4aSRandall Stewart 	 */
2398f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
2399f8829a4aSRandall Stewart 	unsigned int eff_mtu, ovh;
2400f8829a4aSRandall Stewart 
2401f8829a4aSRandall Stewart 	asoc->smallest_mtu = mtu;
2402f8829a4aSRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2403f8829a4aSRandall Stewart 		ovh = SCTP_MIN_OVERHEAD;
2404f8829a4aSRandall Stewart 	} else {
2405f8829a4aSRandall Stewart 		ovh = SCTP_MIN_V4_OVERHEAD;
2406f8829a4aSRandall Stewart 	}
2407f8829a4aSRandall Stewart 	eff_mtu = mtu - ovh;
2408f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
2409f8829a4aSRandall Stewart 		if (chk->send_size > eff_mtu) {
2410f8829a4aSRandall Stewart 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2411f8829a4aSRandall Stewart 		}
2412f8829a4aSRandall Stewart 	}
2413f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
2414f8829a4aSRandall Stewart 		if (chk->send_size > eff_mtu) {
2415f8829a4aSRandall Stewart 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2416f8829a4aSRandall Stewart 		}
2417f8829a4aSRandall Stewart 	}
2418f8829a4aSRandall Stewart }
2419f8829a4aSRandall Stewart 
2420f8829a4aSRandall Stewart 
2421f8829a4aSRandall Stewart /*
2422f8829a4aSRandall Stewart  * given an association and starting time of the current RTT period return
2423f42a358aSRandall Stewart  * RTO in number of msecs net should point to the current network
2424f8829a4aSRandall Stewart  */
2425899288aeSRandall Stewart 
2426f8829a4aSRandall Stewart uint32_t
2427f8829a4aSRandall Stewart sctp_calculate_rto(struct sctp_tcb *stcb,
2428f8829a4aSRandall Stewart     struct sctp_association *asoc,
2429f8829a4aSRandall Stewart     struct sctp_nets *net,
243018e198d3SRandall Stewart     struct timeval *told,
2431f79aab18SRandall Stewart     int safe, int rtt_from_sack)
2432f8829a4aSRandall Stewart {
243318e198d3SRandall Stewart 	/*-
2434f8829a4aSRandall Stewart 	 * given an association and the starting time of the current RTT
2435f42a358aSRandall Stewart 	 * period (in value1/value2) return RTO in number of msecs.
2436f8829a4aSRandall Stewart 	 */
2437be1d9176SMichael Tuexen 	int32_t rtt;		/* RTT in ms */
2438be1d9176SMichael Tuexen 	uint32_t new_rto;
2439f8829a4aSRandall Stewart 	int first_measure = 0;
244018e198d3SRandall Stewart 	struct timeval now, then, *old;
2441f8829a4aSRandall Stewart 
244218e198d3SRandall Stewart 	/* Copy it out for sparc64 */
244318e198d3SRandall Stewart 	if (safe == sctp_align_unsafe_makecopy) {
244418e198d3SRandall Stewart 		old = &then;
244518e198d3SRandall Stewart 		memcpy(&then, told, sizeof(struct timeval));
244618e198d3SRandall Stewart 	} else if (safe == sctp_align_safe_nocopy) {
244718e198d3SRandall Stewart 		old = told;
244818e198d3SRandall Stewart 	} else {
244918e198d3SRandall Stewart 		/* error */
245018e198d3SRandall Stewart 		SCTP_PRINTF("Huh, bad rto calc call\n");
245118e198d3SRandall Stewart 		return (0);
245218e198d3SRandall Stewart 	}
2453f8829a4aSRandall Stewart 	/************************/
2454f8829a4aSRandall Stewart 	/* 1. calculate new RTT */
2455f8829a4aSRandall Stewart 	/************************/
2456f8829a4aSRandall Stewart 	/* get the current time */
2457299108c5SRandall Stewart 	if (stcb->asoc.use_precise_time) {
2458299108c5SRandall Stewart 		(void)SCTP_GETPTIME_TIMEVAL(&now);
2459299108c5SRandall Stewart 	} else {
24606e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
2461299108c5SRandall Stewart 	}
2462be1d9176SMichael Tuexen 	timevalsub(&now, old);
2463be1d9176SMichael Tuexen 	/* store the current RTT in us */
246481eb4e63SMichael Tuexen 	net->rtt = (uint64_t) 1000000 *(uint64_t) now.tv_sec +
2465be1d9176SMichael Tuexen 	        (uint64_t) now.tv_usec;
2466be1d9176SMichael Tuexen 
2467b60b0fe6SMichael Tuexen 	/* compute rtt in ms */
2468b60b0fe6SMichael Tuexen 	rtt = (int32_t) (net->rtt / 1000);
2469f79aab18SRandall Stewart 	if ((asoc->cc_functions.sctp_rtt_calculated) && (rtt_from_sack == SCTP_RTT_FROM_DATA)) {
2470f79aab18SRandall Stewart 		/*
2471f79aab18SRandall Stewart 		 * Tell the CC module that a new update has just occurred
2472f79aab18SRandall Stewart 		 * from a sack
2473f79aab18SRandall Stewart 		 */
2474f79aab18SRandall Stewart 		(*asoc->cc_functions.sctp_rtt_calculated) (stcb, net, &now);
2475f79aab18SRandall Stewart 	}
2476f79aab18SRandall Stewart 	/*
2477f79aab18SRandall Stewart 	 * Do we need to determine the lan? We do this only on sacks i.e.
2478f79aab18SRandall Stewart 	 * RTT being determined from data not non-data (HB/INIT->INITACK).
2479f79aab18SRandall Stewart 	 */
2480f79aab18SRandall Stewart 	if ((rtt_from_sack == SCTP_RTT_FROM_DATA) &&
2481be1d9176SMichael Tuexen 	    (net->lan_type == SCTP_LAN_UNKNOWN)) {
2482be1d9176SMichael Tuexen 		if (net->rtt > SCTP_LOCAL_LAN_RTT) {
2483899288aeSRandall Stewart 			net->lan_type = SCTP_LAN_INTERNET;
2484899288aeSRandall Stewart 		} else {
2485899288aeSRandall Stewart 			net->lan_type = SCTP_LAN_LOCAL;
2486899288aeSRandall Stewart 		}
2487899288aeSRandall Stewart 	}
2488f8829a4aSRandall Stewart 	/***************************/
2489f8829a4aSRandall Stewart 	/* 2. update RTTVAR & SRTT */
2490f8829a4aSRandall Stewart 	/***************************/
2491be1d9176SMichael Tuexen 	/*-
2492be1d9176SMichael Tuexen 	 * Compute the scaled average lastsa and the
2493be1d9176SMichael Tuexen 	 * scaled variance lastsv as described in van Jacobson
2494be1d9176SMichael Tuexen 	 * Paper "Congestion Avoidance and Control", Annex A.
2495be1d9176SMichael Tuexen 	 *
2496be1d9176SMichael Tuexen 	 * (net->lastsa >> SCTP_RTT_SHIFT) is the srtt
2497be1d9176SMichael Tuexen 	 * (net->lastsa >> SCTP_RTT_VAR_SHIFT) is the rttvar
2498be1d9176SMichael Tuexen 	 */
24999a972525SRandall Stewart 	if (net->RTO_measured) {
2500be1d9176SMichael Tuexen 		rtt -= (net->lastsa >> SCTP_RTT_SHIFT);
2501be1d9176SMichael Tuexen 		net->lastsa += rtt;
2502be1d9176SMichael Tuexen 		if (rtt < 0) {
2503be1d9176SMichael Tuexen 			rtt = -rtt;
2504be1d9176SMichael Tuexen 		}
2505be1d9176SMichael Tuexen 		rtt -= (net->lastsv >> SCTP_RTT_VAR_SHIFT);
2506be1d9176SMichael Tuexen 		net->lastsv += rtt;
2507b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2508f8829a4aSRandall Stewart 			rto_logging(net, SCTP_LOG_RTTVAR);
250980fefe0aSRandall Stewart 		}
2510f8829a4aSRandall Stewart 	} else {
2511f8829a4aSRandall Stewart 		/* First RTO measurment */
25129a972525SRandall Stewart 		net->RTO_measured = 1;
2513f8829a4aSRandall Stewart 		first_measure = 1;
2514be1d9176SMichael Tuexen 		net->lastsa = rtt << SCTP_RTT_SHIFT;
2515be1d9176SMichael Tuexen 		net->lastsv = (rtt / 2) << SCTP_RTT_VAR_SHIFT;
2516b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2517f8829a4aSRandall Stewart 			rto_logging(net, SCTP_LOG_INITIAL_RTT);
251880fefe0aSRandall Stewart 		}
2519f8829a4aSRandall Stewart 	}
2520be1d9176SMichael Tuexen 	if (net->lastsv == 0) {
2521be1d9176SMichael Tuexen 		net->lastsv = SCTP_CLOCK_GRANULARITY;
2522be1d9176SMichael Tuexen 	}
2523108df27cSRandall Stewart 	new_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
2524f8829a4aSRandall Stewart 	if ((new_rto > SCTP_SAT_NETWORK_MIN) &&
2525f8829a4aSRandall Stewart 	    (stcb->asoc.sat_network_lockout == 0)) {
2526f8829a4aSRandall Stewart 		stcb->asoc.sat_network = 1;
2527f8829a4aSRandall Stewart 	} else if ((!first_measure) && stcb->asoc.sat_network) {
2528f8829a4aSRandall Stewart 		stcb->asoc.sat_network = 0;
2529f8829a4aSRandall Stewart 		stcb->asoc.sat_network_lockout = 1;
2530f8829a4aSRandall Stewart 	}
2531f8829a4aSRandall Stewart 	/* bound it, per C6/C7 in Section 5.3.1 */
2532f8829a4aSRandall Stewart 	if (new_rto < stcb->asoc.minrto) {
2533f8829a4aSRandall Stewart 		new_rto = stcb->asoc.minrto;
2534f8829a4aSRandall Stewart 	}
2535f8829a4aSRandall Stewart 	if (new_rto > stcb->asoc.maxrto) {
2536f8829a4aSRandall Stewart 		new_rto = stcb->asoc.maxrto;
2537f8829a4aSRandall Stewart 	}
25385e54f665SRandall Stewart 	/* we are now returning the RTO */
25395e54f665SRandall Stewart 	return (new_rto);
2540f8829a4aSRandall Stewart }
2541f8829a4aSRandall Stewart 
2542f8829a4aSRandall Stewart /*
2543f8829a4aSRandall Stewart  * return a pointer to a contiguous piece of data from the given mbuf chain
2544f8829a4aSRandall Stewart  * starting at 'off' for 'len' bytes.  If the desired piece spans more than
2545f8829a4aSRandall Stewart  * one mbuf, a copy is made at 'ptr'. caller must ensure that the buffer size
2546f8829a4aSRandall Stewart  * is >= 'len' returns NULL if there there isn't 'len' bytes in the chain.
2547f8829a4aSRandall Stewart  */
254872fb6fdbSRandall Stewart caddr_t
2549f8829a4aSRandall Stewart sctp_m_getptr(struct mbuf *m, int off, int len, uint8_t * in_ptr)
2550f8829a4aSRandall Stewart {
2551f8829a4aSRandall Stewart 	uint32_t count;
2552f8829a4aSRandall Stewart 	uint8_t *ptr;
2553f8829a4aSRandall Stewart 
2554f8829a4aSRandall Stewart 	ptr = in_ptr;
2555f8829a4aSRandall Stewart 	if ((off < 0) || (len <= 0))
2556f8829a4aSRandall Stewart 		return (NULL);
2557f8829a4aSRandall Stewart 
2558f8829a4aSRandall Stewart 	/* find the desired start location */
2559f8829a4aSRandall Stewart 	while ((m != NULL) && (off > 0)) {
2560139bc87fSRandall Stewart 		if (off < SCTP_BUF_LEN(m))
2561f8829a4aSRandall Stewart 			break;
2562139bc87fSRandall Stewart 		off -= SCTP_BUF_LEN(m);
2563139bc87fSRandall Stewart 		m = SCTP_BUF_NEXT(m);
2564f8829a4aSRandall Stewart 	}
2565f8829a4aSRandall Stewart 	if (m == NULL)
2566f8829a4aSRandall Stewart 		return (NULL);
2567f8829a4aSRandall Stewart 
2568f8829a4aSRandall Stewart 	/* is the current mbuf large enough (eg. contiguous)? */
2569139bc87fSRandall Stewart 	if ((SCTP_BUF_LEN(m) - off) >= len) {
2570f8829a4aSRandall Stewart 		return (mtod(m, caddr_t)+off);
2571f8829a4aSRandall Stewart 	} else {
2572f8829a4aSRandall Stewart 		/* else, it spans more than one mbuf, so save a temp copy... */
2573f8829a4aSRandall Stewart 		while ((m != NULL) && (len > 0)) {
2574139bc87fSRandall Stewart 			count = min(SCTP_BUF_LEN(m) - off, len);
2575f8829a4aSRandall Stewart 			bcopy(mtod(m, caddr_t)+off, ptr, count);
2576f8829a4aSRandall Stewart 			len -= count;
2577f8829a4aSRandall Stewart 			ptr += count;
2578f8829a4aSRandall Stewart 			off = 0;
2579139bc87fSRandall Stewart 			m = SCTP_BUF_NEXT(m);
2580f8829a4aSRandall Stewart 		}
2581f8829a4aSRandall Stewart 		if ((m == NULL) && (len > 0))
2582f8829a4aSRandall Stewart 			return (NULL);
2583f8829a4aSRandall Stewart 		else
2584f8829a4aSRandall Stewart 			return ((caddr_t)in_ptr);
2585f8829a4aSRandall Stewart 	}
2586f8829a4aSRandall Stewart }
2587f8829a4aSRandall Stewart 
2588f8829a4aSRandall Stewart 
258944b7479bSRandall Stewart 
2590f8829a4aSRandall Stewart struct sctp_paramhdr *
2591f8829a4aSRandall Stewart sctp_get_next_param(struct mbuf *m,
2592f8829a4aSRandall Stewart     int offset,
2593f8829a4aSRandall Stewart     struct sctp_paramhdr *pull,
2594f8829a4aSRandall Stewart     int pull_limit)
2595f8829a4aSRandall Stewart {
2596f8829a4aSRandall Stewart 	/* This just provides a typed signature to Peter's Pull routine */
2597f8829a4aSRandall Stewart 	return ((struct sctp_paramhdr *)sctp_m_getptr(m, offset, pull_limit,
2598f8829a4aSRandall Stewart 	    (uint8_t *) pull));
2599f8829a4aSRandall Stewart }
2600f8829a4aSRandall Stewart 
2601f8829a4aSRandall Stewart 
2602ce11b842SMichael Tuexen struct mbuf *
2603f8829a4aSRandall Stewart sctp_add_pad_tombuf(struct mbuf *m, int padlen)
2604f8829a4aSRandall Stewart {
2605ce11b842SMichael Tuexen 	struct mbuf *m_last;
2606ce11b842SMichael Tuexen 	caddr_t dp;
2607f8829a4aSRandall Stewart 
2608f8829a4aSRandall Stewart 	if (padlen > 3) {
2609ce11b842SMichael Tuexen 		return (NULL);
2610f8829a4aSRandall Stewart 	}
261141eee555SRandall Stewart 	if (padlen <= M_TRAILINGSPACE(m)) {
2612f8829a4aSRandall Stewart 		/*
2613f8829a4aSRandall Stewart 		 * The easy way. We hope the majority of the time we hit
2614f8829a4aSRandall Stewart 		 * here :)
2615f8829a4aSRandall Stewart 		 */
2616ce11b842SMichael Tuexen 		m_last = m;
2617f8829a4aSRandall Stewart 	} else {
2618ce11b842SMichael Tuexen 		/* Hard way we must grow the mbuf chain */
2619ce11b842SMichael Tuexen 		m_last = sctp_get_mbuf_for_msg(padlen, 0, M_NOWAIT, 1, MT_DATA);
2620ce11b842SMichael Tuexen 		if (m_last == NULL) {
2621ce11b842SMichael Tuexen 			return (NULL);
2622f8829a4aSRandall Stewart 		}
2623ce11b842SMichael Tuexen 		SCTP_BUF_LEN(m_last) = 0;
2624ce11b842SMichael Tuexen 		SCTP_BUF_NEXT(m_last) = NULL;
2625ce11b842SMichael Tuexen 		SCTP_BUF_NEXT(m) = m_last;
2626f8829a4aSRandall Stewart 	}
2627ce11b842SMichael Tuexen 	dp = mtod(m_last, caddr_t)+SCTP_BUF_LEN(m_last);
2628ce11b842SMichael Tuexen 	SCTP_BUF_LEN(m_last) += padlen;
2629ce11b842SMichael Tuexen 	memset(dp, 0, padlen);
2630ce11b842SMichael Tuexen 	return (m_last);
2631f8829a4aSRandall Stewart }
2632f8829a4aSRandall Stewart 
2633ce11b842SMichael Tuexen struct mbuf *
2634f8829a4aSRandall Stewart sctp_pad_lastmbuf(struct mbuf *m, int padval, struct mbuf *last_mbuf)
2635f8829a4aSRandall Stewart {
2636f8829a4aSRandall Stewart 	/* find the last mbuf in chain and pad it */
2637f8829a4aSRandall Stewart 	struct mbuf *m_at;
2638f8829a4aSRandall Stewart 
2639ce11b842SMichael Tuexen 	if (last_mbuf != NULL) {
2640f8829a4aSRandall Stewart 		return (sctp_add_pad_tombuf(last_mbuf, padval));
2641f8829a4aSRandall Stewart 	} else {
264217267b32SMichael Tuexen 		for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
2643139bc87fSRandall Stewart 			if (SCTP_BUF_NEXT(m_at) == NULL) {
2644f8829a4aSRandall Stewart 				return (sctp_add_pad_tombuf(m_at, padval));
2645f8829a4aSRandall Stewart 			}
2646f8829a4aSRandall Stewart 		}
2647f8829a4aSRandall Stewart 	}
2648ce11b842SMichael Tuexen 	return (NULL);
2649f8829a4aSRandall Stewart }
2650f8829a4aSRandall Stewart 
2651f8829a4aSRandall Stewart static void
2652c5b5675dSMichael Tuexen sctp_notify_assoc_change(uint16_t state, struct sctp_tcb *stcb,
2653410a3b1eSMichael Tuexen     uint16_t error, struct sctp_abort_chunk *abort, uint8_t from_peer, int so_locked
2654ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2655ceaad40aSRandall Stewart     SCTP_UNUSED
2656ceaad40aSRandall Stewart #endif
2657ceaad40aSRandall Stewart )
2658f8829a4aSRandall Stewart {
2659f8829a4aSRandall Stewart 	struct mbuf *m_notify;
2660f8829a4aSRandall Stewart 	struct sctp_assoc_change *sac;
2661f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
2662a2b42326SMichael Tuexen 	size_t notif_len, abort_len;
2663e06b67c7SMichael Tuexen 	unsigned int i;
2664f8829a4aSRandall Stewart 
2665ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2666ceaad40aSRandall Stewart 	struct socket *so;
2667ceaad40aSRandall Stewart 
2668ceaad40aSRandall Stewart #endif
2669ceaad40aSRandall Stewart 
267058411b08SMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT)) {
2671a2b42326SMichael Tuexen 		notif_len = sizeof(struct sctp_assoc_change);
2672a2b42326SMichael Tuexen 		if (abort != NULL) {
2673c9eb4473SMichael Tuexen 			abort_len = ntohs(abort->ch.chunk_length);
2674a2b42326SMichael Tuexen 		} else {
2675a2b42326SMichael Tuexen 			abort_len = 0;
2676c5b5675dSMichael Tuexen 		}
2677a2b42326SMichael Tuexen 		if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
2678a2b42326SMichael Tuexen 			notif_len += SCTP_ASSOC_SUPPORTS_MAX;
2679a2b42326SMichael Tuexen 		} else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
2680a2b42326SMichael Tuexen 			notif_len += abort_len;
2681a2b42326SMichael Tuexen 		}
2682eb1b1807SGleb Smirnoff 		m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
2683a2b42326SMichael Tuexen 		if (m_notify == NULL) {
2684a2b42326SMichael Tuexen 			/* Retry with smaller value. */
2685a2b42326SMichael Tuexen 			notif_len = sizeof(struct sctp_assoc_change);
2686eb1b1807SGleb Smirnoff 			m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
2687a2b42326SMichael Tuexen 			if (m_notify == NULL) {
268858411b08SMichael Tuexen 				goto set_error;
2689a2b42326SMichael Tuexen 			}
2690a2b42326SMichael Tuexen 		}
2691a2b42326SMichael Tuexen 		SCTP_BUF_NEXT(m_notify) = NULL;
2692f8829a4aSRandall Stewart 		sac = mtod(m_notify, struct sctp_assoc_change *);
2693e432298aSXin LI 		memset(sac, 0, notif_len);
2694f8829a4aSRandall Stewart 		sac->sac_type = SCTP_ASSOC_CHANGE;
2695f8829a4aSRandall Stewart 		sac->sac_flags = 0;
2696f8829a4aSRandall Stewart 		sac->sac_length = sizeof(struct sctp_assoc_change);
2697c5b5675dSMichael Tuexen 		sac->sac_state = state;
2698f8829a4aSRandall Stewart 		sac->sac_error = error;
2699f8829a4aSRandall Stewart 		/* XXX verify these stream counts */
2700f8829a4aSRandall Stewart 		sac->sac_outbound_streams = stcb->asoc.streamoutcnt;
2701f8829a4aSRandall Stewart 		sac->sac_inbound_streams = stcb->asoc.streamincnt;
2702f8829a4aSRandall Stewart 		sac->sac_assoc_id = sctp_get_associd(stcb);
2703a2b42326SMichael Tuexen 		if (notif_len > sizeof(struct sctp_assoc_change)) {
2704c5b5675dSMichael Tuexen 			if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
2705e06b67c7SMichael Tuexen 				i = 0;
2706c79bec9cSMichael Tuexen 				if (stcb->asoc.prsctp_supported == 1) {
2707e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_PR;
2708e06b67c7SMichael Tuexen 				}
2709c79bec9cSMichael Tuexen 				if (stcb->asoc.auth_supported == 1) {
2710e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_AUTH;
2711e06b67c7SMichael Tuexen 				}
2712c79bec9cSMichael Tuexen 				if (stcb->asoc.asconf_supported == 1) {
2713e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_ASCONF;
2714e06b67c7SMichael Tuexen 				}
2715e06b67c7SMichael Tuexen 				sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_MULTIBUF;
2716c79bec9cSMichael Tuexen 				if (stcb->asoc.reconfig_supported == 1) {
2717e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_RE_CONFIG;
2718e06b67c7SMichael Tuexen 				}
2719e06b67c7SMichael Tuexen 				sac->sac_length += i;
2720a2b42326SMichael Tuexen 			} else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
2721a2b42326SMichael Tuexen 				memcpy(sac->sac_info, abort, abort_len);
2722a2b42326SMichael Tuexen 				sac->sac_length += abort_len;
2723a2b42326SMichael Tuexen 			}
2724c5b5675dSMichael Tuexen 		}
2725e06b67c7SMichael Tuexen 		SCTP_BUF_LEN(m_notify) = sac->sac_length;
2726f8829a4aSRandall Stewart 		control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
27277215cc1bSMichael Tuexen 		    0, 0, stcb->asoc.context, 0, 0, 0,
2728f8829a4aSRandall Stewart 		    m_notify);
272958411b08SMichael Tuexen 		if (control != NULL) {
2730139bc87fSRandall Stewart 			control->length = SCTP_BUF_LEN(m_notify);
2731f8829a4aSRandall Stewart 			/* not that we need this */
2732f8829a4aSRandall Stewart 			control->tail_mbuf = m_notify;
2733139bc87fSRandall Stewart 			control->spec_flags = M_NOTIFICATION;
2734f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep, stcb,
2735f8829a4aSRandall Stewart 			    control,
2736cfde3ff7SRandall Stewart 			    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD,
2737cfde3ff7SRandall Stewart 			    so_locked);
273858411b08SMichael Tuexen 		} else {
273958411b08SMichael Tuexen 			sctp_m_freem(m_notify);
274058411b08SMichael Tuexen 		}
274158411b08SMichael Tuexen 	}
274258411b08SMichael Tuexen 	/*
274358411b08SMichael Tuexen 	 * For 1-to-1 style sockets, we send up and error when an ABORT
274458411b08SMichael Tuexen 	 * comes in.
274558411b08SMichael Tuexen 	 */
274658411b08SMichael Tuexen set_error:
274758411b08SMichael Tuexen 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
274858411b08SMichael Tuexen 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
274958411b08SMichael Tuexen 	    ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
2750e045904fSMichael Tuexen 		SOCK_LOCK(stcb->sctp_socket);
2751410a3b1eSMichael Tuexen 		if (from_peer) {
275258411b08SMichael Tuexen 			if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
275358411b08SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNREFUSED);
275458411b08SMichael Tuexen 				stcb->sctp_socket->so_error = ECONNREFUSED;
275558411b08SMichael Tuexen 			} else {
275658411b08SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
275758411b08SMichael Tuexen 				stcb->sctp_socket->so_error = ECONNRESET;
275858411b08SMichael Tuexen 			}
2759410a3b1eSMichael Tuexen 		} else {
2760553bb068SMichael Tuexen 			if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) ||
2761553bb068SMichael Tuexen 			    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
2762553bb068SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ETIMEDOUT);
2763553bb068SMichael Tuexen 				stcb->sctp_socket->so_error = ETIMEDOUT;
2764553bb068SMichael Tuexen 			} else {
2765410a3b1eSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNABORTED);
2766410a3b1eSMichael Tuexen 				stcb->sctp_socket->so_error = ECONNABORTED;
2767410a3b1eSMichael Tuexen 			}
276858411b08SMichael Tuexen 		}
2769553bb068SMichael Tuexen 	}
277058411b08SMichael Tuexen 	/* Wake ANY sleepers */
2771ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2772ceaad40aSRandall Stewart 	so = SCTP_INP_SO(stcb->sctp_ep);
2773ceaad40aSRandall Stewart 	if (!so_locked) {
2774ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
2775ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
2776ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
2777ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
2778ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
2779ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2780ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
2781ceaad40aSRandall Stewart 			return;
2782ceaad40aSRandall Stewart 		}
2783ceaad40aSRandall Stewart 	}
2784ceaad40aSRandall Stewart #endif
278558411b08SMichael Tuexen 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
278658411b08SMichael Tuexen 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
278758411b08SMichael Tuexen 	    ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
2788e045904fSMichael Tuexen 		socantrcvmore_locked(stcb->sctp_socket);
278958411b08SMichael Tuexen 	}
279058411b08SMichael Tuexen 	sorwakeup(stcb->sctp_socket);
279158411b08SMichael Tuexen 	sowwakeup(stcb->sctp_socket);
2792ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2793ceaad40aSRandall Stewart 	if (!so_locked) {
2794ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
2795ceaad40aSRandall Stewart 	}
2796ceaad40aSRandall Stewart #endif
2797f8829a4aSRandall Stewart }
2798f8829a4aSRandall Stewart 
2799f8829a4aSRandall Stewart static void
2800f8829a4aSRandall Stewart sctp_notify_peer_addr_change(struct sctp_tcb *stcb, uint32_t state,
28013cb3567dSMichael Tuexen     struct sockaddr *sa, uint32_t error, int so_locked
28023cb3567dSMichael Tuexen #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
28033cb3567dSMichael Tuexen     SCTP_UNUSED
28043cb3567dSMichael Tuexen #endif
28053cb3567dSMichael Tuexen )
2806f8829a4aSRandall Stewart {
2807f8829a4aSRandall Stewart 	struct mbuf *m_notify;
2808f8829a4aSRandall Stewart 	struct sctp_paddr_change *spc;
2809f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
2810f8829a4aSRandall Stewart 
281160990c0cSMichael Tuexen 	if ((stcb == NULL) ||
281260990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT)) {
2813f8829a4aSRandall Stewart 		/* event not enabled */
2814f8829a4aSRandall Stewart 		return;
2815830d754dSRandall Stewart 	}
2816eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_paddr_change), 0, M_NOWAIT, 1, MT_DATA);
2817f8829a4aSRandall Stewart 	if (m_notify == NULL)
2818f8829a4aSRandall Stewart 		return;
2819139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
2820f8829a4aSRandall Stewart 	spc = mtod(m_notify, struct sctp_paddr_change *);
282156711f94SMichael Tuexen 	memset(spc, 0, sizeof(struct sctp_paddr_change));
2822f8829a4aSRandall Stewart 	spc->spc_type = SCTP_PEER_ADDR_CHANGE;
2823f8829a4aSRandall Stewart 	spc->spc_flags = 0;
2824f8829a4aSRandall Stewart 	spc->spc_length = sizeof(struct sctp_paddr_change);
28255e2c2d87SRandall Stewart 	switch (sa->sa_family) {
2826ea5eba11SMichael Tuexen #ifdef INET
28275e2c2d87SRandall Stewart 	case AF_INET:
2828d59107f7SMichael Tuexen #ifdef INET6
2829d59107f7SMichael Tuexen 		if (sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2830d59107f7SMichael Tuexen 			in6_sin_2_v4mapsin6((struct sockaddr_in *)sa,
2831d59107f7SMichael Tuexen 			    (struct sockaddr_in6 *)&spc->spc_aaddr);
2832d59107f7SMichael Tuexen 		} else {
2833f8829a4aSRandall Stewart 			memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
2834d59107f7SMichael Tuexen 		}
2835d59107f7SMichael Tuexen #else
2836d59107f7SMichael Tuexen 		memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
2837d59107f7SMichael Tuexen #endif
28385e2c2d87SRandall Stewart 		break;
2839ea5eba11SMichael Tuexen #endif
28405e2c2d87SRandall Stewart #ifdef INET6
28415e2c2d87SRandall Stewart 	case AF_INET6:
28425e2c2d87SRandall Stewart 		{
2843f42a358aSRandall Stewart 			struct sockaddr_in6 *sin6;
2844f42a358aSRandall Stewart 
2845f8829a4aSRandall Stewart 			memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in6));
2846f42a358aSRandall Stewart 
2847f42a358aSRandall Stewart 			sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
2848f42a358aSRandall Stewart 			if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
284942551e99SRandall Stewart 				if (sin6->sin6_scope_id == 0) {
285042551e99SRandall Stewart 					/* recover scope_id for user */
2851f42a358aSRandall Stewart 					(void)sa6_recoverscope(sin6);
285242551e99SRandall Stewart 				} else {
285342551e99SRandall Stewart 					/* clear embedded scope_id for user */
285442551e99SRandall Stewart 					in6_clearscope(&sin6->sin6_addr);
285542551e99SRandall Stewart 				}
2856f42a358aSRandall Stewart 			}
28575e2c2d87SRandall Stewart 			break;
28585e2c2d87SRandall Stewart 		}
28595e2c2d87SRandall Stewart #endif
28605e2c2d87SRandall Stewart 	default:
28615e2c2d87SRandall Stewart 		/* TSNH */
28625e2c2d87SRandall Stewart 		break;
2863f8829a4aSRandall Stewart 	}
2864f8829a4aSRandall Stewart 	spc->spc_state = state;
2865f8829a4aSRandall Stewart 	spc->spc_error = error;
2866f8829a4aSRandall Stewart 	spc->spc_assoc_id = sctp_get_associd(stcb);
2867f8829a4aSRandall Stewart 
2868139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_paddr_change);
2869139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
2870f8829a4aSRandall Stewart 
2871f8829a4aSRandall Stewart 	/* append to socket */
2872f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
28737215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
2874f8829a4aSRandall Stewart 	    m_notify);
2875f8829a4aSRandall Stewart 	if (control == NULL) {
2876f8829a4aSRandall Stewart 		/* no memory */
2877f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
2878f8829a4aSRandall Stewart 		return;
2879f8829a4aSRandall Stewart 	}
2880139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
2881139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
2882f8829a4aSRandall Stewart 	/* not that we need this */
2883f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
2884f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
2885f8829a4aSRandall Stewart 	    control,
2886cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1,
2887cfde3ff7SRandall Stewart 	    SCTP_READ_LOCK_NOT_HELD,
28883cb3567dSMichael Tuexen 	    so_locked);
2889f8829a4aSRandall Stewart }
2890f8829a4aSRandall Stewart 
2891f8829a4aSRandall Stewart 
2892f8829a4aSRandall Stewart static void
28931edc9dbaSMichael Tuexen sctp_notify_send_failed(struct sctp_tcb *stcb, uint8_t sent, uint32_t error,
2894ceaad40aSRandall Stewart     struct sctp_tmit_chunk *chk, int so_locked
2895ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2896ceaad40aSRandall Stewart     SCTP_UNUSED
2897ceaad40aSRandall Stewart #endif
2898ceaad40aSRandall Stewart )
2899f8829a4aSRandall Stewart {
2900830d754dSRandall Stewart 	struct mbuf *m_notify;
2901f8829a4aSRandall Stewart 	struct sctp_send_failed *ssf;
29029935403aSMichael Tuexen 	struct sctp_send_failed_event *ssfe;
2903f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
2904f8829a4aSRandall Stewart 	int length;
2905f8829a4aSRandall Stewart 
290660990c0cSMichael Tuexen 	if ((stcb == NULL) ||
29079935403aSMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
29089935403aSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
2909f8829a4aSRandall Stewart 		/* event not enabled */
2910f8829a4aSRandall Stewart 		return;
2911830d754dSRandall Stewart 	}
29129935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
29139935403aSMichael Tuexen 		length = sizeof(struct sctp_send_failed_event);
29149935403aSMichael Tuexen 	} else {
29159935403aSMichael Tuexen 		length = sizeof(struct sctp_send_failed);
29169935403aSMichael Tuexen 	}
2917eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(length, 0, M_NOWAIT, 1, MT_DATA);
2918f8829a4aSRandall Stewart 	if (m_notify == NULL)
2919f8829a4aSRandall Stewart 		/* no space left */
2920f8829a4aSRandall Stewart 		return;
2921139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
29229935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
29239935403aSMichael Tuexen 		ssfe = mtod(m_notify, struct sctp_send_failed_event *);
2924e432298aSXin LI 		memset(ssfe, 0, length);
29259935403aSMichael Tuexen 		ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
29261edc9dbaSMichael Tuexen 		if (sent) {
29279935403aSMichael Tuexen 			ssfe->ssfe_flags = SCTP_DATA_SENT;
29281edc9dbaSMichael Tuexen 		} else {
29291edc9dbaSMichael Tuexen 			ssfe->ssfe_flags = SCTP_DATA_UNSENT;
29301edc9dbaSMichael Tuexen 		}
2931e432298aSXin LI 		length += chk->send_size;
2932e432298aSXin LI 		length -= sizeof(struct sctp_data_chunk);
29339935403aSMichael Tuexen 		ssfe->ssfe_length = length;
29349935403aSMichael Tuexen 		ssfe->ssfe_error = error;
29359935403aSMichael Tuexen 		/* not exactly what the user sent in, but should be close :) */
29369935403aSMichael Tuexen 		ssfe->ssfe_info.snd_sid = chk->rec.data.stream_number;
29379935403aSMichael Tuexen 		ssfe->ssfe_info.snd_flags = chk->rec.data.rcv_flags;
29389935403aSMichael Tuexen 		ssfe->ssfe_info.snd_ppid = chk->rec.data.payloadtype;
29399935403aSMichael Tuexen 		ssfe->ssfe_info.snd_context = chk->rec.data.context;
29409935403aSMichael Tuexen 		ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
29419935403aSMichael Tuexen 		ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
29429935403aSMichael Tuexen 		SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed_event);
29439935403aSMichael Tuexen 	} else {
2944f8829a4aSRandall Stewart 		ssf = mtod(m_notify, struct sctp_send_failed *);
2945e432298aSXin LI 		memset(ssf, 0, length);
2946f8829a4aSRandall Stewart 		ssf->ssf_type = SCTP_SEND_FAILED;
29471edc9dbaSMichael Tuexen 		if (sent) {
2948f8829a4aSRandall Stewart 			ssf->ssf_flags = SCTP_DATA_SENT;
29491edc9dbaSMichael Tuexen 		} else {
29501edc9dbaSMichael Tuexen 			ssf->ssf_flags = SCTP_DATA_UNSENT;
29511edc9dbaSMichael Tuexen 		}
2952e432298aSXin LI 		length += chk->send_size;
2953e432298aSXin LI 		length -= sizeof(struct sctp_data_chunk);
2954f8829a4aSRandall Stewart 		ssf->ssf_length = length;
2955f8829a4aSRandall Stewart 		ssf->ssf_error = error;
2956f8829a4aSRandall Stewart 		/* not exactly what the user sent in, but should be close :) */
2957d00aff5dSRandall Stewart 		bzero(&ssf->ssf_info, sizeof(ssf->ssf_info));
2958f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_stream = chk->rec.data.stream_number;
2959f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_ssn = chk->rec.data.stream_seq;
2960f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_flags = chk->rec.data.rcv_flags;
2961f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_ppid = chk->rec.data.payloadtype;
2962f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_context = chk->rec.data.context;
2963f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
2964f8829a4aSRandall Stewart 		ssf->ssf_assoc_id = sctp_get_associd(stcb);
29659935403aSMichael Tuexen 		SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed);
29669935403aSMichael Tuexen 	}
2967830d754dSRandall Stewart 	if (chk->data) {
2968830d754dSRandall Stewart 		/*
2969830d754dSRandall Stewart 		 * trim off the sctp chunk header(it should be there)
2970830d754dSRandall Stewart 		 */
2971830d754dSRandall Stewart 		if (chk->send_size >= sizeof(struct sctp_data_chunk)) {
2972830d754dSRandall Stewart 			m_adj(chk->data, sizeof(struct sctp_data_chunk));
2973830d754dSRandall Stewart 			sctp_mbuf_crush(chk->data);
2974830d754dSRandall Stewart 			chk->send_size -= sizeof(struct sctp_data_chunk);
2975830d754dSRandall Stewart 		}
2976830d754dSRandall Stewart 	}
2977810ec536SMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = chk->data;
2978f8829a4aSRandall Stewart 	/* Steal off the mbuf */
2979f8829a4aSRandall Stewart 	chk->data = NULL;
2980f8829a4aSRandall Stewart 	/*
2981f8829a4aSRandall Stewart 	 * For this case, we check the actual socket buffer, since the assoc
2982f8829a4aSRandall Stewart 	 * is going away we don't want to overfill the socket buffer for a
2983f8829a4aSRandall Stewart 	 * non-reader
2984f8829a4aSRandall Stewart 	 */
2985139bc87fSRandall Stewart 	if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
2986f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
2987f8829a4aSRandall Stewart 		return;
2988f8829a4aSRandall Stewart 	}
2989f8829a4aSRandall Stewart 	/* append to socket */
2990f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
29917215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
2992f8829a4aSRandall Stewart 	    m_notify);
2993f8829a4aSRandall Stewart 	if (control == NULL) {
2994f8829a4aSRandall Stewart 		/* no memory */
2995f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
2996f8829a4aSRandall Stewart 		return;
2997f8829a4aSRandall Stewart 	}
2998139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
2999f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3000f8829a4aSRandall Stewart 	    control,
3001cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1,
3002cfde3ff7SRandall Stewart 	    SCTP_READ_LOCK_NOT_HELD,
3003cfde3ff7SRandall Stewart 	    so_locked);
3004f8829a4aSRandall Stewart }
3005f8829a4aSRandall Stewart 
3006f8829a4aSRandall Stewart 
3007f8829a4aSRandall Stewart static void
3008f8829a4aSRandall Stewart sctp_notify_send_failed2(struct sctp_tcb *stcb, uint32_t error,
3009ceaad40aSRandall Stewart     struct sctp_stream_queue_pending *sp, int so_locked
3010ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3011ceaad40aSRandall Stewart     SCTP_UNUSED
3012ceaad40aSRandall Stewart #endif
3013ceaad40aSRandall Stewart )
3014f8829a4aSRandall Stewart {
3015f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3016f8829a4aSRandall Stewart 	struct sctp_send_failed *ssf;
30179935403aSMichael Tuexen 	struct sctp_send_failed_event *ssfe;
3018f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3019f8829a4aSRandall Stewart 	int length;
3020f8829a4aSRandall Stewart 
302160990c0cSMichael Tuexen 	if ((stcb == NULL) ||
30229935403aSMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
30239935403aSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
3024f8829a4aSRandall Stewart 		/* event not enabled */
3025f8829a4aSRandall Stewart 		return;
3026830d754dSRandall Stewart 	}
30279935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
30289935403aSMichael Tuexen 		length = sizeof(struct sctp_send_failed_event);
30299935403aSMichael Tuexen 	} else {
30309935403aSMichael Tuexen 		length = sizeof(struct sctp_send_failed);
30319935403aSMichael Tuexen 	}
3032eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(length, 0, M_NOWAIT, 1, MT_DATA);
30339935403aSMichael Tuexen 	if (m_notify == NULL) {
3034f8829a4aSRandall Stewart 		/* no space left */
3035f8829a4aSRandall Stewart 		return;
30369935403aSMichael Tuexen 	}
3037139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
30389935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
30399935403aSMichael Tuexen 		ssfe = mtod(m_notify, struct sctp_send_failed_event *);
3040e432298aSXin LI 		memset(ssfe, 0, length);
3041ad83c8a5SMichael Tuexen 		ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
30429935403aSMichael Tuexen 		ssfe->ssfe_flags = SCTP_DATA_UNSENT;
3043e432298aSXin LI 		length += sp->length;
30449935403aSMichael Tuexen 		ssfe->ssfe_length = length;
30459935403aSMichael Tuexen 		ssfe->ssfe_error = error;
30469935403aSMichael Tuexen 		/* not exactly what the user sent in, but should be close :) */
30479935403aSMichael Tuexen 		ssfe->ssfe_info.snd_sid = sp->stream;
30489935403aSMichael Tuexen 		if (sp->some_taken) {
30499935403aSMichael Tuexen 			ssfe->ssfe_info.snd_flags = SCTP_DATA_LAST_FRAG;
30509935403aSMichael Tuexen 		} else {
30519935403aSMichael Tuexen 			ssfe->ssfe_info.snd_flags = SCTP_DATA_NOT_FRAG;
30529935403aSMichael Tuexen 		}
30539935403aSMichael Tuexen 		ssfe->ssfe_info.snd_ppid = sp->ppid;
30549935403aSMichael Tuexen 		ssfe->ssfe_info.snd_context = sp->context;
30559935403aSMichael Tuexen 		ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
30569935403aSMichael Tuexen 		ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
30579935403aSMichael Tuexen 		SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed_event);
30589935403aSMichael Tuexen 	} else {
3059f8829a4aSRandall Stewart 		ssf = mtod(m_notify, struct sctp_send_failed *);
3060e432298aSXin LI 		memset(ssf, 0, length);
3061f8829a4aSRandall Stewart 		ssf->ssf_type = SCTP_SEND_FAILED;
3062f8829a4aSRandall Stewart 		ssf->ssf_flags = SCTP_DATA_UNSENT;
3063e432298aSXin LI 		length += sp->length;
3064f8829a4aSRandall Stewart 		ssf->ssf_length = length;
3065f8829a4aSRandall Stewart 		ssf->ssf_error = error;
3066f8829a4aSRandall Stewart 		/* not exactly what the user sent in, but should be close :) */
3067f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_stream = sp->stream;
3068f3b05218SMichael Tuexen 		ssf->ssf_info.sinfo_ssn = 0;
3069fc14de76SRandall Stewart 		if (sp->some_taken) {
3070fc14de76SRandall Stewart 			ssf->ssf_info.sinfo_flags = SCTP_DATA_LAST_FRAG;
3071fc14de76SRandall Stewart 		} else {
3072fc14de76SRandall Stewart 			ssf->ssf_info.sinfo_flags = SCTP_DATA_NOT_FRAG;
3073fc14de76SRandall Stewart 		}
3074f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_ppid = sp->ppid;
3075f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_context = sp->context;
3076f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3077f8829a4aSRandall Stewart 		ssf->ssf_assoc_id = sctp_get_associd(stcb);
3078139bc87fSRandall Stewart 		SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed);
30799935403aSMichael Tuexen 	}
30809935403aSMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = sp->data;
3081f8829a4aSRandall Stewart 
3082f8829a4aSRandall Stewart 	/* Steal off the mbuf */
3083f8829a4aSRandall Stewart 	sp->data = NULL;
3084f8829a4aSRandall Stewart 	/*
3085f8829a4aSRandall Stewart 	 * For this case, we check the actual socket buffer, since the assoc
3086f8829a4aSRandall Stewart 	 * is going away we don't want to overfill the socket buffer for a
3087f8829a4aSRandall Stewart 	 * non-reader
3088f8829a4aSRandall Stewart 	 */
3089139bc87fSRandall Stewart 	if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3090f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3091f8829a4aSRandall Stewart 		return;
3092f8829a4aSRandall Stewart 	}
3093f8829a4aSRandall Stewart 	/* append to socket */
3094f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
30957215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3096f8829a4aSRandall Stewart 	    m_notify);
3097f8829a4aSRandall Stewart 	if (control == NULL) {
3098f8829a4aSRandall Stewart 		/* no memory */
3099f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3100f8829a4aSRandall Stewart 		return;
3101f8829a4aSRandall Stewart 	}
3102139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3103f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3104f8829a4aSRandall Stewart 	    control,
3105cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3106f8829a4aSRandall Stewart }
3107f8829a4aSRandall Stewart 
3108f8829a4aSRandall Stewart 
3109f8829a4aSRandall Stewart 
3110f8829a4aSRandall Stewart static void
31117215cc1bSMichael Tuexen sctp_notify_adaptation_layer(struct sctp_tcb *stcb)
3112f8829a4aSRandall Stewart {
3113f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3114f8829a4aSRandall Stewart 	struct sctp_adaptation_event *sai;
3115f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3116f8829a4aSRandall Stewart 
311760990c0cSMichael Tuexen 	if ((stcb == NULL) ||
311860990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT)) {
3119f8829a4aSRandall Stewart 		/* event not enabled */
3120f8829a4aSRandall Stewart 		return;
3121830d754dSRandall Stewart 	}
3122eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 0, M_NOWAIT, 1, MT_DATA);
3123f8829a4aSRandall Stewart 	if (m_notify == NULL)
3124f8829a4aSRandall Stewart 		/* no space left */
3125f8829a4aSRandall Stewart 		return;
3126139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3127f8829a4aSRandall Stewart 	sai = mtod(m_notify, struct sctp_adaptation_event *);
3128e432298aSXin LI 	memset(sai, 0, sizeof(struct sctp_adaptation_event));
3129f8829a4aSRandall Stewart 	sai->sai_type = SCTP_ADAPTATION_INDICATION;
3130f8829a4aSRandall Stewart 	sai->sai_flags = 0;
3131f8829a4aSRandall Stewart 	sai->sai_length = sizeof(struct sctp_adaptation_event);
31322afb3e84SRandall Stewart 	sai->sai_adaptation_ind = stcb->asoc.peers_adaptation;
3133f8829a4aSRandall Stewart 	sai->sai_assoc_id = sctp_get_associd(stcb);
3134f8829a4aSRandall Stewart 
3135139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_adaptation_event);
3136139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3137f8829a4aSRandall Stewart 
3138f8829a4aSRandall Stewart 	/* append to socket */
3139f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
31407215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3141f8829a4aSRandall Stewart 	    m_notify);
3142f8829a4aSRandall Stewart 	if (control == NULL) {
3143f8829a4aSRandall Stewart 		/* no memory */
3144f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3145f8829a4aSRandall Stewart 		return;
3146f8829a4aSRandall Stewart 	}
3147139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3148139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3149f8829a4aSRandall Stewart 	/* not that we need this */
3150f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3151f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3152f8829a4aSRandall Stewart 	    control,
3153cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3154f8829a4aSRandall Stewart }
3155f8829a4aSRandall Stewart 
315603b0b021SRandall Stewart /* This always must be called with the read-queue LOCKED in the INP */
3157810ec536SMichael Tuexen static void
31582dad8a55SRandall Stewart sctp_notify_partial_delivery_indication(struct sctp_tcb *stcb, uint32_t error,
3159810ec536SMichael Tuexen     uint32_t val, int so_locked
3160810ec536SMichael Tuexen #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3161810ec536SMichael Tuexen     SCTP_UNUSED
3162810ec536SMichael Tuexen #endif
3163810ec536SMichael Tuexen )
3164f8829a4aSRandall Stewart {
3165f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3166f8829a4aSRandall Stewart 	struct sctp_pdapi_event *pdapi;
3167f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
316803b0b021SRandall Stewart 	struct sockbuf *sb;
3169f8829a4aSRandall Stewart 
317060990c0cSMichael Tuexen 	if ((stcb == NULL) ||
317160990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_PDAPIEVNT)) {
3172f8829a4aSRandall Stewart 		/* event not enabled */
3173f8829a4aSRandall Stewart 		return;
3174830d754dSRandall Stewart 	}
3175cd1386abSMichael Tuexen 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
3176cd1386abSMichael Tuexen 		return;
3177cd1386abSMichael Tuexen 	}
3178eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_pdapi_event), 0, M_NOWAIT, 1, MT_DATA);
3179f8829a4aSRandall Stewart 	if (m_notify == NULL)
3180f8829a4aSRandall Stewart 		/* no space left */
3181f8829a4aSRandall Stewart 		return;
3182139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3183f8829a4aSRandall Stewart 	pdapi = mtod(m_notify, struct sctp_pdapi_event *);
3184e432298aSXin LI 	memset(pdapi, 0, sizeof(struct sctp_pdapi_event));
3185f8829a4aSRandall Stewart 	pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
3186f8829a4aSRandall Stewart 	pdapi->pdapi_flags = 0;
3187f8829a4aSRandall Stewart 	pdapi->pdapi_length = sizeof(struct sctp_pdapi_event);
3188f8829a4aSRandall Stewart 	pdapi->pdapi_indication = error;
31899a6142d8SRandall Stewart 	pdapi->pdapi_stream = (val >> 16);
31909a6142d8SRandall Stewart 	pdapi->pdapi_seq = (val & 0x0000ffff);
3191f8829a4aSRandall Stewart 	pdapi->pdapi_assoc_id = sctp_get_associd(stcb);
3192f8829a4aSRandall Stewart 
3193139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_pdapi_event);
3194139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3195f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
31967215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3197f8829a4aSRandall Stewart 	    m_notify);
3198f8829a4aSRandall Stewart 	if (control == NULL) {
3199f8829a4aSRandall Stewart 		/* no memory */
3200f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3201f8829a4aSRandall Stewart 		return;
3202f8829a4aSRandall Stewart 	}
3203139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3204139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3205f8829a4aSRandall Stewart 	/* not that we need this */
3206f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
320703b0b021SRandall Stewart 	control->held_length = 0;
320803b0b021SRandall Stewart 	control->length = 0;
320903b0b021SRandall Stewart 	sb = &stcb->sctp_socket->so_rcv;
3210b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3211139bc87fSRandall Stewart 		sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m_notify));
321280fefe0aSRandall Stewart 	}
321303b0b021SRandall Stewart 	sctp_sballoc(stcb, sb, m_notify);
3214b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
321503b0b021SRandall Stewart 		sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
321680fefe0aSRandall Stewart 	}
3217139bc87fSRandall Stewart 	atomic_add_int(&control->length, SCTP_BUF_LEN(m_notify));
321803b0b021SRandall Stewart 	control->end_added = 1;
321903b0b021SRandall Stewart 	if (stcb->asoc.control_pdapi)
322003b0b021SRandall Stewart 		TAILQ_INSERT_AFTER(&stcb->sctp_ep->read_queue, stcb->asoc.control_pdapi, control, next);
322103b0b021SRandall Stewart 	else {
322203b0b021SRandall Stewart 		/* we really should not see this case */
322303b0b021SRandall Stewart 		TAILQ_INSERT_TAIL(&stcb->sctp_ep->read_queue, control, next);
322403b0b021SRandall Stewart 	}
322503b0b021SRandall Stewart 	if (stcb->sctp_ep && stcb->sctp_socket) {
322603b0b021SRandall Stewart 		/* This should always be the case */
3227810ec536SMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3228810ec536SMichael Tuexen 		struct socket *so;
3229810ec536SMichael Tuexen 
3230810ec536SMichael Tuexen 		so = SCTP_INP_SO(stcb->sctp_ep);
3231810ec536SMichael Tuexen 		if (!so_locked) {
3232810ec536SMichael Tuexen 			atomic_add_int(&stcb->asoc.refcnt, 1);
3233810ec536SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
3234810ec536SMichael Tuexen 			SCTP_SOCKET_LOCK(so, 1);
3235810ec536SMichael Tuexen 			SCTP_TCB_LOCK(stcb);
3236810ec536SMichael Tuexen 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
3237810ec536SMichael Tuexen 			if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3238810ec536SMichael Tuexen 				SCTP_SOCKET_UNLOCK(so, 1);
3239810ec536SMichael Tuexen 				return;
3240810ec536SMichael Tuexen 			}
3241810ec536SMichael Tuexen 		}
3242810ec536SMichael Tuexen #endif
324303b0b021SRandall Stewart 		sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
3244810ec536SMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3245810ec536SMichael Tuexen 		if (!so_locked) {
3246810ec536SMichael Tuexen 			SCTP_SOCKET_UNLOCK(so, 1);
3247810ec536SMichael Tuexen 		}
3248810ec536SMichael Tuexen #endif
3249f8829a4aSRandall Stewart 	}
3250f8829a4aSRandall Stewart }
3251f8829a4aSRandall Stewart 
3252f8829a4aSRandall Stewart static void
3253f8829a4aSRandall Stewart sctp_notify_shutdown_event(struct sctp_tcb *stcb)
3254f8829a4aSRandall Stewart {
3255f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3256f8829a4aSRandall Stewart 	struct sctp_shutdown_event *sse;
3257f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3258f8829a4aSRandall Stewart 
3259f8829a4aSRandall Stewart 	/*
3260f8829a4aSRandall Stewart 	 * For TCP model AND UDP connected sockets we will send an error up
3261f8829a4aSRandall Stewart 	 * when an SHUTDOWN completes
3262f8829a4aSRandall Stewart 	 */
3263f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3264f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3265f8829a4aSRandall Stewart 		/* mark socket closed for read/write and wakeup! */
3266ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3267ceaad40aSRandall Stewart 		struct socket *so;
3268ceaad40aSRandall Stewart 
3269ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
3270ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
3271ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3272ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
3273ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
3274ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
3275ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3276ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
3277ceaad40aSRandall Stewart 			return;
3278ceaad40aSRandall Stewart 		}
3279ceaad40aSRandall Stewart #endif
3280f8829a4aSRandall Stewart 		socantsendmore(stcb->sctp_socket);
3281ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3282ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
3283ceaad40aSRandall Stewart #endif
3284f8829a4aSRandall Stewart 	}
3285e2e7c62eSMichael Tuexen 	if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)) {
3286f8829a4aSRandall Stewart 		/* event not enabled */
3287f8829a4aSRandall Stewart 		return;
3288830d754dSRandall Stewart 	}
3289eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_event), 0, M_NOWAIT, 1, MT_DATA);
3290f8829a4aSRandall Stewart 	if (m_notify == NULL)
3291f8829a4aSRandall Stewart 		/* no space left */
3292f8829a4aSRandall Stewart 		return;
3293f8829a4aSRandall Stewart 	sse = mtod(m_notify, struct sctp_shutdown_event *);
3294e432298aSXin LI 	memset(sse, 0, sizeof(struct sctp_shutdown_event));
3295f8829a4aSRandall Stewart 	sse->sse_type = SCTP_SHUTDOWN_EVENT;
3296f8829a4aSRandall Stewart 	sse->sse_flags = 0;
3297f8829a4aSRandall Stewart 	sse->sse_length = sizeof(struct sctp_shutdown_event);
3298f8829a4aSRandall Stewart 	sse->sse_assoc_id = sctp_get_associd(stcb);
3299f8829a4aSRandall Stewart 
3300139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_shutdown_event);
3301139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3302f8829a4aSRandall Stewart 
3303f8829a4aSRandall Stewart 	/* append to socket */
3304f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
33057215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3306f8829a4aSRandall Stewart 	    m_notify);
3307f8829a4aSRandall Stewart 	if (control == NULL) {
3308f8829a4aSRandall Stewart 		/* no memory */
3309f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3310f8829a4aSRandall Stewart 		return;
3311f8829a4aSRandall Stewart 	}
3312139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3313139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3314f8829a4aSRandall Stewart 	/* not that we need this */
3315f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3316f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3317f8829a4aSRandall Stewart 	    control,
3318cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3319f8829a4aSRandall Stewart }
3320f8829a4aSRandall Stewart 
3321f8829a4aSRandall Stewart static void
3322830d754dSRandall Stewart sctp_notify_sender_dry_event(struct sctp_tcb *stcb,
3323830d754dSRandall Stewart     int so_locked
3324830d754dSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3325830d754dSRandall Stewart     SCTP_UNUSED
3326830d754dSRandall Stewart #endif
3327830d754dSRandall Stewart )
3328830d754dSRandall Stewart {
3329830d754dSRandall Stewart 	struct mbuf *m_notify;
3330830d754dSRandall Stewart 	struct sctp_sender_dry_event *event;
3331830d754dSRandall Stewart 	struct sctp_queued_to_read *control;
3332830d754dSRandall Stewart 
333360990c0cSMichael Tuexen 	if ((stcb == NULL) ||
333460990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DRYEVNT)) {
3335830d754dSRandall Stewart 		/* event not enabled */
3336830d754dSRandall Stewart 		return;
3337830d754dSRandall Stewart 	}
3338eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_sender_dry_event), 0, M_NOWAIT, 1, MT_DATA);
3339830d754dSRandall Stewart 	if (m_notify == NULL) {
3340830d754dSRandall Stewart 		/* no space left */
3341830d754dSRandall Stewart 		return;
3342830d754dSRandall Stewart 	}
3343830d754dSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3344830d754dSRandall Stewart 	event = mtod(m_notify, struct sctp_sender_dry_event *);
3345e432298aSXin LI 	memset(event, 0, sizeof(struct sctp_sender_dry_event));
3346830d754dSRandall Stewart 	event->sender_dry_type = SCTP_SENDER_DRY_EVENT;
3347830d754dSRandall Stewart 	event->sender_dry_flags = 0;
3348830d754dSRandall Stewart 	event->sender_dry_length = sizeof(struct sctp_sender_dry_event);
3349830d754dSRandall Stewart 	event->sender_dry_assoc_id = sctp_get_associd(stcb);
3350830d754dSRandall Stewart 
3351830d754dSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_sender_dry_event);
3352830d754dSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3353830d754dSRandall Stewart 
3354830d754dSRandall Stewart 	/* append to socket */
3355830d754dSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
33567215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
33577215cc1bSMichael Tuexen 	    m_notify);
3358830d754dSRandall Stewart 	if (control == NULL) {
3359830d754dSRandall Stewart 		/* no memory */
3360830d754dSRandall Stewart 		sctp_m_freem(m_notify);
3361830d754dSRandall Stewart 		return;
3362830d754dSRandall Stewart 	}
3363830d754dSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3364830d754dSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3365830d754dSRandall Stewart 	/* not that we need this */
3366830d754dSRandall Stewart 	control->tail_mbuf = m_notify;
3367830d754dSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3368cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3369830d754dSRandall Stewart }
3370830d754dSRandall Stewart 
3371ea44232bSRandall Stewart 
3372c4e848b7SRandall Stewart void
3373c4e848b7SRandall Stewart sctp_notify_stream_reset_add(struct sctp_tcb *stcb, uint16_t numberin, uint16_t numberout, int flag)
3374ea44232bSRandall Stewart {
3375ea44232bSRandall Stewart 	struct mbuf *m_notify;
3376ea44232bSRandall Stewart 	struct sctp_queued_to_read *control;
3377c4e848b7SRandall Stewart 	struct sctp_stream_change_event *stradd;
3378ea44232bSRandall Stewart 
33798c501e51SMichael Tuexen 	if ((stcb == NULL) ||
33808c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_CHANGEEVNT))) {
3381ea44232bSRandall Stewart 		/* event not enabled */
3382ea44232bSRandall Stewart 		return;
3383ea44232bSRandall Stewart 	}
3384c4e848b7SRandall Stewart 	if ((stcb->asoc.peer_req_out) && flag) {
3385c4e848b7SRandall Stewart 		/* Peer made the request, don't tell the local user */
3386c4e848b7SRandall Stewart 		stcb->asoc.peer_req_out = 0;
3387c4e848b7SRandall Stewart 		return;
3388c4e848b7SRandall Stewart 	}
3389c4e848b7SRandall Stewart 	stcb->asoc.peer_req_out = 0;
3390e432298aSXin LI 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_stream_change_event), 0, M_NOWAIT, 1, MT_DATA);
3391ea44232bSRandall Stewart 	if (m_notify == NULL)
3392ea44232bSRandall Stewart 		/* no space left */
3393ea44232bSRandall Stewart 		return;
3394ea44232bSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3395c4e848b7SRandall Stewart 	stradd = mtod(m_notify, struct sctp_stream_change_event *);
3396e432298aSXin LI 	memset(stradd, 0, sizeof(struct sctp_stream_change_event));
3397c4e848b7SRandall Stewart 	stradd->strchange_type = SCTP_STREAM_CHANGE_EVENT;
3398c4e848b7SRandall Stewart 	stradd->strchange_flags = flag;
3399e432298aSXin LI 	stradd->strchange_length = sizeof(struct sctp_stream_change_event);
3400c4e848b7SRandall Stewart 	stradd->strchange_assoc_id = sctp_get_associd(stcb);
3401c4e848b7SRandall Stewart 	stradd->strchange_instrms = numberin;
3402c4e848b7SRandall Stewart 	stradd->strchange_outstrms = numberout;
3403e432298aSXin LI 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_stream_change_event);
3404ea44232bSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3405ea44232bSRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3406ea44232bSRandall Stewart 		/* no space */
3407ea44232bSRandall Stewart 		sctp_m_freem(m_notify);
3408ea44232bSRandall Stewart 		return;
3409ea44232bSRandall Stewart 	}
3410ea44232bSRandall Stewart 	/* append to socket */
3411ea44232bSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
34127215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3413ea44232bSRandall Stewart 	    m_notify);
3414ea44232bSRandall Stewart 	if (control == NULL) {
3415ea44232bSRandall Stewart 		/* no memory */
3416ea44232bSRandall Stewart 		sctp_m_freem(m_notify);
3417ea44232bSRandall Stewart 		return;
3418ea44232bSRandall Stewart 	}
3419ea44232bSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3420ea44232bSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3421ea44232bSRandall Stewart 	/* not that we need this */
3422ea44232bSRandall Stewart 	control->tail_mbuf = m_notify;
3423ea44232bSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3424ea44232bSRandall Stewart 	    control,
3425cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3426ea44232bSRandall Stewart }
3427ea44232bSRandall Stewart 
3428c4e848b7SRandall Stewart void
3429c4e848b7SRandall Stewart sctp_notify_stream_reset_tsn(struct sctp_tcb *stcb, uint32_t sending_tsn, uint32_t recv_tsn, int flag)
3430c4e848b7SRandall Stewart {
3431c4e848b7SRandall Stewart 	struct mbuf *m_notify;
3432c4e848b7SRandall Stewart 	struct sctp_queued_to_read *control;
3433c4e848b7SRandall Stewart 	struct sctp_assoc_reset_event *strasoc;
3434c4e848b7SRandall Stewart 
34358c501e51SMichael Tuexen 	if ((stcb == NULL) ||
34368c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ASSOC_RESETEVNT))) {
3437c4e848b7SRandall Stewart 		/* event not enabled */
3438c4e848b7SRandall Stewart 		return;
3439c4e848b7SRandall Stewart 	}
3440e432298aSXin LI 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_assoc_reset_event), 0, M_NOWAIT, 1, MT_DATA);
3441c4e848b7SRandall Stewart 	if (m_notify == NULL)
3442c4e848b7SRandall Stewart 		/* no space left */
3443c4e848b7SRandall Stewart 		return;
3444c4e848b7SRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3445c4e848b7SRandall Stewart 	strasoc = mtod(m_notify, struct sctp_assoc_reset_event *);
3446e432298aSXin LI 	memset(strasoc, 0, sizeof(struct sctp_assoc_reset_event));
3447c4e848b7SRandall Stewart 	strasoc->assocreset_type = SCTP_ASSOC_RESET_EVENT;
3448c4e848b7SRandall Stewart 	strasoc->assocreset_flags = flag;
3449e432298aSXin LI 	strasoc->assocreset_length = sizeof(struct sctp_assoc_reset_event);
3450c4e848b7SRandall Stewart 	strasoc->assocreset_assoc_id = sctp_get_associd(stcb);
3451c4e848b7SRandall Stewart 	strasoc->assocreset_local_tsn = sending_tsn;
3452c4e848b7SRandall Stewart 	strasoc->assocreset_remote_tsn = recv_tsn;
3453e432298aSXin LI 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_assoc_reset_event);
3454c4e848b7SRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3455c4e848b7SRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3456c4e848b7SRandall Stewart 		/* no space */
3457c4e848b7SRandall Stewart 		sctp_m_freem(m_notify);
3458c4e848b7SRandall Stewart 		return;
3459c4e848b7SRandall Stewart 	}
3460c4e848b7SRandall Stewart 	/* append to socket */
3461c4e848b7SRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3462c4e848b7SRandall Stewart 	    0, 0, stcb->asoc.context, 0, 0, 0,
3463c4e848b7SRandall Stewart 	    m_notify);
3464c4e848b7SRandall Stewart 	if (control == NULL) {
3465c4e848b7SRandall Stewart 		/* no memory */
3466c4e848b7SRandall Stewart 		sctp_m_freem(m_notify);
3467c4e848b7SRandall Stewart 		return;
3468c4e848b7SRandall Stewart 	}
3469c4e848b7SRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3470c4e848b7SRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3471c4e848b7SRandall Stewart 	/* not that we need this */
3472c4e848b7SRandall Stewart 	control->tail_mbuf = m_notify;
3473c4e848b7SRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3474c4e848b7SRandall Stewart 	    control,
3475c4e848b7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3476c4e848b7SRandall Stewart }
3477c4e848b7SRandall Stewart 
3478c4e848b7SRandall Stewart 
3479ea44232bSRandall Stewart 
3480830d754dSRandall Stewart static void
3481f8829a4aSRandall Stewart sctp_notify_stream_reset(struct sctp_tcb *stcb,
3482f8829a4aSRandall Stewart     int number_entries, uint16_t * list, int flag)
3483f8829a4aSRandall Stewart {
3484f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3485f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3486f8829a4aSRandall Stewart 	struct sctp_stream_reset_event *strreset;
3487f8829a4aSRandall Stewart 	int len;
3488f8829a4aSRandall Stewart 
34898c501e51SMichael Tuexen 	if ((stcb == NULL) ||
34908c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT))) {
3491f8829a4aSRandall Stewart 		/* event not enabled */
3492f8829a4aSRandall Stewart 		return;
3493830d754dSRandall Stewart 	}
3494eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
3495f8829a4aSRandall Stewart 	if (m_notify == NULL)
3496f8829a4aSRandall Stewart 		/* no space left */
3497f8829a4aSRandall Stewart 		return;
3498139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3499f8829a4aSRandall Stewart 	len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
3500f8829a4aSRandall Stewart 	if (len > M_TRAILINGSPACE(m_notify)) {
3501f8829a4aSRandall Stewart 		/* never enough room */
3502f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3503f8829a4aSRandall Stewart 		return;
3504f8829a4aSRandall Stewart 	}
3505f8829a4aSRandall Stewart 	strreset = mtod(m_notify, struct sctp_stream_reset_event *);
3506e432298aSXin LI 	memset(strreset, 0, len);
3507f8829a4aSRandall Stewart 	strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
3508c4e848b7SRandall Stewart 	strreset->strreset_flags = flag;
3509f8829a4aSRandall Stewart 	strreset->strreset_length = len;
3510f8829a4aSRandall Stewart 	strreset->strreset_assoc_id = sctp_get_associd(stcb);
3511f8829a4aSRandall Stewart 	if (number_entries) {
3512f8829a4aSRandall Stewart 		int i;
3513f8829a4aSRandall Stewart 
3514f8829a4aSRandall Stewart 		for (i = 0; i < number_entries; i++) {
3515c4e848b7SRandall Stewart 			strreset->strreset_stream_list[i] = ntohs(list[i]);
3516f8829a4aSRandall Stewart 		}
3517f8829a4aSRandall Stewart 	}
3518139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = len;
3519139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3520139bc87fSRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3521f8829a4aSRandall Stewart 		/* no space */
3522f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3523f8829a4aSRandall Stewart 		return;
3524f8829a4aSRandall Stewart 	}
3525f8829a4aSRandall Stewart 	/* append to socket */
3526f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
35277215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3528f8829a4aSRandall Stewart 	    m_notify);
3529f8829a4aSRandall Stewart 	if (control == NULL) {
3530f8829a4aSRandall Stewart 		/* no memory */
3531f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3532f8829a4aSRandall Stewart 		return;
3533f8829a4aSRandall Stewart 	}
3534139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3535139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3536f8829a4aSRandall Stewart 	/* not that we need this */
3537f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3538f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3539f8829a4aSRandall Stewart 	    control,
3540cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3541f8829a4aSRandall Stewart }
3542f8829a4aSRandall Stewart 
3543f8829a4aSRandall Stewart 
3544389b1b11SMichael Tuexen static void
3545389b1b11SMichael Tuexen sctp_notify_remote_error(struct sctp_tcb *stcb, uint16_t error, struct sctp_error_chunk *chunk)
3546389b1b11SMichael Tuexen {
3547389b1b11SMichael Tuexen 	struct mbuf *m_notify;
3548389b1b11SMichael Tuexen 	struct sctp_remote_error *sre;
3549389b1b11SMichael Tuexen 	struct sctp_queued_to_read *control;
3550389b1b11SMichael Tuexen 	size_t notif_len, chunk_len;
3551389b1b11SMichael Tuexen 
3552389b1b11SMichael Tuexen 	if ((stcb == NULL) ||
3553389b1b11SMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPEERERR)) {
3554389b1b11SMichael Tuexen 		return;
3555389b1b11SMichael Tuexen 	}
3556389b1b11SMichael Tuexen 	if (chunk != NULL) {
3557c9eb4473SMichael Tuexen 		chunk_len = ntohs(chunk->ch.chunk_length);
3558389b1b11SMichael Tuexen 	} else {
3559389b1b11SMichael Tuexen 		chunk_len = 0;
3560389b1b11SMichael Tuexen 	}
3561389b1b11SMichael Tuexen 	notif_len = sizeof(struct sctp_remote_error) + chunk_len;
3562eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3563389b1b11SMichael Tuexen 	if (m_notify == NULL) {
3564389b1b11SMichael Tuexen 		/* Retry with smaller value. */
3565389b1b11SMichael Tuexen 		notif_len = sizeof(struct sctp_remote_error);
3566eb1b1807SGleb Smirnoff 		m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3567389b1b11SMichael Tuexen 		if (m_notify == NULL) {
3568389b1b11SMichael Tuexen 			return;
3569389b1b11SMichael Tuexen 		}
3570389b1b11SMichael Tuexen 	}
3571389b1b11SMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = NULL;
3572389b1b11SMichael Tuexen 	sre = mtod(m_notify, struct sctp_remote_error *);
357356711f94SMichael Tuexen 	memset(sre, 0, notif_len);
3574389b1b11SMichael Tuexen 	sre->sre_type = SCTP_REMOTE_ERROR;
3575389b1b11SMichael Tuexen 	sre->sre_flags = 0;
3576389b1b11SMichael Tuexen 	sre->sre_length = sizeof(struct sctp_remote_error);
3577389b1b11SMichael Tuexen 	sre->sre_error = error;
3578389b1b11SMichael Tuexen 	sre->sre_assoc_id = sctp_get_associd(stcb);
3579389b1b11SMichael Tuexen 	if (notif_len > sizeof(struct sctp_remote_error)) {
3580389b1b11SMichael Tuexen 		memcpy(sre->sre_data, chunk, chunk_len);
3581389b1b11SMichael Tuexen 		sre->sre_length += chunk_len;
3582389b1b11SMichael Tuexen 	}
3583389b1b11SMichael Tuexen 	SCTP_BUF_LEN(m_notify) = sre->sre_length;
3584389b1b11SMichael Tuexen 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3585389b1b11SMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3586389b1b11SMichael Tuexen 	    m_notify);
3587389b1b11SMichael Tuexen 	if (control != NULL) {
3588389b1b11SMichael Tuexen 		control->length = SCTP_BUF_LEN(m_notify);
3589389b1b11SMichael Tuexen 		/* not that we need this */
3590389b1b11SMichael Tuexen 		control->tail_mbuf = m_notify;
3591389b1b11SMichael Tuexen 		control->spec_flags = M_NOTIFICATION;
3592389b1b11SMichael Tuexen 		sctp_add_to_readq(stcb->sctp_ep, stcb,
3593389b1b11SMichael Tuexen 		    control,
3594389b1b11SMichael Tuexen 		    &stcb->sctp_socket->so_rcv, 1,
3595389b1b11SMichael Tuexen 		    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3596389b1b11SMichael Tuexen 	} else {
3597389b1b11SMichael Tuexen 		sctp_m_freem(m_notify);
3598389b1b11SMichael Tuexen 	}
3599389b1b11SMichael Tuexen }
3600389b1b11SMichael Tuexen 
3601389b1b11SMichael Tuexen 
3602f8829a4aSRandall Stewart void
3603f8829a4aSRandall Stewart sctp_ulp_notify(uint32_t notification, struct sctp_tcb *stcb,
3604ceaad40aSRandall Stewart     uint32_t error, void *data, int so_locked
3605ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3606ceaad40aSRandall Stewart     SCTP_UNUSED
3607ceaad40aSRandall Stewart #endif
3608ceaad40aSRandall Stewart )
3609f8829a4aSRandall Stewart {
3610830d754dSRandall Stewart 	if ((stcb == NULL) ||
3611830d754dSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3612f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3613830d754dSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3614830d754dSRandall Stewart 		/* If the socket is gone we are out of here */
3615f8829a4aSRandall Stewart 		return;
3616f8829a4aSRandall Stewart 	}
3617a99b6783SRandall Stewart 	if (stcb->sctp_socket->so_rcv.sb_state & SBS_CANTRCVMORE) {
3618a99b6783SRandall Stewart 		return;
3619a99b6783SRandall Stewart 	}
3620fb4a67d2SMichael Tuexen 	if ((stcb->asoc.state & SCTP_STATE_COOKIE_WAIT) ||
3621fb4a67d2SMichael Tuexen 	    (stcb->asoc.state & SCTP_STATE_COOKIE_ECHOED)) {
362217205eccSRandall Stewart 		if ((notification == SCTP_NOTIFY_INTERFACE_DOWN) ||
362317205eccSRandall Stewart 		    (notification == SCTP_NOTIFY_INTERFACE_UP) ||
362417205eccSRandall Stewart 		    (notification == SCTP_NOTIFY_INTERFACE_CONFIRMED)) {
362517205eccSRandall Stewart 			/* Don't report these in front states */
362617205eccSRandall Stewart 			return;
362717205eccSRandall Stewart 		}
362817205eccSRandall Stewart 	}
3629f8829a4aSRandall Stewart 	switch (notification) {
3630f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_UP:
3631f8829a4aSRandall Stewart 		if (stcb->asoc.assoc_up_sent == 0) {
3632410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_UP, stcb, error, NULL, 0, so_locked);
3633f8829a4aSRandall Stewart 			stcb->asoc.assoc_up_sent = 1;
3634f8829a4aSRandall Stewart 		}
36352afb3e84SRandall Stewart 		if (stcb->asoc.adaptation_needed && (stcb->asoc.adaptation_sent == 0)) {
36367215cc1bSMichael Tuexen 			sctp_notify_adaptation_layer(stcb);
36372afb3e84SRandall Stewart 		}
3638c79bec9cSMichael Tuexen 		if (stcb->asoc.auth_supported == 0) {
3639830d754dSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3640830d754dSRandall Stewart 			    NULL, so_locked);
3641830d754dSRandall Stewart 		}
3642f8829a4aSRandall Stewart 		break;
3643f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_DOWN:
3644410a3b1eSMichael Tuexen 		sctp_notify_assoc_change(SCTP_SHUTDOWN_COMP, stcb, error, NULL, 0, so_locked);
3645f8829a4aSRandall Stewart 		break;
3646f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_DOWN:
3647f8829a4aSRandall Stewart 		{
3648f8829a4aSRandall Stewart 			struct sctp_nets *net;
3649f8829a4aSRandall Stewart 
3650f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
3651f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_UNREACHABLE,
36523cb3567dSMichael Tuexen 			    (struct sockaddr *)&net->ro._l_addr, error, so_locked);
3653f8829a4aSRandall Stewart 			break;
3654f8829a4aSRandall Stewart 		}
3655f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_UP:
3656f8829a4aSRandall Stewart 		{
3657f8829a4aSRandall Stewart 			struct sctp_nets *net;
3658f8829a4aSRandall Stewart 
3659f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
3660f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_AVAILABLE,
36613cb3567dSMichael Tuexen 			    (struct sockaddr *)&net->ro._l_addr, error, so_locked);
3662f8829a4aSRandall Stewart 			break;
3663f8829a4aSRandall Stewart 		}
3664f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_CONFIRMED:
3665f8829a4aSRandall Stewart 		{
3666f8829a4aSRandall Stewart 			struct sctp_nets *net;
3667f8829a4aSRandall Stewart 
3668f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
3669f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_CONFIRMED,
36703cb3567dSMichael Tuexen 			    (struct sockaddr *)&net->ro._l_addr, error, so_locked);
3671f8829a4aSRandall Stewart 			break;
3672f8829a4aSRandall Stewart 		}
3673f8829a4aSRandall Stewart 	case SCTP_NOTIFY_SPECIAL_SP_FAIL:
3674f8829a4aSRandall Stewart 		sctp_notify_send_failed2(stcb, error,
3675ceaad40aSRandall Stewart 		    (struct sctp_stream_queue_pending *)data, so_locked);
3676f8829a4aSRandall Stewart 		break;
36771edc9dbaSMichael Tuexen 	case SCTP_NOTIFY_SENT_DG_FAIL:
36781edc9dbaSMichael Tuexen 		sctp_notify_send_failed(stcb, 1, error,
36791edc9dbaSMichael Tuexen 		    (struct sctp_tmit_chunk *)data, so_locked);
36801edc9dbaSMichael Tuexen 		break;
36811edc9dbaSMichael Tuexen 	case SCTP_NOTIFY_UNSENT_DG_FAIL:
36821edc9dbaSMichael Tuexen 		sctp_notify_send_failed(stcb, 0, error,
3683ceaad40aSRandall Stewart 		    (struct sctp_tmit_chunk *)data, so_locked);
3684f8829a4aSRandall Stewart 		break;
3685f8829a4aSRandall Stewart 	case SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION:
36869a6142d8SRandall Stewart 		{
36879a6142d8SRandall Stewart 			uint32_t val;
36889a6142d8SRandall Stewart 
36899a6142d8SRandall Stewart 			val = *((uint32_t *) data);
36909a6142d8SRandall Stewart 
3691810ec536SMichael Tuexen 			sctp_notify_partial_delivery_indication(stcb, error, val, so_locked);
3692f8829a4aSRandall Stewart 			break;
3693810ec536SMichael Tuexen 		}
3694410a3b1eSMichael Tuexen 	case SCTP_NOTIFY_ASSOC_LOC_ABORTED:
3695fb4a67d2SMichael Tuexen 		if (((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_WAIT) ||
3696fb4a67d2SMichael Tuexen 		    ((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_ECHOED)) {
3697410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 0, so_locked);
3698c105859eSRandall Stewart 		} else {
3699410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 0, so_locked);
3700410a3b1eSMichael Tuexen 		}
3701410a3b1eSMichael Tuexen 		break;
3702410a3b1eSMichael Tuexen 	case SCTP_NOTIFY_ASSOC_REM_ABORTED:
3703fb4a67d2SMichael Tuexen 		if (((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_WAIT) ||
3704fb4a67d2SMichael Tuexen 		    ((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_ECHOED)) {
3705410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 1, so_locked);
3706410a3b1eSMichael Tuexen 		} else {
3707410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 1, so_locked);
3708c105859eSRandall Stewart 		}
3709f8829a4aSRandall Stewart 		break;
3710f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_RESTART:
3711410a3b1eSMichael Tuexen 		sctp_notify_assoc_change(SCTP_RESTART, stcb, error, NULL, 0, so_locked);
3712c79bec9cSMichael Tuexen 		if (stcb->asoc.auth_supported == 0) {
3713830d754dSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3714830d754dSRandall Stewart 			    NULL, so_locked);
3715830d754dSRandall Stewart 		}
3716f8829a4aSRandall Stewart 		break;
3717f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_SEND:
3718d7714577SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STREAM_RESET_OUTGOING_SSN);
3719f8829a4aSRandall Stewart 		break;
3720f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_RECV:
3721d7714577SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STREAM_RESET_INCOMING);
3722f8829a4aSRandall Stewart 		break;
3723f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_FAILED_OUT:
3724c4e848b7SRandall Stewart 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
3725d7714577SMichael Tuexen 		    (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_FAILED));
3726f8829a4aSRandall Stewart 		break;
3727d4260646SMichael Tuexen 	case SCTP_NOTIFY_STR_RESET_DENIED_OUT:
3728d4260646SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
3729d4260646SMichael Tuexen 		    (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_DENIED));
3730d4260646SMichael Tuexen 		break;
3731f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_FAILED_IN:
3732c4e848b7SRandall Stewart 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
3733d7714577SMichael Tuexen 		    (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_FAILED));
3734f8829a4aSRandall Stewart 		break;
3735d4260646SMichael Tuexen 	case SCTP_NOTIFY_STR_RESET_DENIED_IN:
3736d4260646SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
3737d4260646SMichael Tuexen 		    (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_DENIED));
3738d4260646SMichael Tuexen 		break;
3739f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_ADD_IP:
3740f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_ADDED, data,
37413cb3567dSMichael Tuexen 		    error, so_locked);
3742f8829a4aSRandall Stewart 		break;
3743f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_DELETE_IP:
3744f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_REMOVED, data,
37453cb3567dSMichael Tuexen 		    error, so_locked);
3746f8829a4aSRandall Stewart 		break;
3747f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_SET_PRIMARY:
3748f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_MADE_PRIM, data,
37493cb3567dSMichael Tuexen 		    error, so_locked);
3750f8829a4aSRandall Stewart 		break;
3751f8829a4aSRandall Stewart 	case SCTP_NOTIFY_PEER_SHUTDOWN:
3752f8829a4aSRandall Stewart 		sctp_notify_shutdown_event(stcb);
3753f8829a4aSRandall Stewart 		break;
3754f8829a4aSRandall Stewart 	case SCTP_NOTIFY_AUTH_NEW_KEY:
375578f28045SMichael Tuexen 		sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY, error,
3756830d754dSRandall Stewart 		    (uint16_t) (uintptr_t) data,
3757830d754dSRandall Stewart 		    so_locked);
3758f8829a4aSRandall Stewart 		break;
3759830d754dSRandall Stewart 	case SCTP_NOTIFY_AUTH_FREE_KEY:
3760830d754dSRandall Stewart 		sctp_notify_authentication(stcb, SCTP_AUTH_FREE_KEY, error,
3761830d754dSRandall Stewart 		    (uint16_t) (uintptr_t) data,
3762830d754dSRandall Stewart 		    so_locked);
3763f8829a4aSRandall Stewart 		break;
3764830d754dSRandall Stewart 	case SCTP_NOTIFY_NO_PEER_AUTH:
3765830d754dSRandall Stewart 		sctp_notify_authentication(stcb, SCTP_AUTH_NO_AUTH, error,
3766830d754dSRandall Stewart 		    (uint16_t) (uintptr_t) data,
3767830d754dSRandall Stewart 		    so_locked);
3768830d754dSRandall Stewart 		break;
3769830d754dSRandall Stewart 	case SCTP_NOTIFY_SENDER_DRY:
3770830d754dSRandall Stewart 		sctp_notify_sender_dry_event(stcb, so_locked);
3771830d754dSRandall Stewart 		break;
3772389b1b11SMichael Tuexen 	case SCTP_NOTIFY_REMOTE_ERROR:
3773389b1b11SMichael Tuexen 		sctp_notify_remote_error(stcb, error, data);
3774389b1b11SMichael Tuexen 		break;
3775f8829a4aSRandall Stewart 	default:
3776ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_UTIL1, "%s: unknown notification %xh (%u)\n",
3777ad81507eSRandall Stewart 		    __FUNCTION__, notification, notification);
3778f8829a4aSRandall Stewart 		break;
3779f8829a4aSRandall Stewart 	}			/* end switch */
3780f8829a4aSRandall Stewart }
3781f8829a4aSRandall Stewart 
3782f8829a4aSRandall Stewart void
37831edc9dbaSMichael Tuexen sctp_report_all_outbound(struct sctp_tcb *stcb, uint16_t error, int holds_lock, int so_locked
3784ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3785ceaad40aSRandall Stewart     SCTP_UNUSED
3786ceaad40aSRandall Stewart #endif
3787ceaad40aSRandall Stewart )
3788f8829a4aSRandall Stewart {
3789f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3790f8829a4aSRandall Stewart 	struct sctp_stream_out *outs;
37914a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
37924a9ef3f8SMichael Tuexen 	struct sctp_stream_queue_pending *sp, *nsp;
37937f34832bSRandall Stewart 	int i;
3794f8829a4aSRandall Stewart 
3795ad81507eSRandall Stewart 	if (stcb == NULL) {
3796ad81507eSRandall Stewart 		return;
3797ad81507eSRandall Stewart 	}
37984a9ef3f8SMichael Tuexen 	asoc = &stcb->asoc;
37994a9ef3f8SMichael Tuexen 	if (asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3800478fbccbSRandall Stewart 		/* already being freed */
3801478fbccbSRandall Stewart 		return;
3802478fbccbSRandall Stewart 	}
3803f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3804f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
38054a9ef3f8SMichael Tuexen 	    (asoc->state & SCTP_STATE_CLOSED_SOCKET)) {
3806f8829a4aSRandall Stewart 		return;
3807f8829a4aSRandall Stewart 	}
3808f8829a4aSRandall Stewart 	/* now through all the gunk freeing chunks */
3809ad81507eSRandall Stewart 	if (holds_lock == 0) {
38107f34832bSRandall Stewart 		SCTP_TCB_SEND_LOCK(stcb);
3811ad81507eSRandall Stewart 	}
3812d00aff5dSRandall Stewart 	/* sent queue SHOULD be empty */
38134a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
3814d00aff5dSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3815d00aff5dSRandall Stewart 		asoc->sent_queue_cnt--;
3816325c8c46SMichael Tuexen 		if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
3817a7ad6026SMichael Tuexen 			if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) {
3818a7ad6026SMichael Tuexen 				asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--;
3819a7ad6026SMichael Tuexen #ifdef INVARIANTS
3820a7ad6026SMichael Tuexen 			} else {
3821a7ad6026SMichael Tuexen 				panic("No chunks on the queues for sid %u.", chk->rec.data.stream_number);
3822a7ad6026SMichael Tuexen #endif
3823a7ad6026SMichael Tuexen 			}
3824a7ad6026SMichael Tuexen 		}
38250c0982b8SRandall Stewart 		if (chk->data != NULL) {
3826d00aff5dSRandall Stewart 			sctp_free_bufspace(stcb, asoc, chk, 1);
38271edc9dbaSMichael Tuexen 			sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
38281edc9dbaSMichael Tuexen 			    error, chk, so_locked);
3829810ec536SMichael Tuexen 			if (chk->data) {
3830d00aff5dSRandall Stewart 				sctp_m_freem(chk->data);
3831d00aff5dSRandall Stewart 				chk->data = NULL;
3832d00aff5dSRandall Stewart 			}
3833810ec536SMichael Tuexen 		}
3834689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, so_locked);
3835d00aff5dSRandall Stewart 		/* sa_ignore FREED_MEMORY */
3836d00aff5dSRandall Stewart 	}
3837d00aff5dSRandall Stewart 	/* pending send queue SHOULD be empty */
38384a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
3839d00aff5dSRandall Stewart 		TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3840d00aff5dSRandall Stewart 		asoc->send_queue_cnt--;
3841a7ad6026SMichael Tuexen 		if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) {
3842a7ad6026SMichael Tuexen 			asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--;
3843a7ad6026SMichael Tuexen #ifdef INVARIANTS
3844a7ad6026SMichael Tuexen 		} else {
3845a7ad6026SMichael Tuexen 			panic("No chunks on the queues for sid %u.", chk->rec.data.stream_number);
3846a7ad6026SMichael Tuexen #endif
3847a7ad6026SMichael Tuexen 		}
38480c0982b8SRandall Stewart 		if (chk->data != NULL) {
3849d00aff5dSRandall Stewart 			sctp_free_bufspace(stcb, asoc, chk, 1);
38501edc9dbaSMichael Tuexen 			sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
38511edc9dbaSMichael Tuexen 			    error, chk, so_locked);
3852810ec536SMichael Tuexen 			if (chk->data) {
3853d00aff5dSRandall Stewart 				sctp_m_freem(chk->data);
3854d00aff5dSRandall Stewart 				chk->data = NULL;
3855d00aff5dSRandall Stewart 			}
3856810ec536SMichael Tuexen 		}
3857689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, so_locked);
3858d00aff5dSRandall Stewart 		/* sa_ignore FREED_MEMORY */
3859d00aff5dSRandall Stewart 	}
38604a9ef3f8SMichael Tuexen 	for (i = 0; i < asoc->streamoutcnt; i++) {
38617f34832bSRandall Stewart 		/* For each stream */
38624a9ef3f8SMichael Tuexen 		outs = &asoc->strmout[i];
38637f34832bSRandall Stewart 		/* clean up any sends there */
38644a9ef3f8SMichael Tuexen 		asoc->locked_on_sending = NULL;
38654a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
38664a9ef3f8SMichael Tuexen 			asoc->stream_queue_cnt--;
3867f8829a4aSRandall Stewart 			TAILQ_REMOVE(&outs->outqueue, sp, next);
3868f8829a4aSRandall Stewart 			sctp_free_spbufspace(stcb, asoc, sp);
3869478fbccbSRandall Stewart 			if (sp->data) {
3870f8829a4aSRandall Stewart 				sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
38711edc9dbaSMichael Tuexen 				    error, (void *)sp, so_locked);
3872f8829a4aSRandall Stewart 				if (sp->data) {
3873f8829a4aSRandall Stewart 					sctp_m_freem(sp->data);
3874f8829a4aSRandall Stewart 					sp->data = NULL;
3875d07b2ac6SMichael Tuexen 					sp->tail_mbuf = NULL;
3876d07b2ac6SMichael Tuexen 					sp->length = 0;
3877f8829a4aSRandall Stewart 				}
3878478fbccbSRandall Stewart 			}
38799eea4a2dSMichael Tuexen 			if (sp->net) {
3880f8829a4aSRandall Stewart 				sctp_free_remote_addr(sp->net);
3881f8829a4aSRandall Stewart 				sp->net = NULL;
38829eea4a2dSMichael Tuexen 			}
3883f8829a4aSRandall Stewart 			/* Free the chunk */
3884689e6a5fSMichael Tuexen 			sctp_free_a_strmoq(stcb, sp, so_locked);
38853c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
3886f8829a4aSRandall Stewart 		}
3887f8829a4aSRandall Stewart 	}
3888f8829a4aSRandall Stewart 
3889ad81507eSRandall Stewart 	if (holds_lock == 0) {
38907f34832bSRandall Stewart 		SCTP_TCB_SEND_UNLOCK(stcb);
3891f8829a4aSRandall Stewart 	}
3892ad81507eSRandall Stewart }
3893f8829a4aSRandall Stewart 
3894f8829a4aSRandall Stewart void
3895410a3b1eSMichael Tuexen sctp_abort_notification(struct sctp_tcb *stcb, uint8_t from_peer, uint16_t error,
3896a2b42326SMichael Tuexen     struct sctp_abort_chunk *abort, int so_locked
3897ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3898ceaad40aSRandall Stewart     SCTP_UNUSED
3899ceaad40aSRandall Stewart #endif
3900ceaad40aSRandall Stewart )
3901f8829a4aSRandall Stewart {
3902ad81507eSRandall Stewart 	if (stcb == NULL) {
3903ad81507eSRandall Stewart 		return;
3904ad81507eSRandall Stewart 	}
3905c55b70ceSMichael Tuexen 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3906c55b70ceSMichael Tuexen 	    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3907c55b70ceSMichael Tuexen 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
3908c55b70ceSMichael Tuexen 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_WAS_ABORTED;
3909c55b70ceSMichael Tuexen 	}
3910f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3911f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3912f8829a4aSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3913f8829a4aSRandall Stewart 		return;
3914f8829a4aSRandall Stewart 	}
3915f8829a4aSRandall Stewart 	/* Tell them we lost the asoc */
39161edc9dbaSMichael Tuexen 	sctp_report_all_outbound(stcb, error, 1, so_locked);
3917410a3b1eSMichael Tuexen 	if (from_peer) {
3918410a3b1eSMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_REM_ABORTED, stcb, error, abort, so_locked);
3919410a3b1eSMichael Tuexen 	} else {
3920410a3b1eSMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_LOC_ABORTED, stcb, error, abort, so_locked);
3921410a3b1eSMichael Tuexen 	}
3922f8829a4aSRandall Stewart }
3923f8829a4aSRandall Stewart 
3924f8829a4aSRandall Stewart void
3925f8829a4aSRandall Stewart sctp_abort_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3926b1754ad1SMichael Tuexen     struct mbuf *m, int iphlen,
3927b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
3928b1754ad1SMichael Tuexen     struct sctphdr *sh, struct mbuf *op_err,
3929457b4b88SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid,
3930c54a18d2SRandall Stewart     uint32_t vrf_id, uint16_t port)
3931f8829a4aSRandall Stewart {
3932f8829a4aSRandall Stewart 	uint32_t vtag;
3933f8829a4aSRandall Stewart 
3934ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3935ceaad40aSRandall Stewart 	struct socket *so;
3936ceaad40aSRandall Stewart 
3937ceaad40aSRandall Stewart #endif
3938ceaad40aSRandall Stewart 
3939f8829a4aSRandall Stewart 	vtag = 0;
3940f8829a4aSRandall Stewart 	if (stcb != NULL) {
3941f8829a4aSRandall Stewart 		/* We have a TCB to abort, send notification too */
3942f8829a4aSRandall Stewart 		vtag = stcb->asoc.peer_vtag;
3943410a3b1eSMichael Tuexen 		sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
394417205eccSRandall Stewart 		/* get the assoc vrf id and table id */
394517205eccSRandall Stewart 		vrf_id = stcb->asoc.vrf_id;
394663981c2bSRandall Stewart 		stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
3947f8829a4aSRandall Stewart 	}
3948b1754ad1SMichael Tuexen 	sctp_send_abort(m, iphlen, src, dst, sh, vtag, op_err,
3949d089f9b9SMichael Tuexen 	    mflowtype, mflowid, inp->fibnum,
3950f30ac432SMichael Tuexen 	    vrf_id, port);
3951f8829a4aSRandall Stewart 	if (stcb != NULL) {
3952f8829a4aSRandall Stewart 		/* Ok, now lets free it */
3953ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3954ceaad40aSRandall Stewart 		so = SCTP_INP_SO(inp);
3955ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
3956ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3957ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
3958ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
3959ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
3960ceaad40aSRandall Stewart #endif
39610271d0cdSMichael Tuexen 		SCTP_STAT_INCR_COUNTER32(sctps_aborted);
39620271d0cdSMichael Tuexen 		if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
39630271d0cdSMichael Tuexen 		    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
39640271d0cdSMichael Tuexen 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
39650271d0cdSMichael Tuexen 		}
3966ba785902SMichael Tuexen 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
3967ba785902SMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_4);
3968ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3969ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
3970ceaad40aSRandall Stewart #endif
3971f8829a4aSRandall Stewart 	}
3972f8829a4aSRandall Stewart }
3973f8829a4aSRandall Stewart 
3974f1f73e57SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
3975f1f73e57SRandall Stewart void
3976f1f73e57SRandall Stewart sctp_print_out_track_log(struct sctp_tcb *stcb)
3977f1f73e57SRandall Stewart {
397818e198d3SRandall Stewart #ifdef NOSIY_PRINTS
3979f1f73e57SRandall Stewart 	int i;
3980f1f73e57SRandall Stewart 
3981ad81507eSRandall Stewart 	SCTP_PRINTF("Last ep reason:%x\n", stcb->sctp_ep->last_abort_code);
3982ad81507eSRandall Stewart 	SCTP_PRINTF("IN bound TSN log-aaa\n");
3983f1f73e57SRandall Stewart 	if ((stcb->asoc.tsn_in_at == 0) && (stcb->asoc.tsn_in_wrapped == 0)) {
3984ad81507eSRandall Stewart 		SCTP_PRINTF("None rcvd\n");
3985f1f73e57SRandall Stewart 		goto none_in;
3986f1f73e57SRandall Stewart 	}
3987f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_in_wrapped) {
3988f1f73e57SRandall Stewart 		for (i = stcb->asoc.tsn_in_at; i < SCTP_TSN_LOG_SIZE; i++) {
3989ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3990f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].tsn,
3991f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].strm,
3992f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].seq,
3993f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].flgs,
3994f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].sz);
3995f1f73e57SRandall Stewart 		}
3996f1f73e57SRandall Stewart 	}
3997f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_in_at) {
3998f1f73e57SRandall Stewart 		for (i = 0; i < stcb->asoc.tsn_in_at; i++) {
3999ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4000f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].tsn,
4001f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].strm,
4002f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].seq,
4003f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].flgs,
4004f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].sz);
4005f1f73e57SRandall Stewart 		}
4006f1f73e57SRandall Stewart 	}
4007f1f73e57SRandall Stewart none_in:
4008ad81507eSRandall Stewart 	SCTP_PRINTF("OUT bound TSN log-aaa\n");
4009ad81507eSRandall Stewart 	if ((stcb->asoc.tsn_out_at == 0) &&
4010ad81507eSRandall Stewart 	    (stcb->asoc.tsn_out_wrapped == 0)) {
4011ad81507eSRandall Stewart 		SCTP_PRINTF("None sent\n");
4012f1f73e57SRandall Stewart 	}
4013f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_out_wrapped) {
4014f1f73e57SRandall Stewart 		for (i = stcb->asoc.tsn_out_at; i < SCTP_TSN_LOG_SIZE; i++) {
4015ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4016f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].tsn,
4017f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].strm,
4018f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].seq,
4019f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].flgs,
4020f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].sz);
4021f1f73e57SRandall Stewart 		}
4022f1f73e57SRandall Stewart 	}
4023f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_out_at) {
4024f1f73e57SRandall Stewart 		for (i = 0; i < stcb->asoc.tsn_out_at; i++) {
4025ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4026f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].tsn,
4027f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].strm,
4028f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].seq,
4029f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].flgs,
4030f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].sz);
4031f1f73e57SRandall Stewart 		}
4032f1f73e57SRandall Stewart 	}
403318e198d3SRandall Stewart #endif
4034f1f73e57SRandall Stewart }
4035f1f73e57SRandall Stewart 
4036f1f73e57SRandall Stewart #endif
4037f1f73e57SRandall Stewart 
4038f8829a4aSRandall Stewart void
4039f8829a4aSRandall Stewart sctp_abort_an_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4040a2b42326SMichael Tuexen     struct mbuf *op_err,
4041ceaad40aSRandall Stewart     int so_locked
4042ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4043ceaad40aSRandall Stewart     SCTP_UNUSED
4044ceaad40aSRandall Stewart #endif
4045ceaad40aSRandall Stewart )
4046f8829a4aSRandall Stewart {
4047ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4048ceaad40aSRandall Stewart 	struct socket *so;
4049ceaad40aSRandall Stewart 
4050ceaad40aSRandall Stewart #endif
4051ceaad40aSRandall Stewart 
4052ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4053ceaad40aSRandall Stewart 	so = SCTP_INP_SO(inp);
4054ceaad40aSRandall Stewart #endif
4055f8829a4aSRandall Stewart 	if (stcb == NULL) {
4056f8829a4aSRandall Stewart 		/* Got to have a TCB */
4057f8829a4aSRandall Stewart 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4058fe1831e0SMichael Tuexen 			if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4059b0552ae2SRandall Stewart 				sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4060b0552ae2SRandall Stewart 				    SCTP_CALLED_DIRECTLY_NOCMPSET);
4061f8829a4aSRandall Stewart 			}
4062f8829a4aSRandall Stewart 		}
4063f8829a4aSRandall Stewart 		return;
406463981c2bSRandall Stewart 	} else {
406563981c2bSRandall Stewart 		stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
4066f8829a4aSRandall Stewart 	}
4067f8829a4aSRandall Stewart 	/* notify the ulp */
4068a2b42326SMichael Tuexen 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
4069410a3b1eSMichael Tuexen 		sctp_abort_notification(stcb, 0, 0, NULL, so_locked);
4070a2b42326SMichael Tuexen 	}
4071f8829a4aSRandall Stewart 	/* notify the peer */
4072ceaad40aSRandall Stewart 	sctp_send_abort_tcb(stcb, op_err, so_locked);
4073f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4074f8829a4aSRandall Stewart 	if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
4075f8829a4aSRandall Stewart 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4076f8829a4aSRandall Stewart 		SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4077f8829a4aSRandall Stewart 	}
4078f8829a4aSRandall Stewart 	/* now free the asoc */
4079f1f73e57SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
4080f1f73e57SRandall Stewart 	sctp_print_out_track_log(stcb);
4081f1f73e57SRandall Stewart #endif
4082ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4083ceaad40aSRandall Stewart 	if (!so_locked) {
4084ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
4085ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
4086ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
4087ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4088ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4089ceaad40aSRandall Stewart 	}
4090ceaad40aSRandall Stewart #endif
4091ba785902SMichael Tuexen 	(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4092ba785902SMichael Tuexen 	    SCTP_FROM_SCTPUTIL + SCTP_LOC_5);
4093ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4094ceaad40aSRandall Stewart 	if (!so_locked) {
4095ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4096ceaad40aSRandall Stewart 	}
4097ceaad40aSRandall Stewart #endif
4098f8829a4aSRandall Stewart }
4099f8829a4aSRandall Stewart 
4100f8829a4aSRandall Stewart void
4101b1754ad1SMichael Tuexen sctp_handle_ootb(struct mbuf *m, int iphlen, int offset,
4102b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
4103b1754ad1SMichael Tuexen     struct sctphdr *sh, struct sctp_inpcb *inp,
4104ff1ffd74SMichael Tuexen     struct mbuf *cause,
4105d089f9b9SMichael Tuexen     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4106f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
4107f8829a4aSRandall Stewart {
4108f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch, chunk_buf;
4109f8829a4aSRandall Stewart 	unsigned int chk_length;
4110c58e60beSMichael Tuexen 	int contains_init_chunk;
4111f8829a4aSRandall Stewart 
4112f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue);
4113f8829a4aSRandall Stewart 	/* Generate a TO address for future reference */
4114f8829a4aSRandall Stewart 	if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4115fe1831e0SMichael Tuexen 		if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4116b0552ae2SRandall Stewart 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4117b0552ae2SRandall Stewart 			    SCTP_CALLED_DIRECTLY_NOCMPSET);
4118f8829a4aSRandall Stewart 		}
4119f8829a4aSRandall Stewart 	}
4120c58e60beSMichael Tuexen 	contains_init_chunk = 0;
4121f8829a4aSRandall Stewart 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4122f8829a4aSRandall Stewart 	    sizeof(*ch), (uint8_t *) & chunk_buf);
4123f8829a4aSRandall Stewart 	while (ch != NULL) {
4124f8829a4aSRandall Stewart 		chk_length = ntohs(ch->chunk_length);
4125f8829a4aSRandall Stewart 		if (chk_length < sizeof(*ch)) {
4126f8829a4aSRandall Stewart 			/* break to abort land */
4127f8829a4aSRandall Stewart 			break;
4128f8829a4aSRandall Stewart 		}
4129f8829a4aSRandall Stewart 		switch (ch->chunk_type) {
4130c58e60beSMichael Tuexen 		case SCTP_INIT:
4131c58e60beSMichael Tuexen 			contains_init_chunk = 1;
4132c58e60beSMichael Tuexen 			break;
4133f8829a4aSRandall Stewart 		case SCTP_PACKET_DROPPED:
4134f8829a4aSRandall Stewart 			/* we don't respond to pkt-dropped */
4135f8829a4aSRandall Stewart 			return;
4136f8829a4aSRandall Stewart 		case SCTP_ABORT_ASSOCIATION:
4137f8829a4aSRandall Stewart 			/* we don't respond with an ABORT to an ABORT */
4138f8829a4aSRandall Stewart 			return;
4139f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_COMPLETE:
4140f8829a4aSRandall Stewart 			/*
4141f8829a4aSRandall Stewart 			 * we ignore it since we are not waiting for it and
4142f8829a4aSRandall Stewart 			 * peer is gone
4143f8829a4aSRandall Stewart 			 */
4144f8829a4aSRandall Stewart 			return;
4145f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_ACK:
4146b1754ad1SMichael Tuexen 			sctp_send_shutdown_complete2(src, dst, sh,
4147d089f9b9SMichael Tuexen 			    mflowtype, mflowid, fibnum,
4148f30ac432SMichael Tuexen 			    vrf_id, port);
4149f8829a4aSRandall Stewart 			return;
4150f8829a4aSRandall Stewart 		default:
4151f8829a4aSRandall Stewart 			break;
4152f8829a4aSRandall Stewart 		}
4153f8829a4aSRandall Stewart 		offset += SCTP_SIZE32(chk_length);
4154f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4155f8829a4aSRandall Stewart 		    sizeof(*ch), (uint8_t *) & chunk_buf);
4156f8829a4aSRandall Stewart 	}
4157c58e60beSMichael Tuexen 	if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
4158c58e60beSMichael Tuexen 	    ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
4159c58e60beSMichael Tuexen 	    (contains_init_chunk == 0))) {
4160ff1ffd74SMichael Tuexen 		sctp_send_abort(m, iphlen, src, dst, sh, 0, cause,
4161d089f9b9SMichael Tuexen 		    mflowtype, mflowid, fibnum,
4162f30ac432SMichael Tuexen 		    vrf_id, port);
4163f8829a4aSRandall Stewart 	}
4164c58e60beSMichael Tuexen }
4165f8829a4aSRandall Stewart 
4166f8829a4aSRandall Stewart /*
4167f8829a4aSRandall Stewart  * check the inbound datagram to make sure there is not an abort inside it,
4168f8829a4aSRandall Stewart  * if there is return 1, else return 0.
4169f8829a4aSRandall Stewart  */
4170f8829a4aSRandall Stewart int
4171f8829a4aSRandall Stewart sctp_is_there_an_abort_here(struct mbuf *m, int iphlen, uint32_t * vtagfill)
4172f8829a4aSRandall Stewart {
4173f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
4174f8829a4aSRandall Stewart 	struct sctp_init_chunk *init_chk, chunk_buf;
4175f8829a4aSRandall Stewart 	int offset;
4176f8829a4aSRandall Stewart 	unsigned int chk_length;
4177f8829a4aSRandall Stewart 
4178f8829a4aSRandall Stewart 	offset = iphlen + sizeof(struct sctphdr);
4179f8829a4aSRandall Stewart 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch),
4180f8829a4aSRandall Stewart 	    (uint8_t *) & chunk_buf);
4181f8829a4aSRandall Stewart 	while (ch != NULL) {
4182f8829a4aSRandall Stewart 		chk_length = ntohs(ch->chunk_length);
4183f8829a4aSRandall Stewart 		if (chk_length < sizeof(*ch)) {
4184f8829a4aSRandall Stewart 			/* packet is probably corrupt */
4185f8829a4aSRandall Stewart 			break;
4186f8829a4aSRandall Stewart 		}
4187f8829a4aSRandall Stewart 		/* we seem to be ok, is it an abort? */
4188f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) {
4189f8829a4aSRandall Stewart 			/* yep, tell them */
4190f8829a4aSRandall Stewart 			return (1);
4191f8829a4aSRandall Stewart 		}
4192f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_INITIATION) {
4193f8829a4aSRandall Stewart 			/* need to update the Vtag */
4194f8829a4aSRandall Stewart 			init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4195f8829a4aSRandall Stewart 			    offset, sizeof(*init_chk), (uint8_t *) & chunk_buf);
4196f8829a4aSRandall Stewart 			if (init_chk != NULL) {
4197f8829a4aSRandall Stewart 				*vtagfill = ntohl(init_chk->init.initiate_tag);
4198f8829a4aSRandall Stewart 			}
4199f8829a4aSRandall Stewart 		}
4200f8829a4aSRandall Stewart 		/* Nope, move to the next chunk */
4201f8829a4aSRandall Stewart 		offset += SCTP_SIZE32(chk_length);
4202f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4203f8829a4aSRandall Stewart 		    sizeof(*ch), (uint8_t *) & chunk_buf);
4204f8829a4aSRandall Stewart 	}
4205f8829a4aSRandall Stewart 	return (0);
4206f8829a4aSRandall Stewart }
4207f8829a4aSRandall Stewart 
4208f8829a4aSRandall Stewart /*
4209f8829a4aSRandall Stewart  * currently (2/02), ifa_addr embeds scope_id's and don't have sin6_scope_id
4210f8829a4aSRandall Stewart  * set (i.e. it's 0) so, create this function to compare link local scopes
4211f8829a4aSRandall Stewart  */
42125e2c2d87SRandall Stewart #ifdef INET6
4213f8829a4aSRandall Stewart uint32_t
4214f8829a4aSRandall Stewart sctp_is_same_scope(struct sockaddr_in6 *addr1, struct sockaddr_in6 *addr2)
4215f8829a4aSRandall Stewart {
4216f8829a4aSRandall Stewart 	struct sockaddr_in6 a, b;
4217f8829a4aSRandall Stewart 
4218f8829a4aSRandall Stewart 	/* save copies */
4219f8829a4aSRandall Stewart 	a = *addr1;
4220f8829a4aSRandall Stewart 	b = *addr2;
4221f8829a4aSRandall Stewart 
4222f8829a4aSRandall Stewart 	if (a.sin6_scope_id == 0)
4223f8829a4aSRandall Stewart 		if (sa6_recoverscope(&a)) {
4224f8829a4aSRandall Stewart 			/* can't get scope, so can't match */
4225f8829a4aSRandall Stewart 			return (0);
4226f8829a4aSRandall Stewart 		}
4227f8829a4aSRandall Stewart 	if (b.sin6_scope_id == 0)
4228f8829a4aSRandall Stewart 		if (sa6_recoverscope(&b)) {
4229f8829a4aSRandall Stewart 			/* can't get scope, so can't match */
4230f8829a4aSRandall Stewart 			return (0);
4231f8829a4aSRandall Stewart 		}
4232f8829a4aSRandall Stewart 	if (a.sin6_scope_id != b.sin6_scope_id)
4233f8829a4aSRandall Stewart 		return (0);
4234f8829a4aSRandall Stewart 
4235f8829a4aSRandall Stewart 	return (1);
4236f8829a4aSRandall Stewart }
4237f8829a4aSRandall Stewart 
4238f8829a4aSRandall Stewart /*
4239f8829a4aSRandall Stewart  * returns a sockaddr_in6 with embedded scope recovered and removed
4240f8829a4aSRandall Stewart  */
4241f8829a4aSRandall Stewart struct sockaddr_in6 *
4242f8829a4aSRandall Stewart sctp_recover_scope(struct sockaddr_in6 *addr, struct sockaddr_in6 *store)
4243f8829a4aSRandall Stewart {
4244f8829a4aSRandall Stewart 	/* check and strip embedded scope junk */
4245f8829a4aSRandall Stewart 	if (addr->sin6_family == AF_INET6) {
4246f8829a4aSRandall Stewart 		if (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr)) {
4247f8829a4aSRandall Stewart 			if (addr->sin6_scope_id == 0) {
4248f8829a4aSRandall Stewart 				*store = *addr;
4249f8829a4aSRandall Stewart 				if (!sa6_recoverscope(store)) {
4250f8829a4aSRandall Stewart 					/* use the recovered scope */
4251f8829a4aSRandall Stewart 					addr = store;
4252f8829a4aSRandall Stewart 				}
4253f42a358aSRandall Stewart 			} else {
4254f8829a4aSRandall Stewart 				/* else, return the original "to" addr */
4255f42a358aSRandall Stewart 				in6_clearscope(&addr->sin6_addr);
4256f8829a4aSRandall Stewart 			}
4257f8829a4aSRandall Stewart 		}
4258f8829a4aSRandall Stewart 	}
4259f8829a4aSRandall Stewart 	return (addr);
4260f8829a4aSRandall Stewart }
4261f8829a4aSRandall Stewart 
42625e2c2d87SRandall Stewart #endif
42635e2c2d87SRandall Stewart 
4264f8829a4aSRandall Stewart /*
4265f8829a4aSRandall Stewart  * are the two addresses the same?  currently a "scopeless" check returns: 1
4266f8829a4aSRandall Stewart  * if same, 0 if not
4267f8829a4aSRandall Stewart  */
426872fb6fdbSRandall Stewart int
4269f8829a4aSRandall Stewart sctp_cmpaddr(struct sockaddr *sa1, struct sockaddr *sa2)
4270f8829a4aSRandall Stewart {
4271f8829a4aSRandall Stewart 
4272f8829a4aSRandall Stewart 	/* must be valid */
4273f8829a4aSRandall Stewart 	if (sa1 == NULL || sa2 == NULL)
4274f8829a4aSRandall Stewart 		return (0);
4275f8829a4aSRandall Stewart 
4276f8829a4aSRandall Stewart 	/* must be the same family */
4277f8829a4aSRandall Stewart 	if (sa1->sa_family != sa2->sa_family)
4278f8829a4aSRandall Stewart 		return (0);
4279f8829a4aSRandall Stewart 
42805e2c2d87SRandall Stewart 	switch (sa1->sa_family) {
42815e2c2d87SRandall Stewart #ifdef INET6
42825e2c2d87SRandall Stewart 	case AF_INET6:
42835e2c2d87SRandall Stewart 		{
4284f8829a4aSRandall Stewart 			/* IPv6 addresses */
4285f8829a4aSRandall Stewart 			struct sockaddr_in6 *sin6_1, *sin6_2;
4286f8829a4aSRandall Stewart 
4287f8829a4aSRandall Stewart 			sin6_1 = (struct sockaddr_in6 *)sa1;
4288f8829a4aSRandall Stewart 			sin6_2 = (struct sockaddr_in6 *)sa2;
4289c54a18d2SRandall Stewart 			return (SCTP6_ARE_ADDR_EQUAL(sin6_1,
4290c54a18d2SRandall Stewart 			    sin6_2));
42915e2c2d87SRandall Stewart 		}
42925e2c2d87SRandall Stewart #endif
4293ea5eba11SMichael Tuexen #ifdef INET
42945e2c2d87SRandall Stewart 	case AF_INET:
42955e2c2d87SRandall Stewart 		{
4296f8829a4aSRandall Stewart 			/* IPv4 addresses */
4297f8829a4aSRandall Stewart 			struct sockaddr_in *sin_1, *sin_2;
4298f8829a4aSRandall Stewart 
4299f8829a4aSRandall Stewart 			sin_1 = (struct sockaddr_in *)sa1;
4300f8829a4aSRandall Stewart 			sin_2 = (struct sockaddr_in *)sa2;
4301f8829a4aSRandall Stewart 			return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr);
43025e2c2d87SRandall Stewart 		}
4303ea5eba11SMichael Tuexen #endif
43045e2c2d87SRandall Stewart 	default:
4305f8829a4aSRandall Stewart 		/* we don't do these... */
4306f8829a4aSRandall Stewart 		return (0);
4307f8829a4aSRandall Stewart 	}
4308f8829a4aSRandall Stewart }
4309f8829a4aSRandall Stewart 
4310f8829a4aSRandall Stewart void
4311f8829a4aSRandall Stewart sctp_print_address(struct sockaddr *sa)
4312f8829a4aSRandall Stewart {
43135e2c2d87SRandall Stewart #ifdef INET6
43147d32aa0cSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
4315f8829a4aSRandall Stewart 
43165e2c2d87SRandall Stewart #endif
43175e2c2d87SRandall Stewart 
43185e2c2d87SRandall Stewart 	switch (sa->sa_family) {
43195e2c2d87SRandall Stewart #ifdef INET6
43205e2c2d87SRandall Stewart 	case AF_INET6:
43215e2c2d87SRandall Stewart 		{
4322ad81507eSRandall Stewart 			struct sockaddr_in6 *sin6;
4323ad81507eSRandall Stewart 
4324f8829a4aSRandall Stewart 			sin6 = (struct sockaddr_in6 *)sa;
4325ad81507eSRandall Stewart 			SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n",
43267d32aa0cSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &sin6->sin6_addr),
43277d32aa0cSBjoern A. Zeeb 			    ntohs(sin6->sin6_port),
4328f8829a4aSRandall Stewart 			    sin6->sin6_scope_id);
43295e2c2d87SRandall Stewart 			break;
43305e2c2d87SRandall Stewart 		}
43315e2c2d87SRandall Stewart #endif
4332ea5eba11SMichael Tuexen #ifdef INET
43335e2c2d87SRandall Stewart 	case AF_INET:
43345e2c2d87SRandall Stewart 		{
4335f8829a4aSRandall Stewart 			struct sockaddr_in *sin;
4336f8829a4aSRandall Stewart 			unsigned char *p;
4337f8829a4aSRandall Stewart 
4338f8829a4aSRandall Stewart 			sin = (struct sockaddr_in *)sa;
4339f8829a4aSRandall Stewart 			p = (unsigned char *)&sin->sin_addr;
4340ad81507eSRandall Stewart 			SCTP_PRINTF("IPv4 address: %u.%u.%u.%u:%d\n",
4341f8829a4aSRandall Stewart 			    p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
43425e2c2d87SRandall Stewart 			break;
43435e2c2d87SRandall Stewart 		}
4344ea5eba11SMichael Tuexen #endif
43455e2c2d87SRandall Stewart 	default:
4346ad81507eSRandall Stewart 		SCTP_PRINTF("?\n");
43475e2c2d87SRandall Stewart 		break;
4348f8829a4aSRandall Stewart 	}
4349f8829a4aSRandall Stewart }
4350f8829a4aSRandall Stewart 
4351f8829a4aSRandall Stewart void
4352f8829a4aSRandall Stewart sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
4353f8829a4aSRandall Stewart     struct sctp_inpcb *new_inp,
4354d06c82f1SRandall Stewart     struct sctp_tcb *stcb,
4355d06c82f1SRandall Stewart     int waitflags)
4356f8829a4aSRandall Stewart {
4357f8829a4aSRandall Stewart 	/*
4358f8829a4aSRandall Stewart 	 * go through our old INP and pull off any control structures that
4359f8829a4aSRandall Stewart 	 * belong to stcb and move then to the new inp.
4360f8829a4aSRandall Stewart 	 */
4361f8829a4aSRandall Stewart 	struct socket *old_so, *new_so;
4362f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control, *nctl;
4363f8829a4aSRandall Stewart 	struct sctp_readhead tmp_queue;
4364f8829a4aSRandall Stewart 	struct mbuf *m;
4365bff64a4dSRandall Stewart 	int error = 0;
4366f8829a4aSRandall Stewart 
4367f8829a4aSRandall Stewart 	old_so = old_inp->sctp_socket;
4368f8829a4aSRandall Stewart 	new_so = new_inp->sctp_socket;
4369f8829a4aSRandall Stewart 	TAILQ_INIT(&tmp_queue);
4370d06c82f1SRandall Stewart 	error = sblock(&old_so->so_rcv, waitflags);
4371f8829a4aSRandall Stewart 	if (error) {
4372f8829a4aSRandall Stewart 		/*
4373f8829a4aSRandall Stewart 		 * Gak, can't get sblock, we have a problem. data will be
4374f8829a4aSRandall Stewart 		 * left stranded.. and we don't dare look at it since the
4375f8829a4aSRandall Stewart 		 * other thread may be reading something. Oh well, its a
4376f8829a4aSRandall Stewart 		 * screwed up app that does a peeloff OR a accept while
4377f8829a4aSRandall Stewart 		 * reading from the main socket... actually its only the
4378f8829a4aSRandall Stewart 		 * peeloff() case, since I think read will fail on a
4379f8829a4aSRandall Stewart 		 * listening socket..
4380f8829a4aSRandall Stewart 		 */
4381f8829a4aSRandall Stewart 		return;
4382f8829a4aSRandall Stewart 	}
4383f8829a4aSRandall Stewart 	/* lock the socket buffers */
4384f8829a4aSRandall Stewart 	SCTP_INP_READ_LOCK(old_inp);
43854a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(control, &old_inp->read_queue, next, nctl) {
4386f8829a4aSRandall Stewart 		/* Pull off all for out target stcb */
4387f8829a4aSRandall Stewart 		if (control->stcb == stcb) {
4388f8829a4aSRandall Stewart 			/* remove it we want it */
4389f8829a4aSRandall Stewart 			TAILQ_REMOVE(&old_inp->read_queue, control, next);
4390f8829a4aSRandall Stewart 			TAILQ_INSERT_TAIL(&tmp_queue, control, next);
4391f8829a4aSRandall Stewart 			m = control->data;
4392f8829a4aSRandall Stewart 			while (m) {
4393b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4394139bc87fSRandall Stewart 					sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
439580fefe0aSRandall Stewart 				}
4396f8829a4aSRandall Stewart 				sctp_sbfree(control, stcb, &old_so->so_rcv, m);
4397b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4398f8829a4aSRandall Stewart 					sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
439980fefe0aSRandall Stewart 				}
4400139bc87fSRandall Stewart 				m = SCTP_BUF_NEXT(m);
4401f8829a4aSRandall Stewart 			}
4402f8829a4aSRandall Stewart 		}
4403f8829a4aSRandall Stewart 	}
4404f8829a4aSRandall Stewart 	SCTP_INP_READ_UNLOCK(old_inp);
4405f8829a4aSRandall Stewart 	/* Remove the sb-lock on the old socket */
4406f8829a4aSRandall Stewart 
4407f8829a4aSRandall Stewart 	sbunlock(&old_so->so_rcv);
4408f8829a4aSRandall Stewart 	/* Now we move them over to the new socket buffer */
4409f8829a4aSRandall Stewart 	SCTP_INP_READ_LOCK(new_inp);
44104a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(control, &tmp_queue, next, nctl) {
4411f8829a4aSRandall Stewart 		TAILQ_INSERT_TAIL(&new_inp->read_queue, control, next);
4412f8829a4aSRandall Stewart 		m = control->data;
4413f8829a4aSRandall Stewart 		while (m) {
4414b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4415139bc87fSRandall Stewart 				sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
441680fefe0aSRandall Stewart 			}
4417f8829a4aSRandall Stewart 			sctp_sballoc(stcb, &new_so->so_rcv, m);
4418b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4419f8829a4aSRandall Stewart 				sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
442080fefe0aSRandall Stewart 			}
4421139bc87fSRandall Stewart 			m = SCTP_BUF_NEXT(m);
4422f8829a4aSRandall Stewart 		}
4423f8829a4aSRandall Stewart 	}
4424f8829a4aSRandall Stewart 	SCTP_INP_READ_UNLOCK(new_inp);
4425f8829a4aSRandall Stewart }
4426f8829a4aSRandall Stewart 
4427f8829a4aSRandall Stewart void
4428f8829a4aSRandall Stewart sctp_add_to_readq(struct sctp_inpcb *inp,
4429f8829a4aSRandall Stewart     struct sctp_tcb *stcb,
4430f8829a4aSRandall Stewart     struct sctp_queued_to_read *control,
4431f8829a4aSRandall Stewart     struct sockbuf *sb,
4432ceaad40aSRandall Stewart     int end,
4433cfde3ff7SRandall Stewart     int inp_read_lock_held,
4434ceaad40aSRandall Stewart     int so_locked
4435ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4436ceaad40aSRandall Stewart     SCTP_UNUSED
4437ceaad40aSRandall Stewart #endif
4438ceaad40aSRandall Stewart )
4439f8829a4aSRandall Stewart {
4440f8829a4aSRandall Stewart 	/*
4441f8829a4aSRandall Stewart 	 * Here we must place the control on the end of the socket read
44424e88d37aSMichael Tuexen 	 * queue AND increment sb_cc so that select will work properly on
4443f8829a4aSRandall Stewart 	 * read.
4444f8829a4aSRandall Stewart 	 */
4445f8829a4aSRandall Stewart 	struct mbuf *m, *prev = NULL;
4446f8829a4aSRandall Stewart 
444703b0b021SRandall Stewart 	if (inp == NULL) {
444803b0b021SRandall Stewart 		/* Gak, TSNH!! */
4449a5d547adSRandall Stewart #ifdef INVARIANTS
445003b0b021SRandall Stewart 		panic("Gak, inp NULL on add_to_readq");
445103b0b021SRandall Stewart #endif
445203b0b021SRandall Stewart 		return;
445303b0b021SRandall Stewart 	}
4454cfde3ff7SRandall Stewart 	if (inp_read_lock_held == 0)
4455f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(inp);
4456cd1386abSMichael Tuexen 	if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
4457cd1386abSMichael Tuexen 		sctp_free_remote_addr(control->whoFrom);
4458cd1386abSMichael Tuexen 		if (control->data) {
4459cd1386abSMichael Tuexen 			sctp_m_freem(control->data);
4460cd1386abSMichael Tuexen 			control->data = NULL;
4461cd1386abSMichael Tuexen 		}
4462cd1386abSMichael Tuexen 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), control);
4463cd1386abSMichael Tuexen 		if (inp_read_lock_held == 0)
4464cd1386abSMichael Tuexen 			SCTP_INP_READ_UNLOCK(inp);
4465cd1386abSMichael Tuexen 		return;
4466cd1386abSMichael Tuexen 	}
446742551e99SRandall Stewart 	if (!(control->spec_flags & M_NOTIFICATION)) {
4468a5d547adSRandall Stewart 		atomic_add_int(&inp->total_recvs, 1);
446942551e99SRandall Stewart 		if (!control->do_not_ref_stcb) {
4470a5d547adSRandall Stewart 			atomic_add_int(&stcb->total_recvs, 1);
447142551e99SRandall Stewart 		}
447242551e99SRandall Stewart 	}
4473f8829a4aSRandall Stewart 	m = control->data;
4474f8829a4aSRandall Stewart 	control->held_length = 0;
4475f8829a4aSRandall Stewart 	control->length = 0;
4476f8829a4aSRandall Stewart 	while (m) {
4477139bc87fSRandall Stewart 		if (SCTP_BUF_LEN(m) == 0) {
4478f8829a4aSRandall Stewart 			/* Skip mbufs with NO length */
4479f8829a4aSRandall Stewart 			if (prev == NULL) {
4480f8829a4aSRandall Stewart 				/* First one */
4481f8829a4aSRandall Stewart 				control->data = sctp_m_free(m);
4482f8829a4aSRandall Stewart 				m = control->data;
4483f8829a4aSRandall Stewart 			} else {
4484139bc87fSRandall Stewart 				SCTP_BUF_NEXT(prev) = sctp_m_free(m);
4485139bc87fSRandall Stewart 				m = SCTP_BUF_NEXT(prev);
4486f8829a4aSRandall Stewart 			}
4487f8829a4aSRandall Stewart 			if (m == NULL) {
4488c2ede4b3SMartin Blapp 				control->tail_mbuf = prev;
4489f8829a4aSRandall Stewart 			}
4490f8829a4aSRandall Stewart 			continue;
4491f8829a4aSRandall Stewart 		}
4492f8829a4aSRandall Stewart 		prev = m;
4493b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4494139bc87fSRandall Stewart 			sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
449580fefe0aSRandall Stewart 		}
4496f8829a4aSRandall Stewart 		sctp_sballoc(stcb, sb, m);
4497b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4498f8829a4aSRandall Stewart 			sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
449980fefe0aSRandall Stewart 		}
4500139bc87fSRandall Stewart 		atomic_add_int(&control->length, SCTP_BUF_LEN(m));
4501139bc87fSRandall Stewart 		m = SCTP_BUF_NEXT(m);
4502f8829a4aSRandall Stewart 	}
4503f8829a4aSRandall Stewart 	if (prev != NULL) {
4504f8829a4aSRandall Stewart 		control->tail_mbuf = prev;
4505f8829a4aSRandall Stewart 	} else {
4506139bc87fSRandall Stewart 		/* Everything got collapsed out?? */
4507cd1386abSMichael Tuexen 		sctp_free_remote_addr(control->whoFrom);
4508cd1386abSMichael Tuexen 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), control);
4509cfde3ff7SRandall Stewart 		if (inp_read_lock_held == 0)
451047a490cbSMichael Tuexen 			SCTP_INP_READ_UNLOCK(inp);
4511f8829a4aSRandall Stewart 		return;
4512f8829a4aSRandall Stewart 	}
4513f8829a4aSRandall Stewart 	if (end) {
4514f8829a4aSRandall Stewart 		control->end_added = 1;
4515f8829a4aSRandall Stewart 	}
4516f8829a4aSRandall Stewart 	TAILQ_INSERT_TAIL(&inp->read_queue, control, next);
4517cfde3ff7SRandall Stewart 	if (inp_read_lock_held == 0)
4518f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
4519f8829a4aSRandall Stewart 	if (inp && inp->sctp_socket) {
452017205eccSRandall Stewart 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
452117205eccSRandall Stewart 			SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
4522ceaad40aSRandall Stewart 		} else {
4523ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4524ceaad40aSRandall Stewart 			struct socket *so;
4525ceaad40aSRandall Stewart 
4526ceaad40aSRandall Stewart 			so = SCTP_INP_SO(inp);
4527ceaad40aSRandall Stewart 			if (!so_locked) {
452860990c0cSMichael Tuexen 				if (stcb) {
4529ceaad40aSRandall Stewart 					atomic_add_int(&stcb->asoc.refcnt, 1);
4530ceaad40aSRandall Stewart 					SCTP_TCB_UNLOCK(stcb);
453160990c0cSMichael Tuexen 				}
4532ceaad40aSRandall Stewart 				SCTP_SOCKET_LOCK(so, 1);
453360990c0cSMichael Tuexen 				if (stcb) {
4534ceaad40aSRandall Stewart 					SCTP_TCB_LOCK(stcb);
4535ceaad40aSRandall Stewart 					atomic_subtract_int(&stcb->asoc.refcnt, 1);
453660990c0cSMichael Tuexen 				}
4537ceaad40aSRandall Stewart 				if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4538ceaad40aSRandall Stewart 					SCTP_SOCKET_UNLOCK(so, 1);
4539ceaad40aSRandall Stewart 					return;
4540ceaad40aSRandall Stewart 				}
4541ceaad40aSRandall Stewart 			}
4542ceaad40aSRandall Stewart #endif
4543f8829a4aSRandall Stewart 			sctp_sorwakeup(inp, inp->sctp_socket);
4544ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4545ceaad40aSRandall Stewart 			if (!so_locked) {
4546ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
4547ceaad40aSRandall Stewart 			}
4548ceaad40aSRandall Stewart #endif
4549ceaad40aSRandall Stewart 		}
4550f8829a4aSRandall Stewart 	}
4551f8829a4aSRandall Stewart }
4552f8829a4aSRandall Stewart 
4553f8829a4aSRandall Stewart 
4554f8829a4aSRandall Stewart int
4555f8829a4aSRandall Stewart sctp_append_to_readq(struct sctp_inpcb *inp,
4556f8829a4aSRandall Stewart     struct sctp_tcb *stcb,
4557f8829a4aSRandall Stewart     struct sctp_queued_to_read *control,
4558f8829a4aSRandall Stewart     struct mbuf *m,
4559f8829a4aSRandall Stewart     int end,
4560f8829a4aSRandall Stewart     int ctls_cumack,
4561f8829a4aSRandall Stewart     struct sockbuf *sb)
4562f8829a4aSRandall Stewart {
4563f8829a4aSRandall Stewart 	/*
4564f8829a4aSRandall Stewart 	 * A partial delivery API event is underway. OR we are appending on
4565f8829a4aSRandall Stewart 	 * the reassembly queue.
4566f8829a4aSRandall Stewart 	 *
4567f8829a4aSRandall Stewart 	 * If PDAPI this means we need to add m to the end of the data.
45684e88d37aSMichael Tuexen 	 * Increase the length in the control AND increment the sb_cc.
4569f8829a4aSRandall Stewart 	 * Otherwise sb is NULL and all we need to do is put it at the end
4570f8829a4aSRandall Stewart 	 * of the mbuf chain.
4571f8829a4aSRandall Stewart 	 */
4572f8829a4aSRandall Stewart 	int len = 0;
4573f8829a4aSRandall Stewart 	struct mbuf *mm, *tail = NULL, *prev = NULL;
4574f8829a4aSRandall Stewart 
4575f8829a4aSRandall Stewart 	if (inp) {
4576f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(inp);
4577f8829a4aSRandall Stewart 	}
4578f8829a4aSRandall Stewart 	if (control == NULL) {
4579f8829a4aSRandall Stewart get_out:
4580f8829a4aSRandall Stewart 		if (inp) {
4581f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
4582f8829a4aSRandall Stewart 		}
4583f8829a4aSRandall Stewart 		return (-1);
4584f8829a4aSRandall Stewart 	}
4585cd1386abSMichael Tuexen 	if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ)) {
4586cd1386abSMichael Tuexen 		SCTP_INP_READ_UNLOCK(inp);
458760990c0cSMichael Tuexen 		return (0);
4588cd1386abSMichael Tuexen 	}
4589139bc87fSRandall Stewart 	if (control->end_added) {
4590f8829a4aSRandall Stewart 		/* huh this one is complete? */
4591f8829a4aSRandall Stewart 		goto get_out;
4592f8829a4aSRandall Stewart 	}
4593f8829a4aSRandall Stewart 	mm = m;
4594f8829a4aSRandall Stewart 	if (mm == NULL) {
4595f8829a4aSRandall Stewart 		goto get_out;
4596f8829a4aSRandall Stewart 	}
4597f8829a4aSRandall Stewart 	while (mm) {
4598139bc87fSRandall Stewart 		if (SCTP_BUF_LEN(mm) == 0) {
4599f8829a4aSRandall Stewart 			/* Skip mbufs with NO lenght */
4600f8829a4aSRandall Stewart 			if (prev == NULL) {
4601f8829a4aSRandall Stewart 				/* First one */
4602f8829a4aSRandall Stewart 				m = sctp_m_free(mm);
4603f8829a4aSRandall Stewart 				mm = m;
4604f8829a4aSRandall Stewart 			} else {
4605139bc87fSRandall Stewart 				SCTP_BUF_NEXT(prev) = sctp_m_free(mm);
4606139bc87fSRandall Stewart 				mm = SCTP_BUF_NEXT(prev);
4607f8829a4aSRandall Stewart 			}
4608f8829a4aSRandall Stewart 			continue;
4609f8829a4aSRandall Stewart 		}
4610f8829a4aSRandall Stewart 		prev = mm;
4611139bc87fSRandall Stewart 		len += SCTP_BUF_LEN(mm);
4612f8829a4aSRandall Stewart 		if (sb) {
4613b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4614139bc87fSRandall Stewart 				sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(mm));
461580fefe0aSRandall Stewart 			}
4616f8829a4aSRandall Stewart 			sctp_sballoc(stcb, sb, mm);
4617b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4618f8829a4aSRandall Stewart 				sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
461980fefe0aSRandall Stewart 			}
4620f8829a4aSRandall Stewart 		}
4621139bc87fSRandall Stewart 		mm = SCTP_BUF_NEXT(mm);
4622f8829a4aSRandall Stewart 	}
4623f8829a4aSRandall Stewart 	if (prev) {
4624f8829a4aSRandall Stewart 		tail = prev;
4625f8829a4aSRandall Stewart 	} else {
4626f8829a4aSRandall Stewart 		/* Really there should always be a prev */
4627f8829a4aSRandall Stewart 		if (m == NULL) {
4628f8829a4aSRandall Stewart 			/* Huh nothing left? */
4629a5d547adSRandall Stewart #ifdef INVARIANTS
4630f8829a4aSRandall Stewart 			panic("Nothing left to add?");
4631f8829a4aSRandall Stewart #else
4632f8829a4aSRandall Stewart 			goto get_out;
4633f8829a4aSRandall Stewart #endif
4634f8829a4aSRandall Stewart 		}
4635f8829a4aSRandall Stewart 		tail = m;
4636f8829a4aSRandall Stewart 	}
4637f8829a4aSRandall Stewart 	if (control->tail_mbuf) {
4638f8829a4aSRandall Stewart 		/* append */
4639139bc87fSRandall Stewart 		SCTP_BUF_NEXT(control->tail_mbuf) = m;
4640f8829a4aSRandall Stewart 		control->tail_mbuf = tail;
4641f8829a4aSRandall Stewart 	} else {
4642f8829a4aSRandall Stewart 		/* nothing there */
4643a5d547adSRandall Stewart #ifdef INVARIANTS
4644f8829a4aSRandall Stewart 		if (control->data != NULL) {
4645f8829a4aSRandall Stewart 			panic("This should NOT happen");
4646f8829a4aSRandall Stewart 		}
4647f8829a4aSRandall Stewart #endif
4648f8829a4aSRandall Stewart 		control->data = m;
4649f8829a4aSRandall Stewart 		control->tail_mbuf = tail;
4650f8829a4aSRandall Stewart 	}
465118e198d3SRandall Stewart 	atomic_add_int(&control->length, len);
465218e198d3SRandall Stewart 	if (end) {
465318e198d3SRandall Stewart 		/* message is complete */
465418e198d3SRandall Stewart 		if (stcb && (control == stcb->asoc.control_pdapi)) {
465518e198d3SRandall Stewart 			stcb->asoc.control_pdapi = NULL;
465618e198d3SRandall Stewart 		}
465718e198d3SRandall Stewart 		control->held_length = 0;
465818e198d3SRandall Stewart 		control->end_added = 1;
465918e198d3SRandall Stewart 	}
4660ad81507eSRandall Stewart 	if (stcb == NULL) {
4661ad81507eSRandall Stewart 		control->do_not_ref_stcb = 1;
4662ad81507eSRandall Stewart 	}
4663f8829a4aSRandall Stewart 	/*
4664f8829a4aSRandall Stewart 	 * When we are appending in partial delivery, the cum-ack is used
4665f8829a4aSRandall Stewart 	 * for the actual pd-api highest tsn on this mbuf. The true cum-ack
4666f8829a4aSRandall Stewart 	 * is populated in the outbound sinfo structure from the true cumack
4667f8829a4aSRandall Stewart 	 * if the association exists...
4668f8829a4aSRandall Stewart 	 */
4669f8829a4aSRandall Stewart 	control->sinfo_tsn = control->sinfo_cumtsn = ctls_cumack;
4670f8829a4aSRandall Stewart 	if (inp) {
4671f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
4672f8829a4aSRandall Stewart 	}
4673f8829a4aSRandall Stewart 	if (inp && inp->sctp_socket) {
467417205eccSRandall Stewart 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
467517205eccSRandall Stewart 			SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
4676ceaad40aSRandall Stewart 		} else {
4677ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4678ceaad40aSRandall Stewart 			struct socket *so;
4679ceaad40aSRandall Stewart 
4680ceaad40aSRandall Stewart 			so = SCTP_INP_SO(inp);
468160990c0cSMichael Tuexen 			if (stcb) {
4682ceaad40aSRandall Stewart 				atomic_add_int(&stcb->asoc.refcnt, 1);
4683ceaad40aSRandall Stewart 				SCTP_TCB_UNLOCK(stcb);
468460990c0cSMichael Tuexen 			}
4685ceaad40aSRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
468660990c0cSMichael Tuexen 			if (stcb) {
4687ceaad40aSRandall Stewart 				SCTP_TCB_LOCK(stcb);
4688ceaad40aSRandall Stewart 				atomic_subtract_int(&stcb->asoc.refcnt, 1);
468960990c0cSMichael Tuexen 			}
4690ceaad40aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4691ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
4692ceaad40aSRandall Stewart 				return (0);
4693ceaad40aSRandall Stewart 			}
4694ceaad40aSRandall Stewart #endif
4695f8829a4aSRandall Stewart 			sctp_sorwakeup(inp, inp->sctp_socket);
4696ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4697ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
4698ceaad40aSRandall Stewart #endif
4699ceaad40aSRandall Stewart 		}
4700f8829a4aSRandall Stewart 	}
4701f8829a4aSRandall Stewart 	return (0);
4702f8829a4aSRandall Stewart }
4703f8829a4aSRandall Stewart 
4704f8829a4aSRandall Stewart 
4705f8829a4aSRandall Stewart 
4706f8829a4aSRandall Stewart /*************HOLD THIS COMMENT FOR PATCH FILE OF
4707f8829a4aSRandall Stewart  *************ALTERNATE ROUTING CODE
4708f8829a4aSRandall Stewart  */
4709f8829a4aSRandall Stewart 
4710f8829a4aSRandall Stewart /*************HOLD THIS COMMENT FOR END OF PATCH FILE OF
4711f8829a4aSRandall Stewart  *************ALTERNATE ROUTING CODE
4712f8829a4aSRandall Stewart  */
4713f8829a4aSRandall Stewart 
4714f8829a4aSRandall Stewart struct mbuf *
4715ff1ffd74SMichael Tuexen sctp_generate_cause(uint16_t code, char *info)
4716f8829a4aSRandall Stewart {
4717f8829a4aSRandall Stewart 	struct mbuf *m;
4718ff1ffd74SMichael Tuexen 	struct sctp_gen_error_cause *cause;
4719ff1ffd74SMichael Tuexen 	size_t info_len, len;
4720f8829a4aSRandall Stewart 
4721ff1ffd74SMichael Tuexen 	if ((code == 0) || (info == NULL)) {
4722ff1ffd74SMichael Tuexen 		return (NULL);
4723ff1ffd74SMichael Tuexen 	}
4724ff1ffd74SMichael Tuexen 	info_len = strlen(info);
4725ff1ffd74SMichael Tuexen 	len = sizeof(struct sctp_paramhdr) + info_len;
4726ff1ffd74SMichael Tuexen 	m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
4727ff1ffd74SMichael Tuexen 	if (m != NULL) {
4728ff1ffd74SMichael Tuexen 		SCTP_BUF_LEN(m) = len;
4729ff1ffd74SMichael Tuexen 		cause = mtod(m, struct sctp_gen_error_cause *);
4730ff1ffd74SMichael Tuexen 		cause->code = htons(code);
4731ff1ffd74SMichael Tuexen 		cause->length = htons((uint16_t) len);
4732ff1ffd74SMichael Tuexen 		memcpy(cause->info, info, info_len);
4733f8829a4aSRandall Stewart 	}
4734f8829a4aSRandall Stewart 	return (m);
4735f8829a4aSRandall Stewart }
4736f8829a4aSRandall Stewart 
473732451da4SMichael Tuexen struct mbuf *
473832451da4SMichael Tuexen sctp_generate_no_user_data_cause(uint32_t tsn)
473932451da4SMichael Tuexen {
474032451da4SMichael Tuexen 	struct mbuf *m;
474132451da4SMichael Tuexen 	struct sctp_error_no_user_data *no_user_data_cause;
474232451da4SMichael Tuexen 	size_t len;
474332451da4SMichael Tuexen 
474432451da4SMichael Tuexen 	len = sizeof(struct sctp_error_no_user_data);
474532451da4SMichael Tuexen 	m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
474632451da4SMichael Tuexen 	if (m != NULL) {
474732451da4SMichael Tuexen 		SCTP_BUF_LEN(m) = len;
474832451da4SMichael Tuexen 		no_user_data_cause = mtod(m, struct sctp_error_no_user_data *);
474932451da4SMichael Tuexen 		no_user_data_cause->cause.code = htons(SCTP_CAUSE_NO_USER_DATA);
475032451da4SMichael Tuexen 		no_user_data_cause->cause.length = htons((uint16_t) len);
475132451da4SMichael Tuexen 		no_user_data_cause->tsn = tsn;	/* tsn is passed in as NBO */
475232451da4SMichael Tuexen 	}
475332451da4SMichael Tuexen 	return (m);
475432451da4SMichael Tuexen }
475532451da4SMichael Tuexen 
4756f8829a4aSRandall Stewart #ifdef SCTP_MBCNT_LOGGING
4757f8829a4aSRandall Stewart void
4758f8829a4aSRandall Stewart sctp_free_bufspace(struct sctp_tcb *stcb, struct sctp_association *asoc,
4759f8829a4aSRandall Stewart     struct sctp_tmit_chunk *tp1, int chk_cnt)
4760f8829a4aSRandall Stewart {
4761f8829a4aSRandall Stewart 	if (tp1->data == NULL) {
4762f8829a4aSRandall Stewart 		return;
4763f8829a4aSRandall Stewart 	}
4764f8829a4aSRandall Stewart 	asoc->chunks_on_out_queue -= chk_cnt;
4765b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBCNT_LOGGING_ENABLE) {
4766f8829a4aSRandall Stewart 		sctp_log_mbcnt(SCTP_LOG_MBCNT_DECREASE,
4767f8829a4aSRandall Stewart 		    asoc->total_output_queue_size,
4768f8829a4aSRandall Stewart 		    tp1->book_size,
4769f8829a4aSRandall Stewart 		    0,
4770f8829a4aSRandall Stewart 		    tp1->mbcnt);
477180fefe0aSRandall Stewart 	}
4772f8829a4aSRandall Stewart 	if (asoc->total_output_queue_size >= tp1->book_size) {
477344b7479bSRandall Stewart 		atomic_add_int(&asoc->total_output_queue_size, -tp1->book_size);
4774f8829a4aSRandall Stewart 	} else {
4775f8829a4aSRandall Stewart 		asoc->total_output_queue_size = 0;
4776f8829a4aSRandall Stewart 	}
4777f8829a4aSRandall Stewart 
4778f8829a4aSRandall Stewart 	if (stcb->sctp_socket && (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) ||
4779f8829a4aSRandall Stewart 	    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) {
47804e88d37aSMichael Tuexen 		if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) {
47814e88d37aSMichael Tuexen 			stcb->sctp_socket->so_snd.sb_cc -= tp1->book_size;
4782f8829a4aSRandall Stewart 		} else {
47834e88d37aSMichael Tuexen 			stcb->sctp_socket->so_snd.sb_cc = 0;
4784f8829a4aSRandall Stewart 
4785f8829a4aSRandall Stewart 		}
4786f8829a4aSRandall Stewart 	}
4787f8829a4aSRandall Stewart }
4788f8829a4aSRandall Stewart 
4789f8829a4aSRandall Stewart #endif
4790f8829a4aSRandall Stewart 
4791f8829a4aSRandall Stewart int
4792f8829a4aSRandall Stewart sctp_release_pr_sctp_chunk(struct sctp_tcb *stcb, struct sctp_tmit_chunk *tp1,
47931edc9dbaSMichael Tuexen     uint8_t sent, int so_locked
4794ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4795ceaad40aSRandall Stewart     SCTP_UNUSED
4796ceaad40aSRandall Stewart #endif
4797ceaad40aSRandall Stewart )
4798f8829a4aSRandall Stewart {
47990c0982b8SRandall Stewart 	struct sctp_stream_out *strq;
48004a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk = NULL, *tp2;
48010c0982b8SRandall Stewart 	struct sctp_stream_queue_pending *sp;
48020c0982b8SRandall Stewart 	uint16_t stream = 0, seq = 0;
48030c0982b8SRandall Stewart 	uint8_t foundeom = 0;
4804f8829a4aSRandall Stewart 	int ret_sz = 0;
4805f8829a4aSRandall Stewart 	int notdone;
48060c0982b8SRandall Stewart 	int do_wakeup_routine = 0;
4807f8829a4aSRandall Stewart 
48080c0982b8SRandall Stewart 	stream = tp1->rec.data.stream_number;
48090c0982b8SRandall Stewart 	seq = tp1->rec.data.stream_seq;
4810f0396ad1SMichael Tuexen 	if (sent || !(tp1->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG)) {
4811f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_sent[0]++;
4812f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
4813f0396ad1SMichael Tuexen 		stcb->asoc.strmout[stream].abandoned_sent[0]++;
4814f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
4815f0396ad1SMichael Tuexen 		stcb->asoc.strmout[stream].abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
4816f0396ad1SMichael Tuexen #endif
4817f0396ad1SMichael Tuexen 	} else {
4818f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_unsent[0]++;
4819f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
4820f0396ad1SMichael Tuexen 		stcb->asoc.strmout[stream].abandoned_unsent[0]++;
4821f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
4822f0396ad1SMichael Tuexen 		stcb->asoc.strmout[stream].abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
4823f0396ad1SMichael Tuexen #endif
4824f0396ad1SMichael Tuexen 	}
4825f8829a4aSRandall Stewart 	do {
4826f8829a4aSRandall Stewart 		ret_sz += tp1->book_size;
48270c0982b8SRandall Stewart 		if (tp1->data != NULL) {
48288933fa13SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4829830d754dSRandall Stewart 				sctp_flight_size_decrease(tp1);
4830830d754dSRandall Stewart 				sctp_total_flight_decrease(stcb, tp1);
48318933fa13SRandall Stewart 			}
48328933fa13SRandall Stewart 			sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
48330c0982b8SRandall Stewart 			stcb->asoc.peers_rwnd += tp1->send_size;
48340c0982b8SRandall Stewart 			stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
48351edc9dbaSMichael Tuexen 			if (sent) {
48361edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
48371edc9dbaSMichael Tuexen 			} else {
48381edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
48391edc9dbaSMichael Tuexen 			}
48402f99457bSMichael Tuexen 			if (tp1->data) {
4841f8829a4aSRandall Stewart 				sctp_m_freem(tp1->data);
4842f8829a4aSRandall Stewart 				tp1->data = NULL;
48432f99457bSMichael Tuexen 			}
48440c0982b8SRandall Stewart 			do_wakeup_routine = 1;
4845f8829a4aSRandall Stewart 			if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
4846f8829a4aSRandall Stewart 				stcb->asoc.sent_queue_cnt_removeable--;
4847f8829a4aSRandall Stewart 			}
4848f8829a4aSRandall Stewart 		}
48498933fa13SRandall Stewart 		tp1->sent = SCTP_FORWARD_TSN_SKIP;
4850f8829a4aSRandall Stewart 		if ((tp1->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) ==
4851f8829a4aSRandall Stewart 		    SCTP_DATA_NOT_FRAG) {
4852f8829a4aSRandall Stewart 			/* not frag'ed we ae done   */
4853f8829a4aSRandall Stewart 			notdone = 0;
4854f8829a4aSRandall Stewart 			foundeom = 1;
4855f8829a4aSRandall Stewart 		} else if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
4856f8829a4aSRandall Stewart 			/* end of frag, we are done */
4857f8829a4aSRandall Stewart 			notdone = 0;
4858f8829a4aSRandall Stewart 			foundeom = 1;
4859f8829a4aSRandall Stewart 		} else {
4860f8829a4aSRandall Stewart 			/*
4861f8829a4aSRandall Stewart 			 * Its a begin or middle piece, we must mark all of
4862f8829a4aSRandall Stewart 			 * it
4863f8829a4aSRandall Stewart 			 */
4864f8829a4aSRandall Stewart 			notdone = 1;
4865f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
4866f8829a4aSRandall Stewart 		}
4867f8829a4aSRandall Stewart 	} while (tp1 && notdone);
48680c0982b8SRandall Stewart 	if (foundeom == 0) {
4869f8829a4aSRandall Stewart 		/*
4870f8829a4aSRandall Stewart 		 * The multi-part message was scattered across the send and
4871f8829a4aSRandall Stewart 		 * sent queue.
4872f8829a4aSRandall Stewart 		 */
48734a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(tp1, &stcb->asoc.send_queue, sctp_next, tp2) {
48744a9ef3f8SMichael Tuexen 			if ((tp1->rec.data.stream_number != stream) ||
48754a9ef3f8SMichael Tuexen 			    (tp1->rec.data.stream_seq != seq)) {
48764a9ef3f8SMichael Tuexen 				break;
48774a9ef3f8SMichael Tuexen 			}
48780c0982b8SRandall Stewart 			/*
48790c0982b8SRandall Stewart 			 * save to chk in case we have some on stream out
48800c0982b8SRandall Stewart 			 * queue. If so and we have an un-transmitted one we
48810c0982b8SRandall Stewart 			 * don't have to fudge the TSN.
48820c0982b8SRandall Stewart 			 */
48830c0982b8SRandall Stewart 			chk = tp1;
48840c0982b8SRandall Stewart 			ret_sz += tp1->book_size;
48850c0982b8SRandall Stewart 			sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
48861edc9dbaSMichael Tuexen 			if (sent) {
48871edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
48881edc9dbaSMichael Tuexen 			} else {
48891edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
48901edc9dbaSMichael Tuexen 			}
48912f99457bSMichael Tuexen 			if (tp1->data) {
48920c0982b8SRandall Stewart 				sctp_m_freem(tp1->data);
48932f99457bSMichael Tuexen 				tp1->data = NULL;
48942f99457bSMichael Tuexen 			}
48958933fa13SRandall Stewart 			/* No flight involved here book the size to 0 */
48968933fa13SRandall Stewart 			tp1->book_size = 0;
48970c0982b8SRandall Stewart 			if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
48980c0982b8SRandall Stewart 				foundeom = 1;
4899f8829a4aSRandall Stewart 			}
49000c0982b8SRandall Stewart 			do_wakeup_routine = 1;
49010c0982b8SRandall Stewart 			tp1->sent = SCTP_FORWARD_TSN_SKIP;
49020c0982b8SRandall Stewart 			TAILQ_REMOVE(&stcb->asoc.send_queue, tp1, sctp_next);
49030c0982b8SRandall Stewart 			/*
49040c0982b8SRandall Stewart 			 * on to the sent queue so we can wait for it to be
49050c0982b8SRandall Stewart 			 * passed by.
49060c0982b8SRandall Stewart 			 */
49070c0982b8SRandall Stewart 			TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, tp1,
49080c0982b8SRandall Stewart 			    sctp_next);
49090c0982b8SRandall Stewart 			stcb->asoc.send_queue_cnt--;
49100c0982b8SRandall Stewart 			stcb->asoc.sent_queue_cnt++;
49110c0982b8SRandall Stewart 		}
49120c0982b8SRandall Stewart 	}
49130c0982b8SRandall Stewart 	if (foundeom == 0) {
49140c0982b8SRandall Stewart 		/*
49150c0982b8SRandall Stewart 		 * Still no eom found. That means there is stuff left on the
49160c0982b8SRandall Stewart 		 * stream out queue.. yuck.
49170c0982b8SRandall Stewart 		 */
49180c0982b8SRandall Stewart 		SCTP_TCB_SEND_LOCK(stcb);
4919f3b05218SMichael Tuexen 		strq = &stcb->asoc.strmout[stream];
4920f3b05218SMichael Tuexen 		sp = TAILQ_FIRST(&strq->outqueue);
4921f3b05218SMichael Tuexen 		if (sp != NULL) {
49220c0982b8SRandall Stewart 			sp->discard_rest = 1;
49230c0982b8SRandall Stewart 			/*
4924f3b05218SMichael Tuexen 			 * We may need to put a chunk on the queue that
4925f3b05218SMichael Tuexen 			 * holds the TSN that would have been sent with the
4926f3b05218SMichael Tuexen 			 * LAST bit.
49270c0982b8SRandall Stewart 			 */
49280c0982b8SRandall Stewart 			if (chk == NULL) {
49290c0982b8SRandall Stewart 				/* Yep, we have to */
49300c0982b8SRandall Stewart 				sctp_alloc_a_chunk(stcb, chk);
49310c0982b8SRandall Stewart 				if (chk == NULL) {
49320c0982b8SRandall Stewart 					/*
4933f3b05218SMichael Tuexen 					 * we are hosed. All we can do is
4934f3b05218SMichael Tuexen 					 * nothing.. which will cause an
4935f3b05218SMichael Tuexen 					 * abort if the peer is paying
49360c0982b8SRandall Stewart 					 * attention.
49370c0982b8SRandall Stewart 					 */
49380c0982b8SRandall Stewart 					goto oh_well;
49390c0982b8SRandall Stewart 				}
49400c0982b8SRandall Stewart 				memset(chk, 0, sizeof(*chk));
49410c0982b8SRandall Stewart 				chk->rec.data.rcv_flags = SCTP_DATA_LAST_FRAG;
49420c0982b8SRandall Stewart 				chk->sent = SCTP_FORWARD_TSN_SKIP;
49430c0982b8SRandall Stewart 				chk->asoc = &stcb->asoc;
4944f3b05218SMichael Tuexen 				chk->rec.data.stream_seq = strq->next_sequence_send;
49450c0982b8SRandall Stewart 				chk->rec.data.stream_number = sp->stream;
49460c0982b8SRandall Stewart 				chk->rec.data.payloadtype = sp->ppid;
49470c0982b8SRandall Stewart 				chk->rec.data.context = sp->context;
49480c0982b8SRandall Stewart 				chk->flags = sp->act_flags;
49497fd5b436SMichael Tuexen 				chk->whoTo = NULL;
49500c0982b8SRandall Stewart 				chk->rec.data.TSN_seq = atomic_fetchadd_int(&stcb->asoc.sending_seq, 1);
49517fd5b436SMichael Tuexen 				strq->chunks_on_queues++;
49520c0982b8SRandall Stewart 				TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, chk, sctp_next);
49530c0982b8SRandall Stewart 				stcb->asoc.sent_queue_cnt++;
49548933fa13SRandall Stewart 				stcb->asoc.pr_sctp_cnt++;
49550c0982b8SRandall Stewart 			} else {
49560c0982b8SRandall Stewart 				chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
49570c0982b8SRandall Stewart 			}
4958f3b05218SMichael Tuexen 			strq->next_sequence_send++;
49590c0982b8SRandall Stewart 	oh_well:
49600c0982b8SRandall Stewart 			if (sp->data) {
49610c0982b8SRandall Stewart 				/*
4962f3b05218SMichael Tuexen 				 * Pull any data to free up the SB and allow
4963f3b05218SMichael Tuexen 				 * sender to "add more" while we will throw
4964f3b05218SMichael Tuexen 				 * away :-)
49650c0982b8SRandall Stewart 				 */
4966f3b05218SMichael Tuexen 				sctp_free_spbufspace(stcb, &stcb->asoc, sp);
49670c0982b8SRandall Stewart 				ret_sz += sp->length;
49680c0982b8SRandall Stewart 				do_wakeup_routine = 1;
49690c0982b8SRandall Stewart 				sp->some_taken = 1;
49700c0982b8SRandall Stewart 				sctp_m_freem(sp->data);
49710c0982b8SRandall Stewart 				sp->data = NULL;
49720c0982b8SRandall Stewart 				sp->tail_mbuf = NULL;
4973d07b2ac6SMichael Tuexen 				sp->length = 0;
49740c0982b8SRandall Stewart 			}
49750c0982b8SRandall Stewart 		}
49760c0982b8SRandall Stewart 		SCTP_TCB_SEND_UNLOCK(stcb);
49770c0982b8SRandall Stewart 	}
49780c0982b8SRandall Stewart 	if (do_wakeup_routine) {
49790c0982b8SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
49808933fa13SRandall Stewart 		struct socket *so;
49818933fa13SRandall Stewart 
49820c0982b8SRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
49830c0982b8SRandall Stewart 		if (!so_locked) {
49840c0982b8SRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, 1);
49850c0982b8SRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
49860c0982b8SRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
49870c0982b8SRandall Stewart 			SCTP_TCB_LOCK(stcb);
49880c0982b8SRandall Stewart 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
49890c0982b8SRandall Stewart 			if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
49900c0982b8SRandall Stewart 				/* assoc was freed while we were unlocked */
49910c0982b8SRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
49920c0982b8SRandall Stewart 				return (ret_sz);
49930c0982b8SRandall Stewart 			}
49940c0982b8SRandall Stewart 		}
49950c0982b8SRandall Stewart #endif
49960c0982b8SRandall Stewart 		sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
49970c0982b8SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
49980c0982b8SRandall Stewart 		if (!so_locked) {
49990c0982b8SRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
50000c0982b8SRandall Stewart 		}
50010c0982b8SRandall Stewart #endif
5002f8829a4aSRandall Stewart 	}
5003f8829a4aSRandall Stewart 	return (ret_sz);
5004f8829a4aSRandall Stewart }
5005f8829a4aSRandall Stewart 
5006f8829a4aSRandall Stewart /*
5007f8829a4aSRandall Stewart  * checks to see if the given address, sa, is one that is currently known by
5008f8829a4aSRandall Stewart  * the kernel note: can't distinguish the same address on multiple interfaces
5009f8829a4aSRandall Stewart  * and doesn't handle multiple addresses with different zone/scope id's note:
5010f8829a4aSRandall Stewart  * ifa_ifwithaddr() compares the entire sockaddr struct
5011f8829a4aSRandall Stewart  */
501242551e99SRandall Stewart struct sctp_ifa *
501380fefe0aSRandall Stewart sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr,
501480fefe0aSRandall Stewart     int holds_lock)
5015f8829a4aSRandall Stewart {
501642551e99SRandall Stewart 	struct sctp_laddr *laddr;
5017f8829a4aSRandall Stewart 
5018ad81507eSRandall Stewart 	if (holds_lock == 0) {
501942551e99SRandall Stewart 		SCTP_INP_RLOCK(inp);
5020ad81507eSRandall Stewart 	}
502142551e99SRandall Stewart 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
502242551e99SRandall Stewart 		if (laddr->ifa == NULL)
5023f8829a4aSRandall Stewart 			continue;
502442551e99SRandall Stewart 		if (addr->sa_family != laddr->ifa->address.sa.sa_family)
502542551e99SRandall Stewart 			continue;
5026e6194c2eSMichael Tuexen #ifdef INET
502742551e99SRandall Stewart 		if (addr->sa_family == AF_INET) {
502842551e99SRandall Stewart 			if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
502942551e99SRandall Stewart 			    laddr->ifa->address.sin.sin_addr.s_addr) {
503042551e99SRandall Stewart 				/* found him. */
5031ad81507eSRandall Stewart 				if (holds_lock == 0) {
503242551e99SRandall Stewart 					SCTP_INP_RUNLOCK(inp);
5033ad81507eSRandall Stewart 				}
503442551e99SRandall Stewart 				return (laddr->ifa);
503542551e99SRandall Stewart 				break;
503642551e99SRandall Stewart 			}
50375e2c2d87SRandall Stewart 		}
5038e6194c2eSMichael Tuexen #endif
50395e2c2d87SRandall Stewart #ifdef INET6
50405e2c2d87SRandall Stewart 		if (addr->sa_family == AF_INET6) {
5041c54a18d2SRandall Stewart 			if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5042c54a18d2SRandall Stewart 			    &laddr->ifa->address.sin6)) {
504342551e99SRandall Stewart 				/* found him. */
5044ad81507eSRandall Stewart 				if (holds_lock == 0) {
504542551e99SRandall Stewart 					SCTP_INP_RUNLOCK(inp);
5046ad81507eSRandall Stewart 				}
504742551e99SRandall Stewart 				return (laddr->ifa);
504842551e99SRandall Stewart 				break;
504942551e99SRandall Stewart 			}
505042551e99SRandall Stewart 		}
50515e2c2d87SRandall Stewart #endif
505242551e99SRandall Stewart 	}
5053ad81507eSRandall Stewart 	if (holds_lock == 0) {
505442551e99SRandall Stewart 		SCTP_INP_RUNLOCK(inp);
5055ad81507eSRandall Stewart 	}
505642551e99SRandall Stewart 	return (NULL);
505742551e99SRandall Stewart }
5058f8829a4aSRandall Stewart 
50596a27c376SRandall Stewart uint32_t
50606a27c376SRandall Stewart sctp_get_ifa_hash_val(struct sockaddr *addr)
50616a27c376SRandall Stewart {
5062ea5eba11SMichael Tuexen 	switch (addr->sa_family) {
5063ea5eba11SMichael Tuexen #ifdef INET
5064ea5eba11SMichael Tuexen 	case AF_INET:
5065ea5eba11SMichael Tuexen 		{
50666a27c376SRandall Stewart 			struct sockaddr_in *sin;
50676a27c376SRandall Stewart 
50686a27c376SRandall Stewart 			sin = (struct sockaddr_in *)addr;
50696a27c376SRandall Stewart 			return (sin->sin_addr.s_addr ^ (sin->sin_addr.s_addr >> 16));
5070ea5eba11SMichael Tuexen 		}
5071ea5eba11SMichael Tuexen #endif
5072ea5eba11SMichael Tuexen #ifdef INET6
50732c2e3218SMichael Tuexen 	case AF_INET6:
5074ea5eba11SMichael Tuexen 		{
50756a27c376SRandall Stewart 			struct sockaddr_in6 *sin6;
50766a27c376SRandall Stewart 			uint32_t hash_of_addr;
50776a27c376SRandall Stewart 
50786a27c376SRandall Stewart 			sin6 = (struct sockaddr_in6 *)addr;
50796a27c376SRandall Stewart 			hash_of_addr = (sin6->sin6_addr.s6_addr32[0] +
50806a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[1] +
50816a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[2] +
50826a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[3]);
50836a27c376SRandall Stewart 			hash_of_addr = (hash_of_addr ^ (hash_of_addr >> 16));
50846a27c376SRandall Stewart 			return (hash_of_addr);
50856a27c376SRandall Stewart 		}
5086ea5eba11SMichael Tuexen #endif
5087ea5eba11SMichael Tuexen 	default:
5088ea5eba11SMichael Tuexen 		break;
5089ea5eba11SMichael Tuexen 	}
50906a27c376SRandall Stewart 	return (0);
50916a27c376SRandall Stewart }
50926a27c376SRandall Stewart 
509342551e99SRandall Stewart struct sctp_ifa *
509442551e99SRandall Stewart sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock)
509542551e99SRandall Stewart {
509642551e99SRandall Stewart 	struct sctp_ifa *sctp_ifap;
509742551e99SRandall Stewart 	struct sctp_vrf *vrf;
50986a27c376SRandall Stewart 	struct sctp_ifalist *hash_head;
50996a27c376SRandall Stewart 	uint32_t hash_of_addr;
510042551e99SRandall Stewart 
510142551e99SRandall Stewart 	if (holds_lock == 0)
5102c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RLOCK();
510342551e99SRandall Stewart 
5104bff64a4dSRandall Stewart 	vrf = sctp_find_vrf(vrf_id);
5105bff64a4dSRandall Stewart 	if (vrf == NULL) {
5106bff64a4dSRandall Stewart 		if (holds_lock == 0)
5107c99efcf6SRandall Stewart 			SCTP_IPI_ADDR_RUNLOCK();
5108bff64a4dSRandall Stewart 		return (NULL);
5109bff64a4dSRandall Stewart 	}
5110bff64a4dSRandall Stewart 	hash_of_addr = sctp_get_ifa_hash_val(addr);
5111bff64a4dSRandall Stewart 
511217205eccSRandall Stewart 	hash_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
5113bff64a4dSRandall Stewart 	if (hash_head == NULL) {
5114ad81507eSRandall Stewart 		SCTP_PRINTF("hash_of_addr:%x mask:%x table:%x - ",
5115c99efcf6SRandall Stewart 		    hash_of_addr, (uint32_t) vrf->vrf_addr_hashmark,
5116c99efcf6SRandall Stewart 		    (uint32_t) (hash_of_addr & vrf->vrf_addr_hashmark));
5117bff64a4dSRandall Stewart 		sctp_print_address(addr);
5118ad81507eSRandall Stewart 		SCTP_PRINTF("No such bucket for address\n");
5119bff64a4dSRandall Stewart 		if (holds_lock == 0)
5120c99efcf6SRandall Stewart 			SCTP_IPI_ADDR_RUNLOCK();
5121bff64a4dSRandall Stewart 
5122bff64a4dSRandall Stewart 		return (NULL);
5123bff64a4dSRandall Stewart 	}
51246a27c376SRandall Stewart 	LIST_FOREACH(sctp_ifap, hash_head, next_bucket) {
51256a27c376SRandall Stewart 		if (addr->sa_family != sctp_ifap->address.sa.sa_family)
51266a27c376SRandall Stewart 			continue;
5127e6194c2eSMichael Tuexen #ifdef INET
51286a27c376SRandall Stewart 		if (addr->sa_family == AF_INET) {
51296a27c376SRandall Stewart 			if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
51306a27c376SRandall Stewart 			    sctp_ifap->address.sin.sin_addr.s_addr) {
51316a27c376SRandall Stewart 				/* found him. */
513242551e99SRandall Stewart 				if (holds_lock == 0)
5133c99efcf6SRandall Stewart 					SCTP_IPI_ADDR_RUNLOCK();
513442551e99SRandall Stewart 				return (sctp_ifap);
51356a27c376SRandall Stewart 				break;
51366a27c376SRandall Stewart 			}
51375e2c2d87SRandall Stewart 		}
5138e6194c2eSMichael Tuexen #endif
51395e2c2d87SRandall Stewart #ifdef INET6
51405e2c2d87SRandall Stewart 		if (addr->sa_family == AF_INET6) {
5141c54a18d2SRandall Stewart 			if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5142c54a18d2SRandall Stewart 			    &sctp_ifap->address.sin6)) {
51436a27c376SRandall Stewart 				/* found him. */
51446a27c376SRandall Stewart 				if (holds_lock == 0)
5145c99efcf6SRandall Stewart 					SCTP_IPI_ADDR_RUNLOCK();
51466a27c376SRandall Stewart 				return (sctp_ifap);
51476a27c376SRandall Stewart 				break;
51486a27c376SRandall Stewart 			}
514942551e99SRandall Stewart 		}
51505e2c2d87SRandall Stewart #endif
515142551e99SRandall Stewart 	}
515242551e99SRandall Stewart 	if (holds_lock == 0)
5153c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RUNLOCK();
5154f8829a4aSRandall Stewart 	return (NULL);
5155f8829a4aSRandall Stewart }
5156f8829a4aSRandall Stewart 
5157f8829a4aSRandall Stewart static void
51584c9179adSRandall Stewart sctp_user_rcvd(struct sctp_tcb *stcb, uint32_t * freed_so_far, int hold_rlock,
5159f8829a4aSRandall Stewart     uint32_t rwnd_req)
5160f8829a4aSRandall Stewart {
5161f8829a4aSRandall Stewart 	/* User pulled some data, do we need a rwnd update? */
5162f8829a4aSRandall Stewart 	int r_unlocked = 0;
5163f8829a4aSRandall Stewart 	uint32_t dif, rwnd;
5164f8829a4aSRandall Stewart 	struct socket *so = NULL;
5165f8829a4aSRandall Stewart 
5166f8829a4aSRandall Stewart 	if (stcb == NULL)
5167f8829a4aSRandall Stewart 		return;
5168f8829a4aSRandall Stewart 
516950cec919SRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, 1);
5170f8829a4aSRandall Stewart 
517162c1ff9cSRandall Stewart 	if (stcb->asoc.state & (SCTP_STATE_ABOUT_TO_BE_FREED |
517262c1ff9cSRandall Stewart 	    SCTP_STATE_SHUTDOWN_RECEIVED |
51734c9179adSRandall Stewart 	    SCTP_STATE_SHUTDOWN_ACK_SENT)) {
5174f8829a4aSRandall Stewart 		/* Pre-check If we are freeing no update */
5175f8829a4aSRandall Stewart 		goto no_lock;
5176f8829a4aSRandall Stewart 	}
5177f8829a4aSRandall Stewart 	SCTP_INP_INCR_REF(stcb->sctp_ep);
5178f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5179f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5180f8829a4aSRandall Stewart 		goto out;
5181f8829a4aSRandall Stewart 	}
5182f8829a4aSRandall Stewart 	so = stcb->sctp_socket;
5183f8829a4aSRandall Stewart 	if (so == NULL) {
5184f8829a4aSRandall Stewart 		goto out;
5185f8829a4aSRandall Stewart 	}
5186f8829a4aSRandall Stewart 	atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far);
5187f8829a4aSRandall Stewart 	/* Have you have freed enough to look */
5188f8829a4aSRandall Stewart 	*freed_so_far = 0;
5189f8829a4aSRandall Stewart 	/* Yep, its worth a look and the lock overhead */
5190f8829a4aSRandall Stewart 
5191f8829a4aSRandall Stewart 	/* Figure out what the rwnd would be */
5192f8829a4aSRandall Stewart 	rwnd = sctp_calc_rwnd(stcb, &stcb->asoc);
5193f8829a4aSRandall Stewart 	if (rwnd >= stcb->asoc.my_last_reported_rwnd) {
5194f8829a4aSRandall Stewart 		dif = rwnd - stcb->asoc.my_last_reported_rwnd;
5195f8829a4aSRandall Stewart 	} else {
5196f8829a4aSRandall Stewart 		dif = 0;
5197f8829a4aSRandall Stewart 	}
5198f8829a4aSRandall Stewart 	if (dif >= rwnd_req) {
5199f8829a4aSRandall Stewart 		if (hold_rlock) {
5200f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5201f8829a4aSRandall Stewart 			r_unlocked = 1;
5202f8829a4aSRandall Stewart 		}
5203f8829a4aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5204f8829a4aSRandall Stewart 			/*
5205f8829a4aSRandall Stewart 			 * One last check before we allow the guy possibly
5206f8829a4aSRandall Stewart 			 * to get in. There is a race, where the guy has not
5207f8829a4aSRandall Stewart 			 * reached the gate. In that case
5208f8829a4aSRandall Stewart 			 */
5209f8829a4aSRandall Stewart 			goto out;
5210f8829a4aSRandall Stewart 		}
5211f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
5212f8829a4aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5213f8829a4aSRandall Stewart 			/* No reports here */
5214f8829a4aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
5215f8829a4aSRandall Stewart 			goto out;
5216f8829a4aSRandall Stewart 		}
5217f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_wu_sacks_sent);
5218689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_LOCKED);
5219830d754dSRandall Stewart 
5220f8829a4aSRandall Stewart 		sctp_chunk_output(stcb->sctp_ep, stcb,
5221ceaad40aSRandall Stewart 		    SCTP_OUTPUT_FROM_USR_RCVD, SCTP_SO_LOCKED);
5222f8829a4aSRandall Stewart 		/* make sure no timer is running */
5223ba785902SMichael Tuexen 		sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
5224ba785902SMichael Tuexen 		    SCTP_FROM_SCTPUTIL + SCTP_LOC_6);
5225f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
5226f8829a4aSRandall Stewart 	} else {
5227f8829a4aSRandall Stewart 		/* Update how much we have pending */
5228f8829a4aSRandall Stewart 		stcb->freed_by_sorcv_sincelast = dif;
5229f8829a4aSRandall Stewart 	}
5230f8829a4aSRandall Stewart out:
5231f8829a4aSRandall Stewart 	if (so && r_unlocked && hold_rlock) {
5232f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
5233f8829a4aSRandall Stewart 	}
5234f8829a4aSRandall Stewart 	SCTP_INP_DECR_REF(stcb->sctp_ep);
5235f8829a4aSRandall Stewart no_lock:
523650cec919SRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, -1);
5237f8829a4aSRandall Stewart 	return;
5238f8829a4aSRandall Stewart }
5239f8829a4aSRandall Stewart 
5240f8829a4aSRandall Stewart int
5241f8829a4aSRandall Stewart sctp_sorecvmsg(struct socket *so,
5242f8829a4aSRandall Stewart     struct uio *uio,
5243f8829a4aSRandall Stewart     struct mbuf **mp,
5244f8829a4aSRandall Stewart     struct sockaddr *from,
5245f8829a4aSRandall Stewart     int fromlen,
5246f8829a4aSRandall Stewart     int *msg_flags,
5247f8829a4aSRandall Stewart     struct sctp_sndrcvinfo *sinfo,
5248f8829a4aSRandall Stewart     int filling_sinfo)
5249f8829a4aSRandall Stewart {
5250f8829a4aSRandall Stewart 	/*
5251f8829a4aSRandall Stewart 	 * MSG flags we will look at MSG_DONTWAIT - non-blocking IO.
5252f8829a4aSRandall Stewart 	 * MSG_PEEK - Look don't touch :-D (only valid with OUT mbuf copy
5253f8829a4aSRandall Stewart 	 * mp=NULL thus uio is the copy method to userland) MSG_WAITALL - ??
5254f8829a4aSRandall Stewart 	 * On the way out we may send out any combination of:
5255f8829a4aSRandall Stewart 	 * MSG_NOTIFICATION MSG_EOR
5256f8829a4aSRandall Stewart 	 *
5257f8829a4aSRandall Stewart 	 */
5258f8829a4aSRandall Stewart 	struct sctp_inpcb *inp = NULL;
5259f8829a4aSRandall Stewart 	int my_len = 0;
5260f8829a4aSRandall Stewart 	int cp_len = 0, error = 0;
5261f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL;
526294b0d969SMichael Tuexen 	struct mbuf *m = NULL;
5263f8829a4aSRandall Stewart 	struct sctp_tcb *stcb = NULL;
5264f8829a4aSRandall Stewart 	int wakeup_read_socket = 0;
5265f8829a4aSRandall Stewart 	int freecnt_applied = 0;
5266f8829a4aSRandall Stewart 	int out_flags = 0, in_flags = 0;
5267f8829a4aSRandall Stewart 	int block_allowed = 1;
52684c9179adSRandall Stewart 	uint32_t freed_so_far = 0;
526981aca91aSRandall Stewart 	uint32_t copied_so_far = 0;
527093164cf9SRandall Stewart 	int in_eeor_mode = 0;
5271f8829a4aSRandall Stewart 	int no_rcv_needed = 0;
5272f8829a4aSRandall Stewart 	uint32_t rwnd_req = 0;
5273f8829a4aSRandall Stewart 	int hold_sblock = 0;
5274f8829a4aSRandall Stewart 	int hold_rlock = 0;
527542551e99SRandall Stewart 	int slen = 0;
52764c9179adSRandall Stewart 	uint32_t held_length = 0;
52777abab911SRobert Watson 	int sockbuf_lock = 0;
5278f8829a4aSRandall Stewart 
527917205eccSRandall Stewart 	if (uio == NULL) {
5280c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
528117205eccSRandall Stewart 		return (EINVAL);
528217205eccSRandall Stewart 	}
5283f8829a4aSRandall Stewart 	if (msg_flags) {
5284f8829a4aSRandall Stewart 		in_flags = *msg_flags;
5285c105859eSRandall Stewart 		if (in_flags & MSG_PEEK)
5286c105859eSRandall Stewart 			SCTP_STAT_INCR(sctps_read_peeks);
5287f8829a4aSRandall Stewart 	} else {
5288f8829a4aSRandall Stewart 		in_flags = 0;
5289f8829a4aSRandall Stewart 	}
5290f8829a4aSRandall Stewart 	slen = uio->uio_resid;
529117205eccSRandall Stewart 
5292f8829a4aSRandall Stewart 	/* Pull in and set up our int flags */
5293f8829a4aSRandall Stewart 	if (in_flags & MSG_OOB) {
5294f8829a4aSRandall Stewart 		/* Out of band's NOT supported */
5295f8829a4aSRandall Stewart 		return (EOPNOTSUPP);
5296f8829a4aSRandall Stewart 	}
5297f8829a4aSRandall Stewart 	if ((in_flags & MSG_PEEK) && (mp != NULL)) {
5298c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5299f8829a4aSRandall Stewart 		return (EINVAL);
5300f8829a4aSRandall Stewart 	}
5301f8829a4aSRandall Stewart 	if ((in_flags & (MSG_DONTWAIT
5302f8829a4aSRandall Stewart 	    | MSG_NBIO
5303f8829a4aSRandall Stewart 	    )) ||
530442551e99SRandall Stewart 	    SCTP_SO_IS_NBIO(so)) {
5305f8829a4aSRandall Stewart 		block_allowed = 0;
5306f8829a4aSRandall Stewart 	}
5307f8829a4aSRandall Stewart 	/* setup the endpoint */
5308f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
5309f8829a4aSRandall Stewart 	if (inp == NULL) {
5310c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
5311f8829a4aSRandall Stewart 		return (EFAULT);
5312f8829a4aSRandall Stewart 	}
531362c1ff9cSRandall Stewart 	rwnd_req = (SCTP_SB_LIMIT_RCV(so) >> SCTP_RWND_HIWAT_SHIFT);
5314f8829a4aSRandall Stewart 	/* Must be at least a MTU's worth */
5315f8829a4aSRandall Stewart 	if (rwnd_req < SCTP_MIN_RWND)
5316f8829a4aSRandall Stewart 		rwnd_req = SCTP_MIN_RWND;
5317f8829a4aSRandall Stewart 	in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
5318b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5319f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SORECV_ENTER,
53204e88d37aSMichael Tuexen 		    rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, uio->uio_resid);
532180fefe0aSRandall Stewart 	}
5322b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5323f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SORECV_ENTERPL,
53244e88d37aSMichael Tuexen 		    rwnd_req, block_allowed, so->so_rcv.sb_cc, uio->uio_resid);
532580fefe0aSRandall Stewart 	}
5326265de5bbSRobert Watson 	error = sblock(&so->so_rcv, (block_allowed ? SBL_WAIT : 0));
5327f8829a4aSRandall Stewart 	if (error) {
5328f8829a4aSRandall Stewart 		goto release_unlocked;
5329f8829a4aSRandall Stewart 	}
53308e1e6e5fSMateusz Guzik 	sockbuf_lock = 1;
5331f8829a4aSRandall Stewart restart:
53327abab911SRobert Watson 
5333f8829a4aSRandall Stewart 
5334f8829a4aSRandall Stewart restart_nosblocks:
5335f8829a4aSRandall Stewart 	if (hold_sblock == 0) {
5336f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&so->so_rcv);
5337f8829a4aSRandall Stewart 		hold_sblock = 1;
5338f8829a4aSRandall Stewart 	}
5339f8829a4aSRandall Stewart 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5340f8829a4aSRandall Stewart 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5341f8829a4aSRandall Stewart 		goto out;
5342f8829a4aSRandall Stewart 	}
53434e88d37aSMichael Tuexen 	if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) && (so->so_rcv.sb_cc == 0)) {
5344f8829a4aSRandall Stewart 		if (so->so_error) {
5345f8829a4aSRandall Stewart 			error = so->so_error;
534644b7479bSRandall Stewart 			if ((in_flags & MSG_PEEK) == 0)
534744b7479bSRandall Stewart 				so->so_error = 0;
53489f22f500SRandall Stewart 			goto out;
5349f8829a4aSRandall Stewart 		} else {
53504e88d37aSMichael Tuexen 			if (so->so_rcv.sb_cc == 0) {
53517924093fSRandall Stewart 				/* indicate EOF */
53527924093fSRandall Stewart 				error = 0;
5353f8829a4aSRandall Stewart 				goto out;
5354f8829a4aSRandall Stewart 			}
53559f22f500SRandall Stewart 		}
53569f22f500SRandall Stewart 	}
53574e88d37aSMichael Tuexen 	if ((so->so_rcv.sb_cc <= held_length) && block_allowed) {
5358f8829a4aSRandall Stewart 		/* we need to wait for data */
53594e88d37aSMichael Tuexen 		if ((so->so_rcv.sb_cc == 0) &&
5360f8829a4aSRandall Stewart 		    ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5361f8829a4aSRandall Stewart 		    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
5362f8829a4aSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
5363f8829a4aSRandall Stewart 				/*
5364f8829a4aSRandall Stewart 				 * For active open side clear flags for
5365f8829a4aSRandall Stewart 				 * re-use passive open is blocked by
5366f8829a4aSRandall Stewart 				 * connect.
5367f8829a4aSRandall Stewart 				 */
5368f8829a4aSRandall Stewart 				if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
5369f8829a4aSRandall Stewart 					/*
5370f8829a4aSRandall Stewart 					 * You were aborted, passive side
5371f8829a4aSRandall Stewart 					 * always hits here
5372f8829a4aSRandall Stewart 					 */
5373c4739e2fSRandall Stewart 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
5374f8829a4aSRandall Stewart 					error = ECONNRESET;
5375f8829a4aSRandall Stewart 				}
5376f8829a4aSRandall Stewart 				so->so_state &= ~(SS_ISCONNECTING |
5377f8829a4aSRandall Stewart 				    SS_ISDISCONNECTING |
5378f8829a4aSRandall Stewart 				    SS_ISCONFIRMING |
5379f8829a4aSRandall Stewart 				    SS_ISCONNECTED);
5380f8829a4aSRandall Stewart 				if (error == 0) {
5381f8829a4aSRandall Stewart 					if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5382c4739e2fSRandall Stewart 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5383f8829a4aSRandall Stewart 						error = ENOTCONN;
5384f8829a4aSRandall Stewart 					}
5385f8829a4aSRandall Stewart 				}
5386f8829a4aSRandall Stewart 				goto out;
5387f8829a4aSRandall Stewart 			}
5388f8829a4aSRandall Stewart 		}
5389f8829a4aSRandall Stewart 		error = sbwait(&so->so_rcv);
5390f8829a4aSRandall Stewart 		if (error) {
5391f8829a4aSRandall Stewart 			goto out;
5392f8829a4aSRandall Stewart 		}
5393f8829a4aSRandall Stewart 		held_length = 0;
5394f8829a4aSRandall Stewart 		goto restart_nosblocks;
53954e88d37aSMichael Tuexen 	} else if (so->so_rcv.sb_cc == 0) {
539644b7479bSRandall Stewart 		if (so->so_error) {
539744b7479bSRandall Stewart 			error = so->so_error;
539844b7479bSRandall Stewart 			if ((in_flags & MSG_PEEK) == 0)
539944b7479bSRandall Stewart 				so->so_error = 0;
540044b7479bSRandall Stewart 		} else {
540144b7479bSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
540244b7479bSRandall Stewart 			    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
540344b7479bSRandall Stewart 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
540444b7479bSRandall Stewart 					/*
540544b7479bSRandall Stewart 					 * For active open side clear flags
540644b7479bSRandall Stewart 					 * for re-use passive open is
540744b7479bSRandall Stewart 					 * blocked by connect.
540844b7479bSRandall Stewart 					 */
540944b7479bSRandall Stewart 					if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
541044b7479bSRandall Stewart 						/*
541144b7479bSRandall Stewart 						 * You were aborted, passive
541244b7479bSRandall Stewart 						 * side always hits here
541344b7479bSRandall Stewart 						 */
5414c4739e2fSRandall Stewart 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
541544b7479bSRandall Stewart 						error = ECONNRESET;
541644b7479bSRandall Stewart 					}
541744b7479bSRandall Stewart 					so->so_state &= ~(SS_ISCONNECTING |
541844b7479bSRandall Stewart 					    SS_ISDISCONNECTING |
541944b7479bSRandall Stewart 					    SS_ISCONFIRMING |
542044b7479bSRandall Stewart 					    SS_ISCONNECTED);
542144b7479bSRandall Stewart 					if (error == 0) {
542244b7479bSRandall Stewart 						if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5423c4739e2fSRandall Stewart 							SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
542444b7479bSRandall Stewart 							error = ENOTCONN;
542544b7479bSRandall Stewart 						}
542644b7479bSRandall Stewart 					}
542744b7479bSRandall Stewart 					goto out;
542844b7479bSRandall Stewart 				}
542944b7479bSRandall Stewart 			}
5430c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EWOULDBLOCK);
5431f8829a4aSRandall Stewart 			error = EWOULDBLOCK;
543244b7479bSRandall Stewart 		}
5433f8829a4aSRandall Stewart 		goto out;
5434f8829a4aSRandall Stewart 	}
5435d06c82f1SRandall Stewart 	if (hold_sblock == 1) {
5436d06c82f1SRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
5437d06c82f1SRandall Stewart 		hold_sblock = 0;
5438d06c82f1SRandall Stewart 	}
5439f8829a4aSRandall Stewart 	/* we possibly have data we can read */
54403c503c28SRandall Stewart 	/* sa_ignore FREED_MEMORY */
5441f8829a4aSRandall Stewart 	control = TAILQ_FIRST(&inp->read_queue);
5442f8829a4aSRandall Stewart 	if (control == NULL) {
5443f8829a4aSRandall Stewart 		/*
5444f8829a4aSRandall Stewart 		 * This could be happening since the appender did the
5445f8829a4aSRandall Stewart 		 * increment but as not yet did the tailq insert onto the
5446f8829a4aSRandall Stewart 		 * read_queue
5447f8829a4aSRandall Stewart 		 */
5448f8829a4aSRandall Stewart 		if (hold_rlock == 0) {
5449f8829a4aSRandall Stewart 			SCTP_INP_READ_LOCK(inp);
5450f8829a4aSRandall Stewart 		}
5451f8829a4aSRandall Stewart 		control = TAILQ_FIRST(&inp->read_queue);
54524e88d37aSMichael Tuexen 		if ((control == NULL) && (so->so_rcv.sb_cc != 0)) {
5453a5d547adSRandall Stewart #ifdef INVARIANTS
5454f8829a4aSRandall Stewart 			panic("Huh, its non zero and nothing on control?");
5455f8829a4aSRandall Stewart #endif
54564e88d37aSMichael Tuexen 			so->so_rcv.sb_cc = 0;
5457f8829a4aSRandall Stewart 		}
5458f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
5459f8829a4aSRandall Stewart 		hold_rlock = 0;
5460f8829a4aSRandall Stewart 		goto restart;
5461f8829a4aSRandall Stewart 	}
5462f8829a4aSRandall Stewart 	if ((control->length == 0) &&
5463f8829a4aSRandall Stewart 	    (control->do_not_ref_stcb)) {
5464f8829a4aSRandall Stewart 		/*
5465f8829a4aSRandall Stewart 		 * Clean up code for freeing assoc that left behind a
5466f8829a4aSRandall Stewart 		 * pdapi.. maybe a peer in EEOR that just closed after
5467f8829a4aSRandall Stewart 		 * sending and never indicated a EOR.
5468f8829a4aSRandall Stewart 		 */
5469f8829a4aSRandall Stewart 		if (hold_rlock == 0) {
5470f8829a4aSRandall Stewart 			hold_rlock = 1;
5471f8829a4aSRandall Stewart 			SCTP_INP_READ_LOCK(inp);
5472f8829a4aSRandall Stewart 		}
5473f8829a4aSRandall Stewart 		control->held_length = 0;
5474f8829a4aSRandall Stewart 		if (control->data) {
5475f8829a4aSRandall Stewart 			/* Hmm there is data here .. fix */
54764c9179adSRandall Stewart 			struct mbuf *m_tmp;
5477f8829a4aSRandall Stewart 			int cnt = 0;
5478f8829a4aSRandall Stewart 
54794c9179adSRandall Stewart 			m_tmp = control->data;
54804c9179adSRandall Stewart 			while (m_tmp) {
54814c9179adSRandall Stewart 				cnt += SCTP_BUF_LEN(m_tmp);
54824c9179adSRandall Stewart 				if (SCTP_BUF_NEXT(m_tmp) == NULL) {
54834c9179adSRandall Stewart 					control->tail_mbuf = m_tmp;
5484f8829a4aSRandall Stewart 					control->end_added = 1;
5485f8829a4aSRandall Stewart 				}
54864c9179adSRandall Stewart 				m_tmp = SCTP_BUF_NEXT(m_tmp);
5487f8829a4aSRandall Stewart 			}
5488f8829a4aSRandall Stewart 			control->length = cnt;
5489f8829a4aSRandall Stewart 		} else {
5490f8829a4aSRandall Stewart 			/* remove it */
5491f8829a4aSRandall Stewart 			TAILQ_REMOVE(&inp->read_queue, control, next);
5492f8829a4aSRandall Stewart 			/* Add back any hiddend data */
5493f8829a4aSRandall Stewart 			sctp_free_remote_addr(control->whoFrom);
5494f8829a4aSRandall Stewart 			sctp_free_a_readq(stcb, control);
5495f8829a4aSRandall Stewart 		}
5496f8829a4aSRandall Stewart 		if (hold_rlock) {
5497f8829a4aSRandall Stewart 			hold_rlock = 0;
5498f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
5499f8829a4aSRandall Stewart 		}
5500f8829a4aSRandall Stewart 		goto restart;
5501f8829a4aSRandall Stewart 	}
5502810ec536SMichael Tuexen 	if ((control->length == 0) &&
5503810ec536SMichael Tuexen 	    (control->end_added == 1)) {
5504810ec536SMichael Tuexen 		/*
5505810ec536SMichael Tuexen 		 * Do we also need to check for (control->pdapi_aborted ==
5506810ec536SMichael Tuexen 		 * 1)?
5507810ec536SMichael Tuexen 		 */
5508810ec536SMichael Tuexen 		if (hold_rlock == 0) {
5509810ec536SMichael Tuexen 			hold_rlock = 1;
5510810ec536SMichael Tuexen 			SCTP_INP_READ_LOCK(inp);
5511810ec536SMichael Tuexen 		}
5512810ec536SMichael Tuexen 		TAILQ_REMOVE(&inp->read_queue, control, next);
5513810ec536SMichael Tuexen 		if (control->data) {
5514810ec536SMichael Tuexen #ifdef INVARIANTS
5515810ec536SMichael Tuexen 			panic("control->data not null but control->length == 0");
5516810ec536SMichael Tuexen #else
5517810ec536SMichael Tuexen 			SCTP_PRINTF("Strange, data left in the control buffer. Cleaning up.\n");
5518810ec536SMichael Tuexen 			sctp_m_freem(control->data);
5519810ec536SMichael Tuexen 			control->data = NULL;
5520810ec536SMichael Tuexen #endif
5521810ec536SMichael Tuexen 		}
5522810ec536SMichael Tuexen 		if (control->aux_data) {
5523810ec536SMichael Tuexen 			sctp_m_free(control->aux_data);
5524810ec536SMichael Tuexen 			control->aux_data = NULL;
5525810ec536SMichael Tuexen 		}
5526810ec536SMichael Tuexen 		sctp_free_remote_addr(control->whoFrom);
5527810ec536SMichael Tuexen 		sctp_free_a_readq(stcb, control);
5528810ec536SMichael Tuexen 		if (hold_rlock) {
5529810ec536SMichael Tuexen 			hold_rlock = 0;
5530810ec536SMichael Tuexen 			SCTP_INP_READ_UNLOCK(inp);
5531810ec536SMichael Tuexen 		}
5532810ec536SMichael Tuexen 		goto restart;
5533810ec536SMichael Tuexen 	}
5534f8829a4aSRandall Stewart 	if (control->length == 0) {
5535f8829a4aSRandall Stewart 		if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
5536f8829a4aSRandall Stewart 		    (filling_sinfo)) {
5537f8829a4aSRandall Stewart 			/* find a more suitable one then this */
5538f8829a4aSRandall Stewart 			ctl = TAILQ_NEXT(control, next);
5539f8829a4aSRandall Stewart 			while (ctl) {
55409a6142d8SRandall Stewart 				if ((ctl->stcb != control->stcb) && (ctl->length) &&
55419a6142d8SRandall Stewart 				    (ctl->some_taken ||
55426114cd96SRandall Stewart 				    (ctl->spec_flags & M_NOTIFICATION) ||
55439a6142d8SRandall Stewart 				    ((ctl->do_not_ref_stcb == 0) &&
55449a6142d8SRandall Stewart 				    (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))
55459a6142d8SRandall Stewart 				    ) {
55469a6142d8SRandall Stewart 					/*-
55479a6142d8SRandall Stewart 					 * If we have a different TCB next, and there is data
55489a6142d8SRandall Stewart 					 * present. If we have already taken some (pdapi), OR we can
55499a6142d8SRandall Stewart 					 * ref the tcb and no delivery as started on this stream, we
555017205eccSRandall Stewart 					 * take it. Note we allow a notification on a different
555117205eccSRandall Stewart 					 * assoc to be delivered..
55529a6142d8SRandall Stewart 					 */
55539a6142d8SRandall Stewart 					control = ctl;
55549a6142d8SRandall Stewart 					goto found_one;
55559a6142d8SRandall Stewart 				} else if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) &&
55569a6142d8SRandall Stewart 					    (ctl->length) &&
55579a6142d8SRandall Stewart 					    ((ctl->some_taken) ||
55589a6142d8SRandall Stewart 					    ((ctl->do_not_ref_stcb == 0) &&
555917205eccSRandall Stewart 					    ((ctl->spec_flags & M_NOTIFICATION) == 0) &&
5560b5c16493SMichael Tuexen 				    (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))) {
55619a6142d8SRandall Stewart 					/*-
55629a6142d8SRandall Stewart 					 * If we have the same tcb, and there is data present, and we
55639a6142d8SRandall Stewart 					 * have the strm interleave feature present. Then if we have
55649a6142d8SRandall Stewart 					 * taken some (pdapi) or we can refer to tht tcb AND we have
55659a6142d8SRandall Stewart 					 * not started a delivery for this stream, we can take it.
556617205eccSRandall Stewart 					 * Note we do NOT allow a notificaiton on the same assoc to
556717205eccSRandall Stewart 					 * be delivered.
55689a6142d8SRandall Stewart 					 */
5569f8829a4aSRandall Stewart 					control = ctl;
5570f8829a4aSRandall Stewart 					goto found_one;
5571f8829a4aSRandall Stewart 				}
5572f8829a4aSRandall Stewart 				ctl = TAILQ_NEXT(ctl, next);
5573f8829a4aSRandall Stewart 			}
5574f8829a4aSRandall Stewart 		}
5575f8829a4aSRandall Stewart 		/*
5576f8829a4aSRandall Stewart 		 * if we reach here, not suitable replacement is available
55774e88d37aSMichael Tuexen 		 * <or> fragment interleave is NOT on. So stuff the sb_cc
5578f8829a4aSRandall Stewart 		 * into the our held count, and its time to sleep again.
5579f8829a4aSRandall Stewart 		 */
55804e88d37aSMichael Tuexen 		held_length = so->so_rcv.sb_cc;
55814e88d37aSMichael Tuexen 		control->held_length = so->so_rcv.sb_cc;
5582f8829a4aSRandall Stewart 		goto restart;
5583f8829a4aSRandall Stewart 	}
5584f8829a4aSRandall Stewart 	/* Clear the held length since there is something to read */
5585f8829a4aSRandall Stewart 	control->held_length = 0;
5586f8829a4aSRandall Stewart 	if (hold_rlock) {
5587f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
5588f8829a4aSRandall Stewart 		hold_rlock = 0;
5589f8829a4aSRandall Stewart 	}
5590f8829a4aSRandall Stewart found_one:
5591f8829a4aSRandall Stewart 	/*
5592f8829a4aSRandall Stewart 	 * If we reach here, control has a some data for us to read off.
5593f8829a4aSRandall Stewart 	 * Note that stcb COULD be NULL.
5594f8829a4aSRandall Stewart 	 */
55959c04b296SRandall Stewart 	control->some_taken++;
5596f8829a4aSRandall Stewart 	if (hold_sblock) {
5597f8829a4aSRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
5598f8829a4aSRandall Stewart 		hold_sblock = 0;
5599f8829a4aSRandall Stewart 	}
5600f8829a4aSRandall Stewart 	stcb = control->stcb;
5601f8829a4aSRandall Stewart 	if (stcb) {
56020696e120SRandall Stewart 		if ((control->do_not_ref_stcb == 0) &&
56030696e120SRandall Stewart 		    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
560450cec919SRandall Stewart 			if (freecnt_applied == 0)
5605f8829a4aSRandall Stewart 				stcb = NULL;
5606f8829a4aSRandall Stewart 		} else if (control->do_not_ref_stcb == 0) {
5607f8829a4aSRandall Stewart 			/* you can't free it on me please */
5608f8829a4aSRandall Stewart 			/*
5609f8829a4aSRandall Stewart 			 * The lock on the socket buffer protects us so the
5610f8829a4aSRandall Stewart 			 * free code will stop. But since we used the
5611f8829a4aSRandall Stewart 			 * socketbuf lock and the sender uses the tcb_lock
5612f8829a4aSRandall Stewart 			 * to increment, we need to use the atomic add to
5613f8829a4aSRandall Stewart 			 * the refcnt
5614f8829a4aSRandall Stewart 			 */
5615d55b0b1bSRandall Stewart 			if (freecnt_applied) {
5616d55b0b1bSRandall Stewart #ifdef INVARIANTS
5617207304d4SRandall Stewart 				panic("refcnt already incremented");
5618d55b0b1bSRandall Stewart #else
5619cd3fd531SMichael Tuexen 				SCTP_PRINTF("refcnt already incremented?\n");
5620d55b0b1bSRandall Stewart #endif
5621d55b0b1bSRandall Stewart 			} else {
562250cec919SRandall Stewart 				atomic_add_int(&stcb->asoc.refcnt, 1);
5623f8829a4aSRandall Stewart 				freecnt_applied = 1;
5624d55b0b1bSRandall Stewart 			}
5625f8829a4aSRandall Stewart 			/*
5626f8829a4aSRandall Stewart 			 * Setup to remember how much we have not yet told
5627f8829a4aSRandall Stewart 			 * the peer our rwnd has opened up. Note we grab the
5628f8829a4aSRandall Stewart 			 * value from the tcb from last time. Note too that
56290696e120SRandall Stewart 			 * sack sending clears this when a sack is sent,
5630f8829a4aSRandall Stewart 			 * which is fine. Once we hit the rwnd_req, we then
5631f8829a4aSRandall Stewart 			 * will go to the sctp_user_rcvd() that will not
5632f8829a4aSRandall Stewart 			 * lock until it KNOWs it MUST send a WUP-SACK.
5633f8829a4aSRandall Stewart 			 */
5634f8829a4aSRandall Stewart 			freed_so_far = stcb->freed_by_sorcv_sincelast;
5635f8829a4aSRandall Stewart 			stcb->freed_by_sorcv_sincelast = 0;
5636f8829a4aSRandall Stewart 		}
5637f8829a4aSRandall Stewart 	}
56386114cd96SRandall Stewart 	if (stcb &&
56396114cd96SRandall Stewart 	    ((control->spec_flags & M_NOTIFICATION) == 0) &&
56406114cd96SRandall Stewart 	    control->do_not_ref_stcb == 0) {
5641d06c82f1SRandall Stewart 		stcb->asoc.strmin[control->sinfo_stream].delivery_started = 1;
5642d06c82f1SRandall Stewart 	}
5643f8829a4aSRandall Stewart 	/* First lets get off the sinfo and sockaddr info */
5644f8829a4aSRandall Stewart 	if ((sinfo) && filling_sinfo) {
5645f8829a4aSRandall Stewart 		memcpy(sinfo, control, sizeof(struct sctp_nonpad_sndrcvinfo));
5646f8829a4aSRandall Stewart 		nxt = TAILQ_NEXT(control, next);
5647e2e7c62eSMichael Tuexen 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
5648e2e7c62eSMichael Tuexen 		    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) {
5649f8829a4aSRandall Stewart 			struct sctp_extrcvinfo *s_extra;
5650f8829a4aSRandall Stewart 
5651f8829a4aSRandall Stewart 			s_extra = (struct sctp_extrcvinfo *)sinfo;
56529a6142d8SRandall Stewart 			if ((nxt) &&
56539a6142d8SRandall Stewart 			    (nxt->length)) {
56549a6142d8SRandall Stewart 				s_extra->sreinfo_next_flags = SCTP_NEXT_MSG_AVAIL;
5655f8829a4aSRandall Stewart 				if (nxt->sinfo_flags & SCTP_UNORDERED) {
56569a6142d8SRandall Stewart 					s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_IS_UNORDERED;
5657f8829a4aSRandall Stewart 				}
5658f42a358aSRandall Stewart 				if (nxt->spec_flags & M_NOTIFICATION) {
56599a6142d8SRandall Stewart 					s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_IS_NOTIFICATION;
5660f42a358aSRandall Stewart 				}
56619a6142d8SRandall Stewart 				s_extra->sreinfo_next_aid = nxt->sinfo_assoc_id;
56629a6142d8SRandall Stewart 				s_extra->sreinfo_next_length = nxt->length;
56639a6142d8SRandall Stewart 				s_extra->sreinfo_next_ppid = nxt->sinfo_ppid;
56649a6142d8SRandall Stewart 				s_extra->sreinfo_next_stream = nxt->sinfo_stream;
5665f8829a4aSRandall Stewart 				if (nxt->tail_mbuf != NULL) {
5666139bc87fSRandall Stewart 					if (nxt->end_added) {
56679a6142d8SRandall Stewart 						s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_ISCOMPLETE;
5668f8829a4aSRandall Stewart 					}
5669f8829a4aSRandall Stewart 				}
5670f8829a4aSRandall Stewart 			} else {
5671f8829a4aSRandall Stewart 				/*
5672f8829a4aSRandall Stewart 				 * we explicitly 0 this, since the memcpy
5673f8829a4aSRandall Stewart 				 * got some other things beyond the older
5674f8829a4aSRandall Stewart 				 * sinfo_ that is on the control's structure
5675f8829a4aSRandall Stewart 				 * :-D
5676f8829a4aSRandall Stewart 				 */
56779a6142d8SRandall Stewart 				nxt = NULL;
56789a6142d8SRandall Stewart 				s_extra->sreinfo_next_flags = SCTP_NO_NEXT_MSG;
56799a6142d8SRandall Stewart 				s_extra->sreinfo_next_aid = 0;
56809a6142d8SRandall Stewart 				s_extra->sreinfo_next_length = 0;
56819a6142d8SRandall Stewart 				s_extra->sreinfo_next_ppid = 0;
56829a6142d8SRandall Stewart 				s_extra->sreinfo_next_stream = 0;
5683f8829a4aSRandall Stewart 			}
5684f8829a4aSRandall Stewart 		}
5685f8829a4aSRandall Stewart 		/*
5686f8829a4aSRandall Stewart 		 * update off the real current cum-ack, if we have an stcb.
5687f8829a4aSRandall Stewart 		 */
56880696e120SRandall Stewart 		if ((control->do_not_ref_stcb == 0) && stcb)
5689f8829a4aSRandall Stewart 			sinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
5690f8829a4aSRandall Stewart 		/*
5691f8829a4aSRandall Stewart 		 * mask off the high bits, we keep the actual chunk bits in
5692f8829a4aSRandall Stewart 		 * there.
5693f8829a4aSRandall Stewart 		 */
5694f8829a4aSRandall Stewart 		sinfo->sinfo_flags &= 0x00ff;
56955f26a41dSRandall Stewart 		if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) {
56965f26a41dSRandall Stewart 			sinfo->sinfo_flags |= SCTP_UNORDERED;
56975f26a41dSRandall Stewart 		}
5698f8829a4aSRandall Stewart 	}
569918e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
570018e198d3SRandall Stewart 	{
570118e198d3SRandall Stewart 		int index, newindex;
570218e198d3SRandall Stewart 		struct sctp_pcbtsn_rlog *entry;
570318e198d3SRandall Stewart 
570418e198d3SRandall Stewart 		do {
570518e198d3SRandall Stewart 			index = inp->readlog_index;
570618e198d3SRandall Stewart 			newindex = index + 1;
570718e198d3SRandall Stewart 			if (newindex >= SCTP_READ_LOG_SIZE) {
570818e198d3SRandall Stewart 				newindex = 0;
570918e198d3SRandall Stewart 			}
571018e198d3SRandall Stewart 		} while (atomic_cmpset_int(&inp->readlog_index, index, newindex) == 0);
571118e198d3SRandall Stewart 		entry = &inp->readlog[index];
571218e198d3SRandall Stewart 		entry->vtag = control->sinfo_assoc_id;
571318e198d3SRandall Stewart 		entry->strm = control->sinfo_stream;
571418e198d3SRandall Stewart 		entry->seq = control->sinfo_ssn;
571518e198d3SRandall Stewart 		entry->sz = control->length;
571618e198d3SRandall Stewart 		entry->flgs = control->sinfo_flags;
571718e198d3SRandall Stewart 	}
571818e198d3SRandall Stewart #endif
5719d59107f7SMichael Tuexen 	if ((fromlen > 0) && (from != NULL)) {
5720d59107f7SMichael Tuexen 		union sctp_sockstore store;
5721d59107f7SMichael Tuexen 		size_t len;
5722d59107f7SMichael Tuexen 
5723b5b6e5c2SMichael Tuexen 		switch (control->whoFrom->ro._l_addr.sa.sa_family) {
5724b5b6e5c2SMichael Tuexen #ifdef INET6
5725b5b6e5c2SMichael Tuexen 		case AF_INET6:
5726d59107f7SMichael Tuexen 			len = sizeof(struct sockaddr_in6);
5727d59107f7SMichael Tuexen 			store.sin6 = control->whoFrom->ro._l_addr.sin6;
5728d59107f7SMichael Tuexen 			store.sin6.sin6_port = control->port_from;
5729b5b6e5c2SMichael Tuexen 			break;
5730f8829a4aSRandall Stewart #endif
5731b5b6e5c2SMichael Tuexen #ifdef INET
5732b5b6e5c2SMichael Tuexen 		case AF_INET:
5733d59107f7SMichael Tuexen #ifdef INET6
5734d59107f7SMichael Tuexen 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
5735d59107f7SMichael Tuexen 				len = sizeof(struct sockaddr_in6);
5736d59107f7SMichael Tuexen 				in6_sin_2_v4mapsin6(&control->whoFrom->ro._l_addr.sin,
5737d59107f7SMichael Tuexen 				    &store.sin6);
5738d59107f7SMichael Tuexen 				store.sin6.sin6_port = control->port_from;
5739d59107f7SMichael Tuexen 			} else {
5740d59107f7SMichael Tuexen 				len = sizeof(struct sockaddr_in);
5741d59107f7SMichael Tuexen 				store.sin = control->whoFrom->ro._l_addr.sin;
5742d59107f7SMichael Tuexen 				store.sin.sin_port = control->port_from;
5743d59107f7SMichael Tuexen 			}
5744d59107f7SMichael Tuexen #else
5745d59107f7SMichael Tuexen 			len = sizeof(struct sockaddr_in);
5746d59107f7SMichael Tuexen 			store.sin = control->whoFrom->ro._l_addr.sin;
5747d59107f7SMichael Tuexen 			store.sin.sin_port = control->port_from;
5748d59107f7SMichael Tuexen #endif
5749b5b6e5c2SMichael Tuexen 			break;
5750b5b6e5c2SMichael Tuexen #endif
5751b5b6e5c2SMichael Tuexen 		default:
5752d59107f7SMichael Tuexen 			len = 0;
5753b5b6e5c2SMichael Tuexen 			break;
5754b5b6e5c2SMichael Tuexen 		}
5755d59107f7SMichael Tuexen 		memcpy(from, &store, min((size_t)fromlen, len));
5756e0e00a4dSMichael Tuexen #ifdef INET6
5757f8829a4aSRandall Stewart 		{
5758b5b6e5c2SMichael Tuexen 			struct sockaddr_in6 lsa6, *from6;
5759f8829a4aSRandall Stewart 
5760b5b6e5c2SMichael Tuexen 			from6 = (struct sockaddr_in6 *)from;
5761b5b6e5c2SMichael Tuexen 			sctp_recover_scope_mac(from6, (&lsa6));
5762f8829a4aSRandall Stewart 		}
5763f8829a4aSRandall Stewart #endif
5764f8829a4aSRandall Stewart 	}
5765f8829a4aSRandall Stewart 	/* now copy out what data we can */
5766f8829a4aSRandall Stewart 	if (mp == NULL) {
5767f8829a4aSRandall Stewart 		/* copy out each mbuf in the chain up to length */
5768f8829a4aSRandall Stewart get_more_data:
5769f8829a4aSRandall Stewart 		m = control->data;
5770f8829a4aSRandall Stewart 		while (m) {
5771f8829a4aSRandall Stewart 			/* Move out all we can */
5772f8829a4aSRandall Stewart 			cp_len = (int)uio->uio_resid;
5773139bc87fSRandall Stewart 			my_len = (int)SCTP_BUF_LEN(m);
5774f8829a4aSRandall Stewart 			if (cp_len > my_len) {
5775f8829a4aSRandall Stewart 				/* not enough in this buf */
5776f8829a4aSRandall Stewart 				cp_len = my_len;
5777f8829a4aSRandall Stewart 			}
5778f8829a4aSRandall Stewart 			if (hold_rlock) {
5779f8829a4aSRandall Stewart 				SCTP_INP_READ_UNLOCK(inp);
5780f8829a4aSRandall Stewart 				hold_rlock = 0;
5781f8829a4aSRandall Stewart 			}
5782f8829a4aSRandall Stewart 			if (cp_len > 0)
5783f8829a4aSRandall Stewart 				error = uiomove(mtod(m, char *), cp_len, uio);
5784f8829a4aSRandall Stewart 			/* re-read */
5785f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5786f8829a4aSRandall Stewart 				goto release;
5787f8829a4aSRandall Stewart 			}
57880696e120SRandall Stewart 			if ((control->do_not_ref_stcb == 0) && stcb &&
5789f8829a4aSRandall Stewart 			    stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5790f8829a4aSRandall Stewart 				no_rcv_needed = 1;
5791f8829a4aSRandall Stewart 			}
5792f8829a4aSRandall Stewart 			if (error) {
5793f8829a4aSRandall Stewart 				/* error we are out of here */
5794f8829a4aSRandall Stewart 				goto release;
5795f8829a4aSRandall Stewart 			}
5796139bc87fSRandall Stewart 			if ((SCTP_BUF_NEXT(m) == NULL) &&
5797139bc87fSRandall Stewart 			    (cp_len >= SCTP_BUF_LEN(m)) &&
5798f8829a4aSRandall Stewart 			    ((control->end_added == 0) ||
57990696e120SRandall Stewart 			    (control->end_added &&
58000696e120SRandall Stewart 			    (TAILQ_NEXT(control, next) == NULL)))
5801f8829a4aSRandall Stewart 			    ) {
5802f8829a4aSRandall Stewart 				SCTP_INP_READ_LOCK(inp);
5803f8829a4aSRandall Stewart 				hold_rlock = 1;
5804f8829a4aSRandall Stewart 			}
5805139bc87fSRandall Stewart 			if (cp_len == SCTP_BUF_LEN(m)) {
5806139bc87fSRandall Stewart 				if ((SCTP_BUF_NEXT(m) == NULL) &&
5807139bc87fSRandall Stewart 				    (control->end_added)) {
5808f8829a4aSRandall Stewart 					out_flags |= MSG_EOR;
580952129fcdSRandall Stewart 					if ((control->do_not_ref_stcb == 0) &&
581052129fcdSRandall Stewart 					    (control->stcb != NULL) &&
581152129fcdSRandall Stewart 					    ((control->spec_flags & M_NOTIFICATION) == 0))
5812ee7f9857SRandall Stewart 						control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5813f8829a4aSRandall Stewart 				}
5814139bc87fSRandall Stewart 				if (control->spec_flags & M_NOTIFICATION) {
5815f8829a4aSRandall Stewart 					out_flags |= MSG_NOTIFICATION;
5816f8829a4aSRandall Stewart 				}
5817f8829a4aSRandall Stewart 				/* we ate up the mbuf */
5818f8829a4aSRandall Stewart 				if (in_flags & MSG_PEEK) {
5819f8829a4aSRandall Stewart 					/* just looking */
5820139bc87fSRandall Stewart 					m = SCTP_BUF_NEXT(m);
5821f8829a4aSRandall Stewart 					copied_so_far += cp_len;
5822f8829a4aSRandall Stewart 				} else {
5823f8829a4aSRandall Stewart 					/* dispose of the mbuf */
5824b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5825f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv,
5826139bc87fSRandall Stewart 						    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
582780fefe0aSRandall Stewart 					}
5828f8829a4aSRandall Stewart 					sctp_sbfree(control, stcb, &so->so_rcv, m);
5829b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5830f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv,
5831f8829a4aSRandall Stewart 						    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
583280fefe0aSRandall Stewart 					}
5833f8829a4aSRandall Stewart 					copied_so_far += cp_len;
5834f8829a4aSRandall Stewart 					freed_so_far += cp_len;
5835c4739e2fSRandall Stewart 					freed_so_far += MSIZE;
583618e198d3SRandall Stewart 					atomic_subtract_int(&control->length, cp_len);
5837f8829a4aSRandall Stewart 					control->data = sctp_m_free(m);
5838f8829a4aSRandall Stewart 					m = control->data;
5839f8829a4aSRandall Stewart 					/*
5840f8829a4aSRandall Stewart 					 * been through it all, must hold sb
5841f8829a4aSRandall Stewart 					 * lock ok to null tail
5842f8829a4aSRandall Stewart 					 */
5843f8829a4aSRandall Stewart 					if (control->data == NULL) {
5844a5d547adSRandall Stewart #ifdef INVARIANTS
5845f8829a4aSRandall Stewart 						if ((control->end_added == 0) ||
5846f8829a4aSRandall Stewart 						    (TAILQ_NEXT(control, next) == NULL)) {
5847f8829a4aSRandall Stewart 							/*
5848f8829a4aSRandall Stewart 							 * If the end is not
5849f8829a4aSRandall Stewart 							 * added, OR the
5850f8829a4aSRandall Stewart 							 * next is NOT null
5851f8829a4aSRandall Stewart 							 * we MUST have the
5852f8829a4aSRandall Stewart 							 * lock.
5853f8829a4aSRandall Stewart 							 */
5854f8829a4aSRandall Stewart 							if (mtx_owned(&inp->inp_rdata_mtx) == 0) {
5855f8829a4aSRandall Stewart 								panic("Hmm we don't own the lock?");
5856f8829a4aSRandall Stewart 							}
5857f8829a4aSRandall Stewart 						}
5858f8829a4aSRandall Stewart #endif
5859f8829a4aSRandall Stewart 						control->tail_mbuf = NULL;
5860a5d547adSRandall Stewart #ifdef INVARIANTS
5861f8829a4aSRandall Stewart 						if ((control->end_added) && ((out_flags & MSG_EOR) == 0)) {
5862f8829a4aSRandall Stewart 							panic("end_added, nothing left and no MSG_EOR");
5863f8829a4aSRandall Stewart 						}
5864f8829a4aSRandall Stewart #endif
5865f8829a4aSRandall Stewart 					}
5866f8829a4aSRandall Stewart 				}
5867f8829a4aSRandall Stewart 			} else {
5868f8829a4aSRandall Stewart 				/* Do we need to trim the mbuf? */
5869139bc87fSRandall Stewart 				if (control->spec_flags & M_NOTIFICATION) {
5870f8829a4aSRandall Stewart 					out_flags |= MSG_NOTIFICATION;
5871f8829a4aSRandall Stewart 				}
5872f8829a4aSRandall Stewart 				if ((in_flags & MSG_PEEK) == 0) {
5873139bc87fSRandall Stewart 					SCTP_BUF_RESV_UF(m, cp_len);
5874139bc87fSRandall Stewart 					SCTP_BUF_LEN(m) -= cp_len;
5875b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5876f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, cp_len);
587780fefe0aSRandall Stewart 					}
58784e88d37aSMichael Tuexen 					atomic_subtract_int(&so->so_rcv.sb_cc, cp_len);
58790696e120SRandall Stewart 					if ((control->do_not_ref_stcb == 0) &&
58800696e120SRandall Stewart 					    stcb) {
58814e88d37aSMichael Tuexen 						atomic_subtract_int(&stcb->asoc.sb_cc, cp_len);
5882f8829a4aSRandall Stewart 					}
5883f8829a4aSRandall Stewart 					copied_so_far += cp_len;
5884f8829a4aSRandall Stewart 					freed_so_far += cp_len;
5885c4739e2fSRandall Stewart 					freed_so_far += MSIZE;
5886b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5887f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb,
5888f8829a4aSRandall Stewart 						    SCTP_LOG_SBRESULT, 0);
588980fefe0aSRandall Stewart 					}
589018e198d3SRandall Stewart 					atomic_subtract_int(&control->length, cp_len);
5891f8829a4aSRandall Stewart 				} else {
5892f8829a4aSRandall Stewart 					copied_so_far += cp_len;
5893f8829a4aSRandall Stewart 				}
5894f8829a4aSRandall Stewart 			}
5895d61a0ae0SRandall Stewart 			if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) {
5896f8829a4aSRandall Stewart 				break;
5897f8829a4aSRandall Stewart 			}
5898f8829a4aSRandall Stewart 			if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5899f8829a4aSRandall Stewart 			    (control->do_not_ref_stcb == 0) &&
5900f8829a4aSRandall Stewart 			    (freed_so_far >= rwnd_req)) {
5901f8829a4aSRandall Stewart 				sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5902f8829a4aSRandall Stewart 			}
5903f8829a4aSRandall Stewart 		}		/* end while(m) */
5904f8829a4aSRandall Stewart 		/*
5905f8829a4aSRandall Stewart 		 * At this point we have looked at it all and we either have
5906f8829a4aSRandall Stewart 		 * a MSG_EOR/or read all the user wants... <OR>
5907f8829a4aSRandall Stewart 		 * control->length == 0.
5908f8829a4aSRandall Stewart 		 */
5909d61a0ae0SRandall Stewart 		if ((out_flags & MSG_EOR) && ((in_flags & MSG_PEEK) == 0)) {
5910f8829a4aSRandall Stewart 			/* we are done with this control */
5911f8829a4aSRandall Stewart 			if (control->length == 0) {
5912f8829a4aSRandall Stewart 				if (control->data) {
5913a5d547adSRandall Stewart #ifdef INVARIANTS
5914f8829a4aSRandall Stewart 					panic("control->data not null at read eor?");
5915f8829a4aSRandall Stewart #else
5916ad81507eSRandall Stewart 					SCTP_PRINTF("Strange, data left in the control buffer .. invarients would panic?\n");
5917f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
5918f8829a4aSRandall Stewart 					control->data = NULL;
5919f8829a4aSRandall Stewart #endif
5920f8829a4aSRandall Stewart 				}
5921f8829a4aSRandall Stewart 		done_with_control:
5922f8829a4aSRandall Stewart 				if (TAILQ_NEXT(control, next) == NULL) {
5923f8829a4aSRandall Stewart 					/*
5924f8829a4aSRandall Stewart 					 * If we don't have a next we need a
5925b201f536SRandall Stewart 					 * lock, if there is a next
5926b201f536SRandall Stewart 					 * interrupt is filling ahead of us
5927b201f536SRandall Stewart 					 * and we don't need a lock to
5928b201f536SRandall Stewart 					 * remove this guy (which is the
5929b201f536SRandall Stewart 					 * head of the queue).
5930f8829a4aSRandall Stewart 					 */
5931f8829a4aSRandall Stewart 					if (hold_rlock == 0) {
5932f8829a4aSRandall Stewart 						SCTP_INP_READ_LOCK(inp);
5933f8829a4aSRandall Stewart 						hold_rlock = 1;
5934f8829a4aSRandall Stewart 					}
5935f8829a4aSRandall Stewart 				}
5936f8829a4aSRandall Stewart 				TAILQ_REMOVE(&inp->read_queue, control, next);
5937f8829a4aSRandall Stewart 				/* Add back any hiddend data */
5938f8829a4aSRandall Stewart 				if (control->held_length) {
5939f8829a4aSRandall Stewart 					held_length = 0;
5940f8829a4aSRandall Stewart 					control->held_length = 0;
5941f8829a4aSRandall Stewart 					wakeup_read_socket = 1;
5942f8829a4aSRandall Stewart 				}
594317205eccSRandall Stewart 				if (control->aux_data) {
594417205eccSRandall Stewart 					sctp_m_free(control->aux_data);
594517205eccSRandall Stewart 					control->aux_data = NULL;
594617205eccSRandall Stewart 				}
5947f8829a4aSRandall Stewart 				no_rcv_needed = control->do_not_ref_stcb;
5948f8829a4aSRandall Stewart 				sctp_free_remote_addr(control->whoFrom);
5949f8829a4aSRandall Stewart 				control->data = NULL;
5950f8829a4aSRandall Stewart 				sctp_free_a_readq(stcb, control);
5951f8829a4aSRandall Stewart 				control = NULL;
59520696e120SRandall Stewart 				if ((freed_so_far >= rwnd_req) &&
59530696e120SRandall Stewart 				    (no_rcv_needed == 0))
5954f8829a4aSRandall Stewart 					sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5955f8829a4aSRandall Stewart 
5956f8829a4aSRandall Stewart 			} else {
5957f8829a4aSRandall Stewart 				/*
5958f8829a4aSRandall Stewart 				 * The user did not read all of this
5959f8829a4aSRandall Stewart 				 * message, turn off the returned MSG_EOR
5960f8829a4aSRandall Stewart 				 * since we are leaving more behind on the
5961f8829a4aSRandall Stewart 				 * control to read.
5962f8829a4aSRandall Stewart 				 */
5963a5d547adSRandall Stewart #ifdef INVARIANTS
59640696e120SRandall Stewart 				if (control->end_added &&
59650696e120SRandall Stewart 				    (control->data == NULL) &&
5966f8829a4aSRandall Stewart 				    (control->tail_mbuf == NULL)) {
5967f8829a4aSRandall Stewart 					panic("Gak, control->length is corrupt?");
5968f8829a4aSRandall Stewart 				}
5969f8829a4aSRandall Stewart #endif
5970f8829a4aSRandall Stewart 				no_rcv_needed = control->do_not_ref_stcb;
5971f8829a4aSRandall Stewart 				out_flags &= ~MSG_EOR;
5972f8829a4aSRandall Stewart 			}
5973f8829a4aSRandall Stewart 		}
5974f8829a4aSRandall Stewart 		if (out_flags & MSG_EOR) {
5975f8829a4aSRandall Stewart 			goto release;
5976f8829a4aSRandall Stewart 		}
5977f8829a4aSRandall Stewart 		if ((uio->uio_resid == 0) ||
597804aab884SMichael Tuexen 		    ((in_eeor_mode) &&
597904aab884SMichael Tuexen 		    (copied_so_far >= (uint32_t) max(so->so_rcv.sb_lowat, 1)))) {
5980f8829a4aSRandall Stewart 			goto release;
5981f8829a4aSRandall Stewart 		}
5982f8829a4aSRandall Stewart 		/*
5983f8829a4aSRandall Stewart 		 * If I hit here the receiver wants more and this message is
5984f8829a4aSRandall Stewart 		 * NOT done (pd-api). So two questions. Can we block? if not
5985f8829a4aSRandall Stewart 		 * we are done. Did the user NOT set MSG_WAITALL?
5986f8829a4aSRandall Stewart 		 */
5987f8829a4aSRandall Stewart 		if (block_allowed == 0) {
5988f8829a4aSRandall Stewart 			goto release;
5989f8829a4aSRandall Stewart 		}
5990f8829a4aSRandall Stewart 		/*
5991f8829a4aSRandall Stewart 		 * We need to wait for more data a few things: - We don't
5992f8829a4aSRandall Stewart 		 * sbunlock() so we don't get someone else reading. - We
5993f8829a4aSRandall Stewart 		 * must be sure to account for the case where what is added
5994f8829a4aSRandall Stewart 		 * is NOT to our control when we wakeup.
5995f8829a4aSRandall Stewart 		 */
5996f8829a4aSRandall Stewart 
5997f8829a4aSRandall Stewart 		/*
5998f8829a4aSRandall Stewart 		 * Do we need to tell the transport a rwnd update might be
5999f8829a4aSRandall Stewart 		 * needed before we go to sleep?
6000f8829a4aSRandall Stewart 		 */
6001f8829a4aSRandall Stewart 		if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
6002f8829a4aSRandall Stewart 		    ((freed_so_far >= rwnd_req) &&
6003f8829a4aSRandall Stewart 		    (control->do_not_ref_stcb == 0) &&
6004f8829a4aSRandall Stewart 		    (no_rcv_needed == 0))) {
6005f8829a4aSRandall Stewart 			sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6006f8829a4aSRandall Stewart 		}
6007f8829a4aSRandall Stewart wait_some_more:
600844b7479bSRandall Stewart 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
6009f8829a4aSRandall Stewart 			goto release;
6010f8829a4aSRandall Stewart 		}
6011f8829a4aSRandall Stewart 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)
6012f8829a4aSRandall Stewart 			goto release;
6013f8829a4aSRandall Stewart 
6014f8829a4aSRandall Stewart 		if (hold_rlock == 1) {
6015f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
6016f8829a4aSRandall Stewart 			hold_rlock = 0;
6017f8829a4aSRandall Stewart 		}
6018f8829a4aSRandall Stewart 		if (hold_sblock == 0) {
6019f8829a4aSRandall Stewart 			SOCKBUF_LOCK(&so->so_rcv);
6020f8829a4aSRandall Stewart 			hold_sblock = 1;
6021f8829a4aSRandall Stewart 		}
6022851b7298SRandall Stewart 		if ((copied_so_far) && (control->length == 0) &&
6023b5c16493SMichael Tuexen 		    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE))) {
6024851b7298SRandall Stewart 			goto release;
6025851b7298SRandall Stewart 		}
60264e88d37aSMichael Tuexen 		if (so->so_rcv.sb_cc <= control->held_length) {
6027f8829a4aSRandall Stewart 			error = sbwait(&so->so_rcv);
6028f8829a4aSRandall Stewart 			if (error) {
6029f8829a4aSRandall Stewart 				goto release;
6030f8829a4aSRandall Stewart 			}
6031f8829a4aSRandall Stewart 			control->held_length = 0;
6032f8829a4aSRandall Stewart 		}
6033f8829a4aSRandall Stewart 		if (hold_sblock) {
6034f8829a4aSRandall Stewart 			SOCKBUF_UNLOCK(&so->so_rcv);
6035f8829a4aSRandall Stewart 			hold_sblock = 0;
6036f8829a4aSRandall Stewart 		}
6037f8829a4aSRandall Stewart 		if (control->length == 0) {
6038f8829a4aSRandall Stewart 			/* still nothing here */
6039f8829a4aSRandall Stewart 			if (control->end_added == 1) {
6040f8829a4aSRandall Stewart 				/* he aborted, or is done i.e.did a shutdown */
6041f8829a4aSRandall Stewart 				out_flags |= MSG_EOR;
60429a6142d8SRandall Stewart 				if (control->pdapi_aborted) {
60436114cd96SRandall Stewart 					if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
6044ee7f9857SRandall Stewart 						control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
60459a6142d8SRandall Stewart 
604603b0b021SRandall Stewart 					out_flags |= MSG_TRUNC;
60479a6142d8SRandall Stewart 				} else {
60486114cd96SRandall Stewart 					if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
6049ee7f9857SRandall Stewart 						control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
60509a6142d8SRandall Stewart 				}
6051f8829a4aSRandall Stewart 				goto done_with_control;
6052f8829a4aSRandall Stewart 			}
60534e88d37aSMichael Tuexen 			if (so->so_rcv.sb_cc > held_length) {
60544e88d37aSMichael Tuexen 				control->held_length = so->so_rcv.sb_cc;
6055f8829a4aSRandall Stewart 				held_length = 0;
6056f8829a4aSRandall Stewart 			}
6057f8829a4aSRandall Stewart 			goto wait_some_more;
6058f8829a4aSRandall Stewart 		} else if (control->data == NULL) {
605950cec919SRandall Stewart 			/*
606050cec919SRandall Stewart 			 * we must re-sync since data is probably being
606150cec919SRandall Stewart 			 * added
606250cec919SRandall Stewart 			 */
606350cec919SRandall Stewart 			SCTP_INP_READ_LOCK(inp);
606450cec919SRandall Stewart 			if ((control->length > 0) && (control->data == NULL)) {
606550cec919SRandall Stewart 				/*
606650cec919SRandall Stewart 				 * big trouble.. we have the lock and its
606750cec919SRandall Stewart 				 * corrupt?
606850cec919SRandall Stewart 				 */
60699c04b296SRandall Stewart #ifdef INVARIANTS
6070f8829a4aSRandall Stewart 				panic("Impossible data==NULL length !=0");
60719c04b296SRandall Stewart #endif
60729c04b296SRandall Stewart 				out_flags |= MSG_EOR;
60739c04b296SRandall Stewart 				out_flags |= MSG_TRUNC;
60749c04b296SRandall Stewart 				control->length = 0;
60759c04b296SRandall Stewart 				SCTP_INP_READ_UNLOCK(inp);
60769c04b296SRandall Stewart 				goto done_with_control;
6077f8829a4aSRandall Stewart 			}
607850cec919SRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
607950cec919SRandall Stewart 			/* We will fall around to get more data */
608050cec919SRandall Stewart 		}
6081f8829a4aSRandall Stewart 		goto get_more_data;
6082f8829a4aSRandall Stewart 	} else {
608317205eccSRandall Stewart 		/*-
608417205eccSRandall Stewart 		 * Give caller back the mbuf chain,
608517205eccSRandall Stewart 		 * store in uio_resid the length
6086f8829a4aSRandall Stewart 		 */
608717205eccSRandall Stewart 		wakeup_read_socket = 0;
6088f8829a4aSRandall Stewart 		if ((control->end_added == 0) ||
6089f8829a4aSRandall Stewart 		    (TAILQ_NEXT(control, next) == NULL)) {
6090f8829a4aSRandall Stewart 			/* Need to get rlock */
6091f8829a4aSRandall Stewart 			if (hold_rlock == 0) {
6092f8829a4aSRandall Stewart 				SCTP_INP_READ_LOCK(inp);
6093f8829a4aSRandall Stewart 				hold_rlock = 1;
6094f8829a4aSRandall Stewart 			}
6095f8829a4aSRandall Stewart 		}
6096139bc87fSRandall Stewart 		if (control->end_added) {
6097f8829a4aSRandall Stewart 			out_flags |= MSG_EOR;
609860990c0cSMichael Tuexen 			if ((control->do_not_ref_stcb == 0) &&
609960990c0cSMichael Tuexen 			    (control->stcb != NULL) &&
610060990c0cSMichael Tuexen 			    ((control->spec_flags & M_NOTIFICATION) == 0))
6101ee7f9857SRandall Stewart 				control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6102f8829a4aSRandall Stewart 		}
6103139bc87fSRandall Stewart 		if (control->spec_flags & M_NOTIFICATION) {
6104f8829a4aSRandall Stewart 			out_flags |= MSG_NOTIFICATION;
6105f8829a4aSRandall Stewart 		}
610617205eccSRandall Stewart 		uio->uio_resid = control->length;
6107f8829a4aSRandall Stewart 		*mp = control->data;
6108f8829a4aSRandall Stewart 		m = control->data;
6109f8829a4aSRandall Stewart 		while (m) {
6110b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6111f8829a4aSRandall Stewart 				sctp_sblog(&so->so_rcv,
6112139bc87fSRandall Stewart 				    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
611380fefe0aSRandall Stewart 			}
6114f8829a4aSRandall Stewart 			sctp_sbfree(control, stcb, &so->so_rcv, m);
6115139bc87fSRandall Stewart 			freed_so_far += SCTP_BUF_LEN(m);
6116c4739e2fSRandall Stewart 			freed_so_far += MSIZE;
6117b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6118f8829a4aSRandall Stewart 				sctp_sblog(&so->so_rcv,
6119f8829a4aSRandall Stewart 				    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
612080fefe0aSRandall Stewart 			}
6121139bc87fSRandall Stewart 			m = SCTP_BUF_NEXT(m);
6122f8829a4aSRandall Stewart 		}
6123f8829a4aSRandall Stewart 		control->data = control->tail_mbuf = NULL;
6124f8829a4aSRandall Stewart 		control->length = 0;
6125f8829a4aSRandall Stewart 		if (out_flags & MSG_EOR) {
6126f8829a4aSRandall Stewart 			/* Done with this control */
6127f8829a4aSRandall Stewart 			goto done_with_control;
6128f8829a4aSRandall Stewart 		}
6129f8829a4aSRandall Stewart 	}
6130f8829a4aSRandall Stewart release:
6131f8829a4aSRandall Stewart 	if (hold_rlock == 1) {
6132f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
6133f8829a4aSRandall Stewart 		hold_rlock = 0;
6134f8829a4aSRandall Stewart 	}
61357abab911SRobert Watson 	if (hold_sblock == 1) {
61367abab911SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
61377abab911SRobert Watson 		hold_sblock = 0;
6138f8829a4aSRandall Stewart 	}
6139f8829a4aSRandall Stewart 	sbunlock(&so->so_rcv);
61407abab911SRobert Watson 	sockbuf_lock = 0;
6141f8829a4aSRandall Stewart 
6142f8829a4aSRandall Stewart release_unlocked:
6143f8829a4aSRandall Stewart 	if (hold_sblock) {
6144f8829a4aSRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
6145f8829a4aSRandall Stewart 		hold_sblock = 0;
6146f8829a4aSRandall Stewart 	}
6147f8829a4aSRandall Stewart 	if ((stcb) && (in_flags & MSG_PEEK) == 0) {
6148f8829a4aSRandall Stewart 		if ((freed_so_far >= rwnd_req) &&
6149f8829a4aSRandall Stewart 		    (control && (control->do_not_ref_stcb == 0)) &&
6150f8829a4aSRandall Stewart 		    (no_rcv_needed == 0))
6151f8829a4aSRandall Stewart 			sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6152f8829a4aSRandall Stewart 	}
6153f8829a4aSRandall Stewart out:
61541b9f62a0SRandall Stewart 	if (msg_flags) {
61551b9f62a0SRandall Stewart 		*msg_flags = out_flags;
61561b9f62a0SRandall Stewart 	}
61579a6142d8SRandall Stewart 	if (((out_flags & MSG_EOR) == 0) &&
61589a6142d8SRandall Stewart 	    ((in_flags & MSG_PEEK) == 0) &&
61599a6142d8SRandall Stewart 	    (sinfo) &&
6160e2e7c62eSMichael Tuexen 	    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
6161e2e7c62eSMichael Tuexen 	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO))) {
61629a6142d8SRandall Stewart 		struct sctp_extrcvinfo *s_extra;
61639a6142d8SRandall Stewart 
61649a6142d8SRandall Stewart 		s_extra = (struct sctp_extrcvinfo *)sinfo;
61659a6142d8SRandall Stewart 		s_extra->sreinfo_next_flags = SCTP_NO_NEXT_MSG;
61669a6142d8SRandall Stewart 	}
6167f8829a4aSRandall Stewart 	if (hold_rlock == 1) {
6168f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
6169f8829a4aSRandall Stewart 	}
6170f8829a4aSRandall Stewart 	if (hold_sblock) {
6171f8829a4aSRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
6172f8829a4aSRandall Stewart 	}
61737abab911SRobert Watson 	if (sockbuf_lock) {
61747abab911SRobert Watson 		sbunlock(&so->so_rcv);
61757abab911SRobert Watson 	}
617650cec919SRandall Stewart 	if (freecnt_applied) {
6177f8829a4aSRandall Stewart 		/*
6178f8829a4aSRandall Stewart 		 * The lock on the socket buffer protects us so the free
6179f8829a4aSRandall Stewart 		 * code will stop. But since we used the socketbuf lock and
6180f8829a4aSRandall Stewart 		 * the sender uses the tcb_lock to increment, we need to use
6181f8829a4aSRandall Stewart 		 * the atomic add to the refcnt.
6182f8829a4aSRandall Stewart 		 */
618350cec919SRandall Stewart 		if (stcb == NULL) {
6184df6e0cc3SRandall Stewart #ifdef INVARIANTS
618550cec919SRandall Stewart 			panic("stcb for refcnt has gone NULL?");
6186df6e0cc3SRandall Stewart 			goto stage_left;
6187df6e0cc3SRandall Stewart #else
6188df6e0cc3SRandall Stewart 			goto stage_left;
6189df6e0cc3SRandall Stewart #endif
619050cec919SRandall Stewart 		}
619150cec919SRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, -1);
6192f8829a4aSRandall Stewart 		/* Save the value back for next time */
6193f8829a4aSRandall Stewart 		stcb->freed_by_sorcv_sincelast = freed_so_far;
6194f8829a4aSRandall Stewart 	}
6195b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
6196f8829a4aSRandall Stewart 		if (stcb) {
6197f8829a4aSRandall Stewart 			sctp_misc_ints(SCTP_SORECV_DONE,
6198f8829a4aSRandall Stewart 			    freed_so_far,
6199f8829a4aSRandall Stewart 			    ((uio) ? (slen - uio->uio_resid) : slen),
6200f8829a4aSRandall Stewart 			    stcb->asoc.my_rwnd,
62014e88d37aSMichael Tuexen 			    so->so_rcv.sb_cc);
6202f8829a4aSRandall Stewart 		} else {
6203f8829a4aSRandall Stewart 			sctp_misc_ints(SCTP_SORECV_DONE,
6204f8829a4aSRandall Stewart 			    freed_so_far,
6205f8829a4aSRandall Stewart 			    ((uio) ? (slen - uio->uio_resid) : slen),
6206f8829a4aSRandall Stewart 			    0,
62074e88d37aSMichael Tuexen 			    so->so_rcv.sb_cc);
6208f8829a4aSRandall Stewart 		}
620980fefe0aSRandall Stewart 	}
6210df6e0cc3SRandall Stewart stage_left:
6211f8829a4aSRandall Stewart 	if (wakeup_read_socket) {
6212f8829a4aSRandall Stewart 		sctp_sorwakeup(inp, so);
6213f8829a4aSRandall Stewart 	}
6214f8829a4aSRandall Stewart 	return (error);
6215f8829a4aSRandall Stewart }
6216f8829a4aSRandall Stewart 
6217f8829a4aSRandall Stewart 
6218f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
6219f8829a4aSRandall Stewart struct mbuf *
6220f8829a4aSRandall Stewart sctp_m_free(struct mbuf *m)
6221f8829a4aSRandall Stewart {
6222b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6223f8829a4aSRandall Stewart 		sctp_log_mb(m, SCTP_MBUF_IFREE);
6224f8829a4aSRandall Stewart 	}
6225f8829a4aSRandall Stewart 	return (m_free(m));
6226f8829a4aSRandall Stewart }
6227f8829a4aSRandall Stewart 
6228f8829a4aSRandall Stewart void
6229f8829a4aSRandall Stewart sctp_m_freem(struct mbuf *mb)
6230f8829a4aSRandall Stewart {
6231f8829a4aSRandall Stewart 	while (mb != NULL)
6232f8829a4aSRandall Stewart 		mb = sctp_m_free(mb);
6233f8829a4aSRandall Stewart }
6234f8829a4aSRandall Stewart 
6235f8829a4aSRandall Stewart #endif
6236f8829a4aSRandall Stewart 
623742551e99SRandall Stewart int
623842551e99SRandall Stewart sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id)
623942551e99SRandall Stewart {
624042551e99SRandall Stewart 	/*
624142551e99SRandall Stewart 	 * Given a local address. For all associations that holds the
624242551e99SRandall Stewart 	 * address, request a peer-set-primary.
624342551e99SRandall Stewart 	 */
624442551e99SRandall Stewart 	struct sctp_ifa *ifa;
624542551e99SRandall Stewart 	struct sctp_laddr *wi;
624642551e99SRandall Stewart 
624742551e99SRandall Stewart 	ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0);
624842551e99SRandall Stewart 	if (ifa == NULL) {
6249c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EADDRNOTAVAIL);
625042551e99SRandall Stewart 		return (EADDRNOTAVAIL);
625142551e99SRandall Stewart 	}
625242551e99SRandall Stewart 	/*
625342551e99SRandall Stewart 	 * Now that we have the ifa we must awaken the iterator with this
625442551e99SRandall Stewart 	 * message.
625542551e99SRandall Stewart 	 */
6256b3f1ea41SRandall Stewart 	wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
625742551e99SRandall Stewart 	if (wi == NULL) {
6258c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
625942551e99SRandall Stewart 		return (ENOMEM);
626042551e99SRandall Stewart 	}
626142551e99SRandall Stewart 	/* Now incr the count and int wi structure */
626242551e99SRandall Stewart 	SCTP_INCR_LADDR_COUNT();
626342551e99SRandall Stewart 	bzero(wi, sizeof(*wi));
6264d61a0ae0SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
626542551e99SRandall Stewart 	wi->ifa = ifa;
626642551e99SRandall Stewart 	wi->action = SCTP_SET_PRIM_ADDR;
626742551e99SRandall Stewart 	atomic_add_int(&ifa->refcount, 1);
626842551e99SRandall Stewart 
626942551e99SRandall Stewart 	/* Now add it to the work queue */
6270f7517433SRandall Stewart 	SCTP_WQ_ADDR_LOCK();
627142551e99SRandall Stewart 	/*
627242551e99SRandall Stewart 	 * Should this really be a tailq? As it is we will process the
627342551e99SRandall Stewart 	 * newest first :-0
627442551e99SRandall Stewart 	 */
6275b3f1ea41SRandall Stewart 	LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
6276f7517433SRandall Stewart 	SCTP_WQ_ADDR_UNLOCK();
627742551e99SRandall Stewart 	sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
627842551e99SRandall Stewart 	    (struct sctp_inpcb *)NULL,
627942551e99SRandall Stewart 	    (struct sctp_tcb *)NULL,
628042551e99SRandall Stewart 	    (struct sctp_nets *)NULL);
628142551e99SRandall Stewart 	return (0);
628242551e99SRandall Stewart }
628342551e99SRandall Stewart 
628442551e99SRandall Stewart 
6285f8829a4aSRandall Stewart int
628617205eccSRandall Stewart sctp_soreceive(struct socket *so,
628717205eccSRandall Stewart     struct sockaddr **psa,
628817205eccSRandall Stewart     struct uio *uio,
628917205eccSRandall Stewart     struct mbuf **mp0,
629017205eccSRandall Stewart     struct mbuf **controlp,
629117205eccSRandall Stewart     int *flagsp)
6292f8829a4aSRandall Stewart {
6293f8829a4aSRandall Stewart 	int error, fromlen;
6294f8829a4aSRandall Stewart 	uint8_t sockbuf[256];
6295f8829a4aSRandall Stewart 	struct sockaddr *from;
6296f8829a4aSRandall Stewart 	struct sctp_extrcvinfo sinfo;
6297f8829a4aSRandall Stewart 	int filling_sinfo = 1;
6298f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
6299f8829a4aSRandall Stewart 
6300f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
6301f8829a4aSRandall Stewart 	/* pickup the assoc we are reading from */
6302f8829a4aSRandall Stewart 	if (inp == NULL) {
6303c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6304f8829a4aSRandall Stewart 		return (EINVAL);
6305f8829a4aSRandall Stewart 	}
6306e2e7c62eSMichael Tuexen 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) &&
6307e2e7c62eSMichael Tuexen 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
6308e2e7c62eSMichael Tuexen 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) ||
6309f8829a4aSRandall Stewart 	    (controlp == NULL)) {
6310f8829a4aSRandall Stewart 		/* user does not want the sndrcv ctl */
6311f8829a4aSRandall Stewart 		filling_sinfo = 0;
6312f8829a4aSRandall Stewart 	}
6313f8829a4aSRandall Stewart 	if (psa) {
6314f8829a4aSRandall Stewart 		from = (struct sockaddr *)sockbuf;
6315f8829a4aSRandall Stewart 		fromlen = sizeof(sockbuf);
6316f8829a4aSRandall Stewart 		from->sa_len = 0;
6317f8829a4aSRandall Stewart 	} else {
6318f8829a4aSRandall Stewart 		from = NULL;
6319f8829a4aSRandall Stewart 		fromlen = 0;
6320f8829a4aSRandall Stewart 	}
6321f8829a4aSRandall Stewart 
6322e432298aSXin LI 	if (filling_sinfo) {
6323e432298aSXin LI 		memset(&sinfo, 0, sizeof(struct sctp_extrcvinfo));
6324e432298aSXin LI 	}
6325f8829a4aSRandall Stewart 	error = sctp_sorecvmsg(so, uio, mp0, from, fromlen, flagsp,
6326f8829a4aSRandall Stewart 	    (struct sctp_sndrcvinfo *)&sinfo, filling_sinfo);
6327e432298aSXin LI 	if (controlp != NULL) {
6328f8829a4aSRandall Stewart 		/* copy back the sinfo in a CMSG format */
6329f8829a4aSRandall Stewart 		if (filling_sinfo)
6330f8829a4aSRandall Stewart 			*controlp = sctp_build_ctl_nchunk(inp,
6331f8829a4aSRandall Stewart 			    (struct sctp_sndrcvinfo *)&sinfo);
6332f8829a4aSRandall Stewart 		else
6333f8829a4aSRandall Stewart 			*controlp = NULL;
6334f8829a4aSRandall Stewart 	}
6335f8829a4aSRandall Stewart 	if (psa) {
6336f8829a4aSRandall Stewart 		/* copy back the address info */
6337f8829a4aSRandall Stewart 		if (from && from->sa_len) {
6338f8829a4aSRandall Stewart 			*psa = sodupsockaddr(from, M_NOWAIT);
6339f8829a4aSRandall Stewart 		} else {
6340f8829a4aSRandall Stewart 			*psa = NULL;
6341f8829a4aSRandall Stewart 		}
6342f8829a4aSRandall Stewart 	}
6343f8829a4aSRandall Stewart 	return (error);
6344f8829a4aSRandall Stewart }
634517205eccSRandall Stewart 
634617205eccSRandall Stewart 
634717205eccSRandall Stewart 
634817205eccSRandall Stewart 
634917205eccSRandall Stewart 
635017205eccSRandall Stewart int
6351d61a0ae0SRandall Stewart sctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr,
6352d61a0ae0SRandall Stewart     int totaddr, int *error)
635317205eccSRandall Stewart {
635417205eccSRandall Stewart 	int added = 0;
635517205eccSRandall Stewart 	int i;
635617205eccSRandall Stewart 	struct sctp_inpcb *inp;
635717205eccSRandall Stewart 	struct sockaddr *sa;
635817205eccSRandall Stewart 	size_t incr = 0;
635917205eccSRandall Stewart 
636092776dfdSMichael Tuexen #ifdef INET
636192776dfdSMichael Tuexen 	struct sockaddr_in *sin;
636292776dfdSMichael Tuexen 
636392776dfdSMichael Tuexen #endif
636492776dfdSMichael Tuexen #ifdef INET6
636592776dfdSMichael Tuexen 	struct sockaddr_in6 *sin6;
636692776dfdSMichael Tuexen 
636792776dfdSMichael Tuexen #endif
636892776dfdSMichael Tuexen 
636917205eccSRandall Stewart 	sa = addr;
637017205eccSRandall Stewart 	inp = stcb->sctp_ep;
637117205eccSRandall Stewart 	*error = 0;
637217205eccSRandall Stewart 	for (i = 0; i < totaddr; i++) {
6373ea5eba11SMichael Tuexen 		switch (sa->sa_family) {
6374ea5eba11SMichael Tuexen #ifdef INET
6375ea5eba11SMichael Tuexen 		case AF_INET:
637617205eccSRandall Stewart 			incr = sizeof(struct sockaddr_in);
637792776dfdSMichael Tuexen 			sin = (struct sockaddr_in *)sa;
637892776dfdSMichael Tuexen 			if ((sin->sin_addr.s_addr == INADDR_ANY) ||
637992776dfdSMichael Tuexen 			    (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
638092776dfdSMichael Tuexen 			    IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
638192776dfdSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6382ba785902SMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6383ba785902SMichael Tuexen 				    SCTP_FROM_SCTPUTIL + SCTP_LOC_7);
638492776dfdSMichael Tuexen 				*error = EINVAL;
638592776dfdSMichael Tuexen 				goto out_now;
638692776dfdSMichael Tuexen 			}
6387ca85e948SMichael Tuexen 			if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
638817205eccSRandall Stewart 				/* assoc gone no un-lock */
6389c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6390ba785902SMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6391ba785902SMichael Tuexen 				    SCTP_FROM_SCTPUTIL + SCTP_LOC_8);
639217205eccSRandall Stewart 				*error = ENOBUFS;
639317205eccSRandall Stewart 				goto out_now;
639417205eccSRandall Stewart 			}
639517205eccSRandall Stewart 			added++;
6396ea5eba11SMichael Tuexen 			break;
6397ea5eba11SMichael Tuexen #endif
6398ea5eba11SMichael Tuexen #ifdef INET6
6399ea5eba11SMichael Tuexen 		case AF_INET6:
640017205eccSRandall Stewart 			incr = sizeof(struct sockaddr_in6);
640192776dfdSMichael Tuexen 			sin6 = (struct sockaddr_in6 *)sa;
640292776dfdSMichael Tuexen 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
640392776dfdSMichael Tuexen 			    IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
640492776dfdSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6405ba785902SMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6406ba785902SMichael Tuexen 				    SCTP_FROM_SCTPUTIL + SCTP_LOC_9);
640792776dfdSMichael Tuexen 				*error = EINVAL;
640892776dfdSMichael Tuexen 				goto out_now;
640992776dfdSMichael Tuexen 			}
6410ca85e948SMichael Tuexen 			if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
641117205eccSRandall Stewart 				/* assoc gone no un-lock */
6412c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6413ba785902SMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
6414ba785902SMichael Tuexen 				    SCTP_FROM_SCTPUTIL + SCTP_LOC_10);
641517205eccSRandall Stewart 				*error = ENOBUFS;
641617205eccSRandall Stewart 				goto out_now;
641717205eccSRandall Stewart 			}
641817205eccSRandall Stewart 			added++;
6419ea5eba11SMichael Tuexen 			break;
6420ea5eba11SMichael Tuexen #endif
6421ea5eba11SMichael Tuexen 		default:
6422ea5eba11SMichael Tuexen 			break;
642317205eccSRandall Stewart 		}
642417205eccSRandall Stewart 		sa = (struct sockaddr *)((caddr_t)sa + incr);
642517205eccSRandall Stewart 	}
642617205eccSRandall Stewart out_now:
642717205eccSRandall Stewart 	return (added);
642817205eccSRandall Stewart }
642917205eccSRandall Stewart 
643017205eccSRandall Stewart struct sctp_tcb *
6431d61a0ae0SRandall Stewart sctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr,
6432d61a0ae0SRandall Stewart     int *totaddr, int *num_v4, int *num_v6, int *error,
6433d61a0ae0SRandall Stewart     int limit, int *bad_addr)
643417205eccSRandall Stewart {
643517205eccSRandall Stewart 	struct sockaddr *sa;
643617205eccSRandall Stewart 	struct sctp_tcb *stcb = NULL;
643717205eccSRandall Stewart 	size_t incr, at, i;
643817205eccSRandall Stewart 
643917205eccSRandall Stewart 	at = incr = 0;
644017205eccSRandall Stewart 	sa = addr;
6441ea5eba11SMichael Tuexen 
644217205eccSRandall Stewart 	*error = *num_v6 = *num_v4 = 0;
644317205eccSRandall Stewart 	/* account and validate addresses */
64444c9179adSRandall Stewart 	for (i = 0; i < (size_t)*totaddr; i++) {
6445ea5eba11SMichael Tuexen 		switch (sa->sa_family) {
6446ea5eba11SMichael Tuexen #ifdef INET
6447ea5eba11SMichael Tuexen 		case AF_INET:
644817205eccSRandall Stewart 			(*num_v4) += 1;
644917205eccSRandall Stewart 			incr = sizeof(struct sockaddr_in);
6450d61a0ae0SRandall Stewart 			if (sa->sa_len != incr) {
6451c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6452d61a0ae0SRandall Stewart 				*error = EINVAL;
6453d61a0ae0SRandall Stewart 				*bad_addr = 1;
6454d61a0ae0SRandall Stewart 				return (NULL);
6455d61a0ae0SRandall Stewart 			}
6456ea5eba11SMichael Tuexen 			break;
6457ea5eba11SMichael Tuexen #endif
6458ea5eba11SMichael Tuexen #ifdef INET6
6459ea5eba11SMichael Tuexen 		case AF_INET6:
6460ea5eba11SMichael Tuexen 			{
646117205eccSRandall Stewart 				struct sockaddr_in6 *sin6;
646217205eccSRandall Stewart 
646317205eccSRandall Stewart 				sin6 = (struct sockaddr_in6 *)sa;
646417205eccSRandall Stewart 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
646517205eccSRandall Stewart 					/* Must be non-mapped for connectx */
6466c4739e2fSRandall Stewart 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
646717205eccSRandall Stewart 					*error = EINVAL;
6468d61a0ae0SRandall Stewart 					*bad_addr = 1;
646917205eccSRandall Stewart 					return (NULL);
647017205eccSRandall Stewart 				}
647117205eccSRandall Stewart 				(*num_v6) += 1;
647217205eccSRandall Stewart 				incr = sizeof(struct sockaddr_in6);
6473d61a0ae0SRandall Stewart 				if (sa->sa_len != incr) {
6474c4739e2fSRandall Stewart 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6475d61a0ae0SRandall Stewart 					*error = EINVAL;
6476d61a0ae0SRandall Stewart 					*bad_addr = 1;
6477d61a0ae0SRandall Stewart 					return (NULL);
6478d61a0ae0SRandall Stewart 				}
6479ea5eba11SMichael Tuexen 				break;
6480ea5eba11SMichael Tuexen 			}
6481ea5eba11SMichael Tuexen #endif
6482ea5eba11SMichael Tuexen 		default:
648317205eccSRandall Stewart 			*totaddr = i;
648417205eccSRandall Stewart 			/* we are done */
648517205eccSRandall Stewart 			break;
648617205eccSRandall Stewart 		}
6487ea5eba11SMichael Tuexen 		if (i == (size_t)*totaddr) {
6488ea5eba11SMichael Tuexen 			break;
6489ea5eba11SMichael Tuexen 		}
6490d61a0ae0SRandall Stewart 		SCTP_INP_INCR_REF(inp);
649117205eccSRandall Stewart 		stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
649217205eccSRandall Stewart 		if (stcb != NULL) {
649317205eccSRandall Stewart 			/* Already have or am bring up an association */
649417205eccSRandall Stewart 			return (stcb);
6495d61a0ae0SRandall Stewart 		} else {
6496d61a0ae0SRandall Stewart 			SCTP_INP_DECR_REF(inp);
649717205eccSRandall Stewart 		}
64984c9179adSRandall Stewart 		if ((at + incr) > (size_t)limit) {
649917205eccSRandall Stewart 			*totaddr = i;
650017205eccSRandall Stewart 			break;
650117205eccSRandall Stewart 		}
650217205eccSRandall Stewart 		sa = (struct sockaddr *)((caddr_t)sa + incr);
650317205eccSRandall Stewart 	}
650417205eccSRandall Stewart 	return ((struct sctp_tcb *)NULL);
650517205eccSRandall Stewart }
650635918f85SRandall Stewart 
650735918f85SRandall Stewart /*
650835918f85SRandall Stewart  * sctp_bindx(ADD) for one address.
650935918f85SRandall Stewart  * assumes all arguments are valid/checked by caller.
651035918f85SRandall Stewart  */
651135918f85SRandall Stewart void
651235918f85SRandall Stewart sctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp,
651335918f85SRandall Stewart     struct sockaddr *sa, sctp_assoc_t assoc_id,
651435918f85SRandall Stewart     uint32_t vrf_id, int *error, void *p)
651535918f85SRandall Stewart {
651635918f85SRandall Stewart 	struct sockaddr *addr_touse;
65175e2c2d87SRandall Stewart 
6518d59107f7SMichael Tuexen #if defined(INET) && defined(INET6)
651935918f85SRandall Stewart 	struct sockaddr_in sin;
652035918f85SRandall Stewart 
65215e2c2d87SRandall Stewart #endif
65225e2c2d87SRandall Stewart 
652335918f85SRandall Stewart 	/* see if we're bound all already! */
652435918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6525c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
652635918f85SRandall Stewart 		*error = EINVAL;
652735918f85SRandall Stewart 		return;
652835918f85SRandall Stewart 	}
652935918f85SRandall Stewart 	addr_touse = sa;
6530ea5eba11SMichael Tuexen #ifdef INET6
653135918f85SRandall Stewart 	if (sa->sa_family == AF_INET6) {
6532d59107f7SMichael Tuexen #ifdef INET
653335918f85SRandall Stewart 		struct sockaddr_in6 *sin6;
653435918f85SRandall Stewart 
6535d59107f7SMichael Tuexen #endif
653635918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6537c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
653835918f85SRandall Stewart 			*error = EINVAL;
653935918f85SRandall Stewart 			return;
654035918f85SRandall Stewart 		}
6541db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6542db4fd95bSRandall Stewart 			/* can only bind v6 on PF_INET6 sockets */
6543c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6544db4fd95bSRandall Stewart 			*error = EINVAL;
6545db4fd95bSRandall Stewart 			return;
6546db4fd95bSRandall Stewart 		}
6547d59107f7SMichael Tuexen #ifdef INET
654835918f85SRandall Stewart 		sin6 = (struct sockaddr_in6 *)addr_touse;
654935918f85SRandall Stewart 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6550db4fd95bSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6551db4fd95bSRandall Stewart 			    SCTP_IPV6_V6ONLY(inp)) {
6552db4fd95bSRandall Stewart 				/* can't bind v4-mapped on PF_INET sockets */
6553c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6554db4fd95bSRandall Stewart 				*error = EINVAL;
6555db4fd95bSRandall Stewart 				return;
6556db4fd95bSRandall Stewart 			}
655735918f85SRandall Stewart 			in6_sin6_2_sin(&sin, sin6);
655835918f85SRandall Stewart 			addr_touse = (struct sockaddr *)&sin;
655935918f85SRandall Stewart 		}
6560d59107f7SMichael Tuexen #endif
656135918f85SRandall Stewart 	}
656235918f85SRandall Stewart #endif
6563ea5eba11SMichael Tuexen #ifdef INET
656435918f85SRandall Stewart 	if (sa->sa_family == AF_INET) {
656535918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in)) {
6566c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
656735918f85SRandall Stewart 			*error = EINVAL;
656835918f85SRandall Stewart 			return;
656935918f85SRandall Stewart 		}
6570db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6571db4fd95bSRandall Stewart 		    SCTP_IPV6_V6ONLY(inp)) {
6572db4fd95bSRandall Stewart 			/* can't bind v4 on PF_INET sockets */
6573c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6574db4fd95bSRandall Stewart 			*error = EINVAL;
6575db4fd95bSRandall Stewart 			return;
6576db4fd95bSRandall Stewart 		}
657735918f85SRandall Stewart 	}
6578ea5eba11SMichael Tuexen #endif
657935918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
658035918f85SRandall Stewart 		if (p == NULL) {
658135918f85SRandall Stewart 			/* Can't get proc for Net/Open BSD */
6582c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
658335918f85SRandall Stewart 			*error = EINVAL;
658435918f85SRandall Stewart 			return;
658535918f85SRandall Stewart 		}
65861b649582SRandall Stewart 		*error = sctp_inpcb_bind(so, addr_touse, NULL, p);
658735918f85SRandall Stewart 		return;
658835918f85SRandall Stewart 	}
658935918f85SRandall Stewart 	/*
659035918f85SRandall Stewart 	 * No locks required here since bind and mgmt_ep_sa all do their own
659135918f85SRandall Stewart 	 * locking. If we do something for the FIX: below we may need to
659235918f85SRandall Stewart 	 * lock in that case.
659335918f85SRandall Stewart 	 */
659435918f85SRandall Stewart 	if (assoc_id == 0) {
659535918f85SRandall Stewart 		/* add the address */
659635918f85SRandall Stewart 		struct sctp_inpcb *lep;
659797c76f10SRandall Stewart 		struct sockaddr_in *lsin = (struct sockaddr_in *)addr_touse;
659835918f85SRandall Stewart 
659997c76f10SRandall Stewart 		/* validate the incoming port */
660097c76f10SRandall Stewart 		if ((lsin->sin_port != 0) &&
660197c76f10SRandall Stewart 		    (lsin->sin_port != inp->sctp_lport)) {
6602c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
660397c76f10SRandall Stewart 			*error = EINVAL;
660497c76f10SRandall Stewart 			return;
660597c76f10SRandall Stewart 		} else {
660697c76f10SRandall Stewart 			/* user specified 0 port, set it to existing port */
660797c76f10SRandall Stewart 			lsin->sin_port = inp->sctp_lport;
660897c76f10SRandall Stewart 		}
660997c76f10SRandall Stewart 
661035918f85SRandall Stewart 		lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id);
661135918f85SRandall Stewart 		if (lep != NULL) {
661235918f85SRandall Stewart 			/*
661335918f85SRandall Stewart 			 * We must decrement the refcount since we have the
661435918f85SRandall Stewart 			 * ep already and are binding. No remove going on
661535918f85SRandall Stewart 			 * here.
661635918f85SRandall Stewart 			 */
66176d9e8f2bSRandall Stewart 			SCTP_INP_DECR_REF(lep);
661835918f85SRandall Stewart 		}
661935918f85SRandall Stewart 		if (lep == inp) {
662035918f85SRandall Stewart 			/* already bound to it.. ok */
662135918f85SRandall Stewart 			return;
662235918f85SRandall Stewart 		} else if (lep == NULL) {
662335918f85SRandall Stewart 			((struct sockaddr_in *)addr_touse)->sin_port = 0;
662435918f85SRandall Stewart 			*error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
662535918f85SRandall Stewart 			    SCTP_ADD_IP_ADDRESS,
662680fefe0aSRandall Stewart 			    vrf_id, NULL);
662735918f85SRandall Stewart 		} else {
662835918f85SRandall Stewart 			*error = EADDRINUSE;
662935918f85SRandall Stewart 		}
663035918f85SRandall Stewart 		if (*error)
663135918f85SRandall Stewart 			return;
663235918f85SRandall Stewart 	} else {
663335918f85SRandall Stewart 		/*
663435918f85SRandall Stewart 		 * FIX: decide whether we allow assoc based bindx
663535918f85SRandall Stewart 		 */
663635918f85SRandall Stewart 	}
663735918f85SRandall Stewart }
663835918f85SRandall Stewart 
663935918f85SRandall Stewart /*
664035918f85SRandall Stewart  * sctp_bindx(DELETE) for one address.
664135918f85SRandall Stewart  * assumes all arguments are valid/checked by caller.
664235918f85SRandall Stewart  */
664335918f85SRandall Stewart void
66447215cc1bSMichael Tuexen sctp_bindx_delete_address(struct sctp_inpcb *inp,
664535918f85SRandall Stewart     struct sockaddr *sa, sctp_assoc_t assoc_id,
664635918f85SRandall Stewart     uint32_t vrf_id, int *error)
664735918f85SRandall Stewart {
664835918f85SRandall Stewart 	struct sockaddr *addr_touse;
66495e2c2d87SRandall Stewart 
6650d59107f7SMichael Tuexen #if defined(INET) && defined(INET6)
665135918f85SRandall Stewart 	struct sockaddr_in sin;
665235918f85SRandall Stewart 
66535e2c2d87SRandall Stewart #endif
66545e2c2d87SRandall Stewart 
665535918f85SRandall Stewart 	/* see if we're bound all already! */
665635918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6657c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
665835918f85SRandall Stewart 		*error = EINVAL;
665935918f85SRandall Stewart 		return;
666035918f85SRandall Stewart 	}
666135918f85SRandall Stewart 	addr_touse = sa;
6662e0e00a4dSMichael Tuexen #ifdef INET6
666335918f85SRandall Stewart 	if (sa->sa_family == AF_INET6) {
6664d59107f7SMichael Tuexen #ifdef INET
666535918f85SRandall Stewart 		struct sockaddr_in6 *sin6;
666635918f85SRandall Stewart 
6667d59107f7SMichael Tuexen #endif
6668d59107f7SMichael Tuexen 
666935918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6670c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
667135918f85SRandall Stewart 			*error = EINVAL;
667235918f85SRandall Stewart 			return;
667335918f85SRandall Stewart 		}
6674db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6675db4fd95bSRandall Stewart 			/* can only bind v6 on PF_INET6 sockets */
6676c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6677db4fd95bSRandall Stewart 			*error = EINVAL;
6678db4fd95bSRandall Stewart 			return;
6679db4fd95bSRandall Stewart 		}
6680d59107f7SMichael Tuexen #ifdef INET
668135918f85SRandall Stewart 		sin6 = (struct sockaddr_in6 *)addr_touse;
668235918f85SRandall Stewart 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6683db4fd95bSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6684db4fd95bSRandall Stewart 			    SCTP_IPV6_V6ONLY(inp)) {
6685db4fd95bSRandall Stewart 				/* can't bind mapped-v4 on PF_INET sockets */
6686c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6687db4fd95bSRandall Stewart 				*error = EINVAL;
6688db4fd95bSRandall Stewart 				return;
6689db4fd95bSRandall Stewart 			}
669035918f85SRandall Stewart 			in6_sin6_2_sin(&sin, sin6);
669135918f85SRandall Stewart 			addr_touse = (struct sockaddr *)&sin;
669235918f85SRandall Stewart 		}
6693d59107f7SMichael Tuexen #endif
669435918f85SRandall Stewart 	}
669535918f85SRandall Stewart #endif
6696ea5eba11SMichael Tuexen #ifdef INET
669735918f85SRandall Stewart 	if (sa->sa_family == AF_INET) {
669835918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in)) {
6699c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
670035918f85SRandall Stewart 			*error = EINVAL;
670135918f85SRandall Stewart 			return;
670235918f85SRandall Stewart 		}
6703db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6704db4fd95bSRandall Stewart 		    SCTP_IPV6_V6ONLY(inp)) {
6705db4fd95bSRandall Stewart 			/* can't bind v4 on PF_INET sockets */
6706c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6707db4fd95bSRandall Stewart 			*error = EINVAL;
6708db4fd95bSRandall Stewart 			return;
6709db4fd95bSRandall Stewart 		}
671035918f85SRandall Stewart 	}
6711ea5eba11SMichael Tuexen #endif
671235918f85SRandall Stewart 	/*
671335918f85SRandall Stewart 	 * No lock required mgmt_ep_sa does its own locking. If the FIX:
671435918f85SRandall Stewart 	 * below is ever changed we may need to lock before calling
671535918f85SRandall Stewart 	 * association level binding.
671635918f85SRandall Stewart 	 */
671735918f85SRandall Stewart 	if (assoc_id == 0) {
671835918f85SRandall Stewart 		/* delete the address */
671935918f85SRandall Stewart 		*error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
672035918f85SRandall Stewart 		    SCTP_DEL_IP_ADDRESS,
672180fefe0aSRandall Stewart 		    vrf_id, NULL);
672235918f85SRandall Stewart 	} else {
672335918f85SRandall Stewart 		/*
672435918f85SRandall Stewart 		 * FIX: decide whether we allow assoc based bindx
672535918f85SRandall Stewart 		 */
672635918f85SRandall Stewart 	}
672735918f85SRandall Stewart }
67281b649582SRandall Stewart 
67291b649582SRandall Stewart /*
67301b649582SRandall Stewart  * returns the valid local address count for an assoc, taking into account
67311b649582SRandall Stewart  * all scoping rules
67321b649582SRandall Stewart  */
67331b649582SRandall Stewart int
67341b649582SRandall Stewart sctp_local_addr_count(struct sctp_tcb *stcb)
67351b649582SRandall Stewart {
6736b54ddf22SMichael Tuexen 	int loopback_scope;
6737b54ddf22SMichael Tuexen 
6738b54ddf22SMichael Tuexen #if defined(INET)
6739b54ddf22SMichael Tuexen 	int ipv4_local_scope, ipv4_addr_legal;
6740b54ddf22SMichael Tuexen 
6741b54ddf22SMichael Tuexen #endif
6742b54ddf22SMichael Tuexen #if defined (INET6)
6743b54ddf22SMichael Tuexen 	int local_scope, site_scope, ipv6_addr_legal;
6744b54ddf22SMichael Tuexen 
6745b54ddf22SMichael Tuexen #endif
67461b649582SRandall Stewart 	struct sctp_vrf *vrf;
67471b649582SRandall Stewart 	struct sctp_ifn *sctp_ifn;
67481b649582SRandall Stewart 	struct sctp_ifa *sctp_ifa;
67491b649582SRandall Stewart 	int count = 0;
67501b649582SRandall Stewart 
67511b649582SRandall Stewart 	/* Turn on all the appropriate scopes */
6752a1cb341bSMichael Tuexen 	loopback_scope = stcb->asoc.scope.loopback_scope;
6753b54ddf22SMichael Tuexen #if defined(INET)
6754a1cb341bSMichael Tuexen 	ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
6755b54ddf22SMichael Tuexen 	ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
6756b54ddf22SMichael Tuexen #endif
6757b54ddf22SMichael Tuexen #if defined(INET6)
6758a1cb341bSMichael Tuexen 	local_scope = stcb->asoc.scope.local_scope;
6759a1cb341bSMichael Tuexen 	site_scope = stcb->asoc.scope.site_scope;
6760a1cb341bSMichael Tuexen 	ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
6761b54ddf22SMichael Tuexen #endif
6762c99efcf6SRandall Stewart 	SCTP_IPI_ADDR_RLOCK();
67631b649582SRandall Stewart 	vrf = sctp_find_vrf(stcb->asoc.vrf_id);
67641b649582SRandall Stewart 	if (vrf == NULL) {
67651b649582SRandall Stewart 		/* no vrf, no addresses */
6766c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RUNLOCK();
67671b649582SRandall Stewart 		return (0);
67681b649582SRandall Stewart 	}
67691b649582SRandall Stewart 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
67701b649582SRandall Stewart 		/*
67711b649582SRandall Stewart 		 * bound all case: go through all ifns on the vrf
67721b649582SRandall Stewart 		 */
67731b649582SRandall Stewart 		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
67741b649582SRandall Stewart 			if ((loopback_scope == 0) &&
67751b649582SRandall Stewart 			    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
67761b649582SRandall Stewart 				continue;
67771b649582SRandall Stewart 			}
67781b649582SRandall Stewart 			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
67791b649582SRandall Stewart 				if (sctp_is_addr_restricted(stcb, sctp_ifa))
67801b649582SRandall Stewart 					continue;
67815e2c2d87SRandall Stewart 				switch (sctp_ifa->address.sa.sa_family) {
6782ea5eba11SMichael Tuexen #ifdef INET
67835e2c2d87SRandall Stewart 				case AF_INET:
67845e2c2d87SRandall Stewart 					if (ipv4_addr_legal) {
67851b649582SRandall Stewart 						struct sockaddr_in *sin;
67861b649582SRandall Stewart 
678724aaac8dSMichael Tuexen 						sin = &sctp_ifa->address.sin;
67881b649582SRandall Stewart 						if (sin->sin_addr.s_addr == 0) {
67895e2c2d87SRandall Stewart 							/*
67905e2c2d87SRandall Stewart 							 * skip unspecified
67915e2c2d87SRandall Stewart 							 * addrs
67925e2c2d87SRandall Stewart 							 */
67931b649582SRandall Stewart 							continue;
67941b649582SRandall Stewart 						}
67956ba22f19SMichael Tuexen 						if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
67966ba22f19SMichael Tuexen 						    &sin->sin_addr) != 0) {
67976ba22f19SMichael Tuexen 							continue;
67986ba22f19SMichael Tuexen 						}
67991b649582SRandall Stewart 						if ((ipv4_local_scope == 0) &&
68001b649582SRandall Stewart 						    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
68011b649582SRandall Stewart 							continue;
68021b649582SRandall Stewart 						}
68031b649582SRandall Stewart 						/* count this one */
68041b649582SRandall Stewart 						count++;
68055e2c2d87SRandall Stewart 					} else {
68065e2c2d87SRandall Stewart 						continue;
68075e2c2d87SRandall Stewart 					}
68085e2c2d87SRandall Stewart 					break;
6809ea5eba11SMichael Tuexen #endif
68105e2c2d87SRandall Stewart #ifdef INET6
68115e2c2d87SRandall Stewart 				case AF_INET6:
68125e2c2d87SRandall Stewart 					if (ipv6_addr_legal) {
68131b649582SRandall Stewart 						struct sockaddr_in6 *sin6;
68141b649582SRandall Stewart 
681524aaac8dSMichael Tuexen 						sin6 = &sctp_ifa->address.sin6;
68161b649582SRandall Stewart 						if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
68171b649582SRandall Stewart 							continue;
68181b649582SRandall Stewart 						}
68196ba22f19SMichael Tuexen 						if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
68206ba22f19SMichael Tuexen 						    &sin6->sin6_addr) != 0) {
68216ba22f19SMichael Tuexen 							continue;
68226ba22f19SMichael Tuexen 						}
68231b649582SRandall Stewart 						if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
68241b649582SRandall Stewart 							if (local_scope == 0)
68251b649582SRandall Stewart 								continue;
68261b649582SRandall Stewart 							if (sin6->sin6_scope_id == 0) {
68271b649582SRandall Stewart 								if (sa6_recoverscope(sin6) != 0)
68281b649582SRandall Stewart 									/*
68295e2c2d87SRandall Stewart 									 *
68305e2c2d87SRandall Stewart 									 * bad
68315e2c2d87SRandall Stewart 									 *
68325e2c2d87SRandall Stewart 									 * li
68335e2c2d87SRandall Stewart 									 * nk
68345e2c2d87SRandall Stewart 									 *
68355e2c2d87SRandall Stewart 									 * loc
68365e2c2d87SRandall Stewart 									 * al
68375e2c2d87SRandall Stewart 									 *
68385e2c2d87SRandall Stewart 									 * add
68395e2c2d87SRandall Stewart 									 * re
68405e2c2d87SRandall Stewart 									 * ss
68415e2c2d87SRandall Stewart 									 * */
68421b649582SRandall Stewart 									continue;
68431b649582SRandall Stewart 							}
68441b649582SRandall Stewart 						}
68451b649582SRandall Stewart 						if ((site_scope == 0) &&
68461b649582SRandall Stewart 						    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
68471b649582SRandall Stewart 							continue;
68481b649582SRandall Stewart 						}
68491b649582SRandall Stewart 						/* count this one */
68501b649582SRandall Stewart 						count++;
68511b649582SRandall Stewart 					}
68525e2c2d87SRandall Stewart 					break;
68535e2c2d87SRandall Stewart #endif
68545e2c2d87SRandall Stewart 				default:
68555e2c2d87SRandall Stewart 					/* TSNH */
68565e2c2d87SRandall Stewart 					break;
68575e2c2d87SRandall Stewart 				}
68581b649582SRandall Stewart 			}
68591b649582SRandall Stewart 		}
68601b649582SRandall Stewart 	} else {
68611b649582SRandall Stewart 		/*
68621b649582SRandall Stewart 		 * subset bound case
68631b649582SRandall Stewart 		 */
68641b649582SRandall Stewart 		struct sctp_laddr *laddr;
68651b649582SRandall Stewart 
68661b649582SRandall Stewart 		LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list,
68671b649582SRandall Stewart 		    sctp_nxt_addr) {
68681b649582SRandall Stewart 			if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
68691b649582SRandall Stewart 				continue;
68701b649582SRandall Stewart 			}
68711b649582SRandall Stewart 			/* count this one */
68721b649582SRandall Stewart 			count++;
68731b649582SRandall Stewart 		}
68741b649582SRandall Stewart 	}
6875c99efcf6SRandall Stewart 	SCTP_IPI_ADDR_RUNLOCK();
68761b649582SRandall Stewart 	return (count);
68771b649582SRandall Stewart }
6878c4739e2fSRandall Stewart 
6879c4739e2fSRandall Stewart #if defined(SCTP_LOCAL_TRACE_BUF)
6880c4739e2fSRandall Stewart 
6881c4739e2fSRandall Stewart void
6882b27a6b7dSRandall 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)
6883c4739e2fSRandall Stewart {
6884b27a6b7dSRandall Stewart 	uint32_t saveindex, newindex;
6885c4739e2fSRandall Stewart 
6886c4739e2fSRandall Stewart 	do {
6887b3f1ea41SRandall Stewart 		saveindex = SCTP_BASE_SYSCTL(sctp_log).index;
6888c4739e2fSRandall Stewart 		if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
6889c4739e2fSRandall Stewart 			newindex = 1;
6890c4739e2fSRandall Stewart 		} else {
6891c4739e2fSRandall Stewart 			newindex = saveindex + 1;
6892c4739e2fSRandall Stewart 		}
6893b3f1ea41SRandall Stewart 	} while (atomic_cmpset_int(&SCTP_BASE_SYSCTL(sctp_log).index, saveindex, newindex) == 0);
6894c4739e2fSRandall Stewart 	if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
6895c4739e2fSRandall Stewart 		saveindex = 0;
6896c4739e2fSRandall Stewart 	}
6897b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].timestamp = SCTP_GET_CYCLECOUNT;
6898b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].subsys = subsys;
6899b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[0] = a;
6900b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[1] = b;
6901b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[2] = c;
6902b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[3] = d;
6903b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[4] = e;
6904b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[5] = f;
6905c4739e2fSRandall Stewart }
6906c4739e2fSRandall Stewart 
6907c4739e2fSRandall Stewart #endif
6908a99b6783SRandall Stewart static void
69097cca1775SRandall Stewart sctp_recv_udp_tunneled_packet(struct mbuf *m, int off, struct inpcb *inp,
691081d3ec17SBryan Venteicher     const struct sockaddr *sa SCTP_UNUSED, void *ctx SCTP_UNUSED)
6911a99b6783SRandall Stewart {
6912a99b6783SRandall Stewart 	struct ip *iph;
69133a51a264SMichael Tuexen 
69143a51a264SMichael Tuexen #ifdef INET6
69153a51a264SMichael Tuexen 	struct ip6_hdr *ip6;
69163a51a264SMichael Tuexen 
69173a51a264SMichael Tuexen #endif
6918a99b6783SRandall Stewart 	struct mbuf *sp, *last;
6919a99b6783SRandall Stewart 	struct udphdr *uhdr;
6920285052f0SMichael Tuexen 	uint16_t port;
6921a99b6783SRandall Stewart 
6922a99b6783SRandall Stewart 	if ((m->m_flags & M_PKTHDR) == 0) {
6923a99b6783SRandall Stewart 		/* Can't handle one that is not a pkt hdr */
6924a99b6783SRandall Stewart 		goto out;
6925a99b6783SRandall Stewart 	}
6926285052f0SMichael Tuexen 	/* Pull the src port */
6927a99b6783SRandall Stewart 	iph = mtod(m, struct ip *);
6928a99b6783SRandall Stewart 	uhdr = (struct udphdr *)((caddr_t)iph + off);
6929a99b6783SRandall Stewart 	port = uhdr->uh_sport;
6930285052f0SMichael Tuexen 	/*
6931285052f0SMichael Tuexen 	 * Split out the mbuf chain. Leave the IP header in m, place the
6932285052f0SMichael Tuexen 	 * rest in the sp.
6933285052f0SMichael Tuexen 	 */
6934eb1b1807SGleb Smirnoff 	sp = m_split(m, off, M_NOWAIT);
6935a99b6783SRandall Stewart 	if (sp == NULL) {
6936a99b6783SRandall Stewart 		/* Gak, drop packet, we can't do a split */
6937a99b6783SRandall Stewart 		goto out;
6938a99b6783SRandall Stewart 	}
6939285052f0SMichael Tuexen 	if (sp->m_pkthdr.len < sizeof(struct udphdr) + sizeof(struct sctphdr)) {
6940285052f0SMichael Tuexen 		/* Gak, packet can't have an SCTP header in it - too small */
6941a99b6783SRandall Stewart 		m_freem(sp);
6942a99b6783SRandall Stewart 		goto out;
6943a99b6783SRandall Stewart 	}
6944285052f0SMichael Tuexen 	/* Now pull up the UDP header and SCTP header together */
6945285052f0SMichael Tuexen 	sp = m_pullup(sp, sizeof(struct udphdr) + sizeof(struct sctphdr));
6946a99b6783SRandall Stewart 	if (sp == NULL) {
6947a99b6783SRandall Stewart 		/* Gak pullup failed */
6948a99b6783SRandall Stewart 		goto out;
6949a99b6783SRandall Stewart 	}
6950285052f0SMichael Tuexen 	/* Trim out the UDP header */
6951a99b6783SRandall Stewart 	m_adj(sp, sizeof(struct udphdr));
6952a99b6783SRandall Stewart 
6953a99b6783SRandall Stewart 	/* Now reconstruct the mbuf chain */
6954285052f0SMichael Tuexen 	for (last = m; last->m_next; last = last->m_next);
6955a99b6783SRandall Stewart 	last->m_next = sp;
6956a99b6783SRandall Stewart 	m->m_pkthdr.len += sp->m_pkthdr.len;
6957a99b6783SRandall Stewart 	iph = mtod(m, struct ip *);
6958a99b6783SRandall Stewart 	switch (iph->ip_v) {
6959e6194c2eSMichael Tuexen #ifdef INET
6960a99b6783SRandall Stewart 	case IPVERSION:
696109c1c856SMichael Tuexen 		iph->ip_len = htons(ntohs(iph->ip_len) - sizeof(struct udphdr));
6962a99b6783SRandall Stewart 		sctp_input_with_port(m, off, port);
6963a99b6783SRandall Stewart 		break;
6964e6194c2eSMichael Tuexen #endif
6965a99b6783SRandall Stewart #ifdef INET6
6966a99b6783SRandall Stewart 	case IPV6_VERSION >> 4:
69673a51a264SMichael Tuexen 		ip6 = mtod(m, struct ip6_hdr *);
69683a51a264SMichael Tuexen 		ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - sizeof(struct udphdr));
69693a51a264SMichael Tuexen 		sctp6_input_with_port(&m, &off, port);
6970a99b6783SRandall Stewart 		break;
6971a99b6783SRandall Stewart #endif
6972a99b6783SRandall Stewart 	default:
6973285052f0SMichael Tuexen 		goto out;
6974a99b6783SRandall Stewart 		break;
6975a99b6783SRandall Stewart 	}
6976a99b6783SRandall Stewart 	return;
6977a99b6783SRandall Stewart out:
6978a99b6783SRandall Stewart 	m_freem(m);
6979a99b6783SRandall Stewart }
6980c54a18d2SRandall Stewart 
6981c54a18d2SRandall Stewart void
6982c54a18d2SRandall Stewart sctp_over_udp_stop(void)
6983c54a18d2SRandall Stewart {
6984a99b6783SRandall Stewart 	/*
6985a99b6783SRandall Stewart 	 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
6986a99b6783SRandall Stewart 	 * for writting!
6987a99b6783SRandall Stewart 	 */
69883a51a264SMichael Tuexen #ifdef INET
69893a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
69903a51a264SMichael Tuexen 		soclose(SCTP_BASE_INFO(udp4_tun_socket));
69913a51a264SMichael Tuexen 		SCTP_BASE_INFO(udp4_tun_socket) = NULL;
6992c54a18d2SRandall Stewart 	}
69933a51a264SMichael Tuexen #endif
69943a51a264SMichael Tuexen #ifdef INET6
69953a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
69963a51a264SMichael Tuexen 		soclose(SCTP_BASE_INFO(udp6_tun_socket));
69973a51a264SMichael Tuexen 		SCTP_BASE_INFO(udp6_tun_socket) = NULL;
69983a51a264SMichael Tuexen 	}
69993a51a264SMichael Tuexen #endif
7000a99b6783SRandall Stewart }
7001ea5eba11SMichael Tuexen 
7002c54a18d2SRandall Stewart int
7003c54a18d2SRandall Stewart sctp_over_udp_start(void)
7004c54a18d2SRandall Stewart {
7005a99b6783SRandall Stewart 	uint16_t port;
7006a99b6783SRandall Stewart 	int ret;
7007a99b6783SRandall Stewart 
70083a51a264SMichael Tuexen #ifdef INET
70093a51a264SMichael Tuexen 	struct sockaddr_in sin;
70103a51a264SMichael Tuexen 
70113a51a264SMichael Tuexen #endif
70123a51a264SMichael Tuexen #ifdef INET6
70133a51a264SMichael Tuexen 	struct sockaddr_in6 sin6;
70143a51a264SMichael Tuexen 
70153a51a264SMichael Tuexen #endif
7016a99b6783SRandall Stewart 	/*
7017a99b6783SRandall Stewart 	 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
7018a99b6783SRandall Stewart 	 * for writting!
7019a99b6783SRandall Stewart 	 */
7020a99b6783SRandall Stewart 	port = SCTP_BASE_SYSCTL(sctp_udp_tunneling_port);
70213a51a264SMichael Tuexen 	if (ntohs(port) == 0) {
7022a99b6783SRandall Stewart 		/* Must have a port set */
7023a99b6783SRandall Stewart 		return (EINVAL);
7024a99b6783SRandall Stewart 	}
70253a51a264SMichael Tuexen #ifdef INET
70263a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
7027a99b6783SRandall Stewart 		/* Already running -- must stop first */
7028a99b6783SRandall Stewart 		return (EALREADY);
7029a99b6783SRandall Stewart 	}
70303a51a264SMichael Tuexen #endif
70313a51a264SMichael Tuexen #ifdef INET6
70323a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
70333a51a264SMichael Tuexen 		/* Already running -- must stop first */
70343a51a264SMichael Tuexen 		return (EALREADY);
7035a99b6783SRandall Stewart 	}
70363a51a264SMichael Tuexen #endif
70373a51a264SMichael Tuexen #ifdef INET
70383a51a264SMichael Tuexen 	if ((ret = socreate(PF_INET, &SCTP_BASE_INFO(udp4_tun_socket),
70393a51a264SMichael Tuexen 	    SOCK_DGRAM, IPPROTO_UDP,
70403a51a264SMichael Tuexen 	    curthread->td_ucred, curthread))) {
7041a99b6783SRandall Stewart 		sctp_over_udp_stop();
7042a99b6783SRandall Stewart 		return (ret);
7043a99b6783SRandall Stewart 	}
70443a51a264SMichael Tuexen 	/* Call the special UDP hook. */
70453a51a264SMichael Tuexen 	if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp4_tun_socket),
704681d3ec17SBryan Venteicher 	    sctp_recv_udp_tunneled_packet, NULL))) {
70473a51a264SMichael Tuexen 		sctp_over_udp_stop();
70483a51a264SMichael Tuexen 		return (ret);
70493a51a264SMichael Tuexen 	}
70503a51a264SMichael Tuexen 	/* Ok, we have a socket, bind it to the port. */
70513a51a264SMichael Tuexen 	memset(&sin, 0, sizeof(struct sockaddr_in));
70523a51a264SMichael Tuexen 	sin.sin_len = sizeof(struct sockaddr_in);
70533a51a264SMichael Tuexen 	sin.sin_family = AF_INET;
70543a51a264SMichael Tuexen 	sin.sin_port = htons(port);
70553a51a264SMichael Tuexen 	if ((ret = sobind(SCTP_BASE_INFO(udp4_tun_socket),
70563a51a264SMichael Tuexen 	    (struct sockaddr *)&sin, curthread))) {
70573a51a264SMichael Tuexen 		sctp_over_udp_stop();
70583a51a264SMichael Tuexen 		return (ret);
70593a51a264SMichael Tuexen 	}
70603a51a264SMichael Tuexen #endif
70613a51a264SMichael Tuexen #ifdef INET6
70623a51a264SMichael Tuexen 	if ((ret = socreate(PF_INET6, &SCTP_BASE_INFO(udp6_tun_socket),
70633a51a264SMichael Tuexen 	    SOCK_DGRAM, IPPROTO_UDP,
70643a51a264SMichael Tuexen 	    curthread->td_ucred, curthread))) {
70653a51a264SMichael Tuexen 		sctp_over_udp_stop();
70663a51a264SMichael Tuexen 		return (ret);
70673a51a264SMichael Tuexen 	}
70683a51a264SMichael Tuexen 	/* Call the special UDP hook. */
70693a51a264SMichael Tuexen 	if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp6_tun_socket),
707081d3ec17SBryan Venteicher 	    sctp_recv_udp_tunneled_packet, NULL))) {
70713a51a264SMichael Tuexen 		sctp_over_udp_stop();
70723a51a264SMichael Tuexen 		return (ret);
70733a51a264SMichael Tuexen 	}
70743a51a264SMichael Tuexen 	/* Ok, we have a socket, bind it to the port. */
70753a51a264SMichael Tuexen 	memset(&sin6, 0, sizeof(struct sockaddr_in6));
70763a51a264SMichael Tuexen 	sin6.sin6_len = sizeof(struct sockaddr_in6);
70773a51a264SMichael Tuexen 	sin6.sin6_family = AF_INET6;
70783a51a264SMichael Tuexen 	sin6.sin6_port = htons(port);
70793a51a264SMichael Tuexen 	if ((ret = sobind(SCTP_BASE_INFO(udp6_tun_socket),
70803a51a264SMichael Tuexen 	    (struct sockaddr *)&sin6, curthread))) {
70813a51a264SMichael Tuexen 		sctp_over_udp_stop();
70823a51a264SMichael Tuexen 		return (ret);
70833a51a264SMichael Tuexen 	}
70843a51a264SMichael Tuexen #endif
7085a99b6783SRandall Stewart 	return (0);
7086c54a18d2SRandall Stewart }
7087