xref: /freebsd/sys/netinet/sctputil.c (revision dd973b0e151fc8b1c1670aa2211780a46f419935)
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;
7080fefe0aSRandall Stewart 	sctp_clog.x.sb.so_sbcc = sb->sb_cc;
71f8829a4aSRandall Stewart 	if (stcb)
7280fefe0aSRandall Stewart 		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 
220f8829a4aSRandall Stewart void
221f8829a4aSRandall Stewart sctp_log_mb(struct mbuf *m, int from)
222f8829a4aSRandall Stewart {
22380fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
224f8829a4aSRandall Stewart 
22580fefe0aSRandall Stewart 	sctp_clog.x.mb.mp = m;
22680fefe0aSRandall Stewart 	sctp_clog.x.mb.mbuf_flags = (uint8_t) (SCTP_BUF_GET_FLAGS(m));
22780fefe0aSRandall Stewart 	sctp_clog.x.mb.size = (uint16_t) (SCTP_BUF_LEN(m));
22880fefe0aSRandall Stewart 	sctp_clog.x.mb.data = SCTP_BUF_AT(m, 0);
229139bc87fSRandall Stewart 	if (SCTP_BUF_IS_EXTENDED(m)) {
23080fefe0aSRandall Stewart 		sctp_clog.x.mb.ext = SCTP_BUF_EXTEND_BASE(m);
23180fefe0aSRandall Stewart 		sctp_clog.x.mb.refcnt = (uint8_t) (SCTP_BUF_EXTEND_REFCNT(m));
232f8829a4aSRandall Stewart 	} else {
23380fefe0aSRandall Stewart 		sctp_clog.x.mb.ext = 0;
23480fefe0aSRandall Stewart 		sctp_clog.x.mb.refcnt = 0;
235f8829a4aSRandall Stewart 	}
236c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
23780fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_MBUF,
23880fefe0aSRandall Stewart 	    from,
23980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
24080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
24180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
24280fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
243f8829a4aSRandall Stewart }
244f8829a4aSRandall Stewart 
245f8829a4aSRandall Stewart void
246dcb68fbaSMichael Tuexen sctp_log_strm_del(struct sctp_queued_to_read *control, struct sctp_queued_to_read *poschk, int from)
247f8829a4aSRandall Stewart {
24880fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
249f8829a4aSRandall Stewart 
250f8829a4aSRandall Stewart 	if (control == NULL) {
251ad81507eSRandall Stewart 		SCTP_PRINTF("Gak log of NULL?\n");
252f8829a4aSRandall Stewart 		return;
253f8829a4aSRandall Stewart 	}
25480fefe0aSRandall Stewart 	sctp_clog.x.strlog.stcb = control->stcb;
25580fefe0aSRandall Stewart 	sctp_clog.x.strlog.n_tsn = control->sinfo_tsn;
25680fefe0aSRandall Stewart 	sctp_clog.x.strlog.n_sseq = control->sinfo_ssn;
25780fefe0aSRandall Stewart 	sctp_clog.x.strlog.strm = control->sinfo_stream;
258f8829a4aSRandall Stewart 	if (poschk != NULL) {
25980fefe0aSRandall Stewart 		sctp_clog.x.strlog.e_tsn = poschk->sinfo_tsn;
26080fefe0aSRandall Stewart 		sctp_clog.x.strlog.e_sseq = poschk->sinfo_ssn;
261f8829a4aSRandall Stewart 	} else {
26280fefe0aSRandall Stewart 		sctp_clog.x.strlog.e_tsn = 0;
26380fefe0aSRandall Stewart 		sctp_clog.x.strlog.e_sseq = 0;
264f8829a4aSRandall Stewart 	}
265c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
26680fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_STRM,
26780fefe0aSRandall Stewart 	    from,
26880fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
26980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
27080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
27180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
272f8829a4aSRandall Stewart }
273f8829a4aSRandall Stewart 
274f8829a4aSRandall Stewart void
275f8829a4aSRandall Stewart sctp_log_cwnd(struct sctp_tcb *stcb, struct sctp_nets *net, int augment, uint8_t from)
276f8829a4aSRandall Stewart {
27780fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
278f8829a4aSRandall Stewart 
27980fefe0aSRandall Stewart 	sctp_clog.x.cwnd.net = net;
280f8829a4aSRandall Stewart 	if (stcb->asoc.send_queue_cnt > 255)
28180fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_send = 255;
282f8829a4aSRandall Stewart 	else
28380fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
284f8829a4aSRandall Stewart 	if (stcb->asoc.stream_queue_cnt > 255)
28580fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_str = 255;
286f8829a4aSRandall Stewart 	else
28780fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
288f8829a4aSRandall Stewart 
289f8829a4aSRandall Stewart 	if (net) {
29080fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cwnd_new_value = net->cwnd;
29180fefe0aSRandall Stewart 		sctp_clog.x.cwnd.inflight = net->flight_size;
29280fefe0aSRandall Stewart 		sctp_clog.x.cwnd.pseudo_cumack = net->pseudo_cumack;
29380fefe0aSRandall Stewart 		sctp_clog.x.cwnd.meets_pseudo_cumack = net->new_pseudo_cumack;
29480fefe0aSRandall Stewart 		sctp_clog.x.cwnd.need_new_pseudo_cumack = net->find_pseudo_cumack;
295f8829a4aSRandall Stewart 	}
296f8829a4aSRandall Stewart 	if (SCTP_CWNDLOG_PRESEND == from) {
29780fefe0aSRandall Stewart 		sctp_clog.x.cwnd.meets_pseudo_cumack = stcb->asoc.peers_rwnd;
298f8829a4aSRandall Stewart 	}
29980fefe0aSRandall Stewart 	sctp_clog.x.cwnd.cwnd_augment = augment;
300c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
30180fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_CWND,
30280fefe0aSRandall Stewart 	    from,
30380fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
30480fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
30580fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
30680fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
307f8829a4aSRandall Stewart }
308f8829a4aSRandall Stewart 
309f8829a4aSRandall Stewart void
310f8829a4aSRandall Stewart sctp_log_lock(struct sctp_inpcb *inp, struct sctp_tcb *stcb, uint8_t from)
311f8829a4aSRandall Stewart {
31280fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
313f8829a4aSRandall Stewart 
314bfefd190SRandall Stewart 	memset(&sctp_clog, 0, sizeof(sctp_clog));
31503b0b021SRandall Stewart 	if (inp) {
31680fefe0aSRandall Stewart 		sctp_clog.x.lock.sock = (void *)inp->sctp_socket;
31703b0b021SRandall Stewart 
31803b0b021SRandall Stewart 	} else {
31980fefe0aSRandall Stewart 		sctp_clog.x.lock.sock = (void *)NULL;
32003b0b021SRandall Stewart 	}
32180fefe0aSRandall Stewart 	sctp_clog.x.lock.inp = (void *)inp;
322f8829a4aSRandall Stewart 	if (stcb) {
32380fefe0aSRandall Stewart 		sctp_clog.x.lock.tcb_lock = mtx_owned(&stcb->tcb_mtx);
324f8829a4aSRandall Stewart 	} else {
32580fefe0aSRandall Stewart 		sctp_clog.x.lock.tcb_lock = SCTP_LOCK_UNKNOWN;
326f8829a4aSRandall Stewart 	}
327f8829a4aSRandall Stewart 	if (inp) {
32880fefe0aSRandall Stewart 		sctp_clog.x.lock.inp_lock = mtx_owned(&inp->inp_mtx);
32980fefe0aSRandall Stewart 		sctp_clog.x.lock.create_lock = mtx_owned(&inp->inp_create_mtx);
330f8829a4aSRandall Stewart 	} else {
33180fefe0aSRandall Stewart 		sctp_clog.x.lock.inp_lock = SCTP_LOCK_UNKNOWN;
33280fefe0aSRandall Stewart 		sctp_clog.x.lock.create_lock = SCTP_LOCK_UNKNOWN;
333f8829a4aSRandall Stewart 	}
334b3f1ea41SRandall Stewart 	sctp_clog.x.lock.info_lock = rw_wowned(&SCTP_BASE_INFO(ipi_ep_mtx));
33552129fcdSRandall Stewart 	if (inp && (inp->sctp_socket)) {
33680fefe0aSRandall Stewart 		sctp_clog.x.lock.sock_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
33780fefe0aSRandall Stewart 		sctp_clog.x.lock.sockrcvbuf_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx));
33880fefe0aSRandall Stewart 		sctp_clog.x.lock.socksndbuf_lock = mtx_owned(&(inp->sctp_socket->so_snd.sb_mtx));
339f8829a4aSRandall Stewart 	} else {
34080fefe0aSRandall Stewart 		sctp_clog.x.lock.sock_lock = SCTP_LOCK_UNKNOWN;
34180fefe0aSRandall Stewart 		sctp_clog.x.lock.sockrcvbuf_lock = SCTP_LOCK_UNKNOWN;
34280fefe0aSRandall Stewart 		sctp_clog.x.lock.socksndbuf_lock = SCTP_LOCK_UNKNOWN;
343f8829a4aSRandall Stewart 	}
344c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
34580fefe0aSRandall Stewart 	    SCTP_LOG_LOCK_EVENT,
34680fefe0aSRandall Stewart 	    from,
34780fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
34880fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
34980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
35080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
351f8829a4aSRandall Stewart }
352f8829a4aSRandall Stewart 
353f8829a4aSRandall Stewart void
354f8829a4aSRandall Stewart sctp_log_maxburst(struct sctp_tcb *stcb, struct sctp_nets *net, int error, int burst, uint8_t from)
355f8829a4aSRandall Stewart {
35680fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
357f8829a4aSRandall Stewart 
358bfefd190SRandall Stewart 	memset(&sctp_clog, 0, sizeof(sctp_clog));
35980fefe0aSRandall Stewart 	sctp_clog.x.cwnd.net = net;
36080fefe0aSRandall Stewart 	sctp_clog.x.cwnd.cwnd_new_value = error;
36180fefe0aSRandall Stewart 	sctp_clog.x.cwnd.inflight = net->flight_size;
36280fefe0aSRandall Stewart 	sctp_clog.x.cwnd.cwnd_augment = burst;
363f8829a4aSRandall Stewart 	if (stcb->asoc.send_queue_cnt > 255)
36480fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_send = 255;
365f8829a4aSRandall Stewart 	else
36680fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
367f8829a4aSRandall Stewart 	if (stcb->asoc.stream_queue_cnt > 255)
36880fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_str = 255;
369f8829a4aSRandall Stewart 	else
37080fefe0aSRandall Stewart 		sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
371c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
37280fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_MAXBURST,
37380fefe0aSRandall Stewart 	    from,
37480fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
37580fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
37680fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
37780fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
378f8829a4aSRandall Stewart }
379f8829a4aSRandall Stewart 
380f8829a4aSRandall Stewart void
381f8829a4aSRandall Stewart sctp_log_rwnd(uint8_t from, uint32_t peers_rwnd, uint32_t snd_size, uint32_t overhead)
382f8829a4aSRandall Stewart {
38380fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
384f8829a4aSRandall Stewart 
38580fefe0aSRandall Stewart 	sctp_clog.x.rwnd.rwnd = peers_rwnd;
38680fefe0aSRandall Stewart 	sctp_clog.x.rwnd.send_size = snd_size;
38780fefe0aSRandall Stewart 	sctp_clog.x.rwnd.overhead = overhead;
38880fefe0aSRandall Stewart 	sctp_clog.x.rwnd.new_rwnd = 0;
389c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
39080fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_RWND,
39180fefe0aSRandall Stewart 	    from,
39280fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
39380fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
39480fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
39580fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
396f8829a4aSRandall Stewart }
397f8829a4aSRandall Stewart 
398f8829a4aSRandall Stewart void
399f8829a4aSRandall Stewart sctp_log_rwnd_set(uint8_t from, uint32_t peers_rwnd, uint32_t flight_size, uint32_t overhead, uint32_t a_rwndval)
400f8829a4aSRandall Stewart {
40180fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
402f8829a4aSRandall Stewart 
40380fefe0aSRandall Stewart 	sctp_clog.x.rwnd.rwnd = peers_rwnd;
40480fefe0aSRandall Stewart 	sctp_clog.x.rwnd.send_size = flight_size;
40580fefe0aSRandall Stewart 	sctp_clog.x.rwnd.overhead = overhead;
40680fefe0aSRandall Stewart 	sctp_clog.x.rwnd.new_rwnd = a_rwndval;
407c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
40880fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_RWND,
40980fefe0aSRandall Stewart 	    from,
41080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
41180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
41280fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
41380fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
414f8829a4aSRandall Stewart }
415f8829a4aSRandall Stewart 
416f8829a4aSRandall Stewart void
417f8829a4aSRandall Stewart sctp_log_mbcnt(uint8_t from, uint32_t total_oq, uint32_t book, uint32_t total_mbcnt_q, uint32_t mbcnt)
418f8829a4aSRandall Stewart {
41980fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
420f8829a4aSRandall Stewart 
42180fefe0aSRandall Stewart 	sctp_clog.x.mbcnt.total_queue_size = total_oq;
42280fefe0aSRandall Stewart 	sctp_clog.x.mbcnt.size_change = book;
42380fefe0aSRandall Stewart 	sctp_clog.x.mbcnt.total_queue_mb_size = total_mbcnt_q;
42480fefe0aSRandall Stewart 	sctp_clog.x.mbcnt.mbcnt_change = mbcnt;
425c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
42680fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_MBCNT,
42780fefe0aSRandall Stewart 	    from,
42880fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
42980fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
43080fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
43180fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
432f8829a4aSRandall Stewart }
433f8829a4aSRandall Stewart 
434f8829a4aSRandall Stewart void
435f8829a4aSRandall Stewart sctp_misc_ints(uint8_t from, uint32_t a, uint32_t b, uint32_t c, uint32_t d)
436f8829a4aSRandall Stewart {
437c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
43880fefe0aSRandall Stewart 	    SCTP_LOG_MISC_EVENT,
43980fefe0aSRandall Stewart 	    from,
44080fefe0aSRandall Stewart 	    a, b, c, d);
441f8829a4aSRandall Stewart }
442f8829a4aSRandall Stewart 
443f8829a4aSRandall Stewart void
4447215cc1bSMichael Tuexen sctp_wakeup_log(struct sctp_tcb *stcb, uint32_t wake_cnt, int from)
445f8829a4aSRandall Stewart {
44680fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
447f8829a4aSRandall Stewart 
44880fefe0aSRandall Stewart 	sctp_clog.x.wake.stcb = (void *)stcb;
44980fefe0aSRandall Stewart 	sctp_clog.x.wake.wake_cnt = wake_cnt;
45080fefe0aSRandall Stewart 	sctp_clog.x.wake.flight = stcb->asoc.total_flight_count;
45180fefe0aSRandall Stewart 	sctp_clog.x.wake.send_q = stcb->asoc.send_queue_cnt;
45280fefe0aSRandall Stewart 	sctp_clog.x.wake.sent_q = stcb->asoc.sent_queue_cnt;
453f8829a4aSRandall Stewart 
454f8829a4aSRandall Stewart 	if (stcb->asoc.stream_queue_cnt < 0xff)
45580fefe0aSRandall Stewart 		sctp_clog.x.wake.stream_qcnt = (uint8_t) stcb->asoc.stream_queue_cnt;
456f8829a4aSRandall Stewart 	else
45780fefe0aSRandall Stewart 		sctp_clog.x.wake.stream_qcnt = 0xff;
458f8829a4aSRandall Stewart 
459f8829a4aSRandall Stewart 	if (stcb->asoc.chunks_on_out_queue < 0xff)
46080fefe0aSRandall Stewart 		sctp_clog.x.wake.chunks_on_oque = (uint8_t) stcb->asoc.chunks_on_out_queue;
461f8829a4aSRandall Stewart 	else
46280fefe0aSRandall Stewart 		sctp_clog.x.wake.chunks_on_oque = 0xff;
463f8829a4aSRandall Stewart 
46480fefe0aSRandall Stewart 	sctp_clog.x.wake.sctpflags = 0;
465f8829a4aSRandall Stewart 	/* set in the defered mode stuff */
466f8829a4aSRandall Stewart 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE)
46780fefe0aSRandall Stewart 		sctp_clog.x.wake.sctpflags |= 1;
468f8829a4aSRandall Stewart 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT)
46980fefe0aSRandall Stewart 		sctp_clog.x.wake.sctpflags |= 2;
470f8829a4aSRandall Stewart 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT)
47180fefe0aSRandall Stewart 		sctp_clog.x.wake.sctpflags |= 4;
472f8829a4aSRandall Stewart 	/* what about the sb */
473f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
474f8829a4aSRandall Stewart 		struct socket *so = stcb->sctp_socket;
475f8829a4aSRandall Stewart 
47680fefe0aSRandall Stewart 		sctp_clog.x.wake.sbflags = (uint8_t) ((so->so_snd.sb_flags & 0x00ff));
477f8829a4aSRandall Stewart 	} else {
47880fefe0aSRandall Stewart 		sctp_clog.x.wake.sbflags = 0xff;
479f8829a4aSRandall Stewart 	}
480c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
48180fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_WAKE,
48280fefe0aSRandall Stewart 	    from,
48380fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
48480fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
48580fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
48680fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
487f8829a4aSRandall Stewart }
488f8829a4aSRandall Stewart 
489f8829a4aSRandall Stewart void
4907215cc1bSMichael Tuexen sctp_log_block(uint8_t from, struct sctp_association *asoc, int sendlen)
491f8829a4aSRandall Stewart {
49280fefe0aSRandall Stewart 	struct sctp_cwnd_log sctp_clog;
493f8829a4aSRandall Stewart 
49480fefe0aSRandall Stewart 	sctp_clog.x.blk.onsb = asoc->total_output_queue_size;
49580fefe0aSRandall Stewart 	sctp_clog.x.blk.send_sent_qcnt = (uint16_t) (asoc->send_queue_cnt + asoc->sent_queue_cnt);
49680fefe0aSRandall Stewart 	sctp_clog.x.blk.peer_rwnd = asoc->peers_rwnd;
49780fefe0aSRandall Stewart 	sctp_clog.x.blk.stream_qcnt = (uint16_t) asoc->stream_queue_cnt;
49880fefe0aSRandall Stewart 	sctp_clog.x.blk.chunks_on_oque = (uint16_t) asoc->chunks_on_out_queue;
49980fefe0aSRandall Stewart 	sctp_clog.x.blk.flight_size = (uint16_t) (asoc->total_flight / 1024);
50080fefe0aSRandall Stewart 	sctp_clog.x.blk.sndlen = sendlen;
501c4739e2fSRandall Stewart 	SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
50280fefe0aSRandall Stewart 	    SCTP_LOG_EVENT_BLOCK,
50380fefe0aSRandall Stewart 	    from,
50480fefe0aSRandall Stewart 	    sctp_clog.x.misc.log1,
50580fefe0aSRandall Stewart 	    sctp_clog.x.misc.log2,
50680fefe0aSRandall Stewart 	    sctp_clog.x.misc.log3,
50780fefe0aSRandall Stewart 	    sctp_clog.x.misc.log4);
508f8829a4aSRandall Stewart }
509f8829a4aSRandall Stewart 
510f8829a4aSRandall Stewart int
5117215cc1bSMichael Tuexen sctp_fill_stat_log(void *optval SCTP_UNUSED, size_t *optsize SCTP_UNUSED)
512f8829a4aSRandall Stewart {
51380fefe0aSRandall Stewart 	/* May need to fix this if ktrdump does not work */
514f8829a4aSRandall Stewart 	return (0);
515f8829a4aSRandall Stewart }
516f8829a4aSRandall Stewart 
517f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
518f8829a4aSRandall Stewart uint8_t sctp_audit_data[SCTP_AUDIT_SIZE][2];
519f8829a4aSRandall Stewart static int sctp_audit_indx = 0;
520f8829a4aSRandall Stewart 
521f8829a4aSRandall Stewart static
522f8829a4aSRandall Stewart void
523f8829a4aSRandall Stewart sctp_print_audit_report(void)
524f8829a4aSRandall Stewart {
525f8829a4aSRandall Stewart 	int i;
526f8829a4aSRandall Stewart 	int cnt;
527f8829a4aSRandall Stewart 
528f8829a4aSRandall Stewart 	cnt = 0;
529f8829a4aSRandall Stewart 	for (i = sctp_audit_indx; i < SCTP_AUDIT_SIZE; i++) {
530f8829a4aSRandall Stewart 		if ((sctp_audit_data[i][0] == 0xe0) &&
531f8829a4aSRandall Stewart 		    (sctp_audit_data[i][1] == 0x01)) {
532f8829a4aSRandall Stewart 			cnt = 0;
533ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
534f8829a4aSRandall Stewart 		} else if (sctp_audit_data[i][0] == 0xf0) {
535f8829a4aSRandall Stewart 			cnt = 0;
536ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
537f8829a4aSRandall Stewart 		} else if ((sctp_audit_data[i][0] == 0xc0) &&
538f8829a4aSRandall Stewart 		    (sctp_audit_data[i][1] == 0x01)) {
539ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
540f8829a4aSRandall Stewart 			cnt = 0;
541f8829a4aSRandall Stewart 		}
542ad81507eSRandall Stewart 		SCTP_PRINTF("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0],
543f8829a4aSRandall Stewart 		    (uint32_t) sctp_audit_data[i][1]);
544f8829a4aSRandall Stewart 		cnt++;
545f8829a4aSRandall Stewart 		if ((cnt % 14) == 0)
546ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
547f8829a4aSRandall Stewart 	}
548f8829a4aSRandall Stewart 	for (i = 0; i < sctp_audit_indx; i++) {
549f8829a4aSRandall Stewart 		if ((sctp_audit_data[i][0] == 0xe0) &&
550f8829a4aSRandall Stewart 		    (sctp_audit_data[i][1] == 0x01)) {
551f8829a4aSRandall Stewart 			cnt = 0;
552ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
553f8829a4aSRandall Stewart 		} else if (sctp_audit_data[i][0] == 0xf0) {
554f8829a4aSRandall Stewart 			cnt = 0;
555ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
556f8829a4aSRandall Stewart 		} else if ((sctp_audit_data[i][0] == 0xc0) &&
557f8829a4aSRandall Stewart 		    (sctp_audit_data[i][1] == 0x01)) {
558ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
559f8829a4aSRandall Stewart 			cnt = 0;
560f8829a4aSRandall Stewart 		}
561ad81507eSRandall Stewart 		SCTP_PRINTF("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0],
562f8829a4aSRandall Stewart 		    (uint32_t) sctp_audit_data[i][1]);
563f8829a4aSRandall Stewart 		cnt++;
564f8829a4aSRandall Stewart 		if ((cnt % 14) == 0)
565ad81507eSRandall Stewart 			SCTP_PRINTF("\n");
566f8829a4aSRandall Stewart 	}
567ad81507eSRandall Stewart 	SCTP_PRINTF("\n");
568f8829a4aSRandall Stewart }
569f8829a4aSRandall Stewart 
570f8829a4aSRandall Stewart void
571f8829a4aSRandall Stewart sctp_auditing(int from, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
572f8829a4aSRandall Stewart     struct sctp_nets *net)
573f8829a4aSRandall Stewart {
574f8829a4aSRandall Stewart 	int resend_cnt, tot_out, rep, tot_book_cnt;
575f8829a4aSRandall Stewart 	struct sctp_nets *lnet;
576f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
577f8829a4aSRandall Stewart 
578f8829a4aSRandall Stewart 	sctp_audit_data[sctp_audit_indx][0] = 0xAA;
579f8829a4aSRandall Stewart 	sctp_audit_data[sctp_audit_indx][1] = 0x000000ff & from;
580f8829a4aSRandall Stewart 	sctp_audit_indx++;
581f8829a4aSRandall Stewart 	if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
582f8829a4aSRandall Stewart 		sctp_audit_indx = 0;
583f8829a4aSRandall Stewart 	}
584f8829a4aSRandall Stewart 	if (inp == NULL) {
585f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xAF;
586f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] = 0x01;
587f8829a4aSRandall Stewart 		sctp_audit_indx++;
588f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
589f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
590f8829a4aSRandall Stewart 		}
591f8829a4aSRandall Stewart 		return;
592f8829a4aSRandall Stewart 	}
593f8829a4aSRandall Stewart 	if (stcb == NULL) {
594f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xAF;
595f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] = 0x02;
596f8829a4aSRandall Stewart 		sctp_audit_indx++;
597f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
598f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
599f8829a4aSRandall Stewart 		}
600f8829a4aSRandall Stewart 		return;
601f8829a4aSRandall Stewart 	}
602f8829a4aSRandall Stewart 	sctp_audit_data[sctp_audit_indx][0] = 0xA1;
603f8829a4aSRandall Stewart 	sctp_audit_data[sctp_audit_indx][1] =
604f8829a4aSRandall Stewart 	    (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
605f8829a4aSRandall Stewart 	sctp_audit_indx++;
606f8829a4aSRandall Stewart 	if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
607f8829a4aSRandall Stewart 		sctp_audit_indx = 0;
608f8829a4aSRandall Stewart 	}
609f8829a4aSRandall Stewart 	rep = 0;
610f8829a4aSRandall Stewart 	tot_book_cnt = 0;
611f8829a4aSRandall Stewart 	resend_cnt = tot_out = 0;
612f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
613f8829a4aSRandall Stewart 		if (chk->sent == SCTP_DATAGRAM_RESEND) {
614f8829a4aSRandall Stewart 			resend_cnt++;
615f8829a4aSRandall Stewart 		} else if (chk->sent < SCTP_DATAGRAM_RESEND) {
616f8829a4aSRandall Stewart 			tot_out += chk->book_size;
617f8829a4aSRandall Stewart 			tot_book_cnt++;
618f8829a4aSRandall Stewart 		}
619f8829a4aSRandall Stewart 	}
620f8829a4aSRandall Stewart 	if (resend_cnt != stcb->asoc.sent_queue_retran_cnt) {
621f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xAF;
622f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] = 0xA1;
623f8829a4aSRandall Stewart 		sctp_audit_indx++;
624f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
625f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
626f8829a4aSRandall Stewart 		}
627ad81507eSRandall Stewart 		SCTP_PRINTF("resend_cnt:%d asoc-tot:%d\n",
628f8829a4aSRandall Stewart 		    resend_cnt, stcb->asoc.sent_queue_retran_cnt);
629f8829a4aSRandall Stewart 		rep = 1;
630f8829a4aSRandall Stewart 		stcb->asoc.sent_queue_retran_cnt = resend_cnt;
631f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xA2;
632f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] =
633f8829a4aSRandall Stewart 		    (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
634f8829a4aSRandall Stewart 		sctp_audit_indx++;
635f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
636f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
637f8829a4aSRandall Stewart 		}
638f8829a4aSRandall Stewart 	}
639f8829a4aSRandall Stewart 	if (tot_out != stcb->asoc.total_flight) {
640f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xAF;
641f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] = 0xA2;
642f8829a4aSRandall Stewart 		sctp_audit_indx++;
643f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
644f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
645f8829a4aSRandall Stewart 		}
646f8829a4aSRandall Stewart 		rep = 1;
647ad81507eSRandall Stewart 		SCTP_PRINTF("tot_flt:%d asoc_tot:%d\n", tot_out,
648f8829a4aSRandall Stewart 		    (int)stcb->asoc.total_flight);
649f8829a4aSRandall Stewart 		stcb->asoc.total_flight = tot_out;
650f8829a4aSRandall Stewart 	}
651f8829a4aSRandall Stewart 	if (tot_book_cnt != stcb->asoc.total_flight_count) {
652f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xAF;
653f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] = 0xA5;
654f8829a4aSRandall Stewart 		sctp_audit_indx++;
655f8829a4aSRandall Stewart 		if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
656f8829a4aSRandall Stewart 			sctp_audit_indx = 0;
657f8829a4aSRandall Stewart 		}
658f8829a4aSRandall Stewart 		rep = 1;
659f31e6c7fSMichael Tuexen 		SCTP_PRINTF("tot_flt_book:%d\n", tot_book_cnt);
660f8829a4aSRandall Stewart 
661f8829a4aSRandall Stewart 		stcb->asoc.total_flight_count = tot_book_cnt;
662f8829a4aSRandall Stewart 	}
663f8829a4aSRandall Stewart 	tot_out = 0;
664f8829a4aSRandall Stewart 	TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
665f8829a4aSRandall Stewart 		tot_out += lnet->flight_size;
666f8829a4aSRandall Stewart 	}
667f8829a4aSRandall Stewart 	if (tot_out != stcb->asoc.total_flight) {
668f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][0] = 0xAF;
669f8829a4aSRandall Stewart 		sctp_audit_data[sctp_audit_indx][1] = 0xA3;
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;
675ad81507eSRandall Stewart 		SCTP_PRINTF("real flight:%d net total was %d\n",
676f8829a4aSRandall Stewart 		    stcb->asoc.total_flight, tot_out);
677f8829a4aSRandall Stewart 		/* now corrective action */
678f8829a4aSRandall Stewart 		TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
679f8829a4aSRandall Stewart 
680f8829a4aSRandall Stewart 			tot_out = 0;
681f8829a4aSRandall Stewart 			TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
682f8829a4aSRandall Stewart 				if ((chk->whoTo == lnet) &&
683f8829a4aSRandall Stewart 				    (chk->sent < SCTP_DATAGRAM_RESEND)) {
684f8829a4aSRandall Stewart 					tot_out += chk->book_size;
685f8829a4aSRandall Stewart 				}
686f8829a4aSRandall Stewart 			}
687f8829a4aSRandall Stewart 			if (lnet->flight_size != tot_out) {
688f31e6c7fSMichael Tuexen 				SCTP_PRINTF("net:%p flight was %d corrected to %d\n",
689dd294dceSMichael Tuexen 				    (void *)lnet, lnet->flight_size,
690ad81507eSRandall Stewart 				    tot_out);
691f8829a4aSRandall Stewart 				lnet->flight_size = tot_out;
692f8829a4aSRandall Stewart 			}
693f8829a4aSRandall Stewart 		}
694f8829a4aSRandall Stewart 	}
695f8829a4aSRandall Stewart 	if (rep) {
696f8829a4aSRandall Stewart 		sctp_print_audit_report();
697f8829a4aSRandall Stewart 	}
698f8829a4aSRandall Stewart }
699f8829a4aSRandall Stewart 
700f8829a4aSRandall Stewart void
701f8829a4aSRandall Stewart sctp_audit_log(uint8_t ev, uint8_t fd)
702f8829a4aSRandall Stewart {
703f8829a4aSRandall Stewart 
704f8829a4aSRandall Stewart 	sctp_audit_data[sctp_audit_indx][0] = ev;
705f8829a4aSRandall Stewart 	sctp_audit_data[sctp_audit_indx][1] = fd;
706f8829a4aSRandall Stewart 	sctp_audit_indx++;
707f8829a4aSRandall Stewart 	if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
708f8829a4aSRandall Stewart 		sctp_audit_indx = 0;
709f8829a4aSRandall Stewart 	}
710f8829a4aSRandall Stewart }
711f8829a4aSRandall Stewart 
712f8829a4aSRandall Stewart #endif
713f8829a4aSRandall Stewart 
714f8829a4aSRandall Stewart /*
71512af6654SMichael Tuexen  * sctp_stop_timers_for_shutdown() should be called
71612af6654SMichael Tuexen  * when entering the SHUTDOWN_SENT or SHUTDOWN_ACK_SENT
71712af6654SMichael Tuexen  * state to make sure that all timers are stopped.
71812af6654SMichael Tuexen  */
71912af6654SMichael Tuexen void
72012af6654SMichael Tuexen sctp_stop_timers_for_shutdown(struct sctp_tcb *stcb)
72112af6654SMichael Tuexen {
72212af6654SMichael Tuexen 	struct sctp_association *asoc;
72312af6654SMichael Tuexen 	struct sctp_nets *net;
72412af6654SMichael Tuexen 
72512af6654SMichael Tuexen 	asoc = &stcb->asoc;
72612af6654SMichael Tuexen 
72712af6654SMichael Tuexen 	(void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
72812af6654SMichael Tuexen 	(void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
72912af6654SMichael Tuexen 	(void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
73012af6654SMichael Tuexen 	(void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
73112af6654SMichael Tuexen 	(void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
73212af6654SMichael Tuexen 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
73312af6654SMichael Tuexen 		(void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
734ca85e948SMichael Tuexen 		(void)SCTP_OS_TIMER_STOP(&net->hb_timer.timer);
73512af6654SMichael Tuexen 	}
73612af6654SMichael Tuexen }
73712af6654SMichael Tuexen 
73812af6654SMichael Tuexen /*
739f8829a4aSRandall Stewart  * a list of sizes based on typical mtu's, used only if next hop size not
740f8829a4aSRandall Stewart  * returned.
741f8829a4aSRandall Stewart  */
742437fc91aSMichael Tuexen static uint32_t sctp_mtu_sizes[] = {
743f8829a4aSRandall Stewart 	68,
744f8829a4aSRandall Stewart 	296,
745f8829a4aSRandall Stewart 	508,
746f8829a4aSRandall Stewart 	512,
747f8829a4aSRandall Stewart 	544,
748f8829a4aSRandall Stewart 	576,
749f8829a4aSRandall Stewart 	1006,
750f8829a4aSRandall Stewart 	1492,
751f8829a4aSRandall Stewart 	1500,
752f8829a4aSRandall Stewart 	1536,
753f8829a4aSRandall Stewart 	2002,
754f8829a4aSRandall Stewart 	2048,
755f8829a4aSRandall Stewart 	4352,
756f8829a4aSRandall Stewart 	4464,
757f8829a4aSRandall Stewart 	8166,
758f8829a4aSRandall Stewart 	17914,
759f8829a4aSRandall Stewart 	32000,
760f8829a4aSRandall Stewart 	65535
761f8829a4aSRandall Stewart };
762f8829a4aSRandall Stewart 
763f8829a4aSRandall Stewart /*
764437fc91aSMichael Tuexen  * Return the largest MTU smaller than val. If there is no
765437fc91aSMichael Tuexen  * entry, just return val.
766f8829a4aSRandall Stewart  */
767437fc91aSMichael Tuexen uint32_t
768437fc91aSMichael Tuexen sctp_get_prev_mtu(uint32_t val)
769437fc91aSMichael Tuexen {
770437fc91aSMichael Tuexen 	uint32_t i;
771437fc91aSMichael Tuexen 
772437fc91aSMichael Tuexen 	if (val <= sctp_mtu_sizes[0]) {
773437fc91aSMichael Tuexen 		return (val);
774437fc91aSMichael Tuexen 	}
775437fc91aSMichael Tuexen 	for (i = 1; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
776437fc91aSMichael Tuexen 		if (val <= sctp_mtu_sizes[i]) {
777f8829a4aSRandall Stewart 			break;
778f8829a4aSRandall Stewart 		}
779f8829a4aSRandall Stewart 	}
780437fc91aSMichael Tuexen 	return (sctp_mtu_sizes[i - 1]);
781437fc91aSMichael Tuexen }
782437fc91aSMichael Tuexen 
783437fc91aSMichael Tuexen /*
784437fc91aSMichael Tuexen  * Return the smallest MTU larger than val. If there is no
785437fc91aSMichael Tuexen  * entry, just return val.
786437fc91aSMichael Tuexen  */
787437fc91aSMichael Tuexen uint32_t
7887215cc1bSMichael Tuexen sctp_get_next_mtu(uint32_t val)
789437fc91aSMichael Tuexen {
790437fc91aSMichael Tuexen 	/* select another MTU that is just bigger than this one */
791437fc91aSMichael Tuexen 	uint32_t i;
792437fc91aSMichael Tuexen 
793437fc91aSMichael Tuexen 	for (i = 0; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
794437fc91aSMichael Tuexen 		if (val < sctp_mtu_sizes[i]) {
795437fc91aSMichael Tuexen 			return (sctp_mtu_sizes[i]);
796437fc91aSMichael Tuexen 		}
797437fc91aSMichael Tuexen 	}
798437fc91aSMichael Tuexen 	return (val);
799f8829a4aSRandall Stewart }
800f8829a4aSRandall Stewart 
801f8829a4aSRandall Stewart void
802f8829a4aSRandall Stewart sctp_fill_random_store(struct sctp_pcb *m)
803f8829a4aSRandall Stewart {
804f8829a4aSRandall Stewart 	/*
805f8829a4aSRandall Stewart 	 * Here we use the MD5/SHA-1 to hash with our good randomNumbers and
806f8829a4aSRandall Stewart 	 * our counter. The result becomes our good random numbers and we
807f8829a4aSRandall Stewart 	 * then setup to give these out. Note that we do no locking to
808f8829a4aSRandall Stewart 	 * protect this. This is ok, since if competing folks call this we
80917205eccSRandall Stewart 	 * will get more gobbled gook in the random store which is what we
810f8829a4aSRandall Stewart 	 * want. There is a danger that two guys will use the same random
811f8829a4aSRandall Stewart 	 * numbers, but thats ok too since that is random as well :->
812f8829a4aSRandall Stewart 	 */
813f8829a4aSRandall Stewart 	m->store_at = 0;
814ad81507eSRandall Stewart 	(void)sctp_hmac(SCTP_HMAC, (uint8_t *) m->random_numbers,
815f8829a4aSRandall Stewart 	    sizeof(m->random_numbers), (uint8_t *) & m->random_counter,
816f8829a4aSRandall Stewart 	    sizeof(m->random_counter), (uint8_t *) m->random_store);
817f8829a4aSRandall Stewart 	m->random_counter++;
818f8829a4aSRandall Stewart }
819f8829a4aSRandall Stewart 
820f8829a4aSRandall Stewart uint32_t
821851b7298SRandall Stewart sctp_select_initial_TSN(struct sctp_pcb *inp)
822f8829a4aSRandall Stewart {
823f8829a4aSRandall Stewart 	/*
824f8829a4aSRandall Stewart 	 * A true implementation should use random selection process to get
825f8829a4aSRandall Stewart 	 * the initial stream sequence number, using RFC1750 as a good
826f8829a4aSRandall Stewart 	 * guideline
827f8829a4aSRandall Stewart 	 */
828139bc87fSRandall Stewart 	uint32_t x, *xp;
829f8829a4aSRandall Stewart 	uint8_t *p;
830851b7298SRandall Stewart 	int store_at, new_store;
831f8829a4aSRandall Stewart 
832851b7298SRandall Stewart 	if (inp->initial_sequence_debug != 0) {
833f8829a4aSRandall Stewart 		uint32_t ret;
834f8829a4aSRandall Stewart 
835851b7298SRandall Stewart 		ret = inp->initial_sequence_debug;
836851b7298SRandall Stewart 		inp->initial_sequence_debug++;
837f8829a4aSRandall Stewart 		return (ret);
838f8829a4aSRandall Stewart 	}
839851b7298SRandall Stewart retry:
840851b7298SRandall Stewart 	store_at = inp->store_at;
841851b7298SRandall Stewart 	new_store = store_at + sizeof(uint32_t);
842851b7298SRandall Stewart 	if (new_store >= (SCTP_SIGNATURE_SIZE - 3)) {
843851b7298SRandall Stewart 		new_store = 0;
844f8829a4aSRandall Stewart 	}
845851b7298SRandall Stewart 	if (!atomic_cmpset_int(&inp->store_at, store_at, new_store)) {
846851b7298SRandall Stewart 		goto retry;
847851b7298SRandall Stewart 	}
848851b7298SRandall Stewart 	if (new_store == 0) {
849851b7298SRandall Stewart 		/* Refill the random store */
850851b7298SRandall Stewart 		sctp_fill_random_store(inp);
851851b7298SRandall Stewart 	}
852851b7298SRandall Stewart 	p = &inp->random_store[store_at];
853139bc87fSRandall Stewart 	xp = (uint32_t *) p;
854f8829a4aSRandall Stewart 	x = *xp;
855f8829a4aSRandall Stewart 	return (x);
856f8829a4aSRandall Stewart }
857f8829a4aSRandall Stewart 
858f8829a4aSRandall Stewart uint32_t
8597215cc1bSMichael Tuexen sctp_select_a_tag(struct sctp_inpcb *inp, uint16_t lport, uint16_t rport, int check)
860f8829a4aSRandall Stewart {
8617215cc1bSMichael Tuexen 	uint32_t x;
862f8829a4aSRandall Stewart 	struct timeval now;
863f8829a4aSRandall Stewart 
8647215cc1bSMichael Tuexen 	if (check) {
8656e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
8667215cc1bSMichael Tuexen 	}
8677215cc1bSMichael Tuexen 	for (;;) {
868851b7298SRandall Stewart 		x = sctp_select_initial_TSN(&inp->sctp_ep);
869f8829a4aSRandall Stewart 		if (x == 0) {
870f8829a4aSRandall Stewart 			/* we never use 0 */
871f8829a4aSRandall Stewart 			continue;
872f8829a4aSRandall Stewart 		}
8737215cc1bSMichael Tuexen 		if (!check || sctp_is_vtag_good(x, lport, rport, &now)) {
8747215cc1bSMichael Tuexen 			break;
875f8829a4aSRandall Stewart 		}
876f8829a4aSRandall Stewart 	}
877f8829a4aSRandall Stewart 	return (x);
878f8829a4aSRandall Stewart }
879f8829a4aSRandall Stewart 
880f8829a4aSRandall Stewart int
881a1cb341bSMichael Tuexen sctp_init_asoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
882b5c16493SMichael Tuexen     uint32_t override_tag, uint32_t vrf_id)
883f8829a4aSRandall Stewart {
8840696e120SRandall Stewart 	struct sctp_association *asoc;
8850696e120SRandall Stewart 
886f8829a4aSRandall Stewart 	/*
887f8829a4aSRandall Stewart 	 * Anything set to zero is taken care of by the allocation routine's
888f8829a4aSRandall Stewart 	 * bzero
889f8829a4aSRandall Stewart 	 */
890f8829a4aSRandall Stewart 
891f8829a4aSRandall Stewart 	/*
892f8829a4aSRandall Stewart 	 * Up front select what scoping to apply on addresses I tell my peer
893f8829a4aSRandall Stewart 	 * Not sure what to do with these right now, we will need to come up
894f8829a4aSRandall Stewart 	 * with a way to set them. We may need to pass them through from the
895f8829a4aSRandall Stewart 	 * caller in the sctp_aloc_assoc() function.
896f8829a4aSRandall Stewart 	 */
897f8829a4aSRandall Stewart 	int i;
898f8829a4aSRandall Stewart 
8990696e120SRandall Stewart 	asoc = &stcb->asoc;
900f8829a4aSRandall Stewart 	/* init all variables to a known value. */
901c4739e2fSRandall Stewart 	SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_INUSE);
902a1cb341bSMichael Tuexen 	asoc->max_burst = inp->sctp_ep.max_burst;
903a1cb341bSMichael Tuexen 	asoc->fr_max_burst = inp->sctp_ep.fr_max_burst;
904a1cb341bSMichael Tuexen 	asoc->heart_beat_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
905a1cb341bSMichael Tuexen 	asoc->cookie_life = inp->sctp_ep.def_cookie_life;
906a1cb341bSMichael Tuexen 	asoc->sctp_cmt_on_off = inp->sctp_cmt_on_off;
907f342355aSMichael Tuexen 	asoc->ecn_supported = inp->ecn_supported;
908*dd973b0eSMichael Tuexen 	asoc->prsctp_supported = inp->prsctp_supported;
909830d754dSRandall Stewart 	asoc->sctp_nr_sack_on_off = (uint8_t) SCTP_BASE_SYSCTL(sctp_nr_sack_on_off);
910ca85e948SMichael Tuexen 	asoc->sctp_cmt_pf = (uint8_t) 0;
911a1cb341bSMichael Tuexen 	asoc->sctp_frag_point = inp->sctp_frag_point;
912a1cb341bSMichael Tuexen 	asoc->sctp_features = inp->sctp_features;
913a1cb341bSMichael Tuexen 	asoc->default_dscp = inp->sctp_ep.default_dscp;
91442551e99SRandall Stewart #ifdef INET6
915a1cb341bSMichael Tuexen 	if (inp->sctp_ep.default_flowlabel) {
916a1cb341bSMichael Tuexen 		asoc->default_flowlabel = inp->sctp_ep.default_flowlabel;
91758bdb691SMichael Tuexen 	} else {
918a1cb341bSMichael Tuexen 		if (inp->ip_inp.inp.inp_flags & IN6P_AUTOFLOWLABEL) {
919a1cb341bSMichael Tuexen 			asoc->default_flowlabel = sctp_select_initial_TSN(&inp->sctp_ep);
92058bdb691SMichael Tuexen 			asoc->default_flowlabel &= 0x000fffff;
92158bdb691SMichael Tuexen 			asoc->default_flowlabel |= 0x80000000;
92258bdb691SMichael Tuexen 		} else {
923f8829a4aSRandall Stewart 			asoc->default_flowlabel = 0;
92458bdb691SMichael Tuexen 		}
92558bdb691SMichael Tuexen 	}
926f8829a4aSRandall Stewart #endif
9279f22f500SRandall Stewart 	asoc->sb_send_resv = 0;
928f8829a4aSRandall Stewart 	if (override_tag) {
929f8829a4aSRandall Stewart 		asoc->my_vtag = override_tag;
930f8829a4aSRandall Stewart 	} else {
931a1cb341bSMichael Tuexen 		asoc->my_vtag = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
932f8829a4aSRandall Stewart 	}
933de0e935bSRandall Stewart 	/* Get the nonce tags */
934a1cb341bSMichael Tuexen 	asoc->my_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
935a1cb341bSMichael Tuexen 	asoc->peer_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
93642551e99SRandall Stewart 	asoc->vrf_id = vrf_id;
937de0e935bSRandall Stewart 
93818e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
93918e198d3SRandall Stewart 	asoc->tsn_in_at = 0;
94018e198d3SRandall Stewart 	asoc->tsn_out_at = 0;
94118e198d3SRandall Stewart 	asoc->tsn_in_wrapped = 0;
94218e198d3SRandall Stewart 	asoc->tsn_out_wrapped = 0;
94318e198d3SRandall Stewart 	asoc->cumack_log_at = 0;
944b201f536SRandall Stewart 	asoc->cumack_log_atsnt = 0;
94518e198d3SRandall Stewart #endif
94618e198d3SRandall Stewart #ifdef SCTP_FS_SPEC_LOG
94718e198d3SRandall Stewart 	asoc->fs_index = 0;
94818e198d3SRandall Stewart #endif
949f8829a4aSRandall Stewart 	asoc->refcnt = 0;
950f8829a4aSRandall Stewart 	asoc->assoc_up_sent = 0;
951f8829a4aSRandall Stewart 	asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number = asoc->sending_seq =
952a1cb341bSMichael Tuexen 	    sctp_select_initial_TSN(&inp->sctp_ep);
953c54a18d2SRandall Stewart 	asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
954f8829a4aSRandall Stewart 	/* we are optimisitic here */
955f8829a4aSRandall Stewart 	asoc->peer_supports_pktdrop = 1;
956830d754dSRandall Stewart 	asoc->peer_supports_nat = 0;
957f8829a4aSRandall Stewart 	asoc->sent_queue_retran_cnt = 0;
958f8829a4aSRandall Stewart 
959f8829a4aSRandall Stewart 	/* for CMT */
9608933fa13SRandall Stewart 	asoc->last_net_cmt_send_started = NULL;
961f8829a4aSRandall Stewart 
962f8829a4aSRandall Stewart 	/* This will need to be adjusted */
963f8829a4aSRandall Stewart 	asoc->last_acked_seq = asoc->init_seq_number - 1;
964f8829a4aSRandall Stewart 	asoc->advanced_peer_ack_point = asoc->last_acked_seq;
965f8829a4aSRandall Stewart 	asoc->asconf_seq_in = asoc->last_acked_seq;
966f8829a4aSRandall Stewart 
967f8829a4aSRandall Stewart 	/* here we are different, we hold the next one we expect */
968f8829a4aSRandall Stewart 	asoc->str_reset_seq_in = asoc->last_acked_seq + 1;
969f8829a4aSRandall Stewart 
970a1cb341bSMichael Tuexen 	asoc->initial_init_rto_max = inp->sctp_ep.initial_init_rto_max;
971a1cb341bSMichael Tuexen 	asoc->initial_rto = inp->sctp_ep.initial_rto;
972f8829a4aSRandall Stewart 
973a1cb341bSMichael Tuexen 	asoc->max_init_times = inp->sctp_ep.max_init_times;
974a1cb341bSMichael Tuexen 	asoc->max_send_times = inp->sctp_ep.max_send_times;
975a1cb341bSMichael Tuexen 	asoc->def_net_failure = inp->sctp_ep.def_net_failure;
976a1cb341bSMichael Tuexen 	asoc->def_net_pf_threshold = inp->sctp_ep.def_net_pf_threshold;
977f8829a4aSRandall Stewart 	asoc->free_chunk_cnt = 0;
978f8829a4aSRandall Stewart 
979f8829a4aSRandall Stewart 	asoc->iam_blocking = 0;
980a1cb341bSMichael Tuexen 	asoc->context = inp->sctp_context;
981a1cb341bSMichael Tuexen 	asoc->local_strreset_support = inp->local_strreset_support;
982a1cb341bSMichael Tuexen 	asoc->def_send = inp->def_send;
983a1cb341bSMichael Tuexen 	asoc->delayed_ack = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
984a1cb341bSMichael Tuexen 	asoc->sack_freq = inp->sctp_ep.sctp_sack_freq;
985f8829a4aSRandall Stewart 	asoc->pr_sctp_cnt = 0;
986f8829a4aSRandall Stewart 	asoc->total_output_queue_size = 0;
987f8829a4aSRandall Stewart 
988a1cb341bSMichael Tuexen 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
989a1cb341bSMichael Tuexen 		asoc->scope.ipv6_addr_legal = 1;
990a1cb341bSMichael Tuexen 		if (SCTP_IPV6_V6ONLY(inp) == 0) {
991a1cb341bSMichael Tuexen 			asoc->scope.ipv4_addr_legal = 1;
992f8829a4aSRandall Stewart 		} else {
993a1cb341bSMichael Tuexen 			asoc->scope.ipv4_addr_legal = 0;
994f8829a4aSRandall Stewart 		}
995f8829a4aSRandall Stewart 	} else {
996a1cb341bSMichael Tuexen 		asoc->scope.ipv6_addr_legal = 0;
997a1cb341bSMichael Tuexen 		asoc->scope.ipv4_addr_legal = 1;
998f8829a4aSRandall Stewart 	}
999f8829a4aSRandall Stewart 
1000a1cb341bSMichael Tuexen 	asoc->my_rwnd = max(SCTP_SB_LIMIT_RCV(inp->sctp_socket), SCTP_MINIMAL_RWND);
1001a1cb341bSMichael Tuexen 	asoc->peers_rwnd = SCTP_SB_LIMIT_RCV(inp->sctp_socket);
1002f8829a4aSRandall Stewart 
1003a1cb341bSMichael Tuexen 	asoc->smallest_mtu = inp->sctp_frag_point;
1004a1cb341bSMichael Tuexen 	asoc->minrto = inp->sctp_ep.sctp_minrto;
1005a1cb341bSMichael Tuexen 	asoc->maxrto = inp->sctp_ep.sctp_maxrto;
1006f8829a4aSRandall Stewart 
1007f8829a4aSRandall Stewart 	asoc->locked_on_sending = NULL;
1008f8829a4aSRandall Stewart 	asoc->stream_locked_on = 0;
1009f8829a4aSRandall Stewart 	asoc->ecn_echo_cnt_onq = 0;
1010f8829a4aSRandall Stewart 	asoc->stream_locked = 0;
1011f8829a4aSRandall Stewart 
101242551e99SRandall Stewart 	asoc->send_sack = 1;
101342551e99SRandall Stewart 
101442551e99SRandall Stewart 	LIST_INIT(&asoc->sctp_restricted_addrs);
101542551e99SRandall Stewart 
1016f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->nets);
1017f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->pending_reply_queue);
10182afb3e84SRandall Stewart 	TAILQ_INIT(&asoc->asconf_ack_sent);
1019f8829a4aSRandall Stewart 	/* Setup to fill the hb random cache at first HB */
1020f8829a4aSRandall Stewart 	asoc->hb_random_idx = 4;
1021f8829a4aSRandall Stewart 
1022a1cb341bSMichael Tuexen 	asoc->sctp_autoclose_ticks = inp->sctp_ep.auto_close_time;
1023f8829a4aSRandall Stewart 
1024a1cb341bSMichael Tuexen 	stcb->asoc.congestion_control_module = inp->sctp_ep.sctp_default_cc_module;
1025a1cb341bSMichael Tuexen 	stcb->asoc.cc_functions = sctp_cc_functions[inp->sctp_ep.sctp_default_cc_module];
1026b54d3a6cSRandall Stewart 
1027a1cb341bSMichael Tuexen 	stcb->asoc.stream_scheduling_module = inp->sctp_ep.sctp_default_ss_module;
1028a1cb341bSMichael Tuexen 	stcb->asoc.ss_functions = sctp_ss_functions[inp->sctp_ep.sctp_default_ss_module];
1029f7a77f6fSMichael Tuexen 
1030b54d3a6cSRandall Stewart 	/*
1031f8829a4aSRandall Stewart 	 * Now the stream parameters, here we allocate space for all streams
1032f8829a4aSRandall Stewart 	 * that we request by default.
1033f8829a4aSRandall Stewart 	 */
1034ea44232bSRandall Stewart 	asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams =
1035a1cb341bSMichael Tuexen 	    inp->sctp_ep.pre_open_stream_count;
1036f8829a4aSRandall Stewart 	SCTP_MALLOC(asoc->strmout, struct sctp_stream_out *,
1037f8829a4aSRandall Stewart 	    asoc->streamoutcnt * sizeof(struct sctp_stream_out),
1038207304d4SRandall Stewart 	    SCTP_M_STRMO);
1039f8829a4aSRandall Stewart 	if (asoc->strmout == NULL) {
1040f8829a4aSRandall Stewart 		/* big trouble no memory */
1041c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1042f8829a4aSRandall Stewart 		return (ENOMEM);
1043f8829a4aSRandall Stewart 	}
1044f8829a4aSRandall Stewart 	for (i = 0; i < asoc->streamoutcnt; i++) {
1045f8829a4aSRandall Stewart 		/*
1046f8829a4aSRandall Stewart 		 * inbound side must be set to 0xffff, also NOTE when we get
1047f8829a4aSRandall Stewart 		 * the INIT-ACK back (for INIT sender) we MUST reduce the
1048f8829a4aSRandall Stewart 		 * count (streamoutcnt) but first check if we sent to any of
1049f8829a4aSRandall Stewart 		 * the upper streams that were dropped (if some were). Those
1050f8829a4aSRandall Stewart 		 * that were dropped must be notified to the upper layer as
1051f8829a4aSRandall Stewart 		 * failed to send.
1052f8829a4aSRandall Stewart 		 */
1053f3b05218SMichael Tuexen 		asoc->strmout[i].next_sequence_send = 0x0;
1054f8829a4aSRandall Stewart 		TAILQ_INIT(&asoc->strmout[i].outqueue);
1055325c8c46SMichael Tuexen 		asoc->strmout[i].chunks_on_queues = 0;
1056f8829a4aSRandall Stewart 		asoc->strmout[i].stream_no = i;
1057f8829a4aSRandall Stewart 		asoc->strmout[i].last_msg_incomplete = 0;
1058252f7f93SMichael Tuexen 		asoc->ss_functions.sctp_ss_init_stream(&asoc->strmout[i], NULL);
1059f8829a4aSRandall Stewart 	}
1060f7a77f6fSMichael Tuexen 	asoc->ss_functions.sctp_ss_init(stcb, asoc, 0);
1061f7a77f6fSMichael Tuexen 
1062f8829a4aSRandall Stewart 	/* Now the mapping array */
1063f8829a4aSRandall Stewart 	asoc->mapping_array_size = SCTP_INITIAL_MAPPING_ARRAY;
1064f8829a4aSRandall Stewart 	SCTP_MALLOC(asoc->mapping_array, uint8_t *, asoc->mapping_array_size,
1065207304d4SRandall Stewart 	    SCTP_M_MAP);
1066f8829a4aSRandall Stewart 	if (asoc->mapping_array == NULL) {
1067207304d4SRandall Stewart 		SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1068c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1069f8829a4aSRandall Stewart 		return (ENOMEM);
1070f8829a4aSRandall Stewart 	}
1071f8829a4aSRandall Stewart 	memset(asoc->mapping_array, 0, asoc->mapping_array_size);
1072b5c16493SMichael Tuexen 	SCTP_MALLOC(asoc->nr_mapping_array, uint8_t *, asoc->mapping_array_size,
1073830d754dSRandall Stewart 	    SCTP_M_MAP);
1074bf1be571SRandall Stewart 	if (asoc->nr_mapping_array == NULL) {
1075bf1be571SRandall Stewart 		SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1076bf1be571SRandall Stewart 		SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1077bf1be571SRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1078bf1be571SRandall Stewart 		return (ENOMEM);
1079bf1be571SRandall Stewart 	}
1080b5c16493SMichael Tuexen 	memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
1081830d754dSRandall Stewart 
1082f8829a4aSRandall Stewart 	/* Now the init of the other outqueues */
1083f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->free_chunks);
1084f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->control_send_queue);
1085c54a18d2SRandall Stewart 	TAILQ_INIT(&asoc->asconf_send_queue);
1086f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->send_queue);
1087f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->sent_queue);
1088f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->reasmqueue);
1089f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->resetHead);
1090a1cb341bSMichael Tuexen 	asoc->max_inbound_streams = inp->sctp_ep.max_open_streams_intome;
1091f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->asconf_queue);
1092f8829a4aSRandall Stewart 	/* authentication fields */
1093f8829a4aSRandall Stewart 	asoc->authinfo.random = NULL;
1094830d754dSRandall Stewart 	asoc->authinfo.active_keyid = 0;
1095f8829a4aSRandall Stewart 	asoc->authinfo.assoc_key = NULL;
1096f8829a4aSRandall Stewart 	asoc->authinfo.assoc_keyid = 0;
1097f8829a4aSRandall Stewart 	asoc->authinfo.recv_key = NULL;
1098f8829a4aSRandall Stewart 	asoc->authinfo.recv_keyid = 0;
1099f8829a4aSRandall Stewart 	LIST_INIT(&asoc->shared_keys);
1100f42a358aSRandall Stewart 	asoc->marked_retrans = 0;
1101a1cb341bSMichael Tuexen 	asoc->port = inp->sctp_ep.port;
1102f42a358aSRandall Stewart 	asoc->timoinit = 0;
1103f42a358aSRandall Stewart 	asoc->timodata = 0;
1104f42a358aSRandall Stewart 	asoc->timosack = 0;
1105f42a358aSRandall Stewart 	asoc->timoshutdown = 0;
1106f42a358aSRandall Stewart 	asoc->timoheartbeat = 0;
1107f42a358aSRandall Stewart 	asoc->timocookie = 0;
1108f42a358aSRandall Stewart 	asoc->timoshutdownack = 0;
11096e55db54SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&asoc->start_time);
11106e55db54SRandall Stewart 	asoc->discontinuity_time = asoc->start_time;
1111eacc51c5SRandall Stewart 	/*
1112eacc51c5SRandall Stewart 	 * sa_ignore MEMLEAK {memory is put in the assoc mapping array and
111377acdc25SRandall Stewart 	 * freed later when the association is freed.
1114eacc51c5SRandall Stewart 	 */
1115f8829a4aSRandall Stewart 	return (0);
1116f8829a4aSRandall Stewart }
1117f8829a4aSRandall Stewart 
11180e13104dSRandall Stewart void
11190e13104dSRandall Stewart sctp_print_mapping_array(struct sctp_association *asoc)
11200e13104dSRandall Stewart {
1121aed5947cSMichael Tuexen 	unsigned int i, limit;
11220e13104dSRandall Stewart 
1123cd3fd531SMichael Tuexen 	SCTP_PRINTF("Mapping array size: %d, baseTSN: %8.8x, cumAck: %8.8x, highestTSN: (%8.8x, %8.8x).\n",
11240e13104dSRandall Stewart 	    asoc->mapping_array_size,
11250e13104dSRandall Stewart 	    asoc->mapping_array_base_tsn,
11260e13104dSRandall Stewart 	    asoc->cumulative_tsn,
1127aed5947cSMichael Tuexen 	    asoc->highest_tsn_inside_map,
1128aed5947cSMichael Tuexen 	    asoc->highest_tsn_inside_nr_map);
1129aed5947cSMichael Tuexen 	for (limit = asoc->mapping_array_size; limit > 1; limit--) {
113060990c0cSMichael Tuexen 		if (asoc->mapping_array[limit - 1] != 0) {
113177acdc25SRandall Stewart 			break;
113277acdc25SRandall Stewart 		}
113377acdc25SRandall Stewart 	}
1134cd3fd531SMichael Tuexen 	SCTP_PRINTF("Renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
113577acdc25SRandall Stewart 	for (i = 0; i < limit; i++) {
1136cd3fd531SMichael Tuexen 		SCTP_PRINTF("%2.2x%c", asoc->mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
113777acdc25SRandall Stewart 	}
1138aed5947cSMichael Tuexen 	if (limit % 16)
1139cd3fd531SMichael Tuexen 		SCTP_PRINTF("\n");
1140aed5947cSMichael Tuexen 	for (limit = asoc->mapping_array_size; limit > 1; limit--) {
1141aed5947cSMichael Tuexen 		if (asoc->nr_mapping_array[limit - 1]) {
114277acdc25SRandall Stewart 			break;
114377acdc25SRandall Stewart 		}
114477acdc25SRandall Stewart 	}
1145cd3fd531SMichael Tuexen 	SCTP_PRINTF("Non renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
114677acdc25SRandall Stewart 	for (i = 0; i < limit; i++) {
1147cd3fd531SMichael Tuexen 		SCTP_PRINTF("%2.2x%c", asoc->nr_mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
11480e13104dSRandall Stewart 	}
1149aed5947cSMichael Tuexen 	if (limit % 16)
1150cd3fd531SMichael Tuexen 		SCTP_PRINTF("\n");
11510e13104dSRandall Stewart }
11520e13104dSRandall Stewart 
1153f8829a4aSRandall Stewart int
11540696e120SRandall Stewart sctp_expand_mapping_array(struct sctp_association *asoc, uint32_t needed)
1155f8829a4aSRandall Stewart {
1156f8829a4aSRandall Stewart 	/* mapping array needs to grow */
1157b5c16493SMichael Tuexen 	uint8_t *new_array1, *new_array2;
11580696e120SRandall Stewart 	uint32_t new_size;
1159f8829a4aSRandall Stewart 
11600696e120SRandall Stewart 	new_size = asoc->mapping_array_size + ((needed + 7) / 8 + SCTP_MAPPING_ARRAY_INCR);
1161b5c16493SMichael Tuexen 	SCTP_MALLOC(new_array1, uint8_t *, new_size, SCTP_M_MAP);
1162b5c16493SMichael Tuexen 	SCTP_MALLOC(new_array2, uint8_t *, new_size, SCTP_M_MAP);
1163b5c16493SMichael Tuexen 	if ((new_array1 == NULL) || (new_array2 == NULL)) {
1164f8829a4aSRandall Stewart 		/* can't get more, forget it */
1165b5c16493SMichael Tuexen 		SCTP_PRINTF("No memory for expansion of SCTP mapping array %d\n", new_size);
1166b5c16493SMichael Tuexen 		if (new_array1) {
1167b5c16493SMichael Tuexen 			SCTP_FREE(new_array1, SCTP_M_MAP);
1168b5c16493SMichael Tuexen 		}
1169b5c16493SMichael Tuexen 		if (new_array2) {
1170b5c16493SMichael Tuexen 			SCTP_FREE(new_array2, SCTP_M_MAP);
1171b5c16493SMichael Tuexen 		}
1172f8829a4aSRandall Stewart 		return (-1);
1173f8829a4aSRandall Stewart 	}
1174b5c16493SMichael Tuexen 	memset(new_array1, 0, new_size);
1175b5c16493SMichael Tuexen 	memset(new_array2, 0, new_size);
1176b5c16493SMichael Tuexen 	memcpy(new_array1, asoc->mapping_array, asoc->mapping_array_size);
1177b5c16493SMichael Tuexen 	memcpy(new_array2, asoc->nr_mapping_array, asoc->mapping_array_size);
1178207304d4SRandall Stewart 	SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1179830d754dSRandall Stewart 	SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
1180b5c16493SMichael Tuexen 	asoc->mapping_array = new_array1;
1181b5c16493SMichael Tuexen 	asoc->nr_mapping_array = new_array2;
1182b5c16493SMichael Tuexen 	asoc->mapping_array_size = new_size;
1183830d754dSRandall Stewart 	return (0);
1184830d754dSRandall Stewart }
1185830d754dSRandall Stewart 
11868933fa13SRandall Stewart 
118742551e99SRandall Stewart static void
118842551e99SRandall Stewart sctp_iterator_work(struct sctp_iterator *it)
118942551e99SRandall Stewart {
119042551e99SRandall Stewart 	int iteration_count = 0;
119142551e99SRandall Stewart 	int inp_skip = 0;
1192ec4c19fcSRandall Stewart 	int first_in = 1;
1193ec4c19fcSRandall Stewart 	struct sctp_inpcb *tinp;
119442551e99SRandall Stewart 
1195ec4c19fcSRandall Stewart 	SCTP_INP_INFO_RLOCK();
119642551e99SRandall Stewart 	SCTP_ITERATOR_LOCK();
1197ad81507eSRandall Stewart 	if (it->inp) {
1198ec4c19fcSRandall Stewart 		SCTP_INP_RLOCK(it->inp);
119942551e99SRandall Stewart 		SCTP_INP_DECR_REF(it->inp);
1200ad81507eSRandall Stewart 	}
120142551e99SRandall Stewart 	if (it->inp == NULL) {
120242551e99SRandall Stewart 		/* iterator is complete */
120342551e99SRandall Stewart done_with_iterator:
120442551e99SRandall Stewart 		SCTP_ITERATOR_UNLOCK();
1205ec4c19fcSRandall Stewart 		SCTP_INP_INFO_RUNLOCK();
120642551e99SRandall Stewart 		if (it->function_atend != NULL) {
120742551e99SRandall Stewart 			(*it->function_atend) (it->pointer, it->val);
120842551e99SRandall Stewart 		}
1209207304d4SRandall Stewart 		SCTP_FREE(it, SCTP_M_ITER);
121042551e99SRandall Stewart 		return;
121142551e99SRandall Stewart 	}
121242551e99SRandall Stewart select_a_new_ep:
1213ec4c19fcSRandall Stewart 	if (first_in) {
1214ec4c19fcSRandall Stewart 		first_in = 0;
1215ec4c19fcSRandall Stewart 	} else {
1216f7517433SRandall Stewart 		SCTP_INP_RLOCK(it->inp);
1217ec4c19fcSRandall Stewart 	}
121842551e99SRandall Stewart 	while (((it->pcb_flags) &&
121942551e99SRandall Stewart 	    ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) ||
122042551e99SRandall Stewart 	    ((it->pcb_features) &&
122142551e99SRandall Stewart 	    ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) {
122242551e99SRandall Stewart 		/* endpoint flags or features don't match, so keep looking */
122342551e99SRandall Stewart 		if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1224f7517433SRandall Stewart 			SCTP_INP_RUNLOCK(it->inp);
122542551e99SRandall Stewart 			goto done_with_iterator;
122642551e99SRandall Stewart 		}
1227ec4c19fcSRandall Stewart 		tinp = it->inp;
122842551e99SRandall Stewart 		it->inp = LIST_NEXT(it->inp, sctp_list);
1229ec4c19fcSRandall Stewart 		SCTP_INP_RUNLOCK(tinp);
123042551e99SRandall Stewart 		if (it->inp == NULL) {
123142551e99SRandall Stewart 			goto done_with_iterator;
123242551e99SRandall Stewart 		}
123342551e99SRandall Stewart 		SCTP_INP_RLOCK(it->inp);
1234f7517433SRandall Stewart 	}
123542551e99SRandall Stewart 	/* now go through each assoc which is in the desired state */
123642551e99SRandall Stewart 	if (it->done_current_ep == 0) {
123742551e99SRandall Stewart 		if (it->function_inp != NULL)
123842551e99SRandall Stewart 			inp_skip = (*it->function_inp) (it->inp, it->pointer, it->val);
123942551e99SRandall Stewart 		it->done_current_ep = 1;
124042551e99SRandall Stewart 	}
124142551e99SRandall Stewart 	if (it->stcb == NULL) {
124242551e99SRandall Stewart 		/* run the per instance function */
124342551e99SRandall Stewart 		it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
124442551e99SRandall Stewart 	}
124542551e99SRandall Stewart 	if ((inp_skip) || it->stcb == NULL) {
124642551e99SRandall Stewart 		if (it->function_inp_end != NULL) {
124742551e99SRandall Stewart 			inp_skip = (*it->function_inp_end) (it->inp,
124842551e99SRandall Stewart 			    it->pointer,
124942551e99SRandall Stewart 			    it->val);
125042551e99SRandall Stewart 		}
125142551e99SRandall Stewart 		SCTP_INP_RUNLOCK(it->inp);
125242551e99SRandall Stewart 		goto no_stcb;
125342551e99SRandall Stewart 	}
125442551e99SRandall Stewart 	while (it->stcb) {
125542551e99SRandall Stewart 		SCTP_TCB_LOCK(it->stcb);
125642551e99SRandall Stewart 		if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
125742551e99SRandall Stewart 			/* not in the right state... keep looking */
125842551e99SRandall Stewart 			SCTP_TCB_UNLOCK(it->stcb);
125942551e99SRandall Stewart 			goto next_assoc;
126042551e99SRandall Stewart 		}
126142551e99SRandall Stewart 		/* see if we have limited out the iterator loop */
126242551e99SRandall Stewart 		iteration_count++;
126342551e99SRandall Stewart 		if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) {
126442551e99SRandall Stewart 			/* Pause to let others grab the lock */
126542551e99SRandall Stewart 			atomic_add_int(&it->stcb->asoc.refcnt, 1);
126642551e99SRandall Stewart 			SCTP_TCB_UNLOCK(it->stcb);
1267c4739e2fSRandall Stewart 			SCTP_INP_INCR_REF(it->inp);
126842551e99SRandall Stewart 			SCTP_INP_RUNLOCK(it->inp);
126942551e99SRandall Stewart 			SCTP_ITERATOR_UNLOCK();
1270ec4c19fcSRandall Stewart 			SCTP_INP_INFO_RUNLOCK();
1271ec4c19fcSRandall Stewart 			SCTP_INP_INFO_RLOCK();
127242551e99SRandall Stewart 			SCTP_ITERATOR_LOCK();
1273f7517433SRandall Stewart 			if (sctp_it_ctl.iterator_flags) {
1274f7517433SRandall Stewart 				/* We won't be staying here */
1275f7517433SRandall Stewart 				SCTP_INP_DECR_REF(it->inp);
1276f7517433SRandall Stewart 				atomic_add_int(&it->stcb->asoc.refcnt, -1);
1277f7517433SRandall Stewart 				if (sctp_it_ctl.iterator_flags &
1278f7517433SRandall Stewart 				    SCTP_ITERATOR_STOP_CUR_IT) {
1279f7517433SRandall Stewart 					sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_IT;
1280f7517433SRandall Stewart 					goto done_with_iterator;
1281f7517433SRandall Stewart 				}
1282f7517433SRandall Stewart 				if (sctp_it_ctl.iterator_flags &
1283f7517433SRandall Stewart 				    SCTP_ITERATOR_STOP_CUR_INP) {
1284f7517433SRandall Stewart 					sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_INP;
1285f7517433SRandall Stewart 					goto no_stcb;
1286f7517433SRandall Stewart 				}
1287f7517433SRandall Stewart 				/* If we reach here huh? */
1288cd3fd531SMichael Tuexen 				SCTP_PRINTF("Unknown it ctl flag %x\n",
1289f7517433SRandall Stewart 				    sctp_it_ctl.iterator_flags);
1290f7517433SRandall Stewart 				sctp_it_ctl.iterator_flags = 0;
1291f7517433SRandall Stewart 			}
129242551e99SRandall Stewart 			SCTP_INP_RLOCK(it->inp);
1293c4739e2fSRandall Stewart 			SCTP_INP_DECR_REF(it->inp);
129442551e99SRandall Stewart 			SCTP_TCB_LOCK(it->stcb);
129542551e99SRandall Stewart 			atomic_add_int(&it->stcb->asoc.refcnt, -1);
129642551e99SRandall Stewart 			iteration_count = 0;
129742551e99SRandall Stewart 		}
129842551e99SRandall Stewart 		/* run function on this one */
129942551e99SRandall Stewart 		(*it->function_assoc) (it->inp, it->stcb, it->pointer, it->val);
130042551e99SRandall Stewart 
130142551e99SRandall Stewart 		/*
130242551e99SRandall Stewart 		 * we lie here, it really needs to have its own type but
130342551e99SRandall Stewart 		 * first I must verify that this won't effect things :-0
130442551e99SRandall Stewart 		 */
130542551e99SRandall Stewart 		if (it->no_chunk_output == 0)
1306ceaad40aSRandall Stewart 			sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
130742551e99SRandall Stewart 
130842551e99SRandall Stewart 		SCTP_TCB_UNLOCK(it->stcb);
130942551e99SRandall Stewart next_assoc:
131042551e99SRandall Stewart 		it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
131142551e99SRandall Stewart 		if (it->stcb == NULL) {
131242551e99SRandall Stewart 			/* Run last function */
131342551e99SRandall Stewart 			if (it->function_inp_end != NULL) {
131442551e99SRandall Stewart 				inp_skip = (*it->function_inp_end) (it->inp,
131542551e99SRandall Stewart 				    it->pointer,
131642551e99SRandall Stewart 				    it->val);
131742551e99SRandall Stewart 			}
131842551e99SRandall Stewart 		}
131942551e99SRandall Stewart 	}
132042551e99SRandall Stewart 	SCTP_INP_RUNLOCK(it->inp);
132142551e99SRandall Stewart no_stcb:
132242551e99SRandall Stewart 	/* done with all assocs on this endpoint, move on to next endpoint */
132342551e99SRandall Stewart 	it->done_current_ep = 0;
132442551e99SRandall Stewart 	if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
132542551e99SRandall Stewart 		it->inp = NULL;
132642551e99SRandall Stewart 	} else {
132742551e99SRandall Stewart 		it->inp = LIST_NEXT(it->inp, sctp_list);
132842551e99SRandall Stewart 	}
132942551e99SRandall Stewart 	if (it->inp == NULL) {
133042551e99SRandall Stewart 		goto done_with_iterator;
133142551e99SRandall Stewart 	}
133242551e99SRandall Stewart 	goto select_a_new_ep;
133342551e99SRandall Stewart }
133442551e99SRandall Stewart 
133542551e99SRandall Stewart void
133642551e99SRandall Stewart sctp_iterator_worker(void)
133742551e99SRandall Stewart {
13384a9ef3f8SMichael Tuexen 	struct sctp_iterator *it, *nit;
133942551e99SRandall Stewart 
134042551e99SRandall Stewart 	/* This function is called with the WQ lock in place */
134142551e99SRandall Stewart 
1342f7517433SRandall Stewart 	sctp_it_ctl.iterator_running = 1;
13434a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
13444a9ef3f8SMichael Tuexen 		sctp_it_ctl.cur_it = it;
134542551e99SRandall Stewart 		/* now lets work on this one */
1346f7517433SRandall Stewart 		TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
134742551e99SRandall Stewart 		SCTP_IPI_ITERATOR_WQ_UNLOCK();
1348f7517433SRandall Stewart 		CURVNET_SET(it->vn);
134942551e99SRandall Stewart 		sctp_iterator_work(it);
1350f79aab18SRandall Stewart 		sctp_it_ctl.cur_it = NULL;
1351f7517433SRandall Stewart 		CURVNET_RESTORE();
135242551e99SRandall Stewart 		SCTP_IPI_ITERATOR_WQ_LOCK();
13533c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
135442551e99SRandall Stewart 	}
1355f7517433SRandall Stewart 	sctp_it_ctl.iterator_running = 0;
135642551e99SRandall Stewart 	return;
135742551e99SRandall Stewart }
135842551e99SRandall Stewart 
1359f8829a4aSRandall Stewart 
1360f8829a4aSRandall Stewart static void
1361f8829a4aSRandall Stewart sctp_handle_addr_wq(void)
1362f8829a4aSRandall Stewart {
1363f8829a4aSRandall Stewart 	/* deal with the ADDR wq from the rtsock calls */
13644a9ef3f8SMichael Tuexen 	struct sctp_laddr *wi, *nwi;
136542551e99SRandall Stewart 	struct sctp_asconf_iterator *asc;
1366f8829a4aSRandall Stewart 
136742551e99SRandall Stewart 	SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
1368207304d4SRandall Stewart 	    sizeof(struct sctp_asconf_iterator), SCTP_M_ASC_IT);
136942551e99SRandall Stewart 	if (asc == NULL) {
137042551e99SRandall Stewart 		/* Try later, no memory */
1371f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
1372f8829a4aSRandall Stewart 		    (struct sctp_inpcb *)NULL,
1373f8829a4aSRandall Stewart 		    (struct sctp_tcb *)NULL,
1374f8829a4aSRandall Stewart 		    (struct sctp_nets *)NULL);
137542551e99SRandall Stewart 		return;
1376f8829a4aSRandall Stewart 	}
137742551e99SRandall Stewart 	LIST_INIT(&asc->list_of_work);
137842551e99SRandall Stewart 	asc->cnt = 0;
1379f7517433SRandall Stewart 
1380f7517433SRandall Stewart 	SCTP_WQ_ADDR_LOCK();
13814a9ef3f8SMichael Tuexen 	LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
138242551e99SRandall Stewart 		LIST_REMOVE(wi, sctp_nxt_addr);
138342551e99SRandall Stewart 		LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
138442551e99SRandall Stewart 		asc->cnt++;
1385f8829a4aSRandall Stewart 	}
1386f7517433SRandall Stewart 	SCTP_WQ_ADDR_UNLOCK();
1387f7517433SRandall Stewart 
138842551e99SRandall Stewart 	if (asc->cnt == 0) {
1389207304d4SRandall Stewart 		SCTP_FREE(asc, SCTP_M_ASC_IT);
139042551e99SRandall Stewart 	} else {
13911b649582SRandall Stewart 		(void)sctp_initiate_iterator(sctp_asconf_iterator_ep,
13921b649582SRandall Stewart 		    sctp_asconf_iterator_stcb,
139342551e99SRandall Stewart 		    NULL,	/* No ep end for boundall */
139442551e99SRandall Stewart 		    SCTP_PCB_FLAGS_BOUNDALL,
139542551e99SRandall Stewart 		    SCTP_PCB_ANY_FEATURES,
13961b649582SRandall Stewart 		    SCTP_ASOC_ANY_STATE,
13971b649582SRandall Stewart 		    (void *)asc, 0,
13981b649582SRandall Stewart 		    sctp_asconf_iterator_end, NULL, 0);
139942551e99SRandall Stewart 	}
1400f8829a4aSRandall Stewart }
1401f8829a4aSRandall Stewart 
1402f8829a4aSRandall Stewart void
1403f8829a4aSRandall Stewart sctp_timeout_handler(void *t)
1404f8829a4aSRandall Stewart {
1405f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
1406f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
1407f8829a4aSRandall Stewart 	struct sctp_nets *net;
1408f8829a4aSRandall Stewart 	struct sctp_timer *tmr;
1409ceaad40aSRandall Stewart 
1410ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1411ceaad40aSRandall Stewart 	struct socket *so;
1412ceaad40aSRandall Stewart 
1413ceaad40aSRandall Stewart #endif
1414d61374e1SRandall Stewart 	int did_output, type;
1415f8829a4aSRandall Stewart 
1416f8829a4aSRandall Stewart 	tmr = (struct sctp_timer *)t;
1417f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)tmr->ep;
1418f8829a4aSRandall Stewart 	stcb = (struct sctp_tcb *)tmr->tcb;
1419f8829a4aSRandall Stewart 	net = (struct sctp_nets *)tmr->net;
14208518270eSMichael Tuexen 	CURVNET_SET((struct vnet *)tmr->vnet);
1421f8829a4aSRandall Stewart 	did_output = 1;
1422f8829a4aSRandall Stewart 
1423f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1424f8829a4aSRandall Stewart 	sctp_audit_log(0xF0, (uint8_t) tmr->type);
1425f8829a4aSRandall Stewart 	sctp_auditing(3, inp, stcb, net);
1426f8829a4aSRandall Stewart #endif
1427f8829a4aSRandall Stewart 
1428f8829a4aSRandall Stewart 	/* sanity checks... */
1429f8829a4aSRandall Stewart 	if (tmr->self != (void *)tmr) {
1430f8829a4aSRandall Stewart 		/*
1431ad81507eSRandall Stewart 		 * SCTP_PRINTF("Stale SCTP timer fired (%p), ignoring...\n",
1432dd294dceSMichael Tuexen 		 * (void *)tmr);
1433f8829a4aSRandall Stewart 		 */
14348518270eSMichael Tuexen 		CURVNET_RESTORE();
1435f8829a4aSRandall Stewart 		return;
1436f8829a4aSRandall Stewart 	}
1437a5d547adSRandall Stewart 	tmr->stopped_from = 0xa001;
1438f8829a4aSRandall Stewart 	if (!SCTP_IS_TIMER_TYPE_VALID(tmr->type)) {
1439f8829a4aSRandall Stewart 		/*
1440ad81507eSRandall Stewart 		 * SCTP_PRINTF("SCTP timer fired with invalid type: 0x%x\n",
1441f8829a4aSRandall Stewart 		 * tmr->type);
1442f8829a4aSRandall Stewart 		 */
14438518270eSMichael Tuexen 		CURVNET_RESTORE();
1444f8829a4aSRandall Stewart 		return;
1445f8829a4aSRandall Stewart 	}
1446a5d547adSRandall Stewart 	tmr->stopped_from = 0xa002;
1447f8829a4aSRandall Stewart 	if ((tmr->type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL)) {
14488518270eSMichael Tuexen 		CURVNET_RESTORE();
1449f8829a4aSRandall Stewart 		return;
1450f8829a4aSRandall Stewart 	}
1451f8829a4aSRandall Stewart 	/* if this is an iterator timeout, get the struct and clear inp */
1452a5d547adSRandall Stewart 	tmr->stopped_from = 0xa003;
1453d61374e1SRandall Stewart 	type = tmr->type;
1454f8829a4aSRandall Stewart 	if (inp) {
1455f8829a4aSRandall Stewart 		SCTP_INP_INCR_REF(inp);
1456aa1808b7SMichael Tuexen 		if ((inp->sctp_socket == NULL) &&
1457f8829a4aSRandall Stewart 		    ((tmr->type != SCTP_TIMER_TYPE_INPKILL) &&
1458810ec536SMichael Tuexen 		    (tmr->type != SCTP_TIMER_TYPE_INIT) &&
1459a1e13272SRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_SEND) &&
1460a1e13272SRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_RECV) &&
1461a1e13272SRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_HEARTBEAT) &&
1462f8829a4aSRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_SHUTDOWN) &&
1463f8829a4aSRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNACK) &&
1464f8829a4aSRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNGUARD) &&
1465f8829a4aSRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_ASOCKILL))
1466f8829a4aSRandall Stewart 		    ) {
1467f8829a4aSRandall Stewart 			SCTP_INP_DECR_REF(inp);
14688518270eSMichael Tuexen 			CURVNET_RESTORE();
1469f8829a4aSRandall Stewart 			return;
1470f8829a4aSRandall Stewart 		}
1471f8829a4aSRandall Stewart 	}
1472a5d547adSRandall Stewart 	tmr->stopped_from = 0xa004;
1473f8829a4aSRandall Stewart 	if (stcb) {
1474c105859eSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
1475f8829a4aSRandall Stewart 		if (stcb->asoc.state == 0) {
1476c105859eSRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, -1);
1477f8829a4aSRandall Stewart 			if (inp) {
1478f8829a4aSRandall Stewart 				SCTP_INP_DECR_REF(inp);
1479f8829a4aSRandall Stewart 			}
14808518270eSMichael Tuexen 			CURVNET_RESTORE();
1481f8829a4aSRandall Stewart 			return;
1482f8829a4aSRandall Stewart 		}
1483f8829a4aSRandall Stewart 	}
1484a5d547adSRandall Stewart 	tmr->stopped_from = 0xa005;
1485ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_TIMER1, "Timer type %d goes off\n", tmr->type);
1486139bc87fSRandall Stewart 	if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1487f8829a4aSRandall Stewart 		if (inp) {
1488f8829a4aSRandall Stewart 			SCTP_INP_DECR_REF(inp);
1489f8829a4aSRandall Stewart 		}
1490207304d4SRandall Stewart 		if (stcb) {
1491207304d4SRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, -1);
1492207304d4SRandall Stewart 		}
14938518270eSMichael Tuexen 		CURVNET_RESTORE();
1494f8829a4aSRandall Stewart 		return;
1495f8829a4aSRandall Stewart 	}
1496a5d547adSRandall Stewart 	tmr->stopped_from = 0xa006;
1497a5d547adSRandall Stewart 
1498f8829a4aSRandall Stewart 	if (stcb) {
1499f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
150050cec919SRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, -1);
1501b54d3a6cSRandall Stewart 		if ((tmr->type != SCTP_TIMER_TYPE_ASOCKILL) &&
1502b54d3a6cSRandall Stewart 		    ((stcb->asoc.state == 0) ||
1503b54d3a6cSRandall Stewart 		    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED))) {
1504b54d3a6cSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
1505b54d3a6cSRandall Stewart 			if (inp) {
1506b54d3a6cSRandall Stewart 				SCTP_INP_DECR_REF(inp);
1507b54d3a6cSRandall Stewart 			}
15088518270eSMichael Tuexen 			CURVNET_RESTORE();
1509b54d3a6cSRandall Stewart 			return;
1510b54d3a6cSRandall Stewart 		}
1511f8829a4aSRandall Stewart 	}
151244b7479bSRandall Stewart 	/* record in stopped what t-o occured */
151344b7479bSRandall Stewart 	tmr->stopped_from = tmr->type;
151444b7479bSRandall Stewart 
1515f8829a4aSRandall Stewart 	/* mark as being serviced now */
151644b7479bSRandall Stewart 	if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
151744b7479bSRandall Stewart 		/*
151844b7479bSRandall Stewart 		 * Callout has been rescheduled.
151944b7479bSRandall Stewart 		 */
152044b7479bSRandall Stewart 		goto get_out;
152144b7479bSRandall Stewart 	}
152244b7479bSRandall Stewart 	if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
152344b7479bSRandall Stewart 		/*
152444b7479bSRandall Stewart 		 * Not active, so no action.
152544b7479bSRandall Stewart 		 */
152644b7479bSRandall Stewart 		goto get_out;
152744b7479bSRandall Stewart 	}
1528139bc87fSRandall Stewart 	SCTP_OS_TIMER_DEACTIVATE(&tmr->timer);
1529f8829a4aSRandall Stewart 
1530f8829a4aSRandall Stewart 	/* call the handler for the appropriate timer type */
1531f8829a4aSRandall Stewart 	switch (tmr->type) {
1532d61a0ae0SRandall Stewart 	case SCTP_TIMER_TYPE_ZERO_COPY:
1533eacc51c5SRandall Stewart 		if (inp == NULL) {
1534eacc51c5SRandall Stewart 			break;
1535eacc51c5SRandall Stewart 		}
1536d61a0ae0SRandall Stewart 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
1537d61a0ae0SRandall Stewart 			SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
1538d61a0ae0SRandall Stewart 		}
1539d61a0ae0SRandall Stewart 		break;
1540ad21a364SRandall Stewart 	case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
1541eacc51c5SRandall Stewart 		if (inp == NULL) {
1542eacc51c5SRandall Stewart 			break;
1543eacc51c5SRandall Stewart 		}
1544ad21a364SRandall Stewart 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
1545ad21a364SRandall Stewart 			SCTP_ZERO_COPY_SENDQ_EVENT(inp, inp->sctp_socket);
1546ad21a364SRandall Stewart 		}
1547ad21a364SRandall Stewart 		break;
1548f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ADDR_WQ:
1549f8829a4aSRandall Stewart 		sctp_handle_addr_wq();
1550f8829a4aSRandall Stewart 		break;
1551f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SEND:
1552ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1553ad81507eSRandall Stewart 			break;
1554ad81507eSRandall Stewart 		}
1555f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timodata);
1556f42a358aSRandall Stewart 		stcb->asoc.timodata++;
1557f8829a4aSRandall Stewart 		stcb->asoc.num_send_timers_up--;
1558f8829a4aSRandall Stewart 		if (stcb->asoc.num_send_timers_up < 0) {
1559f8829a4aSRandall Stewart 			stcb->asoc.num_send_timers_up = 0;
1560f8829a4aSRandall Stewart 		}
1561b54d3a6cSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
156260990c0cSMichael Tuexen 		if (sctp_t3rxt_timer(inp, stcb, net)) {
1563f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1564f8829a4aSRandall Stewart 
1565f8829a4aSRandall Stewart 			goto out_decr;
1566f8829a4aSRandall Stewart 		}
1567b54d3a6cSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1568f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1569f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1570f8829a4aSRandall Stewart #endif
1571ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1572f8829a4aSRandall Stewart 		if ((stcb->asoc.num_send_timers_up == 0) &&
15734a9ef3f8SMichael Tuexen 		    (stcb->asoc.sent_queue_cnt > 0)) {
1574f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *chk;
1575f8829a4aSRandall Stewart 
1576f8829a4aSRandall Stewart 			/*
1577f8829a4aSRandall Stewart 			 * safeguard. If there on some on the sent queue
1578f8829a4aSRandall Stewart 			 * somewhere but no timers running something is
1579f8829a4aSRandall Stewart 			 * wrong... so we start a timer on the first chunk
1580f8829a4aSRandall Stewart 			 * on the send queue on whatever net it is sent to.
1581f8829a4aSRandall Stewart 			 */
1582f8829a4aSRandall Stewart 			chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
1583f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb,
1584f8829a4aSRandall Stewart 			    chk->whoTo);
1585f8829a4aSRandall Stewart 		}
1586f8829a4aSRandall Stewart 		break;
1587f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
1588ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1589ad81507eSRandall Stewart 			break;
1590ad81507eSRandall Stewart 		}
1591f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoinit);
1592f42a358aSRandall Stewart 		stcb->asoc.timoinit++;
1593f8829a4aSRandall Stewart 		if (sctp_t1init_timer(inp, stcb, net)) {
1594f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1595f8829a4aSRandall Stewart 			goto out_decr;
1596f8829a4aSRandall Stewart 		}
1597f8829a4aSRandall Stewart 		/* We do output but not here */
1598f8829a4aSRandall Stewart 		did_output = 0;
1599f8829a4aSRandall Stewart 		break;
1600f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
1601ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1602ad81507eSRandall Stewart 			break;
1603ca85e948SMichael Tuexen 		}
1604f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timosack);
1605f42a358aSRandall Stewart 		stcb->asoc.timosack++;
1606689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
1607f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1608f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1609f8829a4aSRandall Stewart #endif
1610ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SACK_TMR, SCTP_SO_NOT_LOCKED);
1611f8829a4aSRandall Stewart 		break;
1612f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
1613ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1614ad81507eSRandall Stewart 			break;
1615ad81507eSRandall Stewart 		}
1616f8829a4aSRandall Stewart 		if (sctp_shutdown_timer(inp, stcb, net)) {
1617f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1618f8829a4aSRandall Stewart 			goto out_decr;
1619f8829a4aSRandall Stewart 		}
1620f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoshutdown);
1621f42a358aSRandall Stewart 		stcb->asoc.timoshutdown++;
1622f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1623f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1624f8829a4aSRandall Stewart #endif
1625ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_NOT_LOCKED);
1626f8829a4aSRandall Stewart 		break;
1627f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
1628ca85e948SMichael Tuexen 		if ((stcb == NULL) || (inp == NULL) || (net == NULL)) {
1629ad81507eSRandall Stewart 			break;
1630ad81507eSRandall Stewart 		}
1631f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoheartbeat);
1632f42a358aSRandall Stewart 		stcb->asoc.timoheartbeat++;
1633ca85e948SMichael Tuexen 		if (sctp_heartbeat_timer(inp, stcb, net)) {
1634f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1635f8829a4aSRandall Stewart 			goto out_decr;
1636f8829a4aSRandall Stewart 		}
1637f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1638ca85e948SMichael Tuexen 		sctp_auditing(4, inp, stcb, net);
1639f8829a4aSRandall Stewart #endif
1640ca85e948SMichael Tuexen 		if (!(net->dest_state & SCTP_ADDR_NOHB)) {
1641629749b6SMichael Tuexen 			sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1642ceaad40aSRandall Stewart 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_HB_TMR, SCTP_SO_NOT_LOCKED);
1643f8829a4aSRandall Stewart 		}
1644f8829a4aSRandall Stewart 		break;
1645f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
1646ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1647ad81507eSRandall Stewart 			break;
1648ad81507eSRandall Stewart 		}
1649f8829a4aSRandall Stewart 		if (sctp_cookie_timer(inp, stcb, net)) {
1650f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1651f8829a4aSRandall Stewart 			goto out_decr;
1652f8829a4aSRandall Stewart 		}
1653f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timocookie);
1654f42a358aSRandall Stewart 		stcb->asoc.timocookie++;
1655f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1656f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1657f8829a4aSRandall Stewart #endif
1658f8829a4aSRandall Stewart 		/*
1659f8829a4aSRandall Stewart 		 * We consider T3 and Cookie timer pretty much the same with
1660f8829a4aSRandall Stewart 		 * respect to where from in chunk_output.
1661f8829a4aSRandall Stewart 		 */
1662ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1663f8829a4aSRandall Stewart 		break;
1664f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_NEWCOOKIE:
1665f8829a4aSRandall Stewart 		{
1666f8829a4aSRandall Stewart 			struct timeval tv;
1667f8829a4aSRandall Stewart 			int i, secret;
1668f8829a4aSRandall Stewart 
1669ad81507eSRandall Stewart 			if (inp == NULL) {
1670ad81507eSRandall Stewart 				break;
1671ad81507eSRandall Stewart 			}
1672f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_timosecret);
16736e55db54SRandall Stewart 			(void)SCTP_GETTIME_TIMEVAL(&tv);
1674f8829a4aSRandall Stewart 			SCTP_INP_WLOCK(inp);
1675f8829a4aSRandall Stewart 			inp->sctp_ep.time_of_secret_change = tv.tv_sec;
1676f8829a4aSRandall Stewart 			inp->sctp_ep.last_secret_number =
1677f8829a4aSRandall Stewart 			    inp->sctp_ep.current_secret_number;
1678f8829a4aSRandall Stewart 			inp->sctp_ep.current_secret_number++;
1679f8829a4aSRandall Stewart 			if (inp->sctp_ep.current_secret_number >=
1680f8829a4aSRandall Stewart 			    SCTP_HOW_MANY_SECRETS) {
1681f8829a4aSRandall Stewart 				inp->sctp_ep.current_secret_number = 0;
1682f8829a4aSRandall Stewart 			}
1683f8829a4aSRandall Stewart 			secret = (int)inp->sctp_ep.current_secret_number;
1684f8829a4aSRandall Stewart 			for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1685f8829a4aSRandall Stewart 				inp->sctp_ep.secret_key[secret][i] =
1686f8829a4aSRandall Stewart 				    sctp_select_initial_TSN(&inp->sctp_ep);
1687f8829a4aSRandall Stewart 			}
1688f8829a4aSRandall Stewart 			SCTP_INP_WUNLOCK(inp);
1689f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, stcb, net);
1690f8829a4aSRandall Stewart 		}
1691f8829a4aSRandall Stewart 		did_output = 0;
1692f8829a4aSRandall Stewart 		break;
1693f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
1694ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1695ad81507eSRandall Stewart 			break;
1696ad81507eSRandall Stewart 		}
1697f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timopathmtu);
1698f8829a4aSRandall Stewart 		sctp_pathmtu_timer(inp, stcb, net);
1699f8829a4aSRandall Stewart 		did_output = 0;
1700f8829a4aSRandall Stewart 		break;
1701f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
1702ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1703ad81507eSRandall Stewart 			break;
1704ad81507eSRandall Stewart 		}
1705f8829a4aSRandall Stewart 		if (sctp_shutdownack_timer(inp, stcb, net)) {
1706f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1707f8829a4aSRandall Stewart 			goto out_decr;
1708f8829a4aSRandall Stewart 		}
1709f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoshutdownack);
1710f42a358aSRandall Stewart 		stcb->asoc.timoshutdownack++;
1711f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1712f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1713f8829a4aSRandall Stewart #endif
1714ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_ACK_TMR, SCTP_SO_NOT_LOCKED);
1715f8829a4aSRandall Stewart 		break;
1716f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
1717ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1718ad81507eSRandall Stewart 			break;
1719ad81507eSRandall Stewart 		}
1720f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoshutdownguard);
1721a2b42326SMichael Tuexen 		sctp_abort_an_association(inp, stcb, NULL, SCTP_SO_NOT_LOCKED);
1722f8829a4aSRandall Stewart 		/* no need to unlock on tcb its gone */
1723f8829a4aSRandall Stewart 		goto out_decr;
1724f8829a4aSRandall Stewart 
1725f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_STRRESET:
1726ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1727ad81507eSRandall Stewart 			break;
1728ad81507eSRandall Stewart 		}
1729f8829a4aSRandall Stewart 		if (sctp_strreset_timer(inp, stcb, net)) {
1730f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1731f8829a4aSRandall Stewart 			goto out_decr;
1732f8829a4aSRandall Stewart 		}
1733f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timostrmrst);
1734ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_TMR, SCTP_SO_NOT_LOCKED);
1735f8829a4aSRandall Stewart 		break;
1736f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASCONF:
1737ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1738ad81507eSRandall Stewart 			break;
1739ad81507eSRandall Stewart 		}
1740f8829a4aSRandall Stewart 		if (sctp_asconf_timer(inp, stcb, net)) {
1741f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1742f8829a4aSRandall Stewart 			goto out_decr;
1743f8829a4aSRandall Stewart 		}
1744f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoasconf);
1745f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1746f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1747f8829a4aSRandall Stewart #endif
1748ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_ASCONF_TMR, SCTP_SO_NOT_LOCKED);
1749f8829a4aSRandall Stewart 		break;
1750851b7298SRandall Stewart 	case SCTP_TIMER_TYPE_PRIM_DELETED:
1751851b7298SRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1752851b7298SRandall Stewart 			break;
1753851b7298SRandall Stewart 		}
175404ee05e8SRandall Stewart 		sctp_delete_prim_timer(inp, stcb, net);
1755851b7298SRandall Stewart 		SCTP_STAT_INCR(sctps_timodelprim);
1756851b7298SRandall Stewart 		break;
1757f8829a4aSRandall Stewart 
1758f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_AUTOCLOSE:
1759ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1760ad81507eSRandall Stewart 			break;
1761ad81507eSRandall Stewart 		}
1762f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoautoclose);
1763f8829a4aSRandall Stewart 		sctp_autoclose_timer(inp, stcb, net);
1764ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
1765f8829a4aSRandall Stewart 		did_output = 0;
1766f8829a4aSRandall Stewart 		break;
1767f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASOCKILL:
1768ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1769ad81507eSRandall Stewart 			break;
1770ad81507eSRandall Stewart 		}
1771f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoassockill);
1772f8829a4aSRandall Stewart 		/* Can we free it yet? */
1773f8829a4aSRandall Stewart 		SCTP_INP_DECR_REF(inp);
1774a5d547adSRandall Stewart 		sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_1);
1775ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1776ceaad40aSRandall Stewart 		so = SCTP_INP_SO(inp);
1777ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
1778ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
1779ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
1780ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
1781ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
1782ceaad40aSRandall Stewart #endif
1783c4739e2fSRandall Stewart 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_2);
1784ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1785ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
1786ceaad40aSRandall Stewart #endif
1787f8829a4aSRandall Stewart 		/*
1788f8829a4aSRandall Stewart 		 * free asoc, always unlocks (or destroy's) so prevent
1789f8829a4aSRandall Stewart 		 * duplicate unlock or unlock of a free mtx :-0
1790f8829a4aSRandall Stewart 		 */
1791f8829a4aSRandall Stewart 		stcb = NULL;
1792f8829a4aSRandall Stewart 		goto out_no_decr;
1793f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INPKILL:
1794f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoinpkill);
1795ad81507eSRandall Stewart 		if (inp == NULL) {
1796ad81507eSRandall Stewart 			break;
1797ad81507eSRandall Stewart 		}
1798f8829a4aSRandall Stewart 		/*
1799f8829a4aSRandall Stewart 		 * special case, take away our increment since WE are the
1800f8829a4aSRandall Stewart 		 * killer
1801f8829a4aSRandall Stewart 		 */
1802f8829a4aSRandall Stewart 		SCTP_INP_DECR_REF(inp);
1803a5d547adSRandall Stewart 		sctp_timer_stop(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_3);
1804b0552ae2SRandall Stewart 		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
18050c7dc840SRandall Stewart 		    SCTP_CALLED_FROM_INPKILL_TIMER);
1806d61374e1SRandall Stewart 		inp = NULL;
1807f8829a4aSRandall Stewart 		goto out_no_decr;
1808f8829a4aSRandall Stewart 	default:
1809ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER1, "sctp_timeout_handler:unknown timer %d\n",
1810f8829a4aSRandall Stewart 		    tmr->type);
1811f8829a4aSRandall Stewart 		break;
181260990c0cSMichael Tuexen 	}
1813f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1814f8829a4aSRandall Stewart 	sctp_audit_log(0xF1, (uint8_t) tmr->type);
1815f8829a4aSRandall Stewart 	if (inp)
1816f8829a4aSRandall Stewart 		sctp_auditing(5, inp, stcb, net);
1817f8829a4aSRandall Stewart #endif
1818f8829a4aSRandall Stewart 	if ((did_output) && stcb) {
1819f8829a4aSRandall Stewart 		/*
1820f8829a4aSRandall Stewart 		 * Now we need to clean up the control chunk chain if an
1821f8829a4aSRandall Stewart 		 * ECNE is on it. It must be marked as UNSENT again so next
1822f8829a4aSRandall Stewart 		 * call will continue to send it until such time that we get
1823f8829a4aSRandall Stewart 		 * a CWR, to remove it. It is, however, less likely that we
1824f8829a4aSRandall Stewart 		 * will find a ecn echo on the chain though.
1825f8829a4aSRandall Stewart 		 */
1826f8829a4aSRandall Stewart 		sctp_fix_ecn_echo(&stcb->asoc);
1827f8829a4aSRandall Stewart 	}
182844b7479bSRandall Stewart get_out:
1829f8829a4aSRandall Stewart 	if (stcb) {
1830f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
1831f8829a4aSRandall Stewart 	}
1832f8829a4aSRandall Stewart out_decr:
1833f8829a4aSRandall Stewart 	if (inp) {
1834f8829a4aSRandall Stewart 		SCTP_INP_DECR_REF(inp);
1835f8829a4aSRandall Stewart 	}
1836f8829a4aSRandall Stewart out_no_decr:
1837ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_TIMER1, "Timer now complete (type %d)\n",
1838d61374e1SRandall Stewart 	    type);
18398518270eSMichael Tuexen 	CURVNET_RESTORE();
1840f8829a4aSRandall Stewart }
1841f8829a4aSRandall Stewart 
1842ad81507eSRandall Stewart void
1843f8829a4aSRandall Stewart sctp_timer_start(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1844f8829a4aSRandall Stewart     struct sctp_nets *net)
1845f8829a4aSRandall Stewart {
1846ca85e948SMichael Tuexen 	uint32_t to_ticks;
1847f8829a4aSRandall Stewart 	struct sctp_timer *tmr;
1848f8829a4aSRandall Stewart 
1849139bc87fSRandall Stewart 	if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL))
1850ad81507eSRandall Stewart 		return;
1851f8829a4aSRandall Stewart 
1852f8829a4aSRandall Stewart 	tmr = NULL;
1853f8829a4aSRandall Stewart 	if (stcb) {
1854f8829a4aSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1855f8829a4aSRandall Stewart 	}
1856f8829a4aSRandall Stewart 	switch (t_type) {
1857d61a0ae0SRandall Stewart 	case SCTP_TIMER_TYPE_ZERO_COPY:
1858d61a0ae0SRandall Stewart 		tmr = &inp->sctp_ep.zero_copy_timer;
1859d61a0ae0SRandall Stewart 		to_ticks = SCTP_ZERO_COPY_TICK_DELAY;
1860d61a0ae0SRandall Stewart 		break;
1861ad21a364SRandall Stewart 	case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
1862ad21a364SRandall Stewart 		tmr = &inp->sctp_ep.zero_copy_sendq_timer;
1863ad21a364SRandall Stewart 		to_ticks = SCTP_ZERO_COPY_SENDQ_TICK_DELAY;
1864ad21a364SRandall Stewart 		break;
1865f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ADDR_WQ:
1866f8829a4aSRandall Stewart 		/* Only 1 tick away :-) */
1867b3f1ea41SRandall Stewart 		tmr = &SCTP_BASE_INFO(addr_wq_timer);
186842551e99SRandall Stewart 		to_ticks = SCTP_ADDRESS_TICK_DELAY;
1869f8829a4aSRandall Stewart 		break;
1870f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SEND:
1871f8829a4aSRandall Stewart 		/* Here we use the RTO timer */
1872f8829a4aSRandall Stewart 		{
1873f8829a4aSRandall Stewart 			int rto_val;
1874f8829a4aSRandall Stewart 
1875f8829a4aSRandall Stewart 			if ((stcb == NULL) || (net == NULL)) {
1876ad81507eSRandall Stewart 				return;
1877f8829a4aSRandall Stewart 			}
1878f8829a4aSRandall Stewart 			tmr = &net->rxt_timer;
1879f8829a4aSRandall Stewart 			if (net->RTO == 0) {
1880f8829a4aSRandall Stewart 				rto_val = stcb->asoc.initial_rto;
1881f8829a4aSRandall Stewart 			} else {
1882f8829a4aSRandall Stewart 				rto_val = net->RTO;
1883f8829a4aSRandall Stewart 			}
1884f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(rto_val);
1885f8829a4aSRandall Stewart 		}
1886f8829a4aSRandall Stewart 		break;
1887f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
1888f8829a4aSRandall Stewart 		/*
1889f8829a4aSRandall Stewart 		 * Here we use the INIT timer default usually about 1
1890f8829a4aSRandall Stewart 		 * minute.
1891f8829a4aSRandall Stewart 		 */
1892f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
1893ad81507eSRandall Stewart 			return;
1894f8829a4aSRandall Stewart 		}
1895f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
1896f8829a4aSRandall Stewart 		if (net->RTO == 0) {
1897f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1898f8829a4aSRandall Stewart 		} else {
1899f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
1900f8829a4aSRandall Stewart 		}
1901f8829a4aSRandall Stewart 		break;
1902f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
1903f8829a4aSRandall Stewart 		/*
1904f8829a4aSRandall Stewart 		 * Here we use the Delayed-Ack timer value from the inp
1905f8829a4aSRandall Stewart 		 * ususually about 200ms.
1906f8829a4aSRandall Stewart 		 */
1907f8829a4aSRandall Stewart 		if (stcb == NULL) {
1908ad81507eSRandall Stewart 			return;
1909f8829a4aSRandall Stewart 		}
1910f8829a4aSRandall Stewart 		tmr = &stcb->asoc.dack_timer;
1911f8829a4aSRandall Stewart 		to_ticks = MSEC_TO_TICKS(stcb->asoc.delayed_ack);
1912f8829a4aSRandall Stewart 		break;
1913f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
1914f8829a4aSRandall Stewart 		/* Here we use the RTO of the destination. */
1915f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
1916ad81507eSRandall Stewart 			return;
1917f8829a4aSRandall Stewart 		}
1918f8829a4aSRandall Stewart 		if (net->RTO == 0) {
1919f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1920f8829a4aSRandall Stewart 		} else {
1921f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
1922f8829a4aSRandall Stewart 		}
1923f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
1924f8829a4aSRandall Stewart 		break;
1925f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
1926f8829a4aSRandall Stewart 		/*
1927f8829a4aSRandall Stewart 		 * the net is used here so that we can add in the RTO. Even
1928f8829a4aSRandall Stewart 		 * though we use a different timer. We also add the HB timer
1929f8829a4aSRandall Stewart 		 * PLUS a random jitter.
1930f8829a4aSRandall Stewart 		 */
1931ca85e948SMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
1932ad81507eSRandall Stewart 			return;
1933ad81507eSRandall Stewart 		} else {
1934f8829a4aSRandall Stewart 			uint32_t rndval;
1935ca85e948SMichael Tuexen 			uint32_t jitter;
1936f8829a4aSRandall Stewart 
1937ca85e948SMichael Tuexen 			if ((net->dest_state & SCTP_ADDR_NOHB) &&
1938ca85e948SMichael Tuexen 			    !(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
1939ad81507eSRandall Stewart 				return;
1940f8829a4aSRandall Stewart 			}
1941f8829a4aSRandall Stewart 			if (net->RTO == 0) {
1942ca85e948SMichael Tuexen 				to_ticks = stcb->asoc.initial_rto;
1943f8829a4aSRandall Stewart 			} else {
1944ca85e948SMichael Tuexen 				to_ticks = net->RTO;
1945f8829a4aSRandall Stewart 			}
1946ca85e948SMichael Tuexen 			rndval = sctp_select_initial_TSN(&inp->sctp_ep);
1947ca85e948SMichael Tuexen 			jitter = rndval % to_ticks;
1948ca85e948SMichael Tuexen 			if (jitter >= (to_ticks >> 1)) {
1949ca85e948SMichael Tuexen 				to_ticks = to_ticks + (jitter - (to_ticks >> 1));
1950f8829a4aSRandall Stewart 			} else {
1951ca85e948SMichael Tuexen 				to_ticks = to_ticks - jitter;
1952f8829a4aSRandall Stewart 			}
1953ca85e948SMichael Tuexen 			if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
1954ca85e948SMichael Tuexen 			    !(net->dest_state & SCTP_ADDR_PF)) {
1955ca85e948SMichael Tuexen 				to_ticks += net->heart_beat_delay;
1956f8829a4aSRandall Stewart 			}
1957f8829a4aSRandall Stewart 			/*
1958f8829a4aSRandall Stewart 			 * Now we must convert the to_ticks that are now in
1959f8829a4aSRandall Stewart 			 * ms to ticks.
1960f8829a4aSRandall Stewart 			 */
1961f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(to_ticks);
1962ca85e948SMichael Tuexen 			tmr = &net->hb_timer;
1963f8829a4aSRandall Stewart 		}
1964f8829a4aSRandall Stewart 		break;
1965f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
1966f8829a4aSRandall Stewart 		/*
1967f8829a4aSRandall Stewart 		 * Here we can use the RTO timer from the network since one
1968f8829a4aSRandall Stewart 		 * RTT was compelete. If a retran happened then we will be
1969f8829a4aSRandall Stewart 		 * using the RTO initial value.
1970f8829a4aSRandall Stewart 		 */
1971f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
1972ad81507eSRandall Stewart 			return;
1973f8829a4aSRandall Stewart 		}
1974f8829a4aSRandall Stewart 		if (net->RTO == 0) {
1975f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1976f8829a4aSRandall Stewart 		} else {
1977f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
1978f8829a4aSRandall Stewart 		}
1979f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
1980f8829a4aSRandall Stewart 		break;
1981f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_NEWCOOKIE:
1982f8829a4aSRandall Stewart 		/*
1983f8829a4aSRandall Stewart 		 * nothing needed but the endpoint here ususually about 60
1984f8829a4aSRandall Stewart 		 * minutes.
1985f8829a4aSRandall Stewart 		 */
1986ad81507eSRandall Stewart 		if (inp == NULL) {
1987ad81507eSRandall Stewart 			return;
1988ad81507eSRandall Stewart 		}
1989f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
1990f8829a4aSRandall Stewart 		to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_SIGNATURE];
1991f8829a4aSRandall Stewart 		break;
1992f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASOCKILL:
1993f8829a4aSRandall Stewart 		if (stcb == NULL) {
1994ad81507eSRandall Stewart 			return;
1995f8829a4aSRandall Stewart 		}
1996f8829a4aSRandall Stewart 		tmr = &stcb->asoc.strreset_timer;
1997f8829a4aSRandall Stewart 		to_ticks = MSEC_TO_TICKS(SCTP_ASOC_KILL_TIMEOUT);
1998f8829a4aSRandall Stewart 		break;
1999f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INPKILL:
2000f8829a4aSRandall Stewart 		/*
2001f8829a4aSRandall Stewart 		 * The inp is setup to die. We re-use the signature_chage
2002f8829a4aSRandall Stewart 		 * timer since that has stopped and we are in the GONE
2003f8829a4aSRandall Stewart 		 * state.
2004f8829a4aSRandall Stewart 		 */
2005ad81507eSRandall Stewart 		if (inp == NULL) {
2006ad81507eSRandall Stewart 			return;
2007ad81507eSRandall Stewart 		}
2008f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
2009f8829a4aSRandall Stewart 		to_ticks = MSEC_TO_TICKS(SCTP_INP_KILL_TIMEOUT);
2010f8829a4aSRandall Stewart 		break;
2011f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
2012f8829a4aSRandall Stewart 		/*
2013f8829a4aSRandall Stewart 		 * Here we use the value found in the EP for PMTU ususually
2014f8829a4aSRandall Stewart 		 * about 10 minutes.
2015f8829a4aSRandall Stewart 		 */
2016ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
2017ad81507eSRandall Stewart 			return;
2018f8829a4aSRandall Stewart 		}
2019f8829a4aSRandall Stewart 		if (net == NULL) {
2020ad81507eSRandall Stewart 			return;
2021f8829a4aSRandall Stewart 		}
202280c79bbeSMichael Tuexen 		if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
202380c79bbeSMichael Tuexen 			return;
202480c79bbeSMichael Tuexen 		}
2025f8829a4aSRandall Stewart 		to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_PMTU];
2026f8829a4aSRandall Stewart 		tmr = &net->pmtu_timer;
2027f8829a4aSRandall Stewart 		break;
2028f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
2029f8829a4aSRandall Stewart 		/* Here we use the RTO of the destination */
2030f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
2031ad81507eSRandall Stewart 			return;
2032f8829a4aSRandall Stewart 		}
2033f8829a4aSRandall Stewart 		if (net->RTO == 0) {
2034f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2035f8829a4aSRandall Stewart 		} else {
2036f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
2037f8829a4aSRandall Stewart 		}
2038f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2039f8829a4aSRandall Stewart 		break;
2040f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2041f8829a4aSRandall Stewart 		/*
2042f8829a4aSRandall Stewart 		 * Here we use the endpoints shutdown guard timer usually
2043f8829a4aSRandall Stewart 		 * about 3 minutes.
2044f8829a4aSRandall Stewart 		 */
2045ad81507eSRandall Stewart 		if ((inp == NULL) || (stcb == NULL)) {
2046ad81507eSRandall Stewart 			return;
2047f8829a4aSRandall Stewart 		}
2048f8829a4aSRandall Stewart 		to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN];
2049f8829a4aSRandall Stewart 		tmr = &stcb->asoc.shut_guard_timer;
2050f8829a4aSRandall Stewart 		break;
2051f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_STRRESET:
2052f8829a4aSRandall Stewart 		/*
20531b649582SRandall Stewart 		 * Here the timer comes from the stcb but its value is from
20541b649582SRandall Stewart 		 * the net's RTO.
2055f8829a4aSRandall Stewart 		 */
2056f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
2057ad81507eSRandall Stewart 			return;
2058f8829a4aSRandall Stewart 		}
2059f8829a4aSRandall Stewart 		if (net->RTO == 0) {
2060f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2061f8829a4aSRandall Stewart 		} else {
2062f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
2063f8829a4aSRandall Stewart 		}
2064f8829a4aSRandall Stewart 		tmr = &stcb->asoc.strreset_timer;
2065f8829a4aSRandall Stewart 		break;
2066f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASCONF:
2067f8829a4aSRandall Stewart 		/*
20681b649582SRandall Stewart 		 * Here the timer comes from the stcb but its value is from
20691b649582SRandall Stewart 		 * the net's RTO.
2070f8829a4aSRandall Stewart 		 */
2071f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
2072ad81507eSRandall Stewart 			return;
2073f8829a4aSRandall Stewart 		}
2074f8829a4aSRandall Stewart 		if (net->RTO == 0) {
2075f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2076f8829a4aSRandall Stewart 		} else {
2077f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
2078f8829a4aSRandall Stewart 		}
2079f8829a4aSRandall Stewart 		tmr = &stcb->asoc.asconf_timer;
2080f8829a4aSRandall Stewart 		break;
2081851b7298SRandall Stewart 	case SCTP_TIMER_TYPE_PRIM_DELETED:
2082851b7298SRandall Stewart 		if ((stcb == NULL) || (net != NULL)) {
2083851b7298SRandall Stewart 			return;
2084851b7298SRandall Stewart 		}
2085851b7298SRandall Stewart 		to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2086851b7298SRandall Stewart 		tmr = &stcb->asoc.delete_prim_timer;
2087851b7298SRandall Stewart 		break;
2088f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_AUTOCLOSE:
2089f8829a4aSRandall Stewart 		if (stcb == NULL) {
2090ad81507eSRandall Stewart 			return;
2091f8829a4aSRandall Stewart 		}
2092f8829a4aSRandall Stewart 		if (stcb->asoc.sctp_autoclose_ticks == 0) {
2093f8829a4aSRandall Stewart 			/*
2094f8829a4aSRandall Stewart 			 * Really an error since stcb is NOT set to
2095f8829a4aSRandall Stewart 			 * autoclose
2096f8829a4aSRandall Stewart 			 */
2097ad81507eSRandall Stewart 			return;
2098f8829a4aSRandall Stewart 		}
2099f8829a4aSRandall Stewart 		to_ticks = stcb->asoc.sctp_autoclose_ticks;
2100f8829a4aSRandall Stewart 		tmr = &stcb->asoc.autoclose_timer;
2101f8829a4aSRandall Stewart 		break;
2102f8829a4aSRandall Stewart 	default:
2103ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2104ad81507eSRandall Stewart 		    __FUNCTION__, t_type);
2105ad81507eSRandall Stewart 		return;
2106f8829a4aSRandall Stewart 		break;
210760990c0cSMichael Tuexen 	}
2108f8829a4aSRandall Stewart 	if ((to_ticks <= 0) || (tmr == NULL)) {
2109ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER1, "%s: %d:software error to_ticks:%d tmr:%p not set ??\n",
2110dd294dceSMichael Tuexen 		    __FUNCTION__, t_type, to_ticks, (void *)tmr);
2111ad81507eSRandall Stewart 		return;
2112f8829a4aSRandall Stewart 	}
2113139bc87fSRandall Stewart 	if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
2114f8829a4aSRandall Stewart 		/*
2115f8829a4aSRandall Stewart 		 * we do NOT allow you to have it already running. if it is
2116f8829a4aSRandall Stewart 		 * we leave the current one up unchanged
2117f8829a4aSRandall Stewart 		 */
2118ad81507eSRandall Stewart 		return;
2119f8829a4aSRandall Stewart 	}
2120f8829a4aSRandall Stewart 	/* At this point we can proceed */
2121f8829a4aSRandall Stewart 	if (t_type == SCTP_TIMER_TYPE_SEND) {
2122f8829a4aSRandall Stewart 		stcb->asoc.num_send_timers_up++;
2123f8829a4aSRandall Stewart 	}
2124a5d547adSRandall Stewart 	tmr->stopped_from = 0;
2125f8829a4aSRandall Stewart 	tmr->type = t_type;
2126f8829a4aSRandall Stewart 	tmr->ep = (void *)inp;
2127f8829a4aSRandall Stewart 	tmr->tcb = (void *)stcb;
2128f8829a4aSRandall Stewart 	tmr->net = (void *)net;
2129f8829a4aSRandall Stewart 	tmr->self = (void *)tmr;
21308518270eSMichael Tuexen 	tmr->vnet = (void *)curvnet;
2131c4739e2fSRandall Stewart 	tmr->ticks = sctp_get_tick_count();
2132ad81507eSRandall Stewart 	(void)SCTP_OS_TIMER_START(&tmr->timer, to_ticks, sctp_timeout_handler, tmr);
2133ad81507eSRandall Stewart 	return;
2134f8829a4aSRandall Stewart }
2135f8829a4aSRandall Stewart 
21366e55db54SRandall Stewart void
2137f8829a4aSRandall Stewart sctp_timer_stop(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2138a5d547adSRandall Stewart     struct sctp_nets *net, uint32_t from)
2139f8829a4aSRandall Stewart {
2140f8829a4aSRandall Stewart 	struct sctp_timer *tmr;
2141f8829a4aSRandall Stewart 
2142f8829a4aSRandall Stewart 	if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) &&
2143f8829a4aSRandall Stewart 	    (inp == NULL))
21446e55db54SRandall Stewart 		return;
2145f8829a4aSRandall Stewart 
2146f8829a4aSRandall Stewart 	tmr = NULL;
2147f8829a4aSRandall Stewart 	if (stcb) {
2148f8829a4aSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
2149f8829a4aSRandall Stewart 	}
2150f8829a4aSRandall Stewart 	switch (t_type) {
2151d61a0ae0SRandall Stewart 	case SCTP_TIMER_TYPE_ZERO_COPY:
2152d61a0ae0SRandall Stewart 		tmr = &inp->sctp_ep.zero_copy_timer;
2153d61a0ae0SRandall Stewart 		break;
2154ad21a364SRandall Stewart 	case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
2155ad21a364SRandall Stewart 		tmr = &inp->sctp_ep.zero_copy_sendq_timer;
2156ad21a364SRandall Stewart 		break;
2157f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ADDR_WQ:
2158b3f1ea41SRandall Stewart 		tmr = &SCTP_BASE_INFO(addr_wq_timer);
2159f8829a4aSRandall Stewart 		break;
2160f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SEND:
2161f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
21626e55db54SRandall Stewart 			return;
2163f8829a4aSRandall Stewart 		}
2164f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2165f8829a4aSRandall Stewart 		break;
2166f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
2167f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
21686e55db54SRandall Stewart 			return;
2169f8829a4aSRandall Stewart 		}
2170f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2171f8829a4aSRandall Stewart 		break;
2172f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
2173f8829a4aSRandall Stewart 		if (stcb == NULL) {
21746e55db54SRandall Stewart 			return;
2175f8829a4aSRandall Stewart 		}
2176f8829a4aSRandall Stewart 		tmr = &stcb->asoc.dack_timer;
2177f8829a4aSRandall Stewart 		break;
2178f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
2179f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
21806e55db54SRandall Stewart 			return;
2181f8829a4aSRandall Stewart 		}
2182f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2183f8829a4aSRandall Stewart 		break;
2184f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
2185ca85e948SMichael Tuexen 		if ((stcb == NULL) || (net == NULL)) {
21866e55db54SRandall Stewart 			return;
2187f8829a4aSRandall Stewart 		}
2188ca85e948SMichael Tuexen 		tmr = &net->hb_timer;
2189f8829a4aSRandall Stewart 		break;
2190f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
2191f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
21926e55db54SRandall Stewart 			return;
2193f8829a4aSRandall Stewart 		}
2194f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2195f8829a4aSRandall Stewart 		break;
2196f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_NEWCOOKIE:
2197f8829a4aSRandall Stewart 		/* nothing needed but the endpoint here */
2198f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
2199f8829a4aSRandall Stewart 		/*
2200f8829a4aSRandall Stewart 		 * We re-use the newcookie timer for the INP kill timer. We
2201f8829a4aSRandall Stewart 		 * must assure that we do not kill it by accident.
2202f8829a4aSRandall Stewart 		 */
2203f8829a4aSRandall Stewart 		break;
2204f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASOCKILL:
2205f8829a4aSRandall Stewart 		/*
2206f8829a4aSRandall Stewart 		 * Stop the asoc kill timer.
2207f8829a4aSRandall Stewart 		 */
2208f8829a4aSRandall Stewart 		if (stcb == NULL) {
22096e55db54SRandall Stewart 			return;
2210f8829a4aSRandall Stewart 		}
2211f8829a4aSRandall Stewart 		tmr = &stcb->asoc.strreset_timer;
2212f8829a4aSRandall Stewart 		break;
2213f8829a4aSRandall Stewart 
2214f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INPKILL:
2215f8829a4aSRandall Stewart 		/*
2216f8829a4aSRandall Stewart 		 * The inp is setup to die. We re-use the signature_chage
2217f8829a4aSRandall Stewart 		 * timer since that has stopped and we are in the GONE
2218f8829a4aSRandall Stewart 		 * state.
2219f8829a4aSRandall Stewart 		 */
2220f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
2221f8829a4aSRandall Stewart 		break;
2222f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
2223f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
22246e55db54SRandall Stewart 			return;
2225f8829a4aSRandall Stewart 		}
2226f8829a4aSRandall Stewart 		tmr = &net->pmtu_timer;
2227f8829a4aSRandall Stewart 		break;
2228f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
2229f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
22306e55db54SRandall Stewart 			return;
2231f8829a4aSRandall Stewart 		}
2232f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2233f8829a4aSRandall Stewart 		break;
2234f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2235f8829a4aSRandall Stewart 		if (stcb == NULL) {
22366e55db54SRandall Stewart 			return;
2237f8829a4aSRandall Stewart 		}
2238f8829a4aSRandall Stewart 		tmr = &stcb->asoc.shut_guard_timer;
2239f8829a4aSRandall Stewart 		break;
2240f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_STRRESET:
2241f8829a4aSRandall Stewart 		if (stcb == NULL) {
22426e55db54SRandall Stewart 			return;
2243f8829a4aSRandall Stewart 		}
2244f8829a4aSRandall Stewart 		tmr = &stcb->asoc.strreset_timer;
2245f8829a4aSRandall Stewart 		break;
2246f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASCONF:
2247f8829a4aSRandall Stewart 		if (stcb == NULL) {
22486e55db54SRandall Stewart 			return;
2249f8829a4aSRandall Stewart 		}
2250f8829a4aSRandall Stewart 		tmr = &stcb->asoc.asconf_timer;
2251f8829a4aSRandall Stewart 		break;
2252851b7298SRandall Stewart 	case SCTP_TIMER_TYPE_PRIM_DELETED:
2253851b7298SRandall Stewart 		if (stcb == NULL) {
2254851b7298SRandall Stewart 			return;
2255851b7298SRandall Stewart 		}
2256851b7298SRandall Stewart 		tmr = &stcb->asoc.delete_prim_timer;
2257851b7298SRandall Stewart 		break;
2258f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_AUTOCLOSE:
2259f8829a4aSRandall Stewart 		if (stcb == NULL) {
22606e55db54SRandall Stewart 			return;
2261f8829a4aSRandall Stewart 		}
2262f8829a4aSRandall Stewart 		tmr = &stcb->asoc.autoclose_timer;
2263f8829a4aSRandall Stewart 		break;
2264f8829a4aSRandall Stewart 	default:
2265ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2266ad81507eSRandall Stewart 		    __FUNCTION__, t_type);
2267f8829a4aSRandall Stewart 		break;
226860990c0cSMichael Tuexen 	}
2269f8829a4aSRandall Stewart 	if (tmr == NULL) {
22706e55db54SRandall Stewart 		return;
2271f8829a4aSRandall Stewart 	}
2272f8829a4aSRandall Stewart 	if ((tmr->type != t_type) && tmr->type) {
2273f8829a4aSRandall Stewart 		/*
2274f8829a4aSRandall Stewart 		 * Ok we have a timer that is under joint use. Cookie timer
2275f8829a4aSRandall Stewart 		 * per chance with the SEND timer. We therefore are NOT
2276f8829a4aSRandall Stewart 		 * running the timer that the caller wants stopped.  So just
2277f8829a4aSRandall Stewart 		 * return.
2278f8829a4aSRandall Stewart 		 */
22796e55db54SRandall Stewart 		return;
2280f8829a4aSRandall Stewart 	}
2281ad81507eSRandall Stewart 	if ((t_type == SCTP_TIMER_TYPE_SEND) && (stcb != NULL)) {
2282f8829a4aSRandall Stewart 		stcb->asoc.num_send_timers_up--;
2283f8829a4aSRandall Stewart 		if (stcb->asoc.num_send_timers_up < 0) {
2284f8829a4aSRandall Stewart 			stcb->asoc.num_send_timers_up = 0;
2285f8829a4aSRandall Stewart 		}
2286f8829a4aSRandall Stewart 	}
2287f8829a4aSRandall Stewart 	tmr->self = NULL;
2288a5d547adSRandall Stewart 	tmr->stopped_from = from;
22896e55db54SRandall Stewart 	(void)SCTP_OS_TIMER_STOP(&tmr->timer);
22906e55db54SRandall Stewart 	return;
2291f8829a4aSRandall Stewart }
2292f8829a4aSRandall Stewart 
2293f8829a4aSRandall Stewart uint32_t
2294f8829a4aSRandall Stewart sctp_calculate_len(struct mbuf *m)
2295f8829a4aSRandall Stewart {
2296f8829a4aSRandall Stewart 	uint32_t tlen = 0;
2297f8829a4aSRandall Stewart 	struct mbuf *at;
2298f8829a4aSRandall Stewart 
2299f8829a4aSRandall Stewart 	at = m;
2300f8829a4aSRandall Stewart 	while (at) {
2301139bc87fSRandall Stewart 		tlen += SCTP_BUF_LEN(at);
2302139bc87fSRandall Stewart 		at = SCTP_BUF_NEXT(at);
2303f8829a4aSRandall Stewart 	}
2304f8829a4aSRandall Stewart 	return (tlen);
2305f8829a4aSRandall Stewart }
2306f8829a4aSRandall Stewart 
2307f8829a4aSRandall Stewart void
2308f8829a4aSRandall Stewart sctp_mtu_size_reset(struct sctp_inpcb *inp,
230944b7479bSRandall Stewart     struct sctp_association *asoc, uint32_t mtu)
2310f8829a4aSRandall Stewart {
2311f8829a4aSRandall Stewart 	/*
2312f8829a4aSRandall Stewart 	 * Reset the P-MTU size on this association, this involves changing
2313f8829a4aSRandall Stewart 	 * the asoc MTU, going through ANY chunk+overhead larger than mtu to
2314f8829a4aSRandall Stewart 	 * allow the DF flag to be cleared.
2315f8829a4aSRandall Stewart 	 */
2316f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
2317f8829a4aSRandall Stewart 	unsigned int eff_mtu, ovh;
2318f8829a4aSRandall Stewart 
2319f8829a4aSRandall Stewart 	asoc->smallest_mtu = mtu;
2320f8829a4aSRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2321f8829a4aSRandall Stewart 		ovh = SCTP_MIN_OVERHEAD;
2322f8829a4aSRandall Stewart 	} else {
2323f8829a4aSRandall Stewart 		ovh = SCTP_MIN_V4_OVERHEAD;
2324f8829a4aSRandall Stewart 	}
2325f8829a4aSRandall Stewart 	eff_mtu = mtu - ovh;
2326f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
2327f8829a4aSRandall Stewart 		if (chk->send_size > eff_mtu) {
2328f8829a4aSRandall Stewart 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2329f8829a4aSRandall Stewart 		}
2330f8829a4aSRandall Stewart 	}
2331f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
2332f8829a4aSRandall Stewart 		if (chk->send_size > eff_mtu) {
2333f8829a4aSRandall Stewart 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2334f8829a4aSRandall Stewart 		}
2335f8829a4aSRandall Stewart 	}
2336f8829a4aSRandall Stewart }
2337f8829a4aSRandall Stewart 
2338f8829a4aSRandall Stewart 
2339f8829a4aSRandall Stewart /*
2340f8829a4aSRandall Stewart  * given an association and starting time of the current RTT period return
2341f42a358aSRandall Stewart  * RTO in number of msecs net should point to the current network
2342f8829a4aSRandall Stewart  */
2343899288aeSRandall Stewart 
2344f8829a4aSRandall Stewart uint32_t
2345f8829a4aSRandall Stewart sctp_calculate_rto(struct sctp_tcb *stcb,
2346f8829a4aSRandall Stewart     struct sctp_association *asoc,
2347f8829a4aSRandall Stewart     struct sctp_nets *net,
234818e198d3SRandall Stewart     struct timeval *told,
2349f79aab18SRandall Stewart     int safe, int rtt_from_sack)
2350f8829a4aSRandall Stewart {
235118e198d3SRandall Stewart 	/*-
2352f8829a4aSRandall Stewart 	 * given an association and the starting time of the current RTT
2353f42a358aSRandall Stewart 	 * period (in value1/value2) return RTO in number of msecs.
2354f8829a4aSRandall Stewart 	 */
2355be1d9176SMichael Tuexen 	int32_t rtt;		/* RTT in ms */
2356be1d9176SMichael Tuexen 	uint32_t new_rto;
2357f8829a4aSRandall Stewart 	int first_measure = 0;
235818e198d3SRandall Stewart 	struct timeval now, then, *old;
2359f8829a4aSRandall Stewart 
236018e198d3SRandall Stewart 	/* Copy it out for sparc64 */
236118e198d3SRandall Stewart 	if (safe == sctp_align_unsafe_makecopy) {
236218e198d3SRandall Stewart 		old = &then;
236318e198d3SRandall Stewart 		memcpy(&then, told, sizeof(struct timeval));
236418e198d3SRandall Stewart 	} else if (safe == sctp_align_safe_nocopy) {
236518e198d3SRandall Stewart 		old = told;
236618e198d3SRandall Stewart 	} else {
236718e198d3SRandall Stewart 		/* error */
236818e198d3SRandall Stewart 		SCTP_PRINTF("Huh, bad rto calc call\n");
236918e198d3SRandall Stewart 		return (0);
237018e198d3SRandall Stewart 	}
2371f8829a4aSRandall Stewart 	/************************/
2372f8829a4aSRandall Stewart 	/* 1. calculate new RTT */
2373f8829a4aSRandall Stewart 	/************************/
2374f8829a4aSRandall Stewart 	/* get the current time */
2375299108c5SRandall Stewart 	if (stcb->asoc.use_precise_time) {
2376299108c5SRandall Stewart 		(void)SCTP_GETPTIME_TIMEVAL(&now);
2377299108c5SRandall Stewart 	} else {
23786e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
2379299108c5SRandall Stewart 	}
2380be1d9176SMichael Tuexen 	timevalsub(&now, old);
2381be1d9176SMichael Tuexen 	/* store the current RTT in us */
238281eb4e63SMichael Tuexen 	net->rtt = (uint64_t) 1000000 *(uint64_t) now.tv_sec +
2383be1d9176SMichael Tuexen 	        (uint64_t) now.tv_usec;
2384be1d9176SMichael Tuexen 
2385be1d9176SMichael Tuexen 	/* computer rtt in ms */
2386be1d9176SMichael Tuexen 	rtt = net->rtt / 1000;
2387f79aab18SRandall Stewart 	if ((asoc->cc_functions.sctp_rtt_calculated) && (rtt_from_sack == SCTP_RTT_FROM_DATA)) {
2388f79aab18SRandall Stewart 		/*
2389f79aab18SRandall Stewart 		 * Tell the CC module that a new update has just occurred
2390f79aab18SRandall Stewart 		 * from a sack
2391f79aab18SRandall Stewart 		 */
2392f79aab18SRandall Stewart 		(*asoc->cc_functions.sctp_rtt_calculated) (stcb, net, &now);
2393f79aab18SRandall Stewart 	}
2394f79aab18SRandall Stewart 	/*
2395f79aab18SRandall Stewart 	 * Do we need to determine the lan? We do this only on sacks i.e.
2396f79aab18SRandall Stewart 	 * RTT being determined from data not non-data (HB/INIT->INITACK).
2397f79aab18SRandall Stewart 	 */
2398f79aab18SRandall Stewart 	if ((rtt_from_sack == SCTP_RTT_FROM_DATA) &&
2399be1d9176SMichael Tuexen 	    (net->lan_type == SCTP_LAN_UNKNOWN)) {
2400be1d9176SMichael Tuexen 		if (net->rtt > SCTP_LOCAL_LAN_RTT) {
2401899288aeSRandall Stewart 			net->lan_type = SCTP_LAN_INTERNET;
2402899288aeSRandall Stewart 		} else {
2403899288aeSRandall Stewart 			net->lan_type = SCTP_LAN_LOCAL;
2404899288aeSRandall Stewart 		}
2405899288aeSRandall Stewart 	}
2406f8829a4aSRandall Stewart 	/***************************/
2407f8829a4aSRandall Stewart 	/* 2. update RTTVAR & SRTT */
2408f8829a4aSRandall Stewart 	/***************************/
2409be1d9176SMichael Tuexen 	/*-
2410be1d9176SMichael Tuexen 	 * Compute the scaled average lastsa and the
2411be1d9176SMichael Tuexen 	 * scaled variance lastsv as described in van Jacobson
2412be1d9176SMichael Tuexen 	 * Paper "Congestion Avoidance and Control", Annex A.
2413be1d9176SMichael Tuexen 	 *
2414be1d9176SMichael Tuexen 	 * (net->lastsa >> SCTP_RTT_SHIFT) is the srtt
2415be1d9176SMichael Tuexen 	 * (net->lastsa >> SCTP_RTT_VAR_SHIFT) is the rttvar
2416be1d9176SMichael Tuexen 	 */
24179a972525SRandall Stewart 	if (net->RTO_measured) {
2418be1d9176SMichael Tuexen 		rtt -= (net->lastsa >> SCTP_RTT_SHIFT);
2419be1d9176SMichael Tuexen 		net->lastsa += rtt;
2420be1d9176SMichael Tuexen 		if (rtt < 0) {
2421be1d9176SMichael Tuexen 			rtt = -rtt;
2422be1d9176SMichael Tuexen 		}
2423be1d9176SMichael Tuexen 		rtt -= (net->lastsv >> SCTP_RTT_VAR_SHIFT);
2424be1d9176SMichael Tuexen 		net->lastsv += rtt;
2425b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2426f8829a4aSRandall Stewart 			rto_logging(net, SCTP_LOG_RTTVAR);
242780fefe0aSRandall Stewart 		}
2428f8829a4aSRandall Stewart 	} else {
2429f8829a4aSRandall Stewart 		/* First RTO measurment */
24309a972525SRandall Stewart 		net->RTO_measured = 1;
2431f8829a4aSRandall Stewart 		first_measure = 1;
2432be1d9176SMichael Tuexen 		net->lastsa = rtt << SCTP_RTT_SHIFT;
2433be1d9176SMichael Tuexen 		net->lastsv = (rtt / 2) << SCTP_RTT_VAR_SHIFT;
2434b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2435f8829a4aSRandall Stewart 			rto_logging(net, SCTP_LOG_INITIAL_RTT);
243680fefe0aSRandall Stewart 		}
2437f8829a4aSRandall Stewart 	}
2438be1d9176SMichael Tuexen 	if (net->lastsv == 0) {
2439be1d9176SMichael Tuexen 		net->lastsv = SCTP_CLOCK_GRANULARITY;
2440be1d9176SMichael Tuexen 	}
2441108df27cSRandall Stewart 	new_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
2442f8829a4aSRandall Stewart 	if ((new_rto > SCTP_SAT_NETWORK_MIN) &&
2443f8829a4aSRandall Stewart 	    (stcb->asoc.sat_network_lockout == 0)) {
2444f8829a4aSRandall Stewart 		stcb->asoc.sat_network = 1;
2445f8829a4aSRandall Stewart 	} else if ((!first_measure) && stcb->asoc.sat_network) {
2446f8829a4aSRandall Stewart 		stcb->asoc.sat_network = 0;
2447f8829a4aSRandall Stewart 		stcb->asoc.sat_network_lockout = 1;
2448f8829a4aSRandall Stewart 	}
2449f8829a4aSRandall Stewart 	/* bound it, per C6/C7 in Section 5.3.1 */
2450f8829a4aSRandall Stewart 	if (new_rto < stcb->asoc.minrto) {
2451f8829a4aSRandall Stewart 		new_rto = stcb->asoc.minrto;
2452f8829a4aSRandall Stewart 	}
2453f8829a4aSRandall Stewart 	if (new_rto > stcb->asoc.maxrto) {
2454f8829a4aSRandall Stewart 		new_rto = stcb->asoc.maxrto;
2455f8829a4aSRandall Stewart 	}
24565e54f665SRandall Stewart 	/* we are now returning the RTO */
24575e54f665SRandall Stewart 	return (new_rto);
2458f8829a4aSRandall Stewart }
2459f8829a4aSRandall Stewart 
2460f8829a4aSRandall Stewart /*
2461f8829a4aSRandall Stewart  * return a pointer to a contiguous piece of data from the given mbuf chain
2462f8829a4aSRandall Stewart  * starting at 'off' for 'len' bytes.  If the desired piece spans more than
2463f8829a4aSRandall Stewart  * one mbuf, a copy is made at 'ptr'. caller must ensure that the buffer size
2464f8829a4aSRandall Stewart  * is >= 'len' returns NULL if there there isn't 'len' bytes in the chain.
2465f8829a4aSRandall Stewart  */
246672fb6fdbSRandall Stewart caddr_t
2467f8829a4aSRandall Stewart sctp_m_getptr(struct mbuf *m, int off, int len, uint8_t * in_ptr)
2468f8829a4aSRandall Stewart {
2469f8829a4aSRandall Stewart 	uint32_t count;
2470f8829a4aSRandall Stewart 	uint8_t *ptr;
2471f8829a4aSRandall Stewart 
2472f8829a4aSRandall Stewart 	ptr = in_ptr;
2473f8829a4aSRandall Stewart 	if ((off < 0) || (len <= 0))
2474f8829a4aSRandall Stewart 		return (NULL);
2475f8829a4aSRandall Stewart 
2476f8829a4aSRandall Stewart 	/* find the desired start location */
2477f8829a4aSRandall Stewart 	while ((m != NULL) && (off > 0)) {
2478139bc87fSRandall Stewart 		if (off < SCTP_BUF_LEN(m))
2479f8829a4aSRandall Stewart 			break;
2480139bc87fSRandall Stewart 		off -= SCTP_BUF_LEN(m);
2481139bc87fSRandall Stewart 		m = SCTP_BUF_NEXT(m);
2482f8829a4aSRandall Stewart 	}
2483f8829a4aSRandall Stewart 	if (m == NULL)
2484f8829a4aSRandall Stewart 		return (NULL);
2485f8829a4aSRandall Stewart 
2486f8829a4aSRandall Stewart 	/* is the current mbuf large enough (eg. contiguous)? */
2487139bc87fSRandall Stewart 	if ((SCTP_BUF_LEN(m) - off) >= len) {
2488f8829a4aSRandall Stewart 		return (mtod(m, caddr_t)+off);
2489f8829a4aSRandall Stewart 	} else {
2490f8829a4aSRandall Stewart 		/* else, it spans more than one mbuf, so save a temp copy... */
2491f8829a4aSRandall Stewart 		while ((m != NULL) && (len > 0)) {
2492139bc87fSRandall Stewart 			count = min(SCTP_BUF_LEN(m) - off, len);
2493f8829a4aSRandall Stewart 			bcopy(mtod(m, caddr_t)+off, ptr, count);
2494f8829a4aSRandall Stewart 			len -= count;
2495f8829a4aSRandall Stewart 			ptr += count;
2496f8829a4aSRandall Stewart 			off = 0;
2497139bc87fSRandall Stewart 			m = SCTP_BUF_NEXT(m);
2498f8829a4aSRandall Stewart 		}
2499f8829a4aSRandall Stewart 		if ((m == NULL) && (len > 0))
2500f8829a4aSRandall Stewart 			return (NULL);
2501f8829a4aSRandall Stewart 		else
2502f8829a4aSRandall Stewart 			return ((caddr_t)in_ptr);
2503f8829a4aSRandall Stewart 	}
2504f8829a4aSRandall Stewart }
2505f8829a4aSRandall Stewart 
2506f8829a4aSRandall Stewart 
250744b7479bSRandall Stewart 
2508f8829a4aSRandall Stewart struct sctp_paramhdr *
2509f8829a4aSRandall Stewart sctp_get_next_param(struct mbuf *m,
2510f8829a4aSRandall Stewart     int offset,
2511f8829a4aSRandall Stewart     struct sctp_paramhdr *pull,
2512f8829a4aSRandall Stewart     int pull_limit)
2513f8829a4aSRandall Stewart {
2514f8829a4aSRandall Stewart 	/* This just provides a typed signature to Peter's Pull routine */
2515f8829a4aSRandall Stewart 	return ((struct sctp_paramhdr *)sctp_m_getptr(m, offset, pull_limit,
2516f8829a4aSRandall Stewart 	    (uint8_t *) pull));
2517f8829a4aSRandall Stewart }
2518f8829a4aSRandall Stewart 
2519f8829a4aSRandall Stewart 
2520ce11b842SMichael Tuexen struct mbuf *
2521f8829a4aSRandall Stewart sctp_add_pad_tombuf(struct mbuf *m, int padlen)
2522f8829a4aSRandall Stewart {
2523ce11b842SMichael Tuexen 	struct mbuf *m_last;
2524ce11b842SMichael Tuexen 	caddr_t dp;
2525f8829a4aSRandall Stewart 
2526f8829a4aSRandall Stewart 	if (padlen > 3) {
2527ce11b842SMichael Tuexen 		return (NULL);
2528f8829a4aSRandall Stewart 	}
252941eee555SRandall Stewart 	if (padlen <= M_TRAILINGSPACE(m)) {
2530f8829a4aSRandall Stewart 		/*
2531f8829a4aSRandall Stewart 		 * The easy way. We hope the majority of the time we hit
2532f8829a4aSRandall Stewart 		 * here :)
2533f8829a4aSRandall Stewart 		 */
2534ce11b842SMichael Tuexen 		m_last = m;
2535f8829a4aSRandall Stewart 	} else {
2536ce11b842SMichael Tuexen 		/* Hard way we must grow the mbuf chain */
2537ce11b842SMichael Tuexen 		m_last = sctp_get_mbuf_for_msg(padlen, 0, M_NOWAIT, 1, MT_DATA);
2538ce11b842SMichael Tuexen 		if (m_last == NULL) {
2539ce11b842SMichael Tuexen 			return (NULL);
2540f8829a4aSRandall Stewart 		}
2541ce11b842SMichael Tuexen 		SCTP_BUF_LEN(m_last) = 0;
2542ce11b842SMichael Tuexen 		SCTP_BUF_NEXT(m_last) = NULL;
2543ce11b842SMichael Tuexen 		SCTP_BUF_NEXT(m) = m_last;
2544f8829a4aSRandall Stewart 	}
2545ce11b842SMichael Tuexen 	dp = mtod(m_last, caddr_t)+SCTP_BUF_LEN(m_last);
2546ce11b842SMichael Tuexen 	SCTP_BUF_LEN(m_last) += padlen;
2547ce11b842SMichael Tuexen 	memset(dp, 0, padlen);
2548ce11b842SMichael Tuexen 	return (m_last);
2549f8829a4aSRandall Stewart }
2550f8829a4aSRandall Stewart 
2551ce11b842SMichael Tuexen struct mbuf *
2552f8829a4aSRandall Stewart sctp_pad_lastmbuf(struct mbuf *m, int padval, struct mbuf *last_mbuf)
2553f8829a4aSRandall Stewart {
2554f8829a4aSRandall Stewart 	/* find the last mbuf in chain and pad it */
2555f8829a4aSRandall Stewart 	struct mbuf *m_at;
2556f8829a4aSRandall Stewart 
2557ce11b842SMichael Tuexen 	if (last_mbuf != NULL) {
2558f8829a4aSRandall Stewart 		return (sctp_add_pad_tombuf(last_mbuf, padval));
2559f8829a4aSRandall Stewart 	} else {
256017267b32SMichael Tuexen 		for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
2561139bc87fSRandall Stewart 			if (SCTP_BUF_NEXT(m_at) == NULL) {
2562f8829a4aSRandall Stewart 				return (sctp_add_pad_tombuf(m_at, padval));
2563f8829a4aSRandall Stewart 			}
2564f8829a4aSRandall Stewart 		}
2565f8829a4aSRandall Stewart 	}
2566ce11b842SMichael Tuexen 	return (NULL);
2567f8829a4aSRandall Stewart }
2568f8829a4aSRandall Stewart 
2569f8829a4aSRandall Stewart static void
2570c5b5675dSMichael Tuexen sctp_notify_assoc_change(uint16_t state, struct sctp_tcb *stcb,
2571410a3b1eSMichael Tuexen     uint16_t error, struct sctp_abort_chunk *abort, uint8_t from_peer, int so_locked
2572ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2573ceaad40aSRandall Stewart     SCTP_UNUSED
2574ceaad40aSRandall Stewart #endif
2575ceaad40aSRandall Stewart )
2576f8829a4aSRandall Stewart {
2577f8829a4aSRandall Stewart 	struct mbuf *m_notify;
2578f8829a4aSRandall Stewart 	struct sctp_assoc_change *sac;
2579f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
2580a2b42326SMichael Tuexen 	size_t notif_len, abort_len;
2581e06b67c7SMichael Tuexen 	unsigned int i;
2582f8829a4aSRandall Stewart 
2583ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2584ceaad40aSRandall Stewart 	struct socket *so;
2585ceaad40aSRandall Stewart 
2586ceaad40aSRandall Stewart #endif
2587ceaad40aSRandall Stewart 
258858411b08SMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT)) {
2589a2b42326SMichael Tuexen 		notif_len = sizeof(struct sctp_assoc_change);
2590a2b42326SMichael Tuexen 		if (abort != NULL) {
2591c9eb4473SMichael Tuexen 			abort_len = ntohs(abort->ch.chunk_length);
2592a2b42326SMichael Tuexen 		} else {
2593a2b42326SMichael Tuexen 			abort_len = 0;
2594c5b5675dSMichael Tuexen 		}
2595a2b42326SMichael Tuexen 		if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
2596a2b42326SMichael Tuexen 			notif_len += SCTP_ASSOC_SUPPORTS_MAX;
2597a2b42326SMichael Tuexen 		} else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
2598a2b42326SMichael Tuexen 			notif_len += abort_len;
2599a2b42326SMichael Tuexen 		}
2600eb1b1807SGleb Smirnoff 		m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
2601a2b42326SMichael Tuexen 		if (m_notify == NULL) {
2602a2b42326SMichael Tuexen 			/* Retry with smaller value. */
2603a2b42326SMichael Tuexen 			notif_len = sizeof(struct sctp_assoc_change);
2604eb1b1807SGleb Smirnoff 			m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
2605a2b42326SMichael Tuexen 			if (m_notify == NULL) {
260658411b08SMichael Tuexen 				goto set_error;
2607a2b42326SMichael Tuexen 			}
2608a2b42326SMichael Tuexen 		}
2609a2b42326SMichael Tuexen 		SCTP_BUF_NEXT(m_notify) = NULL;
2610f8829a4aSRandall Stewart 		sac = mtod(m_notify, struct sctp_assoc_change *);
2611e432298aSXin LI 		memset(sac, 0, notif_len);
2612f8829a4aSRandall Stewart 		sac->sac_type = SCTP_ASSOC_CHANGE;
2613f8829a4aSRandall Stewart 		sac->sac_flags = 0;
2614f8829a4aSRandall Stewart 		sac->sac_length = sizeof(struct sctp_assoc_change);
2615c5b5675dSMichael Tuexen 		sac->sac_state = state;
2616f8829a4aSRandall Stewart 		sac->sac_error = error;
2617f8829a4aSRandall Stewart 		/* XXX verify these stream counts */
2618f8829a4aSRandall Stewart 		sac->sac_outbound_streams = stcb->asoc.streamoutcnt;
2619f8829a4aSRandall Stewart 		sac->sac_inbound_streams = stcb->asoc.streamincnt;
2620f8829a4aSRandall Stewart 		sac->sac_assoc_id = sctp_get_associd(stcb);
2621a2b42326SMichael Tuexen 		if (notif_len > sizeof(struct sctp_assoc_change)) {
2622c5b5675dSMichael Tuexen 			if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
2623e06b67c7SMichael Tuexen 				i = 0;
2624*dd973b0eSMichael Tuexen 				if (stcb->asoc.prsctp_supported) {
2625e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_PR;
2626e06b67c7SMichael Tuexen 				}
2627e06b67c7SMichael Tuexen 				if (stcb->asoc.peer_supports_auth) {
2628e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_AUTH;
2629e06b67c7SMichael Tuexen 				}
2630e06b67c7SMichael Tuexen 				if (stcb->asoc.peer_supports_asconf) {
2631e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_ASCONF;
2632e06b67c7SMichael Tuexen 				}
2633e06b67c7SMichael Tuexen 				sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_MULTIBUF;
2634e06b67c7SMichael Tuexen 				if (stcb->asoc.peer_supports_strreset) {
2635e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_RE_CONFIG;
2636e06b67c7SMichael Tuexen 				}
2637e06b67c7SMichael Tuexen 				sac->sac_length += i;
2638a2b42326SMichael Tuexen 			} else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
2639a2b42326SMichael Tuexen 				memcpy(sac->sac_info, abort, abort_len);
2640a2b42326SMichael Tuexen 				sac->sac_length += abort_len;
2641a2b42326SMichael Tuexen 			}
2642c5b5675dSMichael Tuexen 		}
2643e06b67c7SMichael Tuexen 		SCTP_BUF_LEN(m_notify) = sac->sac_length;
2644f8829a4aSRandall Stewart 		control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
26457215cc1bSMichael Tuexen 		    0, 0, stcb->asoc.context, 0, 0, 0,
2646f8829a4aSRandall Stewart 		    m_notify);
264758411b08SMichael Tuexen 		if (control != NULL) {
2648139bc87fSRandall Stewart 			control->length = SCTP_BUF_LEN(m_notify);
2649f8829a4aSRandall Stewart 			/* not that we need this */
2650f8829a4aSRandall Stewart 			control->tail_mbuf = m_notify;
2651139bc87fSRandall Stewart 			control->spec_flags = M_NOTIFICATION;
2652f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep, stcb,
2653f8829a4aSRandall Stewart 			    control,
2654cfde3ff7SRandall Stewart 			    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD,
2655cfde3ff7SRandall Stewart 			    so_locked);
265658411b08SMichael Tuexen 		} else {
265758411b08SMichael Tuexen 			sctp_m_freem(m_notify);
265858411b08SMichael Tuexen 		}
265958411b08SMichael Tuexen 	}
266058411b08SMichael Tuexen 	/*
266158411b08SMichael Tuexen 	 * For 1-to-1 style sockets, we send up and error when an ABORT
266258411b08SMichael Tuexen 	 * comes in.
266358411b08SMichael Tuexen 	 */
266458411b08SMichael Tuexen set_error:
266558411b08SMichael Tuexen 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
266658411b08SMichael Tuexen 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
266758411b08SMichael Tuexen 	    ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
2668e045904fSMichael Tuexen 		SOCK_LOCK(stcb->sctp_socket);
2669410a3b1eSMichael Tuexen 		if (from_peer) {
267058411b08SMichael Tuexen 			if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
267158411b08SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNREFUSED);
267258411b08SMichael Tuexen 				stcb->sctp_socket->so_error = ECONNREFUSED;
267358411b08SMichael Tuexen 			} else {
267458411b08SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
267558411b08SMichael Tuexen 				stcb->sctp_socket->so_error = ECONNRESET;
267658411b08SMichael Tuexen 			}
2677410a3b1eSMichael Tuexen 		} else {
2678553bb068SMichael Tuexen 			if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) ||
2679553bb068SMichael Tuexen 			    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
2680553bb068SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ETIMEDOUT);
2681553bb068SMichael Tuexen 				stcb->sctp_socket->so_error = ETIMEDOUT;
2682553bb068SMichael Tuexen 			} else {
2683410a3b1eSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNABORTED);
2684410a3b1eSMichael Tuexen 				stcb->sctp_socket->so_error = ECONNABORTED;
2685410a3b1eSMichael Tuexen 			}
268658411b08SMichael Tuexen 		}
2687553bb068SMichael Tuexen 	}
268858411b08SMichael Tuexen 	/* Wake ANY sleepers */
2689ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2690ceaad40aSRandall Stewart 	so = SCTP_INP_SO(stcb->sctp_ep);
2691ceaad40aSRandall Stewart 	if (!so_locked) {
2692ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
2693ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
2694ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
2695ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
2696ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
2697ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2698ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
2699ceaad40aSRandall Stewart 			return;
2700ceaad40aSRandall Stewart 		}
2701ceaad40aSRandall Stewart 	}
2702ceaad40aSRandall Stewart #endif
270358411b08SMichael Tuexen 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
270458411b08SMichael Tuexen 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
270558411b08SMichael Tuexen 	    ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
2706e045904fSMichael Tuexen 		socantrcvmore_locked(stcb->sctp_socket);
270758411b08SMichael Tuexen 	}
270858411b08SMichael Tuexen 	sorwakeup(stcb->sctp_socket);
270958411b08SMichael Tuexen 	sowwakeup(stcb->sctp_socket);
2710ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2711ceaad40aSRandall Stewart 	if (!so_locked) {
2712ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
2713ceaad40aSRandall Stewart 	}
2714ceaad40aSRandall Stewart #endif
2715f8829a4aSRandall Stewart }
2716f8829a4aSRandall Stewart 
2717f8829a4aSRandall Stewart static void
2718f8829a4aSRandall Stewart sctp_notify_peer_addr_change(struct sctp_tcb *stcb, uint32_t state,
2719f8829a4aSRandall Stewart     struct sockaddr *sa, uint32_t error)
2720f8829a4aSRandall Stewart {
2721f8829a4aSRandall Stewart 	struct mbuf *m_notify;
2722f8829a4aSRandall Stewart 	struct sctp_paddr_change *spc;
2723f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
2724f8829a4aSRandall Stewart 
272560990c0cSMichael Tuexen 	if ((stcb == NULL) ||
272660990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT)) {
2727f8829a4aSRandall Stewart 		/* event not enabled */
2728f8829a4aSRandall Stewart 		return;
2729830d754dSRandall Stewart 	}
2730eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_paddr_change), 0, M_NOWAIT, 1, MT_DATA);
2731f8829a4aSRandall Stewart 	if (m_notify == NULL)
2732f8829a4aSRandall Stewart 		return;
2733139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
2734f8829a4aSRandall Stewart 	spc = mtod(m_notify, struct sctp_paddr_change *);
273556711f94SMichael Tuexen 	memset(spc, 0, sizeof(struct sctp_paddr_change));
2736f8829a4aSRandall Stewart 	spc->spc_type = SCTP_PEER_ADDR_CHANGE;
2737f8829a4aSRandall Stewart 	spc->spc_flags = 0;
2738f8829a4aSRandall Stewart 	spc->spc_length = sizeof(struct sctp_paddr_change);
27395e2c2d87SRandall Stewart 	switch (sa->sa_family) {
2740ea5eba11SMichael Tuexen #ifdef INET
27415e2c2d87SRandall Stewart 	case AF_INET:
2742f8829a4aSRandall Stewart 		memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
27435e2c2d87SRandall Stewart 		break;
2744ea5eba11SMichael Tuexen #endif
27455e2c2d87SRandall Stewart #ifdef INET6
27465e2c2d87SRandall Stewart 	case AF_INET6:
27475e2c2d87SRandall Stewart 		{
2748f42a358aSRandall Stewart 			struct sockaddr_in6 *sin6;
2749f42a358aSRandall Stewart 
2750f8829a4aSRandall Stewart 			memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in6));
2751f42a358aSRandall Stewart 
2752f42a358aSRandall Stewart 			sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
2753f42a358aSRandall Stewart 			if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
275442551e99SRandall Stewart 				if (sin6->sin6_scope_id == 0) {
275542551e99SRandall Stewart 					/* recover scope_id for user */
2756f42a358aSRandall Stewart 					(void)sa6_recoverscope(sin6);
275742551e99SRandall Stewart 				} else {
275842551e99SRandall Stewart 					/* clear embedded scope_id for user */
275942551e99SRandall Stewart 					in6_clearscope(&sin6->sin6_addr);
276042551e99SRandall Stewart 				}
2761f42a358aSRandall Stewart 			}
27625e2c2d87SRandall Stewart 			break;
27635e2c2d87SRandall Stewart 		}
27645e2c2d87SRandall Stewart #endif
27655e2c2d87SRandall Stewart 	default:
27665e2c2d87SRandall Stewart 		/* TSNH */
27675e2c2d87SRandall Stewart 		break;
2768f8829a4aSRandall Stewart 	}
2769f8829a4aSRandall Stewart 	spc->spc_state = state;
2770f8829a4aSRandall Stewart 	spc->spc_error = error;
2771f8829a4aSRandall Stewart 	spc->spc_assoc_id = sctp_get_associd(stcb);
2772f8829a4aSRandall Stewart 
2773139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_paddr_change);
2774139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
2775f8829a4aSRandall Stewart 
2776f8829a4aSRandall Stewart 	/* append to socket */
2777f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
27787215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
2779f8829a4aSRandall Stewart 	    m_notify);
2780f8829a4aSRandall Stewart 	if (control == NULL) {
2781f8829a4aSRandall Stewart 		/* no memory */
2782f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
2783f8829a4aSRandall Stewart 		return;
2784f8829a4aSRandall Stewart 	}
2785139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
2786139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
2787f8829a4aSRandall Stewart 	/* not that we need this */
2788f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
2789f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
2790f8829a4aSRandall Stewart 	    control,
2791cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1,
2792cfde3ff7SRandall Stewart 	    SCTP_READ_LOCK_NOT_HELD,
2793cfde3ff7SRandall Stewart 	    SCTP_SO_NOT_LOCKED);
2794f8829a4aSRandall Stewart }
2795f8829a4aSRandall Stewart 
2796f8829a4aSRandall Stewart 
2797f8829a4aSRandall Stewart static void
27981edc9dbaSMichael Tuexen sctp_notify_send_failed(struct sctp_tcb *stcb, uint8_t sent, uint32_t error,
2799ceaad40aSRandall Stewart     struct sctp_tmit_chunk *chk, int so_locked
2800ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2801ceaad40aSRandall Stewart     SCTP_UNUSED
2802ceaad40aSRandall Stewart #endif
2803ceaad40aSRandall Stewart )
2804f8829a4aSRandall Stewart {
2805830d754dSRandall Stewart 	struct mbuf *m_notify;
2806f8829a4aSRandall Stewart 	struct sctp_send_failed *ssf;
28079935403aSMichael Tuexen 	struct sctp_send_failed_event *ssfe;
2808f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
2809f8829a4aSRandall Stewart 	int length;
2810f8829a4aSRandall Stewart 
281160990c0cSMichael Tuexen 	if ((stcb == NULL) ||
28129935403aSMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
28139935403aSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
2814f8829a4aSRandall Stewart 		/* event not enabled */
2815f8829a4aSRandall Stewart 		return;
2816830d754dSRandall Stewart 	}
28179935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
28189935403aSMichael Tuexen 		length = sizeof(struct sctp_send_failed_event);
28199935403aSMichael Tuexen 	} else {
28209935403aSMichael Tuexen 		length = sizeof(struct sctp_send_failed);
28219935403aSMichael Tuexen 	}
2822eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(length, 0, M_NOWAIT, 1, MT_DATA);
2823f8829a4aSRandall Stewart 	if (m_notify == NULL)
2824f8829a4aSRandall Stewart 		/* no space left */
2825f8829a4aSRandall Stewart 		return;
2826139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
28279935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
28289935403aSMichael Tuexen 		ssfe = mtod(m_notify, struct sctp_send_failed_event *);
2829e432298aSXin LI 		memset(ssfe, 0, length);
28309935403aSMichael Tuexen 		ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
28311edc9dbaSMichael Tuexen 		if (sent) {
28329935403aSMichael Tuexen 			ssfe->ssfe_flags = SCTP_DATA_SENT;
28331edc9dbaSMichael Tuexen 		} else {
28341edc9dbaSMichael Tuexen 			ssfe->ssfe_flags = SCTP_DATA_UNSENT;
28351edc9dbaSMichael Tuexen 		}
2836e432298aSXin LI 		length += chk->send_size;
2837e432298aSXin LI 		length -= sizeof(struct sctp_data_chunk);
28389935403aSMichael Tuexen 		ssfe->ssfe_length = length;
28399935403aSMichael Tuexen 		ssfe->ssfe_error = error;
28409935403aSMichael Tuexen 		/* not exactly what the user sent in, but should be close :) */
28419935403aSMichael Tuexen 		ssfe->ssfe_info.snd_sid = chk->rec.data.stream_number;
28429935403aSMichael Tuexen 		ssfe->ssfe_info.snd_flags = chk->rec.data.rcv_flags;
28439935403aSMichael Tuexen 		ssfe->ssfe_info.snd_ppid = chk->rec.data.payloadtype;
28449935403aSMichael Tuexen 		ssfe->ssfe_info.snd_context = chk->rec.data.context;
28459935403aSMichael Tuexen 		ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
28469935403aSMichael Tuexen 		ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
28479935403aSMichael Tuexen 		SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed_event);
28489935403aSMichael Tuexen 	} else {
2849f8829a4aSRandall Stewart 		ssf = mtod(m_notify, struct sctp_send_failed *);
2850e432298aSXin LI 		memset(ssf, 0, length);
2851f8829a4aSRandall Stewart 		ssf->ssf_type = SCTP_SEND_FAILED;
28521edc9dbaSMichael Tuexen 		if (sent) {
2853f8829a4aSRandall Stewart 			ssf->ssf_flags = SCTP_DATA_SENT;
28541edc9dbaSMichael Tuexen 		} else {
28551edc9dbaSMichael Tuexen 			ssf->ssf_flags = SCTP_DATA_UNSENT;
28561edc9dbaSMichael Tuexen 		}
2857e432298aSXin LI 		length += chk->send_size;
2858e432298aSXin LI 		length -= sizeof(struct sctp_data_chunk);
2859f8829a4aSRandall Stewart 		ssf->ssf_length = length;
2860f8829a4aSRandall Stewart 		ssf->ssf_error = error;
2861f8829a4aSRandall Stewart 		/* not exactly what the user sent in, but should be close :) */
2862d00aff5dSRandall Stewart 		bzero(&ssf->ssf_info, sizeof(ssf->ssf_info));
2863f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_stream = chk->rec.data.stream_number;
2864f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_ssn = chk->rec.data.stream_seq;
2865f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_flags = chk->rec.data.rcv_flags;
2866f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_ppid = chk->rec.data.payloadtype;
2867f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_context = chk->rec.data.context;
2868f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
2869f8829a4aSRandall Stewart 		ssf->ssf_assoc_id = sctp_get_associd(stcb);
28709935403aSMichael Tuexen 		SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed);
28719935403aSMichael Tuexen 	}
2872830d754dSRandall Stewart 	if (chk->data) {
2873830d754dSRandall Stewart 		/*
2874830d754dSRandall Stewart 		 * trim off the sctp chunk header(it should be there)
2875830d754dSRandall Stewart 		 */
2876830d754dSRandall Stewart 		if (chk->send_size >= sizeof(struct sctp_data_chunk)) {
2877830d754dSRandall Stewart 			m_adj(chk->data, sizeof(struct sctp_data_chunk));
2878830d754dSRandall Stewart 			sctp_mbuf_crush(chk->data);
2879830d754dSRandall Stewart 			chk->send_size -= sizeof(struct sctp_data_chunk);
2880830d754dSRandall Stewart 		}
2881830d754dSRandall Stewart 	}
2882810ec536SMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = chk->data;
2883f8829a4aSRandall Stewart 	/* Steal off the mbuf */
2884f8829a4aSRandall Stewart 	chk->data = NULL;
2885f8829a4aSRandall Stewart 	/*
2886f8829a4aSRandall Stewart 	 * For this case, we check the actual socket buffer, since the assoc
2887f8829a4aSRandall Stewart 	 * is going away we don't want to overfill the socket buffer for a
2888f8829a4aSRandall Stewart 	 * non-reader
2889f8829a4aSRandall Stewart 	 */
2890139bc87fSRandall Stewart 	if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
2891f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
2892f8829a4aSRandall Stewart 		return;
2893f8829a4aSRandall Stewart 	}
2894f8829a4aSRandall Stewart 	/* append to socket */
2895f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
28967215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
2897f8829a4aSRandall Stewart 	    m_notify);
2898f8829a4aSRandall Stewart 	if (control == NULL) {
2899f8829a4aSRandall Stewart 		/* no memory */
2900f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
2901f8829a4aSRandall Stewart 		return;
2902f8829a4aSRandall Stewart 	}
2903139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
2904f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
2905f8829a4aSRandall Stewart 	    control,
2906cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1,
2907cfde3ff7SRandall Stewart 	    SCTP_READ_LOCK_NOT_HELD,
2908cfde3ff7SRandall Stewart 	    so_locked);
2909f8829a4aSRandall Stewart }
2910f8829a4aSRandall Stewart 
2911f8829a4aSRandall Stewart 
2912f8829a4aSRandall Stewart static void
2913f8829a4aSRandall Stewart sctp_notify_send_failed2(struct sctp_tcb *stcb, uint32_t error,
2914ceaad40aSRandall Stewart     struct sctp_stream_queue_pending *sp, int so_locked
2915ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2916ceaad40aSRandall Stewart     SCTP_UNUSED
2917ceaad40aSRandall Stewart #endif
2918ceaad40aSRandall Stewart )
2919f8829a4aSRandall Stewart {
2920f8829a4aSRandall Stewart 	struct mbuf *m_notify;
2921f8829a4aSRandall Stewart 	struct sctp_send_failed *ssf;
29229935403aSMichael Tuexen 	struct sctp_send_failed_event *ssfe;
2923f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
2924f8829a4aSRandall Stewart 	int length;
2925f8829a4aSRandall Stewart 
292660990c0cSMichael Tuexen 	if ((stcb == NULL) ||
29279935403aSMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
29289935403aSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
2929f8829a4aSRandall Stewart 		/* event not enabled */
2930f8829a4aSRandall Stewart 		return;
2931830d754dSRandall Stewart 	}
29329935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
29339935403aSMichael Tuexen 		length = sizeof(struct sctp_send_failed_event);
29349935403aSMichael Tuexen 	} else {
29359935403aSMichael Tuexen 		length = sizeof(struct sctp_send_failed);
29369935403aSMichael Tuexen 	}
2937eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(length, 0, M_NOWAIT, 1, MT_DATA);
29389935403aSMichael Tuexen 	if (m_notify == NULL) {
2939f8829a4aSRandall Stewart 		/* no space left */
2940f8829a4aSRandall Stewart 		return;
29419935403aSMichael Tuexen 	}
2942139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
29439935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
29449935403aSMichael Tuexen 		ssfe = mtod(m_notify, struct sctp_send_failed_event *);
2945e432298aSXin LI 		memset(ssfe, 0, length);
2946ad83c8a5SMichael Tuexen 		ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
29479935403aSMichael Tuexen 		ssfe->ssfe_flags = SCTP_DATA_UNSENT;
2948e432298aSXin LI 		length += sp->length;
29499935403aSMichael Tuexen 		ssfe->ssfe_length = length;
29509935403aSMichael Tuexen 		ssfe->ssfe_error = error;
29519935403aSMichael Tuexen 		/* not exactly what the user sent in, but should be close :) */
29529935403aSMichael Tuexen 		ssfe->ssfe_info.snd_sid = sp->stream;
29539935403aSMichael Tuexen 		if (sp->some_taken) {
29549935403aSMichael Tuexen 			ssfe->ssfe_info.snd_flags = SCTP_DATA_LAST_FRAG;
29559935403aSMichael Tuexen 		} else {
29569935403aSMichael Tuexen 			ssfe->ssfe_info.snd_flags = SCTP_DATA_NOT_FRAG;
29579935403aSMichael Tuexen 		}
29589935403aSMichael Tuexen 		ssfe->ssfe_info.snd_ppid = sp->ppid;
29599935403aSMichael Tuexen 		ssfe->ssfe_info.snd_context = sp->context;
29609935403aSMichael Tuexen 		ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
29619935403aSMichael Tuexen 		ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
29629935403aSMichael Tuexen 		SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed_event);
29639935403aSMichael Tuexen 	} else {
2964f8829a4aSRandall Stewart 		ssf = mtod(m_notify, struct sctp_send_failed *);
2965e432298aSXin LI 		memset(ssf, 0, length);
2966f8829a4aSRandall Stewart 		ssf->ssf_type = SCTP_SEND_FAILED;
2967f8829a4aSRandall Stewart 		ssf->ssf_flags = SCTP_DATA_UNSENT;
2968e432298aSXin LI 		length += sp->length;
2969f8829a4aSRandall Stewart 		ssf->ssf_length = length;
2970f8829a4aSRandall Stewart 		ssf->ssf_error = error;
2971f8829a4aSRandall Stewart 		/* not exactly what the user sent in, but should be close :) */
2972f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_stream = sp->stream;
2973f3b05218SMichael Tuexen 		ssf->ssf_info.sinfo_ssn = 0;
2974fc14de76SRandall Stewart 		if (sp->some_taken) {
2975fc14de76SRandall Stewart 			ssf->ssf_info.sinfo_flags = SCTP_DATA_LAST_FRAG;
2976fc14de76SRandall Stewart 		} else {
2977fc14de76SRandall Stewart 			ssf->ssf_info.sinfo_flags = SCTP_DATA_NOT_FRAG;
2978fc14de76SRandall Stewart 		}
2979f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_ppid = sp->ppid;
2980f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_context = sp->context;
2981f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
2982f8829a4aSRandall Stewart 		ssf->ssf_assoc_id = sctp_get_associd(stcb);
2983139bc87fSRandall Stewart 		SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed);
29849935403aSMichael Tuexen 	}
29859935403aSMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = sp->data;
2986f8829a4aSRandall Stewart 
2987f8829a4aSRandall Stewart 	/* Steal off the mbuf */
2988f8829a4aSRandall Stewart 	sp->data = NULL;
2989f8829a4aSRandall Stewart 	/*
2990f8829a4aSRandall Stewart 	 * For this case, we check the actual socket buffer, since the assoc
2991f8829a4aSRandall Stewart 	 * is going away we don't want to overfill the socket buffer for a
2992f8829a4aSRandall Stewart 	 * non-reader
2993f8829a4aSRandall Stewart 	 */
2994139bc87fSRandall Stewart 	if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
2995f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
2996f8829a4aSRandall Stewart 		return;
2997f8829a4aSRandall Stewart 	}
2998f8829a4aSRandall Stewart 	/* append to socket */
2999f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
30007215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3001f8829a4aSRandall Stewart 	    m_notify);
3002f8829a4aSRandall Stewart 	if (control == NULL) {
3003f8829a4aSRandall Stewart 		/* no memory */
3004f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3005f8829a4aSRandall Stewart 		return;
3006f8829a4aSRandall Stewart 	}
3007139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3008f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3009f8829a4aSRandall Stewart 	    control,
3010cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3011f8829a4aSRandall Stewart }
3012f8829a4aSRandall Stewart 
3013f8829a4aSRandall Stewart 
3014f8829a4aSRandall Stewart 
3015f8829a4aSRandall Stewart static void
30167215cc1bSMichael Tuexen sctp_notify_adaptation_layer(struct sctp_tcb *stcb)
3017f8829a4aSRandall Stewart {
3018f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3019f8829a4aSRandall Stewart 	struct sctp_adaptation_event *sai;
3020f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3021f8829a4aSRandall Stewart 
302260990c0cSMichael Tuexen 	if ((stcb == NULL) ||
302360990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT)) {
3024f8829a4aSRandall Stewart 		/* event not enabled */
3025f8829a4aSRandall Stewart 		return;
3026830d754dSRandall Stewart 	}
3027eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 0, M_NOWAIT, 1, MT_DATA);
3028f8829a4aSRandall Stewart 	if (m_notify == NULL)
3029f8829a4aSRandall Stewart 		/* no space left */
3030f8829a4aSRandall Stewart 		return;
3031139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3032f8829a4aSRandall Stewart 	sai = mtod(m_notify, struct sctp_adaptation_event *);
3033e432298aSXin LI 	memset(sai, 0, sizeof(struct sctp_adaptation_event));
3034f8829a4aSRandall Stewart 	sai->sai_type = SCTP_ADAPTATION_INDICATION;
3035f8829a4aSRandall Stewart 	sai->sai_flags = 0;
3036f8829a4aSRandall Stewart 	sai->sai_length = sizeof(struct sctp_adaptation_event);
30372afb3e84SRandall Stewart 	sai->sai_adaptation_ind = stcb->asoc.peers_adaptation;
3038f8829a4aSRandall Stewart 	sai->sai_assoc_id = sctp_get_associd(stcb);
3039f8829a4aSRandall Stewart 
3040139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_adaptation_event);
3041139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3042f8829a4aSRandall Stewart 
3043f8829a4aSRandall Stewart 	/* append to socket */
3044f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
30457215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3046f8829a4aSRandall Stewart 	    m_notify);
3047f8829a4aSRandall Stewart 	if (control == NULL) {
3048f8829a4aSRandall Stewart 		/* no memory */
3049f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3050f8829a4aSRandall Stewart 		return;
3051f8829a4aSRandall Stewart 	}
3052139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3053139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3054f8829a4aSRandall Stewart 	/* not that we need this */
3055f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3056f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3057f8829a4aSRandall Stewart 	    control,
3058cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3059f8829a4aSRandall Stewart }
3060f8829a4aSRandall Stewart 
306103b0b021SRandall Stewart /* This always must be called with the read-queue LOCKED in the INP */
3062810ec536SMichael Tuexen static void
30632dad8a55SRandall Stewart sctp_notify_partial_delivery_indication(struct sctp_tcb *stcb, uint32_t error,
3064810ec536SMichael Tuexen     uint32_t val, int so_locked
3065810ec536SMichael Tuexen #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3066810ec536SMichael Tuexen     SCTP_UNUSED
3067810ec536SMichael Tuexen #endif
3068810ec536SMichael Tuexen )
3069f8829a4aSRandall Stewart {
3070f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3071f8829a4aSRandall Stewart 	struct sctp_pdapi_event *pdapi;
3072f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
307303b0b021SRandall Stewart 	struct sockbuf *sb;
3074f8829a4aSRandall Stewart 
307560990c0cSMichael Tuexen 	if ((stcb == NULL) ||
307660990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_PDAPIEVNT)) {
3077f8829a4aSRandall Stewart 		/* event not enabled */
3078f8829a4aSRandall Stewart 		return;
3079830d754dSRandall Stewart 	}
3080cd1386abSMichael Tuexen 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
3081cd1386abSMichael Tuexen 		return;
3082cd1386abSMichael Tuexen 	}
3083eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_pdapi_event), 0, M_NOWAIT, 1, MT_DATA);
3084f8829a4aSRandall Stewart 	if (m_notify == NULL)
3085f8829a4aSRandall Stewart 		/* no space left */
3086f8829a4aSRandall Stewart 		return;
3087139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3088f8829a4aSRandall Stewart 	pdapi = mtod(m_notify, struct sctp_pdapi_event *);
3089e432298aSXin LI 	memset(pdapi, 0, sizeof(struct sctp_pdapi_event));
3090f8829a4aSRandall Stewart 	pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
3091f8829a4aSRandall Stewart 	pdapi->pdapi_flags = 0;
3092f8829a4aSRandall Stewart 	pdapi->pdapi_length = sizeof(struct sctp_pdapi_event);
3093f8829a4aSRandall Stewart 	pdapi->pdapi_indication = error;
30949a6142d8SRandall Stewart 	pdapi->pdapi_stream = (val >> 16);
30959a6142d8SRandall Stewart 	pdapi->pdapi_seq = (val & 0x0000ffff);
3096f8829a4aSRandall Stewart 	pdapi->pdapi_assoc_id = sctp_get_associd(stcb);
3097f8829a4aSRandall Stewart 
3098139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_pdapi_event);
3099139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3100f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
31017215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3102f8829a4aSRandall Stewart 	    m_notify);
3103f8829a4aSRandall Stewart 	if (control == NULL) {
3104f8829a4aSRandall Stewart 		/* no memory */
3105f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3106f8829a4aSRandall Stewart 		return;
3107f8829a4aSRandall Stewart 	}
3108139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3109139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3110f8829a4aSRandall Stewart 	/* not that we need this */
3111f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
311203b0b021SRandall Stewart 	control->held_length = 0;
311303b0b021SRandall Stewart 	control->length = 0;
311403b0b021SRandall Stewart 	sb = &stcb->sctp_socket->so_rcv;
3115b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3116139bc87fSRandall Stewart 		sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m_notify));
311780fefe0aSRandall Stewart 	}
311803b0b021SRandall Stewart 	sctp_sballoc(stcb, sb, m_notify);
3119b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
312003b0b021SRandall Stewart 		sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
312180fefe0aSRandall Stewart 	}
3122139bc87fSRandall Stewart 	atomic_add_int(&control->length, SCTP_BUF_LEN(m_notify));
312303b0b021SRandall Stewart 	control->end_added = 1;
312403b0b021SRandall Stewart 	if (stcb->asoc.control_pdapi)
312503b0b021SRandall Stewart 		TAILQ_INSERT_AFTER(&stcb->sctp_ep->read_queue, stcb->asoc.control_pdapi, control, next);
312603b0b021SRandall Stewart 	else {
312703b0b021SRandall Stewart 		/* we really should not see this case */
312803b0b021SRandall Stewart 		TAILQ_INSERT_TAIL(&stcb->sctp_ep->read_queue, control, next);
312903b0b021SRandall Stewart 	}
313003b0b021SRandall Stewart 	if (stcb->sctp_ep && stcb->sctp_socket) {
313103b0b021SRandall Stewart 		/* This should always be the case */
3132810ec536SMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3133810ec536SMichael Tuexen 		struct socket *so;
3134810ec536SMichael Tuexen 
3135810ec536SMichael Tuexen 		so = SCTP_INP_SO(stcb->sctp_ep);
3136810ec536SMichael Tuexen 		if (!so_locked) {
3137810ec536SMichael Tuexen 			atomic_add_int(&stcb->asoc.refcnt, 1);
3138810ec536SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
3139810ec536SMichael Tuexen 			SCTP_SOCKET_LOCK(so, 1);
3140810ec536SMichael Tuexen 			SCTP_TCB_LOCK(stcb);
3141810ec536SMichael Tuexen 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
3142810ec536SMichael Tuexen 			if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3143810ec536SMichael Tuexen 				SCTP_SOCKET_UNLOCK(so, 1);
3144810ec536SMichael Tuexen 				return;
3145810ec536SMichael Tuexen 			}
3146810ec536SMichael Tuexen 		}
3147810ec536SMichael Tuexen #endif
314803b0b021SRandall Stewart 		sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
3149810ec536SMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3150810ec536SMichael Tuexen 		if (!so_locked) {
3151810ec536SMichael Tuexen 			SCTP_SOCKET_UNLOCK(so, 1);
3152810ec536SMichael Tuexen 		}
3153810ec536SMichael Tuexen #endif
3154f8829a4aSRandall Stewart 	}
3155f8829a4aSRandall Stewart }
3156f8829a4aSRandall Stewart 
3157f8829a4aSRandall Stewart static void
3158f8829a4aSRandall Stewart sctp_notify_shutdown_event(struct sctp_tcb *stcb)
3159f8829a4aSRandall Stewart {
3160f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3161f8829a4aSRandall Stewart 	struct sctp_shutdown_event *sse;
3162f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3163f8829a4aSRandall Stewart 
3164f8829a4aSRandall Stewart 	/*
3165f8829a4aSRandall Stewart 	 * For TCP model AND UDP connected sockets we will send an error up
3166f8829a4aSRandall Stewart 	 * when an SHUTDOWN completes
3167f8829a4aSRandall Stewart 	 */
3168f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3169f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3170f8829a4aSRandall Stewart 		/* mark socket closed for read/write and wakeup! */
3171ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3172ceaad40aSRandall Stewart 		struct socket *so;
3173ceaad40aSRandall Stewart 
3174ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
3175ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
3176ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3177ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
3178ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
3179ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
3180ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3181ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
3182ceaad40aSRandall Stewart 			return;
3183ceaad40aSRandall Stewart 		}
3184ceaad40aSRandall Stewart #endif
3185f8829a4aSRandall Stewart 		socantsendmore(stcb->sctp_socket);
3186ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3187ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
3188ceaad40aSRandall Stewart #endif
3189f8829a4aSRandall Stewart 	}
3190e2e7c62eSMichael Tuexen 	if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)) {
3191f8829a4aSRandall Stewart 		/* event not enabled */
3192f8829a4aSRandall Stewart 		return;
3193830d754dSRandall Stewart 	}
3194eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_event), 0, M_NOWAIT, 1, MT_DATA);
3195f8829a4aSRandall Stewart 	if (m_notify == NULL)
3196f8829a4aSRandall Stewart 		/* no space left */
3197f8829a4aSRandall Stewart 		return;
3198f8829a4aSRandall Stewart 	sse = mtod(m_notify, struct sctp_shutdown_event *);
3199e432298aSXin LI 	memset(sse, 0, sizeof(struct sctp_shutdown_event));
3200f8829a4aSRandall Stewart 	sse->sse_type = SCTP_SHUTDOWN_EVENT;
3201f8829a4aSRandall Stewart 	sse->sse_flags = 0;
3202f8829a4aSRandall Stewart 	sse->sse_length = sizeof(struct sctp_shutdown_event);
3203f8829a4aSRandall Stewart 	sse->sse_assoc_id = sctp_get_associd(stcb);
3204f8829a4aSRandall Stewart 
3205139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_shutdown_event);
3206139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3207f8829a4aSRandall Stewart 
3208f8829a4aSRandall Stewart 	/* append to socket */
3209f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
32107215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3211f8829a4aSRandall Stewart 	    m_notify);
3212f8829a4aSRandall Stewart 	if (control == NULL) {
3213f8829a4aSRandall Stewart 		/* no memory */
3214f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3215f8829a4aSRandall Stewart 		return;
3216f8829a4aSRandall Stewart 	}
3217139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3218139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3219f8829a4aSRandall Stewart 	/* not that we need this */
3220f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3221f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3222f8829a4aSRandall Stewart 	    control,
3223cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3224f8829a4aSRandall Stewart }
3225f8829a4aSRandall Stewart 
3226f8829a4aSRandall Stewart static void
3227830d754dSRandall Stewart sctp_notify_sender_dry_event(struct sctp_tcb *stcb,
3228830d754dSRandall Stewart     int so_locked
3229830d754dSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3230830d754dSRandall Stewart     SCTP_UNUSED
3231830d754dSRandall Stewart #endif
3232830d754dSRandall Stewart )
3233830d754dSRandall Stewart {
3234830d754dSRandall Stewart 	struct mbuf *m_notify;
3235830d754dSRandall Stewart 	struct sctp_sender_dry_event *event;
3236830d754dSRandall Stewart 	struct sctp_queued_to_read *control;
3237830d754dSRandall Stewart 
323860990c0cSMichael Tuexen 	if ((stcb == NULL) ||
323960990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DRYEVNT)) {
3240830d754dSRandall Stewart 		/* event not enabled */
3241830d754dSRandall Stewart 		return;
3242830d754dSRandall Stewart 	}
3243eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_sender_dry_event), 0, M_NOWAIT, 1, MT_DATA);
3244830d754dSRandall Stewart 	if (m_notify == NULL) {
3245830d754dSRandall Stewart 		/* no space left */
3246830d754dSRandall Stewart 		return;
3247830d754dSRandall Stewart 	}
3248830d754dSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3249830d754dSRandall Stewart 	event = mtod(m_notify, struct sctp_sender_dry_event *);
3250e432298aSXin LI 	memset(event, 0, sizeof(struct sctp_sender_dry_event));
3251830d754dSRandall Stewart 	event->sender_dry_type = SCTP_SENDER_DRY_EVENT;
3252830d754dSRandall Stewart 	event->sender_dry_flags = 0;
3253830d754dSRandall Stewart 	event->sender_dry_length = sizeof(struct sctp_sender_dry_event);
3254830d754dSRandall Stewart 	event->sender_dry_assoc_id = sctp_get_associd(stcb);
3255830d754dSRandall Stewart 
3256830d754dSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_sender_dry_event);
3257830d754dSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3258830d754dSRandall Stewart 
3259830d754dSRandall Stewart 	/* append to socket */
3260830d754dSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
32617215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
32627215cc1bSMichael Tuexen 	    m_notify);
3263830d754dSRandall Stewart 	if (control == NULL) {
3264830d754dSRandall Stewart 		/* no memory */
3265830d754dSRandall Stewart 		sctp_m_freem(m_notify);
3266830d754dSRandall Stewart 		return;
3267830d754dSRandall Stewart 	}
3268830d754dSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3269830d754dSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3270830d754dSRandall Stewart 	/* not that we need this */
3271830d754dSRandall Stewart 	control->tail_mbuf = m_notify;
3272830d754dSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3273cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3274830d754dSRandall Stewart }
3275830d754dSRandall Stewart 
3276ea44232bSRandall Stewart 
3277c4e848b7SRandall Stewart void
3278c4e848b7SRandall Stewart sctp_notify_stream_reset_add(struct sctp_tcb *stcb, uint16_t numberin, uint16_t numberout, int flag)
3279ea44232bSRandall Stewart {
3280ea44232bSRandall Stewart 	struct mbuf *m_notify;
3281ea44232bSRandall Stewart 	struct sctp_queued_to_read *control;
3282c4e848b7SRandall Stewart 	struct sctp_stream_change_event *stradd;
3283ea44232bSRandall Stewart 
32848c501e51SMichael Tuexen 	if ((stcb == NULL) ||
32858c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_CHANGEEVNT))) {
3286ea44232bSRandall Stewart 		/* event not enabled */
3287ea44232bSRandall Stewart 		return;
3288ea44232bSRandall Stewart 	}
3289c4e848b7SRandall Stewart 	if ((stcb->asoc.peer_req_out) && flag) {
3290c4e848b7SRandall Stewart 		/* Peer made the request, don't tell the local user */
3291c4e848b7SRandall Stewart 		stcb->asoc.peer_req_out = 0;
3292c4e848b7SRandall Stewart 		return;
3293c4e848b7SRandall Stewart 	}
3294c4e848b7SRandall Stewart 	stcb->asoc.peer_req_out = 0;
3295e432298aSXin LI 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_stream_change_event), 0, M_NOWAIT, 1, MT_DATA);
3296ea44232bSRandall Stewart 	if (m_notify == NULL)
3297ea44232bSRandall Stewart 		/* no space left */
3298ea44232bSRandall Stewart 		return;
3299ea44232bSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3300c4e848b7SRandall Stewart 	stradd = mtod(m_notify, struct sctp_stream_change_event *);
3301e432298aSXin LI 	memset(stradd, 0, sizeof(struct sctp_stream_change_event));
3302c4e848b7SRandall Stewart 	stradd->strchange_type = SCTP_STREAM_CHANGE_EVENT;
3303c4e848b7SRandall Stewart 	stradd->strchange_flags = flag;
3304e432298aSXin LI 	stradd->strchange_length = sizeof(struct sctp_stream_change_event);
3305c4e848b7SRandall Stewart 	stradd->strchange_assoc_id = sctp_get_associd(stcb);
3306c4e848b7SRandall Stewart 	stradd->strchange_instrms = numberin;
3307c4e848b7SRandall Stewart 	stradd->strchange_outstrms = numberout;
3308e432298aSXin LI 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_stream_change_event);
3309ea44232bSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3310ea44232bSRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3311ea44232bSRandall Stewart 		/* no space */
3312ea44232bSRandall Stewart 		sctp_m_freem(m_notify);
3313ea44232bSRandall Stewart 		return;
3314ea44232bSRandall Stewart 	}
3315ea44232bSRandall Stewart 	/* append to socket */
3316ea44232bSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
33177215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3318ea44232bSRandall Stewart 	    m_notify);
3319ea44232bSRandall Stewart 	if (control == NULL) {
3320ea44232bSRandall Stewart 		/* no memory */
3321ea44232bSRandall Stewart 		sctp_m_freem(m_notify);
3322ea44232bSRandall Stewart 		return;
3323ea44232bSRandall Stewart 	}
3324ea44232bSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3325ea44232bSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3326ea44232bSRandall Stewart 	/* not that we need this */
3327ea44232bSRandall Stewart 	control->tail_mbuf = m_notify;
3328ea44232bSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3329ea44232bSRandall Stewart 	    control,
3330cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3331ea44232bSRandall Stewart }
3332ea44232bSRandall Stewart 
3333c4e848b7SRandall Stewart void
3334c4e848b7SRandall Stewart sctp_notify_stream_reset_tsn(struct sctp_tcb *stcb, uint32_t sending_tsn, uint32_t recv_tsn, int flag)
3335c4e848b7SRandall Stewart {
3336c4e848b7SRandall Stewart 	struct mbuf *m_notify;
3337c4e848b7SRandall Stewart 	struct sctp_queued_to_read *control;
3338c4e848b7SRandall Stewart 	struct sctp_assoc_reset_event *strasoc;
3339c4e848b7SRandall Stewart 
33408c501e51SMichael Tuexen 	if ((stcb == NULL) ||
33418c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ASSOC_RESETEVNT))) {
3342c4e848b7SRandall Stewart 		/* event not enabled */
3343c4e848b7SRandall Stewart 		return;
3344c4e848b7SRandall Stewart 	}
3345e432298aSXin LI 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_assoc_reset_event), 0, M_NOWAIT, 1, MT_DATA);
3346c4e848b7SRandall Stewart 	if (m_notify == NULL)
3347c4e848b7SRandall Stewart 		/* no space left */
3348c4e848b7SRandall Stewart 		return;
3349c4e848b7SRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3350c4e848b7SRandall Stewart 	strasoc = mtod(m_notify, struct sctp_assoc_reset_event *);
3351e432298aSXin LI 	memset(strasoc, 0, sizeof(struct sctp_assoc_reset_event));
3352c4e848b7SRandall Stewart 	strasoc->assocreset_type = SCTP_ASSOC_RESET_EVENT;
3353c4e848b7SRandall Stewart 	strasoc->assocreset_flags = flag;
3354e432298aSXin LI 	strasoc->assocreset_length = sizeof(struct sctp_assoc_reset_event);
3355c4e848b7SRandall Stewart 	strasoc->assocreset_assoc_id = sctp_get_associd(stcb);
3356c4e848b7SRandall Stewart 	strasoc->assocreset_local_tsn = sending_tsn;
3357c4e848b7SRandall Stewart 	strasoc->assocreset_remote_tsn = recv_tsn;
3358e432298aSXin LI 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_assoc_reset_event);
3359c4e848b7SRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3360c4e848b7SRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3361c4e848b7SRandall Stewart 		/* no space */
3362c4e848b7SRandall Stewart 		sctp_m_freem(m_notify);
3363c4e848b7SRandall Stewart 		return;
3364c4e848b7SRandall Stewart 	}
3365c4e848b7SRandall Stewart 	/* append to socket */
3366c4e848b7SRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3367c4e848b7SRandall Stewart 	    0, 0, stcb->asoc.context, 0, 0, 0,
3368c4e848b7SRandall Stewart 	    m_notify);
3369c4e848b7SRandall Stewart 	if (control == NULL) {
3370c4e848b7SRandall Stewart 		/* no memory */
3371c4e848b7SRandall Stewart 		sctp_m_freem(m_notify);
3372c4e848b7SRandall Stewart 		return;
3373c4e848b7SRandall Stewart 	}
3374c4e848b7SRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3375c4e848b7SRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3376c4e848b7SRandall Stewart 	/* not that we need this */
3377c4e848b7SRandall Stewart 	control->tail_mbuf = m_notify;
3378c4e848b7SRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3379c4e848b7SRandall Stewart 	    control,
3380c4e848b7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3381c4e848b7SRandall Stewart }
3382c4e848b7SRandall Stewart 
3383c4e848b7SRandall Stewart 
3384ea44232bSRandall Stewart 
3385830d754dSRandall Stewart static void
3386f8829a4aSRandall Stewart sctp_notify_stream_reset(struct sctp_tcb *stcb,
3387f8829a4aSRandall Stewart     int number_entries, uint16_t * list, int flag)
3388f8829a4aSRandall Stewart {
3389f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3390f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3391f8829a4aSRandall Stewart 	struct sctp_stream_reset_event *strreset;
3392f8829a4aSRandall Stewart 	int len;
3393f8829a4aSRandall Stewart 
33948c501e51SMichael Tuexen 	if ((stcb == NULL) ||
33958c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT))) {
3396f8829a4aSRandall Stewart 		/* event not enabled */
3397f8829a4aSRandall Stewart 		return;
3398830d754dSRandall Stewart 	}
3399eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
3400f8829a4aSRandall Stewart 	if (m_notify == NULL)
3401f8829a4aSRandall Stewart 		/* no space left */
3402f8829a4aSRandall Stewart 		return;
3403139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3404f8829a4aSRandall Stewart 	len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
3405f8829a4aSRandall Stewart 	if (len > M_TRAILINGSPACE(m_notify)) {
3406f8829a4aSRandall Stewart 		/* never enough room */
3407f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3408f8829a4aSRandall Stewart 		return;
3409f8829a4aSRandall Stewart 	}
3410f8829a4aSRandall Stewart 	strreset = mtod(m_notify, struct sctp_stream_reset_event *);
3411e432298aSXin LI 	memset(strreset, 0, len);
3412f8829a4aSRandall Stewart 	strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
3413c4e848b7SRandall Stewart 	strreset->strreset_flags = flag;
3414f8829a4aSRandall Stewart 	strreset->strreset_length = len;
3415f8829a4aSRandall Stewart 	strreset->strreset_assoc_id = sctp_get_associd(stcb);
3416f8829a4aSRandall Stewart 	if (number_entries) {
3417f8829a4aSRandall Stewart 		int i;
3418f8829a4aSRandall Stewart 
3419f8829a4aSRandall Stewart 		for (i = 0; i < number_entries; i++) {
3420c4e848b7SRandall Stewart 			strreset->strreset_stream_list[i] = ntohs(list[i]);
3421f8829a4aSRandall Stewart 		}
3422f8829a4aSRandall Stewart 	}
3423139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = len;
3424139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3425139bc87fSRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3426f8829a4aSRandall Stewart 		/* no space */
3427f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3428f8829a4aSRandall Stewart 		return;
3429f8829a4aSRandall Stewart 	}
3430f8829a4aSRandall Stewart 	/* append to socket */
3431f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
34327215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3433f8829a4aSRandall Stewart 	    m_notify);
3434f8829a4aSRandall Stewart 	if (control == NULL) {
3435f8829a4aSRandall Stewart 		/* no memory */
3436f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3437f8829a4aSRandall Stewart 		return;
3438f8829a4aSRandall Stewart 	}
3439139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3440139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3441f8829a4aSRandall Stewart 	/* not that we need this */
3442f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3443f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3444f8829a4aSRandall Stewart 	    control,
3445cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3446f8829a4aSRandall Stewart }
3447f8829a4aSRandall Stewart 
3448f8829a4aSRandall Stewart 
3449389b1b11SMichael Tuexen static void
3450389b1b11SMichael Tuexen sctp_notify_remote_error(struct sctp_tcb *stcb, uint16_t error, struct sctp_error_chunk *chunk)
3451389b1b11SMichael Tuexen {
3452389b1b11SMichael Tuexen 	struct mbuf *m_notify;
3453389b1b11SMichael Tuexen 	struct sctp_remote_error *sre;
3454389b1b11SMichael Tuexen 	struct sctp_queued_to_read *control;
3455389b1b11SMichael Tuexen 	size_t notif_len, chunk_len;
3456389b1b11SMichael Tuexen 
3457389b1b11SMichael Tuexen 	if ((stcb == NULL) ||
3458389b1b11SMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPEERERR)) {
3459389b1b11SMichael Tuexen 		return;
3460389b1b11SMichael Tuexen 	}
3461389b1b11SMichael Tuexen 	if (chunk != NULL) {
3462c9eb4473SMichael Tuexen 		chunk_len = ntohs(chunk->ch.chunk_length);
3463389b1b11SMichael Tuexen 	} else {
3464389b1b11SMichael Tuexen 		chunk_len = 0;
3465389b1b11SMichael Tuexen 	}
3466389b1b11SMichael Tuexen 	notif_len = sizeof(struct sctp_remote_error) + chunk_len;
3467eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3468389b1b11SMichael Tuexen 	if (m_notify == NULL) {
3469389b1b11SMichael Tuexen 		/* Retry with smaller value. */
3470389b1b11SMichael Tuexen 		notif_len = sizeof(struct sctp_remote_error);
3471eb1b1807SGleb Smirnoff 		m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3472389b1b11SMichael Tuexen 		if (m_notify == NULL) {
3473389b1b11SMichael Tuexen 			return;
3474389b1b11SMichael Tuexen 		}
3475389b1b11SMichael Tuexen 	}
3476389b1b11SMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = NULL;
3477389b1b11SMichael Tuexen 	sre = mtod(m_notify, struct sctp_remote_error *);
347856711f94SMichael Tuexen 	memset(sre, 0, notif_len);
3479389b1b11SMichael Tuexen 	sre->sre_type = SCTP_REMOTE_ERROR;
3480389b1b11SMichael Tuexen 	sre->sre_flags = 0;
3481389b1b11SMichael Tuexen 	sre->sre_length = sizeof(struct sctp_remote_error);
3482389b1b11SMichael Tuexen 	sre->sre_error = error;
3483389b1b11SMichael Tuexen 	sre->sre_assoc_id = sctp_get_associd(stcb);
3484389b1b11SMichael Tuexen 	if (notif_len > sizeof(struct sctp_remote_error)) {
3485389b1b11SMichael Tuexen 		memcpy(sre->sre_data, chunk, chunk_len);
3486389b1b11SMichael Tuexen 		sre->sre_length += chunk_len;
3487389b1b11SMichael Tuexen 	}
3488389b1b11SMichael Tuexen 	SCTP_BUF_LEN(m_notify) = sre->sre_length;
3489389b1b11SMichael Tuexen 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3490389b1b11SMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3491389b1b11SMichael Tuexen 	    m_notify);
3492389b1b11SMichael Tuexen 	if (control != NULL) {
3493389b1b11SMichael Tuexen 		control->length = SCTP_BUF_LEN(m_notify);
3494389b1b11SMichael Tuexen 		/* not that we need this */
3495389b1b11SMichael Tuexen 		control->tail_mbuf = m_notify;
3496389b1b11SMichael Tuexen 		control->spec_flags = M_NOTIFICATION;
3497389b1b11SMichael Tuexen 		sctp_add_to_readq(stcb->sctp_ep, stcb,
3498389b1b11SMichael Tuexen 		    control,
3499389b1b11SMichael Tuexen 		    &stcb->sctp_socket->so_rcv, 1,
3500389b1b11SMichael Tuexen 		    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3501389b1b11SMichael Tuexen 	} else {
3502389b1b11SMichael Tuexen 		sctp_m_freem(m_notify);
3503389b1b11SMichael Tuexen 	}
3504389b1b11SMichael Tuexen }
3505389b1b11SMichael Tuexen 
3506389b1b11SMichael Tuexen 
3507f8829a4aSRandall Stewart void
3508f8829a4aSRandall Stewart sctp_ulp_notify(uint32_t notification, struct sctp_tcb *stcb,
3509ceaad40aSRandall Stewart     uint32_t error, void *data, int so_locked
3510ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3511ceaad40aSRandall Stewart     SCTP_UNUSED
3512ceaad40aSRandall Stewart #endif
3513ceaad40aSRandall Stewart )
3514f8829a4aSRandall Stewart {
3515830d754dSRandall Stewart 	if ((stcb == NULL) ||
3516830d754dSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3517f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3518830d754dSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3519830d754dSRandall Stewart 		/* If the socket is gone we are out of here */
3520f8829a4aSRandall Stewart 		return;
3521f8829a4aSRandall Stewart 	}
3522a99b6783SRandall Stewart 	if (stcb->sctp_socket->so_rcv.sb_state & SBS_CANTRCVMORE) {
3523a99b6783SRandall Stewart 		return;
3524a99b6783SRandall Stewart 	}
3525fb4a67d2SMichael Tuexen 	if ((stcb->asoc.state & SCTP_STATE_COOKIE_WAIT) ||
3526fb4a67d2SMichael Tuexen 	    (stcb->asoc.state & SCTP_STATE_COOKIE_ECHOED)) {
352717205eccSRandall Stewart 		if ((notification == SCTP_NOTIFY_INTERFACE_DOWN) ||
352817205eccSRandall Stewart 		    (notification == SCTP_NOTIFY_INTERFACE_UP) ||
352917205eccSRandall Stewart 		    (notification == SCTP_NOTIFY_INTERFACE_CONFIRMED)) {
353017205eccSRandall Stewart 			/* Don't report these in front states */
353117205eccSRandall Stewart 			return;
353217205eccSRandall Stewart 		}
353317205eccSRandall Stewart 	}
3534f8829a4aSRandall Stewart 	switch (notification) {
3535f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_UP:
3536f8829a4aSRandall Stewart 		if (stcb->asoc.assoc_up_sent == 0) {
3537410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_UP, stcb, error, NULL, 0, so_locked);
3538f8829a4aSRandall Stewart 			stcb->asoc.assoc_up_sent = 1;
3539f8829a4aSRandall Stewart 		}
35402afb3e84SRandall Stewart 		if (stcb->asoc.adaptation_needed && (stcb->asoc.adaptation_sent == 0)) {
35417215cc1bSMichael Tuexen 			sctp_notify_adaptation_layer(stcb);
35422afb3e84SRandall Stewart 		}
3543830d754dSRandall Stewart 		if (stcb->asoc.peer_supports_auth == 0) {
3544830d754dSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3545830d754dSRandall Stewart 			    NULL, so_locked);
3546830d754dSRandall Stewart 		}
3547f8829a4aSRandall Stewart 		break;
3548f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_DOWN:
3549410a3b1eSMichael Tuexen 		sctp_notify_assoc_change(SCTP_SHUTDOWN_COMP, stcb, error, NULL, 0, so_locked);
3550f8829a4aSRandall Stewart 		break;
3551f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_DOWN:
3552f8829a4aSRandall Stewart 		{
3553f8829a4aSRandall Stewart 			struct sctp_nets *net;
3554f8829a4aSRandall Stewart 
3555f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
3556f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_UNREACHABLE,
3557f8829a4aSRandall Stewart 			    (struct sockaddr *)&net->ro._l_addr, error);
3558f8829a4aSRandall Stewart 			break;
3559f8829a4aSRandall Stewart 		}
3560f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_UP:
3561f8829a4aSRandall Stewart 		{
3562f8829a4aSRandall Stewart 			struct sctp_nets *net;
3563f8829a4aSRandall Stewart 
3564f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
3565f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_AVAILABLE,
3566f8829a4aSRandall Stewart 			    (struct sockaddr *)&net->ro._l_addr, error);
3567f8829a4aSRandall Stewart 			break;
3568f8829a4aSRandall Stewart 		}
3569f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_CONFIRMED:
3570f8829a4aSRandall Stewart 		{
3571f8829a4aSRandall Stewart 			struct sctp_nets *net;
3572f8829a4aSRandall Stewart 
3573f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
3574f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_CONFIRMED,
3575f8829a4aSRandall Stewart 			    (struct sockaddr *)&net->ro._l_addr, error);
3576f8829a4aSRandall Stewart 			break;
3577f8829a4aSRandall Stewart 		}
3578f8829a4aSRandall Stewart 	case SCTP_NOTIFY_SPECIAL_SP_FAIL:
3579f8829a4aSRandall Stewart 		sctp_notify_send_failed2(stcb, error,
3580ceaad40aSRandall Stewart 		    (struct sctp_stream_queue_pending *)data, so_locked);
3581f8829a4aSRandall Stewart 		break;
35821edc9dbaSMichael Tuexen 	case SCTP_NOTIFY_SENT_DG_FAIL:
35831edc9dbaSMichael Tuexen 		sctp_notify_send_failed(stcb, 1, error,
35841edc9dbaSMichael Tuexen 		    (struct sctp_tmit_chunk *)data, so_locked);
35851edc9dbaSMichael Tuexen 		break;
35861edc9dbaSMichael Tuexen 	case SCTP_NOTIFY_UNSENT_DG_FAIL:
35871edc9dbaSMichael Tuexen 		sctp_notify_send_failed(stcb, 0, error,
3588ceaad40aSRandall Stewart 		    (struct sctp_tmit_chunk *)data, so_locked);
3589f8829a4aSRandall Stewart 		break;
3590f8829a4aSRandall Stewart 	case SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION:
35919a6142d8SRandall Stewart 		{
35929a6142d8SRandall Stewart 			uint32_t val;
35939a6142d8SRandall Stewart 
35949a6142d8SRandall Stewart 			val = *((uint32_t *) data);
35959a6142d8SRandall Stewart 
3596810ec536SMichael Tuexen 			sctp_notify_partial_delivery_indication(stcb, error, val, so_locked);
3597f8829a4aSRandall Stewart 			break;
3598810ec536SMichael Tuexen 		}
3599410a3b1eSMichael Tuexen 	case SCTP_NOTIFY_ASSOC_LOC_ABORTED:
3600fb4a67d2SMichael Tuexen 		if (((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_WAIT) ||
3601fb4a67d2SMichael Tuexen 		    ((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_ECHOED)) {
3602410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 0, so_locked);
3603c105859eSRandall Stewart 		} else {
3604410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 0, so_locked);
3605410a3b1eSMichael Tuexen 		}
3606410a3b1eSMichael Tuexen 		break;
3607410a3b1eSMichael Tuexen 	case SCTP_NOTIFY_ASSOC_REM_ABORTED:
3608fb4a67d2SMichael Tuexen 		if (((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_WAIT) ||
3609fb4a67d2SMichael Tuexen 		    ((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_ECHOED)) {
3610410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 1, so_locked);
3611410a3b1eSMichael Tuexen 		} else {
3612410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 1, so_locked);
3613c105859eSRandall Stewart 		}
3614f8829a4aSRandall Stewart 		break;
3615f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_RESTART:
3616410a3b1eSMichael Tuexen 		sctp_notify_assoc_change(SCTP_RESTART, stcb, error, NULL, 0, so_locked);
3617830d754dSRandall Stewart 		if (stcb->asoc.peer_supports_auth == 0) {
3618830d754dSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3619830d754dSRandall Stewart 			    NULL, so_locked);
3620830d754dSRandall Stewart 		}
3621f8829a4aSRandall Stewart 		break;
3622f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_SEND:
3623d7714577SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STREAM_RESET_OUTGOING_SSN);
3624f8829a4aSRandall Stewart 		break;
3625f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_RECV:
3626d7714577SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STREAM_RESET_INCOMING);
3627f8829a4aSRandall Stewart 		break;
3628f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_FAILED_OUT:
3629c4e848b7SRandall Stewart 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
3630d7714577SMichael Tuexen 		    (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_FAILED));
3631f8829a4aSRandall Stewart 		break;
3632d4260646SMichael Tuexen 	case SCTP_NOTIFY_STR_RESET_DENIED_OUT:
3633d4260646SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
3634d4260646SMichael Tuexen 		    (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_DENIED));
3635d4260646SMichael Tuexen 		break;
3636f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_FAILED_IN:
3637c4e848b7SRandall Stewart 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
3638d7714577SMichael Tuexen 		    (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_FAILED));
3639f8829a4aSRandall Stewart 		break;
3640d4260646SMichael Tuexen 	case SCTP_NOTIFY_STR_RESET_DENIED_IN:
3641d4260646SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
3642d4260646SMichael Tuexen 		    (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_DENIED));
3643d4260646SMichael Tuexen 		break;
3644f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_ADD_IP:
3645f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_ADDED, data,
3646f8829a4aSRandall Stewart 		    error);
3647f8829a4aSRandall Stewart 		break;
3648f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_DELETE_IP:
3649f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_REMOVED, data,
3650f8829a4aSRandall Stewart 		    error);
3651f8829a4aSRandall Stewart 		break;
3652f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_SET_PRIMARY:
3653f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_MADE_PRIM, data,
3654f8829a4aSRandall Stewart 		    error);
3655f8829a4aSRandall Stewart 		break;
3656f8829a4aSRandall Stewart 	case SCTP_NOTIFY_PEER_SHUTDOWN:
3657f8829a4aSRandall Stewart 		sctp_notify_shutdown_event(stcb);
3658f8829a4aSRandall Stewart 		break;
3659f8829a4aSRandall Stewart 	case SCTP_NOTIFY_AUTH_NEW_KEY:
366078f28045SMichael Tuexen 		sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY, error,
3661830d754dSRandall Stewart 		    (uint16_t) (uintptr_t) data,
3662830d754dSRandall Stewart 		    so_locked);
3663f8829a4aSRandall Stewart 		break;
3664830d754dSRandall Stewart 	case SCTP_NOTIFY_AUTH_FREE_KEY:
3665830d754dSRandall Stewart 		sctp_notify_authentication(stcb, SCTP_AUTH_FREE_KEY, error,
3666830d754dSRandall Stewart 		    (uint16_t) (uintptr_t) data,
3667830d754dSRandall Stewart 		    so_locked);
3668f8829a4aSRandall Stewart 		break;
3669830d754dSRandall Stewart 	case SCTP_NOTIFY_NO_PEER_AUTH:
3670830d754dSRandall Stewart 		sctp_notify_authentication(stcb, SCTP_AUTH_NO_AUTH, error,
3671830d754dSRandall Stewart 		    (uint16_t) (uintptr_t) data,
3672830d754dSRandall Stewart 		    so_locked);
3673830d754dSRandall Stewart 		break;
3674830d754dSRandall Stewart 	case SCTP_NOTIFY_SENDER_DRY:
3675830d754dSRandall Stewart 		sctp_notify_sender_dry_event(stcb, so_locked);
3676830d754dSRandall Stewart 		break;
3677389b1b11SMichael Tuexen 	case SCTP_NOTIFY_REMOTE_ERROR:
3678389b1b11SMichael Tuexen 		sctp_notify_remote_error(stcb, error, data);
3679389b1b11SMichael Tuexen 		break;
3680f8829a4aSRandall Stewart 	default:
3681ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_UTIL1, "%s: unknown notification %xh (%u)\n",
3682ad81507eSRandall Stewart 		    __FUNCTION__, notification, notification);
3683f8829a4aSRandall Stewart 		break;
3684f8829a4aSRandall Stewart 	}			/* end switch */
3685f8829a4aSRandall Stewart }
3686f8829a4aSRandall Stewart 
3687f8829a4aSRandall Stewart void
36881edc9dbaSMichael Tuexen sctp_report_all_outbound(struct sctp_tcb *stcb, uint16_t error, int holds_lock, int so_locked
3689ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3690ceaad40aSRandall Stewart     SCTP_UNUSED
3691ceaad40aSRandall Stewart #endif
3692ceaad40aSRandall Stewart )
3693f8829a4aSRandall Stewart {
3694f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3695f8829a4aSRandall Stewart 	struct sctp_stream_out *outs;
36964a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
36974a9ef3f8SMichael Tuexen 	struct sctp_stream_queue_pending *sp, *nsp;
36987f34832bSRandall Stewart 	int i;
3699f8829a4aSRandall Stewart 
3700ad81507eSRandall Stewart 	if (stcb == NULL) {
3701ad81507eSRandall Stewart 		return;
3702ad81507eSRandall Stewart 	}
37034a9ef3f8SMichael Tuexen 	asoc = &stcb->asoc;
37044a9ef3f8SMichael Tuexen 	if (asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3705478fbccbSRandall Stewart 		/* already being freed */
3706478fbccbSRandall Stewart 		return;
3707478fbccbSRandall Stewart 	}
3708f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3709f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
37104a9ef3f8SMichael Tuexen 	    (asoc->state & SCTP_STATE_CLOSED_SOCKET)) {
3711f8829a4aSRandall Stewart 		return;
3712f8829a4aSRandall Stewart 	}
3713f8829a4aSRandall Stewart 	/* now through all the gunk freeing chunks */
3714ad81507eSRandall Stewart 	if (holds_lock == 0) {
37157f34832bSRandall Stewart 		SCTP_TCB_SEND_LOCK(stcb);
3716ad81507eSRandall Stewart 	}
3717d00aff5dSRandall Stewart 	/* sent queue SHOULD be empty */
37184a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
3719d00aff5dSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3720d00aff5dSRandall Stewart 		asoc->sent_queue_cnt--;
3721325c8c46SMichael Tuexen 		if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
3722a7ad6026SMichael Tuexen 			if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) {
3723a7ad6026SMichael Tuexen 				asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--;
3724a7ad6026SMichael Tuexen #ifdef INVARIANTS
3725a7ad6026SMichael Tuexen 			} else {
3726a7ad6026SMichael Tuexen 				panic("No chunks on the queues for sid %u.", chk->rec.data.stream_number);
3727a7ad6026SMichael Tuexen #endif
3728a7ad6026SMichael Tuexen 			}
3729a7ad6026SMichael Tuexen 		}
37300c0982b8SRandall Stewart 		if (chk->data != NULL) {
3731d00aff5dSRandall Stewart 			sctp_free_bufspace(stcb, asoc, chk, 1);
37321edc9dbaSMichael Tuexen 			sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
37331edc9dbaSMichael Tuexen 			    error, chk, so_locked);
3734810ec536SMichael Tuexen 			if (chk->data) {
3735d00aff5dSRandall Stewart 				sctp_m_freem(chk->data);
3736d00aff5dSRandall Stewart 				chk->data = NULL;
3737d00aff5dSRandall Stewart 			}
3738810ec536SMichael Tuexen 		}
3739689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, so_locked);
3740d00aff5dSRandall Stewart 		/* sa_ignore FREED_MEMORY */
3741d00aff5dSRandall Stewart 	}
3742d00aff5dSRandall Stewart 	/* pending send queue SHOULD be empty */
37434a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
3744d00aff5dSRandall Stewart 		TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3745d00aff5dSRandall Stewart 		asoc->send_queue_cnt--;
3746a7ad6026SMichael Tuexen 		if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) {
3747a7ad6026SMichael Tuexen 			asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--;
3748a7ad6026SMichael Tuexen #ifdef INVARIANTS
3749a7ad6026SMichael Tuexen 		} else {
3750a7ad6026SMichael Tuexen 			panic("No chunks on the queues for sid %u.", chk->rec.data.stream_number);
3751a7ad6026SMichael Tuexen #endif
3752a7ad6026SMichael Tuexen 		}
37530c0982b8SRandall Stewart 		if (chk->data != NULL) {
3754d00aff5dSRandall Stewart 			sctp_free_bufspace(stcb, asoc, chk, 1);
37551edc9dbaSMichael Tuexen 			sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
37561edc9dbaSMichael Tuexen 			    error, chk, so_locked);
3757810ec536SMichael Tuexen 			if (chk->data) {
3758d00aff5dSRandall Stewart 				sctp_m_freem(chk->data);
3759d00aff5dSRandall Stewart 				chk->data = NULL;
3760d00aff5dSRandall Stewart 			}
3761810ec536SMichael Tuexen 		}
3762689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, so_locked);
3763d00aff5dSRandall Stewart 		/* sa_ignore FREED_MEMORY */
3764d00aff5dSRandall Stewart 	}
37654a9ef3f8SMichael Tuexen 	for (i = 0; i < asoc->streamoutcnt; i++) {
37667f34832bSRandall Stewart 		/* For each stream */
37674a9ef3f8SMichael Tuexen 		outs = &asoc->strmout[i];
37687f34832bSRandall Stewart 		/* clean up any sends there */
37694a9ef3f8SMichael Tuexen 		asoc->locked_on_sending = NULL;
37704a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
37714a9ef3f8SMichael Tuexen 			asoc->stream_queue_cnt--;
3772f8829a4aSRandall Stewart 			TAILQ_REMOVE(&outs->outqueue, sp, next);
3773f8829a4aSRandall Stewart 			sctp_free_spbufspace(stcb, asoc, sp);
3774478fbccbSRandall Stewart 			if (sp->data) {
3775f8829a4aSRandall Stewart 				sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
37761edc9dbaSMichael Tuexen 				    error, (void *)sp, so_locked);
3777f8829a4aSRandall Stewart 				if (sp->data) {
3778f8829a4aSRandall Stewart 					sctp_m_freem(sp->data);
3779f8829a4aSRandall Stewart 					sp->data = NULL;
3780d07b2ac6SMichael Tuexen 					sp->tail_mbuf = NULL;
3781d07b2ac6SMichael Tuexen 					sp->length = 0;
3782f8829a4aSRandall Stewart 				}
3783478fbccbSRandall Stewart 			}
37849eea4a2dSMichael Tuexen 			if (sp->net) {
3785f8829a4aSRandall Stewart 				sctp_free_remote_addr(sp->net);
3786f8829a4aSRandall Stewart 				sp->net = NULL;
37879eea4a2dSMichael Tuexen 			}
3788f8829a4aSRandall Stewart 			/* Free the chunk */
3789689e6a5fSMichael Tuexen 			sctp_free_a_strmoq(stcb, sp, so_locked);
37903c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
3791f8829a4aSRandall Stewart 		}
3792f8829a4aSRandall Stewart 	}
3793f8829a4aSRandall Stewart 
3794ad81507eSRandall Stewart 	if (holds_lock == 0) {
37957f34832bSRandall Stewart 		SCTP_TCB_SEND_UNLOCK(stcb);
3796f8829a4aSRandall Stewart 	}
3797ad81507eSRandall Stewart }
3798f8829a4aSRandall Stewart 
3799f8829a4aSRandall Stewart void
3800410a3b1eSMichael Tuexen sctp_abort_notification(struct sctp_tcb *stcb, uint8_t from_peer, uint16_t error,
3801a2b42326SMichael Tuexen     struct sctp_abort_chunk *abort, int so_locked
3802ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3803ceaad40aSRandall Stewart     SCTP_UNUSED
3804ceaad40aSRandall Stewart #endif
3805ceaad40aSRandall Stewart )
3806f8829a4aSRandall Stewart {
3807ad81507eSRandall Stewart 	if (stcb == NULL) {
3808ad81507eSRandall Stewart 		return;
3809ad81507eSRandall Stewart 	}
3810c55b70ceSMichael Tuexen 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3811c55b70ceSMichael Tuexen 	    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3812c55b70ceSMichael Tuexen 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
3813c55b70ceSMichael Tuexen 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_WAS_ABORTED;
3814c55b70ceSMichael Tuexen 	}
3815f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3816f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3817f8829a4aSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3818f8829a4aSRandall Stewart 		return;
3819f8829a4aSRandall Stewart 	}
3820f8829a4aSRandall Stewart 	/* Tell them we lost the asoc */
38211edc9dbaSMichael Tuexen 	sctp_report_all_outbound(stcb, error, 1, so_locked);
3822410a3b1eSMichael Tuexen 	if (from_peer) {
3823410a3b1eSMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_REM_ABORTED, stcb, error, abort, so_locked);
3824410a3b1eSMichael Tuexen 	} else {
3825410a3b1eSMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_LOC_ABORTED, stcb, error, abort, so_locked);
3826410a3b1eSMichael Tuexen 	}
3827f8829a4aSRandall Stewart }
3828f8829a4aSRandall Stewart 
3829f8829a4aSRandall Stewart void
3830f8829a4aSRandall Stewart sctp_abort_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3831b1754ad1SMichael Tuexen     struct mbuf *m, int iphlen,
3832b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
3833b1754ad1SMichael Tuexen     struct sctphdr *sh, struct mbuf *op_err,
3834f30ac432SMichael Tuexen     uint8_t use_mflowid, uint32_t mflowid,
3835c54a18d2SRandall Stewart     uint32_t vrf_id, uint16_t port)
3836f8829a4aSRandall Stewart {
3837f8829a4aSRandall Stewart 	uint32_t vtag;
3838f8829a4aSRandall Stewart 
3839ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3840ceaad40aSRandall Stewart 	struct socket *so;
3841ceaad40aSRandall Stewart 
3842ceaad40aSRandall Stewart #endif
3843ceaad40aSRandall Stewart 
3844f8829a4aSRandall Stewart 	vtag = 0;
3845f8829a4aSRandall Stewart 	if (stcb != NULL) {
3846f8829a4aSRandall Stewart 		/* We have a TCB to abort, send notification too */
3847f8829a4aSRandall Stewart 		vtag = stcb->asoc.peer_vtag;
3848410a3b1eSMichael Tuexen 		sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
384917205eccSRandall Stewart 		/* get the assoc vrf id and table id */
385017205eccSRandall Stewart 		vrf_id = stcb->asoc.vrf_id;
385163981c2bSRandall Stewart 		stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
3852f8829a4aSRandall Stewart 	}
3853b1754ad1SMichael Tuexen 	sctp_send_abort(m, iphlen, src, dst, sh, vtag, op_err,
3854f30ac432SMichael Tuexen 	    use_mflowid, mflowid,
3855f30ac432SMichael Tuexen 	    vrf_id, port);
3856f8829a4aSRandall Stewart 	if (stcb != NULL) {
3857f8829a4aSRandall Stewart 		/* Ok, now lets free it */
3858ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3859ceaad40aSRandall Stewart 		so = SCTP_INP_SO(inp);
3860ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
3861ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3862ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
3863ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
3864ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
3865ceaad40aSRandall Stewart #endif
38660271d0cdSMichael Tuexen 		SCTP_STAT_INCR_COUNTER32(sctps_aborted);
38670271d0cdSMichael Tuexen 		if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
38680271d0cdSMichael Tuexen 		    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
38690271d0cdSMichael Tuexen 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
38700271d0cdSMichael Tuexen 		}
3871c4739e2fSRandall Stewart 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_4);
3872ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3873ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
3874ceaad40aSRandall Stewart #endif
3875f8829a4aSRandall Stewart 	}
3876f8829a4aSRandall Stewart }
3877f8829a4aSRandall Stewart 
3878f1f73e57SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
3879f1f73e57SRandall Stewart void
3880f1f73e57SRandall Stewart sctp_print_out_track_log(struct sctp_tcb *stcb)
3881f1f73e57SRandall Stewart {
388218e198d3SRandall Stewart #ifdef NOSIY_PRINTS
3883f1f73e57SRandall Stewart 	int i;
3884f1f73e57SRandall Stewart 
3885ad81507eSRandall Stewart 	SCTP_PRINTF("Last ep reason:%x\n", stcb->sctp_ep->last_abort_code);
3886ad81507eSRandall Stewart 	SCTP_PRINTF("IN bound TSN log-aaa\n");
3887f1f73e57SRandall Stewart 	if ((stcb->asoc.tsn_in_at == 0) && (stcb->asoc.tsn_in_wrapped == 0)) {
3888ad81507eSRandall Stewart 		SCTP_PRINTF("None rcvd\n");
3889f1f73e57SRandall Stewart 		goto none_in;
3890f1f73e57SRandall Stewart 	}
3891f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_in_wrapped) {
3892f1f73e57SRandall Stewart 		for (i = stcb->asoc.tsn_in_at; i < SCTP_TSN_LOG_SIZE; i++) {
3893ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3894f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].tsn,
3895f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].strm,
3896f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].seq,
3897f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].flgs,
3898f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].sz);
3899f1f73e57SRandall Stewart 		}
3900f1f73e57SRandall Stewart 	}
3901f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_in_at) {
3902f1f73e57SRandall Stewart 		for (i = 0; i < stcb->asoc.tsn_in_at; i++) {
3903ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3904f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].tsn,
3905f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].strm,
3906f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].seq,
3907f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].flgs,
3908f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].sz);
3909f1f73e57SRandall Stewart 		}
3910f1f73e57SRandall Stewart 	}
3911f1f73e57SRandall Stewart none_in:
3912ad81507eSRandall Stewart 	SCTP_PRINTF("OUT bound TSN log-aaa\n");
3913ad81507eSRandall Stewart 	if ((stcb->asoc.tsn_out_at == 0) &&
3914ad81507eSRandall Stewart 	    (stcb->asoc.tsn_out_wrapped == 0)) {
3915ad81507eSRandall Stewart 		SCTP_PRINTF("None sent\n");
3916f1f73e57SRandall Stewart 	}
3917f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_out_wrapped) {
3918f1f73e57SRandall Stewart 		for (i = stcb->asoc.tsn_out_at; i < SCTP_TSN_LOG_SIZE; i++) {
3919ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3920f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].tsn,
3921f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].strm,
3922f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].seq,
3923f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].flgs,
3924f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].sz);
3925f1f73e57SRandall Stewart 		}
3926f1f73e57SRandall Stewart 	}
3927f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_out_at) {
3928f1f73e57SRandall Stewart 		for (i = 0; i < stcb->asoc.tsn_out_at; i++) {
3929ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3930f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].tsn,
3931f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].strm,
3932f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].seq,
3933f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].flgs,
3934f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].sz);
3935f1f73e57SRandall Stewart 		}
3936f1f73e57SRandall Stewart 	}
393718e198d3SRandall Stewart #endif
3938f1f73e57SRandall Stewart }
3939f1f73e57SRandall Stewart 
3940f1f73e57SRandall Stewart #endif
3941f1f73e57SRandall Stewart 
3942f8829a4aSRandall Stewart void
3943f8829a4aSRandall Stewart sctp_abort_an_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3944a2b42326SMichael Tuexen     struct mbuf *op_err,
3945ceaad40aSRandall Stewart     int so_locked
3946ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3947ceaad40aSRandall Stewart     SCTP_UNUSED
3948ceaad40aSRandall Stewart #endif
3949ceaad40aSRandall Stewart )
3950f8829a4aSRandall Stewart {
3951ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3952ceaad40aSRandall Stewart 	struct socket *so;
3953ceaad40aSRandall Stewart 
3954ceaad40aSRandall Stewart #endif
3955ceaad40aSRandall Stewart 
3956ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3957ceaad40aSRandall Stewart 	so = SCTP_INP_SO(inp);
3958ceaad40aSRandall Stewart #endif
3959f8829a4aSRandall Stewart 	if (stcb == NULL) {
3960f8829a4aSRandall Stewart 		/* Got to have a TCB */
3961f8829a4aSRandall Stewart 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3962fe1831e0SMichael Tuexen 			if (LIST_EMPTY(&inp->sctp_asoc_list)) {
3963b0552ae2SRandall Stewart 				sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
3964b0552ae2SRandall Stewart 				    SCTP_CALLED_DIRECTLY_NOCMPSET);
3965f8829a4aSRandall Stewart 			}
3966f8829a4aSRandall Stewart 		}
3967f8829a4aSRandall Stewart 		return;
396863981c2bSRandall Stewart 	} else {
396963981c2bSRandall Stewart 		stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
3970f8829a4aSRandall Stewart 	}
3971f8829a4aSRandall Stewart 	/* notify the ulp */
3972a2b42326SMichael Tuexen 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
3973410a3b1eSMichael Tuexen 		sctp_abort_notification(stcb, 0, 0, NULL, so_locked);
3974a2b42326SMichael Tuexen 	}
3975f8829a4aSRandall Stewart 	/* notify the peer */
3976ceaad40aSRandall Stewart 	sctp_send_abort_tcb(stcb, op_err, so_locked);
3977f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3978f8829a4aSRandall Stewart 	if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
3979f8829a4aSRandall Stewart 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3980f8829a4aSRandall Stewart 		SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3981f8829a4aSRandall Stewart 	}
3982f8829a4aSRandall Stewart 	/* now free the asoc */
3983f1f73e57SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
3984f1f73e57SRandall Stewart 	sctp_print_out_track_log(stcb);
3985f1f73e57SRandall Stewart #endif
3986ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3987ceaad40aSRandall Stewart 	if (!so_locked) {
3988ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
3989ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3990ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
3991ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
3992ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
3993ceaad40aSRandall Stewart 	}
3994ceaad40aSRandall Stewart #endif
3995c4739e2fSRandall Stewart 	(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_5);
3996ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3997ceaad40aSRandall Stewart 	if (!so_locked) {
3998ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
3999ceaad40aSRandall Stewart 	}
4000ceaad40aSRandall Stewart #endif
4001f8829a4aSRandall Stewart }
4002f8829a4aSRandall Stewart 
4003f8829a4aSRandall Stewart void
4004b1754ad1SMichael Tuexen sctp_handle_ootb(struct mbuf *m, int iphlen, int offset,
4005b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
4006b1754ad1SMichael Tuexen     struct sctphdr *sh, struct sctp_inpcb *inp,
4007ff1ffd74SMichael Tuexen     struct mbuf *cause,
4008f30ac432SMichael Tuexen     uint8_t use_mflowid, uint32_t mflowid,
4009f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
4010f8829a4aSRandall Stewart {
4011f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch, chunk_buf;
4012f8829a4aSRandall Stewart 	unsigned int chk_length;
4013c58e60beSMichael Tuexen 	int contains_init_chunk;
4014f8829a4aSRandall Stewart 
4015f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue);
4016f8829a4aSRandall Stewart 	/* Generate a TO address for future reference */
4017f8829a4aSRandall Stewart 	if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4018fe1831e0SMichael Tuexen 		if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4019b0552ae2SRandall Stewart 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4020b0552ae2SRandall Stewart 			    SCTP_CALLED_DIRECTLY_NOCMPSET);
4021f8829a4aSRandall Stewart 		}
4022f8829a4aSRandall Stewart 	}
4023c58e60beSMichael Tuexen 	contains_init_chunk = 0;
4024f8829a4aSRandall Stewart 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4025f8829a4aSRandall Stewart 	    sizeof(*ch), (uint8_t *) & chunk_buf);
4026f8829a4aSRandall Stewart 	while (ch != NULL) {
4027f8829a4aSRandall Stewart 		chk_length = ntohs(ch->chunk_length);
4028f8829a4aSRandall Stewart 		if (chk_length < sizeof(*ch)) {
4029f8829a4aSRandall Stewart 			/* break to abort land */
4030f8829a4aSRandall Stewart 			break;
4031f8829a4aSRandall Stewart 		}
4032f8829a4aSRandall Stewart 		switch (ch->chunk_type) {
4033c58e60beSMichael Tuexen 		case SCTP_INIT:
4034c58e60beSMichael Tuexen 			contains_init_chunk = 1;
4035c58e60beSMichael Tuexen 			break;
4036f8829a4aSRandall Stewart 		case SCTP_PACKET_DROPPED:
4037f8829a4aSRandall Stewart 			/* we don't respond to pkt-dropped */
4038f8829a4aSRandall Stewart 			return;
4039f8829a4aSRandall Stewart 		case SCTP_ABORT_ASSOCIATION:
4040f8829a4aSRandall Stewart 			/* we don't respond with an ABORT to an ABORT */
4041f8829a4aSRandall Stewart 			return;
4042f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_COMPLETE:
4043f8829a4aSRandall Stewart 			/*
4044f8829a4aSRandall Stewart 			 * we ignore it since we are not waiting for it and
4045f8829a4aSRandall Stewart 			 * peer is gone
4046f8829a4aSRandall Stewart 			 */
4047f8829a4aSRandall Stewart 			return;
4048f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_ACK:
4049b1754ad1SMichael Tuexen 			sctp_send_shutdown_complete2(src, dst, sh,
4050f30ac432SMichael Tuexen 			    use_mflowid, mflowid,
4051f30ac432SMichael Tuexen 			    vrf_id, port);
4052f8829a4aSRandall Stewart 			return;
4053f8829a4aSRandall Stewart 		default:
4054f8829a4aSRandall Stewart 			break;
4055f8829a4aSRandall Stewart 		}
4056f8829a4aSRandall Stewart 		offset += SCTP_SIZE32(chk_length);
4057f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4058f8829a4aSRandall Stewart 		    sizeof(*ch), (uint8_t *) & chunk_buf);
4059f8829a4aSRandall Stewart 	}
4060c58e60beSMichael Tuexen 	if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
4061c58e60beSMichael Tuexen 	    ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
4062c58e60beSMichael Tuexen 	    (contains_init_chunk == 0))) {
4063ff1ffd74SMichael Tuexen 		sctp_send_abort(m, iphlen, src, dst, sh, 0, cause,
4064f30ac432SMichael Tuexen 		    use_mflowid, mflowid,
4065f30ac432SMichael Tuexen 		    vrf_id, port);
4066f8829a4aSRandall Stewart 	}
4067c58e60beSMichael Tuexen }
4068f8829a4aSRandall Stewart 
4069f8829a4aSRandall Stewart /*
4070f8829a4aSRandall Stewart  * check the inbound datagram to make sure there is not an abort inside it,
4071f8829a4aSRandall Stewart  * if there is return 1, else return 0.
4072f8829a4aSRandall Stewart  */
4073f8829a4aSRandall Stewart int
4074f8829a4aSRandall Stewart sctp_is_there_an_abort_here(struct mbuf *m, int iphlen, uint32_t * vtagfill)
4075f8829a4aSRandall Stewart {
4076f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
4077f8829a4aSRandall Stewart 	struct sctp_init_chunk *init_chk, chunk_buf;
4078f8829a4aSRandall Stewart 	int offset;
4079f8829a4aSRandall Stewart 	unsigned int chk_length;
4080f8829a4aSRandall Stewart 
4081f8829a4aSRandall Stewart 	offset = iphlen + sizeof(struct sctphdr);
4082f8829a4aSRandall Stewart 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch),
4083f8829a4aSRandall Stewart 	    (uint8_t *) & chunk_buf);
4084f8829a4aSRandall Stewart 	while (ch != NULL) {
4085f8829a4aSRandall Stewart 		chk_length = ntohs(ch->chunk_length);
4086f8829a4aSRandall Stewart 		if (chk_length < sizeof(*ch)) {
4087f8829a4aSRandall Stewart 			/* packet is probably corrupt */
4088f8829a4aSRandall Stewart 			break;
4089f8829a4aSRandall Stewart 		}
4090f8829a4aSRandall Stewart 		/* we seem to be ok, is it an abort? */
4091f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) {
4092f8829a4aSRandall Stewart 			/* yep, tell them */
4093f8829a4aSRandall Stewart 			return (1);
4094f8829a4aSRandall Stewart 		}
4095f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_INITIATION) {
4096f8829a4aSRandall Stewart 			/* need to update the Vtag */
4097f8829a4aSRandall Stewart 			init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4098f8829a4aSRandall Stewart 			    offset, sizeof(*init_chk), (uint8_t *) & chunk_buf);
4099f8829a4aSRandall Stewart 			if (init_chk != NULL) {
4100f8829a4aSRandall Stewart 				*vtagfill = ntohl(init_chk->init.initiate_tag);
4101f8829a4aSRandall Stewart 			}
4102f8829a4aSRandall Stewart 		}
4103f8829a4aSRandall Stewart 		/* Nope, move to the next chunk */
4104f8829a4aSRandall Stewart 		offset += SCTP_SIZE32(chk_length);
4105f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4106f8829a4aSRandall Stewart 		    sizeof(*ch), (uint8_t *) & chunk_buf);
4107f8829a4aSRandall Stewart 	}
4108f8829a4aSRandall Stewart 	return (0);
4109f8829a4aSRandall Stewart }
4110f8829a4aSRandall Stewart 
4111f8829a4aSRandall Stewart /*
4112f8829a4aSRandall Stewart  * currently (2/02), ifa_addr embeds scope_id's and don't have sin6_scope_id
4113f8829a4aSRandall Stewart  * set (i.e. it's 0) so, create this function to compare link local scopes
4114f8829a4aSRandall Stewart  */
41155e2c2d87SRandall Stewart #ifdef INET6
4116f8829a4aSRandall Stewart uint32_t
4117f8829a4aSRandall Stewart sctp_is_same_scope(struct sockaddr_in6 *addr1, struct sockaddr_in6 *addr2)
4118f8829a4aSRandall Stewart {
4119f8829a4aSRandall Stewart 	struct sockaddr_in6 a, b;
4120f8829a4aSRandall Stewart 
4121f8829a4aSRandall Stewart 	/* save copies */
4122f8829a4aSRandall Stewart 	a = *addr1;
4123f8829a4aSRandall Stewart 	b = *addr2;
4124f8829a4aSRandall Stewart 
4125f8829a4aSRandall Stewart 	if (a.sin6_scope_id == 0)
4126f8829a4aSRandall Stewart 		if (sa6_recoverscope(&a)) {
4127f8829a4aSRandall Stewart 			/* can't get scope, so can't match */
4128f8829a4aSRandall Stewart 			return (0);
4129f8829a4aSRandall Stewart 		}
4130f8829a4aSRandall Stewart 	if (b.sin6_scope_id == 0)
4131f8829a4aSRandall Stewart 		if (sa6_recoverscope(&b)) {
4132f8829a4aSRandall Stewart 			/* can't get scope, so can't match */
4133f8829a4aSRandall Stewart 			return (0);
4134f8829a4aSRandall Stewart 		}
4135f8829a4aSRandall Stewart 	if (a.sin6_scope_id != b.sin6_scope_id)
4136f8829a4aSRandall Stewart 		return (0);
4137f8829a4aSRandall Stewart 
4138f8829a4aSRandall Stewart 	return (1);
4139f8829a4aSRandall Stewart }
4140f8829a4aSRandall Stewart 
4141f8829a4aSRandall Stewart /*
4142f8829a4aSRandall Stewart  * returns a sockaddr_in6 with embedded scope recovered and removed
4143f8829a4aSRandall Stewart  */
4144f8829a4aSRandall Stewart struct sockaddr_in6 *
4145f8829a4aSRandall Stewart sctp_recover_scope(struct sockaddr_in6 *addr, struct sockaddr_in6 *store)
4146f8829a4aSRandall Stewart {
4147f8829a4aSRandall Stewart 	/* check and strip embedded scope junk */
4148f8829a4aSRandall Stewart 	if (addr->sin6_family == AF_INET6) {
4149f8829a4aSRandall Stewart 		if (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr)) {
4150f8829a4aSRandall Stewart 			if (addr->sin6_scope_id == 0) {
4151f8829a4aSRandall Stewart 				*store = *addr;
4152f8829a4aSRandall Stewart 				if (!sa6_recoverscope(store)) {
4153f8829a4aSRandall Stewart 					/* use the recovered scope */
4154f8829a4aSRandall Stewart 					addr = store;
4155f8829a4aSRandall Stewart 				}
4156f42a358aSRandall Stewart 			} else {
4157f8829a4aSRandall Stewart 				/* else, return the original "to" addr */
4158f42a358aSRandall Stewart 				in6_clearscope(&addr->sin6_addr);
4159f8829a4aSRandall Stewart 			}
4160f8829a4aSRandall Stewart 		}
4161f8829a4aSRandall Stewart 	}
4162f8829a4aSRandall Stewart 	return (addr);
4163f8829a4aSRandall Stewart }
4164f8829a4aSRandall Stewart 
41655e2c2d87SRandall Stewart #endif
41665e2c2d87SRandall Stewart 
4167f8829a4aSRandall Stewart /*
4168f8829a4aSRandall Stewart  * are the two addresses the same?  currently a "scopeless" check returns: 1
4169f8829a4aSRandall Stewart  * if same, 0 if not
4170f8829a4aSRandall Stewart  */
417172fb6fdbSRandall Stewart int
4172f8829a4aSRandall Stewart sctp_cmpaddr(struct sockaddr *sa1, struct sockaddr *sa2)
4173f8829a4aSRandall Stewart {
4174f8829a4aSRandall Stewart 
4175f8829a4aSRandall Stewart 	/* must be valid */
4176f8829a4aSRandall Stewart 	if (sa1 == NULL || sa2 == NULL)
4177f8829a4aSRandall Stewart 		return (0);
4178f8829a4aSRandall Stewart 
4179f8829a4aSRandall Stewart 	/* must be the same family */
4180f8829a4aSRandall Stewart 	if (sa1->sa_family != sa2->sa_family)
4181f8829a4aSRandall Stewart 		return (0);
4182f8829a4aSRandall Stewart 
41835e2c2d87SRandall Stewart 	switch (sa1->sa_family) {
41845e2c2d87SRandall Stewart #ifdef INET6
41855e2c2d87SRandall Stewart 	case AF_INET6:
41865e2c2d87SRandall Stewart 		{
4187f8829a4aSRandall Stewart 			/* IPv6 addresses */
4188f8829a4aSRandall Stewart 			struct sockaddr_in6 *sin6_1, *sin6_2;
4189f8829a4aSRandall Stewart 
4190f8829a4aSRandall Stewart 			sin6_1 = (struct sockaddr_in6 *)sa1;
4191f8829a4aSRandall Stewart 			sin6_2 = (struct sockaddr_in6 *)sa2;
4192c54a18d2SRandall Stewart 			return (SCTP6_ARE_ADDR_EQUAL(sin6_1,
4193c54a18d2SRandall Stewart 			    sin6_2));
41945e2c2d87SRandall Stewart 		}
41955e2c2d87SRandall Stewart #endif
4196ea5eba11SMichael Tuexen #ifdef INET
41975e2c2d87SRandall Stewart 	case AF_INET:
41985e2c2d87SRandall Stewart 		{
4199f8829a4aSRandall Stewart 			/* IPv4 addresses */
4200f8829a4aSRandall Stewart 			struct sockaddr_in *sin_1, *sin_2;
4201f8829a4aSRandall Stewart 
4202f8829a4aSRandall Stewart 			sin_1 = (struct sockaddr_in *)sa1;
4203f8829a4aSRandall Stewart 			sin_2 = (struct sockaddr_in *)sa2;
4204f8829a4aSRandall Stewart 			return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr);
42055e2c2d87SRandall Stewart 		}
4206ea5eba11SMichael Tuexen #endif
42075e2c2d87SRandall Stewart 	default:
4208f8829a4aSRandall Stewart 		/* we don't do these... */
4209f8829a4aSRandall Stewart 		return (0);
4210f8829a4aSRandall Stewart 	}
4211f8829a4aSRandall Stewart }
4212f8829a4aSRandall Stewart 
4213f8829a4aSRandall Stewart void
4214f8829a4aSRandall Stewart sctp_print_address(struct sockaddr *sa)
4215f8829a4aSRandall Stewart {
42165e2c2d87SRandall Stewart #ifdef INET6
42177d32aa0cSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
4218f8829a4aSRandall Stewart 
42195e2c2d87SRandall Stewart #endif
42205e2c2d87SRandall Stewart 
42215e2c2d87SRandall Stewart 	switch (sa->sa_family) {
42225e2c2d87SRandall Stewart #ifdef INET6
42235e2c2d87SRandall Stewart 	case AF_INET6:
42245e2c2d87SRandall Stewart 		{
4225ad81507eSRandall Stewart 			struct sockaddr_in6 *sin6;
4226ad81507eSRandall Stewart 
4227f8829a4aSRandall Stewart 			sin6 = (struct sockaddr_in6 *)sa;
4228ad81507eSRandall Stewart 			SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n",
42297d32aa0cSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &sin6->sin6_addr),
42307d32aa0cSBjoern A. Zeeb 			    ntohs(sin6->sin6_port),
4231f8829a4aSRandall Stewart 			    sin6->sin6_scope_id);
42325e2c2d87SRandall Stewart 			break;
42335e2c2d87SRandall Stewart 		}
42345e2c2d87SRandall Stewart #endif
4235ea5eba11SMichael Tuexen #ifdef INET
42365e2c2d87SRandall Stewart 	case AF_INET:
42375e2c2d87SRandall Stewart 		{
4238f8829a4aSRandall Stewart 			struct sockaddr_in *sin;
4239f8829a4aSRandall Stewart 			unsigned char *p;
4240f8829a4aSRandall Stewart 
4241f8829a4aSRandall Stewart 			sin = (struct sockaddr_in *)sa;
4242f8829a4aSRandall Stewart 			p = (unsigned char *)&sin->sin_addr;
4243ad81507eSRandall Stewart 			SCTP_PRINTF("IPv4 address: %u.%u.%u.%u:%d\n",
4244f8829a4aSRandall Stewart 			    p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
42455e2c2d87SRandall Stewart 			break;
42465e2c2d87SRandall Stewart 		}
4247ea5eba11SMichael Tuexen #endif
42485e2c2d87SRandall Stewart 	default:
4249ad81507eSRandall Stewart 		SCTP_PRINTF("?\n");
42505e2c2d87SRandall Stewart 		break;
4251f8829a4aSRandall Stewart 	}
4252f8829a4aSRandall Stewart }
4253f8829a4aSRandall Stewart 
4254f8829a4aSRandall Stewart void
4255f8829a4aSRandall Stewart sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
4256f8829a4aSRandall Stewart     struct sctp_inpcb *new_inp,
4257d06c82f1SRandall Stewart     struct sctp_tcb *stcb,
4258d06c82f1SRandall Stewart     int waitflags)
4259f8829a4aSRandall Stewart {
4260f8829a4aSRandall Stewart 	/*
4261f8829a4aSRandall Stewart 	 * go through our old INP and pull off any control structures that
4262f8829a4aSRandall Stewart 	 * belong to stcb and move then to the new inp.
4263f8829a4aSRandall Stewart 	 */
4264f8829a4aSRandall Stewart 	struct socket *old_so, *new_so;
4265f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control, *nctl;
4266f8829a4aSRandall Stewart 	struct sctp_readhead tmp_queue;
4267f8829a4aSRandall Stewart 	struct mbuf *m;
4268bff64a4dSRandall Stewart 	int error = 0;
4269f8829a4aSRandall Stewart 
4270f8829a4aSRandall Stewart 	old_so = old_inp->sctp_socket;
4271f8829a4aSRandall Stewart 	new_so = new_inp->sctp_socket;
4272f8829a4aSRandall Stewart 	TAILQ_INIT(&tmp_queue);
4273d06c82f1SRandall Stewart 	error = sblock(&old_so->so_rcv, waitflags);
4274f8829a4aSRandall Stewart 	if (error) {
4275f8829a4aSRandall Stewart 		/*
4276f8829a4aSRandall Stewart 		 * Gak, can't get sblock, we have a problem. data will be
4277f8829a4aSRandall Stewart 		 * left stranded.. and we don't dare look at it since the
4278f8829a4aSRandall Stewart 		 * other thread may be reading something. Oh well, its a
4279f8829a4aSRandall Stewart 		 * screwed up app that does a peeloff OR a accept while
4280f8829a4aSRandall Stewart 		 * reading from the main socket... actually its only the
4281f8829a4aSRandall Stewart 		 * peeloff() case, since I think read will fail on a
4282f8829a4aSRandall Stewart 		 * listening socket..
4283f8829a4aSRandall Stewart 		 */
4284f8829a4aSRandall Stewart 		return;
4285f8829a4aSRandall Stewart 	}
4286f8829a4aSRandall Stewart 	/* lock the socket buffers */
4287f8829a4aSRandall Stewart 	SCTP_INP_READ_LOCK(old_inp);
42884a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(control, &old_inp->read_queue, next, nctl) {
4289f8829a4aSRandall Stewart 		/* Pull off all for out target stcb */
4290f8829a4aSRandall Stewart 		if (control->stcb == stcb) {
4291f8829a4aSRandall Stewart 			/* remove it we want it */
4292f8829a4aSRandall Stewart 			TAILQ_REMOVE(&old_inp->read_queue, control, next);
4293f8829a4aSRandall Stewart 			TAILQ_INSERT_TAIL(&tmp_queue, control, next);
4294f8829a4aSRandall Stewart 			m = control->data;
4295f8829a4aSRandall Stewart 			while (m) {
4296b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4297139bc87fSRandall Stewart 					sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
429880fefe0aSRandall Stewart 				}
4299f8829a4aSRandall Stewart 				sctp_sbfree(control, stcb, &old_so->so_rcv, m);
4300b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4301f8829a4aSRandall Stewart 					sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
430280fefe0aSRandall Stewart 				}
4303139bc87fSRandall Stewart 				m = SCTP_BUF_NEXT(m);
4304f8829a4aSRandall Stewart 			}
4305f8829a4aSRandall Stewart 		}
4306f8829a4aSRandall Stewart 	}
4307f8829a4aSRandall Stewart 	SCTP_INP_READ_UNLOCK(old_inp);
4308f8829a4aSRandall Stewart 	/* Remove the sb-lock on the old socket */
4309f8829a4aSRandall Stewart 
4310f8829a4aSRandall Stewart 	sbunlock(&old_so->so_rcv);
4311f8829a4aSRandall Stewart 	/* Now we move them over to the new socket buffer */
4312f8829a4aSRandall Stewart 	SCTP_INP_READ_LOCK(new_inp);
43134a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(control, &tmp_queue, next, nctl) {
4314f8829a4aSRandall Stewart 		TAILQ_INSERT_TAIL(&new_inp->read_queue, control, next);
4315f8829a4aSRandall Stewart 		m = control->data;
4316f8829a4aSRandall Stewart 		while (m) {
4317b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4318139bc87fSRandall Stewart 				sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
431980fefe0aSRandall Stewart 			}
4320f8829a4aSRandall Stewart 			sctp_sballoc(stcb, &new_so->so_rcv, m);
4321b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4322f8829a4aSRandall Stewart 				sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
432380fefe0aSRandall Stewart 			}
4324139bc87fSRandall Stewart 			m = SCTP_BUF_NEXT(m);
4325f8829a4aSRandall Stewart 		}
4326f8829a4aSRandall Stewart 	}
4327f8829a4aSRandall Stewart 	SCTP_INP_READ_UNLOCK(new_inp);
4328f8829a4aSRandall Stewart }
4329f8829a4aSRandall Stewart 
4330f8829a4aSRandall Stewart void
4331f8829a4aSRandall Stewart sctp_add_to_readq(struct sctp_inpcb *inp,
4332f8829a4aSRandall Stewart     struct sctp_tcb *stcb,
4333f8829a4aSRandall Stewart     struct sctp_queued_to_read *control,
4334f8829a4aSRandall Stewart     struct sockbuf *sb,
4335ceaad40aSRandall Stewart     int end,
4336cfde3ff7SRandall Stewart     int inp_read_lock_held,
4337ceaad40aSRandall Stewart     int so_locked
4338ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4339ceaad40aSRandall Stewart     SCTP_UNUSED
4340ceaad40aSRandall Stewart #endif
4341ceaad40aSRandall Stewart )
4342f8829a4aSRandall Stewart {
4343f8829a4aSRandall Stewart 	/*
4344f8829a4aSRandall Stewart 	 * Here we must place the control on the end of the socket read
4345f8829a4aSRandall Stewart 	 * queue AND increment sb_cc so that select will work properly on
4346f8829a4aSRandall Stewart 	 * read.
4347f8829a4aSRandall Stewart 	 */
4348f8829a4aSRandall Stewart 	struct mbuf *m, *prev = NULL;
4349f8829a4aSRandall Stewart 
435003b0b021SRandall Stewart 	if (inp == NULL) {
435103b0b021SRandall Stewart 		/* Gak, TSNH!! */
4352a5d547adSRandall Stewart #ifdef INVARIANTS
435303b0b021SRandall Stewart 		panic("Gak, inp NULL on add_to_readq");
435403b0b021SRandall Stewart #endif
435503b0b021SRandall Stewart 		return;
435603b0b021SRandall Stewart 	}
4357cfde3ff7SRandall Stewart 	if (inp_read_lock_held == 0)
4358f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(inp);
4359cd1386abSMichael Tuexen 	if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
4360cd1386abSMichael Tuexen 		sctp_free_remote_addr(control->whoFrom);
4361cd1386abSMichael Tuexen 		if (control->data) {
4362cd1386abSMichael Tuexen 			sctp_m_freem(control->data);
4363cd1386abSMichael Tuexen 			control->data = NULL;
4364cd1386abSMichael Tuexen 		}
4365cd1386abSMichael Tuexen 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), control);
4366cd1386abSMichael Tuexen 		if (inp_read_lock_held == 0)
4367cd1386abSMichael Tuexen 			SCTP_INP_READ_UNLOCK(inp);
4368cd1386abSMichael Tuexen 		return;
4369cd1386abSMichael Tuexen 	}
437042551e99SRandall Stewart 	if (!(control->spec_flags & M_NOTIFICATION)) {
4371a5d547adSRandall Stewart 		atomic_add_int(&inp->total_recvs, 1);
437242551e99SRandall Stewart 		if (!control->do_not_ref_stcb) {
4373a5d547adSRandall Stewart 			atomic_add_int(&stcb->total_recvs, 1);
437442551e99SRandall Stewart 		}
437542551e99SRandall Stewart 	}
4376f8829a4aSRandall Stewart 	m = control->data;
4377f8829a4aSRandall Stewart 	control->held_length = 0;
4378f8829a4aSRandall Stewart 	control->length = 0;
4379f8829a4aSRandall Stewart 	while (m) {
4380139bc87fSRandall Stewart 		if (SCTP_BUF_LEN(m) == 0) {
4381f8829a4aSRandall Stewart 			/* Skip mbufs with NO length */
4382f8829a4aSRandall Stewart 			if (prev == NULL) {
4383f8829a4aSRandall Stewart 				/* First one */
4384f8829a4aSRandall Stewart 				control->data = sctp_m_free(m);
4385f8829a4aSRandall Stewart 				m = control->data;
4386f8829a4aSRandall Stewart 			} else {
4387139bc87fSRandall Stewart 				SCTP_BUF_NEXT(prev) = sctp_m_free(m);
4388139bc87fSRandall Stewart 				m = SCTP_BUF_NEXT(prev);
4389f8829a4aSRandall Stewart 			}
4390f8829a4aSRandall Stewart 			if (m == NULL) {
4391c2ede4b3SMartin Blapp 				control->tail_mbuf = prev;
4392f8829a4aSRandall Stewart 			}
4393f8829a4aSRandall Stewart 			continue;
4394f8829a4aSRandall Stewart 		}
4395f8829a4aSRandall Stewart 		prev = m;
4396b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4397139bc87fSRandall Stewart 			sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
439880fefe0aSRandall Stewart 		}
4399f8829a4aSRandall Stewart 		sctp_sballoc(stcb, sb, m);
4400b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4401f8829a4aSRandall Stewart 			sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
440280fefe0aSRandall Stewart 		}
4403139bc87fSRandall Stewart 		atomic_add_int(&control->length, SCTP_BUF_LEN(m));
4404139bc87fSRandall Stewart 		m = SCTP_BUF_NEXT(m);
4405f8829a4aSRandall Stewart 	}
4406f8829a4aSRandall Stewart 	if (prev != NULL) {
4407f8829a4aSRandall Stewart 		control->tail_mbuf = prev;
4408f8829a4aSRandall Stewart 	} else {
4409139bc87fSRandall Stewart 		/* Everything got collapsed out?? */
4410cd1386abSMichael Tuexen 		sctp_free_remote_addr(control->whoFrom);
4411cd1386abSMichael Tuexen 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), control);
4412cfde3ff7SRandall Stewart 		if (inp_read_lock_held == 0)
441347a490cbSMichael Tuexen 			SCTP_INP_READ_UNLOCK(inp);
4414f8829a4aSRandall Stewart 		return;
4415f8829a4aSRandall Stewart 	}
4416f8829a4aSRandall Stewart 	if (end) {
4417f8829a4aSRandall Stewart 		control->end_added = 1;
4418f8829a4aSRandall Stewart 	}
4419f8829a4aSRandall Stewart 	TAILQ_INSERT_TAIL(&inp->read_queue, control, next);
4420cfde3ff7SRandall Stewart 	if (inp_read_lock_held == 0)
4421f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
4422f8829a4aSRandall Stewart 	if (inp && inp->sctp_socket) {
442317205eccSRandall Stewart 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
442417205eccSRandall Stewart 			SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
4425ceaad40aSRandall Stewart 		} else {
4426ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4427ceaad40aSRandall Stewart 			struct socket *so;
4428ceaad40aSRandall Stewart 
4429ceaad40aSRandall Stewart 			so = SCTP_INP_SO(inp);
4430ceaad40aSRandall Stewart 			if (!so_locked) {
443160990c0cSMichael Tuexen 				if (stcb) {
4432ceaad40aSRandall Stewart 					atomic_add_int(&stcb->asoc.refcnt, 1);
4433ceaad40aSRandall Stewart 					SCTP_TCB_UNLOCK(stcb);
443460990c0cSMichael Tuexen 				}
4435ceaad40aSRandall Stewart 				SCTP_SOCKET_LOCK(so, 1);
443660990c0cSMichael Tuexen 				if (stcb) {
4437ceaad40aSRandall Stewart 					SCTP_TCB_LOCK(stcb);
4438ceaad40aSRandall Stewart 					atomic_subtract_int(&stcb->asoc.refcnt, 1);
443960990c0cSMichael Tuexen 				}
4440ceaad40aSRandall Stewart 				if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4441ceaad40aSRandall Stewart 					SCTP_SOCKET_UNLOCK(so, 1);
4442ceaad40aSRandall Stewart 					return;
4443ceaad40aSRandall Stewart 				}
4444ceaad40aSRandall Stewart 			}
4445ceaad40aSRandall Stewart #endif
4446f8829a4aSRandall Stewart 			sctp_sorwakeup(inp, inp->sctp_socket);
4447ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4448ceaad40aSRandall Stewart 			if (!so_locked) {
4449ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
4450ceaad40aSRandall Stewart 			}
4451ceaad40aSRandall Stewart #endif
4452ceaad40aSRandall Stewart 		}
4453f8829a4aSRandall Stewart 	}
4454f8829a4aSRandall Stewart }
4455f8829a4aSRandall Stewart 
4456f8829a4aSRandall Stewart 
4457f8829a4aSRandall Stewart int
4458f8829a4aSRandall Stewart sctp_append_to_readq(struct sctp_inpcb *inp,
4459f8829a4aSRandall Stewart     struct sctp_tcb *stcb,
4460f8829a4aSRandall Stewart     struct sctp_queued_to_read *control,
4461f8829a4aSRandall Stewart     struct mbuf *m,
4462f8829a4aSRandall Stewart     int end,
4463f8829a4aSRandall Stewart     int ctls_cumack,
4464f8829a4aSRandall Stewart     struct sockbuf *sb)
4465f8829a4aSRandall Stewart {
4466f8829a4aSRandall Stewart 	/*
4467f8829a4aSRandall Stewart 	 * A partial delivery API event is underway. OR we are appending on
4468f8829a4aSRandall Stewart 	 * the reassembly queue.
4469f8829a4aSRandall Stewart 	 *
4470f8829a4aSRandall Stewart 	 * If PDAPI this means we need to add m to the end of the data.
4471f8829a4aSRandall Stewart 	 * Increase the length in the control AND increment the sb_cc.
4472f8829a4aSRandall Stewart 	 * Otherwise sb is NULL and all we need to do is put it at the end
4473f8829a4aSRandall Stewart 	 * of the mbuf chain.
4474f8829a4aSRandall Stewart 	 */
4475f8829a4aSRandall Stewart 	int len = 0;
4476f8829a4aSRandall Stewart 	struct mbuf *mm, *tail = NULL, *prev = NULL;
4477f8829a4aSRandall Stewart 
4478f8829a4aSRandall Stewart 	if (inp) {
4479f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(inp);
4480f8829a4aSRandall Stewart 	}
4481f8829a4aSRandall Stewart 	if (control == NULL) {
4482f8829a4aSRandall Stewart get_out:
4483f8829a4aSRandall Stewart 		if (inp) {
4484f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
4485f8829a4aSRandall Stewart 		}
4486f8829a4aSRandall Stewart 		return (-1);
4487f8829a4aSRandall Stewart 	}
4488cd1386abSMichael Tuexen 	if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ)) {
4489cd1386abSMichael Tuexen 		SCTP_INP_READ_UNLOCK(inp);
449060990c0cSMichael Tuexen 		return (0);
4491cd1386abSMichael Tuexen 	}
4492139bc87fSRandall Stewart 	if (control->end_added) {
4493f8829a4aSRandall Stewart 		/* huh this one is complete? */
4494f8829a4aSRandall Stewart 		goto get_out;
4495f8829a4aSRandall Stewart 	}
4496f8829a4aSRandall Stewart 	mm = m;
4497f8829a4aSRandall Stewart 	if (mm == NULL) {
4498f8829a4aSRandall Stewart 		goto get_out;
4499f8829a4aSRandall Stewart 	}
4500f8829a4aSRandall Stewart 	while (mm) {
4501139bc87fSRandall Stewart 		if (SCTP_BUF_LEN(mm) == 0) {
4502f8829a4aSRandall Stewart 			/* Skip mbufs with NO lenght */
4503f8829a4aSRandall Stewart 			if (prev == NULL) {
4504f8829a4aSRandall Stewart 				/* First one */
4505f8829a4aSRandall Stewart 				m = sctp_m_free(mm);
4506f8829a4aSRandall Stewart 				mm = m;
4507f8829a4aSRandall Stewart 			} else {
4508139bc87fSRandall Stewart 				SCTP_BUF_NEXT(prev) = sctp_m_free(mm);
4509139bc87fSRandall Stewart 				mm = SCTP_BUF_NEXT(prev);
4510f8829a4aSRandall Stewart 			}
4511f8829a4aSRandall Stewart 			continue;
4512f8829a4aSRandall Stewart 		}
4513f8829a4aSRandall Stewart 		prev = mm;
4514139bc87fSRandall Stewart 		len += SCTP_BUF_LEN(mm);
4515f8829a4aSRandall Stewart 		if (sb) {
4516b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4517139bc87fSRandall Stewart 				sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(mm));
451880fefe0aSRandall Stewart 			}
4519f8829a4aSRandall Stewart 			sctp_sballoc(stcb, sb, mm);
4520b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4521f8829a4aSRandall Stewart 				sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
452280fefe0aSRandall Stewart 			}
4523f8829a4aSRandall Stewart 		}
4524139bc87fSRandall Stewart 		mm = SCTP_BUF_NEXT(mm);
4525f8829a4aSRandall Stewart 	}
4526f8829a4aSRandall Stewart 	if (prev) {
4527f8829a4aSRandall Stewart 		tail = prev;
4528f8829a4aSRandall Stewart 	} else {
4529f8829a4aSRandall Stewart 		/* Really there should always be a prev */
4530f8829a4aSRandall Stewart 		if (m == NULL) {
4531f8829a4aSRandall Stewart 			/* Huh nothing left? */
4532a5d547adSRandall Stewart #ifdef INVARIANTS
4533f8829a4aSRandall Stewart 			panic("Nothing left to add?");
4534f8829a4aSRandall Stewart #else
4535f8829a4aSRandall Stewart 			goto get_out;
4536f8829a4aSRandall Stewart #endif
4537f8829a4aSRandall Stewart 		}
4538f8829a4aSRandall Stewart 		tail = m;
4539f8829a4aSRandall Stewart 	}
4540f8829a4aSRandall Stewart 	if (control->tail_mbuf) {
4541f8829a4aSRandall Stewart 		/* append */
4542139bc87fSRandall Stewart 		SCTP_BUF_NEXT(control->tail_mbuf) = m;
4543f8829a4aSRandall Stewart 		control->tail_mbuf = tail;
4544f8829a4aSRandall Stewart 	} else {
4545f8829a4aSRandall Stewart 		/* nothing there */
4546a5d547adSRandall Stewart #ifdef INVARIANTS
4547f8829a4aSRandall Stewart 		if (control->data != NULL) {
4548f8829a4aSRandall Stewart 			panic("This should NOT happen");
4549f8829a4aSRandall Stewart 		}
4550f8829a4aSRandall Stewart #endif
4551f8829a4aSRandall Stewart 		control->data = m;
4552f8829a4aSRandall Stewart 		control->tail_mbuf = tail;
4553f8829a4aSRandall Stewart 	}
455418e198d3SRandall Stewart 	atomic_add_int(&control->length, len);
455518e198d3SRandall Stewart 	if (end) {
455618e198d3SRandall Stewart 		/* message is complete */
455718e198d3SRandall Stewart 		if (stcb && (control == stcb->asoc.control_pdapi)) {
455818e198d3SRandall Stewart 			stcb->asoc.control_pdapi = NULL;
455918e198d3SRandall Stewart 		}
456018e198d3SRandall Stewart 		control->held_length = 0;
456118e198d3SRandall Stewart 		control->end_added = 1;
456218e198d3SRandall Stewart 	}
4563ad81507eSRandall Stewart 	if (stcb == NULL) {
4564ad81507eSRandall Stewart 		control->do_not_ref_stcb = 1;
4565ad81507eSRandall Stewart 	}
4566f8829a4aSRandall Stewart 	/*
4567f8829a4aSRandall Stewart 	 * When we are appending in partial delivery, the cum-ack is used
4568f8829a4aSRandall Stewart 	 * for the actual pd-api highest tsn on this mbuf. The true cum-ack
4569f8829a4aSRandall Stewart 	 * is populated in the outbound sinfo structure from the true cumack
4570f8829a4aSRandall Stewart 	 * if the association exists...
4571f8829a4aSRandall Stewart 	 */
4572f8829a4aSRandall Stewart 	control->sinfo_tsn = control->sinfo_cumtsn = ctls_cumack;
4573f8829a4aSRandall Stewart 	if (inp) {
4574f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
4575f8829a4aSRandall Stewart 	}
4576f8829a4aSRandall Stewart 	if (inp && inp->sctp_socket) {
457717205eccSRandall Stewart 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
457817205eccSRandall Stewart 			SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
4579ceaad40aSRandall Stewart 		} else {
4580ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4581ceaad40aSRandall Stewart 			struct socket *so;
4582ceaad40aSRandall Stewart 
4583ceaad40aSRandall Stewart 			so = SCTP_INP_SO(inp);
458460990c0cSMichael Tuexen 			if (stcb) {
4585ceaad40aSRandall Stewart 				atomic_add_int(&stcb->asoc.refcnt, 1);
4586ceaad40aSRandall Stewart 				SCTP_TCB_UNLOCK(stcb);
458760990c0cSMichael Tuexen 			}
4588ceaad40aSRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
458960990c0cSMichael Tuexen 			if (stcb) {
4590ceaad40aSRandall Stewart 				SCTP_TCB_LOCK(stcb);
4591ceaad40aSRandall Stewart 				atomic_subtract_int(&stcb->asoc.refcnt, 1);
459260990c0cSMichael Tuexen 			}
4593ceaad40aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4594ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
4595ceaad40aSRandall Stewart 				return (0);
4596ceaad40aSRandall Stewart 			}
4597ceaad40aSRandall Stewart #endif
4598f8829a4aSRandall Stewart 			sctp_sorwakeup(inp, inp->sctp_socket);
4599ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4600ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
4601ceaad40aSRandall Stewart #endif
4602ceaad40aSRandall Stewart 		}
4603f8829a4aSRandall Stewart 	}
4604f8829a4aSRandall Stewart 	return (0);
4605f8829a4aSRandall Stewart }
4606f8829a4aSRandall Stewart 
4607f8829a4aSRandall Stewart 
4608f8829a4aSRandall Stewart 
4609f8829a4aSRandall Stewart /*************HOLD THIS COMMENT FOR PATCH FILE OF
4610f8829a4aSRandall Stewart  *************ALTERNATE ROUTING CODE
4611f8829a4aSRandall Stewart  */
4612f8829a4aSRandall Stewart 
4613f8829a4aSRandall Stewart /*************HOLD THIS COMMENT FOR END OF PATCH FILE OF
4614f8829a4aSRandall Stewart  *************ALTERNATE ROUTING CODE
4615f8829a4aSRandall Stewart  */
4616f8829a4aSRandall Stewart 
4617f8829a4aSRandall Stewart struct mbuf *
4618ff1ffd74SMichael Tuexen sctp_generate_cause(uint16_t code, char *info)
4619f8829a4aSRandall Stewart {
4620f8829a4aSRandall Stewart 	struct mbuf *m;
4621ff1ffd74SMichael Tuexen 	struct sctp_gen_error_cause *cause;
4622ff1ffd74SMichael Tuexen 	size_t info_len, len;
4623f8829a4aSRandall Stewart 
4624ff1ffd74SMichael Tuexen 	if ((code == 0) || (info == NULL)) {
4625ff1ffd74SMichael Tuexen 		return (NULL);
4626ff1ffd74SMichael Tuexen 	}
4627ff1ffd74SMichael Tuexen 	info_len = strlen(info);
4628ff1ffd74SMichael Tuexen 	len = sizeof(struct sctp_paramhdr) + info_len;
4629ff1ffd74SMichael Tuexen 	m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
4630ff1ffd74SMichael Tuexen 	if (m != NULL) {
4631ff1ffd74SMichael Tuexen 		SCTP_BUF_LEN(m) = len;
4632ff1ffd74SMichael Tuexen 		cause = mtod(m, struct sctp_gen_error_cause *);
4633ff1ffd74SMichael Tuexen 		cause->code = htons(code);
4634ff1ffd74SMichael Tuexen 		cause->length = htons((uint16_t) len);
4635ff1ffd74SMichael Tuexen 		memcpy(cause->info, info, info_len);
4636f8829a4aSRandall Stewart 	}
4637f8829a4aSRandall Stewart 	return (m);
4638f8829a4aSRandall Stewart }
4639f8829a4aSRandall Stewart 
464032451da4SMichael Tuexen struct mbuf *
464132451da4SMichael Tuexen sctp_generate_no_user_data_cause(uint32_t tsn)
464232451da4SMichael Tuexen {
464332451da4SMichael Tuexen 	struct mbuf *m;
464432451da4SMichael Tuexen 	struct sctp_error_no_user_data *no_user_data_cause;
464532451da4SMichael Tuexen 	size_t len;
464632451da4SMichael Tuexen 
464732451da4SMichael Tuexen 	len = sizeof(struct sctp_error_no_user_data);
464832451da4SMichael Tuexen 	m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
464932451da4SMichael Tuexen 	if (m != NULL) {
465032451da4SMichael Tuexen 		SCTP_BUF_LEN(m) = len;
465132451da4SMichael Tuexen 		no_user_data_cause = mtod(m, struct sctp_error_no_user_data *);
465232451da4SMichael Tuexen 		no_user_data_cause->cause.code = htons(SCTP_CAUSE_NO_USER_DATA);
465332451da4SMichael Tuexen 		no_user_data_cause->cause.length = htons((uint16_t) len);
465432451da4SMichael Tuexen 		no_user_data_cause->tsn = tsn;	/* tsn is passed in as NBO */
465532451da4SMichael Tuexen 	}
465632451da4SMichael Tuexen 	return (m);
465732451da4SMichael Tuexen }
465832451da4SMichael Tuexen 
4659f8829a4aSRandall Stewart #ifdef SCTP_MBCNT_LOGGING
4660f8829a4aSRandall Stewart void
4661f8829a4aSRandall Stewart sctp_free_bufspace(struct sctp_tcb *stcb, struct sctp_association *asoc,
4662f8829a4aSRandall Stewart     struct sctp_tmit_chunk *tp1, int chk_cnt)
4663f8829a4aSRandall Stewart {
4664f8829a4aSRandall Stewart 	if (tp1->data == NULL) {
4665f8829a4aSRandall Stewart 		return;
4666f8829a4aSRandall Stewart 	}
4667f8829a4aSRandall Stewart 	asoc->chunks_on_out_queue -= chk_cnt;
4668b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBCNT_LOGGING_ENABLE) {
4669f8829a4aSRandall Stewart 		sctp_log_mbcnt(SCTP_LOG_MBCNT_DECREASE,
4670f8829a4aSRandall Stewart 		    asoc->total_output_queue_size,
4671f8829a4aSRandall Stewart 		    tp1->book_size,
4672f8829a4aSRandall Stewart 		    0,
4673f8829a4aSRandall Stewart 		    tp1->mbcnt);
467480fefe0aSRandall Stewart 	}
4675f8829a4aSRandall Stewart 	if (asoc->total_output_queue_size >= tp1->book_size) {
467644b7479bSRandall Stewart 		atomic_add_int(&asoc->total_output_queue_size, -tp1->book_size);
4677f8829a4aSRandall Stewart 	} else {
4678f8829a4aSRandall Stewart 		asoc->total_output_queue_size = 0;
4679f8829a4aSRandall Stewart 	}
4680f8829a4aSRandall Stewart 
4681f8829a4aSRandall Stewart 	if (stcb->sctp_socket && (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) ||
4682f8829a4aSRandall Stewart 	    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) {
4683f8829a4aSRandall Stewart 		if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) {
4684f8829a4aSRandall Stewart 			stcb->sctp_socket->so_snd.sb_cc -= tp1->book_size;
4685f8829a4aSRandall Stewart 		} else {
4686f8829a4aSRandall Stewart 			stcb->sctp_socket->so_snd.sb_cc = 0;
4687f8829a4aSRandall Stewart 
4688f8829a4aSRandall Stewart 		}
4689f8829a4aSRandall Stewart 	}
4690f8829a4aSRandall Stewart }
4691f8829a4aSRandall Stewart 
4692f8829a4aSRandall Stewart #endif
4693f8829a4aSRandall Stewart 
4694f8829a4aSRandall Stewart int
4695f8829a4aSRandall Stewart sctp_release_pr_sctp_chunk(struct sctp_tcb *stcb, struct sctp_tmit_chunk *tp1,
46961edc9dbaSMichael Tuexen     uint8_t sent, int so_locked
4697ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4698ceaad40aSRandall Stewart     SCTP_UNUSED
4699ceaad40aSRandall Stewart #endif
4700ceaad40aSRandall Stewart )
4701f8829a4aSRandall Stewart {
47020c0982b8SRandall Stewart 	struct sctp_stream_out *strq;
47034a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk = NULL, *tp2;
47040c0982b8SRandall Stewart 	struct sctp_stream_queue_pending *sp;
47050c0982b8SRandall Stewart 	uint16_t stream = 0, seq = 0;
47060c0982b8SRandall Stewart 	uint8_t foundeom = 0;
4707f8829a4aSRandall Stewart 	int ret_sz = 0;
4708f8829a4aSRandall Stewart 	int notdone;
47090c0982b8SRandall Stewart 	int do_wakeup_routine = 0;
4710f8829a4aSRandall Stewart 
47110c0982b8SRandall Stewart 	stream = tp1->rec.data.stream_number;
47120c0982b8SRandall Stewart 	seq = tp1->rec.data.stream_seq;
4713f8829a4aSRandall Stewart 	do {
4714f8829a4aSRandall Stewart 		ret_sz += tp1->book_size;
47150c0982b8SRandall Stewart 		if (tp1->data != NULL) {
47168933fa13SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4717830d754dSRandall Stewart 				sctp_flight_size_decrease(tp1);
4718830d754dSRandall Stewart 				sctp_total_flight_decrease(stcb, tp1);
47198933fa13SRandall Stewart 			}
47208933fa13SRandall Stewart 			sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
47210c0982b8SRandall Stewart 			stcb->asoc.peers_rwnd += tp1->send_size;
47220c0982b8SRandall Stewart 			stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
47231edc9dbaSMichael Tuexen 			if (sent) {
47241edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
47251edc9dbaSMichael Tuexen 			} else {
47261edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
47271edc9dbaSMichael Tuexen 			}
47282f99457bSMichael Tuexen 			if (tp1->data) {
4729f8829a4aSRandall Stewart 				sctp_m_freem(tp1->data);
4730f8829a4aSRandall Stewart 				tp1->data = NULL;
47312f99457bSMichael Tuexen 			}
47320c0982b8SRandall Stewart 			do_wakeup_routine = 1;
4733f8829a4aSRandall Stewart 			if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
4734f8829a4aSRandall Stewart 				stcb->asoc.sent_queue_cnt_removeable--;
4735f8829a4aSRandall Stewart 			}
4736f8829a4aSRandall Stewart 		}
47378933fa13SRandall Stewart 		tp1->sent = SCTP_FORWARD_TSN_SKIP;
4738f8829a4aSRandall Stewart 		if ((tp1->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) ==
4739f8829a4aSRandall Stewart 		    SCTP_DATA_NOT_FRAG) {
4740f8829a4aSRandall Stewart 			/* not frag'ed we ae done   */
4741f8829a4aSRandall Stewart 			notdone = 0;
4742f8829a4aSRandall Stewart 			foundeom = 1;
4743f8829a4aSRandall Stewart 		} else if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
4744f8829a4aSRandall Stewart 			/* end of frag, we are done */
4745f8829a4aSRandall Stewart 			notdone = 0;
4746f8829a4aSRandall Stewart 			foundeom = 1;
4747f8829a4aSRandall Stewart 		} else {
4748f8829a4aSRandall Stewart 			/*
4749f8829a4aSRandall Stewart 			 * Its a begin or middle piece, we must mark all of
4750f8829a4aSRandall Stewart 			 * it
4751f8829a4aSRandall Stewart 			 */
4752f8829a4aSRandall Stewart 			notdone = 1;
4753f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
4754f8829a4aSRandall Stewart 		}
4755f8829a4aSRandall Stewart 	} while (tp1 && notdone);
47560c0982b8SRandall Stewart 	if (foundeom == 0) {
4757f8829a4aSRandall Stewart 		/*
4758f8829a4aSRandall Stewart 		 * The multi-part message was scattered across the send and
4759f8829a4aSRandall Stewart 		 * sent queue.
4760f8829a4aSRandall Stewart 		 */
47614a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(tp1, &stcb->asoc.send_queue, sctp_next, tp2) {
47624a9ef3f8SMichael Tuexen 			if ((tp1->rec.data.stream_number != stream) ||
47634a9ef3f8SMichael Tuexen 			    (tp1->rec.data.stream_seq != seq)) {
47644a9ef3f8SMichael Tuexen 				break;
47654a9ef3f8SMichael Tuexen 			}
47660c0982b8SRandall Stewart 			/*
47670c0982b8SRandall Stewart 			 * save to chk in case we have some on stream out
47680c0982b8SRandall Stewart 			 * queue. If so and we have an un-transmitted one we
47690c0982b8SRandall Stewart 			 * don't have to fudge the TSN.
47700c0982b8SRandall Stewart 			 */
47710c0982b8SRandall Stewart 			chk = tp1;
47720c0982b8SRandall Stewart 			ret_sz += tp1->book_size;
47730c0982b8SRandall Stewart 			sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
47741edc9dbaSMichael Tuexen 			if (sent) {
47751edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
47761edc9dbaSMichael Tuexen 			} else {
47771edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
47781edc9dbaSMichael Tuexen 			}
47792f99457bSMichael Tuexen 			if (tp1->data) {
47800c0982b8SRandall Stewart 				sctp_m_freem(tp1->data);
47812f99457bSMichael Tuexen 				tp1->data = NULL;
47822f99457bSMichael Tuexen 			}
47838933fa13SRandall Stewart 			/* No flight involved here book the size to 0 */
47848933fa13SRandall Stewart 			tp1->book_size = 0;
47850c0982b8SRandall Stewart 			if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
47860c0982b8SRandall Stewart 				foundeom = 1;
4787f8829a4aSRandall Stewart 			}
47880c0982b8SRandall Stewart 			do_wakeup_routine = 1;
47890c0982b8SRandall Stewart 			tp1->sent = SCTP_FORWARD_TSN_SKIP;
47900c0982b8SRandall Stewart 			TAILQ_REMOVE(&stcb->asoc.send_queue, tp1, sctp_next);
47910c0982b8SRandall Stewart 			/*
47920c0982b8SRandall Stewart 			 * on to the sent queue so we can wait for it to be
47930c0982b8SRandall Stewart 			 * passed by.
47940c0982b8SRandall Stewart 			 */
47950c0982b8SRandall Stewart 			TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, tp1,
47960c0982b8SRandall Stewart 			    sctp_next);
47970c0982b8SRandall Stewart 			stcb->asoc.send_queue_cnt--;
47980c0982b8SRandall Stewart 			stcb->asoc.sent_queue_cnt++;
47990c0982b8SRandall Stewart 		}
48000c0982b8SRandall Stewart 	}
48010c0982b8SRandall Stewart 	if (foundeom == 0) {
48020c0982b8SRandall Stewart 		/*
48030c0982b8SRandall Stewart 		 * Still no eom found. That means there is stuff left on the
48040c0982b8SRandall Stewart 		 * stream out queue.. yuck.
48050c0982b8SRandall Stewart 		 */
48060c0982b8SRandall Stewart 		SCTP_TCB_SEND_LOCK(stcb);
4807f3b05218SMichael Tuexen 		strq = &stcb->asoc.strmout[stream];
4808f3b05218SMichael Tuexen 		sp = TAILQ_FIRST(&strq->outqueue);
4809f3b05218SMichael Tuexen 		if (sp != NULL) {
48100c0982b8SRandall Stewart 			sp->discard_rest = 1;
48110c0982b8SRandall Stewart 			/*
4812f3b05218SMichael Tuexen 			 * We may need to put a chunk on the queue that
4813f3b05218SMichael Tuexen 			 * holds the TSN that would have been sent with the
4814f3b05218SMichael Tuexen 			 * LAST bit.
48150c0982b8SRandall Stewart 			 */
48160c0982b8SRandall Stewart 			if (chk == NULL) {
48170c0982b8SRandall Stewart 				/* Yep, we have to */
48180c0982b8SRandall Stewart 				sctp_alloc_a_chunk(stcb, chk);
48190c0982b8SRandall Stewart 				if (chk == NULL) {
48200c0982b8SRandall Stewart 					/*
4821f3b05218SMichael Tuexen 					 * we are hosed. All we can do is
4822f3b05218SMichael Tuexen 					 * nothing.. which will cause an
4823f3b05218SMichael Tuexen 					 * abort if the peer is paying
48240c0982b8SRandall Stewart 					 * attention.
48250c0982b8SRandall Stewart 					 */
48260c0982b8SRandall Stewart 					goto oh_well;
48270c0982b8SRandall Stewart 				}
48280c0982b8SRandall Stewart 				memset(chk, 0, sizeof(*chk));
48290c0982b8SRandall Stewart 				chk->rec.data.rcv_flags = SCTP_DATA_LAST_FRAG;
48300c0982b8SRandall Stewart 				chk->sent = SCTP_FORWARD_TSN_SKIP;
48310c0982b8SRandall Stewart 				chk->asoc = &stcb->asoc;
4832f3b05218SMichael Tuexen 				chk->rec.data.stream_seq = strq->next_sequence_send;
48330c0982b8SRandall Stewart 				chk->rec.data.stream_number = sp->stream;
48340c0982b8SRandall Stewart 				chk->rec.data.payloadtype = sp->ppid;
48350c0982b8SRandall Stewart 				chk->rec.data.context = sp->context;
48360c0982b8SRandall Stewart 				chk->flags = sp->act_flags;
48379eea4a2dSMichael Tuexen 				if (sp->net)
48380c0982b8SRandall Stewart 					chk->whoTo = sp->net;
48399eea4a2dSMichael Tuexen 				else
48409eea4a2dSMichael Tuexen 					chk->whoTo = stcb->asoc.primary_destination;
48410c0982b8SRandall Stewart 				atomic_add_int(&chk->whoTo->ref_count, 1);
48420c0982b8SRandall Stewart 				chk->rec.data.TSN_seq = atomic_fetchadd_int(&stcb->asoc.sending_seq, 1);
48430c0982b8SRandall Stewart 				stcb->asoc.pr_sctp_cnt++;
48440c0982b8SRandall Stewart 				TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, chk, sctp_next);
48450c0982b8SRandall Stewart 				stcb->asoc.sent_queue_cnt++;
48468933fa13SRandall Stewart 				stcb->asoc.pr_sctp_cnt++;
48470c0982b8SRandall Stewart 			} else {
48480c0982b8SRandall Stewart 				chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
48490c0982b8SRandall Stewart 			}
4850f3b05218SMichael Tuexen 			strq->next_sequence_send++;
48510c0982b8SRandall Stewart 	oh_well:
48520c0982b8SRandall Stewart 			if (sp->data) {
48530c0982b8SRandall Stewart 				/*
4854f3b05218SMichael Tuexen 				 * Pull any data to free up the SB and allow
4855f3b05218SMichael Tuexen 				 * sender to "add more" while we will throw
4856f3b05218SMichael Tuexen 				 * away :-)
48570c0982b8SRandall Stewart 				 */
4858f3b05218SMichael Tuexen 				sctp_free_spbufspace(stcb, &stcb->asoc, sp);
48590c0982b8SRandall Stewart 				ret_sz += sp->length;
48600c0982b8SRandall Stewart 				do_wakeup_routine = 1;
48610c0982b8SRandall Stewart 				sp->some_taken = 1;
48620c0982b8SRandall Stewart 				sctp_m_freem(sp->data);
48630c0982b8SRandall Stewart 				sp->data = NULL;
48640c0982b8SRandall Stewart 				sp->tail_mbuf = NULL;
4865d07b2ac6SMichael Tuexen 				sp->length = 0;
48660c0982b8SRandall Stewart 			}
48670c0982b8SRandall Stewart 		}
48680c0982b8SRandall Stewart 		SCTP_TCB_SEND_UNLOCK(stcb);
48690c0982b8SRandall Stewart 	}
48700c0982b8SRandall Stewart 	if (do_wakeup_routine) {
48710c0982b8SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
48728933fa13SRandall Stewart 		struct socket *so;
48738933fa13SRandall Stewart 
48740c0982b8SRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
48750c0982b8SRandall Stewart 		if (!so_locked) {
48760c0982b8SRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, 1);
48770c0982b8SRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
48780c0982b8SRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
48790c0982b8SRandall Stewart 			SCTP_TCB_LOCK(stcb);
48800c0982b8SRandall Stewart 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
48810c0982b8SRandall Stewart 			if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
48820c0982b8SRandall Stewart 				/* assoc was freed while we were unlocked */
48830c0982b8SRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
48840c0982b8SRandall Stewart 				return (ret_sz);
48850c0982b8SRandall Stewart 			}
48860c0982b8SRandall Stewart 		}
48870c0982b8SRandall Stewart #endif
48880c0982b8SRandall Stewart 		sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
48890c0982b8SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
48900c0982b8SRandall Stewart 		if (!so_locked) {
48910c0982b8SRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
48920c0982b8SRandall Stewart 		}
48930c0982b8SRandall Stewart #endif
4894f8829a4aSRandall Stewart 	}
4895f8829a4aSRandall Stewart 	return (ret_sz);
4896f8829a4aSRandall Stewart }
4897f8829a4aSRandall Stewart 
4898f8829a4aSRandall Stewart /*
4899f8829a4aSRandall Stewart  * checks to see if the given address, sa, is one that is currently known by
4900f8829a4aSRandall Stewart  * the kernel note: can't distinguish the same address on multiple interfaces
4901f8829a4aSRandall Stewart  * and doesn't handle multiple addresses with different zone/scope id's note:
4902f8829a4aSRandall Stewart  * ifa_ifwithaddr() compares the entire sockaddr struct
4903f8829a4aSRandall Stewart  */
490442551e99SRandall Stewart struct sctp_ifa *
490580fefe0aSRandall Stewart sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr,
490680fefe0aSRandall Stewart     int holds_lock)
4907f8829a4aSRandall Stewart {
490842551e99SRandall Stewart 	struct sctp_laddr *laddr;
4909f8829a4aSRandall Stewart 
4910ad81507eSRandall Stewart 	if (holds_lock == 0) {
491142551e99SRandall Stewart 		SCTP_INP_RLOCK(inp);
4912ad81507eSRandall Stewart 	}
491342551e99SRandall Stewart 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
491442551e99SRandall Stewart 		if (laddr->ifa == NULL)
4915f8829a4aSRandall Stewart 			continue;
491642551e99SRandall Stewart 		if (addr->sa_family != laddr->ifa->address.sa.sa_family)
491742551e99SRandall Stewart 			continue;
4918e6194c2eSMichael Tuexen #ifdef INET
491942551e99SRandall Stewart 		if (addr->sa_family == AF_INET) {
492042551e99SRandall Stewart 			if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
492142551e99SRandall Stewart 			    laddr->ifa->address.sin.sin_addr.s_addr) {
492242551e99SRandall Stewart 				/* found him. */
4923ad81507eSRandall Stewart 				if (holds_lock == 0) {
492442551e99SRandall Stewart 					SCTP_INP_RUNLOCK(inp);
4925ad81507eSRandall Stewart 				}
492642551e99SRandall Stewart 				return (laddr->ifa);
492742551e99SRandall Stewart 				break;
492842551e99SRandall Stewart 			}
49295e2c2d87SRandall Stewart 		}
4930e6194c2eSMichael Tuexen #endif
49315e2c2d87SRandall Stewart #ifdef INET6
49325e2c2d87SRandall Stewart 		if (addr->sa_family == AF_INET6) {
4933c54a18d2SRandall Stewart 			if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
4934c54a18d2SRandall Stewart 			    &laddr->ifa->address.sin6)) {
493542551e99SRandall Stewart 				/* found him. */
4936ad81507eSRandall Stewart 				if (holds_lock == 0) {
493742551e99SRandall Stewart 					SCTP_INP_RUNLOCK(inp);
4938ad81507eSRandall Stewart 				}
493942551e99SRandall Stewart 				return (laddr->ifa);
494042551e99SRandall Stewart 				break;
494142551e99SRandall Stewart 			}
494242551e99SRandall Stewart 		}
49435e2c2d87SRandall Stewart #endif
494442551e99SRandall Stewart 	}
4945ad81507eSRandall Stewart 	if (holds_lock == 0) {
494642551e99SRandall Stewart 		SCTP_INP_RUNLOCK(inp);
4947ad81507eSRandall Stewart 	}
494842551e99SRandall Stewart 	return (NULL);
494942551e99SRandall Stewart }
4950f8829a4aSRandall Stewart 
49516a27c376SRandall Stewart uint32_t
49526a27c376SRandall Stewart sctp_get_ifa_hash_val(struct sockaddr *addr)
49536a27c376SRandall Stewart {
4954ea5eba11SMichael Tuexen 	switch (addr->sa_family) {
4955ea5eba11SMichael Tuexen #ifdef INET
4956ea5eba11SMichael Tuexen 	case AF_INET:
4957ea5eba11SMichael Tuexen 		{
49586a27c376SRandall Stewart 			struct sockaddr_in *sin;
49596a27c376SRandall Stewart 
49606a27c376SRandall Stewart 			sin = (struct sockaddr_in *)addr;
49616a27c376SRandall Stewart 			return (sin->sin_addr.s_addr ^ (sin->sin_addr.s_addr >> 16));
4962ea5eba11SMichael Tuexen 		}
4963ea5eba11SMichael Tuexen #endif
4964ea5eba11SMichael Tuexen #ifdef INET6
49652c2e3218SMichael Tuexen 	case AF_INET6:
4966ea5eba11SMichael Tuexen 		{
49676a27c376SRandall Stewart 			struct sockaddr_in6 *sin6;
49686a27c376SRandall Stewart 			uint32_t hash_of_addr;
49696a27c376SRandall Stewart 
49706a27c376SRandall Stewart 			sin6 = (struct sockaddr_in6 *)addr;
49716a27c376SRandall Stewart 			hash_of_addr = (sin6->sin6_addr.s6_addr32[0] +
49726a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[1] +
49736a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[2] +
49746a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[3]);
49756a27c376SRandall Stewart 			hash_of_addr = (hash_of_addr ^ (hash_of_addr >> 16));
49766a27c376SRandall Stewart 			return (hash_of_addr);
49776a27c376SRandall Stewart 		}
4978ea5eba11SMichael Tuexen #endif
4979ea5eba11SMichael Tuexen 	default:
4980ea5eba11SMichael Tuexen 		break;
4981ea5eba11SMichael Tuexen 	}
49826a27c376SRandall Stewart 	return (0);
49836a27c376SRandall Stewart }
49846a27c376SRandall Stewart 
498542551e99SRandall Stewart struct sctp_ifa *
498642551e99SRandall Stewart sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock)
498742551e99SRandall Stewart {
498842551e99SRandall Stewart 	struct sctp_ifa *sctp_ifap;
498942551e99SRandall Stewart 	struct sctp_vrf *vrf;
49906a27c376SRandall Stewart 	struct sctp_ifalist *hash_head;
49916a27c376SRandall Stewart 	uint32_t hash_of_addr;
499242551e99SRandall Stewart 
499342551e99SRandall Stewart 	if (holds_lock == 0)
4994c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RLOCK();
499542551e99SRandall Stewart 
4996bff64a4dSRandall Stewart 	vrf = sctp_find_vrf(vrf_id);
4997bff64a4dSRandall Stewart 	if (vrf == NULL) {
4998df6e0cc3SRandall Stewart stage_right:
4999bff64a4dSRandall Stewart 		if (holds_lock == 0)
5000c99efcf6SRandall Stewart 			SCTP_IPI_ADDR_RUNLOCK();
5001bff64a4dSRandall Stewart 		return (NULL);
5002bff64a4dSRandall Stewart 	}
5003bff64a4dSRandall Stewart 	hash_of_addr = sctp_get_ifa_hash_val(addr);
5004bff64a4dSRandall Stewart 
500517205eccSRandall Stewart 	hash_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
5006bff64a4dSRandall Stewart 	if (hash_head == NULL) {
5007ad81507eSRandall Stewart 		SCTP_PRINTF("hash_of_addr:%x mask:%x table:%x - ",
5008c99efcf6SRandall Stewart 		    hash_of_addr, (uint32_t) vrf->vrf_addr_hashmark,
5009c99efcf6SRandall Stewart 		    (uint32_t) (hash_of_addr & vrf->vrf_addr_hashmark));
5010bff64a4dSRandall Stewart 		sctp_print_address(addr);
5011ad81507eSRandall Stewart 		SCTP_PRINTF("No such bucket for address\n");
5012bff64a4dSRandall Stewart 		if (holds_lock == 0)
5013c99efcf6SRandall Stewart 			SCTP_IPI_ADDR_RUNLOCK();
5014bff64a4dSRandall Stewart 
5015bff64a4dSRandall Stewart 		return (NULL);
5016bff64a4dSRandall Stewart 	}
50176a27c376SRandall Stewart 	LIST_FOREACH(sctp_ifap, hash_head, next_bucket) {
5018bff64a4dSRandall Stewart 		if (sctp_ifap == NULL) {
5019df6e0cc3SRandall Stewart #ifdef INVARIANTS
5020bff64a4dSRandall Stewart 			panic("Huh LIST_FOREACH corrupt");
5021df6e0cc3SRandall Stewart 			goto stage_right;
5022df6e0cc3SRandall Stewart #else
5023df6e0cc3SRandall Stewart 			SCTP_PRINTF("LIST corrupt of sctp_ifap's?\n");
5024df6e0cc3SRandall Stewart 			goto stage_right;
5025df6e0cc3SRandall Stewart #endif
5026bff64a4dSRandall Stewart 		}
50276a27c376SRandall Stewart 		if (addr->sa_family != sctp_ifap->address.sa.sa_family)
50286a27c376SRandall Stewart 			continue;
5029e6194c2eSMichael Tuexen #ifdef INET
50306a27c376SRandall Stewart 		if (addr->sa_family == AF_INET) {
50316a27c376SRandall Stewart 			if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
50326a27c376SRandall Stewart 			    sctp_ifap->address.sin.sin_addr.s_addr) {
50336a27c376SRandall Stewart 				/* found him. */
503442551e99SRandall Stewart 				if (holds_lock == 0)
5035c99efcf6SRandall Stewart 					SCTP_IPI_ADDR_RUNLOCK();
503642551e99SRandall Stewart 				return (sctp_ifap);
50376a27c376SRandall Stewart 				break;
50386a27c376SRandall Stewart 			}
50395e2c2d87SRandall Stewart 		}
5040e6194c2eSMichael Tuexen #endif
50415e2c2d87SRandall Stewart #ifdef INET6
50425e2c2d87SRandall Stewart 		if (addr->sa_family == AF_INET6) {
5043c54a18d2SRandall Stewart 			if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5044c54a18d2SRandall Stewart 			    &sctp_ifap->address.sin6)) {
50456a27c376SRandall Stewart 				/* found him. */
50466a27c376SRandall Stewart 				if (holds_lock == 0)
5047c99efcf6SRandall Stewart 					SCTP_IPI_ADDR_RUNLOCK();
50486a27c376SRandall Stewart 				return (sctp_ifap);
50496a27c376SRandall Stewart 				break;
50506a27c376SRandall Stewart 			}
505142551e99SRandall Stewart 		}
50525e2c2d87SRandall Stewart #endif
505342551e99SRandall Stewart 	}
505442551e99SRandall Stewart 	if (holds_lock == 0)
5055c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RUNLOCK();
5056f8829a4aSRandall Stewart 	return (NULL);
5057f8829a4aSRandall Stewart }
5058f8829a4aSRandall Stewart 
5059f8829a4aSRandall Stewart static void
50604c9179adSRandall Stewart sctp_user_rcvd(struct sctp_tcb *stcb, uint32_t * freed_so_far, int hold_rlock,
5061f8829a4aSRandall Stewart     uint32_t rwnd_req)
5062f8829a4aSRandall Stewart {
5063f8829a4aSRandall Stewart 	/* User pulled some data, do we need a rwnd update? */
5064f8829a4aSRandall Stewart 	int r_unlocked = 0;
5065f8829a4aSRandall Stewart 	uint32_t dif, rwnd;
5066f8829a4aSRandall Stewart 	struct socket *so = NULL;
5067f8829a4aSRandall Stewart 
5068f8829a4aSRandall Stewart 	if (stcb == NULL)
5069f8829a4aSRandall Stewart 		return;
5070f8829a4aSRandall Stewart 
507150cec919SRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, 1);
5072f8829a4aSRandall Stewart 
507362c1ff9cSRandall Stewart 	if (stcb->asoc.state & (SCTP_STATE_ABOUT_TO_BE_FREED |
507462c1ff9cSRandall Stewart 	    SCTP_STATE_SHUTDOWN_RECEIVED |
50754c9179adSRandall Stewart 	    SCTP_STATE_SHUTDOWN_ACK_SENT)) {
5076f8829a4aSRandall Stewart 		/* Pre-check If we are freeing no update */
5077f8829a4aSRandall Stewart 		goto no_lock;
5078f8829a4aSRandall Stewart 	}
5079f8829a4aSRandall Stewart 	SCTP_INP_INCR_REF(stcb->sctp_ep);
5080f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5081f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5082f8829a4aSRandall Stewart 		goto out;
5083f8829a4aSRandall Stewart 	}
5084f8829a4aSRandall Stewart 	so = stcb->sctp_socket;
5085f8829a4aSRandall Stewart 	if (so == NULL) {
5086f8829a4aSRandall Stewart 		goto out;
5087f8829a4aSRandall Stewart 	}
5088f8829a4aSRandall Stewart 	atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far);
5089f8829a4aSRandall Stewart 	/* Have you have freed enough to look */
5090f8829a4aSRandall Stewart 	*freed_so_far = 0;
5091f8829a4aSRandall Stewart 	/* Yep, its worth a look and the lock overhead */
5092f8829a4aSRandall Stewart 
5093f8829a4aSRandall Stewart 	/* Figure out what the rwnd would be */
5094f8829a4aSRandall Stewart 	rwnd = sctp_calc_rwnd(stcb, &stcb->asoc);
5095f8829a4aSRandall Stewart 	if (rwnd >= stcb->asoc.my_last_reported_rwnd) {
5096f8829a4aSRandall Stewart 		dif = rwnd - stcb->asoc.my_last_reported_rwnd;
5097f8829a4aSRandall Stewart 	} else {
5098f8829a4aSRandall Stewart 		dif = 0;
5099f8829a4aSRandall Stewart 	}
5100f8829a4aSRandall Stewart 	if (dif >= rwnd_req) {
5101f8829a4aSRandall Stewart 		if (hold_rlock) {
5102f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5103f8829a4aSRandall Stewart 			r_unlocked = 1;
5104f8829a4aSRandall Stewart 		}
5105f8829a4aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5106f8829a4aSRandall Stewart 			/*
5107f8829a4aSRandall Stewart 			 * One last check before we allow the guy possibly
5108f8829a4aSRandall Stewart 			 * to get in. There is a race, where the guy has not
5109f8829a4aSRandall Stewart 			 * reached the gate. In that case
5110f8829a4aSRandall Stewart 			 */
5111f8829a4aSRandall Stewart 			goto out;
5112f8829a4aSRandall Stewart 		}
5113f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
5114f8829a4aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5115f8829a4aSRandall Stewart 			/* No reports here */
5116f8829a4aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
5117f8829a4aSRandall Stewart 			goto out;
5118f8829a4aSRandall Stewart 		}
5119f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_wu_sacks_sent);
5120689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_LOCKED);
5121830d754dSRandall Stewart 
5122f8829a4aSRandall Stewart 		sctp_chunk_output(stcb->sctp_ep, stcb,
5123ceaad40aSRandall Stewart 		    SCTP_OUTPUT_FROM_USR_RCVD, SCTP_SO_LOCKED);
5124f8829a4aSRandall Stewart 		/* make sure no timer is running */
5125a5d547adSRandall Stewart 		sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_6);
5126f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
5127f8829a4aSRandall Stewart 	} else {
5128f8829a4aSRandall Stewart 		/* Update how much we have pending */
5129f8829a4aSRandall Stewart 		stcb->freed_by_sorcv_sincelast = dif;
5130f8829a4aSRandall Stewart 	}
5131f8829a4aSRandall Stewart out:
5132f8829a4aSRandall Stewart 	if (so && r_unlocked && hold_rlock) {
5133f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
5134f8829a4aSRandall Stewart 	}
5135f8829a4aSRandall Stewart 	SCTP_INP_DECR_REF(stcb->sctp_ep);
5136f8829a4aSRandall Stewart no_lock:
513750cec919SRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, -1);
5138f8829a4aSRandall Stewart 	return;
5139f8829a4aSRandall Stewart }
5140f8829a4aSRandall Stewart 
5141f8829a4aSRandall Stewart int
5142f8829a4aSRandall Stewart sctp_sorecvmsg(struct socket *so,
5143f8829a4aSRandall Stewart     struct uio *uio,
5144f8829a4aSRandall Stewart     struct mbuf **mp,
5145f8829a4aSRandall Stewart     struct sockaddr *from,
5146f8829a4aSRandall Stewart     int fromlen,
5147f8829a4aSRandall Stewart     int *msg_flags,
5148f8829a4aSRandall Stewart     struct sctp_sndrcvinfo *sinfo,
5149f8829a4aSRandall Stewart     int filling_sinfo)
5150f8829a4aSRandall Stewart {
5151f8829a4aSRandall Stewart 	/*
5152f8829a4aSRandall Stewart 	 * MSG flags we will look at MSG_DONTWAIT - non-blocking IO.
5153f8829a4aSRandall Stewart 	 * MSG_PEEK - Look don't touch :-D (only valid with OUT mbuf copy
5154f8829a4aSRandall Stewart 	 * mp=NULL thus uio is the copy method to userland) MSG_WAITALL - ??
5155f8829a4aSRandall Stewart 	 * On the way out we may send out any combination of:
5156f8829a4aSRandall Stewart 	 * MSG_NOTIFICATION MSG_EOR
5157f8829a4aSRandall Stewart 	 *
5158f8829a4aSRandall Stewart 	 */
5159f8829a4aSRandall Stewart 	struct sctp_inpcb *inp = NULL;
5160f8829a4aSRandall Stewart 	int my_len = 0;
5161f8829a4aSRandall Stewart 	int cp_len = 0, error = 0;
5162f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL;
516394b0d969SMichael Tuexen 	struct mbuf *m = NULL;
5164f8829a4aSRandall Stewart 	struct sctp_tcb *stcb = NULL;
5165f8829a4aSRandall Stewart 	int wakeup_read_socket = 0;
5166f8829a4aSRandall Stewart 	int freecnt_applied = 0;
5167f8829a4aSRandall Stewart 	int out_flags = 0, in_flags = 0;
5168f8829a4aSRandall Stewart 	int block_allowed = 1;
51694c9179adSRandall Stewart 	uint32_t freed_so_far = 0;
517081aca91aSRandall Stewart 	uint32_t copied_so_far = 0;
517193164cf9SRandall Stewart 	int in_eeor_mode = 0;
5172f8829a4aSRandall Stewart 	int no_rcv_needed = 0;
5173f8829a4aSRandall Stewart 	uint32_t rwnd_req = 0;
5174f8829a4aSRandall Stewart 	int hold_sblock = 0;
5175f8829a4aSRandall Stewart 	int hold_rlock = 0;
517642551e99SRandall Stewart 	int slen = 0;
51774c9179adSRandall Stewart 	uint32_t held_length = 0;
51787abab911SRobert Watson 	int sockbuf_lock = 0;
5179f8829a4aSRandall Stewart 
518017205eccSRandall Stewart 	if (uio == NULL) {
5181c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
518217205eccSRandall Stewart 		return (EINVAL);
518317205eccSRandall Stewart 	}
5184f8829a4aSRandall Stewart 	if (msg_flags) {
5185f8829a4aSRandall Stewart 		in_flags = *msg_flags;
5186c105859eSRandall Stewart 		if (in_flags & MSG_PEEK)
5187c105859eSRandall Stewart 			SCTP_STAT_INCR(sctps_read_peeks);
5188f8829a4aSRandall Stewart 	} else {
5189f8829a4aSRandall Stewart 		in_flags = 0;
5190f8829a4aSRandall Stewart 	}
5191f8829a4aSRandall Stewart 	slen = uio->uio_resid;
519217205eccSRandall Stewart 
5193f8829a4aSRandall Stewart 	/* Pull in and set up our int flags */
5194f8829a4aSRandall Stewart 	if (in_flags & MSG_OOB) {
5195f8829a4aSRandall Stewart 		/* Out of band's NOT supported */
5196f8829a4aSRandall Stewart 		return (EOPNOTSUPP);
5197f8829a4aSRandall Stewart 	}
5198f8829a4aSRandall Stewart 	if ((in_flags & MSG_PEEK) && (mp != NULL)) {
5199c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5200f8829a4aSRandall Stewart 		return (EINVAL);
5201f8829a4aSRandall Stewart 	}
5202f8829a4aSRandall Stewart 	if ((in_flags & (MSG_DONTWAIT
5203f8829a4aSRandall Stewart 	    | MSG_NBIO
5204f8829a4aSRandall Stewart 	    )) ||
520542551e99SRandall Stewart 	    SCTP_SO_IS_NBIO(so)) {
5206f8829a4aSRandall Stewart 		block_allowed = 0;
5207f8829a4aSRandall Stewart 	}
5208f8829a4aSRandall Stewart 	/* setup the endpoint */
5209f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
5210f8829a4aSRandall Stewart 	if (inp == NULL) {
5211c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
5212f8829a4aSRandall Stewart 		return (EFAULT);
5213f8829a4aSRandall Stewart 	}
521462c1ff9cSRandall Stewart 	rwnd_req = (SCTP_SB_LIMIT_RCV(so) >> SCTP_RWND_HIWAT_SHIFT);
5215f8829a4aSRandall Stewart 	/* Must be at least a MTU's worth */
5216f8829a4aSRandall Stewart 	if (rwnd_req < SCTP_MIN_RWND)
5217f8829a4aSRandall Stewart 		rwnd_req = SCTP_MIN_RWND;
5218f8829a4aSRandall Stewart 	in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
5219b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5220f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SORECV_ENTER,
522117205eccSRandall Stewart 		    rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, uio->uio_resid);
522280fefe0aSRandall Stewart 	}
5223b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5224f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SORECV_ENTERPL,
522517205eccSRandall Stewart 		    rwnd_req, block_allowed, so->so_rcv.sb_cc, uio->uio_resid);
522680fefe0aSRandall Stewart 	}
5227265de5bbSRobert Watson 	error = sblock(&so->so_rcv, (block_allowed ? SBL_WAIT : 0));
5228f8829a4aSRandall Stewart 	if (error) {
5229f8829a4aSRandall Stewart 		goto release_unlocked;
5230f8829a4aSRandall Stewart 	}
52318e1e6e5fSMateusz Guzik 	sockbuf_lock = 1;
5232f8829a4aSRandall Stewart restart:
52337abab911SRobert Watson 
5234f8829a4aSRandall Stewart 
5235f8829a4aSRandall Stewart restart_nosblocks:
5236f8829a4aSRandall Stewart 	if (hold_sblock == 0) {
5237f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&so->so_rcv);
5238f8829a4aSRandall Stewart 		hold_sblock = 1;
5239f8829a4aSRandall Stewart 	}
5240f8829a4aSRandall Stewart 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5241f8829a4aSRandall Stewart 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5242f8829a4aSRandall Stewart 		goto out;
5243f8829a4aSRandall Stewart 	}
524458411b08SMichael Tuexen 	if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) && (so->so_rcv.sb_cc == 0)) {
5245f8829a4aSRandall Stewart 		if (so->so_error) {
5246f8829a4aSRandall Stewart 			error = so->so_error;
524744b7479bSRandall Stewart 			if ((in_flags & MSG_PEEK) == 0)
524844b7479bSRandall Stewart 				so->so_error = 0;
52499f22f500SRandall Stewart 			goto out;
5250f8829a4aSRandall Stewart 		} else {
52519f22f500SRandall Stewart 			if (so->so_rcv.sb_cc == 0) {
52527924093fSRandall Stewart 				/* indicate EOF */
52537924093fSRandall Stewart 				error = 0;
5254f8829a4aSRandall Stewart 				goto out;
5255f8829a4aSRandall Stewart 			}
52569f22f500SRandall Stewart 		}
52579f22f500SRandall Stewart 	}
5258f8829a4aSRandall Stewart 	if ((so->so_rcv.sb_cc <= held_length) && block_allowed) {
5259f8829a4aSRandall Stewart 		/* we need to wait for data */
5260f8829a4aSRandall Stewart 		if ((so->so_rcv.sb_cc == 0) &&
5261f8829a4aSRandall Stewart 		    ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5262f8829a4aSRandall Stewart 		    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
5263f8829a4aSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
5264f8829a4aSRandall Stewart 				/*
5265f8829a4aSRandall Stewart 				 * For active open side clear flags for
5266f8829a4aSRandall Stewart 				 * re-use passive open is blocked by
5267f8829a4aSRandall Stewart 				 * connect.
5268f8829a4aSRandall Stewart 				 */
5269f8829a4aSRandall Stewart 				if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
5270f8829a4aSRandall Stewart 					/*
5271f8829a4aSRandall Stewart 					 * You were aborted, passive side
5272f8829a4aSRandall Stewart 					 * always hits here
5273f8829a4aSRandall Stewart 					 */
5274c4739e2fSRandall Stewart 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
5275f8829a4aSRandall Stewart 					error = ECONNRESET;
5276f8829a4aSRandall Stewart 				}
5277f8829a4aSRandall Stewart 				so->so_state &= ~(SS_ISCONNECTING |
5278f8829a4aSRandall Stewart 				    SS_ISDISCONNECTING |
5279f8829a4aSRandall Stewart 				    SS_ISCONFIRMING |
5280f8829a4aSRandall Stewart 				    SS_ISCONNECTED);
5281f8829a4aSRandall Stewart 				if (error == 0) {
5282f8829a4aSRandall Stewart 					if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5283c4739e2fSRandall Stewart 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5284f8829a4aSRandall Stewart 						error = ENOTCONN;
5285f8829a4aSRandall Stewart 					}
5286f8829a4aSRandall Stewart 				}
5287f8829a4aSRandall Stewart 				goto out;
5288f8829a4aSRandall Stewart 			}
5289f8829a4aSRandall Stewart 		}
5290f8829a4aSRandall Stewart 		error = sbwait(&so->so_rcv);
5291f8829a4aSRandall Stewart 		if (error) {
5292f8829a4aSRandall Stewart 			goto out;
5293f8829a4aSRandall Stewart 		}
5294f8829a4aSRandall Stewart 		held_length = 0;
5295f8829a4aSRandall Stewart 		goto restart_nosblocks;
5296f8829a4aSRandall Stewart 	} else if (so->so_rcv.sb_cc == 0) {
529744b7479bSRandall Stewart 		if (so->so_error) {
529844b7479bSRandall Stewart 			error = so->so_error;
529944b7479bSRandall Stewart 			if ((in_flags & MSG_PEEK) == 0)
530044b7479bSRandall Stewart 				so->so_error = 0;
530144b7479bSRandall Stewart 		} else {
530244b7479bSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
530344b7479bSRandall Stewart 			    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
530444b7479bSRandall Stewart 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
530544b7479bSRandall Stewart 					/*
530644b7479bSRandall Stewart 					 * For active open side clear flags
530744b7479bSRandall Stewart 					 * for re-use passive open is
530844b7479bSRandall Stewart 					 * blocked by connect.
530944b7479bSRandall Stewart 					 */
531044b7479bSRandall Stewart 					if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
531144b7479bSRandall Stewart 						/*
531244b7479bSRandall Stewart 						 * You were aborted, passive
531344b7479bSRandall Stewart 						 * side always hits here
531444b7479bSRandall Stewart 						 */
5315c4739e2fSRandall Stewart 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
531644b7479bSRandall Stewart 						error = ECONNRESET;
531744b7479bSRandall Stewart 					}
531844b7479bSRandall Stewart 					so->so_state &= ~(SS_ISCONNECTING |
531944b7479bSRandall Stewart 					    SS_ISDISCONNECTING |
532044b7479bSRandall Stewart 					    SS_ISCONFIRMING |
532144b7479bSRandall Stewart 					    SS_ISCONNECTED);
532244b7479bSRandall Stewart 					if (error == 0) {
532344b7479bSRandall Stewart 						if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5324c4739e2fSRandall Stewart 							SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
532544b7479bSRandall Stewart 							error = ENOTCONN;
532644b7479bSRandall Stewart 						}
532744b7479bSRandall Stewart 					}
532844b7479bSRandall Stewart 					goto out;
532944b7479bSRandall Stewart 				}
533044b7479bSRandall Stewart 			}
5331c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EWOULDBLOCK);
5332f8829a4aSRandall Stewart 			error = EWOULDBLOCK;
533344b7479bSRandall Stewart 		}
5334f8829a4aSRandall Stewart 		goto out;
5335f8829a4aSRandall Stewart 	}
5336d06c82f1SRandall Stewart 	if (hold_sblock == 1) {
5337d06c82f1SRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
5338d06c82f1SRandall Stewart 		hold_sblock = 0;
5339d06c82f1SRandall Stewart 	}
5340f8829a4aSRandall Stewart 	/* we possibly have data we can read */
53413c503c28SRandall Stewart 	/* sa_ignore FREED_MEMORY */
5342f8829a4aSRandall Stewart 	control = TAILQ_FIRST(&inp->read_queue);
5343f8829a4aSRandall Stewart 	if (control == NULL) {
5344f8829a4aSRandall Stewart 		/*
5345f8829a4aSRandall Stewart 		 * This could be happening since the appender did the
5346f8829a4aSRandall Stewart 		 * increment but as not yet did the tailq insert onto the
5347f8829a4aSRandall Stewart 		 * read_queue
5348f8829a4aSRandall Stewart 		 */
5349f8829a4aSRandall Stewart 		if (hold_rlock == 0) {
5350f8829a4aSRandall Stewart 			SCTP_INP_READ_LOCK(inp);
5351f8829a4aSRandall Stewart 		}
5352f8829a4aSRandall Stewart 		control = TAILQ_FIRST(&inp->read_queue);
5353f8829a4aSRandall Stewart 		if ((control == NULL) && (so->so_rcv.sb_cc != 0)) {
5354a5d547adSRandall Stewart #ifdef INVARIANTS
5355f8829a4aSRandall Stewart 			panic("Huh, its non zero and nothing on control?");
5356f8829a4aSRandall Stewart #endif
5357f8829a4aSRandall Stewart 			so->so_rcv.sb_cc = 0;
5358f8829a4aSRandall Stewart 		}
5359f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
5360f8829a4aSRandall Stewart 		hold_rlock = 0;
5361f8829a4aSRandall Stewart 		goto restart;
5362f8829a4aSRandall Stewart 	}
5363f8829a4aSRandall Stewart 	if ((control->length == 0) &&
5364f8829a4aSRandall Stewart 	    (control->do_not_ref_stcb)) {
5365f8829a4aSRandall Stewart 		/*
5366f8829a4aSRandall Stewart 		 * Clean up code for freeing assoc that left behind a
5367f8829a4aSRandall Stewart 		 * pdapi.. maybe a peer in EEOR that just closed after
5368f8829a4aSRandall Stewart 		 * sending and never indicated a EOR.
5369f8829a4aSRandall Stewart 		 */
5370f8829a4aSRandall Stewart 		if (hold_rlock == 0) {
5371f8829a4aSRandall Stewart 			hold_rlock = 1;
5372f8829a4aSRandall Stewart 			SCTP_INP_READ_LOCK(inp);
5373f8829a4aSRandall Stewart 		}
5374f8829a4aSRandall Stewart 		control->held_length = 0;
5375f8829a4aSRandall Stewart 		if (control->data) {
5376f8829a4aSRandall Stewart 			/* Hmm there is data here .. fix */
53774c9179adSRandall Stewart 			struct mbuf *m_tmp;
5378f8829a4aSRandall Stewart 			int cnt = 0;
5379f8829a4aSRandall Stewart 
53804c9179adSRandall Stewart 			m_tmp = control->data;
53814c9179adSRandall Stewart 			while (m_tmp) {
53824c9179adSRandall Stewart 				cnt += SCTP_BUF_LEN(m_tmp);
53834c9179adSRandall Stewart 				if (SCTP_BUF_NEXT(m_tmp) == NULL) {
53844c9179adSRandall Stewart 					control->tail_mbuf = m_tmp;
5385f8829a4aSRandall Stewart 					control->end_added = 1;
5386f8829a4aSRandall Stewart 				}
53874c9179adSRandall Stewart 				m_tmp = SCTP_BUF_NEXT(m_tmp);
5388f8829a4aSRandall Stewart 			}
5389f8829a4aSRandall Stewart 			control->length = cnt;
5390f8829a4aSRandall Stewart 		} else {
5391f8829a4aSRandall Stewart 			/* remove it */
5392f8829a4aSRandall Stewart 			TAILQ_REMOVE(&inp->read_queue, control, next);
5393f8829a4aSRandall Stewart 			/* Add back any hiddend data */
5394f8829a4aSRandall Stewart 			sctp_free_remote_addr(control->whoFrom);
5395f8829a4aSRandall Stewart 			sctp_free_a_readq(stcb, control);
5396f8829a4aSRandall Stewart 		}
5397f8829a4aSRandall Stewart 		if (hold_rlock) {
5398f8829a4aSRandall Stewart 			hold_rlock = 0;
5399f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
5400f8829a4aSRandall Stewart 		}
5401f8829a4aSRandall Stewart 		goto restart;
5402f8829a4aSRandall Stewart 	}
5403810ec536SMichael Tuexen 	if ((control->length == 0) &&
5404810ec536SMichael Tuexen 	    (control->end_added == 1)) {
5405810ec536SMichael Tuexen 		/*
5406810ec536SMichael Tuexen 		 * Do we also need to check for (control->pdapi_aborted ==
5407810ec536SMichael Tuexen 		 * 1)?
5408810ec536SMichael Tuexen 		 */
5409810ec536SMichael Tuexen 		if (hold_rlock == 0) {
5410810ec536SMichael Tuexen 			hold_rlock = 1;
5411810ec536SMichael Tuexen 			SCTP_INP_READ_LOCK(inp);
5412810ec536SMichael Tuexen 		}
5413810ec536SMichael Tuexen 		TAILQ_REMOVE(&inp->read_queue, control, next);
5414810ec536SMichael Tuexen 		if (control->data) {
5415810ec536SMichael Tuexen #ifdef INVARIANTS
5416810ec536SMichael Tuexen 			panic("control->data not null but control->length == 0");
5417810ec536SMichael Tuexen #else
5418810ec536SMichael Tuexen 			SCTP_PRINTF("Strange, data left in the control buffer. Cleaning up.\n");
5419810ec536SMichael Tuexen 			sctp_m_freem(control->data);
5420810ec536SMichael Tuexen 			control->data = NULL;
5421810ec536SMichael Tuexen #endif
5422810ec536SMichael Tuexen 		}
5423810ec536SMichael Tuexen 		if (control->aux_data) {
5424810ec536SMichael Tuexen 			sctp_m_free(control->aux_data);
5425810ec536SMichael Tuexen 			control->aux_data = NULL;
5426810ec536SMichael Tuexen 		}
5427810ec536SMichael Tuexen 		sctp_free_remote_addr(control->whoFrom);
5428810ec536SMichael Tuexen 		sctp_free_a_readq(stcb, control);
5429810ec536SMichael Tuexen 		if (hold_rlock) {
5430810ec536SMichael Tuexen 			hold_rlock = 0;
5431810ec536SMichael Tuexen 			SCTP_INP_READ_UNLOCK(inp);
5432810ec536SMichael Tuexen 		}
5433810ec536SMichael Tuexen 		goto restart;
5434810ec536SMichael Tuexen 	}
5435f8829a4aSRandall Stewart 	if (control->length == 0) {
5436f8829a4aSRandall Stewart 		if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
5437f8829a4aSRandall Stewart 		    (filling_sinfo)) {
5438f8829a4aSRandall Stewart 			/* find a more suitable one then this */
5439f8829a4aSRandall Stewart 			ctl = TAILQ_NEXT(control, next);
5440f8829a4aSRandall Stewart 			while (ctl) {
54419a6142d8SRandall Stewart 				if ((ctl->stcb != control->stcb) && (ctl->length) &&
54429a6142d8SRandall Stewart 				    (ctl->some_taken ||
54436114cd96SRandall Stewart 				    (ctl->spec_flags & M_NOTIFICATION) ||
54449a6142d8SRandall Stewart 				    ((ctl->do_not_ref_stcb == 0) &&
54459a6142d8SRandall Stewart 				    (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))
54469a6142d8SRandall Stewart 				    ) {
54479a6142d8SRandall Stewart 					/*-
54489a6142d8SRandall Stewart 					 * If we have a different TCB next, and there is data
54499a6142d8SRandall Stewart 					 * present. If we have already taken some (pdapi), OR we can
54509a6142d8SRandall Stewart 					 * ref the tcb and no delivery as started on this stream, we
545117205eccSRandall Stewart 					 * take it. Note we allow a notification on a different
545217205eccSRandall Stewart 					 * assoc to be delivered..
54539a6142d8SRandall Stewart 					 */
54549a6142d8SRandall Stewart 					control = ctl;
54559a6142d8SRandall Stewart 					goto found_one;
54569a6142d8SRandall Stewart 				} else if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) &&
54579a6142d8SRandall Stewart 					    (ctl->length) &&
54589a6142d8SRandall Stewart 					    ((ctl->some_taken) ||
54599a6142d8SRandall Stewart 					    ((ctl->do_not_ref_stcb == 0) &&
546017205eccSRandall Stewart 					    ((ctl->spec_flags & M_NOTIFICATION) == 0) &&
5461b5c16493SMichael Tuexen 				    (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))) {
54629a6142d8SRandall Stewart 					/*-
54639a6142d8SRandall Stewart 					 * If we have the same tcb, and there is data present, and we
54649a6142d8SRandall Stewart 					 * have the strm interleave feature present. Then if we have
54659a6142d8SRandall Stewart 					 * taken some (pdapi) or we can refer to tht tcb AND we have
54669a6142d8SRandall Stewart 					 * not started a delivery for this stream, we can take it.
546717205eccSRandall Stewart 					 * Note we do NOT allow a notificaiton on the same assoc to
546817205eccSRandall Stewart 					 * be delivered.
54699a6142d8SRandall Stewart 					 */
5470f8829a4aSRandall Stewart 					control = ctl;
5471f8829a4aSRandall Stewart 					goto found_one;
5472f8829a4aSRandall Stewart 				}
5473f8829a4aSRandall Stewart 				ctl = TAILQ_NEXT(ctl, next);
5474f8829a4aSRandall Stewart 			}
5475f8829a4aSRandall Stewart 		}
5476f8829a4aSRandall Stewart 		/*
5477f8829a4aSRandall Stewart 		 * if we reach here, not suitable replacement is available
5478f8829a4aSRandall Stewart 		 * <or> fragment interleave is NOT on. So stuff the sb_cc
5479f8829a4aSRandall Stewart 		 * into the our held count, and its time to sleep again.
5480f8829a4aSRandall Stewart 		 */
5481f8829a4aSRandall Stewart 		held_length = so->so_rcv.sb_cc;
5482f8829a4aSRandall Stewart 		control->held_length = so->so_rcv.sb_cc;
5483f8829a4aSRandall Stewart 		goto restart;
5484f8829a4aSRandall Stewart 	}
5485f8829a4aSRandall Stewart 	/* Clear the held length since there is something to read */
5486f8829a4aSRandall Stewart 	control->held_length = 0;
5487f8829a4aSRandall Stewart 	if (hold_rlock) {
5488f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
5489f8829a4aSRandall Stewart 		hold_rlock = 0;
5490f8829a4aSRandall Stewart 	}
5491f8829a4aSRandall Stewart found_one:
5492f8829a4aSRandall Stewart 	/*
5493f8829a4aSRandall Stewart 	 * If we reach here, control has a some data for us to read off.
5494f8829a4aSRandall Stewart 	 * Note that stcb COULD be NULL.
5495f8829a4aSRandall Stewart 	 */
54969c04b296SRandall Stewart 	control->some_taken++;
5497f8829a4aSRandall Stewart 	if (hold_sblock) {
5498f8829a4aSRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
5499f8829a4aSRandall Stewart 		hold_sblock = 0;
5500f8829a4aSRandall Stewart 	}
5501f8829a4aSRandall Stewart 	stcb = control->stcb;
5502f8829a4aSRandall Stewart 	if (stcb) {
55030696e120SRandall Stewart 		if ((control->do_not_ref_stcb == 0) &&
55040696e120SRandall Stewart 		    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
550550cec919SRandall Stewart 			if (freecnt_applied == 0)
5506f8829a4aSRandall Stewart 				stcb = NULL;
5507f8829a4aSRandall Stewart 		} else if (control->do_not_ref_stcb == 0) {
5508f8829a4aSRandall Stewart 			/* you can't free it on me please */
5509f8829a4aSRandall Stewart 			/*
5510f8829a4aSRandall Stewart 			 * The lock on the socket buffer protects us so the
5511f8829a4aSRandall Stewart 			 * free code will stop. But since we used the
5512f8829a4aSRandall Stewart 			 * socketbuf lock and the sender uses the tcb_lock
5513f8829a4aSRandall Stewart 			 * to increment, we need to use the atomic add to
5514f8829a4aSRandall Stewart 			 * the refcnt
5515f8829a4aSRandall Stewart 			 */
5516d55b0b1bSRandall Stewart 			if (freecnt_applied) {
5517d55b0b1bSRandall Stewart #ifdef INVARIANTS
5518207304d4SRandall Stewart 				panic("refcnt already incremented");
5519d55b0b1bSRandall Stewart #else
5520cd3fd531SMichael Tuexen 				SCTP_PRINTF("refcnt already incremented?\n");
5521d55b0b1bSRandall Stewart #endif
5522d55b0b1bSRandall Stewart 			} else {
552350cec919SRandall Stewart 				atomic_add_int(&stcb->asoc.refcnt, 1);
5524f8829a4aSRandall Stewart 				freecnt_applied = 1;
5525d55b0b1bSRandall Stewart 			}
5526f8829a4aSRandall Stewart 			/*
5527f8829a4aSRandall Stewart 			 * Setup to remember how much we have not yet told
5528f8829a4aSRandall Stewart 			 * the peer our rwnd has opened up. Note we grab the
5529f8829a4aSRandall Stewart 			 * value from the tcb from last time. Note too that
55300696e120SRandall Stewart 			 * sack sending clears this when a sack is sent,
5531f8829a4aSRandall Stewart 			 * which is fine. Once we hit the rwnd_req, we then
5532f8829a4aSRandall Stewart 			 * will go to the sctp_user_rcvd() that will not
5533f8829a4aSRandall Stewart 			 * lock until it KNOWs it MUST send a WUP-SACK.
5534f8829a4aSRandall Stewart 			 */
5535f8829a4aSRandall Stewart 			freed_so_far = stcb->freed_by_sorcv_sincelast;
5536f8829a4aSRandall Stewart 			stcb->freed_by_sorcv_sincelast = 0;
5537f8829a4aSRandall Stewart 		}
5538f8829a4aSRandall Stewart 	}
55396114cd96SRandall Stewart 	if (stcb &&
55406114cd96SRandall Stewart 	    ((control->spec_flags & M_NOTIFICATION) == 0) &&
55416114cd96SRandall Stewart 	    control->do_not_ref_stcb == 0) {
5542d06c82f1SRandall Stewart 		stcb->asoc.strmin[control->sinfo_stream].delivery_started = 1;
5543d06c82f1SRandall Stewart 	}
5544f8829a4aSRandall Stewart 	/* First lets get off the sinfo and sockaddr info */
5545f8829a4aSRandall Stewart 	if ((sinfo) && filling_sinfo) {
5546f8829a4aSRandall Stewart 		memcpy(sinfo, control, sizeof(struct sctp_nonpad_sndrcvinfo));
5547f8829a4aSRandall Stewart 		nxt = TAILQ_NEXT(control, next);
5548e2e7c62eSMichael Tuexen 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
5549e2e7c62eSMichael Tuexen 		    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) {
5550f8829a4aSRandall Stewart 			struct sctp_extrcvinfo *s_extra;
5551f8829a4aSRandall Stewart 
5552f8829a4aSRandall Stewart 			s_extra = (struct sctp_extrcvinfo *)sinfo;
55539a6142d8SRandall Stewart 			if ((nxt) &&
55549a6142d8SRandall Stewart 			    (nxt->length)) {
55559a6142d8SRandall Stewart 				s_extra->sreinfo_next_flags = SCTP_NEXT_MSG_AVAIL;
5556f8829a4aSRandall Stewart 				if (nxt->sinfo_flags & SCTP_UNORDERED) {
55579a6142d8SRandall Stewart 					s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_IS_UNORDERED;
5558f8829a4aSRandall Stewart 				}
5559f42a358aSRandall Stewart 				if (nxt->spec_flags & M_NOTIFICATION) {
55609a6142d8SRandall Stewart 					s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_IS_NOTIFICATION;
5561f42a358aSRandall Stewart 				}
55629a6142d8SRandall Stewart 				s_extra->sreinfo_next_aid = nxt->sinfo_assoc_id;
55639a6142d8SRandall Stewart 				s_extra->sreinfo_next_length = nxt->length;
55649a6142d8SRandall Stewart 				s_extra->sreinfo_next_ppid = nxt->sinfo_ppid;
55659a6142d8SRandall Stewart 				s_extra->sreinfo_next_stream = nxt->sinfo_stream;
5566f8829a4aSRandall Stewart 				if (nxt->tail_mbuf != NULL) {
5567139bc87fSRandall Stewart 					if (nxt->end_added) {
55689a6142d8SRandall Stewart 						s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_ISCOMPLETE;
5569f8829a4aSRandall Stewart 					}
5570f8829a4aSRandall Stewart 				}
5571f8829a4aSRandall Stewart 			} else {
5572f8829a4aSRandall Stewart 				/*
5573f8829a4aSRandall Stewart 				 * we explicitly 0 this, since the memcpy
5574f8829a4aSRandall Stewart 				 * got some other things beyond the older
5575f8829a4aSRandall Stewart 				 * sinfo_ that is on the control's structure
5576f8829a4aSRandall Stewart 				 * :-D
5577f8829a4aSRandall Stewart 				 */
55789a6142d8SRandall Stewart 				nxt = NULL;
55799a6142d8SRandall Stewart 				s_extra->sreinfo_next_flags = SCTP_NO_NEXT_MSG;
55809a6142d8SRandall Stewart 				s_extra->sreinfo_next_aid = 0;
55819a6142d8SRandall Stewart 				s_extra->sreinfo_next_length = 0;
55829a6142d8SRandall Stewart 				s_extra->sreinfo_next_ppid = 0;
55839a6142d8SRandall Stewart 				s_extra->sreinfo_next_stream = 0;
5584f8829a4aSRandall Stewart 			}
5585f8829a4aSRandall Stewart 		}
5586f8829a4aSRandall Stewart 		/*
5587f8829a4aSRandall Stewart 		 * update off the real current cum-ack, if we have an stcb.
5588f8829a4aSRandall Stewart 		 */
55890696e120SRandall Stewart 		if ((control->do_not_ref_stcb == 0) && stcb)
5590f8829a4aSRandall Stewart 			sinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
5591f8829a4aSRandall Stewart 		/*
5592f8829a4aSRandall Stewart 		 * mask off the high bits, we keep the actual chunk bits in
5593f8829a4aSRandall Stewart 		 * there.
5594f8829a4aSRandall Stewart 		 */
5595f8829a4aSRandall Stewart 		sinfo->sinfo_flags &= 0x00ff;
55965f26a41dSRandall Stewart 		if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) {
55975f26a41dSRandall Stewart 			sinfo->sinfo_flags |= SCTP_UNORDERED;
55985f26a41dSRandall Stewart 		}
5599f8829a4aSRandall Stewart 	}
560018e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
560118e198d3SRandall Stewart 	{
560218e198d3SRandall Stewart 		int index, newindex;
560318e198d3SRandall Stewart 		struct sctp_pcbtsn_rlog *entry;
560418e198d3SRandall Stewart 
560518e198d3SRandall Stewart 		do {
560618e198d3SRandall Stewart 			index = inp->readlog_index;
560718e198d3SRandall Stewart 			newindex = index + 1;
560818e198d3SRandall Stewart 			if (newindex >= SCTP_READ_LOG_SIZE) {
560918e198d3SRandall Stewart 				newindex = 0;
561018e198d3SRandall Stewart 			}
561118e198d3SRandall Stewart 		} while (atomic_cmpset_int(&inp->readlog_index, index, newindex) == 0);
561218e198d3SRandall Stewart 		entry = &inp->readlog[index];
561318e198d3SRandall Stewart 		entry->vtag = control->sinfo_assoc_id;
561418e198d3SRandall Stewart 		entry->strm = control->sinfo_stream;
561518e198d3SRandall Stewart 		entry->seq = control->sinfo_ssn;
561618e198d3SRandall Stewart 		entry->sz = control->length;
561718e198d3SRandall Stewart 		entry->flgs = control->sinfo_flags;
561818e198d3SRandall Stewart 	}
561918e198d3SRandall Stewart #endif
5620f8829a4aSRandall Stewart 	if (fromlen && from) {
5621b5b6e5c2SMichael Tuexen 		cp_len = min((size_t)fromlen, (size_t)control->whoFrom->ro._l_addr.sa.sa_len);
5622b5b6e5c2SMichael Tuexen 		switch (control->whoFrom->ro._l_addr.sa.sa_family) {
5623b5b6e5c2SMichael Tuexen #ifdef INET6
5624b5b6e5c2SMichael Tuexen 		case AF_INET6:
5625f8829a4aSRandall Stewart 			((struct sockaddr_in6 *)from)->sin6_port = control->port_from;
5626b5b6e5c2SMichael Tuexen 			break;
5627f8829a4aSRandall Stewart #endif
5628b5b6e5c2SMichael Tuexen #ifdef INET
5629b5b6e5c2SMichael Tuexen 		case AF_INET:
5630b5b6e5c2SMichael Tuexen 			((struct sockaddr_in *)from)->sin_port = control->port_from;
5631b5b6e5c2SMichael Tuexen 			break;
5632b5b6e5c2SMichael Tuexen #endif
5633b5b6e5c2SMichael Tuexen 		default:
5634b5b6e5c2SMichael Tuexen 			break;
5635b5b6e5c2SMichael Tuexen 		}
5636b5b6e5c2SMichael Tuexen 		memcpy(from, &control->whoFrom->ro._l_addr, cp_len);
5637f8829a4aSRandall Stewart 
563842551e99SRandall Stewart #if defined(INET) && defined(INET6)
56395e2c2d87SRandall Stewart 		if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
5640b5b6e5c2SMichael Tuexen 		    (from->sa_family == AF_INET) &&
5641f8829a4aSRandall Stewart 		    ((size_t)fromlen >= sizeof(struct sockaddr_in6))) {
5642f8829a4aSRandall Stewart 			struct sockaddr_in *sin;
5643f8829a4aSRandall Stewart 			struct sockaddr_in6 sin6;
5644f8829a4aSRandall Stewart 
5645b5b6e5c2SMichael Tuexen 			sin = (struct sockaddr_in *)from;
5646f8829a4aSRandall Stewart 			bzero(&sin6, sizeof(sin6));
5647f8829a4aSRandall Stewart 			sin6.sin6_family = AF_INET6;
5648f8829a4aSRandall Stewart 			sin6.sin6_len = sizeof(struct sockaddr_in6);
5649d6af161aSRandall Stewart 			sin6.sin6_addr.s6_addr32[2] = htonl(0xffff);
5650f8829a4aSRandall Stewart 			bcopy(&sin->sin_addr,
5651d6af161aSRandall Stewart 			    &sin6.sin6_addr.s6_addr32[3],
5652d6af161aSRandall Stewart 			    sizeof(sin6.sin6_addr.s6_addr32[3]));
5653f8829a4aSRandall Stewart 			sin6.sin6_port = sin->sin_port;
5654b5b6e5c2SMichael Tuexen 			memcpy(from, &sin6, sizeof(struct sockaddr_in6));
5655f8829a4aSRandall Stewart 		}
5656f8829a4aSRandall Stewart #endif
5657e0e00a4dSMichael Tuexen #ifdef INET6
5658f8829a4aSRandall Stewart 		{
5659b5b6e5c2SMichael Tuexen 			struct sockaddr_in6 lsa6, *from6;
5660f8829a4aSRandall Stewart 
5661b5b6e5c2SMichael Tuexen 			from6 = (struct sockaddr_in6 *)from;
5662b5b6e5c2SMichael Tuexen 			sctp_recover_scope_mac(from6, (&lsa6));
5663f8829a4aSRandall Stewart 		}
5664f8829a4aSRandall Stewart #endif
5665f8829a4aSRandall Stewart 	}
5666f8829a4aSRandall Stewart 	/* now copy out what data we can */
5667f8829a4aSRandall Stewart 	if (mp == NULL) {
5668f8829a4aSRandall Stewart 		/* copy out each mbuf in the chain up to length */
5669f8829a4aSRandall Stewart get_more_data:
5670f8829a4aSRandall Stewart 		m = control->data;
5671f8829a4aSRandall Stewart 		while (m) {
5672f8829a4aSRandall Stewart 			/* Move out all we can */
5673f8829a4aSRandall Stewart 			cp_len = (int)uio->uio_resid;
5674139bc87fSRandall Stewart 			my_len = (int)SCTP_BUF_LEN(m);
5675f8829a4aSRandall Stewart 			if (cp_len > my_len) {
5676f8829a4aSRandall Stewart 				/* not enough in this buf */
5677f8829a4aSRandall Stewart 				cp_len = my_len;
5678f8829a4aSRandall Stewart 			}
5679f8829a4aSRandall Stewart 			if (hold_rlock) {
5680f8829a4aSRandall Stewart 				SCTP_INP_READ_UNLOCK(inp);
5681f8829a4aSRandall Stewart 				hold_rlock = 0;
5682f8829a4aSRandall Stewart 			}
5683f8829a4aSRandall Stewart 			if (cp_len > 0)
5684f8829a4aSRandall Stewart 				error = uiomove(mtod(m, char *), cp_len, uio);
5685f8829a4aSRandall Stewart 			/* re-read */
5686f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5687f8829a4aSRandall Stewart 				goto release;
5688f8829a4aSRandall Stewart 			}
56890696e120SRandall Stewart 			if ((control->do_not_ref_stcb == 0) && stcb &&
5690f8829a4aSRandall Stewart 			    stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5691f8829a4aSRandall Stewart 				no_rcv_needed = 1;
5692f8829a4aSRandall Stewart 			}
5693f8829a4aSRandall Stewart 			if (error) {
5694f8829a4aSRandall Stewart 				/* error we are out of here */
5695f8829a4aSRandall Stewart 				goto release;
5696f8829a4aSRandall Stewart 			}
5697139bc87fSRandall Stewart 			if ((SCTP_BUF_NEXT(m) == NULL) &&
5698139bc87fSRandall Stewart 			    (cp_len >= SCTP_BUF_LEN(m)) &&
5699f8829a4aSRandall Stewart 			    ((control->end_added == 0) ||
57000696e120SRandall Stewart 			    (control->end_added &&
57010696e120SRandall Stewart 			    (TAILQ_NEXT(control, next) == NULL)))
5702f8829a4aSRandall Stewart 			    ) {
5703f8829a4aSRandall Stewart 				SCTP_INP_READ_LOCK(inp);
5704f8829a4aSRandall Stewart 				hold_rlock = 1;
5705f8829a4aSRandall Stewart 			}
5706139bc87fSRandall Stewart 			if (cp_len == SCTP_BUF_LEN(m)) {
5707139bc87fSRandall Stewart 				if ((SCTP_BUF_NEXT(m) == NULL) &&
5708139bc87fSRandall Stewart 				    (control->end_added)) {
5709f8829a4aSRandall Stewart 					out_flags |= MSG_EOR;
571052129fcdSRandall Stewart 					if ((control->do_not_ref_stcb == 0) &&
571152129fcdSRandall Stewart 					    (control->stcb != NULL) &&
571252129fcdSRandall Stewart 					    ((control->spec_flags & M_NOTIFICATION) == 0))
5713ee7f9857SRandall Stewart 						control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5714f8829a4aSRandall Stewart 				}
5715139bc87fSRandall Stewart 				if (control->spec_flags & M_NOTIFICATION) {
5716f8829a4aSRandall Stewart 					out_flags |= MSG_NOTIFICATION;
5717f8829a4aSRandall Stewart 				}
5718f8829a4aSRandall Stewart 				/* we ate up the mbuf */
5719f8829a4aSRandall Stewart 				if (in_flags & MSG_PEEK) {
5720f8829a4aSRandall Stewart 					/* just looking */
5721139bc87fSRandall Stewart 					m = SCTP_BUF_NEXT(m);
5722f8829a4aSRandall Stewart 					copied_so_far += cp_len;
5723f8829a4aSRandall Stewart 				} else {
5724f8829a4aSRandall Stewart 					/* dispose of the mbuf */
5725b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5726f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv,
5727139bc87fSRandall Stewart 						    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
572880fefe0aSRandall Stewart 					}
5729f8829a4aSRandall Stewart 					sctp_sbfree(control, stcb, &so->so_rcv, m);
5730b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5731f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv,
5732f8829a4aSRandall Stewart 						    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
573380fefe0aSRandall Stewart 					}
5734f8829a4aSRandall Stewart 					copied_so_far += cp_len;
5735f8829a4aSRandall Stewart 					freed_so_far += cp_len;
5736c4739e2fSRandall Stewart 					freed_so_far += MSIZE;
573718e198d3SRandall Stewart 					atomic_subtract_int(&control->length, cp_len);
5738f8829a4aSRandall Stewart 					control->data = sctp_m_free(m);
5739f8829a4aSRandall Stewart 					m = control->data;
5740f8829a4aSRandall Stewart 					/*
5741f8829a4aSRandall Stewart 					 * been through it all, must hold sb
5742f8829a4aSRandall Stewart 					 * lock ok to null tail
5743f8829a4aSRandall Stewart 					 */
5744f8829a4aSRandall Stewart 					if (control->data == NULL) {
5745a5d547adSRandall Stewart #ifdef INVARIANTS
5746f8829a4aSRandall Stewart 						if ((control->end_added == 0) ||
5747f8829a4aSRandall Stewart 						    (TAILQ_NEXT(control, next) == NULL)) {
5748f8829a4aSRandall Stewart 							/*
5749f8829a4aSRandall Stewart 							 * If the end is not
5750f8829a4aSRandall Stewart 							 * added, OR the
5751f8829a4aSRandall Stewart 							 * next is NOT null
5752f8829a4aSRandall Stewart 							 * we MUST have the
5753f8829a4aSRandall Stewart 							 * lock.
5754f8829a4aSRandall Stewart 							 */
5755f8829a4aSRandall Stewart 							if (mtx_owned(&inp->inp_rdata_mtx) == 0) {
5756f8829a4aSRandall Stewart 								panic("Hmm we don't own the lock?");
5757f8829a4aSRandall Stewart 							}
5758f8829a4aSRandall Stewart 						}
5759f8829a4aSRandall Stewart #endif
5760f8829a4aSRandall Stewart 						control->tail_mbuf = NULL;
5761a5d547adSRandall Stewart #ifdef INVARIANTS
5762f8829a4aSRandall Stewart 						if ((control->end_added) && ((out_flags & MSG_EOR) == 0)) {
5763f8829a4aSRandall Stewart 							panic("end_added, nothing left and no MSG_EOR");
5764f8829a4aSRandall Stewart 						}
5765f8829a4aSRandall Stewart #endif
5766f8829a4aSRandall Stewart 					}
5767f8829a4aSRandall Stewart 				}
5768f8829a4aSRandall Stewart 			} else {
5769f8829a4aSRandall Stewart 				/* Do we need to trim the mbuf? */
5770139bc87fSRandall Stewart 				if (control->spec_flags & M_NOTIFICATION) {
5771f8829a4aSRandall Stewart 					out_flags |= MSG_NOTIFICATION;
5772f8829a4aSRandall Stewart 				}
5773f8829a4aSRandall Stewart 				if ((in_flags & MSG_PEEK) == 0) {
5774139bc87fSRandall Stewart 					SCTP_BUF_RESV_UF(m, cp_len);
5775139bc87fSRandall Stewart 					SCTP_BUF_LEN(m) -= cp_len;
5776b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5777f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, cp_len);
577880fefe0aSRandall Stewart 					}
5779f8829a4aSRandall Stewart 					atomic_subtract_int(&so->so_rcv.sb_cc, cp_len);
57800696e120SRandall Stewart 					if ((control->do_not_ref_stcb == 0) &&
57810696e120SRandall Stewart 					    stcb) {
5782f8829a4aSRandall Stewart 						atomic_subtract_int(&stcb->asoc.sb_cc, cp_len);
5783f8829a4aSRandall Stewart 					}
5784f8829a4aSRandall Stewart 					copied_so_far += cp_len;
5785f8829a4aSRandall Stewart 					freed_so_far += cp_len;
5786c4739e2fSRandall Stewart 					freed_so_far += MSIZE;
5787b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5788f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb,
5789f8829a4aSRandall Stewart 						    SCTP_LOG_SBRESULT, 0);
579080fefe0aSRandall Stewart 					}
579118e198d3SRandall Stewart 					atomic_subtract_int(&control->length, cp_len);
5792f8829a4aSRandall Stewart 				} else {
5793f8829a4aSRandall Stewart 					copied_so_far += cp_len;
5794f8829a4aSRandall Stewart 				}
5795f8829a4aSRandall Stewart 			}
5796d61a0ae0SRandall Stewart 			if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) {
5797f8829a4aSRandall Stewart 				break;
5798f8829a4aSRandall Stewart 			}
5799f8829a4aSRandall Stewart 			if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5800f8829a4aSRandall Stewart 			    (control->do_not_ref_stcb == 0) &&
5801f8829a4aSRandall Stewart 			    (freed_so_far >= rwnd_req)) {
5802f8829a4aSRandall Stewart 				sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5803f8829a4aSRandall Stewart 			}
5804f8829a4aSRandall Stewart 		}		/* end while(m) */
5805f8829a4aSRandall Stewart 		/*
5806f8829a4aSRandall Stewart 		 * At this point we have looked at it all and we either have
5807f8829a4aSRandall Stewart 		 * a MSG_EOR/or read all the user wants... <OR>
5808f8829a4aSRandall Stewart 		 * control->length == 0.
5809f8829a4aSRandall Stewart 		 */
5810d61a0ae0SRandall Stewart 		if ((out_flags & MSG_EOR) && ((in_flags & MSG_PEEK) == 0)) {
5811f8829a4aSRandall Stewart 			/* we are done with this control */
5812f8829a4aSRandall Stewart 			if (control->length == 0) {
5813f8829a4aSRandall Stewart 				if (control->data) {
5814a5d547adSRandall Stewart #ifdef INVARIANTS
5815f8829a4aSRandall Stewart 					panic("control->data not null at read eor?");
5816f8829a4aSRandall Stewart #else
5817ad81507eSRandall Stewart 					SCTP_PRINTF("Strange, data left in the control buffer .. invarients would panic?\n");
5818f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
5819f8829a4aSRandall Stewart 					control->data = NULL;
5820f8829a4aSRandall Stewart #endif
5821f8829a4aSRandall Stewart 				}
5822f8829a4aSRandall Stewart 		done_with_control:
5823f8829a4aSRandall Stewart 				if (TAILQ_NEXT(control, next) == NULL) {
5824f8829a4aSRandall Stewart 					/*
5825f8829a4aSRandall Stewart 					 * If we don't have a next we need a
5826b201f536SRandall Stewart 					 * lock, if there is a next
5827b201f536SRandall Stewart 					 * interrupt is filling ahead of us
5828b201f536SRandall Stewart 					 * and we don't need a lock to
5829b201f536SRandall Stewart 					 * remove this guy (which is the
5830b201f536SRandall Stewart 					 * head of the queue).
5831f8829a4aSRandall Stewart 					 */
5832f8829a4aSRandall Stewart 					if (hold_rlock == 0) {
5833f8829a4aSRandall Stewart 						SCTP_INP_READ_LOCK(inp);
5834f8829a4aSRandall Stewart 						hold_rlock = 1;
5835f8829a4aSRandall Stewart 					}
5836f8829a4aSRandall Stewart 				}
5837f8829a4aSRandall Stewart 				TAILQ_REMOVE(&inp->read_queue, control, next);
5838f8829a4aSRandall Stewart 				/* Add back any hiddend data */
5839f8829a4aSRandall Stewart 				if (control->held_length) {
5840f8829a4aSRandall Stewart 					held_length = 0;
5841f8829a4aSRandall Stewart 					control->held_length = 0;
5842f8829a4aSRandall Stewart 					wakeup_read_socket = 1;
5843f8829a4aSRandall Stewart 				}
584417205eccSRandall Stewart 				if (control->aux_data) {
584517205eccSRandall Stewart 					sctp_m_free(control->aux_data);
584617205eccSRandall Stewart 					control->aux_data = NULL;
584717205eccSRandall Stewart 				}
5848f8829a4aSRandall Stewart 				no_rcv_needed = control->do_not_ref_stcb;
5849f8829a4aSRandall Stewart 				sctp_free_remote_addr(control->whoFrom);
5850f8829a4aSRandall Stewart 				control->data = NULL;
5851f8829a4aSRandall Stewart 				sctp_free_a_readq(stcb, control);
5852f8829a4aSRandall Stewart 				control = NULL;
58530696e120SRandall Stewart 				if ((freed_so_far >= rwnd_req) &&
58540696e120SRandall Stewart 				    (no_rcv_needed == 0))
5855f8829a4aSRandall Stewart 					sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5856f8829a4aSRandall Stewart 
5857f8829a4aSRandall Stewart 			} else {
5858f8829a4aSRandall Stewart 				/*
5859f8829a4aSRandall Stewart 				 * The user did not read all of this
5860f8829a4aSRandall Stewart 				 * message, turn off the returned MSG_EOR
5861f8829a4aSRandall Stewart 				 * since we are leaving more behind on the
5862f8829a4aSRandall Stewart 				 * control to read.
5863f8829a4aSRandall Stewart 				 */
5864a5d547adSRandall Stewart #ifdef INVARIANTS
58650696e120SRandall Stewart 				if (control->end_added &&
58660696e120SRandall Stewart 				    (control->data == NULL) &&
5867f8829a4aSRandall Stewart 				    (control->tail_mbuf == NULL)) {
5868f8829a4aSRandall Stewart 					panic("Gak, control->length is corrupt?");
5869f8829a4aSRandall Stewart 				}
5870f8829a4aSRandall Stewart #endif
5871f8829a4aSRandall Stewart 				no_rcv_needed = control->do_not_ref_stcb;
5872f8829a4aSRandall Stewart 				out_flags &= ~MSG_EOR;
5873f8829a4aSRandall Stewart 			}
5874f8829a4aSRandall Stewart 		}
5875f8829a4aSRandall Stewart 		if (out_flags & MSG_EOR) {
5876f8829a4aSRandall Stewart 			goto release;
5877f8829a4aSRandall Stewart 		}
5878f8829a4aSRandall Stewart 		if ((uio->uio_resid == 0) ||
587904aab884SMichael Tuexen 		    ((in_eeor_mode) &&
588004aab884SMichael Tuexen 		    (copied_so_far >= (uint32_t) max(so->so_rcv.sb_lowat, 1)))) {
5881f8829a4aSRandall Stewart 			goto release;
5882f8829a4aSRandall Stewart 		}
5883f8829a4aSRandall Stewart 		/*
5884f8829a4aSRandall Stewart 		 * If I hit here the receiver wants more and this message is
5885f8829a4aSRandall Stewart 		 * NOT done (pd-api). So two questions. Can we block? if not
5886f8829a4aSRandall Stewart 		 * we are done. Did the user NOT set MSG_WAITALL?
5887f8829a4aSRandall Stewart 		 */
5888f8829a4aSRandall Stewart 		if (block_allowed == 0) {
5889f8829a4aSRandall Stewart 			goto release;
5890f8829a4aSRandall Stewart 		}
5891f8829a4aSRandall Stewart 		/*
5892f8829a4aSRandall Stewart 		 * We need to wait for more data a few things: - We don't
5893f8829a4aSRandall Stewart 		 * sbunlock() so we don't get someone else reading. - We
5894f8829a4aSRandall Stewart 		 * must be sure to account for the case where what is added
5895f8829a4aSRandall Stewart 		 * is NOT to our control when we wakeup.
5896f8829a4aSRandall Stewart 		 */
5897f8829a4aSRandall Stewart 
5898f8829a4aSRandall Stewart 		/*
5899f8829a4aSRandall Stewart 		 * Do we need to tell the transport a rwnd update might be
5900f8829a4aSRandall Stewart 		 * needed before we go to sleep?
5901f8829a4aSRandall Stewart 		 */
5902f8829a4aSRandall Stewart 		if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5903f8829a4aSRandall Stewart 		    ((freed_so_far >= rwnd_req) &&
5904f8829a4aSRandall Stewart 		    (control->do_not_ref_stcb == 0) &&
5905f8829a4aSRandall Stewart 		    (no_rcv_needed == 0))) {
5906f8829a4aSRandall Stewart 			sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5907f8829a4aSRandall Stewart 		}
5908f8829a4aSRandall Stewart wait_some_more:
590944b7479bSRandall Stewart 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
5910f8829a4aSRandall Stewart 			goto release;
5911f8829a4aSRandall Stewart 		}
5912f8829a4aSRandall Stewart 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)
5913f8829a4aSRandall Stewart 			goto release;
5914f8829a4aSRandall Stewart 
5915f8829a4aSRandall Stewart 		if (hold_rlock == 1) {
5916f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
5917f8829a4aSRandall Stewart 			hold_rlock = 0;
5918f8829a4aSRandall Stewart 		}
5919f8829a4aSRandall Stewart 		if (hold_sblock == 0) {
5920f8829a4aSRandall Stewart 			SOCKBUF_LOCK(&so->so_rcv);
5921f8829a4aSRandall Stewart 			hold_sblock = 1;
5922f8829a4aSRandall Stewart 		}
5923851b7298SRandall Stewart 		if ((copied_so_far) && (control->length == 0) &&
5924b5c16493SMichael Tuexen 		    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE))) {
5925851b7298SRandall Stewart 			goto release;
5926851b7298SRandall Stewart 		}
5927f8829a4aSRandall Stewart 		if (so->so_rcv.sb_cc <= control->held_length) {
5928f8829a4aSRandall Stewart 			error = sbwait(&so->so_rcv);
5929f8829a4aSRandall Stewart 			if (error) {
5930f8829a4aSRandall Stewart 				goto release;
5931f8829a4aSRandall Stewart 			}
5932f8829a4aSRandall Stewart 			control->held_length = 0;
5933f8829a4aSRandall Stewart 		}
5934f8829a4aSRandall Stewart 		if (hold_sblock) {
5935f8829a4aSRandall Stewart 			SOCKBUF_UNLOCK(&so->so_rcv);
5936f8829a4aSRandall Stewart 			hold_sblock = 0;
5937f8829a4aSRandall Stewart 		}
5938f8829a4aSRandall Stewart 		if (control->length == 0) {
5939f8829a4aSRandall Stewart 			/* still nothing here */
5940f8829a4aSRandall Stewart 			if (control->end_added == 1) {
5941f8829a4aSRandall Stewart 				/* he aborted, or is done i.e.did a shutdown */
5942f8829a4aSRandall Stewart 				out_flags |= MSG_EOR;
59439a6142d8SRandall Stewart 				if (control->pdapi_aborted) {
59446114cd96SRandall Stewart 					if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5945ee7f9857SRandall Stewart 						control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
59469a6142d8SRandall Stewart 
594703b0b021SRandall Stewart 					out_flags |= MSG_TRUNC;
59489a6142d8SRandall Stewart 				} else {
59496114cd96SRandall Stewart 					if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5950ee7f9857SRandall Stewart 						control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
59519a6142d8SRandall Stewart 				}
5952f8829a4aSRandall Stewart 				goto done_with_control;
5953f8829a4aSRandall Stewart 			}
5954f8829a4aSRandall Stewart 			if (so->so_rcv.sb_cc > held_length) {
5955f8829a4aSRandall Stewart 				control->held_length = so->so_rcv.sb_cc;
5956f8829a4aSRandall Stewart 				held_length = 0;
5957f8829a4aSRandall Stewart 			}
5958f8829a4aSRandall Stewart 			goto wait_some_more;
5959f8829a4aSRandall Stewart 		} else if (control->data == NULL) {
596050cec919SRandall Stewart 			/*
596150cec919SRandall Stewart 			 * we must re-sync since data is probably being
596250cec919SRandall Stewart 			 * added
596350cec919SRandall Stewart 			 */
596450cec919SRandall Stewart 			SCTP_INP_READ_LOCK(inp);
596550cec919SRandall Stewart 			if ((control->length > 0) && (control->data == NULL)) {
596650cec919SRandall Stewart 				/*
596750cec919SRandall Stewart 				 * big trouble.. we have the lock and its
596850cec919SRandall Stewart 				 * corrupt?
596950cec919SRandall Stewart 				 */
59709c04b296SRandall Stewart #ifdef INVARIANTS
5971f8829a4aSRandall Stewart 				panic("Impossible data==NULL length !=0");
59729c04b296SRandall Stewart #endif
59739c04b296SRandall Stewart 				out_flags |= MSG_EOR;
59749c04b296SRandall Stewart 				out_flags |= MSG_TRUNC;
59759c04b296SRandall Stewart 				control->length = 0;
59769c04b296SRandall Stewart 				SCTP_INP_READ_UNLOCK(inp);
59779c04b296SRandall Stewart 				goto done_with_control;
5978f8829a4aSRandall Stewart 			}
597950cec919SRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
598050cec919SRandall Stewart 			/* We will fall around to get more data */
598150cec919SRandall Stewart 		}
5982f8829a4aSRandall Stewart 		goto get_more_data;
5983f8829a4aSRandall Stewart 	} else {
598417205eccSRandall Stewart 		/*-
598517205eccSRandall Stewart 		 * Give caller back the mbuf chain,
598617205eccSRandall Stewart 		 * store in uio_resid the length
5987f8829a4aSRandall Stewart 		 */
598817205eccSRandall Stewart 		wakeup_read_socket = 0;
5989f8829a4aSRandall Stewart 		if ((control->end_added == 0) ||
5990f8829a4aSRandall Stewart 		    (TAILQ_NEXT(control, next) == NULL)) {
5991f8829a4aSRandall Stewart 			/* Need to get rlock */
5992f8829a4aSRandall Stewart 			if (hold_rlock == 0) {
5993f8829a4aSRandall Stewart 				SCTP_INP_READ_LOCK(inp);
5994f8829a4aSRandall Stewart 				hold_rlock = 1;
5995f8829a4aSRandall Stewart 			}
5996f8829a4aSRandall Stewart 		}
5997139bc87fSRandall Stewart 		if (control->end_added) {
5998f8829a4aSRandall Stewart 			out_flags |= MSG_EOR;
599960990c0cSMichael Tuexen 			if ((control->do_not_ref_stcb == 0) &&
600060990c0cSMichael Tuexen 			    (control->stcb != NULL) &&
600160990c0cSMichael Tuexen 			    ((control->spec_flags & M_NOTIFICATION) == 0))
6002ee7f9857SRandall Stewart 				control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6003f8829a4aSRandall Stewart 		}
6004139bc87fSRandall Stewart 		if (control->spec_flags & M_NOTIFICATION) {
6005f8829a4aSRandall Stewart 			out_flags |= MSG_NOTIFICATION;
6006f8829a4aSRandall Stewart 		}
600717205eccSRandall Stewart 		uio->uio_resid = control->length;
6008f8829a4aSRandall Stewart 		*mp = control->data;
6009f8829a4aSRandall Stewart 		m = control->data;
6010f8829a4aSRandall Stewart 		while (m) {
6011b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6012f8829a4aSRandall Stewart 				sctp_sblog(&so->so_rcv,
6013139bc87fSRandall Stewart 				    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
601480fefe0aSRandall Stewart 			}
6015f8829a4aSRandall Stewart 			sctp_sbfree(control, stcb, &so->so_rcv, m);
6016139bc87fSRandall Stewart 			freed_so_far += SCTP_BUF_LEN(m);
6017c4739e2fSRandall Stewart 			freed_so_far += MSIZE;
6018b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6019f8829a4aSRandall Stewart 				sctp_sblog(&so->so_rcv,
6020f8829a4aSRandall Stewart 				    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
602180fefe0aSRandall Stewart 			}
6022139bc87fSRandall Stewart 			m = SCTP_BUF_NEXT(m);
6023f8829a4aSRandall Stewart 		}
6024f8829a4aSRandall Stewart 		control->data = control->tail_mbuf = NULL;
6025f8829a4aSRandall Stewart 		control->length = 0;
6026f8829a4aSRandall Stewart 		if (out_flags & MSG_EOR) {
6027f8829a4aSRandall Stewart 			/* Done with this control */
6028f8829a4aSRandall Stewart 			goto done_with_control;
6029f8829a4aSRandall Stewart 		}
6030f8829a4aSRandall Stewart 	}
6031f8829a4aSRandall Stewart release:
6032f8829a4aSRandall Stewart 	if (hold_rlock == 1) {
6033f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
6034f8829a4aSRandall Stewart 		hold_rlock = 0;
6035f8829a4aSRandall Stewart 	}
60367abab911SRobert Watson 	if (hold_sblock == 1) {
60377abab911SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
60387abab911SRobert Watson 		hold_sblock = 0;
6039f8829a4aSRandall Stewart 	}
6040f8829a4aSRandall Stewart 	sbunlock(&so->so_rcv);
60417abab911SRobert Watson 	sockbuf_lock = 0;
6042f8829a4aSRandall Stewart 
6043f8829a4aSRandall Stewart release_unlocked:
6044f8829a4aSRandall Stewart 	if (hold_sblock) {
6045f8829a4aSRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
6046f8829a4aSRandall Stewart 		hold_sblock = 0;
6047f8829a4aSRandall Stewart 	}
6048f8829a4aSRandall Stewart 	if ((stcb) && (in_flags & MSG_PEEK) == 0) {
6049f8829a4aSRandall Stewart 		if ((freed_so_far >= rwnd_req) &&
6050f8829a4aSRandall Stewart 		    (control && (control->do_not_ref_stcb == 0)) &&
6051f8829a4aSRandall Stewart 		    (no_rcv_needed == 0))
6052f8829a4aSRandall Stewart 			sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6053f8829a4aSRandall Stewart 	}
6054f8829a4aSRandall Stewart out:
60551b9f62a0SRandall Stewart 	if (msg_flags) {
60561b9f62a0SRandall Stewart 		*msg_flags = out_flags;
60571b9f62a0SRandall Stewart 	}
60589a6142d8SRandall Stewart 	if (((out_flags & MSG_EOR) == 0) &&
60599a6142d8SRandall Stewart 	    ((in_flags & MSG_PEEK) == 0) &&
60609a6142d8SRandall Stewart 	    (sinfo) &&
6061e2e7c62eSMichael Tuexen 	    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
6062e2e7c62eSMichael Tuexen 	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO))) {
60639a6142d8SRandall Stewart 		struct sctp_extrcvinfo *s_extra;
60649a6142d8SRandall Stewart 
60659a6142d8SRandall Stewart 		s_extra = (struct sctp_extrcvinfo *)sinfo;
60669a6142d8SRandall Stewart 		s_extra->sreinfo_next_flags = SCTP_NO_NEXT_MSG;
60679a6142d8SRandall Stewart 	}
6068f8829a4aSRandall Stewart 	if (hold_rlock == 1) {
6069f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
6070f8829a4aSRandall Stewart 	}
6071f8829a4aSRandall Stewart 	if (hold_sblock) {
6072f8829a4aSRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
6073f8829a4aSRandall Stewart 	}
60747abab911SRobert Watson 	if (sockbuf_lock) {
60757abab911SRobert Watson 		sbunlock(&so->so_rcv);
60767abab911SRobert Watson 	}
607750cec919SRandall Stewart 	if (freecnt_applied) {
6078f8829a4aSRandall Stewart 		/*
6079f8829a4aSRandall Stewart 		 * The lock on the socket buffer protects us so the free
6080f8829a4aSRandall Stewart 		 * code will stop. But since we used the socketbuf lock and
6081f8829a4aSRandall Stewart 		 * the sender uses the tcb_lock to increment, we need to use
6082f8829a4aSRandall Stewart 		 * the atomic add to the refcnt.
6083f8829a4aSRandall Stewart 		 */
608450cec919SRandall Stewart 		if (stcb == NULL) {
6085df6e0cc3SRandall Stewart #ifdef INVARIANTS
608650cec919SRandall Stewart 			panic("stcb for refcnt has gone NULL?");
6087df6e0cc3SRandall Stewart 			goto stage_left;
6088df6e0cc3SRandall Stewart #else
6089df6e0cc3SRandall Stewart 			goto stage_left;
6090df6e0cc3SRandall Stewart #endif
609150cec919SRandall Stewart 		}
609250cec919SRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, -1);
6093f8829a4aSRandall Stewart 		/* Save the value back for next time */
6094f8829a4aSRandall Stewart 		stcb->freed_by_sorcv_sincelast = freed_so_far;
6095f8829a4aSRandall Stewart 	}
6096b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
6097f8829a4aSRandall Stewart 		if (stcb) {
6098f8829a4aSRandall Stewart 			sctp_misc_ints(SCTP_SORECV_DONE,
6099f8829a4aSRandall Stewart 			    freed_so_far,
6100f8829a4aSRandall Stewart 			    ((uio) ? (slen - uio->uio_resid) : slen),
6101f8829a4aSRandall Stewart 			    stcb->asoc.my_rwnd,
6102f8829a4aSRandall Stewart 			    so->so_rcv.sb_cc);
6103f8829a4aSRandall Stewart 		} else {
6104f8829a4aSRandall Stewart 			sctp_misc_ints(SCTP_SORECV_DONE,
6105f8829a4aSRandall Stewart 			    freed_so_far,
6106f8829a4aSRandall Stewart 			    ((uio) ? (slen - uio->uio_resid) : slen),
6107f8829a4aSRandall Stewart 			    0,
6108f8829a4aSRandall Stewart 			    so->so_rcv.sb_cc);
6109f8829a4aSRandall Stewart 		}
611080fefe0aSRandall Stewart 	}
6111df6e0cc3SRandall Stewart stage_left:
6112f8829a4aSRandall Stewart 	if (wakeup_read_socket) {
6113f8829a4aSRandall Stewart 		sctp_sorwakeup(inp, so);
6114f8829a4aSRandall Stewart 	}
6115f8829a4aSRandall Stewart 	return (error);
6116f8829a4aSRandall Stewart }
6117f8829a4aSRandall Stewart 
6118f8829a4aSRandall Stewart 
6119f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
6120f8829a4aSRandall Stewart struct mbuf *
6121f8829a4aSRandall Stewart sctp_m_free(struct mbuf *m)
6122f8829a4aSRandall Stewart {
6123b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6124139bc87fSRandall Stewart 		if (SCTP_BUF_IS_EXTENDED(m)) {
6125f8829a4aSRandall Stewart 			sctp_log_mb(m, SCTP_MBUF_IFREE);
6126f8829a4aSRandall Stewart 		}
612780fefe0aSRandall Stewart 	}
6128f8829a4aSRandall Stewart 	return (m_free(m));
6129f8829a4aSRandall Stewart }
6130f8829a4aSRandall Stewart 
6131f8829a4aSRandall Stewart void
6132f8829a4aSRandall Stewart sctp_m_freem(struct mbuf *mb)
6133f8829a4aSRandall Stewart {
6134f8829a4aSRandall Stewart 	while (mb != NULL)
6135f8829a4aSRandall Stewart 		mb = sctp_m_free(mb);
6136f8829a4aSRandall Stewart }
6137f8829a4aSRandall Stewart 
6138f8829a4aSRandall Stewart #endif
6139f8829a4aSRandall Stewart 
614042551e99SRandall Stewart int
614142551e99SRandall Stewart sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id)
614242551e99SRandall Stewart {
614342551e99SRandall Stewart 	/*
614442551e99SRandall Stewart 	 * Given a local address. For all associations that holds the
614542551e99SRandall Stewart 	 * address, request a peer-set-primary.
614642551e99SRandall Stewart 	 */
614742551e99SRandall Stewart 	struct sctp_ifa *ifa;
614842551e99SRandall Stewart 	struct sctp_laddr *wi;
614942551e99SRandall Stewart 
615042551e99SRandall Stewart 	ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0);
615142551e99SRandall Stewart 	if (ifa == NULL) {
6152c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EADDRNOTAVAIL);
615342551e99SRandall Stewart 		return (EADDRNOTAVAIL);
615442551e99SRandall Stewart 	}
615542551e99SRandall Stewart 	/*
615642551e99SRandall Stewart 	 * Now that we have the ifa we must awaken the iterator with this
615742551e99SRandall Stewart 	 * message.
615842551e99SRandall Stewart 	 */
6159b3f1ea41SRandall Stewart 	wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
616042551e99SRandall Stewart 	if (wi == NULL) {
6161c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
616242551e99SRandall Stewart 		return (ENOMEM);
616342551e99SRandall Stewart 	}
616442551e99SRandall Stewart 	/* Now incr the count and int wi structure */
616542551e99SRandall Stewart 	SCTP_INCR_LADDR_COUNT();
616642551e99SRandall Stewart 	bzero(wi, sizeof(*wi));
6167d61a0ae0SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
616842551e99SRandall Stewart 	wi->ifa = ifa;
616942551e99SRandall Stewart 	wi->action = SCTP_SET_PRIM_ADDR;
617042551e99SRandall Stewart 	atomic_add_int(&ifa->refcount, 1);
617142551e99SRandall Stewart 
617242551e99SRandall Stewart 	/* Now add it to the work queue */
6173f7517433SRandall Stewart 	SCTP_WQ_ADDR_LOCK();
617442551e99SRandall Stewart 	/*
617542551e99SRandall Stewart 	 * Should this really be a tailq? As it is we will process the
617642551e99SRandall Stewart 	 * newest first :-0
617742551e99SRandall Stewart 	 */
6178b3f1ea41SRandall Stewart 	LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
6179f7517433SRandall Stewart 	SCTP_WQ_ADDR_UNLOCK();
618042551e99SRandall Stewart 	sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
618142551e99SRandall Stewart 	    (struct sctp_inpcb *)NULL,
618242551e99SRandall Stewart 	    (struct sctp_tcb *)NULL,
618342551e99SRandall Stewart 	    (struct sctp_nets *)NULL);
618442551e99SRandall Stewart 	return (0);
618542551e99SRandall Stewart }
618642551e99SRandall Stewart 
618742551e99SRandall Stewart 
6188f8829a4aSRandall Stewart int
618917205eccSRandall Stewart sctp_soreceive(struct socket *so,
619017205eccSRandall Stewart     struct sockaddr **psa,
619117205eccSRandall Stewart     struct uio *uio,
619217205eccSRandall Stewart     struct mbuf **mp0,
619317205eccSRandall Stewart     struct mbuf **controlp,
619417205eccSRandall Stewart     int *flagsp)
6195f8829a4aSRandall Stewart {
6196f8829a4aSRandall Stewart 	int error, fromlen;
6197f8829a4aSRandall Stewart 	uint8_t sockbuf[256];
6198f8829a4aSRandall Stewart 	struct sockaddr *from;
6199f8829a4aSRandall Stewart 	struct sctp_extrcvinfo sinfo;
6200f8829a4aSRandall Stewart 	int filling_sinfo = 1;
6201f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
6202f8829a4aSRandall Stewart 
6203f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
6204f8829a4aSRandall Stewart 	/* pickup the assoc we are reading from */
6205f8829a4aSRandall Stewart 	if (inp == NULL) {
6206c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6207f8829a4aSRandall Stewart 		return (EINVAL);
6208f8829a4aSRandall Stewart 	}
6209e2e7c62eSMichael Tuexen 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) &&
6210e2e7c62eSMichael Tuexen 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
6211e2e7c62eSMichael Tuexen 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) ||
6212f8829a4aSRandall Stewart 	    (controlp == NULL)) {
6213f8829a4aSRandall Stewart 		/* user does not want the sndrcv ctl */
6214f8829a4aSRandall Stewart 		filling_sinfo = 0;
6215f8829a4aSRandall Stewart 	}
6216f8829a4aSRandall Stewart 	if (psa) {
6217f8829a4aSRandall Stewart 		from = (struct sockaddr *)sockbuf;
6218f8829a4aSRandall Stewart 		fromlen = sizeof(sockbuf);
6219f8829a4aSRandall Stewart 		from->sa_len = 0;
6220f8829a4aSRandall Stewart 	} else {
6221f8829a4aSRandall Stewart 		from = NULL;
6222f8829a4aSRandall Stewart 		fromlen = 0;
6223f8829a4aSRandall Stewart 	}
6224f8829a4aSRandall Stewart 
6225e432298aSXin LI 	if (filling_sinfo) {
6226e432298aSXin LI 		memset(&sinfo, 0, sizeof(struct sctp_extrcvinfo));
6227e432298aSXin LI 	}
6228f8829a4aSRandall Stewart 	error = sctp_sorecvmsg(so, uio, mp0, from, fromlen, flagsp,
6229f8829a4aSRandall Stewart 	    (struct sctp_sndrcvinfo *)&sinfo, filling_sinfo);
6230e432298aSXin LI 	if (controlp != NULL) {
6231f8829a4aSRandall Stewart 		/* copy back the sinfo in a CMSG format */
6232f8829a4aSRandall Stewart 		if (filling_sinfo)
6233f8829a4aSRandall Stewart 			*controlp = sctp_build_ctl_nchunk(inp,
6234f8829a4aSRandall Stewart 			    (struct sctp_sndrcvinfo *)&sinfo);
6235f8829a4aSRandall Stewart 		else
6236f8829a4aSRandall Stewart 			*controlp = NULL;
6237f8829a4aSRandall Stewart 	}
6238f8829a4aSRandall Stewart 	if (psa) {
6239f8829a4aSRandall Stewart 		/* copy back the address info */
6240f8829a4aSRandall Stewart 		if (from && from->sa_len) {
6241f8829a4aSRandall Stewart 			*psa = sodupsockaddr(from, M_NOWAIT);
6242f8829a4aSRandall Stewart 		} else {
6243f8829a4aSRandall Stewart 			*psa = NULL;
6244f8829a4aSRandall Stewart 		}
6245f8829a4aSRandall Stewart 	}
6246f8829a4aSRandall Stewart 	return (error);
6247f8829a4aSRandall Stewart }
624817205eccSRandall Stewart 
624917205eccSRandall Stewart 
625017205eccSRandall Stewart 
625117205eccSRandall Stewart 
625217205eccSRandall Stewart 
625317205eccSRandall Stewart int
6254d61a0ae0SRandall Stewart sctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr,
6255d61a0ae0SRandall Stewart     int totaddr, int *error)
625617205eccSRandall Stewart {
625717205eccSRandall Stewart 	int added = 0;
625817205eccSRandall Stewart 	int i;
625917205eccSRandall Stewart 	struct sctp_inpcb *inp;
626017205eccSRandall Stewart 	struct sockaddr *sa;
626117205eccSRandall Stewart 	size_t incr = 0;
626217205eccSRandall Stewart 
626392776dfdSMichael Tuexen #ifdef INET
626492776dfdSMichael Tuexen 	struct sockaddr_in *sin;
626592776dfdSMichael Tuexen 
626692776dfdSMichael Tuexen #endif
626792776dfdSMichael Tuexen #ifdef INET6
626892776dfdSMichael Tuexen 	struct sockaddr_in6 *sin6;
626992776dfdSMichael Tuexen 
627092776dfdSMichael Tuexen #endif
627192776dfdSMichael Tuexen 
627217205eccSRandall Stewart 	sa = addr;
627317205eccSRandall Stewart 	inp = stcb->sctp_ep;
627417205eccSRandall Stewart 	*error = 0;
627517205eccSRandall Stewart 	for (i = 0; i < totaddr; i++) {
6276ea5eba11SMichael Tuexen 		switch (sa->sa_family) {
6277ea5eba11SMichael Tuexen #ifdef INET
6278ea5eba11SMichael Tuexen 		case AF_INET:
627917205eccSRandall Stewart 			incr = sizeof(struct sockaddr_in);
628092776dfdSMichael Tuexen 			sin = (struct sockaddr_in *)sa;
628192776dfdSMichael Tuexen 			if ((sin->sin_addr.s_addr == INADDR_ANY) ||
628292776dfdSMichael Tuexen 			    (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
628392776dfdSMichael Tuexen 			    IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
628492776dfdSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
628592776dfdSMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
628692776dfdSMichael Tuexen 				*error = EINVAL;
628792776dfdSMichael Tuexen 				goto out_now;
628892776dfdSMichael Tuexen 			}
6289ca85e948SMichael Tuexen 			if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
629017205eccSRandall Stewart 				/* assoc gone no un-lock */
6291c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6292c4739e2fSRandall Stewart 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
629317205eccSRandall Stewart 				*error = ENOBUFS;
629417205eccSRandall Stewart 				goto out_now;
629517205eccSRandall Stewart 			}
629617205eccSRandall Stewart 			added++;
6297ea5eba11SMichael Tuexen 			break;
6298ea5eba11SMichael Tuexen #endif
6299ea5eba11SMichael Tuexen #ifdef INET6
6300ea5eba11SMichael Tuexen 		case AF_INET6:
630117205eccSRandall Stewart 			incr = sizeof(struct sockaddr_in6);
630292776dfdSMichael Tuexen 			sin6 = (struct sockaddr_in6 *)sa;
630392776dfdSMichael Tuexen 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
630492776dfdSMichael Tuexen 			    IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
630592776dfdSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
630692776dfdSMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_8);
630792776dfdSMichael Tuexen 				*error = EINVAL;
630892776dfdSMichael Tuexen 				goto out_now;
630992776dfdSMichael Tuexen 			}
6310ca85e948SMichael Tuexen 			if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
631117205eccSRandall Stewart 				/* assoc gone no un-lock */
6312c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6313c4739e2fSRandall Stewart 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_8);
631417205eccSRandall Stewart 				*error = ENOBUFS;
631517205eccSRandall Stewart 				goto out_now;
631617205eccSRandall Stewart 			}
631717205eccSRandall Stewart 			added++;
6318ea5eba11SMichael Tuexen 			break;
6319ea5eba11SMichael Tuexen #endif
6320ea5eba11SMichael Tuexen 		default:
6321ea5eba11SMichael Tuexen 			break;
632217205eccSRandall Stewart 		}
632317205eccSRandall Stewart 		sa = (struct sockaddr *)((caddr_t)sa + incr);
632417205eccSRandall Stewart 	}
632517205eccSRandall Stewart out_now:
632617205eccSRandall Stewart 	return (added);
632717205eccSRandall Stewart }
632817205eccSRandall Stewart 
632917205eccSRandall Stewart struct sctp_tcb *
6330d61a0ae0SRandall Stewart sctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr,
6331d61a0ae0SRandall Stewart     int *totaddr, int *num_v4, int *num_v6, int *error,
6332d61a0ae0SRandall Stewart     int limit, int *bad_addr)
633317205eccSRandall Stewart {
633417205eccSRandall Stewart 	struct sockaddr *sa;
633517205eccSRandall Stewart 	struct sctp_tcb *stcb = NULL;
633617205eccSRandall Stewart 	size_t incr, at, i;
633717205eccSRandall Stewart 
633817205eccSRandall Stewart 	at = incr = 0;
633917205eccSRandall Stewart 	sa = addr;
6340ea5eba11SMichael Tuexen 
634117205eccSRandall Stewart 	*error = *num_v6 = *num_v4 = 0;
634217205eccSRandall Stewart 	/* account and validate addresses */
63434c9179adSRandall Stewart 	for (i = 0; i < (size_t)*totaddr; i++) {
6344ea5eba11SMichael Tuexen 		switch (sa->sa_family) {
6345ea5eba11SMichael Tuexen #ifdef INET
6346ea5eba11SMichael Tuexen 		case AF_INET:
634717205eccSRandall Stewart 			(*num_v4) += 1;
634817205eccSRandall Stewart 			incr = sizeof(struct sockaddr_in);
6349d61a0ae0SRandall Stewart 			if (sa->sa_len != incr) {
6350c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6351d61a0ae0SRandall Stewart 				*error = EINVAL;
6352d61a0ae0SRandall Stewart 				*bad_addr = 1;
6353d61a0ae0SRandall Stewart 				return (NULL);
6354d61a0ae0SRandall Stewart 			}
6355ea5eba11SMichael Tuexen 			break;
6356ea5eba11SMichael Tuexen #endif
6357ea5eba11SMichael Tuexen #ifdef INET6
6358ea5eba11SMichael Tuexen 		case AF_INET6:
6359ea5eba11SMichael Tuexen 			{
636017205eccSRandall Stewart 				struct sockaddr_in6 *sin6;
636117205eccSRandall Stewart 
636217205eccSRandall Stewart 				sin6 = (struct sockaddr_in6 *)sa;
636317205eccSRandall Stewart 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
636417205eccSRandall Stewart 					/* Must be non-mapped for connectx */
6365c4739e2fSRandall Stewart 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
636617205eccSRandall Stewart 					*error = EINVAL;
6367d61a0ae0SRandall Stewart 					*bad_addr = 1;
636817205eccSRandall Stewart 					return (NULL);
636917205eccSRandall Stewart 				}
637017205eccSRandall Stewart 				(*num_v6) += 1;
637117205eccSRandall Stewart 				incr = sizeof(struct sockaddr_in6);
6372d61a0ae0SRandall Stewart 				if (sa->sa_len != incr) {
6373c4739e2fSRandall Stewart 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6374d61a0ae0SRandall Stewart 					*error = EINVAL;
6375d61a0ae0SRandall Stewart 					*bad_addr = 1;
6376d61a0ae0SRandall Stewart 					return (NULL);
6377d61a0ae0SRandall Stewart 				}
6378ea5eba11SMichael Tuexen 				break;
6379ea5eba11SMichael Tuexen 			}
6380ea5eba11SMichael Tuexen #endif
6381ea5eba11SMichael Tuexen 		default:
638217205eccSRandall Stewart 			*totaddr = i;
638317205eccSRandall Stewart 			/* we are done */
638417205eccSRandall Stewart 			break;
638517205eccSRandall Stewart 		}
6386ea5eba11SMichael Tuexen 		if (i == (size_t)*totaddr) {
6387ea5eba11SMichael Tuexen 			break;
6388ea5eba11SMichael Tuexen 		}
6389d61a0ae0SRandall Stewart 		SCTP_INP_INCR_REF(inp);
639017205eccSRandall Stewart 		stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
639117205eccSRandall Stewart 		if (stcb != NULL) {
639217205eccSRandall Stewart 			/* Already have or am bring up an association */
639317205eccSRandall Stewart 			return (stcb);
6394d61a0ae0SRandall Stewart 		} else {
6395d61a0ae0SRandall Stewart 			SCTP_INP_DECR_REF(inp);
639617205eccSRandall Stewart 		}
63974c9179adSRandall Stewart 		if ((at + incr) > (size_t)limit) {
639817205eccSRandall Stewart 			*totaddr = i;
639917205eccSRandall Stewart 			break;
640017205eccSRandall Stewart 		}
640117205eccSRandall Stewart 		sa = (struct sockaddr *)((caddr_t)sa + incr);
640217205eccSRandall Stewart 	}
640317205eccSRandall Stewart 	return ((struct sctp_tcb *)NULL);
640417205eccSRandall Stewart }
640535918f85SRandall Stewart 
640635918f85SRandall Stewart /*
640735918f85SRandall Stewart  * sctp_bindx(ADD) for one address.
640835918f85SRandall Stewart  * assumes all arguments are valid/checked by caller.
640935918f85SRandall Stewart  */
641035918f85SRandall Stewart void
641135918f85SRandall Stewart sctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp,
641235918f85SRandall Stewart     struct sockaddr *sa, sctp_assoc_t assoc_id,
641335918f85SRandall Stewart     uint32_t vrf_id, int *error, void *p)
641435918f85SRandall Stewart {
641535918f85SRandall Stewart 	struct sockaddr *addr_touse;
64165e2c2d87SRandall Stewart 
64175e2c2d87SRandall Stewart #ifdef INET6
641835918f85SRandall Stewart 	struct sockaddr_in sin;
641935918f85SRandall Stewart 
64205e2c2d87SRandall Stewart #endif
64215e2c2d87SRandall Stewart 
642235918f85SRandall Stewart 	/* see if we're bound all already! */
642335918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6424c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
642535918f85SRandall Stewart 		*error = EINVAL;
642635918f85SRandall Stewart 		return;
642735918f85SRandall Stewart 	}
642835918f85SRandall Stewart 	addr_touse = sa;
6429ea5eba11SMichael Tuexen #ifdef INET6
643035918f85SRandall Stewart 	if (sa->sa_family == AF_INET6) {
643135918f85SRandall Stewart 		struct sockaddr_in6 *sin6;
643235918f85SRandall Stewart 
643335918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6434c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
643535918f85SRandall Stewart 			*error = EINVAL;
643635918f85SRandall Stewart 			return;
643735918f85SRandall Stewart 		}
6438db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6439db4fd95bSRandall Stewart 			/* can only bind v6 on PF_INET6 sockets */
6440c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6441db4fd95bSRandall Stewart 			*error = EINVAL;
6442db4fd95bSRandall Stewart 			return;
6443db4fd95bSRandall Stewart 		}
644435918f85SRandall Stewart 		sin6 = (struct sockaddr_in6 *)addr_touse;
644535918f85SRandall Stewart 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6446db4fd95bSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6447db4fd95bSRandall Stewart 			    SCTP_IPV6_V6ONLY(inp)) {
6448db4fd95bSRandall Stewart 				/* can't bind v4-mapped on PF_INET sockets */
6449c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6450db4fd95bSRandall Stewart 				*error = EINVAL;
6451db4fd95bSRandall Stewart 				return;
6452db4fd95bSRandall Stewart 			}
645335918f85SRandall Stewart 			in6_sin6_2_sin(&sin, sin6);
645435918f85SRandall Stewart 			addr_touse = (struct sockaddr *)&sin;
645535918f85SRandall Stewart 		}
645635918f85SRandall Stewart 	}
645735918f85SRandall Stewart #endif
6458ea5eba11SMichael Tuexen #ifdef INET
645935918f85SRandall Stewart 	if (sa->sa_family == AF_INET) {
646035918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in)) {
6461c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
646235918f85SRandall Stewart 			*error = EINVAL;
646335918f85SRandall Stewart 			return;
646435918f85SRandall Stewart 		}
6465db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6466db4fd95bSRandall Stewart 		    SCTP_IPV6_V6ONLY(inp)) {
6467db4fd95bSRandall Stewart 			/* can't bind v4 on PF_INET sockets */
6468c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6469db4fd95bSRandall Stewart 			*error = EINVAL;
6470db4fd95bSRandall Stewart 			return;
6471db4fd95bSRandall Stewart 		}
647235918f85SRandall Stewart 	}
6473ea5eba11SMichael Tuexen #endif
647435918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
647535918f85SRandall Stewart 		if (p == NULL) {
647635918f85SRandall Stewart 			/* Can't get proc for Net/Open BSD */
6477c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
647835918f85SRandall Stewart 			*error = EINVAL;
647935918f85SRandall Stewart 			return;
648035918f85SRandall Stewart 		}
64811b649582SRandall Stewart 		*error = sctp_inpcb_bind(so, addr_touse, NULL, p);
648235918f85SRandall Stewart 		return;
648335918f85SRandall Stewart 	}
648435918f85SRandall Stewart 	/*
648535918f85SRandall Stewart 	 * No locks required here since bind and mgmt_ep_sa all do their own
648635918f85SRandall Stewart 	 * locking. If we do something for the FIX: below we may need to
648735918f85SRandall Stewart 	 * lock in that case.
648835918f85SRandall Stewart 	 */
648935918f85SRandall Stewart 	if (assoc_id == 0) {
649035918f85SRandall Stewart 		/* add the address */
649135918f85SRandall Stewart 		struct sctp_inpcb *lep;
649297c76f10SRandall Stewart 		struct sockaddr_in *lsin = (struct sockaddr_in *)addr_touse;
649335918f85SRandall Stewart 
649497c76f10SRandall Stewart 		/* validate the incoming port */
649597c76f10SRandall Stewart 		if ((lsin->sin_port != 0) &&
649697c76f10SRandall Stewart 		    (lsin->sin_port != inp->sctp_lport)) {
6497c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
649897c76f10SRandall Stewart 			*error = EINVAL;
649997c76f10SRandall Stewart 			return;
650097c76f10SRandall Stewart 		} else {
650197c76f10SRandall Stewart 			/* user specified 0 port, set it to existing port */
650297c76f10SRandall Stewart 			lsin->sin_port = inp->sctp_lport;
650397c76f10SRandall Stewart 		}
650497c76f10SRandall Stewart 
650535918f85SRandall Stewart 		lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id);
650635918f85SRandall Stewart 		if (lep != NULL) {
650735918f85SRandall Stewart 			/*
650835918f85SRandall Stewart 			 * We must decrement the refcount since we have the
650935918f85SRandall Stewart 			 * ep already and are binding. No remove going on
651035918f85SRandall Stewart 			 * here.
651135918f85SRandall Stewart 			 */
65126d9e8f2bSRandall Stewart 			SCTP_INP_DECR_REF(lep);
651335918f85SRandall Stewart 		}
651435918f85SRandall Stewart 		if (lep == inp) {
651535918f85SRandall Stewart 			/* already bound to it.. ok */
651635918f85SRandall Stewart 			return;
651735918f85SRandall Stewart 		} else if (lep == NULL) {
651835918f85SRandall Stewart 			((struct sockaddr_in *)addr_touse)->sin_port = 0;
651935918f85SRandall Stewart 			*error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
652035918f85SRandall Stewart 			    SCTP_ADD_IP_ADDRESS,
652180fefe0aSRandall Stewart 			    vrf_id, NULL);
652235918f85SRandall Stewart 		} else {
652335918f85SRandall Stewart 			*error = EADDRINUSE;
652435918f85SRandall Stewart 		}
652535918f85SRandall Stewart 		if (*error)
652635918f85SRandall Stewart 			return;
652735918f85SRandall Stewart 	} else {
652835918f85SRandall Stewart 		/*
652935918f85SRandall Stewart 		 * FIX: decide whether we allow assoc based bindx
653035918f85SRandall Stewart 		 */
653135918f85SRandall Stewart 	}
653235918f85SRandall Stewart }
653335918f85SRandall Stewart 
653435918f85SRandall Stewart /*
653535918f85SRandall Stewart  * sctp_bindx(DELETE) for one address.
653635918f85SRandall Stewart  * assumes all arguments are valid/checked by caller.
653735918f85SRandall Stewart  */
653835918f85SRandall Stewart void
65397215cc1bSMichael Tuexen sctp_bindx_delete_address(struct sctp_inpcb *inp,
654035918f85SRandall Stewart     struct sockaddr *sa, sctp_assoc_t assoc_id,
654135918f85SRandall Stewart     uint32_t vrf_id, int *error)
654235918f85SRandall Stewart {
654335918f85SRandall Stewart 	struct sockaddr *addr_touse;
65445e2c2d87SRandall Stewart 
65455e2c2d87SRandall Stewart #ifdef INET6
654635918f85SRandall Stewart 	struct sockaddr_in sin;
654735918f85SRandall Stewart 
65485e2c2d87SRandall Stewart #endif
65495e2c2d87SRandall Stewart 
655035918f85SRandall Stewart 	/* see if we're bound all already! */
655135918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6552c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
655335918f85SRandall Stewart 		*error = EINVAL;
655435918f85SRandall Stewart 		return;
655535918f85SRandall Stewart 	}
655635918f85SRandall Stewart 	addr_touse = sa;
6557e0e00a4dSMichael Tuexen #ifdef INET6
655835918f85SRandall Stewart 	if (sa->sa_family == AF_INET6) {
655935918f85SRandall Stewart 		struct sockaddr_in6 *sin6;
656035918f85SRandall Stewart 
656135918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6562c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
656335918f85SRandall Stewart 			*error = EINVAL;
656435918f85SRandall Stewart 			return;
656535918f85SRandall Stewart 		}
6566db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6567db4fd95bSRandall Stewart 			/* can only bind v6 on PF_INET6 sockets */
6568c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6569db4fd95bSRandall Stewart 			*error = EINVAL;
6570db4fd95bSRandall Stewart 			return;
6571db4fd95bSRandall Stewart 		}
657235918f85SRandall Stewart 		sin6 = (struct sockaddr_in6 *)addr_touse;
657335918f85SRandall Stewart 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6574db4fd95bSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6575db4fd95bSRandall Stewart 			    SCTP_IPV6_V6ONLY(inp)) {
6576db4fd95bSRandall Stewart 				/* can't bind mapped-v4 on PF_INET sockets */
6577c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6578db4fd95bSRandall Stewart 				*error = EINVAL;
6579db4fd95bSRandall Stewart 				return;
6580db4fd95bSRandall Stewart 			}
658135918f85SRandall Stewart 			in6_sin6_2_sin(&sin, sin6);
658235918f85SRandall Stewart 			addr_touse = (struct sockaddr *)&sin;
658335918f85SRandall Stewart 		}
658435918f85SRandall Stewart 	}
658535918f85SRandall Stewart #endif
6586ea5eba11SMichael Tuexen #ifdef INET
658735918f85SRandall Stewart 	if (sa->sa_family == AF_INET) {
658835918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in)) {
6589c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
659035918f85SRandall Stewart 			*error = EINVAL;
659135918f85SRandall Stewart 			return;
659235918f85SRandall Stewart 		}
6593db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6594db4fd95bSRandall Stewart 		    SCTP_IPV6_V6ONLY(inp)) {
6595db4fd95bSRandall Stewart 			/* can't bind v4 on PF_INET sockets */
6596c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6597db4fd95bSRandall Stewart 			*error = EINVAL;
6598db4fd95bSRandall Stewart 			return;
6599db4fd95bSRandall Stewart 		}
660035918f85SRandall Stewart 	}
6601ea5eba11SMichael Tuexen #endif
660235918f85SRandall Stewart 	/*
660335918f85SRandall Stewart 	 * No lock required mgmt_ep_sa does its own locking. If the FIX:
660435918f85SRandall Stewart 	 * below is ever changed we may need to lock before calling
660535918f85SRandall Stewart 	 * association level binding.
660635918f85SRandall Stewart 	 */
660735918f85SRandall Stewart 	if (assoc_id == 0) {
660835918f85SRandall Stewart 		/* delete the address */
660935918f85SRandall Stewart 		*error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
661035918f85SRandall Stewart 		    SCTP_DEL_IP_ADDRESS,
661180fefe0aSRandall Stewart 		    vrf_id, NULL);
661235918f85SRandall Stewart 	} else {
661335918f85SRandall Stewart 		/*
661435918f85SRandall Stewart 		 * FIX: decide whether we allow assoc based bindx
661535918f85SRandall Stewart 		 */
661635918f85SRandall Stewart 	}
661735918f85SRandall Stewart }
66181b649582SRandall Stewart 
66191b649582SRandall Stewart /*
66201b649582SRandall Stewart  * returns the valid local address count for an assoc, taking into account
66211b649582SRandall Stewart  * all scoping rules
66221b649582SRandall Stewart  */
66231b649582SRandall Stewart int
66241b649582SRandall Stewart sctp_local_addr_count(struct sctp_tcb *stcb)
66251b649582SRandall Stewart {
6626b54ddf22SMichael Tuexen 	int loopback_scope;
6627b54ddf22SMichael Tuexen 
6628b54ddf22SMichael Tuexen #if defined(INET)
6629b54ddf22SMichael Tuexen 	int ipv4_local_scope, ipv4_addr_legal;
6630b54ddf22SMichael Tuexen 
6631b54ddf22SMichael Tuexen #endif
6632b54ddf22SMichael Tuexen #if defined (INET6)
6633b54ddf22SMichael Tuexen 	int local_scope, site_scope, ipv6_addr_legal;
6634b54ddf22SMichael Tuexen 
6635b54ddf22SMichael Tuexen #endif
66361b649582SRandall Stewart 	struct sctp_vrf *vrf;
66371b649582SRandall Stewart 	struct sctp_ifn *sctp_ifn;
66381b649582SRandall Stewart 	struct sctp_ifa *sctp_ifa;
66391b649582SRandall Stewart 	int count = 0;
66401b649582SRandall Stewart 
66411b649582SRandall Stewart 	/* Turn on all the appropriate scopes */
6642a1cb341bSMichael Tuexen 	loopback_scope = stcb->asoc.scope.loopback_scope;
6643b54ddf22SMichael Tuexen #if defined(INET)
6644a1cb341bSMichael Tuexen 	ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
6645b54ddf22SMichael Tuexen 	ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
6646b54ddf22SMichael Tuexen #endif
6647b54ddf22SMichael Tuexen #if defined(INET6)
6648a1cb341bSMichael Tuexen 	local_scope = stcb->asoc.scope.local_scope;
6649a1cb341bSMichael Tuexen 	site_scope = stcb->asoc.scope.site_scope;
6650a1cb341bSMichael Tuexen 	ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
6651b54ddf22SMichael Tuexen #endif
6652c99efcf6SRandall Stewart 	SCTP_IPI_ADDR_RLOCK();
66531b649582SRandall Stewart 	vrf = sctp_find_vrf(stcb->asoc.vrf_id);
66541b649582SRandall Stewart 	if (vrf == NULL) {
66551b649582SRandall Stewart 		/* no vrf, no addresses */
6656c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RUNLOCK();
66571b649582SRandall Stewart 		return (0);
66581b649582SRandall Stewart 	}
66591b649582SRandall Stewart 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
66601b649582SRandall Stewart 		/*
66611b649582SRandall Stewart 		 * bound all case: go through all ifns on the vrf
66621b649582SRandall Stewart 		 */
66631b649582SRandall Stewart 		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
66641b649582SRandall Stewart 			if ((loopback_scope == 0) &&
66651b649582SRandall Stewart 			    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
66661b649582SRandall Stewart 				continue;
66671b649582SRandall Stewart 			}
66681b649582SRandall Stewart 			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
66691b649582SRandall Stewart 				if (sctp_is_addr_restricted(stcb, sctp_ifa))
66701b649582SRandall Stewart 					continue;
66715e2c2d87SRandall Stewart 				switch (sctp_ifa->address.sa.sa_family) {
6672ea5eba11SMichael Tuexen #ifdef INET
66735e2c2d87SRandall Stewart 				case AF_INET:
66745e2c2d87SRandall Stewart 					if (ipv4_addr_legal) {
66751b649582SRandall Stewart 						struct sockaddr_in *sin;
66761b649582SRandall Stewart 
66771b649582SRandall Stewart 						sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
66781b649582SRandall Stewart 						if (sin->sin_addr.s_addr == 0) {
66795e2c2d87SRandall Stewart 							/*
66805e2c2d87SRandall Stewart 							 * skip unspecified
66815e2c2d87SRandall Stewart 							 * addrs
66825e2c2d87SRandall Stewart 							 */
66831b649582SRandall Stewart 							continue;
66841b649582SRandall Stewart 						}
66856ba22f19SMichael Tuexen 						if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
66866ba22f19SMichael Tuexen 						    &sin->sin_addr) != 0) {
66876ba22f19SMichael Tuexen 							continue;
66886ba22f19SMichael Tuexen 						}
66891b649582SRandall Stewart 						if ((ipv4_local_scope == 0) &&
66901b649582SRandall Stewart 						    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
66911b649582SRandall Stewart 							continue;
66921b649582SRandall Stewart 						}
66931b649582SRandall Stewart 						/* count this one */
66941b649582SRandall Stewart 						count++;
66955e2c2d87SRandall Stewart 					} else {
66965e2c2d87SRandall Stewart 						continue;
66975e2c2d87SRandall Stewart 					}
66985e2c2d87SRandall Stewart 					break;
6699ea5eba11SMichael Tuexen #endif
67005e2c2d87SRandall Stewart #ifdef INET6
67015e2c2d87SRandall Stewart 				case AF_INET6:
67025e2c2d87SRandall Stewart 					if (ipv6_addr_legal) {
67031b649582SRandall Stewart 						struct sockaddr_in6 *sin6;
67041b649582SRandall Stewart 
67051b649582SRandall Stewart 						sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
67061b649582SRandall Stewart 						if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
67071b649582SRandall Stewart 							continue;
67081b649582SRandall Stewart 						}
67096ba22f19SMichael Tuexen 						if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
67106ba22f19SMichael Tuexen 						    &sin6->sin6_addr) != 0) {
67116ba22f19SMichael Tuexen 							continue;
67126ba22f19SMichael Tuexen 						}
67131b649582SRandall Stewart 						if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
67141b649582SRandall Stewart 							if (local_scope == 0)
67151b649582SRandall Stewart 								continue;
67161b649582SRandall Stewart 							if (sin6->sin6_scope_id == 0) {
67171b649582SRandall Stewart 								if (sa6_recoverscope(sin6) != 0)
67181b649582SRandall Stewart 									/*
67195e2c2d87SRandall Stewart 									 *
67205e2c2d87SRandall Stewart 									 * bad
67215e2c2d87SRandall Stewart 									 *
67225e2c2d87SRandall Stewart 									 * li
67235e2c2d87SRandall Stewart 									 * nk
67245e2c2d87SRandall Stewart 									 *
67255e2c2d87SRandall Stewart 									 * loc
67265e2c2d87SRandall Stewart 									 * al
67275e2c2d87SRandall Stewart 									 *
67285e2c2d87SRandall Stewart 									 * add
67295e2c2d87SRandall Stewart 									 * re
67305e2c2d87SRandall Stewart 									 * ss
67315e2c2d87SRandall Stewart 									 * */
67321b649582SRandall Stewart 									continue;
67331b649582SRandall Stewart 							}
67341b649582SRandall Stewart 						}
67351b649582SRandall Stewart 						if ((site_scope == 0) &&
67361b649582SRandall Stewart 						    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
67371b649582SRandall Stewart 							continue;
67381b649582SRandall Stewart 						}
67391b649582SRandall Stewart 						/* count this one */
67401b649582SRandall Stewart 						count++;
67411b649582SRandall Stewart 					}
67425e2c2d87SRandall Stewart 					break;
67435e2c2d87SRandall Stewart #endif
67445e2c2d87SRandall Stewart 				default:
67455e2c2d87SRandall Stewart 					/* TSNH */
67465e2c2d87SRandall Stewart 					break;
67475e2c2d87SRandall Stewart 				}
67481b649582SRandall Stewart 			}
67491b649582SRandall Stewart 		}
67501b649582SRandall Stewart 	} else {
67511b649582SRandall Stewart 		/*
67521b649582SRandall Stewart 		 * subset bound case
67531b649582SRandall Stewart 		 */
67541b649582SRandall Stewart 		struct sctp_laddr *laddr;
67551b649582SRandall Stewart 
67561b649582SRandall Stewart 		LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list,
67571b649582SRandall Stewart 		    sctp_nxt_addr) {
67581b649582SRandall Stewart 			if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
67591b649582SRandall Stewart 				continue;
67601b649582SRandall Stewart 			}
67611b649582SRandall Stewart 			/* count this one */
67621b649582SRandall Stewart 			count++;
67631b649582SRandall Stewart 		}
67641b649582SRandall Stewart 	}
6765c99efcf6SRandall Stewart 	SCTP_IPI_ADDR_RUNLOCK();
67661b649582SRandall Stewart 	return (count);
67671b649582SRandall Stewart }
6768c4739e2fSRandall Stewart 
6769c4739e2fSRandall Stewart #if defined(SCTP_LOCAL_TRACE_BUF)
6770c4739e2fSRandall Stewart 
6771c4739e2fSRandall Stewart void
6772b27a6b7dSRandall 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)
6773c4739e2fSRandall Stewart {
6774b27a6b7dSRandall Stewart 	uint32_t saveindex, newindex;
6775c4739e2fSRandall Stewart 
6776c4739e2fSRandall Stewart 	do {
6777b3f1ea41SRandall Stewart 		saveindex = SCTP_BASE_SYSCTL(sctp_log).index;
6778c4739e2fSRandall Stewart 		if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
6779c4739e2fSRandall Stewart 			newindex = 1;
6780c4739e2fSRandall Stewart 		} else {
6781c4739e2fSRandall Stewart 			newindex = saveindex + 1;
6782c4739e2fSRandall Stewart 		}
6783b3f1ea41SRandall Stewart 	} while (atomic_cmpset_int(&SCTP_BASE_SYSCTL(sctp_log).index, saveindex, newindex) == 0);
6784c4739e2fSRandall Stewart 	if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
6785c4739e2fSRandall Stewart 		saveindex = 0;
6786c4739e2fSRandall Stewart 	}
6787b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].timestamp = SCTP_GET_CYCLECOUNT;
6788b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].subsys = subsys;
6789b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[0] = a;
6790b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[1] = b;
6791b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[2] = c;
6792b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[3] = d;
6793b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[4] = e;
6794b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[5] = f;
6795c4739e2fSRandall Stewart }
6796c4739e2fSRandall Stewart 
6797c4739e2fSRandall Stewart #endif
6798a99b6783SRandall Stewart static void
6799a99b6783SRandall Stewart sctp_recv_udp_tunneled_packet(struct mbuf *m, int off, struct inpcb *ignored)
6800a99b6783SRandall Stewart {
6801a99b6783SRandall Stewart 	struct ip *iph;
68023a51a264SMichael Tuexen 
68033a51a264SMichael Tuexen #ifdef INET6
68043a51a264SMichael Tuexen 	struct ip6_hdr *ip6;
68053a51a264SMichael Tuexen 
68063a51a264SMichael Tuexen #endif
6807a99b6783SRandall Stewart 	struct mbuf *sp, *last;
6808a99b6783SRandall Stewart 	struct udphdr *uhdr;
6809285052f0SMichael Tuexen 	uint16_t port;
6810a99b6783SRandall Stewart 
6811a99b6783SRandall Stewart 	if ((m->m_flags & M_PKTHDR) == 0) {
6812a99b6783SRandall Stewart 		/* Can't handle one that is not a pkt hdr */
6813a99b6783SRandall Stewart 		goto out;
6814a99b6783SRandall Stewart 	}
6815285052f0SMichael Tuexen 	/* Pull the src port */
6816a99b6783SRandall Stewart 	iph = mtod(m, struct ip *);
6817a99b6783SRandall Stewart 	uhdr = (struct udphdr *)((caddr_t)iph + off);
6818a99b6783SRandall Stewart 	port = uhdr->uh_sport;
6819285052f0SMichael Tuexen 	/*
6820285052f0SMichael Tuexen 	 * Split out the mbuf chain. Leave the IP header in m, place the
6821285052f0SMichael Tuexen 	 * rest in the sp.
6822285052f0SMichael Tuexen 	 */
6823eb1b1807SGleb Smirnoff 	sp = m_split(m, off, M_NOWAIT);
6824a99b6783SRandall Stewart 	if (sp == NULL) {
6825a99b6783SRandall Stewart 		/* Gak, drop packet, we can't do a split */
6826a99b6783SRandall Stewart 		goto out;
6827a99b6783SRandall Stewart 	}
6828285052f0SMichael Tuexen 	if (sp->m_pkthdr.len < sizeof(struct udphdr) + sizeof(struct sctphdr)) {
6829285052f0SMichael Tuexen 		/* Gak, packet can't have an SCTP header in it - too small */
6830a99b6783SRandall Stewart 		m_freem(sp);
6831a99b6783SRandall Stewart 		goto out;
6832a99b6783SRandall Stewart 	}
6833285052f0SMichael Tuexen 	/* Now pull up the UDP header and SCTP header together */
6834285052f0SMichael Tuexen 	sp = m_pullup(sp, sizeof(struct udphdr) + sizeof(struct sctphdr));
6835a99b6783SRandall Stewart 	if (sp == NULL) {
6836a99b6783SRandall Stewart 		/* Gak pullup failed */
6837a99b6783SRandall Stewart 		goto out;
6838a99b6783SRandall Stewart 	}
6839285052f0SMichael Tuexen 	/* Trim out the UDP header */
6840a99b6783SRandall Stewart 	m_adj(sp, sizeof(struct udphdr));
6841a99b6783SRandall Stewart 
6842a99b6783SRandall Stewart 	/* Now reconstruct the mbuf chain */
6843285052f0SMichael Tuexen 	for (last = m; last->m_next; last = last->m_next);
6844a99b6783SRandall Stewart 	last->m_next = sp;
6845a99b6783SRandall Stewart 	m->m_pkthdr.len += sp->m_pkthdr.len;
6846a99b6783SRandall Stewart 	iph = mtod(m, struct ip *);
6847a99b6783SRandall Stewart 	switch (iph->ip_v) {
6848e6194c2eSMichael Tuexen #ifdef INET
6849a99b6783SRandall Stewart 	case IPVERSION:
685009c1c856SMichael Tuexen 		iph->ip_len = htons(ntohs(iph->ip_len) - sizeof(struct udphdr));
6851a99b6783SRandall Stewart 		sctp_input_with_port(m, off, port);
6852a99b6783SRandall Stewart 		break;
6853e6194c2eSMichael Tuexen #endif
6854a99b6783SRandall Stewart #ifdef INET6
6855a99b6783SRandall Stewart 	case IPV6_VERSION >> 4:
68563a51a264SMichael Tuexen 		ip6 = mtod(m, struct ip6_hdr *);
68573a51a264SMichael Tuexen 		ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - sizeof(struct udphdr));
68583a51a264SMichael Tuexen 		sctp6_input_with_port(&m, &off, port);
6859a99b6783SRandall Stewart 		break;
6860a99b6783SRandall Stewart #endif
6861a99b6783SRandall Stewart 	default:
6862285052f0SMichael Tuexen 		goto out;
6863a99b6783SRandall Stewart 		break;
6864a99b6783SRandall Stewart 	}
6865a99b6783SRandall Stewart 	return;
6866a99b6783SRandall Stewart out:
6867a99b6783SRandall Stewart 	m_freem(m);
6868a99b6783SRandall Stewart }
6869c54a18d2SRandall Stewart 
6870c54a18d2SRandall Stewart void
6871c54a18d2SRandall Stewart sctp_over_udp_stop(void)
6872c54a18d2SRandall Stewart {
6873a99b6783SRandall Stewart 	/*
6874a99b6783SRandall Stewart 	 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
6875a99b6783SRandall Stewart 	 * for writting!
6876a99b6783SRandall Stewart 	 */
68773a51a264SMichael Tuexen #ifdef INET
68783a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
68793a51a264SMichael Tuexen 		soclose(SCTP_BASE_INFO(udp4_tun_socket));
68803a51a264SMichael Tuexen 		SCTP_BASE_INFO(udp4_tun_socket) = NULL;
6881c54a18d2SRandall Stewart 	}
68823a51a264SMichael Tuexen #endif
68833a51a264SMichael Tuexen #ifdef INET6
68843a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
68853a51a264SMichael Tuexen 		soclose(SCTP_BASE_INFO(udp6_tun_socket));
68863a51a264SMichael Tuexen 		SCTP_BASE_INFO(udp6_tun_socket) = NULL;
68873a51a264SMichael Tuexen 	}
68883a51a264SMichael Tuexen #endif
6889a99b6783SRandall Stewart }
6890ea5eba11SMichael Tuexen 
6891c54a18d2SRandall Stewart int
6892c54a18d2SRandall Stewart sctp_over_udp_start(void)
6893c54a18d2SRandall Stewart {
6894a99b6783SRandall Stewart 	uint16_t port;
6895a99b6783SRandall Stewart 	int ret;
6896a99b6783SRandall Stewart 
68973a51a264SMichael Tuexen #ifdef INET
68983a51a264SMichael Tuexen 	struct sockaddr_in sin;
68993a51a264SMichael Tuexen 
69003a51a264SMichael Tuexen #endif
69013a51a264SMichael Tuexen #ifdef INET6
69023a51a264SMichael Tuexen 	struct sockaddr_in6 sin6;
69033a51a264SMichael Tuexen 
69043a51a264SMichael Tuexen #endif
6905a99b6783SRandall Stewart 	/*
6906a99b6783SRandall Stewart 	 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
6907a99b6783SRandall Stewart 	 * for writting!
6908a99b6783SRandall Stewart 	 */
6909a99b6783SRandall Stewart 	port = SCTP_BASE_SYSCTL(sctp_udp_tunneling_port);
69103a51a264SMichael Tuexen 	if (ntohs(port) == 0) {
6911a99b6783SRandall Stewart 		/* Must have a port set */
6912a99b6783SRandall Stewart 		return (EINVAL);
6913a99b6783SRandall Stewart 	}
69143a51a264SMichael Tuexen #ifdef INET
69153a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
6916a99b6783SRandall Stewart 		/* Already running -- must stop first */
6917a99b6783SRandall Stewart 		return (EALREADY);
6918a99b6783SRandall Stewart 	}
69193a51a264SMichael Tuexen #endif
69203a51a264SMichael Tuexen #ifdef INET6
69213a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
69223a51a264SMichael Tuexen 		/* Already running -- must stop first */
69233a51a264SMichael Tuexen 		return (EALREADY);
6924a99b6783SRandall Stewart 	}
69253a51a264SMichael Tuexen #endif
69263a51a264SMichael Tuexen #ifdef INET
69273a51a264SMichael Tuexen 	if ((ret = socreate(PF_INET, &SCTP_BASE_INFO(udp4_tun_socket),
69283a51a264SMichael Tuexen 	    SOCK_DGRAM, IPPROTO_UDP,
69293a51a264SMichael Tuexen 	    curthread->td_ucred, curthread))) {
6930a99b6783SRandall Stewart 		sctp_over_udp_stop();
6931a99b6783SRandall Stewart 		return (ret);
6932a99b6783SRandall Stewart 	}
69333a51a264SMichael Tuexen 	/* Call the special UDP hook. */
69343a51a264SMichael Tuexen 	if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp4_tun_socket),
69353a51a264SMichael Tuexen 	    sctp_recv_udp_tunneled_packet))) {
69363a51a264SMichael Tuexen 		sctp_over_udp_stop();
69373a51a264SMichael Tuexen 		return (ret);
69383a51a264SMichael Tuexen 	}
69393a51a264SMichael Tuexen 	/* Ok, we have a socket, bind it to the port. */
69403a51a264SMichael Tuexen 	memset(&sin, 0, sizeof(struct sockaddr_in));
69413a51a264SMichael Tuexen 	sin.sin_len = sizeof(struct sockaddr_in);
69423a51a264SMichael Tuexen 	sin.sin_family = AF_INET;
69433a51a264SMichael Tuexen 	sin.sin_port = htons(port);
69443a51a264SMichael Tuexen 	if ((ret = sobind(SCTP_BASE_INFO(udp4_tun_socket),
69453a51a264SMichael Tuexen 	    (struct sockaddr *)&sin, curthread))) {
69463a51a264SMichael Tuexen 		sctp_over_udp_stop();
69473a51a264SMichael Tuexen 		return (ret);
69483a51a264SMichael Tuexen 	}
69493a51a264SMichael Tuexen #endif
69503a51a264SMichael Tuexen #ifdef INET6
69513a51a264SMichael Tuexen 	if ((ret = socreate(PF_INET6, &SCTP_BASE_INFO(udp6_tun_socket),
69523a51a264SMichael Tuexen 	    SOCK_DGRAM, IPPROTO_UDP,
69533a51a264SMichael Tuexen 	    curthread->td_ucred, curthread))) {
69543a51a264SMichael Tuexen 		sctp_over_udp_stop();
69553a51a264SMichael Tuexen 		return (ret);
69563a51a264SMichael Tuexen 	}
69573a51a264SMichael Tuexen 	/* Call the special UDP hook. */
69583a51a264SMichael Tuexen 	if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp6_tun_socket),
69593a51a264SMichael Tuexen 	    sctp_recv_udp_tunneled_packet))) {
69603a51a264SMichael Tuexen 		sctp_over_udp_stop();
69613a51a264SMichael Tuexen 		return (ret);
69623a51a264SMichael Tuexen 	}
69633a51a264SMichael Tuexen 	/* Ok, we have a socket, bind it to the port. */
69643a51a264SMichael Tuexen 	memset(&sin6, 0, sizeof(struct sockaddr_in6));
69653a51a264SMichael Tuexen 	sin6.sin6_len = sizeof(struct sockaddr_in6);
69663a51a264SMichael Tuexen 	sin6.sin6_family = AF_INET6;
69673a51a264SMichael Tuexen 	sin6.sin6_port = htons(port);
69683a51a264SMichael Tuexen 	if ((ret = sobind(SCTP_BASE_INFO(udp6_tun_socket),
69693a51a264SMichael Tuexen 	    (struct sockaddr *)&sin6, curthread))) {
69703a51a264SMichael Tuexen 		sctp_over_udp_stop();
69713a51a264SMichael Tuexen 		return (ret);
69723a51a264SMichael Tuexen 	}
69733a51a264SMichael Tuexen #endif
6974a99b6783SRandall Stewart 	return (0);
6975c54a18d2SRandall Stewart }
6976