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 42f8829a4aSRandall Stewart #endif 43f8829a4aSRandall Stewart #include <netinet/sctp_header.h> 44f8829a4aSRandall Stewart #include <netinet/sctp_output.h> 45f8829a4aSRandall Stewart #include <netinet/sctp_uio.h> 46f8829a4aSRandall Stewart #include <netinet/sctp_timer.h> 47f8829a4aSRandall Stewart #include <netinet/sctp_indata.h>/* for sctp_deliver_data() */ 48f8829a4aSRandall Stewart #include <netinet/sctp_auth.h> 49f8829a4aSRandall Stewart #include <netinet/sctp_asconf.h> 50f7517433SRandall Stewart #include <netinet/sctp_bsd_addr.h> 51f8829a4aSRandall Stewart 52f8829a4aSRandall Stewart 53b9e7085aSRandall Stewart #ifndef KTR_SCTP 54b9e7085aSRandall Stewart #define KTR_SCTP KTR_SUBSYS 5580fefe0aSRandall Stewart #endif 56f8829a4aSRandall Stewart 570e9a9c10SMichael Tuexen extern struct sctp_cc_functions sctp_cc_functions[]; 58f7a77f6fSMichael Tuexen extern struct sctp_ss_functions sctp_ss_functions[]; 590e9a9c10SMichael Tuexen 60f8829a4aSRandall Stewart void 61dcb68fbaSMichael Tuexen sctp_sblog(struct sockbuf *sb, struct sctp_tcb *stcb, int from, int incr) 62f8829a4aSRandall Stewart { 6380fefe0aSRandall Stewart struct sctp_cwnd_log sctp_clog; 64f8829a4aSRandall Stewart 6580fefe0aSRandall Stewart sctp_clog.x.sb.stcb = stcb; 6680fefe0aSRandall Stewart sctp_clog.x.sb.so_sbcc = sb->sb_cc; 67f8829a4aSRandall Stewart if (stcb) 6880fefe0aSRandall Stewart sctp_clog.x.sb.stcb_sbcc = stcb->asoc.sb_cc; 69f8829a4aSRandall Stewart else 7080fefe0aSRandall Stewart sctp_clog.x.sb.stcb_sbcc = 0; 7180fefe0aSRandall Stewart sctp_clog.x.sb.incr = incr; 72c4739e2fSRandall Stewart SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", 7380fefe0aSRandall Stewart SCTP_LOG_EVENT_SB, 7480fefe0aSRandall Stewart from, 7580fefe0aSRandall Stewart sctp_clog.x.misc.log1, 7680fefe0aSRandall Stewart sctp_clog.x.misc.log2, 7780fefe0aSRandall Stewart sctp_clog.x.misc.log3, 7880fefe0aSRandall Stewart sctp_clog.x.misc.log4); 79f8829a4aSRandall Stewart } 80f8829a4aSRandall Stewart 81f8829a4aSRandall Stewart void 82f8829a4aSRandall Stewart sctp_log_closing(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int16_t loc) 83f8829a4aSRandall Stewart { 8480fefe0aSRandall Stewart struct sctp_cwnd_log sctp_clog; 85f8829a4aSRandall Stewart 8680fefe0aSRandall Stewart sctp_clog.x.close.inp = (void *)inp; 8780fefe0aSRandall Stewart sctp_clog.x.close.sctp_flags = inp->sctp_flags; 88f8829a4aSRandall Stewart if (stcb) { 8980fefe0aSRandall Stewart sctp_clog.x.close.stcb = (void *)stcb; 9080fefe0aSRandall Stewart sctp_clog.x.close.state = (uint16_t) stcb->asoc.state; 91f8829a4aSRandall Stewart } else { 9280fefe0aSRandall Stewart sctp_clog.x.close.stcb = 0; 9380fefe0aSRandall Stewart sctp_clog.x.close.state = 0; 94f8829a4aSRandall Stewart } 9580fefe0aSRandall Stewart sctp_clog.x.close.loc = loc; 96c4739e2fSRandall Stewart SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", 9780fefe0aSRandall Stewart SCTP_LOG_EVENT_CLOSE, 9880fefe0aSRandall Stewart 0, 9980fefe0aSRandall Stewart sctp_clog.x.misc.log1, 10080fefe0aSRandall Stewart sctp_clog.x.misc.log2, 10180fefe0aSRandall Stewart sctp_clog.x.misc.log3, 10280fefe0aSRandall Stewart sctp_clog.x.misc.log4); 103f8829a4aSRandall Stewart } 104f8829a4aSRandall Stewart 105f8829a4aSRandall Stewart void 106f8829a4aSRandall Stewart rto_logging(struct sctp_nets *net, int from) 107f8829a4aSRandall Stewart { 10880fefe0aSRandall Stewart struct sctp_cwnd_log sctp_clog; 109f8829a4aSRandall Stewart 110bfefd190SRandall Stewart memset(&sctp_clog, 0, sizeof(sctp_clog)); 11180fefe0aSRandall Stewart sctp_clog.x.rto.net = (void *)net; 112be1d9176SMichael Tuexen sctp_clog.x.rto.rtt = net->rtt / 1000; 113c4739e2fSRandall Stewart SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", 11480fefe0aSRandall Stewart SCTP_LOG_EVENT_RTT, 11580fefe0aSRandall Stewart from, 11680fefe0aSRandall Stewart sctp_clog.x.misc.log1, 11780fefe0aSRandall Stewart sctp_clog.x.misc.log2, 11880fefe0aSRandall Stewart sctp_clog.x.misc.log3, 11980fefe0aSRandall Stewart sctp_clog.x.misc.log4); 120f8829a4aSRandall Stewart } 121f8829a4aSRandall Stewart 122f8829a4aSRandall Stewart void 1236a91f103SRandall Stewart sctp_log_strm_del_alt(struct sctp_tcb *stcb, uint32_t tsn, uint16_t sseq, uint16_t stream, int from) 124f8829a4aSRandall Stewart { 12580fefe0aSRandall Stewart struct sctp_cwnd_log sctp_clog; 126f8829a4aSRandall Stewart 12780fefe0aSRandall Stewart sctp_clog.x.strlog.stcb = stcb; 12880fefe0aSRandall Stewart sctp_clog.x.strlog.n_tsn = tsn; 12980fefe0aSRandall Stewart sctp_clog.x.strlog.n_sseq = sseq; 13080fefe0aSRandall Stewart sctp_clog.x.strlog.e_tsn = 0; 13180fefe0aSRandall Stewart sctp_clog.x.strlog.e_sseq = 0; 13280fefe0aSRandall Stewart sctp_clog.x.strlog.strm = stream; 133c4739e2fSRandall Stewart SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", 13480fefe0aSRandall Stewart SCTP_LOG_EVENT_STRM, 13580fefe0aSRandall Stewart from, 13680fefe0aSRandall Stewart sctp_clog.x.misc.log1, 13780fefe0aSRandall Stewart sctp_clog.x.misc.log2, 13880fefe0aSRandall Stewart sctp_clog.x.misc.log3, 13980fefe0aSRandall Stewart sctp_clog.x.misc.log4); 140f8829a4aSRandall Stewart } 141f8829a4aSRandall Stewart 142f8829a4aSRandall Stewart void 143f8829a4aSRandall Stewart sctp_log_nagle_event(struct sctp_tcb *stcb, int action) 144f8829a4aSRandall Stewart { 14580fefe0aSRandall Stewart struct sctp_cwnd_log sctp_clog; 146f8829a4aSRandall Stewart 14780fefe0aSRandall Stewart sctp_clog.x.nagle.stcb = (void *)stcb; 14880fefe0aSRandall Stewart sctp_clog.x.nagle.total_flight = stcb->asoc.total_flight; 14980fefe0aSRandall Stewart sctp_clog.x.nagle.total_in_queue = stcb->asoc.total_output_queue_size; 15080fefe0aSRandall Stewart sctp_clog.x.nagle.count_in_queue = stcb->asoc.chunks_on_out_queue; 15180fefe0aSRandall Stewart sctp_clog.x.nagle.count_in_flight = stcb->asoc.total_flight_count; 152c4739e2fSRandall Stewart SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", 15380fefe0aSRandall Stewart SCTP_LOG_EVENT_NAGLE, 15480fefe0aSRandall Stewart action, 15580fefe0aSRandall Stewart sctp_clog.x.misc.log1, 15680fefe0aSRandall Stewart sctp_clog.x.misc.log2, 15780fefe0aSRandall Stewart sctp_clog.x.misc.log3, 15880fefe0aSRandall Stewart sctp_clog.x.misc.log4); 159f8829a4aSRandall Stewart } 160f8829a4aSRandall Stewart 161f8829a4aSRandall Stewart void 162f8829a4aSRandall Stewart sctp_log_sack(uint32_t old_cumack, uint32_t cumack, uint32_t tsn, uint16_t gaps, uint16_t dups, int from) 163f8829a4aSRandall Stewart { 16480fefe0aSRandall Stewart struct sctp_cwnd_log sctp_clog; 165f8829a4aSRandall Stewart 16680fefe0aSRandall Stewart sctp_clog.x.sack.cumack = cumack; 16780fefe0aSRandall Stewart sctp_clog.x.sack.oldcumack = old_cumack; 16880fefe0aSRandall Stewart sctp_clog.x.sack.tsn = tsn; 16980fefe0aSRandall Stewart sctp_clog.x.sack.numGaps = gaps; 17080fefe0aSRandall Stewart sctp_clog.x.sack.numDups = dups; 171c4739e2fSRandall Stewart SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", 17280fefe0aSRandall Stewart SCTP_LOG_EVENT_SACK, 17380fefe0aSRandall Stewart from, 17480fefe0aSRandall Stewart sctp_clog.x.misc.log1, 17580fefe0aSRandall Stewart sctp_clog.x.misc.log2, 17680fefe0aSRandall Stewart sctp_clog.x.misc.log3, 17780fefe0aSRandall Stewart sctp_clog.x.misc.log4); 178f8829a4aSRandall Stewart } 179f8829a4aSRandall Stewart 180f8829a4aSRandall Stewart void 181f8829a4aSRandall Stewart sctp_log_map(uint32_t map, uint32_t cum, uint32_t high, int from) 182f8829a4aSRandall Stewart { 18380fefe0aSRandall Stewart struct sctp_cwnd_log sctp_clog; 184f8829a4aSRandall Stewart 185bfefd190SRandall Stewart memset(&sctp_clog, 0, sizeof(sctp_clog)); 18680fefe0aSRandall Stewart sctp_clog.x.map.base = map; 18780fefe0aSRandall Stewart sctp_clog.x.map.cum = cum; 18880fefe0aSRandall Stewart sctp_clog.x.map.high = high; 189c4739e2fSRandall Stewart SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", 19080fefe0aSRandall Stewart SCTP_LOG_EVENT_MAP, 19180fefe0aSRandall Stewart from, 19280fefe0aSRandall Stewart sctp_clog.x.misc.log1, 19380fefe0aSRandall Stewart sctp_clog.x.misc.log2, 19480fefe0aSRandall Stewart sctp_clog.x.misc.log3, 19580fefe0aSRandall Stewart sctp_clog.x.misc.log4); 196f8829a4aSRandall Stewart } 197f8829a4aSRandall Stewart 198f8829a4aSRandall Stewart void 199dcb68fbaSMichael Tuexen sctp_log_fr(uint32_t biggest_tsn, uint32_t biggest_new_tsn, uint32_t tsn, int from) 200f8829a4aSRandall Stewart { 20180fefe0aSRandall Stewart struct sctp_cwnd_log sctp_clog; 202f8829a4aSRandall Stewart 203bfefd190SRandall Stewart memset(&sctp_clog, 0, sizeof(sctp_clog)); 20480fefe0aSRandall Stewart sctp_clog.x.fr.largest_tsn = biggest_tsn; 20580fefe0aSRandall Stewart sctp_clog.x.fr.largest_new_tsn = biggest_new_tsn; 20680fefe0aSRandall Stewart sctp_clog.x.fr.tsn = tsn; 207c4739e2fSRandall Stewart SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", 20880fefe0aSRandall Stewart SCTP_LOG_EVENT_FR, 20980fefe0aSRandall Stewart from, 21080fefe0aSRandall Stewart sctp_clog.x.misc.log1, 21180fefe0aSRandall Stewart sctp_clog.x.misc.log2, 21280fefe0aSRandall Stewart sctp_clog.x.misc.log3, 21380fefe0aSRandall Stewart sctp_clog.x.misc.log4); 214f8829a4aSRandall Stewart } 215f8829a4aSRandall Stewart 216f8829a4aSRandall Stewart void 217f8829a4aSRandall Stewart sctp_log_mb(struct mbuf *m, int from) 218f8829a4aSRandall Stewart { 21980fefe0aSRandall Stewart struct sctp_cwnd_log sctp_clog; 220f8829a4aSRandall Stewart 22180fefe0aSRandall Stewart sctp_clog.x.mb.mp = m; 22280fefe0aSRandall Stewart sctp_clog.x.mb.mbuf_flags = (uint8_t) (SCTP_BUF_GET_FLAGS(m)); 22380fefe0aSRandall Stewart sctp_clog.x.mb.size = (uint16_t) (SCTP_BUF_LEN(m)); 22480fefe0aSRandall Stewart sctp_clog.x.mb.data = SCTP_BUF_AT(m, 0); 225139bc87fSRandall Stewart if (SCTP_BUF_IS_EXTENDED(m)) { 22680fefe0aSRandall Stewart sctp_clog.x.mb.ext = SCTP_BUF_EXTEND_BASE(m); 22780fefe0aSRandall Stewart sctp_clog.x.mb.refcnt = (uint8_t) (SCTP_BUF_EXTEND_REFCNT(m)); 228f8829a4aSRandall Stewart } else { 22980fefe0aSRandall Stewart sctp_clog.x.mb.ext = 0; 23080fefe0aSRandall Stewart sctp_clog.x.mb.refcnt = 0; 231f8829a4aSRandall Stewart } 232c4739e2fSRandall Stewart SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", 23380fefe0aSRandall Stewart SCTP_LOG_EVENT_MBUF, 23480fefe0aSRandall Stewart from, 23580fefe0aSRandall Stewart sctp_clog.x.misc.log1, 23680fefe0aSRandall Stewart sctp_clog.x.misc.log2, 23780fefe0aSRandall Stewart sctp_clog.x.misc.log3, 23880fefe0aSRandall Stewart sctp_clog.x.misc.log4); 239f8829a4aSRandall Stewart } 240f8829a4aSRandall Stewart 241f8829a4aSRandall Stewart void 242dcb68fbaSMichael Tuexen sctp_log_strm_del(struct sctp_queued_to_read *control, struct sctp_queued_to_read *poschk, int from) 243f8829a4aSRandall Stewart { 24480fefe0aSRandall Stewart struct sctp_cwnd_log sctp_clog; 245f8829a4aSRandall Stewart 246f8829a4aSRandall Stewart if (control == NULL) { 247ad81507eSRandall Stewart SCTP_PRINTF("Gak log of NULL?\n"); 248f8829a4aSRandall Stewart return; 249f8829a4aSRandall Stewart } 25080fefe0aSRandall Stewart sctp_clog.x.strlog.stcb = control->stcb; 25180fefe0aSRandall Stewart sctp_clog.x.strlog.n_tsn = control->sinfo_tsn; 25280fefe0aSRandall Stewart sctp_clog.x.strlog.n_sseq = control->sinfo_ssn; 25380fefe0aSRandall Stewart sctp_clog.x.strlog.strm = control->sinfo_stream; 254f8829a4aSRandall Stewart if (poschk != NULL) { 25580fefe0aSRandall Stewart sctp_clog.x.strlog.e_tsn = poschk->sinfo_tsn; 25680fefe0aSRandall Stewart sctp_clog.x.strlog.e_sseq = poschk->sinfo_ssn; 257f8829a4aSRandall Stewart } else { 25880fefe0aSRandall Stewart sctp_clog.x.strlog.e_tsn = 0; 25980fefe0aSRandall Stewart sctp_clog.x.strlog.e_sseq = 0; 260f8829a4aSRandall Stewart } 261c4739e2fSRandall Stewart SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", 26280fefe0aSRandall Stewart SCTP_LOG_EVENT_STRM, 26380fefe0aSRandall Stewart from, 26480fefe0aSRandall Stewart sctp_clog.x.misc.log1, 26580fefe0aSRandall Stewart sctp_clog.x.misc.log2, 26680fefe0aSRandall Stewart sctp_clog.x.misc.log3, 26780fefe0aSRandall Stewart sctp_clog.x.misc.log4); 268f8829a4aSRandall Stewart } 269f8829a4aSRandall Stewart 270f8829a4aSRandall Stewart void 271f8829a4aSRandall Stewart sctp_log_cwnd(struct sctp_tcb *stcb, struct sctp_nets *net, int augment, uint8_t from) 272f8829a4aSRandall Stewart { 27380fefe0aSRandall Stewart struct sctp_cwnd_log sctp_clog; 274f8829a4aSRandall Stewart 27580fefe0aSRandall Stewart sctp_clog.x.cwnd.net = net; 276f8829a4aSRandall Stewart if (stcb->asoc.send_queue_cnt > 255) 27780fefe0aSRandall Stewart sctp_clog.x.cwnd.cnt_in_send = 255; 278f8829a4aSRandall Stewart else 27980fefe0aSRandall Stewart sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt; 280f8829a4aSRandall Stewart if (stcb->asoc.stream_queue_cnt > 255) 28180fefe0aSRandall Stewart sctp_clog.x.cwnd.cnt_in_str = 255; 282f8829a4aSRandall Stewart else 28380fefe0aSRandall Stewart sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt; 284f8829a4aSRandall Stewart 285f8829a4aSRandall Stewart if (net) { 28680fefe0aSRandall Stewart sctp_clog.x.cwnd.cwnd_new_value = net->cwnd; 28780fefe0aSRandall Stewart sctp_clog.x.cwnd.inflight = net->flight_size; 28880fefe0aSRandall Stewart sctp_clog.x.cwnd.pseudo_cumack = net->pseudo_cumack; 28980fefe0aSRandall Stewart sctp_clog.x.cwnd.meets_pseudo_cumack = net->new_pseudo_cumack; 29080fefe0aSRandall Stewart sctp_clog.x.cwnd.need_new_pseudo_cumack = net->find_pseudo_cumack; 291f8829a4aSRandall Stewart } 292f8829a4aSRandall Stewart if (SCTP_CWNDLOG_PRESEND == from) { 29380fefe0aSRandall Stewart sctp_clog.x.cwnd.meets_pseudo_cumack = stcb->asoc.peers_rwnd; 294f8829a4aSRandall Stewart } 29580fefe0aSRandall Stewart sctp_clog.x.cwnd.cwnd_augment = augment; 296c4739e2fSRandall Stewart SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", 29780fefe0aSRandall Stewart SCTP_LOG_EVENT_CWND, 29880fefe0aSRandall Stewart from, 29980fefe0aSRandall Stewart sctp_clog.x.misc.log1, 30080fefe0aSRandall Stewart sctp_clog.x.misc.log2, 30180fefe0aSRandall Stewart sctp_clog.x.misc.log3, 30280fefe0aSRandall Stewart sctp_clog.x.misc.log4); 303f8829a4aSRandall Stewart } 304f8829a4aSRandall Stewart 305f8829a4aSRandall Stewart void 306f8829a4aSRandall Stewart sctp_log_lock(struct sctp_inpcb *inp, struct sctp_tcb *stcb, uint8_t from) 307f8829a4aSRandall Stewart { 30880fefe0aSRandall Stewart struct sctp_cwnd_log sctp_clog; 309f8829a4aSRandall Stewart 310bfefd190SRandall Stewart memset(&sctp_clog, 0, sizeof(sctp_clog)); 31103b0b021SRandall Stewart if (inp) { 31280fefe0aSRandall Stewart sctp_clog.x.lock.sock = (void *)inp->sctp_socket; 31303b0b021SRandall Stewart 31403b0b021SRandall Stewart } else { 31580fefe0aSRandall Stewart sctp_clog.x.lock.sock = (void *)NULL; 31603b0b021SRandall Stewart } 31780fefe0aSRandall Stewart sctp_clog.x.lock.inp = (void *)inp; 318f8829a4aSRandall Stewart if (stcb) { 31980fefe0aSRandall Stewart sctp_clog.x.lock.tcb_lock = mtx_owned(&stcb->tcb_mtx); 320f8829a4aSRandall Stewart } else { 32180fefe0aSRandall Stewart sctp_clog.x.lock.tcb_lock = SCTP_LOCK_UNKNOWN; 322f8829a4aSRandall Stewart } 323f8829a4aSRandall Stewart if (inp) { 32480fefe0aSRandall Stewart sctp_clog.x.lock.inp_lock = mtx_owned(&inp->inp_mtx); 32580fefe0aSRandall Stewart sctp_clog.x.lock.create_lock = mtx_owned(&inp->inp_create_mtx); 326f8829a4aSRandall Stewart } else { 32780fefe0aSRandall Stewart sctp_clog.x.lock.inp_lock = SCTP_LOCK_UNKNOWN; 32880fefe0aSRandall Stewart sctp_clog.x.lock.create_lock = SCTP_LOCK_UNKNOWN; 329f8829a4aSRandall Stewart } 330b3f1ea41SRandall Stewart sctp_clog.x.lock.info_lock = rw_wowned(&SCTP_BASE_INFO(ipi_ep_mtx)); 33152129fcdSRandall Stewart if (inp && (inp->sctp_socket)) { 33280fefe0aSRandall Stewart sctp_clog.x.lock.sock_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx)); 33380fefe0aSRandall Stewart sctp_clog.x.lock.sockrcvbuf_lock = mtx_owned(&(inp->sctp_socket->so_rcv.sb_mtx)); 33480fefe0aSRandall Stewart sctp_clog.x.lock.socksndbuf_lock = mtx_owned(&(inp->sctp_socket->so_snd.sb_mtx)); 335f8829a4aSRandall Stewart } else { 33680fefe0aSRandall Stewart sctp_clog.x.lock.sock_lock = SCTP_LOCK_UNKNOWN; 33780fefe0aSRandall Stewart sctp_clog.x.lock.sockrcvbuf_lock = SCTP_LOCK_UNKNOWN; 33880fefe0aSRandall Stewart sctp_clog.x.lock.socksndbuf_lock = SCTP_LOCK_UNKNOWN; 339f8829a4aSRandall Stewart } 340c4739e2fSRandall Stewart SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", 34180fefe0aSRandall Stewart SCTP_LOG_LOCK_EVENT, 34280fefe0aSRandall Stewart from, 34380fefe0aSRandall Stewart sctp_clog.x.misc.log1, 34480fefe0aSRandall Stewart sctp_clog.x.misc.log2, 34580fefe0aSRandall Stewart sctp_clog.x.misc.log3, 34680fefe0aSRandall Stewart sctp_clog.x.misc.log4); 347f8829a4aSRandall Stewart } 348f8829a4aSRandall Stewart 349f8829a4aSRandall Stewart void 350f8829a4aSRandall Stewart sctp_log_maxburst(struct sctp_tcb *stcb, struct sctp_nets *net, int error, int burst, uint8_t from) 351f8829a4aSRandall Stewart { 35280fefe0aSRandall Stewart struct sctp_cwnd_log sctp_clog; 353f8829a4aSRandall Stewart 354bfefd190SRandall Stewart memset(&sctp_clog, 0, sizeof(sctp_clog)); 35580fefe0aSRandall Stewart sctp_clog.x.cwnd.net = net; 35680fefe0aSRandall Stewart sctp_clog.x.cwnd.cwnd_new_value = error; 35780fefe0aSRandall Stewart sctp_clog.x.cwnd.inflight = net->flight_size; 35880fefe0aSRandall Stewart sctp_clog.x.cwnd.cwnd_augment = burst; 359f8829a4aSRandall Stewart if (stcb->asoc.send_queue_cnt > 255) 36080fefe0aSRandall Stewart sctp_clog.x.cwnd.cnt_in_send = 255; 361f8829a4aSRandall Stewart else 36280fefe0aSRandall Stewart sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt; 363f8829a4aSRandall Stewart if (stcb->asoc.stream_queue_cnt > 255) 36480fefe0aSRandall Stewart sctp_clog.x.cwnd.cnt_in_str = 255; 365f8829a4aSRandall Stewart else 36680fefe0aSRandall Stewart sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt; 367c4739e2fSRandall Stewart SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", 36880fefe0aSRandall Stewart SCTP_LOG_EVENT_MAXBURST, 36980fefe0aSRandall Stewart from, 37080fefe0aSRandall Stewart sctp_clog.x.misc.log1, 37180fefe0aSRandall Stewart sctp_clog.x.misc.log2, 37280fefe0aSRandall Stewart sctp_clog.x.misc.log3, 37380fefe0aSRandall Stewart sctp_clog.x.misc.log4); 374f8829a4aSRandall Stewart } 375f8829a4aSRandall Stewart 376f8829a4aSRandall Stewart void 377f8829a4aSRandall Stewart sctp_log_rwnd(uint8_t from, uint32_t peers_rwnd, uint32_t snd_size, uint32_t overhead) 378f8829a4aSRandall Stewart { 37980fefe0aSRandall Stewart struct sctp_cwnd_log sctp_clog; 380f8829a4aSRandall Stewart 38180fefe0aSRandall Stewart sctp_clog.x.rwnd.rwnd = peers_rwnd; 38280fefe0aSRandall Stewart sctp_clog.x.rwnd.send_size = snd_size; 38380fefe0aSRandall Stewart sctp_clog.x.rwnd.overhead = overhead; 38480fefe0aSRandall Stewart sctp_clog.x.rwnd.new_rwnd = 0; 385c4739e2fSRandall Stewart SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", 38680fefe0aSRandall Stewart SCTP_LOG_EVENT_RWND, 38780fefe0aSRandall Stewart from, 38880fefe0aSRandall Stewart sctp_clog.x.misc.log1, 38980fefe0aSRandall Stewart sctp_clog.x.misc.log2, 39080fefe0aSRandall Stewart sctp_clog.x.misc.log3, 39180fefe0aSRandall Stewart sctp_clog.x.misc.log4); 392f8829a4aSRandall Stewart } 393f8829a4aSRandall Stewart 394f8829a4aSRandall Stewart void 395f8829a4aSRandall Stewart sctp_log_rwnd_set(uint8_t from, uint32_t peers_rwnd, uint32_t flight_size, uint32_t overhead, uint32_t a_rwndval) 396f8829a4aSRandall Stewart { 39780fefe0aSRandall Stewart struct sctp_cwnd_log sctp_clog; 398f8829a4aSRandall Stewart 39980fefe0aSRandall Stewart sctp_clog.x.rwnd.rwnd = peers_rwnd; 40080fefe0aSRandall Stewart sctp_clog.x.rwnd.send_size = flight_size; 40180fefe0aSRandall Stewart sctp_clog.x.rwnd.overhead = overhead; 40280fefe0aSRandall Stewart sctp_clog.x.rwnd.new_rwnd = a_rwndval; 403c4739e2fSRandall Stewart SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", 40480fefe0aSRandall Stewart SCTP_LOG_EVENT_RWND, 40580fefe0aSRandall Stewart from, 40680fefe0aSRandall Stewart sctp_clog.x.misc.log1, 40780fefe0aSRandall Stewart sctp_clog.x.misc.log2, 40880fefe0aSRandall Stewart sctp_clog.x.misc.log3, 40980fefe0aSRandall Stewart sctp_clog.x.misc.log4); 410f8829a4aSRandall Stewart } 411f8829a4aSRandall Stewart 412f8829a4aSRandall Stewart void 413f8829a4aSRandall Stewart sctp_log_mbcnt(uint8_t from, uint32_t total_oq, uint32_t book, uint32_t total_mbcnt_q, uint32_t mbcnt) 414f8829a4aSRandall Stewart { 41580fefe0aSRandall Stewart struct sctp_cwnd_log sctp_clog; 416f8829a4aSRandall Stewart 41780fefe0aSRandall Stewart sctp_clog.x.mbcnt.total_queue_size = total_oq; 41880fefe0aSRandall Stewart sctp_clog.x.mbcnt.size_change = book; 41980fefe0aSRandall Stewart sctp_clog.x.mbcnt.total_queue_mb_size = total_mbcnt_q; 42080fefe0aSRandall Stewart sctp_clog.x.mbcnt.mbcnt_change = mbcnt; 421c4739e2fSRandall Stewart SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", 42280fefe0aSRandall Stewart SCTP_LOG_EVENT_MBCNT, 42380fefe0aSRandall Stewart from, 42480fefe0aSRandall Stewart sctp_clog.x.misc.log1, 42580fefe0aSRandall Stewart sctp_clog.x.misc.log2, 42680fefe0aSRandall Stewart sctp_clog.x.misc.log3, 42780fefe0aSRandall Stewart sctp_clog.x.misc.log4); 428f8829a4aSRandall Stewart } 429f8829a4aSRandall Stewart 430f8829a4aSRandall Stewart void 431f8829a4aSRandall Stewart sctp_misc_ints(uint8_t from, uint32_t a, uint32_t b, uint32_t c, uint32_t d) 432f8829a4aSRandall Stewart { 433c4739e2fSRandall Stewart SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", 43480fefe0aSRandall Stewart SCTP_LOG_MISC_EVENT, 43580fefe0aSRandall Stewart from, 43680fefe0aSRandall Stewart a, b, c, d); 437f8829a4aSRandall Stewart } 438f8829a4aSRandall Stewart 439f8829a4aSRandall Stewart void 4407215cc1bSMichael Tuexen sctp_wakeup_log(struct sctp_tcb *stcb, uint32_t wake_cnt, int from) 441f8829a4aSRandall Stewart { 44280fefe0aSRandall Stewart struct sctp_cwnd_log sctp_clog; 443f8829a4aSRandall Stewart 44480fefe0aSRandall Stewart sctp_clog.x.wake.stcb = (void *)stcb; 44580fefe0aSRandall Stewart sctp_clog.x.wake.wake_cnt = wake_cnt; 44680fefe0aSRandall Stewart sctp_clog.x.wake.flight = stcb->asoc.total_flight_count; 44780fefe0aSRandall Stewart sctp_clog.x.wake.send_q = stcb->asoc.send_queue_cnt; 44880fefe0aSRandall Stewart sctp_clog.x.wake.sent_q = stcb->asoc.sent_queue_cnt; 449f8829a4aSRandall Stewart 450f8829a4aSRandall Stewart if (stcb->asoc.stream_queue_cnt < 0xff) 45180fefe0aSRandall Stewart sctp_clog.x.wake.stream_qcnt = (uint8_t) stcb->asoc.stream_queue_cnt; 452f8829a4aSRandall Stewart else 45380fefe0aSRandall Stewart sctp_clog.x.wake.stream_qcnt = 0xff; 454f8829a4aSRandall Stewart 455f8829a4aSRandall Stewart if (stcb->asoc.chunks_on_out_queue < 0xff) 45680fefe0aSRandall Stewart sctp_clog.x.wake.chunks_on_oque = (uint8_t) stcb->asoc.chunks_on_out_queue; 457f8829a4aSRandall Stewart else 45880fefe0aSRandall Stewart sctp_clog.x.wake.chunks_on_oque = 0xff; 459f8829a4aSRandall Stewart 46080fefe0aSRandall Stewart sctp_clog.x.wake.sctpflags = 0; 461f8829a4aSRandall Stewart /* set in the defered mode stuff */ 462f8829a4aSRandall Stewart if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) 46380fefe0aSRandall Stewart sctp_clog.x.wake.sctpflags |= 1; 464f8829a4aSRandall Stewart if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) 46580fefe0aSRandall Stewart sctp_clog.x.wake.sctpflags |= 2; 466f8829a4aSRandall Stewart if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) 46780fefe0aSRandall Stewart sctp_clog.x.wake.sctpflags |= 4; 468f8829a4aSRandall Stewart /* what about the sb */ 469f8829a4aSRandall Stewart if (stcb->sctp_socket) { 470f8829a4aSRandall Stewart struct socket *so = stcb->sctp_socket; 471f8829a4aSRandall Stewart 47280fefe0aSRandall Stewart sctp_clog.x.wake.sbflags = (uint8_t) ((so->so_snd.sb_flags & 0x00ff)); 473f8829a4aSRandall Stewart } else { 47480fefe0aSRandall Stewart sctp_clog.x.wake.sbflags = 0xff; 475f8829a4aSRandall Stewart } 476c4739e2fSRandall Stewart SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", 47780fefe0aSRandall Stewart SCTP_LOG_EVENT_WAKE, 47880fefe0aSRandall Stewart from, 47980fefe0aSRandall Stewart sctp_clog.x.misc.log1, 48080fefe0aSRandall Stewart sctp_clog.x.misc.log2, 48180fefe0aSRandall Stewart sctp_clog.x.misc.log3, 48280fefe0aSRandall Stewart sctp_clog.x.misc.log4); 483f8829a4aSRandall Stewart } 484f8829a4aSRandall Stewart 485f8829a4aSRandall Stewart void 4867215cc1bSMichael Tuexen sctp_log_block(uint8_t from, struct sctp_association *asoc, int sendlen) 487f8829a4aSRandall Stewart { 48880fefe0aSRandall Stewart struct sctp_cwnd_log sctp_clog; 489f8829a4aSRandall Stewart 49080fefe0aSRandall Stewart sctp_clog.x.blk.onsb = asoc->total_output_queue_size; 49180fefe0aSRandall Stewart sctp_clog.x.blk.send_sent_qcnt = (uint16_t) (asoc->send_queue_cnt + asoc->sent_queue_cnt); 49280fefe0aSRandall Stewart sctp_clog.x.blk.peer_rwnd = asoc->peers_rwnd; 49380fefe0aSRandall Stewart sctp_clog.x.blk.stream_qcnt = (uint16_t) asoc->stream_queue_cnt; 49480fefe0aSRandall Stewart sctp_clog.x.blk.chunks_on_oque = (uint16_t) asoc->chunks_on_out_queue; 49580fefe0aSRandall Stewart sctp_clog.x.blk.flight_size = (uint16_t) (asoc->total_flight / 1024); 49680fefe0aSRandall Stewart sctp_clog.x.blk.sndlen = sendlen; 497c4739e2fSRandall Stewart SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x", 49880fefe0aSRandall Stewart SCTP_LOG_EVENT_BLOCK, 49980fefe0aSRandall Stewart from, 50080fefe0aSRandall Stewart sctp_clog.x.misc.log1, 50180fefe0aSRandall Stewart sctp_clog.x.misc.log2, 50280fefe0aSRandall Stewart sctp_clog.x.misc.log3, 50380fefe0aSRandall Stewart sctp_clog.x.misc.log4); 504f8829a4aSRandall Stewart } 505f8829a4aSRandall Stewart 506f8829a4aSRandall Stewart int 5077215cc1bSMichael Tuexen sctp_fill_stat_log(void *optval SCTP_UNUSED, size_t *optsize SCTP_UNUSED) 508f8829a4aSRandall Stewart { 50980fefe0aSRandall Stewart /* May need to fix this if ktrdump does not work */ 510f8829a4aSRandall Stewart return (0); 511f8829a4aSRandall Stewart } 512f8829a4aSRandall Stewart 513f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED 514f8829a4aSRandall Stewart uint8_t sctp_audit_data[SCTP_AUDIT_SIZE][2]; 515f8829a4aSRandall Stewart static int sctp_audit_indx = 0; 516f8829a4aSRandall Stewart 517f8829a4aSRandall Stewart static 518f8829a4aSRandall Stewart void 519f8829a4aSRandall Stewart sctp_print_audit_report(void) 520f8829a4aSRandall Stewart { 521f8829a4aSRandall Stewart int i; 522f8829a4aSRandall Stewart int cnt; 523f8829a4aSRandall Stewart 524f8829a4aSRandall Stewart cnt = 0; 525f8829a4aSRandall Stewart for (i = sctp_audit_indx; i < SCTP_AUDIT_SIZE; i++) { 526f8829a4aSRandall Stewart if ((sctp_audit_data[i][0] == 0xe0) && 527f8829a4aSRandall Stewart (sctp_audit_data[i][1] == 0x01)) { 528f8829a4aSRandall Stewart cnt = 0; 529ad81507eSRandall Stewart SCTP_PRINTF("\n"); 530f8829a4aSRandall Stewart } else if (sctp_audit_data[i][0] == 0xf0) { 531f8829a4aSRandall Stewart cnt = 0; 532ad81507eSRandall Stewart SCTP_PRINTF("\n"); 533f8829a4aSRandall Stewart } else if ((sctp_audit_data[i][0] == 0xc0) && 534f8829a4aSRandall Stewart (sctp_audit_data[i][1] == 0x01)) { 535ad81507eSRandall Stewart SCTP_PRINTF("\n"); 536f8829a4aSRandall Stewart cnt = 0; 537f8829a4aSRandall Stewart } 538ad81507eSRandall Stewart SCTP_PRINTF("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0], 539f8829a4aSRandall Stewart (uint32_t) sctp_audit_data[i][1]); 540f8829a4aSRandall Stewart cnt++; 541f8829a4aSRandall Stewart if ((cnt % 14) == 0) 542ad81507eSRandall Stewart SCTP_PRINTF("\n"); 543f8829a4aSRandall Stewart } 544f8829a4aSRandall Stewart for (i = 0; i < sctp_audit_indx; i++) { 545f8829a4aSRandall Stewart if ((sctp_audit_data[i][0] == 0xe0) && 546f8829a4aSRandall Stewart (sctp_audit_data[i][1] == 0x01)) { 547f8829a4aSRandall Stewart cnt = 0; 548ad81507eSRandall Stewart SCTP_PRINTF("\n"); 549f8829a4aSRandall Stewart } else if (sctp_audit_data[i][0] == 0xf0) { 550f8829a4aSRandall Stewart cnt = 0; 551ad81507eSRandall Stewart SCTP_PRINTF("\n"); 552f8829a4aSRandall Stewart } else if ((sctp_audit_data[i][0] == 0xc0) && 553f8829a4aSRandall Stewart (sctp_audit_data[i][1] == 0x01)) { 554ad81507eSRandall Stewart SCTP_PRINTF("\n"); 555f8829a4aSRandall Stewart cnt = 0; 556f8829a4aSRandall Stewart } 557ad81507eSRandall Stewart SCTP_PRINTF("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0], 558f8829a4aSRandall Stewart (uint32_t) sctp_audit_data[i][1]); 559f8829a4aSRandall Stewart cnt++; 560f8829a4aSRandall Stewart if ((cnt % 14) == 0) 561ad81507eSRandall Stewart SCTP_PRINTF("\n"); 562f8829a4aSRandall Stewart } 563ad81507eSRandall Stewart SCTP_PRINTF("\n"); 564f8829a4aSRandall Stewart } 565f8829a4aSRandall Stewart 566f8829a4aSRandall Stewart void 567f8829a4aSRandall Stewart sctp_auditing(int from, struct sctp_inpcb *inp, struct sctp_tcb *stcb, 568f8829a4aSRandall Stewart struct sctp_nets *net) 569f8829a4aSRandall Stewart { 570f8829a4aSRandall Stewart int resend_cnt, tot_out, rep, tot_book_cnt; 571f8829a4aSRandall Stewart struct sctp_nets *lnet; 572f8829a4aSRandall Stewart struct sctp_tmit_chunk *chk; 573f8829a4aSRandall Stewart 574f8829a4aSRandall Stewart sctp_audit_data[sctp_audit_indx][0] = 0xAA; 575f8829a4aSRandall Stewart sctp_audit_data[sctp_audit_indx][1] = 0x000000ff & from; 576f8829a4aSRandall Stewart sctp_audit_indx++; 577f8829a4aSRandall Stewart if (sctp_audit_indx >= SCTP_AUDIT_SIZE) { 578f8829a4aSRandall Stewart sctp_audit_indx = 0; 579f8829a4aSRandall Stewart } 580f8829a4aSRandall Stewart if (inp == NULL) { 581f8829a4aSRandall Stewart sctp_audit_data[sctp_audit_indx][0] = 0xAF; 582f8829a4aSRandall Stewart sctp_audit_data[sctp_audit_indx][1] = 0x01; 583f8829a4aSRandall Stewart sctp_audit_indx++; 584f8829a4aSRandall Stewart if (sctp_audit_indx >= SCTP_AUDIT_SIZE) { 585f8829a4aSRandall Stewart sctp_audit_indx = 0; 586f8829a4aSRandall Stewart } 587f8829a4aSRandall Stewart return; 588f8829a4aSRandall Stewart } 589f8829a4aSRandall Stewart if (stcb == NULL) { 590f8829a4aSRandall Stewart sctp_audit_data[sctp_audit_indx][0] = 0xAF; 591f8829a4aSRandall Stewart sctp_audit_data[sctp_audit_indx][1] = 0x02; 592f8829a4aSRandall Stewart sctp_audit_indx++; 593f8829a4aSRandall Stewart if (sctp_audit_indx >= SCTP_AUDIT_SIZE) { 594f8829a4aSRandall Stewart sctp_audit_indx = 0; 595f8829a4aSRandall Stewart } 596f8829a4aSRandall Stewart return; 597f8829a4aSRandall Stewart } 598f8829a4aSRandall Stewart sctp_audit_data[sctp_audit_indx][0] = 0xA1; 599f8829a4aSRandall Stewart sctp_audit_data[sctp_audit_indx][1] = 600f8829a4aSRandall Stewart (0x000000ff & stcb->asoc.sent_queue_retran_cnt); 601f8829a4aSRandall Stewart sctp_audit_indx++; 602f8829a4aSRandall Stewart if (sctp_audit_indx >= SCTP_AUDIT_SIZE) { 603f8829a4aSRandall Stewart sctp_audit_indx = 0; 604f8829a4aSRandall Stewart } 605f8829a4aSRandall Stewart rep = 0; 606f8829a4aSRandall Stewart tot_book_cnt = 0; 607f8829a4aSRandall Stewart resend_cnt = tot_out = 0; 608f8829a4aSRandall Stewart TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 609f8829a4aSRandall Stewart if (chk->sent == SCTP_DATAGRAM_RESEND) { 610f8829a4aSRandall Stewart resend_cnt++; 611f8829a4aSRandall Stewart } else if (chk->sent < SCTP_DATAGRAM_RESEND) { 612f8829a4aSRandall Stewart tot_out += chk->book_size; 613f8829a4aSRandall Stewart tot_book_cnt++; 614f8829a4aSRandall Stewart } 615f8829a4aSRandall Stewart } 616f8829a4aSRandall Stewart if (resend_cnt != stcb->asoc.sent_queue_retran_cnt) { 617f8829a4aSRandall Stewart sctp_audit_data[sctp_audit_indx][0] = 0xAF; 618f8829a4aSRandall Stewart sctp_audit_data[sctp_audit_indx][1] = 0xA1; 619f8829a4aSRandall Stewart sctp_audit_indx++; 620f8829a4aSRandall Stewart if (sctp_audit_indx >= SCTP_AUDIT_SIZE) { 621f8829a4aSRandall Stewart sctp_audit_indx = 0; 622f8829a4aSRandall Stewart } 623ad81507eSRandall Stewart SCTP_PRINTF("resend_cnt:%d asoc-tot:%d\n", 624f8829a4aSRandall Stewart resend_cnt, stcb->asoc.sent_queue_retran_cnt); 625f8829a4aSRandall Stewart rep = 1; 626f8829a4aSRandall Stewart stcb->asoc.sent_queue_retran_cnt = resend_cnt; 627f8829a4aSRandall Stewart sctp_audit_data[sctp_audit_indx][0] = 0xA2; 628f8829a4aSRandall Stewart sctp_audit_data[sctp_audit_indx][1] = 629f8829a4aSRandall Stewart (0x000000ff & stcb->asoc.sent_queue_retran_cnt); 630f8829a4aSRandall Stewart sctp_audit_indx++; 631f8829a4aSRandall Stewart if (sctp_audit_indx >= SCTP_AUDIT_SIZE) { 632f8829a4aSRandall Stewart sctp_audit_indx = 0; 633f8829a4aSRandall Stewart } 634f8829a4aSRandall Stewart } 635f8829a4aSRandall Stewart if (tot_out != stcb->asoc.total_flight) { 636f8829a4aSRandall Stewart sctp_audit_data[sctp_audit_indx][0] = 0xAF; 637f8829a4aSRandall Stewart sctp_audit_data[sctp_audit_indx][1] = 0xA2; 638f8829a4aSRandall Stewart sctp_audit_indx++; 639f8829a4aSRandall Stewart if (sctp_audit_indx >= SCTP_AUDIT_SIZE) { 640f8829a4aSRandall Stewart sctp_audit_indx = 0; 641f8829a4aSRandall Stewart } 642f8829a4aSRandall Stewart rep = 1; 643ad81507eSRandall Stewart SCTP_PRINTF("tot_flt:%d asoc_tot:%d\n", tot_out, 644f8829a4aSRandall Stewart (int)stcb->asoc.total_flight); 645f8829a4aSRandall Stewart stcb->asoc.total_flight = tot_out; 646f8829a4aSRandall Stewart } 647f8829a4aSRandall Stewart if (tot_book_cnt != stcb->asoc.total_flight_count) { 648f8829a4aSRandall Stewart sctp_audit_data[sctp_audit_indx][0] = 0xAF; 649f8829a4aSRandall Stewart sctp_audit_data[sctp_audit_indx][1] = 0xA5; 650f8829a4aSRandall Stewart sctp_audit_indx++; 651f8829a4aSRandall Stewart if (sctp_audit_indx >= SCTP_AUDIT_SIZE) { 652f8829a4aSRandall Stewart sctp_audit_indx = 0; 653f8829a4aSRandall Stewart } 654f8829a4aSRandall Stewart rep = 1; 655f31e6c7fSMichael Tuexen SCTP_PRINTF("tot_flt_book:%d\n", tot_book_cnt); 656f8829a4aSRandall Stewart 657f8829a4aSRandall Stewart stcb->asoc.total_flight_count = tot_book_cnt; 658f8829a4aSRandall Stewart } 659f8829a4aSRandall Stewart tot_out = 0; 660f8829a4aSRandall Stewart TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) { 661f8829a4aSRandall Stewart tot_out += lnet->flight_size; 662f8829a4aSRandall Stewart } 663f8829a4aSRandall Stewart if (tot_out != stcb->asoc.total_flight) { 664f8829a4aSRandall Stewart sctp_audit_data[sctp_audit_indx][0] = 0xAF; 665f8829a4aSRandall Stewart sctp_audit_data[sctp_audit_indx][1] = 0xA3; 666f8829a4aSRandall Stewart sctp_audit_indx++; 667f8829a4aSRandall Stewart if (sctp_audit_indx >= SCTP_AUDIT_SIZE) { 668f8829a4aSRandall Stewart sctp_audit_indx = 0; 669f8829a4aSRandall Stewart } 670f8829a4aSRandall Stewart rep = 1; 671ad81507eSRandall Stewart SCTP_PRINTF("real flight:%d net total was %d\n", 672f8829a4aSRandall Stewart stcb->asoc.total_flight, tot_out); 673f8829a4aSRandall Stewart /* now corrective action */ 674f8829a4aSRandall Stewart TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) { 675f8829a4aSRandall Stewart 676f8829a4aSRandall Stewart tot_out = 0; 677f8829a4aSRandall Stewart TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 678f8829a4aSRandall Stewart if ((chk->whoTo == lnet) && 679f8829a4aSRandall Stewart (chk->sent < SCTP_DATAGRAM_RESEND)) { 680f8829a4aSRandall Stewart tot_out += chk->book_size; 681f8829a4aSRandall Stewart } 682f8829a4aSRandall Stewart } 683f8829a4aSRandall Stewart if (lnet->flight_size != tot_out) { 684f31e6c7fSMichael Tuexen SCTP_PRINTF("net:%p flight was %d corrected to %d\n", 685dd294dceSMichael Tuexen (void *)lnet, lnet->flight_size, 686ad81507eSRandall Stewart tot_out); 687f8829a4aSRandall Stewart lnet->flight_size = tot_out; 688f8829a4aSRandall Stewart } 689f8829a4aSRandall Stewart } 690f8829a4aSRandall Stewart } 691f8829a4aSRandall Stewart if (rep) { 692f8829a4aSRandall Stewart sctp_print_audit_report(); 693f8829a4aSRandall Stewart } 694f8829a4aSRandall Stewart } 695f8829a4aSRandall Stewart 696f8829a4aSRandall Stewart void 697f8829a4aSRandall Stewart sctp_audit_log(uint8_t ev, uint8_t fd) 698f8829a4aSRandall Stewart { 699f8829a4aSRandall Stewart 700f8829a4aSRandall Stewart sctp_audit_data[sctp_audit_indx][0] = ev; 701f8829a4aSRandall Stewart sctp_audit_data[sctp_audit_indx][1] = fd; 702f8829a4aSRandall Stewart sctp_audit_indx++; 703f8829a4aSRandall Stewart if (sctp_audit_indx >= SCTP_AUDIT_SIZE) { 704f8829a4aSRandall Stewart sctp_audit_indx = 0; 705f8829a4aSRandall Stewart } 706f8829a4aSRandall Stewart } 707f8829a4aSRandall Stewart 708f8829a4aSRandall Stewart #endif 709f8829a4aSRandall Stewart 710f8829a4aSRandall Stewart /* 71112af6654SMichael Tuexen * sctp_stop_timers_for_shutdown() should be called 71212af6654SMichael Tuexen * when entering the SHUTDOWN_SENT or SHUTDOWN_ACK_SENT 71312af6654SMichael Tuexen * state to make sure that all timers are stopped. 71412af6654SMichael Tuexen */ 71512af6654SMichael Tuexen void 71612af6654SMichael Tuexen sctp_stop_timers_for_shutdown(struct sctp_tcb *stcb) 71712af6654SMichael Tuexen { 71812af6654SMichael Tuexen struct sctp_association *asoc; 71912af6654SMichael Tuexen struct sctp_nets *net; 72012af6654SMichael Tuexen 72112af6654SMichael Tuexen asoc = &stcb->asoc; 72212af6654SMichael Tuexen 72312af6654SMichael Tuexen (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer); 72412af6654SMichael Tuexen (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer); 72512af6654SMichael Tuexen (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer); 72612af6654SMichael Tuexen (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer); 72712af6654SMichael Tuexen (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer); 72812af6654SMichael Tuexen TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 72912af6654SMichael Tuexen (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer); 730ca85e948SMichael Tuexen (void)SCTP_OS_TIMER_STOP(&net->hb_timer.timer); 73112af6654SMichael Tuexen } 73212af6654SMichael Tuexen } 73312af6654SMichael Tuexen 73412af6654SMichael Tuexen /* 735f8829a4aSRandall Stewart * a list of sizes based on typical mtu's, used only if next hop size not 736f8829a4aSRandall Stewart * returned. 737f8829a4aSRandall Stewart */ 738437fc91aSMichael Tuexen static uint32_t sctp_mtu_sizes[] = { 739f8829a4aSRandall Stewart 68, 740f8829a4aSRandall Stewart 296, 741f8829a4aSRandall Stewart 508, 742f8829a4aSRandall Stewart 512, 743f8829a4aSRandall Stewart 544, 744f8829a4aSRandall Stewart 576, 745f8829a4aSRandall Stewart 1006, 746f8829a4aSRandall Stewart 1492, 747f8829a4aSRandall Stewart 1500, 748f8829a4aSRandall Stewart 1536, 749f8829a4aSRandall Stewart 2002, 750f8829a4aSRandall Stewart 2048, 751f8829a4aSRandall Stewart 4352, 752f8829a4aSRandall Stewart 4464, 753f8829a4aSRandall Stewart 8166, 754f8829a4aSRandall Stewart 17914, 755f8829a4aSRandall Stewart 32000, 756f8829a4aSRandall Stewart 65535 757f8829a4aSRandall Stewart }; 758f8829a4aSRandall Stewart 759f8829a4aSRandall Stewart /* 760437fc91aSMichael Tuexen * Return the largest MTU smaller than val. If there is no 761437fc91aSMichael Tuexen * entry, just return val. 762f8829a4aSRandall Stewart */ 763437fc91aSMichael Tuexen uint32_t 764437fc91aSMichael Tuexen sctp_get_prev_mtu(uint32_t val) 765437fc91aSMichael Tuexen { 766437fc91aSMichael Tuexen uint32_t i; 767437fc91aSMichael Tuexen 768437fc91aSMichael Tuexen if (val <= sctp_mtu_sizes[0]) { 769437fc91aSMichael Tuexen return (val); 770437fc91aSMichael Tuexen } 771437fc91aSMichael Tuexen for (i = 1; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) { 772437fc91aSMichael Tuexen if (val <= sctp_mtu_sizes[i]) { 773f8829a4aSRandall Stewart break; 774f8829a4aSRandall Stewart } 775f8829a4aSRandall Stewart } 776437fc91aSMichael Tuexen return (sctp_mtu_sizes[i - 1]); 777437fc91aSMichael Tuexen } 778437fc91aSMichael Tuexen 779437fc91aSMichael Tuexen /* 780437fc91aSMichael Tuexen * Return the smallest MTU larger than val. If there is no 781437fc91aSMichael Tuexen * entry, just return val. 782437fc91aSMichael Tuexen */ 783437fc91aSMichael Tuexen uint32_t 7847215cc1bSMichael Tuexen sctp_get_next_mtu(uint32_t val) 785437fc91aSMichael Tuexen { 786437fc91aSMichael Tuexen /* select another MTU that is just bigger than this one */ 787437fc91aSMichael Tuexen uint32_t i; 788437fc91aSMichael Tuexen 789437fc91aSMichael Tuexen for (i = 0; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) { 790437fc91aSMichael Tuexen if (val < sctp_mtu_sizes[i]) { 791437fc91aSMichael Tuexen return (sctp_mtu_sizes[i]); 792437fc91aSMichael Tuexen } 793437fc91aSMichael Tuexen } 794437fc91aSMichael Tuexen return (val); 795f8829a4aSRandall Stewart } 796f8829a4aSRandall Stewart 797f8829a4aSRandall Stewart void 798f8829a4aSRandall Stewart sctp_fill_random_store(struct sctp_pcb *m) 799f8829a4aSRandall Stewart { 800f8829a4aSRandall Stewart /* 801f8829a4aSRandall Stewart * Here we use the MD5/SHA-1 to hash with our good randomNumbers and 802f8829a4aSRandall Stewart * our counter. The result becomes our good random numbers and we 803f8829a4aSRandall Stewart * then setup to give these out. Note that we do no locking to 804f8829a4aSRandall Stewart * protect this. This is ok, since if competing folks call this we 80517205eccSRandall Stewart * will get more gobbled gook in the random store which is what we 806f8829a4aSRandall Stewart * want. There is a danger that two guys will use the same random 807f8829a4aSRandall Stewart * numbers, but thats ok too since that is random as well :-> 808f8829a4aSRandall Stewart */ 809f8829a4aSRandall Stewart m->store_at = 0; 810ad81507eSRandall Stewart (void)sctp_hmac(SCTP_HMAC, (uint8_t *) m->random_numbers, 811f8829a4aSRandall Stewart sizeof(m->random_numbers), (uint8_t *) & m->random_counter, 812f8829a4aSRandall Stewart sizeof(m->random_counter), (uint8_t *) m->random_store); 813f8829a4aSRandall Stewart m->random_counter++; 814f8829a4aSRandall Stewart } 815f8829a4aSRandall Stewart 816f8829a4aSRandall Stewart uint32_t 817851b7298SRandall Stewart sctp_select_initial_TSN(struct sctp_pcb *inp) 818f8829a4aSRandall Stewart { 819f8829a4aSRandall Stewart /* 820f8829a4aSRandall Stewart * A true implementation should use random selection process to get 821f8829a4aSRandall Stewart * the initial stream sequence number, using RFC1750 as a good 822f8829a4aSRandall Stewart * guideline 823f8829a4aSRandall Stewart */ 824139bc87fSRandall Stewart uint32_t x, *xp; 825f8829a4aSRandall Stewart uint8_t *p; 826851b7298SRandall Stewart int store_at, new_store; 827f8829a4aSRandall Stewart 828851b7298SRandall Stewart if (inp->initial_sequence_debug != 0) { 829f8829a4aSRandall Stewart uint32_t ret; 830f8829a4aSRandall Stewart 831851b7298SRandall Stewart ret = inp->initial_sequence_debug; 832851b7298SRandall Stewart inp->initial_sequence_debug++; 833f8829a4aSRandall Stewart return (ret); 834f8829a4aSRandall Stewart } 835851b7298SRandall Stewart retry: 836851b7298SRandall Stewart store_at = inp->store_at; 837851b7298SRandall Stewart new_store = store_at + sizeof(uint32_t); 838851b7298SRandall Stewart if (new_store >= (SCTP_SIGNATURE_SIZE - 3)) { 839851b7298SRandall Stewart new_store = 0; 840f8829a4aSRandall Stewart } 841851b7298SRandall Stewart if (!atomic_cmpset_int(&inp->store_at, store_at, new_store)) { 842851b7298SRandall Stewart goto retry; 843851b7298SRandall Stewart } 844851b7298SRandall Stewart if (new_store == 0) { 845851b7298SRandall Stewart /* Refill the random store */ 846851b7298SRandall Stewart sctp_fill_random_store(inp); 847851b7298SRandall Stewart } 848851b7298SRandall Stewart p = &inp->random_store[store_at]; 849139bc87fSRandall Stewart xp = (uint32_t *) p; 850f8829a4aSRandall Stewart x = *xp; 851f8829a4aSRandall Stewart return (x); 852f8829a4aSRandall Stewart } 853f8829a4aSRandall Stewart 854f8829a4aSRandall Stewart uint32_t 8557215cc1bSMichael Tuexen sctp_select_a_tag(struct sctp_inpcb *inp, uint16_t lport, uint16_t rport, int check) 856f8829a4aSRandall Stewart { 8577215cc1bSMichael Tuexen uint32_t x; 858f8829a4aSRandall Stewart struct timeval now; 859f8829a4aSRandall Stewart 8607215cc1bSMichael Tuexen if (check) { 8616e55db54SRandall Stewart (void)SCTP_GETTIME_TIMEVAL(&now); 8627215cc1bSMichael Tuexen } 8637215cc1bSMichael Tuexen for (;;) { 864851b7298SRandall Stewart x = sctp_select_initial_TSN(&inp->sctp_ep); 865f8829a4aSRandall Stewart if (x == 0) { 866f8829a4aSRandall Stewart /* we never use 0 */ 867f8829a4aSRandall Stewart continue; 868f8829a4aSRandall Stewart } 8697215cc1bSMichael Tuexen if (!check || sctp_is_vtag_good(x, lport, rport, &now)) { 8707215cc1bSMichael Tuexen break; 871f8829a4aSRandall Stewart } 872f8829a4aSRandall Stewart } 873f8829a4aSRandall Stewart return (x); 874f8829a4aSRandall Stewart } 875f8829a4aSRandall Stewart 876f8829a4aSRandall Stewart int 8770696e120SRandall Stewart sctp_init_asoc(struct sctp_inpcb *m, struct sctp_tcb *stcb, 878b5c16493SMichael Tuexen uint32_t override_tag, uint32_t vrf_id) 879f8829a4aSRandall Stewart { 8800696e120SRandall Stewart struct sctp_association *asoc; 8810696e120SRandall Stewart 882f8829a4aSRandall Stewart /* 883f8829a4aSRandall Stewart * Anything set to zero is taken care of by the allocation routine's 884f8829a4aSRandall Stewart * bzero 885f8829a4aSRandall Stewart */ 886f8829a4aSRandall Stewart 887f8829a4aSRandall Stewart /* 888f8829a4aSRandall Stewart * Up front select what scoping to apply on addresses I tell my peer 889f8829a4aSRandall Stewart * Not sure what to do with these right now, we will need to come up 890f8829a4aSRandall Stewart * with a way to set them. We may need to pass them through from the 891f8829a4aSRandall Stewart * caller in the sctp_aloc_assoc() function. 892f8829a4aSRandall Stewart */ 893f8829a4aSRandall Stewart int i; 894f8829a4aSRandall Stewart 8950696e120SRandall Stewart asoc = &stcb->asoc; 896f8829a4aSRandall Stewart /* init all variables to a known value. */ 897c4739e2fSRandall Stewart SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_INUSE); 898f8829a4aSRandall Stewart asoc->max_burst = m->sctp_ep.max_burst; 899899288aeSRandall Stewart asoc->fr_max_burst = m->sctp_ep.fr_max_burst; 900f8829a4aSRandall Stewart asoc->heart_beat_delay = TICKS_TO_MSEC(m->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]); 901f8829a4aSRandall Stewart asoc->cookie_life = m->sctp_ep.def_cookie_life; 90220083c2eSMichael Tuexen asoc->sctp_cmt_on_off = m->sctp_cmt_on_off; 903c446091bSMichael Tuexen asoc->ecn_allowed = m->sctp_ecn_enable; 904830d754dSRandall Stewart asoc->sctp_nr_sack_on_off = (uint8_t) SCTP_BASE_SYSCTL(sctp_nr_sack_on_off); 905ca85e948SMichael Tuexen asoc->sctp_cmt_pf = (uint8_t) 0; 906d61a0ae0SRandall Stewart asoc->sctp_frag_point = m->sctp_frag_point; 907e2e7c62eSMichael Tuexen asoc->sctp_features = m->sctp_features; 90858bdb691SMichael Tuexen asoc->default_dscp = m->sctp_ep.default_dscp; 90942551e99SRandall Stewart #ifdef INET6 91058bdb691SMichael Tuexen if (m->sctp_ep.default_flowlabel) { 91158bdb691SMichael Tuexen asoc->default_flowlabel = m->sctp_ep.default_flowlabel; 91258bdb691SMichael Tuexen } else { 91358bdb691SMichael Tuexen if (m->ip_inp.inp.inp_flags & IN6P_AUTOFLOWLABEL) { 91458bdb691SMichael Tuexen asoc->default_flowlabel = sctp_select_initial_TSN(&m->sctp_ep); 91558bdb691SMichael Tuexen asoc->default_flowlabel &= 0x000fffff; 91658bdb691SMichael Tuexen asoc->default_flowlabel |= 0x80000000; 91758bdb691SMichael Tuexen } else { 918f8829a4aSRandall Stewart asoc->default_flowlabel = 0; 91958bdb691SMichael Tuexen } 92058bdb691SMichael Tuexen } 921f8829a4aSRandall Stewart #endif 9229f22f500SRandall Stewart asoc->sb_send_resv = 0; 923f8829a4aSRandall Stewart if (override_tag) { 924f8829a4aSRandall Stewart asoc->my_vtag = override_tag; 925f8829a4aSRandall Stewart } else { 926830d754dSRandall Stewart asoc->my_vtag = sctp_select_a_tag(m, stcb->sctp_ep->sctp_lport, stcb->rport, 1); 927f8829a4aSRandall Stewart } 928de0e935bSRandall Stewart /* Get the nonce tags */ 929830d754dSRandall Stewart asoc->my_vtag_nonce = sctp_select_a_tag(m, stcb->sctp_ep->sctp_lport, stcb->rport, 0); 930830d754dSRandall Stewart asoc->peer_vtag_nonce = sctp_select_a_tag(m, stcb->sctp_ep->sctp_lport, stcb->rport, 0); 93142551e99SRandall Stewart asoc->vrf_id = vrf_id; 932de0e935bSRandall Stewart 93318e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS 93418e198d3SRandall Stewart asoc->tsn_in_at = 0; 93518e198d3SRandall Stewart asoc->tsn_out_at = 0; 93618e198d3SRandall Stewart asoc->tsn_in_wrapped = 0; 93718e198d3SRandall Stewart asoc->tsn_out_wrapped = 0; 93818e198d3SRandall Stewart asoc->cumack_log_at = 0; 939b201f536SRandall Stewart asoc->cumack_log_atsnt = 0; 94018e198d3SRandall Stewart #endif 94118e198d3SRandall Stewart #ifdef SCTP_FS_SPEC_LOG 94218e198d3SRandall Stewart asoc->fs_index = 0; 94318e198d3SRandall Stewart #endif 944f8829a4aSRandall Stewart asoc->refcnt = 0; 945f8829a4aSRandall Stewart asoc->assoc_up_sent = 0; 946f8829a4aSRandall Stewart asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number = asoc->sending_seq = 947f8829a4aSRandall Stewart sctp_select_initial_TSN(&m->sctp_ep); 948c54a18d2SRandall Stewart asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1; 949f8829a4aSRandall Stewart /* we are optimisitic here */ 950f8829a4aSRandall Stewart asoc->peer_supports_pktdrop = 1; 951830d754dSRandall Stewart asoc->peer_supports_nat = 0; 952f8829a4aSRandall Stewart asoc->sent_queue_retran_cnt = 0; 953f8829a4aSRandall Stewart 954f8829a4aSRandall Stewart /* for CMT */ 9558933fa13SRandall Stewart asoc->last_net_cmt_send_started = NULL; 956f8829a4aSRandall Stewart 957f8829a4aSRandall Stewart /* This will need to be adjusted */ 958f8829a4aSRandall Stewart asoc->last_acked_seq = asoc->init_seq_number - 1; 959f8829a4aSRandall Stewart asoc->advanced_peer_ack_point = asoc->last_acked_seq; 960f8829a4aSRandall Stewart asoc->asconf_seq_in = asoc->last_acked_seq; 961f8829a4aSRandall Stewart 962f8829a4aSRandall Stewart /* here we are different, we hold the next one we expect */ 963f8829a4aSRandall Stewart asoc->str_reset_seq_in = asoc->last_acked_seq + 1; 964f8829a4aSRandall Stewart 965f8829a4aSRandall Stewart asoc->initial_init_rto_max = m->sctp_ep.initial_init_rto_max; 966f8829a4aSRandall Stewart asoc->initial_rto = m->sctp_ep.initial_rto; 967f8829a4aSRandall Stewart 968f8829a4aSRandall Stewart asoc->max_init_times = m->sctp_ep.max_init_times; 969f8829a4aSRandall Stewart asoc->max_send_times = m->sctp_ep.max_send_times; 970f8829a4aSRandall Stewart asoc->def_net_failure = m->sctp_ep.def_net_failure; 971ca85e948SMichael Tuexen asoc->def_net_pf_threshold = m->sctp_ep.def_net_pf_threshold; 972f8829a4aSRandall Stewart asoc->free_chunk_cnt = 0; 973f8829a4aSRandall Stewart 974f8829a4aSRandall Stewart asoc->iam_blocking = 0; 975f8829a4aSRandall Stewart asoc->context = m->sctp_context; 976c4e848b7SRandall Stewart asoc->local_strreset_support = m->local_strreset_support; 977f8829a4aSRandall Stewart asoc->def_send = m->def_send; 978f8829a4aSRandall Stewart asoc->delayed_ack = TICKS_TO_MSEC(m->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]); 97942551e99SRandall Stewart asoc->sack_freq = m->sctp_ep.sctp_sack_freq; 980f8829a4aSRandall Stewart asoc->pr_sctp_cnt = 0; 981f8829a4aSRandall Stewart asoc->total_output_queue_size = 0; 982f8829a4aSRandall Stewart 983f8829a4aSRandall Stewart if (m->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 984f8829a4aSRandall Stewart struct in6pcb *inp6; 985f8829a4aSRandall Stewart 986f8829a4aSRandall Stewart /* Its a V6 socket */ 987f8829a4aSRandall Stewart inp6 = (struct in6pcb *)m; 988f8829a4aSRandall Stewart asoc->ipv6_addr_legal = 1; 989f8829a4aSRandall Stewart /* Now look at the binding flag to see if V4 will be legal */ 99044b7479bSRandall Stewart if (SCTP_IPV6_V6ONLY(inp6) == 0) { 991f8829a4aSRandall Stewart asoc->ipv4_addr_legal = 1; 992f8829a4aSRandall Stewart } else { 993f8829a4aSRandall Stewart /* V4 addresses are NOT legal on the association */ 994f8829a4aSRandall Stewart asoc->ipv4_addr_legal = 0; 995f8829a4aSRandall Stewart } 996f8829a4aSRandall Stewart } else { 997f8829a4aSRandall Stewart /* Its a V4 socket, no - V6 */ 998f8829a4aSRandall Stewart asoc->ipv4_addr_legal = 1; 999f8829a4aSRandall Stewart asoc->ipv6_addr_legal = 0; 1000f8829a4aSRandall Stewart } 1001f8829a4aSRandall Stewart 100262c1ff9cSRandall Stewart asoc->my_rwnd = max(SCTP_SB_LIMIT_RCV(m->sctp_socket), SCTP_MINIMAL_RWND); 100362c1ff9cSRandall Stewart asoc->peers_rwnd = SCTP_SB_LIMIT_RCV(m->sctp_socket); 1004f8829a4aSRandall Stewart 1005f8829a4aSRandall Stewart asoc->smallest_mtu = m->sctp_frag_point; 1006f8829a4aSRandall Stewart asoc->minrto = m->sctp_ep.sctp_minrto; 1007f8829a4aSRandall Stewart asoc->maxrto = m->sctp_ep.sctp_maxrto; 1008f8829a4aSRandall Stewart 1009f8829a4aSRandall Stewart asoc->locked_on_sending = NULL; 1010f8829a4aSRandall Stewart asoc->stream_locked_on = 0; 1011f8829a4aSRandall Stewart asoc->ecn_echo_cnt_onq = 0; 1012f8829a4aSRandall Stewart asoc->stream_locked = 0; 1013f8829a4aSRandall Stewart 101442551e99SRandall Stewart asoc->send_sack = 1; 101542551e99SRandall Stewart 101642551e99SRandall Stewart LIST_INIT(&asoc->sctp_restricted_addrs); 101742551e99SRandall Stewart 1018f8829a4aSRandall Stewart TAILQ_INIT(&asoc->nets); 1019f8829a4aSRandall Stewart TAILQ_INIT(&asoc->pending_reply_queue); 10202afb3e84SRandall Stewart TAILQ_INIT(&asoc->asconf_ack_sent); 1021f8829a4aSRandall Stewart /* Setup to fill the hb random cache at first HB */ 1022f8829a4aSRandall Stewart asoc->hb_random_idx = 4; 1023f8829a4aSRandall Stewart 1024f8829a4aSRandall Stewart asoc->sctp_autoclose_ticks = m->sctp_ep.auto_close_time; 1025f8829a4aSRandall Stewart 10260e9a9c10SMichael Tuexen stcb->asoc.congestion_control_module = m->sctp_ep.sctp_default_cc_module; 10270e9a9c10SMichael Tuexen stcb->asoc.cc_functions = sctp_cc_functions[m->sctp_ep.sctp_default_cc_module]; 1028b54d3a6cSRandall Stewart 1029f7a77f6fSMichael Tuexen stcb->asoc.stream_scheduling_module = m->sctp_ep.sctp_default_ss_module; 1030f7a77f6fSMichael Tuexen stcb->asoc.ss_functions = sctp_ss_functions[m->sctp_ep.sctp_default_ss_module]; 1031f7a77f6fSMichael Tuexen 1032b54d3a6cSRandall Stewart /* 1033f8829a4aSRandall Stewart * Now the stream parameters, here we allocate space for all streams 1034f8829a4aSRandall Stewart * that we request by default. 1035f8829a4aSRandall Stewart */ 1036ea44232bSRandall Stewart asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams = 1037f8829a4aSRandall Stewart m->sctp_ep.pre_open_stream_count; 1038f8829a4aSRandall Stewart SCTP_MALLOC(asoc->strmout, struct sctp_stream_out *, 1039f8829a4aSRandall Stewart asoc->streamoutcnt * sizeof(struct sctp_stream_out), 1040207304d4SRandall Stewart SCTP_M_STRMO); 1041f8829a4aSRandall Stewart if (asoc->strmout == NULL) { 1042f8829a4aSRandall Stewart /* big trouble no memory */ 1043c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM); 1044f8829a4aSRandall Stewart return (ENOMEM); 1045f8829a4aSRandall Stewart } 1046f8829a4aSRandall Stewart for (i = 0; i < asoc->streamoutcnt; i++) { 1047f8829a4aSRandall Stewart /* 1048f8829a4aSRandall Stewart * inbound side must be set to 0xffff, also NOTE when we get 1049f8829a4aSRandall Stewart * the INIT-ACK back (for INIT sender) we MUST reduce the 1050f8829a4aSRandall Stewart * count (streamoutcnt) but first check if we sent to any of 1051f8829a4aSRandall Stewart * the upper streams that were dropped (if some were). Those 1052f8829a4aSRandall Stewart * that were dropped must be notified to the upper layer as 1053f8829a4aSRandall Stewart * failed to send. 1054f8829a4aSRandall Stewart */ 1055f3b05218SMichael Tuexen asoc->strmout[i].next_sequence_send = 0x0; 1056f8829a4aSRandall Stewart TAILQ_INIT(&asoc->strmout[i].outqueue); 1057f8829a4aSRandall Stewart asoc->strmout[i].stream_no = i; 1058f8829a4aSRandall Stewart asoc->strmout[i].last_msg_incomplete = 0; 1059252f7f93SMichael Tuexen asoc->ss_functions.sctp_ss_init_stream(&asoc->strmout[i], NULL); 1060f8829a4aSRandall Stewart } 1061f7a77f6fSMichael Tuexen asoc->ss_functions.sctp_ss_init(stcb, asoc, 0); 1062f7a77f6fSMichael Tuexen 1063f8829a4aSRandall Stewart /* Now the mapping array */ 1064f8829a4aSRandall Stewart asoc->mapping_array_size = SCTP_INITIAL_MAPPING_ARRAY; 1065f8829a4aSRandall Stewart SCTP_MALLOC(asoc->mapping_array, uint8_t *, asoc->mapping_array_size, 1066207304d4SRandall Stewart SCTP_M_MAP); 1067f8829a4aSRandall Stewart if (asoc->mapping_array == NULL) { 1068207304d4SRandall Stewart SCTP_FREE(asoc->strmout, SCTP_M_STRMO); 1069c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM); 1070f8829a4aSRandall Stewart return (ENOMEM); 1071f8829a4aSRandall Stewart } 1072f8829a4aSRandall Stewart memset(asoc->mapping_array, 0, asoc->mapping_array_size); 1073b5c16493SMichael Tuexen SCTP_MALLOC(asoc->nr_mapping_array, uint8_t *, asoc->mapping_array_size, 1074830d754dSRandall Stewart SCTP_M_MAP); 1075bf1be571SRandall Stewart if (asoc->nr_mapping_array == NULL) { 1076bf1be571SRandall Stewart SCTP_FREE(asoc->strmout, SCTP_M_STRMO); 1077bf1be571SRandall Stewart SCTP_FREE(asoc->mapping_array, SCTP_M_MAP); 1078bf1be571SRandall Stewart SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM); 1079bf1be571SRandall Stewart return (ENOMEM); 1080bf1be571SRandall Stewart } 1081b5c16493SMichael Tuexen memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size); 1082830d754dSRandall Stewart 1083f8829a4aSRandall Stewart /* Now the init of the other outqueues */ 1084f8829a4aSRandall Stewart TAILQ_INIT(&asoc->free_chunks); 1085f8829a4aSRandall Stewart TAILQ_INIT(&asoc->control_send_queue); 1086c54a18d2SRandall Stewart TAILQ_INIT(&asoc->asconf_send_queue); 1087f8829a4aSRandall Stewart TAILQ_INIT(&asoc->send_queue); 1088f8829a4aSRandall Stewart TAILQ_INIT(&asoc->sent_queue); 1089f8829a4aSRandall Stewart TAILQ_INIT(&asoc->reasmqueue); 1090f8829a4aSRandall Stewart TAILQ_INIT(&asoc->resetHead); 1091f8829a4aSRandall Stewart asoc->max_inbound_streams = m->sctp_ep.max_open_streams_intome; 1092f8829a4aSRandall Stewart TAILQ_INIT(&asoc->asconf_queue); 1093f8829a4aSRandall Stewart /* authentication fields */ 1094f8829a4aSRandall Stewart asoc->authinfo.random = NULL; 1095830d754dSRandall Stewart asoc->authinfo.active_keyid = 0; 1096f8829a4aSRandall Stewart asoc->authinfo.assoc_key = NULL; 1097f8829a4aSRandall Stewart asoc->authinfo.assoc_keyid = 0; 1098f8829a4aSRandall Stewart asoc->authinfo.recv_key = NULL; 1099f8829a4aSRandall Stewart asoc->authinfo.recv_keyid = 0; 1100f8829a4aSRandall Stewart LIST_INIT(&asoc->shared_keys); 1101f42a358aSRandall Stewart asoc->marked_retrans = 0; 1102c9c58059SMichael Tuexen asoc->port = m->sctp_ep.port; 1103f42a358aSRandall Stewart asoc->timoinit = 0; 1104f42a358aSRandall Stewart asoc->timodata = 0; 1105f42a358aSRandall Stewart asoc->timosack = 0; 1106f42a358aSRandall Stewart asoc->timoshutdown = 0; 1107f42a358aSRandall Stewart asoc->timoheartbeat = 0; 1108f42a358aSRandall Stewart asoc->timocookie = 0; 1109f42a358aSRandall Stewart asoc->timoshutdownack = 0; 11106e55db54SRandall Stewart (void)SCTP_GETTIME_TIMEVAL(&asoc->start_time); 11116e55db54SRandall Stewart asoc->discontinuity_time = asoc->start_time; 1112eacc51c5SRandall Stewart /* 1113eacc51c5SRandall Stewart * sa_ignore MEMLEAK {memory is put in the assoc mapping array and 111477acdc25SRandall Stewart * freed later when the association is freed. 1115eacc51c5SRandall Stewart */ 1116f8829a4aSRandall Stewart return (0); 1117f8829a4aSRandall Stewart } 1118f8829a4aSRandall Stewart 11190e13104dSRandall Stewart void 11200e13104dSRandall Stewart sctp_print_mapping_array(struct sctp_association *asoc) 11210e13104dSRandall Stewart { 1122aed5947cSMichael Tuexen unsigned int i, limit; 11230e13104dSRandall Stewart 1124cd3fd531SMichael Tuexen SCTP_PRINTF("Mapping array size: %d, baseTSN: %8.8x, cumAck: %8.8x, highestTSN: (%8.8x, %8.8x).\n", 11250e13104dSRandall Stewart asoc->mapping_array_size, 11260e13104dSRandall Stewart asoc->mapping_array_base_tsn, 11270e13104dSRandall Stewart asoc->cumulative_tsn, 1128aed5947cSMichael Tuexen asoc->highest_tsn_inside_map, 1129aed5947cSMichael Tuexen asoc->highest_tsn_inside_nr_map); 1130aed5947cSMichael Tuexen for (limit = asoc->mapping_array_size; limit > 1; limit--) { 113160990c0cSMichael Tuexen if (asoc->mapping_array[limit - 1] != 0) { 113277acdc25SRandall Stewart break; 113377acdc25SRandall Stewart } 113477acdc25SRandall Stewart } 1135cd3fd531SMichael Tuexen SCTP_PRINTF("Renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit); 113677acdc25SRandall Stewart for (i = 0; i < limit; i++) { 1137cd3fd531SMichael Tuexen SCTP_PRINTF("%2.2x%c", asoc->mapping_array[i], ((i + 1) % 16) ? ' ' : '\n'); 113877acdc25SRandall Stewart } 1139aed5947cSMichael Tuexen if (limit % 16) 1140cd3fd531SMichael Tuexen SCTP_PRINTF("\n"); 1141aed5947cSMichael Tuexen for (limit = asoc->mapping_array_size; limit > 1; limit--) { 1142aed5947cSMichael Tuexen if (asoc->nr_mapping_array[limit - 1]) { 114377acdc25SRandall Stewart break; 114477acdc25SRandall Stewart } 114577acdc25SRandall Stewart } 1146cd3fd531SMichael Tuexen SCTP_PRINTF("Non renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit); 114777acdc25SRandall Stewart for (i = 0; i < limit; i++) { 1148cd3fd531SMichael Tuexen SCTP_PRINTF("%2.2x%c", asoc->nr_mapping_array[i], ((i + 1) % 16) ? ' ' : '\n'); 11490e13104dSRandall Stewart } 1150aed5947cSMichael Tuexen if (limit % 16) 1151cd3fd531SMichael Tuexen SCTP_PRINTF("\n"); 11520e13104dSRandall Stewart } 11530e13104dSRandall Stewart 1154f8829a4aSRandall Stewart int 11550696e120SRandall Stewart sctp_expand_mapping_array(struct sctp_association *asoc, uint32_t needed) 1156f8829a4aSRandall Stewart { 1157f8829a4aSRandall Stewart /* mapping array needs to grow */ 1158b5c16493SMichael Tuexen uint8_t *new_array1, *new_array2; 11590696e120SRandall Stewart uint32_t new_size; 1160f8829a4aSRandall Stewart 11610696e120SRandall Stewart new_size = asoc->mapping_array_size + ((needed + 7) / 8 + SCTP_MAPPING_ARRAY_INCR); 1162b5c16493SMichael Tuexen SCTP_MALLOC(new_array1, uint8_t *, new_size, SCTP_M_MAP); 1163b5c16493SMichael Tuexen SCTP_MALLOC(new_array2, uint8_t *, new_size, SCTP_M_MAP); 1164b5c16493SMichael Tuexen if ((new_array1 == NULL) || (new_array2 == NULL)) { 1165f8829a4aSRandall Stewart /* can't get more, forget it */ 1166b5c16493SMichael Tuexen SCTP_PRINTF("No memory for expansion of SCTP mapping array %d\n", new_size); 1167b5c16493SMichael Tuexen if (new_array1) { 1168b5c16493SMichael Tuexen SCTP_FREE(new_array1, SCTP_M_MAP); 1169b5c16493SMichael Tuexen } 1170b5c16493SMichael Tuexen if (new_array2) { 1171b5c16493SMichael Tuexen SCTP_FREE(new_array2, SCTP_M_MAP); 1172b5c16493SMichael Tuexen } 1173f8829a4aSRandall Stewart return (-1); 1174f8829a4aSRandall Stewart } 1175b5c16493SMichael Tuexen memset(new_array1, 0, new_size); 1176b5c16493SMichael Tuexen memset(new_array2, 0, new_size); 1177b5c16493SMichael Tuexen memcpy(new_array1, asoc->mapping_array, asoc->mapping_array_size); 1178b5c16493SMichael Tuexen memcpy(new_array2, asoc->nr_mapping_array, asoc->mapping_array_size); 1179207304d4SRandall Stewart SCTP_FREE(asoc->mapping_array, SCTP_M_MAP); 1180830d754dSRandall Stewart SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP); 1181b5c16493SMichael Tuexen asoc->mapping_array = new_array1; 1182b5c16493SMichael Tuexen asoc->nr_mapping_array = new_array2; 1183b5c16493SMichael Tuexen asoc->mapping_array_size = new_size; 1184830d754dSRandall Stewart return (0); 1185830d754dSRandall Stewart } 1186830d754dSRandall Stewart 11878933fa13SRandall Stewart 118842551e99SRandall Stewart static void 118942551e99SRandall Stewart sctp_iterator_work(struct sctp_iterator *it) 119042551e99SRandall Stewart { 119142551e99SRandall Stewart int iteration_count = 0; 119242551e99SRandall Stewart int inp_skip = 0; 1193ec4c19fcSRandall Stewart int first_in = 1; 1194ec4c19fcSRandall Stewart struct sctp_inpcb *tinp; 119542551e99SRandall Stewart 1196ec4c19fcSRandall Stewart SCTP_INP_INFO_RLOCK(); 119742551e99SRandall Stewart SCTP_ITERATOR_LOCK(); 1198ad81507eSRandall Stewart if (it->inp) { 1199ec4c19fcSRandall Stewart SCTP_INP_RLOCK(it->inp); 120042551e99SRandall Stewart SCTP_INP_DECR_REF(it->inp); 1201ad81507eSRandall Stewart } 120242551e99SRandall Stewart if (it->inp == NULL) { 120342551e99SRandall Stewart /* iterator is complete */ 120442551e99SRandall Stewart done_with_iterator: 120542551e99SRandall Stewart SCTP_ITERATOR_UNLOCK(); 1206ec4c19fcSRandall Stewart SCTP_INP_INFO_RUNLOCK(); 120742551e99SRandall Stewart if (it->function_atend != NULL) { 120842551e99SRandall Stewart (*it->function_atend) (it->pointer, it->val); 120942551e99SRandall Stewart } 1210207304d4SRandall Stewart SCTP_FREE(it, SCTP_M_ITER); 121142551e99SRandall Stewart return; 121242551e99SRandall Stewart } 121342551e99SRandall Stewart select_a_new_ep: 1214ec4c19fcSRandall Stewart if (first_in) { 1215ec4c19fcSRandall Stewart first_in = 0; 1216ec4c19fcSRandall Stewart } else { 1217f7517433SRandall Stewart SCTP_INP_RLOCK(it->inp); 1218ec4c19fcSRandall Stewart } 121942551e99SRandall Stewart while (((it->pcb_flags) && 122042551e99SRandall Stewart ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) || 122142551e99SRandall Stewart ((it->pcb_features) && 122242551e99SRandall Stewart ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) { 122342551e99SRandall Stewart /* endpoint flags or features don't match, so keep looking */ 122442551e99SRandall Stewart if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) { 1225f7517433SRandall Stewart SCTP_INP_RUNLOCK(it->inp); 122642551e99SRandall Stewart goto done_with_iterator; 122742551e99SRandall Stewart } 1228ec4c19fcSRandall Stewart tinp = it->inp; 122942551e99SRandall Stewart it->inp = LIST_NEXT(it->inp, sctp_list); 1230ec4c19fcSRandall Stewart SCTP_INP_RUNLOCK(tinp); 123142551e99SRandall Stewart if (it->inp == NULL) { 123242551e99SRandall Stewart goto done_with_iterator; 123342551e99SRandall Stewart } 123442551e99SRandall Stewart SCTP_INP_RLOCK(it->inp); 1235f7517433SRandall Stewart } 123642551e99SRandall Stewart /* now go through each assoc which is in the desired state */ 123742551e99SRandall Stewart if (it->done_current_ep == 0) { 123842551e99SRandall Stewart if (it->function_inp != NULL) 123942551e99SRandall Stewart inp_skip = (*it->function_inp) (it->inp, it->pointer, it->val); 124042551e99SRandall Stewart it->done_current_ep = 1; 124142551e99SRandall Stewart } 124242551e99SRandall Stewart if (it->stcb == NULL) { 124342551e99SRandall Stewart /* run the per instance function */ 124442551e99SRandall Stewart it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list); 124542551e99SRandall Stewart } 124642551e99SRandall Stewart if ((inp_skip) || it->stcb == NULL) { 124742551e99SRandall Stewart if (it->function_inp_end != NULL) { 124842551e99SRandall Stewart inp_skip = (*it->function_inp_end) (it->inp, 124942551e99SRandall Stewart it->pointer, 125042551e99SRandall Stewart it->val); 125142551e99SRandall Stewart } 125242551e99SRandall Stewart SCTP_INP_RUNLOCK(it->inp); 125342551e99SRandall Stewart goto no_stcb; 125442551e99SRandall Stewart } 125542551e99SRandall Stewart while (it->stcb) { 125642551e99SRandall Stewart SCTP_TCB_LOCK(it->stcb); 125742551e99SRandall Stewart if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) { 125842551e99SRandall Stewart /* not in the right state... keep looking */ 125942551e99SRandall Stewart SCTP_TCB_UNLOCK(it->stcb); 126042551e99SRandall Stewart goto next_assoc; 126142551e99SRandall Stewart } 126242551e99SRandall Stewart /* see if we have limited out the iterator loop */ 126342551e99SRandall Stewart iteration_count++; 126442551e99SRandall Stewart if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) { 126542551e99SRandall Stewart /* Pause to let others grab the lock */ 126642551e99SRandall Stewart atomic_add_int(&it->stcb->asoc.refcnt, 1); 126742551e99SRandall Stewart SCTP_TCB_UNLOCK(it->stcb); 1268c4739e2fSRandall Stewart SCTP_INP_INCR_REF(it->inp); 126942551e99SRandall Stewart SCTP_INP_RUNLOCK(it->inp); 127042551e99SRandall Stewart SCTP_ITERATOR_UNLOCK(); 1271ec4c19fcSRandall Stewart SCTP_INP_INFO_RUNLOCK(); 1272ec4c19fcSRandall Stewart SCTP_INP_INFO_RLOCK(); 127342551e99SRandall Stewart SCTP_ITERATOR_LOCK(); 1274f7517433SRandall Stewart if (sctp_it_ctl.iterator_flags) { 1275f7517433SRandall Stewart /* We won't be staying here */ 1276f7517433SRandall Stewart SCTP_INP_DECR_REF(it->inp); 1277f7517433SRandall Stewart atomic_add_int(&it->stcb->asoc.refcnt, -1); 1278f7517433SRandall Stewart if (sctp_it_ctl.iterator_flags & 1279f7517433SRandall Stewart SCTP_ITERATOR_STOP_CUR_IT) { 1280f7517433SRandall Stewart sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_IT; 1281f7517433SRandall Stewart goto done_with_iterator; 1282f7517433SRandall Stewart } 1283f7517433SRandall Stewart if (sctp_it_ctl.iterator_flags & 1284f7517433SRandall Stewart SCTP_ITERATOR_STOP_CUR_INP) { 1285f7517433SRandall Stewart sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_INP; 1286f7517433SRandall Stewart goto no_stcb; 1287f7517433SRandall Stewart } 1288f7517433SRandall Stewart /* If we reach here huh? */ 1289cd3fd531SMichael Tuexen SCTP_PRINTF("Unknown it ctl flag %x\n", 1290f7517433SRandall Stewart sctp_it_ctl.iterator_flags); 1291f7517433SRandall Stewart sctp_it_ctl.iterator_flags = 0; 1292f7517433SRandall Stewart } 129342551e99SRandall Stewart SCTP_INP_RLOCK(it->inp); 1294c4739e2fSRandall Stewart SCTP_INP_DECR_REF(it->inp); 129542551e99SRandall Stewart SCTP_TCB_LOCK(it->stcb); 129642551e99SRandall Stewart atomic_add_int(&it->stcb->asoc.refcnt, -1); 129742551e99SRandall Stewart iteration_count = 0; 129842551e99SRandall Stewart } 129942551e99SRandall Stewart /* run function on this one */ 130042551e99SRandall Stewart (*it->function_assoc) (it->inp, it->stcb, it->pointer, it->val); 130142551e99SRandall Stewart 130242551e99SRandall Stewart /* 130342551e99SRandall Stewart * we lie here, it really needs to have its own type but 130442551e99SRandall Stewart * first I must verify that this won't effect things :-0 130542551e99SRandall Stewart */ 130642551e99SRandall Stewart if (it->no_chunk_output == 0) 1307ceaad40aSRandall Stewart sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED); 130842551e99SRandall Stewart 130942551e99SRandall Stewart SCTP_TCB_UNLOCK(it->stcb); 131042551e99SRandall Stewart next_assoc: 131142551e99SRandall Stewart it->stcb = LIST_NEXT(it->stcb, sctp_tcblist); 131242551e99SRandall Stewart if (it->stcb == NULL) { 131342551e99SRandall Stewart /* Run last function */ 131442551e99SRandall Stewart if (it->function_inp_end != NULL) { 131542551e99SRandall Stewart inp_skip = (*it->function_inp_end) (it->inp, 131642551e99SRandall Stewart it->pointer, 131742551e99SRandall Stewart it->val); 131842551e99SRandall Stewart } 131942551e99SRandall Stewart } 132042551e99SRandall Stewart } 132142551e99SRandall Stewart SCTP_INP_RUNLOCK(it->inp); 132242551e99SRandall Stewart no_stcb: 132342551e99SRandall Stewart /* done with all assocs on this endpoint, move on to next endpoint */ 132442551e99SRandall Stewart it->done_current_ep = 0; 132542551e99SRandall Stewart if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) { 132642551e99SRandall Stewart it->inp = NULL; 132742551e99SRandall Stewart } else { 132842551e99SRandall Stewart it->inp = LIST_NEXT(it->inp, sctp_list); 132942551e99SRandall Stewart } 133042551e99SRandall Stewart if (it->inp == NULL) { 133142551e99SRandall Stewart goto done_with_iterator; 133242551e99SRandall Stewart } 133342551e99SRandall Stewart goto select_a_new_ep; 133442551e99SRandall Stewart } 133542551e99SRandall Stewart 133642551e99SRandall Stewart void 133742551e99SRandall Stewart sctp_iterator_worker(void) 133842551e99SRandall Stewart { 13394a9ef3f8SMichael Tuexen struct sctp_iterator *it, *nit; 134042551e99SRandall Stewart 134142551e99SRandall Stewart /* This function is called with the WQ lock in place */ 134242551e99SRandall Stewart 1343f7517433SRandall Stewart sctp_it_ctl.iterator_running = 1; 13444a9ef3f8SMichael Tuexen TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) { 13454a9ef3f8SMichael Tuexen sctp_it_ctl.cur_it = it; 134642551e99SRandall Stewart /* now lets work on this one */ 1347f7517433SRandall Stewart TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr); 134842551e99SRandall Stewart SCTP_IPI_ITERATOR_WQ_UNLOCK(); 1349f7517433SRandall Stewart CURVNET_SET(it->vn); 135042551e99SRandall Stewart sctp_iterator_work(it); 1351f79aab18SRandall Stewart sctp_it_ctl.cur_it = NULL; 1352f7517433SRandall Stewart CURVNET_RESTORE(); 135342551e99SRandall Stewart SCTP_IPI_ITERATOR_WQ_LOCK(); 13543c503c28SRandall Stewart /* sa_ignore FREED_MEMORY */ 135542551e99SRandall Stewart } 1356f7517433SRandall Stewart sctp_it_ctl.iterator_running = 0; 135742551e99SRandall Stewart return; 135842551e99SRandall Stewart } 135942551e99SRandall Stewart 1360f8829a4aSRandall Stewart 1361f8829a4aSRandall Stewart static void 1362f8829a4aSRandall Stewart sctp_handle_addr_wq(void) 1363f8829a4aSRandall Stewart { 1364f8829a4aSRandall Stewart /* deal with the ADDR wq from the rtsock calls */ 13654a9ef3f8SMichael Tuexen struct sctp_laddr *wi, *nwi; 136642551e99SRandall Stewart struct sctp_asconf_iterator *asc; 1367f8829a4aSRandall Stewart 136842551e99SRandall Stewart SCTP_MALLOC(asc, struct sctp_asconf_iterator *, 1369207304d4SRandall Stewart sizeof(struct sctp_asconf_iterator), SCTP_M_ASC_IT); 137042551e99SRandall Stewart if (asc == NULL) { 137142551e99SRandall Stewart /* Try later, no memory */ 1372f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ, 1373f8829a4aSRandall Stewart (struct sctp_inpcb *)NULL, 1374f8829a4aSRandall Stewart (struct sctp_tcb *)NULL, 1375f8829a4aSRandall Stewart (struct sctp_nets *)NULL); 137642551e99SRandall Stewart return; 1377f8829a4aSRandall Stewart } 137842551e99SRandall Stewart LIST_INIT(&asc->list_of_work); 137942551e99SRandall Stewart asc->cnt = 0; 1380f7517433SRandall Stewart 1381f7517433SRandall Stewart SCTP_WQ_ADDR_LOCK(); 13824a9ef3f8SMichael Tuexen LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) { 138342551e99SRandall Stewart LIST_REMOVE(wi, sctp_nxt_addr); 138442551e99SRandall Stewart LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr); 138542551e99SRandall Stewart asc->cnt++; 1386f8829a4aSRandall Stewart } 1387f7517433SRandall Stewart SCTP_WQ_ADDR_UNLOCK(); 1388f7517433SRandall Stewart 138942551e99SRandall Stewart if (asc->cnt == 0) { 1390207304d4SRandall Stewart SCTP_FREE(asc, SCTP_M_ASC_IT); 139142551e99SRandall Stewart } else { 13921b649582SRandall Stewart (void)sctp_initiate_iterator(sctp_asconf_iterator_ep, 13931b649582SRandall Stewart sctp_asconf_iterator_stcb, 139442551e99SRandall Stewart NULL, /* No ep end for boundall */ 139542551e99SRandall Stewart SCTP_PCB_FLAGS_BOUNDALL, 139642551e99SRandall Stewart SCTP_PCB_ANY_FEATURES, 13971b649582SRandall Stewart SCTP_ASOC_ANY_STATE, 13981b649582SRandall Stewart (void *)asc, 0, 13991b649582SRandall Stewart sctp_asconf_iterator_end, NULL, 0); 140042551e99SRandall Stewart } 1401f8829a4aSRandall Stewart } 1402f8829a4aSRandall Stewart 1403f8829a4aSRandall Stewart void 1404f8829a4aSRandall Stewart sctp_timeout_handler(void *t) 1405f8829a4aSRandall Stewart { 1406f8829a4aSRandall Stewart struct sctp_inpcb *inp; 1407f8829a4aSRandall Stewart struct sctp_tcb *stcb; 1408f8829a4aSRandall Stewart struct sctp_nets *net; 1409f8829a4aSRandall Stewart struct sctp_timer *tmr; 1410ceaad40aSRandall Stewart 1411ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1412ceaad40aSRandall Stewart struct socket *so; 1413ceaad40aSRandall Stewart 1414ceaad40aSRandall Stewart #endif 1415d61374e1SRandall Stewart int did_output, type; 1416f8829a4aSRandall Stewart 1417f8829a4aSRandall Stewart tmr = (struct sctp_timer *)t; 1418f8829a4aSRandall Stewart inp = (struct sctp_inpcb *)tmr->ep; 1419f8829a4aSRandall Stewart stcb = (struct sctp_tcb *)tmr->tcb; 1420f8829a4aSRandall Stewart net = (struct sctp_nets *)tmr->net; 14218518270eSMichael Tuexen CURVNET_SET((struct vnet *)tmr->vnet); 1422f8829a4aSRandall Stewart did_output = 1; 1423f8829a4aSRandall Stewart 1424f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED 1425f8829a4aSRandall Stewart sctp_audit_log(0xF0, (uint8_t) tmr->type); 1426f8829a4aSRandall Stewart sctp_auditing(3, inp, stcb, net); 1427f8829a4aSRandall Stewart #endif 1428f8829a4aSRandall Stewart 1429f8829a4aSRandall Stewart /* sanity checks... */ 1430f8829a4aSRandall Stewart if (tmr->self != (void *)tmr) { 1431f8829a4aSRandall Stewart /* 1432ad81507eSRandall Stewart * SCTP_PRINTF("Stale SCTP timer fired (%p), ignoring...\n", 1433dd294dceSMichael Tuexen * (void *)tmr); 1434f8829a4aSRandall Stewart */ 14358518270eSMichael Tuexen CURVNET_RESTORE(); 1436f8829a4aSRandall Stewart return; 1437f8829a4aSRandall Stewart } 1438a5d547adSRandall Stewart tmr->stopped_from = 0xa001; 1439f8829a4aSRandall Stewart if (!SCTP_IS_TIMER_TYPE_VALID(tmr->type)) { 1440f8829a4aSRandall Stewart /* 1441ad81507eSRandall Stewart * SCTP_PRINTF("SCTP timer fired with invalid type: 0x%x\n", 1442f8829a4aSRandall Stewart * tmr->type); 1443f8829a4aSRandall Stewart */ 14448518270eSMichael Tuexen CURVNET_RESTORE(); 1445f8829a4aSRandall Stewart return; 1446f8829a4aSRandall Stewart } 1447a5d547adSRandall Stewart tmr->stopped_from = 0xa002; 1448f8829a4aSRandall Stewart if ((tmr->type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL)) { 14498518270eSMichael Tuexen CURVNET_RESTORE(); 1450f8829a4aSRandall Stewart return; 1451f8829a4aSRandall Stewart } 1452f8829a4aSRandall Stewart /* if this is an iterator timeout, get the struct and clear inp */ 1453a5d547adSRandall Stewart tmr->stopped_from = 0xa003; 1454d61374e1SRandall Stewart type = tmr->type; 1455f8829a4aSRandall Stewart if (inp) { 1456f8829a4aSRandall Stewart SCTP_INP_INCR_REF(inp); 1457aa1808b7SMichael Tuexen if ((inp->sctp_socket == NULL) && 1458f8829a4aSRandall Stewart ((tmr->type != SCTP_TIMER_TYPE_INPKILL) && 1459810ec536SMichael Tuexen (tmr->type != SCTP_TIMER_TYPE_INIT) && 1460a1e13272SRandall Stewart (tmr->type != SCTP_TIMER_TYPE_SEND) && 1461a1e13272SRandall Stewart (tmr->type != SCTP_TIMER_TYPE_RECV) && 1462a1e13272SRandall Stewart (tmr->type != SCTP_TIMER_TYPE_HEARTBEAT) && 1463f8829a4aSRandall Stewart (tmr->type != SCTP_TIMER_TYPE_SHUTDOWN) && 1464f8829a4aSRandall Stewart (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNACK) && 1465f8829a4aSRandall Stewart (tmr->type != SCTP_TIMER_TYPE_SHUTDOWNGUARD) && 1466f8829a4aSRandall Stewart (tmr->type != SCTP_TIMER_TYPE_ASOCKILL)) 1467f8829a4aSRandall Stewart ) { 1468f8829a4aSRandall Stewart SCTP_INP_DECR_REF(inp); 14698518270eSMichael Tuexen CURVNET_RESTORE(); 1470f8829a4aSRandall Stewart return; 1471f8829a4aSRandall Stewart } 1472f8829a4aSRandall Stewart } 1473a5d547adSRandall Stewart tmr->stopped_from = 0xa004; 1474f8829a4aSRandall Stewart if (stcb) { 1475c105859eSRandall Stewart atomic_add_int(&stcb->asoc.refcnt, 1); 1476f8829a4aSRandall Stewart if (stcb->asoc.state == 0) { 1477c105859eSRandall Stewart atomic_add_int(&stcb->asoc.refcnt, -1); 1478f8829a4aSRandall Stewart if (inp) { 1479f8829a4aSRandall Stewart SCTP_INP_DECR_REF(inp); 1480f8829a4aSRandall Stewart } 14818518270eSMichael Tuexen CURVNET_RESTORE(); 1482f8829a4aSRandall Stewart return; 1483f8829a4aSRandall Stewart } 1484f8829a4aSRandall Stewart } 1485a5d547adSRandall Stewart tmr->stopped_from = 0xa005; 1486ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_TIMER1, "Timer type %d goes off\n", tmr->type); 1487139bc87fSRandall Stewart if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) { 1488f8829a4aSRandall Stewart if (inp) { 1489f8829a4aSRandall Stewart SCTP_INP_DECR_REF(inp); 1490f8829a4aSRandall Stewart } 1491207304d4SRandall Stewart if (stcb) { 1492207304d4SRandall Stewart atomic_add_int(&stcb->asoc.refcnt, -1); 1493207304d4SRandall Stewart } 14948518270eSMichael Tuexen CURVNET_RESTORE(); 1495f8829a4aSRandall Stewart return; 1496f8829a4aSRandall Stewart } 1497a5d547adSRandall Stewart tmr->stopped_from = 0xa006; 1498a5d547adSRandall Stewart 1499f8829a4aSRandall Stewart if (stcb) { 1500f8829a4aSRandall Stewart SCTP_TCB_LOCK(stcb); 150150cec919SRandall Stewart atomic_add_int(&stcb->asoc.refcnt, -1); 1502b54d3a6cSRandall Stewart if ((tmr->type != SCTP_TIMER_TYPE_ASOCKILL) && 1503b54d3a6cSRandall Stewart ((stcb->asoc.state == 0) || 1504b54d3a6cSRandall Stewart (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED))) { 1505b54d3a6cSRandall Stewart SCTP_TCB_UNLOCK(stcb); 1506b54d3a6cSRandall Stewart if (inp) { 1507b54d3a6cSRandall Stewart SCTP_INP_DECR_REF(inp); 1508b54d3a6cSRandall Stewart } 15098518270eSMichael Tuexen CURVNET_RESTORE(); 1510b54d3a6cSRandall Stewart return; 1511b54d3a6cSRandall Stewart } 1512f8829a4aSRandall Stewart } 151344b7479bSRandall Stewart /* record in stopped what t-o occured */ 151444b7479bSRandall Stewart tmr->stopped_from = tmr->type; 151544b7479bSRandall Stewart 1516f8829a4aSRandall Stewart /* mark as being serviced now */ 151744b7479bSRandall Stewart if (SCTP_OS_TIMER_PENDING(&tmr->timer)) { 151844b7479bSRandall Stewart /* 151944b7479bSRandall Stewart * Callout has been rescheduled. 152044b7479bSRandall Stewart */ 152144b7479bSRandall Stewart goto get_out; 152244b7479bSRandall Stewart } 152344b7479bSRandall Stewart if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) { 152444b7479bSRandall Stewart /* 152544b7479bSRandall Stewart * Not active, so no action. 152644b7479bSRandall Stewart */ 152744b7479bSRandall Stewart goto get_out; 152844b7479bSRandall Stewart } 1529139bc87fSRandall Stewart SCTP_OS_TIMER_DEACTIVATE(&tmr->timer); 1530f8829a4aSRandall Stewart 1531f8829a4aSRandall Stewart /* call the handler for the appropriate timer type */ 1532f8829a4aSRandall Stewart switch (tmr->type) { 1533d61a0ae0SRandall Stewart case SCTP_TIMER_TYPE_ZERO_COPY: 1534eacc51c5SRandall Stewart if (inp == NULL) { 1535eacc51c5SRandall Stewart break; 1536eacc51c5SRandall Stewart } 1537d61a0ae0SRandall Stewart if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) { 1538d61a0ae0SRandall Stewart SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket); 1539d61a0ae0SRandall Stewart } 1540d61a0ae0SRandall Stewart break; 1541ad21a364SRandall Stewart case SCTP_TIMER_TYPE_ZCOPY_SENDQ: 1542eacc51c5SRandall Stewart if (inp == NULL) { 1543eacc51c5SRandall Stewart break; 1544eacc51c5SRandall Stewart } 1545ad21a364SRandall Stewart if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) { 1546ad21a364SRandall Stewart SCTP_ZERO_COPY_SENDQ_EVENT(inp, inp->sctp_socket); 1547ad21a364SRandall Stewart } 1548ad21a364SRandall Stewart break; 1549f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_ADDR_WQ: 1550f8829a4aSRandall Stewart sctp_handle_addr_wq(); 1551f8829a4aSRandall Stewart break; 1552f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_SEND: 1553ad81507eSRandall Stewart if ((stcb == NULL) || (inp == NULL)) { 1554ad81507eSRandall Stewart break; 1555ad81507eSRandall Stewart } 1556f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_timodata); 1557f42a358aSRandall Stewart stcb->asoc.timodata++; 1558f8829a4aSRandall Stewart stcb->asoc.num_send_timers_up--; 1559f8829a4aSRandall Stewart if (stcb->asoc.num_send_timers_up < 0) { 1560f8829a4aSRandall Stewart stcb->asoc.num_send_timers_up = 0; 1561f8829a4aSRandall Stewart } 1562b54d3a6cSRandall Stewart SCTP_TCB_LOCK_ASSERT(stcb); 156360990c0cSMichael Tuexen if (sctp_t3rxt_timer(inp, stcb, net)) { 1564f8829a4aSRandall Stewart /* no need to unlock on tcb its gone */ 1565f8829a4aSRandall Stewart 1566f8829a4aSRandall Stewart goto out_decr; 1567f8829a4aSRandall Stewart } 1568b54d3a6cSRandall Stewart SCTP_TCB_LOCK_ASSERT(stcb); 1569f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED 1570f8829a4aSRandall Stewart sctp_auditing(4, inp, stcb, net); 1571f8829a4aSRandall Stewart #endif 1572ceaad40aSRandall Stewart sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED); 1573f8829a4aSRandall Stewart if ((stcb->asoc.num_send_timers_up == 0) && 15744a9ef3f8SMichael Tuexen (stcb->asoc.sent_queue_cnt > 0)) { 1575f8829a4aSRandall Stewart struct sctp_tmit_chunk *chk; 1576f8829a4aSRandall Stewart 1577f8829a4aSRandall Stewart /* 1578f8829a4aSRandall Stewart * safeguard. If there on some on the sent queue 1579f8829a4aSRandall Stewart * somewhere but no timers running something is 1580f8829a4aSRandall Stewart * wrong... so we start a timer on the first chunk 1581f8829a4aSRandall Stewart * on the send queue on whatever net it is sent to. 1582f8829a4aSRandall Stewart */ 1583f8829a4aSRandall Stewart chk = TAILQ_FIRST(&stcb->asoc.sent_queue); 1584f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, 1585f8829a4aSRandall Stewart chk->whoTo); 1586f8829a4aSRandall Stewart } 1587f8829a4aSRandall Stewart break; 1588f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_INIT: 1589ad81507eSRandall Stewart if ((stcb == NULL) || (inp == NULL)) { 1590ad81507eSRandall Stewart break; 1591ad81507eSRandall Stewart } 1592f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_timoinit); 1593f42a358aSRandall Stewart stcb->asoc.timoinit++; 1594f8829a4aSRandall Stewart if (sctp_t1init_timer(inp, stcb, net)) { 1595f8829a4aSRandall Stewart /* no need to unlock on tcb its gone */ 1596f8829a4aSRandall Stewart goto out_decr; 1597f8829a4aSRandall Stewart } 1598f8829a4aSRandall Stewart /* We do output but not here */ 1599f8829a4aSRandall Stewart did_output = 0; 1600f8829a4aSRandall Stewart break; 1601f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_RECV: 1602ad81507eSRandall Stewart if ((stcb == NULL) || (inp == NULL)) { 1603ad81507eSRandall Stewart break; 1604ca85e948SMichael Tuexen } 1605f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_timosack); 1606f42a358aSRandall Stewart stcb->asoc.timosack++; 1607689e6a5fSMichael Tuexen sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED); 1608f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED 1609f8829a4aSRandall Stewart sctp_auditing(4, inp, stcb, net); 1610f8829a4aSRandall Stewart #endif 1611ceaad40aSRandall Stewart sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SACK_TMR, SCTP_SO_NOT_LOCKED); 1612f8829a4aSRandall Stewart break; 1613f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_SHUTDOWN: 1614ad81507eSRandall Stewart if ((stcb == NULL) || (inp == NULL)) { 1615ad81507eSRandall Stewart break; 1616ad81507eSRandall Stewart } 1617f8829a4aSRandall Stewart if (sctp_shutdown_timer(inp, stcb, net)) { 1618f8829a4aSRandall Stewart /* no need to unlock on tcb its gone */ 1619f8829a4aSRandall Stewart goto out_decr; 1620f8829a4aSRandall Stewart } 1621f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_timoshutdown); 1622f42a358aSRandall Stewart stcb->asoc.timoshutdown++; 1623f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED 1624f8829a4aSRandall Stewart sctp_auditing(4, inp, stcb, net); 1625f8829a4aSRandall Stewart #endif 1626ceaad40aSRandall Stewart sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_NOT_LOCKED); 1627f8829a4aSRandall Stewart break; 1628f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_HEARTBEAT: 1629ca85e948SMichael Tuexen if ((stcb == NULL) || (inp == NULL) || (net == NULL)) { 1630ad81507eSRandall Stewart break; 1631ad81507eSRandall Stewart } 1632f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_timoheartbeat); 1633f42a358aSRandall Stewart stcb->asoc.timoheartbeat++; 1634ca85e948SMichael Tuexen if (sctp_heartbeat_timer(inp, stcb, net)) { 1635f8829a4aSRandall Stewart /* no need to unlock on tcb its gone */ 1636f8829a4aSRandall Stewart goto out_decr; 1637f8829a4aSRandall Stewart } 1638f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED 1639ca85e948SMichael Tuexen sctp_auditing(4, inp, stcb, net); 1640f8829a4aSRandall Stewart #endif 1641ca85e948SMichael Tuexen if (!(net->dest_state & SCTP_ADDR_NOHB)) { 1642629749b6SMichael Tuexen sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net); 1643ceaad40aSRandall Stewart sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_HB_TMR, SCTP_SO_NOT_LOCKED); 1644f8829a4aSRandall Stewart } 1645f8829a4aSRandall Stewart break; 1646f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_COOKIE: 1647ad81507eSRandall Stewart if ((stcb == NULL) || (inp == NULL)) { 1648ad81507eSRandall Stewart break; 1649ad81507eSRandall Stewart } 1650f8829a4aSRandall Stewart if (sctp_cookie_timer(inp, stcb, net)) { 1651f8829a4aSRandall Stewart /* no need to unlock on tcb its gone */ 1652f8829a4aSRandall Stewart goto out_decr; 1653f8829a4aSRandall Stewart } 1654f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_timocookie); 1655f42a358aSRandall Stewart stcb->asoc.timocookie++; 1656f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED 1657f8829a4aSRandall Stewart sctp_auditing(4, inp, stcb, net); 1658f8829a4aSRandall Stewart #endif 1659f8829a4aSRandall Stewart /* 1660f8829a4aSRandall Stewart * We consider T3 and Cookie timer pretty much the same with 1661f8829a4aSRandall Stewart * respect to where from in chunk_output. 1662f8829a4aSRandall Stewart */ 1663ceaad40aSRandall Stewart sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED); 1664f8829a4aSRandall Stewart break; 1665f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_NEWCOOKIE: 1666f8829a4aSRandall Stewart { 1667f8829a4aSRandall Stewart struct timeval tv; 1668f8829a4aSRandall Stewart int i, secret; 1669f8829a4aSRandall Stewart 1670ad81507eSRandall Stewart if (inp == NULL) { 1671ad81507eSRandall Stewart break; 1672ad81507eSRandall Stewart } 1673f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_timosecret); 16746e55db54SRandall Stewart (void)SCTP_GETTIME_TIMEVAL(&tv); 1675f8829a4aSRandall Stewart SCTP_INP_WLOCK(inp); 1676f8829a4aSRandall Stewart inp->sctp_ep.time_of_secret_change = tv.tv_sec; 1677f8829a4aSRandall Stewart inp->sctp_ep.last_secret_number = 1678f8829a4aSRandall Stewart inp->sctp_ep.current_secret_number; 1679f8829a4aSRandall Stewart inp->sctp_ep.current_secret_number++; 1680f8829a4aSRandall Stewart if (inp->sctp_ep.current_secret_number >= 1681f8829a4aSRandall Stewart SCTP_HOW_MANY_SECRETS) { 1682f8829a4aSRandall Stewart inp->sctp_ep.current_secret_number = 0; 1683f8829a4aSRandall Stewart } 1684f8829a4aSRandall Stewart secret = (int)inp->sctp_ep.current_secret_number; 1685f8829a4aSRandall Stewart for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) { 1686f8829a4aSRandall Stewart inp->sctp_ep.secret_key[secret][i] = 1687f8829a4aSRandall Stewart sctp_select_initial_TSN(&inp->sctp_ep); 1688f8829a4aSRandall Stewart } 1689f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(inp); 1690f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, stcb, net); 1691f8829a4aSRandall Stewart } 1692f8829a4aSRandall Stewart did_output = 0; 1693f8829a4aSRandall Stewart break; 1694f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_PATHMTURAISE: 1695ad81507eSRandall Stewart if ((stcb == NULL) || (inp == NULL)) { 1696ad81507eSRandall Stewart break; 1697ad81507eSRandall Stewart } 1698f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_timopathmtu); 1699f8829a4aSRandall Stewart sctp_pathmtu_timer(inp, stcb, net); 1700f8829a4aSRandall Stewart did_output = 0; 1701f8829a4aSRandall Stewart break; 1702f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_SHUTDOWNACK: 1703ad81507eSRandall Stewart if ((stcb == NULL) || (inp == NULL)) { 1704ad81507eSRandall Stewart break; 1705ad81507eSRandall Stewart } 1706f8829a4aSRandall Stewart if (sctp_shutdownack_timer(inp, stcb, net)) { 1707f8829a4aSRandall Stewart /* no need to unlock on tcb its gone */ 1708f8829a4aSRandall Stewart goto out_decr; 1709f8829a4aSRandall Stewart } 1710f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_timoshutdownack); 1711f42a358aSRandall Stewart stcb->asoc.timoshutdownack++; 1712f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED 1713f8829a4aSRandall Stewart sctp_auditing(4, inp, stcb, net); 1714f8829a4aSRandall Stewart #endif 1715ceaad40aSRandall Stewart sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_ACK_TMR, SCTP_SO_NOT_LOCKED); 1716f8829a4aSRandall Stewart break; 1717f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_SHUTDOWNGUARD: 1718ad81507eSRandall Stewart if ((stcb == NULL) || (inp == NULL)) { 1719ad81507eSRandall Stewart break; 1720ad81507eSRandall Stewart } 1721f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_timoshutdownguard); 1722a2b42326SMichael Tuexen sctp_abort_an_association(inp, stcb, NULL, SCTP_SO_NOT_LOCKED); 1723f8829a4aSRandall Stewart /* no need to unlock on tcb its gone */ 1724f8829a4aSRandall Stewart goto out_decr; 1725f8829a4aSRandall Stewart 1726f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_STRRESET: 1727ad81507eSRandall Stewart if ((stcb == NULL) || (inp == NULL)) { 1728ad81507eSRandall Stewart break; 1729ad81507eSRandall Stewart } 1730f8829a4aSRandall Stewart if (sctp_strreset_timer(inp, stcb, net)) { 1731f8829a4aSRandall Stewart /* no need to unlock on tcb its gone */ 1732f8829a4aSRandall Stewart goto out_decr; 1733f8829a4aSRandall Stewart } 1734f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_timostrmrst); 1735ceaad40aSRandall Stewart sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_TMR, SCTP_SO_NOT_LOCKED); 1736f8829a4aSRandall Stewart break; 1737f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_ASCONF: 1738ad81507eSRandall Stewart if ((stcb == NULL) || (inp == NULL)) { 1739ad81507eSRandall Stewart break; 1740ad81507eSRandall Stewart } 1741f8829a4aSRandall Stewart if (sctp_asconf_timer(inp, stcb, net)) { 1742f8829a4aSRandall Stewart /* no need to unlock on tcb its gone */ 1743f8829a4aSRandall Stewart goto out_decr; 1744f8829a4aSRandall Stewart } 1745f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_timoasconf); 1746f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED 1747f8829a4aSRandall Stewart sctp_auditing(4, inp, stcb, net); 1748f8829a4aSRandall Stewart #endif 1749ceaad40aSRandall Stewart sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_ASCONF_TMR, SCTP_SO_NOT_LOCKED); 1750f8829a4aSRandall Stewart break; 1751851b7298SRandall Stewart case SCTP_TIMER_TYPE_PRIM_DELETED: 1752851b7298SRandall Stewart if ((stcb == NULL) || (inp == NULL)) { 1753851b7298SRandall Stewart break; 1754851b7298SRandall Stewart } 175504ee05e8SRandall Stewart sctp_delete_prim_timer(inp, stcb, net); 1756851b7298SRandall Stewart SCTP_STAT_INCR(sctps_timodelprim); 1757851b7298SRandall Stewart break; 1758f8829a4aSRandall Stewart 1759f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_AUTOCLOSE: 1760ad81507eSRandall Stewart if ((stcb == NULL) || (inp == NULL)) { 1761ad81507eSRandall Stewart break; 1762ad81507eSRandall Stewart } 1763f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_timoautoclose); 1764f8829a4aSRandall Stewart sctp_autoclose_timer(inp, stcb, net); 1765ceaad40aSRandall Stewart sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED); 1766f8829a4aSRandall Stewart did_output = 0; 1767f8829a4aSRandall Stewart break; 1768f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_ASOCKILL: 1769ad81507eSRandall Stewart if ((stcb == NULL) || (inp == NULL)) { 1770ad81507eSRandall Stewart break; 1771ad81507eSRandall Stewart } 1772f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_timoassockill); 1773f8829a4aSRandall Stewart /* Can we free it yet? */ 1774f8829a4aSRandall Stewart SCTP_INP_DECR_REF(inp); 1775a5d547adSRandall Stewart sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_1); 1776ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1777ceaad40aSRandall Stewart so = SCTP_INP_SO(inp); 1778ceaad40aSRandall Stewart atomic_add_int(&stcb->asoc.refcnt, 1); 1779ceaad40aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 1780ceaad40aSRandall Stewart SCTP_SOCKET_LOCK(so, 1); 1781ceaad40aSRandall Stewart SCTP_TCB_LOCK(stcb); 1782ceaad40aSRandall Stewart atomic_subtract_int(&stcb->asoc.refcnt, 1); 1783ceaad40aSRandall Stewart #endif 1784c4739e2fSRandall Stewart (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_2); 1785ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1786ceaad40aSRandall Stewart SCTP_SOCKET_UNLOCK(so, 1); 1787ceaad40aSRandall Stewart #endif 1788f8829a4aSRandall Stewart /* 1789f8829a4aSRandall Stewart * free asoc, always unlocks (or destroy's) so prevent 1790f8829a4aSRandall Stewart * duplicate unlock or unlock of a free mtx :-0 1791f8829a4aSRandall Stewart */ 1792f8829a4aSRandall Stewart stcb = NULL; 1793f8829a4aSRandall Stewart goto out_no_decr; 1794f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_INPKILL: 1795f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_timoinpkill); 1796ad81507eSRandall Stewart if (inp == NULL) { 1797ad81507eSRandall Stewart break; 1798ad81507eSRandall Stewart } 1799f8829a4aSRandall Stewart /* 1800f8829a4aSRandall Stewart * special case, take away our increment since WE are the 1801f8829a4aSRandall Stewart * killer 1802f8829a4aSRandall Stewart */ 1803f8829a4aSRandall Stewart SCTP_INP_DECR_REF(inp); 1804a5d547adSRandall Stewart sctp_timer_stop(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_3); 1805b0552ae2SRandall Stewart sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT, 18060c7dc840SRandall Stewart SCTP_CALLED_FROM_INPKILL_TIMER); 1807d61374e1SRandall Stewart inp = NULL; 1808f8829a4aSRandall Stewart goto out_no_decr; 1809f8829a4aSRandall Stewart default: 1810ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_TIMER1, "sctp_timeout_handler:unknown timer %d\n", 1811f8829a4aSRandall Stewart tmr->type); 1812f8829a4aSRandall Stewart break; 181360990c0cSMichael Tuexen } 1814f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED 1815f8829a4aSRandall Stewart sctp_audit_log(0xF1, (uint8_t) tmr->type); 1816f8829a4aSRandall Stewart if (inp) 1817f8829a4aSRandall Stewart sctp_auditing(5, inp, stcb, net); 1818f8829a4aSRandall Stewart #endif 1819f8829a4aSRandall Stewart if ((did_output) && stcb) { 1820f8829a4aSRandall Stewart /* 1821f8829a4aSRandall Stewart * Now we need to clean up the control chunk chain if an 1822f8829a4aSRandall Stewart * ECNE is on it. It must be marked as UNSENT again so next 1823f8829a4aSRandall Stewart * call will continue to send it until such time that we get 1824f8829a4aSRandall Stewart * a CWR, to remove it. It is, however, less likely that we 1825f8829a4aSRandall Stewart * will find a ecn echo on the chain though. 1826f8829a4aSRandall Stewart */ 1827f8829a4aSRandall Stewart sctp_fix_ecn_echo(&stcb->asoc); 1828f8829a4aSRandall Stewart } 182944b7479bSRandall Stewart get_out: 1830f8829a4aSRandall Stewart if (stcb) { 1831f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 1832f8829a4aSRandall Stewart } 1833f8829a4aSRandall Stewart out_decr: 1834f8829a4aSRandall Stewart if (inp) { 1835f8829a4aSRandall Stewart SCTP_INP_DECR_REF(inp); 1836f8829a4aSRandall Stewart } 1837f8829a4aSRandall Stewart out_no_decr: 1838ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_TIMER1, "Timer now complete (type %d)\n", 1839d61374e1SRandall Stewart type); 18408518270eSMichael Tuexen CURVNET_RESTORE(); 1841f8829a4aSRandall Stewart } 1842f8829a4aSRandall Stewart 1843ad81507eSRandall Stewart void 1844f8829a4aSRandall Stewart sctp_timer_start(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb, 1845f8829a4aSRandall Stewart struct sctp_nets *net) 1846f8829a4aSRandall Stewart { 1847ca85e948SMichael Tuexen uint32_t to_ticks; 1848f8829a4aSRandall Stewart struct sctp_timer *tmr; 1849f8829a4aSRandall Stewart 1850139bc87fSRandall Stewart if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) && (inp == NULL)) 1851ad81507eSRandall Stewart return; 1852f8829a4aSRandall Stewart 1853f8829a4aSRandall Stewart tmr = NULL; 1854f8829a4aSRandall Stewart if (stcb) { 1855f8829a4aSRandall Stewart SCTP_TCB_LOCK_ASSERT(stcb); 1856f8829a4aSRandall Stewart } 1857f8829a4aSRandall Stewart switch (t_type) { 1858d61a0ae0SRandall Stewart case SCTP_TIMER_TYPE_ZERO_COPY: 1859d61a0ae0SRandall Stewart tmr = &inp->sctp_ep.zero_copy_timer; 1860d61a0ae0SRandall Stewart to_ticks = SCTP_ZERO_COPY_TICK_DELAY; 1861d61a0ae0SRandall Stewart break; 1862ad21a364SRandall Stewart case SCTP_TIMER_TYPE_ZCOPY_SENDQ: 1863ad21a364SRandall Stewart tmr = &inp->sctp_ep.zero_copy_sendq_timer; 1864ad21a364SRandall Stewart to_ticks = SCTP_ZERO_COPY_SENDQ_TICK_DELAY; 1865ad21a364SRandall Stewart break; 1866f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_ADDR_WQ: 1867f8829a4aSRandall Stewart /* Only 1 tick away :-) */ 1868b3f1ea41SRandall Stewart tmr = &SCTP_BASE_INFO(addr_wq_timer); 186942551e99SRandall Stewart to_ticks = SCTP_ADDRESS_TICK_DELAY; 1870f8829a4aSRandall Stewart break; 1871f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_SEND: 1872f8829a4aSRandall Stewart /* Here we use the RTO timer */ 1873f8829a4aSRandall Stewart { 1874f8829a4aSRandall Stewart int rto_val; 1875f8829a4aSRandall Stewart 1876f8829a4aSRandall Stewart if ((stcb == NULL) || (net == NULL)) { 1877ad81507eSRandall Stewart return; 1878f8829a4aSRandall Stewart } 1879f8829a4aSRandall Stewart tmr = &net->rxt_timer; 1880f8829a4aSRandall Stewart if (net->RTO == 0) { 1881f8829a4aSRandall Stewart rto_val = stcb->asoc.initial_rto; 1882f8829a4aSRandall Stewart } else { 1883f8829a4aSRandall Stewart rto_val = net->RTO; 1884f8829a4aSRandall Stewart } 1885f8829a4aSRandall Stewart to_ticks = MSEC_TO_TICKS(rto_val); 1886f8829a4aSRandall Stewart } 1887f8829a4aSRandall Stewart break; 1888f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_INIT: 1889f8829a4aSRandall Stewart /* 1890f8829a4aSRandall Stewart * Here we use the INIT timer default usually about 1 1891f8829a4aSRandall Stewart * minute. 1892f8829a4aSRandall Stewart */ 1893f8829a4aSRandall Stewart if ((stcb == NULL) || (net == NULL)) { 1894ad81507eSRandall Stewart return; 1895f8829a4aSRandall Stewart } 1896f8829a4aSRandall Stewart tmr = &net->rxt_timer; 1897f8829a4aSRandall Stewart if (net->RTO == 0) { 1898f8829a4aSRandall Stewart to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto); 1899f8829a4aSRandall Stewart } else { 1900f8829a4aSRandall Stewart to_ticks = MSEC_TO_TICKS(net->RTO); 1901f8829a4aSRandall Stewart } 1902f8829a4aSRandall Stewart break; 1903f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_RECV: 1904f8829a4aSRandall Stewart /* 1905f8829a4aSRandall Stewart * Here we use the Delayed-Ack timer value from the inp 1906f8829a4aSRandall Stewart * ususually about 200ms. 1907f8829a4aSRandall Stewart */ 1908f8829a4aSRandall Stewart if (stcb == NULL) { 1909ad81507eSRandall Stewart return; 1910f8829a4aSRandall Stewart } 1911f8829a4aSRandall Stewart tmr = &stcb->asoc.dack_timer; 1912f8829a4aSRandall Stewart to_ticks = MSEC_TO_TICKS(stcb->asoc.delayed_ack); 1913f8829a4aSRandall Stewart break; 1914f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_SHUTDOWN: 1915f8829a4aSRandall Stewart /* Here we use the RTO of the destination. */ 1916f8829a4aSRandall Stewart if ((stcb == NULL) || (net == NULL)) { 1917ad81507eSRandall Stewart return; 1918f8829a4aSRandall Stewart } 1919f8829a4aSRandall Stewart if (net->RTO == 0) { 1920f8829a4aSRandall Stewart to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto); 1921f8829a4aSRandall Stewart } else { 1922f8829a4aSRandall Stewart to_ticks = MSEC_TO_TICKS(net->RTO); 1923f8829a4aSRandall Stewart } 1924f8829a4aSRandall Stewart tmr = &net->rxt_timer; 1925f8829a4aSRandall Stewart break; 1926f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_HEARTBEAT: 1927f8829a4aSRandall Stewart /* 1928f8829a4aSRandall Stewart * the net is used here so that we can add in the RTO. Even 1929f8829a4aSRandall Stewart * though we use a different timer. We also add the HB timer 1930f8829a4aSRandall Stewart * PLUS a random jitter. 1931f8829a4aSRandall Stewart */ 1932ca85e948SMichael Tuexen if ((inp == NULL) || (stcb == NULL) || (net == NULL)) { 1933ad81507eSRandall Stewart return; 1934ad81507eSRandall Stewart } else { 1935f8829a4aSRandall Stewart uint32_t rndval; 1936ca85e948SMichael Tuexen uint32_t jitter; 1937f8829a4aSRandall Stewart 1938ca85e948SMichael Tuexen if ((net->dest_state & SCTP_ADDR_NOHB) && 1939ca85e948SMichael Tuexen !(net->dest_state & SCTP_ADDR_UNCONFIRMED)) { 1940ad81507eSRandall Stewart return; 1941f8829a4aSRandall Stewart } 1942f8829a4aSRandall Stewart if (net->RTO == 0) { 1943ca85e948SMichael Tuexen to_ticks = stcb->asoc.initial_rto; 1944f8829a4aSRandall Stewart } else { 1945ca85e948SMichael Tuexen to_ticks = net->RTO; 1946f8829a4aSRandall Stewart } 1947ca85e948SMichael Tuexen rndval = sctp_select_initial_TSN(&inp->sctp_ep); 1948ca85e948SMichael Tuexen jitter = rndval % to_ticks; 1949ca85e948SMichael Tuexen if (jitter >= (to_ticks >> 1)) { 1950ca85e948SMichael Tuexen to_ticks = to_ticks + (jitter - (to_ticks >> 1)); 1951f8829a4aSRandall Stewart } else { 1952ca85e948SMichael Tuexen to_ticks = to_ticks - jitter; 1953f8829a4aSRandall Stewart } 1954ca85e948SMichael Tuexen if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) && 1955ca85e948SMichael Tuexen !(net->dest_state & SCTP_ADDR_PF)) { 1956ca85e948SMichael Tuexen to_ticks += net->heart_beat_delay; 1957f8829a4aSRandall Stewart } 1958f8829a4aSRandall Stewart /* 1959f8829a4aSRandall Stewart * Now we must convert the to_ticks that are now in 1960f8829a4aSRandall Stewart * ms to ticks. 1961f8829a4aSRandall Stewart */ 1962f8829a4aSRandall Stewart to_ticks = MSEC_TO_TICKS(to_ticks); 1963ca85e948SMichael Tuexen tmr = &net->hb_timer; 1964f8829a4aSRandall Stewart } 1965f8829a4aSRandall Stewart break; 1966f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_COOKIE: 1967f8829a4aSRandall Stewart /* 1968f8829a4aSRandall Stewart * Here we can use the RTO timer from the network since one 1969f8829a4aSRandall Stewart * RTT was compelete. If a retran happened then we will be 1970f8829a4aSRandall Stewart * using the RTO initial value. 1971f8829a4aSRandall Stewart */ 1972f8829a4aSRandall Stewart if ((stcb == NULL) || (net == NULL)) { 1973ad81507eSRandall Stewart return; 1974f8829a4aSRandall Stewart } 1975f8829a4aSRandall Stewart if (net->RTO == 0) { 1976f8829a4aSRandall Stewart to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto); 1977f8829a4aSRandall Stewart } else { 1978f8829a4aSRandall Stewart to_ticks = MSEC_TO_TICKS(net->RTO); 1979f8829a4aSRandall Stewart } 1980f8829a4aSRandall Stewart tmr = &net->rxt_timer; 1981f8829a4aSRandall Stewart break; 1982f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_NEWCOOKIE: 1983f8829a4aSRandall Stewart /* 1984f8829a4aSRandall Stewart * nothing needed but the endpoint here ususually about 60 1985f8829a4aSRandall Stewart * minutes. 1986f8829a4aSRandall Stewart */ 1987ad81507eSRandall Stewart if (inp == NULL) { 1988ad81507eSRandall Stewart return; 1989ad81507eSRandall Stewart } 1990f8829a4aSRandall Stewart tmr = &inp->sctp_ep.signature_change; 1991f8829a4aSRandall Stewart to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_SIGNATURE]; 1992f8829a4aSRandall Stewart break; 1993f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_ASOCKILL: 1994f8829a4aSRandall Stewart if (stcb == NULL) { 1995ad81507eSRandall Stewart return; 1996f8829a4aSRandall Stewart } 1997f8829a4aSRandall Stewart tmr = &stcb->asoc.strreset_timer; 1998f8829a4aSRandall Stewart to_ticks = MSEC_TO_TICKS(SCTP_ASOC_KILL_TIMEOUT); 1999f8829a4aSRandall Stewart break; 2000f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_INPKILL: 2001f8829a4aSRandall Stewart /* 2002f8829a4aSRandall Stewart * The inp is setup to die. We re-use the signature_chage 2003f8829a4aSRandall Stewart * timer since that has stopped and we are in the GONE 2004f8829a4aSRandall Stewart * state. 2005f8829a4aSRandall Stewart */ 2006ad81507eSRandall Stewart if (inp == NULL) { 2007ad81507eSRandall Stewart return; 2008ad81507eSRandall Stewart } 2009f8829a4aSRandall Stewart tmr = &inp->sctp_ep.signature_change; 2010f8829a4aSRandall Stewart to_ticks = MSEC_TO_TICKS(SCTP_INP_KILL_TIMEOUT); 2011f8829a4aSRandall Stewart break; 2012f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_PATHMTURAISE: 2013f8829a4aSRandall Stewart /* 2014f8829a4aSRandall Stewart * Here we use the value found in the EP for PMTU ususually 2015f8829a4aSRandall Stewart * about 10 minutes. 2016f8829a4aSRandall Stewart */ 2017ad81507eSRandall Stewart if ((stcb == NULL) || (inp == NULL)) { 2018ad81507eSRandall Stewart return; 2019f8829a4aSRandall Stewart } 2020f8829a4aSRandall Stewart if (net == NULL) { 2021ad81507eSRandall Stewart return; 2022f8829a4aSRandall Stewart } 202380c79bbeSMichael Tuexen if (net->dest_state & SCTP_ADDR_NO_PMTUD) { 202480c79bbeSMichael Tuexen return; 202580c79bbeSMichael Tuexen } 2026f8829a4aSRandall Stewart to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_PMTU]; 2027f8829a4aSRandall Stewart tmr = &net->pmtu_timer; 2028f8829a4aSRandall Stewart break; 2029f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_SHUTDOWNACK: 2030f8829a4aSRandall Stewart /* Here we use the RTO of the destination */ 2031f8829a4aSRandall Stewart if ((stcb == NULL) || (net == NULL)) { 2032ad81507eSRandall Stewart return; 2033f8829a4aSRandall Stewart } 2034f8829a4aSRandall Stewart if (net->RTO == 0) { 2035f8829a4aSRandall Stewart to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto); 2036f8829a4aSRandall Stewart } else { 2037f8829a4aSRandall Stewart to_ticks = MSEC_TO_TICKS(net->RTO); 2038f8829a4aSRandall Stewart } 2039f8829a4aSRandall Stewart tmr = &net->rxt_timer; 2040f8829a4aSRandall Stewart break; 2041f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_SHUTDOWNGUARD: 2042f8829a4aSRandall Stewart /* 2043f8829a4aSRandall Stewart * Here we use the endpoints shutdown guard timer usually 2044f8829a4aSRandall Stewart * about 3 minutes. 2045f8829a4aSRandall Stewart */ 2046ad81507eSRandall Stewart if ((inp == NULL) || (stcb == NULL)) { 2047ad81507eSRandall Stewart return; 2048f8829a4aSRandall Stewart } 2049f8829a4aSRandall Stewart to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN]; 2050f8829a4aSRandall Stewart tmr = &stcb->asoc.shut_guard_timer; 2051f8829a4aSRandall Stewart break; 2052f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_STRRESET: 2053f8829a4aSRandall Stewart /* 20541b649582SRandall Stewart * Here the timer comes from the stcb but its value is from 20551b649582SRandall Stewart * the net's RTO. 2056f8829a4aSRandall Stewart */ 2057f8829a4aSRandall Stewart if ((stcb == NULL) || (net == NULL)) { 2058ad81507eSRandall Stewart return; 2059f8829a4aSRandall Stewart } 2060f8829a4aSRandall Stewart if (net->RTO == 0) { 2061f8829a4aSRandall Stewart to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto); 2062f8829a4aSRandall Stewart } else { 2063f8829a4aSRandall Stewart to_ticks = MSEC_TO_TICKS(net->RTO); 2064f8829a4aSRandall Stewart } 2065f8829a4aSRandall Stewart tmr = &stcb->asoc.strreset_timer; 2066f8829a4aSRandall Stewart break; 2067f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_ASCONF: 2068f8829a4aSRandall Stewart /* 20691b649582SRandall Stewart * Here the timer comes from the stcb but its value is from 20701b649582SRandall Stewart * the net's RTO. 2071f8829a4aSRandall Stewart */ 2072f8829a4aSRandall Stewart if ((stcb == NULL) || (net == NULL)) { 2073ad81507eSRandall Stewart return; 2074f8829a4aSRandall Stewart } 2075f8829a4aSRandall Stewart if (net->RTO == 0) { 2076f8829a4aSRandall Stewart to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto); 2077f8829a4aSRandall Stewart } else { 2078f8829a4aSRandall Stewart to_ticks = MSEC_TO_TICKS(net->RTO); 2079f8829a4aSRandall Stewart } 2080f8829a4aSRandall Stewart tmr = &stcb->asoc.asconf_timer; 2081f8829a4aSRandall Stewart break; 2082851b7298SRandall Stewart case SCTP_TIMER_TYPE_PRIM_DELETED: 2083851b7298SRandall Stewart if ((stcb == NULL) || (net != NULL)) { 2084851b7298SRandall Stewart return; 2085851b7298SRandall Stewart } 2086851b7298SRandall Stewart to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto); 2087851b7298SRandall Stewart tmr = &stcb->asoc.delete_prim_timer; 2088851b7298SRandall Stewart break; 2089f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_AUTOCLOSE: 2090f8829a4aSRandall Stewart if (stcb == NULL) { 2091ad81507eSRandall Stewart return; 2092f8829a4aSRandall Stewart } 2093f8829a4aSRandall Stewart if (stcb->asoc.sctp_autoclose_ticks == 0) { 2094f8829a4aSRandall Stewart /* 2095f8829a4aSRandall Stewart * Really an error since stcb is NOT set to 2096f8829a4aSRandall Stewart * autoclose 2097f8829a4aSRandall Stewart */ 2098ad81507eSRandall Stewart return; 2099f8829a4aSRandall Stewart } 2100f8829a4aSRandall Stewart to_ticks = stcb->asoc.sctp_autoclose_ticks; 2101f8829a4aSRandall Stewart tmr = &stcb->asoc.autoclose_timer; 2102f8829a4aSRandall Stewart break; 2103f8829a4aSRandall Stewart default: 2104ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n", 2105ad81507eSRandall Stewart __FUNCTION__, t_type); 2106ad81507eSRandall Stewart return; 2107f8829a4aSRandall Stewart break; 210860990c0cSMichael Tuexen } 2109f8829a4aSRandall Stewart if ((to_ticks <= 0) || (tmr == NULL)) { 2110ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_TIMER1, "%s: %d:software error to_ticks:%d tmr:%p not set ??\n", 2111dd294dceSMichael Tuexen __FUNCTION__, t_type, to_ticks, (void *)tmr); 2112ad81507eSRandall Stewart return; 2113f8829a4aSRandall Stewart } 2114139bc87fSRandall Stewart if (SCTP_OS_TIMER_PENDING(&tmr->timer)) { 2115f8829a4aSRandall Stewart /* 2116f8829a4aSRandall Stewart * we do NOT allow you to have it already running. if it is 2117f8829a4aSRandall Stewart * we leave the current one up unchanged 2118f8829a4aSRandall Stewart */ 2119ad81507eSRandall Stewart return; 2120f8829a4aSRandall Stewart } 2121f8829a4aSRandall Stewart /* At this point we can proceed */ 2122f8829a4aSRandall Stewart if (t_type == SCTP_TIMER_TYPE_SEND) { 2123f8829a4aSRandall Stewart stcb->asoc.num_send_timers_up++; 2124f8829a4aSRandall Stewart } 2125a5d547adSRandall Stewart tmr->stopped_from = 0; 2126f8829a4aSRandall Stewart tmr->type = t_type; 2127f8829a4aSRandall Stewart tmr->ep = (void *)inp; 2128f8829a4aSRandall Stewart tmr->tcb = (void *)stcb; 2129f8829a4aSRandall Stewart tmr->net = (void *)net; 2130f8829a4aSRandall Stewart tmr->self = (void *)tmr; 21318518270eSMichael Tuexen tmr->vnet = (void *)curvnet; 2132c4739e2fSRandall Stewart tmr->ticks = sctp_get_tick_count(); 2133ad81507eSRandall Stewart (void)SCTP_OS_TIMER_START(&tmr->timer, to_ticks, sctp_timeout_handler, tmr); 2134ad81507eSRandall Stewart return; 2135f8829a4aSRandall Stewart } 2136f8829a4aSRandall Stewart 21376e55db54SRandall Stewart void 2138f8829a4aSRandall Stewart sctp_timer_stop(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb, 2139a5d547adSRandall Stewart struct sctp_nets *net, uint32_t from) 2140f8829a4aSRandall Stewart { 2141f8829a4aSRandall Stewart struct sctp_timer *tmr; 2142f8829a4aSRandall Stewart 2143f8829a4aSRandall Stewart if ((t_type != SCTP_TIMER_TYPE_ADDR_WQ) && 2144f8829a4aSRandall Stewart (inp == NULL)) 21456e55db54SRandall Stewart return; 2146f8829a4aSRandall Stewart 2147f8829a4aSRandall Stewart tmr = NULL; 2148f8829a4aSRandall Stewart if (stcb) { 2149f8829a4aSRandall Stewart SCTP_TCB_LOCK_ASSERT(stcb); 2150f8829a4aSRandall Stewart } 2151f8829a4aSRandall Stewart switch (t_type) { 2152d61a0ae0SRandall Stewart case SCTP_TIMER_TYPE_ZERO_COPY: 2153d61a0ae0SRandall Stewart tmr = &inp->sctp_ep.zero_copy_timer; 2154d61a0ae0SRandall Stewart break; 2155ad21a364SRandall Stewart case SCTP_TIMER_TYPE_ZCOPY_SENDQ: 2156ad21a364SRandall Stewart tmr = &inp->sctp_ep.zero_copy_sendq_timer; 2157ad21a364SRandall Stewart break; 2158f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_ADDR_WQ: 2159b3f1ea41SRandall Stewart tmr = &SCTP_BASE_INFO(addr_wq_timer); 2160f8829a4aSRandall Stewart break; 2161f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_SEND: 2162f8829a4aSRandall Stewart if ((stcb == NULL) || (net == NULL)) { 21636e55db54SRandall Stewart return; 2164f8829a4aSRandall Stewart } 2165f8829a4aSRandall Stewart tmr = &net->rxt_timer; 2166f8829a4aSRandall Stewart break; 2167f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_INIT: 2168f8829a4aSRandall Stewart if ((stcb == NULL) || (net == NULL)) { 21696e55db54SRandall Stewart return; 2170f8829a4aSRandall Stewart } 2171f8829a4aSRandall Stewart tmr = &net->rxt_timer; 2172f8829a4aSRandall Stewart break; 2173f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_RECV: 2174f8829a4aSRandall Stewart if (stcb == NULL) { 21756e55db54SRandall Stewart return; 2176f8829a4aSRandall Stewart } 2177f8829a4aSRandall Stewart tmr = &stcb->asoc.dack_timer; 2178f8829a4aSRandall Stewart break; 2179f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_SHUTDOWN: 2180f8829a4aSRandall Stewart if ((stcb == NULL) || (net == NULL)) { 21816e55db54SRandall Stewart return; 2182f8829a4aSRandall Stewart } 2183f8829a4aSRandall Stewart tmr = &net->rxt_timer; 2184f8829a4aSRandall Stewart break; 2185f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_HEARTBEAT: 2186ca85e948SMichael Tuexen if ((stcb == NULL) || (net == NULL)) { 21876e55db54SRandall Stewart return; 2188f8829a4aSRandall Stewart } 2189ca85e948SMichael Tuexen tmr = &net->hb_timer; 2190f8829a4aSRandall Stewart break; 2191f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_COOKIE: 2192f8829a4aSRandall Stewart if ((stcb == NULL) || (net == NULL)) { 21936e55db54SRandall Stewart return; 2194f8829a4aSRandall Stewart } 2195f8829a4aSRandall Stewart tmr = &net->rxt_timer; 2196f8829a4aSRandall Stewart break; 2197f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_NEWCOOKIE: 2198f8829a4aSRandall Stewart /* nothing needed but the endpoint here */ 2199f8829a4aSRandall Stewart tmr = &inp->sctp_ep.signature_change; 2200f8829a4aSRandall Stewart /* 2201f8829a4aSRandall Stewart * We re-use the newcookie timer for the INP kill timer. We 2202f8829a4aSRandall Stewart * must assure that we do not kill it by accident. 2203f8829a4aSRandall Stewart */ 2204f8829a4aSRandall Stewart break; 2205f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_ASOCKILL: 2206f8829a4aSRandall Stewart /* 2207f8829a4aSRandall Stewart * Stop the asoc kill timer. 2208f8829a4aSRandall Stewart */ 2209f8829a4aSRandall Stewart if (stcb == NULL) { 22106e55db54SRandall Stewart return; 2211f8829a4aSRandall Stewart } 2212f8829a4aSRandall Stewart tmr = &stcb->asoc.strreset_timer; 2213f8829a4aSRandall Stewart break; 2214f8829a4aSRandall Stewart 2215f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_INPKILL: 2216f8829a4aSRandall Stewart /* 2217f8829a4aSRandall Stewart * The inp is setup to die. We re-use the signature_chage 2218f8829a4aSRandall Stewart * timer since that has stopped and we are in the GONE 2219f8829a4aSRandall Stewart * state. 2220f8829a4aSRandall Stewart */ 2221f8829a4aSRandall Stewart tmr = &inp->sctp_ep.signature_change; 2222f8829a4aSRandall Stewart break; 2223f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_PATHMTURAISE: 2224f8829a4aSRandall Stewart if ((stcb == NULL) || (net == NULL)) { 22256e55db54SRandall Stewart return; 2226f8829a4aSRandall Stewart } 2227f8829a4aSRandall Stewart tmr = &net->pmtu_timer; 2228f8829a4aSRandall Stewart break; 2229f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_SHUTDOWNACK: 2230f8829a4aSRandall Stewart if ((stcb == NULL) || (net == NULL)) { 22316e55db54SRandall Stewart return; 2232f8829a4aSRandall Stewart } 2233f8829a4aSRandall Stewart tmr = &net->rxt_timer; 2234f8829a4aSRandall Stewart break; 2235f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_SHUTDOWNGUARD: 2236f8829a4aSRandall Stewart if (stcb == NULL) { 22376e55db54SRandall Stewart return; 2238f8829a4aSRandall Stewart } 2239f8829a4aSRandall Stewart tmr = &stcb->asoc.shut_guard_timer; 2240f8829a4aSRandall Stewart break; 2241f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_STRRESET: 2242f8829a4aSRandall Stewart if (stcb == NULL) { 22436e55db54SRandall Stewart return; 2244f8829a4aSRandall Stewart } 2245f8829a4aSRandall Stewart tmr = &stcb->asoc.strreset_timer; 2246f8829a4aSRandall Stewart break; 2247f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_ASCONF: 2248f8829a4aSRandall Stewart if (stcb == NULL) { 22496e55db54SRandall Stewart return; 2250f8829a4aSRandall Stewart } 2251f8829a4aSRandall Stewart tmr = &stcb->asoc.asconf_timer; 2252f8829a4aSRandall Stewart break; 2253851b7298SRandall Stewart case SCTP_TIMER_TYPE_PRIM_DELETED: 2254851b7298SRandall Stewart if (stcb == NULL) { 2255851b7298SRandall Stewart return; 2256851b7298SRandall Stewart } 2257851b7298SRandall Stewart tmr = &stcb->asoc.delete_prim_timer; 2258851b7298SRandall Stewart break; 2259f8829a4aSRandall Stewart case SCTP_TIMER_TYPE_AUTOCLOSE: 2260f8829a4aSRandall Stewart if (stcb == NULL) { 22616e55db54SRandall Stewart return; 2262f8829a4aSRandall Stewart } 2263f8829a4aSRandall Stewart tmr = &stcb->asoc.autoclose_timer; 2264f8829a4aSRandall Stewart break; 2265f8829a4aSRandall Stewart default: 2266ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_TIMER1, "%s: Unknown timer type %d\n", 2267ad81507eSRandall Stewart __FUNCTION__, t_type); 2268f8829a4aSRandall Stewart break; 226960990c0cSMichael Tuexen } 2270f8829a4aSRandall Stewart if (tmr == NULL) { 22716e55db54SRandall Stewart return; 2272f8829a4aSRandall Stewart } 2273f8829a4aSRandall Stewart if ((tmr->type != t_type) && tmr->type) { 2274f8829a4aSRandall Stewart /* 2275f8829a4aSRandall Stewart * Ok we have a timer that is under joint use. Cookie timer 2276f8829a4aSRandall Stewart * per chance with the SEND timer. We therefore are NOT 2277f8829a4aSRandall Stewart * running the timer that the caller wants stopped. So just 2278f8829a4aSRandall Stewart * return. 2279f8829a4aSRandall Stewart */ 22806e55db54SRandall Stewart return; 2281f8829a4aSRandall Stewart } 2282ad81507eSRandall Stewart if ((t_type == SCTP_TIMER_TYPE_SEND) && (stcb != NULL)) { 2283f8829a4aSRandall Stewart stcb->asoc.num_send_timers_up--; 2284f8829a4aSRandall Stewart if (stcb->asoc.num_send_timers_up < 0) { 2285f8829a4aSRandall Stewart stcb->asoc.num_send_timers_up = 0; 2286f8829a4aSRandall Stewart } 2287f8829a4aSRandall Stewart } 2288f8829a4aSRandall Stewart tmr->self = NULL; 2289a5d547adSRandall Stewart tmr->stopped_from = from; 22906e55db54SRandall Stewart (void)SCTP_OS_TIMER_STOP(&tmr->timer); 22916e55db54SRandall Stewart return; 2292f8829a4aSRandall Stewart } 2293f8829a4aSRandall Stewart 2294f8829a4aSRandall Stewart uint32_t 2295f8829a4aSRandall Stewart sctp_calculate_len(struct mbuf *m) 2296f8829a4aSRandall Stewart { 2297f8829a4aSRandall Stewart uint32_t tlen = 0; 2298f8829a4aSRandall Stewart struct mbuf *at; 2299f8829a4aSRandall Stewart 2300f8829a4aSRandall Stewart at = m; 2301f8829a4aSRandall Stewart while (at) { 2302139bc87fSRandall Stewart tlen += SCTP_BUF_LEN(at); 2303139bc87fSRandall Stewart at = SCTP_BUF_NEXT(at); 2304f8829a4aSRandall Stewart } 2305f8829a4aSRandall Stewart return (tlen); 2306f8829a4aSRandall Stewart } 2307f8829a4aSRandall Stewart 2308f8829a4aSRandall Stewart void 2309f8829a4aSRandall Stewart sctp_mtu_size_reset(struct sctp_inpcb *inp, 231044b7479bSRandall Stewart struct sctp_association *asoc, uint32_t mtu) 2311f8829a4aSRandall Stewart { 2312f8829a4aSRandall Stewart /* 2313f8829a4aSRandall Stewart * Reset the P-MTU size on this association, this involves changing 2314f8829a4aSRandall Stewart * the asoc MTU, going through ANY chunk+overhead larger than mtu to 2315f8829a4aSRandall Stewart * allow the DF flag to be cleared. 2316f8829a4aSRandall Stewart */ 2317f8829a4aSRandall Stewart struct sctp_tmit_chunk *chk; 2318f8829a4aSRandall Stewart unsigned int eff_mtu, ovh; 2319f8829a4aSRandall Stewart 2320f8829a4aSRandall Stewart asoc->smallest_mtu = mtu; 2321f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 2322f8829a4aSRandall Stewart ovh = SCTP_MIN_OVERHEAD; 2323f8829a4aSRandall Stewart } else { 2324f8829a4aSRandall Stewart ovh = SCTP_MIN_V4_OVERHEAD; 2325f8829a4aSRandall Stewart } 2326f8829a4aSRandall Stewart eff_mtu = mtu - ovh; 2327f8829a4aSRandall Stewart TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) { 2328f8829a4aSRandall Stewart if (chk->send_size > eff_mtu) { 2329f8829a4aSRandall Stewart chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; 2330f8829a4aSRandall Stewart } 2331f8829a4aSRandall Stewart } 2332f8829a4aSRandall Stewart TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) { 2333f8829a4aSRandall Stewart if (chk->send_size > eff_mtu) { 2334f8829a4aSRandall Stewart chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; 2335f8829a4aSRandall Stewart } 2336f8829a4aSRandall Stewart } 2337f8829a4aSRandall Stewart } 2338f8829a4aSRandall Stewart 2339f8829a4aSRandall Stewart 2340f8829a4aSRandall Stewart /* 2341f8829a4aSRandall Stewart * given an association and starting time of the current RTT period return 2342f42a358aSRandall Stewart * RTO in number of msecs net should point to the current network 2343f8829a4aSRandall Stewart */ 2344899288aeSRandall Stewart 2345f8829a4aSRandall Stewart uint32_t 2346f8829a4aSRandall Stewart sctp_calculate_rto(struct sctp_tcb *stcb, 2347f8829a4aSRandall Stewart struct sctp_association *asoc, 2348f8829a4aSRandall Stewart struct sctp_nets *net, 234918e198d3SRandall Stewart struct timeval *told, 2350f79aab18SRandall Stewart int safe, int rtt_from_sack) 2351f8829a4aSRandall Stewart { 235218e198d3SRandall Stewart /*- 2353f8829a4aSRandall Stewart * given an association and the starting time of the current RTT 2354f42a358aSRandall Stewart * period (in value1/value2) return RTO in number of msecs. 2355f8829a4aSRandall Stewart */ 2356be1d9176SMichael Tuexen int32_t rtt; /* RTT in ms */ 2357be1d9176SMichael Tuexen uint32_t new_rto; 2358f8829a4aSRandall Stewart int first_measure = 0; 235918e198d3SRandall Stewart struct timeval now, then, *old; 2360f8829a4aSRandall Stewart 236118e198d3SRandall Stewart /* Copy it out for sparc64 */ 236218e198d3SRandall Stewart if (safe == sctp_align_unsafe_makecopy) { 236318e198d3SRandall Stewart old = &then; 236418e198d3SRandall Stewart memcpy(&then, told, sizeof(struct timeval)); 236518e198d3SRandall Stewart } else if (safe == sctp_align_safe_nocopy) { 236618e198d3SRandall Stewart old = told; 236718e198d3SRandall Stewart } else { 236818e198d3SRandall Stewart /* error */ 236918e198d3SRandall Stewart SCTP_PRINTF("Huh, bad rto calc call\n"); 237018e198d3SRandall Stewart return (0); 237118e198d3SRandall Stewart } 2372f8829a4aSRandall Stewart /************************/ 2373f8829a4aSRandall Stewart /* 1. calculate new RTT */ 2374f8829a4aSRandall Stewart /************************/ 2375f8829a4aSRandall Stewart /* get the current time */ 2376299108c5SRandall Stewart if (stcb->asoc.use_precise_time) { 2377299108c5SRandall Stewart (void)SCTP_GETPTIME_TIMEVAL(&now); 2378299108c5SRandall Stewart } else { 23796e55db54SRandall Stewart (void)SCTP_GETTIME_TIMEVAL(&now); 2380299108c5SRandall Stewart } 2381be1d9176SMichael Tuexen timevalsub(&now, old); 2382be1d9176SMichael Tuexen /* store the current RTT in us */ 238381eb4e63SMichael Tuexen net->rtt = (uint64_t) 1000000 *(uint64_t) now.tv_sec + 2384be1d9176SMichael Tuexen (uint64_t) now.tv_usec; 2385be1d9176SMichael Tuexen 2386be1d9176SMichael Tuexen /* computer rtt in ms */ 2387be1d9176SMichael Tuexen rtt = net->rtt / 1000; 2388f79aab18SRandall Stewart if ((asoc->cc_functions.sctp_rtt_calculated) && (rtt_from_sack == SCTP_RTT_FROM_DATA)) { 2389f79aab18SRandall Stewart /* 2390f79aab18SRandall Stewart * Tell the CC module that a new update has just occurred 2391f79aab18SRandall Stewart * from a sack 2392f79aab18SRandall Stewart */ 2393f79aab18SRandall Stewart (*asoc->cc_functions.sctp_rtt_calculated) (stcb, net, &now); 2394f79aab18SRandall Stewart } 2395f79aab18SRandall Stewart /* 2396f79aab18SRandall Stewart * Do we need to determine the lan? We do this only on sacks i.e. 2397f79aab18SRandall Stewart * RTT being determined from data not non-data (HB/INIT->INITACK). 2398f79aab18SRandall Stewart */ 2399f79aab18SRandall Stewart if ((rtt_from_sack == SCTP_RTT_FROM_DATA) && 2400be1d9176SMichael Tuexen (net->lan_type == SCTP_LAN_UNKNOWN)) { 2401be1d9176SMichael Tuexen if (net->rtt > SCTP_LOCAL_LAN_RTT) { 2402899288aeSRandall Stewart net->lan_type = SCTP_LAN_INTERNET; 2403899288aeSRandall Stewart } else { 2404899288aeSRandall Stewart net->lan_type = SCTP_LAN_LOCAL; 2405899288aeSRandall Stewart } 2406899288aeSRandall Stewart } 2407f8829a4aSRandall Stewart /***************************/ 2408f8829a4aSRandall Stewart /* 2. update RTTVAR & SRTT */ 2409f8829a4aSRandall Stewart /***************************/ 2410be1d9176SMichael Tuexen /*- 2411be1d9176SMichael Tuexen * Compute the scaled average lastsa and the 2412be1d9176SMichael Tuexen * scaled variance lastsv as described in van Jacobson 2413be1d9176SMichael Tuexen * Paper "Congestion Avoidance and Control", Annex A. 2414be1d9176SMichael Tuexen * 2415be1d9176SMichael Tuexen * (net->lastsa >> SCTP_RTT_SHIFT) is the srtt 2416be1d9176SMichael Tuexen * (net->lastsa >> SCTP_RTT_VAR_SHIFT) is the rttvar 2417be1d9176SMichael Tuexen */ 24189a972525SRandall Stewart if (net->RTO_measured) { 2419be1d9176SMichael Tuexen rtt -= (net->lastsa >> SCTP_RTT_SHIFT); 2420be1d9176SMichael Tuexen net->lastsa += rtt; 2421be1d9176SMichael Tuexen if (rtt < 0) { 2422be1d9176SMichael Tuexen rtt = -rtt; 2423be1d9176SMichael Tuexen } 2424be1d9176SMichael Tuexen rtt -= (net->lastsv >> SCTP_RTT_VAR_SHIFT); 2425be1d9176SMichael Tuexen net->lastsv += rtt; 2426b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) { 2427f8829a4aSRandall Stewart rto_logging(net, SCTP_LOG_RTTVAR); 242880fefe0aSRandall Stewart } 2429f8829a4aSRandall Stewart } else { 2430f8829a4aSRandall Stewart /* First RTO measurment */ 24319a972525SRandall Stewart net->RTO_measured = 1; 2432f8829a4aSRandall Stewart first_measure = 1; 2433be1d9176SMichael Tuexen net->lastsa = rtt << SCTP_RTT_SHIFT; 2434be1d9176SMichael Tuexen net->lastsv = (rtt / 2) << SCTP_RTT_VAR_SHIFT; 2435b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) { 2436f8829a4aSRandall Stewart rto_logging(net, SCTP_LOG_INITIAL_RTT); 243780fefe0aSRandall Stewart } 2438f8829a4aSRandall Stewart } 2439be1d9176SMichael Tuexen if (net->lastsv == 0) { 2440be1d9176SMichael Tuexen net->lastsv = SCTP_CLOCK_GRANULARITY; 2441be1d9176SMichael Tuexen } 2442108df27cSRandall Stewart new_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv; 2443f8829a4aSRandall Stewart if ((new_rto > SCTP_SAT_NETWORK_MIN) && 2444f8829a4aSRandall Stewart (stcb->asoc.sat_network_lockout == 0)) { 2445f8829a4aSRandall Stewart stcb->asoc.sat_network = 1; 2446f8829a4aSRandall Stewart } else if ((!first_measure) && stcb->asoc.sat_network) { 2447f8829a4aSRandall Stewart stcb->asoc.sat_network = 0; 2448f8829a4aSRandall Stewart stcb->asoc.sat_network_lockout = 1; 2449f8829a4aSRandall Stewart } 2450f8829a4aSRandall Stewart /* bound it, per C6/C7 in Section 5.3.1 */ 2451f8829a4aSRandall Stewart if (new_rto < stcb->asoc.minrto) { 2452f8829a4aSRandall Stewart new_rto = stcb->asoc.minrto; 2453f8829a4aSRandall Stewart } 2454f8829a4aSRandall Stewart if (new_rto > stcb->asoc.maxrto) { 2455f8829a4aSRandall Stewart new_rto = stcb->asoc.maxrto; 2456f8829a4aSRandall Stewart } 24575e54f665SRandall Stewart /* we are now returning the RTO */ 24585e54f665SRandall Stewart return (new_rto); 2459f8829a4aSRandall Stewart } 2460f8829a4aSRandall Stewart 2461f8829a4aSRandall Stewart /* 2462f8829a4aSRandall Stewart * return a pointer to a contiguous piece of data from the given mbuf chain 2463f8829a4aSRandall Stewart * starting at 'off' for 'len' bytes. If the desired piece spans more than 2464f8829a4aSRandall Stewart * one mbuf, a copy is made at 'ptr'. caller must ensure that the buffer size 2465f8829a4aSRandall Stewart * is >= 'len' returns NULL if there there isn't 'len' bytes in the chain. 2466f8829a4aSRandall Stewart */ 246772fb6fdbSRandall Stewart caddr_t 2468f8829a4aSRandall Stewart sctp_m_getptr(struct mbuf *m, int off, int len, uint8_t * in_ptr) 2469f8829a4aSRandall Stewart { 2470f8829a4aSRandall Stewart uint32_t count; 2471f8829a4aSRandall Stewart uint8_t *ptr; 2472f8829a4aSRandall Stewart 2473f8829a4aSRandall Stewart ptr = in_ptr; 2474f8829a4aSRandall Stewart if ((off < 0) || (len <= 0)) 2475f8829a4aSRandall Stewart return (NULL); 2476f8829a4aSRandall Stewart 2477f8829a4aSRandall Stewart /* find the desired start location */ 2478f8829a4aSRandall Stewart while ((m != NULL) && (off > 0)) { 2479139bc87fSRandall Stewart if (off < SCTP_BUF_LEN(m)) 2480f8829a4aSRandall Stewart break; 2481139bc87fSRandall Stewart off -= SCTP_BUF_LEN(m); 2482139bc87fSRandall Stewart m = SCTP_BUF_NEXT(m); 2483f8829a4aSRandall Stewart } 2484f8829a4aSRandall Stewart if (m == NULL) 2485f8829a4aSRandall Stewart return (NULL); 2486f8829a4aSRandall Stewart 2487f8829a4aSRandall Stewart /* is the current mbuf large enough (eg. contiguous)? */ 2488139bc87fSRandall Stewart if ((SCTP_BUF_LEN(m) - off) >= len) { 2489f8829a4aSRandall Stewart return (mtod(m, caddr_t)+off); 2490f8829a4aSRandall Stewart } else { 2491f8829a4aSRandall Stewart /* else, it spans more than one mbuf, so save a temp copy... */ 2492f8829a4aSRandall Stewart while ((m != NULL) && (len > 0)) { 2493139bc87fSRandall Stewart count = min(SCTP_BUF_LEN(m) - off, len); 2494f8829a4aSRandall Stewart bcopy(mtod(m, caddr_t)+off, ptr, count); 2495f8829a4aSRandall Stewart len -= count; 2496f8829a4aSRandall Stewart ptr += count; 2497f8829a4aSRandall Stewart off = 0; 2498139bc87fSRandall Stewart m = SCTP_BUF_NEXT(m); 2499f8829a4aSRandall Stewart } 2500f8829a4aSRandall Stewart if ((m == NULL) && (len > 0)) 2501f8829a4aSRandall Stewart return (NULL); 2502f8829a4aSRandall Stewart else 2503f8829a4aSRandall Stewart return ((caddr_t)in_ptr); 2504f8829a4aSRandall Stewart } 2505f8829a4aSRandall Stewart } 2506f8829a4aSRandall Stewart 2507f8829a4aSRandall Stewart 250844b7479bSRandall Stewart 2509f8829a4aSRandall Stewart struct sctp_paramhdr * 2510f8829a4aSRandall Stewart sctp_get_next_param(struct mbuf *m, 2511f8829a4aSRandall Stewart int offset, 2512f8829a4aSRandall Stewart struct sctp_paramhdr *pull, 2513f8829a4aSRandall Stewart int pull_limit) 2514f8829a4aSRandall Stewart { 2515f8829a4aSRandall Stewart /* This just provides a typed signature to Peter's Pull routine */ 2516f8829a4aSRandall Stewart return ((struct sctp_paramhdr *)sctp_m_getptr(m, offset, pull_limit, 2517f8829a4aSRandall Stewart (uint8_t *) pull)); 2518f8829a4aSRandall Stewart } 2519f8829a4aSRandall Stewart 2520f8829a4aSRandall Stewart 2521f8829a4aSRandall Stewart int 2522f8829a4aSRandall Stewart sctp_add_pad_tombuf(struct mbuf *m, int padlen) 2523f8829a4aSRandall Stewart { 2524f8829a4aSRandall Stewart /* 2525f8829a4aSRandall Stewart * add padlen bytes of 0 filled padding to the end of the mbuf. If 2526f8829a4aSRandall Stewart * padlen is > 3 this routine will fail. 2527f8829a4aSRandall Stewart */ 2528f8829a4aSRandall Stewart uint8_t *dp; 2529f8829a4aSRandall Stewart int i; 2530f8829a4aSRandall Stewart 2531f8829a4aSRandall Stewart if (padlen > 3) { 2532c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET_PKT(m, NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS); 2533f8829a4aSRandall Stewart return (ENOBUFS); 2534f8829a4aSRandall Stewart } 253541eee555SRandall Stewart if (padlen <= M_TRAILINGSPACE(m)) { 2536f8829a4aSRandall Stewart /* 2537f8829a4aSRandall Stewart * The easy way. We hope the majority of the time we hit 2538f8829a4aSRandall Stewart * here :) 2539f8829a4aSRandall Stewart */ 2540139bc87fSRandall Stewart dp = (uint8_t *) (mtod(m, caddr_t)+SCTP_BUF_LEN(m)); 2541139bc87fSRandall Stewart SCTP_BUF_LEN(m) += padlen; 2542f8829a4aSRandall Stewart } else { 2543f8829a4aSRandall Stewart /* Hard way we must grow the mbuf */ 2544f8829a4aSRandall Stewart struct mbuf *tmp; 2545f8829a4aSRandall Stewart 2546f8829a4aSRandall Stewart tmp = sctp_get_mbuf_for_msg(padlen, 0, M_DONTWAIT, 1, MT_DATA); 2547f8829a4aSRandall Stewart if (tmp == NULL) { 2548f8829a4aSRandall Stewart /* Out of space GAK! we are in big trouble. */ 2549953b6058SMichael Tuexen SCTP_LTRACE_ERR_RET_PKT(m, NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS); 2550953b6058SMichael Tuexen return (ENOBUFS); 2551f8829a4aSRandall Stewart } 2552f8829a4aSRandall Stewart /* setup and insert in middle */ 2553139bc87fSRandall Stewart SCTP_BUF_LEN(tmp) = padlen; 255441eee555SRandall Stewart SCTP_BUF_NEXT(tmp) = NULL; 2555139bc87fSRandall Stewart SCTP_BUF_NEXT(m) = tmp; 2556f8829a4aSRandall Stewart dp = mtod(tmp, uint8_t *); 2557f8829a4aSRandall Stewart } 2558f8829a4aSRandall Stewart /* zero out the pad */ 2559f8829a4aSRandall Stewart for (i = 0; i < padlen; i++) { 2560f8829a4aSRandall Stewart *dp = 0; 2561f8829a4aSRandall Stewart dp++; 2562f8829a4aSRandall Stewart } 2563f8829a4aSRandall Stewart return (0); 2564f8829a4aSRandall Stewart } 2565f8829a4aSRandall Stewart 2566f8829a4aSRandall Stewart int 2567f8829a4aSRandall Stewart sctp_pad_lastmbuf(struct mbuf *m, int padval, struct mbuf *last_mbuf) 2568f8829a4aSRandall Stewart { 2569f8829a4aSRandall Stewart /* find the last mbuf in chain and pad it */ 2570f8829a4aSRandall Stewart struct mbuf *m_at; 2571f8829a4aSRandall Stewart 2572f8829a4aSRandall Stewart if (last_mbuf) { 2573f8829a4aSRandall Stewart return (sctp_add_pad_tombuf(last_mbuf, padval)); 2574f8829a4aSRandall Stewart } else { 257517267b32SMichael Tuexen for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) { 2576139bc87fSRandall Stewart if (SCTP_BUF_NEXT(m_at) == NULL) { 2577f8829a4aSRandall Stewart return (sctp_add_pad_tombuf(m_at, padval)); 2578f8829a4aSRandall Stewart } 2579f8829a4aSRandall Stewart } 2580f8829a4aSRandall Stewart } 2581c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET_PKT(m, NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT); 2582f8829a4aSRandall Stewart return (EFAULT); 2583f8829a4aSRandall Stewart } 2584f8829a4aSRandall Stewart 2585f8829a4aSRandall Stewart static void 2586c5b5675dSMichael Tuexen sctp_notify_assoc_change(uint16_t state, struct sctp_tcb *stcb, 2587410a3b1eSMichael Tuexen uint16_t error, struct sctp_abort_chunk *abort, uint8_t from_peer, int so_locked 2588ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 2589ceaad40aSRandall Stewart SCTP_UNUSED 2590ceaad40aSRandall Stewart #endif 2591ceaad40aSRandall Stewart ) 2592f8829a4aSRandall Stewart { 2593f8829a4aSRandall Stewart struct mbuf *m_notify; 2594f8829a4aSRandall Stewart struct sctp_assoc_change *sac; 2595f8829a4aSRandall Stewart struct sctp_queued_to_read *control; 2596a2b42326SMichael Tuexen size_t notif_len, abort_len; 2597e06b67c7SMichael Tuexen unsigned int i; 2598f8829a4aSRandall Stewart 2599ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2600ceaad40aSRandall Stewart struct socket *so; 2601ceaad40aSRandall Stewart 2602ceaad40aSRandall Stewart #endif 2603ceaad40aSRandall Stewart 260458411b08SMichael Tuexen if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT)) { 2605a2b42326SMichael Tuexen notif_len = sizeof(struct sctp_assoc_change); 2606a2b42326SMichael Tuexen if (abort != NULL) { 2607a2b42326SMichael Tuexen abort_len = htons(abort->ch.chunk_length); 2608a2b42326SMichael Tuexen } else { 2609a2b42326SMichael Tuexen abort_len = 0; 2610c5b5675dSMichael Tuexen } 2611a2b42326SMichael Tuexen if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) { 2612a2b42326SMichael Tuexen notif_len += SCTP_ASSOC_SUPPORTS_MAX; 2613a2b42326SMichael Tuexen } else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) { 2614a2b42326SMichael Tuexen notif_len += abort_len; 2615a2b42326SMichael Tuexen } 2616a2b42326SMichael Tuexen m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_DONTWAIT, 1, MT_DATA); 2617a2b42326SMichael Tuexen if (m_notify == NULL) { 2618a2b42326SMichael Tuexen /* Retry with smaller value. */ 2619a2b42326SMichael Tuexen notif_len = sizeof(struct sctp_assoc_change); 2620a2b42326SMichael Tuexen m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_DONTWAIT, 1, MT_DATA); 2621a2b42326SMichael Tuexen if (m_notify == NULL) { 262258411b08SMichael Tuexen goto set_error; 2623a2b42326SMichael Tuexen } 2624a2b42326SMichael Tuexen } 2625a2b42326SMichael Tuexen SCTP_BUF_NEXT(m_notify) = NULL; 2626f8829a4aSRandall Stewart sac = mtod(m_notify, struct sctp_assoc_change *); 2627f8829a4aSRandall Stewart sac->sac_type = SCTP_ASSOC_CHANGE; 2628f8829a4aSRandall Stewart sac->sac_flags = 0; 2629f8829a4aSRandall Stewart sac->sac_length = sizeof(struct sctp_assoc_change); 2630c5b5675dSMichael Tuexen sac->sac_state = state; 2631f8829a4aSRandall Stewart sac->sac_error = error; 2632f8829a4aSRandall Stewart /* XXX verify these stream counts */ 2633f8829a4aSRandall Stewart sac->sac_outbound_streams = stcb->asoc.streamoutcnt; 2634f8829a4aSRandall Stewart sac->sac_inbound_streams = stcb->asoc.streamincnt; 2635f8829a4aSRandall Stewart sac->sac_assoc_id = sctp_get_associd(stcb); 2636a2b42326SMichael Tuexen if (notif_len > sizeof(struct sctp_assoc_change)) { 2637c5b5675dSMichael Tuexen if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) { 2638e06b67c7SMichael Tuexen i = 0; 2639e06b67c7SMichael Tuexen if (stcb->asoc.peer_supports_prsctp) { 2640e06b67c7SMichael Tuexen sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_PR; 2641e06b67c7SMichael Tuexen } 2642e06b67c7SMichael Tuexen if (stcb->asoc.peer_supports_auth) { 2643e06b67c7SMichael Tuexen sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_AUTH; 2644e06b67c7SMichael Tuexen } 2645e06b67c7SMichael Tuexen if (stcb->asoc.peer_supports_asconf) { 2646e06b67c7SMichael Tuexen sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_ASCONF; 2647e06b67c7SMichael Tuexen } 2648e06b67c7SMichael Tuexen sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_MULTIBUF; 2649e06b67c7SMichael Tuexen if (stcb->asoc.peer_supports_strreset) { 2650e06b67c7SMichael Tuexen sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_RE_CONFIG; 2651e06b67c7SMichael Tuexen } 2652e06b67c7SMichael Tuexen sac->sac_length += i; 2653a2b42326SMichael Tuexen } else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) { 2654a2b42326SMichael Tuexen memcpy(sac->sac_info, abort, abort_len); 2655a2b42326SMichael Tuexen sac->sac_length += abort_len; 2656a2b42326SMichael Tuexen } 2657c5b5675dSMichael Tuexen } 2658e06b67c7SMichael Tuexen SCTP_BUF_LEN(m_notify) = sac->sac_length; 2659f8829a4aSRandall Stewart control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, 26607215cc1bSMichael Tuexen 0, 0, stcb->asoc.context, 0, 0, 0, 2661f8829a4aSRandall Stewart m_notify); 266258411b08SMichael Tuexen if (control != NULL) { 2663139bc87fSRandall Stewart control->length = SCTP_BUF_LEN(m_notify); 2664f8829a4aSRandall Stewart /* not that we need this */ 2665f8829a4aSRandall Stewart control->tail_mbuf = m_notify; 2666139bc87fSRandall Stewart control->spec_flags = M_NOTIFICATION; 2667f8829a4aSRandall Stewart sctp_add_to_readq(stcb->sctp_ep, stcb, 2668f8829a4aSRandall Stewart control, 2669cfde3ff7SRandall Stewart &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, 2670cfde3ff7SRandall Stewart so_locked); 267158411b08SMichael Tuexen } else { 267258411b08SMichael Tuexen sctp_m_freem(m_notify); 267358411b08SMichael Tuexen } 267458411b08SMichael Tuexen } 267558411b08SMichael Tuexen /* 267658411b08SMichael Tuexen * For 1-to-1 style sockets, we send up and error when an ABORT 267758411b08SMichael Tuexen * comes in. 267858411b08SMichael Tuexen */ 267958411b08SMichael Tuexen set_error: 268058411b08SMichael Tuexen if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 268158411b08SMichael Tuexen (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 268258411b08SMichael Tuexen ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) { 2683410a3b1eSMichael Tuexen if (from_peer) { 268458411b08SMichael Tuexen if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) { 268558411b08SMichael Tuexen SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNREFUSED); 268658411b08SMichael Tuexen stcb->sctp_socket->so_error = ECONNREFUSED; 268758411b08SMichael Tuexen } else { 268858411b08SMichael Tuexen SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET); 268958411b08SMichael Tuexen stcb->sctp_socket->so_error = ECONNRESET; 269058411b08SMichael Tuexen } 2691410a3b1eSMichael Tuexen } else { 2692410a3b1eSMichael Tuexen SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNABORTED); 2693410a3b1eSMichael Tuexen stcb->sctp_socket->so_error = ECONNABORTED; 2694410a3b1eSMichael Tuexen } 269558411b08SMichael Tuexen } 269658411b08SMichael Tuexen /* Wake ANY sleepers */ 2697ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2698ceaad40aSRandall Stewart so = SCTP_INP_SO(stcb->sctp_ep); 2699ceaad40aSRandall Stewart if (!so_locked) { 2700ceaad40aSRandall Stewart atomic_add_int(&stcb->asoc.refcnt, 1); 2701ceaad40aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 2702ceaad40aSRandall Stewart SCTP_SOCKET_LOCK(so, 1); 2703ceaad40aSRandall Stewart SCTP_TCB_LOCK(stcb); 2704ceaad40aSRandall Stewart atomic_subtract_int(&stcb->asoc.refcnt, 1); 2705ceaad40aSRandall Stewart if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 2706ceaad40aSRandall Stewart SCTP_SOCKET_UNLOCK(so, 1); 2707ceaad40aSRandall Stewart return; 2708ceaad40aSRandall Stewart } 2709ceaad40aSRandall Stewart } 2710ceaad40aSRandall Stewart #endif 271158411b08SMichael Tuexen if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 271258411b08SMichael Tuexen (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) && 271358411b08SMichael Tuexen ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) { 271458411b08SMichael Tuexen socantrcvmore(stcb->sctp_socket); 271558411b08SMichael Tuexen } 271658411b08SMichael Tuexen sorwakeup(stcb->sctp_socket); 271758411b08SMichael Tuexen sowwakeup(stcb->sctp_socket); 2718ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 2719ceaad40aSRandall Stewart if (!so_locked) { 2720ceaad40aSRandall Stewart SCTP_SOCKET_UNLOCK(so, 1); 2721ceaad40aSRandall Stewart } 2722ceaad40aSRandall Stewart #endif 2723f8829a4aSRandall Stewart } 2724f8829a4aSRandall Stewart 2725f8829a4aSRandall Stewart static void 2726f8829a4aSRandall Stewart sctp_notify_peer_addr_change(struct sctp_tcb *stcb, uint32_t state, 2727f8829a4aSRandall Stewart struct sockaddr *sa, uint32_t error) 2728f8829a4aSRandall Stewart { 2729f8829a4aSRandall Stewart struct mbuf *m_notify; 2730f8829a4aSRandall Stewart struct sctp_paddr_change *spc; 2731f8829a4aSRandall Stewart struct sctp_queued_to_read *control; 2732f8829a4aSRandall Stewart 273360990c0cSMichael Tuexen if ((stcb == NULL) || 273460990c0cSMichael Tuexen sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT)) { 2735f8829a4aSRandall Stewart /* event not enabled */ 2736f8829a4aSRandall Stewart return; 2737830d754dSRandall Stewart } 2738139bc87fSRandall Stewart m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_paddr_change), 0, M_DONTWAIT, 1, MT_DATA); 2739f8829a4aSRandall Stewart if (m_notify == NULL) 2740f8829a4aSRandall Stewart return; 2741139bc87fSRandall Stewart SCTP_BUF_LEN(m_notify) = 0; 2742f8829a4aSRandall Stewart spc = mtod(m_notify, struct sctp_paddr_change *); 2743f8829a4aSRandall Stewart spc->spc_type = SCTP_PEER_ADDR_CHANGE; 2744f8829a4aSRandall Stewart spc->spc_flags = 0; 2745f8829a4aSRandall Stewart spc->spc_length = sizeof(struct sctp_paddr_change); 27465e2c2d87SRandall Stewart switch (sa->sa_family) { 2747ea5eba11SMichael Tuexen #ifdef INET 27485e2c2d87SRandall Stewart case AF_INET: 2749f8829a4aSRandall Stewart memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in)); 27505e2c2d87SRandall Stewart break; 2751ea5eba11SMichael Tuexen #endif 27525e2c2d87SRandall Stewart #ifdef INET6 27535e2c2d87SRandall Stewart case AF_INET6: 27545e2c2d87SRandall Stewart { 2755f42a358aSRandall Stewart struct sockaddr_in6 *sin6; 2756f42a358aSRandall Stewart 2757f8829a4aSRandall Stewart memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in6)); 2758f42a358aSRandall Stewart 2759f42a358aSRandall Stewart sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr; 2760f42a358aSRandall Stewart if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) { 276142551e99SRandall Stewart if (sin6->sin6_scope_id == 0) { 276242551e99SRandall Stewart /* recover scope_id for user */ 2763f42a358aSRandall Stewart (void)sa6_recoverscope(sin6); 276442551e99SRandall Stewart } else { 276542551e99SRandall Stewart /* clear embedded scope_id for user */ 276642551e99SRandall Stewart in6_clearscope(&sin6->sin6_addr); 276742551e99SRandall Stewart } 2768f42a358aSRandall Stewart } 27695e2c2d87SRandall Stewart break; 27705e2c2d87SRandall Stewart } 27715e2c2d87SRandall Stewart #endif 27725e2c2d87SRandall Stewart default: 27735e2c2d87SRandall Stewart /* TSNH */ 27745e2c2d87SRandall Stewart break; 2775f8829a4aSRandall Stewart } 2776f8829a4aSRandall Stewart spc->spc_state = state; 2777f8829a4aSRandall Stewart spc->spc_error = error; 2778f8829a4aSRandall Stewart spc->spc_assoc_id = sctp_get_associd(stcb); 2779f8829a4aSRandall Stewart 2780139bc87fSRandall Stewart SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_paddr_change); 2781139bc87fSRandall Stewart SCTP_BUF_NEXT(m_notify) = NULL; 2782f8829a4aSRandall Stewart 2783f8829a4aSRandall Stewart /* append to socket */ 2784f8829a4aSRandall Stewart control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, 27857215cc1bSMichael Tuexen 0, 0, stcb->asoc.context, 0, 0, 0, 2786f8829a4aSRandall Stewart m_notify); 2787f8829a4aSRandall Stewart if (control == NULL) { 2788f8829a4aSRandall Stewart /* no memory */ 2789f8829a4aSRandall Stewart sctp_m_freem(m_notify); 2790f8829a4aSRandall Stewart return; 2791f8829a4aSRandall Stewart } 2792139bc87fSRandall Stewart control->length = SCTP_BUF_LEN(m_notify); 2793139bc87fSRandall Stewart control->spec_flags = M_NOTIFICATION; 2794f8829a4aSRandall Stewart /* not that we need this */ 2795f8829a4aSRandall Stewart control->tail_mbuf = m_notify; 2796f8829a4aSRandall Stewart sctp_add_to_readq(stcb->sctp_ep, stcb, 2797f8829a4aSRandall Stewart control, 2798cfde3ff7SRandall Stewart &stcb->sctp_socket->so_rcv, 1, 2799cfde3ff7SRandall Stewart SCTP_READ_LOCK_NOT_HELD, 2800cfde3ff7SRandall Stewart SCTP_SO_NOT_LOCKED); 2801f8829a4aSRandall Stewart } 2802f8829a4aSRandall Stewart 2803f8829a4aSRandall Stewart 2804f8829a4aSRandall Stewart static void 28051edc9dbaSMichael Tuexen sctp_notify_send_failed(struct sctp_tcb *stcb, uint8_t sent, uint32_t error, 2806ceaad40aSRandall Stewart struct sctp_tmit_chunk *chk, int so_locked 2807ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 2808ceaad40aSRandall Stewart SCTP_UNUSED 2809ceaad40aSRandall Stewart #endif 2810ceaad40aSRandall Stewart ) 2811f8829a4aSRandall Stewart { 2812830d754dSRandall Stewart struct mbuf *m_notify; 2813f8829a4aSRandall Stewart struct sctp_send_failed *ssf; 28149935403aSMichael Tuexen struct sctp_send_failed_event *ssfe; 2815f8829a4aSRandall Stewart struct sctp_queued_to_read *control; 2816f8829a4aSRandall Stewart int length; 2817f8829a4aSRandall Stewart 281860990c0cSMichael Tuexen if ((stcb == NULL) || 28199935403aSMichael Tuexen (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) && 28209935403aSMichael Tuexen sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) { 2821f8829a4aSRandall Stewart /* event not enabled */ 2822f8829a4aSRandall Stewart return; 2823830d754dSRandall Stewart } 28249935403aSMichael Tuexen if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) { 28259935403aSMichael Tuexen length = sizeof(struct sctp_send_failed_event); 28269935403aSMichael Tuexen } else { 28279935403aSMichael Tuexen length = sizeof(struct sctp_send_failed); 28289935403aSMichael Tuexen } 28299935403aSMichael Tuexen m_notify = sctp_get_mbuf_for_msg(length, 0, M_DONTWAIT, 1, MT_DATA); 2830f8829a4aSRandall Stewart if (m_notify == NULL) 2831f8829a4aSRandall Stewart /* no space left */ 2832f8829a4aSRandall Stewart return; 28339935403aSMichael Tuexen length += chk->send_size; 2834fc14de76SRandall Stewart length -= sizeof(struct sctp_data_chunk); 2835139bc87fSRandall Stewart SCTP_BUF_LEN(m_notify) = 0; 28369935403aSMichael Tuexen if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) { 28379935403aSMichael Tuexen ssfe = mtod(m_notify, struct sctp_send_failed_event *); 28389935403aSMichael Tuexen ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT; 28391edc9dbaSMichael Tuexen if (sent) { 28409935403aSMichael Tuexen ssfe->ssfe_flags = SCTP_DATA_SENT; 28411edc9dbaSMichael Tuexen } else { 28421edc9dbaSMichael Tuexen ssfe->ssfe_flags = SCTP_DATA_UNSENT; 28431edc9dbaSMichael Tuexen } 28449935403aSMichael Tuexen ssfe->ssfe_length = length; 28459935403aSMichael Tuexen ssfe->ssfe_error = error; 28469935403aSMichael Tuexen /* not exactly what the user sent in, but should be close :) */ 28479935403aSMichael Tuexen bzero(&ssfe->ssfe_info, sizeof(ssfe->ssfe_info)); 28489935403aSMichael Tuexen ssfe->ssfe_info.snd_sid = chk->rec.data.stream_number; 28499935403aSMichael Tuexen ssfe->ssfe_info.snd_flags = chk->rec.data.rcv_flags; 28509935403aSMichael Tuexen ssfe->ssfe_info.snd_ppid = chk->rec.data.payloadtype; 28519935403aSMichael Tuexen ssfe->ssfe_info.snd_context = chk->rec.data.context; 28529935403aSMichael Tuexen ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb); 28539935403aSMichael Tuexen ssfe->ssfe_assoc_id = sctp_get_associd(stcb); 28549935403aSMichael Tuexen SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed_event); 28559935403aSMichael Tuexen } else { 2856f8829a4aSRandall Stewart ssf = mtod(m_notify, struct sctp_send_failed *); 2857f8829a4aSRandall Stewart ssf->ssf_type = SCTP_SEND_FAILED; 28581edc9dbaSMichael Tuexen if (sent) { 2859f8829a4aSRandall Stewart ssf->ssf_flags = SCTP_DATA_SENT; 28601edc9dbaSMichael Tuexen } else { 28611edc9dbaSMichael Tuexen ssf->ssf_flags = SCTP_DATA_UNSENT; 28621edc9dbaSMichael Tuexen } 2863f8829a4aSRandall Stewart ssf->ssf_length = length; 2864f8829a4aSRandall Stewart ssf->ssf_error = error; 2865f8829a4aSRandall Stewart /* not exactly what the user sent in, but should be close :) */ 2866d00aff5dSRandall Stewart bzero(&ssf->ssf_info, sizeof(ssf->ssf_info)); 2867f8829a4aSRandall Stewart ssf->ssf_info.sinfo_stream = chk->rec.data.stream_number; 2868f8829a4aSRandall Stewart ssf->ssf_info.sinfo_ssn = chk->rec.data.stream_seq; 2869f8829a4aSRandall Stewart ssf->ssf_info.sinfo_flags = chk->rec.data.rcv_flags; 2870f8829a4aSRandall Stewart ssf->ssf_info.sinfo_ppid = chk->rec.data.payloadtype; 2871f8829a4aSRandall Stewart ssf->ssf_info.sinfo_context = chk->rec.data.context; 2872f8829a4aSRandall Stewart ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb); 2873f8829a4aSRandall Stewart ssf->ssf_assoc_id = sctp_get_associd(stcb); 28749935403aSMichael Tuexen SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed); 28759935403aSMichael Tuexen } 2876830d754dSRandall Stewart if (chk->data) { 2877830d754dSRandall Stewart /* 2878830d754dSRandall Stewart * trim off the sctp chunk header(it should be there) 2879830d754dSRandall Stewart */ 2880830d754dSRandall Stewart if (chk->send_size >= sizeof(struct sctp_data_chunk)) { 2881830d754dSRandall Stewart m_adj(chk->data, sizeof(struct sctp_data_chunk)); 2882830d754dSRandall Stewart sctp_mbuf_crush(chk->data); 2883830d754dSRandall Stewart chk->send_size -= sizeof(struct sctp_data_chunk); 2884830d754dSRandall Stewart } 2885830d754dSRandall Stewart } 2886810ec536SMichael Tuexen SCTP_BUF_NEXT(m_notify) = chk->data; 2887f8829a4aSRandall Stewart /* Steal off the mbuf */ 2888f8829a4aSRandall Stewart chk->data = NULL; 2889f8829a4aSRandall Stewart /* 2890f8829a4aSRandall Stewart * For this case, we check the actual socket buffer, since the assoc 2891f8829a4aSRandall Stewart * is going away we don't want to overfill the socket buffer for a 2892f8829a4aSRandall Stewart * non-reader 2893f8829a4aSRandall Stewart */ 2894139bc87fSRandall Stewart if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) { 2895f8829a4aSRandall Stewart sctp_m_freem(m_notify); 2896f8829a4aSRandall Stewart return; 2897f8829a4aSRandall Stewart } 2898f8829a4aSRandall Stewart /* append to socket */ 2899f8829a4aSRandall Stewart control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, 29007215cc1bSMichael Tuexen 0, 0, stcb->asoc.context, 0, 0, 0, 2901f8829a4aSRandall Stewart m_notify); 2902f8829a4aSRandall Stewart if (control == NULL) { 2903f8829a4aSRandall Stewart /* no memory */ 2904f8829a4aSRandall Stewart sctp_m_freem(m_notify); 2905f8829a4aSRandall Stewart return; 2906f8829a4aSRandall Stewart } 2907139bc87fSRandall Stewart control->spec_flags = M_NOTIFICATION; 2908f8829a4aSRandall Stewart sctp_add_to_readq(stcb->sctp_ep, stcb, 2909f8829a4aSRandall Stewart control, 2910cfde3ff7SRandall Stewart &stcb->sctp_socket->so_rcv, 1, 2911cfde3ff7SRandall Stewart SCTP_READ_LOCK_NOT_HELD, 2912cfde3ff7SRandall Stewart so_locked); 2913f8829a4aSRandall Stewart } 2914f8829a4aSRandall Stewart 2915f8829a4aSRandall Stewart 2916f8829a4aSRandall Stewart static void 2917f8829a4aSRandall Stewart sctp_notify_send_failed2(struct sctp_tcb *stcb, uint32_t error, 2918ceaad40aSRandall Stewart struct sctp_stream_queue_pending *sp, int so_locked 2919ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 2920ceaad40aSRandall Stewart SCTP_UNUSED 2921ceaad40aSRandall Stewart #endif 2922ceaad40aSRandall Stewart ) 2923f8829a4aSRandall Stewart { 2924f8829a4aSRandall Stewart struct mbuf *m_notify; 2925f8829a4aSRandall Stewart struct sctp_send_failed *ssf; 29269935403aSMichael Tuexen struct sctp_send_failed_event *ssfe; 2927f8829a4aSRandall Stewart struct sctp_queued_to_read *control; 2928f8829a4aSRandall Stewart int length; 2929f8829a4aSRandall Stewart 293060990c0cSMichael Tuexen if ((stcb == NULL) || 29319935403aSMichael Tuexen (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) && 29329935403aSMichael Tuexen sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) { 2933f8829a4aSRandall Stewart /* event not enabled */ 2934f8829a4aSRandall Stewart return; 2935830d754dSRandall Stewart } 29369935403aSMichael Tuexen if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) { 29379935403aSMichael Tuexen length = sizeof(struct sctp_send_failed_event); 29389935403aSMichael Tuexen } else { 29399935403aSMichael Tuexen length = sizeof(struct sctp_send_failed); 29409935403aSMichael Tuexen } 29419935403aSMichael Tuexen m_notify = sctp_get_mbuf_for_msg(length, 0, M_DONTWAIT, 1, MT_DATA); 29429935403aSMichael Tuexen if (m_notify == NULL) { 2943f8829a4aSRandall Stewart /* no space left */ 2944f8829a4aSRandall Stewart return; 29459935403aSMichael Tuexen } 29469935403aSMichael Tuexen length += sp->length; 2947139bc87fSRandall Stewart SCTP_BUF_LEN(m_notify) = 0; 29489935403aSMichael Tuexen if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) { 29499935403aSMichael Tuexen ssfe = mtod(m_notify, struct sctp_send_failed_event *); 2950ad83c8a5SMichael Tuexen ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT; 29519935403aSMichael Tuexen ssfe->ssfe_flags = SCTP_DATA_UNSENT; 29529935403aSMichael Tuexen ssfe->ssfe_length = length; 29539935403aSMichael Tuexen ssfe->ssfe_error = error; 29549935403aSMichael Tuexen /* not exactly what the user sent in, but should be close :) */ 29559935403aSMichael Tuexen bzero(&ssfe->ssfe_info, sizeof(ssfe->ssfe_info)); 29569935403aSMichael Tuexen ssfe->ssfe_info.snd_sid = sp->stream; 29579935403aSMichael Tuexen if (sp->some_taken) { 29589935403aSMichael Tuexen ssfe->ssfe_info.snd_flags = SCTP_DATA_LAST_FRAG; 29599935403aSMichael Tuexen } else { 29609935403aSMichael Tuexen ssfe->ssfe_info.snd_flags = SCTP_DATA_NOT_FRAG; 29619935403aSMichael Tuexen } 29629935403aSMichael Tuexen ssfe->ssfe_info.snd_ppid = sp->ppid; 29639935403aSMichael Tuexen ssfe->ssfe_info.snd_context = sp->context; 29649935403aSMichael Tuexen ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb); 29659935403aSMichael Tuexen ssfe->ssfe_assoc_id = sctp_get_associd(stcb); 29669935403aSMichael Tuexen SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed_event); 29679935403aSMichael Tuexen } else { 2968f8829a4aSRandall Stewart ssf = mtod(m_notify, struct sctp_send_failed *); 2969f8829a4aSRandall Stewart ssf->ssf_type = SCTP_SEND_FAILED; 2970f8829a4aSRandall Stewart ssf->ssf_flags = SCTP_DATA_UNSENT; 2971f8829a4aSRandall Stewart ssf->ssf_length = length; 2972f8829a4aSRandall Stewart ssf->ssf_error = error; 2973f8829a4aSRandall Stewart /* not exactly what the user sent in, but should be close :) */ 2974d00aff5dSRandall Stewart bzero(&ssf->ssf_info, sizeof(ssf->ssf_info)); 2975f8829a4aSRandall Stewart ssf->ssf_info.sinfo_stream = sp->stream; 2976f3b05218SMichael Tuexen ssf->ssf_info.sinfo_ssn = 0; 2977fc14de76SRandall Stewart if (sp->some_taken) { 2978fc14de76SRandall Stewart ssf->ssf_info.sinfo_flags = SCTP_DATA_LAST_FRAG; 2979fc14de76SRandall Stewart } else { 2980fc14de76SRandall Stewart ssf->ssf_info.sinfo_flags = SCTP_DATA_NOT_FRAG; 2981fc14de76SRandall Stewart } 2982f8829a4aSRandall Stewart ssf->ssf_info.sinfo_ppid = sp->ppid; 2983f8829a4aSRandall Stewart ssf->ssf_info.sinfo_context = sp->context; 2984f8829a4aSRandall Stewart ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb); 2985f8829a4aSRandall Stewart ssf->ssf_assoc_id = sctp_get_associd(stcb); 2986139bc87fSRandall Stewart SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_send_failed); 29879935403aSMichael Tuexen } 29889935403aSMichael Tuexen SCTP_BUF_NEXT(m_notify) = sp->data; 2989f8829a4aSRandall Stewart 2990f8829a4aSRandall Stewart /* Steal off the mbuf */ 2991f8829a4aSRandall Stewart sp->data = NULL; 2992f8829a4aSRandall Stewart /* 2993f8829a4aSRandall Stewart * For this case, we check the actual socket buffer, since the assoc 2994f8829a4aSRandall Stewart * is going away we don't want to overfill the socket buffer for a 2995f8829a4aSRandall Stewart * non-reader 2996f8829a4aSRandall Stewart */ 2997139bc87fSRandall Stewart if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) { 2998f8829a4aSRandall Stewart sctp_m_freem(m_notify); 2999f8829a4aSRandall Stewart return; 3000f8829a4aSRandall Stewart } 3001f8829a4aSRandall Stewart /* append to socket */ 3002f8829a4aSRandall Stewart control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, 30037215cc1bSMichael Tuexen 0, 0, stcb->asoc.context, 0, 0, 0, 3004f8829a4aSRandall Stewart m_notify); 3005f8829a4aSRandall Stewart if (control == NULL) { 3006f8829a4aSRandall Stewart /* no memory */ 3007f8829a4aSRandall Stewart sctp_m_freem(m_notify); 3008f8829a4aSRandall Stewart return; 3009f8829a4aSRandall Stewart } 3010139bc87fSRandall Stewart control->spec_flags = M_NOTIFICATION; 3011f8829a4aSRandall Stewart sctp_add_to_readq(stcb->sctp_ep, stcb, 3012f8829a4aSRandall Stewart control, 3013cfde3ff7SRandall Stewart &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked); 3014f8829a4aSRandall Stewart } 3015f8829a4aSRandall Stewart 3016f8829a4aSRandall Stewart 3017f8829a4aSRandall Stewart 3018f8829a4aSRandall Stewart static void 30197215cc1bSMichael Tuexen sctp_notify_adaptation_layer(struct sctp_tcb *stcb) 3020f8829a4aSRandall Stewart { 3021f8829a4aSRandall Stewart struct mbuf *m_notify; 3022f8829a4aSRandall Stewart struct sctp_adaptation_event *sai; 3023f8829a4aSRandall Stewart struct sctp_queued_to_read *control; 3024f8829a4aSRandall Stewart 302560990c0cSMichael Tuexen if ((stcb == NULL) || 302660990c0cSMichael Tuexen sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT)) { 3027f8829a4aSRandall Stewart /* event not enabled */ 3028f8829a4aSRandall Stewart return; 3029830d754dSRandall Stewart } 3030139bc87fSRandall Stewart m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 0, M_DONTWAIT, 1, MT_DATA); 3031f8829a4aSRandall Stewart if (m_notify == NULL) 3032f8829a4aSRandall Stewart /* no space left */ 3033f8829a4aSRandall Stewart return; 3034139bc87fSRandall Stewart SCTP_BUF_LEN(m_notify) = 0; 3035f8829a4aSRandall Stewart sai = mtod(m_notify, struct sctp_adaptation_event *); 3036f8829a4aSRandall Stewart sai->sai_type = SCTP_ADAPTATION_INDICATION; 3037f8829a4aSRandall Stewart sai->sai_flags = 0; 3038f8829a4aSRandall Stewart sai->sai_length = sizeof(struct sctp_adaptation_event); 30392afb3e84SRandall Stewart sai->sai_adaptation_ind = stcb->asoc.peers_adaptation; 3040f8829a4aSRandall Stewart sai->sai_assoc_id = sctp_get_associd(stcb); 3041f8829a4aSRandall Stewart 3042139bc87fSRandall Stewart SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_adaptation_event); 3043139bc87fSRandall Stewart SCTP_BUF_NEXT(m_notify) = NULL; 3044f8829a4aSRandall Stewart 3045f8829a4aSRandall Stewart /* append to socket */ 3046f8829a4aSRandall Stewart control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, 30477215cc1bSMichael Tuexen 0, 0, stcb->asoc.context, 0, 0, 0, 3048f8829a4aSRandall Stewart m_notify); 3049f8829a4aSRandall Stewart if (control == NULL) { 3050f8829a4aSRandall Stewart /* no memory */ 3051f8829a4aSRandall Stewart sctp_m_freem(m_notify); 3052f8829a4aSRandall Stewart return; 3053f8829a4aSRandall Stewart } 3054139bc87fSRandall Stewart control->length = SCTP_BUF_LEN(m_notify); 3055139bc87fSRandall Stewart control->spec_flags = M_NOTIFICATION; 3056f8829a4aSRandall Stewart /* not that we need this */ 3057f8829a4aSRandall Stewart control->tail_mbuf = m_notify; 3058f8829a4aSRandall Stewart sctp_add_to_readq(stcb->sctp_ep, stcb, 3059f8829a4aSRandall Stewart control, 3060cfde3ff7SRandall Stewart &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED); 3061f8829a4aSRandall Stewart } 3062f8829a4aSRandall Stewart 306303b0b021SRandall Stewart /* This always must be called with the read-queue LOCKED in the INP */ 3064810ec536SMichael Tuexen static void 30652dad8a55SRandall Stewart sctp_notify_partial_delivery_indication(struct sctp_tcb *stcb, uint32_t error, 3066810ec536SMichael Tuexen uint32_t val, int so_locked 3067810ec536SMichael Tuexen #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 3068810ec536SMichael Tuexen SCTP_UNUSED 3069810ec536SMichael Tuexen #endif 3070810ec536SMichael Tuexen ) 3071f8829a4aSRandall Stewart { 3072f8829a4aSRandall Stewart struct mbuf *m_notify; 3073f8829a4aSRandall Stewart struct sctp_pdapi_event *pdapi; 3074f8829a4aSRandall Stewart struct sctp_queued_to_read *control; 307503b0b021SRandall Stewart struct sockbuf *sb; 3076f8829a4aSRandall Stewart 307760990c0cSMichael Tuexen if ((stcb == NULL) || 307860990c0cSMichael Tuexen sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_PDAPIEVNT)) { 3079f8829a4aSRandall Stewart /* event not enabled */ 3080f8829a4aSRandall Stewart return; 3081830d754dSRandall Stewart } 3082cd1386abSMichael Tuexen if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) { 3083cd1386abSMichael Tuexen return; 3084cd1386abSMichael Tuexen } 3085139bc87fSRandall Stewart m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_pdapi_event), 0, M_DONTWAIT, 1, MT_DATA); 3086f8829a4aSRandall Stewart if (m_notify == NULL) 3087f8829a4aSRandall Stewart /* no space left */ 3088f8829a4aSRandall Stewart return; 3089139bc87fSRandall Stewart SCTP_BUF_LEN(m_notify) = 0; 3090f8829a4aSRandall Stewart pdapi = mtod(m_notify, struct sctp_pdapi_event *); 3091f8829a4aSRandall Stewart pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT; 3092f8829a4aSRandall Stewart pdapi->pdapi_flags = 0; 3093f8829a4aSRandall Stewart pdapi->pdapi_length = sizeof(struct sctp_pdapi_event); 3094f8829a4aSRandall Stewart pdapi->pdapi_indication = error; 30959a6142d8SRandall Stewart pdapi->pdapi_stream = (val >> 16); 30969a6142d8SRandall Stewart pdapi->pdapi_seq = (val & 0x0000ffff); 3097f8829a4aSRandall Stewart pdapi->pdapi_assoc_id = sctp_get_associd(stcb); 3098f8829a4aSRandall Stewart 3099139bc87fSRandall Stewart SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_pdapi_event); 3100139bc87fSRandall Stewart SCTP_BUF_NEXT(m_notify) = NULL; 3101f8829a4aSRandall Stewart control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, 31027215cc1bSMichael Tuexen 0, 0, stcb->asoc.context, 0, 0, 0, 3103f8829a4aSRandall Stewart m_notify); 3104f8829a4aSRandall Stewart if (control == NULL) { 3105f8829a4aSRandall Stewart /* no memory */ 3106f8829a4aSRandall Stewart sctp_m_freem(m_notify); 3107f8829a4aSRandall Stewart return; 3108f8829a4aSRandall Stewart } 3109139bc87fSRandall Stewart control->spec_flags = M_NOTIFICATION; 3110139bc87fSRandall Stewart control->length = SCTP_BUF_LEN(m_notify); 3111f8829a4aSRandall Stewart /* not that we need this */ 3112f8829a4aSRandall Stewart control->tail_mbuf = m_notify; 311303b0b021SRandall Stewart control->held_length = 0; 311403b0b021SRandall Stewart control->length = 0; 311503b0b021SRandall Stewart sb = &stcb->sctp_socket->so_rcv; 3116b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { 3117139bc87fSRandall Stewart sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m_notify)); 311880fefe0aSRandall Stewart } 311903b0b021SRandall Stewart sctp_sballoc(stcb, sb, m_notify); 3120b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { 312103b0b021SRandall Stewart sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0); 312280fefe0aSRandall Stewart } 3123139bc87fSRandall Stewart atomic_add_int(&control->length, SCTP_BUF_LEN(m_notify)); 312403b0b021SRandall Stewart control->end_added = 1; 312503b0b021SRandall Stewart if (stcb->asoc.control_pdapi) 312603b0b021SRandall Stewart TAILQ_INSERT_AFTER(&stcb->sctp_ep->read_queue, stcb->asoc.control_pdapi, control, next); 312703b0b021SRandall Stewart else { 312803b0b021SRandall Stewart /* we really should not see this case */ 312903b0b021SRandall Stewart TAILQ_INSERT_TAIL(&stcb->sctp_ep->read_queue, control, next); 313003b0b021SRandall Stewart } 313103b0b021SRandall Stewart if (stcb->sctp_ep && stcb->sctp_socket) { 313203b0b021SRandall Stewart /* This should always be the case */ 3133810ec536SMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3134810ec536SMichael Tuexen struct socket *so; 3135810ec536SMichael Tuexen 3136810ec536SMichael Tuexen so = SCTP_INP_SO(stcb->sctp_ep); 3137810ec536SMichael Tuexen if (!so_locked) { 3138810ec536SMichael Tuexen atomic_add_int(&stcb->asoc.refcnt, 1); 3139810ec536SMichael Tuexen SCTP_TCB_UNLOCK(stcb); 3140810ec536SMichael Tuexen SCTP_SOCKET_LOCK(so, 1); 3141810ec536SMichael Tuexen SCTP_TCB_LOCK(stcb); 3142810ec536SMichael Tuexen atomic_subtract_int(&stcb->asoc.refcnt, 1); 3143810ec536SMichael Tuexen if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 3144810ec536SMichael Tuexen SCTP_SOCKET_UNLOCK(so, 1); 3145810ec536SMichael Tuexen return; 3146810ec536SMichael Tuexen } 3147810ec536SMichael Tuexen } 3148810ec536SMichael Tuexen #endif 314903b0b021SRandall Stewart sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 3150810ec536SMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3151810ec536SMichael Tuexen if (!so_locked) { 3152810ec536SMichael Tuexen SCTP_SOCKET_UNLOCK(so, 1); 3153810ec536SMichael Tuexen } 3154810ec536SMichael Tuexen #endif 3155f8829a4aSRandall Stewart } 3156f8829a4aSRandall Stewart } 3157f8829a4aSRandall Stewart 3158f8829a4aSRandall Stewart static void 3159f8829a4aSRandall Stewart sctp_notify_shutdown_event(struct sctp_tcb *stcb) 3160f8829a4aSRandall Stewart { 3161f8829a4aSRandall Stewart struct mbuf *m_notify; 3162f8829a4aSRandall Stewart struct sctp_shutdown_event *sse; 3163f8829a4aSRandall Stewart struct sctp_queued_to_read *control; 3164f8829a4aSRandall Stewart 3165f8829a4aSRandall Stewart /* 3166f8829a4aSRandall Stewart * For TCP model AND UDP connected sockets we will send an error up 3167f8829a4aSRandall Stewart * when an SHUTDOWN completes 3168f8829a4aSRandall Stewart */ 3169f8829a4aSRandall Stewart if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 3170f8829a4aSRandall Stewart (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 3171f8829a4aSRandall Stewart /* mark socket closed for read/write and wakeup! */ 3172ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3173ceaad40aSRandall Stewart struct socket *so; 3174ceaad40aSRandall Stewart 3175ceaad40aSRandall Stewart so = SCTP_INP_SO(stcb->sctp_ep); 3176ceaad40aSRandall Stewart atomic_add_int(&stcb->asoc.refcnt, 1); 3177ceaad40aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 3178ceaad40aSRandall Stewart SCTP_SOCKET_LOCK(so, 1); 3179ceaad40aSRandall Stewart SCTP_TCB_LOCK(stcb); 3180ceaad40aSRandall Stewart atomic_subtract_int(&stcb->asoc.refcnt, 1); 3181ceaad40aSRandall Stewart if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 3182ceaad40aSRandall Stewart SCTP_SOCKET_UNLOCK(so, 1); 3183ceaad40aSRandall Stewart return; 3184ceaad40aSRandall Stewart } 3185ceaad40aSRandall Stewart #endif 3186f8829a4aSRandall Stewart socantsendmore(stcb->sctp_socket); 3187ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3188ceaad40aSRandall Stewart SCTP_SOCKET_UNLOCK(so, 1); 3189ceaad40aSRandall Stewart #endif 3190f8829a4aSRandall Stewart } 3191e2e7c62eSMichael Tuexen if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)) { 3192f8829a4aSRandall Stewart /* event not enabled */ 3193f8829a4aSRandall Stewart return; 3194830d754dSRandall Stewart } 3195139bc87fSRandall Stewart m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_event), 0, M_DONTWAIT, 1, MT_DATA); 3196f8829a4aSRandall Stewart if (m_notify == NULL) 3197f8829a4aSRandall Stewart /* no space left */ 3198f8829a4aSRandall Stewart return; 3199f8829a4aSRandall Stewart sse = mtod(m_notify, struct sctp_shutdown_event *); 3200f8829a4aSRandall Stewart sse->sse_type = SCTP_SHUTDOWN_EVENT; 3201f8829a4aSRandall Stewart sse->sse_flags = 0; 3202f8829a4aSRandall Stewart sse->sse_length = sizeof(struct sctp_shutdown_event); 3203f8829a4aSRandall Stewart sse->sse_assoc_id = sctp_get_associd(stcb); 3204f8829a4aSRandall Stewart 3205139bc87fSRandall Stewart SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_shutdown_event); 3206139bc87fSRandall Stewart SCTP_BUF_NEXT(m_notify) = NULL; 3207f8829a4aSRandall Stewart 3208f8829a4aSRandall Stewart /* append to socket */ 3209f8829a4aSRandall Stewart control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, 32107215cc1bSMichael Tuexen 0, 0, stcb->asoc.context, 0, 0, 0, 3211f8829a4aSRandall Stewart m_notify); 3212f8829a4aSRandall Stewart if (control == NULL) { 3213f8829a4aSRandall Stewart /* no memory */ 3214f8829a4aSRandall Stewart sctp_m_freem(m_notify); 3215f8829a4aSRandall Stewart return; 3216f8829a4aSRandall Stewart } 3217139bc87fSRandall Stewart control->spec_flags = M_NOTIFICATION; 3218139bc87fSRandall Stewart control->length = SCTP_BUF_LEN(m_notify); 3219f8829a4aSRandall Stewart /* not that we need this */ 3220f8829a4aSRandall Stewart control->tail_mbuf = m_notify; 3221f8829a4aSRandall Stewart sctp_add_to_readq(stcb->sctp_ep, stcb, 3222f8829a4aSRandall Stewart control, 3223cfde3ff7SRandall Stewart &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED); 3224f8829a4aSRandall Stewart } 3225f8829a4aSRandall Stewart 3226f8829a4aSRandall Stewart static void 3227830d754dSRandall Stewart sctp_notify_sender_dry_event(struct sctp_tcb *stcb, 3228830d754dSRandall Stewart int so_locked 3229830d754dSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 3230830d754dSRandall Stewart SCTP_UNUSED 3231830d754dSRandall Stewart #endif 3232830d754dSRandall Stewart ) 3233830d754dSRandall Stewart { 3234830d754dSRandall Stewart struct mbuf *m_notify; 3235830d754dSRandall Stewart struct sctp_sender_dry_event *event; 3236830d754dSRandall Stewart struct sctp_queued_to_read *control; 3237830d754dSRandall Stewart 323860990c0cSMichael Tuexen if ((stcb == NULL) || 323960990c0cSMichael Tuexen sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DRYEVNT)) { 3240830d754dSRandall Stewart /* event not enabled */ 3241830d754dSRandall Stewart return; 3242830d754dSRandall Stewart } 3243830d754dSRandall Stewart m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_sender_dry_event), 0, M_DONTWAIT, 1, MT_DATA); 3244830d754dSRandall Stewart if (m_notify == NULL) { 3245830d754dSRandall Stewart /* no space left */ 3246830d754dSRandall Stewart return; 3247830d754dSRandall Stewart } 3248830d754dSRandall Stewart SCTP_BUF_LEN(m_notify) = 0; 3249830d754dSRandall Stewart event = mtod(m_notify, struct sctp_sender_dry_event *); 3250830d754dSRandall Stewart event->sender_dry_type = SCTP_SENDER_DRY_EVENT; 3251830d754dSRandall Stewart event->sender_dry_flags = 0; 3252830d754dSRandall Stewart event->sender_dry_length = sizeof(struct sctp_sender_dry_event); 3253830d754dSRandall Stewart event->sender_dry_assoc_id = sctp_get_associd(stcb); 3254830d754dSRandall Stewart 3255830d754dSRandall Stewart SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_sender_dry_event); 3256830d754dSRandall Stewart SCTP_BUF_NEXT(m_notify) = NULL; 3257830d754dSRandall Stewart 3258830d754dSRandall Stewart /* append to socket */ 3259830d754dSRandall Stewart control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, 32607215cc1bSMichael Tuexen 0, 0, stcb->asoc.context, 0, 0, 0, 32617215cc1bSMichael Tuexen m_notify); 3262830d754dSRandall Stewart if (control == NULL) { 3263830d754dSRandall Stewart /* no memory */ 3264830d754dSRandall Stewart sctp_m_freem(m_notify); 3265830d754dSRandall Stewart return; 3266830d754dSRandall Stewart } 3267830d754dSRandall Stewart control->length = SCTP_BUF_LEN(m_notify); 3268830d754dSRandall Stewart control->spec_flags = M_NOTIFICATION; 3269830d754dSRandall Stewart /* not that we need this */ 3270830d754dSRandall Stewart control->tail_mbuf = m_notify; 3271830d754dSRandall Stewart sctp_add_to_readq(stcb->sctp_ep, stcb, control, 3272cfde3ff7SRandall Stewart &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked); 3273830d754dSRandall Stewart } 3274830d754dSRandall Stewart 3275ea44232bSRandall Stewart 3276c4e848b7SRandall Stewart void 3277c4e848b7SRandall Stewart sctp_notify_stream_reset_add(struct sctp_tcb *stcb, uint16_t numberin, uint16_t numberout, int flag) 3278ea44232bSRandall Stewart { 3279ea44232bSRandall Stewart struct mbuf *m_notify; 3280ea44232bSRandall Stewart struct sctp_queued_to_read *control; 3281c4e848b7SRandall Stewart struct sctp_stream_change_event *stradd; 3282ea44232bSRandall Stewart int len; 3283ea44232bSRandall Stewart 32848c501e51SMichael Tuexen if ((stcb == NULL) || 32858c501e51SMichael Tuexen (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_CHANGEEVNT))) { 3286ea44232bSRandall Stewart /* event not enabled */ 3287ea44232bSRandall Stewart return; 3288ea44232bSRandall Stewart } 3289c4e848b7SRandall Stewart if ((stcb->asoc.peer_req_out) && flag) { 3290c4e848b7SRandall Stewart /* Peer made the request, don't tell the local user */ 3291c4e848b7SRandall Stewart stcb->asoc.peer_req_out = 0; 3292c4e848b7SRandall Stewart return; 3293c4e848b7SRandall Stewart } 3294c4e848b7SRandall Stewart stcb->asoc.peer_req_out = 0; 3295ea44232bSRandall Stewart m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 3296ea44232bSRandall Stewart if (m_notify == NULL) 3297ea44232bSRandall Stewart /* no space left */ 3298ea44232bSRandall Stewart return; 3299ea44232bSRandall Stewart SCTP_BUF_LEN(m_notify) = 0; 3300c4e848b7SRandall Stewart len = sizeof(struct sctp_stream_change_event); 3301ea44232bSRandall Stewart if (len > M_TRAILINGSPACE(m_notify)) { 3302ea44232bSRandall Stewart /* never enough room */ 3303ea44232bSRandall Stewart sctp_m_freem(m_notify); 3304ea44232bSRandall Stewart return; 3305ea44232bSRandall Stewart } 3306c4e848b7SRandall Stewart stradd = mtod(m_notify, struct sctp_stream_change_event *); 3307c4e848b7SRandall Stewart stradd->strchange_type = SCTP_STREAM_CHANGE_EVENT; 3308c4e848b7SRandall Stewart stradd->strchange_flags = flag; 3309c4e848b7SRandall Stewart stradd->strchange_length = len; 3310c4e848b7SRandall Stewart stradd->strchange_assoc_id = sctp_get_associd(stcb); 3311c4e848b7SRandall Stewart stradd->strchange_instrms = numberin; 3312c4e848b7SRandall Stewart stradd->strchange_outstrms = numberout; 3313ea44232bSRandall Stewart SCTP_BUF_LEN(m_notify) = len; 3314ea44232bSRandall Stewart SCTP_BUF_NEXT(m_notify) = NULL; 3315ea44232bSRandall Stewart if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) { 3316ea44232bSRandall Stewart /* no space */ 3317ea44232bSRandall Stewart sctp_m_freem(m_notify); 3318ea44232bSRandall Stewart return; 3319ea44232bSRandall Stewart } 3320ea44232bSRandall Stewart /* append to socket */ 3321ea44232bSRandall Stewart control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, 33227215cc1bSMichael Tuexen 0, 0, stcb->asoc.context, 0, 0, 0, 3323ea44232bSRandall Stewart m_notify); 3324ea44232bSRandall Stewart if (control == NULL) { 3325ea44232bSRandall Stewart /* no memory */ 3326ea44232bSRandall Stewart sctp_m_freem(m_notify); 3327ea44232bSRandall Stewart return; 3328ea44232bSRandall Stewart } 3329ea44232bSRandall Stewart control->spec_flags = M_NOTIFICATION; 3330ea44232bSRandall Stewart control->length = SCTP_BUF_LEN(m_notify); 3331ea44232bSRandall Stewart /* not that we need this */ 3332ea44232bSRandall Stewart control->tail_mbuf = m_notify; 3333ea44232bSRandall Stewart sctp_add_to_readq(stcb->sctp_ep, stcb, 3334ea44232bSRandall Stewart control, 3335cfde3ff7SRandall Stewart &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED); 3336ea44232bSRandall Stewart } 3337ea44232bSRandall Stewart 3338c4e848b7SRandall Stewart void 3339c4e848b7SRandall Stewart sctp_notify_stream_reset_tsn(struct sctp_tcb *stcb, uint32_t sending_tsn, uint32_t recv_tsn, int flag) 3340c4e848b7SRandall Stewart { 3341c4e848b7SRandall Stewart struct mbuf *m_notify; 3342c4e848b7SRandall Stewart struct sctp_queued_to_read *control; 3343c4e848b7SRandall Stewart struct sctp_assoc_reset_event *strasoc; 3344c4e848b7SRandall Stewart int len; 3345c4e848b7SRandall Stewart 33468c501e51SMichael Tuexen if ((stcb == NULL) || 33478c501e51SMichael Tuexen (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ASSOC_RESETEVNT))) { 3348c4e848b7SRandall Stewart /* event not enabled */ 3349c4e848b7SRandall Stewart return; 3350c4e848b7SRandall Stewart } 3351c4e848b7SRandall Stewart m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 3352c4e848b7SRandall Stewart if (m_notify == NULL) 3353c4e848b7SRandall Stewart /* no space left */ 3354c4e848b7SRandall Stewart return; 3355c4e848b7SRandall Stewart SCTP_BUF_LEN(m_notify) = 0; 3356c4e848b7SRandall Stewart len = sizeof(struct sctp_assoc_reset_event); 3357c4e848b7SRandall Stewart if (len > M_TRAILINGSPACE(m_notify)) { 3358c4e848b7SRandall Stewart /* never enough room */ 3359c4e848b7SRandall Stewart sctp_m_freem(m_notify); 3360c4e848b7SRandall Stewart return; 3361c4e848b7SRandall Stewart } 3362c4e848b7SRandall Stewart strasoc = mtod(m_notify, struct sctp_assoc_reset_event *); 3363c4e848b7SRandall Stewart strasoc->assocreset_type = SCTP_ASSOC_RESET_EVENT; 3364c4e848b7SRandall Stewart strasoc->assocreset_flags = flag; 3365c4e848b7SRandall Stewart strasoc->assocreset_length = len; 3366c4e848b7SRandall Stewart strasoc->assocreset_assoc_id = sctp_get_associd(stcb); 3367c4e848b7SRandall Stewart strasoc->assocreset_local_tsn = sending_tsn; 3368c4e848b7SRandall Stewart strasoc->assocreset_remote_tsn = recv_tsn; 3369c4e848b7SRandall Stewart SCTP_BUF_LEN(m_notify) = len; 3370c4e848b7SRandall Stewart SCTP_BUF_NEXT(m_notify) = NULL; 3371c4e848b7SRandall Stewart if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) { 3372c4e848b7SRandall Stewart /* no space */ 3373c4e848b7SRandall Stewart sctp_m_freem(m_notify); 3374c4e848b7SRandall Stewart return; 3375c4e848b7SRandall Stewart } 3376c4e848b7SRandall Stewart /* append to socket */ 3377c4e848b7SRandall Stewart control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, 3378c4e848b7SRandall Stewart 0, 0, stcb->asoc.context, 0, 0, 0, 3379c4e848b7SRandall Stewart m_notify); 3380c4e848b7SRandall Stewart if (control == NULL) { 3381c4e848b7SRandall Stewart /* no memory */ 3382c4e848b7SRandall Stewart sctp_m_freem(m_notify); 3383c4e848b7SRandall Stewart return; 3384c4e848b7SRandall Stewart } 3385c4e848b7SRandall Stewart control->spec_flags = M_NOTIFICATION; 3386c4e848b7SRandall Stewart control->length = SCTP_BUF_LEN(m_notify); 3387c4e848b7SRandall Stewart /* not that we need this */ 3388c4e848b7SRandall Stewart control->tail_mbuf = m_notify; 3389c4e848b7SRandall Stewart sctp_add_to_readq(stcb->sctp_ep, stcb, 3390c4e848b7SRandall Stewart control, 3391c4e848b7SRandall Stewart &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED); 3392c4e848b7SRandall Stewart } 3393c4e848b7SRandall Stewart 3394c4e848b7SRandall Stewart 3395ea44232bSRandall Stewart 3396830d754dSRandall Stewart static void 3397f8829a4aSRandall Stewart sctp_notify_stream_reset(struct sctp_tcb *stcb, 3398f8829a4aSRandall Stewart int number_entries, uint16_t * list, int flag) 3399f8829a4aSRandall Stewart { 3400f8829a4aSRandall Stewart struct mbuf *m_notify; 3401f8829a4aSRandall Stewart struct sctp_queued_to_read *control; 3402f8829a4aSRandall Stewart struct sctp_stream_reset_event *strreset; 3403f8829a4aSRandall Stewart int len; 3404f8829a4aSRandall Stewart 34058c501e51SMichael Tuexen if ((stcb == NULL) || 34068c501e51SMichael Tuexen (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT))) { 3407f8829a4aSRandall Stewart /* event not enabled */ 3408f8829a4aSRandall Stewart return; 3409830d754dSRandall Stewart } 3410139bc87fSRandall Stewart m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA); 3411f8829a4aSRandall Stewart if (m_notify == NULL) 3412f8829a4aSRandall Stewart /* no space left */ 3413f8829a4aSRandall Stewart return; 3414139bc87fSRandall Stewart SCTP_BUF_LEN(m_notify) = 0; 3415f8829a4aSRandall Stewart len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t)); 3416f8829a4aSRandall Stewart if (len > M_TRAILINGSPACE(m_notify)) { 3417f8829a4aSRandall Stewart /* never enough room */ 3418f8829a4aSRandall Stewart sctp_m_freem(m_notify); 3419f8829a4aSRandall Stewart return; 3420f8829a4aSRandall Stewart } 3421f8829a4aSRandall Stewart strreset = mtod(m_notify, struct sctp_stream_reset_event *); 3422f8829a4aSRandall Stewart strreset->strreset_type = SCTP_STREAM_RESET_EVENT; 3423c4e848b7SRandall Stewart strreset->strreset_flags = flag; 3424f8829a4aSRandall Stewart strreset->strreset_length = len; 3425f8829a4aSRandall Stewart strreset->strreset_assoc_id = sctp_get_associd(stcb); 3426f8829a4aSRandall Stewart if (number_entries) { 3427f8829a4aSRandall Stewart int i; 3428f8829a4aSRandall Stewart 3429f8829a4aSRandall Stewart for (i = 0; i < number_entries; i++) { 3430c4e848b7SRandall Stewart strreset->strreset_stream_list[i] = ntohs(list[i]); 3431f8829a4aSRandall Stewart } 3432f8829a4aSRandall Stewart } 3433139bc87fSRandall Stewart SCTP_BUF_LEN(m_notify) = len; 3434139bc87fSRandall Stewart SCTP_BUF_NEXT(m_notify) = NULL; 3435139bc87fSRandall Stewart if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) { 3436f8829a4aSRandall Stewart /* no space */ 3437f8829a4aSRandall Stewart sctp_m_freem(m_notify); 3438f8829a4aSRandall Stewart return; 3439f8829a4aSRandall Stewart } 3440f8829a4aSRandall Stewart /* append to socket */ 3441f8829a4aSRandall Stewart control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, 34427215cc1bSMichael Tuexen 0, 0, stcb->asoc.context, 0, 0, 0, 3443f8829a4aSRandall Stewart m_notify); 3444f8829a4aSRandall Stewart if (control == NULL) { 3445f8829a4aSRandall Stewart /* no memory */ 3446f8829a4aSRandall Stewart sctp_m_freem(m_notify); 3447f8829a4aSRandall Stewart return; 3448f8829a4aSRandall Stewart } 3449139bc87fSRandall Stewart control->spec_flags = M_NOTIFICATION; 3450139bc87fSRandall Stewart control->length = SCTP_BUF_LEN(m_notify); 3451f8829a4aSRandall Stewart /* not that we need this */ 3452f8829a4aSRandall Stewart control->tail_mbuf = m_notify; 3453f8829a4aSRandall Stewart sctp_add_to_readq(stcb->sctp_ep, stcb, 3454f8829a4aSRandall Stewart control, 3455cfde3ff7SRandall Stewart &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED); 3456f8829a4aSRandall Stewart } 3457f8829a4aSRandall Stewart 3458f8829a4aSRandall Stewart 3459389b1b11SMichael Tuexen static void 3460389b1b11SMichael Tuexen sctp_notify_remote_error(struct sctp_tcb *stcb, uint16_t error, struct sctp_error_chunk *chunk) 3461389b1b11SMichael Tuexen { 3462389b1b11SMichael Tuexen struct mbuf *m_notify; 3463389b1b11SMichael Tuexen struct sctp_remote_error *sre; 3464389b1b11SMichael Tuexen struct sctp_queued_to_read *control; 3465389b1b11SMichael Tuexen size_t notif_len, chunk_len; 3466389b1b11SMichael Tuexen 3467389b1b11SMichael Tuexen if ((stcb == NULL) || 3468389b1b11SMichael Tuexen sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPEERERR)) { 3469389b1b11SMichael Tuexen return; 3470389b1b11SMichael Tuexen } 3471389b1b11SMichael Tuexen if (chunk != NULL) { 3472389b1b11SMichael Tuexen chunk_len = htons(chunk->ch.chunk_length); 3473389b1b11SMichael Tuexen } else { 3474389b1b11SMichael Tuexen chunk_len = 0; 3475389b1b11SMichael Tuexen } 3476389b1b11SMichael Tuexen notif_len = sizeof(struct sctp_remote_error) + chunk_len; 3477389b1b11SMichael Tuexen m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_DONTWAIT, 1, MT_DATA); 3478389b1b11SMichael Tuexen if (m_notify == NULL) { 3479389b1b11SMichael Tuexen /* Retry with smaller value. */ 3480389b1b11SMichael Tuexen notif_len = sizeof(struct sctp_remote_error); 3481389b1b11SMichael Tuexen m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_DONTWAIT, 1, MT_DATA); 3482389b1b11SMichael Tuexen if (m_notify == NULL) { 3483389b1b11SMichael Tuexen return; 3484389b1b11SMichael Tuexen } 3485389b1b11SMichael Tuexen } 3486389b1b11SMichael Tuexen SCTP_BUF_NEXT(m_notify) = NULL; 3487389b1b11SMichael Tuexen sre = mtod(m_notify, struct sctp_remote_error *); 3488389b1b11SMichael Tuexen sre->sre_type = SCTP_REMOTE_ERROR; 3489389b1b11SMichael Tuexen sre->sre_flags = 0; 3490389b1b11SMichael Tuexen sre->sre_length = sizeof(struct sctp_remote_error); 3491389b1b11SMichael Tuexen sre->sre_error = error; 3492389b1b11SMichael Tuexen sre->sre_assoc_id = sctp_get_associd(stcb); 3493389b1b11SMichael Tuexen if (notif_len > sizeof(struct sctp_remote_error)) { 3494389b1b11SMichael Tuexen memcpy(sre->sre_data, chunk, chunk_len); 3495389b1b11SMichael Tuexen sre->sre_length += chunk_len; 3496389b1b11SMichael Tuexen } 3497389b1b11SMichael Tuexen SCTP_BUF_LEN(m_notify) = sre->sre_length; 3498389b1b11SMichael Tuexen control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, 3499389b1b11SMichael Tuexen 0, 0, stcb->asoc.context, 0, 0, 0, 3500389b1b11SMichael Tuexen m_notify); 3501389b1b11SMichael Tuexen if (control != NULL) { 3502389b1b11SMichael Tuexen control->length = SCTP_BUF_LEN(m_notify); 3503389b1b11SMichael Tuexen /* not that we need this */ 3504389b1b11SMichael Tuexen control->tail_mbuf = m_notify; 3505389b1b11SMichael Tuexen control->spec_flags = M_NOTIFICATION; 3506389b1b11SMichael Tuexen sctp_add_to_readq(stcb->sctp_ep, stcb, 3507389b1b11SMichael Tuexen control, 3508389b1b11SMichael Tuexen &stcb->sctp_socket->so_rcv, 1, 3509389b1b11SMichael Tuexen SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED); 3510389b1b11SMichael Tuexen } else { 3511389b1b11SMichael Tuexen sctp_m_freem(m_notify); 3512389b1b11SMichael Tuexen } 3513389b1b11SMichael Tuexen } 3514389b1b11SMichael Tuexen 3515389b1b11SMichael Tuexen 3516f8829a4aSRandall Stewart void 3517f8829a4aSRandall Stewart sctp_ulp_notify(uint32_t notification, struct sctp_tcb *stcb, 3518ceaad40aSRandall Stewart uint32_t error, void *data, int so_locked 3519ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 3520ceaad40aSRandall Stewart SCTP_UNUSED 3521ceaad40aSRandall Stewart #endif 3522ceaad40aSRandall Stewart ) 3523f8829a4aSRandall Stewart { 3524830d754dSRandall Stewart if ((stcb == NULL) || 3525830d754dSRandall Stewart (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 3526f8829a4aSRandall Stewart (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 3527830d754dSRandall Stewart (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) { 3528830d754dSRandall Stewart /* If the socket is gone we are out of here */ 3529f8829a4aSRandall Stewart return; 3530f8829a4aSRandall Stewart } 3531a99b6783SRandall Stewart if (stcb->sctp_socket->so_rcv.sb_state & SBS_CANTRCVMORE) { 3532a99b6783SRandall Stewart return; 3533a99b6783SRandall Stewart } 353417205eccSRandall Stewart if (stcb && ((stcb->asoc.state & SCTP_STATE_COOKIE_WAIT) || 353517205eccSRandall Stewart (stcb->asoc.state & SCTP_STATE_COOKIE_ECHOED))) { 353617205eccSRandall Stewart if ((notification == SCTP_NOTIFY_INTERFACE_DOWN) || 353717205eccSRandall Stewart (notification == SCTP_NOTIFY_INTERFACE_UP) || 353817205eccSRandall Stewart (notification == SCTP_NOTIFY_INTERFACE_CONFIRMED)) { 353917205eccSRandall Stewart /* Don't report these in front states */ 354017205eccSRandall Stewart return; 354117205eccSRandall Stewart } 354217205eccSRandall Stewart } 3543f8829a4aSRandall Stewart switch (notification) { 3544f8829a4aSRandall Stewart case SCTP_NOTIFY_ASSOC_UP: 3545f8829a4aSRandall Stewart if (stcb->asoc.assoc_up_sent == 0) { 3546410a3b1eSMichael Tuexen sctp_notify_assoc_change(SCTP_COMM_UP, stcb, error, NULL, 0, so_locked); 3547f8829a4aSRandall Stewart stcb->asoc.assoc_up_sent = 1; 3548f8829a4aSRandall Stewart } 35492afb3e84SRandall Stewart if (stcb->asoc.adaptation_needed && (stcb->asoc.adaptation_sent == 0)) { 35507215cc1bSMichael Tuexen sctp_notify_adaptation_layer(stcb); 35512afb3e84SRandall Stewart } 3552830d754dSRandall Stewart if (stcb->asoc.peer_supports_auth == 0) { 3553830d754dSRandall Stewart sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0, 3554830d754dSRandall Stewart NULL, so_locked); 3555830d754dSRandall Stewart } 3556f8829a4aSRandall Stewart break; 3557f8829a4aSRandall Stewart case SCTP_NOTIFY_ASSOC_DOWN: 3558410a3b1eSMichael Tuexen sctp_notify_assoc_change(SCTP_SHUTDOWN_COMP, stcb, error, NULL, 0, so_locked); 3559f8829a4aSRandall Stewart break; 3560f8829a4aSRandall Stewart case SCTP_NOTIFY_INTERFACE_DOWN: 3561f8829a4aSRandall Stewart { 3562f8829a4aSRandall Stewart struct sctp_nets *net; 3563f8829a4aSRandall Stewart 3564f8829a4aSRandall Stewart net = (struct sctp_nets *)data; 3565f8829a4aSRandall Stewart sctp_notify_peer_addr_change(stcb, SCTP_ADDR_UNREACHABLE, 3566f8829a4aSRandall Stewart (struct sockaddr *)&net->ro._l_addr, error); 3567f8829a4aSRandall Stewart break; 3568f8829a4aSRandall Stewart } 3569f8829a4aSRandall Stewart case SCTP_NOTIFY_INTERFACE_UP: 3570f8829a4aSRandall Stewart { 3571f8829a4aSRandall Stewart struct sctp_nets *net; 3572f8829a4aSRandall Stewart 3573f8829a4aSRandall Stewart net = (struct sctp_nets *)data; 3574f8829a4aSRandall Stewart sctp_notify_peer_addr_change(stcb, SCTP_ADDR_AVAILABLE, 3575f8829a4aSRandall Stewart (struct sockaddr *)&net->ro._l_addr, error); 3576f8829a4aSRandall Stewart break; 3577f8829a4aSRandall Stewart } 3578f8829a4aSRandall Stewart case SCTP_NOTIFY_INTERFACE_CONFIRMED: 3579f8829a4aSRandall Stewart { 3580f8829a4aSRandall Stewart struct sctp_nets *net; 3581f8829a4aSRandall Stewart 3582f8829a4aSRandall Stewart net = (struct sctp_nets *)data; 3583f8829a4aSRandall Stewart sctp_notify_peer_addr_change(stcb, SCTP_ADDR_CONFIRMED, 3584f8829a4aSRandall Stewart (struct sockaddr *)&net->ro._l_addr, error); 3585f8829a4aSRandall Stewart break; 3586f8829a4aSRandall Stewart } 3587f8829a4aSRandall Stewart case SCTP_NOTIFY_SPECIAL_SP_FAIL: 3588f8829a4aSRandall Stewart sctp_notify_send_failed2(stcb, error, 3589ceaad40aSRandall Stewart (struct sctp_stream_queue_pending *)data, so_locked); 3590f8829a4aSRandall Stewart break; 35911edc9dbaSMichael Tuexen case SCTP_NOTIFY_SENT_DG_FAIL: 35921edc9dbaSMichael Tuexen sctp_notify_send_failed(stcb, 1, error, 35931edc9dbaSMichael Tuexen (struct sctp_tmit_chunk *)data, so_locked); 35941edc9dbaSMichael Tuexen break; 35951edc9dbaSMichael Tuexen case SCTP_NOTIFY_UNSENT_DG_FAIL: 35961edc9dbaSMichael Tuexen sctp_notify_send_failed(stcb, 0, error, 3597ceaad40aSRandall Stewart (struct sctp_tmit_chunk *)data, so_locked); 3598f8829a4aSRandall Stewart break; 3599f8829a4aSRandall Stewart case SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION: 36009a6142d8SRandall Stewart { 36019a6142d8SRandall Stewart uint32_t val; 36029a6142d8SRandall Stewart 36039a6142d8SRandall Stewart val = *((uint32_t *) data); 36049a6142d8SRandall Stewart 3605810ec536SMichael Tuexen sctp_notify_partial_delivery_indication(stcb, error, val, so_locked); 3606f8829a4aSRandall Stewart break; 3607810ec536SMichael Tuexen } 3608410a3b1eSMichael Tuexen case SCTP_NOTIFY_ASSOC_LOC_ABORTED: 3609c105859eSRandall Stewart if ((stcb) && (((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_WAIT) || 3610c105859eSRandall Stewart ((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_ECHOED))) { 3611410a3b1eSMichael Tuexen sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 0, so_locked); 3612c105859eSRandall Stewart } else { 3613410a3b1eSMichael Tuexen sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 0, so_locked); 3614410a3b1eSMichael Tuexen } 3615410a3b1eSMichael Tuexen break; 3616410a3b1eSMichael Tuexen case SCTP_NOTIFY_ASSOC_REM_ABORTED: 3617410a3b1eSMichael Tuexen if ((stcb) && (((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_WAIT) || 3618410a3b1eSMichael Tuexen ((stcb->asoc.state & SCTP_STATE_MASK) == SCTP_STATE_COOKIE_ECHOED))) { 3619410a3b1eSMichael Tuexen sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, 1, so_locked); 3620410a3b1eSMichael Tuexen } else { 3621410a3b1eSMichael Tuexen sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, 1, so_locked); 3622c105859eSRandall Stewart } 3623f8829a4aSRandall Stewart break; 3624f8829a4aSRandall Stewart case SCTP_NOTIFY_ASSOC_RESTART: 3625410a3b1eSMichael Tuexen sctp_notify_assoc_change(SCTP_RESTART, stcb, error, NULL, 0, so_locked); 3626830d754dSRandall Stewart if (stcb->asoc.peer_supports_auth == 0) { 3627830d754dSRandall Stewart sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0, 3628830d754dSRandall Stewart NULL, so_locked); 3629830d754dSRandall Stewart } 3630f8829a4aSRandall Stewart break; 3631f8829a4aSRandall Stewart case SCTP_NOTIFY_STR_RESET_SEND: 3632d7714577SMichael Tuexen sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STREAM_RESET_OUTGOING_SSN); 3633f8829a4aSRandall Stewart break; 3634f8829a4aSRandall Stewart case SCTP_NOTIFY_STR_RESET_RECV: 3635d7714577SMichael Tuexen sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STREAM_RESET_INCOMING); 3636f8829a4aSRandall Stewart break; 3637f8829a4aSRandall Stewart case SCTP_NOTIFY_STR_RESET_FAILED_OUT: 3638c4e848b7SRandall Stewart sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), 3639d7714577SMichael Tuexen (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_FAILED)); 3640f8829a4aSRandall Stewart break; 3641d4260646SMichael Tuexen case SCTP_NOTIFY_STR_RESET_DENIED_OUT: 3642d4260646SMichael Tuexen sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), 3643d4260646SMichael Tuexen (SCTP_STREAM_RESET_OUTGOING_SSN | SCTP_STREAM_RESET_DENIED)); 3644d4260646SMichael Tuexen break; 3645f8829a4aSRandall Stewart case SCTP_NOTIFY_STR_RESET_FAILED_IN: 3646c4e848b7SRandall Stewart sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), 3647d7714577SMichael Tuexen (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_FAILED)); 3648f8829a4aSRandall Stewart break; 3649d4260646SMichael Tuexen case SCTP_NOTIFY_STR_RESET_DENIED_IN: 3650d4260646SMichael Tuexen sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), 3651d4260646SMichael Tuexen (SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_DENIED)); 3652d4260646SMichael Tuexen break; 3653f8829a4aSRandall Stewart case SCTP_NOTIFY_ASCONF_ADD_IP: 3654f8829a4aSRandall Stewart sctp_notify_peer_addr_change(stcb, SCTP_ADDR_ADDED, data, 3655f8829a4aSRandall Stewart error); 3656f8829a4aSRandall Stewart break; 3657f8829a4aSRandall Stewart case SCTP_NOTIFY_ASCONF_DELETE_IP: 3658f8829a4aSRandall Stewart sctp_notify_peer_addr_change(stcb, SCTP_ADDR_REMOVED, data, 3659f8829a4aSRandall Stewart error); 3660f8829a4aSRandall Stewart break; 3661f8829a4aSRandall Stewart case SCTP_NOTIFY_ASCONF_SET_PRIMARY: 3662f8829a4aSRandall Stewart sctp_notify_peer_addr_change(stcb, SCTP_ADDR_MADE_PRIM, data, 3663f8829a4aSRandall Stewart error); 3664f8829a4aSRandall Stewart break; 3665f8829a4aSRandall Stewart case SCTP_NOTIFY_PEER_SHUTDOWN: 3666f8829a4aSRandall Stewart sctp_notify_shutdown_event(stcb); 3667f8829a4aSRandall Stewart break; 3668f8829a4aSRandall Stewart case SCTP_NOTIFY_AUTH_NEW_KEY: 366978f28045SMichael Tuexen sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY, error, 3670830d754dSRandall Stewart (uint16_t) (uintptr_t) data, 3671830d754dSRandall Stewart so_locked); 3672f8829a4aSRandall Stewart break; 3673830d754dSRandall Stewart case SCTP_NOTIFY_AUTH_FREE_KEY: 3674830d754dSRandall Stewart sctp_notify_authentication(stcb, SCTP_AUTH_FREE_KEY, error, 3675830d754dSRandall Stewart (uint16_t) (uintptr_t) data, 3676830d754dSRandall Stewart so_locked); 3677f8829a4aSRandall Stewart break; 3678830d754dSRandall Stewart case SCTP_NOTIFY_NO_PEER_AUTH: 3679830d754dSRandall Stewart sctp_notify_authentication(stcb, SCTP_AUTH_NO_AUTH, error, 3680830d754dSRandall Stewart (uint16_t) (uintptr_t) data, 3681830d754dSRandall Stewart so_locked); 3682830d754dSRandall Stewart break; 3683830d754dSRandall Stewart case SCTP_NOTIFY_SENDER_DRY: 3684830d754dSRandall Stewart sctp_notify_sender_dry_event(stcb, so_locked); 3685830d754dSRandall Stewart break; 3686389b1b11SMichael Tuexen case SCTP_NOTIFY_REMOTE_ERROR: 3687389b1b11SMichael Tuexen sctp_notify_remote_error(stcb, error, data); 3688389b1b11SMichael Tuexen break; 3689f8829a4aSRandall Stewart default: 3690ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_UTIL1, "%s: unknown notification %xh (%u)\n", 3691ad81507eSRandall Stewart __FUNCTION__, notification, notification); 3692f8829a4aSRandall Stewart break; 3693f8829a4aSRandall Stewart } /* end switch */ 3694f8829a4aSRandall Stewart } 3695f8829a4aSRandall Stewart 3696f8829a4aSRandall Stewart void 36971edc9dbaSMichael Tuexen sctp_report_all_outbound(struct sctp_tcb *stcb, uint16_t error, int holds_lock, int so_locked 3698ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 3699ceaad40aSRandall Stewart SCTP_UNUSED 3700ceaad40aSRandall Stewart #endif 3701ceaad40aSRandall Stewart ) 3702f8829a4aSRandall Stewart { 3703f8829a4aSRandall Stewart struct sctp_association *asoc; 3704f8829a4aSRandall Stewart struct sctp_stream_out *outs; 37054a9ef3f8SMichael Tuexen struct sctp_tmit_chunk *chk, *nchk; 37064a9ef3f8SMichael Tuexen struct sctp_stream_queue_pending *sp, *nsp; 37077f34832bSRandall Stewart int i; 3708f8829a4aSRandall Stewart 3709ad81507eSRandall Stewart if (stcb == NULL) { 3710ad81507eSRandall Stewart return; 3711ad81507eSRandall Stewart } 37124a9ef3f8SMichael Tuexen asoc = &stcb->asoc; 37134a9ef3f8SMichael Tuexen if (asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) { 3714478fbccbSRandall Stewart /* already being freed */ 3715478fbccbSRandall Stewart return; 3716478fbccbSRandall Stewart } 3717f8829a4aSRandall Stewart if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 3718f8829a4aSRandall Stewart (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 37194a9ef3f8SMichael Tuexen (asoc->state & SCTP_STATE_CLOSED_SOCKET)) { 3720f8829a4aSRandall Stewart return; 3721f8829a4aSRandall Stewart } 3722f8829a4aSRandall Stewart /* now through all the gunk freeing chunks */ 3723ad81507eSRandall Stewart if (holds_lock == 0) { 37247f34832bSRandall Stewart SCTP_TCB_SEND_LOCK(stcb); 3725ad81507eSRandall Stewart } 3726d00aff5dSRandall Stewart /* sent queue SHOULD be empty */ 37274a9ef3f8SMichael Tuexen TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) { 3728d00aff5dSRandall Stewart TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next); 3729d00aff5dSRandall Stewart asoc->sent_queue_cnt--; 37300c0982b8SRandall Stewart if (chk->data != NULL) { 3731d00aff5dSRandall Stewart sctp_free_bufspace(stcb, asoc, chk, 1); 37321edc9dbaSMichael Tuexen sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 37331edc9dbaSMichael Tuexen error, chk, so_locked); 3734810ec536SMichael Tuexen if (chk->data) { 3735d00aff5dSRandall Stewart sctp_m_freem(chk->data); 3736d00aff5dSRandall Stewart chk->data = NULL; 3737d00aff5dSRandall Stewart } 3738810ec536SMichael Tuexen } 3739689e6a5fSMichael Tuexen sctp_free_a_chunk(stcb, chk, so_locked); 3740d00aff5dSRandall Stewart /* sa_ignore FREED_MEMORY */ 3741d00aff5dSRandall Stewart } 3742d00aff5dSRandall Stewart /* pending send queue SHOULD be empty */ 37434a9ef3f8SMichael Tuexen TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) { 3744d00aff5dSRandall Stewart TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next); 3745d00aff5dSRandall Stewart asoc->send_queue_cnt--; 37460c0982b8SRandall Stewart if (chk->data != NULL) { 3747d00aff5dSRandall Stewart sctp_free_bufspace(stcb, asoc, chk, 1); 37481edc9dbaSMichael Tuexen sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 37491edc9dbaSMichael Tuexen error, chk, so_locked); 3750810ec536SMichael Tuexen if (chk->data) { 3751d00aff5dSRandall Stewart sctp_m_freem(chk->data); 3752d00aff5dSRandall Stewart chk->data = NULL; 3753d00aff5dSRandall Stewart } 3754810ec536SMichael Tuexen } 3755689e6a5fSMichael Tuexen sctp_free_a_chunk(stcb, chk, so_locked); 3756d00aff5dSRandall Stewart /* sa_ignore FREED_MEMORY */ 3757d00aff5dSRandall Stewart } 37584a9ef3f8SMichael Tuexen for (i = 0; i < asoc->streamoutcnt; i++) { 37597f34832bSRandall Stewart /* For each stream */ 37604a9ef3f8SMichael Tuexen outs = &asoc->strmout[i]; 37617f34832bSRandall Stewart /* clean up any sends there */ 37624a9ef3f8SMichael Tuexen asoc->locked_on_sending = NULL; 37634a9ef3f8SMichael Tuexen TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) { 37644a9ef3f8SMichael Tuexen asoc->stream_queue_cnt--; 3765f8829a4aSRandall Stewart TAILQ_REMOVE(&outs->outqueue, sp, next); 3766f8829a4aSRandall Stewart sctp_free_spbufspace(stcb, asoc, sp); 3767478fbccbSRandall Stewart if (sp->data) { 3768f8829a4aSRandall Stewart sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb, 37691edc9dbaSMichael Tuexen error, (void *)sp, so_locked); 3770f8829a4aSRandall Stewart if (sp->data) { 3771f8829a4aSRandall Stewart sctp_m_freem(sp->data); 3772f8829a4aSRandall Stewart sp->data = NULL; 3773d07b2ac6SMichael Tuexen sp->tail_mbuf = NULL; 3774d07b2ac6SMichael Tuexen sp->length = 0; 3775f8829a4aSRandall Stewart } 3776478fbccbSRandall Stewart } 37779eea4a2dSMichael Tuexen if (sp->net) { 3778f8829a4aSRandall Stewart sctp_free_remote_addr(sp->net); 3779f8829a4aSRandall Stewart sp->net = NULL; 37809eea4a2dSMichael Tuexen } 3781f8829a4aSRandall Stewart /* Free the chunk */ 3782689e6a5fSMichael Tuexen sctp_free_a_strmoq(stcb, sp, so_locked); 37833c503c28SRandall Stewart /* sa_ignore FREED_MEMORY */ 3784f8829a4aSRandall Stewart } 3785f8829a4aSRandall Stewart } 3786f8829a4aSRandall Stewart 3787ad81507eSRandall Stewart if (holds_lock == 0) { 37887f34832bSRandall Stewart SCTP_TCB_SEND_UNLOCK(stcb); 3789f8829a4aSRandall Stewart } 3790ad81507eSRandall Stewart } 3791f8829a4aSRandall Stewart 3792f8829a4aSRandall Stewart void 3793410a3b1eSMichael Tuexen sctp_abort_notification(struct sctp_tcb *stcb, uint8_t from_peer, uint16_t error, 3794a2b42326SMichael Tuexen struct sctp_abort_chunk *abort, int so_locked 3795ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 3796ceaad40aSRandall Stewart SCTP_UNUSED 3797ceaad40aSRandall Stewart #endif 3798ceaad40aSRandall Stewart ) 3799f8829a4aSRandall Stewart { 3800ad81507eSRandall Stewart if (stcb == NULL) { 3801ad81507eSRandall Stewart return; 3802ad81507eSRandall Stewart } 3803c55b70ceSMichael Tuexen if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 3804c55b70ceSMichael Tuexen ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 3805c55b70ceSMichael Tuexen (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) { 3806c55b70ceSMichael Tuexen stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_WAS_ABORTED; 3807c55b70ceSMichael Tuexen } 3808f8829a4aSRandall Stewart if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 3809f8829a4aSRandall Stewart (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 3810f8829a4aSRandall Stewart (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) { 3811f8829a4aSRandall Stewart return; 3812f8829a4aSRandall Stewart } 3813f8829a4aSRandall Stewart /* Tell them we lost the asoc */ 38141edc9dbaSMichael Tuexen sctp_report_all_outbound(stcb, error, 1, so_locked); 3815410a3b1eSMichael Tuexen if (from_peer) { 3816410a3b1eSMichael Tuexen sctp_ulp_notify(SCTP_NOTIFY_ASSOC_REM_ABORTED, stcb, error, abort, so_locked); 3817410a3b1eSMichael Tuexen } else { 3818410a3b1eSMichael Tuexen sctp_ulp_notify(SCTP_NOTIFY_ASSOC_LOC_ABORTED, stcb, error, abort, so_locked); 3819410a3b1eSMichael Tuexen } 3820f8829a4aSRandall Stewart } 3821f8829a4aSRandall Stewart 3822f8829a4aSRandall Stewart void 3823f8829a4aSRandall Stewart sctp_abort_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb, 3824b1754ad1SMichael Tuexen struct mbuf *m, int iphlen, 3825b1754ad1SMichael Tuexen struct sockaddr *src, struct sockaddr *dst, 3826b1754ad1SMichael Tuexen struct sctphdr *sh, struct mbuf *op_err, 3827f30ac432SMichael Tuexen uint8_t use_mflowid, uint32_t mflowid, 3828c54a18d2SRandall Stewart uint32_t vrf_id, uint16_t port) 3829f8829a4aSRandall Stewart { 3830f8829a4aSRandall Stewart uint32_t vtag; 3831f8829a4aSRandall Stewart 3832ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3833ceaad40aSRandall Stewart struct socket *so; 3834ceaad40aSRandall Stewart 3835ceaad40aSRandall Stewart #endif 3836ceaad40aSRandall Stewart 3837f8829a4aSRandall Stewart vtag = 0; 3838f8829a4aSRandall Stewart if (stcb != NULL) { 3839f8829a4aSRandall Stewart /* We have a TCB to abort, send notification too */ 3840f8829a4aSRandall Stewart vtag = stcb->asoc.peer_vtag; 3841410a3b1eSMichael Tuexen sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED); 384217205eccSRandall Stewart /* get the assoc vrf id and table id */ 384317205eccSRandall Stewart vrf_id = stcb->asoc.vrf_id; 384463981c2bSRandall Stewart stcb->asoc.state |= SCTP_STATE_WAS_ABORTED; 3845f8829a4aSRandall Stewart } 3846b1754ad1SMichael Tuexen sctp_send_abort(m, iphlen, src, dst, sh, vtag, op_err, 3847f30ac432SMichael Tuexen use_mflowid, mflowid, 3848f30ac432SMichael Tuexen vrf_id, port); 3849f8829a4aSRandall Stewart if (stcb != NULL) { 3850f8829a4aSRandall Stewart /* Ok, now lets free it */ 3851ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3852ceaad40aSRandall Stewart so = SCTP_INP_SO(inp); 3853ceaad40aSRandall Stewart atomic_add_int(&stcb->asoc.refcnt, 1); 3854ceaad40aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 3855ceaad40aSRandall Stewart SCTP_SOCKET_LOCK(so, 1); 3856ceaad40aSRandall Stewart SCTP_TCB_LOCK(stcb); 3857ceaad40aSRandall Stewart atomic_subtract_int(&stcb->asoc.refcnt, 1); 3858ceaad40aSRandall Stewart #endif 38590271d0cdSMichael Tuexen SCTP_STAT_INCR_COUNTER32(sctps_aborted); 38600271d0cdSMichael Tuexen if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || 38610271d0cdSMichael Tuexen (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 38620271d0cdSMichael Tuexen SCTP_STAT_DECR_GAUGE32(sctps_currestab); 38630271d0cdSMichael Tuexen } 3864c4739e2fSRandall Stewart (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_4); 3865ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3866ceaad40aSRandall Stewart SCTP_SOCKET_UNLOCK(so, 1); 3867ceaad40aSRandall Stewart #endif 3868f8829a4aSRandall Stewart } 3869f8829a4aSRandall Stewart } 3870f8829a4aSRandall Stewart 3871f1f73e57SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS 3872f1f73e57SRandall Stewart void 3873f1f73e57SRandall Stewart sctp_print_out_track_log(struct sctp_tcb *stcb) 3874f1f73e57SRandall Stewart { 387518e198d3SRandall Stewart #ifdef NOSIY_PRINTS 3876f1f73e57SRandall Stewart int i; 3877f1f73e57SRandall Stewart 3878ad81507eSRandall Stewart SCTP_PRINTF("Last ep reason:%x\n", stcb->sctp_ep->last_abort_code); 3879ad81507eSRandall Stewart SCTP_PRINTF("IN bound TSN log-aaa\n"); 3880f1f73e57SRandall Stewart if ((stcb->asoc.tsn_in_at == 0) && (stcb->asoc.tsn_in_wrapped == 0)) { 3881ad81507eSRandall Stewart SCTP_PRINTF("None rcvd\n"); 3882f1f73e57SRandall Stewart goto none_in; 3883f1f73e57SRandall Stewart } 3884f1f73e57SRandall Stewart if (stcb->asoc.tsn_in_wrapped) { 3885f1f73e57SRandall Stewart for (i = stcb->asoc.tsn_in_at; i < SCTP_TSN_LOG_SIZE; i++) { 3886ad81507eSRandall Stewart SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n", 3887f1f73e57SRandall Stewart stcb->asoc.in_tsnlog[i].tsn, 3888f1f73e57SRandall Stewart stcb->asoc.in_tsnlog[i].strm, 3889f1f73e57SRandall Stewart stcb->asoc.in_tsnlog[i].seq, 3890f1f73e57SRandall Stewart stcb->asoc.in_tsnlog[i].flgs, 3891f1f73e57SRandall Stewart stcb->asoc.in_tsnlog[i].sz); 3892f1f73e57SRandall Stewart } 3893f1f73e57SRandall Stewart } 3894f1f73e57SRandall Stewart if (stcb->asoc.tsn_in_at) { 3895f1f73e57SRandall Stewart for (i = 0; i < stcb->asoc.tsn_in_at; i++) { 3896ad81507eSRandall Stewart SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n", 3897f1f73e57SRandall Stewart stcb->asoc.in_tsnlog[i].tsn, 3898f1f73e57SRandall Stewart stcb->asoc.in_tsnlog[i].strm, 3899f1f73e57SRandall Stewart stcb->asoc.in_tsnlog[i].seq, 3900f1f73e57SRandall Stewart stcb->asoc.in_tsnlog[i].flgs, 3901f1f73e57SRandall Stewart stcb->asoc.in_tsnlog[i].sz); 3902f1f73e57SRandall Stewart } 3903f1f73e57SRandall Stewart } 3904f1f73e57SRandall Stewart none_in: 3905ad81507eSRandall Stewart SCTP_PRINTF("OUT bound TSN log-aaa\n"); 3906ad81507eSRandall Stewart if ((stcb->asoc.tsn_out_at == 0) && 3907ad81507eSRandall Stewart (stcb->asoc.tsn_out_wrapped == 0)) { 3908ad81507eSRandall Stewart SCTP_PRINTF("None sent\n"); 3909f1f73e57SRandall Stewart } 3910f1f73e57SRandall Stewart if (stcb->asoc.tsn_out_wrapped) { 3911f1f73e57SRandall Stewart for (i = stcb->asoc.tsn_out_at; i < SCTP_TSN_LOG_SIZE; i++) { 3912ad81507eSRandall Stewart SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n", 3913f1f73e57SRandall Stewart stcb->asoc.out_tsnlog[i].tsn, 3914f1f73e57SRandall Stewart stcb->asoc.out_tsnlog[i].strm, 3915f1f73e57SRandall Stewart stcb->asoc.out_tsnlog[i].seq, 3916f1f73e57SRandall Stewart stcb->asoc.out_tsnlog[i].flgs, 3917f1f73e57SRandall Stewart stcb->asoc.out_tsnlog[i].sz); 3918f1f73e57SRandall Stewart } 3919f1f73e57SRandall Stewart } 3920f1f73e57SRandall Stewart if (stcb->asoc.tsn_out_at) { 3921f1f73e57SRandall Stewart for (i = 0; i < stcb->asoc.tsn_out_at; i++) { 3922ad81507eSRandall Stewart SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n", 3923f1f73e57SRandall Stewart stcb->asoc.out_tsnlog[i].tsn, 3924f1f73e57SRandall Stewart stcb->asoc.out_tsnlog[i].strm, 3925f1f73e57SRandall Stewart stcb->asoc.out_tsnlog[i].seq, 3926f1f73e57SRandall Stewart stcb->asoc.out_tsnlog[i].flgs, 3927f1f73e57SRandall Stewart stcb->asoc.out_tsnlog[i].sz); 3928f1f73e57SRandall Stewart } 3929f1f73e57SRandall Stewart } 393018e198d3SRandall Stewart #endif 3931f1f73e57SRandall Stewart } 3932f1f73e57SRandall Stewart 3933f1f73e57SRandall Stewart #endif 3934f1f73e57SRandall Stewart 3935f8829a4aSRandall Stewart void 3936f8829a4aSRandall Stewart sctp_abort_an_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb, 3937a2b42326SMichael Tuexen struct mbuf *op_err, 3938ceaad40aSRandall Stewart int so_locked 3939ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 3940ceaad40aSRandall Stewart SCTP_UNUSED 3941ceaad40aSRandall Stewart #endif 3942ceaad40aSRandall Stewart ) 3943f8829a4aSRandall Stewart { 3944ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3945ceaad40aSRandall Stewart struct socket *so; 3946ceaad40aSRandall Stewart 3947ceaad40aSRandall Stewart #endif 3948ceaad40aSRandall Stewart 3949ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3950ceaad40aSRandall Stewart so = SCTP_INP_SO(inp); 3951ceaad40aSRandall Stewart #endif 3952f8829a4aSRandall Stewart if (stcb == NULL) { 3953f8829a4aSRandall Stewart /* Got to have a TCB */ 3954f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 3955f8829a4aSRandall Stewart if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) { 3956b0552ae2SRandall Stewart sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT, 3957b0552ae2SRandall Stewart SCTP_CALLED_DIRECTLY_NOCMPSET); 3958f8829a4aSRandall Stewart } 3959f8829a4aSRandall Stewart } 3960f8829a4aSRandall Stewart return; 396163981c2bSRandall Stewart } else { 396263981c2bSRandall Stewart stcb->asoc.state |= SCTP_STATE_WAS_ABORTED; 3963f8829a4aSRandall Stewart } 3964f8829a4aSRandall Stewart /* notify the ulp */ 3965a2b42326SMichael Tuexen if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) { 3966410a3b1eSMichael Tuexen sctp_abort_notification(stcb, 0, 0, NULL, so_locked); 3967a2b42326SMichael Tuexen } 3968f8829a4aSRandall Stewart /* notify the peer */ 3969ceaad40aSRandall Stewart sctp_send_abort_tcb(stcb, op_err, so_locked); 3970f8829a4aSRandall Stewart SCTP_STAT_INCR_COUNTER32(sctps_aborted); 3971f8829a4aSRandall Stewart if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || 3972f8829a4aSRandall Stewart (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 3973f8829a4aSRandall Stewart SCTP_STAT_DECR_GAUGE32(sctps_currestab); 3974f8829a4aSRandall Stewart } 3975f8829a4aSRandall Stewart /* now free the asoc */ 3976f1f73e57SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS 3977f1f73e57SRandall Stewart sctp_print_out_track_log(stcb); 3978f1f73e57SRandall Stewart #endif 3979ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3980ceaad40aSRandall Stewart if (!so_locked) { 3981ceaad40aSRandall Stewart atomic_add_int(&stcb->asoc.refcnt, 1); 3982ceaad40aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 3983ceaad40aSRandall Stewart SCTP_SOCKET_LOCK(so, 1); 3984ceaad40aSRandall Stewart SCTP_TCB_LOCK(stcb); 3985ceaad40aSRandall Stewart atomic_subtract_int(&stcb->asoc.refcnt, 1); 3986ceaad40aSRandall Stewart } 3987ceaad40aSRandall Stewart #endif 3988c4739e2fSRandall Stewart (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTPUTIL + SCTP_LOC_5); 3989ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3990ceaad40aSRandall Stewart if (!so_locked) { 3991ceaad40aSRandall Stewart SCTP_SOCKET_UNLOCK(so, 1); 3992ceaad40aSRandall Stewart } 3993ceaad40aSRandall Stewart #endif 3994f8829a4aSRandall Stewart } 3995f8829a4aSRandall Stewart 3996f8829a4aSRandall Stewart void 3997b1754ad1SMichael Tuexen sctp_handle_ootb(struct mbuf *m, int iphlen, int offset, 3998b1754ad1SMichael Tuexen struct sockaddr *src, struct sockaddr *dst, 3999b1754ad1SMichael Tuexen struct sctphdr *sh, struct sctp_inpcb *inp, 4000f30ac432SMichael Tuexen uint8_t use_mflowid, uint32_t mflowid, 4001f30ac432SMichael Tuexen uint32_t vrf_id, uint16_t port) 4002f8829a4aSRandall Stewart { 4003f8829a4aSRandall Stewart struct sctp_chunkhdr *ch, chunk_buf; 4004f8829a4aSRandall Stewart unsigned int chk_length; 4005c58e60beSMichael Tuexen int contains_init_chunk; 4006f8829a4aSRandall Stewart 4007f8829a4aSRandall Stewart SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue); 4008f8829a4aSRandall Stewart /* Generate a TO address for future reference */ 4009f8829a4aSRandall Stewart if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) { 4010f8829a4aSRandall Stewart if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) { 4011b0552ae2SRandall Stewart sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT, 4012b0552ae2SRandall Stewart SCTP_CALLED_DIRECTLY_NOCMPSET); 4013f8829a4aSRandall Stewart } 4014f8829a4aSRandall Stewart } 4015c58e60beSMichael Tuexen contains_init_chunk = 0; 4016f8829a4aSRandall Stewart ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, 4017f8829a4aSRandall Stewart sizeof(*ch), (uint8_t *) & chunk_buf); 4018f8829a4aSRandall Stewart while (ch != NULL) { 4019f8829a4aSRandall Stewart chk_length = ntohs(ch->chunk_length); 4020f8829a4aSRandall Stewart if (chk_length < sizeof(*ch)) { 4021f8829a4aSRandall Stewart /* break to abort land */ 4022f8829a4aSRandall Stewart break; 4023f8829a4aSRandall Stewart } 4024f8829a4aSRandall Stewart switch (ch->chunk_type) { 4025c58e60beSMichael Tuexen case SCTP_INIT: 4026c58e60beSMichael Tuexen contains_init_chunk = 1; 4027c58e60beSMichael Tuexen break; 4028d55b0b1bSRandall Stewart case SCTP_COOKIE_ECHO: 4029d55b0b1bSRandall Stewart /* We hit here only if the assoc is being freed */ 4030d55b0b1bSRandall Stewart return; 4031f8829a4aSRandall Stewart case SCTP_PACKET_DROPPED: 4032f8829a4aSRandall Stewart /* we don't respond to pkt-dropped */ 4033f8829a4aSRandall Stewart return; 4034f8829a4aSRandall Stewart case SCTP_ABORT_ASSOCIATION: 4035f8829a4aSRandall Stewart /* we don't respond with an ABORT to an ABORT */ 4036f8829a4aSRandall Stewart return; 4037f8829a4aSRandall Stewart case SCTP_SHUTDOWN_COMPLETE: 4038f8829a4aSRandall Stewart /* 4039f8829a4aSRandall Stewart * we ignore it since we are not waiting for it and 4040f8829a4aSRandall Stewart * peer is gone 4041f8829a4aSRandall Stewart */ 4042f8829a4aSRandall Stewart return; 4043f8829a4aSRandall Stewart case SCTP_SHUTDOWN_ACK: 4044b1754ad1SMichael Tuexen sctp_send_shutdown_complete2(src, dst, sh, 4045f30ac432SMichael Tuexen use_mflowid, mflowid, 4046f30ac432SMichael Tuexen vrf_id, port); 4047f8829a4aSRandall Stewart return; 4048f8829a4aSRandall Stewart default: 4049f8829a4aSRandall Stewart break; 4050f8829a4aSRandall Stewart } 4051f8829a4aSRandall Stewart offset += SCTP_SIZE32(chk_length); 4052f8829a4aSRandall Stewart ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, 4053f8829a4aSRandall Stewart sizeof(*ch), (uint8_t *) & chunk_buf); 4054f8829a4aSRandall Stewart } 4055c58e60beSMichael Tuexen if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) || 4056c58e60beSMichael Tuexen ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) && 4057c58e60beSMichael Tuexen (contains_init_chunk == 0))) { 4058b1754ad1SMichael Tuexen sctp_send_abort(m, iphlen, src, dst, sh, 0, NULL, 4059f30ac432SMichael Tuexen use_mflowid, mflowid, 4060f30ac432SMichael Tuexen vrf_id, port); 4061f8829a4aSRandall Stewart } 4062c58e60beSMichael Tuexen } 4063f8829a4aSRandall Stewart 4064f8829a4aSRandall Stewart /* 4065f8829a4aSRandall Stewart * check the inbound datagram to make sure there is not an abort inside it, 4066f8829a4aSRandall Stewart * if there is return 1, else return 0. 4067f8829a4aSRandall Stewart */ 4068f8829a4aSRandall Stewart int 4069f8829a4aSRandall Stewart sctp_is_there_an_abort_here(struct mbuf *m, int iphlen, uint32_t * vtagfill) 4070f8829a4aSRandall Stewart { 4071f8829a4aSRandall Stewart struct sctp_chunkhdr *ch; 4072f8829a4aSRandall Stewart struct sctp_init_chunk *init_chk, chunk_buf; 4073f8829a4aSRandall Stewart int offset; 4074f8829a4aSRandall Stewart unsigned int chk_length; 4075f8829a4aSRandall Stewart 4076f8829a4aSRandall Stewart offset = iphlen + sizeof(struct sctphdr); 4077f8829a4aSRandall Stewart ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch), 4078f8829a4aSRandall Stewart (uint8_t *) & chunk_buf); 4079f8829a4aSRandall Stewart while (ch != NULL) { 4080f8829a4aSRandall Stewart chk_length = ntohs(ch->chunk_length); 4081f8829a4aSRandall Stewart if (chk_length < sizeof(*ch)) { 4082f8829a4aSRandall Stewart /* packet is probably corrupt */ 4083f8829a4aSRandall Stewart break; 4084f8829a4aSRandall Stewart } 4085f8829a4aSRandall Stewart /* we seem to be ok, is it an abort? */ 4086f8829a4aSRandall Stewart if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) { 4087f8829a4aSRandall Stewart /* yep, tell them */ 4088f8829a4aSRandall Stewart return (1); 4089f8829a4aSRandall Stewart } 4090f8829a4aSRandall Stewart if (ch->chunk_type == SCTP_INITIATION) { 4091f8829a4aSRandall Stewart /* need to update the Vtag */ 4092f8829a4aSRandall Stewart init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 4093f8829a4aSRandall Stewart offset, sizeof(*init_chk), (uint8_t *) & chunk_buf); 4094f8829a4aSRandall Stewart if (init_chk != NULL) { 4095f8829a4aSRandall Stewart *vtagfill = ntohl(init_chk->init.initiate_tag); 4096f8829a4aSRandall Stewart } 4097f8829a4aSRandall Stewart } 4098f8829a4aSRandall Stewart /* Nope, move to the next chunk */ 4099f8829a4aSRandall Stewart offset += SCTP_SIZE32(chk_length); 4100f8829a4aSRandall Stewart ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, 4101f8829a4aSRandall Stewart sizeof(*ch), (uint8_t *) & chunk_buf); 4102f8829a4aSRandall Stewart } 4103f8829a4aSRandall Stewart return (0); 4104f8829a4aSRandall Stewart } 4105f8829a4aSRandall Stewart 4106f8829a4aSRandall Stewart /* 4107f8829a4aSRandall Stewart * currently (2/02), ifa_addr embeds scope_id's and don't have sin6_scope_id 4108f8829a4aSRandall Stewart * set (i.e. it's 0) so, create this function to compare link local scopes 4109f8829a4aSRandall Stewart */ 41105e2c2d87SRandall Stewart #ifdef INET6 4111f8829a4aSRandall Stewart uint32_t 4112f8829a4aSRandall Stewart sctp_is_same_scope(struct sockaddr_in6 *addr1, struct sockaddr_in6 *addr2) 4113f8829a4aSRandall Stewart { 4114f8829a4aSRandall Stewart struct sockaddr_in6 a, b; 4115f8829a4aSRandall Stewart 4116f8829a4aSRandall Stewart /* save copies */ 4117f8829a4aSRandall Stewart a = *addr1; 4118f8829a4aSRandall Stewart b = *addr2; 4119f8829a4aSRandall Stewart 4120f8829a4aSRandall Stewart if (a.sin6_scope_id == 0) 4121f8829a4aSRandall Stewart if (sa6_recoverscope(&a)) { 4122f8829a4aSRandall Stewart /* can't get scope, so can't match */ 4123f8829a4aSRandall Stewart return (0); 4124f8829a4aSRandall Stewart } 4125f8829a4aSRandall Stewart if (b.sin6_scope_id == 0) 4126f8829a4aSRandall Stewart if (sa6_recoverscope(&b)) { 4127f8829a4aSRandall Stewart /* can't get scope, so can't match */ 4128f8829a4aSRandall Stewart return (0); 4129f8829a4aSRandall Stewart } 4130f8829a4aSRandall Stewart if (a.sin6_scope_id != b.sin6_scope_id) 4131f8829a4aSRandall Stewart return (0); 4132f8829a4aSRandall Stewart 4133f8829a4aSRandall Stewart return (1); 4134f8829a4aSRandall Stewart } 4135f8829a4aSRandall Stewart 4136f8829a4aSRandall Stewart /* 4137f8829a4aSRandall Stewart * returns a sockaddr_in6 with embedded scope recovered and removed 4138f8829a4aSRandall Stewart */ 4139f8829a4aSRandall Stewart struct sockaddr_in6 * 4140f8829a4aSRandall Stewart sctp_recover_scope(struct sockaddr_in6 *addr, struct sockaddr_in6 *store) 4141f8829a4aSRandall Stewart { 4142f8829a4aSRandall Stewart /* check and strip embedded scope junk */ 4143f8829a4aSRandall Stewart if (addr->sin6_family == AF_INET6) { 4144f8829a4aSRandall Stewart if (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr)) { 4145f8829a4aSRandall Stewart if (addr->sin6_scope_id == 0) { 4146f8829a4aSRandall Stewart *store = *addr; 4147f8829a4aSRandall Stewart if (!sa6_recoverscope(store)) { 4148f8829a4aSRandall Stewart /* use the recovered scope */ 4149f8829a4aSRandall Stewart addr = store; 4150f8829a4aSRandall Stewart } 4151f42a358aSRandall Stewart } else { 4152f8829a4aSRandall Stewart /* else, return the original "to" addr */ 4153f42a358aSRandall Stewart in6_clearscope(&addr->sin6_addr); 4154f8829a4aSRandall Stewart } 4155f8829a4aSRandall Stewart } 4156f8829a4aSRandall Stewart } 4157f8829a4aSRandall Stewart return (addr); 4158f8829a4aSRandall Stewart } 4159f8829a4aSRandall Stewart 41605e2c2d87SRandall Stewart #endif 41615e2c2d87SRandall Stewart 4162f8829a4aSRandall Stewart /* 4163f8829a4aSRandall Stewart * are the two addresses the same? currently a "scopeless" check returns: 1 4164f8829a4aSRandall Stewart * if same, 0 if not 4165f8829a4aSRandall Stewart */ 416672fb6fdbSRandall Stewart int 4167f8829a4aSRandall Stewart sctp_cmpaddr(struct sockaddr *sa1, struct sockaddr *sa2) 4168f8829a4aSRandall Stewart { 4169f8829a4aSRandall Stewart 4170f8829a4aSRandall Stewart /* must be valid */ 4171f8829a4aSRandall Stewart if (sa1 == NULL || sa2 == NULL) 4172f8829a4aSRandall Stewart return (0); 4173f8829a4aSRandall Stewart 4174f8829a4aSRandall Stewart /* must be the same family */ 4175f8829a4aSRandall Stewart if (sa1->sa_family != sa2->sa_family) 4176f8829a4aSRandall Stewart return (0); 4177f8829a4aSRandall Stewart 41785e2c2d87SRandall Stewart switch (sa1->sa_family) { 41795e2c2d87SRandall Stewart #ifdef INET6 41805e2c2d87SRandall Stewart case AF_INET6: 41815e2c2d87SRandall Stewart { 4182f8829a4aSRandall Stewart /* IPv6 addresses */ 4183f8829a4aSRandall Stewart struct sockaddr_in6 *sin6_1, *sin6_2; 4184f8829a4aSRandall Stewart 4185f8829a4aSRandall Stewart sin6_1 = (struct sockaddr_in6 *)sa1; 4186f8829a4aSRandall Stewart sin6_2 = (struct sockaddr_in6 *)sa2; 4187c54a18d2SRandall Stewart return (SCTP6_ARE_ADDR_EQUAL(sin6_1, 4188c54a18d2SRandall Stewart sin6_2)); 41895e2c2d87SRandall Stewart } 41905e2c2d87SRandall Stewart #endif 4191ea5eba11SMichael Tuexen #ifdef INET 41925e2c2d87SRandall Stewart case AF_INET: 41935e2c2d87SRandall Stewart { 4194f8829a4aSRandall Stewart /* IPv4 addresses */ 4195f8829a4aSRandall Stewart struct sockaddr_in *sin_1, *sin_2; 4196f8829a4aSRandall Stewart 4197f8829a4aSRandall Stewart sin_1 = (struct sockaddr_in *)sa1; 4198f8829a4aSRandall Stewart sin_2 = (struct sockaddr_in *)sa2; 4199f8829a4aSRandall Stewart return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr); 42005e2c2d87SRandall Stewart } 4201ea5eba11SMichael Tuexen #endif 42025e2c2d87SRandall Stewart default: 4203f8829a4aSRandall Stewart /* we don't do these... */ 4204f8829a4aSRandall Stewart return (0); 4205f8829a4aSRandall Stewart } 4206f8829a4aSRandall Stewart } 4207f8829a4aSRandall Stewart 4208f8829a4aSRandall Stewart void 4209f8829a4aSRandall Stewart sctp_print_address(struct sockaddr *sa) 4210f8829a4aSRandall Stewart { 42115e2c2d87SRandall Stewart #ifdef INET6 42127d32aa0cSBjoern A. Zeeb char ip6buf[INET6_ADDRSTRLEN]; 4213f8829a4aSRandall Stewart 42145e2c2d87SRandall Stewart #endif 42155e2c2d87SRandall Stewart 42165e2c2d87SRandall Stewart switch (sa->sa_family) { 42175e2c2d87SRandall Stewart #ifdef INET6 42185e2c2d87SRandall Stewart case AF_INET6: 42195e2c2d87SRandall Stewart { 4220ad81507eSRandall Stewart struct sockaddr_in6 *sin6; 4221ad81507eSRandall Stewart 4222f8829a4aSRandall Stewart sin6 = (struct sockaddr_in6 *)sa; 4223ad81507eSRandall Stewart SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n", 42247d32aa0cSBjoern A. Zeeb ip6_sprintf(ip6buf, &sin6->sin6_addr), 42257d32aa0cSBjoern A. Zeeb ntohs(sin6->sin6_port), 4226f8829a4aSRandall Stewart sin6->sin6_scope_id); 42275e2c2d87SRandall Stewart break; 42285e2c2d87SRandall Stewart } 42295e2c2d87SRandall Stewart #endif 4230ea5eba11SMichael Tuexen #ifdef INET 42315e2c2d87SRandall Stewart case AF_INET: 42325e2c2d87SRandall Stewart { 4233f8829a4aSRandall Stewart struct sockaddr_in *sin; 4234f8829a4aSRandall Stewart unsigned char *p; 4235f8829a4aSRandall Stewart 4236f8829a4aSRandall Stewart sin = (struct sockaddr_in *)sa; 4237f8829a4aSRandall Stewart p = (unsigned char *)&sin->sin_addr; 4238ad81507eSRandall Stewart SCTP_PRINTF("IPv4 address: %u.%u.%u.%u:%d\n", 4239f8829a4aSRandall Stewart p[0], p[1], p[2], p[3], ntohs(sin->sin_port)); 42405e2c2d87SRandall Stewart break; 42415e2c2d87SRandall Stewart } 4242ea5eba11SMichael Tuexen #endif 42435e2c2d87SRandall Stewart default: 4244ad81507eSRandall Stewart SCTP_PRINTF("?\n"); 42455e2c2d87SRandall Stewart break; 4246f8829a4aSRandall Stewart } 4247f8829a4aSRandall Stewart } 4248f8829a4aSRandall Stewart 4249f8829a4aSRandall Stewart void 4250f8829a4aSRandall Stewart sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp, 4251f8829a4aSRandall Stewart struct sctp_inpcb *new_inp, 4252d06c82f1SRandall Stewart struct sctp_tcb *stcb, 4253d06c82f1SRandall Stewart int waitflags) 4254f8829a4aSRandall Stewart { 4255f8829a4aSRandall Stewart /* 4256f8829a4aSRandall Stewart * go through our old INP and pull off any control structures that 4257f8829a4aSRandall Stewart * belong to stcb and move then to the new inp. 4258f8829a4aSRandall Stewart */ 4259f8829a4aSRandall Stewart struct socket *old_so, *new_so; 4260f8829a4aSRandall Stewart struct sctp_queued_to_read *control, *nctl; 4261f8829a4aSRandall Stewart struct sctp_readhead tmp_queue; 4262f8829a4aSRandall Stewart struct mbuf *m; 4263bff64a4dSRandall Stewart int error = 0; 4264f8829a4aSRandall Stewart 4265f8829a4aSRandall Stewart old_so = old_inp->sctp_socket; 4266f8829a4aSRandall Stewart new_so = new_inp->sctp_socket; 4267f8829a4aSRandall Stewart TAILQ_INIT(&tmp_queue); 4268d06c82f1SRandall Stewart error = sblock(&old_so->so_rcv, waitflags); 4269f8829a4aSRandall Stewart if (error) { 4270f8829a4aSRandall Stewart /* 4271f8829a4aSRandall Stewart * Gak, can't get sblock, we have a problem. data will be 4272f8829a4aSRandall Stewart * left stranded.. and we don't dare look at it since the 4273f8829a4aSRandall Stewart * other thread may be reading something. Oh well, its a 4274f8829a4aSRandall Stewart * screwed up app that does a peeloff OR a accept while 4275f8829a4aSRandall Stewart * reading from the main socket... actually its only the 4276f8829a4aSRandall Stewart * peeloff() case, since I think read will fail on a 4277f8829a4aSRandall Stewart * listening socket.. 4278f8829a4aSRandall Stewart */ 4279f8829a4aSRandall Stewart return; 4280f8829a4aSRandall Stewart } 4281f8829a4aSRandall Stewart /* lock the socket buffers */ 4282f8829a4aSRandall Stewart SCTP_INP_READ_LOCK(old_inp); 42834a9ef3f8SMichael Tuexen TAILQ_FOREACH_SAFE(control, &old_inp->read_queue, next, nctl) { 4284f8829a4aSRandall Stewart /* Pull off all for out target stcb */ 4285f8829a4aSRandall Stewart if (control->stcb == stcb) { 4286f8829a4aSRandall Stewart /* remove it we want it */ 4287f8829a4aSRandall Stewart TAILQ_REMOVE(&old_inp->read_queue, control, next); 4288f8829a4aSRandall Stewart TAILQ_INSERT_TAIL(&tmp_queue, control, next); 4289f8829a4aSRandall Stewart m = control->data; 4290f8829a4aSRandall Stewart while (m) { 4291b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { 4292139bc87fSRandall Stewart sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m)); 429380fefe0aSRandall Stewart } 4294f8829a4aSRandall Stewart sctp_sbfree(control, stcb, &old_so->so_rcv, m); 4295b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { 4296f8829a4aSRandall Stewart sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0); 429780fefe0aSRandall Stewart } 4298139bc87fSRandall Stewart m = SCTP_BUF_NEXT(m); 4299f8829a4aSRandall Stewart } 4300f8829a4aSRandall Stewart } 4301f8829a4aSRandall Stewart } 4302f8829a4aSRandall Stewart SCTP_INP_READ_UNLOCK(old_inp); 4303f8829a4aSRandall Stewart /* Remove the sb-lock on the old socket */ 4304f8829a4aSRandall Stewart 4305f8829a4aSRandall Stewart sbunlock(&old_so->so_rcv); 4306f8829a4aSRandall Stewart /* Now we move them over to the new socket buffer */ 4307f8829a4aSRandall Stewart SCTP_INP_READ_LOCK(new_inp); 43084a9ef3f8SMichael Tuexen TAILQ_FOREACH_SAFE(control, &tmp_queue, next, nctl) { 4309f8829a4aSRandall Stewart TAILQ_INSERT_TAIL(&new_inp->read_queue, control, next); 4310f8829a4aSRandall Stewart m = control->data; 4311f8829a4aSRandall Stewart while (m) { 4312b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { 4313139bc87fSRandall Stewart sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m)); 431480fefe0aSRandall Stewart } 4315f8829a4aSRandall Stewart sctp_sballoc(stcb, &new_so->so_rcv, m); 4316b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { 4317f8829a4aSRandall Stewart sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0); 431880fefe0aSRandall Stewart } 4319139bc87fSRandall Stewart m = SCTP_BUF_NEXT(m); 4320f8829a4aSRandall Stewart } 4321f8829a4aSRandall Stewart } 4322f8829a4aSRandall Stewart SCTP_INP_READ_UNLOCK(new_inp); 4323f8829a4aSRandall Stewart } 4324f8829a4aSRandall Stewart 4325f8829a4aSRandall Stewart void 4326f8829a4aSRandall Stewart sctp_add_to_readq(struct sctp_inpcb *inp, 4327f8829a4aSRandall Stewart struct sctp_tcb *stcb, 4328f8829a4aSRandall Stewart struct sctp_queued_to_read *control, 4329f8829a4aSRandall Stewart struct sockbuf *sb, 4330ceaad40aSRandall Stewart int end, 4331cfde3ff7SRandall Stewart int inp_read_lock_held, 4332ceaad40aSRandall Stewart int so_locked 4333ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 4334ceaad40aSRandall Stewart SCTP_UNUSED 4335ceaad40aSRandall Stewart #endif 4336ceaad40aSRandall Stewart ) 4337f8829a4aSRandall Stewart { 4338f8829a4aSRandall Stewart /* 4339f8829a4aSRandall Stewart * Here we must place the control on the end of the socket read 4340f8829a4aSRandall Stewart * queue AND increment sb_cc so that select will work properly on 4341f8829a4aSRandall Stewart * read. 4342f8829a4aSRandall Stewart */ 4343f8829a4aSRandall Stewart struct mbuf *m, *prev = NULL; 4344f8829a4aSRandall Stewart 434503b0b021SRandall Stewart if (inp == NULL) { 434603b0b021SRandall Stewart /* Gak, TSNH!! */ 4347a5d547adSRandall Stewart #ifdef INVARIANTS 434803b0b021SRandall Stewart panic("Gak, inp NULL on add_to_readq"); 434903b0b021SRandall Stewart #endif 435003b0b021SRandall Stewart return; 435103b0b021SRandall Stewart } 4352cfde3ff7SRandall Stewart if (inp_read_lock_held == 0) 4353f8829a4aSRandall Stewart SCTP_INP_READ_LOCK(inp); 4354cd1386abSMichael Tuexen if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) { 4355cd1386abSMichael Tuexen sctp_free_remote_addr(control->whoFrom); 4356cd1386abSMichael Tuexen if (control->data) { 4357cd1386abSMichael Tuexen sctp_m_freem(control->data); 4358cd1386abSMichael Tuexen control->data = NULL; 4359cd1386abSMichael Tuexen } 4360cd1386abSMichael Tuexen SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), control); 4361cd1386abSMichael Tuexen if (inp_read_lock_held == 0) 4362cd1386abSMichael Tuexen SCTP_INP_READ_UNLOCK(inp); 4363cd1386abSMichael Tuexen return; 4364cd1386abSMichael Tuexen } 436542551e99SRandall Stewart if (!(control->spec_flags & M_NOTIFICATION)) { 4366a5d547adSRandall Stewart atomic_add_int(&inp->total_recvs, 1); 436742551e99SRandall Stewart if (!control->do_not_ref_stcb) { 4368a5d547adSRandall Stewart atomic_add_int(&stcb->total_recvs, 1); 436942551e99SRandall Stewart } 437042551e99SRandall Stewart } 4371f8829a4aSRandall Stewart m = control->data; 4372f8829a4aSRandall Stewart control->held_length = 0; 4373f8829a4aSRandall Stewart control->length = 0; 4374f8829a4aSRandall Stewart while (m) { 4375139bc87fSRandall Stewart if (SCTP_BUF_LEN(m) == 0) { 4376f8829a4aSRandall Stewart /* Skip mbufs with NO length */ 4377f8829a4aSRandall Stewart if (prev == NULL) { 4378f8829a4aSRandall Stewart /* First one */ 4379f8829a4aSRandall Stewart control->data = sctp_m_free(m); 4380f8829a4aSRandall Stewart m = control->data; 4381f8829a4aSRandall Stewart } else { 4382139bc87fSRandall Stewart SCTP_BUF_NEXT(prev) = sctp_m_free(m); 4383139bc87fSRandall Stewart m = SCTP_BUF_NEXT(prev); 4384f8829a4aSRandall Stewart } 4385f8829a4aSRandall Stewart if (m == NULL) { 4386c2ede4b3SMartin Blapp control->tail_mbuf = prev; 4387f8829a4aSRandall Stewart } 4388f8829a4aSRandall Stewart continue; 4389f8829a4aSRandall Stewart } 4390f8829a4aSRandall Stewart prev = m; 4391b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { 4392139bc87fSRandall Stewart sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m)); 439380fefe0aSRandall Stewart } 4394f8829a4aSRandall Stewart sctp_sballoc(stcb, sb, m); 4395b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { 4396f8829a4aSRandall Stewart sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0); 439780fefe0aSRandall Stewart } 4398139bc87fSRandall Stewart atomic_add_int(&control->length, SCTP_BUF_LEN(m)); 4399139bc87fSRandall Stewart m = SCTP_BUF_NEXT(m); 4400f8829a4aSRandall Stewart } 4401f8829a4aSRandall Stewart if (prev != NULL) { 4402f8829a4aSRandall Stewart control->tail_mbuf = prev; 4403f8829a4aSRandall Stewart } else { 4404139bc87fSRandall Stewart /* Everything got collapsed out?? */ 4405cd1386abSMichael Tuexen sctp_free_remote_addr(control->whoFrom); 4406cd1386abSMichael Tuexen SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), control); 4407cfde3ff7SRandall Stewart if (inp_read_lock_held == 0) 440847a490cbSMichael Tuexen SCTP_INP_READ_UNLOCK(inp); 4409f8829a4aSRandall Stewart return; 4410f8829a4aSRandall Stewart } 4411f8829a4aSRandall Stewart if (end) { 4412f8829a4aSRandall Stewart control->end_added = 1; 4413f8829a4aSRandall Stewart } 4414f8829a4aSRandall Stewart TAILQ_INSERT_TAIL(&inp->read_queue, control, next); 4415cfde3ff7SRandall Stewart if (inp_read_lock_held == 0) 4416f8829a4aSRandall Stewart SCTP_INP_READ_UNLOCK(inp); 4417f8829a4aSRandall Stewart if (inp && inp->sctp_socket) { 441817205eccSRandall Stewart if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) { 441917205eccSRandall Stewart SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket); 4420ceaad40aSRandall Stewart } else { 4421ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4422ceaad40aSRandall Stewart struct socket *so; 4423ceaad40aSRandall Stewart 4424ceaad40aSRandall Stewart so = SCTP_INP_SO(inp); 4425ceaad40aSRandall Stewart if (!so_locked) { 442660990c0cSMichael Tuexen if (stcb) { 4427ceaad40aSRandall Stewart atomic_add_int(&stcb->asoc.refcnt, 1); 4428ceaad40aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 442960990c0cSMichael Tuexen } 4430ceaad40aSRandall Stewart SCTP_SOCKET_LOCK(so, 1); 443160990c0cSMichael Tuexen if (stcb) { 4432ceaad40aSRandall Stewart SCTP_TCB_LOCK(stcb); 4433ceaad40aSRandall Stewart atomic_subtract_int(&stcb->asoc.refcnt, 1); 443460990c0cSMichael Tuexen } 4435ceaad40aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4436ceaad40aSRandall Stewart SCTP_SOCKET_UNLOCK(so, 1); 4437ceaad40aSRandall Stewart return; 4438ceaad40aSRandall Stewart } 4439ceaad40aSRandall Stewart } 4440ceaad40aSRandall Stewart #endif 4441f8829a4aSRandall Stewart sctp_sorwakeup(inp, inp->sctp_socket); 4442ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4443ceaad40aSRandall Stewart if (!so_locked) { 4444ceaad40aSRandall Stewart SCTP_SOCKET_UNLOCK(so, 1); 4445ceaad40aSRandall Stewart } 4446ceaad40aSRandall Stewart #endif 4447ceaad40aSRandall Stewart } 4448f8829a4aSRandall Stewart } 4449f8829a4aSRandall Stewart } 4450f8829a4aSRandall Stewart 4451f8829a4aSRandall Stewart 4452f8829a4aSRandall Stewart int 4453f8829a4aSRandall Stewart sctp_append_to_readq(struct sctp_inpcb *inp, 4454f8829a4aSRandall Stewart struct sctp_tcb *stcb, 4455f8829a4aSRandall Stewart struct sctp_queued_to_read *control, 4456f8829a4aSRandall Stewart struct mbuf *m, 4457f8829a4aSRandall Stewart int end, 4458f8829a4aSRandall Stewart int ctls_cumack, 4459f8829a4aSRandall Stewart struct sockbuf *sb) 4460f8829a4aSRandall Stewart { 4461f8829a4aSRandall Stewart /* 4462f8829a4aSRandall Stewart * A partial delivery API event is underway. OR we are appending on 4463f8829a4aSRandall Stewart * the reassembly queue. 4464f8829a4aSRandall Stewart * 4465f8829a4aSRandall Stewart * If PDAPI this means we need to add m to the end of the data. 4466f8829a4aSRandall Stewart * Increase the length in the control AND increment the sb_cc. 4467f8829a4aSRandall Stewart * Otherwise sb is NULL and all we need to do is put it at the end 4468f8829a4aSRandall Stewart * of the mbuf chain. 4469f8829a4aSRandall Stewart */ 4470f8829a4aSRandall Stewart int len = 0; 4471f8829a4aSRandall Stewart struct mbuf *mm, *tail = NULL, *prev = NULL; 4472f8829a4aSRandall Stewart 4473f8829a4aSRandall Stewart if (inp) { 4474f8829a4aSRandall Stewart SCTP_INP_READ_LOCK(inp); 4475f8829a4aSRandall Stewart } 4476f8829a4aSRandall Stewart if (control == NULL) { 4477f8829a4aSRandall Stewart get_out: 4478f8829a4aSRandall Stewart if (inp) { 4479f8829a4aSRandall Stewart SCTP_INP_READ_UNLOCK(inp); 4480f8829a4aSRandall Stewart } 4481f8829a4aSRandall Stewart return (-1); 4482f8829a4aSRandall Stewart } 4483cd1386abSMichael Tuexen if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ)) { 4484cd1386abSMichael Tuexen SCTP_INP_READ_UNLOCK(inp); 448560990c0cSMichael Tuexen return (0); 4486cd1386abSMichael Tuexen } 4487139bc87fSRandall Stewart if (control->end_added) { 4488f8829a4aSRandall Stewart /* huh this one is complete? */ 4489f8829a4aSRandall Stewart goto get_out; 4490f8829a4aSRandall Stewart } 4491f8829a4aSRandall Stewart mm = m; 4492f8829a4aSRandall Stewart if (mm == NULL) { 4493f8829a4aSRandall Stewart goto get_out; 4494f8829a4aSRandall Stewart } 4495f8829a4aSRandall Stewart while (mm) { 4496139bc87fSRandall Stewart if (SCTP_BUF_LEN(mm) == 0) { 4497f8829a4aSRandall Stewart /* Skip mbufs with NO lenght */ 4498f8829a4aSRandall Stewart if (prev == NULL) { 4499f8829a4aSRandall Stewart /* First one */ 4500f8829a4aSRandall Stewart m = sctp_m_free(mm); 4501f8829a4aSRandall Stewart mm = m; 4502f8829a4aSRandall Stewart } else { 4503139bc87fSRandall Stewart SCTP_BUF_NEXT(prev) = sctp_m_free(mm); 4504139bc87fSRandall Stewart mm = SCTP_BUF_NEXT(prev); 4505f8829a4aSRandall Stewart } 4506f8829a4aSRandall Stewart continue; 4507f8829a4aSRandall Stewart } 4508f8829a4aSRandall Stewart prev = mm; 4509139bc87fSRandall Stewart len += SCTP_BUF_LEN(mm); 4510f8829a4aSRandall Stewart if (sb) { 4511b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { 4512139bc87fSRandall Stewart sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(mm)); 451380fefe0aSRandall Stewart } 4514f8829a4aSRandall Stewart sctp_sballoc(stcb, sb, mm); 4515b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { 4516f8829a4aSRandall Stewart sctp_sblog(sb, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0); 451780fefe0aSRandall Stewart } 4518f8829a4aSRandall Stewart } 4519139bc87fSRandall Stewart mm = SCTP_BUF_NEXT(mm); 4520f8829a4aSRandall Stewart } 4521f8829a4aSRandall Stewart if (prev) { 4522f8829a4aSRandall Stewart tail = prev; 4523f8829a4aSRandall Stewart } else { 4524f8829a4aSRandall Stewart /* Really there should always be a prev */ 4525f8829a4aSRandall Stewart if (m == NULL) { 4526f8829a4aSRandall Stewart /* Huh nothing left? */ 4527a5d547adSRandall Stewart #ifdef INVARIANTS 4528f8829a4aSRandall Stewart panic("Nothing left to add?"); 4529f8829a4aSRandall Stewart #else 4530f8829a4aSRandall Stewart goto get_out; 4531f8829a4aSRandall Stewart #endif 4532f8829a4aSRandall Stewart } 4533f8829a4aSRandall Stewart tail = m; 4534f8829a4aSRandall Stewart } 4535f8829a4aSRandall Stewart if (control->tail_mbuf) { 4536f8829a4aSRandall Stewart /* append */ 4537139bc87fSRandall Stewart SCTP_BUF_NEXT(control->tail_mbuf) = m; 4538f8829a4aSRandall Stewart control->tail_mbuf = tail; 4539f8829a4aSRandall Stewart } else { 4540f8829a4aSRandall Stewart /* nothing there */ 4541a5d547adSRandall Stewart #ifdef INVARIANTS 4542f8829a4aSRandall Stewart if (control->data != NULL) { 4543f8829a4aSRandall Stewart panic("This should NOT happen"); 4544f8829a4aSRandall Stewart } 4545f8829a4aSRandall Stewart #endif 4546f8829a4aSRandall Stewart control->data = m; 4547f8829a4aSRandall Stewart control->tail_mbuf = tail; 4548f8829a4aSRandall Stewart } 454918e198d3SRandall Stewart atomic_add_int(&control->length, len); 455018e198d3SRandall Stewart if (end) { 455118e198d3SRandall Stewart /* message is complete */ 455218e198d3SRandall Stewart if (stcb && (control == stcb->asoc.control_pdapi)) { 455318e198d3SRandall Stewart stcb->asoc.control_pdapi = NULL; 455418e198d3SRandall Stewart } 455518e198d3SRandall Stewart control->held_length = 0; 455618e198d3SRandall Stewart control->end_added = 1; 455718e198d3SRandall Stewart } 4558ad81507eSRandall Stewart if (stcb == NULL) { 4559ad81507eSRandall Stewart control->do_not_ref_stcb = 1; 4560ad81507eSRandall Stewart } 4561f8829a4aSRandall Stewart /* 4562f8829a4aSRandall Stewart * When we are appending in partial delivery, the cum-ack is used 4563f8829a4aSRandall Stewart * for the actual pd-api highest tsn on this mbuf. The true cum-ack 4564f8829a4aSRandall Stewart * is populated in the outbound sinfo structure from the true cumack 4565f8829a4aSRandall Stewart * if the association exists... 4566f8829a4aSRandall Stewart */ 4567f8829a4aSRandall Stewart control->sinfo_tsn = control->sinfo_cumtsn = ctls_cumack; 4568f8829a4aSRandall Stewart if (inp) { 4569f8829a4aSRandall Stewart SCTP_INP_READ_UNLOCK(inp); 4570f8829a4aSRandall Stewart } 4571f8829a4aSRandall Stewart if (inp && inp->sctp_socket) { 457217205eccSRandall Stewart if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE)) { 457317205eccSRandall Stewart SCTP_ZERO_COPY_EVENT(inp, inp->sctp_socket); 4574ceaad40aSRandall Stewart } else { 4575ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4576ceaad40aSRandall Stewart struct socket *so; 4577ceaad40aSRandall Stewart 4578ceaad40aSRandall Stewart so = SCTP_INP_SO(inp); 457960990c0cSMichael Tuexen if (stcb) { 4580ceaad40aSRandall Stewart atomic_add_int(&stcb->asoc.refcnt, 1); 4581ceaad40aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 458260990c0cSMichael Tuexen } 4583ceaad40aSRandall Stewart SCTP_SOCKET_LOCK(so, 1); 458460990c0cSMichael Tuexen if (stcb) { 4585ceaad40aSRandall Stewart SCTP_TCB_LOCK(stcb); 4586ceaad40aSRandall Stewart atomic_subtract_int(&stcb->asoc.refcnt, 1); 458760990c0cSMichael Tuexen } 4588ceaad40aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 4589ceaad40aSRandall Stewart SCTP_SOCKET_UNLOCK(so, 1); 4590ceaad40aSRandall Stewart return (0); 4591ceaad40aSRandall Stewart } 4592ceaad40aSRandall Stewart #endif 4593f8829a4aSRandall Stewart sctp_sorwakeup(inp, inp->sctp_socket); 4594ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4595ceaad40aSRandall Stewart SCTP_SOCKET_UNLOCK(so, 1); 4596ceaad40aSRandall Stewart #endif 4597ceaad40aSRandall Stewart } 4598f8829a4aSRandall Stewart } 4599f8829a4aSRandall Stewart return (0); 4600f8829a4aSRandall Stewart } 4601f8829a4aSRandall Stewart 4602f8829a4aSRandall Stewart 4603f8829a4aSRandall Stewart 4604f8829a4aSRandall Stewart /*************HOLD THIS COMMENT FOR PATCH FILE OF 4605f8829a4aSRandall Stewart *************ALTERNATE ROUTING CODE 4606f8829a4aSRandall Stewart */ 4607f8829a4aSRandall Stewart 4608f8829a4aSRandall Stewart /*************HOLD THIS COMMENT FOR END OF PATCH FILE OF 4609f8829a4aSRandall Stewart *************ALTERNATE ROUTING CODE 4610f8829a4aSRandall Stewart */ 4611f8829a4aSRandall Stewart 4612f8829a4aSRandall Stewart struct mbuf * 4613f8829a4aSRandall Stewart sctp_generate_invmanparam(int err) 4614f8829a4aSRandall Stewart { 4615f8829a4aSRandall Stewart /* Return a MBUF with a invalid mandatory parameter */ 4616f8829a4aSRandall Stewart struct mbuf *m; 4617f8829a4aSRandall Stewart 4618f8829a4aSRandall Stewart m = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA); 4619f8829a4aSRandall Stewart if (m) { 4620f8829a4aSRandall Stewart struct sctp_paramhdr *ph; 4621f8829a4aSRandall Stewart 4622139bc87fSRandall Stewart SCTP_BUF_LEN(m) = sizeof(struct sctp_paramhdr); 4623f8829a4aSRandall Stewart ph = mtod(m, struct sctp_paramhdr *); 4624f8829a4aSRandall Stewart ph->param_length = htons(sizeof(struct sctp_paramhdr)); 4625f8829a4aSRandall Stewart ph->param_type = htons(err); 4626f8829a4aSRandall Stewart } 4627f8829a4aSRandall Stewart return (m); 4628f8829a4aSRandall Stewart } 4629f8829a4aSRandall Stewart 4630f8829a4aSRandall Stewart #ifdef SCTP_MBCNT_LOGGING 4631f8829a4aSRandall Stewart void 4632f8829a4aSRandall Stewart sctp_free_bufspace(struct sctp_tcb *stcb, struct sctp_association *asoc, 4633f8829a4aSRandall Stewart struct sctp_tmit_chunk *tp1, int chk_cnt) 4634f8829a4aSRandall Stewart { 4635f8829a4aSRandall Stewart if (tp1->data == NULL) { 4636f8829a4aSRandall Stewart return; 4637f8829a4aSRandall Stewart } 4638f8829a4aSRandall Stewart asoc->chunks_on_out_queue -= chk_cnt; 4639b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBCNT_LOGGING_ENABLE) { 4640f8829a4aSRandall Stewart sctp_log_mbcnt(SCTP_LOG_MBCNT_DECREASE, 4641f8829a4aSRandall Stewart asoc->total_output_queue_size, 4642f8829a4aSRandall Stewart tp1->book_size, 4643f8829a4aSRandall Stewart 0, 4644f8829a4aSRandall Stewart tp1->mbcnt); 464580fefe0aSRandall Stewart } 4646f8829a4aSRandall Stewart if (asoc->total_output_queue_size >= tp1->book_size) { 464744b7479bSRandall Stewart atomic_add_int(&asoc->total_output_queue_size, -tp1->book_size); 4648f8829a4aSRandall Stewart } else { 4649f8829a4aSRandall Stewart asoc->total_output_queue_size = 0; 4650f8829a4aSRandall Stewart } 4651f8829a4aSRandall Stewart 4652f8829a4aSRandall Stewart if (stcb->sctp_socket && (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) || 4653f8829a4aSRandall Stewart ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) { 4654f8829a4aSRandall Stewart if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) { 4655f8829a4aSRandall Stewart stcb->sctp_socket->so_snd.sb_cc -= tp1->book_size; 4656f8829a4aSRandall Stewart } else { 4657f8829a4aSRandall Stewart stcb->sctp_socket->so_snd.sb_cc = 0; 4658f8829a4aSRandall Stewart 4659f8829a4aSRandall Stewart } 4660f8829a4aSRandall Stewart } 4661f8829a4aSRandall Stewart } 4662f8829a4aSRandall Stewart 4663f8829a4aSRandall Stewart #endif 4664f8829a4aSRandall Stewart 4665f8829a4aSRandall Stewart int 4666f8829a4aSRandall Stewart sctp_release_pr_sctp_chunk(struct sctp_tcb *stcb, struct sctp_tmit_chunk *tp1, 46671edc9dbaSMichael Tuexen uint8_t sent, int so_locked 4668ceaad40aSRandall Stewart #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 4669ceaad40aSRandall Stewart SCTP_UNUSED 4670ceaad40aSRandall Stewart #endif 4671ceaad40aSRandall Stewart ) 4672f8829a4aSRandall Stewart { 46730c0982b8SRandall Stewart struct sctp_stream_out *strq; 46744a9ef3f8SMichael Tuexen struct sctp_tmit_chunk *chk = NULL, *tp2; 46750c0982b8SRandall Stewart struct sctp_stream_queue_pending *sp; 46760c0982b8SRandall Stewart uint16_t stream = 0, seq = 0; 46770c0982b8SRandall Stewart uint8_t foundeom = 0; 4678f8829a4aSRandall Stewart int ret_sz = 0; 4679f8829a4aSRandall Stewart int notdone; 46800c0982b8SRandall Stewart int do_wakeup_routine = 0; 4681f8829a4aSRandall Stewart 46820c0982b8SRandall Stewart stream = tp1->rec.data.stream_number; 46830c0982b8SRandall Stewart seq = tp1->rec.data.stream_seq; 4684f8829a4aSRandall Stewart do { 4685f8829a4aSRandall Stewart ret_sz += tp1->book_size; 46860c0982b8SRandall Stewart if (tp1->data != NULL) { 46878933fa13SRandall Stewart if (tp1->sent < SCTP_DATAGRAM_RESEND) { 4688830d754dSRandall Stewart sctp_flight_size_decrease(tp1); 4689830d754dSRandall Stewart sctp_total_flight_decrease(stcb, tp1); 46908933fa13SRandall Stewart } 46918933fa13SRandall Stewart sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1); 46920c0982b8SRandall Stewart stcb->asoc.peers_rwnd += tp1->send_size; 46930c0982b8SRandall Stewart stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh); 46941edc9dbaSMichael Tuexen if (sent) { 46951edc9dbaSMichael Tuexen sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked); 46961edc9dbaSMichael Tuexen } else { 46971edc9dbaSMichael Tuexen sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked); 46981edc9dbaSMichael Tuexen } 46992f99457bSMichael Tuexen if (tp1->data) { 4700f8829a4aSRandall Stewart sctp_m_freem(tp1->data); 4701f8829a4aSRandall Stewart tp1->data = NULL; 47022f99457bSMichael Tuexen } 47030c0982b8SRandall Stewart do_wakeup_routine = 1; 4704f8829a4aSRandall Stewart if (PR_SCTP_BUF_ENABLED(tp1->flags)) { 4705f8829a4aSRandall Stewart stcb->asoc.sent_queue_cnt_removeable--; 4706f8829a4aSRandall Stewart } 4707f8829a4aSRandall Stewart } 47088933fa13SRandall Stewart tp1->sent = SCTP_FORWARD_TSN_SKIP; 4709f8829a4aSRandall Stewart if ((tp1->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) == 4710f8829a4aSRandall Stewart SCTP_DATA_NOT_FRAG) { 4711f8829a4aSRandall Stewart /* not frag'ed we ae done */ 4712f8829a4aSRandall Stewart notdone = 0; 4713f8829a4aSRandall Stewart foundeom = 1; 4714f8829a4aSRandall Stewart } else if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) { 4715f8829a4aSRandall Stewart /* end of frag, we are done */ 4716f8829a4aSRandall Stewart notdone = 0; 4717f8829a4aSRandall Stewart foundeom = 1; 4718f8829a4aSRandall Stewart } else { 4719f8829a4aSRandall Stewart /* 4720f8829a4aSRandall Stewart * Its a begin or middle piece, we must mark all of 4721f8829a4aSRandall Stewart * it 4722f8829a4aSRandall Stewart */ 4723f8829a4aSRandall Stewart notdone = 1; 4724f8829a4aSRandall Stewart tp1 = TAILQ_NEXT(tp1, sctp_next); 4725f8829a4aSRandall Stewart } 4726f8829a4aSRandall Stewart } while (tp1 && notdone); 47270c0982b8SRandall Stewart if (foundeom == 0) { 4728f8829a4aSRandall Stewart /* 4729f8829a4aSRandall Stewart * The multi-part message was scattered across the send and 4730f8829a4aSRandall Stewart * sent queue. 4731f8829a4aSRandall Stewart */ 47324a9ef3f8SMichael Tuexen TAILQ_FOREACH_SAFE(tp1, &stcb->asoc.send_queue, sctp_next, tp2) { 47334a9ef3f8SMichael Tuexen if ((tp1->rec.data.stream_number != stream) || 47344a9ef3f8SMichael Tuexen (tp1->rec.data.stream_seq != seq)) { 47354a9ef3f8SMichael Tuexen break; 47364a9ef3f8SMichael Tuexen } 47370c0982b8SRandall Stewart /* 47380c0982b8SRandall Stewart * save to chk in case we have some on stream out 47390c0982b8SRandall Stewart * queue. If so and we have an un-transmitted one we 47400c0982b8SRandall Stewart * don't have to fudge the TSN. 47410c0982b8SRandall Stewart */ 47420c0982b8SRandall Stewart chk = tp1; 47430c0982b8SRandall Stewart ret_sz += tp1->book_size; 47440c0982b8SRandall Stewart sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1); 47451edc9dbaSMichael Tuexen if (sent) { 47461edc9dbaSMichael Tuexen sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked); 47471edc9dbaSMichael Tuexen } else { 47481edc9dbaSMichael Tuexen sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked); 47491edc9dbaSMichael Tuexen } 47502f99457bSMichael Tuexen if (tp1->data) { 47510c0982b8SRandall Stewart sctp_m_freem(tp1->data); 47522f99457bSMichael Tuexen tp1->data = NULL; 47532f99457bSMichael Tuexen } 47548933fa13SRandall Stewart /* No flight involved here book the size to 0 */ 47558933fa13SRandall Stewart tp1->book_size = 0; 47560c0982b8SRandall Stewart if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) { 47570c0982b8SRandall Stewart foundeom = 1; 4758f8829a4aSRandall Stewart } 47590c0982b8SRandall Stewart do_wakeup_routine = 1; 47600c0982b8SRandall Stewart tp1->sent = SCTP_FORWARD_TSN_SKIP; 47610c0982b8SRandall Stewart TAILQ_REMOVE(&stcb->asoc.send_queue, tp1, sctp_next); 47620c0982b8SRandall Stewart /* 47630c0982b8SRandall Stewart * on to the sent queue so we can wait for it to be 47640c0982b8SRandall Stewart * passed by. 47650c0982b8SRandall Stewart */ 47660c0982b8SRandall Stewart TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, tp1, 47670c0982b8SRandall Stewart sctp_next); 47680c0982b8SRandall Stewart stcb->asoc.send_queue_cnt--; 47690c0982b8SRandall Stewart stcb->asoc.sent_queue_cnt++; 47700c0982b8SRandall Stewart } 47710c0982b8SRandall Stewart } 47720c0982b8SRandall Stewart if (foundeom == 0) { 47730c0982b8SRandall Stewart /* 47740c0982b8SRandall Stewart * Still no eom found. That means there is stuff left on the 47750c0982b8SRandall Stewart * stream out queue.. yuck. 47760c0982b8SRandall Stewart */ 47770c0982b8SRandall Stewart SCTP_TCB_SEND_LOCK(stcb); 4778f3b05218SMichael Tuexen strq = &stcb->asoc.strmout[stream]; 4779f3b05218SMichael Tuexen sp = TAILQ_FIRST(&strq->outqueue); 4780f3b05218SMichael Tuexen if (sp != NULL) { 47810c0982b8SRandall Stewart sp->discard_rest = 1; 47820c0982b8SRandall Stewart /* 4783f3b05218SMichael Tuexen * We may need to put a chunk on the queue that 4784f3b05218SMichael Tuexen * holds the TSN that would have been sent with the 4785f3b05218SMichael Tuexen * LAST bit. 47860c0982b8SRandall Stewart */ 47870c0982b8SRandall Stewart if (chk == NULL) { 47880c0982b8SRandall Stewart /* Yep, we have to */ 47890c0982b8SRandall Stewart sctp_alloc_a_chunk(stcb, chk); 47900c0982b8SRandall Stewart if (chk == NULL) { 47910c0982b8SRandall Stewart /* 4792f3b05218SMichael Tuexen * we are hosed. All we can do is 4793f3b05218SMichael Tuexen * nothing.. which will cause an 4794f3b05218SMichael Tuexen * abort if the peer is paying 47950c0982b8SRandall Stewart * attention. 47960c0982b8SRandall Stewart */ 47970c0982b8SRandall Stewart goto oh_well; 47980c0982b8SRandall Stewart } 47990c0982b8SRandall Stewart memset(chk, 0, sizeof(*chk)); 48000c0982b8SRandall Stewart chk->rec.data.rcv_flags = SCTP_DATA_LAST_FRAG; 48010c0982b8SRandall Stewart chk->sent = SCTP_FORWARD_TSN_SKIP; 48020c0982b8SRandall Stewart chk->asoc = &stcb->asoc; 4803f3b05218SMichael Tuexen chk->rec.data.stream_seq = strq->next_sequence_send; 48040c0982b8SRandall Stewart chk->rec.data.stream_number = sp->stream; 48050c0982b8SRandall Stewart chk->rec.data.payloadtype = sp->ppid; 48060c0982b8SRandall Stewart chk->rec.data.context = sp->context; 48070c0982b8SRandall Stewart chk->flags = sp->act_flags; 48089eea4a2dSMichael Tuexen if (sp->net) 48090c0982b8SRandall Stewart chk->whoTo = sp->net; 48109eea4a2dSMichael Tuexen else 48119eea4a2dSMichael Tuexen chk->whoTo = stcb->asoc.primary_destination; 48120c0982b8SRandall Stewart atomic_add_int(&chk->whoTo->ref_count, 1); 48130c0982b8SRandall Stewart chk->rec.data.TSN_seq = atomic_fetchadd_int(&stcb->asoc.sending_seq, 1); 48140c0982b8SRandall Stewart stcb->asoc.pr_sctp_cnt++; 48150c0982b8SRandall Stewart chk->pr_sctp_on = 1; 48160c0982b8SRandall Stewart TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, chk, sctp_next); 48170c0982b8SRandall Stewart stcb->asoc.sent_queue_cnt++; 48188933fa13SRandall Stewart stcb->asoc.pr_sctp_cnt++; 48190c0982b8SRandall Stewart } else { 48200c0982b8SRandall Stewart chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG; 48210c0982b8SRandall Stewart } 4822f3b05218SMichael Tuexen strq->next_sequence_send++; 48230c0982b8SRandall Stewart oh_well: 48240c0982b8SRandall Stewart if (sp->data) { 48250c0982b8SRandall Stewart /* 4826f3b05218SMichael Tuexen * Pull any data to free up the SB and allow 4827f3b05218SMichael Tuexen * sender to "add more" while we will throw 4828f3b05218SMichael Tuexen * away :-) 48290c0982b8SRandall Stewart */ 4830f3b05218SMichael Tuexen sctp_free_spbufspace(stcb, &stcb->asoc, sp); 48310c0982b8SRandall Stewart ret_sz += sp->length; 48320c0982b8SRandall Stewart do_wakeup_routine = 1; 48330c0982b8SRandall Stewart sp->some_taken = 1; 48340c0982b8SRandall Stewart sctp_m_freem(sp->data); 48350c0982b8SRandall Stewart sp->data = NULL; 48360c0982b8SRandall Stewart sp->tail_mbuf = NULL; 4837d07b2ac6SMichael Tuexen sp->length = 0; 48380c0982b8SRandall Stewart } 48390c0982b8SRandall Stewart } 48400c0982b8SRandall Stewart SCTP_TCB_SEND_UNLOCK(stcb); 48410c0982b8SRandall Stewart } 48420c0982b8SRandall Stewart if (do_wakeup_routine) { 48430c0982b8SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 48448933fa13SRandall Stewart struct socket *so; 48458933fa13SRandall Stewart 48460c0982b8SRandall Stewart so = SCTP_INP_SO(stcb->sctp_ep); 48470c0982b8SRandall Stewart if (!so_locked) { 48480c0982b8SRandall Stewart atomic_add_int(&stcb->asoc.refcnt, 1); 48490c0982b8SRandall Stewart SCTP_TCB_UNLOCK(stcb); 48500c0982b8SRandall Stewart SCTP_SOCKET_LOCK(so, 1); 48510c0982b8SRandall Stewart SCTP_TCB_LOCK(stcb); 48520c0982b8SRandall Stewart atomic_subtract_int(&stcb->asoc.refcnt, 1); 48530c0982b8SRandall Stewart if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 48540c0982b8SRandall Stewart /* assoc was freed while we were unlocked */ 48550c0982b8SRandall Stewart SCTP_SOCKET_UNLOCK(so, 1); 48560c0982b8SRandall Stewart return (ret_sz); 48570c0982b8SRandall Stewart } 48580c0982b8SRandall Stewart } 48590c0982b8SRandall Stewart #endif 48600c0982b8SRandall Stewart sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket); 48610c0982b8SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 48620c0982b8SRandall Stewart if (!so_locked) { 48630c0982b8SRandall Stewart SCTP_SOCKET_UNLOCK(so, 1); 48640c0982b8SRandall Stewart } 48650c0982b8SRandall Stewart #endif 4866f8829a4aSRandall Stewart } 4867f8829a4aSRandall Stewart return (ret_sz); 4868f8829a4aSRandall Stewart } 4869f8829a4aSRandall Stewart 4870f8829a4aSRandall Stewart /* 4871f8829a4aSRandall Stewart * checks to see if the given address, sa, is one that is currently known by 4872f8829a4aSRandall Stewart * the kernel note: can't distinguish the same address on multiple interfaces 4873f8829a4aSRandall Stewart * and doesn't handle multiple addresses with different zone/scope id's note: 4874f8829a4aSRandall Stewart * ifa_ifwithaddr() compares the entire sockaddr struct 4875f8829a4aSRandall Stewart */ 487642551e99SRandall Stewart struct sctp_ifa * 487780fefe0aSRandall Stewart sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr, 487880fefe0aSRandall Stewart int holds_lock) 4879f8829a4aSRandall Stewart { 488042551e99SRandall Stewart struct sctp_laddr *laddr; 4881f8829a4aSRandall Stewart 4882ad81507eSRandall Stewart if (holds_lock == 0) { 488342551e99SRandall Stewart SCTP_INP_RLOCK(inp); 4884ad81507eSRandall Stewart } 488542551e99SRandall Stewart LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 488642551e99SRandall Stewart if (laddr->ifa == NULL) 4887f8829a4aSRandall Stewart continue; 488842551e99SRandall Stewart if (addr->sa_family != laddr->ifa->address.sa.sa_family) 488942551e99SRandall Stewart continue; 4890e6194c2eSMichael Tuexen #ifdef INET 489142551e99SRandall Stewart if (addr->sa_family == AF_INET) { 489242551e99SRandall Stewart if (((struct sockaddr_in *)addr)->sin_addr.s_addr == 489342551e99SRandall Stewart laddr->ifa->address.sin.sin_addr.s_addr) { 489442551e99SRandall Stewart /* found him. */ 4895ad81507eSRandall Stewart if (holds_lock == 0) { 489642551e99SRandall Stewart SCTP_INP_RUNLOCK(inp); 4897ad81507eSRandall Stewart } 489842551e99SRandall Stewart return (laddr->ifa); 489942551e99SRandall Stewart break; 490042551e99SRandall Stewart } 49015e2c2d87SRandall Stewart } 4902e6194c2eSMichael Tuexen #endif 49035e2c2d87SRandall Stewart #ifdef INET6 49045e2c2d87SRandall Stewart if (addr->sa_family == AF_INET6) { 4905c54a18d2SRandall Stewart if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr, 4906c54a18d2SRandall Stewart &laddr->ifa->address.sin6)) { 490742551e99SRandall Stewart /* found him. */ 4908ad81507eSRandall Stewart if (holds_lock == 0) { 490942551e99SRandall Stewart SCTP_INP_RUNLOCK(inp); 4910ad81507eSRandall Stewart } 491142551e99SRandall Stewart return (laddr->ifa); 491242551e99SRandall Stewart break; 491342551e99SRandall Stewart } 491442551e99SRandall Stewart } 49155e2c2d87SRandall Stewart #endif 491642551e99SRandall Stewart } 4917ad81507eSRandall Stewart if (holds_lock == 0) { 491842551e99SRandall Stewart SCTP_INP_RUNLOCK(inp); 4919ad81507eSRandall Stewart } 492042551e99SRandall Stewart return (NULL); 492142551e99SRandall Stewart } 4922f8829a4aSRandall Stewart 49236a27c376SRandall Stewart uint32_t 49246a27c376SRandall Stewart sctp_get_ifa_hash_val(struct sockaddr *addr) 49256a27c376SRandall Stewart { 4926ea5eba11SMichael Tuexen switch (addr->sa_family) { 4927ea5eba11SMichael Tuexen #ifdef INET 4928ea5eba11SMichael Tuexen case AF_INET: 4929ea5eba11SMichael Tuexen { 49306a27c376SRandall Stewart struct sockaddr_in *sin; 49316a27c376SRandall Stewart 49326a27c376SRandall Stewart sin = (struct sockaddr_in *)addr; 49336a27c376SRandall Stewart return (sin->sin_addr.s_addr ^ (sin->sin_addr.s_addr >> 16)); 4934ea5eba11SMichael Tuexen } 4935ea5eba11SMichael Tuexen #endif 4936ea5eba11SMichael Tuexen #ifdef INET6 4937ea5eba11SMichael Tuexen case INET6: 4938ea5eba11SMichael Tuexen { 49396a27c376SRandall Stewart struct sockaddr_in6 *sin6; 49406a27c376SRandall Stewart uint32_t hash_of_addr; 49416a27c376SRandall Stewart 49426a27c376SRandall Stewart sin6 = (struct sockaddr_in6 *)addr; 49436a27c376SRandall Stewart hash_of_addr = (sin6->sin6_addr.s6_addr32[0] + 49446a27c376SRandall Stewart sin6->sin6_addr.s6_addr32[1] + 49456a27c376SRandall Stewart sin6->sin6_addr.s6_addr32[2] + 49466a27c376SRandall Stewart sin6->sin6_addr.s6_addr32[3]); 49476a27c376SRandall Stewart hash_of_addr = (hash_of_addr ^ (hash_of_addr >> 16)); 49486a27c376SRandall Stewart return (hash_of_addr); 49496a27c376SRandall Stewart } 4950ea5eba11SMichael Tuexen #endif 4951ea5eba11SMichael Tuexen default: 4952ea5eba11SMichael Tuexen break; 4953ea5eba11SMichael Tuexen } 49546a27c376SRandall Stewart return (0); 49556a27c376SRandall Stewart } 49566a27c376SRandall Stewart 495742551e99SRandall Stewart struct sctp_ifa * 495842551e99SRandall Stewart sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock) 495942551e99SRandall Stewart { 496042551e99SRandall Stewart struct sctp_ifa *sctp_ifap; 496142551e99SRandall Stewart struct sctp_vrf *vrf; 49626a27c376SRandall Stewart struct sctp_ifalist *hash_head; 49636a27c376SRandall Stewart uint32_t hash_of_addr; 496442551e99SRandall Stewart 496542551e99SRandall Stewart if (holds_lock == 0) 4966c99efcf6SRandall Stewart SCTP_IPI_ADDR_RLOCK(); 496742551e99SRandall Stewart 4968bff64a4dSRandall Stewart vrf = sctp_find_vrf(vrf_id); 4969bff64a4dSRandall Stewart if (vrf == NULL) { 4970df6e0cc3SRandall Stewart stage_right: 4971bff64a4dSRandall Stewart if (holds_lock == 0) 4972c99efcf6SRandall Stewart SCTP_IPI_ADDR_RUNLOCK(); 4973bff64a4dSRandall Stewart return (NULL); 4974bff64a4dSRandall Stewart } 4975bff64a4dSRandall Stewart hash_of_addr = sctp_get_ifa_hash_val(addr); 4976bff64a4dSRandall Stewart 497717205eccSRandall Stewart hash_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)]; 4978bff64a4dSRandall Stewart if (hash_head == NULL) { 4979ad81507eSRandall Stewart SCTP_PRINTF("hash_of_addr:%x mask:%x table:%x - ", 4980c99efcf6SRandall Stewart hash_of_addr, (uint32_t) vrf->vrf_addr_hashmark, 4981c99efcf6SRandall Stewart (uint32_t) (hash_of_addr & vrf->vrf_addr_hashmark)); 4982bff64a4dSRandall Stewart sctp_print_address(addr); 4983ad81507eSRandall Stewart SCTP_PRINTF("No such bucket for address\n"); 4984bff64a4dSRandall Stewart if (holds_lock == 0) 4985c99efcf6SRandall Stewart SCTP_IPI_ADDR_RUNLOCK(); 4986bff64a4dSRandall Stewart 4987bff64a4dSRandall Stewart return (NULL); 4988bff64a4dSRandall Stewart } 49896a27c376SRandall Stewart LIST_FOREACH(sctp_ifap, hash_head, next_bucket) { 4990bff64a4dSRandall Stewart if (sctp_ifap == NULL) { 4991df6e0cc3SRandall Stewart #ifdef INVARIANTS 4992bff64a4dSRandall Stewart panic("Huh LIST_FOREACH corrupt"); 4993df6e0cc3SRandall Stewart goto stage_right; 4994df6e0cc3SRandall Stewart #else 4995df6e0cc3SRandall Stewart SCTP_PRINTF("LIST corrupt of sctp_ifap's?\n"); 4996df6e0cc3SRandall Stewart goto stage_right; 4997df6e0cc3SRandall Stewart #endif 4998bff64a4dSRandall Stewart } 49996a27c376SRandall Stewart if (addr->sa_family != sctp_ifap->address.sa.sa_family) 50006a27c376SRandall Stewart continue; 5001e6194c2eSMichael Tuexen #ifdef INET 50026a27c376SRandall Stewart if (addr->sa_family == AF_INET) { 50036a27c376SRandall Stewart if (((struct sockaddr_in *)addr)->sin_addr.s_addr == 50046a27c376SRandall Stewart sctp_ifap->address.sin.sin_addr.s_addr) { 50056a27c376SRandall Stewart /* found him. */ 500642551e99SRandall Stewart if (holds_lock == 0) 5007c99efcf6SRandall Stewart SCTP_IPI_ADDR_RUNLOCK(); 500842551e99SRandall Stewart return (sctp_ifap); 50096a27c376SRandall Stewart break; 50106a27c376SRandall Stewart } 50115e2c2d87SRandall Stewart } 5012e6194c2eSMichael Tuexen #endif 50135e2c2d87SRandall Stewart #ifdef INET6 50145e2c2d87SRandall Stewart if (addr->sa_family == AF_INET6) { 5015c54a18d2SRandall Stewart if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr, 5016c54a18d2SRandall Stewart &sctp_ifap->address.sin6)) { 50176a27c376SRandall Stewart /* found him. */ 50186a27c376SRandall Stewart if (holds_lock == 0) 5019c99efcf6SRandall Stewart SCTP_IPI_ADDR_RUNLOCK(); 50206a27c376SRandall Stewart return (sctp_ifap); 50216a27c376SRandall Stewart break; 50226a27c376SRandall Stewart } 502342551e99SRandall Stewart } 50245e2c2d87SRandall Stewart #endif 502542551e99SRandall Stewart } 502642551e99SRandall Stewart if (holds_lock == 0) 5027c99efcf6SRandall Stewart SCTP_IPI_ADDR_RUNLOCK(); 5028f8829a4aSRandall Stewart return (NULL); 5029f8829a4aSRandall Stewart } 5030f8829a4aSRandall Stewart 5031f8829a4aSRandall Stewart static void 50324c9179adSRandall Stewart sctp_user_rcvd(struct sctp_tcb *stcb, uint32_t * freed_so_far, int hold_rlock, 5033f8829a4aSRandall Stewart uint32_t rwnd_req) 5034f8829a4aSRandall Stewart { 5035f8829a4aSRandall Stewart /* User pulled some data, do we need a rwnd update? */ 5036f8829a4aSRandall Stewart int r_unlocked = 0; 5037f8829a4aSRandall Stewart uint32_t dif, rwnd; 5038f8829a4aSRandall Stewart struct socket *so = NULL; 5039f8829a4aSRandall Stewart 5040f8829a4aSRandall Stewart if (stcb == NULL) 5041f8829a4aSRandall Stewart return; 5042f8829a4aSRandall Stewart 504350cec919SRandall Stewart atomic_add_int(&stcb->asoc.refcnt, 1); 5044f8829a4aSRandall Stewart 504562c1ff9cSRandall Stewart if (stcb->asoc.state & (SCTP_STATE_ABOUT_TO_BE_FREED | 504662c1ff9cSRandall Stewart SCTP_STATE_SHUTDOWN_RECEIVED | 50474c9179adSRandall Stewart SCTP_STATE_SHUTDOWN_ACK_SENT)) { 5048f8829a4aSRandall Stewart /* Pre-check If we are freeing no update */ 5049f8829a4aSRandall Stewart goto no_lock; 5050f8829a4aSRandall Stewart } 5051f8829a4aSRandall Stewart SCTP_INP_INCR_REF(stcb->sctp_ep); 5052f8829a4aSRandall Stewart if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 5053f8829a4aSRandall Stewart (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { 5054f8829a4aSRandall Stewart goto out; 5055f8829a4aSRandall Stewart } 5056f8829a4aSRandall Stewart so = stcb->sctp_socket; 5057f8829a4aSRandall Stewart if (so == NULL) { 5058f8829a4aSRandall Stewart goto out; 5059f8829a4aSRandall Stewart } 5060f8829a4aSRandall Stewart atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far); 5061f8829a4aSRandall Stewart /* Have you have freed enough to look */ 5062f8829a4aSRandall Stewart *freed_so_far = 0; 5063f8829a4aSRandall Stewart /* Yep, its worth a look and the lock overhead */ 5064f8829a4aSRandall Stewart 5065f8829a4aSRandall Stewart /* Figure out what the rwnd would be */ 5066f8829a4aSRandall Stewart rwnd = sctp_calc_rwnd(stcb, &stcb->asoc); 5067f8829a4aSRandall Stewart if (rwnd >= stcb->asoc.my_last_reported_rwnd) { 5068f8829a4aSRandall Stewart dif = rwnd - stcb->asoc.my_last_reported_rwnd; 5069f8829a4aSRandall Stewart } else { 5070f8829a4aSRandall Stewart dif = 0; 5071f8829a4aSRandall Stewart } 5072f8829a4aSRandall Stewart if (dif >= rwnd_req) { 5073f8829a4aSRandall Stewart if (hold_rlock) { 5074f8829a4aSRandall Stewart SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 5075f8829a4aSRandall Stewart r_unlocked = 1; 5076f8829a4aSRandall Stewart } 5077f8829a4aSRandall Stewart if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { 5078f8829a4aSRandall Stewart /* 5079f8829a4aSRandall Stewart * One last check before we allow the guy possibly 5080f8829a4aSRandall Stewart * to get in. There is a race, where the guy has not 5081f8829a4aSRandall Stewart * reached the gate. In that case 5082f8829a4aSRandall Stewart */ 5083f8829a4aSRandall Stewart goto out; 5084f8829a4aSRandall Stewart } 5085f8829a4aSRandall Stewart SCTP_TCB_LOCK(stcb); 5086f8829a4aSRandall Stewart if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { 5087f8829a4aSRandall Stewart /* No reports here */ 5088f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 5089f8829a4aSRandall Stewart goto out; 5090f8829a4aSRandall Stewart } 5091f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_wu_sacks_sent); 5092689e6a5fSMichael Tuexen sctp_send_sack(stcb, SCTP_SO_LOCKED); 5093830d754dSRandall Stewart 5094f8829a4aSRandall Stewart sctp_chunk_output(stcb->sctp_ep, stcb, 5095ceaad40aSRandall Stewart SCTP_OUTPUT_FROM_USR_RCVD, SCTP_SO_LOCKED); 5096f8829a4aSRandall Stewart /* make sure no timer is running */ 5097a5d547adSRandall Stewart sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTPUTIL + SCTP_LOC_6); 5098f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 5099f8829a4aSRandall Stewart } else { 5100f8829a4aSRandall Stewart /* Update how much we have pending */ 5101f8829a4aSRandall Stewart stcb->freed_by_sorcv_sincelast = dif; 5102f8829a4aSRandall Stewart } 5103f8829a4aSRandall Stewart out: 5104f8829a4aSRandall Stewart if (so && r_unlocked && hold_rlock) { 5105f8829a4aSRandall Stewart SCTP_INP_READ_LOCK(stcb->sctp_ep); 5106f8829a4aSRandall Stewart } 5107f8829a4aSRandall Stewart SCTP_INP_DECR_REF(stcb->sctp_ep); 5108f8829a4aSRandall Stewart no_lock: 510950cec919SRandall Stewart atomic_add_int(&stcb->asoc.refcnt, -1); 5110f8829a4aSRandall Stewart return; 5111f8829a4aSRandall Stewart } 5112f8829a4aSRandall Stewart 5113f8829a4aSRandall Stewart int 5114f8829a4aSRandall Stewart sctp_sorecvmsg(struct socket *so, 5115f8829a4aSRandall Stewart struct uio *uio, 5116f8829a4aSRandall Stewart struct mbuf **mp, 5117f8829a4aSRandall Stewart struct sockaddr *from, 5118f8829a4aSRandall Stewart int fromlen, 5119f8829a4aSRandall Stewart int *msg_flags, 5120f8829a4aSRandall Stewart struct sctp_sndrcvinfo *sinfo, 5121f8829a4aSRandall Stewart int filling_sinfo) 5122f8829a4aSRandall Stewart { 5123f8829a4aSRandall Stewart /* 5124f8829a4aSRandall Stewart * MSG flags we will look at MSG_DONTWAIT - non-blocking IO. 5125f8829a4aSRandall Stewart * MSG_PEEK - Look don't touch :-D (only valid with OUT mbuf copy 5126f8829a4aSRandall Stewart * mp=NULL thus uio is the copy method to userland) MSG_WAITALL - ?? 5127f8829a4aSRandall Stewart * On the way out we may send out any combination of: 5128f8829a4aSRandall Stewart * MSG_NOTIFICATION MSG_EOR 5129f8829a4aSRandall Stewart * 5130f8829a4aSRandall Stewart */ 5131f8829a4aSRandall Stewart struct sctp_inpcb *inp = NULL; 5132f8829a4aSRandall Stewart int my_len = 0; 5133f8829a4aSRandall Stewart int cp_len = 0, error = 0; 5134f8829a4aSRandall Stewart struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL; 513594b0d969SMichael Tuexen struct mbuf *m = NULL; 5136f8829a4aSRandall Stewart struct sctp_tcb *stcb = NULL; 5137f8829a4aSRandall Stewart int wakeup_read_socket = 0; 5138f8829a4aSRandall Stewart int freecnt_applied = 0; 5139f8829a4aSRandall Stewart int out_flags = 0, in_flags = 0; 5140f8829a4aSRandall Stewart int block_allowed = 1; 51414c9179adSRandall Stewart uint32_t freed_so_far = 0; 514281aca91aSRandall Stewart uint32_t copied_so_far = 0; 514393164cf9SRandall Stewart int in_eeor_mode = 0; 5144f8829a4aSRandall Stewart int no_rcv_needed = 0; 5145f8829a4aSRandall Stewart uint32_t rwnd_req = 0; 5146f8829a4aSRandall Stewart int hold_sblock = 0; 5147f8829a4aSRandall Stewart int hold_rlock = 0; 514842551e99SRandall Stewart int slen = 0; 51494c9179adSRandall Stewart uint32_t held_length = 0; 51507abab911SRobert Watson int sockbuf_lock = 0; 5151f8829a4aSRandall Stewart 515217205eccSRandall Stewart if (uio == NULL) { 5153c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); 515417205eccSRandall Stewart return (EINVAL); 515517205eccSRandall Stewart } 5156f8829a4aSRandall Stewart if (msg_flags) { 5157f8829a4aSRandall Stewart in_flags = *msg_flags; 5158c105859eSRandall Stewart if (in_flags & MSG_PEEK) 5159c105859eSRandall Stewart SCTP_STAT_INCR(sctps_read_peeks); 5160f8829a4aSRandall Stewart } else { 5161f8829a4aSRandall Stewart in_flags = 0; 5162f8829a4aSRandall Stewart } 5163f8829a4aSRandall Stewart slen = uio->uio_resid; 516417205eccSRandall Stewart 5165f8829a4aSRandall Stewart /* Pull in and set up our int flags */ 5166f8829a4aSRandall Stewart if (in_flags & MSG_OOB) { 5167f8829a4aSRandall Stewart /* Out of band's NOT supported */ 5168f8829a4aSRandall Stewart return (EOPNOTSUPP); 5169f8829a4aSRandall Stewart } 5170f8829a4aSRandall Stewart if ((in_flags & MSG_PEEK) && (mp != NULL)) { 5171c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); 5172f8829a4aSRandall Stewart return (EINVAL); 5173f8829a4aSRandall Stewart } 5174f8829a4aSRandall Stewart if ((in_flags & (MSG_DONTWAIT 5175f8829a4aSRandall Stewart | MSG_NBIO 5176f8829a4aSRandall Stewart )) || 517742551e99SRandall Stewart SCTP_SO_IS_NBIO(so)) { 5178f8829a4aSRandall Stewart block_allowed = 0; 5179f8829a4aSRandall Stewart } 5180f8829a4aSRandall Stewart /* setup the endpoint */ 5181f8829a4aSRandall Stewart inp = (struct sctp_inpcb *)so->so_pcb; 5182f8829a4aSRandall Stewart if (inp == NULL) { 5183c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT); 5184f8829a4aSRandall Stewart return (EFAULT); 5185f8829a4aSRandall Stewart } 518662c1ff9cSRandall Stewart rwnd_req = (SCTP_SB_LIMIT_RCV(so) >> SCTP_RWND_HIWAT_SHIFT); 5187f8829a4aSRandall Stewart /* Must be at least a MTU's worth */ 5188f8829a4aSRandall Stewart if (rwnd_req < SCTP_MIN_RWND) 5189f8829a4aSRandall Stewart rwnd_req = SCTP_MIN_RWND; 5190f8829a4aSRandall Stewart in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR); 5191b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) { 5192f8829a4aSRandall Stewart sctp_misc_ints(SCTP_SORECV_ENTER, 519317205eccSRandall Stewart rwnd_req, in_eeor_mode, so->so_rcv.sb_cc, uio->uio_resid); 519480fefe0aSRandall Stewart } 5195b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) { 5196f8829a4aSRandall Stewart sctp_misc_ints(SCTP_SORECV_ENTERPL, 519717205eccSRandall Stewart rwnd_req, block_allowed, so->so_rcv.sb_cc, uio->uio_resid); 519880fefe0aSRandall Stewart } 5199265de5bbSRobert Watson error = sblock(&so->so_rcv, (block_allowed ? SBL_WAIT : 0)); 5200f8829a4aSRandall Stewart if (error) { 5201f8829a4aSRandall Stewart goto release_unlocked; 5202f8829a4aSRandall Stewart } 5203*8e1e6e5fSMateusz Guzik sockbuf_lock = 1; 5204f8829a4aSRandall Stewart restart: 52057abab911SRobert Watson 5206f8829a4aSRandall Stewart 5207f8829a4aSRandall Stewart restart_nosblocks: 5208f8829a4aSRandall Stewart if (hold_sblock == 0) { 5209f8829a4aSRandall Stewart SOCKBUF_LOCK(&so->so_rcv); 5210f8829a4aSRandall Stewart hold_sblock = 1; 5211f8829a4aSRandall Stewart } 5212f8829a4aSRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 5213f8829a4aSRandall Stewart (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { 5214f8829a4aSRandall Stewart goto out; 5215f8829a4aSRandall Stewart } 521658411b08SMichael Tuexen if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) && (so->so_rcv.sb_cc == 0)) { 5217f8829a4aSRandall Stewart if (so->so_error) { 5218f8829a4aSRandall Stewart error = so->so_error; 521944b7479bSRandall Stewart if ((in_flags & MSG_PEEK) == 0) 522044b7479bSRandall Stewart so->so_error = 0; 52219f22f500SRandall Stewart goto out; 5222f8829a4aSRandall Stewart } else { 52239f22f500SRandall Stewart if (so->so_rcv.sb_cc == 0) { 52247924093fSRandall Stewart /* indicate EOF */ 52257924093fSRandall Stewart error = 0; 5226f8829a4aSRandall Stewart goto out; 5227f8829a4aSRandall Stewart } 52289f22f500SRandall Stewart } 52299f22f500SRandall Stewart } 5230f8829a4aSRandall Stewart if ((so->so_rcv.sb_cc <= held_length) && block_allowed) { 5231f8829a4aSRandall Stewart /* we need to wait for data */ 5232f8829a4aSRandall Stewart if ((so->so_rcv.sb_cc == 0) && 5233f8829a4aSRandall Stewart ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 5234f8829a4aSRandall Stewart (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) { 5235f8829a4aSRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) { 5236f8829a4aSRandall Stewart /* 5237f8829a4aSRandall Stewart * For active open side clear flags for 5238f8829a4aSRandall Stewart * re-use passive open is blocked by 5239f8829a4aSRandall Stewart * connect. 5240f8829a4aSRandall Stewart */ 5241f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) { 5242f8829a4aSRandall Stewart /* 5243f8829a4aSRandall Stewart * You were aborted, passive side 5244f8829a4aSRandall Stewart * always hits here 5245f8829a4aSRandall Stewart */ 5246c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET); 5247f8829a4aSRandall Stewart error = ECONNRESET; 5248f8829a4aSRandall Stewart } 5249f8829a4aSRandall Stewart so->so_state &= ~(SS_ISCONNECTING | 5250f8829a4aSRandall Stewart SS_ISDISCONNECTING | 5251f8829a4aSRandall Stewart SS_ISCONFIRMING | 5252f8829a4aSRandall Stewart SS_ISCONNECTED); 5253f8829a4aSRandall Stewart if (error == 0) { 5254f8829a4aSRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) { 5255c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN); 5256f8829a4aSRandall Stewart error = ENOTCONN; 5257f8829a4aSRandall Stewart } 5258f8829a4aSRandall Stewart } 5259f8829a4aSRandall Stewart goto out; 5260f8829a4aSRandall Stewart } 5261f8829a4aSRandall Stewart } 5262f8829a4aSRandall Stewart error = sbwait(&so->so_rcv); 5263f8829a4aSRandall Stewart if (error) { 5264f8829a4aSRandall Stewart goto out; 5265f8829a4aSRandall Stewart } 5266f8829a4aSRandall Stewart held_length = 0; 5267f8829a4aSRandall Stewart goto restart_nosblocks; 5268f8829a4aSRandall Stewart } else if (so->so_rcv.sb_cc == 0) { 526944b7479bSRandall Stewart if (so->so_error) { 527044b7479bSRandall Stewart error = so->so_error; 527144b7479bSRandall Stewart if ((in_flags & MSG_PEEK) == 0) 527244b7479bSRandall Stewart so->so_error = 0; 527344b7479bSRandall Stewart } else { 527444b7479bSRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 527544b7479bSRandall Stewart (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 527644b7479bSRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) { 527744b7479bSRandall Stewart /* 527844b7479bSRandall Stewart * For active open side clear flags 527944b7479bSRandall Stewart * for re-use passive open is 528044b7479bSRandall Stewart * blocked by connect. 528144b7479bSRandall Stewart */ 528244b7479bSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) { 528344b7479bSRandall Stewart /* 528444b7479bSRandall Stewart * You were aborted, passive 528544b7479bSRandall Stewart * side always hits here 528644b7479bSRandall Stewart */ 5287c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET); 528844b7479bSRandall Stewart error = ECONNRESET; 528944b7479bSRandall Stewart } 529044b7479bSRandall Stewart so->so_state &= ~(SS_ISCONNECTING | 529144b7479bSRandall Stewart SS_ISDISCONNECTING | 529244b7479bSRandall Stewart SS_ISCONFIRMING | 529344b7479bSRandall Stewart SS_ISCONNECTED); 529444b7479bSRandall Stewart if (error == 0) { 529544b7479bSRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) { 5296c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN); 529744b7479bSRandall Stewart error = ENOTCONN; 529844b7479bSRandall Stewart } 529944b7479bSRandall Stewart } 530044b7479bSRandall Stewart goto out; 530144b7479bSRandall Stewart } 530244b7479bSRandall Stewart } 5303c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EWOULDBLOCK); 5304f8829a4aSRandall Stewart error = EWOULDBLOCK; 530544b7479bSRandall Stewart } 5306f8829a4aSRandall Stewart goto out; 5307f8829a4aSRandall Stewart } 5308d06c82f1SRandall Stewart if (hold_sblock == 1) { 5309d06c82f1SRandall Stewart SOCKBUF_UNLOCK(&so->so_rcv); 5310d06c82f1SRandall Stewart hold_sblock = 0; 5311d06c82f1SRandall Stewart } 5312f8829a4aSRandall Stewart /* we possibly have data we can read */ 53133c503c28SRandall Stewart /* sa_ignore FREED_MEMORY */ 5314f8829a4aSRandall Stewart control = TAILQ_FIRST(&inp->read_queue); 5315f8829a4aSRandall Stewart if (control == NULL) { 5316f8829a4aSRandall Stewart /* 5317f8829a4aSRandall Stewart * This could be happening since the appender did the 5318f8829a4aSRandall Stewart * increment but as not yet did the tailq insert onto the 5319f8829a4aSRandall Stewart * read_queue 5320f8829a4aSRandall Stewart */ 5321f8829a4aSRandall Stewart if (hold_rlock == 0) { 5322f8829a4aSRandall Stewart SCTP_INP_READ_LOCK(inp); 5323f8829a4aSRandall Stewart } 5324f8829a4aSRandall Stewart control = TAILQ_FIRST(&inp->read_queue); 5325f8829a4aSRandall Stewart if ((control == NULL) && (so->so_rcv.sb_cc != 0)) { 5326a5d547adSRandall Stewart #ifdef INVARIANTS 5327f8829a4aSRandall Stewart panic("Huh, its non zero and nothing on control?"); 5328f8829a4aSRandall Stewart #endif 5329f8829a4aSRandall Stewart so->so_rcv.sb_cc = 0; 5330f8829a4aSRandall Stewart } 5331f8829a4aSRandall Stewart SCTP_INP_READ_UNLOCK(inp); 5332f8829a4aSRandall Stewart hold_rlock = 0; 5333f8829a4aSRandall Stewart goto restart; 5334f8829a4aSRandall Stewart } 5335f8829a4aSRandall Stewart if ((control->length == 0) && 5336f8829a4aSRandall Stewart (control->do_not_ref_stcb)) { 5337f8829a4aSRandall Stewart /* 5338f8829a4aSRandall Stewart * Clean up code for freeing assoc that left behind a 5339f8829a4aSRandall Stewart * pdapi.. maybe a peer in EEOR that just closed after 5340f8829a4aSRandall Stewart * sending and never indicated a EOR. 5341f8829a4aSRandall Stewart */ 5342f8829a4aSRandall Stewart if (hold_rlock == 0) { 5343f8829a4aSRandall Stewart hold_rlock = 1; 5344f8829a4aSRandall Stewart SCTP_INP_READ_LOCK(inp); 5345f8829a4aSRandall Stewart } 5346f8829a4aSRandall Stewart control->held_length = 0; 5347f8829a4aSRandall Stewart if (control->data) { 5348f8829a4aSRandall Stewart /* Hmm there is data here .. fix */ 53494c9179adSRandall Stewart struct mbuf *m_tmp; 5350f8829a4aSRandall Stewart int cnt = 0; 5351f8829a4aSRandall Stewart 53524c9179adSRandall Stewart m_tmp = control->data; 53534c9179adSRandall Stewart while (m_tmp) { 53544c9179adSRandall Stewart cnt += SCTP_BUF_LEN(m_tmp); 53554c9179adSRandall Stewart if (SCTP_BUF_NEXT(m_tmp) == NULL) { 53564c9179adSRandall Stewart control->tail_mbuf = m_tmp; 5357f8829a4aSRandall Stewart control->end_added = 1; 5358f8829a4aSRandall Stewart } 53594c9179adSRandall Stewart m_tmp = SCTP_BUF_NEXT(m_tmp); 5360f8829a4aSRandall Stewart } 5361f8829a4aSRandall Stewart control->length = cnt; 5362f8829a4aSRandall Stewart } else { 5363f8829a4aSRandall Stewart /* remove it */ 5364f8829a4aSRandall Stewart TAILQ_REMOVE(&inp->read_queue, control, next); 5365f8829a4aSRandall Stewart /* Add back any hiddend data */ 5366f8829a4aSRandall Stewart sctp_free_remote_addr(control->whoFrom); 5367f8829a4aSRandall Stewart sctp_free_a_readq(stcb, control); 5368f8829a4aSRandall Stewart } 5369f8829a4aSRandall Stewart if (hold_rlock) { 5370f8829a4aSRandall Stewart hold_rlock = 0; 5371f8829a4aSRandall Stewart SCTP_INP_READ_UNLOCK(inp); 5372f8829a4aSRandall Stewart } 5373f8829a4aSRandall Stewart goto restart; 5374f8829a4aSRandall Stewart } 5375810ec536SMichael Tuexen if ((control->length == 0) && 5376810ec536SMichael Tuexen (control->end_added == 1)) { 5377810ec536SMichael Tuexen /* 5378810ec536SMichael Tuexen * Do we also need to check for (control->pdapi_aborted == 5379810ec536SMichael Tuexen * 1)? 5380810ec536SMichael Tuexen */ 5381810ec536SMichael Tuexen if (hold_rlock == 0) { 5382810ec536SMichael Tuexen hold_rlock = 1; 5383810ec536SMichael Tuexen SCTP_INP_READ_LOCK(inp); 5384810ec536SMichael Tuexen } 5385810ec536SMichael Tuexen TAILQ_REMOVE(&inp->read_queue, control, next); 5386810ec536SMichael Tuexen if (control->data) { 5387810ec536SMichael Tuexen #ifdef INVARIANTS 5388810ec536SMichael Tuexen panic("control->data not null but control->length == 0"); 5389810ec536SMichael Tuexen #else 5390810ec536SMichael Tuexen SCTP_PRINTF("Strange, data left in the control buffer. Cleaning up.\n"); 5391810ec536SMichael Tuexen sctp_m_freem(control->data); 5392810ec536SMichael Tuexen control->data = NULL; 5393810ec536SMichael Tuexen #endif 5394810ec536SMichael Tuexen } 5395810ec536SMichael Tuexen if (control->aux_data) { 5396810ec536SMichael Tuexen sctp_m_free(control->aux_data); 5397810ec536SMichael Tuexen control->aux_data = NULL; 5398810ec536SMichael Tuexen } 5399810ec536SMichael Tuexen sctp_free_remote_addr(control->whoFrom); 5400810ec536SMichael Tuexen sctp_free_a_readq(stcb, control); 5401810ec536SMichael Tuexen if (hold_rlock) { 5402810ec536SMichael Tuexen hold_rlock = 0; 5403810ec536SMichael Tuexen SCTP_INP_READ_UNLOCK(inp); 5404810ec536SMichael Tuexen } 5405810ec536SMichael Tuexen goto restart; 5406810ec536SMichael Tuexen } 5407f8829a4aSRandall Stewart if (control->length == 0) { 5408f8829a4aSRandall Stewart if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) && 5409f8829a4aSRandall Stewart (filling_sinfo)) { 5410f8829a4aSRandall Stewart /* find a more suitable one then this */ 5411f8829a4aSRandall Stewart ctl = TAILQ_NEXT(control, next); 5412f8829a4aSRandall Stewart while (ctl) { 54139a6142d8SRandall Stewart if ((ctl->stcb != control->stcb) && (ctl->length) && 54149a6142d8SRandall Stewart (ctl->some_taken || 54156114cd96SRandall Stewart (ctl->spec_flags & M_NOTIFICATION) || 54169a6142d8SRandall Stewart ((ctl->do_not_ref_stcb == 0) && 54179a6142d8SRandall Stewart (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0))) 54189a6142d8SRandall Stewart ) { 54199a6142d8SRandall Stewart /*- 54209a6142d8SRandall Stewart * If we have a different TCB next, and there is data 54219a6142d8SRandall Stewart * present. If we have already taken some (pdapi), OR we can 54229a6142d8SRandall Stewart * ref the tcb and no delivery as started on this stream, we 542317205eccSRandall Stewart * take it. Note we allow a notification on a different 542417205eccSRandall Stewart * assoc to be delivered.. 54259a6142d8SRandall Stewart */ 54269a6142d8SRandall Stewart control = ctl; 54279a6142d8SRandall Stewart goto found_one; 54289a6142d8SRandall Stewart } else if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) && 54299a6142d8SRandall Stewart (ctl->length) && 54309a6142d8SRandall Stewart ((ctl->some_taken) || 54319a6142d8SRandall Stewart ((ctl->do_not_ref_stcb == 0) && 543217205eccSRandall Stewart ((ctl->spec_flags & M_NOTIFICATION) == 0) && 5433b5c16493SMichael Tuexen (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))) { 54349a6142d8SRandall Stewart /*- 54359a6142d8SRandall Stewart * If we have the same tcb, and there is data present, and we 54369a6142d8SRandall Stewart * have the strm interleave feature present. Then if we have 54379a6142d8SRandall Stewart * taken some (pdapi) or we can refer to tht tcb AND we have 54389a6142d8SRandall Stewart * not started a delivery for this stream, we can take it. 543917205eccSRandall Stewart * Note we do NOT allow a notificaiton on the same assoc to 544017205eccSRandall Stewart * be delivered. 54419a6142d8SRandall Stewart */ 5442f8829a4aSRandall Stewart control = ctl; 5443f8829a4aSRandall Stewart goto found_one; 5444f8829a4aSRandall Stewart } 5445f8829a4aSRandall Stewart ctl = TAILQ_NEXT(ctl, next); 5446f8829a4aSRandall Stewart } 5447f8829a4aSRandall Stewart } 5448f8829a4aSRandall Stewart /* 5449f8829a4aSRandall Stewart * if we reach here, not suitable replacement is available 5450f8829a4aSRandall Stewart * <or> fragment interleave is NOT on. So stuff the sb_cc 5451f8829a4aSRandall Stewart * into the our held count, and its time to sleep again. 5452f8829a4aSRandall Stewart */ 5453f8829a4aSRandall Stewart held_length = so->so_rcv.sb_cc; 5454f8829a4aSRandall Stewart control->held_length = so->so_rcv.sb_cc; 5455f8829a4aSRandall Stewart goto restart; 5456f8829a4aSRandall Stewart } 5457f8829a4aSRandall Stewart /* Clear the held length since there is something to read */ 5458f8829a4aSRandall Stewart control->held_length = 0; 5459f8829a4aSRandall Stewart if (hold_rlock) { 5460f8829a4aSRandall Stewart SCTP_INP_READ_UNLOCK(inp); 5461f8829a4aSRandall Stewart hold_rlock = 0; 5462f8829a4aSRandall Stewart } 5463f8829a4aSRandall Stewart found_one: 5464f8829a4aSRandall Stewart /* 5465f8829a4aSRandall Stewart * If we reach here, control has a some data for us to read off. 5466f8829a4aSRandall Stewart * Note that stcb COULD be NULL. 5467f8829a4aSRandall Stewart */ 54689c04b296SRandall Stewart control->some_taken++; 5469f8829a4aSRandall Stewart if (hold_sblock) { 5470f8829a4aSRandall Stewart SOCKBUF_UNLOCK(&so->so_rcv); 5471f8829a4aSRandall Stewart hold_sblock = 0; 5472f8829a4aSRandall Stewart } 5473f8829a4aSRandall Stewart stcb = control->stcb; 5474f8829a4aSRandall Stewart if (stcb) { 54750696e120SRandall Stewart if ((control->do_not_ref_stcb == 0) && 54760696e120SRandall Stewart (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) { 547750cec919SRandall Stewart if (freecnt_applied == 0) 5478f8829a4aSRandall Stewart stcb = NULL; 5479f8829a4aSRandall Stewart } else if (control->do_not_ref_stcb == 0) { 5480f8829a4aSRandall Stewart /* you can't free it on me please */ 5481f8829a4aSRandall Stewart /* 5482f8829a4aSRandall Stewart * The lock on the socket buffer protects us so the 5483f8829a4aSRandall Stewart * free code will stop. But since we used the 5484f8829a4aSRandall Stewart * socketbuf lock and the sender uses the tcb_lock 5485f8829a4aSRandall Stewart * to increment, we need to use the atomic add to 5486f8829a4aSRandall Stewart * the refcnt 5487f8829a4aSRandall Stewart */ 5488d55b0b1bSRandall Stewart if (freecnt_applied) { 5489d55b0b1bSRandall Stewart #ifdef INVARIANTS 5490207304d4SRandall Stewart panic("refcnt already incremented"); 5491d55b0b1bSRandall Stewart #else 5492cd3fd531SMichael Tuexen SCTP_PRINTF("refcnt already incremented?\n"); 5493d55b0b1bSRandall Stewart #endif 5494d55b0b1bSRandall Stewart } else { 549550cec919SRandall Stewart atomic_add_int(&stcb->asoc.refcnt, 1); 5496f8829a4aSRandall Stewart freecnt_applied = 1; 5497d55b0b1bSRandall Stewart } 5498f8829a4aSRandall Stewart /* 5499f8829a4aSRandall Stewart * Setup to remember how much we have not yet told 5500f8829a4aSRandall Stewart * the peer our rwnd has opened up. Note we grab the 5501f8829a4aSRandall Stewart * value from the tcb from last time. Note too that 55020696e120SRandall Stewart * sack sending clears this when a sack is sent, 5503f8829a4aSRandall Stewart * which is fine. Once we hit the rwnd_req, we then 5504f8829a4aSRandall Stewart * will go to the sctp_user_rcvd() that will not 5505f8829a4aSRandall Stewart * lock until it KNOWs it MUST send a WUP-SACK. 5506f8829a4aSRandall Stewart */ 5507f8829a4aSRandall Stewart freed_so_far = stcb->freed_by_sorcv_sincelast; 5508f8829a4aSRandall Stewart stcb->freed_by_sorcv_sincelast = 0; 5509f8829a4aSRandall Stewart } 5510f8829a4aSRandall Stewart } 55116114cd96SRandall Stewart if (stcb && 55126114cd96SRandall Stewart ((control->spec_flags & M_NOTIFICATION) == 0) && 55136114cd96SRandall Stewart control->do_not_ref_stcb == 0) { 5514d06c82f1SRandall Stewart stcb->asoc.strmin[control->sinfo_stream].delivery_started = 1; 5515d06c82f1SRandall Stewart } 5516f8829a4aSRandall Stewart /* First lets get off the sinfo and sockaddr info */ 5517f8829a4aSRandall Stewart if ((sinfo) && filling_sinfo) { 5518f8829a4aSRandall Stewart memcpy(sinfo, control, sizeof(struct sctp_nonpad_sndrcvinfo)); 5519f8829a4aSRandall Stewart nxt = TAILQ_NEXT(control, next); 5520e2e7c62eSMichael Tuexen if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) || 5521e2e7c62eSMichael Tuexen sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) { 5522f8829a4aSRandall Stewart struct sctp_extrcvinfo *s_extra; 5523f8829a4aSRandall Stewart 5524f8829a4aSRandall Stewart s_extra = (struct sctp_extrcvinfo *)sinfo; 55259a6142d8SRandall Stewart if ((nxt) && 55269a6142d8SRandall Stewart (nxt->length)) { 55279a6142d8SRandall Stewart s_extra->sreinfo_next_flags = SCTP_NEXT_MSG_AVAIL; 5528f8829a4aSRandall Stewart if (nxt->sinfo_flags & SCTP_UNORDERED) { 55299a6142d8SRandall Stewart s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_IS_UNORDERED; 5530f8829a4aSRandall Stewart } 5531f42a358aSRandall Stewart if (nxt->spec_flags & M_NOTIFICATION) { 55329a6142d8SRandall Stewart s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_IS_NOTIFICATION; 5533f42a358aSRandall Stewart } 55349a6142d8SRandall Stewart s_extra->sreinfo_next_aid = nxt->sinfo_assoc_id; 55359a6142d8SRandall Stewart s_extra->sreinfo_next_length = nxt->length; 55369a6142d8SRandall Stewart s_extra->sreinfo_next_ppid = nxt->sinfo_ppid; 55379a6142d8SRandall Stewart s_extra->sreinfo_next_stream = nxt->sinfo_stream; 5538f8829a4aSRandall Stewart if (nxt->tail_mbuf != NULL) { 5539139bc87fSRandall Stewart if (nxt->end_added) { 55409a6142d8SRandall Stewart s_extra->sreinfo_next_flags |= SCTP_NEXT_MSG_ISCOMPLETE; 5541f8829a4aSRandall Stewart } 5542f8829a4aSRandall Stewart } 5543f8829a4aSRandall Stewart } else { 5544f8829a4aSRandall Stewart /* 5545f8829a4aSRandall Stewart * we explicitly 0 this, since the memcpy 5546f8829a4aSRandall Stewart * got some other things beyond the older 5547f8829a4aSRandall Stewart * sinfo_ that is on the control's structure 5548f8829a4aSRandall Stewart * :-D 5549f8829a4aSRandall Stewart */ 55509a6142d8SRandall Stewart nxt = NULL; 55519a6142d8SRandall Stewart s_extra->sreinfo_next_flags = SCTP_NO_NEXT_MSG; 55529a6142d8SRandall Stewart s_extra->sreinfo_next_aid = 0; 55539a6142d8SRandall Stewart s_extra->sreinfo_next_length = 0; 55549a6142d8SRandall Stewart s_extra->sreinfo_next_ppid = 0; 55559a6142d8SRandall Stewart s_extra->sreinfo_next_stream = 0; 5556f8829a4aSRandall Stewart } 5557f8829a4aSRandall Stewart } 5558f8829a4aSRandall Stewart /* 5559f8829a4aSRandall Stewart * update off the real current cum-ack, if we have an stcb. 5560f8829a4aSRandall Stewart */ 55610696e120SRandall Stewart if ((control->do_not_ref_stcb == 0) && stcb) 5562f8829a4aSRandall Stewart sinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn; 5563f8829a4aSRandall Stewart /* 5564f8829a4aSRandall Stewart * mask off the high bits, we keep the actual chunk bits in 5565f8829a4aSRandall Stewart * there. 5566f8829a4aSRandall Stewart */ 5567f8829a4aSRandall Stewart sinfo->sinfo_flags &= 0x00ff; 55685f26a41dSRandall Stewart if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) { 55695f26a41dSRandall Stewart sinfo->sinfo_flags |= SCTP_UNORDERED; 55705f26a41dSRandall Stewart } 5571f8829a4aSRandall Stewart } 557218e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS 557318e198d3SRandall Stewart { 557418e198d3SRandall Stewart int index, newindex; 557518e198d3SRandall Stewart struct sctp_pcbtsn_rlog *entry; 557618e198d3SRandall Stewart 557718e198d3SRandall Stewart do { 557818e198d3SRandall Stewart index = inp->readlog_index; 557918e198d3SRandall Stewart newindex = index + 1; 558018e198d3SRandall Stewart if (newindex >= SCTP_READ_LOG_SIZE) { 558118e198d3SRandall Stewart newindex = 0; 558218e198d3SRandall Stewart } 558318e198d3SRandall Stewart } while (atomic_cmpset_int(&inp->readlog_index, index, newindex) == 0); 558418e198d3SRandall Stewart entry = &inp->readlog[index]; 558518e198d3SRandall Stewart entry->vtag = control->sinfo_assoc_id; 558618e198d3SRandall Stewart entry->strm = control->sinfo_stream; 558718e198d3SRandall Stewart entry->seq = control->sinfo_ssn; 558818e198d3SRandall Stewart entry->sz = control->length; 558918e198d3SRandall Stewart entry->flgs = control->sinfo_flags; 559018e198d3SRandall Stewart } 559118e198d3SRandall Stewart #endif 5592f8829a4aSRandall Stewart if (fromlen && from) { 5593b5b6e5c2SMichael Tuexen cp_len = min((size_t)fromlen, (size_t)control->whoFrom->ro._l_addr.sa.sa_len); 5594b5b6e5c2SMichael Tuexen switch (control->whoFrom->ro._l_addr.sa.sa_family) { 5595b5b6e5c2SMichael Tuexen #ifdef INET6 5596b5b6e5c2SMichael Tuexen case AF_INET6: 5597f8829a4aSRandall Stewart ((struct sockaddr_in6 *)from)->sin6_port = control->port_from; 5598b5b6e5c2SMichael Tuexen break; 5599f8829a4aSRandall Stewart #endif 5600b5b6e5c2SMichael Tuexen #ifdef INET 5601b5b6e5c2SMichael Tuexen case AF_INET: 5602b5b6e5c2SMichael Tuexen ((struct sockaddr_in *)from)->sin_port = control->port_from; 5603b5b6e5c2SMichael Tuexen break; 5604b5b6e5c2SMichael Tuexen #endif 5605b5b6e5c2SMichael Tuexen default: 5606b5b6e5c2SMichael Tuexen break; 5607b5b6e5c2SMichael Tuexen } 5608b5b6e5c2SMichael Tuexen memcpy(from, &control->whoFrom->ro._l_addr, cp_len); 5609f8829a4aSRandall Stewart 561042551e99SRandall Stewart #if defined(INET) && defined(INET6) 56115e2c2d87SRandall Stewart if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) && 5612b5b6e5c2SMichael Tuexen (from->sa_family == AF_INET) && 5613f8829a4aSRandall Stewart ((size_t)fromlen >= sizeof(struct sockaddr_in6))) { 5614f8829a4aSRandall Stewart struct sockaddr_in *sin; 5615f8829a4aSRandall Stewart struct sockaddr_in6 sin6; 5616f8829a4aSRandall Stewart 5617b5b6e5c2SMichael Tuexen sin = (struct sockaddr_in *)from; 5618f8829a4aSRandall Stewart bzero(&sin6, sizeof(sin6)); 5619f8829a4aSRandall Stewart sin6.sin6_family = AF_INET6; 5620f8829a4aSRandall Stewart sin6.sin6_len = sizeof(struct sockaddr_in6); 5621d6af161aSRandall Stewart sin6.sin6_addr.s6_addr32[2] = htonl(0xffff); 5622f8829a4aSRandall Stewart bcopy(&sin->sin_addr, 5623d6af161aSRandall Stewart &sin6.sin6_addr.s6_addr32[3], 5624d6af161aSRandall Stewart sizeof(sin6.sin6_addr.s6_addr32[3])); 5625f8829a4aSRandall Stewart sin6.sin6_port = sin->sin_port; 5626b5b6e5c2SMichael Tuexen memcpy(from, &sin6, sizeof(struct sockaddr_in6)); 5627f8829a4aSRandall Stewart } 5628f8829a4aSRandall Stewart #endif 5629e0e00a4dSMichael Tuexen #ifdef INET6 5630f8829a4aSRandall Stewart { 5631b5b6e5c2SMichael Tuexen struct sockaddr_in6 lsa6, *from6; 5632f8829a4aSRandall Stewart 5633b5b6e5c2SMichael Tuexen from6 = (struct sockaddr_in6 *)from; 5634b5b6e5c2SMichael Tuexen sctp_recover_scope_mac(from6, (&lsa6)); 5635f8829a4aSRandall Stewart } 5636f8829a4aSRandall Stewart #endif 5637f8829a4aSRandall Stewart } 5638f8829a4aSRandall Stewart /* now copy out what data we can */ 5639f8829a4aSRandall Stewart if (mp == NULL) { 5640f8829a4aSRandall Stewart /* copy out each mbuf in the chain up to length */ 5641f8829a4aSRandall Stewart get_more_data: 5642f8829a4aSRandall Stewart m = control->data; 5643f8829a4aSRandall Stewart while (m) { 5644f8829a4aSRandall Stewart /* Move out all we can */ 5645f8829a4aSRandall Stewart cp_len = (int)uio->uio_resid; 5646139bc87fSRandall Stewart my_len = (int)SCTP_BUF_LEN(m); 5647f8829a4aSRandall Stewart if (cp_len > my_len) { 5648f8829a4aSRandall Stewart /* not enough in this buf */ 5649f8829a4aSRandall Stewart cp_len = my_len; 5650f8829a4aSRandall Stewart } 5651f8829a4aSRandall Stewart if (hold_rlock) { 5652f8829a4aSRandall Stewart SCTP_INP_READ_UNLOCK(inp); 5653f8829a4aSRandall Stewart hold_rlock = 0; 5654f8829a4aSRandall Stewart } 5655f8829a4aSRandall Stewart if (cp_len > 0) 5656f8829a4aSRandall Stewart error = uiomove(mtod(m, char *), cp_len, uio); 5657f8829a4aSRandall Stewart /* re-read */ 5658f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 5659f8829a4aSRandall Stewart goto release; 5660f8829a4aSRandall Stewart } 56610696e120SRandall Stewart if ((control->do_not_ref_stcb == 0) && stcb && 5662f8829a4aSRandall Stewart stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { 5663f8829a4aSRandall Stewart no_rcv_needed = 1; 5664f8829a4aSRandall Stewart } 5665f8829a4aSRandall Stewart if (error) { 5666f8829a4aSRandall Stewart /* error we are out of here */ 5667f8829a4aSRandall Stewart goto release; 5668f8829a4aSRandall Stewart } 5669139bc87fSRandall Stewart if ((SCTP_BUF_NEXT(m) == NULL) && 5670139bc87fSRandall Stewart (cp_len >= SCTP_BUF_LEN(m)) && 5671f8829a4aSRandall Stewart ((control->end_added == 0) || 56720696e120SRandall Stewart (control->end_added && 56730696e120SRandall Stewart (TAILQ_NEXT(control, next) == NULL))) 5674f8829a4aSRandall Stewart ) { 5675f8829a4aSRandall Stewart SCTP_INP_READ_LOCK(inp); 5676f8829a4aSRandall Stewart hold_rlock = 1; 5677f8829a4aSRandall Stewart } 5678139bc87fSRandall Stewart if (cp_len == SCTP_BUF_LEN(m)) { 5679139bc87fSRandall Stewart if ((SCTP_BUF_NEXT(m) == NULL) && 5680139bc87fSRandall Stewart (control->end_added)) { 5681f8829a4aSRandall Stewart out_flags |= MSG_EOR; 568252129fcdSRandall Stewart if ((control->do_not_ref_stcb == 0) && 568352129fcdSRandall Stewart (control->stcb != NULL) && 568452129fcdSRandall Stewart ((control->spec_flags & M_NOTIFICATION) == 0)) 5685ee7f9857SRandall Stewart control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0; 5686f8829a4aSRandall Stewart } 5687139bc87fSRandall Stewart if (control->spec_flags & M_NOTIFICATION) { 5688f8829a4aSRandall Stewart out_flags |= MSG_NOTIFICATION; 5689f8829a4aSRandall Stewart } 5690f8829a4aSRandall Stewart /* we ate up the mbuf */ 5691f8829a4aSRandall Stewart if (in_flags & MSG_PEEK) { 5692f8829a4aSRandall Stewart /* just looking */ 5693139bc87fSRandall Stewart m = SCTP_BUF_NEXT(m); 5694f8829a4aSRandall Stewart copied_so_far += cp_len; 5695f8829a4aSRandall Stewart } else { 5696f8829a4aSRandall Stewart /* dispose of the mbuf */ 5697b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { 5698f8829a4aSRandall Stewart sctp_sblog(&so->so_rcv, 5699139bc87fSRandall Stewart control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m)); 570080fefe0aSRandall Stewart } 5701f8829a4aSRandall Stewart sctp_sbfree(control, stcb, &so->so_rcv, m); 5702b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { 5703f8829a4aSRandall Stewart sctp_sblog(&so->so_rcv, 5704f8829a4aSRandall Stewart control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0); 570580fefe0aSRandall Stewart } 5706f8829a4aSRandall Stewart copied_so_far += cp_len; 5707f8829a4aSRandall Stewart freed_so_far += cp_len; 5708c4739e2fSRandall Stewart freed_so_far += MSIZE; 570918e198d3SRandall Stewart atomic_subtract_int(&control->length, cp_len); 5710f8829a4aSRandall Stewart control->data = sctp_m_free(m); 5711f8829a4aSRandall Stewart m = control->data; 5712f8829a4aSRandall Stewart /* 5713f8829a4aSRandall Stewart * been through it all, must hold sb 5714f8829a4aSRandall Stewart * lock ok to null tail 5715f8829a4aSRandall Stewart */ 5716f8829a4aSRandall Stewart if (control->data == NULL) { 5717a5d547adSRandall Stewart #ifdef INVARIANTS 5718f8829a4aSRandall Stewart if ((control->end_added == 0) || 5719f8829a4aSRandall Stewart (TAILQ_NEXT(control, next) == NULL)) { 5720f8829a4aSRandall Stewart /* 5721f8829a4aSRandall Stewart * If the end is not 5722f8829a4aSRandall Stewart * added, OR the 5723f8829a4aSRandall Stewart * next is NOT null 5724f8829a4aSRandall Stewart * we MUST have the 5725f8829a4aSRandall Stewart * lock. 5726f8829a4aSRandall Stewart */ 5727f8829a4aSRandall Stewart if (mtx_owned(&inp->inp_rdata_mtx) == 0) { 5728f8829a4aSRandall Stewart panic("Hmm we don't own the lock?"); 5729f8829a4aSRandall Stewart } 5730f8829a4aSRandall Stewart } 5731f8829a4aSRandall Stewart #endif 5732f8829a4aSRandall Stewart control->tail_mbuf = NULL; 5733a5d547adSRandall Stewart #ifdef INVARIANTS 5734f8829a4aSRandall Stewart if ((control->end_added) && ((out_flags & MSG_EOR) == 0)) { 5735f8829a4aSRandall Stewart panic("end_added, nothing left and no MSG_EOR"); 5736f8829a4aSRandall Stewart } 5737f8829a4aSRandall Stewart #endif 5738f8829a4aSRandall Stewart } 5739f8829a4aSRandall Stewart } 5740f8829a4aSRandall Stewart } else { 5741f8829a4aSRandall Stewart /* Do we need to trim the mbuf? */ 5742139bc87fSRandall Stewart if (control->spec_flags & M_NOTIFICATION) { 5743f8829a4aSRandall Stewart out_flags |= MSG_NOTIFICATION; 5744f8829a4aSRandall Stewart } 5745f8829a4aSRandall Stewart if ((in_flags & MSG_PEEK) == 0) { 5746139bc87fSRandall Stewart SCTP_BUF_RESV_UF(m, cp_len); 5747139bc87fSRandall Stewart SCTP_BUF_LEN(m) -= cp_len; 5748b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { 5749f8829a4aSRandall Stewart sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, cp_len); 575080fefe0aSRandall Stewart } 5751f8829a4aSRandall Stewart atomic_subtract_int(&so->so_rcv.sb_cc, cp_len); 57520696e120SRandall Stewart if ((control->do_not_ref_stcb == 0) && 57530696e120SRandall Stewart stcb) { 5754f8829a4aSRandall Stewart atomic_subtract_int(&stcb->asoc.sb_cc, cp_len); 5755f8829a4aSRandall Stewart } 5756f8829a4aSRandall Stewart copied_so_far += cp_len; 5757f8829a4aSRandall Stewart freed_so_far += cp_len; 5758c4739e2fSRandall Stewart freed_so_far += MSIZE; 5759b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { 5760f8829a4aSRandall Stewart sctp_sblog(&so->so_rcv, control->do_not_ref_stcb ? NULL : stcb, 5761f8829a4aSRandall Stewart SCTP_LOG_SBRESULT, 0); 576280fefe0aSRandall Stewart } 576318e198d3SRandall Stewart atomic_subtract_int(&control->length, cp_len); 5764f8829a4aSRandall Stewart } else { 5765f8829a4aSRandall Stewart copied_so_far += cp_len; 5766f8829a4aSRandall Stewart } 5767f8829a4aSRandall Stewart } 5768d61a0ae0SRandall Stewart if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) { 5769f8829a4aSRandall Stewart break; 5770f8829a4aSRandall Stewart } 5771f8829a4aSRandall Stewart if (((stcb) && (in_flags & MSG_PEEK) == 0) && 5772f8829a4aSRandall Stewart (control->do_not_ref_stcb == 0) && 5773f8829a4aSRandall Stewart (freed_so_far >= rwnd_req)) { 5774f8829a4aSRandall Stewart sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req); 5775f8829a4aSRandall Stewart } 5776f8829a4aSRandall Stewart } /* end while(m) */ 5777f8829a4aSRandall Stewart /* 5778f8829a4aSRandall Stewart * At this point we have looked at it all and we either have 5779f8829a4aSRandall Stewart * a MSG_EOR/or read all the user wants... <OR> 5780f8829a4aSRandall Stewart * control->length == 0. 5781f8829a4aSRandall Stewart */ 5782d61a0ae0SRandall Stewart if ((out_flags & MSG_EOR) && ((in_flags & MSG_PEEK) == 0)) { 5783f8829a4aSRandall Stewart /* we are done with this control */ 5784f8829a4aSRandall Stewart if (control->length == 0) { 5785f8829a4aSRandall Stewart if (control->data) { 5786a5d547adSRandall Stewart #ifdef INVARIANTS 5787f8829a4aSRandall Stewart panic("control->data not null at read eor?"); 5788f8829a4aSRandall Stewart #else 5789ad81507eSRandall Stewart SCTP_PRINTF("Strange, data left in the control buffer .. invarients would panic?\n"); 5790f8829a4aSRandall Stewart sctp_m_freem(control->data); 5791f8829a4aSRandall Stewart control->data = NULL; 5792f8829a4aSRandall Stewart #endif 5793f8829a4aSRandall Stewart } 5794f8829a4aSRandall Stewart done_with_control: 5795f8829a4aSRandall Stewart if (TAILQ_NEXT(control, next) == NULL) { 5796f8829a4aSRandall Stewart /* 5797f8829a4aSRandall Stewart * If we don't have a next we need a 5798b201f536SRandall Stewart * lock, if there is a next 5799b201f536SRandall Stewart * interrupt is filling ahead of us 5800b201f536SRandall Stewart * and we don't need a lock to 5801b201f536SRandall Stewart * remove this guy (which is the 5802b201f536SRandall Stewart * head of the queue). 5803f8829a4aSRandall Stewart */ 5804f8829a4aSRandall Stewart if (hold_rlock == 0) { 5805f8829a4aSRandall Stewart SCTP_INP_READ_LOCK(inp); 5806f8829a4aSRandall Stewart hold_rlock = 1; 5807f8829a4aSRandall Stewart } 5808f8829a4aSRandall Stewart } 5809f8829a4aSRandall Stewart TAILQ_REMOVE(&inp->read_queue, control, next); 5810f8829a4aSRandall Stewart /* Add back any hiddend data */ 5811f8829a4aSRandall Stewart if (control->held_length) { 5812f8829a4aSRandall Stewart held_length = 0; 5813f8829a4aSRandall Stewart control->held_length = 0; 5814f8829a4aSRandall Stewart wakeup_read_socket = 1; 5815f8829a4aSRandall Stewart } 581617205eccSRandall Stewart if (control->aux_data) { 581717205eccSRandall Stewart sctp_m_free(control->aux_data); 581817205eccSRandall Stewart control->aux_data = NULL; 581917205eccSRandall Stewart } 5820f8829a4aSRandall Stewart no_rcv_needed = control->do_not_ref_stcb; 5821f8829a4aSRandall Stewart sctp_free_remote_addr(control->whoFrom); 5822f8829a4aSRandall Stewart control->data = NULL; 5823f8829a4aSRandall Stewart sctp_free_a_readq(stcb, control); 5824f8829a4aSRandall Stewart control = NULL; 58250696e120SRandall Stewart if ((freed_so_far >= rwnd_req) && 58260696e120SRandall Stewart (no_rcv_needed == 0)) 5827f8829a4aSRandall Stewart sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req); 5828f8829a4aSRandall Stewart 5829f8829a4aSRandall Stewart } else { 5830f8829a4aSRandall Stewart /* 5831f8829a4aSRandall Stewart * The user did not read all of this 5832f8829a4aSRandall Stewart * message, turn off the returned MSG_EOR 5833f8829a4aSRandall Stewart * since we are leaving more behind on the 5834f8829a4aSRandall Stewart * control to read. 5835f8829a4aSRandall Stewart */ 5836a5d547adSRandall Stewart #ifdef INVARIANTS 58370696e120SRandall Stewart if (control->end_added && 58380696e120SRandall Stewart (control->data == NULL) && 5839f8829a4aSRandall Stewart (control->tail_mbuf == NULL)) { 5840f8829a4aSRandall Stewart panic("Gak, control->length is corrupt?"); 5841f8829a4aSRandall Stewart } 5842f8829a4aSRandall Stewart #endif 5843f8829a4aSRandall Stewart no_rcv_needed = control->do_not_ref_stcb; 5844f8829a4aSRandall Stewart out_flags &= ~MSG_EOR; 5845f8829a4aSRandall Stewart } 5846f8829a4aSRandall Stewart } 5847f8829a4aSRandall Stewart if (out_flags & MSG_EOR) { 5848f8829a4aSRandall Stewart goto release; 5849f8829a4aSRandall Stewart } 5850f8829a4aSRandall Stewart if ((uio->uio_resid == 0) || 5851f8829a4aSRandall Stewart ((in_eeor_mode) && (copied_so_far >= max(so->so_rcv.sb_lowat, 1))) 5852f8829a4aSRandall Stewart ) { 5853f8829a4aSRandall Stewart goto release; 5854f8829a4aSRandall Stewart } 5855f8829a4aSRandall Stewart /* 5856f8829a4aSRandall Stewart * If I hit here the receiver wants more and this message is 5857f8829a4aSRandall Stewart * NOT done (pd-api). So two questions. Can we block? if not 5858f8829a4aSRandall Stewart * we are done. Did the user NOT set MSG_WAITALL? 5859f8829a4aSRandall Stewart */ 5860f8829a4aSRandall Stewart if (block_allowed == 0) { 5861f8829a4aSRandall Stewart goto release; 5862f8829a4aSRandall Stewart } 5863f8829a4aSRandall Stewart /* 5864f8829a4aSRandall Stewart * We need to wait for more data a few things: - We don't 5865f8829a4aSRandall Stewart * sbunlock() so we don't get someone else reading. - We 5866f8829a4aSRandall Stewart * must be sure to account for the case where what is added 5867f8829a4aSRandall Stewart * is NOT to our control when we wakeup. 5868f8829a4aSRandall Stewart */ 5869f8829a4aSRandall Stewart 5870f8829a4aSRandall Stewart /* 5871f8829a4aSRandall Stewart * Do we need to tell the transport a rwnd update might be 5872f8829a4aSRandall Stewart * needed before we go to sleep? 5873f8829a4aSRandall Stewart */ 5874f8829a4aSRandall Stewart if (((stcb) && (in_flags & MSG_PEEK) == 0) && 5875f8829a4aSRandall Stewart ((freed_so_far >= rwnd_req) && 5876f8829a4aSRandall Stewart (control->do_not_ref_stcb == 0) && 5877f8829a4aSRandall Stewart (no_rcv_needed == 0))) { 5878f8829a4aSRandall Stewart sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req); 5879f8829a4aSRandall Stewart } 5880f8829a4aSRandall Stewart wait_some_more: 588144b7479bSRandall Stewart if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 5882f8829a4aSRandall Stewart goto release; 5883f8829a4aSRandall Stewart } 5884f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) 5885f8829a4aSRandall Stewart goto release; 5886f8829a4aSRandall Stewart 5887f8829a4aSRandall Stewart if (hold_rlock == 1) { 5888f8829a4aSRandall Stewart SCTP_INP_READ_UNLOCK(inp); 5889f8829a4aSRandall Stewart hold_rlock = 0; 5890f8829a4aSRandall Stewart } 5891f8829a4aSRandall Stewart if (hold_sblock == 0) { 5892f8829a4aSRandall Stewart SOCKBUF_LOCK(&so->so_rcv); 5893f8829a4aSRandall Stewart hold_sblock = 1; 5894f8829a4aSRandall Stewart } 5895851b7298SRandall Stewart if ((copied_so_far) && (control->length == 0) && 5896b5c16493SMichael Tuexen (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE))) { 5897851b7298SRandall Stewart goto release; 5898851b7298SRandall Stewart } 5899f8829a4aSRandall Stewart if (so->so_rcv.sb_cc <= control->held_length) { 5900f8829a4aSRandall Stewart error = sbwait(&so->so_rcv); 5901f8829a4aSRandall Stewart if (error) { 5902f8829a4aSRandall Stewart goto release; 5903f8829a4aSRandall Stewart } 5904f8829a4aSRandall Stewart control->held_length = 0; 5905f8829a4aSRandall Stewart } 5906f8829a4aSRandall Stewart if (hold_sblock) { 5907f8829a4aSRandall Stewart SOCKBUF_UNLOCK(&so->so_rcv); 5908f8829a4aSRandall Stewart hold_sblock = 0; 5909f8829a4aSRandall Stewart } 5910f8829a4aSRandall Stewart if (control->length == 0) { 5911f8829a4aSRandall Stewart /* still nothing here */ 5912f8829a4aSRandall Stewart if (control->end_added == 1) { 5913f8829a4aSRandall Stewart /* he aborted, or is done i.e.did a shutdown */ 5914f8829a4aSRandall Stewart out_flags |= MSG_EOR; 59159a6142d8SRandall Stewart if (control->pdapi_aborted) { 59166114cd96SRandall Stewart if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0)) 5917ee7f9857SRandall Stewart control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0; 59189a6142d8SRandall Stewart 591903b0b021SRandall Stewart out_flags |= MSG_TRUNC; 59209a6142d8SRandall Stewart } else { 59216114cd96SRandall Stewart if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0)) 5922ee7f9857SRandall Stewart control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0; 59239a6142d8SRandall Stewart } 5924f8829a4aSRandall Stewart goto done_with_control; 5925f8829a4aSRandall Stewart } 5926f8829a4aSRandall Stewart if (so->so_rcv.sb_cc > held_length) { 5927f8829a4aSRandall Stewart control->held_length = so->so_rcv.sb_cc; 5928f8829a4aSRandall Stewart held_length = 0; 5929f8829a4aSRandall Stewart } 5930f8829a4aSRandall Stewart goto wait_some_more; 5931f8829a4aSRandall Stewart } else if (control->data == NULL) { 593250cec919SRandall Stewart /* 593350cec919SRandall Stewart * we must re-sync since data is probably being 593450cec919SRandall Stewart * added 593550cec919SRandall Stewart */ 593650cec919SRandall Stewart SCTP_INP_READ_LOCK(inp); 593750cec919SRandall Stewart if ((control->length > 0) && (control->data == NULL)) { 593850cec919SRandall Stewart /* 593950cec919SRandall Stewart * big trouble.. we have the lock and its 594050cec919SRandall Stewart * corrupt? 594150cec919SRandall Stewart */ 59429c04b296SRandall Stewart #ifdef INVARIANTS 5943f8829a4aSRandall Stewart panic("Impossible data==NULL length !=0"); 59449c04b296SRandall Stewart #endif 59459c04b296SRandall Stewart out_flags |= MSG_EOR; 59469c04b296SRandall Stewart out_flags |= MSG_TRUNC; 59479c04b296SRandall Stewart control->length = 0; 59489c04b296SRandall Stewart SCTP_INP_READ_UNLOCK(inp); 59499c04b296SRandall Stewart goto done_with_control; 5950f8829a4aSRandall Stewart } 595150cec919SRandall Stewart SCTP_INP_READ_UNLOCK(inp); 595250cec919SRandall Stewart /* We will fall around to get more data */ 595350cec919SRandall Stewart } 5954f8829a4aSRandall Stewart goto get_more_data; 5955f8829a4aSRandall Stewart } else { 595617205eccSRandall Stewart /*- 595717205eccSRandall Stewart * Give caller back the mbuf chain, 595817205eccSRandall Stewart * store in uio_resid the length 5959f8829a4aSRandall Stewart */ 596017205eccSRandall Stewart wakeup_read_socket = 0; 5961f8829a4aSRandall Stewart if ((control->end_added == 0) || 5962f8829a4aSRandall Stewart (TAILQ_NEXT(control, next) == NULL)) { 5963f8829a4aSRandall Stewart /* Need to get rlock */ 5964f8829a4aSRandall Stewart if (hold_rlock == 0) { 5965f8829a4aSRandall Stewart SCTP_INP_READ_LOCK(inp); 5966f8829a4aSRandall Stewart hold_rlock = 1; 5967f8829a4aSRandall Stewart } 5968f8829a4aSRandall Stewart } 5969139bc87fSRandall Stewart if (control->end_added) { 5970f8829a4aSRandall Stewart out_flags |= MSG_EOR; 597160990c0cSMichael Tuexen if ((control->do_not_ref_stcb == 0) && 597260990c0cSMichael Tuexen (control->stcb != NULL) && 597360990c0cSMichael Tuexen ((control->spec_flags & M_NOTIFICATION) == 0)) 5974ee7f9857SRandall Stewart control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0; 5975f8829a4aSRandall Stewart } 5976139bc87fSRandall Stewart if (control->spec_flags & M_NOTIFICATION) { 5977f8829a4aSRandall Stewart out_flags |= MSG_NOTIFICATION; 5978f8829a4aSRandall Stewart } 597917205eccSRandall Stewart uio->uio_resid = control->length; 5980f8829a4aSRandall Stewart *mp = control->data; 5981f8829a4aSRandall Stewart m = control->data; 5982f8829a4aSRandall Stewart while (m) { 5983b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { 5984f8829a4aSRandall Stewart sctp_sblog(&so->so_rcv, 5985139bc87fSRandall Stewart control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m)); 598680fefe0aSRandall Stewart } 5987f8829a4aSRandall Stewart sctp_sbfree(control, stcb, &so->so_rcv, m); 5988139bc87fSRandall Stewart freed_so_far += SCTP_BUF_LEN(m); 5989c4739e2fSRandall Stewart freed_so_far += MSIZE; 5990b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) { 5991f8829a4aSRandall Stewart sctp_sblog(&so->so_rcv, 5992f8829a4aSRandall Stewart control->do_not_ref_stcb ? NULL : stcb, SCTP_LOG_SBRESULT, 0); 599380fefe0aSRandall Stewart } 5994139bc87fSRandall Stewart m = SCTP_BUF_NEXT(m); 5995f8829a4aSRandall Stewart } 5996f8829a4aSRandall Stewart control->data = control->tail_mbuf = NULL; 5997f8829a4aSRandall Stewart control->length = 0; 5998f8829a4aSRandall Stewart if (out_flags & MSG_EOR) { 5999f8829a4aSRandall Stewart /* Done with this control */ 6000f8829a4aSRandall Stewart goto done_with_control; 6001f8829a4aSRandall Stewart } 6002f8829a4aSRandall Stewart } 6003f8829a4aSRandall Stewart release: 6004f8829a4aSRandall Stewart if (hold_rlock == 1) { 6005f8829a4aSRandall Stewart SCTP_INP_READ_UNLOCK(inp); 6006f8829a4aSRandall Stewart hold_rlock = 0; 6007f8829a4aSRandall Stewart } 60087abab911SRobert Watson if (hold_sblock == 1) { 60097abab911SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 60107abab911SRobert Watson hold_sblock = 0; 6011f8829a4aSRandall Stewart } 6012f8829a4aSRandall Stewart sbunlock(&so->so_rcv); 60137abab911SRobert Watson sockbuf_lock = 0; 6014f8829a4aSRandall Stewart 6015f8829a4aSRandall Stewart release_unlocked: 6016f8829a4aSRandall Stewart if (hold_sblock) { 6017f8829a4aSRandall Stewart SOCKBUF_UNLOCK(&so->so_rcv); 6018f8829a4aSRandall Stewart hold_sblock = 0; 6019f8829a4aSRandall Stewart } 6020f8829a4aSRandall Stewart if ((stcb) && (in_flags & MSG_PEEK) == 0) { 6021f8829a4aSRandall Stewart if ((freed_so_far >= rwnd_req) && 6022f8829a4aSRandall Stewart (control && (control->do_not_ref_stcb == 0)) && 6023f8829a4aSRandall Stewart (no_rcv_needed == 0)) 6024f8829a4aSRandall Stewart sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req); 6025f8829a4aSRandall Stewart } 6026f8829a4aSRandall Stewart out: 60271b9f62a0SRandall Stewart if (msg_flags) { 60281b9f62a0SRandall Stewart *msg_flags = out_flags; 60291b9f62a0SRandall Stewart } 60309a6142d8SRandall Stewart if (((out_flags & MSG_EOR) == 0) && 60319a6142d8SRandall Stewart ((in_flags & MSG_PEEK) == 0) && 60329a6142d8SRandall Stewart (sinfo) && 6033e2e7c62eSMichael Tuexen (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) || 6034e2e7c62eSMichael Tuexen sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO))) { 60359a6142d8SRandall Stewart struct sctp_extrcvinfo *s_extra; 60369a6142d8SRandall Stewart 60379a6142d8SRandall Stewart s_extra = (struct sctp_extrcvinfo *)sinfo; 60389a6142d8SRandall Stewart s_extra->sreinfo_next_flags = SCTP_NO_NEXT_MSG; 60399a6142d8SRandall Stewart } 6040f8829a4aSRandall Stewart if (hold_rlock == 1) { 6041f8829a4aSRandall Stewart SCTP_INP_READ_UNLOCK(inp); 6042f8829a4aSRandall Stewart } 6043f8829a4aSRandall Stewart if (hold_sblock) { 6044f8829a4aSRandall Stewart SOCKBUF_UNLOCK(&so->so_rcv); 6045f8829a4aSRandall Stewart } 60467abab911SRobert Watson if (sockbuf_lock) { 60477abab911SRobert Watson sbunlock(&so->so_rcv); 60487abab911SRobert Watson } 604950cec919SRandall Stewart if (freecnt_applied) { 6050f8829a4aSRandall Stewart /* 6051f8829a4aSRandall Stewart * The lock on the socket buffer protects us so the free 6052f8829a4aSRandall Stewart * code will stop. But since we used the socketbuf lock and 6053f8829a4aSRandall Stewart * the sender uses the tcb_lock to increment, we need to use 6054f8829a4aSRandall Stewart * the atomic add to the refcnt. 6055f8829a4aSRandall Stewart */ 605650cec919SRandall Stewart if (stcb == NULL) { 6057df6e0cc3SRandall Stewart #ifdef INVARIANTS 605850cec919SRandall Stewart panic("stcb for refcnt has gone NULL?"); 6059df6e0cc3SRandall Stewart goto stage_left; 6060df6e0cc3SRandall Stewart #else 6061df6e0cc3SRandall Stewart goto stage_left; 6062df6e0cc3SRandall Stewart #endif 606350cec919SRandall Stewart } 606450cec919SRandall Stewart atomic_add_int(&stcb->asoc.refcnt, -1); 6065f8829a4aSRandall Stewart /* Save the value back for next time */ 6066f8829a4aSRandall Stewart stcb->freed_by_sorcv_sincelast = freed_so_far; 6067f8829a4aSRandall Stewart } 6068b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) { 6069f8829a4aSRandall Stewart if (stcb) { 6070f8829a4aSRandall Stewart sctp_misc_ints(SCTP_SORECV_DONE, 6071f8829a4aSRandall Stewart freed_so_far, 6072f8829a4aSRandall Stewart ((uio) ? (slen - uio->uio_resid) : slen), 6073f8829a4aSRandall Stewart stcb->asoc.my_rwnd, 6074f8829a4aSRandall Stewart so->so_rcv.sb_cc); 6075f8829a4aSRandall Stewart } else { 6076f8829a4aSRandall Stewart sctp_misc_ints(SCTP_SORECV_DONE, 6077f8829a4aSRandall Stewart freed_so_far, 6078f8829a4aSRandall Stewart ((uio) ? (slen - uio->uio_resid) : slen), 6079f8829a4aSRandall Stewart 0, 6080f8829a4aSRandall Stewart so->so_rcv.sb_cc); 6081f8829a4aSRandall Stewart } 608280fefe0aSRandall Stewart } 6083df6e0cc3SRandall Stewart stage_left: 6084f8829a4aSRandall Stewart if (wakeup_read_socket) { 6085f8829a4aSRandall Stewart sctp_sorwakeup(inp, so); 6086f8829a4aSRandall Stewart } 6087f8829a4aSRandall Stewart return (error); 6088f8829a4aSRandall Stewart } 6089f8829a4aSRandall Stewart 6090f8829a4aSRandall Stewart 6091f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING 6092f8829a4aSRandall Stewart struct mbuf * 6093f8829a4aSRandall Stewart sctp_m_free(struct mbuf *m) 6094f8829a4aSRandall Stewart { 6095b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 6096139bc87fSRandall Stewart if (SCTP_BUF_IS_EXTENDED(m)) { 6097f8829a4aSRandall Stewart sctp_log_mb(m, SCTP_MBUF_IFREE); 6098f8829a4aSRandall Stewart } 609980fefe0aSRandall Stewart } 6100f8829a4aSRandall Stewart return (m_free(m)); 6101f8829a4aSRandall Stewart } 6102f8829a4aSRandall Stewart 6103f8829a4aSRandall Stewart void 6104f8829a4aSRandall Stewart sctp_m_freem(struct mbuf *mb) 6105f8829a4aSRandall Stewart { 6106f8829a4aSRandall Stewart while (mb != NULL) 6107f8829a4aSRandall Stewart mb = sctp_m_free(mb); 6108f8829a4aSRandall Stewart } 6109f8829a4aSRandall Stewart 6110f8829a4aSRandall Stewart #endif 6111f8829a4aSRandall Stewart 611242551e99SRandall Stewart int 611342551e99SRandall Stewart sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id) 611442551e99SRandall Stewart { 611542551e99SRandall Stewart /* 611642551e99SRandall Stewart * Given a local address. For all associations that holds the 611742551e99SRandall Stewart * address, request a peer-set-primary. 611842551e99SRandall Stewart */ 611942551e99SRandall Stewart struct sctp_ifa *ifa; 612042551e99SRandall Stewart struct sctp_laddr *wi; 612142551e99SRandall Stewart 612242551e99SRandall Stewart ifa = sctp_find_ifa_by_addr(sa, vrf_id, 0); 612342551e99SRandall Stewart if (ifa == NULL) { 6124c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EADDRNOTAVAIL); 612542551e99SRandall Stewart return (EADDRNOTAVAIL); 612642551e99SRandall Stewart } 612742551e99SRandall Stewart /* 612842551e99SRandall Stewart * Now that we have the ifa we must awaken the iterator with this 612942551e99SRandall Stewart * message. 613042551e99SRandall Stewart */ 6131b3f1ea41SRandall Stewart wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr); 613242551e99SRandall Stewart if (wi == NULL) { 6133c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOMEM); 613442551e99SRandall Stewart return (ENOMEM); 613542551e99SRandall Stewart } 613642551e99SRandall Stewart /* Now incr the count and int wi structure */ 613742551e99SRandall Stewart SCTP_INCR_LADDR_COUNT(); 613842551e99SRandall Stewart bzero(wi, sizeof(*wi)); 6139d61a0ae0SRandall Stewart (void)SCTP_GETTIME_TIMEVAL(&wi->start_time); 614042551e99SRandall Stewart wi->ifa = ifa; 614142551e99SRandall Stewart wi->action = SCTP_SET_PRIM_ADDR; 614242551e99SRandall Stewart atomic_add_int(&ifa->refcount, 1); 614342551e99SRandall Stewart 614442551e99SRandall Stewart /* Now add it to the work queue */ 6145f7517433SRandall Stewart SCTP_WQ_ADDR_LOCK(); 614642551e99SRandall Stewart /* 614742551e99SRandall Stewart * Should this really be a tailq? As it is we will process the 614842551e99SRandall Stewart * newest first :-0 614942551e99SRandall Stewart */ 6150b3f1ea41SRandall Stewart LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr); 6151f7517433SRandall Stewart SCTP_WQ_ADDR_UNLOCK(); 615242551e99SRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ, 615342551e99SRandall Stewart (struct sctp_inpcb *)NULL, 615442551e99SRandall Stewart (struct sctp_tcb *)NULL, 615542551e99SRandall Stewart (struct sctp_nets *)NULL); 615642551e99SRandall Stewart return (0); 615742551e99SRandall Stewart } 615842551e99SRandall Stewart 615942551e99SRandall Stewart 6160f8829a4aSRandall Stewart int 616117205eccSRandall Stewart sctp_soreceive(struct socket *so, 616217205eccSRandall Stewart struct sockaddr **psa, 616317205eccSRandall Stewart struct uio *uio, 616417205eccSRandall Stewart struct mbuf **mp0, 616517205eccSRandall Stewart struct mbuf **controlp, 616617205eccSRandall Stewart int *flagsp) 6167f8829a4aSRandall Stewart { 6168f8829a4aSRandall Stewart int error, fromlen; 6169f8829a4aSRandall Stewart uint8_t sockbuf[256]; 6170f8829a4aSRandall Stewart struct sockaddr *from; 6171f8829a4aSRandall Stewart struct sctp_extrcvinfo sinfo; 6172f8829a4aSRandall Stewart int filling_sinfo = 1; 6173f8829a4aSRandall Stewart struct sctp_inpcb *inp; 6174f8829a4aSRandall Stewart 6175f8829a4aSRandall Stewart inp = (struct sctp_inpcb *)so->so_pcb; 6176f8829a4aSRandall Stewart /* pickup the assoc we are reading from */ 6177f8829a4aSRandall Stewart if (inp == NULL) { 6178c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); 6179f8829a4aSRandall Stewart return (EINVAL); 6180f8829a4aSRandall Stewart } 6181e2e7c62eSMichael Tuexen if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) && 6182e2e7c62eSMichael Tuexen sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) && 6183e2e7c62eSMichael Tuexen sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) || 6184f8829a4aSRandall Stewart (controlp == NULL)) { 6185f8829a4aSRandall Stewart /* user does not want the sndrcv ctl */ 6186f8829a4aSRandall Stewart filling_sinfo = 0; 6187f8829a4aSRandall Stewart } 6188f8829a4aSRandall Stewart if (psa) { 6189f8829a4aSRandall Stewart from = (struct sockaddr *)sockbuf; 6190f8829a4aSRandall Stewart fromlen = sizeof(sockbuf); 6191f8829a4aSRandall Stewart from->sa_len = 0; 6192f8829a4aSRandall Stewart } else { 6193f8829a4aSRandall Stewart from = NULL; 6194f8829a4aSRandall Stewart fromlen = 0; 6195f8829a4aSRandall Stewart } 6196f8829a4aSRandall Stewart 6197f8829a4aSRandall Stewart error = sctp_sorecvmsg(so, uio, mp0, from, fromlen, flagsp, 6198f8829a4aSRandall Stewart (struct sctp_sndrcvinfo *)&sinfo, filling_sinfo); 6199f8829a4aSRandall Stewart if ((controlp) && (filling_sinfo)) { 6200f8829a4aSRandall Stewart /* copy back the sinfo in a CMSG format */ 6201f8829a4aSRandall Stewart if (filling_sinfo) 6202f8829a4aSRandall Stewart *controlp = sctp_build_ctl_nchunk(inp, 6203f8829a4aSRandall Stewart (struct sctp_sndrcvinfo *)&sinfo); 6204f8829a4aSRandall Stewart else 6205f8829a4aSRandall Stewart *controlp = NULL; 6206f8829a4aSRandall Stewart } 6207f8829a4aSRandall Stewart if (psa) { 6208f8829a4aSRandall Stewart /* copy back the address info */ 6209f8829a4aSRandall Stewart if (from && from->sa_len) { 6210f8829a4aSRandall Stewart *psa = sodupsockaddr(from, M_NOWAIT); 6211f8829a4aSRandall Stewart } else { 6212f8829a4aSRandall Stewart *psa = NULL; 6213f8829a4aSRandall Stewart } 6214f8829a4aSRandall Stewart } 6215f8829a4aSRandall Stewart return (error); 6216f8829a4aSRandall Stewart } 621717205eccSRandall Stewart 621817205eccSRandall Stewart 621917205eccSRandall Stewart 622017205eccSRandall Stewart 622117205eccSRandall Stewart 622217205eccSRandall Stewart int 6223d61a0ae0SRandall Stewart sctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr, 6224d61a0ae0SRandall Stewart int totaddr, int *error) 622517205eccSRandall Stewart { 622617205eccSRandall Stewart int added = 0; 622717205eccSRandall Stewart int i; 622817205eccSRandall Stewart struct sctp_inpcb *inp; 622917205eccSRandall Stewart struct sockaddr *sa; 623017205eccSRandall Stewart size_t incr = 0; 623117205eccSRandall Stewart 623292776dfdSMichael Tuexen #ifdef INET 623392776dfdSMichael Tuexen struct sockaddr_in *sin; 623492776dfdSMichael Tuexen 623592776dfdSMichael Tuexen #endif 623692776dfdSMichael Tuexen #ifdef INET6 623792776dfdSMichael Tuexen struct sockaddr_in6 *sin6; 623892776dfdSMichael Tuexen 623992776dfdSMichael Tuexen #endif 624092776dfdSMichael Tuexen 624117205eccSRandall Stewart sa = addr; 624217205eccSRandall Stewart inp = stcb->sctp_ep; 624317205eccSRandall Stewart *error = 0; 624417205eccSRandall Stewart for (i = 0; i < totaddr; i++) { 6245ea5eba11SMichael Tuexen switch (sa->sa_family) { 6246ea5eba11SMichael Tuexen #ifdef INET 6247ea5eba11SMichael Tuexen case AF_INET: 624817205eccSRandall Stewart incr = sizeof(struct sockaddr_in); 624992776dfdSMichael Tuexen sin = (struct sockaddr_in *)sa; 625092776dfdSMichael Tuexen if ((sin->sin_addr.s_addr == INADDR_ANY) || 625192776dfdSMichael Tuexen (sin->sin_addr.s_addr == INADDR_BROADCAST) || 625292776dfdSMichael Tuexen IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 625392776dfdSMichael Tuexen SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL); 625492776dfdSMichael Tuexen (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7); 625592776dfdSMichael Tuexen *error = EINVAL; 625692776dfdSMichael Tuexen goto out_now; 625792776dfdSMichael Tuexen } 6258ca85e948SMichael Tuexen if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) { 625917205eccSRandall Stewart /* assoc gone no un-lock */ 6260c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS); 6261c4739e2fSRandall Stewart (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7); 626217205eccSRandall Stewart *error = ENOBUFS; 626317205eccSRandall Stewart goto out_now; 626417205eccSRandall Stewart } 626517205eccSRandall Stewart added++; 6266ea5eba11SMichael Tuexen break; 6267ea5eba11SMichael Tuexen #endif 6268ea5eba11SMichael Tuexen #ifdef INET6 6269ea5eba11SMichael Tuexen case AF_INET6: 627017205eccSRandall Stewart incr = sizeof(struct sockaddr_in6); 627192776dfdSMichael Tuexen sin6 = (struct sockaddr_in6 *)sa; 627292776dfdSMichael Tuexen if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) || 627392776dfdSMichael Tuexen IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 627492776dfdSMichael Tuexen SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL); 627592776dfdSMichael Tuexen (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_8); 627692776dfdSMichael Tuexen *error = EINVAL; 627792776dfdSMichael Tuexen goto out_now; 627892776dfdSMichael Tuexen } 6279ca85e948SMichael Tuexen if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) { 628017205eccSRandall Stewart /* assoc gone no un-lock */ 6281c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS); 6282c4739e2fSRandall Stewart (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_8); 628317205eccSRandall Stewart *error = ENOBUFS; 628417205eccSRandall Stewart goto out_now; 628517205eccSRandall Stewart } 628617205eccSRandall Stewart added++; 6287ea5eba11SMichael Tuexen break; 6288ea5eba11SMichael Tuexen #endif 6289ea5eba11SMichael Tuexen default: 6290ea5eba11SMichael Tuexen break; 629117205eccSRandall Stewart } 629217205eccSRandall Stewart sa = (struct sockaddr *)((caddr_t)sa + incr); 629317205eccSRandall Stewart } 629417205eccSRandall Stewart out_now: 629517205eccSRandall Stewart return (added); 629617205eccSRandall Stewart } 629717205eccSRandall Stewart 629817205eccSRandall Stewart struct sctp_tcb * 6299d61a0ae0SRandall Stewart sctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr, 6300d61a0ae0SRandall Stewart int *totaddr, int *num_v4, int *num_v6, int *error, 6301d61a0ae0SRandall Stewart int limit, int *bad_addr) 630217205eccSRandall Stewart { 630317205eccSRandall Stewart struct sockaddr *sa; 630417205eccSRandall Stewart struct sctp_tcb *stcb = NULL; 630517205eccSRandall Stewart size_t incr, at, i; 630617205eccSRandall Stewart 630717205eccSRandall Stewart at = incr = 0; 630817205eccSRandall Stewart sa = addr; 6309ea5eba11SMichael Tuexen 631017205eccSRandall Stewart *error = *num_v6 = *num_v4 = 0; 631117205eccSRandall Stewart /* account and validate addresses */ 63124c9179adSRandall Stewart for (i = 0; i < (size_t)*totaddr; i++) { 6313ea5eba11SMichael Tuexen switch (sa->sa_family) { 6314ea5eba11SMichael Tuexen #ifdef INET 6315ea5eba11SMichael Tuexen case AF_INET: 631617205eccSRandall Stewart (*num_v4) += 1; 631717205eccSRandall Stewart incr = sizeof(struct sockaddr_in); 6318d61a0ae0SRandall Stewart if (sa->sa_len != incr) { 6319c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); 6320d61a0ae0SRandall Stewart *error = EINVAL; 6321d61a0ae0SRandall Stewart *bad_addr = 1; 6322d61a0ae0SRandall Stewart return (NULL); 6323d61a0ae0SRandall Stewart } 6324ea5eba11SMichael Tuexen break; 6325ea5eba11SMichael Tuexen #endif 6326ea5eba11SMichael Tuexen #ifdef INET6 6327ea5eba11SMichael Tuexen case AF_INET6: 6328ea5eba11SMichael Tuexen { 632917205eccSRandall Stewart struct sockaddr_in6 *sin6; 633017205eccSRandall Stewart 633117205eccSRandall Stewart sin6 = (struct sockaddr_in6 *)sa; 633217205eccSRandall Stewart if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 633317205eccSRandall Stewart /* Must be non-mapped for connectx */ 6334c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); 633517205eccSRandall Stewart *error = EINVAL; 6336d61a0ae0SRandall Stewart *bad_addr = 1; 633717205eccSRandall Stewart return (NULL); 633817205eccSRandall Stewart } 633917205eccSRandall Stewart (*num_v6) += 1; 634017205eccSRandall Stewart incr = sizeof(struct sockaddr_in6); 6341d61a0ae0SRandall Stewart if (sa->sa_len != incr) { 6342c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); 6343d61a0ae0SRandall Stewart *error = EINVAL; 6344d61a0ae0SRandall Stewart *bad_addr = 1; 6345d61a0ae0SRandall Stewart return (NULL); 6346d61a0ae0SRandall Stewart } 6347ea5eba11SMichael Tuexen break; 6348ea5eba11SMichael Tuexen } 6349ea5eba11SMichael Tuexen #endif 6350ea5eba11SMichael Tuexen default: 635117205eccSRandall Stewart *totaddr = i; 635217205eccSRandall Stewart /* we are done */ 635317205eccSRandall Stewart break; 635417205eccSRandall Stewart } 6355ea5eba11SMichael Tuexen if (i == (size_t)*totaddr) { 6356ea5eba11SMichael Tuexen break; 6357ea5eba11SMichael Tuexen } 6358d61a0ae0SRandall Stewart SCTP_INP_INCR_REF(inp); 635917205eccSRandall Stewart stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL); 636017205eccSRandall Stewart if (stcb != NULL) { 636117205eccSRandall Stewart /* Already have or am bring up an association */ 636217205eccSRandall Stewart return (stcb); 6363d61a0ae0SRandall Stewart } else { 6364d61a0ae0SRandall Stewart SCTP_INP_DECR_REF(inp); 636517205eccSRandall Stewart } 63664c9179adSRandall Stewart if ((at + incr) > (size_t)limit) { 636717205eccSRandall Stewart *totaddr = i; 636817205eccSRandall Stewart break; 636917205eccSRandall Stewart } 637017205eccSRandall Stewart sa = (struct sockaddr *)((caddr_t)sa + incr); 637117205eccSRandall Stewart } 637217205eccSRandall Stewart return ((struct sctp_tcb *)NULL); 637317205eccSRandall Stewart } 637435918f85SRandall Stewart 637535918f85SRandall Stewart /* 637635918f85SRandall Stewart * sctp_bindx(ADD) for one address. 637735918f85SRandall Stewart * assumes all arguments are valid/checked by caller. 637835918f85SRandall Stewart */ 637935918f85SRandall Stewart void 638035918f85SRandall Stewart sctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp, 638135918f85SRandall Stewart struct sockaddr *sa, sctp_assoc_t assoc_id, 638235918f85SRandall Stewart uint32_t vrf_id, int *error, void *p) 638335918f85SRandall Stewart { 638435918f85SRandall Stewart struct sockaddr *addr_touse; 63855e2c2d87SRandall Stewart 63865e2c2d87SRandall Stewart #ifdef INET6 638735918f85SRandall Stewart struct sockaddr_in sin; 638835918f85SRandall Stewart 63895e2c2d87SRandall Stewart #endif 63905e2c2d87SRandall Stewart 639135918f85SRandall Stewart /* see if we're bound all already! */ 639235918f85SRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 6393c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); 639435918f85SRandall Stewart *error = EINVAL; 639535918f85SRandall Stewart return; 639635918f85SRandall Stewart } 639735918f85SRandall Stewart addr_touse = sa; 6398ea5eba11SMichael Tuexen #ifdef INET6 639935918f85SRandall Stewart if (sa->sa_family == AF_INET6) { 640035918f85SRandall Stewart struct sockaddr_in6 *sin6; 640135918f85SRandall Stewart 640235918f85SRandall Stewart if (sa->sa_len != sizeof(struct sockaddr_in6)) { 6403c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); 640435918f85SRandall Stewart *error = EINVAL; 640535918f85SRandall Stewart return; 640635918f85SRandall Stewart } 6407db4fd95bSRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) { 6408db4fd95bSRandall Stewart /* can only bind v6 on PF_INET6 sockets */ 6409c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); 6410db4fd95bSRandall Stewart *error = EINVAL; 6411db4fd95bSRandall Stewart return; 6412db4fd95bSRandall Stewart } 641335918f85SRandall Stewart sin6 = (struct sockaddr_in6 *)addr_touse; 641435918f85SRandall Stewart if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6415db4fd95bSRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && 6416db4fd95bSRandall Stewart SCTP_IPV6_V6ONLY(inp)) { 6417db4fd95bSRandall Stewart /* can't bind v4-mapped on PF_INET sockets */ 6418c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); 6419db4fd95bSRandall Stewart *error = EINVAL; 6420db4fd95bSRandall Stewart return; 6421db4fd95bSRandall Stewart } 642235918f85SRandall Stewart in6_sin6_2_sin(&sin, sin6); 642335918f85SRandall Stewart addr_touse = (struct sockaddr *)&sin; 642435918f85SRandall Stewart } 642535918f85SRandall Stewart } 642635918f85SRandall Stewart #endif 6427ea5eba11SMichael Tuexen #ifdef INET 642835918f85SRandall Stewart if (sa->sa_family == AF_INET) { 642935918f85SRandall Stewart if (sa->sa_len != sizeof(struct sockaddr_in)) { 6430c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); 643135918f85SRandall Stewart *error = EINVAL; 643235918f85SRandall Stewart return; 643335918f85SRandall Stewart } 6434db4fd95bSRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && 6435db4fd95bSRandall Stewart SCTP_IPV6_V6ONLY(inp)) { 6436db4fd95bSRandall Stewart /* can't bind v4 on PF_INET sockets */ 6437c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); 6438db4fd95bSRandall Stewart *error = EINVAL; 6439db4fd95bSRandall Stewart return; 6440db4fd95bSRandall Stewart } 644135918f85SRandall Stewart } 6442ea5eba11SMichael Tuexen #endif 644335918f85SRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) { 644435918f85SRandall Stewart if (p == NULL) { 644535918f85SRandall Stewart /* Can't get proc for Net/Open BSD */ 6446c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); 644735918f85SRandall Stewart *error = EINVAL; 644835918f85SRandall Stewart return; 644935918f85SRandall Stewart } 64501b649582SRandall Stewart *error = sctp_inpcb_bind(so, addr_touse, NULL, p); 645135918f85SRandall Stewart return; 645235918f85SRandall Stewart } 645335918f85SRandall Stewart /* 645435918f85SRandall Stewart * No locks required here since bind and mgmt_ep_sa all do their own 645535918f85SRandall Stewart * locking. If we do something for the FIX: below we may need to 645635918f85SRandall Stewart * lock in that case. 645735918f85SRandall Stewart */ 645835918f85SRandall Stewart if (assoc_id == 0) { 645935918f85SRandall Stewart /* add the address */ 646035918f85SRandall Stewart struct sctp_inpcb *lep; 646197c76f10SRandall Stewart struct sockaddr_in *lsin = (struct sockaddr_in *)addr_touse; 646235918f85SRandall Stewart 646397c76f10SRandall Stewart /* validate the incoming port */ 646497c76f10SRandall Stewart if ((lsin->sin_port != 0) && 646597c76f10SRandall Stewart (lsin->sin_port != inp->sctp_lport)) { 6466c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); 646797c76f10SRandall Stewart *error = EINVAL; 646897c76f10SRandall Stewart return; 646997c76f10SRandall Stewart } else { 647097c76f10SRandall Stewart /* user specified 0 port, set it to existing port */ 647197c76f10SRandall Stewart lsin->sin_port = inp->sctp_lport; 647297c76f10SRandall Stewart } 647397c76f10SRandall Stewart 647435918f85SRandall Stewart lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id); 647535918f85SRandall Stewart if (lep != NULL) { 647635918f85SRandall Stewart /* 647735918f85SRandall Stewart * We must decrement the refcount since we have the 647835918f85SRandall Stewart * ep already and are binding. No remove going on 647935918f85SRandall Stewart * here. 648035918f85SRandall Stewart */ 64816d9e8f2bSRandall Stewart SCTP_INP_DECR_REF(lep); 648235918f85SRandall Stewart } 648335918f85SRandall Stewart if (lep == inp) { 648435918f85SRandall Stewart /* already bound to it.. ok */ 648535918f85SRandall Stewart return; 648635918f85SRandall Stewart } else if (lep == NULL) { 648735918f85SRandall Stewart ((struct sockaddr_in *)addr_touse)->sin_port = 0; 648835918f85SRandall Stewart *error = sctp_addr_mgmt_ep_sa(inp, addr_touse, 648935918f85SRandall Stewart SCTP_ADD_IP_ADDRESS, 649080fefe0aSRandall Stewart vrf_id, NULL); 649135918f85SRandall Stewart } else { 649235918f85SRandall Stewart *error = EADDRINUSE; 649335918f85SRandall Stewart } 649435918f85SRandall Stewart if (*error) 649535918f85SRandall Stewart return; 649635918f85SRandall Stewart } else { 649735918f85SRandall Stewart /* 649835918f85SRandall Stewart * FIX: decide whether we allow assoc based bindx 649935918f85SRandall Stewart */ 650035918f85SRandall Stewart } 650135918f85SRandall Stewart } 650235918f85SRandall Stewart 650335918f85SRandall Stewart /* 650435918f85SRandall Stewart * sctp_bindx(DELETE) for one address. 650535918f85SRandall Stewart * assumes all arguments are valid/checked by caller. 650635918f85SRandall Stewart */ 650735918f85SRandall Stewart void 65087215cc1bSMichael Tuexen sctp_bindx_delete_address(struct sctp_inpcb *inp, 650935918f85SRandall Stewart struct sockaddr *sa, sctp_assoc_t assoc_id, 651035918f85SRandall Stewart uint32_t vrf_id, int *error) 651135918f85SRandall Stewart { 651235918f85SRandall Stewart struct sockaddr *addr_touse; 65135e2c2d87SRandall Stewart 65145e2c2d87SRandall Stewart #ifdef INET6 651535918f85SRandall Stewart struct sockaddr_in sin; 651635918f85SRandall Stewart 65175e2c2d87SRandall Stewart #endif 65185e2c2d87SRandall Stewart 651935918f85SRandall Stewart /* see if we're bound all already! */ 652035918f85SRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 6521c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); 652235918f85SRandall Stewart *error = EINVAL; 652335918f85SRandall Stewart return; 652435918f85SRandall Stewart } 652535918f85SRandall Stewart addr_touse = sa; 6526e0e00a4dSMichael Tuexen #ifdef INET6 652735918f85SRandall Stewart if (sa->sa_family == AF_INET6) { 652835918f85SRandall Stewart struct sockaddr_in6 *sin6; 652935918f85SRandall Stewart 653035918f85SRandall Stewart if (sa->sa_len != sizeof(struct sockaddr_in6)) { 6531c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); 653235918f85SRandall Stewart *error = EINVAL; 653335918f85SRandall Stewart return; 653435918f85SRandall Stewart } 6535db4fd95bSRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) { 6536db4fd95bSRandall Stewart /* can only bind v6 on PF_INET6 sockets */ 6537c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); 6538db4fd95bSRandall Stewart *error = EINVAL; 6539db4fd95bSRandall Stewart return; 6540db4fd95bSRandall Stewart } 654135918f85SRandall Stewart sin6 = (struct sockaddr_in6 *)addr_touse; 654235918f85SRandall Stewart if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6543db4fd95bSRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && 6544db4fd95bSRandall Stewart SCTP_IPV6_V6ONLY(inp)) { 6545db4fd95bSRandall Stewart /* can't bind mapped-v4 on PF_INET sockets */ 6546c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); 6547db4fd95bSRandall Stewart *error = EINVAL; 6548db4fd95bSRandall Stewart return; 6549db4fd95bSRandall Stewart } 655035918f85SRandall Stewart in6_sin6_2_sin(&sin, sin6); 655135918f85SRandall Stewart addr_touse = (struct sockaddr *)&sin; 655235918f85SRandall Stewart } 655335918f85SRandall Stewart } 655435918f85SRandall Stewart #endif 6555ea5eba11SMichael Tuexen #ifdef INET 655635918f85SRandall Stewart if (sa->sa_family == AF_INET) { 655735918f85SRandall Stewart if (sa->sa_len != sizeof(struct sockaddr_in)) { 6558c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); 655935918f85SRandall Stewart *error = EINVAL; 656035918f85SRandall Stewart return; 656135918f85SRandall Stewart } 6562db4fd95bSRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && 6563db4fd95bSRandall Stewart SCTP_IPV6_V6ONLY(inp)) { 6564db4fd95bSRandall Stewart /* can't bind v4 on PF_INET sockets */ 6565c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL); 6566db4fd95bSRandall Stewart *error = EINVAL; 6567db4fd95bSRandall Stewart return; 6568db4fd95bSRandall Stewart } 656935918f85SRandall Stewart } 6570ea5eba11SMichael Tuexen #endif 657135918f85SRandall Stewart /* 657235918f85SRandall Stewart * No lock required mgmt_ep_sa does its own locking. If the FIX: 657335918f85SRandall Stewart * below is ever changed we may need to lock before calling 657435918f85SRandall Stewart * association level binding. 657535918f85SRandall Stewart */ 657635918f85SRandall Stewart if (assoc_id == 0) { 657735918f85SRandall Stewart /* delete the address */ 657835918f85SRandall Stewart *error = sctp_addr_mgmt_ep_sa(inp, addr_touse, 657935918f85SRandall Stewart SCTP_DEL_IP_ADDRESS, 658080fefe0aSRandall Stewart vrf_id, NULL); 658135918f85SRandall Stewart } else { 658235918f85SRandall Stewart /* 658335918f85SRandall Stewart * FIX: decide whether we allow assoc based bindx 658435918f85SRandall Stewart */ 658535918f85SRandall Stewart } 658635918f85SRandall Stewart } 65871b649582SRandall Stewart 65881b649582SRandall Stewart /* 65891b649582SRandall Stewart * returns the valid local address count for an assoc, taking into account 65901b649582SRandall Stewart * all scoping rules 65911b649582SRandall Stewart */ 65921b649582SRandall Stewart int 65931b649582SRandall Stewart sctp_local_addr_count(struct sctp_tcb *stcb) 65941b649582SRandall Stewart { 65951b649582SRandall Stewart int loopback_scope, ipv4_local_scope, local_scope, site_scope; 65961b649582SRandall Stewart int ipv4_addr_legal, ipv6_addr_legal; 65971b649582SRandall Stewart struct sctp_vrf *vrf; 65981b649582SRandall Stewart struct sctp_ifn *sctp_ifn; 65991b649582SRandall Stewart struct sctp_ifa *sctp_ifa; 66001b649582SRandall Stewart int count = 0; 66011b649582SRandall Stewart 66021b649582SRandall Stewart /* Turn on all the appropriate scopes */ 66031b649582SRandall Stewart loopback_scope = stcb->asoc.loopback_scope; 66041b649582SRandall Stewart ipv4_local_scope = stcb->asoc.ipv4_local_scope; 66051b649582SRandall Stewart local_scope = stcb->asoc.local_scope; 66061b649582SRandall Stewart site_scope = stcb->asoc.site_scope; 66071b649582SRandall Stewart ipv4_addr_legal = ipv6_addr_legal = 0; 66081b649582SRandall Stewart if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 66091b649582SRandall Stewart ipv6_addr_legal = 1; 66101b649582SRandall Stewart if (SCTP_IPV6_V6ONLY(stcb->sctp_ep) == 0) { 66111b649582SRandall Stewart ipv4_addr_legal = 1; 66121b649582SRandall Stewart } 66131b649582SRandall Stewart } else { 66141b649582SRandall Stewart ipv4_addr_legal = 1; 66151b649582SRandall Stewart } 66161b649582SRandall Stewart 6617c99efcf6SRandall Stewart SCTP_IPI_ADDR_RLOCK(); 66181b649582SRandall Stewart vrf = sctp_find_vrf(stcb->asoc.vrf_id); 66191b649582SRandall Stewart if (vrf == NULL) { 66201b649582SRandall Stewart /* no vrf, no addresses */ 6621c99efcf6SRandall Stewart SCTP_IPI_ADDR_RUNLOCK(); 66221b649582SRandall Stewart return (0); 66231b649582SRandall Stewart } 66241b649582SRandall Stewart if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 66251b649582SRandall Stewart /* 66261b649582SRandall Stewart * bound all case: go through all ifns on the vrf 66271b649582SRandall Stewart */ 66281b649582SRandall Stewart LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) { 66291b649582SRandall Stewart if ((loopback_scope == 0) && 66301b649582SRandall Stewart SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) { 66311b649582SRandall Stewart continue; 66321b649582SRandall Stewart } 66331b649582SRandall Stewart LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { 66341b649582SRandall Stewart if (sctp_is_addr_restricted(stcb, sctp_ifa)) 66351b649582SRandall Stewart continue; 66365e2c2d87SRandall Stewart switch (sctp_ifa->address.sa.sa_family) { 6637ea5eba11SMichael Tuexen #ifdef INET 66385e2c2d87SRandall Stewart case AF_INET: 66395e2c2d87SRandall Stewart if (ipv4_addr_legal) { 66401b649582SRandall Stewart struct sockaddr_in *sin; 66411b649582SRandall Stewart 66421b649582SRandall Stewart sin = (struct sockaddr_in *)&sctp_ifa->address.sa; 66431b649582SRandall Stewart if (sin->sin_addr.s_addr == 0) { 66445e2c2d87SRandall Stewart /* 66455e2c2d87SRandall Stewart * skip unspecified 66465e2c2d87SRandall Stewart * addrs 66475e2c2d87SRandall Stewart */ 66481b649582SRandall Stewart continue; 66491b649582SRandall Stewart } 66501b649582SRandall Stewart if ((ipv4_local_scope == 0) && 66511b649582SRandall Stewart (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) { 66521b649582SRandall Stewart continue; 66531b649582SRandall Stewart } 66541b649582SRandall Stewart /* count this one */ 66551b649582SRandall Stewart count++; 66565e2c2d87SRandall Stewart } else { 66575e2c2d87SRandall Stewart continue; 66585e2c2d87SRandall Stewart } 66595e2c2d87SRandall Stewart break; 6660ea5eba11SMichael Tuexen #endif 66615e2c2d87SRandall Stewart #ifdef INET6 66625e2c2d87SRandall Stewart case AF_INET6: 66635e2c2d87SRandall Stewart if (ipv6_addr_legal) { 66641b649582SRandall Stewart struct sockaddr_in6 *sin6; 66651b649582SRandall Stewart 66661b649582SRandall Stewart sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa; 66671b649582SRandall Stewart if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 66681b649582SRandall Stewart continue; 66691b649582SRandall Stewart } 66701b649582SRandall Stewart if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { 66711b649582SRandall Stewart if (local_scope == 0) 66721b649582SRandall Stewart continue; 66731b649582SRandall Stewart if (sin6->sin6_scope_id == 0) { 66741b649582SRandall Stewart if (sa6_recoverscope(sin6) != 0) 66751b649582SRandall Stewart /* 66765e2c2d87SRandall Stewart * 66775e2c2d87SRandall Stewart * bad 66785e2c2d87SRandall Stewart * 66795e2c2d87SRandall Stewart * li 66805e2c2d87SRandall Stewart * nk 66815e2c2d87SRandall Stewart * 66825e2c2d87SRandall Stewart * loc 66835e2c2d87SRandall Stewart * al 66845e2c2d87SRandall Stewart * 66855e2c2d87SRandall Stewart * add 66865e2c2d87SRandall Stewart * re 66875e2c2d87SRandall Stewart * ss 66885e2c2d87SRandall Stewart * */ 66891b649582SRandall Stewart continue; 66901b649582SRandall Stewart } 66911b649582SRandall Stewart } 66921b649582SRandall Stewart if ((site_scope == 0) && 66931b649582SRandall Stewart (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) { 66941b649582SRandall Stewart continue; 66951b649582SRandall Stewart } 66961b649582SRandall Stewart /* count this one */ 66971b649582SRandall Stewart count++; 66981b649582SRandall Stewart } 66995e2c2d87SRandall Stewart break; 67005e2c2d87SRandall Stewart #endif 67015e2c2d87SRandall Stewart default: 67025e2c2d87SRandall Stewart /* TSNH */ 67035e2c2d87SRandall Stewart break; 67045e2c2d87SRandall Stewart } 67051b649582SRandall Stewart } 67061b649582SRandall Stewart } 67071b649582SRandall Stewart } else { 67081b649582SRandall Stewart /* 67091b649582SRandall Stewart * subset bound case 67101b649582SRandall Stewart */ 67111b649582SRandall Stewart struct sctp_laddr *laddr; 67121b649582SRandall Stewart 67131b649582SRandall Stewart LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, 67141b649582SRandall Stewart sctp_nxt_addr) { 67151b649582SRandall Stewart if (sctp_is_addr_restricted(stcb, laddr->ifa)) { 67161b649582SRandall Stewart continue; 67171b649582SRandall Stewart } 67181b649582SRandall Stewart /* count this one */ 67191b649582SRandall Stewart count++; 67201b649582SRandall Stewart } 67211b649582SRandall Stewart } 6722c99efcf6SRandall Stewart SCTP_IPI_ADDR_RUNLOCK(); 67231b649582SRandall Stewart return (count); 67241b649582SRandall Stewart } 6725c4739e2fSRandall Stewart 6726c4739e2fSRandall Stewart #if defined(SCTP_LOCAL_TRACE_BUF) 6727c4739e2fSRandall Stewart 6728c4739e2fSRandall Stewart void 6729b27a6b7dSRandall 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) 6730c4739e2fSRandall Stewart { 6731b27a6b7dSRandall Stewart uint32_t saveindex, newindex; 6732c4739e2fSRandall Stewart 6733c4739e2fSRandall Stewart do { 6734b3f1ea41SRandall Stewart saveindex = SCTP_BASE_SYSCTL(sctp_log).index; 6735c4739e2fSRandall Stewart if (saveindex >= SCTP_MAX_LOGGING_SIZE) { 6736c4739e2fSRandall Stewart newindex = 1; 6737c4739e2fSRandall Stewart } else { 6738c4739e2fSRandall Stewart newindex = saveindex + 1; 6739c4739e2fSRandall Stewart } 6740b3f1ea41SRandall Stewart } while (atomic_cmpset_int(&SCTP_BASE_SYSCTL(sctp_log).index, saveindex, newindex) == 0); 6741c4739e2fSRandall Stewart if (saveindex >= SCTP_MAX_LOGGING_SIZE) { 6742c4739e2fSRandall Stewart saveindex = 0; 6743c4739e2fSRandall Stewart } 6744b3f1ea41SRandall Stewart SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].timestamp = SCTP_GET_CYCLECOUNT; 6745b3f1ea41SRandall Stewart SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].subsys = subsys; 6746b3f1ea41SRandall Stewart SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[0] = a; 6747b3f1ea41SRandall Stewart SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[1] = b; 6748b3f1ea41SRandall Stewart SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[2] = c; 6749b3f1ea41SRandall Stewart SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[3] = d; 6750b3f1ea41SRandall Stewart SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[4] = e; 6751b3f1ea41SRandall Stewart SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[5] = f; 6752c4739e2fSRandall Stewart } 6753c4739e2fSRandall Stewart 6754c4739e2fSRandall Stewart #endif 6755ea5eba11SMichael Tuexen /* XXX: Remove the #ifdef after tunneling over IPv6 works also on FreeBSD. */ 6756ea5eba11SMichael Tuexen #ifdef INET 6757c54a18d2SRandall Stewart /* We will need to add support 6758c54a18d2SRandall Stewart * to bind the ports and such here 6759c54a18d2SRandall Stewart * so we can do UDP tunneling. In 6760c54a18d2SRandall Stewart * the mean-time, we return error 6761c54a18d2SRandall Stewart */ 6762a99b6783SRandall Stewart #include <netinet/udp.h> 6763a99b6783SRandall Stewart #include <netinet/udp_var.h> 6764a99b6783SRandall Stewart #include <sys/proc.h> 6765a1f2f7a5SRandall Stewart #ifdef INET6 6766a99b6783SRandall Stewart #include <netinet6/sctp6_var.h> 6767a1f2f7a5SRandall Stewart #endif 6768a99b6783SRandall Stewart 6769a99b6783SRandall Stewart static void 6770a99b6783SRandall Stewart sctp_recv_udp_tunneled_packet(struct mbuf *m, int off, struct inpcb *ignored) 6771a99b6783SRandall Stewart { 6772a99b6783SRandall Stewart struct ip *iph; 6773a99b6783SRandall Stewart struct mbuf *sp, *last; 6774a99b6783SRandall Stewart struct udphdr *uhdr; 6775285052f0SMichael Tuexen uint16_t port; 6776a99b6783SRandall Stewart 6777a99b6783SRandall Stewart if ((m->m_flags & M_PKTHDR) == 0) { 6778a99b6783SRandall Stewart /* Can't handle one that is not a pkt hdr */ 6779a99b6783SRandall Stewart goto out; 6780a99b6783SRandall Stewart } 6781285052f0SMichael Tuexen /* Pull the src port */ 6782a99b6783SRandall Stewart iph = mtod(m, struct ip *); 6783a99b6783SRandall Stewart uhdr = (struct udphdr *)((caddr_t)iph + off); 6784a99b6783SRandall Stewart port = uhdr->uh_sport; 6785285052f0SMichael Tuexen /* 6786285052f0SMichael Tuexen * Split out the mbuf chain. Leave the IP header in m, place the 6787285052f0SMichael Tuexen * rest in the sp. 6788285052f0SMichael Tuexen */ 6789a99b6783SRandall Stewart sp = m_split(m, off, M_DONTWAIT); 6790a99b6783SRandall Stewart if (sp == NULL) { 6791a99b6783SRandall Stewart /* Gak, drop packet, we can't do a split */ 6792a99b6783SRandall Stewart goto out; 6793a99b6783SRandall Stewart } 6794285052f0SMichael Tuexen if (sp->m_pkthdr.len < sizeof(struct udphdr) + sizeof(struct sctphdr)) { 6795285052f0SMichael Tuexen /* Gak, packet can't have an SCTP header in it - too small */ 6796a99b6783SRandall Stewart m_freem(sp); 6797a99b6783SRandall Stewart goto out; 6798a99b6783SRandall Stewart } 6799285052f0SMichael Tuexen /* Now pull up the UDP header and SCTP header together */ 6800285052f0SMichael Tuexen sp = m_pullup(sp, sizeof(struct udphdr) + sizeof(struct sctphdr)); 6801a99b6783SRandall Stewart if (sp == NULL) { 6802a99b6783SRandall Stewart /* Gak pullup failed */ 6803a99b6783SRandall Stewart goto out; 6804a99b6783SRandall Stewart } 6805285052f0SMichael Tuexen /* Trim out the UDP header */ 6806a99b6783SRandall Stewart m_adj(sp, sizeof(struct udphdr)); 6807a99b6783SRandall Stewart 6808a99b6783SRandall Stewart /* Now reconstruct the mbuf chain */ 6809285052f0SMichael Tuexen for (last = m; last->m_next; last = last->m_next); 6810a99b6783SRandall Stewart last->m_next = sp; 6811a99b6783SRandall Stewart m->m_pkthdr.len += sp->m_pkthdr.len; 6812a99b6783SRandall Stewart iph = mtod(m, struct ip *); 6813a99b6783SRandall Stewart switch (iph->ip_v) { 6814e6194c2eSMichael Tuexen #ifdef INET 6815a99b6783SRandall Stewart case IPVERSION: 681609c1c856SMichael Tuexen iph->ip_len = htons(ntohs(iph->ip_len) - sizeof(struct udphdr)); 6817a99b6783SRandall Stewart sctp_input_with_port(m, off, port); 6818a99b6783SRandall Stewart break; 6819e6194c2eSMichael Tuexen #endif 6820a99b6783SRandall Stewart #ifdef INET6 6821a99b6783SRandall Stewart case IPV6_VERSION >> 4: 6822285052f0SMichael Tuexen /* Not yet supported. */ 6823a99b6783SRandall Stewart goto out; 6824a99b6783SRandall Stewart break; 6825a99b6783SRandall Stewart 6826a99b6783SRandall Stewart #endif 6827a99b6783SRandall Stewart default: 6828285052f0SMichael Tuexen goto out; 6829a99b6783SRandall Stewart break; 6830a99b6783SRandall Stewart } 6831a99b6783SRandall Stewart return; 6832a99b6783SRandall Stewart out: 6833a99b6783SRandall Stewart m_freem(m); 6834a99b6783SRandall Stewart } 6835c54a18d2SRandall Stewart 6836c54a18d2SRandall Stewart void 6837c54a18d2SRandall Stewart sctp_over_udp_stop(void) 6838c54a18d2SRandall Stewart { 6839a99b6783SRandall Stewart struct socket *sop; 6840a99b6783SRandall Stewart 6841a99b6783SRandall Stewart /* 6842a99b6783SRandall Stewart * This function assumes sysctl caller holds sctp_sysctl_info_lock() 6843a99b6783SRandall Stewart * for writting! 6844a99b6783SRandall Stewart */ 6845a99b6783SRandall Stewart if (SCTP_BASE_INFO(udp_tun_socket) == NULL) { 6846a99b6783SRandall Stewart /* Nothing to do */ 6847c54a18d2SRandall Stewart return; 6848c54a18d2SRandall Stewart } 6849a99b6783SRandall Stewart sop = SCTP_BASE_INFO(udp_tun_socket); 6850a99b6783SRandall Stewart soclose(sop); 6851a99b6783SRandall Stewart SCTP_BASE_INFO(udp_tun_socket) = NULL; 6852a99b6783SRandall Stewart } 6853ea5eba11SMichael Tuexen 6854c54a18d2SRandall Stewart int 6855c54a18d2SRandall Stewart sctp_over_udp_start(void) 6856c54a18d2SRandall Stewart { 6857a99b6783SRandall Stewart uint16_t port; 6858a99b6783SRandall Stewart int ret; 6859a99b6783SRandall Stewart struct sockaddr_in sin; 6860a99b6783SRandall Stewart struct socket *sop = NULL; 6861a99b6783SRandall Stewart struct thread *th; 6862a99b6783SRandall Stewart struct ucred *cred; 6863a99b6783SRandall Stewart 6864a99b6783SRandall Stewart /* 6865a99b6783SRandall Stewart * This function assumes sysctl caller holds sctp_sysctl_info_lock() 6866a99b6783SRandall Stewart * for writting! 6867a99b6783SRandall Stewart */ 6868a99b6783SRandall Stewart port = SCTP_BASE_SYSCTL(sctp_udp_tunneling_port); 6869a99b6783SRandall Stewart if (port == 0) { 6870a99b6783SRandall Stewart /* Must have a port set */ 6871a99b6783SRandall Stewart return (EINVAL); 6872a99b6783SRandall Stewart } 6873a99b6783SRandall Stewart if (SCTP_BASE_INFO(udp_tun_socket) != NULL) { 6874a99b6783SRandall Stewart /* Already running -- must stop first */ 6875a99b6783SRandall Stewart return (EALREADY); 6876a99b6783SRandall Stewart } 6877a99b6783SRandall Stewart th = curthread; 6878a99b6783SRandall Stewart cred = th->td_ucred; 6879a99b6783SRandall Stewart if ((ret = socreate(PF_INET, &sop, 6880a99b6783SRandall Stewart SOCK_DGRAM, IPPROTO_UDP, cred, th))) { 6881a99b6783SRandall Stewart return (ret); 6882a99b6783SRandall Stewart } 6883a99b6783SRandall Stewart SCTP_BASE_INFO(udp_tun_socket) = sop; 6884a99b6783SRandall Stewart /* call the special UDP hook */ 6885a99b6783SRandall Stewart ret = udp_set_kernel_tunneling(sop, sctp_recv_udp_tunneled_packet); 6886a99b6783SRandall Stewart if (ret) { 6887a99b6783SRandall Stewart goto exit_stage_left; 6888a99b6783SRandall Stewart } 6889a99b6783SRandall Stewart /* Ok we have a socket, bind it to the port */ 6890a99b6783SRandall Stewart memset(&sin, 0, sizeof(sin)); 6891a99b6783SRandall Stewart sin.sin_len = sizeof(sin); 6892a99b6783SRandall Stewart sin.sin_family = AF_INET; 6893a99b6783SRandall Stewart sin.sin_port = htons(port); 6894a99b6783SRandall Stewart ret = sobind(sop, (struct sockaddr *)&sin, th); 6895a99b6783SRandall Stewart if (ret) { 6896a99b6783SRandall Stewart /* Close up we cant get the port */ 6897a99b6783SRandall Stewart exit_stage_left: 6898a99b6783SRandall Stewart sctp_over_udp_stop(); 6899a99b6783SRandall Stewart return (ret); 6900a99b6783SRandall Stewart } 6901a99b6783SRandall Stewart /* 6902a99b6783SRandall Stewart * Ok we should now get UDP packets directly to our input routine 6903a99b6783SRandall Stewart * sctp_recv_upd_tunneled_packet(). 6904a99b6783SRandall Stewart */ 6905a99b6783SRandall Stewart return (0); 6906c54a18d2SRandall Stewart } 6907ea5eba11SMichael Tuexen 6908ea5eba11SMichael Tuexen #endif 6909