xref: /freebsd/sys/netinet/sctputil.c (revision 317e00ef863d88f89a17d10ce24031e1a0eac010)
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;
908dd973b0eSMichael Tuexen 	asoc->prsctp_supported = inp->prsctp_supported;
909*317e00efSMichael Tuexen 	asoc->reconfig_supported = inp->reconfig_supported;
910caea9879SMichael Tuexen 	asoc->nrsack_supported = inp->nrsack_supported;
911cb9b8e6fSMichael Tuexen 	asoc->pktdrop_supported = inp->pktdrop_supported;
912ca85e948SMichael Tuexen 	asoc->sctp_cmt_pf = (uint8_t) 0;
913a1cb341bSMichael Tuexen 	asoc->sctp_frag_point = inp->sctp_frag_point;
914a1cb341bSMichael Tuexen 	asoc->sctp_features = inp->sctp_features;
915a1cb341bSMichael Tuexen 	asoc->default_dscp = inp->sctp_ep.default_dscp;
91642551e99SRandall Stewart #ifdef INET6
917a1cb341bSMichael Tuexen 	if (inp->sctp_ep.default_flowlabel) {
918a1cb341bSMichael Tuexen 		asoc->default_flowlabel = inp->sctp_ep.default_flowlabel;
91958bdb691SMichael Tuexen 	} else {
920a1cb341bSMichael Tuexen 		if (inp->ip_inp.inp.inp_flags & IN6P_AUTOFLOWLABEL) {
921a1cb341bSMichael Tuexen 			asoc->default_flowlabel = sctp_select_initial_TSN(&inp->sctp_ep);
92258bdb691SMichael Tuexen 			asoc->default_flowlabel &= 0x000fffff;
92358bdb691SMichael Tuexen 			asoc->default_flowlabel |= 0x80000000;
92458bdb691SMichael Tuexen 		} else {
925f8829a4aSRandall Stewart 			asoc->default_flowlabel = 0;
92658bdb691SMichael Tuexen 		}
92758bdb691SMichael Tuexen 	}
928f8829a4aSRandall Stewart #endif
9299f22f500SRandall Stewart 	asoc->sb_send_resv = 0;
930f8829a4aSRandall Stewart 	if (override_tag) {
931f8829a4aSRandall Stewart 		asoc->my_vtag = override_tag;
932f8829a4aSRandall Stewart 	} else {
933a1cb341bSMichael Tuexen 		asoc->my_vtag = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
934f8829a4aSRandall Stewart 	}
935de0e935bSRandall Stewart 	/* Get the nonce tags */
936a1cb341bSMichael Tuexen 	asoc->my_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
937a1cb341bSMichael Tuexen 	asoc->peer_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
93842551e99SRandall Stewart 	asoc->vrf_id = vrf_id;
939de0e935bSRandall Stewart 
94018e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
94118e198d3SRandall Stewart 	asoc->tsn_in_at = 0;
94218e198d3SRandall Stewart 	asoc->tsn_out_at = 0;
94318e198d3SRandall Stewart 	asoc->tsn_in_wrapped = 0;
94418e198d3SRandall Stewart 	asoc->tsn_out_wrapped = 0;
94518e198d3SRandall Stewart 	asoc->cumack_log_at = 0;
946b201f536SRandall Stewart 	asoc->cumack_log_atsnt = 0;
94718e198d3SRandall Stewart #endif
94818e198d3SRandall Stewart #ifdef SCTP_FS_SPEC_LOG
94918e198d3SRandall Stewart 	asoc->fs_index = 0;
95018e198d3SRandall Stewart #endif
951f8829a4aSRandall Stewart 	asoc->refcnt = 0;
952f8829a4aSRandall Stewart 	asoc->assoc_up_sent = 0;
953f8829a4aSRandall Stewart 	asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number = asoc->sending_seq =
954a1cb341bSMichael Tuexen 	    sctp_select_initial_TSN(&inp->sctp_ep);
955c54a18d2SRandall Stewart 	asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
956f8829a4aSRandall Stewart 	/* we are optimisitic here */
957830d754dSRandall Stewart 	asoc->peer_supports_nat = 0;
958f8829a4aSRandall Stewart 	asoc->sent_queue_retran_cnt = 0;
959f8829a4aSRandall Stewart 
960f8829a4aSRandall Stewart 	/* for CMT */
9618933fa13SRandall Stewart 	asoc->last_net_cmt_send_started = NULL;
962f8829a4aSRandall Stewart 
963f8829a4aSRandall Stewart 	/* This will need to be adjusted */
964f8829a4aSRandall Stewart 	asoc->last_acked_seq = asoc->init_seq_number - 1;
965f8829a4aSRandall Stewart 	asoc->advanced_peer_ack_point = asoc->last_acked_seq;
966f8829a4aSRandall Stewart 	asoc->asconf_seq_in = asoc->last_acked_seq;
967f8829a4aSRandall Stewart 
968f8829a4aSRandall Stewart 	/* here we are different, we hold the next one we expect */
969f8829a4aSRandall Stewart 	asoc->str_reset_seq_in = asoc->last_acked_seq + 1;
970f8829a4aSRandall Stewart 
971a1cb341bSMichael Tuexen 	asoc->initial_init_rto_max = inp->sctp_ep.initial_init_rto_max;
972a1cb341bSMichael Tuexen 	asoc->initial_rto = inp->sctp_ep.initial_rto;
973f8829a4aSRandall Stewart 
974a1cb341bSMichael Tuexen 	asoc->max_init_times = inp->sctp_ep.max_init_times;
975a1cb341bSMichael Tuexen 	asoc->max_send_times = inp->sctp_ep.max_send_times;
976a1cb341bSMichael Tuexen 	asoc->def_net_failure = inp->sctp_ep.def_net_failure;
977a1cb341bSMichael Tuexen 	asoc->def_net_pf_threshold = inp->sctp_ep.def_net_pf_threshold;
978f8829a4aSRandall Stewart 	asoc->free_chunk_cnt = 0;
979f8829a4aSRandall Stewart 
980f8829a4aSRandall Stewart 	asoc->iam_blocking = 0;
981a1cb341bSMichael Tuexen 	asoc->context = inp->sctp_context;
982a1cb341bSMichael Tuexen 	asoc->local_strreset_support = inp->local_strreset_support;
983a1cb341bSMichael Tuexen 	asoc->def_send = inp->def_send;
984a1cb341bSMichael Tuexen 	asoc->delayed_ack = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
985a1cb341bSMichael Tuexen 	asoc->sack_freq = inp->sctp_ep.sctp_sack_freq;
986f8829a4aSRandall Stewart 	asoc->pr_sctp_cnt = 0;
987f8829a4aSRandall Stewart 	asoc->total_output_queue_size = 0;
988f8829a4aSRandall Stewart 
989a1cb341bSMichael Tuexen 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
990a1cb341bSMichael Tuexen 		asoc->scope.ipv6_addr_legal = 1;
991a1cb341bSMichael Tuexen 		if (SCTP_IPV6_V6ONLY(inp) == 0) {
992a1cb341bSMichael Tuexen 			asoc->scope.ipv4_addr_legal = 1;
993f8829a4aSRandall Stewart 		} else {
994a1cb341bSMichael Tuexen 			asoc->scope.ipv4_addr_legal = 0;
995f8829a4aSRandall Stewart 		}
996f8829a4aSRandall Stewart 	} else {
997a1cb341bSMichael Tuexen 		asoc->scope.ipv6_addr_legal = 0;
998a1cb341bSMichael Tuexen 		asoc->scope.ipv4_addr_legal = 1;
999f8829a4aSRandall Stewart 	}
1000f8829a4aSRandall Stewart 
1001a1cb341bSMichael Tuexen 	asoc->my_rwnd = max(SCTP_SB_LIMIT_RCV(inp->sctp_socket), SCTP_MINIMAL_RWND);
1002a1cb341bSMichael Tuexen 	asoc->peers_rwnd = SCTP_SB_LIMIT_RCV(inp->sctp_socket);
1003f8829a4aSRandall Stewart 
1004a1cb341bSMichael Tuexen 	asoc->smallest_mtu = inp->sctp_frag_point;
1005a1cb341bSMichael Tuexen 	asoc->minrto = inp->sctp_ep.sctp_minrto;
1006a1cb341bSMichael Tuexen 	asoc->maxrto = inp->sctp_ep.sctp_maxrto;
1007f8829a4aSRandall Stewart 
1008f8829a4aSRandall Stewart 	asoc->locked_on_sending = NULL;
1009f8829a4aSRandall Stewart 	asoc->stream_locked_on = 0;
1010f8829a4aSRandall Stewart 	asoc->ecn_echo_cnt_onq = 0;
1011f8829a4aSRandall Stewart 	asoc->stream_locked = 0;
1012f8829a4aSRandall Stewart 
101342551e99SRandall Stewart 	asoc->send_sack = 1;
101442551e99SRandall Stewart 
101542551e99SRandall Stewart 	LIST_INIT(&asoc->sctp_restricted_addrs);
101642551e99SRandall Stewart 
1017f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->nets);
1018f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->pending_reply_queue);
10192afb3e84SRandall Stewart 	TAILQ_INIT(&asoc->asconf_ack_sent);
1020f8829a4aSRandall Stewart 	/* Setup to fill the hb random cache at first HB */
1021f8829a4aSRandall Stewart 	asoc->hb_random_idx = 4;
1022f8829a4aSRandall Stewart 
1023a1cb341bSMichael Tuexen 	asoc->sctp_autoclose_ticks = inp->sctp_ep.auto_close_time;
1024f8829a4aSRandall Stewart 
1025a1cb341bSMichael Tuexen 	stcb->asoc.congestion_control_module = inp->sctp_ep.sctp_default_cc_module;
1026a1cb341bSMichael Tuexen 	stcb->asoc.cc_functions = sctp_cc_functions[inp->sctp_ep.sctp_default_cc_module];
1027b54d3a6cSRandall Stewart 
1028a1cb341bSMichael Tuexen 	stcb->asoc.stream_scheduling_module = inp->sctp_ep.sctp_default_ss_module;
1029a1cb341bSMichael Tuexen 	stcb->asoc.ss_functions = sctp_ss_functions[inp->sctp_ep.sctp_default_ss_module];
1030f7a77f6fSMichael Tuexen 
1031b54d3a6cSRandall Stewart 	/*
1032f8829a4aSRandall Stewart 	 * Now the stream parameters, here we allocate space for all streams
1033f8829a4aSRandall Stewart 	 * that we request by default.
1034f8829a4aSRandall Stewart 	 */
1035ea44232bSRandall Stewart 	asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams =
1036a1cb341bSMichael Tuexen 	    inp->sctp_ep.pre_open_stream_count;
1037f8829a4aSRandall Stewart 	SCTP_MALLOC(asoc->strmout, struct sctp_stream_out *,
1038f8829a4aSRandall Stewart 	    asoc->streamoutcnt * sizeof(struct sctp_stream_out),
1039207304d4SRandall Stewart 	    SCTP_M_STRMO);
1040f8829a4aSRandall Stewart 	if (asoc->strmout == NULL) {
1041f8829a4aSRandall Stewart 		/* big trouble no memory */
1042c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1043f8829a4aSRandall Stewart 		return (ENOMEM);
1044f8829a4aSRandall Stewart 	}
1045f8829a4aSRandall Stewart 	for (i = 0; i < asoc->streamoutcnt; i++) {
1046f8829a4aSRandall Stewart 		/*
1047f8829a4aSRandall Stewart 		 * inbound side must be set to 0xffff, also NOTE when we get
1048f8829a4aSRandall Stewart 		 * the INIT-ACK back (for INIT sender) we MUST reduce the
1049f8829a4aSRandall Stewart 		 * count (streamoutcnt) but first check if we sent to any of
1050f8829a4aSRandall Stewart 		 * the upper streams that were dropped (if some were). Those
1051f8829a4aSRandall Stewart 		 * that were dropped must be notified to the upper layer as
1052f8829a4aSRandall Stewart 		 * failed to send.
1053f8829a4aSRandall Stewart 		 */
1054f3b05218SMichael Tuexen 		asoc->strmout[i].next_sequence_send = 0x0;
1055f8829a4aSRandall Stewart 		TAILQ_INIT(&asoc->strmout[i].outqueue);
1056325c8c46SMichael Tuexen 		asoc->strmout[i].chunks_on_queues = 0;
1057f8829a4aSRandall Stewart 		asoc->strmout[i].stream_no = i;
1058f8829a4aSRandall Stewart 		asoc->strmout[i].last_msg_incomplete = 0;
1059252f7f93SMichael Tuexen 		asoc->ss_functions.sctp_ss_init_stream(&asoc->strmout[i], NULL);
1060f8829a4aSRandall Stewart 	}
1061f7a77f6fSMichael Tuexen 	asoc->ss_functions.sctp_ss_init(stcb, asoc, 0);
1062f7a77f6fSMichael Tuexen 
1063f8829a4aSRandall Stewart 	/* Now the mapping array */
1064f8829a4aSRandall Stewart 	asoc->mapping_array_size = SCTP_INITIAL_MAPPING_ARRAY;
1065f8829a4aSRandall Stewart 	SCTP_MALLOC(asoc->mapping_array, uint8_t *, asoc->mapping_array_size,
1066207304d4SRandall Stewart 	    SCTP_M_MAP);
1067f8829a4aSRandall Stewart 	if (asoc->mapping_array == NULL) {
1068207304d4SRandall Stewart 		SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1069c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1070f8829a4aSRandall Stewart 		return (ENOMEM);
1071f8829a4aSRandall Stewart 	}
1072f8829a4aSRandall Stewart 	memset(asoc->mapping_array, 0, asoc->mapping_array_size);
1073b5c16493SMichael Tuexen 	SCTP_MALLOC(asoc->nr_mapping_array, uint8_t *, asoc->mapping_array_size,
1074830d754dSRandall Stewart 	    SCTP_M_MAP);
1075bf1be571SRandall Stewart 	if (asoc->nr_mapping_array == NULL) {
1076bf1be571SRandall Stewart 		SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1077bf1be571SRandall Stewart 		SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1078bf1be571SRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1079bf1be571SRandall Stewart 		return (ENOMEM);
1080bf1be571SRandall Stewart 	}
1081b5c16493SMichael Tuexen 	memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
1082830d754dSRandall Stewart 
1083f8829a4aSRandall Stewart 	/* Now the init of the other outqueues */
1084f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->free_chunks);
1085f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->control_send_queue);
1086c54a18d2SRandall Stewart 	TAILQ_INIT(&asoc->asconf_send_queue);
1087f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->send_queue);
1088f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->sent_queue);
1089f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->reasmqueue);
1090f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->resetHead);
1091a1cb341bSMichael Tuexen 	asoc->max_inbound_streams = inp->sctp_ep.max_open_streams_intome;
1092f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->asconf_queue);
1093f8829a4aSRandall Stewart 	/* authentication fields */
1094f8829a4aSRandall Stewart 	asoc->authinfo.random = NULL;
1095830d754dSRandall Stewart 	asoc->authinfo.active_keyid = 0;
1096f8829a4aSRandall Stewart 	asoc->authinfo.assoc_key = NULL;
1097f8829a4aSRandall Stewart 	asoc->authinfo.assoc_keyid = 0;
1098f8829a4aSRandall Stewart 	asoc->authinfo.recv_key = NULL;
1099f8829a4aSRandall Stewart 	asoc->authinfo.recv_keyid = 0;
1100f8829a4aSRandall Stewart 	LIST_INIT(&asoc->shared_keys);
1101f42a358aSRandall Stewart 	asoc->marked_retrans = 0;
1102a1cb341bSMichael Tuexen 	asoc->port = inp->sctp_ep.port;
1103f42a358aSRandall Stewart 	asoc->timoinit = 0;
1104f42a358aSRandall Stewart 	asoc->timodata = 0;
1105f42a358aSRandall Stewart 	asoc->timosack = 0;
1106f42a358aSRandall Stewart 	asoc->timoshutdown = 0;
1107f42a358aSRandall Stewart 	asoc->timoheartbeat = 0;
1108f42a358aSRandall Stewart 	asoc->timocookie = 0;
1109f42a358aSRandall Stewart 	asoc->timoshutdownack = 0;
11106e55db54SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&asoc->start_time);
11116e55db54SRandall Stewart 	asoc->discontinuity_time = asoc->start_time;
1112eacc51c5SRandall Stewart 	/*
1113eacc51c5SRandall Stewart 	 * sa_ignore MEMLEAK {memory is put in the assoc mapping array and
111477acdc25SRandall Stewart 	 * freed later when the association is freed.
1115eacc51c5SRandall Stewart 	 */
1116f8829a4aSRandall Stewart 	return (0);
1117f8829a4aSRandall Stewart }
1118f8829a4aSRandall Stewart 
11190e13104dSRandall Stewart void
11200e13104dSRandall Stewart sctp_print_mapping_array(struct sctp_association *asoc)
11210e13104dSRandall Stewart {
1122aed5947cSMichael Tuexen 	unsigned int i, limit;
11230e13104dSRandall Stewart 
1124cd3fd531SMichael Tuexen 	SCTP_PRINTF("Mapping array size: %d, baseTSN: %8.8x, cumAck: %8.8x, highestTSN: (%8.8x, %8.8x).\n",
11250e13104dSRandall Stewart 	    asoc->mapping_array_size,
11260e13104dSRandall Stewart 	    asoc->mapping_array_base_tsn,
11270e13104dSRandall Stewart 	    asoc->cumulative_tsn,
1128aed5947cSMichael Tuexen 	    asoc->highest_tsn_inside_map,
1129aed5947cSMichael Tuexen 	    asoc->highest_tsn_inside_nr_map);
1130aed5947cSMichael Tuexen 	for (limit = asoc->mapping_array_size; limit > 1; limit--) {
113160990c0cSMichael Tuexen 		if (asoc->mapping_array[limit - 1] != 0) {
113277acdc25SRandall Stewart 			break;
113377acdc25SRandall Stewart 		}
113477acdc25SRandall Stewart 	}
1135cd3fd531SMichael Tuexen 	SCTP_PRINTF("Renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
113677acdc25SRandall Stewart 	for (i = 0; i < limit; i++) {
1137cd3fd531SMichael Tuexen 		SCTP_PRINTF("%2.2x%c", asoc->mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
113877acdc25SRandall Stewart 	}
1139aed5947cSMichael Tuexen 	if (limit % 16)
1140cd3fd531SMichael Tuexen 		SCTP_PRINTF("\n");
1141aed5947cSMichael Tuexen 	for (limit = asoc->mapping_array_size; limit > 1; limit--) {
1142aed5947cSMichael Tuexen 		if (asoc->nr_mapping_array[limit - 1]) {
114377acdc25SRandall Stewart 			break;
114477acdc25SRandall Stewart 		}
114577acdc25SRandall Stewart 	}
1146cd3fd531SMichael Tuexen 	SCTP_PRINTF("Non renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
114777acdc25SRandall Stewart 	for (i = 0; i < limit; i++) {
1148cd3fd531SMichael Tuexen 		SCTP_PRINTF("%2.2x%c", asoc->nr_mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
11490e13104dSRandall Stewart 	}
1150aed5947cSMichael Tuexen 	if (limit % 16)
1151cd3fd531SMichael Tuexen 		SCTP_PRINTF("\n");
11520e13104dSRandall Stewart }
11530e13104dSRandall Stewart 
1154f8829a4aSRandall Stewart int
11550696e120SRandall Stewart sctp_expand_mapping_array(struct sctp_association *asoc, uint32_t needed)
1156f8829a4aSRandall Stewart {
1157f8829a4aSRandall Stewart 	/* mapping array needs to grow */
1158b5c16493SMichael Tuexen 	uint8_t *new_array1, *new_array2;
11590696e120SRandall Stewart 	uint32_t new_size;
1160f8829a4aSRandall Stewart 
11610696e120SRandall Stewart 	new_size = asoc->mapping_array_size + ((needed + 7) / 8 + SCTP_MAPPING_ARRAY_INCR);
1162b5c16493SMichael Tuexen 	SCTP_MALLOC(new_array1, uint8_t *, new_size, SCTP_M_MAP);
1163b5c16493SMichael Tuexen 	SCTP_MALLOC(new_array2, uint8_t *, new_size, SCTP_M_MAP);
1164b5c16493SMichael Tuexen 	if ((new_array1 == NULL) || (new_array2 == NULL)) {
1165f8829a4aSRandall Stewart 		/* can't get more, forget it */
1166b5c16493SMichael Tuexen 		SCTP_PRINTF("No memory for expansion of SCTP mapping array %d\n", new_size);
1167b5c16493SMichael Tuexen 		if (new_array1) {
1168b5c16493SMichael Tuexen 			SCTP_FREE(new_array1, SCTP_M_MAP);
1169b5c16493SMichael Tuexen 		}
1170b5c16493SMichael Tuexen 		if (new_array2) {
1171b5c16493SMichael Tuexen 			SCTP_FREE(new_array2, SCTP_M_MAP);
1172b5c16493SMichael Tuexen 		}
1173f8829a4aSRandall Stewart 		return (-1);
1174f8829a4aSRandall Stewart 	}
1175b5c16493SMichael Tuexen 	memset(new_array1, 0, new_size);
1176b5c16493SMichael Tuexen 	memset(new_array2, 0, new_size);
1177b5c16493SMichael Tuexen 	memcpy(new_array1, asoc->mapping_array, asoc->mapping_array_size);
1178b5c16493SMichael Tuexen 	memcpy(new_array2, asoc->nr_mapping_array, asoc->mapping_array_size);
1179207304d4SRandall Stewart 	SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1180830d754dSRandall Stewart 	SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
1181b5c16493SMichael Tuexen 	asoc->mapping_array = new_array1;
1182b5c16493SMichael Tuexen 	asoc->nr_mapping_array = new_array2;
1183b5c16493SMichael Tuexen 	asoc->mapping_array_size = new_size;
1184830d754dSRandall Stewart 	return (0);
1185830d754dSRandall Stewart }
1186830d754dSRandall Stewart 
11878933fa13SRandall Stewart 
118842551e99SRandall Stewart static void
118942551e99SRandall Stewart sctp_iterator_work(struct sctp_iterator *it)
119042551e99SRandall Stewart {
119142551e99SRandall Stewart 	int iteration_count = 0;
119242551e99SRandall Stewart 	int inp_skip = 0;
1193ec4c19fcSRandall Stewart 	int first_in = 1;
1194ec4c19fcSRandall Stewart 	struct sctp_inpcb *tinp;
119542551e99SRandall Stewart 
1196ec4c19fcSRandall Stewart 	SCTP_INP_INFO_RLOCK();
119742551e99SRandall Stewart 	SCTP_ITERATOR_LOCK();
1198ad81507eSRandall Stewart 	if (it->inp) {
1199ec4c19fcSRandall Stewart 		SCTP_INP_RLOCK(it->inp);
120042551e99SRandall Stewart 		SCTP_INP_DECR_REF(it->inp);
1201ad81507eSRandall Stewart 	}
120242551e99SRandall Stewart 	if (it->inp == NULL) {
120342551e99SRandall Stewart 		/* iterator is complete */
120442551e99SRandall Stewart done_with_iterator:
120542551e99SRandall Stewart 		SCTP_ITERATOR_UNLOCK();
1206ec4c19fcSRandall Stewart 		SCTP_INP_INFO_RUNLOCK();
120742551e99SRandall Stewart 		if (it->function_atend != NULL) {
120842551e99SRandall Stewart 			(*it->function_atend) (it->pointer, it->val);
120942551e99SRandall Stewart 		}
1210207304d4SRandall Stewart 		SCTP_FREE(it, SCTP_M_ITER);
121142551e99SRandall Stewart 		return;
121242551e99SRandall Stewart 	}
121342551e99SRandall Stewart select_a_new_ep:
1214ec4c19fcSRandall Stewart 	if (first_in) {
1215ec4c19fcSRandall Stewart 		first_in = 0;
1216ec4c19fcSRandall Stewart 	} else {
1217f7517433SRandall Stewart 		SCTP_INP_RLOCK(it->inp);
1218ec4c19fcSRandall Stewart 	}
121942551e99SRandall Stewart 	while (((it->pcb_flags) &&
122042551e99SRandall Stewart 	    ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) ||
122142551e99SRandall Stewart 	    ((it->pcb_features) &&
122242551e99SRandall Stewart 	    ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) {
122342551e99SRandall Stewart 		/* endpoint flags or features don't match, so keep looking */
122442551e99SRandall Stewart 		if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1225f7517433SRandall Stewart 			SCTP_INP_RUNLOCK(it->inp);
122642551e99SRandall Stewart 			goto done_with_iterator;
122742551e99SRandall Stewart 		}
1228ec4c19fcSRandall Stewart 		tinp = it->inp;
122942551e99SRandall Stewart 		it->inp = LIST_NEXT(it->inp, sctp_list);
1230ec4c19fcSRandall Stewart 		SCTP_INP_RUNLOCK(tinp);
123142551e99SRandall Stewart 		if (it->inp == NULL) {
123242551e99SRandall Stewart 			goto done_with_iterator;
123342551e99SRandall Stewart 		}
123442551e99SRandall Stewart 		SCTP_INP_RLOCK(it->inp);
1235f7517433SRandall Stewart 	}
123642551e99SRandall Stewart 	/* now go through each assoc which is in the desired state */
123742551e99SRandall Stewart 	if (it->done_current_ep == 0) {
123842551e99SRandall Stewart 		if (it->function_inp != NULL)
123942551e99SRandall Stewart 			inp_skip = (*it->function_inp) (it->inp, it->pointer, it->val);
124042551e99SRandall Stewart 		it->done_current_ep = 1;
124142551e99SRandall Stewart 	}
124242551e99SRandall Stewart 	if (it->stcb == NULL) {
124342551e99SRandall Stewart 		/* run the per instance function */
124442551e99SRandall Stewart 		it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
124542551e99SRandall Stewart 	}
124642551e99SRandall Stewart 	if ((inp_skip) || it->stcb == NULL) {
124742551e99SRandall Stewart 		if (it->function_inp_end != NULL) {
124842551e99SRandall Stewart 			inp_skip = (*it->function_inp_end) (it->inp,
124942551e99SRandall Stewart 			    it->pointer,
125042551e99SRandall Stewart 			    it->val);
125142551e99SRandall Stewart 		}
125242551e99SRandall Stewart 		SCTP_INP_RUNLOCK(it->inp);
125342551e99SRandall Stewart 		goto no_stcb;
125442551e99SRandall Stewart 	}
125542551e99SRandall Stewart 	while (it->stcb) {
125642551e99SRandall Stewart 		SCTP_TCB_LOCK(it->stcb);
125742551e99SRandall Stewart 		if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
125842551e99SRandall Stewart 			/* not in the right state... keep looking */
125942551e99SRandall Stewart 			SCTP_TCB_UNLOCK(it->stcb);
126042551e99SRandall Stewart 			goto next_assoc;
126142551e99SRandall Stewart 		}
126242551e99SRandall Stewart 		/* see if we have limited out the iterator loop */
126342551e99SRandall Stewart 		iteration_count++;
126442551e99SRandall Stewart 		if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) {
126542551e99SRandall Stewart 			/* Pause to let others grab the lock */
126642551e99SRandall Stewart 			atomic_add_int(&it->stcb->asoc.refcnt, 1);
126742551e99SRandall Stewart 			SCTP_TCB_UNLOCK(it->stcb);
1268c4739e2fSRandall Stewart 			SCTP_INP_INCR_REF(it->inp);
126942551e99SRandall Stewart 			SCTP_INP_RUNLOCK(it->inp);
127042551e99SRandall Stewart 			SCTP_ITERATOR_UNLOCK();
1271ec4c19fcSRandall Stewart 			SCTP_INP_INFO_RUNLOCK();
1272ec4c19fcSRandall Stewart 			SCTP_INP_INFO_RLOCK();
127342551e99SRandall Stewart 			SCTP_ITERATOR_LOCK();
1274f7517433SRandall Stewart 			if (sctp_it_ctl.iterator_flags) {
1275f7517433SRandall Stewart 				/* We won't be staying here */
1276f7517433SRandall Stewart 				SCTP_INP_DECR_REF(it->inp);
1277f7517433SRandall Stewart 				atomic_add_int(&it->stcb->asoc.refcnt, -1);
1278f7517433SRandall Stewart 				if (sctp_it_ctl.iterator_flags &
1279f7517433SRandall Stewart 				    SCTP_ITERATOR_STOP_CUR_IT) {
1280f7517433SRandall Stewart 					sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_IT;
1281f7517433SRandall Stewart 					goto done_with_iterator;
1282f7517433SRandall Stewart 				}
1283f7517433SRandall Stewart 				if (sctp_it_ctl.iterator_flags &
1284f7517433SRandall Stewart 				    SCTP_ITERATOR_STOP_CUR_INP) {
1285f7517433SRandall Stewart 					sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_INP;
1286f7517433SRandall Stewart 					goto no_stcb;
1287f7517433SRandall Stewart 				}
1288f7517433SRandall Stewart 				/* If we reach here huh? */
1289cd3fd531SMichael Tuexen 				SCTP_PRINTF("Unknown it ctl flag %x\n",
1290f7517433SRandall Stewart 				    sctp_it_ctl.iterator_flags);
1291f7517433SRandall Stewart 				sctp_it_ctl.iterator_flags = 0;
1292f7517433SRandall Stewart 			}
129342551e99SRandall Stewart 			SCTP_INP_RLOCK(it->inp);
1294c4739e2fSRandall Stewart 			SCTP_INP_DECR_REF(it->inp);
129542551e99SRandall Stewart 			SCTP_TCB_LOCK(it->stcb);
129642551e99SRandall Stewart 			atomic_add_int(&it->stcb->asoc.refcnt, -1);
129742551e99SRandall Stewart 			iteration_count = 0;
129842551e99SRandall Stewart 		}
129942551e99SRandall Stewart 		/* run function on this one */
130042551e99SRandall Stewart 		(*it->function_assoc) (it->inp, it->stcb, it->pointer, it->val);
130142551e99SRandall Stewart 
130242551e99SRandall Stewart 		/*
130342551e99SRandall Stewart 		 * we lie here, it really needs to have its own type but
130442551e99SRandall Stewart 		 * first I must verify that this won't effect things :-0
130542551e99SRandall Stewart 		 */
130642551e99SRandall Stewart 		if (it->no_chunk_output == 0)
1307ceaad40aSRandall Stewart 			sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
130842551e99SRandall Stewart 
130942551e99SRandall Stewart 		SCTP_TCB_UNLOCK(it->stcb);
131042551e99SRandall Stewart next_assoc:
131142551e99SRandall Stewart 		it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
131242551e99SRandall Stewart 		if (it->stcb == NULL) {
131342551e99SRandall Stewart 			/* Run last function */
131442551e99SRandall Stewart 			if (it->function_inp_end != NULL) {
131542551e99SRandall Stewart 				inp_skip = (*it->function_inp_end) (it->inp,
131642551e99SRandall Stewart 				    it->pointer,
131742551e99SRandall Stewart 				    it->val);
131842551e99SRandall Stewart 			}
131942551e99SRandall Stewart 		}
132042551e99SRandall Stewart 	}
132142551e99SRandall Stewart 	SCTP_INP_RUNLOCK(it->inp);
132242551e99SRandall Stewart no_stcb:
132342551e99SRandall Stewart 	/* done with all assocs on this endpoint, move on to next endpoint */
132442551e99SRandall Stewart 	it->done_current_ep = 0;
132542551e99SRandall Stewart 	if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
132642551e99SRandall Stewart 		it->inp = NULL;
132742551e99SRandall Stewart 	} else {
132842551e99SRandall Stewart 		it->inp = LIST_NEXT(it->inp, sctp_list);
132942551e99SRandall Stewart 	}
133042551e99SRandall Stewart 	if (it->inp == NULL) {
133142551e99SRandall Stewart 		goto done_with_iterator;
133242551e99SRandall Stewart 	}
133342551e99SRandall Stewart 	goto select_a_new_ep;
133442551e99SRandall Stewart }
133542551e99SRandall Stewart 
133642551e99SRandall Stewart void
133742551e99SRandall Stewart sctp_iterator_worker(void)
133842551e99SRandall Stewart {
13394a9ef3f8SMichael Tuexen 	struct sctp_iterator *it, *nit;
134042551e99SRandall Stewart 
134142551e99SRandall Stewart 	/* This function is called with the WQ lock in place */
134242551e99SRandall Stewart 
1343f7517433SRandall Stewart 	sctp_it_ctl.iterator_running = 1;
13444a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
13454a9ef3f8SMichael Tuexen 		sctp_it_ctl.cur_it = it;
134642551e99SRandall Stewart 		/* now lets work on this one */
1347f7517433SRandall Stewart 		TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
134842551e99SRandall Stewart 		SCTP_IPI_ITERATOR_WQ_UNLOCK();
1349f7517433SRandall Stewart 		CURVNET_SET(it->vn);
135042551e99SRandall Stewart 		sctp_iterator_work(it);
1351f79aab18SRandall Stewart 		sctp_it_ctl.cur_it = NULL;
1352f7517433SRandall Stewart 		CURVNET_RESTORE();
135342551e99SRandall Stewart 		SCTP_IPI_ITERATOR_WQ_LOCK();
13543c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
135542551e99SRandall Stewart 	}
1356f7517433SRandall Stewart 	sctp_it_ctl.iterator_running = 0;
135742551e99SRandall Stewart 	return;
135842551e99SRandall Stewart }
135942551e99SRandall Stewart 
1360f8829a4aSRandall Stewart 
1361f8829a4aSRandall Stewart static void
1362f8829a4aSRandall Stewart sctp_handle_addr_wq(void)
1363f8829a4aSRandall Stewart {
1364f8829a4aSRandall Stewart 	/* deal with the ADDR wq from the rtsock calls */
13654a9ef3f8SMichael Tuexen 	struct sctp_laddr *wi, *nwi;
136642551e99SRandall Stewart 	struct sctp_asconf_iterator *asc;
1367f8829a4aSRandall Stewart 
136842551e99SRandall Stewart 	SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
1369207304d4SRandall Stewart 	    sizeof(struct sctp_asconf_iterator), SCTP_M_ASC_IT);
137042551e99SRandall Stewart 	if (asc == NULL) {
137142551e99SRandall Stewart 		/* Try later, no memory */
1372f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
1373f8829a4aSRandall Stewart 		    (struct sctp_inpcb *)NULL,
1374f8829a4aSRandall Stewart 		    (struct sctp_tcb *)NULL,
1375f8829a4aSRandall Stewart 		    (struct sctp_nets *)NULL);
137642551e99SRandall Stewart 		return;
1377f8829a4aSRandall Stewart 	}
137842551e99SRandall Stewart 	LIST_INIT(&asc->list_of_work);
137942551e99SRandall Stewart 	asc->cnt = 0;
1380f7517433SRandall Stewart 
1381f7517433SRandall Stewart 	SCTP_WQ_ADDR_LOCK();
13824a9ef3f8SMichael Tuexen 	LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
138342551e99SRandall Stewart 		LIST_REMOVE(wi, sctp_nxt_addr);
138442551e99SRandall Stewart 		LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
138542551e99SRandall Stewart 		asc->cnt++;
1386f8829a4aSRandall Stewart 	}
1387f7517433SRandall Stewart 	SCTP_WQ_ADDR_UNLOCK();
1388f7517433SRandall Stewart 
138942551e99SRandall Stewart 	if (asc->cnt == 0) {
1390207304d4SRandall Stewart 		SCTP_FREE(asc, SCTP_M_ASC_IT);
139142551e99SRandall Stewart 	} else {
13921b649582SRandall Stewart 		(void)sctp_initiate_iterator(sctp_asconf_iterator_ep,
13931b649582SRandall Stewart 		    sctp_asconf_iterator_stcb,
139442551e99SRandall Stewart 		    NULL,	/* No ep end for boundall */
139542551e99SRandall Stewart 		    SCTP_PCB_FLAGS_BOUNDALL,
139642551e99SRandall Stewart 		    SCTP_PCB_ANY_FEATURES,
13971b649582SRandall Stewart 		    SCTP_ASOC_ANY_STATE,
13981b649582SRandall Stewart 		    (void *)asc, 0,
13991b649582SRandall Stewart 		    sctp_asconf_iterator_end, NULL, 0);
140042551e99SRandall Stewart 	}
1401f8829a4aSRandall Stewart }
1402f8829a4aSRandall Stewart 
1403f8829a4aSRandall Stewart void
1404f8829a4aSRandall Stewart sctp_timeout_handler(void *t)
1405f8829a4aSRandall Stewart {
1406f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
1407f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
1408f8829a4aSRandall Stewart 	struct sctp_nets *net;
1409f8829a4aSRandall Stewart 	struct sctp_timer *tmr;
1410ceaad40aSRandall Stewart 
1411ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1412ceaad40aSRandall Stewart 	struct socket *so;
1413ceaad40aSRandall Stewart 
1414ceaad40aSRandall Stewart #endif
1415d61374e1SRandall Stewart 	int did_output, type;
1416f8829a4aSRandall Stewart 
1417f8829a4aSRandall Stewart 	tmr = (struct sctp_timer *)t;
1418f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)tmr->ep;
1419f8829a4aSRandall Stewart 	stcb = (struct sctp_tcb *)tmr->tcb;
1420f8829a4aSRandall Stewart 	net = (struct sctp_nets *)tmr->net;
14218518270eSMichael Tuexen 	CURVNET_SET((struct vnet *)tmr->vnet);
1422f8829a4aSRandall Stewart 	did_output = 1;
1423f8829a4aSRandall Stewart 
1424f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1425f8829a4aSRandall Stewart 	sctp_audit_log(0xF0, (uint8_t) tmr->type);
1426f8829a4aSRandall Stewart 	sctp_auditing(3, inp, stcb, net);
1427f8829a4aSRandall Stewart #endif
1428f8829a4aSRandall Stewart 
1429f8829a4aSRandall Stewart 	/* sanity checks... */
1430f8829a4aSRandall Stewart 	if (tmr->self != (void *)tmr) {
1431f8829a4aSRandall Stewart 		/*
1432ad81507eSRandall Stewart 		 * SCTP_PRINTF("Stale SCTP timer fired (%p), ignoring...\n",
1433dd294dceSMichael Tuexen 		 * (void *)tmr);
1434f8829a4aSRandall Stewart 		 */
14358518270eSMichael Tuexen 		CURVNET_RESTORE();
1436f8829a4aSRandall Stewart 		return;
1437f8829a4aSRandall Stewart 	}
1438a5d547adSRandall Stewart 	tmr->stopped_from = 0xa001;
1439f8829a4aSRandall Stewart 	if (!SCTP_IS_TIMER_TYPE_VALID(tmr->type)) {
1440f8829a4aSRandall Stewart 		/*
1441ad81507eSRandall Stewart 		 * SCTP_PRINTF("SCTP timer fired with invalid type: 0x%x\n",
1442f8829a4aSRandall Stewart 		 * tmr->type);
1443f8829a4aSRandall Stewart 		 */
14448518270eSMichael Tuexen 		CURVNET_RESTORE();
1445f8829a4aSRandall Stewart 		return;
1446f8829a4aSRandall Stewart 	}
1447a5d547adSRandall Stewart 	tmr->stopped_from = 0xa002;
1448f8829a4aSRandall Stewart 	if ((tmr->type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL)) {
14498518270eSMichael Tuexen 		CURVNET_RESTORE();
1450f8829a4aSRandall Stewart 		return;
1451f8829a4aSRandall Stewart 	}
1452f8829a4aSRandall Stewart 	/* if this is an iterator timeout, get the struct and clear inp */
1453a5d547adSRandall Stewart 	tmr->stopped_from = 0xa003;
1454d61374e1SRandall Stewart 	type = tmr->type;
1455f8829a4aSRandall Stewart 	if (inp) {
1456f8829a4aSRandall Stewart 		SCTP_INP_INCR_REF(inp);
1457aa1808b7SMichael Tuexen 		if ((inp->sctp_socket == NULL) &&
1458f8829a4aSRandall Stewart 		    ((tmr->type != SCTP_TIMER_TYPE_INPKILL) &&
1459810ec536SMichael Tuexen 		    (tmr->type != SCTP_TIMER_TYPE_INIT) &&
1460a1e13272SRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_SEND) &&
1461a1e13272SRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_RECV) &&
1462a1e13272SRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_HEARTBEAT) &&
1463f8829a4aSRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_SHUTDOWN) &&
1464f8829a4aSRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNACK) &&
1465f8829a4aSRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNGUARD) &&
1466f8829a4aSRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_ASOCKILL))
1467f8829a4aSRandall Stewart 		    ) {
1468f8829a4aSRandall Stewart 			SCTP_INP_DECR_REF(inp);
14698518270eSMichael Tuexen 			CURVNET_RESTORE();
1470f8829a4aSRandall Stewart 			return;
1471f8829a4aSRandall Stewart 		}
1472f8829a4aSRandall Stewart 	}
1473a5d547adSRandall Stewart 	tmr->stopped_from = 0xa004;
1474f8829a4aSRandall Stewart 	if (stcb) {
1475c105859eSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
1476f8829a4aSRandall Stewart 		if (stcb->asoc.state == 0) {
1477c105859eSRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, -1);
1478f8829a4aSRandall Stewart 			if (inp) {
1479f8829a4aSRandall Stewart 				SCTP_INP_DECR_REF(inp);
1480f8829a4aSRandall Stewart 			}
14818518270eSMichael Tuexen 			CURVNET_RESTORE();
1482f8829a4aSRandall Stewart 			return;
1483f8829a4aSRandall Stewart 		}
1484f8829a4aSRandall Stewart 	}
1485a5d547adSRandall Stewart 	tmr->stopped_from = 0xa005;
1486ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_TIMER1, "Timer type %d goes off\n", tmr->type);
1487139bc87fSRandall Stewart 	if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1488f8829a4aSRandall Stewart 		if (inp) {
1489f8829a4aSRandall Stewart 			SCTP_INP_DECR_REF(inp);
1490f8829a4aSRandall Stewart 		}
1491207304d4SRandall Stewart 		if (stcb) {
1492207304d4SRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, -1);
1493207304d4SRandall Stewart 		}
14948518270eSMichael Tuexen 		CURVNET_RESTORE();
1495f8829a4aSRandall Stewart 		return;
1496f8829a4aSRandall Stewart 	}
1497a5d547adSRandall Stewart 	tmr->stopped_from = 0xa006;
1498a5d547adSRandall Stewart 
1499f8829a4aSRandall Stewart 	if (stcb) {
1500f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
150150cec919SRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, -1);
1502b54d3a6cSRandall Stewart 		if ((tmr->type != SCTP_TIMER_TYPE_ASOCKILL) &&
1503b54d3a6cSRandall Stewart 		    ((stcb->asoc.state == 0) ||
1504b54d3a6cSRandall Stewart 		    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED))) {
1505b54d3a6cSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
1506b54d3a6cSRandall Stewart 			if (inp) {
1507b54d3a6cSRandall Stewart 				SCTP_INP_DECR_REF(inp);
1508b54d3a6cSRandall Stewart 			}
15098518270eSMichael Tuexen 			CURVNET_RESTORE();
1510b54d3a6cSRandall Stewart 			return;
1511b54d3a6cSRandall Stewart 		}
1512f8829a4aSRandall Stewart 	}
151344b7479bSRandall Stewart 	/* record in stopped what t-o occured */
151444b7479bSRandall Stewart 	tmr->stopped_from = tmr->type;
151544b7479bSRandall Stewart 
1516f8829a4aSRandall Stewart 	/* mark as being serviced now */
151744b7479bSRandall Stewart 	if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
151844b7479bSRandall Stewart 		/*
151944b7479bSRandall Stewart 		 * Callout has been rescheduled.
152044b7479bSRandall Stewart 		 */
152144b7479bSRandall Stewart 		goto get_out;
152244b7479bSRandall Stewart 	}
152344b7479bSRandall Stewart 	if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
152444b7479bSRandall Stewart 		/*
152544b7479bSRandall Stewart 		 * Not active, so no action.
152644b7479bSRandall Stewart 		 */
152744b7479bSRandall Stewart 		goto get_out;
152844b7479bSRandall Stewart 	}
1529139bc87fSRandall Stewart 	SCTP_OS_TIMER_DEACTIVATE(&tmr->timer);
1530f8829a4aSRandall Stewart 
1531f8829a4aSRandall Stewart 	/* call the handler for the appropriate timer type */
1532f8829a4aSRandall Stewart 	switch (tmr->type) {
1533d61a0ae0SRandall Stewart 	case SCTP_TIMER_TYPE_ZERO_COPY:
1534eacc51c5SRandall Stewart 		if (inp == NULL) {
1535eacc51c5SRandall Stewart 			break;
1536eacc51c5SRandall Stewart 		}
1537d61a0ae0SRandall Stewart 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
1538d61a0ae0SRandall Stewart 			SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
1539d61a0ae0SRandall Stewart 		}
1540d61a0ae0SRandall Stewart 		break;
1541ad21a364SRandall Stewart 	case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
1542eacc51c5SRandall Stewart 		if (inp == NULL) {
1543eacc51c5SRandall Stewart 			break;
1544eacc51c5SRandall Stewart 		}
1545ad21a364SRandall Stewart 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
1546ad21a364SRandall Stewart 			SCTP_ZERO_COPY_SENDQ_EVENT(inp, inp->sctp_socket);
1547ad21a364SRandall Stewart 		}
1548ad21a364SRandall Stewart 		break;
1549f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ADDR_WQ:
1550f8829a4aSRandall Stewart 		sctp_handle_addr_wq();
1551f8829a4aSRandall Stewart 		break;
1552f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SEND:
1553ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1554ad81507eSRandall Stewart 			break;
1555ad81507eSRandall Stewart 		}
1556f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timodata);
1557f42a358aSRandall Stewart 		stcb->asoc.timodata++;
1558f8829a4aSRandall Stewart 		stcb->asoc.num_send_timers_up--;
1559f8829a4aSRandall Stewart 		if (stcb->asoc.num_send_timers_up < 0) {
1560f8829a4aSRandall Stewart 			stcb->asoc.num_send_timers_up = 0;
1561f8829a4aSRandall Stewart 		}
1562b54d3a6cSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
156360990c0cSMichael Tuexen 		if (sctp_t3rxt_timer(inp, stcb, net)) {
1564f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1565f8829a4aSRandall Stewart 
1566f8829a4aSRandall Stewart 			goto out_decr;
1567f8829a4aSRandall Stewart 		}
1568b54d3a6cSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1569f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1570f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1571f8829a4aSRandall Stewart #endif
1572ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1573f8829a4aSRandall Stewart 		if ((stcb->asoc.num_send_timers_up == 0) &&
15744a9ef3f8SMichael Tuexen 		    (stcb->asoc.sent_queue_cnt > 0)) {
1575f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *chk;
1576f8829a4aSRandall Stewart 
1577f8829a4aSRandall Stewart 			/*
1578f8829a4aSRandall Stewart 			 * safeguard. If there on some on the sent queue
1579f8829a4aSRandall Stewart 			 * somewhere but no timers running something is
1580f8829a4aSRandall Stewart 			 * wrong... so we start a timer on the first chunk
1581f8829a4aSRandall Stewart 			 * on the send queue on whatever net it is sent to.
1582f8829a4aSRandall Stewart 			 */
1583f8829a4aSRandall Stewart 			chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
1584f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb,
1585f8829a4aSRandall Stewart 			    chk->whoTo);
1586f8829a4aSRandall Stewart 		}
1587f8829a4aSRandall Stewart 		break;
1588f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
1589ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1590ad81507eSRandall Stewart 			break;
1591ad81507eSRandall Stewart 		}
1592f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoinit);
1593f42a358aSRandall Stewart 		stcb->asoc.timoinit++;
1594f8829a4aSRandall Stewart 		if (sctp_t1init_timer(inp, stcb, net)) {
1595f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1596f8829a4aSRandall Stewart 			goto out_decr;
1597f8829a4aSRandall Stewart 		}
1598f8829a4aSRandall Stewart 		/* We do output but not here */
1599f8829a4aSRandall Stewart 		did_output = 0;
1600f8829a4aSRandall Stewart 		break;
1601f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
1602ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1603ad81507eSRandall Stewart 			break;
1604ca85e948SMichael Tuexen 		}
1605f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timosack);
1606f42a358aSRandall Stewart 		stcb->asoc.timosack++;
1607689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
1608f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1609f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1610f8829a4aSRandall Stewart #endif
1611ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SACK_TMR, SCTP_SO_NOT_LOCKED);
1612f8829a4aSRandall Stewart 		break;
1613f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
1614ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1615ad81507eSRandall Stewart 			break;
1616ad81507eSRandall Stewart 		}
1617f8829a4aSRandall Stewart 		if (sctp_shutdown_timer(inp, stcb, net)) {
1618f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1619f8829a4aSRandall Stewart 			goto out_decr;
1620f8829a4aSRandall Stewart 		}
1621f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoshutdown);
1622f42a358aSRandall Stewart 		stcb->asoc.timoshutdown++;
1623f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1624f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1625f8829a4aSRandall Stewart #endif
1626ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_NOT_LOCKED);
1627f8829a4aSRandall Stewart 		break;
1628f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
1629ca85e948SMichael Tuexen 		if ((stcb == NULL) || (inp == NULL) || (net == NULL)) {
1630ad81507eSRandall Stewart 			break;
1631ad81507eSRandall Stewart 		}
1632f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoheartbeat);
1633f42a358aSRandall Stewart 		stcb->asoc.timoheartbeat++;
1634ca85e948SMichael Tuexen 		if (sctp_heartbeat_timer(inp, stcb, net)) {
1635f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1636f8829a4aSRandall Stewart 			goto out_decr;
1637f8829a4aSRandall Stewart 		}
1638f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1639ca85e948SMichael Tuexen 		sctp_auditing(4, inp, stcb, net);
1640f8829a4aSRandall Stewart #endif
1641ca85e948SMichael Tuexen 		if (!(net->dest_state & SCTP_ADDR_NOHB)) {
1642629749b6SMichael Tuexen 			sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1643ceaad40aSRandall Stewart 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_HB_TMR, SCTP_SO_NOT_LOCKED);
1644f8829a4aSRandall Stewart 		}
1645f8829a4aSRandall Stewart 		break;
1646f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
1647ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1648ad81507eSRandall Stewart 			break;
1649ad81507eSRandall Stewart 		}
1650f8829a4aSRandall Stewart 		if (sctp_cookie_timer(inp, stcb, net)) {
1651f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1652f8829a4aSRandall Stewart 			goto out_decr;
1653f8829a4aSRandall Stewart 		}
1654f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timocookie);
1655f42a358aSRandall Stewart 		stcb->asoc.timocookie++;
1656f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1657f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1658f8829a4aSRandall Stewart #endif
1659f8829a4aSRandall Stewart 		/*
1660f8829a4aSRandall Stewart 		 * We consider T3 and Cookie timer pretty much the same with
1661f8829a4aSRandall Stewart 		 * respect to where from in chunk_output.
1662f8829a4aSRandall Stewart 		 */
1663ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1664f8829a4aSRandall Stewart 		break;
1665f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_NEWCOOKIE:
1666f8829a4aSRandall Stewart 		{
1667f8829a4aSRandall Stewart 			struct timeval tv;
1668f8829a4aSRandall Stewart 			int i, secret;
1669f8829a4aSRandall Stewart 
1670ad81507eSRandall Stewart 			if (inp == NULL) {
1671ad81507eSRandall Stewart 				break;
1672ad81507eSRandall Stewart 			}
1673f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_timosecret);
16746e55db54SRandall Stewart 			(void)SCTP_GETTIME_TIMEVAL(&tv);
1675f8829a4aSRandall Stewart 			SCTP_INP_WLOCK(inp);
1676f8829a4aSRandall Stewart 			inp->sctp_ep.time_of_secret_change = tv.tv_sec;
1677f8829a4aSRandall Stewart 			inp->sctp_ep.last_secret_number =
1678f8829a4aSRandall Stewart 			    inp->sctp_ep.current_secret_number;
1679f8829a4aSRandall Stewart 			inp->sctp_ep.current_secret_number++;
1680f8829a4aSRandall Stewart 			if (inp->sctp_ep.current_secret_number >=
1681f8829a4aSRandall Stewart 			    SCTP_HOW_MANY_SECRETS) {
1682f8829a4aSRandall Stewart 				inp->sctp_ep.current_secret_number = 0;
1683f8829a4aSRandall Stewart 			}
1684f8829a4aSRandall Stewart 			secret = (int)inp->sctp_ep.current_secret_number;
1685f8829a4aSRandall Stewart 			for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1686f8829a4aSRandall Stewart 				inp->sctp_ep.secret_key[secret][i] =
1687f8829a4aSRandall Stewart 				    sctp_select_initial_TSN(&inp->sctp_ep);
1688f8829a4aSRandall Stewart 			}
1689f8829a4aSRandall Stewart 			SCTP_INP_WUNLOCK(inp);
1690f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, stcb, net);
1691f8829a4aSRandall Stewart 		}
1692f8829a4aSRandall Stewart 		did_output = 0;
1693f8829a4aSRandall Stewart 		break;
1694f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
1695ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1696ad81507eSRandall Stewart 			break;
1697ad81507eSRandall Stewart 		}
1698f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timopathmtu);
1699f8829a4aSRandall Stewart 		sctp_pathmtu_timer(inp, stcb, net);
1700f8829a4aSRandall Stewart 		did_output = 0;
1701f8829a4aSRandall Stewart 		break;
1702f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
1703ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1704ad81507eSRandall Stewart 			break;
1705ad81507eSRandall Stewart 		}
1706f8829a4aSRandall Stewart 		if (sctp_shutdownack_timer(inp, stcb, net)) {
1707f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1708f8829a4aSRandall Stewart 			goto out_decr;
1709f8829a4aSRandall Stewart 		}
1710f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoshutdownack);
1711f42a358aSRandall Stewart 		stcb->asoc.timoshutdownack++;
1712f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1713f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1714f8829a4aSRandall Stewart #endif
1715ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_ACK_TMR, SCTP_SO_NOT_LOCKED);
1716f8829a4aSRandall Stewart 		break;
1717f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
1718ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1719ad81507eSRandall Stewart 			break;
1720ad81507eSRandall Stewart 		}
1721f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoshutdownguard);
1722a2b42326SMichael Tuexen 		sctp_abort_an_association(inp, stcb, NULL, SCTP_SO_NOT_LOCKED);
1723f8829a4aSRandall Stewart 		/* no need to unlock on tcb its gone */
1724f8829a4aSRandall Stewart 		goto out_decr;
1725f8829a4aSRandall Stewart 
1726f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_STRRESET:
1727ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1728ad81507eSRandall Stewart 			break;
1729ad81507eSRandall Stewart 		}
1730f8829a4aSRandall Stewart 		if (sctp_strreset_timer(inp, stcb, net)) {
1731f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1732f8829a4aSRandall Stewart 			goto out_decr;
1733f8829a4aSRandall Stewart 		}
1734f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timostrmrst);
1735ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_TMR, SCTP_SO_NOT_LOCKED);
1736f8829a4aSRandall Stewart 		break;
1737f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASCONF:
1738ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1739ad81507eSRandall Stewart 			break;
1740ad81507eSRandall Stewart 		}
1741f8829a4aSRandall Stewart 		if (sctp_asconf_timer(inp, stcb, net)) {
1742f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1743f8829a4aSRandall Stewart 			goto out_decr;
1744f8829a4aSRandall Stewart 		}
1745f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoasconf);
1746f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1747f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1748f8829a4aSRandall Stewart #endif
1749ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_ASCONF_TMR, SCTP_SO_NOT_LOCKED);
1750f8829a4aSRandall Stewart 		break;
1751851b7298SRandall Stewart 	case SCTP_TIMER_TYPE_PRIM_DELETED:
1752851b7298SRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1753851b7298SRandall Stewart 			break;
1754851b7298SRandall Stewart 		}
175504ee05e8SRandall Stewart 		sctp_delete_prim_timer(inp, stcb, net);
1756851b7298SRandall Stewart 		SCTP_STAT_INCR(sctps_timodelprim);
1757851b7298SRandall Stewart 		break;
1758f8829a4aSRandall Stewart 
1759f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_AUTOCLOSE:
1760ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1761ad81507eSRandall Stewart 			break;
1762ad81507eSRandall Stewart 		}
1763f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoautoclose);
1764f8829a4aSRandall Stewart 		sctp_autoclose_timer(inp, stcb, net);
1765ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
1766f8829a4aSRandall Stewart 		did_output = 0;
1767f8829a4aSRandall Stewart 		break;
1768f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASOCKILL:
1769ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1770ad81507eSRandall Stewart 			break;
1771ad81507eSRandall Stewart 		}
1772f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoassockill);
1773f8829a4aSRandall Stewart 		/* Can we free it yet? */
1774f8829a4aSRandall Stewart 		SCTP_INP_DECR_REF(inp);
1775a5d547adSRandall Stewart 		sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_1);
1776ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1777ceaad40aSRandall Stewart 		so = SCTP_INP_SO(inp);
1778ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
1779ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
1780ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
1781ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
1782ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
1783ceaad40aSRandall Stewart #endif
1784c4739e2fSRandall Stewart 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_2);
1785ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1786ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
1787ceaad40aSRandall Stewart #endif
1788f8829a4aSRandall Stewart 		/*
1789f8829a4aSRandall Stewart 		 * free asoc, always unlocks (or destroy's) so prevent
1790f8829a4aSRandall Stewart 		 * duplicate unlock or unlock of a free mtx :-0
1791f8829a4aSRandall Stewart 		 */
1792f8829a4aSRandall Stewart 		stcb = NULL;
1793f8829a4aSRandall Stewart 		goto out_no_decr;
1794f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INPKILL:
1795f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoinpkill);
1796ad81507eSRandall Stewart 		if (inp == NULL) {
1797ad81507eSRandall Stewart 			break;
1798ad81507eSRandall Stewart 		}
1799f8829a4aSRandall Stewart 		/*
1800f8829a4aSRandall Stewart 		 * special case, take away our increment since WE are the
1801f8829a4aSRandall Stewart 		 * killer
1802f8829a4aSRandall Stewart 		 */
1803f8829a4aSRandall Stewart 		SCTP_INP_DECR_REF(inp);
1804a5d547adSRandall Stewart 		sctp_timer_stop(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_3);
1805b0552ae2SRandall Stewart 		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
18060c7dc840SRandall Stewart 		    SCTP_CALLED_FROM_INPKILL_TIMER);
1807d61374e1SRandall Stewart 		inp = NULL;
1808f8829a4aSRandall Stewart 		goto out_no_decr;
1809f8829a4aSRandall Stewart 	default:
1810ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER1, "sctp_timeout_handler:unknown timer %d\n",
1811f8829a4aSRandall Stewart 		    tmr->type);
1812f8829a4aSRandall Stewart 		break;
181360990c0cSMichael Tuexen 	}
1814f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1815f8829a4aSRandall Stewart 	sctp_audit_log(0xF1, (uint8_t) tmr->type);
1816f8829a4aSRandall Stewart 	if (inp)
1817f8829a4aSRandall Stewart 		sctp_auditing(5, inp, stcb, net);
1818f8829a4aSRandall Stewart #endif
1819f8829a4aSRandall Stewart 	if ((did_output) && stcb) {
1820f8829a4aSRandall Stewart 		/*
1821f8829a4aSRandall Stewart 		 * Now we need to clean up the control chunk chain if an
1822f8829a4aSRandall Stewart 		 * ECNE is on it. It must be marked as UNSENT again so next
1823f8829a4aSRandall Stewart 		 * call will continue to send it until such time that we get
1824f8829a4aSRandall Stewart 		 * a CWR, to remove it. It is, however, less likely that we
1825f8829a4aSRandall Stewart 		 * will find a ecn echo on the chain though.
1826f8829a4aSRandall Stewart 		 */
1827f8829a4aSRandall Stewart 		sctp_fix_ecn_echo(&stcb->asoc);
1828f8829a4aSRandall Stewart 	}
182944b7479bSRandall Stewart get_out:
1830f8829a4aSRandall Stewart 	if (stcb) {
1831f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
1832f8829a4aSRandall Stewart 	}
1833f8829a4aSRandall Stewart out_decr:
1834f8829a4aSRandall Stewart 	if (inp) {
1835f8829a4aSRandall Stewart 		SCTP_INP_DECR_REF(inp);
1836f8829a4aSRandall Stewart 	}
1837f8829a4aSRandall Stewart out_no_decr:
1838ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_TIMER1, "Timer now complete (type %d)\n",
1839d61374e1SRandall Stewart 	    type);
18408518270eSMichael Tuexen 	CURVNET_RESTORE();
1841f8829a4aSRandall Stewart }
1842f8829a4aSRandall Stewart 
1843ad81507eSRandall Stewart void
1844f8829a4aSRandall Stewart sctp_timer_start(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1845f8829a4aSRandall Stewart     struct sctp_nets *net)
1846f8829a4aSRandall Stewart {
1847ca85e948SMichael Tuexen 	uint32_t to_ticks;
1848f8829a4aSRandall Stewart 	struct sctp_timer *tmr;
1849f8829a4aSRandall Stewart 
1850139bc87fSRandall Stewart 	if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL))
1851ad81507eSRandall Stewart 		return;
1852f8829a4aSRandall Stewart 
1853f8829a4aSRandall Stewart 	tmr = NULL;
1854f8829a4aSRandall Stewart 	if (stcb) {
1855f8829a4aSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1856f8829a4aSRandall Stewart 	}
1857f8829a4aSRandall Stewart 	switch (t_type) {
1858d61a0ae0SRandall Stewart 	case SCTP_TIMER_TYPE_ZERO_COPY:
1859d61a0ae0SRandall Stewart 		tmr = &inp->sctp_ep.zero_copy_timer;
1860d61a0ae0SRandall Stewart 		to_ticks = SCTP_ZERO_COPY_TICK_DELAY;
1861d61a0ae0SRandall Stewart 		break;
1862ad21a364SRandall Stewart 	case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
1863ad21a364SRandall Stewart 		tmr = &inp->sctp_ep.zero_copy_sendq_timer;
1864ad21a364SRandall Stewart 		to_ticks = SCTP_ZERO_COPY_SENDQ_TICK_DELAY;
1865ad21a364SRandall Stewart 		break;
1866f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ADDR_WQ:
1867f8829a4aSRandall Stewart 		/* Only 1 tick away :-) */
1868b3f1ea41SRandall Stewart 		tmr = &SCTP_BASE_INFO(addr_wq_timer);
186942551e99SRandall Stewart 		to_ticks = SCTP_ADDRESS_TICK_DELAY;
1870f8829a4aSRandall Stewart 		break;
1871f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SEND:
1872f8829a4aSRandall Stewart 		/* Here we use the RTO timer */
1873f8829a4aSRandall Stewart 		{
1874f8829a4aSRandall Stewart 			int rto_val;
1875f8829a4aSRandall Stewart 
1876f8829a4aSRandall Stewart 			if ((stcb == NULL) || (net == NULL)) {
1877ad81507eSRandall Stewart 				return;
1878f8829a4aSRandall Stewart 			}
1879f8829a4aSRandall Stewart 			tmr = &net->rxt_timer;
1880f8829a4aSRandall Stewart 			if (net->RTO == 0) {
1881f8829a4aSRandall Stewart 				rto_val = stcb->asoc.initial_rto;
1882f8829a4aSRandall Stewart 			} else {
1883f8829a4aSRandall Stewart 				rto_val = net->RTO;
1884f8829a4aSRandall Stewart 			}
1885f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(rto_val);
1886f8829a4aSRandall Stewart 		}
1887f8829a4aSRandall Stewart 		break;
1888f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
1889f8829a4aSRandall Stewart 		/*
1890f8829a4aSRandall Stewart 		 * Here we use the INIT timer default usually about 1
1891f8829a4aSRandall Stewart 		 * minute.
1892f8829a4aSRandall Stewart 		 */
1893f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
1894ad81507eSRandall Stewart 			return;
1895f8829a4aSRandall Stewart 		}
1896f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
1897f8829a4aSRandall Stewart 		if (net->RTO == 0) {
1898f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1899f8829a4aSRandall Stewart 		} else {
1900f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
1901f8829a4aSRandall Stewart 		}
1902f8829a4aSRandall Stewart 		break;
1903f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
1904f8829a4aSRandall Stewart 		/*
1905f8829a4aSRandall Stewart 		 * Here we use the Delayed-Ack timer value from the inp
1906f8829a4aSRandall Stewart 		 * ususually about 200ms.
1907f8829a4aSRandall Stewart 		 */
1908f8829a4aSRandall Stewart 		if (stcb == NULL) {
1909ad81507eSRandall Stewart 			return;
1910f8829a4aSRandall Stewart 		}
1911f8829a4aSRandall Stewart 		tmr = &stcb->asoc.dack_timer;
1912f8829a4aSRandall Stewart 		to_ticks = MSEC_TO_TICKS(stcb->asoc.delayed_ack);
1913f8829a4aSRandall Stewart 		break;
1914f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
1915f8829a4aSRandall Stewart 		/* Here we use the RTO of the destination. */
1916f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
1917ad81507eSRandall Stewart 			return;
1918f8829a4aSRandall Stewart 		}
1919f8829a4aSRandall Stewart 		if (net->RTO == 0) {
1920f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1921f8829a4aSRandall Stewart 		} else {
1922f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
1923f8829a4aSRandall Stewart 		}
1924f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
1925f8829a4aSRandall Stewart 		break;
1926f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
1927f8829a4aSRandall Stewart 		/*
1928f8829a4aSRandall Stewart 		 * the net is used here so that we can add in the RTO. Even
1929f8829a4aSRandall Stewart 		 * though we use a different timer. We also add the HB timer
1930f8829a4aSRandall Stewart 		 * PLUS a random jitter.
1931f8829a4aSRandall Stewart 		 */
1932ca85e948SMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
1933ad81507eSRandall Stewart 			return;
1934ad81507eSRandall Stewart 		} else {
1935f8829a4aSRandall Stewart 			uint32_t rndval;
1936ca85e948SMichael Tuexen 			uint32_t jitter;
1937f8829a4aSRandall Stewart 
1938ca85e948SMichael Tuexen 			if ((net->dest_state & SCTP_ADDR_NOHB) &&
1939ca85e948SMichael Tuexen 			    !(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
1940ad81507eSRandall Stewart 				return;
1941f8829a4aSRandall Stewart 			}
1942f8829a4aSRandall Stewart 			if (net->RTO == 0) {
1943ca85e948SMichael Tuexen 				to_ticks = stcb->asoc.initial_rto;
1944f8829a4aSRandall Stewart 			} else {
1945ca85e948SMichael Tuexen 				to_ticks = net->RTO;
1946f8829a4aSRandall Stewart 			}
1947ca85e948SMichael Tuexen 			rndval = sctp_select_initial_TSN(&inp->sctp_ep);
1948ca85e948SMichael Tuexen 			jitter = rndval % to_ticks;
1949ca85e948SMichael Tuexen 			if (jitter >= (to_ticks >> 1)) {
1950ca85e948SMichael Tuexen 				to_ticks = to_ticks + (jitter - (to_ticks >> 1));
1951f8829a4aSRandall Stewart 			} else {
1952ca85e948SMichael Tuexen 				to_ticks = to_ticks - jitter;
1953f8829a4aSRandall Stewart 			}
1954ca85e948SMichael Tuexen 			if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
1955ca85e948SMichael Tuexen 			    !(net->dest_state & SCTP_ADDR_PF)) {
1956ca85e948SMichael Tuexen 				to_ticks += net->heart_beat_delay;
1957f8829a4aSRandall Stewart 			}
1958f8829a4aSRandall Stewart 			/*
1959f8829a4aSRandall Stewart 			 * Now we must convert the to_ticks that are now in
1960f8829a4aSRandall Stewart 			 * ms to ticks.
1961f8829a4aSRandall Stewart 			 */
1962f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(to_ticks);
1963ca85e948SMichael Tuexen 			tmr = &net->hb_timer;
1964f8829a4aSRandall Stewart 		}
1965f8829a4aSRandall Stewart 		break;
1966f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
1967f8829a4aSRandall Stewart 		/*
1968f8829a4aSRandall Stewart 		 * Here we can use the RTO timer from the network since one
1969f8829a4aSRandall Stewart 		 * RTT was compelete. If a retran happened then we will be
1970f8829a4aSRandall Stewart 		 * using the RTO initial value.
1971f8829a4aSRandall Stewart 		 */
1972f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
1973ad81507eSRandall Stewart 			return;
1974f8829a4aSRandall Stewart 		}
1975f8829a4aSRandall Stewart 		if (net->RTO == 0) {
1976f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1977f8829a4aSRandall Stewart 		} else {
1978f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
1979f8829a4aSRandall Stewart 		}
1980f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
1981f8829a4aSRandall Stewart 		break;
1982f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_NEWCOOKIE:
1983f8829a4aSRandall Stewart 		/*
1984f8829a4aSRandall Stewart 		 * nothing needed but the endpoint here ususually about 60
1985f8829a4aSRandall Stewart 		 * minutes.
1986f8829a4aSRandall Stewart 		 */
1987ad81507eSRandall Stewart 		if (inp == NULL) {
1988ad81507eSRandall Stewart 			return;
1989ad81507eSRandall Stewart 		}
1990f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
1991f8829a4aSRandall Stewart 		to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_SIGNATURE];
1992f8829a4aSRandall Stewart 		break;
1993f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASOCKILL:
1994f8829a4aSRandall Stewart 		if (stcb == NULL) {
1995ad81507eSRandall Stewart 			return;
1996f8829a4aSRandall Stewart 		}
1997f8829a4aSRandall Stewart 		tmr = &stcb->asoc.strreset_timer;
1998f8829a4aSRandall Stewart 		to_ticks = MSEC_TO_TICKS(SCTP_ASOC_KILL_TIMEOUT);
1999f8829a4aSRandall Stewart 		break;
2000f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INPKILL:
2001f8829a4aSRandall Stewart 		/*
2002f8829a4aSRandall Stewart 		 * The inp is setup to die. We re-use the signature_chage
2003f8829a4aSRandall Stewart 		 * timer since that has stopped and we are in the GONE
2004f8829a4aSRandall Stewart 		 * state.
2005f8829a4aSRandall Stewart 		 */
2006ad81507eSRandall Stewart 		if (inp == NULL) {
2007ad81507eSRandall Stewart 			return;
2008ad81507eSRandall Stewart 		}
2009f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
2010f8829a4aSRandall Stewart 		to_ticks = MSEC_TO_TICKS(SCTP_INP_KILL_TIMEOUT);
2011f8829a4aSRandall Stewart 		break;
2012f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
2013f8829a4aSRandall Stewart 		/*
2014f8829a4aSRandall Stewart 		 * Here we use the value found in the EP for PMTU ususually
2015f8829a4aSRandall Stewart 		 * about 10 minutes.
2016f8829a4aSRandall Stewart 		 */
2017ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
2018ad81507eSRandall Stewart 			return;
2019f8829a4aSRandall Stewart 		}
2020f8829a4aSRandall Stewart 		if (net == NULL) {
2021ad81507eSRandall Stewart 			return;
2022f8829a4aSRandall Stewart 		}
202380c79bbeSMichael Tuexen 		if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
202480c79bbeSMichael Tuexen 			return;
202580c79bbeSMichael Tuexen 		}
2026f8829a4aSRandall Stewart 		to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_PMTU];
2027f8829a4aSRandall Stewart 		tmr = &net->pmtu_timer;
2028f8829a4aSRandall Stewart 		break;
2029f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
2030f8829a4aSRandall Stewart 		/* Here we use the RTO of the destination */
2031f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
2032ad81507eSRandall Stewart 			return;
2033f8829a4aSRandall Stewart 		}
2034f8829a4aSRandall Stewart 		if (net->RTO == 0) {
2035f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2036f8829a4aSRandall Stewart 		} else {
2037f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
2038f8829a4aSRandall Stewart 		}
2039f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2040f8829a4aSRandall Stewart 		break;
2041f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2042f8829a4aSRandall Stewart 		/*
2043f8829a4aSRandall Stewart 		 * Here we use the endpoints shutdown guard timer usually
2044f8829a4aSRandall Stewart 		 * about 3 minutes.
2045f8829a4aSRandall Stewart 		 */
2046ad81507eSRandall Stewart 		if ((inp == NULL) || (stcb == NULL)) {
2047ad81507eSRandall Stewart 			return;
2048f8829a4aSRandall Stewart 		}
2049f8829a4aSRandall Stewart 		to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN];
2050f8829a4aSRandall Stewart 		tmr = &stcb->asoc.shut_guard_timer;
2051f8829a4aSRandall Stewart 		break;
2052f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_STRRESET:
2053f8829a4aSRandall Stewart 		/*
20541b649582SRandall Stewart 		 * Here the timer comes from the stcb but its value is from
20551b649582SRandall Stewart 		 * the net's RTO.
2056f8829a4aSRandall Stewart 		 */
2057f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
2058ad81507eSRandall Stewart 			return;
2059f8829a4aSRandall Stewart 		}
2060f8829a4aSRandall Stewart 		if (net->RTO == 0) {
2061f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2062f8829a4aSRandall Stewart 		} else {
2063f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
2064f8829a4aSRandall Stewart 		}
2065f8829a4aSRandall Stewart 		tmr = &stcb->asoc.strreset_timer;
2066f8829a4aSRandall Stewart 		break;
2067f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASCONF:
2068f8829a4aSRandall Stewart 		/*
20691b649582SRandall Stewart 		 * Here the timer comes from the stcb but its value is from
20701b649582SRandall Stewart 		 * the net's RTO.
2071f8829a4aSRandall Stewart 		 */
2072f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
2073ad81507eSRandall Stewart 			return;
2074f8829a4aSRandall Stewart 		}
2075f8829a4aSRandall Stewart 		if (net->RTO == 0) {
2076f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2077f8829a4aSRandall Stewart 		} else {
2078f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
2079f8829a4aSRandall Stewart 		}
2080f8829a4aSRandall Stewart 		tmr = &stcb->asoc.asconf_timer;
2081f8829a4aSRandall Stewart 		break;
2082851b7298SRandall Stewart 	case SCTP_TIMER_TYPE_PRIM_DELETED:
2083851b7298SRandall Stewart 		if ((stcb == NULL) || (net != NULL)) {
2084851b7298SRandall Stewart 			return;
2085851b7298SRandall Stewart 		}
2086851b7298SRandall Stewart 		to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2087851b7298SRandall Stewart 		tmr = &stcb->asoc.delete_prim_timer;
2088851b7298SRandall Stewart 		break;
2089f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_AUTOCLOSE:
2090f8829a4aSRandall Stewart 		if (stcb == NULL) {
2091ad81507eSRandall Stewart 			return;
2092f8829a4aSRandall Stewart 		}
2093f8829a4aSRandall Stewart 		if (stcb->asoc.sctp_autoclose_ticks == 0) {
2094f8829a4aSRandall Stewart 			/*
2095f8829a4aSRandall Stewart 			 * Really an error since stcb is NOT set to
2096f8829a4aSRandall Stewart 			 * autoclose
2097f8829a4aSRandall Stewart 			 */
2098ad81507eSRandall Stewart 			return;
2099f8829a4aSRandall Stewart 		}
2100f8829a4aSRandall Stewart 		to_ticks = stcb->asoc.sctp_autoclose_ticks;
2101f8829a4aSRandall Stewart 		tmr = &stcb->asoc.autoclose_timer;
2102f8829a4aSRandall Stewart 		break;
2103f8829a4aSRandall Stewart 	default:
2104ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2105ad81507eSRandall Stewart 		    __FUNCTION__, t_type);
2106ad81507eSRandall Stewart 		return;
2107f8829a4aSRandall Stewart 		break;
210860990c0cSMichael Tuexen 	}
2109f8829a4aSRandall Stewart 	if ((to_ticks <= 0) || (tmr == NULL)) {
2110ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER1, "%s: %d:software error to_ticks:%d tmr:%p not set ??\n",
2111dd294dceSMichael Tuexen 		    __FUNCTION__, t_type, to_ticks, (void *)tmr);
2112ad81507eSRandall Stewart 		return;
2113f8829a4aSRandall Stewart 	}
2114139bc87fSRandall Stewart 	if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
2115f8829a4aSRandall Stewart 		/*
2116f8829a4aSRandall Stewart 		 * we do NOT allow you to have it already running. if it is
2117f8829a4aSRandall Stewart 		 * we leave the current one up unchanged
2118f8829a4aSRandall Stewart 		 */
2119ad81507eSRandall Stewart 		return;
2120f8829a4aSRandall Stewart 	}
2121f8829a4aSRandall Stewart 	/* At this point we can proceed */
2122f8829a4aSRandall Stewart 	if (t_type == SCTP_TIMER_TYPE_SEND) {
2123f8829a4aSRandall Stewart 		stcb->asoc.num_send_timers_up++;
2124f8829a4aSRandall Stewart 	}
2125a5d547adSRandall Stewart 	tmr->stopped_from = 0;
2126f8829a4aSRandall Stewart 	tmr->type = t_type;
2127f8829a4aSRandall Stewart 	tmr->ep = (void *)inp;
2128f8829a4aSRandall Stewart 	tmr->tcb = (void *)stcb;
2129f8829a4aSRandall Stewart 	tmr->net = (void *)net;
2130f8829a4aSRandall Stewart 	tmr->self = (void *)tmr;
21318518270eSMichael Tuexen 	tmr->vnet = (void *)curvnet;
2132c4739e2fSRandall Stewart 	tmr->ticks = sctp_get_tick_count();
2133ad81507eSRandall Stewart 	(void)SCTP_OS_TIMER_START(&tmr->timer, to_ticks, sctp_timeout_handler, tmr);
2134ad81507eSRandall Stewart 	return;
2135f8829a4aSRandall Stewart }
2136f8829a4aSRandall Stewart 
21376e55db54SRandall Stewart void
2138f8829a4aSRandall Stewart sctp_timer_stop(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2139a5d547adSRandall Stewart     struct sctp_nets *net, uint32_t from)
2140f8829a4aSRandall Stewart {
2141f8829a4aSRandall Stewart 	struct sctp_timer *tmr;
2142f8829a4aSRandall Stewart 
2143f8829a4aSRandall Stewart 	if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) &&
2144f8829a4aSRandall Stewart 	    (inp == NULL))
21456e55db54SRandall Stewart 		return;
2146f8829a4aSRandall Stewart 
2147f8829a4aSRandall Stewart 	tmr = NULL;
2148f8829a4aSRandall Stewart 	if (stcb) {
2149f8829a4aSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
2150f8829a4aSRandall Stewart 	}
2151f8829a4aSRandall Stewart 	switch (t_type) {
2152d61a0ae0SRandall Stewart 	case SCTP_TIMER_TYPE_ZERO_COPY:
2153d61a0ae0SRandall Stewart 		tmr = &inp->sctp_ep.zero_copy_timer;
2154d61a0ae0SRandall Stewart 		break;
2155ad21a364SRandall Stewart 	case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
2156ad21a364SRandall Stewart 		tmr = &inp->sctp_ep.zero_copy_sendq_timer;
2157ad21a364SRandall Stewart 		break;
2158f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ADDR_WQ:
2159b3f1ea41SRandall Stewart 		tmr = &SCTP_BASE_INFO(addr_wq_timer);
2160f8829a4aSRandall Stewart 		break;
2161f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SEND:
2162f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
21636e55db54SRandall Stewart 			return;
2164f8829a4aSRandall Stewart 		}
2165f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2166f8829a4aSRandall Stewart 		break;
2167f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
2168f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
21696e55db54SRandall Stewart 			return;
2170f8829a4aSRandall Stewart 		}
2171f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2172f8829a4aSRandall Stewart 		break;
2173f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
2174f8829a4aSRandall Stewart 		if (stcb == NULL) {
21756e55db54SRandall Stewart 			return;
2176f8829a4aSRandall Stewart 		}
2177f8829a4aSRandall Stewart 		tmr = &stcb->asoc.dack_timer;
2178f8829a4aSRandall Stewart 		break;
2179f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
2180f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
21816e55db54SRandall Stewart 			return;
2182f8829a4aSRandall Stewart 		}
2183f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2184f8829a4aSRandall Stewart 		break;
2185f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
2186ca85e948SMichael Tuexen 		if ((stcb == NULL) || (net == NULL)) {
21876e55db54SRandall Stewart 			return;
2188f8829a4aSRandall Stewart 		}
2189ca85e948SMichael Tuexen 		tmr = &net->hb_timer;
2190f8829a4aSRandall Stewart 		break;
2191f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
2192f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
21936e55db54SRandall Stewart 			return;
2194f8829a4aSRandall Stewart 		}
2195f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2196f8829a4aSRandall Stewart 		break;
2197f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_NEWCOOKIE:
2198f8829a4aSRandall Stewart 		/* nothing needed but the endpoint here */
2199f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
2200f8829a4aSRandall Stewart 		/*
2201f8829a4aSRandall Stewart 		 * We re-use the newcookie timer for the INP kill timer. We
2202f8829a4aSRandall Stewart 		 * must assure that we do not kill it by accident.
2203f8829a4aSRandall Stewart 		 */
2204f8829a4aSRandall Stewart 		break;
2205f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASOCKILL:
2206f8829a4aSRandall Stewart 		/*
2207f8829a4aSRandall Stewart 		 * Stop the asoc kill timer.
2208f8829a4aSRandall Stewart 		 */
2209f8829a4aSRandall Stewart 		if (stcb == NULL) {
22106e55db54SRandall Stewart 			return;
2211f8829a4aSRandall Stewart 		}
2212f8829a4aSRandall Stewart 		tmr = &stcb->asoc.strreset_timer;
2213f8829a4aSRandall Stewart 		break;
2214f8829a4aSRandall Stewart 
2215f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INPKILL:
2216f8829a4aSRandall Stewart 		/*
2217f8829a4aSRandall Stewart 		 * The inp is setup to die. We re-use the signature_chage
2218f8829a4aSRandall Stewart 		 * timer since that has stopped and we are in the GONE
2219f8829a4aSRandall Stewart 		 * state.
2220f8829a4aSRandall Stewart 		 */
2221f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
2222f8829a4aSRandall Stewart 		break;
2223f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
2224f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
22256e55db54SRandall Stewart 			return;
2226f8829a4aSRandall Stewart 		}
2227f8829a4aSRandall Stewart 		tmr = &net->pmtu_timer;
2228f8829a4aSRandall Stewart 		break;
2229f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
2230f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
22316e55db54SRandall Stewart 			return;
2232f8829a4aSRandall Stewart 		}
2233f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2234f8829a4aSRandall Stewart 		break;
2235f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2236f8829a4aSRandall Stewart 		if (stcb == NULL) {
22376e55db54SRandall Stewart 			return;
2238f8829a4aSRandall Stewart 		}
2239f8829a4aSRandall Stewart 		tmr = &stcb->asoc.shut_guard_timer;
2240f8829a4aSRandall Stewart 		break;
2241f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_STRRESET:
2242f8829a4aSRandall Stewart 		if (stcb == NULL) {
22436e55db54SRandall Stewart 			return;
2244f8829a4aSRandall Stewart 		}
2245f8829a4aSRandall Stewart 		tmr = &stcb->asoc.strreset_timer;
2246f8829a4aSRandall Stewart 		break;
2247f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASCONF:
2248f8829a4aSRandall Stewart 		if (stcb == NULL) {
22496e55db54SRandall Stewart 			return;
2250f8829a4aSRandall Stewart 		}
2251f8829a4aSRandall Stewart 		tmr = &stcb->asoc.asconf_timer;
2252f8829a4aSRandall Stewart 		break;
2253851b7298SRandall Stewart 	case SCTP_TIMER_TYPE_PRIM_DELETED:
2254851b7298SRandall Stewart 		if (stcb == NULL) {
2255851b7298SRandall Stewart 			return;
2256851b7298SRandall Stewart 		}
2257851b7298SRandall Stewart 		tmr = &stcb->asoc.delete_prim_timer;
2258851b7298SRandall Stewart 		break;
2259f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_AUTOCLOSE:
2260f8829a4aSRandall Stewart 		if (stcb == NULL) {
22616e55db54SRandall Stewart 			return;
2262f8829a4aSRandall Stewart 		}
2263f8829a4aSRandall Stewart 		tmr = &stcb->asoc.autoclose_timer;
2264f8829a4aSRandall Stewart 		break;
2265f8829a4aSRandall Stewart 	default:
2266ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2267ad81507eSRandall Stewart 		    __FUNCTION__, t_type);
2268f8829a4aSRandall Stewart 		break;
226960990c0cSMichael Tuexen 	}
2270f8829a4aSRandall Stewart 	if (tmr == NULL) {
22716e55db54SRandall Stewart 		return;
2272f8829a4aSRandall Stewart 	}
2273f8829a4aSRandall Stewart 	if ((tmr->type != t_type) && tmr->type) {
2274f8829a4aSRandall Stewart 		/*
2275f8829a4aSRandall Stewart 		 * Ok we have a timer that is under joint use. Cookie timer
2276f8829a4aSRandall Stewart 		 * per chance with the SEND timer. We therefore are NOT
2277f8829a4aSRandall Stewart 		 * running the timer that the caller wants stopped.  So just
2278f8829a4aSRandall Stewart 		 * return.
2279f8829a4aSRandall Stewart 		 */
22806e55db54SRandall Stewart 		return;
2281f8829a4aSRandall Stewart 	}
2282ad81507eSRandall Stewart 	if ((t_type == SCTP_TIMER_TYPE_SEND) && (stcb != NULL)) {
2283f8829a4aSRandall Stewart 		stcb->asoc.num_send_timers_up--;
2284f8829a4aSRandall Stewart 		if (stcb->asoc.num_send_timers_up < 0) {
2285f8829a4aSRandall Stewart 			stcb->asoc.num_send_timers_up = 0;
2286f8829a4aSRandall Stewart 		}
2287f8829a4aSRandall Stewart 	}
2288f8829a4aSRandall Stewart 	tmr->self = NULL;
2289a5d547adSRandall Stewart 	tmr->stopped_from = from;
22906e55db54SRandall Stewart 	(void)SCTP_OS_TIMER_STOP(&tmr->timer);
22916e55db54SRandall Stewart 	return;
2292f8829a4aSRandall Stewart }
2293f8829a4aSRandall Stewart 
2294f8829a4aSRandall Stewart uint32_t
2295f8829a4aSRandall Stewart sctp_calculate_len(struct mbuf *m)
2296f8829a4aSRandall Stewart {
2297f8829a4aSRandall Stewart 	uint32_t tlen = 0;
2298f8829a4aSRandall Stewart 	struct mbuf *at;
2299f8829a4aSRandall Stewart 
2300f8829a4aSRandall Stewart 	at = m;
2301f8829a4aSRandall Stewart 	while (at) {
2302139bc87fSRandall Stewart 		tlen += SCTP_BUF_LEN(at);
2303139bc87fSRandall Stewart 		at = SCTP_BUF_NEXT(at);
2304f8829a4aSRandall Stewart 	}
2305f8829a4aSRandall Stewart 	return (tlen);
2306f8829a4aSRandall Stewart }
2307f8829a4aSRandall Stewart 
2308f8829a4aSRandall Stewart void
2309f8829a4aSRandall Stewart sctp_mtu_size_reset(struct sctp_inpcb *inp,
231044b7479bSRandall Stewart     struct sctp_association *asoc, uint32_t mtu)
2311f8829a4aSRandall Stewart {
2312f8829a4aSRandall Stewart 	/*
2313f8829a4aSRandall Stewart 	 * Reset the P-MTU size on this association, this involves changing
2314f8829a4aSRandall Stewart 	 * the asoc MTU, going through ANY chunk+overhead larger than mtu to
2315f8829a4aSRandall Stewart 	 * allow the DF flag to be cleared.
2316f8829a4aSRandall Stewart 	 */
2317f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
2318f8829a4aSRandall Stewart 	unsigned int eff_mtu, ovh;
2319f8829a4aSRandall Stewart 
2320f8829a4aSRandall Stewart 	asoc->smallest_mtu = mtu;
2321f8829a4aSRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2322f8829a4aSRandall Stewart 		ovh = SCTP_MIN_OVERHEAD;
2323f8829a4aSRandall Stewart 	} else {
2324f8829a4aSRandall Stewart 		ovh = SCTP_MIN_V4_OVERHEAD;
2325f8829a4aSRandall Stewart 	}
2326f8829a4aSRandall Stewart 	eff_mtu = mtu - ovh;
2327f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
2328f8829a4aSRandall Stewart 		if (chk->send_size > eff_mtu) {
2329f8829a4aSRandall Stewart 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2330f8829a4aSRandall Stewart 		}
2331f8829a4aSRandall Stewart 	}
2332f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
2333f8829a4aSRandall Stewart 		if (chk->send_size > eff_mtu) {
2334f8829a4aSRandall Stewart 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2335f8829a4aSRandall Stewart 		}
2336f8829a4aSRandall Stewart 	}
2337f8829a4aSRandall Stewart }
2338f8829a4aSRandall Stewart 
2339f8829a4aSRandall Stewart 
2340f8829a4aSRandall Stewart /*
2341f8829a4aSRandall Stewart  * given an association and starting time of the current RTT period return
2342f42a358aSRandall Stewart  * RTO in number of msecs net should point to the current network
2343f8829a4aSRandall Stewart  */
2344899288aeSRandall Stewart 
2345f8829a4aSRandall Stewart uint32_t
2346f8829a4aSRandall Stewart sctp_calculate_rto(struct sctp_tcb *stcb,
2347f8829a4aSRandall Stewart     struct sctp_association *asoc,
2348f8829a4aSRandall Stewart     struct sctp_nets *net,
234918e198d3SRandall Stewart     struct timeval *told,
2350f79aab18SRandall Stewart     int safe, int rtt_from_sack)
2351f8829a4aSRandall Stewart {
235218e198d3SRandall Stewart 	/*-
2353f8829a4aSRandall Stewart 	 * given an association and the starting time of the current RTT
2354f42a358aSRandall Stewart 	 * period (in value1/value2) return RTO in number of msecs.
2355f8829a4aSRandall Stewart 	 */
2356be1d9176SMichael Tuexen 	int32_t rtt;		/* RTT in ms */
2357be1d9176SMichael Tuexen 	uint32_t new_rto;
2358f8829a4aSRandall Stewart 	int first_measure = 0;
235918e198d3SRandall Stewart 	struct timeval now, then, *old;
2360f8829a4aSRandall Stewart 
236118e198d3SRandall Stewart 	/* Copy it out for sparc64 */
236218e198d3SRandall Stewart 	if (safe == sctp_align_unsafe_makecopy) {
236318e198d3SRandall Stewart 		old = &then;
236418e198d3SRandall Stewart 		memcpy(&then, told, sizeof(struct timeval));
236518e198d3SRandall Stewart 	} else if (safe == sctp_align_safe_nocopy) {
236618e198d3SRandall Stewart 		old = told;
236718e198d3SRandall Stewart 	} else {
236818e198d3SRandall Stewart 		/* error */
236918e198d3SRandall Stewart 		SCTP_PRINTF("Huh, bad rto calc call\n");
237018e198d3SRandall Stewart 		return (0);
237118e198d3SRandall Stewart 	}
2372f8829a4aSRandall Stewart 	/************************/
2373f8829a4aSRandall Stewart 	/* 1. calculate new RTT */
2374f8829a4aSRandall Stewart 	/************************/
2375f8829a4aSRandall Stewart 	/* get the current time */
2376299108c5SRandall Stewart 	if (stcb->asoc.use_precise_time) {
2377299108c5SRandall Stewart 		(void)SCTP_GETPTIME_TIMEVAL(&now);
2378299108c5SRandall Stewart 	} else {
23796e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
2380299108c5SRandall Stewart 	}
2381be1d9176SMichael Tuexen 	timevalsub(&now, old);
2382be1d9176SMichael Tuexen 	/* store the current RTT in us */
238381eb4e63SMichael Tuexen 	net->rtt = (uint64_t) 1000000 *(uint64_t) now.tv_sec +
2384be1d9176SMichael Tuexen 	        (uint64_t) now.tv_usec;
2385be1d9176SMichael Tuexen 
2386be1d9176SMichael Tuexen 	/* computer rtt in ms */
2387be1d9176SMichael Tuexen 	rtt = net->rtt / 1000;
2388f79aab18SRandall Stewart 	if ((asoc->cc_functions.sctp_rtt_calculated) && (rtt_from_sack == SCTP_RTT_FROM_DATA)) {
2389f79aab18SRandall Stewart 		/*
2390f79aab18SRandall Stewart 		 * Tell the CC module that a new update has just occurred
2391f79aab18SRandall Stewart 		 * from a sack
2392f79aab18SRandall Stewart 		 */
2393f79aab18SRandall Stewart 		(*asoc->cc_functions.sctp_rtt_calculated) (stcb, net, &now);
2394f79aab18SRandall Stewart 	}
2395f79aab18SRandall Stewart 	/*
2396f79aab18SRandall Stewart 	 * Do we need to determine the lan? We do this only on sacks i.e.
2397f79aab18SRandall Stewart 	 * RTT being determined from data not non-data (HB/INIT->INITACK).
2398f79aab18SRandall Stewart 	 */
2399f79aab18SRandall Stewart 	if ((rtt_from_sack == SCTP_RTT_FROM_DATA) &&
2400be1d9176SMichael Tuexen 	    (net->lan_type == SCTP_LAN_UNKNOWN)) {
2401be1d9176SMichael Tuexen 		if (net->rtt > SCTP_LOCAL_LAN_RTT) {
2402899288aeSRandall Stewart 			net->lan_type = SCTP_LAN_INTERNET;
2403899288aeSRandall Stewart 		} else {
2404899288aeSRandall Stewart 			net->lan_type = SCTP_LAN_LOCAL;
2405899288aeSRandall Stewart 		}
2406899288aeSRandall Stewart 	}
2407f8829a4aSRandall Stewart 	/***************************/
2408f8829a4aSRandall Stewart 	/* 2. update RTTVAR & SRTT */
2409f8829a4aSRandall Stewart 	/***************************/
2410be1d9176SMichael Tuexen 	/*-
2411be1d9176SMichael Tuexen 	 * Compute the scaled average lastsa and the
2412be1d9176SMichael Tuexen 	 * scaled variance lastsv as described in van Jacobson
2413be1d9176SMichael Tuexen 	 * Paper "Congestion Avoidance and Control", Annex A.
2414be1d9176SMichael Tuexen 	 *
2415be1d9176SMichael Tuexen 	 * (net->lastsa >> SCTP_RTT_SHIFT) is the srtt
2416be1d9176SMichael Tuexen 	 * (net->lastsa >> SCTP_RTT_VAR_SHIFT) is the rttvar
2417be1d9176SMichael Tuexen 	 */
24189a972525SRandall Stewart 	if (net->RTO_measured) {
2419be1d9176SMichael Tuexen 		rtt -= (net->lastsa >> SCTP_RTT_SHIFT);
2420be1d9176SMichael Tuexen 		net->lastsa += rtt;
2421be1d9176SMichael Tuexen 		if (rtt < 0) {
2422be1d9176SMichael Tuexen 			rtt = -rtt;
2423be1d9176SMichael Tuexen 		}
2424be1d9176SMichael Tuexen 		rtt -= (net->lastsv >> SCTP_RTT_VAR_SHIFT);
2425be1d9176SMichael Tuexen 		net->lastsv += rtt;
2426b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2427f8829a4aSRandall Stewart 			rto_logging(net, SCTP_LOG_RTTVAR);
242880fefe0aSRandall Stewart 		}
2429f8829a4aSRandall Stewart 	} else {
2430f8829a4aSRandall Stewart 		/* First RTO measurment */
24319a972525SRandall Stewart 		net->RTO_measured = 1;
2432f8829a4aSRandall Stewart 		first_measure = 1;
2433be1d9176SMichael Tuexen 		net->lastsa = rtt << SCTP_RTT_SHIFT;
2434be1d9176SMichael Tuexen 		net->lastsv = (rtt / 2) << SCTP_RTT_VAR_SHIFT;
2435b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2436f8829a4aSRandall Stewart 			rto_logging(net, SCTP_LOG_INITIAL_RTT);
243780fefe0aSRandall Stewart 		}
2438f8829a4aSRandall Stewart 	}
2439be1d9176SMichael Tuexen 	if (net->lastsv == 0) {
2440be1d9176SMichael Tuexen 		net->lastsv = SCTP_CLOCK_GRANULARITY;
2441be1d9176SMichael Tuexen 	}
2442108df27cSRandall Stewart 	new_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
2443f8829a4aSRandall Stewart 	if ((new_rto > SCTP_SAT_NETWORK_MIN) &&
2444f8829a4aSRandall Stewart 	    (stcb->asoc.sat_network_lockout == 0)) {
2445f8829a4aSRandall Stewart 		stcb->asoc.sat_network = 1;
2446f8829a4aSRandall Stewart 	} else if ((!first_measure) && stcb->asoc.sat_network) {
2447f8829a4aSRandall Stewart 		stcb->asoc.sat_network = 0;
2448f8829a4aSRandall Stewart 		stcb->asoc.sat_network_lockout = 1;
2449f8829a4aSRandall Stewart 	}
2450f8829a4aSRandall Stewart 	/* bound it, per C6/C7 in Section 5.3.1 */
2451f8829a4aSRandall Stewart 	if (new_rto < stcb->asoc.minrto) {
2452f8829a4aSRandall Stewart 		new_rto = stcb->asoc.minrto;
2453f8829a4aSRandall Stewart 	}
2454f8829a4aSRandall Stewart 	if (new_rto > stcb->asoc.maxrto) {
2455f8829a4aSRandall Stewart 		new_rto = stcb->asoc.maxrto;
2456f8829a4aSRandall Stewart 	}
24575e54f665SRandall Stewart 	/* we are now returning the RTO */
24585e54f665SRandall Stewart 	return (new_rto);
2459f8829a4aSRandall Stewart }
2460f8829a4aSRandall Stewart 
2461f8829a4aSRandall Stewart /*
2462f8829a4aSRandall Stewart  * return a pointer to a contiguous piece of data from the given mbuf chain
2463f8829a4aSRandall Stewart  * starting at 'off' for 'len' bytes.  If the desired piece spans more than
2464f8829a4aSRandall Stewart  * one mbuf, a copy is made at 'ptr'. caller must ensure that the buffer size
2465f8829a4aSRandall Stewart  * is >= 'len' returns NULL if there there isn't 'len' bytes in the chain.
2466f8829a4aSRandall Stewart  */
246772fb6fdbSRandall Stewart caddr_t
2468f8829a4aSRandall Stewart sctp_m_getptr(struct mbuf *m, int off, int len, uint8_t * in_ptr)
2469f8829a4aSRandall Stewart {
2470f8829a4aSRandall Stewart 	uint32_t count;
2471f8829a4aSRandall Stewart 	uint8_t *ptr;
2472f8829a4aSRandall Stewart 
2473f8829a4aSRandall Stewart 	ptr = in_ptr;
2474f8829a4aSRandall Stewart 	if ((off < 0) || (len <= 0))
2475f8829a4aSRandall Stewart 		return (NULL);
2476f8829a4aSRandall Stewart 
2477f8829a4aSRandall Stewart 	/* find the desired start location */
2478f8829a4aSRandall Stewart 	while ((m != NULL) && (off > 0)) {
2479139bc87fSRandall Stewart 		if (off < SCTP_BUF_LEN(m))
2480f8829a4aSRandall Stewart 			break;
2481139bc87fSRandall Stewart 		off -= SCTP_BUF_LEN(m);
2482139bc87fSRandall Stewart 		m = SCTP_BUF_NEXT(m);
2483f8829a4aSRandall Stewart 	}
2484f8829a4aSRandall Stewart 	if (m == NULL)
2485f8829a4aSRandall Stewart 		return (NULL);
2486f8829a4aSRandall Stewart 
2487f8829a4aSRandall Stewart 	/* is the current mbuf large enough (eg. contiguous)? */
2488139bc87fSRandall Stewart 	if ((SCTP_BUF_LEN(m) - off) >= len) {
2489f8829a4aSRandall Stewart 		return (mtod(m, caddr_t)+off);
2490f8829a4aSRandall Stewart 	} else {
2491f8829a4aSRandall Stewart 		/* else, it spans more than one mbuf, so save a temp copy... */
2492f8829a4aSRandall Stewart 		while ((m != NULL) && (len > 0)) {
2493139bc87fSRandall Stewart 			count = min(SCTP_BUF_LEN(m) - off, len);
2494f8829a4aSRandall Stewart 			bcopy(mtod(m, caddr_t)+off, ptr, count);
2495f8829a4aSRandall Stewart 			len -= count;
2496f8829a4aSRandall Stewart 			ptr += count;
2497f8829a4aSRandall Stewart 			off = 0;
2498139bc87fSRandall Stewart 			m = SCTP_BUF_NEXT(m);
2499f8829a4aSRandall Stewart 		}
2500f8829a4aSRandall Stewart 		if ((m == NULL) && (len > 0))
2501f8829a4aSRandall Stewart 			return (NULL);
2502f8829a4aSRandall Stewart 		else
2503f8829a4aSRandall Stewart 			return ((caddr_t)in_ptr);
2504f8829a4aSRandall Stewart 	}
2505f8829a4aSRandall Stewart }
2506f8829a4aSRandall Stewart 
2507f8829a4aSRandall Stewart 
250844b7479bSRandall Stewart 
2509f8829a4aSRandall Stewart struct sctp_paramhdr *
2510f8829a4aSRandall Stewart sctp_get_next_param(struct mbuf *m,
2511f8829a4aSRandall Stewart     int offset,
2512f8829a4aSRandall Stewart     struct sctp_paramhdr *pull,
2513f8829a4aSRandall Stewart     int pull_limit)
2514f8829a4aSRandall Stewart {
2515f8829a4aSRandall Stewart 	/* This just provides a typed signature to Peter's Pull routine */
2516f8829a4aSRandall Stewart 	return ((struct sctp_paramhdr *)sctp_m_getptr(m, offset, pull_limit,
2517f8829a4aSRandall Stewart 	    (uint8_t *) pull));
2518f8829a4aSRandall Stewart }
2519f8829a4aSRandall Stewart 
2520f8829a4aSRandall Stewart 
2521ce11b842SMichael Tuexen struct mbuf *
2522f8829a4aSRandall Stewart sctp_add_pad_tombuf(struct mbuf *m, int padlen)
2523f8829a4aSRandall Stewart {
2524ce11b842SMichael Tuexen 	struct mbuf *m_last;
2525ce11b842SMichael Tuexen 	caddr_t dp;
2526f8829a4aSRandall Stewart 
2527f8829a4aSRandall Stewart 	if (padlen > 3) {
2528ce11b842SMichael Tuexen 		return (NULL);
2529f8829a4aSRandall Stewart 	}
253041eee555SRandall Stewart 	if (padlen <= M_TRAILINGSPACE(m)) {
2531f8829a4aSRandall Stewart 		/*
2532f8829a4aSRandall Stewart 		 * The easy way. We hope the majority of the time we hit
2533f8829a4aSRandall Stewart 		 * here :)
2534f8829a4aSRandall Stewart 		 */
2535ce11b842SMichael Tuexen 		m_last = m;
2536f8829a4aSRandall Stewart 	} else {
2537ce11b842SMichael Tuexen 		/* Hard way we must grow the mbuf chain */
2538ce11b842SMichael Tuexen 		m_last = sctp_get_mbuf_for_msg(padlen, 0, M_NOWAIT, 1, MT_DATA);
2539ce11b842SMichael Tuexen 		if (m_last == NULL) {
2540ce11b842SMichael Tuexen 			return (NULL);
2541f8829a4aSRandall Stewart 		}
2542ce11b842SMichael Tuexen 		SCTP_BUF_LEN(m_last) = 0;
2543ce11b842SMichael Tuexen 		SCTP_BUF_NEXT(m_last) = NULL;
2544ce11b842SMichael Tuexen 		SCTP_BUF_NEXT(m) = m_last;
2545f8829a4aSRandall Stewart 	}
2546ce11b842SMichael Tuexen 	dp = mtod(m_last, caddr_t)+SCTP_BUF_LEN(m_last);
2547ce11b842SMichael Tuexen 	SCTP_BUF_LEN(m_last) += padlen;
2548ce11b842SMichael Tuexen 	memset(dp, 0, padlen);
2549ce11b842SMichael Tuexen 	return (m_last);
2550f8829a4aSRandall Stewart }
2551f8829a4aSRandall Stewart 
2552ce11b842SMichael Tuexen struct mbuf *
2553f8829a4aSRandall Stewart sctp_pad_lastmbuf(struct mbuf *m, int padval, struct mbuf *last_mbuf)
2554f8829a4aSRandall Stewart {
2555f8829a4aSRandall Stewart 	/* find the last mbuf in chain and pad it */
2556f8829a4aSRandall Stewart 	struct mbuf *m_at;
2557f8829a4aSRandall Stewart 
2558ce11b842SMichael Tuexen 	if (last_mbuf != NULL) {
2559f8829a4aSRandall Stewart 		return (sctp_add_pad_tombuf(last_mbuf, padval));
2560f8829a4aSRandall Stewart 	} else {
256117267b32SMichael Tuexen 		for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
2562139bc87fSRandall Stewart 			if (SCTP_BUF_NEXT(m_at) == NULL) {
2563f8829a4aSRandall Stewart 				return (sctp_add_pad_tombuf(m_at, padval));
2564f8829a4aSRandall Stewart 			}
2565f8829a4aSRandall Stewart 		}
2566f8829a4aSRandall Stewart 	}
2567ce11b842SMichael Tuexen 	return (NULL);
2568f8829a4aSRandall Stewart }
2569f8829a4aSRandall Stewart 
2570f8829a4aSRandall Stewart static void
2571c5b5675dSMichael Tuexen sctp_notify_assoc_change(uint16_t state, struct sctp_tcb *stcb,
2572410a3b1eSMichael Tuexen     uint16_t error, struct sctp_abort_chunk *abort, uint8_t from_peer, int so_locked
2573ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2574ceaad40aSRandall Stewart     SCTP_UNUSED
2575ceaad40aSRandall Stewart #endif
2576ceaad40aSRandall Stewart )
2577f8829a4aSRandall Stewart {
2578f8829a4aSRandall Stewart 	struct mbuf *m_notify;
2579f8829a4aSRandall Stewart 	struct sctp_assoc_change *sac;
2580f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
2581a2b42326SMichael Tuexen 	size_t notif_len, abort_len;
2582e06b67c7SMichael Tuexen 	unsigned int i;
2583f8829a4aSRandall Stewart 
2584ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2585ceaad40aSRandall Stewart 	struct socket *so;
2586ceaad40aSRandall Stewart 
2587ceaad40aSRandall Stewart #endif
2588ceaad40aSRandall Stewart 
258958411b08SMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT)) {
2590a2b42326SMichael Tuexen 		notif_len = sizeof(struct sctp_assoc_change);
2591a2b42326SMichael Tuexen 		if (abort != NULL) {
2592c9eb4473SMichael Tuexen 			abort_len = ntohs(abort->ch.chunk_length);
2593a2b42326SMichael Tuexen 		} else {
2594a2b42326SMichael Tuexen 			abort_len = 0;
2595c5b5675dSMichael Tuexen 		}
2596a2b42326SMichael Tuexen 		if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
2597a2b42326SMichael Tuexen 			notif_len += SCTP_ASSOC_SUPPORTS_MAX;
2598a2b42326SMichael Tuexen 		} else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
2599a2b42326SMichael Tuexen 			notif_len += abort_len;
2600a2b42326SMichael Tuexen 		}
2601eb1b1807SGleb Smirnoff 		m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
2602a2b42326SMichael Tuexen 		if (m_notify == NULL) {
2603a2b42326SMichael Tuexen 			/* Retry with smaller value. */
2604a2b42326SMichael Tuexen 			notif_len = sizeof(struct sctp_assoc_change);
2605eb1b1807SGleb Smirnoff 			m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
2606a2b42326SMichael Tuexen 			if (m_notify == NULL) {
260758411b08SMichael Tuexen 				goto set_error;
2608a2b42326SMichael Tuexen 			}
2609a2b42326SMichael Tuexen 		}
2610a2b42326SMichael Tuexen 		SCTP_BUF_NEXT(m_notify) = NULL;
2611f8829a4aSRandall Stewart 		sac = mtod(m_notify, struct sctp_assoc_change *);
2612e432298aSXin LI 		memset(sac, 0, notif_len);
2613f8829a4aSRandall Stewart 		sac->sac_type = SCTP_ASSOC_CHANGE;
2614f8829a4aSRandall Stewart 		sac->sac_flags = 0;
2615f8829a4aSRandall Stewart 		sac->sac_length = sizeof(struct sctp_assoc_change);
2616c5b5675dSMichael Tuexen 		sac->sac_state = state;
2617f8829a4aSRandall Stewart 		sac->sac_error = error;
2618f8829a4aSRandall Stewart 		/* XXX verify these stream counts */
2619f8829a4aSRandall Stewart 		sac->sac_outbound_streams = stcb->asoc.streamoutcnt;
2620f8829a4aSRandall Stewart 		sac->sac_inbound_streams = stcb->asoc.streamincnt;
2621f8829a4aSRandall Stewart 		sac->sac_assoc_id = sctp_get_associd(stcb);
2622a2b42326SMichael Tuexen 		if (notif_len > sizeof(struct sctp_assoc_change)) {
2623c5b5675dSMichael Tuexen 			if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
2624e06b67c7SMichael Tuexen 				i = 0;
2625dd973b0eSMichael Tuexen 				if (stcb->asoc.prsctp_supported) {
2626e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_PR;
2627e06b67c7SMichael Tuexen 				}
2628e06b67c7SMichael Tuexen 				if (stcb->asoc.peer_supports_auth) {
2629e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_AUTH;
2630e06b67c7SMichael Tuexen 				}
2631e06b67c7SMichael Tuexen 				if (stcb->asoc.peer_supports_asconf) {
2632e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_ASCONF;
2633e06b67c7SMichael Tuexen 				}
2634e06b67c7SMichael Tuexen 				sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_MULTIBUF;
2635*317e00efSMichael Tuexen 				if (stcb->asoc.reconfig_supported) {
2636e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_RE_CONFIG;
2637e06b67c7SMichael Tuexen 				}
2638e06b67c7SMichael Tuexen 				sac->sac_length += i;
2639a2b42326SMichael Tuexen 			} else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
2640a2b42326SMichael Tuexen 				memcpy(sac->sac_info, abort, abort_len);
2641a2b42326SMichael Tuexen 				sac->sac_length += abort_len;
2642a2b42326SMichael Tuexen 			}
2643c5b5675dSMichael Tuexen 		}
2644e06b67c7SMichael Tuexen 		SCTP_BUF_LEN(m_notify) = sac->sac_length;
2645f8829a4aSRandall Stewart 		control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
26467215cc1bSMichael Tuexen 		    0, 0, stcb->asoc.context, 0, 0, 0,
2647f8829a4aSRandall Stewart 		    m_notify);
264858411b08SMichael Tuexen 		if (control != NULL) {
2649139bc87fSRandall Stewart 			control->length = SCTP_BUF_LEN(m_notify);
2650f8829a4aSRandall Stewart 			/* not that we need this */
2651f8829a4aSRandall Stewart 			control->tail_mbuf = m_notify;
2652139bc87fSRandall Stewart 			control->spec_flags = M_NOTIFICATION;
2653f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep, stcb,
2654f8829a4aSRandall Stewart 			    control,
2655cfde3ff7SRandall Stewart 			    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD,
2656cfde3ff7SRandall Stewart 			    so_locked);
265758411b08SMichael Tuexen 		} else {
265858411b08SMichael Tuexen 			sctp_m_freem(m_notify);
265958411b08SMichael Tuexen 		}
266058411b08SMichael Tuexen 	}
266158411b08SMichael Tuexen 	/*
266258411b08SMichael Tuexen 	 * For 1-to-1 style sockets, we send up and error when an ABORT
266358411b08SMichael Tuexen 	 * comes in.
266458411b08SMichael Tuexen 	 */
266558411b08SMichael Tuexen set_error:
266658411b08SMichael Tuexen 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
266758411b08SMichael Tuexen 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
266858411b08SMichael Tuexen 	    ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
2669e045904fSMichael Tuexen 		SOCK_LOCK(stcb->sctp_socket);
2670410a3b1eSMichael Tuexen 		if (from_peer) {
267158411b08SMichael Tuexen 			if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
267258411b08SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNREFUSED);
267358411b08SMichael Tuexen 				stcb->sctp_socket->so_error = ECONNREFUSED;
267458411b08SMichael Tuexen 			} else {
267558411b08SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
267658411b08SMichael Tuexen 				stcb->sctp_socket->so_error = ECONNRESET;
267758411b08SMichael Tuexen 			}
2678410a3b1eSMichael Tuexen 		} else {
2679553bb068SMichael Tuexen 			if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) ||
2680553bb068SMichael Tuexen 			    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
2681553bb068SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ETIMEDOUT);
2682553bb068SMichael Tuexen 				stcb->sctp_socket->so_error = ETIMEDOUT;
2683553bb068SMichael Tuexen 			} else {
2684410a3b1eSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNABORTED);
2685410a3b1eSMichael Tuexen 				stcb->sctp_socket->so_error = ECONNABORTED;
2686410a3b1eSMichael Tuexen 			}
268758411b08SMichael Tuexen 		}
2688553bb068SMichael Tuexen 	}
268958411b08SMichael Tuexen 	/* Wake ANY sleepers */
2690ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2691ceaad40aSRandall Stewart 	so = SCTP_INP_SO(stcb->sctp_ep);
2692ceaad40aSRandall Stewart 	if (!so_locked) {
2693ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
2694ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
2695ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
2696ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
2697ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
2698ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2699ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
2700ceaad40aSRandall Stewart 			return;
2701ceaad40aSRandall Stewart 		}
2702ceaad40aSRandall Stewart 	}
2703ceaad40aSRandall Stewart #endif
270458411b08SMichael Tuexen 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
270558411b08SMichael Tuexen 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
270658411b08SMichael Tuexen 	    ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
2707e045904fSMichael Tuexen 		socantrcvmore_locked(stcb->sctp_socket);
270858411b08SMichael Tuexen 	}
270958411b08SMichael Tuexen 	sorwakeup(stcb->sctp_socket);
271058411b08SMichael Tuexen 	sowwakeup(stcb->sctp_socket);
2711ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2712ceaad40aSRandall Stewart 	if (!so_locked) {
2713ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
2714ceaad40aSRandall Stewart 	}
2715ceaad40aSRandall Stewart #endif
2716f8829a4aSRandall Stewart }
2717f8829a4aSRandall Stewart 
2718f8829a4aSRandall Stewart static void
2719f8829a4aSRandall Stewart sctp_notify_peer_addr_change(struct sctp_tcb *stcb, uint32_t state,
2720f8829a4aSRandall Stewart     struct sockaddr *sa, uint32_t error)
2721f8829a4aSRandall Stewart {
2722f8829a4aSRandall Stewart 	struct mbuf *m_notify;
2723f8829a4aSRandall Stewart 	struct sctp_paddr_change *spc;
2724f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
2725f8829a4aSRandall Stewart 
272660990c0cSMichael Tuexen 	if ((stcb == NULL) ||
272760990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT)) {
2728f8829a4aSRandall Stewart 		/* event not enabled */
2729f8829a4aSRandall Stewart 		return;
2730830d754dSRandall Stewart 	}
2731eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_paddr_change), 0, M_NOWAIT, 1, MT_DATA);
2732f8829a4aSRandall Stewart 	if (m_notify == NULL)
2733f8829a4aSRandall Stewart 		return;
2734139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
2735f8829a4aSRandall Stewart 	spc = mtod(m_notify, struct sctp_paddr_change *);
273656711f94SMichael Tuexen 	memset(spc, 0, sizeof(struct sctp_paddr_change));
2737f8829a4aSRandall Stewart 	spc->spc_type = SCTP_PEER_ADDR_CHANGE;
2738f8829a4aSRandall Stewart 	spc->spc_flags = 0;
2739f8829a4aSRandall Stewart 	spc->spc_length = sizeof(struct sctp_paddr_change);
27405e2c2d87SRandall Stewart 	switch (sa->sa_family) {
2741ea5eba11SMichael Tuexen #ifdef INET
27425e2c2d87SRandall Stewart 	case AF_INET:
2743f8829a4aSRandall Stewart 		memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
27445e2c2d87SRandall Stewart 		break;
2745ea5eba11SMichael Tuexen #endif
27465e2c2d87SRandall Stewart #ifdef INET6
27475e2c2d87SRandall Stewart 	case AF_INET6:
27485e2c2d87SRandall Stewart 		{
2749f42a358aSRandall Stewart 			struct sockaddr_in6 *sin6;
2750f42a358aSRandall Stewart 
2751f8829a4aSRandall Stewart 			memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in6));
2752f42a358aSRandall Stewart 
2753f42a358aSRandall Stewart 			sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
2754f42a358aSRandall Stewart 			if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
275542551e99SRandall Stewart 				if (sin6->sin6_scope_id == 0) {
275642551e99SRandall Stewart 					/* recover scope_id for user */
2757f42a358aSRandall Stewart 					(void)sa6_recoverscope(sin6);
275842551e99SRandall Stewart 				} else {
275942551e99SRandall Stewart 					/* clear embedded scope_id for user */
276042551e99SRandall Stewart 					in6_clearscope(&sin6->sin6_addr);
276142551e99SRandall Stewart 				}
2762f42a358aSRandall Stewart 			}
27635e2c2d87SRandall Stewart 			break;
27645e2c2d87SRandall Stewart 		}
27655e2c2d87SRandall Stewart #endif
27665e2c2d87SRandall Stewart 	default:
27675e2c2d87SRandall Stewart 		/* TSNH */
27685e2c2d87SRandall Stewart 		break;
2769f8829a4aSRandall Stewart 	}
2770f8829a4aSRandall Stewart 	spc->spc_state = state;
2771f8829a4aSRandall Stewart 	spc->spc_error = error;
2772f8829a4aSRandall Stewart 	spc->spc_assoc_id = sctp_get_associd(stcb);
2773f8829a4aSRandall Stewart 
2774139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_paddr_change);
2775139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
2776f8829a4aSRandall Stewart 
2777f8829a4aSRandall Stewart 	/* append to socket */
2778f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
27797215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
2780f8829a4aSRandall Stewart 	    m_notify);
2781f8829a4aSRandall Stewart 	if (control == NULL) {
2782f8829a4aSRandall Stewart 		/* no memory */
2783f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
2784f8829a4aSRandall Stewart 		return;
2785f8829a4aSRandall Stewart 	}
2786139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
2787139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
2788f8829a4aSRandall Stewart 	/* not that we need this */
2789f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
2790f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
2791f8829a4aSRandall Stewart 	    control,
2792cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1,
2793cfde3ff7SRandall Stewart 	    SCTP_READ_LOCK_NOT_HELD,
2794cfde3ff7SRandall Stewart 	    SCTP_SO_NOT_LOCKED);
2795f8829a4aSRandall Stewart }
2796f8829a4aSRandall Stewart 
2797f8829a4aSRandall Stewart 
2798f8829a4aSRandall Stewart static void
27991edc9dbaSMichael Tuexen sctp_notify_send_failed(struct sctp_tcb *stcb, uint8_t sent, uint32_t error,
2800ceaad40aSRandall Stewart     struct sctp_tmit_chunk *chk, int so_locked
2801ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2802ceaad40aSRandall Stewart     SCTP_UNUSED
2803ceaad40aSRandall Stewart #endif
2804ceaad40aSRandall Stewart )
2805f8829a4aSRandall Stewart {
2806830d754dSRandall Stewart 	struct mbuf *m_notify;
2807f8829a4aSRandall Stewart 	struct sctp_send_failed *ssf;
28089935403aSMichael Tuexen 	struct sctp_send_failed_event *ssfe;
2809f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
2810f8829a4aSRandall Stewart 	int length;
2811f8829a4aSRandall Stewart 
281260990c0cSMichael Tuexen 	if ((stcb == NULL) ||
28139935403aSMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
28149935403aSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
2815f8829a4aSRandall Stewart 		/* event not enabled */
2816f8829a4aSRandall Stewart 		return;
2817830d754dSRandall Stewart 	}
28189935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
28199935403aSMichael Tuexen 		length = sizeof(struct sctp_send_failed_event);
28209935403aSMichael Tuexen 	} else {
28219935403aSMichael Tuexen 		length = sizeof(struct sctp_send_failed);
28229935403aSMichael Tuexen 	}
2823eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(length, 0, M_NOWAIT, 1, MT_DATA);
2824f8829a4aSRandall Stewart 	if (m_notify == NULL)
2825f8829a4aSRandall Stewart 		/* no space left */
2826f8829a4aSRandall Stewart 		return;
2827139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
28289935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
28299935403aSMichael Tuexen 		ssfe = mtod(m_notify, struct sctp_send_failed_event *);
2830e432298aSXin LI 		memset(ssfe, 0, length);
28319935403aSMichael Tuexen 		ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
28321edc9dbaSMichael Tuexen 		if (sent) {
28339935403aSMichael Tuexen 			ssfe->ssfe_flags = SCTP_DATA_SENT;
28341edc9dbaSMichael Tuexen 		} else {
28351edc9dbaSMichael Tuexen 			ssfe->ssfe_flags = SCTP_DATA_UNSENT;
28361edc9dbaSMichael Tuexen 		}
2837e432298aSXin LI 		length += chk->send_size;
2838e432298aSXin LI 		length -= sizeof(struct sctp_data_chunk);
28399935403aSMichael Tuexen 		ssfe->ssfe_length = length;
28409935403aSMichael Tuexen 		ssfe->ssfe_error = error;
28419935403aSMichael Tuexen 		/* not exactly what the user sent in, but should be close :) */
28429935403aSMichael Tuexen 		ssfe->ssfe_info.snd_sid = chk->rec.data.stream_number;
28439935403aSMichael Tuexen 		ssfe->ssfe_info.snd_flags = chk->rec.data.rcv_flags;
28449935403aSMichael Tuexen 		ssfe->ssfe_info.snd_ppid = chk->rec.data.payloadtype;
28459935403aSMichael Tuexen 		ssfe->ssfe_info.snd_context = chk->rec.data.context;
28469935403aSMichael Tuexen 		ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
28479935403aSMichael Tuexen 		ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
28489935403aSMichael Tuexen 		SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed_event);
28499935403aSMichael Tuexen 	} else {
2850f8829a4aSRandall Stewart 		ssf = mtod(m_notify, struct sctp_send_failed *);
2851e432298aSXin LI 		memset(ssf, 0, length);
2852f8829a4aSRandall Stewart 		ssf->ssf_type = SCTP_SEND_FAILED;
28531edc9dbaSMichael Tuexen 		if (sent) {
2854f8829a4aSRandall Stewart 			ssf->ssf_flags = SCTP_DATA_SENT;
28551edc9dbaSMichael Tuexen 		} else {
28561edc9dbaSMichael Tuexen 			ssf->ssf_flags = SCTP_DATA_UNSENT;
28571edc9dbaSMichael Tuexen 		}
2858e432298aSXin LI 		length += chk->send_size;
2859e432298aSXin LI 		length -= sizeof(struct sctp_data_chunk);
2860f8829a4aSRandall Stewart 		ssf->ssf_length = length;
2861f8829a4aSRandall Stewart 		ssf->ssf_error = error;
2862f8829a4aSRandall Stewart 		/* not exactly what the user sent in, but should be close :) */
2863d00aff5dSRandall Stewart 		bzero(&ssf->ssf_info, sizeof(ssf->ssf_info));
2864f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_stream = chk->rec.data.stream_number;
2865f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_ssn = chk->rec.data.stream_seq;
2866f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_flags = chk->rec.data.rcv_flags;
2867f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_ppid = chk->rec.data.payloadtype;
2868f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_context = chk->rec.data.context;
2869f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
2870f8829a4aSRandall Stewart 		ssf->ssf_assoc_id = sctp_get_associd(stcb);
28719935403aSMichael Tuexen 		SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed);
28729935403aSMichael Tuexen 	}
2873830d754dSRandall Stewart 	if (chk->data) {
2874830d754dSRandall Stewart 		/*
2875830d754dSRandall Stewart 		 * trim off the sctp chunk header(it should be there)
2876830d754dSRandall Stewart 		 */
2877830d754dSRandall Stewart 		if (chk->send_size >= sizeof(struct sctp_data_chunk)) {
2878830d754dSRandall Stewart 			m_adj(chk->data, sizeof(struct sctp_data_chunk));
2879830d754dSRandall Stewart 			sctp_mbuf_crush(chk->data);
2880830d754dSRandall Stewart 			chk->send_size -= sizeof(struct sctp_data_chunk);
2881830d754dSRandall Stewart 		}
2882830d754dSRandall Stewart 	}
2883810ec536SMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = chk->data;
2884f8829a4aSRandall Stewart 	/* Steal off the mbuf */
2885f8829a4aSRandall Stewart 	chk->data = NULL;
2886f8829a4aSRandall Stewart 	/*
2887f8829a4aSRandall Stewart 	 * For this case, we check the actual socket buffer, since the assoc
2888f8829a4aSRandall Stewart 	 * is going away we don't want to overfill the socket buffer for a
2889f8829a4aSRandall Stewart 	 * non-reader
2890f8829a4aSRandall Stewart 	 */
2891139bc87fSRandall Stewart 	if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
2892f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
2893f8829a4aSRandall Stewart 		return;
2894f8829a4aSRandall Stewart 	}
2895f8829a4aSRandall Stewart 	/* append to socket */
2896f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
28977215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
2898f8829a4aSRandall Stewart 	    m_notify);
2899f8829a4aSRandall Stewart 	if (control == NULL) {
2900f8829a4aSRandall Stewart 		/* no memory */
2901f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
2902f8829a4aSRandall Stewart 		return;
2903f8829a4aSRandall Stewart 	}
2904139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
2905f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
2906f8829a4aSRandall Stewart 	    control,
2907cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1,
2908cfde3ff7SRandall Stewart 	    SCTP_READ_LOCK_NOT_HELD,
2909cfde3ff7SRandall Stewart 	    so_locked);
2910f8829a4aSRandall Stewart }
2911f8829a4aSRandall Stewart 
2912f8829a4aSRandall Stewart 
2913f8829a4aSRandall Stewart static void
2914f8829a4aSRandall Stewart sctp_notify_send_failed2(struct sctp_tcb *stcb, uint32_t error,
2915ceaad40aSRandall Stewart     struct sctp_stream_queue_pending *sp, int so_locked
2916ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2917ceaad40aSRandall Stewart     SCTP_UNUSED
2918ceaad40aSRandall Stewart #endif
2919ceaad40aSRandall Stewart )
2920f8829a4aSRandall Stewart {
2921f8829a4aSRandall Stewart 	struct mbuf *m_notify;
2922f8829a4aSRandall Stewart 	struct sctp_send_failed *ssf;
29239935403aSMichael Tuexen 	struct sctp_send_failed_event *ssfe;
2924f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
2925f8829a4aSRandall Stewart 	int length;
2926f8829a4aSRandall Stewart 
292760990c0cSMichael Tuexen 	if ((stcb == NULL) ||
29289935403aSMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
29299935403aSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
2930f8829a4aSRandall Stewart 		/* event not enabled */
2931f8829a4aSRandall Stewart 		return;
2932830d754dSRandall Stewart 	}
29339935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
29349935403aSMichael Tuexen 		length = sizeof(struct sctp_send_failed_event);
29359935403aSMichael Tuexen 	} else {
29369935403aSMichael Tuexen 		length = sizeof(struct sctp_send_failed);
29379935403aSMichael Tuexen 	}
2938eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(length, 0, M_NOWAIT, 1, MT_DATA);
29399935403aSMichael Tuexen 	if (m_notify == NULL) {
2940f8829a4aSRandall Stewart 		/* no space left */
2941f8829a4aSRandall Stewart 		return;
29429935403aSMichael Tuexen 	}
2943139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
29449935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
29459935403aSMichael Tuexen 		ssfe = mtod(m_notify, struct sctp_send_failed_event *);
2946e432298aSXin LI 		memset(ssfe, 0, length);
2947ad83c8a5SMichael Tuexen 		ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
29489935403aSMichael Tuexen 		ssfe->ssfe_flags = SCTP_DATA_UNSENT;
2949e432298aSXin LI 		length += sp->length;
29509935403aSMichael Tuexen 		ssfe->ssfe_length = length;
29519935403aSMichael Tuexen 		ssfe->ssfe_error = error;
29529935403aSMichael Tuexen 		/* not exactly what the user sent in, but should be close :) */
29539935403aSMichael Tuexen 		ssfe->ssfe_info.snd_sid = sp->stream;
29549935403aSMichael Tuexen 		if (sp->some_taken) {
29559935403aSMichael Tuexen 			ssfe->ssfe_info.snd_flags = SCTP_DATA_LAST_FRAG;
29569935403aSMichael Tuexen 		} else {
29579935403aSMichael Tuexen 			ssfe->ssfe_info.snd_flags = SCTP_DATA_NOT_FRAG;
29589935403aSMichael Tuexen 		}
29599935403aSMichael Tuexen 		ssfe->ssfe_info.snd_ppid = sp->ppid;
29609935403aSMichael Tuexen 		ssfe->ssfe_info.snd_context = sp->context;
29619935403aSMichael Tuexen 		ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
29629935403aSMichael Tuexen 		ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
29639935403aSMichael Tuexen 		SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed_event);
29649935403aSMichael Tuexen 	} else {
2965f8829a4aSRandall Stewart 		ssf = mtod(m_notify, struct sctp_send_failed *);
2966e432298aSXin LI 		memset(ssf, 0, length);
2967f8829a4aSRandall Stewart 		ssf->ssf_type = SCTP_SEND_FAILED;
2968f8829a4aSRandall Stewart 		ssf->ssf_flags = SCTP_DATA_UNSENT;
2969e432298aSXin LI 		length += sp->length;
2970f8829a4aSRandall Stewart 		ssf->ssf_length = length;
2971f8829a4aSRandall Stewart 		ssf->ssf_error = error;
2972f8829a4aSRandall Stewart 		/* not exactly what the user sent in, but should be close :) */
2973f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_stream = sp->stream;
2974f3b05218SMichael Tuexen 		ssf->ssf_info.sinfo_ssn = 0;
2975fc14de76SRandall Stewart 		if (sp->some_taken) {
2976fc14de76SRandall Stewart 			ssf->ssf_info.sinfo_flags = SCTP_DATA_LAST_FRAG;
2977fc14de76SRandall Stewart 		} else {
2978fc14de76SRandall Stewart 			ssf->ssf_info.sinfo_flags = SCTP_DATA_NOT_FRAG;
2979fc14de76SRandall Stewart 		}
2980f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_ppid = sp->ppid;
2981f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_context = sp->context;
2982f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
2983f8829a4aSRandall Stewart 		ssf->ssf_assoc_id = sctp_get_associd(stcb);
2984139bc87fSRandall Stewart 		SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed);
29859935403aSMichael Tuexen 	}
29869935403aSMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = sp->data;
2987f8829a4aSRandall Stewart 
2988f8829a4aSRandall Stewart 	/* Steal off the mbuf */
2989f8829a4aSRandall Stewart 	sp->data = NULL;
2990f8829a4aSRandall Stewart 	/*
2991f8829a4aSRandall Stewart 	 * For this case, we check the actual socket buffer, since the assoc
2992f8829a4aSRandall Stewart 	 * is going away we don't want to overfill the socket buffer for a
2993f8829a4aSRandall Stewart 	 * non-reader
2994f8829a4aSRandall Stewart 	 */
2995139bc87fSRandall Stewart 	if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
2996f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
2997f8829a4aSRandall Stewart 		return;
2998f8829a4aSRandall Stewart 	}
2999f8829a4aSRandall Stewart 	/* append to socket */
3000f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
30017215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3002f8829a4aSRandall Stewart 	    m_notify);
3003f8829a4aSRandall Stewart 	if (control == NULL) {
3004f8829a4aSRandall Stewart 		/* no memory */
3005f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3006f8829a4aSRandall Stewart 		return;
3007f8829a4aSRandall Stewart 	}
3008139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3009f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3010f8829a4aSRandall Stewart 	    control,
3011cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3012f8829a4aSRandall Stewart }
3013f8829a4aSRandall Stewart 
3014f8829a4aSRandall Stewart 
3015f8829a4aSRandall Stewart 
3016f8829a4aSRandall Stewart static void
30177215cc1bSMichael Tuexen sctp_notify_adaptation_layer(struct sctp_tcb *stcb)
3018f8829a4aSRandall Stewart {
3019f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3020f8829a4aSRandall Stewart 	struct sctp_adaptation_event *sai;
3021f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3022f8829a4aSRandall Stewart 
302360990c0cSMichael Tuexen 	if ((stcb == NULL) ||
302460990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT)) {
3025f8829a4aSRandall Stewart 		/* event not enabled */
3026f8829a4aSRandall Stewart 		return;
3027830d754dSRandall Stewart 	}
3028eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 0, M_NOWAIT, 1, MT_DATA);
3029f8829a4aSRandall Stewart 	if (m_notify == NULL)
3030f8829a4aSRandall Stewart 		/* no space left */
3031f8829a4aSRandall Stewart 		return;
3032139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3033f8829a4aSRandall Stewart 	sai = mtod(m_notify, struct sctp_adaptation_event *);
3034e432298aSXin LI 	memset(sai, 0, sizeof(struct sctp_adaptation_event));
3035f8829a4aSRandall Stewart 	sai->sai_type = SCTP_ADAPTATION_INDICATION;
3036f8829a4aSRandall Stewart 	sai->sai_flags = 0;
3037f8829a4aSRandall Stewart 	sai->sai_length = sizeof(struct sctp_adaptation_event);
30382afb3e84SRandall Stewart 	sai->sai_adaptation_ind = stcb->asoc.peers_adaptation;
3039f8829a4aSRandall Stewart 	sai->sai_assoc_id = sctp_get_associd(stcb);
3040f8829a4aSRandall Stewart 
3041139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_adaptation_event);
3042139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3043f8829a4aSRandall Stewart 
3044f8829a4aSRandall Stewart 	/* append to socket */
3045f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
30467215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3047f8829a4aSRandall Stewart 	    m_notify);
3048f8829a4aSRandall Stewart 	if (control == NULL) {
3049f8829a4aSRandall Stewart 		/* no memory */
3050f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3051f8829a4aSRandall Stewart 		return;
3052f8829a4aSRandall Stewart 	}
3053139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3054139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3055f8829a4aSRandall Stewart 	/* not that we need this */
3056f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3057f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3058f8829a4aSRandall Stewart 	    control,
3059cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3060f8829a4aSRandall Stewart }
3061f8829a4aSRandall Stewart 
306203b0b021SRandall Stewart /* This always must be called with the read-queue LOCKED in the INP */
3063810ec536SMichael Tuexen static void
30642dad8a55SRandall Stewart sctp_notify_partial_delivery_indication(struct sctp_tcb *stcb, uint32_t error,
3065810ec536SMichael Tuexen     uint32_t val, int so_locked
3066810ec536SMichael Tuexen #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3067810ec536SMichael Tuexen     SCTP_UNUSED
3068810ec536SMichael Tuexen #endif
3069810ec536SMichael Tuexen )
3070f8829a4aSRandall Stewart {
3071f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3072f8829a4aSRandall Stewart 	struct sctp_pdapi_event *pdapi;
3073f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
307403b0b021SRandall Stewart 	struct sockbuf *sb;
3075f8829a4aSRandall Stewart 
307660990c0cSMichael Tuexen 	if ((stcb == NULL) ||
307760990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_PDAPIEVNT)) {
3078f8829a4aSRandall Stewart 		/* event not enabled */
3079f8829a4aSRandall Stewart 		return;
3080830d754dSRandall Stewart 	}
3081cd1386abSMichael Tuexen 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
3082cd1386abSMichael Tuexen 		return;
3083cd1386abSMichael Tuexen 	}
3084eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_pdapi_event), 0, M_NOWAIT, 1, MT_DATA);
3085f8829a4aSRandall Stewart 	if (m_notify == NULL)
3086f8829a4aSRandall Stewart 		/* no space left */
3087f8829a4aSRandall Stewart 		return;
3088139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3089f8829a4aSRandall Stewart 	pdapi = mtod(m_notify, struct sctp_pdapi_event *);
3090e432298aSXin LI 	memset(pdapi, 0, sizeof(struct sctp_pdapi_event));
3091f8829a4aSRandall Stewart 	pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
3092f8829a4aSRandall Stewart 	pdapi->pdapi_flags = 0;
3093f8829a4aSRandall Stewart 	pdapi->pdapi_length = sizeof(struct sctp_pdapi_event);
3094f8829a4aSRandall Stewart 	pdapi->pdapi_indication = error;
30959a6142d8SRandall Stewart 	pdapi->pdapi_stream = (val >> 16);
30969a6142d8SRandall Stewart 	pdapi->pdapi_seq = (val & 0x0000ffff);
3097f8829a4aSRandall Stewart 	pdapi->pdapi_assoc_id = sctp_get_associd(stcb);
3098f8829a4aSRandall Stewart 
3099139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_pdapi_event);
3100139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3101f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
31027215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3103f8829a4aSRandall Stewart 	    m_notify);
3104f8829a4aSRandall Stewart 	if (control == NULL) {
3105f8829a4aSRandall Stewart 		/* no memory */
3106f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3107f8829a4aSRandall Stewart 		return;
3108f8829a4aSRandall Stewart 	}
3109139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3110139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3111f8829a4aSRandall Stewart 	/* not that we need this */
3112f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
311303b0b021SRandall Stewart 	control->held_length = 0;
311403b0b021SRandall Stewart 	control->length = 0;
311503b0b021SRandall Stewart 	sb = &stcb->sctp_socket->so_rcv;
3116b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3117139bc87fSRandall Stewart 		sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m_notify));
311880fefe0aSRandall Stewart 	}
311903b0b021SRandall Stewart 	sctp_sballoc(stcb, sb, m_notify);
3120b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
312103b0b021SRandall Stewart 		sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
312280fefe0aSRandall Stewart 	}
3123139bc87fSRandall Stewart 	atomic_add_int(&control->length, SCTP_BUF_LEN(m_notify));
312403b0b021SRandall Stewart 	control->end_added = 1;
312503b0b021SRandall Stewart 	if (stcb->asoc.control_pdapi)
312603b0b021SRandall Stewart 		TAILQ_INSERT_AFTER(&stcb->sctp_ep->read_queue, stcb->asoc.control_pdapi, control, next);
312703b0b021SRandall Stewart 	else {
312803b0b021SRandall Stewart 		/* we really should not see this case */
312903b0b021SRandall Stewart 		TAILQ_INSERT_TAIL(&stcb->sctp_ep->read_queue, control, next);
313003b0b021SRandall Stewart 	}
313103b0b021SRandall Stewart 	if (stcb->sctp_ep && stcb->sctp_socket) {
313203b0b021SRandall Stewart 		/* This should always be the case */
3133810ec536SMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3134810ec536SMichael Tuexen 		struct socket *so;
3135810ec536SMichael Tuexen 
3136810ec536SMichael Tuexen 		so = SCTP_INP_SO(stcb->sctp_ep);
3137810ec536SMichael Tuexen 		if (!so_locked) {
3138810ec536SMichael Tuexen 			atomic_add_int(&stcb->asoc.refcnt, 1);
3139810ec536SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
3140810ec536SMichael Tuexen 			SCTP_SOCKET_LOCK(so, 1);
3141810ec536SMichael Tuexen 			SCTP_TCB_LOCK(stcb);
3142810ec536SMichael Tuexen 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
3143810ec536SMichael Tuexen 			if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3144810ec536SMichael Tuexen 				SCTP_SOCKET_UNLOCK(so, 1);
3145810ec536SMichael Tuexen 				return;
3146810ec536SMichael Tuexen 			}
3147810ec536SMichael Tuexen 		}
3148810ec536SMichael Tuexen #endif
314903b0b021SRandall Stewart 		sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
3150810ec536SMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3151810ec536SMichael Tuexen 		if (!so_locked) {
3152810ec536SMichael Tuexen 			SCTP_SOCKET_UNLOCK(so, 1);
3153810ec536SMichael Tuexen 		}
3154810ec536SMichael Tuexen #endif
3155f8829a4aSRandall Stewart 	}
3156f8829a4aSRandall Stewart }
3157f8829a4aSRandall Stewart 
3158f8829a4aSRandall Stewart static void
3159f8829a4aSRandall Stewart sctp_notify_shutdown_event(struct sctp_tcb *stcb)
3160f8829a4aSRandall Stewart {
3161f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3162f8829a4aSRandall Stewart 	struct sctp_shutdown_event *sse;
3163f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3164f8829a4aSRandall Stewart 
3165f8829a4aSRandall Stewart 	/*
3166f8829a4aSRandall Stewart 	 * For TCP model AND UDP connected sockets we will send an error up
3167f8829a4aSRandall Stewart 	 * when an SHUTDOWN completes
3168f8829a4aSRandall Stewart 	 */
3169f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3170f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3171f8829a4aSRandall Stewart 		/* mark socket closed for read/write and wakeup! */
3172ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3173ceaad40aSRandall Stewart 		struct socket *so;
3174ceaad40aSRandall Stewart 
3175ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
3176ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
3177ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3178ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
3179ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
3180ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
3181ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3182ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
3183ceaad40aSRandall Stewart 			return;
3184ceaad40aSRandall Stewart 		}
3185ceaad40aSRandall Stewart #endif
3186f8829a4aSRandall Stewart 		socantsendmore(stcb->sctp_socket);
3187ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3188ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
3189ceaad40aSRandall Stewart #endif
3190f8829a4aSRandall Stewart 	}
3191e2e7c62eSMichael Tuexen 	if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)) {
3192f8829a4aSRandall Stewart 		/* event not enabled */
3193f8829a4aSRandall Stewart 		return;
3194830d754dSRandall Stewart 	}
3195eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_event), 0, M_NOWAIT, 1, MT_DATA);
3196f8829a4aSRandall Stewart 	if (m_notify == NULL)
3197f8829a4aSRandall Stewart 		/* no space left */
3198f8829a4aSRandall Stewart 		return;
3199f8829a4aSRandall Stewart 	sse = mtod(m_notify, struct sctp_shutdown_event *);
3200e432298aSXin LI 	memset(sse, 0, sizeof(struct sctp_shutdown_event));
3201f8829a4aSRandall Stewart 	sse->sse_type = SCTP_SHUTDOWN_EVENT;
3202f8829a4aSRandall Stewart 	sse->sse_flags = 0;
3203f8829a4aSRandall Stewart 	sse->sse_length = sizeof(struct sctp_shutdown_event);
3204f8829a4aSRandall Stewart 	sse->sse_assoc_id = sctp_get_associd(stcb);
3205f8829a4aSRandall Stewart 
3206139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_shutdown_event);
3207139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3208f8829a4aSRandall Stewart 
3209f8829a4aSRandall Stewart 	/* append to socket */
3210f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
32117215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3212f8829a4aSRandall Stewart 	    m_notify);
3213f8829a4aSRandall Stewart 	if (control == NULL) {
3214f8829a4aSRandall Stewart 		/* no memory */
3215f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3216f8829a4aSRandall Stewart 		return;
3217f8829a4aSRandall Stewart 	}
3218139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3219139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3220f8829a4aSRandall Stewart 	/* not that we need this */
3221f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3222f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3223f8829a4aSRandall Stewart 	    control,
3224cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3225f8829a4aSRandall Stewart }
3226f8829a4aSRandall Stewart 
3227f8829a4aSRandall Stewart static void
3228830d754dSRandall Stewart sctp_notify_sender_dry_event(struct sctp_tcb *stcb,
3229830d754dSRandall Stewart     int so_locked
3230830d754dSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3231830d754dSRandall Stewart     SCTP_UNUSED
3232830d754dSRandall Stewart #endif
3233830d754dSRandall Stewart )
3234830d754dSRandall Stewart {
3235830d754dSRandall Stewart 	struct mbuf *m_notify;
3236830d754dSRandall Stewart 	struct sctp_sender_dry_event *event;
3237830d754dSRandall Stewart 	struct sctp_queued_to_read *control;
3238830d754dSRandall Stewart 
323960990c0cSMichael Tuexen 	if ((stcb == NULL) ||
324060990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DRYEVNT)) {
3241830d754dSRandall Stewart 		/* event not enabled */
3242830d754dSRandall Stewart 		return;
3243830d754dSRandall Stewart 	}
3244eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_sender_dry_event), 0, M_NOWAIT, 1, MT_DATA);
3245830d754dSRandall Stewart 	if (m_notify == NULL) {
3246830d754dSRandall Stewart 		/* no space left */
3247830d754dSRandall Stewart 		return;
3248830d754dSRandall Stewart 	}
3249830d754dSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3250830d754dSRandall Stewart 	event = mtod(m_notify, struct sctp_sender_dry_event *);
3251e432298aSXin LI 	memset(event, 0, sizeof(struct sctp_sender_dry_event));
3252830d754dSRandall Stewart 	event->sender_dry_type = SCTP_SENDER_DRY_EVENT;
3253830d754dSRandall Stewart 	event->sender_dry_flags = 0;
3254830d754dSRandall Stewart 	event->sender_dry_length = sizeof(struct sctp_sender_dry_event);
3255830d754dSRandall Stewart 	event->sender_dry_assoc_id = sctp_get_associd(stcb);
3256830d754dSRandall Stewart 
3257830d754dSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_sender_dry_event);
3258830d754dSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3259830d754dSRandall Stewart 
3260830d754dSRandall Stewart 	/* append to socket */
3261830d754dSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
32627215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
32637215cc1bSMichael Tuexen 	    m_notify);
3264830d754dSRandall Stewart 	if (control == NULL) {
3265830d754dSRandall Stewart 		/* no memory */
3266830d754dSRandall Stewart 		sctp_m_freem(m_notify);
3267830d754dSRandall Stewart 		return;
3268830d754dSRandall Stewart 	}
3269830d754dSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3270830d754dSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3271830d754dSRandall Stewart 	/* not that we need this */
3272830d754dSRandall Stewart 	control->tail_mbuf = m_notify;
3273830d754dSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3274cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3275830d754dSRandall Stewart }
3276830d754dSRandall Stewart 
3277ea44232bSRandall Stewart 
3278c4e848b7SRandall Stewart void
3279c4e848b7SRandall Stewart sctp_notify_stream_reset_add(struct sctp_tcb *stcb, uint16_t numberin, uint16_t numberout, int flag)
3280ea44232bSRandall Stewart {
3281ea44232bSRandall Stewart 	struct mbuf *m_notify;
3282ea44232bSRandall Stewart 	struct sctp_queued_to_read *control;
3283c4e848b7SRandall Stewart 	struct sctp_stream_change_event *stradd;
3284ea44232bSRandall Stewart 
32858c501e51SMichael Tuexen 	if ((stcb == NULL) ||
32868c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_CHANGEEVNT))) {
3287ea44232bSRandall Stewart 		/* event not enabled */
3288ea44232bSRandall Stewart 		return;
3289ea44232bSRandall Stewart 	}
3290c4e848b7SRandall Stewart 	if ((stcb->asoc.peer_req_out) && flag) {
3291c4e848b7SRandall Stewart 		/* Peer made the request, don't tell the local user */
3292c4e848b7SRandall Stewart 		stcb->asoc.peer_req_out = 0;
3293c4e848b7SRandall Stewart 		return;
3294c4e848b7SRandall Stewart 	}
3295c4e848b7SRandall Stewart 	stcb->asoc.peer_req_out = 0;
3296e432298aSXin LI 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_stream_change_event), 0, M_NOWAIT, 1, MT_DATA);
3297ea44232bSRandall Stewart 	if (m_notify == NULL)
3298ea44232bSRandall Stewart 		/* no space left */
3299ea44232bSRandall Stewart 		return;
3300ea44232bSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3301c4e848b7SRandall Stewart 	stradd = mtod(m_notify, struct sctp_stream_change_event *);
3302e432298aSXin LI 	memset(stradd, 0, sizeof(struct sctp_stream_change_event));
3303c4e848b7SRandall Stewart 	stradd->strchange_type = SCTP_STREAM_CHANGE_EVENT;
3304c4e848b7SRandall Stewart 	stradd->strchange_flags = flag;
3305e432298aSXin LI 	stradd->strchange_length = sizeof(struct sctp_stream_change_event);
3306c4e848b7SRandall Stewart 	stradd->strchange_assoc_id = sctp_get_associd(stcb);
3307c4e848b7SRandall Stewart 	stradd->strchange_instrms = numberin;
3308c4e848b7SRandall Stewart 	stradd->strchange_outstrms = numberout;
3309e432298aSXin LI 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_stream_change_event);
3310ea44232bSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3311ea44232bSRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3312ea44232bSRandall Stewart 		/* no space */
3313ea44232bSRandall Stewart 		sctp_m_freem(m_notify);
3314ea44232bSRandall Stewart 		return;
3315ea44232bSRandall Stewart 	}
3316ea44232bSRandall Stewart 	/* append to socket */
3317ea44232bSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
33187215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3319ea44232bSRandall Stewart 	    m_notify);
3320ea44232bSRandall Stewart 	if (control == NULL) {
3321ea44232bSRandall Stewart 		/* no memory */
3322ea44232bSRandall Stewart 		sctp_m_freem(m_notify);
3323ea44232bSRandall Stewart 		return;
3324ea44232bSRandall Stewart 	}
3325ea44232bSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3326ea44232bSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3327ea44232bSRandall Stewart 	/* not that we need this */
3328ea44232bSRandall Stewart 	control->tail_mbuf = m_notify;
3329ea44232bSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3330ea44232bSRandall Stewart 	    control,
3331cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3332ea44232bSRandall Stewart }
3333ea44232bSRandall Stewart 
3334c4e848b7SRandall Stewart void
3335c4e848b7SRandall Stewart sctp_notify_stream_reset_tsn(struct sctp_tcb *stcb, uint32_t sending_tsn, uint32_t recv_tsn, int flag)
3336c4e848b7SRandall Stewart {
3337c4e848b7SRandall Stewart 	struct mbuf *m_notify;
3338c4e848b7SRandall Stewart 	struct sctp_queued_to_read *control;
3339c4e848b7SRandall Stewart 	struct sctp_assoc_reset_event *strasoc;
3340c4e848b7SRandall Stewart 
33418c501e51SMichael Tuexen 	if ((stcb == NULL) ||
33428c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ASSOC_RESETEVNT))) {
3343c4e848b7SRandall Stewart 		/* event not enabled */
3344c4e848b7SRandall Stewart 		return;
3345c4e848b7SRandall Stewart 	}
3346e432298aSXin LI 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_assoc_reset_event), 0, M_NOWAIT, 1, MT_DATA);
3347c4e848b7SRandall Stewart 	if (m_notify == NULL)
3348c4e848b7SRandall Stewart 		/* no space left */
3349c4e848b7SRandall Stewart 		return;
3350c4e848b7SRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3351c4e848b7SRandall Stewart 	strasoc = mtod(m_notify, struct sctp_assoc_reset_event *);
3352e432298aSXin LI 	memset(strasoc, 0, sizeof(struct sctp_assoc_reset_event));
3353c4e848b7SRandall Stewart 	strasoc->assocreset_type = SCTP_ASSOC_RESET_EVENT;
3354c4e848b7SRandall Stewart 	strasoc->assocreset_flags = flag;
3355e432298aSXin LI 	strasoc->assocreset_length = sizeof(struct sctp_assoc_reset_event);
3356c4e848b7SRandall Stewart 	strasoc->assocreset_assoc_id = sctp_get_associd(stcb);
3357c4e848b7SRandall Stewart 	strasoc->assocreset_local_tsn = sending_tsn;
3358c4e848b7SRandall Stewart 	strasoc->assocreset_remote_tsn = recv_tsn;
3359e432298aSXin LI 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_assoc_reset_event);
3360c4e848b7SRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3361c4e848b7SRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3362c4e848b7SRandall Stewart 		/* no space */
3363c4e848b7SRandall Stewart 		sctp_m_freem(m_notify);
3364c4e848b7SRandall Stewart 		return;
3365c4e848b7SRandall Stewart 	}
3366c4e848b7SRandall Stewart 	/* append to socket */
3367c4e848b7SRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3368c4e848b7SRandall Stewart 	    0, 0, stcb->asoc.context, 0, 0, 0,
3369c4e848b7SRandall Stewart 	    m_notify);
3370c4e848b7SRandall Stewart 	if (control == NULL) {
3371c4e848b7SRandall Stewart 		/* no memory */
3372c4e848b7SRandall Stewart 		sctp_m_freem(m_notify);
3373c4e848b7SRandall Stewart 		return;
3374c4e848b7SRandall Stewart 	}
3375c4e848b7SRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3376c4e848b7SRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3377c4e848b7SRandall Stewart 	/* not that we need this */
3378c4e848b7SRandall Stewart 	control->tail_mbuf = m_notify;
3379c4e848b7SRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3380c4e848b7SRandall Stewart 	    control,
3381c4e848b7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3382c4e848b7SRandall Stewart }
3383c4e848b7SRandall Stewart 
3384c4e848b7SRandall Stewart 
3385ea44232bSRandall Stewart 
3386830d754dSRandall Stewart static void
3387f8829a4aSRandall Stewart sctp_notify_stream_reset(struct sctp_tcb *stcb,
3388f8829a4aSRandall Stewart     int number_entries, uint16_t * list, int flag)
3389f8829a4aSRandall Stewart {
3390f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3391f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3392f8829a4aSRandall Stewart 	struct sctp_stream_reset_event *strreset;
3393f8829a4aSRandall Stewart 	int len;
3394f8829a4aSRandall Stewart 
33958c501e51SMichael Tuexen 	if ((stcb == NULL) ||
33968c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT))) {
3397f8829a4aSRandall Stewart 		/* event not enabled */
3398f8829a4aSRandall Stewart 		return;
3399830d754dSRandall Stewart 	}
3400eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
3401f8829a4aSRandall Stewart 	if (m_notify == NULL)
3402f8829a4aSRandall Stewart 		/* no space left */
3403f8829a4aSRandall Stewart 		return;
3404139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3405f8829a4aSRandall Stewart 	len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
3406f8829a4aSRandall Stewart 	if (len > M_TRAILINGSPACE(m_notify)) {
3407f8829a4aSRandall Stewart 		/* never enough room */
3408f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3409f8829a4aSRandall Stewart 		return;
3410f8829a4aSRandall Stewart 	}
3411f8829a4aSRandall Stewart 	strreset = mtod(m_notify, struct sctp_stream_reset_event *);
3412e432298aSXin LI 	memset(strreset, 0, len);
3413f8829a4aSRandall Stewart 	strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
3414c4e848b7SRandall Stewart 	strreset->strreset_flags = flag;
3415f8829a4aSRandall Stewart 	strreset->strreset_length = len;
3416f8829a4aSRandall Stewart 	strreset->strreset_assoc_id = sctp_get_associd(stcb);
3417f8829a4aSRandall Stewart 	if (number_entries) {
3418f8829a4aSRandall Stewart 		int i;
3419f8829a4aSRandall Stewart 
3420f8829a4aSRandall Stewart 		for (i = 0; i < number_entries; i++) {
3421c4e848b7SRandall Stewart 			strreset->strreset_stream_list[i] = ntohs(list[i]);
3422f8829a4aSRandall Stewart 		}
3423f8829a4aSRandall Stewart 	}
3424139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = len;
3425139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3426139bc87fSRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3427f8829a4aSRandall Stewart 		/* no space */
3428f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3429f8829a4aSRandall Stewart 		return;
3430f8829a4aSRandall Stewart 	}
3431f8829a4aSRandall Stewart 	/* append to socket */
3432f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
34337215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3434f8829a4aSRandall Stewart 	    m_notify);
3435f8829a4aSRandall Stewart 	if (control == NULL) {
3436f8829a4aSRandall Stewart 		/* no memory */
3437f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3438f8829a4aSRandall Stewart 		return;
3439f8829a4aSRandall Stewart 	}
3440139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3441139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3442f8829a4aSRandall Stewart 	/* not that we need this */
3443f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3444f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3445f8829a4aSRandall Stewart 	    control,
3446cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3447f8829a4aSRandall Stewart }
3448f8829a4aSRandall Stewart 
3449f8829a4aSRandall Stewart 
3450389b1b11SMichael Tuexen static void
3451389b1b11SMichael Tuexen sctp_notify_remote_error(struct sctp_tcb *stcb, uint16_t error, struct sctp_error_chunk *chunk)
3452389b1b11SMichael Tuexen {
3453389b1b11SMichael Tuexen 	struct mbuf *m_notify;
3454389b1b11SMichael Tuexen 	struct sctp_remote_error *sre;
3455389b1b11SMichael Tuexen 	struct sctp_queued_to_read *control;
3456389b1b11SMichael Tuexen 	size_t notif_len, chunk_len;
3457389b1b11SMichael Tuexen 
3458389b1b11SMichael Tuexen 	if ((stcb == NULL) ||
3459389b1b11SMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPEERERR)) {
3460389b1b11SMichael Tuexen 		return;
3461389b1b11SMichael Tuexen 	}
3462389b1b11SMichael Tuexen 	if (chunk != NULL) {
3463c9eb4473SMichael Tuexen 		chunk_len = ntohs(chunk->ch.chunk_length);
3464389b1b11SMichael Tuexen 	} else {
3465389b1b11SMichael Tuexen 		chunk_len = 0;
3466389b1b11SMichael Tuexen 	}
3467389b1b11SMichael Tuexen 	notif_len = sizeof(struct sctp_remote_error) + chunk_len;
3468eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3469389b1b11SMichael Tuexen 	if (m_notify == NULL) {
3470389b1b11SMichael Tuexen 		/* Retry with smaller value. */
3471389b1b11SMichael Tuexen 		notif_len = sizeof(struct sctp_remote_error);
3472eb1b1807SGleb Smirnoff 		m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3473389b1b11SMichael Tuexen 		if (m_notify == NULL) {
3474389b1b11SMichael Tuexen 			return;
3475389b1b11SMichael Tuexen 		}
3476389b1b11SMichael Tuexen 	}
3477389b1b11SMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = NULL;
3478389b1b11SMichael Tuexen 	sre = mtod(m_notify, struct sctp_remote_error *);
347956711f94SMichael Tuexen 	memset(sre, 0, notif_len);
3480389b1b11SMichael Tuexen 	sre->sre_type = SCTP_REMOTE_ERROR;
3481389b1b11SMichael Tuexen 	sre->sre_flags = 0;
3482389b1b11SMichael Tuexen 	sre->sre_length = sizeof(struct sctp_remote_error);
3483389b1b11SMichael Tuexen 	sre->sre_error = error;
3484389b1b11SMichael Tuexen 	sre->sre_assoc_id = sctp_get_associd(stcb);
3485389b1b11SMichael Tuexen 	if (notif_len > sizeof(struct sctp_remote_error)) {
3486389b1b11SMichael Tuexen 		memcpy(sre->sre_data, chunk, chunk_len);
3487389b1b11SMichael Tuexen 		sre->sre_length += chunk_len;
3488389b1b11SMichael Tuexen 	}
3489389b1b11SMichael Tuexen 	SCTP_BUF_LEN(m_notify) = sre->sre_length;
3490389b1b11SMichael Tuexen 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3491389b1b11SMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3492389b1b11SMichael Tuexen 	    m_notify);
3493389b1b11SMichael Tuexen 	if (control != NULL) {
3494389b1b11SMichael Tuexen 		control->length = SCTP_BUF_LEN(m_notify);
3495389b1b11SMichael Tuexen 		/* not that we need this */
3496389b1b11SMichael Tuexen 		control->tail_mbuf = m_notify;
3497389b1b11SMichael Tuexen 		control->spec_flags = M_NOTIFICATION;
3498389b1b11SMichael Tuexen 		sctp_add_to_readq(stcb->sctp_ep, stcb,
3499389b1b11SMichael Tuexen 		    control,
3500389b1b11SMichael Tuexen 		    &stcb->sctp_socket->so_rcv, 1,
3501389b1b11SMichael Tuexen 		    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3502389b1b11SMichael Tuexen 	} else {
3503389b1b11SMichael Tuexen 		sctp_m_freem(m_notify);
3504389b1b11SMichael Tuexen 	}
3505389b1b11SMichael Tuexen }
3506389b1b11SMichael Tuexen 
3507389b1b11SMichael Tuexen 
3508f8829a4aSRandall Stewart void
3509f8829a4aSRandall Stewart sctp_ulp_notify(uint32_t notification, struct sctp_tcb *stcb,
3510ceaad40aSRandall Stewart     uint32_t error, void *data, int so_locked
3511ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3512ceaad40aSRandall Stewart     SCTP_UNUSED
3513ceaad40aSRandall Stewart #endif
3514ceaad40aSRandall Stewart )
3515f8829a4aSRandall Stewart {
3516830d754dSRandall Stewart 	if ((stcb == NULL) ||
3517830d754dSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3518f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3519830d754dSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3520830d754dSRandall Stewart 		/* If the socket is gone we are out of here */
3521f8829a4aSRandall Stewart 		return;
3522f8829a4aSRandall Stewart 	}
3523a99b6783SRandall Stewart 	if (stcb->sctp_socket->so_rcv.sb_state & SBS_CANTRCVMORE) {
3524a99b6783SRandall Stewart 		return;
3525a99b6783SRandall Stewart 	}
3526fb4a67d2SMichael Tuexen 	if ((stcb->asoc.state & SCTP_STATE_COOKIE_WAIT) ||
3527fb4a67d2SMichael Tuexen 	    (stcb->asoc.state & SCTP_STATE_COOKIE_ECHOED)) {
352817205eccSRandall Stewart 		if ((notification == SCTP_NOTIFY_INTERFACE_DOWN) ||
352917205eccSRandall Stewart 		    (notification == SCTP_NOTIFY_INTERFACE_UP) ||
353017205eccSRandall Stewart 		    (notification == SCTP_NOTIFY_INTERFACE_CONFIRMED)) {
353117205eccSRandall Stewart 			/* Don't report these in front states */
353217205eccSRandall Stewart 			return;
353317205eccSRandall Stewart 		}
353417205eccSRandall Stewart 	}
3535f8829a4aSRandall Stewart 	switch (notification) {
3536f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_UP:
3537f8829a4aSRandall Stewart 		if (stcb->asoc.assoc_up_sent == 0) {
3538410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_UP, stcb, error, NULL, 0, so_locked);
3539f8829a4aSRandall Stewart 			stcb->asoc.assoc_up_sent = 1;
3540f8829a4aSRandall Stewart 		}
35412afb3e84SRandall Stewart 		if (stcb->asoc.adaptation_needed && (stcb->asoc.adaptation_sent == 0)) {
35427215cc1bSMichael Tuexen 			sctp_notify_adaptation_layer(stcb);
35432afb3e84SRandall Stewart 		}
3544830d754dSRandall Stewart 		if (stcb->asoc.peer_supports_auth == 0) {
3545830d754dSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3546830d754dSRandall Stewart 			    NULL, so_locked);
3547830d754dSRandall Stewart 		}
3548f8829a4aSRandall Stewart 		break;
3549f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_DOWN:
3550410a3b1eSMichael Tuexen 		sctp_notify_assoc_change(SCTP_SHUTDOWN_COMP, stcb, error, NULL, 0, so_locked);
3551f8829a4aSRandall Stewart 		break;
3552f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_DOWN:
3553f8829a4aSRandall Stewart 		{
3554f8829a4aSRandall Stewart 			struct sctp_nets *net;
3555f8829a4aSRandall Stewart 
3556f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
3557f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_UNREACHABLE,
3558f8829a4aSRandall Stewart 			    (struct sockaddr *)&net->ro._l_addr, error);
3559f8829a4aSRandall Stewart 			break;
3560f8829a4aSRandall Stewart 		}
3561f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_UP:
3562f8829a4aSRandall Stewart 		{
3563f8829a4aSRandall Stewart 			struct sctp_nets *net;
3564f8829a4aSRandall Stewart 
3565f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
3566f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_AVAILABLE,
3567f8829a4aSRandall Stewart 			    (struct sockaddr *)&net->ro._l_addr, error);
3568f8829a4aSRandall Stewart 			break;
3569f8829a4aSRandall Stewart 		}
3570f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_CONFIRMED:
3571f8829a4aSRandall Stewart 		{
3572f8829a4aSRandall Stewart 			struct sctp_nets *net;
3573f8829a4aSRandall Stewart 
3574f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
3575f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_CONFIRMED,
3576f8829a4aSRandall Stewart 			    (struct sockaddr *)&net->ro._l_addr, error);
3577f8829a4aSRandall Stewart 			break;
3578f8829a4aSRandall Stewart 		}
3579f8829a4aSRandall Stewart 	case SCTP_NOTIFY_SPECIAL_SP_FAIL:
3580f8829a4aSRandall Stewart 		sctp_notify_send_failed2(stcb, error,
3581ceaad40aSRandall Stewart 		    (struct sctp_stream_queue_pending *)data, so_locked);
3582f8829a4aSRandall Stewart 		break;
35831edc9dbaSMichael Tuexen 	case SCTP_NOTIFY_SENT_DG_FAIL:
35841edc9dbaSMichael Tuexen 		sctp_notify_send_failed(stcb, 1, error,
35851edc9dbaSMichael Tuexen 		    (struct sctp_tmit_chunk *)data, so_locked);
35861edc9dbaSMichael Tuexen 		break;
35871edc9dbaSMichael Tuexen 	case SCTP_NOTIFY_UNSENT_DG_FAIL:
35881edc9dbaSMichael Tuexen 		sctp_notify_send_failed(stcb, 0, error,
3589ceaad40aSRandall Stewart 		    (struct sctp_tmit_chunk *)data, so_locked);
3590f8829a4aSRandall Stewart 		break;
3591f8829a4aSRandall Stewart 	case SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION:
35929a6142d8SRandall Stewart 		{
35939a6142d8SRandall Stewart 			uint32_t val;
35949a6142d8SRandall Stewart 
35959a6142d8SRandall Stewart 			val = *((uint32_t *) data);
35969a6142d8SRandall Stewart 
3597810ec536SMichael Tuexen 			sctp_notify_partial_delivery_indication(stcb, error, val, so_locked);
3598f8829a4aSRandall Stewart 			break;
3599810ec536SMichael Tuexen 		}
3600410a3b1eSMichael Tuexen 	case SCTP_NOTIFY_ASSOC_LOC_ABORTED:
3601fb4a67d2SMichael Tuexen 		if (((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_WAIT) ||
3602fb4a67d2SMichael Tuexen 		    ((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_ECHOED)) {
3603410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 0, so_locked);
3604c105859eSRandall Stewart 		} else {
3605410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 0, so_locked);
3606410a3b1eSMichael Tuexen 		}
3607410a3b1eSMichael Tuexen 		break;
3608410a3b1eSMichael Tuexen 	case SCTP_NOTIFY_ASSOC_REM_ABORTED:
3609fb4a67d2SMichael Tuexen 		if (((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_WAIT) ||
3610fb4a67d2SMichael Tuexen 		    ((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_ECHOED)) {
3611410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 1, so_locked);
3612410a3b1eSMichael Tuexen 		} else {
3613410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 1, so_locked);
3614c105859eSRandall Stewart 		}
3615f8829a4aSRandall Stewart 		break;
3616f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_RESTART:
3617410a3b1eSMichael Tuexen 		sctp_notify_assoc_change(SCTP_RESTART, stcb, error, NULL, 0, so_locked);
3618830d754dSRandall Stewart 		if (stcb->asoc.peer_supports_auth == 0) {
3619830d754dSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3620830d754dSRandall Stewart 			    NULL, so_locked);
3621830d754dSRandall Stewart 		}
3622f8829a4aSRandall Stewart 		break;
3623f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_SEND:
3624d7714577SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STREAM_RESET_OUTGOING_SSN);
3625f8829a4aSRandall Stewart 		break;
3626f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_RECV:
3627d7714577SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STREAM_RESET_INCOMING);
3628f8829a4aSRandall Stewart 		break;
3629f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_FAILED_OUT:
3630c4e848b7SRandall Stewart 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
3631d7714577SMichael Tuexen 		    (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_FAILED));
3632f8829a4aSRandall Stewart 		break;
3633d4260646SMichael Tuexen 	case SCTP_NOTIFY_STR_RESET_DENIED_OUT:
3634d4260646SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
3635d4260646SMichael Tuexen 		    (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_DENIED));
3636d4260646SMichael Tuexen 		break;
3637f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_FAILED_IN:
3638c4e848b7SRandall Stewart 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
3639d7714577SMichael Tuexen 		    (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_FAILED));
3640f8829a4aSRandall Stewart 		break;
3641d4260646SMichael Tuexen 	case SCTP_NOTIFY_STR_RESET_DENIED_IN:
3642d4260646SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
3643d4260646SMichael Tuexen 		    (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_DENIED));
3644d4260646SMichael Tuexen 		break;
3645f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_ADD_IP:
3646f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_ADDED, data,
3647f8829a4aSRandall Stewart 		    error);
3648f8829a4aSRandall Stewart 		break;
3649f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_DELETE_IP:
3650f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_REMOVED, data,
3651f8829a4aSRandall Stewart 		    error);
3652f8829a4aSRandall Stewart 		break;
3653f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_SET_PRIMARY:
3654f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_MADE_PRIM, data,
3655f8829a4aSRandall Stewart 		    error);
3656f8829a4aSRandall Stewart 		break;
3657f8829a4aSRandall Stewart 	case SCTP_NOTIFY_PEER_SHUTDOWN:
3658f8829a4aSRandall Stewart 		sctp_notify_shutdown_event(stcb);
3659f8829a4aSRandall Stewart 		break;
3660f8829a4aSRandall Stewart 	case SCTP_NOTIFY_AUTH_NEW_KEY:
366178f28045SMichael Tuexen 		sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY, error,
3662830d754dSRandall Stewart 		    (uint16_t) (uintptr_t) data,
3663830d754dSRandall Stewart 		    so_locked);
3664f8829a4aSRandall Stewart 		break;
3665830d754dSRandall Stewart 	case SCTP_NOTIFY_AUTH_FREE_KEY:
3666830d754dSRandall Stewart 		sctp_notify_authentication(stcb, SCTP_AUTH_FREE_KEY, error,
3667830d754dSRandall Stewart 		    (uint16_t) (uintptr_t) data,
3668830d754dSRandall Stewart 		    so_locked);
3669f8829a4aSRandall Stewart 		break;
3670830d754dSRandall Stewart 	case SCTP_NOTIFY_NO_PEER_AUTH:
3671830d754dSRandall Stewart 		sctp_notify_authentication(stcb, SCTP_AUTH_NO_AUTH, error,
3672830d754dSRandall Stewart 		    (uint16_t) (uintptr_t) data,
3673830d754dSRandall Stewart 		    so_locked);
3674830d754dSRandall Stewart 		break;
3675830d754dSRandall Stewart 	case SCTP_NOTIFY_SENDER_DRY:
3676830d754dSRandall Stewart 		sctp_notify_sender_dry_event(stcb, so_locked);
3677830d754dSRandall Stewart 		break;
3678389b1b11SMichael Tuexen 	case SCTP_NOTIFY_REMOTE_ERROR:
3679389b1b11SMichael Tuexen 		sctp_notify_remote_error(stcb, error, data);
3680389b1b11SMichael Tuexen 		break;
3681f8829a4aSRandall Stewart 	default:
3682ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_UTIL1, "%s: unknown notification %xh (%u)\n",
3683ad81507eSRandall Stewart 		    __FUNCTION__, notification, notification);
3684f8829a4aSRandall Stewart 		break;
3685f8829a4aSRandall Stewart 	}			/* end switch */
3686f8829a4aSRandall Stewart }
3687f8829a4aSRandall Stewart 
3688f8829a4aSRandall Stewart void
36891edc9dbaSMichael Tuexen sctp_report_all_outbound(struct sctp_tcb *stcb, uint16_t error, int holds_lock, int so_locked
3690ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3691ceaad40aSRandall Stewart     SCTP_UNUSED
3692ceaad40aSRandall Stewart #endif
3693ceaad40aSRandall Stewart )
3694f8829a4aSRandall Stewart {
3695f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3696f8829a4aSRandall Stewart 	struct sctp_stream_out *outs;
36974a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
36984a9ef3f8SMichael Tuexen 	struct sctp_stream_queue_pending *sp, *nsp;
36997f34832bSRandall Stewart 	int i;
3700f8829a4aSRandall Stewart 
3701ad81507eSRandall Stewart 	if (stcb == NULL) {
3702ad81507eSRandall Stewart 		return;
3703ad81507eSRandall Stewart 	}
37044a9ef3f8SMichael Tuexen 	asoc = &stcb->asoc;
37054a9ef3f8SMichael Tuexen 	if (asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3706478fbccbSRandall Stewart 		/* already being freed */
3707478fbccbSRandall Stewart 		return;
3708478fbccbSRandall Stewart 	}
3709f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3710f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
37114a9ef3f8SMichael Tuexen 	    (asoc->state & SCTP_STATE_CLOSED_SOCKET)) {
3712f8829a4aSRandall Stewart 		return;
3713f8829a4aSRandall Stewart 	}
3714f8829a4aSRandall Stewart 	/* now through all the gunk freeing chunks */
3715ad81507eSRandall Stewart 	if (holds_lock == 0) {
37167f34832bSRandall Stewart 		SCTP_TCB_SEND_LOCK(stcb);
3717ad81507eSRandall Stewart 	}
3718d00aff5dSRandall Stewart 	/* sent queue SHOULD be empty */
37194a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
3720d00aff5dSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3721d00aff5dSRandall Stewart 		asoc->sent_queue_cnt--;
3722325c8c46SMichael Tuexen 		if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
3723a7ad6026SMichael Tuexen 			if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) {
3724a7ad6026SMichael Tuexen 				asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--;
3725a7ad6026SMichael Tuexen #ifdef INVARIANTS
3726a7ad6026SMichael Tuexen 			} else {
3727a7ad6026SMichael Tuexen 				panic("No chunks on the queues for sid %u.", chk->rec.data.stream_number);
3728a7ad6026SMichael Tuexen #endif
3729a7ad6026SMichael Tuexen 			}
3730a7ad6026SMichael Tuexen 		}
37310c0982b8SRandall Stewart 		if (chk->data != NULL) {
3732d00aff5dSRandall Stewart 			sctp_free_bufspace(stcb, asoc, chk, 1);
37331edc9dbaSMichael Tuexen 			sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
37341edc9dbaSMichael Tuexen 			    error, chk, so_locked);
3735810ec536SMichael Tuexen 			if (chk->data) {
3736d00aff5dSRandall Stewart 				sctp_m_freem(chk->data);
3737d00aff5dSRandall Stewart 				chk->data = NULL;
3738d00aff5dSRandall Stewart 			}
3739810ec536SMichael Tuexen 		}
3740689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, so_locked);
3741d00aff5dSRandall Stewart 		/* sa_ignore FREED_MEMORY */
3742d00aff5dSRandall Stewart 	}
3743d00aff5dSRandall Stewart 	/* pending send queue SHOULD be empty */
37444a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
3745d00aff5dSRandall Stewart 		TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3746d00aff5dSRandall Stewart 		asoc->send_queue_cnt--;
3747a7ad6026SMichael Tuexen 		if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) {
3748a7ad6026SMichael Tuexen 			asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--;
3749a7ad6026SMichael Tuexen #ifdef INVARIANTS
3750a7ad6026SMichael Tuexen 		} else {
3751a7ad6026SMichael Tuexen 			panic("No chunks on the queues for sid %u.", chk->rec.data.stream_number);
3752a7ad6026SMichael Tuexen #endif
3753a7ad6026SMichael Tuexen 		}
37540c0982b8SRandall Stewart 		if (chk->data != NULL) {
3755d00aff5dSRandall Stewart 			sctp_free_bufspace(stcb, asoc, chk, 1);
37561edc9dbaSMichael Tuexen 			sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
37571edc9dbaSMichael Tuexen 			    error, chk, so_locked);
3758810ec536SMichael Tuexen 			if (chk->data) {
3759d00aff5dSRandall Stewart 				sctp_m_freem(chk->data);
3760d00aff5dSRandall Stewart 				chk->data = NULL;
3761d00aff5dSRandall Stewart 			}
3762810ec536SMichael Tuexen 		}
3763689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, so_locked);
3764d00aff5dSRandall Stewart 		/* sa_ignore FREED_MEMORY */
3765d00aff5dSRandall Stewart 	}
37664a9ef3f8SMichael Tuexen 	for (i = 0; i < asoc->streamoutcnt; i++) {
37677f34832bSRandall Stewart 		/* For each stream */
37684a9ef3f8SMichael Tuexen 		outs = &asoc->strmout[i];
37697f34832bSRandall Stewart 		/* clean up any sends there */
37704a9ef3f8SMichael Tuexen 		asoc->locked_on_sending = NULL;
37714a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
37724a9ef3f8SMichael Tuexen 			asoc->stream_queue_cnt--;
3773f8829a4aSRandall Stewart 			TAILQ_REMOVE(&outs->outqueue, sp, next);
3774f8829a4aSRandall Stewart 			sctp_free_spbufspace(stcb, asoc, sp);
3775478fbccbSRandall Stewart 			if (sp->data) {
3776f8829a4aSRandall Stewart 				sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
37771edc9dbaSMichael Tuexen 				    error, (void *)sp, so_locked);
3778f8829a4aSRandall Stewart 				if (sp->data) {
3779f8829a4aSRandall Stewart 					sctp_m_freem(sp->data);
3780f8829a4aSRandall Stewart 					sp->data = NULL;
3781d07b2ac6SMichael Tuexen 					sp->tail_mbuf = NULL;
3782d07b2ac6SMichael Tuexen 					sp->length = 0;
3783f8829a4aSRandall Stewart 				}
3784478fbccbSRandall Stewart 			}
37859eea4a2dSMichael Tuexen 			if (sp->net) {
3786f8829a4aSRandall Stewart 				sctp_free_remote_addr(sp->net);
3787f8829a4aSRandall Stewart 				sp->net = NULL;
37889eea4a2dSMichael Tuexen 			}
3789f8829a4aSRandall Stewart 			/* Free the chunk */
3790689e6a5fSMichael Tuexen 			sctp_free_a_strmoq(stcb, sp, so_locked);
37913c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
3792f8829a4aSRandall Stewart 		}
3793f8829a4aSRandall Stewart 	}
3794f8829a4aSRandall Stewart 
3795ad81507eSRandall Stewart 	if (holds_lock == 0) {
37967f34832bSRandall Stewart 		SCTP_TCB_SEND_UNLOCK(stcb);
3797f8829a4aSRandall Stewart 	}
3798ad81507eSRandall Stewart }
3799f8829a4aSRandall Stewart 
3800f8829a4aSRandall Stewart void
3801410a3b1eSMichael Tuexen sctp_abort_notification(struct sctp_tcb *stcb, uint8_t from_peer, uint16_t error,
3802a2b42326SMichael Tuexen     struct sctp_abort_chunk *abort, int so_locked
3803ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3804ceaad40aSRandall Stewart     SCTP_UNUSED
3805ceaad40aSRandall Stewart #endif
3806ceaad40aSRandall Stewart )
3807f8829a4aSRandall Stewart {
3808ad81507eSRandall Stewart 	if (stcb == NULL) {
3809ad81507eSRandall Stewart 		return;
3810ad81507eSRandall Stewart 	}
3811c55b70ceSMichael Tuexen 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3812c55b70ceSMichael Tuexen 	    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3813c55b70ceSMichael Tuexen 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
3814c55b70ceSMichael Tuexen 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_WAS_ABORTED;
3815c55b70ceSMichael Tuexen 	}
3816f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3817f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3818f8829a4aSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3819f8829a4aSRandall Stewart 		return;
3820f8829a4aSRandall Stewart 	}
3821f8829a4aSRandall Stewart 	/* Tell them we lost the asoc */
38221edc9dbaSMichael Tuexen 	sctp_report_all_outbound(stcb, error, 1, so_locked);
3823410a3b1eSMichael Tuexen 	if (from_peer) {
3824410a3b1eSMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_REM_ABORTED, stcb, error, abort, so_locked);
3825410a3b1eSMichael Tuexen 	} else {
3826410a3b1eSMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_LOC_ABORTED, stcb, error, abort, so_locked);
3827410a3b1eSMichael Tuexen 	}
3828f8829a4aSRandall Stewart }
3829f8829a4aSRandall Stewart 
3830f8829a4aSRandall Stewart void
3831f8829a4aSRandall Stewart sctp_abort_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3832b1754ad1SMichael Tuexen     struct mbuf *m, int iphlen,
3833b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
3834b1754ad1SMichael Tuexen     struct sctphdr *sh, struct mbuf *op_err,
3835f30ac432SMichael Tuexen     uint8_t use_mflowid, uint32_t mflowid,
3836c54a18d2SRandall Stewart     uint32_t vrf_id, uint16_t port)
3837f8829a4aSRandall Stewart {
3838f8829a4aSRandall Stewart 	uint32_t vtag;
3839f8829a4aSRandall Stewart 
3840ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3841ceaad40aSRandall Stewart 	struct socket *so;
3842ceaad40aSRandall Stewart 
3843ceaad40aSRandall Stewart #endif
3844ceaad40aSRandall Stewart 
3845f8829a4aSRandall Stewart 	vtag = 0;
3846f8829a4aSRandall Stewart 	if (stcb != NULL) {
3847f8829a4aSRandall Stewart 		/* We have a TCB to abort, send notification too */
3848f8829a4aSRandall Stewart 		vtag = stcb->asoc.peer_vtag;
3849410a3b1eSMichael Tuexen 		sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
385017205eccSRandall Stewart 		/* get the assoc vrf id and table id */
385117205eccSRandall Stewart 		vrf_id = stcb->asoc.vrf_id;
385263981c2bSRandall Stewart 		stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
3853f8829a4aSRandall Stewart 	}
3854b1754ad1SMichael Tuexen 	sctp_send_abort(m, iphlen, src, dst, sh, vtag, op_err,
3855f30ac432SMichael Tuexen 	    use_mflowid, mflowid,
3856f30ac432SMichael Tuexen 	    vrf_id, port);
3857f8829a4aSRandall Stewart 	if (stcb != NULL) {
3858f8829a4aSRandall Stewart 		/* Ok, now lets free it */
3859ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3860ceaad40aSRandall Stewart 		so = SCTP_INP_SO(inp);
3861ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
3862ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3863ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
3864ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
3865ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
3866ceaad40aSRandall Stewart #endif
38670271d0cdSMichael Tuexen 		SCTP_STAT_INCR_COUNTER32(sctps_aborted);
38680271d0cdSMichael Tuexen 		if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
38690271d0cdSMichael Tuexen 		    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
38700271d0cdSMichael Tuexen 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
38710271d0cdSMichael Tuexen 		}
3872c4739e2fSRandall Stewart 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_4);
3873ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3874ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
3875ceaad40aSRandall Stewart #endif
3876f8829a4aSRandall Stewart 	}
3877f8829a4aSRandall Stewart }
3878f8829a4aSRandall Stewart 
3879f1f73e57SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
3880f1f73e57SRandall Stewart void
3881f1f73e57SRandall Stewart sctp_print_out_track_log(struct sctp_tcb *stcb)
3882f1f73e57SRandall Stewart {
388318e198d3SRandall Stewart #ifdef NOSIY_PRINTS
3884f1f73e57SRandall Stewart 	int i;
3885f1f73e57SRandall Stewart 
3886ad81507eSRandall Stewart 	SCTP_PRINTF("Last ep reason:%x\n", stcb->sctp_ep->last_abort_code);
3887ad81507eSRandall Stewart 	SCTP_PRINTF("IN bound TSN log-aaa\n");
3888f1f73e57SRandall Stewart 	if ((stcb->asoc.tsn_in_at == 0) && (stcb->asoc.tsn_in_wrapped == 0)) {
3889ad81507eSRandall Stewart 		SCTP_PRINTF("None rcvd\n");
3890f1f73e57SRandall Stewart 		goto none_in;
3891f1f73e57SRandall Stewart 	}
3892f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_in_wrapped) {
3893f1f73e57SRandall Stewart 		for (i = stcb->asoc.tsn_in_at; i < SCTP_TSN_LOG_SIZE; i++) {
3894ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3895f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].tsn,
3896f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].strm,
3897f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].seq,
3898f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].flgs,
3899f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].sz);
3900f1f73e57SRandall Stewart 		}
3901f1f73e57SRandall Stewart 	}
3902f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_in_at) {
3903f1f73e57SRandall Stewart 		for (i = 0; i < stcb->asoc.tsn_in_at; i++) {
3904ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3905f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].tsn,
3906f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].strm,
3907f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].seq,
3908f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].flgs,
3909f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].sz);
3910f1f73e57SRandall Stewart 		}
3911f1f73e57SRandall Stewart 	}
3912f1f73e57SRandall Stewart none_in:
3913ad81507eSRandall Stewart 	SCTP_PRINTF("OUT bound TSN log-aaa\n");
3914ad81507eSRandall Stewart 	if ((stcb->asoc.tsn_out_at == 0) &&
3915ad81507eSRandall Stewart 	    (stcb->asoc.tsn_out_wrapped == 0)) {
3916ad81507eSRandall Stewart 		SCTP_PRINTF("None sent\n");
3917f1f73e57SRandall Stewart 	}
3918f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_out_wrapped) {
3919f1f73e57SRandall Stewart 		for (i = stcb->asoc.tsn_out_at; i < SCTP_TSN_LOG_SIZE; i++) {
3920ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3921f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].tsn,
3922f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].strm,
3923f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].seq,
3924f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].flgs,
3925f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].sz);
3926f1f73e57SRandall Stewart 		}
3927f1f73e57SRandall Stewart 	}
3928f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_out_at) {
3929f1f73e57SRandall Stewart 		for (i = 0; i < stcb->asoc.tsn_out_at; i++) {
3930ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3931f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].tsn,
3932f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].strm,
3933f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].seq,
3934f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].flgs,
3935f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].sz);
3936f1f73e57SRandall Stewart 		}
3937f1f73e57SRandall Stewart 	}
393818e198d3SRandall Stewart #endif
3939f1f73e57SRandall Stewart }
3940f1f73e57SRandall Stewart 
3941f1f73e57SRandall Stewart #endif
3942f1f73e57SRandall Stewart 
3943f8829a4aSRandall Stewart void
3944f8829a4aSRandall Stewart sctp_abort_an_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3945a2b42326SMichael Tuexen     struct mbuf *op_err,
3946ceaad40aSRandall Stewart     int so_locked
3947ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3948ceaad40aSRandall Stewart     SCTP_UNUSED
3949ceaad40aSRandall Stewart #endif
3950ceaad40aSRandall Stewart )
3951f8829a4aSRandall Stewart {
3952ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3953ceaad40aSRandall Stewart 	struct socket *so;
3954ceaad40aSRandall Stewart 
3955ceaad40aSRandall Stewart #endif
3956ceaad40aSRandall Stewart 
3957ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3958ceaad40aSRandall Stewart 	so = SCTP_INP_SO(inp);
3959ceaad40aSRandall Stewart #endif
3960f8829a4aSRandall Stewart 	if (stcb == NULL) {
3961f8829a4aSRandall Stewart 		/* Got to have a TCB */
3962f8829a4aSRandall Stewart 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3963fe1831e0SMichael Tuexen 			if (LIST_EMPTY(&inp->sctp_asoc_list)) {
3964b0552ae2SRandall Stewart 				sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
3965b0552ae2SRandall Stewart 				    SCTP_CALLED_DIRECTLY_NOCMPSET);
3966f8829a4aSRandall Stewart 			}
3967f8829a4aSRandall Stewart 		}
3968f8829a4aSRandall Stewart 		return;
396963981c2bSRandall Stewart 	} else {
397063981c2bSRandall Stewart 		stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
3971f8829a4aSRandall Stewart 	}
3972f8829a4aSRandall Stewart 	/* notify the ulp */
3973a2b42326SMichael Tuexen 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
3974410a3b1eSMichael Tuexen 		sctp_abort_notification(stcb, 0, 0, NULL, so_locked);
3975a2b42326SMichael Tuexen 	}
3976f8829a4aSRandall Stewart 	/* notify the peer */
3977ceaad40aSRandall Stewart 	sctp_send_abort_tcb(stcb, op_err, so_locked);
3978f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3979f8829a4aSRandall Stewart 	if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
3980f8829a4aSRandall Stewart 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3981f8829a4aSRandall Stewart 		SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3982f8829a4aSRandall Stewart 	}
3983f8829a4aSRandall Stewart 	/* now free the asoc */
3984f1f73e57SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
3985f1f73e57SRandall Stewart 	sctp_print_out_track_log(stcb);
3986f1f73e57SRandall Stewart #endif
3987ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3988ceaad40aSRandall Stewart 	if (!so_locked) {
3989ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
3990ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3991ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
3992ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
3993ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
3994ceaad40aSRandall Stewart 	}
3995ceaad40aSRandall Stewart #endif
3996c4739e2fSRandall Stewart 	(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_5);
3997ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3998ceaad40aSRandall Stewart 	if (!so_locked) {
3999ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4000ceaad40aSRandall Stewart 	}
4001ceaad40aSRandall Stewart #endif
4002f8829a4aSRandall Stewart }
4003f8829a4aSRandall Stewart 
4004f8829a4aSRandall Stewart void
4005b1754ad1SMichael Tuexen sctp_handle_ootb(struct mbuf *m, int iphlen, int offset,
4006b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
4007b1754ad1SMichael Tuexen     struct sctphdr *sh, struct sctp_inpcb *inp,
4008ff1ffd74SMichael Tuexen     struct mbuf *cause,
4009f30ac432SMichael Tuexen     uint8_t use_mflowid, uint32_t mflowid,
4010f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
4011f8829a4aSRandall Stewart {
4012f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch, chunk_buf;
4013f8829a4aSRandall Stewart 	unsigned int chk_length;
4014c58e60beSMichael Tuexen 	int contains_init_chunk;
4015f8829a4aSRandall Stewart 
4016f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue);
4017f8829a4aSRandall Stewart 	/* Generate a TO address for future reference */
4018f8829a4aSRandall Stewart 	if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4019fe1831e0SMichael Tuexen 		if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4020b0552ae2SRandall Stewart 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4021b0552ae2SRandall Stewart 			    SCTP_CALLED_DIRECTLY_NOCMPSET);
4022f8829a4aSRandall Stewart 		}
4023f8829a4aSRandall Stewart 	}
4024c58e60beSMichael Tuexen 	contains_init_chunk = 0;
4025f8829a4aSRandall Stewart 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4026f8829a4aSRandall Stewart 	    sizeof(*ch), (uint8_t *) & chunk_buf);
4027f8829a4aSRandall Stewart 	while (ch != NULL) {
4028f8829a4aSRandall Stewart 		chk_length = ntohs(ch->chunk_length);
4029f8829a4aSRandall Stewart 		if (chk_length < sizeof(*ch)) {
4030f8829a4aSRandall Stewart 			/* break to abort land */
4031f8829a4aSRandall Stewart 			break;
4032f8829a4aSRandall Stewart 		}
4033f8829a4aSRandall Stewart 		switch (ch->chunk_type) {
4034c58e60beSMichael Tuexen 		case SCTP_INIT:
4035c58e60beSMichael Tuexen 			contains_init_chunk = 1;
4036c58e60beSMichael Tuexen 			break;
4037f8829a4aSRandall Stewart 		case SCTP_PACKET_DROPPED:
4038f8829a4aSRandall Stewart 			/* we don't respond to pkt-dropped */
4039f8829a4aSRandall Stewart 			return;
4040f8829a4aSRandall Stewart 		case SCTP_ABORT_ASSOCIATION:
4041f8829a4aSRandall Stewart 			/* we don't respond with an ABORT to an ABORT */
4042f8829a4aSRandall Stewart 			return;
4043f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_COMPLETE:
4044f8829a4aSRandall Stewart 			/*
4045f8829a4aSRandall Stewart 			 * we ignore it since we are not waiting for it and
4046f8829a4aSRandall Stewart 			 * peer is gone
4047f8829a4aSRandall Stewart 			 */
4048f8829a4aSRandall Stewart 			return;
4049f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_ACK:
4050b1754ad1SMichael Tuexen 			sctp_send_shutdown_complete2(src, dst, sh,
4051f30ac432SMichael Tuexen 			    use_mflowid, mflowid,
4052f30ac432SMichael Tuexen 			    vrf_id, port);
4053f8829a4aSRandall Stewart 			return;
4054f8829a4aSRandall Stewart 		default:
4055f8829a4aSRandall Stewart 			break;
4056f8829a4aSRandall Stewart 		}
4057f8829a4aSRandall Stewart 		offset += SCTP_SIZE32(chk_length);
4058f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4059f8829a4aSRandall Stewart 		    sizeof(*ch), (uint8_t *) & chunk_buf);
4060f8829a4aSRandall Stewart 	}
4061c58e60beSMichael Tuexen 	if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
4062c58e60beSMichael Tuexen 	    ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
4063c58e60beSMichael Tuexen 	    (contains_init_chunk == 0))) {
4064ff1ffd74SMichael Tuexen 		sctp_send_abort(m, iphlen, src, dst, sh, 0, cause,
4065f30ac432SMichael Tuexen 		    use_mflowid, mflowid,
4066f30ac432SMichael Tuexen 		    vrf_id, port);
4067f8829a4aSRandall Stewart 	}
4068c58e60beSMichael Tuexen }
4069f8829a4aSRandall Stewart 
4070f8829a4aSRandall Stewart /*
4071f8829a4aSRandall Stewart  * check the inbound datagram to make sure there is not an abort inside it,
4072f8829a4aSRandall Stewart  * if there is return 1, else return 0.
4073f8829a4aSRandall Stewart  */
4074f8829a4aSRandall Stewart int
4075f8829a4aSRandall Stewart sctp_is_there_an_abort_here(struct mbuf *m, int iphlen, uint32_t * vtagfill)
4076f8829a4aSRandall Stewart {
4077f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
4078f8829a4aSRandall Stewart 	struct sctp_init_chunk *init_chk, chunk_buf;
4079f8829a4aSRandall Stewart 	int offset;
4080f8829a4aSRandall Stewart 	unsigned int chk_length;
4081f8829a4aSRandall Stewart 
4082f8829a4aSRandall Stewart 	offset = iphlen + sizeof(struct sctphdr);
4083f8829a4aSRandall Stewart 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch),
4084f8829a4aSRandall Stewart 	    (uint8_t *) & chunk_buf);
4085f8829a4aSRandall Stewart 	while (ch != NULL) {
4086f8829a4aSRandall Stewart 		chk_length = ntohs(ch->chunk_length);
4087f8829a4aSRandall Stewart 		if (chk_length < sizeof(*ch)) {
4088f8829a4aSRandall Stewart 			/* packet is probably corrupt */
4089f8829a4aSRandall Stewart 			break;
4090f8829a4aSRandall Stewart 		}
4091f8829a4aSRandall Stewart 		/* we seem to be ok, is it an abort? */
4092f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) {
4093f8829a4aSRandall Stewart 			/* yep, tell them */
4094f8829a4aSRandall Stewart 			return (1);
4095f8829a4aSRandall Stewart 		}
4096f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_INITIATION) {
4097f8829a4aSRandall Stewart 			/* need to update the Vtag */
4098f8829a4aSRandall Stewart 			init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4099f8829a4aSRandall Stewart 			    offset, sizeof(*init_chk), (uint8_t *) & chunk_buf);
4100f8829a4aSRandall Stewart 			if (init_chk != NULL) {
4101f8829a4aSRandall Stewart 				*vtagfill = ntohl(init_chk->init.initiate_tag);
4102f8829a4aSRandall Stewart 			}
4103f8829a4aSRandall Stewart 		}
4104f8829a4aSRandall Stewart 		/* Nope, move to the next chunk */
4105f8829a4aSRandall Stewart 		offset += SCTP_SIZE32(chk_length);
4106f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4107f8829a4aSRandall Stewart 		    sizeof(*ch), (uint8_t *) & chunk_buf);
4108f8829a4aSRandall Stewart 	}
4109f8829a4aSRandall Stewart 	return (0);
4110f8829a4aSRandall Stewart }
4111f8829a4aSRandall Stewart 
4112f8829a4aSRandall Stewart /*
4113f8829a4aSRandall Stewart  * currently (2/02), ifa_addr embeds scope_id's and don't have sin6_scope_id
4114f8829a4aSRandall Stewart  * set (i.e. it's 0) so, create this function to compare link local scopes
4115f8829a4aSRandall Stewart  */
41165e2c2d87SRandall Stewart #ifdef INET6
4117f8829a4aSRandall Stewart uint32_t
4118f8829a4aSRandall Stewart sctp_is_same_scope(struct sockaddr_in6 *addr1, struct sockaddr_in6 *addr2)
4119f8829a4aSRandall Stewart {
4120f8829a4aSRandall Stewart 	struct sockaddr_in6 a, b;
4121f8829a4aSRandall Stewart 
4122f8829a4aSRandall Stewart 	/* save copies */
4123f8829a4aSRandall Stewart 	a = *addr1;
4124f8829a4aSRandall Stewart 	b = *addr2;
4125f8829a4aSRandall Stewart 
4126f8829a4aSRandall Stewart 	if (a.sin6_scope_id == 0)
4127f8829a4aSRandall Stewart 		if (sa6_recoverscope(&a)) {
4128f8829a4aSRandall Stewart 			/* can't get scope, so can't match */
4129f8829a4aSRandall Stewart 			return (0);
4130f8829a4aSRandall Stewart 		}
4131f8829a4aSRandall Stewart 	if (b.sin6_scope_id == 0)
4132f8829a4aSRandall Stewart 		if (sa6_recoverscope(&b)) {
4133f8829a4aSRandall Stewart 			/* can't get scope, so can't match */
4134f8829a4aSRandall Stewart 			return (0);
4135f8829a4aSRandall Stewart 		}
4136f8829a4aSRandall Stewart 	if (a.sin6_scope_id != b.sin6_scope_id)
4137f8829a4aSRandall Stewart 		return (0);
4138f8829a4aSRandall Stewart 
4139f8829a4aSRandall Stewart 	return (1);
4140f8829a4aSRandall Stewart }
4141f8829a4aSRandall Stewart 
4142f8829a4aSRandall Stewart /*
4143f8829a4aSRandall Stewart  * returns a sockaddr_in6 with embedded scope recovered and removed
4144f8829a4aSRandall Stewart  */
4145f8829a4aSRandall Stewart struct sockaddr_in6 *
4146f8829a4aSRandall Stewart sctp_recover_scope(struct sockaddr_in6 *addr, struct sockaddr_in6 *store)
4147f8829a4aSRandall Stewart {
4148f8829a4aSRandall Stewart 	/* check and strip embedded scope junk */
4149f8829a4aSRandall Stewart 	if (addr->sin6_family == AF_INET6) {
4150f8829a4aSRandall Stewart 		if (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr)) {
4151f8829a4aSRandall Stewart 			if (addr->sin6_scope_id == 0) {
4152f8829a4aSRandall Stewart 				*store = *addr;
4153f8829a4aSRandall Stewart 				if (!sa6_recoverscope(store)) {
4154f8829a4aSRandall Stewart 					/* use the recovered scope */
4155f8829a4aSRandall Stewart 					addr = store;
4156f8829a4aSRandall Stewart 				}
4157f42a358aSRandall Stewart 			} else {
4158f8829a4aSRandall Stewart 				/* else, return the original "to" addr */
4159f42a358aSRandall Stewart 				in6_clearscope(&addr->sin6_addr);
4160f8829a4aSRandall Stewart 			}
4161f8829a4aSRandall Stewart 		}
4162f8829a4aSRandall Stewart 	}
4163f8829a4aSRandall Stewart 	return (addr);
4164f8829a4aSRandall Stewart }
4165f8829a4aSRandall Stewart 
41665e2c2d87SRandall Stewart #endif
41675e2c2d87SRandall Stewart 
4168f8829a4aSRandall Stewart /*
4169f8829a4aSRandall Stewart  * are the two addresses the same?  currently a "scopeless" check returns: 1
4170f8829a4aSRandall Stewart  * if same, 0 if not
4171f8829a4aSRandall Stewart  */
417272fb6fdbSRandall Stewart int
4173f8829a4aSRandall Stewart sctp_cmpaddr(struct sockaddr *sa1, struct sockaddr *sa2)
4174f8829a4aSRandall Stewart {
4175f8829a4aSRandall Stewart 
4176f8829a4aSRandall Stewart 	/* must be valid */
4177f8829a4aSRandall Stewart 	if (sa1 == NULL || sa2 == NULL)
4178f8829a4aSRandall Stewart 		return (0);
4179f8829a4aSRandall Stewart 
4180f8829a4aSRandall Stewart 	/* must be the same family */
4181f8829a4aSRandall Stewart 	if (sa1->sa_family != sa2->sa_family)
4182f8829a4aSRandall Stewart 		return (0);
4183f8829a4aSRandall Stewart 
41845e2c2d87SRandall Stewart 	switch (sa1->sa_family) {
41855e2c2d87SRandall Stewart #ifdef INET6
41865e2c2d87SRandall Stewart 	case AF_INET6:
41875e2c2d87SRandall Stewart 		{
4188f8829a4aSRandall Stewart 			/* IPv6 addresses */
4189f8829a4aSRandall Stewart 			struct sockaddr_in6 *sin6_1, *sin6_2;
4190f8829a4aSRandall Stewart 
4191f8829a4aSRandall Stewart 			sin6_1 = (struct sockaddr_in6 *)sa1;
4192f8829a4aSRandall Stewart 			sin6_2 = (struct sockaddr_in6 *)sa2;
4193c54a18d2SRandall Stewart 			return (SCTP6_ARE_ADDR_EQUAL(sin6_1,
4194c54a18d2SRandall Stewart 			    sin6_2));
41955e2c2d87SRandall Stewart 		}
41965e2c2d87SRandall Stewart #endif
4197ea5eba11SMichael Tuexen #ifdef INET
41985e2c2d87SRandall Stewart 	case AF_INET:
41995e2c2d87SRandall Stewart 		{
4200f8829a4aSRandall Stewart 			/* IPv4 addresses */
4201f8829a4aSRandall Stewart 			struct sockaddr_in *sin_1, *sin_2;
4202f8829a4aSRandall Stewart 
4203f8829a4aSRandall Stewart 			sin_1 = (struct sockaddr_in *)sa1;
4204f8829a4aSRandall Stewart 			sin_2 = (struct sockaddr_in *)sa2;
4205f8829a4aSRandall Stewart 			return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr);
42065e2c2d87SRandall Stewart 		}
4207ea5eba11SMichael Tuexen #endif
42085e2c2d87SRandall Stewart 	default:
4209f8829a4aSRandall Stewart 		/* we don't do these... */
4210f8829a4aSRandall Stewart 		return (0);
4211f8829a4aSRandall Stewart 	}
4212f8829a4aSRandall Stewart }
4213f8829a4aSRandall Stewart 
4214f8829a4aSRandall Stewart void
4215f8829a4aSRandall Stewart sctp_print_address(struct sockaddr *sa)
4216f8829a4aSRandall Stewart {
42175e2c2d87SRandall Stewart #ifdef INET6
42187d32aa0cSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
4219f8829a4aSRandall Stewart 
42205e2c2d87SRandall Stewart #endif
42215e2c2d87SRandall Stewart 
42225e2c2d87SRandall Stewart 	switch (sa->sa_family) {
42235e2c2d87SRandall Stewart #ifdef INET6
42245e2c2d87SRandall Stewart 	case AF_INET6:
42255e2c2d87SRandall Stewart 		{
4226ad81507eSRandall Stewart 			struct sockaddr_in6 *sin6;
4227ad81507eSRandall Stewart 
4228f8829a4aSRandall Stewart 			sin6 = (struct sockaddr_in6 *)sa;
4229ad81507eSRandall Stewart 			SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n",
42307d32aa0cSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &sin6->sin6_addr),
42317d32aa0cSBjoern A. Zeeb 			    ntohs(sin6->sin6_port),
4232f8829a4aSRandall Stewart 			    sin6->sin6_scope_id);
42335e2c2d87SRandall Stewart 			break;
42345e2c2d87SRandall Stewart 		}
42355e2c2d87SRandall Stewart #endif
4236ea5eba11SMichael Tuexen #ifdef INET
42375e2c2d87SRandall Stewart 	case AF_INET:
42385e2c2d87SRandall Stewart 		{
4239f8829a4aSRandall Stewart 			struct sockaddr_in *sin;
4240f8829a4aSRandall Stewart 			unsigned char *p;
4241f8829a4aSRandall Stewart 
4242f8829a4aSRandall Stewart 			sin = (struct sockaddr_in *)sa;
4243f8829a4aSRandall Stewart 			p = (unsigned char *)&sin->sin_addr;
4244ad81507eSRandall Stewart 			SCTP_PRINTF("IPv4 address: %u.%u.%u.%u:%d\n",
4245f8829a4aSRandall Stewart 			    p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
42465e2c2d87SRandall Stewart 			break;
42475e2c2d87SRandall Stewart 		}
4248ea5eba11SMichael Tuexen #endif
42495e2c2d87SRandall Stewart 	default:
4250ad81507eSRandall Stewart 		SCTP_PRINTF("?\n");
42515e2c2d87SRandall Stewart 		break;
4252f8829a4aSRandall Stewart 	}
4253f8829a4aSRandall Stewart }
4254f8829a4aSRandall Stewart 
4255f8829a4aSRandall Stewart void
4256f8829a4aSRandall Stewart sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
4257f8829a4aSRandall Stewart     struct sctp_inpcb *new_inp,
4258d06c82f1SRandall Stewart     struct sctp_tcb *stcb,
4259d06c82f1SRandall Stewart     int waitflags)
4260f8829a4aSRandall Stewart {
4261f8829a4aSRandall Stewart 	/*
4262f8829a4aSRandall Stewart 	 * go through our old INP and pull off any control structures that
4263f8829a4aSRandall Stewart 	 * belong to stcb and move then to the new inp.
4264f8829a4aSRandall Stewart 	 */
4265f8829a4aSRandall Stewart 	struct socket *old_so, *new_so;
4266f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control, *nctl;
4267f8829a4aSRandall Stewart 	struct sctp_readhead tmp_queue;
4268f8829a4aSRandall Stewart 	struct mbuf *m;
4269bff64a4dSRandall Stewart 	int error = 0;
4270f8829a4aSRandall Stewart 
4271f8829a4aSRandall Stewart 	old_so = old_inp->sctp_socket;
4272f8829a4aSRandall Stewart 	new_so = new_inp->sctp_socket;
4273f8829a4aSRandall Stewart 	TAILQ_INIT(&tmp_queue);
4274d06c82f1SRandall Stewart 	error = sblock(&old_so->so_rcv, waitflags);
4275f8829a4aSRandall Stewart 	if (error) {
4276f8829a4aSRandall Stewart 		/*
4277f8829a4aSRandall Stewart 		 * Gak, can't get sblock, we have a problem. data will be
4278f8829a4aSRandall Stewart 		 * left stranded.. and we don't dare look at it since the
4279f8829a4aSRandall Stewart 		 * other thread may be reading something. Oh well, its a
4280f8829a4aSRandall Stewart 		 * screwed up app that does a peeloff OR a accept while
4281f8829a4aSRandall Stewart 		 * reading from the main socket... actually its only the
4282f8829a4aSRandall Stewart 		 * peeloff() case, since I think read will fail on a
4283f8829a4aSRandall Stewart 		 * listening socket..
4284f8829a4aSRandall Stewart 		 */
4285f8829a4aSRandall Stewart 		return;
4286f8829a4aSRandall Stewart 	}
4287f8829a4aSRandall Stewart 	/* lock the socket buffers */
4288f8829a4aSRandall Stewart 	SCTP_INP_READ_LOCK(old_inp);
42894a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(control, &old_inp->read_queue, next, nctl) {
4290f8829a4aSRandall Stewart 		/* Pull off all for out target stcb */
4291f8829a4aSRandall Stewart 		if (control->stcb == stcb) {
4292f8829a4aSRandall Stewart 			/* remove it we want it */
4293f8829a4aSRandall Stewart 			TAILQ_REMOVE(&old_inp->read_queue, control, next);
4294f8829a4aSRandall Stewart 			TAILQ_INSERT_TAIL(&tmp_queue, control, next);
4295f8829a4aSRandall Stewart 			m = control->data;
4296f8829a4aSRandall Stewart 			while (m) {
4297b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4298139bc87fSRandall Stewart 					sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
429980fefe0aSRandall Stewart 				}
4300f8829a4aSRandall Stewart 				sctp_sbfree(control, stcb, &old_so->so_rcv, m);
4301b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4302f8829a4aSRandall Stewart 					sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
430380fefe0aSRandall Stewart 				}
4304139bc87fSRandall Stewart 				m = SCTP_BUF_NEXT(m);
4305f8829a4aSRandall Stewart 			}
4306f8829a4aSRandall Stewart 		}
4307f8829a4aSRandall Stewart 	}
4308f8829a4aSRandall Stewart 	SCTP_INP_READ_UNLOCK(old_inp);
4309f8829a4aSRandall Stewart 	/* Remove the sb-lock on the old socket */
4310f8829a4aSRandall Stewart 
4311f8829a4aSRandall Stewart 	sbunlock(&old_so->so_rcv);
4312f8829a4aSRandall Stewart 	/* Now we move them over to the new socket buffer */
4313f8829a4aSRandall Stewart 	SCTP_INP_READ_LOCK(new_inp);
43144a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(control, &tmp_queue, next, nctl) {
4315f8829a4aSRandall Stewart 		TAILQ_INSERT_TAIL(&new_inp->read_queue, control, next);
4316f8829a4aSRandall Stewart 		m = control->data;
4317f8829a4aSRandall Stewart 		while (m) {
4318b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4319139bc87fSRandall Stewart 				sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
432080fefe0aSRandall Stewart 			}
4321f8829a4aSRandall Stewart 			sctp_sballoc(stcb, &new_so->so_rcv, m);
4322b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4323f8829a4aSRandall Stewart 				sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
432480fefe0aSRandall Stewart 			}
4325139bc87fSRandall Stewart 			m = SCTP_BUF_NEXT(m);
4326f8829a4aSRandall Stewart 		}
4327f8829a4aSRandall Stewart 	}
4328f8829a4aSRandall Stewart 	SCTP_INP_READ_UNLOCK(new_inp);
4329f8829a4aSRandall Stewart }
4330f8829a4aSRandall Stewart 
4331f8829a4aSRandall Stewart void
4332f8829a4aSRandall Stewart sctp_add_to_readq(struct sctp_inpcb *inp,
4333f8829a4aSRandall Stewart     struct sctp_tcb *stcb,
4334f8829a4aSRandall Stewart     struct sctp_queued_to_read *control,
4335f8829a4aSRandall Stewart     struct sockbuf *sb,
4336ceaad40aSRandall Stewart     int end,
4337cfde3ff7SRandall Stewart     int inp_read_lock_held,
4338ceaad40aSRandall Stewart     int so_locked
4339ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4340ceaad40aSRandall Stewart     SCTP_UNUSED
4341ceaad40aSRandall Stewart #endif
4342ceaad40aSRandall Stewart )
4343f8829a4aSRandall Stewart {
4344f8829a4aSRandall Stewart 	/*
4345f8829a4aSRandall Stewart 	 * Here we must place the control on the end of the socket read
4346f8829a4aSRandall Stewart 	 * queue AND increment sb_cc so that select will work properly on
4347f8829a4aSRandall Stewart 	 * read.
4348f8829a4aSRandall Stewart 	 */
4349f8829a4aSRandall Stewart 	struct mbuf *m, *prev = NULL;
4350f8829a4aSRandall Stewart 
435103b0b021SRandall Stewart 	if (inp == NULL) {
435203b0b021SRandall Stewart 		/* Gak, TSNH!! */
4353a5d547adSRandall Stewart #ifdef INVARIANTS
435403b0b021SRandall Stewart 		panic("Gak, inp NULL on add_to_readq");
435503b0b021SRandall Stewart #endif
435603b0b021SRandall Stewart 		return;
435703b0b021SRandall Stewart 	}
4358cfde3ff7SRandall Stewart 	if (inp_read_lock_held == 0)
4359f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(inp);
4360cd1386abSMichael Tuexen 	if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
4361cd1386abSMichael Tuexen 		sctp_free_remote_addr(control->whoFrom);
4362cd1386abSMichael Tuexen 		if (control->data) {
4363cd1386abSMichael Tuexen 			sctp_m_freem(control->data);
4364cd1386abSMichael Tuexen 			control->data = NULL;
4365cd1386abSMichael Tuexen 		}
4366cd1386abSMichael Tuexen 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), control);
4367cd1386abSMichael Tuexen 		if (inp_read_lock_held == 0)
4368cd1386abSMichael Tuexen 			SCTP_INP_READ_UNLOCK(inp);
4369cd1386abSMichael Tuexen 		return;
4370cd1386abSMichael Tuexen 	}
437142551e99SRandall Stewart 	if (!(control->spec_flags & M_NOTIFICATION)) {
4372a5d547adSRandall Stewart 		atomic_add_int(&inp->total_recvs, 1);
437342551e99SRandall Stewart 		if (!control->do_not_ref_stcb) {
4374a5d547adSRandall Stewart 			atomic_add_int(&stcb->total_recvs, 1);
437542551e99SRandall Stewart 		}
437642551e99SRandall Stewart 	}
4377f8829a4aSRandall Stewart 	m = control->data;
4378f8829a4aSRandall Stewart 	control->held_length = 0;
4379f8829a4aSRandall Stewart 	control->length = 0;
4380f8829a4aSRandall Stewart 	while (m) {
4381139bc87fSRandall Stewart 		if (SCTP_BUF_LEN(m) == 0) {
4382f8829a4aSRandall Stewart 			/* Skip mbufs with NO length */
4383f8829a4aSRandall Stewart 			if (prev == NULL) {
4384f8829a4aSRandall Stewart 				/* First one */
4385f8829a4aSRandall Stewart 				control->data = sctp_m_free(m);
4386f8829a4aSRandall Stewart 				m = control->data;
4387f8829a4aSRandall Stewart 			} else {
4388139bc87fSRandall Stewart 				SCTP_BUF_NEXT(prev) = sctp_m_free(m);
4389139bc87fSRandall Stewart 				m = SCTP_BUF_NEXT(prev);
4390f8829a4aSRandall Stewart 			}
4391f8829a4aSRandall Stewart 			if (m == NULL) {
4392c2ede4b3SMartin Blapp 				control->tail_mbuf = prev;
4393f8829a4aSRandall Stewart 			}
4394f8829a4aSRandall Stewart 			continue;
4395f8829a4aSRandall Stewart 		}
4396f8829a4aSRandall Stewart 		prev = m;
4397b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4398139bc87fSRandall Stewart 			sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
439980fefe0aSRandall Stewart 		}
4400f8829a4aSRandall Stewart 		sctp_sballoc(stcb, sb, m);
4401b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4402f8829a4aSRandall Stewart 			sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
440380fefe0aSRandall Stewart 		}
4404139bc87fSRandall Stewart 		atomic_add_int(&control->length, SCTP_BUF_LEN(m));
4405139bc87fSRandall Stewart 		m = SCTP_BUF_NEXT(m);
4406f8829a4aSRandall Stewart 	}
4407f8829a4aSRandall Stewart 	if (prev != NULL) {
4408f8829a4aSRandall Stewart 		control->tail_mbuf = prev;
4409f8829a4aSRandall Stewart 	} else {
4410139bc87fSRandall Stewart 		/* Everything got collapsed out?? */
4411cd1386abSMichael Tuexen 		sctp_free_remote_addr(control->whoFrom);
4412cd1386abSMichael Tuexen 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), control);
4413cfde3ff7SRandall Stewart 		if (inp_read_lock_held == 0)
441447a490cbSMichael Tuexen 			SCTP_INP_READ_UNLOCK(inp);
4415f8829a4aSRandall Stewart 		return;
4416f8829a4aSRandall Stewart 	}
4417f8829a4aSRandall Stewart 	if (end) {
4418f8829a4aSRandall Stewart 		control->end_added = 1;
4419f8829a4aSRandall Stewart 	}
4420f8829a4aSRandall Stewart 	TAILQ_INSERT_TAIL(&inp->read_queue, control, next);
4421cfde3ff7SRandall Stewart 	if (inp_read_lock_held == 0)
4422f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
4423f8829a4aSRandall Stewart 	if (inp && inp->sctp_socket) {
442417205eccSRandall Stewart 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
442517205eccSRandall Stewart 			SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
4426ceaad40aSRandall Stewart 		} else {
4427ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4428ceaad40aSRandall Stewart 			struct socket *so;
4429ceaad40aSRandall Stewart 
4430ceaad40aSRandall Stewart 			so = SCTP_INP_SO(inp);
4431ceaad40aSRandall Stewart 			if (!so_locked) {
443260990c0cSMichael Tuexen 				if (stcb) {
4433ceaad40aSRandall Stewart 					atomic_add_int(&stcb->asoc.refcnt, 1);
4434ceaad40aSRandall Stewart 					SCTP_TCB_UNLOCK(stcb);
443560990c0cSMichael Tuexen 				}
4436ceaad40aSRandall Stewart 				SCTP_SOCKET_LOCK(so, 1);
443760990c0cSMichael Tuexen 				if (stcb) {
4438ceaad40aSRandall Stewart 					SCTP_TCB_LOCK(stcb);
4439ceaad40aSRandall Stewart 					atomic_subtract_int(&stcb->asoc.refcnt, 1);
444060990c0cSMichael Tuexen 				}
4441ceaad40aSRandall Stewart 				if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4442ceaad40aSRandall Stewart 					SCTP_SOCKET_UNLOCK(so, 1);
4443ceaad40aSRandall Stewart 					return;
4444ceaad40aSRandall Stewart 				}
4445ceaad40aSRandall Stewart 			}
4446ceaad40aSRandall Stewart #endif
4447f8829a4aSRandall Stewart 			sctp_sorwakeup(inp, inp->sctp_socket);
4448ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4449ceaad40aSRandall Stewart 			if (!so_locked) {
4450ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
4451ceaad40aSRandall Stewart 			}
4452ceaad40aSRandall Stewart #endif
4453ceaad40aSRandall Stewart 		}
4454f8829a4aSRandall Stewart 	}
4455f8829a4aSRandall Stewart }
4456f8829a4aSRandall Stewart 
4457f8829a4aSRandall Stewart 
4458f8829a4aSRandall Stewart int
4459f8829a4aSRandall Stewart sctp_append_to_readq(struct sctp_inpcb *inp,
4460f8829a4aSRandall Stewart     struct sctp_tcb *stcb,
4461f8829a4aSRandall Stewart     struct sctp_queued_to_read *control,
4462f8829a4aSRandall Stewart     struct mbuf *m,
4463f8829a4aSRandall Stewart     int end,
4464f8829a4aSRandall Stewart     int ctls_cumack,
4465f8829a4aSRandall Stewart     struct sockbuf *sb)
4466f8829a4aSRandall Stewart {
4467f8829a4aSRandall Stewart 	/*
4468f8829a4aSRandall Stewart 	 * A partial delivery API event is underway. OR we are appending on
4469f8829a4aSRandall Stewart 	 * the reassembly queue.
4470f8829a4aSRandall Stewart 	 *
4471f8829a4aSRandall Stewart 	 * If PDAPI this means we need to add m to the end of the data.
4472f8829a4aSRandall Stewart 	 * Increase the length in the control AND increment the sb_cc.
4473f8829a4aSRandall Stewart 	 * Otherwise sb is NULL and all we need to do is put it at the end
4474f8829a4aSRandall Stewart 	 * of the mbuf chain.
4475f8829a4aSRandall Stewart 	 */
4476f8829a4aSRandall Stewart 	int len = 0;
4477f8829a4aSRandall Stewart 	struct mbuf *mm, *tail = NULL, *prev = NULL;
4478f8829a4aSRandall Stewart 
4479f8829a4aSRandall Stewart 	if (inp) {
4480f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(inp);
4481f8829a4aSRandall Stewart 	}
4482f8829a4aSRandall Stewart 	if (control == NULL) {
4483f8829a4aSRandall Stewart get_out:
4484f8829a4aSRandall Stewart 		if (inp) {
4485f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
4486f8829a4aSRandall Stewart 		}
4487f8829a4aSRandall Stewart 		return (-1);
4488f8829a4aSRandall Stewart 	}
4489cd1386abSMichael Tuexen 	if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ)) {
4490cd1386abSMichael Tuexen 		SCTP_INP_READ_UNLOCK(inp);
449160990c0cSMichael Tuexen 		return (0);
4492cd1386abSMichael Tuexen 	}
4493139bc87fSRandall Stewart 	if (control->end_added) {
4494f8829a4aSRandall Stewart 		/* huh this one is complete? */
4495f8829a4aSRandall Stewart 		goto get_out;
4496f8829a4aSRandall Stewart 	}
4497f8829a4aSRandall Stewart 	mm = m;
4498f8829a4aSRandall Stewart 	if (mm == NULL) {
4499f8829a4aSRandall Stewart 		goto get_out;
4500f8829a4aSRandall Stewart 	}
4501f8829a4aSRandall Stewart 	while (mm) {
4502139bc87fSRandall Stewart 		if (SCTP_BUF_LEN(mm) == 0) {
4503f8829a4aSRandall Stewart 			/* Skip mbufs with NO lenght */
4504f8829a4aSRandall Stewart 			if (prev == NULL) {
4505f8829a4aSRandall Stewart 				/* First one */
4506f8829a4aSRandall Stewart 				m = sctp_m_free(mm);
4507f8829a4aSRandall Stewart 				mm = m;
4508f8829a4aSRandall Stewart 			} else {
4509139bc87fSRandall Stewart 				SCTP_BUF_NEXT(prev) = sctp_m_free(mm);
4510139bc87fSRandall Stewart 				mm = SCTP_BUF_NEXT(prev);
4511f8829a4aSRandall Stewart 			}
4512f8829a4aSRandall Stewart 			continue;
4513f8829a4aSRandall Stewart 		}
4514f8829a4aSRandall Stewart 		prev = mm;
4515139bc87fSRandall Stewart 		len += SCTP_BUF_LEN(mm);
4516f8829a4aSRandall Stewart 		if (sb) {
4517b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4518139bc87fSRandall Stewart 				sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(mm));
451980fefe0aSRandall Stewart 			}
4520f8829a4aSRandall Stewart 			sctp_sballoc(stcb, sb, mm);
4521b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4522f8829a4aSRandall Stewart 				sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
452380fefe0aSRandall Stewart 			}
4524f8829a4aSRandall Stewart 		}
4525139bc87fSRandall Stewart 		mm = SCTP_BUF_NEXT(mm);
4526f8829a4aSRandall Stewart 	}
4527f8829a4aSRandall Stewart 	if (prev) {
4528f8829a4aSRandall Stewart 		tail = prev;
4529f8829a4aSRandall Stewart 	} else {
4530f8829a4aSRandall Stewart 		/* Really there should always be a prev */
4531f8829a4aSRandall Stewart 		if (m == NULL) {
4532f8829a4aSRandall Stewart 			/* Huh nothing left? */
4533a5d547adSRandall Stewart #ifdef INVARIANTS
4534f8829a4aSRandall Stewart 			panic("Nothing left to add?");
4535f8829a4aSRandall Stewart #else
4536f8829a4aSRandall Stewart 			goto get_out;
4537f8829a4aSRandall Stewart #endif
4538f8829a4aSRandall Stewart 		}
4539f8829a4aSRandall Stewart 		tail = m;
4540f8829a4aSRandall Stewart 	}
4541f8829a4aSRandall Stewart 	if (control->tail_mbuf) {
4542f8829a4aSRandall Stewart 		/* append */
4543139bc87fSRandall Stewart 		SCTP_BUF_NEXT(control->tail_mbuf) = m;
4544f8829a4aSRandall Stewart 		control->tail_mbuf = tail;
4545f8829a4aSRandall Stewart 	} else {
4546f8829a4aSRandall Stewart 		/* nothing there */
4547a5d547adSRandall Stewart #ifdef INVARIANTS
4548f8829a4aSRandall Stewart 		if (control->data != NULL) {
4549f8829a4aSRandall Stewart 			panic("This should NOT happen");
4550f8829a4aSRandall Stewart 		}
4551f8829a4aSRandall Stewart #endif
4552f8829a4aSRandall Stewart 		control->data = m;
4553f8829a4aSRandall Stewart 		control->tail_mbuf = tail;
4554f8829a4aSRandall Stewart 	}
455518e198d3SRandall Stewart 	atomic_add_int(&control->length, len);
455618e198d3SRandall Stewart 	if (end) {
455718e198d3SRandall Stewart 		/* message is complete */
455818e198d3SRandall Stewart 		if (stcb && (control == stcb->asoc.control_pdapi)) {
455918e198d3SRandall Stewart 			stcb->asoc.control_pdapi = NULL;
456018e198d3SRandall Stewart 		}
456118e198d3SRandall Stewart 		control->held_length = 0;
456218e198d3SRandall Stewart 		control->end_added = 1;
456318e198d3SRandall Stewart 	}
4564ad81507eSRandall Stewart 	if (stcb == NULL) {
4565ad81507eSRandall Stewart 		control->do_not_ref_stcb = 1;
4566ad81507eSRandall Stewart 	}
4567f8829a4aSRandall Stewart 	/*
4568f8829a4aSRandall Stewart 	 * When we are appending in partial delivery, the cum-ack is used
4569f8829a4aSRandall Stewart 	 * for the actual pd-api highest tsn on this mbuf. The true cum-ack
4570f8829a4aSRandall Stewart 	 * is populated in the outbound sinfo structure from the true cumack
4571f8829a4aSRandall Stewart 	 * if the association exists...
4572f8829a4aSRandall Stewart 	 */
4573f8829a4aSRandall Stewart 	control->sinfo_tsn = control->sinfo_cumtsn = ctls_cumack;
4574f8829a4aSRandall Stewart 	if (inp) {
4575f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
4576f8829a4aSRandall Stewart 	}
4577f8829a4aSRandall Stewart 	if (inp && inp->sctp_socket) {
457817205eccSRandall Stewart 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
457917205eccSRandall Stewart 			SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
4580ceaad40aSRandall Stewart 		} else {
4581ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4582ceaad40aSRandall Stewart 			struct socket *so;
4583ceaad40aSRandall Stewart 
4584ceaad40aSRandall Stewart 			so = SCTP_INP_SO(inp);
458560990c0cSMichael Tuexen 			if (stcb) {
4586ceaad40aSRandall Stewart 				atomic_add_int(&stcb->asoc.refcnt, 1);
4587ceaad40aSRandall Stewart 				SCTP_TCB_UNLOCK(stcb);
458860990c0cSMichael Tuexen 			}
4589ceaad40aSRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
459060990c0cSMichael Tuexen 			if (stcb) {
4591ceaad40aSRandall Stewart 				SCTP_TCB_LOCK(stcb);
4592ceaad40aSRandall Stewart 				atomic_subtract_int(&stcb->asoc.refcnt, 1);
459360990c0cSMichael Tuexen 			}
4594ceaad40aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4595ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
4596ceaad40aSRandall Stewart 				return (0);
4597ceaad40aSRandall Stewart 			}
4598ceaad40aSRandall Stewart #endif
4599f8829a4aSRandall Stewart 			sctp_sorwakeup(inp, inp->sctp_socket);
4600ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4601ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
4602ceaad40aSRandall Stewart #endif
4603ceaad40aSRandall Stewart 		}
4604f8829a4aSRandall Stewart 	}
4605f8829a4aSRandall Stewart 	return (0);
4606f8829a4aSRandall Stewart }
4607f8829a4aSRandall Stewart 
4608f8829a4aSRandall Stewart 
4609f8829a4aSRandall Stewart 
4610f8829a4aSRandall Stewart /*************HOLD THIS COMMENT FOR PATCH FILE OF
4611f8829a4aSRandall Stewart  *************ALTERNATE ROUTING CODE
4612f8829a4aSRandall Stewart  */
4613f8829a4aSRandall Stewart 
4614f8829a4aSRandall Stewart /*************HOLD THIS COMMENT FOR END OF PATCH FILE OF
4615f8829a4aSRandall Stewart  *************ALTERNATE ROUTING CODE
4616f8829a4aSRandall Stewart  */
4617f8829a4aSRandall Stewart 
4618f8829a4aSRandall Stewart struct mbuf *
4619ff1ffd74SMichael Tuexen sctp_generate_cause(uint16_t code, char *info)
4620f8829a4aSRandall Stewart {
4621f8829a4aSRandall Stewart 	struct mbuf *m;
4622ff1ffd74SMichael Tuexen 	struct sctp_gen_error_cause *cause;
4623ff1ffd74SMichael Tuexen 	size_t info_len, len;
4624f8829a4aSRandall Stewart 
4625ff1ffd74SMichael Tuexen 	if ((code == 0) || (info == NULL)) {
4626ff1ffd74SMichael Tuexen 		return (NULL);
4627ff1ffd74SMichael Tuexen 	}
4628ff1ffd74SMichael Tuexen 	info_len = strlen(info);
4629ff1ffd74SMichael Tuexen 	len = sizeof(struct sctp_paramhdr) + info_len;
4630ff1ffd74SMichael Tuexen 	m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
4631ff1ffd74SMichael Tuexen 	if (m != NULL) {
4632ff1ffd74SMichael Tuexen 		SCTP_BUF_LEN(m) = len;
4633ff1ffd74SMichael Tuexen 		cause = mtod(m, struct sctp_gen_error_cause *);
4634ff1ffd74SMichael Tuexen 		cause->code = htons(code);
4635ff1ffd74SMichael Tuexen 		cause->length = htons((uint16_t) len);
4636ff1ffd74SMichael Tuexen 		memcpy(cause->info, info, info_len);
4637f8829a4aSRandall Stewart 	}
4638f8829a4aSRandall Stewart 	return (m);
4639f8829a4aSRandall Stewart }
4640f8829a4aSRandall Stewart 
464132451da4SMichael Tuexen struct mbuf *
464232451da4SMichael Tuexen sctp_generate_no_user_data_cause(uint32_t tsn)
464332451da4SMichael Tuexen {
464432451da4SMichael Tuexen 	struct mbuf *m;
464532451da4SMichael Tuexen 	struct sctp_error_no_user_data *no_user_data_cause;
464632451da4SMichael Tuexen 	size_t len;
464732451da4SMichael Tuexen 
464832451da4SMichael Tuexen 	len = sizeof(struct sctp_error_no_user_data);
464932451da4SMichael Tuexen 	m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
465032451da4SMichael Tuexen 	if (m != NULL) {
465132451da4SMichael Tuexen 		SCTP_BUF_LEN(m) = len;
465232451da4SMichael Tuexen 		no_user_data_cause = mtod(m, struct sctp_error_no_user_data *);
465332451da4SMichael Tuexen 		no_user_data_cause->cause.code = htons(SCTP_CAUSE_NO_USER_DATA);
465432451da4SMichael Tuexen 		no_user_data_cause->cause.length = htons((uint16_t) len);
465532451da4SMichael Tuexen 		no_user_data_cause->tsn = tsn;	/* tsn is passed in as NBO */
465632451da4SMichael Tuexen 	}
465732451da4SMichael Tuexen 	return (m);
465832451da4SMichael Tuexen }
465932451da4SMichael Tuexen 
4660f8829a4aSRandall Stewart #ifdef SCTP_MBCNT_LOGGING
4661f8829a4aSRandall Stewart void
4662f8829a4aSRandall Stewart sctp_free_bufspace(struct sctp_tcb *stcb, struct sctp_association *asoc,
4663f8829a4aSRandall Stewart     struct sctp_tmit_chunk *tp1, int chk_cnt)
4664f8829a4aSRandall Stewart {
4665f8829a4aSRandall Stewart 	if (tp1->data == NULL) {
4666f8829a4aSRandall Stewart 		return;
4667f8829a4aSRandall Stewart 	}
4668f8829a4aSRandall Stewart 	asoc->chunks_on_out_queue -= chk_cnt;
4669b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBCNT_LOGGING_ENABLE) {
4670f8829a4aSRandall Stewart 		sctp_log_mbcnt(SCTP_LOG_MBCNT_DECREASE,
4671f8829a4aSRandall Stewart 		    asoc->total_output_queue_size,
4672f8829a4aSRandall Stewart 		    tp1->book_size,
4673f8829a4aSRandall Stewart 		    0,
4674f8829a4aSRandall Stewart 		    tp1->mbcnt);
467580fefe0aSRandall Stewart 	}
4676f8829a4aSRandall Stewart 	if (asoc->total_output_queue_size >= tp1->book_size) {
467744b7479bSRandall Stewart 		atomic_add_int(&asoc->total_output_queue_size, -tp1->book_size);
4678f8829a4aSRandall Stewart 	} else {
4679f8829a4aSRandall Stewart 		asoc->total_output_queue_size = 0;
4680f8829a4aSRandall Stewart 	}
4681f8829a4aSRandall Stewart 
4682f8829a4aSRandall Stewart 	if (stcb->sctp_socket && (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) ||
4683f8829a4aSRandall Stewart 	    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) {
4684f8829a4aSRandall Stewart 		if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) {
4685f8829a4aSRandall Stewart 			stcb->sctp_socket->so_snd.sb_cc -= tp1->book_size;
4686f8829a4aSRandall Stewart 		} else {
4687f8829a4aSRandall Stewart 			stcb->sctp_socket->so_snd.sb_cc = 0;
4688f8829a4aSRandall Stewart 
4689f8829a4aSRandall Stewart 		}
4690f8829a4aSRandall Stewart 	}
4691f8829a4aSRandall Stewart }
4692f8829a4aSRandall Stewart 
4693f8829a4aSRandall Stewart #endif
4694f8829a4aSRandall Stewart 
4695f8829a4aSRandall Stewart int
4696f8829a4aSRandall Stewart sctp_release_pr_sctp_chunk(struct sctp_tcb *stcb, struct sctp_tmit_chunk *tp1,
46971edc9dbaSMichael Tuexen     uint8_t sent, int so_locked
4698ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4699ceaad40aSRandall Stewart     SCTP_UNUSED
4700ceaad40aSRandall Stewart #endif
4701ceaad40aSRandall Stewart )
4702f8829a4aSRandall Stewart {
47030c0982b8SRandall Stewart 	struct sctp_stream_out *strq;
47044a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk = NULL, *tp2;
47050c0982b8SRandall Stewart 	struct sctp_stream_queue_pending *sp;
47060c0982b8SRandall Stewart 	uint16_t stream = 0, seq = 0;
47070c0982b8SRandall Stewart 	uint8_t foundeom = 0;
4708f8829a4aSRandall Stewart 	int ret_sz = 0;
4709f8829a4aSRandall Stewart 	int notdone;
47100c0982b8SRandall Stewart 	int do_wakeup_routine = 0;
4711f8829a4aSRandall Stewart 
47120c0982b8SRandall Stewart 	stream = tp1->rec.data.stream_number;
47130c0982b8SRandall Stewart 	seq = tp1->rec.data.stream_seq;
4714f8829a4aSRandall Stewart 	do {
4715f8829a4aSRandall Stewart 		ret_sz += tp1->book_size;
47160c0982b8SRandall Stewart 		if (tp1->data != NULL) {
47178933fa13SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4718830d754dSRandall Stewart 				sctp_flight_size_decrease(tp1);
4719830d754dSRandall Stewart 				sctp_total_flight_decrease(stcb, tp1);
47208933fa13SRandall Stewart 			}
47218933fa13SRandall Stewart 			sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
47220c0982b8SRandall Stewart 			stcb->asoc.peers_rwnd += tp1->send_size;
47230c0982b8SRandall Stewart 			stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
47241edc9dbaSMichael Tuexen 			if (sent) {
47251edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
47261edc9dbaSMichael Tuexen 			} else {
47271edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
47281edc9dbaSMichael Tuexen 			}
47292f99457bSMichael Tuexen 			if (tp1->data) {
4730f8829a4aSRandall Stewart 				sctp_m_freem(tp1->data);
4731f8829a4aSRandall Stewart 				tp1->data = NULL;
47322f99457bSMichael Tuexen 			}
47330c0982b8SRandall Stewart 			do_wakeup_routine = 1;
4734f8829a4aSRandall Stewart 			if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
4735f8829a4aSRandall Stewart 				stcb->asoc.sent_queue_cnt_removeable--;
4736f8829a4aSRandall Stewart 			}
4737f8829a4aSRandall Stewart 		}
47388933fa13SRandall Stewart 		tp1->sent = SCTP_FORWARD_TSN_SKIP;
4739f8829a4aSRandall Stewart 		if ((tp1->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) ==
4740f8829a4aSRandall Stewart 		    SCTP_DATA_NOT_FRAG) {
4741f8829a4aSRandall Stewart 			/* not frag'ed we ae done   */
4742f8829a4aSRandall Stewart 			notdone = 0;
4743f8829a4aSRandall Stewart 			foundeom = 1;
4744f8829a4aSRandall Stewart 		} else if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
4745f8829a4aSRandall Stewart 			/* end of frag, we are done */
4746f8829a4aSRandall Stewart 			notdone = 0;
4747f8829a4aSRandall Stewart 			foundeom = 1;
4748f8829a4aSRandall Stewart 		} else {
4749f8829a4aSRandall Stewart 			/*
4750f8829a4aSRandall Stewart 			 * Its a begin or middle piece, we must mark all of
4751f8829a4aSRandall Stewart 			 * it
4752f8829a4aSRandall Stewart 			 */
4753f8829a4aSRandall Stewart 			notdone = 1;
4754f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
4755f8829a4aSRandall Stewart 		}
4756f8829a4aSRandall Stewart 	} while (tp1 && notdone);
47570c0982b8SRandall Stewart 	if (foundeom == 0) {
4758f8829a4aSRandall Stewart 		/*
4759f8829a4aSRandall Stewart 		 * The multi-part message was scattered across the send and
4760f8829a4aSRandall Stewart 		 * sent queue.
4761f8829a4aSRandall Stewart 		 */
47624a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(tp1, &stcb->asoc.send_queue, sctp_next, tp2) {
47634a9ef3f8SMichael Tuexen 			if ((tp1->rec.data.stream_number != stream) ||
47644a9ef3f8SMichael Tuexen 			    (tp1->rec.data.stream_seq != seq)) {
47654a9ef3f8SMichael Tuexen 				break;
47664a9ef3f8SMichael Tuexen 			}
47670c0982b8SRandall Stewart 			/*
47680c0982b8SRandall Stewart 			 * save to chk in case we have some on stream out
47690c0982b8SRandall Stewart 			 * queue. If so and we have an un-transmitted one we
47700c0982b8SRandall Stewart 			 * don't have to fudge the TSN.
47710c0982b8SRandall Stewart 			 */
47720c0982b8SRandall Stewart 			chk = tp1;
47730c0982b8SRandall Stewart 			ret_sz += tp1->book_size;
47740c0982b8SRandall Stewart 			sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
47751edc9dbaSMichael Tuexen 			if (sent) {
47761edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
47771edc9dbaSMichael Tuexen 			} else {
47781edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
47791edc9dbaSMichael Tuexen 			}
47802f99457bSMichael Tuexen 			if (tp1->data) {
47810c0982b8SRandall Stewart 				sctp_m_freem(tp1->data);
47822f99457bSMichael Tuexen 				tp1->data = NULL;
47832f99457bSMichael Tuexen 			}
47848933fa13SRandall Stewart 			/* No flight involved here book the size to 0 */
47858933fa13SRandall Stewart 			tp1->book_size = 0;
47860c0982b8SRandall Stewart 			if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
47870c0982b8SRandall Stewart 				foundeom = 1;
4788f8829a4aSRandall Stewart 			}
47890c0982b8SRandall Stewart 			do_wakeup_routine = 1;
47900c0982b8SRandall Stewart 			tp1->sent = SCTP_FORWARD_TSN_SKIP;
47910c0982b8SRandall Stewart 			TAILQ_REMOVE(&stcb->asoc.send_queue, tp1, sctp_next);
47920c0982b8SRandall Stewart 			/*
47930c0982b8SRandall Stewart 			 * on to the sent queue so we can wait for it to be
47940c0982b8SRandall Stewart 			 * passed by.
47950c0982b8SRandall Stewart 			 */
47960c0982b8SRandall Stewart 			TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, tp1,
47970c0982b8SRandall Stewart 			    sctp_next);
47980c0982b8SRandall Stewart 			stcb->asoc.send_queue_cnt--;
47990c0982b8SRandall Stewart 			stcb->asoc.sent_queue_cnt++;
48000c0982b8SRandall Stewart 		}
48010c0982b8SRandall Stewart 	}
48020c0982b8SRandall Stewart 	if (foundeom == 0) {
48030c0982b8SRandall Stewart 		/*
48040c0982b8SRandall Stewart 		 * Still no eom found. That means there is stuff left on the
48050c0982b8SRandall Stewart 		 * stream out queue.. yuck.
48060c0982b8SRandall Stewart 		 */
48070c0982b8SRandall Stewart 		SCTP_TCB_SEND_LOCK(stcb);
4808f3b05218SMichael Tuexen 		strq = &stcb->asoc.strmout[stream];
4809f3b05218SMichael Tuexen 		sp = TAILQ_FIRST(&strq->outqueue);
4810f3b05218SMichael Tuexen 		if (sp != NULL) {
48110c0982b8SRandall Stewart 			sp->discard_rest = 1;
48120c0982b8SRandall Stewart 			/*
4813f3b05218SMichael Tuexen 			 * We may need to put a chunk on the queue that
4814f3b05218SMichael Tuexen 			 * holds the TSN that would have been sent with the
4815f3b05218SMichael Tuexen 			 * LAST bit.
48160c0982b8SRandall Stewart 			 */
48170c0982b8SRandall Stewart 			if (chk == NULL) {
48180c0982b8SRandall Stewart 				/* Yep, we have to */
48190c0982b8SRandall Stewart 				sctp_alloc_a_chunk(stcb, chk);
48200c0982b8SRandall Stewart 				if (chk == NULL) {
48210c0982b8SRandall Stewart 					/*
4822f3b05218SMichael Tuexen 					 * we are hosed. All we can do is
4823f3b05218SMichael Tuexen 					 * nothing.. which will cause an
4824f3b05218SMichael Tuexen 					 * abort if the peer is paying
48250c0982b8SRandall Stewart 					 * attention.
48260c0982b8SRandall Stewart 					 */
48270c0982b8SRandall Stewart 					goto oh_well;
48280c0982b8SRandall Stewart 				}
48290c0982b8SRandall Stewart 				memset(chk, 0, sizeof(*chk));
48300c0982b8SRandall Stewart 				chk->rec.data.rcv_flags = SCTP_DATA_LAST_FRAG;
48310c0982b8SRandall Stewart 				chk->sent = SCTP_FORWARD_TSN_SKIP;
48320c0982b8SRandall Stewart 				chk->asoc = &stcb->asoc;
4833f3b05218SMichael Tuexen 				chk->rec.data.stream_seq = strq->next_sequence_send;
48340c0982b8SRandall Stewart 				chk->rec.data.stream_number = sp->stream;
48350c0982b8SRandall Stewart 				chk->rec.data.payloadtype = sp->ppid;
48360c0982b8SRandall Stewart 				chk->rec.data.context = sp->context;
48370c0982b8SRandall Stewart 				chk->flags = sp->act_flags;
48389eea4a2dSMichael Tuexen 				if (sp->net)
48390c0982b8SRandall Stewart 					chk->whoTo = sp->net;
48409eea4a2dSMichael Tuexen 				else
48419eea4a2dSMichael Tuexen 					chk->whoTo = stcb->asoc.primary_destination;
48420c0982b8SRandall Stewart 				atomic_add_int(&chk->whoTo->ref_count, 1);
48430c0982b8SRandall Stewart 				chk->rec.data.TSN_seq = atomic_fetchadd_int(&stcb->asoc.sending_seq, 1);
48440c0982b8SRandall Stewart 				stcb->asoc.pr_sctp_cnt++;
48450c0982b8SRandall Stewart 				TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, chk, sctp_next);
48460c0982b8SRandall Stewart 				stcb->asoc.sent_queue_cnt++;
48478933fa13SRandall Stewart 				stcb->asoc.pr_sctp_cnt++;
48480c0982b8SRandall Stewart 			} else {
48490c0982b8SRandall Stewart 				chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
48500c0982b8SRandall Stewart 			}
4851f3b05218SMichael Tuexen 			strq->next_sequence_send++;
48520c0982b8SRandall Stewart 	oh_well:
48530c0982b8SRandall Stewart 			if (sp->data) {
48540c0982b8SRandall Stewart 				/*
4855f3b05218SMichael Tuexen 				 * Pull any data to free up the SB and allow
4856f3b05218SMichael Tuexen 				 * sender to "add more" while we will throw
4857f3b05218SMichael Tuexen 				 * away :-)
48580c0982b8SRandall Stewart 				 */
4859f3b05218SMichael Tuexen 				sctp_free_spbufspace(stcb, &stcb->asoc, sp);
48600c0982b8SRandall Stewart 				ret_sz += sp->length;
48610c0982b8SRandall Stewart 				do_wakeup_routine = 1;
48620c0982b8SRandall Stewart 				sp->some_taken = 1;
48630c0982b8SRandall Stewart 				sctp_m_freem(sp->data);
48640c0982b8SRandall Stewart 				sp->data = NULL;
48650c0982b8SRandall Stewart 				sp->tail_mbuf = NULL;
4866d07b2ac6SMichael Tuexen 				sp->length = 0;
48670c0982b8SRandall Stewart 			}
48680c0982b8SRandall Stewart 		}
48690c0982b8SRandall Stewart 		SCTP_TCB_SEND_UNLOCK(stcb);
48700c0982b8SRandall Stewart 	}
48710c0982b8SRandall Stewart 	if (do_wakeup_routine) {
48720c0982b8SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
48738933fa13SRandall Stewart 		struct socket *so;
48748933fa13SRandall Stewart 
48750c0982b8SRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
48760c0982b8SRandall Stewart 		if (!so_locked) {
48770c0982b8SRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, 1);
48780c0982b8SRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
48790c0982b8SRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
48800c0982b8SRandall Stewart 			SCTP_TCB_LOCK(stcb);
48810c0982b8SRandall Stewart 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
48820c0982b8SRandall Stewart 			if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
48830c0982b8SRandall Stewart 				/* assoc was freed while we were unlocked */
48840c0982b8SRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
48850c0982b8SRandall Stewart 				return (ret_sz);
48860c0982b8SRandall Stewart 			}
48870c0982b8SRandall Stewart 		}
48880c0982b8SRandall Stewart #endif
48890c0982b8SRandall Stewart 		sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
48900c0982b8SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
48910c0982b8SRandall Stewart 		if (!so_locked) {
48920c0982b8SRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
48930c0982b8SRandall Stewart 		}
48940c0982b8SRandall Stewart #endif
4895f8829a4aSRandall Stewart 	}
4896f8829a4aSRandall Stewart 	return (ret_sz);
4897f8829a4aSRandall Stewart }
4898f8829a4aSRandall Stewart 
4899f8829a4aSRandall Stewart /*
4900f8829a4aSRandall Stewart  * checks to see if the given address, sa, is one that is currently known by
4901f8829a4aSRandall Stewart  * the kernel note: can't distinguish the same address on multiple interfaces
4902f8829a4aSRandall Stewart  * and doesn't handle multiple addresses with different zone/scope id's note:
4903f8829a4aSRandall Stewart  * ifa_ifwithaddr() compares the entire sockaddr struct
4904f8829a4aSRandall Stewart  */
490542551e99SRandall Stewart struct sctp_ifa *
490680fefe0aSRandall Stewart sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr,
490780fefe0aSRandall Stewart     int holds_lock)
4908f8829a4aSRandall Stewart {
490942551e99SRandall Stewart 	struct sctp_laddr *laddr;
4910f8829a4aSRandall Stewart 
4911ad81507eSRandall Stewart 	if (holds_lock == 0) {
491242551e99SRandall Stewart 		SCTP_INP_RLOCK(inp);
4913ad81507eSRandall Stewart 	}
491442551e99SRandall Stewart 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
491542551e99SRandall Stewart 		if (laddr->ifa == NULL)
4916f8829a4aSRandall Stewart 			continue;
491742551e99SRandall Stewart 		if (addr->sa_family != laddr->ifa->address.sa.sa_family)
491842551e99SRandall Stewart 			continue;
4919e6194c2eSMichael Tuexen #ifdef INET
492042551e99SRandall Stewart 		if (addr->sa_family == AF_INET) {
492142551e99SRandall Stewart 			if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
492242551e99SRandall Stewart 			    laddr->ifa->address.sin.sin_addr.s_addr) {
492342551e99SRandall Stewart 				/* found him. */
4924ad81507eSRandall Stewart 				if (holds_lock == 0) {
492542551e99SRandall Stewart 					SCTP_INP_RUNLOCK(inp);
4926ad81507eSRandall Stewart 				}
492742551e99SRandall Stewart 				return (laddr->ifa);
492842551e99SRandall Stewart 				break;
492942551e99SRandall Stewart 			}
49305e2c2d87SRandall Stewart 		}
4931e6194c2eSMichael Tuexen #endif
49325e2c2d87SRandall Stewart #ifdef INET6
49335e2c2d87SRandall Stewart 		if (addr->sa_family == AF_INET6) {
4934c54a18d2SRandall Stewart 			if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
4935c54a18d2SRandall Stewart 			    &laddr->ifa->address.sin6)) {
493642551e99SRandall Stewart 				/* found him. */
4937ad81507eSRandall Stewart 				if (holds_lock == 0) {
493842551e99SRandall Stewart 					SCTP_INP_RUNLOCK(inp);
4939ad81507eSRandall Stewart 				}
494042551e99SRandall Stewart 				return (laddr->ifa);
494142551e99SRandall Stewart 				break;
494242551e99SRandall Stewart 			}
494342551e99SRandall Stewart 		}
49445e2c2d87SRandall Stewart #endif
494542551e99SRandall Stewart 	}
4946ad81507eSRandall Stewart 	if (holds_lock == 0) {
494742551e99SRandall Stewart 		SCTP_INP_RUNLOCK(inp);
4948ad81507eSRandall Stewart 	}
494942551e99SRandall Stewart 	return (NULL);
495042551e99SRandall Stewart }
4951f8829a4aSRandall Stewart 
49526a27c376SRandall Stewart uint32_t
49536a27c376SRandall Stewart sctp_get_ifa_hash_val(struct sockaddr *addr)
49546a27c376SRandall Stewart {
4955ea5eba11SMichael Tuexen 	switch (addr->sa_family) {
4956ea5eba11SMichael Tuexen #ifdef INET
4957ea5eba11SMichael Tuexen 	case AF_INET:
4958ea5eba11SMichael Tuexen 		{
49596a27c376SRandall Stewart 			struct sockaddr_in *sin;
49606a27c376SRandall Stewart 
49616a27c376SRandall Stewart 			sin = (struct sockaddr_in *)addr;
49626a27c376SRandall Stewart 			return (sin->sin_addr.s_addr ^ (sin->sin_addr.s_addr >> 16));
4963ea5eba11SMichael Tuexen 		}
4964ea5eba11SMichael Tuexen #endif
4965ea5eba11SMichael Tuexen #ifdef INET6
49662c2e3218SMichael Tuexen 	case AF_INET6:
4967ea5eba11SMichael Tuexen 		{
49686a27c376SRandall Stewart 			struct sockaddr_in6 *sin6;
49696a27c376SRandall Stewart 			uint32_t hash_of_addr;
49706a27c376SRandall Stewart 
49716a27c376SRandall Stewart 			sin6 = (struct sockaddr_in6 *)addr;
49726a27c376SRandall Stewart 			hash_of_addr = (sin6->sin6_addr.s6_addr32[0] +
49736a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[1] +
49746a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[2] +
49756a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[3]);
49766a27c376SRandall Stewart 			hash_of_addr = (hash_of_addr ^ (hash_of_addr >> 16));
49776a27c376SRandall Stewart 			return (hash_of_addr);
49786a27c376SRandall Stewart 		}
4979ea5eba11SMichael Tuexen #endif
4980ea5eba11SMichael Tuexen 	default:
4981ea5eba11SMichael Tuexen 		break;
4982ea5eba11SMichael Tuexen 	}
49836a27c376SRandall Stewart 	return (0);
49846a27c376SRandall Stewart }
49856a27c376SRandall Stewart 
498642551e99SRandall Stewart struct sctp_ifa *
498742551e99SRandall Stewart sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock)
498842551e99SRandall Stewart {
498942551e99SRandall Stewart 	struct sctp_ifa *sctp_ifap;
499042551e99SRandall Stewart 	struct sctp_vrf *vrf;
49916a27c376SRandall Stewart 	struct sctp_ifalist *hash_head;
49926a27c376SRandall Stewart 	uint32_t hash_of_addr;
499342551e99SRandall Stewart 
499442551e99SRandall Stewart 	if (holds_lock == 0)
4995c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RLOCK();
499642551e99SRandall Stewart 
4997bff64a4dSRandall Stewart 	vrf = sctp_find_vrf(vrf_id);
4998bff64a4dSRandall Stewart 	if (vrf == NULL) {
4999df6e0cc3SRandall Stewart stage_right:
5000bff64a4dSRandall Stewart 		if (holds_lock == 0)
5001c99efcf6SRandall Stewart 			SCTP_IPI_ADDR_RUNLOCK();
5002bff64a4dSRandall Stewart 		return (NULL);
5003bff64a4dSRandall Stewart 	}
5004bff64a4dSRandall Stewart 	hash_of_addr = sctp_get_ifa_hash_val(addr);
5005bff64a4dSRandall Stewart 
500617205eccSRandall Stewart 	hash_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
5007bff64a4dSRandall Stewart 	if (hash_head == NULL) {
5008ad81507eSRandall Stewart 		SCTP_PRINTF("hash_of_addr:%x mask:%x table:%x - ",
5009c99efcf6SRandall Stewart 		    hash_of_addr, (uint32_t) vrf->vrf_addr_hashmark,
5010c99efcf6SRandall Stewart 		    (uint32_t) (hash_of_addr & vrf->vrf_addr_hashmark));
5011bff64a4dSRandall Stewart 		sctp_print_address(addr);
5012ad81507eSRandall Stewart 		SCTP_PRINTF("No such bucket for address\n");
5013bff64a4dSRandall Stewart 		if (holds_lock == 0)
5014c99efcf6SRandall Stewart 			SCTP_IPI_ADDR_RUNLOCK();
5015bff64a4dSRandall Stewart 
5016bff64a4dSRandall Stewart 		return (NULL);
5017bff64a4dSRandall Stewart 	}
50186a27c376SRandall Stewart 	LIST_FOREACH(sctp_ifap, hash_head, next_bucket) {
5019bff64a4dSRandall Stewart 		if (sctp_ifap == NULL) {
5020df6e0cc3SRandall Stewart #ifdef INVARIANTS
5021bff64a4dSRandall Stewart 			panic("Huh LIST_FOREACH corrupt");
5022df6e0cc3SRandall Stewart 			goto stage_right;
5023df6e0cc3SRandall Stewart #else
5024df6e0cc3SRandall Stewart 			SCTP_PRINTF("LIST corrupt of sctp_ifap's?\n");
5025df6e0cc3SRandall Stewart 			goto stage_right;
5026df6e0cc3SRandall Stewart #endif
5027bff64a4dSRandall Stewart 		}
50286a27c376SRandall Stewart 		if (addr->sa_family != sctp_ifap->address.sa.sa_family)
50296a27c376SRandall Stewart 			continue;
5030e6194c2eSMichael Tuexen #ifdef INET
50316a27c376SRandall Stewart 		if (addr->sa_family == AF_INET) {
50326a27c376SRandall Stewart 			if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
50336a27c376SRandall Stewart 			    sctp_ifap->address.sin.sin_addr.s_addr) {
50346a27c376SRandall Stewart 				/* found him. */
503542551e99SRandall Stewart 				if (holds_lock == 0)
5036c99efcf6SRandall Stewart 					SCTP_IPI_ADDR_RUNLOCK();
503742551e99SRandall Stewart 				return (sctp_ifap);
50386a27c376SRandall Stewart 				break;
50396a27c376SRandall Stewart 			}
50405e2c2d87SRandall Stewart 		}
5041e6194c2eSMichael Tuexen #endif
50425e2c2d87SRandall Stewart #ifdef INET6
50435e2c2d87SRandall Stewart 		if (addr->sa_family == AF_INET6) {
5044c54a18d2SRandall Stewart 			if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5045c54a18d2SRandall Stewart 			    &sctp_ifap->address.sin6)) {
50466a27c376SRandall Stewart 				/* found him. */
50476a27c376SRandall Stewart 				if (holds_lock == 0)
5048c99efcf6SRandall Stewart 					SCTP_IPI_ADDR_RUNLOCK();
50496a27c376SRandall Stewart 				return (sctp_ifap);
50506a27c376SRandall Stewart 				break;
50516a27c376SRandall Stewart 			}
505242551e99SRandall Stewart 		}
50535e2c2d87SRandall Stewart #endif
505442551e99SRandall Stewart 	}
505542551e99SRandall Stewart 	if (holds_lock == 0)
5056c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RUNLOCK();
5057f8829a4aSRandall Stewart 	return (NULL);
5058f8829a4aSRandall Stewart }
5059f8829a4aSRandall Stewart 
5060f8829a4aSRandall Stewart static void
50614c9179adSRandall Stewart sctp_user_rcvd(struct sctp_tcb *stcb, uint32_t * freed_so_far, int hold_rlock,
5062f8829a4aSRandall Stewart     uint32_t rwnd_req)
5063f8829a4aSRandall Stewart {
5064f8829a4aSRandall Stewart 	/* User pulled some data, do we need a rwnd update? */
5065f8829a4aSRandall Stewart 	int r_unlocked = 0;
5066f8829a4aSRandall Stewart 	uint32_t dif, rwnd;
5067f8829a4aSRandall Stewart 	struct socket *so = NULL;
5068f8829a4aSRandall Stewart 
5069f8829a4aSRandall Stewart 	if (stcb == NULL)
5070f8829a4aSRandall Stewart 		return;
5071f8829a4aSRandall Stewart 
507250cec919SRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, 1);
5073f8829a4aSRandall Stewart 
507462c1ff9cSRandall Stewart 	if (stcb->asoc.state & (SCTP_STATE_ABOUT_TO_BE_FREED |
507562c1ff9cSRandall Stewart 	    SCTP_STATE_SHUTDOWN_RECEIVED |
50764c9179adSRandall Stewart 	    SCTP_STATE_SHUTDOWN_ACK_SENT)) {
5077f8829a4aSRandall Stewart 		/* Pre-check If we are freeing no update */
5078f8829a4aSRandall Stewart 		goto no_lock;
5079f8829a4aSRandall Stewart 	}
5080f8829a4aSRandall Stewart 	SCTP_INP_INCR_REF(stcb->sctp_ep);
5081f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5082f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5083f8829a4aSRandall Stewart 		goto out;
5084f8829a4aSRandall Stewart 	}
5085f8829a4aSRandall Stewart 	so = stcb->sctp_socket;
5086f8829a4aSRandall Stewart 	if (so == NULL) {
5087f8829a4aSRandall Stewart 		goto out;
5088f8829a4aSRandall Stewart 	}
5089f8829a4aSRandall Stewart 	atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far);
5090f8829a4aSRandall Stewart 	/* Have you have freed enough to look */
5091f8829a4aSRandall Stewart 	*freed_so_far = 0;
5092f8829a4aSRandall Stewart 	/* Yep, its worth a look and the lock overhead */
5093f8829a4aSRandall Stewart 
5094f8829a4aSRandall Stewart 	/* Figure out what the rwnd would be */
5095f8829a4aSRandall Stewart 	rwnd = sctp_calc_rwnd(stcb, &stcb->asoc);
5096f8829a4aSRandall Stewart 	if (rwnd >= stcb->asoc.my_last_reported_rwnd) {
5097f8829a4aSRandall Stewart 		dif = rwnd - stcb->asoc.my_last_reported_rwnd;
5098f8829a4aSRandall Stewart 	} else {
5099f8829a4aSRandall Stewart 		dif = 0;
5100f8829a4aSRandall Stewart 	}
5101f8829a4aSRandall Stewart 	if (dif >= rwnd_req) {
5102f8829a4aSRandall Stewart 		if (hold_rlock) {
5103f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5104f8829a4aSRandall Stewart 			r_unlocked = 1;
5105f8829a4aSRandall Stewart 		}
5106f8829a4aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5107f8829a4aSRandall Stewart 			/*
5108f8829a4aSRandall Stewart 			 * One last check before we allow the guy possibly
5109f8829a4aSRandall Stewart 			 * to get in. There is a race, where the guy has not
5110f8829a4aSRandall Stewart 			 * reached the gate. In that case
5111f8829a4aSRandall Stewart 			 */
5112f8829a4aSRandall Stewart 			goto out;
5113f8829a4aSRandall Stewart 		}
5114f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
5115f8829a4aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5116f8829a4aSRandall Stewart 			/* No reports here */
5117f8829a4aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
5118f8829a4aSRandall Stewart 			goto out;
5119f8829a4aSRandall Stewart 		}
5120f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_wu_sacks_sent);
5121689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_LOCKED);
5122830d754dSRandall Stewart 
5123f8829a4aSRandall Stewart 		sctp_chunk_output(stcb->sctp_ep, stcb,
5124ceaad40aSRandall Stewart 		    SCTP_OUTPUT_FROM_USR_RCVD, SCTP_SO_LOCKED);
5125f8829a4aSRandall Stewart 		/* make sure no timer is running */
5126a5d547adSRandall Stewart 		sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_6);
5127f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
5128f8829a4aSRandall Stewart 	} else {
5129f8829a4aSRandall Stewart 		/* Update how much we have pending */
5130f8829a4aSRandall Stewart 		stcb->freed_by_sorcv_sincelast = dif;
5131f8829a4aSRandall Stewart 	}
5132f8829a4aSRandall Stewart out:
5133f8829a4aSRandall Stewart 	if (so && r_unlocked && hold_rlock) {
5134f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
5135f8829a4aSRandall Stewart 	}
5136f8829a4aSRandall Stewart 	SCTP_INP_DECR_REF(stcb->sctp_ep);
5137f8829a4aSRandall Stewart no_lock:
513850cec919SRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, -1);
5139f8829a4aSRandall Stewart 	return;
5140f8829a4aSRandall Stewart }
5141f8829a4aSRandall Stewart 
5142f8829a4aSRandall Stewart int
5143f8829a4aSRandall Stewart sctp_sorecvmsg(struct socket *so,
5144f8829a4aSRandall Stewart     struct uio *uio,
5145f8829a4aSRandall Stewart     struct mbuf **mp,
5146f8829a4aSRandall Stewart     struct sockaddr *from,
5147f8829a4aSRandall Stewart     int fromlen,
5148f8829a4aSRandall Stewart     int *msg_flags,
5149f8829a4aSRandall Stewart     struct sctp_sndrcvinfo *sinfo,
5150f8829a4aSRandall Stewart     int filling_sinfo)
5151f8829a4aSRandall Stewart {
5152f8829a4aSRandall Stewart 	/*
5153f8829a4aSRandall Stewart 	 * MSG flags we will look at MSG_DONTWAIT - non-blocking IO.
5154f8829a4aSRandall Stewart 	 * MSG_PEEK - Look don't touch :-D (only valid with OUT mbuf copy
5155f8829a4aSRandall Stewart 	 * mp=NULL thus uio is the copy method to userland) MSG_WAITALL - ??
5156f8829a4aSRandall Stewart 	 * On the way out we may send out any combination of:
5157f8829a4aSRandall Stewart 	 * MSG_NOTIFICATION MSG_EOR
5158f8829a4aSRandall Stewart 	 *
5159f8829a4aSRandall Stewart 	 */
5160f8829a4aSRandall Stewart 	struct sctp_inpcb *inp = NULL;
5161f8829a4aSRandall Stewart 	int my_len = 0;
5162f8829a4aSRandall Stewart 	int cp_len = 0, error = 0;
5163f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL;
516494b0d969SMichael Tuexen 	struct mbuf *m = NULL;
5165f8829a4aSRandall Stewart 	struct sctp_tcb *stcb = NULL;
5166f8829a4aSRandall Stewart 	int wakeup_read_socket = 0;
5167f8829a4aSRandall Stewart 	int freecnt_applied = 0;
5168f8829a4aSRandall Stewart 	int out_flags = 0, in_flags = 0;
5169f8829a4aSRandall Stewart 	int block_allowed = 1;
51704c9179adSRandall Stewart 	uint32_t freed_so_far = 0;
517181aca91aSRandall Stewart 	uint32_t copied_so_far = 0;
517293164cf9SRandall Stewart 	int in_eeor_mode = 0;
5173f8829a4aSRandall Stewart 	int no_rcv_needed = 0;
5174f8829a4aSRandall Stewart 	uint32_t rwnd_req = 0;
5175f8829a4aSRandall Stewart 	int hold_sblock = 0;
5176f8829a4aSRandall Stewart 	int hold_rlock = 0;
517742551e99SRandall Stewart 	int slen = 0;
51784c9179adSRandall Stewart 	uint32_t held_length = 0;
51797abab911SRobert Watson 	int sockbuf_lock = 0;
5180f8829a4aSRandall Stewart 
518117205eccSRandall Stewart 	if (uio == NULL) {
5182c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
518317205eccSRandall Stewart 		return (EINVAL);
518417205eccSRandall Stewart 	}
5185f8829a4aSRandall Stewart 	if (msg_flags) {
5186f8829a4aSRandall Stewart 		in_flags = *msg_flags;
5187c105859eSRandall Stewart 		if (in_flags & MSG_PEEK)
5188c105859eSRandall Stewart 			SCTP_STAT_INCR(sctps_read_peeks);
5189f8829a4aSRandall Stewart 	} else {
5190f8829a4aSRandall Stewart 		in_flags = 0;
5191f8829a4aSRandall Stewart 	}
5192f8829a4aSRandall Stewart 	slen = uio->uio_resid;
519317205eccSRandall Stewart 
5194f8829a4aSRandall Stewart 	/* Pull in and set up our int flags */
5195f8829a4aSRandall Stewart 	if (in_flags & MSG_OOB) {
5196f8829a4aSRandall Stewart 		/* Out of band's NOT supported */
5197f8829a4aSRandall Stewart 		return (EOPNOTSUPP);
5198f8829a4aSRandall Stewart 	}
5199f8829a4aSRandall Stewart 	if ((in_flags & MSG_PEEK) && (mp != NULL)) {
5200c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5201f8829a4aSRandall Stewart 		return (EINVAL);
5202f8829a4aSRandall Stewart 	}
5203f8829a4aSRandall Stewart 	if ((in_flags & (MSG_DONTWAIT
5204f8829a4aSRandall Stewart 	    | MSG_NBIO
5205f8829a4aSRandall Stewart 	    )) ||
520642551e99SRandall Stewart 	    SCTP_SO_IS_NBIO(so)) {
5207f8829a4aSRandall Stewart 		block_allowed = 0;
5208f8829a4aSRandall Stewart 	}
5209f8829a4aSRandall Stewart 	/* setup the endpoint */
5210f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
5211f8829a4aSRandall Stewart 	if (inp == NULL) {
5212c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
5213f8829a4aSRandall Stewart 		return (EFAULT);
5214f8829a4aSRandall Stewart 	}
521562c1ff9cSRandall Stewart 	rwnd_req = (SCTP_SB_LIMIT_RCV(so) >> SCTP_RWND_HIWAT_SHIFT);
5216f8829a4aSRandall Stewart 	/* Must be at least a MTU's worth */
5217f8829a4aSRandall Stewart 	if (rwnd_req < SCTP_MIN_RWND)
5218f8829a4aSRandall Stewart 		rwnd_req = SCTP_MIN_RWND;
5219f8829a4aSRandall Stewart 	in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
5220b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5221f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SORECV_ENTER,
522217205eccSRandall Stewart 		    rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, uio->uio_resid);
522380fefe0aSRandall Stewart 	}
5224b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5225f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SORECV_ENTERPL,
522617205eccSRandall Stewart 		    rwnd_req, block_allowed, so->so_rcv.sb_cc, uio->uio_resid);
522780fefe0aSRandall Stewart 	}
5228265de5bbSRobert Watson 	error = sblock(&so->so_rcv, (block_allowed ? SBL_WAIT : 0));
5229f8829a4aSRandall Stewart 	if (error) {
5230f8829a4aSRandall Stewart 		goto release_unlocked;
5231f8829a4aSRandall Stewart 	}
52328e1e6e5fSMateusz Guzik 	sockbuf_lock = 1;
5233f8829a4aSRandall Stewart restart:
52347abab911SRobert Watson 
5235f8829a4aSRandall Stewart 
5236f8829a4aSRandall Stewart restart_nosblocks:
5237f8829a4aSRandall Stewart 	if (hold_sblock == 0) {
5238f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&so->so_rcv);
5239f8829a4aSRandall Stewart 		hold_sblock = 1;
5240f8829a4aSRandall Stewart 	}
5241f8829a4aSRandall Stewart 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5242f8829a4aSRandall Stewart 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5243f8829a4aSRandall Stewart 		goto out;
5244f8829a4aSRandall Stewart 	}
524558411b08SMichael Tuexen 	if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) && (so->so_rcv.sb_cc == 0)) {
5246f8829a4aSRandall Stewart 		if (so->so_error) {
5247f8829a4aSRandall Stewart 			error = so->so_error;
524844b7479bSRandall Stewart 			if ((in_flags & MSG_PEEK) == 0)
524944b7479bSRandall Stewart 				so->so_error = 0;
52509f22f500SRandall Stewart 			goto out;
5251f8829a4aSRandall Stewart 		} else {
52529f22f500SRandall Stewart 			if (so->so_rcv.sb_cc == 0) {
52537924093fSRandall Stewart 				/* indicate EOF */
52547924093fSRandall Stewart 				error = 0;
5255f8829a4aSRandall Stewart 				goto out;
5256f8829a4aSRandall Stewart 			}
52579f22f500SRandall Stewart 		}
52589f22f500SRandall Stewart 	}
5259f8829a4aSRandall Stewart 	if ((so->so_rcv.sb_cc <= held_length) && block_allowed) {
5260f8829a4aSRandall Stewart 		/* we need to wait for data */
5261f8829a4aSRandall Stewart 		if ((so->so_rcv.sb_cc == 0) &&
5262f8829a4aSRandall Stewart 		    ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5263f8829a4aSRandall Stewart 		    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
5264f8829a4aSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
5265f8829a4aSRandall Stewart 				/*
5266f8829a4aSRandall Stewart 				 * For active open side clear flags for
5267f8829a4aSRandall Stewart 				 * re-use passive open is blocked by
5268f8829a4aSRandall Stewart 				 * connect.
5269f8829a4aSRandall Stewart 				 */
5270f8829a4aSRandall Stewart 				if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
5271f8829a4aSRandall Stewart 					/*
5272f8829a4aSRandall Stewart 					 * You were aborted, passive side
5273f8829a4aSRandall Stewart 					 * always hits here
5274f8829a4aSRandall Stewart 					 */
5275c4739e2fSRandall Stewart 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
5276f8829a4aSRandall Stewart 					error = ECONNRESET;
5277f8829a4aSRandall Stewart 				}
5278f8829a4aSRandall Stewart 				so->so_state &= ~(SS_ISCONNECTING |
5279f8829a4aSRandall Stewart 				    SS_ISDISCONNECTING |
5280f8829a4aSRandall Stewart 				    SS_ISCONFIRMING |
5281f8829a4aSRandall Stewart 				    SS_ISCONNECTED);
5282f8829a4aSRandall Stewart 				if (error == 0) {
5283f8829a4aSRandall Stewart 					if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5284c4739e2fSRandall Stewart 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5285f8829a4aSRandall Stewart 						error = ENOTCONN;
5286f8829a4aSRandall Stewart 					}
5287f8829a4aSRandall Stewart 				}
5288f8829a4aSRandall Stewart 				goto out;
5289f8829a4aSRandall Stewart 			}
5290f8829a4aSRandall Stewart 		}
5291f8829a4aSRandall Stewart 		error = sbwait(&so->so_rcv);
5292f8829a4aSRandall Stewart 		if (error) {
5293f8829a4aSRandall Stewart 			goto out;
5294f8829a4aSRandall Stewart 		}
5295f8829a4aSRandall Stewart 		held_length = 0;
5296f8829a4aSRandall Stewart 		goto restart_nosblocks;
5297f8829a4aSRandall Stewart 	} else if (so->so_rcv.sb_cc == 0) {
529844b7479bSRandall Stewart 		if (so->so_error) {
529944b7479bSRandall Stewart 			error = so->so_error;
530044b7479bSRandall Stewart 			if ((in_flags & MSG_PEEK) == 0)
530144b7479bSRandall Stewart 				so->so_error = 0;
530244b7479bSRandall Stewart 		} else {
530344b7479bSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
530444b7479bSRandall Stewart 			    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
530544b7479bSRandall Stewart 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
530644b7479bSRandall Stewart 					/*
530744b7479bSRandall Stewart 					 * For active open side clear flags
530844b7479bSRandall Stewart 					 * for re-use passive open is
530944b7479bSRandall Stewart 					 * blocked by connect.
531044b7479bSRandall Stewart 					 */
531144b7479bSRandall Stewart 					if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
531244b7479bSRandall Stewart 						/*
531344b7479bSRandall Stewart 						 * You were aborted, passive
531444b7479bSRandall Stewart 						 * side always hits here
531544b7479bSRandall Stewart 						 */
5316c4739e2fSRandall Stewart 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
531744b7479bSRandall Stewart 						error = ECONNRESET;
531844b7479bSRandall Stewart 					}
531944b7479bSRandall Stewart 					so->so_state &= ~(SS_ISCONNECTING |
532044b7479bSRandall Stewart 					    SS_ISDISCONNECTING |
532144b7479bSRandall Stewart 					    SS_ISCONFIRMING |
532244b7479bSRandall Stewart 					    SS_ISCONNECTED);
532344b7479bSRandall Stewart 					if (error == 0) {
532444b7479bSRandall Stewart 						if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5325c4739e2fSRandall Stewart 							SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
532644b7479bSRandall Stewart 							error = ENOTCONN;
532744b7479bSRandall Stewart 						}
532844b7479bSRandall Stewart 					}
532944b7479bSRandall Stewart 					goto out;
533044b7479bSRandall Stewart 				}
533144b7479bSRandall Stewart 			}
5332c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EWOULDBLOCK);
5333f8829a4aSRandall Stewart 			error = EWOULDBLOCK;
533444b7479bSRandall Stewart 		}
5335f8829a4aSRandall Stewart 		goto out;
5336f8829a4aSRandall Stewart 	}
5337d06c82f1SRandall Stewart 	if (hold_sblock == 1) {
5338d06c82f1SRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
5339d06c82f1SRandall Stewart 		hold_sblock = 0;
5340d06c82f1SRandall Stewart 	}
5341f8829a4aSRandall Stewart 	/* we possibly have data we can read */
53423c503c28SRandall Stewart 	/* sa_ignore FREED_MEMORY */
5343f8829a4aSRandall Stewart 	control = TAILQ_FIRST(&inp->read_queue);
5344f8829a4aSRandall Stewart 	if (control == NULL) {
5345f8829a4aSRandall Stewart 		/*
5346f8829a4aSRandall Stewart 		 * This could be happening since the appender did the
5347f8829a4aSRandall Stewart 		 * increment but as not yet did the tailq insert onto the
5348f8829a4aSRandall Stewart 		 * read_queue
5349f8829a4aSRandall Stewart 		 */
5350f8829a4aSRandall Stewart 		if (hold_rlock == 0) {
5351f8829a4aSRandall Stewart 			SCTP_INP_READ_LOCK(inp);
5352f8829a4aSRandall Stewart 		}
5353f8829a4aSRandall Stewart 		control = TAILQ_FIRST(&inp->read_queue);
5354f8829a4aSRandall Stewart 		if ((control == NULL) && (so->so_rcv.sb_cc != 0)) {
5355a5d547adSRandall Stewart #ifdef INVARIANTS
5356f8829a4aSRandall Stewart 			panic("Huh, its non zero and nothing on control?");
5357f8829a4aSRandall Stewart #endif
5358f8829a4aSRandall Stewart 			so->so_rcv.sb_cc = 0;
5359f8829a4aSRandall Stewart 		}
5360f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
5361f8829a4aSRandall Stewart 		hold_rlock = 0;
5362f8829a4aSRandall Stewart 		goto restart;
5363f8829a4aSRandall Stewart 	}
5364f8829a4aSRandall Stewart 	if ((control->length == 0) &&
5365f8829a4aSRandall Stewart 	    (control->do_not_ref_stcb)) {
5366f8829a4aSRandall Stewart 		/*
5367f8829a4aSRandall Stewart 		 * Clean up code for freeing assoc that left behind a
5368f8829a4aSRandall Stewart 		 * pdapi.. maybe a peer in EEOR that just closed after
5369f8829a4aSRandall Stewart 		 * sending and never indicated a EOR.
5370f8829a4aSRandall Stewart 		 */
5371f8829a4aSRandall Stewart 		if (hold_rlock == 0) {
5372f8829a4aSRandall Stewart 			hold_rlock = 1;
5373f8829a4aSRandall Stewart 			SCTP_INP_READ_LOCK(inp);
5374f8829a4aSRandall Stewart 		}
5375f8829a4aSRandall Stewart 		control->held_length = 0;
5376f8829a4aSRandall Stewart 		if (control->data) {
5377f8829a4aSRandall Stewart 			/* Hmm there is data here .. fix */
53784c9179adSRandall Stewart 			struct mbuf *m_tmp;
5379f8829a4aSRandall Stewart 			int cnt = 0;
5380f8829a4aSRandall Stewart 
53814c9179adSRandall Stewart 			m_tmp = control->data;
53824c9179adSRandall Stewart 			while (m_tmp) {
53834c9179adSRandall Stewart 				cnt += SCTP_BUF_LEN(m_tmp);
53844c9179adSRandall Stewart 				if (SCTP_BUF_NEXT(m_tmp) == NULL) {
53854c9179adSRandall Stewart 					control->tail_mbuf = m_tmp;
5386f8829a4aSRandall Stewart 					control->end_added = 1;
5387f8829a4aSRandall Stewart 				}
53884c9179adSRandall Stewart 				m_tmp = SCTP_BUF_NEXT(m_tmp);
5389f8829a4aSRandall Stewart 			}
5390f8829a4aSRandall Stewart 			control->length = cnt;
5391f8829a4aSRandall Stewart 		} else {
5392f8829a4aSRandall Stewart 			/* remove it */
5393f8829a4aSRandall Stewart 			TAILQ_REMOVE(&inp->read_queue, control, next);
5394f8829a4aSRandall Stewart 			/* Add back any hiddend data */
5395f8829a4aSRandall Stewart 			sctp_free_remote_addr(control->whoFrom);
5396f8829a4aSRandall Stewart 			sctp_free_a_readq(stcb, control);
5397f8829a4aSRandall Stewart 		}
5398f8829a4aSRandall Stewart 		if (hold_rlock) {
5399f8829a4aSRandall Stewart 			hold_rlock = 0;
5400f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
5401f8829a4aSRandall Stewart 		}
5402f8829a4aSRandall Stewart 		goto restart;
5403f8829a4aSRandall Stewart 	}
5404810ec536SMichael Tuexen 	if ((control->length == 0) &&
5405810ec536SMichael Tuexen 	    (control->end_added == 1)) {
5406810ec536SMichael Tuexen 		/*
5407810ec536SMichael Tuexen 		 * Do we also need to check for (control->pdapi_aborted ==
5408810ec536SMichael Tuexen 		 * 1)?
5409810ec536SMichael Tuexen 		 */
5410810ec536SMichael Tuexen 		if (hold_rlock == 0) {
5411810ec536SMichael Tuexen 			hold_rlock = 1;
5412810ec536SMichael Tuexen 			SCTP_INP_READ_LOCK(inp);
5413810ec536SMichael Tuexen 		}
5414810ec536SMichael Tuexen 		TAILQ_REMOVE(&inp->read_queue, control, next);
5415810ec536SMichael Tuexen 		if (control->data) {
5416810ec536SMichael Tuexen #ifdef INVARIANTS
5417810ec536SMichael Tuexen 			panic("control->data not null but control->length == 0");
5418810ec536SMichael Tuexen #else
5419810ec536SMichael Tuexen 			SCTP_PRINTF("Strange, data left in the control buffer. Cleaning up.\n");
5420810ec536SMichael Tuexen 			sctp_m_freem(control->data);
5421810ec536SMichael Tuexen 			control->data = NULL;
5422810ec536SMichael Tuexen #endif
5423810ec536SMichael Tuexen 		}
5424810ec536SMichael Tuexen 		if (control->aux_data) {
5425810ec536SMichael Tuexen 			sctp_m_free(control->aux_data);
5426810ec536SMichael Tuexen 			control->aux_data = NULL;
5427810ec536SMichael Tuexen 		}
5428810ec536SMichael Tuexen 		sctp_free_remote_addr(control->whoFrom);
5429810ec536SMichael Tuexen 		sctp_free_a_readq(stcb, control);
5430810ec536SMichael Tuexen 		if (hold_rlock) {
5431810ec536SMichael Tuexen 			hold_rlock = 0;
5432810ec536SMichael Tuexen 			SCTP_INP_READ_UNLOCK(inp);
5433810ec536SMichael Tuexen 		}
5434810ec536SMichael Tuexen 		goto restart;
5435810ec536SMichael Tuexen 	}
5436f8829a4aSRandall Stewart 	if (control->length == 0) {
5437f8829a4aSRandall Stewart 		if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
5438f8829a4aSRandall Stewart 		    (filling_sinfo)) {
5439f8829a4aSRandall Stewart 			/* find a more suitable one then this */
5440f8829a4aSRandall Stewart 			ctl = TAILQ_NEXT(control, next);
5441f8829a4aSRandall Stewart 			while (ctl) {
54429a6142d8SRandall Stewart 				if ((ctl->stcb != control->stcb) && (ctl->length) &&
54439a6142d8SRandall Stewart 				    (ctl->some_taken ||
54446114cd96SRandall Stewart 				    (ctl->spec_flags & M_NOTIFICATION) ||
54459a6142d8SRandall Stewart 				    ((ctl->do_not_ref_stcb == 0) &&
54469a6142d8SRandall Stewart 				    (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))
54479a6142d8SRandall Stewart 				    ) {
54489a6142d8SRandall Stewart 					/*-
54499a6142d8SRandall Stewart 					 * If we have a different TCB next, and there is data
54509a6142d8SRandall Stewart 					 * present. If we have already taken some (pdapi), OR we can
54519a6142d8SRandall Stewart 					 * ref the tcb and no delivery as started on this stream, we
545217205eccSRandall Stewart 					 * take it. Note we allow a notification on a different
545317205eccSRandall Stewart 					 * assoc to be delivered..
54549a6142d8SRandall Stewart 					 */
54559a6142d8SRandall Stewart 					control = ctl;
54569a6142d8SRandall Stewart 					goto found_one;
54579a6142d8SRandall Stewart 				} else if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) &&
54589a6142d8SRandall Stewart 					    (ctl->length) &&
54599a6142d8SRandall Stewart 					    ((ctl->some_taken) ||
54609a6142d8SRandall Stewart 					    ((ctl->do_not_ref_stcb == 0) &&
546117205eccSRandall Stewart 					    ((ctl->spec_flags & M_NOTIFICATION) == 0) &&
5462b5c16493SMichael Tuexen 				    (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))) {
54639a6142d8SRandall Stewart 					/*-
54649a6142d8SRandall Stewart 					 * If we have the same tcb, and there is data present, and we
54659a6142d8SRandall Stewart 					 * have the strm interleave feature present. Then if we have
54669a6142d8SRandall Stewart 					 * taken some (pdapi) or we can refer to tht tcb AND we have
54679a6142d8SRandall Stewart 					 * not started a delivery for this stream, we can take it.
546817205eccSRandall Stewart 					 * Note we do NOT allow a notificaiton on the same assoc to
546917205eccSRandall Stewart 					 * be delivered.
54709a6142d8SRandall Stewart 					 */
5471f8829a4aSRandall Stewart 					control = ctl;
5472f8829a4aSRandall Stewart 					goto found_one;
5473f8829a4aSRandall Stewart 				}
5474f8829a4aSRandall Stewart 				ctl = TAILQ_NEXT(ctl, next);
5475f8829a4aSRandall Stewart 			}
5476f8829a4aSRandall Stewart 		}
5477f8829a4aSRandall Stewart 		/*
5478f8829a4aSRandall Stewart 		 * if we reach here, not suitable replacement is available
5479f8829a4aSRandall Stewart 		 * <or> fragment interleave is NOT on. So stuff the sb_cc
5480f8829a4aSRandall Stewart 		 * into the our held count, and its time to sleep again.
5481f8829a4aSRandall Stewart 		 */
5482f8829a4aSRandall Stewart 		held_length = so->so_rcv.sb_cc;
5483f8829a4aSRandall Stewart 		control->held_length = so->so_rcv.sb_cc;
5484f8829a4aSRandall Stewart 		goto restart;
5485f8829a4aSRandall Stewart 	}
5486f8829a4aSRandall Stewart 	/* Clear the held length since there is something to read */
5487f8829a4aSRandall Stewart 	control->held_length = 0;
5488f8829a4aSRandall Stewart 	if (hold_rlock) {
5489f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
5490f8829a4aSRandall Stewart 		hold_rlock = 0;
5491f8829a4aSRandall Stewart 	}
5492f8829a4aSRandall Stewart found_one:
5493f8829a4aSRandall Stewart 	/*
5494f8829a4aSRandall Stewart 	 * If we reach here, control has a some data for us to read off.
5495f8829a4aSRandall Stewart 	 * Note that stcb COULD be NULL.
5496f8829a4aSRandall Stewart 	 */
54979c04b296SRandall Stewart 	control->some_taken++;
5498f8829a4aSRandall Stewart 	if (hold_sblock) {
5499f8829a4aSRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
5500f8829a4aSRandall Stewart 		hold_sblock = 0;
5501f8829a4aSRandall Stewart 	}
5502f8829a4aSRandall Stewart 	stcb = control->stcb;
5503f8829a4aSRandall Stewart 	if (stcb) {
55040696e120SRandall Stewart 		if ((control->do_not_ref_stcb == 0) &&
55050696e120SRandall Stewart 		    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
550650cec919SRandall Stewart 			if (freecnt_applied == 0)
5507f8829a4aSRandall Stewart 				stcb = NULL;
5508f8829a4aSRandall Stewart 		} else if (control->do_not_ref_stcb == 0) {
5509f8829a4aSRandall Stewart 			/* you can't free it on me please */
5510f8829a4aSRandall Stewart 			/*
5511f8829a4aSRandall Stewart 			 * The lock on the socket buffer protects us so the
5512f8829a4aSRandall Stewart 			 * free code will stop. But since we used the
5513f8829a4aSRandall Stewart 			 * socketbuf lock and the sender uses the tcb_lock
5514f8829a4aSRandall Stewart 			 * to increment, we need to use the atomic add to
5515f8829a4aSRandall Stewart 			 * the refcnt
5516f8829a4aSRandall Stewart 			 */
5517d55b0b1bSRandall Stewart 			if (freecnt_applied) {
5518d55b0b1bSRandall Stewart #ifdef INVARIANTS
5519207304d4SRandall Stewart 				panic("refcnt already incremented");
5520d55b0b1bSRandall Stewart #else
5521cd3fd531SMichael Tuexen 				SCTP_PRINTF("refcnt already incremented?\n");
5522d55b0b1bSRandall Stewart #endif
5523d55b0b1bSRandall Stewart 			} else {
552450cec919SRandall Stewart 				atomic_add_int(&stcb->asoc.refcnt, 1);
5525f8829a4aSRandall Stewart 				freecnt_applied = 1;
5526d55b0b1bSRandall Stewart 			}
5527f8829a4aSRandall Stewart 			/*
5528f8829a4aSRandall Stewart 			 * Setup to remember how much we have not yet told
5529f8829a4aSRandall Stewart 			 * the peer our rwnd has opened up. Note we grab the
5530f8829a4aSRandall Stewart 			 * value from the tcb from last time. Note too that
55310696e120SRandall Stewart 			 * sack sending clears this when a sack is sent,
5532f8829a4aSRandall Stewart 			 * which is fine. Once we hit the rwnd_req, we then
5533f8829a4aSRandall Stewart 			 * will go to the sctp_user_rcvd() that will not
5534f8829a4aSRandall Stewart 			 * lock until it KNOWs it MUST send a WUP-SACK.
5535f8829a4aSRandall Stewart 			 */
5536f8829a4aSRandall Stewart 			freed_so_far = stcb->freed_by_sorcv_sincelast;
5537f8829a4aSRandall Stewart 			stcb->freed_by_sorcv_sincelast = 0;
5538f8829a4aSRandall Stewart 		}
5539f8829a4aSRandall Stewart 	}
55406114cd96SRandall Stewart 	if (stcb &&
55416114cd96SRandall Stewart 	    ((control->spec_flags & M_NOTIFICATION) == 0) &&
55426114cd96SRandall Stewart 	    control->do_not_ref_stcb == 0) {
5543d06c82f1SRandall Stewart 		stcb->asoc.strmin[control->sinfo_stream].delivery_started = 1;
5544d06c82f1SRandall Stewart 	}
5545f8829a4aSRandall Stewart 	/* First lets get off the sinfo and sockaddr info */
5546f8829a4aSRandall Stewart 	if ((sinfo) && filling_sinfo) {
5547f8829a4aSRandall Stewart 		memcpy(sinfo, control, sizeof(struct sctp_nonpad_sndrcvinfo));
5548f8829a4aSRandall Stewart 		nxt = TAILQ_NEXT(control, next);
5549e2e7c62eSMichael Tuexen 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
5550e2e7c62eSMichael Tuexen 		    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) {
5551f8829a4aSRandall Stewart 			struct sctp_extrcvinfo *s_extra;
5552f8829a4aSRandall Stewart 
5553f8829a4aSRandall Stewart 			s_extra = (struct sctp_extrcvinfo *)sinfo;
55549a6142d8SRandall Stewart 			if ((nxt) &&
55559a6142d8SRandall Stewart 			    (nxt->length)) {
55569a6142d8SRandall Stewart 				s_extra->sreinfo_next_flags = SCTP_NEXT_MSG_AVAIL;
5557f8829a4aSRandall Stewart 				if (nxt->sinfo_flags & SCTP_UNORDERED) {
55589a6142d8SRandall Stewart 					s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_IS_UNORDERED;
5559f8829a4aSRandall Stewart 				}
5560f42a358aSRandall Stewart 				if (nxt->spec_flags & M_NOTIFICATION) {
55619a6142d8SRandall Stewart 					s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_IS_NOTIFICATION;
5562f42a358aSRandall Stewart 				}
55639a6142d8SRandall Stewart 				s_extra->sreinfo_next_aid = nxt->sinfo_assoc_id;
55649a6142d8SRandall Stewart 				s_extra->sreinfo_next_length = nxt->length;
55659a6142d8SRandall Stewart 				s_extra->sreinfo_next_ppid = nxt->sinfo_ppid;
55669a6142d8SRandall Stewart 				s_extra->sreinfo_next_stream = nxt->sinfo_stream;
5567f8829a4aSRandall Stewart 				if (nxt->tail_mbuf != NULL) {
5568139bc87fSRandall Stewart 					if (nxt->end_added) {
55699a6142d8SRandall Stewart 						s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_ISCOMPLETE;
5570f8829a4aSRandall Stewart 					}
5571f8829a4aSRandall Stewart 				}
5572f8829a4aSRandall Stewart 			} else {
5573f8829a4aSRandall Stewart 				/*
5574f8829a4aSRandall Stewart 				 * we explicitly 0 this, since the memcpy
5575f8829a4aSRandall Stewart 				 * got some other things beyond the older
5576f8829a4aSRandall Stewart 				 * sinfo_ that is on the control's structure
5577f8829a4aSRandall Stewart 				 * :-D
5578f8829a4aSRandall Stewart 				 */
55799a6142d8SRandall Stewart 				nxt = NULL;
55809a6142d8SRandall Stewart 				s_extra->sreinfo_next_flags = SCTP_NO_NEXT_MSG;
55819a6142d8SRandall Stewart 				s_extra->sreinfo_next_aid = 0;
55829a6142d8SRandall Stewart 				s_extra->sreinfo_next_length = 0;
55839a6142d8SRandall Stewart 				s_extra->sreinfo_next_ppid = 0;
55849a6142d8SRandall Stewart 				s_extra->sreinfo_next_stream = 0;
5585f8829a4aSRandall Stewart 			}
5586f8829a4aSRandall Stewart 		}
5587f8829a4aSRandall Stewart 		/*
5588f8829a4aSRandall Stewart 		 * update off the real current cum-ack, if we have an stcb.
5589f8829a4aSRandall Stewart 		 */
55900696e120SRandall Stewart 		if ((control->do_not_ref_stcb == 0) && stcb)
5591f8829a4aSRandall Stewart 			sinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
5592f8829a4aSRandall Stewart 		/*
5593f8829a4aSRandall Stewart 		 * mask off the high bits, we keep the actual chunk bits in
5594f8829a4aSRandall Stewart 		 * there.
5595f8829a4aSRandall Stewart 		 */
5596f8829a4aSRandall Stewart 		sinfo->sinfo_flags &= 0x00ff;
55975f26a41dSRandall Stewart 		if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) {
55985f26a41dSRandall Stewart 			sinfo->sinfo_flags |= SCTP_UNORDERED;
55995f26a41dSRandall Stewart 		}
5600f8829a4aSRandall Stewart 	}
560118e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
560218e198d3SRandall Stewart 	{
560318e198d3SRandall Stewart 		int index, newindex;
560418e198d3SRandall Stewart 		struct sctp_pcbtsn_rlog *entry;
560518e198d3SRandall Stewart 
560618e198d3SRandall Stewart 		do {
560718e198d3SRandall Stewart 			index = inp->readlog_index;
560818e198d3SRandall Stewart 			newindex = index + 1;
560918e198d3SRandall Stewart 			if (newindex >= SCTP_READ_LOG_SIZE) {
561018e198d3SRandall Stewart 				newindex = 0;
561118e198d3SRandall Stewart 			}
561218e198d3SRandall Stewart 		} while (atomic_cmpset_int(&inp->readlog_index, index, newindex) == 0);
561318e198d3SRandall Stewart 		entry = &inp->readlog[index];
561418e198d3SRandall Stewart 		entry->vtag = control->sinfo_assoc_id;
561518e198d3SRandall Stewart 		entry->strm = control->sinfo_stream;
561618e198d3SRandall Stewart 		entry->seq = control->sinfo_ssn;
561718e198d3SRandall Stewart 		entry->sz = control->length;
561818e198d3SRandall Stewart 		entry->flgs = control->sinfo_flags;
561918e198d3SRandall Stewart 	}
562018e198d3SRandall Stewart #endif
5621f8829a4aSRandall Stewart 	if (fromlen && from) {
5622b5b6e5c2SMichael Tuexen 		cp_len = min((size_t)fromlen, (size_t)control->whoFrom->ro._l_addr.sa.sa_len);
5623b5b6e5c2SMichael Tuexen 		switch (control->whoFrom->ro._l_addr.sa.sa_family) {
5624b5b6e5c2SMichael Tuexen #ifdef INET6
5625b5b6e5c2SMichael Tuexen 		case AF_INET6:
5626f8829a4aSRandall Stewart 			((struct sockaddr_in6 *)from)->sin6_port = control->port_from;
5627b5b6e5c2SMichael Tuexen 			break;
5628f8829a4aSRandall Stewart #endif
5629b5b6e5c2SMichael Tuexen #ifdef INET
5630b5b6e5c2SMichael Tuexen 		case AF_INET:
5631b5b6e5c2SMichael Tuexen 			((struct sockaddr_in *)from)->sin_port = control->port_from;
5632b5b6e5c2SMichael Tuexen 			break;
5633b5b6e5c2SMichael Tuexen #endif
5634b5b6e5c2SMichael Tuexen 		default:
5635b5b6e5c2SMichael Tuexen 			break;
5636b5b6e5c2SMichael Tuexen 		}
5637b5b6e5c2SMichael Tuexen 		memcpy(from, &control->whoFrom->ro._l_addr, cp_len);
5638f8829a4aSRandall Stewart 
563942551e99SRandall Stewart #if defined(INET) && defined(INET6)
56405e2c2d87SRandall Stewart 		if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
5641b5b6e5c2SMichael Tuexen 		    (from->sa_family == AF_INET) &&
5642f8829a4aSRandall Stewart 		    ((size_t)fromlen >= sizeof(struct sockaddr_in6))) {
5643f8829a4aSRandall Stewart 			struct sockaddr_in *sin;
5644f8829a4aSRandall Stewart 			struct sockaddr_in6 sin6;
5645f8829a4aSRandall Stewart 
5646b5b6e5c2SMichael Tuexen 			sin = (struct sockaddr_in *)from;
5647f8829a4aSRandall Stewart 			bzero(&sin6, sizeof(sin6));
5648f8829a4aSRandall Stewart 			sin6.sin6_family = AF_INET6;
5649f8829a4aSRandall Stewart 			sin6.sin6_len = sizeof(struct sockaddr_in6);
5650d6af161aSRandall Stewart 			sin6.sin6_addr.s6_addr32[2] = htonl(0xffff);
5651f8829a4aSRandall Stewart 			bcopy(&sin->sin_addr,
5652d6af161aSRandall Stewart 			    &sin6.sin6_addr.s6_addr32[3],
5653d6af161aSRandall Stewart 			    sizeof(sin6.sin6_addr.s6_addr32[3]));
5654f8829a4aSRandall Stewart 			sin6.sin6_port = sin->sin_port;
5655b5b6e5c2SMichael Tuexen 			memcpy(from, &sin6, sizeof(struct sockaddr_in6));
5656f8829a4aSRandall Stewart 		}
5657f8829a4aSRandall Stewart #endif
5658e0e00a4dSMichael Tuexen #ifdef INET6
5659f8829a4aSRandall Stewart 		{
5660b5b6e5c2SMichael Tuexen 			struct sockaddr_in6 lsa6, *from6;
5661f8829a4aSRandall Stewart 
5662b5b6e5c2SMichael Tuexen 			from6 = (struct sockaddr_in6 *)from;
5663b5b6e5c2SMichael Tuexen 			sctp_recover_scope_mac(from6, (&lsa6));
5664f8829a4aSRandall Stewart 		}
5665f8829a4aSRandall Stewart #endif
5666f8829a4aSRandall Stewart 	}
5667f8829a4aSRandall Stewart 	/* now copy out what data we can */
5668f8829a4aSRandall Stewart 	if (mp == NULL) {
5669f8829a4aSRandall Stewart 		/* copy out each mbuf in the chain up to length */
5670f8829a4aSRandall Stewart get_more_data:
5671f8829a4aSRandall Stewart 		m = control->data;
5672f8829a4aSRandall Stewart 		while (m) {
5673f8829a4aSRandall Stewart 			/* Move out all we can */
5674f8829a4aSRandall Stewart 			cp_len = (int)uio->uio_resid;
5675139bc87fSRandall Stewart 			my_len = (int)SCTP_BUF_LEN(m);
5676f8829a4aSRandall Stewart 			if (cp_len > my_len) {
5677f8829a4aSRandall Stewart 				/* not enough in this buf */
5678f8829a4aSRandall Stewart 				cp_len = my_len;
5679f8829a4aSRandall Stewart 			}
5680f8829a4aSRandall Stewart 			if (hold_rlock) {
5681f8829a4aSRandall Stewart 				SCTP_INP_READ_UNLOCK(inp);
5682f8829a4aSRandall Stewart 				hold_rlock = 0;
5683f8829a4aSRandall Stewart 			}
5684f8829a4aSRandall Stewart 			if (cp_len > 0)
5685f8829a4aSRandall Stewart 				error = uiomove(mtod(m, char *), cp_len, uio);
5686f8829a4aSRandall Stewart 			/* re-read */
5687f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5688f8829a4aSRandall Stewart 				goto release;
5689f8829a4aSRandall Stewart 			}
56900696e120SRandall Stewart 			if ((control->do_not_ref_stcb == 0) && stcb &&
5691f8829a4aSRandall Stewart 			    stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5692f8829a4aSRandall Stewart 				no_rcv_needed = 1;
5693f8829a4aSRandall Stewart 			}
5694f8829a4aSRandall Stewart 			if (error) {
5695f8829a4aSRandall Stewart 				/* error we are out of here */
5696f8829a4aSRandall Stewart 				goto release;
5697f8829a4aSRandall Stewart 			}
5698139bc87fSRandall Stewart 			if ((SCTP_BUF_NEXT(m) == NULL) &&
5699139bc87fSRandall Stewart 			    (cp_len >= SCTP_BUF_LEN(m)) &&
5700f8829a4aSRandall Stewart 			    ((control->end_added == 0) ||
57010696e120SRandall Stewart 			    (control->end_added &&
57020696e120SRandall Stewart 			    (TAILQ_NEXT(control, next) == NULL)))
5703f8829a4aSRandall Stewart 			    ) {
5704f8829a4aSRandall Stewart 				SCTP_INP_READ_LOCK(inp);
5705f8829a4aSRandall Stewart 				hold_rlock = 1;
5706f8829a4aSRandall Stewart 			}
5707139bc87fSRandall Stewart 			if (cp_len == SCTP_BUF_LEN(m)) {
5708139bc87fSRandall Stewart 				if ((SCTP_BUF_NEXT(m) == NULL) &&
5709139bc87fSRandall Stewart 				    (control->end_added)) {
5710f8829a4aSRandall Stewart 					out_flags |= MSG_EOR;
571152129fcdSRandall Stewart 					if ((control->do_not_ref_stcb == 0) &&
571252129fcdSRandall Stewart 					    (control->stcb != NULL) &&
571352129fcdSRandall Stewart 					    ((control->spec_flags & M_NOTIFICATION) == 0))
5714ee7f9857SRandall Stewart 						control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5715f8829a4aSRandall Stewart 				}
5716139bc87fSRandall Stewart 				if (control->spec_flags & M_NOTIFICATION) {
5717f8829a4aSRandall Stewart 					out_flags |= MSG_NOTIFICATION;
5718f8829a4aSRandall Stewart 				}
5719f8829a4aSRandall Stewart 				/* we ate up the mbuf */
5720f8829a4aSRandall Stewart 				if (in_flags & MSG_PEEK) {
5721f8829a4aSRandall Stewart 					/* just looking */
5722139bc87fSRandall Stewart 					m = SCTP_BUF_NEXT(m);
5723f8829a4aSRandall Stewart 					copied_so_far += cp_len;
5724f8829a4aSRandall Stewart 				} else {
5725f8829a4aSRandall Stewart 					/* dispose of the mbuf */
5726b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5727f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv,
5728139bc87fSRandall Stewart 						    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
572980fefe0aSRandall Stewart 					}
5730f8829a4aSRandall Stewart 					sctp_sbfree(control, stcb, &so->so_rcv, m);
5731b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5732f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv,
5733f8829a4aSRandall Stewart 						    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
573480fefe0aSRandall Stewart 					}
5735f8829a4aSRandall Stewart 					copied_so_far += cp_len;
5736f8829a4aSRandall Stewart 					freed_so_far += cp_len;
5737c4739e2fSRandall Stewart 					freed_so_far += MSIZE;
573818e198d3SRandall Stewart 					atomic_subtract_int(&control->length, cp_len);
5739f8829a4aSRandall Stewart 					control->data = sctp_m_free(m);
5740f8829a4aSRandall Stewart 					m = control->data;
5741f8829a4aSRandall Stewart 					/*
5742f8829a4aSRandall Stewart 					 * been through it all, must hold sb
5743f8829a4aSRandall Stewart 					 * lock ok to null tail
5744f8829a4aSRandall Stewart 					 */
5745f8829a4aSRandall Stewart 					if (control->data == NULL) {
5746a5d547adSRandall Stewart #ifdef INVARIANTS
5747f8829a4aSRandall Stewart 						if ((control->end_added == 0) ||
5748f8829a4aSRandall Stewart 						    (TAILQ_NEXT(control, next) == NULL)) {
5749f8829a4aSRandall Stewart 							/*
5750f8829a4aSRandall Stewart 							 * If the end is not
5751f8829a4aSRandall Stewart 							 * added, OR the
5752f8829a4aSRandall Stewart 							 * next is NOT null
5753f8829a4aSRandall Stewart 							 * we MUST have the
5754f8829a4aSRandall Stewart 							 * lock.
5755f8829a4aSRandall Stewart 							 */
5756f8829a4aSRandall Stewart 							if (mtx_owned(&inp->inp_rdata_mtx) == 0) {
5757f8829a4aSRandall Stewart 								panic("Hmm we don't own the lock?");
5758f8829a4aSRandall Stewart 							}
5759f8829a4aSRandall Stewart 						}
5760f8829a4aSRandall Stewart #endif
5761f8829a4aSRandall Stewart 						control->tail_mbuf = NULL;
5762a5d547adSRandall Stewart #ifdef INVARIANTS
5763f8829a4aSRandall Stewart 						if ((control->end_added) && ((out_flags & MSG_EOR) == 0)) {
5764f8829a4aSRandall Stewart 							panic("end_added, nothing left and no MSG_EOR");
5765f8829a4aSRandall Stewart 						}
5766f8829a4aSRandall Stewart #endif
5767f8829a4aSRandall Stewart 					}
5768f8829a4aSRandall Stewart 				}
5769f8829a4aSRandall Stewart 			} else {
5770f8829a4aSRandall Stewart 				/* Do we need to trim the mbuf? */
5771139bc87fSRandall Stewart 				if (control->spec_flags & M_NOTIFICATION) {
5772f8829a4aSRandall Stewart 					out_flags |= MSG_NOTIFICATION;
5773f8829a4aSRandall Stewart 				}
5774f8829a4aSRandall Stewart 				if ((in_flags & MSG_PEEK) == 0) {
5775139bc87fSRandall Stewart 					SCTP_BUF_RESV_UF(m, cp_len);
5776139bc87fSRandall Stewart 					SCTP_BUF_LEN(m) -= cp_len;
5777b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5778f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, cp_len);
577980fefe0aSRandall Stewart 					}
5780f8829a4aSRandall Stewart 					atomic_subtract_int(&so->so_rcv.sb_cc, cp_len);
57810696e120SRandall Stewart 					if ((control->do_not_ref_stcb == 0) &&
57820696e120SRandall Stewart 					    stcb) {
5783f8829a4aSRandall Stewart 						atomic_subtract_int(&stcb->asoc.sb_cc, cp_len);
5784f8829a4aSRandall Stewart 					}
5785f8829a4aSRandall Stewart 					copied_so_far += cp_len;
5786f8829a4aSRandall Stewart 					freed_so_far += cp_len;
5787c4739e2fSRandall Stewart 					freed_so_far += MSIZE;
5788b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5789f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb,
5790f8829a4aSRandall Stewart 						    SCTP_LOG_SBRESULT, 0);
579180fefe0aSRandall Stewart 					}
579218e198d3SRandall Stewart 					atomic_subtract_int(&control->length, cp_len);
5793f8829a4aSRandall Stewart 				} else {
5794f8829a4aSRandall Stewart 					copied_so_far += cp_len;
5795f8829a4aSRandall Stewart 				}
5796f8829a4aSRandall Stewart 			}
5797d61a0ae0SRandall Stewart 			if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) {
5798f8829a4aSRandall Stewart 				break;
5799f8829a4aSRandall Stewart 			}
5800f8829a4aSRandall Stewart 			if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5801f8829a4aSRandall Stewart 			    (control->do_not_ref_stcb == 0) &&
5802f8829a4aSRandall Stewart 			    (freed_so_far >= rwnd_req)) {
5803f8829a4aSRandall Stewart 				sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5804f8829a4aSRandall Stewart 			}
5805f8829a4aSRandall Stewart 		}		/* end while(m) */
5806f8829a4aSRandall Stewart 		/*
5807f8829a4aSRandall Stewart 		 * At this point we have looked at it all and we either have
5808f8829a4aSRandall Stewart 		 * a MSG_EOR/or read all the user wants... <OR>
5809f8829a4aSRandall Stewart 		 * control->length == 0.
5810f8829a4aSRandall Stewart 		 */
5811d61a0ae0SRandall Stewart 		if ((out_flags & MSG_EOR) && ((in_flags & MSG_PEEK) == 0)) {
5812f8829a4aSRandall Stewart 			/* we are done with this control */
5813f8829a4aSRandall Stewart 			if (control->length == 0) {
5814f8829a4aSRandall Stewart 				if (control->data) {
5815a5d547adSRandall Stewart #ifdef INVARIANTS
5816f8829a4aSRandall Stewart 					panic("control->data not null at read eor?");
5817f8829a4aSRandall Stewart #else
5818ad81507eSRandall Stewart 					SCTP_PRINTF("Strange, data left in the control buffer .. invarients would panic?\n");
5819f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
5820f8829a4aSRandall Stewart 					control->data = NULL;
5821f8829a4aSRandall Stewart #endif
5822f8829a4aSRandall Stewart 				}
5823f8829a4aSRandall Stewart 		done_with_control:
5824f8829a4aSRandall Stewart 				if (TAILQ_NEXT(control, next) == NULL) {
5825f8829a4aSRandall Stewart 					/*
5826f8829a4aSRandall Stewart 					 * If we don't have a next we need a
5827b201f536SRandall Stewart 					 * lock, if there is a next
5828b201f536SRandall Stewart 					 * interrupt is filling ahead of us
5829b201f536SRandall Stewart 					 * and we don't need a lock to
5830b201f536SRandall Stewart 					 * remove this guy (which is the
5831b201f536SRandall Stewart 					 * head of the queue).
5832f8829a4aSRandall Stewart 					 */
5833f8829a4aSRandall Stewart 					if (hold_rlock == 0) {
5834f8829a4aSRandall Stewart 						SCTP_INP_READ_LOCK(inp);
5835f8829a4aSRandall Stewart 						hold_rlock = 1;
5836f8829a4aSRandall Stewart 					}
5837f8829a4aSRandall Stewart 				}
5838f8829a4aSRandall Stewart 				TAILQ_REMOVE(&inp->read_queue, control, next);
5839f8829a4aSRandall Stewart 				/* Add back any hiddend data */
5840f8829a4aSRandall Stewart 				if (control->held_length) {
5841f8829a4aSRandall Stewart 					held_length = 0;
5842f8829a4aSRandall Stewart 					control->held_length = 0;
5843f8829a4aSRandall Stewart 					wakeup_read_socket = 1;
5844f8829a4aSRandall Stewart 				}
584517205eccSRandall Stewart 				if (control->aux_data) {
584617205eccSRandall Stewart 					sctp_m_free(control->aux_data);
584717205eccSRandall Stewart 					control->aux_data = NULL;
584817205eccSRandall Stewart 				}
5849f8829a4aSRandall Stewart 				no_rcv_needed = control->do_not_ref_stcb;
5850f8829a4aSRandall Stewart 				sctp_free_remote_addr(control->whoFrom);
5851f8829a4aSRandall Stewart 				control->data = NULL;
5852f8829a4aSRandall Stewart 				sctp_free_a_readq(stcb, control);
5853f8829a4aSRandall Stewart 				control = NULL;
58540696e120SRandall Stewart 				if ((freed_so_far >= rwnd_req) &&
58550696e120SRandall Stewart 				    (no_rcv_needed == 0))
5856f8829a4aSRandall Stewart 					sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5857f8829a4aSRandall Stewart 
5858f8829a4aSRandall Stewart 			} else {
5859f8829a4aSRandall Stewart 				/*
5860f8829a4aSRandall Stewart 				 * The user did not read all of this
5861f8829a4aSRandall Stewart 				 * message, turn off the returned MSG_EOR
5862f8829a4aSRandall Stewart 				 * since we are leaving more behind on the
5863f8829a4aSRandall Stewart 				 * control to read.
5864f8829a4aSRandall Stewart 				 */
5865a5d547adSRandall Stewart #ifdef INVARIANTS
58660696e120SRandall Stewart 				if (control->end_added &&
58670696e120SRandall Stewart 				    (control->data == NULL) &&
5868f8829a4aSRandall Stewart 				    (control->tail_mbuf == NULL)) {
5869f8829a4aSRandall Stewart 					panic("Gak, control->length is corrupt?");
5870f8829a4aSRandall Stewart 				}
5871f8829a4aSRandall Stewart #endif
5872f8829a4aSRandall Stewart 				no_rcv_needed = control->do_not_ref_stcb;
5873f8829a4aSRandall Stewart 				out_flags &= ~MSG_EOR;
5874f8829a4aSRandall Stewart 			}
5875f8829a4aSRandall Stewart 		}
5876f8829a4aSRandall Stewart 		if (out_flags & MSG_EOR) {
5877f8829a4aSRandall Stewart 			goto release;
5878f8829a4aSRandall Stewart 		}
5879f8829a4aSRandall Stewart 		if ((uio->uio_resid == 0) ||
588004aab884SMichael Tuexen 		    ((in_eeor_mode) &&
588104aab884SMichael Tuexen 		    (copied_so_far >= (uint32_t) max(so->so_rcv.sb_lowat, 1)))) {
5882f8829a4aSRandall Stewart 			goto release;
5883f8829a4aSRandall Stewart 		}
5884f8829a4aSRandall Stewart 		/*
5885f8829a4aSRandall Stewart 		 * If I hit here the receiver wants more and this message is
5886f8829a4aSRandall Stewart 		 * NOT done (pd-api). So two questions. Can we block? if not
5887f8829a4aSRandall Stewart 		 * we are done. Did the user NOT set MSG_WAITALL?
5888f8829a4aSRandall Stewart 		 */
5889f8829a4aSRandall Stewart 		if (block_allowed == 0) {
5890f8829a4aSRandall Stewart 			goto release;
5891f8829a4aSRandall Stewart 		}
5892f8829a4aSRandall Stewart 		/*
5893f8829a4aSRandall Stewart 		 * We need to wait for more data a few things: - We don't
5894f8829a4aSRandall Stewart 		 * sbunlock() so we don't get someone else reading. - We
5895f8829a4aSRandall Stewart 		 * must be sure to account for the case where what is added
5896f8829a4aSRandall Stewart 		 * is NOT to our control when we wakeup.
5897f8829a4aSRandall Stewart 		 */
5898f8829a4aSRandall Stewart 
5899f8829a4aSRandall Stewart 		/*
5900f8829a4aSRandall Stewart 		 * Do we need to tell the transport a rwnd update might be
5901f8829a4aSRandall Stewart 		 * needed before we go to sleep?
5902f8829a4aSRandall Stewart 		 */
5903f8829a4aSRandall Stewart 		if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5904f8829a4aSRandall Stewart 		    ((freed_so_far >= rwnd_req) &&
5905f8829a4aSRandall Stewart 		    (control->do_not_ref_stcb == 0) &&
5906f8829a4aSRandall Stewart 		    (no_rcv_needed == 0))) {
5907f8829a4aSRandall Stewart 			sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5908f8829a4aSRandall Stewart 		}
5909f8829a4aSRandall Stewart wait_some_more:
591044b7479bSRandall Stewart 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
5911f8829a4aSRandall Stewart 			goto release;
5912f8829a4aSRandall Stewart 		}
5913f8829a4aSRandall Stewart 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)
5914f8829a4aSRandall Stewart 			goto release;
5915f8829a4aSRandall Stewart 
5916f8829a4aSRandall Stewart 		if (hold_rlock == 1) {
5917f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
5918f8829a4aSRandall Stewart 			hold_rlock = 0;
5919f8829a4aSRandall Stewart 		}
5920f8829a4aSRandall Stewart 		if (hold_sblock == 0) {
5921f8829a4aSRandall Stewart 			SOCKBUF_LOCK(&so->so_rcv);
5922f8829a4aSRandall Stewart 			hold_sblock = 1;
5923f8829a4aSRandall Stewart 		}
5924851b7298SRandall Stewart 		if ((copied_so_far) && (control->length == 0) &&
5925b5c16493SMichael Tuexen 		    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE))) {
5926851b7298SRandall Stewart 			goto release;
5927851b7298SRandall Stewart 		}
5928f8829a4aSRandall Stewart 		if (so->so_rcv.sb_cc <= control->held_length) {
5929f8829a4aSRandall Stewart 			error = sbwait(&so->so_rcv);
5930f8829a4aSRandall Stewart 			if (error) {
5931f8829a4aSRandall Stewart 				goto release;
5932f8829a4aSRandall Stewart 			}
5933f8829a4aSRandall Stewart 			control->held_length = 0;
5934f8829a4aSRandall Stewart 		}
5935f8829a4aSRandall Stewart 		if (hold_sblock) {
5936f8829a4aSRandall Stewart 			SOCKBUF_UNLOCK(&so->so_rcv);
5937f8829a4aSRandall Stewart 			hold_sblock = 0;
5938f8829a4aSRandall Stewart 		}
5939f8829a4aSRandall Stewart 		if (control->length == 0) {
5940f8829a4aSRandall Stewart 			/* still nothing here */
5941f8829a4aSRandall Stewart 			if (control->end_added == 1) {
5942f8829a4aSRandall Stewart 				/* he aborted, or is done i.e.did a shutdown */
5943f8829a4aSRandall Stewart 				out_flags |= MSG_EOR;
59449a6142d8SRandall Stewart 				if (control->pdapi_aborted) {
59456114cd96SRandall Stewart 					if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5946ee7f9857SRandall Stewart 						control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
59479a6142d8SRandall Stewart 
594803b0b021SRandall Stewart 					out_flags |= MSG_TRUNC;
59499a6142d8SRandall Stewart 				} else {
59506114cd96SRandall Stewart 					if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5951ee7f9857SRandall Stewart 						control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
59529a6142d8SRandall Stewart 				}
5953f8829a4aSRandall Stewart 				goto done_with_control;
5954f8829a4aSRandall Stewart 			}
5955f8829a4aSRandall Stewart 			if (so->so_rcv.sb_cc > held_length) {
5956f8829a4aSRandall Stewart 				control->held_length = so->so_rcv.sb_cc;
5957f8829a4aSRandall Stewart 				held_length = 0;
5958f8829a4aSRandall Stewart 			}
5959f8829a4aSRandall Stewart 			goto wait_some_more;
5960f8829a4aSRandall Stewart 		} else if (control->data == NULL) {
596150cec919SRandall Stewart 			/*
596250cec919SRandall Stewart 			 * we must re-sync since data is probably being
596350cec919SRandall Stewart 			 * added
596450cec919SRandall Stewart 			 */
596550cec919SRandall Stewart 			SCTP_INP_READ_LOCK(inp);
596650cec919SRandall Stewart 			if ((control->length > 0) && (control->data == NULL)) {
596750cec919SRandall Stewart 				/*
596850cec919SRandall Stewart 				 * big trouble.. we have the lock and its
596950cec919SRandall Stewart 				 * corrupt?
597050cec919SRandall Stewart 				 */
59719c04b296SRandall Stewart #ifdef INVARIANTS
5972f8829a4aSRandall Stewart 				panic("Impossible data==NULL length !=0");
59739c04b296SRandall Stewart #endif
59749c04b296SRandall Stewart 				out_flags |= MSG_EOR;
59759c04b296SRandall Stewart 				out_flags |= MSG_TRUNC;
59769c04b296SRandall Stewart 				control->length = 0;
59779c04b296SRandall Stewart 				SCTP_INP_READ_UNLOCK(inp);
59789c04b296SRandall Stewart 				goto done_with_control;
5979f8829a4aSRandall Stewart 			}
598050cec919SRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
598150cec919SRandall Stewart 			/* We will fall around to get more data */
598250cec919SRandall Stewart 		}
5983f8829a4aSRandall Stewart 		goto get_more_data;
5984f8829a4aSRandall Stewart 	} else {
598517205eccSRandall Stewart 		/*-
598617205eccSRandall Stewart 		 * Give caller back the mbuf chain,
598717205eccSRandall Stewart 		 * store in uio_resid the length
5988f8829a4aSRandall Stewart 		 */
598917205eccSRandall Stewart 		wakeup_read_socket = 0;
5990f8829a4aSRandall Stewart 		if ((control->end_added == 0) ||
5991f8829a4aSRandall Stewart 		    (TAILQ_NEXT(control, next) == NULL)) {
5992f8829a4aSRandall Stewart 			/* Need to get rlock */
5993f8829a4aSRandall Stewart 			if (hold_rlock == 0) {
5994f8829a4aSRandall Stewart 				SCTP_INP_READ_LOCK(inp);
5995f8829a4aSRandall Stewart 				hold_rlock = 1;
5996f8829a4aSRandall Stewart 			}
5997f8829a4aSRandall Stewart 		}
5998139bc87fSRandall Stewart 		if (control->end_added) {
5999f8829a4aSRandall Stewart 			out_flags |= MSG_EOR;
600060990c0cSMichael Tuexen 			if ((control->do_not_ref_stcb == 0) &&
600160990c0cSMichael Tuexen 			    (control->stcb != NULL) &&
600260990c0cSMichael Tuexen 			    ((control->spec_flags & M_NOTIFICATION) == 0))
6003ee7f9857SRandall Stewart 				control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6004f8829a4aSRandall Stewart 		}
6005139bc87fSRandall Stewart 		if (control->spec_flags & M_NOTIFICATION) {
6006f8829a4aSRandall Stewart 			out_flags |= MSG_NOTIFICATION;
6007f8829a4aSRandall Stewart 		}
600817205eccSRandall Stewart 		uio->uio_resid = control->length;
6009f8829a4aSRandall Stewart 		*mp = control->data;
6010f8829a4aSRandall Stewart 		m = control->data;
6011f8829a4aSRandall Stewart 		while (m) {
6012b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6013f8829a4aSRandall Stewart 				sctp_sblog(&so->so_rcv,
6014139bc87fSRandall Stewart 				    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
601580fefe0aSRandall Stewart 			}
6016f8829a4aSRandall Stewart 			sctp_sbfree(control, stcb, &so->so_rcv, m);
6017139bc87fSRandall Stewart 			freed_so_far += SCTP_BUF_LEN(m);
6018c4739e2fSRandall Stewart 			freed_so_far += MSIZE;
6019b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6020f8829a4aSRandall Stewart 				sctp_sblog(&so->so_rcv,
6021f8829a4aSRandall Stewart 				    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
602280fefe0aSRandall Stewart 			}
6023139bc87fSRandall Stewart 			m = SCTP_BUF_NEXT(m);
6024f8829a4aSRandall Stewart 		}
6025f8829a4aSRandall Stewart 		control->data = control->tail_mbuf = NULL;
6026f8829a4aSRandall Stewart 		control->length = 0;
6027f8829a4aSRandall Stewart 		if (out_flags & MSG_EOR) {
6028f8829a4aSRandall Stewart 			/* Done with this control */
6029f8829a4aSRandall Stewart 			goto done_with_control;
6030f8829a4aSRandall Stewart 		}
6031f8829a4aSRandall Stewart 	}
6032f8829a4aSRandall Stewart release:
6033f8829a4aSRandall Stewart 	if (hold_rlock == 1) {
6034f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
6035f8829a4aSRandall Stewart 		hold_rlock = 0;
6036f8829a4aSRandall Stewart 	}
60377abab911SRobert Watson 	if (hold_sblock == 1) {
60387abab911SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
60397abab911SRobert Watson 		hold_sblock = 0;
6040f8829a4aSRandall Stewart 	}
6041f8829a4aSRandall Stewart 	sbunlock(&so->so_rcv);
60427abab911SRobert Watson 	sockbuf_lock = 0;
6043f8829a4aSRandall Stewart 
6044f8829a4aSRandall Stewart release_unlocked:
6045f8829a4aSRandall Stewart 	if (hold_sblock) {
6046f8829a4aSRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
6047f8829a4aSRandall Stewart 		hold_sblock = 0;
6048f8829a4aSRandall Stewart 	}
6049f8829a4aSRandall Stewart 	if ((stcb) && (in_flags & MSG_PEEK) == 0) {
6050f8829a4aSRandall Stewart 		if ((freed_so_far >= rwnd_req) &&
6051f8829a4aSRandall Stewart 		    (control && (control->do_not_ref_stcb == 0)) &&
6052f8829a4aSRandall Stewart 		    (no_rcv_needed == 0))
6053f8829a4aSRandall Stewart 			sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6054f8829a4aSRandall Stewart 	}
6055f8829a4aSRandall Stewart out:
60561b9f62a0SRandall Stewart 	if (msg_flags) {
60571b9f62a0SRandall Stewart 		*msg_flags = out_flags;
60581b9f62a0SRandall Stewart 	}
60599a6142d8SRandall Stewart 	if (((out_flags & MSG_EOR) == 0) &&
60609a6142d8SRandall Stewart 	    ((in_flags & MSG_PEEK) == 0) &&
60619a6142d8SRandall Stewart 	    (sinfo) &&
6062e2e7c62eSMichael Tuexen 	    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
6063e2e7c62eSMichael Tuexen 	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO))) {
60649a6142d8SRandall Stewart 		struct sctp_extrcvinfo *s_extra;
60659a6142d8SRandall Stewart 
60669a6142d8SRandall Stewart 		s_extra = (struct sctp_extrcvinfo *)sinfo;
60679a6142d8SRandall Stewart 		s_extra->sreinfo_next_flags = SCTP_NO_NEXT_MSG;
60689a6142d8SRandall Stewart 	}
6069f8829a4aSRandall Stewart 	if (hold_rlock == 1) {
6070f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
6071f8829a4aSRandall Stewart 	}
6072f8829a4aSRandall Stewart 	if (hold_sblock) {
6073f8829a4aSRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
6074f8829a4aSRandall Stewart 	}
60757abab911SRobert Watson 	if (sockbuf_lock) {
60767abab911SRobert Watson 		sbunlock(&so->so_rcv);
60777abab911SRobert Watson 	}
607850cec919SRandall Stewart 	if (freecnt_applied) {
6079f8829a4aSRandall Stewart 		/*
6080f8829a4aSRandall Stewart 		 * The lock on the socket buffer protects us so the free
6081f8829a4aSRandall Stewart 		 * code will stop. But since we used the socketbuf lock and
6082f8829a4aSRandall Stewart 		 * the sender uses the tcb_lock to increment, we need to use
6083f8829a4aSRandall Stewart 		 * the atomic add to the refcnt.
6084f8829a4aSRandall Stewart 		 */
608550cec919SRandall Stewart 		if (stcb == NULL) {
6086df6e0cc3SRandall Stewart #ifdef INVARIANTS
608750cec919SRandall Stewart 			panic("stcb for refcnt has gone NULL?");
6088df6e0cc3SRandall Stewart 			goto stage_left;
6089df6e0cc3SRandall Stewart #else
6090df6e0cc3SRandall Stewart 			goto stage_left;
6091df6e0cc3SRandall Stewart #endif
609250cec919SRandall Stewart 		}
609350cec919SRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, -1);
6094f8829a4aSRandall Stewart 		/* Save the value back for next time */
6095f8829a4aSRandall Stewart 		stcb->freed_by_sorcv_sincelast = freed_so_far;
6096f8829a4aSRandall Stewart 	}
6097b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
6098f8829a4aSRandall Stewart 		if (stcb) {
6099f8829a4aSRandall Stewart 			sctp_misc_ints(SCTP_SORECV_DONE,
6100f8829a4aSRandall Stewart 			    freed_so_far,
6101f8829a4aSRandall Stewart 			    ((uio) ? (slen - uio->uio_resid) : slen),
6102f8829a4aSRandall Stewart 			    stcb->asoc.my_rwnd,
6103f8829a4aSRandall Stewart 			    so->so_rcv.sb_cc);
6104f8829a4aSRandall Stewart 		} else {
6105f8829a4aSRandall Stewart 			sctp_misc_ints(SCTP_SORECV_DONE,
6106f8829a4aSRandall Stewart 			    freed_so_far,
6107f8829a4aSRandall Stewart 			    ((uio) ? (slen - uio->uio_resid) : slen),
6108f8829a4aSRandall Stewart 			    0,
6109f8829a4aSRandall Stewart 			    so->so_rcv.sb_cc);
6110f8829a4aSRandall Stewart 		}
611180fefe0aSRandall Stewart 	}
6112df6e0cc3SRandall Stewart stage_left:
6113f8829a4aSRandall Stewart 	if (wakeup_read_socket) {
6114f8829a4aSRandall Stewart 		sctp_sorwakeup(inp, so);
6115f8829a4aSRandall Stewart 	}
6116f8829a4aSRandall Stewart 	return (error);
6117f8829a4aSRandall Stewart }
6118f8829a4aSRandall Stewart 
6119f8829a4aSRandall Stewart 
6120f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
6121f8829a4aSRandall Stewart struct mbuf *
6122f8829a4aSRandall Stewart sctp_m_free(struct mbuf *m)
6123f8829a4aSRandall Stewart {
6124b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6125139bc87fSRandall Stewart 		if (SCTP_BUF_IS_EXTENDED(m)) {
6126f8829a4aSRandall Stewart 			sctp_log_mb(m, SCTP_MBUF_IFREE);
6127f8829a4aSRandall Stewart 		}
612880fefe0aSRandall Stewart 	}
6129f8829a4aSRandall Stewart 	return (m_free(m));
6130f8829a4aSRandall Stewart }
6131f8829a4aSRandall Stewart 
6132f8829a4aSRandall Stewart void
6133f8829a4aSRandall Stewart sctp_m_freem(struct mbuf *mb)
6134f8829a4aSRandall Stewart {
6135f8829a4aSRandall Stewart 	while (mb != NULL)
6136f8829a4aSRandall Stewart 		mb = sctp_m_free(mb);
6137f8829a4aSRandall Stewart }
6138f8829a4aSRandall Stewart 
6139f8829a4aSRandall Stewart #endif
6140f8829a4aSRandall Stewart 
614142551e99SRandall Stewart int
614242551e99SRandall Stewart sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id)
614342551e99SRandall Stewart {
614442551e99SRandall Stewart 	/*
614542551e99SRandall Stewart 	 * Given a local address. For all associations that holds the
614642551e99SRandall Stewart 	 * address, request a peer-set-primary.
614742551e99SRandall Stewart 	 */
614842551e99SRandall Stewart 	struct sctp_ifa *ifa;
614942551e99SRandall Stewart 	struct sctp_laddr *wi;
615042551e99SRandall Stewart 
615142551e99SRandall Stewart 	ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0);
615242551e99SRandall Stewart 	if (ifa == NULL) {
6153c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EADDRNOTAVAIL);
615442551e99SRandall Stewart 		return (EADDRNOTAVAIL);
615542551e99SRandall Stewart 	}
615642551e99SRandall Stewart 	/*
615742551e99SRandall Stewart 	 * Now that we have the ifa we must awaken the iterator with this
615842551e99SRandall Stewart 	 * message.
615942551e99SRandall Stewart 	 */
6160b3f1ea41SRandall Stewart 	wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
616142551e99SRandall Stewart 	if (wi == NULL) {
6162c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
616342551e99SRandall Stewart 		return (ENOMEM);
616442551e99SRandall Stewart 	}
616542551e99SRandall Stewart 	/* Now incr the count and int wi structure */
616642551e99SRandall Stewart 	SCTP_INCR_LADDR_COUNT();
616742551e99SRandall Stewart 	bzero(wi, sizeof(*wi));
6168d61a0ae0SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
616942551e99SRandall Stewart 	wi->ifa = ifa;
617042551e99SRandall Stewart 	wi->action = SCTP_SET_PRIM_ADDR;
617142551e99SRandall Stewart 	atomic_add_int(&ifa->refcount, 1);
617242551e99SRandall Stewart 
617342551e99SRandall Stewart 	/* Now add it to the work queue */
6174f7517433SRandall Stewart 	SCTP_WQ_ADDR_LOCK();
617542551e99SRandall Stewart 	/*
617642551e99SRandall Stewart 	 * Should this really be a tailq? As it is we will process the
617742551e99SRandall Stewart 	 * newest first :-0
617842551e99SRandall Stewart 	 */
6179b3f1ea41SRandall Stewart 	LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
6180f7517433SRandall Stewart 	SCTP_WQ_ADDR_UNLOCK();
618142551e99SRandall Stewart 	sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
618242551e99SRandall Stewart 	    (struct sctp_inpcb *)NULL,
618342551e99SRandall Stewart 	    (struct sctp_tcb *)NULL,
618442551e99SRandall Stewart 	    (struct sctp_nets *)NULL);
618542551e99SRandall Stewart 	return (0);
618642551e99SRandall Stewart }
618742551e99SRandall Stewart 
618842551e99SRandall Stewart 
6189f8829a4aSRandall Stewart int
619017205eccSRandall Stewart sctp_soreceive(struct socket *so,
619117205eccSRandall Stewart     struct sockaddr **psa,
619217205eccSRandall Stewart     struct uio *uio,
619317205eccSRandall Stewart     struct mbuf **mp0,
619417205eccSRandall Stewart     struct mbuf **controlp,
619517205eccSRandall Stewart     int *flagsp)
6196f8829a4aSRandall Stewart {
6197f8829a4aSRandall Stewart 	int error, fromlen;
6198f8829a4aSRandall Stewart 	uint8_t sockbuf[256];
6199f8829a4aSRandall Stewart 	struct sockaddr *from;
6200f8829a4aSRandall Stewart 	struct sctp_extrcvinfo sinfo;
6201f8829a4aSRandall Stewart 	int filling_sinfo = 1;
6202f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
6203f8829a4aSRandall Stewart 
6204f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
6205f8829a4aSRandall Stewart 	/* pickup the assoc we are reading from */
6206f8829a4aSRandall Stewart 	if (inp == NULL) {
6207c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6208f8829a4aSRandall Stewart 		return (EINVAL);
6209f8829a4aSRandall Stewart 	}
6210e2e7c62eSMichael Tuexen 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) &&
6211e2e7c62eSMichael Tuexen 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
6212e2e7c62eSMichael Tuexen 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) ||
6213f8829a4aSRandall Stewart 	    (controlp == NULL)) {
6214f8829a4aSRandall Stewart 		/* user does not want the sndrcv ctl */
6215f8829a4aSRandall Stewart 		filling_sinfo = 0;
6216f8829a4aSRandall Stewart 	}
6217f8829a4aSRandall Stewart 	if (psa) {
6218f8829a4aSRandall Stewart 		from = (struct sockaddr *)sockbuf;
6219f8829a4aSRandall Stewart 		fromlen = sizeof(sockbuf);
6220f8829a4aSRandall Stewart 		from->sa_len = 0;
6221f8829a4aSRandall Stewart 	} else {
6222f8829a4aSRandall Stewart 		from = NULL;
6223f8829a4aSRandall Stewart 		fromlen = 0;
6224f8829a4aSRandall Stewart 	}
6225f8829a4aSRandall Stewart 
6226e432298aSXin LI 	if (filling_sinfo) {
6227e432298aSXin LI 		memset(&sinfo, 0, sizeof(struct sctp_extrcvinfo));
6228e432298aSXin LI 	}
6229f8829a4aSRandall Stewart 	error = sctp_sorecvmsg(so, uio, mp0, from, fromlen, flagsp,
6230f8829a4aSRandall Stewart 	    (struct sctp_sndrcvinfo *)&sinfo, filling_sinfo);
6231e432298aSXin LI 	if (controlp != NULL) {
6232f8829a4aSRandall Stewart 		/* copy back the sinfo in a CMSG format */
6233f8829a4aSRandall Stewart 		if (filling_sinfo)
6234f8829a4aSRandall Stewart 			*controlp = sctp_build_ctl_nchunk(inp,
6235f8829a4aSRandall Stewart 			    (struct sctp_sndrcvinfo *)&sinfo);
6236f8829a4aSRandall Stewart 		else
6237f8829a4aSRandall Stewart 			*controlp = NULL;
6238f8829a4aSRandall Stewart 	}
6239f8829a4aSRandall Stewart 	if (psa) {
6240f8829a4aSRandall Stewart 		/* copy back the address info */
6241f8829a4aSRandall Stewart 		if (from && from->sa_len) {
6242f8829a4aSRandall Stewart 			*psa = sodupsockaddr(from, M_NOWAIT);
6243f8829a4aSRandall Stewart 		} else {
6244f8829a4aSRandall Stewart 			*psa = NULL;
6245f8829a4aSRandall Stewart 		}
6246f8829a4aSRandall Stewart 	}
6247f8829a4aSRandall Stewart 	return (error);
6248f8829a4aSRandall Stewart }
624917205eccSRandall Stewart 
625017205eccSRandall Stewart 
625117205eccSRandall Stewart 
625217205eccSRandall Stewart 
625317205eccSRandall Stewart 
625417205eccSRandall Stewart int
6255d61a0ae0SRandall Stewart sctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr,
6256d61a0ae0SRandall Stewart     int totaddr, int *error)
625717205eccSRandall Stewart {
625817205eccSRandall Stewart 	int added = 0;
625917205eccSRandall Stewart 	int i;
626017205eccSRandall Stewart 	struct sctp_inpcb *inp;
626117205eccSRandall Stewart 	struct sockaddr *sa;
626217205eccSRandall Stewart 	size_t incr = 0;
626317205eccSRandall Stewart 
626492776dfdSMichael Tuexen #ifdef INET
626592776dfdSMichael Tuexen 	struct sockaddr_in *sin;
626692776dfdSMichael Tuexen 
626792776dfdSMichael Tuexen #endif
626892776dfdSMichael Tuexen #ifdef INET6
626992776dfdSMichael Tuexen 	struct sockaddr_in6 *sin6;
627092776dfdSMichael Tuexen 
627192776dfdSMichael Tuexen #endif
627292776dfdSMichael Tuexen 
627317205eccSRandall Stewart 	sa = addr;
627417205eccSRandall Stewart 	inp = stcb->sctp_ep;
627517205eccSRandall Stewart 	*error = 0;
627617205eccSRandall Stewart 	for (i = 0; i < totaddr; i++) {
6277ea5eba11SMichael Tuexen 		switch (sa->sa_family) {
6278ea5eba11SMichael Tuexen #ifdef INET
6279ea5eba11SMichael Tuexen 		case AF_INET:
628017205eccSRandall Stewart 			incr = sizeof(struct sockaddr_in);
628192776dfdSMichael Tuexen 			sin = (struct sockaddr_in *)sa;
628292776dfdSMichael Tuexen 			if ((sin->sin_addr.s_addr == INADDR_ANY) ||
628392776dfdSMichael Tuexen 			    (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
628492776dfdSMichael Tuexen 			    IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
628592776dfdSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
628692776dfdSMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
628792776dfdSMichael Tuexen 				*error = EINVAL;
628892776dfdSMichael Tuexen 				goto out_now;
628992776dfdSMichael Tuexen 			}
6290ca85e948SMichael Tuexen 			if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
629117205eccSRandall Stewart 				/* assoc gone no un-lock */
6292c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6293c4739e2fSRandall Stewart 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
629417205eccSRandall Stewart 				*error = ENOBUFS;
629517205eccSRandall Stewart 				goto out_now;
629617205eccSRandall Stewart 			}
629717205eccSRandall Stewart 			added++;
6298ea5eba11SMichael Tuexen 			break;
6299ea5eba11SMichael Tuexen #endif
6300ea5eba11SMichael Tuexen #ifdef INET6
6301ea5eba11SMichael Tuexen 		case AF_INET6:
630217205eccSRandall Stewart 			incr = sizeof(struct sockaddr_in6);
630392776dfdSMichael Tuexen 			sin6 = (struct sockaddr_in6 *)sa;
630492776dfdSMichael Tuexen 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
630592776dfdSMichael Tuexen 			    IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
630692776dfdSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
630792776dfdSMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_8);
630892776dfdSMichael Tuexen 				*error = EINVAL;
630992776dfdSMichael Tuexen 				goto out_now;
631092776dfdSMichael Tuexen 			}
6311ca85e948SMichael Tuexen 			if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
631217205eccSRandall Stewart 				/* assoc gone no un-lock */
6313c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6314c4739e2fSRandall Stewart 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_8);
631517205eccSRandall Stewart 				*error = ENOBUFS;
631617205eccSRandall Stewart 				goto out_now;
631717205eccSRandall Stewart 			}
631817205eccSRandall Stewart 			added++;
6319ea5eba11SMichael Tuexen 			break;
6320ea5eba11SMichael Tuexen #endif
6321ea5eba11SMichael Tuexen 		default:
6322ea5eba11SMichael Tuexen 			break;
632317205eccSRandall Stewart 		}
632417205eccSRandall Stewart 		sa = (struct sockaddr *)((caddr_t)sa + incr);
632517205eccSRandall Stewart 	}
632617205eccSRandall Stewart out_now:
632717205eccSRandall Stewart 	return (added);
632817205eccSRandall Stewart }
632917205eccSRandall Stewart 
633017205eccSRandall Stewart struct sctp_tcb *
6331d61a0ae0SRandall Stewart sctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr,
6332d61a0ae0SRandall Stewart     int *totaddr, int *num_v4, int *num_v6, int *error,
6333d61a0ae0SRandall Stewart     int limit, int *bad_addr)
633417205eccSRandall Stewart {
633517205eccSRandall Stewart 	struct sockaddr *sa;
633617205eccSRandall Stewart 	struct sctp_tcb *stcb = NULL;
633717205eccSRandall Stewart 	size_t incr, at, i;
633817205eccSRandall Stewart 
633917205eccSRandall Stewart 	at = incr = 0;
634017205eccSRandall Stewart 	sa = addr;
6341ea5eba11SMichael Tuexen 
634217205eccSRandall Stewart 	*error = *num_v6 = *num_v4 = 0;
634317205eccSRandall Stewart 	/* account and validate addresses */
63444c9179adSRandall Stewart 	for (i = 0; i < (size_t)*totaddr; i++) {
6345ea5eba11SMichael Tuexen 		switch (sa->sa_family) {
6346ea5eba11SMichael Tuexen #ifdef INET
6347ea5eba11SMichael Tuexen 		case AF_INET:
634817205eccSRandall Stewart 			(*num_v4) += 1;
634917205eccSRandall Stewart 			incr = sizeof(struct sockaddr_in);
6350d61a0ae0SRandall Stewart 			if (sa->sa_len != incr) {
6351c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6352d61a0ae0SRandall Stewart 				*error = EINVAL;
6353d61a0ae0SRandall Stewart 				*bad_addr = 1;
6354d61a0ae0SRandall Stewart 				return (NULL);
6355d61a0ae0SRandall Stewart 			}
6356ea5eba11SMichael Tuexen 			break;
6357ea5eba11SMichael Tuexen #endif
6358ea5eba11SMichael Tuexen #ifdef INET6
6359ea5eba11SMichael Tuexen 		case AF_INET6:
6360ea5eba11SMichael Tuexen 			{
636117205eccSRandall Stewart 				struct sockaddr_in6 *sin6;
636217205eccSRandall Stewart 
636317205eccSRandall Stewart 				sin6 = (struct sockaddr_in6 *)sa;
636417205eccSRandall Stewart 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
636517205eccSRandall Stewart 					/* Must be non-mapped for connectx */
6366c4739e2fSRandall Stewart 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
636717205eccSRandall Stewart 					*error = EINVAL;
6368d61a0ae0SRandall Stewart 					*bad_addr = 1;
636917205eccSRandall Stewart 					return (NULL);
637017205eccSRandall Stewart 				}
637117205eccSRandall Stewart 				(*num_v6) += 1;
637217205eccSRandall Stewart 				incr = sizeof(struct sockaddr_in6);
6373d61a0ae0SRandall Stewart 				if (sa->sa_len != incr) {
6374c4739e2fSRandall Stewart 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6375d61a0ae0SRandall Stewart 					*error = EINVAL;
6376d61a0ae0SRandall Stewart 					*bad_addr = 1;
6377d61a0ae0SRandall Stewart 					return (NULL);
6378d61a0ae0SRandall Stewart 				}
6379ea5eba11SMichael Tuexen 				break;
6380ea5eba11SMichael Tuexen 			}
6381ea5eba11SMichael Tuexen #endif
6382ea5eba11SMichael Tuexen 		default:
638317205eccSRandall Stewart 			*totaddr = i;
638417205eccSRandall Stewart 			/* we are done */
638517205eccSRandall Stewart 			break;
638617205eccSRandall Stewart 		}
6387ea5eba11SMichael Tuexen 		if (i == (size_t)*totaddr) {
6388ea5eba11SMichael Tuexen 			break;
6389ea5eba11SMichael Tuexen 		}
6390d61a0ae0SRandall Stewart 		SCTP_INP_INCR_REF(inp);
639117205eccSRandall Stewart 		stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
639217205eccSRandall Stewart 		if (stcb != NULL) {
639317205eccSRandall Stewart 			/* Already have or am bring up an association */
639417205eccSRandall Stewart 			return (stcb);
6395d61a0ae0SRandall Stewart 		} else {
6396d61a0ae0SRandall Stewart 			SCTP_INP_DECR_REF(inp);
639717205eccSRandall Stewart 		}
63984c9179adSRandall Stewart 		if ((at + incr) > (size_t)limit) {
639917205eccSRandall Stewart 			*totaddr = i;
640017205eccSRandall Stewart 			break;
640117205eccSRandall Stewart 		}
640217205eccSRandall Stewart 		sa = (struct sockaddr *)((caddr_t)sa + incr);
640317205eccSRandall Stewart 	}
640417205eccSRandall Stewart 	return ((struct sctp_tcb *)NULL);
640517205eccSRandall Stewart }
640635918f85SRandall Stewart 
640735918f85SRandall Stewart /*
640835918f85SRandall Stewart  * sctp_bindx(ADD) for one address.
640935918f85SRandall Stewart  * assumes all arguments are valid/checked by caller.
641035918f85SRandall Stewart  */
641135918f85SRandall Stewart void
641235918f85SRandall Stewart sctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp,
641335918f85SRandall Stewart     struct sockaddr *sa, sctp_assoc_t assoc_id,
641435918f85SRandall Stewart     uint32_t vrf_id, int *error, void *p)
641535918f85SRandall Stewart {
641635918f85SRandall Stewart 	struct sockaddr *addr_touse;
64175e2c2d87SRandall Stewart 
64185e2c2d87SRandall Stewart #ifdef INET6
641935918f85SRandall Stewart 	struct sockaddr_in sin;
642035918f85SRandall Stewart 
64215e2c2d87SRandall Stewart #endif
64225e2c2d87SRandall Stewart 
642335918f85SRandall Stewart 	/* see if we're bound all already! */
642435918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6425c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
642635918f85SRandall Stewart 		*error = EINVAL;
642735918f85SRandall Stewart 		return;
642835918f85SRandall Stewart 	}
642935918f85SRandall Stewart 	addr_touse = sa;
6430ea5eba11SMichael Tuexen #ifdef INET6
643135918f85SRandall Stewart 	if (sa->sa_family == AF_INET6) {
643235918f85SRandall Stewart 		struct sockaddr_in6 *sin6;
643335918f85SRandall Stewart 
643435918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6435c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
643635918f85SRandall Stewart 			*error = EINVAL;
643735918f85SRandall Stewart 			return;
643835918f85SRandall Stewart 		}
6439db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6440db4fd95bSRandall Stewart 			/* can only bind v6 on PF_INET6 sockets */
6441c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6442db4fd95bSRandall Stewart 			*error = EINVAL;
6443db4fd95bSRandall Stewart 			return;
6444db4fd95bSRandall Stewart 		}
644535918f85SRandall Stewart 		sin6 = (struct sockaddr_in6 *)addr_touse;
644635918f85SRandall Stewart 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6447db4fd95bSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6448db4fd95bSRandall Stewart 			    SCTP_IPV6_V6ONLY(inp)) {
6449db4fd95bSRandall Stewart 				/* can't bind v4-mapped on PF_INET sockets */
6450c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6451db4fd95bSRandall Stewart 				*error = EINVAL;
6452db4fd95bSRandall Stewart 				return;
6453db4fd95bSRandall Stewart 			}
645435918f85SRandall Stewart 			in6_sin6_2_sin(&sin, sin6);
645535918f85SRandall Stewart 			addr_touse = (struct sockaddr *)&sin;
645635918f85SRandall Stewart 		}
645735918f85SRandall Stewart 	}
645835918f85SRandall Stewart #endif
6459ea5eba11SMichael Tuexen #ifdef INET
646035918f85SRandall Stewart 	if (sa->sa_family == AF_INET) {
646135918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in)) {
6462c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
646335918f85SRandall Stewart 			*error = EINVAL;
646435918f85SRandall Stewart 			return;
646535918f85SRandall Stewart 		}
6466db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6467db4fd95bSRandall Stewart 		    SCTP_IPV6_V6ONLY(inp)) {
6468db4fd95bSRandall Stewart 			/* can't bind v4 on PF_INET sockets */
6469c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6470db4fd95bSRandall Stewart 			*error = EINVAL;
6471db4fd95bSRandall Stewart 			return;
6472db4fd95bSRandall Stewart 		}
647335918f85SRandall Stewart 	}
6474ea5eba11SMichael Tuexen #endif
647535918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
647635918f85SRandall Stewart 		if (p == NULL) {
647735918f85SRandall Stewart 			/* Can't get proc for Net/Open BSD */
6478c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
647935918f85SRandall Stewart 			*error = EINVAL;
648035918f85SRandall Stewart 			return;
648135918f85SRandall Stewart 		}
64821b649582SRandall Stewart 		*error = sctp_inpcb_bind(so, addr_touse, NULL, p);
648335918f85SRandall Stewart 		return;
648435918f85SRandall Stewart 	}
648535918f85SRandall Stewart 	/*
648635918f85SRandall Stewart 	 * No locks required here since bind and mgmt_ep_sa all do their own
648735918f85SRandall Stewart 	 * locking. If we do something for the FIX: below we may need to
648835918f85SRandall Stewart 	 * lock in that case.
648935918f85SRandall Stewart 	 */
649035918f85SRandall Stewart 	if (assoc_id == 0) {
649135918f85SRandall Stewart 		/* add the address */
649235918f85SRandall Stewart 		struct sctp_inpcb *lep;
649397c76f10SRandall Stewart 		struct sockaddr_in *lsin = (struct sockaddr_in *)addr_touse;
649435918f85SRandall Stewart 
649597c76f10SRandall Stewart 		/* validate the incoming port */
649697c76f10SRandall Stewart 		if ((lsin->sin_port != 0) &&
649797c76f10SRandall Stewart 		    (lsin->sin_port != inp->sctp_lport)) {
6498c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
649997c76f10SRandall Stewart 			*error = EINVAL;
650097c76f10SRandall Stewart 			return;
650197c76f10SRandall Stewart 		} else {
650297c76f10SRandall Stewart 			/* user specified 0 port, set it to existing port */
650397c76f10SRandall Stewart 			lsin->sin_port = inp->sctp_lport;
650497c76f10SRandall Stewart 		}
650597c76f10SRandall Stewart 
650635918f85SRandall Stewart 		lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id);
650735918f85SRandall Stewart 		if (lep != NULL) {
650835918f85SRandall Stewart 			/*
650935918f85SRandall Stewart 			 * We must decrement the refcount since we have the
651035918f85SRandall Stewart 			 * ep already and are binding. No remove going on
651135918f85SRandall Stewart 			 * here.
651235918f85SRandall Stewart 			 */
65136d9e8f2bSRandall Stewart 			SCTP_INP_DECR_REF(lep);
651435918f85SRandall Stewart 		}
651535918f85SRandall Stewart 		if (lep == inp) {
651635918f85SRandall Stewart 			/* already bound to it.. ok */
651735918f85SRandall Stewart 			return;
651835918f85SRandall Stewart 		} else if (lep == NULL) {
651935918f85SRandall Stewart 			((struct sockaddr_in *)addr_touse)->sin_port = 0;
652035918f85SRandall Stewart 			*error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
652135918f85SRandall Stewart 			    SCTP_ADD_IP_ADDRESS,
652280fefe0aSRandall Stewart 			    vrf_id, NULL);
652335918f85SRandall Stewart 		} else {
652435918f85SRandall Stewart 			*error = EADDRINUSE;
652535918f85SRandall Stewart 		}
652635918f85SRandall Stewart 		if (*error)
652735918f85SRandall Stewart 			return;
652835918f85SRandall Stewart 	} else {
652935918f85SRandall Stewart 		/*
653035918f85SRandall Stewart 		 * FIX: decide whether we allow assoc based bindx
653135918f85SRandall Stewart 		 */
653235918f85SRandall Stewart 	}
653335918f85SRandall Stewart }
653435918f85SRandall Stewart 
653535918f85SRandall Stewart /*
653635918f85SRandall Stewart  * sctp_bindx(DELETE) for one address.
653735918f85SRandall Stewart  * assumes all arguments are valid/checked by caller.
653835918f85SRandall Stewart  */
653935918f85SRandall Stewart void
65407215cc1bSMichael Tuexen sctp_bindx_delete_address(struct sctp_inpcb *inp,
654135918f85SRandall Stewart     struct sockaddr *sa, sctp_assoc_t assoc_id,
654235918f85SRandall Stewart     uint32_t vrf_id, int *error)
654335918f85SRandall Stewart {
654435918f85SRandall Stewart 	struct sockaddr *addr_touse;
65455e2c2d87SRandall Stewart 
65465e2c2d87SRandall Stewart #ifdef INET6
654735918f85SRandall Stewart 	struct sockaddr_in sin;
654835918f85SRandall Stewart 
65495e2c2d87SRandall Stewart #endif
65505e2c2d87SRandall Stewart 
655135918f85SRandall Stewart 	/* see if we're bound all already! */
655235918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6553c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
655435918f85SRandall Stewart 		*error = EINVAL;
655535918f85SRandall Stewart 		return;
655635918f85SRandall Stewart 	}
655735918f85SRandall Stewart 	addr_touse = sa;
6558e0e00a4dSMichael Tuexen #ifdef INET6
655935918f85SRandall Stewart 	if (sa->sa_family == AF_INET6) {
656035918f85SRandall Stewart 		struct sockaddr_in6 *sin6;
656135918f85SRandall Stewart 
656235918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6563c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
656435918f85SRandall Stewart 			*error = EINVAL;
656535918f85SRandall Stewart 			return;
656635918f85SRandall Stewart 		}
6567db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6568db4fd95bSRandall Stewart 			/* can only bind v6 on PF_INET6 sockets */
6569c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6570db4fd95bSRandall Stewart 			*error = EINVAL;
6571db4fd95bSRandall Stewart 			return;
6572db4fd95bSRandall Stewart 		}
657335918f85SRandall Stewart 		sin6 = (struct sockaddr_in6 *)addr_touse;
657435918f85SRandall Stewart 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6575db4fd95bSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6576db4fd95bSRandall Stewart 			    SCTP_IPV6_V6ONLY(inp)) {
6577db4fd95bSRandall Stewart 				/* can't bind mapped-v4 on PF_INET sockets */
6578c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6579db4fd95bSRandall Stewart 				*error = EINVAL;
6580db4fd95bSRandall Stewart 				return;
6581db4fd95bSRandall Stewart 			}
658235918f85SRandall Stewart 			in6_sin6_2_sin(&sin, sin6);
658335918f85SRandall Stewart 			addr_touse = (struct sockaddr *)&sin;
658435918f85SRandall Stewart 		}
658535918f85SRandall Stewart 	}
658635918f85SRandall Stewart #endif
6587ea5eba11SMichael Tuexen #ifdef INET
658835918f85SRandall Stewart 	if (sa->sa_family == AF_INET) {
658935918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in)) {
6590c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
659135918f85SRandall Stewart 			*error = EINVAL;
659235918f85SRandall Stewart 			return;
659335918f85SRandall Stewart 		}
6594db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6595db4fd95bSRandall Stewart 		    SCTP_IPV6_V6ONLY(inp)) {
6596db4fd95bSRandall Stewart 			/* can't bind v4 on PF_INET sockets */
6597c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6598db4fd95bSRandall Stewart 			*error = EINVAL;
6599db4fd95bSRandall Stewart 			return;
6600db4fd95bSRandall Stewart 		}
660135918f85SRandall Stewart 	}
6602ea5eba11SMichael Tuexen #endif
660335918f85SRandall Stewart 	/*
660435918f85SRandall Stewart 	 * No lock required mgmt_ep_sa does its own locking. If the FIX:
660535918f85SRandall Stewart 	 * below is ever changed we may need to lock before calling
660635918f85SRandall Stewart 	 * association level binding.
660735918f85SRandall Stewart 	 */
660835918f85SRandall Stewart 	if (assoc_id == 0) {
660935918f85SRandall Stewart 		/* delete the address */
661035918f85SRandall Stewart 		*error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
661135918f85SRandall Stewart 		    SCTP_DEL_IP_ADDRESS,
661280fefe0aSRandall Stewart 		    vrf_id, NULL);
661335918f85SRandall Stewart 	} else {
661435918f85SRandall Stewart 		/*
661535918f85SRandall Stewart 		 * FIX: decide whether we allow assoc based bindx
661635918f85SRandall Stewart 		 */
661735918f85SRandall Stewart 	}
661835918f85SRandall Stewart }
66191b649582SRandall Stewart 
66201b649582SRandall Stewart /*
66211b649582SRandall Stewart  * returns the valid local address count for an assoc, taking into account
66221b649582SRandall Stewart  * all scoping rules
66231b649582SRandall Stewart  */
66241b649582SRandall Stewart int
66251b649582SRandall Stewart sctp_local_addr_count(struct sctp_tcb *stcb)
66261b649582SRandall Stewart {
6627b54ddf22SMichael Tuexen 	int loopback_scope;
6628b54ddf22SMichael Tuexen 
6629b54ddf22SMichael Tuexen #if defined(INET)
6630b54ddf22SMichael Tuexen 	int ipv4_local_scope, ipv4_addr_legal;
6631b54ddf22SMichael Tuexen 
6632b54ddf22SMichael Tuexen #endif
6633b54ddf22SMichael Tuexen #if defined (INET6)
6634b54ddf22SMichael Tuexen 	int local_scope, site_scope, ipv6_addr_legal;
6635b54ddf22SMichael Tuexen 
6636b54ddf22SMichael Tuexen #endif
66371b649582SRandall Stewart 	struct sctp_vrf *vrf;
66381b649582SRandall Stewart 	struct sctp_ifn *sctp_ifn;
66391b649582SRandall Stewart 	struct sctp_ifa *sctp_ifa;
66401b649582SRandall Stewart 	int count = 0;
66411b649582SRandall Stewart 
66421b649582SRandall Stewart 	/* Turn on all the appropriate scopes */
6643a1cb341bSMichael Tuexen 	loopback_scope = stcb->asoc.scope.loopback_scope;
6644b54ddf22SMichael Tuexen #if defined(INET)
6645a1cb341bSMichael Tuexen 	ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
6646b54ddf22SMichael Tuexen 	ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
6647b54ddf22SMichael Tuexen #endif
6648b54ddf22SMichael Tuexen #if defined(INET6)
6649a1cb341bSMichael Tuexen 	local_scope = stcb->asoc.scope.local_scope;
6650a1cb341bSMichael Tuexen 	site_scope = stcb->asoc.scope.site_scope;
6651a1cb341bSMichael Tuexen 	ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
6652b54ddf22SMichael Tuexen #endif
6653c99efcf6SRandall Stewart 	SCTP_IPI_ADDR_RLOCK();
66541b649582SRandall Stewart 	vrf = sctp_find_vrf(stcb->asoc.vrf_id);
66551b649582SRandall Stewart 	if (vrf == NULL) {
66561b649582SRandall Stewart 		/* no vrf, no addresses */
6657c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RUNLOCK();
66581b649582SRandall Stewart 		return (0);
66591b649582SRandall Stewart 	}
66601b649582SRandall Stewart 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
66611b649582SRandall Stewart 		/*
66621b649582SRandall Stewart 		 * bound all case: go through all ifns on the vrf
66631b649582SRandall Stewart 		 */
66641b649582SRandall Stewart 		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
66651b649582SRandall Stewart 			if ((loopback_scope == 0) &&
66661b649582SRandall Stewart 			    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
66671b649582SRandall Stewart 				continue;
66681b649582SRandall Stewart 			}
66691b649582SRandall Stewart 			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
66701b649582SRandall Stewart 				if (sctp_is_addr_restricted(stcb, sctp_ifa))
66711b649582SRandall Stewart 					continue;
66725e2c2d87SRandall Stewart 				switch (sctp_ifa->address.sa.sa_family) {
6673ea5eba11SMichael Tuexen #ifdef INET
66745e2c2d87SRandall Stewart 				case AF_INET:
66755e2c2d87SRandall Stewart 					if (ipv4_addr_legal) {
66761b649582SRandall Stewart 						struct sockaddr_in *sin;
66771b649582SRandall Stewart 
66781b649582SRandall Stewart 						sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
66791b649582SRandall Stewart 						if (sin->sin_addr.s_addr == 0) {
66805e2c2d87SRandall Stewart 							/*
66815e2c2d87SRandall Stewart 							 * skip unspecified
66825e2c2d87SRandall Stewart 							 * addrs
66835e2c2d87SRandall Stewart 							 */
66841b649582SRandall Stewart 							continue;
66851b649582SRandall Stewart 						}
66866ba22f19SMichael Tuexen 						if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
66876ba22f19SMichael Tuexen 						    &sin->sin_addr) != 0) {
66886ba22f19SMichael Tuexen 							continue;
66896ba22f19SMichael Tuexen 						}
66901b649582SRandall Stewart 						if ((ipv4_local_scope == 0) &&
66911b649582SRandall Stewart 						    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
66921b649582SRandall Stewart 							continue;
66931b649582SRandall Stewart 						}
66941b649582SRandall Stewart 						/* count this one */
66951b649582SRandall Stewart 						count++;
66965e2c2d87SRandall Stewart 					} else {
66975e2c2d87SRandall Stewart 						continue;
66985e2c2d87SRandall Stewart 					}
66995e2c2d87SRandall Stewart 					break;
6700ea5eba11SMichael Tuexen #endif
67015e2c2d87SRandall Stewart #ifdef INET6
67025e2c2d87SRandall Stewart 				case AF_INET6:
67035e2c2d87SRandall Stewart 					if (ipv6_addr_legal) {
67041b649582SRandall Stewart 						struct sockaddr_in6 *sin6;
67051b649582SRandall Stewart 
67061b649582SRandall Stewart 						sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
67071b649582SRandall Stewart 						if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
67081b649582SRandall Stewart 							continue;
67091b649582SRandall Stewart 						}
67106ba22f19SMichael Tuexen 						if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
67116ba22f19SMichael Tuexen 						    &sin6->sin6_addr) != 0) {
67126ba22f19SMichael Tuexen 							continue;
67136ba22f19SMichael Tuexen 						}
67141b649582SRandall Stewart 						if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
67151b649582SRandall Stewart 							if (local_scope == 0)
67161b649582SRandall Stewart 								continue;
67171b649582SRandall Stewart 							if (sin6->sin6_scope_id == 0) {
67181b649582SRandall Stewart 								if (sa6_recoverscope(sin6) != 0)
67191b649582SRandall Stewart 									/*
67205e2c2d87SRandall Stewart 									 *
67215e2c2d87SRandall Stewart 									 * bad
67225e2c2d87SRandall Stewart 									 *
67235e2c2d87SRandall Stewart 									 * li
67245e2c2d87SRandall Stewart 									 * nk
67255e2c2d87SRandall Stewart 									 *
67265e2c2d87SRandall Stewart 									 * loc
67275e2c2d87SRandall Stewart 									 * al
67285e2c2d87SRandall Stewart 									 *
67295e2c2d87SRandall Stewart 									 * add
67305e2c2d87SRandall Stewart 									 * re
67315e2c2d87SRandall Stewart 									 * ss
67325e2c2d87SRandall Stewart 									 * */
67331b649582SRandall Stewart 									continue;
67341b649582SRandall Stewart 							}
67351b649582SRandall Stewart 						}
67361b649582SRandall Stewart 						if ((site_scope == 0) &&
67371b649582SRandall Stewart 						    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
67381b649582SRandall Stewart 							continue;
67391b649582SRandall Stewart 						}
67401b649582SRandall Stewart 						/* count this one */
67411b649582SRandall Stewart 						count++;
67421b649582SRandall Stewart 					}
67435e2c2d87SRandall Stewart 					break;
67445e2c2d87SRandall Stewart #endif
67455e2c2d87SRandall Stewart 				default:
67465e2c2d87SRandall Stewart 					/* TSNH */
67475e2c2d87SRandall Stewart 					break;
67485e2c2d87SRandall Stewart 				}
67491b649582SRandall Stewart 			}
67501b649582SRandall Stewart 		}
67511b649582SRandall Stewart 	} else {
67521b649582SRandall Stewart 		/*
67531b649582SRandall Stewart 		 * subset bound case
67541b649582SRandall Stewart 		 */
67551b649582SRandall Stewart 		struct sctp_laddr *laddr;
67561b649582SRandall Stewart 
67571b649582SRandall Stewart 		LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list,
67581b649582SRandall Stewart 		    sctp_nxt_addr) {
67591b649582SRandall Stewart 			if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
67601b649582SRandall Stewart 				continue;
67611b649582SRandall Stewart 			}
67621b649582SRandall Stewart 			/* count this one */
67631b649582SRandall Stewart 			count++;
67641b649582SRandall Stewart 		}
67651b649582SRandall Stewart 	}
6766c99efcf6SRandall Stewart 	SCTP_IPI_ADDR_RUNLOCK();
67671b649582SRandall Stewart 	return (count);
67681b649582SRandall Stewart }
6769c4739e2fSRandall Stewart 
6770c4739e2fSRandall Stewart #if defined(SCTP_LOCAL_TRACE_BUF)
6771c4739e2fSRandall Stewart 
6772c4739e2fSRandall Stewart void
6773b27a6b7dSRandall 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)
6774c4739e2fSRandall Stewart {
6775b27a6b7dSRandall Stewart 	uint32_t saveindex, newindex;
6776c4739e2fSRandall Stewart 
6777c4739e2fSRandall Stewart 	do {
6778b3f1ea41SRandall Stewart 		saveindex = SCTP_BASE_SYSCTL(sctp_log).index;
6779c4739e2fSRandall Stewart 		if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
6780c4739e2fSRandall Stewart 			newindex = 1;
6781c4739e2fSRandall Stewart 		} else {
6782c4739e2fSRandall Stewart 			newindex = saveindex + 1;
6783c4739e2fSRandall Stewart 		}
6784b3f1ea41SRandall Stewart 	} while (atomic_cmpset_int(&SCTP_BASE_SYSCTL(sctp_log).index, saveindex, newindex) == 0);
6785c4739e2fSRandall Stewart 	if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
6786c4739e2fSRandall Stewart 		saveindex = 0;
6787c4739e2fSRandall Stewart 	}
6788b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].timestamp = SCTP_GET_CYCLECOUNT;
6789b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].subsys = subsys;
6790b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[0] = a;
6791b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[1] = b;
6792b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[2] = c;
6793b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[3] = d;
6794b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[4] = e;
6795b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[5] = f;
6796c4739e2fSRandall Stewart }
6797c4739e2fSRandall Stewart 
6798c4739e2fSRandall Stewart #endif
6799a99b6783SRandall Stewart static void
6800a99b6783SRandall Stewart sctp_recv_udp_tunneled_packet(struct mbuf *m, int off, struct inpcb *ignored)
6801a99b6783SRandall Stewart {
6802a99b6783SRandall Stewart 	struct ip *iph;
68033a51a264SMichael Tuexen 
68043a51a264SMichael Tuexen #ifdef INET6
68053a51a264SMichael Tuexen 	struct ip6_hdr *ip6;
68063a51a264SMichael Tuexen 
68073a51a264SMichael Tuexen #endif
6808a99b6783SRandall Stewart 	struct mbuf *sp, *last;
6809a99b6783SRandall Stewart 	struct udphdr *uhdr;
6810285052f0SMichael Tuexen 	uint16_t port;
6811a99b6783SRandall Stewart 
6812a99b6783SRandall Stewart 	if ((m->m_flags & M_PKTHDR) == 0) {
6813a99b6783SRandall Stewart 		/* Can't handle one that is not a pkt hdr */
6814a99b6783SRandall Stewart 		goto out;
6815a99b6783SRandall Stewart 	}
6816285052f0SMichael Tuexen 	/* Pull the src port */
6817a99b6783SRandall Stewart 	iph = mtod(m, struct ip *);
6818a99b6783SRandall Stewart 	uhdr = (struct udphdr *)((caddr_t)iph + off);
6819a99b6783SRandall Stewart 	port = uhdr->uh_sport;
6820285052f0SMichael Tuexen 	/*
6821285052f0SMichael Tuexen 	 * Split out the mbuf chain. Leave the IP header in m, place the
6822285052f0SMichael Tuexen 	 * rest in the sp.
6823285052f0SMichael Tuexen 	 */
6824eb1b1807SGleb Smirnoff 	sp = m_split(m, off, M_NOWAIT);
6825a99b6783SRandall Stewart 	if (sp == NULL) {
6826a99b6783SRandall Stewart 		/* Gak, drop packet, we can't do a split */
6827a99b6783SRandall Stewart 		goto out;
6828a99b6783SRandall Stewart 	}
6829285052f0SMichael Tuexen 	if (sp->m_pkthdr.len < sizeof(struct udphdr) + sizeof(struct sctphdr)) {
6830285052f0SMichael Tuexen 		/* Gak, packet can't have an SCTP header in it - too small */
6831a99b6783SRandall Stewart 		m_freem(sp);
6832a99b6783SRandall Stewart 		goto out;
6833a99b6783SRandall Stewart 	}
6834285052f0SMichael Tuexen 	/* Now pull up the UDP header and SCTP header together */
6835285052f0SMichael Tuexen 	sp = m_pullup(sp, sizeof(struct udphdr) + sizeof(struct sctphdr));
6836a99b6783SRandall Stewart 	if (sp == NULL) {
6837a99b6783SRandall Stewart 		/* Gak pullup failed */
6838a99b6783SRandall Stewart 		goto out;
6839a99b6783SRandall Stewart 	}
6840285052f0SMichael Tuexen 	/* Trim out the UDP header */
6841a99b6783SRandall Stewart 	m_adj(sp, sizeof(struct udphdr));
6842a99b6783SRandall Stewart 
6843a99b6783SRandall Stewart 	/* Now reconstruct the mbuf chain */
6844285052f0SMichael Tuexen 	for (last = m; last->m_next; last = last->m_next);
6845a99b6783SRandall Stewart 	last->m_next = sp;
6846a99b6783SRandall Stewart 	m->m_pkthdr.len += sp->m_pkthdr.len;
6847a99b6783SRandall Stewart 	iph = mtod(m, struct ip *);
6848a99b6783SRandall Stewart 	switch (iph->ip_v) {
6849e6194c2eSMichael Tuexen #ifdef INET
6850a99b6783SRandall Stewart 	case IPVERSION:
685109c1c856SMichael Tuexen 		iph->ip_len = htons(ntohs(iph->ip_len) - sizeof(struct udphdr));
6852a99b6783SRandall Stewart 		sctp_input_with_port(m, off, port);
6853a99b6783SRandall Stewart 		break;
6854e6194c2eSMichael Tuexen #endif
6855a99b6783SRandall Stewart #ifdef INET6
6856a99b6783SRandall Stewart 	case IPV6_VERSION >> 4:
68573a51a264SMichael Tuexen 		ip6 = mtod(m, struct ip6_hdr *);
68583a51a264SMichael Tuexen 		ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - sizeof(struct udphdr));
68593a51a264SMichael Tuexen 		sctp6_input_with_port(&m, &off, port);
6860a99b6783SRandall Stewart 		break;
6861a99b6783SRandall Stewart #endif
6862a99b6783SRandall Stewart 	default:
6863285052f0SMichael Tuexen 		goto out;
6864a99b6783SRandall Stewart 		break;
6865a99b6783SRandall Stewart 	}
6866a99b6783SRandall Stewart 	return;
6867a99b6783SRandall Stewart out:
6868a99b6783SRandall Stewart 	m_freem(m);
6869a99b6783SRandall Stewart }
6870c54a18d2SRandall Stewart 
6871c54a18d2SRandall Stewart void
6872c54a18d2SRandall Stewart sctp_over_udp_stop(void)
6873c54a18d2SRandall Stewart {
6874a99b6783SRandall Stewart 	/*
6875a99b6783SRandall Stewart 	 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
6876a99b6783SRandall Stewart 	 * for writting!
6877a99b6783SRandall Stewart 	 */
68783a51a264SMichael Tuexen #ifdef INET
68793a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
68803a51a264SMichael Tuexen 		soclose(SCTP_BASE_INFO(udp4_tun_socket));
68813a51a264SMichael Tuexen 		SCTP_BASE_INFO(udp4_tun_socket) = NULL;
6882c54a18d2SRandall Stewart 	}
68833a51a264SMichael Tuexen #endif
68843a51a264SMichael Tuexen #ifdef INET6
68853a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
68863a51a264SMichael Tuexen 		soclose(SCTP_BASE_INFO(udp6_tun_socket));
68873a51a264SMichael Tuexen 		SCTP_BASE_INFO(udp6_tun_socket) = NULL;
68883a51a264SMichael Tuexen 	}
68893a51a264SMichael Tuexen #endif
6890a99b6783SRandall Stewart }
6891ea5eba11SMichael Tuexen 
6892c54a18d2SRandall Stewart int
6893c54a18d2SRandall Stewart sctp_over_udp_start(void)
6894c54a18d2SRandall Stewart {
6895a99b6783SRandall Stewart 	uint16_t port;
6896a99b6783SRandall Stewart 	int ret;
6897a99b6783SRandall Stewart 
68983a51a264SMichael Tuexen #ifdef INET
68993a51a264SMichael Tuexen 	struct sockaddr_in sin;
69003a51a264SMichael Tuexen 
69013a51a264SMichael Tuexen #endif
69023a51a264SMichael Tuexen #ifdef INET6
69033a51a264SMichael Tuexen 	struct sockaddr_in6 sin6;
69043a51a264SMichael Tuexen 
69053a51a264SMichael Tuexen #endif
6906a99b6783SRandall Stewart 	/*
6907a99b6783SRandall Stewart 	 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
6908a99b6783SRandall Stewart 	 * for writting!
6909a99b6783SRandall Stewart 	 */
6910a99b6783SRandall Stewart 	port = SCTP_BASE_SYSCTL(sctp_udp_tunneling_port);
69113a51a264SMichael Tuexen 	if (ntohs(port) == 0) {
6912a99b6783SRandall Stewart 		/* Must have a port set */
6913a99b6783SRandall Stewart 		return (EINVAL);
6914a99b6783SRandall Stewart 	}
69153a51a264SMichael Tuexen #ifdef INET
69163a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
6917a99b6783SRandall Stewart 		/* Already running -- must stop first */
6918a99b6783SRandall Stewart 		return (EALREADY);
6919a99b6783SRandall Stewart 	}
69203a51a264SMichael Tuexen #endif
69213a51a264SMichael Tuexen #ifdef INET6
69223a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
69233a51a264SMichael Tuexen 		/* Already running -- must stop first */
69243a51a264SMichael Tuexen 		return (EALREADY);
6925a99b6783SRandall Stewart 	}
69263a51a264SMichael Tuexen #endif
69273a51a264SMichael Tuexen #ifdef INET
69283a51a264SMichael Tuexen 	if ((ret = socreate(PF_INET, &SCTP_BASE_INFO(udp4_tun_socket),
69293a51a264SMichael Tuexen 	    SOCK_DGRAM, IPPROTO_UDP,
69303a51a264SMichael Tuexen 	    curthread->td_ucred, curthread))) {
6931a99b6783SRandall Stewart 		sctp_over_udp_stop();
6932a99b6783SRandall Stewart 		return (ret);
6933a99b6783SRandall Stewart 	}
69343a51a264SMichael Tuexen 	/* Call the special UDP hook. */
69353a51a264SMichael Tuexen 	if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp4_tun_socket),
69363a51a264SMichael Tuexen 	    sctp_recv_udp_tunneled_packet))) {
69373a51a264SMichael Tuexen 		sctp_over_udp_stop();
69383a51a264SMichael Tuexen 		return (ret);
69393a51a264SMichael Tuexen 	}
69403a51a264SMichael Tuexen 	/* Ok, we have a socket, bind it to the port. */
69413a51a264SMichael Tuexen 	memset(&sin, 0, sizeof(struct sockaddr_in));
69423a51a264SMichael Tuexen 	sin.sin_len = sizeof(struct sockaddr_in);
69433a51a264SMichael Tuexen 	sin.sin_family = AF_INET;
69443a51a264SMichael Tuexen 	sin.sin_port = htons(port);
69453a51a264SMichael Tuexen 	if ((ret = sobind(SCTP_BASE_INFO(udp4_tun_socket),
69463a51a264SMichael Tuexen 	    (struct sockaddr *)&sin, curthread))) {
69473a51a264SMichael Tuexen 		sctp_over_udp_stop();
69483a51a264SMichael Tuexen 		return (ret);
69493a51a264SMichael Tuexen 	}
69503a51a264SMichael Tuexen #endif
69513a51a264SMichael Tuexen #ifdef INET6
69523a51a264SMichael Tuexen 	if ((ret = socreate(PF_INET6, &SCTP_BASE_INFO(udp6_tun_socket),
69533a51a264SMichael Tuexen 	    SOCK_DGRAM, IPPROTO_UDP,
69543a51a264SMichael Tuexen 	    curthread->td_ucred, curthread))) {
69553a51a264SMichael Tuexen 		sctp_over_udp_stop();
69563a51a264SMichael Tuexen 		return (ret);
69573a51a264SMichael Tuexen 	}
69583a51a264SMichael Tuexen 	/* Call the special UDP hook. */
69593a51a264SMichael Tuexen 	if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp6_tun_socket),
69603a51a264SMichael Tuexen 	    sctp_recv_udp_tunneled_packet))) {
69613a51a264SMichael Tuexen 		sctp_over_udp_stop();
69623a51a264SMichael Tuexen 		return (ret);
69633a51a264SMichael Tuexen 	}
69643a51a264SMichael Tuexen 	/* Ok, we have a socket, bind it to the port. */
69653a51a264SMichael Tuexen 	memset(&sin6, 0, sizeof(struct sockaddr_in6));
69663a51a264SMichael Tuexen 	sin6.sin6_len = sizeof(struct sockaddr_in6);
69673a51a264SMichael Tuexen 	sin6.sin6_family = AF_INET6;
69683a51a264SMichael Tuexen 	sin6.sin6_port = htons(port);
69693a51a264SMichael Tuexen 	if ((ret = sobind(SCTP_BASE_INFO(udp6_tun_socket),
69703a51a264SMichael Tuexen 	    (struct sockaddr *)&sin6, curthread))) {
69713a51a264SMichael Tuexen 		sctp_over_udp_stop();
69723a51a264SMichael Tuexen 		return (ret);
69733a51a264SMichael Tuexen 	}
69743a51a264SMichael Tuexen #endif
6975a99b6783SRandall Stewart 	return (0);
6976c54a18d2SRandall Stewart }
6977