xref: /freebsd/sys/netinet/sctputil.c (revision b60b0fe6fdfda5fd6ad513de68d870b4b54b04da)
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 
899f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
900f0396ad1SMichael Tuexen 	int j;
901f0396ad1SMichael Tuexen 
902f0396ad1SMichael Tuexen #endif
903f0396ad1SMichael Tuexen 
9040696e120SRandall Stewart 	asoc = &stcb->asoc;
905f8829a4aSRandall Stewart 	/* init all variables to a known value. */
906c4739e2fSRandall Stewart 	SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_INUSE);
907a1cb341bSMichael Tuexen 	asoc->max_burst = inp->sctp_ep.max_burst;
908a1cb341bSMichael Tuexen 	asoc->fr_max_burst = inp->sctp_ep.fr_max_burst;
909a1cb341bSMichael Tuexen 	asoc->heart_beat_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
910a1cb341bSMichael Tuexen 	asoc->cookie_life = inp->sctp_ep.def_cookie_life;
911a1cb341bSMichael Tuexen 	asoc->sctp_cmt_on_off = inp->sctp_cmt_on_off;
912f342355aSMichael Tuexen 	asoc->ecn_supported = inp->ecn_supported;
913dd973b0eSMichael Tuexen 	asoc->prsctp_supported = inp->prsctp_supported;
914c79bec9cSMichael Tuexen 	asoc->auth_supported = inp->auth_supported;
915c79bec9cSMichael Tuexen 	asoc->asconf_supported = inp->asconf_supported;
916317e00efSMichael Tuexen 	asoc->reconfig_supported = inp->reconfig_supported;
917caea9879SMichael Tuexen 	asoc->nrsack_supported = inp->nrsack_supported;
918cb9b8e6fSMichael Tuexen 	asoc->pktdrop_supported = inp->pktdrop_supported;
919ca85e948SMichael Tuexen 	asoc->sctp_cmt_pf = (uint8_t) 0;
920a1cb341bSMichael Tuexen 	asoc->sctp_frag_point = inp->sctp_frag_point;
921a1cb341bSMichael Tuexen 	asoc->sctp_features = inp->sctp_features;
922a1cb341bSMichael Tuexen 	asoc->default_dscp = inp->sctp_ep.default_dscp;
92342551e99SRandall Stewart #ifdef INET6
924a1cb341bSMichael Tuexen 	if (inp->sctp_ep.default_flowlabel) {
925a1cb341bSMichael Tuexen 		asoc->default_flowlabel = inp->sctp_ep.default_flowlabel;
92658bdb691SMichael Tuexen 	} else {
927a1cb341bSMichael Tuexen 		if (inp->ip_inp.inp.inp_flags & IN6P_AUTOFLOWLABEL) {
928a1cb341bSMichael Tuexen 			asoc->default_flowlabel = sctp_select_initial_TSN(&inp->sctp_ep);
92958bdb691SMichael Tuexen 			asoc->default_flowlabel &= 0x000fffff;
93058bdb691SMichael Tuexen 			asoc->default_flowlabel |= 0x80000000;
93158bdb691SMichael Tuexen 		} else {
932f8829a4aSRandall Stewart 			asoc->default_flowlabel = 0;
93358bdb691SMichael Tuexen 		}
93458bdb691SMichael Tuexen 	}
935f8829a4aSRandall Stewart #endif
9369f22f500SRandall Stewart 	asoc->sb_send_resv = 0;
937f8829a4aSRandall Stewart 	if (override_tag) {
938f8829a4aSRandall Stewart 		asoc->my_vtag = override_tag;
939f8829a4aSRandall Stewart 	} else {
940a1cb341bSMichael Tuexen 		asoc->my_vtag = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
941f8829a4aSRandall Stewart 	}
942de0e935bSRandall Stewart 	/* Get the nonce tags */
943a1cb341bSMichael Tuexen 	asoc->my_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
944a1cb341bSMichael Tuexen 	asoc->peer_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
94542551e99SRandall Stewart 	asoc->vrf_id = vrf_id;
946de0e935bSRandall Stewart 
94718e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
94818e198d3SRandall Stewart 	asoc->tsn_in_at = 0;
94918e198d3SRandall Stewart 	asoc->tsn_out_at = 0;
95018e198d3SRandall Stewart 	asoc->tsn_in_wrapped = 0;
95118e198d3SRandall Stewart 	asoc->tsn_out_wrapped = 0;
95218e198d3SRandall Stewart 	asoc->cumack_log_at = 0;
953b201f536SRandall Stewart 	asoc->cumack_log_atsnt = 0;
95418e198d3SRandall Stewart #endif
95518e198d3SRandall Stewart #ifdef SCTP_FS_SPEC_LOG
95618e198d3SRandall Stewart 	asoc->fs_index = 0;
95718e198d3SRandall Stewart #endif
958f8829a4aSRandall Stewart 	asoc->refcnt = 0;
959f8829a4aSRandall Stewart 	asoc->assoc_up_sent = 0;
960f8829a4aSRandall Stewart 	asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number = asoc->sending_seq =
961a1cb341bSMichael Tuexen 	    sctp_select_initial_TSN(&inp->sctp_ep);
962c54a18d2SRandall Stewart 	asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
963f8829a4aSRandall Stewart 	/* we are optimisitic here */
964830d754dSRandall Stewart 	asoc->peer_supports_nat = 0;
965f8829a4aSRandall Stewart 	asoc->sent_queue_retran_cnt = 0;
966f8829a4aSRandall Stewart 
967f8829a4aSRandall Stewart 	/* for CMT */
9688933fa13SRandall Stewart 	asoc->last_net_cmt_send_started = NULL;
969f8829a4aSRandall Stewart 
970f8829a4aSRandall Stewart 	/* This will need to be adjusted */
971f8829a4aSRandall Stewart 	asoc->last_acked_seq = asoc->init_seq_number - 1;
972f8829a4aSRandall Stewart 	asoc->advanced_peer_ack_point = asoc->last_acked_seq;
973f8829a4aSRandall Stewart 	asoc->asconf_seq_in = asoc->last_acked_seq;
974f8829a4aSRandall Stewart 
975f8829a4aSRandall Stewart 	/* here we are different, we hold the next one we expect */
976f8829a4aSRandall Stewart 	asoc->str_reset_seq_in = asoc->last_acked_seq + 1;
977f8829a4aSRandall Stewart 
978a1cb341bSMichael Tuexen 	asoc->initial_init_rto_max = inp->sctp_ep.initial_init_rto_max;
979a1cb341bSMichael Tuexen 	asoc->initial_rto = inp->sctp_ep.initial_rto;
980f8829a4aSRandall Stewart 
981a1cb341bSMichael Tuexen 	asoc->max_init_times = inp->sctp_ep.max_init_times;
982a1cb341bSMichael Tuexen 	asoc->max_send_times = inp->sctp_ep.max_send_times;
983a1cb341bSMichael Tuexen 	asoc->def_net_failure = inp->sctp_ep.def_net_failure;
984a1cb341bSMichael Tuexen 	asoc->def_net_pf_threshold = inp->sctp_ep.def_net_pf_threshold;
985f8829a4aSRandall Stewart 	asoc->free_chunk_cnt = 0;
986f8829a4aSRandall Stewart 
987f8829a4aSRandall Stewart 	asoc->iam_blocking = 0;
988a1cb341bSMichael Tuexen 	asoc->context = inp->sctp_context;
989a1cb341bSMichael Tuexen 	asoc->local_strreset_support = inp->local_strreset_support;
990a1cb341bSMichael Tuexen 	asoc->def_send = inp->def_send;
991a1cb341bSMichael Tuexen 	asoc->delayed_ack = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
992a1cb341bSMichael Tuexen 	asoc->sack_freq = inp->sctp_ep.sctp_sack_freq;
993f8829a4aSRandall Stewart 	asoc->pr_sctp_cnt = 0;
994f8829a4aSRandall Stewart 	asoc->total_output_queue_size = 0;
995f8829a4aSRandall Stewart 
996a1cb341bSMichael Tuexen 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
997a1cb341bSMichael Tuexen 		asoc->scope.ipv6_addr_legal = 1;
998a1cb341bSMichael Tuexen 		if (SCTP_IPV6_V6ONLY(inp) == 0) {
999a1cb341bSMichael Tuexen 			asoc->scope.ipv4_addr_legal = 1;
1000f8829a4aSRandall Stewart 		} else {
1001a1cb341bSMichael Tuexen 			asoc->scope.ipv4_addr_legal = 0;
1002f8829a4aSRandall Stewart 		}
1003f8829a4aSRandall Stewart 	} else {
1004a1cb341bSMichael Tuexen 		asoc->scope.ipv6_addr_legal = 0;
1005a1cb341bSMichael Tuexen 		asoc->scope.ipv4_addr_legal = 1;
1006f8829a4aSRandall Stewart 	}
1007f8829a4aSRandall Stewart 
1008a1cb341bSMichael Tuexen 	asoc->my_rwnd = max(SCTP_SB_LIMIT_RCV(inp->sctp_socket), SCTP_MINIMAL_RWND);
1009a1cb341bSMichael Tuexen 	asoc->peers_rwnd = SCTP_SB_LIMIT_RCV(inp->sctp_socket);
1010f8829a4aSRandall Stewart 
1011a1cb341bSMichael Tuexen 	asoc->smallest_mtu = inp->sctp_frag_point;
1012a1cb341bSMichael Tuexen 	asoc->minrto = inp->sctp_ep.sctp_minrto;
1013a1cb341bSMichael Tuexen 	asoc->maxrto = inp->sctp_ep.sctp_maxrto;
1014f8829a4aSRandall Stewart 
1015f8829a4aSRandall Stewart 	asoc->locked_on_sending = NULL;
1016f8829a4aSRandall Stewart 	asoc->stream_locked_on = 0;
1017f8829a4aSRandall Stewart 	asoc->ecn_echo_cnt_onq = 0;
1018f8829a4aSRandall Stewart 	asoc->stream_locked = 0;
1019f8829a4aSRandall Stewart 
102042551e99SRandall Stewart 	asoc->send_sack = 1;
102142551e99SRandall Stewart 
102242551e99SRandall Stewart 	LIST_INIT(&asoc->sctp_restricted_addrs);
102342551e99SRandall Stewart 
1024f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->nets);
1025f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->pending_reply_queue);
10262afb3e84SRandall Stewart 	TAILQ_INIT(&asoc->asconf_ack_sent);
1027f8829a4aSRandall Stewart 	/* Setup to fill the hb random cache at first HB */
1028f8829a4aSRandall Stewart 	asoc->hb_random_idx = 4;
1029f8829a4aSRandall Stewart 
1030a1cb341bSMichael Tuexen 	asoc->sctp_autoclose_ticks = inp->sctp_ep.auto_close_time;
1031f8829a4aSRandall Stewart 
1032a1cb341bSMichael Tuexen 	stcb->asoc.congestion_control_module = inp->sctp_ep.sctp_default_cc_module;
1033a1cb341bSMichael Tuexen 	stcb->asoc.cc_functions = sctp_cc_functions[inp->sctp_ep.sctp_default_cc_module];
1034b54d3a6cSRandall Stewart 
1035a1cb341bSMichael Tuexen 	stcb->asoc.stream_scheduling_module = inp->sctp_ep.sctp_default_ss_module;
1036a1cb341bSMichael Tuexen 	stcb->asoc.ss_functions = sctp_ss_functions[inp->sctp_ep.sctp_default_ss_module];
1037f7a77f6fSMichael Tuexen 
1038b54d3a6cSRandall Stewart 	/*
1039f8829a4aSRandall Stewart 	 * Now the stream parameters, here we allocate space for all streams
1040f8829a4aSRandall Stewart 	 * that we request by default.
1041f8829a4aSRandall Stewart 	 */
1042ea44232bSRandall Stewart 	asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams =
1043a1cb341bSMichael Tuexen 	    inp->sctp_ep.pre_open_stream_count;
1044f8829a4aSRandall Stewart 	SCTP_MALLOC(asoc->strmout, struct sctp_stream_out *,
1045f8829a4aSRandall Stewart 	    asoc->streamoutcnt * sizeof(struct sctp_stream_out),
1046207304d4SRandall Stewart 	    SCTP_M_STRMO);
1047f8829a4aSRandall Stewart 	if (asoc->strmout == NULL) {
1048f8829a4aSRandall Stewart 		/* big trouble no memory */
1049c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1050f8829a4aSRandall Stewart 		return (ENOMEM);
1051f8829a4aSRandall Stewart 	}
1052f8829a4aSRandall Stewart 	for (i = 0; i < asoc->streamoutcnt; i++) {
1053f8829a4aSRandall Stewart 		/*
1054f8829a4aSRandall Stewart 		 * inbound side must be set to 0xffff, also NOTE when we get
1055f8829a4aSRandall Stewart 		 * the INIT-ACK back (for INIT sender) we MUST reduce the
1056f8829a4aSRandall Stewart 		 * count (streamoutcnt) but first check if we sent to any of
1057f8829a4aSRandall Stewart 		 * the upper streams that were dropped (if some were). Those
1058f8829a4aSRandall Stewart 		 * that were dropped must be notified to the upper layer as
1059f8829a4aSRandall Stewart 		 * failed to send.
1060f8829a4aSRandall Stewart 		 */
1061f3b05218SMichael Tuexen 		asoc->strmout[i].next_sequence_send = 0x0;
1062f8829a4aSRandall Stewart 		TAILQ_INIT(&asoc->strmout[i].outqueue);
1063325c8c46SMichael Tuexen 		asoc->strmout[i].chunks_on_queues = 0;
1064f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
1065f0396ad1SMichael Tuexen 		for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1066f0396ad1SMichael Tuexen 			asoc->strmout[i].abandoned_sent[j] = 0;
1067f0396ad1SMichael Tuexen 			asoc->strmout[i].abandoned_unsent[j] = 0;
1068f0396ad1SMichael Tuexen 		}
1069f0396ad1SMichael Tuexen #else
1070f0396ad1SMichael Tuexen 		asoc->strmout[i].abandoned_sent[0] = 0;
1071f0396ad1SMichael Tuexen 		asoc->strmout[i].abandoned_unsent[0] = 0;
1072f0396ad1SMichael Tuexen #endif
1073f8829a4aSRandall Stewart 		asoc->strmout[i].stream_no = i;
1074f8829a4aSRandall Stewart 		asoc->strmout[i].last_msg_incomplete = 0;
1075252f7f93SMichael Tuexen 		asoc->ss_functions.sctp_ss_init_stream(&asoc->strmout[i], NULL);
1076f8829a4aSRandall Stewart 	}
1077f7a77f6fSMichael Tuexen 	asoc->ss_functions.sctp_ss_init(stcb, asoc, 0);
1078f7a77f6fSMichael Tuexen 
1079f8829a4aSRandall Stewart 	/* Now the mapping array */
1080f8829a4aSRandall Stewart 	asoc->mapping_array_size = SCTP_INITIAL_MAPPING_ARRAY;
1081f8829a4aSRandall Stewart 	SCTP_MALLOC(asoc->mapping_array, uint8_t *, asoc->mapping_array_size,
1082207304d4SRandall Stewart 	    SCTP_M_MAP);
1083f8829a4aSRandall Stewart 	if (asoc->mapping_array == NULL) {
1084207304d4SRandall Stewart 		SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1085c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1086f8829a4aSRandall Stewart 		return (ENOMEM);
1087f8829a4aSRandall Stewart 	}
1088f8829a4aSRandall Stewart 	memset(asoc->mapping_array, 0, asoc->mapping_array_size);
1089b5c16493SMichael Tuexen 	SCTP_MALLOC(asoc->nr_mapping_array, uint8_t *, asoc->mapping_array_size,
1090830d754dSRandall Stewart 	    SCTP_M_MAP);
1091bf1be571SRandall Stewart 	if (asoc->nr_mapping_array == NULL) {
1092bf1be571SRandall Stewart 		SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1093bf1be571SRandall Stewart 		SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1094bf1be571SRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1095bf1be571SRandall Stewart 		return (ENOMEM);
1096bf1be571SRandall Stewart 	}
1097b5c16493SMichael Tuexen 	memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
1098830d754dSRandall Stewart 
1099f8829a4aSRandall Stewart 	/* Now the init of the other outqueues */
1100f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->free_chunks);
1101f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->control_send_queue);
1102c54a18d2SRandall Stewart 	TAILQ_INIT(&asoc->asconf_send_queue);
1103f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->send_queue);
1104f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->sent_queue);
1105f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->reasmqueue);
1106f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->resetHead);
1107a1cb341bSMichael Tuexen 	asoc->max_inbound_streams = inp->sctp_ep.max_open_streams_intome;
1108f8829a4aSRandall Stewart 	TAILQ_INIT(&asoc->asconf_queue);
1109f8829a4aSRandall Stewart 	/* authentication fields */
1110f8829a4aSRandall Stewart 	asoc->authinfo.random = NULL;
1111830d754dSRandall Stewart 	asoc->authinfo.active_keyid = 0;
1112f8829a4aSRandall Stewart 	asoc->authinfo.assoc_key = NULL;
1113f8829a4aSRandall Stewart 	asoc->authinfo.assoc_keyid = 0;
1114f8829a4aSRandall Stewart 	asoc->authinfo.recv_key = NULL;
1115f8829a4aSRandall Stewart 	asoc->authinfo.recv_keyid = 0;
1116f8829a4aSRandall Stewart 	LIST_INIT(&asoc->shared_keys);
1117f42a358aSRandall Stewart 	asoc->marked_retrans = 0;
1118a1cb341bSMichael Tuexen 	asoc->port = inp->sctp_ep.port;
1119f42a358aSRandall Stewart 	asoc->timoinit = 0;
1120f42a358aSRandall Stewart 	asoc->timodata = 0;
1121f42a358aSRandall Stewart 	asoc->timosack = 0;
1122f42a358aSRandall Stewart 	asoc->timoshutdown = 0;
1123f42a358aSRandall Stewart 	asoc->timoheartbeat = 0;
1124f42a358aSRandall Stewart 	asoc->timocookie = 0;
1125f42a358aSRandall Stewart 	asoc->timoshutdownack = 0;
11266e55db54SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&asoc->start_time);
11276e55db54SRandall Stewart 	asoc->discontinuity_time = asoc->start_time;
1128f0396ad1SMichael Tuexen 	for (i = 0; i < SCTP_PR_SCTP_MAX + 1; i++) {
1129f0396ad1SMichael Tuexen 		asoc->abandoned_unsent[i] = 0;
1130f0396ad1SMichael Tuexen 		asoc->abandoned_sent[i] = 0;
1131f0396ad1SMichael Tuexen 	}
1132eacc51c5SRandall Stewart 	/*
1133eacc51c5SRandall Stewart 	 * sa_ignore MEMLEAK {memory is put in the assoc mapping array and
113477acdc25SRandall Stewart 	 * freed later when the association is freed.
1135eacc51c5SRandall Stewart 	 */
1136f8829a4aSRandall Stewart 	return (0);
1137f8829a4aSRandall Stewart }
1138f8829a4aSRandall Stewart 
11390e13104dSRandall Stewart void
11400e13104dSRandall Stewart sctp_print_mapping_array(struct sctp_association *asoc)
11410e13104dSRandall Stewart {
1142aed5947cSMichael Tuexen 	unsigned int i, limit;
11430e13104dSRandall Stewart 
1144cd3fd531SMichael Tuexen 	SCTP_PRINTF("Mapping array size: %d, baseTSN: %8.8x, cumAck: %8.8x, highestTSN: (%8.8x, %8.8x).\n",
11450e13104dSRandall Stewart 	    asoc->mapping_array_size,
11460e13104dSRandall Stewart 	    asoc->mapping_array_base_tsn,
11470e13104dSRandall Stewart 	    asoc->cumulative_tsn,
1148aed5947cSMichael Tuexen 	    asoc->highest_tsn_inside_map,
1149aed5947cSMichael Tuexen 	    asoc->highest_tsn_inside_nr_map);
1150aed5947cSMichael Tuexen 	for (limit = asoc->mapping_array_size; limit > 1; limit--) {
115160990c0cSMichael Tuexen 		if (asoc->mapping_array[limit - 1] != 0) {
115277acdc25SRandall Stewart 			break;
115377acdc25SRandall Stewart 		}
115477acdc25SRandall Stewart 	}
1155cd3fd531SMichael Tuexen 	SCTP_PRINTF("Renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
115677acdc25SRandall Stewart 	for (i = 0; i < limit; i++) {
1157cd3fd531SMichael Tuexen 		SCTP_PRINTF("%2.2x%c", asoc->mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
115877acdc25SRandall Stewart 	}
1159aed5947cSMichael Tuexen 	if (limit % 16)
1160cd3fd531SMichael Tuexen 		SCTP_PRINTF("\n");
1161aed5947cSMichael Tuexen 	for (limit = asoc->mapping_array_size; limit > 1; limit--) {
1162aed5947cSMichael Tuexen 		if (asoc->nr_mapping_array[limit - 1]) {
116377acdc25SRandall Stewart 			break;
116477acdc25SRandall Stewart 		}
116577acdc25SRandall Stewart 	}
1166cd3fd531SMichael Tuexen 	SCTP_PRINTF("Non renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
116777acdc25SRandall Stewart 	for (i = 0; i < limit; i++) {
1168cd3fd531SMichael Tuexen 		SCTP_PRINTF("%2.2x%c", asoc->nr_mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
11690e13104dSRandall Stewart 	}
1170aed5947cSMichael Tuexen 	if (limit % 16)
1171cd3fd531SMichael Tuexen 		SCTP_PRINTF("\n");
11720e13104dSRandall Stewart }
11730e13104dSRandall Stewart 
1174f8829a4aSRandall Stewart int
11750696e120SRandall Stewart sctp_expand_mapping_array(struct sctp_association *asoc, uint32_t needed)
1176f8829a4aSRandall Stewart {
1177f8829a4aSRandall Stewart 	/* mapping array needs to grow */
1178b5c16493SMichael Tuexen 	uint8_t *new_array1, *new_array2;
11790696e120SRandall Stewart 	uint32_t new_size;
1180f8829a4aSRandall Stewart 
11810696e120SRandall Stewart 	new_size = asoc->mapping_array_size + ((needed + 7) / 8 + SCTP_MAPPING_ARRAY_INCR);
1182b5c16493SMichael Tuexen 	SCTP_MALLOC(new_array1, uint8_t *, new_size, SCTP_M_MAP);
1183b5c16493SMichael Tuexen 	SCTP_MALLOC(new_array2, uint8_t *, new_size, SCTP_M_MAP);
1184b5c16493SMichael Tuexen 	if ((new_array1 == NULL) || (new_array2 == NULL)) {
1185f8829a4aSRandall Stewart 		/* can't get more, forget it */
1186b5c16493SMichael Tuexen 		SCTP_PRINTF("No memory for expansion of SCTP mapping array %d\n", new_size);
1187b5c16493SMichael Tuexen 		if (new_array1) {
1188b5c16493SMichael Tuexen 			SCTP_FREE(new_array1, SCTP_M_MAP);
1189b5c16493SMichael Tuexen 		}
1190b5c16493SMichael Tuexen 		if (new_array2) {
1191b5c16493SMichael Tuexen 			SCTP_FREE(new_array2, SCTP_M_MAP);
1192b5c16493SMichael Tuexen 		}
1193f8829a4aSRandall Stewart 		return (-1);
1194f8829a4aSRandall Stewart 	}
1195b5c16493SMichael Tuexen 	memset(new_array1, 0, new_size);
1196b5c16493SMichael Tuexen 	memset(new_array2, 0, new_size);
1197b5c16493SMichael Tuexen 	memcpy(new_array1, asoc->mapping_array, asoc->mapping_array_size);
1198b5c16493SMichael Tuexen 	memcpy(new_array2, asoc->nr_mapping_array, asoc->mapping_array_size);
1199207304d4SRandall Stewart 	SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1200830d754dSRandall Stewart 	SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
1201b5c16493SMichael Tuexen 	asoc->mapping_array = new_array1;
1202b5c16493SMichael Tuexen 	asoc->nr_mapping_array = new_array2;
1203b5c16493SMichael Tuexen 	asoc->mapping_array_size = new_size;
1204830d754dSRandall Stewart 	return (0);
1205830d754dSRandall Stewart }
1206830d754dSRandall Stewart 
12078933fa13SRandall Stewart 
120842551e99SRandall Stewart static void
120942551e99SRandall Stewart sctp_iterator_work(struct sctp_iterator *it)
121042551e99SRandall Stewart {
121142551e99SRandall Stewart 	int iteration_count = 0;
121242551e99SRandall Stewart 	int inp_skip = 0;
1213ec4c19fcSRandall Stewart 	int first_in = 1;
1214ec4c19fcSRandall Stewart 	struct sctp_inpcb *tinp;
121542551e99SRandall Stewart 
1216ec4c19fcSRandall Stewart 	SCTP_INP_INFO_RLOCK();
121742551e99SRandall Stewart 	SCTP_ITERATOR_LOCK();
1218ad81507eSRandall Stewart 	if (it->inp) {
1219ec4c19fcSRandall Stewart 		SCTP_INP_RLOCK(it->inp);
122042551e99SRandall Stewart 		SCTP_INP_DECR_REF(it->inp);
1221ad81507eSRandall Stewart 	}
122242551e99SRandall Stewart 	if (it->inp == NULL) {
122342551e99SRandall Stewart 		/* iterator is complete */
122442551e99SRandall Stewart done_with_iterator:
122542551e99SRandall Stewart 		SCTP_ITERATOR_UNLOCK();
1226ec4c19fcSRandall Stewart 		SCTP_INP_INFO_RUNLOCK();
122742551e99SRandall Stewart 		if (it->function_atend != NULL) {
122842551e99SRandall Stewart 			(*it->function_atend) (it->pointer, it->val);
122942551e99SRandall Stewart 		}
1230207304d4SRandall Stewart 		SCTP_FREE(it, SCTP_M_ITER);
123142551e99SRandall Stewart 		return;
123242551e99SRandall Stewart 	}
123342551e99SRandall Stewart select_a_new_ep:
1234ec4c19fcSRandall Stewart 	if (first_in) {
1235ec4c19fcSRandall Stewart 		first_in = 0;
1236ec4c19fcSRandall Stewart 	} else {
1237f7517433SRandall Stewart 		SCTP_INP_RLOCK(it->inp);
1238ec4c19fcSRandall Stewart 	}
123942551e99SRandall Stewart 	while (((it->pcb_flags) &&
124042551e99SRandall Stewart 	    ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) ||
124142551e99SRandall Stewart 	    ((it->pcb_features) &&
124242551e99SRandall Stewart 	    ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) {
124342551e99SRandall Stewart 		/* endpoint flags or features don't match, so keep looking */
124442551e99SRandall Stewart 		if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1245f7517433SRandall Stewart 			SCTP_INP_RUNLOCK(it->inp);
124642551e99SRandall Stewart 			goto done_with_iterator;
124742551e99SRandall Stewart 		}
1248ec4c19fcSRandall Stewart 		tinp = it->inp;
124942551e99SRandall Stewart 		it->inp = LIST_NEXT(it->inp, sctp_list);
1250ec4c19fcSRandall Stewart 		SCTP_INP_RUNLOCK(tinp);
125142551e99SRandall Stewart 		if (it->inp == NULL) {
125242551e99SRandall Stewart 			goto done_with_iterator;
125342551e99SRandall Stewart 		}
125442551e99SRandall Stewart 		SCTP_INP_RLOCK(it->inp);
1255f7517433SRandall Stewart 	}
125642551e99SRandall Stewart 	/* now go through each assoc which is in the desired state */
125742551e99SRandall Stewart 	if (it->done_current_ep == 0) {
125842551e99SRandall Stewart 		if (it->function_inp != NULL)
125942551e99SRandall Stewart 			inp_skip = (*it->function_inp) (it->inp, it->pointer, it->val);
126042551e99SRandall Stewart 		it->done_current_ep = 1;
126142551e99SRandall Stewart 	}
126242551e99SRandall Stewart 	if (it->stcb == NULL) {
126342551e99SRandall Stewart 		/* run the per instance function */
126442551e99SRandall Stewart 		it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
126542551e99SRandall Stewart 	}
126642551e99SRandall Stewart 	if ((inp_skip) || it->stcb == NULL) {
126742551e99SRandall Stewart 		if (it->function_inp_end != NULL) {
126842551e99SRandall Stewart 			inp_skip = (*it->function_inp_end) (it->inp,
126942551e99SRandall Stewart 			    it->pointer,
127042551e99SRandall Stewart 			    it->val);
127142551e99SRandall Stewart 		}
127242551e99SRandall Stewart 		SCTP_INP_RUNLOCK(it->inp);
127342551e99SRandall Stewart 		goto no_stcb;
127442551e99SRandall Stewart 	}
127542551e99SRandall Stewart 	while (it->stcb) {
127642551e99SRandall Stewart 		SCTP_TCB_LOCK(it->stcb);
127742551e99SRandall Stewart 		if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
127842551e99SRandall Stewart 			/* not in the right state... keep looking */
127942551e99SRandall Stewart 			SCTP_TCB_UNLOCK(it->stcb);
128042551e99SRandall Stewart 			goto next_assoc;
128142551e99SRandall Stewart 		}
128242551e99SRandall Stewart 		/* see if we have limited out the iterator loop */
128342551e99SRandall Stewart 		iteration_count++;
128442551e99SRandall Stewart 		if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) {
128542551e99SRandall Stewart 			/* Pause to let others grab the lock */
128642551e99SRandall Stewart 			atomic_add_int(&it->stcb->asoc.refcnt, 1);
128742551e99SRandall Stewart 			SCTP_TCB_UNLOCK(it->stcb);
1288c4739e2fSRandall Stewart 			SCTP_INP_INCR_REF(it->inp);
128942551e99SRandall Stewart 			SCTP_INP_RUNLOCK(it->inp);
129042551e99SRandall Stewart 			SCTP_ITERATOR_UNLOCK();
1291ec4c19fcSRandall Stewart 			SCTP_INP_INFO_RUNLOCK();
1292ec4c19fcSRandall Stewart 			SCTP_INP_INFO_RLOCK();
129342551e99SRandall Stewart 			SCTP_ITERATOR_LOCK();
1294f7517433SRandall Stewart 			if (sctp_it_ctl.iterator_flags) {
1295f7517433SRandall Stewart 				/* We won't be staying here */
1296f7517433SRandall Stewart 				SCTP_INP_DECR_REF(it->inp);
1297f7517433SRandall Stewart 				atomic_add_int(&it->stcb->asoc.refcnt, -1);
1298f7517433SRandall Stewart 				if (sctp_it_ctl.iterator_flags &
1299f7517433SRandall Stewart 				    SCTP_ITERATOR_STOP_CUR_IT) {
1300f7517433SRandall Stewart 					sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_IT;
1301f7517433SRandall Stewart 					goto done_with_iterator;
1302f7517433SRandall Stewart 				}
1303f7517433SRandall Stewart 				if (sctp_it_ctl.iterator_flags &
1304f7517433SRandall Stewart 				    SCTP_ITERATOR_STOP_CUR_INP) {
1305f7517433SRandall Stewart 					sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_INP;
1306f7517433SRandall Stewart 					goto no_stcb;
1307f7517433SRandall Stewart 				}
1308f7517433SRandall Stewart 				/* If we reach here huh? */
1309cd3fd531SMichael Tuexen 				SCTP_PRINTF("Unknown it ctl flag %x\n",
1310f7517433SRandall Stewart 				    sctp_it_ctl.iterator_flags);
1311f7517433SRandall Stewart 				sctp_it_ctl.iterator_flags = 0;
1312f7517433SRandall Stewart 			}
131342551e99SRandall Stewart 			SCTP_INP_RLOCK(it->inp);
1314c4739e2fSRandall Stewart 			SCTP_INP_DECR_REF(it->inp);
131542551e99SRandall Stewart 			SCTP_TCB_LOCK(it->stcb);
131642551e99SRandall Stewart 			atomic_add_int(&it->stcb->asoc.refcnt, -1);
131742551e99SRandall Stewart 			iteration_count = 0;
131842551e99SRandall Stewart 		}
131942551e99SRandall Stewart 		/* run function on this one */
132042551e99SRandall Stewart 		(*it->function_assoc) (it->inp, it->stcb, it->pointer, it->val);
132142551e99SRandall Stewart 
132242551e99SRandall Stewart 		/*
132342551e99SRandall Stewart 		 * we lie here, it really needs to have its own type but
132442551e99SRandall Stewart 		 * first I must verify that this won't effect things :-0
132542551e99SRandall Stewart 		 */
132642551e99SRandall Stewart 		if (it->no_chunk_output == 0)
1327ceaad40aSRandall Stewart 			sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
132842551e99SRandall Stewart 
132942551e99SRandall Stewart 		SCTP_TCB_UNLOCK(it->stcb);
133042551e99SRandall Stewart next_assoc:
133142551e99SRandall Stewart 		it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
133242551e99SRandall Stewart 		if (it->stcb == NULL) {
133342551e99SRandall Stewart 			/* Run last function */
133442551e99SRandall Stewart 			if (it->function_inp_end != NULL) {
133542551e99SRandall Stewart 				inp_skip = (*it->function_inp_end) (it->inp,
133642551e99SRandall Stewart 				    it->pointer,
133742551e99SRandall Stewart 				    it->val);
133842551e99SRandall Stewart 			}
133942551e99SRandall Stewart 		}
134042551e99SRandall Stewart 	}
134142551e99SRandall Stewart 	SCTP_INP_RUNLOCK(it->inp);
134242551e99SRandall Stewart no_stcb:
134342551e99SRandall Stewart 	/* done with all assocs on this endpoint, move on to next endpoint */
134442551e99SRandall Stewart 	it->done_current_ep = 0;
134542551e99SRandall Stewart 	if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
134642551e99SRandall Stewart 		it->inp = NULL;
134742551e99SRandall Stewart 	} else {
134842551e99SRandall Stewart 		it->inp = LIST_NEXT(it->inp, sctp_list);
134942551e99SRandall Stewart 	}
135042551e99SRandall Stewart 	if (it->inp == NULL) {
135142551e99SRandall Stewart 		goto done_with_iterator;
135242551e99SRandall Stewart 	}
135342551e99SRandall Stewart 	goto select_a_new_ep;
135442551e99SRandall Stewart }
135542551e99SRandall Stewart 
135642551e99SRandall Stewart void
135742551e99SRandall Stewart sctp_iterator_worker(void)
135842551e99SRandall Stewart {
13594a9ef3f8SMichael Tuexen 	struct sctp_iterator *it, *nit;
136042551e99SRandall Stewart 
136142551e99SRandall Stewart 	/* This function is called with the WQ lock in place */
136242551e99SRandall Stewart 
1363f7517433SRandall Stewart 	sctp_it_ctl.iterator_running = 1;
13644a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
13654a9ef3f8SMichael Tuexen 		sctp_it_ctl.cur_it = it;
136642551e99SRandall Stewart 		/* now lets work on this one */
1367f7517433SRandall Stewart 		TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
136842551e99SRandall Stewart 		SCTP_IPI_ITERATOR_WQ_UNLOCK();
1369f7517433SRandall Stewart 		CURVNET_SET(it->vn);
137042551e99SRandall Stewart 		sctp_iterator_work(it);
1371f79aab18SRandall Stewart 		sctp_it_ctl.cur_it = NULL;
1372f7517433SRandall Stewart 		CURVNET_RESTORE();
137342551e99SRandall Stewart 		SCTP_IPI_ITERATOR_WQ_LOCK();
13743c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
137542551e99SRandall Stewart 	}
1376f7517433SRandall Stewart 	sctp_it_ctl.iterator_running = 0;
137742551e99SRandall Stewart 	return;
137842551e99SRandall Stewart }
137942551e99SRandall Stewart 
1380f8829a4aSRandall Stewart 
1381f8829a4aSRandall Stewart static void
1382f8829a4aSRandall Stewart sctp_handle_addr_wq(void)
1383f8829a4aSRandall Stewart {
1384f8829a4aSRandall Stewart 	/* deal with the ADDR wq from the rtsock calls */
13854a9ef3f8SMichael Tuexen 	struct sctp_laddr *wi, *nwi;
138642551e99SRandall Stewart 	struct sctp_asconf_iterator *asc;
1387f8829a4aSRandall Stewart 
138842551e99SRandall Stewart 	SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
1389207304d4SRandall Stewart 	    sizeof(struct sctp_asconf_iterator), SCTP_M_ASC_IT);
139042551e99SRandall Stewart 	if (asc == NULL) {
139142551e99SRandall Stewart 		/* Try later, no memory */
1392f8829a4aSRandall Stewart 		sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
1393f8829a4aSRandall Stewart 		    (struct sctp_inpcb *)NULL,
1394f8829a4aSRandall Stewart 		    (struct sctp_tcb *)NULL,
1395f8829a4aSRandall Stewart 		    (struct sctp_nets *)NULL);
139642551e99SRandall Stewart 		return;
1397f8829a4aSRandall Stewart 	}
139842551e99SRandall Stewart 	LIST_INIT(&asc->list_of_work);
139942551e99SRandall Stewart 	asc->cnt = 0;
1400f7517433SRandall Stewart 
1401f7517433SRandall Stewart 	SCTP_WQ_ADDR_LOCK();
14024a9ef3f8SMichael Tuexen 	LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
140342551e99SRandall Stewart 		LIST_REMOVE(wi, sctp_nxt_addr);
140442551e99SRandall Stewart 		LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
140542551e99SRandall Stewart 		asc->cnt++;
1406f8829a4aSRandall Stewart 	}
1407f7517433SRandall Stewart 	SCTP_WQ_ADDR_UNLOCK();
1408f7517433SRandall Stewart 
140942551e99SRandall Stewart 	if (asc->cnt == 0) {
1410207304d4SRandall Stewart 		SCTP_FREE(asc, SCTP_M_ASC_IT);
141142551e99SRandall Stewart 	} else {
14121b649582SRandall Stewart 		(void)sctp_initiate_iterator(sctp_asconf_iterator_ep,
14131b649582SRandall Stewart 		    sctp_asconf_iterator_stcb,
141442551e99SRandall Stewart 		    NULL,	/* No ep end for boundall */
141542551e99SRandall Stewart 		    SCTP_PCB_FLAGS_BOUNDALL,
141642551e99SRandall Stewart 		    SCTP_PCB_ANY_FEATURES,
14171b649582SRandall Stewart 		    SCTP_ASOC_ANY_STATE,
14181b649582SRandall Stewart 		    (void *)asc, 0,
14191b649582SRandall Stewart 		    sctp_asconf_iterator_end, NULL, 0);
142042551e99SRandall Stewart 	}
1421f8829a4aSRandall Stewart }
1422f8829a4aSRandall Stewart 
1423f8829a4aSRandall Stewart void
1424f8829a4aSRandall Stewart sctp_timeout_handler(void *t)
1425f8829a4aSRandall Stewart {
1426f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
1427f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
1428f8829a4aSRandall Stewart 	struct sctp_nets *net;
1429f8829a4aSRandall Stewart 	struct sctp_timer *tmr;
1430ceaad40aSRandall Stewart 
1431ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1432ceaad40aSRandall Stewart 	struct socket *so;
1433ceaad40aSRandall Stewart 
1434ceaad40aSRandall Stewart #endif
1435d61374e1SRandall Stewart 	int did_output, type;
1436f8829a4aSRandall Stewart 
1437f8829a4aSRandall Stewart 	tmr = (struct sctp_timer *)t;
1438f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)tmr->ep;
1439f8829a4aSRandall Stewart 	stcb = (struct sctp_tcb *)tmr->tcb;
1440f8829a4aSRandall Stewart 	net = (struct sctp_nets *)tmr->net;
14418518270eSMichael Tuexen 	CURVNET_SET((struct vnet *)tmr->vnet);
1442f8829a4aSRandall Stewart 	did_output = 1;
1443f8829a4aSRandall Stewart 
1444f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1445f8829a4aSRandall Stewart 	sctp_audit_log(0xF0, (uint8_t) tmr->type);
1446f8829a4aSRandall Stewart 	sctp_auditing(3, inp, stcb, net);
1447f8829a4aSRandall Stewart #endif
1448f8829a4aSRandall Stewart 
1449f8829a4aSRandall Stewart 	/* sanity checks... */
1450f8829a4aSRandall Stewart 	if (tmr->self != (void *)tmr) {
1451f8829a4aSRandall Stewart 		/*
1452ad81507eSRandall Stewart 		 * SCTP_PRINTF("Stale SCTP timer fired (%p), ignoring...\n",
1453dd294dceSMichael Tuexen 		 * (void *)tmr);
1454f8829a4aSRandall Stewart 		 */
14558518270eSMichael Tuexen 		CURVNET_RESTORE();
1456f8829a4aSRandall Stewart 		return;
1457f8829a4aSRandall Stewart 	}
1458a5d547adSRandall Stewart 	tmr->stopped_from = 0xa001;
1459f8829a4aSRandall Stewart 	if (!SCTP_IS_TIMER_TYPE_VALID(tmr->type)) {
1460f8829a4aSRandall Stewart 		/*
1461ad81507eSRandall Stewart 		 * SCTP_PRINTF("SCTP timer fired with invalid type: 0x%x\n",
1462f8829a4aSRandall Stewart 		 * tmr->type);
1463f8829a4aSRandall Stewart 		 */
14648518270eSMichael Tuexen 		CURVNET_RESTORE();
1465f8829a4aSRandall Stewart 		return;
1466f8829a4aSRandall Stewart 	}
1467a5d547adSRandall Stewart 	tmr->stopped_from = 0xa002;
1468f8829a4aSRandall Stewart 	if ((tmr->type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL)) {
14698518270eSMichael Tuexen 		CURVNET_RESTORE();
1470f8829a4aSRandall Stewart 		return;
1471f8829a4aSRandall Stewart 	}
1472f8829a4aSRandall Stewart 	/* if this is an iterator timeout, get the struct and clear inp */
1473a5d547adSRandall Stewart 	tmr->stopped_from = 0xa003;
1474d61374e1SRandall Stewart 	type = tmr->type;
1475f8829a4aSRandall Stewart 	if (inp) {
1476f8829a4aSRandall Stewart 		SCTP_INP_INCR_REF(inp);
1477aa1808b7SMichael Tuexen 		if ((inp->sctp_socket == NULL) &&
1478f8829a4aSRandall Stewart 		    ((tmr->type != SCTP_TIMER_TYPE_INPKILL) &&
1479810ec536SMichael Tuexen 		    (tmr->type != SCTP_TIMER_TYPE_INIT) &&
1480a1e13272SRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_SEND) &&
1481a1e13272SRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_RECV) &&
1482a1e13272SRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_HEARTBEAT) &&
1483f8829a4aSRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_SHUTDOWN) &&
1484f8829a4aSRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNACK) &&
1485f8829a4aSRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNGUARD) &&
1486f8829a4aSRandall Stewart 		    (tmr->type != SCTP_TIMER_TYPE_ASOCKILL))
1487f8829a4aSRandall Stewart 		    ) {
1488f8829a4aSRandall Stewart 			SCTP_INP_DECR_REF(inp);
14898518270eSMichael Tuexen 			CURVNET_RESTORE();
1490f8829a4aSRandall Stewart 			return;
1491f8829a4aSRandall Stewart 		}
1492f8829a4aSRandall Stewart 	}
1493a5d547adSRandall Stewart 	tmr->stopped_from = 0xa004;
1494f8829a4aSRandall Stewart 	if (stcb) {
1495c105859eSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
1496f8829a4aSRandall Stewart 		if (stcb->asoc.state == 0) {
1497c105859eSRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, -1);
1498f8829a4aSRandall Stewart 			if (inp) {
1499f8829a4aSRandall Stewart 				SCTP_INP_DECR_REF(inp);
1500f8829a4aSRandall Stewart 			}
15018518270eSMichael Tuexen 			CURVNET_RESTORE();
1502f8829a4aSRandall Stewart 			return;
1503f8829a4aSRandall Stewart 		}
1504f8829a4aSRandall Stewart 	}
1505a5d547adSRandall Stewart 	tmr->stopped_from = 0xa005;
1506ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_TIMER1, "Timer type %d goes off\n", tmr->type);
1507139bc87fSRandall Stewart 	if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1508f8829a4aSRandall Stewart 		if (inp) {
1509f8829a4aSRandall Stewart 			SCTP_INP_DECR_REF(inp);
1510f8829a4aSRandall Stewart 		}
1511207304d4SRandall Stewart 		if (stcb) {
1512207304d4SRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, -1);
1513207304d4SRandall Stewart 		}
15148518270eSMichael Tuexen 		CURVNET_RESTORE();
1515f8829a4aSRandall Stewart 		return;
1516f8829a4aSRandall Stewart 	}
1517a5d547adSRandall Stewart 	tmr->stopped_from = 0xa006;
1518a5d547adSRandall Stewart 
1519f8829a4aSRandall Stewart 	if (stcb) {
1520f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
152150cec919SRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, -1);
1522b54d3a6cSRandall Stewart 		if ((tmr->type != SCTP_TIMER_TYPE_ASOCKILL) &&
1523b54d3a6cSRandall Stewart 		    ((stcb->asoc.state == 0) ||
1524b54d3a6cSRandall Stewart 		    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED))) {
1525b54d3a6cSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
1526b54d3a6cSRandall Stewart 			if (inp) {
1527b54d3a6cSRandall Stewart 				SCTP_INP_DECR_REF(inp);
1528b54d3a6cSRandall Stewart 			}
15298518270eSMichael Tuexen 			CURVNET_RESTORE();
1530b54d3a6cSRandall Stewart 			return;
1531b54d3a6cSRandall Stewart 		}
1532f8829a4aSRandall Stewart 	}
153344b7479bSRandall Stewart 	/* record in stopped what t-o occured */
153444b7479bSRandall Stewart 	tmr->stopped_from = tmr->type;
153544b7479bSRandall Stewart 
1536f8829a4aSRandall Stewart 	/* mark as being serviced now */
153744b7479bSRandall Stewart 	if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
153844b7479bSRandall Stewart 		/*
153944b7479bSRandall Stewart 		 * Callout has been rescheduled.
154044b7479bSRandall Stewart 		 */
154144b7479bSRandall Stewart 		goto get_out;
154244b7479bSRandall Stewart 	}
154344b7479bSRandall Stewart 	if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
154444b7479bSRandall Stewart 		/*
154544b7479bSRandall Stewart 		 * Not active, so no action.
154644b7479bSRandall Stewart 		 */
154744b7479bSRandall Stewart 		goto get_out;
154844b7479bSRandall Stewart 	}
1549139bc87fSRandall Stewart 	SCTP_OS_TIMER_DEACTIVATE(&tmr->timer);
1550f8829a4aSRandall Stewart 
1551f8829a4aSRandall Stewart 	/* call the handler for the appropriate timer type */
1552f8829a4aSRandall Stewart 	switch (tmr->type) {
1553d61a0ae0SRandall Stewart 	case SCTP_TIMER_TYPE_ZERO_COPY:
1554eacc51c5SRandall Stewart 		if (inp == NULL) {
1555eacc51c5SRandall Stewart 			break;
1556eacc51c5SRandall Stewart 		}
1557d61a0ae0SRandall Stewart 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
1558d61a0ae0SRandall Stewart 			SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
1559d61a0ae0SRandall Stewart 		}
1560d61a0ae0SRandall Stewart 		break;
1561ad21a364SRandall Stewart 	case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
1562eacc51c5SRandall Stewart 		if (inp == NULL) {
1563eacc51c5SRandall Stewart 			break;
1564eacc51c5SRandall Stewart 		}
1565ad21a364SRandall Stewart 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
1566ad21a364SRandall Stewart 			SCTP_ZERO_COPY_SENDQ_EVENT(inp, inp->sctp_socket);
1567ad21a364SRandall Stewart 		}
1568ad21a364SRandall Stewart 		break;
1569f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ADDR_WQ:
1570f8829a4aSRandall Stewart 		sctp_handle_addr_wq();
1571f8829a4aSRandall Stewart 		break;
1572f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SEND:
1573ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1574ad81507eSRandall Stewart 			break;
1575ad81507eSRandall Stewart 		}
1576f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timodata);
1577f42a358aSRandall Stewart 		stcb->asoc.timodata++;
1578f8829a4aSRandall Stewart 		stcb->asoc.num_send_timers_up--;
1579f8829a4aSRandall Stewart 		if (stcb->asoc.num_send_timers_up < 0) {
1580f8829a4aSRandall Stewart 			stcb->asoc.num_send_timers_up = 0;
1581f8829a4aSRandall Stewart 		}
1582b54d3a6cSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
158360990c0cSMichael Tuexen 		if (sctp_t3rxt_timer(inp, stcb, net)) {
1584f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1585f8829a4aSRandall Stewart 
1586f8829a4aSRandall Stewart 			goto out_decr;
1587f8829a4aSRandall Stewart 		}
1588b54d3a6cSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1589f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1590f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1591f8829a4aSRandall Stewart #endif
1592ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1593f8829a4aSRandall Stewart 		if ((stcb->asoc.num_send_timers_up == 0) &&
15944a9ef3f8SMichael Tuexen 		    (stcb->asoc.sent_queue_cnt > 0)) {
1595f8829a4aSRandall Stewart 			struct sctp_tmit_chunk *chk;
1596f8829a4aSRandall Stewart 
1597f8829a4aSRandall Stewart 			/*
1598f8829a4aSRandall Stewart 			 * safeguard. If there on some on the sent queue
1599f8829a4aSRandall Stewart 			 * somewhere but no timers running something is
1600f8829a4aSRandall Stewart 			 * wrong... so we start a timer on the first chunk
1601f8829a4aSRandall Stewart 			 * on the send queue on whatever net it is sent to.
1602f8829a4aSRandall Stewart 			 */
1603f8829a4aSRandall Stewart 			chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
1604f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb,
1605f8829a4aSRandall Stewart 			    chk->whoTo);
1606f8829a4aSRandall Stewart 		}
1607f8829a4aSRandall Stewart 		break;
1608f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
1609ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1610ad81507eSRandall Stewart 			break;
1611ad81507eSRandall Stewart 		}
1612f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoinit);
1613f42a358aSRandall Stewart 		stcb->asoc.timoinit++;
1614f8829a4aSRandall Stewart 		if (sctp_t1init_timer(inp, stcb, net)) {
1615f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1616f8829a4aSRandall Stewart 			goto out_decr;
1617f8829a4aSRandall Stewart 		}
1618f8829a4aSRandall Stewart 		/* We do output but not here */
1619f8829a4aSRandall Stewart 		did_output = 0;
1620f8829a4aSRandall Stewart 		break;
1621f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
1622ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1623ad81507eSRandall Stewart 			break;
1624ca85e948SMichael Tuexen 		}
1625f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timosack);
1626f42a358aSRandall Stewart 		stcb->asoc.timosack++;
1627689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
1628f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1629f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1630f8829a4aSRandall Stewart #endif
1631ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SACK_TMR, SCTP_SO_NOT_LOCKED);
1632f8829a4aSRandall Stewart 		break;
1633f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
1634ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1635ad81507eSRandall Stewart 			break;
1636ad81507eSRandall Stewart 		}
1637f8829a4aSRandall Stewart 		if (sctp_shutdown_timer(inp, stcb, net)) {
1638f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1639f8829a4aSRandall Stewart 			goto out_decr;
1640f8829a4aSRandall Stewart 		}
1641f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoshutdown);
1642f42a358aSRandall Stewart 		stcb->asoc.timoshutdown++;
1643f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1644f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1645f8829a4aSRandall Stewart #endif
1646ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_NOT_LOCKED);
1647f8829a4aSRandall Stewart 		break;
1648f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
1649ca85e948SMichael Tuexen 		if ((stcb == NULL) || (inp == NULL) || (net == NULL)) {
1650ad81507eSRandall Stewart 			break;
1651ad81507eSRandall Stewart 		}
1652f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoheartbeat);
1653f42a358aSRandall Stewart 		stcb->asoc.timoheartbeat++;
1654ca85e948SMichael Tuexen 		if (sctp_heartbeat_timer(inp, stcb, net)) {
1655f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1656f8829a4aSRandall Stewart 			goto out_decr;
1657f8829a4aSRandall Stewart 		}
1658f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1659ca85e948SMichael Tuexen 		sctp_auditing(4, inp, stcb, net);
1660f8829a4aSRandall Stewart #endif
1661ca85e948SMichael Tuexen 		if (!(net->dest_state & SCTP_ADDR_NOHB)) {
1662629749b6SMichael Tuexen 			sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1663ceaad40aSRandall Stewart 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_HB_TMR, SCTP_SO_NOT_LOCKED);
1664f8829a4aSRandall Stewart 		}
1665f8829a4aSRandall Stewart 		break;
1666f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
1667ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1668ad81507eSRandall Stewart 			break;
1669ad81507eSRandall Stewart 		}
1670f8829a4aSRandall Stewart 		if (sctp_cookie_timer(inp, stcb, net)) {
1671f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1672f8829a4aSRandall Stewart 			goto out_decr;
1673f8829a4aSRandall Stewart 		}
1674f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timocookie);
1675f42a358aSRandall Stewart 		stcb->asoc.timocookie++;
1676f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1677f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1678f8829a4aSRandall Stewart #endif
1679f8829a4aSRandall Stewart 		/*
1680f8829a4aSRandall Stewart 		 * We consider T3 and Cookie timer pretty much the same with
1681f8829a4aSRandall Stewart 		 * respect to where from in chunk_output.
1682f8829a4aSRandall Stewart 		 */
1683ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1684f8829a4aSRandall Stewart 		break;
1685f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_NEWCOOKIE:
1686f8829a4aSRandall Stewart 		{
1687f8829a4aSRandall Stewart 			struct timeval tv;
1688f8829a4aSRandall Stewart 			int i, secret;
1689f8829a4aSRandall Stewart 
1690ad81507eSRandall Stewart 			if (inp == NULL) {
1691ad81507eSRandall Stewart 				break;
1692ad81507eSRandall Stewart 			}
1693f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_timosecret);
16946e55db54SRandall Stewart 			(void)SCTP_GETTIME_TIMEVAL(&tv);
1695f8829a4aSRandall Stewart 			SCTP_INP_WLOCK(inp);
1696f8829a4aSRandall Stewart 			inp->sctp_ep.time_of_secret_change = tv.tv_sec;
1697f8829a4aSRandall Stewart 			inp->sctp_ep.last_secret_number =
1698f8829a4aSRandall Stewart 			    inp->sctp_ep.current_secret_number;
1699f8829a4aSRandall Stewart 			inp->sctp_ep.current_secret_number++;
1700f8829a4aSRandall Stewart 			if (inp->sctp_ep.current_secret_number >=
1701f8829a4aSRandall Stewart 			    SCTP_HOW_MANY_SECRETS) {
1702f8829a4aSRandall Stewart 				inp->sctp_ep.current_secret_number = 0;
1703f8829a4aSRandall Stewart 			}
1704f8829a4aSRandall Stewart 			secret = (int)inp->sctp_ep.current_secret_number;
1705f8829a4aSRandall Stewart 			for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1706f8829a4aSRandall Stewart 				inp->sctp_ep.secret_key[secret][i] =
1707f8829a4aSRandall Stewart 				    sctp_select_initial_TSN(&inp->sctp_ep);
1708f8829a4aSRandall Stewart 			}
1709f8829a4aSRandall Stewart 			SCTP_INP_WUNLOCK(inp);
1710f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, stcb, net);
1711f8829a4aSRandall Stewart 		}
1712f8829a4aSRandall Stewart 		did_output = 0;
1713f8829a4aSRandall Stewart 		break;
1714f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
1715ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1716ad81507eSRandall Stewart 			break;
1717ad81507eSRandall Stewart 		}
1718f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timopathmtu);
1719f8829a4aSRandall Stewart 		sctp_pathmtu_timer(inp, stcb, net);
1720f8829a4aSRandall Stewart 		did_output = 0;
1721f8829a4aSRandall Stewart 		break;
1722f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
1723ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1724ad81507eSRandall Stewart 			break;
1725ad81507eSRandall Stewart 		}
1726f8829a4aSRandall Stewart 		if (sctp_shutdownack_timer(inp, stcb, net)) {
1727f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1728f8829a4aSRandall Stewart 			goto out_decr;
1729f8829a4aSRandall Stewart 		}
1730f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoshutdownack);
1731f42a358aSRandall Stewart 		stcb->asoc.timoshutdownack++;
1732f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1733f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1734f8829a4aSRandall Stewart #endif
1735ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_ACK_TMR, SCTP_SO_NOT_LOCKED);
1736f8829a4aSRandall Stewart 		break;
1737f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
1738ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1739ad81507eSRandall Stewart 			break;
1740ad81507eSRandall Stewart 		}
1741f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoshutdownguard);
1742a2b42326SMichael Tuexen 		sctp_abort_an_association(inp, stcb, NULL, SCTP_SO_NOT_LOCKED);
1743f8829a4aSRandall Stewart 		/* no need to unlock on tcb its gone */
1744f8829a4aSRandall Stewart 		goto out_decr;
1745f8829a4aSRandall Stewart 
1746f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_STRRESET:
1747ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1748ad81507eSRandall Stewart 			break;
1749ad81507eSRandall Stewart 		}
1750f8829a4aSRandall Stewart 		if (sctp_strreset_timer(inp, stcb, net)) {
1751f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1752f8829a4aSRandall Stewart 			goto out_decr;
1753f8829a4aSRandall Stewart 		}
1754f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timostrmrst);
1755ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_TMR, SCTP_SO_NOT_LOCKED);
1756f8829a4aSRandall Stewart 		break;
1757f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASCONF:
1758ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1759ad81507eSRandall Stewart 			break;
1760ad81507eSRandall Stewart 		}
1761f8829a4aSRandall Stewart 		if (sctp_asconf_timer(inp, stcb, net)) {
1762f8829a4aSRandall Stewart 			/* no need to unlock on tcb its gone */
1763f8829a4aSRandall Stewart 			goto out_decr;
1764f8829a4aSRandall Stewart 		}
1765f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoasconf);
1766f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1767f8829a4aSRandall Stewart 		sctp_auditing(4, inp, stcb, net);
1768f8829a4aSRandall Stewart #endif
1769ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_ASCONF_TMR, SCTP_SO_NOT_LOCKED);
1770f8829a4aSRandall Stewart 		break;
1771851b7298SRandall Stewart 	case SCTP_TIMER_TYPE_PRIM_DELETED:
1772851b7298SRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1773851b7298SRandall Stewart 			break;
1774851b7298SRandall Stewart 		}
177504ee05e8SRandall Stewart 		sctp_delete_prim_timer(inp, stcb, net);
1776851b7298SRandall Stewart 		SCTP_STAT_INCR(sctps_timodelprim);
1777851b7298SRandall Stewart 		break;
1778f8829a4aSRandall Stewart 
1779f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_AUTOCLOSE:
1780ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1781ad81507eSRandall Stewart 			break;
1782ad81507eSRandall Stewart 		}
1783f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoautoclose);
1784f8829a4aSRandall Stewart 		sctp_autoclose_timer(inp, stcb, net);
1785ceaad40aSRandall Stewart 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
1786f8829a4aSRandall Stewart 		did_output = 0;
1787f8829a4aSRandall Stewart 		break;
1788f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASOCKILL:
1789ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
1790ad81507eSRandall Stewart 			break;
1791ad81507eSRandall Stewart 		}
1792f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoassockill);
1793f8829a4aSRandall Stewart 		/* Can we free it yet? */
1794f8829a4aSRandall Stewart 		SCTP_INP_DECR_REF(inp);
1795a5d547adSRandall Stewart 		sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_1);
1796ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1797ceaad40aSRandall Stewart 		so = SCTP_INP_SO(inp);
1798ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
1799ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
1800ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
1801ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
1802ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
1803ceaad40aSRandall Stewart #endif
1804c4739e2fSRandall Stewart 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_2);
1805ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1806ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
1807ceaad40aSRandall Stewart #endif
1808f8829a4aSRandall Stewart 		/*
1809f8829a4aSRandall Stewart 		 * free asoc, always unlocks (or destroy's) so prevent
1810f8829a4aSRandall Stewart 		 * duplicate unlock or unlock of a free mtx :-0
1811f8829a4aSRandall Stewart 		 */
1812f8829a4aSRandall Stewart 		stcb = NULL;
1813f8829a4aSRandall Stewart 		goto out_no_decr;
1814f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INPKILL:
1815f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_timoinpkill);
1816ad81507eSRandall Stewart 		if (inp == NULL) {
1817ad81507eSRandall Stewart 			break;
1818ad81507eSRandall Stewart 		}
1819f8829a4aSRandall Stewart 		/*
1820f8829a4aSRandall Stewart 		 * special case, take away our increment since WE are the
1821f8829a4aSRandall Stewart 		 * killer
1822f8829a4aSRandall Stewart 		 */
1823f8829a4aSRandall Stewart 		SCTP_INP_DECR_REF(inp);
1824a5d547adSRandall Stewart 		sctp_timer_stop(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_3);
1825b0552ae2SRandall Stewart 		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
18260c7dc840SRandall Stewart 		    SCTP_CALLED_FROM_INPKILL_TIMER);
1827d61374e1SRandall Stewart 		inp = NULL;
1828f8829a4aSRandall Stewart 		goto out_no_decr;
1829f8829a4aSRandall Stewart 	default:
1830ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER1, "sctp_timeout_handler:unknown timer %d\n",
1831f8829a4aSRandall Stewart 		    tmr->type);
1832f8829a4aSRandall Stewart 		break;
183360990c0cSMichael Tuexen 	}
1834f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
1835f8829a4aSRandall Stewart 	sctp_audit_log(0xF1, (uint8_t) tmr->type);
1836f8829a4aSRandall Stewart 	if (inp)
1837f8829a4aSRandall Stewart 		sctp_auditing(5, inp, stcb, net);
1838f8829a4aSRandall Stewart #endif
1839f8829a4aSRandall Stewart 	if ((did_output) && stcb) {
1840f8829a4aSRandall Stewart 		/*
1841f8829a4aSRandall Stewart 		 * Now we need to clean up the control chunk chain if an
1842f8829a4aSRandall Stewart 		 * ECNE is on it. It must be marked as UNSENT again so next
1843f8829a4aSRandall Stewart 		 * call will continue to send it until such time that we get
1844f8829a4aSRandall Stewart 		 * a CWR, to remove it. It is, however, less likely that we
1845f8829a4aSRandall Stewart 		 * will find a ecn echo on the chain though.
1846f8829a4aSRandall Stewart 		 */
1847f8829a4aSRandall Stewart 		sctp_fix_ecn_echo(&stcb->asoc);
1848f8829a4aSRandall Stewart 	}
184944b7479bSRandall Stewart get_out:
1850f8829a4aSRandall Stewart 	if (stcb) {
1851f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
1852f8829a4aSRandall Stewart 	}
1853f8829a4aSRandall Stewart out_decr:
1854f8829a4aSRandall Stewart 	if (inp) {
1855f8829a4aSRandall Stewart 		SCTP_INP_DECR_REF(inp);
1856f8829a4aSRandall Stewart 	}
1857f8829a4aSRandall Stewart out_no_decr:
1858ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_TIMER1, "Timer now complete (type %d)\n",
1859d61374e1SRandall Stewart 	    type);
18608518270eSMichael Tuexen 	CURVNET_RESTORE();
1861f8829a4aSRandall Stewart }
1862f8829a4aSRandall Stewart 
1863ad81507eSRandall Stewart void
1864f8829a4aSRandall Stewart sctp_timer_start(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1865f8829a4aSRandall Stewart     struct sctp_nets *net)
1866f8829a4aSRandall Stewart {
1867ca85e948SMichael Tuexen 	uint32_t to_ticks;
1868f8829a4aSRandall Stewart 	struct sctp_timer *tmr;
1869f8829a4aSRandall Stewart 
1870139bc87fSRandall Stewart 	if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL))
1871ad81507eSRandall Stewart 		return;
1872f8829a4aSRandall Stewart 
1873f8829a4aSRandall Stewart 	tmr = NULL;
1874f8829a4aSRandall Stewart 	if (stcb) {
1875f8829a4aSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1876f8829a4aSRandall Stewart 	}
1877f8829a4aSRandall Stewart 	switch (t_type) {
1878d61a0ae0SRandall Stewart 	case SCTP_TIMER_TYPE_ZERO_COPY:
1879d61a0ae0SRandall Stewart 		tmr = &inp->sctp_ep.zero_copy_timer;
1880d61a0ae0SRandall Stewart 		to_ticks = SCTP_ZERO_COPY_TICK_DELAY;
1881d61a0ae0SRandall Stewart 		break;
1882ad21a364SRandall Stewart 	case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
1883ad21a364SRandall Stewart 		tmr = &inp->sctp_ep.zero_copy_sendq_timer;
1884ad21a364SRandall Stewart 		to_ticks = SCTP_ZERO_COPY_SENDQ_TICK_DELAY;
1885ad21a364SRandall Stewart 		break;
1886f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ADDR_WQ:
1887f8829a4aSRandall Stewart 		/* Only 1 tick away :-) */
1888b3f1ea41SRandall Stewart 		tmr = &SCTP_BASE_INFO(addr_wq_timer);
188942551e99SRandall Stewart 		to_ticks = SCTP_ADDRESS_TICK_DELAY;
1890f8829a4aSRandall Stewart 		break;
1891f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SEND:
1892f8829a4aSRandall Stewart 		/* Here we use the RTO timer */
1893f8829a4aSRandall Stewart 		{
1894f8829a4aSRandall Stewart 			int rto_val;
1895f8829a4aSRandall Stewart 
1896f8829a4aSRandall Stewart 			if ((stcb == NULL) || (net == NULL)) {
1897ad81507eSRandall Stewart 				return;
1898f8829a4aSRandall Stewart 			}
1899f8829a4aSRandall Stewart 			tmr = &net->rxt_timer;
1900f8829a4aSRandall Stewart 			if (net->RTO == 0) {
1901f8829a4aSRandall Stewart 				rto_val = stcb->asoc.initial_rto;
1902f8829a4aSRandall Stewart 			} else {
1903f8829a4aSRandall Stewart 				rto_val = net->RTO;
1904f8829a4aSRandall Stewart 			}
1905f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(rto_val);
1906f8829a4aSRandall Stewart 		}
1907f8829a4aSRandall Stewart 		break;
1908f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
1909f8829a4aSRandall Stewart 		/*
1910f8829a4aSRandall Stewart 		 * Here we use the INIT timer default usually about 1
1911f8829a4aSRandall Stewart 		 * minute.
1912f8829a4aSRandall Stewart 		 */
1913f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
1914ad81507eSRandall Stewart 			return;
1915f8829a4aSRandall Stewart 		}
1916f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
1917f8829a4aSRandall Stewart 		if (net->RTO == 0) {
1918f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1919f8829a4aSRandall Stewart 		} else {
1920f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
1921f8829a4aSRandall Stewart 		}
1922f8829a4aSRandall Stewart 		break;
1923f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
1924f8829a4aSRandall Stewart 		/*
1925f8829a4aSRandall Stewart 		 * Here we use the Delayed-Ack timer value from the inp
1926f8829a4aSRandall Stewart 		 * ususually about 200ms.
1927f8829a4aSRandall Stewart 		 */
1928f8829a4aSRandall Stewart 		if (stcb == NULL) {
1929ad81507eSRandall Stewart 			return;
1930f8829a4aSRandall Stewart 		}
1931f8829a4aSRandall Stewart 		tmr = &stcb->asoc.dack_timer;
1932f8829a4aSRandall Stewart 		to_ticks = MSEC_TO_TICKS(stcb->asoc.delayed_ack);
1933f8829a4aSRandall Stewart 		break;
1934f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
1935f8829a4aSRandall Stewart 		/* Here we use the RTO of the destination. */
1936f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
1937ad81507eSRandall Stewart 			return;
1938f8829a4aSRandall Stewart 		}
1939f8829a4aSRandall Stewart 		if (net->RTO == 0) {
1940f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1941f8829a4aSRandall Stewart 		} else {
1942f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
1943f8829a4aSRandall Stewart 		}
1944f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
1945f8829a4aSRandall Stewart 		break;
1946f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
1947f8829a4aSRandall Stewart 		/*
1948f8829a4aSRandall Stewart 		 * the net is used here so that we can add in the RTO. Even
1949f8829a4aSRandall Stewart 		 * though we use a different timer. We also add the HB timer
1950f8829a4aSRandall Stewart 		 * PLUS a random jitter.
1951f8829a4aSRandall Stewart 		 */
1952ca85e948SMichael Tuexen 		if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
1953ad81507eSRandall Stewart 			return;
1954ad81507eSRandall Stewart 		} else {
1955f8829a4aSRandall Stewart 			uint32_t rndval;
1956ca85e948SMichael Tuexen 			uint32_t jitter;
1957f8829a4aSRandall Stewart 
1958ca85e948SMichael Tuexen 			if ((net->dest_state & SCTP_ADDR_NOHB) &&
1959ca85e948SMichael Tuexen 			    !(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
1960ad81507eSRandall Stewart 				return;
1961f8829a4aSRandall Stewart 			}
1962f8829a4aSRandall Stewart 			if (net->RTO == 0) {
1963ca85e948SMichael Tuexen 				to_ticks = stcb->asoc.initial_rto;
1964f8829a4aSRandall Stewart 			} else {
1965ca85e948SMichael Tuexen 				to_ticks = net->RTO;
1966f8829a4aSRandall Stewart 			}
1967ca85e948SMichael Tuexen 			rndval = sctp_select_initial_TSN(&inp->sctp_ep);
1968ca85e948SMichael Tuexen 			jitter = rndval % to_ticks;
1969ca85e948SMichael Tuexen 			if (jitter >= (to_ticks >> 1)) {
1970ca85e948SMichael Tuexen 				to_ticks = to_ticks + (jitter - (to_ticks >> 1));
1971f8829a4aSRandall Stewart 			} else {
1972ca85e948SMichael Tuexen 				to_ticks = to_ticks - jitter;
1973f8829a4aSRandall Stewart 			}
1974ca85e948SMichael Tuexen 			if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
1975ca85e948SMichael Tuexen 			    !(net->dest_state & SCTP_ADDR_PF)) {
1976ca85e948SMichael Tuexen 				to_ticks += net->heart_beat_delay;
1977f8829a4aSRandall Stewart 			}
1978f8829a4aSRandall Stewart 			/*
1979f8829a4aSRandall Stewart 			 * Now we must convert the to_ticks that are now in
1980f8829a4aSRandall Stewart 			 * ms to ticks.
1981f8829a4aSRandall Stewart 			 */
1982f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(to_ticks);
1983ca85e948SMichael Tuexen 			tmr = &net->hb_timer;
1984f8829a4aSRandall Stewart 		}
1985f8829a4aSRandall Stewart 		break;
1986f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
1987f8829a4aSRandall Stewart 		/*
1988f8829a4aSRandall Stewart 		 * Here we can use the RTO timer from the network since one
1989f8829a4aSRandall Stewart 		 * RTT was compelete. If a retran happened then we will be
1990f8829a4aSRandall Stewart 		 * using the RTO initial value.
1991f8829a4aSRandall Stewart 		 */
1992f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
1993ad81507eSRandall Stewart 			return;
1994f8829a4aSRandall Stewart 		}
1995f8829a4aSRandall Stewart 		if (net->RTO == 0) {
1996f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
1997f8829a4aSRandall Stewart 		} else {
1998f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
1999f8829a4aSRandall Stewart 		}
2000f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2001f8829a4aSRandall Stewart 		break;
2002f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_NEWCOOKIE:
2003f8829a4aSRandall Stewart 		/*
2004f8829a4aSRandall Stewart 		 * nothing needed but the endpoint here ususually about 60
2005f8829a4aSRandall Stewart 		 * minutes.
2006f8829a4aSRandall Stewart 		 */
2007ad81507eSRandall Stewart 		if (inp == NULL) {
2008ad81507eSRandall Stewart 			return;
2009ad81507eSRandall Stewart 		}
2010f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
2011f8829a4aSRandall Stewart 		to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_SIGNATURE];
2012f8829a4aSRandall Stewart 		break;
2013f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASOCKILL:
2014f8829a4aSRandall Stewart 		if (stcb == NULL) {
2015ad81507eSRandall Stewart 			return;
2016f8829a4aSRandall Stewart 		}
2017f8829a4aSRandall Stewart 		tmr = &stcb->asoc.strreset_timer;
2018f8829a4aSRandall Stewart 		to_ticks = MSEC_TO_TICKS(SCTP_ASOC_KILL_TIMEOUT);
2019f8829a4aSRandall Stewart 		break;
2020f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INPKILL:
2021f8829a4aSRandall Stewart 		/*
2022f8829a4aSRandall Stewart 		 * The inp is setup to die. We re-use the signature_chage
2023f8829a4aSRandall Stewart 		 * timer since that has stopped and we are in the GONE
2024f8829a4aSRandall Stewart 		 * state.
2025f8829a4aSRandall Stewart 		 */
2026ad81507eSRandall Stewart 		if (inp == NULL) {
2027ad81507eSRandall Stewart 			return;
2028ad81507eSRandall Stewart 		}
2029f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
2030f8829a4aSRandall Stewart 		to_ticks = MSEC_TO_TICKS(SCTP_INP_KILL_TIMEOUT);
2031f8829a4aSRandall Stewart 		break;
2032f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
2033f8829a4aSRandall Stewart 		/*
2034f8829a4aSRandall Stewart 		 * Here we use the value found in the EP for PMTU ususually
2035f8829a4aSRandall Stewart 		 * about 10 minutes.
2036f8829a4aSRandall Stewart 		 */
2037ad81507eSRandall Stewart 		if ((stcb == NULL) || (inp == NULL)) {
2038ad81507eSRandall Stewart 			return;
2039f8829a4aSRandall Stewart 		}
2040f8829a4aSRandall Stewart 		if (net == NULL) {
2041ad81507eSRandall Stewart 			return;
2042f8829a4aSRandall Stewart 		}
204380c79bbeSMichael Tuexen 		if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
204480c79bbeSMichael Tuexen 			return;
204580c79bbeSMichael Tuexen 		}
2046f8829a4aSRandall Stewart 		to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_PMTU];
2047f8829a4aSRandall Stewart 		tmr = &net->pmtu_timer;
2048f8829a4aSRandall Stewart 		break;
2049f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
2050f8829a4aSRandall Stewart 		/* Here we use the RTO of the destination */
2051f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
2052ad81507eSRandall Stewart 			return;
2053f8829a4aSRandall Stewart 		}
2054f8829a4aSRandall Stewart 		if (net->RTO == 0) {
2055f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2056f8829a4aSRandall Stewart 		} else {
2057f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
2058f8829a4aSRandall Stewart 		}
2059f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2060f8829a4aSRandall Stewart 		break;
2061f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2062f8829a4aSRandall Stewart 		/*
2063f8829a4aSRandall Stewart 		 * Here we use the endpoints shutdown guard timer usually
2064f8829a4aSRandall Stewart 		 * about 3 minutes.
2065f8829a4aSRandall Stewart 		 */
2066ad81507eSRandall Stewart 		if ((inp == NULL) || (stcb == NULL)) {
2067ad81507eSRandall Stewart 			return;
2068f8829a4aSRandall Stewart 		}
2069f8829a4aSRandall Stewart 		to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN];
2070f8829a4aSRandall Stewart 		tmr = &stcb->asoc.shut_guard_timer;
2071f8829a4aSRandall Stewart 		break;
2072f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_STRRESET:
2073f8829a4aSRandall Stewart 		/*
20741b649582SRandall Stewart 		 * Here the timer comes from the stcb but its value is from
20751b649582SRandall Stewart 		 * the net's RTO.
2076f8829a4aSRandall Stewart 		 */
2077f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
2078ad81507eSRandall Stewart 			return;
2079f8829a4aSRandall Stewart 		}
2080f8829a4aSRandall Stewart 		if (net->RTO == 0) {
2081f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2082f8829a4aSRandall Stewart 		} else {
2083f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
2084f8829a4aSRandall Stewart 		}
2085f8829a4aSRandall Stewart 		tmr = &stcb->asoc.strreset_timer;
2086f8829a4aSRandall Stewart 		break;
2087f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASCONF:
2088f8829a4aSRandall Stewart 		/*
20891b649582SRandall Stewart 		 * Here the timer comes from the stcb but its value is from
20901b649582SRandall Stewart 		 * the net's RTO.
2091f8829a4aSRandall Stewart 		 */
2092f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
2093ad81507eSRandall Stewart 			return;
2094f8829a4aSRandall Stewart 		}
2095f8829a4aSRandall Stewart 		if (net->RTO == 0) {
2096f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2097f8829a4aSRandall Stewart 		} else {
2098f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
2099f8829a4aSRandall Stewart 		}
2100f8829a4aSRandall Stewart 		tmr = &stcb->asoc.asconf_timer;
2101f8829a4aSRandall Stewart 		break;
2102851b7298SRandall Stewart 	case SCTP_TIMER_TYPE_PRIM_DELETED:
2103851b7298SRandall Stewart 		if ((stcb == NULL) || (net != NULL)) {
2104851b7298SRandall Stewart 			return;
2105851b7298SRandall Stewart 		}
2106851b7298SRandall Stewart 		to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
2107851b7298SRandall Stewart 		tmr = &stcb->asoc.delete_prim_timer;
2108851b7298SRandall Stewart 		break;
2109f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_AUTOCLOSE:
2110f8829a4aSRandall Stewart 		if (stcb == NULL) {
2111ad81507eSRandall Stewart 			return;
2112f8829a4aSRandall Stewart 		}
2113f8829a4aSRandall Stewart 		if (stcb->asoc.sctp_autoclose_ticks == 0) {
2114f8829a4aSRandall Stewart 			/*
2115f8829a4aSRandall Stewart 			 * Really an error since stcb is NOT set to
2116f8829a4aSRandall Stewart 			 * autoclose
2117f8829a4aSRandall Stewart 			 */
2118ad81507eSRandall Stewart 			return;
2119f8829a4aSRandall Stewart 		}
2120f8829a4aSRandall Stewart 		to_ticks = stcb->asoc.sctp_autoclose_ticks;
2121f8829a4aSRandall Stewart 		tmr = &stcb->asoc.autoclose_timer;
2122f8829a4aSRandall Stewart 		break;
2123f8829a4aSRandall Stewart 	default:
2124ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2125ad81507eSRandall Stewart 		    __FUNCTION__, t_type);
2126ad81507eSRandall Stewart 		return;
2127f8829a4aSRandall Stewart 		break;
212860990c0cSMichael Tuexen 	}
2129f8829a4aSRandall Stewart 	if ((to_ticks <= 0) || (tmr == NULL)) {
2130ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER1, "%s: %d:software error to_ticks:%d tmr:%p not set ??\n",
2131dd294dceSMichael Tuexen 		    __FUNCTION__, t_type, to_ticks, (void *)tmr);
2132ad81507eSRandall Stewart 		return;
2133f8829a4aSRandall Stewart 	}
2134139bc87fSRandall Stewart 	if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
2135f8829a4aSRandall Stewart 		/*
2136f8829a4aSRandall Stewart 		 * we do NOT allow you to have it already running. if it is
2137f8829a4aSRandall Stewart 		 * we leave the current one up unchanged
2138f8829a4aSRandall Stewart 		 */
2139ad81507eSRandall Stewart 		return;
2140f8829a4aSRandall Stewart 	}
2141f8829a4aSRandall Stewart 	/* At this point we can proceed */
2142f8829a4aSRandall Stewart 	if (t_type == SCTP_TIMER_TYPE_SEND) {
2143f8829a4aSRandall Stewart 		stcb->asoc.num_send_timers_up++;
2144f8829a4aSRandall Stewart 	}
2145a5d547adSRandall Stewart 	tmr->stopped_from = 0;
2146f8829a4aSRandall Stewart 	tmr->type = t_type;
2147f8829a4aSRandall Stewart 	tmr->ep = (void *)inp;
2148f8829a4aSRandall Stewart 	tmr->tcb = (void *)stcb;
2149f8829a4aSRandall Stewart 	tmr->net = (void *)net;
2150f8829a4aSRandall Stewart 	tmr->self = (void *)tmr;
21518518270eSMichael Tuexen 	tmr->vnet = (void *)curvnet;
2152c4739e2fSRandall Stewart 	tmr->ticks = sctp_get_tick_count();
2153ad81507eSRandall Stewart 	(void)SCTP_OS_TIMER_START(&tmr->timer, to_ticks, sctp_timeout_handler, tmr);
2154ad81507eSRandall Stewart 	return;
2155f8829a4aSRandall Stewart }
2156f8829a4aSRandall Stewart 
21576e55db54SRandall Stewart void
2158f8829a4aSRandall Stewart sctp_timer_stop(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2159a5d547adSRandall Stewart     struct sctp_nets *net, uint32_t from)
2160f8829a4aSRandall Stewart {
2161f8829a4aSRandall Stewart 	struct sctp_timer *tmr;
2162f8829a4aSRandall Stewart 
2163f8829a4aSRandall Stewart 	if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) &&
2164f8829a4aSRandall Stewart 	    (inp == NULL))
21656e55db54SRandall Stewart 		return;
2166f8829a4aSRandall Stewart 
2167f8829a4aSRandall Stewart 	tmr = NULL;
2168f8829a4aSRandall Stewart 	if (stcb) {
2169f8829a4aSRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
2170f8829a4aSRandall Stewart 	}
2171f8829a4aSRandall Stewart 	switch (t_type) {
2172d61a0ae0SRandall Stewart 	case SCTP_TIMER_TYPE_ZERO_COPY:
2173d61a0ae0SRandall Stewart 		tmr = &inp->sctp_ep.zero_copy_timer;
2174d61a0ae0SRandall Stewart 		break;
2175ad21a364SRandall Stewart 	case SCTP_TIMER_TYPE_ZCOPY_SENDQ:
2176ad21a364SRandall Stewart 		tmr = &inp->sctp_ep.zero_copy_sendq_timer;
2177ad21a364SRandall Stewart 		break;
2178f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ADDR_WQ:
2179b3f1ea41SRandall Stewart 		tmr = &SCTP_BASE_INFO(addr_wq_timer);
2180f8829a4aSRandall Stewart 		break;
2181f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SEND:
2182f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
21836e55db54SRandall Stewart 			return;
2184f8829a4aSRandall Stewart 		}
2185f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2186f8829a4aSRandall Stewart 		break;
2187f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INIT:
2188f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
21896e55db54SRandall Stewart 			return;
2190f8829a4aSRandall Stewart 		}
2191f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2192f8829a4aSRandall Stewart 		break;
2193f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_RECV:
2194f8829a4aSRandall Stewart 		if (stcb == NULL) {
21956e55db54SRandall Stewart 			return;
2196f8829a4aSRandall Stewart 		}
2197f8829a4aSRandall Stewart 		tmr = &stcb->asoc.dack_timer;
2198f8829a4aSRandall Stewart 		break;
2199f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWN:
2200f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
22016e55db54SRandall Stewart 			return;
2202f8829a4aSRandall Stewart 		}
2203f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2204f8829a4aSRandall Stewart 		break;
2205f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_HEARTBEAT:
2206ca85e948SMichael Tuexen 		if ((stcb == NULL) || (net == NULL)) {
22076e55db54SRandall Stewart 			return;
2208f8829a4aSRandall Stewart 		}
2209ca85e948SMichael Tuexen 		tmr = &net->hb_timer;
2210f8829a4aSRandall Stewart 		break;
2211f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_COOKIE:
2212f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
22136e55db54SRandall Stewart 			return;
2214f8829a4aSRandall Stewart 		}
2215f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2216f8829a4aSRandall Stewart 		break;
2217f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_NEWCOOKIE:
2218f8829a4aSRandall Stewart 		/* nothing needed but the endpoint here */
2219f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
2220f8829a4aSRandall Stewart 		/*
2221f8829a4aSRandall Stewart 		 * We re-use the newcookie timer for the INP kill timer. We
2222f8829a4aSRandall Stewart 		 * must assure that we do not kill it by accident.
2223f8829a4aSRandall Stewart 		 */
2224f8829a4aSRandall Stewart 		break;
2225f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASOCKILL:
2226f8829a4aSRandall Stewart 		/*
2227f8829a4aSRandall Stewart 		 * Stop the asoc kill timer.
2228f8829a4aSRandall Stewart 		 */
2229f8829a4aSRandall Stewart 		if (stcb == NULL) {
22306e55db54SRandall Stewart 			return;
2231f8829a4aSRandall Stewart 		}
2232f8829a4aSRandall Stewart 		tmr = &stcb->asoc.strreset_timer;
2233f8829a4aSRandall Stewart 		break;
2234f8829a4aSRandall Stewart 
2235f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_INPKILL:
2236f8829a4aSRandall Stewart 		/*
2237f8829a4aSRandall Stewart 		 * The inp is setup to die. We re-use the signature_chage
2238f8829a4aSRandall Stewart 		 * timer since that has stopped and we are in the GONE
2239f8829a4aSRandall Stewart 		 * state.
2240f8829a4aSRandall Stewart 		 */
2241f8829a4aSRandall Stewart 		tmr = &inp->sctp_ep.signature_change;
2242f8829a4aSRandall Stewart 		break;
2243f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_PATHMTURAISE:
2244f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
22456e55db54SRandall Stewart 			return;
2246f8829a4aSRandall Stewart 		}
2247f8829a4aSRandall Stewart 		tmr = &net->pmtu_timer;
2248f8829a4aSRandall Stewart 		break;
2249f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNACK:
2250f8829a4aSRandall Stewart 		if ((stcb == NULL) || (net == NULL)) {
22516e55db54SRandall Stewart 			return;
2252f8829a4aSRandall Stewart 		}
2253f8829a4aSRandall Stewart 		tmr = &net->rxt_timer;
2254f8829a4aSRandall Stewart 		break;
2255f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2256f8829a4aSRandall Stewart 		if (stcb == NULL) {
22576e55db54SRandall Stewart 			return;
2258f8829a4aSRandall Stewart 		}
2259f8829a4aSRandall Stewart 		tmr = &stcb->asoc.shut_guard_timer;
2260f8829a4aSRandall Stewart 		break;
2261f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_STRRESET:
2262f8829a4aSRandall Stewart 		if (stcb == NULL) {
22636e55db54SRandall Stewart 			return;
2264f8829a4aSRandall Stewart 		}
2265f8829a4aSRandall Stewart 		tmr = &stcb->asoc.strreset_timer;
2266f8829a4aSRandall Stewart 		break;
2267f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_ASCONF:
2268f8829a4aSRandall Stewart 		if (stcb == NULL) {
22696e55db54SRandall Stewart 			return;
2270f8829a4aSRandall Stewart 		}
2271f8829a4aSRandall Stewart 		tmr = &stcb->asoc.asconf_timer;
2272f8829a4aSRandall Stewart 		break;
2273851b7298SRandall Stewart 	case SCTP_TIMER_TYPE_PRIM_DELETED:
2274851b7298SRandall Stewart 		if (stcb == NULL) {
2275851b7298SRandall Stewart 			return;
2276851b7298SRandall Stewart 		}
2277851b7298SRandall Stewart 		tmr = &stcb->asoc.delete_prim_timer;
2278851b7298SRandall Stewart 		break;
2279f8829a4aSRandall Stewart 	case SCTP_TIMER_TYPE_AUTOCLOSE:
2280f8829a4aSRandall Stewart 		if (stcb == NULL) {
22816e55db54SRandall Stewart 			return;
2282f8829a4aSRandall Stewart 		}
2283f8829a4aSRandall Stewart 		tmr = &stcb->asoc.autoclose_timer;
2284f8829a4aSRandall Stewart 		break;
2285f8829a4aSRandall Stewart 	default:
2286ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n",
2287ad81507eSRandall Stewart 		    __FUNCTION__, t_type);
2288f8829a4aSRandall Stewart 		break;
228960990c0cSMichael Tuexen 	}
2290f8829a4aSRandall Stewart 	if (tmr == NULL) {
22916e55db54SRandall Stewart 		return;
2292f8829a4aSRandall Stewart 	}
2293f8829a4aSRandall Stewart 	if ((tmr->type != t_type) && tmr->type) {
2294f8829a4aSRandall Stewart 		/*
2295f8829a4aSRandall Stewart 		 * Ok we have a timer that is under joint use. Cookie timer
2296f8829a4aSRandall Stewart 		 * per chance with the SEND timer. We therefore are NOT
2297f8829a4aSRandall Stewart 		 * running the timer that the caller wants stopped.  So just
2298f8829a4aSRandall Stewart 		 * return.
2299f8829a4aSRandall Stewart 		 */
23006e55db54SRandall Stewart 		return;
2301f8829a4aSRandall Stewart 	}
2302ad81507eSRandall Stewart 	if ((t_type == SCTP_TIMER_TYPE_SEND) && (stcb != NULL)) {
2303f8829a4aSRandall Stewart 		stcb->asoc.num_send_timers_up--;
2304f8829a4aSRandall Stewart 		if (stcb->asoc.num_send_timers_up < 0) {
2305f8829a4aSRandall Stewart 			stcb->asoc.num_send_timers_up = 0;
2306f8829a4aSRandall Stewart 		}
2307f8829a4aSRandall Stewart 	}
2308f8829a4aSRandall Stewart 	tmr->self = NULL;
2309a5d547adSRandall Stewart 	tmr->stopped_from = from;
23106e55db54SRandall Stewart 	(void)SCTP_OS_TIMER_STOP(&tmr->timer);
23116e55db54SRandall Stewart 	return;
2312f8829a4aSRandall Stewart }
2313f8829a4aSRandall Stewart 
2314f8829a4aSRandall Stewart uint32_t
2315f8829a4aSRandall Stewart sctp_calculate_len(struct mbuf *m)
2316f8829a4aSRandall Stewart {
2317f8829a4aSRandall Stewart 	uint32_t tlen = 0;
2318f8829a4aSRandall Stewart 	struct mbuf *at;
2319f8829a4aSRandall Stewart 
2320f8829a4aSRandall Stewart 	at = m;
2321f8829a4aSRandall Stewart 	while (at) {
2322139bc87fSRandall Stewart 		tlen += SCTP_BUF_LEN(at);
2323139bc87fSRandall Stewart 		at = SCTP_BUF_NEXT(at);
2324f8829a4aSRandall Stewart 	}
2325f8829a4aSRandall Stewart 	return (tlen);
2326f8829a4aSRandall Stewart }
2327f8829a4aSRandall Stewart 
2328f8829a4aSRandall Stewart void
2329f8829a4aSRandall Stewart sctp_mtu_size_reset(struct sctp_inpcb *inp,
233044b7479bSRandall Stewart     struct sctp_association *asoc, uint32_t mtu)
2331f8829a4aSRandall Stewart {
2332f8829a4aSRandall Stewart 	/*
2333f8829a4aSRandall Stewart 	 * Reset the P-MTU size on this association, this involves changing
2334f8829a4aSRandall Stewart 	 * the asoc MTU, going through ANY chunk+overhead larger than mtu to
2335f8829a4aSRandall Stewart 	 * allow the DF flag to be cleared.
2336f8829a4aSRandall Stewart 	 */
2337f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
2338f8829a4aSRandall Stewart 	unsigned int eff_mtu, ovh;
2339f8829a4aSRandall Stewart 
2340f8829a4aSRandall Stewart 	asoc->smallest_mtu = mtu;
2341f8829a4aSRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2342f8829a4aSRandall Stewart 		ovh = SCTP_MIN_OVERHEAD;
2343f8829a4aSRandall Stewart 	} else {
2344f8829a4aSRandall Stewart 		ovh = SCTP_MIN_V4_OVERHEAD;
2345f8829a4aSRandall Stewart 	}
2346f8829a4aSRandall Stewart 	eff_mtu = mtu - ovh;
2347f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
2348f8829a4aSRandall Stewart 		if (chk->send_size > eff_mtu) {
2349f8829a4aSRandall Stewart 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2350f8829a4aSRandall Stewart 		}
2351f8829a4aSRandall Stewart 	}
2352f8829a4aSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
2353f8829a4aSRandall Stewart 		if (chk->send_size > eff_mtu) {
2354f8829a4aSRandall Stewart 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
2355f8829a4aSRandall Stewart 		}
2356f8829a4aSRandall Stewart 	}
2357f8829a4aSRandall Stewart }
2358f8829a4aSRandall Stewart 
2359f8829a4aSRandall Stewart 
2360f8829a4aSRandall Stewart /*
2361f8829a4aSRandall Stewart  * given an association and starting time of the current RTT period return
2362f42a358aSRandall Stewart  * RTO in number of msecs net should point to the current network
2363f8829a4aSRandall Stewart  */
2364899288aeSRandall Stewart 
2365f8829a4aSRandall Stewart uint32_t
2366f8829a4aSRandall Stewart sctp_calculate_rto(struct sctp_tcb *stcb,
2367f8829a4aSRandall Stewart     struct sctp_association *asoc,
2368f8829a4aSRandall Stewart     struct sctp_nets *net,
236918e198d3SRandall Stewart     struct timeval *told,
2370f79aab18SRandall Stewart     int safe, int rtt_from_sack)
2371f8829a4aSRandall Stewart {
237218e198d3SRandall Stewart 	/*-
2373f8829a4aSRandall Stewart 	 * given an association and the starting time of the current RTT
2374f42a358aSRandall Stewart 	 * period (in value1/value2) return RTO in number of msecs.
2375f8829a4aSRandall Stewart 	 */
2376be1d9176SMichael Tuexen 	int32_t rtt;		/* RTT in ms */
2377be1d9176SMichael Tuexen 	uint32_t new_rto;
2378f8829a4aSRandall Stewart 	int first_measure = 0;
237918e198d3SRandall Stewart 	struct timeval now, then, *old;
2380f8829a4aSRandall Stewart 
238118e198d3SRandall Stewart 	/* Copy it out for sparc64 */
238218e198d3SRandall Stewart 	if (safe == sctp_align_unsafe_makecopy) {
238318e198d3SRandall Stewart 		old = &then;
238418e198d3SRandall Stewart 		memcpy(&then, told, sizeof(struct timeval));
238518e198d3SRandall Stewart 	} else if (safe == sctp_align_safe_nocopy) {
238618e198d3SRandall Stewart 		old = told;
238718e198d3SRandall Stewart 	} else {
238818e198d3SRandall Stewart 		/* error */
238918e198d3SRandall Stewart 		SCTP_PRINTF("Huh, bad rto calc call\n");
239018e198d3SRandall Stewart 		return (0);
239118e198d3SRandall Stewart 	}
2392f8829a4aSRandall Stewart 	/************************/
2393f8829a4aSRandall Stewart 	/* 1. calculate new RTT */
2394f8829a4aSRandall Stewart 	/************************/
2395f8829a4aSRandall Stewart 	/* get the current time */
2396299108c5SRandall Stewart 	if (stcb->asoc.use_precise_time) {
2397299108c5SRandall Stewart 		(void)SCTP_GETPTIME_TIMEVAL(&now);
2398299108c5SRandall Stewart 	} else {
23996e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
2400299108c5SRandall Stewart 	}
2401be1d9176SMichael Tuexen 	timevalsub(&now, old);
2402be1d9176SMichael Tuexen 	/* store the current RTT in us */
240381eb4e63SMichael Tuexen 	net->rtt = (uint64_t) 1000000 *(uint64_t) now.tv_sec +
2404be1d9176SMichael Tuexen 	        (uint64_t) now.tv_usec;
2405be1d9176SMichael Tuexen 
2406*b60b0fe6SMichael Tuexen 	/* compute rtt in ms */
2407*b60b0fe6SMichael Tuexen 	rtt = (int32_t) (net->rtt / 1000);
2408f79aab18SRandall Stewart 	if ((asoc->cc_functions.sctp_rtt_calculated) && (rtt_from_sack == SCTP_RTT_FROM_DATA)) {
2409f79aab18SRandall Stewart 		/*
2410f79aab18SRandall Stewart 		 * Tell the CC module that a new update has just occurred
2411f79aab18SRandall Stewart 		 * from a sack
2412f79aab18SRandall Stewart 		 */
2413f79aab18SRandall Stewart 		(*asoc->cc_functions.sctp_rtt_calculated) (stcb, net, &now);
2414f79aab18SRandall Stewart 	}
2415f79aab18SRandall Stewart 	/*
2416f79aab18SRandall Stewart 	 * Do we need to determine the lan? We do this only on sacks i.e.
2417f79aab18SRandall Stewart 	 * RTT being determined from data not non-data (HB/INIT->INITACK).
2418f79aab18SRandall Stewart 	 */
2419f79aab18SRandall Stewart 	if ((rtt_from_sack == SCTP_RTT_FROM_DATA) &&
2420be1d9176SMichael Tuexen 	    (net->lan_type == SCTP_LAN_UNKNOWN)) {
2421be1d9176SMichael Tuexen 		if (net->rtt > SCTP_LOCAL_LAN_RTT) {
2422899288aeSRandall Stewart 			net->lan_type = SCTP_LAN_INTERNET;
2423899288aeSRandall Stewart 		} else {
2424899288aeSRandall Stewart 			net->lan_type = SCTP_LAN_LOCAL;
2425899288aeSRandall Stewart 		}
2426899288aeSRandall Stewart 	}
2427f8829a4aSRandall Stewart 	/***************************/
2428f8829a4aSRandall Stewart 	/* 2. update RTTVAR & SRTT */
2429f8829a4aSRandall Stewart 	/***************************/
2430be1d9176SMichael Tuexen 	/*-
2431be1d9176SMichael Tuexen 	 * Compute the scaled average lastsa and the
2432be1d9176SMichael Tuexen 	 * scaled variance lastsv as described in van Jacobson
2433be1d9176SMichael Tuexen 	 * Paper "Congestion Avoidance and Control", Annex A.
2434be1d9176SMichael Tuexen 	 *
2435be1d9176SMichael Tuexen 	 * (net->lastsa >> SCTP_RTT_SHIFT) is the srtt
2436be1d9176SMichael Tuexen 	 * (net->lastsa >> SCTP_RTT_VAR_SHIFT) is the rttvar
2437be1d9176SMichael Tuexen 	 */
24389a972525SRandall Stewart 	if (net->RTO_measured) {
2439be1d9176SMichael Tuexen 		rtt -= (net->lastsa >> SCTP_RTT_SHIFT);
2440be1d9176SMichael Tuexen 		net->lastsa += rtt;
2441be1d9176SMichael Tuexen 		if (rtt < 0) {
2442be1d9176SMichael Tuexen 			rtt = -rtt;
2443be1d9176SMichael Tuexen 		}
2444be1d9176SMichael Tuexen 		rtt -= (net->lastsv >> SCTP_RTT_VAR_SHIFT);
2445be1d9176SMichael Tuexen 		net->lastsv += rtt;
2446b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2447f8829a4aSRandall Stewart 			rto_logging(net, SCTP_LOG_RTTVAR);
244880fefe0aSRandall Stewart 		}
2449f8829a4aSRandall Stewart 	} else {
2450f8829a4aSRandall Stewart 		/* First RTO measurment */
24519a972525SRandall Stewart 		net->RTO_measured = 1;
2452f8829a4aSRandall Stewart 		first_measure = 1;
2453be1d9176SMichael Tuexen 		net->lastsa = rtt << SCTP_RTT_SHIFT;
2454be1d9176SMichael Tuexen 		net->lastsv = (rtt / 2) << SCTP_RTT_VAR_SHIFT;
2455b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
2456f8829a4aSRandall Stewart 			rto_logging(net, SCTP_LOG_INITIAL_RTT);
245780fefe0aSRandall Stewart 		}
2458f8829a4aSRandall Stewart 	}
2459be1d9176SMichael Tuexen 	if (net->lastsv == 0) {
2460be1d9176SMichael Tuexen 		net->lastsv = SCTP_CLOCK_GRANULARITY;
2461be1d9176SMichael Tuexen 	}
2462108df27cSRandall Stewart 	new_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
2463f8829a4aSRandall Stewart 	if ((new_rto > SCTP_SAT_NETWORK_MIN) &&
2464f8829a4aSRandall Stewart 	    (stcb->asoc.sat_network_lockout == 0)) {
2465f8829a4aSRandall Stewart 		stcb->asoc.sat_network = 1;
2466f8829a4aSRandall Stewart 	} else if ((!first_measure) && stcb->asoc.sat_network) {
2467f8829a4aSRandall Stewart 		stcb->asoc.sat_network = 0;
2468f8829a4aSRandall Stewart 		stcb->asoc.sat_network_lockout = 1;
2469f8829a4aSRandall Stewart 	}
2470f8829a4aSRandall Stewart 	/* bound it, per C6/C7 in Section 5.3.1 */
2471f8829a4aSRandall Stewart 	if (new_rto < stcb->asoc.minrto) {
2472f8829a4aSRandall Stewart 		new_rto = stcb->asoc.minrto;
2473f8829a4aSRandall Stewart 	}
2474f8829a4aSRandall Stewart 	if (new_rto > stcb->asoc.maxrto) {
2475f8829a4aSRandall Stewart 		new_rto = stcb->asoc.maxrto;
2476f8829a4aSRandall Stewart 	}
24775e54f665SRandall Stewart 	/* we are now returning the RTO */
24785e54f665SRandall Stewart 	return (new_rto);
2479f8829a4aSRandall Stewart }
2480f8829a4aSRandall Stewart 
2481f8829a4aSRandall Stewart /*
2482f8829a4aSRandall Stewart  * return a pointer to a contiguous piece of data from the given mbuf chain
2483f8829a4aSRandall Stewart  * starting at 'off' for 'len' bytes.  If the desired piece spans more than
2484f8829a4aSRandall Stewart  * one mbuf, a copy is made at 'ptr'. caller must ensure that the buffer size
2485f8829a4aSRandall Stewart  * is >= 'len' returns NULL if there there isn't 'len' bytes in the chain.
2486f8829a4aSRandall Stewart  */
248772fb6fdbSRandall Stewart caddr_t
2488f8829a4aSRandall Stewart sctp_m_getptr(struct mbuf *m, int off, int len, uint8_t * in_ptr)
2489f8829a4aSRandall Stewart {
2490f8829a4aSRandall Stewart 	uint32_t count;
2491f8829a4aSRandall Stewart 	uint8_t *ptr;
2492f8829a4aSRandall Stewart 
2493f8829a4aSRandall Stewart 	ptr = in_ptr;
2494f8829a4aSRandall Stewart 	if ((off < 0) || (len <= 0))
2495f8829a4aSRandall Stewart 		return (NULL);
2496f8829a4aSRandall Stewart 
2497f8829a4aSRandall Stewart 	/* find the desired start location */
2498f8829a4aSRandall Stewart 	while ((m != NULL) && (off > 0)) {
2499139bc87fSRandall Stewart 		if (off < SCTP_BUF_LEN(m))
2500f8829a4aSRandall Stewart 			break;
2501139bc87fSRandall Stewart 		off -= SCTP_BUF_LEN(m);
2502139bc87fSRandall Stewart 		m = SCTP_BUF_NEXT(m);
2503f8829a4aSRandall Stewart 	}
2504f8829a4aSRandall Stewart 	if (m == NULL)
2505f8829a4aSRandall Stewart 		return (NULL);
2506f8829a4aSRandall Stewart 
2507f8829a4aSRandall Stewart 	/* is the current mbuf large enough (eg. contiguous)? */
2508139bc87fSRandall Stewart 	if ((SCTP_BUF_LEN(m) - off) >= len) {
2509f8829a4aSRandall Stewart 		return (mtod(m, caddr_t)+off);
2510f8829a4aSRandall Stewart 	} else {
2511f8829a4aSRandall Stewart 		/* else, it spans more than one mbuf, so save a temp copy... */
2512f8829a4aSRandall Stewart 		while ((m != NULL) && (len > 0)) {
2513139bc87fSRandall Stewart 			count = min(SCTP_BUF_LEN(m) - off, len);
2514f8829a4aSRandall Stewart 			bcopy(mtod(m, caddr_t)+off, ptr, count);
2515f8829a4aSRandall Stewart 			len -= count;
2516f8829a4aSRandall Stewart 			ptr += count;
2517f8829a4aSRandall Stewart 			off = 0;
2518139bc87fSRandall Stewart 			m = SCTP_BUF_NEXT(m);
2519f8829a4aSRandall Stewart 		}
2520f8829a4aSRandall Stewart 		if ((m == NULL) && (len > 0))
2521f8829a4aSRandall Stewart 			return (NULL);
2522f8829a4aSRandall Stewart 		else
2523f8829a4aSRandall Stewart 			return ((caddr_t)in_ptr);
2524f8829a4aSRandall Stewart 	}
2525f8829a4aSRandall Stewart }
2526f8829a4aSRandall Stewart 
2527f8829a4aSRandall Stewart 
252844b7479bSRandall Stewart 
2529f8829a4aSRandall Stewart struct sctp_paramhdr *
2530f8829a4aSRandall Stewart sctp_get_next_param(struct mbuf *m,
2531f8829a4aSRandall Stewart     int offset,
2532f8829a4aSRandall Stewart     struct sctp_paramhdr *pull,
2533f8829a4aSRandall Stewart     int pull_limit)
2534f8829a4aSRandall Stewart {
2535f8829a4aSRandall Stewart 	/* This just provides a typed signature to Peter's Pull routine */
2536f8829a4aSRandall Stewart 	return ((struct sctp_paramhdr *)sctp_m_getptr(m, offset, pull_limit,
2537f8829a4aSRandall Stewart 	    (uint8_t *) pull));
2538f8829a4aSRandall Stewart }
2539f8829a4aSRandall Stewart 
2540f8829a4aSRandall Stewart 
2541ce11b842SMichael Tuexen struct mbuf *
2542f8829a4aSRandall Stewart sctp_add_pad_tombuf(struct mbuf *m, int padlen)
2543f8829a4aSRandall Stewart {
2544ce11b842SMichael Tuexen 	struct mbuf *m_last;
2545ce11b842SMichael Tuexen 	caddr_t dp;
2546f8829a4aSRandall Stewart 
2547f8829a4aSRandall Stewart 	if (padlen > 3) {
2548ce11b842SMichael Tuexen 		return (NULL);
2549f8829a4aSRandall Stewart 	}
255041eee555SRandall Stewart 	if (padlen <= M_TRAILINGSPACE(m)) {
2551f8829a4aSRandall Stewart 		/*
2552f8829a4aSRandall Stewart 		 * The easy way. We hope the majority of the time we hit
2553f8829a4aSRandall Stewart 		 * here :)
2554f8829a4aSRandall Stewart 		 */
2555ce11b842SMichael Tuexen 		m_last = m;
2556f8829a4aSRandall Stewart 	} else {
2557ce11b842SMichael Tuexen 		/* Hard way we must grow the mbuf chain */
2558ce11b842SMichael Tuexen 		m_last = sctp_get_mbuf_for_msg(padlen, 0, M_NOWAIT, 1, MT_DATA);
2559ce11b842SMichael Tuexen 		if (m_last == NULL) {
2560ce11b842SMichael Tuexen 			return (NULL);
2561f8829a4aSRandall Stewart 		}
2562ce11b842SMichael Tuexen 		SCTP_BUF_LEN(m_last) = 0;
2563ce11b842SMichael Tuexen 		SCTP_BUF_NEXT(m_last) = NULL;
2564ce11b842SMichael Tuexen 		SCTP_BUF_NEXT(m) = m_last;
2565f8829a4aSRandall Stewart 	}
2566ce11b842SMichael Tuexen 	dp = mtod(m_last, caddr_t)+SCTP_BUF_LEN(m_last);
2567ce11b842SMichael Tuexen 	SCTP_BUF_LEN(m_last) += padlen;
2568ce11b842SMichael Tuexen 	memset(dp, 0, padlen);
2569ce11b842SMichael Tuexen 	return (m_last);
2570f8829a4aSRandall Stewart }
2571f8829a4aSRandall Stewart 
2572ce11b842SMichael Tuexen struct mbuf *
2573f8829a4aSRandall Stewart sctp_pad_lastmbuf(struct mbuf *m, int padval, struct mbuf *last_mbuf)
2574f8829a4aSRandall Stewart {
2575f8829a4aSRandall Stewart 	/* find the last mbuf in chain and pad it */
2576f8829a4aSRandall Stewart 	struct mbuf *m_at;
2577f8829a4aSRandall Stewart 
2578ce11b842SMichael Tuexen 	if (last_mbuf != NULL) {
2579f8829a4aSRandall Stewart 		return (sctp_add_pad_tombuf(last_mbuf, padval));
2580f8829a4aSRandall Stewart 	} else {
258117267b32SMichael Tuexen 		for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
2582139bc87fSRandall Stewart 			if (SCTP_BUF_NEXT(m_at) == NULL) {
2583f8829a4aSRandall Stewart 				return (sctp_add_pad_tombuf(m_at, padval));
2584f8829a4aSRandall Stewart 			}
2585f8829a4aSRandall Stewart 		}
2586f8829a4aSRandall Stewart 	}
2587ce11b842SMichael Tuexen 	return (NULL);
2588f8829a4aSRandall Stewart }
2589f8829a4aSRandall Stewart 
2590f8829a4aSRandall Stewart static void
2591c5b5675dSMichael Tuexen sctp_notify_assoc_change(uint16_t state, struct sctp_tcb *stcb,
2592410a3b1eSMichael Tuexen     uint16_t error, struct sctp_abort_chunk *abort, uint8_t from_peer, int so_locked
2593ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2594ceaad40aSRandall Stewart     SCTP_UNUSED
2595ceaad40aSRandall Stewart #endif
2596ceaad40aSRandall Stewart )
2597f8829a4aSRandall Stewart {
2598f8829a4aSRandall Stewart 	struct mbuf *m_notify;
2599f8829a4aSRandall Stewart 	struct sctp_assoc_change *sac;
2600f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
2601a2b42326SMichael Tuexen 	size_t notif_len, abort_len;
2602e06b67c7SMichael Tuexen 	unsigned int i;
2603f8829a4aSRandall Stewart 
2604ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2605ceaad40aSRandall Stewart 	struct socket *so;
2606ceaad40aSRandall Stewart 
2607ceaad40aSRandall Stewart #endif
2608ceaad40aSRandall Stewart 
260958411b08SMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT)) {
2610a2b42326SMichael Tuexen 		notif_len = sizeof(struct sctp_assoc_change);
2611a2b42326SMichael Tuexen 		if (abort != NULL) {
2612c9eb4473SMichael Tuexen 			abort_len = ntohs(abort->ch.chunk_length);
2613a2b42326SMichael Tuexen 		} else {
2614a2b42326SMichael Tuexen 			abort_len = 0;
2615c5b5675dSMichael Tuexen 		}
2616a2b42326SMichael Tuexen 		if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
2617a2b42326SMichael Tuexen 			notif_len += SCTP_ASSOC_SUPPORTS_MAX;
2618a2b42326SMichael Tuexen 		} else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
2619a2b42326SMichael Tuexen 			notif_len += abort_len;
2620a2b42326SMichael Tuexen 		}
2621eb1b1807SGleb Smirnoff 		m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
2622a2b42326SMichael Tuexen 		if (m_notify == NULL) {
2623a2b42326SMichael Tuexen 			/* Retry with smaller value. */
2624a2b42326SMichael Tuexen 			notif_len = sizeof(struct sctp_assoc_change);
2625eb1b1807SGleb Smirnoff 			m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
2626a2b42326SMichael Tuexen 			if (m_notify == NULL) {
262758411b08SMichael Tuexen 				goto set_error;
2628a2b42326SMichael Tuexen 			}
2629a2b42326SMichael Tuexen 		}
2630a2b42326SMichael Tuexen 		SCTP_BUF_NEXT(m_notify) = NULL;
2631f8829a4aSRandall Stewart 		sac = mtod(m_notify, struct sctp_assoc_change *);
2632e432298aSXin LI 		memset(sac, 0, notif_len);
2633f8829a4aSRandall Stewart 		sac->sac_type = SCTP_ASSOC_CHANGE;
2634f8829a4aSRandall Stewart 		sac->sac_flags = 0;
2635f8829a4aSRandall Stewart 		sac->sac_length = sizeof(struct sctp_assoc_change);
2636c5b5675dSMichael Tuexen 		sac->sac_state = state;
2637f8829a4aSRandall Stewart 		sac->sac_error = error;
2638f8829a4aSRandall Stewart 		/* XXX verify these stream counts */
2639f8829a4aSRandall Stewart 		sac->sac_outbound_streams = stcb->asoc.streamoutcnt;
2640f8829a4aSRandall Stewart 		sac->sac_inbound_streams = stcb->asoc.streamincnt;
2641f8829a4aSRandall Stewart 		sac->sac_assoc_id = sctp_get_associd(stcb);
2642a2b42326SMichael Tuexen 		if (notif_len > sizeof(struct sctp_assoc_change)) {
2643c5b5675dSMichael Tuexen 			if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
2644e06b67c7SMichael Tuexen 				i = 0;
2645c79bec9cSMichael Tuexen 				if (stcb->asoc.prsctp_supported == 1) {
2646e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_PR;
2647e06b67c7SMichael Tuexen 				}
2648c79bec9cSMichael Tuexen 				if (stcb->asoc.auth_supported == 1) {
2649e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_AUTH;
2650e06b67c7SMichael Tuexen 				}
2651c79bec9cSMichael Tuexen 				if (stcb->asoc.asconf_supported == 1) {
2652e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_ASCONF;
2653e06b67c7SMichael Tuexen 				}
2654e06b67c7SMichael Tuexen 				sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_MULTIBUF;
2655c79bec9cSMichael Tuexen 				if (stcb->asoc.reconfig_supported == 1) {
2656e06b67c7SMichael Tuexen 					sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_RE_CONFIG;
2657e06b67c7SMichael Tuexen 				}
2658e06b67c7SMichael Tuexen 				sac->sac_length += i;
2659a2b42326SMichael Tuexen 			} else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
2660a2b42326SMichael Tuexen 				memcpy(sac->sac_info, abort, abort_len);
2661a2b42326SMichael Tuexen 				sac->sac_length += abort_len;
2662a2b42326SMichael Tuexen 			}
2663c5b5675dSMichael Tuexen 		}
2664e06b67c7SMichael Tuexen 		SCTP_BUF_LEN(m_notify) = sac->sac_length;
2665f8829a4aSRandall Stewart 		control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
26667215cc1bSMichael Tuexen 		    0, 0, stcb->asoc.context, 0, 0, 0,
2667f8829a4aSRandall Stewart 		    m_notify);
266858411b08SMichael Tuexen 		if (control != NULL) {
2669139bc87fSRandall Stewart 			control->length = SCTP_BUF_LEN(m_notify);
2670f8829a4aSRandall Stewart 			/* not that we need this */
2671f8829a4aSRandall Stewart 			control->tail_mbuf = m_notify;
2672139bc87fSRandall Stewart 			control->spec_flags = M_NOTIFICATION;
2673f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep, stcb,
2674f8829a4aSRandall Stewart 			    control,
2675cfde3ff7SRandall Stewart 			    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD,
2676cfde3ff7SRandall Stewart 			    so_locked);
267758411b08SMichael Tuexen 		} else {
267858411b08SMichael Tuexen 			sctp_m_freem(m_notify);
267958411b08SMichael Tuexen 		}
268058411b08SMichael Tuexen 	}
268158411b08SMichael Tuexen 	/*
268258411b08SMichael Tuexen 	 * For 1-to-1 style sockets, we send up and error when an ABORT
268358411b08SMichael Tuexen 	 * comes in.
268458411b08SMichael Tuexen 	 */
268558411b08SMichael Tuexen set_error:
268658411b08SMichael Tuexen 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
268758411b08SMichael Tuexen 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
268858411b08SMichael Tuexen 	    ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
2689e045904fSMichael Tuexen 		SOCK_LOCK(stcb->sctp_socket);
2690410a3b1eSMichael Tuexen 		if (from_peer) {
269158411b08SMichael Tuexen 			if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) {
269258411b08SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNREFUSED);
269358411b08SMichael Tuexen 				stcb->sctp_socket->so_error = ECONNREFUSED;
269458411b08SMichael Tuexen 			} else {
269558411b08SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
269658411b08SMichael Tuexen 				stcb->sctp_socket->so_error = ECONNRESET;
269758411b08SMichael Tuexen 			}
2698410a3b1eSMichael Tuexen 		} else {
2699553bb068SMichael Tuexen 			if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) ||
2700553bb068SMichael Tuexen 			    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
2701553bb068SMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ETIMEDOUT);
2702553bb068SMichael Tuexen 				stcb->sctp_socket->so_error = ETIMEDOUT;
2703553bb068SMichael Tuexen 			} else {
2704410a3b1eSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNABORTED);
2705410a3b1eSMichael Tuexen 				stcb->sctp_socket->so_error = ECONNABORTED;
2706410a3b1eSMichael Tuexen 			}
270758411b08SMichael Tuexen 		}
2708553bb068SMichael Tuexen 	}
270958411b08SMichael Tuexen 	/* Wake ANY sleepers */
2710ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2711ceaad40aSRandall Stewart 	so = SCTP_INP_SO(stcb->sctp_ep);
2712ceaad40aSRandall Stewart 	if (!so_locked) {
2713ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
2714ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
2715ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
2716ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
2717ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
2718ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2719ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
2720ceaad40aSRandall Stewart 			return;
2721ceaad40aSRandall Stewart 		}
2722ceaad40aSRandall Stewart 	}
2723ceaad40aSRandall Stewart #endif
272458411b08SMichael Tuexen 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
272558411b08SMichael Tuexen 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
272658411b08SMichael Tuexen 	    ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
2727e045904fSMichael Tuexen 		socantrcvmore_locked(stcb->sctp_socket);
272858411b08SMichael Tuexen 	}
272958411b08SMichael Tuexen 	sorwakeup(stcb->sctp_socket);
273058411b08SMichael Tuexen 	sowwakeup(stcb->sctp_socket);
2731ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2732ceaad40aSRandall Stewart 	if (!so_locked) {
2733ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
2734ceaad40aSRandall Stewart 	}
2735ceaad40aSRandall Stewart #endif
2736f8829a4aSRandall Stewart }
2737f8829a4aSRandall Stewart 
2738f8829a4aSRandall Stewart static void
2739f8829a4aSRandall Stewart sctp_notify_peer_addr_change(struct sctp_tcb *stcb, uint32_t state,
2740f8829a4aSRandall Stewart     struct sockaddr *sa, uint32_t error)
2741f8829a4aSRandall Stewart {
2742f8829a4aSRandall Stewart 	struct mbuf *m_notify;
2743f8829a4aSRandall Stewart 	struct sctp_paddr_change *spc;
2744f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
2745f8829a4aSRandall Stewart 
274660990c0cSMichael Tuexen 	if ((stcb == NULL) ||
274760990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT)) {
2748f8829a4aSRandall Stewart 		/* event not enabled */
2749f8829a4aSRandall Stewart 		return;
2750830d754dSRandall Stewart 	}
2751eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_paddr_change), 0, M_NOWAIT, 1, MT_DATA);
2752f8829a4aSRandall Stewart 	if (m_notify == NULL)
2753f8829a4aSRandall Stewart 		return;
2754139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
2755f8829a4aSRandall Stewart 	spc = mtod(m_notify, struct sctp_paddr_change *);
275656711f94SMichael Tuexen 	memset(spc, 0, sizeof(struct sctp_paddr_change));
2757f8829a4aSRandall Stewart 	spc->spc_type = SCTP_PEER_ADDR_CHANGE;
2758f8829a4aSRandall Stewart 	spc->spc_flags = 0;
2759f8829a4aSRandall Stewart 	spc->spc_length = sizeof(struct sctp_paddr_change);
27605e2c2d87SRandall Stewart 	switch (sa->sa_family) {
2761ea5eba11SMichael Tuexen #ifdef INET
27625e2c2d87SRandall Stewart 	case AF_INET:
2763f8829a4aSRandall Stewart 		memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
27645e2c2d87SRandall Stewart 		break;
2765ea5eba11SMichael Tuexen #endif
27665e2c2d87SRandall Stewart #ifdef INET6
27675e2c2d87SRandall Stewart 	case AF_INET6:
27685e2c2d87SRandall Stewart 		{
2769f42a358aSRandall Stewart 			struct sockaddr_in6 *sin6;
2770f42a358aSRandall Stewart 
2771f8829a4aSRandall Stewart 			memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in6));
2772f42a358aSRandall Stewart 
2773f42a358aSRandall Stewart 			sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
2774f42a358aSRandall Stewart 			if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
277542551e99SRandall Stewart 				if (sin6->sin6_scope_id == 0) {
277642551e99SRandall Stewart 					/* recover scope_id for user */
2777f42a358aSRandall Stewart 					(void)sa6_recoverscope(sin6);
277842551e99SRandall Stewart 				} else {
277942551e99SRandall Stewart 					/* clear embedded scope_id for user */
278042551e99SRandall Stewart 					in6_clearscope(&sin6->sin6_addr);
278142551e99SRandall Stewart 				}
2782f42a358aSRandall Stewart 			}
27835e2c2d87SRandall Stewart 			break;
27845e2c2d87SRandall Stewart 		}
27855e2c2d87SRandall Stewart #endif
27865e2c2d87SRandall Stewart 	default:
27875e2c2d87SRandall Stewart 		/* TSNH */
27885e2c2d87SRandall Stewart 		break;
2789f8829a4aSRandall Stewart 	}
2790f8829a4aSRandall Stewart 	spc->spc_state = state;
2791f8829a4aSRandall Stewart 	spc->spc_error = error;
2792f8829a4aSRandall Stewart 	spc->spc_assoc_id = sctp_get_associd(stcb);
2793f8829a4aSRandall Stewart 
2794139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_paddr_change);
2795139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
2796f8829a4aSRandall Stewart 
2797f8829a4aSRandall Stewart 	/* append to socket */
2798f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
27997215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
2800f8829a4aSRandall Stewart 	    m_notify);
2801f8829a4aSRandall Stewart 	if (control == NULL) {
2802f8829a4aSRandall Stewart 		/* no memory */
2803f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
2804f8829a4aSRandall Stewart 		return;
2805f8829a4aSRandall Stewart 	}
2806139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
2807139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
2808f8829a4aSRandall Stewart 	/* not that we need this */
2809f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
2810f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
2811f8829a4aSRandall Stewart 	    control,
2812cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1,
2813cfde3ff7SRandall Stewart 	    SCTP_READ_LOCK_NOT_HELD,
2814cfde3ff7SRandall Stewart 	    SCTP_SO_NOT_LOCKED);
2815f8829a4aSRandall Stewart }
2816f8829a4aSRandall Stewart 
2817f8829a4aSRandall Stewart 
2818f8829a4aSRandall Stewart static void
28191edc9dbaSMichael Tuexen sctp_notify_send_failed(struct sctp_tcb *stcb, uint8_t sent, uint32_t error,
2820ceaad40aSRandall Stewart     struct sctp_tmit_chunk *chk, int so_locked
2821ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2822ceaad40aSRandall Stewart     SCTP_UNUSED
2823ceaad40aSRandall Stewart #endif
2824ceaad40aSRandall Stewart )
2825f8829a4aSRandall Stewart {
2826830d754dSRandall Stewart 	struct mbuf *m_notify;
2827f8829a4aSRandall Stewart 	struct sctp_send_failed *ssf;
28289935403aSMichael Tuexen 	struct sctp_send_failed_event *ssfe;
2829f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
2830f8829a4aSRandall Stewart 	int length;
2831f8829a4aSRandall Stewart 
283260990c0cSMichael Tuexen 	if ((stcb == NULL) ||
28339935403aSMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
28349935403aSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
2835f8829a4aSRandall Stewart 		/* event not enabled */
2836f8829a4aSRandall Stewart 		return;
2837830d754dSRandall Stewart 	}
28389935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
28399935403aSMichael Tuexen 		length = sizeof(struct sctp_send_failed_event);
28409935403aSMichael Tuexen 	} else {
28419935403aSMichael Tuexen 		length = sizeof(struct sctp_send_failed);
28429935403aSMichael Tuexen 	}
2843eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(length, 0, M_NOWAIT, 1, MT_DATA);
2844f8829a4aSRandall Stewart 	if (m_notify == NULL)
2845f8829a4aSRandall Stewart 		/* no space left */
2846f8829a4aSRandall Stewart 		return;
2847139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
28489935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
28499935403aSMichael Tuexen 		ssfe = mtod(m_notify, struct sctp_send_failed_event *);
2850e432298aSXin LI 		memset(ssfe, 0, length);
28519935403aSMichael Tuexen 		ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
28521edc9dbaSMichael Tuexen 		if (sent) {
28539935403aSMichael Tuexen 			ssfe->ssfe_flags = SCTP_DATA_SENT;
28541edc9dbaSMichael Tuexen 		} else {
28551edc9dbaSMichael Tuexen 			ssfe->ssfe_flags = SCTP_DATA_UNSENT;
28561edc9dbaSMichael Tuexen 		}
2857e432298aSXin LI 		length += chk->send_size;
2858e432298aSXin LI 		length -= sizeof(struct sctp_data_chunk);
28599935403aSMichael Tuexen 		ssfe->ssfe_length = length;
28609935403aSMichael Tuexen 		ssfe->ssfe_error = error;
28619935403aSMichael Tuexen 		/* not exactly what the user sent in, but should be close :) */
28629935403aSMichael Tuexen 		ssfe->ssfe_info.snd_sid = chk->rec.data.stream_number;
28639935403aSMichael Tuexen 		ssfe->ssfe_info.snd_flags = chk->rec.data.rcv_flags;
28649935403aSMichael Tuexen 		ssfe->ssfe_info.snd_ppid = chk->rec.data.payloadtype;
28659935403aSMichael Tuexen 		ssfe->ssfe_info.snd_context = chk->rec.data.context;
28669935403aSMichael Tuexen 		ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
28679935403aSMichael Tuexen 		ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
28689935403aSMichael Tuexen 		SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed_event);
28699935403aSMichael Tuexen 	} else {
2870f8829a4aSRandall Stewart 		ssf = mtod(m_notify, struct sctp_send_failed *);
2871e432298aSXin LI 		memset(ssf, 0, length);
2872f8829a4aSRandall Stewart 		ssf->ssf_type = SCTP_SEND_FAILED;
28731edc9dbaSMichael Tuexen 		if (sent) {
2874f8829a4aSRandall Stewart 			ssf->ssf_flags = SCTP_DATA_SENT;
28751edc9dbaSMichael Tuexen 		} else {
28761edc9dbaSMichael Tuexen 			ssf->ssf_flags = SCTP_DATA_UNSENT;
28771edc9dbaSMichael Tuexen 		}
2878e432298aSXin LI 		length += chk->send_size;
2879e432298aSXin LI 		length -= sizeof(struct sctp_data_chunk);
2880f8829a4aSRandall Stewart 		ssf->ssf_length = length;
2881f8829a4aSRandall Stewart 		ssf->ssf_error = error;
2882f8829a4aSRandall Stewart 		/* not exactly what the user sent in, but should be close :) */
2883d00aff5dSRandall Stewart 		bzero(&ssf->ssf_info, sizeof(ssf->ssf_info));
2884f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_stream = chk->rec.data.stream_number;
2885f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_ssn = chk->rec.data.stream_seq;
2886f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_flags = chk->rec.data.rcv_flags;
2887f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_ppid = chk->rec.data.payloadtype;
2888f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_context = chk->rec.data.context;
2889f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
2890f8829a4aSRandall Stewart 		ssf->ssf_assoc_id = sctp_get_associd(stcb);
28919935403aSMichael Tuexen 		SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed);
28929935403aSMichael Tuexen 	}
2893830d754dSRandall Stewart 	if (chk->data) {
2894830d754dSRandall Stewart 		/*
2895830d754dSRandall Stewart 		 * trim off the sctp chunk header(it should be there)
2896830d754dSRandall Stewart 		 */
2897830d754dSRandall Stewart 		if (chk->send_size >= sizeof(struct sctp_data_chunk)) {
2898830d754dSRandall Stewart 			m_adj(chk->data, sizeof(struct sctp_data_chunk));
2899830d754dSRandall Stewart 			sctp_mbuf_crush(chk->data);
2900830d754dSRandall Stewart 			chk->send_size -= sizeof(struct sctp_data_chunk);
2901830d754dSRandall Stewart 		}
2902830d754dSRandall Stewart 	}
2903810ec536SMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = chk->data;
2904f8829a4aSRandall Stewart 	/* Steal off the mbuf */
2905f8829a4aSRandall Stewart 	chk->data = NULL;
2906f8829a4aSRandall Stewart 	/*
2907f8829a4aSRandall Stewart 	 * For this case, we check the actual socket buffer, since the assoc
2908f8829a4aSRandall Stewart 	 * is going away we don't want to overfill the socket buffer for a
2909f8829a4aSRandall Stewart 	 * non-reader
2910f8829a4aSRandall Stewart 	 */
2911139bc87fSRandall Stewart 	if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
2912f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
2913f8829a4aSRandall Stewart 		return;
2914f8829a4aSRandall Stewart 	}
2915f8829a4aSRandall Stewart 	/* append to socket */
2916f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
29177215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
2918f8829a4aSRandall Stewart 	    m_notify);
2919f8829a4aSRandall Stewart 	if (control == NULL) {
2920f8829a4aSRandall Stewart 		/* no memory */
2921f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
2922f8829a4aSRandall Stewart 		return;
2923f8829a4aSRandall Stewart 	}
2924139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
2925f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
2926f8829a4aSRandall Stewart 	    control,
2927cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1,
2928cfde3ff7SRandall Stewart 	    SCTP_READ_LOCK_NOT_HELD,
2929cfde3ff7SRandall Stewart 	    so_locked);
2930f8829a4aSRandall Stewart }
2931f8829a4aSRandall Stewart 
2932f8829a4aSRandall Stewart 
2933f8829a4aSRandall Stewart static void
2934f8829a4aSRandall Stewart sctp_notify_send_failed2(struct sctp_tcb *stcb, uint32_t error,
2935ceaad40aSRandall Stewart     struct sctp_stream_queue_pending *sp, int so_locked
2936ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
2937ceaad40aSRandall Stewart     SCTP_UNUSED
2938ceaad40aSRandall Stewart #endif
2939ceaad40aSRandall Stewart )
2940f8829a4aSRandall Stewart {
2941f8829a4aSRandall Stewart 	struct mbuf *m_notify;
2942f8829a4aSRandall Stewart 	struct sctp_send_failed *ssf;
29439935403aSMichael Tuexen 	struct sctp_send_failed_event *ssfe;
2944f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
2945f8829a4aSRandall Stewart 	int length;
2946f8829a4aSRandall Stewart 
294760990c0cSMichael Tuexen 	if ((stcb == NULL) ||
29489935403aSMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
29499935403aSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
2950f8829a4aSRandall Stewart 		/* event not enabled */
2951f8829a4aSRandall Stewart 		return;
2952830d754dSRandall Stewart 	}
29539935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
29549935403aSMichael Tuexen 		length = sizeof(struct sctp_send_failed_event);
29559935403aSMichael Tuexen 	} else {
29569935403aSMichael Tuexen 		length = sizeof(struct sctp_send_failed);
29579935403aSMichael Tuexen 	}
2958eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(length, 0, M_NOWAIT, 1, MT_DATA);
29599935403aSMichael Tuexen 	if (m_notify == NULL) {
2960f8829a4aSRandall Stewart 		/* no space left */
2961f8829a4aSRandall Stewart 		return;
29629935403aSMichael Tuexen 	}
2963139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
29649935403aSMichael Tuexen 	if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
29659935403aSMichael Tuexen 		ssfe = mtod(m_notify, struct sctp_send_failed_event *);
2966e432298aSXin LI 		memset(ssfe, 0, length);
2967ad83c8a5SMichael Tuexen 		ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
29689935403aSMichael Tuexen 		ssfe->ssfe_flags = SCTP_DATA_UNSENT;
2969e432298aSXin LI 		length += sp->length;
29709935403aSMichael Tuexen 		ssfe->ssfe_length = length;
29719935403aSMichael Tuexen 		ssfe->ssfe_error = error;
29729935403aSMichael Tuexen 		/* not exactly what the user sent in, but should be close :) */
29739935403aSMichael Tuexen 		ssfe->ssfe_info.snd_sid = sp->stream;
29749935403aSMichael Tuexen 		if (sp->some_taken) {
29759935403aSMichael Tuexen 			ssfe->ssfe_info.snd_flags = SCTP_DATA_LAST_FRAG;
29769935403aSMichael Tuexen 		} else {
29779935403aSMichael Tuexen 			ssfe->ssfe_info.snd_flags = SCTP_DATA_NOT_FRAG;
29789935403aSMichael Tuexen 		}
29799935403aSMichael Tuexen 		ssfe->ssfe_info.snd_ppid = sp->ppid;
29809935403aSMichael Tuexen 		ssfe->ssfe_info.snd_context = sp->context;
29819935403aSMichael Tuexen 		ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
29829935403aSMichael Tuexen 		ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
29839935403aSMichael Tuexen 		SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed_event);
29849935403aSMichael Tuexen 	} else {
2985f8829a4aSRandall Stewart 		ssf = mtod(m_notify, struct sctp_send_failed *);
2986e432298aSXin LI 		memset(ssf, 0, length);
2987f8829a4aSRandall Stewart 		ssf->ssf_type = SCTP_SEND_FAILED;
2988f8829a4aSRandall Stewart 		ssf->ssf_flags = SCTP_DATA_UNSENT;
2989e432298aSXin LI 		length += sp->length;
2990f8829a4aSRandall Stewart 		ssf->ssf_length = length;
2991f8829a4aSRandall Stewart 		ssf->ssf_error = error;
2992f8829a4aSRandall Stewart 		/* not exactly what the user sent in, but should be close :) */
2993f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_stream = sp->stream;
2994f3b05218SMichael Tuexen 		ssf->ssf_info.sinfo_ssn = 0;
2995fc14de76SRandall Stewart 		if (sp->some_taken) {
2996fc14de76SRandall Stewart 			ssf->ssf_info.sinfo_flags = SCTP_DATA_LAST_FRAG;
2997fc14de76SRandall Stewart 		} else {
2998fc14de76SRandall Stewart 			ssf->ssf_info.sinfo_flags = SCTP_DATA_NOT_FRAG;
2999fc14de76SRandall Stewart 		}
3000f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_ppid = sp->ppid;
3001f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_context = sp->context;
3002f8829a4aSRandall Stewart 		ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3003f8829a4aSRandall Stewart 		ssf->ssf_assoc_id = sctp_get_associd(stcb);
3004139bc87fSRandall Stewart 		SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed);
30059935403aSMichael Tuexen 	}
30069935403aSMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = sp->data;
3007f8829a4aSRandall Stewart 
3008f8829a4aSRandall Stewart 	/* Steal off the mbuf */
3009f8829a4aSRandall Stewart 	sp->data = NULL;
3010f8829a4aSRandall Stewart 	/*
3011f8829a4aSRandall Stewart 	 * For this case, we check the actual socket buffer, since the assoc
3012f8829a4aSRandall Stewart 	 * is going away we don't want to overfill the socket buffer for a
3013f8829a4aSRandall Stewart 	 * non-reader
3014f8829a4aSRandall Stewart 	 */
3015139bc87fSRandall Stewart 	if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3016f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3017f8829a4aSRandall Stewart 		return;
3018f8829a4aSRandall Stewart 	}
3019f8829a4aSRandall Stewart 	/* append to socket */
3020f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
30217215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3022f8829a4aSRandall Stewart 	    m_notify);
3023f8829a4aSRandall Stewart 	if (control == NULL) {
3024f8829a4aSRandall Stewart 		/* no memory */
3025f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3026f8829a4aSRandall Stewart 		return;
3027f8829a4aSRandall Stewart 	}
3028139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3029f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3030f8829a4aSRandall Stewart 	    control,
3031cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3032f8829a4aSRandall Stewart }
3033f8829a4aSRandall Stewart 
3034f8829a4aSRandall Stewart 
3035f8829a4aSRandall Stewart 
3036f8829a4aSRandall Stewart static void
30377215cc1bSMichael Tuexen sctp_notify_adaptation_layer(struct sctp_tcb *stcb)
3038f8829a4aSRandall Stewart {
3039f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3040f8829a4aSRandall Stewart 	struct sctp_adaptation_event *sai;
3041f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3042f8829a4aSRandall Stewart 
304360990c0cSMichael Tuexen 	if ((stcb == NULL) ||
304460990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT)) {
3045f8829a4aSRandall Stewart 		/* event not enabled */
3046f8829a4aSRandall Stewart 		return;
3047830d754dSRandall Stewart 	}
3048eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 0, M_NOWAIT, 1, MT_DATA);
3049f8829a4aSRandall Stewart 	if (m_notify == NULL)
3050f8829a4aSRandall Stewart 		/* no space left */
3051f8829a4aSRandall Stewart 		return;
3052139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3053f8829a4aSRandall Stewart 	sai = mtod(m_notify, struct sctp_adaptation_event *);
3054e432298aSXin LI 	memset(sai, 0, sizeof(struct sctp_adaptation_event));
3055f8829a4aSRandall Stewart 	sai->sai_type = SCTP_ADAPTATION_INDICATION;
3056f8829a4aSRandall Stewart 	sai->sai_flags = 0;
3057f8829a4aSRandall Stewart 	sai->sai_length = sizeof(struct sctp_adaptation_event);
30582afb3e84SRandall Stewart 	sai->sai_adaptation_ind = stcb->asoc.peers_adaptation;
3059f8829a4aSRandall Stewart 	sai->sai_assoc_id = sctp_get_associd(stcb);
3060f8829a4aSRandall Stewart 
3061139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_adaptation_event);
3062139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3063f8829a4aSRandall Stewart 
3064f8829a4aSRandall Stewart 	/* append to socket */
3065f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
30667215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3067f8829a4aSRandall Stewart 	    m_notify);
3068f8829a4aSRandall Stewart 	if (control == NULL) {
3069f8829a4aSRandall Stewart 		/* no memory */
3070f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3071f8829a4aSRandall Stewart 		return;
3072f8829a4aSRandall Stewart 	}
3073139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3074139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3075f8829a4aSRandall Stewart 	/* not that we need this */
3076f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3077f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3078f8829a4aSRandall Stewart 	    control,
3079cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3080f8829a4aSRandall Stewart }
3081f8829a4aSRandall Stewart 
308203b0b021SRandall Stewart /* This always must be called with the read-queue LOCKED in the INP */
3083810ec536SMichael Tuexen static void
30842dad8a55SRandall Stewart sctp_notify_partial_delivery_indication(struct sctp_tcb *stcb, uint32_t error,
3085810ec536SMichael Tuexen     uint32_t val, int so_locked
3086810ec536SMichael Tuexen #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3087810ec536SMichael Tuexen     SCTP_UNUSED
3088810ec536SMichael Tuexen #endif
3089810ec536SMichael Tuexen )
3090f8829a4aSRandall Stewart {
3091f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3092f8829a4aSRandall Stewart 	struct sctp_pdapi_event *pdapi;
3093f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
309403b0b021SRandall Stewart 	struct sockbuf *sb;
3095f8829a4aSRandall Stewart 
309660990c0cSMichael Tuexen 	if ((stcb == NULL) ||
309760990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_PDAPIEVNT)) {
3098f8829a4aSRandall Stewart 		/* event not enabled */
3099f8829a4aSRandall Stewart 		return;
3100830d754dSRandall Stewart 	}
3101cd1386abSMichael Tuexen 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
3102cd1386abSMichael Tuexen 		return;
3103cd1386abSMichael Tuexen 	}
3104eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_pdapi_event), 0, M_NOWAIT, 1, MT_DATA);
3105f8829a4aSRandall Stewart 	if (m_notify == NULL)
3106f8829a4aSRandall Stewart 		/* no space left */
3107f8829a4aSRandall Stewart 		return;
3108139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3109f8829a4aSRandall Stewart 	pdapi = mtod(m_notify, struct sctp_pdapi_event *);
3110e432298aSXin LI 	memset(pdapi, 0, sizeof(struct sctp_pdapi_event));
3111f8829a4aSRandall Stewart 	pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
3112f8829a4aSRandall Stewart 	pdapi->pdapi_flags = 0;
3113f8829a4aSRandall Stewart 	pdapi->pdapi_length = sizeof(struct sctp_pdapi_event);
3114f8829a4aSRandall Stewart 	pdapi->pdapi_indication = error;
31159a6142d8SRandall Stewart 	pdapi->pdapi_stream = (val >> 16);
31169a6142d8SRandall Stewart 	pdapi->pdapi_seq = (val & 0x0000ffff);
3117f8829a4aSRandall Stewart 	pdapi->pdapi_assoc_id = sctp_get_associd(stcb);
3118f8829a4aSRandall Stewart 
3119139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_pdapi_event);
3120139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3121f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
31227215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3123f8829a4aSRandall Stewart 	    m_notify);
3124f8829a4aSRandall Stewart 	if (control == NULL) {
3125f8829a4aSRandall Stewart 		/* no memory */
3126f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3127f8829a4aSRandall Stewart 		return;
3128f8829a4aSRandall Stewart 	}
3129139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3130139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3131f8829a4aSRandall Stewart 	/* not that we need this */
3132f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
313303b0b021SRandall Stewart 	control->held_length = 0;
313403b0b021SRandall Stewart 	control->length = 0;
313503b0b021SRandall Stewart 	sb = &stcb->sctp_socket->so_rcv;
3136b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3137139bc87fSRandall Stewart 		sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m_notify));
313880fefe0aSRandall Stewart 	}
313903b0b021SRandall Stewart 	sctp_sballoc(stcb, sb, m_notify);
3140b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
314103b0b021SRandall Stewart 		sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
314280fefe0aSRandall Stewart 	}
3143139bc87fSRandall Stewart 	atomic_add_int(&control->length, SCTP_BUF_LEN(m_notify));
314403b0b021SRandall Stewart 	control->end_added = 1;
314503b0b021SRandall Stewart 	if (stcb->asoc.control_pdapi)
314603b0b021SRandall Stewart 		TAILQ_INSERT_AFTER(&stcb->sctp_ep->read_queue, stcb->asoc.control_pdapi, control, next);
314703b0b021SRandall Stewart 	else {
314803b0b021SRandall Stewart 		/* we really should not see this case */
314903b0b021SRandall Stewart 		TAILQ_INSERT_TAIL(&stcb->sctp_ep->read_queue, control, next);
315003b0b021SRandall Stewart 	}
315103b0b021SRandall Stewart 	if (stcb->sctp_ep && stcb->sctp_socket) {
315203b0b021SRandall Stewart 		/* This should always be the case */
3153810ec536SMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3154810ec536SMichael Tuexen 		struct socket *so;
3155810ec536SMichael Tuexen 
3156810ec536SMichael Tuexen 		so = SCTP_INP_SO(stcb->sctp_ep);
3157810ec536SMichael Tuexen 		if (!so_locked) {
3158810ec536SMichael Tuexen 			atomic_add_int(&stcb->asoc.refcnt, 1);
3159810ec536SMichael Tuexen 			SCTP_TCB_UNLOCK(stcb);
3160810ec536SMichael Tuexen 			SCTP_SOCKET_LOCK(so, 1);
3161810ec536SMichael Tuexen 			SCTP_TCB_LOCK(stcb);
3162810ec536SMichael Tuexen 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
3163810ec536SMichael Tuexen 			if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3164810ec536SMichael Tuexen 				SCTP_SOCKET_UNLOCK(so, 1);
3165810ec536SMichael Tuexen 				return;
3166810ec536SMichael Tuexen 			}
3167810ec536SMichael Tuexen 		}
3168810ec536SMichael Tuexen #endif
316903b0b021SRandall Stewart 		sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
3170810ec536SMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3171810ec536SMichael Tuexen 		if (!so_locked) {
3172810ec536SMichael Tuexen 			SCTP_SOCKET_UNLOCK(so, 1);
3173810ec536SMichael Tuexen 		}
3174810ec536SMichael Tuexen #endif
3175f8829a4aSRandall Stewart 	}
3176f8829a4aSRandall Stewart }
3177f8829a4aSRandall Stewart 
3178f8829a4aSRandall Stewart static void
3179f8829a4aSRandall Stewart sctp_notify_shutdown_event(struct sctp_tcb *stcb)
3180f8829a4aSRandall Stewart {
3181f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3182f8829a4aSRandall Stewart 	struct sctp_shutdown_event *sse;
3183f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3184f8829a4aSRandall Stewart 
3185f8829a4aSRandall Stewart 	/*
3186f8829a4aSRandall Stewart 	 * For TCP model AND UDP connected sockets we will send an error up
3187f8829a4aSRandall Stewart 	 * when an SHUTDOWN completes
3188f8829a4aSRandall Stewart 	 */
3189f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3190f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3191f8829a4aSRandall Stewart 		/* mark socket closed for read/write and wakeup! */
3192ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3193ceaad40aSRandall Stewart 		struct socket *so;
3194ceaad40aSRandall Stewart 
3195ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
3196ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
3197ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3198ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
3199ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
3200ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
3201ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3202ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
3203ceaad40aSRandall Stewart 			return;
3204ceaad40aSRandall Stewart 		}
3205ceaad40aSRandall Stewart #endif
3206f8829a4aSRandall Stewart 		socantsendmore(stcb->sctp_socket);
3207ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3208ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
3209ceaad40aSRandall Stewart #endif
3210f8829a4aSRandall Stewart 	}
3211e2e7c62eSMichael Tuexen 	if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)) {
3212f8829a4aSRandall Stewart 		/* event not enabled */
3213f8829a4aSRandall Stewart 		return;
3214830d754dSRandall Stewart 	}
3215eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_event), 0, M_NOWAIT, 1, MT_DATA);
3216f8829a4aSRandall Stewart 	if (m_notify == NULL)
3217f8829a4aSRandall Stewart 		/* no space left */
3218f8829a4aSRandall Stewart 		return;
3219f8829a4aSRandall Stewart 	sse = mtod(m_notify, struct sctp_shutdown_event *);
3220e432298aSXin LI 	memset(sse, 0, sizeof(struct sctp_shutdown_event));
3221f8829a4aSRandall Stewart 	sse->sse_type = SCTP_SHUTDOWN_EVENT;
3222f8829a4aSRandall Stewart 	sse->sse_flags = 0;
3223f8829a4aSRandall Stewart 	sse->sse_length = sizeof(struct sctp_shutdown_event);
3224f8829a4aSRandall Stewart 	sse->sse_assoc_id = sctp_get_associd(stcb);
3225f8829a4aSRandall Stewart 
3226139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_shutdown_event);
3227139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3228f8829a4aSRandall Stewart 
3229f8829a4aSRandall Stewart 	/* append to socket */
3230f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
32317215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3232f8829a4aSRandall Stewart 	    m_notify);
3233f8829a4aSRandall Stewart 	if (control == NULL) {
3234f8829a4aSRandall Stewart 		/* no memory */
3235f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3236f8829a4aSRandall Stewart 		return;
3237f8829a4aSRandall Stewart 	}
3238139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3239139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3240f8829a4aSRandall Stewart 	/* not that we need this */
3241f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3242f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3243f8829a4aSRandall Stewart 	    control,
3244cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3245f8829a4aSRandall Stewart }
3246f8829a4aSRandall Stewart 
3247f8829a4aSRandall Stewart static void
3248830d754dSRandall Stewart sctp_notify_sender_dry_event(struct sctp_tcb *stcb,
3249830d754dSRandall Stewart     int so_locked
3250830d754dSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3251830d754dSRandall Stewart     SCTP_UNUSED
3252830d754dSRandall Stewart #endif
3253830d754dSRandall Stewart )
3254830d754dSRandall Stewart {
3255830d754dSRandall Stewart 	struct mbuf *m_notify;
3256830d754dSRandall Stewart 	struct sctp_sender_dry_event *event;
3257830d754dSRandall Stewart 	struct sctp_queued_to_read *control;
3258830d754dSRandall Stewart 
325960990c0cSMichael Tuexen 	if ((stcb == NULL) ||
326060990c0cSMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DRYEVNT)) {
3261830d754dSRandall Stewart 		/* event not enabled */
3262830d754dSRandall Stewart 		return;
3263830d754dSRandall Stewart 	}
3264eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_sender_dry_event), 0, M_NOWAIT, 1, MT_DATA);
3265830d754dSRandall Stewart 	if (m_notify == NULL) {
3266830d754dSRandall Stewart 		/* no space left */
3267830d754dSRandall Stewart 		return;
3268830d754dSRandall Stewart 	}
3269830d754dSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3270830d754dSRandall Stewart 	event = mtod(m_notify, struct sctp_sender_dry_event *);
3271e432298aSXin LI 	memset(event, 0, sizeof(struct sctp_sender_dry_event));
3272830d754dSRandall Stewart 	event->sender_dry_type = SCTP_SENDER_DRY_EVENT;
3273830d754dSRandall Stewart 	event->sender_dry_flags = 0;
3274830d754dSRandall Stewart 	event->sender_dry_length = sizeof(struct sctp_sender_dry_event);
3275830d754dSRandall Stewart 	event->sender_dry_assoc_id = sctp_get_associd(stcb);
3276830d754dSRandall Stewart 
3277830d754dSRandall Stewart 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_sender_dry_event);
3278830d754dSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3279830d754dSRandall Stewart 
3280830d754dSRandall Stewart 	/* append to socket */
3281830d754dSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
32827215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
32837215cc1bSMichael Tuexen 	    m_notify);
3284830d754dSRandall Stewart 	if (control == NULL) {
3285830d754dSRandall Stewart 		/* no memory */
3286830d754dSRandall Stewart 		sctp_m_freem(m_notify);
3287830d754dSRandall Stewart 		return;
3288830d754dSRandall Stewart 	}
3289830d754dSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3290830d754dSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3291830d754dSRandall Stewart 	/* not that we need this */
3292830d754dSRandall Stewart 	control->tail_mbuf = m_notify;
3293830d754dSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3294cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
3295830d754dSRandall Stewart }
3296830d754dSRandall Stewart 
3297ea44232bSRandall Stewart 
3298c4e848b7SRandall Stewart void
3299c4e848b7SRandall Stewart sctp_notify_stream_reset_add(struct sctp_tcb *stcb, uint16_t numberin, uint16_t numberout, int flag)
3300ea44232bSRandall Stewart {
3301ea44232bSRandall Stewart 	struct mbuf *m_notify;
3302ea44232bSRandall Stewart 	struct sctp_queued_to_read *control;
3303c4e848b7SRandall Stewart 	struct sctp_stream_change_event *stradd;
3304ea44232bSRandall Stewart 
33058c501e51SMichael Tuexen 	if ((stcb == NULL) ||
33068c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_CHANGEEVNT))) {
3307ea44232bSRandall Stewart 		/* event not enabled */
3308ea44232bSRandall Stewart 		return;
3309ea44232bSRandall Stewart 	}
3310c4e848b7SRandall Stewart 	if ((stcb->asoc.peer_req_out) && flag) {
3311c4e848b7SRandall Stewart 		/* Peer made the request, don't tell the local user */
3312c4e848b7SRandall Stewart 		stcb->asoc.peer_req_out = 0;
3313c4e848b7SRandall Stewart 		return;
3314c4e848b7SRandall Stewart 	}
3315c4e848b7SRandall Stewart 	stcb->asoc.peer_req_out = 0;
3316e432298aSXin LI 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_stream_change_event), 0, M_NOWAIT, 1, MT_DATA);
3317ea44232bSRandall Stewart 	if (m_notify == NULL)
3318ea44232bSRandall Stewart 		/* no space left */
3319ea44232bSRandall Stewart 		return;
3320ea44232bSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3321c4e848b7SRandall Stewart 	stradd = mtod(m_notify, struct sctp_stream_change_event *);
3322e432298aSXin LI 	memset(stradd, 0, sizeof(struct sctp_stream_change_event));
3323c4e848b7SRandall Stewart 	stradd->strchange_type = SCTP_STREAM_CHANGE_EVENT;
3324c4e848b7SRandall Stewart 	stradd->strchange_flags = flag;
3325e432298aSXin LI 	stradd->strchange_length = sizeof(struct sctp_stream_change_event);
3326c4e848b7SRandall Stewart 	stradd->strchange_assoc_id = sctp_get_associd(stcb);
3327c4e848b7SRandall Stewart 	stradd->strchange_instrms = numberin;
3328c4e848b7SRandall Stewart 	stradd->strchange_outstrms = numberout;
3329e432298aSXin LI 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_stream_change_event);
3330ea44232bSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3331ea44232bSRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3332ea44232bSRandall Stewart 		/* no space */
3333ea44232bSRandall Stewart 		sctp_m_freem(m_notify);
3334ea44232bSRandall Stewart 		return;
3335ea44232bSRandall Stewart 	}
3336ea44232bSRandall Stewart 	/* append to socket */
3337ea44232bSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
33387215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3339ea44232bSRandall Stewart 	    m_notify);
3340ea44232bSRandall Stewart 	if (control == NULL) {
3341ea44232bSRandall Stewart 		/* no memory */
3342ea44232bSRandall Stewart 		sctp_m_freem(m_notify);
3343ea44232bSRandall Stewart 		return;
3344ea44232bSRandall Stewart 	}
3345ea44232bSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3346ea44232bSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3347ea44232bSRandall Stewart 	/* not that we need this */
3348ea44232bSRandall Stewart 	control->tail_mbuf = m_notify;
3349ea44232bSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3350ea44232bSRandall Stewart 	    control,
3351cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3352ea44232bSRandall Stewart }
3353ea44232bSRandall Stewart 
3354c4e848b7SRandall Stewart void
3355c4e848b7SRandall Stewart sctp_notify_stream_reset_tsn(struct sctp_tcb *stcb, uint32_t sending_tsn, uint32_t recv_tsn, int flag)
3356c4e848b7SRandall Stewart {
3357c4e848b7SRandall Stewart 	struct mbuf *m_notify;
3358c4e848b7SRandall Stewart 	struct sctp_queued_to_read *control;
3359c4e848b7SRandall Stewart 	struct sctp_assoc_reset_event *strasoc;
3360c4e848b7SRandall Stewart 
33618c501e51SMichael Tuexen 	if ((stcb == NULL) ||
33628c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ASSOC_RESETEVNT))) {
3363c4e848b7SRandall Stewart 		/* event not enabled */
3364c4e848b7SRandall Stewart 		return;
3365c4e848b7SRandall Stewart 	}
3366e432298aSXin LI 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_assoc_reset_event), 0, M_NOWAIT, 1, MT_DATA);
3367c4e848b7SRandall Stewart 	if (m_notify == NULL)
3368c4e848b7SRandall Stewart 		/* no space left */
3369c4e848b7SRandall Stewart 		return;
3370c4e848b7SRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3371c4e848b7SRandall Stewart 	strasoc = mtod(m_notify, struct sctp_assoc_reset_event *);
3372e432298aSXin LI 	memset(strasoc, 0, sizeof(struct sctp_assoc_reset_event));
3373c4e848b7SRandall Stewart 	strasoc->assocreset_type = SCTP_ASSOC_RESET_EVENT;
3374c4e848b7SRandall Stewart 	strasoc->assocreset_flags = flag;
3375e432298aSXin LI 	strasoc->assocreset_length = sizeof(struct sctp_assoc_reset_event);
3376c4e848b7SRandall Stewart 	strasoc->assocreset_assoc_id = sctp_get_associd(stcb);
3377c4e848b7SRandall Stewart 	strasoc->assocreset_local_tsn = sending_tsn;
3378c4e848b7SRandall Stewart 	strasoc->assocreset_remote_tsn = recv_tsn;
3379e432298aSXin LI 	SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_assoc_reset_event);
3380c4e848b7SRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3381c4e848b7SRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3382c4e848b7SRandall Stewart 		/* no space */
3383c4e848b7SRandall Stewart 		sctp_m_freem(m_notify);
3384c4e848b7SRandall Stewart 		return;
3385c4e848b7SRandall Stewart 	}
3386c4e848b7SRandall Stewart 	/* append to socket */
3387c4e848b7SRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3388c4e848b7SRandall Stewart 	    0, 0, stcb->asoc.context, 0, 0, 0,
3389c4e848b7SRandall Stewart 	    m_notify);
3390c4e848b7SRandall Stewart 	if (control == NULL) {
3391c4e848b7SRandall Stewart 		/* no memory */
3392c4e848b7SRandall Stewart 		sctp_m_freem(m_notify);
3393c4e848b7SRandall Stewart 		return;
3394c4e848b7SRandall Stewart 	}
3395c4e848b7SRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3396c4e848b7SRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3397c4e848b7SRandall Stewart 	/* not that we need this */
3398c4e848b7SRandall Stewart 	control->tail_mbuf = m_notify;
3399c4e848b7SRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3400c4e848b7SRandall Stewart 	    control,
3401c4e848b7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3402c4e848b7SRandall Stewart }
3403c4e848b7SRandall Stewart 
3404c4e848b7SRandall Stewart 
3405ea44232bSRandall Stewart 
3406830d754dSRandall Stewart static void
3407f8829a4aSRandall Stewart sctp_notify_stream_reset(struct sctp_tcb *stcb,
3408f8829a4aSRandall Stewart     int number_entries, uint16_t * list, int flag)
3409f8829a4aSRandall Stewart {
3410f8829a4aSRandall Stewart 	struct mbuf *m_notify;
3411f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
3412f8829a4aSRandall Stewart 	struct sctp_stream_reset_event *strreset;
3413f8829a4aSRandall Stewart 	int len;
3414f8829a4aSRandall Stewart 
34158c501e51SMichael Tuexen 	if ((stcb == NULL) ||
34168c501e51SMichael Tuexen 	    (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT))) {
3417f8829a4aSRandall Stewart 		/* event not enabled */
3418f8829a4aSRandall Stewart 		return;
3419830d754dSRandall Stewart 	}
3420eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
3421f8829a4aSRandall Stewart 	if (m_notify == NULL)
3422f8829a4aSRandall Stewart 		/* no space left */
3423f8829a4aSRandall Stewart 		return;
3424139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = 0;
3425f8829a4aSRandall Stewart 	len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
3426f8829a4aSRandall Stewart 	if (len > M_TRAILINGSPACE(m_notify)) {
3427f8829a4aSRandall Stewart 		/* never enough room */
3428f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3429f8829a4aSRandall Stewart 		return;
3430f8829a4aSRandall Stewart 	}
3431f8829a4aSRandall Stewart 	strreset = mtod(m_notify, struct sctp_stream_reset_event *);
3432e432298aSXin LI 	memset(strreset, 0, len);
3433f8829a4aSRandall Stewart 	strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
3434c4e848b7SRandall Stewart 	strreset->strreset_flags = flag;
3435f8829a4aSRandall Stewart 	strreset->strreset_length = len;
3436f8829a4aSRandall Stewart 	strreset->strreset_assoc_id = sctp_get_associd(stcb);
3437f8829a4aSRandall Stewart 	if (number_entries) {
3438f8829a4aSRandall Stewart 		int i;
3439f8829a4aSRandall Stewart 
3440f8829a4aSRandall Stewart 		for (i = 0; i < number_entries; i++) {
3441c4e848b7SRandall Stewart 			strreset->strreset_stream_list[i] = ntohs(list[i]);
3442f8829a4aSRandall Stewart 		}
3443f8829a4aSRandall Stewart 	}
3444139bc87fSRandall Stewart 	SCTP_BUF_LEN(m_notify) = len;
3445139bc87fSRandall Stewart 	SCTP_BUF_NEXT(m_notify) = NULL;
3446139bc87fSRandall Stewart 	if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3447f8829a4aSRandall Stewart 		/* no space */
3448f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3449f8829a4aSRandall Stewart 		return;
3450f8829a4aSRandall Stewart 	}
3451f8829a4aSRandall Stewart 	/* append to socket */
3452f8829a4aSRandall Stewart 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
34537215cc1bSMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3454f8829a4aSRandall Stewart 	    m_notify);
3455f8829a4aSRandall Stewart 	if (control == NULL) {
3456f8829a4aSRandall Stewart 		/* no memory */
3457f8829a4aSRandall Stewart 		sctp_m_freem(m_notify);
3458f8829a4aSRandall Stewart 		return;
3459f8829a4aSRandall Stewart 	}
3460139bc87fSRandall Stewart 	control->spec_flags = M_NOTIFICATION;
3461139bc87fSRandall Stewart 	control->length = SCTP_BUF_LEN(m_notify);
3462f8829a4aSRandall Stewart 	/* not that we need this */
3463f8829a4aSRandall Stewart 	control->tail_mbuf = m_notify;
3464f8829a4aSRandall Stewart 	sctp_add_to_readq(stcb->sctp_ep, stcb,
3465f8829a4aSRandall Stewart 	    control,
3466cfde3ff7SRandall Stewart 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3467f8829a4aSRandall Stewart }
3468f8829a4aSRandall Stewart 
3469f8829a4aSRandall Stewart 
3470389b1b11SMichael Tuexen static void
3471389b1b11SMichael Tuexen sctp_notify_remote_error(struct sctp_tcb *stcb, uint16_t error, struct sctp_error_chunk *chunk)
3472389b1b11SMichael Tuexen {
3473389b1b11SMichael Tuexen 	struct mbuf *m_notify;
3474389b1b11SMichael Tuexen 	struct sctp_remote_error *sre;
3475389b1b11SMichael Tuexen 	struct sctp_queued_to_read *control;
3476389b1b11SMichael Tuexen 	size_t notif_len, chunk_len;
3477389b1b11SMichael Tuexen 
3478389b1b11SMichael Tuexen 	if ((stcb == NULL) ||
3479389b1b11SMichael Tuexen 	    sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPEERERR)) {
3480389b1b11SMichael Tuexen 		return;
3481389b1b11SMichael Tuexen 	}
3482389b1b11SMichael Tuexen 	if (chunk != NULL) {
3483c9eb4473SMichael Tuexen 		chunk_len = ntohs(chunk->ch.chunk_length);
3484389b1b11SMichael Tuexen 	} else {
3485389b1b11SMichael Tuexen 		chunk_len = 0;
3486389b1b11SMichael Tuexen 	}
3487389b1b11SMichael Tuexen 	notif_len = sizeof(struct sctp_remote_error) + chunk_len;
3488eb1b1807SGleb Smirnoff 	m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3489389b1b11SMichael Tuexen 	if (m_notify == NULL) {
3490389b1b11SMichael Tuexen 		/* Retry with smaller value. */
3491389b1b11SMichael Tuexen 		notif_len = sizeof(struct sctp_remote_error);
3492eb1b1807SGleb Smirnoff 		m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3493389b1b11SMichael Tuexen 		if (m_notify == NULL) {
3494389b1b11SMichael Tuexen 			return;
3495389b1b11SMichael Tuexen 		}
3496389b1b11SMichael Tuexen 	}
3497389b1b11SMichael Tuexen 	SCTP_BUF_NEXT(m_notify) = NULL;
3498389b1b11SMichael Tuexen 	sre = mtod(m_notify, struct sctp_remote_error *);
349956711f94SMichael Tuexen 	memset(sre, 0, notif_len);
3500389b1b11SMichael Tuexen 	sre->sre_type = SCTP_REMOTE_ERROR;
3501389b1b11SMichael Tuexen 	sre->sre_flags = 0;
3502389b1b11SMichael Tuexen 	sre->sre_length = sizeof(struct sctp_remote_error);
3503389b1b11SMichael Tuexen 	sre->sre_error = error;
3504389b1b11SMichael Tuexen 	sre->sre_assoc_id = sctp_get_associd(stcb);
3505389b1b11SMichael Tuexen 	if (notif_len > sizeof(struct sctp_remote_error)) {
3506389b1b11SMichael Tuexen 		memcpy(sre->sre_data, chunk, chunk_len);
3507389b1b11SMichael Tuexen 		sre->sre_length += chunk_len;
3508389b1b11SMichael Tuexen 	}
3509389b1b11SMichael Tuexen 	SCTP_BUF_LEN(m_notify) = sre->sre_length;
3510389b1b11SMichael Tuexen 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3511389b1b11SMichael Tuexen 	    0, 0, stcb->asoc.context, 0, 0, 0,
3512389b1b11SMichael Tuexen 	    m_notify);
3513389b1b11SMichael Tuexen 	if (control != NULL) {
3514389b1b11SMichael Tuexen 		control->length = SCTP_BUF_LEN(m_notify);
3515389b1b11SMichael Tuexen 		/* not that we need this */
3516389b1b11SMichael Tuexen 		control->tail_mbuf = m_notify;
3517389b1b11SMichael Tuexen 		control->spec_flags = M_NOTIFICATION;
3518389b1b11SMichael Tuexen 		sctp_add_to_readq(stcb->sctp_ep, stcb,
3519389b1b11SMichael Tuexen 		    control,
3520389b1b11SMichael Tuexen 		    &stcb->sctp_socket->so_rcv, 1,
3521389b1b11SMichael Tuexen 		    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
3522389b1b11SMichael Tuexen 	} else {
3523389b1b11SMichael Tuexen 		sctp_m_freem(m_notify);
3524389b1b11SMichael Tuexen 	}
3525389b1b11SMichael Tuexen }
3526389b1b11SMichael Tuexen 
3527389b1b11SMichael Tuexen 
3528f8829a4aSRandall Stewart void
3529f8829a4aSRandall Stewart sctp_ulp_notify(uint32_t notification, struct sctp_tcb *stcb,
3530ceaad40aSRandall Stewart     uint32_t error, void *data, int so_locked
3531ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3532ceaad40aSRandall Stewart     SCTP_UNUSED
3533ceaad40aSRandall Stewart #endif
3534ceaad40aSRandall Stewart )
3535f8829a4aSRandall Stewart {
3536830d754dSRandall Stewart 	if ((stcb == NULL) ||
3537830d754dSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3538f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3539830d754dSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3540830d754dSRandall Stewart 		/* If the socket is gone we are out of here */
3541f8829a4aSRandall Stewart 		return;
3542f8829a4aSRandall Stewart 	}
3543a99b6783SRandall Stewart 	if (stcb->sctp_socket->so_rcv.sb_state & SBS_CANTRCVMORE) {
3544a99b6783SRandall Stewart 		return;
3545a99b6783SRandall Stewart 	}
3546fb4a67d2SMichael Tuexen 	if ((stcb->asoc.state & SCTP_STATE_COOKIE_WAIT) ||
3547fb4a67d2SMichael Tuexen 	    (stcb->asoc.state & SCTP_STATE_COOKIE_ECHOED)) {
354817205eccSRandall Stewart 		if ((notification == SCTP_NOTIFY_INTERFACE_DOWN) ||
354917205eccSRandall Stewart 		    (notification == SCTP_NOTIFY_INTERFACE_UP) ||
355017205eccSRandall Stewart 		    (notification == SCTP_NOTIFY_INTERFACE_CONFIRMED)) {
355117205eccSRandall Stewart 			/* Don't report these in front states */
355217205eccSRandall Stewart 			return;
355317205eccSRandall Stewart 		}
355417205eccSRandall Stewart 	}
3555f8829a4aSRandall Stewart 	switch (notification) {
3556f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_UP:
3557f8829a4aSRandall Stewart 		if (stcb->asoc.assoc_up_sent == 0) {
3558410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_UP, stcb, error, NULL, 0, so_locked);
3559f8829a4aSRandall Stewart 			stcb->asoc.assoc_up_sent = 1;
3560f8829a4aSRandall Stewart 		}
35612afb3e84SRandall Stewart 		if (stcb->asoc.adaptation_needed && (stcb->asoc.adaptation_sent == 0)) {
35627215cc1bSMichael Tuexen 			sctp_notify_adaptation_layer(stcb);
35632afb3e84SRandall Stewart 		}
3564c79bec9cSMichael Tuexen 		if (stcb->asoc.auth_supported == 0) {
3565830d754dSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3566830d754dSRandall Stewart 			    NULL, so_locked);
3567830d754dSRandall Stewart 		}
3568f8829a4aSRandall Stewart 		break;
3569f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_DOWN:
3570410a3b1eSMichael Tuexen 		sctp_notify_assoc_change(SCTP_SHUTDOWN_COMP, stcb, error, NULL, 0, so_locked);
3571f8829a4aSRandall Stewart 		break;
3572f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_DOWN:
3573f8829a4aSRandall Stewart 		{
3574f8829a4aSRandall Stewart 			struct sctp_nets *net;
3575f8829a4aSRandall Stewart 
3576f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
3577f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_UNREACHABLE,
3578f8829a4aSRandall Stewart 			    (struct sockaddr *)&net->ro._l_addr, error);
3579f8829a4aSRandall Stewart 			break;
3580f8829a4aSRandall Stewart 		}
3581f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_UP:
3582f8829a4aSRandall Stewart 		{
3583f8829a4aSRandall Stewart 			struct sctp_nets *net;
3584f8829a4aSRandall Stewart 
3585f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
3586f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_AVAILABLE,
3587f8829a4aSRandall Stewart 			    (struct sockaddr *)&net->ro._l_addr, error);
3588f8829a4aSRandall Stewart 			break;
3589f8829a4aSRandall Stewart 		}
3590f8829a4aSRandall Stewart 	case SCTP_NOTIFY_INTERFACE_CONFIRMED:
3591f8829a4aSRandall Stewart 		{
3592f8829a4aSRandall Stewart 			struct sctp_nets *net;
3593f8829a4aSRandall Stewart 
3594f8829a4aSRandall Stewart 			net = (struct sctp_nets *)data;
3595f8829a4aSRandall Stewart 			sctp_notify_peer_addr_change(stcb, SCTP_ADDR_CONFIRMED,
3596f8829a4aSRandall Stewart 			    (struct sockaddr *)&net->ro._l_addr, error);
3597f8829a4aSRandall Stewart 			break;
3598f8829a4aSRandall Stewart 		}
3599f8829a4aSRandall Stewart 	case SCTP_NOTIFY_SPECIAL_SP_FAIL:
3600f8829a4aSRandall Stewart 		sctp_notify_send_failed2(stcb, error,
3601ceaad40aSRandall Stewart 		    (struct sctp_stream_queue_pending *)data, so_locked);
3602f8829a4aSRandall Stewart 		break;
36031edc9dbaSMichael Tuexen 	case SCTP_NOTIFY_SENT_DG_FAIL:
36041edc9dbaSMichael Tuexen 		sctp_notify_send_failed(stcb, 1, error,
36051edc9dbaSMichael Tuexen 		    (struct sctp_tmit_chunk *)data, so_locked);
36061edc9dbaSMichael Tuexen 		break;
36071edc9dbaSMichael Tuexen 	case SCTP_NOTIFY_UNSENT_DG_FAIL:
36081edc9dbaSMichael Tuexen 		sctp_notify_send_failed(stcb, 0, error,
3609ceaad40aSRandall Stewart 		    (struct sctp_tmit_chunk *)data, so_locked);
3610f8829a4aSRandall Stewart 		break;
3611f8829a4aSRandall Stewart 	case SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION:
36129a6142d8SRandall Stewart 		{
36139a6142d8SRandall Stewart 			uint32_t val;
36149a6142d8SRandall Stewart 
36159a6142d8SRandall Stewart 			val = *((uint32_t *) data);
36169a6142d8SRandall Stewart 
3617810ec536SMichael Tuexen 			sctp_notify_partial_delivery_indication(stcb, error, val, so_locked);
3618f8829a4aSRandall Stewart 			break;
3619810ec536SMichael Tuexen 		}
3620410a3b1eSMichael Tuexen 	case SCTP_NOTIFY_ASSOC_LOC_ABORTED:
3621fb4a67d2SMichael Tuexen 		if (((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_WAIT) ||
3622fb4a67d2SMichael Tuexen 		    ((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_ECHOED)) {
3623410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 0, so_locked);
3624c105859eSRandall Stewart 		} else {
3625410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 0, so_locked);
3626410a3b1eSMichael Tuexen 		}
3627410a3b1eSMichael Tuexen 		break;
3628410a3b1eSMichael Tuexen 	case SCTP_NOTIFY_ASSOC_REM_ABORTED:
3629fb4a67d2SMichael Tuexen 		if (((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_WAIT) ||
3630fb4a67d2SMichael Tuexen 		    ((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_ECHOED)) {
3631410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 1, so_locked);
3632410a3b1eSMichael Tuexen 		} else {
3633410a3b1eSMichael Tuexen 			sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 1, so_locked);
3634c105859eSRandall Stewart 		}
3635f8829a4aSRandall Stewart 		break;
3636f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASSOC_RESTART:
3637410a3b1eSMichael Tuexen 		sctp_notify_assoc_change(SCTP_RESTART, stcb, error, NULL, 0, so_locked);
3638c79bec9cSMichael Tuexen 		if (stcb->asoc.auth_supported == 0) {
3639830d754dSRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
3640830d754dSRandall Stewart 			    NULL, so_locked);
3641830d754dSRandall Stewart 		}
3642f8829a4aSRandall Stewart 		break;
3643f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_SEND:
3644d7714577SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STREAM_RESET_OUTGOING_SSN);
3645f8829a4aSRandall Stewart 		break;
3646f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_RECV:
3647d7714577SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STREAM_RESET_INCOMING);
3648f8829a4aSRandall Stewart 		break;
3649f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_FAILED_OUT:
3650c4e848b7SRandall Stewart 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
3651d7714577SMichael Tuexen 		    (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_FAILED));
3652f8829a4aSRandall Stewart 		break;
3653d4260646SMichael Tuexen 	case SCTP_NOTIFY_STR_RESET_DENIED_OUT:
3654d4260646SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
3655d4260646SMichael Tuexen 		    (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_DENIED));
3656d4260646SMichael Tuexen 		break;
3657f8829a4aSRandall Stewart 	case SCTP_NOTIFY_STR_RESET_FAILED_IN:
3658c4e848b7SRandall Stewart 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
3659d7714577SMichael Tuexen 		    (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_FAILED));
3660f8829a4aSRandall Stewart 		break;
3661d4260646SMichael Tuexen 	case SCTP_NOTIFY_STR_RESET_DENIED_IN:
3662d4260646SMichael Tuexen 		sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
3663d4260646SMichael Tuexen 		    (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_DENIED));
3664d4260646SMichael Tuexen 		break;
3665f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_ADD_IP:
3666f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_ADDED, data,
3667f8829a4aSRandall Stewart 		    error);
3668f8829a4aSRandall Stewart 		break;
3669f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_DELETE_IP:
3670f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_REMOVED, data,
3671f8829a4aSRandall Stewart 		    error);
3672f8829a4aSRandall Stewart 		break;
3673f8829a4aSRandall Stewart 	case SCTP_NOTIFY_ASCONF_SET_PRIMARY:
3674f8829a4aSRandall Stewart 		sctp_notify_peer_addr_change(stcb, SCTP_ADDR_MADE_PRIM, data,
3675f8829a4aSRandall Stewart 		    error);
3676f8829a4aSRandall Stewart 		break;
3677f8829a4aSRandall Stewart 	case SCTP_NOTIFY_PEER_SHUTDOWN:
3678f8829a4aSRandall Stewart 		sctp_notify_shutdown_event(stcb);
3679f8829a4aSRandall Stewart 		break;
3680f8829a4aSRandall Stewart 	case SCTP_NOTIFY_AUTH_NEW_KEY:
368178f28045SMichael Tuexen 		sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY, error,
3682830d754dSRandall Stewart 		    (uint16_t) (uintptr_t) data,
3683830d754dSRandall Stewart 		    so_locked);
3684f8829a4aSRandall Stewart 		break;
3685830d754dSRandall Stewart 	case SCTP_NOTIFY_AUTH_FREE_KEY:
3686830d754dSRandall Stewart 		sctp_notify_authentication(stcb, SCTP_AUTH_FREE_KEY, error,
3687830d754dSRandall Stewart 		    (uint16_t) (uintptr_t) data,
3688830d754dSRandall Stewart 		    so_locked);
3689f8829a4aSRandall Stewart 		break;
3690830d754dSRandall Stewart 	case SCTP_NOTIFY_NO_PEER_AUTH:
3691830d754dSRandall Stewart 		sctp_notify_authentication(stcb, SCTP_AUTH_NO_AUTH, error,
3692830d754dSRandall Stewart 		    (uint16_t) (uintptr_t) data,
3693830d754dSRandall Stewart 		    so_locked);
3694830d754dSRandall Stewart 		break;
3695830d754dSRandall Stewart 	case SCTP_NOTIFY_SENDER_DRY:
3696830d754dSRandall Stewart 		sctp_notify_sender_dry_event(stcb, so_locked);
3697830d754dSRandall Stewart 		break;
3698389b1b11SMichael Tuexen 	case SCTP_NOTIFY_REMOTE_ERROR:
3699389b1b11SMichael Tuexen 		sctp_notify_remote_error(stcb, error, data);
3700389b1b11SMichael Tuexen 		break;
3701f8829a4aSRandall Stewart 	default:
3702ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_UTIL1, "%s: unknown notification %xh (%u)\n",
3703ad81507eSRandall Stewart 		    __FUNCTION__, notification, notification);
3704f8829a4aSRandall Stewart 		break;
3705f8829a4aSRandall Stewart 	}			/* end switch */
3706f8829a4aSRandall Stewart }
3707f8829a4aSRandall Stewart 
3708f8829a4aSRandall Stewart void
37091edc9dbaSMichael Tuexen sctp_report_all_outbound(struct sctp_tcb *stcb, uint16_t error, int holds_lock, int so_locked
3710ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3711ceaad40aSRandall Stewart     SCTP_UNUSED
3712ceaad40aSRandall Stewart #endif
3713ceaad40aSRandall Stewart )
3714f8829a4aSRandall Stewart {
3715f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3716f8829a4aSRandall Stewart 	struct sctp_stream_out *outs;
37174a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
37184a9ef3f8SMichael Tuexen 	struct sctp_stream_queue_pending *sp, *nsp;
37197f34832bSRandall Stewart 	int i;
3720f8829a4aSRandall Stewart 
3721ad81507eSRandall Stewart 	if (stcb == NULL) {
3722ad81507eSRandall Stewart 		return;
3723ad81507eSRandall Stewart 	}
37244a9ef3f8SMichael Tuexen 	asoc = &stcb->asoc;
37254a9ef3f8SMichael Tuexen 	if (asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3726478fbccbSRandall Stewart 		/* already being freed */
3727478fbccbSRandall Stewart 		return;
3728478fbccbSRandall Stewart 	}
3729f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3730f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
37314a9ef3f8SMichael Tuexen 	    (asoc->state & SCTP_STATE_CLOSED_SOCKET)) {
3732f8829a4aSRandall Stewart 		return;
3733f8829a4aSRandall Stewart 	}
3734f8829a4aSRandall Stewart 	/* now through all the gunk freeing chunks */
3735ad81507eSRandall Stewart 	if (holds_lock == 0) {
37367f34832bSRandall Stewart 		SCTP_TCB_SEND_LOCK(stcb);
3737ad81507eSRandall Stewart 	}
3738d00aff5dSRandall Stewart 	/* sent queue SHOULD be empty */
37394a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
3740d00aff5dSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3741d00aff5dSRandall Stewart 		asoc->sent_queue_cnt--;
3742325c8c46SMichael Tuexen 		if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
3743a7ad6026SMichael Tuexen 			if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) {
3744a7ad6026SMichael Tuexen 				asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--;
3745a7ad6026SMichael Tuexen #ifdef INVARIANTS
3746a7ad6026SMichael Tuexen 			} else {
3747a7ad6026SMichael Tuexen 				panic("No chunks on the queues for sid %u.", chk->rec.data.stream_number);
3748a7ad6026SMichael Tuexen #endif
3749a7ad6026SMichael Tuexen 			}
3750a7ad6026SMichael Tuexen 		}
37510c0982b8SRandall Stewart 		if (chk->data != NULL) {
3752d00aff5dSRandall Stewart 			sctp_free_bufspace(stcb, asoc, chk, 1);
37531edc9dbaSMichael Tuexen 			sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
37541edc9dbaSMichael Tuexen 			    error, chk, so_locked);
3755810ec536SMichael Tuexen 			if (chk->data) {
3756d00aff5dSRandall Stewart 				sctp_m_freem(chk->data);
3757d00aff5dSRandall Stewart 				chk->data = NULL;
3758d00aff5dSRandall Stewart 			}
3759810ec536SMichael Tuexen 		}
3760689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, so_locked);
3761d00aff5dSRandall Stewart 		/* sa_ignore FREED_MEMORY */
3762d00aff5dSRandall Stewart 	}
3763d00aff5dSRandall Stewart 	/* pending send queue SHOULD be empty */
37644a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
3765d00aff5dSRandall Stewart 		TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3766d00aff5dSRandall Stewart 		asoc->send_queue_cnt--;
3767a7ad6026SMichael Tuexen 		if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) {
3768a7ad6026SMichael Tuexen 			asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--;
3769a7ad6026SMichael Tuexen #ifdef INVARIANTS
3770a7ad6026SMichael Tuexen 		} else {
3771a7ad6026SMichael Tuexen 			panic("No chunks on the queues for sid %u.", chk->rec.data.stream_number);
3772a7ad6026SMichael Tuexen #endif
3773a7ad6026SMichael Tuexen 		}
37740c0982b8SRandall Stewart 		if (chk->data != NULL) {
3775d00aff5dSRandall Stewart 			sctp_free_bufspace(stcb, asoc, chk, 1);
37761edc9dbaSMichael Tuexen 			sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
37771edc9dbaSMichael Tuexen 			    error, chk, so_locked);
3778810ec536SMichael Tuexen 			if (chk->data) {
3779d00aff5dSRandall Stewart 				sctp_m_freem(chk->data);
3780d00aff5dSRandall Stewart 				chk->data = NULL;
3781d00aff5dSRandall Stewart 			}
3782810ec536SMichael Tuexen 		}
3783689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, so_locked);
3784d00aff5dSRandall Stewart 		/* sa_ignore FREED_MEMORY */
3785d00aff5dSRandall Stewart 	}
37864a9ef3f8SMichael Tuexen 	for (i = 0; i < asoc->streamoutcnt; i++) {
37877f34832bSRandall Stewart 		/* For each stream */
37884a9ef3f8SMichael Tuexen 		outs = &asoc->strmout[i];
37897f34832bSRandall Stewart 		/* clean up any sends there */
37904a9ef3f8SMichael Tuexen 		asoc->locked_on_sending = NULL;
37914a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
37924a9ef3f8SMichael Tuexen 			asoc->stream_queue_cnt--;
3793f8829a4aSRandall Stewart 			TAILQ_REMOVE(&outs->outqueue, sp, next);
3794f8829a4aSRandall Stewart 			sctp_free_spbufspace(stcb, asoc, sp);
3795478fbccbSRandall Stewart 			if (sp->data) {
3796f8829a4aSRandall Stewart 				sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
37971edc9dbaSMichael Tuexen 				    error, (void *)sp, so_locked);
3798f8829a4aSRandall Stewart 				if (sp->data) {
3799f8829a4aSRandall Stewart 					sctp_m_freem(sp->data);
3800f8829a4aSRandall Stewart 					sp->data = NULL;
3801d07b2ac6SMichael Tuexen 					sp->tail_mbuf = NULL;
3802d07b2ac6SMichael Tuexen 					sp->length = 0;
3803f8829a4aSRandall Stewart 				}
3804478fbccbSRandall Stewart 			}
38059eea4a2dSMichael Tuexen 			if (sp->net) {
3806f8829a4aSRandall Stewart 				sctp_free_remote_addr(sp->net);
3807f8829a4aSRandall Stewart 				sp->net = NULL;
38089eea4a2dSMichael Tuexen 			}
3809f8829a4aSRandall Stewart 			/* Free the chunk */
3810689e6a5fSMichael Tuexen 			sctp_free_a_strmoq(stcb, sp, so_locked);
38113c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
3812f8829a4aSRandall Stewart 		}
3813f8829a4aSRandall Stewart 	}
3814f8829a4aSRandall Stewart 
3815ad81507eSRandall Stewart 	if (holds_lock == 0) {
38167f34832bSRandall Stewart 		SCTP_TCB_SEND_UNLOCK(stcb);
3817f8829a4aSRandall Stewart 	}
3818ad81507eSRandall Stewart }
3819f8829a4aSRandall Stewart 
3820f8829a4aSRandall Stewart void
3821410a3b1eSMichael Tuexen sctp_abort_notification(struct sctp_tcb *stcb, uint8_t from_peer, uint16_t error,
3822a2b42326SMichael Tuexen     struct sctp_abort_chunk *abort, int so_locked
3823ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3824ceaad40aSRandall Stewart     SCTP_UNUSED
3825ceaad40aSRandall Stewart #endif
3826ceaad40aSRandall Stewart )
3827f8829a4aSRandall Stewart {
3828ad81507eSRandall Stewart 	if (stcb == NULL) {
3829ad81507eSRandall Stewart 		return;
3830ad81507eSRandall Stewart 	}
3831c55b70ceSMichael Tuexen 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3832c55b70ceSMichael Tuexen 	    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3833c55b70ceSMichael Tuexen 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
3834c55b70ceSMichael Tuexen 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_WAS_ABORTED;
3835c55b70ceSMichael Tuexen 	}
3836f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3837f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3838f8829a4aSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
3839f8829a4aSRandall Stewart 		return;
3840f8829a4aSRandall Stewart 	}
3841f8829a4aSRandall Stewart 	/* Tell them we lost the asoc */
38421edc9dbaSMichael Tuexen 	sctp_report_all_outbound(stcb, error, 1, so_locked);
3843410a3b1eSMichael Tuexen 	if (from_peer) {
3844410a3b1eSMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_REM_ABORTED, stcb, error, abort, so_locked);
3845410a3b1eSMichael Tuexen 	} else {
3846410a3b1eSMichael Tuexen 		sctp_ulp_notify(SCTP_NOTIFY_ASSOC_LOC_ABORTED, stcb, error, abort, so_locked);
3847410a3b1eSMichael Tuexen 	}
3848f8829a4aSRandall Stewart }
3849f8829a4aSRandall Stewart 
3850f8829a4aSRandall Stewart void
3851f8829a4aSRandall Stewart sctp_abort_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3852b1754ad1SMichael Tuexen     struct mbuf *m, int iphlen,
3853b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
3854b1754ad1SMichael Tuexen     struct sctphdr *sh, struct mbuf *op_err,
3855f30ac432SMichael Tuexen     uint8_t use_mflowid, uint32_t mflowid,
3856c54a18d2SRandall Stewart     uint32_t vrf_id, uint16_t port)
3857f8829a4aSRandall Stewart {
3858f8829a4aSRandall Stewart 	uint32_t vtag;
3859f8829a4aSRandall Stewart 
3860ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3861ceaad40aSRandall Stewart 	struct socket *so;
3862ceaad40aSRandall Stewart 
3863ceaad40aSRandall Stewart #endif
3864ceaad40aSRandall Stewart 
3865f8829a4aSRandall Stewart 	vtag = 0;
3866f8829a4aSRandall Stewart 	if (stcb != NULL) {
3867f8829a4aSRandall Stewart 		/* We have a TCB to abort, send notification too */
3868f8829a4aSRandall Stewart 		vtag = stcb->asoc.peer_vtag;
3869410a3b1eSMichael Tuexen 		sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
387017205eccSRandall Stewart 		/* get the assoc vrf id and table id */
387117205eccSRandall Stewart 		vrf_id = stcb->asoc.vrf_id;
387263981c2bSRandall Stewart 		stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
3873f8829a4aSRandall Stewart 	}
3874b1754ad1SMichael Tuexen 	sctp_send_abort(m, iphlen, src, dst, sh, vtag, op_err,
3875f30ac432SMichael Tuexen 	    use_mflowid, mflowid,
3876f30ac432SMichael Tuexen 	    vrf_id, port);
3877f8829a4aSRandall Stewart 	if (stcb != NULL) {
3878f8829a4aSRandall Stewart 		/* Ok, now lets free it */
3879ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3880ceaad40aSRandall Stewart 		so = SCTP_INP_SO(inp);
3881ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
3882ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3883ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
3884ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
3885ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
3886ceaad40aSRandall Stewart #endif
38870271d0cdSMichael Tuexen 		SCTP_STAT_INCR_COUNTER32(sctps_aborted);
38880271d0cdSMichael Tuexen 		if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
38890271d0cdSMichael Tuexen 		    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
38900271d0cdSMichael Tuexen 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
38910271d0cdSMichael Tuexen 		}
3892c4739e2fSRandall Stewart 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_4);
3893ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3894ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
3895ceaad40aSRandall Stewart #endif
3896f8829a4aSRandall Stewart 	}
3897f8829a4aSRandall Stewart }
3898f8829a4aSRandall Stewart 
3899f1f73e57SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
3900f1f73e57SRandall Stewart void
3901f1f73e57SRandall Stewart sctp_print_out_track_log(struct sctp_tcb *stcb)
3902f1f73e57SRandall Stewart {
390318e198d3SRandall Stewart #ifdef NOSIY_PRINTS
3904f1f73e57SRandall Stewart 	int i;
3905f1f73e57SRandall Stewart 
3906ad81507eSRandall Stewart 	SCTP_PRINTF("Last ep reason:%x\n", stcb->sctp_ep->last_abort_code);
3907ad81507eSRandall Stewart 	SCTP_PRINTF("IN bound TSN log-aaa\n");
3908f1f73e57SRandall Stewart 	if ((stcb->asoc.tsn_in_at == 0) && (stcb->asoc.tsn_in_wrapped == 0)) {
3909ad81507eSRandall Stewart 		SCTP_PRINTF("None rcvd\n");
3910f1f73e57SRandall Stewart 		goto none_in;
3911f1f73e57SRandall Stewart 	}
3912f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_in_wrapped) {
3913f1f73e57SRandall Stewart 		for (i = stcb->asoc.tsn_in_at; i < SCTP_TSN_LOG_SIZE; i++) {
3914ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3915f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].tsn,
3916f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].strm,
3917f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].seq,
3918f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].flgs,
3919f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].sz);
3920f1f73e57SRandall Stewart 		}
3921f1f73e57SRandall Stewart 	}
3922f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_in_at) {
3923f1f73e57SRandall Stewart 		for (i = 0; i < stcb->asoc.tsn_in_at; i++) {
3924ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3925f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].tsn,
3926f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].strm,
3927f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].seq,
3928f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].flgs,
3929f1f73e57SRandall Stewart 			    stcb->asoc.in_tsnlog[i].sz);
3930f1f73e57SRandall Stewart 		}
3931f1f73e57SRandall Stewart 	}
3932f1f73e57SRandall Stewart none_in:
3933ad81507eSRandall Stewart 	SCTP_PRINTF("OUT bound TSN log-aaa\n");
3934ad81507eSRandall Stewart 	if ((stcb->asoc.tsn_out_at == 0) &&
3935ad81507eSRandall Stewart 	    (stcb->asoc.tsn_out_wrapped == 0)) {
3936ad81507eSRandall Stewart 		SCTP_PRINTF("None sent\n");
3937f1f73e57SRandall Stewart 	}
3938f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_out_wrapped) {
3939f1f73e57SRandall Stewart 		for (i = stcb->asoc.tsn_out_at; i < SCTP_TSN_LOG_SIZE; i++) {
3940ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3941f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].tsn,
3942f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].strm,
3943f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].seq,
3944f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].flgs,
3945f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].sz);
3946f1f73e57SRandall Stewart 		}
3947f1f73e57SRandall Stewart 	}
3948f1f73e57SRandall Stewart 	if (stcb->asoc.tsn_out_at) {
3949f1f73e57SRandall Stewart 		for (i = 0; i < stcb->asoc.tsn_out_at; i++) {
3950ad81507eSRandall Stewart 			SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
3951f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].tsn,
3952f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].strm,
3953f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].seq,
3954f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].flgs,
3955f1f73e57SRandall Stewart 			    stcb->asoc.out_tsnlog[i].sz);
3956f1f73e57SRandall Stewart 		}
3957f1f73e57SRandall Stewart 	}
395818e198d3SRandall Stewart #endif
3959f1f73e57SRandall Stewart }
3960f1f73e57SRandall Stewart 
3961f1f73e57SRandall Stewart #endif
3962f1f73e57SRandall Stewart 
3963f8829a4aSRandall Stewart void
3964f8829a4aSRandall Stewart sctp_abort_an_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3965a2b42326SMichael Tuexen     struct mbuf *op_err,
3966ceaad40aSRandall Stewart     int so_locked
3967ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3968ceaad40aSRandall Stewart     SCTP_UNUSED
3969ceaad40aSRandall Stewart #endif
3970ceaad40aSRandall Stewart )
3971f8829a4aSRandall Stewart {
3972ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3973ceaad40aSRandall Stewart 	struct socket *so;
3974ceaad40aSRandall Stewart 
3975ceaad40aSRandall Stewart #endif
3976ceaad40aSRandall Stewart 
3977ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3978ceaad40aSRandall Stewart 	so = SCTP_INP_SO(inp);
3979ceaad40aSRandall Stewart #endif
3980f8829a4aSRandall Stewart 	if (stcb == NULL) {
3981f8829a4aSRandall Stewart 		/* Got to have a TCB */
3982f8829a4aSRandall Stewart 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3983fe1831e0SMichael Tuexen 			if (LIST_EMPTY(&inp->sctp_asoc_list)) {
3984b0552ae2SRandall Stewart 				sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
3985b0552ae2SRandall Stewart 				    SCTP_CALLED_DIRECTLY_NOCMPSET);
3986f8829a4aSRandall Stewart 			}
3987f8829a4aSRandall Stewart 		}
3988f8829a4aSRandall Stewart 		return;
398963981c2bSRandall Stewart 	} else {
399063981c2bSRandall Stewart 		stcb->asoc.state |= SCTP_STATE_WAS_ABORTED;
3991f8829a4aSRandall Stewart 	}
3992f8829a4aSRandall Stewart 	/* notify the ulp */
3993a2b42326SMichael Tuexen 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
3994410a3b1eSMichael Tuexen 		sctp_abort_notification(stcb, 0, 0, NULL, so_locked);
3995a2b42326SMichael Tuexen 	}
3996f8829a4aSRandall Stewart 	/* notify the peer */
3997ceaad40aSRandall Stewart 	sctp_send_abort_tcb(stcb, op_err, so_locked);
3998f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3999f8829a4aSRandall Stewart 	if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
4000f8829a4aSRandall Stewart 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4001f8829a4aSRandall Stewart 		SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4002f8829a4aSRandall Stewart 	}
4003f8829a4aSRandall Stewart 	/* now free the asoc */
4004f1f73e57SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
4005f1f73e57SRandall Stewart 	sctp_print_out_track_log(stcb);
4006f1f73e57SRandall Stewart #endif
4007ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4008ceaad40aSRandall Stewart 	if (!so_locked) {
4009ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
4010ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
4011ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
4012ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4013ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4014ceaad40aSRandall Stewart 	}
4015ceaad40aSRandall Stewart #endif
4016c4739e2fSRandall Stewart 	(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_5);
4017ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4018ceaad40aSRandall Stewart 	if (!so_locked) {
4019ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4020ceaad40aSRandall Stewart 	}
4021ceaad40aSRandall Stewart #endif
4022f8829a4aSRandall Stewart }
4023f8829a4aSRandall Stewart 
4024f8829a4aSRandall Stewart void
4025b1754ad1SMichael Tuexen sctp_handle_ootb(struct mbuf *m, int iphlen, int offset,
4026b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
4027b1754ad1SMichael Tuexen     struct sctphdr *sh, struct sctp_inpcb *inp,
4028ff1ffd74SMichael Tuexen     struct mbuf *cause,
4029f30ac432SMichael Tuexen     uint8_t use_mflowid, uint32_t mflowid,
4030f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
4031f8829a4aSRandall Stewart {
4032f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch, chunk_buf;
4033f8829a4aSRandall Stewart 	unsigned int chk_length;
4034c58e60beSMichael Tuexen 	int contains_init_chunk;
4035f8829a4aSRandall Stewart 
4036f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue);
4037f8829a4aSRandall Stewart 	/* Generate a TO address for future reference */
4038f8829a4aSRandall Stewart 	if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4039fe1831e0SMichael Tuexen 		if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4040b0552ae2SRandall Stewart 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4041b0552ae2SRandall Stewart 			    SCTP_CALLED_DIRECTLY_NOCMPSET);
4042f8829a4aSRandall Stewart 		}
4043f8829a4aSRandall Stewart 	}
4044c58e60beSMichael Tuexen 	contains_init_chunk = 0;
4045f8829a4aSRandall Stewart 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4046f8829a4aSRandall Stewart 	    sizeof(*ch), (uint8_t *) & chunk_buf);
4047f8829a4aSRandall Stewart 	while (ch != NULL) {
4048f8829a4aSRandall Stewart 		chk_length = ntohs(ch->chunk_length);
4049f8829a4aSRandall Stewart 		if (chk_length < sizeof(*ch)) {
4050f8829a4aSRandall Stewart 			/* break to abort land */
4051f8829a4aSRandall Stewart 			break;
4052f8829a4aSRandall Stewart 		}
4053f8829a4aSRandall Stewart 		switch (ch->chunk_type) {
4054c58e60beSMichael Tuexen 		case SCTP_INIT:
4055c58e60beSMichael Tuexen 			contains_init_chunk = 1;
4056c58e60beSMichael Tuexen 			break;
4057f8829a4aSRandall Stewart 		case SCTP_PACKET_DROPPED:
4058f8829a4aSRandall Stewart 			/* we don't respond to pkt-dropped */
4059f8829a4aSRandall Stewart 			return;
4060f8829a4aSRandall Stewart 		case SCTP_ABORT_ASSOCIATION:
4061f8829a4aSRandall Stewart 			/* we don't respond with an ABORT to an ABORT */
4062f8829a4aSRandall Stewart 			return;
4063f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_COMPLETE:
4064f8829a4aSRandall Stewart 			/*
4065f8829a4aSRandall Stewart 			 * we ignore it since we are not waiting for it and
4066f8829a4aSRandall Stewart 			 * peer is gone
4067f8829a4aSRandall Stewart 			 */
4068f8829a4aSRandall Stewart 			return;
4069f8829a4aSRandall Stewart 		case SCTP_SHUTDOWN_ACK:
4070b1754ad1SMichael Tuexen 			sctp_send_shutdown_complete2(src, dst, sh,
4071f30ac432SMichael Tuexen 			    use_mflowid, mflowid,
4072f30ac432SMichael Tuexen 			    vrf_id, port);
4073f8829a4aSRandall Stewart 			return;
4074f8829a4aSRandall Stewart 		default:
4075f8829a4aSRandall Stewart 			break;
4076f8829a4aSRandall Stewart 		}
4077f8829a4aSRandall Stewart 		offset += SCTP_SIZE32(chk_length);
4078f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4079f8829a4aSRandall Stewart 		    sizeof(*ch), (uint8_t *) & chunk_buf);
4080f8829a4aSRandall Stewart 	}
4081c58e60beSMichael Tuexen 	if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
4082c58e60beSMichael Tuexen 	    ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
4083c58e60beSMichael Tuexen 	    (contains_init_chunk == 0))) {
4084ff1ffd74SMichael Tuexen 		sctp_send_abort(m, iphlen, src, dst, sh, 0, cause,
4085f30ac432SMichael Tuexen 		    use_mflowid, mflowid,
4086f30ac432SMichael Tuexen 		    vrf_id, port);
4087f8829a4aSRandall Stewart 	}
4088c58e60beSMichael Tuexen }
4089f8829a4aSRandall Stewart 
4090f8829a4aSRandall Stewart /*
4091f8829a4aSRandall Stewart  * check the inbound datagram to make sure there is not an abort inside it,
4092f8829a4aSRandall Stewart  * if there is return 1, else return 0.
4093f8829a4aSRandall Stewart  */
4094f8829a4aSRandall Stewart int
4095f8829a4aSRandall Stewart sctp_is_there_an_abort_here(struct mbuf *m, int iphlen, uint32_t * vtagfill)
4096f8829a4aSRandall Stewart {
4097f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
4098f8829a4aSRandall Stewart 	struct sctp_init_chunk *init_chk, chunk_buf;
4099f8829a4aSRandall Stewart 	int offset;
4100f8829a4aSRandall Stewart 	unsigned int chk_length;
4101f8829a4aSRandall Stewart 
4102f8829a4aSRandall Stewart 	offset = iphlen + sizeof(struct sctphdr);
4103f8829a4aSRandall Stewart 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch),
4104f8829a4aSRandall Stewart 	    (uint8_t *) & chunk_buf);
4105f8829a4aSRandall Stewart 	while (ch != NULL) {
4106f8829a4aSRandall Stewart 		chk_length = ntohs(ch->chunk_length);
4107f8829a4aSRandall Stewart 		if (chk_length < sizeof(*ch)) {
4108f8829a4aSRandall Stewart 			/* packet is probably corrupt */
4109f8829a4aSRandall Stewart 			break;
4110f8829a4aSRandall Stewart 		}
4111f8829a4aSRandall Stewart 		/* we seem to be ok, is it an abort? */
4112f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) {
4113f8829a4aSRandall Stewart 			/* yep, tell them */
4114f8829a4aSRandall Stewart 			return (1);
4115f8829a4aSRandall Stewart 		}
4116f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_INITIATION) {
4117f8829a4aSRandall Stewart 			/* need to update the Vtag */
4118f8829a4aSRandall Stewart 			init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4119f8829a4aSRandall Stewart 			    offset, sizeof(*init_chk), (uint8_t *) & chunk_buf);
4120f8829a4aSRandall Stewart 			if (init_chk != NULL) {
4121f8829a4aSRandall Stewart 				*vtagfill = ntohl(init_chk->init.initiate_tag);
4122f8829a4aSRandall Stewart 			}
4123f8829a4aSRandall Stewart 		}
4124f8829a4aSRandall Stewart 		/* Nope, move to the next chunk */
4125f8829a4aSRandall Stewart 		offset += SCTP_SIZE32(chk_length);
4126f8829a4aSRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4127f8829a4aSRandall Stewart 		    sizeof(*ch), (uint8_t *) & chunk_buf);
4128f8829a4aSRandall Stewart 	}
4129f8829a4aSRandall Stewart 	return (0);
4130f8829a4aSRandall Stewart }
4131f8829a4aSRandall Stewart 
4132f8829a4aSRandall Stewart /*
4133f8829a4aSRandall Stewart  * currently (2/02), ifa_addr embeds scope_id's and don't have sin6_scope_id
4134f8829a4aSRandall Stewart  * set (i.e. it's 0) so, create this function to compare link local scopes
4135f8829a4aSRandall Stewart  */
41365e2c2d87SRandall Stewart #ifdef INET6
4137f8829a4aSRandall Stewart uint32_t
4138f8829a4aSRandall Stewart sctp_is_same_scope(struct sockaddr_in6 *addr1, struct sockaddr_in6 *addr2)
4139f8829a4aSRandall Stewart {
4140f8829a4aSRandall Stewart 	struct sockaddr_in6 a, b;
4141f8829a4aSRandall Stewart 
4142f8829a4aSRandall Stewart 	/* save copies */
4143f8829a4aSRandall Stewart 	a = *addr1;
4144f8829a4aSRandall Stewart 	b = *addr2;
4145f8829a4aSRandall Stewart 
4146f8829a4aSRandall Stewart 	if (a.sin6_scope_id == 0)
4147f8829a4aSRandall Stewart 		if (sa6_recoverscope(&a)) {
4148f8829a4aSRandall Stewart 			/* can't get scope, so can't match */
4149f8829a4aSRandall Stewart 			return (0);
4150f8829a4aSRandall Stewart 		}
4151f8829a4aSRandall Stewart 	if (b.sin6_scope_id == 0)
4152f8829a4aSRandall Stewart 		if (sa6_recoverscope(&b)) {
4153f8829a4aSRandall Stewart 			/* can't get scope, so can't match */
4154f8829a4aSRandall Stewart 			return (0);
4155f8829a4aSRandall Stewart 		}
4156f8829a4aSRandall Stewart 	if (a.sin6_scope_id != b.sin6_scope_id)
4157f8829a4aSRandall Stewart 		return (0);
4158f8829a4aSRandall Stewart 
4159f8829a4aSRandall Stewart 	return (1);
4160f8829a4aSRandall Stewart }
4161f8829a4aSRandall Stewart 
4162f8829a4aSRandall Stewart /*
4163f8829a4aSRandall Stewart  * returns a sockaddr_in6 with embedded scope recovered and removed
4164f8829a4aSRandall Stewart  */
4165f8829a4aSRandall Stewart struct sockaddr_in6 *
4166f8829a4aSRandall Stewart sctp_recover_scope(struct sockaddr_in6 *addr, struct sockaddr_in6 *store)
4167f8829a4aSRandall Stewart {
4168f8829a4aSRandall Stewart 	/* check and strip embedded scope junk */
4169f8829a4aSRandall Stewart 	if (addr->sin6_family == AF_INET6) {
4170f8829a4aSRandall Stewart 		if (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr)) {
4171f8829a4aSRandall Stewart 			if (addr->sin6_scope_id == 0) {
4172f8829a4aSRandall Stewart 				*store = *addr;
4173f8829a4aSRandall Stewart 				if (!sa6_recoverscope(store)) {
4174f8829a4aSRandall Stewart 					/* use the recovered scope */
4175f8829a4aSRandall Stewart 					addr = store;
4176f8829a4aSRandall Stewart 				}
4177f42a358aSRandall Stewart 			} else {
4178f8829a4aSRandall Stewart 				/* else, return the original "to" addr */
4179f42a358aSRandall Stewart 				in6_clearscope(&addr->sin6_addr);
4180f8829a4aSRandall Stewart 			}
4181f8829a4aSRandall Stewart 		}
4182f8829a4aSRandall Stewart 	}
4183f8829a4aSRandall Stewart 	return (addr);
4184f8829a4aSRandall Stewart }
4185f8829a4aSRandall Stewart 
41865e2c2d87SRandall Stewart #endif
41875e2c2d87SRandall Stewart 
4188f8829a4aSRandall Stewart /*
4189f8829a4aSRandall Stewart  * are the two addresses the same?  currently a "scopeless" check returns: 1
4190f8829a4aSRandall Stewart  * if same, 0 if not
4191f8829a4aSRandall Stewart  */
419272fb6fdbSRandall Stewart int
4193f8829a4aSRandall Stewart sctp_cmpaddr(struct sockaddr *sa1, struct sockaddr *sa2)
4194f8829a4aSRandall Stewart {
4195f8829a4aSRandall Stewart 
4196f8829a4aSRandall Stewart 	/* must be valid */
4197f8829a4aSRandall Stewart 	if (sa1 == NULL || sa2 == NULL)
4198f8829a4aSRandall Stewart 		return (0);
4199f8829a4aSRandall Stewart 
4200f8829a4aSRandall Stewart 	/* must be the same family */
4201f8829a4aSRandall Stewart 	if (sa1->sa_family != sa2->sa_family)
4202f8829a4aSRandall Stewart 		return (0);
4203f8829a4aSRandall Stewart 
42045e2c2d87SRandall Stewart 	switch (sa1->sa_family) {
42055e2c2d87SRandall Stewart #ifdef INET6
42065e2c2d87SRandall Stewart 	case AF_INET6:
42075e2c2d87SRandall Stewart 		{
4208f8829a4aSRandall Stewart 			/* IPv6 addresses */
4209f8829a4aSRandall Stewart 			struct sockaddr_in6 *sin6_1, *sin6_2;
4210f8829a4aSRandall Stewart 
4211f8829a4aSRandall Stewart 			sin6_1 = (struct sockaddr_in6 *)sa1;
4212f8829a4aSRandall Stewart 			sin6_2 = (struct sockaddr_in6 *)sa2;
4213c54a18d2SRandall Stewart 			return (SCTP6_ARE_ADDR_EQUAL(sin6_1,
4214c54a18d2SRandall Stewart 			    sin6_2));
42155e2c2d87SRandall Stewart 		}
42165e2c2d87SRandall Stewart #endif
4217ea5eba11SMichael Tuexen #ifdef INET
42185e2c2d87SRandall Stewart 	case AF_INET:
42195e2c2d87SRandall Stewart 		{
4220f8829a4aSRandall Stewart 			/* IPv4 addresses */
4221f8829a4aSRandall Stewart 			struct sockaddr_in *sin_1, *sin_2;
4222f8829a4aSRandall Stewart 
4223f8829a4aSRandall Stewart 			sin_1 = (struct sockaddr_in *)sa1;
4224f8829a4aSRandall Stewart 			sin_2 = (struct sockaddr_in *)sa2;
4225f8829a4aSRandall Stewart 			return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr);
42265e2c2d87SRandall Stewart 		}
4227ea5eba11SMichael Tuexen #endif
42285e2c2d87SRandall Stewart 	default:
4229f8829a4aSRandall Stewart 		/* we don't do these... */
4230f8829a4aSRandall Stewart 		return (0);
4231f8829a4aSRandall Stewart 	}
4232f8829a4aSRandall Stewart }
4233f8829a4aSRandall Stewart 
4234f8829a4aSRandall Stewart void
4235f8829a4aSRandall Stewart sctp_print_address(struct sockaddr *sa)
4236f8829a4aSRandall Stewart {
42375e2c2d87SRandall Stewart #ifdef INET6
42387d32aa0cSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
4239f8829a4aSRandall Stewart 
42405e2c2d87SRandall Stewart #endif
42415e2c2d87SRandall Stewart 
42425e2c2d87SRandall Stewart 	switch (sa->sa_family) {
42435e2c2d87SRandall Stewart #ifdef INET6
42445e2c2d87SRandall Stewart 	case AF_INET6:
42455e2c2d87SRandall Stewart 		{
4246ad81507eSRandall Stewart 			struct sockaddr_in6 *sin6;
4247ad81507eSRandall Stewart 
4248f8829a4aSRandall Stewart 			sin6 = (struct sockaddr_in6 *)sa;
4249ad81507eSRandall Stewart 			SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n",
42507d32aa0cSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &sin6->sin6_addr),
42517d32aa0cSBjoern A. Zeeb 			    ntohs(sin6->sin6_port),
4252f8829a4aSRandall Stewart 			    sin6->sin6_scope_id);
42535e2c2d87SRandall Stewart 			break;
42545e2c2d87SRandall Stewart 		}
42555e2c2d87SRandall Stewart #endif
4256ea5eba11SMichael Tuexen #ifdef INET
42575e2c2d87SRandall Stewart 	case AF_INET:
42585e2c2d87SRandall Stewart 		{
4259f8829a4aSRandall Stewart 			struct sockaddr_in *sin;
4260f8829a4aSRandall Stewart 			unsigned char *p;
4261f8829a4aSRandall Stewart 
4262f8829a4aSRandall Stewart 			sin = (struct sockaddr_in *)sa;
4263f8829a4aSRandall Stewart 			p = (unsigned char *)&sin->sin_addr;
4264ad81507eSRandall Stewart 			SCTP_PRINTF("IPv4 address: %u.%u.%u.%u:%d\n",
4265f8829a4aSRandall Stewart 			    p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
42665e2c2d87SRandall Stewart 			break;
42675e2c2d87SRandall Stewart 		}
4268ea5eba11SMichael Tuexen #endif
42695e2c2d87SRandall Stewart 	default:
4270ad81507eSRandall Stewart 		SCTP_PRINTF("?\n");
42715e2c2d87SRandall Stewart 		break;
4272f8829a4aSRandall Stewart 	}
4273f8829a4aSRandall Stewart }
4274f8829a4aSRandall Stewart 
4275f8829a4aSRandall Stewart void
4276f8829a4aSRandall Stewart sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
4277f8829a4aSRandall Stewart     struct sctp_inpcb *new_inp,
4278d06c82f1SRandall Stewart     struct sctp_tcb *stcb,
4279d06c82f1SRandall Stewart     int waitflags)
4280f8829a4aSRandall Stewart {
4281f8829a4aSRandall Stewart 	/*
4282f8829a4aSRandall Stewart 	 * go through our old INP and pull off any control structures that
4283f8829a4aSRandall Stewart 	 * belong to stcb and move then to the new inp.
4284f8829a4aSRandall Stewart 	 */
4285f8829a4aSRandall Stewart 	struct socket *old_so, *new_so;
4286f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control, *nctl;
4287f8829a4aSRandall Stewart 	struct sctp_readhead tmp_queue;
4288f8829a4aSRandall Stewart 	struct mbuf *m;
4289bff64a4dSRandall Stewart 	int error = 0;
4290f8829a4aSRandall Stewart 
4291f8829a4aSRandall Stewart 	old_so = old_inp->sctp_socket;
4292f8829a4aSRandall Stewart 	new_so = new_inp->sctp_socket;
4293f8829a4aSRandall Stewart 	TAILQ_INIT(&tmp_queue);
4294d06c82f1SRandall Stewart 	error = sblock(&old_so->so_rcv, waitflags);
4295f8829a4aSRandall Stewart 	if (error) {
4296f8829a4aSRandall Stewart 		/*
4297f8829a4aSRandall Stewart 		 * Gak, can't get sblock, we have a problem. data will be
4298f8829a4aSRandall Stewart 		 * left stranded.. and we don't dare look at it since the
4299f8829a4aSRandall Stewart 		 * other thread may be reading something. Oh well, its a
4300f8829a4aSRandall Stewart 		 * screwed up app that does a peeloff OR a accept while
4301f8829a4aSRandall Stewart 		 * reading from the main socket... actually its only the
4302f8829a4aSRandall Stewart 		 * peeloff() case, since I think read will fail on a
4303f8829a4aSRandall Stewart 		 * listening socket..
4304f8829a4aSRandall Stewart 		 */
4305f8829a4aSRandall Stewart 		return;
4306f8829a4aSRandall Stewart 	}
4307f8829a4aSRandall Stewart 	/* lock the socket buffers */
4308f8829a4aSRandall Stewart 	SCTP_INP_READ_LOCK(old_inp);
43094a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(control, &old_inp->read_queue, next, nctl) {
4310f8829a4aSRandall Stewart 		/* Pull off all for out target stcb */
4311f8829a4aSRandall Stewart 		if (control->stcb == stcb) {
4312f8829a4aSRandall Stewart 			/* remove it we want it */
4313f8829a4aSRandall Stewart 			TAILQ_REMOVE(&old_inp->read_queue, control, next);
4314f8829a4aSRandall Stewart 			TAILQ_INSERT_TAIL(&tmp_queue, control, next);
4315f8829a4aSRandall Stewart 			m = control->data;
4316f8829a4aSRandall Stewart 			while (m) {
4317b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4318139bc87fSRandall Stewart 					sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
431980fefe0aSRandall Stewart 				}
4320f8829a4aSRandall Stewart 				sctp_sbfree(control, stcb, &old_so->so_rcv, m);
4321b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4322f8829a4aSRandall Stewart 					sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
432380fefe0aSRandall Stewart 				}
4324139bc87fSRandall Stewart 				m = SCTP_BUF_NEXT(m);
4325f8829a4aSRandall Stewart 			}
4326f8829a4aSRandall Stewart 		}
4327f8829a4aSRandall Stewart 	}
4328f8829a4aSRandall Stewart 	SCTP_INP_READ_UNLOCK(old_inp);
4329f8829a4aSRandall Stewart 	/* Remove the sb-lock on the old socket */
4330f8829a4aSRandall Stewart 
4331f8829a4aSRandall Stewart 	sbunlock(&old_so->so_rcv);
4332f8829a4aSRandall Stewart 	/* Now we move them over to the new socket buffer */
4333f8829a4aSRandall Stewart 	SCTP_INP_READ_LOCK(new_inp);
43344a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(control, &tmp_queue, next, nctl) {
4335f8829a4aSRandall Stewart 		TAILQ_INSERT_TAIL(&new_inp->read_queue, control, next);
4336f8829a4aSRandall Stewart 		m = control->data;
4337f8829a4aSRandall Stewart 		while (m) {
4338b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4339139bc87fSRandall Stewart 				sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
434080fefe0aSRandall Stewart 			}
4341f8829a4aSRandall Stewart 			sctp_sballoc(stcb, &new_so->so_rcv, m);
4342b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4343f8829a4aSRandall Stewart 				sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
434480fefe0aSRandall Stewart 			}
4345139bc87fSRandall Stewart 			m = SCTP_BUF_NEXT(m);
4346f8829a4aSRandall Stewart 		}
4347f8829a4aSRandall Stewart 	}
4348f8829a4aSRandall Stewart 	SCTP_INP_READ_UNLOCK(new_inp);
4349f8829a4aSRandall Stewart }
4350f8829a4aSRandall Stewart 
4351f8829a4aSRandall Stewart void
4352f8829a4aSRandall Stewart sctp_add_to_readq(struct sctp_inpcb *inp,
4353f8829a4aSRandall Stewart     struct sctp_tcb *stcb,
4354f8829a4aSRandall Stewart     struct sctp_queued_to_read *control,
4355f8829a4aSRandall Stewart     struct sockbuf *sb,
4356ceaad40aSRandall Stewart     int end,
4357cfde3ff7SRandall Stewart     int inp_read_lock_held,
4358ceaad40aSRandall Stewart     int so_locked
4359ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4360ceaad40aSRandall Stewart     SCTP_UNUSED
4361ceaad40aSRandall Stewart #endif
4362ceaad40aSRandall Stewart )
4363f8829a4aSRandall Stewart {
4364f8829a4aSRandall Stewart 	/*
4365f8829a4aSRandall Stewart 	 * Here we must place the control on the end of the socket read
4366f8829a4aSRandall Stewart 	 * queue AND increment sb_cc so that select will work properly on
4367f8829a4aSRandall Stewart 	 * read.
4368f8829a4aSRandall Stewart 	 */
4369f8829a4aSRandall Stewart 	struct mbuf *m, *prev = NULL;
4370f8829a4aSRandall Stewart 
437103b0b021SRandall Stewart 	if (inp == NULL) {
437203b0b021SRandall Stewart 		/* Gak, TSNH!! */
4373a5d547adSRandall Stewart #ifdef INVARIANTS
437403b0b021SRandall Stewart 		panic("Gak, inp NULL on add_to_readq");
437503b0b021SRandall Stewart #endif
437603b0b021SRandall Stewart 		return;
437703b0b021SRandall Stewart 	}
4378cfde3ff7SRandall Stewart 	if (inp_read_lock_held == 0)
4379f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(inp);
4380cd1386abSMichael Tuexen 	if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
4381cd1386abSMichael Tuexen 		sctp_free_remote_addr(control->whoFrom);
4382cd1386abSMichael Tuexen 		if (control->data) {
4383cd1386abSMichael Tuexen 			sctp_m_freem(control->data);
4384cd1386abSMichael Tuexen 			control->data = NULL;
4385cd1386abSMichael Tuexen 		}
4386cd1386abSMichael Tuexen 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), control);
4387cd1386abSMichael Tuexen 		if (inp_read_lock_held == 0)
4388cd1386abSMichael Tuexen 			SCTP_INP_READ_UNLOCK(inp);
4389cd1386abSMichael Tuexen 		return;
4390cd1386abSMichael Tuexen 	}
439142551e99SRandall Stewart 	if (!(control->spec_flags & M_NOTIFICATION)) {
4392a5d547adSRandall Stewart 		atomic_add_int(&inp->total_recvs, 1);
439342551e99SRandall Stewart 		if (!control->do_not_ref_stcb) {
4394a5d547adSRandall Stewart 			atomic_add_int(&stcb->total_recvs, 1);
439542551e99SRandall Stewart 		}
439642551e99SRandall Stewart 	}
4397f8829a4aSRandall Stewart 	m = control->data;
4398f8829a4aSRandall Stewart 	control->held_length = 0;
4399f8829a4aSRandall Stewart 	control->length = 0;
4400f8829a4aSRandall Stewart 	while (m) {
4401139bc87fSRandall Stewart 		if (SCTP_BUF_LEN(m) == 0) {
4402f8829a4aSRandall Stewart 			/* Skip mbufs with NO length */
4403f8829a4aSRandall Stewart 			if (prev == NULL) {
4404f8829a4aSRandall Stewart 				/* First one */
4405f8829a4aSRandall Stewart 				control->data = sctp_m_free(m);
4406f8829a4aSRandall Stewart 				m = control->data;
4407f8829a4aSRandall Stewart 			} else {
4408139bc87fSRandall Stewart 				SCTP_BUF_NEXT(prev) = sctp_m_free(m);
4409139bc87fSRandall Stewart 				m = SCTP_BUF_NEXT(prev);
4410f8829a4aSRandall Stewart 			}
4411f8829a4aSRandall Stewart 			if (m == NULL) {
4412c2ede4b3SMartin Blapp 				control->tail_mbuf = prev;
4413f8829a4aSRandall Stewart 			}
4414f8829a4aSRandall Stewart 			continue;
4415f8829a4aSRandall Stewart 		}
4416f8829a4aSRandall Stewart 		prev = m;
4417b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4418139bc87fSRandall Stewart 			sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
441980fefe0aSRandall Stewart 		}
4420f8829a4aSRandall Stewart 		sctp_sballoc(stcb, sb, m);
4421b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4422f8829a4aSRandall Stewart 			sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
442380fefe0aSRandall Stewart 		}
4424139bc87fSRandall Stewart 		atomic_add_int(&control->length, SCTP_BUF_LEN(m));
4425139bc87fSRandall Stewart 		m = SCTP_BUF_NEXT(m);
4426f8829a4aSRandall Stewart 	}
4427f8829a4aSRandall Stewart 	if (prev != NULL) {
4428f8829a4aSRandall Stewart 		control->tail_mbuf = prev;
4429f8829a4aSRandall Stewart 	} else {
4430139bc87fSRandall Stewart 		/* Everything got collapsed out?? */
4431cd1386abSMichael Tuexen 		sctp_free_remote_addr(control->whoFrom);
4432cd1386abSMichael Tuexen 		SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), control);
4433cfde3ff7SRandall Stewart 		if (inp_read_lock_held == 0)
443447a490cbSMichael Tuexen 			SCTP_INP_READ_UNLOCK(inp);
4435f8829a4aSRandall Stewart 		return;
4436f8829a4aSRandall Stewart 	}
4437f8829a4aSRandall Stewart 	if (end) {
4438f8829a4aSRandall Stewart 		control->end_added = 1;
4439f8829a4aSRandall Stewart 	}
4440f8829a4aSRandall Stewart 	TAILQ_INSERT_TAIL(&inp->read_queue, control, next);
4441cfde3ff7SRandall Stewart 	if (inp_read_lock_held == 0)
4442f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
4443f8829a4aSRandall Stewart 	if (inp && inp->sctp_socket) {
444417205eccSRandall Stewart 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
444517205eccSRandall Stewart 			SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
4446ceaad40aSRandall Stewart 		} else {
4447ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4448ceaad40aSRandall Stewart 			struct socket *so;
4449ceaad40aSRandall Stewart 
4450ceaad40aSRandall Stewart 			so = SCTP_INP_SO(inp);
4451ceaad40aSRandall Stewart 			if (!so_locked) {
445260990c0cSMichael Tuexen 				if (stcb) {
4453ceaad40aSRandall Stewart 					atomic_add_int(&stcb->asoc.refcnt, 1);
4454ceaad40aSRandall Stewart 					SCTP_TCB_UNLOCK(stcb);
445560990c0cSMichael Tuexen 				}
4456ceaad40aSRandall Stewart 				SCTP_SOCKET_LOCK(so, 1);
445760990c0cSMichael Tuexen 				if (stcb) {
4458ceaad40aSRandall Stewart 					SCTP_TCB_LOCK(stcb);
4459ceaad40aSRandall Stewart 					atomic_subtract_int(&stcb->asoc.refcnt, 1);
446060990c0cSMichael Tuexen 				}
4461ceaad40aSRandall Stewart 				if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4462ceaad40aSRandall Stewart 					SCTP_SOCKET_UNLOCK(so, 1);
4463ceaad40aSRandall Stewart 					return;
4464ceaad40aSRandall Stewart 				}
4465ceaad40aSRandall Stewart 			}
4466ceaad40aSRandall Stewart #endif
4467f8829a4aSRandall Stewart 			sctp_sorwakeup(inp, inp->sctp_socket);
4468ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4469ceaad40aSRandall Stewart 			if (!so_locked) {
4470ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
4471ceaad40aSRandall Stewart 			}
4472ceaad40aSRandall Stewart #endif
4473ceaad40aSRandall Stewart 		}
4474f8829a4aSRandall Stewart 	}
4475f8829a4aSRandall Stewart }
4476f8829a4aSRandall Stewart 
4477f8829a4aSRandall Stewart 
4478f8829a4aSRandall Stewart int
4479f8829a4aSRandall Stewart sctp_append_to_readq(struct sctp_inpcb *inp,
4480f8829a4aSRandall Stewart     struct sctp_tcb *stcb,
4481f8829a4aSRandall Stewart     struct sctp_queued_to_read *control,
4482f8829a4aSRandall Stewart     struct mbuf *m,
4483f8829a4aSRandall Stewart     int end,
4484f8829a4aSRandall Stewart     int ctls_cumack,
4485f8829a4aSRandall Stewart     struct sockbuf *sb)
4486f8829a4aSRandall Stewart {
4487f8829a4aSRandall Stewart 	/*
4488f8829a4aSRandall Stewart 	 * A partial delivery API event is underway. OR we are appending on
4489f8829a4aSRandall Stewart 	 * the reassembly queue.
4490f8829a4aSRandall Stewart 	 *
4491f8829a4aSRandall Stewart 	 * If PDAPI this means we need to add m to the end of the data.
4492f8829a4aSRandall Stewart 	 * Increase the length in the control AND increment the sb_cc.
4493f8829a4aSRandall Stewart 	 * Otherwise sb is NULL and all we need to do is put it at the end
4494f8829a4aSRandall Stewart 	 * of the mbuf chain.
4495f8829a4aSRandall Stewart 	 */
4496f8829a4aSRandall Stewart 	int len = 0;
4497f8829a4aSRandall Stewart 	struct mbuf *mm, *tail = NULL, *prev = NULL;
4498f8829a4aSRandall Stewart 
4499f8829a4aSRandall Stewart 	if (inp) {
4500f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(inp);
4501f8829a4aSRandall Stewart 	}
4502f8829a4aSRandall Stewart 	if (control == NULL) {
4503f8829a4aSRandall Stewart get_out:
4504f8829a4aSRandall Stewart 		if (inp) {
4505f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
4506f8829a4aSRandall Stewart 		}
4507f8829a4aSRandall Stewart 		return (-1);
4508f8829a4aSRandall Stewart 	}
4509cd1386abSMichael Tuexen 	if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ)) {
4510cd1386abSMichael Tuexen 		SCTP_INP_READ_UNLOCK(inp);
451160990c0cSMichael Tuexen 		return (0);
4512cd1386abSMichael Tuexen 	}
4513139bc87fSRandall Stewart 	if (control->end_added) {
4514f8829a4aSRandall Stewart 		/* huh this one is complete? */
4515f8829a4aSRandall Stewart 		goto get_out;
4516f8829a4aSRandall Stewart 	}
4517f8829a4aSRandall Stewart 	mm = m;
4518f8829a4aSRandall Stewart 	if (mm == NULL) {
4519f8829a4aSRandall Stewart 		goto get_out;
4520f8829a4aSRandall Stewart 	}
4521f8829a4aSRandall Stewart 	while (mm) {
4522139bc87fSRandall Stewart 		if (SCTP_BUF_LEN(mm) == 0) {
4523f8829a4aSRandall Stewart 			/* Skip mbufs with NO lenght */
4524f8829a4aSRandall Stewart 			if (prev == NULL) {
4525f8829a4aSRandall Stewart 				/* First one */
4526f8829a4aSRandall Stewart 				m = sctp_m_free(mm);
4527f8829a4aSRandall Stewart 				mm = m;
4528f8829a4aSRandall Stewart 			} else {
4529139bc87fSRandall Stewart 				SCTP_BUF_NEXT(prev) = sctp_m_free(mm);
4530139bc87fSRandall Stewart 				mm = SCTP_BUF_NEXT(prev);
4531f8829a4aSRandall Stewart 			}
4532f8829a4aSRandall Stewart 			continue;
4533f8829a4aSRandall Stewart 		}
4534f8829a4aSRandall Stewart 		prev = mm;
4535139bc87fSRandall Stewart 		len += SCTP_BUF_LEN(mm);
4536f8829a4aSRandall Stewart 		if (sb) {
4537b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4538139bc87fSRandall Stewart 				sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(mm));
453980fefe0aSRandall Stewart 			}
4540f8829a4aSRandall Stewart 			sctp_sballoc(stcb, sb, mm);
4541b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
4542f8829a4aSRandall Stewart 				sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
454380fefe0aSRandall Stewart 			}
4544f8829a4aSRandall Stewart 		}
4545139bc87fSRandall Stewart 		mm = SCTP_BUF_NEXT(mm);
4546f8829a4aSRandall Stewart 	}
4547f8829a4aSRandall Stewart 	if (prev) {
4548f8829a4aSRandall Stewart 		tail = prev;
4549f8829a4aSRandall Stewart 	} else {
4550f8829a4aSRandall Stewart 		/* Really there should always be a prev */
4551f8829a4aSRandall Stewart 		if (m == NULL) {
4552f8829a4aSRandall Stewart 			/* Huh nothing left? */
4553a5d547adSRandall Stewart #ifdef INVARIANTS
4554f8829a4aSRandall Stewart 			panic("Nothing left to add?");
4555f8829a4aSRandall Stewart #else
4556f8829a4aSRandall Stewart 			goto get_out;
4557f8829a4aSRandall Stewart #endif
4558f8829a4aSRandall Stewart 		}
4559f8829a4aSRandall Stewart 		tail = m;
4560f8829a4aSRandall Stewart 	}
4561f8829a4aSRandall Stewart 	if (control->tail_mbuf) {
4562f8829a4aSRandall Stewart 		/* append */
4563139bc87fSRandall Stewart 		SCTP_BUF_NEXT(control->tail_mbuf) = m;
4564f8829a4aSRandall Stewart 		control->tail_mbuf = tail;
4565f8829a4aSRandall Stewart 	} else {
4566f8829a4aSRandall Stewart 		/* nothing there */
4567a5d547adSRandall Stewart #ifdef INVARIANTS
4568f8829a4aSRandall Stewart 		if (control->data != NULL) {
4569f8829a4aSRandall Stewart 			panic("This should NOT happen");
4570f8829a4aSRandall Stewart 		}
4571f8829a4aSRandall Stewart #endif
4572f8829a4aSRandall Stewart 		control->data = m;
4573f8829a4aSRandall Stewart 		control->tail_mbuf = tail;
4574f8829a4aSRandall Stewart 	}
457518e198d3SRandall Stewart 	atomic_add_int(&control->length, len);
457618e198d3SRandall Stewart 	if (end) {
457718e198d3SRandall Stewart 		/* message is complete */
457818e198d3SRandall Stewart 		if (stcb && (control == stcb->asoc.control_pdapi)) {
457918e198d3SRandall Stewart 			stcb->asoc.control_pdapi = NULL;
458018e198d3SRandall Stewart 		}
458118e198d3SRandall Stewart 		control->held_length = 0;
458218e198d3SRandall Stewart 		control->end_added = 1;
458318e198d3SRandall Stewart 	}
4584ad81507eSRandall Stewart 	if (stcb == NULL) {
4585ad81507eSRandall Stewart 		control->do_not_ref_stcb = 1;
4586ad81507eSRandall Stewart 	}
4587f8829a4aSRandall Stewart 	/*
4588f8829a4aSRandall Stewart 	 * When we are appending in partial delivery, the cum-ack is used
4589f8829a4aSRandall Stewart 	 * for the actual pd-api highest tsn on this mbuf. The true cum-ack
4590f8829a4aSRandall Stewart 	 * is populated in the outbound sinfo structure from the true cumack
4591f8829a4aSRandall Stewart 	 * if the association exists...
4592f8829a4aSRandall Stewart 	 */
4593f8829a4aSRandall Stewart 	control->sinfo_tsn = control->sinfo_cumtsn = ctls_cumack;
4594f8829a4aSRandall Stewart 	if (inp) {
4595f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
4596f8829a4aSRandall Stewart 	}
4597f8829a4aSRandall Stewart 	if (inp && inp->sctp_socket) {
459817205eccSRandall Stewart 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) {
459917205eccSRandall Stewart 			SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket);
4600ceaad40aSRandall Stewart 		} else {
4601ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4602ceaad40aSRandall Stewart 			struct socket *so;
4603ceaad40aSRandall Stewart 
4604ceaad40aSRandall Stewart 			so = SCTP_INP_SO(inp);
460560990c0cSMichael Tuexen 			if (stcb) {
4606ceaad40aSRandall Stewart 				atomic_add_int(&stcb->asoc.refcnt, 1);
4607ceaad40aSRandall Stewart 				SCTP_TCB_UNLOCK(stcb);
460860990c0cSMichael Tuexen 			}
4609ceaad40aSRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
461060990c0cSMichael Tuexen 			if (stcb) {
4611ceaad40aSRandall Stewart 				SCTP_TCB_LOCK(stcb);
4612ceaad40aSRandall Stewart 				atomic_subtract_int(&stcb->asoc.refcnt, 1);
461360990c0cSMichael Tuexen 			}
4614ceaad40aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4615ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
4616ceaad40aSRandall Stewart 				return (0);
4617ceaad40aSRandall Stewart 			}
4618ceaad40aSRandall Stewart #endif
4619f8829a4aSRandall Stewart 			sctp_sorwakeup(inp, inp->sctp_socket);
4620ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4621ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
4622ceaad40aSRandall Stewart #endif
4623ceaad40aSRandall Stewart 		}
4624f8829a4aSRandall Stewart 	}
4625f8829a4aSRandall Stewart 	return (0);
4626f8829a4aSRandall Stewart }
4627f8829a4aSRandall Stewart 
4628f8829a4aSRandall Stewart 
4629f8829a4aSRandall Stewart 
4630f8829a4aSRandall Stewart /*************HOLD THIS COMMENT FOR PATCH FILE OF
4631f8829a4aSRandall Stewart  *************ALTERNATE ROUTING CODE
4632f8829a4aSRandall Stewart  */
4633f8829a4aSRandall Stewart 
4634f8829a4aSRandall Stewart /*************HOLD THIS COMMENT FOR END OF PATCH FILE OF
4635f8829a4aSRandall Stewart  *************ALTERNATE ROUTING CODE
4636f8829a4aSRandall Stewart  */
4637f8829a4aSRandall Stewart 
4638f8829a4aSRandall Stewart struct mbuf *
4639ff1ffd74SMichael Tuexen sctp_generate_cause(uint16_t code, char *info)
4640f8829a4aSRandall Stewart {
4641f8829a4aSRandall Stewart 	struct mbuf *m;
4642ff1ffd74SMichael Tuexen 	struct sctp_gen_error_cause *cause;
4643ff1ffd74SMichael Tuexen 	size_t info_len, len;
4644f8829a4aSRandall Stewart 
4645ff1ffd74SMichael Tuexen 	if ((code == 0) || (info == NULL)) {
4646ff1ffd74SMichael Tuexen 		return (NULL);
4647ff1ffd74SMichael Tuexen 	}
4648ff1ffd74SMichael Tuexen 	info_len = strlen(info);
4649ff1ffd74SMichael Tuexen 	len = sizeof(struct sctp_paramhdr) + info_len;
4650ff1ffd74SMichael Tuexen 	m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
4651ff1ffd74SMichael Tuexen 	if (m != NULL) {
4652ff1ffd74SMichael Tuexen 		SCTP_BUF_LEN(m) = len;
4653ff1ffd74SMichael Tuexen 		cause = mtod(m, struct sctp_gen_error_cause *);
4654ff1ffd74SMichael Tuexen 		cause->code = htons(code);
4655ff1ffd74SMichael Tuexen 		cause->length = htons((uint16_t) len);
4656ff1ffd74SMichael Tuexen 		memcpy(cause->info, info, info_len);
4657f8829a4aSRandall Stewart 	}
4658f8829a4aSRandall Stewart 	return (m);
4659f8829a4aSRandall Stewart }
4660f8829a4aSRandall Stewart 
466132451da4SMichael Tuexen struct mbuf *
466232451da4SMichael Tuexen sctp_generate_no_user_data_cause(uint32_t tsn)
466332451da4SMichael Tuexen {
466432451da4SMichael Tuexen 	struct mbuf *m;
466532451da4SMichael Tuexen 	struct sctp_error_no_user_data *no_user_data_cause;
466632451da4SMichael Tuexen 	size_t len;
466732451da4SMichael Tuexen 
466832451da4SMichael Tuexen 	len = sizeof(struct sctp_error_no_user_data);
466932451da4SMichael Tuexen 	m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
467032451da4SMichael Tuexen 	if (m != NULL) {
467132451da4SMichael Tuexen 		SCTP_BUF_LEN(m) = len;
467232451da4SMichael Tuexen 		no_user_data_cause = mtod(m, struct sctp_error_no_user_data *);
467332451da4SMichael Tuexen 		no_user_data_cause->cause.code = htons(SCTP_CAUSE_NO_USER_DATA);
467432451da4SMichael Tuexen 		no_user_data_cause->cause.length = htons((uint16_t) len);
467532451da4SMichael Tuexen 		no_user_data_cause->tsn = tsn;	/* tsn is passed in as NBO */
467632451da4SMichael Tuexen 	}
467732451da4SMichael Tuexen 	return (m);
467832451da4SMichael Tuexen }
467932451da4SMichael Tuexen 
4680f8829a4aSRandall Stewart #ifdef SCTP_MBCNT_LOGGING
4681f8829a4aSRandall Stewart void
4682f8829a4aSRandall Stewart sctp_free_bufspace(struct sctp_tcb *stcb, struct sctp_association *asoc,
4683f8829a4aSRandall Stewart     struct sctp_tmit_chunk *tp1, int chk_cnt)
4684f8829a4aSRandall Stewart {
4685f8829a4aSRandall Stewart 	if (tp1->data == NULL) {
4686f8829a4aSRandall Stewart 		return;
4687f8829a4aSRandall Stewart 	}
4688f8829a4aSRandall Stewart 	asoc->chunks_on_out_queue -= chk_cnt;
4689b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBCNT_LOGGING_ENABLE) {
4690f8829a4aSRandall Stewart 		sctp_log_mbcnt(SCTP_LOG_MBCNT_DECREASE,
4691f8829a4aSRandall Stewart 		    asoc->total_output_queue_size,
4692f8829a4aSRandall Stewart 		    tp1->book_size,
4693f8829a4aSRandall Stewart 		    0,
4694f8829a4aSRandall Stewart 		    tp1->mbcnt);
469580fefe0aSRandall Stewart 	}
4696f8829a4aSRandall Stewart 	if (asoc->total_output_queue_size >= tp1->book_size) {
469744b7479bSRandall Stewart 		atomic_add_int(&asoc->total_output_queue_size, -tp1->book_size);
4698f8829a4aSRandall Stewart 	} else {
4699f8829a4aSRandall Stewart 		asoc->total_output_queue_size = 0;
4700f8829a4aSRandall Stewart 	}
4701f8829a4aSRandall Stewart 
4702f8829a4aSRandall Stewart 	if (stcb->sctp_socket && (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) ||
4703f8829a4aSRandall Stewart 	    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) {
4704f8829a4aSRandall Stewart 		if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) {
4705f8829a4aSRandall Stewart 			stcb->sctp_socket->so_snd.sb_cc -= tp1->book_size;
4706f8829a4aSRandall Stewart 		} else {
4707f8829a4aSRandall Stewart 			stcb->sctp_socket->so_snd.sb_cc = 0;
4708f8829a4aSRandall Stewart 
4709f8829a4aSRandall Stewart 		}
4710f8829a4aSRandall Stewart 	}
4711f8829a4aSRandall Stewart }
4712f8829a4aSRandall Stewart 
4713f8829a4aSRandall Stewart #endif
4714f8829a4aSRandall Stewart 
4715f8829a4aSRandall Stewart int
4716f8829a4aSRandall Stewart sctp_release_pr_sctp_chunk(struct sctp_tcb *stcb, struct sctp_tmit_chunk *tp1,
47171edc9dbaSMichael Tuexen     uint8_t sent, int so_locked
4718ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4719ceaad40aSRandall Stewart     SCTP_UNUSED
4720ceaad40aSRandall Stewart #endif
4721ceaad40aSRandall Stewart )
4722f8829a4aSRandall Stewart {
47230c0982b8SRandall Stewart 	struct sctp_stream_out *strq;
47244a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk = NULL, *tp2;
47250c0982b8SRandall Stewart 	struct sctp_stream_queue_pending *sp;
47260c0982b8SRandall Stewart 	uint16_t stream = 0, seq = 0;
47270c0982b8SRandall Stewart 	uint8_t foundeom = 0;
4728f8829a4aSRandall Stewart 	int ret_sz = 0;
4729f8829a4aSRandall Stewart 	int notdone;
47300c0982b8SRandall Stewart 	int do_wakeup_routine = 0;
4731f8829a4aSRandall Stewart 
47320c0982b8SRandall Stewart 	stream = tp1->rec.data.stream_number;
47330c0982b8SRandall Stewart 	seq = tp1->rec.data.stream_seq;
4734f0396ad1SMichael Tuexen 	if (sent || !(tp1->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG)) {
4735f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_sent[0]++;
4736f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
4737f0396ad1SMichael Tuexen 		stcb->asoc.strmout[stream].abandoned_sent[0]++;
4738f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
4739f0396ad1SMichael Tuexen 		stcb->asoc.strmout[stream].abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
4740f0396ad1SMichael Tuexen #endif
4741f0396ad1SMichael Tuexen 	} else {
4742f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_unsent[0]++;
4743f0396ad1SMichael Tuexen 		stcb->asoc.abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
4744f0396ad1SMichael Tuexen 		stcb->asoc.strmout[stream].abandoned_unsent[0]++;
4745f0396ad1SMichael Tuexen #if defined(SCTP_DETAILED_STR_STATS)
4746f0396ad1SMichael Tuexen 		stcb->asoc.strmout[stream].abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
4747f0396ad1SMichael Tuexen #endif
4748f0396ad1SMichael Tuexen 	}
4749f8829a4aSRandall Stewart 	do {
4750f8829a4aSRandall Stewart 		ret_sz += tp1->book_size;
47510c0982b8SRandall Stewart 		if (tp1->data != NULL) {
47528933fa13SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4753830d754dSRandall Stewart 				sctp_flight_size_decrease(tp1);
4754830d754dSRandall Stewart 				sctp_total_flight_decrease(stcb, tp1);
47558933fa13SRandall Stewart 			}
47568933fa13SRandall Stewart 			sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
47570c0982b8SRandall Stewart 			stcb->asoc.peers_rwnd += tp1->send_size;
47580c0982b8SRandall Stewart 			stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
47591edc9dbaSMichael Tuexen 			if (sent) {
47601edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
47611edc9dbaSMichael Tuexen 			} else {
47621edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
47631edc9dbaSMichael Tuexen 			}
47642f99457bSMichael Tuexen 			if (tp1->data) {
4765f8829a4aSRandall Stewart 				sctp_m_freem(tp1->data);
4766f8829a4aSRandall Stewart 				tp1->data = NULL;
47672f99457bSMichael Tuexen 			}
47680c0982b8SRandall Stewart 			do_wakeup_routine = 1;
4769f8829a4aSRandall Stewart 			if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
4770f8829a4aSRandall Stewart 				stcb->asoc.sent_queue_cnt_removeable--;
4771f8829a4aSRandall Stewart 			}
4772f8829a4aSRandall Stewart 		}
47738933fa13SRandall Stewart 		tp1->sent = SCTP_FORWARD_TSN_SKIP;
4774f8829a4aSRandall Stewart 		if ((tp1->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) ==
4775f8829a4aSRandall Stewart 		    SCTP_DATA_NOT_FRAG) {
4776f8829a4aSRandall Stewart 			/* not frag'ed we ae done   */
4777f8829a4aSRandall Stewart 			notdone = 0;
4778f8829a4aSRandall Stewart 			foundeom = 1;
4779f8829a4aSRandall Stewart 		} else if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
4780f8829a4aSRandall Stewart 			/* end of frag, we are done */
4781f8829a4aSRandall Stewart 			notdone = 0;
4782f8829a4aSRandall Stewart 			foundeom = 1;
4783f8829a4aSRandall Stewart 		} else {
4784f8829a4aSRandall Stewart 			/*
4785f8829a4aSRandall Stewart 			 * Its a begin or middle piece, we must mark all of
4786f8829a4aSRandall Stewart 			 * it
4787f8829a4aSRandall Stewart 			 */
4788f8829a4aSRandall Stewart 			notdone = 1;
4789f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
4790f8829a4aSRandall Stewart 		}
4791f8829a4aSRandall Stewart 	} while (tp1 && notdone);
47920c0982b8SRandall Stewart 	if (foundeom == 0) {
4793f8829a4aSRandall Stewart 		/*
4794f8829a4aSRandall Stewart 		 * The multi-part message was scattered across the send and
4795f8829a4aSRandall Stewart 		 * sent queue.
4796f8829a4aSRandall Stewart 		 */
47974a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(tp1, &stcb->asoc.send_queue, sctp_next, tp2) {
47984a9ef3f8SMichael Tuexen 			if ((tp1->rec.data.stream_number != stream) ||
47994a9ef3f8SMichael Tuexen 			    (tp1->rec.data.stream_seq != seq)) {
48004a9ef3f8SMichael Tuexen 				break;
48014a9ef3f8SMichael Tuexen 			}
48020c0982b8SRandall Stewart 			/*
48030c0982b8SRandall Stewart 			 * save to chk in case we have some on stream out
48040c0982b8SRandall Stewart 			 * queue. If so and we have an un-transmitted one we
48050c0982b8SRandall Stewart 			 * don't have to fudge the TSN.
48060c0982b8SRandall Stewart 			 */
48070c0982b8SRandall Stewart 			chk = tp1;
48080c0982b8SRandall Stewart 			ret_sz += tp1->book_size;
48090c0982b8SRandall Stewart 			sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
48101edc9dbaSMichael Tuexen 			if (sent) {
48111edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
48121edc9dbaSMichael Tuexen 			} else {
48131edc9dbaSMichael Tuexen 				sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
48141edc9dbaSMichael Tuexen 			}
48152f99457bSMichael Tuexen 			if (tp1->data) {
48160c0982b8SRandall Stewart 				sctp_m_freem(tp1->data);
48172f99457bSMichael Tuexen 				tp1->data = NULL;
48182f99457bSMichael Tuexen 			}
48198933fa13SRandall Stewart 			/* No flight involved here book the size to 0 */
48208933fa13SRandall Stewart 			tp1->book_size = 0;
48210c0982b8SRandall Stewart 			if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
48220c0982b8SRandall Stewart 				foundeom = 1;
4823f8829a4aSRandall Stewart 			}
48240c0982b8SRandall Stewart 			do_wakeup_routine = 1;
48250c0982b8SRandall Stewart 			tp1->sent = SCTP_FORWARD_TSN_SKIP;
48260c0982b8SRandall Stewart 			TAILQ_REMOVE(&stcb->asoc.send_queue, tp1, sctp_next);
48270c0982b8SRandall Stewart 			/*
48280c0982b8SRandall Stewart 			 * on to the sent queue so we can wait for it to be
48290c0982b8SRandall Stewart 			 * passed by.
48300c0982b8SRandall Stewart 			 */
48310c0982b8SRandall Stewart 			TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, tp1,
48320c0982b8SRandall Stewart 			    sctp_next);
48330c0982b8SRandall Stewart 			stcb->asoc.send_queue_cnt--;
48340c0982b8SRandall Stewart 			stcb->asoc.sent_queue_cnt++;
48350c0982b8SRandall Stewart 		}
48360c0982b8SRandall Stewart 	}
48370c0982b8SRandall Stewart 	if (foundeom == 0) {
48380c0982b8SRandall Stewart 		/*
48390c0982b8SRandall Stewart 		 * Still no eom found. That means there is stuff left on the
48400c0982b8SRandall Stewart 		 * stream out queue.. yuck.
48410c0982b8SRandall Stewart 		 */
48420c0982b8SRandall Stewart 		SCTP_TCB_SEND_LOCK(stcb);
4843f3b05218SMichael Tuexen 		strq = &stcb->asoc.strmout[stream];
4844f3b05218SMichael Tuexen 		sp = TAILQ_FIRST(&strq->outqueue);
4845f3b05218SMichael Tuexen 		if (sp != NULL) {
48460c0982b8SRandall Stewart 			sp->discard_rest = 1;
48470c0982b8SRandall Stewart 			/*
4848f3b05218SMichael Tuexen 			 * We may need to put a chunk on the queue that
4849f3b05218SMichael Tuexen 			 * holds the TSN that would have been sent with the
4850f3b05218SMichael Tuexen 			 * LAST bit.
48510c0982b8SRandall Stewart 			 */
48520c0982b8SRandall Stewart 			if (chk == NULL) {
48530c0982b8SRandall Stewart 				/* Yep, we have to */
48540c0982b8SRandall Stewart 				sctp_alloc_a_chunk(stcb, chk);
48550c0982b8SRandall Stewart 				if (chk == NULL) {
48560c0982b8SRandall Stewart 					/*
4857f3b05218SMichael Tuexen 					 * we are hosed. All we can do is
4858f3b05218SMichael Tuexen 					 * nothing.. which will cause an
4859f3b05218SMichael Tuexen 					 * abort if the peer is paying
48600c0982b8SRandall Stewart 					 * attention.
48610c0982b8SRandall Stewart 					 */
48620c0982b8SRandall Stewart 					goto oh_well;
48630c0982b8SRandall Stewart 				}
48640c0982b8SRandall Stewart 				memset(chk, 0, sizeof(*chk));
48650c0982b8SRandall Stewart 				chk->rec.data.rcv_flags = SCTP_DATA_LAST_FRAG;
48660c0982b8SRandall Stewart 				chk->sent = SCTP_FORWARD_TSN_SKIP;
48670c0982b8SRandall Stewart 				chk->asoc = &stcb->asoc;
4868f3b05218SMichael Tuexen 				chk->rec.data.stream_seq = strq->next_sequence_send;
48690c0982b8SRandall Stewart 				chk->rec.data.stream_number = sp->stream;
48700c0982b8SRandall Stewart 				chk->rec.data.payloadtype = sp->ppid;
48710c0982b8SRandall Stewart 				chk->rec.data.context = sp->context;
48720c0982b8SRandall Stewart 				chk->flags = sp->act_flags;
48739eea4a2dSMichael Tuexen 				if (sp->net)
48740c0982b8SRandall Stewart 					chk->whoTo = sp->net;
48759eea4a2dSMichael Tuexen 				else
48769eea4a2dSMichael Tuexen 					chk->whoTo = stcb->asoc.primary_destination;
48770c0982b8SRandall Stewart 				atomic_add_int(&chk->whoTo->ref_count, 1);
48780c0982b8SRandall Stewart 				chk->rec.data.TSN_seq = atomic_fetchadd_int(&stcb->asoc.sending_seq, 1);
48790c0982b8SRandall Stewart 				stcb->asoc.pr_sctp_cnt++;
48800c0982b8SRandall Stewart 				TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, chk, sctp_next);
48810c0982b8SRandall Stewart 				stcb->asoc.sent_queue_cnt++;
48828933fa13SRandall Stewart 				stcb->asoc.pr_sctp_cnt++;
48830c0982b8SRandall Stewart 			} else {
48840c0982b8SRandall Stewart 				chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
48850c0982b8SRandall Stewart 			}
4886f3b05218SMichael Tuexen 			strq->next_sequence_send++;
48870c0982b8SRandall Stewart 	oh_well:
48880c0982b8SRandall Stewart 			if (sp->data) {
48890c0982b8SRandall Stewart 				/*
4890f3b05218SMichael Tuexen 				 * Pull any data to free up the SB and allow
4891f3b05218SMichael Tuexen 				 * sender to "add more" while we will throw
4892f3b05218SMichael Tuexen 				 * away :-)
48930c0982b8SRandall Stewart 				 */
4894f3b05218SMichael Tuexen 				sctp_free_spbufspace(stcb, &stcb->asoc, sp);
48950c0982b8SRandall Stewart 				ret_sz += sp->length;
48960c0982b8SRandall Stewart 				do_wakeup_routine = 1;
48970c0982b8SRandall Stewart 				sp->some_taken = 1;
48980c0982b8SRandall Stewart 				sctp_m_freem(sp->data);
48990c0982b8SRandall Stewart 				sp->data = NULL;
49000c0982b8SRandall Stewart 				sp->tail_mbuf = NULL;
4901d07b2ac6SMichael Tuexen 				sp->length = 0;
49020c0982b8SRandall Stewart 			}
49030c0982b8SRandall Stewart 		}
49040c0982b8SRandall Stewart 		SCTP_TCB_SEND_UNLOCK(stcb);
49050c0982b8SRandall Stewart 	}
49060c0982b8SRandall Stewart 	if (do_wakeup_routine) {
49070c0982b8SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
49088933fa13SRandall Stewart 		struct socket *so;
49098933fa13SRandall Stewart 
49100c0982b8SRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
49110c0982b8SRandall Stewart 		if (!so_locked) {
49120c0982b8SRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, 1);
49130c0982b8SRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
49140c0982b8SRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
49150c0982b8SRandall Stewart 			SCTP_TCB_LOCK(stcb);
49160c0982b8SRandall Stewart 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
49170c0982b8SRandall Stewart 			if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
49180c0982b8SRandall Stewart 				/* assoc was freed while we were unlocked */
49190c0982b8SRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
49200c0982b8SRandall Stewart 				return (ret_sz);
49210c0982b8SRandall Stewart 			}
49220c0982b8SRandall Stewart 		}
49230c0982b8SRandall Stewart #endif
49240c0982b8SRandall Stewart 		sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
49250c0982b8SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
49260c0982b8SRandall Stewart 		if (!so_locked) {
49270c0982b8SRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
49280c0982b8SRandall Stewart 		}
49290c0982b8SRandall Stewart #endif
4930f8829a4aSRandall Stewart 	}
4931f8829a4aSRandall Stewart 	return (ret_sz);
4932f8829a4aSRandall Stewart }
4933f8829a4aSRandall Stewart 
4934f8829a4aSRandall Stewart /*
4935f8829a4aSRandall Stewart  * checks to see if the given address, sa, is one that is currently known by
4936f8829a4aSRandall Stewart  * the kernel note: can't distinguish the same address on multiple interfaces
4937f8829a4aSRandall Stewart  * and doesn't handle multiple addresses with different zone/scope id's note:
4938f8829a4aSRandall Stewart  * ifa_ifwithaddr() compares the entire sockaddr struct
4939f8829a4aSRandall Stewart  */
494042551e99SRandall Stewart struct sctp_ifa *
494180fefe0aSRandall Stewart sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr,
494280fefe0aSRandall Stewart     int holds_lock)
4943f8829a4aSRandall Stewart {
494442551e99SRandall Stewart 	struct sctp_laddr *laddr;
4945f8829a4aSRandall Stewart 
4946ad81507eSRandall Stewart 	if (holds_lock == 0) {
494742551e99SRandall Stewart 		SCTP_INP_RLOCK(inp);
4948ad81507eSRandall Stewart 	}
494942551e99SRandall Stewart 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
495042551e99SRandall Stewart 		if (laddr->ifa == NULL)
4951f8829a4aSRandall Stewart 			continue;
495242551e99SRandall Stewart 		if (addr->sa_family != laddr->ifa->address.sa.sa_family)
495342551e99SRandall Stewart 			continue;
4954e6194c2eSMichael Tuexen #ifdef INET
495542551e99SRandall Stewart 		if (addr->sa_family == AF_INET) {
495642551e99SRandall Stewart 			if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
495742551e99SRandall Stewart 			    laddr->ifa->address.sin.sin_addr.s_addr) {
495842551e99SRandall Stewart 				/* found him. */
4959ad81507eSRandall Stewart 				if (holds_lock == 0) {
496042551e99SRandall Stewart 					SCTP_INP_RUNLOCK(inp);
4961ad81507eSRandall Stewart 				}
496242551e99SRandall Stewart 				return (laddr->ifa);
496342551e99SRandall Stewart 				break;
496442551e99SRandall Stewart 			}
49655e2c2d87SRandall Stewart 		}
4966e6194c2eSMichael Tuexen #endif
49675e2c2d87SRandall Stewart #ifdef INET6
49685e2c2d87SRandall Stewart 		if (addr->sa_family == AF_INET6) {
4969c54a18d2SRandall Stewart 			if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
4970c54a18d2SRandall Stewart 			    &laddr->ifa->address.sin6)) {
497142551e99SRandall Stewart 				/* found him. */
4972ad81507eSRandall Stewart 				if (holds_lock == 0) {
497342551e99SRandall Stewart 					SCTP_INP_RUNLOCK(inp);
4974ad81507eSRandall Stewart 				}
497542551e99SRandall Stewart 				return (laddr->ifa);
497642551e99SRandall Stewart 				break;
497742551e99SRandall Stewart 			}
497842551e99SRandall Stewart 		}
49795e2c2d87SRandall Stewart #endif
498042551e99SRandall Stewart 	}
4981ad81507eSRandall Stewart 	if (holds_lock == 0) {
498242551e99SRandall Stewart 		SCTP_INP_RUNLOCK(inp);
4983ad81507eSRandall Stewart 	}
498442551e99SRandall Stewart 	return (NULL);
498542551e99SRandall Stewart }
4986f8829a4aSRandall Stewart 
49876a27c376SRandall Stewart uint32_t
49886a27c376SRandall Stewart sctp_get_ifa_hash_val(struct sockaddr *addr)
49896a27c376SRandall Stewart {
4990ea5eba11SMichael Tuexen 	switch (addr->sa_family) {
4991ea5eba11SMichael Tuexen #ifdef INET
4992ea5eba11SMichael Tuexen 	case AF_INET:
4993ea5eba11SMichael Tuexen 		{
49946a27c376SRandall Stewart 			struct sockaddr_in *sin;
49956a27c376SRandall Stewart 
49966a27c376SRandall Stewart 			sin = (struct sockaddr_in *)addr;
49976a27c376SRandall Stewart 			return (sin->sin_addr.s_addr ^ (sin->sin_addr.s_addr >> 16));
4998ea5eba11SMichael Tuexen 		}
4999ea5eba11SMichael Tuexen #endif
5000ea5eba11SMichael Tuexen #ifdef INET6
50012c2e3218SMichael Tuexen 	case AF_INET6:
5002ea5eba11SMichael Tuexen 		{
50036a27c376SRandall Stewart 			struct sockaddr_in6 *sin6;
50046a27c376SRandall Stewart 			uint32_t hash_of_addr;
50056a27c376SRandall Stewart 
50066a27c376SRandall Stewart 			sin6 = (struct sockaddr_in6 *)addr;
50076a27c376SRandall Stewart 			hash_of_addr = (sin6->sin6_addr.s6_addr32[0] +
50086a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[1] +
50096a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[2] +
50106a27c376SRandall Stewart 			    sin6->sin6_addr.s6_addr32[3]);
50116a27c376SRandall Stewart 			hash_of_addr = (hash_of_addr ^ (hash_of_addr >> 16));
50126a27c376SRandall Stewart 			return (hash_of_addr);
50136a27c376SRandall Stewart 		}
5014ea5eba11SMichael Tuexen #endif
5015ea5eba11SMichael Tuexen 	default:
5016ea5eba11SMichael Tuexen 		break;
5017ea5eba11SMichael Tuexen 	}
50186a27c376SRandall Stewart 	return (0);
50196a27c376SRandall Stewart }
50206a27c376SRandall Stewart 
502142551e99SRandall Stewart struct sctp_ifa *
502242551e99SRandall Stewart sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock)
502342551e99SRandall Stewart {
502442551e99SRandall Stewart 	struct sctp_ifa *sctp_ifap;
502542551e99SRandall Stewart 	struct sctp_vrf *vrf;
50266a27c376SRandall Stewart 	struct sctp_ifalist *hash_head;
50276a27c376SRandall Stewart 	uint32_t hash_of_addr;
502842551e99SRandall Stewart 
502942551e99SRandall Stewart 	if (holds_lock == 0)
5030c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RLOCK();
503142551e99SRandall Stewart 
5032bff64a4dSRandall Stewart 	vrf = sctp_find_vrf(vrf_id);
5033bff64a4dSRandall Stewart 	if (vrf == NULL) {
5034df6e0cc3SRandall Stewart stage_right:
5035bff64a4dSRandall Stewart 		if (holds_lock == 0)
5036c99efcf6SRandall Stewart 			SCTP_IPI_ADDR_RUNLOCK();
5037bff64a4dSRandall Stewart 		return (NULL);
5038bff64a4dSRandall Stewart 	}
5039bff64a4dSRandall Stewart 	hash_of_addr = sctp_get_ifa_hash_val(addr);
5040bff64a4dSRandall Stewart 
504117205eccSRandall Stewart 	hash_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
5042bff64a4dSRandall Stewart 	if (hash_head == NULL) {
5043ad81507eSRandall Stewart 		SCTP_PRINTF("hash_of_addr:%x mask:%x table:%x - ",
5044c99efcf6SRandall Stewart 		    hash_of_addr, (uint32_t) vrf->vrf_addr_hashmark,
5045c99efcf6SRandall Stewart 		    (uint32_t) (hash_of_addr & vrf->vrf_addr_hashmark));
5046bff64a4dSRandall Stewart 		sctp_print_address(addr);
5047ad81507eSRandall Stewart 		SCTP_PRINTF("No such bucket for address\n");
5048bff64a4dSRandall Stewart 		if (holds_lock == 0)
5049c99efcf6SRandall Stewart 			SCTP_IPI_ADDR_RUNLOCK();
5050bff64a4dSRandall Stewart 
5051bff64a4dSRandall Stewart 		return (NULL);
5052bff64a4dSRandall Stewart 	}
50536a27c376SRandall Stewart 	LIST_FOREACH(sctp_ifap, hash_head, next_bucket) {
5054bff64a4dSRandall Stewart 		if (sctp_ifap == NULL) {
5055df6e0cc3SRandall Stewart #ifdef INVARIANTS
5056bff64a4dSRandall Stewart 			panic("Huh LIST_FOREACH corrupt");
5057df6e0cc3SRandall Stewart 			goto stage_right;
5058df6e0cc3SRandall Stewart #else
5059df6e0cc3SRandall Stewart 			SCTP_PRINTF("LIST corrupt of sctp_ifap's?\n");
5060df6e0cc3SRandall Stewart 			goto stage_right;
5061df6e0cc3SRandall Stewart #endif
5062bff64a4dSRandall Stewart 		}
50636a27c376SRandall Stewart 		if (addr->sa_family != sctp_ifap->address.sa.sa_family)
50646a27c376SRandall Stewart 			continue;
5065e6194c2eSMichael Tuexen #ifdef INET
50666a27c376SRandall Stewart 		if (addr->sa_family == AF_INET) {
50676a27c376SRandall Stewart 			if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
50686a27c376SRandall Stewart 			    sctp_ifap->address.sin.sin_addr.s_addr) {
50696a27c376SRandall Stewart 				/* found him. */
507042551e99SRandall Stewart 				if (holds_lock == 0)
5071c99efcf6SRandall Stewart 					SCTP_IPI_ADDR_RUNLOCK();
507242551e99SRandall Stewart 				return (sctp_ifap);
50736a27c376SRandall Stewart 				break;
50746a27c376SRandall Stewart 			}
50755e2c2d87SRandall Stewart 		}
5076e6194c2eSMichael Tuexen #endif
50775e2c2d87SRandall Stewart #ifdef INET6
50785e2c2d87SRandall Stewart 		if (addr->sa_family == AF_INET6) {
5079c54a18d2SRandall Stewart 			if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5080c54a18d2SRandall Stewart 			    &sctp_ifap->address.sin6)) {
50816a27c376SRandall Stewart 				/* found him. */
50826a27c376SRandall Stewart 				if (holds_lock == 0)
5083c99efcf6SRandall Stewart 					SCTP_IPI_ADDR_RUNLOCK();
50846a27c376SRandall Stewart 				return (sctp_ifap);
50856a27c376SRandall Stewart 				break;
50866a27c376SRandall Stewart 			}
508742551e99SRandall Stewart 		}
50885e2c2d87SRandall Stewart #endif
508942551e99SRandall Stewart 	}
509042551e99SRandall Stewart 	if (holds_lock == 0)
5091c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RUNLOCK();
5092f8829a4aSRandall Stewart 	return (NULL);
5093f8829a4aSRandall Stewart }
5094f8829a4aSRandall Stewart 
5095f8829a4aSRandall Stewart static void
50964c9179adSRandall Stewart sctp_user_rcvd(struct sctp_tcb *stcb, uint32_t * freed_so_far, int hold_rlock,
5097f8829a4aSRandall Stewart     uint32_t rwnd_req)
5098f8829a4aSRandall Stewart {
5099f8829a4aSRandall Stewart 	/* User pulled some data, do we need a rwnd update? */
5100f8829a4aSRandall Stewart 	int r_unlocked = 0;
5101f8829a4aSRandall Stewart 	uint32_t dif, rwnd;
5102f8829a4aSRandall Stewart 	struct socket *so = NULL;
5103f8829a4aSRandall Stewart 
5104f8829a4aSRandall Stewart 	if (stcb == NULL)
5105f8829a4aSRandall Stewart 		return;
5106f8829a4aSRandall Stewart 
510750cec919SRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, 1);
5108f8829a4aSRandall Stewart 
510962c1ff9cSRandall Stewart 	if (stcb->asoc.state & (SCTP_STATE_ABOUT_TO_BE_FREED |
511062c1ff9cSRandall Stewart 	    SCTP_STATE_SHUTDOWN_RECEIVED |
51114c9179adSRandall Stewart 	    SCTP_STATE_SHUTDOWN_ACK_SENT)) {
5112f8829a4aSRandall Stewart 		/* Pre-check If we are freeing no update */
5113f8829a4aSRandall Stewart 		goto no_lock;
5114f8829a4aSRandall Stewart 	}
5115f8829a4aSRandall Stewart 	SCTP_INP_INCR_REF(stcb->sctp_ep);
5116f8829a4aSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5117f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5118f8829a4aSRandall Stewart 		goto out;
5119f8829a4aSRandall Stewart 	}
5120f8829a4aSRandall Stewart 	so = stcb->sctp_socket;
5121f8829a4aSRandall Stewart 	if (so == NULL) {
5122f8829a4aSRandall Stewart 		goto out;
5123f8829a4aSRandall Stewart 	}
5124f8829a4aSRandall Stewart 	atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far);
5125f8829a4aSRandall Stewart 	/* Have you have freed enough to look */
5126f8829a4aSRandall Stewart 	*freed_so_far = 0;
5127f8829a4aSRandall Stewart 	/* Yep, its worth a look and the lock overhead */
5128f8829a4aSRandall Stewart 
5129f8829a4aSRandall Stewart 	/* Figure out what the rwnd would be */
5130f8829a4aSRandall Stewart 	rwnd = sctp_calc_rwnd(stcb, &stcb->asoc);
5131f8829a4aSRandall Stewart 	if (rwnd >= stcb->asoc.my_last_reported_rwnd) {
5132f8829a4aSRandall Stewart 		dif = rwnd - stcb->asoc.my_last_reported_rwnd;
5133f8829a4aSRandall Stewart 	} else {
5134f8829a4aSRandall Stewart 		dif = 0;
5135f8829a4aSRandall Stewart 	}
5136f8829a4aSRandall Stewart 	if (dif >= rwnd_req) {
5137f8829a4aSRandall Stewart 		if (hold_rlock) {
5138f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5139f8829a4aSRandall Stewart 			r_unlocked = 1;
5140f8829a4aSRandall Stewart 		}
5141f8829a4aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5142f8829a4aSRandall Stewart 			/*
5143f8829a4aSRandall Stewart 			 * One last check before we allow the guy possibly
5144f8829a4aSRandall Stewart 			 * to get in. There is a race, where the guy has not
5145f8829a4aSRandall Stewart 			 * reached the gate. In that case
5146f8829a4aSRandall Stewart 			 */
5147f8829a4aSRandall Stewart 			goto out;
5148f8829a4aSRandall Stewart 		}
5149f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
5150f8829a4aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5151f8829a4aSRandall Stewart 			/* No reports here */
5152f8829a4aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
5153f8829a4aSRandall Stewart 			goto out;
5154f8829a4aSRandall Stewart 		}
5155f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_wu_sacks_sent);
5156689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_LOCKED);
5157830d754dSRandall Stewart 
5158f8829a4aSRandall Stewart 		sctp_chunk_output(stcb->sctp_ep, stcb,
5159ceaad40aSRandall Stewart 		    SCTP_OUTPUT_FROM_USR_RCVD, SCTP_SO_LOCKED);
5160f8829a4aSRandall Stewart 		/* make sure no timer is running */
5161a5d547adSRandall Stewart 		sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_6);
5162f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
5163f8829a4aSRandall Stewart 	} else {
5164f8829a4aSRandall Stewart 		/* Update how much we have pending */
5165f8829a4aSRandall Stewart 		stcb->freed_by_sorcv_sincelast = dif;
5166f8829a4aSRandall Stewart 	}
5167f8829a4aSRandall Stewart out:
5168f8829a4aSRandall Stewart 	if (so && r_unlocked && hold_rlock) {
5169f8829a4aSRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
5170f8829a4aSRandall Stewart 	}
5171f8829a4aSRandall Stewart 	SCTP_INP_DECR_REF(stcb->sctp_ep);
5172f8829a4aSRandall Stewart no_lock:
517350cec919SRandall Stewart 	atomic_add_int(&stcb->asoc.refcnt, -1);
5174f8829a4aSRandall Stewart 	return;
5175f8829a4aSRandall Stewart }
5176f8829a4aSRandall Stewart 
5177f8829a4aSRandall Stewart int
5178f8829a4aSRandall Stewart sctp_sorecvmsg(struct socket *so,
5179f8829a4aSRandall Stewart     struct uio *uio,
5180f8829a4aSRandall Stewart     struct mbuf **mp,
5181f8829a4aSRandall Stewart     struct sockaddr *from,
5182f8829a4aSRandall Stewart     int fromlen,
5183f8829a4aSRandall Stewart     int *msg_flags,
5184f8829a4aSRandall Stewart     struct sctp_sndrcvinfo *sinfo,
5185f8829a4aSRandall Stewart     int filling_sinfo)
5186f8829a4aSRandall Stewart {
5187f8829a4aSRandall Stewart 	/*
5188f8829a4aSRandall Stewart 	 * MSG flags we will look at MSG_DONTWAIT - non-blocking IO.
5189f8829a4aSRandall Stewart 	 * MSG_PEEK - Look don't touch :-D (only valid with OUT mbuf copy
5190f8829a4aSRandall Stewart 	 * mp=NULL thus uio is the copy method to userland) MSG_WAITALL - ??
5191f8829a4aSRandall Stewart 	 * On the way out we may send out any combination of:
5192f8829a4aSRandall Stewart 	 * MSG_NOTIFICATION MSG_EOR
5193f8829a4aSRandall Stewart 	 *
5194f8829a4aSRandall Stewart 	 */
5195f8829a4aSRandall Stewart 	struct sctp_inpcb *inp = NULL;
5196f8829a4aSRandall Stewart 	int my_len = 0;
5197f8829a4aSRandall Stewart 	int cp_len = 0, error = 0;
5198f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL;
519994b0d969SMichael Tuexen 	struct mbuf *m = NULL;
5200f8829a4aSRandall Stewart 	struct sctp_tcb *stcb = NULL;
5201f8829a4aSRandall Stewart 	int wakeup_read_socket = 0;
5202f8829a4aSRandall Stewart 	int freecnt_applied = 0;
5203f8829a4aSRandall Stewart 	int out_flags = 0, in_flags = 0;
5204f8829a4aSRandall Stewart 	int block_allowed = 1;
52054c9179adSRandall Stewart 	uint32_t freed_so_far = 0;
520681aca91aSRandall Stewart 	uint32_t copied_so_far = 0;
520793164cf9SRandall Stewart 	int in_eeor_mode = 0;
5208f8829a4aSRandall Stewart 	int no_rcv_needed = 0;
5209f8829a4aSRandall Stewart 	uint32_t rwnd_req = 0;
5210f8829a4aSRandall Stewart 	int hold_sblock = 0;
5211f8829a4aSRandall Stewart 	int hold_rlock = 0;
521242551e99SRandall Stewart 	int slen = 0;
52134c9179adSRandall Stewart 	uint32_t held_length = 0;
52147abab911SRobert Watson 	int sockbuf_lock = 0;
5215f8829a4aSRandall Stewart 
521617205eccSRandall Stewart 	if (uio == NULL) {
5217c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
521817205eccSRandall Stewart 		return (EINVAL);
521917205eccSRandall Stewart 	}
5220f8829a4aSRandall Stewart 	if (msg_flags) {
5221f8829a4aSRandall Stewart 		in_flags = *msg_flags;
5222c105859eSRandall Stewart 		if (in_flags & MSG_PEEK)
5223c105859eSRandall Stewart 			SCTP_STAT_INCR(sctps_read_peeks);
5224f8829a4aSRandall Stewart 	} else {
5225f8829a4aSRandall Stewart 		in_flags = 0;
5226f8829a4aSRandall Stewart 	}
5227f8829a4aSRandall Stewart 	slen = uio->uio_resid;
522817205eccSRandall Stewart 
5229f8829a4aSRandall Stewart 	/* Pull in and set up our int flags */
5230f8829a4aSRandall Stewart 	if (in_flags & MSG_OOB) {
5231f8829a4aSRandall Stewart 		/* Out of band's NOT supported */
5232f8829a4aSRandall Stewart 		return (EOPNOTSUPP);
5233f8829a4aSRandall Stewart 	}
5234f8829a4aSRandall Stewart 	if ((in_flags & MSG_PEEK) && (mp != NULL)) {
5235c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
5236f8829a4aSRandall Stewart 		return (EINVAL);
5237f8829a4aSRandall Stewart 	}
5238f8829a4aSRandall Stewart 	if ((in_flags & (MSG_DONTWAIT
5239f8829a4aSRandall Stewart 	    | MSG_NBIO
5240f8829a4aSRandall Stewart 	    )) ||
524142551e99SRandall Stewart 	    SCTP_SO_IS_NBIO(so)) {
5242f8829a4aSRandall Stewart 		block_allowed = 0;
5243f8829a4aSRandall Stewart 	}
5244f8829a4aSRandall Stewart 	/* setup the endpoint */
5245f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
5246f8829a4aSRandall Stewart 	if (inp == NULL) {
5247c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
5248f8829a4aSRandall Stewart 		return (EFAULT);
5249f8829a4aSRandall Stewart 	}
525062c1ff9cSRandall Stewart 	rwnd_req = (SCTP_SB_LIMIT_RCV(so) >> SCTP_RWND_HIWAT_SHIFT);
5251f8829a4aSRandall Stewart 	/* Must be at least a MTU's worth */
5252f8829a4aSRandall Stewart 	if (rwnd_req < SCTP_MIN_RWND)
5253f8829a4aSRandall Stewart 		rwnd_req = SCTP_MIN_RWND;
5254f8829a4aSRandall Stewart 	in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
5255b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5256f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SORECV_ENTER,
525717205eccSRandall Stewart 		    rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, uio->uio_resid);
525880fefe0aSRandall Stewart 	}
5259b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
5260f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SORECV_ENTERPL,
526117205eccSRandall Stewart 		    rwnd_req, block_allowed, so->so_rcv.sb_cc, uio->uio_resid);
526280fefe0aSRandall Stewart 	}
5263265de5bbSRobert Watson 	error = sblock(&so->so_rcv, (block_allowed ? SBL_WAIT : 0));
5264f8829a4aSRandall Stewart 	if (error) {
5265f8829a4aSRandall Stewart 		goto release_unlocked;
5266f8829a4aSRandall Stewart 	}
52678e1e6e5fSMateusz Guzik 	sockbuf_lock = 1;
5268f8829a4aSRandall Stewart restart:
52697abab911SRobert Watson 
5270f8829a4aSRandall Stewart 
5271f8829a4aSRandall Stewart restart_nosblocks:
5272f8829a4aSRandall Stewart 	if (hold_sblock == 0) {
5273f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&so->so_rcv);
5274f8829a4aSRandall Stewart 		hold_sblock = 1;
5275f8829a4aSRandall Stewart 	}
5276f8829a4aSRandall Stewart 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5277f8829a4aSRandall Stewart 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5278f8829a4aSRandall Stewart 		goto out;
5279f8829a4aSRandall Stewart 	}
528058411b08SMichael Tuexen 	if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) && (so->so_rcv.sb_cc == 0)) {
5281f8829a4aSRandall Stewart 		if (so->so_error) {
5282f8829a4aSRandall Stewart 			error = so->so_error;
528344b7479bSRandall Stewart 			if ((in_flags & MSG_PEEK) == 0)
528444b7479bSRandall Stewart 				so->so_error = 0;
52859f22f500SRandall Stewart 			goto out;
5286f8829a4aSRandall Stewart 		} else {
52879f22f500SRandall Stewart 			if (so->so_rcv.sb_cc == 0) {
52887924093fSRandall Stewart 				/* indicate EOF */
52897924093fSRandall Stewart 				error = 0;
5290f8829a4aSRandall Stewart 				goto out;
5291f8829a4aSRandall Stewart 			}
52929f22f500SRandall Stewart 		}
52939f22f500SRandall Stewart 	}
5294f8829a4aSRandall Stewart 	if ((so->so_rcv.sb_cc <= held_length) && block_allowed) {
5295f8829a4aSRandall Stewart 		/* we need to wait for data */
5296f8829a4aSRandall Stewart 		if ((so->so_rcv.sb_cc == 0) &&
5297f8829a4aSRandall Stewart 		    ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5298f8829a4aSRandall Stewart 		    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
5299f8829a4aSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
5300f8829a4aSRandall Stewart 				/*
5301f8829a4aSRandall Stewart 				 * For active open side clear flags for
5302f8829a4aSRandall Stewart 				 * re-use passive open is blocked by
5303f8829a4aSRandall Stewart 				 * connect.
5304f8829a4aSRandall Stewart 				 */
5305f8829a4aSRandall Stewart 				if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
5306f8829a4aSRandall Stewart 					/*
5307f8829a4aSRandall Stewart 					 * You were aborted, passive side
5308f8829a4aSRandall Stewart 					 * always hits here
5309f8829a4aSRandall Stewart 					 */
5310c4739e2fSRandall Stewart 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
5311f8829a4aSRandall Stewart 					error = ECONNRESET;
5312f8829a4aSRandall Stewart 				}
5313f8829a4aSRandall Stewart 				so->so_state &= ~(SS_ISCONNECTING |
5314f8829a4aSRandall Stewart 				    SS_ISDISCONNECTING |
5315f8829a4aSRandall Stewart 				    SS_ISCONFIRMING |
5316f8829a4aSRandall Stewart 				    SS_ISCONNECTED);
5317f8829a4aSRandall Stewart 				if (error == 0) {
5318f8829a4aSRandall Stewart 					if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5319c4739e2fSRandall Stewart 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
5320f8829a4aSRandall Stewart 						error = ENOTCONN;
5321f8829a4aSRandall Stewart 					}
5322f8829a4aSRandall Stewart 				}
5323f8829a4aSRandall Stewart 				goto out;
5324f8829a4aSRandall Stewart 			}
5325f8829a4aSRandall Stewart 		}
5326f8829a4aSRandall Stewart 		error = sbwait(&so->so_rcv);
5327f8829a4aSRandall Stewart 		if (error) {
5328f8829a4aSRandall Stewart 			goto out;
5329f8829a4aSRandall Stewart 		}
5330f8829a4aSRandall Stewart 		held_length = 0;
5331f8829a4aSRandall Stewart 		goto restart_nosblocks;
5332f8829a4aSRandall Stewart 	} else if (so->so_rcv.sb_cc == 0) {
533344b7479bSRandall Stewart 		if (so->so_error) {
533444b7479bSRandall Stewart 			error = so->so_error;
533544b7479bSRandall Stewart 			if ((in_flags & MSG_PEEK) == 0)
533644b7479bSRandall Stewart 				so->so_error = 0;
533744b7479bSRandall Stewart 		} else {
533844b7479bSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
533944b7479bSRandall Stewart 			    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
534044b7479bSRandall Stewart 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
534144b7479bSRandall Stewart 					/*
534244b7479bSRandall Stewart 					 * For active open side clear flags
534344b7479bSRandall Stewart 					 * for re-use passive open is
534444b7479bSRandall Stewart 					 * blocked by connect.
534544b7479bSRandall Stewart 					 */
534644b7479bSRandall Stewart 					if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
534744b7479bSRandall Stewart 						/*
534844b7479bSRandall Stewart 						 * You were aborted, passive
534944b7479bSRandall Stewart 						 * side always hits here
535044b7479bSRandall Stewart 						 */
5351c4739e2fSRandall Stewart 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
535244b7479bSRandall Stewart 						error = ECONNRESET;
535344b7479bSRandall Stewart 					}
535444b7479bSRandall Stewart 					so->so_state &= ~(SS_ISCONNECTING |
535544b7479bSRandall Stewart 					    SS_ISDISCONNECTING |
535644b7479bSRandall Stewart 					    SS_ISCONFIRMING |
535744b7479bSRandall Stewart 					    SS_ISCONNECTED);
535844b7479bSRandall Stewart 					if (error == 0) {
535944b7479bSRandall Stewart 						if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
5360c4739e2fSRandall Stewart 							SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
536144b7479bSRandall Stewart 							error = ENOTCONN;
536244b7479bSRandall Stewart 						}
536344b7479bSRandall Stewart 					}
536444b7479bSRandall Stewart 					goto out;
536544b7479bSRandall Stewart 				}
536644b7479bSRandall Stewart 			}
5367c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EWOULDBLOCK);
5368f8829a4aSRandall Stewart 			error = EWOULDBLOCK;
536944b7479bSRandall Stewart 		}
5370f8829a4aSRandall Stewart 		goto out;
5371f8829a4aSRandall Stewart 	}
5372d06c82f1SRandall Stewart 	if (hold_sblock == 1) {
5373d06c82f1SRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
5374d06c82f1SRandall Stewart 		hold_sblock = 0;
5375d06c82f1SRandall Stewart 	}
5376f8829a4aSRandall Stewart 	/* we possibly have data we can read */
53773c503c28SRandall Stewart 	/* sa_ignore FREED_MEMORY */
5378f8829a4aSRandall Stewart 	control = TAILQ_FIRST(&inp->read_queue);
5379f8829a4aSRandall Stewart 	if (control == NULL) {
5380f8829a4aSRandall Stewart 		/*
5381f8829a4aSRandall Stewart 		 * This could be happening since the appender did the
5382f8829a4aSRandall Stewart 		 * increment but as not yet did the tailq insert onto the
5383f8829a4aSRandall Stewart 		 * read_queue
5384f8829a4aSRandall Stewart 		 */
5385f8829a4aSRandall Stewart 		if (hold_rlock == 0) {
5386f8829a4aSRandall Stewart 			SCTP_INP_READ_LOCK(inp);
5387f8829a4aSRandall Stewart 		}
5388f8829a4aSRandall Stewart 		control = TAILQ_FIRST(&inp->read_queue);
5389f8829a4aSRandall Stewart 		if ((control == NULL) && (so->so_rcv.sb_cc != 0)) {
5390a5d547adSRandall Stewart #ifdef INVARIANTS
5391f8829a4aSRandall Stewart 			panic("Huh, its non zero and nothing on control?");
5392f8829a4aSRandall Stewart #endif
5393f8829a4aSRandall Stewart 			so->so_rcv.sb_cc = 0;
5394f8829a4aSRandall Stewart 		}
5395f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
5396f8829a4aSRandall Stewart 		hold_rlock = 0;
5397f8829a4aSRandall Stewart 		goto restart;
5398f8829a4aSRandall Stewart 	}
5399f8829a4aSRandall Stewart 	if ((control->length == 0) &&
5400f8829a4aSRandall Stewart 	    (control->do_not_ref_stcb)) {
5401f8829a4aSRandall Stewart 		/*
5402f8829a4aSRandall Stewart 		 * Clean up code for freeing assoc that left behind a
5403f8829a4aSRandall Stewart 		 * pdapi.. maybe a peer in EEOR that just closed after
5404f8829a4aSRandall Stewart 		 * sending and never indicated a EOR.
5405f8829a4aSRandall Stewart 		 */
5406f8829a4aSRandall Stewart 		if (hold_rlock == 0) {
5407f8829a4aSRandall Stewart 			hold_rlock = 1;
5408f8829a4aSRandall Stewart 			SCTP_INP_READ_LOCK(inp);
5409f8829a4aSRandall Stewart 		}
5410f8829a4aSRandall Stewart 		control->held_length = 0;
5411f8829a4aSRandall Stewart 		if (control->data) {
5412f8829a4aSRandall Stewart 			/* Hmm there is data here .. fix */
54134c9179adSRandall Stewart 			struct mbuf *m_tmp;
5414f8829a4aSRandall Stewart 			int cnt = 0;
5415f8829a4aSRandall Stewart 
54164c9179adSRandall Stewart 			m_tmp = control->data;
54174c9179adSRandall Stewart 			while (m_tmp) {
54184c9179adSRandall Stewart 				cnt += SCTP_BUF_LEN(m_tmp);
54194c9179adSRandall Stewart 				if (SCTP_BUF_NEXT(m_tmp) == NULL) {
54204c9179adSRandall Stewart 					control->tail_mbuf = m_tmp;
5421f8829a4aSRandall Stewart 					control->end_added = 1;
5422f8829a4aSRandall Stewart 				}
54234c9179adSRandall Stewart 				m_tmp = SCTP_BUF_NEXT(m_tmp);
5424f8829a4aSRandall Stewart 			}
5425f8829a4aSRandall Stewart 			control->length = cnt;
5426f8829a4aSRandall Stewart 		} else {
5427f8829a4aSRandall Stewart 			/* remove it */
5428f8829a4aSRandall Stewart 			TAILQ_REMOVE(&inp->read_queue, control, next);
5429f8829a4aSRandall Stewart 			/* Add back any hiddend data */
5430f8829a4aSRandall Stewart 			sctp_free_remote_addr(control->whoFrom);
5431f8829a4aSRandall Stewart 			sctp_free_a_readq(stcb, control);
5432f8829a4aSRandall Stewart 		}
5433f8829a4aSRandall Stewart 		if (hold_rlock) {
5434f8829a4aSRandall Stewart 			hold_rlock = 0;
5435f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
5436f8829a4aSRandall Stewart 		}
5437f8829a4aSRandall Stewart 		goto restart;
5438f8829a4aSRandall Stewart 	}
5439810ec536SMichael Tuexen 	if ((control->length == 0) &&
5440810ec536SMichael Tuexen 	    (control->end_added == 1)) {
5441810ec536SMichael Tuexen 		/*
5442810ec536SMichael Tuexen 		 * Do we also need to check for (control->pdapi_aborted ==
5443810ec536SMichael Tuexen 		 * 1)?
5444810ec536SMichael Tuexen 		 */
5445810ec536SMichael Tuexen 		if (hold_rlock == 0) {
5446810ec536SMichael Tuexen 			hold_rlock = 1;
5447810ec536SMichael Tuexen 			SCTP_INP_READ_LOCK(inp);
5448810ec536SMichael Tuexen 		}
5449810ec536SMichael Tuexen 		TAILQ_REMOVE(&inp->read_queue, control, next);
5450810ec536SMichael Tuexen 		if (control->data) {
5451810ec536SMichael Tuexen #ifdef INVARIANTS
5452810ec536SMichael Tuexen 			panic("control->data not null but control->length == 0");
5453810ec536SMichael Tuexen #else
5454810ec536SMichael Tuexen 			SCTP_PRINTF("Strange, data left in the control buffer. Cleaning up.\n");
5455810ec536SMichael Tuexen 			sctp_m_freem(control->data);
5456810ec536SMichael Tuexen 			control->data = NULL;
5457810ec536SMichael Tuexen #endif
5458810ec536SMichael Tuexen 		}
5459810ec536SMichael Tuexen 		if (control->aux_data) {
5460810ec536SMichael Tuexen 			sctp_m_free(control->aux_data);
5461810ec536SMichael Tuexen 			control->aux_data = NULL;
5462810ec536SMichael Tuexen 		}
5463810ec536SMichael Tuexen 		sctp_free_remote_addr(control->whoFrom);
5464810ec536SMichael Tuexen 		sctp_free_a_readq(stcb, control);
5465810ec536SMichael Tuexen 		if (hold_rlock) {
5466810ec536SMichael Tuexen 			hold_rlock = 0;
5467810ec536SMichael Tuexen 			SCTP_INP_READ_UNLOCK(inp);
5468810ec536SMichael Tuexen 		}
5469810ec536SMichael Tuexen 		goto restart;
5470810ec536SMichael Tuexen 	}
5471f8829a4aSRandall Stewart 	if (control->length == 0) {
5472f8829a4aSRandall Stewart 		if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
5473f8829a4aSRandall Stewart 		    (filling_sinfo)) {
5474f8829a4aSRandall Stewart 			/* find a more suitable one then this */
5475f8829a4aSRandall Stewart 			ctl = TAILQ_NEXT(control, next);
5476f8829a4aSRandall Stewart 			while (ctl) {
54779a6142d8SRandall Stewart 				if ((ctl->stcb != control->stcb) && (ctl->length) &&
54789a6142d8SRandall Stewart 				    (ctl->some_taken ||
54796114cd96SRandall Stewart 				    (ctl->spec_flags & M_NOTIFICATION) ||
54809a6142d8SRandall Stewart 				    ((ctl->do_not_ref_stcb == 0) &&
54819a6142d8SRandall Stewart 				    (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))
54829a6142d8SRandall Stewart 				    ) {
54839a6142d8SRandall Stewart 					/*-
54849a6142d8SRandall Stewart 					 * If we have a different TCB next, and there is data
54859a6142d8SRandall Stewart 					 * present. If we have already taken some (pdapi), OR we can
54869a6142d8SRandall Stewart 					 * ref the tcb and no delivery as started on this stream, we
548717205eccSRandall Stewart 					 * take it. Note we allow a notification on a different
548817205eccSRandall Stewart 					 * assoc to be delivered..
54899a6142d8SRandall Stewart 					 */
54909a6142d8SRandall Stewart 					control = ctl;
54919a6142d8SRandall Stewart 					goto found_one;
54929a6142d8SRandall Stewart 				} else if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) &&
54939a6142d8SRandall Stewart 					    (ctl->length) &&
54949a6142d8SRandall Stewart 					    ((ctl->some_taken) ||
54959a6142d8SRandall Stewart 					    ((ctl->do_not_ref_stcb == 0) &&
549617205eccSRandall Stewart 					    ((ctl->spec_flags & M_NOTIFICATION) == 0) &&
5497b5c16493SMichael Tuexen 				    (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))) {
54989a6142d8SRandall Stewart 					/*-
54999a6142d8SRandall Stewart 					 * If we have the same tcb, and there is data present, and we
55009a6142d8SRandall Stewart 					 * have the strm interleave feature present. Then if we have
55019a6142d8SRandall Stewart 					 * taken some (pdapi) or we can refer to tht tcb AND we have
55029a6142d8SRandall Stewart 					 * not started a delivery for this stream, we can take it.
550317205eccSRandall Stewart 					 * Note we do NOT allow a notificaiton on the same assoc to
550417205eccSRandall Stewart 					 * be delivered.
55059a6142d8SRandall Stewart 					 */
5506f8829a4aSRandall Stewart 					control = ctl;
5507f8829a4aSRandall Stewart 					goto found_one;
5508f8829a4aSRandall Stewart 				}
5509f8829a4aSRandall Stewart 				ctl = TAILQ_NEXT(ctl, next);
5510f8829a4aSRandall Stewart 			}
5511f8829a4aSRandall Stewart 		}
5512f8829a4aSRandall Stewart 		/*
5513f8829a4aSRandall Stewart 		 * if we reach here, not suitable replacement is available
5514f8829a4aSRandall Stewart 		 * <or> fragment interleave is NOT on. So stuff the sb_cc
5515f8829a4aSRandall Stewart 		 * into the our held count, and its time to sleep again.
5516f8829a4aSRandall Stewart 		 */
5517f8829a4aSRandall Stewart 		held_length = so->so_rcv.sb_cc;
5518f8829a4aSRandall Stewart 		control->held_length = so->so_rcv.sb_cc;
5519f8829a4aSRandall Stewart 		goto restart;
5520f8829a4aSRandall Stewart 	}
5521f8829a4aSRandall Stewart 	/* Clear the held length since there is something to read */
5522f8829a4aSRandall Stewart 	control->held_length = 0;
5523f8829a4aSRandall Stewart 	if (hold_rlock) {
5524f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
5525f8829a4aSRandall Stewart 		hold_rlock = 0;
5526f8829a4aSRandall Stewart 	}
5527f8829a4aSRandall Stewart found_one:
5528f8829a4aSRandall Stewart 	/*
5529f8829a4aSRandall Stewart 	 * If we reach here, control has a some data for us to read off.
5530f8829a4aSRandall Stewart 	 * Note that stcb COULD be NULL.
5531f8829a4aSRandall Stewart 	 */
55329c04b296SRandall Stewart 	control->some_taken++;
5533f8829a4aSRandall Stewart 	if (hold_sblock) {
5534f8829a4aSRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
5535f8829a4aSRandall Stewart 		hold_sblock = 0;
5536f8829a4aSRandall Stewart 	}
5537f8829a4aSRandall Stewart 	stcb = control->stcb;
5538f8829a4aSRandall Stewart 	if (stcb) {
55390696e120SRandall Stewart 		if ((control->do_not_ref_stcb == 0) &&
55400696e120SRandall Stewart 		    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
554150cec919SRandall Stewart 			if (freecnt_applied == 0)
5542f8829a4aSRandall Stewart 				stcb = NULL;
5543f8829a4aSRandall Stewart 		} else if (control->do_not_ref_stcb == 0) {
5544f8829a4aSRandall Stewart 			/* you can't free it on me please */
5545f8829a4aSRandall Stewart 			/*
5546f8829a4aSRandall Stewart 			 * The lock on the socket buffer protects us so the
5547f8829a4aSRandall Stewart 			 * free code will stop. But since we used the
5548f8829a4aSRandall Stewart 			 * socketbuf lock and the sender uses the tcb_lock
5549f8829a4aSRandall Stewart 			 * to increment, we need to use the atomic add to
5550f8829a4aSRandall Stewart 			 * the refcnt
5551f8829a4aSRandall Stewart 			 */
5552d55b0b1bSRandall Stewart 			if (freecnt_applied) {
5553d55b0b1bSRandall Stewart #ifdef INVARIANTS
5554207304d4SRandall Stewart 				panic("refcnt already incremented");
5555d55b0b1bSRandall Stewart #else
5556cd3fd531SMichael Tuexen 				SCTP_PRINTF("refcnt already incremented?\n");
5557d55b0b1bSRandall Stewart #endif
5558d55b0b1bSRandall Stewart 			} else {
555950cec919SRandall Stewart 				atomic_add_int(&stcb->asoc.refcnt, 1);
5560f8829a4aSRandall Stewart 				freecnt_applied = 1;
5561d55b0b1bSRandall Stewart 			}
5562f8829a4aSRandall Stewart 			/*
5563f8829a4aSRandall Stewart 			 * Setup to remember how much we have not yet told
5564f8829a4aSRandall Stewart 			 * the peer our rwnd has opened up. Note we grab the
5565f8829a4aSRandall Stewart 			 * value from the tcb from last time. Note too that
55660696e120SRandall Stewart 			 * sack sending clears this when a sack is sent,
5567f8829a4aSRandall Stewart 			 * which is fine. Once we hit the rwnd_req, we then
5568f8829a4aSRandall Stewart 			 * will go to the sctp_user_rcvd() that will not
5569f8829a4aSRandall Stewart 			 * lock until it KNOWs it MUST send a WUP-SACK.
5570f8829a4aSRandall Stewart 			 */
5571f8829a4aSRandall Stewart 			freed_so_far = stcb->freed_by_sorcv_sincelast;
5572f8829a4aSRandall Stewart 			stcb->freed_by_sorcv_sincelast = 0;
5573f8829a4aSRandall Stewart 		}
5574f8829a4aSRandall Stewart 	}
55756114cd96SRandall Stewart 	if (stcb &&
55766114cd96SRandall Stewart 	    ((control->spec_flags & M_NOTIFICATION) == 0) &&
55776114cd96SRandall Stewart 	    control->do_not_ref_stcb == 0) {
5578d06c82f1SRandall Stewart 		stcb->asoc.strmin[control->sinfo_stream].delivery_started = 1;
5579d06c82f1SRandall Stewart 	}
5580f8829a4aSRandall Stewart 	/* First lets get off the sinfo and sockaddr info */
5581f8829a4aSRandall Stewart 	if ((sinfo) && filling_sinfo) {
5582f8829a4aSRandall Stewart 		memcpy(sinfo, control, sizeof(struct sctp_nonpad_sndrcvinfo));
5583f8829a4aSRandall Stewart 		nxt = TAILQ_NEXT(control, next);
5584e2e7c62eSMichael Tuexen 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
5585e2e7c62eSMichael Tuexen 		    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) {
5586f8829a4aSRandall Stewart 			struct sctp_extrcvinfo *s_extra;
5587f8829a4aSRandall Stewart 
5588f8829a4aSRandall Stewart 			s_extra = (struct sctp_extrcvinfo *)sinfo;
55899a6142d8SRandall Stewart 			if ((nxt) &&
55909a6142d8SRandall Stewart 			    (nxt->length)) {
55919a6142d8SRandall Stewart 				s_extra->sreinfo_next_flags = SCTP_NEXT_MSG_AVAIL;
5592f8829a4aSRandall Stewart 				if (nxt->sinfo_flags & SCTP_UNORDERED) {
55939a6142d8SRandall Stewart 					s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_IS_UNORDERED;
5594f8829a4aSRandall Stewart 				}
5595f42a358aSRandall Stewart 				if (nxt->spec_flags & M_NOTIFICATION) {
55969a6142d8SRandall Stewart 					s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_IS_NOTIFICATION;
5597f42a358aSRandall Stewart 				}
55989a6142d8SRandall Stewart 				s_extra->sreinfo_next_aid = nxt->sinfo_assoc_id;
55999a6142d8SRandall Stewart 				s_extra->sreinfo_next_length = nxt->length;
56009a6142d8SRandall Stewart 				s_extra->sreinfo_next_ppid = nxt->sinfo_ppid;
56019a6142d8SRandall Stewart 				s_extra->sreinfo_next_stream = nxt->sinfo_stream;
5602f8829a4aSRandall Stewart 				if (nxt->tail_mbuf != NULL) {
5603139bc87fSRandall Stewart 					if (nxt->end_added) {
56049a6142d8SRandall Stewart 						s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_ISCOMPLETE;
5605f8829a4aSRandall Stewart 					}
5606f8829a4aSRandall Stewart 				}
5607f8829a4aSRandall Stewart 			} else {
5608f8829a4aSRandall Stewart 				/*
5609f8829a4aSRandall Stewart 				 * we explicitly 0 this, since the memcpy
5610f8829a4aSRandall Stewart 				 * got some other things beyond the older
5611f8829a4aSRandall Stewart 				 * sinfo_ that is on the control's structure
5612f8829a4aSRandall Stewart 				 * :-D
5613f8829a4aSRandall Stewart 				 */
56149a6142d8SRandall Stewart 				nxt = NULL;
56159a6142d8SRandall Stewart 				s_extra->sreinfo_next_flags = SCTP_NO_NEXT_MSG;
56169a6142d8SRandall Stewart 				s_extra->sreinfo_next_aid = 0;
56179a6142d8SRandall Stewart 				s_extra->sreinfo_next_length = 0;
56189a6142d8SRandall Stewart 				s_extra->sreinfo_next_ppid = 0;
56199a6142d8SRandall Stewart 				s_extra->sreinfo_next_stream = 0;
5620f8829a4aSRandall Stewart 			}
5621f8829a4aSRandall Stewart 		}
5622f8829a4aSRandall Stewart 		/*
5623f8829a4aSRandall Stewart 		 * update off the real current cum-ack, if we have an stcb.
5624f8829a4aSRandall Stewart 		 */
56250696e120SRandall Stewart 		if ((control->do_not_ref_stcb == 0) && stcb)
5626f8829a4aSRandall Stewart 			sinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
5627f8829a4aSRandall Stewart 		/*
5628f8829a4aSRandall Stewart 		 * mask off the high bits, we keep the actual chunk bits in
5629f8829a4aSRandall Stewart 		 * there.
5630f8829a4aSRandall Stewart 		 */
5631f8829a4aSRandall Stewart 		sinfo->sinfo_flags &= 0x00ff;
56325f26a41dSRandall Stewart 		if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) {
56335f26a41dSRandall Stewart 			sinfo->sinfo_flags |= SCTP_UNORDERED;
56345f26a41dSRandall Stewart 		}
5635f8829a4aSRandall Stewart 	}
563618e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
563718e198d3SRandall Stewart 	{
563818e198d3SRandall Stewart 		int index, newindex;
563918e198d3SRandall Stewart 		struct sctp_pcbtsn_rlog *entry;
564018e198d3SRandall Stewart 
564118e198d3SRandall Stewart 		do {
564218e198d3SRandall Stewart 			index = inp->readlog_index;
564318e198d3SRandall Stewart 			newindex = index + 1;
564418e198d3SRandall Stewart 			if (newindex >= SCTP_READ_LOG_SIZE) {
564518e198d3SRandall Stewart 				newindex = 0;
564618e198d3SRandall Stewart 			}
564718e198d3SRandall Stewart 		} while (atomic_cmpset_int(&inp->readlog_index, index, newindex) == 0);
564818e198d3SRandall Stewart 		entry = &inp->readlog[index];
564918e198d3SRandall Stewart 		entry->vtag = control->sinfo_assoc_id;
565018e198d3SRandall Stewart 		entry->strm = control->sinfo_stream;
565118e198d3SRandall Stewart 		entry->seq = control->sinfo_ssn;
565218e198d3SRandall Stewart 		entry->sz = control->length;
565318e198d3SRandall Stewart 		entry->flgs = control->sinfo_flags;
565418e198d3SRandall Stewart 	}
565518e198d3SRandall Stewart #endif
5656f8829a4aSRandall Stewart 	if (fromlen && from) {
5657b5b6e5c2SMichael Tuexen 		cp_len = min((size_t)fromlen, (size_t)control->whoFrom->ro._l_addr.sa.sa_len);
5658b5b6e5c2SMichael Tuexen 		switch (control->whoFrom->ro._l_addr.sa.sa_family) {
5659b5b6e5c2SMichael Tuexen #ifdef INET6
5660b5b6e5c2SMichael Tuexen 		case AF_INET6:
5661f8829a4aSRandall Stewart 			((struct sockaddr_in6 *)from)->sin6_port = control->port_from;
5662b5b6e5c2SMichael Tuexen 			break;
5663f8829a4aSRandall Stewart #endif
5664b5b6e5c2SMichael Tuexen #ifdef INET
5665b5b6e5c2SMichael Tuexen 		case AF_INET:
5666b5b6e5c2SMichael Tuexen 			((struct sockaddr_in *)from)->sin_port = control->port_from;
5667b5b6e5c2SMichael Tuexen 			break;
5668b5b6e5c2SMichael Tuexen #endif
5669b5b6e5c2SMichael Tuexen 		default:
5670b5b6e5c2SMichael Tuexen 			break;
5671b5b6e5c2SMichael Tuexen 		}
5672b5b6e5c2SMichael Tuexen 		memcpy(from, &control->whoFrom->ro._l_addr, cp_len);
5673f8829a4aSRandall Stewart 
567442551e99SRandall Stewart #if defined(INET) && defined(INET6)
56755e2c2d87SRandall Stewart 		if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
5676b5b6e5c2SMichael Tuexen 		    (from->sa_family == AF_INET) &&
5677f8829a4aSRandall Stewart 		    ((size_t)fromlen >= sizeof(struct sockaddr_in6))) {
5678f8829a4aSRandall Stewart 			struct sockaddr_in *sin;
5679f8829a4aSRandall Stewart 			struct sockaddr_in6 sin6;
5680f8829a4aSRandall Stewart 
5681b5b6e5c2SMichael Tuexen 			sin = (struct sockaddr_in *)from;
5682f8829a4aSRandall Stewart 			bzero(&sin6, sizeof(sin6));
5683f8829a4aSRandall Stewart 			sin6.sin6_family = AF_INET6;
5684f8829a4aSRandall Stewart 			sin6.sin6_len = sizeof(struct sockaddr_in6);
5685d6af161aSRandall Stewart 			sin6.sin6_addr.s6_addr32[2] = htonl(0xffff);
5686f8829a4aSRandall Stewart 			bcopy(&sin->sin_addr,
5687d6af161aSRandall Stewart 			    &sin6.sin6_addr.s6_addr32[3],
5688d6af161aSRandall Stewart 			    sizeof(sin6.sin6_addr.s6_addr32[3]));
5689f8829a4aSRandall Stewart 			sin6.sin6_port = sin->sin_port;
5690b5b6e5c2SMichael Tuexen 			memcpy(from, &sin6, sizeof(struct sockaddr_in6));
5691f8829a4aSRandall Stewart 		}
5692f8829a4aSRandall Stewart #endif
5693e0e00a4dSMichael Tuexen #ifdef INET6
5694f8829a4aSRandall Stewart 		{
5695b5b6e5c2SMichael Tuexen 			struct sockaddr_in6 lsa6, *from6;
5696f8829a4aSRandall Stewart 
5697b5b6e5c2SMichael Tuexen 			from6 = (struct sockaddr_in6 *)from;
5698b5b6e5c2SMichael Tuexen 			sctp_recover_scope_mac(from6, (&lsa6));
5699f8829a4aSRandall Stewart 		}
5700f8829a4aSRandall Stewart #endif
5701f8829a4aSRandall Stewart 	}
5702f8829a4aSRandall Stewart 	/* now copy out what data we can */
5703f8829a4aSRandall Stewart 	if (mp == NULL) {
5704f8829a4aSRandall Stewart 		/* copy out each mbuf in the chain up to length */
5705f8829a4aSRandall Stewart get_more_data:
5706f8829a4aSRandall Stewart 		m = control->data;
5707f8829a4aSRandall Stewart 		while (m) {
5708f8829a4aSRandall Stewart 			/* Move out all we can */
5709f8829a4aSRandall Stewart 			cp_len = (int)uio->uio_resid;
5710139bc87fSRandall Stewart 			my_len = (int)SCTP_BUF_LEN(m);
5711f8829a4aSRandall Stewart 			if (cp_len > my_len) {
5712f8829a4aSRandall Stewart 				/* not enough in this buf */
5713f8829a4aSRandall Stewart 				cp_len = my_len;
5714f8829a4aSRandall Stewart 			}
5715f8829a4aSRandall Stewart 			if (hold_rlock) {
5716f8829a4aSRandall Stewart 				SCTP_INP_READ_UNLOCK(inp);
5717f8829a4aSRandall Stewart 				hold_rlock = 0;
5718f8829a4aSRandall Stewart 			}
5719f8829a4aSRandall Stewart 			if (cp_len > 0)
5720f8829a4aSRandall Stewart 				error = uiomove(mtod(m, char *), cp_len, uio);
5721f8829a4aSRandall Stewart 			/* re-read */
5722f8829a4aSRandall Stewart 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5723f8829a4aSRandall Stewart 				goto release;
5724f8829a4aSRandall Stewart 			}
57250696e120SRandall Stewart 			if ((control->do_not_ref_stcb == 0) && stcb &&
5726f8829a4aSRandall Stewart 			    stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
5727f8829a4aSRandall Stewart 				no_rcv_needed = 1;
5728f8829a4aSRandall Stewart 			}
5729f8829a4aSRandall Stewart 			if (error) {
5730f8829a4aSRandall Stewart 				/* error we are out of here */
5731f8829a4aSRandall Stewart 				goto release;
5732f8829a4aSRandall Stewart 			}
5733139bc87fSRandall Stewart 			if ((SCTP_BUF_NEXT(m) == NULL) &&
5734139bc87fSRandall Stewart 			    (cp_len >= SCTP_BUF_LEN(m)) &&
5735f8829a4aSRandall Stewart 			    ((control->end_added == 0) ||
57360696e120SRandall Stewart 			    (control->end_added &&
57370696e120SRandall Stewart 			    (TAILQ_NEXT(control, next) == NULL)))
5738f8829a4aSRandall Stewart 			    ) {
5739f8829a4aSRandall Stewart 				SCTP_INP_READ_LOCK(inp);
5740f8829a4aSRandall Stewart 				hold_rlock = 1;
5741f8829a4aSRandall Stewart 			}
5742139bc87fSRandall Stewart 			if (cp_len == SCTP_BUF_LEN(m)) {
5743139bc87fSRandall Stewart 				if ((SCTP_BUF_NEXT(m) == NULL) &&
5744139bc87fSRandall Stewart 				    (control->end_added)) {
5745f8829a4aSRandall Stewart 					out_flags |= MSG_EOR;
574652129fcdSRandall Stewart 					if ((control->do_not_ref_stcb == 0) &&
574752129fcdSRandall Stewart 					    (control->stcb != NULL) &&
574852129fcdSRandall Stewart 					    ((control->spec_flags & M_NOTIFICATION) == 0))
5749ee7f9857SRandall Stewart 						control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
5750f8829a4aSRandall Stewart 				}
5751139bc87fSRandall Stewart 				if (control->spec_flags & M_NOTIFICATION) {
5752f8829a4aSRandall Stewart 					out_flags |= MSG_NOTIFICATION;
5753f8829a4aSRandall Stewart 				}
5754f8829a4aSRandall Stewart 				/* we ate up the mbuf */
5755f8829a4aSRandall Stewart 				if (in_flags & MSG_PEEK) {
5756f8829a4aSRandall Stewart 					/* just looking */
5757139bc87fSRandall Stewart 					m = SCTP_BUF_NEXT(m);
5758f8829a4aSRandall Stewart 					copied_so_far += cp_len;
5759f8829a4aSRandall Stewart 				} else {
5760f8829a4aSRandall Stewart 					/* dispose of the mbuf */
5761b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5762f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv,
5763139bc87fSRandall Stewart 						    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
576480fefe0aSRandall Stewart 					}
5765f8829a4aSRandall Stewart 					sctp_sbfree(control, stcb, &so->so_rcv, m);
5766b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5767f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv,
5768f8829a4aSRandall Stewart 						    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
576980fefe0aSRandall Stewart 					}
5770f8829a4aSRandall Stewart 					copied_so_far += cp_len;
5771f8829a4aSRandall Stewart 					freed_so_far += cp_len;
5772c4739e2fSRandall Stewart 					freed_so_far += MSIZE;
577318e198d3SRandall Stewart 					atomic_subtract_int(&control->length, cp_len);
5774f8829a4aSRandall Stewart 					control->data = sctp_m_free(m);
5775f8829a4aSRandall Stewart 					m = control->data;
5776f8829a4aSRandall Stewart 					/*
5777f8829a4aSRandall Stewart 					 * been through it all, must hold sb
5778f8829a4aSRandall Stewart 					 * lock ok to null tail
5779f8829a4aSRandall Stewart 					 */
5780f8829a4aSRandall Stewart 					if (control->data == NULL) {
5781a5d547adSRandall Stewart #ifdef INVARIANTS
5782f8829a4aSRandall Stewart 						if ((control->end_added == 0) ||
5783f8829a4aSRandall Stewart 						    (TAILQ_NEXT(control, next) == NULL)) {
5784f8829a4aSRandall Stewart 							/*
5785f8829a4aSRandall Stewart 							 * If the end is not
5786f8829a4aSRandall Stewart 							 * added, OR the
5787f8829a4aSRandall Stewart 							 * next is NOT null
5788f8829a4aSRandall Stewart 							 * we MUST have the
5789f8829a4aSRandall Stewart 							 * lock.
5790f8829a4aSRandall Stewart 							 */
5791f8829a4aSRandall Stewart 							if (mtx_owned(&inp->inp_rdata_mtx) == 0) {
5792f8829a4aSRandall Stewart 								panic("Hmm we don't own the lock?");
5793f8829a4aSRandall Stewart 							}
5794f8829a4aSRandall Stewart 						}
5795f8829a4aSRandall Stewart #endif
5796f8829a4aSRandall Stewart 						control->tail_mbuf = NULL;
5797a5d547adSRandall Stewart #ifdef INVARIANTS
5798f8829a4aSRandall Stewart 						if ((control->end_added) && ((out_flags & MSG_EOR) == 0)) {
5799f8829a4aSRandall Stewart 							panic("end_added, nothing left and no MSG_EOR");
5800f8829a4aSRandall Stewart 						}
5801f8829a4aSRandall Stewart #endif
5802f8829a4aSRandall Stewart 					}
5803f8829a4aSRandall Stewart 				}
5804f8829a4aSRandall Stewart 			} else {
5805f8829a4aSRandall Stewart 				/* Do we need to trim the mbuf? */
5806139bc87fSRandall Stewart 				if (control->spec_flags & M_NOTIFICATION) {
5807f8829a4aSRandall Stewart 					out_flags |= MSG_NOTIFICATION;
5808f8829a4aSRandall Stewart 				}
5809f8829a4aSRandall Stewart 				if ((in_flags & MSG_PEEK) == 0) {
5810139bc87fSRandall Stewart 					SCTP_BUF_RESV_UF(m, cp_len);
5811139bc87fSRandall Stewart 					SCTP_BUF_LEN(m) -= cp_len;
5812b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5813f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, cp_len);
581480fefe0aSRandall Stewart 					}
5815f8829a4aSRandall Stewart 					atomic_subtract_int(&so->so_rcv.sb_cc, cp_len);
58160696e120SRandall Stewart 					if ((control->do_not_ref_stcb == 0) &&
58170696e120SRandall Stewart 					    stcb) {
5818f8829a4aSRandall Stewart 						atomic_subtract_int(&stcb->asoc.sb_cc, cp_len);
5819f8829a4aSRandall Stewart 					}
5820f8829a4aSRandall Stewart 					copied_so_far += cp_len;
5821f8829a4aSRandall Stewart 					freed_so_far += cp_len;
5822c4739e2fSRandall Stewart 					freed_so_far += MSIZE;
5823b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5824f8829a4aSRandall Stewart 						sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb,
5825f8829a4aSRandall Stewart 						    SCTP_LOG_SBRESULT, 0);
582680fefe0aSRandall Stewart 					}
582718e198d3SRandall Stewart 					atomic_subtract_int(&control->length, cp_len);
5828f8829a4aSRandall Stewart 				} else {
5829f8829a4aSRandall Stewart 					copied_so_far += cp_len;
5830f8829a4aSRandall Stewart 				}
5831f8829a4aSRandall Stewart 			}
5832d61a0ae0SRandall Stewart 			if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) {
5833f8829a4aSRandall Stewart 				break;
5834f8829a4aSRandall Stewart 			}
5835f8829a4aSRandall Stewart 			if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5836f8829a4aSRandall Stewart 			    (control->do_not_ref_stcb == 0) &&
5837f8829a4aSRandall Stewart 			    (freed_so_far >= rwnd_req)) {
5838f8829a4aSRandall Stewart 				sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5839f8829a4aSRandall Stewart 			}
5840f8829a4aSRandall Stewart 		}		/* end while(m) */
5841f8829a4aSRandall Stewart 		/*
5842f8829a4aSRandall Stewart 		 * At this point we have looked at it all and we either have
5843f8829a4aSRandall Stewart 		 * a MSG_EOR/or read all the user wants... <OR>
5844f8829a4aSRandall Stewart 		 * control->length == 0.
5845f8829a4aSRandall Stewart 		 */
5846d61a0ae0SRandall Stewart 		if ((out_flags & MSG_EOR) && ((in_flags & MSG_PEEK) == 0)) {
5847f8829a4aSRandall Stewart 			/* we are done with this control */
5848f8829a4aSRandall Stewart 			if (control->length == 0) {
5849f8829a4aSRandall Stewart 				if (control->data) {
5850a5d547adSRandall Stewart #ifdef INVARIANTS
5851f8829a4aSRandall Stewart 					panic("control->data not null at read eor?");
5852f8829a4aSRandall Stewart #else
5853ad81507eSRandall Stewart 					SCTP_PRINTF("Strange, data left in the control buffer .. invarients would panic?\n");
5854f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
5855f8829a4aSRandall Stewart 					control->data = NULL;
5856f8829a4aSRandall Stewart #endif
5857f8829a4aSRandall Stewart 				}
5858f8829a4aSRandall Stewart 		done_with_control:
5859f8829a4aSRandall Stewart 				if (TAILQ_NEXT(control, next) == NULL) {
5860f8829a4aSRandall Stewart 					/*
5861f8829a4aSRandall Stewart 					 * If we don't have a next we need a
5862b201f536SRandall Stewart 					 * lock, if there is a next
5863b201f536SRandall Stewart 					 * interrupt is filling ahead of us
5864b201f536SRandall Stewart 					 * and we don't need a lock to
5865b201f536SRandall Stewart 					 * remove this guy (which is the
5866b201f536SRandall Stewart 					 * head of the queue).
5867f8829a4aSRandall Stewart 					 */
5868f8829a4aSRandall Stewart 					if (hold_rlock == 0) {
5869f8829a4aSRandall Stewart 						SCTP_INP_READ_LOCK(inp);
5870f8829a4aSRandall Stewart 						hold_rlock = 1;
5871f8829a4aSRandall Stewart 					}
5872f8829a4aSRandall Stewart 				}
5873f8829a4aSRandall Stewart 				TAILQ_REMOVE(&inp->read_queue, control, next);
5874f8829a4aSRandall Stewart 				/* Add back any hiddend data */
5875f8829a4aSRandall Stewart 				if (control->held_length) {
5876f8829a4aSRandall Stewart 					held_length = 0;
5877f8829a4aSRandall Stewart 					control->held_length = 0;
5878f8829a4aSRandall Stewart 					wakeup_read_socket = 1;
5879f8829a4aSRandall Stewart 				}
588017205eccSRandall Stewart 				if (control->aux_data) {
588117205eccSRandall Stewart 					sctp_m_free(control->aux_data);
588217205eccSRandall Stewart 					control->aux_data = NULL;
588317205eccSRandall Stewart 				}
5884f8829a4aSRandall Stewart 				no_rcv_needed = control->do_not_ref_stcb;
5885f8829a4aSRandall Stewart 				sctp_free_remote_addr(control->whoFrom);
5886f8829a4aSRandall Stewart 				control->data = NULL;
5887f8829a4aSRandall Stewart 				sctp_free_a_readq(stcb, control);
5888f8829a4aSRandall Stewart 				control = NULL;
58890696e120SRandall Stewart 				if ((freed_so_far >= rwnd_req) &&
58900696e120SRandall Stewart 				    (no_rcv_needed == 0))
5891f8829a4aSRandall Stewart 					sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5892f8829a4aSRandall Stewart 
5893f8829a4aSRandall Stewart 			} else {
5894f8829a4aSRandall Stewart 				/*
5895f8829a4aSRandall Stewart 				 * The user did not read all of this
5896f8829a4aSRandall Stewart 				 * message, turn off the returned MSG_EOR
5897f8829a4aSRandall Stewart 				 * since we are leaving more behind on the
5898f8829a4aSRandall Stewart 				 * control to read.
5899f8829a4aSRandall Stewart 				 */
5900a5d547adSRandall Stewart #ifdef INVARIANTS
59010696e120SRandall Stewart 				if (control->end_added &&
59020696e120SRandall Stewart 				    (control->data == NULL) &&
5903f8829a4aSRandall Stewart 				    (control->tail_mbuf == NULL)) {
5904f8829a4aSRandall Stewart 					panic("Gak, control->length is corrupt?");
5905f8829a4aSRandall Stewart 				}
5906f8829a4aSRandall Stewart #endif
5907f8829a4aSRandall Stewart 				no_rcv_needed = control->do_not_ref_stcb;
5908f8829a4aSRandall Stewart 				out_flags &= ~MSG_EOR;
5909f8829a4aSRandall Stewart 			}
5910f8829a4aSRandall Stewart 		}
5911f8829a4aSRandall Stewart 		if (out_flags & MSG_EOR) {
5912f8829a4aSRandall Stewart 			goto release;
5913f8829a4aSRandall Stewart 		}
5914f8829a4aSRandall Stewart 		if ((uio->uio_resid == 0) ||
591504aab884SMichael Tuexen 		    ((in_eeor_mode) &&
591604aab884SMichael Tuexen 		    (copied_so_far >= (uint32_t) max(so->so_rcv.sb_lowat, 1)))) {
5917f8829a4aSRandall Stewart 			goto release;
5918f8829a4aSRandall Stewart 		}
5919f8829a4aSRandall Stewart 		/*
5920f8829a4aSRandall Stewart 		 * If I hit here the receiver wants more and this message is
5921f8829a4aSRandall Stewart 		 * NOT done (pd-api). So two questions. Can we block? if not
5922f8829a4aSRandall Stewart 		 * we are done. Did the user NOT set MSG_WAITALL?
5923f8829a4aSRandall Stewart 		 */
5924f8829a4aSRandall Stewart 		if (block_allowed == 0) {
5925f8829a4aSRandall Stewart 			goto release;
5926f8829a4aSRandall Stewart 		}
5927f8829a4aSRandall Stewart 		/*
5928f8829a4aSRandall Stewart 		 * We need to wait for more data a few things: - We don't
5929f8829a4aSRandall Stewart 		 * sbunlock() so we don't get someone else reading. - We
5930f8829a4aSRandall Stewart 		 * must be sure to account for the case where what is added
5931f8829a4aSRandall Stewart 		 * is NOT to our control when we wakeup.
5932f8829a4aSRandall Stewart 		 */
5933f8829a4aSRandall Stewart 
5934f8829a4aSRandall Stewart 		/*
5935f8829a4aSRandall Stewart 		 * Do we need to tell the transport a rwnd update might be
5936f8829a4aSRandall Stewart 		 * needed before we go to sleep?
5937f8829a4aSRandall Stewart 		 */
5938f8829a4aSRandall Stewart 		if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
5939f8829a4aSRandall Stewart 		    ((freed_so_far >= rwnd_req) &&
5940f8829a4aSRandall Stewart 		    (control->do_not_ref_stcb == 0) &&
5941f8829a4aSRandall Stewart 		    (no_rcv_needed == 0))) {
5942f8829a4aSRandall Stewart 			sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
5943f8829a4aSRandall Stewart 		}
5944f8829a4aSRandall Stewart wait_some_more:
594544b7479bSRandall Stewart 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
5946f8829a4aSRandall Stewart 			goto release;
5947f8829a4aSRandall Stewart 		}
5948f8829a4aSRandall Stewart 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)
5949f8829a4aSRandall Stewart 			goto release;
5950f8829a4aSRandall Stewart 
5951f8829a4aSRandall Stewart 		if (hold_rlock == 1) {
5952f8829a4aSRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
5953f8829a4aSRandall Stewart 			hold_rlock = 0;
5954f8829a4aSRandall Stewart 		}
5955f8829a4aSRandall Stewart 		if (hold_sblock == 0) {
5956f8829a4aSRandall Stewart 			SOCKBUF_LOCK(&so->so_rcv);
5957f8829a4aSRandall Stewart 			hold_sblock = 1;
5958f8829a4aSRandall Stewart 		}
5959851b7298SRandall Stewart 		if ((copied_so_far) && (control->length == 0) &&
5960b5c16493SMichael Tuexen 		    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE))) {
5961851b7298SRandall Stewart 			goto release;
5962851b7298SRandall Stewart 		}
5963f8829a4aSRandall Stewart 		if (so->so_rcv.sb_cc <= control->held_length) {
5964f8829a4aSRandall Stewart 			error = sbwait(&so->so_rcv);
5965f8829a4aSRandall Stewart 			if (error) {
5966f8829a4aSRandall Stewart 				goto release;
5967f8829a4aSRandall Stewart 			}
5968f8829a4aSRandall Stewart 			control->held_length = 0;
5969f8829a4aSRandall Stewart 		}
5970f8829a4aSRandall Stewart 		if (hold_sblock) {
5971f8829a4aSRandall Stewart 			SOCKBUF_UNLOCK(&so->so_rcv);
5972f8829a4aSRandall Stewart 			hold_sblock = 0;
5973f8829a4aSRandall Stewart 		}
5974f8829a4aSRandall Stewart 		if (control->length == 0) {
5975f8829a4aSRandall Stewart 			/* still nothing here */
5976f8829a4aSRandall Stewart 			if (control->end_added == 1) {
5977f8829a4aSRandall Stewart 				/* he aborted, or is done i.e.did a shutdown */
5978f8829a4aSRandall Stewart 				out_flags |= MSG_EOR;
59799a6142d8SRandall Stewart 				if (control->pdapi_aborted) {
59806114cd96SRandall Stewart 					if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5981ee7f9857SRandall Stewart 						control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
59829a6142d8SRandall Stewart 
598303b0b021SRandall Stewart 					out_flags |= MSG_TRUNC;
59849a6142d8SRandall Stewart 				} else {
59856114cd96SRandall Stewart 					if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
5986ee7f9857SRandall Stewart 						control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
59879a6142d8SRandall Stewart 				}
5988f8829a4aSRandall Stewart 				goto done_with_control;
5989f8829a4aSRandall Stewart 			}
5990f8829a4aSRandall Stewart 			if (so->so_rcv.sb_cc > held_length) {
5991f8829a4aSRandall Stewart 				control->held_length = so->so_rcv.sb_cc;
5992f8829a4aSRandall Stewart 				held_length = 0;
5993f8829a4aSRandall Stewart 			}
5994f8829a4aSRandall Stewart 			goto wait_some_more;
5995f8829a4aSRandall Stewart 		} else if (control->data == NULL) {
599650cec919SRandall Stewart 			/*
599750cec919SRandall Stewart 			 * we must re-sync since data is probably being
599850cec919SRandall Stewart 			 * added
599950cec919SRandall Stewart 			 */
600050cec919SRandall Stewart 			SCTP_INP_READ_LOCK(inp);
600150cec919SRandall Stewart 			if ((control->length > 0) && (control->data == NULL)) {
600250cec919SRandall Stewart 				/*
600350cec919SRandall Stewart 				 * big trouble.. we have the lock and its
600450cec919SRandall Stewart 				 * corrupt?
600550cec919SRandall Stewart 				 */
60069c04b296SRandall Stewart #ifdef INVARIANTS
6007f8829a4aSRandall Stewart 				panic("Impossible data==NULL length !=0");
60089c04b296SRandall Stewart #endif
60099c04b296SRandall Stewart 				out_flags |= MSG_EOR;
60109c04b296SRandall Stewart 				out_flags |= MSG_TRUNC;
60119c04b296SRandall Stewart 				control->length = 0;
60129c04b296SRandall Stewart 				SCTP_INP_READ_UNLOCK(inp);
60139c04b296SRandall Stewart 				goto done_with_control;
6014f8829a4aSRandall Stewart 			}
601550cec919SRandall Stewart 			SCTP_INP_READ_UNLOCK(inp);
601650cec919SRandall Stewart 			/* We will fall around to get more data */
601750cec919SRandall Stewart 		}
6018f8829a4aSRandall Stewart 		goto get_more_data;
6019f8829a4aSRandall Stewart 	} else {
602017205eccSRandall Stewart 		/*-
602117205eccSRandall Stewart 		 * Give caller back the mbuf chain,
602217205eccSRandall Stewart 		 * store in uio_resid the length
6023f8829a4aSRandall Stewart 		 */
602417205eccSRandall Stewart 		wakeup_read_socket = 0;
6025f8829a4aSRandall Stewart 		if ((control->end_added == 0) ||
6026f8829a4aSRandall Stewart 		    (TAILQ_NEXT(control, next) == NULL)) {
6027f8829a4aSRandall Stewart 			/* Need to get rlock */
6028f8829a4aSRandall Stewart 			if (hold_rlock == 0) {
6029f8829a4aSRandall Stewart 				SCTP_INP_READ_LOCK(inp);
6030f8829a4aSRandall Stewart 				hold_rlock = 1;
6031f8829a4aSRandall Stewart 			}
6032f8829a4aSRandall Stewart 		}
6033139bc87fSRandall Stewart 		if (control->end_added) {
6034f8829a4aSRandall Stewart 			out_flags |= MSG_EOR;
603560990c0cSMichael Tuexen 			if ((control->do_not_ref_stcb == 0) &&
603660990c0cSMichael Tuexen 			    (control->stcb != NULL) &&
603760990c0cSMichael Tuexen 			    ((control->spec_flags & M_NOTIFICATION) == 0))
6038ee7f9857SRandall Stewart 				control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6039f8829a4aSRandall Stewart 		}
6040139bc87fSRandall Stewart 		if (control->spec_flags & M_NOTIFICATION) {
6041f8829a4aSRandall Stewart 			out_flags |= MSG_NOTIFICATION;
6042f8829a4aSRandall Stewart 		}
604317205eccSRandall Stewart 		uio->uio_resid = control->length;
6044f8829a4aSRandall Stewart 		*mp = control->data;
6045f8829a4aSRandall Stewart 		m = control->data;
6046f8829a4aSRandall Stewart 		while (m) {
6047b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6048f8829a4aSRandall Stewart 				sctp_sblog(&so->so_rcv,
6049139bc87fSRandall Stewart 				    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
605080fefe0aSRandall Stewart 			}
6051f8829a4aSRandall Stewart 			sctp_sbfree(control, stcb, &so->so_rcv, m);
6052139bc87fSRandall Stewart 			freed_so_far += SCTP_BUF_LEN(m);
6053c4739e2fSRandall Stewart 			freed_so_far += MSIZE;
6054b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6055f8829a4aSRandall Stewart 				sctp_sblog(&so->so_rcv,
6056f8829a4aSRandall Stewart 				    control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0);
605780fefe0aSRandall Stewart 			}
6058139bc87fSRandall Stewart 			m = SCTP_BUF_NEXT(m);
6059f8829a4aSRandall Stewart 		}
6060f8829a4aSRandall Stewart 		control->data = control->tail_mbuf = NULL;
6061f8829a4aSRandall Stewart 		control->length = 0;
6062f8829a4aSRandall Stewart 		if (out_flags & MSG_EOR) {
6063f8829a4aSRandall Stewart 			/* Done with this control */
6064f8829a4aSRandall Stewart 			goto done_with_control;
6065f8829a4aSRandall Stewart 		}
6066f8829a4aSRandall Stewart 	}
6067f8829a4aSRandall Stewart release:
6068f8829a4aSRandall Stewart 	if (hold_rlock == 1) {
6069f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
6070f8829a4aSRandall Stewart 		hold_rlock = 0;
6071f8829a4aSRandall Stewart 	}
60727abab911SRobert Watson 	if (hold_sblock == 1) {
60737abab911SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
60747abab911SRobert Watson 		hold_sblock = 0;
6075f8829a4aSRandall Stewart 	}
6076f8829a4aSRandall Stewart 	sbunlock(&so->so_rcv);
60777abab911SRobert Watson 	sockbuf_lock = 0;
6078f8829a4aSRandall Stewart 
6079f8829a4aSRandall Stewart release_unlocked:
6080f8829a4aSRandall Stewart 	if (hold_sblock) {
6081f8829a4aSRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
6082f8829a4aSRandall Stewart 		hold_sblock = 0;
6083f8829a4aSRandall Stewart 	}
6084f8829a4aSRandall Stewart 	if ((stcb) && (in_flags & MSG_PEEK) == 0) {
6085f8829a4aSRandall Stewart 		if ((freed_so_far >= rwnd_req) &&
6086f8829a4aSRandall Stewart 		    (control && (control->do_not_ref_stcb == 0)) &&
6087f8829a4aSRandall Stewart 		    (no_rcv_needed == 0))
6088f8829a4aSRandall Stewart 			sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6089f8829a4aSRandall Stewart 	}
6090f8829a4aSRandall Stewart out:
60911b9f62a0SRandall Stewart 	if (msg_flags) {
60921b9f62a0SRandall Stewart 		*msg_flags = out_flags;
60931b9f62a0SRandall Stewart 	}
60949a6142d8SRandall Stewart 	if (((out_flags & MSG_EOR) == 0) &&
60959a6142d8SRandall Stewart 	    ((in_flags & MSG_PEEK) == 0) &&
60969a6142d8SRandall Stewart 	    (sinfo) &&
6097e2e7c62eSMichael Tuexen 	    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
6098e2e7c62eSMichael Tuexen 	    sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO))) {
60999a6142d8SRandall Stewart 		struct sctp_extrcvinfo *s_extra;
61009a6142d8SRandall Stewart 
61019a6142d8SRandall Stewart 		s_extra = (struct sctp_extrcvinfo *)sinfo;
61029a6142d8SRandall Stewart 		s_extra->sreinfo_next_flags = SCTP_NO_NEXT_MSG;
61039a6142d8SRandall Stewart 	}
6104f8829a4aSRandall Stewart 	if (hold_rlock == 1) {
6105f8829a4aSRandall Stewart 		SCTP_INP_READ_UNLOCK(inp);
6106f8829a4aSRandall Stewart 	}
6107f8829a4aSRandall Stewart 	if (hold_sblock) {
6108f8829a4aSRandall Stewart 		SOCKBUF_UNLOCK(&so->so_rcv);
6109f8829a4aSRandall Stewart 	}
61107abab911SRobert Watson 	if (sockbuf_lock) {
61117abab911SRobert Watson 		sbunlock(&so->so_rcv);
61127abab911SRobert Watson 	}
611350cec919SRandall Stewart 	if (freecnt_applied) {
6114f8829a4aSRandall Stewart 		/*
6115f8829a4aSRandall Stewart 		 * The lock on the socket buffer protects us so the free
6116f8829a4aSRandall Stewart 		 * code will stop. But since we used the socketbuf lock and
6117f8829a4aSRandall Stewart 		 * the sender uses the tcb_lock to increment, we need to use
6118f8829a4aSRandall Stewart 		 * the atomic add to the refcnt.
6119f8829a4aSRandall Stewart 		 */
612050cec919SRandall Stewart 		if (stcb == NULL) {
6121df6e0cc3SRandall Stewart #ifdef INVARIANTS
612250cec919SRandall Stewart 			panic("stcb for refcnt has gone NULL?");
6123df6e0cc3SRandall Stewart 			goto stage_left;
6124df6e0cc3SRandall Stewart #else
6125df6e0cc3SRandall Stewart 			goto stage_left;
6126df6e0cc3SRandall Stewart #endif
612750cec919SRandall Stewart 		}
612850cec919SRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, -1);
6129f8829a4aSRandall Stewart 		/* Save the value back for next time */
6130f8829a4aSRandall Stewart 		stcb->freed_by_sorcv_sincelast = freed_so_far;
6131f8829a4aSRandall Stewart 	}
6132b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
6133f8829a4aSRandall Stewart 		if (stcb) {
6134f8829a4aSRandall Stewart 			sctp_misc_ints(SCTP_SORECV_DONE,
6135f8829a4aSRandall Stewart 			    freed_so_far,
6136f8829a4aSRandall Stewart 			    ((uio) ? (slen - uio->uio_resid) : slen),
6137f8829a4aSRandall Stewart 			    stcb->asoc.my_rwnd,
6138f8829a4aSRandall Stewart 			    so->so_rcv.sb_cc);
6139f8829a4aSRandall Stewart 		} else {
6140f8829a4aSRandall Stewart 			sctp_misc_ints(SCTP_SORECV_DONE,
6141f8829a4aSRandall Stewart 			    freed_so_far,
6142f8829a4aSRandall Stewart 			    ((uio) ? (slen - uio->uio_resid) : slen),
6143f8829a4aSRandall Stewart 			    0,
6144f8829a4aSRandall Stewart 			    so->so_rcv.sb_cc);
6145f8829a4aSRandall Stewart 		}
614680fefe0aSRandall Stewart 	}
6147df6e0cc3SRandall Stewart stage_left:
6148f8829a4aSRandall Stewart 	if (wakeup_read_socket) {
6149f8829a4aSRandall Stewart 		sctp_sorwakeup(inp, so);
6150f8829a4aSRandall Stewart 	}
6151f8829a4aSRandall Stewart 	return (error);
6152f8829a4aSRandall Stewart }
6153f8829a4aSRandall Stewart 
6154f8829a4aSRandall Stewart 
6155f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
6156f8829a4aSRandall Stewart struct mbuf *
6157f8829a4aSRandall Stewart sctp_m_free(struct mbuf *m)
6158f8829a4aSRandall Stewart {
6159b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6160139bc87fSRandall Stewart 		if (SCTP_BUF_IS_EXTENDED(m)) {
6161f8829a4aSRandall Stewart 			sctp_log_mb(m, SCTP_MBUF_IFREE);
6162f8829a4aSRandall Stewart 		}
616380fefe0aSRandall Stewart 	}
6164f8829a4aSRandall Stewart 	return (m_free(m));
6165f8829a4aSRandall Stewart }
6166f8829a4aSRandall Stewart 
6167f8829a4aSRandall Stewart void
6168f8829a4aSRandall Stewart sctp_m_freem(struct mbuf *mb)
6169f8829a4aSRandall Stewart {
6170f8829a4aSRandall Stewart 	while (mb != NULL)
6171f8829a4aSRandall Stewart 		mb = sctp_m_free(mb);
6172f8829a4aSRandall Stewart }
6173f8829a4aSRandall Stewart 
6174f8829a4aSRandall Stewart #endif
6175f8829a4aSRandall Stewart 
617642551e99SRandall Stewart int
617742551e99SRandall Stewart sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id)
617842551e99SRandall Stewart {
617942551e99SRandall Stewart 	/*
618042551e99SRandall Stewart 	 * Given a local address. For all associations that holds the
618142551e99SRandall Stewart 	 * address, request a peer-set-primary.
618242551e99SRandall Stewart 	 */
618342551e99SRandall Stewart 	struct sctp_ifa *ifa;
618442551e99SRandall Stewart 	struct sctp_laddr *wi;
618542551e99SRandall Stewart 
618642551e99SRandall Stewart 	ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0);
618742551e99SRandall Stewart 	if (ifa == NULL) {
6188c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EADDRNOTAVAIL);
618942551e99SRandall Stewart 		return (EADDRNOTAVAIL);
619042551e99SRandall Stewart 	}
619142551e99SRandall Stewart 	/*
619242551e99SRandall Stewart 	 * Now that we have the ifa we must awaken the iterator with this
619342551e99SRandall Stewart 	 * message.
619442551e99SRandall Stewart 	 */
6195b3f1ea41SRandall Stewart 	wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
619642551e99SRandall Stewart 	if (wi == NULL) {
6197c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
619842551e99SRandall Stewart 		return (ENOMEM);
619942551e99SRandall Stewart 	}
620042551e99SRandall Stewart 	/* Now incr the count and int wi structure */
620142551e99SRandall Stewart 	SCTP_INCR_LADDR_COUNT();
620242551e99SRandall Stewart 	bzero(wi, sizeof(*wi));
6203d61a0ae0SRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
620442551e99SRandall Stewart 	wi->ifa = ifa;
620542551e99SRandall Stewart 	wi->action = SCTP_SET_PRIM_ADDR;
620642551e99SRandall Stewart 	atomic_add_int(&ifa->refcount, 1);
620742551e99SRandall Stewart 
620842551e99SRandall Stewart 	/* Now add it to the work queue */
6209f7517433SRandall Stewart 	SCTP_WQ_ADDR_LOCK();
621042551e99SRandall Stewart 	/*
621142551e99SRandall Stewart 	 * Should this really be a tailq? As it is we will process the
621242551e99SRandall Stewart 	 * newest first :-0
621342551e99SRandall Stewart 	 */
6214b3f1ea41SRandall Stewart 	LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
6215f7517433SRandall Stewart 	SCTP_WQ_ADDR_UNLOCK();
621642551e99SRandall Stewart 	sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
621742551e99SRandall Stewart 	    (struct sctp_inpcb *)NULL,
621842551e99SRandall Stewart 	    (struct sctp_tcb *)NULL,
621942551e99SRandall Stewart 	    (struct sctp_nets *)NULL);
622042551e99SRandall Stewart 	return (0);
622142551e99SRandall Stewart }
622242551e99SRandall Stewart 
622342551e99SRandall Stewart 
6224f8829a4aSRandall Stewart int
622517205eccSRandall Stewart sctp_soreceive(struct socket *so,
622617205eccSRandall Stewart     struct sockaddr **psa,
622717205eccSRandall Stewart     struct uio *uio,
622817205eccSRandall Stewart     struct mbuf **mp0,
622917205eccSRandall Stewart     struct mbuf **controlp,
623017205eccSRandall Stewart     int *flagsp)
6231f8829a4aSRandall Stewart {
6232f8829a4aSRandall Stewart 	int error, fromlen;
6233f8829a4aSRandall Stewart 	uint8_t sockbuf[256];
6234f8829a4aSRandall Stewart 	struct sockaddr *from;
6235f8829a4aSRandall Stewart 	struct sctp_extrcvinfo sinfo;
6236f8829a4aSRandall Stewart 	int filling_sinfo = 1;
6237f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
6238f8829a4aSRandall Stewart 
6239f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
6240f8829a4aSRandall Stewart 	/* pickup the assoc we are reading from */
6241f8829a4aSRandall Stewart 	if (inp == NULL) {
6242c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6243f8829a4aSRandall Stewart 		return (EINVAL);
6244f8829a4aSRandall Stewart 	}
6245e2e7c62eSMichael Tuexen 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) &&
6246e2e7c62eSMichael Tuexen 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
6247e2e7c62eSMichael Tuexen 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) ||
6248f8829a4aSRandall Stewart 	    (controlp == NULL)) {
6249f8829a4aSRandall Stewart 		/* user does not want the sndrcv ctl */
6250f8829a4aSRandall Stewart 		filling_sinfo = 0;
6251f8829a4aSRandall Stewart 	}
6252f8829a4aSRandall Stewart 	if (psa) {
6253f8829a4aSRandall Stewart 		from = (struct sockaddr *)sockbuf;
6254f8829a4aSRandall Stewart 		fromlen = sizeof(sockbuf);
6255f8829a4aSRandall Stewart 		from->sa_len = 0;
6256f8829a4aSRandall Stewart 	} else {
6257f8829a4aSRandall Stewart 		from = NULL;
6258f8829a4aSRandall Stewart 		fromlen = 0;
6259f8829a4aSRandall Stewart 	}
6260f8829a4aSRandall Stewart 
6261e432298aSXin LI 	if (filling_sinfo) {
6262e432298aSXin LI 		memset(&sinfo, 0, sizeof(struct sctp_extrcvinfo));
6263e432298aSXin LI 	}
6264f8829a4aSRandall Stewart 	error = sctp_sorecvmsg(so, uio, mp0, from, fromlen, flagsp,
6265f8829a4aSRandall Stewart 	    (struct sctp_sndrcvinfo *)&sinfo, filling_sinfo);
6266e432298aSXin LI 	if (controlp != NULL) {
6267f8829a4aSRandall Stewart 		/* copy back the sinfo in a CMSG format */
6268f8829a4aSRandall Stewart 		if (filling_sinfo)
6269f8829a4aSRandall Stewart 			*controlp = sctp_build_ctl_nchunk(inp,
6270f8829a4aSRandall Stewart 			    (struct sctp_sndrcvinfo *)&sinfo);
6271f8829a4aSRandall Stewart 		else
6272f8829a4aSRandall Stewart 			*controlp = NULL;
6273f8829a4aSRandall Stewart 	}
6274f8829a4aSRandall Stewart 	if (psa) {
6275f8829a4aSRandall Stewart 		/* copy back the address info */
6276f8829a4aSRandall Stewart 		if (from && from->sa_len) {
6277f8829a4aSRandall Stewart 			*psa = sodupsockaddr(from, M_NOWAIT);
6278f8829a4aSRandall Stewart 		} else {
6279f8829a4aSRandall Stewart 			*psa = NULL;
6280f8829a4aSRandall Stewart 		}
6281f8829a4aSRandall Stewart 	}
6282f8829a4aSRandall Stewart 	return (error);
6283f8829a4aSRandall Stewart }
628417205eccSRandall Stewart 
628517205eccSRandall Stewart 
628617205eccSRandall Stewart 
628717205eccSRandall Stewart 
628817205eccSRandall Stewart 
628917205eccSRandall Stewart int
6290d61a0ae0SRandall Stewart sctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr,
6291d61a0ae0SRandall Stewart     int totaddr, int *error)
629217205eccSRandall Stewart {
629317205eccSRandall Stewart 	int added = 0;
629417205eccSRandall Stewart 	int i;
629517205eccSRandall Stewart 	struct sctp_inpcb *inp;
629617205eccSRandall Stewart 	struct sockaddr *sa;
629717205eccSRandall Stewart 	size_t incr = 0;
629817205eccSRandall Stewart 
629992776dfdSMichael Tuexen #ifdef INET
630092776dfdSMichael Tuexen 	struct sockaddr_in *sin;
630192776dfdSMichael Tuexen 
630292776dfdSMichael Tuexen #endif
630392776dfdSMichael Tuexen #ifdef INET6
630492776dfdSMichael Tuexen 	struct sockaddr_in6 *sin6;
630592776dfdSMichael Tuexen 
630692776dfdSMichael Tuexen #endif
630792776dfdSMichael Tuexen 
630817205eccSRandall Stewart 	sa = addr;
630917205eccSRandall Stewart 	inp = stcb->sctp_ep;
631017205eccSRandall Stewart 	*error = 0;
631117205eccSRandall Stewart 	for (i = 0; i < totaddr; i++) {
6312ea5eba11SMichael Tuexen 		switch (sa->sa_family) {
6313ea5eba11SMichael Tuexen #ifdef INET
6314ea5eba11SMichael Tuexen 		case AF_INET:
631517205eccSRandall Stewart 			incr = sizeof(struct sockaddr_in);
631692776dfdSMichael Tuexen 			sin = (struct sockaddr_in *)sa;
631792776dfdSMichael Tuexen 			if ((sin->sin_addr.s_addr == INADDR_ANY) ||
631892776dfdSMichael Tuexen 			    (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
631992776dfdSMichael Tuexen 			    IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
632092776dfdSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
632192776dfdSMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
632292776dfdSMichael Tuexen 				*error = EINVAL;
632392776dfdSMichael Tuexen 				goto out_now;
632492776dfdSMichael Tuexen 			}
6325ca85e948SMichael Tuexen 			if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
632617205eccSRandall Stewart 				/* assoc gone no un-lock */
6327c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6328c4739e2fSRandall Stewart 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
632917205eccSRandall Stewart 				*error = ENOBUFS;
633017205eccSRandall Stewart 				goto out_now;
633117205eccSRandall Stewart 			}
633217205eccSRandall Stewart 			added++;
6333ea5eba11SMichael Tuexen 			break;
6334ea5eba11SMichael Tuexen #endif
6335ea5eba11SMichael Tuexen #ifdef INET6
6336ea5eba11SMichael Tuexen 		case AF_INET6:
633717205eccSRandall Stewart 			incr = sizeof(struct sockaddr_in6);
633892776dfdSMichael Tuexen 			sin6 = (struct sockaddr_in6 *)sa;
633992776dfdSMichael Tuexen 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
634092776dfdSMichael Tuexen 			    IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
634192776dfdSMichael Tuexen 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
634292776dfdSMichael Tuexen 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_8);
634392776dfdSMichael Tuexen 				*error = EINVAL;
634492776dfdSMichael Tuexen 				goto out_now;
634592776dfdSMichael Tuexen 			}
6346ca85e948SMichael Tuexen 			if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
634717205eccSRandall Stewart 				/* assoc gone no un-lock */
6348c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
6349c4739e2fSRandall Stewart 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_8);
635017205eccSRandall Stewart 				*error = ENOBUFS;
635117205eccSRandall Stewart 				goto out_now;
635217205eccSRandall Stewart 			}
635317205eccSRandall Stewart 			added++;
6354ea5eba11SMichael Tuexen 			break;
6355ea5eba11SMichael Tuexen #endif
6356ea5eba11SMichael Tuexen 		default:
6357ea5eba11SMichael Tuexen 			break;
635817205eccSRandall Stewart 		}
635917205eccSRandall Stewart 		sa = (struct sockaddr *)((caddr_t)sa + incr);
636017205eccSRandall Stewart 	}
636117205eccSRandall Stewart out_now:
636217205eccSRandall Stewart 	return (added);
636317205eccSRandall Stewart }
636417205eccSRandall Stewart 
636517205eccSRandall Stewart struct sctp_tcb *
6366d61a0ae0SRandall Stewart sctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr,
6367d61a0ae0SRandall Stewart     int *totaddr, int *num_v4, int *num_v6, int *error,
6368d61a0ae0SRandall Stewart     int limit, int *bad_addr)
636917205eccSRandall Stewart {
637017205eccSRandall Stewart 	struct sockaddr *sa;
637117205eccSRandall Stewart 	struct sctp_tcb *stcb = NULL;
637217205eccSRandall Stewart 	size_t incr, at, i;
637317205eccSRandall Stewart 
637417205eccSRandall Stewart 	at = incr = 0;
637517205eccSRandall Stewart 	sa = addr;
6376ea5eba11SMichael Tuexen 
637717205eccSRandall Stewart 	*error = *num_v6 = *num_v4 = 0;
637817205eccSRandall Stewart 	/* account and validate addresses */
63794c9179adSRandall Stewart 	for (i = 0; i < (size_t)*totaddr; i++) {
6380ea5eba11SMichael Tuexen 		switch (sa->sa_family) {
6381ea5eba11SMichael Tuexen #ifdef INET
6382ea5eba11SMichael Tuexen 		case AF_INET:
638317205eccSRandall Stewart 			(*num_v4) += 1;
638417205eccSRandall Stewart 			incr = sizeof(struct sockaddr_in);
6385d61a0ae0SRandall Stewart 			if (sa->sa_len != incr) {
6386c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6387d61a0ae0SRandall Stewart 				*error = EINVAL;
6388d61a0ae0SRandall Stewart 				*bad_addr = 1;
6389d61a0ae0SRandall Stewart 				return (NULL);
6390d61a0ae0SRandall Stewart 			}
6391ea5eba11SMichael Tuexen 			break;
6392ea5eba11SMichael Tuexen #endif
6393ea5eba11SMichael Tuexen #ifdef INET6
6394ea5eba11SMichael Tuexen 		case AF_INET6:
6395ea5eba11SMichael Tuexen 			{
639617205eccSRandall Stewart 				struct sockaddr_in6 *sin6;
639717205eccSRandall Stewart 
639817205eccSRandall Stewart 				sin6 = (struct sockaddr_in6 *)sa;
639917205eccSRandall Stewart 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
640017205eccSRandall Stewart 					/* Must be non-mapped for connectx */
6401c4739e2fSRandall Stewart 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
640217205eccSRandall Stewart 					*error = EINVAL;
6403d61a0ae0SRandall Stewart 					*bad_addr = 1;
640417205eccSRandall Stewart 					return (NULL);
640517205eccSRandall Stewart 				}
640617205eccSRandall Stewart 				(*num_v6) += 1;
640717205eccSRandall Stewart 				incr = sizeof(struct sockaddr_in6);
6408d61a0ae0SRandall Stewart 				if (sa->sa_len != incr) {
6409c4739e2fSRandall Stewart 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6410d61a0ae0SRandall Stewart 					*error = EINVAL;
6411d61a0ae0SRandall Stewart 					*bad_addr = 1;
6412d61a0ae0SRandall Stewart 					return (NULL);
6413d61a0ae0SRandall Stewart 				}
6414ea5eba11SMichael Tuexen 				break;
6415ea5eba11SMichael Tuexen 			}
6416ea5eba11SMichael Tuexen #endif
6417ea5eba11SMichael Tuexen 		default:
641817205eccSRandall Stewart 			*totaddr = i;
641917205eccSRandall Stewart 			/* we are done */
642017205eccSRandall Stewart 			break;
642117205eccSRandall Stewart 		}
6422ea5eba11SMichael Tuexen 		if (i == (size_t)*totaddr) {
6423ea5eba11SMichael Tuexen 			break;
6424ea5eba11SMichael Tuexen 		}
6425d61a0ae0SRandall Stewart 		SCTP_INP_INCR_REF(inp);
642617205eccSRandall Stewart 		stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
642717205eccSRandall Stewart 		if (stcb != NULL) {
642817205eccSRandall Stewart 			/* Already have or am bring up an association */
642917205eccSRandall Stewart 			return (stcb);
6430d61a0ae0SRandall Stewart 		} else {
6431d61a0ae0SRandall Stewart 			SCTP_INP_DECR_REF(inp);
643217205eccSRandall Stewart 		}
64334c9179adSRandall Stewart 		if ((at + incr) > (size_t)limit) {
643417205eccSRandall Stewart 			*totaddr = i;
643517205eccSRandall Stewart 			break;
643617205eccSRandall Stewart 		}
643717205eccSRandall Stewart 		sa = (struct sockaddr *)((caddr_t)sa + incr);
643817205eccSRandall Stewart 	}
643917205eccSRandall Stewart 	return ((struct sctp_tcb *)NULL);
644017205eccSRandall Stewart }
644135918f85SRandall Stewart 
644235918f85SRandall Stewart /*
644335918f85SRandall Stewart  * sctp_bindx(ADD) for one address.
644435918f85SRandall Stewart  * assumes all arguments are valid/checked by caller.
644535918f85SRandall Stewart  */
644635918f85SRandall Stewart void
644735918f85SRandall Stewart sctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp,
644835918f85SRandall Stewart     struct sockaddr *sa, sctp_assoc_t assoc_id,
644935918f85SRandall Stewart     uint32_t vrf_id, int *error, void *p)
645035918f85SRandall Stewart {
645135918f85SRandall Stewart 	struct sockaddr *addr_touse;
64525e2c2d87SRandall Stewart 
64535e2c2d87SRandall Stewart #ifdef INET6
645435918f85SRandall Stewart 	struct sockaddr_in sin;
645535918f85SRandall Stewart 
64565e2c2d87SRandall Stewart #endif
64575e2c2d87SRandall Stewart 
645835918f85SRandall Stewart 	/* see if we're bound all already! */
645935918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6460c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
646135918f85SRandall Stewart 		*error = EINVAL;
646235918f85SRandall Stewart 		return;
646335918f85SRandall Stewart 	}
646435918f85SRandall Stewart 	addr_touse = sa;
6465ea5eba11SMichael Tuexen #ifdef INET6
646635918f85SRandall Stewart 	if (sa->sa_family == AF_INET6) {
646735918f85SRandall Stewart 		struct sockaddr_in6 *sin6;
646835918f85SRandall Stewart 
646935918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6470c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
647135918f85SRandall Stewart 			*error = EINVAL;
647235918f85SRandall Stewart 			return;
647335918f85SRandall Stewart 		}
6474db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6475db4fd95bSRandall Stewart 			/* can only bind v6 on PF_INET6 sockets */
6476c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6477db4fd95bSRandall Stewart 			*error = EINVAL;
6478db4fd95bSRandall Stewart 			return;
6479db4fd95bSRandall Stewart 		}
648035918f85SRandall Stewart 		sin6 = (struct sockaddr_in6 *)addr_touse;
648135918f85SRandall Stewart 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6482db4fd95bSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6483db4fd95bSRandall Stewart 			    SCTP_IPV6_V6ONLY(inp)) {
6484db4fd95bSRandall Stewart 				/* can't bind v4-mapped on PF_INET sockets */
6485c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6486db4fd95bSRandall Stewart 				*error = EINVAL;
6487db4fd95bSRandall Stewart 				return;
6488db4fd95bSRandall Stewart 			}
648935918f85SRandall Stewart 			in6_sin6_2_sin(&sin, sin6);
649035918f85SRandall Stewart 			addr_touse = (struct sockaddr *)&sin;
649135918f85SRandall Stewart 		}
649235918f85SRandall Stewart 	}
649335918f85SRandall Stewart #endif
6494ea5eba11SMichael Tuexen #ifdef INET
649535918f85SRandall Stewart 	if (sa->sa_family == AF_INET) {
649635918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in)) {
6497c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
649835918f85SRandall Stewart 			*error = EINVAL;
649935918f85SRandall Stewart 			return;
650035918f85SRandall Stewart 		}
6501db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6502db4fd95bSRandall Stewart 		    SCTP_IPV6_V6ONLY(inp)) {
6503db4fd95bSRandall Stewart 			/* can't bind v4 on PF_INET sockets */
6504c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6505db4fd95bSRandall Stewart 			*error = EINVAL;
6506db4fd95bSRandall Stewart 			return;
6507db4fd95bSRandall Stewart 		}
650835918f85SRandall Stewart 	}
6509ea5eba11SMichael Tuexen #endif
651035918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
651135918f85SRandall Stewart 		if (p == NULL) {
651235918f85SRandall Stewart 			/* Can't get proc for Net/Open BSD */
6513c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
651435918f85SRandall Stewart 			*error = EINVAL;
651535918f85SRandall Stewart 			return;
651635918f85SRandall Stewart 		}
65171b649582SRandall Stewart 		*error = sctp_inpcb_bind(so, addr_touse, NULL, p);
651835918f85SRandall Stewart 		return;
651935918f85SRandall Stewart 	}
652035918f85SRandall Stewart 	/*
652135918f85SRandall Stewart 	 * No locks required here since bind and mgmt_ep_sa all do their own
652235918f85SRandall Stewart 	 * locking. If we do something for the FIX: below we may need to
652335918f85SRandall Stewart 	 * lock in that case.
652435918f85SRandall Stewart 	 */
652535918f85SRandall Stewart 	if (assoc_id == 0) {
652635918f85SRandall Stewart 		/* add the address */
652735918f85SRandall Stewart 		struct sctp_inpcb *lep;
652897c76f10SRandall Stewart 		struct sockaddr_in *lsin = (struct sockaddr_in *)addr_touse;
652935918f85SRandall Stewart 
653097c76f10SRandall Stewart 		/* validate the incoming port */
653197c76f10SRandall Stewart 		if ((lsin->sin_port != 0) &&
653297c76f10SRandall Stewart 		    (lsin->sin_port != inp->sctp_lport)) {
6533c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
653497c76f10SRandall Stewart 			*error = EINVAL;
653597c76f10SRandall Stewart 			return;
653697c76f10SRandall Stewart 		} else {
653797c76f10SRandall Stewart 			/* user specified 0 port, set it to existing port */
653897c76f10SRandall Stewart 			lsin->sin_port = inp->sctp_lport;
653997c76f10SRandall Stewart 		}
654097c76f10SRandall Stewart 
654135918f85SRandall Stewart 		lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id);
654235918f85SRandall Stewart 		if (lep != NULL) {
654335918f85SRandall Stewart 			/*
654435918f85SRandall Stewart 			 * We must decrement the refcount since we have the
654535918f85SRandall Stewart 			 * ep already and are binding. No remove going on
654635918f85SRandall Stewart 			 * here.
654735918f85SRandall Stewart 			 */
65486d9e8f2bSRandall Stewart 			SCTP_INP_DECR_REF(lep);
654935918f85SRandall Stewart 		}
655035918f85SRandall Stewart 		if (lep == inp) {
655135918f85SRandall Stewart 			/* already bound to it.. ok */
655235918f85SRandall Stewart 			return;
655335918f85SRandall Stewart 		} else if (lep == NULL) {
655435918f85SRandall Stewart 			((struct sockaddr_in *)addr_touse)->sin_port = 0;
655535918f85SRandall Stewart 			*error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
655635918f85SRandall Stewart 			    SCTP_ADD_IP_ADDRESS,
655780fefe0aSRandall Stewart 			    vrf_id, NULL);
655835918f85SRandall Stewart 		} else {
655935918f85SRandall Stewart 			*error = EADDRINUSE;
656035918f85SRandall Stewart 		}
656135918f85SRandall Stewart 		if (*error)
656235918f85SRandall Stewart 			return;
656335918f85SRandall Stewart 	} else {
656435918f85SRandall Stewart 		/*
656535918f85SRandall Stewart 		 * FIX: decide whether we allow assoc based bindx
656635918f85SRandall Stewart 		 */
656735918f85SRandall Stewart 	}
656835918f85SRandall Stewart }
656935918f85SRandall Stewart 
657035918f85SRandall Stewart /*
657135918f85SRandall Stewart  * sctp_bindx(DELETE) for one address.
657235918f85SRandall Stewart  * assumes all arguments are valid/checked by caller.
657335918f85SRandall Stewart  */
657435918f85SRandall Stewart void
65757215cc1bSMichael Tuexen sctp_bindx_delete_address(struct sctp_inpcb *inp,
657635918f85SRandall Stewart     struct sockaddr *sa, sctp_assoc_t assoc_id,
657735918f85SRandall Stewart     uint32_t vrf_id, int *error)
657835918f85SRandall Stewart {
657935918f85SRandall Stewart 	struct sockaddr *addr_touse;
65805e2c2d87SRandall Stewart 
65815e2c2d87SRandall Stewart #ifdef INET6
658235918f85SRandall Stewart 	struct sockaddr_in sin;
658335918f85SRandall Stewart 
65845e2c2d87SRandall Stewart #endif
65855e2c2d87SRandall Stewart 
658635918f85SRandall Stewart 	/* see if we're bound all already! */
658735918f85SRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6588c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
658935918f85SRandall Stewart 		*error = EINVAL;
659035918f85SRandall Stewart 		return;
659135918f85SRandall Stewart 	}
659235918f85SRandall Stewart 	addr_touse = sa;
6593e0e00a4dSMichael Tuexen #ifdef INET6
659435918f85SRandall Stewart 	if (sa->sa_family == AF_INET6) {
659535918f85SRandall Stewart 		struct sockaddr_in6 *sin6;
659635918f85SRandall Stewart 
659735918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in6)) {
6598c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
659935918f85SRandall Stewart 			*error = EINVAL;
660035918f85SRandall Stewart 			return;
660135918f85SRandall Stewart 		}
6602db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6603db4fd95bSRandall Stewart 			/* can only bind v6 on PF_INET6 sockets */
6604c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6605db4fd95bSRandall Stewart 			*error = EINVAL;
6606db4fd95bSRandall Stewart 			return;
6607db4fd95bSRandall Stewart 		}
660835918f85SRandall Stewart 		sin6 = (struct sockaddr_in6 *)addr_touse;
660935918f85SRandall Stewart 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6610db4fd95bSRandall Stewart 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6611db4fd95bSRandall Stewart 			    SCTP_IPV6_V6ONLY(inp)) {
6612db4fd95bSRandall Stewart 				/* can't bind mapped-v4 on PF_INET sockets */
6613c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6614db4fd95bSRandall Stewart 				*error = EINVAL;
6615db4fd95bSRandall Stewart 				return;
6616db4fd95bSRandall Stewart 			}
661735918f85SRandall Stewart 			in6_sin6_2_sin(&sin, sin6);
661835918f85SRandall Stewart 			addr_touse = (struct sockaddr *)&sin;
661935918f85SRandall Stewart 		}
662035918f85SRandall Stewart 	}
662135918f85SRandall Stewart #endif
6622ea5eba11SMichael Tuexen #ifdef INET
662335918f85SRandall Stewart 	if (sa->sa_family == AF_INET) {
662435918f85SRandall Stewart 		if (sa->sa_len != sizeof(struct sockaddr_in)) {
6625c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
662635918f85SRandall Stewart 			*error = EINVAL;
662735918f85SRandall Stewart 			return;
662835918f85SRandall Stewart 		}
6629db4fd95bSRandall Stewart 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
6630db4fd95bSRandall Stewart 		    SCTP_IPV6_V6ONLY(inp)) {
6631db4fd95bSRandall Stewart 			/* can't bind v4 on PF_INET sockets */
6632c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6633db4fd95bSRandall Stewart 			*error = EINVAL;
6634db4fd95bSRandall Stewart 			return;
6635db4fd95bSRandall Stewart 		}
663635918f85SRandall Stewart 	}
6637ea5eba11SMichael Tuexen #endif
663835918f85SRandall Stewart 	/*
663935918f85SRandall Stewart 	 * No lock required mgmt_ep_sa does its own locking. If the FIX:
664035918f85SRandall Stewart 	 * below is ever changed we may need to lock before calling
664135918f85SRandall Stewart 	 * association level binding.
664235918f85SRandall Stewart 	 */
664335918f85SRandall Stewart 	if (assoc_id == 0) {
664435918f85SRandall Stewart 		/* delete the address */
664535918f85SRandall Stewart 		*error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
664635918f85SRandall Stewart 		    SCTP_DEL_IP_ADDRESS,
664780fefe0aSRandall Stewart 		    vrf_id, NULL);
664835918f85SRandall Stewart 	} else {
664935918f85SRandall Stewart 		/*
665035918f85SRandall Stewart 		 * FIX: decide whether we allow assoc based bindx
665135918f85SRandall Stewart 		 */
665235918f85SRandall Stewart 	}
665335918f85SRandall Stewart }
66541b649582SRandall Stewart 
66551b649582SRandall Stewart /*
66561b649582SRandall Stewart  * returns the valid local address count for an assoc, taking into account
66571b649582SRandall Stewart  * all scoping rules
66581b649582SRandall Stewart  */
66591b649582SRandall Stewart int
66601b649582SRandall Stewart sctp_local_addr_count(struct sctp_tcb *stcb)
66611b649582SRandall Stewart {
6662b54ddf22SMichael Tuexen 	int loopback_scope;
6663b54ddf22SMichael Tuexen 
6664b54ddf22SMichael Tuexen #if defined(INET)
6665b54ddf22SMichael Tuexen 	int ipv4_local_scope, ipv4_addr_legal;
6666b54ddf22SMichael Tuexen 
6667b54ddf22SMichael Tuexen #endif
6668b54ddf22SMichael Tuexen #if defined (INET6)
6669b54ddf22SMichael Tuexen 	int local_scope, site_scope, ipv6_addr_legal;
6670b54ddf22SMichael Tuexen 
6671b54ddf22SMichael Tuexen #endif
66721b649582SRandall Stewart 	struct sctp_vrf *vrf;
66731b649582SRandall Stewart 	struct sctp_ifn *sctp_ifn;
66741b649582SRandall Stewart 	struct sctp_ifa *sctp_ifa;
66751b649582SRandall Stewart 	int count = 0;
66761b649582SRandall Stewart 
66771b649582SRandall Stewart 	/* Turn on all the appropriate scopes */
6678a1cb341bSMichael Tuexen 	loopback_scope = stcb->asoc.scope.loopback_scope;
6679b54ddf22SMichael Tuexen #if defined(INET)
6680a1cb341bSMichael Tuexen 	ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
6681b54ddf22SMichael Tuexen 	ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
6682b54ddf22SMichael Tuexen #endif
6683b54ddf22SMichael Tuexen #if defined(INET6)
6684a1cb341bSMichael Tuexen 	local_scope = stcb->asoc.scope.local_scope;
6685a1cb341bSMichael Tuexen 	site_scope = stcb->asoc.scope.site_scope;
6686a1cb341bSMichael Tuexen 	ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
6687b54ddf22SMichael Tuexen #endif
6688c99efcf6SRandall Stewart 	SCTP_IPI_ADDR_RLOCK();
66891b649582SRandall Stewart 	vrf = sctp_find_vrf(stcb->asoc.vrf_id);
66901b649582SRandall Stewart 	if (vrf == NULL) {
66911b649582SRandall Stewart 		/* no vrf, no addresses */
6692c99efcf6SRandall Stewart 		SCTP_IPI_ADDR_RUNLOCK();
66931b649582SRandall Stewart 		return (0);
66941b649582SRandall Stewart 	}
66951b649582SRandall Stewart 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
66961b649582SRandall Stewart 		/*
66971b649582SRandall Stewart 		 * bound all case: go through all ifns on the vrf
66981b649582SRandall Stewart 		 */
66991b649582SRandall Stewart 		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
67001b649582SRandall Stewart 			if ((loopback_scope == 0) &&
67011b649582SRandall Stewart 			    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
67021b649582SRandall Stewart 				continue;
67031b649582SRandall Stewart 			}
67041b649582SRandall Stewart 			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
67051b649582SRandall Stewart 				if (sctp_is_addr_restricted(stcb, sctp_ifa))
67061b649582SRandall Stewart 					continue;
67075e2c2d87SRandall Stewart 				switch (sctp_ifa->address.sa.sa_family) {
6708ea5eba11SMichael Tuexen #ifdef INET
67095e2c2d87SRandall Stewart 				case AF_INET:
67105e2c2d87SRandall Stewart 					if (ipv4_addr_legal) {
67111b649582SRandall Stewart 						struct sockaddr_in *sin;
67121b649582SRandall Stewart 
671324aaac8dSMichael Tuexen 						sin = &sctp_ifa->address.sin;
67141b649582SRandall Stewart 						if (sin->sin_addr.s_addr == 0) {
67155e2c2d87SRandall Stewart 							/*
67165e2c2d87SRandall Stewart 							 * skip unspecified
67175e2c2d87SRandall Stewart 							 * addrs
67185e2c2d87SRandall Stewart 							 */
67191b649582SRandall Stewart 							continue;
67201b649582SRandall Stewart 						}
67216ba22f19SMichael Tuexen 						if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
67226ba22f19SMichael Tuexen 						    &sin->sin_addr) != 0) {
67236ba22f19SMichael Tuexen 							continue;
67246ba22f19SMichael Tuexen 						}
67251b649582SRandall Stewart 						if ((ipv4_local_scope == 0) &&
67261b649582SRandall Stewart 						    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
67271b649582SRandall Stewart 							continue;
67281b649582SRandall Stewart 						}
67291b649582SRandall Stewart 						/* count this one */
67301b649582SRandall Stewart 						count++;
67315e2c2d87SRandall Stewart 					} else {
67325e2c2d87SRandall Stewart 						continue;
67335e2c2d87SRandall Stewart 					}
67345e2c2d87SRandall Stewart 					break;
6735ea5eba11SMichael Tuexen #endif
67365e2c2d87SRandall Stewart #ifdef INET6
67375e2c2d87SRandall Stewart 				case AF_INET6:
67385e2c2d87SRandall Stewart 					if (ipv6_addr_legal) {
67391b649582SRandall Stewart 						struct sockaddr_in6 *sin6;
67401b649582SRandall Stewart 
674124aaac8dSMichael Tuexen 						sin6 = &sctp_ifa->address.sin6;
67421b649582SRandall Stewart 						if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
67431b649582SRandall Stewart 							continue;
67441b649582SRandall Stewart 						}
67456ba22f19SMichael Tuexen 						if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
67466ba22f19SMichael Tuexen 						    &sin6->sin6_addr) != 0) {
67476ba22f19SMichael Tuexen 							continue;
67486ba22f19SMichael Tuexen 						}
67491b649582SRandall Stewart 						if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
67501b649582SRandall Stewart 							if (local_scope == 0)
67511b649582SRandall Stewart 								continue;
67521b649582SRandall Stewart 							if (sin6->sin6_scope_id == 0) {
67531b649582SRandall Stewart 								if (sa6_recoverscope(sin6) != 0)
67541b649582SRandall Stewart 									/*
67555e2c2d87SRandall Stewart 									 *
67565e2c2d87SRandall Stewart 									 * bad
67575e2c2d87SRandall Stewart 									 *
67585e2c2d87SRandall Stewart 									 * li
67595e2c2d87SRandall Stewart 									 * nk
67605e2c2d87SRandall Stewart 									 *
67615e2c2d87SRandall Stewart 									 * loc
67625e2c2d87SRandall Stewart 									 * al
67635e2c2d87SRandall Stewart 									 *
67645e2c2d87SRandall Stewart 									 * add
67655e2c2d87SRandall Stewart 									 * re
67665e2c2d87SRandall Stewart 									 * ss
67675e2c2d87SRandall Stewart 									 * */
67681b649582SRandall Stewart 									continue;
67691b649582SRandall Stewart 							}
67701b649582SRandall Stewart 						}
67711b649582SRandall Stewart 						if ((site_scope == 0) &&
67721b649582SRandall Stewart 						    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
67731b649582SRandall Stewart 							continue;
67741b649582SRandall Stewart 						}
67751b649582SRandall Stewart 						/* count this one */
67761b649582SRandall Stewart 						count++;
67771b649582SRandall Stewart 					}
67785e2c2d87SRandall Stewart 					break;
67795e2c2d87SRandall Stewart #endif
67805e2c2d87SRandall Stewart 				default:
67815e2c2d87SRandall Stewart 					/* TSNH */
67825e2c2d87SRandall Stewart 					break;
67835e2c2d87SRandall Stewart 				}
67841b649582SRandall Stewart 			}
67851b649582SRandall Stewart 		}
67861b649582SRandall Stewart 	} else {
67871b649582SRandall Stewart 		/*
67881b649582SRandall Stewart 		 * subset bound case
67891b649582SRandall Stewart 		 */
67901b649582SRandall Stewart 		struct sctp_laddr *laddr;
67911b649582SRandall Stewart 
67921b649582SRandall Stewart 		LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list,
67931b649582SRandall Stewart 		    sctp_nxt_addr) {
67941b649582SRandall Stewart 			if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
67951b649582SRandall Stewart 				continue;
67961b649582SRandall Stewart 			}
67971b649582SRandall Stewart 			/* count this one */
67981b649582SRandall Stewart 			count++;
67991b649582SRandall Stewart 		}
68001b649582SRandall Stewart 	}
6801c99efcf6SRandall Stewart 	SCTP_IPI_ADDR_RUNLOCK();
68021b649582SRandall Stewart 	return (count);
68031b649582SRandall Stewart }
6804c4739e2fSRandall Stewart 
6805c4739e2fSRandall Stewart #if defined(SCTP_LOCAL_TRACE_BUF)
6806c4739e2fSRandall Stewart 
6807c4739e2fSRandall Stewart void
6808b27a6b7dSRandall 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)
6809c4739e2fSRandall Stewart {
6810b27a6b7dSRandall Stewart 	uint32_t saveindex, newindex;
6811c4739e2fSRandall Stewart 
6812c4739e2fSRandall Stewart 	do {
6813b3f1ea41SRandall Stewart 		saveindex = SCTP_BASE_SYSCTL(sctp_log).index;
6814c4739e2fSRandall Stewart 		if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
6815c4739e2fSRandall Stewart 			newindex = 1;
6816c4739e2fSRandall Stewart 		} else {
6817c4739e2fSRandall Stewart 			newindex = saveindex + 1;
6818c4739e2fSRandall Stewart 		}
6819b3f1ea41SRandall Stewart 	} while (atomic_cmpset_int(&SCTP_BASE_SYSCTL(sctp_log).index, saveindex, newindex) == 0);
6820c4739e2fSRandall Stewart 	if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
6821c4739e2fSRandall Stewart 		saveindex = 0;
6822c4739e2fSRandall Stewart 	}
6823b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].timestamp = SCTP_GET_CYCLECOUNT;
6824b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].subsys = subsys;
6825b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[0] = a;
6826b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[1] = b;
6827b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[2] = c;
6828b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[3] = d;
6829b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[4] = e;
6830b3f1ea41SRandall Stewart 	SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[5] = f;
6831c4739e2fSRandall Stewart }
6832c4739e2fSRandall Stewart 
6833c4739e2fSRandall Stewart #endif
6834a99b6783SRandall Stewart static void
6835a99b6783SRandall Stewart sctp_recv_udp_tunneled_packet(struct mbuf *m, int off, struct inpcb *ignored)
6836a99b6783SRandall Stewart {
6837a99b6783SRandall Stewart 	struct ip *iph;
68383a51a264SMichael Tuexen 
68393a51a264SMichael Tuexen #ifdef INET6
68403a51a264SMichael Tuexen 	struct ip6_hdr *ip6;
68413a51a264SMichael Tuexen 
68423a51a264SMichael Tuexen #endif
6843a99b6783SRandall Stewart 	struct mbuf *sp, *last;
6844a99b6783SRandall Stewart 	struct udphdr *uhdr;
6845285052f0SMichael Tuexen 	uint16_t port;
6846a99b6783SRandall Stewart 
6847a99b6783SRandall Stewart 	if ((m->m_flags & M_PKTHDR) == 0) {
6848a99b6783SRandall Stewart 		/* Can't handle one that is not a pkt hdr */
6849a99b6783SRandall Stewart 		goto out;
6850a99b6783SRandall Stewart 	}
6851285052f0SMichael Tuexen 	/* Pull the src port */
6852a99b6783SRandall Stewart 	iph = mtod(m, struct ip *);
6853a99b6783SRandall Stewart 	uhdr = (struct udphdr *)((caddr_t)iph + off);
6854a99b6783SRandall Stewart 	port = uhdr->uh_sport;
6855285052f0SMichael Tuexen 	/*
6856285052f0SMichael Tuexen 	 * Split out the mbuf chain. Leave the IP header in m, place the
6857285052f0SMichael Tuexen 	 * rest in the sp.
6858285052f0SMichael Tuexen 	 */
6859eb1b1807SGleb Smirnoff 	sp = m_split(m, off, M_NOWAIT);
6860a99b6783SRandall Stewart 	if (sp == NULL) {
6861a99b6783SRandall Stewart 		/* Gak, drop packet, we can't do a split */
6862a99b6783SRandall Stewart 		goto out;
6863a99b6783SRandall Stewart 	}
6864285052f0SMichael Tuexen 	if (sp->m_pkthdr.len < sizeof(struct udphdr) + sizeof(struct sctphdr)) {
6865285052f0SMichael Tuexen 		/* Gak, packet can't have an SCTP header in it - too small */
6866a99b6783SRandall Stewart 		m_freem(sp);
6867a99b6783SRandall Stewart 		goto out;
6868a99b6783SRandall Stewart 	}
6869285052f0SMichael Tuexen 	/* Now pull up the UDP header and SCTP header together */
6870285052f0SMichael Tuexen 	sp = m_pullup(sp, sizeof(struct udphdr) + sizeof(struct sctphdr));
6871a99b6783SRandall Stewart 	if (sp == NULL) {
6872a99b6783SRandall Stewart 		/* Gak pullup failed */
6873a99b6783SRandall Stewart 		goto out;
6874a99b6783SRandall Stewart 	}
6875285052f0SMichael Tuexen 	/* Trim out the UDP header */
6876a99b6783SRandall Stewart 	m_adj(sp, sizeof(struct udphdr));
6877a99b6783SRandall Stewart 
6878a99b6783SRandall Stewart 	/* Now reconstruct the mbuf chain */
6879285052f0SMichael Tuexen 	for (last = m; last->m_next; last = last->m_next);
6880a99b6783SRandall Stewart 	last->m_next = sp;
6881a99b6783SRandall Stewart 	m->m_pkthdr.len += sp->m_pkthdr.len;
6882a99b6783SRandall Stewart 	iph = mtod(m, struct ip *);
6883a99b6783SRandall Stewart 	switch (iph->ip_v) {
6884e6194c2eSMichael Tuexen #ifdef INET
6885a99b6783SRandall Stewart 	case IPVERSION:
688609c1c856SMichael Tuexen 		iph->ip_len = htons(ntohs(iph->ip_len) - sizeof(struct udphdr));
6887a99b6783SRandall Stewart 		sctp_input_with_port(m, off, port);
6888a99b6783SRandall Stewart 		break;
6889e6194c2eSMichael Tuexen #endif
6890a99b6783SRandall Stewart #ifdef INET6
6891a99b6783SRandall Stewart 	case IPV6_VERSION >> 4:
68923a51a264SMichael Tuexen 		ip6 = mtod(m, struct ip6_hdr *);
68933a51a264SMichael Tuexen 		ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - sizeof(struct udphdr));
68943a51a264SMichael Tuexen 		sctp6_input_with_port(&m, &off, port);
6895a99b6783SRandall Stewart 		break;
6896a99b6783SRandall Stewart #endif
6897a99b6783SRandall Stewart 	default:
6898285052f0SMichael Tuexen 		goto out;
6899a99b6783SRandall Stewart 		break;
6900a99b6783SRandall Stewart 	}
6901a99b6783SRandall Stewart 	return;
6902a99b6783SRandall Stewart out:
6903a99b6783SRandall Stewart 	m_freem(m);
6904a99b6783SRandall Stewart }
6905c54a18d2SRandall Stewart 
6906c54a18d2SRandall Stewart void
6907c54a18d2SRandall Stewart sctp_over_udp_stop(void)
6908c54a18d2SRandall Stewart {
6909a99b6783SRandall Stewart 	/*
6910a99b6783SRandall Stewart 	 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
6911a99b6783SRandall Stewart 	 * for writting!
6912a99b6783SRandall Stewart 	 */
69133a51a264SMichael Tuexen #ifdef INET
69143a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
69153a51a264SMichael Tuexen 		soclose(SCTP_BASE_INFO(udp4_tun_socket));
69163a51a264SMichael Tuexen 		SCTP_BASE_INFO(udp4_tun_socket) = NULL;
6917c54a18d2SRandall Stewart 	}
69183a51a264SMichael Tuexen #endif
69193a51a264SMichael Tuexen #ifdef INET6
69203a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
69213a51a264SMichael Tuexen 		soclose(SCTP_BASE_INFO(udp6_tun_socket));
69223a51a264SMichael Tuexen 		SCTP_BASE_INFO(udp6_tun_socket) = NULL;
69233a51a264SMichael Tuexen 	}
69243a51a264SMichael Tuexen #endif
6925a99b6783SRandall Stewart }
6926ea5eba11SMichael Tuexen 
6927c54a18d2SRandall Stewart int
6928c54a18d2SRandall Stewart sctp_over_udp_start(void)
6929c54a18d2SRandall Stewart {
6930a99b6783SRandall Stewart 	uint16_t port;
6931a99b6783SRandall Stewart 	int ret;
6932a99b6783SRandall Stewart 
69333a51a264SMichael Tuexen #ifdef INET
69343a51a264SMichael Tuexen 	struct sockaddr_in sin;
69353a51a264SMichael Tuexen 
69363a51a264SMichael Tuexen #endif
69373a51a264SMichael Tuexen #ifdef INET6
69383a51a264SMichael Tuexen 	struct sockaddr_in6 sin6;
69393a51a264SMichael Tuexen 
69403a51a264SMichael Tuexen #endif
6941a99b6783SRandall Stewart 	/*
6942a99b6783SRandall Stewart 	 * This function assumes sysctl caller holds sctp_sysctl_info_lock()
6943a99b6783SRandall Stewart 	 * for writting!
6944a99b6783SRandall Stewart 	 */
6945a99b6783SRandall Stewart 	port = SCTP_BASE_SYSCTL(sctp_udp_tunneling_port);
69463a51a264SMichael Tuexen 	if (ntohs(port) == 0) {
6947a99b6783SRandall Stewart 		/* Must have a port set */
6948a99b6783SRandall Stewart 		return (EINVAL);
6949a99b6783SRandall Stewart 	}
69503a51a264SMichael Tuexen #ifdef INET
69513a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
6952a99b6783SRandall Stewart 		/* Already running -- must stop first */
6953a99b6783SRandall Stewart 		return (EALREADY);
6954a99b6783SRandall Stewart 	}
69553a51a264SMichael Tuexen #endif
69563a51a264SMichael Tuexen #ifdef INET6
69573a51a264SMichael Tuexen 	if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
69583a51a264SMichael Tuexen 		/* Already running -- must stop first */
69593a51a264SMichael Tuexen 		return (EALREADY);
6960a99b6783SRandall Stewart 	}
69613a51a264SMichael Tuexen #endif
69623a51a264SMichael Tuexen #ifdef INET
69633a51a264SMichael Tuexen 	if ((ret = socreate(PF_INET, &SCTP_BASE_INFO(udp4_tun_socket),
69643a51a264SMichael Tuexen 	    SOCK_DGRAM, IPPROTO_UDP,
69653a51a264SMichael Tuexen 	    curthread->td_ucred, curthread))) {
6966a99b6783SRandall Stewart 		sctp_over_udp_stop();
6967a99b6783SRandall Stewart 		return (ret);
6968a99b6783SRandall Stewart 	}
69693a51a264SMichael Tuexen 	/* Call the special UDP hook. */
69703a51a264SMichael Tuexen 	if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp4_tun_socket),
69713a51a264SMichael Tuexen 	    sctp_recv_udp_tunneled_packet))) {
69723a51a264SMichael Tuexen 		sctp_over_udp_stop();
69733a51a264SMichael Tuexen 		return (ret);
69743a51a264SMichael Tuexen 	}
69753a51a264SMichael Tuexen 	/* Ok, we have a socket, bind it to the port. */
69763a51a264SMichael Tuexen 	memset(&sin, 0, sizeof(struct sockaddr_in));
69773a51a264SMichael Tuexen 	sin.sin_len = sizeof(struct sockaddr_in);
69783a51a264SMichael Tuexen 	sin.sin_family = AF_INET;
69793a51a264SMichael Tuexen 	sin.sin_port = htons(port);
69803a51a264SMichael Tuexen 	if ((ret = sobind(SCTP_BASE_INFO(udp4_tun_socket),
69813a51a264SMichael Tuexen 	    (struct sockaddr *)&sin, curthread))) {
69823a51a264SMichael Tuexen 		sctp_over_udp_stop();
69833a51a264SMichael Tuexen 		return (ret);
69843a51a264SMichael Tuexen 	}
69853a51a264SMichael Tuexen #endif
69863a51a264SMichael Tuexen #ifdef INET6
69873a51a264SMichael Tuexen 	if ((ret = socreate(PF_INET6, &SCTP_BASE_INFO(udp6_tun_socket),
69883a51a264SMichael Tuexen 	    SOCK_DGRAM, IPPROTO_UDP,
69893a51a264SMichael Tuexen 	    curthread->td_ucred, curthread))) {
69903a51a264SMichael Tuexen 		sctp_over_udp_stop();
69913a51a264SMichael Tuexen 		return (ret);
69923a51a264SMichael Tuexen 	}
69933a51a264SMichael Tuexen 	/* Call the special UDP hook. */
69943a51a264SMichael Tuexen 	if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp6_tun_socket),
69953a51a264SMichael Tuexen 	    sctp_recv_udp_tunneled_packet))) {
69963a51a264SMichael Tuexen 		sctp_over_udp_stop();
69973a51a264SMichael Tuexen 		return (ret);
69983a51a264SMichael Tuexen 	}
69993a51a264SMichael Tuexen 	/* Ok, we have a socket, bind it to the port. */
70003a51a264SMichael Tuexen 	memset(&sin6, 0, sizeof(struct sockaddr_in6));
70013a51a264SMichael Tuexen 	sin6.sin6_len = sizeof(struct sockaddr_in6);
70023a51a264SMichael Tuexen 	sin6.sin6_family = AF_INET6;
70033a51a264SMichael Tuexen 	sin6.sin6_port = htons(port);
70043a51a264SMichael Tuexen 	if ((ret = sobind(SCTP_BASE_INFO(udp6_tun_socket),
70053a51a264SMichael Tuexen 	    (struct sockaddr *)&sin6, curthread))) {
70063a51a264SMichael Tuexen 		sctp_over_udp_stop();
70073a51a264SMichael Tuexen 		return (ret);
70083a51a264SMichael Tuexen 	}
70093a51a264SMichael Tuexen #endif
7010a99b6783SRandall Stewart 	return (0);
7011c54a18d2SRandall Stewart }
7012